From davestechshop at gmail.com Fri Jun 1 00:21:22 2012 From: davestechshop at gmail.com (Dave) Date: Thu, 31 May 2012 18:21:22 -0400 Subject: [Tutor] iPython check if user running script is root user (Linux) Message-ID: Hi. What is the right way to have an iPython script check to see if the user is currently root? Here's how I do it in bash scripts: ########################## CHECK USERNAME PRIVILEGE ################################ if [ $(id -u) != "0" ];then echo "This script must be run as root." exit 1 fi -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Jun 1 00:29:17 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 31 May 2012 23:29:17 +0100 Subject: [Tutor] iPython check if user running script is root user (Linux) In-Reply-To: References: Message-ID: On 31/05/12 23:21, Dave wrote: > Hi. What is the right way to have an iPython script check to see if the > user is currently root? I don;t know anything much about ipython but so far as IO know its just a shell on standard python so: os.getuid() should work. In general if you are trying to replace bash with python look at the os, shutil, glob, os.path modules as a first port of call. hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From corey at octayn.net Fri Jun 1 00:30:01 2012 From: corey at octayn.net (Corey Richardson) Date: Thu, 31 May 2012 18:30:01 -0400 Subject: [Tutor] iPython check if user running script is root user (Linux) In-Reply-To: References: Message-ID: <20120531183001.3d3cd927@Ulysses> On Thu, 31 May 2012 18:21:22 -0400 Dave wrote: > Hi. What is the right way to have an iPython script check to see if > the user is currently root? > > Here's how I do it in bash scripts: > > > ########################## CHECK USERNAME PRIVILEGE > ################################ > > if [ $(id -u) != "0" ];then > echo "This script must be run as root." > exit 1 > fi import os if os.environ['USER'] != 'root': print('This script must be run as root.') exit(1) -- Corey Richardson From emile at fenx.com Fri Jun 1 00:30:28 2012 From: emile at fenx.com (Emile van Sebille) Date: Thu, 31 May 2012 15:30:28 -0700 Subject: [Tutor] iPython check if user running script is root user (Linux) In-Reply-To: References: Message-ID: On 5/31/2012 3:21 PM Dave said... > Hi. What is the right way to have an iPython script check to see if the > user is currently root? Googling for "python check if user is root" yields the answer: import os, sys # if not root...kick out if not os.geteuid()==0: sys.exit("\nOnly root can run this script\n") See http://code.activestate.com/recipes/299410-root-access-required-to-run-a-script/ Emile From davestechshop at gmail.com Fri Jun 1 01:18:09 2012 From: davestechshop at gmail.com (Dave) Date: Thu, 31 May 2012 19:18:09 -0400 Subject: [Tutor] iPython check if user running script is root user (Linux) In-Reply-To: References: Message-ID: Thanks. I like the integer-based option. Since this is my first question to the list, is it appropriate to reply with a "thanks, that solved it" or is that considered unnecessary? On Thu, May 31, 2012 at 6:30 PM, Emile van Sebille wrote: > On 5/31/2012 3:21 PM Dave said... > > Hi. What is the right way to have an iPython script check to see if the >> user is currently root? >> > > Googling for "python check if user is root" yields the answer: > > > import os, sys > > # if not root...kick out > if not os.geteuid()==0: > sys.exit("\nOnly root can run this script\n") > > > See http://code.activestate.com/**recipes/299410-root-access-** > required-to-run-a-script/ > > Emile > > > ______________________________**_________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Jun 1 08:32:52 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 01 Jun 2012 07:32:52 +0100 Subject: [Tutor] iPython check if user running script is root user (Linux) In-Reply-To: References: Message-ID: On 01/06/12 00:18, Dave wrote: > Thanks. I like the integer-based option. > > Since this is my first question to the list, is it appropriate to reply > with a "thanks, that solved it" or is that considered unnecessary? Its not necessary but its not frowned on either. It is most useful where numerous different options are being suggested since it indicates to future googlers which option worked best for the OP... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From koendemunter at hotmail.com Fri Jun 1 09:42:15 2012 From: koendemunter at hotmail.com (Koen De Munter) Date: Fri, 1 Jun 2012 09:42:15 +0200 Subject: [Tutor] Masked arrays & scipy.ndimage. Message-ID: Dear tutors, Given an image, I want to generate another image with the mean values of the pixels in their neighbourhood, thereby ignoring some of the neighbouring pixels (e.g. the padded boundary). I hoped I could use masked arrays for that, but apparently, this does not work. -- def fnc(buffer) ???? return numpy.mean(buffer) mean = scipy.ndimage.generic_filter(masked_array, fnc, footprint = f, mode = 'constant', cval = 0.0) -- Is there a way to get around this issue, or do I have to accept the filter function can not handle masked arrays? Thanks in advance! Sincerely, Koen From steve at pearwood.info Fri Jun 1 10:50:15 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 01 Jun 2012 18:50:15 +1000 Subject: [Tutor] Masked arrays & scipy.ndimage. In-Reply-To: References: Message-ID: <4FC88247.8060201@pearwood.info> Koen De Munter wrote: > Dear tutors, > > Given an image, I want to generate another image with the mean values of > the pixels in their neighbourhood, thereby ignoring some of the > neighbouring pixels (e.g. the padded boundary). I hoped I could use > masked arrays for that, but apparently, this does not work. Define "does not work". Do you get an exception? Your computer crashes? You get a result, but it's not the result you were expecting? Something else? > -- > def fnc(buffer) > return numpy.mean(buffer) > mean = scipy.ndimage.generic_filter(masked_array, fnc, footprint = f, mode = 'constant', cval = 0.0) scipy, even more than numpy, is a fairly specialised library. You may have better luck asking this question on a dedicated scipy mailing list. > Is there a way to get around this issue, or do I have to accept the filter function can not handle masked arrays? What does the documentation for generic_filter say? What happens if you test it with a really small, simple example, say an array of just five or six values? Can you show us what you expect to get and what you actually get? -- Steven From amgaweda at gmail.com Fri Jun 1 15:12:08 2012 From: amgaweda at gmail.com (Adam) Date: Fri, 01 Jun 2012 09:12:08 -0400 Subject: [Tutor] Benefit/Disadvantage to storing data as dictionary vs. in a class Message-ID: <4FC8BFA8.1060004@gmail.com> I'm working on a class that handles multiple rooms that generate a large amount of data. Currently my class model looks something like this (more apologies for any indentation errors): Class Model: rooms= {} for z in range(num_of_zones): for i in range(24): tmp[i] = { VAR:0, SD:0, AVG:0, READINGS:[] } tmp[i]['updated'] = datetime.utcnow() for j in OT_RANGE: tmp[i][j] = { VAR:0, SD:0, AVG:0, READINGS:[] } rooms[z] = tmp In case that gets complicated, I'm looking to store readings based off the current hour and current outside temperature. The Model class has other variables and functions that it uses to compare and manipulate the data as it comes in. My question is in regards to the storing all this data in a dictionary, which makes it easy to reference self.rooms[z][12][85][AVG]; however is this better/worse or the same as creating a 'Room' class to store the data? Since a 'Room' only contains a large amount of data, but doesn't contain any functions, which form of storing the data is considered 'better'? Thank you, Adam From koendemunter at hotmail.com Fri Jun 1 15:16:06 2012 From: koendemunter at hotmail.com (Koen De Munter) Date: Fri, 1 Jun 2012 15:16:06 +0200 Subject: [Tutor] Masked arrays & scipy.ndimage. In-Reply-To: <4FC88247.8060201@pearwood.info> References: , <4FC88247.8060201@pearwood.info> Message-ID: ---------------------------------------- > Date: Fri, 1 Jun 2012 18:50:15 +1000 > From: steve at pearwood.info > To: tutor at python.org > Subject: Re: [Tutor] Masked arrays & scipy.ndimage. > > Koen De Munter wrote: >> Dear tutors, >> >> Given an image, I want to generate another image with the mean values of >> the pixels in their neighbourhood, thereby ignoring some of the >> neighbouring pixels (e.g. the padded boundary). I hoped I could use >> masked arrays for that, but apparently, this does not work. > > Define "does not work". > > Do you get an exception? Your computer crashes? You get a result, but it's not > the result you were expecting? Something else? > > > >> -- >> def fnc(buffer) >> return numpy.mean(buffer) > >> mean = scipy.ndimage.generic_filter(masked_array, fnc, footprint = f, mode = 'constant', cval = 0.0) > > > scipy, even more than numpy, is a fairly specialised library. You may have > better luck asking this question on a dedicated scipy mailing list. > >> Is there a way to get around this issue, or do I have to accept the filter function can not handle masked arrays? > > What does the documentation for generic_filter say? > > What happens if you test it with a really small, simple example, say an array > of just five or six values? Can you show us what you expect to get and what > you actually get? > > > > -- > Steven > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Hi Steven, Thanks for your reply. 1) "does not work" ------------------ With this I mean that each time, all pixels belonging to the neighbourhood are involved in the calculation of the mean value. My neighbourhood is a circle, defined in a binary square footprint array (1 = neighbourhood, 0 = not neighbourhood). With a masked array, I aim to command the filter method as follows: "ignore those pixels that are identified by the mask array as 'invalid'". But, it seems the generic_method doesn't work with masked arrays: it only takes account for the data part of a masked array, not the mask part. So: result is not what I expect to get. For the bigger story I can refer to the following link: http://stackoverflow.com/questions/10683596/efficiently-calculating-boundary-adapted-neighbourhood-average 2) A small example ------------------ d = array([[0, 0, 0, 0],[0, 1, 6, 0],[0, 0, 0, 0]]) #this represents an image array [1, 6], surrounded by zeroes d = d.astype(float) m = array([[1, 1, 1, 1],[1, 0, 0, 1],[1, 1, 1, 1]]) #the ones in the mask have a meaning of 'invalid' md = numpy.ma.masked_array(d, mask = m) f = array([[0, 1, 0],[1, 1, 1],[0, 1, 0]]) #small footprint def fnc(buffer): return numpy.mean(buffer) avg = scipy.ndimage.generic_filter(md, fnc, footprint = f, mode = 'constant', cval = 0.0) --> what I expect to get: array([[ 0. , 1. , 6. , 0. ], ?????? [ 1. , 3.5, 3.5, 6. ], ?????? [ 0. , 1. , 6. , 0. ]]) --> what I actually get: array([[ 0. , 0.2, 1.2, 0. ], ?????? [ 0.2, 1.4, 1.4, 1.2], ?????? [ 0. , 0.2, 1.2, 0. ]]) Footprint f has 5 elements. I expect this number to change according to the number of elements that should be ignored. But that doesn't. The code as I presented above always calculates the mean of the full 5 members of the footprint/neighbourhood. Regards, Koen From eire1130 at gmail.com Fri Jun 1 16:24:09 2012 From: eire1130 at gmail.com (James Reynolds) Date: Fri, 1 Jun 2012 10:24:09 -0400 Subject: [Tutor] Benefit/Disadvantage to storing data as dictionary vs. in a class In-Reply-To: <4FC8BFA8.1060004@gmail.com> References: <4FC8BFA8.1060004@gmail.com> Message-ID: On Fri, Jun 1, 2012 at 9:12 AM, Adam wrote: > I'm working on a class that handles multiple rooms that generate a large > amount of data. Currently my class model looks something like this (more > apologies for any indentation errors): > Class Model: > rooms= {} > for z in range(num_of_zones): > for i in range(24): > tmp[i] = { VAR:0, SD:0, AVG:0, READINGS:[] } > tmp[i]['updated'] = datetime.utcnow() > for j in OT_RANGE: > tmp[i][j] = { VAR:0, SD:0, AVG:0, READINGS:[] } > rooms[z] = tmp > > In case that gets complicated, I'm looking to store readings based off the > current hour and current outside temperature. The Model class has other > variables and functions that it uses to compare and manipulate the data as > it comes in. > > My question is in regards to the storing all this data in a dictionary, > which makes it easy to reference self.rooms[z][12][85][AVG]; however is > this better/worse or the same as creating a 'Room' class to store the data? > Since a 'Room' only contains a large amount of data, but doesn't contain > any functions, which form of storing the data is considered 'better'? > > Thank you, > Adam > ______________________________**_________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > If you have functions (or in this case methods) which will be used on a certain 'objects' data structure, a class representation of the data is the natural fit. Otherwise, you will need to pass the dictionary around to various functions and this will be seriously confusing. However, you probably should consider how to represent that data within a class body, that is, it is extremely confusing to access data such as self.rooms[z][12][85][AVG]. Instead, you should break down what the data structure into like-things and how they are related, for example, you might consider something like this: class Weather(object): def __init__(self, temp, precipitation, humidity, bara_pres): self. temp = temp self. temp = precipitation self. temp = humidity self. temp = bara_pres def my_func(self, *args,**kwargs): pass #do stuff here However, if you need to either further define the datapoints of if you want the datapoints to be able to "do things" then you could do something like this: class Temperature(object): def __init__(self, temp): self. temp = temp self.other_stuff_about_temp = stuff def my_temp_func(self, *args,**kwargs): pass #do stuff here class Weather(object): def __init__(self, temp, precipitation, humidity, bara_pres): self. temp = Temperature(temp) self.precipitation = precipitation self.humidity = humidity self.bara_pres = bara_pres def my_func(self, *args,**kwargs): pass #do stuff here from here you could either put your instantiated objects into a list and do math on the elements thereof, or you could put them into another class object like this: class WeatherOverTime(object): def __init__(self, my_list): self.my_list = my_list def avg(self, attr): #attr is the attribute you want to take an average of, so humidity, temp, whatever. temp_list = map(lambda x: getattr(x, attr), my_list) return sum(temp_list) / count(temp_list) -------------- next part -------------- An HTML attachment was scrubbed... URL: From amgaweda at gmail.com Fri Jun 1 16:45:30 2012 From: amgaweda at gmail.com (Adam) Date: Fri, 01 Jun 2012 10:45:30 -0400 Subject: [Tutor] Benefit/Disadvantage to storing data as dictionary vs. in a class In-Reply-To: References: <4FC8BFA8.1060004@gmail.com> Message-ID: <4FC8D58A.5000606@gmail.com> At least in my own case, the 'self.rooms[z][12][85]' is a means to accessing a 'bucket' array for a given room (z), during a given hour (12), at a given temperature(85). Each bucket holds, the average, variance, standard deviation and an array of previous readings (note, readings are not temperatures, these are actually timestamps). Is there any benefit to creating a class for each bucket (with an 'update' method) vs. storing those variables as a dictionary with the 'Room' class as {'var':0, 'avg': 0, 'sd':0, 'readings':[]} and having the Room class hold the update function; not so much from good programming practices, but more attempting to number of resources the class will be using. Thank you, Adam On 6/1/2012 10:24 AM, James Reynolds wrote: > If you have functions (or in this case methods) which will be used on > a certain 'objects' data structure, a class representation of the data > is the natural fit. Otherwise, you will need to pass the dictionary > around to various functions and this will be seriously confusing. > > However, you probably should consider how to represent that data > within a class body, that is, it is extremely confusing to access data > such as self.rooms[z][12][85][AVG]. > > Instead, you should break down what the data structure into > like-things and how they are related, for example, you might consider > something like this: > > class Weather(object): > def __init__(self, temp, precipitation, humidity, bara_pres): > self. temp = temp > self. temp = precipitation > self. temp = humidity > self. temp = bara_pres > def my_func(self, *args,**kwargs): > pass #do stuff here > > However, if you need to either further define the datapoints of if you > want the datapoints to be able to "do things" then you could do > something like this: > > class Temperature(object): > def __init__(self, temp): > self. temp = temp > self.other_stuff_about_temp = stuff > > def my_temp_func(self, *args,**kwargs): > pass #do stuff here > > > class Weather(object): > def __init__(self, temp, precipitation, humidity, bara_pres): > self. temp = Temperature(temp) > self.precipitation = precipitation > self.humidity = humidity > self.bara_pres = bara_pres > def my_func(self, *args,**kwargs): > pass #do stuff here > > from here you could either put your instantiated objects into a list > and do math on the elements thereof, or you could put them into > another class object like this: > > class WeatherOverTime(object): > def __init__(self, my_list): > self.my_list = my_list > > def avg(self, attr): #attr is the attribute you want to take an > average of, so humidity, temp, whatever. > temp_list = map(lambda x: getattr(x, attr), my_list) > return sum(temp_list) / count(temp_list) -------------- next part -------------- An HTML attachment was scrubbed... URL: From ramit.prasad at jpmorgan.com Fri Jun 1 17:33:16 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 1 Jun 2012 15:33:16 +0000 Subject: [Tutor] iPython check if user running script is root user (Linux) In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF47409373A4B@SCACMX008.exchad.jpmchase.net> > > Since this is my first question to the list, is it appropriate to reply > > with a "thanks, that solved it" or is that considered unnecessary? > > Its not necessary but its not frowned on either. It is most useful where > numerous different options are being suggested since it indicates to > future googlers which option worked best for the OP... I think everyone likes to be thanked. Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 wprins at gmail.com Fri Jun 1 18:07:11 2012 From: wprins at gmail.com (Walter Prins) Date: Fri, 1 Jun 2012 17:07:11 +0100 Subject: [Tutor] Benefit/Disadvantage to storing data as dictionary vs. in a class In-Reply-To: <4FC8D58A.5000606@gmail.com> References: <4FC8BFA8.1060004@gmail.com> <4FC8D58A.5000606@gmail.com> Message-ID: Hi Adam, On 1 June 2012 15:45, Adam wrote: > Is there any benefit to creating a class for each bucket (with an 'update' > method) vs. storing those variables as a dictionary with the 'Room' class as > {'var':0, 'avg': 0, 'sd':0, 'readings':[]} and having the Room class hold > the update function; not so much from good programming practices, but more > attempting to number of resources the class will be using. I'd submit that worrying about resource usage above/beyond/to the exclusion of worrying about the easy maintenance and readability of your code at this apparently early stage may be a sign of premature optimisation. IMHO the difference in resource usage is going to be fairly negligible either way and you should lean towards prioritising keeping your code easy to read and easy to maintain at this point. How many rooms/buckets are we talking about anyway? (I might add I like Adam also find the triple indexed self.rooms[z][12][85] obscure and would prefer even just say a method with positional and/or names parameters. But I suppose I may feel differently if I worked with your API for a while. :) ) Walter From jbarrett81 at ymail.com Fri Jun 1 19:37:43 2012 From: jbarrett81 at ymail.com (Jason Barrett) Date: Fri, 1 Jun 2012 10:37:43 -0700 (PDT) Subject: [Tutor] (no subject) Message-ID: <1338572263.29394.YahooMailNeo@web112318.mail.gq1.yahoo.com> In python, why does 17/-10= -2? Shouldn't it be -1? Godsend221 -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Fri Jun 1 19:51:50 2012 From: __peter__ at web.de (Peter Otten) Date: Fri, 01 Jun 2012 19:51:50 +0200 Subject: [Tutor] (no subject) References: <1338572263.29394.YahooMailNeo@web112318.mail.gq1.yahoo.com> Message-ID: Jason Barrett wrote: > In python, why does 17/-10= -2? Shouldn't it be -1? http://docs.python.org/faq/programming.html#why-does-22-10-return-3 From npicciano19 at gmail.com Sat Jun 2 01:01:00 2012 From: npicciano19 at gmail.com (Nicholas Picciano) Date: Fri, 1 Jun 2012 19:01:00 -0400 Subject: [Tutor] Connecting to MySQLdb Message-ID: Hello, I have downloaded MySQLdb 1.2.3 from: http://pypi.python.org/pypi/MySQL-python Also, I'm using Python 2.7, but I can't even get past here: >>> import MySQLdb Traceback (most recent call last): File "", line 1, in ImportError: No module named MySQLdb Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From corey at octayn.net Sat Jun 2 01:19:30 2012 From: corey at octayn.net (Corey Richardson) Date: Fri, 1 Jun 2012 19:19:30 -0400 Subject: [Tutor] Connecting to MySQLdb In-Reply-To: References: Message-ID: <20120601191930.736e4999@Ulysses.myhome.westell.com> On Fri, 1 Jun 2012 19:01:00 -0400 Nicholas Picciano wrote: > Hello, > > I have downloaded MySQLdb 1.2.3 from: > > http://pypi.python.org/pypi/MySQL-python > > Also, I'm using Python 2.7, but I can't even get past here: > > >>> import MySQLdb > Traceback (most recent call last): > File "", line 1, in > ImportError: No module named MySQLdb > > Thanks Did you install it? Assuming UNIX(-like): I highly, *highly* recommend you use a virtualenv [1] and, if you want a fancy UI, virtualenvwrapper [2]. From there you can install all the packages you want with pip, without having to worry about mucking up the system, versions of things, etc. Assuming Windows: Good luck! -- Corey Richardson From eire1130 at gmail.com Sat Jun 2 01:46:59 2012 From: eire1130 at gmail.com (James Reynolds) Date: Fri, 1 Jun 2012 19:46:59 -0400 Subject: [Tutor] Connecting to MySQLdb In-Reply-To: <20120601191930.736e4999@Ulysses.myhome.westell.com> References: <20120601191930.736e4999@Ulysses.myhome.westell.com> Message-ID: Virtualenv works just fine in windows On Jun 1, 2012 7:20 PM, "Corey Richardson" wrote: > On Fri, 1 Jun 2012 19:01:00 -0400 > Nicholas Picciano wrote: > > > Hello, > > > > I have downloaded MySQLdb 1.2.3 from: > > > > http://pypi.python.org/pypi/MySQL-python > > > > Also, I'm using Python 2.7, but I can't even get past here: > > > > >>> import MySQLdb > > Traceback (most recent call last): > > File "", line 1, in > > ImportError: No module named MySQLdb > > > > Thanks > > Did you install it? > > Assuming UNIX(-like): > > I highly, *highly* recommend you use a virtualenv [1] and, if you want > a fancy UI, virtualenvwrapper [2]. From there you can install all the > packages you want with pip, without having to worry about mucking up > the system, versions of things, etc. > > Assuming Windows: > > Good luck! > > -- > Corey Richardson > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sat Jun 2 04:12:59 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 02 Jun 2012 12:12:59 +1000 Subject: [Tutor] Benefit/Disadvantage to storing data as dictionary vs. in a class In-Reply-To: <4FC8BFA8.1060004@gmail.com> References: <4FC8BFA8.1060004@gmail.com> Message-ID: <4FC976AB.3080902@pearwood.info> Adam wrote: > I'm working on a class that handles multiple rooms that generate a large > amount of data. Currently my class model looks something like this (more > apologies for any indentation errors): In my experience, what people consider "a large amount of data" and what the computer considers large is often very, very different. You talk about "multiple rooms" with 24 records per day (one per hour). If each room has 100 readings per hour, and 100 outside temperatures each with 100 readings, I would expect you to need under 50 MB per room. That's not that much these days. > My question is in regards to the storing all this data in a dictionary, > which makes it easy to reference self.rooms[z][12][85][AVG]; however is > this better/worse or the same as creating a 'Room' class to store the > data? Since a 'Room' only contains a large amount of data, but doesn't > contain any functions, which form of storing the data is considered > 'better'? In modern object-oriented languages like Python, the fact that a class doesn't contain any methods (functions) is not a problem. Objects can be used as the equivalent of Pascal records or C structs too. Besides, Python dicts and lists are already objects. The amount of overhead in putting them together into programmer-friendly classes with named attributes is trivial. Assuming my figure of 50 MB above is accurate, I'd expect the equivalent figure for a class-based solution would be 50.2 MB. Big deal. Write your code to make it is easy and simple to read and write as possible. Program as if the next guy who maintains the program will be a violent psychopath who knows where you live. Your aim is to have code that is so simple that it is obviously correct, rather than so complicated that there are no obvious bugs. Expressions like: self.rooms[z][12][85][AVG] strike me as tending towards the later ("no obvious bugs") than the first. -- Steven From __peter__ at web.de Sat Jun 2 10:52:14 2012 From: __peter__ at web.de (Peter Otten) Date: Sat, 02 Jun 2012 10:52:14 +0200 Subject: [Tutor] Unexpected result of division by negative integer, was: (no subject) References: <1338572263.29394.YahooMailNeo@web112318.mail.gq1.yahoo.com> Message-ID: Jason Barrett wrote: [Jason, please use "reply all" instead of sending a private mail. Your answer will then appear on the mailing list and other readers get a chance to offer an alternative explanation.] >> In python, why does 17/-10= -2? Shouldn't it be -1? > >> http://docs.python.org/faq/programming.html#why-does-22-10-return-3 > I just started programming so all this is new to me. I don't really > understand the explanation. I'm a novice at programming. From wolfrage8765 at gmail.com Sat Jun 2 13:29:04 2012 From: wolfrage8765 at gmail.com (Jordan) Date: Sat, 02 Jun 2012 13:29:04 +0200 Subject: [Tutor] Octal confusion, please explain why. Python 3.2 Message-ID: <4FC9F900.7060605@gmail.com> Hello, first off I am using Python 3.2 on Linux Mint 12 64-bit. I am confused as to why I can not successfully compare a variable that was created as an octal to a variable that is converted to an octal in a if statement yet print yields that they are the same octal value. I think it is because they are transported around within python as a integer value, is this correct and if so why? Code example 1: This code works because both are explicitly converted to Octals. PATH = '/home/wolfrage/Documents' DIR_PERMISSIONS = 0o40740 for dirname, dirnames, filenames in os.walk(PATH): for subdirname in dirnames: #list each subdirectory in this directory. full_path = os.path.join(dirname, subdirname) print(full_path) stat_info = os.stat(full_path) uid = stat_info.st_uid gid = stat_info.st_gid #TODO: Group and User ID Check #TODO: Directories need the right Masks, and then read, write, and # execute permissions for User(Owner), Group and Others #TODO: UMask is seperate from permissions, so handle it seperately. if oct(stat_info.st_mode) != oct(DIR_PERMISSIONS): #TODO: Fix User Read Permissions print(str(oct(stat_info.st_mode)) + ' Vs ' + str(oct(DIR_PERMISSIONS))) os.chmod(full_path, DIR_PERMISSIONS) print(subdirname + ' has bad user permissions.') Code Example 2: This code does not work because we do not use an explicit conversion. PATH = '/home/wolfrage/Documents' DIR_PERMISSIONS = 0o40740 for dirname, dirnames, filenames in os.walk(PATH): for subdirname in dirnames: #list each subdirectory in this directory. full_path = os.path.join(dirname, subdirname) print(full_path) stat_info = os.stat(full_path) uid = stat_info.st_uid gid = stat_info.st_gid #TODO: Group and User ID Check #TODO: Directories need the right Masks, and then read, write, and # execute permissions for User(Owner), Group and Others #TODO: UMask is seperate from permissions, so handle it seperately. if oct(stat_info.st_mode) != DIR_PERMISSIONS: #TODO: Fix User Read Permissions print(str(oct(stat_info.st_mode)) + ' Vs ' + str(DIR_PERMISSIONS)) # The Above print statement shows that DIR_PERMISSIONS is printed as an # Integer. But Why does Python convert a explicitly created Octal to an # Integer? os.chmod(full_path, DIR_PERMISSIONS) print(subdirname + ' has bad user permissions.') From wolfrage8765 at gmail.com Sat Jun 2 13:39:40 2012 From: wolfrage8765 at gmail.com (Jordan) Date: Sat, 02 Jun 2012 13:39:40 +0200 Subject: [Tutor] Joining all strings in stringList into one string In-Reply-To: References: Message-ID: <4FC9FB7C.2070605@gmail.com> On 05/30/2012 06:21 PM, Akeria Timothy wrote: > Hello all, > > I am working on learning Python(on my own) and ran into an exercise > that I figured out but I wanted to know if there was a different way > to write the code? I know he wanted a different answer for the body > because we haven't gotten to the ' '.join() command yet. > > This is what I have: > > def joinStrings(stringList): > string = [] > for string in stringList: # Here you never used the string variable so why have a for statement? > print ''.join(stringList) #Another version might look like this: def join_strings2(string_list): final_string = '' for string in string_list: final_string += string print(final_string) return final_string # Tested in Python 3.2 join_strings2(['1', '2', '3', '4', '5']) > > > def main(): > print joinStrings(['very', 'hot', 'day']) > print joinStrings(['this', 'is', 'it']) > print joinStrings(['1', '2', '3', '4', '5']) > > main() > > > thanks all > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Jun 2 13:40:09 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 02 Jun 2012 12:40:09 +0100 Subject: [Tutor] Unexpected result of division by negative integer, was: (no subject) In-Reply-To: References: <1338572263.29394.YahooMailNeo@web112318.mail.gq1.yahoo.com> Message-ID: On 02/06/12 09:52, Peter Otten wrote: >> I just started programming so all this is new to me. I don't really >> understand the explanation. I'm a novice at programming. OK, Here is an attempt to explain the explanation! Original question: Why does -17 / 10 => -2 instead of -1 Python FAQ response: > It?s primarily driven by the desire that i % j have the same > sign as j. The % operator returns the "remainder" in integer division. It turns out that in programming we can often use the remainder to perform programming "tricks", especially when dealing with cyclic data - such as the days of the week. For example if you want to find out which day of the week it is 53 days from now it is 53 % 7 plus whatever todays number is... Now for those tricks to work we want to have the % operator work as described in the FAQ above > If you want that, and also want: > > i == (i // j) * j + (i % j) > > then integer division has to return the floor. This is just saying that the integer division(//) result times the original divisor plus the remainder should equal the starting number. Thus: 17/10 => 1,7 So (17 // 10) => 1 and (17 % 10) => 7 So 1 x 10 + 7 => 17. And to achieve those two aims(remainder with the same sign as the divisor and the sum returning the original value) we must define integer division to return the "floor" of the division result. The floor of a real number is the integer value *lower* than its real value. Thus: floor(5.5) -> 5 floor(-5.5) -> -6 17/10 -> 1.7 floor(1.7) -> 1 -17/10 -> -1.7 floor(-1.7) -> -2 So X // Y == floor(X / Y) HTH. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Sat Jun 2 13:51:59 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 02 Jun 2012 21:51:59 +1000 Subject: [Tutor] Octal confusion, please explain why. Python 3.2 In-Reply-To: <4FC9F900.7060605@gmail.com> References: <4FC9F900.7060605@gmail.com> Message-ID: <4FC9FE5F.4090904@pearwood.info> Jordan wrote: > Hello, first off I am using Python 3.2 on Linux Mint 12 64-bit. > I am confused as to why I can not successfully compare a variable that > was created as an octal to a variable that is converted to an octal in a > if statement yet print yields that they are the same octal value. Because the oct() function returns a string, not a number. py> oct(63) '0o77' In Python, strings are never equal to numbers: py> 42 == '42' False Octal literals are just regular integers that you type differently: py> 0o77 63 "Octals" aren't a different type or object; the difference between the numbers 0o77 and 63 is cosmetic only. 0o77 is just the base 8 version of the base 10 number 63 or the binary number 0b111111. Because octal notation is only used for input, there's no need to covert numbers to octal strings to compare them: py> 0o77 == 63 # no different to "63 == 63" True The same applies for hex and bin syntax too: py> 0x1ff == 511 True -- Steven From wolfrage8765 at gmail.com Sat Jun 2 13:58:29 2012 From: wolfrage8765 at gmail.com (Jordan) Date: Sat, 02 Jun 2012 13:58:29 +0200 Subject: [Tutor] Octal confusion, please explain why. Python 3.2 In-Reply-To: <4FC9FE5F.4090904@pearwood.info> References: <4FC9F900.7060605@gmail.com> <4FC9FE5F.4090904@pearwood.info> Message-ID: <4FC9FFE5.4020707@gmail.com> Thank you for the detailed answer, now I understand and I understand that each number format is an integer just with a different base and cosmetic appearance. On 06/02/2012 01:51 PM, Steven D'Aprano wrote: > Jordan wrote: >> Hello, first off I am using Python 3.2 on Linux Mint 12 64-bit. >> I am confused as to why I can not successfully compare a variable that >> was created as an octal to a variable that is converted to an octal in a >> if statement yet print yields that they are the same octal value. > > Because the oct() function returns a string, not a number. Ahh, I see. That makes sense next time I will read the documentation on the functions that I am using. > py> oct(63) > '0o77' > > In Python, strings are never equal to numbers: > > py> 42 == '42' > False > > > Octal literals are just regular integers that you type differently: > > py> 0o77 > 63 > > "Octals" aren't a different type or object; the difference between the > numbers 0o77 and 63 is cosmetic only. 0o77 is just the base 8 version > of the base 10 number 63 or the binary number 0b111111. > > > Because octal notation is only used for input, there's no need to > covert numbers to octal strings to compare them: > > py> 0o77 == 63 # no different to "63 == 63" > True > > > The same applies for hex and bin syntax too: > > py> 0x1ff == 511 > True > > > From alan.gauld at btinternet.com Sat Jun 2 14:59:54 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 02 Jun 2012 13:59:54 +0100 Subject: [Tutor] Octal confusion, please explain why. Python 3.2 In-Reply-To: <4FC9F900.7060605@gmail.com> References: <4FC9F900.7060605@gmail.com> Message-ID: On 02/06/12 12:29, Jordan wrote: > think it is because they are transported around within python as a > integer value, is this correct and if so why? Steven has already answered your issue but just to emphasise this point. Python stores all data as binary values in memory. Octal, decimal, hex are all representations of that raw data. Python interprets some binary data as strings, some as integers, some as booleans etc and then tries to display these objects in a suitable representation. (We can use the struct module to define the way python interprets binary data input.) For integers the default is base 10 but we can exercise some control over the format by using modifiers such as oct() hex() etc. But these functions only change the representation they do not change the underlying data in any way. HTH, Alan G. From steve at pearwood.info Sat Jun 2 15:11:14 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 02 Jun 2012 23:11:14 +1000 Subject: [Tutor] Division by negative numbers [was: no subject] In-Reply-To: <1338572263.29394.YahooMailNeo@web112318.mail.gq1.yahoo.com> References: <1338572263.29394.YahooMailNeo@web112318.mail.gq1.yahoo.com> Message-ID: <4FCA10F2.1030609@pearwood.info> Jason Barrett wrote: > In python, why does 17/-10= -2? Shouldn't it be -1? Integer division with negative numbers can be done in two different ways. (1) 17/-10 gives -1, with remainder 7. Proof: -1 times -10 is 10, plus 7 gives 17. (2) 17/-10 gives -2, with remainder -3. Proof: -2 times -10 is 20, plus -3 gives 17. [Aside: technically, integer division with positive numbers also has the same choice: 17 = 1*10+7 or 2*10-3.] Every language has to choose whether to support the first or the second version. Python happens to choose the second. Other languages might make the same choice, or the other. For example, C leaves that decision up to the compiler. Different C compiles may give different results, which is not very useful in my opinion. Ruby behaves like Python: steve at orac:/home/steve$ irb irb(main):001:0> 17/10 => 1 irb(main):002:0> 17/-10 => -2 Other languages may be different. P.S. in future, please try to include a meaningful subject line to your posts. -- Steven From steve at pearwood.info Sat Jun 2 16:29:07 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 03 Jun 2012 00:29:07 +1000 Subject: [Tutor] Joining all strings in stringList into one string In-Reply-To: <4FC9FB7C.2070605@gmail.com> References: <4FC9FB7C.2070605@gmail.com> Message-ID: <4FCA2333.3080302@pearwood.info> Jordan wrote: > #Another version might look like this: > > def join_strings2(string_list): > final_string = '' > for string in string_list: > final_string += string > print(final_string) > return final_string Please don't do that. This risks becoming slow. REALLY slow. Painfully slow. Like, 10 minutes versus 3 seconds slow. Seriously. The reason for this is quite technical, and the reason why you might not notice is even more complicated, but the short version is this: Never build up a long string by repeated concatenation of short strings. Always build up a list of substrings first, then use the join method to assemble them into one long string. Why repeated concatenation is slow: Suppose you want to concatenation two strings, "hello" and "world", to make a new string "helloworld". What happens? Firstly, Python has to count the number of characters needed, which fortunately is fast in Python, so we can ignore it. In this case, we need 5+5 = 10 characters. Secondly, Python sets aside enough memory for those 10 characters, plus a little bit of overhead: ---------- Then it copies the characters from "hello" into the new area: hello----- followed by the characters of "world": helloworld and now it is done. Simple, right? Concatenating two strings is pretty fast. You can't get much faster. Ah, but what happens if you do it *repeatedly*? Suppose we have SIX strings we want to concatenate, in a loop: words = ['hello', 'world', 'foo', 'bar', 'spam', 'ham'] result = '' for word in words: result = result + word How much work does Python have to do? Step one: add '' + 'hello', giving result 'hello' Python needs to copy 0+5 = 5 characters. Step two: add 'hello' + 'world', giving result 'helloworld' Python needs to copy 5+5 = 10 characters, as shown above. Step three: add 'helloworld' + 'foo', giving 'helloworldfoo' Python needs to copy 10+3 = 13 characters. Step four: add 'helloworldfoo' + 'bar', giving 'helloworldfoobar' Python needs to copy 13+3 = 16 characters. Step five: add 'helloworldfoobar' + 'spam', giving 'helloworldfoobarspam' Python needs to copy 16+4 = 20 characters. Step six: add 'helloworldfoobarspam' + 'ham', giving 'helloworldfoobarspamham' Python needs to copy 20+3 = 23 characters. So in total, Python has to copy 5+10+13+16+20+23 = 87 characters, just to build up a 23 character string. And as the number of loops increases, the amount of extra work needed just keeps expanding. Even though a single string concatenation is fast, repeated concatenation is painfully SLOW. In comparison, ''.join(words) one copies each substring once: it counts out that it needs 23 characters, allocates space for 23 characters, then copies each substring into the right place instead of making a whole lot of temporary strings and redundant copying. So, join() is much faster than repeated concatenation. But you may never have noticed. Why not? Well, for starters, for small enough pieces of data, everything is fast. The difference between copying 87 characters (the slow way) and 23 characters (the fast way) is trivial. But more importantly, some years ago (Python 2.4, about 8 years ago?) the Python developers found a really neat trick that they can do to optimize string concatenation so it doesn't need to repeatedly copy characters over and over and over again. I won't go into details, but the thing is, this trick works well enough that repeated concatenation is about as fast as the join method MOST of the time. Except when it fails. Because it is a trick, it doesn't always work. And when it does fail, your repeated string concatenation code will suddenly drop from running in 0.1 milliseconds to a full second or two; or worse, from 20 seconds to over an hour. (Potentially; the actual slow-down depends on the speed of your computer, your operating system, how much memory you have, etc.) Because this is a cunning trick, it doesn't always work, and when it doesn't work, and you have slow code and no hint as to why. What can cause it to fail? - Old versions of Python, before 2.4, will be slow. - Other implementations of Python, such as Jython and IronPython, will not have the trick, and so will be slow. - The trick is highly-dependent on internal details of the memory management of Python and the way it interacts with the operating system. So what's fast under Linux may be slow under Windows, or the other way around. - The trick is highly-dependent on specific circumstances to do with the substrings being added. Without going into details, if those circumstances are violated, you will have slow code. - The trick only works when you are adding strings to the end of the new string, not if you are building it up from the beginning. So even though your function works, you can't rely on it being fast. -- Steven From fomcl at yahoo.com Sat Jun 2 21:00:14 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sat, 2 Jun 2012 12:00:14 -0700 (PDT) Subject: [Tutor] How to identify clusters of similar files Message-ID: <1338663614.57385.YahooMailNeo@web110709.mail.gq1.yahoo.com> Hi, I want to use difflib to compare a lot (tens of thousands) of text files. I know that many files are quite similar as they are subsequent versions of the same document (a primitive kind of version control). What would be a good approach to cluster the files based on their likeness? I want to be able to say something like: the number of files could be reduced by a factor of ten when the number of (near-)duplicates is taken into account. So let's say I have ten versions of a txt file: 'file0.txt', 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', 'file5.txt', 'file6.txt', 'file7.txt', 'file8.txt', 'file9.txt'. How could I to some degree of certainty say they are related (I can't rely on the file names I'm affraid). file0 may be very similar to file1, but no longer to file10. But their likeness is "chained". The situation is easier with perfectly identical files. The crude code below illustrates what I'd like to do, but it's too simplistic. I'd appreciate some thoughts or references to theoretical approaches to this kind of stuff. import difflib, glob, os path = "/home/aj/Destkop/someDir" extension = ".txt" cut_off = 0.95 allTheFiles = sorted(glob.glob(os.path.join(path, "*" + extension))) for f_a in allTheFiles: ? for f_b in allTheFiles: ??? file_a = open(f_a).readlines() ??? file_b = open(f_b).readlines() ??? if f_a != f_b: ?????? likeness = difflib.SequenceMatcher(lambda x: x == " ", file_a, file_b).ratio() ?????? if likeness >= cut_off: ???????? try: ?????????? clusters[f_a].append(f_b) ???????? except KeyError: ?????????? clusters[f_a] = [f_b] ? Thank you in advance! Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? -------------- next part -------------- An HTML attachment was scrubbed... URL: From wolfrage8765 at gmail.com Sat Jun 2 23:20:48 2012 From: wolfrage8765 at gmail.com (Jordan) Date: Sat, 02 Jun 2012 23:20:48 +0200 Subject: [Tutor] Joining all strings in stringList into one string In-Reply-To: <4FCA2333.3080302@pearwood.info> References: <4FC9FB7C.2070605@gmail.com> <4FCA2333.3080302@pearwood.info> Message-ID: <4FCA83B0.4030902@gmail.com> Thanks for the excellent feedback and very informative. I guess I just did not consider the memory side of things and did not think about just how much extra addition was having to occur. Additionally I also found this supporting link: http://wiki.python.org/moin/PythonSpeed/PerformanceTips#String_Concatenation On 06/02/2012 04:29 PM, Steven D'Aprano wrote: > Jordan wrote: > >> #Another version might look like this: >> >> def join_strings2(string_list): >> final_string = '' >> for string in string_list: >> final_string += string >> print(final_string) >> return final_string > > Please don't do that. This risks becoming slow. REALLY slow. Painfully > slow. Like, 10 minutes versus 3 seconds slow. Seriously. > > The reason for this is quite technical, and the reason why you might > not notice is even more complicated, but the short version is this: > > Never build up a long string by repeated concatenation of short strings. > Always build up a list of substrings first, then use the join method to > assemble them into one long string. > > > Why repeated concatenation is slow: > > Suppose you want to concatenation two strings, "hello" and "world", to > make a new string "helloworld". What happens? > > Firstly, Python has to count the number of characters needed, which > fortunately is fast in Python, so we can ignore it. In this case, we > need 5+5 = 10 characters. > > Secondly, Python sets aside enough memory for those 10 characters, > plus a little bit of overhead: ---------- > > Then it copies the characters from "hello" into the new area: hello----- > > followed by the characters of "world": helloworld > > and now it is done. Simple, right? Concatenating two strings is pretty > fast. You can't get much faster. > > Ah, but what happens if you do it *repeatedly*? Suppose we have SIX > strings we want to concatenate, in a loop: > > words = ['hello', 'world', 'foo', 'bar', 'spam', 'ham'] > result = '' > for word in words: > result = result + word > > How much work does Python have to do? > > Step one: add '' + 'hello', giving result 'hello' > Python needs to copy 0+5 = 5 characters. > > Step two: add 'hello' + 'world', giving result 'helloworld' > Python needs to copy 5+5 = 10 characters, as shown above. > > Step three: add 'helloworld' + 'foo', giving 'helloworldfoo' > Python needs to copy 10+3 = 13 characters. > > Step four: add 'helloworldfoo' + 'bar', giving 'helloworldfoobar' > Python needs to copy 13+3 = 16 characters. > > Step five: add 'helloworldfoobar' + 'spam', giving 'helloworldfoobarspam' > Python needs to copy 16+4 = 20 characters. > > Step six: add 'helloworldfoobarspam' + 'ham', giving > 'helloworldfoobarspamham' > Python needs to copy 20+3 = 23 characters. > > So in total, Python has to copy 5+10+13+16+20+23 = 87 characters, just > to build up a 23 character string. And as the number of loops > increases, the amount of extra work needed just keeps expanding. Even > though a single string concatenation is fast, repeated concatenation > is painfully SLOW. > > In comparison, ''.join(words) one copies each substring once: it > counts out that it needs 23 characters, allocates space for 23 > characters, then copies each substring into the right place instead of > making a whole lot of temporary strings and redundant copying. > > So, join() is much faster than repeated concatenation. But you may > never have noticed. Why not? > > Well, for starters, for small enough pieces of data, everything is > fast. The difference between copying 87 characters (the slow way) and > 23 characters (the fast way) is trivial. > > But more importantly, some years ago (Python 2.4, about 8 years ago?) > the Python developers found a really neat trick that they can do to > optimize string concatenation so it doesn't need to repeatedly copy > characters over and over and over again. I won't go into details, but > the thing is, this trick works well enough that repeated concatenation > is about as fast as the join method MOST of the time. > > Except when it fails. Because it is a trick, it doesn't always work. > And when it does fail, your repeated string concatenation code will > suddenly drop from running in 0.1 milliseconds to a full second or > two; or worse, from 20 seconds to over an hour. (Potentially; the > actual slow-down depends on the speed of your computer, your operating > system, how much memory you have, etc.) > > Because this is a cunning trick, it doesn't always work, and when it > doesn't work, and you have slow code and no hint as to why. > > What can cause it to fail? > > - Old versions of Python, before 2.4, will be slow. > > - Other implementations of Python, such as Jython and IronPython, will > not have the trick, and so will be slow. > > - The trick is highly-dependent on internal details of the memory > management of Python and the way it interacts with the operating > system. So what's fast under Linux may be slow under Windows, or the > other way around. > > - The trick is highly-dependent on specific circumstances to do with > the substrings being added. Without going into details, if those > circumstances are violated, you will have slow code. > > - The trick only works when you are adding strings to the end of the > new string, not if you are building it up from the beginning. > > > So even though your function works, you can't rely on it being fast. > > > > From steve at pearwood.info Sun Jun 3 04:00:32 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 03 Jun 2012 12:00:32 +1000 Subject: [Tutor] How to identify clusters of similar files In-Reply-To: <1338663614.57385.YahooMailNeo@web110709.mail.gq1.yahoo.com> References: <1338663614.57385.YahooMailNeo@web110709.mail.gq1.yahoo.com> Message-ID: <4FCAC540.2020402@pearwood.info> Albert-Jan Roskam wrote: > Hi, > > I want to use difflib to compare a lot (tens of thousands) of text files. I > know that many files are quite similar as they are subsequent versions of > the same document (a primitive kind of version control). What would be a > good approach to cluster the files based on their likeness? You have already identified the basic tool: difflib. But your question is not really about Python, it is more about the algorithm used for clustering data according to goodness of fit. That's a hard problem, and you should consider asking it on the main Python mailing list or newsgroup too. Some search terms to get you started: biopython nltk (the Natural Language Tool Kit) unrooted phylogram Good luck! -- Steven From fomcl at yahoo.com Sun Jun 3 12:37:06 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sun, 3 Jun 2012 03:37:06 -0700 (PDT) Subject: [Tutor] How to identify clusters of similar files In-Reply-To: <4FCAC540.2020402@pearwood.info> References: <1338663614.57385.YahooMailNeo@web110709.mail.gq1.yahoo.com> <4FCAC540.2020402@pearwood.info> Message-ID: <1338719826.78939.YahooMailNeo@web110705.mail.gq1.yahoo.com> From: Steven D'Aprano >To: Python Mailing List >Sent: Sunday, June 3, 2012 4:00 AM >Subject: Re: [Tutor] How to identify clusters of similar files > >Albert-Jan Roskam wrote: >> Hi, >> >> I want to use difflib to compare a lot (tens of thousands) of text files. I >> know that many files are quite similar as they are subsequent versions of >> the same document (a primitive kind of version control). What would be a >> good approach to cluster the files based on their likeness? > >You have already identified the basic tool: difflib. But your question is not really about Python, it is more about the algorithm used for clustering data according to goodness of fit. That's a hard problem, and you should consider asking it on the main Python mailing list or newsgroup too. > >Some search terms to get you started: > >biopython >nltk? (the Natural Language Tool Kit) >unrooted phylogram > > >Good luck! > > >-- Steven > >Hi Steven, > >Thanks! Biopython looks very interesting. While browsing I was thinking this problem could also be considered as a probabilistic/fuzzy linkage problem (Fellegi & Sunter). Instead of linking records, I am trying to 'link'? files. > > >Best wishes, >Albert-Jan > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tehn.yit.chin at gmail.com Sun Jun 3 23:43:30 2012 From: tehn.yit.chin at gmail.com (Tehn Yit Chin) Date: Sun, 3 Jun 2012 23:43:30 +0200 Subject: [Tutor] use the data None Message-ID: Hi all, I am trying to understand when it is appropriate to use None as in the following example if abc != None: print "abc is not None" 1) Can I use it to determine if the variable abc exists? 2) Can I use it to determine if the variable abc does not contain anything? Many thanks -- Tehn Yit Chin -------------- next part -------------- An HTML attachment was scrubbed... URL: From corey at octayn.net Mon Jun 4 00:08:12 2012 From: corey at octayn.net (Corey Richardson) Date: Sun, 3 Jun 2012 18:08:12 -0400 Subject: [Tutor] use the data None In-Reply-To: References: Message-ID: <20120603180812.0d1fbc15@Ulysses.myhome.westell.com> On Sun, 3 Jun 2012 23:43:30 +0200 Tehn Yit Chin wrote: > Hi all, > > I am trying to understand when it is appropriate to use None as in the > following example > > if abc != None: > print "abc is not None" > The proper idiom, for a variety of reasons, is: if abc is not None: # do stuff > 1) Can I use it to determine if the variable abc exists? > No. It will raise a NameError, which you would have to catch. To do what you want, something along the lines of: try: abc # it exists except NameError: # it doesn't However, this is pretty dirty. Why are you using variables that potentially don't exist? > 2) Can I use it to determine if the variable abc does not contain > anything? > For a generic foo, if foo not in abc: # it's not there, Jim Does this answer your questions? (I've been away from Python for a bit, one of the other tutors might correct me with better practice things, I'm rusty!) -- Corey Richardson From steve at pearwood.info Mon Jun 4 02:19:17 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 04 Jun 2012 10:19:17 +1000 Subject: [Tutor] use the data None In-Reply-To: <20120603180812.0d1fbc15@Ulysses.myhome.westell.com> References: <20120603180812.0d1fbc15@Ulysses.myhome.westell.com> Message-ID: <4FCBFF05.3070103@pearwood.info> Corey Richardson wrote: [snip] > (I've been away from Python for a bit, one of the other tutors might > correct me with better practice things, I'm rusty!) Your reply is good. -- Steven From alan.gauld at btinternet.com Mon Jun 4 02:39:10 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 04 Jun 2012 01:39:10 +0100 Subject: [Tutor] use the data None In-Reply-To: References: Message-ID: On 03/06/12 22:43, Tehn Yit Chin wrote: > I am trying to understand when it is appropriate to use None as in the > following example > > if abc != None: > print "abc is not None" In general I only use that idf its a defaulted parameter to a function: def foo(x=None): if x is None: do the default thing with x else: use the x that was passed in. > 1) Can I use it to determine if the variable abc exists? There are better ways of doing that, usually by detecting a NameErropr. > 2) Can I use it to determine if the variable abc does not contain anything? That's not the same as None a = '' # empty string b = 0 # zero number c = [] # empty list usually you detect emptiness by a simply truth test: for var in (a,b,c): if not var: print var.__name__, ' is empty or false' else: use the value That's because, by convention, Python types assign falseness to be the same as emptiness, certainly for the built-in or standard types. There are occasional uses for None but mostly it's there to make the language consistent! eg. a return type for functions that don't explicitly return anything... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Mon Jun 4 03:09:25 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 04 Jun 2012 02:09:25 +0100 Subject: [Tutor] use the data None In-Reply-To: References: Message-ID: On 04/06/12 01:39, Alan Gauld wrote: > for var in (a,b,c): > if not var: > print var.__name__, ' is empty or false' Oops, that won't work. __name__ is not an attribute of object, which I thought it was... But hopefully the intention was clear. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From wh1t3ha7 at tormail.org Mon Jun 4 03:41:00 2012 From: wh1t3ha7 at tormail.org (Wh1t3ha7) Date: Sun, 03 Jun 2012 18:41:00 -0700 Subject: [Tutor] [Parser/pgen] Error 1 Message-ID: <1SbMKz-000FtH-4h@internal.tormail.org> I was trying to install Python on Ubuntu 12.04 and Im getting this error message when entering the make -j command: Parser/grammar1.o: file not recognized: File truncated collect2: ld returned 1 exit status make: *** [Parser/pgen] Error 1 make: *** Waiting for unfinished jobs.... I was following instructions on this link: http://eli.thegreenplace.net/2011/10/10/installing-python-2-7-on-ubuntu/ Thank you all so much I cannot wait to learn from you guys :) From joel.goldstick at gmail.com Mon Jun 4 07:17:59 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 4 Jun 2012 01:17:59 -0400 Subject: [Tutor] [Parser/pgen] Error 1 In-Reply-To: <1SbMKz-000FtH-4h@internal.tormail.org> References: <1SbMKz-000FtH-4h@internal.tormail.org> Message-ID: On Sun, Jun 3, 2012 at 9:41 PM, Wh1t3ha7 wrote: > I was trying to install Python on Ubuntu 12.04 and Im getting this error > message when entering the make -j command: > > Parser/grammar1.o: file not recognized: File truncated > collect2: ld returned 1 exit status > make: *** [Parser/pgen] Error 1 > make: *** Waiting for unfinished jobs.... > > > I was following instructions on this link: > > http://eli.thegreenplace.net/2011/10/10/installing-python-2-7-on-ubuntu/ > > > Thank you all so much I cannot wait to learn from you guys :) > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Not sure about your error. However, python is pre installed in ubuntu. go to a shell and type python. See if you get the python shell -- Joel Goldstick From tehn.yit.chin at gmail.com Mon Jun 4 12:14:01 2012 From: tehn.yit.chin at gmail.com (Tehn Yit Chin) Date: Mon, 4 Jun 2012 12:14:01 +0200 Subject: [Tutor] use the data None In-Reply-To: References: Message-ID: Thanks for the quick answers. The potential for the variable not to exists is when I am using the optparser module, and I want to check if a particular parameter was passed in or not. If the parameter was not passed in, then the variable would not exists. Eg If I call a python script is expecting a parameter "param1" but I failed to passed it via the command line, I would do the following check (options, args)=parser.parse_args() if options.param1 != None: param1 = int(options.param1 ) else: param1 = 30 By the way, please excuse the way I am coding, I come from a C background. I think I shall try the catch method. Thanks! On Mon, Jun 4, 2012 at 3:09 AM, Alan Gauld wrote: > On 04/06/12 01:39, Alan Gauld wrote: > > for var in (a,b,c): >> if not var: >> print var.__name__, ' is empty or false' >> > > Oops, that won't work. __name__ is not an attribute > of object, which I thought it was... > > But hopefully the intention was clear. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > ______________________________**_________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > -- Tehn Yit Chin -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Mon Jun 4 13:01:07 2012 From: sander.sweers at gmail.com (Sander Sweers) Date: Mon, 4 Jun 2012 13:01:07 +0200 Subject: [Tutor] use the data None In-Reply-To: References: Message-ID: On 4 June 2012 12:14, Tehn Yit Chin wrote: > The potential for the variable not to exists is when I am using the > optparser module, and I want to check if a particular parameter was passed > in or not. If the parameter was not passed in, then the variable would not > exists. Eg Optparse will always create the variable, it may contain None. > If I call a python script is expecting a parameter "param1" but I failed to > passed it via the command line, I would do the following check > > (options, args)=parser.parse_args() > if options.param1 != None: > ???????? param1 = int(options.param1 ) > else: > ???????? param1 = 30 > > By the way, please excuse the way I am coding, I come from a C background. The important part of you code is missing. You should show how you create the parser (with the options etc). The optparse module can provide a default value, did you know this? See the online documentation on [1]. It shows an example of this right at the beginning. Greets Sander [1] http://docs.python.org/library/optparse.html From Mustapha.Bilal at imag.fr Mon Jun 4 15:58:24 2012 From: Mustapha.Bilal at imag.fr (BILAL Mustapha) Date: Mon, 04 Jun 2012 15:58:24 +0200 Subject: [Tutor] packets_USB_python Message-ID: <4FCCBF00.3020204@imag.fr> Hello, I am beginner with python and I recently started to use it in order to achieve a small part of something I am working on. I have 2 processes (process 1 and process 2). I am wondering if you can provide me with some example or sample code of how I can do the following: - Process 2 receives icmpv6 (or any packet) from process1 and retransmit it to a serial link (USB) - Process 2 receives icmpv6 (or any packet) from the serial link (USB) and restransmit it to process 1. I had tried to write some code to, at least, ping but apparently I am missing something. Many thanks From alan.gauld at btinternet.com Mon Jun 4 16:10:52 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 04 Jun 2012 15:10:52 +0100 Subject: [Tutor] packets_USB_python In-Reply-To: <4FCCBF00.3020204@imag.fr> References: <4FCCBF00.3020204@imag.fr> Message-ID: On 04/06/12 14:58, BILAL Mustapha wrote: > - Process 2 receives icmpv6 (or any packet) from process1 and retransmit > it to a serial link (USB) > - Process 2 receives icmpv6 (or any packet) from the serial link (USB) > and restransmit it to process 1. Oddly, Python does not have ICMP support in its standard library however there is an icmpv6 module on Activestate: http://code.activestate.com/recipes/409689-icmplib-library-for-creating-and-reading-icmp-pack/ And there is also a module for reading/writing to usb ports (pyusb)... http://pyusb.sourceforge.net/docs/1.0/tutorial.html hth, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From emile at fenx.com Mon Jun 4 16:15:00 2012 From: emile at fenx.com (Emile van Sebille) Date: Mon, 04 Jun 2012 07:15:00 -0700 Subject: [Tutor] packets_USB_python In-Reply-To: <4FCCBF00.3020204@imag.fr> References: <4FCCBF00.3020204@imag.fr> Message-ID: On 6/4/2012 6:58 AM BILAL Mustapha said... > Hello, > > I am beginner with python and I recently started to use it in order to > achieve a small part of something I am working on. > > I have 2 processes (process 1 and process 2). I am wondering if you can > provide me with some example or sample code of how I can do the following: Interprocess communications may be a bit advanced for this forum. Look at http://wiki.python.org/moin/ParallelProcessing as you're likely to find something appropriate there. HTH, Emile > > - Process 2 receives icmpv6 (or any packet) from process1 and retransmit > it to a serial link (USB) > - Process 2 receives icmpv6 (or any packet) from the serial link (USB) > and restransmit it to process 1. > > I had tried to write some code to, at least, ping but apparently I am > missing something. > > Many thanks > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From Mustapha.Bilal at imag.fr Mon Jun 4 16:40:59 2012 From: Mustapha.Bilal at imag.fr (BILAL Mustapha) Date: Mon, 04 Jun 2012 16:40:59 +0200 Subject: [Tutor] packets_USB_python In-Reply-To: References: <4FCCBF00.3020204@imag.fr> Message-ID: <4FCCC8FB.4050901@imag.fr> Le 04/06/2012 16:10, Alan Gauld a ?crit : > On 04/06/12 14:58, BILAL Mustapha wrote: > >> - Process 2 receives icmpv6 (or any packet) from process1 and retransmit >> it to a serial link (USB) >> - Process 2 receives icmpv6 (or any packet) from the serial link (USB) >> and restransmit it to process 1. > > Oddly, Python does not have ICMP support in its standard library > however there is an icmpv6 module on Activestate: > > http://code.activestate.com/recipes/409689-icmplib-library-for-creating-and-reading-icmp-pack/ > > > And there is also a module for reading/writing to usb ports (pyusb)... > > http://pyusb.sourceforge.net/docs/1.0/tutorial.html > > hth, > Thank you for your reply and for the useful links. However Can't I use the popen() to lunch the process1 and write on its standard input? same thing for the process1 when it wants to sends infos(or packets) to process2 then it will use its standard output! Best regards From akeriatimothy at gmail.com Mon Jun 4 17:12:54 2012 From: akeriatimothy at gmail.com (Akeria Timothy) Date: Mon, 4 Jun 2012 11:12:54 -0400 Subject: [Tutor] Web Questionnaire Message-ID: Hello All, I am still new to learning Python and I wanted to know if anyone knew of a script out there that would allow users to answer three basic questions, hit submit and send that information to an excel spreadsheet? So, I would want them to submit the following information: Name: Dept: Software needed: Semester requesting software for: I work for a college and I wanted to streamline the software requests for the school year. Is this a basic script? I have started my script but I know it will probably take me longer to write it so I figured I would ask if there was something out there that I could just modify. Thanks in advance -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Mon Jun 4 17:56:50 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 4 Jun 2012 11:56:50 -0400 Subject: [Tutor] Web Questionnaire In-Reply-To: References: Message-ID: On Mon, Jun 4, 2012 at 11:12 AM, Akeria Timothy wrote: > Hello All, > > I am still new to learning Python and I wanted to know if anyone knew of a > script out there that would allow users to answer three basic questions, hit > submit and send that information to an excel spreadsheet? > > So, I would want them to submit the following information: > > Name: > Dept: > Software needed: > Semester requesting software for: > > I work for a college and I wanted to streamline the software requests for > the school year. > > Is this a basic script? I have started my script but I know it will probably > take me longer to write it so I figured I would ask if there was something > out there that I could just modify. > > > Thanks in advance > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > I'm not sure this is a 'python tutor' question. You would need to have a website that presumable limits access to certain people so that anyone couldn't make a request. Django could do that for you out of the box more or less, but then you would have to deploy it. So, its not just a small python script, but a website you would need to build. Are you up to that yet? If you are thinking up useful projects to expand your python skills, perhaps publishing an email box for requests would be easier. You could read the emails and enter the data in a simple command line program you could write in python to added data in csv format to a file. This file could be viewed in excel. Such a project would help you learn about csv module, and how to request data from a user. -- both useful things to learn -- Joel Goldstick From eire1130 at gmail.com Mon Jun 4 18:27:19 2012 From: eire1130 at gmail.com (James Reynolds) Date: Mon, 4 Jun 2012 12:27:19 -0400 Subject: [Tutor] Web Questionnaire In-Reply-To: References: Message-ID: I would say start using django. The best way to learn is by doing. This will let you learn about databases, servers, webapps, and you can get some functionality out of it. Just start going through the django tutorial. On Jun 4, 2012 11:57 AM, "Joel Goldstick" wrote: > On Mon, Jun 4, 2012 at 11:12 AM, Akeria Timothy > wrote: > > Hello All, > > > > I am still new to learning Python and I wanted to know if anyone knew of > a > > script out there that would allow users to answer three basic questions, > hit > > submit and send that information to an excel spreadsheet? > > > > So, I would want them to submit the following information: > > > > Name: > > Dept: > > Software needed: > > Semester requesting software for: > > > > I work for a college and I wanted to streamline the software requests for > > the school year. > > > > Is this a basic script? I have started my script but I know it will > probably > > take me longer to write it so I figured I would ask if there was > something > > out there that I could just modify. > > > > > > Thanks in advance > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > > > I'm not sure this is a 'python tutor' question. You would need to > have a website that presumable limits access to certain people so that > anyone couldn't make a request. Django could do that for you out of > the box more or less, but then you would have to deploy it. So, its > not just a small python script, but a website you would need to build. > Are you up to that yet? > > If you are thinking up useful projects to expand your python skills, > perhaps publishing an email box for requests would be easier. You > could read the emails and enter the data in a simple command line > program you could write in python to added data in csv format to a > file. This file could be viewed in excel. > > Such a project would help you learn about csv module, and how to > request data from a user. -- both useful things to learn > > > -- > Joel Goldstick > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From akeriatimothy at gmail.com Mon Jun 4 18:29:42 2012 From: akeriatimothy at gmail.com (Akeria Timothy) Date: Mon, 4 Jun 2012 12:29:42 -0400 Subject: [Tutor] Web Questionnaire In-Reply-To: References: Message-ID: Thanks Joel the email box suggestion is a good one. I guess I'm lookingfor an easier way to get the information from faculty members on my campus. My boss currently just sends an email out to them. The error with this is that they reply with answers not related to what the email asks. On Jun 4, 2012 11:56 AM, "Joel Goldstick" wrote: > On Mon, Jun 4, 2012 at 11:12 AM, Akeria Timothy > wrote: > > Hello All, > > > > I am still new to learning Python and I wanted to know if anyone knew of > a > > script out there that would allow users to answer three basic questions, > hit > > submit and send that information to an excel spreadsheet? > > > > So, I would want them to submit the following information: > > > > Name: > > Dept: > > Software needed: > > Semester requesting software for: > > > > I work for a college and I wanted to streamline the software requests for > > the school year. > > > > Is this a basic script? I have started my script but I know it will > probably > > take me longer to write it so I figured I would ask if there was > something > > out there that I could just modify. > > > > > > Thanks in advance > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > > > I'm not sure this is a 'python tutor' question. You would need to > have a website that presumable limits access to certain people so that > anyone couldn't make a request. Django could do that for you out of > the box more or less, but then you would have to deploy it. So, its > not just a small python script, but a website you would need to build. > Are you up to that yet? > > If you are thinking up useful projects to expand your python skills, > perhaps publishing an email box for requests would be easier. You > could read the emails and enter the data in a simple command line > program you could write in python to added data in csv format to a > file. This file could be viewed in excel. > > Such a project would help you learn about csv module, and how to > request data from a user. -- both useful things to learn > > > -- > Joel Goldstick > -------------- next part -------------- An HTML attachment was scrubbed... URL: From email at justinstraube.com Mon Jun 4 17:36:50 2012 From: email at justinstraube.com (Justin Straube) Date: Mon, 04 Jun 2012 09:36:50 -0600 Subject: [Tutor] suggestions to improve design Message-ID: <4FCCD612.7010109@justinstraube.com> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello Tutors, I've written a script to use within XChat (IRC client) and everything is working without issue. The script is a little tool to give an emote command and a handful of general pre written actions. As it is now, the script works. But I've got maybe a dozen emotes in there, using if/elif loops to check against a list of emotes and format a string for display in the channel. But, I also have another dictionary containing duplicate values with text placeholders where a %s would be substituted. I feel that, as the list of available emotes grows, the script will suffer from a poor design. I have thought about pickling or using a file to store the dict or loops, but that only seems like a bandage and there may be a performance issue with potentially hundreds of loops to check through. Ok, so I have posted my script at http://www.baked-potato.us/files/2ndthat.py My question is, would anybody please take a look and offer suggestions on what I may do to improve this script? Thank you, Justin -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.16 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQEcBAEBAgAGBQJPzNYQAAoJEBXz28t2j4R5cmYIAIVIQMJI7HshaV0WqmbUKp1U +vFdLbD52JHs+x4dUwq6DBWdAllauxkm0GW6PGYhHefQZrzRrplTG37Oj+C6czvT pHT6mI1AwXdrQIQab6D0JEPkqIw9kuQormNrUynxwZFSU7M/Z1qmBv0twikwDUVD R3hClBeuVMydLk2DhIuPpgqEO/6ljkN8tK6El5+1vIQFsVRizEX2HVeco7oMz/9x ePedVpEfW4cGpRcFz2gnthzLawoPPmxKc3fTTq29za/tR8eqvUxXY4vjxWWeOr/+ N97Yfdglo1pa4Kir/Ukd+678R/WHZzrqZVRTa4fRZC17cSOU1Pd96L2cqwOCqzs= =0zEs -----END PGP SIGNATURE----- -------------- next part -------------- A non-text attachment was scrubbed... Name: 0x768F8479.asc Type: application/pgp-keys Size: 1744 bytes Desc: not available URL: From ramit.prasad at jpmorgan.com Mon Jun 4 18:12:25 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Mon, 4 Jun 2012 16:12:25 +0000 Subject: [Tutor] Joining all strings in stringList into one string In-Reply-To: <4FCA2333.3080302@pearwood.info> References: <4FC9FB7C.2070605@gmail.com> <4FCA2333.3080302@pearwood.info> Message-ID: <5B80DD153D7D744689F57F4FB69AF47412D7A2A9@SCACMX008.exchad.jpmchase.net> > But more importantly, some years ago (Python 2.4, about 8 years ago?) the > Python developers found a really neat trick that they can do to optimize > string concatenation so it doesn't need to repeatedly copy characters over > and > over and over again. I won't go into details, but the thing is, this trick > works well enough that repeated concatenation is about as fast as the join > method MOST of the time. I would like to learn a bit more about the trick if you have a reference handy. I have no intention of using it, but it sounds interesting and might teach me more about Python internals. Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 ramit.prasad at jpmorgan.com Mon Jun 4 18:18:59 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Mon, 4 Jun 2012 16:18:59 +0000 Subject: [Tutor] [Parser/pgen] Error 1 In-Reply-To: References: <1SbMKz-000FtH-4h@internal.tormail.org> Message-ID: <5B80DD153D7D744689F57F4FB69AF47412D7B2DD@SCACMX008.exchad.jpmchase.net> > > I was following instructions on this link: > > > > http://eli.thegreenplace.net/2011/10/10/installing-python-2-7-on-ubuntu/ > > > > > > Thank you all so much I cannot wait to learn from you guys :) > Not sure about your error. However, python is pre installed in > ubuntu. go to a shell and type python. See if you get the python > shell The Ubuntu repository should have most versions you would want if you decide you need a different version than the system default. More of a Debian user myself so I would do something like the following to see the different versions (just a guess since I am away from my Debian machine). `apt-cache search python | grep ^python` I grep for ^python because there are probably a lot of python package libraries that are unrelated to core python, but useful for other applications. Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 alan.gauld at btinternet.com Mon Jun 4 19:27:00 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 04 Jun 2012 18:27:00 +0100 Subject: [Tutor] [Parser/pgen] Error 1 In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47412D7B2DD@SCACMX008.exchad.jpmchase.net> References: <1SbMKz-000FtH-4h@internal.tormail.org> <5B80DD153D7D744689F57F4FB69AF47412D7B2DD@SCACMX008.exchad.jpmchase.net> Message-ID: On 04/06/12 17:18, Prasad, Ramit wrote: > The Ubuntu repository should have most versions you would want > if you decide you need a different version than the system default. > > `apt-cache search python | grep ^python` apt-cache search python | grep ^python[1-9] Works on Ubuntu too. But when I try it (10.4 LTS) it only shows versions 2.6, 3 and 3.1. Not 2.7... But the easier way is to use the Ubuntu Software centre. Look under Developer Tools and there is a whole section on Python... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From ramit.prasad at jpmorgan.com Mon Jun 4 19:28:29 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Mon, 4 Jun 2012 17:28:29 +0000 Subject: [Tutor] suggestions to improve design In-Reply-To: <4FCCD612.7010109@justinstraube.com> References: <4FCCD612.7010109@justinstraube.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF47412D7B3FD@SCACMX008.exchad.jpmchase.net> > I've written a script to use within XChat (IRC client) and everything is > working without issue. The script is a little tool to give an emote > command and a handful of general pre written actions. > > As it is now, the script works. But I've got maybe a dozen emotes in > there, using if/elif loops to check against a list of emotes and > format a string for display in the channel. > > But, I also have another dictionary containing duplicate values with > text placeholders where a %s would be substituted. > > I feel that, as the list of available emotes grows, the script will > suffer from a poor design. I have thought about pickling or using a file > to store the dict or loops, but that only seems like a bandage and > there may be a performance issue with potentially hundreds of loops to > check through. > > Ok, so I have posted my script at > http://www.baked-potato.us/files/2ndthat.py > > My question is, would anybody please take a look and offer suggestions > on what I may do to improve this script? Unless the script gets a *very* large list of emotes or you are on an older machine I doubt that you will really see any performance issues. That being said, my comments are below and other tutors can correct anything I missed or said incorrectly. I like the presence of your documentation, kudos! Why use a class for storing the 'tions' and dict directly from the module? tions = { [snip] } I also think get_emote should not be passed the entire argv, it should be passed only the arguments it needs so I would change the call to def emote(argv): emote = argv[1] if emote in tions: # look at dictionary, no need to pull keys manually emstring = get_emote(emote, argv[2:]) # pass only arguments # print a generic message to the channel displaying the emote string #xchat.emit_print("Generic Message", nick, emstring) xchat.command("me %s" % (emstring)) def get_emote( e, args ): if e in tions: if '%s' in tions[e]: return tions[e] % tuple(args) # will error here if not the correct number of variables # in args; might want to catch and then return an error string else return tions[e] else: return 'Not an understood emote' def list_em(): print "There are %s emotes available." % (len(tions)) print "Usage: /em \002<$emote>\002 - user supplied values are shown as \002<$bold>\002.\n" for key,value in tions.iteritems(): print "Emote: %s - %s" % (key, value) As a general note, you should use docstrings instead of comments above the function. CHANGE # show function - displays a single requested emote def show(s): TO def show(s): 'displays a single requested emote' # single line docstring OR def show(s): ''' multi line docstring ''' Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 wprins at gmail.com Mon Jun 4 20:07:23 2012 From: wprins at gmail.com (Walter Prins) Date: Mon, 4 Jun 2012 19:07:23 +0100 Subject: [Tutor] [Parser/pgen] Error 1 In-Reply-To: References: <1SbMKz-000FtH-4h@internal.tormail.org> <5B80DD153D7D744689F57F4FB69AF47412D7B2DD@SCACMX008.exchad.jpmchase.net> Message-ID: Hi, On 4 June 2012 18:27, Alan Gauld wrote: > On 04/06/12 17:18, Prasad, Ramit wrote: > > apt-cache search python | grep ^python[1-9] > >> Works on Ubuntu too. But when I try it (10.4 LTS) it only > shows versions 2.6, 3 and 3.1. Not 2.7... Just to add/point out for the sake of the original poster: The original web page referenced was about installing Python 2.7 on Ubuntu 10.04, which didn't include and didn't have Python 2.7 available from package repositories, which is why the author of the web page had to install it manually. However, the original question poster is in fact using Ubuntu 12.04, which does come with Python 2.7 included and also likely has a few other version available in the package repositories. So the web page is actually not applicable to the original poster, unless he's also trying to install a version of Python not available in the 12.04 package repositories. To the original poster: What version of Python are you trying to install? Best regards, Walter From dispatch3d at live.com Mon Jun 4 20:50:00 2012 From: dispatch3d at live.com (=?utf-8?B?ZGlzcGF0Y2gzZEBsaXZlLmNvbQ==?=) Date: Mon, 4 Jun 2012 11:50:00 -0700 Subject: [Tutor] =?utf-8?q?Beginner?= Message-ID: I am new to the python idle. I am a little confused on the GUI section on where to write it. The tutorial I am using says not to write GUI in the idle section so I have been using the python shell to write the GUI app. This is not to productive for me cause after I save it as .pyw I am unable to open it to edit or add to the code. I have to start over in another shell. My question is working with Tkinter in order to run it and being able to see step by step progress and maintain the ability to edit or add as I go where do I need to code it what program? As well once I have saved the code as .pyw when I go to the location to open and run it it does nothing how do I run it after its saved and completed. I am running winows 7 thanks Dean. Sent from my Verizon Wireless 4G LTE Smartphone -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Tue Jun 5 00:25:25 2012 From: wprins at gmail.com (Walter Prins) Date: Mon, 4 Jun 2012 23:25:25 +0100 Subject: [Tutor] Beginner In-Reply-To: References: Message-ID: Hello, On 4 June 2012 19:50, dispatch3d at live.com wrote: > I am new to the python idle. I am a little confused on the GUI section on > where to write it. I think that's perhaps an understatement. >> The tutorial I am using says not to write GUI in the idle section so I have > been using the python shell to write the GUI app. I'm pretty sure the tutorial didn't instruct you to actually use the shell to write GUI (or any kind of) applications... (By the way, you didn't mention what tutorial you're using?) > This is not to productive > for me cause after I save it as .pyw I am unable to open it to edit or add > to the code. I have to start over in another shell. Yes, well, it's a bad idea, and the downsides you mention is true of writing any Python application, not just GUI ones. You should use the shell to experiment, test, peform introspection, read documentation, etc. etc, not to actually develop your applications... that's what editors and IDE's are for. > My question is working with Tkinter in order to run it and being able to see > step by step progress and maintain the ability to edit or add as I go where > do I need to code it what program? TkInter apps can sometimes not play nice with Idle when run from within Idle for various reasons which I've now forgotten (partly because Idle is itself written in TkInter I think), but I seem to also vaguely recall that this is somewhat less of a problem nowadays than it used to be and/or the issues can be avoided with a bit of care. (I'm sure the others on the list will correct me if I'm imagining things and/or elaborate on this.) So, if your app is simple, and certainly while you're learning you might want to use Idle despite what tutorial says. At worst, you can avoid issues by simply running your application manually e.g. apart from Idle. (Notwithstanding your next point.) > As well once I have saved the code as .pyw when I go to the location to open > and run it it does nothing how do I run it after its saved and completed. Well when done correctly it should work. It's impossible to tell without more information about what's really going on why it's "doing nothing." There may be a problem with your program, there might be a problem with your Python setup, there may be a problem with your operating system environment, etc. etc. Anyway, if you open your .pyw file with say Windows notepad or any other text editor (or, indeed, Idle), what does it contain? > I am running winows 7 thanks What version of Python? I recommend the Activestate distribution of Python on Windows these days as their installer does several convenience steps automatically that the official Python distributions do not do and provides several tools useful on Windows as well. You might have a look at it/switch if you're not using it yet: http://www.activestate.com/activepython/downloads Hope that helps, Walter From alan.gauld at btinternet.com Tue Jun 5 02:25:03 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 05 Jun 2012 01:25:03 +0100 Subject: [Tutor] Beginner In-Reply-To: References: Message-ID: On 04/06/12 19:50, dispatch3d at live.com wrote: > The tutorial I am using says not to write GUI in the idle section I suspect it only says not to *run* it in IDLE. Writing it in IDLE is fine. But sometimes running Tkinter apps in Idle causes strange behaviour. So save the file then run it from Windows Explorer, eg. by double clicking the file. > have been using the python shell to write the GUI app. The Python shell is designed for testing and exploring not for writing apps. Use a text editor (Idle or something else). On windows I tended to use vim or Scite or occasionally Eclipse with PyDev plugin. > As well once I have saved the code as .pyw when I go to the location to > open and run it it does nothing how do I run it after its saved and > completed. That's probably due to a coding error. Write a short Tkinter program that exhibits the problem and post it here. We can then try to advise you. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From d at davea.name Tue Jun 5 04:00:52 2012 From: d at davea.name (Dave Angel) Date: Mon, 04 Jun 2012 22:00:52 -0400 Subject: [Tutor] use the data None In-Reply-To: References: Message-ID: <4FCD6854.9010508@davea.name> On 06/04/2012 06:14 AM, Tehn Yit Chin wrote: > Thanks for the quick answers. > > The potential for the variable not to exists is when I am using the > optparser module, and I want to check if a particular parameter was passed > in or not. If the parameter was not passed in, then the variable would not > exists. Eg > > If I call a python script is expecting a parameter "param1" but I failed to > passed it via the command line, I would do the following check > > (options, args)=parser.parse_args() > if options.param1 != None: > param1 = int(options.param1 ) > else: > param1 = 30 > > By the way, please excuse the way I am coding, I come from a C background. > > I think I shall try the catch method. > > Thanks! > > On Mon, Jun 4, 2012 at 3:09 AM, Alan Gauld wrote: > >> On 04/06/12 01:39, Alan Gauld wrote: >> >> for var in (a,b,c): >>> if not var: >>> print var.__name__, ' is empty or false' >>> >> Oops, that won't work. __name__ is not an attribute >> of object, which I thought it was... >> >> But hopefully the intention was clear. >> >> >> -- >> Alan G >> Author of the Learn to Program web site >> http://www.alan-g.me.uk/ >> >> ______________________________**_________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/**mailman/listinfo/tutor >> > Gosh, you said the variable might not exist. But then you're using the code options.param1 where param1 is an attribute, not a variable in any sense of the word. If you have an *attribute* that may or may not exist, you can use getattr() function on it, and even specify your own default. Of course, if the stuff is coming from some argument parsing logic, it probably already handles this for you, and this is all besides the point. -- DaveA From email at justinstraube.com Tue Jun 5 06:27:48 2012 From: email at justinstraube.com (Justin Straube) Date: Mon, 04 Jun 2012 22:27:48 -0600 Subject: [Tutor] suggestions to improve design In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47412D7B3FD@SCACMX008.exchad.jpmchase.net> References: <4FCCD612.7010109@justinstraube.com> <5B80DD153D7D744689F57F4FB69AF47412D7B3FD@SCACMX008.exchad.jpmchase.net> Message-ID: <4FCD8AC4.4090501@justinstraube.com> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 06/04/2012 11:28 AM, Prasad, Ramit wrote: > Unless the script gets a *very* large list of emotes or you are on > an older machine I doubt that you will really see any performance > issues. That being said, my comments are below and other tutors Thank you for your time, Ramit. > can correct anything I missed or said incorrectly. I like the > presence of your documentation, kudos! As a hobbyist, I can afford the time to document. I also find that even with code I wrote, the documentation helps me understand better, and find areas that need more work. > Why use a class for storing the 'tions' and dict directly from the > module? > > tions = { [snip] } When I started, I was unsure what else I would be putting in the class Emo. After I got going, I was unsure about changing that, if the design needed adjusting and it would be used further. Before I do to much more for this, I am going to remove the class and just use the dictionary, with a better name. Thank you for your other ideas. Ill work over this and also clean up my comments. Justin > I also think get_emote should not be passed the entire argv, it > should be passed only the arguments it needs so I would change the > call to > > def emote(argv): emote = argv[1] if emote in tions: # look at > dictionary, no need to pull keys manually emstring = > get_emote(emote, argv[2:]) # pass only arguments > > # print a generic message to the channel displaying the emote > string #xchat.emit_print("Generic Message", nick, emstring) > xchat.command("me %s" % (emstring)) > > def get_emote( e, args ): if e in tions: if '%s' in tions[e]: > return tions[e] % tuple(args) # will error here if not the correct > number of variables # in args; might want to catch and then return > an error string else return tions[e] else: return 'Not an > understood emote' > > > def list_em(): print "There are %s emotes available." % > (len(tions)) print "Usage: /em \002<$emote>\002 - user supplied > values are shown as \002<$bold>\002.\n" > > for key,value in tions.iteritems(): print "Emote: %s - %s" % (key, > value) > > > As a general note, you should use docstrings instead of comments > above the function. > > CHANGE > > # show function - displays a single requested emote def show(s): > > > TO > > def show(s): 'displays a single requested emote' # single line > docstring > > OR > > def show(s): ''' multi line docstring ''' > > > Ramit > > > Ramit Prasad | JPMorgan Chase Investment Bank | Currencies > Technology 712 Main Street | Houston, TX 77002 work phone: 713 - > 216 - 5423 -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.16 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQEcBAEBAgAGBQJPzYq7AAoJEBXz28t2j4R5MT4H/2ILvS33kQmW5r8sNyeszoir p5BNJOOEcEpc9Th41+12hXrP1Jir0COUur1HKqoom6Ufx07FuYPhiQ2MOC7W43EZ I9m8/F0b/DkxLBBwg4CL+tfTAhIZ9R2/rfKxQ8GQSN4Yn29X6WGrP/uK9TRlHERq ucb3tYPOp8b/zMujILVjndkqozupqNTrGZVBe8F8tphcnlv6ScZHjDRsaK898SbL mcK/Ua45XVpaGpunIV8Um03hVf+UZwwvat348vOE8fgsOyywXaZc0oHsVr6G41os zsQXR2/3CJA88ZVqJzRKmwdHJ2xuPsNWnrTuxplsHnsNWuHMHBHmnPj+HC9Gnsw= =BPDI -----END PGP SIGNATURE----- -------------- next part -------------- A non-text attachment was scrubbed... Name: 0x768F8479.asc Type: application/pgp-keys Size: 1744 bytes Desc: not available URL: From dave6502 at gmail.com Tue Jun 5 12:13:41 2012 From: dave6502 at gmail.com (dave selby) Date: Tue, 5 Jun 2012 11:13:41 +0100 Subject: [Tutor] Threads, ok to access parent script methods by passing 'self' ? Message-ID: Hi All, If a thread is started and 'self' is passed as a parameter, is it acceptable to access methods of the calling class via 'self.updateGrid()' etc from within the thread or does everything have to be done via wx.lib.pubsub and publisher calls ? Cheers Dave -- Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html From wprins at gmail.com Tue Jun 5 12:41:17 2012 From: wprins at gmail.com (Walter Prins) Date: Tue, 5 Jun 2012 11:41:17 +0100 Subject: [Tutor] Threads, ok to access parent script methods by passing 'self' ? In-Reply-To: References: Message-ID: Hi Dave, On 5 June 2012 11:13, dave selby wrote: > Hi All, > > If a thread is started and 'self' is passed as a parameter, is it > acceptable to access methods of the calling class via > 'self.updateGrid()' etc from within the thread or does everything have > to be done via wx.lib.pubsub and publisher calls ? I infer you're using WXPython? Anyway, generally most GUIs run on a single thread and the resources attached to these GUI objects can only safely be accessed by that main thread that the GUI is running. Directly calling on GUI objects from threads can somtimes work but can give rise to very obscure and strange bugs. Consequently you need to use a mechanism of some sort to return data from or signal from a worker thread back to the main thread, without directly calling the GUI objects from these worker thread(s) (or directly calling methods that directly call on GUI objects), which is what it sounds like your updateGrid() method may be doing. There are a number of ways to achieve this but concretely perhaps the simplest way for now in your case is to use the wx.CallAfter() method. This basically lets you hand off the method to call together with the parameters to use to WX, which will then cal your method on your behalf, from the context of the main GUI thread. See: http://www.wxpython.org/docs/api/wx-module.html#CallAfter or shortened: http://is.gd/abnhbG HTH, Walter From dave6502 at gmail.com Tue Jun 5 13:29:54 2012 From: dave6502 at gmail.com (dave selby) Date: Tue, 5 Jun 2012 12:29:54 +0100 Subject: [Tutor] Threads, ok to access parent script methods by passing 'self' ? In-Reply-To: References: Message-ID: On 5 June 2012 12:05, dave selby wrote: > I was running the thread by instantiating a separate class but this > will make the very neat CallAfter() difficult, is it OK to call a > method in the main class as a thread in which case the CallAfter() > should work OK ? > > Thanks Again > > Dave -- Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html From Mustapha.Bilal at imag.fr Tue Jun 5 13:53:09 2012 From: Mustapha.Bilal at imag.fr (BILAL Mustapha) Date: Tue, 05 Jun 2012 13:53:09 +0200 Subject: [Tutor] error_connection_refused In-Reply-To: References: Message-ID: <4FCDF325.6020705@imag.fr> Hello, I am trying to lunch a small python program but I am always getting this error: s.connect((IP, PORT)) File "/usr/lib/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 111] Connection refused What's weird that it doesn't even print the line "test sending data" so how it could get to s.connect() without passing through this line!! Here is the code: #!/usr/bin/python import socket import thread import time from threading import Thread import threading import sys IP = "130.190.31.167" PADDING = "a" * 1000 DATA = PADDING + "this is sentence number = " PORT = 8000 killed = False count=0 def main(): print("the network manager process is created") send_data() def send_data(): *print("test sending data")* s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) *s.connect((IP, PORT))* while killed==False: count= count+1 sent = s.send(DATA+ str(count) + "\n") if sent == 0: print 'The connection is died' killed=True s.close() if __name__ == "__main__": main() Many thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From wrw at mac.com Tue Jun 5 21:00:32 2012 From: wrw at mac.com (William R. Wing (Bill Wing)) Date: Tue, 05 Jun 2012 15:00:32 -0400 Subject: [Tutor] Newbe problem with wxPython on Mac OS X (need a work-around) Message-ID: I'm running Mac OS-X 10.7.4 (Lion) and have installed Python 2.7 from Python.org. It's in /Library so as to not conflict with the one from Apple in /System/Library and I've set my .profile to make it my default. So far so good. I decided I wanted to start playing with wxPython, and downloaded the "stable" version for Python 2.7 at wxPython.org. I promptly ran into the following error when I tried one of the demos: Python[45569:f07] Error loading /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: dlopen(/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types, 262): no suitable image found. Did find: /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: no matching architecture in universal wrapper Python: OpenScripting.framework - scripting addition "/Library/ScriptingAdditions/Adobe Unit Types.osax" declares no loadable handlers. I assumed this was due to an architecture conflict (32 vs 64 bits), so grabbed the 2.9 version of wxPython which is based on cocoa widgets and should be 64-bit clean. I get exactly the same error, so the problem really is with the ScriptingAdditions. I asked Google about the error and there seems to be a work-around for Adobe users (they are supposed to invoke arch -i386 osascript script.scpt in instead of osascript script.scpt). As far as I can tell, that isn't applicable to Python users, so I assume there must be a work around for this until Adobe gets 64 bit support compiled in. Does anyone know what it might be? Thanks, Bill From alan.gauld at btinternet.com Tue Jun 5 23:04:51 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 05 Jun 2012 22:04:51 +0100 Subject: [Tutor] Newbe problem with wxPython on Mac OS X (need a work-around) In-Reply-To: References: Message-ID: On 05/06/12 20:00, William R. Wing (Bill Wing) wrote: > I assumed this was due to an architecture conflict (32 vs 64 bits), > so grabbed the 2.9 version of wxPython which is based on cocoa > I get exactly the same error, so the problem really is with the > ScriptingAdditions. I suggest you'll get better support asking on the wxPython and MacPython mailing lists. This is a wee bit specific to wxPython and MacOS for most folks here, although you might just strike lucky... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue Jun 5 23:12:28 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 05 Jun 2012 22:12:28 +0100 Subject: [Tutor] error_connection_refused In-Reply-To: <4FCDF325.6020705@imag.fr> References: <4FCDF325.6020705@imag.fr> Message-ID: On 05/06/12 12:53, BILAL Mustapha wrote: > Hello, Hi, Its best not to hijack somebody else's thread coz it messes up all the threaded mail/news readers out there. (At least you changed the subject though!). Please start with a fresh mail for a new topic. > I am trying to lunch a small python program but I am always getting this > error: How are you "launching" it? From a batch file or shell script? From another Python program? From the Python prompt? > s.connect((IP, PORT)) > File "/usr/lib/python2.7/socket.py", line 224, in meth > return getattr(self._sock,name)(*args) > socket.error: [Errno 111] Connection refused > > What's weird that it doesn't even print the line "test sending data" so > how it could get to s.connect() without passing through this line!! It sounds like you might be using a Python >>> prompt and not reloading the module after making changes. importing a second time won't work as Python knows you already have it and ignores it. You must use reload() for changes to take effect. If thats not it then we need more details about what you are doing. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Wed Jun 6 03:05:33 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 06 Jun 2012 11:05:33 +1000 Subject: [Tutor] error_connection_refused In-Reply-To: <4FCDF325.6020705@imag.fr> References: <4FCDF325.6020705@imag.fr> Message-ID: <4FCEACDD.2040600@pearwood.info> BILAL Mustapha wrote: > Here is the code: Unfortunately not. What you have posted contains at least two SyntaxErrors that prevents it from running: > def send_data(): > *print("test sending data")* > s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) > *s.connect((IP, PORT))* Two of those four lines are invalid: the indentation is wrong, and there are leading and trailing asterisks * on the lines. Possibly your email program has "helpfully" added them because you marked the lines as bold for some reason. There may be other errors I haven't spotted. When sending code, please ensure that you send actual RUNNING code that does what you say it does. If you help us to run your code, we can help you fix the bug. Copy and paste from the code you actually run, or attach the file to your email as an attachment. Double-check that your email program sends plain text, including correct indentation, and not HTML/Rich Text. Double-check the indentation is correct. If you must add comments or annotations to the code, use Python comment syntax # like this and not asterisks * or formatting, or animated gifs of dancing bears, or any other rubbish that gets in the way of running the code. This may also help: http://sscce.org/ Thank you, -- Steven From marc.tompkins at gmail.com Wed Jun 6 03:10:39 2012 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 5 Jun 2012 18:10:39 -0700 Subject: [Tutor] The dreaded UnicodeDecodeError... why, why, why does it still want ascii? Message-ID: Hello all - it's been a while! I'm trying to parse a webpage using lxml; every time I try, I'm rewarded with "UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position?????: ordinal not in range(128)"? (the byte value and the position occasionally change; the error never does.) The page's encoding is UTF-8: ? so I have tried: -? setting HTMLParser's encoding to 'utf-8' -? reading the page first, decoding as 'utf-8', then re-encoding as 'ascii' with options 'replace' or 'ignore' -? and various combinations thereof Here's my current version, trying everything at once: from __future__ import print_function import datetime import urllib2 from lxml import etree url = 'http://www.wpc-edi.com/reference/codelists/healthcare/claim-adjustment-reason-codes/' page = urllib2.urlopen(url) pagecontents = page.read() pagecontents = pagecontents.decode('utf-8') pagecontents = pagecontents.encode('ascii', 'ignore') tree = etree.parse(pagecontents, etree.HTMLParser(encoding='utf-8',recover=True)) and here's the result: Traceback (most recent call last): File "etreeTest.py", line 10, in tree = etree.parse(pagecontents, etree.HTMLParser(encoding='utf-8',recover=True)) File "lxml.etree.pyx", line 2942, in lxml.etree.parse (src/lxml/lxml.etree.c:54187) File "parser.pxi", line 1528, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:79485) File "parser.pxi", line 1557, in lxml.etree._parseDocumentFromURL (src/lxml/lxml.etree.c:79768) File "parser.pxi", line 1457, in lxml.etree._parseDocFromFile (src/lxml/lxml.etree.c:78843) File "parser.pxi", line 997, in lxml.etree._BaseParser._parseDocFromFile (src/lxml/lxml.etree.c:75698) File "parser.pxi", line 564, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:71739) File "parser.pxi", line 645, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:72614) File "parser.pxi", line 579, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:71894) UnicodeDecodeError: 'ascii' codec can't decode byte 0x94 in position 63953: ordinal not in range(128) Script terminated. I'm at my wit's end: how do I either change HTMLParser's codec to UTF-8, or strip non-ASCII characters out of the stream? What am I missing? Environment: Python 2.7.3, 32bit - on Windows 7 Ultimate, 64bit lxml 2.3 From benjamincox_2014 at depauw.edu Wed Jun 6 05:14:00 2012 From: benjamincox_2014 at depauw.edu (Benjamin Cox) Date: Tue, 5 Jun 2012 22:14:00 -0500 Subject: [Tutor] Running .py Files Message-ID: Hello, So, I'm very new to Python and programming in general, and I've been having trouble figuring out how to run programs by clicking on their .py file name. For some reason, my computer's default setting is to open them in wordpad. I have Windows 7 32-bit OS and Python 3.2.3. Any help would be greatly appreciated! Thanks, Ben -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Wed Jun 6 05:41:16 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 06 Jun 2012 04:41:16 +0100 Subject: [Tutor] Running .py Files In-Reply-To: References: Message-ID: On 06/06/2012 04:14, Benjamin Cox wrote: > Hello, > > So, I'm very new to Python and programming in general, and I've been having > trouble figuring out how to run programs by clicking on their .py file > name. For some reason, my computer's default setting is to open them in > wordpad. I have Windows 7 32-bit OS and Python 3.2.3. Any help would be > greatly appreciated! > > Thanks, > Ben > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Start here http://docs.python.org/release/3.2/using/windows.html, any problems please feel free to ask as we don't bite :) -- Cheers. Mark Lawrence. From stefan_ml at behnel.de Wed Jun 6 08:22:35 2012 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 06 Jun 2012 08:22:35 +0200 Subject: [Tutor] The dreaded UnicodeDecodeError... why, why, why does it still want ascii? In-Reply-To: References: Message-ID: Marc Tompkins, 06.06.2012 03:10: > I'm trying to parse a webpage using lxml; every time I try, I'm > rewarded with "UnicodeDecodeError: 'ascii' codec can't decode byte > 0x?? in position?????: ordinal not in range(128)" (the byte value and > the position occasionally change; the error never does.) > > The page's encoding is UTF-8: > > so I have tried: > - setting HTMLParser's encoding to 'utf-8' That's the way to do it, although the parser should be able to figure it out by itself, given the above content type declaration. > Here's my current version, trying everything at once: > > from __future__ import print_function > import datetime > import urllib2 > from lxml import etree > url = 'http://www.wpc-edi.com/reference/codelists/healthcare/claim-adjustment-reason-codes/' > page = urllib2.urlopen(url) > pagecontents = page.read() > pagecontents = pagecontents.decode('utf-8') > pagecontents = pagecontents.encode('ascii', 'ignore') > tree = etree.parse(pagecontents, > etree.HTMLParser(encoding='utf-8',recover=True)) parse() is meant to parse from files and file-like objects, so you are telling it to parse from the "file path" in pagecontents, which obviously does not exist. I admit that the error message is not helpful. You can do this: connection = urllib2.urlopen(url) tree = etree.parse(connection, my_html_parser) Alternatively, use fromstring() to parse from strings: page = urllib2.urlopen(url) pagecontents = page.read() html_root = etree.fromstring(pagecontents, my_html_parser) See the lxml tutorial. Also note that there's lxml.html, which provides an extended tool set for HTML processing. Stefan From Mustapha.Bilal at imag.fr Wed Jun 6 09:49:21 2012 From: Mustapha.Bilal at imag.fr (BILAL Mustapha) Date: Wed, 06 Jun 2012 09:49:21 +0200 Subject: [Tutor] error_connection_refused In-Reply-To: References: <4FCDF325.6020705@imag.fr> Message-ID: <4FCF0B81.6050509@imag.fr> Hello, Thank you for your reply. Please read underlines. Le 05/06/2012 23:12, Alan Gauld a ?crit : > On 05/06/12 12:53, BILAL Mustapha wrote: >> Hello, > > Hi, Its best not to hijack somebody else's thread coz it messes up all > the threaded mail/news readers out there. (At least you changed the > subject though!). Please start with a fresh mail for a new topic. > > Oops, sorry I didn't pay attention to it.. >> I am trying to lunch a small python program but I am always getting this >> error: > > How are you "launching" it? From a batch file or shell script? > From another Python program? From the Python prompt? > >> s.connect((IP, PORT)) >> File "/usr/lib/python2.7/socket.py", line 224, in meth >> return getattr(self._sock,name)(*args) >> socket.error: [Errno 111] Connection refused >> >> What's weird that it doesn't even print the line "test sending data" so >> how it could get to s.connect() without passing through this line!! > > It sounds like you might be using a Python >>> prompt and not > reloading the module after making changes. importing a second time > won't work as Python knows you already have it and ignores it. You > must use reload() for changes to take effect. > > If thats not it then we need more details about what you are doing. > I actually found the solution which was somehow trivial, I was not listening to an open port so I changed the port and it worked fine. Thank you again Regards > HTH, > From marc.tompkins at gmail.com Wed Jun 6 10:21:08 2012 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Wed, 6 Jun 2012 01:21:08 -0700 Subject: [Tutor] The dreaded UnicodeDecodeError... why, why, why does it still want ascii? In-Reply-To: References: Message-ID: On Tue, Jun 5, 2012 at 11:22 PM, Stefan Behnel wrote: > You can do this: > > connection = urllib2.urlopen(url) > tree = etree.parse(connection, my_html_parser) > > Alternatively, use fromstring() to parse from strings: > > page = urllib2.urlopen(url) > pagecontents = page.read() > html_root = etree.fromstring(pagecontents, my_html_parser) > > Thank you! fromstring() did the trick for me. Interestingly, your first suggestion - parsing straight from the connection without an intermediate read() - appears to create the tree successfully, but my first strip_tags() fails, with the error "ValueError: Input object has no document: lxml.etree._ElementTree". Since fromstring() works just fine, I will set this aside as a mystery for my copious free time (after this project is done, for example.) > See the lxml tutorial. I did - I've been consulting it religiously - but I missed the fact that I was mixing strings with file-like IO, and (as you mentioned) the error message really wasn't helping me figure out my problem. Perhaps I should have figured it out from the fact that the character value and position change, even though the webpage doesn't... but no. > Also note that there's lxml.html, which provides an > extended tool set for HTML processing. > I've been using lxml.etree because I'm used to the syntax, and because (perhaps mistakenly) I was under the impression that its parser was more resilient in the face of broken HTML - this page has unclosed tags all over the place. I'll try lxml.html, but (again) it'll have to be some time later. -------------- next part -------------- An HTML attachment was scrubbed... URL: From Mustapha.Bilal at imag.fr Wed Jun 6 11:02:01 2012 From: Mustapha.Bilal at imag.fr (BILAL Mustapha) Date: Wed, 06 Jun 2012 11:02:01 +0200 Subject: [Tutor] connecting to USB Message-ID: <4FCF1C89.3040803@imag.fr> Hello, I am working to connect to a serial link (USB). In order to do that I have to write this command: sudo ../../tools/stm32w/serialdump-linux -b115200 -d10000 (keeping in mind dat*serialdump-linux*, which is in the path, is not a folder, it's an executable) To get to the root, I have used: returncode = subprocess.call(["/usr/bin/sudo", "/usr/bin/id"]) Then to connect to the USB through the command above I tried the following: usb = Popen(['../../tools/stm32w/serialdump-linux','-b115200','-d10000'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) usb.communicate()[0] But I am getting an error, it's considering that serialdump-linux as a folder!! Any help how I can run the command? Many thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan_ml at behnel.de Wed Jun 6 11:36:14 2012 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 06 Jun 2012 11:36:14 +0200 Subject: [Tutor] The dreaded UnicodeDecodeError... why, why, why does it still want ascii? In-Reply-To: References: Message-ID: Marc Tompkins, 06.06.2012 10:21: > On Tue, Jun 5, 2012 at 11:22 PM, Stefan Behnel wrote: > >> You can do this: >> >> connection = urllib2.urlopen(url) >> tree = etree.parse(connection, my_html_parser) >> >> Alternatively, use fromstring() to parse from strings: >> >> page = urllib2.urlopen(url) >> pagecontents = page.read() >> html_root = etree.fromstring(pagecontents, my_html_parser) >> >> > Thank you! fromstring() did the trick for me. > > Interestingly, your first suggestion - parsing straight from the connection > without an intermediate read() - appears to create the tree successfully, > but my first strip_tags() fails, with the error "ValueError: Input object > has no document: lxml.etree._ElementTree". Weird. You may want to check the parser error log to see if it has any hint. >> See the lxml tutorial. > > I did - I've been consulting it religiously - but I missed the fact that I > was mixing strings with file-like IO, and (as you mentioned) the error > message really wasn't helping me figure out my problem. Yes, I think it could do better here. Reporting a parser error with an "unprintable error message" would at least make it less likely that users are being diverted from the actual cause of the problem. >> Also note that there's lxml.html, which provides an >> extended tool set for HTML processing. > > I've been using lxml.etree because I'm used to the syntax, and because > (perhaps mistakenly) I was under the impression that its parser was more > resilient in the face of broken HTML - this page has unclosed tags all over > the place. Both are using the same parser and share most of their API. lxml.html is mostly just an extension to lxml.etree with special HTML tools. Stefan From Mustapha.Bilal at imag.fr Wed Jun 6 11:59:46 2012 From: Mustapha.Bilal at imag.fr (BILAL Mustapha) Date: Wed, 06 Jun 2012 11:59:46 +0200 Subject: [Tutor] connecting to USB In-Reply-To: <4FCF1C89.3040803@imag.fr> References: <4FCF1C89.3040803@imag.fr> Message-ID: <4FCF2A12.8010601@imag.fr> Le 06/06/2012 11:02, BILAL Mustapha a ?crit : > Hello, > > I am working to connect to a serial link (USB). In order to do that I > have to write this command: > > sudo ../../tools/stm32w/serialdump-linux -b115200 -d10000 (keeping in > mind dat*serialdump-linux*, which is in the path, is not a folder, > it's an executable) > > To get to the root, I have used: returncode = > subprocess.call(["/usr/bin/sudo", "/usr/bin/id"]) > > Then to connect to the USB through the command above I tried the > following: > > usb = > Popen(['../../tools/stm32w/serialdump-linux','-b115200','-d10000'], > stdout=PIPE, stdin=PIPE, stderr=STDOUT) > > usb.communicate()[0] > > But I am getting an error, it's considering that serialdump-linux as > a folder!! > > Any help how I can run the command? > > Many thanks > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor I resolved the issue, you can do the following: First method: args = ("sudo", "../../tools/stm32w/serialdump-linux", "-b115200", "-d10000") usb = Popen(args, stdout=PIPE, stdin=PIPE) usb.wait() usb.communicate()[0] Second method: cmd = 'sudo ../../tools/stm32w/serialdump-linux -b115200 -d10000' os.system(cmd) you can run it using cmd and os or using Popen if you need to use stdout and stdin in your application (which is my case) PS. -b115200 -d10000 are two options related to USB so you can change the whole command and use any option you would like to. Regards -------------- next part -------------- An HTML attachment was scrubbed... URL: From opher.lubzens at gmail.com Wed Jun 6 14:20:38 2012 From: opher.lubzens at gmail.com (Opher Lubzens) Date: Wed, 6 Jun 2012 15:20:38 +0300 Subject: [Tutor] Python 2.7 Static Version and Postgresql In-Reply-To: References: Message-ID: On Tue, May 29, 2012 at 9:26 PM, Opher Lubzens wrote: > > That actually might be workable- I'll have to look into it. I'll have > to understand them better then I do after a brief look in their > webpages and check whether they're compatible with our specific flavor > of OS, since both of these are Windows oriented according to their > pages, but this may be a good answer for what I need. > > Thank you for your help, > Opher Lubzens Just reporting how I solved this: Turns out that a static bundle will identify a built library if you copy the library dir to the same directory as the bundle. So I used this to copy a pg8000 library I built in Ubuntu into the same directory after disabling the SSL dependency, since our OS doesn't have some of the .so files that the ssl library needs, and my script won't use SSL connection anyway since it operates from within the server. Thanks for the help, Opher Lubzens From davestechshop at gmail.com Wed Jun 6 21:19:11 2012 From: davestechshop at gmail.com (Dave) Date: Wed, 6 Jun 2012 15:19:11 -0400 Subject: [Tutor] special attributes naming confusion Message-ID: I was reading some tutorial material on creating iterators. It shows the following example implementation of an iterator: class Reverse: """Iterator for looping over a sequence backwards.""" def __init__(self, data): self.data = data self.index = len(data) def __iter__(self): return self def next(self): if self.index == 0: raise StopIteration self.index = self.index - 1 return self.data[self.index] My question is how was I supposed to kinow that the function I call using the name iter() is implemented using the name __iter__()? Is there a rule that describes when I would implement an attribute name with leading and trailing double underscores, and then call it without those underscores? How many names like this exist in Python? Are these special cases or is there a general rule that leading and trailing double underscores get dropped when calling functions that were implemented with these names? I'm trying to understand the big picture as far as how Python works when it comes to this situation. Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From emailkgnow at gmail.com Wed Jun 6 21:29:27 2012 From: emailkgnow at gmail.com (Khalid Al-Ghamdi) Date: Wed, 6 Jun 2012 22:29:27 +0300 Subject: [Tutor] sqlite3 Question Message-ID: Hi All, hi Joel, Regarding sqlite3 practice code below: 1. import sqlite3 2. conn=sqlite3.connect("mydb2.db") 3. c=conn.cursor() 4. c.execute('create table family (Id integer primary key, name text, age integer)') 5. c.execute("insert into family values (1,'elham',32)") 6. c.execute('select * from family') 7. conn.commit() 8. for i in c: 9. print(i) Why is it that if I call the for statement in the shell it doesn't print the information more than once? I've found this to be true in other situations where cursor carries the data only once. So, how can i manipulate/use the data if I can only access it once? I have tried applying useing x=list(c), but it doesn't seem to work! Any guidance? Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From ramit.prasad at jpmorgan.com Wed Jun 6 21:30:27 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Wed, 6 Jun 2012 19:30:27 +0000 Subject: [Tutor] special attributes naming confusion In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF47412D84CFF@SCACMX008.exchad.jpmchase.net> > My question is how was I supposed to kinow that the function I call using the > name iter() is implemented using the name __iter__()? > > Is there a rule that describes when I would implement an attribute name with > leading and trailing double underscores, and then call it without those > underscores? How many names like this exist in Python? Are these special cases > or is there a general rule that leading and trailing double underscores get > dropped when calling functions that were implemented with these names? I'm > trying to understand the big picture as far as how Python works when it comes > to this situation. Thanks. They are listed here http://docs.python.org/reference/datamodel.html#specialnames Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 dohoang4093 at comcast.net Wed Jun 6 21:33:32 2012 From: dohoang4093 at comcast.net (dohoang4093 at comcast.net) Date: Wed, 6 Jun 2012 19:33:32 +0000 (UTC) Subject: [Tutor] Searching 2 Strings in A File In-Reply-To: <665692465.1422991.1339011160347.JavaMail.root@sz0045a.westchester.pa.mail.comcast.net> Message-ID: <168319527.1423046.1339011212076.JavaMail.root@sz0045a.westchester.pa.mail.comcast.net> i am writing a python script that will be invoked as follows: myScript.py and the script is as follwos: x = "Device " + sys.argv[1] + " restored the database" y = "Created connection to " + sys.argv[1] cmd_line = Popen(["egrep", "(x,y)", aLogFile], stdout=PIPE, stdin=PIPE, stderr=STDOUT) the above code does not work because it will look for x and y in aLogFile instead of looking for <"Device " + sys.argv[1] + " restored the database"> and <"Created connection to " + sys.argv[1]> Any hint is appreciated. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ramit.prasad at jpmorgan.com Wed Jun 6 21:36:35 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Wed, 6 Jun 2012 19:36:35 +0000 Subject: [Tutor] sqlite3 Question In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF47412D84D1D@SCACMX008.exchad.jpmchase.net> > > 1. import sqlite3 > 2. conn=sqlite3.connect("mydb2.db") > 3. c=conn.cursor() > 4. c.execute('create table family (Id integer primary key, name text, age > integer)') > 5. c.execute("insert into family values (1,'elham',32)") > 6. c.execute('select * from family') > 7. conn.commit() > 8. for i in c: > 9. print(i) > > Why is it that if I call the for statement in the shell it doesn't print the > information more than once? I've found this to be true in other situations > where cursor carries the data only once. So, how can i manipulate/use the data > if I can only access it once? I have tried applying useing x=list(c), but it > doesn't seem to work! The cursor only remembers the last action so you need to get the results before reusing the cursor. If you want to keep the data in memory you can use data = c.fetchall()# or c.fetchmany(num) or c.fetchone() Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 breamoreboy at yahoo.co.uk Wed Jun 6 21:40:13 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 06 Jun 2012 20:40:13 +0100 Subject: [Tutor] special attributes naming confusion In-Reply-To: References: Message-ID: On 06/06/2012 20:19, Dave wrote: > I was reading some tutorial material on creating iterators. It shows the > following example implementation of an iterator: > > class Reverse: > """Iterator for looping over a sequence backwards.""" > def __init__(self, data): > self.data = data > self.index = len(data) > def __iter__(self): > return self > def next(self): > if self.index == 0: > raise StopIteration > self.index = self.index - 1 > return self.data[self.index] > > > My question is how was I supposed to kinow that the function I call using > the name iter() is implemented using the name __iter__()? > > Is there a rule that describes when I would implement an attribute name > with leading and trailing double underscores, and then call it without > those underscores? How many names like this exist in Python? Are these > special cases or is there a general rule that leading and trailing double > underscores get dropped when calling functions that were implemented with > these names? I'm trying to understand the big picture as far as how Python > works when it comes to this situation. Thanks. > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Try this to start with http://docs.python.org/reference/datamodel.html#special-method-names. Note this is for Python 2.7.3, there may be differences in Python 3.x. -- Cheers. Mark Lawrence. From d at davea.name Wed Jun 6 21:46:40 2012 From: d at davea.name (Dave Angel) Date: Wed, 06 Jun 2012 15:46:40 -0400 Subject: [Tutor] Searching 2 Strings in A File In-Reply-To: <168319527.1423046.1339011212076.JavaMail.root@sz0045a.westchester.pa.mail.comcast.net> References: <168319527.1423046.1339011212076.JavaMail.root@sz0045a.westchester.pa.mail.comcast.net> Message-ID: <4FCFB3A0.402@davea.name> On 06/06/2012 03:33 PM, dohoang4093 at comcast.net wrote: > i am writing a python script that will be invoked as follows: > > myScript.py > > and the script is as follwos: > > x = "Device " + sys.argv[1] + " restored the database" > y = "Created connection to " + sys.argv[1] > > cmd_line = Popen(["egrep", "(x,y)", aLogFile], stdout=PIPE, stdin=PIPE, stderr=STDOUT) > > the above code does not work because it will look for x and y in aLogFile instead of > looking for <"Device " + sys.argv[1] + " restored the database"> and <"Created connection to " + sys.argv[1]> > Any hint is appreciated. > I'm not commenting on egrep and its parsing rules, but when you are passing a list to Popen(), each item is a string. Your second item is "(x,y)" when you want something like <"Device... You have the word "and" in there but I have no idea what you mean by it. Are there two arguments to egrep there, or one? I'll assume one for now. So build another variable called arg1, and make it right. The cmd_line will then just be cmd_line = Popen(["egrep", arg1, aLogFile], stdout=PIPE, stdin=PIPE, stderr=STDOUT) arg1 *might* just be something like arg1 = x+y but I seriously doubt it. You'll probably be adding quotes, and maybe even backslashes and other stuff to the mix. If you showed us exactly what you're expecting the commandline of egrep to look like, we might be able to come up with an answer. But you definitely don't want to try to do it all in one line. Pick something simple and get that working, then make it more complex till it meets your requirements. -- DaveA From davestechshop at gmail.com Wed Jun 6 21:47:09 2012 From: davestechshop at gmail.com (Dave) Date: Wed, 6 Jun 2012 15:47:09 -0400 Subject: [Tutor] special attributes naming confusion In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47412D84CFF@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF47412D84CFF@SCACMX008.exchad.jpmchase.net> Message-ID: On Wed, Jun 6, 2012 at 3:30 PM, Prasad, Ramit wrote: > > My question is how was I supposed to kinow that the function I call > using the > > name iter() is implemented using the name __iter__()? > > > > Is there a rule that describes when I would implement an attribute name > with > > leading and trailing double underscores, and then call it without those > > underscores? How many names like this exist in Python? Are these special > cases > > or is there a general rule that leading and trailing double underscores > get > > dropped when calling functions that were implemented with these names? > I'm > > trying to understand the big picture as far as how Python works when it > comes > > to this situation. Thanks. > > They are listed here > http://docs.python.org/reference/datamodel.html#specialnames > > Ramit > > Thank you. That's a good start. It appears to answer half my question. It tells me about special names like __new__, __init__, etc. And there is also mention of __iter__(self) on that page too. But I don't see any discussion of the convention regarding mappings from those names to the typical names used to call the functions in code. Unless I'm overlooking it, that page doesn't explain how to generalize the above example where calling the function by the name iter() actually calls the implementation named __iter__(). Are the leading and trailing double underscores simply dropped always? (It doesn't seem that simple because functions like __init__ are called behind the scenes.) -------------- next part -------------- An HTML attachment was scrubbed... URL: From davestechshop at gmail.com Wed Jun 6 21:59:03 2012 From: davestechshop at gmail.com (Dave) Date: Wed, 6 Jun 2012 15:59:03 -0400 Subject: [Tutor] special attributes naming confusion In-Reply-To: References: Message-ID: On Wed, Jun 6, 2012 at 3:40 PM, Mark Lawrence wrote: > On 06/06/2012 20:19, Dave wrote: > >> I was reading some tutorial material on creating iterators. It shows the >> following example implementation of an iterator: >> >> class Reverse: >> """Iterator for looping over a sequence backwards.""" >> def __init__(self, data): >> self.data = data >> self.index = len(data) >> def __iter__(self): >> return self >> def next(self): >> if self.index == 0: >> raise StopIteration >> self.index = self.index - 1 >> return self.data[self.index] >> >> >> My question is how was I supposed to kinow that the function I call using >> the name iter() is implemented using the name __iter__()? >> >> Is there a rule that describes when I would implement an attribute name >> with leading and trailing double underscores, and then call it without >> those underscores? How many names like this exist in Python? Are these >> special cases or is there a general rule that leading and trailing double >> underscores get dropped when calling functions that were implemented with >> these names? I'm trying to understand the big picture as far as how Python >> works when it comes to this situation. Thanks. >> >> > Try this to start with http://docs.python.org/**reference/datamodel.html#* > *special-method-names. > Note this is for Python 2.7.3, there may be differences in Python 3.x. > > -- > Actually, I think I'm getting it now... as I read more of this page I see that there is no single generalization. These are indeed all special cases. But the documentation does appear to be incomplete. It leaves out the mapping to the name or symbol that should be used to call the special function in some cases. In particular, in the case of __iter(self), which is one of the first ones I looked at, it doesn't mention that this is usually called via iter(). It does mention how it would be called for mapping containers, however (i.e., iterkeys()). object.__iter__(*self*) This method is called when an iterator is required for a container. This method should return a new iterator object that can iterate over all the objects in the container. For mappings, it should iterate over the keys of the container, and should also be made available as the method iterkeys(). But as I read more, I see that much of the documentation does mention how these special method names are called. For example: object.__lt__(*self*, *other*) object.__le__(*self*, *other*)? object.__eq__(*self*, *other*) object.__ne__(*self*, *other*) object.__gt__( *self*, *other*) object.__ge__(*self*, *other*) New in version 2.1. These are the so-called ?rich comparison? methods, and are called for comparison operators in preference to __cmp__()below. The correspondence between operator symbols and method names is as follows: xy call x.__ne__(y), x>y calls x.__gt__(y), and x>=ycalls x.__ge__(y). I think there is enough info at this page to answer my question as well as I need it answered right now. Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ramit.prasad at jpmorgan.com Wed Jun 6 21:38:45 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Wed, 6 Jun 2012 19:38:45 +0000 Subject: [Tutor] Searching 2 Strings in A File In-Reply-To: <168319527.1423046.1339011212076.JavaMail.root@sz0045a.westchester.pa.mail.comcast.net> References: <665692465.1422991.1339011160347.JavaMail.root@sz0045a.westchester.pa.mail.comcast.net> <168319527.1423046.1339011212076.JavaMail.root@sz0045a.westchester.pa.mail.comcast.net> Message-ID: <5B80DD153D7D744689F57F4FB69AF47412D84D30@SCACMX008.exchad.jpmchase.net> > x = "Device " + sys.argv[1] + " restored the database" > y = "Created connection to " + sys.argv[1] > > cmd_line = Popen(["egrep", "(x,y)", aLogFile], stdout=PIPE, stdin=PIPE, > stderr=STDOUT) > > the above code does not work because it will look for x and y in aLogFile > instead of > looking for <"Device " + sys.argv[1] + " restored the database"> and <"Created > connection to " + sys.argv[1]> > Any hint is appreciated. Try pattern = "({0},{1})".format(x, y) cmd_line = Popen(["egrep", pattern, aLogFile], stdout=PIPE, stdin=PIPE, stderr=STDOUT) Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 ramit.prasad at jpmorgan.com Wed Jun 6 21:49:58 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Wed, 6 Jun 2012 19:49:58 +0000 Subject: [Tutor] Searching 2 Strings in A File References: <665692465.1422991.1339011160347.JavaMail.root@sz0045a.westchester.pa.mail.comcast.net> <168319527.1423046.1339011212076.JavaMail.root@sz0045a.westchester.pa.mail.comcast.net> Message-ID: <5B80DD153D7D744689F57F4FB69AF47412D84D9B@SCACMX008.exchad.jpmchase.net> > > x = "Device " + sys.argv[1] + " restored the database" > > y = "Created connection to " + sys.argv[1] > > > > cmd_line = Popen(["egrep", "(x,y)", aLogFile], stdout=PIPE, stdin=PIPE, > > stderr=STDOUT) > > > > the above code does not work because it will look for x and y in aLogFile > > instead of > > looking for <"Device " + sys.argv[1] + " restored the database"> and > <"Created > > connection to " + sys.argv[1]> > > Any hint is appreciated. > > Try > > pattern = "({0},{1})".format(x, y) > cmd_line = Popen(["egrep", pattern, aLogFile], stdout=PIPE, stdin=PIPE, > stderr=STDOUT) Also, I think it should be '|' in pattern and not ','. Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 davestechshop at gmail.com Wed Jun 6 22:32:45 2012 From: davestechshop at gmail.com (Dave) Date: Wed, 6 Jun 2012 16:32:45 -0400 Subject: [Tutor] special attributes naming confusion In-Reply-To: References: Message-ID: I'm not sure where this comment belongs, but I want to share my perspective on the documentation of these special method names. In the following section there is an inconsistency which could be confusing to someone just learning Python (e.g., me). In the sentence on implementing custom mapping objects, the recommended method names are listed as the short calling names: eys(), values(), items(), etc. But, in contrast, in the sentence on implementing sequence types, the recommended method names are listed as the double underscore internal implementation names: __add__(), __radd__(), __iadd__(), __mul__(), etc. Here's the section of the documentation with this inconsistency. I think it would help to use one or the other of these pairs (calling name vs. internal implementation name) consistently in this section. 3.4.6. Emulating container types? The following methods can be defined to implement container objects. Containers usually are sequences (such as lists or tuples) or mappings (like dictionaries), but can represent other containers as well. The first set of methods is used either to emulate a sequence or to emulate a mapping; the difference is that for a sequence, the allowable keys should be the integers *k* for which 0 <= k < N where *N* is the length of the sequence, or slice objects, which define a range of items. (For backwards compatibility, the method __getslice__()(see below) can also be defined to handle simple, but not extended slices.) It is also recommended that mappings provide the methods keys(), values(), items(), has_key(), get(), clear(), setdefault(), iterkeys(), itervalues(), iteritems(), pop(), popitem(), copy(), and update() behaving similar to those for Python?s standard dictionary objects. The UserDictmodule provides a DictMixin class to help create those methods from a base set of __getitem__(), __setitem__(), __delitem__(), and keys(). Mutable sequences should provide methods append(), count(), index(), extend(), insert(), pop(), remove(), reverse() and sort(), like Python standard list objects. Finally, sequence types should implement addition (meaning concatenation) and multiplication (meaning repetition) by defining the methods __add__(), __radd__() , __iadd__() , __mul__() , __rmul__() and __imul__() described below; they should not define __coerce__()or other numerical operators. It is recommended that both mappings and sequences implement the __contains__()method to allow efficient use of the in operator; for mappings, in should be equivalent of has_key(); for sequences, it should search through the values. It is further recommended that both mappings and sequences implement the __iter__()method to allow efficient iteration through the container; for mappings, __iter__() should be the same as iterkeys(); for sequences, it should iterate through the values. This is in addition to some missing documentation regarding mention how some of the special method names should be conveniently called in code. (See below.) On Wed, Jun 6, 2012 at 3:59 PM, Dave wrote: > > > On Wed, Jun 6, 2012 at 3:40 PM, Mark Lawrence wrote: > >> On 06/06/2012 20:19, Dave wrote: >> >>> I was reading some tutorial material on creating iterators. It shows the >>> following example implementation of an iterator: >>> >>> class Reverse: >>> """Iterator for looping over a sequence backwards.""" >>> def __init__(self, data): >>> self.data = data >>> self.index = len(data) >>> def __iter__(self): >>> return self >>> def next(self): >>> if self.index == 0: >>> raise StopIteration >>> self.index = self.index - 1 >>> return self.data[self.index] >>> >>> >>> My question is how was I supposed to kinow that the function I call using >>> the name iter() is implemented using the name __iter__()? >>> >>> Is there a rule that describes when I would implement an attribute name >>> with leading and trailing double underscores, and then call it without >>> those underscores? How many names like this exist in Python? Are these >>> special cases or is there a general rule that leading and trailing double >>> underscores get dropped when calling functions that were implemented with >>> these names? I'm trying to understand the big picture as far as how >>> Python >>> works when it comes to this situation. Thanks. >>> >>> >> Try this to start with http://docs.python.org/**reference/datamodel.html# >> **special-method-names. >> Note this is for Python 2.7.3, there may be differences in Python 3.x. >> >> -- >> > > Actually, I think I'm getting it now... as I read more of this page I see > that there is no single generalization. These are indeed all special cases. > > But the documentation does appear to be incomplete. It leaves out the > mapping to the name or symbol that should be used to call the special > function in some cases. In particular, in the case of __iter(self), which > is one of the first ones I looked at, it doesn't mention that this is > usually called via iter(). It does mention how it would be called for > mapping containers, however (i.e., iterkeys()). > object.__iter__(*self*) > > This method is called when an iterator is required for a container. This > method should return a new iterator object that can iterate over all the > objects in the container. For mappings, it should iterate over the keys of > the container, and should also be made available as the method iterkeys(). > > But as I read more, I see that much of the documentation does mention how > these special method names are called. For example: > > object.__lt__(*self*, *other*) object.__le__(*self*, *other*)? > object.__eq__(*self*, *other*) object.__ne__(*self*, *other*) object. > __gt__(*self*, *other*) object.__ge__(*self*, *other*) > > New in version 2.1. > > These are the so-called ?rich comparison? methods, and are called for > comparison operators in preference to __cmp__()below. The correspondence between operator symbols and method names is as > follows: x x.__eq__(y), x!=y and x<>y call x.__ne__(y), x>y calls x.__gt__(y), and > x>=y calls x.__ge__(y). > I think there is enough info at this page to answer my question as well as > I need it answered right now. Thanks. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From malaclypse2 at gmail.com Wed Jun 6 22:45:05 2012 From: malaclypse2 at gmail.com (Jerry Hill) Date: Wed, 6 Jun 2012 16:45:05 -0400 Subject: [Tutor] special attributes naming confusion In-Reply-To: References: Message-ID: On Wed, Jun 6, 2012 at 4:32 PM, Dave wrote: > I'm not sure where this comment belongs, but I want to share my perspective > on the documentation of these special method names. In the following section > there is an inconsistency which could be confusing to someone just learning > Python (e.g., me). > > In the sentence on implementing custom mapping objects, the recommended > method names are listed as the short calling names: eys(), values(), > items(), etc. > > But, in contrast, in the sentence on implementing sequence types, the > recommended method names are listed as the double underscore internal > implementation names: __add__(), __radd__(), __iadd__(), __mul__(), etc. > > Here's the section of the documentation with this inconsistency. I think it > would help to use one or the other of these pairs (calling name vs. internal > implementation name) consistently in this section. Those aren't pairs, where you can implement either one. You have to implement ALL of those methods to correctly emulate one of the built in container classes. The difference is that the "normal" method names (the ones without double underscores) are meant to be called directly on an instance of your class. The double underscored names are called by the internals of python, often in a variety of different places. For instance, the __iter__() method is called by the iter() built-in. It can also called by any other piece of code that needs to acquire an iterator from a generic container object. There is no list of all such pieces of code that could ever call the __iter__() method of your object. Jerry From ramit.prasad at jpmorgan.com Wed Jun 6 23:13:35 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Wed, 6 Jun 2012 21:13:35 +0000 Subject: [Tutor] Searching 2 Strings in A File In-Reply-To: <63935675.1428309.1339017023327.JavaMail.root@sz0045a.westchester.pa.mail.comcast.net> References: <168319527.1423046.1339011212076.JavaMail.root@sz0045a.westchester.pa.mail.comcast.net> <63935675.1428309.1339017023327.JavaMail.root@sz0045a.westchester.pa.mail.comcast.net> Message-ID: <5B80DD153D7D744689F57F4FB69AF47412D84E70@SCACMX008.exchad.jpmchase.net> Please respond back to the list (so others know what worked and that the problem is solved). :) > > hi all, > > thanks a lot for your quick response. > > Dave, actually it's a 2 arguments.? Sorry, i did not make it clear in my > question.? I used Ramit's hint and it worked.? The code should be as follows: > > pattern = "({0}|{1})".format(x,y) > cmd_line = Popen(["egrep", pattern, aLogFile], stdout=PIPE, stdin=PIPE, > stderr=STDOUT) > > Regards, > > Do Nguyen Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 ramit.prasad at jpmorgan.com Wed Jun 6 23:23:50 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Wed, 6 Jun 2012 21:23:50 +0000 Subject: [Tutor] special attributes naming confusion In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF47412D84EA1@SCACMX008.exchad.jpmchase.net> > For instance, the __iter__() method is called by the iter() built-in. > It can also called by any other piece of code that needs to acquire an > iterator from a generic container object. There is no list of all > such pieces of code that could ever call the __iter__() method of your A lot of times iter()/__iter__() are used implicitly and not explicitly. So knowing that iter() specifically calls __iter__ is not as useful as knowing *when* to implement __iter__(). For example, for x in obj: # this is an implicit call; works for collections or instance that # implements __iter__() pass Knowing when the special methods are used is more important than knowing the "mapping" or what built-in calls it. Most of the times I do not even use built-ins like iter() explicitly--the exception being len(). Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 steve at pearwood.info Thu Jun 7 00:37:38 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 07 Jun 2012 08:37:38 +1000 Subject: [Tutor] special attributes naming confusion In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47412D84EA1@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF47412D84EA1@SCACMX008.exchad.jpmchase.net> Message-ID: <4FCFDBB2.3070808@pearwood.info> Prasad, Ramit wrote: >> For instance, the __iter__() method is called by the iter() built-in. >> It can also called by any other piece of code that needs to acquire an >> iterator from a generic container object. There is no list of all >> such pieces of code that could ever call the __iter__() method of your > > A lot of times iter()/__iter__() are used implicitly and not explicitly. > So knowing that iter() specifically calls __iter__ is not as useful as > knowing *when* to implement __iter__(). For example, > > for x in obj: > # this is an implicit call; works for collections or instance that > # implements __iter__() > pass Not just __iter__. For loops work over anything that implements the iterator protocol or the sequence protocol. That is, for loops first try to build an iterator by calling __iter__, and if that fails they try the sequence protocol obj[0], obj[1], obj[2], ... As a general rule, except when you are writing your own classes and defining __DoubleUNDERscore__ methods, you should (almost) never need to explicitly use such dunder methods. -- Steven From ramit.prasad at jpmorgan.com Thu Jun 7 00:54:10 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Wed, 6 Jun 2012 22:54:10 +0000 Subject: [Tutor] special attributes naming confusion In-Reply-To: <4FCFDBB2.3070808@pearwood.info> References: <5B80DD153D7D744689F57F4FB69AF47412D84EA1@SCACMX008.exchad.jpmchase.net> <4FCFDBB2.3070808@pearwood.info> Message-ID: <5B80DD153D7D744689F57F4FB69AF47412D8518A@SCACMX008.exchad.jpmchase.net> > That is, for loops first try to build an iterator by calling __iter__, and if > that fails they try the sequence protocol obj[0], obj[1], obj[2], ... So...I could instead write __getitem__ for the same effect? Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 steve at pearwood.info Thu Jun 7 01:06:31 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 07 Jun 2012 09:06:31 +1000 Subject: [Tutor] Joining all strings in stringList into one string In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47412D7A2A9@SCACMX008.exchad.jpmchase.net> References: <4FC9FB7C.2070605@gmail.com> <4FCA2333.3080302@pearwood.info> <5B80DD153D7D744689F57F4FB69AF47412D7A2A9@SCACMX008.exchad.jpmchase.net> Message-ID: <4FCFE277.7050407@pearwood.info> Prasad, Ramit wrote: >> But more importantly, some years ago (Python 2.4, about 8 years ago?) the >> Python developers found a really neat trick that they can do to optimize >> string concatenation so it doesn't need to repeatedly copy characters over >> and >> over and over again. I won't go into details, but the thing is, this trick >> works well enough that repeated concatenation is about as fast as the join >> method MOST of the time. > > I would like to learn a bit more about the trick if you have a > reference handy. I have no intention of using it, but it sounds > interesting and might teach me more about Python internals. In a nutshell, CPython identifies cases like: mystr = mystr + otherstr mystr += otherstr where mystr is not used in any other place, and if possible, resizes mystr in place and appends otherstr, rather than copying both to a new string object. The "if possible" hides a lot of technical detail, which is why the optimization can fail on some platforms while working on others. See this painful discussion trying to debug httplib slowness: http://mail.python.org/pipermail/python-dev/2009-August/091125.html After many dead-ends and red herrings, somebody spotted the problem: http://mail.python.org/pipermail/python-dev/2009-September/091582.html ending with GvR admitting that it was an embarrassment that repeated string concatenation had survived in the standard library for so long. The author even knew it was slow because he put a comment warning about it! Here is the middle of the discussion adding the optimization back in 2004: http://mail.python.org/pipermail/python-dev/2004-August/046695.html which talks about the possibility of other implementations doing something similar. You can find the beginning of the discussion yourself :) And here is a good description of the optimization itself: http://utcc.utoronto.ca/~cks/space/blog/python/ExaminingStringConcatOpt -- Steven From steve at pearwood.info Thu Jun 7 01:28:07 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 07 Jun 2012 09:28:07 +1000 Subject: [Tutor] special attributes naming confusion In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47412D8518A@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF47412D84EA1@SCACMX008.exchad.jpmchase.net> <4FCFDBB2.3070808@pearwood.info> <5B80DD153D7D744689F57F4FB69AF47412D8518A@SCACMX008.exchad.jpmchase.net> Message-ID: <4FCFE787.7000704@pearwood.info> Prasad, Ramit wrote: >> That is, for loops first try to build an iterator by calling __iter__, and if >> that fails they try the sequence protocol obj[0], obj[1], obj[2], ... > > So...I could instead write __getitem__ for the same effect? Er, no... __getitem__ and __iter__ do very different things. __getitem__ returns individual items, and __iter__ returns an iterator object which, when passed to the next() builtin, returns the next item. I trust you aren't calling __iter__ explicitly! Always call it from the built-in function iter(), or better still, don't call it at all and let the for loop do so. For the record, here is pseudo-code emulating how Python for-loops work. "for x in obj: do_something(x)" becomes something similar to this: try: # Try the iterator protocol first. it = iter(x) except TypeError: # Fall back on the old-fashioned sequence protocol. counter = 0 while True: try: x = obj[counter] except IndexError: # We must be past the end of the sequence. del counter break do_something(x) counter += 1 else: # Iterator protocol. while True: try: x = next(it) except StopIteration: # We're done. del it break do_something(x) -- Steven From steve at pearwood.info Thu Jun 7 01:36:38 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 07 Jun 2012 09:36:38 +1000 Subject: [Tutor] special attributes naming confusion In-Reply-To: References: Message-ID: <4FCFE986.5040708@pearwood.info> Dave wrote: > I was reading some tutorial material on creating iterators. It shows the > following example implementation of an iterator: > > class Reverse: > """Iterator for looping over a sequence backwards.""" > def __init__(self, data): > self.data = data > self.index = len(data) > def __iter__(self): > return self > def next(self): > if self.index == 0: > raise StopIteration > self.index = self.index - 1 > return self.data[self.index] > > > My question is how was I supposed to kinow that the function I call using > the name iter() is implemented using the name __iter__()? By reading the Fine Manual :) Unless you are defining your own classes, you (almost) never need to care about __DoubleUNDERscore__ (dunder) methods and attributes. The main exception I can think of is the idiom for writing Python scripts: if __name__ == '__main__': # Running as a script. main() # Otherwise we're imported as a library. Even when defining your own classes, simply define the methods you care about, such as __init__ for instance initialisation, but don't call __init__ directly. Python will call it for you. (Why keep a dog and bark yourself?) > Is there a rule that describes when I would implement an attribute name > with leading and trailing double underscores, and then call it without > those underscores? How many names like this exist in Python? Are these > special cases or is there a general rule that leading and trailing double > underscores get dropped when calling functions that were implemented with > these names? I'm trying to understand the big picture as far as how Python > works when it comes to this situation. Thanks. The big picture is that dunder methods are used for customizing operators and functions, but you really need to check the manual to see which operator maps to which dunder method. -- Steven From alan.gauld at btinternet.com Thu Jun 7 01:47:23 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 07 Jun 2012 00:47:23 +0100 Subject: [Tutor] special attributes naming confusion In-Reply-To: References: Message-ID: On 06/06/12 21:32, Dave wrote: > I'm not sure where this comment belongs, but I want to share my > perspective on the documentation of these special method names. In the > following section there is an inconsistency which could be confusing to > someone just learning Python (e.g., me). In general someone "just learning python" doesn't need to know about special names, they just use the operations they define. The only exception being __init__() in class definitions. These methods are really only of interest to someone who wants to define their own abstract data type and make it look like a built in type(*). In the vast majority of beginner scenarios that is not needed or can be faked without use of them. Even where it is needed the special names can be discovered on a "need to know" basis. By the time that need arises the programmer is hopefully so familiar with Python implementation style(use of dictionaries for namespaces etc) that the special name trick will be no great surprise and in fact come as an "aha!" moment of understanding. So mostly I'd say just ignore them for now, you almost certainly don;t need them. And if you do you probably aren't "just learning" any more! :-) (*) Someone from a Lisp, Smalltalk or C++ background might stumble on them earlier because these languages support operator overloading and so the beginner may wonder about how to do it in Python. Most other languages don't use the concept so it won't even occur to ask! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu Jun 7 01:53:09 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 07 Jun 2012 00:53:09 +0100 Subject: [Tutor] Searching 2 Strings in A File In-Reply-To: <168319527.1423046.1339011212076.JavaMail.root@sz0045a.westchester.pa.mail.comcast.net> References: <665692465.1422991.1339011160347.JavaMail.root@sz0045a.westchester.pa.mail.comcast.net> <168319527.1423046.1339011212076.JavaMail.root@sz0045a.westchester.pa.mail.comcast.net> Message-ID: On 06/06/12 20:33, dohoang4093 at comcast.net wrote: > i am writing a python script that will be invoked as follows: > cmd_line = Popen(["egrep", "(x,y)", aLogFile], stdout=PIPE, stdin=PIPE, > stderr=STDOUT) Assuming you really want to use egrep, why? You could process the file directly in Python searching for your strings (using the re module if need be). Using Popen makes sense if the command is doing a lot of work or is very CPU intensive. But for file processing its probably as easy to just use Python to scan the log files directly. But if you are really doing something more complex I see that others have answered the Popen() question... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From redacted@example.com Thu Jun 7 06:29:20 2012 From: redacted@example.com (Alexander Quest) Date: Wed, 6 Jun 2012 21:29:20 -0700 Subject: [Tutor] Notepad++ question Message-ID: Hey all; my question is regarding editing Python code in Notepad++. When I run this piece of code in Notepad++: def fix_start(s): var1 = s[0] var2 = "*" var3 = s.replace(var1, var2) return var3 I get an indentation error, which reads: File "C:\google-python-exercises\google-python-exercises\basic>string1.py line 56 var2 = "*" ^ IndentationError: unexpected indent The thing is that in Notepad++, that code does not appear with an indentation where var2 is. It appears like this: def fix_start(s): var1 = s[0] var2 = "*" var3 = s.replace(var1, var2) return var3 but when I copy and paste it, it pastes with an indentation where var2 is, which is what I think is causing the error. The code runs fine if I just use IDLE. I am doing Google's python exercises, and they recommended I edit the settings on Notepad++ to indent 2 spaces upon a tab, this being the convention at Google. Does anyone know what the deal is here? Also, I am wondering why use Notepad++ or other such programs when IDLE seems to be fine for writing code. Thanks. -Alex -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Thu Jun 7 06:50:45 2012 From: d at davea.name (Dave Angel) Date: Thu, 07 Jun 2012 00:50:45 -0400 Subject: [Tutor] Notepad++ question In-Reply-To: References: Message-ID: <4FD03325.5020204@davea.name> On 06/07/2012 12:29 AM, Alexander Quest wrote: > Hey all; my question is regarding editing Python code in Notepad++. When I > run this piece of code in Notepad++: > > def fix_start(s): > var1 = s[0] > var2 = "*" > var3 = s.replace(var1, var2) > > return var3 > > > I get an indentation error, which reads: > > > File "C:\google-python-exercises\google-python-exercises\basic>string1.py > line 56 > var2 = "*" > ^ > IndentationError: unexpected indent > > > The thing is that in Notepad++, that code does not appear with an > indentation where var2 is. It appears like this: > > def fix_start(s): > var1 = s[0] > var2 = "*" > var3 = s.replace(var1, var2) > > return var3 > > but when I copy and paste it, it pastes with an indentation where var2 is, > which is what I think is causing the error. The code runs fine if I just > use IDLE. I am doing Google's python exercises, and they recommended I edit > the settings on Notepad++ to indent 2 spaces upon a tab, this being the > convention at Google. Does anyone know what the deal is here? Also, I am > wondering why use Notepad++ or other such programs when IDLE seems to be > fine for writing code. Thanks. > > -Alex > > I can't tell you what settings to use in Notepad++, but what you want is either for tabs or spaces to be used for all indenting in a source file, and never a mixture. I think if you view your file in a hex viewer, you'll see an invisible tab character tucked on that one line. My opinion is to stick entirely to spaces. 30 years ago, when i first faced this problem, I came up with what seems to be the best answer. The editor should not make any connection between the keyboard button called tab and the file character hex(09). When you use the tab key to line up columns, the editor should insert an appropriate number of spaces to make it come out right. And if you subsequently want to convert all the leading characters into one or more tabs followed by one or more spaces, that should be done to the whole file at once. Others think one should use tab characters exclusively for indenting. While there are some conceptual advantages to that, the biggest downside is that very few editor/viewers make the tab characters visible. I used such an editor decades ago, and it solved most of the problems. As for IDLE, I've never used it, so I don't know it's limitations. -- DaveA From alan.gauld at btinternet.com Thu Jun 7 09:41:02 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 07 Jun 2012 08:41:02 +0100 Subject: [Tutor] Notepad++ question In-Reply-To: References: Message-ID: On 07/06/12 05:29, Alexander Quest wrote: > Hey all; my question is regarding editing Python code in Notepad++. When > I run this piece of code in Notepad++: > > def fix_start(s): > var1 = s[0] > var2 = "*" > var3 = s.replace(var1, var2) > > return var3 > > > I get an indentation error, which reads: > > > File "C:\google-python-exercises\google-python-exercises\basic>string1.py > line 56 > var2 = "*" > ^ > IndentationError: unexpected indent > > > The thing is that in Notepad++, that code does not appear with an > indentation where var2 is. It appears like this: This is almost certainly down to mixing tabs and spaces. You can mix them in a file but not within a block. (And even then I would recommend sticking to just one style) Some editors make it worse by autoindenting with a mixture of tabs and spaces which is fine for C or Java but no good for Python... I don't know Notepad++ so can't comment on it. > is here? Also, I am wondering why use Notepad++ or other such programs > when IDLE seems to be fine for writing code. Thanks. IDLE is fine for beginners. It is less powerful than other text editors such as vim or emacs or Eclipse or even Scite. But that power is mainly useful when dealing with multi-file projects or large files. But IDLE is fine for most Python tasks. If you are happy with IDLE use IDLE. Some of the missing features that I like are: - incremental search - navigate to a character (t or f) - rectangular cut n paste - split screens vertically - integration with the OS - goto-function definition - macros But editor choice is very personal, some like a simple editor some like all the bells and whistles possible. It's up to you. Find one 9or several!) that you like and use it. But don't let choosing an editor get in the way of learning Python, they are two separate things. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From marc.tompkins at gmail.com Thu Jun 7 09:52:57 2012 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Thu, 7 Jun 2012 00:52:57 -0700 Subject: [Tutor] Notepad++ question In-Reply-To: References: Message-ID: On Thu, Jun 7, 2012 at 12:41 AM, Alan Gauld wrote: > This is almost certainly down to mixing tabs and spaces. > You can mix them in a file but not within a block. (And > even then I would recommend sticking to just one style) > Some editors make it worse by autoindenting with a mixture > of tabs and spaces which is fine for C or Java but no > good for Python... I don't know Notepad++ so can't comment > on it. > > In Notepad++, select Settings/Preferences. There's a tab called "Language Menu/Tab Settings" (they've put the two things on one tab to save space; the tab settings are on the right side.) You'll find a setting for "Tab size"; the default is 4 - and also a checkbox for "Replace by space", which is NOT checked by default. If you're going to be writing Python in Notepad++, you should _definitely_ check that box. Click "Close", and off you go! -------------- next part -------------- An HTML attachment was scrubbed... URL: From jtkbishop at gmail.com Thu Jun 7 10:18:31 2012 From: jtkbishop at gmail.com (Jonathan Bishop) Date: Thu, 7 Jun 2012 18:18:31 +1000 Subject: [Tutor] Notepad++ question In-Reply-To: References: Message-ID: Hi, as a Python beginner myself, I started out using Geany IDE. It was pretty cool editor but it caused me indentation hell! Since then I have moved to eclipse with the pydev plugin and found it much friendlier when dealing with indentation problems. But each to there own! Notepad!! is a sweet editor, I wish there was a linux version but I can't recommend Eclipse enough for beginners. Regards, Jon. On Thu, Jun 7, 2012 at 5:52 PM, Marc Tompkins wrote: > On Thu, Jun 7, 2012 at 12:41 AM, Alan Gauld wrote: > >> This is almost certainly down to mixing tabs and spaces. >> You can mix them in a file but not within a block. (And >> even then I would recommend sticking to just one style) >> Some editors make it worse by autoindenting with a mixture >> of tabs and spaces which is fine for C or Java but no >> good for Python... I don't know Notepad++ so can't comment >> on it. >> >> > In Notepad++, select Settings/Preferences. > There's a tab called "Language Menu/Tab Settings" (they've put the two > things on one tab to save space; the tab settings are on the right side.) > You'll find a setting for "Tab size"; the default is 4 - and also a > checkbox for "Replace by space", which is NOT checked by default. If > you're going to be writing Python in Notepad++, you should _definitely_ > check that box. > Click "Close", and off you go! > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kala586 at gmail.com Thu Jun 7 11:59:08 2012 From: kala586 at gmail.com (kala Vinay) Date: Thu, 7 Jun 2012 15:29:08 +0530 Subject: [Tutor] Tutor Digest, Vol 100, Issue 22 In-Reply-To: References: Message-ID: Dear All, credativ India specialized in open source, conducting a two days course on Python Programming on 27th and 28th June 2012. Interested candidates can contact: training at credativ.in Regards, Vijayakala -------------- next part -------------- An HTML attachment was scrubbed... URL: From brad.hudson at gmail.com Thu Jun 7 14:09:51 2012 From: brad.hudson at gmail.com (Brad Hudson) Date: Thu, 7 Jun 2012 07:09:51 -0500 Subject: [Tutor] Notepad++ question In-Reply-To: References: Message-ID: I don't use notepad or notepad++, so I don't know the settings. However, if you're familiar with vi/vim, you can download the CLI from www.vim.org for almost any flavor OS and it works quite well on Windows. BTW: you can customize your settings to use spaces instead of tabs, among others, which is usually a source of these types of indentation errors. On Wed, Jun 6, 2012 at 11:29 PM, Alexander Quest wrote: > Hey all; my question is regarding editing Python code in Notepad++. When I > run this piece of code in Notepad++: > > def fix_start(s): > var1 = s[0] > var2 = "*" > var3 = s.replace(var1, var2) > > return var3 > > > I get an indentation error, which reads: > > > File "C:\google-python-exercises\google-python-exercises\basic>string1.py > line 56 > var2 = "*" > ^ > IndentationError: unexpected indent > > > The thing is that in Notepad++, that code does not appear with an > indentation where var2 is. It appears like this: > > def fix_start(s): > var1 = s[0] > var2 = "*" > var3 = s.replace(var1, var2) > > return var3 > > but when I copy and paste it, it pastes with an indentation where var2 is, > which is what I think is causing the error. The code runs fine if I just > use IDLE. I am doing Google's python exercises, and they recommended I edit > the settings on Notepad++ to indent 2 spaces upon a tab, this being the > convention at Google. Does anyone know what the deal is here? Also, I am > wondering why use Notepad++ or other such programs when IDLE seems to be > fine for writing code. Thanks. > > -Alex > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From Michael at shamirlens.co.uk Thu Jun 7 17:51:49 2012 From: Michael at shamirlens.co.uk (Michael M Mason) Date: Thu, 7 Jun 2012 15:51:49 +0000 Subject: [Tutor] Notepad++ question In-Reply-To: References: Message-ID: <5378B081D0A21C45A6135E92E182BD7F810387E8@Mail1-Shamir.shamir.org.il> Marc Tompkins wrote on 07 June 2012 at 08:53:- > In Notepad++, select Settings/Preferences.? > There's a tab called "Language Menu/Tab Settings" (they've put the two things on > one tab to save space; the tab settings are on the right side.)? You'll find a > setting for "Tab size"; the default is 4 - and also a checkbox for "Replace by space", > which is NOT checked by default.? If you're going to be writing Python in Notepad++, > you should _definitely_ check that box.? > Click "Close", and off you go! There is also an option to make tab characters and space characters visible. In the 'View' menu, select 'Show Symbol' and then make sure the 'Show White Space and TAB' option is ticked. That makes it very easy to see where tabs and spaces are being used. -- Michael This mail was sent via Mail-SeCure System. ************************************************************************************ This footnote confirms that this email message has been scanned by PineApp Mail-SeCure for the presence of malicious code, vandals & computer viruses. ************************************************************************************ From emile at fenx.com Thu Jun 7 18:11:26 2012 From: emile at fenx.com (Emile van Sebille) Date: Thu, 07 Jun 2012 09:11:26 -0700 Subject: [Tutor] special attributes naming confusion In-Reply-To: <4FCFE787.7000704@pearwood.info> References: <5B80DD153D7D744689F57F4FB69AF47412D84EA1@SCACMX008.exchad.jpmchase.net> <4FCFDBB2.3070808@pearwood.info> <5B80DD153D7D744689F57F4FB69AF47412D8518A@SCACMX008.exchad.jpmchase.net> <4FCFE787.7000704@pearwood.info> Message-ID: On 6/6/2012 4:28 PM Steven D'Aprano said... > Prasad, Ramit wrote: >>> That is, for loops first try to build an iterator by calling >>> __iter__, and if >>> that fails they try the sequence protocol obj[0], obj[1], obj[2], ... >> >> So...I could instead write __getitem__ for the same effect? > > > Er, no... __getitem__ and __iter__ do very different things. __getitem__ > returns individual items, and __iter__ returns an iterator object which, > when passed to the next() builtin, returns the next item. I'd say it depends on what you need when you say for the same effect. This is the same effect for some definition thereof: class Iterable: def __init__(self,count): self.maxitems=count def result(self,idx): return idx**idx def __getitem__(self,idx): if idx References: <5B80DD153D7D744689F57F4FB69AF47412D84EA1@SCACMX008.exchad.jpmchase.net> <4FCFDBB2.3070808@pearwood.info> <5B80DD153D7D744689F57F4FB69AF47412D8518A@SCACMX008.exchad.jpmchase.net> <4FCFE787.7000704@pearwood.info> Message-ID: <5B80DD153D7D744689F57F4FB69AF47412D86C93@SCACMX008.exchad.jpmchase.net> > >> That is, for loops first try to build an iterator by calling __iter__, and > if > >> that fails they try the sequence protocol obj[0], obj[1], obj[2], ... > > > > So...I could instead write __getitem__ for the same effect? > > > Er, no... __getitem__ and __iter__ do very different things. __getitem__ > returns individual items, and __iter__ returns an iterator object which, when > passed to the next() builtin, returns the next item. > > I trust you aren't calling __iter__ explicitly! Always call it from the > built-in function iter(), or better still, don't call it at all and let the > for loop do so. > I never call __iter__ and rarely call iter; usually I just let Python handle all that. > For the record, here is pseudo-code emulating how Python for-loops work. > "for x in obj: do_something(x)" becomes something similar to this: > > try: > # Try the iterator protocol first. > it = iter(x) > except TypeError: > # Fall back on the old-fashioned sequence protocol. > counter = 0 > while True: > try: > x = obj[counter] Does this access not use __getitem__()? Not that I would ever use this in reality, but it is interesting to note. > except IndexError: > # We must be past the end of the sequence. > del counter > break > do_something(x) > counter += 1 > else: > # Iterator protocol. > while True: > try: > x = next(it) > except StopIteration: > # We're done. > del it > break > do_something(x) Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 d at davea.name Fri Jun 8 03:16:49 2012 From: d at davea.name (Dave Angel) Date: Thu, 07 Jun 2012 21:16:49 -0400 Subject: [Tutor] Notepad++ question In-Reply-To: References: Message-ID: <4FD15281.6080203@davea.name> On 06/07/2012 02:36 PM, Alexander Quest wrote: > Ok, thanks guys. I also had one more quick question regarding a piece of > boilerplate code: > To get a response, you will needs to leave your question at the python tutor newsgroup. We are part of a group, not offering private advice. Normally, you just do a Reply-all to one of the existing messages on the thread, to include the group. But you can also type tutor at python.org as a CC: I've been volunteering my time on various forums for over 25 years now, so I think I speak for lots of other volunteers. -- DaveA From mnickey at gmail.com Sat Jun 9 02:10:56 2012 From: mnickey at gmail.com (Mike Nickey) Date: Fri, 8 Jun 2012 17:10:56 -0700 Subject: [Tutor] Finding palindromes in a list Message-ID: I'm trying to write a bit of code that will run through a text file and find the item or word string that is the same forward as it is when it is revered...a palindrome. I'm having a bit of issue looping through the wordList[] that I have created. If you have any comments or suggestions I'd really appreciate the assistance. Code: def getFile(): fname = raw_input('Enter location and filename: ') print #attempt the open the file for reading try: fobj = open(fname, 'r') except IOError, e: print "*** file open error:", e else: #display contents to the screen for eachLine in fobj: # create list here str(eachLine).strip().split() wordList.append(eachLine) fobj.close() def compareElements(wordList): for item in wordList(): if item == item.reversed(): print item else: next(item) wordList = [] getFile() print print "Printing new Word list!!!" print wordList[:] print "\nComparing Word List!!!" compareElements(wordList) -- ~MEN From marc.tompkins at gmail.com Sat Jun 9 03:01:22 2012 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Fri, 8 Jun 2012 18:01:22 -0700 Subject: [Tutor] Finding palindromes in a list In-Reply-To: References: Message-ID: On Fri, Jun 8, 2012 at 5:10 PM, Mike Nickey wrote: > def compareElements(wordList): > for item in wordList(): > if item == item.reversed(): > print item > else: > next(item) > > reversed() is not a string method. To reverse a string, try something like this: > if item == item[::-1] > http://stackoverflow.com/questions/931092/reverse-a-string-in-python -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Sat Jun 9 03:38:32 2012 From: d at davea.name (Dave Angel) Date: Fri, 08 Jun 2012 21:38:32 -0400 Subject: [Tutor] Finding palindromes in a list In-Reply-To: References: Message-ID: <4FD2A918.5060403@davea.name> On 06/08/2012 09:01 PM, Marc Tompkins wrote: > On Fri, Jun 8, 2012 at 5:10 PM, Mike Nickey wrote: > >> def compareElements(wordList): >> for item in wordList(): >> if item == item.reversed(): >> print item >> else: >> next(item) >> >> > reversed() is not a string method. To reverse a string, try something like > this: > >> if item == item[::-1] >> > Also next(item) is useless and probably an error. You do not need an else clause there. the for loop takes care of those details. -- DaveA From mnickey at gmail.com Sat Jun 9 03:46:57 2012 From: mnickey at gmail.com (Mike Nickey) Date: Fri, 8 Jun 2012 18:46:57 -0700 Subject: [Tutor] Finding palindromes in a list In-Reply-To: <4FD2A918.5060403@davea.name> References: <4FD2A918.5060403@davea.name> Message-ID: Thanks guys, Those have helped. Now a new issue comes up. The list 'palindrome' is not being populated. I know that there is a few in the list. I think this is in the compareElements def. While I'm familiar with the .append() call I don't know why it's not populating the palindrome list. Thanks again for all the assistance. CODE: def getFile(): fname = raw_input('Enter location and filename: ') print #attempt the open the file for reading try: fobj = open(fname, 'r') except IOError, e: print "*** file open error:", e else: #display contents to the screen for eachLine in fobj: # create list here eachLine.strip().split() wordList.append(eachLine) fobj.close() def compareElements(wordList): for item in wordList: if item == item[::-1]: #wordList[item] == wordList[item].reverse(): print item palindromes.append(item) wordList = [] palindromes = [] getFile() print "\nPrinting new Word list!!!" print wordList[:] print "\nComparing Word List!!!" compareElements(wordList) print "\nPrinting palindromes" print palindromes[:] print "\nDone!!!" On Fri, Jun 8, 2012 at 6:38 PM, Dave Angel wrote: > On 06/08/2012 09:01 PM, Marc Tompkins wrote: >> On Fri, Jun 8, 2012 at 5:10 PM, Mike Nickey wrote: >> >>> def compareElements(wordList): >>> ? ?for item in wordList(): >>> ? ? ? ?if item == item.reversed(): >>> ? ? ? ? ? ?print item >>> ? ? ? ?else: >>> ? ? ? ? ? ?next(item) >>> >>> >> reversed() is not a string method. ?To reverse a string, try something like >> this: >> >>> if item == item[::-1] >>> >> > > > Also next(item) is useless and probably an error. ?You do not need an > else clause there. ?the for loop takes care of those details. > > > > -- > > DaveA > -- ~MEN From d at davea.name Sat Jun 9 04:03:45 2012 From: d at davea.name (Dave Angel) Date: Fri, 08 Jun 2012 22:03:45 -0400 Subject: [Tutor] Finding palindromes in a list In-Reply-To: References: <4FD2A918.5060403@davea.name> Message-ID: <4FD2AF01.5020308@davea.name> On 06/08/2012 09:46 PM, Mike Nickey wrote: > Thanks guys, > You top-posted your comments, so we lose all context. Put your remarks after the part you're quoting. Or if the stuff you're quoting is irrelevant, strip it out, as I've done here. It's irrelevant because the order is wrong. > Those have helped. Now a new issue comes up. The list 'palindrome' is > not being populated. I know that there is a few in the list. I think > this is in the compareElements def. While I'm familiar with the > .append() call I don't know why it's not populating the palindrome > list. > > Thanks again for all the assistance. > Where's a sample input? What's the file supposed to look like? One word per line, or multiple ones? > > CODE: > def getFile(): > fname = raw_input('Enter location and filename: ') > print > > #attempt the open the file for reading > try: > fobj = open(fname, 'r') > except IOError, e: > print "*** file open error:", e > else: > #display contents to the screen > for eachLine in fobj: > # create list here > eachLine.strip().split() Why don't you do something with the results of these operations. As they stand, they do nothing useful. > wordList.append(eachLine) So wordList is a list of the raw lines from the file, complete with newline on each line. If that's what you want, call it linelist. > fobj.close() > > def compareElements(wordList): > for item in wordList: > if item == item[::-1]: #wordList[item] == > wordList[item].reverse(): > print item > palindromes.append(item) > > wordList = [] > palindromes = [] > getFile() > print "\nPrinting new Word list!!!" > print wordList[:] Did this display what you wanted? If not, fix that before trying to figure out the compareElements part. Hint, you probably wanted WORDs in your wordlist, not lines. > print "\nComparing Word List!!!" > compareElements(wordList) > print "\nPrinting palindromes" > print palindromes[:] > print "\nDone!!!" > > -- DaveA From d at davea.name Sat Jun 9 04:46:53 2012 From: d at davea.name (Dave Angel) Date: Fri, 08 Jun 2012 22:46:53 -0400 Subject: [Tutor] Finding palindromes in a list In-Reply-To: References: <4FD2A918.5060403@davea.name> <4FD2AF01.5020308@davea.name> Message-ID: <4FD2B91D.9000708@davea.name> On 06/08/2012 10:17 PM, Mike Nickey wrote: > On Fri, Jun 8, 2012 at 7:03 PM, Dave Angel wrote: >> On 06/08/2012 09:46 PM, Mike Nickey wrote: >>> Thanks guys, >>> This time you forgot to include the list in your posting. Easiest way is to do Reply-all. But you've done it right several times, so this was probably just a slip of the fingers. >> >> Where's a sample input? What's the file supposed to look like? One word >> per line, or multiple ones? > > I've attached a sample file that I am using as a test. I hope that in > replying below each area will help restore some context. And I've pasted the first few lines of that sample into the message, since many people can't see attachments. {\rtf1\ansi\ansicpg1252\cocoartf1138\cocoasubrtf320 {\fonttbl\f0\fswiss\fcharset0 Helvetica;\f1\froman\fcharset0 Times-Roman;} {\colortbl;\red255\green255\blue255;} \margl1440\margr1440\vieww20780\viewh14200\viewkind0 \deftab720 \pard\pardeftab720 >>> >>> CODE: >>> def getFile(): >>> fname = raw_input('Enter location and filename: ') >>> print >>> >>> #attempt the open the file for reading >>> try: >>> fobj = open(fname, 'r') >>> except IOError, e: >>> print "*** file open error:", e >>> else: >>> #display contents to the screen >>> for eachLine in fobj: >>> # create list here >>> eachLine.strip().split() >> >> Why don't you do something with the results of these operations. As >> they stand, they do nothing useful. >> > This is simply to populate a list from a file. From what I was taught, > and there may be a differing opinion here, each def is supposed to do > one thing. This definition is supposed to populate a list from a text > file. I was referring to a specific source line "eachLine.strip().split()" You don't save the output of this expression, Chances are you intended to. > >>> wordList.append(eachLine) >> >> So wordList is a list of the raw lines from the file, complete with >> newline on each line. If that's what you want, call it linelist. > > Noted and adopted. I have changed the list to be named lineList. >> >>> fobj.close() >>> >>> def compareElements(wordList): >>> for item in wordList: >>> if item == item[::-1]: #wordList[item] == >>> wordList[item].reverse(): >>> print item >>> palindromes.append(item) >>> >>> wordList = [] >>> palindromes = [] >>> getFile() >>> print "\nPrinting new Word list!!!" >>> print wordList[:] >> >> Did this display what you wanted? If not, fix that before trying to >> figure out the compareElements part. Hint, you probably wanted WORDs in >> your wordlist, not lines. >> > It did however they are not really words but variable strings of > characters separated by a semicolon. Is the semi-colon relevant? Did you maybe mean to separate the line based on those semicolons? And perhaps remove the newline at the end of each line? That is approximately what you might have intended with the line that had strip and split in it. So maybe you should correct the line, and save its output. And it's output might be what you want to combine into the wordList. Or something. > >>> print "\nComparing Word List!!!" >>> compareElements(wordList) >>> print "\nPrinting palindromes" >>> print palindromes[:] >>> print "\nDone!!!" >>> >>> >> >> >> -- >> >> DaveA > > > none of the lines in your sample data come close to looking like a palindrome. Maybe you should build an artificial file that has some lines that should trigger, and some that should not. -- DaveA From steve at pearwood.info Sat Jun 9 10:26:54 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 09 Jun 2012 18:26:54 +1000 Subject: [Tutor] special attributes naming confusion In-Reply-To: References: Message-ID: <4FD308CE.3090403@pearwood.info> Alan Gauld wrote: > In general someone "just learning python" doesn't need to know about > special names, they just use the operations they define. > The only exception being __init__() in class definitions. > > These methods are really only of interest to someone who wants to define > their own abstract data type and make it look like a built in type(*). > In the vast majority of beginner scenarios that is not > needed or can be faked without use of them. I mostly agree with this, more or less, although I would like to correct your terminology. "Abstract data type" does not mean what you seem to think it means here. I think you mean concrete types, not abstract. Types which look like the built-in strings, numbers, lists etc. are concrete types, not abstract. Working with abstract types is a relatively advanced thing to do, at least in Python, but concrete types are very common. In computer science, an abstract data type is a theoretical entity used for reasoning about algorithms, rather than an actual programming type. For example, you might define an abstract list type with certain methods, without caring about the actual details of how you would program such a list. These leads to the other common sense of "abstract", as in "abstract base class" or ABC, which is a class which cannot be instantiated. ABCs must be subclassed before you can use them. -- Steven From alan.gauld at btinternet.com Sat Jun 9 14:08:53 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 09 Jun 2012 13:08:53 +0100 Subject: [Tutor] special attributes naming confusion In-Reply-To: <4FD308CE.3090403@pearwood.info> References: <4FD308CE.3090403@pearwood.info> Message-ID: On 09/06/12 09:26, Steven D'Aprano wrote: >> These methods are really only of interest to someone who wants to >> define their own abstract data type and make it look like a built in > > I mostly agree with this, more or less, although I would like to correct > your terminology. "Abstract data type" does not mean what you seem to > think it means here. Oops, quite right, a slip of the fingers, I meant to say "custom data type" Not sure how Abstract came out instead... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From dave6502 at gmail.com Sun Jun 10 11:06:39 2012 From: dave6502 at gmail.com (dave selby) Date: Sun, 10 Jun 2012 10:06:39 +0100 Subject: [Tutor] Subprocess.Popen process seems to be outputting to stdout even though redirected ? Message-ID: I have a simple script to tail a log file for an error string, it works AOK but there is an unexpected side effect, when I run it in a shell. if I echo "ERROR TEST" >> LOG in a separate shell, I get the following from the script ERROR TEST GOT YA :) ERROR TEST So it looks like I am getting stdout from the tail -f, and two copies of it ?, stdout should be piped to subprocess.stdout tail = subprocess.Popen(['tail', '-f', LOG_FILE], shell=False, stdout=subprocess.PIPE) for out in iter(tail.stdout.readline, ''): out_str = out.rstrip('\n') if out_str.find('ERROR') != -1: print 'GOT YA :)' time.sleep(1) Any ideas anyone ? Cheers Dave -- Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html From dave6502 at gmail.com Sun Jun 10 11:47:36 2012 From: dave6502 at gmail.com (dave selby) Date: Sun, 10 Jun 2012 10:47:36 +0100 Subject: [Tutor] Subprocess.Popen process seems to be outputting to stdout even though redirected ? In-Reply-To: References: Message-ID: This list is amazing, I scratch my head, think and think then finally post a question, maybe its the act of spelling it out, ps ax showed 2 x 'tail' processes, I am guessing zombies of previous attempts, killed them, all works AOK now On 10 June 2012 10:06, dave selby wrote: > I have a simple script to tail a log file for an error string, it > works AOK but there is an unexpected side effect, when I run it in a > shell. > > if I > > echo "ERROR TEST" >> LOG > > in a separate shell, I get the following from the script > > ERROR TEST > GOT YA :) > ERROR TEST > > So it looks like I am getting stdout from the tail -f, and two copies > of it ?, stdout should be piped to subprocess.stdout > > tail = subprocess.Popen(['tail', '-f', LOG_FILE], shell=False, > stdout=subprocess.PIPE) > > for out in iter(tail.stdout.readline, ''): > > ? ? ? ?out_str = out.rstrip('\n') > > ? ? ? ?if out_str.find('ERROR') != -1: > ? ? ? ? ? ? ? ?print 'GOT YA :)' > > ? ? ? ?time.sleep(1) > > Any ideas anyone ? > > Cheers > > Dave > > -- > > Please avoid sending me Word or PowerPoint attachments. > See http://www.gnu.org/philosophy/no-word-attachments.html -- Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html From nick.harman at mac.com Sun Jun 10 12:33:38 2012 From: nick.harman at mac.com (Nicholas Harman) Date: Sun, 10 Jun 2012 12:33:38 +0200 Subject: [Tutor] properties beginner M Dawson book Message-ID: Hi, I am teaching myself Python with the book :Python Programming for the Absolute Beginner, 3rd edition by M Dawson. It is an excellent book and I am enjoying completing the challenges at the end of each chapter. There are, unfortunately, no answers nor examples of good and bad code. Honestly I don?t want to ?cheat? but I have to teach Python to some young students in September, using this book, and I want all the responses to any doubts they have ready ..... Here?s something specific: chapter 8 challenge 2: ? Write a program that simulates a television by creating it as an object. The user should be able to enter a channel number and raise or lower the volume. Make sure that the channel number and volume level stay within valid ranges.? I have done a little programming in BlueJ Java before but I decided to try the ?Python style? as explained in the chapter, using properties rather than variables and getters and setters ..... My main question is: When using property and setter it seems I must use ?__name? or ?__volume? (Python?s version of private variables). Is this true? Here are parts of my code: - any comments about how bad / good this code is for the level in the book (NOT advanced stuff please) would be greatly appreciated. Thank you class Television(object): #Contstructor def __init__(self, name, channel = 1, volume = 5): print("A television has been made!") #As I am using properties these assignments indirectly call the #relevant property setter methods listed below self.name = name self.channel = channel self.volume = volume #Note: "self.__name = name" works but then no call is made to the property setter method @property def name(self): return self.__name @name.setter def name(self, newName): if newName != "": self.__name = newName print("This TV is now called: ", self.__name) else: print("Sorry, empty string not allowed as a name.") #If the setter was called in the middle of the program run #that is it already has an attribute self.__name with a value #So leave the namae as it was, unchanged. try: print("The name stays as: ", self.__name) #This will generate an attribute error when called as part of the __init__ #because self.name has not yet been created nor assigned. #So catch this error and give self.__name a "default name" except AttributeError: self.__name = "Default" print("The TV is now called: ", self.__name) and ............ def main(): tv_name = input("What do you want to call your television? ") tv = Television(tv_name) choice = None while choice != "0": print \ (""" TV Maker 0 - Quit 1 - Television Details 2 - Set Channel 3 - Set Volume 4 - Chanage the name of the TV """) choice = input("Choice: ") print() # exit if choice == "0": print("Good-bye.") # Print details of television elif choice == "1": print("Television details.") print(tv) # Change the channel elif choice == "2": amount = int(input("Enter 1 to 100 to choose a channel. ")) tv.channel = amount # Change the volume elif choice == "3": amount = int(input("Enter 0 to 10 set the volume. ")) tv.volume = amount elif choice == "4": print("Choose a new name for your television but not nothing!") tv.name = input("Enter a new name here: ") # some unknown choice else: print("\nSorry, but", choice, "isn't a valid choice.") From steve at pearwood.info Sun Jun 10 13:58:37 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 10 Jun 2012 21:58:37 +1000 Subject: [Tutor] properties beginner M Dawson book In-Reply-To: References: Message-ID: <4FD48BED.4090006@pearwood.info> Nicholas Harman wrote: > I have done a little programming in BlueJ Java before but I decided to try > the ?Python style? as explained in the chapter, using properties rather > than variables and getters and setters ..... My main question is: When > using property and setter it seems I must use ?__name? or ?__volume? > (Python?s version of private variables). Is this true? Certainly not. Python doesn't enforce any naming convention on you. You can use any name you like. But you *should* use something like "__name" or "__volume", simply because that will be more sensible than most other names. But if you choose to use something else, Python won't stop you. Note: you can also use just a single underscore, _name and _volume, to indicate that the attributes are private and should not be touched. -- Steven From steve at pearwood.info Sun Jun 10 14:11:39 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 10 Jun 2012 22:11:39 +1000 Subject: [Tutor] properties beginner M Dawson book In-Reply-To: References: Message-ID: <4FD48EFB.3060209@pearwood.info> Nicholas Harman wrote: > class Television(object): > #Contstructor Spelling error: Constructor. Actually, technically __init__ is not the constructor. __init__ is the initializer, as the object has already been created before __init__ is called. __new__ is the constructor, although in this case it is unused. > def __init__(self, name, channel = 1, volume = 5): Optional: it is the usual convention to declare default values without a space, that is, __init__(self, name, channel=1, volume=5). > def main(): > > tv_name = input("What do you want to call your television? ") Are you using Python 2, or Python 3? If Python 3, this is fine. But in Python 2, you should use raw_input instead of input. > tv = Television(tv_name) > > > choice = None > while choice != "0": > print \ > (""" > TV Maker > > 0 - Quit > 1 - Television Details > 2 - Set Channel > 3 - Set Volume > 4 - Chanage the name of the TV > """) Spelling: Change, not Chanage. The rest seems fine to me, assuming you are writing for beginners. -- Steven From emile at fenx.com Mon Jun 11 15:36:59 2012 From: emile at fenx.com (Emile van Sebille) Date: Mon, 11 Jun 2012 06:36:59 -0700 Subject: [Tutor] properties beginner M Dawson book In-Reply-To: <4FD48BED.4090006@pearwood.info> References: <4FD48BED.4090006@pearwood.info> Message-ID: On 6/10/2012 4:58 AM Steven D'Aprano said... > Nicholas Harman wrote: > >> I have done a little programming in BlueJ Java before but I decided to >> try >> the ?Python style? as explained in the chapter, using properties rather >> than variables and getters and setters ..... My main question is: When >> using property and setter it seems I must use ?__name? or ?__volume? >> (Python?s version of private variables). Is this true? > > Certainly not. Python doesn't enforce any naming convention on you. You > can use any name you like. But you *should* use something like "__name" > or "__volume", simply because that will be more sensible than most other > names. But if you choose to use something else, Python won't stop you. > Note that use the doubleunderscore names is a special construct that invokes name mangling. You might want to introduce them when teaching about sub-classing. Pep 8 speaks to their use. See http://www.python.org/dev/peps/pep-0008/ HTH, Emile From brianjamesarb at gmail.com Mon Jun 11 17:05:51 2012 From: brianjamesarb at gmail.com (brian arb) Date: Mon, 11 Jun 2012 11:05:51 -0400 Subject: [Tutor] properties beginner M Dawson book In-Reply-To: References: Message-ID: I would look into pylint, Python source code looking for bugs and signs of poor quality. > There are, unfortunately, no answers nor examples of good and bad code. > Honestly I don?t want to ?cheat? but I have to teach Python to some young > students in September, using this book, and I want all the responses to any > doubts they have ready ..... > > > Here?s something specific: > chapter 8 challenge 2: ? Write a program that simulates a television by > creating it as an object. The user should be able to enter a channel number > and raise or lower the volume. Make sure that the channel number and > volume level stay within valid ranges.? Highly recommend writing unittest, each unit test sends a specific input to a method and verifies that the method returns the expected value, or takes the expected action. Unit tests prove that the code you are testing does in fact do what you expect it to do. -------------- next part -------------- An HTML attachment was scrubbed... URL: From brianjamesarb at gmail.com Mon Jun 11 17:08:40 2012 From: brianjamesarb at gmail.com (brian arb) Date: Mon, 11 Jun 2012 11:08:40 -0400 Subject: [Tutor] properties beginner M Dawson book In-Reply-To: References: Message-ID: I'm a bad fan of Nick Parlante teaching style. He has a excellent site that has some exercises to teach Python, and they are backed with unittests. http://codingbat.com/python On Mon, Jun 11, 2012 at 11:05 AM, brian arb wrote: > I would look into pylint, Python source code looking for bugs and signs > of poor quality. > > >> There are, unfortunately, no answers nor examples of good and bad code. >> Honestly I don?t want to ?cheat? but I have to teach Python to some young >> students in September, using this book, and I want all the responses to any >> doubts they have ready ..... >> >> > >> Here?s something specific: >> chapter 8 challenge 2: ? Write a program that simulates a television by >> creating it as an object. The user should be able to enter a channel number >> and raise or lower the volume. Make sure that the channel number and >> volume level stay within valid ranges.? > > > Highly recommend writing unittest, each unit test sends a specific input > to a method and verifies that the method returns the expected value, or > takes the expected action. Unit tests prove that the code you are testing > does in fact do what you expect it to do. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Mon Jun 11 17:57:48 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 11 Jun 2012 16:57:48 +0100 Subject: [Tutor] properties beginner M Dawson book In-Reply-To: References: Message-ID: On 11/06/2012 16:05, brian arb wrote: > I would look into pylint, Python source code looking for bugs and signs of > poor quality. > Or pychecker or pyflakes. -- Cheers. Mark Lawrence. From alan.gauld at btinternet.com Mon Jun 11 18:24:48 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 11 Jun 2012 17:24:48 +0100 Subject: [Tutor] properties beginner M Dawson book In-Reply-To: References: Message-ID: On 11/06/12 16:05, brian arb wrote: > Highly recommend writing unittest, each unit test sends a specific input > to a method and verifies that the method returns the expected value, or > takes the expected action. Unit tests prove that the code you are > testing does in fact do what you expect it to do. While unit testing is absolutely essential we need to remember the limitations. They only test that output matches the expected results for the inputs that we remember to test. Many of the bugs found in system testing are the result of inputs the programmer never thought to unit test... So "prove" in the statement above may be a tad optimistic. One reason that testing should ideally never be done by the person writing the code. (And even when it isn't things still get missed!) But in practice unit testing usually is. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From bodsda at googlemail.com Tue Jun 12 10:07:13 2012 From: bodsda at googlemail.com (Bod Soutar) Date: Tue, 12 Jun 2012 09:07:13 +0100 Subject: [Tutor] Issue with classes Message-ID: Hi, I am having some difficulty calling a class method from a different class. When I run the attached script like this "python cheatsheet.py --list" C:\>python cheatsheet.py --list done here? Traceback (most recent call last): File "cheatsheet.py", line 167, in main() File "cheatsheet.py", line 165, in main ca.parseArgs() File "cheatsheet.py", line 39, in parseArgs self.argList() File "cheatsheet.py", line 96, in argList handle = cf.load() NameError: global name 'cf' is not defined So the traceback points to the issue being on line 96, specifically the call to cf.load(). cf.load() (line 147) is part of the cheatFile class (line 125), and is instantiated with cf = cheatFile() (line 161) I confess I don't know anything about classes really so I'm probably doing something stupid, but can anyone point me in the right direction? Thanks, Bodsda -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: cheatsheet.py Type: application/octet-stream Size: 4238 bytes Desc: not available URL: From andipersti at gmail.com Tue Jun 12 10:54:41 2012 From: andipersti at gmail.com (Andreas Perstinger) Date: Tue, 12 Jun 2012 10:54:41 +0200 Subject: [Tutor] Issue with classes In-Reply-To: References: Message-ID: <20120612105441.d9f011eac9d6dd711bbe1cf3@gmail.com> On Tue, 12 Jun 2012 09:07:13 +0100 Bod Soutar wrote: > C:\>python cheatsheet.py --list > done > here? > Traceback (most recent call last): > File "cheatsheet.py", line 167, in > main() > File "cheatsheet.py", line 165, in main > ca.parseArgs() > File "cheatsheet.py", line 39, in parseArgs > self.argList() > File "cheatsheet.py", line 96, in argList > handle = cf.load() > NameError: global name 'cf' is not defined You define "cf" only inside your "main" function, so the "cliArgs" class doesn't know about it. So either make "cf" global (bad) or change your "cliArgs" class so that it takes a "cheatFile" object as an argument and you would call it like: ca = cliArgs(cf) HTH, Andreas From bodsda at googlemail.com Tue Jun 12 10:59:56 2012 From: bodsda at googlemail.com (Bod Soutar) Date: Tue, 12 Jun 2012 09:59:56 +0100 Subject: [Tutor] Issue with classes In-Reply-To: References: <20120612105441.d9f011eac9d6dd711bbe1cf3@gmail.com> Message-ID: On Jun 12, 2012 9:56 AM, "Andreas Perstinger" wrote: > > On Tue, 12 Jun 2012 09:07:13 +0100 > Bod Soutar wrote: > > > C:\>python cheatsheet.py --list > > done > > here? > > Traceback (most recent call last): > > File "cheatsheet.py", line 167, in > > main() > > File "cheatsheet.py", line 165, in main > > ca.parseArgs() > > File "cheatsheet.py", line 39, in parseArgs > > self.argList() > > File "cheatsheet.py", line 96, in argList > > handle = cf.load() > > NameError: global name 'cf' is not defined > > You define "cf" only inside your "main" function, so the "cliArgs" > class doesn't know about it. So either make "cf" global (bad) or change > your "cliArgs" class so that it takes a "cheatFile" object as an > argument and you would call it like: > > ca = cliArgs(cf) > > HTH, Andreas > Thanks, that makes sense. I'll give it a go. Cheers, Bodsda _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeanpierreda at gmail.com Tue Jun 12 11:00:57 2012 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Tue, 12 Jun 2012 05:00:57 -0400 Subject: [Tutor] Issue with classes In-Reply-To: References: Message-ID: On Tue, Jun 12, 2012 at 4:07 AM, Bod Soutar wrote: > I confess I don't know anything about classes really so I'm probably doing > something stupid, but can anyone point me in the right direction? A smaller example: def foo(): mydict = {} bar() print mydict def bar(): mydict[3] = 5 Why do you suppose mydict is not visible from bar? This is the same reason that cf is not visible from methods on your class. -- Devin From __peter__ at web.de Tue Jun 12 11:02:11 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 12 Jun 2012 11:02:11 +0200 Subject: [Tutor] Issue with classes References: Message-ID: Bod Soutar wrote: > Hi, > > I am having some difficulty calling a class method from a different class. > When I run the attached script like this "python cheatsheet.py --list" > > C:\>python cheatsheet.py --list > done > here? > Traceback (most recent call last): > File "cheatsheet.py", line 167, in > main() > File "cheatsheet.py", line 165, in main > ca.parseArgs() > File "cheatsheet.py", line 39, in parseArgs > self.argList() > File "cheatsheet.py", line 96, in argList > handle = cf.load() > NameError: global name 'cf' is not defined > > > So the traceback points to the issue being on line 96, specifically the > call to cf.load(). cf.load() (line 147) is part of the cheatFile class > (line 125), and is instantiated with cf = cheatFile() (line 161) > > I confess I don't know anything about classes really so I'm probably doing > something stupid, but can anyone point me in the right direction? > > Thanks, > Bodsda Your problem has nothing to do with classes, you are trying to use a variable outsite its scope. For example the same error will be triggered by def print_message(): print(message) def main(): message = "hello" print_message() main() The tempting newbie fix is to make message a global variable def main(): global message message = "hello" print_message() but this makes for brittle code and you and up with lots of implicit dependencies. Instead you should pass an argument explicitly: def print_message(message): print(message) def main(): message = "hello" print_message(message) If you are dealing with a class you can either pass the argument to the relevant method, or, if you need it in a lot of places make it an attribute class Message: def __init__(self, message): self.message = message def print_it(self): print(self.message) def main(): m = Message("hello") m.print_it() Can you apply that idea to your code? Your main() function will become something like def main(): cf = cheatFile() print "done" ca = cliArgs(cf) ca.parseArgs() Come back here if you run into problems. By the way, Python's standard library offers an excellent way to deal with commandline arguments, see http://docs.python.org/library/argparse.html which you should prefer over you self-made cliArgs. From rfquerin at gmail.com Tue Jun 12 21:01:08 2012 From: rfquerin at gmail.com (Richard Querin) Date: Tue, 12 Jun 2012 15:01:08 -0400 Subject: [Tutor] Question about autocomplete functionality Message-ID: Hi All, I wrote a small utility several months ago to easily search a project list (an excel spreadsheet) by project name or number etc. However I am thinking of expanding it with the following sort of functionality - the ability to add 'tags' to a given project (tags being just simple descriptive words about a given project). However, this is only really useful if I can keep a running list of all tags that have ever been entered and use a sort of autocomplete functionality to make them quicker to enter. And to reduce the number of, and discourage the use of, 'almost duplicate' tags. Are there any resources out there (libraries, tutorials etc) that could help me figure this sort of autocomplete functionality out? Thanks, Richard -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Tue Jun 12 21:40:19 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 12 Jun 2012 15:40:19 -0400 Subject: [Tutor] Question about autocomplete functionality In-Reply-To: References: Message-ID: On Tue, Jun 12, 2012 at 3:38 PM, Joel Goldstick wrote: > On Tue, Jun 12, 2012 at 3:01 PM, Richard Querin wrote: >> Hi All, >> >> I wrote a small utility several months ago to easily search a project list >> (an excel spreadsheet) by project name or number etc. However I am thinking >> of expanding it with the following sort of functionality?- the ability to >> add 'tags' to a given project (tags being just simple descriptive words >> about a given project). >> >> However, this is only really useful if I can keep a running list of all tags >> that have ever been entered and use a sort of autocomplete functionality to >> make them quicker to enter. And to reduce the number of, and discourage the >> use of, 'almost duplicate' tags. >> >> Are there any resources out there (libraries, tutorials etc) that could help >> me figure this sort of autocomplete functionality out? >> >> Thanks, >> Richard I think I messed up my post so trying again: > If you want a GUI desktop sort of application check this out: > http://www.freenetpages.co.uk/hp/alan.gauld/ > There is a chapter on GUI programming that might give you some good ideas > >> >> >> >> _______________________________________________ >> Tutor maillist ?- ?Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > -- > Joel Goldstick -- Joel Goldstick From alan.gauld at btinternet.com Tue Jun 12 21:45:41 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 12 Jun 2012 20:45:41 +0100 Subject: [Tutor] Question about autocomplete functionality In-Reply-To: References: Message-ID: On 12/06/12 20:01, Richard Querin wrote: > tags that have ever been entered and use a sort of autocomplete > functionality to make them quicker to enter. And to reduce the number > of, and discourage the use of, 'almost duplicate' tags. > > Are there any resources out there (libraries, tutorials etc) that could > help me figure this sort of autocomplete functionality out? IDLE does autocomplete so you could look at how it does it - since it is written in Python... The IDLE mailing list can probably help find the right source file. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From joel.goldstick at gmail.com Tue Jun 12 21:38:30 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 12 Jun 2012 15:38:30 -0400 Subject: [Tutor] Question about autocomplete functionality In-Reply-To: References: Message-ID: On Tue, Jun 12, 2012 at 3:01 PM, Richard Querin wrote: > Hi All, > > I wrote a small utility several months ago to easily search a project list > (an excel spreadsheet) by project name or number etc. However I am thinking > of expanding it with the following sort of functionality?- the ability to > add 'tags' to a given project (tags being just simple descriptive words > about a given project). > > However, this is only really useful if I can keep a running list of all tags > that have ever been entered and use a sort of autocomplete functionality to > make them quicker to enter. And to reduce the number of, and discourage the > use of, 'almost duplicate' tags. > > Are there any resources out there (libraries, tutorials etc) that could help > me figure this sort of autocomplete functionality out? > > Thanks, > Richard > > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > If you want a GUI desktop sort of application check this out: http://www.freenetpages.co.uk/hp/alan.gauld/ There is a chapter on GUI programming that might give you some good ideas -- Joel Goldstick From ramit.prasad at jpmorgan.com Tue Jun 12 22:17:15 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 12 Jun 2012 20:17:15 +0000 Subject: [Tutor] Question about autocomplete functionality In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF47412D90638@SCACMX008.exchad.jpmchase.net> > However, this is only really useful if I can keep a running list of all tags > that have ever been entered and use a sort of autocomplete functionality to > make them quicker to enter. And to reduce the number of, and discourage the > use of, 'almost duplicate' tags. > > Are there any resources out there (libraries, tutorials etc) that could help > me figure this sort of autocomplete functionality out? I am sure there are examples of widgets that do autocomplete depending on which UI library you want to use. Here is an example for wxPython http://wiki.wxpython.org/TextCtrlAutoComplete . The discouraging of 'almost duplicate' tags is going to be more difficult because it probably requires someone to either merge tags or some intelligence to know that two tags are equivalent. For casual use, it would be more efficient to create a process where a moderator can merge tags. I suppose you could also disallow the use of new tags or have them put into a moderation queue. Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 alan.gauld at btinternet.com Wed Jun 13 01:20:02 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 13 Jun 2012 00:20:02 +0100 Subject: [Tutor] Question about autocomplete functionality In-Reply-To: References: Message-ID: > If you want a GUI desktop sort of application check this out: > http://www.freenetpages.co.uk/hp/alan.gauld/ > There is a chapter on GUI programming that might give you some good ideas But that's a prehistoric version of the tutor :-) Use the link in my sig instead... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Wed Jun 13 01:22:08 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 13 Jun 2012 00:22:08 +0100 Subject: [Tutor] Question about autocomplete functionality In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47412D90638@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF47412D90638@SCACMX008.exchad.jpmchase.net> Message-ID: On 12/06/12 21:17, Prasad, Ramit wrote: > The discouraging of 'almost duplicate' tags is going to be more difficult I understood the OP to simply mean that autocompletion would reduce the amount of mis-typing by only completing existing tag values therefore reducing misspellings etc. If that's correct it is a much simpler problem. Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From abrunn1133 at yahoo.com Wed Jun 13 07:29:32 2012 From: abrunn1133 at yahoo.com (Andrew Brunn) Date: Tue, 12 Jun 2012 22:29:32 -0700 (PDT) Subject: [Tutor] While Loop Message-ID: <1339565372.38787.YahooMailClassic@web65903.mail.ac4.yahoo.com> Let me preface by saying that I am currently taking my first programming class; so I have been a "programmer" for just over a week now. Here is my question; I am trying to create a while loop that will allow me ask for a username and password.? If a wrong username and password is entered, I would like the program to prompt the user to re-enter the username and password until they get it correct.? That means even if they get it wrong a thousand times, I want to program to keep asking them. This is my code; [code] username = "" while not username: ??? username=raw_input("Username: ") ?????????????????????? password = "" while not password: ??? password=raw_input("Password: ") if username == "john doe" and password == "fopwpo": ??? print "Login successful" else: ??? print "Error. Please re-enter your username and password." [code] Andy Brunn -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Wed Jun 13 07:59:02 2012 From: d at davea.name (Dave Angel) Date: Wed, 13 Jun 2012 01:59:02 -0400 Subject: [Tutor] While Loop In-Reply-To: <1339565372.38787.YahooMailClassic@web65903.mail.ac4.yahoo.com> References: <1339565372.38787.YahooMailClassic@web65903.mail.ac4.yahoo.com> Message-ID: <4FD82C26.5050404@davea.name> On 06/13/2012 01:29 AM, Andrew Brunn wrote: > Let me preface by saying that I am currently taking my first programming > class; so I have been a "programmer" for just over a week now. > > Here is my question; > > I > am trying to create a while loop that will allow me ask for a username > and password. If a wrong username and password is entered, I would like > the program to prompt the user to re-enter the username and password > until they get it correct. That means even if they get it wrong a > thousand times, I want to program to keep asking them. > > This is my code; > > [code] > username = "" > > while not username: > username=raw_input("Username: ") > > password = "" > > while not password: > > password=raw_input("Password: ") > if username == "john doe" and password == "fopwpo": > print "Login successful" > else: > print "Error. Please re-enter your username and password." > [code] > > > > Andy Brunn > So put the whole thing in a while loop. For that matter, also put it in a function. Anything over about 3 lines should be written as one or more functions. The simplest change would be to make the while loop look like while True:, and put a break right after the print "Login successful" line, def getUserPass(): while True (existing code here) if username == "xxxx" and password == "yyyy": print "login successful" break else: print "Error: Please ...." -- DaveA From suryak at live.com Wed Jun 13 11:06:10 2012 From: suryak at live.com (Surya K) Date: Wed, 13 Jun 2012 14:36:10 +0530 Subject: [Tutor] How to read websites - Web Scraping or Parsing in python Message-ID: Hi, I am trying to write a python program which reads any webpage's content. Considering a blog, I'd like to read all the content written by the author in it. So, each blog/ site would be having different type of HTML/ XML whether its Blogger or Wordpress or Typepad or any.. I thought of using their RSS/Atom feeds to extract the content. Then, I used Universal Feed Parser to extract the content. import feedparserurl = "http://knolzone.com/feed" parsedFeed = feedparser.parse(url) websiteTitle = parsedFeed.feed.titlefor aArticle in parsedFeed.entries: print aArticle.link print aArticle.summary I could able to find links of all articles and their summaries with the website's title. But I'd like to read the whole content of a particular article, not just summary Say, for example we take a webpage http://knolzone.com/unlock-hidden-themes-in-windows-7-and-other-useful-tips-part-5-of-7/. The author had written some article in it and I'd like to read that portion of webpage. As my target webpage could be anyone of web, and each website's designers could have designed in their own fashion using different "class names", I am unable to figure out how to read "article content" in a webpage. So, can anyone tell me what libraries should I ultimately use to achieve it ?? and what elements and attributes I should read?? I thought of using BeautifulSoup but I really don't know which elements ( div's or p or a ) I should read. Considering the above webpage given, I consists of lots of "p" elements in it and fortunately all the article content is in "p" elements.. However, there are few other "p" elements which don't belong to article content. In that case how should I eliminate them???? Thanks for reading.. I hope you help. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Jun 13 12:09:36 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 13 Jun 2012 11:09:36 +0100 Subject: [Tutor] How to read websites - Web Scraping or Parsing in python In-Reply-To: References: Message-ID: On 13/06/12 10:06, Surya K wrote: > As my target webpage could be anyone of web, and each website's > designers could have designed in their own fashion using different > "class names", I am unable to figure out how to read "article content" > in a webpage. This is always the problem with scraping webpages, you are dependant on how the individual author structures their pages. And if they change the format it will likely break your scraper. Also some web sites implement devices to deliberately make it hard to scrape the pages - such as changing the div/class names arbitrarily. This is to encourage you to use their web site and see the beautiful adverts they have on display and that pay for the service. > So, can anyone tell me what libraries should I ultimately use to achieve > it ?? and what elements and attributes I should read?? The most basic are urllib, urllib2 and httplib in the standard library > I thought of using BeautifulSoup but I really don't know which elements > ( div's or p or a ) I should read. BS is good but you will need to know which tags you are interested in. Usually the low level

tags are inside a

so you can locate the
and only fetch the

's from that section. But you will need to do some digging and probably some trial and error. - put it in a module/class and use the >>> prompt to experiment is my advice. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From yashwinkanchan at gmail.com Wed Jun 13 14:24:52 2012 From: yashwinkanchan at gmail.com (Yashwin Kanchan) Date: Wed, 13 Jun 2012 14:24:52 +0200 Subject: [Tutor] How to read websites - Web Scraping or Parsing in python In-Reply-To: References: Message-ID: Hi Surya Have you tried using IE automation (assuming you are using windows). I used the library from http://www.mayukhbose.com/python/IEC/index.php import IEC ie = IEC.IEController() ie.Navigate('http://knolzone.com/unlock-hidden-themes-in-windows-7-and-other-useful-tips-part-5-of-7/') ie.GetDocumentText() This gives the complete text of the webpage , you can then work on cutting out the section that you actually require. This isnt a complete solution to your problem , but it would bring you a bit closer. Regards Yashwin Kanchan From bfishbein79 at gmail.com Wed Jun 13 14:35:21 2012 From: bfishbein79 at gmail.com (Benjamin Fishbein) Date: Wed, 13 Jun 2012 07:35:21 -0500 Subject: [Tutor] entering text on web site Message-ID: Hello. I have been teaching myself Python and have been stuck on this problem for a week--I scoured the web for an answer and even asked a couple computer programmer friends. I managed to write a simple program with urllib that downloads the content from web pages; but I want to put text in a text box on the webpage, have it entered into the website, and then get the results. Is there a way to do this with Python? Thanks, Ben From wprins at gmail.com Wed Jun 13 15:27:23 2012 From: wprins at gmail.com (Walter Prins) Date: Wed, 13 Jun 2012 14:27:23 +0100 Subject: [Tutor] entering text on web site In-Reply-To: References: Message-ID: Hi, On 13 June 2012 13:35, Benjamin Fishbein wrote: > Hello. I have been teaching myself Python and have been stuck on this problem for a week--I scoured the web for an answer and even asked a couple computer programmer friends. > I managed to write a simple program with urllib that downloads the content from web pages; but I want to put text in a text box on the webpage, have it entered into the website, and then get the results. Is there a way to do this with Python? Please clarify, are you: a) Trying to automatically post some text to a web page from Python and then retrieve the resultant page? (for example, username and password and retrieve resultant logon page.) b) Trying to use Python to present a webpage that a user then puts text in a box which the Python program then captures when the user submits the page? I'm assuming a.) If so, then I should ask, does this page use POST or GET style submission? In any case, here's a stackeroverflow question regarding using POST and cookies etc: http://stackoverflow.com/questions/2954381/python-form-post-using-urllib2-also-question-on-saving-using-cookies .. with the default Python documentation doing a decent job of explaining GET: http://docs.python.org/howto/urllib2.html This also seems useful/relevant: http://stackoverflow.com/questions/10916745/scraping-data-from-simple-website-change-post-to-get Walter From motoom at xs4all.nl Wed Jun 13 16:29:49 2012 From: motoom at xs4all.nl (Michiel Overtoom) Date: Wed, 13 Jun 2012 16:29:49 +0200 Subject: [Tutor] entering text on web site In-Reply-To: References: Message-ID: <29AF4EC4-B242-4609-A03D-E416C204A818@xs4all.nl> On Jun 13, 2012, at 14:35, Benjamin Fishbein wrote: > I want to put text in a text box on the webpage, have it entered into the website, and then get the results. Is there a way to do this with Python? http://wwwsearch.sourceforge.net/mechanize/ is good for this. -- Test your knowledge of flowers! http://www.learn-the-flowers.com or http://www.leer-de-bloemen.nl for the Dutch version. Test je kennis van bloemen! http://www.leer-de-bloemen.nl of http://www.learn-the-flowers.com voor de Engelse versie. From scarolan at gmail.com Wed Jun 13 21:22:49 2012 From: scarolan at gmail.com (Sean Carolan) Date: Wed, 13 Jun 2012 14:22:49 -0500 Subject: [Tutor] matplotlib pylab question - offset bar graph labels Message-ID: I've got a function that builds a bar graph. We are 95% happy with it, but the x-axis tick labels are a little bit skewed to the right. Anyone know how to skew the labels over by 10 or 20 pixels? Here's the code that generates my xticks: xticks(range(len(builds)), users, rotation=30) From scarolan at gmail.com Wed Jun 13 21:44:54 2012 From: scarolan at gmail.com (Sean Carolan) Date: Wed, 13 Jun 2012 14:44:54 -0500 Subject: [Tutor] matplotlib pylab question - offset bar graph labels In-Reply-To: References: Message-ID: > I've got a function that builds a bar graph. ?We are 95% happy with > it, but the x-axis tick labels are a little bit skewed to the right. > Anyone know how to skew the labels over by 10 or 20 pixels? ?Here's > the code that generates my xticks: > > xticks(range(len(builds)), users, rotation=30) In case anyone else comes across this issue, I found an acceptable solution, these settings seem to work well for us: xticks(range(len(xnames)), xnames, rotation=45, size=8, horizontalalignment='center') From bfishbein79 at gmail.com Thu Jun 14 04:44:47 2012 From: bfishbein79 at gmail.com (Benjamin Fishbein) Date: Wed, 13 Jun 2012 21:44:47 -0500 Subject: [Tutor] entering text on web site In-Reply-To: References: Message-ID: Thanks. I'm trying to automatically post text to a web page from Python and retrieve the resultant page. It's not just name and password, though; I need to do it over and over again with chunks of info from a list. The page seems to be using POST. form method='POST' is written all throughout the code. The links you recommended use modules like Client Cookie, mechanize, etc. I guess Python didn't come standard with a module that could do what I wanted... Which is the best module to get for this? Ben On Jun 13, 2012, at 8:27 AM, Walter Prins wrote: > Hi, > > On 13 June 2012 13:35, Benjamin Fishbein wrote: >> Hello. I have been teaching myself Python and have been stuck on this problem for a week--I scoured the web for an answer and even asked a couple computer programmer friends. >> I managed to write a simple program with urllib that downloads the content from web pages; but I want to put text in a text box on the webpage, have it entered into the website, and then get the results. Is there a way to do this with Python? > > Please clarify, are you: > a) Trying to automatically post some text to a web page from Python > and then retrieve the resultant page? (for example, username and > password and retrieve resultant logon page.) > b) Trying to use Python to present a webpage that a user then puts > text in a box which the Python program then captures when the user > submits the page? > > I'm assuming a.) If so, then I should ask, does this page use POST or > GET style submission? > > In any case, here's a stackeroverflow question regarding using POST > and cookies etc: > http://stackoverflow.com/questions/2954381/python-form-post-using-urllib2-also-question-on-saving-using-cookies > .. with the default Python documentation doing a decent job of explaining GET: > http://docs.python.org/howto/urllib2.html > > This also seems useful/relevant: > http://stackoverflow.com/questions/10916745/scraping-data-from-simple-website-change-post-to-get > > Walter > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Jun 14 10:09:07 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 14 Jun 2012 09:09:07 +0100 Subject: [Tutor] entering text on web site In-Reply-To: References: Message-ID: On 14/06/12 03:44, Benjamin Fishbein wrote: > etc. I guess Python didn't come standard with a module that could do > what I wanted... > Which is the best module to get for this? Python does come with low level modules that you can use but its a lot of work. The suggested add ins are generally easier to use or more powerful 'out of the box'. But you could use things like urlib2 and httplib and cgi etc to do what you want if you really need to stick with the standard library. As to which is "best" that depend on your favoured programming style and the exact nature of your problem. We can't answer that for you. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From timomlists at gmail.com Thu Jun 14 11:42:15 2012 From: timomlists at gmail.com (Timo) Date: Thu, 14 Jun 2012 11:42:15 +0200 Subject: [Tutor] entering text on web site In-Reply-To: References: Message-ID: <4FD9B1F7.6040404@gmail.com> Op 14-06-12 04:44, Benjamin Fishbein schreef: > Thanks. I'm trying to automatically post text to a web page from > Python and retrieve the resultant page. It's not just name and > password, though; I need to do it over and over again with chunks of > info from a list. > The page seems to be using POST. > form method='POST' > is written all throughout the code. > The links you recommended use modules like Client Cookie, mechanize, > etc. I guess Python didn't come standard with a module that could do > what I wanted... > Which is the best module to get for this? I had successful results with the mechanize package in the past. Timo > > Ben > > > On Jun 13, 2012, at 8:27 AM, Walter Prins wrote: > >> Hi, >> >> On 13 June 2012 13:35, Benjamin Fishbein > > wrote: >>> Hello. I have been teaching myself Python and have been stuck on >>> this problem for a week--I scoured the web for an answer and even >>> asked a couple computer programmer friends. >>> I managed to write a simple program with urllib that downloads the >>> content from web pages; but I want to put text in a text box on the >>> webpage, have it entered into the website, and then get the results. >>> Is there a way to do this with Python? >> >> Please clarify, are you: >> a) Trying to automatically post some text to a web page from Python >> and then retrieve the resultant page? (for example, username and >> password and retrieve resultant logon page.) >> b) Trying to use Python to present a webpage that a user then puts >> text in a box which the Python program then captures when the user >> submits the page? >> >> I'm assuming a.) If so, then I should ask, does this page use POST or >> GET style submission? >> >> In any case, here's a stackeroverflow question regarding using POST >> and cookies etc: >> http://stackoverflow.com/questions/2954381/python-form-post-using-urllib2-also-question-on-saving-using-cookies >> .. with the default Python documentation doing a decent job of >> explaining GET: >> http://docs.python.org/howto/urllib2.html >> >> This also seems useful/relevant: >> http://stackoverflow.com/questions/10916745/scraping-data-from-simple-website-change-post-to-get >> >> Walter >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From tomavos at yahoo.com.ar Thu Jun 14 17:30:08 2012 From: tomavos at yahoo.com.ar (Tom Avos) Date: Thu, 14 Jun 2012 08:30:08 -0700 (PDT) Subject: [Tutor] Trouble running Python on Windows Vista Message-ID: <1339687808.36267.YahooMailNeo@web122601.mail.ne1.yahoo.com> Hi there, I'm learning Python. I have to work with a Notebook running Widows Vista Home Premium.(have no other choice) It works fine when working in Interactive Mode or by? Opening a .py file from the IDLE and then running it by pressing F5, but I can not run .py files by invoking them from the prompt (neither from IDLE nor Dos Command Line); ? Every time I try to do this I get the following error:(In this case I'm tryig to invoke triangulo.py saved in C:\python27); ? >>> triangulo.py Traceback (most recent call last): ? File "", line 1, in ??? triangulo.py NameError: name 'triangulo' is not defined >>> ? The same happens for every file I want to run by calling them from the prompt, although if I double click on any .py file it runs ok. ? I've installed Python as follows: ? I've downloaded the Python 2.7.3 Windows Installer?from www.python.org/download/. Once I've got it I run the installer and Python was installed on C:\Python27, and I've added Python to the my directory by doing set path=%path%;C:\python27. ? Any ideas will be very welcome :-) Thanks in advance for your attention. -------------- next part -------------- An HTML attachment was scrubbed... URL: From kwpolska at gmail.com Thu Jun 14 18:33:26 2012 From: kwpolska at gmail.com (Kwpolska) Date: Thu, 14 Jun 2012 18:33:26 +0200 Subject: [Tutor] Trouble running Python on Windows Vista In-Reply-To: <1339687808.36267.YahooMailNeo@web122601.mail.ne1.yahoo.com> References: <1339687808.36267.YahooMailNeo@web122601.mail.ne1.yahoo.com> Message-ID: On Thu, Jun 14, 2012 at 5:30 PM, Tom Avos wrote: > Hi there, > I'm learning Python. I have to work with a Notebook running Widows Vista > Home Premium.(have no other choice) > It works fine when working in Interactive Mode or by? Opening a .py file > from the IDLE and then running it by pressing F5, but I can not run .py > files by invoking them from the prompt (neither from IDLE nor Dos Command > Line); > > Every time I try to do this I get the following error:(In this case I'm > tryig to invoke triangulo.py saved in C:\python27); > >>>> triangulo.py > Traceback (most recent call last): > ? File "", line 1, in > ??? triangulo.py > NameError: name 'triangulo' is not defined >>>> > > The same happens for every file I want to run by calling them from the > prompt, although if I double click on any .py file it runs ok. > > I've installed Python as follows: > > I've downloaded the Python 2.7.3 Windows Installer?from > www.python.org/download/. > Once I've got it I run the installer and Python was installed on > C:\Python27, and I've added Python to the my directory by doing set > path=%path%;C:\python27. > > Any ideas will be very welcome :-) > Thanks in advance for your attention. > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > That's not how you run files. python path-to-your-file If it isn't in your %PATH% (it might not be, as you need to set it in Computer->Properties->Advanced system settings->Environment Variables), prepend it with c:\python27\. And please don't put them in C:\Python27\, it isn't supposed to be used for that. -- Kwpolska stop html mail ? ? ?| always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 ? | Arch Linux x86_64, zsh, mutt, vim. # vim:set textwidth=70: From alan.gauld at btinternet.com Thu Jun 14 18:37:00 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 14 Jun 2012 17:37:00 +0100 Subject: [Tutor] Trouble running Python on Windows Vista In-Reply-To: <1339687808.36267.YahooMailNeo@web122601.mail.ne1.yahoo.com> References: <1339687808.36267.YahooMailNeo@web122601.mail.ne1.yahoo.com> Message-ID: On 14/06/12 16:30, Tom Avos wrote: > It works fine when working in Interactive Mode or by Opening a .py file > from the IDLE and then running it by pressing F5, but I can not run .py > files by invoking them from the prompt (neither from IDLE nor Dos > Command Line); It looks like you are trying to run them from the Python prompt but you need to run them from the DOS prompt (or Windows Explorer as you discovered). At the DOS prompt (looking something like C:\WINDOWS>) you type python C:\Full\Path\To\File\triangulo.py ie you need to tell Python where to find the file, python does not use the DOS PATH value. Alternatively CD into the folder with the .py file and Python will find it in the local folder. hth, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From tomavos at yahoo.com.ar Thu Jun 14 20:54:47 2012 From: tomavos at yahoo.com.ar (Tom Avos) Date: Thu, 14 Jun 2012 11:54:47 -0700 (PDT) Subject: [Tutor] Trouble running Python on Windows Vista In-Reply-To: <1339690750.91391.YahooMailNeo@web122604.mail.ne1.yahoo.com> References: <1339687808.36267.YahooMailNeo@web122601.mail.ne1.yahoo.com> <1339689345.89878.YahooMailNeo@web122604.mail.ne1.yahoo.com> <1339690750.91391.YahooMailNeo@web122604.mail.ne1.yahoo.com> Message-ID: <1339700087.18036.YahooMailNeo@web122603.mail.ne1.yahoo.com> ________________________________ De: Tom Avos Para: "Flynn, Stephen (L & P - IT)" Enviado: jueves, 14 de junio de 2012 13:19 Asunto: Re: [Tutor] Trouble running Python on Windows Vista Great! You're right.. It works from the Command Line :-) ? So, how should I call it inside the IDLE?? ... If I write ">>> triangulo.py" I get the "NameError" ? Thanks again. ________________________________ De: "Flynn, Stephen (L & P - IT)" Para: Tom Avos Enviado: jueves, 14 de junio de 2012 13:00 Asunto: RE: [Tutor] Trouble running Python on Windows Vista Looks like (from the ?>>>? prompt) that you?re still inside IDLE when you try to fire up the interpreter. This is how one would kick off the python code from the command line. ? S. ? ________________________________ From:Tom Avos [mailto:tomavos at yahoo.com.ar] Sent: Thursday, June 14, 2012 4:56 PM To: Flynn, Stephen (L & P - IT) Subject: Re: [Tutor] Trouble running Python on Windows Vista ? Hi Stephen, I followed your advice, but still no luck... I get the following error ("triangulo" highlighted): >>> python.exe triangulo.py SyntaxError: invalid syntax >>> Best, ? ? De:" Flynn, Stephen (L & P - IT)" Para: Tom Avos Enviado: jueves, 14 de junio de 2012 12:36 Asunto: RE: [Tutor] Trouble running Python on Windows Vista Hi Tom, ? ? ?python.exe triangulo.py? ? ? ? ? S. ? ? From:tutor-bounces+steve.flynn=capita.co.uk at python.org [mailto:tutor-bounces+steve.flynn=capita.co.uk at python.org] On Behalf Of Tom Avos Sent: Thursday, June 14, 2012 4:30 PM To: tutor at python.org Subject: [Tutor] Trouble running Python on Windows Vista ? Hi there, I'm learning Python. I have to work with a Notebook running Widows Vista Home Premium.(have no other choice) It works fine when working in Interactive Mode or by? Opening a .py file from the IDLE and then running it by pressing F5, but I can not run .py files by invoking them from the prompt (neither from IDLE nor Dos Command Line); ? Every time I try to do this I get the following error:(In this case I'm tryig to invoke triangulo.py saved in C:\python27); ? >>> triangulo.py Traceback (most recent call last): ? File "", line 1, in ??? triangulo.py NameError: name 'triangulo' is not defined >>> ? The same happens for every file I want to run by calling them from the prompt, although if I double click on any .py file it runs ok. ? I've installed Python as follows: ? I've downloaded the Python 2.7.3 Windows Installer?from www.python.org/download/. Once I've got it I run the installer and Python was installed on C:\Python27, and I've added Python to the my directory by doing set path=%path%;C:\python27. ? Any ideas will be very welcome :-) Thanks in advance for your attention. ? ? Click here to report this email as spam. ? This email and any attachment to it are confidential.? Unless you are the intended recipient, you may not use, copy or disclose either the message or any information contained in the message. If you are not the intended recipient, you should delete this email and notify the sender immediately. ? ?Any views or opinions expressed in this email are those of the sender only, unless otherwise stated.? All copyright in any Capita material in this email is reserved. ? ?All emails, incoming and outgoing, may be recorded by Capita and monitored for legitimate business purposes. ? ?Capita exclude all liability for any loss or damage arising or resulting from the receipt, use or transmission of this email to the fullest extent permitted by law. ? ?This message has been scanned for malware by Websense. www.websense.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From tomavos at yahoo.com.ar Thu Jun 14 21:19:36 2012 From: tomavos at yahoo.com.ar (Tom Avos) Date: Thu, 14 Jun 2012 12:19:36 -0700 (PDT) Subject: [Tutor] Trouble running Python on Windows Vista In-Reply-To: References: <1339687808.36267.YahooMailNeo@web122601.mail.ne1.yahoo.com> <1339689345.89878.YahooMailNeo@web122604.mail.ne1.yahoo.com> <1339690750.91391.YahooMailNeo@web122604.mail.ne1.yahoo.com> <1339700087.18036.YahooMailNeo@web122603.mail.ne1.yahoo.com> Message-ID: <1339701576.88862.YahooMailNeo@web122605.mail.ne1.yahoo.com> Yes, If I open the file from the IDLE and then I press "f5" it works, but I want to know why it does not work if I write the filename after the ">>>" prompt . That way is much more handy, I would like to make it work. ? ________________________________ De: Kwpolska Para: Tom Avos Enviado: jueves, 14 de junio de 2012 16:01 Asunto: Re: [Tutor] Trouble running Python on Windows Vista On Thu, Jun 14, 2012 at 8:54 PM, Tom Avos wrote: > De: Tom Avos > Para: "Flynn, Stephen (L & P - IT)" > Enviado: jueves, 14 de junio de 2012 13:19 > Asunto: Re: [Tutor] Trouble running Python on Windows Vista > > Great! You're right.. It works from the Command Line :-) > > So, how should I call it inside the IDLE?? ... If I write ">>> triangulo.py" > I get the "NameError" > > Thanks again. > > De: "Flynn, Stephen (L & P - IT)" > Para: Tom Avos > Enviado: jueves, 14 de junio de 2012 13:00 > Asunto: RE: [Tutor] Trouble running Python on Windows Vista > > Looks like (from the ?>>>? prompt) that you?re still inside IDLE when you > try to fire up the interpreter. This is how one would kick off the python > code from the command line. > > S. > > From: Tom Avos [mailto:tomavos at yahoo.com.ar] > Sent: Thursday, June 14, 2012 4:56 PM > To: Flynn, Stephen (L & P - IT) > Subject: Re: [Tutor] Trouble running Python on Windows Vista > > Hi Stephen, > I followed your advice, but still no luck... I get the following error > ("triangulo" highlighted): >>>> python.exe triangulo.py > SyntaxError: invalid syntax >>>> > Best, > > > De: " Flynn, Stephen (L & P - IT)" > Para: Tom Avos > Enviado: jueves, 14 de junio de 2012 12:36 > Asunto: RE: [Tutor] Trouble running Python on Windows Vista > > > Hi Tom, > > > ?python.exe triangulo.py? > > > > > S. > > > From: tutor-bounces+steve.flynn=capita.co.uk at python.org > [mailto:tutor-bounces+steve.flynn=capita.co.uk at python.org] On Behalf Of Tom > Avos > Sent: Thursday, June 14, 2012 4:30 PM > To: tutor at python.org > Subject: [Tutor] Trouble running Python on Windows Vista > > Hi there, > I'm learning Python. I have to work with a Notebook running Widows Vista > Home Premium.(have no other choice) > It works fine when working in Interactive Mode or by? Opening a .py file > from the IDLE and then running it by pressing F5, but I can not run .py > files by invoking them from the prompt (neither from IDLE nor Dos Command > Line); > > Every time I try to do this I get the following error:(In this case I'm > tryig to invoke triangulo.py saved in C:\python27); > >>>> triangulo.py > Traceback (most recent call last): > ? File "", line 1, in > ??? triangulo.py > NameError: name 'triangulo' is not defined >>>> > > The same happens for every file I want to run by calling them from the > prompt, although if I double click on any .py file it runs ok. > > I've installed Python as follows: > > I've downloaded the Python 2.7.3 Windows Installer?from > www.python.org/download/. > Once I've got it I run the installer and Python was installed on > C:\Python27, and I've added Python to the my directory by doing set > path=%path%;C:\python27. > > Any ideas will be very welcome :-) > Thanks in advance for your attention. > > > Click here to report this email as spam. > > This email and any attachment to it are confidential.? Unless you are the > intended recipient, you may not use, copy or disclose either the message or > any information contained in the message. If you are not the intended > recipient, you should delete this email and notify the sender immediately. > > ?Any views or opinions expressed in this email are those of the sender only, > unless otherwise stated.? All copyright in any Capita material in this email > is reserved. > > ?All emails, incoming and outgoing, may be recorded by Capita and monitored > for legitimate business purposes. > > ?Capita exclude all liability for any loss or damage arising or resulting > from the receipt, use or transmission of this email to the fullest extent > permitted by law. > > ?This message has been scanned for malware by Websense. www.websense.com > > > > > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Open the file in the IDLE editor and press F5. -- Kwpolska stop html mail ? ? ?| always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 ? | Arch Linux x86_64, zsh, mutt, vim. # vim:set textwidth=70: -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Thu Jun 14 21:35:30 2012 From: emile at fenx.com (Emile van Sebille) Date: Thu, 14 Jun 2012 12:35:30 -0700 Subject: [Tutor] Trouble running Python on Windows Vista In-Reply-To: <1339701576.88862.YahooMailNeo@web122605.mail.ne1.yahoo.com> References: <1339687808.36267.YahooMailNeo@web122601.mail.ne1.yahoo.com> <1339689345.89878.YahooMailNeo@web122604.mail.ne1.yahoo.com> <1339690750.91391.YahooMailNeo@web122604.mail.ne1.yahoo.com> <1339700087.18036.YahooMailNeo@web122603.mail.ne1.yahoo.com> <1339701576.88862.YahooMailNeo@web122605.mail.ne1.yahoo.com> Message-ID: On 6/14/2012 12:19 PM Tom Avos said... > Yes, If I open the file from the IDLE and then I press "f5" it works, > but I want to know why it does not work if I write the filename after > the ">>>" prompt . That way is much more handy, I would like to make it > work. Once you've got the >>> prompt you're executing python words, not file system file names. IDLE is a development environment and is not a run environment. Further, IDLE is or has been known to suffer from various 'leaks' that contribute to oddities when made to do as you're asking, so even when you do get it going, it's not a stable platform. I'd-like-apples-to-be-oranges-but-they're-not-ly y'rs, Emile From tomavos at yahoo.com.ar Thu Jun 14 22:27:29 2012 From: tomavos at yahoo.com.ar (Tom Avos) Date: Thu, 14 Jun 2012 13:27:29 -0700 (PDT) Subject: [Tutor] Trouble running Python on Windows Vista In-Reply-To: References: <1339687808.36267.YahooMailNeo@web122601.mail.ne1.yahoo.com> <1339689345.89878.YahooMailNeo@web122604.mail.ne1.yahoo.com> <1339690750.91391.YahooMailNeo@web122604.mail.ne1.yahoo.com> <1339700087.18036.YahooMailNeo@web122603.mail.ne1.yahoo.com> <1339701576.88862.YahooMailNeo@web122605.mail.ne1.yahoo.com> Message-ID: <1339705649.8845.YahooMailNeo@web122606.mail.ne1.yahoo.com> Thanks!! ________________________________ De: Emile van Sebille Para: tutor at python.org Enviado: jueves, 14 de junio de 2012 16:35 Asunto: Re: [Tutor] Trouble running Python on Windows Vista On 6/14/2012 12:19 PM Tom Avos said... > Yes, If I open the file from the IDLE and then I press "f5" it works, > but I want to know why it does not work if I write the filename after > the ">>>" prompt . That way is much more handy, I would like to make it > work. Once you've got the >>> prompt you're executing python words, not file system file names. IDLE is a development environment and is not a run environment. Further, IDLE is or has been known to suffer from various 'leaks' that contribute to oddities when made to do as you're asking, so even when you do get it going, it's not a stable platform. I'd-like-apples-to-be-oranges-but-they're-not-ly y'rs, Emile _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Jun 14 23:29:50 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 14 Jun 2012 22:29:50 +0100 Subject: [Tutor] Trouble running Python on Windows Vista In-Reply-To: <1339701576.88862.YahooMailNeo@web122605.mail.ne1.yahoo.com> References: <1339687808.36267.YahooMailNeo@web122601.mail.ne1.yahoo.com> <1339689345.89878.YahooMailNeo@web122604.mail.ne1.yahoo.com> <1339690750.91391.YahooMailNeo@web122604.mail.ne1.yahoo.com> <1339700087.18036.YahooMailNeo@web122603.mail.ne1.yahoo.com> <1339701576.88862.YahooMailNeo@web122605.mail.ne1.yahoo.com> Message-ID: On 14/06/12 20:19, Tom Avos wrote: > Yes, If I open the file from the IDLE and then I press "f5" it works, > but I want to know why it does not work if I write the filename after > the ">>>" prompt . That way is much more handy, I would like to make it > work. Its not really the same but you can just import it: >>> import triangulo # note no .py That will execute any runnable code in the file. But its not really what the IDLE >>> prompt is intended for. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From redacted@example.com Fri Jun 15 00:17:01 2012 From: redacted@example.com (Alexander Quest) Date: Thu, 14 Jun 2012 15:17:01 -0700 Subject: [Tutor] Notepad++ question In-Reply-To: <4FD15281.6080203@davea.name> References: <4FD15281.6080203@davea.name> Message-ID: Got it Dave- sorry about not sending it to the newsgroup as well. My question was regarding a piece of boilerplate code: if __name__ == '__main__': main() This calls the main function, but I don't understand what the 'if' statement is doing here. In the simple programs that I've seen this so far, there is no variable called "_name_", and even if there was, why is it comparing it to "_main_"? Why can't the main function just be called by typing main()- why do we need this if statement to precede it? Thanks. -Alex On Thu, Jun 7, 2012 at 6:16 PM, Dave Angel wrote: > On 06/07/2012 02:36 PM, Alexander Quest wrote: > > Ok, thanks guys. I also had one more quick question regarding a piece of > > boilerplate code: > > > > To get a response, you will needs to leave your question at the python > tutor newsgroup. We are part of a group, not offering private advice. > > Normally, you just do a Reply-all to one of the existing messages on the > thread, to include the group. But you can also type tutor at python.org > as a CC: > > I've been volunteering my time on various forums for over 25 years now, > so I think I speak for lots of other volunteers. > > > -- > > DaveA > -------------- next part -------------- An HTML attachment was scrubbed... URL: From redacted@example.com Fri Jun 15 00:18:56 2012 From: redacted@example.com (Alexander Quest) Date: Thu, 14 Jun 2012 15:18:56 -0700 Subject: [Tutor] Notepad++ question In-Reply-To: References: <4FD15281.6080203@davea.name> Message-ID: [Resending because I messed up on last email] My question was regarding a piece of boilerplate code: if __name__ == '__main__': main() This calls the main function, but I don't understand what the 'if' statement is doing here. In the simple programs that I've seen this so far, there is no variable called "_name_", and even if there was, why is it comparing it to "_main_"? Why can't the main function just be called by typing main()- why do we need this if statement to precede it? Thanks. -Alex On Thu, Jun 14, 2012 at 3:17 PM, Alexander Quest wrote: > Got it Dave- sorry about not sending it to the newsgroup as well. > > My question was regarding a piece of boilerplate code: > > > if __name__ == '__main__': > main() > > This calls the main function, but I don't understand what the 'if' > statement is doing here. In the simple programs that I've seen this so far, > there is no variable called "_name_", and even if there was, why is it > comparing it to "_main_"? Why can't the main function just be called by > typing main()- why do we need this if statement to precede it? Thanks. > > -Alex > > On Thu, Jun 7, 2012 at 6:16 PM, Dave Angel wrote: > >> On 06/07/2012 02:36 PM, Alexander Quest wrote: >> > Ok, thanks guys. I also had one more quick question regarding a piece of >> > boilerplate code: >> > >> >> To get a response, you will needs to leave your question at the python >> tutor newsgroup. We are part of a group, not offering private advice. >> >> Normally, you just do a Reply-all to one of the existing messages on the >> thread, to include the group. But you can also type tutor at python.org >> as a CC: >> >> I've been volunteering my time on various forums for over 25 years now, >> so I think I speak for lots of other volunteers. >> >> >> -- >> >> DaveA >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Fri Jun 15 01:08:32 2012 From: d at davea.name (Dave Angel) Date: Thu, 14 Jun 2012 19:08:32 -0400 Subject: [Tutor] Notepad++ question In-Reply-To: References: <4FD15281.6080203@davea.name> Message-ID: <4FDA6EF0.1080307@davea.name> On 06/14/2012 06:18 PM, Alexander Quest wrote: > [Resending because I messed up on last email] > > My question was regarding a piece of boilerplate code: > > if __name__ == '__main__': > main() > > This calls the main function, but I don't understand what the 'if' > statement is doing here. In the simple programs that I've seen this so far, > there is no variable called "_name_", and even if there was, why is it > comparing it to "_main_"? Why can't the main function just be called by > typing main()- why do we need this if statement to precede it? Thanks. > > -Alex > It may not matter for simple scripts. But many times the same file is used in two ways. When it's used as a script, __name__ is set to the literal "__main__" But when it's used as a module (ie. somebody imports it) then the name is set to the module name (filename without the .py) So, you normally do not want to execute your main() function unless it's being used as a script. This becomes very important when you want to reuse code, or when you want to write test cases for modules. -- DaveA From wprins at gmail.com Fri Jun 15 01:10:08 2012 From: wprins at gmail.com (Walter Prins) Date: Fri, 15 Jun 2012 00:10:08 +0100 Subject: [Tutor] Notepad++ question In-Reply-To: References: <4FD15281.6080203@davea.name> Message-ID: Hi Alex, On 14 June 2012 23:18, Alexander Quest wrote: > if __name__ == '__main__': > ? main() > > This calls the main function, but I don't understand what the 'if' statement > is doing here. In the simple programs that I've seen this so far, there is > no variable called "_name_", and even if there was, why is it comparing it > to "_main_"? Why can't the main function just be called by typing main()- > why do we need this if statement to precede it? Thanks. In short, consider the implications of the fact that your file, apart from being a program that can run standalone, might also be a Python module that might be used in another program. Oftentimes you want to write your Python code in such a way that when the module is run directly you want it to do something useful (such as run a main() function, e.g. maybe run some unit/self-tests or whatever), while when you import it for use in another program/module then you probably rather do *not* want it to run as if it is itself the "main program". So, in order to differentiate the 2 cases, there exists the above Python idiom. So, when a module is directly run as the "main program", then the name of the module being run, which is reflected by the variable __name__, made available by the Python interpreter, will be equal to "__main__", while when it's imported it will be equal to the module name. This allows your module to know when it's running whether it's running as the main program or just running because it's been imported by another module. Does that answer your question? Walter From emile at fenx.com Fri Jun 15 01:19:20 2012 From: emile at fenx.com (Emile van Sebille) Date: Thu, 14 Jun 2012 16:19:20 -0700 Subject: [Tutor] Notepad++ question In-Reply-To: References: <4FD15281.6080203@davea.name> Message-ID: On 6/14/2012 3:18 PM Alexander Quest said... > [Resending because I messed up on last email] > > My question was regarding a piece of boilerplate code: > > if __name__ == '__main__': > main() > __name__ within a python module is either '__main__' when invoked directly from a command line, or contains the module name. This allows the module to sense if it was imported or run directly and perform any relevant code that may depend on that. One common use is to run test code within libraries intended to be imported. This illustrates the difference: $ cat > test.py if __name__ == '__main__': print "__name__ == '__main__'" else: print "__name__ == '",__name__,"'" Emile at MIS2 ~ $ python test.py __name__ == '__main__' Emile at MIS2 ~ $ python Python 2.4.3 (#1, May 18 2006, 07:40:45) [GCC 3.3.3 (cygwin special)] on cygwin Type "help", "copyright", "credits" or "license" for more information. >>> import test __name__ == ' test ' >>> > This calls the main function, but I don't understand what the 'if' > statement is doing here. In the simple programs that I've seen this so > far, there is no variable called "_name_", and even if there was, why is > it comparing it to "_main_"? Why can't the main function just be called > by typing main()- why do we need this if statement to precede it? Thanks. > > -Alex From alan.gauld at btinternet.com Fri Jun 15 01:58:15 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 15 Jun 2012 00:58:15 +0100 Subject: [Tutor] Notepad++ question In-Reply-To: References: <4FD15281.6080203@davea.name> Message-ID: On 14/06/12 23:18, Alexander Quest wrote: > so far, there is no variable called "_name_", and even if there was, > why is it comparing it to "_main_"? Why can't the main function just Note that in both cases there are two '_' characters before and after the name. __name__ and '__main__'. Double underscores usually indicates a special variable used by Python internally. Thus __name__ is something that Python sets when the file is either imported or run, as others have already explained. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From redacted@example.com Fri Jun 15 07:45:54 2012 From: redacted@example.com (Alexander Quest) Date: Thu, 14 Jun 2012 22:45:54 -0700 Subject: [Tutor] Notepad++ question In-Reply-To: References: <4FD15281.6080203@davea.name> Message-ID: Thanks Walter; I believe I understand the reasoning behind it, though not all of the mechanics, but for now, your answer is more than sufficient. -Alex On Thu, Jun 14, 2012 at 4:10 PM, Walter Prins wrote: > Hi Alex, > > On 14 June 2012 23:18, Alexander Quest wrote: > > if __name__ == '__main__': > > main() > > > > This calls the main function, but I don't understand what the 'if' > statement > > is doing here. In the simple programs that I've seen this so far, there > is > > no variable called "_name_", and even if there was, why is it comparing > it > > to "_main_"? Why can't the main function just be called by typing main()- > > why do we need this if statement to precede it? Thanks. > > In short, consider the implications of the fact that your file, apart > from being a program that can run standalone, might also be a Python > module that might be used in another program. Oftentimes you want to > write your Python code in such a way that when the module is run > directly you want it to do something useful (such as run a main() > function, e.g. maybe run some unit/self-tests or whatever), while when > you import it for use in another program/module then you probably > rather do *not* want it to run as if it is itself the "main program". > So, in order to differentiate the 2 cases, there exists the above > Python idiom. So, when a module is directly run as the "main > program", then the name of the module being run, which is reflected by > the variable __name__, made available by the Python interpreter, will > be equal to "__main__", while when it's imported it will be equal to > the module name. This allows your module to know when it's running > whether it's running as the main program or just running because it's > been imported by another module. > > Does that answer your question? > > Walter > -------------- next part -------------- An HTML attachment was scrubbed... URL: From spawgi at gmail.com Fri Jun 15 13:44:56 2012 From: spawgi at gmail.com (spawgi at gmail.com) Date: Fri, 15 Jun 2012 17:14:56 +0530 Subject: [Tutor] Query - Where to put in global variables, if needed, as a good programming practice Message-ID: Hello, The point of good-bad-ness of global variables aside, if I needed to use them, which is a better place to put them. 1. In the __init__ function of a class? So they are available at the time an object is initialized or 2. In the actual function of the class where the variables are needed? Pros and Cons of either approach? Thanks and Regards, Sumod -- http://spawgi.wordpress.com We can do it and do it better. -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Fri Jun 15 14:30:29 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 15 Jun 2012 13:30:29 +0100 Subject: [Tutor] Query - Where to put in global variables, if needed, as a good programming practice In-Reply-To: References: Message-ID: On 15/06/2012 12:44, spawgi at gmail.com wrote: > Hello, > > The point of good-bad-ness of global variables aside, if I needed to use > them, which is a better place to put them. > 1. In the __init__ function of a class? So they are available at the time > an object is initialized or > 2. In the actual function of the class where the variables are needed? > Pros and Cons of either approach? > > Thanks and Regards, > Sumod > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor I don't understand why you would need global variables, and then promptly start discussing them wrt classes. Please explain what you are trying to achieve and I'm certain that we'll come up with the best solution for your use case. -- Cheers. Mark Lawrence. From wprins at gmail.com Fri Jun 15 14:48:58 2012 From: wprins at gmail.com (Walter Prins) Date: Fri, 15 Jun 2012 13:48:58 +0100 Subject: [Tutor] Query - Where to put in global variables, if needed, as a good programming practice In-Reply-To: References: Message-ID: Hi Spawgi, On 15 June 2012 12:44, wrote: > Hello, > > The point of good-bad-ness of global variables aside, if I needed to use > them, which is a better place to put them. > 1. In the __init__ function of a class? So they are available at the time an > object is initialized or Firstly note as a bit of an aside that member variables of an object (or perhaps of the class), are not considered global, at least not in the sense most people mean when they talk about "global" variables. I'm however assuming you're NOT referring to a variable that's actually either a class or object member variable in your question, but rather something akin to a true global. I say "akin", because Python does not really have true global variables in the sense that one might expect coming from some other langauges. The closest is probably the contents of the '__builtin__' module, which is the last searched when resolving names and hence practically speaking probably the closest to containing what one might describe as global variables, but I'd hazard to submit there will virtually without exception always be a better location for your variables than to plug them into the __builtin__ module. > 2. In the actual function of the class where the variables are needed? > Pros and Cons of either approach? OK, to state the obvious again, but if a variable is only needed in one method (function) of a class, then by definition you don't actually need a global. If it's needed in more than one method in an object, then you should probably consider an object member variable or suitably parameterizethe methods. That aside, to answer your question in a general fashion, initialisation should always happen in the initialization section (as appropriate) of the namespace that a variable is being declared/contained in. So package level varibles should be initialized in __init__.py of the package, module variables should be initialized in the module itself, prior to any other code that may refer to them obviously, and presumably towards the top of the module in most cases, class level variables should be initialized directly in the class prior to use, object level variables in the object initializer (__init__ method) and so on. So, your (presumably module) global variables should therefore *not* be initialized as part of the object initializer (what happens when you create multiple instances of your object?), object methods (what happens when you call the method multiple times?) or even plain functions (again, what happens when you call the method multiple times?) as you suggested, and assuming you're talking about module globals, they should in fact simply be initialized in the module, prior to use elsewhere in the module. (Use from other modules are possible by importing the module containing the module global variable from such other modules etc.) Walter From steve at pearwood.info Sat Jun 16 00:30:56 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 16 Jun 2012 08:30:56 +1000 Subject: [Tutor] Query - Where to put in global variables, if needed, as a good programming practice In-Reply-To: References: Message-ID: <4FDBB7A0.2040100@pearwood.info> spawgi at gmail.com wrote: > Hello, > > The point of good-bad-ness of global variables aside, if I needed to use > them, which is a better place to put them. > 1. In the __init__ function of a class? So they are available at the time > an object is initialized or > 2. In the actual function of the class where the variables are needed? > Pros and Cons of either approach? Neither of those are *global* variables. In Python, global variables are those at the top level of the module, and are only global to a single module, not your entire program. If your program is a single module, there is no difference. This confusion is one of the reasons that I hate the Java-ism of calling things-attached-to-classes-or-attributes as "variables" instead of members or attributes. In Python, the usual term for them is "attributes", and you can have class attributes shared between all instances of a class, and instance attributes that are specific to the instance. class K: shared = "this attribute is shared" def __init__(self): self.attribute = "this one is specific to the instance" Pros for class attributes: + they are shared, so all your instances see the same value + you can reach them directly from the class, without creating an instance first: K.shared works Cons for class attributes: - they are shared, so all your instances see the same value Pros for instance attributes: + they are not shared, so all your instances don't see the same value Cons for class attributes: - they are not shared, so all your instances don't see the same value - every time you create an instance, the attribute has to be created - you can't reach them directly from the class without creating an instance first: K.attribute does not work -- Steven From k4n0ne at gmail.com Sat Jun 16 04:48:47 2012 From: k4n0ne at gmail.com (Kanone Seele) Date: Sat, 16 Jun 2012 10:48:47 +0800 Subject: [Tutor] How does slicing work? Message-ID: Hello,I'm a beginner in python.I found some qustions in Chapter 9's Quiz of the book* Learning Python,4th ed*, >>> L = [1, 2, 3, 4] >>> L[-1000:100] [1, 2, 3, 4] >>> L[3:1] [] >>> L[3:1] = ['?'] >>> L [1, 2, 3, '?', 4] How does python get the result of slicing?Such as the examples above,they really confused me. -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Sat Jun 16 05:54:41 2012 From: emile at fenx.com (Emile van Sebille) Date: Fri, 15 Jun 2012 20:54:41 -0700 Subject: [Tutor] How does slicing work? In-Reply-To: References: Message-ID: On 6/15/2012 7:48 PM Kanone Seele said... > Hello,I'm a beginner in python.I found some qustions in Chapter 9's Quiz > of the book/Learning Python,4th ed/, > >> >> L = [1, 2, 3, 4] > >> >> L[-1000:100] > > [1, 2, 3, 4] > >> >> L[3:1] > > [] > >> >> L[3:1] = ['?'] > >> >> L > > [1, 2, 3, '?', 4] > > > How does python get the result of slicing?Such as the examples > above,they really confused me. > > There's some good examples at: http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation HTH, Emile From mnickey at gmail.com Sat Jun 16 07:44:37 2012 From: mnickey at gmail.com (Mike Nickey) Date: Fri, 15 Jun 2012 22:44:37 -0700 Subject: [Tutor] getpass Message-ID: Hey all, I'm working on a bit of code here and I'm having an issue with getpass. In the documentation, it says it hides the text entered "Prompt the user for a password without echoing." -- http://www.python.org/doc//current/library/getpass.html However when I run it in Eclipse, the password is clear as day. Here is a snippet of the code that I am working with. Any assistance would be greatly appreciated. ===== ... print 'Connected to Gmail' + '\n' try: gmail_user = str(raw_input('Enter your email: ')).lower().strip() gmail_pwd = getpass.getpass('Enter your email password: ').strip() smtpserver.login(gmail_user, gmail_pwd) -- ~MEN From steve at pearwood.info Sat Jun 16 08:21:02 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 16 Jun 2012 16:21:02 +1000 Subject: [Tutor] getpass In-Reply-To: References: Message-ID: <4FDC25CE.60902@pearwood.info> Mike Nickey wrote: > Hey all, > > I'm working on a bit of code here and I'm having an issue with > getpass. In the documentation, it says it hides the text entered > > "Prompt the user for a password without echoing." -- > http://www.python.org/doc//current/library/getpass.html > > However when I run it in Eclipse, the password is clear as day. Then don't run it in Eclipse. Seriously. Hiding the user's input requires getpass to work hand-in-hand with the terminal. If another piece of software, like Eclipse, gets in the way, or if the terminal is not one which is supported, then getpass falls back on whatever it can, which unfortunately displays the password. Idle also fails to work properly with getpass. If you run getpass in Idle for Python 2.7, the prompt is written back to the parent terminal, not the Idle window, where the user may not see it. In general, code which requires intimate knowledge of the terminal should be run directly from a terminal and not via an intermediate layer like Eclipse or Idle. -- Steven From alan.gauld at btinternet.com Sat Jun 16 10:32:53 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 16 Jun 2012 09:32:53 +0100 Subject: [Tutor] How does slicing work? In-Reply-To: References: Message-ID: On 16/06/12 03:48, Kanone Seele wrote: > Hello,I'm a beginner in python.I found some qustions in Chapter 9's Quiz > of the book/Learning Python,4th ed/, There's a bunch of math behind slicing but one of the best descriptions I found when I was learning Python was to think of the slice action as a pair of blades that slice between the list entries. Thus zero is before the first entry. -1 is one back from the end - ie the last element. So applying that to your examples lets see what we get: >>>> L = [1, 2, 3, 4] > >>>> L[-1000:100] > > [1, 2, 3, 4] One blade 1000 elements back from the end, the other 100 elements forward from the beginning. Between the blades we have the whole list... >>>> L[3:1] > > [] First blade in front of the second blade doesn't work. You can only cut 'backwards' using negative indexes (or the third index argument) eg: >>> L[-3:-1] [2,3] >>> L[3:1:-1] [4,3] Back to your examples: >>>> L[3:1] = ['?'] >>>> L > [1, 2, 3, '?', 4] OK, This one is a bit more confusing. The [3:1] slice above returns an empty slice between 3 and 4. The assignment operation replaces the slice with the assigned list. So in this case we replace the empty slice with the question mark. Here are some more simple examples working up to yours: L = [1,2,3,4] L[:0] = [-1] # insert before list L [-1, 1, 2, 3, 4] L[-1000:0]=[-2] # and again L [-2, -1, 1, 2, 3, 4] L[100:]=[5] # insert after list L [-2, -1, 1, 2, 3, 4, 5] L[3:6] = [6] # replace multiple members L [-2, -1, 1, 6, 5] L = [1,2,3,4] # reset list L[3:3] # empty slice [] L[3:3]=[3.5] # insert after 3 L [1, 2, 3, 3.5, 4] L[3:1]=['?'] # your example is effectively the same L [1, 2, 3, '?', 3.5, 4] HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sat Jun 16 10:39:50 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 16 Jun 2012 09:39:50 +0100 Subject: [Tutor] getpass In-Reply-To: References: Message-ID: On 16/06/12 06:44, Mike Nickey wrote: > "Prompt the user for a password without echoing." -- > http://www.python.org/doc//current/library/getpass.html > > However when I run it in Eclipse, the password is clear as day. Remember Eclipse is a development environment not aruntime environment. Like all IDEs it captures the stdin/stdout activity and displays 9or interprets!) it in its own way. Any time you are running I/O sensitive code (including GUIs) and you hit something strange always try it in the vanilla interpreter to make sure its not the IDE trying to be 'helpful' (or even just being buggy!!) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From k4n0ne at gmail.com Sat Jun 16 14:16:55 2012 From: k4n0ne at gmail.com (Kanone Seele) Date: Sat, 16 Jun 2012 20:16:55 +0800 Subject: [Tutor] How does slicing work? In-Reply-To: References: Message-ID: Thank you guys~ the two blades method really works well! And I think it'd be better to dive into the source code of Pytho.Well,it seems difficult for me now. -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Sat Jun 16 15:06:53 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 16 Jun 2012 14:06:53 +0100 Subject: [Tutor] How does slicing work? In-Reply-To: References: Message-ID: On 16/06/2012 13:16, Kanone Seele wrote: > Thank you guys~ the two blades method really works well! > And I think it'd be better to dive into the source code of Pytho.Well,it > seems difficult for me now. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor I suggest you read the tutorial before starting on source code. -- Cheers. Mark Lawrence. From mariocatch at gmail.com Sun Jun 17 03:45:19 2012 From: mariocatch at gmail.com (mariocatch) Date: Sat, 16 Jun 2012 21:45:19 -0400 Subject: [Tutor] Simple Python Address Book (Advice welcome!) Message-ID: Hello All, I'm currently in the midst of learning Python, and am absolutely loving the language thus far! I'm a .NET developer in my professional life, and have been recently influenced to learn Python - and I'm glad I was :) I am working on an address book application (input through raw_input(), nothing fancy) in the 2.7 redist. I'm essentially asking if someone wouldn't mind taking a look at what I have so far, and giving some advice on some of my weak areas, and areas I've marked with comments on how to better approach some of my solutions (in a more *Pythological* way :) ). I'm still familiarizing myself with Python, and I would greatly appreciate anyone's help in this task of mine :) Just a learning project for myself, one of many! I'm not sure how you would like the code, I'll simply paste it here since I imagine people are wear of opening attachments through email: import re # email validation import textwrap #wrapping text in raw_input (not sure if this is best way to do this?) # forward declarations addressBookPath = r'C:\temp\addresses.txt' addressBook = {} # address book held in memory before dumped to file emailFormatRegex = r'(\w[\w]*)@([\w]+\.[\w]+)' recordDelimiter = ' | ' # split name | email in file def LoadAddresses(): """ Loads all addresses from file and places them in tuple in form (bool, list), where the list is each row from the file (a person's name | email). """ success, lines = (False, []) try: f = open(addressBookPath, 'r') lines = f.readlines() f.close() success, lines = (True, lines) except IOError as e: print 'Error opening address book: ', e return (success, lines) def main(): (success, lines) = LoadAddresses() if not success: shouldMakeNewBook = raw_input(textwrap.fill("""You do not have an address book yet. Would you like to create one?""")) if shouldMakeNewBook in ('y', 'ye', 'yes'): f = open(addressBookPath, 'w') f.close() print 'New address book created.', addressBookPath else: print 'Exiting...' return else: # now that we have the file loaded into memory, # fill out the addressbook from file for line in lines: splitStr = line.split(recordDelimiter) addressBook[splitStr[0]] = splitStr[-1] # main input loop (break with 'q' or 'quit' during input) while True: newPersonNameInput = raw_input('Enter new person\'s name: (q/quit to stop):') if newPersonNameInput.lower() in ('q', 'quit'): break addressBook[newPersonNameInput] = newPersonNameInput while True: # loop until email is in valid format (x at y.z) newPersonEmailInput = raw_input('Enter new person\'s email: (q/quit to stop):') match = re.search(emailFormatRegex, newPersonEmailInput) if not match: print 'email validation failed... try again.' continue else: addressBook[newPersonNameInput] = newPersonEmailInput break # success RecordAddresses() print addressBook def RecordAddresses(): """ Writes out each address to the file in the form of name | email. """ f = open(addressBookPath, 'w') for k, v in sorted(addressBook.items()): #is there a better way to do this without placing a newline after each row? #it's causing multiple line separations when writing back out to file (because each # time we finish reading, it ends with a trailing newline). f.write("{0}{1}{2}\n".format(k, recordDelimiter, v)) f.close() if __name__ == '__main__': main() I appreciate any feedback! -catch -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sun Jun 17 10:08:48 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 17 Jun 2012 09:08:48 +0100 Subject: [Tutor] Simple Python Address Book (Advice welcome!) In-Reply-To: References: Message-ID: On 17/06/12 02:45, mariocatch wrote: > I'm essentially asking if someone wouldn't mind taking a look at what I > have so far, and giving some advice on some of my weak areas, Welcome, I've inserted a few comments below. In general it's OK although personally I'd probably put the email validation in a short helper function (including the regex definition etc) just to tidy it up and avoid one level of indentation. (Its also potentially reusable so I'd probably put that function in its own module too... I'd probably also put all the splitting logic into LoadAddresses so that what I get back is the actual address dictionary rather than the raw list of lines. That would clean up main() quite a bit. Also it's conventional to make function names start with lowerCase. UpperCase names usually imply it's a class. Finally, I'd put all the functions above main() rather than having the lonely looking RecordAddresses() at the end. > import re # email validation > import textwrap > # forward declarations > addressBookPath = r'C:\temp\addresses.txt' > addressBook = {} # address book held in memory before dumped to file > emailFormatRegex = r'(\w[\w]*)@([\w]+\.[\w]+)' > recordDelimiter = ' | ' # split name | email in file > def LoadAddresses(): > """ Loads all addresses from file and places > them in tuple in form (bool, list), where the list is each > row from the file (a person's name | email). """ > success, lines = (False, []) > try: > f = open(addressBookPath, 'r') > lines = f.readlines() > f.close() > success, lines = (True, lines) > except IOError as e: > print 'Error opening address book: ', e > return (success, lines) The latest style preference in Python is to use with statements for file handling, so that would become: try: with f as open(.....): success,lines = True, f.readlines() except IOError as e: print ... return (success,lines) 'with' guarantees file closure automatically. > def main(): > (success, lines) = LoadAddresses() > if not success: > shouldMakeNewBook = raw_input(textwrap.fill("""You do not have > an address book yet. > Would you like to > create one?""")) > if shouldMakeNewBook in ('y', 'ye', 'yes'): > f = open(addressBookPath, 'w') > f.close() > print 'New address book created.', addressBookPath > else: > print 'Exiting...' > return > else: > # now that we have the file loaded into memory, > # fill out the addressbook from file > for line in lines: > splitStr = line.split(recordDelimiter) > addressBook[splitStr[0]] = splitStr[-1] > # main input loop (break with 'q' or 'quit' during input) > while True: > newPersonNameInput = raw_input('Enter new person\'s name: > (q/quit to stop):') > if newPersonNameInput.lower() in ('q', 'quit'): > break > addressBook[newPersonNameInput] = newPersonNameInput > while True: # loop until email is in valid format (x at y.z > ) > newPersonEmailInput = raw_input('Enter new person\'s email: > (q/quit to stop):') > match = re.search(emailFormatRegex, newPersonEmailInput) > if not match: > print 'email validation failed... try again.' > continue continue is not strictly needed here, but does make the intent clear. > else: > addressBook[newPersonNameInput] = newPersonEmailInput > break # success > RecordAddresses() > print addressBook > def RecordAddresses(): > """ Writes out each address to the file in the form of name | > email. """ > f = open(addressBookPath, 'w') > for k, v in sorted(addressBook.items()): Should have a try in case the file fails to open. Also slightly more pythonic to do for k,v in open(....): There is no point in sorting the data if you are going to read it into a dictionary, Python's dictionaries are unsorted. (Unless you plan on reading the file manually - in a text editor say) > #is there a better way to do this without placing a newline > after each row? > #it's causing multiple line separations when writing back out > to file (because each > # time we finish reading, it ends with a trailing newline). > f.write("{0}{1}{2}\n".format(k, recordDelimiter, v)) Sorry, you have to add a newline when writing to the file and strip it off when reading. Its just how it is... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From wayne at waynewerner.com Sun Jun 17 13:57:08 2012 From: wayne at waynewerner.com (Wayne Werner) Date: Sun, 17 Jun 2012 06:57:08 -0500 (CDT) Subject: [Tutor] Simple Python Address Book (Advice welcome!) In-Reply-To: References: Message-ID: On Sun, 17 Jun 2012, Alan Gauld wrote: > On 17/06/12 02:45, mariocatch wrote: >> after each row? >> #it's causing multiple line separations when writing back out >> to file (because each >> # time we finish reading, it ends with a trailing newline). >> f.write("{0}{1}{2}\n".format(k, recordDelimiter, v)) > > Sorry, you have to add a newline when writing to the file and strip it off > when reading. Its just how it is... If you're using Python3 or from __future__ import print_function, you can do this print(k,v, sep=recordDelimiter, file=f) Which might make things a little cleaner. There's also the end parameter which defaults to end='\n' HTH, Wayne From selbyrowleycannon at ymail.com Sun Jun 17 16:14:35 2012 From: selbyrowleycannon at ymail.com (Selby Rowley-Cannon) Date: Sun, 17 Jun 2012 07:14:35 -0700 (PDT) Subject: [Tutor] Dictionaries Message-ID: <1339942475.51723.YahooMailNeo@web140205.mail.bf1.yahoo.com> Hello, ??? ??? ??? I am having a problem with a small application I am writing. I have to have the user input the key, then have the program output the value associated with it. A way to inform the user that the key they entered is not in the dictionary or somefing would be nice also. Fank you for your help, ??? ??? -Selby -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Sun Jun 17 16:34:36 2012 From: emile at fenx.com (Emile van Sebille) Date: Sun, 17 Jun 2012 07:34:36 -0700 Subject: [Tutor] Dictionaries In-Reply-To: <1339942475.51723.YahooMailNeo@web140205.mail.bf1.yahoo.com> References: <1339942475.51723.YahooMailNeo@web140205.mail.bf1.yahoo.com> Message-ID: On 6/17/2012 7:14 AM Selby Rowley-Cannon said... > Hello, > I am having a problem with a small application I am writing. I have to > have the user input the key, then have the program output the value > associated with it. A way to inform the user that the key they entered > is not in the dictionary or somefing would be nice also. > Fank you for your help, > -Selby It's easiest for us to help you when you've posted a copy of your code and the resulting traceback information showing the error you're getting. To be complete, include your platform and python version. Then we can provide you the specific help you need to keep you moving forward. You might also want to read the highly regarded article at http://www.catb.org/~esr/faqs/smart-questions.html Emile From martin at linux-ip.net Sun Jun 17 16:50:20 2012 From: martin at linux-ip.net (Martin A. Brown) Date: Sun, 17 Jun 2012 10:50:20 -0400 Subject: [Tutor] Dictionaries In-Reply-To: <1339942475.51723.YahooMailNeo@web140205.mail.bf1.yahoo.com> References: <1339942475.51723.YahooMailNeo@web140205.mail.bf1.yahoo.com> Message-ID: Greetings, : I am having a problem with a small application I am writing. I : have to have the user input the key, then have the program output : the value associated with it. A way to inform the user that the : key they entered is not in the dictionary or somefing would be : nice also. I would agree also with Emile--in the future, you will probably get a better answer if you are more specific in your question. This one is a pretty easy question to answer and show you some samples for how to approach this with code snippets. I will assume that userinput is a variable with the contents of interest that your user typed. I will use the variable d to represent some dictionary (any dictionary). if userinput in d: print 'Found', userinput, 'in dictionary, value was', d[userinput] else: print 'No entry' Depending on what else you are doing (besides printing), you might also look into the get() method of dictionaries: default = '' string_to_print = d.get(userinput,default) print 'You typed %s. The entry was %s.' % ( userinput, string_to_print) These are pretty common usage patterns for dictionaries, so you might benefit from looking at the other sorts of things that you can do with dictionaries. Good luck, -Martin -- Martin A. Brown http://linux-ip.net/ From alan.gauld at btinternet.com Sun Jun 17 18:29:07 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 17 Jun 2012 17:29:07 +0100 Subject: [Tutor] Dictionaries In-Reply-To: <1339942475.51723.YahooMailNeo@web140205.mail.bf1.yahoo.com> References: <1339942475.51723.YahooMailNeo@web140205.mail.bf1.yahoo.com> Message-ID: On 17/06/12 15:14, Selby Rowley-Cannon wrote: > I am having a problem with a small application I am writing. I have > to have the user input the key, then have the program output the value > associated with it. A way to inform the user that the key they entered > is not in the dictionary or somefing would be nice also. So what's the problem? This sounds a wee bit like a homework assignment and we have a policy of not doing those for you... If you show us what you've done so far, and any errors then you can ask for more specific help. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From selbyrowleycannon at ymail.com Sun Jun 17 20:26:25 2012 From: selbyrowleycannon at ymail.com (Selby Rowley-Cannon) Date: Sun, 17 Jun 2012 11:26:25 -0700 (PDT) Subject: [Tutor] Dictionary Message-ID: <1339957585.35682.YahooMailNeo@web140206.mail.bf1.yahoo.com> Version: 2.7 OS: Ubuntu 12.04 LTS I am writing a small translation app for Rydish (A language that exists in the same way Klingon does, invented by my company for a[n] RPG). Here is my current method of translation: Edictionary = {'English keys':'Rydish values'} TextEng = raw_input('Please enter your text: ') if TextEng in Edictionary: ??? print(TextEng + ' traslates to ' + Edictionary[TextEng]) But I have found that this is only going to translate one word at a time. I thought about a loop of somesort, but I can't seem to find one that won't still force the user to translate one word at a time. Can anyone tell me how to translate a full sentance/several sentances? -------------- next part -------------- An HTML attachment was scrubbed... URL: From eire1130 at gmail.com Sun Jun 17 21:38:42 2012 From: eire1130 at gmail.com (James Reynolds) Date: Sun, 17 Jun 2012 15:38:42 -0400 Subject: [Tutor] Dictionary In-Reply-To: <1339957585.35682.YahooMailNeo@web140206.mail.bf1.yahoo.com> References: <1339957585.35682.YahooMailNeo@web140206.mail.bf1.yahoo.com> Message-ID: Does this language have grammar independent of english? If no, just use .split() on the string and loop through that. If yes, well, its much more complicated On Jun 17, 2012 2:27 PM, "Selby Rowley-Cannon" wrote: > Version: 2.7 > OS: Ubuntu 12.04 LTS > > I am writing a small translation app for Rydish (A language that exists in > the same way Klingon does, invented by my company for a[n] RPG). > Here is my current method of translation: > > Edictionary = {'English keys':'Rydish values'} > TextEng = raw_input('Please enter your text: ') > if TextEng in Edictionary: > print(TextEng + ' traslates to ' + Edictionary[TextEng]) > > But I have found that this is only going to translate one word at a time. > I thought about a loop of somesort, but I can't seem to find one that won't > still force the user to translate one word at a time. Can anyone tell me > how to translate a full sentance/several sentances? > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kendy at kendy.org Mon Jun 18 01:38:57 2012 From: kendy at kendy.org (kendy at kendy.org) Date: Sun, 17 Jun 2012 16:38:57 PDT Subject: [Tutor] python varying mulitple inheritance Message-ID: <45043.1339976337@speakeasy.net> Hello I'm new to classes. And I hope the my question isn't too big. I have electronic test equipment, but thought that a boat example would be easier. Can you help me untangle my class design? My problem is basically multiple inheritance, but I want to be flexible for how many will inherit. I'll put my example below and also as an attachment. I'm running Python 2.7 on Linux. Python is the BEST! Thanks Ken ------------ cut here ------------ #!/usr/bin/env python """ Variable Multiple Inheritance. email: kendy at kendy.org A boat has a body and is powered by any combination of Battery and/or Solar and/or Gas or None. If a boat is powered, it should have only one emergency_shutoff. """ class Body: """ A boat has a body """ def __init__(self, length): self.length = length class Power: """ Super Base class If a boat is powered. """ def __init__(self, emergency_shutoff): self.emergency_shutoff = emergency_shutoff def Emergency(): emergency_shutoff = True class Battery(Power): """ sub Child class """ def __init__(self, emergency_shutoff, battery_volt): self.battery_volt = battery_volt Power.__init__(self, emergency_shutoff) def ChargeBattery(): pass class Solar(Power): """ sub Child class """ def __init__(self, emergency_shutoff, solar_volt): self.solar_volt = solar_volt Power.__init__(self, emergency_shutoff) def DoSomethingSolar(): pass class Gasoline(Power): """ sub Child class """ def __init__(self, emergency_shutoff, liters): self.liters = liters Power.__init__(self, emergency_shutoff) class Boat1(Body): """ Sailboat """ pass class Boat2(Body, Gasoline, Battery): """ Gas, with battery backup powered """ def __init__(self, length, emergency_shutoff, liters, battery_volt): Body.__init__(self, length) Power.__init__(self, emergency_shutoff) Gasoline.__init__(self, emergency_shutoff, liters) Battery.__init__(self, emergency_shutoff, battery_volt) class Boat3(Body, Battery, Solar): """ Electric powered """ def __init__(self, length, emergency_shutoff, solar_volt, battery_volt): Body.__init__(self, length) Power.__init__(self, emergency_shutoff) Solar.__init__(self, emergency_shutoff, solar_volt) Battery.__init__(self, emergency_shutoff, battery_volt) def ShowAssembly(assembly_queue): for workon in assembly_queue: pass def main(): """ Acts like a container class""" assembly_queue = [] # A list of boats to be assembled, in order. assembly_queue.append(Boat2(30, False, 'half-full', 50)) assembly_queue.append(Boat3(25, False, 30, 10)) assembly_queue.append(Boat1(20)) ShowAssembly(assembly_queue) if __name__ == '__main__': main() ------------ cut here ------------ -------------- next part -------------- A non-text attachment was scrubbed... Name: vary_mult_inherit.py Type: application/octet-stream Size: 2454 bytes Desc: not available URL: From steve at pearwood.info Mon Jun 18 03:36:20 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 18 Jun 2012 11:36:20 +1000 Subject: [Tutor] python varying mulitple inheritance In-Reply-To: <45043.1339976337@speakeasy.net> References: <45043.1339976337@speakeasy.net> Message-ID: <4FDE8614.9010905@pearwood.info> kendy at kendy.org wrote: > Hello > > I'm new to classes. And I hope the my question isn't too big. > > I have electronic test equipment, but thought that a boat example > would be easier. Can you help me untangle my class design? > My problem is basically multiple inheritance, but I want to be > flexible for how many will inherit. Your first problem is that you are misusing inheritance when you should be using composition instead. Inheritance is for "is-a" relationships. For example: Bob is a Person, so Bob should be an instance of Person class. A Person is a Mammal, so Person should inherit from Mammal class. A Mammal is an Animal, so Mammal should inherit from Animal class. Bob has a wallet, he is not a wallet himself. So the Bob instance should have an attribute "wallet", storing an instance of Wallet class. This is called composition. In your example, you have a Boat class. Boat is pretty generic, so it might be the top-level class. Or it might be a subclass of Vehicle. You might then subclass Boat to give SailingBoat, MotorBoat, SpeedBoat, Battleship, etc. But boats are not kinds of engines, so it is inappropriate for Boat to inherit from Engine. A boat (well, at least some boats) has an engine, so you might do something like this: class Boat: pass class SailBoat(Boat): def __init__(self, sails): self.sails = sails class RowBoat(Boat): def __init__(self, oars): self.oars = oars class PowerBoat(Boat): def __init__(self, engine): self.engine = engine class SpeedBoat(PowerBoat): """A SpeedBoat is a kind of PowerBoat with an outboard motor.""" def __init__(self): super(SpeedBoat, self).__init__(outboard_motor) class Yacht(SailBoat): """A Yacht is a kind of SailBoat.""" # Note that the name Yacht is ambiguous, as it is also used for # a class of small power boats! def __init__(self): super(SailBoat, self).__init__(spinnaker) A boat with both sails and an engine would then use multiple inheritance to inherit from both PowerBoat and SailBoat; it would have both an engine and a sail. Note that when using single-inheritance, you have a choice in how to call the parent classes' method: class K(C): def method(self, arg): result = C.method(self, arg) # Call the parent method directly. process(result) or use super: class K(C): def method(self, arg): result = super(K, self).method(arg) # Let Python find the method. process(result) Either way is fine. BUT once you intend to use multiple inheritance, you MUST use super, or your code risks being buggy. NEVER call a superclass method directly using multiple inheritance, or even if you might someday want to use multiple inheritance. Always use super instead. (Note: in Python 3, you can simplify the call to super to just super() instead of super(K, self). This does not work in Python 2.) Multiple inheritance is quite tricky to get right, which is why many programming languages prohibit it. If you google for "multiple inheritance python" you will find many discussions about it, explaining the pros and cons, difficulties and alternatives. One last thing: you will save yourself a lot of pain in the future if you stick to a consistent convention for naming things. The usual convention used in Python is the classes are named with an initial Capital letter, using so-called "CamelCase" if the name has multiple words. Methods, functions, attributes and instances are named in lowercase, using underscores to separate words: bob_the_builder = Builder(name='Bob'). For more about the suggested (but not compulsory) naming conventions, see this: http://www.python.org/dev/peps/pep-0008/ -- Steven From bgailer at gmail.com Mon Jun 18 17:17:24 2012 From: bgailer at gmail.com (bob gailer) Date: Mon, 18 Jun 2012 11:17:24 -0400 Subject: [Tutor] Dictionary In-Reply-To: <1339957585.35682.YahooMailNeo@web140206.mail.bf1.yahoo.com> References: <1339957585.35682.YahooMailNeo@web140206.mail.bf1.yahoo.com> Message-ID: <4FDF4684.10106@gmail.com> On 6/17/2012 2:26 PM, Selby Rowley-Cannon wrote: [snip] Do you have any programming (algorithm development) experience? Do you want to translate words independent of context? -- Bob Gailer 919-636-4239 Chapel Hill NC From ramit.prasad at jpmorgan.com Mon Jun 18 18:28:50 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Mon, 18 Jun 2012 16:28:50 +0000 Subject: [Tutor] Simple Python Address Book (Advice welcome!) In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF47413233DF4@SCACMX008.exchad.jpmchase.net> > > def LoadAddresses(): > > """ Loads all addresses from file and places > > them in tuple in form (bool, list), where the list is each > > row from the file (a person's name | email). """ > > success, lines = (False, []) > > try: > > f = open(addressBookPath, 'r') > > lines = f.readlines() > > f.close() > > success, lines = (True, lines) > > except IOError as e: > > print 'Error opening address book: ', e > > return (success, lines) > > The latest style preference in Python is to use with statements for file > handling, so that would become: > > try: > with f as open(.....): > success,lines = True, f.readlines() > except IOError as e: > print ... > return (success,lines) > > def main(): > > > (success, lines) = LoadAddresses() > > if not success: No real reason to return success as you can do this instead for the same effect. The only difference would be on reading an empty file. Not sure what you would want it to do... def main(): lines = LoadAddressses() if not lines: Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 ramit.prasad at jpmorgan.com Mon Jun 18 18:31:01 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Mon, 18 Jun 2012 16:31:01 +0000 Subject: [Tutor] Dictionaries In-Reply-To: References: <1339942475.51723.YahooMailNeo@web140205.mail.bf1.yahoo.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF47413233E17@SCACMX008.exchad.jpmchase.net> > This sounds a wee bit like a homework assignment and we have a policy of > not doing those for you... Just to clarify, we will help you on it but not do it for you. Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 torkamani at gmail.com Mon Jun 18 21:50:36 2012 From: torkamani at gmail.com (Ali Torkamani) Date: Mon, 18 Jun 2012 15:50:36 -0400 Subject: [Tutor] Installing NumPy Message-ID: Hi Every body, I'm trying to install NumPy on Python 2.7 for windows 32. But have had no success so far. I appreciate any help. Thanks, Ali -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Mon Jun 18 23:15:55 2012 From: emile at fenx.com (Emile van Sebille) Date: Mon, 18 Jun 2012 14:15:55 -0700 Subject: [Tutor] Installing NumPy In-Reply-To: References: Message-ID: On 6/18/2012 12:50 PM Ali Torkamani said... > Hi Every body, > > I'm trying to install NumPy on Python 2.7 for windows 32. But have had > no success so far. Hmmm. I have Active State's version 2.7 installed on the machine I';m in front of today. Launched and confirmed no numpy installed. Then I downloaded and ran http://pypi.python.org/packages/2.7/n/numpy/numpy-1.6.2.win32-py2.7.exe and I can now import numpy. What did you try? Emile From torkamani at gmail.com Mon Jun 18 23:35:59 2012 From: torkamani at gmail.com (Ali Torkamani) Date: Mon, 18 Jun 2012 17:35:59 -0400 Subject: [Tutor] Installing NumPy In-Reply-To: References: Message-ID: Thanks. I installed active python and then ran numpy package installer, and now it seems liek I can import numpy. Previously I had installed: http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi but there should be a problem connecting this one with numpy. Thanks any way, it's working now! Ali On Mon, Jun 18, 2012 at 5:15 PM, Emile van Sebille wrote: > On 6/18/2012 12:50 PM Ali Torkamani said... > > Hi Every body, >> >> I'm trying to install NumPy on Python 2.7 for windows 32. But have had >> no success so far. >> > > > Hmmm. I have Active State's version 2.7 installed on the machine I';m in > front of today. Launched and confirmed no numpy installed. Then I > downloaded and ran http://pypi.python.org/**packages/2.7/n/numpy/numpy-1.* > *6.2.win32-py2.7.exeand I can now import numpy. > > What did you try? > > Emile > > ______________________________**_________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From scarolan at gmail.com Mon Jun 18 23:52:13 2012 From: scarolan at gmail.com (Sean Carolan) Date: Mon, 18 Jun 2012 16:52:13 -0500 Subject: [Tutor] Help with Matplotlib labels Message-ID: I'm working on a simple python web app that generates graphs, because managers love graphs. I've got it about 90% done, but I'm having trouble getting labels onto my stacked graph. In the matplotlib documentation there is a nice example showing how to create the legend. Note how the variables p1 and p2 are used to generate the legend in this example: http://matplotlib.sourceforge.net/examples/pylab_examples/bar_stacked.html p1 = plt.bar(ind, menMeans, width, color='r', yerr=womenStd) p2 = plt.bar(ind, womenMeans, width, color='y', bottom=menMeans, yerr=menStd) plt.ylabel('Scores') plt.title('Scores by group and gender') plt.xticks(ind+width/2., ('G1', 'G2', 'G3', 'G4', 'G5') ) plt.yticks(np.arange(0,81,10)) plt.legend( (p1[0], p2[0]), ('Men', 'Women') ) Unfortunately my graph is generated dynamically. How can I create my legend when my 'bar' objects have no names to refer to? for admin in bd: bar(ind, bd[admin], width, color=colordict[admin]) xticks(ind+width/2., datenames) legend() grid('on') outfile = 'testfile.png' savefig('/var/www/'+outfile) return outfile From scarolan at gmail.com Tue Jun 19 01:13:54 2012 From: scarolan at gmail.com (Sean Carolan) Date: Mon, 18 Jun 2012 18:13:54 -0500 Subject: [Tutor] Help with Matplotlib labels In-Reply-To: References: Message-ID: > Unfortunately my graph is generated dynamically. How can I create my > legend when my 'bar' objects have no names to refer to? I also noticed another issue with my stacked bar graph; the total height of the bar is the size of the largest number of the dataset, and not the total of all the individual items. Anyone matplotlib experts out there who can weigh in? From alan.gauld at btinternet.com Tue Jun 19 01:46:07 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 19 Jun 2012 00:46:07 +0100 Subject: [Tutor] Help with Matplotlib labels In-Reply-To: References: Message-ID: On 19/06/12 00:13, Sean Carolan wrote: > and not the total of all the individual items. Anyone matplotlib > experts out there who can weigh in? Not me, but I notice there is a gmane newsfeed for matplotlib: gmane.comp.python.matplotlib.general Probably worth posting questions there. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From scarolan at gmail.com Tue Jun 19 02:19:39 2012 From: scarolan at gmail.com (Sean Carolan) Date: Mon, 18 Jun 2012 19:19:39 -0500 Subject: [Tutor] Help with Matplotlib labels In-Reply-To: References: Message-ID: > Not me, but I notice there is a gmane newsfeed for matplotlib: > > gmane.comp.python.matplotlib.general > > Probably worth posting questions there. Thank you, I will inquire on the newsfeed. From scarolan at gmail.com Tue Jun 19 03:26:36 2012 From: scarolan at gmail.com (Sean Carolan) Date: Mon, 18 Jun 2012 20:26:36 -0500 Subject: [Tutor] Help with Matplotlib labels In-Reply-To: References: Message-ID: > Unfortunately my graph is generated dynamically. How can I create my > legend when my 'bar' objects have no names to refer to? > > ? ?for admin in bd: > ? ? ? ?bar(ind, bd[admin], width, color=colordict[admin]) > ? ?xticks(ind+width/2., datenames) > ? ?legend() > ? ?grid('on') > ? ?outfile = 'testfile.png' > ? ?savefig('/var/www/'+outfile) > ? ?return outfile I found the solution to this problem, one of the optional kwargs for the pyplyt.bar() function is "label". Once I assigned that, legend() worked fine. From scarolan at gmail.com Tue Jun 19 04:10:06 2012 From: scarolan at gmail.com (Sean Carolan) Date: Mon, 18 Jun 2012 21:10:06 -0500 Subject: [Tutor] Help with Matplotlib labels In-Reply-To: References: Message-ID: > I also noticed another issue with my stacked bar graph; the total > height of the bar is the size of the largest number of the dataset, > and not the total of all the individual items. ?Anyone matplotlib > experts out there who can weigh in? I figured out what was going on here; the bars were all rendering on top of one another. Adjustments to the "bottom" parameter have fixed the issue. From kendy at kendy.org Tue Jun 19 07:03:45 2012 From: kendy at kendy.org (kendy at kendy.org) Date: Mon, 18 Jun 2012 22:03:45 PDT Subject: [Tutor] python varying mulitple inheritance Message-ID: <5830.1340082225@speakeasy.net> Thank you Steve, for the pointing me in a smarter direction! My immediate homework is: - Practice consistent conventions for naming things -- pep-0008. - Learn composition/has-a. I thought of a better way to think of my problem. A list element would contain the things that Bob has in his pockets, at a given moment. For example: Bob has 6 things. They are well known things and it's easy to make a class for each of them. But Bob could have 2 to the power of 6, combinations of those things in his pockets. I don't want to make 64 classes, to cover each possible combination. I would read from a file to know what Bob has: time wallet keys comb USB cell_phone work_badge 0 0 0 0 0 0 0 1 1 1 0 0 0 1 2 0 0 0 0 1 0 3 0 0 0 0 1 1 4 1 1 1 1 0 0 ... 0 = not in his pocket 1 = has-a in his pocket I don't want an instance of something, if it's not in Bob's pocket at that time. (Maybe I'm making it harder than it needs to be.) If no one responds, it's OK because now I know where to focus. (But help would be welcome.) I was told that you guys were great. You are!!! Ken On Sun Jun 17 18:36 , Steven D"Aprano sent: >kendy at kendy.org wrote: >> Hello >> >> I'm new to classes. And I hope the my question isn't too big. >> >> I have electronic test equipment, but thought that a boat example >> would be easier. Can you help me untangle my class design? >> My problem is basically multiple inheritance, but I want to be >> flexible for how many will inherit. > >Your first problem is that you are misusing inheritance when you should be >using composition instead. >http://www.python.org/dev/peps/pep-0008/ From kendy at kendy.org Tue Jun 19 07:14:52 2012 From: kendy at kendy.org (kendy at kendy.org) Date: Mon, 18 Jun 2012 22:14:52 PDT Subject: [Tutor] python varying mulitple inheritance Message-ID: <7263.1340082892@speakeasy.net> Hmmm. No need to write the whole thing written out. Just how not to code: class Pocket1(Wallet, Keys, Comb, Usb, CellPhone, WorkBadge): class Pocket2(Wallet, Keys, Comb, Usb, CellPhone): class Pocket3(Wallet, Keys, Comb, Usb, WorkBadge): class Pocket4(Wallet, Keys, Comb, Usb): class Pocket5(Wallet, Keys, Comb, CellPhone, WorkBadge): class Pocket6(Wallet, Keys, Comb, CellPhone): ... On Mon Jun 18 22:03 , sent: >Thank you Steve, for the pointing me in a smarter direction! > >My immediate homework is: >- Practice consistent conventions for naming things -- pep-0008. >- Learn composition/has-a. > >I thought of a better way to think of my problem. A list element would contain >the things that Bob has in his pockets, at a given moment. For example: > >Bob has 6 things. They are well known things and it's easy to make a class for >each of them. But Bob could have 2 to the power of 6, combinations of those >things in his pockets. I don't want to make 64 classes, to cover each possible >combination. I would read from a file to know what Bob has: > >time wallet keys comb USB cell_phone work_badge >0 0 0 0 0 0 0 >1 1 1 0 0 0 1 >2 0 0 0 0 1 0 >3 0 0 0 0 1 1 >4 1 1 1 1 0 0 >... > > > 0 = not in his pocket > 1 = has-a in his pocket > >I don't want an instance of something, if it's not in Bob's pocket at that time. >(Maybe I'm making it harder than it needs to be.) > >If no one responds, it's OK because now I know where to focus. (But help would be >welcome.) > >I was told that you guys were great. You are!!! > >Ken > > >On Sun Jun 17 18:36 , Steven D"Aprano sent: > >>kendy at kendy.org wrote: >>> Hello >>> >>> I'm new to classes. And I hope the my question isn't too big. >>> >>> I have electronic test equipment, but thought that a boat example >>> would be easier. Can you help me untangle my class design? >>> My problem is basically multiple inheritance, but I want to be >>> flexible for how many will inherit. >> >>Your first problem is that you are misusing inheritance when you should be >>using composition instead. > > >>http://www.python.org/dev/peps/pep-0008/ > >_______________________________________________ >Tutor maillist - Tutor at python.org >To unsubscribe or change subscription options: >http://mail.python.org/mailman/listinfo/tutor From ranjithtenz at gmail.com Tue Jun 19 07:22:37 2012 From: ranjithtenz at gmail.com (Ranjith Kumar) Date: Tue, 19 Jun 2012 10:52:37 +0530 Subject: [Tutor] Pymongo Error Message-ID: Hi all, I tried Django with Mongodb while running manage.py syncdb I endup with this error note : it works fine with sqlite and mysql db (django-1.3)ranjith at ranjith:~/ sandbox/python-box/hukkster-core-site/hukk$ ./manage.py syncdb /home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/pymongo/connection.py:385: UserWarning: must provide a username and password to authenticate to hukkster_testing "to authenticate to %s" % (db,)) Creating tables ... Traceback (most recent call last): File "./manage.py", line 14, in execute_manager(settings) File "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager utility.execute() File "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv self.execute(*args, **options.__dict__) File "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute output = self.handle(*args, **options) File "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/base.py", line 351, in handle return self.handle_noargs(**options) File "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 109, in handle_noargs emit_post_sync_signal(created_models, verbosity, interactive, db) File "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/sql.py", line 190, in emit_post_sync_signal interactive=interactive, db=db) File "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 172, in send response = receiver(signal=self, sender=sender, **named) File "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 41, in create_permissions "content_type", "codename" File "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/db/models/query.py", line 107, in _result_iter self._fill_cache() File "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/db/models/query.py", line 772, in _fill_cache self._result_cache.append(self._iter.next()) File "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/db/models/query.py", line 959, in iterator for row in self.query.get_compiler(self.db).results_iter(): File "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/djangotoolbox/db/basecompiler.py", line 229, in results_iter for entity in self.build_query(fields).fetch(low_mark, high_mark): File "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/djangotoolbox/db/basecompiler.py", line 290, in build_query query.order_by(self._get_ordering()) File "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/djangotoolbox/db/basecompiler.py", line 339, in _get_ordering raise DatabaseError("Ordering can't span tables on non-relational backends (%s)" % order) django.db.utils.DatabaseError: Ordering can't span tables on non-relational backends (content_type__app_label) DB settings in settings.py DATABASES = { 'default': { 'ENGINE': 'django_mongodb_engine', 'NAME': 'helloworld', 'USER': '', 'PASSWORD': '12424214', 'HOST': 'mongodb://staff.mongohq.com/', 'PORT': 'XXXXX', }, } my requirement packages, Django==1.3 dictshield==0.4.4 django-mongodb-engine==0.4.0 django-social-auth==0.6.9 djangotoolbox==0.0.1 httplib2==0.7.4 mongoengine==0.6.10 mongokit==0.8 oauth2==1.5.211 pymongo==2.2 python-openid==2.2.5 simplejson==2.5.2 wsgiref==0.1.2 Please point me what i missed here... -- Cheers, Ranjith Kumar K, Chennai. http://ranjithtenz.wordpress.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From johangeld at gmail.com Tue Jun 19 07:37:54 2012 From: johangeld at gmail.com (Johan Geldenhuys) Date: Tue, 19 Jun 2012 15:37:54 +1000 Subject: [Tutor] Writing to Windows 64-bit event log Message-ID: Hi there, I've looked all over, but couldn't find any help as far as an API goes to log to a 64-bit Windows7 machine event log. My Python version is 2.7.1 and I am running the 32-bit Windows version on a Windows 7 laptop. Is there a way I can log entries to the Windows event log on my laptop from my Python application? Thanks Johan -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Jun 19 09:38:55 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 19 Jun 2012 08:38:55 +0100 Subject: [Tutor] python varying mulitple inheritance In-Reply-To: <7263.1340082892@speakeasy.net> References: <7263.1340082892@speakeasy.net> Message-ID: On 19/06/12 06:14, kendy at kendy.org wrote: > Hmmm. No need to write the whole thing written out. Just how not to code: > > class Pocket1(Wallet, Keys, Comb, Usb, CellPhone, WorkBadge): > class Pocket2(Wallet, Keys, Comb, Usb, CellPhone): > class Pocket3(Wallet, Keys, Comb, Usb, WorkBadge): > class Pocket4(Wallet, Keys, Comb, Usb): > class Pocket5(Wallet, Keys, Comb, CellPhone, WorkBadge): > class Pocket6(Wallet, Keys, Comb, CellPhone): > ... Back to basics. a class is a definition of a *type* of thing, not a particular case. A Pocket is a general purpose container. Each instance can hold many things. So you would normally expect one pocket class and 6 instances. Each instance holding a different list of items. The only time you need a different Pocket class is where the pocket has different *behaviour*. It is normally the behaviours that differentiate classes not the data. The class data is there to support the class behaviour. The behaviour of a Pocket is probably some thing like: addItem removeItem findItem countItems open close empty In most implementations a Pocket will look a lot like a list... HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From mail at timgolden.me.uk Tue Jun 19 09:49:23 2012 From: mail at timgolden.me.uk (Tim Golden) Date: Tue, 19 Jun 2012 08:49:23 +0100 Subject: [Tutor] Writing to Windows 64-bit event log In-Reply-To: References: Message-ID: <4FE02F03.5060308@timgolden.me.uk> On 19/06/2012 06:37, Johan Geldenhuys wrote: > I've looked all over, but couldn't find any help as far as an API goes > to log to a 64-bit Windows7 machine event log. There are functions available in the pywin32 package under the win32evtlog module. I have wrapped some of them in my winsys package. Some examples here: http://winsys.readthedocs.org/en/latest/cookbook/event_logs.html TJG From alan.gauld at btinternet.com Tue Jun 19 09:49:35 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 19 Jun 2012 08:49:35 +0100 Subject: [Tutor] Writing to Windows 64-bit event log In-Reply-To: References: Message-ID: On 19/06/12 06:37, Johan Geldenhuys wrote: > I've looked all over, but couldn't find any help as far as an API goes > to log to a 64-bit Windows7 machine event log. I don't know if your search included MSDN but that would be my third port of call after the standard library and Google... There will be a Windows API call and you can access those via ctypes or Pythonwin. There is almost certainly a Windows Knowledge Base article too, use the MSDN search to find it... hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From breamoreboy at yahoo.co.uk Tue Jun 19 15:30:46 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 19 Jun 2012 14:30:46 +0100 Subject: [Tutor] Help with Matplotlib labels In-Reply-To: References: Message-ID: On 19/06/2012 00:46, Alan Gauld wrote: > On 19/06/12 00:13, Sean Carolan wrote: > >> and not the total of all the individual items. Anyone matplotlib >> experts out there who can weigh in? > > Not me, but I notice there is a gmane newsfeed for matplotlib: > > gmane.comp.python.matplotlib.general > > Probably worth posting questions there. > > No probably about it, I've asked questions there and like all Python lists found them extremely friendly and helpful. -- Cheers. Mark Lawrence. From kwpolska at gmail.com Tue Jun 19 15:33:28 2012 From: kwpolska at gmail.com (Kwpolska) Date: Tue, 19 Jun 2012 15:33:28 +0200 Subject: [Tutor] Pymongo Error In-Reply-To: References: Message-ID: For some reason, GMail sent my response to Ranjith and not to the whole list. Fixing with his response added. On Tue, Jun 19, 2012 at 7:22 AM, Ranjith Kumar wrote: > Hi all, > I tried Django with Mongodb while running manage.py syncdb I endup with this > error > > note : it works fine with sqlite and mysql db > > (django-1.3)ranjith at ranjith:~/ > sandbox/python-box/hukkster-core-site/hukk$ ./manage.py syncdb > /home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/pymongo/connection.py:385: > UserWarning: must provide a username and password to authenticate to > hukkster_testing > ? "to authenticate to %s" % (db,)) > Creating tables ... > Traceback (most recent call last): > ? File "./manage.py", line 14, in > ??? execute_manager(settings) > ? File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/__init__.py", > line 438, in execute_manager > ??? utility.execute() > ? File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/__init__.py", > line 379, in execute > ??? self.fetch_command(subcommand).run_from_argv(self.argv) > ? File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/base.py", > line 191, in run_from_argv > ??? self.execute(*args, **options.__dict__) > ? File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/base.py", > line 220, in execute > ??? output = self.handle(*args, **options) > ? File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/base.py", > line 351, in handle > ??? return self.handle_noargs(**options) > ? File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", > line 109, in handle_noargs > ??? emit_post_sync_signal(created_models, verbosity, interactive, db) > ? File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/sql.py", > line 190, in emit_post_sync_signal > ??? interactive=interactive, db=db) > ? File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", > line 172, in send > ??? response = receiver(signal=self, sender=sender, **named) > ? File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", > line 41, in create_permissions > ??? "content_type", "codename" > ? File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/db/models/query.py", > line 107, in _result_iter > ??? self._fill_cache() > ? File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/db/models/query.py", > line 772, in _fill_cache > ??? self._result_cache.append(self._iter.next()) > ? File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/db/models/query.py", > line 959, in iterator > ??? for row in self.query.get_compiler(self.db).results_iter(): > ? File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/djangotoolbox/db/basecompiler.py", > line 229, in results_iter > ??? for entity in self.build_query(fields).fetch(low_mark, high_mark): > ? File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/djangotoolbox/db/basecompiler.py", > line 290, in build_query > ??? query.order_by(self._get_ordering()) > ? File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/djangotoolbox/db/basecompiler.py", > line 339, in _get_ordering > ??? raise DatabaseError("Ordering can't span tables on non-relational > backends (%s)" % order) > django.db.utils.DatabaseError: Ordering can't span tables on non-relational > backends (content_type__app_label) > > > DB settings in settings.py > > DATABASES = { > ??? 'default': { > ??? 'ENGINE': 'django_mongodb_engine', > ??? 'NAME': 'helloworld', > ??? 'USER': '', > ??? 'PASSWORD': '12424214', > ??? 'HOST': 'mongodb://staff.mongohq.com/', > ??? 'PORT': 'XXXXX', > ??? }, > } > > my requirement packages, > Django==1.3 > dictshield==0.4.4 > django-mongodb-engine==0.4.0 > django-social-auth==0.6.9 > djangotoolbox==0.0.1 > httplib2==0.7.4 > mongoengine==0.6.10 > mongokit==0.8 > oauth2==1.5.211 > pymongo==2.2 > python-openid==2.2.5 > simplejson==2.5.2 > wsgiref==0.1.2 > > Please point me what i missed here... > > -- > Cheers, > Ranjith Kumar K, > Chennai. > > http://ranjithtenz.wordpress.com > > > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > On 2012-06-19T13:05:00Z Kwpolska wrote: > http://mongodb-is-web-scale.com > > Seriously: > django.db.utils.DatabaseError: Ordering can't span tables on non-relational > backends (content_type__app_label) > >It's left to you to understand the problem. On 2012-06-19T13:30:00Z Ranjith Kumar wrote: > Thanks i have sorted out -- Kwpolska stop html mail ? ? ?| always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 ? | Arch Linux x86_64, zsh, mutt, vim. # vim:set textwidth=70: From eire1130 at gmail.com Tue Jun 19 15:34:04 2012 From: eire1130 at gmail.com (James Reynolds) Date: Tue, 19 Jun 2012 09:34:04 -0400 Subject: [Tutor] Pymongo Error In-Reply-To: References: Message-ID: On Tue, Jun 19, 2012 at 1:22 AM, Ranjith Kumar wrote: > Hi all, > I tried Django with Mongodb while running manage.py syncdb I endup with > this error > > note : it works fine with sqlite and mysql db > > (django-1.3)ranjith at ranjith:~/ > sandbox/python-box/hukkster-core-site/hukk$ ./manage.py syncdb > /home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/pymongo/connection.py:385: > UserWarning: must provide a username and password to authenticate to > hukkster_testing > "to authenticate to %s" % (db,)) > Creating tables ... > Traceback (most recent call last): > File "./manage.py", line 14, in > execute_manager(settings) > File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/__init__.py", > line 438, in execute_manager > utility.execute() > File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/__init__.py", > line 379, in execute > self.fetch_command(subcommand).run_from_argv(self.argv) > File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/base.py", > line 191, in run_from_argv > self.execute(*args, **options.__dict__) > File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/base.py", > line 220, in execute > output = self.handle(*args, **options) > File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/base.py", > line 351, in handle > return self.handle_noargs(**options) > File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", > line 109, in handle_noargs > emit_post_sync_signal(created_models, verbosity, interactive, db) > File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/core/management/sql.py", > line 190, in emit_post_sync_signal > interactive=interactive, db=db) > File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", > line 172, in send > response = receiver(signal=self, sender=sender, **named) > File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", > line 41, in create_permissions > "content_type", "codename" > File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/db/models/query.py", > line 107, in _result_iter > self._fill_cache() > File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/db/models/query.py", > line 772, in _fill_cache > self._result_cache.append(self._iter.next()) > File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/django/db/models/query.py", > line 959, in iterator > for row in self.query.get_compiler(self.db).results_iter(): > File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/djangotoolbox/db/basecompiler.py", > line 229, in results_iter > for entity in self.build_query(fields).fetch(low_mark, high_mark): > File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/djangotoolbox/db/basecompiler.py", > line 290, in build_query > query.order_by(self._get_ordering()) > File > "/home/ranjith/virtualenvs/django-1.3/local/lib/python2.7/site-packages/djangotoolbox/db/basecompiler.py", > line 339, in _get_ordering > raise DatabaseError("Ordering can't span tables on non-relational > backends (%s)" % order) > django.db.utils.DatabaseError: Ordering can't span tables on > non-relational backends (content_type__app_label) > > > DB settings in settings.py > > DATABASES = { > 'default': { > 'ENGINE': 'django_mongodb_engine', > 'NAME': 'helloworld', > 'USER': '', > 'PASSWORD': '12424214', > 'HOST': 'mongodb://staff.mongohq.com/', > 'PORT': 'XXXXX', > }, > } > > my requirement packages, > Django==1.3 > dictshield==0.4.4 > django-mongodb-engine==0.4.0 > django-social-auth==0.6.9 > djangotoolbox==0.0.1 > httplib2==0.7.4 > mongoengine==0.6.10 > mongokit==0.8 > oauth2==1.5.211 > pymongo==2.2 > python-openid==2.2.5 > simplejson==2.5.2 > wsgiref==0.1.2 > > Please point me what i missed here... > > -- > Cheers, > Ranjith Kumar K, > Chennai. > > http://ranjithtenz.wordpress.com > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > This list here is for new users to python. Using mongo with django is a pretty advanced topic. I would suggest posting this on stackoverflow, or to the django-users group (and there are a ton of resources on this already on those two sites on this topic) That said, having used mongo with django, I'll bet your problem is that you are using the django version provided by the django project. For Mongo, you have to use the django-norel fork. Just google Django Mongo NoRel in various combinations and you will find it. To do this, and not screw up your working environment, you will need to make certain you have a virtual environment set up. Anyway, once you get it set up, it actually works pretty good. -------------- next part -------------- An HTML attachment was scrubbed... URL: From kendy at kendy.org Tue Jun 19 17:01:17 2012 From: kendy at kendy.org (kendy at kendy.org) Date: Tue, 19 Jun 2012 08:01:17 PDT Subject: [Tutor] python varying mulitple inheritance Message-ID: <7527.1340118077@speakeasy.net> Thank you Steve and Alan! I'll keep studying and practicing. Ken >Back to basics. a class is a definition of a *type* of thing, not a >particular case. A Pocket is a general purpose container. Each instance >can hold many things. So you would normally expect one pocket class and >6 instances. Each instance holding a different list of items. > >The only time you need a different Pocket class is where the pocket has >different *behaviour*. It is normally the behaviours that differentiate >classes not the data. The class data is there to support the class >behaviour. The behaviour of a Pocket is probably some thing like: >addItem >removeItem >findItem >countItems >open >close >empty > >In most implementations a Pocket will look a lot like a list... From selbyrowleycannon at ymail.com Tue Jun 19 21:35:33 2012 From: selbyrowleycannon at ymail.com (Selby Rowley-Cannon) Date: Tue, 19 Jun 2012 12:35:33 -0700 (PDT) Subject: [Tutor] (no subject) Message-ID: <1340134533.23458.YahooMailNeo@web140206.mail.bf1.yahoo.com> Mailing list; ??? ??? I have a small, [for the most part] functioning translation app for Rydish, a language created for the sole purpose of an RPG. The only problem is when I enter a word that has not yet been translated, I get this error: Traceback (most recent call last): ? File "translator.py", line 25, in ??? Etranslate() ? File "translator.py", line 14, in Etranslate ??? print(Edictionary[Eword]) KeyError: 'world' Is there a way to print a user-freindly message instead of displaying a traceback? Thanks, ??? ??? -Selby -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Tue Jun 19 21:59:12 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 19 Jun 2012 15:59:12 -0400 Subject: [Tutor] (no subject) In-Reply-To: <1340134533.23458.YahooMailNeo@web140206.mail.bf1.yahoo.com> References: <1340134533.23458.YahooMailNeo@web140206.mail.bf1.yahoo.com> Message-ID: On Tue, Jun 19, 2012 at 3:35 PM, Selby Rowley-Cannon wrote: > Mailing list; > ??? ??? I have a small, [for the most part] functioning translation app for > Rydish, a language created for the sole purpose of an RPG. The only problem > is when I enter a word that has not yet been translated, I get this error: > > Traceback (most recent call last): > ? File "translator.py", line 25, in > ??? Etranslate() > ? File "translator.py", line 14, in Etranslate > ??? print(Edictionary[Eword]) > KeyError: 'world' > > Is there a way to print a user-freindly message instead of displaying a > traceback? > Thanks, > ??? ??? -Selby > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > try: print(Edictionary[Eword] except KeyError: print Eword, " has not yet been translated" If you catch the exception you can print a useful message. Not sure what that would be but I took a stab -- Joel Goldstick From martin at linux-ip.net Tue Jun 19 22:02:44 2012 From: martin at linux-ip.net (Martin A. Brown) Date: Tue, 19 Jun 2012 16:02:44 -0400 Subject: [Tutor] (no subject) In-Reply-To: <1340134533.23458.YahooMailNeo@web140206.mail.bf1.yahoo.com> References: <1340134533.23458.YahooMailNeo@web140206.mail.bf1.yahoo.com> Message-ID: Greetings Selby, : ??? ??? I have a small, [for the most part] functioning : translation app for Rydish, a language created for the sole : purpose of an RPG. The only problem is when I enter a word that : has not yet been translated, I get this error: : : Traceback (most recent call last): : ? File "translator.py", line 25, in : ??? Etranslate() : ? File "translator.py", line 14, in Etranslate : ??? print(Edictionary[Eword]) : KeyError: 'world' : : Is there a way to print a user-freindly message instead of : displaying a traceback? Thanks, ??? ??? -Selby Try the two techniques here (and the one Joel just posted): http://mail.python.org/pipermail/tutor/2012-June/090025.html -Martin (You asked your question much more clearly this time and with a code sample--so, keep it up and enjoy the Python.) -- Martin A. Brown http://linux-ip.net/ From d at davea.name Tue Jun 19 22:07:11 2012 From: d at davea.name (Dave Angel) Date: Tue, 19 Jun 2012 16:07:11 -0400 Subject: [Tutor] (no subject) In-Reply-To: <1340134533.23458.YahooMailNeo@web140206.mail.bf1.yahoo.com> References: <1340134533.23458.YahooMailNeo@web140206.mail.bf1.yahoo.com> Message-ID: <4FE0DBEF.7000403@davea.name> On 06/19/2012 03:35 PM, Selby Rowley-Cannon wrote: > Mailing list; > I have a small, [for the most part] functioning translation app for Rydish, a language created for the sole purpose of an RPG. The only problem is when I enter a word that has not yet been translated, I get this error: > > Traceback (most recent call last): > File "translator.py", line 25, in > Etranslate() > File "translator.py", line 14, in Etranslate > print(Edictionary[Eword]) > KeyError: 'world' > > Is there a way to print a user-freindly message instead of displaying a traceback? > Thanks, > -Selby > > Even easier than catching an exception is to test for the condition you expect might not pass. Assuming your Edictionary is a dict, all you need to do is to make the reference conditional on the presence of that particular key. if Eword in Edictionary: print(Edictionary[Eword]) else: print("oops.... -- DaveA From breamoreboy at yahoo.co.uk Wed Jun 20 00:48:14 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 19 Jun 2012 23:48:14 +0100 Subject: [Tutor] (no subject) In-Reply-To: <4FE0DBEF.7000403@davea.name> References: <1340134533.23458.YahooMailNeo@web140206.mail.bf1.yahoo.com> <4FE0DBEF.7000403@davea.name> Message-ID: On 19/06/2012 21:07, Dave Angel wrote: > On 06/19/2012 03:35 PM, Selby Rowley-Cannon wrote: >> Mailing list; >> I have a small, [for the most part] functioning translation app for Rydish, a language created for the sole purpose of an RPG. The only problem is when I enter a word that has not yet been translated, I get this error: >> >> Traceback (most recent call last): >> File "translator.py", line 25, in >> Etranslate() >> File "translator.py", line 14, in Etranslate >> print(Edictionary[Eword]) >> KeyError: 'world' >> >> Is there a way to print a user-freindly message instead of displaying a traceback? >> Thanks, >> -Selby >> >> > > Even easier than catching an exception is to test for the condition you > expect might not pass. Assuming your Edictionary is a dict, all you > need to do is to make the reference conditional on the presence of that > particular key. > > if Eword in Edictionary: > print(Edictionary[Eword]) > else: > print("oops.... > > > But note the comparison between LBYL and EAFP - I'll leave those interested to google for it :) -- Cheers. Mark Lawrence. From d at davea.name Wed Jun 20 03:04:59 2012 From: d at davea.name (Dave Angel) Date: Tue, 19 Jun 2012 21:04:59 -0400 Subject: [Tutor] (no subject) In-Reply-To: References: <1340134533.23458.YahooMailNeo@web140206.mail.bf1.yahoo.com> <4FE0DBEF.7000403@davea.name> Message-ID: <4FE121BB.4000102@davea.name> On 06/19/2012 06:48 PM, Mark Lawrence wrote: > On 19/06/2012 21:07, Dave Angel wrote: >> On 06/19/2012 03:35 PM, Selby Rowley-Cannon wrote: >>> Mailing list; >>> I have a small, [for the most part] functioning translation >>> app for Rydish, a language created for the sole purpose of an RPG. >>> The only problem is when I enter a word that has not yet been >>> translated, I get this error: >>> >>> Traceback (most recent call last): >>> File "translator.py", line 25, in >>> Etranslate() >>> File "translator.py", line 14, in Etranslate >>> print(Edictionary[Eword]) >>> KeyError: 'world' >>> >>> Is there a way to print a user-freindly message instead of >>> displaying a traceback? >>> Thanks, >>> -Selby >>> >>> >> >> Even easier than catching an exception is to test for the condition you >> expect might not pass. Assuming your Edictionary is a dict, all you >> need to do is to make the reference conditional on the presence of that >> particular key. >> >> if Eword in Edictionary: >> print(Edictionary[Eword]) >> else: >> print("oops.... >> >> >> > > But note the comparison between LBYL and EAFP - I'll leave those > interested to google for it :) > If the program is multithreaded, or if it's NOT a dictionary, then perhaps LBYL is not the right answer. Otherwise, it describes what's wanted, and doesn't risk catching too broad an exception. I've seen far too many programs that used a bare except to mask not only the condition, but lots of other things. -- DaveA From emile at fenx.com Wed Jun 20 04:03:45 2012 From: emile at fenx.com (Emile van Sebille) Date: Tue, 19 Jun 2012 19:03:45 -0700 Subject: [Tutor] (no subject) In-Reply-To: <1340134533.23458.YahooMailNeo@web140206.mail.bf1.yahoo.com> References: <1340134533.23458.YahooMailNeo@web140206.mail.bf1.yahoo.com> Message-ID: On 6/19/2012 12:35 PM Selby Rowley-Cannon said... > Mailing list; > I have a small, [for the most part] functioning translation app for > Rydish, a language created for the sole purpose of an RPG. The only > problem is when I enter a word that has not yet been translated, I get > this error: > > Traceback (most recent call last): > File "translator.py", line 25, in > Etranslate() > File "translator.py", line 14, in Etranslate > print(Edictionary[Eword]) > KeyError: 'world' > > Is there a way to print a user-freindly message instead of displaying a > traceback? As you're writing a translator it may be interesting to swap in something that can still be used in the translation. No one's mentioned get as an option: >>> Edict = dict(zip('abcde','ABCDE')) >>> for ii in 'agbdteabcdfe': ... print Edict.get(ii,'-grumble-') ... A -grumble- B D -grumble- E A B C D -grumble- E >>> Or spice it up! >>> import random >>> undefined = ['-grumble-','-mumble-','-garbled-','-cough-'] >>> for ii in 'agbdteabcdfe': ... print Edict.get(ii,random.choice(undefined)) ... A -mumble- B D -grumble- E A B C D -cough- E >>> Emile From ogreden at gmail.com Wed Jun 20 14:53:14 2012 From: ogreden at gmail.com (=?ISO-8859-9?B?T/B1emhhbiDW8HJlZGVu?=) Date: Wed, 20 Jun 2012 15:53:14 +0300 Subject: [Tutor] Counting Items in a List Message-ID: Hi, I have been learning Python and trying little bits of coding for a while. Recently I tried to have a paragraph and create a list of its words, then counting those words. Here is the code: import re > > >> a = """Performs the template substitution, returning a new string. >> mapping is any dictionary-like \ > > object with keys that match the placeholders in the template. >> Alternatively, you can provide keyword \ > > arguments, where the keywords are the placeholders. When both mapping and >> kws are given and there \ > > are duplicates, the placeholders from kws take precedence.""" > > >> b = [] > > c = 0 > > >> for y in a.split(" "): > > b.append(y.lower()) > > if re.search(",", y) != None: > > c = b.index(y.lower()) > > b.remove(y.lower()) > > b.insert(c, y.strip(",").lower()) > > b.sort() > > b.extend(["you"]) > > count = 0 > > >> for x in b: > > count = b.count(x) > > if count > 1: > > c = b.index(x) > > if b[c+1] != x is True: > > print "%s listede %d tane var." % (x, count) > > else: > > print "%s listede %d tane var." % (x, count) > > >> And here are the questions: - This code doesn't print for the items that occur more than one time. Why? - And actually as I was writing, I realized that "if b[c+1]" may return an error if the last item on the list occured more than one, however, it didn't even if a manually made the last item occur two times. Was my initial expectation a false one? If not, how could I turn it into a b[c-1] so that it will not fail in first item? Thanks, O?uzhan -------------- next part -------------- An HTML attachment was scrubbed... URL: From mariocatch at gmail.com Wed Jun 20 15:34:21 2012 From: mariocatch at gmail.com (mariocatch) Date: Wed, 20 Jun 2012 09:34:21 -0400 Subject: [Tutor] Counting Items in a List In-Reply-To: References: Message-ID: Hello, Not sure if this is what you're trying to solve, although it sounds like it based on your leading statement. I think what you have is more complicated than it needs to be for something that simple: Why not something like this? * paragraph = """I have been learning Python and trying little bits of coding for a while. Recently I tried to have a paragraph and create a list of its words, then counting those words. Here is the code:"""* * splitParagraph = paragraph.strip().split(' ') print len(splitParagraph) # yields 36 print splitParagraph # yields a list [] of each word separated by split()* ** -Mario On Wed, Jun 20, 2012 at 8:53 AM, O?uzhan ??reden wrote: > Hi, > > I have been learning Python and trying little bits of coding for a while. > Recently I tried to have a paragraph and create a list of its words, then > counting those words. Here is the code: > > import re >> >> >>> a = """Performs the template substitution, returning a new string. >>> mapping is any dictionary-like \ >> >> object with keys that match the placeholders in the template. >>> Alternatively, you can provide keyword \ >> >> arguments, where the keywords are the placeholders. When both mapping and >>> kws are given and there \ >> >> are duplicates, the placeholders from kws take precedence.""" >> >> >>> b = [] >> >> c = 0 >> >> >>> for y in a.split(" "): >> >> b.append(y.lower()) >> >> if re.search(",", y) != None: >> >> c = b.index(y.lower()) >> >> b.remove(y.lower()) >> >> b.insert(c, y.strip(",").lower()) >> >> b.sort() >> >> b.extend(["you"]) >> >> count = 0 >> >> >>> for x in b: >> >> count = b.count(x) >> >> if count > 1: >> >> c = b.index(x) >> >> if b[c+1] != x is True: >> >> print "%s listede %d tane var." % (x, count) >> >> else: >> >> print "%s listede %d tane var." % (x, count) >> >> >>> > And here are the questions: > > > - This code doesn't print for the items that occur more than one time. > Why? > - And actually as I was writing, I realized that "if b[c+1]" may > return an error if the last item on the list occured more than one, > however, it didn't even if a manually made the last item occur two times. > Was my initial expectation a false one? If not, how could I turn it into a > b[c-1] so that it will not fail in first item? > > Thanks, > O?uzhan > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ogreden at gmail.com Wed Jun 20 15:44:19 2012 From: ogreden at gmail.com (=?ISO-8859-9?B?T/B1emhhbiDW8HJlZGVu?=) Date: Wed, 20 Jun 2012 16:44:19 +0300 Subject: [Tutor] Counting Items in a List In-Reply-To: References: Message-ID: Sorry, it seems like I was not clear enough in that statement. I should have written something like "counting how many times each word occured" insted of "counting words". 2012/6/20 mariocatch > Hello, > > Not sure if this is what you're trying to solve, although it sounds like > it based on your leading statement. > I think what you have is more complicated than it needs to be for > something that simple: > Why not something like this? > > * paragraph = """I have been learning Python and trying little bits of > coding for a while. Recently I tried to have a paragraph and create a list > of its words, then counting those words. Here is the code:"""* > * splitParagraph = paragraph.strip().split(' ') > print len(splitParagraph) # yields 36 > print splitParagraph # yields a list [] of each word separated by > split()* > > ** > -Mario > > > On Wed, Jun 20, 2012 at 8:53 AM, O?uzhan ??reden wrote: > >> Hi, >> >> I have been learning Python and trying little bits of coding for a while. >> Recently I tried to have a paragraph and create a list of its words, then >> counting those words. Here is the code: >> >> import re >>> >>> >>>> a = """Performs the template substitution, returning a new string. >>>> mapping is any dictionary-like \ >>> >>> object with keys that match the placeholders in the template. >>>> Alternatively, you can provide keyword \ >>> >>> arguments, where the keywords are the placeholders. When both mapping >>>> and kws are given and there \ >>> >>> are duplicates, the placeholders from kws take precedence.""" >>> >>> >>>> b = [] >>> >>> c = 0 >>> >>> >>>> for y in a.split(" "): >>> >>> b.append(y.lower()) >>> >>> if re.search(",", y) != None: >>> >>> c = b.index(y.lower()) >>> >>> b.remove(y.lower()) >>> >>> b.insert(c, y.strip(",").lower()) >>> >>> b.sort() >>> >>> b.extend(["you"]) >>> >>> count = 0 >>> >>> >>>> for x in b: >>> >>> count = b.count(x) >>> >>> if count > 1: >>> >>> c = b.index(x) >>> >>> if b[c+1] != x is True: >>> >>> print "%s listede %d tane var." % (x, count) >>> >>> else: >>> >>> print "%s listede %d tane var." % (x, count) >>> >>> >>>> >> And here are the questions: >> >> >> - This code doesn't print for the items that occur more than one >> time. Why? >> - And actually as I was writing, I realized that "if b[c+1]" may >> return an error if the last item on the list occured more than one, >> however, it didn't even if a manually made the last item occur two times. >> Was my initial expectation a false one? If not, how could I turn it into a >> b[c-1] so that it will not fail in first item? >> >> Thanks, >> O?uzhan >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From brad.hudson at gmail.com Wed Jun 20 17:12:39 2012 From: brad.hudson at gmail.com (Brad Hudson) Date: Wed, 20 Jun 2012 10:12:39 -0500 Subject: [Tutor] Counting Items in a List In-Reply-To: References: Message-ID: I think this is more of what you ar looking for: >>> a = 'this is a list with is repeated twice.' >>> a 'this is a list with is repeated twice.' >>> alist = a.strip().split() >>> alist ['this', 'is', 'a', 'list', 'with', 'is', 'repeated', 'twice.'] >>> var='is' >>> alist.count(var) 2 >>> On Wed, Jun 20, 2012 at 8:44 AM, O?uzhan ??reden wrote: > Sorry, it seems like I was not clear enough in that statement. I should > have written something like "counting how many times each word occured" > insted of "counting words". -------------- next part -------------- An HTML attachment was scrubbed... URL: From martin at linux-ip.net Wed Jun 20 17:20:21 2012 From: martin at linux-ip.net (Martin A. Brown) Date: Wed, 20 Jun 2012 11:20:21 -0400 Subject: [Tutor] Counting Items in a List In-Reply-To: References: Message-ID: Hello, : Sorry, it seems like I was not clear enough in that statement. I : should have written something like "counting how many times each : word occured" insted of "counting words". You might like to try out collections.defaultdict(). import collections summary = collections.defaultdict(int) for line in lines: words = line.strip().split() for word in words: summary[word] += 1 Lots of interesting features in the collections module... -Martin -- Martin A. Brown http://linux-ip.net/ From alan.gauld at btinternet.com Wed Jun 20 19:26:51 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 20 Jun 2012 18:26:51 +0100 Subject: [Tutor] Counting Items in a List In-Reply-To: References: Message-ID: On 20/06/12 16:20, Martin A. Brown wrote: > : should have written something like "counting how many times each > : word occured" insted of "counting words". > > You might like to try out collections.defaultdict(). Even a standard dict would simplify the code dramatically. The defaultdict does suit this case better though. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From mariocatch at gmail.com Wed Jun 20 20:34:21 2012 From: mariocatch at gmail.com (mariocatch) Date: Wed, 20 Jun 2012 14:34:21 -0400 Subject: [Tutor] Counting Items in a List In-Reply-To: References: Message-ID: Here's one way, can surely be optimized: * paragraph = "This paragraph contains words once, more than once, and possibly not at all either. Figure that one out. " para = paragraph.strip('.').strip(',').strip().split() wordDict = {} for word in para: word = word.strip('.').strip(',') if wordDict.has_key(word): wordDict[word] += 1 else: wordDict[word] = 1* * print wordDict * -Mario On Wed, Jun 20, 2012 at 1:26 PM, Alan Gauld wrote: > On 20/06/12 16:20, Martin A. Brown wrote: > > : should have written something like "counting how many times each >> : word occured" insted of "counting words". >> >> You might like to try out collections.defaultdict(). >> > > Even a standard dict would simplify the code dramatically. > The defaultdict does suit this case better though. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > > > ______________________________**_________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ramit.prasad at jpmorgan.com Wed Jun 20 20:46:17 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Wed, 20 Jun 2012 18:46:17 +0000 Subject: [Tutor] Counting Items in a List In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF4741323C899@SCACMX008.exchad.jpmchase.net> >??? para = paragraph.strip('.').strip(',').strip().split() I think you want replace not strip. See http://docs.python.org/library/stdtypes.html#string-methods Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 steve at pearwood.info Wed Jun 20 21:39:50 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 21 Jun 2012 05:39:50 +1000 Subject: [Tutor] Counting Items in a List In-Reply-To: References: Message-ID: <4FE22706.7070601@pearwood.info> O?uzhan ??reden wrote: > Hi, > > I have been learning Python and trying little bits of coding for a while. > Recently I tried to have a paragraph and create a list of its words, then > counting those words. Here is the code: [code snipped] I can't understand your code! Indentation is messed up badly. It has inconsistent numbers of > quote marks at the start of some lines. It's a mess. If you are using Python 2.7 or better, the best way to count items in a list is with a Counter object: import collections text = """ Performs the template substitution, returning a new string. mapping is any dictionary-like object with keys that match the placeholders in the template. Alternatively, you can provide keyword arguments, where the keywords are the placeholders. When both mapping and kws are given and there are duplicates, the placeholders from kws take precedence. """ text = text.replace('.', '').replace(',', '') words = text.lower().split() counts = collections.Counter(words) Once I do this, I can check the most common words: counts.most_common(5) => [('the', 6), ('are', 3), ('placeholders', 3), ('and', 2), ('kws', 2)] or look up the counts of words: counts['keys'] => 1 counts['oranges'] => 0 More about Counter here: http://docs.python.org/library/collections.html#collections.Counter -- Steven From ramit.prasad at jpmorgan.com Wed Jun 20 21:56:32 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Wed, 20 Jun 2012 19:56:32 +0000 Subject: [Tutor] Counting Items in a List In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF4741323C899@SCACMX008.exchad.jpmchase.net> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741323DB2F@SCACMX008.exchad.jpmchase.net> Please always post back to the list. > Nope, strip() was intended so we get a list back instead of a string. Need > the list for iteration down below that. >> > para = paragraph.strip('.').strip(',').strip().split() >> I think you want replace not strip. >> >> See http://docs.python.org/library/stdtypes.html#string-methods No, you are wrong. If you had looked at the link (or tested the code) you would find strip() does not do what you think it does. What do you mean by "we get a list back instead of a string"? strip does not return a list...it returns a string. split is what returns the list for iteration. >>> paragraph = "This paragraph contains words once, more than once, and possibly not at all either. Figure that one out. " >>> para = paragraph.strip('.').strip(',').strip().split() >>> print para ['This', 'paragraph', 'contains', 'words', 'once,', 'more', 'than', 'once,', 'and', 'possibly', 'not', 'at', 'all', 'either.', 'Figure', 'that', 'one', 'out.'] Note the inclusion of 'once,' and 'either.' and 'out.'. Use replace to remove punctuation instead and then just compare words. Most probably you want to lower() or upper() the entire paragraph to be thorough, otherwise 'This' and 'this' will be counted separately. Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 mariocatch at gmail.com Wed Jun 20 22:10:02 2012 From: mariocatch at gmail.com (mariocatch) Date: Wed, 20 Jun 2012 16:10:02 -0400 Subject: [Tutor] Counting Items in a List In-Reply-To: <5B80DD153D7D744689F57F4FB69AF4741323DB2F@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF4741323C899@SCACMX008.exchad.jpmchase.net> <5B80DD153D7D744689F57F4FB69AF4741323DB2F@SCACMX008.exchad.jpmchase.net> Message-ID: Yes, I meant split(). Was a typo :) On Wed, Jun 20, 2012 at 3:56 PM, Prasad, Ramit wrote: > Please always post back to the list. > > > Nope, strip() was intended so we get a list back instead of a string. > Need > > the list for iteration down below that. > > >> > para = paragraph.strip('.').strip(',').strip().split() > >> I think you want replace not strip. > >> > >> See http://docs.python.org/library/stdtypes.html#string-methods > > No, you are wrong. If you had looked at the link (or tested the code) you > would find strip() does not do what you think it does. > > What do you mean by "we get a list back instead of a string"? strip does > not return a list...it returns a string. split is what returns the list for > iteration. > > > >>> paragraph = "This paragraph contains words once, more than once, and > possibly not at all either. Figure that one out. " > >>> para = paragraph.strip('.').strip(',').strip().split() > >>> print para > ['This', 'paragraph', 'contains', 'words', 'once,', 'more', 'than', > 'once,', 'and', 'possibly', 'not', 'at', 'all', 'either.', 'Figure', > 'that', 'one', 'out.'] > > Note the inclusion of 'once,' and 'either.' and 'out.'. Use replace to > remove > punctuation instead and then just compare words. Most probably you want to > lower() > or upper() the entire paragraph to be thorough, otherwise 'This' and > 'this' will > be counted separately. > > > Ramit > > > Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology > 712 Main Street | Houston, TX 77002 > work phone: 713 - 216 - 5423 > > -- > > This email is confidential and subject to important disclaimers and > conditions including on offers for the purchase or sale of > securities, accuracy and completeness of information, viruses, > confidentiality, legal privilege, and legal entity disclaimers, > available at http://www.jpmorgan.com/pages/disclosures/email. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From mariocatch at gmail.com Wed Jun 20 22:12:20 2012 From: mariocatch at gmail.com (mariocatch) Date: Wed, 20 Jun 2012 16:12:20 -0400 Subject: [Tutor] Counting Items in a List In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF4741323C899@SCACMX008.exchad.jpmchase.net> <5B80DD153D7D744689F57F4FB69AF4741323DB2F@SCACMX008.exchad.jpmchase.net> Message-ID: Sorry for the double-post. I don't think I am wrong either. Replace doesn't return a list of words in the way that my code sample was setup to do. How would you solve it with replace? Maybe an example prove the point better. -Mario On Wed, Jun 20, 2012 at 4:10 PM, mariocatch wrote: > Yes, I meant split(). Was a typo :) > > > On Wed, Jun 20, 2012 at 3:56 PM, Prasad, Ramit wrote: > >> Please always post back to the list. >> >> > Nope, strip() was intended so we get a list back instead of a string. >> Need >> > the list for iteration down below that. >> >> >> > para = paragraph.strip('.').strip(',').strip().split() >> >> I think you want replace not strip. >> >> >> >> See http://docs.python.org/library/stdtypes.html#string-methods >> >> No, you are wrong. If you had looked at the link (or tested the code) you >> would find strip() does not do what you think it does. >> >> What do you mean by "we get a list back instead of a string"? strip does >> not return a list...it returns a string. split is what returns the list >> for >> iteration. >> >> >> >>> paragraph = "This paragraph contains words once, more than once, and >> possibly not at all either. Figure that one out. " >> >>> para = paragraph.strip('.').strip(',').strip().split() >> >>> print para >> ['This', 'paragraph', 'contains', 'words', 'once,', 'more', 'than', >> 'once,', 'and', 'possibly', 'not', 'at', 'all', 'either.', 'Figure', >> 'that', 'one', 'out.'] >> >> Note the inclusion of 'once,' and 'either.' and 'out.'. Use replace to >> remove >> punctuation instead and then just compare words. Most probably you want >> to lower() >> or upper() the entire paragraph to be thorough, otherwise 'This' and >> 'this' will >> be counted separately. >> >> >> Ramit >> >> >> Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology >> 712 Main Street | Houston, TX 77002 >> work phone: 713 - 216 - 5423 >> >> -- >> >> This email is confidential and subject to important disclaimers and >> conditions including on offers for the purchase or sale of >> securities, accuracy and completeness of information, viruses, >> confidentiality, legal privilege, and legal entity disclaimers, >> available at http://www.jpmorgan.com/pages/disclosures/email. >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ramit.prasad at jpmorgan.com Wed Jun 20 23:08:48 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Wed, 20 Jun 2012 21:08:48 +0000 Subject: [Tutor] Counting Items in a List In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF4741323C899@SCACMX008.exchad.jpmchase.net> <5B80DD153D7D744689F57F4FB69AF4741323DB2F@SCACMX008.exchad.jpmchase.net> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741323DC3A@SCACMX008.exchad.jpmchase.net> Please do not top post. > Sorry for the double-post. > > I don't think I am wrong either. Replace doesn't return a list of words in > the way that my code sample was setup to do. > How would you solve it with replace? Maybe an example prove the point > better. > > -Mario > On Wed, Jun 20, 2012 at 4:10 PM, mariocatch wrote: > Yes, I meant split(). Was a typo :) > > On Wed, Jun 20, 2012 at 3:56 PM, Prasad, Ramit > wrote: > Please always post back to the list. > > > Nope, strip() was intended so we get a list back instead of a string. > Need > > the list for iteration down below that. > >> > para = paragraph.strip('.').strip(',').strip().split() > >> I think you want replace not strip. > >> > >> See http://docs.python.org/library/stdtypes.html#string-methods > No, you are wrong. If you had looked at the link (or tested the code) you > would find strip() does not do what you think it does. > > What do you mean by "we get a list back instead of a string"? strip does > not return a list...it returns a string. split is what returns the list for > iteration. > > > >>> paragraph = "This paragraph contains words once, more than once, and > possibly not at all either. Figure that one out. " > >>> para = paragraph.strip('.').strip(',').strip().split() > >>> print para > ['This', 'paragraph', 'contains', 'words', 'once,', 'more', 'than', > 'once,', 'and', 'possibly', 'not', 'at', 'all', 'either.', 'Figure', > 'that', 'one', 'out.'] > > Note the inclusion of 'once,' and 'either.' and 'out.'. Use replace to > remove > punctuation instead and then just compare words. Most probably you want to > lower() > or upper() the entire paragraph to be thorough, otherwise 'This' and 'this' > will > be counted separately. You are not reading my replies closely enough. I am talking about *replace* instead of *strip* and NOT split. Again, if you read the documentation I linked you would see what I am talking about, but if not just look at the difference between a and b below. a = ' Something, is happening here.'.strip(',') b = ' Something, is happening here.'.replace(',', '') print a==b Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 mnickey at gmail.com Thu Jun 21 01:05:02 2012 From: mnickey at gmail.com (Mike Nickey) Date: Wed, 20 Jun 2012 16:05:02 -0700 Subject: [Tutor] Sorting tuples Message-ID: Hey all, I'm working through the google classes to increase my python understanding and I'm stuck on sorting tuples. What is being asked is to sort tuples based on the second element of the tuple in ascending order. Given a list of non-empty tuples, return a list sorted in increasing order by the last element in each tuple. e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields [(2, 2), (1, 3), (3, 4, 5), (1, 7)] What I have is def sort_last(tuples): # +++your code here+++ print sorted(tuples, key=lamda tuples: tuples[-1]) return I'm stuck and don't really understand what lamda is. From various web-boards I have seen that sorting on -1 should give me the last element o each sub-list. Can anyone assist? -- ~MEN From mnickey at gmail.com Thu Jun 21 01:27:57 2012 From: mnickey at gmail.com (Mike Nickey) Date: Wed, 20 Jun 2012 16:27:57 -0700 Subject: [Tutor] Sorting tuples In-Reply-To: References: Message-ID: While i'm still not sure what lamda is or represents, I've found a solution. Thank you all for your assistance, past, present and future. :) This seems to work: def sort_last(tuples): return sorted(tuples, key=myDef) def myDef(s): return s[-1] On Wed, Jun 20, 2012 at 4:05 PM, Mike Nickey wrote: > Hey all, > > I'm working through the google classes to increase my python > understanding and I'm stuck on sorting tuples. > What is being asked is to sort tuples based on the second element of > the tuple in ascending order. > > Given a list of non-empty tuples, return a list sorted in increasing > order by the last element in each tuple. > e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields [(2, 2), (1, 3), (3, > 4, 5), (1, 7)] > > What I have is > def sort_last(tuples): > ? ?# +++your code here+++ > ? ?print sorted(tuples, key=lamda tuples: tuples[-1]) > ? ?return > > I'm stuck and don't really understand what lamda is. From various > web-boards I have seen that sorting on -1 should give me the last > element o each sub-list. ?Can anyone assist? > > -- > ~MEN -- ~MEN From alan.gauld at btinternet.com Thu Jun 21 02:13:31 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 21 Jun 2012 01:13:31 +0100 Subject: [Tutor] Sorting tuples In-Reply-To: References: Message-ID: On 21/06/12 00:27, Mike Nickey wrote: > While i'm still not sure what lamda is or represents, lambda is a function without a name. >> def sort_last(tuples): >> # +++your code here+++ >> print sorted(tuples, key=lamda tuples: tuples[-1]) >> return The lamda above is identical to doing: def f(t): return t[-1] print sorted(tuples, key=f) And in general: def f(arg): return expression can be written: f = lambda arg: expression And they are most usually used, as in the case of sorted, where its convenient to define the function in place rather than having small functions scattered around which might only be used once. The term lambda comes from the Lambda calculus which is one of the underlying mathematical models that programming is based upon. (Along with state automata theory, Boolean algebra, relational theory etc) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Thu Jun 21 02:15:13 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 21 Jun 2012 10:15:13 +1000 Subject: [Tutor] Sorting tuples In-Reply-To: References: Message-ID: <4FE26791.5080906@pearwood.info> Mike Nickey wrote: > While i'm still not sure what lamda is or represents, I've found a solution. Unfortunately, you haven't. It's only by accident that your not-quite-solution gives the correct value for the example data. Try it with this data instead: [(1, 7), (1, 3), (3, 4, 8), (2, 2)] required result: [(2, 2), (1, 3), (3, 4, 8), (1, 7)] your result: [(2, 2), (1, 3), (1, 7), (3, 4, 8)] > This seems to work: > def sort_last(tuples): > return sorted(tuples, key=myDef) You have the right idea: sort the list of tuples, using a custom key function. But you have misunderstood the question. Earlier you said: "What is being asked is to sort tuples based on the SECOND element of the tuple in ascending order." [emphasis added] not the last element. Remember that elements are numbered from 0, so the first element of a tuple has index 0, the second element has index 1, the third index 2, and so forth; also you can index from the end: the last element is -1, the second element is -2, and so forth. You also asked what "lamda" is. Nothing -- it doesn't exist. However lambda (note the spelling) does exist, and it is a Python short-cut for creating an anonymous function. (For the record, lambda is the Greek letter L. There is a field of computer science called "lambda calculus" which is very important to the theory of computing, and involves the deep fundamentals of functions and functional programming.) The syntax is: lambda arguments : expression which is a short-cut for: def func(arguments): return expression except that no "func" variable is created (the anonymous part). An example might make this more clear: lambda x, y=2: 3*x + y creates an anonymous function object which takes one required argument x, and one optional argument y defaulting to 2, and returns the value of 3*x + y. You should consider lambda to be entirely optional, and moderately advanced. Using lambda is never *necessary*, but sometimes it is convenient. -- Steven From d at davea.name Thu Jun 21 02:23:52 2012 From: d at davea.name (Dave Angel) Date: Wed, 20 Jun 2012 20:23:52 -0400 Subject: [Tutor] Sorting tuples In-Reply-To: References: Message-ID: <4FE26998.7040207@davea.name> On 06/20/2012 07:05 PM, Mike Nickey wrote: > Hey all, > > I'm working through the google classes to increase my python > understanding and I'm stuck on sorting tuples. > What is being asked is to sort tuples based on the second element of > the tuple in ascending order. > > Given a list of non-empty tuples, return a list sorted in increasing > order by the last element in each tuple. Whoops. You have a contradictory problem statement. First you said you wanted the second element, then you said you wanted the last. The second element is elem[1], while the last one is elem[-1] so it's your choice which part of the spec was the desired one. -- DaveA From suryak at live.com Thu Jun 21 20:42:38 2012 From: suryak at live.com (Surya K) Date: Fri, 22 Jun 2012 00:12:38 +0530 Subject: [Tutor] what would be the best authentication method for my facebook application Message-ID: I am writing a facebook application in python where I used Blogger API to read posts from blogs (currently I am using ClientLogin under development). As each user(visitor) of my application should be able to view my without any authentication, I am using OAuth for authentication but I am unable to choose the type.. So, can anyone help in choosing what type of OAuth authentication I should use for my app and how could I do that. -------------- next part -------------- An HTML attachment was scrubbed... URL: From emeraldoffice at hotmail.com Fri Jun 22 03:53:35 2012 From: emeraldoffice at hotmail.com (Tamar Osher) Date: Thu, 21 Jun 2012 20:53:35 -0500 Subject: [Tutor] web design and python Message-ID: I have many questions, and eagerly ask you to please respond to me. I value your expertise, and greatly appreciate you taking the time to share. I want to find out, in elaborate detail, about the Python/Django relationship to web design. Why does a person or a company seek out and choose a Python-Django website, instead of Drupal, or Joomla, or Wordpress, or something else? What is the description of the unique, small niche that Python/Django has in the world of web design? Who are these individuals and companies that want Python/Django/TurboGears websites? What is the Python/Django/TurboGear relationship to "responsive web design" (which means the website is beautifully, perfectly view-able on all sizes of screens, including a smartphone, an ipad, and a desktop). What are the pros and cons of choosing Python/Django/TurboGears, compared to other web development options, such as Drupal, Joomla, and Wordpress? Is it really true that Python/Django websites cannot use just any web hosting service, and are more expensive to maintain? I have read several books about Python web development, and have extensively researched online, but have not found any of this information. It would be nice if someone could write a full article about this, to explain the true, current situation and realistic 2012 options to beginners who don't have degrees in computer science. I am very, very eager to hear from each one of you. THANK YOU for your time and help! -------------- next part -------------- An HTML attachment was scrubbed... URL: From eire1130 at gmail.com Fri Jun 22 04:32:43 2012 From: eire1130 at gmail.com (James Reynolds) Date: Thu, 21 Jun 2012 19:32:43 -0700 Subject: [Tutor] web design and python In-Reply-To: References: Message-ID: On Thu, Jun 21, 2012 at 6:53 PM, Tamar Osher wrote: > I have many questions, and eagerly ask you to please respond to me. I > value your expertise, and greatly appreciate you taking the time to share. > I want to find out, in elaborate detail, about the Python/Django > relationship to web design. Why does a person or a company seek out and > choose a Python-Django website, instead of Drupal, or Joomla, or Wordpress, > or something else? What is the description of the unique, small niche that > Python/Django has in the world of web design? Who are these individuals > and companies that want Python/Django/TurboGears websites? What is the > Python/Django/TurboGear relationship to "responsive web design" (which > means the website is beautifully, perfectly view-able on all sizes of > screens, including a smartphone, an ipad, and a desktop). What are the pros > and cons of choosing Python/Django/TurboGears, compared to other web > development options, such as Drupal, Joomla, and Wordpress? Is it really > true that Python/Django websites cannot use just any web hosting service, > and are more expensive to maintain? I have read several books about Python > web development, and have extensively researched online, but have not found > any of this information. It would be nice if someone could write a full > article about this, to explain the true, current situation and realistic > 2012 options to beginners who don't have degrees in computer science. I am > very, very eager to hear from each one of you. THANK YOU for your time and > help! > > > ** > * > > > > * > > * * > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor I'm not really sure how to respond to you, but I'll give it a go. Why does a person or a company seek out and choose a Python-Django > website, instead of Drupal, or Joomla, or Wordpress, or something else Well, Drupal, Joomla, Wordpress are all CMS (content management Systems). They are also written in PHP. I think the goals of developing in django vs having a wordpress site are quite a bit different, although they could be the same, I suppose. For example, if you wanted a site to just publish your daily thoughts, you might use wordpress. However, if you want to identify a tree by an image you took on your iphone, you might use django (and a lot of other stuff to make that magic happen!) Generally, I think, companies and people choose django because of the community and how much you can get done with it in a short period of time. What is the description of the unique, small niche that Python/Django has > in the world of web design? Not sure what this sentence means, but I wouldn't call Python / Django as small niche in the world of web design. It's probably the third most popular option right now, behind PHP and rails. What is the Python/Django/TurboGear relationship to "responsive web > design" (which means the website is beautifully, perfectly view-able on all > sizes of screens, including a smartphone, an ipad, and a desktop) Turbogears is another framework. I'm not sure of many sites built on it; I'm sure they are out there, I just don't know of them. You should check out: http://www.djangosites.org/ Is it really true that Python/Django websites cannot use just any web > hosting service, and are more expensive to maintain? Django is just a set of libraries written in python. So long as the webhosting service allows you to install python, install apache (or something similar) and allows you to broadcast, you can host the website. As for the second part, more expensive than what? Once you get the site, they sort of run themselves. You just have to pay for bandwidth and hosting. If your saving a lot of data and using a lot of bandwidth, that could get costly, but... that is not an issue of django, but rather of design / product needs. Some webhosting companies make it easier to set up django though. Some already have it installed, some do not. to beginners who don't have degrees in computer science I also do not have a degree in computer science. I am also a full time web developer working in Python / Django. -------------- next part -------------- An HTML attachment was scrubbed... URL: From emeraldoffice at hotmail.com Fri Jun 22 05:43:57 2012 From: emeraldoffice at hotmail.com (Tamar Osher) Date: Thu, 21 Jun 2012 22:43:57 -0500 Subject: [Tutor] Python and web design Message-ID: Could someone please be my mentor or career counselor, and share with me some of your experiences, and answer my wonderment questions? As a web designer, how did you push away and bypass the masses of people shouting "WORDPRESS, WORDPRESS", and "DREAMWEAVER, DREAMWEAVER"? How did you get drawn into Python web design? I learned Python and thought it was sort of obvious, simple, and easy, although I have not done anything with it after reading about it. But Wordpress - to this day, it is a 100% waste of my time. I want to be a web designer, but not with Wordpress or Joomla. I have learned html5, CSS3, and responsive web design. I like CoffeeCup html5 editor, and like Concrete5 CMS. I have not learned Django because it is not yet Python version 3. As a Python web designer, are you an employee of a company, or do you instead have your own private practice, providing contracted services to individual clients? What kind of websites do you build? Who hires you? Large companies, small businesses, individuals? Who would NOT want a Python/Django website? Do you work from home, or do you drive into the office, or do you travel around to visit clients? Do you basically only use Python and Django, or instead do you have to also know a whole bunch of other stuff? For instance, do you HAVE to know SQL, or is it just a fancy extra? Do you know Javascript? What are the minimum skills needed to become employable as a Python/Django web designer? Concerning Python programming, do I have to be skillful enough to actually create, design, and write my own Python code, or can I just find/copy/paste code into the website? As a Python web designer, do you work on the front-end or the back-end of a website, or both front and back? Can you give me direction and advice on how I can make money with Python/html5/CSS3 skills? -------------- next part -------------- An HTML attachment was scrubbed... URL: From ldl08 at gmx.net Sun Jun 24 18:11:10 2012 From: ldl08 at gmx.net (David) Date: Sun, 24 Jun 2012 18:11:10 +0200 Subject: [Tutor] joining selected items in list Message-ID: <4FE73C1E.30302@gmx.net> Dear Tutors, I have a list that I wish to reorganise into fewer list items. What happens is that some of the items belong together: not ['keine', 'Antwort'] but ['Keine Antwort']. I am not aware of any list methods that can help me here, and would thus be grateful for a hint or two. Thank you! David Original list: [['Intervall-', 'Anzahl', 'Rufzeit', 'Rufzeit', 'Rufzeit', 'Rufzeit', '>', 'Mittlere', 'Anzahl', 'Unzul\xe4ssiger', '\xdcberlauf', 'Zielanschlu\xdf', 'keine', 'Antwort', 'nicht', 'aktiv', 'Ung\xfcltiger', 'REST', '(andere']] What I want is this: [['Intervall-', 'Anzahl', 'Rufzeit', 'Rufzeit', 'Rufzeit', 'Rufzeit >', 'Mittlere', 'Anzahl', 'Unzul\xe4ssiger', '\xdcberlauf', 'Zielanschlu\xdf', 'keine Antwort', 'nicht aktiv', 'Ung\xfcltiger', 'REST' (andere']] From emile at fenx.com Sun Jun 24 20:26:21 2012 From: emile at fenx.com (Emile van Sebille) Date: Sun, 24 Jun 2012 11:26:21 -0700 Subject: [Tutor] joining selected items in list In-Reply-To: <4FE73C1E.30302@gmx.net> References: <4FE73C1E.30302@gmx.net> Message-ID: On 6/24/2012 9:11 AM David said... > Dear Tutors, > > I have a list that I wish to reorganise into fewer list items. > What happens is that some of the items belong together: > not ['keine', 'Antwort'] but ['Keine Antwort']. Can you define the complete list of items that belong together? If so, there's are several techniques for locations a sub-list within a list at http://stackoverflow.com/questions/2250633/python-find-a-list-within-members-of-another-listin-order But if not, I'm not sure there's an answer. Emile > > I am not aware of any list methods that can help me here, and would thus > be grateful for a hint or two. > > Thank you! > > David > > > > Original list: > > > [['Intervall-', 'Anzahl', 'Rufzeit', 'Rufzeit', 'Rufzeit', 'Rufzeit', > '>', 'Mittlere', 'Anzahl', 'Unzul\xe4ssiger', '\xdcberlauf', > 'Zielanschlu\xdf', 'keine', 'Antwort', 'nicht', 'aktiv', > 'Ung\xfcltiger', 'REST', '(andere']] > > What I want is this: > > [['Intervall-', > 'Anzahl', > 'Rufzeit', > 'Rufzeit', > 'Rufzeit', > 'Rufzeit>', > 'Mittlere', > 'Anzahl', > 'Unzul\xe4ssiger', > '\xdcberlauf', > 'Zielanschlu\xdf', > 'keine Antwort', > 'nicht aktiv', > 'Ung\xfcltiger', > 'REST' (andere']] > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From promis at yorku.ca Sun Jun 24 20:17:56 2012 From: promis at yorku.ca (promis at yorku.ca) Date: Sun, 24 Jun 2012 14:17:56 -0400 Subject: [Tutor] problem in opening files Message-ID: <20120624141756.91yw6ll3k8444kww@mail.math.yorku.ca> I'm am just new to Python, and am having trouble getting started. I am trying to open files of data that I want to work on, and keep on getting a message that no such file exist. For example here is a copy of what I've typed and the error message >>> fo = open('mort.txt') THIS IS WHAT I TYPED AND THE MESSAGE FOLLOWS Traceback (most recent call last): File "", line 1, in fo = open('mort.txt') IOError: [Errno 2] No such file or directory: 'mort.txt' >>> The file mort.txt certainly seems to me to exist. I can see it on my desktop. It was a small sample file that I just created to test this out. At first I was just using 'mort', which is what appears on my desktop, but it was suggested to me that I should include the extension .txt, which I have verified is the correct extension, so that's not the problem anymore. I have in fact tried this with a number of files and I get the same message every time. I am using Python versus 3.2.2. , on a MAC with system OS 10.6.8 . Any help would be appreciated David From ccesareo at legend3d.com Sun Jun 24 20:32:15 2012 From: ccesareo at legend3d.com (Craig Cesareo) Date: Sun, 24 Jun 2012 18:32:15 +0000 Subject: [Tutor] problem in opening files In-Reply-To: <20120624141756.91yw6ll3k8444kww@mail.math.yorku.ca> References: <20120624141756.91yw6ll3k8444kww@mail.math.yorku.ca> Message-ID: <1A3F906D-BDD1-40F4-B0C9-B72571869B01@legend3d.com> Be sure your current working directory is your desktop. import os print os.getcwd() Otherwise specify the full path to the text file. r'c:\users....\mort.txt' -- Craig Cesareo - iPhone Pipeline Technical Director http://www.legend3d.com/ http://www.craigcesareo.com/ 609.994.6370 On Jun 24, 2012, at 11:28 AM, "promis at yorku.ca" wrote: > I'm am just new to Python, and am having trouble getting started. I am trying to open files of data that I want to work on, and keep on getting a message that no such file exist. For example here is a copy of what I've typed and the error message > > > >>> fo = open('mort.txt') THIS IS WHAT I TYPED AND THE MESSAGE FOLLOWS > > Traceback (most recent call last): > File "", line 1, in > fo = open('mort.txt') > IOError: [Errno 2] No such file or directory: 'mort.txt' >>>> > > The file mort.txt certainly seems to me to exist. I can see it on my desktop. It was a small sample file that I just created to test this out. At first I was just using 'mort', which is what appears on my desktop, but it was suggested to me that I should include the extension .txt, which I have verified is the correct extension, so that's not the problem anymore. I have in fact tried this with a number of files and I get the same message every time. I am using Python versus 3.2.2. , on a MAC with system OS 10.6.8 . > > Any help would be appreciated > > David > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Sun Jun 24 21:35:16 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 24 Jun 2012 20:35:16 +0100 Subject: [Tutor] joining selected items in list In-Reply-To: <4FE73C1E.30302@gmx.net> References: <4FE73C1E.30302@gmx.net> Message-ID: On 24/06/12 17:11, David wrote: > Dear Tutors, > > I have a list that I wish to reorganise into fewer list items. > What happens is that some of the items belong together: > not ['keine', 'Antwort'] but ['Keine Antwort']. > > I am not aware of any list methods that can help me here, and would thus > be grateful for a hint or two. Can you define the set of rules whereby items are grouped? If not you are right there is nothing that can help you. But if you can then we should be able to suggest something. But for now your exact requirement seems more than a little arbitrary. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sun Jun 24 21:39:16 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 24 Jun 2012 20:39:16 +0100 Subject: [Tutor] problem in opening files In-Reply-To: <20120624141756.91yw6ll3k8444kww@mail.math.yorku.ca> References: <20120624141756.91yw6ll3k8444kww@mail.math.yorku.ca> Message-ID: On 24/06/12 19:17, promis at yorku.ca wrote: > >>> fo = open('mort.txt') THIS IS WHAT I TYPED AND THE MESSAGE > FOLLOWS > > Traceback (most recent call last): > File "", line 1, in > fo = open('mort.txt') > IOError: [Errno 2] No such file or directory: 'mort.txt' >>>> > > The file mort.txt certainly seems to me to exist. I can see it on my > desktop. It was a small sample file that I just created to test this Python is not psychic. It cannot guess where your file lives so it just looks in the current directory - the one from which you ran Python. If that's not where the file exists then you get an error., You need to provide the full path to the file or run the program from the same folder where the file lives. You can get similar problems if you don't have the right access permissions but since you created this file that's not going to be the problem here. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From andipersti at gmail.com Sun Jun 24 22:43:38 2012 From: andipersti at gmail.com (Andreas Perstinger) Date: Sun, 24 Jun 2012 22:43:38 +0200 Subject: [Tutor] joining selected items in list In-Reply-To: <4FE73C1E.30302@gmx.net> References: <4FE73C1E.30302@gmx.net> Message-ID: <20120624224338.92f19aeae0271f206fde48eb@gmail.com> On Sun, 24 Jun 2012 18:11:10 +0200 David wrote: > I have a list that I wish to reorganise into fewer list items. > What happens is that some of the items belong together: > not ['keine', 'Antwort'] but ['Keine Antwort']. > > I am not aware of any list methods that can help me here, and would > thus be grateful for a hint or two. If you know the indeces of the items which belong together, you could do for example: l = [['Intervall-', 'Anzahl', 'Rufzeit', 'Rufzeit', 'Rufzeit', 'Rufzeit', '>', 'Mittlere', 'Anzahl', 'Unzul\xe4ssiger', '\xdcberlauf', 'Zielanschlu\xdf', 'keine', 'Antwort', 'nicht', 'aktiv', 'Ung \xfcltiger', 'REST', '(andere']] indices = [5, 12, 14, 17] for index in reversed(indices): l[0][index] = " ".join([l[0][index], l[0].pop(index + 1)]) Bye, Andreas From steve at pearwood.info Mon Jun 25 01:54:20 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 25 Jun 2012 09:54:20 +1000 Subject: [Tutor] problem in opening files In-Reply-To: <1A3F906D-BDD1-40F4-B0C9-B72571869B01@legend3d.com> References: <20120624141756.91yw6ll3k8444kww@mail.math.yorku.ca> <1A3F906D-BDD1-40F4-B0C9-B72571869B01@legend3d.com> Message-ID: <4FE7A8AC.7020407@pearwood.info> Craig Cesareo wrote: > Be sure your current working directory is your desktop. > > import os > print os.getcwd() Be careful about changing the working directory from *within* Python. While you can do so, and in the hands of somebody who knows what they're doing it is perfectly fine, there are some pitfalls to avoid and you should consider it an advanced technique. But reading the WD with os.getcdd() is perfectly fine. -- Steven From steve at pearwood.info Mon Jun 25 02:23:14 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 25 Jun 2012 10:23:14 +1000 Subject: [Tutor] joining selected items in list In-Reply-To: <4FE73C1E.30302@gmx.net> References: <4FE73C1E.30302@gmx.net> Message-ID: <4FE7AF72.5020408@pearwood.info> David wrote: > Dear Tutors, > > I have a list that I wish to reorganise into fewer list items. > What happens is that some of the items belong together: > not ['keine', 'Antwort'] but ['Keine Antwort']. > > I am not aware of any list methods that can help me here, and would thus > be grateful for a hint or two. This solves like a good case for one of the oldest, most general techniques in use: an accumulator. You construct a new list, from the old list, but using a temporary accumulator first. The basic idea is: for each item in old list: if data in the accumulator is complete: move the completed accumulator data to the new list clear the accumulator so it is ready to start collecting fresh data otherwise: move item from old list to the accumulator There's a million variations on this, depending on whether you test the accumulator data at the start of the loop or the end of the loop, how and where you process items, etc. In your case, it sounds like it will be even easier, since you need collect *at most* two items: set a temporary variable to empty # the accumulator for each item in old list: if the temporary variable is not empty: combine the temp variable and the current item move them to the new list otherwise: if item is complete on its own: move it straight into the new list otherwise: move it to a temporary variable Converting this to Python is trivial: tmp = '' for item in old_list: if tmp: new_list.append(tmp + item) else: if stands_alone(item): # You must write this function. new_list.append(item) else: tmp = item Define this function first: def stands_alone(item): decide whether item stands alone, and if so, return True otherwise return False That decision function is the hard part. Is there some rule that tells you whether or not a word in the list belongs with the next word? -- Steven From kens7601 at gmail.com Mon Jun 25 04:00:57 2012 From: kens7601 at gmail.com (ken) Date: Sun, 24 Jun 2012 22:00:57 -0400 Subject: [Tutor] Tkinter message box Message-ID: <87r4t4w3om.fsf@gmail.com> If this belongs on another list, please let me know. I am using PYTHON 3 and tkinter. I have been playing around with messagebox. Is there any way I can display the value of a variable inside a message box? Thank you, Ken From steve at pearwood.info Mon Jun 25 06:07:08 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 25 Jun 2012 14:07:08 +1000 Subject: [Tutor] Tkinter message box In-Reply-To: <87r4t4w3om.fsf@gmail.com> References: <87r4t4w3om.fsf@gmail.com> Message-ID: <20120625040707.GA14784@ando> On Sun, Jun 24, 2012 at 10:00:57PM -0400, ken wrote: > > If this belongs on another list, please let me know. I am using PYTHON 3 > and tkinter. I have been playing around with messagebox. Is there any > way I can display the value of a variable inside a message box? Of course. tkinter doesn't care where the values come from, you can use a variable just as easily as a static string literal. Using Python 3.2: >>> import tkinter.messagebox >>> import time >>> title = "a trivial example".title() >>> message = "It is now %s, do you know where your computer is?" % time.asctime() >>> tkinter.messagebox.showinfo(title, message) 'ok' If that's not the sort of thing you mean, please show us what code you are using. -- Steven From alan.gauld at btinternet.com Mon Jun 25 09:21:39 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 25 Jun 2012 08:21:39 +0100 Subject: [Tutor] Tkinter message box In-Reply-To: <87r4t4w3om.fsf@gmail.com> References: <87r4t4w3om.fsf@gmail.com> Message-ID: On 25/06/12 03:00, ken wrote: > > If this belongs on another list, please let me know. I am using PYTHON 3 > and tkinter. I have been playing around with messagebox. Is there any > way I can display the value of a variable inside a message box? Yes, just insert it intro the message string prior to displaying the messagebox: import tkMessageBox num = 6 tkMessageBox.showinfo("Village News", "Message for number %d" % num) HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From ecofunds.developer at gmail.com Mon Jun 25 15:21:02 2012 From: ecofunds.developer at gmail.com (Developer Ecofunds) Date: Mon, 25 Jun 2012 10:21:02 -0300 Subject: [Tutor] Working with lists - why does my script not work? Message-ID: Hello guys, I'd like to ask you a little question about a script I did and isn't working properly. It is one excercise from googles python classes < http://code.google.com/edu/languages/google-python-class/set-up.html> I'm using python 2.5 because it's the one works with Google App Engine < https://developers.google.com/appengine/docs/python/gettingstarted/> This is the problem: ##########################3 # B. front_x # Given a list of strings, return a list with the strings # in sorted order, except group all the strings that begin with 'x' first. # e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields # ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'] # Hint: this can be done by making 2 lists and sorting each of them # before combining them. # This is the code I did -- Romulo. def front_x(words): x_list = [] for string in words: if string[0]=='x': x_list.append(string) words.remove(string) sorted(words) sorted(x_list) x_list.extend(words) return x_list ############################## The problem with this code is that it only gets the first word beginning with x and the others remaisn on the original list, sorted at the end. I've tested on terminal many parts of the code and it whas fine, but when I run it complete, it does not work. Following is the solution of the problem. I can understand it, I just can't understand why my code does not work. ############################# def front_x(words): x_list = [] other_list = [] for w in words: if w.startswith('x'): x_list.append(w) else: other_list.append(w) return sorted(x_list) + sorted(other_list) ############################## To download all the exercises, access: http://code.google.com/edu/languages/google-python-class/google-python-exercises.zip Thank y'all. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ccesareo at legend3d.com Mon Jun 25 15:28:18 2012 From: ccesareo at legend3d.com (Craig Cesareo) Date: Mon, 25 Jun 2012 13:28:18 +0000 Subject: [Tutor] Working with lists - why does my script not work? In-Reply-To: References: Message-ID: <7CC01651-953E-4794-A852-20879D2AF1D5@legend3d.com> Try putting. [:] After "words" in the for loop line. What's happening is you're removing words from the list you are iterating through and it's messing with the loop. The empty indice I'm having you insert makes an in place copy of the list so your remove doesn't effect it. -- Craig Cesareo - iPhone Pipeline Technical Director http://www.legend3d.com/ http://www.craigcesareo.com/ 609.994.6370 On Jun 25, 2012, at 6:23 AM, "Developer Ecofunds" > wrote: Hello guys, I'd like to ask you a little question about a script I did and isn't working properly. It is one excercise from googles python classes I'm using python 2.5 because it's the one works with Google App Engine This is the problem: ##########################3 # B. front_x # Given a list of strings, return a list with the strings # in sorted order, except group all the strings that begin with 'x' first. # e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields # ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'] # Hint: this can be done by making 2 lists and sorting each of them # before combining them. # This is the code I did -- Romulo. def front_x(words): x_list = [] for string in words: if string[0]=='x': x_list.append(string) words.remove(string) sorted(words) sorted(x_list) x_list.extend(words) return x_list ############################## The problem with this code is that it only gets the first word beginning with x and the others remaisn on the original list, sorted at the end. I've tested on terminal many parts of the code and it whas fine, but when I run it complete, it does not work. Following is the solution of the problem. I can understand it, I just can't understand why my code does not work. ############################# def front_x(words): x_list = [] other_list = [] for w in words: if w.startswith('x'): x_list.append(w) else: other_list.append(w) return sorted(x_list) + sorted(other_list) ############################## To download all the exercises, access: http://code.google.com/edu/languages/google-python-class/google-python-exercises.zip Thank y'all. _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From ecofunds.developer at gmail.com Mon Jun 25 15:33:44 2012 From: ecofunds.developer at gmail.com (Developer Ecofunds) Date: Mon, 25 Jun 2012 10:33:44 -0300 Subject: [Tutor] List Methods, String Slices and For loops Message-ID: Hello guys, I'd like to ask you a little question about a script I did and isn't working properly. It is one excercise from googles python classes < http://code.google.com/edu/languages/google-python-class/set-up.html> I'm using python 2.5 because it's the one works with Google App Engine < https://developers.google.com/appengine/docs/python/gettingstarted/> This is the problem: ##########################3 # B. front_x # Given a list of strings, return a list with the strings # in sorted order, except group all the strings that begin with 'x' first. # e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields # ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'] # Hint: this can be done by making 2 lists and sorting each of them # before combining them. # This is the code I did -- Romulo. def front_x(words): x_list = [] for string in words: if string[0]=='x': x_list.append(string) words.remove(string) sorted(words) sorted(x_list) x_list.extend(words) return x_list ############################## The problem with this code is that it only gets the first word beginning with x and the others remaisn on the original list, sorted at the end. I've tested on terminal many parts of the code and it whas fine, but when I run it complete, it does not work. Following is the solution of the problem. I can understand it, I just can't understand why my code does not work. ############################# def front_x(words): x_list = [] other_list = [] for w in words: if w.startswith('x'): x_list.append(w) else: other_list.append(w) return sorted(x_list) + sorted(other_list) ############################## To download all the exercises, access: http://code.google.com/edu/languages/google-python-class/google-python-exercises.zip Thank y'all. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ecofunds.developer at gmail.com Mon Jun 25 15:35:38 2012 From: ecofunds.developer at gmail.com (Developer Ecofunds) Date: Mon, 25 Jun 2012 10:35:38 -0300 Subject: [Tutor] List Methods, String Slices and For loops In-Reply-To: References: Message-ID: Soory by the new message with same question. The goup said my subject was messy and the previous message was denied. Please ignore. 2012/6/25 Developer Ecofunds > Hello guys, > > I'd like to ask you a little question about a script I did and isn't > working properly. > It is one excercise from googles python classes < > http://code.google.com/edu/languages/google-python-class/set-up.html> > > I'm using python 2.5 because it's the one works with Google App Engine < > https://developers.google.com/appengine/docs/python/gettingstarted/> > > This is the problem: > > ##########################3 > > # B. front_x > # Given a list of strings, return a list with the strings > # in sorted order, except group all the strings that begin with 'x' first. > # e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields > # ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'] > # Hint: this can be done by making 2 lists and sorting each of them > # before combining them. > > # This is the code I did -- Romulo. > > def front_x(words): > x_list = [] > for string in words: > if string[0]=='x': > x_list.append(string) > words.remove(string) > sorted(words) > sorted(x_list) > x_list.extend(words) > return x_list > > ############################## > > The problem with this code is that it only gets the first word beginning > with x and the others remaisn on the original list, sorted at the end. I've > tested on terminal many parts of the code and it whas fine, but when I run > it complete, it does not work. > > Following is the solution of the problem. I can understand it, I just > can't understand why my code does not work. > > ############################# > def front_x(words): > x_list = [] > other_list = [] > for w in words: > if w.startswith('x'): > x_list.append(w) > else: > other_list.append(w) > return sorted(x_list) + sorted(other_list) > ############################## > > To download all the exercises, access: > http://code.google.com/edu/languages/google-python-class/google-python-exercises.zip > > > Thank y'all. > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kwpolska at gmail.com Mon Jun 25 15:37:38 2012 From: kwpolska at gmail.com (Kwpolska) Date: Mon, 25 Jun 2012 15:37:38 +0200 Subject: [Tutor] Working with lists - why does my script not work? In-Reply-To: References: Message-ID: On Mon, Jun 25, 2012 at 3:21 PM, Developer Ecofunds wrote: > I'm using python 2.5 because it's the one works with Google App Engine > Not quite. > Note: If you are starting a new project, it is recommended that you use the Python 2.7 runtime. This tutorial is for the older version of the Python runtime that lacks many of the newer App Engine features. >From your link. -- Kwpolska stop html mail ? ? ?| always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 ? | Arch Linux x86_64, zsh, mutt, vim. # vim:set textwidth=70: From punchagan at gmail.com Mon Jun 25 15:39:54 2012 From: punchagan at gmail.com (Puneeth Chaganti) Date: Mon, 25 Jun 2012 19:09:54 +0530 Subject: [Tutor] Working with lists - why does my script not work? In-Reply-To: References: Message-ID: On Mon, Jun 25, 2012 at 6:51 PM, Developer Ecofunds wrote: > Hello guys, > > I'd like to ask you a little question about a script I did and isn't working > properly. > It is one excercise from googles python classes > > > I'm using python 2.5 because it's the one works with Google App Engine > > > This is the problem: > > ##########################3 > > # B. front_x > # Given a list of strings, return a list with the strings > # in sorted order, except group all the strings that begin with 'x' first. > # e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields > # ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'] > # Hint: this can be done by making 2 lists and sorting each of them > # before combining them. > > # This is the code I did -- Romulo. > > def front_x(words): > ? x_list = [] > ? for string in words: > ? ? if string[0]=='x': > ? ? ? x_list.append(string) > ? ? ? words.remove(string) > ? sorted(words) > ? sorted(x_list) `sorted`[0] returns a new list unlike `sort`[1], which sorts a list in-place. Something like this, would work: words = sorted(words) x_list = sorted(x_list) Also, in your code, you are changing the `words` list, while you are iterating over it, and this will cause problems while you iterate over the list. Generate a copy of the list and iterate over it by iterating over `words[:]` instead of `words`. Hope that helps, Puneeth [0] - http://docs.python.org/library/functions.html#sorted [1] - http://docs.python.org/tutorial/datastructures.html#more-on-lists From ccesareo at legend3d.com Mon Jun 25 15:39:19 2012 From: ccesareo at legend3d.com (Craig Cesareo) Date: Mon, 25 Jun 2012 13:39:19 +0000 Subject: [Tutor] List Methods, String Slices and For loops In-Reply-To: References: , Message-ID: In case you didn't get my reply on the previous message... Try putting. [:] After "words" in the for loop line. What's happening is you're removing words from the list you are iterating through and it's messing with the loop. The empty indice I'm having you insert makes an in place copy of the list so your remove doesn't effect it. On Jun 25, 2012, at 6:37 AM, "Developer Ecofunds" > wrote: Soory by the new message with same question. The goup said my subject was messy and the previous message was denied. Please ignore. 2012/6/25 Developer Ecofunds > Hello guys, I'd like to ask you a little question about a script I did and isn't working properly. It is one excercise from googles python classes I'm using python 2.5 because it's the one works with Google App Engine This is the problem: ##########################3 # B. front_x # Given a list of strings, return a list with the strings # in sorted order, except group all the strings that begin with 'x' first. # e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields # ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'] # Hint: this can be done by making 2 lists and sorting each of them # before combining them. # This is the code I did -- Romulo. def front_x(words): x_list = [] for string in words: if string[0]=='x': x_list.append(string) words.remove(string) sorted(words) sorted(x_list) x_list.extend(words) return x_list ############################## The problem with this code is that it only gets the first word beginning with x and the others remaisn on the original list, sorted at the end. I've tested on terminal many parts of the code and it whas fine, but when I run it complete, it does not work. Following is the solution of the problem. I can understand it, I just can't understand why my code does not work. ############################# def front_x(words): x_list = [] other_list = [] for w in words: if w.startswith('x'): x_list.append(w) else: other_list.append(w) return sorted(x_list) + sorted(other_list) ############################## To download all the exercises, access: http://code.google.com/edu/languages/google-python-class/google-python-exercises.zip Thank y'all. _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Mon Jun 25 15:51:31 2012 From: wprins at gmail.com (Walter Prins) Date: Mon, 25 Jun 2012 14:51:31 +0100 Subject: [Tutor] List Methods, String Slices and For loops In-Reply-To: References: Message-ID: Hi, Just one tangential comment: On 25 June 2012 14:33, Developer Ecofunds wrote: > I'm using python 2.5 because it's the one works with Google App Engine > GAE works with Python 2.7 also: https://developers.google.com/appengine/docs/python/gettingstartedpython27/ https://developers.google.com/appengine/docs/python/python27/newin27 (You can in fact see this in the link you posted also in the left-hand sidebar below the part about Python 2.5 that you referenced.) As you'll see there's lots of features only available on 2.7, so it's probably highly preferable to work on 2.7 rather than 2.5. Cheers, Walter From alan.gauld at btinternet.com Mon Jun 25 22:23:40 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 25 Jun 2012 21:23:40 +0100 Subject: [Tutor] Working with lists - why does my script not work? In-Reply-To: References: Message-ID: On 25/06/12 14:39, Puneeth Chaganti wrote: > `sorted`[0] returns a new list unlike `sort`[1], which sorts a list in-place. > > Something like this, would work: > > words = sorted(words) > x_list = sorted(x_list) or just words.sort() x_list.sort() which will work in place as mentioned above. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From mnickey at gmail.com Tue Jun 26 00:12:46 2012 From: mnickey at gmail.com (Mike Nickey) Date: Mon, 25 Jun 2012 15:12:46 -0700 Subject: [Tutor] Tutor Digest, Vol 100, Issue 58 In-Reply-To: References: Message-ID: >> The problem with this code is that it only gets the first word beginning >> with x and the others remaisn on the original list, sorted at the end. I've >> tested on terminal many parts of the code and it whas fine, but when I run >> it complete, it does not work. >> >> Following is the solution of the problem. I can understand it, I just >> can't understand why my code does not work. >> >> ############################# >> def front_x(words): >> ? x_list = [] >> ? other_list = [] >> ? for w in words: >> ? ? if w.startswith('x'): >> ? ? ? x_list.append(w) >> ? ? else: >> ? ? ? other_list.append(w) >> ? return sorted(x_list) + sorted(other_list) >> ############################## I did the same exercise so maybe I can assist here. From what I see, the line... if w.startswith('x'): seems to be ineffective. Try using the operator that checks for equality such as if w[0] == 'x': This will check the first of each word for 'x' and move it to the proper list. You should be able to figure it out from here. Good luck! >> >> To download all the exercises, access: >> http://code.google.com/edu/languages/google-python-class/google-python-exercises.zip >> >> >> Thank y'all. >> >> >> >> > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: > > ------------------------------ > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > > End of Tutor Digest, Vol 100, Issue 58 > ************************************** -- ~MEN From emile at fenx.com Tue Jun 26 01:26:55 2012 From: emile at fenx.com (Emile van Sebille) Date: Mon, 25 Jun 2012 16:26:55 -0700 Subject: [Tutor] Tutor Digest, Vol 100, Issue 58 In-Reply-To: References: Message-ID: On 6/25/2012 3:12 PM Mike Nickey said... >>> The problem with this code is that it only gets the first word beginning >>> with x and the others remaisn on the original list, sorted at the end. I've >>> tested on terminal many parts of the code and it whas fine, but when I run >>> it complete, it does not work. >>> >>> Following is the solution of the problem. I can understand it, I just >>> can't understand why my code does not work. >>> >>> ############################# >>> def front_x(words): >>> x_list = [] >>> other_list = [] >>> for w in words: >>> if w.startswith('x'): >>> x_list.append(w) >>> else: >>> other_list.append(w) >>> return sorted(x_list) + sorted(other_list) >>> ############################## > > I did the same exercise so maybe I can assist here. From what I see, the line... > if w.startswith('x'): seems to be ineffective. Why do you think so? > Try using the operator that checks for equality such as > if w[0] == 'x': This does the same thing and works even when w is an empty string. Emile From alan.gauld at btinternet.com Tue Jun 26 02:07:31 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 26 Jun 2012 01:07:31 +0100 Subject: [Tutor] Tutor Digest, Vol 100, Issue 58 In-Reply-To: References: Message-ID: On 25/06/12 23:12, Mike Nickey wrote: > I did the same exercise so maybe I can assist here. From what I see, the line... > if w.startswith('x'): seems to be ineffective. If that's true you have a seriously broken version of Python! startswith() is a better idiom because it conveys the intent much more clearly. And it should work just fine! >>> 'xray'.startswith('x') True >>> 'xray'.startswith('y') False >>> The only real downside is a few extra characters typing! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue Jun 26 02:10:09 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 26 Jun 2012 01:10:09 +0100 Subject: [Tutor] Tutor Digest, Vol 100, Issue 58 In-Reply-To: References: Message-ID: Hi Emile, On 26/06/12 00:26, Emile van Sebille wrote: >> Try using the operator that checks for equality such as >> if w[0] == 'x': > > This does the same thing and works even when w is an empty string. Umm, so does startswith()? Can you clarify what you mean by the last sentence? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue Jun 26 02:13:58 2012 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Tue, 26 Jun 2012 01:13:58 +0100 (BST) Subject: [Tutor] Working with lists - why does my script not work? In-Reply-To: References: Message-ID: <1340669638.56414.YahooMailNeo@web87702.mail.ir2.yahoo.com> CCing the list, please use replyAll when responding to the list. ? By the way for some reason, the google class says that the functions sorted() is better than the method list.sort(). > > > >" list.sort() -- sorts the list in place (does not return it). (The sorted() function shown below is preferred.) " > >http://code.google.com/edu/languages/google-python-class/lists.html > > >Somebody knows why? > >No idea to be honest but it could be because in pure computer science theory functions that return a value (ie sorted) are considered better than functions? that change?things as?a side effect (ie sort). ? Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Tue Jun 26 03:22:54 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 26 Jun 2012 11:22:54 +1000 Subject: [Tutor] Tutor Digest, Vol 100, Issue 58 In-Reply-To: References: Message-ID: <4FE90EEE.3090508@pearwood.info> Alan Gauld wrote: > Hi Emile, > > On 26/06/12 00:26, Emile van Sebille wrote: >>> Try using the operator that checks for equality such as >>> if w[0] == 'x': >> >> This does the same thing and works even when w is an empty string. > > Umm, so does startswith()? Can you clarify what you mean by the last > sentence? I think you've messed up your quoting. It was Mike Nickey, not Emile, who suggested using w[0] == 'x'. As Emile said, startswith is better because it works when w is an empty string, while the w[0]=='x' test fails: py> w = '' py> w.startswith('x') False py> w[0] == 'x' Traceback (most recent call last): File "", line 1, in IndexError: string index out of range -- Steven From alan.gauld at btinternet.com Tue Jun 26 10:10:51 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 26 Jun 2012 09:10:51 +0100 Subject: [Tutor] Tutor Digest, Vol 100, Issue 58 In-Reply-To: <4FE90EEE.3090508@pearwood.info> References: <4FE90EEE.3090508@pearwood.info> Message-ID: On 26/06/12 02:22, Steven D'Aprano wrote: > I think you've messed up your quoting. It was Mike Nickey, not Emile, > who suggested using w[0] == 'x'. Yes, but Emile's comment was in context of Mike's assertion about w[0]. However, reading it back I think that the "This" in Emile's comment was actually intended to refer back to the startswith discussed in his earlier(unquoted) comment... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From elainahyde at gmail.com Tue Jun 26 10:40:50 2012 From: elainahyde at gmail.com (Elaina Ann Hyde) Date: Tue, 26 Jun 2012 18:40:50 +1000 Subject: [Tutor] Looping over histogram plots Message-ID: Hello all, I have been making some big multiplots lately and found a nice little way of writing out 30 plots as follows, this part works great and leads up to my question, here I have 30 sets defined by the set=(), in this case I get a nice arrangement of 30 plots for V(GSR) and Log(g) (2 variables): ----------------------------------------- fig, axes = plt.subplots(nrows=5, ncols=6, figsize=(12,6)) index=0 for b in axes: for ax in b: index=index+1 set=(dat['a'+str(index)] == 1.00) #write the data ax.plot(VGSR[set],logg[set],'.b') #label the axis if index==13.0: ax.set_ylabel('counts') if index >= 25.0: ax.set_xlabel('VGSR') plt.show() However, if I want a histogram plot instead, I get my histogram only on the last i.e. set(dat['a30']==1) plot, so instead of 30 nice plots I get 29 empty ones and one crowded plot with some number of histograms in it. The set is the same, the data is the same, the only difference is the histogram, the code also looks pretty much the same, it is: #---------------------------------------------- fig, axes = plt.subplots(nrows=5, ncols=6, figsize=(12,6)) index=0 for b in axes: for ax in b: index=index+1 set=(dat['a'+str(index)] == 1.00) #write the data P.hist(VGSR[set],bins=30, normed=True) #label the axis if index==13.0: ax.set_ylabel('counts') if index >= 25.0: ax.set_xlabel('VGSR') plt.show() #----------------------------------------------- Here I use import pylab as P and import matplotlib.pyplot as plt Any ideas would be appreciated, thanks in advance! ~Elaina -- PhD Candidate Department of Physics and Astronomy Faculty of Science Macquarie University North Ryde, NSW 2109, Australia -------------- next part -------------- An HTML attachment was scrubbed... URL: From dfjennings at gmail.com Tue Jun 26 13:19:22 2012 From: dfjennings at gmail.com (Don Jennings) Date: Tue, 26 Jun 2012 07:19:22 -0400 Subject: [Tutor] Looping over histogram plots In-Reply-To: References: Message-ID: <2F1B56F1-4F78-44E7-93D0-A46829F0F15E@gmail.com> > Message: 1 > Date: Tue, 26 Jun 2012 18:40:50 +1000 > From: Elaina Ann Hyde > To: tutor at python.org > Subject: [Tutor] Looping over histogram plots > set=(dat['a'+str(index)] == 1.00) You should not override the builtin set() type [1] as you've done here by assigning it. > #write the data > P.hist(VGSR[set],bins=30, normed=True) I am not familiar with matplotlib, etc. but given that the primary difference in your two code samples is where you write the data, I suspect you want something like: ax.plot(P.hist(VGSR[set],bins=30, normed=True)) Take care, Don [1] http://docs.python.org/library/stdtypes.html#set From zebra05 at gmail.com Tue Jun 26 15:34:50 2012 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Tue, 26 Jun 2012 15:34:50 +0200 Subject: [Tutor] Barcode decoder in Python Message-ID: Hi everyone, Is there any decent barcode decoder software which one could use to read image barcodes and return a result to a calling function/ app? I wish to implement a Python server backend which would import and use such a module (if it were that, for instance). Thanks. -- Regards, Sithembewena Lloyd Dube -------------- next part -------------- An HTML attachment was scrubbed... URL: From zebra05 at gmail.com Tue Jun 26 16:52:37 2012 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Tue, 26 Jun 2012 16:52:37 +0200 Subject: [Tutor] Generating random alphanumeric codes Message-ID: HI, Would anyone have tips on how to generate random 4-digit alphanumeric codes in python? Also, how does one calculate the number of possible combinations? Thanks in advance. -- Regards, Sithembewena Lloyd Dube -------------- next part -------------- An HTML attachment was scrubbed... URL: From hugo.yoshi at gmail.com Tue Jun 26 17:04:40 2012 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Tue, 26 Jun 2012 17:04:40 +0200 Subject: [Tutor] Generating random alphanumeric codes In-Reply-To: References: Message-ID: On Tue, Jun 26, 2012 at 4:52 PM, Sithembewena Lloyd Dube wrote: > HI, > > Would anyone have tips on how to generate random 4-digit alphanumeric > codes in python? Also, how does one calculate the number of possible > combinations? > > Thanks in advance. > > Python's, random module is your friend. I'd suggest looking at the random.choice() function. As for the number of possibilities, if your codes are not case sensitive, you'll have 10 + 26 = 36 possibilities for each character/digit. 4 digits, so 36**4 possible codes. Hugo -------------- next part -------------- An HTML attachment was scrubbed... URL: From taserian at gmail.com Tue Jun 26 17:08:41 2012 From: taserian at gmail.com (taserian) Date: Tue, 26 Jun 2012 11:08:41 -0400 Subject: [Tutor] Generating random alphanumeric codes In-Reply-To: References: Message-ID: First of all, determine your alphabet (the pool of characters you'll derive your 4-character code from): - For example, using an English alphabet with both lowercase and uppercase letters and digits 0-9 makes for 62 characters (26 + 26 + 10). Then, ask if you want to allow a character to repeat itself in the 4-character code. - If so, then it's easy: take the length of your alphabet, and calculate (length)^4. In my example above, it would be 62^4 = 14,776,336 - If not, then it's a little more complicated, calculate (length) * (length-1) * (length-2) * (length-3), or in other words the factorial of length minus the factorial of (length - 4). In my example, 62! - (62-4)! = 13,388,280 AR On Tue, Jun 26, 2012 at 10:52 AM, Sithembewena Lloyd Dube wrote: > HI, > > Would anyone have tips on how to generate random 4-digit alphanumeric > codes in python? Also, how does one calculate the number of possible > combinations? > > Thanks in advance. > > -- > Regards, > Sithembewena Lloyd Dube > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From taserian at gmail.com Tue Jun 26 17:09:58 2012 From: taserian at gmail.com (taserian) Date: Tue, 26 Jun 2012 11:09:58 -0400 Subject: [Tutor] Generating random alphanumeric codes In-Reply-To: References: Message-ID: Correcting what was said below. - If not, then it's a little more complicated, calculate (length) * (length-1) * (length-2) * (length-3), or in other words the factorial of length divided by the factorial of (length - 4). In my example, 62! / (62-4)! = 13,388,280 On Tue, Jun 26, 2012 at 11:08 AM, taserian wrote: > First of all, determine your alphabet (the pool of characters you'll > derive your 4-character code from): > > - For example, using an English alphabet with both lowercase and uppercase > letters and digits 0-9 makes for 62 characters (26 + 26 + 10). > > Then, ask if you want to allow a character to repeat itself in the > 4-character code. > > - If so, then it's easy: take the length of your alphabet, and calculate > (length)^4. In my example above, it would be 62^4 = 14,776,336 > > - If not, then it's a little more complicated, calculate (length) * > (length-1) * (length-2) * (length-3), or in other words the factorial of > length minus the factorial of (length - 4). In my example, 62! - (62-4)! = > 13,388,280 > > AR > > On Tue, Jun 26, 2012 at 10:52 AM, Sithembewena Lloyd Dube < > zebra05 at gmail.com> wrote: > >> HI, >> >> Would anyone have tips on how to generate random 4-digit alphanumeric >> codes in python? Also, how does one calculate the number of possible >> combinations? >> >> Thanks in advance. >> >> -- >> Regards, >> Sithembewena Lloyd Dube >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Tue Jun 26 18:21:38 2012 From: emile at fenx.com (Emile van Sebille) Date: Tue, 26 Jun 2012 09:21:38 -0700 Subject: [Tutor] Tutor Digest, Vol 100, Issue 58 In-Reply-To: References: <4FE90EEE.3090508@pearwood.info> Message-ID: On 6/26/2012 1:10 AM Alan Gauld said... > On 26/06/12 02:22, Steven D'Aprano wrote: > >> I think you've messed up your quoting. It was Mike Nickey, not Emile, >> who suggested using w[0] == 'x'. > > Yes, but Emile's comment was in context of Mike's assertion about w[0]. > However, reading it back I think that the "This" in Emile's comment was > actually intended to refer back to the startswith discussed in his > earlier(unquoted) comment... Actually, my first comment at that location was phrased with a negative as in something like 'and doesn't suffer the side effect of failing when the string is empty like this does' but as I edited before hitting send I rephrased my response and left the 'this' oinappropriately placed. Emile From martin at linux-ip.net Tue Jun 26 21:47:06 2012 From: martin at linux-ip.net (Martin A. Brown) Date: Tue, 26 Jun 2012 15:47:06 -0400 Subject: [Tutor] Generating random alphanumeric codes In-Reply-To: References: Message-ID: Hello, : Would anyone have tips on how to generate random 4-digit : alphanumeric codes in python? Also, how does one calculate the : number of possible combinations? Here are some thoughts that come to my mind: import string import random # -- technique #1, using random.choice print ''.join([random.choice(string.lowercase) for x in range(4)]) # -- technique #2, using random.shuffle t = [ x for x in string.lowercase ] random.shuffle(t) ''.join(t[0:4]) # -- technique #3, using random.sample ''.join(random.sample(string.lowercase,4)) I would be quite surprised if there were not more efficient ways of accomplishing this. -Martin -- Martin A. Brown http://linux-ip.net/ From d at davea.name Tue Jun 26 22:47:21 2012 From: d at davea.name (Dave Angel) Date: Tue, 26 Jun 2012 16:47:21 -0400 Subject: [Tutor] Generating random alphanumeric codes In-Reply-To: References: Message-ID: <4FEA1FD9.7030105@davea.name> On 06/26/2012 03:47 PM, Martin A. Brown wrote: > Hello, > > : Would anyone have tips on how to generate random 4-digit > : alphanumeric codes in python? Also, how does one calculate the > : number of possible combinations? > > Here are some thoughts that come to my mind: > > import string > import random > > # -- technique #1, using random.choice > print ''.join([random.choice(string.lowercase) for x in range(4)]) > > # -- technique #2, using random.shuffle > t = [ x for x in string.lowercase ] > random.shuffle(t) > ''.join(t[0:4]) > > # -- technique #3, using random.sample > ''.join(random.sample(string.lowercase,4)) > > I would be quite surprised if there were not more efficient ways of > accomplishing this. > > -Martin > Two problems that I see: string.lowercase includes the lowercase letters, but not the digits. Your methods 2 and 3 produce different results than method1. The OP never specified which he wanted, but the distinction can well be important. Likewise his query about how many possibilities there are depends on the same question: Are duplicates permitted between the four characters in any given answer. -- DaveA From redacted@example.com Wed Jun 27 00:30:05 2012 From: redacted@example.com (Alexander Quest) Date: Tue, 26 Jun 2012 15:30:05 -0700 Subject: [Tutor] Re.findall question Message-ID: I'm a bit confused about extracting data using re.search or re.findall. Say I have the following code: tuples = re.findall(r'blahblah(\d+)yattayattayatta(\w+)moreblahblahblah(\w+)over', text) So I'm looking for that string in 'text', and I intend to extract the parts which have parentheses around them. And it works: the variable "tuples", which I assigned to get the return of re.findall, returns a tuple list, each 'element' therein being a tuple of 3 elements (which is what I wanted since I had 3 sets of parentheses). My question is how does Python know to return just the part in the parentheses and not to return the "blahblah" and the "yattayattayatta", etc...? The 're.search' function returns the whole thing, and if I want just the parentheses parts, I do tuples.group(1) or tuples.group(2) or tuples.group(3), depending on which set of parentheses I want. Does the re.findall command by default ignore anything outside of the parentheses and only return the parentheses as a grouping withing one tuple (i.e., the first element in "tuples" would be, as it is, a list comprised of 3 elements corresponding respectively to the 1st, 2nd, and 3rd parentheses)? Thank you for reading. -Alex -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Jun 27 01:15:34 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 27 Jun 2012 00:15:34 +0100 Subject: [Tutor] Re.findall question In-Reply-To: References: Message-ID: On 26/06/12 23:30, Alexander Quest wrote: > My question is how does Python know to return just the part in the > parentheses and not to return the "blahblah" and the "yattayattayatta", > etc...? If you want to know *how* Python does it you will have to read the module code (probably in C so download the source code) > ... Does the > re.findall command by default ignore anything outside of the parentheses > and only return the parentheses as a grouping withing one tuple The help() function returns: -------------- findall(pattern, string, flags=0) Return a list of all non-overlapping matches in the string. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result. ------------------ So that's what its defined to do. How it does it is another matter. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From elainahyde at gmail.com Wed Jun 27 01:32:36 2012 From: elainahyde at gmail.com (Elaina Ann Hyde) Date: Wed, 27 Jun 2012 09:32:36 +1000 Subject: [Tutor] Looping over histogram plots In-Reply-To: <2F1B56F1-4F78-44E7-93D0-A46829F0F15E@gmail.com> References: <2F1B56F1-4F78-44E7-93D0-A46829F0F15E@gmail.com> Message-ID: Dear Don, Thanks for the comment, the set type is no problem for me, this is just a variable that I call set... and it works great for my purposes, I do suspect it is something in the way that matplotlib/pyplot deals with histograms, but I have not so far been able to find the right syntax. Note, the first code example works great, it is only the second (with the hist attempt) that does not do well. It creates 29 blank plots and 1 histogram instead of 30 histograms... so probably it does need a different phrasing, but the one you suggest gives invalid syntax. ~Elaina On Tue, Jun 26, 2012 at 9:19 PM, Don Jennings wrote: > > Message: 1 > > Date: Tue, 26 Jun 2012 18:40:50 +1000 > > From: Elaina Ann Hyde > > To: tutor at python.org > > Subject: [Tutor] Looping over histogram plots > > > > > set=(dat['a'+str(index)] == 1.00) > > You should not override the builtin set() type [1] as you've done here by > assigning it. > > > #write the data > > P.hist(VGSR[set],bins=30, normed=True) > > I am not familiar with matplotlib, etc. but given that the primary > difference in your two code samples is where you write the data, I suspect > you want something like: > > ax.plot(P.hist(VGSR[set],bins=30, normed=True)) > > Take care, > Don > > [1] http://docs.python.org/library/stdtypes.html#set -- PhD Candidate Department of Physics and Astronomy Faculty of Science Macquarie University North Ryde, NSW 2109, Australia -------------- next part -------------- An HTML attachment was scrubbed... URL: From elainahyde at gmail.com Wed Jun 27 03:44:36 2012 From: elainahyde at gmail.com (Elaina Ann Hyde) Date: Wed, 27 Jun 2012 11:44:36 +1000 Subject: [Tutor] Looping over histogram plots In-Reply-To: References: <2F1B56F1-4F78-44E7-93D0-A46829F0F15E@gmail.com> Message-ID: Yay Python: The solution was a syntax one, if anyone else ever feels like massively multi-plotting histograms, here is the working code: #---------------------------------------------- fig, axes = plt.subplots(nrows=5, ncols=6, figsize=(12,6)) index=0 for b in axes: for ax in b: index=index+1 set=(dat['a'+str(index)] == 1.00) #write the data n, bins, patches = ax.hist(VGSR[set], 30, normed=1) #label the axis if index==13.0: ax.set_ylabel('counts') if index >= 25.0: ax.set_xlabel('VGSR') plt.show() #----------------------------------------------- ~Elaina Hyde -- PhD Candidate Department of Physics and Astronomy Faculty of Science Macquarie University North Ryde, NSW 2109, Australia -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Jun 27 09:47:08 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 27 Jun 2012 08:47:08 +0100 Subject: [Tutor] Looping over histogram plots In-Reply-To: References: <2F1B56F1-4F78-44E7-93D0-A46829F0F15E@gmail.com> Message-ID: On 27/06/12 00:32, Elaina Ann Hyde wrote: > Thanks for the comment, the set type is no problem for me, this is > just a variable that I call set... and it works great for my purposes, It may work just now but if you ever decide you need to use a Python set you will be unable to because you have effectively hidden that data type. And it's unlikely to beimmediately obvious why its not working. That's why its a bad idea to use the built-in type names as variables. eg: myList = [1,2,3,3,4,5,6,3,1,5] myUniqueList = list(set(mylist)) # remove duplicates In your program the second line would fail. If you are confident you will never use sets then its not an issue, but these decisions have a habit of coming back to bite you in the future! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Wed Jun 27 10:12:05 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 27 Jun 2012 18:12:05 +1000 Subject: [Tutor] Looping over histogram plots In-Reply-To: References: <2F1B56F1-4F78-44E7-93D0-A46829F0F15E@gmail.com> Message-ID: <20120627081205.GB14784@ando> On Wed, Jun 27, 2012 at 08:47:08AM +0100, Alan Gauld wrote: > On 27/06/12 00:32, Elaina Ann Hyde wrote: > > > Thanks for the comment, the set type is no problem for me, this is > >just a variable that I call set... and it works great for my purposes, > > It may work just now but if you ever decide you need to use a Python set > you will be unable to because you have effectively hidden that data > type. And it's unlikely to beimmediately obvious why its not working. > That's why its a bad idea to use the built-in type names as variables. This is called "shadowing a built-in", and is discouraged for exactly the reasons that Alan mentions. It is much less risky to shadow built-ins inside small functions, so you are not surprised that the built-in doesn't work as expected. When you do it deliberately to change the behaviour of a built-in, it is often called "monkey-patching"[1] which is a risky, sometimes useful but advanced technique. Nevertheless, any of these cases should be used with care, and only if necessary. [1] Sometimes called "raving insanity" *grin* -- Steven From breamoreboy at yahoo.co.uk Wed Jun 27 10:42:23 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 27 Jun 2012 09:42:23 +0100 Subject: [Tutor] Looping over histogram plots In-Reply-To: References: <2F1B56F1-4F78-44E7-93D0-A46829F0F15E@gmail.com> Message-ID: On 27/06/2012 08:47, Alan Gauld wrote: > On 27/06/12 00:32, Elaina Ann Hyde wrote: > >> Thanks for the comment, the set type is no problem for me, this is >> just a variable that I call set... and it works great for my purposes, > > It may work just now but if you ever decide you need to use a Python set > you will be unable to because you have effectively hidden that data > type. And it's unlikely to beimmediately obvious why its not working. > That's why its a bad idea to use the built-in type names as variables. > > eg: > > myList = [1,2,3,3,4,5,6,3,1,5] > myUniqueList = list(set(mylist)) # remove duplicates > > In your program the second line would fail. > If you are confident you will never use sets then its not an issue, but > these decisions have a habit of coming back to bite you in the future! > And they always seem to bite the most tender part of the anatomy that they can find :) -- Cheers. Mark Lawrence. From hdfatty99 at gmail.com Thu Jun 28 01:59:45 2012 From: hdfatty99 at gmail.com (Imran Javeed) Date: Thu, 28 Jun 2012 00:59:45 +0100 Subject: [Tutor] wordcount.py query Message-ID: Hello I have attempted the wordcount.py exercise and am trying to understand the logic behind the code. Im trying to replay the code on the python cmd line but keep getting this error >>> w_count[string] = w_count[string] + 1 Traceback (most recent call last): File "", line 1, in TypeError: cannot concatenate 'str' and 'int' objects I was trying to recreate the scenario whereby the dict value is incremented by 1 as in the following code, it works in the python script but not when i manually create & populate the dict value & increment it on the pythin cmd line, can you please explain why? def count_words(filename): w_count = {} file = open(filename, 'rU') for line in file: w = line.split() for string in w: string = string.lower() if not string in w_count: w_count[string] = 1 else:* ** w_count[string] = w_count[string] + 1* file.close() return w_count -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Thu Jun 28 02:17:21 2012 From: d at davea.name (Dave Angel) Date: Wed, 27 Jun 2012 20:17:21 -0400 Subject: [Tutor] wordcount.py query In-Reply-To: References: Message-ID: <4FEBA291.7070309@davea.name> On 06/27/2012 07:59 PM, Imran Javeed wrote: > Hello > > I have attempted the wordcount.py exercise and am trying to understand the > logic behind the code. > > Im trying to replay the code on the python cmd line but keep getting this > error > >>>> w_count[string] = w_count[string] + 1 > Traceback (most recent call last): > File "", line 1, in > TypeError: cannot concatenate 'str' and 'int' objects > > > I was trying to recreate the scenario whereby the dict value is incremented > by 1 as in the following code, it works in the python script but not when i > manually create & populate the dict value & increment it on the pythin cmd > line, can you please explain why? > > def count_words(filename): > w_count = {} > file = open(filename, 'rU') > for line in file: > w = line.split() > for string in w: > string = string.lower() > if not string in w_count: > w_count[string] = 1 > else:* > ** w_count[string] = w_count[string] + 1* > file.close() > return w_count > > Clearly you typed more at the interpreter prompt than that one line. So in one of your previous lines you must have set w_count[string] to a string value, rather than to the integer 1. -- DaveA From dmjohnsonn at gmail.com Thu Jun 28 02:19:47 2012 From: dmjohnsonn at gmail.com (moheem ilyas) Date: Wed, 27 Jun 2012 20:19:47 -0400 Subject: [Tutor] Reading file and storing keys Message-ID: I am working on a problem from a book, Think Python, which I thought would be fairly easy. The problem is: Exercise 11.1. Write a function that reads the words in words.txt and stores them as keys in a dictionary. It doesn?t matter what the values are. Then you can use the in operator as a fast way to check whether a string is in the dictionary. Note: words.txt is just a huge word list file if anyone is confused about that Here is my failed solution: def tester(): fin = open('/home/moheem/Documents/words.txt', 'r') value = 0 wordDict = dict() for word in fin: wordDict[word] = value value = value + 1 fin.close() There seems to be a logical error. That is, when I check a key, i.e. one of the words from the file, is in the dictionary, I get false. (To check, I use: 'aa' in wordDict). I think the problem is that the key does not actually get placed in the dictionary, but the question is why? -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Thu Jun 28 02:33:56 2012 From: d at davea.name (Dave Angel) Date: Wed, 27 Jun 2012 20:33:56 -0400 Subject: [Tutor] Reading file and storing keys In-Reply-To: References: Message-ID: <4FEBA674.3050006@davea.name> On 06/27/2012 08:19 PM, moheem ilyas wrote: > I am working on a problem from a book, Think Python, which I thought would > be fairly easy. The problem is: > > Exercise 11.1. Write a function that reads the words in words.txt and > stores them as keys in a > dictionary. It doesn?t matter what the values are. Then you can use the in > operator as a fast way to > check whether a string is in the dictionary. > > Note: words.txt is just a huge word list file if anyone is confused about > that > > Here is my failed solution: > > def tester(): > fin = open('/home/moheem/Documents/words.txt', 'r') > value = 0 > wordDict = dict() > for word in fin: > wordDict[word] = value > value = value + 1 > > fin.close() > > There seems to be a logical error. That is, when I check a key, i.e. one of > the words from the file, is in the dictionary, I get false. (To check, I > use: 'aa' in wordDict). I think the problem is that the key does not > actually get placed in the dictionary, but the question is why? > > Somehow you're thinking that the file consists only of words, and that the for loop you've got will give you those words one at a time. It doesn't. When you loop on a file that way, the lines each end in a newline character, so you've got to strip them off before using them as keys. for word in fin: word = word.rstrip() wordDict[word] = value value += 1 You could have discovered this by simply printing out wordDict -- DaveA From dmjohnsonn at gmail.com Thu Jun 28 02:38:45 2012 From: dmjohnsonn at gmail.com (moheem ilyas) Date: Wed, 27 Jun 2012 20:38:45 -0400 Subject: [Tutor] Reading file and storing keys In-Reply-To: <4FEBA674.3050006@davea.name> References: <4FEBA674.3050006@davea.name> Message-ID: Thanks. That makes sense, just failed to notice it. Btw, I tried printing out wordDict and Python became unresponsive. Any ideas On Wed, Jun 27, 2012 at 8:33 PM, Dave Angel wrote: > On 06/27/2012 08:19 PM, moheem ilyas wrote: > > I am working on a problem from a book, Think Python, which I thought > would > > be fairly easy. The problem is: > > > > Exercise 11.1. Write a function that reads the words in words.txt and > > stores them as keys in a > > dictionary. It doesn?t matter what the values are. Then you can use the > in > > operator as a fast way to > > check whether a string is in the dictionary. > > > > Note: words.txt is just a huge word list file if anyone is confused about > > that > > > > Here is my failed solution: > > > > def tester(): > > fin = open('/home/moheem/Documents/words.txt', 'r') > > value = 0 > > wordDict = dict() > > for word in fin: > > wordDict[word] = value > > value = value + 1 > > > > fin.close() > > > > There seems to be a logical error. That is, when I check a key, i.e. one > of > > the words from the file, is in the dictionary, I get false. (To check, I > > use: 'aa' in wordDict). I think the problem is that the key does not > > actually get placed in the dictionary, but the question is why? > > > > > > Somehow you're thinking that the file consists only of words, and that > the for loop you've got will give you those words one at a time. It > doesn't. > > When you loop on a file that way, the lines each end in a newline > character, so you've got to strip them off before using them as keys. > > for word in fin: > word = word.rstrip() > wordDict[word] = value > value += 1 > > You could have discovered this by simply printing out wordDict > > > > > > -- > > DaveA > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Thu Jun 28 02:47:26 2012 From: d at davea.name (Dave Angel) Date: Wed, 27 Jun 2012 20:47:26 -0400 Subject: [Tutor] Reading file and storing keys In-Reply-To: References: <4FEBA674.3050006@davea.name> Message-ID: <4FEBA99E.2080307@davea.name> On 06/27/2012 08:38 PM, moheem ilyas wrote: > Thanks. That makes sense, just failed to notice it. Btw, I tried printing > out wordDict and Python became unresponsive. Any ideas > Not without knowing how you tried it. Offhand, the only reason i can think of is that your were testing with a huge file. When debugging code, start with data which is small enough to keep track of in your head, then increase the size. -- DaveA From dmjohnsonn at gmail.com Thu Jun 28 03:16:13 2012 From: dmjohnsonn at gmail.com (moheem ilyas) Date: Wed, 27 Jun 2012 21:16:13 -0400 Subject: [Tutor] Reading file and storing keys In-Reply-To: <4FEBA99E.2080307@davea.name> References: <4FEBA674.3050006@davea.name> <4FEBA99E.2080307@davea.name> Message-ID: It was because I was testing with a large file. Thanks for your help/advice. On Wed, Jun 27, 2012 at 8:47 PM, Dave Angel wrote: > On 06/27/2012 08:38 PM, moheem ilyas wrote: > > Thanks. That makes sense, just failed to notice it. Btw, I tried printing > > out wordDict and Python became unresponsive. Any ideas > > > > Not without knowing how you tried it. Offhand, the only reason i can > think of is that your were testing with a huge file. When debugging > code, start with data which is small enough to keep track of in your > head, then increase the size. > > > > > -- > > DaveA > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Thu Jun 28 04:37:15 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 28 Jun 2012 12:37:15 +1000 Subject: [Tutor] wordcount.py query In-Reply-To: References: Message-ID: <20120628023715.GC14784@ando> On Thu, Jun 28, 2012 at 12:59:45AM +0100, Imran Javeed wrote: > Im trying to replay the code on the python cmd line but keep getting this > error > > >>> w_count[string] = w_count[string] + 1 > > Traceback (most recent call last): > File "", line 1, in > TypeError: cannot concatenate 'str' and 'int' objects > > > I was trying to recreate the scenario whereby the dict value is incremented > by 1 as in the following code, it works in the python script but not when i > manually create & populate the dict value & increment it on the pythin cmd > line, can you please explain why? Is the error message not clear? If you have a suggestion for what would be more helpful to you, please tell us. You are trying to concatenate (join) a string and an int (a number), and Python will not let you do that. Both these examples will raise an exception: "hello world" + 1 "42" + 1 These however will succeed: "hello world" + "1" "42" + "1" 42 + 1 Python is not Perl -- if you pass a string and a number to the plus operator, Python will not try to guess whether you wanted addition or concatenation. You must use two strings, or two numbers. If still having trouble diagnosing the problem, you can do this at the interactive interpreter: print(string) print(w_count[string]) print(type(w_count[string])) That will show you the values causing trouble. -- Steven From alan.gauld at btinternet.com Thu Jun 28 10:39:48 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 28 Jun 2012 09:39:48 +0100 Subject: [Tutor] Reading file and storing keys In-Reply-To: References: Message-ID: On 28/06/12 01:19, moheem ilyas wrote: > def tester(): > fin = open('/home/moheem/Documents/words.txt', 'r') > value = 0 > wordDict = dict() > for word in fin: > wordDict[word] = value > value = value + 1 > fin.close() > > There seems to be a logical error. That is, when I check a key, i.e. one > of the words from the file, is in the dictionary, I get false. Notice that you do not return the dictionary. So when the function completes the dictionary gets thrown away. You probably need a line like return wordDict at the end of the function. Then when you call tester assign the result to a variable: words = tester() > check, I use: 'aa' in wordDict). I think the problem is that the key > does not actually get placed in the dictionary, but the question is why? Then you can use 'aa' in words But the other advice about stripping the newlines still applies too! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From james at uplinkzero.com Thu Jun 28 19:19:54 2012 From: james at uplinkzero.com (James Chapman) Date: Thu, 28 Jun 2012 18:19:54 +0100 Subject: [Tutor] Opening filenames with unicode characters Message-ID: <1340903994.4fec923a7aac1@webmail.uplinkzero.com> Hi there python list. I'm trying to open a text file named "This is_a-test'FILE to Ensure$ that? stuff^ works.txt" (without the quotes) but I'm struggling to find a way to open it. >>> filename = "This is_a-test'FILE to Ensure$ that? stuff^ works.txt.js" >>> open(filename) Traceback (most recent call last): ? File "", line 1, in IOError: [Errno 2] No such file or directory: "This is_a-test'FILE to Ensure$ that\x9c stuff^ works.txt.js" >>> import codecs >>> codecs.open(filename, 'r', 'utf-8') Traceback (most recent call last): ? File "", line 1, in ? File "C:\Python27\lib\codecs.py", line 881, in open ??? file = __builtin__.open(filename, mode, buffering) IOError: [Errno 2] No such file or directory: "This is_a-test'FILE to Ensure$ that\x9c stuff^ works.txt.js" >>> os.listdir(".") ["This is_a-test'FILE to Ensure$ that\xa3 stuff^ works.txt.js"] >>> filename.decode() Traceback (most recent call last): ? File "", line 1, in UnicodeDecodeError: 'ascii' codec can't decode byte 0x9c in position 35: ordinal not in range(128) I've tried all sorts of encode and decode methods on the string containing the file name but nothing seems to be working. Any help would be appreciated. James PS: This is on 64bit Windows with ActivePython 2.7.3 -------------- next part -------------- An HTML attachment was scrubbed... URL: From mail at timgolden.me.uk Thu Jun 28 19:39:31 2012 From: mail at timgolden.me.uk (Tim Golden) Date: Thu, 28 Jun 2012 18:39:31 +0100 Subject: [Tutor] Opening filenames with unicode characters In-Reply-To: <1340903994.4fec923a7aac1@webmail.uplinkzero.com> References: <1340903994.4fec923a7aac1@webmail.uplinkzero.com> Message-ID: <4FEC96D3.7090707@timgolden.me.uk> On 28/06/2012 18:19, James Chapman wrote: > Hi there python list. > > I'm trying to open a text file named "This is_a-test'FILE to Ensure$ > that? stuff^ works.txt" (without the quotes) but I'm struggling to > find a way to open it. Happily, you're using Windows, which makes this very much easier. Short Explanation Do this: open (u"blah ?3.50.txt").read () # note the u- prefix Long Explanation: There's way too many levels of indirection between you and the disk to go into it all, but basically you're telling Python to create a (byte) string from those characters using its default encoding for code, which is UTF-8. UTF-8 maps "?" to the one-byte 0x9c. Somewhere in the layers between that string and the representation on disk, a mismatch occurs. TJG From james at uplinkzero.com Thu Jun 28 19:55:09 2012 From: james at uplinkzero.com (James Chapman) Date: Thu, 28 Jun 2012 18:55:09 +0100 Subject: [Tutor] Opening filenames with unicode characters Message-ID: <1340906109.4fec9a7d92170@webmail.uplinkzero.com> Thanks Tim, while this works, I need the name to be stored in a variable as it's dynamic. In other words, how do I rewrite open(u"blah?.txt") to be filename = "blah?.txt" open(filename) At Thursday, 28/06/2012 on 18:39 Tim Golden wrote: On 28/06/2012 18:19, James Chapman wrote: > Hi there python list. > > I'm trying to open a text file named "This is_a-test'FILE to Ensure$ > that? stuff^ works.txt" (without the quotes) but I'm struggling to > find a way to open it. Happily, you're using Windows, which makes this very much easier. Short Explanation Do this: open (u"blah ?3.50.txt").read () # note the u- prefix Long Explanation: There's way too many levels of indirection between you and the disk to go into it all, but basically you're telling Python to create a (byte) string from those characters using its default encoding for code, which is UTF-8. UTF-8 maps "?" to the one-byte 0x9c. Somewhere in the layers between that string and the representation on disk, a mismatch occurs. TJG _______________________________________________ Tutor maillist??-??Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From malaclypse2 at gmail.com Thu Jun 28 19:58:18 2012 From: malaclypse2 at gmail.com (Jerry Hill) Date: Thu, 28 Jun 2012 13:58:18 -0400 Subject: [Tutor] Opening filenames with unicode characters In-Reply-To: <1340906109.4fec9a7d92170@webmail.uplinkzero.com> References: <1340906109.4fec9a7d92170@webmail.uplinkzero.com> Message-ID: On Thu, Jun 28, 2012 at 1:55 PM, James Chapman wrote: > Thanks Tim, while this works, I need the name to be stored in a variable as > it's dynamic. > > In other words, how do I rewrite > open(u"blah?.txt") > > to be > filename = "blah?.txt" > open(filename) You have to create a unicode-string, not a byte-string. So, like this: filename = u"blah?.txt" open(filename) -- Jerry From james at uplinkzero.com Thu Jun 28 20:55:06 2012 From: james at uplinkzero.com (James Chapman) Date: Thu, 28 Jun 2012 19:55:06 +0100 Subject: [Tutor] Opening filenames with unicode characters Message-ID: <1340909706.4feca88aeb880@webmail.uplinkzero.com> Why can I not convert my existing byte string into a unicode string? In the mean time I'll create my original string as unicode and see if that solves my problem. >>> fileName = unicode(filename) Traceback (most recent call last): ? File "", line 1, in UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c in position 35: invalid start byte -- James At Thursday, 28/06/2012 on 18:58 Jerry Hill wrote: On Thu, Jun 28, 2012 at 1:55 PM, James Chapman wrote: > Thanks Tim, while this works, I need the name to be stored in a variable as > it's dynamic. > > In other words, how do I rewrite > open(u"blah?.txt") > > to be > filename = "blah?.txt" > open(filename) You have to create a unicode-string, not a byte-string.??So, like this: filename = u"blah?.txt" open(filename) -- Jerry -------------- next part -------------- An HTML attachment was scrubbed... URL: From malaclypse2 at gmail.com Thu Jun 28 21:06:03 2012 From: malaclypse2 at gmail.com (Jerry Hill) Date: Thu, 28 Jun 2012 15:06:03 -0400 Subject: [Tutor] Opening filenames with unicode characters In-Reply-To: <1340909706.4feca88aeb880@webmail.uplinkzero.com> References: <1340909706.4feca88aeb880@webmail.uplinkzero.com> Message-ID: On Thu, Jun 28, 2012 at 2:55 PM, James Chapman wrote: > Why can I not convert my existing byte string into a unicode string? That would work fine. > In the mean time I'll create my original string as unicode and see if that > solves my problem. > >>>> fileName = unicode(filename) > > Traceback (most recent call last): > File "", line 1, in > UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c in position 35: > invalid start byte Here's a couple of questions that you'll need to answer 'Yes' to before you're going to get this to work reliably: Are you familiar with the differences between byte strings and unicode strings? Do you understand how to convert from one to the other, using a particular encoding? Do you know what encoding your source file is saved in? If your string is not coming from a source file, but some other source of bytes, do you know what encoding those bytes are using? Try the following. Before trying to convert filename to unicode, do a "print repr(filename)". That will show you the byte string, along with the numeric codes for the non-ascii parts. Then convert those bytes to a unicode object using the appropriate encoding. If the bytes are utf-8, then you'd do something like this: unicode_filename = unicode(filename, 'utf-8') If your bytestring is actually shift-jis encoded, you'd do this instead: unicode_filename = unicode(filename, 'shift-jis') If you don't know what encoding your byte string is in, you either have to give up, guess, or try a bunch of likely possibilities until something works. If you really, really have to guess and there's no way for you to know for sure what encoding a particular byte string is in, the third party chardet module may be able to help. -- Jerry From james at uplinkzero.com Thu Jun 28 21:48:19 2012 From: james at uplinkzero.com (James Chapman) Date: Thu, 28 Jun 2012 20:48:19 +0100 Subject: [Tutor] Opening filenames with unicode characters Message-ID: <1340912899.4fecb503d3bb2@webmail.uplinkzero.com> Informative thanks Jerry, however I'm not out of the woods yet. > Here's a couple of questions that you'll need to answer 'Yes' to > before you're going to get this to work reliably: > > Are you familiar with the differences between byte strings and unicode > strings? I think so, although I'm probably missing key bits of information. > Do you understand how to convert from one to the other, > using a particular encoding? No not really. This is something that's still very new to me. > Do you know what encoding your source > file is saved in? The name of the file I'm trying to open comes from a UTF-16 encoded text file, I'm then using regex to extract the string (filename) I need to open. However, all the examples I've been using here are just typed into the python console, meaning string source at this stage is largely irrelevant. > If your string is not coming from a source file, > but some other source of bytes, do you know what encoding those bytes > are using? > > Try the following. Before trying to convert filename to unicode, do a > "print repr(filename)". That will show you the byte string, along > with the numeric codes for the non-ascii parts. Then convert those > bytes to a unicode object using the appropriate encoding. If the > bytes are utf-8, then you'd do something like this: > unicode_filename = unicode(filename, 'utf-8') >>> print(repr(filename)) "This is_a-test'FILE to Ensure$ that\x9c stuff^ works.txt.js" >>> fileName = unicode(filename, 'utf-8') Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c in position 35: invalid start byte >>> fileName = unicode(filename, 'utf-16') >>> fileName u'\u6854\u7369\u6920\u5f73\u2d61\u6574\u7473\u4627\u4c49\u2045\u6f74\u4520\u736e\u7275\u2465\u7420\u6168\u9c74\u7320\u7574\u6666\u205e\u6f77\u6b72\u2e73\u7874\u2e74\u736a' So I now have a UTF-16 encoded string, but I still can't open it. >>> codecs.open(fileName, 'r', 'utf-16') Traceback (most recent call last): File "", line 1, in File "C:\Python27\lib\codecs.py", line 881, in open file = __builtin__.open(filename, mode, buffering) IOError: [Errno 2] No such file or directory: u'\u6854\u7369\u6920\u5f73\u2d61\u6574\u7473\u4627\u4c49\u2045\u6f74\u4520\u736e\u72 75\u2465\u7420\u6168\u9c74\u7320\u7574\u6666\u205e\u6f77\u6b72\u2e73\u7874\u2e74\u736a' I presume I need to perform some kind of decode operation on it to open the file but then am I not basically going back to my starting point? Apologies if I'm missing the obvious. -- James From mail at timgolden.me.uk Thu Jun 28 22:17:40 2012 From: mail at timgolden.me.uk (Tim Golden) Date: Thu, 28 Jun 2012 21:17:40 +0100 Subject: [Tutor] Opening filenames with unicode characters In-Reply-To: <1340912899.4fecb503d3bb2@webmail.uplinkzero.com> References: <1340912899.4fecb503d3bb2@webmail.uplinkzero.com> Message-ID: <4FECBBE4.2010704@timgolden.me.uk> On 28/06/2012 20:48, James Chapman wrote: > The name of the file I'm trying to open comes from a UTF-16 encoded > text file, I'm then using regex to extract the string (filename) I > need to open. OK. Let's focus on that. For the moment -- although it might well be very relevant -- I'm going to ignore the regex side of things. It's always trying to portray things like this because there's such confusion between what characters I write to represent the data and the data represented by those characters themselves! OK, let's adopt a convention whereby I represent the data as they kind of thing you'd see in a hex editor. This obviously isn't how it appear in a a text file but hopefully it'll be clear what's going on. I have a filename ?10.txt -- that is the characters: POUND SIGN DIGIT ONE DIGIT ZERO FULL STOP LATIN SMALL LETTER T LATIN SMALL LETTER X LATIN SMALL LETTER T I have -- prior to your getting there -- placed this in a text file which I guarantee is UTF16-encoded. For the purposes of illustration I shall do that in Python code here: with open ("filedata.dat", "wb") as f: f.write (u"?10.txt".encode ("utf16")) The file is named "filedata.dat" and looks like this (per our convention): ff fe a3 00 31 00 30 00 2e 00 74 00 78 00 74 00 I now want to read the contents of the that file as a filename and open the file in question. Here goes: # # Open the file and extract the data as a set of # bytes into a Python (byte) string. # with open("filedata.dat", "rb") as f: data = f.read() # # Convert the data into a unicode object by decoding # the UTF16 bytes # filename = data.decode("utf16") # filename is now a unicode object which, depending on # what your console offers, will either display as # ?10.txt or as \xa310.txt or as something else. # # Open that file by passing the unicode object directly # to Python's file-opening mechanism # ten_pound_txt = open (filename, "rb") print ten_pound_txt.read () # whatever ten_pound_txt.close () I don't know if that makes anything clearer for you, but at least it gives you something to try out. The business with the regex clouds the issue: regex can play a little awkwardly with Unicode, so you'd have to show some code if you need help there. TJG From malaclypse2 at gmail.com Thu Jun 28 22:26:34 2012 From: malaclypse2 at gmail.com (Jerry Hill) Date: Thu, 28 Jun 2012 16:26:34 -0400 Subject: [Tutor] Opening filenames with unicode characters In-Reply-To: <1340912899.4fecb503d3bb2@webmail.uplinkzero.com> References: <1340912899.4fecb503d3bb2@webmail.uplinkzero.com> Message-ID: On Thu, Jun 28, 2012 at 3:48 PM, James Chapman wrote: > Informative thanks Jerry, however I'm not out of the woods yet. > > >> Here's a couple of questions that you'll need to answer 'Yes' to >> before you're going to get this to work reliably: >> >> Are you familiar with the differences between byte strings and unicode >> strings? > > I think so, although I'm probably missing key bits of information. Okay. I would start with this article as a refresher: http://www.joelonsoftware.com/articles/Unicode.html This isn't a bad reference either: http://docs.python.org/howto/unicode.html >> Do you understand how to convert from one to the other, >> using a particular encoding? > > No not really. This is something that's still very new to me. Okay. The lowest level way is to work with the strings themselves. Byte strings (the python2.x str type) have a .decode() method that will transform a byte string in a particular encoding into the equivalent unicode string. Unicode strings have a .encode() method to convert a unicode string into the equivalent byte string in a particular encoding. So, for example: byte_string = "This is a pound sign: \xA3" My terminal uses cp1252 as the encoding. I know that because I checked sys.stdin.encoding as I was working up this example. In CP1252, the pound character is 0xA3. I looked that up on a chart on wikipedia. So, if I print the string, and the encoding of the byte string matches the encoding on my terminal, everything should look fine: >>> print byte_string This is a pound sign: ? That works, because the encoding my terminal knows to display is the same as the bytes python put out. If I want to convert that string to unicode, I could do this: unicode_string = byte_string.decode('cp1252') now unicode_string has the unicode version of my original string. I can now encode that to any encoding I want. For instance: >>> print repr(unicode_string.encode('utf8')) 'This is a pound sign: \xc2\xa3' >>> print repr(unicode_string.encode('utf-16')) '\xff\xfeT\x00h\x00i\x00s\x00 \x00i\x00s\x00 \x00a\x00 \x00p\x00o\x00u\x00n\x00d\x00 \x00s\x00i\x00g\x00n\x00:\x00 \x00\xa3\x00' >>> print repr(unicode_string.encode('shift-jis')) 'This is a pound sign: \x81\x92' >> Do you know what encoding your source >> file is saved in? > > The name of the file I'm trying to open comes from a UTF-16 encoded text file, I'm then using regex to extract the string (filename) I need to open. However, all the examples I've been using here are just typed into the python console, meaning string source at this stage is largely irrelevant. > >> If your string is not coming from a source file, >> but some other source of bytes, do you know what encoding those bytes >> are using? >> >> Try the following. Before trying to convert filename to unicode, do a >> "print repr(filename)". That will show you the byte string, along >> with the numeric codes for the non-ascii parts. Then convert those >> bytes to a unicode object using the appropriate encoding. If the >> bytes are utf-8, then you'd do something like this: >> unicode_filename = unicode(filename, 'utf-8') > >>>> print(repr(filename)) > "This is_a-test'FILE to Ensure$ that\x9c stuff^ works.txt.js" > >>>> fileName = unicode(filename, 'utf-8') > Traceback (most recent call last): > File "", line 1, in > UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c in position 35: invalid start byte > >>>> fileName = unicode(filename, 'utf-16') > >>>> fileName > u'\u6854\u7369\u6920\u5f73\u2d61\u6574\u7473\u4627\u4c49\u2045\u6f74\u4520\u736e\u7275\u2465\u7420\u6168\u9c74\u7320\u7574\u6666\u205e\u6f77\u6b72\u2e73\u7874\u2e74\u736a' > > > > So I now have a UTF-16 encoded string, but I still can't open it. No. You have mojibake Try printing that unicode string, and you'll see that you have a huge mess of asian characters. You need to figure out what encoding that byte string is really in. If you're typing it on the terminal, do this: import sys print sys.stdin.encoding If the pound sign is being encoded as 0x9c (which it is, based on the example you showed), then your terminal is using an odd encoding. At a guess, cp850, maybe? > >>>> codecs.open(fileName, 'r', 'utf-16') > Traceback (most recent call last): > File "", line 1, in > File "C:\Python27\lib\codecs.py", line 881, in open > file = __builtin__.open(filename, mode, buffering) > IOError: [Errno 2] No such file or directory: u'\u6854\u7369\u6920\u5f73\u2d61\u6574\u7473\u4627\u4c49\u2045\u6f74\u4520\u736e\u72 > 75\u2465\u7420\u6168\u9c74\u7320\u7574\u6666\u205e\u6f77\u6b72\u2e73\u7874\u2e74\u736a' > > > I presume I need to perform some kind of decode operation on it to open the file but then am I not basically going back to my starting point? Once you have the name properly decoded from a byte string to a unicode string, just supply the unicode string to the open() function. Everything should be okay then. -- Jerry From ramit.prasad at jpmorgan.com Thu Jun 28 22:33:25 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Thu, 28 Jun 2012 20:33:25 +0000 Subject: [Tutor] Opening filenames with unicode characters In-Reply-To: <1340912899.4fecb503d3bb2@webmail.uplinkzero.com> References: <1340912899.4fecb503d3bb2@webmail.uplinkzero.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741652F9E7@SCACMX008.exchad.jpmchase.net> > The name of the file I'm trying to open comes from a UTF-16 encoded text file, > I'm then using regex to extract the string (filename) I need to open. However, > all the examples I've been using here are just typed into the python console, > meaning string source at this stage is largely irrelevant. > > >>> print(repr(filename)) > "This is_a-test'FILE to Ensure$ that\x9c stuff^ works.txt.js" > > >>> fileName = unicode(filename, 'utf-8') > Traceback (most recent call last): > File "", line 1, in > UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c in position 35: > invalid start byte > > >>> fileName = unicode(filename, 'utf-16') > > >>> fileName > u'\u6854\u7369\u6920\u5f73\u2d61\u6574\u7473\u4627\u4c49\u2045\u6f74\u4520\u73 > 6e\u7275\u2465\u7420\u6168\u9c74\u7320\u7574\u6666\u205e\u6f77\u6b72\u2e73\u78 > 74\u2e74\u736a' > So I now have a UTF-16 encoded string, but I still can't open it. > > >>> codecs.open(fileName, 'r', 'utf-16') > Traceback (most recent call last): > File "", line 1, in > File "C:\Python27\lib\codecs.py", line 881, in open > file = __builtin__.open(filename, mode, buffering) > IOError: [Errno 2] No such file or directory: > u'\u6854\u7369\u6920\u5f73\u2d61\u6574\u7473\u4627\u4c49\u2045\u6f74\u4520\u73 > 6e\u72 > 75\u2465\u7420\u6168\u9c74\u7320\u7574\u6666\u205e\u6f77\u6b72\u2e73\u7874\u2e > 74\u736a' > What happens if you use the filename as given above without converting? ("This is_a-test'FILE to Ensure$ that\x9c stuff^ works.txt.js" ) That works for me. Then just use codecs.open(filename). If you also use codecs.open() for your UTF-16 source file then I think you would not need to worry about any conversion. Oddly, >>>"This is_a-test'FILE to Ensure$ that\x9c stuff^ works.txt.js".decode('utf16').encode('utf16') "\xff\xfeThis is_a-test'FILE to Ensure$ that\x9c stuff^ works.txt.js" Not sure why that happens, but I assume it some kind of boundary issue. Maybe you can just strip off the two first characters? >>print '\xff\xfe' ?? Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 james at uplinkzero.com Thu Jun 28 23:47:23 2012 From: james at uplinkzero.com (James Chapman) Date: Thu, 28 Jun 2012 22:47:23 +0100 Subject: [Tutor] Opening filenames with unicode characters Message-ID: <1340920043.4fecd0eb3f657@webmail.uplinkzero.com> Thanks to everyone who responded on this thread, your time is greatly appreciated. It appears however that my problem is related to the environment. I sent my original email right before leaving work and have since been working on a physical machine without any problems. I've copied some of that code to my remote virtual machine where I'm doing the dev work and the same example that works on my physical win7 machine fails on my virtual win 2008 machine. Win2008 host platform is Linux with VirtualBox. The only remaining question is whether this is a one off issue, whether it's related to the virtual machine or whether it's related to Windows 2008. I guess I'll find out tomorrow. Oh and Tim, you'll be happy to know that regex does not affect the string in this case. Well, at least not the way I'm using it to extract data. -- James At Thursday, 28/06/2012 on 21:17 Tim Golden wrote: On 28/06/2012 20:48, James Chapman wrote: > The name of the file I'm trying to open comes from a UTF-16 encoded > text file, I'm then using regex to extract the string (filename) I > need to open. OK. Let's focus on that. For the moment -- although it might well be very relevant -- I'm going to ignore the regex side of things. It's always trying to portray things like this because there's such confusion between what characters I write to represent the data and the data represented by those characters themselves! OK, let's adopt a convention whereby I represent the data as they kind of thing you'd see in a hex editor. This obviously isn't how it appear in a a text file but hopefully it'll be clear what's going on. I have a filename ?10.txt -- that is the characters: POUND SIGN DIGIT ONE DIGIT ZERO FULL STOP LATIN SMALL LETTER T LATIN SMALL LETTER X LATIN SMALL LETTER T I have -- prior to your getting there -- placed this in a text file which I guarantee is UTF16-encoded. For the purposes of illustration I shall do that in Python code here: with open ("filedata.dat", "wb") as f: ?? f.write (u"?10.txt".encode ("utf16")) The file is named "filedata.dat" and looks like this (per our convention): ff fe a3 00 31 00 30 00 2e 00 74 00 78 00 74 00 I now want to read the contents of the that file as a filename and open the file in question. Here goes: # # Open the file and extract the data as a set of # bytes into a Python (byte) string. # with open("filedata.dat", "rb") as f: ?? data = f.read() # # Convert the data into a unicode object by decoding # the UTF16 bytes # filename = data.decode("utf16") # filename is now a unicode object which, depending on # what your console offers, will either display as # ?10.txt or as \xa310.txt or as something else. # # Open that file by passing the unicode object directly # to Python's file-opening mechanism # ten_pound_txt = open (filename, "rb") print ten_pound_txt.read () # whatever ten_pound_txt.close () I don't know if that makes anything clearer for you, but at least it gives you something to try out. The business with the regex clouds the issue: regex can play a little awkwardly with Unicode, so you'd have to show some code if you need help there. TJG _______________________________________________ Tutor maillist??-??Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From fomcl at yahoo.com Fri Jun 29 14:48:13 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 29 Jun 2012 05:48:13 -0700 (PDT) Subject: [Tutor] .get dicionary question (mutability??) Message-ID: <1340974093.16846.YahooMailNeo@web110710.mail.gq1.yahoo.com> Hello, ? I was wondering why, in the code below, dictionary 'a' can be populated while dictionary 'b' can't. b.get(k, []) will not return the default value [], but?'None' if k is not present in 'b'. Why? Of course, one could use try-except to do this, but I'd just like to understand why the .get() version does not work. ? Thanks! ? import random ? a = {} for i in range(100): ? k = random.randint(1, 10) ? a[k] = a.get(k, 0) + 1 print a ? b = {} process = lambda k: k**2 for i in range(100): ? k = random.randint(1, 10) ? v = process(k) ? b[k] = b.get(k, []).append(v) # <--- error! print b Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Fri Jun 29 15:10:30 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 29 Jun 2012 23:10:30 +1000 Subject: [Tutor] .get dicionary question (mutability??) In-Reply-To: <1340974093.16846.YahooMailNeo@web110710.mail.gq1.yahoo.com> References: <1340974093.16846.YahooMailNeo@web110710.mail.gq1.yahoo.com> Message-ID: <4FEDA946.4050200@pearwood.info> Albert-Jan Roskam wrote: > b.get(k, []) will not return the default value [], but 'None' if k is not present in 'b'. Why? Incorrect. b.get(k, []) returns the default value [] as expected. You then call the append method on that list, which returns None, and then you assign that result (None) to the dict item. > b = {} > process = lambda k: k**2 "process"? Surely "squared" would be a more descriptive and less misleading name. > for i in range(100): > k = random.randint(1, 10) > v = process(k) > b[k] = b.get(k, []).append(v) # <--- error! If b.get(k, []) did not return a list, the call to append would fail with AttributeError. Here are two ways to solve this correctly: for i in range(100): k = random.randint(1, 10) v = process(k) b.setdefault(k, []).append(v) for i in range(100): k = random.randint(1, 10) v = process(k) b[k] = b.get(k, []) + [v] The first way is more efficient and will probably be faster as the lists get longer. For small lists it is unlikely to make much difference. -- Steven From fomcl at yahoo.com Fri Jun 29 15:45:14 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 29 Jun 2012 06:45:14 -0700 (PDT) Subject: [Tutor] .get dicionary question (mutability??) In-Reply-To: <4FEDA946.4050200@pearwood.info> References: <1340974093.16846.YahooMailNeo@web110710.mail.gq1.yahoo.com> <4FEDA946.4050200@pearwood.info> Message-ID: <1340977514.63270.YahooMailNeo@web110704.mail.gq1.yahoo.com> Hi Steven, ? Thanks for helping me. It makes more sense now. Calling append() on the datatype list returns None but calling __add__ on the datatype int returns, well, the result. Somehow it makes more sense to me when writing + as __add__ a[k] = a.get(k, 0).__add__(1) b[k] = b.get(k, []]).__add__([v]) >>> x = 1 >>> y = [] >>> z = y.append(x) >>> z is None True ? ? Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? >________________________________ >From: Steven D'Aprano >To: Python Mailing List >Sent: Friday, June 29, 2012 3:10 PM >Subject: Re: [Tutor] .get dicionary question (mutability??) > >Albert-Jan Roskam wrote: >> b.get(k, []) will not return the default value [], but 'None' if k is not present in 'b'. Why? > >Incorrect. b.get(k, []) returns the default value [] as expected. > >You then call the append method on that list, which returns None, and then you assign that result (None) to the dict item. > > >> b = {} >> process = lambda k: k**2 > >"process"? Surely "squared" would be a more descriptive and less misleading name. > > >> for i in range(100): >>? k = random.randint(1, 10) >>? v = process(k) >>? b[k] = b.get(k, []).append(v) # <--- error! > >If b.get(k, []) did not return a list, the call to append would fail with AttributeError. > > >Here are two ways to solve this correctly: > > >for i in range(100): >? ? k = random.randint(1, 10) >? ? v = process(k) >? ? b.setdefault(k, []).append(v) > > >for i in range(100): >? ? k = random.randint(1, 10) >? ? v = process(k) >? ? b[k] = b.get(k, []) + [v] > > >The first way is more efficient and will probably be faster as the lists get longer. For small lists it is unlikely to make much difference. > > > >-- Steven > >_______________________________________________ >Tutor maillist? -? Tutor at python.org >To unsubscribe or change subscription options: >http://mail.python.org/mailman/listinfo/tutor > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Fri Jun 29 17:32:48 2012 From: emile at fenx.com (Emile van Sebille) Date: Fri, 29 Jun 2012 08:32:48 -0700 Subject: [Tutor] .get dicionary question (mutability??) In-Reply-To: <1340977514.63270.YahooMailNeo@web110704.mail.gq1.yahoo.com> References: <1340974093.16846.YahooMailNeo@web110710.mail.gq1.yahoo.com> <4FEDA946.4050200@pearwood.info> <1340977514.63270.YahooMailNeo@web110704.mail.gq1.yahoo.com> Message-ID: On 6/29/2012 6:45 AM Albert-Jan Roskam said... > Hi Steven, > Thanks for helping me. It makes more sense now. Calling append() on the > datatype list returns None but calling __add__ on the datatype int > returns, well, the result. I don't know that it's consistent across all types, but I've found that mutable types tend to change in place and return None while immutable types tend to return the altered result, which is as you describe below. Emile > Somehow it makes more sense to me when > writing + as __add__ > a[k] = a.get(k, 0).__add__(1) > b[k] = b.get(k, []]).__add__([v]) > >>> x = 1 > >>> y = [] > >>> z = y.append(x) > >>> z is None > True > Regards, > Albert-Jan > > From d at davea.name Fri Jun 29 18:09:58 2012 From: d at davea.name (Dave Angel) Date: Fri, 29 Jun 2012 12:09:58 -0400 Subject: [Tutor] .get dicionary question (mutability??) In-Reply-To: References: <1340974093.16846.YahooMailNeo@web110710.mail.gq1.yahoo.com> <4FEDA946.4050200@pearwood.info> <1340977514.63270.YahooMailNeo@web110704.mail.gq1.yahoo.com> Message-ID: <4FEDD356.6020904@davea.name> On 06/29/2012 11:32 AM, Emile van Sebille wrote: > On 6/29/2012 6:45 AM Albert-Jan Roskam said... >> Hi Steven, >> Thanks for helping me. It makes more sense now. Calling append() on the >> datatype list returns None but calling __add__ on the datatype int >> returns, well, the result. > > I don't know that it's consistent across all types, but I've found > that mutable types tend to change in place and return None while > immutable types tend to return the altered result, which is as you > describe below. > > Emile > > Close. methods/functions that modify their objects/arguments return None, while those that do not modify, will return the result. For example, take sort() and sorted(). The first method modifies its object and returns None. (It cannot be used on an immutable type) The sorted() function does not modify its argument, and returns the sorted item. (It can be used on mutable or immutable types) -- DaveA From rbv2 at CORNELL.EDU Fri Jun 29 18:33:01 2012 From: rbv2 at CORNELL.EDU (R Bruce van Dover) Date: Fri, 29 Jun 2012 12:33:01 -0400 Subject: [Tutor] libtiff--can't find library Message-ID: <4FEDD8BD.2090106@cornell.edu> Presumably this is a newbie question; apologies in advance, but I have spent hours trying to RTFM, to no avail. Can anyone help? I've installed pylibtiff-0.1-svn.win32.exe since I want to be able to read a TIFF file. But when I type (in IDLE) I get >>>from libtiffimport TIFFfile, TIFFimage Traceback (most recent call last): File "", line 1, in from libtiff import TIFFfile, TIFFimage File "E:\Python27\lib\site-packages\libtiff\__init__.py", line 4, in from .libtiff import libtiff, TIFF File "E:\Python27\lib\site-packages\libtiff\libtiff.py", line 35, in raise ImportError('Failed to find TIFF library. Make sure that libtiff is installed and its location is listed in PATH|LD_LIBRARY_PATH|..') ImportError: Failed to find TIFF library. Make sure that libtiff is installed and its location is listed in PATH|LD_LIBRARY_PATH|.. >>> import sys >>> print sys.path ['E:\\Python27\\Lib\\idlelib', 'E:\\Windows\\system32\\python27.zip', 'E:\\Python27\\DLLs', 'E:\\Python27\\lib', 'E:\\Python27\\lib\\plat-win', 'E:\\Python27\\lib\\lib-tk', 'E:\\Python27', 'E:\\Python27\\lib\\site-packages'] Libtiff is in the 'E:\\Python27\\lib\\site-packages' directory as it's supposed to. So is, e.g., Numpy, which imports just fine. What am I doing wrong? FWIW, I tried the PIL package, and had the same problem (module not found). Why do these modules not import when Numpy, matplotlib, scipy, etc. import as expected? Running Win7, 32bit, Python 2.7.1. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ramit.prasad at jpmorgan.com Fri Jun 29 19:42:46 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 29 Jun 2012 17:42:46 +0000 Subject: [Tutor] libtiff--can't find library In-Reply-To: <4FEDD8BD.2090106@cornell.edu> References: <4FEDD8BD.2090106@cornell.edu> Message-ID: <5B80DD153D7D744689F57F4FB69AF47416536242@SCACMX008.exchad.jpmchase.net> > Presumably this is a newbie question; apologies in advance, but I have spent > hours trying to RTFM, to no avail. Can anyone help? > > I've installed pylibtiff-0.1-svn.win32.exe since I want to be able to read a > TIFF file. But when I type (in IDLE) I get > >>>from libtiff import TIFFfile, TIFFimage > Traceback (most recent call last): > File "", line 1, in > from libtiff import TIFFfile, TIFFimage > File "E:\Python27\lib\site-packages\libtiff\__init__.py", line 4, in > > from .libtiff import libtiff, TIFF > File "E:\Python27\lib\site-packages\libtiff\libtiff.py", line 35, in > > raise ImportError('Failed to find TIFF library. Make sure that libtiff is > installed and its location is listed in PATH|LD_LIBRARY_PATH|..') > ImportError: Failed to find TIFF library. Make sure that libtiff is installed > and its location is listed in PATH|LD_LIBRARY_PATH|.. > >>> import sys > >>> print sys.path > ['E:\\Python27\\Lib\\idlelib', 'E:\\Windows\\system32\\python27.zip', > 'E:\\Python27\\DLLs', 'E:\\Python27\\lib', 'E:\\Python27\\lib\\plat-win', > 'E:\\Python27\\lib\\lib-tk', 'E:\\Python27', 'E:\\Python27\\lib\\site- > packages'] > Libtiff is in the 'E:\\Python27\\lib\\site-packages' directory as it's > supposed to. So is, e.g., Numpy, which imports just fine. > > What am I doing wrong? FWIW, I tried the PIL package, and had the same problem > (module not found). Why do these modules not import when Numpy, matplotlib, > scipy, etc. import as expected? Just a shot in the dark here... Maybe they were not installed properly? How did you install Python and the modules? Do you have more than one installed version of Python? Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- 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 29 20:54:25 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 29 Jun 2012 11:54:25 -0700 (PDT) Subject: [Tutor] libtiff--can't find library In-Reply-To: <4FEDD8BD.2090106@cornell.edu> References: <4FEDD8BD.2090106@cornell.edu> Message-ID: <1340996065.43755.YahooMailNeo@web110702.mail.gq1.yahoo.com> From: R Bruce van Dover To: tutor at python.org >Sent: Friday, June 29, 2012 6:33 PM >Subject: [Tutor] libtiff--can't find library > > >Presumably this is a newbie question; apologies in advance, but I have spent hours trying to RTFM, to no avail. Can anyone help? > >I've installed pylibtiff-0.1-svn.win32.exe since I want to be able to read a TIFF file. But when I type (in IDLE) I get > >>>>fromlibtiff importTIFFfile,TIFFimage Traceback (most recent call last): ?File "", line 1, in ?from libtiff import TIFFfile, TIFFimage File "E:\Python27\lib\site-packages\libtiff\__init__.py", line 4, in ? from .libtiff import libtiff, TIFF ? File "E:\Python27\lib\site-packages\libtiff\libtiff.py", line 35, in ??? raise ImportError('Failed to find TIFF library. Make sure that libtiff is installed and its location is listed in PATH|LD_LIBRARY_PATH|..') ImportError: Failed to find TIFF library. Make sure that libtiff is installed and its location is listed in PATH|LD_LIBRARY_PATH|.. >>> import sys >>> print sys.path ['E:\\Python27\\Lib\\idlelib', 'E:\\Windows\\system32\\python27.zip', 'E:\\Python27\\DLLs', 'E:\\Python27\\lib', 'E:\\Python27\\lib\\plat-win', 'E:\\Python27\\lib\\lib-tk', 'E:\\Python27', 'E:\\Python27\\lib\\site-packages'] Libtiff is in the 'E:\\Python27\\lib\\site-packages' directory as it's supposed to. So is, e.g., Numpy, which imports just fine. > >What am I doing wrong? FWIW, I tried the PIL package, and had the same problem (module not found). Why do these modules not import when Numpy, matplotlib, scipy, etc. import as expected? > >Running Win7, 32bit, Python 2.7.1. > >===> Sounds likthe Python wrapper to the tiff library was installed, but the tiff library itself wasn't, or your OS doesn't know where to look for it. Python uses ctypes to call C functions in dll files from tifflib, but it can't call those if the dlls aren't there or if %PATH% doesn't specify where they live. That's my theory about this ;-) Btw, I used WX Python to read tiff files before. Tiff is actually a family of files and it turned out the WX Python could read bitonic (B/W) multipage tiffs, but PIL couldn't. -------------- next part -------------- An HTML attachment was scrubbed... URL: From malaclypse2 at gmail.com Fri Jun 29 21:00:58 2012 From: malaclypse2 at gmail.com (Jerry Hill) Date: Fri, 29 Jun 2012 15:00:58 -0400 Subject: [Tutor] libtiff--can't find library In-Reply-To: <4FEDD8BD.2090106@cornell.edu> References: <4FEDD8BD.2090106@cornell.edu> Message-ID: On Fri, Jun 29, 2012 at 12:33 PM, R Bruce van Dover wrote: > Presumably this is a newbie question; apologies in advance, but I have spent > hours trying to RTFM, to no avail. Can anyone help? There's an issue on the pylibtiff Issues page that sounds related: http://code.google.com/p/pylibtiff/issues/detail?id=12 Have you tried the steps suggested there? -- Jerry From steve at pearwood.info Sat Jun 30 03:52:21 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 30 Jun 2012 11:52:21 +1000 Subject: [Tutor] libtiff--can't find library In-Reply-To: <4FEDD8BD.2090106@cornell.edu> References: <4FEDD8BD.2090106@cornell.edu> Message-ID: <4FEE5BD5.20105@pearwood.info> R Bruce van Dover wrote: > ImportError: Failed to find TIFF library. Make sure that libtiff is > installed and its location is listed in PATH|LD_LIBRARY_PATH|.. > Libtiff is in the 'E:\\Python27\\lib\\site-packages' directory as it's > supposed to. So is, e.g., Numpy, which imports just fine. > > What am I doing wrong? FWIW, I tried the PIL package, and had the same > problem (module not found). Why do these modules not import when Numpy, > matplotlib, scipy, etc. import as expected? A couple of suggestions: - the error message hints that the TIFF library may be listed in the environment variable LD_LIBRARY_PATH. Try setting that env var to 'E:\\Python27\\lib\\site-packages'. If tifflib is in a subdirectory, make sure you give the full subdirectory. - you might like to investigate the tiffany package, which claims to be able to work with TIFFs without dependencies on PIL. Perhaps it is easier to work with? http://pypi.python.org/pypi/tiffany -- Steven From dthomas86 at me.com Sat Jun 23 23:17:14 2012 From: dthomas86 at me.com (David Thomas) Date: Sat, 23 Jun 2012 22:17:14 +0100 Subject: [Tutor] Python Launcher and MacOSX 10.7 Message-ID: Hi, I am trying to get one of my scripts to be executed using Python Launcher 3.2.3. I have tried the code using a Virtual Machine running Windows 7 and the program is executed fine, but when I execute it on my Mac it will not work I keep getting an error in Terminal and sometimes Python Launcher crashes. I have attached screen shots below. The code is a simple operation to close the console window once the enter key has been pressed. If you could provide me with any further information, I would greatly appreciate it. Kind Regards, David Thomas -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Screen Shot 2012-06-23 at 19.41.53.png Type: image/png Size: 69010 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Screen Shot 2012-06-23 at 19.58.17.png Type: image/png Size: 34391 bytes Desc: not available URL: From japhy at pearachute.com Wed Jun 27 16:05:12 2012 From: japhy at pearachute.com (Japhy Bartlett) Date: Wed, 27 Jun 2012 09:05:12 -0500 Subject: [Tutor] Generating random alphanumeric codes In-Reply-To: <4FEA1FD9.7030105@davea.name> References: <4FEA1FD9.7030105@davea.name> Message-ID: I've always liked uuid4 >>> from uuid import uuid4 >>> str(uuid4())[:4] '0ca6' On Tue, Jun 26, 2012 at 3:47 PM, Dave Angel wrote: > On 06/26/2012 03:47 PM, Martin A. Brown wrote: > > Hello, > > > > : Would anyone have tips on how to generate random 4-digit > > : alphanumeric codes in python? Also, how does one calculate the > > : number of possible combinations? > > > > Here are some thoughts that come to my mind: > > > > import string > > import random > > > > # -- technique #1, using random.choice > > print ''.join([random.choice(string.lowercase) for x in range(4)]) > > > > # -- technique #2, using random.shuffle > > t = [ x for x in string.lowercase ] > > random.shuffle(t) > > ''.join(t[0:4]) > > > > # -- technique #3, using random.sample > > ''.join(random.sample(string.lowercase,4)) > > > > I would be quite surprised if there were not more efficient ways of > > accomplishing this. > > > > -Martin > > > Two problems that I see: > > string.lowercase includes the lowercase letters, but not the digits. > > Your methods 2 and 3 produce different results than method1. The OP > never specified which he wanted, but the distinction can well be important. > > Likewise his query about how many possibilities there are depends on the > same question: > Are duplicates permitted between the four characters in any given > answer. > > > > > -- > > DaveA > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From pniraula at mail.spc.edu Thu Jun 28 00:59:31 2012 From: pniraula at mail.spc.edu (Prajwal Niraula) Date: Wed, 27 Jun 2012 18:59:31 -0400 Subject: [Tutor] VPYTHON Message-ID: Hi, I am trying to learn VPython for more interesting simulation. I downloaded it from the website: www.vpython.org While I have Python 3.2.3 in my computer, and it seems no equivalent version for vpython is available. Besides when installing I had problem in which the installer would not recognise the location of python, even though it is located at C:\Python32. What should I be doing? -------------- next part -------------- An HTML attachment was scrubbed... URL: From D.Wilder at F5.com Thu Jun 28 14:54:22 2012 From: D.Wilder at F5.com (Dave Wilder) Date: Thu, 28 Jun 2012 12:54:22 +0000 Subject: [Tutor] Python and boot sequences Message-ID: Hello, This is a general Python question, so I don't require specific answers on how to do it, but... Can a Python script be written that has the ability to stop a Linux device in the middle of a boot when a certain sequence occurs and then perform an action? For example, when I boot my switch (w/ Linux OS 2.7.3), I want to stop the boot when I see a series of asterisks. When I see this, I need to hit the <9> sequence. Then, I need to select a menu option (e.g. 1 to run a test or Q to quit and continue w/ the boot). Another example would be when doing a PXE boot, selecting the image to load when prompted during the reboot. I don't know enough about Python yet to know if that is something that Python can be used for or I am barking up the wrong tree. Thank You, Dave -------------- next part -------------- An HTML attachment was scrubbed... URL: From link6746 at gmail.com Fri Jun 29 08:51:00 2012 From: link6746 at gmail.com (Joseph Hines) Date: Thu, 28 Jun 2012 23:51:00 -0700 Subject: [Tutor] Passing numeral result of a defined function to a named value Message-ID: Hello. Sorry to bother you, but I'm self-teaching myself python, and having difficulty with a script I'm writing to auto-calc various derived stats for a PnP game I am hosting soon. I need to pass a numeral result of a defined function to a "named" value (not sure if that's what it's called) The offending segment of code is this: XP_Per_Level_Base = 20-int_mod > XP_Per_Level_Limit = 5 > try: > def XP_Per_Level_Calc(XP_Per_Level_Base, XP_Per_Level_Limit): > if XP_Per_Level_Base < XP_Per_Level_Limit: > return XP_Per_Level_Limit > else: > return XP_Per_Level_Base > XP_Per_Level = XP_Per_Level_Calc(XP_Per_Level_Base, XP_Per_Level_Limit) > except ValueError: > print "Script B0RK3D, Link Shall Come to Town." > print "send him a message at Link6746 at gmail.com" #Calcs XP per Level# > If you need the whole script, I'd be glad to send it to you, just promise me not to divulge too many details of it (It's regarding a custom PnP system that I'm planning on releasing under the Open Gaming License, and I'd prefer not to risk it becoming a proprietary system for someone I don't know) -------------- next part -------------- An HTML attachment was scrubbed... URL: From Aristotle at triad.rr.com Tue Jun 26 20:28:25 2012 From: Aristotle at triad.rr.com (Aristotle) Date: Tue, 26 Jun 2012 14:28:25 -0400 Subject: [Tutor] Seeking help with reading and writing files in Python Message-ID: <4FE9FF49.203@triad.rr.com> Hello, I am reading Tony Gaddis "Teach Python" book and I am having trouble with an assignment that asks me to read from one file and then write to another file. The problem is that the file to read from has not been created yet. Can anyone help me understand this instruction a little better? I am averaging an A in the course and would like to keep it. Below you will find the correspondence between the instructor and myself. ( Written Last to First) Thanks, Jim Hello Ms. H, I am still having trouble with using two text files. I cannot find any examples in Cha.7 text book or Code Examples. Would you please point me in the direction where I may find an example for this requirement? Thank You, Jim Hello Ms. H, The sequence of events that I am using now is to open a program that will gather gallons used from user, call a module to make calculations, use the info from the module to write info to a file named FinalProjectBill.txt. After that is done, I open another program that will read the info written to FinalProjectBill.txt I guess my confusion about the instructions is, if I should read from one file FinalProjectBill.txt first how does data get written to that file to begin with? Thank you for the great help. Jim From Instructor: You should read from one file (FinalProjectBill.txt) and then write to one (BranleyFinal.txt) with the results as we are learning file i/o (input/output) - The screen is fine to display results also, but in the real world, often times all processing is done by reading and writing to files (actually databases that are beyond the scope of this class) and nothing ever displays on the screen. You should not write to your input file. Make sense? From: Me Sent: Tuesday, June 26, 2012 5:54 AM To: Instructor Subject: Two txt files Hello Ms. H, I started writing the program yesterday and by the evening I had a WaterPayModule, an InputProgram, an OutputProgram and a text file named FinalProjectBill.txt. When I run the OutputProgram it gets input from the user, sends info to the WaterModule and receives some values that I use to write output to the hard drive file FinalProjectBill.txt. When I run the InputProgram it reads input from the hard drive text file FinalProjectBill.txt and prints it on the screen. My question is why do I need two text files FinalProjectBill.txt and BranleyJamesFinal.txt ? Thank you, Jim From amit4465 at gmail.com Thu Jun 28 07:21:19 2012 From: amit4465 at gmail.com (Amit Prasad) Date: Thu, 28 Jun 2012 10:51:19 +0530 Subject: [Tutor] Script Message-ID: Hello Sir, I want to write a script to replace hostname from /etc/hosts and /etc/sysconfig/network files. And the new hostname will be a new entry everytime. I mean everytime when i run the script and give a new hostname, it should be replaced with the old one. I am running out of time, please help me out, as i am new to scripting. Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: ments/20120628/619fccf8/attachment.html>