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 1678
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_co m 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.newAttribu te = value" would create __newAttribute_ _
This should become available whereever such __<attributes>_ _ occour.
(I assume this should be implementable with e.g. metaclasses) 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 comment on it. I
realize many of you already do this on your own--this is just sugar for
the newbies. I'm releasing/announcing it for three reasons:
1. The ego rush of authorship, :D
2. I continually hear people asking for similar (if not...
|
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 a single
collections type, I should be proposing a new "namespaces" module
instead. Some of my reasons:
(1) Namespace is feeling less and less like a collection to me. Even
though it's still intended as a data-only structure, the use cases...
|
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 candidate for Python 3000 yet?
Chris
|
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 is there
some deeper underlying reason?
| |
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# and I
absolutely *love* it. It makes me think more about what I'm doing it
before I just spew code into the editor. I'm writing better code than
ever.
The only thing so far that I don't like about it is the switch
construct. I can't do this:
|
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 is done.
Thank you,
Kelly
|
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 the app is
concerned). The problem is, If someone makes a typo, they may get an
unexpected error due accidentally calling the original attribute
instead of the wrapped version. Does anyone have a simple solution for
this?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |