473,416 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

accesibility of the namespace

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

Mar 9 '06 #1
4 996
On Thu, 09 Mar 2006 01:42:55 -0800, Petr Jakes wrote:
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


--
Steven.

Mar 9 '06 #2
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

Mar 9 '06 #3
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

Mar 9 '06 #4
Petr Jakes wrote:
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
lcd={ 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
Mar 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John L. Clark | last post by:
I am curious as to the rationale, and effect, of having default namespaces not applying (directly) to attributes (see http://www.w3.org/TR/REC-xml-names/#defaulting). Given an attribute without a...
25
by: kj | last post by:
Consider the following XML document: <?xml version='1.0' encoding='UTF-8'?> <bar:foo xmlns:bar='someuri'> <baz/> </bar:foo> What namespace does baz belong to? What is this namespace bound...
3
by: Mike Dickens | last post by:
hi, i'm sure this has come up before but havn't managed to find an answer. if i have the following xslt <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet method="xml" version="1.0"...
6
by: Don Wash | last post by:
Hi There! I just need some advice on Namespace management for creating reusable VB.NET applications. I would like my applications to have this namespace structure... Namespace MyCompany ...
8
by: Petter Reinholdtsen | last post by:
I ran into a problem on HP-UX 11.00 the other day, where it refused to compile a program using 'using namespace std;' at the top. The reason seem to be that the compiler refuses to accept 'using...
10
by: Monte Gardner | last post by:
I'm teaching a class on web authoring and html. I'm trying to find a good reading article on accesibility issues. I'm trying to keep the reading supplements to relatively light reading, so I...
7
by: Kevin Newman | last post by:
I've been toying with a namespace manager, and wanted to get some input. So what do you think? if (typeof com == 'undefined') var com = {}; if (!com.unFocus) com.unFocus = {}; ...
14
by: Tiraman | last post by:
Hi , I would like to use nested namespace . I have 3 namespace as dll's : Namespace A Namespace B Namespace C And i want to have some namespace that contain them all , some thing like
2
by: petinboy | last post by:
Hi All! I am developing an accessibility keyboard application with Visual Studio 2005 Professional Edition and with C#. The application is a keyboard with buttons that interacts with any...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.