Discussion:
[melbourne-pug] Variable Inheritence between modules. I have no idea!
David Crisp
2014-09-12 02:11:16 UTC
Permalink
Hello,

I've managed to muddle my way through python for the last while and have
finally come up with something I dont know how to deal with.

(I have included a simplified code group below that represents what m
trying to do and see)

I have a main module (test6.py) which calls a configuration module
(test8.py) and then a number of other modules (test7 etc) which use the
data from the configuration module. (ie: configuration.output returns
"excel")

when I run the code I get the following error:
C:\Python33\python.exe C:/Users/dcrisp/Documents/Python/gui/test6.py
sql
Traceback (most recent call last):
excel
File "C:/Users/dcrisp/Documents/Python/gui/test6.py", line 10,
in <module>
mainWin = test7.MainWindow()
File "C:\Users\dcrisp\Documents\Python\gui\test7.py", line 5,
in __init__
if configuration.input.upper() == "EXCEL":
NameError: global name 'configuration' is not defined

Which is telling me that configuration isnt a global ...

Help? please? OKay, an actual question.

How do I read the configuration opbject from within test7 when it is
called from test6? I dont really want to call it from every module that
needs it as there is meant to be some write back functionality happening
to a configuration file and if I try and do that from more than one entry
point I will end up writing a corrupted config back. So A single entry
point for configuration would be nice..

Or am I doing it wrong?

Whats the best way of doing what I want to do.

Again, if I havent asked the right questions, please guide and I will try
and provide the information you need.

Regards,
David Crisp

Three simplified files provided below.

Module 1:
test6.py
import test8
import test7

if __name__ == '__main__':
configuration = test8.client_configuration()

print(configuration.output)
print(configuration.input)

mainWin = test7.MainWindow()
pass

Module 2:
test7.py
class MainWindow():
def __init__(self):
if configuration.input.upper() == "EXCEL":
print("excel in")
elif configuration.input.upper() == "SQL":
print("SQL in")
else:
print("Inappropriate Configuration Set")


if __name__ == '__main__':
import test8
configuration = test8.client_configuration()
mainWin = MainWindow()
pass

Module 3:
test8.py
class client_configuration():
def __init__(self):
self.input = "excel"
self.output = "sql"

if __name__ == '__main__':
configuration = client_configuration()

print(configuration.input)
print(configuration.output)
Mike Dewhirst
2014-09-12 03:25:35 UTC
Permalink
Post by David Crisp
Hello,
I've managed to muddle my way through python for the last while and have
finally come up with something I dont know how to deal with.
(I have included a simplified code group below that represents what m
trying to do and see)
I have a main module (test6.py) which calls a configuration module
(test8.py) and then a number of other modules (test7 etc) which use the
data from the configuration module. (ie: configuration.output returns
"excel")
C:\Python33\python.exe C:/Users/dcrisp/Documents/Python/gui/test6.py
sql
excel
File "C:/Users/dcrisp/Documents/Python/gui/test6.py", line 10, in
<module>
mainWin = test7.MainWindow()
File "C:\Users\dcrisp\Documents\Python\gui\test7.py", line 5, in
__init__
NameError: global name 'configuration' is not defined
Which is telling me that configuration isnt a global ...
Help? please? OKay, an actual question.
How do I read the configuration opbject from within test7 when it is
called from test6? I dont really want to call it from every module
that needs it as there is meant to be some write back functionality
happening to a configuration file and if I try and do that from more
than one entry point I will end up writing a corrupted config back. So
A single entry point for configuration would be nice..
Or am I doing it wrong?
Whats the best way of doing what I want to do.
Again, if I havent asked the right questions, please guide and I will
try and provide the information you need.
Regards,
David Crisp
Three simplified files provided below.
test6.py
import test8
import test7
configuration = test8.client_configuration()
print(configuration.output)
print(configuration.input)
mainWin = test7.MainWindow()
pass
test7.py
This class doesn't get "configuration" declared or passed in or
otherwise made available to it. You could do ...

def __init__(self, configuration):

... which would at least generate an error if you don't pass it in.
Post by David Crisp
print("excel in")
print("SQL in")
print("Inappropriate Configuration Set")
When running test6 the following code in test7 never runs. It only runs
if test7 is run independently.
Post by David Crisp
import test8
configuration = test8.client_configuration()
mainWin = MainWindow()
pass
test8.py
self.input = "excel"
self.output = "sql"
ditto for test8
Post by David Crisp
configuration = client_configuration()
print(configuration.input)
print(configuration.output)
_______________________________________________
melbourne-pug mailing list
melbourne-pug at python.org
https://mail.python.org/mailman/listinfo/melbourne-pug
David Crisp
2014-09-12 03:43:08 UTC
Permalink
Post by David Crisp
Hello,
This class doesn't get "configuration" declared or passed in or otherwise
made available to it. You could do ...
... which would at least generate an error if you don't pass it in.
Post by David Crisp
print("excel in")
print("SQL in")
print("Inappropriate Configuration Set")
When running test6 the following code in test7 never runs. It only runs if
test7 is run independently.
Sorry, yes, I realised this. I left the testing functions in..

Thanks!
Regards,
David
William ML Leslie
2014-09-12 03:44:17 UTC
Permalink
Post by David Crisp
How do I read the configuration opbject from within test7 when it is
called from test6? I dont really want to call it from every module that
needs it as there is meant to be some write back functionality happening to
a configuration file and if I try and do that from more than one entry
point I will end up writing a corrupted config back. So A single entry
point for configuration would be nice..
??
?What makes you think it will be corrupted? Have you tried it??


?There are no globals in python, really, besides the module system (every
import of module 'x' will give the same object(s)), but you can even work
around that.
Post by David Crisp
??
Or am I doing it wrong?
Whats the best way of doing what I want to do.
Again, if I havent asked the right questions, please guide and I will try
and provide the information you need.
?Why not:?

test8.py
class ClientConfiguration:
def __init__(self):
self.input = "excel"
self.output = "sql"

configuration = ClientConfiguration()

?then there's one configuration object that everyone can use (or not use,
if they like):

from test8 import configuration?

Current best practice, as documented by
https://glyph.twistedmatrix.com/2007/07/functional-functions-and-python.html
is to have clean places to override the usage of the module, that's the
'best way' really.
--
William Leslie

Notice:
Likely much of this email is, by the nature of copyright, covered under
copyright law. You absolutely MAY reproduce any part of it in accordance
with the copyright law of the nation you are reading this in. Any attempt
to DENY YOU THOSE RIGHTS would be illegal without prior contractual
agreement.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/melbourne-pug/attachments/20140912/a671e797/attachment-0001.html>
David Crisp
2014-09-12 04:29:25 UTC
Permalink
Post by William ML Leslie
Post by David Crisp
How do I read the configuration opbject from within test7 when it is
called from test6? I dont really want to call it from every module that
needs it as there is meant to be some write back functionality happening to
a configuration file and if I try and do that from more than one entry
point I will end up writing a corrupted config back. So A single entry
point for configuration would be nice..
??
?What makes you think it will be corrupted? Have you tried it??
No I havent tried it. I just ASSumed.
Post by William ML Leslie
?There are no globals in python, really, besides the module system (every
import of module 'x' will give the same object(s)), but you can even work
around that.
The other way I could sort this out.. probably.. is to have everything in
the module. (But I Asthetically I do like having my classes in seperate
files (but not 100%... I tend to group functionality into files))

Regards,
David

Loading...