From wolfgang.maier at biologie.uni-freiburg.de Sun Jun 1 00:31:25 2014 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Sun, 01 Jun 2014 00:31:25 +0200 Subject: [Tutor] Break outside the loop error (line 23) In-Reply-To: References: Message-ID: <538A583D.40608@biologie.uni-freiburg.de> On 31.05.2014 11:23, Sasi - wrote: > Hi, i tried to make a code to execute the functions that i described > below. However i got a break outside the loop error and i have tried to > tab and all that but i still get the same error for line 23. I'm using > python 2.7 > Hi, as a general rule, copy paste the traceback of the actual python exception instead of summarizing it. In this specific case the error is obvious though (see below). > Please help me. I'm completely new to coding (just started last week) > Thank you for your time, i have pasted the code below. > > ## open both files > file1 = open('human_brain_proteins.csv') > file2 = open('human_plasma_proteins.csv') > #file1 = open('log1') > #file2 = open('log2') > > ## createdd a counter to count lines > count1 = 0 > count2 = 0 > > ## define 3 lists to be filled in later > common = list() > brain = list() > plasma = list() > > ## Created the lists for brain and plasma before searching for common > bioseq. > while True: > count1 += 1 > s1=file1.readline().partition(',')[0] your while loop ends here because the next line is not indented (probably not what you intended). > if s1 and count1 > 1: > brain.append(s1) > if not s1: > break since you are not inside the while loop anymore, there is nothing to break from. > while True: > count2 += 1 > s2=file2.readline().partition(',')[0] > if s2 and count2 > 1: > plasma.append(s2) > if not s2: > break > > ## Compared the lists to find out common bioseq., add the common bioseq. > into the common list, > ## then remove the common bioseq. from both lists > for item1 in brain: > for item2 in plasma: > if item1 == item2: > common.append(item1) > brain.remove(item1) > plasma.remove(item2) > > ## closed both files > file1.close() > file2.close() > > ## print out the lists > print "Common biosequence:" > print common,"\n" > print "Brain specific biosequence:" > print brain,"\n" > print "Plasma specific biosequence:" > print plasma,"\n" > additional suggestion: read about the set datatype of python and its intersection and difference methods (https://docs.python.org/2/library/stdtypes.html#set-types-set-frozenset). It looks like handling your data as two sets instead of two lists should be much more convenient. Best, Wolfgang From alan.gauld at btinternet.com Sun Jun 1 00:49:27 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 31 May 2014 23:49:27 +0100 Subject: [Tutor] Break outside the loop error (line 23) In-Reply-To: References: Message-ID: On 31/05/14 10:23, Sasi - wrote: > Hi, i tried to make a code to execute the functions that i described > below. However i got a break outside the loop error Always post the full error message it usually contains lots of useful detail. > ## Created the lists for brain and plasma before searching for common > bioseq. > while True: > count1 += 1 > s1=file1.readline().partition(',')[0] > if s1 and count1 > 1: This line is not indented so the loop stops on the previous line. > brain.append(s1) > if not s1: > break But your 'break' is here so it is outside the loop. You need to indent the whole section from if s1 and count1 > 1: so that it is inside the loop. > while True: > count2 += 1 > s2=file2.readline().partition(',')[0] > if s2 and count2 > 1: > plasma.append(s2) > if not s2: > break And the same problem here. The indentation defines what is inside the loop. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From ritwikraghav14 at gmail.com Sun Jun 1 06:27:37 2014 From: ritwikraghav14 at gmail.com (Ritwik Raghav) Date: Sun, 1 Jun 2014 09:57:37 +0530 Subject: [Tutor] self keyword in recursive function In-Reply-To: References: Message-ID: That's all the code I'm writing. As for the reference to line 182, I have no idea since my code doesn't have line 182. Also, as Peter wrote: "The "topcoder" site is probably infested with the world view of Java where every function is a method inside a class." Topcoder seems too much in love with Java style of coding and Python seems much better. I'm switching to some other platform. Thank You all for the quick response. On Sat, May 31, 2014 at 11:53 AM, Marc Tompkins wrote: > On Fri, May 30, 2014 at 11:06 PM, Ritwik Raghav > wrote: > >> That's all the code I'm writing. >> > > That can't be true - the 11 lines of code you posted doesn't include > anything that would give you "Correct Return Value: No", let alone any > reference to PersistentNumber. From the error message, it would appear > that you've written (at least) 182 lines, and that the problem is on line > 182 - but that's not what you're showing us. > > > >> The complete problem statement is: >> http://pastebin.com/E970qYXk >> > > Yes, but that's not _your_ code, so it tells us nothing about your error. > > > -- Ritwik Raghav -------------- next part -------------- An HTML attachment was scrubbed... URL: From ss3141 at att.com Sun Jun 1 09:05:45 2014 From: ss3141 at att.com (SABARWAL, SHAL) Date: Sun, 1 Jun 2014 07:05:45 +0000 Subject: [Tutor] cgi.FieldStorage() causing thread.error: can't allocate lock In-Reply-To: References: Message-ID: Folks, Appreciate any more insight into this problem. ? The error occurs not frequently, and would still like to understand and resolve it. Thanks Shal From: SABARWAL, SHAL Sent: Wednesday, May 28, 2014 5:07 PM To: 'tutor at python.org' Subject: RE: cgi.FieldStorage() causing thread.error: can't allocate lock Hi, Sorry for getting back so late on this. First time user of the mailing list, so was not aware of the protocol. Anyway the problem occurs not frequently. Haven't been able to recreate it on demand. The calling application(applicationCode.py) has a basic - form = cgi.FieldStorage() I have attached cgi.py, mimetools.py and tempfile.py The error is pointed out at the import statement as below File "applicationCode.py", line 4, in import cgi File "/usr/local/lib/python2.7/cgi.py", line 51, in import mimetools File "/usr/local/lib/python2.7/mimetools.py", line 6, in import tempfile File "/usr/local/lib/python2.7/tempfile.py", line 83, in _once_lock = _allocate_lock() Thanks Shal From: SABARWAL, SHAL Sent: Friday, May 23, 2014 7:57 AM To: tutor at python.org Subject: cgi.FieldStorage() causing thread.error: can't allocate lock Wondering if anyone came across this error in using form = cgi.FieldStorage() import tempfile File /tempfile.py", line 83, in _once_lock = _allocate_lock() thread.error: can't allocate lock puthon version 2.7, on HP-UX 11.11 -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sun Jun 1 12:20:00 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 01 Jun 2014 11:20:00 +0100 Subject: [Tutor] cgi.FieldStorage() causing thread.error: can't allocate lock In-Reply-To: References: Message-ID: On 01/06/14 08:05, SABARWAL, SHAL wrote: > Folks, > > Appreciate any more insight into this problem. ? > > The error occurs not frequently, and would still like to understand and > resolve it. > The calling application(applicationCode.py) has a basic - form = > cgi.FieldStorage() > > I have attached cgi.py, mimetools.py and tempfile.py The one file you have not shared is yours. What does applicationCode.py look like? I know you've told us it has a form = cgi.FieldStorage() call within it but it might help to see the context. Also did you try Dave's suggestion of importing tempfile in your code before calling the cgi functions. > The error is pointed out at the import statement as below > > File "applicationCode.py", line 4, in > import cgi > File "/usr/local/lib/python2.7/cgi.py", line 51, in > import mimetools > File "/usr/local/lib/python2.7/mimetools.py", line 6, in > import tempfile > File "/usr/local/lib/python2.7/tempfile.py", line 83, in > _once_lock = _allocate_lock() > thread.error: can't allocate lock -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From awg1 at gmx.com Sun Jun 1 14:01:23 2014 From: awg1 at gmx.com (Adam Gold) Date: Sun, 01 Jun 2014 13:01:23 +0100 Subject: [Tutor] gnupg within a for loop Message-ID: <538B1613.6000000@gmx.com> Hi there. I'm trying to do the following using python 3: create a list from all files in a particular directory, then loop over that list symmetrically encrypting each file using the gnupg module (note, for the moment I'm embedding the passphrase in the code while obviously in practice it would have to be inputted by the user). I start with the following which can be used to encrypt a single file (assume I have the indentations correct in the actual code, I can't seem to modify the wrapping with my email client): phrase = '12345' cipher = 'AES256' gpg = gnupg.GPG(gnupghome='/home/adam/.gnupg') with open('/home/adam/file1', 'rb') as f: status = gpg.encrypt_file(f, None, passphrase=phrase, symmetric=cipher.upper(), output='/home/adam/file1.gpg') This produces a single encrypted file, 'file1.gpg'. I then try to embed this in a for loop: unencrypted = [u for u in os.listdir('/home/adam/temp')] for G in unencrypted: gpg = gnupg.GPG(gnupghome='/home/adam/.gnupg') phrase = '12345' cipher = 'AES256' with open (G, 'rb') as f: status = gpg.encrypt_file(f, None, passphrase=phrase, symmetric=cipher.upper(), output=G + '.gpg') This produces the following error: Traceback (most recent call last): File "", line 5, in with open (G, 'rb') as f: FileNotFoundError: [Errno 2] No such file or directory: 'file1' It seems 'G', which I'm intending to represent each successive element of the list as the for loop iterates, does not find its way into the gnupg code lines. I have a feeling I'm missing something really basic here :) From jf_byrnes at comcast.net Sun Jun 1 18:53:12 2014 From: jf_byrnes at comcast.net (Jim Byrnes) Date: Sun, 01 Jun 2014 11:53:12 -0500 Subject: [Tutor] gnupg within a for loop In-Reply-To: <538B1613.6000000@gmx.com> References: <538B1613.6000000@gmx.com> Message-ID: On 06/01/2014 07:01 AM, Adam Gold wrote: > Hi there. I'm trying to do the following using python 3: create a list > from all files in a particular directory, then loop over that list > symmetrically encrypting each file using the gnupg module (note, for the > moment I'm embedding the passphrase in the code while obviously in > practice it would have to be inputted by the user). > > I start with the following which can be used to encrypt a single file > (assume I have the indentations correct in the actual code, I can't seem > to modify the wrapping with my email client): > > phrase = '12345' > cipher = 'AES256' > gpg = gnupg.GPG(gnupghome='/home/adam/.gnupg') > with open('/home/adam/file1', 'rb') as f: > status = gpg.encrypt_file(f, None, passphrase=phrase, > symmetric=cipher.upper(), output='/home/adam/file1.gpg') Here you are giving with open( ) a full path to file1 > > This produces a single encrypted file, 'file1.gpg'. I then try to embed > this in a for loop: > > unencrypted = [u for u in os.listdir('/home/adam/temp')] If I run this code with my info and print it, I get a list of files in my temp directory. > for G in unencrypted: > gpg = gnupg.GPG(gnupghome='/home/adam/.gnupg') > phrase = '12345' > cipher = 'AES256' > with open (G, 'rb') as f: > status = gpg.encrypt_file(f, None, passphrase=phrase, > symmetric=cipher.upper(), output=G + '.gpg') If I then do: for G in unencrypted: print(G) I get just a file name printed on each line. When you do: with open (G, 'rb') as f: I think you have lost the path info for the files. If you give with open() path info it should work. Hopefully if I am wrong someone with more knowledge will step in and give you a better answer. Regards, Jim > > This produces the following error: > Traceback (most recent call last): > File "", line 5, in > with open (G, 'rb') as f: > FileNotFoundError: [Errno 2] No such file or directory: 'file1' > > It seems 'G', which I'm intending to represent each successive element > of the list as the for loop iterates, does not find its way into the > gnupg code lines. I have a feeling I'm missing something really basic > here :) > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From dyoo at hashcollision.org Sun Jun 1 19:28:44 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 1 Jun 2014 10:28:44 -0700 Subject: [Tutor] gnupg within a for loop In-Reply-To: <538B1613.6000000@gmx.com> References: <538B1613.6000000@gmx.com> Message-ID: Hi Adam, Ah; I've seen this before. Make sure the file name is either relative to current working directory, or make the file name absolute. What's happening is that os.listdir() is giving you file names that are relative to the base directory you've passed it, but open() doesn't know about the base directory. You can use os.path.join() to create such paths: https://docs.python.org/2/library/os.path.html#os.path.join From dyoo at hashcollision.org Sun Jun 1 19:44:13 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 1 Jun 2014 10:44:13 -0700 Subject: [Tutor] cgi.FieldStorage() causing thread.error: can't allocate lock In-Reply-To: References: Message-ID: Hi Shal, You may want to report a bug on Python's bug tracker. What you're seeing is unusual enough to warrant sending a bug report upstream. Visit: http://bugs.python.org/ and send a report. In the report, please include all the information you've shown us here, and hopefully someone there can respond promptly to the problem. --- To others on this list: what's happening is all in the standard library. cgi imports mimetools, which imports tempfile. tempfile tries to allocate a lock at import time. The error we're seeing is a very low-level one: tempfile should never fail like this. From wolfgang.maier at biologie.uni-freiburg.de Sun Jun 1 18:22:30 2014 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Sun, 1 Jun 2014 16:22:30 +0000 (UTC) Subject: [Tutor] gnupg within a for loop References: <538B1613.6000000@gmx.com> Message-ID: Adam Gold gmx.com> writes: > > I start with the following which can be used to encrypt a single file > (assume I have the indentations correct in the actual code, I can't seem > to modify the wrapping with my email client): > > phrase = '12345' > cipher = 'AES256' > gpg = gnupg.GPG(gnupghome='/home/adam/.gnupg') > with open('/home/adam/file1', 'rb') as f: > status = gpg.encrypt_file(f, None, passphrase=phrase, > symmetric=cipher.upper(), output='/home/adam/file1.gpg') > > This produces a single encrypted file, 'file1.gpg'. I then try to embed > this in a for loop: > > unencrypted = [u for u in os.listdir('/home/adam/temp')] > for G in unencrypted: > gpg = gnupg.GPG(gnupghome='/home/adam/.gnupg') > phrase = '12345' > cipher = 'AES256' > with open (G, 'rb') as f: > status = gpg.encrypt_file(f, None, passphrase=phrase, > symmetric=cipher.upper(), output=G + '.gpg') > > This produces the following error: > Traceback (most recent call last): > File "", line 5, in > with open (G, 'rb') as f: > FileNotFoundError: [Errno 2] No such file or directory: 'file1' > > It seems 'G', which I'm intending to represent each successive element > of the list as the for loop iterates, does not find its way into the > gnupg code lines. I have a feeling I'm missing something really basic > here :) The really basic thing is that os.listdir returns the file names it finds in the specified directory, but that does not include the path to the file again. So the first element of unencrypted in your case is just 'file1' and that doesn't seem to exist in your working directory. You'll have to prepend your original path '/home/adam/temp/' to it again (via os.path.join for example). Best, Wolfgang From dyoo at hashcollision.org Sun Jun 1 19:51:29 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 1 Jun 2014 10:51:29 -0700 Subject: [Tutor] cgi.FieldStorage() causing thread.error: can't allocate lock In-Reply-To: References: Message-ID: On Sun, Jun 1, 2014 at 10:44 AM, Danny Yoo wrote: > Hi Shal, > > You may want to report a bug on Python's bug tracker. What you're > seeing is unusual enough to warrant sending a bug report upstream. > Visit: > > http://bugs.python.org/ > > and send a report. In the report, please include all the information > you've shown us here, and hopefully someone there can respond promptly > to the problem. Also, I see that you mention: > The error occurs not frequently, and would still like to understand and resolve it. In your bug report, If you can quantify how often this fails in terms of real numbers, that will be _very_ helpful. Is the system under heavy load when it fails? Or do the failures correlate with other activity? It may be that some system resource is being exhausted somehow. Perhaps the number of open files has gone beyond a certain limit. In searches for similar issues, I see things like: https://mail.python.org/pipermail/python-dev/2002-August/027799.html where it appears there's a very low system limit on HP systems with regards to number of open files at once. I can see scenarios where maxfiles may be exhausted, so that low-level issues start to occur, and in that scenario, I wouldn't be surprised to see errors like the ones you're seeing. From alan.gauld at btinternet.com Sun Jun 1 22:59:04 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 01 Jun 2014 21:59:04 +0100 Subject: [Tutor] gnupg within a for loop In-Reply-To: References: <538B1613.6000000@gmx.com> Message-ID: On 01/06/14 17:22, Wolfgang Maier wrote: > The really basic thing is that os.listdir returns the file names it finds in > the specified directory, but that does not include the path to the file > again. So the first element of unencrypted in your case is just 'file1' and > that doesn't seem to exist in your working directory. You'll have to prepend > your original path '/home/adam/temp/' to it again (via os.path.join for > example). An alternative is to use os.chdir() to set your working directory to /home/adam/temp That should also fix the problem. hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Mon Jun 2 02:35:44 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 1 Jun 2014 17:35:44 -0700 Subject: [Tutor] gnupg within a for loop In-Reply-To: <538BC399.809@gmx.com> References: <538B1613.6000000@gmx.com> <538BC399.809@gmx.com> Message-ID: > Thanks Danny, that was spot on. I actually used os.chdir to change to > the base directory (which I assigned to a variable) just before the open > statement. I don't know if that's 'pythonically' correct but it seemed > like a simple way to do it. Again, thank you for helping me fix this. I am glad! I would recommend using os.path.join instead to build absolute paths, as it's less fragile to external factors. I think of current working directory the same way I think of global variables: use it judiciously. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dragriesti at comcast.net Mon Jun 2 03:33:37 2014 From: dragriesti at comcast.net (Charles Agriesti) Date: Sun, 01 Jun 2014 20:33:37 -0500 Subject: [Tutor] Swampy: No module name World Message-ID: <538BD471.2040007@comcast.net> from swampy.World import World world = World() ImportError: No module name World These scripts run with no problem as long as the file location is the python27 folder. But not from outside the folder. Other modules, like math, import with no problem from other locations import math from math import pi 2*pi 6.28... how can I fix this? thanks in advance -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Mon Jun 2 04:55:19 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 2 Jun 2014 12:55:19 +1000 Subject: [Tutor] Swampy: No module name World In-Reply-To: <538BD471.2040007@comcast.net> References: <538BD471.2040007@comcast.net> Message-ID: <20140602025518.GP10355@ando> On Sun, Jun 01, 2014 at 08:33:37PM -0500, Charles Agriesti wrote: > from swampy.World import World > world = World() > > ImportError: No module name World > > These scripts run with no problem as long as the file location is the > python27 folder. But not from outside the folder. Where does swampy come from? Is it your module? Somebody else's? It looks to me like it is a bug in swampy. You are trying to use it as if it were a package containing a sub-module, but it isn't actually a package, just a folder full of modules. If swampy is somebody else's project, you should report this to them as a bug, but if it is yours, then you should be able to fix it by adding an empty __init__.py file inside the swampy folder. My guess is that you have a folder like this inside the python27 folder: python27 +-- [other python files] +-- swampy +-- World.py but you need this: python27 +-- [other python files] +-- swampy +-- __init__.py +-- World.py If this is not what you have, you will need to explain in more detail what the layout of your files is, where swampy came from, and where it is. -- Steven From davea at davea.name Mon Jun 2 11:10:55 2014 From: davea at davea.name (Dave Angel) Date: Mon, 2 Jun 2014 05:10:55 -0400 (EDT) Subject: [Tutor] Swampy: No module name World References: <538BD471.2040007@comcast.net> Message-ID: Charles Agriesti Wrote in message: > > from swampy.World import World > world = World() > ImportError: No module name World 1. please use text mail on this mailing-list. Your email software should have an option to change that. 2. You're referring to some nonstandard package called swampy, and the tutor list is dedicated to learning the language and standard library. And you don't even tell us where you got it; did you write it yourself? If not, you'd have better odds either on python-list or on a swampy forum. 3. There are a few standard places that python looks for packages and libraries. Clearly swampy is not installed in any of those places. Please reread the installation instructions for swampy. 4. If there are no such instructions, you can see the default list of places by examining sys.path. import sys print sys.path 5. Generally you should include the whole traceback, not just one line from it. In this case it didn't matter. -- DaveA From alan.gauld at btinternet.com Mon Jun 2 11:22:53 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 02 Jun 2014 10:22:53 +0100 Subject: [Tutor] Swampy: No module name World In-Reply-To: <538BD471.2040007@comcast.net> References: <538BD471.2040007@comcast.net> Message-ID: On 02/06/14 02:33, Charles Agriesti wrote: > from swampy.World import World > world = World() > > ImportError: No module name World > > These scripts run with no problem as long as the file location is the > python27 folder. But not from outside the folder. It sounds like you installed swampy in the wrong place. Python has a set of locations that it looks for modules. One is the current directory. So if, for example, swampy is in your Python27 folder that would explain it. > Other modules, like math, import with no problem from other locations Because the standard modules are in the location where Python looks for them. You can check the sys.path contents to see where Python is looking. If swampy is not in any of those locations Python won't se it. > how can I fix this? Probably by reinstalling swampy. But you'll need to check their web site. This list is for core Python and standard library. We can't offer much help for external modules. You are best asking on dedicated forums. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From awg1 at gmx.com Mon Jun 2 02:21:45 2014 From: awg1 at gmx.com (Adam Gold) Date: Mon, 02 Jun 2014 01:21:45 +0100 Subject: [Tutor] gnupg within a for loop In-Reply-To: References: <538B1613.6000000@gmx.com> Message-ID: <538BC399.809@gmx.com> On 01/06/14 18:28, Danny Yoo wrote: > Hi Adam, > > Ah; I've seen this before. Make sure the file name is either relative > to current working directory, or make the file name absolute. What's > happening is that os.listdir() is giving you file names that are > relative to the base directory you've passed it, but open() doesn't > know about the base directory. > > You can use os.path.join() to create such paths: > > https://docs.python.org/2/library/os.path.html#os.path.join > Thanks Danny, that was spot on. I actually used os.chdir to change to the base directory (which I assigned to a variable) just before the open statement. I don't know if that's 'pythonically' correct but it seemed like a simple way to do it. Again, thank you for helping me fix this. From awg1 at gmx.com Mon Jun 2 02:40:22 2014 From: awg1 at gmx.com (Adam Gold) Date: Mon, 02 Jun 2014 01:40:22 +0100 Subject: [Tutor] gnupg within a for loop In-Reply-To: References: <538B1613.6000000@gmx.com> <538BC399.809@gmx.com> Message-ID: <538BC7F6.9010902@gmx.com> On 02/06/14 01:35, Danny Yoo wrote: >> Thanks Danny, that was spot on. I actually used os.chdir to change to >> the base directory (which I assigned to a variable) just before the open >> statement. I don't know if that's 'pythonically' correct but it seemed >> like a simple way to do it. Again, thank you for helping me fix this. > > I am glad! I would recommend using os.path.join instead to build absolute > paths, as it's less fragile to external factors. I think of current > working directory the same way I think of global variables: use it > judiciously. Yup, makes sense, thanks for the advice, From s.shall at virginmedia.com Mon Jun 2 14:21:29 2014 From: s.shall at virginmedia.com (Sydney Shall) Date: Mon, 02 Jun 2014 13:21:29 +0100 Subject: [Tutor] How does one construct a module for import? In-Reply-To: <20140602025518.GP10355@ando> References: <538BD471.2040007@comcast.net> <20140602025518.GP10355@ando> Message-ID: <538C6C49.9080805@virginmedia.com> I am having a similar problem. I have now worked out how to copy my helper file to the correct location, in my case is: '/Users/sydney/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages' When I type the following at the IPython prompt I get no error message; import findGraphParametersV2 And the following led me to believe all was well. I type in the following: In [19]: dir(findGraphParametersV2) Out[19]: ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'findGraphParameters', 'np', 'pylab'] However, when I use the import statement in my program I get a runtime error as follows: in () ----> 1 CapitalSimulation(51, 4000.0, 20.0, 20.0, 100, 1.0, 0.0, 40.0, 1.0) /Users/sydney/My_Documents/Political_Economy/Capital_Simulation/Capital/Current version/CapitalWithProdV14.py in CapitalSimulation(number_of_cycles, capital_advanced, unit_constant_capital, wagerate, labour_powers, productivity, prodinc, work_duration, labour_intensity) 525 lnsurplusvalue, lnvariablecapital, lnconstantcapital, 526 lnlabourpowers, lnnewvaluecreated, rationvtoac, --> 527 rationvtorc) 528 529 plotDataV2(cycles, AdvancedCapital, lnAdvancedCapital, RealisedCapital, TypeError: 'module' object is not callable I do not really understand what Steven is recommending below. Is it an init statement in a file or is it an independent file. Thanks to you all. Sydney On 02/06/2014 03:55, Steven D'Aprano wrote: > On Sun, Jun 01, 2014 at 08:33:37PM -0500, Charles Agriesti wrote: >> from swampy.World import World >> world = World() >> >> ImportError: No module name World >> >> These scripts run with no problem as long as the file location is the >> python27 folder. But not from outside the folder. > Where does swampy come from? Is it your module? Somebody else's? > > It looks to me like it is a bug in swampy. You are trying to use it as > if it were a package containing a sub-module, but it isn't actually a > package, just a folder full of modules. > > If swampy is somebody else's project, you should report this to them as > a bug, but if it is yours, then you should be able to fix it by adding > an empty __init__.py file inside the swampy folder. > > My guess is that you have a folder like this inside the python27 folder: > > > python27 > +-- [other python files] > +-- swampy > +-- World.py > > > but you need this: > > python27 > +-- [other python files] > +-- swampy > +-- __init__.py > +-- World.py > > > > If this is not what you have, you will need to explain in more detail > what the layout of your files is, where swampy came from, and where it > is. > > -- Sydney Shall From breamoreboy at yahoo.co.uk Mon Jun 2 14:24:33 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 02 Jun 2014 13:24:33 +0100 Subject: [Tutor] Swampy: No module name World In-Reply-To: <538BD471.2040007@comcast.net> References: <538BD471.2040007@comcast.net> Message-ID: On 02/06/2014 02:33, Charles Agriesti wrote: > from swampy.World import World > world = World() > > ImportError: No module name World > > These scripts run with no problem as long as the file location is the > python27 folder. But not from outside the folder. > Other modules, like math, import with no problem from other locations > import math > from math import pi > 2*pi > 6.28... > > how can I fix this? > > thanks in advance > As swampy is on pypi you should have installed it using easy_install or pip, so that it is under the site-packages directory. Python would have found it there automatically. I'm guessing that you've downloaded a tar or zip file and manually installed it soemwhere else, am I correct? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From alan.gauld at btinternet.com Mon Jun 2 15:52:42 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 02 Jun 2014 14:52:42 +0100 Subject: [Tutor] How does one construct a module for import? In-Reply-To: <538C6C49.9080805@virginmedia.com> References: <538BD471.2040007@comcast.net> <20140602025518.GP10355@ando> <538C6C49.9080805@virginmedia.com> Message-ID: On 02/06/14 13:21, Sydney Shall wrote: > I do not really understand what Steven is recommending below. > Is it an init statement in a file or is it an independent file. It is an independent file (which can be empty) whose existence indicates to python that a folder is a package. Thus if you have a folder abc which contains files a,b and c. Even if abc is in Python's import path you cannot import a,b or c unless there is an __init__.py HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From emile at fenx.com Mon Jun 2 15:58:43 2014 From: emile at fenx.com (Emile van Sebille) Date: Mon, 02 Jun 2014 06:58:43 -0700 Subject: [Tutor] How does one construct a module for import? In-Reply-To: <538C6C49.9080805@virginmedia.com> References: <538BD471.2040007@comcast.net> <20140602025518.GP10355@ando> <538C6C49.9080805@virginmedia.com> Message-ID: On 6/2/2014 5:21 AM, Sydney Shall wrote: > I am having a similar problem. > However, when I use the import statement in my program I get a runtime > error as follows: > > in () > ----> 1 CapitalSimulation(51, 4000.0, 20.0, 20.0, 100, 1.0, 0.0, 40.0, 1.0) > > /Users/sydney/My_Documents/Political_Economy/Capital_Simulation/Capital/Current > version/CapitalWithProdV14.py in CapitalSimulation(number_of_cycles, > capital_advanced, unit_constant_capital, wagerate, labour_powers, > productivity, prodinc, work_duration, labour_intensity) > 525 lnsurplusvalue, lnvariablecapital, > lnconstantcapital, > 526 lnlabourpowers, lnnewvaluecreated, > rationvtoac, > --> 527 rationvtorc) this is the end of something (as per the closing paren) The error message would indicate that the name on the start of this something is a module name. so you've likely done something like: import xyz xyz(a,b,c,d,e...,rationvtorc) when you should be doing something like: import xyz xyz.callable(a,b,c,d,e...,rationvtorc) HTH, Emile > 528 > 529 plotDataV2(cycles, AdvancedCapital, lnAdvancedCapital, > RealisedCapital, > > TypeError: 'module' object is not callable > > I do not really understand what Steven is recommending below. > Is it an init statement in a file or is it an independent file. > > Thanks to you all. > > Sydney > From s.shall at virginmedia.com Mon Jun 2 16:41:15 2014 From: s.shall at virginmedia.com (Sydney Shall) Date: Mon, 02 Jun 2014 15:41:15 +0100 Subject: [Tutor] How does one construct a module for import? In-Reply-To: References: <538BD471.2040007@comcast.net> <20140602025518.GP10355@ando> <538C6C49.9080805@virginmedia.com> Message-ID: <538C8D0B.2050706@virginmedia.com> Alan, Please forgive me, but I am still unclear. Do you mean that I must add a file called __ini__.py to my folder or do you mean that each file that I wish to import should have that statement [ __init__.py ] immediately after (I presume) my def statement? If it must be a file, what is in this file? With many thanks, Sydney On 02/06/2014 14:52, Alan Gauld wrote: > On 02/06/14 13:21, Sydney Shall wrote: > >> I do not really understand what Steven is recommending below. >> Is it an init statement in a file or is it an independent file. > > It is an independent file (which can be empty) whose existence > indicates to python that a folder is a package. > > Thus if you have a folder abc which contains files a,b and c. > Even if abc is in Python's import path you cannot > import a,b or c unless there is an __init__.py > > HTH -- Sydney Shall From steve at pearwood.info Mon Jun 2 17:31:24 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 3 Jun 2014 01:31:24 +1000 Subject: [Tutor] How does one construct a module for import? In-Reply-To: <538C6C49.9080805@virginmedia.com> References: <538BD471.2040007@comcast.net> <20140602025518.GP10355@ando> <538C6C49.9080805@virginmedia.com> Message-ID: <20140602153124.GQ10355@ando> On Mon, Jun 02, 2014 at 01:21:29PM +0100, Sydney Shall wrote: > I am having a similar problem. Actually, I don't think so. Your problem doesn't appear to have anything to do with the problem that Charles Agriesti is having. The only connection seems to be that you are both using Python. Read on for more details. > I have now worked out how to copy my helper file to the correct > location, in my case is: > '/Users/sydney/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages' > > When I type the following at the IPython prompt I get no error message; > import findGraphParametersV2 Right. This says that, unlike Charles' situation, in your case Python is correctly importing your module. You may have a problem, but *importing* is not the problem. > And the following led me to believe all was well. > > I type in the following: > In [19]: dir(findGraphParametersV2) > > Out[19]: > ['__builtins__', '__doc__', '__file__', '__name__', '__package__', > 'findGraphParameters', 'np', 'pylab'] This shows that the findGraphParametersV2 (whew, that's a mouthful!) module has eight attributes. Some of them (like __name__) are created automatically by Python. Others, like np and pylab, are probably created when your module imports other modules. The one that you probably care about is findGraphParameters, which you need to call using: findGraphParametersV2.findGraphParameters( arguments ) Notice that you need to give the module name first, followed by the name of the thing inside the module. > However, when I use the import statement in my program I get a runtime > error as follows: Just a moment. A couple of sentences ago, you said that importing works. Now you say it doesn't. Which is it? Please be more specific about the code you are running. Unfortunately, while we know Python quite well, we're not very good at reading your mind, and we can't see your code. You need to identify what line of code is being run, and tell us. If the code is: import findGraphParametersV2 which then fails with ImportError, that tells us some valuable information. If the code is: result = findGraphParametersV2(some, arguments, here) which then fails with the error you mentioned: TypeError: 'module' object is not callable that tells us something completely different! Both the type of the exception (ImportError, TypeError) and the error message are important, but equally important is what you did that resulted in the error. > in () > ----> 1 CapitalSimulation(51, 4000.0, 20.0, 20.0, 100, 1.0, 0.0, 40.0, 1.0) > > /Users/sydney/My_Documents/Political_Economy/Capital_Simulation/Capital/Current > version/CapitalWithProdV14.py in CapitalSimulation(number_of_cycles, > capital_advanced, unit_constant_capital, wagerate, labour_powers, > productivity, prodinc, work_duration, labour_intensity) If I am reading this correctly, this has absolutely nothing to do with the findGraphParametersV2 module or the findGraphParameters function inside that module. It looks to me like you have a *different* module, called CapitalSimulation, and you try to call it as if it were a function. It is difficult to tell exactly what is going on, but my guess is that inside the CapitalSimulation module you have a function *also* called CapitalSimulation. So in your module CapitalWithProdV14 (that's the THIRD module!!!) you probably have some code like: import CapitalSimulation # this is a module, not a function CapitalSimulation(number_of_cycles, capital_advanced, blah blah blah...) That second line is the problem. You need to change it to: CapitalSimulation.CapitalSimulation(number_of_cycles, ...) I think. Like I said, without understanding your code, it's difficult to be sure exactly what's going on. Reading between the lines, I feel that perhaps somebody has told you that you should have one class or one function per file. Or perhaps you have been reading Java programming books. Either way, it seems to me that you have an excess of modules and too many confusing imports. That way leads to frustration. I believe that you will be much better served to have *one* file per project, rather than splitting your project into a dozen itty bitty files. That way you don't need to care about importing your own modules, because everything is already inside the one file. If you *must* have separate files, never never never (well, almost never) give them the same name as the class or function inside them. A good convention is to name the module in all lower case, and the class in InitialCaps: # no, too confusing CapitalSimulation.CapitalSimulation(...) # better capital_simulation.CapitalSimulation(...) That way, you can tell at a glance which is the module and which is the class inside the module, and if you make a mistake, it will be more easily understood: capital_simulation(...) # wait a second, that's a module! [...] > I do not really understand what Steven is recommending below. > Is it an init statement in a file or is it an independent file. I don't believe this has anything to do with your problem, but for the record, there is no init statement in Python (although classes do have an __init__ method). I suggested to Charles that he add an empty __init__.py file inside his project folder. That would make it an separate file. Good luck! -- Steven From steve at pearwood.info Mon Jun 2 17:35:12 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 3 Jun 2014 01:35:12 +1000 Subject: [Tutor] How does one construct a module for import? In-Reply-To: <538C8D0B.2050706@virginmedia.com> References: <538BD471.2040007@comcast.net> <20140602025518.GP10355@ando> <538C6C49.9080805@virginmedia.com> <538C8D0B.2050706@virginmedia.com> Message-ID: <20140602153511.GR10355@ando> On Mon, Jun 02, 2014 at 03:41:15PM +0100, Sydney Shall wrote: > Alan, > Please forgive me, but I am still unclear. > Do you mean that I must add a file called __ini__.py to my folder or do > you mean that each file that I wish to import should have that statement > [ __init__.py ] immediately after (I presume) my def statement? Neither. Forget about __init__.py, that doesn't seem to be related to your problem. -- Steven From s.shall at virginmedia.com Mon Jun 2 18:08:21 2014 From: s.shall at virginmedia.com (Sydney Shall) Date: Mon, 02 Jun 2014 17:08:21 +0100 Subject: [Tutor] How does one construct a module for import? In-Reply-To: <20140602153124.GQ10355@ando> References: <538BD471.2040007@comcast.net> <20140602025518.GP10355@ando> <538C6C49.9080805@virginmedia.com> <20140602153124.GQ10355@ando> Message-ID: <538CA175.3080200@virginmedia.com> Thanks for the detailed reply, Steven. It seems that you have correctly identified my prblem. But I am still puzzled, because I do not know how this happened. I simply copied the two files that I wished to import to a directory called (nowMyModule). It now contains only three files; > pwd Out[99]: u'/Users/sydney/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/MyModule' ls -l total 32 -rw-r--r-- 1 sydney staff 29 Jun 2 16:31 __init__.py -rw-r--r-- 1 sydney staff 2936 Jun 2 16:32 findGraphParametersV2.py -rw-r--r-- 1 sydney staff 7147 Jun 2 16:32 plotDataV2.py What does the line above; total 32 refer to? But when I do the following, this is what I get. dir(plotDataV2) Out[107]: ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'plotData', 'pylab'] In the file plotDataV2, I have imported of course, pylab. I do not know where plotData comes from although there is such a file in another directory. dir(findGraphParametersV2) Out[108]: ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'findGraphParameters', 'np', 'pylab'] Here I have imported numpy as np, pylab and math which doers not appear? I do understand that I should have only ONE directory containing my own Modules and only ONE copy of each file in that directory. But that is what I thought I had done. Do I undestand correctly that what I need to do is to have a single directory, say called MyModule, in the directory ....site-packages. And then I need to copy just once each of the two function files that I want to be importable? Once again, many thanks for your advice, Sydney On 02/06/2014 16:31, Steven D'Aprano wrote: > On Mon, Jun 02, 2014 at 01:21:29PM +0100, Sydney Shall wrote: >> I am having a similar problem. > Actually, I don't think so. Your problem doesn't appear to have anything > to do with the problem that Charles Agriesti is having. The only > connection seems to be that you are both using Python. Read on for more > details. > > >> I have now worked out how to copy my helper file to the correct >> location, in my case is: >> '/Users/sydney/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages' >> >> When I type the following at the IPython prompt I get no error message; >> import findGraphParametersV2 > Right. This says that, unlike Charles' situation, in your case Python is > correctly importing your module. You may have a problem, but *importing* > is not the problem. > > >> And the following led me to believe all was well. >> >> I type in the following: >> In [19]: dir(findGraphParametersV2) >> >> Out[19]: >> ['__builtins__', '__doc__', '__file__', '__name__', '__package__', >> 'findGraphParameters', 'np', 'pylab'] > This shows that the findGraphParametersV2 (whew, that's a mouthful!) > module has eight attributes. Some of them (like __name__) are created > automatically by Python. Others, like np and pylab, are probably created > when your module imports other modules. The one that you probably care > about is findGraphParameters, which you need to call using: > > findGraphParametersV2.findGraphParameters( arguments ) > > Notice that you need to give the module name first, followed by the name > of the thing inside the module. > > >> However, when I use the import statement in my program I get a runtime >> error as follows: > Just a moment. A couple of sentences ago, you said that importing works. > Now you say it doesn't. Which is it? > > Please be more specific about the code you are running. Unfortunately, > while we know Python quite well, we're not very good at reading your > mind, and we can't see your code. You need to identify what line of code > is being run, and tell us. > > If the code is: > > import findGraphParametersV2 > > which then fails with ImportError, that tells us some valuable > information. If the code is: > > result = findGraphParametersV2(some, arguments, here) > > which then fails with the error you mentioned: > > TypeError: 'module' object is not callable > > that tells us something completely different! Both the type of the > exception (ImportError, TypeError) and the error message are important, > but equally important is what you did that resulted in the error. > > >> in () >> ----> 1 CapitalSimulation(51, 4000.0, 20.0, 20.0, 100, 1.0, 0.0, 40.0, 1.0) >> >> /Users/sydney/My_Documents/Political_Economy/Capital_Simulation/Capital/Current >> version/CapitalWithProdV14.py in CapitalSimulation(number_of_cycles, >> capital_advanced, unit_constant_capital, wagerate, labour_powers, >> productivity, prodinc, work_duration, labour_intensity) > > If I am reading this correctly, this has absolutely nothing to do with > the findGraphParametersV2 module or the findGraphParameters function > inside that module. It looks to me like you have a *different* module, > called CapitalSimulation, and you try to call it as if it were a > function. > > It is difficult to tell exactly what is going on, but my guess is that > inside the CapitalSimulation module you have a function *also* called > CapitalSimulation. So in your module CapitalWithProdV14 (that's the > THIRD module!!!) you probably have some code like: > > import CapitalSimulation # this is a module, not a function > > CapitalSimulation(number_of_cycles, capital_advanced, blah blah blah...) > > > That second line is the problem. You need to change it to: > > CapitalSimulation.CapitalSimulation(number_of_cycles, ...) > > > I think. Like I said, without understanding your code, it's difficult to > be sure exactly what's going on. > > > Reading between the lines, I feel that perhaps somebody has told you > that you should have one class or one function per file. Or perhaps you > have been reading Java programming books. Either way, it seems to me > that you have an excess of modules and too many confusing imports. That > way leads to frustration. > > I believe that you will be much better served to have *one* file per > project, rather than splitting your project into a dozen itty bitty > files. That way you don't need to care about importing your own modules, > because everything is already inside the one file. > > If you *must* have separate files, never never never (well, almost > never) give them the same name as the class or function inside them. A > good convention is to name the module in all lower case, and the class > in InitialCaps: > > # no, too confusing > CapitalSimulation.CapitalSimulation(...) > > # better > capital_simulation.CapitalSimulation(...) > > > That way, you can tell at a glance which is the module and which is the > class inside the module, and if you make a mistake, it will be more > easily understood: > > capital_simulation(...) # wait a second, that's a module! > > > [...] >> I do not really understand what Steven is recommending below. >> Is it an init statement in a file or is it an independent file. > I don't believe this has anything to do with your problem, but for the > record, there is no init statement in Python (although classes do have > an __init__ method). I suggested to Charles that he add an empty > __init__.py file inside his project folder. That would make it an > separate file. > > > Good luck! > > > -- Sydney Shall -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Mon Jun 2 18:47:25 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 3 Jun 2014 02:47:25 +1000 Subject: [Tutor] How does one construct a module for import? In-Reply-To: <538CA175.3080200@virginmedia.com> References: <538BD471.2040007@comcast.net> <20140602025518.GP10355@ando> <538C6C49.9080805@virginmedia.com> <20140602153124.GQ10355@ando> <538CA175.3080200@virginmedia.com> Message-ID: <20140602164725.GS10355@ando> On Mon, Jun 02, 2014 at 05:08:21PM +0100, Sydney Shall wrote: > Thanks for the detailed reply, Steven. > It seems that you have correctly identified my prblem. > > But I am still puzzled, because I do not know how this happened. > I simply copied the two files that I wished to import to a directory > called (nowMyModule). > It now contains only three files; > > > pwd > Out[99]: > u'/Users/sydney/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/MyModule' > > ls -l > total 32 > -rw-r--r-- 1 sydney staff 29 Jun 2 16:31 __init__.py > -rw-r--r-- 1 sydney staff 2936 Jun 2 16:32 findGraphParametersV2.py > -rw-r--r-- 1 sydney staff 7147 Jun 2 16:32 plotDataV2.py > > What does the line above; total 32 refer to? It is the number of disk blocks used by the files, assuming 1024 bytes per block. In this example, the __init__.py file uses 29 bytes, and so requires a minimum of 1 block; the findGraph file uses 2936 bytes, so requires at least 3 blocks; the plotData file uses 7147 bytes, so it needs at least 7 blocks. I don't actually know where the remaining 21 blocks end up. Possibly in the directory itself? > But when I do the following, this is what I get. > > > dir(plotDataV2) > Out[107]: > ['__builtins__', > '__doc__', > '__file__', > '__name__', > '__package__', > 'plotData', > 'pylab'] > > > In the file plotDataV2, I have imported of course, pylab. > I do not know where plotData comes from although there is such a file in > another directory. You will have a line like from findGraphParametersV2 import plotData or possibly: plotData = 23 # or anything really, who knows? or def plotData(): ... There's no way for us to tell what plotData is. But YOU can do it: print(plotDataV2.plotData) If that doesn't display enough information for you to identify what it is, try these two commands: print(repr(plotDataV2.plotData)) print(type(plotDataV2.plotData)) > dir(findGraphParametersV2) > Out[108]: > ['__builtins__', > '__doc__', > '__file__', > '__name__', > '__package__', > 'findGraphParameters', > 'np', > 'pylab'] > > Here I have imported numpy as np, pylab and math which doers not appear? If math does not appear, you haven't imported it. Possibly you imported it in findGraphParametersV1 or findGraphParametersV3, but not V2. Or maybe you imported it, but then deleted it: import math del math Again, there is no way for us to tell what you've done just by the result. > I do understand that I should have only ONE directory containing my own > Modules and only ONE copy of each file in that directory. But that is > what I thought I had done. Um, yes? > Do I undestand correctly that what I need to do is to have a single > directory, say called MyModule, in the directory ....site-packages. And > then I need to copy just once each of the two function files that I want > to be importable? No, I'm saying, *forget about directories of files*. Have ONE FILE per project. You seem to be confusing yourself into all sorts of knots by having multiple files trying to import other files. You're working with advanced functionality, packages, before you understand how to deal with basic functionality, modules and attributes. You currently have this: site-packages/ # directory +-- MyModule # directory inside site-packages .....+-- __init__.py # file inside MyModule .....+-- findGraphParametersV2.py # another file .....+-- plotDataV2.py # yet another file Instead, I suggest you should have: site-packages/ # directory +-- MyModule.py # file inside site-packages and put the contents of findGraphParametersV2.py and plotDataV2.py inside the MyModule.py file. -- Steven From s.shall at virginmedia.com Mon Jun 2 19:05:29 2014 From: s.shall at virginmedia.com (Sydney Shall) Date: Mon, 02 Jun 2014 18:05:29 +0100 Subject: [Tutor] How does one construct a module for import? In-Reply-To: <20140602164725.GS10355@ando> References: <538BD471.2040007@comcast.net> <20140602025518.GP10355@ando> <538C6C49.9080805@virginmedia.com> <20140602153124.GQ10355@ando> <538CA175.3080200@virginmedia.com> <20140602164725.GS10355@ando> Message-ID: <538CAED9.3070308@virginmedia.com> Thanks Steven I will try what you recommend. You were correct. Not understanding properly, I had constucted both a directory and a file in my module directory. I can now correct it, I think. Thanks for all your help Sydney On 02/06/2014 17:47, Steven D'Aprano wrote: > On Mon, Jun 02, 2014 at 05:08:21PM +0100, Sydney Shall wrote: >> Thanks for the detailed reply, Steven. >> It seems that you have correctly identified my prblem. >> >> But I am still puzzled, because I do not know how this happened. >> I simply copied the two files that I wished to import to a directory >> called (nowMyModule). >> It now contains only three files; >> pwd >> Out[99]: >> u'/Users/sydney/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/MyModule' >> >> ls -l >> total 32 >> -rw-r--r-- 1 sydney staff 29 Jun 2 16:31 __init__.py >> -rw-r--r-- 1 sydney staff 2936 Jun 2 16:32 findGraphParametersV2.py >> -rw-r--r-- 1 sydney staff 7147 Jun 2 16:32 plotDataV2.py >> >> What does the line above; total 32 refer to? > It is the number of disk blocks used by the files, assuming 1024 bytes > per block. In this example, the __init__.py file uses 29 bytes, and so > requires a minimum of 1 block; the findGraph file uses 2936 bytes, so > requires at least 3 blocks; the plotData file uses 7147 bytes, so it > needs at least 7 blocks. I don't actually know where the remaining 21 > blocks end up. Possibly in the directory itself? > >> But when I do the following, this is what I get. >> >> >> dir(plotDataV2) >> Out[107]: >> ['__builtins__', >> '__doc__', >> '__file__', >> '__name__', >> '__package__', >> 'plotData', >> 'pylab'] >> >> >> In the file plotDataV2, I have imported of course, pylab. >> I do not know where plotData comes from although there is such a file in >> another directory. > You will have a line like > > from findGraphParametersV2 import plotData > > or possibly: > > plotData = 23 # or anything really, who knows? > > or > > def plotData(): ... > > > There's no way for us to tell what plotData is. But YOU can do it: > > print(plotDataV2.plotData) > > > > If that doesn't display enough information for you to identify what it > is, try these two commands: > > print(repr(plotDataV2.plotData)) > print(type(plotDataV2.plotData)) > > > >> dir(findGraphParametersV2) >> Out[108]: >> ['__builtins__', >> '__doc__', >> '__file__', >> '__name__', >> '__package__', >> 'findGraphParameters', >> 'np', >> 'pylab'] >> >> Here I have imported numpy as np, pylab and math which doers not appear? > If math does not appear, you haven't imported it. Possibly you imported > it in findGraphParametersV1 or findGraphParametersV3, but not V2. > > Or maybe you imported it, but then deleted it: > > import math > del math > > Again, there is no way for us to tell what you've done just by the > result. > > >> I do understand that I should have only ONE directory containing my own >> Modules and only ONE copy of each file in that directory. But that is >> what I thought I had done. > Um, yes? > > >> Do I undestand correctly that what I need to do is to have a single >> directory, say called MyModule, in the directory ....site-packages. And >> then I need to copy just once each of the two function files that I want >> to be importable? > No, I'm saying, *forget about directories of files*. Have ONE FILE per > project. You seem to be confusing yourself into all sorts of knots by > having multiple files trying to import other files. You're working with > advanced functionality, packages, before you understand how to deal with > basic functionality, modules and attributes. > > > You currently have this: > > site-packages/ # directory > +-- MyModule # directory inside site-packages > .....+-- __init__.py # file inside MyModule > .....+-- findGraphParametersV2.py # another file > .....+-- plotDataV2.py # yet another file > > > Instead, I suggest you should have: > > site-packages/ # directory > +-- MyModule.py # file inside site-packages > > > and put the contents of findGraphParametersV2.py and plotDataV2.py > inside the MyModule.py file. > > > -- Sydney Shall From breamoreboy at yahoo.co.uk Mon Jun 2 19:55:19 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 02 Jun 2014 18:55:19 +0100 Subject: [Tutor] How does one construct a module for import? In-Reply-To: <538CA175.3080200@virginmedia.com> References: <538BD471.2040007@comcast.net> <20140602025518.GP10355@ando> <538C6C49.9080805@virginmedia.com> <20140602153124.GQ10355@ando> <538CA175.3080200@virginmedia.com> Message-ID: On 02/06/2014 17:08, Sydney Shall wrote: Would you please be kind enough to stop top posting, it makes following a thread difficult, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From jeanrmichel at gmail.com Mon Jun 2 17:34:16 2014 From: jeanrmichel at gmail.com (JEAN MICHEL) Date: Mon, 2 Jun 2014 08:34:16 -0700 Subject: [Tutor] Working with Data files & Lists Message-ID: I'm a Python beginner trying write a program that reads outside txt files, takes the data like the name and test grades of students then calculate the average and also assigns a grade and writes the data into a new txt file. I'm having difficulties writing the program so far I've been able to write up half of the program but I am noticing there might be bugs. I've tried running the program but every time I do, my program output is blank and there is no error messages I'm trying to write 4 Functions to extract data from files into Student name, Score1, Score2 and Score 3 lists. then from there I'm trying to create a function that will calculate the average test score using the score1, score2, score3. After that I'm trying to create a function that will to write to file using Student name, lettergrade list. Here is my program so far. def calcaverage(test1,test2,test3): for count in range(test1,test2,test3): curraverage=0 curraverage=((test1[count]+ test2[count]+ test3[count])/3) currentaverage.append(curraverage) if curraverage>= 90: grade= "A" lettergrades.append(grade) elif curraverage >= 80 and curraverage < 90: grade= "B" lettergrades.append(grade) elif curraverage >= 70 and curraverage < 80: grade= "C" lettergrades.append(grade) elif curraverage < 70: grade= "F" lettergrades.append(grade) name=[] test1=[] test2=[] test3=[] averagescore=[] lettergrades=[] with open ("period1.txt", 'r') as infile: for line in infile: values = line.split() name.append(values[0] + ','+ values[1]) for line in infile: values = line.split() score1=float(values[2]) test1.append(score1) for line in infile: values = line.split() score2=float(values[3]) test2.append(score2) for line in inline: values = line.split() score3=float(values[4]) test3.append(score3) averagescore=calcaverage(test1,test2,test3) print(line) -------------- next part -------------- An HTML attachment was scrubbed... URL: From dragriesti at comcast.net Tue Jun 3 01:08:58 2014 From: dragriesti at comcast.net (Charles Agriesti) Date: Mon, 02 Jun 2014 18:08:58 -0500 Subject: [Tutor] Swampy: No module name World , (Steven D'Aprano)(Dave Angel)(Alan Gauld) In-Reply-To: References: Message-ID: <538D040A.6090900@comcast.net> Thank you very much for the kind replies. apologies for the html being on. Apparently Thunderbird turns it back on when it updates. Should be off now. swampy does contain __init__.py it is completely blank sampy also contains __init__.pyc that does contain a couple of lines ending with: C:\Python27\Lib\site-packages\swampy\__init__.pyt  s sys.path in python2 is ['', 'C:\\Python27\\Lib\\idlelib', 'C:\\Windows\\SYSTEM32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages'] thanks, I did not know that existed. What is Swampy? It uses Tkinter for gui exercises. http://www.greenteapress.com/thinkpython/swampy/ Module written by Allen Downey to accompany the text 'Think Python' http://www.greenteapress.com/thinkpython and necessary for several sections in the book. Apologies for not explaining this initially. Note, 'Think Python' uses Python2, but this computer has both 2 and 3 on it. They were both working, scripts finding python from any directory, with nothing entered in the win sys Path as long as only one version of python was open at a time. After this problem started I amended the system path %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Python27;C:\python27\scripts adding those last two items. Made a diff for running from console (powershell, actually) but was not needed for using "run module" in Idle. Python3 still works just fine with nothing mentioning it in the path. Location of World module: C:\Python27\Lib\site-packages\swampy\World.py (there is also a compiled file: World.pyc) Swampy modules come up okay as long as the script requesting it is in the Python27 folder. I normally run them from the desktop instead to make it easy to get all of them deleted or stored. So: C:\Python27\my_script.py world.mainloop() works also from swampy.World import World works C:\Users\Charles\Desktop\practice_folder\my_script.py swampy.World import World does not work error message today is Traceback (most recent call last): File "", line 1, in from swampy import World EOFError: EOF read where object expected yesterday I was getting a different error, cannot seem to get it to do the same thing today. Yesterday it said there was no module named 'World'. > From dyoo at hashcollision.org Tue Jun 3 02:32:49 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 2 Jun 2014 17:32:49 -0700 Subject: [Tutor] Working with Data files & Lists In-Reply-To: References: Message-ID: On Mon, Jun 2, 2014 at 8:34 AM, JEAN MICHEL wrote: > I'm a Python beginner trying write a program that reads outside txt files, > takes the data like the name and test grades of students then calculate the > average and also assigns a grade and writes the data into a new txt file. > I'm having difficulties writing the program so far I've been able to write > up half of the program but I am noticing there might be bugs. I've tried > running the program but every time I do, my program output is blank and > there is no error messages Wasn't this asked a few weeks ago? This feels hauntingly familiar. Ah, yes, found it. https://mail.python.org/pipermail/tutor/2014-May/101310.html Can you give folks a quick "diff"? Can you explain what has changed since then? That way, folks will know to not repeat something that you already know about. From dragriesti at comcast.net Tue Jun 3 16:53:00 2014 From: dragriesti at comcast.net (Charles Agriesti) Date: Tue, 03 Jun 2014 07:53:00 -0700 Subject: [Tutor] Swampy: No module name World In-Reply-To: References: Message-ID: <538DE14C.5030908@comcast.net> Thanks all. Problem apparently solved. Shortly after that last message it quit working completely. Reinstall Python 2.7 + swampy -> no good; uninstall both 2.7 and 3.4, manually remove the folders that remained because of scripts that got saved in the base folder, reset sys path, reinstall both, add 2.7 to path, reinstall Swampy from new download -> no good; remove 2.7, new 2.7 dl and install, new Swampy dl and install -> works perfectly this morning. --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From alan.gauld at btinternet.com Tue Jun 3 15:28:40 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 03 Jun 2014 14:28:40 +0100 Subject: [Tutor] Swampy: No module name World , (Steven D'Aprano)(Dave Angel)(Alan Gauld) In-Reply-To: <538D040A.6090900@comcast.net> References: <538D040A.6090900@comcast.net> Message-ID: On 03/06/14 00:08, Charles Agriesti wrote: > Thank you very much for the kind replies. I see you got it working but there is one thing that rings alarm bells and could bite in the future.... > What is Swampy? > It uses Tkinter for gui exercises. > http://www.greenteapress.com/thinkpython/swampy/ > Module written by Allen Downey to accompany the text 'Think Python' > Note, 'Think Python' uses Python2, but this computer has both 2 and 3 on > it. Tkinter is fundamentally different in version 3 than in version 2. Trying to use a module written for V2 Tkinter will almost certainly not work on V3. The module structure and names have all been changed. So if you are trying to use a single copy of Swampy on both v2 and v3 it will likely end in tears... But I may be misreading your comments, or you may have two versions installed. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From jarod_v6 at libero.it Tue Jun 3 18:24:30 2014 From: jarod_v6 at libero.it (jarod_v6 at libero.it) Date: Tue, 3 Jun 2014 18:24:30 +0200 (CEST) Subject: [Tutor] How to create a dictionary for ount elements Message-ID: <2119882546.4848741401812670107.JavaMail.actor@webmail34> HI there!!! I have afile like this: file.txt programs sample gene program1 sample1 TP53 program1 sample1 TP53 program1 sample2 PRNP program1 sample2 ATF3 program2 sample1 TP53 program2 sample1 PRNP program2 sample2 TRIM32 program2 sample2 TLK1 program2 sample2 KIT with open("prova.csv") as p: for i in p: ...: lines = i.rstrip("\n").split("\t") ...: print lines ...: ['programs ', 'sample', 'gene', 'values'] ['program1', 'sample1', 'TP53', '2'] ['program1', 'sample1', 'TP53', '3'] ['program1', 'sample2', 'PRNP', '4'] ['program1', 'sample2', 'ATF3', '3'] ['program2', 'sample1', 'TP53', '2'] ['program2', 'sample1', 'PRNP', '5'] ['program2', 'sample2', 'TRIM32', '4'] ['program2', 'sample2', 'TLK1', '4'] I want to create a dictionary with set data with the names of the genes: example: dic = {} dic['program1-sample1] = set(TP53) dic['program1-sample2] = set(TP53,PRNP,ATF3) So If I have a dictionary like that I can compare two set I will compare the capacity of the programs in function of the gene show. Thanks in advance for your help! From marc.tompkins at gmail.com Tue Jun 3 18:44:08 2014 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 3 Jun 2014 09:44:08 -0700 Subject: [Tutor] Swampy: No module name World In-Reply-To: <538DE14C.5030908@comcast.net> References: <538DE14C.5030908@comcast.net> Message-ID: On Tue, Jun 3, 2014 at 7:53 AM, Charles Agriesti wrote: > Thanks all. Problem apparently solved. Shortly after that last message it > quit working completely. Reinstall Python 2.7 + swampy -> no good; > uninstall both 2.7 and 3.4, manually remove the folders that remained > because of scripts that got saved in the base folder, reset sys path, > reinstall both, add 2.7 to path, reinstall Swampy from new download -> no > good; remove 2.7, new 2.7 dl and install, new Swampy dl and install -> > works perfectly this morning. > > Sorry to jump in at the end, but I can't help noticing that something important (mentioned earlier in the thread) may have got missed: swampy is available from PyPi, which means that there are several automatic tools (easy-install, pip, etc.) available to install it for you (and presumably make the details work properly.) With very few exceptions, if a module is available from PyPi, the proper way to install it is via one of those tools and NOT a manual download. The fact that you mention installing Swampy from a download tells me you're not using those tools - am I mistaken? For Windows users, there's pip-win ( https://sites.google.com/site/pydatalog/python/pip-for-windows). It's not only incredibly simple to use, but it's specifically designed for machines with multiple Python versions; so (gulp!) I actually recommend the Windows tool over the command-line in this case. > > --- > This email is free from viruses and malware because avast! Antivirus > protection is active. > http://www.avast.com > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: pip-win.jpg Type: image/jpeg Size: 20328 bytes Desc: not available URL: From alan.gauld at btinternet.com Tue Jun 3 19:31:15 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 03 Jun 2014 18:31:15 +0100 Subject: [Tutor] How to create a dictionary for ount elements In-Reply-To: <2119882546.4848741401812670107.JavaMail.actor@webmail34> References: <2119882546.4848741401812670107.JavaMail.actor@webmail34> Message-ID: On 03/06/14 17:24, jarod_v6 at libero.it wrote: > with open("prova.csv") as p: If processing csv files its usually better to use the csv modulew, it can handle many obscure quoting issues etc, especially if the data is sourced from, say, Excel. In particular I recommend using the dict-reader which returns a list of dictionaries based on your column names. This makes writing list comprehensions to extract data sets much easier. > ['programs ', 'sample', 'gene', 'values'] > ['program1', 'sample1', 'TP53', '2'] > ['program1', 'sample1', 'TP53', '3'] > ['program1', 'sample2', 'PRNP', '4'] > ['program1', 'sample2', 'ATF3', '3'] > ['program2', 'sample1', 'TP53', '2'] > ['program2', 'sample1', 'PRNP', '5'] > ['program2', 'sample2', 'TRIM32', '4'] > ['program2', 'sample2', 'TLK1', '4'] > > > I want to create a dictionary with set data with the names of the genes: > > example: > dic = {} > > dic['program1-sample1] = set(TP53) You could do this using the dict reader of csv module like tp53set = [sample for sample in data if datum['gene'] == 'TP53'] Obviously you can select which sample columns to keep as you require. You ask for a set but don't illustrate what that set would actually contain so I'm not sure what aspects of the data you are trying to get at... > So If I have a dictionary like that I can compare two set I will compare the > capacity of the programs in function of the gene show. Sorry, that was just gobbledegook to me. No idea what you mean. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From breamoreboy at yahoo.co.uk Tue Jun 3 20:00:22 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 03 Jun 2014 19:00:22 +0100 Subject: [Tutor] Swampy: No module name World In-Reply-To: References: <538DE14C.5030908@comcast.net> Message-ID: On 03/06/2014 17:44, Marc Tompkins wrote: > On Tue, Jun 3, 2014 at 7:53 AM, Charles Agriesti > wrote: > > Thanks all. Problem apparently solved. Shortly after that last > message it quit working completely. Reinstall Python 2.7 + swampy -> > no good; uninstall both 2.7 and 3.4, manually remove the folders > that remained because of scripts that got saved in the base folder, > reset sys path, reinstall both, add 2.7 to path, reinstall Swampy > from new download -> no good; remove 2.7, new 2.7 dl and install, > new Swampy dl and install -> works perfectly this morning. This will cause no end of fun as you'll be changing the file type. For 3.4 it should look like this c:\cpython\PCbuild>assoc .py .py=Python.File c:\cpython\PCbuild>ftype python.file python.file="C:\Windows\py.exe" "%1" %* If you reinstall 2.7 the file type will point to your 2.7 .exe file. Reinstall 3.4 it reverts to the py.exe file. I've also no idea what, if anything, the different versions do to your path settings. > > Sorry to jump in at the end, but I can't help noticing that something > important (mentioned earlier in the thread) may have got missed: swampy > is available from PyPi, which means that there are several automatic > tools (easy-install, pip, etc.) available to install it for you (and > presumably make the details work properly.) With very few exceptions, > if a module is available from PyPi, the proper way to install it is via > one of those tools and NOT a manual download. The fact that you mention > installing Swampy from a download tells me you're not using those tools > - am I mistaken? Looks as if I've got something correct just for once, as in using pypi :) Actually if an installation from pypi fails as you've not got the appropriate VC++ compiler, try this site http://www.lfd.uci.edu/~gohlke/pythonlibs/ The title might be "Unofficial Windows Binaries for Python Extension Packages" but just ignore that, I've never had a problem with anything I've used from there. > > For Windows users, there's pip-win > (https://sites.google.com/site/pydatalog/python/pip-for-windows). It's > not only incredibly simple to use, but it's specifically designed for > machines with multiple Python versions; so (gulp!) I actually recommend > the Windows tool over the command-line in this case. > Never heard of it so thanks for the link. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From marc.tompkins at gmail.com Tue Jun 3 20:39:25 2014 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 3 Jun 2014 11:39:25 -0700 Subject: [Tutor] Swampy: No module name World In-Reply-To: References: <538DE14C.5030908@comcast.net> Message-ID: On Tue, Jun 3, 2014 at 11:00 AM, Mark Lawrence wrote: pip-win (https://sites.google.com/site/pydatalog/python/pip-for-windows) > > > Never heard of it so thanks for the link. > > I don't know how long it's been around but I'll tell you this much for free: I wish I'd known about it when I first started with Python! The hours I wasted, trying to wrangle various XML and SQL packages... I'll never get that time back. (And it was ALL WASTED TIME, because this mechanical stuff has nothing to do with programming. I get incredibly irritated with the tone of some forum posts on this topic - are people somehow losers because they've got better things to do than figure out the syntax of one package manager versus another? These are TOOLS, folks! They're supposed to work for YOU!) Sorry - I feel better now. -------------- next part -------------- An HTML attachment was scrubbed... URL: From wolfgang.maier at biologie.uni-freiburg.de Tue Jun 3 21:56:00 2014 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Tue, 03 Jun 2014 21:56:00 +0200 Subject: [Tutor] How to create a dictionary for ount elements In-Reply-To: <2119882546.4848741401812670107.JavaMail.actor@webmail34> References: <2119882546.4848741401812670107.JavaMail.actor@webmail34> Message-ID: <538E2850.4000201@biologie.uni-freiburg.de> On 03.06.2014 18:24, jarod_v6 at libero.it wrote: > HI there!!! > I have afile like this: > file.txt > programs sample gene > program1 sample1 TP53 > program1 sample1 TP53 > program1 sample2 PRNP > program1 sample2 ATF3 > program2 sample1 TP53 > program2 sample1 PRNP > program2 sample2 TRIM32 > program2 sample2 TLK1 > program2 sample2 KIT > > > with open("prova.csv") as p: > for i in p: > ...: lines = i.rstrip("\n").split("\t") > ...: print lines > ...: > ['programs ', 'sample', 'gene', 'values'] > ['program1', 'sample1', 'TP53', '2'] > ['program1', 'sample1', 'TP53', '3'] > ['program1', 'sample2', 'PRNP', '4'] > ['program1', 'sample2', 'ATF3', '3'] > ['program2', 'sample1', 'TP53', '2'] > ['program2', 'sample1', 'PRNP', '5'] > ['program2', 'sample2', 'TRIM32', '4'] > ['program2', 'sample2', 'TLK1', '4'] > Be exact / do not provide approximate information if you are looking for adequate answers !! Your file did not look like the one you showed, there was an additional 'values' column in it. What do you want to do with it ?? > > I want to create a dictionary with set data with the names of the genes: > > example: > dic = {} > > > dic['program1-sample1] = set(TP53) > dic['program1-sample2] = set(TP53,PRNP,ATF3) > Again, this is nothing you were ever really trying in a python shell since that would raise errors for several reasons, just try it yourself! I would not build dictionary keys by concatenating the 'programs' and 'sample' strings - rather use a tuple of the two (any immutable object works as a dict key), e.g.: dic[('program1', 'sample1')] = {'TP53'} Essentially, what you need to do is: - instead of printing each individual list you've parsed from the input file, use the first two elements as a tuple for the dict key, then add the third element (the gene) to the set stored under that key (use set.add() for that purpose. - the tricky part is what to do with keys that are encountered for the first time and, thus, don't have a set associated with them yet. Here, dict.setdefault() will help you (https://docs.python.org/2.7/library/stdtypes.html?highlight=setdefault#dict.setdefault). hint: your_dict(your_key, set()).add(the_gene) will work whether or not the key has been encountered before or not. > So If I have a dictionary like that I can compare two set I will compare the > capacity of the programs in function of the gene show. I have no idea what you are trying to do, so I can't tell you whether the data structure will be good for it. Wolfgang From breamoreboy at yahoo.co.uk Wed Jun 4 00:47:38 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 03 Jun 2014 23:47:38 +0100 Subject: [Tutor] How to create a dictionary for ount elements In-Reply-To: <2119882546.4848741401812670107.JavaMail.actor@webmail34> References: <2119882546.4848741401812670107.JavaMail.actor@webmail34> Message-ID: On 03/06/2014 17:24, jarod_v6 at libero.it wrote: > HI there!!! > I have afile like this: > file.txt > programs sample gene > program1 sample1 TP53 > program1 sample1 TP53 > program1 sample2 PRNP > program1 sample2 ATF3 > program2 sample1 TP53 > program2 sample1 PRNP > program2 sample2 TRIM32 > program2 sample2 TLK1 > program2 sample2 KIT > > > with open("prova.csv") as p: > for i in p: > ...: lines = i.rstrip("\n").split("\t") > ...: print lines > ...: > ['programs ', 'sample', 'gene', 'values'] > ['program1', 'sample1', 'TP53', '2'] > ['program1', 'sample1', 'TP53', '3'] > ['program1', 'sample2', 'PRNP', '4'] > ['program1', 'sample2', 'ATF3', '3'] > ['program2', 'sample1', 'TP53', '2'] > ['program2', 'sample1', 'PRNP', '5'] > ['program2', 'sample2', 'TRIM32', '4'] > ['program2', 'sample2', 'TLK1', '4'] > > > I want to create a dictionary with set data with the names of the genes: > > example: > dic = {} > > > dic['program1-sample1] = set(TP53) > dic['program1-sample2] = set(TP53,PRNP,ATF3) > > So If I have a dictionary like that I can compare two set I will compare the > capacity of the programs in function of the gene show. > Thanks in advance for your help! > Is this of any use to you https://docs.python.org/3/library/collections.html#collections.Counter ? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From wolfgang.maier at biologie.uni-freiburg.de Tue Jun 3 23:53:09 2014 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Tue, 03 Jun 2014 23:53:09 +0200 Subject: [Tutor] How to create a dictionary for ount elements In-Reply-To: <538E2850.4000201@biologie.uni-freiburg.de> References: <2119882546.4848741401812670107.JavaMail.actor@webmail34> <538E2850.4000201@biologie.uni-freiburg.de> Message-ID: <538E43C5.1010109@biologie.uni-freiburg.de> On 03.06.2014 21:56, Wolfgang Maier wrote: > - the tricky part is what to do with keys that are encountered for the > first time and, thus, don't have a set associated with them yet. > Here, dict.setdefault() will help you > (https://docs.python.org/2.7/library/stdtypes.html?highlight=setdefault#dict.setdefault). > > hint: your_dict(your_key, set()).add(the_gene) will work whether or not > the key has been encountered before or not. I meant: your_dict.setdefault(your_key, set()).add(the_gene) From mariopy at gmx.com Wed Jun 4 07:47:44 2014 From: mariopy at gmx.com (Mario Py) Date: Tue, 03 Jun 2014 23:47:44 -0600 Subject: [Tutor] Android Message-ID: <538EB300.1030206@gmx.com> I'm writing one small simple program that will use Tkinter. Once finished, will I be able to run it on android tablet? From alan.gauld at btinternet.com Wed Jun 4 10:54:36 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 04 Jun 2014 09:54:36 +0100 Subject: [Tutor] Android In-Reply-To: <538EB300.1030206@gmx.com> References: <538EB300.1030206@gmx.com> Message-ID: On 04/06/14 06:47, Mario Py wrote: > I'm writing one small simple program that will use Tkinter. > Once finished, will I be able to run it on android tablet? I don't think there is a port of Tk to Android so the answer would be no. But I may be wrong, Things move so fast in the App wold its hard to keep up! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From jarod_v6 at libero.it Wed Jun 4 12:29:38 2014 From: jarod_v6 at libero.it (jarod_v6 at libero.it) Date: Wed, 4 Jun 2014 12:29:38 +0200 (CEST) Subject: [Tutor] How to create a dictionary for ount elements Message-ID: <931987894.5598541401877778062.JavaMail.actor@webmail27> Dear all thanks for your suggestion!!! Thanks to your suggestion I create this structure:with open("prova.csv") as p: for i in p: lines =i.rstrip("\n").split("\t") ...: print lines ...: ['programs ', 'sample', 'gene', 'values'] ['program1', 'sample1', 'TP53', '2'] ['program1', 'sample1', 'TP53', '3'] ['program1', 'sample2', 'PRNP', '4'] ['program1', 'sample2', 'ATF3', '3'] ['program2', 'sample1', 'TP53', '2'] ['program2', 'sample1', 'PRNP', '5'] ['program2', 'sample2', 'TRIM32', '4'] ['program2', 'sample2', 'TLK1', '4'] In [4]: with open("prova.csv") as p: for i in p: lines =i.rstrip("\n").split("\t") dizlines diz In [4]: with open("prova.csv") as p: for i in p: lines =i.rstrip("\n").split("\t") line = (lines[0],lines[1]) ...: diz.setdefault(line,set()).add(lines[2]) ...: In [5]: diz Out[5]: {('program1', 'sample1'): {'TP53'}, ('program1', 'sample2'): {'ATF3', 'PRNP'}, ('program2', 'sample1'): {'PRNP', 'TP53'}, ('program2', 'sample2'): {'TLK1', 'TRIM32'}, ('programs ', 'sample'): {'gene'}} So what I want to do is to use intersect between the keys recursively: s = diz[('program2', 'sample1'] ....: ....: KeyboardInterrupt In [14]: s = diz[('program2', 'sample1')] In [15]: s Out[15]: {'PRNP', 'TP53'} In [16]: a Out[16]: {'ATF3', 'PRNP'} In [17]: s.inte s.intersection s.intersection_update In [17]: s.intersection(a) Out[17]: {'PRNP'} How can Have a intersect of all my dictionary and from ('program1', 'sample1') vs ('program1', 'sample2')... I want to count how many genes are common Thanks in advance for your help! From stefan_ml at behnel.de Wed Jun 4 14:38:56 2014 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 04 Jun 2014 14:38:56 +0200 Subject: [Tutor] Android In-Reply-To: <538EB300.1030206@gmx.com> References: <538EB300.1030206@gmx.com> Message-ID: Mario Py, 04.06.2014 07:47: > I'm writing one small simple program that will use Tkinter. > Once finished, will I be able to run it on android tablet? Have a look at kivy, it supports different systems, including various mobile devices. http://kivy.org/ Stefan From lu.nemec at gmail.com Wed Jun 4 18:02:00 2014 From: lu.nemec at gmail.com (Lukas Nemec) Date: Wed, 04 Jun 2014 18:02:00 +0200 Subject: [Tutor] How to create a dictionary for ount elements In-Reply-To: <931987894.5598541401877778062.JavaMail.actor@webmail27> References: <931987894.5598541401877778062.JavaMail.actor@webmail27> Message-ID: <538F42F8.8020601@gmail.com> Hi, I'd suggest using different data structure for the intersect of samples from a program. data = { 'program1': { 'sample1': {'TP53', 'ASD'}, 'sample2': {'ASD'}, }, 'program2': { 'sample1': {'ASD'} } } this way you can do this: for program in data: program_genes = [] for sample, value in data[program].iteritems(): program_genes.append(value) data[program]['intersection'] = set.intersection(*program_genes) Now you have in each 'programX' sub-dictionary key 'intersection', with intersection of all the program genes. Lukas On 06/04/2014 12:29 PM, jarod_v6 at libero.it wrote: > Dear all thanks for your suggestion!!! > Thanks to your suggestion I create this structure:with open("prova.csv") as p: > for i in p: > lines =i.rstrip("\n").split("\t") > ...: print lines > ...: > ['programs ', 'sample', 'gene', 'values'] > ['program1', 'sample1', 'TP53', '2'] > ['program1', 'sample1', 'TP53', '3'] > ['program1', 'sample2', 'PRNP', '4'] > ['program1', 'sample2', 'ATF3', '3'] > ['program2', 'sample1', 'TP53', '2'] > ['program2', 'sample1', 'PRNP', '5'] > ['program2', 'sample2', 'TRIM32', '4'] > ['program2', 'sample2', 'TLK1', '4'] > > In [4]: with open("prova.csv") as p: > for i in p: > lines =i.rstrip("\n").split("\t") > dizlines > diz > > In [4]: with open("prova.csv") as p: > for i in p: > lines =i.rstrip("\n").split("\t") > line = (lines[0],lines[1]) > ...: diz.setdefault(line,set()).add(lines[2]) > ...: > > In [5]: diz > Out[5]: > {('program1', 'sample1'): {'TP53'}, > ('program1', 'sample2'): {'ATF3', 'PRNP'}, > ('program2', 'sample1'): {'PRNP', 'TP53'}, > ('program2', 'sample2'): {'TLK1', 'TRIM32'}, > ('programs ', 'sample'): {'gene'}} > > > So what I want to do is to use intersect between the keys recursively: > s = diz[('program2', 'sample1'] > ....: > ....: > KeyboardInterrupt > > In [14]: s = diz[('program2', 'sample1')] > > In [15]: s > Out[15]: {'PRNP', 'TP53'} > > In [16]: a > Out[16]: {'ATF3', 'PRNP'} > > In [17]: s.inte > s.intersection s.intersection_update > > In [17]: s.intersection(a) > Out[17]: {'PRNP'} > > How can Have a intersect of all my dictionary and from ('program1', 'sample1') > vs ('program1', 'sample2')... > I want to count how many genes are common > Thanks in advance for your help! > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From __peter__ at web.de Wed Jun 4 12:53:16 2014 From: __peter__ at web.de (Peter Otten) Date: Wed, 04 Jun 2014 12:53:16 +0200 Subject: [Tutor] How to create a dictionary for ount elements References: <931987894.5598541401877778062.JavaMail.actor@webmail27> Message-ID: jarod_v6 at libero.it wrote: > Dear all thanks for your suggestion!!! > Thanks to your suggestion I create this structure:with open("prova.csv") > as p: > for i in p: > lines =i.rstrip("\n").split("\t") > ...: print lines > ...: > ['programs ', 'sample', 'gene', 'values'] > ['program1', 'sample1', 'TP53', '2'] > ['program1', 'sample1', 'TP53', '3'] > ['program1', 'sample2', 'PRNP', '4'] > ['program1', 'sample2', 'ATF3', '3'] > ['program2', 'sample1', 'TP53', '2'] > ['program2', 'sample1', 'PRNP', '5'] > ['program2', 'sample2', 'TRIM32', '4'] > ['program2', 'sample2', 'TLK1', '4'] > > In [4]: with open("prova.csv") as p: > for i in p: > lines =i.rstrip("\n").split("\t") > dizlines > diz > > In [4]: with open("prova.csv") as p: > for i in p: > lines =i.rstrip("\n").split("\t") > line = (lines[0],lines[1]) > ...: diz.setdefault(line,set()).add(lines[2]) > ...: > > In [5]: diz > Out[5]: > {('program1', 'sample1'): {'TP53'}, > ('program1', 'sample2'): {'ATF3', 'PRNP'}, > ('program2', 'sample1'): {'PRNP', 'TP53'}, > ('program2', 'sample2'): {'TLK1', 'TRIM32'}, > ('programs ', 'sample'): {'gene'}} > > > So what I want to do is to use intersect between the keys recursively: > s = diz[('program2', 'sample1'] > ....: > ....: > KeyboardInterrupt > > In [14]: s = diz[('program2', 'sample1')] > > In [15]: s > Out[15]: {'PRNP', 'TP53'} > > In [16]: a > Out[16]: {'ATF3', 'PRNP'} > > In [17]: s.inte > s.intersection s.intersection_update > > In [17]: s.intersection(a) > Out[17]: {'PRNP'} > > How can Have a intersect of all my dictionary and from ('program1', > 'sample1') vs ('program1', 'sample2')... > I want to count how many genes are common > Thanks in advance for your help! For that you have to map genes to program/sample pairs -- or just count them as already suggested: >>> import csv >>> from collections import Counter >>> with open("prova.csv") as f: ... rows = csv.DictReader(f, delimiter="\t") ... freq = Counter(row["gene"] for row in rows) ... >>> freq.most_common(2) [('TP53', 3), ('PRNP', 2)] From mariopy at gmx.com Wed Jun 4 16:13:00 2014 From: mariopy at gmx.com (Mario Py) Date: Wed, 04 Jun 2014 08:13:00 -0600 Subject: [Tutor] Android In-Reply-To: References: <538EB300.1030206@gmx.com> Message-ID: <538F296C.208@gmx.com> Allan, Hans and Stefan, Thank you for your replies. I will check out Kivy and Google API, thanks a lot for suggestions On 6/4/2014 6:38 AM, Stefan Behnel wrote: > Mario Py, 04.06.2014 07:47: >> I'm writing one small simple program that will use Tkinter. >> Once finished, will I be able to run it on android tablet? > > Have a look at kivy, it supports different systems, including various > mobile devices. > > http://kivy.org/ > > Stefan > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From wolfgang.maier at biologie.uni-freiburg.de Wed Jun 4 21:02:16 2014 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Wed, 04 Jun 2014 21:02:16 +0200 Subject: [Tutor] How to create a dictionary for ount elements In-Reply-To: <931987894.5598541401877778062.JavaMail.actor@webmail27> References: <931987894.5598541401877778062.JavaMail.actor@webmail27> Message-ID: <538F6D38.4080105@biologie.uni-freiburg.de> On 04.06.2014 12:29, jarod_v6 at libero.it wrote: > Dear all thanks for your suggestion!!! > > In [4]: with open("prova.csv") as p: > for i in p: > lines =i.rstrip("\n").split("\t") > line = (lines[0],lines[1]) > ...: diz.setdefault(line,set()).add(lines[2]) > ...: > > In [5]: diz > Out[5]: > {('program1', 'sample1'): {'TP53'}, > ('program1', 'sample2'): {'ATF3', 'PRNP'}, > ('program2', 'sample1'): {'PRNP', 'TP53'}, > ('program2', 'sample2'): {'TLK1', 'TRIM32'}, > ('programs ', 'sample'): {'gene'}} > > > So what I want to do is to use intersect between the keys recursively: > s = diz[('program2', 'sample1'] > ....: > ....: > KeyboardInterrupt > > In [14]: s = diz[('program2', 'sample1')] > > In [15]: s > Out[15]: {'PRNP', 'TP53'} > > In [16]: a > Out[16]: {'ATF3', 'PRNP'} > > In [17]: s.inte > s.intersection s.intersection_update > > In [17]: s.intersection(a) > Out[17]: {'PRNP'} > > How can Have a intersect of all my dictionary and from ('program1', 'sample1') > vs ('program1', 'sample2')... > I want to count how many genes are common > Thanks in advance for your help! > So you know the basic method set.intersection (or its operator form &). The rest depends on which intersections you're interested in. For all pair-wise comparisons, you can use nested for loops across all key, value pairs, like so: for k1, v1 in your_dict.iteritems(): for k2, v2 in your_dict.iteritems(): do_something() For any kind of systematic comparison, it's probably better to build a set of all programs and one of all samples during file parsing, in addition to the dict, then use their values for accessing the dict. Best, Wolfgang From colin.ross.dal at gmail.com Fri Jun 6 00:36:33 2014 From: colin.ross.dal at gmail.com (Colin Ross) Date: Thu, 5 Jun 2014 19:36:33 -0300 Subject: [Tutor] =?utf-8?q?Error_message_received_when_running_=E2=80=9Cfr?= =?utf-8?q?om_scipy_import_interpolate=E2=80=9D?= Message-ID: I am attempting to run the following in python: from scipy import interpolate I receive this error message: Traceback (most recent call last): File "", line 1, in File "/Library/Python/2.7/site-packages/scipy-0.11.0.dev_0496569_20111005-py2.7-macosx-10.7-x86_64.egg/scipy/interpolate/__init__.py", line 156, in from ndgriddata import * File "/Library/Python/2.7/site-packages/scipy-0.11.0.dev_0496569_20111005-py2.7-macosx-10.7-x86_64.egg/scipy/interpolate/ndgriddata.py", line 9, in from interpnd import LinearNDInterpolator, NDInterpolatorBase, \ File "numpy.pxd", line 172, in init interpnd (scipy/interpolate/interpnd.c:7696)ValueError: numpy.ndarray has the wrong size, try recompiling I am currently running Mac OS X Lion 10.7.5 and Python 2.7.1. with MacPorts installed. I am attempting to run the code on a different computer than it was created on. After doing some reading my understanding is that this error may be a result of the ABI not being forward compatible. To try and solve the issue I updated my MacPorts and then ran ($ sudo port upgrade outdated) to upgrade the installed ports. However, the same error continues to appear when I try and run the code. -------------- next part -------------- An HTML attachment was scrubbed... URL: From waterfallroad at gmail.com Thu Jun 5 21:02:21 2014 From: waterfallroad at gmail.com (Leo Nardo) Date: Thu, 5 Jun 2014 15:02:21 -0400 Subject: [Tutor] Ctypes and Sockets Message-ID: Looking for a complete introduction to ctypes that I can understand. I learn well from David Malans stuff, and the book 'how to think like a computer scientist for python 3'. Hope that helps with my learning style. https://www.youtube.com/results?search_query=david+malan http://openbookproject.net/thinkcs/python/english3e/index.html I would also like someones input on the following code and what it basically does. Im guessing it creates a connection between two computers but sends no meaningful data? Is there a way i can manipulate the following code to make it send strings or integers back and forth? """"""""" A simple server"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" #!/usr/bin/python # This is server.py file import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.bind((host, port)) # Bind to the port s.listen(5) # Now wait for client connection. while True: c, addr = s.accept() # Establish connection with client. print 'Got connection from', addr c.send('Thank you for connecting') c.close() # Close the connection """"""""" A simple client"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" #!/usr/bin/python # This is client.py file import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.connect((host, port)) print s.recv(1024) s.close # Close the socket when done From alan.gauld at btinternet.com Fri Jun 6 01:57:10 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 06 Jun 2014 00:57:10 +0100 Subject: [Tutor] =?windows-1252?q?Error_message_received_when_running_=93f?= =?windows-1252?q?rom_scipy_import_interpolate=94?= In-Reply-To: References: Message-ID: On 05/06/14 23:36, Colin Ross wrote: > I am attempting to run the following in python: > > |from scipyimport interpolate| This list is for those learning the core Python language and its standard library. Support for scipy is probably best gained from the scipy forum The MacPython list may also be able to help with MacOS specific queries. But there are some Mac/Scipy users here so I'll leave it to them to chime in if they can help. > I receive this error message: > > |Traceback (most recent call last): > File "", line1, in > File "/Library/Python/2.7/site-packages/scipy-0.11.0.dev_0496569_20111005-py2.7-macosx-10.7-x86_64.egg/scipy/interpolate/__init__.py", line156, in > from ndgriddataimport * > File "/Library/Python/2.7/site-packages/scipy-0.11.0.dev_0496569_20111005-py2.7-macosx-10.7-x86_64.egg/scipy/interpolate/ndgriddata.py", line9, in > from interpndimport LinearNDInterpolator, NDInterpolatorBase, \ > File "numpy.pxd", line172, in init interpnd(scipy/interpolate/interpnd.c:7696) > ValueError: numpy.ndarray has the wrong size, try recompiling| > > I am currently running Mac OS X Lion 10.7.5 and Python 2.7.1. with > MacPorts installed. > > I am attempting to run the code on a different computer than it was > created on. After doing some reading my understanding is that this error > may be a result of the ABI not being forward compatible. To try and > solve the issue I updated my MacPorts and then ran ($ sudo port upgrade > outdated) to upgrade the installed ports. However, the same error > continues to appear when I try and run the code. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Fri Jun 6 02:00:23 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 06 Jun 2014 01:00:23 +0100 Subject: [Tutor] Ctypes and Sockets In-Reply-To: References: Message-ID: On 05/06/14 20:02, Leo Nardo wrote: > Looking for a complete introduction to ctypes that I can understand. How well do you know C? Without understanding C a complete intro to ctypes will be next to impossible. > I would also like someones input on the following code and what it > basically does. Im guessing it creates a connection between two > computers but sends no meaningful data? Is there a way i can > manipulate the following code to make it send strings or integers back > and forth? Yes. You might find my tutorial on using sockets useful, it contains an example of such a program as well as some pictorial explanation of hiw it works. Its in the Python v2 tutor under the heading "Network Programming" HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From gb.gabrielebrambilla at gmail.com Fri Jun 6 04:04:35 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Thu, 5 Jun 2014 22:04:35 -0400 Subject: [Tutor] glob and file names Message-ID: Hi, I'm trying to use glob to read a file of which I don't know the complete name (but only some parts). fiLUMOname = 'Lsum_' + period + '_' + parts[2] + '_' + parts[3] + '_' + parts[4] + '_*.dat' aaa = glob.glob(fiLUMOname) print(aaa) fiLUMO = open(aaa[0], 'r') where period, and the elements of parts are strings. but aaa results empty. (and so it cannot open the file) The file exist and I'm able to recover it if I write by hand inside glob.glob() the name between " " with the * where I don't know the name. How can I pass my string to glob.glob inside " "? Is there a simpler method to obtain the same result? thanks Gabriele -------------- next part -------------- An HTML attachment was scrubbed... URL: From gb.gabrielebrambilla at gmail.com Fri Jun 6 04:15:20 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Thu, 5 Jun 2014 22:15:20 -0400 Subject: [Tutor] Fwd: glob and file names In-Reply-To: References: <539122FA.3090507@gmail.com> Message-ID: ---------- Forwarded message ---------- From: Gabriele Brambilla Date: 2014-06-05 22:15 GMT-04:00 Subject: Re: [Tutor] glob and file names To: Peter Romfeld thanks, it works. Gabriele 2014-06-05 22:10 GMT-04:00 Peter Romfeld : On Friday, June 06, 2014 10:04 AM, Gabriele Brambilla wrote: > > > > fiLUMOname = 'Lsum_' + period + '_' + parts[2] + '_' + parts[3] + '_' > > + parts[4] + '_*.dat' > > > > aaa = glob.glob(fiLUMOname) > > print(aaa) > > fiLUMO = open(aaa[0], 'r') > > i would do: > > aaa = glob.glob('Lsum_%s_%s_%s_%s_*.dat' % (period, parts[2], parts[3], > parts[4])) > > cheers > -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Fri Jun 6 05:13:43 2014 From: davea at davea.name (Dave Angel) Date: Thu, 5 Jun 2014 22:13:43 -0500 (CDT) Subject: [Tutor] glob and file names References: Message-ID: Gabriele Brambilla Wrote in message: > > (missing because you posted in html. Please tell your email program to use text) Simplify your program to where it's small and self contained. You currently have a string built up from several parts. Since you think the problem is in the glob call, you should be able to show it in 3 lines or so. filespec="f gytrr y yyrddty*.txt" hh= glob.glo... print (repr (hh)) Then specify the python version and OS. Since I have to guess, I'd say you had an unprintable in one of those variables. -- DaveA From gb.gabrielebrambilla at gmail.com Fri Jun 6 05:18:58 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Thu, 5 Jun 2014 23:18:58 -0400 Subject: [Tutor] glob and file names In-Reply-To: References: Message-ID: No, I wanted to say that Peter method works! Thanks anyway and sorry for the html. Gabriele sent from Samsung Mobile Il giorno 05/giu/2014 23:14, "Dave Angel" ha scritto: > Gabriele Brambilla Wrote in message: > > > > > (missing because you posted in html. Please tell your email program to use > text) > > Simplify your program to where it's small and self contained. You > currently have a string built up from several parts. Since you > think the problem is in the glob call, you should be able to show > it in 3 lines or so. > > filespec="f gytrr y yyrddty*.txt" > hh= glob.glo... > print (repr (hh)) > > Then specify the python version and OS. > > Since I have to guess, I'd say you had an unprintable in one of > those variables. > > > > > -- > DaveA > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Fri Jun 6 09:05:44 2014 From: __peter__ at web.de (Peter Otten) Date: Fri, 06 Jun 2014 09:05:44 +0200 Subject: [Tutor] Fwd: glob and file names References: <539122FA.3090507@gmail.com> Message-ID: Gabriele Brambilla wrote: > 2014-06-05 22:10 GMT-04:00 Peter Romfeld : > > On Friday, June 06, 2014 10:04 AM, Gabriele Brambilla wrote: >>> >>> fiLUMOname = 'Lsum_' + period + '_' + parts[2] + '_' + parts[3] + '_' >>> + parts[4] + '_*.dat' >>> >>> aaa = glob.glob(fiLUMOname) >>> print(aaa) >>> fiLUMO = open(aaa[0], 'r') >> >> i would do: >> >> aaa = glob.glob('Lsum_%s_%s_%s_%s_*.dat' % (period, parts[2], parts[3], >> parts[4])) > > thanks, it works. While Peter's way may be easier to read both approaches should give you the same result, assuming 'period' and the items in the 'parts' list are all strings. There must be an accidental change elsewhere -- maybe you changed the current working directory before invoking the script? From gb.gabrielebrambilla at gmail.com Fri Jun 6 20:16:11 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Fri, 6 Jun 2014 14:16:11 -0400 Subject: [Tutor] Fwd: glob and file names In-Reply-To: References: <539122FA.3090507@gmail.com> Message-ID: oh yes! I had a problem inside the file name! (I had a lot of file to open and I changed the one I was looking at!the one I was trying to open has a wrong filename...) Sorry for the misunderstanding. Gabriele 2014-06-06 3:05 GMT-04:00 Peter Otten <__peter__ at web.de>: > Gabriele Brambilla wrote: > > > 2014-06-05 22:10 GMT-04:00 Peter Romfeld : > > > > On Friday, June 06, 2014 10:04 AM, Gabriele Brambilla wrote: > >>> > >>> fiLUMOname = 'Lsum_' + period + '_' + parts[2] + '_' + parts[3] + '_' > >>> + parts[4] + '_*.dat' > >>> > >>> aaa = glob.glob(fiLUMOname) > >>> print(aaa) > >>> fiLUMO = open(aaa[0], 'r') > >> > >> i would do: > >> > >> aaa = glob.glob('Lsum_%s_%s_%s_%s_*.dat' % (period, parts[2], parts[3], > >> parts[4])) > > > > thanks, it works. > > While Peter's way may be easier to read both approaches should give you the > same result, assuming 'period' and the items in the 'parts' list are all > strings. There must be an accidental change elsewhere -- maybe you changed > the current working directory before invoking the script? > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Sat Jun 7 00:13:24 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 06 Jun 2014 23:13:24 +0100 Subject: [Tutor] idlex Message-ID: I'd never heard of this until today so thought I'd flag it up for the benefit of anybody who might be interested. It's at http://idlex.sourceforge.net/ Quoting from the site "IdleX is a collection of over twenty extensions and plugins that provide additional functionality to IDLE, a Python IDE provided in the standard library. It transforms IDLE into a more useful tool for academic research and development as well as exploratory programming.". It's available from pypi via easy_install or pip. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From alan.gauld at btinternet.com Sat Jun 7 02:12:09 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 07 Jun 2014 01:12:09 +0100 Subject: [Tutor] idlex In-Reply-To: References: Message-ID: On 06/06/14 23:13, Mark Lawrence wrote: > I'd never heard of this until today so thought I'd flag it up for the > benefit of anybody who might be interested. It's at > http://idlex.sourceforge.net/ IdleX is cool. They've been trying to get many of the features incorporated into core Idle for years. I don't follow the discussion closely enough to understand why there is so much reluctance to do so. So far as I can tell all the changes are good! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From gchan401 at msn.com Sat Jun 7 01:00:01 2014 From: gchan401 at msn.com (Glen Chan) Date: Fri, 6 Jun 2014 19:00:01 -0400 Subject: [Tutor] Pease help Message-ID: Hello I am a student trying to figure out this program. Here is my objective and program below. What am I doing wrong? I can't get this to work. Calculate the average pints of blood donated during a blood drive. The program should take in the number of pints donated during the drive, based on a seven hour drive period. The average pints donated during that period should be calculated and written to a file. Write a loop around the program to run multiple times. The data should be appended to the file to keep track of multiple days. If the user wants to print data from the file, read it in and then display it. Store the pints per hour and the average pints donated in a file called blood.txt. #the main function def main(): endProgram = 'no' print while endProgram == 'no': option = 0 print print 'Enter 1 to enter in new data and store to file' print 'Enter 2 to display data from the file' option = input('Enter now ->') print # declare variables pints = [0] * 7 totalPints = 0 averagePints = 0 if option == 1: # function calls pints = getPints(pints) totalPints = getTotal(pints, totalPints) averagePints = getAverage(totalPints, averagePints) else: endProgram = raw_input('Do you want to end program? (Enter no or yes): ') while not (endProgram == 'yes' or endProgram == 'no'): print 'Please enter a yes or no' endProgram = raw_input('Do you want to end program? (Enter no or yes): ') #the getPints function def getPints(pints): counter = 0 while counter < 7: pints[counter] = input('Enter pints collected: ') counter = counter + 1 return pints #the getTotal function def getTotal(pints, totalPints): counter = 0 while counter < 7: totalPints = totalPints + pints[counter] counter = counter + 1 return totalPints #the getAverage function def getAverage(totalPints, averagePints): averagePints = float(totalPints) / 7 return averagePints #the writeToFile function def writeToFile(averagePints, pints): #the readFromFile function def readFromFile(averagePints, pints): # calls main main() -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Sat Jun 7 10:19:08 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 07 Jun 2014 09:19:08 +0100 Subject: [Tutor] idlex In-Reply-To: References: Message-ID: On 07/06/2014 01:12, Alan Gauld wrote: > On 06/06/14 23:13, Mark Lawrence wrote: >> I'd never heard of this until today so thought I'd flag it up for the >> benefit of anybody who might be interested. It's at >> http://idlex.sourceforge.net/ > > > IdleX is cool. They've been trying to get many of the > features incorporated into core Idle for years. I don't > follow the discussion closely enough to understand why > there is so much reluctance to do so. So far as I can > tell all the changes are good! > Part of the problem was the question of "is this a bug or an enhancement request?". That's been resolved via PEP 434 http://legacy.python.org/dev/peps/pep-0434/ Terry Reedy & Co are doing a lot of work to improve Idle. GSOC students are helping again this summer. Jessica McClellan from the PSF is also pushing, and Raymond Hettinger has a strong interest as he teaches using Idle. Nothing like having a few big guns behind you :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From alan.gauld at btinternet.com Sat Jun 7 10:50:56 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 07 Jun 2014 09:50:56 +0100 Subject: [Tutor] Pease help In-Reply-To: References: Message-ID: On 07/06/14 00:00, Glen Chan wrote: > Hello I am a student trying to figure out this program. Here is my > objective and program below. What am I doing wrong? I can't get this to > work. > Thats not a very helpful description of the problem. Tell us what you get. Include any error messages- the full text not a summary. Also tell us the Python version you are using and the OS. In this case it looks like you are using Python v2.x but if you are trying to ru it in python3 that could be part of your problem. > #the main function > > def main(): > endProgram = 'no' > print > while endProgram == 'no': > option = 0 > print > print 'Enter 1 to enter in new data and store to file' > print 'Enter 2 to display data from the file' > option = input('Enter now ->') > print Unfortunately you posted using HTML which loses all the formatting. Find out how to post using plain text, it makes life much easier. However there are some general comments below: > # declare variables > > pints = [0] * 7 > totalPints = 0 > averagePints = 0 > if option == 1: > > # function calls > > pints = getPints(pints) > totalPints = getTotal(pints, totalPints) > averagePints = getAverage(totalPints, averagePints) > > else: > > > endProgram = raw_input('Do you want to end program? (Enter no or yes): ') > > > while not (endProgram == 'yes' or endProgram == 'no'): > > print 'Please enter a yes or no' > endProgram = raw_input('Do you want to end program? (Enter no or yes): ') > This loop doesn't seem to do much. It gets a yes/no value from the user but you don't do anything with that value? > def getPints(pints): > counter = 0 > while counter < 7: > pints[counter] = input('Enter pints collected: ') > counter = counter + 1 > return pints You could do that more easily with a for loop. for counter in range(7): pints[counter] = input(...) return pints If you have covered list comprehensions you could do it in a single line! But I'm guessing thats a step too far for now. However using input() is a bad idea. input() executes the input as if it were Python code. That's not secure because users can (deliberately or accidentally) enter damaging code which could, for example, result in your hard disk being deleted. It's better to use raw_input() for everything and convert the data from a string using int() or float() etc. > def getTotal(pints, totalPints): > counter = 0 > while counter < 7: > totalPints = totalPints + pints[counter] > counter = counter + 1 > return totalPints You can use the built-in sum() function to add all the elements in a collection totalPints = sum(pints) > def getAverage(totalPints, averagePints): > averagePints = float(totalPints) / 7 > return averagePints > def writeToFile(averagePints, pints): You don't have any code here? Not even a pass statement? > #the readFromFile function > def readFromFile(averagePints, pints): Same here. If you don't have any code to put in the body insert either a pass or an empty return statement. You can then replace it with your real code later, but at least Python will be happy. > main() -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From cs at zip.com.au Sat Jun 7 03:38:57 2014 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 7 Jun 2014 11:38:57 +1000 Subject: [Tutor] Pease help In-Reply-To: References: Message-ID: <20140607013857.GA99328@cskk.homeip.net> On 06Jun2014 19:00, Glen Chan wrote: >Hello I am a student trying to figure out this program. Here is my objective and program below. What am I doing wrong? I can't get this to work. Hi Glen, First, please configure your mailer to send plain text instead of HTML. It works better for everyone, especially with program code. Next, what does it do? You've supplied the requirements, fairly obviously a homework or tutorial exercise. However, you need to describe what you expected your program to do, and what it appears to do instead. Always try to include the output of the failing run so that people can look at it and make suggestions. Meanwhile, I'll make a few remarks about the program itself, inline below. >def main(): > endProgram = 'no' "endProgram" looks like a Boolean variable. Use a Boolean value with it: endProgram = False > print > while endProgram == 'no': And if you set endProgram as above, you can write this as: while not endProgram: > option = 0 > print > print 'Enter 1 to enter in new data and >store to file' > print 'Enter 2 to display data from the >file' > option = input('Enter now ->') Based on the "print" statements above, I'm presuming you are using Python 2. Don't use "input", use "raw_input". "input" treats the input as a Python expression, and can do all sorts of horrible and dangerous things. Instead, if "optiuon" is supposed to be an integer, do this: option = int(raw_input('Enter now ->')) [...] > endProgram = raw_input('Do you want to end program? (Enter no or yes): ') > while not (endProgram == 'yes' or endProgram == 'no'): > print 'Please enter a yes or no' [...] Based on the suggested change earlier, this loop should then be followed by: endProgram = (endProgram == 'yes') to get a Boolean value for the loop control variable "endProgram". Cheers, Cameron Simpson From jacklittlemc at yahoo.com Sat Jun 7 16:52:08 2014 From: jacklittlemc at yahoo.com (Jack Little) Date: Sat, 7 Jun 2014 07:52:08 -0700 Subject: [Tutor] SHA256 P2P Chat in python References: <1402062064.51342.YahooMailNeo@web122901.mail.ne1.yahoo.com> Message-ID: <6F63216E-2D0D-4A65-90BB-B047F74BC65A@yahoo.com> Sent from my iPhone Begin forwarded message: > From: Jack Little > Date: June 6, 2014 at 6:41:04 PDT > To: "tutor-request at python.org" > Subject: SHA256 P2P Chat in python > Reply-To: Jack Little > > Hi Mailing List. > > I was wondering if it is possible to make a p2p chat application with sha256 secured onion routing in python, and if so, what functions, guides, modules, etc. would I have to look at. > > > > Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Sun Jun 8 09:07:34 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 8 Jun 2014 00:07:34 -0700 Subject: [Tutor] SHA256 P2P Chat in python In-Reply-To: <6F63216E-2D0D-4A65-90BB-B047F74BC65A@yahoo.com> References: <1402062064.51342.YahooMailNeo@web122901.mail.ne1.yahoo.com> <6F63216E-2D0D-4A65-90BB-B047F74BC65A@yahoo.com> Message-ID: >> I was wondering if it is possible to make a p2p chat application with sha256 secured onion routing in python, and if so, what functions, guides, modules, etc. would I have to look at. This is somewhat outside of the experience of many folks on this list; you might consider asking on a broader forum. I suspect it is possible, given that Bram Cohen's original bittorent was written in Python. Try asking on the main Python mailing list. Good luck! -------------- next part -------------- An HTML attachment was scrubbed... URL: From mariopy at gmx.com Sun Jun 8 20:56:40 2014 From: mariopy at gmx.com (Mario Py) Date: Sun, 08 Jun 2014 12:56:40 -0600 Subject: [Tutor] Coma separated instead TAB separated In-Reply-To: <538F296C.208@gmx.com> References: <538EB300.1030206@gmx.com> <538F296C.208@gmx.com> Message-ID: <5394B1E8.5030907@gmx.com> Hi everyone, this is very basic/beginner question. I'm reading TXT file, two words per line that are separated by TAB: question, rightAnswer = line.strip().split('\t') I would like to use TXT file that it would be separated by coma. How do I change that line of code? I tried these two versions but it is not working: question, rightAnswer = line.strip().split('\c') # c for coma? question, rightAnswer = line.strip().split('\,') # , for coma? I have latest Python version. From alan.gauld at btinternet.com Mon Jun 9 01:33:17 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 09 Jun 2014 00:33:17 +0100 Subject: [Tutor] Coma separated instead TAB separated In-Reply-To: <5394B1E8.5030907@gmx.com> References: <538EB300.1030206@gmx.com> <538F296C.208@gmx.com> <5394B1E8.5030907@gmx.com> Message-ID: On 08/06/14 19:56, Mario Py wrote: > Hi everyone, this is very basic/beginner question. Hi, Please don;t reply to an existing message to start a new discussion. It messes up the threading and makes it harder for people searching the archives in the future. > I'm reading TXT file, two words per line that are separated by TAB: > > question, rightAnswer = line.strip().split('\t') > > I would like to use TXT file that it would be separated by coma. > How do I change that line of code? You should probably use the csv module instead, it is designed for reading comma separated files (or tab or any other character). But if you really do only have two fields and you know they won't have commas within them then you can do it with split()... > I tried these two versions but it is not working: > > question, rightAnswer = line.strip().split('\c') # c for coma? > question, rightAnswer = line.strip().split('\,') # , for coma? Since comma is not a special character you don't need the escape(\) character. So your line should just be: question, rightAnswer = line.strip().split(',') HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Mon Jun 9 03:05:21 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 9 Jun 2014 11:05:21 +1000 Subject: [Tutor] Coma separated instead TAB separated In-Reply-To: <5394B1E8.5030907@gmx.com> References: <538EB300.1030206@gmx.com> <538F296C.208@gmx.com> <5394B1E8.5030907@gmx.com> Message-ID: <20140609010521.GT10355@ando> On Sun, Jun 08, 2014 at 12:56:40PM -0600, Mario Py wrote: > Hi everyone, this is very basic/beginner question. > > I'm reading TXT file, two words per line that are separated by TAB: > > question, rightAnswer = line.strip().split('\t') For a simple format like this, that's perfectly acceptable, but for more advanced data, you should investigate the csv module. https://docs.python.org/3/library/csv.html ? > I would like to use TXT file that it would be separated by coma. > How do I change that line of code? > > I tried these two versions but it is not working: > > question, rightAnswer = line.strip().split('\c') # c for coma? > question, rightAnswer = line.strip().split('\,') # , for coma? How about "," for comma? You only need a backslash-escape for special characters that are impossible to type or otherwise tricky or inconvenient to include in strings. Tabs are tricky, because they're invisible and look like spaces. Here is a list of the escape sequences allowed: \a BEL (bell) \b BS (backspace) \f FF (formfeed) \n LF (linefeed or newline) \r CR (carriage return) \t HT (horizontal tab) \v VT (vertical tab) \0 NUL (that's a zero, not the letter Oh) \\ Backslash \' Single quote \" Double quote Of these, the most common one by far is \n. There are also escape sequences for arbitrary characters: \0dd Character dd (one or two digits) in octal (base eight) \xdd Character dd (two digits) in hexadecimal (base sixteen) In both the \0dd and \xdd cases, the value is limited to the range 0 through 255. In octal, that's 0 through 377, or in hex it is 0 to FF. A backslash followed by a newline (end of line) is a line continuation, that is, the newline is ignored: s = "this is a really, really, really \ long string." In Unicode strings, you can also use: \udddd Unicode code point U+dddd (four digits) in hexadecimal \Uddddddd Same, but eight digits \N{name} Unicode character called "name" (They must be exactly 4 digits or 8 digits, nothing in between). Last but not least, any other backslash escape \c gets left alone. -- Steven From lu.nemec at gmail.com Mon Jun 9 09:14:06 2014 From: lu.nemec at gmail.com (Lukas Nemec) Date: Mon, 09 Jun 2014 09:14:06 +0200 Subject: [Tutor] SHA256 P2P Chat in python In-Reply-To: References: <1402062064.51342.YahooMailNeo@web122901.mail.ne1.yahoo.com> <6F63216E-2D0D-4A65-90BB-B047F74BC65A@yahoo.com> Message-ID: <53955EBE.1000809@gmail.com> Hi, I did a similar thing recently, a chat, that encrypts and signs (for authenticity) each message sent with private-pub keypair: https://github.com/lunemec/python-chat. It is not p2p, it sends messages to server, which decrypts them, encrypts with its pubkey and sends to all clients for decoding. It is just a prototype and could be done better. I wanted to have the server act only as a relay for clients to discover each other (no need for DHT) and then to forward each client's pubkey to all others. I'm not sure exactly how onion routing would be done, I suppose something similar to Tor, but how would you want to use it on chat? Lukas On 06/08/2014 09:07 AM, Danny Yoo wrote: > > > >> I was wondering if it is possible to make a p2p chat application > with sha256 secured onion routing in python, and if so, what > functions, guides, modules, etc. would I have to look at. > > This is somewhat outside of the experience of many folks on this list; > you might consider asking on a broader forum. I suspect it is > possible, given that Bram Cohen's original bittorent was written in > Python. > > Try asking on the main Python mailing list. Good luck! > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From jacobjerin20 at gmail.com Mon Jun 9 07:12:56 2014 From: jacobjerin20 at gmail.com (JERIN JACOB) Date: Mon, 9 Jun 2014 10:42:56 +0530 Subject: [Tutor] Introduction Message-ID: Dear friends I am new to the mailinglist, i hope all the supports -------------- next part -------------- An HTML attachment was scrubbed... URL: From mariopy at gmx.com Mon Jun 9 02:03:39 2014 From: mariopy at gmx.com (Mario Py) Date: Sun, 08 Jun 2014 18:03:39 -0600 Subject: [Tutor] Coma separated instead TAB separated In-Reply-To: References: <538EB300.1030206@gmx.com> <538F296C.208@gmx.com> <5394B1E8.5030907@gmx.com> Message-ID: <5394F9DB.1060705@gmx.com> Thank you Alan, so simple solution! Just show how little I know! >> You should probably use the csv module instead << For now split should be OK, in the future I will try to learn CVS module. OK I understand, new subject, new post, thanks again Mario On 6/8/2014 5:33 PM, Alan Gauld wrote: > On 08/06/14 19:56, Mario Py wrote: >> Hi everyone, this is very basic/beginner question. > > Hi, > > Please don;t reply to an existing message to start a new discussion. It > messes up the threading and makes it harder for people searching the > archives in the future. > >> I'm reading TXT file, two words per line that are separated by TAB: >> >> question, rightAnswer = line.strip().split('\t') >> >> I would like to use TXT file that it would be separated by coma. >> How do I change that line of code? > > You should probably use the csv module instead, it is designed for > reading comma separated files (or tab or any other character). > But if you really do only have two fields and you know they won't have > commas within them then you can do it with split()... > >> I tried these two versions but it is not working: >> >> question, rightAnswer = line.strip().split('\c') # c for coma? >> question, rightAnswer = line.strip().split('\,') # , for coma? > > Since comma is not a special character you don't need the escape(\) > character. So your line should just be: > > question, rightAnswer = line.strip().split(',') > > HTH From mariopy at gmx.com Mon Jun 9 04:11:27 2014 From: mariopy at gmx.com (Mario Py) Date: Sun, 08 Jun 2014 20:11:27 -0600 Subject: [Tutor] Coma separated instead TAB separated In-Reply-To: <20140609010521.GT10355@ando> References: <538EB300.1030206@gmx.com> <538F296C.208@gmx.com> <5394B1E8.5030907@gmx.com> <20140609010521.GT10355@ando> Message-ID: <539517CF.10309@gmx.com> Steven, > How about "," for comma? LOL! I didn't know, now I know :-) Thank you also for all additional info. I printed it out! > Here is a list of the escape sequences allowed: > > \a BEL (bell) > \b BS (backspace) > \f FF (formfeed) > \n LF (linefeed or newline) > \r CR (carriage return) > \t HT (horizontal tab) > \v VT (vertical tab) > \0 NUL (that's a zero, not the letter Oh) > \\ Backslash > \' Single quote > \" Double quote > > > Of these, the most common one by far is \n. > > There are also escape sequences for arbitrary characters: > > \0dd Character dd (one or two digits) in octal (base eight) > \xdd Character dd (two digits) in hexadecimal (base sixteen) > > In both the \0dd and \xdd cases, the value is limited to the range 0 > through 255. In octal, that's 0 through 377, or in hex it is 0 to FF. > > A backslash followed by a newline (end of line) is a line continuation, > that is, the newline is ignored: > > s = "this is a really, really, really \ > long string." > > In Unicode strings, you can also use: > > \udddd Unicode code point U+dddd (four digits) in hexadecimal > \Uddddddd Same, but eight digits > \N{name} Unicode character called "name" > > (They must be exactly 4 digits or 8 digits, nothing in between). > > Last but not least, any other backslash escape \c gets left alone. From diliupg at gmail.com Tue Jun 10 08:42:56 2014 From: diliupg at gmail.com (diliup gabadamudalige) Date: Tue, 10 Jun 2014 12:12:56 +0530 Subject: [Tutor] which is faster Message-ID: Hi All, This is a Pygame related question and if not answered it's ok and I apologise for asking. But if someone can answer it is much appreciated. In Pygame Which is faster? 1. filling the screen with a colour or 2. blitting an image to screen Thank you for the time. May you be well. -- Diliup Gabadamudalige http://www.diliupg.com http://soft.diliupg.com/ ********************************************************************************************** This e-mail is confidential. It may also be legally privileged. If you are not the intended recipient or have received it in error, please delete it and all copies from your system and notify the sender immediately by return e-mail. Any unauthorized reading, reproducing, printing or further dissemination of this e-mail or its contents is strictly prohibited and may be unlawful. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions. ********************************************************************************************** -------------- next part -------------- An HTML attachment was scrubbed... URL: From jon.engle at gmail.com Tue Jun 10 01:33:09 2014 From: jon.engle at gmail.com (Jon Engle) Date: Mon, 9 Jun 2014 19:33:09 -0400 Subject: [Tutor] python sockets Message-ID: I am trying to open ports 1025-65535 with the following code (Mostly found online with small modifications). I am unable to "bind" anything other than the one port which is selected as input. What am I missing and how do I bind all the ports simultaneously? #!/usr/bin/python # This is server.py file from socket import * #import the socket library import thread #import the thread library startingPort=input("\nPlease enter starting port: ") startingPort=int(startingPort) def setup(): ##let's set up some constants HOST = '' #we are the host PORT = startingPort #arbitrary port not currently in use ADDR = (HOST,PORT) #we need a tuple for the address BUFSIZE = 4096 #reasonably sized buffer for data ## now we create a new socket object (serv) ## see the python docs for more information on the socket types/flags serv = socket( AF_INET,SOCK_STREAM) ##bind our socket to the address serv.bind((ADDR)) #the double parens are to create a tuple with one element serv.listen(5) #5 is the maximum number of queued connections we'll allow serv = socket( AF_INET,SOCK_STREAM) ##bind our socket to the address serv.bind((ADDR)) #the double parens are to create a tuple with one element serv.listen(5) #5 is the maximum number of queued connections we'll allow print 'listening...' PORT=PORT+1 conn,addr = serv.accept() #accept the connection print '...connected!' conn.send('TEST') conn.close() while startingPort<65535: thread.start_new_thread(setup()) startingPort=startingPort+1 -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Jun 10 09:19:59 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 10 Jun 2014 08:19:59 +0100 Subject: [Tutor] which is faster In-Reply-To: References: Message-ID: On 10/06/14 07:42, diliup gabadamudalige wrote: > Hi All, > > This is a Pygame related question and if not answered it's ok and I > apologise for asking. But if someone can answer it is much appreciated. > > In Pygame Which is faster? > > 1. filling the screen with a colour > or > 2. blitting an image to screen My guess is blitting, but it is only a guess. You'd be much better off asking the pygame community on the pygame forum/list HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From lu.nemec at gmail.com Tue Jun 10 09:28:45 2014 From: lu.nemec at gmail.com (Lukas Nemec) Date: Tue, 10 Jun 2014 09:28:45 +0200 Subject: [Tutor] python sockets In-Reply-To: References: Message-ID: <5396B3AD.6060203@gmail.com> Hi, fist - are you really triyng to have open 64 000 ports? ok, i suppose you have your reasons, but this is not a good idea - you'll block most applications that use these ports .. The problem is with your main function - you have PORT defined, but it is not global, it is only local, and when you add +1 to it, next spawned process will have PORT with previous value. either use global PORT, or have it as a parameter for the function: main(port): ...... PORT = port while startingPort<65535: thread.start_new_thread(setup(startingPort)) startingPort=startingPort+1 Lukas On 06/10/2014 01:33 AM, Jon Engle wrote: > I am trying to open ports 1025-65535 with the following code (Mostly > found online with small modifications). I am unable to "bind" anything > other than the one port which is selected as input. What am I missing > and how do I bind all the ports simultaneously? > > #!/usr/bin/python # This is server.py file > from socket import * #import the socket library > import thread #import the thread library > > startingPort=input("\nPlease enter starting port: ") > startingPort=int(startingPort) > > def setup(): > ##let's set up some constants > HOST = '' #we are the host > PORT = startingPort #arbitrary port not currently in use > ADDR = (HOST,PORT) #we need a tuple for the address > BUFSIZE = 4096 #reasonably sized buffer for data > > ## now we create a new socket object (serv) > ## see the python docs for more information on the socket types/flags > serv = socket( AF_INET,SOCK_STREAM) > > ##bind our socket to the address > serv.bind((ADDR)) #the double parens are to create a tuple with one > element > serv.listen(5) #5 is the maximum number of queued connections we'll > allow > > serv = socket( AF_INET,SOCK_STREAM) > > ##bind our socket to the address > serv.bind((ADDR)) #the double parens are to create a tuple with one > element > serv.listen(5) #5 is the maximum number of queued connections we'll > allow > print 'listening...' > PORT=PORT+1 > conn,addr = serv.accept() #accept the connection > print '...connected!' > conn.send('TEST') > conn.close() > > while startingPort<65535: > thread.start_new_thread(setup()) > startingPort=startingPort+1 > > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Jun 10 09:35:34 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 10 Jun 2014 08:35:34 +0100 Subject: [Tutor] python sockets In-Reply-To: References: Message-ID: On 10/06/14 00:33, Jon Engle wrote: > I am trying to open ports 1025-65535 with the following code Why would you want to do that? It sounds like a great way to cripple your PC as it runs 64000 threads monitoring each of those ports. And it assumes that nothing else is using those ports already... And if you did find something trying to connect, what port is the server going to allocate? You've already grabbed them all? Can you explain your rationale for trying to do this? Unless you are trying a brute force technique to prevent anything from connecting to your computer? > found online with small modifications). I am unable to "bind" anything > other than the one port which is selected as input. What am I missing > and how do I bind all the ports simultaneously? I think you are missing the basic concepts of server computing. You should never need to bind all the ports at once. However as to your code... its hard to critique because you lost the indentation - presumably through posting in HTML? Try using plain text for posting code. > #!/usr/bin/python # This is server.py file > from socket import * #import the socket library > import thread #import the thread library > > startingPort=input("\nPlease enter starting port: ") > startingPort=int(startingPort) > > def setup(): ... > ## now we create a new socket object (serv) > ## see the python docs for more information on the socket types/flags > serv = socket( AF_INET,SOCK_STREAM) > serv.bind((ADDR)) > serv.listen(5) #5 is the maximum number of queued connections we'll allow > > serv = socket( AF_INET,SOCK_STREAM) > serv.bind((ADDR)) > serv.listen(5) #5 is the maximum number of queued connections we'll allow Why do you do it twice? > print 'listening...' Is this Python 2 or 3? Your input lines above suggest its Python 3 but this print line suggests its Python 2. Which are you using? > PORT=PORT+1 > conn,addr = serv.accept() #accept the connection > print '...connected!' > conn.send('TEST') > conn.close() You normally put the listening code inside a loop, waiting for a connection, processing it and then going back to listen some more.... > while startingPort<65535: > thread.start_new_thread(setup()) > startingPort=startingPort+1 Minor niggle, if you must do this use a for loop. Its tidier. As a minimum you need some error handling to deal with unsuccessful attempts to bind. And you need a better way of processing connections. But fundamentally, I suspect that whatever you are trying to do there is a better approach! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Tue Jun 10 13:21:59 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 10 Jun 2014 21:21:59 +1000 Subject: [Tutor] which is faster In-Reply-To: References: Message-ID: <20140610112159.GV10355@ando> On Tue, Jun 10, 2014 at 12:12:56PM +0530, diliup gabadamudalige wrote: > In Pygame Which is faster? > > 1. filling the screen with a colour > or > 2. blitting an image to screen Why don't you time it and find out? Don't forget to come back and report what you discover, I'm sure I'm not the only one who would like to know the answer to the question. http://pymotw.com/2/timeit/ More information here: https://docs.python.org/2/library/timeit.html -- Steven From __peter__ at web.de Tue Jun 10 10:07:02 2014 From: __peter__ at web.de (Peter Otten) Date: Tue, 10 Jun 2014 10:07:02 +0200 Subject: [Tutor] python sockets References: <5396B3AD.6060203@gmail.com> Message-ID: Lukas Nemec wrote: > Hi, > > fist - are you really triyng to have open 64 000 ports? ok, i suppose > you have your reasons, but this is not a good idea - you'll block most > applications that use these ports .. > > The problem is with your main function - > you have PORT defined, but it is not global, it is only local, and when > you add +1 to it, next spawned process will have PORT with previous value. > > either use global PORT, or have it as a parameter for the function: > > main(port): > ...... > PORT = port > > while startingPort<65535: > thread.start_new_thread(setup(startingPort)) > startingPort=startingPort+1 setup() is still called in the main thread, likely listens forever which is why thread.start_new_thread() is never called and therefore doesn't complain about the missing argument... Try def setup(PORT): ... # don't reassign port inside the function for port in range(startingPort, 65535): thread.start_new_thread(setup, (port,)) Note that some_func(setup(port)) passes the result of the setup() call to some_func while some_func(setup, (port,)) passes the setup function and a 1-tuple with the port as its only item. The comma is necessary to create a tuple, parentheses alone have no effect: >>> (1) 1 >>> (1,) (1,) PS: You should also consider using the (higlevel) threading module instead of the thread module. From __peter__ at web.de Tue Jun 10 10:43:13 2014 From: __peter__ at web.de (Peter Otten) Date: Tue, 10 Jun 2014 10:43:13 +0200 Subject: [Tutor] [OT] Long delay until my posts appear Message-ID: I'm posting via gmane. Since last month there is a delay (usually a few hours I think) until my posts appear and I seem to be getting a "Your message to Tutor awaits moderator approval, would you like to cancel..." mail every time I post. I'm trying hard to not get annoyed ;) Is there something I can do to go back to how it used to work (post appears within minutes, no helpful spam)? Thank you. From jon.engle at gmail.com Tue Jun 10 12:55:08 2014 From: jon.engle at gmail.com (Jon Engle) Date: Tue, 10 Jun 2014 06:55:08 -0400 Subject: [Tutor] Tutor Digest, Vol 124, Issue 21 In-Reply-To: References: Message-ID: Thank you for taking the time to help me understand, here is what I am trying to accomplish: Client FW Server ------------- 1024 | Allow | 1024 1025 | Deny | 1025 1026 | Allow | 1026 65535 | .... | 65535 I am trying to test the outbound configuration of a firewall (treat it like a black box) for purposes of validating current configurations. The client side of this is pretty straightforward but the server side is where I run into issues. I am trying to keep this as simple as possible as I am pretty new to Python. One of the challenges I see is not having a communications channel between the client and the server. In the example above what happens when port 1025 is blocked outbound and the client never receives a response from the server? How would I make the client and the server synchronize ports? I considered timers but again I am trying to keep this simple. So that is what lead me to the path of binding all the ports at once so that the client doesn't have to care what the server did/did not receive. In my case I felt that binding all the server ports at once,was the simplest solution but I am certainly open to other/better ways. I am currently using python 2.7.5 On Tue, Jun 10, 2014 at 6:00 AM, wrote: > Send Tutor mailing list submissions to > tutor at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > https://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to > tutor-request at python.org > > You can reach the person managing the list at > tutor-owner at python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > 1. Re: python sockets (Alan Gauld) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Tue, 10 Jun 2014 08:35:34 +0100 > From: Alan Gauld > To: tutor at python.org > Subject: Re: [Tutor] python sockets > Message-ID: > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > On 10/06/14 00:33, Jon Engle wrote: > > I am trying to open ports 1025-65535 with the following code > > Why would you want to do that? > It sounds like a great way to cripple your PC as it runs 64000 threads > monitoring each of those ports. And it assumes that nothing else is > using those ports already... And if you did find something trying to > connect, what port is the server going to allocate? You've already > grabbed them all? > > Can you explain your rationale for trying to do this? Unless you are > trying a brute force technique to prevent anything from connecting to > your computer? > > > found online with small modifications). I am unable to "bind" anything > > other than the one port which is selected as input. What am I missing > > and how do I bind all the ports simultaneously? > > I think you are missing the basic concepts of server computing. You > should never need to bind all the ports at once. > > However as to your code... its hard to critique because you lost the > indentation - presumably through posting in HTML? Try using plain text > for posting code. > > > #!/usr/bin/python # This is server.py file > > from socket import * #import the socket library > > import thread #import the thread library > > > > startingPort=input("\nPlease enter starting port: ") > > startingPort=int(startingPort) > > > > def setup(): > ... > > ## now we create a new socket object (serv) > > ## see the python docs for more information on the socket types/flags > > serv = socket( AF_INET,SOCK_STREAM) > > serv.bind((ADDR)) > > serv.listen(5) #5 is the maximum number of queued connections we'll > allow > > > > serv = socket( AF_INET,SOCK_STREAM) > > serv.bind((ADDR)) > > serv.listen(5) #5 is the maximum number of queued connections we'll > allow > > > Why do you do it twice? > > > print 'listening...' > > Is this Python 2 or 3? Your input lines above suggest its Python 3 but > this print line suggests its Python 2. Which are you using? > > > PORT=PORT+1 > > conn,addr = serv.accept() #accept the connection > > print '...connected!' > > conn.send('TEST') > > conn.close() > > You normally put the listening code inside a loop, waiting for a > connection, processing it and then going back to listen some more.... > > > while startingPort<65535: > > thread.start_new_thread(setup()) > > startingPort=startingPort+1 > > Minor niggle, if you must do this use a for loop. Its tidier. > As a minimum you need some error handling to deal with > unsuccessful attempts to bind. And you need a better way > of processing connections. > > But fundamentally, I suspect that whatever you are trying to > do there is a better approach! > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Tutor maillist - Tutor at python.org > https://mail.python.org/mailman/listinfo/tutor > > > ------------------------------ > > End of Tutor Digest, Vol 124, Issue 21 > ************************************** > -- Cheers, Jon S. Engle jon.engle at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From yanghe8 at qq.com Tue Jun 10 14:07:56 2014 From: yanghe8 at qq.com (=?ISO-8859-1?B?SGUgWWFuZw==?=) Date: Tue, 10 Jun 2014 20:07:56 +0800 Subject: [Tutor] python ODE and DAE solver Message-ID: Hi, I use Maple for my modelling work, and now I am considering switching to Python. Lately, I've spend some time on the documentation of python and SciPy( python based maths package), but only managed to find a ODE solver, can someone familiar with python ODE and DAE solver send me more information about those solvers, thanks in advance. in addition, i am trying to employ cloud computing servers to solve equations, i am not sure whether the server would allow me to link python and other programming languages like C or Fortran, anyone knows this? H. Y. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Jun 10 14:30:06 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 10 Jun 2014 13:30:06 +0100 Subject: [Tutor] [OT] Long delay until my posts appear In-Reply-To: References: Message-ID: On 10/06/14 09:43, Peter Otten wrote: > I'm posting via gmane. Since last month there is a delay (usually a few > hours I think) until my posts appear and I seem to be getting a > > "Your message to Tutor awaits moderator approval, would you like to > cancel..." Something has changed on the list server which means that any time someone edits their settings it automatically sets up full moderation. Presumably you changed a setting somewhere... Anyway I have now gone in and switched off moderation for your account. Normal service should have resumed. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Tue Jun 10 14:49:14 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 10 Jun 2014 13:49:14 +0100 Subject: [Tutor] python ODE and DAE solver In-Reply-To: References: Message-ID: On 10/06/14 13:07, He Yang wrote: > I use Maple for my modelling work, and now I am considering switching to > Python. Lately, I've spend some time on the documentation of python > and SciPy( python based maths package), but only managed to find a ODE > solver, can someone familiar with python ODE and DAE solver .... There is a dedicated Numpy/Scipy mailing list, you will probabnly get better results asking there. This list is really for core Python and the standard library. There are a few scipy users here though so you may get lucky... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From wrw at mac.com Tue Jun 10 18:09:35 2014 From: wrw at mac.com (William Ray Wing) Date: Tue, 10 Jun 2014 12:09:35 -0400 Subject: [Tutor] which is faster In-Reply-To: References: Message-ID: <968E82A5-018A-46B8-A2B8-8AA7BBA8F978@mac.com> On Jun 10, 2014, at 2:42 AM, diliup gabadamudalige wrote: > Hi All, > > This is a Pygame related question and if not answered it's ok and I apologise for asking. But if someone can answer it is much appreciated. > > In Pygame Which is faster? > > 1. filling the screen with a colour > or > 2. blitting an image to screen > > Thank you for the time. > May you be well. > > > -- > Diliup Gabadamudalige > I don?t use Pygame, but it seems to me the answer is likely to depend on what graphics hardware is available on the computer in question. -Bill From awg1 at gmx.com Tue Jun 10 17:51:20 2014 From: awg1 at gmx.com (Adam Gold) Date: Tue, 10 Jun 2014 16:51:20 +0100 Subject: [Tutor] code review Message-ID: <53972978.5090600@gmx.com> Hi there. I've been writing a script that is now finished and working (thanks, in part, to some helpful input from this board). What I'd really like to do now is go through it with an 'expert' who can point out ways I may have been able to code more efficiently/effectively. I don't think it would be appropriate to post the whole script here and ask "how could I do this better" (!) so I was wondering if anyone knows of ways for python noobs to connect with python experts for this sort of exercise. I understand people can be really busy so I'm happy to pay for someone's time if necessary. From jon.engle at gmail.com Tue Jun 10 18:28:22 2014 From: jon.engle at gmail.com (Jon Engle) Date: Tue, 10 Jun 2014 12:28:22 -0400 Subject: [Tutor] python sockets Message-ID: Thank you for your help! This updated code does not "bind" the selected port to a "listen" state, it simply exits. I feel like part of this has to do with the creation of a procedure. Any ideas/recommendations on how to make this loop "bind" to a socket? #!/usr/bin/python # This is server.py file from socket import * #import the socket library import thread #import the thread library startingPort=input("\nPlease enter starting port: ") startingPort=int(startingPort) def setup(PORT): ##let's set up some constants HOST = '' #we are the host PORT = startingPort #arbitrary port not currently in use ADDR = (HOST,PORT) #we need a tuple for the address BUFSIZE = 4096 #reasonably sized buffer for data ## now we create a new socket object (serv) ## see the python docs for more information on the socket types/flags serv = socket( AF_INET,SOCK_STREAM) ##bind our socket to the address serv.bind((ADDR)) #the double parens are to create a tuple with one element serv.listen(5) #5 is the maximum number of queued connections we'll allow print 'listening...' conn,addr = serv.accept() #accept the connection print '...connected!' conn.send('TEST') conn.close() for port in range (startingPort, 65535): thread.start_new_thread(setup, (port,)) startingPort=startingPort+1 #print startingPort -------------- next part -------------- An HTML attachment was scrubbed... URL: From stephenb at nmsu.edu Tue Jun 10 16:55:18 2014 From: stephenb at nmsu.edu (Stephen Brazil) Date: Tue, 10 Jun 2014 14:55:18 +0000 Subject: [Tutor] Simple python help Message-ID: Hello! I am brand new to python and need to know how to make the attached lines of code work. Should be pretty self-explanatory. [cid:40C1630E-BBA6-41A6-A641-3B1FBE3CBFB8] -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Screen Shot 2014-06-10 at 10.43.50 AM.png Type: image/png Size: 40476 bytes Desc: Screen Shot 2014-06-10 at 10.43.50 AM.png URL: From joel.goldstick at gmail.com Tue Jun 10 21:21:50 2014 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 10 Jun 2014 15:21:50 -0400 Subject: [Tutor] Simple python help In-Reply-To: References: Message-ID: On Jun 10, 2014 3:14 PM, "Stephen Brazil" wrote: > > Hello! I am brand new to python and need to know how to make the attached lines of code work. Should be pretty self-explanatory. > > You need to quote 'Stephen' _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Tue Jun 10 21:25:55 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 10 Jun 2014 20:25:55 +0100 Subject: [Tutor] Simple python help In-Reply-To: References: Message-ID: On 10/06/2014 15:55, Stephen Brazil wrote: > Hello! I am brand new to python and need to know how to make the > attached lines of code work. Should be pretty self-explanatory. > I am not responding to Stephen and his amazing technicolour code, sorry :-( -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From chigga101 at gmail.com Tue Jun 10 21:50:34 2014 From: chigga101 at gmail.com (Matthew Ngaha) Date: Tue, 10 Jun 2014 20:50:34 +0100 Subject: [Tutor] python sockets In-Reply-To: References: Message-ID: On Tue, Jun 10, 2014 at 5:28 PM, Jon Engle wrote: > > startingPort=input("\nPlease enter starting port: ") > startingPort=int(startingPort) > > def setup(PORT): > PORT = startingPort #arbitrary port not currently in use There's a conflict with this PORT variable. From marc.tompkins at gmail.com Tue Jun 10 21:53:17 2014 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 10 Jun 2014 12:53:17 -0700 Subject: [Tutor] Simple python help In-Reply-To: References: Message-ID: On Tue, Jun 10, 2014 at 7:55 AM, Stephen Brazil wrote: > Hello! I am brand new to python and need to know how to make the attached > lines of code work. Should be pretty > You need quotes. Stephen (without quotes) is an object, which you haven't previously defined. 'Stephen' is a string, which can be compared to person. Which version of Python are you using? If it's 2.x, PLEASE don't use input() - use raw_input() instead. In 3.x, input() does what raw_input() used to, so it's cool. Finally, you're likely to catch some grief for posting your code as a picture; in future, post code as plain text. (Sure, we won't see the nifty syntax highlighting, but that's OK!) Every single post to this list that's anything but plain text generates at least one complaint; it significantly lowers the signal-to-noise ratio. -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Tue Jun 10 21:55:47 2014 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 10 Jun 2014 15:55:47 -0400 Subject: [Tutor] Simple python help In-Reply-To: References: Message-ID: On Jun 10, 2014 3:14 PM, "Stephen Brazil" wrote: > > Hello! I am brand new to python and need to know how to make the attached lines of code work. Should be pretty self-explanatory. > > Also, copy and paste code. Use text only not rtfm or html to post. Lose the curly braces. Python doesn't need them _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From marc.tompkins at gmail.com Tue Jun 10 22:23:41 2014 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 10 Jun 2014 13:23:41 -0700 Subject: [Tutor] python sockets In-Reply-To: References: Message-ID: On Tue, Jun 10, 2014 at 9:28 AM, Jon Engle wrote: > for port in range (startingPort, 65535): > thread.start_new_thread(setup, (port,)) > startingPort=startingPort+1 > #print startingPort > I think you just need this: for port in range (startingPort, 65535): > thread.start_new_thread(setup(port)) > #print port > and inside of setup, get rid of this line: PORT = startingPort #arbitrary port not currently in use -------------- next part -------------- An HTML attachment was scrubbed... URL: From marc.tompkins at gmail.com Tue Jun 10 23:07:37 2014 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 10 Jun 2014 14:07:37 -0700 Subject: [Tutor] python sockets In-Reply-To: References: Message-ID: On Tue, Jun 10, 2014 at 2:00 PM, Jon Engle wrote: > Ok, so after making the changes the code does bind the startingPort > variable but that is the only port that gets bound. Also when connecting to > the startingPort I receive the following error: > > Please enter starting port: 65520 > > listening... > > ...connected! > > Traceback (most recent call last): > > File "response.py", line 31, in > > thread.start_new_thread(setup(port)) > > TypeError: start_new_thread expected at least 2 arguments, got 1 > Sorry about that! I should have read the docs (https://docs.python.org/2/library/thread.html) for thread.start_new_thread(); you had it right in the code you posted (it was the rest of your loop that was a problem.) So, change it back to: for port in range (startingPort, 65535): > thread.start_new_thread(setup, (port,)) > #print port > My apologies for the bum steer. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Wed Jun 11 00:56:16 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 11 Jun 2014 08:56:16 +1000 Subject: [Tutor] which is faster In-Reply-To: References: <20140610112159.GV10355@ando> Message-ID: <20140610225616.GX10355@ando> On Tue, Jun 10, 2014 at 09:57:51PM +0530, diliup gabadamudalige wrote: > Thank you very much for the links. I will install this module, time the two > code snippets and then share the info. Thank you very much! You're welcome, but there is no need to install the timeit module, it is part of the standard library that is pre-installed with every Python since version 2.3 (about eleven years ago). -- Steven From steve at pearwood.info Wed Jun 11 01:04:06 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 11 Jun 2014 09:04:06 +1000 Subject: [Tutor] code review In-Reply-To: <53972978.5090600@gmx.com> References: <53972978.5090600@gmx.com> Message-ID: <20140610230406.GY10355@ando> On Tue, Jun 10, 2014 at 04:51:20PM +0100, Adam Gold wrote: > Hi there. I've been writing a script that is now finished and working > (thanks, in part, to some helpful input from this board). What I'd > really like to do now is go through it with an 'expert' who can point > out ways I may have been able to code more efficiently/effectively. I > don't think it would be appropriate to post the whole script here and > ask "how could I do this better" (!) so I was wondering if anyone knows > of ways for python noobs to connect with python experts for this sort of > exercise. I understand people can be really busy so I'm happy to pay > for someone's time if necessary. How big is the script? A single file, or hundreds of files? Fifty lines of code? A thousand? In simple English, what does it do? Does it require specialized knowledge to understand? Is it available somewhere on the Internet? E.g. on Google code, github, sourceforge, your own personal website? Are there confidentiality restrictions on it? The answer to these questions will influence the type of code review you get. -- Steven From steve at pearwood.info Wed Jun 11 01:21:08 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 11 Jun 2014 09:21:08 +1000 Subject: [Tutor] python ODE and DAE solver In-Reply-To: References: Message-ID: <20140610232108.GZ10355@ando> On Tue, Jun 10, 2014 at 08:07:56PM +0800, He Yang wrote: > Hi, > > I use Maple for my modelling work, and now I am considering switching > to Python. Lately, I've spend some time on the documentation of python > and SciPy( python based maths package), but only managed to find a ODE > solver, can someone familiar with python ODE and DAE solver send me > more information about those solvers, thanks in advance. This is fairly specialised information, you might try here: http://www.scipy.org/scipylib/mailing-lists.html sympy includes a simple ODE solver: http://docs.sympy.org/dev/modules/mpmath/calculus/odes.html > in addition, > i am trying to employ cloud computing servers to solve equations, i am > not sure whether the server would allow me to link python and other > programming languages like C or Fortran, anyone knows this? That will surely depend on what cloud computing servers you have access to, and what capabilities they offer. If you have full, unrestricted access to a server, then you can do anything you want. If you don't, then you'll need to find out what access you actually do have. -- Steven From akleider at sonic.net Wed Jun 11 01:17:00 2014 From: akleider at sonic.net (Alex Kleider) Date: Tue, 10 Jun 2014 16:17:00 -0700 Subject: [Tutor] Simple python help In-Reply-To: References: Message-ID: <4b93eb3f83e7bf6334995eae9393169d@sonic.net> On 2014-06-10 07:55, Stephen Brazil wrote: > Hello! I am brand new to python and need to know how to make the > attached lines of code work. Should be pretty self-explanatory. > [cid:40C1630E-BBA6-41A6-A641-3B1FBE3CBFB8] > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor I am curious to know if this is interpretable (i.e. does the "cid" in square brackets refer to anything meaningful?) ?perhaps the list strips attachments and substitutes some other reference? From alan.gauld at btinternet.com Wed Jun 11 02:05:40 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 11 Jun 2014 01:05:40 +0100 Subject: [Tutor] Simple python help In-Reply-To: References: Message-ID: On 10/06/14 15:55, Stephen Brazil wrote: > Hello! I am brand new to python and need to know how to make the > attached lines of code work. Should be pretty self-explanatory. Not if your mail tool doesn't display images. This is a text based list so your code is invisible... But on another mail reader.... You have semi colons which are not needed in Python You have curly braces which are not needed in Python You have no quotes around your string (stephen) which are needed You have no colons after the if or else statements, which are needed. You need to go back to your tutorial and read it more carefully. And remember that Python is not C/C++/Java/JavaScript/Perl or PHP... Or try mine, especially the topic on Branching -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From lu.nemec at gmail.com Wed Jun 11 08:23:37 2014 From: lu.nemec at gmail.com (Lukas Nemec) Date: Wed, 11 Jun 2014 08:23:37 +0200 Subject: [Tutor] code review In-Reply-To: <53972978.5090600@gmx.com> References: <53972978.5090600@gmx.com> Message-ID: <5397F5E9.10902@gmail.com> Post it somewhere on github and I'll try to take a look at it. Lukas On 06/10/2014 05:51 PM, Adam Gold wrote: > Hi there. I've been writing a script that is now finished and working > (thanks, in part, to some helpful input from this board). What I'd > really like to do now is go through it with an 'expert' who can point > out ways I may have been able to code more efficiently/effectively. I > don't think it would be appropriate to post the whole script here and > ask "how could I do this better" (!) so I was wondering if anyone knows > of ways for python noobs to connect with python experts for this sort of > exercise. I understand people can be really busy so I'm happy to pay > for someone's time if necessary. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From awg1 at gmx.com Wed Jun 11 01:30:54 2014 From: awg1 at gmx.com (Adam Gold) Date: Wed, 11 Jun 2014 00:30:54 +0100 Subject: [Tutor] code review In-Reply-To: <20140610230406.GY10355@ando> References: <53972978.5090600@gmx.com> <20140610230406.GY10355@ando> Message-ID: <5397952E.50700@gmx.com> On 11/06/14 00:04, Steven D'Aprano wrote: > On Tue, Jun 10, 2014 at 04:51:20PM +0100, Adam Gold wrote: >> Hi there. I've been writing a script that is now finished and working >> (thanks, in part, to some helpful input from this board). What I'd >> really like to do now is go through it with an 'expert' who can point >> out ways I may have been able to code more efficiently/effectively. I >> don't think it would be appropriate to post the whole script here and >> ask "how could I do this better" (!) so I was wondering if anyone knows >> of ways for python noobs to connect with python experts for this sort of >> exercise. I understand people can be really busy so I'm happy to pay >> for someone's time if necessary. > > > How big is the script? A single file, or hundreds of files? Fifty lines > of code? A thousand? Thanks for the reply Steven. It's no more than 100 lines at a guess (I'm quite a noob although I think/hope that the brevity of the script may be partially attributable to some choices I made). > In simple English, what does it do? Does it require specialized > knowledge to understand? It's a backup script, more specifically: - create lv snapshots of pre-selected logical volumes on my server which are running xen VMs - dd and bzip2 (using a pipe) the snapshots to .img.bz2 files for storage on the same server - gpg encrypt the same files and uploads them to s3 - removes the lv snapshots and the .gpg files - deletes files in the S3 directory which are older than X days > > Is it available somewhere on the Internet? E.g. on Google code, github, > sourceforge, your own personal website? Are there confidentiality > restrictions on it? The are no restrictions (license or otherwise), no confidentiality issues. The file is not currently available on the internet but I can make it available easily enough on pastebin, as a downloadable file etc. > The answer to these questions will influence the type of code review > you get. I look forward to hearing further thoughts. From jon.engle at gmail.com Tue Jun 10 23:00:54 2014 From: jon.engle at gmail.com (Jon Engle) Date: Tue, 10 Jun 2014 17:00:54 -0400 Subject: [Tutor] python sockets In-Reply-To: References: Message-ID: Ok, so after making the changes the code does bind the startingPort variable but that is the only port that gets bound. Also when connecting to the startingPort I receive the following error: Please enter starting port: 65520 listening... ...connected! Traceback (most recent call last): File "response.py", line 31, in thread.start_new_thread(setup(port)) TypeError: start_new_thread expected at least 2 arguments, got 1 On Tue, Jun 10, 2014 at 4:23 PM, Marc Tompkins wrote: > On Tue, Jun 10, 2014 at 9:28 AM, Jon Engle wrote: > > >> for port in range (startingPort, 65535): >> thread.start_new_thread(setup, (port,)) >> startingPort=startingPort+1 >> #print startingPort >> > > I think you just need this: > > for port in range (startingPort, 65535): >> thread.start_new_thread(setup(port)) >> #print port >> > > and inside of setup, get rid of this line: > PORT = startingPort #arbitrary port not currently in use > > -- Cheers, Jon S. Engle jon.engle at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From jon.engle at gmail.com Wed Jun 11 01:08:42 2014 From: jon.engle at gmail.com (Jon Engle) Date: Tue, 10 Jun 2014 19:08:42 -0400 Subject: [Tutor] python sockets Message-ID: Ok, so when I run the code it immediately terminates and never 'listens' to the ports in the loop. I have verified by running netstat -an | grep 65530 and the startingPort is not binding. ***Server*** Jons-Mac:Desktop Jon$ python response.py Please enter starting port: 65530 Jons-Mac:Desktop Jon$ Jons-Mac:Desktop Jon$ netstat -an | grep 65530 Jons-MacDesktop Jon$ ***Code*** #!/usr/bin/python # This is server.py file from socket import * #import the socket library import thread #import the thread library startingPort=input("\nPlease enter starting port: ") startingPort=int(startingPort) def setup(PORT): ##let's set up some constants HOST = '' #we are the host #PORT = startingPort #arbitrary port not currently in use ADDR = (HOST,PORT) #we need a tuple for the address BUFSIZE = 4096 #reasonably sized buffer for data ## now we create a new socket object (serv) ## see the python docs for more information on the socket types/flags serv = socket( AF_INET,SOCK_STREAM) ##bind our socket to the address serv.bind((ADDR)) #the double parens are to create a tuple with one element serv.listen(5) #5 is the maximum number of queued connections we'll allow print 'listening...' conn,addr = serv.accept() #accept the connection print '...connected!' conn.send('TEST') conn.close() for port in range (startingPort, 65535): thread.start_new_thread(setup, (port,)) startingPort=startingPort+1 #print startingPort -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Jun 11 09:11:01 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 11 Jun 2014 08:11:01 +0100 Subject: [Tutor] code review In-Reply-To: <5397952E.50700@gmx.com> References: <53972978.5090600@gmx.com> <20140610230406.GY10355@ando> <5397952E.50700@gmx.com> Message-ID: On 11/06/14 00:30, Adam Gold wrote: > Thanks for the reply Steven. It's no more than 100 lines at a guess In that case just copy and paste it into a message and send it to the group. Anyone with time available can then take a peek. hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Wed Jun 11 09:17:48 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 11 Jun 2014 08:17:48 +0100 Subject: [Tutor] python sockets In-Reply-To: References: Message-ID: On 11/06/14 00:08, Jon Engle wrote: > Ok, so when I run the code it immediately terminates and never 'listens' This has nothing to do with your immediate problem but... > ***Code*** > > #!/usr/bin/python # This is server.py file > from socket import * #import the socket library > import thread#import the thread library > > startingPort=input("\nPlease enter starting port: ") > startingPort=int(startingPort) You said you were using Python 2.7. Do not use input() in Python v2 use raw_input() instead. Especially since you are using int() to convert the data anyway. v2 input() evaluates whatever the user types which could be damaging code (whether deliberate or inadvertent). v2 input() is so dangerous it was removed from v3 and raw_input renamed as input. (Which has just created lots of confusion IMHO!) > def setup(PORT): > ##let's set up some constants > HOST = '' #we are the host BTW I'm still losing nearly all indentation on your posts. Are you posting in plain text? Nobody else is complaining so it might just be my reader that's barfing on it... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From marc.tompkins at gmail.com Wed Jun 11 10:16:53 2014 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Wed, 11 Jun 2014 01:16:53 -0700 Subject: [Tutor] python sockets In-Reply-To: References: Message-ID: On Tue, Jun 10, 2014 at 4:08 PM, Jon Engle wrote: > Ok, so when I run the code it immediately terminates and never 'listens' to > the ports in the loop. I have verified by running netstat -an | grep 65530 > and the startingPort is not binding. The problem is that all threads started by a program terminate when the program terminates - and you haven't told your program to stick around when it's done setting up. So it's setting up and then immediately exiting - and by the time you run netstat a few seconds later you find nothing. (Also, by leaving HOST = '', you're listening at address 0.0.0.0, so good luck catching any traffic...) Try something like this: #!/usr/bin/python # This is server.py file from socket import * #import the socket library import thread #import the thread library def setup(PORT): HOST = '127.0.0.1' #we are the host ADDR = (HOST,PORT) #we need a tuple for the address BUFSIZE = 4096 #reasonably sized buffer for data serv = socket( AF_INET,SOCK_STREAM) serv.bind((ADDR)) #the double parens are to create a tuple with one element serv.listen(5) #5 is the maximum number of queued connections we'll allow print '\nlistening on port %i...' % PORT conn,addr = serv.accept() #accept the connection print '\n...port %i connected!' % PORT conn.send('TEST') conn.close() def main(): startingPort=int(raw_input("\nPlease enter starting port: ")) for port in range (startingPort, 65535): thread.start_new_thread(setup, (port,)) quitNow = '' while quitNow not in ('Q', 'q'): quitNow = raw_input('Enter Q to quit.') if __name__ == '__main__': main() This will stick around until you enter 'Q', and if you run netstat in another window you'll see that it's LISTENING on all the ports you asked for. (All of those print statements will show up in a surprising order!) I'm not running the other side of this experiment, so I haven't tested a successful connection... good luck. From linux at barrowhillfarm.org.uk Wed Jun 11 10:40:28 2014 From: linux at barrowhillfarm.org.uk (Bob Williams) Date: Wed, 11 Jun 2014 09:40:28 +0100 Subject: [Tutor] code review In-Reply-To: References: <53972978.5090600@gmx.com> <20140610230406.GY10355@ando> <5397952E.50700@gmx.com> Message-ID: <539815FC.3030607@barrowhillfarm.org.uk> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 11/06/14 08:11, Alan Gauld wrote: > On 11/06/14 00:30, Adam Gold wrote: > >> Thanks for the reply Steven. It's no more than 100 lines at a >> guess > > In that case just copy and paste it into a message and send it to > the group. Anyone with time available can then take a peek. > > > hth One way noobs anywhere can learn is by listening in to other people's conversations - it's called lurking, I believe. So I would say, please do this on the list, and many more people than Adam may benefit. Others can ignore the thread if they wish. Bob - -- Bob Williams System: Linux 3.11.10-11-desktop Distro: openSUSE 13.1 (x86_64) with KDE Development Platform: 4.13.1 Uptime: 06:00am up 3 days 11:36, 3 users, load average: 0.05, 0.03, 0.05 -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.22 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iEYEARECAAYFAlOYFfkACgkQ0Sr7eZJrmU5EUwCgkvjIWEdp1AzodJj6j5fY5yAL wtsAoKFSwk2U4kq5HW5KsmeErH+9fcXI =lJCF -----END PGP SIGNATURE----- From __peter__ at web.de Wed Jun 11 10:44:20 2014 From: __peter__ at web.de (Peter Otten) Date: Wed, 11 Jun 2014 10:44:20 +0200 Subject: [Tutor] python sockets References: Message-ID: Jon Engle wrote: > Ok, so when I run the code it immediately terminates and never 'listens' > to the ports in the loop. I have verified by running netstat -an | grep > 65530 and the startingPort is not binding. As I've already hinted the easiest way to keep your listening threads alive is to use the threading instead of the thread module: $ cat bind_ports.py #!/usr/bin/python import socket import threading import sys HOST = '' STARTPORT = int(sys.argv[1]) ENDPORT = int(sys.argv[2]) def setup(port): print 'setting up port', port addr = (HOST, port) serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serv.bind(addr) serv.listen(1) conn, addr = serv.accept() print 'port', port, '...connected!' conn.sendall('TEST') conn.close() print 'port', port, '...CLOSED!' if __name__ == "__main__": for port in range(STARTPORT, ENDPORT): threading.Thread(target=setup, args=(port,)).start() $ python bind_ports.py 11110 11113 & [1] 9214 $ setting up port 11110 setting up port 11111 setting up port 11112 netstat -an | grep 1111 tcp 0 0 0.0.0.0:11110 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:11111 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:11112 0.0.0.0:* LISTEN To test it I've applied a minor modification to the client of the echoserver example in https://docs.python.org/2/library/socket.html#example $ cat read_socket.py import socket import sys HOST = 'localhost' PORT = int(sys.argv[1]) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) s.sendall('Hello, world') data = s.recv(1024) s.close() print 'Received', repr(data) $ python read_socket.py 11110 port 11110 ...connected! port 11110 ...CLOSED! Received 'TEST' $ python read_socket.py 11112 port 11112 ...connected! port 11112 ...CLOSED! Received 'TEST' $ python read_socket.py 11111 port 11111 ...connected! port 11111 ...CLOSED! Received 'TEST' $ fg bash: fg: Programm ist beendet. [1]+ Fertig python bind_ports.py 11110 11113 $ From __peter__ at web.de Wed Jun 11 10:49:35 2014 From: __peter__ at web.de (Peter Otten) Date: Wed, 11 Jun 2014 10:49:35 +0200 Subject: [Tutor] [OT] Long delay until my posts appear References: Message-ID: Alan Gauld wrote: > On 10/06/14 09:43, Peter Otten wrote: >> I'm posting via gmane. Since last month there is a delay (usually a few >> hours I think) until my posts appear and I seem to be getting a >> >> "Your message to Tutor awaits moderator approval, would you like to >> cancel..." > > Something has changed on the list server which means that any time > someone edits their settings it automatically sets up full moderation. > Presumably you changed a setting somewhere... > > Anyway I have now gone in and switched off moderation for your account. > Normal service should have resumed. It has! Thank you. From __peter__ at web.de Wed Jun 11 10:48:45 2014 From: __peter__ at web.de (Peter Otten) Date: Wed, 11 Jun 2014 10:48:45 +0200 Subject: [Tutor] python sockets References: Message-ID: Alan Gauld wrote: > On 11/06/14 00:08, Jon Engle wrote: >> Ok, so when I run the code it immediately terminates and never 'listens' > > This has nothing to do with your immediate problem but... > >> ***Code*** >> >> #!/usr/bin/python # This is server.py file >> from socket import * #import the socket library >> import thread#import the thread library >> >> startingPort=input("\nPlease enter starting port: ") >> startingPort=int(startingPort) > > > You said you were using Python 2.7. > Do not use input() in Python v2 use raw_input() instead. > Especially since you are using int() to convert the data anyway. > > v2 input() evaluates whatever the user types which could > be damaging code (whether deliberate or inadvertent). > v2 input() is so dangerous it was removed from v3 and > raw_input renamed as input. (Which has just created lots > of confusion IMHO!) > >> def setup(PORT): >> ##let's set up some constants >> HOST = '' #we are the host > > BTW I'm still losing nearly all indentation on your posts. > Are you posting in plain text? Nobody else is complaining > so it might just be my reader that's barfing on it... No, it's not just you, I don't see any indentation either. From alan.gauld at btinternet.com Wed Jun 11 13:46:58 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 11 Jun 2014 12:46:58 +0100 Subject: [Tutor] [OT] Long delay until my posts appear In-Reply-To: References: Message-ID: On 11/06/14 09:49, Peter Otten wrote: >> Something has changed on the list server which means that any time >> someone edits their settings it automatically sets up full moderation. >> Normal service should have resumed. > > It has! Thank you. Good, now if only I could find a way to stop the server switching on moderation for anything except new members joining... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From jon.engle at gmail.com Wed Jun 11 14:42:24 2014 From: jon.engle at gmail.com (Jon Engle) Date: Wed, 11 Jun 2014 08:42:24 -0400 Subject: [Tutor] python sockets In-Reply-To: References: Message-ID: Thank you for your help, this definitely gets me going in the right direction! On Wed, Jun 11, 2014 at 4:16 AM, Marc Tompkins wrote: > On Tue, Jun 10, 2014 at 4:08 PM, Jon Engle wrote: > > Ok, so when I run the code it immediately terminates and never 'listens' > to > > the ports in the loop. I have verified by running netstat -an | grep > 65530 > > and the startingPort is not binding. > > The problem is that all threads started by a program terminate when > the program terminates - and you haven't told your program to stick > around when it's done setting up. So it's setting up and then > immediately exiting - and by the time you run netstat a few seconds > later you find nothing. (Also, by leaving HOST = '', you're listening > at address 0.0.0.0, so good luck catching any traffic...) > Try something like this: > > > #!/usr/bin/python # This is server.py file > from socket import * #import the socket library > import thread #import the thread library > > def setup(PORT): > HOST = '127.0.0.1' #we are the host > ADDR = (HOST,PORT) #we need a tuple for the address > BUFSIZE = 4096 #reasonably sized buffer for data > > serv = socket( AF_INET,SOCK_STREAM) > > serv.bind((ADDR)) #the double parens are to create a tuple with > one element > serv.listen(5) #5 is the maximum number of queued connections we'll > allow > print '\nlistening on port %i...' % PORT > conn,addr = serv.accept() #accept the connection > print '\n...port %i connected!' % PORT > conn.send('TEST') > conn.close() > > def main(): > startingPort=int(raw_input("\nPlease enter starting port: ")) > for port in range (startingPort, 65535): > thread.start_new_thread(setup, (port,)) > quitNow = '' > while quitNow not in ('Q', 'q'): > quitNow = raw_input('Enter Q to quit.') > > if __name__ == '__main__': > main() > > > This will stick around until you enter 'Q', and if you run netstat in > another window you'll see that it's LISTENING on all the ports you > asked for. (All of those print statements will show up in a > surprising order!) > > I'm not running the other side of this experiment, so I haven't tested > a successful connection... good luck. > -- Cheers, Jon S. Engle jon.engle at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From awg1 at gmx.com Wed Jun 11 12:43:33 2014 From: awg1 at gmx.com (Adam Gold) Date: Wed, 11 Jun 2014 11:43:33 +0100 Subject: [Tutor] code review In-Reply-To: <539815FC.3030607@barrowhillfarm.org.uk> References: <53972978.5090600@gmx.com> <20140610230406.GY10355@ando> <5397952E.50700@gmx.com> <539815FC.3030607@barrowhillfarm.org.uk> Message-ID: <539832D5.8060106@gmx.com> >>> Thanks for the reply Steven. It's no more than 100 lines at a >>> guess > >> In that case just copy and paste it into a message and send it to >> the group. Anyone with time available can then take a peek. > > One way noobs anywhere can learn is by listening in to other people's > conversations - it's called lurking, I believe. > > So I would say, please do this on the list, and many more people than > Adam may benefit. Others can ignore the thread if they wish. > > Bob Oke doke, here it is below. Just for convenience's sake, I'm going to repeat what the basic steps are. It's a backup script for certain xen virtual machines ("VM") running on my server. Each VM runs on its own logical volume (as opposed to a file-based loop device). From my own (bitter) experience, the absolutely best way to back up a VM running on a logical volume is to clone it to an image file using dd. I'm aware that a separate discussion could be had around this (on a different mailing list) but, unless someone thinks this is a horribly flawed approach, it may be best to assume this approach is 'fine' so as not to distract from the code review!! Here are the steps: 1) create snapshots of the xen logical volumes using the built in snapshot feature of LVM2 (this way I can backup each logical volume without having to shut down the VM) 2) dd and bzip2 (using a pipe) the snapshots to .img.bz2 files for storage on the same server 3) gpg encrypt the same files and upload them to Amazon s3 4) remove the logical volume snapshots (because they accumulate disk space and I'm doing this daily) and the .gpg files 5) deletes files in the s3 directory which are older than X days As I've mentioned, I'm a real noob, so I'm still mastering some basic stuff. The script works fine for my purposes, I'm keen to understand where it could be improved from a python pov. Finally, yes I could have written this in bash but I prefer python! P.S. I think some of the comments have been wrapped onto more than one line by my email client, I hope this doesn't cause too much inconvenience. ==================================== #!/usr/bin/python3 ############################################ ## XEN VIRTUAL MACHINE BACKUP SCRIPT ## ## Copyright (C) 2014 Adam Gold ## ## This program is free software: you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation, either version 3 of the License, or (at ## your option) any later version. ## ## This program is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See ## the GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program. If not see ## ## Version: 0.4 ## 2014-06-10 ############################################ import datetime, time, subprocess, shlex, os, gnupg, glob, shutil # logical volumes exist in two different volume groups, vgxen and vg_data # hence two lists of vms vgxenList = ['vm1', 'vm2', 'vm3', 'vm4', 'vm5', 'vm6' ] vg_dataList = ['vm1', 'vm2'] backupList = [ ] snapNameList = [ ] # create snapshot names like the following: 2014-06-10T01-00-01.vm1.img.bz2 for i in vgxenList: DATE = datetime.datetime.now().strftime("%Y-%m-%d" + "T" + "%H-%M-%S") vgxenName = "/dev/vgxen/" lvName = i origName = vgxenName + lvName snapName= DATE + "." + lvName snapNameList.append(snapName) backupList.append(vgxenName + snapName) subprocess.call(['lvcreate', '-s', '-L1G', origName, '-n', snapName]) for h in vg_dataList: DATE = datetime.datetime.now().strftime("%Y-%m-%d" + "T" + "%H-%M-%S") vg_dataName = "/dev/vg_data/" lvName = h origName = vg_dataName + lvName snapName = DATE + "." + lvName snapNameList.append(snapName) backupList.append(vg_dataName + snapName) subprocess.call(['lvcreate', '-s', '-L1G', origName, '-n', snapName]) # backupPath is list of full paths of each snapshot # the string is extacted from backupList using 'join' backupPath = ' '.join(backupList) for j, k in zip(backupList, snapNameList): backupPath = j backupSnapshot = k # run dd and pipe to bz2 file using subprocess module ddIf = shlex.split("dd if=%s bs=4k conv=noerror,notrunc,sync" % (backupPath)) compress = "pbzip2" filename = "/home/files/temp/%s.img.bz2" % (backupSnapshot) p1 = subprocess.Popen(ddIf, stdout=subprocess.PIPE) with p1.stdout as fin, open(filename, "w") as fout: p2 = subprocess.Popen(compress, stdin=fin, stdout=fout) ret1 = p1.wait() ret2 = p2.wait() # create list of files to be encrypted with full path names # start with list of unencrypted files cryptDir = '/home/files/temp/' unencrypted = [u for u in os.listdir(cryptDir)] # join absolute path to file names to create new list (list comprehension) cryptDir_unencrypted = [ os.path.join(cryptDir, s) for s in unencrypted ] # encrypt files for G in cryptDir_unencrypted: gpg = gnupg.GPG(gnupghome='/root/.gnupg') phrase = "passphrase" # HORRIBLE SECURITY, I KNOW! The script is running as a cronjob so I can't interactively enter the passphrase. Suggestions are welcome. cipher = "AES256" with open(G, 'rb') as f: status = gpg.encrypt_file(f, None, armor=False, passphrase=phrase, symmetric=cipher.upper(), output=G + '.gpg') # move unencypted files out of temp directory for data in glob.glob(cryptDir + '*.bz2'): shutil.move(data,'/home/files/') # delete snapshots for r in snapNameList: removeSnapshots1 = 'lvremove -f ' + vgxenName + r subprocess.call(shlex.split(removeSnapshots1)) removeSnapshots2 = 'lvremove -f ' + vg_dataName + r subprocess.call(shlex.split(removeSnapshots2)) # create list of file names to be uploaded (list comprehension) uploads = [y for y in os.listdir(cryptDir)] # join absolute path to file names to create new list (list comprehension) cryptDir_uploads = [ os.path.join(cryptDir, t) for t in uploads ] # upload to Amazon s3 for d in cryptDir_uploads: s3Upload = 's3cmd put ' + d + ' s3://bucket/dir/' subprocess.call(shlex.split(s3Upload)) subprocess.call(shlex.split('rm ' + d)) # move working path to list of unencrypted vm backup files path = '/home/files/' os.chdir(path) # build list of unencrypted vm backup files (list comprehension) fileNames = [ u for u in os.listdir(path) if os.path.isfile(u) ] # build list of unencrypted vm backup files with .gpg # this will mirror the list of files in s3 (list comprehension) fileNames_gpg = [ p + '.gpg' for p in fileNames ] # NOTE: I tried to collapse the previous two list comprehensions into one # as it would seem to be possible - the contents of fileNames_gpg should be # an os.listdir() of the directory they are in. For some reason I couldn't # do this so I did the above hack by appending the .gpg extension to a different list # calculate age of unencrypted vm backup files and # hence encrypted files on s3 (list comprehension) # NOTE: I have to use the unencrypted files on the server # as the gpg files get deleted after each upload to s3 ageList = [ round((time.time() - os.stat(n).st_mtime)/60/60/24) for n in fileNames ] # delete files older than 'age' variable; age of file in ageList compared # against name in fileNames_gpg age = 7 for x, y in zip(fileNames_gpg, ageList): if y > age: subprocess.call(shlex.split('s3cmd del s3://bucket/dir/' + x)) From lu.nemec at gmail.com Wed Jun 11 21:37:55 2014 From: lu.nemec at gmail.com (=?UTF-8?B?THVrw6HFoSBOxJttZWM=?=) Date: Wed, 11 Jun 2014 21:37:55 +0200 Subject: [Tutor] code review In-Reply-To: <539832D5.8060106@gmx.com> References: <53972978.5090600@gmx.com> <20140610230406.GY10355@ando> <5397952E.50700@gmx.com> <539815FC.3030607@barrowhillfarm.org.uk> <539832D5.8060106@gmx.com> Message-ID: <5398B013.1040804@gmail.com> Ok, not so bad, hoewer there are some parts of the code that could be done a bit cleaner. I'll write them below in the response. >>>> Thanks for the reply Steven. It's no more than 100 lines at a >>>> guess >>> In that case just copy and paste it into a message and send it to >>> the group. Anyone with time available can then take a peek. >> One way noobs anywhere can learn is by listening in to other people's >> conversations - it's called lurking, I believe. >> >> So I would say, please do this on the list, and many more people than >> Adam may benefit. Others can ignore the thread if they wish. >> >> Bob > Oke doke, here it is below. Just for convenience's sake, I'm going to > repeat what the basic steps are. It's a backup script for certain xen > virtual machines ("VM") running on my server. Each VM runs on its own > logical volume (as opposed to a file-based loop device). From my own > (bitter) experience, the absolutely best way to back up a VM running on > a logical volume is to clone it to an image file using dd. I'm aware > that a separate discussion could be had around this (on a different > mailing list) but, unless someone thinks this is a horribly flawed > approach, it may be best to assume this approach is 'fine' so as not to > distract from the code review!! > > Here are the steps: > 1) create snapshots of the xen logical volumes using the built in > snapshot feature of LVM2 (this way I can backup each logical volume > without having to shut down the VM) > 2) dd and bzip2 (using a pipe) the snapshots to .img.bz2 files for > storage on the same server > 3) gpg encrypt the same files and upload them to Amazon s3 > 4) remove the logical volume snapshots (because they accumulate disk > space and I'm doing this daily) and the .gpg files > 5) deletes files in the s3 directory which are older than X days > > As I've mentioned, I'm a real noob, so I'm still mastering some basic > stuff. The script works fine for my purposes, I'm keen to understand > where it could be improved from a python pov. Finally, yes I could have > written this in bash but I prefer python! > > P.S. I think some of the comments have been wrapped onto more than one > line by my email client, I hope this doesn't cause too much inconvenience. > ==================================== > > #!/usr/bin/python3 > > ############################################ > ## XEN VIRTUAL MACHINE BACKUP SCRIPT > ## > ## Copyright (C) 2014 Adam Gold > ## > > > ## This program is free software: you can redistribute it and/or modify > ## it under the terms of the GNU General Public License as published by > ## the Free Software Foundation, either version 3 of the License, or (at > ## your option) any later version. > ## > ## This program is distributed in the hope that it will be useful, but > ## WITHOUT ANY WARRANTY; without even the implied warranty of > ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See > ## the GNU General Public License for more details. > ## > ## You should have received a copy of the GNU General Public License > ## along with this program. If not see > ## > > ## Version: 0.4 > ## 2014-06-10 > > ############################################ > > import datetime, time, subprocess, shlex, os, gnupg, glob, shutil imports should be one module per line and alphabetically- more readable: import datetime import glob import gnupg import os import shlex import shutil import subprocess import time > > # logical volumes exist in two different volume groups, vgxen and vg_data > # hence two lists of vms > vgxenList = ['vm1', 'vm2', 'vm3', 'vm4', 'vm5', 'vm6' ] > vg_dataList = ['vm1', 'vm2'] > backupList = [ ] > snapNameList = [ ] > > > # create snapshot names like the following: 2014-06-10T01-00-01.vm1.img.bz2 > for i in vgxenList: > DATE = datetime.datetime.now().strftime("%Y-%m-%d" + "T" + "%H-%M-%S") > vgxenName = "/dev/vgxen/" > lvName = i > origName = vgxenName + lvName > snapName= DATE + "." + lvName > snapNameList.append(snapName) > backupList.append(vgxenName + snapName) > subprocess.call(['lvcreate', '-s', '-L1G', origName, '-n', snapName]) > > > for h in vg_dataList: > DATE = datetime.datetime.now().strftime("%Y-%m-%d" + "T" + "%H-%M-%S") > vg_dataName = "/dev/vg_data/" > lvName = h > origName = vg_dataName + lvName > snapName = DATE + "." + lvName > snapNameList.append(snapName) > backupList.append(vg_dataName + snapName) > subprocess.call(['lvcreate', '-s', '-L1G', origName, '-n', snapName]) Here in the two loops, you can notice they're almost the same ... NOTE: I thing you're dealing with file paths, you should use os.path and os.path.join instead of string conactenation and you should also check if the file exists first before calling subprocess ... you can figure out how yourself Basic DRY (don't repeat yourself) principle - distill their essence into a function or more: def snapshot_name(item, name): """ :param item: string (one of 'vm1', 'vm2' ..) :param name: string ('/dev/vgxen/') """ isodate = datetime.datetime.now().isoformat() # notice datetime already has this time format orig_name = '{}{}'.format(name, item) # i'd rather use format or % string formatting than + snapshot_name = '{}.{}'.format(isodate, item) backup_path = '{}{}'.format(name, snapshot_name) return (orig_name, snapshot_name, backup_path) for h in vg_dataList: # note: vg_dataList - use either _ or CamelCase, not both! orig_name, snapshot_name, backup_path = snapshot_name(h, '/dev/vg_data/') backup_list.append(backup_path) # only add path if exists ... subprocess.call(....) > > # backupPath is list of full paths of each snapshot > # the string is extacted from backupList using 'join' > backupPath = ' '.join(backupList) > for j, k in zip(backupList, snapNameList): > backupPath = j > backupSnapshot = k > # run dd and pipe to bz2 file using subprocess module > ddIf = shlex.split("dd if=%s bs=4k conv=noerror,notrunc,sync" % > (backupPath)) > compress = "pbzip2" > filename = "/home/files/temp/%s.img.bz2" % (backupSnapshot) > p1 = subprocess.Popen(ddIf, stdout=subprocess.PIPE) > with p1.stdout as fin, open(filename, "w") as fout: > p2 = subprocess.Popen(compress, stdin=fin, stdout=fout) > ret1 = p1.wait() > ret2 = p2.wait() This part with dd ... you can read the file with open(filename, 'rb') and then bzip it with python - it is up to you example: with open(filename, 'rb') as f: bzip_file = bz2.BZ2File(outfile, 'w') # you specify compress level here - see docs bzip_file.write(f.read()) # you can do sequential read of the file and sequential compression - better for snapshots, this might not actually work for large files bzip_file.close() > > > # create list of files to be encrypted with full path names > # start with list of unencrypted files > cryptDir = '/home/files/temp/' > unencrypted = [u for u in os.listdir(cryptDir)] > # join absolute path to file names to create new list (list comprehension) > cryptDir_unencrypted = [ os.path.join(cryptDir, s) for s in unencrypted ] > > > # encrypt files > for G in cryptDir_unencrypted: > gpg = gnupg.GPG(gnupghome='/root/.gnupg') > phrase = "passphrase" # HORRIBLE SECURITY, I KNOW! The script is > running as a cronjob so I can't interactively enter the passphrase. > Suggestions are welcome. > cipher = "AES256" > with open(G, 'rb') as f: > status = gpg.encrypt_file(f, None, armor=False, > passphrase=phrase, symmetric=cipher.upper(), output=G + '.gpg') For the encryption part, you can create a keypair, encrypt the snapshot with public key and decrypt the snapshot with private key on the amazon S3 directly without passwords - pycrypto module > > > # move unencypted files out of temp directory > for data in glob.glob(cryptDir + '*.bz2'): > shutil.move(data,'/home/files/') > > > # delete snapshots > for r in snapNameList: > removeSnapshots1 = 'lvremove -f ' + vgxenName + r > subprocess.call(shlex.split(removeSnapshots1)) > removeSnapshots2 = 'lvremove -f ' + vg_dataName + r > subprocess.call(shlex.split(removeSnapshots2)) Also DRY - make a function that does this and takes parameters. > > > # create list of file names to be uploaded (list comprehension) > uploads = [y for y in os.listdir(cryptDir)] > # join absolute path to file names to create new list (list comprehension) > cryptDir_uploads = [ os.path.join(cryptDir, t) for t in uploads ] > > # upload to Amazon s3 > for d in cryptDir_uploads: > s3Upload = 's3cmd put ' + d + ' s3://bucket/dir/' > subprocess.call(shlex.split(s3Upload)) > subprocess.call(shlex.split('rm ' + d)) > > > # move working path to list of unencrypted vm backup files > path = '/home/files/' > os.chdir(path) > > # build list of unencrypted vm backup files (list comprehension) > fileNames = [ u for u in os.listdir(path) if os.path.isfile(u) ] > > # build list of unencrypted vm backup files with .gpg > # this will mirror the list of files in s3 (list comprehension) > fileNames_gpg = [ p + '.gpg' for p in fileNames ] > > # NOTE: I tried to collapse the previous two list comprehensions into one > # as it would seem to be possible - the contents of fileNames_gpg should be > # an os.listdir() of the directory they are in. For some reason I couldn't > # do this so I did the above hack by appending the .gpg extension to a > different list > > # calculate age of unencrypted vm backup files and > # hence encrypted files on s3 (list comprehension) > # NOTE: I have to use the unencrypted files on the server > # as the gpg files get deleted after each upload to s3 > ageList = [ round((time.time() - os.stat(n).st_mtime)/60/60/24) for n in > fileNames ] > > # delete files older than 'age' variable; age of file in ageList compared > # against name in fileNames_gpg > age = 7 > for x, y in zip(fileNames_gpg, ageList): > if y > age: > subprocess.call(shlex.split('s3cmd del s3://bucket/dir/' + x)) > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor Also a few hints: I'm not so sure it's a good idea to delete the files from this script - make another that deletes old scripts (only when there are more than one for "partition") and call that with cron. Would it not be better to make one loop that prepares all the files? - make snapshots, move them bzip them, encrypt them. And another that just uploads them all? I hope this helps! Lukas From codemonkey at inbox.com Wed Jun 11 22:46:11 2014 From: codemonkey at inbox.com (Deb Wyatt) Date: Wed, 11 Jun 2014 12:46:11 -0800 Subject: [Tutor] A couple newbie questions about Python Message-ID: <38A421DA4B5.0000023Ecodemonkey@inbox.com> Hi. Everywhere I have read, the 'standard practice' for indentation is 4 spaces, but I am running into 2 space indentation in a lot of tutorials and such. Should I keep with the 4 spaces, or does it even matter, as long as it is consistent? I just recently became aware of the inaccuracy of calculations using floats and I am concerned about that. I am using Python 3, just fyi. Thanks for any enlightenment on these questions. Deb in WA, USA ____________________________________________________________ FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop! Check it out at http://www.inbox.com/marineaquarium From alan.gauld at btinternet.com Thu Jun 12 00:48:37 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 11 Jun 2014 23:48:37 +0100 Subject: [Tutor] A couple newbie questions about Python In-Reply-To: <38A421DA4B5.0000023Ecodemonkey@inbox.com> References: <38A421DA4B5.0000023Ecodemonkey@inbox.com> Message-ID: On 11/06/14 21:46, Deb Wyatt wrote: > Hi. Everywhere I have read, the 'standard practice' for indentation is 4 spaces, That's a style recommendation. Python doesn't care. But your readers will. 2 spaces is the absolute minimum, 8 spaces is about the absolute maximum. Outside that it gets hard to read the code. 3, 4 or 5 spaces is pretty good and you are unlikely to get complaints except from style pedants or if you are submitting code for the standard library where they like to stick with the official guidance. > I just recently became aware of the inaccuracy of calculations using floats > and I am concerned about that. The inaccuracies are an inevitable result of the way your computer processes floating point numbers. In what way are you concerned? Its not a Python issue - the exact same issues occur in any computer - or even your pocket calculator. If it concerns you in Excel or in Visual Basic/Java/C++ etc too then the good news is that Python has alternative renderings that can often reduce/eliminate them - but at the cost of speed and complexity. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Thu Jun 12 01:38:49 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 12 Jun 2014 00:38:49 +0100 Subject: [Tutor] code review In-Reply-To: <539832D5.8060106@gmx.com> References: <53972978.5090600@gmx.com> <20140610230406.GY10355@ando> <5397952E.50700@gmx.com> <539815FC.3030607@barrowhillfarm.org.uk> <539832D5.8060106@gmx.com> Message-ID: On 11/06/14 11:43, Adam Gold wrote: > # create snapshot names like the following: 2014-06-10T01-00-01.vm1.img.bz2 > for i in vgxenList: > DATE = datetime.datetime.now().strftime("%Y-%m-%d" + "T" + "%H-%M-%S") Why use addition? You could just insett the literal T... DATE = datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S") Also using all caps for the name suggests a constant by convention. > vgxenName = "/dev/vgxen/" > lvName = i why not just say for lvname in vxgenlist and avoid the extra assignment? > origName = vgxenName + lvName > snapName= DATE + "." + lvName Again you could have done that in the original strftime() call. > snapNameList.append(snapName) > backupList.append(vgxenName + snapName) > subprocess.call(['lvcreate', '-s', '-L1G', origName, '-n', snapName]) > > > for h in vg_dataList: > DATE = datetime.datetime.now().strftime("%Y-%m-%d" + "T" + "%H-%M-%S") see above > vg_dataName = "/dev/vg_data/" > lvName = h again, why not just use the meaningful name in the for loop? > origName = vg_dataName + lvName > snapName = DATE + "." + lvName > snapNameList.append(snapName) > backupList.append(vg_dataName + snapName) > subprocess.call(['lvcreate', '-s', '-L1G', origName, '-n', snapName]) In fact these loops are so similar you should probably make a function with a couple of parameters and call it from both loops. The only things different are the list you loop over and the path. Make those params and you get something like this (untested): def create_volumes(volList, path): for name in volList: dt = datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S."+name) dataName = path origName = dataName + name snapName = DATE + "." + name snapNameList.append(snapName) backupList.append(dataName + snapName) subprocess.call(['lvcreate', '-s', '-L1G', origName, '-n', snapName]) > # backupPath is list of full paths of each snapshot > # the string is extacted from backupList using 'join' > backupPath = ' '.join(backupList) > for j, k in zip(backupList, snapNameList): > backupPath = j > backupSnapshot = k > # run dd and pipe to bz2 file using subprocess module > ddIf = shlex.split("dd if=%s bs=4k conv=noerror,notrunc,sync" % > (backupPath)) > compress = "pbzip2" > filename = "/home/files/temp/%s.img.bz2" % (backupSnapshot) > p1 = subprocess.Popen(ddIf, stdout=subprocess.PIPE) > with p1.stdout as fin, open(filename, "w") as fout: > p2 = subprocess.Popen(compress, stdin=fin, stdout=fout) Take note of the warning in the subprocess documentation about using stdin/stdout directly. You should ideally access them via Popen.communicate(). > ret1 = p1.wait() > ret2 = p2.wait() I may just be missing it but I don't see you do anything with these ret1,ret2 variables? > # create list of files to be encrypted with full path names > # start with list of unencrypted files > cryptDir = '/home/files/temp/' > unencrypted = [u for u in os.listdir(cryptDir)] This is a redundant comprehension. listdir() returns the list for you. Any time you see foo = [item for item in a_list] it probably should just be: foo = a_list Or if a_list is really an iterator it could be foo = list(an_iter) Comprehensions are good when you have a filter condition on the end or if you are returning a function/expression involving item. > # join absolute path to file names to create new list (list comprehension) > cryptDir_unencrypted = [ os.path.join(cryptDir, s) for s in unencrypted ] Like that one for example... > # encrypt files > for G in cryptDir_unencrypted: You seem to like single letter loop variables. Its better to use something more meaningful. Say: for filename in cryptDir_unencrypted: > gpg = gnupg.GPG(gnupghome='/root/.gnupg') > phrase = "passphrase" # HORRIBLE SECURITY, I KNOW! The script is > running as a cronjob so I can't interactively enter the passphrase. > Suggestions are welcome. Use a config file that you can edit when needed. Read the passwords from that. It could even be encrypted... > cipher = "AES256" > with open(G, 'rb') as f: > status = gpg.encrypt_file(f, None, armor=False, > passphrase=phrase, symmetric=cipher.upper(), output=G + '.gpg') > > > # move unencypted files out of temp directory > for data in glob.glob(cryptDir + '*.bz2'): You should really use os.join() instead of string addition... > shutil.move(data,'/home/files/') > > > # delete snapshots > for r in snapNameList: > removeSnapshots1 = 'lvremove -f ' + vgxenName + r > subprocess.call(shlex.split(removeSnapshots1)) > removeSnapshots2 = 'lvremove -f ' + vg_dataName + r > subprocess.call(shlex.split(removeSnapshots2)) > > > # create list of file names to be uploaded (list comprehension) > uploads = [y for y in os.listdir(cryptDir)] see previous comment > # join absolute path to file names to create new list (list comprehension) > cryptDir_uploads = [ os.path.join(cryptDir, t) for t in uploads ] > > # upload to Amazon s3 > for d in cryptDir_uploads: > s3Upload = 's3cmd put ' + d + ' s3://bucket/dir/' > subprocess.call(shlex.split(s3Upload)) > subprocess.call(shlex.split('rm ' + d)) I'm not sure the shlex.split calls add much value. You could just hard code the command lists in this case. > # move working path to list of unencrypted vm backup files > path = '/home/files/' > os.chdir(path) > > # build list of unencrypted vm backup files (list comprehension) > fileNames = [ u for u in os.listdir(path) if os.path.isfile(u) ] > > # build list of unencrypted vm backup files with .gpg > # this will mirror the list of files in s3 (list comprehension) > fileNames_gpg = [ p + '.gpg' for p in fileNames ] > > # NOTE: I tried to collapse the previous two list comprehensions into one > # as it would seem to be possible Why not: fileNames_gpg = [ u+'.gpg' for u in os.listdir(path) if os.path.isfile(u) ] > # NOTE: I have to use the unencrypted files on the server > # as the gpg files get deleted after each upload to s3 > ageList = [ round((time.time() - os.stat(n).st_mtime)/60/60/24) for n in > fileNames ] This seems to give the lie to the statement that you wanted to combine the comprehensions... you are using the intermediate list here... > # delete files older than 'age' variable; age of file in ageList compared > # against name in fileNames_gpg > age = 7 > for x, y in zip(fileNames_gpg, ageList): > if y > age: This is probably neater done using datetime and timedelta objects. > subprocess.call(shlex.split('s3cmd del s3://bucket/dir/' + x)) Again, I'm not sure shlex adds anything here. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From davea at davea.name Thu Jun 12 05:08:58 2014 From: davea at davea.name (Dave Angel) Date: Wed, 11 Jun 2014 22:08:58 -0500 (CDT) Subject: [Tutor] A couple newbie questions about Python References: <38A421DA4B5.0000023Ecodemonkey@inbox.com> Message-ID: Deb Wyatt Wrote in message: > Hi. Everywhere I have read, the 'standard practice' for indentation is 4 spaces, but I am running into 2 space indentation in a lot of tutorials and such. Should I keep with the 4 spaces, or does it even matter, as long as it is consistent? > 4 spaces is an excellent choice in my opinion, and many other people's. We just tell our editor to turn the tab key into 4 column alignment and pretty much forget about it. The python interpreter doesn't care. But other people's opinions will matter as soon as you share your code, or work on a multi person project. Note that when you're looking at other people's code, you may be seeing it differently than they, chief reason being html (thank you for remembering to post here in text). The other reason you may think they're using 2 is proportional spacing. It should be off for code. > I just recently became aware of the inaccuracy of calculations using floats and I am concerned about that. > > I learned programming in 1967 with Fortran, and McCracken spent a chapter warning about that same thing. Probably everything he warned about still applies to Python and modern computers. It is impossible to do serious computing for long without running into these issues. But Python has as many ways of avoiding them as any mainstream language. You can use decimal to avoid some types of problems, and fractions to avoid others. And common sense for others. You will need to understand your tools. BTW, calculators and spreadsheets frequently use decimal rather than binary, and I wrote a decimal floating package for a computer in the mid 70's. It didn't even offer binary. -- DaveA From steve at pearwood.info Thu Jun 12 05:57:06 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 12 Jun 2014 13:57:06 +1000 Subject: [Tutor] A couple newbie questions about Python In-Reply-To: <38A421DA4B5.0000023Ecodemonkey@inbox.com> References: <38A421DA4B5.0000023Ecodemonkey@inbox.com> Message-ID: <20140612035706.GB10355@ando> Hi Deb, My responses below, interleaved with your questions. On Wed, Jun 11, 2014 at 12:46:11PM -0800, Deb Wyatt wrote: > Hi. Everywhere I have read, the 'standard practice' for indentation > is 4 spaces, but I am running into 2 space indentation in a lot of > tutorials and such. Should I keep with the 4 spaces, or does it even > matter, as long as it is consistent? Four spaces is common, and recommended by "PEP 8", which describes the coding styles for the Python standard library: https://www.python.org/dev/peps/pep-0008 But it's not compulsory (except for contributions to the standard library). There are good reasons for sticking to four spaces, but eight spaces or a single tab are also common. In my opinion, two spaces is too little, and one space is right out. But as far as the Python interpreter is concerned, it doesn't care, so long as you're consistent. (However, other people reading your code may care.) > I just recently became aware of the inaccuracy of calculations using > floats and I am concerned about that. Floating point mathematics is tricky. What you're seeing is not a bug in Python, but a general problem with floating point calculations in general. Regardless of whether you use Python, or C, or Java, or some other programming language, you have to face the same issues that calculations with floats are not always accurate compared to what you expect from pure mathematics. (Think about your calculator: how often do you get a number like 3.0000001 instead of 3, or 0.4999999 instead of 0.5?) You can read up on some of the issues here: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html although it's quite technical. Some less technical discussions can be found here: http://randomascii.wordpress.com/2012/04/05/floating-point-complexities/ http://support.microsoft.com/kb/42980 but however you look at it, it's complicated, and inherent to the problem, not the fault of Python. -- Steven From akleider at sonic.net Thu Jun 12 07:27:55 2014 From: akleider at sonic.net (Alex Kleider) Date: Wed, 11 Jun 2014 22:27:55 -0700 Subject: [Tutor] A couple newbie questions about Python In-Reply-To: References: <38A421DA4B5.0000023Ecodemonkey@inbox.com> Message-ID: <4a49ae4adfc7f79b23f613e445b9d23a@sonic.net> On 2014-06-11 20:08, Dave Angel wrote: > I learned programming in 1967 with Fortran, and McCracken spent a > chapter warning about that same thing. Probably everything he > warned about still applies to Python and modern computers. A google search for "McCracken" did not yield anyone that seems to fit the person you reference. Can you clarify, please? From lu.nemec at gmail.com Thu Jun 12 07:49:52 2014 From: lu.nemec at gmail.com (=?UTF-8?B?THVrw6HFoSBOxJttZWM=?=) Date: Thu, 12 Jun 2014 07:49:52 +0200 Subject: [Tutor] A couple newbie questions about Python In-Reply-To: <38A421DA4B5.0000023Ecodemonkey@inbox.com> References: <38A421DA4B5.0000023Ecodemonkey@inbox.com> Message-ID: <53993F80.7090006@gmail.com> Hi, responses below... Dne 11. 6. 2014 22:46, Deb Wyatt napsal(a): > Hi. Everywhere I have read, the 'standard practice' for indentation is 4 spaces, but I am running into 2 space indentation in a lot of tutorials and such. Should I keep with the 4 spaces, or does it even matter, as long as it is consistent? I recommend to use ammount of spaces that is consistent with already existing project. If it is a new project, use whatever you like - 4 spaces is recommended (looks good :) > > I just recently became aware of the inaccuracy of calculations using floats and I am concerned about that. If there are numerical operations where precision is necessary (accounting or banking) use decimal type from decimal import Decimal a = Decimal('0.1') b = Decimal('0.9') print a + b Note these numbers are passed to Decimal as a string, that is for a reason - if you pass float to Decimal, it will already loose precision and the whole thing doesn't make sense - and aslo will throw an exception. > > I am using Python 3, just fyi. > > Thanks for any enlightenment on these questions. > > Deb in WA, USA > > ____________________________________________________________ > FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop! > Check it out at http://www.inbox.com/marineaquarium > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor Lukas From admin at c12.in Thu Jun 12 04:04:04 2014 From: admin at c12.in (Mirage Web Studio) Date: Thu, 12 Jun 2014 07:34:04 +0530 Subject: [Tutor] Fwd: Problem reading large files in binary mode In-Reply-To: <53989434.4000802@c12.in> References: <53989434.4000802@c12.in> Message-ID: <53990A94.80705@c12.in> Hello I am new to python programming. while trying it out i find that in my code file io.read is not reading large files particularly over 1 gb. my code is posted below. i am working on python 3.3 on windows with ntfs partition and intel corei3 ram 3gb. the execution always stops saying error with py.exe but idle editor and idle window remains open. thank you George ##function module generates md5 for given file data import hashlib import io import os def filemd5(file="g:\\filelargerthan1gb.iso"): #print ("reached here") #print ("File for processing %s",file) try: filelen=os.path.getsize(file) except PermissionError: print("Don't have permission for ",file,".\tTry running as administrator") return '' if filelen>1073741824: print ("file len greater than 1gb.\nNot hashing") return '' try: f=open(file,'rb', buffering=0) #i have tried without buffering also except PermissionError: print("Don't have permission for ",file,".\tTry running as administrator") return '' try: #print ("Readding") fcontents=f.read() #print ("Read successfully") hashvalue=hashlib.md5(fcontents).hexdigest() #print ("md5 successfully") except: hashvalue='' print ("Error during reading md5") f.close() #print (hashvalue) return hashvalue filemd5() --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From codemonkey at inbox.com Thu Jun 12 04:47:49 2014 From: codemonkey at inbox.com (Deb Wyatt) Date: Wed, 11 Jun 2014 18:47:49 -0800 Subject: [Tutor] A couple newbie questions about Python In-Reply-To: References: <38a421da4b5.0000023ecodemonkey@inbox.com> Message-ID: <3BCC737EE6A.000004F2codemonkey@inbox.com> Thanks very much Alan. As for the my concern about inaccuracy in float math, I suppose I am concerned because I was not aware of the issue before, and I wonder how you deal with such things when you need accuracy for financial things. Deb in WA, USA > -----Original Message----- > From: alan.gauld at btinternet.com > Sent: Wed, 11 Jun 2014 23:48:37 +0100 > To: tutor at python.org > Subject: Re: [Tutor] A couple newbie questions about Python > > On 11/06/14 21:46, Deb Wyatt wrote: >> Hi. Everywhere I have read, the 'standard practice' for indentation is >> 4 spaces, > > That's a style recommendation. Python doesn't care. > But your readers will. 2 spaces is the absolute minimum, > 8 spaces is about the absolute maximum. Outside that it > gets hard to read the code. > > 3, 4 or 5 spaces is pretty good and you are unlikely > to get complaints except from style pedants or if you > are submitting code for the standard library where they > like to stick with the official guidance. > > >> I just recently became aware of the inaccuracy of calculations using >> floats > > and I am concerned about that. > > The inaccuracies are an inevitable result of the way your computer > processes floating point numbers. > > In what way are you concerned? Its not a Python issue - the exact same > issues occur in any computer - or even your pocket calculator. > If it concerns you in Excel or in Visual Basic/Java/C++ etc too then the > good news is that Python has alternative renderings that can often > reduce/eliminate them - but at the cost of speed and complexity. > > > HTH > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ____________________________________________________________ Protect your computer files with professional cloud backup. Get PCRx Backup and upload unlimited files automatically. Learn more at http://backup.pcrx.com/mail From davidmarino838 at gmail.com Thu Jun 12 02:48:25 2014 From: davidmarino838 at gmail.com (Marino David) Date: Thu, 12 Jun 2014 08:48:25 +0800 Subject: [Tutor] How can I let the Python Console display more decimal precision? Message-ID: Hi All: I am a newbie at the Python. I type "26/12" in Python Console and get result of "2". It is obvious that the corresponding result should be 2.3333...... I don't know why the Console only returns the integer part of true result. Anyone can help me out? Thanks David -------------- next part -------------- An HTML attachment was scrubbed... URL: From mariopy at gmx.com Thu Jun 12 00:42:01 2014 From: mariopy at gmx.com (Mario Py) Date: Wed, 11 Jun 2014 16:42:01 -0600 Subject: [Tutor] MIT - Free Python course started today Message-ID: <5398DB39.4010406@gmx.com> Sort of off topic, (don't kill me) but then again very useful for beginners! MIT started free Python course today: https://www.edx.org/course/mitx/mitx-6-00-1x-introduction-computer-1841 Unfortunately they are using Python 2.7 :-( You can even get a certificate if you finish with high enough score (will cost you something for certificate) Anyway, even if partially off topic I think it may be beneficiary for some beginners here. I'm already going through the lessons :-) From user0873 at gmail.com Thu Jun 12 06:51:38 2014 From: user0873 at gmail.com (jason sam) Date: Thu, 12 Jun 2014 09:51:38 +0500 Subject: [Tutor] AttributeError: 'module' object has no attribute 'start' Message-ID: Hi All, I am new to wxPython.I have made a simple GUI that contains a button and by pressing that button i am calling another .py file(top_block.py)...But i am getting error using the command: top_block.start() The error is: AttributeError: 'module' object has no attribute 'start' The code is as attached.I am using python 2.7.3.Any improvements in the code are also welcome.The same error is received when i use'top_block.run()'.Is there any alternative to this commands? Regards, Ali -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: wxgui.py Type: text/x-python Size: 827 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: top_block.py Type: text/x-python Size: 690 bytes Desc: not available URL: From alan.gauld at btinternet.com Thu Jun 12 09:55:00 2014 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Thu, 12 Jun 2014 08:55:00 +0100 (BST) Subject: [Tutor] A couple newbie questions about Python In-Reply-To: <3BCC737EE6A.000004F2codemonkey@inbox.com> References: <38a421da4b5.0000023ecodemonkey@inbox.com> <3BCC737EE6A.000004F2codemonkey@inbox.com> Message-ID: <1402559700.62574.YahooMailNeo@web186001.mail.ir2.yahoo.com> I wonder how you deal with such things when you need accuracy for financial things. Financial applications traditionally work in the lowest unit of currency and use integers. For display purposes they convert the pennies into dollars/cents or pounds/pence or whatever. ? Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos -------------- next part -------------- An HTML attachment was scrubbed... URL: From lu.nemec at gmail.com Thu Jun 12 09:58:02 2014 From: lu.nemec at gmail.com (Lukas Nemec) Date: Thu, 12 Jun 2014 09:58:02 +0200 Subject: [Tutor] How can I let the Python Console display more decimal precision? In-Reply-To: References: Message-ID: <53995D8A.8010503@gmail.com> Hi, from the question you're using python 2.x you can do either: 26.0/12 (float divide by int - it retypes both to floats and gets you 2.3333) or at the beginning do: from __future__ import division That will activate python3 way of dividing - which gives you 2.3333 Lukas. On 06/12/2014 02:48 AM, Marino David wrote: > Hi All: > > I am a newbie at the Python. > > I type "26/12" in Python Console and get result of "2". > > It is obvious that the corresponding result should be 2.3333...... I > don't know why the Console only returns the integer part of true > result. Anyone can help me out? > > Thanks > > David > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Thu Jun 12 10:00:58 2014 From: __peter__ at web.de (Peter Otten) Date: Thu, 12 Jun 2014 10:00:58 +0200 Subject: [Tutor] Fwd: Problem reading large files in binary mode References: <53989434.4000802@c12.in> <53990A94.80705@c12.in> Message-ID: Mirage Web Studio wrote: > I am new to python programming. while trying it out i find that in my > code file io.read is not reading large files particularly over 1 gb. my > code is posted below. i am working on python 3.3 on windows with ntfs > partition and intel corei3 ram 3gb. the execution always stops saying > error with py.exe but idle editor and idle window remains open. > try: > #print ("Readding") > fcontents=f.read() In the line above you are reading the whole file into memory. > #print ("Read successfully") > hashvalue=hashlib.md5(fcontents).hexdigest() > #print ("md5 successfully") Try reading the file in chunks instead: CHUNKSIZE = 2**20 hash = hashlib.md5() while True: chunk = f.read(CHUNKSIZE) if not chunk: break hash.update(chunk) hashvalue = hash.hexdigest() From breamoreboy at yahoo.co.uk Thu Jun 12 10:18:16 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 12 Jun 2014 09:18:16 +0100 Subject: [Tutor] A couple newbie questions about Python In-Reply-To: <3BCC737EE6A.000004F2codemonkey@inbox.com> References: <38a421da4b5.0000023ecodemonkey@inbox.com> <3BCC737EE6A.000004F2codemonkey@inbox.com> Message-ID: Please don't top post, it makes following a long thread extremely difficult, thanks. On 12/06/2014 03:47, Deb Wyatt wrote: > > Thanks very much Alan. As for the my concern about inaccuracy in float math, I suppose I am concerned because I was not aware of the issue before, and I wonder how you deal with such things when you need accuracy for financial things. > > Deb in WA, USA > > Besides Alan's previous statement about using integers you can also use things like Python's decimal module see https://docs.python.org/3/library/decimal.html#module-decimal -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From vladimir.samoded at onat.edu.ua Thu Jun 12 11:06:02 2014 From: vladimir.samoded at onat.edu.ua (Volodymyr Samodid) Date: Thu, 12 Jun 2014 12:06:02 +0300 Subject: [Tutor] MIT - Free Python course started today In-Reply-To: <5398DB39.4010406@gmx.com> References: <5398DB39.4010406@gmx.com> Message-ID: <53996D7A.9060301@onat.edu.ua> Thanks a lot. On 06/12/2014 01:42 AM, Mario Py wrote: > Sort of off topic, (don't kill me) but then again very useful for > beginners! > > MIT started free Python course today: > > https://www.edx.org/course/mitx/mitx-6-00-1x-introduction-computer-1841 > > Unfortunately they are using Python 2.7 :-( > You can even get a certificate if you finish with high enough score > (will cost you something for certificate) > > Anyway, even if partially off topic I think it may be beneficiary for > some beginners here. I'm already going through the lessons :-) > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- ? ?????????, ??????? ???????? SVAJ-RIPE|SVA88-UANIC From dotancohen at gmail.com Thu Jun 12 11:54:22 2014 From: dotancohen at gmail.com (Dotan Cohen) Date: Thu, 12 Jun 2014 12:54:22 +0300 Subject: [Tutor] MIT - Free Python course started today In-Reply-To: <5398DB39.4010406@gmx.com> References: <5398DB39.4010406@gmx.com> Message-ID: On Thu, Jun 12, 2014 at 1:42 AM, Mario Py wrote: > Sort of off topic, (don't kill me) but then again very useful for beginners! > > MIT started free Python course today: > > https://www.edx.org/course/mitx/mitx-6-00-1x-introduction-computer-1841 > > Unfortunately they are using Python 2.7 :-( > You can even get a certificate if you finish with high enough score (will > cost you something for certificate) > > Anyway, even if partially off topic I think it may be beneficiary for some > beginners here. I'm already going through the lessons :-) Thank you, I'm signing up! The same organization is doing an introduction to Linux course that starts on August 1st. I'm signed up for that one as well: https://www.edx.org/course/linuxfoundationx/linuxfoundationx-lfs101x-introduction-1621 -- Dotan Cohen http://gibberish.co.il http://what-is-what.com From eryksun at gmail.com Thu Jun 12 12:18:09 2014 From: eryksun at gmail.com (eryksun) Date: Thu, 12 Jun 2014 06:18:09 -0400 Subject: [Tutor] A couple newbie questions about Python In-Reply-To: <4a49ae4adfc7f79b23f613e445b9d23a@sonic.net> References: <38A421DA4B5.0000023Ecodemonkey@inbox.com> <4a49ae4adfc7f79b23f613e445b9d23a@sonic.net> Message-ID: On Thu, Jun 12, 2014 at 1:27 AM, Alex Kleider wrote: > On 2014-06-11 20:08, Dave Angel wrote: > >> I learned programming in 1967 with Fortran, and McCracken spent a >> chapter warning about that same thing. Probably everything he >> warned about still applies to Python and modern computers. > > A google search for "McCracken" did not yield anyone that seems to fit the > person you reference. Can you clarify, please? 1961 A Guide to Fortran Programming openlibrary.org/works/OL4628575W 1964 Numerical Methods and Fortran Programming openlibrary.org/books/OL5914708M 1965 A Guide to Fortran IV Programming openlibrary.org/books/OL5953402M 1967 Fortran with Engineering Applications openlibrary.org/books/OL5539818M snippets: books.google.com/books?id=qQY_AAAAIAAJ&dq=editions:STANFORD36105002062847 1974 A Simplified Guide to Fortran Programming openlibrary.org/works/OL4628579W 1972 Numerical Methods with Fortran IV Case Studies openlibrary.org/books/OL4559608M 1984 Computing for Engineers and Scientists with Fortran 77 openlibrary.org/books/OL3180840M From wprins at gmail.com Thu Jun 12 12:41:58 2014 From: wprins at gmail.com (Walter Prins) Date: Thu, 12 Jun 2014 11:41:58 +0100 Subject: [Tutor] AttributeError: 'module' object has no attribute 'start' In-Reply-To: References: Message-ID: Hi, On 12 June 2014 05:51, jason sam wrote: > > Hi All, > I am new to wxPython.I have made a simple GUI that contains a button and by pressing that button i am calling another .py file(top_block.py)...But i am getting error using the command: > > top_block.start() > > The error is: > > AttributeError: 'module' object has no attribute 'start' The message states that there's no attribute in the module called 'start', which is correct -- top_block.py doesn't contain anything named 'start'. (What made you think that modules have a magic method start() that causes them to run?) You should probably learn more about Python itself, name binding, module objects etc. > > The code is as attached.I am using python 2.7.3.Any improvements in the code are also welcome.The same error is received when i use'top_block.run()'.Is there any alternative to this commands? I'm not a wxPython expert but you should probably be creating an instance of the the frame class defined in top_block and then call show() that, from your wxgui.py, when your button click handler is called. I would also rename the classes to represent what they are. (MyFrame is not exactly useful and is doubly ambiguous as you have 2 defined in different modules.) Suggestion: Try to fix your program with both the main program and the top bar in one module. And then only when this works try moving it back out to another module again, if required. HTH Walter From steve at pearwood.info Thu Jun 12 14:11:37 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 12 Jun 2014 22:11:37 +1000 Subject: [Tutor] AttributeError: 'module' object has no attribute 'start' In-Reply-To: References: Message-ID: <20140612121137.GC10355@ando> On Thu, Jun 12, 2014 at 11:41:58AM +0100, Walter Prins wrote: > (What made you think that modules have a magic method > start() that causes them to run?) You should probably learn more > about Python itself, name binding, module objects etc. That's what this list is for! -- Steven From steve at pearwood.info Thu Jun 12 14:31:05 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 12 Jun 2014 22:31:05 +1000 Subject: [Tutor] How can I let the Python Console display more decimal precision? In-Reply-To: References: Message-ID: <20140612123105.GD10355@ando> On Thu, Jun 12, 2014 at 08:48:25AM +0800, Marino David wrote: > Hi All: > > I am a newbie at the Python. > > I type "26/12" in Python Console and get result of "2". > > It is obvious that the corresponding result should be 2.3333...... I don't > know why the Console only returns the integer part of true result. Anyone > can help me out? Try this instead: 26.0/12 and it will print a fractional number instead of an int: py> 26.0/12 2.1666666666666665 What's going on? Back in the early mists of time, when Python first came out, Python's creator Guido van Rossum decided that the / division operator should behave like in the C programming language. In C, division of two integers performs *integer division*, and drops the remainder, while division of one or more floating point number keeps the remainder as a fraction: 1/2 => 0 1/2.0 => 0.5 That was one of those decisions that seemed like a good idea at the time, but turned out to be a mistake. But for backwards compatibility, Python had to keep it until recently. In Python version 3, / now does calculator division, like you expect. But in Python 2, you have to either convert one or both numbers to a float, or you can put this at the top of your program: from __future__ import division Note that there are TWO underscores at the beginning and end of "future". If you want integer division, where the remainder is ignored, you can use the // operator: py> 26.0//12 2.0 -- Steven From alan.gauld at btinternet.com Thu Jun 12 15:30:55 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 12 Jun 2014 14:30:55 +0100 Subject: [Tutor] A couple newbie questions about Python In-Reply-To: References: <38a421da4b5.0000023ecodemonkey@inbox.com> <3BCC737EE6A.000004F2codemonkey@inbox.com> Message-ID: On 12/06/14 09:18, Mark Lawrence wrote: > Besides Alan's previous statement about using integers you can also use > things like Python's decimal module see > https://docs.python.org/3/library/decimal.html#module-decimal Although Decimal doesn't solve the issue of rounding errors for financial calculations (neither do pennies) nor the issues of repeating results like 1/7.... It just moves them into the more familiar territory of decimal numbers rather than binary. So at least what you write is represented accurately. As Steven said in his reply, floating point on computers is hard. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Thu Jun 12 15:39:43 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 12 Jun 2014 14:39:43 +0100 Subject: [Tutor] AttributeError: 'module' object has no attribute 'start' In-Reply-To: References: Message-ID: On 12/06/14 05:51, jason sam wrote: > Hi All, > I am new to wxPython.I have made a simple GUI that contains a button and > by pressing that button i am calling another .py > file(top_block.py)... No you are not. You are importing it and then trying to access something called start which doesn't exist. importing is not the same as running a script. For a start the _name__ attribute will not be __main__ so your if clause at the bottom won't be executed. But the class definition code will have been run so you can access and create those classes from your wxGUI module. However, it is very strange practice to try to create a second GUI program from a first (not totally unheard of but uncommon). More usually you would define a dialog and open that from your Execute method. > top_block.start() > > The error is: > > AttributeError: 'module' object has no attribute 'start' Please always post the full error message not just a summary. It has all sorts of useful extra details in there. In this case it doesn't matter because as the error says your module has no start attribute - because you didn't create one. > code are also welcome.The same error is received when i > use'top_block.run()'.Is there any alternative to this commands? Yep, you didn't define a run() function either. modules can only execute what you define within them (and one or two special case attributes defined by Python; all starting with __ Thee are several other oddities in your code but this is probably enough for now. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From breamoreboy at yahoo.co.uk Thu Jun 12 18:47:23 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 12 Jun 2014 17:47:23 +0100 Subject: [Tutor] AttributeError: 'module' object has no attribute 'start' In-Reply-To: References: Message-ID: On 12/06/2014 05:51, jason sam wrote: > Hi All, > I am new to wxPython.I have made a simple GUI that contains a button and > by pressing that button i am calling another .py > file(top_block.py)...But i am getting error using the command: > > top_block.start() > > The error is: > > AttributeError: 'module' object has no attribute 'start' > > The code is as attached.I am using python 2.7.3.Any improvements in the > code are also welcome.The same error is received when i > use'top_block.run()'.Is there any alternative to this commands? > > Regards, > Ali > I suspect that you'll get better answers on the wxpython list, especially consdiering that you asked there all of two minutes before asking here. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From steve at pearwood.info Thu Jun 12 19:26:21 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 13 Jun 2014 03:26:21 +1000 Subject: [Tutor] AttributeError: 'module' object has no attribute 'start' In-Reply-To: References: Message-ID: <20140612172621.GE10355@ando> On Thu, Jun 12, 2014 at 05:47:23PM +0100, Mark Lawrence wrote: > I suspect that you'll get better answers on the wxpython list, > especially consdiering that you asked there all of two minutes before > asking here. This is not aimed specifically at Mark. Can we please stop chastising people for asking questions in different forums? There's nothing wrong with asking in a few different places simultaneously and getting answers from people with different perspectives. Nor should people have to ask those questions sequentially ("ask on tutor list, wait a week, then ask on the wxpython list, then wait another week, then ask somewhere else") when they can do so in parallel. In other words, if you ask a question in one place, you don't necessarily have to wait before asking it again elsewhere. If you (generic you, not just Mark) happen to be members of multiple mailing lists or other forums, and you see the same question in both places, feel free to link to good answers in the other forum. Otherwise, just remember that what is a duplicate question to you, is not duplicate to those who aren't members of both forums. Thank you. -- Steven From wprins at gmail.com Thu Jun 12 22:25:12 2014 From: wprins at gmail.com (Walter Prins) Date: Thu, 12 Jun 2014 21:25:12 +0100 Subject: [Tutor] AttributeError: 'module' object has no attribute 'start' In-Reply-To: <20140612121137.GC10355@ando> References: <20140612121137.GC10355@ando> Message-ID: On 12 June 2014 13:11, Steven D'Aprano wrote: > On Thu, Jun 12, 2014 at 11:41:58AM +0100, Walter Prins wrote: > >> (What made you think that modules have a magic method >> start() that causes them to run?) You should probably learn more >> about Python itself, name binding, module objects etc. > > That's what this list is for! Of course. The way the OP asked his question, stating he's a beginner to wxPython, but omitting to state that he's also Python beginner, leaves one with the impression that he might be (or think that he is) relatively/more skilled at at Python. The nature of the problem he's having however indicates a basic lack of understanding of Python itself, which should be easily remedied by a bit of self study, which, given that he's teaching himself wxPython is probably realistic, and which is why I suggested it. Of course, if he does run into further questions then he's most welcome to ask, apologies if I might've given a different impression. Walter From cs at zip.com.au Fri Jun 13 04:28:07 2014 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 13 Jun 2014 12:28:07 +1000 Subject: [Tutor] code review In-Reply-To: References: Message-ID: <20140613022807.GA36332@cskk.homeip.net> On 12Jun2014 00:38, Alan Gauld wrote: >On 11/06/14 11:43, Adam Gold wrote: >># create snapshot names like the following: 2014-06-10T01-00-01.vm1.img.bz2 >>for i in vgxenList: >> DATE = datetime.datetime.now().strftime("%Y-%m-%d" + "T" + "%H-%M-%S") > >Why use addition? You could just insett the literal T... > >DATE = datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S") > >Also using all caps for the name suggests a constant by convention. > >> vgxenName = "/dev/vgxen/" >> lvName = i Just further to these two snippets, "/dev/vgxen/" is just the kind of thing you would treat as a "constant" (Python doesn't really have constants, just the convention that names in all UPPERCASE represent stable ad hoc values which won't change, and would be "constants" in some other languages.) So at the top of your script (after imports) you might have: VXGEN_DIR = "/dev/vgxen" and use it later. Note, no trailing slash in that dir name. In view of this, later where you append to backupList you might go: snap_path = os.path.join(VXGEN_DIR, snapName) backupList.append(snap_name) os.path.join is the usual way to assemble pathnames form path components; it is in principle platform independent. BTW, someone has proably mentioned it, but in case not: names like this_that are the convention for normal variables in Python as opposed to thisThat camelcase names. The main use of camelcase in Python names is class names "This That". It doesn't affect your code in any functional way, but others will find you code slightly more readable. These common conventions are outlined in PEP 8: http://legacy.python.org/dev/peps/pep-0008/ Worth a read. It is only strict for stdlib module source code, but adherence is common in most code. Cheers, Cameron Simpson A good catchword can obscure analysis for fifty years. From cs at zip.com.au Fri Jun 13 08:21:35 2014 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 13 Jun 2014 16:21:35 +1000 Subject: [Tutor] code review In-Reply-To: <20140613022807.GA36332@cskk.homeip.net> References: <20140613022807.GA36332@cskk.homeip.net> Message-ID: <20140613062135.GA93409@cskk.homeip.net> On 13Jun2014 12:28, Cameron Simpson wrote: > snap_path = os.path.join(VXGEN_DIR, snapName) > backupList.append(snap_name) Sorry, "snap_path" in both lines:-( Cheers, Cameron Simpson From admin at c12.in Thu Jun 12 16:15:32 2014 From: admin at c12.in (Mirage Web Studio) Date: Thu, 12 Jun 2014 19:45:32 +0530 Subject: [Tutor] Problem reading large files in binary mode In-Reply-To: <53990A94.80705@c12.in> References: <53990A94.80705@c12.in> Message-ID: <5399B604.8030407@c12.in> Try reading the file in chunks instead: CHUNKSIZE = 2**20 hash = hashlib.md5() while True: chunk = f.read(CHUNKSIZE) if not chunk: break hash.update(chunk) hashvalue = hash.hexdigest() Thank you peter for the above valubale reply. but shouldn't read() by itself work because i have enough memory to load it or should it be a bug. thank you george --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From awg1 at gmx.com Thu Jun 12 10:14:50 2014 From: awg1 at gmx.com (Adam Gold) Date: Thu, 12 Jun 2014 09:14:50 +0100 Subject: [Tutor] code review In-Reply-To: References: <53972978.5090600@gmx.com> <20140610230406.GY10355@ando> <5397952E.50700@gmx.com> <539815FC.3030607@barrowhillfarm.org.uk> <539832D5.8060106@gmx.com> Message-ID: <5399617A.2080607@gmx.com> On 12/06/14 00:38, Alan Gauld wrote: > > HTH Thanks Alan and Luk?? for your very helpful comments. I will attempt to revise the script in light of them and will revert if I hit any brick walls :) From codemonkey at inbox.com Thu Jun 12 20:59:19 2014 From: codemonkey at inbox.com (Deb Wyatt) Date: Thu, 12 Jun 2014 10:59:19 -0800 Subject: [Tutor] A couple newbie questions about Python In-Reply-To: References: <38a421da4b5.0000023ecodemonkey@inbox.com> <3bcc737ee6a.000004f2codemonkey@inbox.com> Message-ID: <4447EE93AD2.00000435codemonkey@inbox.com> > -----Original Message----- > From: breamoreboy at yahoo.co.uk > Sent: Thu, 12 Jun 2014 09:18:16 +0100 > To: tutor at python.org > Subject: Re: [Tutor] A couple newbie questions about Python > > Please don't top post, it makes following a long thread extremely > difficult, thanks. > sorry. The other python list wants you to, so I just assumed... Deb in WA, USA She says as she almost top posts again :P... ____________________________________________________________ FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop! Check it out at http://www.inbox.com/marineaquarium From codemonkey at inbox.com Thu Jun 12 18:32:43 2014 From: codemonkey at inbox.com (Deb Wyatt) Date: Thu, 12 Jun 2014 08:32:43 -0800 Subject: [Tutor] MIT - Free Python course started today In-Reply-To: <5398DB39.4010406@gmx.com> Message-ID: <430038F5323.000002CCcodemonkey@inbox.com> oh cool. I actually started taking this here: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/ But to have access to a forum and other people taking it at the same time will be wonderful. thank you for posting this. Signing up right now :). Deb in WA, USA > -----Original Message----- > From: mariopy at gmx.com > Sent: Wed, 11 Jun 2014 16:42:01 -0600 > To: tutor at python.org > Subject: [Tutor] MIT - Free Python course started today > > Sort of off topic, (don't kill me) but then again very useful for > beginners! > > MIT started free Python course today: > > https://www.edx.org/course/mitx/mitx-6-00-1x-introduction-computer-1841 > > Unfortunately they are using Python 2.7 :-( > You can even get a certificate if you finish with high enough score > (will cost you something for certificate) > > Anyway, even if partially off topic I think it may be beneficiary for > some beginners here. I'm already going through the lessons :-) > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ____________________________________________________________ FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop! Check it out at http://www.inbox.com/marineaquarium From diliupg at gmail.com Fri Jun 13 09:21:25 2014 From: diliupg at gmail.com (diliup gabadamudalige) Date: Fri, 13 Jun 2014 12:51:25 +0530 Subject: [Tutor] global variables/constants versus volatile variables/constants Message-ID: Hi All! Hope everyone is well. In my code there are many dictionaries and lists which are used in various functions. Is it better/pythonic/efficient to have these inside the function itself or declared at the beginning of the program in which case they will be global? They are all read only. I understand that global constants and variable have memory allocated to them but when declared inside a function are created on the fly, used and discarded. Please enlighten me further on this and correct me if i'm wrong. Thank you for your time and may you be well. -- Diliup Gabadamudalige http://www.diliupg.com http://soft.diliupg.com/ ********************************************************************************************** This e-mail is confidential. It may also be legally privileged. If you are not the intended recipient or have received it in error, please delete it and all copies from your system and notify the sender immediately by return e-mail. Any unauthorized reading, reproducing, printing or further dissemination of this e-mail or its contents is strictly prohibited and may be unlawful. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions. ********************************************************************************************** -------------- next part -------------- An HTML attachment was scrubbed... URL: From user0873 at gmail.com Thu Jun 12 18:42:12 2014 From: user0873 at gmail.com (jason sam) Date: Thu, 12 Jun 2014 21:42:12 +0500 Subject: [Tutor] AttributeError: 'module' object has no attribute 'start' In-Reply-To: References: Message-ID: Thnx for all the suggestions...Now i have made amendments to my program and its working now...but there is a new problem now....I am now calling a python script generated by GNU Radio Companion(for those who know about GRC will understand my question better)...when i press the execute button i get the following error..Both files are as attached.Is there a different way to call a script generated by GRC?? Form: -> Error translating value: "<__main__.MyFrame; proxy of >" bad operand type for abs(): 'MyFrame' Enter a float with optional scale suffix. E.g., 100.1M Form: -> Error translating value: "<__main__.MyFrame; proxy of >" unsupported operand type(s) for -: 'MyFrame' and 'float' Value should be within slider range Using Volk machine: avx_64_mmx_orc Traceback (most recent call last): File "wxgui.py", line 26, in Execute self.aNewFrame = uhd_fft.uhd_fft(self) File "/home/ali/Desktop/WXGUI/uhd_fft.py", line 155, in __init__ size=((-1, 400)), File "/usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/fftsink_gl.py", line 126, in __init__ persist_alpha=persist_alpha, File "/usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/fft_window.py", line 304, in __init__ self.update_grid() File "/usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/fft_window.py", line 401, in update_grid baseband_freq - sample_rate/2.0, TypeError: unsupported operand type(s) for -: 'MyFrame' and 'float' Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/forms/forms.py", line 102, in widget.Bind(EVT_DATA, lambda x: self._update(x.data)) File "/usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/forms/forms.py", line 248, in _update def _update(self, value): self._text_box.SetValue(value); self._update_color() File "/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode/wx/_controls.py", line 1754, in SetValue return _controls_.TextCtrl_SetValue(*args, **kwargs) TypeError: String or Unicode type required Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/forms/forms.py", line 102, in widget.Bind(EVT_DATA, lambda x: self._update(x.data)) File "/usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/forms/forms.py", line 181, in _update def _update(self, value): self._slider.SetValue(int(round(value))) TypeError: a float is required On Thu, Jun 12, 2014 at 6:39 PM, Alan Gauld wrote: > On 12/06/14 05:51, jason sam wrote: > >> Hi All, >> I am new to wxPython.I have made a simple GUI that contains a button and >> by pressing that button i am calling another .py >> file(top_block.py)... >> > > No you are not. > You are importing it and then trying to access something called start > which doesn't exist. importing is not the same as running a script. > For a start the _name__ attribute will not be __main__ so your if clause > at the bottom won't be executed. But the class definition > code will have been run so you can access and create those classes > from your wxGUI module. > > However, it is very strange practice to try to create a second > GUI program from a first (not totally unheard of but uncommon). > More usually you would define a dialog and open that from > your Execute method. > > > top_block.start() >> >> The error is: >> >> AttributeError: 'module' object has no attribute 'start' >> > > Please always post the full error message not just a summary. > It has all sorts of useful extra details in there. In this case it doesn't > matter because as the error says your module has no start attribute - > because you didn't create one. > > > > code are also welcome.The same error is received when i >> use'top_block.run()'.Is there any alternative to this commands? >> > > Yep, you didn't define a run() function either. > modules can only execute what you define within them (and one or two > special case attributes defined by Python; all starting with __ > > Thee are several other oddities in your code but this is > probably enough for now. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: uhd_fft.py Type: text/x-python Size: 9220 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: wxgui.py Type: text/x-python Size: 914 bytes Desc: not available URL: From user0873 at gmail.com Thu Jun 12 18:51:56 2014 From: user0873 at gmail.com (jason sam) Date: Thu, 12 Jun 2014 21:51:56 +0500 Subject: [Tutor] AttributeError: 'module' object has no attribute 'start' In-Reply-To: References: Message-ID: The last question i hadn't asked yet..and if u are not interested in answering the question then also refrain from commenting please! On Thu, Jun 12, 2014 at 9:47 PM, Mark Lawrence wrote: > On 12/06/2014 05:51, jason sam wrote: > >> Hi All, >> I am new to wxPython.I have made a simple GUI that contains a button and >> by pressing that button i am calling another .py >> file(top_block.py)...But i am getting error using the command: >> >> top_block.start() >> >> The error is: >> >> AttributeError: 'module' object has no attribute 'start' >> >> The code is as attached.I am using python 2.7.3.Any improvements in the >> code are also welcome.The same error is received when i >> use'top_block.run()'.Is there any alternative to this commands? >> >> Regards, >> Ali >> >> > I suspect that you'll get better answers on the wxpython list, especially > consdiering that you asked there all of two minutes before asking here. > > -- > My fellow Pythonistas, ask not what our language can do for you, ask what > you can do for our language. > > Mark Lawrence > > --- > This email is free from viruses and malware because avast! Antivirus > protection is active. > http://www.avast.com > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Jun 13 09:55:54 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 13 Jun 2014 08:55:54 +0100 Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: References: Message-ID: On 13/06/14 08:21, diliup gabadamudalige wrote: > In my code there are many dictionaries and lists which are used in > various functions. Is it better/pythonic/efficient to have these inside > the function itself or declared at the beginning of the program in which > case they will be global? If you are sharing a set of data structures between several functions thats often a sign they should be in a class. The data then becomes the class instance attributes and the functions become the methods. > global constants and variable have memory allocated to them but when > declared inside a function are created on the fly, used and discarded. That's not usually the main issue when deciding for global/local. Its more about the maintainability of the code and the over use of side effects which makes the code difficult to read. If the data is read only then things are not so bad and globals can be an OK solution. hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From reuben.dlink at gmail.com Fri Jun 13 10:27:07 2014 From: reuben.dlink at gmail.com (Reuben) Date: Fri, 13 Jun 2014 01:27:07 -0700 Subject: [Tutor] How to test mobile apps using python Message-ID: Hi, I would like to know how we could test android mobile apps using python. Lets say I want to test my skype app. How can we go about? Regards, Reuben -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Jun 13 10:32:12 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 13 Jun 2014 09:32:12 +0100 Subject: [Tutor] How to test mobile apps using python In-Reply-To: References: Message-ID: On 13/06/14 09:27, Reuben wrote: > I would like to know how we could test android mobile apps using python. > Lets say I want to test my skype app. How can we go about? Presumably you are writing these mobile apps in Python? If so what are you using to build the mobile app? What library/framework etc? Its not a feature of standard Python I'm aware of, so it would be useful to have the context. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From reuben.dlink at gmail.com Fri Jun 13 10:38:15 2014 From: reuben.dlink at gmail.com (Reuben) Date: Fri, 13 Jun 2014 01:38:15 -0700 Subject: [Tutor] How to test mobile apps using python In-Reply-To: References: Message-ID: These apps are native android apps downloaded from google play. Skype being an example. As far as I am aware all these apps are written in Java. But my problem statement requires that we test this app using python script. Regards, Reuben On Fri, Jun 13, 2014 at 1:32 AM, Alan Gauld wrote: > On 13/06/14 09:27, Reuben wrote: > > I would like to know how we could test android mobile apps using python. >> Lets say I want to test my skype app. How can we go about? >> > > Presumably you are writing these mobile apps in Python? > If so what are you using to build the mobile app? What library/framework > etc? > > Its not a feature of standard Python I'm aware of, so it > would be useful to have the context. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Jun 13 12:17:28 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 13 Jun 2014 11:17:28 +0100 Subject: [Tutor] How to test mobile apps using python In-Reply-To: References: Message-ID: On 13/06/14 09:38, Reuben wrote: > These apps are native android apps downloaded from google play. Skype > being an example. > As far as I am aware all these apps are written in Java. > > But my problem statement requires that we test this app using python script. What kind of "testing" do you have in mind? Code testing will have been done by the developer in their development tools. I assume you only want to test performance or somesuch? There is a Python interpreter/IDE available for Android (QPython) but how you would interact with a native app I don't know. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Fri Jun 13 12:13:49 2014 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Fri, 13 Jun 2014 11:13:49 +0100 (BST) Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: References: Message-ID: <1402654429.32061.YahooMailNeo@web186004.mail.ir2.yahoo.com> CCing the tutor list. Please use ReplyAll when responding to the list. ? Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos >________________________________ > From: diliup gabadamudalige >To: Alan Gauld >Sent: Friday, 13 June 2014, 9:32 >Subject: Re: [Tutor] global variables/constants versus volatile variables/constants > > > >thanks Alan! > >in the program that i am writing i used a class called variables and declare all variables i use in the program in the def __init__ section. >so all variables which otherwise would have to be declared as global so that they can be modified ?by other functions are in a class.? >Is this ok? >The strings, lists and dictionaries which are not read only are used as locals in functions and classes. >If there is a string, list or dict. which is used in several functions is it better to use a global or several locals? > > > >On Fri, Jun 13, 2014 at 1:25 PM, Alan Gauld wrote: > >On 13/06/14 08:21, diliup gabadamudalige wrote: >> >> >>In my code there are many dictionaries and lists which are used in >>>various functions. Is it better/pythonic/efficient to have these inside >>>the function itself or declared at the beginning of the program in which >>>case they will be global? >>> >> If you are sharing a set of data structures between several functions thats often a sign they should be in a class. The data then becomes the class instance attributes and the functions become the methods. >> >> >> >>global constants and variable have memory allocated to them but when >>>declared inside a function are created on the fly, used and discarded. >>> >> That's not usually the main issue when deciding for global/local. >>Its more about the maintainability of the code and the over >>use of side effects which makes the code difficult to read. >>If the data is read only then things are not so bad and globals >>can be an OK solution. >> >>hth >>-- >>Alan G >>Author of the Learn to Program web site >>http://www.alan-g.me.uk/ >>http://www.flickr.com/photos/alangauldphotos >> >>_______________________________________________ >>Tutor maillist ?- ?Tutor at python.org >>To unsubscribe or change subscription options: >>https://mail.python.org/mailman/listinfo/tutor >> > > > >-- > >Diliup Gabadamudalige > >http://www.diliupg.com >http://soft.diliupg.com/ > >********************************************************************************************** >This e-mail is confidential. It may also be legally privileged. If you are not the intended recipient or have received it in error, please delete it and all copies from your system and notify the sender immediately by return e-mail. Any unauthorized reading, reproducing, printing or further dissemination of this e-mail or its contents is strictly prohibited and may be unlawful. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions. >********************************************************************************************** > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Fri Jun 13 12:37:00 2014 From: __peter__ at web.de (Peter Otten) Date: Fri, 13 Jun 2014 12:37 +0200 Subject: [Tutor] Problem reading large files in binary mode References: <53990A94.80705@c12.in> <5399B604.8030407@c12.in> Message-ID: Mirage Web Studio wrote: > Try reading the file in chunks instead: > > CHUNKSIZE = 2**20 > hash = hashlib.md5() > while True: > chunk = f.read(CHUNKSIZE) > if not chunk: > break > hash.update(chunk) > hashvalue = hash.hexdigest() > > > Thank you peter for the above valubale reply. but shouldn't read() by > itself work because i have enough memory to load it or should it be a bug. I think you are right. At least you should get a MemoryError (the well- behaved way of the Python interpreter to say that it cannot allocate enough memory) while your description hints at a segmentation fault. A quick test with the Python versions I have lying around: $ python -c 'open("bigfile", "rb").read()' Traceback (most recent call last): File "", line 1, in MemoryError $ python3.3 -c 'open("bigfile", "rb").read()' Segmentation fault $ python3.3 -V Python 3.3.2+ $ python3.4 -c 'open("bigfile", "rb").read()' Traceback (most recent call last): File "", line 1, in MemoryError So the bug occurs in 3.3 at least up to 3.3.2. If you don't have the latest bugfix release Python 3.3.4 you can try and install that or if you are not tied to 3.3 update to 3.4.1. Note that you may still run out of memory, particularly if you are using the 32 bit version. Also it is never a good idea to load a lot of data into memory when you intend to use it just once. Therefore I recommend that you calculate the checksum the way that I have shown in the example. PS: There was an email in my inbox where eryksun suggests potential improvements to my code: > You might see better performance if you preallocate a bytearray and > `readinto` it. On Windows, you might see even better performance if > you map sections of the file using mmap; the map `length` needs to be > a multiple of ALLOCATIONGRANULARITY (except the residual) to set the > `offset` for a sliding window. While I don't expect significant improvements since the problem is "I/O- bound", i. e. the speed limit is imposed by communication with the harddisk rather than the Python interpreter, you may still find it instructive to compare the various approaches. Another candidate when you are working in an environment where the md5sum utility is available is to delegate the work to the "specialist": hashvalue = subprocess.Popen( ["md5sum", filename], stdout=subprocess.PIPE).communicate()[0].split()[0].decode() From steve at pearwood.info Fri Jun 13 13:25:31 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 13 Jun 2014 21:25:31 +1000 Subject: [Tutor] How to test mobile apps using python In-Reply-To: References: Message-ID: <20140613112531.GF10355@ando> On Fri, Jun 13, 2014 at 01:27:07AM -0700, Reuben wrote: > Hi, > > I would like to know how we could test android mobile apps using python. > Lets say I want to test my skype app. How can we go about? This forum is for learning the Python language, and unfortunately there's no "test_android_app" function in the language. So you're asking a very specialized question: it requires not just knowledge of Python, or of testing frameworks in Python, but of testing frameworks in Python on the Android platform. If you were writing your apps in Python, I'd suggest just using the doctest and unittest modules. For testing native Android apps, I don't know. If you can drive the app using Python, then you can write tests using unittest. https://docs.python.org/2/library/unittest.html unittest is based very closely on Java unit testing, so if you know Java, you should find it quite familiar. Other than that, you may find some help from a Jython discussion group, they might be able to suggest how to test Java apps from Python. Or from a Kivvy group, they may be able to suggest how to test apps on Android. Good luck. -- Steven From davea at davea.name Fri Jun 13 13:33:53 2014 From: davea at davea.name (Dave Angel) Date: Fri, 13 Jun 2014 07:33:53 -0400 (EDT) Subject: [Tutor] global variables/constants versus volatile variables/constants References: Message-ID: diliup gabadamudalige Wrote in message: > there are many dictionaries and lists which are used in various functions. Is it better/pythonic/efficient to have these inside the function itself or declared at the beginning of the program in which case they will be global? They are all read only. I understand that global constants and variable have memory allocated to them but when declared inside a function are created on the fly, used and discarded. > (Please use text mail to post here, not html. Html messages can cause several kinds of problems in a text mailing list. ) If they are read-only, you don't want to waste time recreating them over and over. Neither do you want to be repeating the code that initializes them. So make them either global or a class member, and be sure to name them all caps to signify they shouldn't be altered. -- DaveA From steve at pearwood.info Fri Jun 13 13:45:24 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 13 Jun 2014 21:45:24 +1000 Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: References: Message-ID: <20140613114524.GG10355@ando> On Fri, Jun 13, 2014 at 12:51:25PM +0530, diliup gabadamudalige wrote: > Hi All! > Hope everyone is well. > > In my code there are many dictionaries and lists which are used in various > functions. Is it better/pythonic/efficient to have these inside the > function itself or declared at the beginning of the program in which case > they will be global? They are all read only. I understand that global > constants and variable have memory allocated to them but when declared > inside a function are created on the fly, used and discarded. Please > enlighten me further on this and correct me if i'm wrong. A good question. Depending on the size of these dictionaries and lists, worrying about efficiency here may be premature optimization. As they say: "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." -- Donald Knuth "The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet." -- Michael A. Jackson "More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason ? including blind stupidity." -- W.A. Wulf If these lists and dicts are small, say, fewer than a dozen items, the time to create and destroy them is probably trivial, especially if you construct them from constant literals. In that case, it's a matter of personal taste whether you prefer them as global constants or local to a function. But if it takes a long time to build the list, then you definitely should move it outside the function and perform the initialisation step only once: # This may take a while... PRIMES = [prime(i) for i in range(1, 1000001)] If your lists really are read-only, then you should consider turning them into tuples: # Not this: SOME_ITEMS = ["this", "that", "another"] # Better: SOME_ITEMS = ("this", "that", "another") One advantage of the tuple is that in recent versions of Python, the tuple may be compiled as a constant instead of being built at runtime. This is from Python 2.7: py> from dis import dis py> code = compile("x = [2, 4, 8]", "", "exec") py> dis(code) 1 0 LOAD_CONST 0 (2) 3 LOAD_CONST 1 (4) 6 LOAD_CONST 2 (8) 9 BUILD_LIST 3 12 STORE_NAME 0 (x) 15 LOAD_CONST 3 (None) 18 RETURN_VALUE py> code = compile("x = (2, 4, 8)", "", "exec") py> dis(code) 1 0 LOAD_CONST 4 ((2, 4, 8)) 3 STORE_NAME 0 (x) 6 LOAD_CONST 3 (None) 9 RETURN_VALUE -- Steven From fomcl at yahoo.com Fri Jun 13 14:10:28 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 13 Jun 2014 05:10:28 -0700 (PDT) Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: <20140613114524.GG10355@ando> References: <20140613114524.GG10355@ando> Message-ID: <1402661428.72896.YahooMailNeo@web163805.mail.gq1.yahoo.com> ----- Original Message ----- > From: Steven D'Aprano > To: tutor at python.org > Cc: > Sent: Friday, June 13, 2014 1:45 PM > Subject: Re: [Tutor] global variables/constants versus volatile variables/constants > > On Fri, Jun 13, 2014 at 12:51:25PM +0530, diliup gabadamudalige wrote: >> Hi All! >> Hope everyone is well. >> >> In my code there are many dictionaries and lists which are used in various >> functions. Is it better/pythonic/efficient to have these inside the >> function itself or declared at the beginning of the program in which case >> they will be global? They are all read only. I understand that global >> constants and variable have memory allocated to them but when declared >> inside a function are created on the fly, used and discarded. Please >> enlighten me further on this and correct me if i'm wrong. > > A good question. > > Depending on the size of these dictionaries and lists, worrying about > efficiency here may be premature optimization. As they say: > > > ? "We should forget about small efficiencies, say about 97% of > ? ? the time: premature optimization is the root of all evil." > ? ? -- Donald Knuth The other day I used collections.namedtuple and I re-initialized Record (see below) with every function*) call. Bad idea! It looks nicer because I did not need a global (and globals are baaad, mkay?), but it was *much* slower. I processed a log of a few million lines, I think. # bad --> time-consuming import collections def do_something_with(raw_record): ?? Record = collections.namedtuple("_", " ".join("v%%03d" % i for i in range(100))) ?? return Record(*raw_record.split()) # better --> even though it uses a global variable import collections Record = collections.namedtuple("_", " ".join("v%%03d" % i for i in range(100))) def do_something_with(raw_record): ?? return Record(*raw_record.split()) *) the function contained more code, of course! Albert-Jan From davea at davea.name Fri Jun 13 14:38:00 2014 From: davea at davea.name (Dave Angel) Date: Fri, 13 Jun 2014 08:38:00 -0400 (EDT) Subject: [Tutor] AttributeError: 'module' object has no attribute 'start' References: Message-ID: jason sam Wrote in message: > (Please use text mail, as html gets distorted and/or jumbled. Also, many people here cannot see attachments, so you should paste them into your text message. And be sure to put a marker before each file indicating it's original filename) self.update_grid() File "/usr/local/lib/python2.7/dist-packages/g nuradio/wxgui/fft_window.py", line 401, in update_grid baseband_freq - sample_rate/2.0, TypeError: unsupported operand type(s) for -: 'MyFrame' and 'float' You have several tracebacks, but perhaps fixing this one will give you a head start on the others. > Your variable baseband_freq is not a float, but an instance of class MyFrame. Perhaps you had intended to call a method of that class, instead of using the class instance. -- DaveA From steve at pearwood.info Fri Jun 13 15:08:40 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 13 Jun 2014 23:08:40 +1000 Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: <1402661428.72896.YahooMailNeo@web163805.mail.gq1.yahoo.com> References: <20140613114524.GG10355@ando> <1402661428.72896.YahooMailNeo@web163805.mail.gq1.yahoo.com> Message-ID: <20140613130840.GH10355@ando> On Fri, Jun 13, 2014 at 05:10:28AM -0700, Albert-Jan Roskam wrote: > The other day I used collections.namedtuple and I re-initialized > Record (see below) with every function*) call. Bad idea! It looks > nicer because I did not need a global (and globals are baaad, mkay?), > but it was *much* slower. I processed a log of a few million lines, I > think. > > # bad --> time-consuming > import collections > > def do_something_with(raw_record): > ?? Record = collections.namedtuple("_", " ".join("v%%03d" % i for i in range(100))) > ?? return Record(*raw_record.split()) Look at how much work you do here. First, you create a long string of the form: "v000 v001 v002 v003 ... v099" representing 1000 v-digits names. Then you create a brand new Record class that takes those 100 v-digits names as arguments. Creating that class requires building a string, parsing it as Python code, and then running it. (You're not expected to know that, but if you read the source code for namedtuple you will see that's how it works.) So creating that class is slow. Every time you call the function, it builds a new "v000 ... v099" string, from scratch, then builds a new class, also from scratch, and finally populates an instance of that class with 100 values from the raw_record. Only that last step needs to be done inside the function. > # better --> even though it uses a global variable > import collections > > Record = collections.namedtuple("_", " ".join("v%%03d" % i for i in range(100))) [Aside: you may find it easier to debug problems with this if you give the namedtuple class a sensible name, like "Record", rather than "_".] How is that a global *variable*? It's a global name, "Record", but it is no more a "variable" than it would be if you did: class Record(tuple): def __new__(cls, v000, v001, v002, ... , v099): # code goes here @property def v000(self): return self[0] # likewise for v001, v002, ... v099 # plus additional methods namedtuple is a factory function which creates a class. Buried deep within it is a class statement, just as if you had written the class yourself. Normally, when you create a class, you don't treat it as a variable, you treat it as a constant, like functions. That is no different from classes you create with the class keyword. So "global variables are bad" doesn't apply because it's not a variable. Even if it were a variable, what really matters is not that it gets stored in the global scope, but whether or not it gets explicitly passed to functions as arguments, or implicitly modified secretly behind the scenes. For example: # Not bad data = ["put", "stuff", "here"] process(data) do_things_with(data) # Bad, for various reasons data = ["put", "stuff", "here"] process() # process what? do_things_with() # What are we doing things with? In the first case, "data" may be stored in the global scope, but inside each function it is treated as a regular local variable. Let's contrast how one might operate on a second set of data in each case: # Too easy process(some_other_data) # Ouch, this is painful save_data = data data = some_other_data process() data = save_data # restore the previous value Global variables aren't bad because Moses came down from the mountains with a stone tablet that declares that they are bad. They're bad because they cause excessive coupling, they operate by side-effect, they spoil idepotent code, and they are implicit instead of explicit. > def do_something_with(raw_record): > ?? return Record(*raw_record.split()) Much more sensible! -- Steven From breamoreboy at yahoo.co.uk Fri Jun 13 15:14:43 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 13 Jun 2014 14:14:43 +0100 Subject: [Tutor] AttributeError: 'module' object has no attribute 'start' In-Reply-To: References: Message-ID: On 12/06/2014 17:51, jason sam wrote: > The last question i hadn't asked yet..and if u are not interested in > answering the question then also refrain from commenting please! > > On Thu, Jun 12, 2014 at 9:47 PM, Mark Lawrence > wrote: > > On 12/06/2014 05:51, jason sam wrote: > > Hi All, > I am new to wxPython.I have made a simple GUI that contains a > button and > by pressing that button i am calling another .py > file(top_block.py)...But i am getting error using the command: > > top_block.start() > > The error is: > > AttributeError: 'module' object has no attribute 'start' > > The code is as attached.I am using python 2.7.3.Any improvements > in the > code are also welcome.The same error is received when i > use'top_block.run()'.Is there any alternative to this commands? > > Regards, > Ali > > > I suspect that you'll get better answers on the wxpython list, > especially consdiering that you asked there all of two minutes > before asking here. > > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence > > --- Please don't top post on this list. Please don't try telling me what to do or not do. Thanks in anticipation. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From ramit.prasad at jpmorgan.com Fri Jun 13 17:05:21 2014 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 13 Jun 2014 15:05:21 +0000 Subject: [Tutor] AttributeError: 'module' object has no attribute 'start' In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF47424849384@SCACMX008.exchad.jpmchase.net> > Please don't top post on this list. Please don't try telling me what to > do or not do. Thanks in anticipation. Oh the irony. Ramit 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. From fomcl at yahoo.com Fri Jun 13 19:14:46 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 13 Jun 2014 10:14:46 -0700 (PDT) Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: <20140613130840.GH10355@ando> References: <20140613114524.GG10355@ando> <1402661428.72896.YahooMailNeo@web163805.mail.gq1.yahoo.com> <20140613130840.GH10355@ando> Message-ID: <1402679686.52228.YahooMailNeo@web163804.mail.gq1.yahoo.com> ----- Original Message ----- > From: Steven D'Aprano > To: tutor at python.org > Cc: > Sent: Friday, June 13, 2014 3:08 PM > Subject: Re: [Tutor] global variables/constants versus volatile variables/constants > > On Fri, Jun 13, 2014 at 05:10:28AM -0700, Albert-Jan Roskam wrote: > >> The other day I used collections.namedtuple and I re-initialized >> Record (see below) with every function*) call. Bad idea! It looks >> nicer because I did not need a global (and globals are baaad, mkay?), >> but it was *much* slower. I processed a log of a few million lines, I >> think. >> >> # bad --> time-consuming >> import collections >> >> def do_something_with(raw_record): >> ?? Record = collections.namedtuple("_", " > ".join("v%%03d" % i for i in range(100))) >> ?? return Record(*raw_record.split()) > > Look at how much work you do here. First, you create a long string of > the form: > > ? ? "v000 v001 v002 v003 ... v099" > > representing 1000 v-digits names. Then you create a brand new Record > class that takes those 100 v-digits names as arguments. Creating that > class requires building a string, parsing it as Python code, and then > running it. (You're not expected to know that, but if you read the > source code for namedtuple you will see that's how it works.) So > creating that class is slow. Every time you call the function, it builds > a new "v000 ... v099" string, from scratch, then builds a new class, > also from scratch, and finally populates an instance of that class with > 100 values from the raw_record. > > Only that last step needs to be done inside the function. ? Hmm, if I create the namedtuple with 'verbose=True' it *really* makes clear why it took so much longer. (what's '_property' btw? I know 'property' and the newer decorator wih the same name, but not _property). In [1]: import collections In [2]: Record = collections.namedtuple("_", " ".join("v%03d" % i for i in range(100)),verbose=True) class _(tuple): ??? '_(v000, v001, v002, v003, v004, v005, v006, v007, v008, v009, v010, v011, v012, v013, v014, v015, v016, v017, v018, v019, v020, v021, v022, v023, v024, v025, v026, v027, v028, v029, v030, v031, v032, v033, v034, v035, v036, v037, v038, v039, v040, v041, v042, v043, v044, v045, v046, v047, v048, v049, v050, v051, v052, v053, v054, v055, v056, v057, v058, v059, v060, v061, v062, v063, v064, v065, v066, v067, v068, v069, v070, v071, v072, v073, v074, v075, v076, v077, v078, v079, v080, v081, v082, v083, v084, v085, v086, v087, v088, v089, v090, v091, v092, v093, v094, v095, v096, v097, v098, v099)' ??? __slots__ = () ??? _fields = ('v000', 'v001', 'v002', 'v003', 'v004', 'v005', 'v006', 'v007', 'v008', 'v009', 'v010', 'v011', 'v012', 'v013', 'v014', 'v015', 'v016', 'v017', 'v018', 'v019', 'v020', 'v021', 'v022', 'v023', 'v024', 'v025', 'v026', 'v027', 'v028', 'v029', 'v030', 'v031', 'v032', 'v033', 'v034', 'v035', 'v036', 'v037', 'v038', 'v039', 'v040', 'v041', 'v042', 'v043', 'v044', 'v045', 'v046', 'v047', 'v048', 'v049', 'v050', 'v051', 'v052', 'v053', 'v054', 'v055', 'v056', 'v057', 'v058', 'v059', 'v060', 'v061', 'v062', 'v063', 'v064', 'v065', 'v066', 'v067', 'v068', 'v069', 'v070', 'v071', 'v072', 'v073', 'v074', 'v075', 'v076', 'v077', 'v078', 'v079', 'v080', 'v081', 'v082', 'v083', 'v084', 'v085', 'v086', 'v087', 'v088', 'v089', 'v090', 'v091', 'v092', 'v093', 'v094', 'v095', 'v096', 'v097', 'v098', 'v099') ??? def __new__(_cls, v000, v001, v002, v003, v004, v005, v006, v007, v008, v009, v010, v011, v012, v013, v014, v015, v016, v017, v018, v019, v020, v021, v022, v023, v024, v025, v026, v027, v028, v029, v030, v031, v032, v033, v034, v035, v036, v037, v038, v039, v040, v041, v042, v043, v044, v045, v046, v047, v048, v049, v050, v051, v052, v053, v054, v055, v056, v057, v058, v059, v060, v061, v062, v063, v064, v065, v066, v067, v068, v069, v070, v071, v072, v073, v074, v075, v076, v077, v078, v079, v080, v081, v082, v083, v084, v085, v086, v087, v088, v089, v090, v091, v092, v093, v094, v095, v096, v097, v098, v099): ??????? 'Create new instance of _(v000, v001, v002, v003, v004, v005, v006, v007, v008, v009, v010, v011, v012, v013, v014, v015, v016, v017, v018, v019, v020, v021, v022, v023, v024, v025, v026, v027, v028, v029, v030, v031, v032, v033, v034, v035, v036, v037, v038, v039, v040, v041, v042, v043, v044, v045, v046, v047, v048, v049, v050, v051, v052, v053, v054, v055, v056, v057, v058, v059, v060, v061, v062, v063, v064, v065, v066, v067, v068, v069, v070, v071, v072, v073, v074, v075, v076, v077, v078, v079, v080, v081, v082, v083, v084, v085, v086, v087, v088, v089, v090, v091, v092, v093, v094, v095, v096, v097, v098, v099)' ??????? return _tuple.__new__(_cls, (v000, v001, v002, v003, v004, v005, v006, v007, v008, v009, v010, v011, v012, v013, v014, v015, v016, v017, v018, v019, v020, v021, v022, v023, v024, v025, v026, v027, v028, v029, v030, v031, v032, v033, v034, v035, v036, v037, v038, v039, v040, v041, v042, v043, v044, v045, v046, v047, v048, v049, v050, v051, v052, v053, v054, v055, v056, v057, v058, v059, v060, v061, v062, v063, v064, v065, v066, v067, v068, v069, v070, v071, v072, v073, v074, v075, v076, v077, v078, v079, v080, v081, v082, v083, v084, v085, v086, v087, v088, v089, v090, v091, v092, v093, v094, v095, v096, v097, v098, v099)) ??? @classmethod ??? def _make(cls, iterable, new=tuple.__new__, len=len): ??????? 'Make a new _ object from a sequence or iterable' ??????? result = new(cls, iterable) ??????? if len(result) != 100: ??????????? raise TypeError('Expected 100 arguments, got %d' % len(result)) ??????? return result ??? def __repr__(self): ??????? 'Return a nicely formatted representation string' ??????? return '_(v000=%r, v001=%r, v002=%r, v003=%r, v004=%r, v005=%r, v006=%r, v007=%r, v008=%r, v009=%r, v010=%r, v011=%r, v012=%r, v013=%r, v014=%r, v015=%r, v016=%r, v017=%r, v018=%r, v019=%r, v020=%r, v021=%r, v022=%r, v023=%r, v024=%r, v025=%r, v026=%r, v027=%r, v028=%r, v029=%r, v030=%r, v031=%r, v032=%r, v033=%r, v034=%r, v035=%r, v036=%r, v037=%r, v038=%r, v039=%r, v040=%r, v041=%r, v042=%r, v043=%r, v044=%r, v045=%r, v046=%r, v047=%r, v048=%r, v049=%r, v050=%r, v051=%r, v052=%r, v053=%r, v054=%r, v055=%r, v056=%r, v057=%r, v058=%r, v059=%r, v060=%r, v061=%r, v062=%r, v063=%r, v064=%r, v065=%r, v066=%r, v067=%r, v068=%r, v069=%r, v070=%r, v071=%r, v072=%r, v073=%r, v074=%r, v075=%r, v076=%r, v077=%r, v078=%r, v079=%r, v080=%r, v081=%r, v082=%r, v083=%r, v084=%r, v085=%r, v086=%r, v087=%r, v088=%r, v089=%r, v090=%r, v091=%r, v092=%r, v093=%r, v094=%r, v095=%r, v096=%r, v097=%r, v098=%r, v099=%r)' % self ??? def _asdict(self): ??????? 'Return a new OrderedDict which maps field names to their values' ??????? return OrderedDict(zip(self._fields, self)) ??? __dict__ = property(_asdict) ??? def _replace(_self, **kwds): ??????? 'Return a new _ object replacing specified fields with new values' ??????? result = _self._make(map(kwds.pop, ('v000', 'v001', 'v002', 'v003', 'v004', 'v005', 'v006', 'v007', 'v008', 'v009', 'v010', 'v011', 'v012', 'v013', 'v014', 'v015', 'v016', 'v017', 'v018', 'v019', 'v020', 'v021', 'v022', 'v023', 'v024', 'v025', 'v026', 'v027', 'v028', 'v029', 'v030', 'v031', 'v032', 'v033', 'v034', 'v035', 'v036', 'v037', 'v038', 'v039', 'v040', 'v041', 'v042', 'v043', 'v044', 'v045', 'v046', 'v047', 'v048', 'v049', 'v050', 'v051', 'v052', 'v053', 'v054', 'v055', 'v056', 'v057', 'v058', 'v059', 'v060', 'v061', 'v062', 'v063', 'v064', 'v065', 'v066', 'v067', 'v068', 'v069', 'v070', 'v071', 'v072', 'v073', 'v074', 'v075', 'v076', 'v077', 'v078', 'v079', 'v080', 'v081', 'v082', 'v083', 'v084', 'v085', 'v086', 'v087', 'v088', 'v089', 'v090', 'v091', 'v092', 'v093', 'v094', 'v095', 'v096', 'v097', 'v098', 'v099'), _self)) ??????? if kwds: ??????????? raise ValueError('Got unexpected field names: %r' % kwds.keys()) ??????? return result ??? def __getnewargs__(self): ??????? 'Return self as a plain tuple.? Used by copy and pickle.' ??????? return tuple(self) ??? v000 = _property(_itemgetter(0), doc='Alias for field number 0') ??? v001 = _property(_itemgetter(1), doc='Alias for field number 1') ??? v002 = _property(_itemgetter(2), doc='Alias for field number 2') ??? v003 = _property(_itemgetter(3), doc='Alias for field number 3') ??? v004 = _property(_itemgetter(4), doc='Alias for field number 4') ??? v005 = _property(_itemgetter(5), doc='Alias for field number 5') ??? v006 = _property(_itemgetter(6), doc='Alias for field number 6') ??? v007 = _property(_itemgetter(7), doc='Alias for field number 7') ??? v008 = _property(_itemgetter(8), doc='Alias for field number 8') ??? v009 = _property(_itemgetter(9), doc='Alias for field number 9') ??? v010 = _property(_itemgetter(10), doc='Alias for field number 10') ??? v011 = _property(_itemgetter(11), doc='Alias for field number 11') ??? v012 = _property(_itemgetter(12), doc='Alias for field number 12') ??? v013 = _property(_itemgetter(13), doc='Alias for field number 13') ??? v014 = _property(_itemgetter(14), doc='Alias for field number 14') ??? v015 = _property(_itemgetter(15), doc='Alias for field number 15') ??? v016 = _property(_itemgetter(16), doc='Alias for field number 16') ??? v017 = _property(_itemgetter(17), doc='Alias for field number 17') ??? v018 = _property(_itemgetter(18), doc='Alias for field number 18') ??? v019 = _property(_itemgetter(19), doc='Alias for field number 19') ??? v020 = _property(_itemgetter(20), doc='Alias for field number 20') ??? v021 = _property(_itemgetter(21), doc='Alias for field number 21') ??? v022 = _property(_itemgetter(22), doc='Alias for field number 22') ??? v023 = _property(_itemgetter(23), doc='Alias for field number 23') ??? v024 = _property(_itemgetter(24), doc='Alias for field number 24') ??? v025 = _property(_itemgetter(25), doc='Alias for field number 25') ??? v026 = _property(_itemgetter(26), doc='Alias for field number 26') ??? v027 = _property(_itemgetter(27), doc='Alias for field number 27') ??? v028 = _property(_itemgetter(28), doc='Alias for field number 28') ??? v029 = _property(_itemgetter(29), doc='Alias for field number 29') ??? v030 = _property(_itemgetter(30), doc='Alias for field number 30') ??? v031 = _property(_itemgetter(31), doc='Alias for field number 31') ??? v032 = _property(_itemgetter(32), doc='Alias for field number 32') ??? v033 = _property(_itemgetter(33), doc='Alias for field number 33') ??? v034 = _property(_itemgetter(34), doc='Alias for field number 34') ??? v035 = _property(_itemgetter(35), doc='Alias for field number 35') ??? v036 = _property(_itemgetter(36), doc='Alias for field number 36') ??? v037 = _property(_itemgetter(37), doc='Alias for field number 37') ??? v038 = _property(_itemgetter(38), doc='Alias for field number 38') ??? v039 = _property(_itemgetter(39), doc='Alias for field number 39') ??? v040 = _property(_itemgetter(40), doc='Alias for field number 40') ??? v041 = _property(_itemgetter(41), doc='Alias for field number 41') ??? v042 = _property(_itemgetter(42), doc='Alias for field number 42') ??? v043 = _property(_itemgetter(43), doc='Alias for field number 43') ??? v044 = _property(_itemgetter(44), doc='Alias for field number 44') ??? v045 = _property(_itemgetter(45), doc='Alias for field number 45') ??? v046 = _property(_itemgetter(46), doc='Alias for field number 46') ??? v047 = _property(_itemgetter(47), doc='Alias for field number 47') ??? v048 = _property(_itemgetter(48), doc='Alias for field number 48') ??? v049 = _property(_itemgetter(49), doc='Alias for field number 49') ??? v050 = _property(_itemgetter(50), doc='Alias for field number 50') ??? v051 = _property(_itemgetter(51), doc='Alias for field number 51') ??? v052 = _property(_itemgetter(52), doc='Alias for field number 52') ??? v053 = _property(_itemgetter(53), doc='Alias for field number 53') ??? v054 = _property(_itemgetter(54), doc='Alias for field number 54') ??? v055 = _property(_itemgetter(55), doc='Alias for field number 55') ??? v056 = _property(_itemgetter(56), doc='Alias for field number 56') ??? v057 = _property(_itemgetter(57), doc='Alias for field number 57') ??? v058 = _property(_itemgetter(58), doc='Alias for field number 58') ??? v059 = _property(_itemgetter(59), doc='Alias for field number 59') ??? v060 = _property(_itemgetter(60), doc='Alias for field number 60') ??? v061 = _property(_itemgetter(61), doc='Alias for field number 61') ??? v062 = _property(_itemgetter(62), doc='Alias for field number 62') ??? v063 = _property(_itemgetter(63), doc='Alias for field number 63') ??? v064 = _property(_itemgetter(64), doc='Alias for field number 64') ??? v065 = _property(_itemgetter(65), doc='Alias for field number 65') ??? v066 = _property(_itemgetter(66), doc='Alias for field number 66') ??? v067 = _property(_itemgetter(67), doc='Alias for field number 67') ??? v068 = _property(_itemgetter(68), doc='Alias for field number 68') ??? v069 = _property(_itemgetter(69), doc='Alias for field number 69') ??? v070 = _property(_itemgetter(70), doc='Alias for field number 70') ??? v071 = _property(_itemgetter(71), doc='Alias for field number 71') ??? v072 = _property(_itemgetter(72), doc='Alias for field number 72') ??? v073 = _property(_itemgetter(73), doc='Alias for field number 73') ??? v074 = _property(_itemgetter(74), doc='Alias for field number 74') ??? v075 = _property(_itemgetter(75), doc='Alias for field number 75') ??? v076 = _property(_itemgetter(76), doc='Alias for field number 76') ??? v077 = _property(_itemgetter(77), doc='Alias for field number 77') ??? v078 = _property(_itemgetter(78), doc='Alias for field number 78') ??? v079 = _property(_itemgetter(79), doc='Alias for field number 79') ??? v080 = _property(_itemgetter(80), doc='Alias for field number 80') ??? v081 = _property(_itemgetter(81), doc='Alias for field number 81') ??? v082 = _property(_itemgetter(82), doc='Alias for field number 82') ??? v083 = _property(_itemgetter(83), doc='Alias for field number 83') ??? v084 = _property(_itemgetter(84), doc='Alias for field number 84') ??? v085 = _property(_itemgetter(85), doc='Alias for field number 85') ??? v086 = _property(_itemgetter(86), doc='Alias for field number 86') ??? v087 = _property(_itemgetter(87), doc='Alias for field number 87') ??? v088 = _property(_itemgetter(88), doc='Alias for field number 88') ??? v089 = _property(_itemgetter(89), doc='Alias for field number 89') ??? v090 = _property(_itemgetter(90), doc='Alias for field number 90') ??? v091 = _property(_itemgetter(91), doc='Alias for field number 91') ??? v092 = _property(_itemgetter(92), doc='Alias for field number 92') ??? v093 = _property(_itemgetter(93), doc='Alias for field number 93') ??? v094 = _property(_itemgetter(94), doc='Alias for field number 94') ??? v095 = _property(_itemgetter(95), doc='Alias for field number 95') ??? v096 = _property(_itemgetter(96), doc='Alias for field number 96') ??? v097 = _property(_itemgetter(97), doc='Alias for field number 97') ??? v098 = _property(_itemgetter(98), doc='Alias for field number 98') ??? v099 = _property(_itemgetter(99), doc='Alias for field number 99') > Global variables aren't bad because Moses came down from the mountains > with a stone tablet that declares that they are bad. They're bad because > they cause excessive coupling, they operate by side-effect, they spoil > idepotent code, and they are implicit instead of explicit. LOL :-) Textbooks conditioned me to have a generalized fear of globals. Like Little Albert: http://en.wikipedia.org/wiki/Little_Albert_experiment :-) >> def do_something_with(raw_record): >> ?? return Record(*raw_record.split()) > > Much more sensible! > > > > > -- > Steven From admin at c12.in Fri Jun 13 13:20:23 2014 From: admin at c12.in (Mirage Web Studio) Date: Fri, 13 Jun 2014 16:50:23 +0530 Subject: [Tutor] Problem reading large files in binary mode In-Reply-To: References: <53990A94.80705@c12.in> <5399B604.8030407@c12.in> Message-ID: <539ADE77.9060809@c12.in> Thank you, i will keep all that in mind. My python version is 3.3.5 George On 13-06-2014 16:07, Peter Otten wrote: > Mirage Web Studio wrote: > >> Try reading the file in chunks instead: >> >> CHUNKSIZE = 2**20 >> hash = hashlib.md5() >> while True: >> chunk = f.read(CHUNKSIZE) >> if not chunk: >> break >> hash.update(chunk) >> hashvalue = hash.hexdigest() >> >> >> Thank you peter for the above valubale reply. but shouldn't read() by >> itself work because i have enough memory to load it or should it be a bug. > I think you are right. At least you should get a MemoryError (the well- > behaved way of the Python interpreter to say that it cannot allocate enough > memory) while your description hints at a segmentation fault. > > A quick test with the Python versions I have lying around: > > $ python -c 'open("bigfile", "rb").read()' > Traceback (most recent call last): > File "", line 1, in > MemoryError > $ python3.3 -c 'open("bigfile", "rb").read()' > Segmentation fault > $ python3.3 -V > Python 3.3.2+ > $ python3.4 -c 'open("bigfile", "rb").read()' > Traceback (most recent call last): > File "", line 1, in > MemoryError > > So the bug occurs in 3.3 at least up to 3.3.2. > > If you don't have the latest bugfix release Python 3.3.4 you can try and > install that or if you are not tied to 3.3 update to 3.4.1. > > Note that you may still run out of memory, particularly if you are using the > 32 bit version. > > Also it is never a good idea to load a lot of data into memory when you > intend to use it just once. Therefore I recommend that you calculate the > checksum the way that I have shown in the example. > > PS: There was an email in my inbox where eryksun suggests potential > improvements to my code: > >> You might see better performance if you preallocate a bytearray and >> `readinto` it. On Windows, you might see even better performance if >> you map sections of the file using mmap; the map `length` needs to be >> a multiple of ALLOCATIONGRANULARITY (except the residual) to set the >> `offset` for a sliding window. > While I don't expect significant improvements since the problem is "I/O- > bound", i. e. the speed limit is imposed by communication with the harddisk > rather than the Python interpreter, you may still find it instructive to > compare the various approaches. > > Another candidate when you are working in an environment where the md5sum > utility is available is to delegate the work to the "specialist": > > hashvalue = subprocess.Popen( > ["md5sum", filename], > stdout=subprocess.PIPE).communicate()[0].split()[0].decode() > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From diliupg at gmail.com Fri Jun 13 18:12:34 2014 From: diliupg at gmail.com (diliup gabadamudalige) Date: Fri, 13 Jun 2014 21:42:34 +0530 Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: <20140613130840.GH10355@ando> References: <20140613114524.GG10355@ando> <1402661428.72896.YahooMailNeo@web163805.mail.gq1.yahoo.com> <20140613130840.GH10355@ando> Message-ID: Thank you all for these elaborate explanations. so would it be wrong to assume that, 1. the list or dict if 'large' and not mutated is better declared once in a Class and used throughout the program? or 2. Lists that are read only are better off being tuples? or 3.the list or dict if 'large' and not mutated is better declared once as a global variable and used throughout the program? The variables, lists carrying variables, and dict. carrying variables are declared as a class. So why not include all above in the same class? Varables and constans which are used throughout the program in one class declared beofr the program enters the main executing loop? wouldn't this be fast and the memory usage static up to a point (for all declared items in the variables class)? On Fri, Jun 13, 2014 at 6:38 PM, Steven D'Aprano wrote: > On Fri, Jun 13, 2014 at 05:10:28AM -0700, Albert-Jan Roskam wrote: > > > The other day I used collections.namedtuple and I re-initialized > > Record (see below) with every function*) call. Bad idea! It looks > > nicer because I did not need a global (and globals are baaad, mkay?), > > but it was *much* slower. I processed a log of a few million lines, I > > think. > > > > # bad --> time-consuming > > import collections > > > > def do_something_with(raw_record): > > Record = collections.namedtuple("_", " ".join("v%%03d" % i for i in > range(100))) > > return Record(*raw_record.split()) > > Look at how much work you do here. First, you create a long string of > the form: > > "v000 v001 v002 v003 ... v099" > > representing 1000 v-digits names. Then you create a brand new Record > class that takes those 100 v-digits names as arguments. Creating that > class requires building a string, parsing it as Python code, and then > running it. (You're not expected to know that, but if you read the > source code for namedtuple you will see that's how it works.) So > creating that class is slow. Every time you call the function, it builds > a new "v000 ... v099" string, from scratch, then builds a new class, > also from scratch, and finally populates an instance of that class with > 100 values from the raw_record. > > Only that last step needs to be done inside the function. > > > > # better --> even though it uses a global variable > > import collections > > > > Record = collections.namedtuple("_", " ".join("v%%03d" % i for i in > range(100))) > > [Aside: you may find it easier to debug problems with this if you give > the namedtuple class a sensible name, like "Record", rather than "_".] > > How is that a global *variable*? It's a global name, "Record", but it is > no more a "variable" than it would be if you did: > > class Record(tuple): > def __new__(cls, v000, v001, v002, ... , v099): > # code goes here > > @property > def v000(self): > return self[0] > > # likewise for v001, v002, ... v099 > # plus additional methods > > > namedtuple is a factory function which creates a class. Buried deep > within it is a class statement, just as if you had written the class > yourself. Normally, when you create a class, you don't treat it as a > variable, you treat it as a constant, like functions. That is no > different from classes you create with the class keyword. So "global > variables are bad" doesn't apply because it's not a variable. > > Even if it were a variable, what really matters is not that it gets > stored in the global scope, but whether or not it gets explicitly passed > to functions as arguments, or implicitly modified secretly behind the > scenes. For example: > > # Not bad > data = ["put", "stuff", "here"] > process(data) > do_things_with(data) > > > # Bad, for various reasons > data = ["put", "stuff", "here"] > process() # process what? > do_things_with() # What are we doing things with? > > > > In the first case, "data" may be stored in the global scope, but inside > each function it is treated as a regular local variable. Let's contrast > how one might operate on a second set of data in each case: > > # Too easy > process(some_other_data) > > > # Ouch, this is painful > save_data = data > data = some_other_data > process() > data = save_data # restore the previous value > > > Global variables aren't bad because Moses came down from the mountains > with a stone tablet that declares that they are bad. They're bad because > they cause excessive coupling, they operate by side-effect, they spoil > idepotent code, and they are implicit instead of explicit. > > > > > > def do_something_with(raw_record): > > return Record(*raw_record.split()) > > Much more sensible! > > > > -- > Steven > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Diliup Gabadamudalige http://www.diliupg.com http://soft.diliupg.com/ ********************************************************************************************** This e-mail is confidential. It may also be legally privileged. If you are not the intended recipient or have received it in error, please delete it and all copies from your system and notify the sender immediately by return e-mail. Any unauthorized reading, reproducing, printing or further dissemination of this e-mail or its contents is strictly prohibited and may be unlawful. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions. ********************************************************************************************** -------------- next part -------------- An HTML attachment was scrubbed... URL: From diliupg at gmail.com Fri Jun 13 18:13:11 2014 From: diliupg at gmail.com (diliup gabadamudalige) Date: Fri, 13 Jun 2014 21:43:11 +0530 Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: References: <20140613114524.GG10355@ando> <1402661428.72896.YahooMailNeo@web163805.mail.gq1.yahoo.com> <20140613130840.GH10355@ando> Message-ID: Thank you all for these elaborate explanations. so would it be wrong to assume that, 1. the list or dict if 'large' and not mutated is better declared once in a Class and used throughout the program? or 2. Lists that are read only are better off being tuples? or 3.the list or dict if 'large' and not mutated is better declared once as a global variable and used throughout the program? The variables, lists carrying variables, and dict. carrying variables are declared as a class. So why not include all above in the same class? Variables and constants which are used throughout the program in one class declared before the program enters the main executing loop? wouldn't this be fast and the memory usage static up to a point (for all declared items in the variables class)? On Fri, Jun 13, 2014 at 9:42 PM, diliup gabadamudalige wrote: > Thank you all for these elaborate explanations. > so would it be wrong to assume that, > 1. the list or dict if 'large' and not mutated is better declared once in > a Class and used throughout the program? > or > 2. Lists that are read only are better off being tuples? > or > 3.the list or dict if 'large' and not mutated is better declared once as a > global variable and used throughout the program? > > The variables, lists carrying variables, and dict. carrying variables are > declared as a class. So why not include all above in the same class? > Varables and constans which are used throughout the program in one class > declared beofr the program enters the main executing loop? > wouldn't this be fast and the memory usage static up to a point (for all > declared items in the variables class)? > > > On Fri, Jun 13, 2014 at 6:38 PM, Steven D'Aprano > wrote: > >> On Fri, Jun 13, 2014 at 05:10:28AM -0700, Albert-Jan Roskam wrote: >> >> > The other day I used collections.namedtuple and I re-initialized >> > Record (see below) with every function*) call. Bad idea! It looks >> > nicer because I did not need a global (and globals are baaad, mkay?), >> > but it was *much* slower. I processed a log of a few million lines, I >> > think. >> > >> > # bad --> time-consuming >> > import collections >> > >> > def do_something_with(raw_record): >> > Record = collections.namedtuple("_", " ".join("v%%03d" % i for i in >> range(100))) >> > return Record(*raw_record.split()) >> >> Look at how much work you do here. First, you create a long string of >> the form: >> >> "v000 v001 v002 v003 ... v099" >> >> representing 1000 v-digits names. Then you create a brand new Record >> class that takes those 100 v-digits names as arguments. Creating that >> class requires building a string, parsing it as Python code, and then >> running it. (You're not expected to know that, but if you read the >> source code for namedtuple you will see that's how it works.) So >> creating that class is slow. Every time you call the function, it builds >> a new "v000 ... v099" string, from scratch, then builds a new class, >> also from scratch, and finally populates an instance of that class with >> 100 values from the raw_record. >> >> Only that last step needs to be done inside the function. >> >> >> > # better --> even though it uses a global variable >> > import collections >> > >> > Record = collections.namedtuple("_", " ".join("v%%03d" % i for i in >> range(100))) >> >> [Aside: you may find it easier to debug problems with this if you give >> the namedtuple class a sensible name, like "Record", rather than "_".] >> >> How is that a global *variable*? It's a global name, "Record", but it is >> no more a "variable" than it would be if you did: >> >> class Record(tuple): >> def __new__(cls, v000, v001, v002, ... , v099): >> # code goes here >> >> @property >> def v000(self): >> return self[0] >> >> # likewise for v001, v002, ... v099 >> # plus additional methods >> >> >> namedtuple is a factory function which creates a class. Buried deep >> within it is a class statement, just as if you had written the class >> yourself. Normally, when you create a class, you don't treat it as a >> variable, you treat it as a constant, like functions. That is no >> different from classes you create with the class keyword. So "global >> variables are bad" doesn't apply because it's not a variable. >> >> Even if it were a variable, what really matters is not that it gets >> stored in the global scope, but whether or not it gets explicitly passed >> to functions as arguments, or implicitly modified secretly behind the >> scenes. For example: >> >> # Not bad >> data = ["put", "stuff", "here"] >> process(data) >> do_things_with(data) >> >> >> # Bad, for various reasons >> data = ["put", "stuff", "here"] >> process() # process what? >> do_things_with() # What are we doing things with? >> >> >> >> In the first case, "data" may be stored in the global scope, but inside >> each function it is treated as a regular local variable. Let's contrast >> how one might operate on a second set of data in each case: >> >> # Too easy >> process(some_other_data) >> >> >> # Ouch, this is painful >> save_data = data >> data = some_other_data >> process() >> data = save_data # restore the previous value >> >> >> Global variables aren't bad because Moses came down from the mountains >> with a stone tablet that declares that they are bad. They're bad because >> they cause excessive coupling, they operate by side-effect, they spoil >> idepotent code, and they are implicit instead of explicit. >> >> >> >> >> > def do_something_with(raw_record): >> > return Record(*raw_record.split()) >> >> Much more sensible! >> >> >> >> -- >> Steven >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > > > -- > Diliup Gabadamudalige > > http://www.diliupg.com > http://soft.diliupg.com/ > > > ********************************************************************************************** > This e-mail is confidential. It may also be legally privileged. If you are > not the intended recipient or have received it in error, please delete it > and all copies from your system and notify the sender immediately by return > e-mail. Any unauthorized reading, reproducing, printing or further > dissemination of this e-mail or its contents is strictly prohibited and may > be unlawful. Internet communications cannot be guaranteed to be timely, > secure, error or virus-free. The sender does not accept liability for any > errors or omissions. > > ********************************************************************************************** > > -- Diliup Gabadamudalige http://www.diliupg.com http://soft.diliupg.com/ ********************************************************************************************** This e-mail is confidential. It may also be legally privileged. If you are not the intended recipient or have received it in error, please delete it and all copies from your system and notify the sender immediately by return e-mail. Any unauthorized reading, reproducing, printing or further dissemination of this e-mail or its contents is strictly prohibited and may be unlawful. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions. ********************************************************************************************** -------------- next part -------------- An HTML attachment was scrubbed... URL: From diliupg at gmail.com Fri Jun 13 20:20:53 2014 From: diliupg at gmail.com (diliup gabadamudalige) Date: Fri, 13 Jun 2014 23:50:53 +0530 Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: <1402679686.52228.YahooMailNeo@web163804.mail.gq1.yahoo.com> References: <20140613114524.GG10355@ando> <1402661428.72896.YahooMailNeo@web163805.mail.gq1.yahoo.com> <20140613130840.GH10355@ando> <1402679686.52228.YahooMailNeo@web163804.mail.gq1.yahoo.com> Message-ID: pardon my ignorance. last few questions. I declare the following dict at the beginning of the program. KEY_SIGNATURES = {"C": [], "G": ["F"], "D": ["F", "C"], "A": ["F", "C", "G"], "E": ["F", "C", "G", "D"], "B": ["F", "C", "G", "D", "A"], "F#": ["F", "C", "G", "D", "A", "E"], "C#": ["F", "C", "G", "D", "A", "E", "B"], "F": ["B"], "Bb": ["B", "E"], "Eb": ["B", "E", "A"], "Ab": ["B", "E", "A", "D"], "Db": ["B", "E", "A", "D", "G"], "Gb": ["B", "E", "A", "D", "G", "C"], "Cb": ["B", "E", "A", "D", "G", "C", "F"], "Ahm": [], "Ehm": ["F"], "Bhm": ["F", "C"], "F#hm": ["F", "C", "G"], "C#hm": ["F", "C", "G", "D"], "G#hm": ["F", "C", "G", "D", "A"], "D#hm": ["F", "C", "G", "D", "A", "E"], "A#hm": ["F", "C", "G", "D", "A", "E", "B"], "Dhm": ["B"], "Ghm": ["B", "E"], "Chm": ["B", "E", "A"], "Fhm": ["B", "E", "A", "D"], "Bbhm": ["B", "E", "A", "D", "G"], "Ebhm": ["B", "E", "A", "D", "G", "C"], "Abhm": ["B", "E", "A", "D", "G", "C", "F"], "Amm": [], "Emm": ["F"], "Bmm": ["F", "C"], "F#mm": ["F", "C", "G"], "C#mm": ["F", "C", "G", "D"], "G#mm": ["F", "C", "G", "D", "A"], "D#mm": ["F", "C", "G", "D", "A", "E"], "A#mm": ["F", "C", "G", "D", "A", "E", "B"], "Dmm": ["B"], "Gmm": ["B", "E"], "Cmm": ["B", "E", "A"], "Fmm": ["B", "E", "A", "D"], "Bbmm": ["B", "E", "A", "D", "G"], "Ebmm": ["B", "E", "A", "D", "G", "C"], "Abmm": ["B", "E", "A", "D", "G", "C", "F"]} now in the functions when ever I need to access i just use it. eg: sharps_or_flats = KEY_SIGNATURES[key] or I can create it like this class Variables(): def __init__(self, adv): self.choice = choice # level of advancement in questions self.mousepos = (SCREENW / 2, SCREENH / 2) self.SCREEN = pygame.display.get_surface() self.KEY_SIGNATURES = {"C": [], "G": ["F"], "D": ["F", "C"], "A": ["F", "C", "G"], "E": ["F", "C", "G", "D"], "B": ["F", "C", "G", "D", "A"], "F#": ["F", "C", "G", "D", "A", "E"], "C#": ["F", "C", "G", "D", "A", "E", "B"], "F": ["B"], "Bb": ["B", "E"], "Eb": ["B", "E", "A"], "Ab": ["B", "E", "A", "D"], "Db": ["B", "E", "A", "D", "G"], "Gb": ["B", "E", "A", "D", "G", "C"], "Cb": ["B", "E", "A", "D", "G", "C", "F"], "Ahm": [], "Ehm": ["F"], "Bhm": ["F", "C"], "F#hm": ["F", "C", "G"], "C#hm": ["F", "C", "G", "D"], "G#hm": ["F", "C", "G", "D", "A"], "D#hm": ["F", "C", "G", "D", "A", "E"], "A#hm": ["F", "C", "G", "D", "A", "E", "B"], "Dhm": ["B"], "Ghm": ["B", "E"], "Chm": ["B", "E", "A"], "Fhm": ["B", "E", "A", "D"], "Bbhm": ["B", "E", "A", "D", "G"], "Ebhm": ["B", "E", "A", "D", "G", "C"], "Abhm": ["B", "E", "A", "D", "G", "C", "F"], "Amm": [], "Emm": ["F"], "Bmm": ["F", "C"], "F#mm": ["F", "C", "G"], "C#mm": ["F", "C", "G", "D"], "G#mm": ["F", "C", "G", "D", "A"], "D#mm": ["F", "C", "G", "D", "A", "E"], "A#mm": ["F", "C", "G", "D", "A", "E", "B"], "Dmm": ["B"], "Gmm": ["B", "E"], "Cmm": ["B", "E", "A"], "Fmm": ["B", "E", "A", "D"], "Bbmm": ["B", "E", "A", "D", "G"], "Ebmm": ["B", "E", "A", "D", "G", "C"], "Abmm": ["B", "E", "A", "D", "G", "C", "F"]} now I call the class with v=Variables() now the same dict is v.Key_SIGNATURES sharps_or_flats = v.KEY_SIGNATURES[key] my questions: which is better to use and why? Do both remain in memory(separately of course) till I exit the program once I run it? Will memory usage be the same? will the creation of the dict take the same time in both above? On Fri, Jun 13, 2014 at 10:44 PM, Albert-Jan Roskam < fomcl at yahoo.com.dmarc.invalid> wrote: > > > ----- Original Message ----- > > > From: Steven D'Aprano > > To: tutor at python.org > > Cc: > > Sent: Friday, June 13, 2014 3:08 PM > > Subject: Re: [Tutor] global variables/constants versus volatile > variables/constants > > > > On Fri, Jun 13, 2014 at 05:10:28AM -0700, Albert-Jan Roskam wrote: > > > >> The other day I used collections.namedtuple and I re-initialized > >> Record (see below) with every function*) call. Bad idea! It looks > >> nicer because I did not need a global (and globals are baaad, mkay?), > >> but it was *much* slower. I processed a log of a few million lines, I > >> think. > >> > >> # bad --> time-consuming > >> import collections > >> > >> def do_something_with(raw_record): > >> Record = collections.namedtuple("_", " > > ".join("v%%03d" % i for i in range(100))) > >> return Record(*raw_record.split()) > > > > Look at how much work you do here. First, you create a long string of > > the form: > > > > "v000 v001 v002 v003 ... v099" > > > > representing 1000 v-digits names. Then you create a brand new Record > > class that takes those 100 v-digits names as arguments. Creating that > > class requires building a string, parsing it as Python code, and then > > running it. (You're not expected to know that, but if you read the > > source code for namedtuple you will see that's how it works.) So > > creating that class is slow. Every time you call the function, it builds > > a new "v000 ... v099" string, from scratch, then builds a new class, > > also from scratch, and finally populates an instance of that class with > > 100 values from the raw_record. > > > > Only that last step needs to be done inside the function. > > Hmm, if I create the namedtuple with 'verbose=True' it *really* makes > clear why it took so much longer. > (what's '_property' btw? I know 'property' and the newer decorator wih the > same name, but not _property). > > In [1]: import collections > > In [2]: Record = collections.namedtuple("_", " ".join("v%03d" % i for i in > range(100)),verbose=True) > class _(tuple): > '_(v000, v001, v002, v003, v004, v005, v006, v007, v008, v009, v010, > v011, v012, v013, v014, v015, v016, v017, v018, v019, v020, v021, v022, > v023, v024, v025, v026, v027, v028, v029, v030, v031, v032, v033, v034, > v035, v036, v037, v038, v039, v040, v041, v042, v043, v044, v045, v046, > v047, v048, v049, v050, v051, v052, v053, v054, v055, v056, v057, v058, > v059, v060, v061, v062, v063, v064, v065, v066, v067, v068, v069, v070, > v071, v072, v073, v074, v075, v076, v077, v078, v079, v080, v081, v082, > v083, v084, v085, v086, v087, v088, v089, v090, v091, v092, v093, v094, > v095, v096, v097, v098, v099)' > > __slots__ = () > > _fields = ('v000', 'v001', 'v002', 'v003', 'v004', 'v005', 'v006', > 'v007', 'v008', 'v009', 'v010', 'v011', 'v012', 'v013', 'v014', 'v015', > 'v016', 'v017', 'v018', 'v019', 'v020', 'v021', 'v022', 'v023', 'v024', > 'v025', 'v026', 'v027', 'v028', 'v029', 'v030', 'v031', 'v032', 'v033', > 'v034', 'v035', 'v036', 'v037', 'v038', 'v039', 'v040', 'v041', 'v042', > 'v043', 'v044', 'v045', 'v046', 'v047', 'v048', 'v049', 'v050', 'v051', > 'v052', 'v053', 'v054', 'v055', 'v056', 'v057', 'v058', 'v059', 'v060', > 'v061', 'v062', 'v063', 'v064', 'v065', 'v066', 'v067', 'v068', 'v069', > 'v070', 'v071', 'v072', 'v073', 'v074', 'v075', 'v076', 'v077', 'v078', > 'v079', 'v080', 'v081', 'v082', 'v083', 'v084', 'v085', 'v086', 'v087', > 'v088', 'v089', 'v090', 'v091', 'v092', 'v093', 'v094', 'v095', 'v096', > 'v097', 'v098', 'v099') > > def __new__(_cls, v000, v001, v002, v003, v004, v005, v006, v007, > v008, v009, v010, v011, v012, v013, v014, v015, v016, v017, v018, v019, > v020, v021, v022, v023, v024, v025, v026, v027, v028, v029, v030, v031, > v032, v033, v034, v035, v036, v037, v038, v039, v040, v041, v042, v043, > v044, v045, v046, v047, v048, v049, v050, v051, v052, v053, v054, v055, > v056, v057, v058, v059, v060, v061, v062, v063, v064, v065, v066, v067, > v068, v069, v070, v071, v072, v073, v074, v075, v076, v077, v078, v079, > v080, v081, v082, v083, v084, v085, v086, v087, v088, v089, v090, v091, > v092, v093, v094, v095, v096, v097, v098, v099): > 'Create new instance of _(v000, v001, v002, v003, v004, v005, > v006, v007, v008, v009, v010, v011, v012, v013, v014, v015, v016, v017, > v018, v019, v020, v021, v022, v023, v024, v025, v026, v027, v028, v029, > v030, v031, v032, v033, v034, v035, v036, v037, v038, v039, v040, v041, > v042, v043, v044, v045, v046, v047, v048, v049, v050, v051, v052, v053, > v054, v055, v056, v057, v058, v059, v060, v061, v062, v063, v064, v065, > v066, v067, v068, v069, v070, v071, v072, v073, v074, v075, v076, v077, > v078, v079, v080, v081, v082, v083, v084, v085, v086, v087, v088, v089, > v090, v091, v092, v093, v094, v095, v096, v097, v098, v099)' > return _tuple.__new__(_cls, (v000, v001, v002, v003, v004, v005, > v006, v007, v008, v009, v010, v011, v012, v013, v014, v015, v016, v017, > v018, v019, v020, v021, v022, v023, v024, v025, v026, v027, v028, v029, > v030, v031, v032, v033, v034, v035, v036, v037, v038, v039, v040, v041, > v042, v043, v044, v045, v046, v047, v048, v049, v050, v051, v052, v053, > v054, v055, v056, v057, v058, v059, v060, v061, v062, v063, v064, v065, > v066, v067, v068, v069, v070, v071, v072, v073, v074, v075, v076, v077, > v078, v079, v080, v081, v082, v083, v084, v085, v086, v087, v088, v089, > v090, v091, v092, v093, v094, v095, v096, v097, v098, v099)) > > @classmethod > def _make(cls, iterable, new=tuple.__new__, len=len): > 'Make a new _ object from a sequence or iterable' > result = new(cls, iterable) > if len(result) != 100: > raise TypeError('Expected 100 arguments, got %d' % len(result)) > return result > > def __repr__(self): > 'Return a nicely formatted representation string' > return '_(v000=%r, v001=%r, v002=%r, v003=%r, v004=%r, v005=%r, > v006=%r, v007=%r, v008=%r, v009=%r, v010=%r, v011=%r, v012=%r, v013=%r, > v014=%r, v015=%r, v016=%r, v017=%r, v018=%r, v019=%r, v020=%r, v021=%r, > v022=%r, v023=%r, v024=%r, v025=%r, v026=%r, v027=%r, v028=%r, v029=%r, > v030=%r, v031=%r, v032=%r, v033=%r, v034=%r, v035=%r, v036=%r, v037=%r, > v038=%r, v039=%r, v040=%r, v041=%r, v042=%r, v043=%r, v044=%r, v045=%r, > v046=%r, v047=%r, v048=%r, v049=%r, v050=%r, v051=%r, v052=%r, v053=%r, > v054=%r, v055=%r, v056=%r, v057=%r, v058=%r, v059=%r, v060=%r, v061=%r, > v062=%r, v063=%r, v064=%r, v065=%r, v066=%r, v067=%r, v068=%r, v069=%r, > v070=%r, v071=%r, v072=%r, v073=%r, v074=%r, v075=%r, v076=%r, v077=%r, > v078=%r, v079=%r, v080=%r, v081=%r, v082=%r, v083=%r, v084=%r, v085=%r, > v086=%r, v087=%r, v088=%r, v089=%r, v090=%r, v091=%r, v092=%r, v093=%r, > v094=%r, v095=%r, v096=%r, v097=%r, v098=%r, v099=%r)' % self > > def _asdict(self): > 'Return a new OrderedDict which maps field names to their values' > return OrderedDict(zip(self._fields, self)) > > __dict__ = property(_asdict) > > def _replace(_self, **kwds): > 'Return a new _ object replacing specified fields with new values' > result = _self._make(map(kwds.pop, ('v000', 'v001', 'v002', > 'v003', 'v004', 'v005', 'v006', 'v007', 'v008', 'v009', 'v010', 'v011', > 'v012', 'v013', 'v014', 'v015', 'v016', 'v017', 'v018', 'v019', 'v020', > 'v021', 'v022', 'v023', 'v024', 'v025', 'v026', 'v027', 'v028', 'v029', > 'v030', 'v031', 'v032', 'v033', 'v034', 'v035', 'v036', 'v037', 'v038', > 'v039', 'v040', 'v041', 'v042', 'v043', 'v044', 'v045', 'v046', 'v047', > 'v048', 'v049', 'v050', 'v051', 'v052', 'v053', 'v054', 'v055', 'v056', > 'v057', 'v058', 'v059', 'v060', 'v061', 'v062', 'v063', 'v064', 'v065', > 'v066', 'v067', 'v068', 'v069', 'v070', 'v071', 'v072', 'v073', 'v074', > 'v075', 'v076', 'v077', 'v078', 'v079', 'v080', 'v081', 'v082', 'v083', > 'v084', 'v085', 'v086', 'v087', 'v088', 'v089', 'v090', 'v091', 'v092', > 'v093', 'v094', 'v095', 'v096', 'v097', 'v098', 'v099'), _self)) > if kwds: > raise ValueError('Got unexpected field names: %r' % > kwds.keys()) > return result > > def __getnewargs__(self): > 'Return self as a plain tuple. Used by copy and pickle.' > return tuple(self) > > v000 = _property(_itemgetter(0), doc='Alias for field number 0') > > v001 = _property(_itemgetter(1), doc='Alias for field number 1') > > v002 = _property(_itemgetter(2), doc='Alias for field number 2') > > v003 = _property(_itemgetter(3), doc='Alias for field number 3') > > v004 = _property(_itemgetter(4), doc='Alias for field number 4') > > v005 = _property(_itemgetter(5), doc='Alias for field number 5') > > v006 = _property(_itemgetter(6), doc='Alias for field number 6') > > v007 = _property(_itemgetter(7), doc='Alias for field number 7') > > v008 = _property(_itemgetter(8), doc='Alias for field number 8') > > v009 = _property(_itemgetter(9), doc='Alias for field number 9') > > v010 = _property(_itemgetter(10), doc='Alias for field number 10') > > v011 = _property(_itemgetter(11), doc='Alias for field number 11') > > v012 = _property(_itemgetter(12), doc='Alias for field number 12') > > v013 = _property(_itemgetter(13), doc='Alias for field number 13') > > v014 = _property(_itemgetter(14), doc='Alias for field number 14') > > v015 = _property(_itemgetter(15), doc='Alias for field number 15') > > v016 = _property(_itemgetter(16), doc='Alias for field number 16') > > v017 = _property(_itemgetter(17), doc='Alias for field number 17') > > v018 = _property(_itemgetter(18), doc='Alias for field number 18') > > v019 = _property(_itemgetter(19), doc='Alias for field number 19') > > v020 = _property(_itemgetter(20), doc='Alias for field number 20') > > v021 = _property(_itemgetter(21), doc='Alias for field number 21') > > v022 = _property(_itemgetter(22), doc='Alias for field number 22') > > v023 = _property(_itemgetter(23), doc='Alias for field number 23') > > v024 = _property(_itemgetter(24), doc='Alias for field number 24') > > v025 = _property(_itemgetter(25), doc='Alias for field number 25') > > v026 = _property(_itemgetter(26), doc='Alias for field number 26') > > v027 = _property(_itemgetter(27), doc='Alias for field number 27') > > v028 = _property(_itemgetter(28), doc='Alias for field number 28') > > v029 = _property(_itemgetter(29), doc='Alias for field number 29') > > v030 = _property(_itemgetter(30), doc='Alias for field number 30') > > v031 = _property(_itemgetter(31), doc='Alias for field number 31') > > v032 = _property(_itemgetter(32), doc='Alias for field number 32') > > v033 = _property(_itemgetter(33), doc='Alias for field number 33') > > v034 = _property(_itemgetter(34), doc='Alias for field number 34') > > v035 = _property(_itemgetter(35), doc='Alias for field number 35') > > v036 = _property(_itemgetter(36), doc='Alias for field number 36') > > v037 = _property(_itemgetter(37), doc='Alias for field number 37') > > v038 = _property(_itemgetter(38), doc='Alias for field number 38') > > v039 = _property(_itemgetter(39), doc='Alias for field number 39') > > v040 = _property(_itemgetter(40), doc='Alias for field number 40') > > v041 = _property(_itemgetter(41), doc='Alias for field number 41') > > v042 = _property(_itemgetter(42), doc='Alias for field number 42') > > v043 = _property(_itemgetter(43), doc='Alias for field number 43') > > v044 = _property(_itemgetter(44), doc='Alias for field number 44') > > v045 = _property(_itemgetter(45), doc='Alias for field number 45') > > v046 = _property(_itemgetter(46), doc='Alias for field number 46') > > v047 = _property(_itemgetter(47), doc='Alias for field number 47') > > v048 = _property(_itemgetter(48), doc='Alias for field number 48') > > v049 = _property(_itemgetter(49), doc='Alias for field number 49') > > v050 = _property(_itemgetter(50), doc='Alias for field number 50') > > v051 = _property(_itemgetter(51), doc='Alias for field number 51') > > v052 = _property(_itemgetter(52), doc='Alias for field number 52') > > v053 = _property(_itemgetter(53), doc='Alias for field number 53') > > v054 = _property(_itemgetter(54), doc='Alias for field number 54') > > v055 = _property(_itemgetter(55), doc='Alias for field number 55') > > v056 = _property(_itemgetter(56), doc='Alias for field number 56') > > v057 = _property(_itemgetter(57), doc='Alias for field number 57') > > v058 = _property(_itemgetter(58), doc='Alias for field number 58') > > v059 = _property(_itemgetter(59), doc='Alias for field number 59') > > v060 = _property(_itemgetter(60), doc='Alias for field number 60') > > v061 = _property(_itemgetter(61), doc='Alias for field number 61') > > v062 = _property(_itemgetter(62), doc='Alias for field number 62') > > v063 = _property(_itemgetter(63), doc='Alias for field number 63') > > v064 = _property(_itemgetter(64), doc='Alias for field number 64') > > v065 = _property(_itemgetter(65), doc='Alias for field number 65') > > v066 = _property(_itemgetter(66), doc='Alias for field number 66') > > v067 = _property(_itemgetter(67), doc='Alias for field number 67') > > v068 = _property(_itemgetter(68), doc='Alias for field number 68') > > v069 = _property(_itemgetter(69), doc='Alias for field number 69') > > v070 = _property(_itemgetter(70), doc='Alias for field number 70') > > v071 = _property(_itemgetter(71), doc='Alias for field number 71') > > v072 = _property(_itemgetter(72), doc='Alias for field number 72') > > v073 = _property(_itemgetter(73), doc='Alias for field number 73') > > v074 = _property(_itemgetter(74), doc='Alias for field number 74') > > v075 = _property(_itemgetter(75), doc='Alias for field number 75') > > v076 = _property(_itemgetter(76), doc='Alias for field number 76') > > v077 = _property(_itemgetter(77), doc='Alias for field number 77') > > v078 = _property(_itemgetter(78), doc='Alias for field number 78') > > v079 = _property(_itemgetter(79), doc='Alias for field number 79') > > v080 = _property(_itemgetter(80), doc='Alias for field number 80') > > v081 = _property(_itemgetter(81), doc='Alias for field number 81') > > v082 = _property(_itemgetter(82), doc='Alias for field number 82') > > v083 = _property(_itemgetter(83), doc='Alias for field number 83') > > v084 = _property(_itemgetter(84), doc='Alias for field number 84') > > v085 = _property(_itemgetter(85), doc='Alias for field number 85') > > v086 = _property(_itemgetter(86), doc='Alias for field number 86') > > v087 = _property(_itemgetter(87), doc='Alias for field number 87') > > v088 = _property(_itemgetter(88), doc='Alias for field number 88') > > v089 = _property(_itemgetter(89), doc='Alias for field number 89') > > v090 = _property(_itemgetter(90), doc='Alias for field number 90') > > v091 = _property(_itemgetter(91), doc='Alias for field number 91') > > v092 = _property(_itemgetter(92), doc='Alias for field number 92') > > v093 = _property(_itemgetter(93), doc='Alias for field number 93') > > v094 = _property(_itemgetter(94), doc='Alias for field number 94') > > v095 = _property(_itemgetter(95), doc='Alias for field number 95') > > v096 = _property(_itemgetter(96), doc='Alias for field number 96') > > v097 = _property(_itemgetter(97), doc='Alias for field number 97') > > v098 = _property(_itemgetter(98), doc='Alias for field number 98') > > v099 = _property(_itemgetter(99), doc='Alias for field number 99') > > > > > > > > Global variables aren't bad because Moses came down from the mountains > > with a stone tablet that declares that they are bad. They're bad because > > they cause excessive coupling, they operate by side-effect, they spoil > > idepotent code, and they are implicit instead of explicit. > > > LOL :-) Textbooks conditioned me to have a generalized fear of globals. > Like Little Albert: http://en.wikipedia.org/wiki/Little_Albert_experiment > :-) > > > > > >> def do_something_with(raw_record): > >> return Record(*raw_record.split()) > > > > Much more sensible! > > > > > > > > > > -- > > Steven > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Diliup Gabadamudalige http://www.diliupg.com http://soft.diliupg.com/ ********************************************************************************************** This e-mail is confidential. It may also be legally privileged. If you are not the intended recipient or have received it in error, please delete it and all copies from your system and notify the sender immediately by return e-mail. Any unauthorized reading, reproducing, printing or further dissemination of this e-mail or its contents is strictly prohibited and may be unlawful. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions. ********************************************************************************************** -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Jun 14 01:05:46 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 14 Jun 2014 00:05:46 +0100 Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: References: <20140613114524.GG10355@ando> <1402661428.72896.YahooMailNeo@web163805.mail.gq1.yahoo.com> <20140613130840.GH10355@ando> <1402679686.52228.YahooMailNeo@web163804.mail.gq1.yahoo.com> Message-ID: On 13/06/14 19:20, diliup gabadamudalige wrote: > I declare the following dict at the beginning of the program. > > KEY_SIGNATURES = {"C": [], "G": ["F"], "D": ["F", "C"], "A": ["F", "C", > "G"], "E": ["F", "C", "G", "D"], "B": ["F", "C", "G", "D", "A"], "F#": > > now in the functions when ever I need to access i just use it. > > eg: > > sharps_or_flats = KEY_SIGNATURES[key] > > or I can create it like this > > class Variables(): > def __init__(self, adv): > self.KEY_SIGNATURES = {"C": [], "G": ["F"], > > now I call the class with > > v=Variables() > > now the same dict is > v.Key_SIGNATURES > > my questions: > which is better to use and why? As always there is no absolute answer. It depends on what you are doing in a wider context. However from your message you may be missing the point of the class solution. The pointy of a class is to bring together the variables *and the functions* that operate on those variables. So you should not be doing v.Key_SIGNATURES from functions outside the class, rather you should be defining your functions as methods of the class and inside those methods using self.Key_SIGNATURES Also a class called 'variables' is usually a bad design smell. The class should be called something meaningful in the context of your program. Something that represents the data and operations that you have captured within it. If you discover you have captured two unrelated concepts then split the class into two. That will encourage reuse but also make your code much more readable. > Do both remain in memory(separately of course) till I exit the program > once I run it? Will memory usage be the same? For data this small what happens in memory should be the least of your worries. Its much more important to get a design that is readable, maintainable and reliable. Only when you have proved there is a memory issue to solve do you need to start worrying about it. > will the creation of the dict take the same time in both above? But if you are really worried about it use the timeit module or the profiler to find out, don't guess or rely on others guesses. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Sat Jun 14 03:21:38 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 14 Jun 2014 11:21:38 +1000 Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: References: <20140613114524.GG10355@ando> <1402661428.72896.YahooMailNeo@web163805.mail.gq1.yahoo.com> <20140613130840.GH10355@ando> Message-ID: <20140614012138.GJ10355@ando> On Fri, Jun 13, 2014 at 09:42:34PM +0530, diliup gabadamudalige wrote: > Thank you all for these elaborate explanations. > so would it be wrong to assume that, > 1. the list or dict if 'large' and not mutated is better declared once in a > Class and used throughout the program? There is *no* advantage to writing this: class Foo: data = ['a', 'large', 'list'] result = function(Foo.data) instead of this: data = ['a', 'large', 'list'] result = function(data) Just dumping things into a class is not very helpful. You should ask yourself: * Does my code represent a "thing", like a string, or a web server, or a customer record? Then a class might be a good solution. * Does this thing have data and functions that go together? For example, a customer record might have data like Account Balance, and a function like update_account_balance. Then a class might be a good solution. * Could I have more than one of these "things" at the same time? For example, you obviously could have more than one string or customer record. Then a class is probably a good solution. If none of these things are true, then a class is probably a bad solution. > or > 2. Lists that are read only are better off being tuples? Possibly. It depends. As a general rule, tuples may be used for heterogeneous data, lists should be used for homogeneous data. What do I mean by this? This is a good use for a tuple: customer = (name, address, phone_number, status, balance) You have a fixed number of fields, and each field means something different (they are hetrogeneous). You should not use a list for something like this. This is a good use for a list: [value, another_value, a_third_value, ...] There is an arbitrary number of items, and each item represents the same kind of thing (they are homogeneous). Don't think of types, like "all the items are floats", but instead think "all the items are a person's height". As a micro-optimization, you might treat tuple as a frozen-list. That's not so unusual that you will confuse people by it, but it is a little unusual, and a matter of personal taste. > or > 3.the list or dict if 'large' and not mutated is better declared once as a > global variable and used throughout the program? If it's not mutated, it isn't really a *variable* is it? But yes, I would consider this the simplest thing that can work. This would be my preference. > The variables, lists carrying variables, and dict. carrying variables are > declared as a class. So why not include all above in the same class? > Varables and constans which are used throughout the program in one class > declared beofr the program enters the main executing loop? > wouldn't this be fast and the memory usage static up to a point (for all > declared items in the variables class)? I'm afraid I don't understand this paragraph. Can you try explaining more carefully please? (By the way: please trim unnecessary quoted text. Your email contained dozens of lines of double-quoted > > comments that had nothing to do with the questions you asked. If the comments aren't relevant, please delete them. Thank you.) -- Steven From steve at pearwood.info Sat Jun 14 03:42:24 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 14 Jun 2014 11:42:24 +1000 Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: <1402679686.52228.YahooMailNeo@web163804.mail.gq1.yahoo.com> References: <20140613114524.GG10355@ando> <1402661428.72896.YahooMailNeo@web163805.mail.gq1.yahoo.com> <20140613130840.GH10355@ando> <1402679686.52228.YahooMailNeo@web163804.mail.gq1.yahoo.com> Message-ID: <20140614014224.GK10355@ando> Albert-Jan, I've been meaning to ask for a long time... I don't suppose you're hitting "Forward" rather than "Reply" in your posts are you? Because I've never seen replies from anyone else use the same style as your replies. Further comments below. On Fri, Jun 13, 2014 at 10:14:46AM -0700, Albert-Jan Roskam wrote: > Hmm, if I create the namedtuple with 'verbose=True' it *really* makes clear why it took so much longer. > (what's '_property' btw? I know 'property' and the newer decorator wih the same name, but not _property). _property is just property renamed. If you read the source code to namedtuple, you will see something like the line: _property = property or possibly: from builtins import property as _property That's likely done to make it a local variable, and so a little faster (although I don't know why the author bothered, it will make so little difference), or possibly to prevent clashes in case the caller defines a field name called "property". I'm not sure, but they are my guesses. [...] > ??? v000 = _property(_itemgetter(0), doc='Alias for field number 0') > > ??? v001 = _property(_itemgetter(1), doc='Alias for field number 1') > > ??? v002 = _property(_itemgetter(2), doc='Alias for field number 2') [snip 97 more lines] Yes, thank you, we get the picture. No need to show 100 examples. -- Steven From cs at zip.com.au Sat Jun 14 07:35:52 2014 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 14 Jun 2014 15:35:52 +1000 Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: <20140614012138.GJ10355@ando> References: <20140614012138.GJ10355@ando> Message-ID: <20140614053552.GA87104@cskk.homeip.net> On 14Jun2014 11:21, Steven D'Aprano wrote: >On Fri, Jun 13, 2014 at 09:42:34PM +0530, diliup gabadamudalige wrote: >> 2. Lists that are read only are better off being tuples? > >Possibly. It depends. > >As a general rule, tuples may be used for heterogeneous data, lists >should be used for homogeneous data. To make this less clear, I would say this is a useful rule some of the time. It certainly isn't the only criterion one might use for choosing between a tuple and a list. >What do I mean by this? > >This is a good use for a tuple: > customer = (name, address, phone_number, status, balance) > >You have a fixed number of fields, and each field means something >different (they are hetrogeneous). You should not use a list for >something like this. Were I using the "heterogeneous data" rule I would be advocaing going to whole hog and using a "namedtuple" from the "collections" module. This has the advantage of giving each element of the tuple a nice name so that you can refer to "customer.address" instead of using magic indices like "customer[1]". That makes the code easier to read and easier to write. But if you're not using namedtuple-like attributes to access the tuple elements, then I would not be considering the "heterogeneous data" rule to be as relevant in my choice of tuple or list. There are some advantages to using a tuple as a frozen list, which can't have its values modified nor have its size changed. Specificly, if that is your intent and your code later tried to change the values (because it is buggy) then Python will raise an exception immediately, making it easier to find your mistake. If you stick to a list for this situation then the buggy changes will go ahead and the error will only be apparent much later when you get the wrong results. Fail early, fail often! Conversely, there are downsides to using tuples as frozen lists. The most obvious is that you can't add lists and tuples together, just lists and lists. If you need to add tuples and lists all the time then the surround fiddliness required may be enough to push you back to using lists throughout. Finally, another argument against mixing tuples and lists is simply that it may be early optimisation. Unless you're benefiting from the "you can't accidentally modify a tuple later" effect, just write it all with lists; it will be simpler. You can always come back later after the code is functioning correctly and think about tuples. Cheers, Cameron Simpson From diliupg at gmail.com Sat Jun 14 14:53:08 2014 From: diliupg at gmail.com (diliup gabadamudalige) Date: Sat, 14 Jun 2014 18:23:08 +0530 Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: <20140614053552.GA87104@cskk.homeip.net> References: <20140614012138.GJ10355@ando> <20140614053552.GA87104@cskk.homeip.net> Message-ID: Thank you all for those great clarifications. I learnt a lot from these. My roots in programming are from the early 80s and with a gap of nearly 30 years till I restarted again last year in March with Python. :) So some of the new concepts like classes are a bit alien to me but I am catching on. I made a mistake by using the word variables on variables and constants both. My design structure would most probably be quite unconventional to most like Allen Gauld says. I am still learning and I like to jump into the deep end and learn to swim there and so far have managed to stay alive! :) I have gained immensely from these few emails and hope I can gain more knowledge to write better code. Say if I have a lot of Lists, strings and variables used to carry data to and from various functions why can't I have a class with all these in it? I thought on the lines of blood carrying various things to different organs in the body. One common container class. Containers for data exchange between all classes and functions. so I declare class Containers(): def __init__(self): self.last_selected_scale= "" self.scale_notes = "" self.arpeggionotes = "" self.handplayed_notes = [] self.MIDIscale = [] # the MIDI scale to be played is here self.play_MIDIscale = False self.play_MIDIarpeggio = False self.play_MIDI_scale_note = False self.play_MIDI_arpeggio_note = False self.MIDInote_inscale = 0 now i initiate the class with v=Containers() now say if I play the MIDI keyboard, I collect the notes played which will be appended to the list v.hanplayednotes. This will be done from a function which will scan the keyboard and append played notes to that list. This is how I have used these variables, lists and strings in this program. So far it has run error free. Is this not correct or bad design. I know it is not the normal way of using class but is it wrong? Thank you all in advance. -- Diliup Gabadamudalige http://www.diliupg.com http://soft.diliupg.com/ ********************************************************************************************** This e-mail is confidential. It may also be legally privileged. If you are not the intended recipient or have received it in error, please delete it and all copies from your system and notify the sender immediately by return e-mail. Any unauthorized reading, reproducing, printing or further dissemination of this e-mail or its contents is strictly prohibited and may be unlawful. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions. ********************************************************************************************** -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sat Jun 14 21:33:05 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 15 Jun 2014 05:33:05 +1000 Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: References: <20140614012138.GJ10355@ando> <20140614053552.GA87104@cskk.homeip.net> Message-ID: <20140614193305.GM10355@ando> On Sat, Jun 14, 2014 at 06:23:08PM +0530, diliup gabadamudalige wrote: > Say if I have a lot of Lists, strings and variables used to carry data to > and from various functions why can't I have a class with all these in it? Of course you *can*, but you *should not*. Read on... [...] > so I declare > > class Containers(): > def __init__(self): > self.last_selected_scale= "" > self.scale_notes = "" > self.arpeggionotes = "" etc. > now i initiate the class with > v=Containers() So now you write code where everything you use starts with "v.". What does the v. do? How does it help your programming? Answer: it doesn't. It just makes an extra two characters to type. Compare the code you would write if you just defined the variables directly: # Option 1 last_selected_scale = "" scale_notes = "" arpeggionotes = "" do_something(last_selected_scale) do_another_thing(scale_notes, arpeggionotes) # Versus option 2: class Containers(): def __init__(self): self.last_selected_scale = "" self.scale_notes = "" self.arpeggionotes = "" v = Container() do_something(v.last_selected_scale) do_another_thing(v.scale_notes, v.arpeggionotes) The two options do *exactly* the same thing, but Option 2 takes much more typing and reading. For what benefit? The only benefit is if you might have multiple Containers at the same time: keyboard_a = Container() keyboard_b = Container() do_something(keyboard_a.last_selected_scale) do_another_thing(keyboard_b.scale_notes, keyboard_b.arpeggionotes) Now you can play and record with two keyboards at the same time. (Perhaps you have four arms? *smile*) In this case, the extra cost of using a class pays for itself. Otherwise, it is just wasted effort. -- Steven From stareq13 at yahoo.com Sat Jun 14 21:37:56 2014 From: stareq13 at yahoo.com (S Tareq) Date: Sat, 14 Jun 2014 20:37:56 +0100 (BST) Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: <20140613114524.GG10355@ando> References: <20140613114524.GG10355@ando> Message-ID: <1402774676.70952.YahooMailNeo@web133104.mail.ir2.yahoo.com> ssss On Friday, 13 June 2014, 12:45, Steven D'Aprano wrote: On Fri, Jun 13, 2014 at 12:51:25PM +0530, diliup gabadamudalige wrote: > Hi All! > Hope everyone is well. > > In my code there are many dictionaries and lists which are used in various > functions. Is it better/pythonic/efficient to have these inside the > function itself or declared at the beginning of the program in which case > they will be global? They are all read only. I understand that global > constants and variable have memory allocated to them but when declared > inside a function are created on the fly, used and discarded. Please > enlighten me further on this and correct me if i'm wrong. A good question. Depending on the size of these dictionaries and lists, worrying about efficiency here may be premature optimization. As they say: ? "We should forget about small efficiencies, say about 97% of ? ? the time: premature optimization is the root of all evil." ? ? -- Donald Knuth ? "The First Rule of Program Optimization: Don't do it. The ? ? Second Rule of Program Optimization (for experts only!): ? ? Don't do it yet." -- Michael A. Jackson ? "More computing sins are committed in the name of efficiency ? ? (without necessarily achieving it) than for any other single ? ? reason ? including blind stupidity." -- W.A. Wulf If these lists and dicts are small, say, fewer than a dozen items, the time to create and destroy them is probably trivial, especially if you construct them from constant literals. In that case, it's a matter of personal taste whether you prefer them as global constants or local to a function. But if it takes a long time to build the list, then you definitely should move it outside the function and perform the initialisation step only once: # This may take a while... PRIMES = [prime(i) for i in range(1, 1000001)] If your lists really are read-only, then you should consider turning them into tuples: # Not this: SOME_ITEMS = ["this", "that", "another"] # Better: SOME_ITEMS = ("this", "that", "another") One advantage of the tuple is that in recent versions of Python, the tuple may be compiled as a constant instead of being built at runtime. This is from Python 2.7: py> from dis import dis py> code = compile("x = [2, 4, 8]", "", "exec") py> dis(code) ? 1? ? ? ? ? 0 LOAD_CONST? ? ? ? ? ? ? 0 (2) ? ? ? ? ? ? ? 3 LOAD_CONST? ? ? ? ? ? ? 1 (4) ? ? ? ? ? ? ? 6 LOAD_CONST? ? ? ? ? ? ? 2 (8) ? ? ? ? ? ? ? 9 BUILD_LIST? ? ? ? ? ? ? 3 ? ? ? ? ? ? 12 STORE_NAME? ? ? ? ? ? ? 0 (x) ? ? ? ? ? ? 15 LOAD_CONST? ? ? ? ? ? ? 3 (None) ? ? ? ? ? ? 18 RETURN_VALUE py> code = compile("x = (2, 4, 8)", "", "exec") py> dis(code) ? 1? ? ? ? ? 0 LOAD_CONST? ? ? ? ? ? ? 4 ((2, 4, 8)) ? ? ? ? ? ? ? 3 STORE_NAME? ? ? ? ? ? ? 0 (x) ? ? ? ? ? ? ? 6 LOAD_CONST? ? ? ? ? ? ? 3 (None) ? ? ? ? ? ? ? 9 RETURN_VALUE -- Steven _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From street.sweeper at mailworks.org Sat Jun 14 23:06:14 2014 From: street.sweeper at mailworks.org (street.sweeper at mailworks.org) Date: Sat, 14 Jun 2014 17:06:14 -0400 Subject: [Tutor] python3 equivalent of coreutils stat command Message-ID: <1402779974.26680.128826857.04E26363@webmail.messagingengine.com> With the stat command in GNU coreutils, I can get a file's modification time, with timezone offset. For example, the output of "stat -c %y *" looks like 2014-02-03 14:48:17.000000000 -0200 2014-05-29 19:00:05.000000000 -0100 What I want to do is get the mtime in ISO8601 format, and I've gotten close with os.path.getmtime and os.stat, for example 2014-02-03T14:48:17. But, no timezone offset. coreutils stat can get it, so it must be recorded by the filesystem (ext4 in this case). What do I need to do in python to include this piece of information? Thanks From alan.gauld at btinternet.com Sat Jun 14 23:45:18 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 14 Jun 2014 22:45:18 +0100 Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: References: <20140614012138.GJ10355@ando> <20140614053552.GA87104@cskk.homeip.net> Message-ID: On 14/06/14 13:53, diliup gabadamudalige wrote: > Say if I have a lot of Lists, strings and variables used to carry data > to and from various functions why can't I have a class with all these in > it? You can but it should also have the functions that operate on the data too. Thats the point of classes they link function and data together so you don't need to pass lots of parameters around. If you have a class that jusat holds lots of disparate data items you might as well just use a list/tuple or dictionary. The value of classes is when you add behaviour or methods to the mix. > I thought on the lines of blood carrying various things to different > organs in the body. But even there the blood has a distinct set of related items, it doesn't carry the food to your stomach or the video signals from your eye to your brain. And blood has operations - it can flow, coagulate, increase/decrease red-cell count etc. It has behaviour as well as data. > class Containers(): > def __init__(self): > self.last_selected_scale= "" > self.scale_notes = "" > self.arpeggionotes = "" > self.handplayed_notes = [] > self.MIDIscale = [] # the MIDI scale to be played is here > self.play_MIDIscale = False > self.play_MIDIarpeggio = False > self.play_MIDI_scale_note = False > self.play_MIDI_arpeggio_note = False > self.MIDInote_inscale = 0 Now that could be a Tune... And it might have record/play/pause operations > now say if I play the MIDI keyboard, I collect the notes played which > will be appended to the list v.hanplayednotes. This will be done from a > function which will scan the keyboard and append played notes to that list. But that could be a method of the Tune sub-class, MidiTune, which knows how to record from a MidiSequencer object. > This is how I have used these variables, lists and strings in this > program. So far it has run error free. The issue is not how to make it run - you can do that in assembler. The issue is how easy is it to read and how easy os it to modify and fix later. 80% of the cost of commercial software is in "maintenance" The goal of professional software engineers is to reduce maintenance costs even if that increases the initial 20% development budget. > Is this not correct or bad design. I know it is not the normal way of > using class but is it wrong? Almost certainly, because it adds a layer of complexity for little or no gain. Whereas adding the methods to the class improves the maintainability and readability (and sometimes the performance, although that's a secondary benefit). HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Sat Jun 14 23:52:25 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 14 Jun 2014 22:52:25 +0100 Subject: [Tutor] python3 equivalent of coreutils stat command In-Reply-To: <1402779974.26680.128826857.04E26363@webmail.messagingengine.com> References: <1402779974.26680.128826857.04E26363@webmail.messagingengine.com> Message-ID: On 14/06/14 22:06, street.sweeper at mailworks.org wrote: > With the stat command in GNU coreutils, I can get a file's > modification time, with timezone offset. > gotten close with os.path.getmtime and os.stat, for example > 2014-02-03T14:48:17. But, no timezone offset. os.stat returns the mtime as seconds from the epoch. The time and datetime modules contain functions for converting seconds into local time etc which can show timezone if needed. But be aware of the following caveat from the documentation: ----------- Note: The exact meaning and resolution of the st_atime, st_mtime, and st_ctime attributes depend on the operating system and the file system. For example, on Windows systems using the FAT or FAT32 file systems, st_mtime has 2-second resolution, and st_atime has only 1-day resolution. See your operating system documentation for details. ... ------------------- -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From diliupg at gmail.com Sun Jun 15 06:56:57 2014 From: diliupg at gmail.com (diliup gabadamudalige) Date: Sun, 15 Jun 2014 10:26:57 +0530 Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: References: <20140614012138.GJ10355@ando> <20140614053552.GA87104@cskk.homeip.net> Message-ID: Thank you very much Allan! 100% clear. I thank each and every one of you who contributed to all my question on the above subject. A lot of things became very clear. May you all be well. On Sun, Jun 15, 2014 at 3:15 AM, Alan Gauld wrote: > On 14/06/14 13:53, diliup gabadamudalige wrote: > > Say if I have a lot of Lists, strings and variables used to carry data >> to and from various functions why can't I have a class with all these in >> it? >> > > You can but it should also have the functions that operate on the data > too. Thats the point of classes they link function and data together > so you don't need to pass lots of parameters around. If you have a class > that jusat holds lots of disparate data items you might as well just use a > list/tuple or dictionary. The value of classes is when you add behaviour or > methods to the mix. > > > > I thought on the lines of blood carrying various things to different >> organs in the body. >> > > But even there the blood has a distinct set of related items, it doesn't > carry the food to your stomach or the video signals from > your eye to your brain. And blood has operations - it can flow, > coagulate, increase/decrease red-cell count etc. It has > behaviour as well as data. > > > class Containers(): >> def __init__(self): >> self.last_selected_scale= "" >> self.scale_notes = "" >> self.arpeggionotes = "" >> self.handplayed_notes = [] >> self.MIDIscale = [] # the MIDI scale to be played is here >> self.play_MIDIscale = False >> self.play_MIDIarpeggio = False >> self.play_MIDI_scale_note = False >> self.play_MIDI_arpeggio_note = False >> self.MIDInote_inscale = 0 >> > > Now that could be a Tune... And it might have record/play/pause operations > > > > now say if I play the MIDI keyboard, I collect the notes played which >> will be appended to the list v.hanplayednotes. This will be done from a >> function which will scan the keyboard and append played notes to that >> list. >> > > But that could be a method of the Tune sub-class, MidiTune, which knows > how to record from a MidiSequencer object. > > > This is how I have used these variables, lists and strings in this >> program. So far it has run error free. >> > > The issue is not how to make it run - you can do that in assembler. > The issue is how easy is it to read and how easy os it to modify and fix > later. 80% of the cost of commercial software is in "maintenance" > The goal of professional software engineers is to reduce maintenance > costs even if that increases the initial 20% development budget. > > > Is this not correct or bad design. I know it is not the normal way of >> using class but is it wrong? >> > > Almost certainly, because it adds a layer of complexity for little or no > gain. > > Whereas adding the methods to the class improves the maintainability and > readability (and sometimes the performance, although that's a secondary > benefit). > > HTH > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Diliup Gabadamudalige http://www.diliupg.com http://soft.diliupg.com/ ********************************************************************************************** This e-mail is confidential. It may also be legally privileged. If you are not the intended recipient or have received it in error, please delete it and all copies from your system and notify the sender immediately by return e-mail. Any unauthorized reading, reproducing, printing or further dissemination of this e-mail or its contents is strictly prohibited and may be unlawful. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions. ********************************************************************************************** -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Sun Jun 15 12:41:42 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 15 Jun 2014 11:41:42 +0100 Subject: [Tutor] global variables/constants versus volatile variables/constants In-Reply-To: <1402774676.70952.YahooMailNeo@web133104.mail.ir2.yahoo.com> References: <20140613114524.GG10355@ando> <1402774676.70952.YahooMailNeo@web133104.mail.ir2.yahoo.com> Message-ID: On 14/06/2014 20:37, S Tareq wrote: > ssss > You appear to be having problems with your keyboard. Could you please resend your message, stating precisely what you wanted to say. Thank you. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From aaronmisquith at gmail.com Tue Jun 17 09:30:56 2014 From: aaronmisquith at gmail.com (Aaron Misquith) Date: Tue, 17 Jun 2014 13:00:56 +0530 Subject: [Tutor] Unicode Encode Error Message-ID: I'm trying to obtain the questions present in StackOverflow for a particular tag. Whenever I try to run the program i get this *error:* Message File Name Line Position Traceback C:\Users\Aaron\Desktop\question.py 20 UnicodeEncodeError: 'ascii' codec can't encode character u'\u201c' in position 34: ordinal not in range(128) *This is the code:* import stackexchange import sys sys.path.append('.') so = stackexchange.Site(stackexchange.StackOverflow) term= raw_input("Enter the keyword") print 'Searching for %s...' % term, sys.stdout.flush() qs = so.search(intitle=term) for q in qs: print '%8d %s' % (q.id, q.title) with open('D:\ques.txt', 'a+') as question: question.write(q.title) Can anyone explain me what is going wrong here? This program used to run perfectly fine before. Only since yesterday I have started getting this error. -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Tue Jun 17 12:42:43 2014 From: __peter__ at web.de (Peter Otten) Date: Tue, 17 Jun 2014 12:42:43 +0200 Subject: [Tutor] Unicode Encode Error References: Message-ID: Aaron Misquith wrote: > I'm trying to obtain the questions present in StackOverflow for a > particular tag. > > Whenever I try to run the program i get this *error:* > > Message File Name Line Position > Traceback > C:\Users\Aaron\Desktop\question.py 20 > UnicodeEncodeError: 'ascii' codec can't encode character u'\u201c' in > position 34: ordinal not in range(128) > > > *This is the code:* > import stackexchange > import sys > sys.path.append('.') > so = stackexchange.Site(stackexchange.StackOverflow) > term= raw_input("Enter the keyword") > print 'Searching for %s...' % term, > sys.stdout.flush() > qs = so.search(intitle=term) > > for q in qs: > print '%8d %s' % (q.id, q.title) > with open('D:\ques.txt', 'a+') as question: > question.write(q.title) > > Can anyone explain me what is going wrong here? This program used to run > perfectly fine before. Only since yesterday I have started getting this > error. The traceback and the code you post don't fit together as the latter has less than 20 lines. Therefore I have to guess: q.title is probably unicode When you are writing unicode to a file it is automatically converted to bytes assuming the ascii encoding >>> f = open("tmp.txt", "w") >>> f.write(u"abc") However, this fails when the unicode string contains non-ascii characters: >>> f.write(u"???") Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128) So your code "worked" yesterday because the title you retrieved yesterday did not contain any non-ascii chars. The fix is to open the file with an encoding that can cope with the extra characters. UTF-8 is a good choice here. To use that modify your code as follows: import codecs ... with codecs.open(filename, "a", encoding="utf-8") as question: question.write(q.title) PS: personally I'd open the file once outside the loop: with codecs.open(...) as questions_file: for q in qs: print ... questions_file.write(q.title) From keithadu at live.com Tue Jun 17 20:52:33 2014 From: keithadu at live.com (keith papa) Date: Tue, 17 Jun 2014 14:52:33 -0400 Subject: [Tutor] Tips Message-ID: Hi, I want to no what some tips or information you can give me to remember some of the rules of python, when you first start learning programming? From alan.gauld at btinternet.com Tue Jun 17 22:35:33 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 17 Jun 2014 21:35:33 +0100 Subject: [Tutor] Tips In-Reply-To: References: Message-ID: On 17/06/14 19:52, keith papa wrote: > > > Hi, I want to no what some tips or information you can give me to > remember some of the rules of python, > when you first start learning programming? Most of my tips/rules are about programming rather than python... ----------------------------------------------------------------- Use for loops when you know (or can predict) the number of iterations. Use while loops when you don't know in advance Put repeating code in a function Use meaningful variable names. Use dictionaries more often. Let the data define the structure. And structure your data so that it can. And more Python specific: ------------------- Use modules instead of singleton classes Use "if/elif" instead of "if/else if" trees Variables are just names bound to objects Don't test types, use the interface Is that the kind of thing? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From akleider at sonic.net Wed Jun 18 02:02:02 2014 From: akleider at sonic.net (Alex Kleider) Date: Tue, 17 Jun 2014 17:02:02 -0700 Subject: [Tutor] Tips In-Reply-To: References: Message-ID: On 2014-06-17 13:35, Alan Gauld wrote: > Don't test types, use the interface Can you please explain what you mean by this? alex From dyoo at hashcollision.org Wed Jun 18 06:11:40 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 17 Jun 2014 21:11:40 -0700 Subject: [Tutor] Tips In-Reply-To: References: Message-ID: On Tue, Jun 17, 2014 at 5:02 PM, Alex Kleider wrote: > On 2014-06-17 13:35, Alan Gauld wrote: > >> Don't test types, use the interface > > > Can you please explain what you mean by this? If you are writing type tests on a function's inputs, you might better off by having the inputs implement an interface: the design of the program will often be more maintainable. As a toy example, consider the following: ################################### class Football(object): pass class Baseball(object): pass def printBallSize(game): if isinstance(game, Football): print "The ball is 68-70cm" if isinstance(game, Baseball): print "The ball is 229-235mm" raise ValueError ################################### The problem with getBallSize() is that it hardcoded a decision based on what the type of the input is. It's a bit fragile. A more flexible design allows the game itself to provide that information for itself: ################################### class Football(object): def size(self): return "68-70cm" class Baseball(object): def size(self): return "229-235mm" def printBallSize(game): return "The ball is", game.size() ################################### The reason the latter is usually preferable is because additional games can be supported without having to revise printBallSize(). For example, we can write: ################################### class AmericanFootball(object): def size(self): return "68-70cm" ################################### and printBallSize() will work on American footballs as well. printBallSize() works on anything that implements a size() method. The example above is very toy. A more realistic example might be writing a function whose inputs might be assumed to be a sequence. Rather than hardcode a test that explicitly checks whether the input is a list, just use it. Then anything that satisfies the "interface"---the way you're using the input---will often Just Work. Tuples, for example, will do many of the things that lists will do. And in some cases, even a file might look like a list, for all practical purposes, if all we care about is iterating though it once. Hope that makes some sense! From breamoreboy at yahoo.co.uk Wed Jun 18 09:59:31 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 18 Jun 2014 08:59:31 +0100 Subject: [Tutor] Tips In-Reply-To: References: Message-ID: On 18/06/2014 01:02, Alex Kleider wrote: > On 2014-06-17 13:35, Alan Gauld wrote: > >> Don't test types, use the interface > > Can you please explain what you mean by this? > alex > Further to Danny Yoo's response have a read of this http://en.wikipedia.org/wiki/Duck_typing -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From brandonprice1995 at gmail.com Wed Jun 18 07:27:53 2014 From: brandonprice1995 at gmail.com (Brandon Price) Date: Wed, 18 Jun 2014 01:27:53 -0400 Subject: [Tutor] Are These Resources Good Enough To Learn Python 3? Message-ID: Hi, I'm new to programming and I've tried to learn in the past but I gave up easily. I tried learning Java but I feel that Python would be the best route to take since it's easier to learn and then I'll move on to learning Java. I want to learn Python 3.x not Python 2. My goal(s) are: Learn Python within 1 month Questions: 1. Which one of these resources would you recommend I use: http://www.alan-g.me.uk/l2p/index.htm or http://www.python-course.eu/python3_course.php 2. Once I learn Python, what next? -------------- next part -------------- An HTML attachment was scrubbed... URL: From nanohard at bitmessage.ch Wed Jun 18 02:15:30 2014 From: nanohard at bitmessage.ch (Nanohard) Date: Tue, 17 Jun 2014 20:15:30 -0400 Subject: [Tutor] Tips In-Reply-To: References: Message-ID: <53A0DA22.5020706@bitmessage.ch> > On 2014-06-17 13:35, Alan Gauld wrote: > >> Don't test types, use the interface > > Can you please explain what you mean by this? > alex He means use the Python interpreter, by going to your console and typing "python", or in Windows it's called 'IDLE'. From alan.gauld at btinternet.com Wed Jun 18 11:57:13 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 18 Jun 2014 10:57:13 +0100 Subject: [Tutor] Are These Resources Good Enough To Learn Python 3? In-Reply-To: References: Message-ID: On 18/06/14 06:27, Brandon Price wrote: > Hi, > I'm new to programming and I've tried to learn in the past but I gave up > easily. > Learn Python within 1 month That's achievable, at least to a working level. Expert takes a tad longer. > Questions: > 1. Which one of these resources would you recommend I use: > http://www.alan-g.me.uk/l2p/index.htm or > http://www.python-course.eu/python3_course.php OK, I'm biased... But tutorials are very personal. Some people like a lot of explanation about the background and theory. Others like a lot of hands on and don't care about the 'why'. My tutorial focuses on teaching programming in general while using Python as the example language. The other tutorial you cite focuses on teaching Python and as such gives a deeper study of the language at the expense of missing some of the wider issues. both will teach you enough to stat writing your own programs and be able to read and understand the documentation. But the most important thing for you is which one matches your preferred learning style. > 2. Once I learn Python, what next? Use it! Find a project and write some code. If you can't think of one find someone else's project and lend a hand. That will probably require you to learn other new technologies such as HTML, network programming, databases and SQL, graphics, statistics, and so on... Once you start programming the learning never stops. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Wed Jun 18 11:47:35 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 18 Jun 2014 10:47:35 +0100 Subject: [Tutor] Tips In-Reply-To: <53A0DA22.5020706@bitmessage.ch> References: <53A0DA22.5020706@bitmessage.ch> Message-ID: On 18/06/14 01:15, Nanohard wrote: >> On 2014-06-17 13:35, Alan Gauld wrote: >> >>> Don't test types, use the interface >> >> Can you please explain what you mean by this? > > He means use the Python interpreter, by going to your console and typing "python", or in Windows > it's called 'IDLE'. Nope, I meant what Mark and Danny said. For example don't do this: def add(a,b): if type(a) == int and type(b) == int: return a+b else: raise TypeError Just do this: def add(a,b): return a+b And rely on the interpreter to check that a and b can be added. It makes the function much more flexible and reusable. You can now add strings, lists, floats etc, as well as ints -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From s.shall at virginmedia.com Wed Jun 18 12:35:20 2014 From: s.shall at virginmedia.com (Sydney Shall) Date: Wed, 18 Jun 2014 12:35:20 +0200 Subject: [Tutor] Tips In-Reply-To: References: Message-ID: <53A16B68.1070307@virginmedia.com> On 17/06/2014 22:35, Alan Gauld wrote: > Use modules instead of singleton classes As a new beginner with Python, I am having problem understanding the difference here. I think I understand classes, but still have problems with inheritance, but I do not understand what defines a module. With many thanks. -- Sydney Shall -------------- next part -------------- An HTML attachment was scrubbed... URL: From fomcl at yahoo.com Wed Jun 18 16:25:41 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 18 Jun 2014 07:25:41 -0700 Subject: [Tutor] Tips In-Reply-To: References: <53A0DA22.5020706@bitmessage.ch> Message-ID: <1403101541.4326.YahooMailNeo@web163804.mail.gq1.yahoo.com> ----- Original Message ----- > From: Alan Gauld > To: tutor at python.org > Cc: > Sent: Wednesday, June 18, 2014 11:47 AM > Subject: Re: [Tutor] Tips > > On 18/06/14 01:15, Nanohard wrote: >>> On 2014-06-17 13:35, Alan Gauld wrote: >>> >>>> Don't test types, use the interface >>> >>> Can you please explain what you mean by this? >> >> He means use the Python interpreter, by going to your console and typing > "python", or in Windows >> it's called 'IDLE'. > > > Nope, I meant what Mark and Danny said. > > For example don't do this: > > def add(a,b): > ? ? if type(a) == int and type(b) == int: > ? ? ? ? return a+b > ? ? else: > ? ? ? ? raise TypeError > > Just do this: > > def add(a,b): > ? ? return a+b Given that the concept of Ducktyping has already been mentioned, is there a reason why you did not mention try-except? ? def add(a, b): ??? try: ??????? return a + b ??? except TypeError: ????????raise? ? Btw, given that: >>> {}.__add__ Traceback (most recent call last): File "", line 1, in AttributeError: 'dict' object has no attribute '__add__' ? Why does one only need to use 'except TypeError', not 'except (TypeError, AttributeError)'?in the try-except above? >>> {} + 1 Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'dict' and 'int' From alan.gauld at btinternet.com Wed Jun 18 18:31:46 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 18 Jun 2014 17:31:46 +0100 Subject: [Tutor] Tips In-Reply-To: <1403101541.4326.YahooMailNeo@web163804.mail.gq1.yahoo.com> References: <53A0DA22.5020706@bitmessage.ch> <1403101541.4326.YahooMailNeo@web163804.mail.gq1.yahoo.com> Message-ID: On 18/06/14 15:25, Albert-Jan Roskam wrote: >> Just do this: >> >> def add(a,b): >> return a+b > > Given that the concept of Ducktyping has already been mentioned, is there a reason why you did not mention try-except? > > def add(a, b): > try: > return a + b > except TypeError: > raise Because that's a lot of work for no value. Catching an exception simply to raise it again is a pointless exercise. Only catch stuff you intend to process. Of course an add function is a waste of space too since one already exists in the operators module and the + sign is usually all thats needed. But the function was only an example... but try/except is completely orthogonal to the original 'tip' of not checking types. > Why does one only need to use 'except TypeError', > not 'except (TypeError, AttributeError)' in the try-except above? I'm not sure I understand? You created the try/except. You can catch as much or as little as you wish. Leaving it to Python catches both. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From breamoreboy at yahoo.co.uk Wed Jun 18 21:03:09 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 18 Jun 2014 20:03:09 +0100 Subject: [Tutor] Tips In-Reply-To: <1403101541.4326.YahooMailNeo@web163804.mail.gq1.yahoo.com> References: <53A0DA22.5020706@bitmessage.ch> <1403101541.4326.YahooMailNeo@web163804.mail.gq1.yahoo.com> Message-ID: On 18/06/2014 15:25, Albert-Jan Roskam wrote: > ----- Original Message ----- >> From: Alan Gauld >> To: tutor at python.org >> Cc: >> Sent: Wednesday, June 18, 2014 11:47 AM >> Subject: Re: [Tutor] Tips >> >> On 18/06/14 01:15, Nanohard wrote: >>>> On 2014-06-17 13:35, Alan Gauld wrote: >>>> >>>>> Don't test types, use the interface >>>> >>>> Can you please explain what you mean by this? >>> >>> He means use the Python interpreter, by going to your console and typing >> "python", or in Windows >>> it's called 'IDLE'. >> >> >> Nope, I meant what Mark and Danny said. >> >> For example don't do this: >> >> def add(a,b): >> if type(a) == int and type(b) == int: >> return a+b >> else: >> raise TypeError >> >> Just do this: >> >> def add(a,b): >> return a+b > > Given that the concept of Ducktyping has already been mentioned, is there a reason why you did not mention try-except? > > def add(a, b): > try: > return a + b > except TypeError: > raise > > Btw, given that: >>>> {}.__add__ > Traceback (most recent call last): File "", line 1, in AttributeError: 'dict' object has no attribute '__add__' > > Why does one only need to use 'except TypeError', not 'except (TypeError, AttributeError)' in the try-except above? >>>> {} + 1 > Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'dict' and 'int' > What makes you think that you're calling your add function in either example above? In the first you're not calling anything as you've missed the brackets. Even if you add (groan :) them, you'll be trying to call an add method for a dict, not your add function. In the second example, you're trying to add 1 to an empty dict, again your function doesn't enter into the equation (double groan :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From fomcl at yahoo.com Wed Jun 18 21:17:38 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 18 Jun 2014 12:17:38 -0700 Subject: [Tutor] Tips In-Reply-To: References: <53A0DA22.5020706@bitmessage.ch> <1403101541.4326.YahooMailNeo@web163804.mail.gq1.yahoo.com> Message-ID: <1403119058.92985.YahooMailNeo@web163805.mail.gq1.yahoo.com> ----- Original Message ----- > From: Mark Lawrence > To: tutor at python.org > Cc: > Sent: Wednesday, June 18, 2014 9:03 PM > Subject: Re: [Tutor] Tips > > On 18/06/2014 15:25, Albert-Jan Roskam wrote: >> ----- Original Message ----- >>> From: Alan Gauld >>> To: tutor at python.org >>> Cc: >>> Sent: Wednesday, June 18, 2014 11:47 AM >>> Subject: Re: [Tutor] Tips >>> >>> On 18/06/14 01:15, Nanohard wrote: >>>>> ? On 2014-06-17 13:35, Alan Gauld wrote: >>>>> >>>>>> ? Don't test types, use the interface >>>>> >>>>> ? Can you please explain what you mean by this? >>>> >>>> ? He means use the Python interpreter, by going to your console and > typing >>> "python", or in Windows >>>> ? it's called 'IDLE'. >>> >>> >>> Nope, I meant what Mark and Danny said. >>> >>> For example don't do this: >>> >>> def add(a,b): >>> ? ? ? if type(a) == int and type(b) == int: >>> ? ? ? ? ? return a+b >>> ? ? ? else: >>> ? ? ? ? ? raise TypeError >>> >>> Just do this: >>> >>> def add(a,b): >>> ? ? ? return a+b >> >> Given that the concept of Ducktyping has already been mentioned, is there a > reason why you did not mention try-except? >> >> def add(a, b): >> ? ? ? try: >> ? ? ? ? ? return a + b >> ? ? ? except TypeError: >> ? ? ? ? ? raise >> >> Btw, given that: >>>>> {}.__add__ >> Traceback (most recent call last): File "", line 1, in > AttributeError: 'dict' object has no attribute '__add__' >> >> Why does one only need to use 'except TypeError', not 'except > (TypeError, AttributeError)' in the try-except above? >>>>> {} + 1 >> Traceback (most recent call last): File "", line 1, in TypeError: > unsupported operand type(s) for +: 'dict' and 'int' >> > > What makes you think that you're calling your add function in either > example above?? In the first you're not calling anything as you've > missed the brackets.? Even if you add (groan :) them, you'll be trying > to call an add method for a dict, not your add function.? In the second > example, you're trying to add 1 to an empty dict, again your function > doesn't enter into the equation (double groan :) If I call my add function, then then the return statement would be equivalent to: -... if a={] and b=[1]: a.__add__(b) -... if a={} and b=1: AttributeError, because the class dict does not have an __add__ method. That's why I thought an AttributeError would also have to be caught, just in case the caller is stupid enough to give a dict as the first argument. But indeed (Alan) it was silly of me to just 'raise' and not doing anything else with it. From bensherman at gmail.com Wed Jun 18 21:35:48 2014 From: bensherman at gmail.com (Ben Sherman) Date: Wed, 18 Jun 2014 12:35:48 -0700 Subject: [Tutor] Pulling items from a dict in a print command Message-ID: Whats a more pythony way to do this? I have a dict with a few dozen elements, and I want to pull a few out. I've already shortened it with itemgetter, but it still seems redundant. I feel like I can do something like I've seen with *kwargs, but I'm not sure. I'm using old style sprintf formatting, so feel free to show me a better way to do this with the new way. Thanks for the help! Code: print(("overall_status=%s|" + "mon_count=%s," + "healthy_mons=%s," + "pg_count=%s," + "pg_clean_count=%s," + "osd_count=%s," + "osd_up=%s," + "osd_in=%s," + "bytes_avail=%s," + "bytes_used=%s," + "bytes_total=%s") % itemgetter("overall_status", "mon_count", "healthy_mons", "pg_count", "pg_clean_count", "osd_count", "osd_up", "osd_in", "bytes_avail", "bytes_used", "bytes_total")(parsed_json)) -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Wed Jun 18 22:05:16 2014 From: __peter__ at web.de (Peter Otten) Date: Wed, 18 Jun 2014 22:05:16 +0200 Subject: [Tutor] Pulling items from a dict in a print command References: Message-ID: Ben Sherman wrote: > Whats a more pythony way to do this? I have a dict with a few dozen > elements, and I want to pull a few out. I've already shortened it with > itemgetter, but it still seems redundant. I feel like I can do something > like I've seen with *kwargs, but I'm not sure. > > I'm using old style sprintf formatting, so feel free to show me a better > way to do this with the new way. > > Thanks for the help! > > Code: > > print(("overall_status=%s|" + > "mon_count=%s," + > "healthy_mons=%s," + > "pg_count=%s," + > "pg_clean_count=%s," + > "osd_count=%s," + > "osd_up=%s," + > "osd_in=%s," + > "bytes_avail=%s," + > "bytes_used=%s," + > "bytes_total=%s") % > itemgetter("overall_status", > "mon_count", > "healthy_mons", > "pg_count", > "pg_clean_count", > "osd_count", > "osd_up", > "osd_in", > "bytes_avail", > "bytes_used", > "bytes_total")(parsed_json)) names = ["overall_status", "mon_count", "pg_count", ...] print(", ".join("{}={}".format(name, parsed_json[name]) for name in names)) From breamoreboy at yahoo.co.uk Wed Jun 18 22:36:04 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 18 Jun 2014 21:36:04 +0100 Subject: [Tutor] Tips In-Reply-To: <1403119058.92985.YahooMailNeo@web163805.mail.gq1.yahoo.com> References: <53A0DA22.5020706@bitmessage.ch> <1403101541.4326.YahooMailNeo@web163804.mail.gq1.yahoo.com> <1403119058.92985.YahooMailNeo@web163805.mail.gq1.yahoo.com> Message-ID: On 18/06/2014 20:17, Albert-Jan Roskam wrote: > > > ----- Original Message ----- > >> From: Mark Lawrence >> To: tutor at python.org >> Cc: >> Sent: Wednesday, June 18, 2014 9:03 PM >> Subject: Re: [Tutor] Tips >> >> On 18/06/2014 15:25, Albert-Jan Roskam wrote: >>> ----- Original Message ----- >>>> From: Alan Gauld >>>> To: tutor at python.org >>>> Cc: >>>> Sent: Wednesday, June 18, 2014 11:47 AM >>>> Subject: Re: [Tutor] Tips >>>> >>>> On 18/06/14 01:15, Nanohard wrote: >>>>>> On 2014-06-17 13:35, Alan Gauld wrote: >>>>>> >>>>>>> Don't test types, use the interface >>>>>> >>>>>> Can you please explain what you mean by this? >>>>> >>>>> He means use the Python interpreter, by going to your console and >> typing >>>> "python", or in Windows >>>>> it's called 'IDLE'. >>>> >>>> >>>> Nope, I meant what Mark and Danny said. >>>> >>>> For example don't do this: >>>> >>>> def add(a,b): >>>> if type(a) == int and type(b) == int: >>>> return a+b >>>> else: >>>> raise TypeError >>>> >>>> Just do this: >>>> >>>> def add(a,b): >>>> return a+b >>> >>> Given that the concept of Ducktyping has already been mentioned, is there a >> reason why you did not mention try-except? >>> >>> def add(a, b): >>> try: >>> return a + b >>> except TypeError: >>> raise >>> >>> Btw, given that: >>>>>> {}.__add__ >>> Traceback (most recent call last): File "", line 1, in >> AttributeError: 'dict' object has no attribute '__add__' >>> >>> Why does one only need to use 'except TypeError', not 'except >> (TypeError, AttributeError)' in the try-except above? >>>>>> {} + 1 >>> Traceback (most recent call last): File "", line 1, in TypeError: >> unsupported operand type(s) for +: 'dict' and 'int' >>> >> >> What makes you think that you're calling your add function in either >> example above? In the first you're not calling anything as you've >> missed the brackets. Even if you add (groan :) them, you'll be trying >> to call an add method for a dict, not your add function. In the second >> example, you're trying to add 1 to an empty dict, again your function >> doesn't enter into the equation (double groan :) > > If I call my add function, then then the return statement would be equivalent to: > -... if a={] and b=[1]: a.__add__(b) > -... if a={} and b=1: AttributeError, because the class dict does not have an __add__ method. > That's why I thought an AttributeError would also have to be caught, just in case the caller is stupid enough to give a dict as the first argument. But indeed (Alan) it was silly of me to just 'raise' and not doing anything else with it. > Now you've completely lost me. Please explain precisely what you think your function does, and how it relates to the two examples that you tried that gave exceptions. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From davea at davea.name Wed Jun 18 22:42:50 2014 From: davea at davea.name (Dave Angel) Date: Wed, 18 Jun 2014 16:42:50 -0400 (EDT) Subject: [Tutor] Tips References: <53A16B68.1070307@virginmedia.com> Message-ID: Sydney Shall Wrote in message: > but I do not understand what defines a module Please post in plain text, not html. A module is a source file (eg. .py) or a compiled source file (eg .pyc) that's generally intended to be used via an import. -- DaveA From mahesh.rao78 at gmail.com Thu Jun 19 03:08:58 2014 From: mahesh.rao78 at gmail.com (Umamaheshwar Rao) Date: Wed, 18 Jun 2014 18:08:58 -0700 Subject: [Tutor] File reading Message-ID: Hi Experts, I have a file with below format this is first file operation x-1 "all the best" x-2 "all the best 2 next line check this" x-3 "last line" i need extract the lines starting with x-1 and x-2, can some throw some light on how to do? Thanks, Uma -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Thu Jun 19 03:17:14 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 19 Jun 2014 11:17:14 +1000 Subject: [Tutor] Tips In-Reply-To: <1403101541.4326.YahooMailNeo@web163804.mail.gq1.yahoo.com> References: <53A0DA22.5020706@bitmessage.ch> <1403101541.4326.YahooMailNeo@web163804.mail.gq1.yahoo.com> Message-ID: <20140619011713.GI7742@ando> On Wed, Jun 18, 2014 at 07:25:41AM -0700, Albert-Jan Roskam wrote: > Given that the concept of Ducktyping has already been mentioned, is > there a reason why you did not mention try-except? > ? > def add(a, b): > ??? try: > ??????? return a + b > ??? except TypeError: > ????????raise? As others have mentioned, this is pointless -- there is no good reason to catch an exception, only to *unconditionally* re-raise it. Sometimes it is useful to conditionally re-raise: try: something() except SomeFailure as e: if e.condition == foo: raise else: do_something_else() but even that is pretty rare. In general, the rule is to never catch any exception you aren't prepared to deal with in some way. ? > Btw, given that: > >>> {}.__add__ > Traceback (most recent call last): > File "", line 1, in AttributeError: > 'dict' object has no attribute '__add__' > > Why does one only need to use 'except TypeError', not 'except > (TypeError, AttributeError)'?in the try-except above? You answer your own question by trying it: > >>> {} + 1 > Traceback (most recent call last): > File "", line 1, in TypeError: > unsupported operand type(s) for +: 'dict' and 'int' You don't need to worry about AttributeError for __add__ because you aren't calling __add__ directly, you're using the + operator. x + y is not the same as calling x.__add__(y). It's actually quite clever, it gives *both* x and y a chance to decide what to do: (1) if y is a subclass of x, then try calling y.__radd__(x) otherwise try calling x.__add__(y) (2) if the method doesn't exist (raises AttributeError), or it returns the special value NotImplemented, try the other way around, x.__add__(y) or y.__radd__(x) (3) if that method also doesn't exist, or returns NotImplemented, then raise TypeError So you can see, the + operator catches the AttributeError raised if the object doesn't have __add__ or __radd__ methods, either to try a different method, or to turn it into a TypeError. -- Steven From dyoo at hashcollision.org Thu Jun 19 03:24:50 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 18 Jun 2014 18:24:50 -0700 Subject: [Tutor] File reading In-Reply-To: References: Message-ID: On Wed, Jun 18, 2014 at 6:08 PM, Umamaheshwar Rao wrote: > Hi Experts, > > I have a file with below format > Can you describe the format a little more precisely? You're giving examples, which is wonderful. But the examples are missing something. What's the output you want to get here? Imagine that you magically have the program you want. What would be the ideal structure of the output you're extracting from this file? I am guessing that if you have content of the form: ############################################## ## Some test data, which we'll treat as a file-like object by ## using StringIO: from StringIO import StringIO somefile = StringIO("""this is first file operation x-1 "all the best" x-2 "all the best 2 next line check this" x-3 "last line" """) ############################################## then I think you're expecting this to be parsed as a key-value store, something like this, maybe? ############################################## { "x-1" : "all the best", "x-2" : "all the best 2 next line\n check this", "x-3" : "last line" } ############################################## Can you confirm if this is the structure of the output you'd like to see? Please clarify. You need to make your expectations a bit explicit. By saying that you want to extract everything between x-1 and x-2, there's a bit of ambiguity there that I'd like to avoid By just giving us the input as an example, there are several kinds of output you might expect to see. We don't want to guess. From dyoo at hashcollision.org Thu Jun 19 03:36:12 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 18 Jun 2014 18:36:12 -0700 Subject: [Tutor] Tips In-Reply-To: <20140619011713.GI7742@ando> References: <53A0DA22.5020706@bitmessage.ch> <1403101541.4326.YahooMailNeo@web163804.mail.gq1.yahoo.com> <20140619011713.GI7742@ando> Message-ID: [content about __add__ dispatch resolution cut] We should remember the target audience, lest this thread doesn't spiral away so that only the tutors are talking to each other. I'm guilty of this as anyone, mind you. Pot. Kettle. :P The original question that the OP posed was: I want to know what some tips or information you can give me to remember some of the rules of Python, when you first start learning programming? and if the discussion is going to be on how __add__, __ladd__, and __radd__ all work in concert, then that might a hard right turn for beginners. From steve at pearwood.info Thu Jun 19 03:37:22 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 19 Jun 2014 11:37:22 +1000 Subject: [Tutor] Tips In-Reply-To: <53A16B68.1070307@virginmedia.com> References: <53A16B68.1070307@virginmedia.com> Message-ID: <20140619013722.GJ7742@ando> On Wed, Jun 18, 2014 at 12:35:20PM +0200, Sydney Shall wrote: > On 17/06/2014 22:35, Alan Gauld wrote: > >Use modules instead of singleton classes > As a new beginner with Python, I am having problem understanding the > difference here. > I think I understand classes, but still have problems with inheritance, > but I do not understand what defines a module. I assume you know how to make a class: class Spam: def method(self, arg): ... And then you make instances: x = Spam() y = Spam() A singleton class is one which only allows there to be a single instance (or occasionally, two instances, a "doubleton" class). For example, None is a singleton. Like all instances, None has a class, but see what happens if you try to create a second instance in Python 2.7 (Python 3.3 is slightly different): py> from types import NoneType py> x = NoneType() # create a new instance Traceback (most recent call last): File "", line 1, in TypeError: cannot create 'NoneType' instances The error is slightly inaccurate, it's not that no instances can be created at all, but that only *one* instance can be created, and it's already created and named None. Inheritence is a separate issue, big enough that it deserves a new thread. As for modules, you already use modules, I'm sure, you just don't realise it. Every time you create a python file ending in .py, that's a module. Scripts are modules. Every time you use the import command, you're loading a module: py> import math py> print math Python tries very hard to ensure that every module is loaded only once. (There are circumstances where you can fool it, but they're rare.) Since the module holds state (variables) and behaviour (functions), modules perform the same sort of role as classes, so a module which is loaded once is very similar to a singleton instance. In other words, if you want a class to implement singleton behaviour, you have to work at it. But if you shift the functionality from the class into a module, Python gives you singleton behaviour for free. But if you're not sure why anyone would want a singleton instance, I agree with you: most (but not all) uses of singletons are unnecessary. -- Steven From mahesh.rao78 at gmail.com Thu Jun 19 03:40:49 2014 From: mahesh.rao78 at gmail.com (Umamaheshwar Rao) Date: Wed, 18 Jun 2014 18:40:49 -0700 Subject: [Tutor] File reading In-Reply-To: References: Message-ID: i agree. i expect the output be "all the best" "all the best 2 next line\n check this" On Wed, Jun 18, 2014 at 6:24 PM, Danny Yoo wrote: > On Wed, Jun 18, 2014 at 6:08 PM, Umamaheshwar Rao > wrote: > > Hi Experts, > > > > I have a file with below format > > > > > Can you describe the format a little more precisely? You're giving > examples, which is wonderful. But the examples are missing something. > > What's the output you want to get here? Imagine that you magically > have the program you want. What would be the ideal structure of the > output you're extracting from this file? > > > I am guessing that if you have content of the form: > > ############################################## > ## Some test data, which we'll treat as a file-like object by > ## using StringIO: > from StringIO import StringIO > > somefile = StringIO("""this is first file operation > x-1 "all the best" > x-2 "all the best 2 next line > check this" > x-3 "last line" > """) > ############################################## > > > then I think you're expecting this to be parsed as a key-value store, > something like this, maybe? > > ############################################## > { "x-1" : "all the best", > "x-2" : "all the best 2 next line\n check this", > "x-3" : "last line" } > ############################################## > > Can you confirm if this is the structure of the output you'd like to > see? Please clarify. > > > You need to make your expectations a bit explicit. By saying that you > want to extract everything between x-1 and x-2, there's a bit of > ambiguity there that I'd like to avoid By just giving us the input > as an example, there are several kinds of output you might expect to > see. We don't want to guess. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Thu Jun 19 04:12:31 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 18 Jun 2014 19:12:31 -0700 Subject: [Tutor] File reading In-Reply-To: References: Message-ID: Hi Uma, In your case, I'd look at the file as a sequence of "tokens" and look at this as a tokenization problem I think we'll see some kind of _identifier_, followed by _whitespace_, followed by a _string_. All these three tokens will repeat, until we hit the end of the the file. More formally, I'd try to describe the file's structure in a grammar: ## This is not Python, but just a way for me to formally express what I think your file format is: file := (IDENTIFIER WHITESPACE STRING)* END_OF_FILE The star there is meant to symbolize the "repeatedly" part. Note that we haven't yet said what IDENTIFIER, WHITESPACE, or STRING means at all yet: I'm just making sure we've got the toplevel understanding of the file. If this is true, then we might imagine a function tokenize() that takes the file and breaks it down into a sequence of these tokens, and then the job of pulling out the content you care about should be easier. We can loop over the token sequence, watch for an identifier "x-1", skip over the whitespace token, and then take the string we care about, till we hit the "x-2" identifier and stop. tokenize() should not be too bad to write: we walk the file, and recognize certain patterns as either IDENTIFIER, WHITESPACE, or STRING. IDENTIFIER looks like a bunch of non-whitespace characters. WHITESPACE looks like a bunch of whitespace characters. STRING looks like a quote, followed by a bunch of non-quote characters, followed by a quote. The description above is very handwavy. You can more formally write those descriptions out by hand with the use of regular expressions. Regular expressions are a mini-language for writing out string patterns and extracting content from strings. See: https://docs.python.org/2/howto/regex.html Once we can formally describe the patterns above, then we can walk the characters in the file. We pick out which of the three patterns will match what we're currently seeing, and then add to the list of tokens. Eventually, we hit end of file, and tokenize() can return all the tokens that it has accumulated. From dyoo at hashcollision.org Thu Jun 19 03:51:27 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 18 Jun 2014 18:51:27 -0700 Subject: [Tutor] Pulling items from a dict in a print command In-Reply-To: References: Message-ID: Hi Ben, There's a bit of duplication in the keys being looked up, making it a bit fragile if you need to correct the spelling of a key or need to add additional keys, since there are two places that need to be changed. One way to make this a little cleaner might be to have a helper function that does the work you're doing now in constructing the formatting string. Something like this might be worth it: ################################################ def makeStatusLine(keyValues, firstKey, restKeys): buffer = [] for key in restKeys: buffer.append("%s=%s" % (key, keyValues[key])) firstColumn = "%s=%s|" % (firstKey, keyValues[firstKey]) return firstColumn + ",".join(buffer) ################################################ For example: ################################################ >>> makeStatusLine({'name': 'Ben', 'forum': 'tutor at python.org'}, 'name', ['forum']) 'name=Ben|forum=tutor at python.org' ################################################ That way, you can write the sequence of columns just once, and be a lot more sure that the output is formatted consistently. Here, we'll know the first column is treated specially with the "|" pipe symbol, and the rest will be comma-delimited. From breamoreboy at yahoo.co.uk Thu Jun 19 08:41:52 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 19 Jun 2014 07:41:52 +0100 Subject: [Tutor] Tips In-Reply-To: References: <53A0DA22.5020706@bitmessage.ch> <1403101541.4326.YahooMailNeo@web163804.mail.gq1.yahoo.com> <20140619011713.GI7742@ando> Message-ID: On 19/06/2014 02:36, Danny Yoo wrote: > [content about __add__ dispatch resolution cut] > > > We should remember the target audience, lest this thread doesn't > spiral away so that only the tutors are talking to each other. I'm > guilty of this as anyone, mind you. Pot. Kettle. :P > > > The original question that the OP posed was: > > I want to know what some tips or information you can give me to > remember some of the rules of Python, when you first start learning > programming? > > > and if the discussion is going to be on how __add__, __ladd__, and > __radd__ all work in concert, then that might a hard right turn for > beginners. > I wrote something similar months if not years ago and was shot down in flames. Fortunately for me but unfortunately for you (plural), my parachute opened :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From fomcl at yahoo.com Thu Jun 19 09:22:09 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 19 Jun 2014 00:22:09 -0700 Subject: [Tutor] Tips In-Reply-To: <20140619011713.GI7742@ando> References: <53A0DA22.5020706@bitmessage.ch> <1403101541.4326.YahooMailNeo@web163804.mail.gq1.yahoo.com> <20140619011713.GI7742@ando> Message-ID: <1403162529.35637.YahooMailNeo@web163804.mail.gq1.yahoo.com> ----- Original Message ----- > From: Steven D'Aprano > To: tutor at python.org > Cc: > Sent: Thursday, June 19, 2014 3:17 AM > Subject: Re: [Tutor] Tips > > On Wed, Jun 18, 2014 at 07:25:41AM -0700, Albert-Jan Roskam wrote: > >> Given that the concept of Ducktyping has already been mentioned, is >> there a reason why you did not mention try-except? >> ? >> def add(a, b): >> ??? try: >> ??????? return a + b >> ??? except TypeError: >> ????????raise? > > As others have mentioned, this is pointless -- there is no good reason > to catch an exception, only to *unconditionally* re-raise it. > > Sometimes it is useful to conditionally re-raise: > > try: > ? ? something() > except SomeFailure as e: > ? ? if e.condition == foo: > ? ? ? ? raise > ? ? else: > ? ? ? ? do_something_else() > > but even that is pretty rare. In general, the rule is to never catch any > exception you aren't prepared to deal with in some way. > > ? >> Btw, given that: >> >>> {}.__add__ >> Traceback (most recent call last): >> File "", line 1, in AttributeError: >> 'dict' object has no attribute '__add__' >> >> Why does one only need to use 'except TypeError', not 'except >> (TypeError, AttributeError)'?in the try-except above? > > You answer your own question by trying it: > >> >>> {} + 1 >> Traceback (most recent call last): >> File "", line 1, in TypeError: >> unsupported operand type(s) for +: 'dict' and 'int' > > > You don't need to worry about AttributeError for __add__ because you > aren't calling __add__ directly, you're using the + operator. Aha!!! I always thought that "+" was perfectly equivalent to "__add__", just a more readable way of writing this (I would almost use the term "syntactical sugar" but for some reason that only seems to be used in the context of decorators, so I don't. Oops, I still did use that term. Oh well :-) > x + y is not the same as calling x.__add__(y). It's actually quite > clever, it gives *both* x and y a chance to decide what to do: > > (1) if y is a subclass of x, then try calling y.__radd__(x) > ? ? otherwise try calling x.__add__(y) > (2) if the method doesn't exist (raises AttributeError), > ? ? or it returns the special value NotImplemented, > ? ? try the other way around, x.__add__(y) or y.__radd__(x) > (3) if that method also doesn't exist, or returns > ? ? NotImplemented, then raise TypeError > > So you can see, the + operator catches the AttributeError raised if the > object doesn't have __add__ or __radd__ methods, either to try a > different method, or to turn it into a TypeError. Now I understand it. Very interesting. Thanks, that was exactly what I meant! "+" != "__add__". From fomcl at yahoo.com Thu Jun 19 11:09:03 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 19 Jun 2014 02:09:03 -0700 Subject: [Tutor] Tips In-Reply-To: References: <53A0DA22.5020706@bitmessage.ch> <1403101541.4326.YahooMailNeo@web163804.mail.gq1.yahoo.com> <20140619011713.GI7742@ando> Message-ID: <1403168943.56483.YahooMailNeo@web163801.mail.gq1.yahoo.com> ----- Original Message ----- > From: Mark Lawrence > To: tutor at python.org > Cc: > Sent: Thursday, June 19, 2014 8:41 AM > Subject: Re: [Tutor] Tips > > On 19/06/2014 02:36, Danny Yoo wrote: >> [content about __add__ dispatch resolution cut] >> >> >> We should remember the target audience, lest this thread doesn't >> spiral away so that only the tutors are talking to each other.? I'm >> guilty of this as anyone, mind you.? Pot.? Kettle.? :P >> >> >> The original question that the OP posed was: >> >> ? ? ? I want to know what some tips or information you can give me to >> remember some of the rules of Python, when you first start learning >> programming? >> >> >> and if the discussion is going to be on how __add__, __ladd__, and >> __radd__ all work in concert, then that might a hard right turn for >> beginners. I understand. Sometimes threads go like this, from one topic to another, and then an even more exotic topic, and then, well I didn't even know he played the violin?! http://www.youtube.com/watch?v=Hh_shsRfXqk :-) From s.shall at virginmedia.com Thu Jun 19 13:09:31 2014 From: s.shall at virginmedia.com (Sydney Shall) Date: Thu, 19 Jun 2014 13:09:31 +0200 Subject: [Tutor] Tips In-Reply-To: <20140619013722.GJ7742@ando> References: <53A16B68.1070307@virginmedia.com> <20140619013722.GJ7742@ando> Message-ID: <53A2C4EB.7020907@virginmedia.com> On 19/06/2014 03:37, Steven D'Aprano wrote: > On Wed, Jun 18, 2014 at 12:35:20PM +0200, Sydney Shall wrote: >> On 17/06/2014 22:35, Alan Gauld wrote: >>> Use modules instead of singleton classes >> As a new beginner with Python, I am having problem understanding the >> difference here. >> I think I understand classes, but still have problems with inheritance, >> but I do not understand what defines a module. > I assume you know how to make a class: > > class Spam: > def method(self, arg): > ... > > > And then you make instances: > > x = Spam() > y = Spam() > > A singleton class is one which only allows there to be a single > instance (or occasionally, two instances, a "doubleton" class). > > For example, None is a singleton. Like all instances, None has a class, > but see what happens if you try to create a second instance in Python > 2.7 (Python 3.3 is slightly different): > > py> from types import NoneType > py> x = NoneType() # create a new instance > Traceback (most recent call last): > File "", line 1, in > TypeError: cannot create 'NoneType' instances > > > The error is slightly inaccurate, it's not that no instances can be > created at all, but that only *one* instance can be created, and it's > already created and named None. > > Inheritence is a separate issue, big enough that it deserves a new > thread. > > As for modules, you already use modules, I'm sure, you just don't > realise it. Every time you create a python file ending in .py, that's a > module. Scripts are modules. Every time you use the import command, > you're loading a module: > > py> import math > py> print math > > > > Python tries very hard to ensure that every module is loaded only once. > (There are circumstances where you can fool it, but they're rare.) Since > the module holds state (variables) and behaviour (functions), modules > perform the same sort of role as classes, so a module which is loaded > once is very similar to a singleton instance. In other words, if you > want a class to implement singleton behaviour, you have to work at it. > But if you shift the functionality from the class into a module, Python > gives you singleton behaviour for free. > > But if you're not sure why anyone would want a singleton instance, I > agree with you: most (but not all) uses of singletons are unnecessary. > > Thanks a lot. This was very useful and clear. -- Sydney Shall From duxbuz at hotmail.com Thu Jun 19 14:18:35 2014 From: duxbuz at hotmail.com (Ian D) Date: Thu, 19 Jun 2014 12:18:35 +0000 Subject: [Tutor] python libraries online Message-ID: A while back some one linked to the python source and I was able to view the whole of its libraries and class files something like this java api site http://docs.oracle.com/javase/7/docs/api/ But I cant seem to find what I want now. I wanted to be able to look at the classes etc Anyone help plz From steve at pearwood.info Thu Jun 19 14:23:01 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 19 Jun 2014 22:23:01 +1000 Subject: [Tutor] python libraries online In-Reply-To: References: Message-ID: <20140619122300.GL7742@ando> On Thu, Jun 19, 2014 at 12:18:35PM +0000, Ian D wrote: > A while back some one linked to the python source and I was able to > view the whole of its libraries and class files something like this > java api site http://docs.oracle.com/javase/7/docs/api/ http://hg.python.org/cpython/file/cf70f030a744/Lib/ Don't forget that if you have Python installed on your computer, the standard library (at least the parts written in Python) will be installed on your computer too, and readable. -- Steven From steve at pearwood.info Thu Jun 19 14:25:05 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 19 Jun 2014 22:25:05 +1000 Subject: [Tutor] Tips In-Reply-To: References: <53A0DA22.5020706@bitmessage.ch> <1403101541.4326.YahooMailNeo@web163804.mail.gq1.yahoo.com> <20140619011713.GI7742@ando> Message-ID: <20140619122504.GM7742@ando> On Wed, Jun 18, 2014 at 06:36:12PM -0700, Danny Yoo wrote: > [content about __add__ dispatch resolution cut] > > > We should remember the target audience, lest this thread doesn't > spiral away so that only the tutors are talking to each other. I'm > guilty of this as anyone, mind you. Pot. Kettle. :P Perhaps so, but I wasn't answering the OP, I was answering Albert, who asked a perfectly reasonable follow-up question about + and the __add__ method. Discussion threads often go in strange and interesting directions. [...] > and if the discussion is going to be on how __add__, __ladd__, and > __radd__ all work in concert, then that might a hard right turn for > beginners. __ L add__ ??? :-) -- Steven From duxbuz at hotmail.com Thu Jun 19 14:34:20 2014 From: duxbuz at hotmail.com (Ian D) Date: Thu, 19 Jun 2014 12:34:20 +0000 Subject: [Tutor] python libraries online In-Reply-To: <20140619122300.GL7742@ando> References: , <20140619122300.GL7742@ando> Message-ID: Ok Thanks. I will look on the computer, it seems that the online repository is not so easy to search. i.e If I search csv I get all the changes all the merges... but I just want the class file ---------------------------------------- > Date: Thu, 19 Jun 2014 22:23:01 +1000 > From: steve at pearwood.info > To: tutor at python.org > Subject: Re: [Tutor] python libraries online > > On Thu, Jun 19, 2014 at 12:18:35PM +0000, Ian D wrote: > >> A while back some one linked to the python source and I was able to >> view the whole of its libraries and class files something like this >> java api site http://docs.oracle.com/javase/7/docs/api/ > > http://hg.python.org/cpython/file/cf70f030a744/Lib/ > > > Don't forget that if you have Python installed on your computer, the > standard library (at least the parts written in Python) will be > installed on your computer too, and readable. > > > > -- > Steven > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From duxbuz at hotmail.com Thu Jun 19 14:37:17 2014 From: duxbuz at hotmail.com (Ian D) Date: Thu, 19 Jun 2014 12:37:17 +0000 Subject: [Tutor] python libraries online In-Reply-To: <20140619122300.GL7742@ando> References: , <20140619122300.GL7742@ando> Message-ID: And I wondered what 'cpython' was when I came across it. I thought they might have called it python ---------------------------------------- > Date: Thu, 19 Jun 2014 22:23:01 +1000 > From: steve at pearwood.info > To: tutor at python.org > Subject: Re: [Tutor] python libraries online > > On Thu, Jun 19, 2014 at 12:18:35PM +0000, Ian D wrote: > >> A while back some one linked to the python source and I was able to >> view the whole of its libraries and class files something like this >> java api site http://docs.oracle.com/javase/7/docs/api/ > > http://hg.python.org/cpython/file/cf70f030a744/Lib/ > > > Don't forget that if you have Python installed on your computer, the > standard library (at least the parts written in Python) will be > installed on your computer too, and readable. > > > > -- > Steven > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From breamoreboy at yahoo.co.uk Thu Jun 19 14:40:22 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 19 Jun 2014 13:40:22 +0100 Subject: [Tutor] Tips In-Reply-To: <1403168943.56483.YahooMailNeo@web163801.mail.gq1.yahoo.com> References: <53A0DA22.5020706@bitmessage.ch> <1403101541.4326.YahooMailNeo@web163804.mail.gq1.yahoo.com> <20140619011713.GI7742@ando> <1403168943.56483.YahooMailNeo@web163801.mail.gq1.yahoo.com> Message-ID: On 19/06/2014 10:09, Albert-Jan Roskam wrote: > > > ----- Original Message ----- > >> From: Mark Lawrence >> To: tutor at python.org >> Cc: >> Sent: Thursday, June 19, 2014 8:41 AM >> Subject: Re: [Tutor] Tips >> >> On 19/06/2014 02:36, Danny Yoo wrote: >>> [content about __add__ dispatch resolution cut] >>> >>> >>> We should remember the target audience, lest this thread doesn't >>> spiral away so that only the tutors are talking to each other. I'm >>> guilty of this as anyone, mind you. Pot. Kettle. :P >>> >>> >>> The original question that the OP posed was: >>> >>> I want to know what some tips or information you can give me to >>> remember some of the rules of Python, when you first start learning >>> programming? >>> >>> >>> and if the discussion is going to be on how __add__, __ladd__, and >>> __radd__ all work in concert, then that might a hard right turn for >>> beginners. > > > I understand. Sometimes threads go like this, from one topic to another, and then an even more exotic topic, and then, well I didn't even know he played the violin?! http://www.youtube.com/watch?v=Hh_shsRfXqk :-) > I didn't write any of the above. Did you mean to reply to me, but inadvertantly snipped too much, or did you mean to reply to Danny, or what? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From steve at pearwood.info Thu Jun 19 14:42:21 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 19 Jun 2014 22:42:21 +1000 Subject: [Tutor] Tips In-Reply-To: References: Message-ID: <20140619124221.GN7742@ando> On Tue, Jun 17, 2014 at 02:52:33PM -0400, keith papa wrote: > > Hi, I want to no what some tips or information you can give me to > remember some of the rules of python, when you first start learning > programming? Questions about memory (that is, *human* memory, in the brain) are not really on-topic for this list. We're not likely to be experts on how memory works. (I'm a little dubious that *anyone* is an expert on how memory works, but that's another story.) But having said that, what worked for me was, practice practice and even more practice. I have spent *years* writing little bits of Python code. 99.9% of that code was junk, and either never finished, or thrown away but it's still good practice. If you want to be a master carpenter, you have to expect to spend a lot of time making rubbish before you make your masterpiece. Same applies to programming. Get used to *reading* Python code. (It's easier to read something than to write it.) You will find huge amounts of Python code on the Internet, on Wikipedia, on the Activestate cookbook, on the tutor archives, in the Python standard library, and more: https://en.wikipedia.org/wiki/Python_%28programming_language%29 http://code.activestate.com/recipes/langs/python/ https://mail.python.org/pipermail/tutor/ http://hg.python.org/cpython/file/cf70f030a744/Lib/ https://wiki.python.org/moin/SimplePrograms http://rosettacode.org/wiki/Category:Python To learn to program in Python, you need to spend time writing Python code as well as reading it. You'll make mistakes. That's *good*. Every time you make a mistake: py> for i = 1 to 20: File "", line 1 for i = 1 to 20: ^ SyntaxError: invalid syntax that's an opportunity for learning something. The great thing about making mistakes is that you never run out of opportunities to learn! When you make a mistake, try to match what you've written to something you've read. In the above, I've tried to write a for-loop, but I've written it almost the way I'd write it in Pascal, not Python. Naturally it doesn't work. Where have I seen something with a for loop in Python? http://rosettacode.org/wiki/Flow-control_structures#Loops https://wiki.python.org/moin/ForLoop The more code you have read, the more likely you will have seen something similar to what you are trying to do. And the more you write it, the more it will sink in to your memory and become second nature. -- Steven From breamoreboy at yahoo.co.uk Thu Jun 19 14:46:03 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 19 Jun 2014 13:46:03 +0100 Subject: [Tutor] python libraries online In-Reply-To: References: , <20140619122300.GL7742@ando> Message-ID: On 19/06/2014 13:37, Ian D wrote: > And I wondered what 'cpython' was when I came across it. I thought they might have called it python > Cpython because it's written in C. Look closely at the repository you'll find Jython there as well, it's written in Java. There are umpteen other Python versions, IronPython for .Net amongst others. Please don't top post on this list, it's extremely irritating following long threads when this is done. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From fomcl at yahoo.com Thu Jun 19 14:46:29 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 19 Jun 2014 05:46:29 -0700 Subject: [Tutor] Tips In-Reply-To: References: <53A0DA22.5020706@bitmessage.ch> <1403101541.4326.YahooMailNeo@web163804.mail.gq1.yahoo.com> <20140619011713.GI7742@ando> <1403168943.56483.YahooMailNeo@web163801.mail.gq1.yahoo.com> Message-ID: <1403181989.35645.YahooMailNeo@web163804.mail.gq1.yahoo.com> ----- Original Message ----- > From: Mark Lawrence > To: tutor at python.org > Cc: > Sent: Thursday, June 19, 2014 2:40 PM > Subject: Re: [Tutor] Tips > > On 19/06/2014 10:09, Albert-Jan Roskam wrote: >>> From: Mark Lawrence >>> To: tutor at python.org >>> Cc: >>> Sent: Thursday, June 19, 2014 8:41 AM >>> Subject: Re: [Tutor] Tips >>> >>> On 19/06/2014 02:36, Danny Yoo wrote: >>>> ? [content about __add__ dispatch resolution cut] >>>> >>>> >>>> ? We should remember the target audience, lest this thread > doesn't >>>> ? spiral away so that only the tutors are talking to each other.? > I'm >>>> ? guilty of this as anyone, mind you.? Pot.? Kettle.? :P > I didn't write any of the above.? Did you mean to reply to me, but > inadvertantly snipped too much, or did you mean to reply to Danny, or what? Sorry, yes I intended to reply to Danny's Pot/Kettle remark. From dyoo at hashcollision.org Thu Jun 19 14:53:47 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 19 Jun 2014 05:53:47 -0700 Subject: [Tutor] python libraries online In-Reply-To: References: Message-ID: On Thu, Jun 19, 2014 at 5:18 AM, Ian D wrote: > A while back some one linked to the python source and I was able to view the whole of its libraries and class files something like this java api site http://docs.oracle.com/javase/7/docs/api/ > > > But I cant seem to find what I want now. > > > I wanted to be able to look at the classes etc Hi Ian, Are you looking for the Library Reference? You can find it here: https://docs.python.org/2/library/index.html It should describe the functionality of the standard library. From duxbuz at hotmail.com Thu Jun 19 14:59:58 2014 From: duxbuz at hotmail.com (Ian D) Date: Thu, 19 Jun 2014 12:59:58 +0000 Subject: [Tutor] python libraries online In-Reply-To: References: , , <20140619122300.GL7742@ando>, , Message-ID: What does top post mean? ---------------------------------------- > To: tutor at python.org > From: breamoreboy at yahoo.co.uk > Date: Thu, 19 Jun 2014 13:46:03 +0100 > Subject: Re: [Tutor] python libraries online > > On 19/06/2014 13:37, Ian D wrote: >> And I wondered what 'cpython' was when I came across it. I thought they might have called it python >> > > Cpython because it's written in C. Look closely at the repository > you'll find Jython there as well, it's written in Java. There are > umpteen other Python versions, IronPython for .Net amongst others. > > Please don't top post on this list, it's extremely irritating following > long threads when this is done. > > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence > > --- > This email is free from viruses and malware because avast! Antivirus protection is active. > http://www.avast.com > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From steve at pearwood.info Thu Jun 19 16:00:52 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 20 Jun 2014 00:00:52 +1000 Subject: [Tutor] python libraries online In-Reply-To: References: <20140619122300.GL7742@ando> Message-ID: <20140619140051.GO7742@ando> On Thu, Jun 19, 2014 at 12:37:17PM +0000, Ian D wrote: > And I wondered what 'cpython' was when I came across it. I thought > they might have called it python Python is the name of the programming languages. There are many different implementations of that programming language: - Jython is Python written in Java - IronPython is Python written for Dot-Net - PyPy is a high-performance optimizing Just-In-Time compiler for Python - Stackless is a version of Python that doesn't use the C calling stack - Nuitka is a static compiler for Python written in C++ - and of course, there is the plain old ordinary "python" you are used to, the reference implementation, also called "CPython" because the compiler is written in C. -- Steven From steve at pearwood.info Thu Jun 19 16:30:49 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 20 Jun 2014 00:30:49 +1000 Subject: [Tutor] python libraries online In-Reply-To: References: Message-ID: <20140619143048.GP7742@ando> On Thu, Jun 19, 2014 at 12:59:58PM +0000, Ian D wrote: > What does top post mean? It means posting at the top of the reply, just as you have done here. When you reply to an email, the comments you are replying to are quoted with greater-than signs > at the start of each line. There are three basic places to insert your replies to the comments being quoted: at the top, at the bottom, and interleaved through the middle. Here is an example. Suppose I write an email asking two questions: Hello, how long should I boil a soft-boiled egg? And how many eggs in a dozen? (I didn't say they were *good* questions.) You reply, and my comments are quoted: === This is top-posting === Oh, about 3 minutes, depending on the size of the egg. Twelve eggs. Steven asked: > Hello, how long should I boil a soft-boiled egg? > And how many eggs in a dozen? === This is bottom-posting === Steven asked: > Hello, how long should I boil a soft-boiled egg? > And how many eggs in a dozen? Oh, about 3 minutes, depending on the size of the egg. Twelve eggs. === This is interleaved posting === Steven asked: > Hello, how long should I boil a soft-boiled egg? Oh, about 3 minutes, depending on the size of the egg. > And how many eggs in a dozen? Twelve eggs. =================================== For detailed, complicated discussions where people are replying to multiple points, interleaved posting is by far the best. It is like carrying on a conversation: > Question Answer > Question Answer > Point Counter-point > Question Answer The context for each answer is right there, next to the answer. It makes the email *much* easier to follow when things get technical and complicated. Top-posting and bottom-posting are okay for short, trivial responses where the context is not very important, but sadly they also get used by lazy writers who don't care about the people reading the email. (I hope I do not offend, but I've been dealing with email for close to 20 years and in my experience there are a lot of lazy writers. If you've ever asked somebody four questions in an email, and they've fired off a reply answering one of them and ignoring the other three, you will know what I mean.) Top-posting encourages short, snappy responses, where the context can be inferred from the subject line or the first few sentences of the quoted comments: Okay see you there. > Hey Bill, meet us at the pub tonight? 2pm > Sue, what time is the meeting today? Yes. > Do you want the 2TB hard drive or a 1TB hard drive? But for technical discussions, short, snappy responses are often not very good. A *discussion* may go back and forth over many different points, not just one or two sentence replies. For this reason, in technical forums like this one, interleaved posting is MUCH preferred. -- Steven From duxbuz at hotmail.com Thu Jun 19 16:35:16 2014 From: duxbuz at hotmail.com (Ian D) Date: Thu, 19 Jun 2014 14:35:16 +0000 Subject: [Tutor] python libraries online In-Reply-To: <20140619143048.GP7742@ando> References: , , <20140619143048.GP7742@ando> Message-ID: ---------------------------------------- > Date: Fri, 20 Jun 2014 00:30:49 +1000 > From: steve at pearwood.info > To: tutor at python.org > Subject: Re: [Tutor] python libraries online > > On Thu, Jun 19, 2014 at 12:59:58PM +0000, Ian D wrote: >> What does top post mean? > > It means posting at the top of the reply, just as you have done here. > > When you reply to an email, the comments you are replying to are quoted > with greater-than signs> at the start of each line. There are three > basic places to insert your replies to the comments being quoted: at the > top, at the bottom, and interleaved through the middle. > > Here is an example. Suppose I write an email asking two questions: > > Hello, how long should I boil a soft-boiled egg? > And how many eggs in a dozen? > > > (I didn't say they were *good* questions.) You reply, and my comments are quoted: > > === This is top-posting === > > Oh, about 3 minutes, depending on the size of the egg. > Twelve eggs. > > Steven asked: >> Hello, how long should I boil a soft-boiled egg? >> And how many eggs in a dozen? > > > === This is bottom-posting === > > Steven asked: >> Hello, how long should I boil a soft-boiled egg? >> And how many eggs in a dozen? > > Oh, about 3 minutes, depending on the size of the egg. > Twelve eggs. > > > === This is interleaved posting === > > Steven asked: >> Hello, how long should I boil a soft-boiled egg? > > Oh, about 3 minutes, depending on the size of the egg. > >> And how many eggs in a dozen? > > Twelve eggs. > > =================================== > > > For detailed, complicated discussions where people are replying to > multiple points, interleaved posting is by far the best. It is like > carrying on a conversation: > >> Question > Answer >> Question > Answer >> Point > Counter-point >> Question > Answer > > > The context for each answer is right there, next to the answer. It makes > the email *much* easier to follow when things get technical and > complicated. > > Top-posting and bottom-posting are okay for short, trivial responses > where the context is not very important, but sadly they also get used by > lazy writers who don't care about the people reading the email. > > (I hope I do not offend, but I've been dealing with email for close to > 20 years and in my experience there are a lot of lazy writers. If you've > ever asked somebody four questions in an email, and they've fired off a > reply answering one of them and ignoring the other three, you will know > what I mean.) > > Top-posting encourages short, snappy responses, where the context can > be inferred from the subject line or the first few sentences of the > quoted comments: > > > Okay see you there. >> Hey Bill, meet us at the pub tonight? > > > 2pm >> Sue, what time is the meeting today? > > > Yes. >> Do you want the 2TB hard drive or a 1TB hard drive? > > > But for technical discussions, short, snappy responses are often not > very good. A *discussion* may go back and forth over many different > points, not just one or two sentence replies. For this reason, in > technical forums like this one, interleaved posting is MUCH preferred. > > > > -- > Steven > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor Ok and do I leave all this rubbish at the bottom? or edit it. Or is it bad practice to edit someone's text From breamoreboy at yahoo.co.uk Thu Jun 19 16:58:14 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 19 Jun 2014 15:58:14 +0100 Subject: [Tutor] python libraries online In-Reply-To: References: , , <20140619143048.GP7742@ando> Message-ID: On 19/06/2014 15:35, Ian D wrote: > > Ok and do I leave all this rubbish at the bottom? or edit it. Or is it bad practice to edit someone's text > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > If you're referring to the stuff above I usually delete it. It is bad practice to edit the text if it changes the context. It is good practice if (say) you're replying to only one paragraph out of ten. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From duxbuz at hotmail.com Thu Jun 19 17:13:45 2014 From: duxbuz at hotmail.com (Ian D) Date: Thu, 19 Jun 2014 15:13:45 +0000 Subject: [Tutor] write dictionary to file Message-ID: When trying to write my dictionary to a file I get: f.write(output) TypeError: 'tuple' does not support the buffer interface using this example: #so far this should read a file #using dictreader and take a column and join some text onto it import csv csvfile= open('StudentListToSort.csv', newline='') spamreader = csv.DictReader(csvfile,delimiter=',',quotechar='|') #open a file to write to later f = open('output.csv', 'wb+') #iterate through dictreader object for row in spamreader: if row['year'] == '40': username = row['user'] #I put stuff in variables for ease of viewing/debugging email = "".join([username,'@email.com]) # join text #put output for file in variable first output = email, row['first'],row['last'],row['password'] #change tuple to list #outputlist= list(output) #write results to file f.write(output) print(output) I then tried to cast the tuple to a list thinking that would help like this: #change tuple to list #outputlist= list(output) #write results to file f.write(outputlist) print(outputlist) same problem: f.write(outputlist) TypeError: 'list' does not support the buffer interface So is it the csv.DictWriter that is needed here? From dyoo at hashcollision.org Thu Jun 19 17:46:10 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 19 Jun 2014 08:46:10 -0700 Subject: [Tutor] write dictionary to file In-Reply-To: References: Message-ID: On Thu, Jun 19, 2014 at 8:13 AM, Ian D wrote: > When trying to write my dictionary to a file I get: > > > f.write(output) > TypeError: 'tuple' does not support the buffer interface When writing structured data to disk files, you need to do something extra, because what disk files support are the reading and writing of raw bytes. That is, there are multiple ways to get that structured data to disk, so you've got to choose! :P Typically, you'll use some function to "encode" your structured data into a linear byte string, and write the byte string to disk. Later, to recover that structure, you use another function to "decode" the linear byte string back into structured data. There are several encoder/decoder libraries in Python that you can use. A popular one is the JSON library: it's popular because JSON is well-supported by other programming languages. https://docs.python.org/2/library/json.html But in your case, you probably want to stick with CSV, since that's what your input is in. > So is it the csv.DictWriter that is needed here? That's probably most appropriate in your situation, yes. Since you're using csv.DictReader, it does make sense to use csv.DictWriter when you're storing the data if you want to use the same encoding format. From akleider at sonic.net Thu Jun 19 20:10:26 2014 From: akleider at sonic.net (Alex Kleider) Date: Thu, 19 Jun 2014 11:10:26 -0700 Subject: [Tutor] Tips In-Reply-To: <20140619013722.GJ7742@ando> References: <53A16B68.1070307@virginmedia.com> <20140619013722.GJ7742@ando> Message-ID: On 2014-06-18 18:37, Steven D'Aprano wrote: > Python tries very hard to ensure that every module is loaded only once. > (There are circumstances where you can fool it, but they're rare.) > Since > the module holds state (variables) and behaviour (functions), modules > perform the same sort of role as classes, so a module which is loaded > once is very similar to a singleton instance. In other words, if you > want a class to implement singleton behaviour, you have to work at it. > But if you shift the functionality from the class into a module, Python > gives you singleton behaviour for free. > > But if you're not sure why anyone would want a singleton instance, I > agree with you: most (but not all) uses of singletons are unnecessary. The idea of a singleton class is new to me as is this comparison of class vs module. Can anyone suggest a place to turn for more discussion of the topic? thks, alexK From alan.gauld at btinternet.com Thu Jun 19 20:40:05 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 19 Jun 2014 19:40:05 +0100 Subject: [Tutor] python libraries online In-Reply-To: References: , , <20140619143048.GP7742@ando> Message-ID: On 19/06/14 15:35, Ian D wrote: > Ok and do I leave all this rubbish at the bottom? or edit it. Delete as much as possible that is not needed to understand the reply. But only as much as necessary, err on the generous side if in doubt. But definitely things like the Python mailing list comments etc can be deleted - we've all seen them multiple times! :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From fomcl at yahoo.com Thu Jun 19 21:33:33 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 19 Jun 2014 12:33:33 -0700 Subject: [Tutor] Tips In-Reply-To: References: <53A16B68.1070307@virginmedia.com> <20140619013722.GJ7742@ando> Message-ID: <1403206413.5548.YahooMailNeo@web163805.mail.gq1.yahoo.com> ----- Original Message ----- > From: Alex Kleider > To: tutor at python.org > Cc: > Sent: Thursday, June 19, 2014 8:10 PM > Subject: Re: [Tutor] Tips > > On 2014-06-18 18:37, Steven D'Aprano wrote: >> Python tries very hard to ensure that every module is loaded only once. >> (There are circumstances where you can fool it, but they're rare.) >> Since >> the module holds state (variables) and behaviour (functions), modules >> perform the same sort of role as classes, so a module which is loaded >> once is very similar to a singleton instance. In other words, if you >> want a class to implement singleton behaviour, you have to work at it. >> But if you shift the functionality from the class into a module, Python >> gives you singleton behaviour for free. >> >> But if you're not sure why anyone would want a singleton instance, I >> agree with you: most (but not all) uses of singletons are unnecessary. > > The idea of a singleton class is new to me as is this comparison of > class vs module. > Can anyone suggest a place to turn for more discussion of the topic? > thks, alexK Maybe this (it's about Singleton and Borg): http://stackoverflow.com/questions/1318406/why-is-the-borg-pattern-better-than-the-singleton-pattern-in-python From steve at pearwood.info Fri Jun 20 00:52:57 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 20 Jun 2014 08:52:57 +1000 Subject: [Tutor] Tips In-Reply-To: References: <53A16B68.1070307@virginmedia.com> <20140619013722.GJ7742@ando> Message-ID: <20140619225256.GQ7742@ando> On Thu, Jun 19, 2014 at 11:10:26AM -0700, Alex Kleider wrote: > The idea of a singleton class is new to me as is this comparison of > class vs module. > Can anyone suggest a place to turn for more discussion of the topic? > thks, alexK "Singleton" is one of the classic "design patterns", although these days people are equally divided on whether it's a design pattern or anti-pattern. The idea of design patterns is that they are a standard way of solving a certain type of problem. For example, in the real world, there are various problems which have a certain factor in common: Example problems: - Painting a mural on the ceiling. Building a house. Replacing a broken window on the 2nd story. Making a tree house. Class of problem: - There is work needed at a height well above what you can reach from the ground. General solution: - Use scaffolding to raise the height at which you can comfortably work. So "scaffolding" is the design pattern. Actual scaffolds may be made from many different materials (steel, timber, bamboo) and in many different shapes and sizes, but they're all scaffolds. Rather than there being a "one size fits all" solution for all problems, instead there is a general solution that you customize for the specific problem. The size and shape of the scaffolding needed to replace a broken window will be different than that needed to build a house. Design patterns for software are like scaffolds: a design pattern is not a language feature or function you can call, but a general technique to be used to solve a class of problems. https://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Explicit use of design patterns is very, very big in the Java and Dot Net worlds, less so in other programming languages. To learn more, Duck Duck Go is your friend: https://duckduckgo.com/html/?q=singleton+design+pattern https://duckduckgo.com/html/?q=singleton+anti-pattern If you still prefer Google: https://www.google.com/search?q=singleton+design+pattern or feel free to ask questions here. -- Steven From duxbuz at hotmail.com Fri Jun 20 10:38:52 2014 From: duxbuz at hotmail.com (Ian D) Date: Fri, 20 Jun 2014 08:38:52 +0000 Subject: [Tutor] write dictionary to file In-Reply-To: References: Message-ID: This is driving me nuts. I have tried many different things, but I just do not understand this csv library. I have tried passing various parameters to the writerow method and I am really getting nowhere fast. I just want to read from a file, join text to column and write to file. The writerow section I really do not understand, I copied an example as it seems to make zero sense when looking at examples or reading documentation. I thought the csv library would simplify the task but it seems to complicate it. I do not remember having this problem with sed/awk or perl. #so far this should read a file #using dictreader and take a column and join some text onto it import csv csvfile= open('StudentListToSort.csv', newline='') spamreader = csv.DictReader(csvfile,delimiter=',',quotechar='|') #open a file to write to later fields = ['user','first','last','password','year'] csvoutput = open('output.csv', 'wb+') spamwriter = csv.DictWriter(csvoutput,fieldnames=fields, delimiter=' ') for row in spamreader: if row['year'] == '40': username = row['user'] email = "".join([username,'@email.com]) output = row['user'], row['first'],row['last'],row['password'],row['year'] spamwriter.writerow([spamreader[fields] for fieldnames in fields]) print(output) From wprins at gmail.com Fri Jun 20 11:42:49 2014 From: wprins at gmail.com (Walter Prins) Date: Fri, 20 Jun 2014 10:42:49 +0100 Subject: [Tutor] write dictionary to file In-Reply-To: References: Message-ID: Hi, On 20 June 2014 09:38, Ian D wrote: > #so far this should read a file > #using dictreader and take a column and join some text onto it > > import csv > > csvfile= open('StudentListToSort.csv', newline='') > spamreader = csv.DictReader(csvfile,delimiter=',',quotechar='|') > > #open a file to write to later > fields = ['user','first','last','password','year'] > csvoutput = open('output.csv', 'wb+') > spamwriter = csv.DictWriter(csvoutput,fieldnames=fields, delimiter=' ') > > > > for row in spamreader: > if row['year'] == '40': > username = row['user'] > email = "".join([username,'@email.com]) > > output = row['user'], row['first'],row['last'],row['password'],row['year'] > spamwriter.writerow([spamreader[fields] for fieldnames in fields]) > print(output) Using DictReader and DictWriter means you retrieve and provide Python dict's when interacting with the CSV module. Maybe this is adding some confusion? Anyhow, here's a quick&dirty example modified from the source you posted which adds a column to an existing CSV file. (Initially I create the CSV just using a plain CSV writer. Then that file is read in and a column added to selected records and written out again.) # -*- coding: utf-8 -*- import csv def create_demo_file(csv_demo_filename): csvfile=open(csv_demo_filename, 'wb') csvw = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL) csvw.writerow(['user','first','last','password','year']) csvw.writerows([ (1, 'john', 'smith', 'LKJ?$_?(*$?', 35), (2, 'joe', 'bloggs','5^??J"HLLDD', 40), (3, 'alice','jones', '^%!*&^%1681', 43), (4, 'bob', 'white', '!&??JHLKJ*F', 28), ]) csvfile.close() def add_email_to_csv(csv_input_filename, csv_output_filename): csvfile= open(csv_input_filename) spamreader = csv.DictReader(csvfile) fields = ['user','first','last','password','year', 'email'] csvoutput = open(csv_output_filename, 'wb+') spamwriter = csv.DictWriter(csvoutput,fieldnames=fields) spamwriter.writeheader() for row in spamreader: if row['year'] in ('43', '40'): username = row['user'] row['email'] = username+'@email.com' spamwriter.writerow(row) csvoutput.close() ### create_demo_file('StudentListToSort.csv') print 'Demo input file created contents:' print open('StudentListToSort.csv', 'r').read() add_email_to_csv('StudentListToSort.csv', 'output.csv') print 'Demo output file created contents:' print open('output.csv', 'r').read() From duxbuz at hotmail.com Fri Jun 20 12:19:45 2014 From: duxbuz at hotmail.com (Ian D) Date: Fri, 20 Jun 2014 10:19:45 +0000 Subject: [Tutor] write dictionary to file In-Reply-To: References: , , Message-ID: Thanks for your help I am not much closer in understanding this so I am going to try and start with a simpler example for myself. I will try and write some values to a file as I am struggling even doing this. TypeError: 'str' does not support the buffer interface TypeError: 'tuple' does not support the buffer interface these are the errors I tend to hit. So until I can write a list or a tuple to a file I will not try and get to adventurous. From wprins at gmail.com Fri Jun 20 14:45:34 2014 From: wprins at gmail.com (Walter Prins) Date: Fri, 20 Jun 2014 13:45:34 +0100 Subject: [Tutor] write dictionary to file In-Reply-To: References: Message-ID: Hi, Firstly an apology -- I only just noticed your original code was Python 3 -- my example was Python 2, so there would be some changes required to make the example work on Python 3... On 20 June 2014 11:19, Ian D wrote: > Thanks for your help > > > I am not much closer in understanding this so I am going to try and start with a simpler example for myself. Yes, that's usually a good idea. > I will try and write some values to a file as I am struggling even doing this. > > > TypeError: 'str' does not support the buffer interface > > TypeError: 'tuple' does not support the buffer interface Could you post the lines of code that generates this exception and the full stack trace that goes with it? Nonetheless, having re-read your question and having googled a bit, it seems that your problem might be related to Python 2 vs. Python 3, see here: http://stackoverflow.com/questions/24294457/python-typeerror-str-does-not-support-the-buffer-interface In short: In Python 2 you are expected to open the CSV file in binary mode ('wb'). In Python 3 this should be text mode as per the above question, else you'll only be able to write "bytes" streams, hence the "buffer" interface errors. If you've perhaps been cribbing/using Python 2.x examples and documentation while in fact using using Python 3, then that would help explain the confusion...? Walter From duxbuz at hotmail.com Fri Jun 20 15:55:27 2014 From: duxbuz at hotmail.com (Ian D) Date: Fri, 20 Jun 2014 13:55:27 +0000 Subject: [Tutor] write dictionary to file In-Reply-To: References: , , , , Message-ID: Thanks > > Nonetheless, having re-read your question and having googled a bit, it > seems that your problem might be related to Python 2 vs. Python 3, see > here: > http://stackoverflow.com/questions/24294457/python-typeerror-str-does-not-support-the-buffer-interface > > In short: In Python 2 you are expected to open the CSV file in binary > mode ('wb'). In Python 3 this should be text mode as per the above > question, else you'll only be able to write "bytes" streams, hence the > "buffer" interface errors. If you've perhaps been cribbing/using > Python 2.x examples and documentation while in fact using using Python > 3, then that would help explain the confusion...? Ok I see this error and the example shows a different type of syntax. Rather than a file open for writing: outfile = open('output.csv', 'wb') it uses with open('data.csv', 'w', newline='') as out: now is this written differently in order to implement this text mode thing or is it just the omission of the 'b' on the 'wb' that causes text mode? and if so could it be written: outfile = open('output.csv', 'w') ? From duxbuz at hotmail.com Fri Jun 20 16:01:32 2014 From: duxbuz at hotmail.com (Ian D) Date: Fri, 20 Jun 2014 14:01:32 +0000 Subject: [Tutor] write dictionary to file In-Reply-To: References: , , , , , , , , , Message-ID: Ok making some progress by changing the 'wb' to 'w' > > Ok I see this error and the example shows a different type of syntax. > > > Rather than a file open for writing: > > outfile = open('output.csv', 'wb') > > > it uses > > with open('data.csv', 'w', newline='') as out: > > > > now is this written differently in order to implement this text mode thing or is it just the omission of the 'b' on the 'wb' that causes text mode? > > > > and if so could it be written: > > outfile = open('output.csv', 'w') > > > From duxbuz at hotmail.com Fri Jun 20 16:11:32 2014 From: duxbuz at hotmail.com (Ian D) Date: Fri, 20 Jun 2014 14:11:32 +0000 Subject: [Tutor] write dictionary to file In-Reply-To: References: , ,,, ,,, ,,, , , , , , Message-ID: > > Ok making some progress by changing the 'wb' to 'w' > err no. unstuck again. import csv csvfile= open('StudentListToSort.csv', newline='') spamreader = csv.reader(csvfile,delimiter=',',quotechar='|') outfile = open('outfile.csv','w') for row in spamreader: if row[4] == '6': print("".join([row[0],'@email.com']),row[1]) email = "".join([row[0],'@email.com']) output = email,row[1] outfile.write(output) outfile.close() when I start to concatenate the results, it ends up as a Tuple and the write to file stuff doesn't like Tuples TypeError: must be str, not tuple From breamoreboy at yahoo.co.uk Fri Jun 20 17:54:21 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 20 Jun 2014 16:54:21 +0100 Subject: [Tutor] write dictionary to file In-Reply-To: References: , , , , , , , , , , , , , , , Message-ID: On 20/06/2014 15:11, Ian D wrote: > >> >> Ok making some progress by changing the 'wb' to 'w' >> > > err no. > > unstuck again. > > import csv > > csvfile= open('StudentListToSort.csv', newline='') > spamreader = csv.reader(csvfile,delimiter=',',quotechar='|') > outfile = open('outfile.csv','w') > > for row in spamreader: > > if row[4] == '6': > print("".join([row[0],'@email.com']),row[1]) > email = "".join([row[0],'@email.com']) > output = email,row[1] > outfile.write(output) > > outfile.close() > > when I start to concatenate the results, it ends up as a Tuple and the write to file stuff doesn't like Tuples > > TypeError: must be str, not tuple > Please give the full traceback, not just the last line, as it gives us a lot more information. As it happens you're creating a tuple when you assign output, as it's the comma that makes a tuple, so unless I've missed something there's your problem. Further advice is to slow down a bit, remember more haste, less speed. Walking away from the problem for a few minutes to clear your head often works miracles :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From wprins at gmail.com Fri Jun 20 18:48:57 2014 From: wprins at gmail.com (Walter Prins) Date: Fri, 20 Jun 2014 17:48:57 +0100 Subject: [Tutor] write dictionary to file In-Reply-To: References: Message-ID: Hi, You've had a very good reply from Mark already however I want to add to it and further clarify what he pointed out (why exactly *are* you getting the tuple error after all?), also I've updated the prior example to help explain, see below: On 20 June 2014 15:11, Ian D wrote: > > import csv > > csvfile= open('StudentListToSort.csv', newline='') > spamreader = csv.reader(csvfile,delimiter=',',quotechar='|') > outfile = open('outfile.csv','w') > > for row in spamreader: > > if row[4] == '6': > print("".join([row[0],'@email.com']),row[1]) > email = "".join([row[0],'@email.com']) > output = email,row[1] > outfile.write(output) ... Note that you're writing using the ///plain file object itself///, which indeed would expect a simple string to write. Obviously a plain file object, such as outfile, doesn't know by itself how to write a tuple of objects, that's more the CSV object's job, hence you get the error you're getting. Instead you want to be writing using a CSV object instead, as you originally indeed were doing. I suspect you simply forgot to use the CSV writer and accidentally tried to write the output tuple directly with the file object? So I'm with Mark -- less haste, more speed. :) Finally I've updated the previous example to work with Python 3 and added some comments, so you should be able to run it without problems. Hopefully this should be enough to get you going. :) # -*- coding: utf-8 -*- import csv def create_demo_file(csv_demo_filename): csvfile=open(csv_demo_filename, 'w', newline='') #Here we instantiate a CSV writer that accepts plain list like objects for #writing, and then use it to write some demo data by passing it a list of #tuples. csvwriter = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL) csvwriter.writerow(['user','first','last','password','year']) csvwriter.writerows([ (1, 'john', 'smith', 'LKJ?$_?(*$?', 35), (2, 'joe', 'bloggs','5^??J"HLLDD', 40), (3, 'alice','jones', '^%!*&^%1681', 43), (4, 'bob', 'white', '!&??JHLKJ*F', 28), ]) csvfile.close() def add_email_to_csv(csv_input_filename, csv_output_filename): csvfile= open(csv_input_filename) csvoutput = open(csv_output_filename, 'w+', newline='') #Here we instantiate a CSV reader that gives us each row as a dict object #(as opposed to one which simply gives the values as a plain list) csvreader = csv.DictReader(csvfile) fields = ['user','first','last','password','year', 'email'] #Here we instantiate a csv writer that accepts dicts for writing. #We pass it the formal field list as part of the constructor params #and ask it to write the header line as a first step. csvwriter = csv.DictWriter(csvoutput,fieldnames=fields) csvwriter.writeheader() #Step through each row dict object... for row in csvreader: if row['year'] in ('43', '40'): #Again: The row is a dict, containing an entry for every column in the table. #We cheat, and simply add a new column to this existing dict, # by simply assigning to the dict as normal. Then we ask the CSV output # writer to write this (now modified) dict as the row to the output file: row['email'] = row['user']+'@email.com' csvwriter.writerow(row) csvoutput.close() ### create_demo_file('StudentListToSort.csv') print('Demo input file created contents:') print(open('StudentListToSort.csv', 'r').read() ) add_email_to_csv('StudentListToSort.csv', 'output.csv') print('Demo output file created contents:') print(open('output.csv', 'r').read()) HTH, Walter From akleider at sonic.net Sat Jun 21 02:45:12 2014 From: akleider at sonic.net (Alex Kleider) Date: Fri, 20 Jun 2014 17:45:12 -0700 Subject: [Tutor] Fwd: Re: Tips Message-ID: On 2014-06-19 15:52, Steven D'Aprano wrote: > On Thu, Jun 19, 2014 at 11:10:26AM -0700, Alex Kleider wrote: > >> The idea of a singleton class is new to me as is this comparison of >> class vs module. >> Can anyone suggest a place to turn for more discussion of the topic? >> thks, alexK > > "Singleton" is one of the classic "design patterns", although these > days > people are equally divided on whether it's a design pattern or > anti-pattern. > > The idea of design patterns is that they are a standard way of solving > a > certain type of problem. For example, in the real world, there are > various problems which have a certain factor in common: > > Example problems: > - Painting a mural on the ceiling. Building a house. Replacing > a broken window on the 2nd story. Making a tree house. > > Class of problem: > - There is work needed at a height well above what you can > reach from the ground. > > General solution: > - Use scaffolding to raise the height at which you can > comfortably work. > > So "scaffolding" is the design pattern. Actual scaffolds may be made > from many different materials (steel, timber, bamboo) and in many > different shapes and sizes, but they're all scaffolds. Rather than > there > being a "one size fits all" solution for all problems, instead there is > a general solution that you customize for the specific problem. The > size > and shape of the scaffolding needed to replace a broken window will be > different than that needed to build a house. > > Design patterns for software are like scaffolds: a design pattern is > not > a language feature or function you can call, but a general technique to > be used to solve a class of problems. > > https://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 > > Explicit use of design patterns is very, very big in the Java and > Dot Net worlds, less so in other programming languages. > > > To learn more, Duck Duck Go is your friend: > > https://duckduckgo.com/html/?q=singleton+design+pattern > https://duckduckgo.com/html/?q=singleton+anti-pattern > > If you still prefer Google: > > https://www.google.com/search?q=singleton+design+pattern > > or feel free to ask questions here. You've got me thinking but I'm still at a loss as to what questions need asked! The only applicability that fits in with anything I've experienced has to do with the necessity of globals to represent command line parameters which must be read at the outset and then be available to direct program execution. Thanks for the "tip." alex From steve at pearwood.info Sat Jun 21 06:35:16 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 21 Jun 2014 14:35:16 +1000 Subject: [Tutor] write dictionary to file In-Reply-To: References: Message-ID: <20140621043515.GR7742@ando> On Fri, Jun 20, 2014 at 08:38:52AM +0000, Ian D wrote: > This is driving me nuts. > > I have tried many different things, but I just do not understand this > csv library. Have you tried reading the documentation? It sounds like you're just throwing random bits of code at it and hoping something works. A better approach is to slow down and try to understand what the csv is doing, what it expects from you, and how you can best use it. Ask *focused* questions, rather than just blast us with blobs of code over and over again. > #so far this should read a file > #using dictreader and take a column and join some text onto it > > import csv > > csvfile= open('StudentListToSort.csv', newline='') > spamreader = csv.DictReader(csvfile,delimiter=',',quotechar='|') Are you sure that your input file uses | as a quote character and , as the field delimiter? > #open a file to write to later > fields = ['user','first','last','password','year'] > csvoutput = open('output.csv', 'wb+') I'm pretty sure you don't want to use "wb+" mode. Since you're using Python 3, I think you should just use "w" mode. The "b" turns on binary mode, and in Python 3 you don't want that. The "+" turns on either "read/write" mode or "append" mode, I don't remember which, but either way I don't think it's necessary for what you are doing. > spamwriter = csv.DictWriter(csvoutput,fieldnames=fields, delimiter=' ') Now you're turning every , delimiter into a space. Are you sure you want that? > for row in spamreader: > if row['year'] == '40': > username = row['user'] > email = "".join([username,'@email.com]) Syntax error: you left out the closing single quote. You need: email = "".join([username,'@email.com']) Other than that, so far so good. You're going through each row of the input file, extracting the "year" field, checking if it equals "40", and if it is, creating an email address from the user field. Perhaps a shorter way to do this would be: email = row['user'] + '@email.com' rather than mucking about with "".join for only two pieces of text. > output = row['user'], row['first'],row['last'],row['password'],row['year'] I'd move the print(output) line directly under this line, so it will print the output even if the following line fails. > spamwriter.writerow([spamreader[fields] for fieldnames in fields]) > print(output) I'm sure the writerow line is completely wrong :-( It expects a dict {fieldname: fieldvalue} not a list [fieldvalue]. And you extract the field values through row, not through the spamreader object directly. Try this instead: spamwriter.writerow({name: row[name] for name in fields}) Let me put all those changes together, plus a few more fixes. Take note of my comments. import csv # Open the file we're reading from. csvfile= open('StudentListToSort.csv', newline='') # Open a file to write to. csvoutput = open('output.csv', 'w', newline='') fields = ['user', 'first', 'last', 'password', 'year'] # Are you sure you want | as the quote character? spamreader = csv.DictReader(csvfile, delimiter=',', quotechar='|') # Still using , as a delimiter, not space. spamwriter = csv.DictWriter(csvoutput, fieldnames=fields, delimiter=',') for row in spamreader: if row['year'] == '40': email = row['user'] + '@email.com' output = [ row[fieldname] for fieldname in fields ] print(output) # DictWriter needs a dict, not a list. spamwriter.writerow({name: row[name] for name in fields}) print("Warning: email calculated but never used:", email) # Good practice is to close the files when done. csvfile.close() csvoutput.close() Try that. If you get any errors, please COPY AND PASTE the ***entire*** traceback, not just the last line. -- Steven From alan.gauld at btinternet.com Sat Jun 21 19:23:04 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 21 Jun 2014 18:23:04 +0100 Subject: [Tutor] Fwd: Re: Tips In-Reply-To: References: Message-ID: On 21/06/14 01:45, Alex Kleider wrote: > The only applicability that fits in with anything I've experienced has > to do with the necessity of globals to represent command line parameters That's not a necessity and I hardly ever do that... :-) But it is one pattern for handling CL args. Other common patterns are for state machines. That's where a system responds to events differently depending on what events have already been seen. Another is GUIs where the most common pattern is known as Model-View-Controller. The implementation of MVC varies greatly between languages, toolkits and technologies (Web v Desktop, say) But they all use MVC as the pattern. There are many others especially in the Smalltalk and Java communities where the nature of the languages as well as their community culture tend to restrict the solution choices somewhat (although each in very different ways). There are various pattern web sites. Wikipedia is a good starting point: http://en.wikipedia.org/wiki/Software_design_pattern -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From codemonkey at inbox.com Sat Jun 21 01:59:28 2014 From: codemonkey at inbox.com (Deb Wyatt) Date: Fri, 20 Jun 2014 15:59:28 -0800 Subject: [Tutor] Strange issue output not showing up as floats Message-ID: Hello. I hope everyone is having a good day. I am working on an assignment that is supposed to output floats. I'm using floats in the computations and according to Python's rules the output should be floats, but it's not. When I test in Python shell the calculations display correctly, as floats. I'm using Python 2.7 for this assignment. input is as follows (not in code order): balance = 4213 annualInterestRate = 0.2 monthlyPaymentRate = 0.04 payment = monthlyPaymentRate * balance balance = balance - payment total_paid = payment output looks like this: Month: 1 Minimum monthly payment: 168 Remaining balance: 4111 Month: 2 Minimum monthly payment: 164 Remaining balance: 4013 Month: 3 Minimum monthly payment: 160 Remaining balance: 3916 ...etc... Would any of you have a clue what could be wrong? Deb in WA, USA ____________________________________________________________ FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop! Check it out at http://www.inbox.com/earth From codemonkey at inbox.com Sat Jun 21 02:05:14 2014 From: codemonkey at inbox.com (Deb Wyatt) Date: Fri, 20 Jun 2014 16:05:14 -0800 Subject: [Tutor] regarding my strange issue output not showing up as floats Message-ID: never mind. I figured it out. I was using %d instead of %f in my print statements. duh. Deb in WA, USA ____________________________________________________________ FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family! Visit http://www.inbox.com/photosharing to find out more! From breamoreboy at yahoo.co.uk Sat Jun 21 19:48:43 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 21 Jun 2014 18:48:43 +0100 Subject: [Tutor] Fwd: Re: Tips In-Reply-To: References: Message-ID: On 21/06/2014 18:23, Alan Gauld wrote: > On 21/06/14 01:45, Alex Kleider wrote: > >> The only applicability that fits in with anything I've experienced has >> to do with the necessity of globals to represent command line parameters > > That's not a necessity and I hardly ever do that... :-) > But it is one pattern for handling CL args. > > Other common patterns are for state machines. > That's where a system responds to events differently depending > on what events have already been seen. > > Another is GUIs where the most common pattern is known as > Model-View-Controller. The implementation of MVC varies > greatly between languages, toolkits and technologies > (Web v Desktop, say) But they all use MVC as the pattern. > > There are many others especially in the Smalltalk and Java > communities where the nature of the languages as well as > their community culture tend to restrict the solution > choices somewhat (although each in very different ways). > > There are various pattern web sites. Wikipedia is a good > starting point: > > http://en.wikipedia.org/wiki/Software_design_pattern > Try adding Alex Martelli when looking for anything about Python patterns. He's one of those people who've forgotten more about Python than I've ever learned, the @#$%^&* :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From jslozier at gmail.com Sat Jun 21 20:29:35 2014 From: jslozier at gmail.com (Jay Lozier) Date: Sat, 21 Jun 2014 14:29:35 -0400 Subject: [Tutor] Strange issue output not showing up as floats In-Reply-To: References: Message-ID: <53A5CF0F.5020600@gmail.com> On 06/20/2014 07:59 PM, Deb Wyatt wrote: > Hello. I hope everyone is having a good day. I am working on an assignment that is supposed to output floats. I'm using floats in the computations and according to Python's rules the output should be floats, but it's not. When I test in Python shell the calculations display correctly, as floats. > > I'm using Python 2.7 for this assignment. > > input is as follows (not in code order): > balance = 4213 > annualInterestRate = 0.2 > monthlyPaymentRate = 0.04 > payment = monthlyPaymentRate * balance > balance = balance - payment > total_paid = payment > > output looks like this: > > Month: 1 > Minimum monthly payment: 168 > Remaining balance: 4111 > Month: 2 > Minimum monthly payment: 164 > Remaining balance: 4013 > Month: 3 > Minimum monthly payment: 160 > Remaining balance: 3916 > ...etc... > > Would any of you have a clue what could be wrong? > Deb in WA, USA > Deb, Can you show us your full code listing or if it is lengthy the relevant section showing the print statement. My guess is your print formating is not specified correctly and it is rounding to the nearest whole number. Jay -- Jay Lozier jslozier at gmail.com From alan.gauld at btinternet.com Sat Jun 21 22:29:15 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 21 Jun 2014 21:29:15 +0100 Subject: [Tutor] Strange issue output not showing up as floats In-Reply-To: References: Message-ID: On 21/06/14 00:59, Deb Wyatt wrote: I see you solved it but for future reference... > I'm using Python 2.7 for this assignment. > > input is as follows (not in code order): > balance = 4213 > annualInterestRate = 0.2 > monthlyPaymentRate = 0.04 > payment = monthlyPaymentRate * balance > balance = balance - payment > total_paid = payment Not in code order is bad. We want to see the code - and order matters. > output looks like this: > > Month: 1 > Minimum monthly payment: 168 > Remaining balance: 4111 You show the output but didn't show us how the output was produced. We had no chance of guessing what you were doing wrong. If you want help you need to help us by showing us the code. Don't guess at what might be wrong. If its less that, say, 100 lines post the real code and post any errors. > Would any of you have a clue what could be wrong? > Deb in WA, USA Not if you don't show us your code! > never mind. I figured it out. I was using %d instead > of %f in my print statements. duh. 'duh', indeed but how could we possibly have helped since you didn't post any print statements? In future post real code, real output and real errors. That way we have some chance of helping you. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From breamoreboy at yahoo.co.uk Sun Jun 22 01:02:50 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 22 Jun 2014 00:02:50 +0100 Subject: [Tutor] regarding my strange issue output not showing up as floats In-Reply-To: References: Message-ID: On 21/06/2014 01:05, Deb Wyatt wrote: > never mind. I figured it out. I was using %d instead of %f in my print statements. duh. > > Deb in WA, USA > A good job as it would have been rather difficult from your original post, as Jay Lozier and Alan Gauld have pointed out. So please check this out http://sscce.org/ as it's a very handy reference. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From linlifeng at gmail.com Sun Jun 22 07:18:11 2014 From: linlifeng at gmail.com (Lifeng Lin) Date: Sun, 22 Jun 2014 00:18:11 -0500 Subject: [Tutor] empty gfx module? Message-ID: Hi Everyone! I am recently trying to process a gfx file using python. gfx module does not come default, and with some struggle, i seem to have succeeded in installing the module. However, when I import it, no methods or attributes showed up. here are some codes: ####### import gfx handle = gfx.open("pdf", "sample.pdf") ######## I got the error: AttributeError: 'module' object has no attribute 'open" when i do "dir(gfx)" nothing but the usual suspect showed up. Help? best, Lifeng... -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sun Jun 22 10:32:05 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 22 Jun 2014 18:32:05 +1000 Subject: [Tutor] empty gfx module? In-Reply-To: References: Message-ID: <20140622083205.GS7742@ando> On Sun, Jun 22, 2014 at 12:18:11AM -0500, Lifeng Lin wrote: > Hi Everyone! > > I am recently trying to process a gfx file using python. > gfx module does not come default, and with some struggle, i seem to have > succeeded in installing the module. However, when I import it, no methods > or attributes showed up. Make sure you haven't shadowed the gfx module with a file of your own called "gfx.py" or "gfx.pyc". You can check which file Python sees by doing this: import gfx print(gfx.__file__) -- Steven From alan.gauld at btinternet.com Sun Jun 22 17:08:49 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 22 Jun 2014 16:08:49 +0100 Subject: [Tutor] empty gfx module? In-Reply-To: References: Message-ID: On 22/06/14 06:18, Lifeng Lin wrote: > Hi Everyone! > > I am recently trying to process a gfx file using python. > gfx module does not come default, and with some struggle, i seem to have > succeeded in installing the module. Steven has suggested one possible cause. However this list is for the Python language and standard library. The best place to get help on specific modules is from the author or their specific mailing list/forum. Failing that the main Python list may have more likelihood of another user being able to help. Using the tutor list you are reducing your odds of finding someone who can help. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From codemonkey at inbox.com Mon Jun 23 02:32:40 2014 From: codemonkey at inbox.com (Deb Wyatt) Date: Sun, 22 Jun 2014 16:32:40 -0800 Subject: [Tutor] Strange issue output not showing up as floats In-Reply-To: References: Message-ID: > > Not if you don't show us your code! > > > never mind. I figured it out. I was using %d instead > > of %f in my print statements. duh. > > 'duh', indeed but how could we possibly have helped since > you didn't post any print statements? > > In future post real code, real output and real errors. > That way we have some chance of helping you. > > -- I know, i'm sorry. I was avoiding posting the actual code because it was an assignment, and I did not want this to be construed as cheating. I was just looking for possible ideas of what could possibly be wrong, but not posting the print statement made it impossible for any of you to see my 'duh' error. I was really hoping the moderator would see the second message and not send either message to the group. How long does it take to get off of moderation? Deb in WA, USA ____________________________________________________________ FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop! Check it out at http://www.inbox.com/earth From mangalgishreyas at gmail.com Mon Jun 23 07:56:33 2014 From: mangalgishreyas at gmail.com (Shreyas Mangalgi) Date: Mon, 23 Jun 2014 11:26:33 +0530 Subject: [Tutor] Regarding NDEF URL transfer to arduino using python Message-ID: I want to be able to send URI in NDEF format to my Arduino UNO which is connected to Sony's RC-S801 dynamic NFC tag.I used Hercules setup utility to send "100101000300000000000000001b0030d102165370d1011255017374757474676172742e736f6e792e64650000000000" for http://www.stuttgart.sony.de and I was able to read it from my NFC enabled phone.But when I used the following python code, it didn't work : import serialimport time arduino = serial.Serial('COM6', 115200) arduino.parity = 'M'print arduino print("writing") input = ("100101000300000000000000001b0030d102165370d1011255017374757474676172742e736f6e792e64650000000000") arduino.write(input) time.sleep(5)print("stopped writing") arduino.close() It shows as an empty record.Can you suggest changes to my python code that I should make in order to detect the NDEF message ?Here is the screenshot of the message that I get when I read the tag from my phone: http://i.imgur.com/xn78Sw1.png Thanks Shreyas -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Jun 23 09:51:29 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 23 Jun 2014 08:51:29 +0100 Subject: [Tutor] Strange issue output not showing up as floats In-Reply-To: References: Message-ID: On 23/06/14 01:32, Deb Wyatt wrote: >> In future post real code, real output and real errors. >> That way we have some chance of helping you. >> -- > I know, i'm sorry. I was avoiding posting the actual > code because it was an assignment, Its even more important for assignments because without real code we will tend to assume you haven't tried to do it. If we can see that you have made a real attempt to solve it then we will help fix the problem. So always post the relevant code. > I was really hoping the moderator would see the second > message and not send either message to the group. Unfortunately the second came in just after I'd sent the first... > How long does it take to get off of moderation? Until I notice that somebody is cropping up regularly. :-) You are now off moderation. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From duxbuz at hotmail.com Mon Jun 23 10:07:35 2014 From: duxbuz at hotmail.com (Ian D) Date: Mon, 23 Jun 2014 08:07:35 +0000 Subject: [Tutor] write dictionary to file In-Reply-To: <20140621043515.GR7742@ando> References: , , <20140621043515.GR7742@ando> Message-ID: Thanks a lot this is really helpful as have been the other posts. > > Have you tried reading the documentation? It sounds like you're just > throwing random bits of code at it and hoping something works. > > A better approach is to slow down and try to understand what the csv is > doing, what it expects from you, and how you can best use it. Ask > *focused* questions, rather than just blast us with blobs of code over > and over again. > I do actually try and read this stuff and understand it, but when its new it really is not so simple. When you already have a sound footing and experience it becomes elementary and its hard to understand why you could not understand it in the first place. Admittedly I do usually try and botch together code from examples. When working for solutions sometimes time becomes an issue, and sometimes shortcuts pay off, but in the long run they never really do. I appreciate your patience and your help I am going to try this example now From duxbuz at hotmail.com Mon Jun 23 11:17:44 2014 From: duxbuz at hotmail.com (Ian D) Date: Mon, 23 Jun 2014 09:17:44 +0000 Subject: [Tutor] write dictionary to file In-Reply-To: <20140621043515.GR7742@ando> References: , , <20140621043515.GR7742@ando> Message-ID: >> >> import csv >> >> csvfile= open('StudentListToSort.csv', newline='') >> spamreader = csv.DictReader(csvfile,delimiter=',',quotechar='|') > > Are you sure that your input file uses | as a quote character and , as > the field delimiter? No I overlooked this > > >> #open a file to write to later >> fields = ['user','first','last','password','year'] >> csvoutput = open('output.csv', 'wb+') > > I'm pretty sure you don't want to use "wb+" mode. Since you're using > Python 3, I think you should just use "w" mode. > > The "b" turns on binary mode, and in Python 3 you don't want that. The > "+" turns on either "read/write" mode or "append" mode, I don't remember > which, but either way I don't think it's necessary for what you are > doing. Yes I came across this python 3 idiosyncrasy > > >> spamwriter = csv.DictWriter(csvoutput,fieldnames=fields, delimiter=' ') > > Now you're turning every , delimiter into a space. Are you sure you want > that? > > Overlooked this >> for row in spamreader: >> if row['year'] == '40': >> username = row['user'] >> email = "".join([username,'@email.com]) > > Syntax error: you left out the closing single quote. You need: > > email = "".join([username,'@email.com']) > I think I may have messed that up editing code for public viewing. On to your example..... > > import csv > > # Open the file we're reading from. > csvfile= open('StudentListToSort.csv', newline='') > # Open a file to write to. > csvoutput = open('output.csv', 'w', newline='') > > fields = ['user', 'first', 'last', 'password', 'year'] > > # Are you sure you want | as the quote character? > spamreader = csv.DictReader(csvfile, delimiter=',', quotechar='|') > > > # Still using , as a delimiter, not space. > spamwriter = csv.DictWriter(csvoutput, fieldnames=fields, delimiter=',') > > for row in spamreader: > if row['year'] == '40': > email = row['user'] + '@email.com' > output = [ row[fieldname] for fieldname in fields ] I am unsure about this syntax [ row[fieldname] for fieldname in fields ] The FOR loop is not in any context I have used before. I have seen examples(recently) so its obviously standard practice, but not something I would ever think to type. > print(output) > # DictWriter needs a dict, not a list. > spamwriter.writerow({name: row[name] for name in fields}) > print("Warning: email calculated but never used:", email) And this writerow syntax is something new for me, as are dictionaries( which I have tried to read up and understand.) >spamwriter.writerow({name: row[name] for name in fields}) This looks like the same loop as the one above but a dictionary using curly braces(for dict), its the same unfamiliar way of writing a FOR loop for me. So if I wanted multiple rows in the output csv file I would try: ({name: row[name], row[email, row[first] for name in fields}) which doesn't work as the syntax is invalid, so what would I need to change to allow spamwriter.writerow to generate multiple fields? From alan.gauld at btinternet.com Mon Jun 23 11:25:55 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 23 Jun 2014 10:25:55 +0100 Subject: [Tutor] Regarding NDEF URL transfer to arduino using python In-Reply-To: References: Message-ID: On 23/06/14 06:56, Shreyas Mangalgi wrote: > I want to be able to send URI in NDEF format to my Arduino UNO which is > connected to Sony's RC-S801 dynamic NFC tag. I have no idea what most of that means in practice. This looks very Arduino specific so you will probably get better results asking on an Arduino forum rather than a Python language list. However... > utility to send > > |"100101000300000000000000001b0030d102165370d1011255017374757474676172742e736f6e792e64650000000000"| This is a string of hex digits. Do you mean to send the character representations of the hex or do you mean to send the actual hex digits? > arduino= serial.Serial('COM6', 115200) > arduino.parity= 'M' > print arduino > print("writing") > input= ("100101000300000000000000001b0030d102165370d1011255017374757474676172742e736f6e792e64650000000000") You don't need the parentheses, they do nothing here. But again this is a string representation of the hex data, is that really what you want? > It shows as an empty record.Can you suggest changes to my python code > that I should make in order to detect the NDEF message ?Here is the > screenshot of the message that I get when I read the tag from my phone: > > http://i.imgur.com/xn78Sw1.png Posting images on a text based mailing list is not usually helpful. If you can post the text of the message that is better. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Mon Jun 23 11:44:30 2014 From: __peter__ at web.de (Peter Otten) Date: Mon, 23 Jun 2014 11:44:30 +0200 Subject: [Tutor] write dictionary to file References: <20140621043515.GR7742@ando> Message-ID: Ian, take a step back; the first step to solve a problem is to state it clearly. You want to write a dict to a file. (1) What would the dictionary look like? Give concrete example, e. g. {"foo": 42, "bar": 'that\'s not "funny"'} (2) What should the resulting file look like when you open it in a text editer? Example: foo,bar 42,"that's not ""funny""" Only then you should proceed to write code. From duxbuz at hotmail.com Mon Jun 23 11:50:04 2014 From: duxbuz at hotmail.com (Ian D) Date: Mon, 23 Jun 2014 09:50:04 +0000 Subject: [Tutor] write dictionary to file In-Reply-To: References: , , , <20140621043515.GR7742@ando>, Message-ID: Ok I used : spamwriter.writerow({'user':email,'first':row['first'],'last':row['last'], 'password':row['password'] }) Seems to be almost finished thanks ---------------------------------------- > From: duxbuz at hotmail.com > To: tutor at python.org > Date: Mon, 23 Jun 2014 09:17:44 +0000 > Subject: Re: [Tutor] write dictionary to file > > >>> >>> import csv >>> >>> csvfile= open('StudentListToSort.csv', newline='') >>> spamreader = csv.DictReader(csvfile,delimiter=',',quotechar='|') >> >> Are you sure that your input file uses | as a quote character and , as >> the field delimiter? > > > No I overlooked this > > >> >> >>> #open a file to write to later >>> fields = ['user','first','last','password','year'] >>> csvoutput = open('output.csv', 'wb+') >> >> I'm pretty sure you don't want to use "wb+" mode. Since you're using >> Python 3, I think you should just use "w" mode. >> >> The "b" turns on binary mode, and in Python 3 you don't want that. The >> "+" turns on either "read/write" mode or "append" mode, I don't remember >> which, but either way I don't think it's necessary for what you are >> doing. > > > Yes I came across this python 3 idiosyncrasy > > > >> >> >>> spamwriter = csv.DictWriter(csvoutput,fieldnames=fields, delimiter=' ') >> >> Now you're turning every , delimiter into a space. Are you sure you want >> that? >> >> > > > Overlooked this > > > >>> for row in spamreader: >>> if row['year'] == '40': >>> username = row['user'] >>> email = "".join([username,'@email.com]) >> >> Syntax error: you left out the closing single quote. You need: >> >> email = "".join([username,'@email.com']) >> > > > I think I may have messed that up editing code for public viewing. > > > > > On to your example..... > > > >> >> import csv >> >> # Open the file we're reading from. >> csvfile= open('StudentListToSort.csv', newline='') >> # Open a file to write to. >> csvoutput = open('output.csv', 'w', newline='') >> >> fields = ['user', 'first', 'last', 'password', 'year'] >> >> # Are you sure you want | as the quote character? >> spamreader = csv.DictReader(csvfile, delimiter=',', quotechar='|') > >> >> >> # Still using , as a delimiter, not space. >> spamwriter = csv.DictWriter(csvoutput, fieldnames=fields, delimiter=',') >> >> for row in spamreader: >> if row['year'] == '40': >> email = row['user'] + '@email.com' >> output = [ row[fieldname] for fieldname in fields ] > > > I am unsure about this syntax [ row[fieldname] for fieldname in fields ] > > > > The FOR loop is not in any context I have used before. I have seen examples(recently) so its obviously standard practice, but not something I would ever think to type. > > > >> print(output) >> # DictWriter needs a dict, not a list. >> spamwriter.writerow({name: row[name] for name in fields}) >> print("Warning: email calculated but never used:", email) > > > > > And this writerow syntax is something new for me, as are dictionaries( which I have tried to read up and understand.) > > > >>spamwriter.writerow({name: row[name] for name in fields}) > > > > This looks like the same loop as the one above but a dictionary using curly braces(for dict), its the same unfamiliar way of writing a FOR loop for me. > > > > > So if I wanted multiple rows in the output csv file I would try: > > > ({name: row[name], row[email, row[first] for name in fields}) > > > which doesn't work as the syntax is invalid, so what would I need to change to allow spamwriter.writerow to generate multiple fields? > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From steve at pearwood.info Mon Jun 23 16:54:30 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 24 Jun 2014 00:54:30 +1000 Subject: [Tutor] write dictionary to file In-Reply-To: References: <20140621043515.GR7742@ando> Message-ID: <20140623145430.GW7742@ando> On Mon, Jun 23, 2014 at 09:17:44AM +0000, Ian D wrote: > > for row in spamreader: > > if row['year'] == '40': > > email = row['user'] + '@email.com' > > output = [ row[fieldname] for fieldname in fields ] > > I am unsure about this syntax [ row[fieldname] for fieldname in fields ] That's called a "list comprehension". It is a way of building up a list as a single expression. Let's start with the old-fashioned way to build a list: output = [] for fieldname in fields: output.append( row[fieldname] ) Or, we can do exactly the same thing, in a single line, with a list comprehension, and get rid of the temporary variables: output = [row[fieldname] for fieldname in fields] If you remember your high school maths classes, the form of the list comp is rather close to that of mathematical set builder notation: http://www.mathsisfun.com/sets/set-builder-notation.html Here is a set builder notation, and its English translation: {2n+1 : n ? {1,2,3,4}} This reads as "the set of 2n plus 1, such that n is an element of {1,2,3,4}". Don't be confused by the fact that there is a set inside a set -- this just tells us how to build a new set from an old set. So we start with a set {1,2,3,4}, and let n equal each of those numbers in turn. Then we calculate 2n+1, and use that inside the new set we're creating: n = 1, so 2n+1 => 3 n = 2, so 2n+1 => 5 n = 3, so 2n+1 => 7 n = 4, so 2n+1 => 9 and so the final result is the set {3, 5, 7, 9}. Enough about sets and mathematics! Now we're going to talk about Python! Python uses the same sort of expression, except it builds a list, not a set, and calls it a "list comprehension" instead of a "list builder". (Don't blame me for that, blame the Haskell programming language which invented this.) So, we start with the mathematical set builder: {2n+1 : n ? {1,2,3,4}} Turn the sets into lists: [2n+1 : n ? [1,2,3,4]] Use Python syntax for the formula: [2*n+1 : n ? [1,2,3,4]] Replace the "such that" and "element of" with "for" "in": [2*n+1 for n in [1,2,3,4]] and now you have a Python list comprehension. If you copy and paste that into a Python interpreter, you'll see this: py> [2*n+1 for n in [1,2,3,4]] [3, 5, 7, 9] > > # DictWriter needs a dict, not a list. > > spamwriter.writerow({name: row[name] for name in fields}) > > And this writerow syntax is something new for me, as are dictionaries > (which I have tried to read up and understand.) List comprehensions were added to Python in, I think, version 2.2, which was about ten years ago. They turned out to be so useful and popular that now, in Python 3, we have FOUR different kinds of "comprehensions" or builder syntax: List comprehensions: [2*n+1 for n in [1,2,3,4]] Generator expressions (like a list comp, except values are created lazily on demand, rather than all at once): (x+1 for x in [2, 4, 6, 8]) Set comprehensions (like a list comp, except it builds a set rather than a list): {char.lower() for char in "Hello World!"} Dict comprehensions (like a list comp, except it builds a dictionary of key:value pairs): {char: char.lower() for char in "Hello World!"} So what is a dict? A dict, short for dictionary, is a table of keys with associated values. Think of them as being like words and definitions: words = { "cat": "a small mammal that purrs", "dog": "man's best friend", "snake": "a lizard with no legs", "ostrich": "biggest bird in the world" } The first part, before the colon, is the key. The part after the colon is the value associated with that key. If you want to know the meaning of a word, you look it up in the dictionary: py> print(words['dog']) man's best friend To add a new word: py> words['kangeroo'] = 'jumping marsupial from Australia' py> print(words['kangeroo']) jumping marsupial from Australia So I've already shown you the dict comprehension form: spamwriter.writerow({name: row[name] for name in fields}) Here is how we can do it the old-fashioned way without a dict comp: table = {} for name in fields: value = row[name] table[name] = value spamwriter.writerow(table) Does this help? Ask any further questions you may have. -- Steven From duxbuz at hotmail.com Mon Jun 23 17:30:19 2014 From: duxbuz at hotmail.com (Ian D) Date: Mon, 23 Jun 2014 15:30:19 +0000 Subject: [Tutor] write dictionary to file In-Reply-To: <20140623145430.GW7742@ando> References: <20140621043515.GR7742@ando>, , <20140623145430.GW7742@ando> Message-ID: I will read this when I get a minute, but I must say thanks for the explanation. tutor at python really is the most helpful mailing list EVER! > Date: Tue, 24 Jun 2014 00:54:30 +1000 > From: steve at pearwood.info > To: tutor at python.org > Subject: Re: [Tutor] write dictionary to file > > On Mon, Jun 23, 2014 at 09:17:44AM +0000, Ian D wrote: > > > > for row in spamreader: > > > if row['year'] == '40': > > > email = row['user'] + '@email.com' > > > output = [ row[fieldname] for fieldname in fields ] > > > > I am unsure about this syntax [ row[fieldname] for fieldname in fields ] > > > That's called a "list comprehension". It is a way of building up a list > as a single expression. Let's start with the old-fashioned way to build > a list: > > output = [] > for fieldname in fields: > output.append( row[fieldname] ) > > > Or, we can do exactly the same thing, in a single line, with a list > comprehension, and get rid of the temporary variables: > > output = [row[fieldname] for fieldname in fields] > > If you remember your high school maths classes, the form of the list > comp is rather close to that of mathematical set builder notation: > > http://www.mathsisfun.com/sets/set-builder-notation.html > > Here is a set builder notation, and its English translation: > > {2n+1 : n ? {1,2,3,4}} > > This reads as "the set of 2n plus 1, such that n is an element of > {1,2,3,4}". Don't be confused by the fact that there is a set inside a > set -- this just tells us how to build a new set from an old set. > > So we start with a set {1,2,3,4}, and let n equal each of those numbers > in turn. Then we calculate 2n+1, and use that inside the new set we're > creating: > > n = 1, so 2n+1 => 3 > n = 2, so 2n+1 => 5 > n = 3, so 2n+1 => 7 > n = 4, so 2n+1 => 9 > > and so the final result is the set {3, 5, 7, 9}. > > Enough about sets and mathematics! Now we're going to talk about Python! > Python uses the same sort of expression, except it builds a list, not a > set, and calls it a "list comprehension" instead of a "list builder". > (Don't blame me for that, blame the Haskell programming language which > invented this.) > > So, we start with the mathematical set builder: > > {2n+1 : n ? {1,2,3,4}} > > > Turn the sets into lists: > > [2n+1 : n ? [1,2,3,4]] > > > Use Python syntax for the formula: > > [2*n+1 : n ? [1,2,3,4]] > > > Replace the "such that" and "element of" with "for" "in": > > [2*n+1 for n in [1,2,3,4]] > > > and now you have a Python list comprehension. If you copy and paste that > into a Python interpreter, you'll see this: > > py> [2*n+1 for n in [1,2,3,4]] > [3, 5, 7, 9] > > > > > > # DictWriter needs a dict, not a list. > > > spamwriter.writerow({name: row[name] for name in fields}) > > > > And this writerow syntax is something new for me, as are dictionaries > > (which I have tried to read up and understand.) > > List comprehensions were added to Python in, I think, version 2.2, which > was about ten years ago. They turned out to be so useful and popular > that now, in Python 3, we have FOUR different kinds of "comprehensions" > or builder syntax: > > List comprehensions: > [2*n+1 for n in [1,2,3,4]] > > Generator expressions (like a list comp, except values are created > lazily on demand, rather than all at once): > (x+1 for x in [2, 4, 6, 8]) > > Set comprehensions (like a list comp, except it builds a set rather than > a list): > {char.lower() for char in "Hello World!"} > > Dict comprehensions (like a list comp, except it builds a dictionary of > key:value pairs): > {char: char.lower() for char in "Hello World!"} > > > So what is a dict? A dict, short for dictionary, is a table of keys with > associated values. Think of them as being like words and definitions: > > words = { > "cat": "a small mammal that purrs", > "dog": "man's best friend", > "snake": "a lizard with no legs", > "ostrich": "biggest bird in the world" > } > > > The first part, before the colon, is the key. The part after the colon > is the value associated with that key. If you want to know the meaning > of a word, you look it up in the dictionary: > > py> print(words['dog']) > man's best friend > > > To add a new word: > > py> words['kangeroo'] = 'jumping marsupial from Australia' > py> print(words['kangeroo']) > jumping marsupial from Australia > > > So I've already shown you the dict comprehension form: > > spamwriter.writerow({name: row[name] for name in fields}) > > > Here is how we can do it the old-fashioned way without a dict comp: > > > table = {} > for name in fields: > value = row[name] > table[name] = value > > spamwriter.writerow(table) > > > > > Does this help? Ask any further questions you may have. > > > > > > -- > Steven > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From wolfrage8765 at gmail.com Mon Jun 23 14:59:12 2014 From: wolfrage8765 at gmail.com (Jordan) Date: Mon, 23 Jun 2014 08:59:12 -0400 Subject: [Tutor] Simple python help In-Reply-To: References: Message-ID: <53A824A0.1050200@gmail.com> Python is telling you, that Stephen, meaning the Variable is not defined. Now you can solve this by either defining Stephen, or making a temp variable of type string for comparison. Defining Stephen: #BEGIN SCRIPT# Stephen = 'Stephen' # This is now a variable of type string that equals 'Stephen'. person = input("What is your name?:") if person == Stephen: print('Hello ', Stephen) else: print('Hello ', person) #END SCRIPT# By the way I assume Python 3 is the version you are using, since that is what I use. Let me know if you get any further tracebacks. I hope that explains what it means to define a variable. -- On 06/10/2014 10:55 AM, Stephen Brazil wrote: > Hello! I am brand new to python and need to know how to make the > attached lines of code work. Should be pretty self-explanatory. > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/png Size: 40476 bytes Desc: not available URL: From msmurphy at alumni.unc.edu Tue Jun 24 17:34:31 2014 From: msmurphy at alumni.unc.edu (mark murphy) Date: Tue, 24 Jun 2014 11:34:31 -0400 Subject: [Tutor] How to list/process files with identical character strings Message-ID: Hello Python Tutor Community, This is my first post and I am just getting started with Python, so I apologize in advance for any lack of etiquette. I have a directory of several thousand daily satellite images that I need to process. Approximately 300 of these images are split in half, so in just these instances there will be two files for one day. I need to merge each pair of split images into one image. The naming convention of the files is as follows: TYYYYDDDHHMMSS, where: T= one character satellite code YYYY = 4 digit year DDD = Julian date HH = 2-digit hour MM = 2-digit minute SS = 2-digit second What I hope to be able to do is scan the directory, and for each instance where there are two files where the first 8 characters (TYYYYDDD) are identical, run a process on those two files and place the output (named TYYYYDDD) in a new directory. The actual processing part should be easy enough for me to figure out. The part about finding the split files (each pair of files with the same first 8 characters) and setting those up to be processed is way beyond me. I've done several searches for examples and have not been able to find what I am looking for. Can anyone help? Thanks so much! Mark -- Mark S. Murphy Alumnus Department of Geography msmurphy at alumni.unc.edu 951-252-4325 -------------- next part -------------- An HTML attachment was scrubbed... URL: From akleider at sonic.net Tue Jun 24 20:58:37 2014 From: akleider at sonic.net (Alex Kleider) Date: Tue, 24 Jun 2014 11:58:37 -0700 Subject: [Tutor] How to list/process files with identical character strings In-Reply-To: References: Message-ID: <560a9054cf966bf615ba68d236ceee0c@sonic.net> On 2014-06-24 08:34, mark murphy wrote: > Hello Python Tutor Community, > The actual processing part should be easy enough for me to figure out. > The > part about finding the split files (each pair of files with the same > first > 8 characters) and setting those up to be processed is way beyond me. > I've > done several searches for examples and have not been able to find what > I am > looking for. Since your file system probably already keeps them ordered, each pair will be next to each other. It would seem a simple matter to compare each file name to the one after it and if they match, process the two together. From marc.tompkins at gmail.com Tue Jun 24 21:24:55 2014 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 24 Jun 2014 12:24:55 -0700 Subject: [Tutor] How to list/process files with identical character strings In-Reply-To: References: Message-ID: On Tue, Jun 24, 2014 at 8:34 AM, mark murphy wrote: > What I hope to be able to do is scan the directory, and for each instance > where there are two files where the first 8 characters (TYYYYDDD) are > identical, run a process on those two files and place the output (named > TYYYYDDD) in a new directory. > > I don't know the details of your file system, but I would guess that those files would have some sort of signifier to indicate "this file is the first part of a multi-part image"; "this file is the second part", etc. - maybe the first half has the extension ".001" and the second half has the extension ".002"? If so, I would search for files with the "first part" signifier, and for each one I found I would try to join it with a file with the same base name but the "second part" signifier. If, on the other hand, there's no signifier - just the same date but with a slightly-different timestamp, you can: 1) grab the list of filenames 2) sort it 3) iterate through the list and compare each filename with the previous filename; if the first 8 characters match, you do your processing magic; if not, you move on. -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Tue Jun 24 22:02:53 2014 From: __peter__ at web.de (Peter Otten) Date: Tue, 24 Jun 2014 22:02:53 +0200 Subject: [Tutor] How to list/process files with identical character strings References: Message-ID: mark murphy wrote: > Hello Python Tutor Community, > > This is my first post and I am just getting started with Python, so I > apologize in advance for any lack of etiquette. > > I have a directory of several thousand daily satellite images that I need > to process. Approximately 300 of these images are split in half, so in > just these instances there will be two files for one day. I need to merge > each pair of split images into one image. > > The naming convention of the files is as follows: TYYYYDDDHHMMSS, where: > T= one character satellite code > YYYY = 4 digit year > DDD = Julian date > HH = 2-digit hour > MM = 2-digit minute > SS = 2-digit second > > What I hope to be able to do is scan the directory, and for each instance > where there are two files where the first 8 characters (TYYYYDDD) are > identical, run a process on those two files and place the output (named > TYYYYDDD) in a new directory. > > The actual processing part should be easy enough for me to figure out. > The part about finding the split files (each pair of files with the same > first > 8 characters) and setting those up to be processed is way beyond me. I've > done several searches for examples and have not been able to find what I > am looking for. Sorting is probably the approach that is easiest to understand, but an alternative would be to put the files into a dict that maps the 8-char prefix to a list of files with that prefix: directory = "/some/directory" files = os.listdir(directory) days = {} for filename in files: prefix = filename[:8] filepath = os.path.join(directory, filename) if prefix in days: # add file to the existing list days[prefix].append(filepath) else: # add a new list with one file days[prefix] = [filepath] for fileset in days.values(): if len(fileset) > 1: # process only the list with one or more files print("merging", fileset) (The if prefix in days: days[prefix].append(filepath) else: days[prefix] = [filepath] part can be simplified with the dict.setdefault() method or a collections.defaultdict) From __peter__ at web.de Tue Jun 24 22:10:33 2014 From: __peter__ at web.de (Peter Otten) Date: Tue, 24 Jun 2014 22:10:33 +0200 Subject: [Tutor] How to list/process files with identical character strings References: Message-ID: Peter Otten wrote: > for fileset in days.values(): > if len(fileset) > 1: > # process only the list with one or more files That should have been # process only the lists with two or more files > print("merging", fileset) From dyoo at hashcollision.org Tue Jun 24 22:10:43 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 24 Jun 2014 13:10:43 -0700 Subject: [Tutor] How to list/process files with identical character strings In-Reply-To: References: Message-ID: Hi Mark, Part of the problem statement sounds a little unusual to me, so I need to push on it to confirm. How do we know that there are only two files at a time that we need to manage? The naming convention described in the problem: --- The naming convention of the files is as follows: TYYYYDDDHHMMSS, where: T= one character satellite code YYYY = 4 digit year DDD = Julian date HH = 2-digit hour MM = 2-digit minute SS = 2-digit second --- allows for multiple collisions on the key TYYYYDDD. But without additional information, having more than two collisions seems a likely possibility to me! is there some other convention in play that prevents >2 collisions from occurring? The real world can be a bit dirty, so what happens if there are more? Is that an error? Good luck to you! From marc.tompkins at gmail.com Tue Jun 24 22:18:18 2014 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 24 Jun 2014 13:18:18 -0700 Subject: [Tutor] How to list/process files with identical character strings In-Reply-To: References: Message-ID: On Tue, Jun 24, 2014 at 1:02 PM, Peter Otten <__peter__ at web.de> wrote: > Sorting is probably the approach that is easiest to understand, but an > alternative would be to put the files into a dict that maps the 8-char > prefix to a list of files with that prefix: > I was debating the virtues of the two approaches, but figured I'd err on the side of simplicity... -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Tue Jun 24 22:39:04 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 24 Jun 2014 13:39:04 -0700 Subject: [Tutor] How to list/process files with identical character strings In-Reply-To: References: Message-ID: The sorting approach sounds reasonable. We might even couple it with itertools.groupby() to get the consecutive grouping done for us. https://docs.python.org/2/library/itertools.html#itertools.groupby For example, the following demonstrates that there's a lot that the library will do for us that should apply directly to Mark's problem: ######################################### import itertools import random def firstTwoLetters(s): return s[:2] grouped = itertools.groupby( sorted(open('/usr/share/dict/words')), key=firstTwoLetters) for k, g in grouped: print k, list(g)[:5] ######################################### From msmurphy at alumni.unc.edu Tue Jun 24 23:01:03 2014 From: msmurphy at alumni.unc.edu (mark murphy) Date: Tue, 24 Jun 2014 17:01:03 -0400 Subject: [Tutor] How to list/process files with identical character strings In-Reply-To: References: Message-ID: Hi Danny, Marc, Peter and Alex, Thanks for the responses! Very much appreciated. I will take these pointers and see what I can pull together. Thanks again to all of you for taking the time to help! Cheers, Mark On Tue, Jun 24, 2014 at 4:39 PM, Danny Yoo wrote: > The sorting approach sounds reasonable. We might even couple it with > itertools.groupby() to get the consecutive grouping done for us. > > https://docs.python.org/2/library/itertools.html#itertools.groupby > > > For example, the following demonstrates that there's a lot that the > library will do for us that should apply directly to Mark's problem: > > ######################################### > import itertools > import random > > def firstTwoLetters(s): return s[:2] > > grouped = itertools.groupby( > sorted(open('/usr/share/dict/words')), > key=firstTwoLetters) > > for k, g in grouped: > print k, list(g)[:5] > ######################################### > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Mark S. Murphy Alumnus Department of Geography msmurphy at alumni.unc.edu 951-252-4325 -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Wed Jun 25 00:27:45 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 24 Jun 2014 23:27:45 +0100 Subject: [Tutor] How to list/process files with identical character strings In-Reply-To: References: Message-ID: On 24/06/2014 22:01, mark murphy wrote: > Hi Danny, Marc, Peter and Alex, > > Thanks for the responses! Very much appreciated. > > I will take these pointers and see what I can pull together. > > Thanks again to all of you for taking the time to help! > > Cheers, > Mark > > > On Tue, Jun 24, 2014 at 4:39 PM, Danny Yoo > wrote: > > The sorting approach sounds reasonable. We might even couple it with > itertools.groupby() to get the consecutive grouping done for us. > > https://docs.python.org/2/library/itertools.html#itertools.groupby > > > For example, the following demonstrates that there's a lot that the > library will do for us that should apply directly to Mark's problem: > > ######################################### > import itertools > import random > > def firstTwoLetters(s): return s[:2] > > grouped = itertools.groupby( > sorted(open('/usr/share/dict/words')), > key=firstTwoLetters) > > for k, g in grouped: > print k, list(g)[:5] > ######################################### In order to really overwhelm you see more_itertools.pairwise here http://pythonhosted.org//more-itertools/api.html as I've found it useful on several occasions. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From akleider at sonic.net Wed Jun 25 00:55:58 2014 From: akleider at sonic.net (Alex Kleider) Date: Tue, 24 Jun 2014 15:55:58 -0700 Subject: [Tutor] How to list/process files with identical character strings In-Reply-To: References: Message-ID: On 2014-06-24 14:01, mark murphy wrote: > Hi Danny, Marc, Peter and Alex, > > Thanks for the responses! Very much appreciated. > > I will take these pointers and see what I can pull together. > > Thanks again to all of you for taking the time to help! Assuming your files are ordered and therefore one's that need to be paired will be next to each other, and that you can get an ordered listing of their names, here's a suggestion as to the sort of thing that might work: f2process = None for fname in listing: if not f2process: f2process = fname elif to_be_paired(f2process, fname): process(marry(f2process, fname)) already_processed = fname f2process = None else: process(f2process) already_processed = fname f2process = fname if fname != already_processed: # I'm not sure if 'fname' survives the for/in statement. # If it doesn't, another approach to not loosing the last file will be required. # I hope those more expert will comment. process(fname) def to_be_paired(f1, f2): """Returns a boolean: true if the files need to be amalgamated.""" pass # your code goes here. def marry(f1, f2): """Returns a file object which is a combination of the two files named by f1 and f2.""" pass # your code here. def process(fname_or_object): """Accepts either a file name or a file object, Does what you want done.""" pass # your code here. Comments? I was surprised that the use of dictionaries was suggested, especially since we were told there were many many files. From keithadu at live.com Wed Jun 25 01:23:48 2014 From: keithadu at live.com (keith papa) Date: Tue, 24 Jun 2014 19:23:48 -0400 Subject: [Tutor] learning to programming questions part 1 Message-ID: 1. Hi am new to python and I have a few questions: Why if you want to write multiple comment you use triple quotation marks and not the #? 2. I found this code to be interesting to me because it printed an output of [1,2,3,4,5,6,7] and not [1,2,3,4:4,5,6,7] why is that? Insert two or more elements to an existing list:1 2 3 4 >>> a= [1,2,3,4,7]>>> a[4:4] = [5,6]>>> print a[1,2,3,4,5,6,7] S: Quora -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Wed Jun 25 03:05:42 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 24 Jun 2014 18:05:42 -0700 Subject: [Tutor] learning to programming questions part 1 In-Reply-To: References: Message-ID: On Jun 24, 2014 4:55 PM, "keith papa" wrote: > > 1. Hi am new to python and I have a few questions: > Why if you want to write multiple comment you use triple quotation marks and not the #? > In certain places, string literals are treated as documentation that you can access with the help() function. Triple quotes are a way of writing a strong literal. Comments, on the other hand, are ignored: they don't contribute to documentation. > 2. I found this code to be interesting to me because it printed an output of [1,2,3,4,5,6,7] and not [1,2,3,4:4,5,6,7] why is that? > What do you think the meaning of line two of the program is? That is, what do you think the following line means? a[4:4] = [5,6] -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Wed Jun 25 03:07:41 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 24 Jun 2014 18:07:41 -0700 Subject: [Tutor] learning to programming questions part 1 In-Reply-To: References: Message-ID: > In certain places, string literals are treated as documentation that you can access with the help() function. Triple quotes are a way of writing a strong literal. Sorry! "strong" should be "string". -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Jun 25 03:17:40 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 25 Jun 2014 02:17:40 +0100 Subject: [Tutor] learning to programming questions part 1 In-Reply-To: References: Message-ID: On 25/06/14 00:23, keith papa wrote: > 1. Hi am new to python and I have a few questions: > Why if you want to write multiple comment you use triple quotation marks > and not the #? These are technically not the same as comments, they are documentation strings. The Python help system will find and display them if you ask it to - it won't find comments (starting with #). >>> def f(): ... ''' dummy function with doc string''' ... # this is not found ... pass ... >>> help(f) displays: Help on function f in module __main__: f() dummy function with doc string (END) So the doc string is visible in help but the comment is not. > 2. I found this code to be interesting to me because it printed an > output of [1,2,3,4,5,6,7] and not [1,2,3,4:4,5,6,7] why is that? > >>>> a= [1,2,3,4,7] > >>>> a[4:4] = [5,6] [4:4] is a slice not an index. It takes a section out of an existing list. You can see it if you play at the prompt: >>> a[2:4] [3,4] The contents between index 2(inclusive) and 4(exclusive) Think of it as a knife sitting to the left of the indexed items cutting out a slice... >>> a[4:5] [7] >>> a[4:4] [] In your example you are replacing the empty list found by [4:4] with [5,6] and that gets inserted back into your original list. >>>> print a > > [1,2,3,4,5,6,7] HTH-- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From jf_byrnes at comcast.net Wed Jun 25 03:37:58 2014 From: jf_byrnes at comcast.net (Jim Byrnes) Date: Tue, 24 Jun 2014 20:37:58 -0500 Subject: [Tutor] tkinter.filedialog? Message-ID: I am working with some demo programs in a breezypythongui book. One program contains these two lines: filetypes = [ ("Python files", "*.py"), ("Text files", "*.txt")] fileName = tkinter.filedialog.askopenfilename(parent = self, filetypes = filetypes) According to the book this should open a file dialog and show .py and .txt files, but it only shows .py files. Thinking that maybe it had something to do with the breezypythongui implementation, I opened a terminal and tried: Python 3.3.5 (default, Mar 12 2014, 02:09:17) [GCC 4.6.3] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import tkinter.filedialog >>> filetypes = [("Python files", "*.py"), ("Text files", "*.txt")] >>> fileName = tkinter.filedialog.askopenfilename(filetypes = filetypes) It pops up a file dialog but again only .py files are shown. Why is it that both .py and .txt files are not shown? It seems to be the correct way to do it. Thanks, Jim From __peter__ at web.de Wed Jun 25 09:15:20 2014 From: __peter__ at web.de (Peter Otten) Date: Wed, 25 Jun 2014 09:15:20 +0200 Subject: [Tutor] How to list/process files with identical character strings References: Message-ID: Alex Kleider wrote: > On 2014-06-24 14:01, mark murphy wrote: >> Hi Danny, Marc, Peter and Alex, >> >> Thanks for the responses! Very much appreciated. >> >> I will take these pointers and see what I can pull together. >> >> Thanks again to all of you for taking the time to help! > > > Assuming your files are ordered and therefore one's that need to be > paired will be next to each other, > and that you can get an ordered listing of their names, > here's a suggestion as to the sort of thing that might work: > > f2process = None > for fname in listing: > if not f2process: > f2process = fname > elif to_be_paired(f2process, fname): > process(marry(f2process, fname)) > already_processed = fname > f2process = None > else: > process(f2process) > already_processed = fname > f2process = fname > > if fname != already_processed: > # I'm not sure if 'fname' survives the for/in statement. > # If it doesn't, another approach to not loosing the last file will > be required. > # I hope those more expert will comment. > process(fname) > > > def to_be_paired(f1, f2): > """Returns a boolean: true if the files need to be amalgamated.""" > pass # your code goes here. > > def marry(f1, f2): > """Returns a file object which is a combination of the two files > named by f1 and f2.""" > pass # your code here. > > def process(fname_or_object): > """Accepts either a file name or a file object, Does what you want > done.""" > pass # your code here. > > Comments? > I was surprised that the use of dictionaries was suggested, especially > since we were told there were many many files. (1) 10**6 would be "many files" as in "I don't want to touch them manually", but no problem for the dict approach. "a directory of several thousand daily satellite images" should certainly be managable. (2a) os.listdir() returns a list, so you consume memory proportional to the number of files anyway. (2b) Even if you replace listdir() with a function that generates one filename at a time you cannot safely assume that the names are sorted -- you have to put them in a list to sort them. (3a) Dictionaries are *the* data structure in Python. You should rather be surprised when dict is not proposed for a problem. I might go as far as to say that most of the Python language is syntactic sugar for dicts ;) This leads to (3b) dict-based solutions are usually both efficient and (3c) concise To back 3c here's how I would have written the code if it weren't for educational purposes: directory = "some/directory" files = os.listdir(directory) days = collections.defaultdict(list) for filename in files: days[filename[:8]].append(os.path.join(directory, filename)) for fileset in days.values(): if len(fileset) > 1: print("merging", fileset) But I admit that sort/groupby is also fine: directory = "some/directory" files = os.listdir(directory) files.sort() for _prefix, fileset in itertools.groupby(files, key=lambda name: name[:8]): fileset = list(fileset) if len(fileset) > 1: print("merging", [os.path.join(directory, name) for name in fileset]) From alan.gauld at btinternet.com Wed Jun 25 09:36:44 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 25 Jun 2014 08:36:44 +0100 Subject: [Tutor] tkinter.filedialog? In-Reply-To: References: Message-ID: On 25/06/14 02:37, Jim Byrnes wrote: > >>> import tkinter.filedialog > >>> filetypes = [("Python files", "*.py"), ("Text files", "*.txt")] > >>> fileName = tkinter.filedialog.askopenfilename(filetypes = filetypes) > > It pops up a file dialog but again only .py files are shown. > > Why is it that both .py and .txt files are not shown? filetypes does not control which files are shown it controls which filters are shown. There is a drop down list on the dialog that you can select the filter that is applied. You should find there are two in your case: Python files and text files. If you select the text files filter it will show only text files... hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From wolfgang.maier at biologie.uni-freiburg.de Wed Jun 25 09:35:03 2014 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Wed, 25 Jun 2014 09:35:03 +0200 Subject: [Tutor] How to list/process files with identical character strings In-Reply-To: References: Message-ID: On 25.06.2014 00:55, Alex Kleider wrote: > > I was surprised that the use of dictionaries was suggested, especially > since we were told there were many many files. > The OP was talking about several thousands of files, which is, of course, too many for manual processing, but is far from an impressive number of elements for a Python dictionary on any modern computer. Dictionaries are fast and efficient and their memory consumption is a factor you will have to think about only in extreme cases (and this is definitely not one of them). What is more, your sequential approach of always comparing a pair of elements hides the fact that you will still have the filenames in memory as a list (at least this is what os.listdir would return) and the difference between that and the proposed dictionary is not that huge. What's more important in my opinion is that while the two approaches may look equally potent for the given example, the dictionary provides more flexibility, i.e., the code is easier to adjust to new problems. Think of the afore-mentioned situation that you could also have three parts of a file instead of two. While your suggestion would have to be rewritten almost from scratch, very little changes would be required to the dictionary-based code. Best, Wolfgang From jf_byrnes at comcast.net Wed Jun 25 15:35:30 2014 From: jf_byrnes at comcast.net (Jim Byrnes) Date: Wed, 25 Jun 2014 08:35:30 -0500 Subject: [Tutor] tkinter.filedialog? In-Reply-To: References: Message-ID: On 06/25/2014 02:36 AM, Alan Gauld wrote: > On 25/06/14 02:37, Jim Byrnes wrote: > >> >>> import tkinter.filedialog >> >>> filetypes = [("Python files", "*.py"), ("Text files", "*.txt")] >> >>> fileName = tkinter.filedialog.askopenfilename(filetypes = filetypes) >> >> It pops up a file dialog but again only .py files are shown. >> >> Why is it that both .py and .txt files are not shown? > > filetypes does not control which files are shown it controls which > filters are shown. There is a drop down list on the dialog that you can > select the filter that is applied. You should find there are two in your > case: Python files and text files. > > If you select the text files filter it will show only text files... > > > hth OK, I see it now, thanks. I guess I am conditioned to seeing a downward pointing arrow head or triangle to indicate a drop down list, didn't even pay attention to the little rectangle at the right of the control. Regards, Jim From akleider at sonic.net Thu Jun 26 06:47:07 2014 From: akleider at sonic.net (Alex Kleider) Date: Wed, 25 Jun 2014 21:47:07 -0700 Subject: [Tutor] How to list/process files with identical character strings In-Reply-To: References: Message-ID: <4a45291e17d3bb52a9d59e375130985e@sonic.net> On 2014-06-25 00:35, Wolfgang Maier wrote: > On 25.06.2014 00:55, Alex Kleider wrote: >> >> I was surprised that the use of dictionaries was suggested, especially >> since we were told there were many many files. >> > > The OP was talking about several thousands of files, which is, of > course, too many for manual processing, but is far from an impressive > number of elements for a Python dictionary on any modern computer. > Dictionaries are fast and efficient and their memory consumption is a > factor you will have to think about only in extreme cases (and this is > definitely not one of them). What is more, your sequential approach of > always comparing a pair of elements hides the fact that you will still > have the filenames in memory as a list (at least this is what > os.listdir would return) and the difference between that and the > proposed dictionary is not that huge. > > What's more important in my opinion is that while the two approaches > may look equally potent for the given example, the dictionary provides > more flexibility, i.e., the code is easier to adjust to new problems. > Think of the afore-mentioned situation that you could also have three > parts of a file instead of two. While your suggestion would have to be > rewritten almost from scratch, very little changes would be required > to the dictionary-based code. > > Best, > Wolfgang Thanks for elucidating this. I didn't know that "several thousand" would still be considered a small number. If this is the case, then certainly your points are well taken. Gratefully, alex From somrsaltt at gmail.com Thu Jun 26 10:18:14 2014 From: somrsaltt at gmail.com (Myunggyo Lee) Date: Thu, 26 Jun 2014 17:18:14 +0900 Subject: [Tutor] IndexError: list index out of range Message-ID: Hi all I just started to learn python language. I'm trying to figure out the reason of error but i couldn't find it. first imports short.txt(is attached to this mail) and read in dictionary named gpdic1 Traceback (most recent call last): File "/home/ercsb/test.py", line 11, in hgene = lines[1] IndexError: list index out of range ------------------------------------------------------------------------------------------- # -*- coding: utf-8 -*- import string, os, sys, time, glob inf2 =open('short.txt','r') gpdic1={} while 1: line= inf2.readline() if not line: break lines = line[:-1].split(',') hgene = lines[1] chr1 = lines[4] hgstart = lines[5] hgstop = lines[6] tgene = lines[7] chr2 = lines[10] tgstart = lines[11] tgstop = lines[12] gpdic1["hgene"] = hgene gpdic1["chr1"] = chr1 gpdic1["hgstart"] = hgstart gpdic1["hgstop"] = hgstop gpdic1["tgene"] = tgene gpdic1["chr2"] = chr2 gpdic1["tgstart"] = tgstart gpdic1["tgstop"] = tgstop -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- BF766250,TMEM151B,1,121,6,44273393,44273512,MT1A,118,329,16,56672578,56673816 CV351576,SNRNP70,1,116,19,49611334,49611448,GALT,110,377,9,34648372,34649047 AF487906,AFF1,1,211,4,87968543,87968752,MLL,203,435,11,118359327,118360590 DA403287,ARL16,1,163,17,79650564,79650824,LPIN1,161,542,2,11905658,11911581 BQ027739,RPLP2,19,216,11,812551,812874,HLA-DPA1,212,264,6,4513297,4513349 BF732578,PLD6,1,64,17,17109569,17109632,TNS1,58,729,2,218677091,218682697 DA168843,UQCRH,1,217,1,46769402,46775925,WASF1,217,524,6,110500775,110501082 BC040830,XPO5,121,374,6,43506503,43506758,RNF11,368,431,1,51716222,51716308 BI060051,abParts,13,122,14,106208368,106208477,DDB1,115,226,11,61094327,61096907 BG491331,PMM2,2,666,16,8891714,8906963,FDPS,667,899,1,155287732,155288485 BF800892,HNRNPA2B1,23,136,7,26236477,26236589,AATF,132,346,17,35310229,35310443 BE161420,BCYRN1,1,69,X,70516825,70516893,LMBR1,63,171,7,156473902,156474010 BG015191,MEG3,1,84,14,101301719,101301803,PPM1G,78,395,2,27604463,27605349 DW446667,BTN2A1,16,81,6,26458176,26458241,LAMP1,78,216,13,113977580,113977718 BF931405,MLL2,1,58,12,49445846,49445903,HLA-DPA1,59,122,6,4517371,4517434 DA869870,Unknown,1,293,1,12678018,12678310,COL6A2,292,524,21,47550288,47550520 AF362886,TPM3,12,220,1,154142876,154144580,ALK,212,308,2,29446307,29446402 DA819158,ARHGAP24,1,357,4,86396556,86491874,BANK1,357,561,4,102942670,102946482 BE835134,MPHOSPH10,7,123,2,71362676,71362793,AES,124,223,19,3053161,3053260 AI087196,TAGLN,6,72,11,117075433,117075498,BCORL1,65,396,X,129147106,129147437 From __peter__ at web.de Thu Jun 26 11:23:55 2014 From: __peter__ at web.de (Peter Otten) Date: Thu, 26 Jun 2014 11:23:55 +0200 Subject: [Tutor] IndexError: list index out of range References: Message-ID: Myunggyo Lee wrote: > I'm trying to figure out the reason of error but i couldn't find it. > first imports short.txt(is attached to this mail) > and read in dictionary named gpdic1 > > > Traceback (most recent call last): > File "/home/ercsb/test.py", line 11, in > hgene = lines[1] > IndexError: list index out of range > # -*- coding: utf-8 -*- > import string, os, sys, time, glob > > inf2 =open('short.txt','r') > > gpdic1={} > while 1: > line= inf2.readline() > if not line: break Add a print statement here to see the problem: print "processing", repr(line) Hint: you have an empty line in your data, so: >>> lines = "\n"[:-1].split(",") >>> lines [''] >>> lines[1] Traceback (most recent call last): File "", line 1, in IndexError: list index out of range A possible fix is to skip empty lines for line in open("short.txt"): line = line.strip() if not line: continue # line contains only whitespace, move on to the next line ... # your code > lines = line[:-1].split(',') > > hgene = lines[1] > chr1 = lines[4] > hgstart = lines[5] > hgstop = lines[6] > tgene = lines[7] > chr2 = lines[10] > tgstart = lines[11] > tgstop = lines[12] Note that the following replaces the data of the previous line; once the while loop has finished you have lost all data except that taken from the last line in the file. > gpdic1["hgene"] = hgene > gpdic1["chr1"] = chr1 > gpdic1["hgstart"] = hgstart > gpdic1["hgstop"] = hgstop > gpdic1["tgene"] = tgene > gpdic1["chr2"] = chr2 > gpdic1["tgstart"] = tgstart > gpdic1["tgstop"] = tgstop From davea at davea.name Thu Jun 26 13:13:49 2014 From: davea at davea.name (Dave Angel) Date: Thu, 26 Jun 2014 07:13:49 -0400 (EDT) Subject: [Tutor] IndexError: list index out of range References: Message-ID: Myunggyo Lee Wrote in message: You apparently posted this in html, and you tried to attach a data file. Each of those will cause problems for some readers. Please tell your email program to use text mail, and paste in your data, don't attach it. gpdic1={} while 1: line= inf2.readline() if not line: break lines = line[:-1].split(',') hgene = lines[1] chr1 = lines[4] ..... Apparently, some of your data has less than two fields in it (fields would be a much clearer variable name than lines). You need a test on the len () of lines, probably displaying an error if there are less than 9 (or whatever you're expecting). At the least you could ignore such lines by doing a continue. Do you have any plans for dealing with the second line of the file? It'll wipe out the dictionary items from the first line. Incidentally, are you deliberately ignoring the first field on the line, lines [0] ? -- DaveA From steve at pearwood.info Thu Jun 26 13:54:28 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 26 Jun 2014 21:54:28 +1000 Subject: [Tutor] How to list/process files with identical character strings In-Reply-To: <4a45291e17d3bb52a9d59e375130985e@sonic.net> References: <4a45291e17d3bb52a9d59e375130985e@sonic.net> Message-ID: <20140626115428.GC13014@ando> On Wed, Jun 25, 2014 at 09:47:07PM -0700, Alex Kleider wrote: > Thanks for elucidating this. I didn't know that "several thousand" > would still be considered a small number. On a server, desktop, laptop or notepad, several thousand is not many. My computer can generate a dict with a million items in less than a second and a half: py> with Stopwatch(): ... d = {n: (3*n+2)**4 for n in range(1000000)} ... time taken: 1.331450 seconds and then process it in under half a second: py> with Stopwatch(): ... x = sum(d[n] for n in range(1000000)) ... time taken: 0.429471 seconds py> x 16200013499990999994000001300000 For an embedded device, with perhaps 16 megabytes of RAM, thousands of items is a lot. But for a machine with gigabytes of RAM, it's tiny. -- Steven From alan.gauld at btinternet.com Thu Jun 26 14:42:50 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 26 Jun 2014 13:42:50 +0100 Subject: [Tutor] IndexError: list index out of range In-Reply-To: References: Message-ID: On 26/06/14 09:18, Myunggyo Lee wrote: > Hi all > I just started to learn python language. Welcome. > I'm trying to figure out the reason of error but i couldn't find it. > first imports short.txt(is attached to this mail) > and read in dictionary named gpdic1 > Others have pointed out the specific problems with your code. However you general approach could be different too using some of Pythons features. > # -*- coding: utf-8 -*- > import string, os, sys, time, glob You should not need to import string any more. It is a remnant of very old Python code. Its functionality is (almost) all in the built-in string objects nowadays. Especially if you are using python v3 - which you don't tell us... > inf2 =open('short.txt','r') > > gpdic1={} > while 1: > line= inf2.readline() > if not line: break A better way of doing this in Python is to use a for loop. You could rewrite the lines above as one line: for line in open('short.txt'): # 'r' is the default mode > lines = line[:-1].split(',') rather than stripping of the lst character its usually better to use the rstrip() method - there migfht be more than one character to be removed... lines = line.rstrip().split(',') Also if this is a comma separated file, as seems to be implied by the split(). You will find a csv module in the standard library that can make this much easier. It can read the data directly from the file into a list of dictionaries via a DictReader object. > hgene = lines[1] > chr1 = lines[4] > hgstart = lines[5] > hgstop = lines[6] > tgene = lines[7] > chr2 = lines[10] > tgstart = lines[11] > tgstop = lines[12] > > gpdic1["hgene"] = hgene > gpdic1["chr1"] = chr1 > gpdic1["hgstart"] = hgstart > gpdic1["hgstop"] = hgstop > gpdic1["tgene"] = tgene > gpdic1["chr2"] = chr2 > gpdic1["tgstart"] = tgstart > gpdic1["tgstop"] = tgstop You could have done all of that directly and avoided the double assignments. ie gpdic1["hgene"] = lines[1] etc... The csv.DictReader would do it all for you and correctly return a list of dicts instead of just the last one. import csv datafile = open(...) data = csv.DictReader(datafile) data is now a collection of dictionaries each of which is like your gpdic1 above. If the file does not include the required keys in the first line you can provide them as a list of strings to DictReader - see the manual... Python has a wealth of modules in its library and part of becoming a good Python programmer is in learning whats there and how to use it. That takes time and experience but its often a quick Google in case. Finally, it looks a lot like bioscience. There are some specific Python modules (and versions) designed for that area. You may find a Google search for Python and bioscience throws up something useful. hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From wangzheyu1989 at gmail.com Thu Jun 26 11:23:30 2014 From: wangzheyu1989 at gmail.com (Tony Wang) Date: Thu, 26 Jun 2014 17:23:30 +0800 Subject: [Tutor] Explicitly define the lib path for python during build time Message-ID: Hi there, I'm building python2.7 from src code. Usually, after make, if I define a user folder to install the python, the python will depends on the lib folder in the installer folder. But my needs is quite special here, I need to statically link python with gdb, and I hope the python in gdb I build can run on other PC. I know that I can do some tricky thing to include the python .py lib file inside gdb folder, but I prefer to depends on user PC's native python library. So I need to add the /usr/lib/python2.7 or something like that to the python lib search path. I know that I can ask user to do export PYTHONHOME, but is there any way to explicitly define more than one lib search path during build time? BR, Tony From leamhall at gmail.com Thu Jun 26 15:39:19 2014 From: leamhall at gmail.com (leam hall) Date: Thu, 26 Jun 2014 09:39:19 -0400 Subject: [Tutor] Better way to check *nix remote file age? Message-ID: Python 2.4.3 Writing a function that takes the string from "ssh ls -l /var/log/yum.log" and tries to see if the file is more than a couple months old. The goal is to only run python on the local server and it will ssh into the remote server. Is there a better way to do this? Thanks! Leam #### Standard type of string that would come in date_string: New file: -rw------- 1 sam users 105 Jun 19 13:57 guido2 Old file: -rw------- 1 sam users 105 May 19 2011 guido #### def linux_too_old(date_string): '''(string) -> boolean Returns True if the date string is more than a couple months old. ''' months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] this_month = datetime.date.today().month file_date = date_string.split() if ':' not in file_date[7]: return True file_month = file_date[5] if 0 < (this_month - months.index(file_month)) < 3: return False -- Mind on a Mission -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Jun 26 16:10:28 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 26 Jun 2014 15:10:28 +0100 Subject: [Tutor] Explicitly define the lib path for python during build time In-Reply-To: References: Message-ID: On 26/06/14 10:23, Tony Wang wrote: > But my needs is quite special here, I need to statically link python > with gdb, and I hope the python in gdb I build can run on other PC. This list is for people learning the Python language and library. As such your question is extremely specialized and advanced. You will probably get a better response on the main Python list or perhaps on the python dev list... You might be lucky and get a reply from some of our local gurus but this is really a bit off topic for the tutor list. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From dalveen at gmail.com Thu Jun 26 17:40:39 2014 From: dalveen at gmail.com (Jorge L.) Date: Thu, 26 Jun 2014 17:40:39 +0200 Subject: [Tutor] Object instance package wide with Jython 2.7 Message-ID: To avoid duplicated work: This a cross posting of a SE question: http://programmers.stackexchange.com/questions/246161/object-attribute-needed-at-package-level-with-python Let's consider the following scenario. We have a Python 2.7 package which serves as a library for some scripting projects. We have a `jli.py` and a `server.py` modules within our package: ___init__.py: from jli import JLI from server import Server jli.py: class JLI(object): def __init__(self) pass def setup(self): # In order for this to work I actually need to set two attributes of JLI instances based on input self.foo = foo server.py: class Server(object) def __init__(self, name) self.name = name def some_operation(self) # stuff to be done with JLI.foo now I want to write the following script: from package import JLI, Server cli = JLI() cli.setup() -> cli.foo is available to use serv01 = Server('01') serv01.some_operation() with an arbitrary number of classes like `Server` most of them, needing `JLI.foo` after `setup()` is called in order to work. I don't want to use setup twice in the script or doing imports and so forth. I don't know how to implement this ... any help will be greaty appreciated. -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Thu Jun 26 18:41:30 2014 From: wprins at gmail.com (Walter Prins) Date: Thu, 26 Jun 2014 17:41:30 +0100 Subject: [Tutor] Better way to check *nix remote file age? In-Reply-To: References: Message-ID: On 26 June 2014 14:39, leam hall wrote: > Python 2.4.3 > > Writing a function that takes the string from "ssh ls -l > /var/log/yum.log" and tries to see if the file is more than a couple months > old. The goal is to only run python on the local server and it will ssh into > the remote server. > > Is there a better way to do this? I'd probably rather try Paramiko's SFTPClient and retrieve the file modified date directly: http://paramiko-docs.readthedocs.org/en/latest/api/sftp.html#paramiko.sftp_client.SFTPClient (see the SFTPFile.stat() method in particular, which gives you back a stat object containing mtime, the modification datetime IIRC) 2 more (hopefully) useful links if you decide to go this route: http://www.saltycrane.com/blog/2010/02/python-paramiko-notes/ http://jessenoller.com/blog/2009/02/05/ssh-programming-with-paramiko-completely-different Walter From leamhall at gmail.com Thu Jun 26 19:01:55 2014 From: leamhall at gmail.com (leam hall) Date: Thu, 26 Jun 2014 13:01:55 -0400 Subject: [Tutor] Better way to check *nix remote file age? In-Reply-To: References: Message-ID: On Thu, Jun 26, 2014 at 12:41 PM, Walter Prins wrote: > On 26 June 2014 14:39, leam hall wrote: > > Python 2.4.3 > > > > Writing a function that takes the string from "ssh ls -l > > /var/log/yum.log" and tries to see if the file is more than a couple > months > > old. The goal is to only run python on the local server and it will ssh > into > > the remote server. > > > > Is there a better way to do this? > > I'd probably rather try Paramiko's SFTPClient and retrieve the file > modified date directly: > > http://paramiko-docs.readthedocs.org/en/latest/api/sftp.html#paramiko.sftp_client.SFTPClient > (see the SFTPFile.stat() method in particular, which gives you back a > stat object containing mtime, the modification datetime IIRC) > > 2 more (hopefully) useful links if you decide to go this route: > http://www.saltycrane.com/blog/2010/02/python-paramiko-notes/ > > http://jessenoller.com/blog/2009/02/05/ssh-programming-with-paramiko-completely-different > > > Walter > Seem to not work on Python 2.4.3. -- Mind on a Mission -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Thu Jun 26 19:37:11 2014 From: davea at davea.name (Dave Angel) Date: Thu, 26 Jun 2014 13:37:11 -0400 (EDT) Subject: [Tutor] Object instance package wide with Jython 2.7 References: Message-ID: "Jorge L." Wrote in message: (please post in text mode, as html carries a number of problems in a text list like this one) class Server(object) def __init__(self, name) self.name = name def some_operation(self) # stuff to be done with JLI.foo JL I. foo is undefined. You made an instance attribute, not a class attribute. So you need to associate an appropriate instance of JL I. Probably you want to save that when instantiating Server. class Server(object) def __init__(self, name, jlinstance) self.name = name self. jlinstance = jlinstance def some_operation(self) # stuff to be done with jlinstance.foo -- DaveA From fomcl at yahoo.com Thu Jun 26 20:40:43 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 26 Jun 2014 11:40:43 -0700 Subject: [Tutor] Better way to check *nix remote file age? In-Reply-To: References: Message-ID: <1403808043.95406.YahooMailNeo@web163805.mail.gq1.yahoo.com> > I'd probably rather try Paramiko's SFTPClient and retrieve the file > modified date directly: > http://paramiko-docs.readthedocs.org/en/latest/api/sftp.html#paramiko.sftp_client.SFTPClient > (see the SFTPFile.stat() method in particular, which gives you back a > stat object containing mtime, the modification datetime IIRC) > > 2 more (hopefully) useful links if you decide to go this route: > http://www.saltycrane.com/blog/2010/02/python-paramiko-notes/ > http://jessenoller.com/blog/2009/02/05/ssh-programming-with-paramiko-completely-different Interesting. Question: what is the added value of paramiko compared to using subprocess to use ssh (ie, shell commands)? (Possible answer: it is platform-independent) Regards, Albert-Jan From raulcumplido at gmail.com Fri Jun 27 12:10:25 2014 From: raulcumplido at gmail.com (=?UTF-8?Q?Ra=C3=BAl_Cumplido?=) Date: Fri, 27 Jun 2014 11:10:25 +0100 Subject: [Tutor] Better way to check *nix remote file age? In-Reply-To: <1403808043.95406.YahooMailNeo@web163805.mail.gq1.yahoo.com> References: <1403808043.95406.YahooMailNeo@web163805.mail.gq1.yahoo.com> Message-ID: Hi, I would recommend you to migrate your Python version for a newer one where you can use fabric, paramiko or other ssh tools. It would be easier. I would recommend also instead of doing an "ls -l" command doing something to retrieve only the information you need: /bin/ls -ls | awk '{print $7,$8,$9, $10}' Jun 27 10:36 my_file Then I would use timedelta instead where you can be more accurate with the dates (in the example if the file is older than 62 days): def too_old(text): month, day, year = (text[0], text[1], text[2] if ':' not in text[2] else datetime.datetime.now().year) time_difference = datetime.datetime.now() - datetime.datetime.strptime( "{0}{1}{2}".format(month, day, year), '%b%d%Y') return time_difference > datetime.timedelta(days=62) Thanks, Ra?l On Thu, Jun 26, 2014 at 7:40 PM, Albert-Jan Roskam < fomcl at yahoo.com.dmarc.invalid> wrote: > > > > > I'd probably rather try Paramiko's SFTPClient and retrieve the file > > > modified date directly: > > > http://paramiko-docs.readthedocs.org/en/latest/api/sftp.html#paramiko.sftp_client.SFTPClient > > (see the SFTPFile.stat() method in particular, which gives you back a > > stat object containing mtime, the modification datetime IIRC) > > > > 2 more (hopefully) useful links if you decide to go this route: > > http://www.saltycrane.com/blog/2010/02/python-paramiko-notes/ > > > http://jessenoller.com/blog/2009/02/05/ssh-programming-with-paramiko-completely-different > > Interesting. Question: what is the added value of paramiko compared to > using subprocess to use ssh (ie, shell commands)? (Possible answer: it is > platform-independent) > > Regards, > Albert-Jan > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Ra?l Cumplido -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan_ml at behnel.de Fri Jun 27 13:41:01 2014 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 27 Jun 2014 13:41:01 +0200 Subject: [Tutor] Better way to check *nix remote file age? In-Reply-To: References: <1403808043.95406.YahooMailNeo@web163805.mail.gq1.yahoo.com> Message-ID: Ra?l Cumplido, 27.06.2014 12:10: > I would recommend you to migrate your Python version for a newer one where > you can use fabric, paramiko or other ssh tools. It would be easier. +1 Even compiling it yourself shouldn't be too difficult on Linux. > I would recommend also instead of doing an "ls -l" command doing something > to retrieve only the information you need: > > /bin/ls -ls | awk '{print $7,$8,$9, $10}' > > Jun 27 10:36 my_file Or run some Python code on the other side, e.g. with python -c 'python code here' Stefan From wprins at gmail.com Fri Jun 27 13:41:51 2014 From: wprins at gmail.com (Walter Prins) Date: Fri, 27 Jun 2014 12:41:51 +0100 Subject: [Tutor] Better way to check *nix remote file age? In-Reply-To: <1403808043.95406.YahooMailNeo@web163805.mail.gq1.yahoo.com> References: <1403808043.95406.YahooMailNeo@web163805.mail.gq1.yahoo.com> Message-ID: Hi, On 26 June 2014 19:40, Albert-Jan Roskam wrote: > > > >> I'd probably rather try Paramiko's SFTPClient and retrieve the file > >> modified date directly: >> http://paramiko-docs.readthedocs.org/en/latest/api/sftp.html#paramiko.sftp_client.SFTPClient >> (see the SFTPFile.stat() method in particular, which gives you back a >> stat object containing mtime, the modification datetime IIRC) >> >> 2 more (hopefully) useful links if you decide to go this route: >> http://www.saltycrane.com/blog/2010/02/python-paramiko-notes/ >> http://jessenoller.com/blog/2009/02/05/ssh-programming-with-paramiko-completely-different > > Interesting. Question: what is the added value of paramiko compared to using subprocess to use ssh (ie, shell commands)? (Possible answer: it is platform-independent) Parsing command line output is arguably generally (more) brittle and prone to breakage than coding to a direct API/library -- Paramiko & friends give you a proper programming API to code against with direct/native access to object properties. A file modification date and time (for example) from a shell command command is a string that then must be parsed/interpreted. This might be subject to locale format, command/shell settings and who knows what else. By contrast a "stat" result is a more well defined entity with unambiguous contents, completely removing having to guess/parse text. IMVHO much more preferable. :) Walter From peter.romfeld.hk at gmail.com Fri Jun 27 11:24:01 2014 From: peter.romfeld.hk at gmail.com (Peter Romfeld) Date: Fri, 27 Jun 2014 17:24:01 +0800 Subject: [Tutor] Http Redirect In-Reply-To: <53A3A4A0.4020108@gmail.com> References: <53A3A4A0.4020108@gmail.com> Message-ID: <53AD3831.3030500@gmail.com> Hi, I am not using django, i will use: from urllib2 import HTTPRedirectHandler HTTPRedirectHandler.http_error_302(url) i did not test it yet Cheers > hi, > > i want to make a http-redirect with falconframework. just put it into > response HTML header? > > with cherrypy i had 'raise cherrypy.HTTPRedirect(url) > > i know that i can do that also with HTML: > > > > > i think with django you can do the redirect directly in urls. > > with falconframework i have similiar urls -------------- next part -------------- An HTML attachment was scrubbed... URL: From leamhall at gmail.com Fri Jun 27 13:57:54 2014 From: leamhall at gmail.com (leam hall) Date: Fri, 27 Jun 2014 07:57:54 -0400 Subject: [Tutor] Better way to check *nix remote file age? In-Reply-To: References: <1403808043.95406.YahooMailNeo@web163805.mail.gq1.yahoo.com> Message-ID: On Fri, Jun 27, 2014 at 7:41 AM, Stefan Behnel wrote: > Ra?l Cumplido, 27.06.2014 12:10: > > I would recommend you to migrate your Python version for a newer one > where > > you can use fabric, paramiko or other ssh tools. It would be easier. > > +1 > > Even compiling it yourself shouldn't be too difficult on Linux. > > I'm not retro just for the heck of it. There's no reason to avoid Python 3.x but sometimes you just have to work with what you have. Anyone willing to provide a couple million dollars and 12-18 months of free 24/7/365.25 support? I have to work with four different operating systems and three of the four have two versions. Most might have python installed but some don't. My code has to be clean enough that people new to Python can understand it when something goes south. Somewhat sorry for the rant. I just get tired of people saying "upgrade" when that's not an option. Leam -- Mind on a Mission -------------- next part -------------- An HTML attachment was scrubbed... URL: From pallavi.chaurasia94 at gmail.com Fri Jun 27 14:04:17 2014 From: pallavi.chaurasia94 at gmail.com (Pallavi Chaurasia) Date: Fri, 27 Jun 2014 05:04:17 -0700 Subject: [Tutor] Package Installation Issue Message-ID: Hi, I am facing certain problems regarding installation of the NetworkX package in python . Can you please help me out Thanks in advance . Pallavi -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Fri Jun 27 20:15:22 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 27 Jun 2014 19:15:22 +0100 Subject: [Tutor] Package Installation Issue In-Reply-To: References: Message-ID: On 27/06/2014 13:04, Pallavi Chaurasia wrote: > Hi, > > I am facing certain problems regarding installation of the NetworkX > package in python . Can you please help me out > > Thanks in advance . > Pallavi > If you'd like to tell us what OS you're using, which version of Python, exactly what you've tried and precisely what went wrong I'm sure that we can help. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From wprins at gmail.com Fri Jun 27 23:26:58 2014 From: wprins at gmail.com (Walter Prins) Date: Fri, 27 Jun 2014 22:26:58 +0100 Subject: [Tutor] Better way to check *nix remote file age? In-Reply-To: References: <1403808043.95406.YahooMailNeo@web163805.mail.gq1.yahoo.com> Message-ID: Hi, On 27 June 2014 12:57, leam hall wrote: > On Fri, Jun 27, 2014 at 7:41 AM, Stefan Behnel > wrote: > >> Ra?l Cumplido, 27.06.2014 12:10: >> > I would recommend you to migrate your Python version for a newer one >> where >> > you can use fabric, paramiko or other ssh tools. It would be easier. >> +1 >> >> Even compiling it yourself shouldn't be too difficult on Linux. >> >> I'm not retro just for the heck of it. There's no reason to avoid Python > 3.x but sometimes you just have to work with what you have. > I presume you meant this 3.x reference above in a hyperbolic sense e.g. as to mean "I absolutely can't upgrade the Python version I'm working with, at all." Nonetheless, I'd like to just point out, that at one place I work at one of the servers is also still using Python 2.5.4 with Paramiko without any problems -- so even a small version upgrade from where you're at might be enough to enable you to use things you currently cannot. I realise that even this might be impossible for you though. In such a case, (and apologies again if you already know this and it's not relevant) it's still often possible to have multiple versions of Python installed in a system, even if just in a user context/account and/or using something like virtualenv so as to leave the system wide installation untouched. E.g. just because an operating system has an old version of Python installed that absolutely cannot be touched, this doesn't necessarily mean that a user of the system cannot then have their own set of application binaries (including a newer version of Python) installed and use that instead of the system binaries to get their job done. (Again, I'm sure you know this already, so apologies if I'm telling you what you already know and am not helping here.) > Anyone willing to provide a couple million dollars and 12-18 months of > free 24/7/365.25 support? > I have to work with four different operating systems and three of the four > have two versions. > Most might have python installed but some don't. > I'm seriously not clear how these comments related to your question: In your original post you talked about a local server and a remote server, stating that you wanted the local server (with Python) to contact a remote server.. How does these 4 operating systems you mention factor into your original question? Only the local server needs Python. The remote only needs SSH. > My code has to be clean enough that people new to Python can understand it > when something goes south. > Fair enough. > Somewhat sorry for the rant. I just get tired of people saying "upgrade" > when that's not an option. > Fair enough again. Perhaps try be a bit more explicit about such non-negotiable constraints in future. While you mentioned a Python version in your original question, it wasn't really obvious as being a constraint and I certainly didn't interpret it as such. Best wishes, Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Fri Jun 27 23:40:57 2014 From: wprins at gmail.com (Walter Prins) Date: Fri, 27 Jun 2014 22:40:57 +0100 Subject: [Tutor] Better way to check *nix remote file age? In-Reply-To: References: Message-ID: Hi, On 26 June 2014 18:01, leam hall wrote: > On Thu, Jun 26, 2014 at 12:41 PM, Walter Prins wrote: >> On 26 June 2014 14:39, leam hall wrote: >> > Python 2.4.3 >> > Is there a better way to do this? >> I'd probably rather try Paramiko's SFTPClient and retrieve the file > Seem to not work on Python 2.4.3. What exactly did you try, and what error/output did you get? (I've had another look at Paramiko's homepage and it seems to support back to Python 2.2? [http://www.lag.net/paramiko/ ]) Walter From leamhall at gmail.com Sat Jun 28 00:20:47 2014 From: leamhall at gmail.com (Leam Hall) Date: Fri, 27 Jun 2014 18:20:47 -0400 Subject: [Tutor] Better way to check *nix remote file age? In-Reply-To: References: Message-ID: <53ADEE3F.70904@gmail.com> On 06/27/14 17:40, Walter Prins wrote: > Hi, > > On 26 June 2014 18:01, leam hall wrote: >> On Thu, Jun 26, 2014 at 12:41 PM, Walter Prins wrote: >>> On 26 June 2014 14:39, leam hall wrote: >>>> Python 2.4.3 >>>> Is there a better way to do this? >>> I'd probably rather try Paramiko's SFTPClient and retrieve the file >> Seem to not work on Python 2.4.3. > > What exactly did you try, and what error/output did you get? (I've > had another look at Paramiko's homepage and it seems to support back > to Python 2.2? [http://www.lag.net/paramiko/ ]) > > Walter Walter, I was cranky. Sorry. When I looked at ( http://www.paramiko.org ) it specified Python 2.6+ and 3.3+. That's echoed in the README ( https://github.com/paramiko/paramiko/blob/master/README ) I have not actually tested to see what breaks on a 2.4 box. The other option was to copy in the files and stat them. Trying to build a script that can be extended and not hit so much network bandwidth. The good thing about the way this is currently going is that a co-worker gave me a better Solaris solution that let me standardize the script for both Red Hat Linux and Solaris. Still need to figure out AIX. The possibility exists to build a python outside of the standard. The issue is supportability. Like many others I'm a contractor and need to make sure whatever I do is so boringly simple that I can train my replacement. On one hand, that's a good thing. If people use my scripts after I'm gone then hopefully they won't cuss too much. Since my exciting plans for the weekend have been canned I'll take a little time and think about how best to do this. Some of it is "what should I do with my career" type thinking. Some is just "hey, how does this work?". Thanks for not responding as poorly as I did. Leam -- http://31challenge.net http://31challenge.net/insight From crimevideogames at gmail.com Fri Jun 27 18:47:31 2014 From: crimevideogames at gmail.com (Abbas Haider) Date: Fri, 27 Jun 2014 09:47:31 -0700 Subject: [Tutor] Pydoc Message-ID: Hello, i am having problem envoking pydoc from command line. I have a windows 8 os, and python 2.7.x , and python is properly installed, python shell opens after i type python in command line. But pydoc only opens in certain folders. Could you provide any detail on how to use it? -------------- next part -------------- An HTML attachment was scrubbed... URL: From leamhall at gmail.com Sat Jun 28 00:37:05 2014 From: leamhall at gmail.com (Leam Hall) Date: Fri, 27 Jun 2014 18:37:05 -0400 Subject: [Tutor] Better way to check *nix remote file age? In-Reply-To: References: <1403808043.95406.YahooMailNeo@web163805.mail.gq1.yahoo.com> Message-ID: <53ADF211.1040506@gmail.com> On 06/27/14 06:10, Ra?l Cumplido wrote: > Hi, > > I would recommend also instead of doing an "ls -l" command doing > something to retrieve only the information you need: > > /bin/ls -ls | awk '{print $7,$8,$9, $10}' > > Jun 27 10:36 my_file > > > Then I would use timedelta instead where you can be more accurate with > the dates (in the example if the file is older than 62 days): > def too_old(text): > month, day, year = (text[0], text[1], > text[2] if ':' not in text[2] else > datetime.datetime.now().year) > time_difference = datetime.datetime.now() - datetime.datetime.strptime( > "{0}{1}{2}".format(month, day, year), '%b%d%Y') > return time_difference > datetime.timedelta(days=62) Ra?l, that looks interesting. Let me think through it. Leam -- http://31challenge.net http://31challenge.net/insight From wprins at gmail.com Sat Jun 28 01:27:07 2014 From: wprins at gmail.com (Walter Prins) Date: Sat, 28 Jun 2014 00:27:07 +0100 Subject: [Tutor] Better way to check *nix remote file age? In-Reply-To: <53ADEE3F.70904@gmail.com> References: <53ADEE3F.70904@gmail.com> Message-ID: Hi again ^^ On 27 June 2014 23:20, Leam Hall wrote: > On 06/27/14 17:40, Walter Prins wrote: >> On 26 June 2014 18:01, leam hall wrote: >>> On Thu, Jun 26, 2014 at 12:41 PM, Walter Prins wrote: >>>> On 26 June 2014 14:39, leam hall wrote: >>>>> Python 2.4.3 >>>>> Is there a better way to do this? >>>> I'd probably rather try Paramiko's SFTPClient and retrieve the file >>> Seem to not work on Python 2.4.3. >> What exactly did you try, and what error/output did you get? (I've >> had another look at Paramiko's homepage and it seems to support back >> to Python 2.2? [http://www.lag.net/paramiko/ ]) > Walter, I was cranky. Sorry. Not a problem, know the feeling. :) > When I looked at ( http://www.paramiko.org ) it specified Python 2.6+ and > 3.3+. That's echoed in the README ( > https://github.com/paramiko/paramiko/blob/master/README ) I have not > actually tested to see what breaks on a 2.4 box. Hmmm... OK. Interesting. Perhaps Paramiko head doesn't support it anymore on head in github... but it might still be possible to get an older version for Python 2.4, but I guess (having read the rest of your post already) that maybe this is not the best course of action for your situation -- I'll stop pushing this idea shortly, I promise! Nevertheless, continuing on... > The other option was to copy in the files and stat them. Trying to build a > script that can be extended and not hit so much network bandwidth. OK -- just to note, I think perhaps another small misunderstanding has slipped in somewhere. My suggestion isn't to actually retrieve the file *contents*, but rather to retrieve information *about* the file on the remote system. That means only the remote stats about the file travel over the wire, not the file contents itself. Hence it should be approximate as light, or lighter (give or take) than running an ssh command and retrieving the output I'd guess. > The good > thing about the way this is currently going is that a co-worker gave me a > better Solaris solution that let me standardize the script for both Red Hat > Linux and Solaris. Still need to figure out AIX. OK, I get that would be attractive for your use case. :) > The possibility exists to build a python outside of the standard. The issue > is supportability. Like many others I'm a contractor and need to make sure > whatever I do is so boringly simple that I can train my replacement. On one > hand, that's a good thing. If people use my scripts after I'm gone then > hopefully they won't cuss too much. I get that as well. (A big complicated custom Python + Paramiko install is rather not the simplest/most maintainable solution when all you want is to know if a given file is of a certain age... especially if the in-house Python knowledge is limited and you need to hand it over to in-house staff.) > Since my exciting plans for the weekend have been canned I'll take a little > time and think about how best to do this. Some of it is "what should I do > with my career" type thinking. Some is just "hey, how does this work?". Good luck... need to do some of the same sometime (e.g. what should I do with my career), but I digress. :) > Thanks for not responding as poorly as I did. No worries, hope you have a good weekend despite your plans being canned. Walter From beachkidken at gmail.com Sat Jun 28 19:59:04 2014 From: beachkidken at gmail.com (Ken G.) Date: Sat, 28 Jun 2014 13:59:04 -0400 Subject: [Tutor] Finding numeric day in a year... Message-ID: <53AF0268.3020708@gmail.com> I know the correct answer should be 001, but I keep getting 179 which is the correct answer for June 28, 2014 (I think). I tried using datecode in various places instead of today but I am still getting 179. Currently using Ubuntu 12.04.4 and Python 2.7. Thanks for any feedback and suggestion. PROGRAM DISPLAY: # datefind 03.py import datetime datecode = "20140101" # from database on file month = datecode[4:6] day = datecode[6:8] year = datecode[0:4] datecode = year + "-" + month + "-" + day today = datecode print today print print "Day of year: ", datetime.date.today().strftime("%j") TERMINAL DISPLAY: 2014-01-01 Day of year: 179 ------------------ (program exited with code: 0) Press return to continue -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Sat Jun 28 20:13:28 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 28 Jun 2014 19:13:28 +0100 Subject: [Tutor] Finding numeric day in a year... In-Reply-To: <53AF0268.3020708@gmail.com> References: <53AF0268.3020708@gmail.com> Message-ID: On 28/06/2014 18:59, Ken G. wrote: > I know the correct answer should be 001, but I keep getting 179 which is > the correct answer for June 28, 2014 (I think). I tried using datecode > in various places instead of today but I am still getting 179. Currently > using Ubuntu 12.04.4 and Python 2.7. Thanks for any feedback and suggestion. > > > PROGRAM DISPLAY: > > # datefind 03.py > > import datetime > > datecode = "20140101" # from database on file > > month = datecode[4:6] > day = datecode[6:8] > year = datecode[0:4] > datecode = year + "-" + month + "-" + day > today = datecode > print today > print > > print "Day of year: ", datetime.date.today().strftime("%j") datetime.date.today() ? :) > > TERMINAL DISPLAY: > > 2014-01-01 > > Day of year: 179 > > ------------------ > (program exited with code: 0) > Press return to continue > -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From fomcl at yahoo.com Sat Jun 28 20:20:03 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sat, 28 Jun 2014 11:20:03 -0700 Subject: [Tutor] Finding numeric day in a year... In-Reply-To: <53AF0268.3020708@gmail.com> References: <53AF0268.3020708@gmail.com> Message-ID: <1403979603.78244.YahooMailNeo@web163803.mail.gq1.yahoo.com> >________________________________ > From: Ken G. >To: tutor at python.org >Sent: Saturday, June 28, 2014 7:59 PM >Subject: [Tutor] Finding numeric day in a year... > > > >I know the correct answer should be 001, but I keep getting 179 which is the correct answer for June 28, 2014 (I think). I tried using datecode in various places instead of today but I am still getting 179. Currently using Ubuntu 12.04.4 and Python 2.7. Thanks for any feedback and suggestion. > > >PROGRAM DISPLAY: > ># datefind 03.py > >import datetime > >datecode = "20140101" # from database on file > >month = datecode[4:6] >day? = datecode[6:8] >year? = datecode[0:4] >datecode = year + "-" + month + "-" + day >today = datecode >print today >print > >print "Day of year: ", datetime.date.today().strftime("%j") > > >TERMINAL DISPLAY: > >2014-01-01 > >Day of year:? 179 > >------------------ >(program exited with code: 0) >Press return to continue Hello, Your variable 'today' and the datetime function 'today()' are not the same thing. You need strptime to parse the date string (and tell it the format is yyyymmdd), then strftime to format it using the day-of-year format '%j' >>> datetime.datetime.strptime(datecode, "%Y%m%d").strftime("%j") '001' From beachkidken at gmail.com Sat Jun 28 20:25:42 2014 From: beachkidken at gmail.com (Ken G.) Date: Sat, 28 Jun 2014 14:25:42 -0400 Subject: [Tutor] Finding numeric day in a year... In-Reply-To: References: <53AF0268.3020708@gmail.com> Message-ID: <53AF08A6.7060109@gmail.com> On 06/28/2014 02:13 PM, Mark Lawrence wrote: > On 28/06/2014 18:59, Ken G. wrote: >> I know the correct answer should be 001, but I keep getting 179 which is >> the correct answer for June 28, 2014 (I think). I tried using datecode >> in various places instead of today but I am still getting 179. Currently >> using Ubuntu 12.04.4 and Python 2.7. Thanks for any feedback and >> suggestion. >> >> >> PROGRAM DISPLAY: >> >> # datefind 03.py >> >> import datetime >> >> datecode = "20140101" # from database on file >> >> month = datecode[4:6] >> day = datecode[6:8] >> year = datecode[0:4] >> datecode = year + "-" + month + "-" + day >> today = datecode >> print today >> print >> >> print "Day of year: ", datetime.date.today().strftime("%j") > > datetime.date.today() ? :) Thanks but it came out: Day of year: 2014-06-28 What I wanted was 001 for 01-01-2014 as specified in datecode. Ken From steve at pearwood.info Sat Jun 28 20:36:43 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 29 Jun 2014 04:36:43 +1000 Subject: [Tutor] Finding numeric day in a year... In-Reply-To: <53AF0268.3020708@gmail.com> References: <53AF0268.3020708@gmail.com> Message-ID: <20140628183643.GJ13014@ando> On Sat, Jun 28, 2014 at 01:59:04PM -0400, Ken G. wrote: > I know the correct answer should be 001, but I keep getting 179 which is > the correct answer for June 28, 2014 (I think). Day 179 of 2014 is June 28 according to my diary, which happens to be today. (Well, I'm in Australia, so for me it is yesterday, but for you it is today.) So when you do this: print "Day of year: ", datetime.date.today().strftime("%j") it prints the day of the year for *today*. If you try it again tomorrow, it will print 180. If you wait another week, it will print 187. The existence of a variable "today" is irrelevant and has nothing to do with datetime.date.today. Consider this example: py> import datetime py> today = "the first day of the rest of your life" py> datetime.date.today # just inspect the method py> datetime.date.today() # actually call the method datetime.date(2014, 6, 29) py> today 'the first day of the rest of your life' > I tried using datecode > in various places instead of today but I am still getting 179. datecode holds a string, not a date object. To call date methods, you need a date object. py> the_day = datetime.date(2014, 1, 1) py> the_day.strftime("%j") '001' If you have a date as a string, you can turn it into a date object like this: py> the_day = datetime.datetime.strptime('20140101', '%Y%m%d') py> the_day.strftime("%j") '001' -- Steven From alan.gauld at btinternet.com Sat Jun 28 20:39:20 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 28 Jun 2014 19:39:20 +0100 Subject: [Tutor] Finding numeric day in a year... In-Reply-To: <53AF0268.3020708@gmail.com> References: <53AF0268.3020708@gmail.com> Message-ID: On 28/06/14 18:59, Ken G. wrote: > datecode = "20140101" # from database on file > > month = datecode[4:6] > day = datecode[6:8] > year = datecode[0:4] use strptime() to parse dates, its much more reliable. > datecode = year + "-" + month + "-" + day > today = datecode And use strftime() to format them... > print today > print > > print "Day of year: ", datetime.date.today().strftime("%j") This returns todays date whenever you run it. It has nothing to do with the dates above. But if you use strptime() to get the date from your string you should then be able to use strftime to convert it to julian. BTW You say you get it from "database on file". Now if that is a real database such as SQLite you will find functions there to convert it to julian at source... which is easier than reading it as a string, parsing it, and then converting it back to a date again... HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From beachkidken at gmail.com Sat Jun 28 21:03:15 2014 From: beachkidken at gmail.com (Ken G.) Date: Sat, 28 Jun 2014 15:03:15 -0400 Subject: [Tutor] Finding numeric day in a year... In-Reply-To: <1403979603.78244.YahooMailNeo@web163803.mail.gq1.yahoo.com> References: <53AF0268.3020708@gmail.com> <1403979603.78244.YahooMailNeo@web163803.mail.gq1.yahoo.com> Message-ID: <53AF1173.5040705@gmail.com> On 06/28/2014 02:20 PM, Albert-Jan Roskam wrote: >> ________________________________ >> From: Ken G. >> To: tutor at python.org >> Sent: Saturday, June 28, 2014 7:59 PM >> Subject: [Tutor] Finding numeric day in a year... >> >> >> >> I know the correct answer should be 001, but I keep getting 179 which is the correct answer for June 28, 2014 (I think). I tried using datecode in various places instead of today but I am still getting 179. Currently using Ubuntu 12.04.4 and Python 2.7. Thanks for any feedback and suggestion. >> >> >> PROGRAM DISPLAY: >> >> # datefind 03.py >> >> import datetime >> >> datecode = "20140101" # from database on file >> >> month = datecode[4:6] >> day = datecode[6:8] >> year = datecode[0:4] >> datecode = year + "-" + month + "-" + day >> today = datecode >> print today >> print >> >> print "Day of year: ", datetime.date.today().strftime("%j") >> >> >> TERMINAL DISPLAY: >> >> 2014-01-01 >> >> Day of year: 179 >> >> ------------------ >> (program exited with code: 0) >> Press return to continue > Hello, > > Your variable 'today' and the datetime function 'today()' are not the same thing. You need strptime to parse the date string (and tell it the format is yyyymmdd), then strftime to format it using the day-of-year format '%j' > >>>> datetime.datetime.strptime(datecode, "%Y%m%d").strftime("%j") > '001' > Thanks! That did the trick. Thank you. Ken From beachkidken at gmail.com Sat Jun 28 21:07:21 2014 From: beachkidken at gmail.com (Ken G.) Date: Sat, 28 Jun 2014 15:07:21 -0400 Subject: [Tutor] Finding numeric day in a year... In-Reply-To: <20140628183643.GJ13014@ando> References: <53AF0268.3020708@gmail.com> <20140628183643.GJ13014@ando> Message-ID: <53AF1269.6000501@gmail.com> On 06/28/2014 02:36 PM, Steven D'Aprano wrote: > On Sat, Jun 28, 2014 at 01:59:04PM -0400, Ken G. wrote: >> I know the correct answer should be 001, but I keep getting 179 which is >> the correct answer for June 28, 2014 (I think). > Day 179 of 2014 is June 28 according to my diary, which happens to be > today. (Well, I'm in Australia, so for me it is yesterday, but for you > it is today.) So when you do this: > > print "Day of year: ", datetime.date.today().strftime("%j") > > it prints the day of the year for *today*. If you try it again tomorrow, > it will print 180. If you wait another week, it will print 187. > > The existence of a variable "today" is irrelevant and has nothing to do > with datetime.date.today. Consider this example: > > py> import datetime > py> today = "the first day of the rest of your life" > py> datetime.date.today # just inspect the method > > py> datetime.date.today() # actually call the method > datetime.date(2014, 6, 29) > py> today > 'the first day of the rest of your life' > > > >> I tried using datecode >> in various places instead of today but I am still getting 179. > datecode holds a string, not a date object. To call date methods, you > need a date object. > > py> the_day = datetime.date(2014, 1, 1) > py> the_day.strftime("%j") > '001' > > > If you have a date as a string, you can turn it into a date object like > this: > > py> the_day = datetime.datetime.strptime('20140101', '%Y%m%d') > py> the_day.strftime("%j") > '001' > > Thank you. Much appreciated. Ken From beachkidken at gmail.com Sat Jun 28 21:11:21 2014 From: beachkidken at gmail.com (Ken G.) Date: Sat, 28 Jun 2014 15:11:21 -0400 Subject: [Tutor] Finding numeric day in a year...[SOLVED] In-Reply-To: References: <53AF0268.3020708@gmail.com> Message-ID: <53AF1359.5050507@gmail.com> On 06/28/2014 02:39 PM, Alan Gauld wrote: > On 28/06/14 18:59, Ken G. wrote: > >> datecode = "20140101" # from database on file >> >> month = datecode[4:6] >> day = datecode[6:8] >> year = datecode[0:4] > > use strptime() to parse dates, its much more reliable. > >> datecode = year + "-" + month + "-" + day >> today = datecode > > And use strftime() to format them... > >> print today >> print >> >> print "Day of year: ", datetime.date.today().strftime("%j") > > This returns todays date whenever you run it. > It has nothing to do with the dates above. > But if you use strptime() to get the date from your string you should > then be able to use strftime to convert it to julian. > > BTW You say you get it from "database on file". > Now if that is a real database such as SQLite you will find functions > there to convert it to julian at source... which is easier than > reading it as a string, parsing it, and then converting it back to a > date again... > > > HTH Ah, it is a Python database consisting of year-month-date and past drawn lotto numbers. I have just noticed that I am missing some twice weekly drawing. I am checking which one I am missing so far this year. Thanks to all that helps me solved the puzzle. Ken From breamoreboy at yahoo.co.uk Sat Jun 28 22:35:47 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 28 Jun 2014 21:35:47 +0100 Subject: [Tutor] Finding numeric day in a year... In-Reply-To: References: <53AF0268.3020708@gmail.com> Message-ID: On 28/06/2014 19:39, Alan Gauld wrote: > On 28/06/14 18:59, Ken G. wrote: > > BTW You say you get it from "database on file". > Now if that is a real database such as SQLite you will find functions > there to convert it to julian at source... which is easier than reading > it as a string, parsing it, and then converting it back to a date again... > Well spotted :) Ken G. please see https://docs.python.org/3/library/sqlite3.html#default-adapters-and-converters -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From beachkidken at gmail.com Sun Jun 29 01:08:33 2014 From: beachkidken at gmail.com (Ken G.) Date: Sat, 28 Jun 2014 19:08:33 -0400 Subject: [Tutor] Finding numeric day in a year...[SOLVED] In-Reply-To: References: <53AF0268.3020708@gmail.com> Message-ID: <53AF4AF1.1030302@gmail.com> On 06/28/2014 04:35 PM, Mark Lawrence wrote: > On 28/06/2014 19:39, Alan Gauld wrote: >> On 28/06/14 18:59, Ken G. wrote: >> >> BTW You say you get it from "database on file". >> Now if that is a real database such as SQLite you will find functions >> there to convert it to julian at source... which is easier than reading >> it as a string, parsing it, and then converting it back to a date >> again... >> > > Well spotted :) Ken G. please see > https://docs.python.org/3/library/sqlite3.html#default-adapters-and-converters Thank you. Will take a look at it. Ken From alan.gauld at btinternet.com Mon Jun 30 00:41:45 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 29 Jun 2014 23:41:45 +0100 Subject: [Tutor] What are your favourite unofficial resources Message-ID: I'm looking for tips for an appendix to a book that I'm working on. What are the best unofficial (ie not python.org) resources for people who have learned the basics but are not experts yet? ie Typical tutor list "graduates"... I'm thinking about web sites, blogs, books, videos etc. Anything that might be worth knowing about. I've got a few of my own - Activestate, O'Reilly, ByteOfPython, PythonChallenge, ShowMeDo etc. But I thought the tutor list readers might be an interesting source of alternatives that I hadn't thought of, or even heard of. All contributions considered :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From codemonkey at inbox.com Mon Jun 30 04:05:06 2014 From: codemonkey at inbox.com (Deb Wyatt) Date: Sun, 29 Jun 2014 18:05:06 -0800 Subject: [Tutor] What are your favourite unofficial resources In-Reply-To: Message-ID: <1DBCAA91C5E.000005D4codemonkey@inbox.com> > -----Original Message----- > From: alan.gauld at btinternet.com > Sent: Sun, 29 Jun 2014 23:41:45 +0100 > To: tutor at python.org > Subject: [Tutor] What are your favourite unofficial resources > > I'm looking for tips for an appendix to a book that > I'm working on. > > What are the best unofficial (ie not python.org) > resources for people who have learned the basics > but are not experts yet? ie Typical tutor list > "graduates"... > > I'm thinking about web sites, blogs, books, videos etc. > Anything that might be worth knowing about. > > I've got a few of my own - Activestate, O'Reilly, > ByteOfPython, PythonChallenge, ShowMeDo etc. > > But I thought the tutor list readers might be an > interesting source of alternatives that I hadn't > thought of, or even heard of. > > All contributions considered :-) > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor codecademy.com, codingbat.com, checkio.com Head First Python. There are some more books but I can't think of them right now. Deb in WA, USA ____________________________________________________________ Protect your computer files with professional cloud backup. Get PCRx Backup and upload unlimited files automatically. Learn more at http://backup.pcrx.com/mail From wrw at mac.com Mon Jun 30 04:26:38 2014 From: wrw at mac.com (William Ray Wing) Date: Sun, 29 Jun 2014 22:26:38 -0400 Subject: [Tutor] What are your favourite unofficial resources In-Reply-To: References: Message-ID: Probably obvious (meaning you will get them both 50+ times), but I like both Stackoverflow.com and Doug Hellmann?s site. Thanks, Bill On Jun 29, 2014, at 6:41 PM, Alan Gauld wrote: > I'm looking for tips for an appendix to a book that > I'm working on. > > What are the best unofficial (ie not python.org) > resources for people who have learned the basics > but are not experts yet? ie Typical tutor list > "graduates"... > > I'm thinking about web sites, blogs, books, videos etc. > Anything that might be worth knowing about. > > I've got a few of my own - Activestate, O'Reilly, > ByteOfPython, PythonChallenge, ShowMeDo etc. > > But I thought the tutor list readers might be an > interesting source of alternatives that I hadn't > thought of, or even heard of. > > All contributions considered :-) > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From varunaseneviratna at gmail.com Mon Jun 30 02:46:33 2014 From: varunaseneviratna at gmail.com (Varuna Seneviratna) Date: Mon, 30 Jun 2014 06:16:33 +0530 Subject: [Tutor] What are your favourite unofficial resources In-Reply-To: References: Message-ID: On 30 June 2014 04:11, Alan Gauld wrote: > I'm looking for tips for an appendix to a book that > I'm working on. > > What are the best unofficial (ie not python.org) > resources for people who have learned the basics > but are not experts yet? ie Typical tutor list > "graduates"... > > I'm thinking about web sites, blogs, books, videos etc. > Anything that might be worth knowing about. > > I've got a few of my own - Activestate, O'Reilly, > ByteOfPython, PythonChallenge, ShowMeDo etc. > > But I thought the tutor list readers might be an > interesting source of alternatives that I hadn't > thought of, or even heard of. > > All contributions considered :-) > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor Hope this helps you http://coderbyte.com/ From memilanuk at gmail.com Mon Jun 30 06:47:53 2014 From: memilanuk at gmail.com (memilanuk) Date: Sun, 29 Jun 2014 21:47:53 -0700 Subject: [Tutor] What are your favourite unofficial resources In-Reply-To: References: Message-ID: <53B0EBF9.3080807@gmail.com> On 06/29/2014 03:41 PM, Alan Gauld wrote: > I'm looking for tips for an appendix to a book that > I'm working on. > > What are the best unofficial (ie not python.org) > resources for people who have learned the basics > but are not experts yet? ie Typical tutor list > "graduates"... > > I'm thinking about web sites, blogs, books, videos etc. > Anything that might be worth knowing about. > > I've got a few of my own - Activestate, O'Reilly, > ByteOfPython, PythonChallenge, ShowMeDo etc. > > But I thought the tutor list readers might be an > interesting source of alternatives that I hadn't > thought of, or even heard of. > > All contributions considered :-) > Some stuff that I've bookmarked over time... not all of which I've actually go around to making use of :( http://interactivepython.org/courselib/static/pythonds/index.html http://rosettacode.org/wiki/Rosetta_Code http://rosalind.info/problems/locations/ http://nullege.com/ From memilanuk at gmail.com Mon Jun 30 06:47:53 2014 From: memilanuk at gmail.com (memilanuk) Date: Sun, 29 Jun 2014 21:47:53 -0700 Subject: [Tutor] What are your favourite unofficial resources In-Reply-To: References: Message-ID: <53B0EBF9.3080807@gmail.com> On 06/29/2014 03:41 PM, Alan Gauld wrote: > I'm looking for tips for an appendix to a book that > I'm working on. > > What are the best unofficial (ie not python.org) > resources for people who have learned the basics > but are not experts yet? ie Typical tutor list > "graduates"... > > I'm thinking about web sites, blogs, books, videos etc. > Anything that might be worth knowing about. > > I've got a few of my own - Activestate, O'Reilly, > ByteOfPython, PythonChallenge, ShowMeDo etc. > > But I thought the tutor list readers might be an > interesting source of alternatives that I hadn't > thought of, or even heard of. > > All contributions considered :-) > Some stuff that I've bookmarked over time... not all of which I've actually go around to making use of :( http://interactivepython.org/courselib/static/pythonds/index.html http://rosettacode.org/wiki/Rosetta_Code http://rosalind.info/problems/locations/ http://nullege.com/ From linux at barrowhillfarm.org.uk Mon Jun 30 13:05:49 2014 From: linux at barrowhillfarm.org.uk (Bob Williams) Date: Mon, 30 Jun 2014 12:05:49 +0100 Subject: [Tutor] What are your favourite unofficial resources In-Reply-To: References: Message-ID: <53B1448D.3040802@barrowhillfarm.org.uk> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 29/06/14 23:41, Alan Gauld wrote: > I'm looking for tips for an appendix to a book that I'm working > on. > > What are the best unofficial (ie not python.org) resources for > people who have learned the basics but are not experts yet? ie > Typical tutor list "graduates"... > > I'm thinking about web sites, blogs, books, videos etc. Anything > that might be worth knowing about. > > I've got a few of my own - Activestate, O'Reilly, ByteOfPython, > PythonChallenge, ShowMeDo etc. > > But I thought the tutor list readers might be an interesting source > of alternatives that I hadn't thought of, or even heard of. > > All contributions considered :-) > Python Module of the Week Bob - -- Bob Williams System: Linux 3.11.10-17-desktop Distro: openSUSE 13.1 (x86_64) with KDE Development Platform: 4.13.2 Uptime: 06:00am up 1 day 20:14, 0 users, load average: 0.04, 0.05, 0.05 -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.22 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iEYEARECAAYFAlOxRIsACgkQ0Sr7eZJrmU5kfQCgkE0dRzO1G+o/GX78s4U7oe3U mNQAoJ+ayTo+79Xj+9JaHoMrflxDHCzW =uWQJ -----END PGP SIGNATURE----- From illusiontechniques at gmail.com Mon Jun 30 14:52:21 2014 From: illusiontechniques at gmail.com (C Smith) Date: Mon, 30 Jun 2014 08:52:21 -0400 Subject: [Tutor] What are your favourite unofficial resources In-Reply-To: <53B1448D.3040802@barrowhillfarm.org.uk> References: <53B1448D.3040802@barrowhillfarm.org.uk> Message-ID: Learning Python Design Patterns, by Gennadiy Zlobin Let us know when your book is done! On Mon, Jun 30, 2014 at 7:05 AM, Bob Williams wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On 29/06/14 23:41, Alan Gauld wrote: >> I'm looking for tips for an appendix to a book that I'm working >> on. >> >> What are the best unofficial (ie not python.org) resources for >> people who have learned the basics but are not experts yet? ie >> Typical tutor list "graduates"... >> >> I'm thinking about web sites, blogs, books, videos etc. Anything >> that might be worth knowing about. >> >> I've got a few of my own - Activestate, O'Reilly, ByteOfPython, >> PythonChallenge, ShowMeDo etc. >> >> But I thought the tutor list readers might be an interesting source >> of alternatives that I hadn't thought of, or even heard of. >> >> All contributions considered :-) >> > > Python Module of the Week > > Bob > - -- > Bob Williams > System: Linux 3.11.10-17-desktop > Distro: openSUSE 13.1 (x86_64) with KDE Development Platform: 4.13.2 > Uptime: 06:00am up 1 day 20:14, 0 users, load average: 0.04, 0.05, 0.05 > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v2.0.22 (GNU/Linux) > Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ > > iEYEARECAAYFAlOxRIsACgkQ0Sr7eZJrmU5kfQCgkE0dRzO1G+o/GX78s4U7oe3U > mNQAoJ+ayTo+79Xj+9JaHoMrflxDHCzW > =uWQJ > -----END PGP SIGNATURE----- > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From avinashsajjan at gmail.com Mon Jun 30 12:10:29 2014 From: avinashsajjan at gmail.com (avinash sajjanshetty) Date: Mon, 30 Jun 2014 15:40:29 +0530 Subject: [Tutor] looking suggestions/codereview on my app which alerts users on the product price change Message-ID: I have posted this on codereview.stackexchange, but looking from suggestions from here as well. here is the link: http://codereview.stackexchange.com/questions/55674/e-commerce-product-price-tracker I appreciate any help. Thank you :) regards, avi -------------- next part -------------- An HTML attachment was scrubbed... URL: From varunaseneviratna at gmail.com Mon Jun 30 19:06:32 2014 From: varunaseneviratna at gmail.com (Varuna Seneviratna) Date: Mon, 30 Jun 2014 22:36:32 +0530 Subject: [Tutor] What are your favourite unofficial resources In-Reply-To: References: Message-ID: On 30 June 2014 04:11, Alan Gauld wrote: > I'm looking for tips for an appendix to a book that > I'm working on. > > What are the best unofficial (ie not python.org) > resources for people who have learned the basics > but are not experts yet? ie Typical tutor list > "graduates"... > > I'm thinking about web sites, blogs, books, videos etc. > Anything that might be worth knowing about. > > I've got a few of my own - Activestate, O'Reilly, > ByteOfPython, PythonChallenge, ShowMeDo etc. > > But I thought the tutor list readers might be an > interesting source of alternatives that I hadn't > thought of, or even heard of. > > All contributions considered :-) > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor http://learncodethehardway.org/