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? 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?
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.
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
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
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
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
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++ ?
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
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)
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>
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
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...
|
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...
|
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...
|
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...
|
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#...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |