Re: [Tutor] Passing arguments to & running a python script on a remote machine from a python script on local machine .
On Wed, Sep 19, 2012 at 6:13 PM, eryksun wrote: > > cmd = 'python remote.py "%s" "%s" "%s"' % (arg1, arg2, arg3) > > try: > out = subprocess.check_output(['ssh', '%s@%s' % (user, hostname), > cmd]) I forgot you need to escape special characters in the arguments. You can add quoting and escape special characters at the same time with the undocumented function pipes.quote: import pipes args = tuple(pipes.quote(arg) for arg in (arg1, arg2, arg3)) cmd = 'python test.py %s %s %s' % args Notice there are no longer quotes around each %s in cmd. Python 3.3 will have shlex.quote: http://docs.python.org/dev/library/shlex.html#shlex.quote Also, if you don't care about the output, use subprocess.check_call() instead. However, the connection still waits for the remote shell's stdout to close. If remote.py is long-running, redirect its stdout to a log file or /dev/null and start the process in the background (&). For example: cmd = 'python remote.py %s %s %s >/dev/null &' % args With this command remote.py is put in the background, stdout closes, the forked sshd daemon on the server exits, and ssh on the client immediately returns. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] "bisect" vs "in"
Hi, I want to repeatedly search a list to test whether that list contains a given element X. Of course, I can use membership testing using "in" (list._contains__). But how is this method implemented? It seems to just walk through the whole list from beginning to end (see timeit code below). Is it faster to do a binary search on a sorted list, given that this task has to be done repeatedly? Can such a program be scaled to lists that do not fit into memory (maybe using shelve?)? This is the function I made: import bisect def binarySearch(haystack, needle): return haystack[bisect.bisect_right(haystack, needle) - 1] == needle # needle is at the beginning of the sequence >>> t = timeit.Timer("""import bisect def binarySearch(haystack, needle): return haystack[bisect.bisect_right(haystack, needle) - 1] == needle binarySearch(range(10**7), 0)""") >>> t.timeit(100) 16.737915573068676 >>> t2 = timeit.Timer("0 in range(10**7)") >>> t2.timeit(100) 16.304621956142682 >>> # needle is at the end of the sequence >>> t = timeit.Timer("""import bisect def binarySearch(haystack, needle): return haystack[bisect.bisect_right(haystack, needle) - 1] == needle binarySearch(range(10**7), 10**7-1)""") >>> t.timeit(100) 16.954997632380582 >>> t2 = timeit.Timer("10**7-1 in range(10**7)") >>> t2.timeit(100) 36.3686376341127 Thank you in advance! Regards, Albert-Jan ~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Passing arguments to & running a python script on a remote machine from a python script on local machine .
On Thu, Sep 20, 2012 at 3:43 AM, eryksun wrote: > On Wed, Sep 19, 2012 at 2:47 PM, ashish makani > wrote: > > > > I tried this > import os > os.system ("ssh remoteuser@remote python remote.py arg1 arg2 arg3") > > > > This worked, but if the arguments i tried to pass, had spaces, i was not > > able to 'escape' the spaces. > > Presuming "remote" has an SSH server running, and that a firewall > isn't blocking port 22, and that you've enabled logging in without a > password (i.e. ssh-keygen), try the following: > > import sys >> import subprocess >> user = remoteuser >> hostname = remote >> cmd = 'python remote.py "%s" "%s" "%s"' % (arg1, arg2, arg3) >> try: >> out = subprocess.check_output(['ssh', '%s@%s' % (user, >> hostname), cmd]) >> except subprocess.CalledProcessError as e: >> print >>sys.stderr, str(e) # handle/log the error, retry > > This worked like a charm. Thank you so much. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Passing arguments to & running a python script on a remote machine from a python script on local machine .
On Thu, Sep 20, 2012 at 6:25 AM, eryksun wrote: > > I forgot you need to escape special characters in the arguments. You > can add quoting and escape special characters at the same time with > the undocumented function pipes.quote: > > import pipes > > args = tuple(pipes.quote(arg) for arg in (arg1, arg2, arg3)) > cmd = 'python test.py %s %s %s' % args FYI, here's what pipes.quote does. If the string has any special characters, it first replaces any single quotes in the string with '"'"' and then wraps the string in single quotes. For example "abc'def" becomes "'abc'\"'\"'def'". The shell doesn't use special characters ($`\) in single-quoted strings. Source: def quote(file): """Return a shell-escaped version of the file string.""" for c in file: if c not in _safechars: break else: if not file: return "''" return file # use single quotes, and put single quotes into double quotes # the string $'b is then quoted as '$'"'"'b' return "'" + file.replace("'", "'\"'\"'") + "'" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] "bisect" vs "in"
Albert-Jan Roskam wrote: > I want to repeatedly search a list to test whether that list contains a > given element X. Of course, I can use membership testing using "in" > (list._contains__). But how is this method implemented? It seems to just > walk through the whole list from beginning to end (see timeit code below). Indeed. > Is it faster to do a binary search on a sorted list, given that this task > has to be done repeatedly? Much faster. You don't see that in your timings because you include the creation of the range(10**7) list in your measurement. If you used a set instead of a list you could use "in", and it would be even faster than bisect. > Can such a program be scaled to lists that do > not fit into memory (maybe using shelve?)? That would slowdown things a lot. Disk access is *much* slower than RAM. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] "bisect" vs "in"
On Thu, Sep 20, 2012 at 7:58 AM, Albert-Jan Roskam wrote: > > Can such a program be scaled to lists that do not fit into memory (maybe > using shelve?)? shelve uses a database (e.g. gdbm) based on hashing. It's an unordered mapping. Unlike a dict, the keys must be strings. http://docs.python.org/library/shelve GNU dbm: http://www.gnu.org.ua/software/gdbm/manual/html_node/Intro.html#SEC2 Hash tables: http://en.wikipedia.org/wiki/Hash_table ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Populating a list with object to be called by a class
Morning, I dug out some old code from 5 years ago to clean up and get in working order. It's a simple agent based model. I have class called Ant which contains all the ant-like functions. I have a list that tracks the ants but I did this is a very crude way. I basically copied and pasted everything in there like this: ants = [Ant("Red_1","Red","yellow_food"),Ant("Yellow_1","Yellow","red_food"), Ant("Red_2","Red","yellow_food"),Ant("Yellow_2","Yellow","red_food"), Ant("Red_3","Red","yellow_food"),Ant("Yellow_3","Yellow","red_food"), Ant("Red_4","Red","yellow_food"),Ant("Yellow_4","Yellow","red_food"), ...] I couldn't figure out how to populate the list from a user input. Say if the user wanted 50 Red and 50 Yellow ants. So it's hardcoded at 500 which is not an elegant solution. I went back to work on this over the past couple of days but still can't figure out how to populate the list so I end up with Red_1 then Red_2 etc... What would be a good python way to do this? Thank you for your time. ara -- Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis an sub cardine glacialis ursae. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Populating a list with object to be called by a class
On 9/20/2012 7:41 AM Ara Kooser said... Morning, I dug out some old code from 5 years ago to clean up and get in working order. It's a simple agent based model. I have class called Ant which contains all the ant-like functions. I have a list that tracks the ants but I did this is a very crude way. I basically copied and pasted everything in there like this: ants = [Ant("Red_1","Red","yellow_food"),Ant("Yellow_1","Yellow","red_food"), Ant("Red_2","Red","yellow_food"),Ant("Yellow_2","Yellow","red_food"), Ant("Red_3","Red","yellow_food"),Ant("Yellow_3","Yellow","red_food"), Ant("Red_4","Red","yellow_food"),Ant("Yellow_4","Yellow","red_food"), ...] I couldn't figure out how to populate the list from a user input. Say if the user wanted 50 Red and 50 Yellow ants. So it's hardcoded at 500 which is not an elegant solution. So, say the desired COUNT is 50, you could do: ants = zip( [ Ant("Red_%s" % c ,"Red","yellow_food") for c in range(COUNT) ], [ Ant("Yellow_%s" % c ,"Yellow","red_food") for c in range(COUNT) ]) Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Populating a list with object to be called by a class
On 20 September 2012 15:41, Ara Kooser wrote: > Morning, > > I dug out some old code from 5 years ago to clean up and get in working > order. It's a simple agent based model. I have class called Ant which > contains all the ant-like functions. > > I have a list that tracks the ants but I did this is a very crude way. I > basically copied and pasted everything in there like this: > > ants = > [Ant("Red_1","Red","yellow_food"),Ant("Yellow_1","Yellow","red_food"), > > Ant("Red_2","Red","yellow_food"),Ant("Yellow_2","Yellow","red_food"), > > Ant("Red_3","Red","yellow_food"),Ant("Yellow_3","Yellow","red_food"), > > Ant("Red_4","Red","yellow_food"),Ant("Yellow_4","Yellow","red_food"), > ...] > > I couldn't figure out how to populate the list from a user input. Say if > the user wanted 50 Red and 50 Yellow ants. So it's hardcoded at 500 which > is not an elegant solution. > > I went back to work on this over the past couple of days but still can't > figure out how to populate the list so I end up with Red_1 then Red_2 etc... > > What would be a good python way to do this? > How about this: numants = 500 ants = [] for i in range(1, numants+1): ants.append(Ant('red_%i' % i, 'Red', 'yellow_food')) ants.append(Ant('yellow_%i' % i, 'Yellow', 'red_food')) Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Populating a list with object to be called by a class
Oscar, Thanks for that. I feel a bit silly. I forgot about the % which was hanging me up. I am trying to be a better coder. More work ahead for me! ara On Thu, Sep 20, 2012 at 9:01 AM, Oscar Benjamin wrote: > On 20 September 2012 15:41, Ara Kooser wrote: > >> Morning, >> >> I dug out some old code from 5 years ago to clean up and get in working >> order. It's a simple agent based model. I have class called Ant which >> contains all the ant-like functions. >> >> I have a list that tracks the ants but I did this is a very crude way. I >> basically copied and pasted everything in there like this: >> >> ants = >> [Ant("Red_1","Red","yellow_food"),Ant("Yellow_1","Yellow","red_food"), >> >> Ant("Red_2","Red","yellow_food"),Ant("Yellow_2","Yellow","red_food"), >> >> Ant("Red_3","Red","yellow_food"),Ant("Yellow_3","Yellow","red_food"), >> >> Ant("Red_4","Red","yellow_food"),Ant("Yellow_4","Yellow","red_food"), >> ...] >> >> I couldn't figure out how to populate the list from a user input. Say if >> the user wanted 50 Red and 50 Yellow ants. So it's hardcoded at 500 which >> is not an elegant solution. >> >> I went back to work on this over the past couple of days but still can't >> figure out how to populate the list so I end up with Red_1 then Red_2 etc... >> >> What would be a good python way to do this? >> > > How about this: > > numants = 500 > > ants = [] > for i in range(1, numants+1): > ants.append(Ant('red_%i' % i, 'Red', 'yellow_food')) > ants.append(Ant('yellow_%i' % i, 'Yellow', 'red_food')) > > Oscar > -- Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis an sub cardine glacialis ursae. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Populating a list with object to be called by a class
On Thu, Sep 20, 2012 at 10:41 AM, Ara Kooser wrote: > > I have a list that tracks the ants but I did this is a very crude way. I > basically copied and pasted everything in there like this: > > ants = > [Ant("Red_1","Red","yellow_food"),Ant("Yellow_1","Yellow","red_food"), > Ant("Red_2","Red","yellow_food"),Ant("Yellow_2","Yellow","red_food"), Having the type as instance data is limiting. Consider creating the subclasses RedAnt and YellowAnt, instead. For example, "red_food" can be a default argument for YellowAnt. You can also use a class counter variable that gets incremented for each new ant. Then the names can be automatic, too, but with the option to use a specific name. class Ant(object): _count = 0 def __init__(self, name=None, food='food'): cls = self.__class__ if cls == Ant: raise NotImplementedError cls._count += 1 if name is None: self.name = "%s_%s" % (cls.__name__, cls._count) else: self.name = name self.food = food def __repr__(self): clsname = self.__class__.__name__ name = repr(self.name) food = repr(self.food) return "%s(%s, %s)" % (clsname, name, food) def __str__(self): return str(self.name) class RedAnt(Ant): def __init__(self, name=None, food="yellow_food"): super(RedAnt, self).__init__(name, food) class YellowAnt(Ant): def __init__(self, name=None, food="red_food"): super(YellowAnt, self).__init__(name, food) For example: >>> ants = [cls() for i in range(2) for cls in (RedAnt, YellowAnt)] >>> ants [RedAnt('RedAnt_1', 'yellow_food'), YellowAnt('YellowAnt_1', 'red_food'), RedAnt('RedAnt_2', 'yellow_food'), YellowAnt('YellowAnt_2', 'red_food')] >>> print ', '.join(str(a) for a in ants) RedAnt_1, YellowAnt_1, RedAnt_2, YellowAnt_2 >>> Ant() Traceback (most recent call last): File "", line 1, in File "", line 8, in __init__ NotImplementedError >>> RedAnt('Tom', 'mice') RedAnt('Tom', 'mice') >>> YellowAnt('Jerry', 'cheese') YellowAnt('Jerry', 'cheese') >>> RedAnt._count, YellowAnt._count, Ant._count (3, 3, 0) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Populating a list with object to be called by a class
On Thu, Sep 20, 2012 at 12:25 PM, eryksun wrote: > > cls._count += 1 I forgot the obligatory warning about class variables. The subclass gets a shallow copy of the parent class namespace. The in-place addition above works because numbers are immutable. However, an in-place operation on a mutable object would modify the value shared by the parent and all other subclasses. Simple example: >>> class X(object): data = [] >>> class Y(X): pass >>> Y.data += [1] >>> X.data # modified parent, too [1] But rebinding the attribute name is OK: >>> Y.data = Y.data + [2] # creates new list >>> Y.data [1, 2] >>> X.data [1] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Populating a list with object to be called by a class
On Thu, Sep 20, 2012 at 12:57 PM, eryksun wrote: > > I forgot the obligatory warning about class variables. The subclass > gets a shallow copy of the parent class namespace. The in-place ^^^ > >>> Y.data += [1] > >>> X.data # modified parent, too > [1] > >>> Y.data = Y.data + [2] # creates new list To clarify, it's not an actual dict.copy in the subclass. In fact, the atrribute is initially only in the parent __dict__, where it's found by object.__getattribute__. In the 2nd example, Y.data on the right-hand side is actually found in the parent class X, then the expression creates a new list which is stored back to Y. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Populating a list with object to be called by a class
Ara Kooser wrote: > Morning, > > I dug out some old code from 5 years ago to clean up and get in working > order. It's a simple agent based model. I have class called Ant which > contains all the ant-like functions. > > I have a list that tracks the ants but I did this is a very crude way. I > basically copied and pasted everything in there like this: > > ants = > [Ant("Red_1","Red","yellow_food"),Ant("Yellow_1","Yellow","red_food"), > > Ant("Red_2","Red","yellow_food"),Ant("Yellow_2","Yellow","red_food"), > > Ant("Red_3","Red","yellow_food"),Ant("Yellow_3","Yellow","red_food"), > > Ant("Red_4","Red","yellow_food"),Ant("Yellow_4","Yellow","red_food"), > ...] > > I couldn't figure out how to populate the list from a user input. Say if > the user wanted 50 Red and 50 Yellow ants. So it's hardcoded at 500 which > is not an elegant solution. > > I went back to work on this over the past couple of days but still can't > figure out how to populate the list so I end up with Red_1 then Red_2 > etc... How boooring. Would you like to be called homo_sapiens_853212547? > What would be a good python way to do this? import os import posixpath import random import urllib import textwrap class Ant: def __init__(self, name): self.name = name def fullname(self): return self.name + " " + self.family def __repr__(self): return self.fullname() class RedAnt(Ant): family = "McAnt" class YellowAnt(Ant): family = "O'Ant" NAMES_URL = "http://www.gro-scotland.gov.uk/files1/stats/pop-names-07-t4.csv"; NAMES_FILENAME = posixpath.basename(NAMES_URL) if not os.path.exists(NAMES_FILENAME): urllib.urlretrieve(NAMES_URL, NAMES_FILENAME) names = [] with open(NAMES_FILENAME) as f: for line in f: names += line.split(",")[0::3] names = filter(bool, names) ants = [random.choice((RedAnt, YellowAnt))(name) for name in random.sample(names, 20)] print textwrap.fill(str(ants), 50) Let's do a sample run of the script: $ python ants.py [Sharon McAnt, Shreeram O'Ant, John-Michael McAnt, Patricia McAnt, Lorne McAnt, Kelso O'Ant, Muryam O'Ant, Keilee O'Ant, Timothy O'Ant, Anastacia O'Ant, Zhong O'Ant, Lyndsay O'Ant, Michal McAnt, Emily McAnt, Juwairiyah McAnt, Sukhveer McAnt, Philippa McAnt, Mcbride McAnt, Kaidan McAnt, Sasha O'Ant] > Thank you for your time. You're welcome ;) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Understanding cProfile output
Hi, I am trying to understand the output of cProfile when run against my python code. The code is: from math import floor,sqrt count = int(floor(sqrt(10))) max_check = range(2,count+1) original_list = range(2,11) for j in max_check: temp_list=[] for i in original_list: if i%j==0 and jhttp://sprunge.us/SOEj. Now what I don't understand is what it means by *tottime=16.374 for function countPrime.py:1(). *I understand fine that it took around *0.320s for method append.* So, is 16.374 the total time my scripts takes but according to profiler the total time is 16.705. -- Ranveer Raghuwanshi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Passing arguments to & running a python script on a remote machine from a python script on local machine .
> I forgot you need to escape special characters in the arguments. You > can add quoting and escape special characters at the same time with > the undocumented function pipes.quote: > > import pipes > > args = tuple(pipes.quote(arg) for arg in (arg1, arg2, arg3)) > cmd = 'python test.py %s %s %s' % args > > Notice there are no longer quotes around each %s in cmd. Python 3.3 > will have shlex.quote: > > http://docs.python.org/dev/library/shlex.html#shlex.quote Why is ``pipes.quote`` undocumented? It's useful. -Modulok- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Populating a list with object to be called by a class
On 21/09/12 02:57, eryksun wrote: On Thu, Sep 20, 2012 at 12:25 PM, eryksun wrote: cls._count += 1 I forgot the obligatory warning about class variables. Python is not Java. In Python, classes are first-class objects (no pun intended) and can be assigned to variables the same as any other type. x = something_that_returns_a_float() # x is a float variable s = something_that_returns_a_string() # s is a string variable C = something_that_returns_a_class() # C is a class variable Preferred terminology is attribute, not variable. Class attributes live in the class and are shared across all instances. Instance attributes live in the instance and are not shared. The subclass gets a shallow copy of the parent class namespace. Not so much. py> class C(object): ... x = 1 ... py> class D(C): pass ... py> D.__dict__ == C.__dict__ False py> 'x' in D.__dict__ False -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Populating a list with object to be called by a class
On Thu, Sep 20, 2012 at 3:39 PM, Steven D'Aprano wrote: > On 21/09/12 02:57, eryksun wrote: >> >> On Thu, Sep 20, 2012 at 12:25 PM, eryksun wrote: > > Preferred terminology is attribute, not variable. Class attributes live > in the class and are shared across all instances. Instance attributes > live in the instance and are not shared. That's correct. I try to be careful about terminology, but I slip up from time to time. My first OO language was Java. >> The subclass gets a shallow copy of the parent class namespace. > > > Not so much. > > py> class C(object): > ... x = 1 > ... > py> class D(C): pass > ... > py> D.__dict__ == C.__dict__ > False > py> 'x' in D.__dict__ > False If you didn't notice, I followed up with a correction. But thanks. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Understanding cProfile output
On 21/09/12 04:58, ranveer raghuwanshi wrote: Hi, I am trying to understand the output of cProfile when run against my python code. The code is: [...] What the above code does is it counts the number of prime numbers less than 1,00,000. Now when I profile this code using *python -m cProfile -s time countPrime.py. *The output I get is http://sprunge.us/SOEj. The output is 13 lines. Please don't waste our time, and yours, publishing it on the web. For trivially small amounts of text like that, just paste it into the body of your email, like this: #result9592 90414 function calls in 16.705 seconds Ordered by: internal time ncalls tottime percall cumtime percall filename:lineno(function) 1 16.374 16.374 16.705 16.705 countPrime.py:1() 904070.3200.0000.3200.000 {method 'append' of 'list' objects} 20.0110.0050.0110.005 {range} 10.0000.0000.0000.000 {math.sqrt} 10.0000.0000.0000.000 {math.floor} 10.0000.0000.0000.000 {len} 10.0000.0000.0000.000 {method 'disable' of '_lsprof.Profiler' objects} This ensures that: * even when the pastebin expires, or the website is shut down, the email archives will include the information necessary to understand your question * people who have access to email but not the web (or their web access is restricted) can still understand your question, or respond Now what I don't understand is what it means by *tottime=16.374 for function countPrime.py:1(). *I understand fine that it took around *0.320s for method append.* So, is 16.374 the total time my scripts takes but according to profiler the total time is 16.705. I'm not entirely sure, I'd need to look at the source code of the profiler, but I expect that the discrepancy is probably due to the difference between elapsed wall-clock time and processor time. In other words: * it took 16.705 seconds of actual, physical time (as measured by a clock on the wall) to run your script; * but only 16.374 seconds of that was time spent by the processor actually executing your code; the other 0.331 was probably taken up by other processes running on your system, or the operating system itself. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Populating a list with object to be called by a class
On 21/09/12 03:18, eryksun wrote: On Thu, Sep 20, 2012 at 12:57 PM, eryksun wrote: I forgot the obligatory warning about class variables. The subclass gets a shallow copy of the parent class namespace. The in-place ^^^ >>> Y.data += [1] >>> X.data # modified parent, too [1] >>> Y.data = Y.data + [2] # creates new list To clarify, it's not an actual dict.copy in the subclass. Ah, I didn't notice you had corrected yourself before sending my own correction. In fact, the atrribute is initially only in the parent __dict__, where it's found by object.__getattribute__. In the 2nd example, Y.data on the right-hand side is actually found in the parent class X, then the expression creates a new list which is stored back to Y. Pretty much. Inheritance couldn't work correctly if subclasses made copies of their parent namespaces. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Understanding cProfile output
On 09/20/2012 03:56 PM, Steven D'Aprano wrote: > On 21/09/12 04:58, ranveer raghuwanshi wrote: >> Hi, >> >> I am trying to understand the output of cProfile when run against my >> python >> code. The code is: > [...] >> What the above code does is it counts the number of prime numbers >> less than >> 1,00,000. >> >> Now when I profile this code using *python -m cProfile -s time >> countPrime.py. > > > #result9592 > 90414 function calls in 16.705 seconds > >Ordered by: internal time > >ncalls tottime percall cumtime percall filename:lineno(function) > 1 16.374 16.374 16.705 16.705 countPrime.py:1() > 904070.3200.0000.3200.000 {method 'append' of > 'list' objects} > 20.0110.0050.0110.005 {range} > 10.0000.0000.0000.000 {math.sqrt} > 10.0000.0000.0000.000 {math.floor} > 10.0000.0000.0000.000 {len} > 10.0000.0000.0000.000 {method 'disable' of > '_lsprof.Profiler' objects} > > > > >> Now what I >> don't understand is what it means by *tottime=16.374 for function >> countPrime.py:1(). *I understand fine that it took around >> *0.320s >> for method append.* >> >> So, is 16.374 the total time my scripts takes but according to >> profiler the >> total time is 16.705. > I don't know the Python profiler, but I've worked with many others over the years (and even written one). It looks like it follows very reasonable conventions in its output. tottime is the total time spent in that function, including the functions it calls. cumtime is the cumulative time spent in that function, after deducting for any functions it may have called. So in approximate numbers, 16.705 = 16.374 + .320 + .011 Both columns are very useful, as is the ncalls column. However, the net result is you didn't learn very much about the efficiency of your algorithm. Are you looking for suggestions for speeding it up? Or suggestions for getting more interesting results from profiling? -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Understanding cProfile output
Thanks for the input everyone. @Dave, I basically implemented the sieve of eratosthenes to fiind the number of prime numbers in a given range. So, yes I am looking for suggestions to speed it up. On Fri, Sep 21, 2012 at 2:16 AM, Dave Angel wrote: > On 09/20/2012 03:56 PM, Steven D'Aprano wrote: > > On 21/09/12 04:58, ranveer raghuwanshi wrote: > >> Hi, > >> > >> I am trying to understand the output of cProfile when run against my > >> python > >> code. The code is: > > [...] > >> What the above code does is it counts the number of prime numbers > >> less than > >> 1,00,000. > >> > >> Now when I profile this code using *python -m cProfile -s time > >> countPrime.py. > > > > > > #result9592 > > 90414 function calls in 16.705 seconds > > > >Ordered by: internal time > > > >ncalls tottime percall cumtime percall filename:lineno(function) > > 1 16.374 16.374 16.705 16.705 countPrime.py:1() > > 904070.3200.0000.3200.000 {method 'append' of > > 'list' objects} > > 20.0110.0050.0110.005 {range} > > 10.0000.0000.0000.000 {math.sqrt} > > 10.0000.0000.0000.000 {math.floor} > > 10.0000.0000.0000.000 {len} > > 10.0000.0000.0000.000 {method 'disable' of > > '_lsprof.Profiler' objects} > > > > > > > > > >> Now what I > >> don't understand is what it means by *tottime=16.374 for function > >> countPrime.py:1(). *I understand fine that it took around > >> *0.320s > >> for method append.* > >> > >> So, is 16.374 the total time my scripts takes but according to > >> profiler the > >> total time is 16.705. > > > I don't know the Python profiler, but I've worked with many others over > the years (and even written one). It looks like it follows very > reasonable conventions in its output. > > tottime is the total time spent in that function, including the > functions it calls. cumtime is the cumulative time spent in that > function, after deducting for any functions it may have called. So in > approximate numbers, 16.705 = 16.374 + .320 + .011 > > Both columns are very useful, as is the ncalls column. However, the net > result is you didn't learn very much about the efficiency of your > algorithm. > > Are you looking for suggestions for speeding it up? Or suggestions for > getting more interesting results from profiling? > > > > -- > > DaveA > > -- Ranveer Raghuwanshi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Help
I'm having trouble with a small project. Here's what it needs to do:Has variables for window width and window height and creates a window of that size. Has variables which control the number of columns and number of rows. From this, create a checkboard pattern which evenly fills the window. On approximately 10% of the squares (chosen at random) draw an image which is sized to just fit inside a checkboard square (use the smallest dimension). Keep the program open for 3 seconds and then quit.I've attached the code I have so far."Pic" is just a place holder for an actual image.Not sure what to do with the while statements that are empty in the code.Thanks for the help,Brett# Lab 3: Game Board import pygame import time pygame.display.init() WinH = 600 WinW = 800 num_rows = 8 num_cols = 12 screen = pygame.display.set_mode((WinW,WinH)) screen.fill((128,128,128)) markSurf = pygame.image.load("#Pic") color = (0,0,0) cell_width = WinW/num_cols cell_height = WinH/num_rows while #... while #... pygame.draw.rect(screen, color, (cell_width,cell_height), 0) if color == (0,0,0): color = (0,255,0) else: color = (0,0,0) curRow = 0 while curRow < #: if curRow %2 = 0: color = (0,0,0) else: color = (0,255,0) curRow +=1 pygame.display.update() time.sleep(3) pygame.display.quit() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help
People on this list are not all receiving this via email. It is recommended that you post 1. plain text (instead of Rich Text or HTML) 2. Just include the code directly in the email if it is short (which this is) or post to a website like pastebin for large samples. I have included the code below for the benefit of readers that do not receive attachments. Brett Dailey wrote: > I'm having trouble with a small project. Here's what it needs to do: > > Has variables for window width and window height and creates a window of that > size. > Has variables which control the number of columns and number of rows. > From this, create a checkboard pattern which evenly fills the window. On > approximately 10% of the squares (chosen at random) draw an image which is > sized to just fit inside a checkboard square (use the smallest dimension). > Keep the program open for 3 seconds and then quit. > > I've attached the code I have so far. > "Pic" is just a place holder for an actual image. > Not sure what to do with the while statements that are empty in the code. > > > Thanks for the help, > Brett > # Lab 3: Game Board import pygame import time pygame.display.init() WinH = 600 WinW = 800 num_rows = 8 num_cols = 12 screen = pygame.display.set_mode((WinW,WinH)) screen.fill((128,128,128)) markSurf = pygame.image.load("#Pic") color = (0,0,0) cell_width = WinW/num_cols cell_height = WinH/num_rows while #... while #... pygame.draw.rect(screen, color, (cell_width,cell_height), 0) if color == (0,0,0): color = (0,255,0) else: color = (0,0,0) curRow = 0 while curRow < #: if curRow %2 = 0: color = (0,0,0) else: color = (0,255,0) curRow +=1 pygame.display.update() time.sleep(3) pygame.display.quit() This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help
On Thu, Sep 20, 2012 at 6:31 PM, Brett Dailey wrote: > > From this, create a checkboard pattern which evenly fills the window. On > approximately 10% of the squares (chosen at random) draw an image which is > sized to just fit inside a checkboard square (use the smallest dimension). > Keep the program open for 3 seconds and then quit. Do you need to preserve the aspect ratio of the image? If so, should the image be scaled or scaled/cropped? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help
On 21/09/12 08:54, Prasad, Ramit wrote: People on this list are not all receiving this via email. They're not? How else can you receive this? Unlike the main python-list, this isn't (as far as I know) mirrored on Usenet. I wonder under what circumstances people could read this email without seeing any attachments. It is recommended that you post 1. plain text (instead of Rich Text or HTML) Agreed. 2. Just include the code directly in the email if it is short (which this is) Agreed. or post to a website like pastebin for large samples. Not agreed. Pastebins expire, or their websites disappear. In three months or three years, code pasted to a bin will probably no longer exist and nobody reading this thread via the web archives will have any clue what is being talked about. Also, some people will be reading this thread via email but be behind a firewall that limits their access to the web so that they cannot reach the pastebin. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Understanding cProfile output
On 09/20/2012 05:15 PM, ranveer raghuwanshi wrote: > Thanks for the input everyone. > > @Dave, I basically implemented the sieve of eratosthenes to fiind the > number of prime numbers in a given range. So, yes I am looking for > suggestions to speed it up. > Please don't top-post on this forum. It puts your response out of order with the parts you quote. Put your remarks AFTER the quotes. There are some very clever algorithms out there, and probably the fastest is to download a list of primes from some website (I'm sure we could find it by searching) I don't pretend to know which is the best one. But if you just want your algorithm tweaked, I could suggest that you could greatly reduce the number of comparisons by doing the modulo only on the PRIMES below max_check. Reverse your two loops, so the inner loop is the one that loops over the divisors. Don't do all that list manipulation, just solve the problem. In the code below, your algorithm is prime1(). Then I have prime2() and prime3(), which are equivalent algorithms, yielding approximately 25- and 35-fold improvement. I'm sure there are better algorithms, and ones that don't involve such large lists. But these are true to the original concept, of the sieve. from math import floor,sqrt import time as timer def prime1(limit): count = int(floor(sqrt(10))) max_check = range(2,count+1) original_list = range(2,11) for j in max_check: temp_list=[] for i in original_list: if i%j==0 and jhttp://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.
> > You need to add the folder where python.exe is to your PATH variable. > > First you need to find the folder where python is on your computer. You can > do this from within a python script using the following two lines: > import sys > print sys.executable > > Once you've found the folder containing python.exe, add that folder to your > PATH variable: > http://code.google.com/p/tryton/wiki/AddingPythonToWindowsPath > http://showmedo.com/videotutorials/video?name=96&fromSeriesID=96 Thanks, I'll give it a shot tomorrow morning. Brain is fried right now. Greg > > Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor