473,396 Members | 1,712 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,396 software developers and data experts.

CONSTRUCT - Module Attributes and Execution Environment

I would like to change the construct:

if __name__ == '__main__':

to something like:

if exec.isMain():

My (OO thought) is to place a class in an separate code module and to
instantiate an singleton instance which would keep th something like
this:

class ExecutionEnv:
def isMain(self)
if CALLING_MODULE.__name__ == '__main__':
return true
else
return false

exec = ExecutionEnv()

How to I get access to the CALLING_MODULE ?

-

Are ther alternative constructs/mechanism available, which could be
used to add this functionality possiby directly to a code-module?

Aug 22 '06 #1
11 1652

lazaridis_com wrote:
I would like to change the construct:

if __name__ == '__main__':

to something like:

if exec.isMain():

My (OO thought) is to place a class in an separate code module and to
instantiate an singleton instance which would keep th something like
this:

class ExecutionEnv:
def isMain(self)
if CALLING_MODULE.__name__ == '__main__':
return true
else
return false

exec = ExecutionEnv()

How to I get access to the CALLING_MODULE ?
sys._getframe(1).f_globals['__name__']

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
>
-

Are ther alternative constructs/mechanism available, which could be
used to add this functionality possiby directly to a code-module?
Aug 22 '06 #2
lazaridis_com wrote:
Are ther alternative constructs/mechanism available, which could be
used to add this functionality possiby directly to a code-module?
How about something along these lines:

------------------ auto.py ---------
import sys, atexit

def main_body(f):
if f.func_globals['__name__']=='__main__':
atexit.register(f, sys.argv)
return f

@main_body
def auto(args):
print "auto run", args
------------------------------------

If you run auto.py as a script then the decorated function executes. If you
import it then the decorated function doesn't execute. In your own script
you just need an import statement and to put the decorator on your main
function.
Aug 22 '06 #3
lazaridis_com wrote:
I would like to change the construct:

if __name__ == '__main__':

to something like:

if exec.isMain():

My (OO thought) is to place a class in an separate code module and to
instantiate an singleton instance which would keep th something like
this:

class ExecutionEnv:
def isMain(self)
if CALLING_MODULE.__name__ == '__main__':
return true
else
return false

exec = ExecutionEnv()

How to I get access to the CALLING_MODULE ?

-

Are ther alternative constructs/mechanism available, which could be
used to add this functionality possiby directly to a code-module?
Two thoughts:

1) Don't call a class instance exec, it will mask the built-in
exec statement.

2) IMHO all the suggestions are way more complicated than
if __name__ == "__main__" and are not SOP for most pythoneers.
I know what the former construct means/does. I have to
decipher your class to figure our what the latter does and it
doesn't really save you any code or provide a performance
enhancement.

-Larry
Aug 22 '06 #4
Larry Bates wrote:
lazaridis_com wrote:
>I would like to change the construct:

if __name__ == '__main__':

to something like:

if exec.isMain():

My (OO thought) is to place a class in an separate code module and to
instantiate an singleton instance which would keep th something like
this:

class ExecutionEnv:
def isMain(self)
if CALLING_MODULE.__name__ == '__main__':
return true
else
return false

exec = ExecutionEnv()

How to I get access to the CALLING_MODULE ?

-

Are ther alternative constructs/mechanism available, which could be
used to add this functionality possiby directly to a code-module?
Two thoughts:

1) Don't call a class instance exec, it will mask the built-in
exec statement.
He won't be able to ;)

Georg
Aug 22 '06 #5
Georg Brandl wrote:
Larry Bates wrote:
>lazaridis_com wrote:
>>I would like to change the construct:

if __name__ == '__main__':

to something like:

if exec.isMain():

My (OO thought) is to place a class in an separate code module and to
instantiate an singleton instance which would keep th something like
this:

class ExecutionEnv:
def isMain(self)
if CALLING_MODULE.__name__ == '__main__':
return true
else
return false

exec = ExecutionEnv()

How to I get access to the CALLING_MODULE ?

-

Are ther alternative constructs/mechanism available, which could be
used to add this functionality possiby directly to a code-module?
Two thoughts:

1) Don't call a class instance exec, it will mask the built-in
exec statement.

He won't be able to ;)

Georg
You are correct exec is a reserved word and can't be used as a
variable name. Thanks for pointing that out.

-Larry
Aug 23 '06 #6
Fuzzyman wrote:
lazaridis_com wrote:
I would like to change the construct:

if __name__ == '__main__':

to something like:

if exec.isMain():

My (OO thought) is to place a class in an separate code module and to
instantiate an singleton instance which would keep th something like
this:

class ExecutionEnv:
def isMain(self)
if CALLING_MODULE.__name__ == '__main__':
return true
else
return false

exec = ExecutionEnv()

How to I get access to the CALLING_MODULE ?

sys._getframe(1).f_globals['__name__']
very nice!

This seems to do the work.

Btw: I forgot to mention the use-case:

http://case.lazaridis.com/browser/la...lker.py?rev=44
All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
Aug 25 '06 #7

Duncan Booth wrote:
lazaridis_com wrote:
Are ther alternative constructs/mechanism available, which could be
used to add this functionality possiby directly to a code-module?

How about something along these lines:

------------------ auto.py ---------
import sys, atexit

def main_body(f):
if f.func_globals['__name__']=='__main__':
atexit.register(f, sys.argv)
return f

@main_body
def auto(args):
print "auto run", args
------------------------------------

If you run auto.py as a script then the decorated function executes. If you
import it then the decorated function doesn't execute. In your own script
you just need an import statement and to put the decorator on your main
function.
This construct looks very promising !

Are function decorators available in Ruby / Java / C++ ?

Aug 25 '06 #8
Larry Bates wrote:
lazaridis_com wrote:
I would like to change the construct:

if __name__ == '__main__':

to something like:

if exec.isMain():

My (OO thought) is to place a class in an separate code module and to
instantiate an singleton instance which would keep th something like
this:

class ExecutionEnv:
def isMain(self)
if CALLING_MODULE.__name__ == '__main__':
return true
else
return false

exec = ExecutionEnv()

How to I get access to the CALLING_MODULE ?

-

Are ther alternative constructs/mechanism available, which could be
used to add this functionality possiby directly to a code-module?
Two thoughts:

1) Don't call a class instance exec, it will mask the built-in
exec statement.
ok, I understand.
2) IMHO all the suggestions are way more complicated than
if __name__ == "__main__" and are not SOP for most pythoneers.
I know what the former construct means/does. I have to
decipher your class to figure our what the latter does and it
doesn't really save you any code or provide a performance
enhancement.
"Clarity for Pythoneers" is not a main requirement of the project.

The main requirements (The network of requirements is not yet fully
documented):

http://case.lazaridis.com/wiki/Code

The related issue:

http://case.lazaridis.com/ticket/5
-Larry
Aug 25 '06 #9

lazaridis_com wrote:
I would like to change the construct:

if __name__ == '__main__':
....

Is there a standard way / naming to wrap "__name__" and other similar
attributes to an encapsulating class?

Something like e.g.:

if mod.name ...

or

if gbl.name ...

-

"gbl.newAttribute = value" would create __newAttribute__

This should become available whereever such __<attributes>__ occour.

(I assume this should be implementable with e.g. metaclasses)

Aug 27 '06 #10
Larry Bates wrote:
1) Don't call a class instance exec, it will mask the built-in
exec statement.
"exec" is a reserved word, and cannot be masked:
>>exec = 10
File "<stdin>", line 1
exec = 10
^
SyntaxError: invalid syntax

</F>

Aug 27 '06 #11

lazaridis_com wrote:
lazaridis_com wrote:
I would like to change the construct:

if __name__ == '__main__':
...

Is there a standard way / naming to wrap "__name__" and other similar
attributes to an encapsulating class?
....

see follow-up thread:
http://groups.google.com/group/comp....905d66ae0e37c4

Sep 3 '06 #12

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

Similar topics

1
by: Peter Åstrand | last post by:
There's a new PEP available: PEP 324: popen5 - New POSIX process module A copy is included below. Comments are appreciated. ---- PEP: 324 Title: popen5 - New POSIX process module
0
by: Robert Brewer | last post by:
I finally cleaned up the code I've been using for a while now to grab objects out of modules via dotted-package names (all this work to avoid using exec! Sheesh!). Feel free to use it, abuse it, or...
18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
75
by: Beni | last post by:
I have been programming in C for about a year now. It sounds silly, but I never took the time to question why a C(or C++ or Java) program execution begins only at the main(). Is it a convention or...
17
by: Mike Hofer | last post by:
While I'd toyed with C, C++, and Java over the last 20 years or so, my principal language has been BASIC, QBASIC, then Visual Basic, and finally Visual Basic .NET. But lately, I've been using C#...
3
by: Good Enchiladas | last post by:
Is it possible to determine a method's own attributes from within the method without hardcoding the name of the method as a constant within the method? Please tell me if it is possible and how it...
11
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.