accesibility of the namespace

P

Petr Jakes

In my code I have relatively wide dictionary definition (about 100
rows).

I would like to put it in to the different file (module) because of the
main code readability (say the name of the file will be
"my_dictionary.py")

In the dictionary I have strings formatted using % operator like:
lcd={2:"Your credit= %3d" % (credit)}

While I am trying to import my_dictionary in to the main code, I am
getting:

exception unhandled NameError
name "credit" is not defined

How can I organize my code so the "credit" variable will be "visible"
in the "my_dictionary" namespace?

Thanks

Petr Jakes
 
S

Steven D'Aprano

In my code I have relatively wide dictionary definition (about 100
rows).

I would like to put it in to the different file (module) because of the
main code readability (say the name of the file will be
"my_dictionary.py")

In the dictionary I have strings formatted using % operator like:
lcd={2:"Your credit= %3d" % (credit)}

The values in the dictionary (e.g. "Your credit= 9.99", or whatever value
credit actually has) are fixed at creation. I assume that means that
credit etc. are also fixed values.

While I am trying to import my_dictionary in to the main code, I am
getting:

exception unhandled NameError
name "credit" is not defined

How can I organize my code so the "credit" variable will be "visible"
in the "my_dictionary" namespace?

Put it in the same module as my_dictionary.

E.g.

# Module my_dictionary.py
# which I hope will have a more sensible name before being used
# for production-code
credit = 27
foo = 15
lcd = {2: "Your credit= %3d" % credit}

On the other hand, if credit is a calculated value, this might not be
an easy thing to do. In that case, you can do this:


# Module calculatevalues.py
credit = some_function()
foo = some_other_function()

# Module my_dictionary.py
import calculatevalues
lcd = {2: "Your credit= %3d" % calculatevalues.credit}



On the third hand, if the strings from the dictionary are supposed to be
changed at run-time (which sounds more sensible to me) then do this:

# Module my_dictionary.py
lcd = {2: "Your credit= %3d"}


# main program
import my_dictionary
.... lots of code here
credit = 27
.... more code
print lcd[2] % credit
 
P

Petr Jakes

Thanks Steven,
credit is mentioned to be a calculated value changed at run-time and
your "third hand" suggestion is exactly what I was looking for.

Petr Jakes
 
P

Petr Jakes

Ooooops. My keyboard (fingers) was faster than my mind :(
So....
There is more than one "run-time changed variable" in the dictionary
and not all strings in the dictionary are formatted using % operator.
Example:
lcd={
2:{2:(("Enter you choice"),("Your kredit= %3d" % (kredit)))},
4:{2:(("Your choice: %2s" % (keyboard)),("%-20s" % (actKeyboard)))}}

I do not know which variable(s) to use for the % operator in the time
of the call of the value (% formatted string) from the dictionary.

This is also the reason why the variable names are stored in the
dictionary with the strings.

Any other suggestions?
Thanks
Petr Jakes
 
M

Mikael Olofsson

Petr said:
Ooooops. My keyboard (fingers) was faster than my mind :(
So....
There is more than one "run-time changed variable" in the dictionary
and not all strings in the dictionary are formatted using % operator.
Example:
lcd={
2:{2:(("Enter you choice"),("Your kredit= %3d" % (kredit)))},
4:{2:(("Your choice: %2s" % (keyboard)),("%-20s" % (actKeyboard)))}}

I do not know which variable(s) to use for the % operator in the time
of the call of the value (% formatted string) from the dictionary.

This is also the reason why the variable names are stored in the
dictionary with the strings.

Any other suggestions?

So, use something like
2:{2:("Enter you choice","Your kredit= %(kredit)3d")},
4:{2:("Your choice: %(keyboard)2s" ,"%(actKeyboard)-20s")}}
>>> kredit = 7
>>> keyboard = 'aa'
>>> actKeyboard = 7654
>>> lcd[2][2][-1] % vars() 'Your kredit= 7'
>>> lcd[2][2][0] % vars() 'Enter you choice'
>>> lcd[4][2][0] % vars() 'Your choice: aa'
>>> lcd[4][2][1] % vars() '7654 '
>>>

HTH
/MiO
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,289
Messages
2,571,435
Members
48,121
Latest member
ColinHibne

Latest Threads

Top