Python Forum
DEC pack, unpack and disk-images
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DEC pack, unpack and disk-images
#21
Apologies, what frees the "table" variable memory, the exit of the calling code?
Reply
#22
(Jun-18-2024, 06:52 AM)Curbie Wrote: Apologies, what frees the "table" variable memory, the exit of the calling code?
The 256 bytes are freed when the program exits because 'table' is a global variable. You can free them earlier if you don't need to pack() or unpack() anymore. You can free them by deleting the 'table' variable or assigning None to this variable.

There is little benefit in doing this because 256 bytes is a small amount of memory.
« We can solve any problem by introducing an extra level of indirection »
Reply
#23
Quote:because 'table' is a global variable
stupid question, are both 'table' and 'res' global variables, if not what makes a global variable?
Reply
#24
(Jun-18-2024, 11:37 PM)Curbie Wrote: stupid question, are both 'table' and 'res' global variables, if not what makes a global variable?
Yes they are global variables because they are defined at module level and not in the body of a function. In the following code
spam = 25

def eggs():
    ham = 36
spam is a global variable and ham is a local variable of function eggs(). When function eggs() is called, ham is destroyed at function exit. Spam is never destroyed, unless we write at module level
del spam
We could also do
spam = "...some memory intensive data..."

spam = None  # does not destroy the name 'spam' but destroys the value at which it points.
« We can solve any problem by introducing an extra level of indirection »
Reply
#25
Thanks,

If I understand correctly, local variables can ONLY be accessed by the function in which contains them, global variables are outside functions that all functions can access within that module. (and possibly importing modules, question to follow)

I think I understand that module functions can be called by either importing the whole module or the particular function from that module, but are global variables initialized if I import and call a particular function in a module, seems like they must be but, still trying to head-wrap this stuff?
Can Global variables from an imported module be accessed from the importing module?

I haven't found any py libraries tutorial, and I think I'll need to do a spread-sheet on and read every py library to document their content functions unless there is a better suggestion?
Reply
#26
(Jun-19-2024, 03:31 PM)Curbie Wrote: are global variables initialized if I import and call a particular function in a module, seems like they must be but, still trying to head-wrap this stuff?
Yes. The first time a pure Python module is imported, the code that it contains is executed, hence the global variables are assigned. Also note that functions and classes defined at module level are global variables too.
>>> def spam():
...     pass
... 
>>> spam
<function spam at 0x7ff2fb34db40>
(Jun-19-2024, 03:31 PM)Curbie Wrote: Can Global variables from an imported module be accessed from the importing module?
Yes.
(Jun-19-2024, 03:31 PM)Curbie Wrote: I haven't found any py libraries tutorial
Your first source of documentation is the official documentation, especially the standard library.
Searching the standard library can be a little tedious, you could use my 'pyman' script in a terminal. For example if you type pyman pathlib it will open the Python documentation with the search term pathlib.

A comprehensive introduction to the standard library's modules is the pyMOTW-3 site by Doug Hellmann. He gives example code for many standard modules.
« We can solve any problem by introducing an extra level of indirection »
Reply
#27
thanks Gribouillis,

In light of your "pyman" script and library references I my need to go back to my notes to see if i can create a local library text file of notes on py scripting and py libraries, although, I need work more work with debuggers before I do anything, tutorials show how py scripts work, not how to fix py scripts that don't work.

I need to ponder this some. more

curbie
Reply
#28
I decided to research python tutorials to start my python notes. Now, I'm reading them again, organizing, and structuring the tutorials in a way that settles my brain to suit my dyslexia, It's torture trying to stick to the intent of the tutorial author and not chase questions that the tutorial raises, but this organizing, and structuring phase should hopefully only take a couple months. Unlearning is the scariest.

To take a break from tutorials, I study python code, while studying Gribouillis's code I added a "print(x)" (see below) looking for an "illegal (127)" character. How "if illegal in x:" is raising this error, but the "print(x) is returning "b'\x14\x0f\r'" with NO illegal (127) character.

Now, I WAS playing with reorganizing the "rad50_table" so I'm sure this is my fault, and what "print(x) is returning is what I intended with no displayed illegal characters in 'x', so how is an error coming from 'x'?

class IllegalCharacter(Exception):
    print("Illegal Character from alphamicro.pack()")


def pack(c3):
    x = c3.translate(rad50_table)
    print(x)                                # added
    if illegal in x:
        raise IllegalCharacter
    return (1600 * x[0]) + (40 * x[1]) + x[2]


res = pack(b'tom')                          # pack ascii triplicate
print(res)                                  # H7F65  D32613  O77545 for 'tom'


Output:
Illegal Character from alphamicro.pack() b'\x14\x0f\r' 32613 Process finished with exit code 0
Reply
#29
(Jul-08-2024, 05:28 PM)Curbie Wrote: How "if illegal in x:" is raising this error, but the "print(x) is returning "b'\x14\x0f\r'" with NO illegal (127) character.
The if illegal in x is NOT raising an error. The message is printed because you added a print statement in the body of the IllegalCharacter class. The body of the class is executed unconditionally when the class is read by the interpreter. That print should not be here.
« We can solve any problem by introducing an extra level of indirection »
Reply
#30
Thanks Gribouillis,

I just reversed your "pack()" function for an "unpack()" function and it's doing the same "raise" thing, although it's rad50 functions flawlessly, I'll revisit error tutorials because I like what I understand of the "raise" notion.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mirroring disk structures in nested dictionaries Curbie 12 2,195 Oct-01-2024, 10:52 PM
Last Post: Curbie
  Windows Disk Cleanup Code Help Needed nal2us2 3 1,012 Jul-19-2024, 04:03 AM
Last Post: deanhystad
  Too much values to unpack actualpy 3 1,424 Feb-11-2024, 05:38 PM
Last Post: deanhystad
  Hard disk structure like a file selection dialog malonn 2 1,694 Aug-09-2023, 09:14 PM
Last Post: malonn
  unpack dict menator01 1 1,743 Apr-09-2022, 03:10 PM
Last Post: menator01
  ValueError: not enough values to unpack (expected 4, got 1) vlearner 2 8,230 Jan-28-2022, 06:36 PM
Last Post: deanhystad
  JS Buffer.from VS struct.pack DreamingInsanity 3 3,545 Apr-05-2021, 06:27 PM
Last Post: DreamingInsanity
  [SOLVED] [geopy] "ValueError: too many values to unpack (expected 2)" Winfried 2 3,695 Mar-30-2021, 07:01 PM
Last Post: Winfried
  Cannot unpack non-iterable NoneType object, i would like to ask for help on this. Jadiac 3 11,146 Oct-18-2020, 02:11 PM
Last Post: Jadiac
  subprogram issues: cannot unpack non-iterable function object error djwilson0495 13 8,670 Aug-20-2020, 05:53 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020