由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Python macro question
相关主题
error draw map from shape file Python 3.2 basemapPython的script的兼容问题
python要把@当作矩阵乘法算符python和java里面非memory资源怎么回收?
问一个python的string split问题python: 怎么避免memory leak?
python download pdf[合集] scipy还是matlab
Python:请问如何把list变成structured array。[合集] Python下面如何进行numeric analysis and statistical analysis
Python小问题python不支持多态
Python矩阵一问如何自学python
Python的问题关于python的优势
相关话题的讨论汇总
话题: floattype话题: python话题: float32话题: macro话题: global
进入Programming版参与讨论
1 (共1页)
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
4
Thanks!
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.

1 (共1页)
进入Programming版参与讨论
相关主题
关于python的优势Python:请问如何把list变成structured array。
python开发大型软件的可能性Python小问题
Access, Excel macro 高手看过来Python矩阵一问
很沮丧地问一个非常土的python问题Python的问题
error draw map from shape file Python 3.2 basemapPython的script的兼容问题
python要把@当作矩阵乘法算符python和java里面非memory资源怎么回收?
问一个python的string split问题python: 怎么避免memory leak?
python download pdf[合集] scipy还是matlab
相关话题的讨论汇总
话题: floattype话题: python话题: float32话题: macro话题: global