F********E 发帖数: 1025 | 1 Can python do something like this?
head.py:
***
#define FLOATTYPE=float32
***
source.py
***
a=numpy.array([1,2,3],numpy.FLOATTYPE)
***
Thanks! | a**a 发帖数: 416 | 2 You can do
head.py:
***
FLOATTYPE='float32'
***
source.py
***
a=numpy.array([1,2,3],getattr(numpy, FLOATTYPE))
*** | X****r 发帖数: 3557 | 3 Hum? What does this have anything to do with macro? In Python, types are
objects as well, so you can assign them to variables and use them there, e.g.
floattype = numpy.float32
a=numpy.array([1,2,3], floattype)
【在 F********E 的大作中提到】 : Can python do something like this? : head.py: : *** : #define FLOATTYPE=float32 : *** : source.py : *** : a=numpy.array([1,2,3],numpy.FLOATTYPE) : *** : Thanks!
| F********E 发帖数: 1025 | | F********E 发帖数: 1025 | 5 Thanks! But... come back to the script again:
head.py:
***
FLOATTYPE='float32'
***
source.py
***
a=numpy.array([1,2,3],getattr(numpy, FLOATTYPE))
***
source.py needs to import head.py to get the FLOATTYPE, but, the story is
head.py will invoke source.py, so, is there any way we can setup something
like macro so that source.py will know FLOATTYPE without passing or
importing anything?
.g.
【在 X****r 的大作中提到】 : Hum? What does this have anything to do with macro? In Python, types are : objects as well, so you can assign them to variables and use them there, e.g. : floattype = numpy.float32 : a=numpy.array([1,2,3], floattype)
| X****r 发帖数: 3557 | 6 Why head.py need to invoke source.py?
【在 F********E 的大作中提到】 : Thanks! But... come back to the script again: : head.py: : *** : FLOATTYPE='float32' : *** : source.py : *** : a=numpy.array([1,2,3],getattr(numpy, FLOATTYPE)) : *** : source.py needs to import head.py to get the FLOATTYPE, but, the story is
| F********E 发帖数: 1025 | 7 well, I am doing an automatic unit test for all the testing scripting under
a root. I built a driver at the root level which will invoke all the testing
scripts under each subdirectory.
I will set up a global name in the root driver, and want make sure all the
testing scripts under it will understand the global name.
【在 X****r 的大作中提到】 : Why head.py need to invoke source.py?
| X****r 发帖数: 3557 | 8 I see what you are trying to do. If you really want global names, import __
builtin__ (or builtin for python 3) and assign its property (e.g. FLOATTYPE)
, which will become global for all modules without further explicit imports.
But this is highly unrecommended -- global names are very hard to maintain.
I still don't get why can't you import head.py from source.py. If you'd like
your driver.py change the value on-the-fly (e.g. decide which type to use
from command-line arguments in driver.py), add a level of indirection:
instead of provide a constant, provide a setter and getter of the type, so
source.py can get the type after driver.py set the type, while the type
variable itself is stored in head.py.
under
testing
【在 F********E 的大作中提到】 : well, I am doing an automatic unit test for all the testing scripting under : a root. I built a driver at the root level which will invoke all the testing : scripts under each subdirectory. : I will set up a global name in the root driver, and want make sure all the : testing scripts under it will understand the global name.
| r****t 发帖数: 10904 | 9 OP's head.py cannot be imported into any .py file he/she needs to test.
There're plenty python test frameworks that do exactly what OP needs, why
not pick one..
FLOATTYPE)
imports.
maintain.
like
【在 X****r 的大作中提到】 : I see what you are trying to do. If you really want global names, import __ : builtin__ (or builtin for python 3) and assign its property (e.g. FLOATTYPE) : , which will become global for all modules without further explicit imports. : But this is highly unrecommended -- global names are very hard to maintain. : I still don't get why can't you import head.py from source.py. If you'd like : your driver.py change the value on-the-fly (e.g. decide which type to use : from command-line arguments in driver.py), add a level of indirection: : instead of provide a constant, provide a setter and getter of the type, so : source.py can get the type after driver.py set the type, while the type : variable itself is stored in head.py.
|
|