Re: Import without executing module
Can anyone explain what is the necessity of executing whole script when importing. Isn't it enough to just put the module name in the namespace and execute when some function is called? On Mon, Feb 2, 2009 at 1:36 PM, Stephen Hansen wrote: > > Maybe he can wrap the things he dont need inside > > if __name__ == '__main__': > > check. > > -- > > http://mail.python.org/mailman/listinfo/python-list > > > > Yeah but he said he doesn't want to modify the file itself-- if he can > modify the file this can all go away readily, yes. > > --S > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Import without executing module
On Mon, Feb 2, 2009 at 1:58 PM, Stephen Hansen wrote: > On Sun, Feb 1, 2009 at 11:43 PM, Taskinoor Hasan > wrote: > > Can anyone explain what is the necessity of executing whole script when > > importing. Isn't it enough to just put the module name in the namespace > and > > execute when some function is called? > > I'm not sure if I'm going to explain this right-- so bear with me. > > The code: > >def something(other): >return other + 1 > > Doesn't exist until that def statement is executed. "def", "class" and > such are not magical and immediately register something somewhere. Its > a statement that has to be executed to get a result, and that result > is bound as an assignment to the name specified. > > Its not really all that different from: >something = lambda other: other + 1 > > It can't pick through the top level instructions to determine what to > execute if imported vs run, as every statement has the possibility of > producing some binding of name to value and until you go and execute > it you won't know. You may do something like: > >try: >import blah >except: >blah = None > > You have to execute all of that to get a value for 'blah'. In Python, > there's nothing really special about a function vs any other value. > They are all objects that are assigned to a name in a given namespace. > You can do dynamic things at the top level to result in different > things being bound to the modules namespace based upon the statements > and expressions evaluated. > > Does that make sense? If not someone else'll have to explain :) It make sense :-). So my reasoning..let A is imported in B, i.e. name A is put in B's namespace. When we call something like A.a then the interpreter first resolve A in B's namespace, then to get a, it need to look up A's namespace. And there is no way to populate A's namespace without executing A, and as all statements, including 'def', 'class' etc., are treated in the same way, whole script need to be executed. So whether a script can be imported as module or not is mainly dependent on how the script is written and largely on the intention of the coder. > > > --Stephen > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Do more imported objects affect performance
On Mon, Dec 1, 2008 at 8:21 PM, Filip GruszczyĆski <[EMAIL PROTECTED]>wrote: > I see. Thanks for a really good explanation, I like to know, how to do > things in the proper way :) I always prefer to use import module and then use module.function. The reason is simple. It makes the code more readable and maintainable. > > > 2008/12/1 Nick Craig-Wood <[EMAIL PROTECTED]>: > > Rafe <[EMAIL PROTECTED]> wrote: > >> On Dec 1, 7:26?am, "Filip Gruszczy?ski" <[EMAIL PROTECTED]> wrote: > >> > I have following question: if I use > >> > > >> > from module import * > >> > > >> > instead > >> > > >> > from module import Class > >> > > >> > am I affecting performance of my program? I believe, that all those > >> > names must be stored somewhere, when they are imported and then > >> > browsed when one of them is called. So am I putting a lot of "garbage" > >> > to this storage and make those searches longer? > >> > >> Why use it if you don't need it? Your post implies a choice and the > >> '*' import can really make things muddy if it isn't actually necessary > >> (rare). Why not just import the module and use what you need? It is > >> way easier to read/debug and maintains the name-space. > > > > Importing the module is actualy slower... If you import the name into > > your namespace then there is only one lookup to do. If you import the > > module there are two. > > > > $ python -m timeit -s 'from timeit import Timer' 'Timer' > > 1000 loops, best of 3: 0.0784 usec per loop > > > > $ python -m timeit -s 'import timeit' 'timeit.Timer' > > 100 loops, best of 3: 0.243 usec per loop > > > > I'm not suggestion you should ever use "from module import *" only > > ever import the things you actually need, eg > > "from module import MyClass, my_function" > > > > And here is the test again, actually calling something with the same > > difference in execution speed :- > > > > $ python -m timeit -s 'from os import nice' 'nice(0)' > > 100 loops, best of 3: 1.21 usec per loop > > > > $ python -m timeit -s 'import os' 'os.nice(0)' > > 100 loops, best of 3: 1.48 usec per loop > > > > -- > > Nick Craig-Wood <[EMAIL PROTECTED]> -- http://www.craig-wood.com/nick > > -- > > http://mail.python.org/mailman/listinfo/python-list > > > > > > -- > Filip GruszczyĆski > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I return a non-zero status result from a python script?
sys.exit() raise SystemExit() exception which could be caught and if not caught, terminate only the current thread. If your program is multi-threaded and you want to terminate the process, i.e all threads, immediately then use os._exit(1) On Tue, Dec 16, 2008 at 7:52 AM, Miki wrote: > Hello, > > > How can I return a non-zero status result from the script? Just do a > > return 1? at the end? > raise SystemExit(1) > > HTH, > -- > Miki > http://pythonwise.blogspot.com > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I correctly download Wikipedia pages?
I fetched a different problem. Whenever I tried to fetch any page from wikipedia, I received 403. Then I found that wikipedia don't accept the default user-agent (might be python-urllib2.x or something like this). After setting my own user-agent, it worked fine. You can try this if you receive 403. On Thu, Nov 26, 2009 at 10:04 AM, Stephen Hansen wrote: > > > 2009/11/25 Steven D'Aprano > > I'm trying to scrape a Wikipedia page from Python. Following instructions >> here: >> >> > Have you checked out http://meta.wikimedia.org/wiki/Pywikipediabot? > > Its not just via urllib, but I've scraped several MediaWiki-based sites > with the software successfully. > > --S > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get memory and CPU status of a particular process
On Tue, Feb 23, 2010 at 9:48 AM, R. P. Janaka wrote: > Hi all, > > Is there a way to get system memory consumption and CPU consumption in a > platform independent way, using python...? > > Basically my requirement is, get the memory status and CPU status of a > particular process. If there is a way to get memory info and CPU info by > just giving the process ID, that is exactly what I need to do :) > Is this possible with python..? > I'm afraid that this is not possible in a platform independent way. In case of Windows, you can look at Tim Golden's WMI module for Python. And in case of Linux, you need to dig the details of /proc. Regards Taskinoor Hasan (Sajid) > > -- > Regards, > R. P. Janaka > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Calculating very large exponents in python
First of all, you simply can't use this straight approach of primality testing for very very big numbers. There are a number of algorithms, both deterministic and random. Please Google for them (and don't forget to check Wikipedia too). Study the random algorithms to check whether they can be applied in your situation, since they are faster. And another implementation issue. Try to avoid many recursive calls. It's always possible to convert a recursive function to a non-recursive one and that will be faster if your recursion is too long. Hope it helps. Regards Taskinoor Hasan (Sajid) On Mon, Mar 8, 2010 at 1:15 PM, Fahad Ahmad wrote: > Thanks Geremy, > > That has been an absolute bump... GOD i cant sit on my chair, it > has worked even on 512 bit number and with no time.. > superb i would say. > > lastly, i am using the code below to calculate Largest Prime factor of a > number: > > print > ('''===''' >''' CALCULATE HIGHEST PRIME > FACTOR ''' > > '''===''') > > #!/usr/bin/env python > def highest_prime_factor(n): >if isprime(n): > return n >for x in xrange(2,n ** 0.5 + 1): > if not n % x: > return highest_prime_factor(n/x) > def isprime(n): >for x in xrange(2,n ** 0.5 + 1): > if not n % x: > return False >return True > if __name__ == "__main__": >import time >start = time.time() >print highest_prime_factor(1238162376372637826) >print time.time() - start > > the code works with a bit of delay on the number : "1238162376372637826" > but extending it to > (109026109913291424366305511581086089650628117463925776754560048454991130443047109026109913291424366305511581086089650628117463925776754560048454991130443047) > makes python go crazy. Is there any way just like above, i can have it > calculated it in no time. > > > thanks for the support. > > > > > > Date: Sun, 7 Mar 2010 15:40:59 -0500 > > Subject: Re: Calculating very large exponents in python > > From: debat...@gmail.com > > To: miracles...@hotmail.com > > CC: python-list@python.org > > > > > On Sun, Mar 7, 2010 at 1:55 PM, Fahad Ahmad > wrote: > > > Dear All, > > > > > > i am writing my crytographic scheme in python, i am just a new user to > it. > > > I have written the complete code, the only point i am stuck it is that > i am > > > using 256 exponentiation which is normal in crytography but python just > > > hangs on it. > > > > > > g**x [where both g and x are 256 bit numbers , in decimal they are > around > > > 77] > > > > > > after reading several forums, i just come to know it can be done using > > > numpy, i have installed python(x,y) has has both numpy and scipy > installed > > > but i am not able to make it happen. > > > > > > any idea which library, module, or piece of code can solve this mystery > :S > > > > > > sorry for bad english > > > > A couple of things: > > > > 1) if you're working with modular exponentiation, remember that pow() > takes > > three arguments, ie: > > > > a = 222 > > b = > > pow(a, b, 1200) > > > > will calculate the correct answer (768) very quickly, while > > > > a**b % 1200 > > > > has not terminated in the time it took me to compose this > > email. > > > > 2) sage has a lot of excellent tools for crypto/cryptanalysis that you > > may want to take a look at. > > > > 3) not saying you don't know what you're doing, but be careful when > > rolling your own cryptosystems- even very good cryptographers make > > implementation mistakes! > > > > Geremy Condra > > -- > Hotmail: Powerful Free email with security by Microsoft. Get it > now.<http://clk.atdmt.com/GBL/go/201469230/direct/01/> > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- http://mail.python.org/mailman/listinfo/python-list