From cecilia.chavana at gmail.com Mon Oct 1 00:07:08 2012 From: cecilia.chavana at gmail.com (Cecilia Chavana-Bryant) Date: Sun, 30 Sep 2012 23:07:08 +0100 Subject: [Tutor] Help for Python Beginner with extracting and manipulating data from thousands of ASCII files Message-ID: Hola again Python Tutor! With a friend's help I have the following code to extract reflectance data from an ASCII data file, do a bit of data manipulation to calibrate the data and then write the calibrated file into an out file. import numpy # import glob - use if list_of_files is used dataFile = "1SH0109.001.txt" #list_of_files = glob.glob('./*.txt') to replace dataFile to search for all text files in ASCII_files folder? caliFile1 = "Cal_File_P17.txt" # calibration file to be used on data files created from July to 18 Sep caliFile2 = "Cal_File_P19.txt" # calibration file to be used on data files created from 19 Sep onwards outFile = "Cal_" + dataFile # this will need to change if list_of_files is used fileDate = data[6][16:26] # location of the creation date on the data files #extract data from data file fdData = open(dataFile,"rt") data = fdData.readlines() fdData.close() #extract data from calibration file fdCal = open(caliFile,"rt") calibration = fdCal.readlines() fdCal.close() #create data table k=0 #just a counter dataNum = numpy.ndarray((2151,2)) #the actual data (the numbers) in the data file begin at line 30 for anItem in data[30:]: theNums = anItem.replace("\r\n","").split("\t") dataNum[k,0] = int(theNums[0]) dataNum[k,1] = float(theNums[1]) k+=1 #advance the counter #create the calibration table k = 0 calNum = numpy.ndarray((2151,2)) for anItem in calibration[5:]: theNums = anItem.replace("\r\n","").split("\t") calNum[k,0] = int(theNums[0]) calNum[k,1] = float(theNums[1]) k+=1 #calibrate the data k=0 calibratedData = numpy.ndarray((2151,2)) for aNum in dataNum: calibratedData[k,0] = aNum[0] #first column is the wavelength calibratedData[k,1] = (aNum[1] * dataNum[k,1]) * 100.0 #second column is the measurement to be calibrated. k+=1 #write the calibrated data fd = open(outFile,"wt") #prior to writing the calibrated contents, write the headers for data files and calibration files fd.writelines(data[0:30]) fd.writelines(calibration[0:5]) for aNum in calibratedData: #Write the data in the file in the following format: # "An integer with 3 digits", "tab character", "Floating point number" fd.write("%03d\t%f\n" % (aNum[0],aNum[1])) #close the file fd.close() I have successfully calibrated one ASCII file at a time with this code. However, I have 1,000s of files that I need to calibrate so I would like some help to modify this code so it can: 1. Use one calibration file (Cal_FileP17.txt) on data files created from July to the 18th Sep and a different calibration file (Cal_FileP19.txt) for data files created from the 19th of Sep onwards. 2. Find all the .txt files in a folder called ASCII_files, which is subdivided into 12 different folders and calibrate all these files I have googled and tried thinking about how to make changes and I've managed to get myself a bit more confused. Thus, I would like some guidance on how to tackle/think about this process and how to get started. Please, I am not asking for someone to do my work and write the code for me, I would like some guidance on how to approach this and get started. Many thanks in advance for your help, Cecilia -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Oct 1 01:24:24 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 01 Oct 2012 00:24:24 +0100 Subject: [Tutor] Lotka-Volterra Model Simulation Questions In-Reply-To: References: <5066D082.7050301@pearwood.info> Message-ID: On 30/09/12 11:50, Oscar Benjamin wrote: >>> ...I'm sure that the equations you're refering to would have >>> already been using lots of symbols >> Yes which made them even more difficult to understand. > > Quantum mechanics is hard for anyone. I don't think that an alternative > notation will make it any easier for people in the business of > learning/teaching/using quantum mechanics Possibly (although see below), but I'm not arguing for changing the notation where a standard exists but where the standard is not known. In the original post I suggested renaming the variables precisely because I did not know the standard equation and would understand the algorithm better if the variables had meaningful names. Someone who knew the equation had no need of such because they did the translation mentally using prior knowledge. > think that it is possible to find a notation that will make quantum > mechanics intelligible to a layperson: It doesn't need to be intelligible to a lay person but QM is made up of different communities: researchers, academics, practitioners, consumers etc. I am a consumer of QM and used it in my work even though I could not have derived the equations from scratch. But I understood them once they were explained to me. But to really use them I had to go and rewrite them as pseudo code functions (momentum_of_electon_shell(), energy_of_photon() etc) Now, a pure physicist or mathematician would, I'm sure, have found many faults in my translation but I had to do that to understand the concepts for my own benefit. But the mathematician/physicist in writing those same functions would probably have used the technical terms and the code would have looked a lot like the equations... but for me I had to translate the math 'code' into quantities I understood - and yes that included 'h' for Plank's constant because that one I already knew, so I freely admit it is all relative! > > Now imagine replacing each of those single letter symbols > > with English underscore-separated words so instead of letter > > capital psi you would have 'time_dependent_wave_function' > > and instead of hbar you would have 'planks_constant_over_twopi' > > and so on. Your equation would go from three lines to thirty But this is where it gets interesting. One of the first programming jobs I had after university was working in a team who industrialised the code coming out of our research labs. Most of that work was taking domain specific code written in Fortran, Basic or Forth and translating it into C or Pascal (this was in the 80's...). We never used the pure math forms given us by the researchers but translated it into much more verbose "laymans" code (with the help of the researcher) because we knew that we would have to maintain it not the eggheads. We couldn't become domain experts in all of the fields we serviced so we had to make the research code readable and maintainable by general purpose programmers. > While I can write a script like the OP's in less than 5 minutes, in > practise it takes longer to convince myself that the code is correct (if > it is important for it to be so). I spend most of the time when > developing such a script simply looking at the code and comparing it > with the mathematical problem I was trying to solve. Which is great if you understand the problem domain and the math involved. If you don't you have to rely on getting the algorithm from the experts and then translating it into something you can work with after the expert has moved on. > code diverges from the original problem statement the harder it becomes > to really convince yourself that the code is correct. Renaming all of > your variables (any more than you need to) so that cross-referencing > always requires a mental table would be a waste of time and would > increase the likelihood of bugs. But for the non domain expert the mental table occurs when the technical term is used. technical jargon in math as elsewhere is still jargon and as such only understandable to the select few with the inside knowledge. So while code is restricted to that closed community technical jargon is fine. If it must be shared with a non technical audience then decisions need to be taken, either to provide the mapping in documentation/comments or to provide non technical names. > In any case I guess you won't be pleased by my discovery that, thanks to > PEP 3131, the following is valid code in Python 3 (I've attached the > code in case it doesn't display properly): Not at all, I'd rather see real Greek characters being used than Anglicised spellings (especially many variants of same!). As I say, where a generally understood symbology exists it makes sense to use it. (A good example is in the use of complex numbers. These are not generally understood outside the math/science fraternity but they are nigh essential within that community. To try to reinvent the symbology there would be foolish) It is only where the code must spill out to non cognisant users that care needs to be taken. > # Lotka-Volterra derivative > def f(Z, t): Although I would argue that 'f' is still a rotten name for any function! def lotka_volterra(Z,t): might be better. :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Mon Oct 1 01:33:44 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 01 Oct 2012 00:33:44 +0100 Subject: [Tutor] Help for Python Beginner with extracting and manipulating data from thousands of ASCII files In-Reply-To: References: Message-ID: On 30/09/12 23:07, Cecilia Chavana-Bryant wrote: > Hola again Python Tutor! > > With a friend's help I have the following code to extract reflectance > data from an ASCII data file, do a bit of data manipulation to calibrate > the data and then write the calibrated file into an out file. > I have successfully calibrated one ASCII file at a time with this code. > However, I have 1,000s of files that I need to calibrate so I would like > some help to modify this code so it can: > > 1. Use one calibration file (Cal_FileP17.txt) on data files created from > July to the 18th Sep and a different calibration file (Cal_FileP19.txt) > for data files created from the 19th of Sep onwards. > > 2. Find all the .txt files in a folder called ASCII_files, which is > subdivided into 12 different folders and calibrate all these files Number 2 is easier to solve and the os.walk() and glob.glob() functions should provide all the tools you need. Number 1 is more tricky since there is no obvious way to determine the arbitrary start/stop dates you specify. So I'd suggest you need to generalise the requirement to take a start/stop date as well as the calibration file name and the input data file pattern. Use those as input parameters to a function that generates the list of files to process and then calls your existing code (wrapped in a new function) and possibly provide default values for all/some of the parameters. Another option is to add the start/end dates to the calibration file if you have control of that, but personally I'd stick with input parameters... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From d at davea.name Mon Oct 1 02:16:17 2012 From: d at davea.name (Dave Angel) Date: Sun, 30 Sep 2012 20:16:17 -0400 Subject: [Tutor] Help for Python Beginner with extracting and manipulating data from thousands of ASCII files In-Reply-To: References: Message-ID: <5068E0D1.4050001@davea.name> On 09/30/2012 06:07 PM, Cecilia Chavana-Bryant wrote: > Hola again Python Tutor! > > With a friend's help I have the following code to extract reflectance data > from an ASCII data file, do a bit of data manipulation to calibrate the > data and then write the calibrated file into an out file. > > import numpy > # import glob - use if list_of_files is used > > > dataFile = "1SH0109.001.txt" > #list_of_files = glob.glob('./*.txt') to replace dataFile to search for all > text files in ASCII_files folder? First, an important observation. This code has no functions defined in it. Thus it's not reusable. So every time you make a change, you'll be breaking the existing code and then trying to make the new version work. The decision of one file versus many is usually handled by writing a function that deals with one file. Test it with a single file. Then write another function that uses glob to build a list of files, and call the original one in a loop. As you work on it, you should discover that there are a half dozen other functions that you need, rather than one big one. > caliFile1 = "Cal_File_P17.txt" # calibration file to be used on data files > created from July to 18 Sep > caliFile2 = "Cal_File_P19.txt" # calibration file to be used on data files > created from 19 Sep onwards > outFile = "Cal_" + dataFile # this will need to change if list_of_files is > used > fileDate = data[6][16:26] # location of the creation date on the data files Show us the full traceback from the runtime error you get on this line. > > #extract data from data file > fdData = open(dataFile,"rt") > data = fdData.readlines() > fdData.close() > > #extract data from calibration file > fdCal = open(caliFile,"rt") Show us the full traceback from the runtime error here, as well. > calibration = fdCal.readlines() > fdCal.close() > > #create data table > k=0 #just a counter > dataNum = numpy.ndarray((2151,2)) > > #the actual data (the numbers) in the data file begin at line 30 > for anItem in data[30:]: > theNums = anItem.replace("\r\n","").split("\t") > dataNum[k,0] = int(theNums[0]) > dataNum[k,1] = float(theNums[1]) > k+=1 #advance the counter > > #create the calibration table > k = 0 > calNum = numpy.ndarray((2151,2)) > for anItem in calibration[5:]: > theNums = anItem.replace("\r\n","").split("\t") > calNum[k,0] = int(theNums[0]) > calNum[k,1] = float(theNums[1]) > k+=1 > > #calibrate the data > k=0 > calibratedData = numpy.ndarray((2151,2)) > for aNum in dataNum: > calibratedData[k,0] = aNum[0] #first column is the wavelength > calibratedData[k,1] = (aNum[1] * dataNum[k,1]) * 100.0 #second column > is the measurement to be calibrated. > k+=1 > > #write the calibrated data > fd = open(outFile,"wt") Error traceback ? > #prior to writing the calibrated contents, write the headers for data files > and calibration files > fd.writelines(data[0:30]) > fd.writelines(calibration[0:5]) > for aNum in calibratedData: > #Write the data in the file in the following format: > # "An integer with 3 digits", "tab character", "Floating point number" > fd.write("%03d\t%f\n" % (aNum[0],aNum[1])) > > #close the file > fd.close() > Are the individual files small? By doing readlines() on them, you're assuming you can hold all of both the data file and the calibration file in memory. > I have successfully calibrated one ASCII file at a time with this code. Unless I'm missing something, this code does not run. I didn't try it, though, just inspected it quickly. > However, I have 1,000s of files that I need to calibrate so I would like > some help to modify this code so it can: > > 1. Use one calibration file (Cal_FileP17.txt) on data files created from > July to the 18th Sep and a different calibration file (Cal_FileP19.txt) for > data files created from the 19th of Sep onwards. > > 2. Find all the .txt files in a folder called ASCII_files, which is > subdivided into 12 different folders and calibrate all these files > > I have googled and tried thinking about how to make changes and I've > managed to get myself a bit more confused. Thus, I would like some guidance > on how to tackle/think about this process and how to get started. Please, I > am not asking for someone to do my work and write the code for me, I would > like some guidance on how to approach this and get started. > > Many thanks in advance for your help, > Cecilia > > -- DaveA From dwightdhutto at gmail.com Mon Oct 1 03:16:43 2012 From: dwightdhutto at gmail.com (Dwight Hutto) Date: Sun, 30 Sep 2012 21:16:43 -0400 Subject: [Tutor] OT: Netiquette In-Reply-To: References: <505F5908.1010304@davea.name> <5B80DD153D7D744689F57F4FB69AF474166C5626@SCACMX008.exchad.jpmchase.net> Message-ID: On Fri, Sep 28, 2012 at 6:38 AM, Bod Soutar wrote: > > On Sep 28, 2012 4:47 AM, "Dwight Hutto" wrote: >> >> On Wed, Sep 26, 2012 at 6:59 AM, Walter Prins wrote: >> > Dwight, >> > >> > On 26 September 2012 09:26, Dwight Hutto wrote: >> >> The only face I personally want to see of him >> >>> because of this is his back. >> >>> >> >> >> >> You wanna see my ass, because that's what you want homo. Butt just >> >> look, you can't touch. >> > >> > The personal attacks and innuendo are really not acceptable and you're >> > apparently deliberately twisting/misinterpreting Mark's words there. >> >> Oooh, a PR attack in another post. >> >> >> > Waaaay out of line and quite disingenuous. How is " Oooh, a PR attack in another post." in any way out of line, or disingenuous? Would you respond so >> > aggressively to people in person? Aggressiveness in response, is in direct relation to the emotions of the subjects, and even then(in today's American political environment...moot. No? Well why do you think it's OK >> > to be abusive on the internet? The abuse, was in response to an initial thread you weren't following, in which I was attacked first. (If you do think it's OK to be this >> > abusive to people in person, then you're sadly mistaken.) No, not abusive, put I've known few who in public were polite to me. Grow up. I don't grow, I throw up and you come around the corner, and lick it up with ketchup. >> > Walk away. Learn to be polite to people you don't know. Tell that to the poster not associated with this message, probably in a another thread. This is not >> > the school playground and you're not 5 years old. But he started it. >> Do some careful >> > introspection. >> >> Yeah, all up in my fucking cranium with nothing but me and God to hold >> on to one another. -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com From dwightdhutto at gmail.com Mon Oct 1 03:19:14 2012 From: dwightdhutto at gmail.com (Dwight Hutto) Date: Sun, 30 Sep 2012 21:19:14 -0400 Subject: [Tutor] OT: Netiquette In-Reply-To: References: <505F5908.1010304@davea.name> <5B80DD153D7D744689F57F4FB69AF474166C5626@SCACMX008.exchad.jpmchase.net> Message-ID: On Fri, Sep 28, 2012 at 9:07 AM, Wayne Werner wrote: > On Fri, 28 Sep 2012, Bod Soutar wrote: > >> On Sep 28, 2012 4:47 AM, "Dwight Hutto" wrote: >> > Yeah, all up in my fucking cranium with nothing but me and God to hold >> > on to one another. >> > >> > -- >> > Best Regards, >> > David Hutto >> > CEO: http://www.hitwebdevelopment.com > > That's OK, if you don't like that sort of attitude you're obviously not in > his target market (the one that enjoys was it 60MB GIF files, and background > music). Was that mean as sarcasm? Because it's lame as fuck. Read other posts and you'll see it's being reduced down several MB's at a time in order no to take away from the in place animation. -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com From oscar.j.benjamin at gmail.com Mon Oct 1 10:52:52 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 1 Oct 2012 09:52:52 +0100 Subject: [Tutor] Lotka-Volterra Model Simulation Questions In-Reply-To: References: <5066D082.7050301@pearwood.info> Message-ID: On Oct 1, 2012 12:26 AM, "Alan Gauld" wrote: > > On 30/09/12 11:50, Oscar Benjamin wrote: >> While I can write a script like the OP's in less than 5 minutes, in >> practise it takes longer to convince myself that the code is correct (if >> it is important for it to be so). I spend most of the time when >> developing such a script simply looking at the code and comparing it >> with the mathematical problem I was trying to solve. > > > Which is great if you understand the problem domain and the math involved. If you don't you have to rely on getting the algorithm from the experts and then translating it into something you can work with after the expert has moved on. I guess we won't get to find out but I assumed that the OP understood what he was doing mathematically but was struggling with the Python code: his code is correct in it's description of the mathematical model but the integration algorithm had not been implemented. If I was correct about that then it would have been bad advice to change the variable names. Also (even if I was correct) it's still very likely that my own post went over his head because of the level of assumed Python experience. >> # Lotka-Volterra derivative >> def f(Z, t): > > > Although I would argue that 'f' is still a rotten name > for any function! > > def lotka_volterra(Z,t): > > would be better. :-) I'm not sure I would really use f in a real problem but I should say that it is the mathematical convention to call this particular function f. I'll meet you (sort of)half way: def f_lotka_volterra(x, t): Oscar -------------- next part -------------- An HTML attachment was scrubbed... URL: From oscar.j.benjamin at gmail.com Mon Oct 1 12:02:16 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 1 Oct 2012 11:02:16 +0100 Subject: [Tutor] Help for Python Beginner with extracting and manipulating data from thousands of ASCII files In-Reply-To: References: Message-ID: On Sep 30, 2012 11:10 PM, "Cecilia Chavana-Bryant" wrote: > > Hola again Python Tutor! Hi Cecilia > > With a friend's help I have the following code to extract reflectance data from an ASCII data file, do a bit of data manipulation to calibrate the data and then write the calibrated file into an out file. > > import numpy > # import glob - use if list_of_files is used > > > dataFile = "1SH0109.001.txt" > #list_of_files = glob.glob('./*.txt') to replace dataFile to search for all text files in ASCII_files folder? > caliFile1 = "Cal_File_P17.txt" # calibration file to be used on data files created from July to 18 Sep > caliFile2 = "Cal_File_P19.txt" # calibration file to be used on data files created from 19 Sep onwards > outFile = "Cal_" + dataFile # this will need to change if list_of_files is used > fileDate = data[6][16:26] # location of the creation date on the data files The variable data used in the line above is not created until the lines below run. I think you need to move this line down. What format does fileDate have? I guess it's a string of text from the file. If you can convert it to a datetime (or date) object it will be easy to compare with the dates as required for your calibration file. Can you show us how it looks e.g. '12-Nov-2012' or '12/11/12' or something else? > > #extract data from data file > fdData = open(dataFile,"rt") > data = fdData.readlines() > fdData.close() Python has a slightly better way of writing code like this: with open(dataFile, 'rt') as fdata: data = fdata.readlines() This way you don't need to remember to close the file. In fact Python will even remember to close it if there is an error. > > #extract data from calibration file > fdCal = open(caliFile,"rt") > calibration = fdCal.readlines() > fdCal.close() Where is caliFile set? If your going to load all the data files you might as well load both calibration files here at the beginning. > > #create data table > k=0 #just a counter > dataNum = numpy.ndarray((2151,2)) Does dataNum store integers or floating point numbers? Numpy won't let you do both in the same array. You should always specify the type of the numpy array that you want to create: dataNum = numpy.ndarray((2152, 2), float) or dataNum = numpy.ndarray((2152, 2), int) As it happens you are creating an array floats. That means that when you try to store an integer in the array below it gets converted to a float. > > #the actual data (the numbers) in the data file begin at line 30 > for anItem in data[30:]: > theNums = anItem.replace("\r\n","").split("\t") > dataNum[k,0] = int(theNums[0]) > dataNum[k,1] = float(theNums[1]) > k+=1 #advance the counter You should look into using numpy.fromfile. This function is specifically designed for this purpose. For example: with open(dataFile) as fdata: header_lines = [fdata.readline() for _ in range(30)] dataNum = numpy.fromfile(fdata, float, sep='\t') Oscar From alan.gauld at btinternet.com Mon Oct 1 12:06:12 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 01 Oct 2012 11:06:12 +0100 Subject: [Tutor] Lotka-Volterra Model Simulation Questions In-Reply-To: References: <5066D082.7050301@pearwood.info> Message-ID: On 01/10/12 09:52, Oscar Benjamin wrote: > I guess we won't get to find out but I assumed that the OP understood > what he was doing mathematically but was struggling with the Python In retrospect I think that's true. When I posted my original reply I assumed he was new to Python and learning about the math. (And of course I didn't know about the math side at all) > I'll meet you (sort of)half way: > > def f_lotka_volterra(x, t): :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From fomcl at yahoo.com Mon Oct 1 14:16:58 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Mon, 1 Oct 2012 05:16:58 -0700 (PDT) Subject: [Tutor] generic repr method? In-Reply-To: References: <1348949729.97991.YahooMailNeo@web110705.mail.gq1.yahoo.com> Message-ID: <1349093818.9231.YahooMailNeo@web110714.mail.gq1.yahoo.com> ----- Original Message ----- > From: eryksun > To: Albert-Jan Roskam > Cc: Python Mailing List > Sent: Sunday, September 30, 2012 1:46 AM > Subject: Re: [Tutor] generic repr method? > > On Sat, Sep 29, 2012 at 4:15 PM, Albert-Jan Roskam > wrote: >> >> ? ? def __repr__(self): >> ? ? ? ? code = self.__class__.__name__ + "(" >> ? ? ? ? for arg in inspect.getargspec(self.__init__).args [1:]? : >> ? ? ? ? ? ? if isinstance(eval("self." + arg), basestring): >> ? ? ? ? ? ? ? ? code += ("%(" + arg + ")r, ") >> ? ? ? ? ? ? else: >> ? ? ? ? ? ? ? ? code += ("%(" + arg + ")s, ") >> ? ? ? ? code = code[:-2] + ")" >> ? ? ? ? return code % self.__dict__ > > > __init__ could use *args and **kwds. > Keyword-only arguments in Python 3 require using inspect.getfullargspec. > A class with __slots__ probably lacks a __dict__. > Use the repr of all values. Hi Oscar, Eryksun, Thanks! My code was an attempt to generalize a __repr__ method from Mark Summerfield's book (http://www.amazon.com/Programming-Python-Complete-Introduction-Language/dp/0137129297). But the inability to deal with *args, **kwargs is maybe its biggest shortcoming. And, as Oscar noted, it depends on the convention that somevalue always maps to self.somevalue in __init__. I had not considered __slots__ at all. I have read about it; IIRC they can be uised to "slim down" a class so it uses less memory. Is it true that this is not used very often? It seems that for my current project I could still use the code, though I'd find it more readable if the keywords are also included in the string. Thanks again! Albert-Jan From fomcl at yahoo.com Mon Oct 1 14:24:03 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Mon, 1 Oct 2012 05:24:03 -0700 (PDT) Subject: [Tutor] Lotka-Volterra Model Simulation Questions In-Reply-To: References: Message-ID: <1349094243.94302.YahooMailNeo@web110707.mail.gq1.yahoo.com> ----- Original Message ----- > From: Alan Gauld > To: tutor at python.org > Cc: > Sent: Sunday, September 30, 2012 10:22 AM > Subject: Re: [Tutor] Lotka-Volterra Model Simulation Questions > > On 30/09/12 00:09, Brett Ritter wrote: > >> agreement.? Can you point to any of the research you mention?? I'd >> like to read into to see how my personal experience equates with the >> overall study - I might learn something! > > I can probably dig out some references but a good place to start if you have > access (and every programmer should! :-) is Steve McConnell's book Code > Complete. > That's also one of my favourite books! Definitely worth reading, also because it is written in quite a humorous way. There are checklists of that book that can be found on the internet: http://www.matthewjmiller.net/files/cc2e_checklists.pdf Chapter 11 is relevant for this discussion. From emile at fenx.com Mon Oct 1 16:05:14 2012 From: emile at fenx.com (Emile van Sebille) Date: Mon, 01 Oct 2012 07:05:14 -0700 Subject: [Tutor] OT: Netiquette In-Reply-To: References: <505F5908.1010304@davea.name> <5B80DD153D7D744689F57F4FB69AF474166C5626@SCACMX008.exchad.jpmchase.net> Message-ID: On 9/30/2012 6:16 PM, Dwight Hutto wrote: > But he started it. Now be the man and end it. Emile From eryksun at gmail.com Mon Oct 1 16:12:36 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 1 Oct 2012 10:12:36 -0400 Subject: [Tutor] generic repr method? In-Reply-To: <1349093818.9231.YahooMailNeo@web110714.mail.gq1.yahoo.com> References: <1348949729.97991.YahooMailNeo@web110705.mail.gq1.yahoo.com> <1349093818.9231.YahooMailNeo@web110714.mail.gq1.yahoo.com> Message-ID: On Mon, Oct 1, 2012 at 8:16 AM, Albert-Jan Roskam wrote: > >I had not considered __slots__ at all. I have read about it; IIRC they >can be uised to "slim down" a class so it uses less memory. Is it true >that this is not used very often? In a class defined with __slots__, it's up to you whether there should be a slot for __dict__ and __weakref__ (see the weakref module). Usually these aren't included because the point is, as you say, to have a smaller footprint if you need to create thousands of objects. But __dict__ can still be a property that returns a new dict when requested. For example, have you ever used collections.namedtuple? It's basically a tuple with the indexed items mapped to field-name attributes via properties. It's meant for light-weight, heterogeneous data records. namedtuple uses an an empty __slots__ (the only option for tuple subclasses) in order to exclude creation of an instance dict. Instead the __dict__ attribute is a property (_asdict is the getter) that returns a new collections.OrderedDict. From oscar.j.benjamin at gmail.com Mon Oct 1 16:24:11 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 1 Oct 2012 15:24:11 +0100 Subject: [Tutor] generic repr method? In-Reply-To: <1349093818.9231.YahooMailNeo@web110714.mail.gq1.yahoo.com> References: <1348949729.97991.YahooMailNeo@web110705.mail.gq1.yahoo.com> <1349093818.9231.YahooMailNeo@web110714.mail.gq1.yahoo.com> Message-ID: On 1 October 2012 13:16, Albert-Jan Roskam wrote: > >> On Sat, Sep 29, 2012 at 4:15 PM, Albert-Jan Roskam >> wrote: >>> >>> def __repr__(self): >>> code = self.__class__.__name__ + "(" >>> for arg in inspect.getargspec(self.__init__).args [1:] : >>> if isinstance(eval("self." + arg), basestring): Please don't use eval for this. Python has a much better function that is explicitly designed to use what you want., e.g.: eval("self." + arg) # Bad getattr(self, arg) # Good >>> code += ("%(" + arg + ")r, ") >>> else: >>> code += ("%(" + arg + ")s, ") >>> code = code[:-2] + ")" >>> return code % self.__dict__ >> > It seems that for my current project I could still use the code, though I'd find it more readable if the keywords are also included in the string. Is it so hard to write a repr for each class that needs one (most don't)? I think most repr functions I've written have been very short and easy to write. def __repr__(self): return 'MyClass(x={0}, y={1})'.format(self.x, self.y) It's also good to think about each individual class and whether or not the repr really makes sense. Oscar -------------- next part -------------- An HTML attachment was scrubbed... URL: From computer_dude15 at hotmail.com Mon Oct 1 17:45:59 2012 From: computer_dude15 at hotmail.com (Matthew Dalrymple) Date: Mon, 1 Oct 2012 11:45:59 -0400 Subject: [Tutor] html checker Message-ID: Im trying to write an html syntax checker...pretty much read an imported file, if it has all the opening and closing "<" and ">" it will return True and if it doesn't it will return False. this is what i have so farhttp://pastie.org/4891833 how can i get it to run correctly?thanks in advance -------------- next part -------------- An HTML attachment was scrubbed... URL: From cecilia.chavana at gmail.com Mon Oct 1 17:48:59 2012 From: cecilia.chavana at gmail.com (Cecilia Chavana-Bryant) Date: Mon, 1 Oct 2012 16:48:59 +0100 Subject: [Tutor] Help for Python Beginner with extracting and manipulating data from thousands of ASCII files In-Reply-To: References: Message-ID: On Mon, Oct 1, 2012 at 12:33 AM, Alan Gauld wrote: > On 30/09/12 23:07, Cecilia Chavana-Bryant wrote: > >> Hola again Python Tutor! >> >> With a friend's help I have the following code to extract reflectance >> data from an ASCII data file, do a bit of data manipulation to calibrate >> the data and then write the calibrated file into an out file. >> > > > > > > I have successfully calibrated one ASCII file at a time with this code. >> However, I have 1,000s of files that I need to calibrate so I would like >> some help to modify this code so it can: >> >> 1. Use one calibration file (Cal_FileP17.txt) on data files created from >> July to the 18th Sep and a different calibration file (Cal_FileP19.txt) >> for data files created from the 19th of Sep onwards. >> >> 2. Find all the .txt files in a folder called ASCII_files, which is >> subdivided into 12 different folders and calibrate all these files >> > > > Number 2 is easier to solve and the os.walk() and glob.glob() > functions should provide all the tools you need. > > Number 1 is more tricky since there is no obvious way to determine the > arbitrary start/stop dates you specify. So I'd suggest you need to > generalise the requirement to take a start/stop date as well as the > calibration file name and the input data file pattern. Use those as input > parameters to a function that generates the list of files to process and > then calls your existing code (wrapped in a new function) and possibly > provide default values for all/some of the parameters. > > Another option is to add the start/end dates to the calibration file if > you have control of that, but personally I'd stick with input parameters... > > Many thanks Alan for your reply. I have added start and end dates as part of the header information for the calibration files in the date format: 01/07/2011. So, I now need to write some code to take this into consideration, any suggestions? > > -- > 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 cecilia.chavana at gmail.com Mon Oct 1 18:05:56 2012 From: cecilia.chavana at gmail.com (Cecilia Chavana-Bryant) Date: Mon, 1 Oct 2012 17:05:56 +0100 Subject: [Tutor] Help for Python Beginner with extracting and manipulating data from thousands of ASCII files In-Reply-To: <5068E0D1.4050001@davea.name> References: <5068E0D1.4050001@davea.name> Message-ID: On Mon, Oct 1, 2012 at 1:16 AM, Dave Angel wrote: > On 09/30/2012 06:07 PM, Cecilia Chavana-Bryant wrote: > > Hola again Python Tutor! > > > > With a friend's help I have the following code to extract reflectance > data > > from an ASCII data file, do a bit of data manipulation to calibrate the > > data and then write the calibrated file into an out file. > > > > import numpy > > # import glob - use if list_of_files is used > > > > > > dataFile = "1SH0109.001.txt" > > #list_of_files = glob.glob('./*.txt') to replace dataFile to search for > all > > text files in ASCII_files folder? > > First, an important observation. This code has no functions defined in > it. Thus it's not reusable. So every time you make a change, you'll be > breaking the existing code and then trying to make the new version work. > > The decision of one file versus many is usually handled by writing a > function that deals with one file. Test it with a single file. Then > write another function that uses glob to build a list of files, and call > the original one in a loop. > > As you work on it, you should discover that there are a half dozen other > functions that you need, rather than one big one. > > Many thanks for this advise this helps me to get started with trying to write functions for the different procedures and then think about many files. > > caliFile1 = "Cal_File_P17.txt" # calibration file to be used on data > files > > created from July to 18 Sep > > caliFile2 = "Cal_File_P19.txt" # calibration file to be used on data > files > > created from 19 Sep onwards > > outFile = "Cal_" + dataFile # this will need to change if list_of_files > is > > used > > fileDate = data[6][16:26] # location of the creation date on the data > files > > Show us the full traceback from the runtime error you get on this line. > > The option of using 2 different calibration files is an idea that I haven't tested yet as I am a bit lost in how to do this. I have gotten as far as adding start and end dates on both calibration files as part of the header information for each file. #extract data from data file > > fdData = open(dataFile,"rt") > > data = fdData.readlines() > > fdData.close() > > > > #extract data from calibration file > > fdCal = open(caliFile,"rt") > > Show us the full traceback from the runtime error here, as well. > > In the original code which uses only one calibration file this and the rest of the code works without error. > > calibration = fdCal.readlines() > > fdCal.close() > > > > #create data table > > k=0 #just a counter > > dataNum = numpy.ndarray((2151,2)) > > > > #the actual data (the numbers) in the data file begin at line 30 > > for anItem in data[30:]: > > theNums = anItem.replace("\r\n","").split("\t") > > dataNum[k,0] = int(theNums[0]) > > dataNum[k,1] = float(theNums[1]) > > k+=1 #advance the counter > > > > #create the calibration table > > k = 0 > > calNum = numpy.ndarray((2151,2)) > > for anItem in calibration[5:]: > > theNums = anItem.replace("\r\n","").split("\t") > > calNum[k,0] = int(theNums[0]) > > calNum[k,1] = float(theNums[1]) > > k+=1 > > > > #calibrate the data > > k=0 > > calibratedData = numpy.ndarray((2151,2)) > > for aNum in dataNum: > > calibratedData[k,0] = aNum[0] #first column is the wavelength > > calibratedData[k,1] = (aNum[1] * dataNum[k,1]) * 100.0 #second column > > is the measurement to be calibrated. > > k+=1 > > > > #write the calibrated data > > fd = open(outFile,"wt") > Error traceback ? > > #prior to writing the calibrated contents, write the headers for data > files > > and calibration files > > fd.writelines(data[0:30]) > > fd.writelines(calibration[0:5]) > > for aNum in calibratedData: > > #Write the data in the file in the following format: > > # "An integer with 3 digits", "tab character", "Floating point > number" > > fd.write("%03d\t%f\n" % (aNum[0],aNum[1])) > > > > #close the file > > fd.close() > > > > Are the individual files small? By doing readlines() on them, you're > assuming you can hold all of both the data file and the calibration file > in memory. > > Both the calibration and the data files are small. The original excel calibration files have been saved as "Tab delimited text files" and the data files are ASCII files with 2151 rows and 2 columns. > I have successfully calibrated one ASCII file at a time with this code. > Unless I'm missing something, this code does not run. I didn't try it, > though, just inspected it quickly. > > However, I have 1,000s of files that I need to calibrate so I would like > > some help to modify this code so it can: > > > > 1. Use one calibration file (Cal_FileP17.txt) on data files created from > > July to the 18th Sep and a different calibration file (Cal_FileP19.txt) > for > > data files created from the 19th of Sep onwards. > > > > 2. Find all the .txt files in a folder called ASCII_files, which is > > subdivided into 12 different folders and calibrate all these files > > > > I have googled and tried thinking about how to make changes and I've > > managed to get myself a bit more confused. Thus, I would like some > guidance > > on how to tackle/think about this process and how to get started. > Please, I > > am not asking for someone to do my work and write the code for me, I > would > > like some guidance on how to approach this and get started. > > > > Many thanks in advance for your help, > > Cecilia > > > > > > > -- > > DaveA > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Mon Oct 1 20:48:39 2012 From: bgailer at gmail.com (bob gailer) Date: Mon, 01 Oct 2012 14:48:39 -0400 Subject: [Tutor] html checker In-Reply-To: References: Message-ID: <5069E587.2070605@gmail.com> On 10/1/2012 11:45 AM, Matthew Dalrymple wrote: > Im trying to write an html syntax checker...pretty much read an > imported file, if it has all the opening and closing "<" and ">" it > will return True and if it doesn't it will return False. > > this is what i have so far > http://pastie.org/4891833 > > how can i get it to run correctly? Welcome to the tutor list. I assume this is your first visit. Why? because we always request that you tell us what happens to lead you to say it does not run correctly. This can mean many things. So - what happens when you run the program? Be specific. Also - always reply-all so a copy goes to the list. Tell us which version of Python you are using, which OS and what you do to run the program. Put your response(s) after the item(s) you are responding to (as I have done here). In other words do not "top-post". Delete irrelevant text (as I have done here). A quick glance at your program shows many flaws. from pythondsBasicimport stack This is an unknown (to me and to Google) module. Where did you get it? What is stack? for linein infile: data= infile.readline() This will read all the lines in the file. Each line in turn will be assigned to data, so at the of the loop data will contain the last line. data is not referenced anyweere else in the program. for chin infile: Will immediately terminate as the file is now at EOF. If you were to remove the for line in infile: loop then ch will contain one line, not one character! Your program does an import and defines functions. There is nothing in it to run any of the functions. There are many other problems! Too many for me to continue analyzing and reporting. Did you succeed in the previous labs? How did you get this far and then fail so miserably? -- Bob Gailer 919-636-4239 Chapel Hill NC -------------- next part -------------- An HTML attachment was scrubbed... URL: From computer_dude15 at hotmail.com Mon Oct 1 21:24:40 2012 From: computer_dude15 at hotmail.com (Matthew Dalrymple) Date: Mon, 1 Oct 2012 15:24:40 -0400 Subject: [Tutor] html checker In-Reply-To: <5069E587.2070605@gmail.com> References: , <5069E587.2070605@gmail.com> Message-ID: I don't need to hear how bad my programs are...either you are gonna help or your not...if you have questions about what i have wrote or why i wrote something someway ask...dont just jump to conclusions I forgot to include that i had to write a "stack" function in a "pythonbasic" file to importhttp://pastie.org/4892792 Im here because i want to learn not to be fucking bashed...a few of the functions were copied from my book as they were required for other parts of the lab...so again if you have a problem with those other functions maybe you should take it up with the author... anyway i just don't know where to start to get the htmlChecker function to run...i am not a programmer by any means but is required to learn for my major...this is why im stuggling...i have networking experience not programming Date: Mon, 1 Oct 2012 14:48:39 -0400 From: bgailer at gmail.com To: computer_dude15 at hotmail.com CC: tutor at python.org Subject: Re: [Tutor] html checker On 10/1/2012 11:45 AM, Matthew Dalrymple wrote: Im trying to write an html syntax checker...pretty much read an imported file, if it has all the opening and closing "<" and ">" it will return True and if it doesn't it will return False. this is what i have so far http://pastie.org/4891833 how can i get it to run correctly? Welcome to the tutor list. I assume this is your first visit. Why? because we always request that you tell us what happens to lead you to say it does not run correctly. This can mean many things. So - what happens when you run the program? Be specific. Also - always reply-all so a copy goes to the list. Tell us which version of Python you are using, which OS and what you do to run the program. Put your response(s) after the item(s) you are responding to (as I have done here). In other words do not "top-post". Delete irrelevant text (as I have done here). A quick glance at your program shows many flaws. from pythondsBasic import stack This is an unknown (to me and to Google) module. Where did you get it? What is stack? for line in infile: data = infile.readline() This will read all the lines in the file. Each line in turn will be assigned to data, so at the of the loop data will contain the last line. data is not referenced anyweere else in the program. for ch in infile: Will immediately terminate as the file is now at EOF. If you were to remove the for line in infile: loop then ch will contain one line, not one character! Your program does an import and defines functions. There is nothing in it to run any of the functions. There are many other problems! Too many for me to continue analyzing and reporting. Did you succeed in the previous labs? How did you get this far and then fail so miserably? -- Bob Gailer 919-636-4239 Chapel Hill NC -------------- next part -------------- An HTML attachment was scrubbed... URL: From walksloud at gmail.com Mon Oct 1 21:38:07 2012 From: walksloud at gmail.com (Andre' Walker-Loud) Date: Mon, 1 Oct 2012 12:38:07 -0700 Subject: [Tutor] html checker In-Reply-To: References: , <5069E587.2070605@gmail.com> Message-ID: <00C01BE8-2A69-46B7-A832-ECB34478E041@gmail.com> Dear Matthew, > I don't need to hear how bad my programs are...either you are gonna help or your not...if you have questions about what i have wrote or why i wrote something someway ask...dont just jump to conclusions > > I forgot to include that i had to write a "stack" function in a "pythonbasic" file to import > http://pastie.org/4892792 > > Im here because i want to learn not to be fucking bashed...a few of the functions were copied from my book as they were required for other parts of the lab...so again if you have a problem with those other functions maybe you should take it up with the author... > > anyway i just don't know where to start to get the htmlChecker function to run...i am not a programmer by any means but is required to learn for my major...this is why im stuggling...i have networking experience not programming Bob was not trying to bash you. You clearly must be frustrated with your program, as your response was very intense and negative. Try not to project your frustration with your program out on the people who are trying to help you. Recall, those responding to the list are purely volunteers - they do what they do because they enjoy helping others learn about python - or feel like giving back to new people learning since they received help from this list and others. In addition to helping you with the programming, the senior responders also try to teach you how to think about your problems better, including what information to include in such emails, so that they can help you. Bob was pointing out that you have not included enough information in your original post for those on the list to help you. May I suggest that you relieve your frustration on something local, and then try and see what information Bob was asking for that you can provide, so that people on the list can provide you with guidance and help. Cheers, Andre From illusiontechniques at gmail.com Mon Oct 1 21:42:02 2012 From: illusiontechniques at gmail.com (c smith) Date: Mon, 1 Oct 2012 15:42:02 -0400 Subject: [Tutor] html checker In-Reply-To: References: <5069E587.2070605@gmail.com> Message-ID: You will not find much help in getting a program to 'just work' regardless of your own experience. My advice would be to try and run small parts at a time to pinpoint where the problem is. Are you opening and reading the file properly? Are you iterating over the read file properly? Does your html check work on a local, properly formatted html file? Create a small text file to run it on. Also keep track of file position and when you are assigning variables new values. I am still learning myself, so this isnt very specific, but you will have to ask more specifically so more experienced people will be willing to help without doing your work for you. Good luck -------------- next part -------------- An HTML attachment was scrubbed... URL: From brian.van.den.broek at gmail.com Mon Oct 1 21:53:11 2012 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Mon, 1 Oct 2012 15:53:11 -0400 Subject: [Tutor] html checker In-Reply-To: References: <5069E587.2070605@gmail.com> Message-ID: On 1 Oct 2012 15:28, "Matthew Dalrymple" wrote: > > I don't need to hear how bad my programs are...either you are gonna help or your not...if Matthew, Bob didn't cuddle you and he may have been a bit more brusque than you'd have liked. However, his response to you was intended as help, it provided what would be help if you would read it as such, and it was worth much more than you paid for it. Your reply to that offer of help is not such as to encourage anyone else to give their time to you. I suggest you have a calm breath and reflect. You can surmount your start with this list, but for more than a few who answer here, you'll have to demonstrate you are more appreciative of the time and effort you are given before you get more. While you may not like this email I am fairly sure it contains a message you very much need to hear. Best, Brian vdB -------------- next part -------------- An HTML attachment was scrubbed... URL: From computer_dude15 at hotmail.com Mon Oct 1 21:59:52 2012 From: computer_dude15 at hotmail.com (Matthew Dalrymple) Date: Mon, 1 Oct 2012 15:59:52 -0400 Subject: [Tutor] html checker In-Reply-To: <00C01BE8-2A69-46B7-A832-ECB34478E041@gmail.com> References: , <5069E587.2070605@gmail.com> , <00C01BE8-2A69-46B7-A832-ECB34478E041@gmail.com> Message-ID: > Subject: Re: [Tutor] html checker > From: walksloud at gmail.com > Date: Mon, 1 Oct 2012 12:38:07 -0700 > CC: tutor at python.org > To: computer_dude15 at hotmail.com > > Dear Matthew, > > > I don't need to hear how bad my programs are...either you are gonna help or your not...if you have questions about what i have wrote or why i wrote something someway ask...dont just jump to conclusions > > > > I forgot to include that i had to write a "stack" function in a "pythonbasic" file to import > > http://pastie.org/4892792 > > > > Im here because i want to learn not to be fucking bashed...a few of the functions were copied from my book as they were required for other parts of the lab...so again if you have a problem with those other functions maybe you should take it up with the author... > > > > anyway i just don't know where to start to get the htmlChecker function to run...i am not a programmer by any means but is required to learn for my major...this is why im stuggling...i have networking experience not programming > > > Bob was not trying to bash you. > You clearly must be frustrated with your program, as your response was very intense and negative. > Try not to project your frustration with your program out on the people who are trying to help you. > Recall, those responding to the list are purely volunteers - they do what they do because they enjoy helping others learn about python - or feel like giving back to new people learning since they received help from this list and others. > > In addition to helping you with the programming, the senior responders also try to teach you how to think about your problems better, including what information to include in such emails, so that they can help you. Bob was pointing out that you have not included enough information in your original post for those on the list to help you. > > May I suggest that you relieve your frustration on something local, and then try and see what information Bob was asking for that you can provide, so that people on the list can provide you with guidance and help. > > > Cheers, > > Andre No i understand that i might not have been as specific as i needed to be and that i did forget to include a lot of information that is kind of required to understand why i did what i did with certan areas... what bothered me though is when he asked if i was ever succesful in previous labs an how i could have gotten so far and still fail so miserably...i feel that was uncalled for and really not necessary in helping me with the program I wrote the "stack" moduleI have made a few changes to the file opening lines...and i think that part is working nowThe professor wants to start the program on his own by running "htmlChecker()" in IDLEAlso this is in Python 3.2 What i need to have done is to verify that every "<" at the beginning of the tags has a ">" at the end...and visa versa...and i have to do this using "pop" and "push" i don't really understand the pop and push or even stacks for that matter...the professor i have isn't really the best at teaching...so if anyone could give me a hand with any of this that would be appreciated Matt -------------- next part -------------- An HTML attachment was scrubbed... URL: From brian.van.den.broek at gmail.com Mon Oct 1 22:05:47 2012 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Mon, 1 Oct 2012 16:05:47 -0400 Subject: [Tutor] Lotka-Volterra Model Simulation Questions In-Reply-To: References: <5066D082.7050301@pearwood.info> Message-ID: On 30 September 2012 04:37, Alan Gauld wrote: > > One of the things that makes math hard for people to grasp is its insistence > on abstracting functions/values to single letter names etc. (especially when > those names are in a foreign language/symbology, > like Greek!) Of course, the abstraction is powerful in its own right because > it can then be applied in multiple domains, but that abstraction is often > the barrier to people understanding the > principle. Those that are "good at math" are often really those > who are "good at abstraction". > Hi Alan and all, While I think I see what you mean here, Alan, I cannot quite resist and, as this thread long since got hopelessly off-topic :-) I feel no need for restraint. To a first approximation, mathematics can reasonably be thought of as the science of abstraction. So, to say (with a hint of complaint) that those who are good at math are often those who are good at abstraction seems a bit like complaining that it is those with good spatial reasoning and a sense of direction that are good at navigation. While it is indeed possible for mathematical presentation to devolve into unhelpful abstraction (it is this that I suspect Alan intended to target), abstraction is of the essence to the enterprise; nothing that still counts as maths could be easily understood by those without the ability to think abstractly. Having posted twice in a half-hour, I resume my lurk-cloak. Best to all, Brian vdB From illusiontechniques at gmail.com Mon Oct 1 22:15:03 2012 From: illusiontechniques at gmail.com (c smith) Date: Mon, 1 Oct 2012 16:15:03 -0400 Subject: [Tutor] html checker In-Reply-To: References: <5069E587.2070605@gmail.com> <00C01BE8-2A69-46B7-A832-ECB34478E041@gmail.com> Message-ID: yourlisthere.pop() will return the last element in the list and change the list so it no longer contains the element. yourlisthere.push(x) will add x to the end of the list. Works on more than just lists -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Mon Oct 1 22:16:46 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 1 Oct 2012 16:16:46 -0400 Subject: [Tutor] html checker In-Reply-To: References: <5069E587.2070605@gmail.com> <00C01BE8-2A69-46B7-A832-ECB34478E041@gmail.com> Message-ID: > i don't really understand the pop and push or even stacks for that > matter...the professor i have isn't really the best at teaching...so if > anyone could give me a hand with any of this that would be appreciated The way I was taught about pop and push: Think of a stack of dishes. Each time you push you are adding a new dish to the top of a stack. Each time you pop you are removing a dish from the stack. > > Matt > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick From eryksun at gmail.com Mon Oct 1 22:23:22 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 1 Oct 2012 16:23:22 -0400 Subject: [Tutor] html checker In-Reply-To: References: Message-ID: On Mon, Oct 1, 2012 at 11:45 AM, Matthew Dalrymple wrote: > > Im trying to write an html syntax checker...pretty much read an imported > file, if it has all the opening and closing "<" and ">" it will return True > and if it doesn't it will return False. It's just this htmlChecker function that you need help with, right? Below I've indented your code by 4 spaces and split it into sections. I've placed simple code suggestions in-line with my comments. I hope it's easy enough to follow. def htmlChecker(): fname = input('What is the name of the file you would like to open: ') infile = open(fname, 'r+') You should generalize this by having the function take "fname" as an argument and moving the "input" line to another function that calls htmlChecker. Keep the back-end processing separate from the user interface. Also, why are you opening in read-write mode? Use "open(fname)" to open the file in read-only text mode. for line in infile: data = infile.readline() Python 3 doesn't raise an error for this, but Python 2 would. (1) You're already iterating through the file by line, i.e. "for line in infile", so why do you call readline() in the body of the loop? (2) This repeatedly assigns a line from the file to the name "data". It does not extend "data", nor would that be generally advisable since it's inefficient to repeatedly allocate, copy, and deallocate memory to extend a string. (3) If you want the entire contents of the file in "data", use either "data = infile.read()" to get it all as one string, or "data = infile.readlines()" (note the "s" in "readlines") to load the lines of the file into a list. s = stack() s1 = stack() Stack s1 seems to be for the contents of the tag. You can just use a list for this, i.e. "s1 = []". Actually, you can use a list as a stack, too, but I assume you were instructed to use this particular stack data structure. A stack is just like a stack of plates. You can push a plate on top, and pop one off. The "list.append" method functions to "push" a value on the top (end of the list), and "list.pop()" defaults to removing the last item if you don't specify an index (like popping off the top of a stack). for ch in infile: (1) File objects iterate by line, not by character. (2) You've already exhausted the iterator from the last loop, so this loop immediately terminates. Let's assume you've read the entire file into memory as a string with "data = infile.read()". Then you can iterate over the characters with "for ch in data". if ch =='<': if s.isEmpty(): s.push(ch) else: return s.isEmpty() Why are you returning s.isEmpty()? Does this method return something other than True/False? Otherwise, I think you should explicitly "return False". if ch =='>': s.pop() This should use "elif", not "if". ch can't be both '<' and '>' (it's made of classical bits, not quantum qubits), so there's no reason to test for '>' if it already matched '<'. if not s.isEmpty()and ch!='<': s1.push(ch) As above, but this line should be "elif not s.isEmpty()". If you make s1 a list, you can append the character with "s1.append(ch)". Finally: print(s1) If s1 is an iterable sequence, you can print the string using either "print(''.join(s1))" or "print(*s1, sep='')". Also, at this point should s1 be emptied? From eryksun at gmail.com Mon Oct 1 22:34:55 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 1 Oct 2012 16:34:55 -0400 Subject: [Tutor] html checker In-Reply-To: References: Message-ID: On Mon, Oct 1, 2012 at 4:23 PM, eryksun wrote: > > Finally: > > > print(s1) > > > If s1 is an iterable sequence, you can print the string using either > "print(''.join(s1))" or "print(*s1, sep='')". Also, at this point > should s1 be emptied? Sorry, that last statement was wrong. I was thinking you were progressively printing the contents of each tag. But that would need to happen when you pop() the stack. What you need to do here is "return True", else your function implicitly returns "None". From bala.biophysics at gmail.com Mon Oct 1 23:04:42 2012 From: bala.biophysics at gmail.com (Bala subramanian) Date: Mon, 1 Oct 2012 23:04:42 +0200 Subject: [Tutor] 1d to 2d array creation Message-ID: Friends, I have an 1d array like a=[1, 1, 2, 2, 2, 3, 3, 1, 1, 1], i have to convert it to 2d array for plotting as follows. The 2d array is filled by a[colum index] to obtain the new array shown below. [ [ 1., 1., 0., 0., 0., 0., 0., 1., 1., 1.], [ 0., 0., 2., 2., 2., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 3., 3., 0., 0., 0.] ] I wrote the following simple code for the conversion. However i guess there should be more fancy/speeder way to do that. Also i need to create such 2d-array from larger 1d arrays of size 20000,30000 items etc. Hence i would like to request hints for a better code for the purpose. Here no. rows in my case is always = no. of discrete values in array a. >>>my=1 >>>for i in range(3): >>> for j in range(10): >>> if a[j] == my : b[i,j]=my >>> else: b[i,j]=0 >>> my +=1 Thanks, Bala From oscar.j.benjamin at gmail.com Tue Oct 2 00:27:39 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 1 Oct 2012 23:27:39 +0100 Subject: [Tutor] 1d to 2d array creation In-Reply-To: References: Message-ID: On 1 October 2012 22:04, Bala subramanian wrote: > Friends, > I have an 1d array like a=[1, 1, 2, 2, 2, 3, 3, 1, 1, 1], i have to > convert it to 2d array for plotting as follows. The 2d array is filled > by a[colum index] to obtain the new array shown below. > > [ [ 1., 1., 0., 0., 0., 0., 0., 1., 1., 1.], > [ 0., 0., 2., 2., 2., 0., 0., 0., 0., 0.], > [ 0., 0., 0., 0., 0., 3., 3., 0., 0., 0.] ] > > I wrote the following simple code for the conversion. However i guess > there should be more fancy/speeder way to do that. Also i need to > create such 2d-array from larger 1d arrays of size 20000,30000 items > etc. Hence i would like to request hints for a better code for the > purpose. > > Here no. rows in my case is always = no. of discrete values in array a. > > >>>my=1 > >>>for i in range(3): > >>> for j in range(10): > >>> if a[j] == my : b[i,j]=my > >>> else: b[i,j]=0 > >>> my +=1 > Instead of my = 1 for i in range(3): # stuff my += 1 why not do for my in range(1, 4): # stuff But actually it makes more sense to eliminate one of the loops and do: for i, ai in enumerate(a): b[i, ai] = ai It may be that you get better speed with something like for j in range(max(a.max)): b[j, a==j+1] = j Oscar -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Oct 2 01:30:28 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 02 Oct 2012 00:30:28 +0100 Subject: [Tutor] Lotka-Volterra Model Simulation Questions In-Reply-To: References: <5066D082.7050301@pearwood.info> Message-ID: On 01/10/12 21:05, Brian van den Broek wrote: > On 30 September 2012 04:37, Alan Gauld wrote: >> like Greek!) Of course, the abstraction is powerful in its own right because >> it can then be applied in multiple domains, but that abstraction is often >> the barrier to people understanding the principle. > To a first approximation, mathematics can reasonably be thought of as > the science of abstraction. Absolutely and that's what I mean by its general applicability. > So, to say (with a hint of complaint) No complaint was intended it was just an observation. But equally I have observed that people who think they can't do math can often *use* math successfully once the abstract has been translated to the specific. They understand what the math is telling them but can't relate to it in a purely abstract form. The math community sometimes forgets that not everyone thinks as they do and to communicate their ideas they need to revert to specifics sometimes. > it is indeed possible for mathematical presentation to devolve into > unhelpful abstraction (it is this that I suspect Alan intended to > target), abstraction is of the essence to the enterprise; Absolutely. I suspect this thread has been sparked because I just finished reading a book (The Geek Manifesto) which complains at length about how few figures in public life understand the workings of science and math. But I think that the same could be said about scientists' understanding of accounts/law/politics. If we don't expect scientists to grok legalese why should we expect politicians to speak math. Those who can need to do the translation for them, not just complain of their 'ignorance'. But that's now taking things way, way off topic!! :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From brian.van.den.broek at gmail.com Tue Oct 2 01:42:00 2012 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Mon, 1 Oct 2012 19:42:00 -0400 Subject: [Tutor] Lotka-Volterra Model Simulation Questions In-Reply-To: References: <5066D082.7050301@pearwood.info> Message-ID: On 1 October 2012 19:30, Alan Gauld wrote: > translation for them, not just complain of their 'ignorance'. But that's now > taking things way, way off topic!! :-) I think you meant ``way^2 off topic'' ;-) Brian vdB From alan.gauld at btinternet.com Tue Oct 2 01:46:31 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 02 Oct 2012 00:46:31 +0100 Subject: [Tutor] html checker In-Reply-To: References: , <5069E587.2070605@gmail.com> , <00C01BE8-2A69-46B7-A832-ECB34478E041@gmail.com> Message-ID: On 01/10/12 20:59, Matthew Dalrymple wrote: > i don't really understand the pop and push or even stacks for that > matter...the professor i have isn't really the best at teaching...so if > anyone could give me a hand with any of this that would be appreciated The Raw materials topic in my tutor has a short intro to stacks. It says, in part: ---------------- Stack Think of a stack of trays in a restaurant. A member of staff puts a pile of clean trays on top and these are removed one by one by customers. The trays at the bottom of the stack get used last (and least!). Data stacks work the same way: you push an item onto the stack or pop one off. The item popped is always the last one pushed. This property of stacks is sometimes called Last In First Out or LIFO. One useful property of stacks is that you can reverse a list of items by pushing the list onto the stack then popping it off again. The result will be the reverse of the starting list. Stacks are not built in to Python, VBScript or JavaScript. You have to write some program code to implement the behavior. Lists are usually the best starting point since like stacks they can grow as needed. ----------------- As c smith points out, Python lists have a pop/push mechanism as standard which makes implementing a stack in Python fairly trivial. To expand on how reversing works consider pushing the string foo onto the stack then popping it off again: s = 'foo' stack = [] stack.push(s[0]) # stack -> ['f'] stack.push(s[1]) # stack -> ['o','f'] stack.push(s[2]) # stack -> ['o','o','f'] c1 = stack.pop() # stack -> ['o','f'], c1 = 'o' c2 = stack.pop() # stack -> ['f'], c1 = 'o', c2 = 'o' c3 = stack.pop() # stack -> [], c1 = 'o', c2 = 'o', c3 = 'f' print c1+c2+c3 # prints 'oof' the reverse of s HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From illusiontechniques at gmail.com Tue Oct 2 02:08:29 2012 From: illusiontechniques at gmail.com (c smith) Date: Mon, 1 Oct 2012 20:08:29 -0400 Subject: [Tutor] HELP! In-Reply-To: <50689E23.4050901@gmail.com> References: <50689E23.4050901@gmail.com> Message-ID: Is the only problem that your code is giving unexpected results, or that it doesnt run or what? -------------- next part -------------- An HTML attachment was scrubbed... URL: From illusiontechniques at gmail.com Tue Oct 2 02:16:59 2012 From: illusiontechniques at gmail.com (c smith) Date: Mon, 1 Oct 2012 20:16:59 -0400 Subject: [Tutor] python help? In-Reply-To: References: Message-ID: It is hard to see things like images and attachments. I think purely html is preferred, but i would have to look over 'the list rules' again. You should look into dictionaries as the structure to hold your info. -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Tue Oct 2 03:14:40 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 1 Oct 2012 21:14:40 -0400 Subject: [Tutor] html checker In-Reply-To: References: <5069E587.2070605@gmail.com> <00C01BE8-2A69-46B7-A832-ECB34478E041@gmail.com> Message-ID: On Mon, Oct 1, 2012 at 7:46 PM, Alan Gauld wrote: > > As c smith points out, Python lists have a pop/push mechanism as standard > which makes implementing a stack in Python fairly trivial. > > To expand on how reversing works consider pushing the string foo onto the > stack then popping it off again: > > s = 'foo' > stack = [] > stack.push(s[0]) # stack -> ['f'] > stack.push(s[1]) # stack -> ['o','f'] > stack.push(s[2]) # stack -> ['o','o','f'] > > c1 = stack.pop() # stack -> ['o','f'], c1 = 'o' > c2 = stack.pop() # stack -> ['f'], c1 = 'o', c2 = 'o' > c3 = stack.pop() # stack -> [], c1 = 'o', c2 = 'o', c3 = 'f' > > print c1+c2+c3 # prints 'oof' the reverse of s It's a good example, but please indicate when you've switched to pseudocode. In Python "stack = []" assigns a new list to stack, which has no "push" method. It can append() and pop() from the end in constant time to implement an efficient stack. From d at davea.name Tue Oct 2 03:32:57 2012 From: d at davea.name (Dave Angel) Date: Mon, 01 Oct 2012 21:32:57 -0400 Subject: [Tutor] python help? In-Reply-To: References: Message-ID: <506A4449.50007@davea.name> On 10/01/2012 08:16 PM, c smith wrote: > It is hard to see things like images and attachments. I think purely html > is preferred, but i would have to look over 'the list rules' again. Since this is a text mailing-list, it's text messages that are preferred. html messages frequently trash indentation, which can be fatal to a python source excerpt. And many people cannot see attachments at all, while others would simply skip any messages that require looking at attachments. > You should look into dictionaries as the structure to hold your info. > > -- DaveA From robertvstepp at gmail.com Tue Oct 2 03:34:03 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Mon, 1 Oct 2012 20:34:03 -0500 Subject: [Tutor] Civil discourse from a newbie's perspective Message-ID: I have been following the discussions here since middle-May of this year. I have gathered that the volunteers strongly value precision of speech and proper formatting of posts and especially making a strong effort to solve one's problem(s) before bringing it(them) up here for help. I think I understand the frustrations that can arise when newcomer after newcomer continue to repeat the same basic posting errors, seemingly without end (May I never do the same!). However, I think that extra care may be needed in dealing with newbies who may only be taking a programming course using Python because of curriculum requirements. I'm sure I am stating the obvious to most of you, but, nonetheless, I think it is still worth saying (Again?). How one of you phrase your meant-to-be-helpful response can inadvertently prove crushing to someone who is very insecure in their programming knowledge, and in some instances, apparently, incite them to profanity. In my first post here I inappropriately used the phrase, "begs the question." I soon was informed of the error of my ways! While educational (And, I confess, somewhat irritating.), these clarifications had nothing to do with the actual intent of my post. I shrugged it off, examined the comments and links about my offending phrase, and went on to appreciate the valuable helpful comments that I did receive on what I was really asking about. I only bring this up as a concrete example of an instance that might have caused a newbie to go elsewhere in frustration and anger, if I were thin-skinned about it. I am hesitant in even bringing these thoughts up, but it seems recently there has been a rash of anger, profanity and hurt feelings. Of course this is my subjective impression, which may be colored by my lack of a thick skin. But I DO want to say, that I greatly value the efforts of the volunteers who strive to be so helpful and demonstrate repeatedly great patience in the face of what clearly must, at times, be extremely frustrating. But please exercise precision in your communications with overly sensitive, frustrated or tentative newbies! -- Cheers! boB Stepp From d at davea.name Tue Oct 2 03:44:47 2012 From: d at davea.name (Dave Angel) Date: Mon, 01 Oct 2012 21:44:47 -0400 Subject: [Tutor] python help? In-Reply-To: References: Message-ID: <506A470F.5030302@davea.name> On 09/30/2012 02:02 AM, patrick Howard wrote: > I have to write a program that takes an input file, with students names and > various grades. > My vindictive teacher also added grades that are not supposed to count, so I > need to pick the grades that are 'HM1-4"; not HW, or TEST or anything else. > Then when these are listed in order with that persons name, sort the list > alphabetically. I know that this is '.sort; > But, how do I get it to sort only names, AND keep the right grades with them. > Below is a list of parameters? Can you help me please? > > What part are you stuck on? It's hard to give advice when we don't know what part of the assignment matters. First question is what language are you to write this in, and on what OS. Assuming it's Python, then what's the version? I'm a little surprised you call the teacher vindictive. Bad data is quite common in the real world. And in the real world, I'd be writing a separate program to "cleanse" the data before processing. Anyway, first you need to be able to open the specified file and read and parse the text lines into some form of data structure. Second you manipulate that structure, so that the output can be produced in a straightforward manner. Third, you produce the output, which now should be pretty straightforward. Write them as three (at least) separate functions, and you can debug them separately. Much easier than trying to make it a monolith which either works or is hopeless. So the first question is what kind of structure can hold all that data. I have no idea what level of Python you've attained, but I'll guess you don't know how to write your own classes or generators. So you have to build the structures from whatever's in the standard library. Outermost structure is a list, one per student. Each item of the list is a defaultdict, keyed by name, and by the various HMn fields. Values of each of those dict items are floats. -- DaveA From bgailer at gmail.com Tue Oct 2 03:51:39 2012 From: bgailer at gmail.com (bob gailer) Date: Mon, 01 Oct 2012 21:51:39 -0400 Subject: [Tutor] what's wrong with my code? (was HELP!) In-Reply-To: <50689E23.4050901@gmail.com> References: <50689E23.4050901@gmail.com> Message-ID: <506A48AB.2040205@gmail.com> On 9/30/2012 3:31 PM, Mark Rourke wrote: > hello, I am a college student in my first year of computer > programming, I was wondering if you could look at my code to see whats > wrong with it. Welcome to the tutor list. I assume this is your first visit. Why? because we always request that you tell us what happens to lead you to say it does not run correctly. This can mean many things. So - what happens when you run the program? Be specific. Also - always reply-all so a copy goes to the list. Tell us which version of Python you are using, which OS and what you do to run the program. Put your response(s) after the item(s) you are responding to (as I have done here). In other words do not "top-post". Delete irrelevant text (as I have done here). Provide a problem-specific subject (not HELP) so we can track our conversation. Realize that we are a few volunteers (no pay other than the fun of assisting). -- Bob Gailer 919-636-4239 Chapel Hill NC -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Tue Oct 2 03:58:36 2012 From: d at davea.name (Dave Angel) Date: Mon, 01 Oct 2012 21:58:36 -0400 Subject: [Tutor] python help? In-Reply-To: <506A470F.5030302@davea.name> References: <506A470F.5030302@davea.name> Message-ID: <506A4A4C.1040707@davea.name> On 10/01/2012 09:44 PM, Dave Angel wrote: > On 09/30/2012 02:02 AM, patrick Howard wrote: >> I have to write a program that takes an input file, with students names and >> various grades. >> My vindictive teacher also added grades that are not supposed to count, so I >> need to pick the grades that are 'HM1-4"; not HW, or TEST or anything else. >> Then when these are listed in order with that persons name, sort the list >> alphabetically. I know that this is '.sort; >> But, how do I get it to sort only names, AND keep the right grades with them. Use a tuple of (name, grades) where the name is a string, and the grades are in a dict or defaultdict. When sorting a list of tuples, the sort will work only on the first element of each tuple, unless there's a tie. Checking for duplicate student names is one of those things that gets separately verified, before even running your code. >> Below is a list of parameters? Can you help me please? >> >> > What part are you stuck on? It's hard to give advice when we don't know > what part of the assignment matters. First question is what language are > you to write this in, and on what OS. Assuming it's Python, then what's > the version? > > I'm a little surprised you call the teacher vindictive. Bad data is > quite common in the real world. And in the real world, I'd be writing a > separate program to "cleanse" the data before processing. > > Anyway, first you need to be able to open the specified file and read > and parse the text lines into some form of data structure. > > Second you manipulate that structure, so that the output can be produced > in a straightforward manner. > > Third, you produce the output, which now should be pretty straightforward. > > Write them as three (at least) separate functions, and you can debug > them separately. Much easier than trying to make it a monolith which > either works or is hopeless. > > So the first question is what kind of structure can hold all that data. > I have no idea what level of Python you've attained, but I'll guess you > don't know how to write your own classes or generators. So you have to > build the structures from whatever's in the standard library. > > Outermost structure is a list, one per student. Each item of the list is > a defaultdict, keyed by name, and by the various HMn fields. Values of > each of those dict items are floats. > One simplification, so you won't need a confusing key function, is to make each item of the list a tuple, student-name string followed by defaultdict. By not putting the name inside the dict, sort of the list will get the order right by default. In particular, when sorting a list of tuples, it uses the first item of each tuple as a primary key. Recapping: A list of tuples, one tuple per line. First item of the tuple is the studentname for that line. Other item of the tuple is a defaultdict keyed by such strings as HM1, HM2, ... Now, write some code, and when you get stuck, show us what you have, how you tested it, and what went wrong. -- DaveA From brian.van.den.broek at gmail.com Tue Oct 2 04:13:23 2012 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Mon, 1 Oct 2012 22:13:23 -0400 Subject: [Tutor] HELP! In-Reply-To: <50689E23.4050901@gmail.com> References: <50689E23.4050901@gmail.com> Message-ID: On 1 Oct 2012 19:58, "Mark Rourke" wrote: > > hello, I am a college student in my first year of computer programming, I was wondering if you could look at my code to see whats wrong with it. > > # Mark Rourke > # Sept 29, 2012 > # Write a program to calculate the sales tax at the rate of 4% and 2% respectively > # Thereafter compute the total sales tax (sum of the state tax and the county tax) > # and the total purchase amount (sum of the purchase amount and the total sales tax). > SALES_TAX = 0.4 > > COUNTY_TAX = 0.02 > purchaseAmount = input("Please input the Purchase Amount: $") > > #Calculate the State Sales Tax, County Sales Tax, Total Sales Tax, Total Purchase Amount > > purchaseAmount = int(purchaseAmount) > > stateSalesTax = int(purchaseAmount * SALES_TAX) Hi Mark, c smith is certainly right to suggest that you ought to specify a bit more about what the symptoms are that you would like help diagnosing. That said, what should be the result if an item with a sticker price of 1.35 is purchased? Perhaps thinking about that and comparing the following will help: IDLE 2.6.6 >>> user_input = "1.35" >>> purchaseAmount = int(user_input) Traceback (most recent call last): File "", line 1, in purchaseAmount = int(user_input) ValueError: invalid literal for int() with base 10: '1.35' >>> user_input = float("1.35") >>> purchaseAmount = int(user_input) >>> purchaseAmount 1 >>> Best, Brian vdB From steve at pearwood.info Tue Oct 2 07:53:02 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 2 Oct 2012 15:53:02 +1000 Subject: [Tutor] Civil discourse from a newbie's perspective In-Reply-To: References: Message-ID: <20121002055302.GA19020@ando> On Mon, Oct 01, 2012 at 08:34:03PM -0500, boB Stepp wrote: > I have been following the discussions here since middle-May of this > year. I have gathered that the volunteers strongly value precision of > speech and proper formatting of posts Some more than others, but yes. > and especially making a strong > effort to solve one's problem(s) before bringing it(them) up here for > help. Certainly! Many beginners don't realise that the most important skill to learn is not programming syntax, or programming libraries, but the skill to fix your own code. The reason that experienced programmers rarely ask for help is not because they don't make mistakes, but because when they do so, they have the skills to fix the mistake themselves. Skills such as: - read the traceback that Python gives you, it is chock-full of useful information - learn how to interpolate from the error to the cause of the error - learn how to work backwards from the immediate cause of the error to the original cause - test code snippets at the interactive interpreter or IDLE - insert calls to print in your code - how to use the Python debugger. > I think I understand the frustrations that can arise when > newcomer after newcomer continue to repeat the same basic posting > errors, seemingly without end (May I never do the same!). Thank you :) > However, I > think that extra care may be needed in dealing with newbies who may > only be taking a programming course using Python because of curriculum > requirements. I'm sure I am stating the obvious to most of you, but, > nonetheless, I think it is still worth saying (Again?). How one of you > phrase your meant-to-be-helpful response can inadvertently prove > crushing to someone who is very insecure in their programming > knowledge, and in some instances, apparently, incite them to > profanity. What you say is very true. But I want to give you a different perspective on volunteer-based help. On another mailing list I subscribe to, one where I am a beginner, somebody asked a question about clearing the mail indexes used by Kmail so they would be regenerated when he next opened the mail program. It was a pretty simple question, and one of the regulars replied, sympathizing with his problems, wishing that her software problems were as easy to solve, and giving him the answer he was looking for. He turned around and sarcastically thanked her for helping him with his "trivial" problem, gave her permission to ignore his posts in the future, and declared that he too would ignore hers as he was filtering them straight to the trash. What a dick, right? So you'll understand if I'm a tad underwhelmed by suggestions that volunteers should moderate their behaviour to make it easier for ungrateful wretches like this person. Not everyone deserves free help -- it is a privilege, not a right. There are more people who need help than those who are willing and able to give help. When there is more demand for help than there is supply of it, how can we ration assistance? - by charging for it? - by lottery? - first come, first served? - how about by being uncompromisingly strict on those asking for help and insisting that they make it easy for us to help them? If they get offended and abuse us, they're sent to the back of the queue. If they get upset and go away, that's one less person who is asking for help. Only those who demonstrate sufficient humility, intelligence and dedication to accept criticism and learn from that criticism will hang around to ask for, and be given, help. Does that sound harsh? Well, perhaps a little, but what's the alternative? "You volunteers have to spend all your time and energy trying to decipher the mysterious ramblings and cries for help from people too lazy to write in complete sentences, and they probably won't listen to your advice anyway, if they don't abuse you for helping." Screw that. It's about give and take. Both sides have to give a little to take a lot. > In my first post here I inappropriately used the phrase, "begs the > question." I soon was informed of the error of my ways! I should certainly accept so! The correct use of idiom is unvalued for communication, otherwise we will all be hoist up by the petard. *grin* [...] > But I DO want to say, that I greatly value the efforts of the > volunteers who strive to be so helpful and demonstrate repeatedly > great patience in the face of what clearly must, at times, be > extremely frustrating. But please exercise precision in your > communications with overly sensitive, frustrated or tentative newbies! And thank you for saying so, and thank you for your frank expression of your opinion. -- Steven From alan.gauld at btinternet.com Tue Oct 2 10:08:42 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 02 Oct 2012 09:08:42 +0100 Subject: [Tutor] Civil discourse from a newbie's perspective In-Reply-To: References: Message-ID: On 02/10/12 02:34, boB Stepp wrote: > I have been following the discussions here since middle-May of this > year. I have gathered that the volunteers strongly value precision of > speech and proper formatting of posts and especially making a strong > effort to solve one's problem(s) before bringing it(them) up here for > help. That's true although of late we seem to have become a tad over zealous in the policing of such things as top posting and formatting of context. This has not always been the case and we do need to remember that this is not Usenet and the audience include many newcomers to computing and programming and mailing lists, as well as newcomers to python. It's good to teach best practice in such things but we need to remember our main objective is teaching Python not netiquette. > nonetheless, I think it is still worth saying (Again?). How one of you > phrase your meant-to-be-helpful response can inadvertently prove > crushing to someone who is very insecure Indeed, but we are dealing with a multitude of cultures and not all of us speak English as a first language so inadvertent clashes are inevitable. A thick skin and a generous helping of graciousness go a long way, as does the assumption that people are genuinely trying to help (ie. presumed innocence!). > In my first post here I inappropriately used the phrase, "begs the > question." I soon was informed of the error of my ways! It begs the question what that had to do with Python... (sorry, I couldn't resist :-) > phrase, and went on to appreciate the valuable helpful comments that I > did receive on what I was really asking about. I only bring this up as > a concrete example of an instance that might have caused a newbie to > go elsewhere in frustration and anger, if I were thin-skinned about > it. Exactly so and if that were to happen we would have failed in our mission as a mailing list community. We are here to help and if we put people off asking genuine questions for fear of being flamed then we are missing the purpose. > I am hesitant in even bringing these thoughts up, but it seems > recently there has been a rash of anger, profanity and hurt feelings. > Of course this is my subjective impression, which may be colored by my > lack of a thick skin. I am glad you brought the subject up. I had been thinking of posting something similar. There have been a few outbursts of late, and we have seen some posts where the content was entirely aimed at improving the format of the message rather than trying to address the Python aspects of the question. The intent of this list is to provide a friendly and encouraging place where beginners can ask for and receive help with Python. Almost by definition beginners will not all know the preferred internet posting idioms, especially since business standards encourage many of the bad habits we prefer to avoid! Gentle reminders are valid but they should be accompanied by help on the actual issue being raised too! Regards, -- Alan G Tutor list moderator From leamhall at gmail.com Tue Oct 2 11:04:35 2012 From: leamhall at gmail.com (leam hall) Date: Tue, 2 Oct 2012 05:04:35 -0400 Subject: [Tutor] Civil discourse from a newbie's perspective In-Reply-To: References: Message-ID: My own struggles to better communicate, and to have my message heard, supports the concerns raised here. The Python community is a very good one and we are only made better by treating people well. it is easy to go to other lists where I am a newbie and find top posting preferred and other behavior encouraged. Does the welcome e-mail cover any of the recommended behavior? Are there easier ways to request participation within guidelines? Leam -- Mind on a Mission -------------- next part -------------- An HTML attachment was scrubbed... URL: From cecilia.chavana at gmail.com Tue Oct 2 12:16:22 2012 From: cecilia.chavana at gmail.com (Cecilia Chavana-Bryant) Date: Tue, 2 Oct 2012 11:16:22 +0100 Subject: [Tutor] Civil discourse from a newbie's perspective In-Reply-To: References: Message-ID: First of all, a HUGE thanks to the volunteers that share their knowledge in this forum for free! When I first became aware of this and other volunteer forums I was amazed that there are people out there willing to spend their valuable time freely helping those of us in need. Thanks also to everyone that has participated on this post, it has been an enlightening read. In my case, I am a complete beginner not only to python but programming in general, a complete beginner to internet forums (this is my third post ever) and I am also not a native English speaker. So, I feel triply ignorant when I post a question. Not only do I find it difficult to articulate my problem, as being a beginner to programming, I basically don't know what the hell I'm talking about, but after reading other posts where people have been corrected on their formatting I am also very insecure about this. So, I would second Leam's suggestion to add recommended behaviour to the welcome email to this forum and if it is not too much to ask, some guidelines on appropriate formating when participating in this forum. If this information has already been given in previous posts, maybe a permanent link on the forum website to these could be setup so troublesome, ignorant beginners like myself can be referred to it and valuable volunteer's time is not wasted on making these corrections over and over again. On Tue, Oct 2, 2012 at 10:04 AM, leam hall wrote: > My own struggles to better communicate, and to have my message heard, > supports the concerns raised here. The Python community is a very good one > and we are only made better by treating people well. it is easy to go to > other lists where I am a newbie and find top posting preferred and other > behavior encouraged. > > Does the welcome e-mail cover any of the recommended behavior? Are there > easier ways to request participation within guidelines? > > Leam > > -- > Mind on a Mission > > > _______________________________________________ > 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 Tue Oct 2 12:16:41 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 2 Oct 2012 20:16:41 +1000 Subject: [Tutor] Civil discourse from a newbie's perspective In-Reply-To: References: Message-ID: <20121002101641.GB19020@ando> On Tue, Oct 02, 2012 at 05:04:35AM -0400, leam hall wrote: > My own struggles to better communicate, and to have my message heard, > supports the concerns raised here. The Python community is a very good one > and we are only made better by treating people well. it is easy to go to > other lists where I am a newbie and find top posting preferred and other > behavior encouraged. Which lists are those? I am interested to check them out. And which "other behaviour" are you referring to? -- Steven From oscar.j.benjamin at gmail.com Tue Oct 2 13:44:06 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 2 Oct 2012 12:44:06 +0100 Subject: [Tutor] Civil discourse from a newbie's perspective In-Reply-To: References: Message-ID: On 2 October 2012 11:16, Cecilia Chavana-Bryant wrote: > > In my case, I am a complete beginner not only to python but programming in general, a complete beginner to internet forums (this is my third post ever) and I am also not a native English speaker. So, I feel triply ignorant when I post a question. Not only do I find it difficult to articulate my problem, as being a beginner to programming, I basically don't know what the hell I'm talking about, but after reading other posts where people have been corrected on their formatting I am also very insecure about this. Please don't be nervous about posting because you are not sure about the best way to write your post. Nobody will really get upset with you for top-posting or not including all of the relevant information (on other mailing lists they might). The comments that are sometimes made about formatting posts or providing extra information should not be interpreted as criticism. They are intended to help everyone communicate on this list. They are also good advice for posting on other mailing lists. If someone makes a comment like that to you, consider it a piece of advice (that you can ignore if you like). The point is that if your own communication is better then you will get more out of this mailing list. Oscar From breamoreboy at yahoo.co.uk Tue Oct 2 14:17:51 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 02 Oct 2012 13:17:51 +0100 Subject: [Tutor] Civil discourse from a newbie's perspective In-Reply-To: References: Message-ID: On 02/10/2012 12:44, Oscar Benjamin wrote: > On 2 October 2012 11:16, Cecilia Chavana-Bryant > wrote: >> >> In my case, I am a complete beginner not only to python but programming in general, a complete beginner to internet forums (this is my third post ever) and I am also not a native English speaker. So, I feel triply ignorant when I post a question. Not only do I find it difficult to articulate my problem, as being a beginner to programming, I basically don't know what the hell I'm talking about, but after reading other posts where people have been corrected on their formatting I am also very insecure about this. > > Please don't be nervous about posting because you are not sure about > the best way to write your post. Nobody will really get upset with you > for top-posting or not including all of the relevant information (on > other mailing lists they might). > > The comments that are sometimes made about formatting posts or > providing extra information should not be interpreted as criticism. > They are intended to help everyone communicate on this list. They are > also good advice for posting on other mailing lists. > > If someone makes a comment like that to you, consider it a piece of > advice (that you can ignore if you like). The point is that if your > own communication is better then you will get more out of this mailing > list. > > Oscar > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > I do not intend helping anyone who insists on top posting despite requests not to do so. When I was taught to read English it was always top to bottom and left to right. I do not recall this being changed, or have I missed something? -- Cheers. Mark Lawrence. From francois.dion at gmail.com Tue Oct 2 14:25:59 2012 From: francois.dion at gmail.com (Francois Dion) Date: Tue, 2 Oct 2012 08:25:59 -0400 Subject: [Tutor] Civil discourse from a newbie's perspective In-Reply-To: References: Message-ID: On Mon, Oct 1, 2012 at 9:34 PM, boB Stepp wrote: > However, I > think that extra care may be needed in dealing with newbies who may > only be taking a programming course using Python because of curriculum > requirements. A lot of questions seem to come from college students trying to get the best grade they can for their assignments and once done with their Python assignments might never go back to programming. Still, you are correct in your statement, we are building goodwill for Python and its community (user groups, forums, lists, conferences, teachers, students etc) through this list, and it can positively influence people to stick around (Python and CS) instead of abandoning the field of CS. Programming with Python is fun! Well, it is supposed to be fun. Communities for some other programming languages (not naming names) are known to be very harsh on newcomers, while Python is known to have a friendly community. We just have to continue mentoring and teaching with that in mind. Even worse would be the case of a young aficionado. It is very hard to infer the age of a person from a post. As Python is making inroads in the K-12 sector and through inexpensive computing platforms such as OLPC, the Raspberry Pi and the like, there is a potential to be interacting with some very young programmers. I think right now the bulk of them are sticking to forums (definitely the case with the Raspberry Pi), but it is inevitable that the makeup of the readership of this list will change. What will happen when a 9 year old kid who loves computers is turned away by a cutting remark or heavy criticism? Similarly, one should be considerate as to the language used. I'll conclude with King Solomon's proverbial saying: "As apples of gold in silver carvings is a word spoken at the right time for it" Thank you kindly, Francois -- Francois Dion solarisdesktop.blogspot.com - raspberry-python.blogspot.com From breamoreboy at yahoo.co.uk Tue Oct 2 14:27:42 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 02 Oct 2012 13:27:42 +0100 Subject: [Tutor] Civil discourse from a newbie's perspective In-Reply-To: References: Message-ID: On 02/10/2012 09:08, Alan Gauld wrote: > > I am glad you brought the subject up. I had been thinking of posting > something similar. There have been a few outbursts of late, and we have > seen some posts where the content was entirely aimed at improving the > format of the message rather than trying to address the Python aspects > of the question. The intent of this list is to provide a friendly and > encouraging place where beginners can ask for and receive help with > Python. Almost by definition beginners will not all know the preferred > internet posting idioms, especially since business standards encourage > many of the bad habits we prefer to avoid! Gentle reminders are valid > but they should be accompanied by help on the actual issue being raised > too! > > Regards, > May I suggest this or similar for your post http://netforbeginners.about.com/od/navigatingthenet/tp/top_10_search_engines_for_beginners.htm -- Cheers. Mark Lawrence. From robertvstepp at gmail.com Tue Oct 2 14:38:06 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 2 Oct 2012 07:38:06 -0500 Subject: [Tutor] Civil discourse from a newbie's perspective In-Reply-To: References: Message-ID: On Tue, Oct 2, 2012 at 7:25 AM, Francois Dion wrote: > Even worse would be the case of a young aficionado. It is very hard > to infer the age of a person from a post. As Python is making inroads > in the K-12 sector and through inexpensive computing platforms such as > OLPC, the Raspberry Pi and the like, there is a potential to be > interacting with some very young programmers. I think right now the > bulk of them are sticking to forums (definitely the case with the > Raspberry Pi), but it is inevitable that the makeup of the readership > of this list will change. What will happen when a 9 year old kid who > loves computers is turned away by a cutting remark or heavy criticism? > Similarly, one should be considerate as to the language used. > I am glad that you brought this point up. As some may recall from my first post, I mentioned that I was trying to encourage my 9-year old son (then 8 years old) to explore programming. As part of that effort I have set him up with his own PC with Internet access. We try to keep him well-supervised, but he is gaining in confidence in conducting searches for answers to his own questions. If he does ever "click" in his interest in Python, I could easily see him exploring this forum for answers. However, I would hate for him to be exposed to some of the "rhetoric" that has arisen here recently. On the other hand, I cannot perfectly protect him from everything and the information accessible from the Internet is of too much utility to forbid him its access. I will just prepare myself for some interesting questions in the near future! ~(:>)) -- Cheers! boB From zouzhberk at gmail.com Tue Oct 2 16:38:28 2012 From: zouzhberk at gmail.com (zouzhberk) Date: Tue, 02 Oct 2012 22:38:28 +0800 Subject: [Tutor] Tutor Digest, Vol 104, Issue 8 Message-ID: tutor-request at python.org??? >Send Tutor mailing list submissions to > tutor at python.org > >To subscribe or unsubscribe via the World Wide Web, visit > http://mail.python.org/mailman/listinfo/tutor >or, via email, send a message with subject or body 'help' to > tutor-request at python.org > >You can reach the person managing the list at > tutor-owner at python.org > >When replying, please edit your Subject line so it is more specific >than "Re: Contents of Tutor digest..." > > >Today's Topics: > > 1. Re: html checker (Alan Gauld) > 2. HELP! (Mark Rourke) > 3. python help? (patrick Howard) > > >---------------------------------------------------------------------- > >Message: 1 >Date: Tue, 02 Oct 2012 00:46:31 +0100 >From: Alan Gauld >To: tutor at python.org >Subject: Re: [Tutor] html checker >Message-ID: >Content-Type: text/plain; charset=ISO-8859-1; format=flowed > >On 01/10/12 20:59, Matthew Dalrymple wrote: > >> i don't really understand the pop and push or even stacks for that >> matter...the professor i have isn't really the best at teaching...so if >> anyone could give me a hand with any of this that would be appreciated > >The Raw materials topic in my tutor has a short intro to stacks. It >says, in part: >---------------- >Stack > >Think of a stack of trays in a restaurant. A member of staff puts a pile >of clean trays on top and these are removed one by one by customers. The >trays at the bottom of the stack get used last (and least!). Data stacks >work the same way: you push an item onto the stack or pop one off. The >item popped is always the last one pushed. This property of stacks is >sometimes called Last In First Out or LIFO. One useful property of >stacks is that you can reverse a list of items by pushing the list onto >the stack then popping it off again. The result will be the reverse of >the starting list. Stacks are not built in to Python, VBScript or >JavaScript. You have to write some program code to implement the >behavior. Lists are usually the best starting point since like stacks >they can grow as needed. >----------------- > >As c smith points out, Python lists have a pop/push mechanism as >standard which makes implementing a stack in Python fairly trivial. > >To expand on how reversing works consider pushing the string foo onto >the stack then popping it off again: > >s = 'foo' >stack = [] >stack.push(s[0]) # stack -> ['f'] >stack.push(s[1]) # stack -> ['o','f'] >stack.push(s[2]) # stack -> ['o','o','f'] > >c1 = stack.pop() # stack -> ['o','f'], c1 = 'o' >c2 = stack.pop() # stack -> ['f'], c1 = 'o', c2 = 'o' >c3 = stack.pop() # stack -> [], c1 = 'o', c2 = 'o', c3 = 'f' > >print c1+c2+c3 # prints 'oof' the reverse of s > >HTH, > >-- >Alan G >Author of the Learn to Program web site >http://www.alan-g.me.uk/ > > > >------------------------------ > >Message: 2 >Date: Sun, 30 Sep 2012 15:31:47 -0400 >From: Mark Rourke >To: tutor at python.org >Subject: [Tutor] HELP! >Message-ID: <50689E23.4050901 at gmail.com> >Content-Type: text/plain; charset="iso-8859-1"; Format="flowed" > >hello, I am a college student in my first year of computer programming, >I was wondering if you could look at my code to see whats wrong with it. > ># Mark Rourke ># Sept 29, 2012 ># Write a program to calculate the sales tax at the rate of 4% and 2% >respectively ># Thereafter compute the total sales tax (sum of the state tax and the >county tax) ># and the total purchase amount (sum of the purchase amount and the >total sales tax). ># Finally, display the amount of purchase, the state sales tax, the >county sales tax, >#the total sales tax and the total amount of the sale. > >#Variable Declarations >#Real purchaseAmount, stateSalesTax, countySalesTax, totalSalesTax, >totalPurchaseAmount >#Constant Real SALES_TAX = 0.4, COUNTY_TAX = 0.02 > >#Display "Input Purchase Amount: $" > >#input the hours worked and hourly wage wage > >SALES_TAX = 0.4 > >COUNTY_TAX = 0.02 >print("----------------------------------------------------------") >print(("This program calculates the sales tax at the rate of 4% and 2% >respectively, as well sum of the state tax")) >print("----------------------------------------------------------") > >purchaseAmount = input("Please input the Purchase Amount: $") > >#Calculate the State Sales Tax, County Sales Tax, Total Sales Tax, Total >Purchase Amount > >purchaseAmount = int(purchaseAmount) > >stateSalesTax = int(purchaseAmount * SALES_TAX) > >countySalesTax = int(purchaseAmount * COUNTY_TAX) > >totalSalesTax = int(stateSalesTax + countySalesTax) > >totalPurchaseAmount = int(purchaseAmount + totalSalesTax) > >#Output the results > >Display ("Purchase Amount:$") purchaseAmount >Display ("The State Sales Tax $") SALES_TAX >Display ("The County Sales Tax: $") COUNTY_TAX >Display ("The Total Sales Tax: $") totalSalesTax >Display ("The Total Amount of the Purchase: $") totalPurchaseAmount > >-- > >Mark Rourke > >T: 705-728-6169 >M: 705-331-0175 >E: Mark.Rourke7 at gmail.com > >-------------- next part -------------- >An HTML attachment was scrubbed... >URL: > >------------------------------ > >Message: 3 >Date: Sun, 30 Sep 2012 01:02:57 -0500 >From: patrick Howard >To: tutor at python.org >Subject: [Tutor] python help? >Message-ID: >Content-Type: text/plain; charset="windows-1252" > >I have to write a program that takes an input file, with students names and various grades. >My vindictive teacher also added grades that are not supposed to count, so I need to pick the grades that are 'HM1-4"; not HW, or TEST or anything else. >Then when these are listed in order with that persons name, sort the list alphabetically. I know that this is '.sort; >But, how do I get it to sort only names, AND keep the right grades with them. Below is a list of parameters? Can you help me please? > >-------------- next part -------------- >An HTML attachment was scrubbed... >URL: >-------------- next part -------------- >A non-text attachment was scrubbed... >Name: parameters.png >Type: image/png >Size: 111589 bytes >Desc: not available >URL: > >------------------------------ > >Subject: Digest Footer > >_______________________________________________ >Tutor maillist - Tutor at python.org >http://mail.python.org/mailman/listinfo/tutor > > >------------------------------ > >End of Tutor Digest, Vol 104, Issue 8 >************************************* From bgailer at gmail.com Tue Oct 2 17:10:04 2012 From: bgailer at gmail.com (bob gailer) Date: Tue, 02 Oct 2012 11:10:04 -0400 Subject: [Tutor] Thanks for all your comments regarding how we communicate. Message-ID: <506B03CC.7030505@gmail.com> Thanks for all your comments regarding how we communicate. I appreciate your support when someone responds negatively to my offerings of help. It is true that I can be "brusque" (which is an embarrassment for me as a teacher of Nonviolent Communication). Your encouragement to be welcoming and supportive is important to me and I will strive to be more so. I am often puzzled when I see folk asking for help with homework when it seems that the student is not in a class designed to help him learn Python (either by being in the wrong class, not meeting the prerequisites or having an instructor who cannot communicate clearly or give assignments that will get good results. I have taught in industry and in fast-path-masters )FPM) programs. I once had 5evenings to teach Pascal to FPM students. Their prerequisite class failed to do what it was supposed to do, and I got the blame for not being able to deliver fully my 5 5evenings' worth. I also find it frustrating when presented with a program that is a hodge-podge of things (e.g. functions) that came from various sources with no indication of who wrote what, and no sense that the programmer understands the various pieces. A couple of years ago a local graduate found me by searching and finding references to my participation on these lists. We negotiated a private tutoring agreement. On his first visit he presented a Python program which I assumed he had written. I could see that it was doing things the hard and inefficient way, so I coached him on how to make it better. Eventually I discovered that (1) he knew almost noting about Python; (2) the original program had been written by his advisor. (3) His advisor did not even know enough about Python to understand how inefficient the program was; (4) his advisor kept aiming at a moving target. Very frustrating for all of us. Once i understood his position I created a series of exercises to help me evaluate his knowledge and to help him learn. These exercises turned out to be at a beginner's tutorial level. Let us be compassionate; put our effort where it does the best; but continue asking for cooperation from the questioner. If I had more time I probably could make the above shorter. -- Bob Gailer 919-636-4239 Chapel Hill NC From bgailer at gmail.com Tue Oct 2 18:44:44 2012 From: bgailer at gmail.com (bob gailer) Date: Tue, 02 Oct 2012 12:44:44 -0400 Subject: [Tutor] How can we help? In-Reply-To: References: Message-ID: <506B19FC.9000308@gmail.com> On 10/2/2012 10:38 AM, zouzhberk wrote: > > tutor-request at python.org??? > >> Send Tutor mailing list submissions to >> tutor at python.org >> >> To subscribe or unsubscribe via the World Wide Web, visit >> http://mail.python.org/mailman/listinfo/tutor >> or, via email, send a message with subject or body 'help' to >> tutor-request at python.org >> >> You can reach the person managing the list at >> tutor-owner at python.org >> >> When replying, please edit your Subject line so it is more specific >> than "Re: Contents of Tutor digest..." >> rest of message deleted. Hi - your post was not very communicative. Did you want to ask or say something? Please note paragraph above starting with "When replying" and follow those guidelines. -- Bob Gailer 919-636-4239 Chapel Hill NC From katerina.sto at gmail.com Tue Oct 2 18:55:32 2012 From: katerina.sto at gmail.com (Katya Stolpovskaya) Date: Wed, 3 Oct 2012 00:55:32 +0800 Subject: [Tutor] a question about maxint Message-ID: Hi all, I have this error: >>> from sys import * >>> maxint Traceback (most recent call last): File "", line 1, in maxint NameError: name 'maxint' is not defined What does it mean and how to deal with it? Thank you in advance, Katya -- AKA XIAOJIA -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Oct 2 19:01:49 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 02 Oct 2012 18:01:49 +0100 Subject: [Tutor] Civil discourse from a newbie's perspective In-Reply-To: References: Message-ID: On 02/10/12 13:17, Mark Lawrence wrote: > I do not intend helping anyone who insists on top posting despite > requests not to do so. That is of course your prerogative, we can reply to, or ignore, whichever posts we choose. But we need to be realistic about top posting. It is the norm, and generally preferred in business thanks mainly to Microsoft outlooks default settings. But it is what people have become used to in the commercial world. That is inevitably moving into the internet community too. So we cannot judge too harshly if someone uses top posting, hopefully they will see the advantages of "context posting" (rather than bottom posting which can be almost as bad a top posting!) and of stripping excess verbiage. They may also observe how much easier it is to get useful answers if the follow the popular idioms! But we all have the choice to engage or ignore with anyone on the list just as we choose. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From __peter__ at web.de Tue Oct 2 19:17:52 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 02 Oct 2012 19:17:52 +0200 Subject: [Tutor] a question about maxint References: Message-ID: Katya Stolpovskaya wrote: > I have this error: > >>>> from sys import * >>>> maxint > Traceback (most recent call last): > File "", line 1, in > maxint > NameError: name 'maxint' is not defined > > > What does it mean and how to deal with it? You are probably using Python 3 which doesn't have sys.maxint. How to deal with that depends on you want to do with the value -- so what are you trying to do? (For most applications sys.maxsize which exists in Python 2 and 3 should work just as well) From breamoreboy at yahoo.co.uk Tue Oct 2 19:31:25 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 02 Oct 2012 18:31:25 +0100 Subject: [Tutor] a question about maxint In-Reply-To: References: Message-ID: On 02/10/2012 17:55, Katya Stolpovskaya wrote: > Hi all, > > I have this error: > >>>> from sys import * >>>> maxint > Traceback (most recent call last): > File "", line 1, in > maxint > NameError: name 'maxint' is not defined > > > What does it mean and how to deal with it? > > Thank you in advance, > > Katya > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > It means exactly what it says. Your version of Python doesn't have maxint defined in the built-in sys module. I'd hazard a guess that you're using a Python 3 version in which case you have maxsize defined instead of maxint. As a slight aside, using the import that way is usually considered bad style. -- Cheers. Mark Lawrence. From oberoc at gmail.com Tue Oct 2 19:44:47 2012 From: oberoc at gmail.com (Tino Dai) Date: Tue, 2 Oct 2012 13:44:47 -0400 Subject: [Tutor] getattr works sometimes Message-ID: Hi All, I'm using the get_class from: http://stackoverflow.com/questions/452969/does-python-have-an-equivalent-to-java-class-forname and the get_class class works sometime for finding modules within a certain directory. If the get_class doesn't work, it throws an AttributeError. The module exists in the directory, and I'm trying to debug this. Does anybody have any hints to go about debug this? -Tino From eryksun at gmail.com Tue Oct 2 19:53:25 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 2 Oct 2012 13:53:25 -0400 Subject: [Tutor] a question about maxint In-Reply-To: References: Message-ID: On Tue, Oct 2, 2012 at 12:55 PM, Katya Stolpovskaya wrote: > > I have this error: > >>>> from sys import * >>>> maxint > Traceback (most recent call last): > File "", line 1, in > maxint > NameError: name 'maxint' is not defined > > > What does it mean and how to deal with it? The "int" type in Python 3 is actually a "long", so there is no maxint. The machine limit that's still relevant in CPython is sys.maxsize (it's also in Python 2), which should be the maximum value of an ssize_t (signed size_t) on your platform. For example, this limits the maximum length of a list. It has to be signed because negative values are used in some cases to indicate errors. For example, list.sort() sets the "allocated" size to -1 in order to detect if the list has been mutated during the sort: http://hg.python.org/cpython/file/bd8afb90ebf2/Objects/listobject.c#l1932 http://hg.python.org/cpython/file/bd8afb90ebf2/Objects/listobject.c#l2042 Further reading: Using ssize_t as the index type http://www.python.org/dev/peps/pep-0353 http://en.wikipedia.org/wiki/C_data_types#Size_and_pointer_difference_types From oscar.j.benjamin at gmail.com Tue Oct 2 20:12:50 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 2 Oct 2012 19:12:50 +0100 Subject: [Tutor] getattr works sometimes In-Reply-To: References: Message-ID: On 2 October 2012 18:44, Tino Dai wrote: > Hi All, Hi Tino > > I'm using the get_class from: > > http://stackoverflow.com/questions/452969/does-python-have-an-equivalent-to-java-class-forname Can you show the relevant portion of your code please? > > and the get_class class works sometime for finding modules within a > certain directory. If the get_class > doesn't work, it throws an AttributeError. I don't really understand what you mean by this. Can you copy and paste the actual error message (all of it)? > > The module exists in the directory, and I'm trying to debug this. Does > anybody have any hints to go about debug > this? Not really as you haven't provided enough information. Oscar From oberoc at gmail.com Tue Oct 2 20:20:45 2012 From: oberoc at gmail.com (Tino Dai) Date: Tue, 2 Oct 2012 14:20:45 -0400 Subject: [Tutor] getattr works sometimes In-Reply-To: References: Message-ID: >> and the get_class class works sometime for finding modules within a >> certain directory. If the get_class >> doesn't work, it throws an AttributeError. > > I don't really understand what you mean by this. Can you copy and > paste the actual error message (all of it)? > >> >> The module exists in the directory, and I'm trying to debug this. Does >> anybody have any hints to go about debug >> this? > get_class('etl.transfers.bill_subject') # etl.transfers.bill_subject does exist under the transfers directory get_class('etl.transfers.related_bills') # Also exists under the transfer directory ERROR:root:'module' object has no attribute 'related_bills' Traceback (most recent call last): File "./leg_apps/etl/transfers/__init__.py", line 63, in get_class m = getattr(m, comp) AttributeError: 'module' object has no attribute 'related_bills' Out[15]: That's all I got for the stack trace (logged with exc_info=True) -Tino From oberoc at gmail.com Tue Oct 2 20:27:42 2012 From: oberoc at gmail.com (Tino Dai) Date: Tue, 2 Oct 2012 14:27:42 -0400 Subject: [Tutor] getattr works sometimes In-Reply-To: References: Message-ID: On Tue, Oct 2, 2012 at 2:20 PM, Tino Dai wrote: >>> and the get_class class works sometime for finding modules within a >>> certain directory. If the get_class >>> doesn't work, it throws an AttributeError. >> >> I don't really understand what you mean by this. Can you copy and >> paste the actual error message (all of it)? >> >>> >>> The module exists in the directory, and I'm trying to debug this. Does >>> anybody have any hints to go about debug >>> this? >> > > get_class('etl.transfers.bill_subject') # > etl.transfers.bill_subject does exist under the transfers directory > './leg_apps/etl/transfers/bill_subject.pyc'> > > get_class('etl.transfers.related_bills') # Also exists under the > transfer directory > ERROR:root:'module' object has no attribute 'related_bills' > Traceback (most recent call last): > File "./leg_apps/etl/transfers/__init__.py", line 63, in get_class > m = getattr(m, comp) > AttributeError: 'module' object has no attribute 'related_bills' > Out[15]: > > That's all I got for the stack trace (logged with exc_info=True) > > -Tino Correction, now neither example is working. :( -T From oscar.j.benjamin at gmail.com Tue Oct 2 20:32:03 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 2 Oct 2012 19:32:03 +0100 Subject: [Tutor] getattr works sometimes In-Reply-To: References: Message-ID: On 2 October 2012 19:27, Tino Dai wrote: > On Tue, Oct 2, 2012 at 2:20 PM, Tino Dai wrote: >>>> and the get_class class works sometime for finding modules within a >>>> certain directory. If the get_class >>>> doesn't work, it throws an AttributeError. >>> >>> I don't really understand what you mean by this. Can you copy and >>> paste the actual error message (all of it)? >>> >>>> >>>> The module exists in the directory, and I'm trying to debug this. Does >>>> anybody have any hints to go about debug >>>> this? >>> >> >> get_class('etl.transfers.bill_subject') # >> etl.transfers.bill_subject does exist under the transfers directory >> > './leg_apps/etl/transfers/bill_subject.pyc'> It shouldn't be returning a module. Is there a class in the bill_subject module that you wanted to get? What happens if you do: get_class('etl.transfers.bill_subject.BillSubject') where BillSubject is the name of the class in the module. Oscar From oscar.j.benjamin at gmail.com Tue Oct 2 20:59:26 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 2 Oct 2012 19:59:26 +0100 Subject: [Tutor] Help for Python Beginner with extracting and manipulating data from thousands of ASCII files In-Reply-To: References: Message-ID: Hi Cecilia, I'm sending this again as the first message was sent only to you (I hadn't realised that your own message was sent only to me as well). If you want to reply please reply-all to this message. On 1 October 2012 17:42, Cecilia Chavana-Bryant wrote: > On Mon, Oct 1, 2012 at 11:02 AM, Oscar Benjamin > wrote: >> >> On Sep 30, 2012 11:10 PM, "Cecilia Chavana-Bryant" >> wrote: >> > >> > fileDate = data[6][16:26] # location of the creation date on the data >> > files >> >> What format does fileDate have? I guess it's a string of text from the file. If >> you can convert it to a datetime (or date) object it will be easy to >> compare with the dates as required for your calibration file. Can you >> show us how it looks e.g. >> >> '12-Nov-2012' >> or >> '12/11/12' >> or something else? > > > Date is in the following format: dd/mm/yyyy The standard way to work with dates is to turn the date strings into Python datetime objects. You can read about those here: http://docs.python.org/library/datetime.html datetime objects can be create directly: >>> from datetime import datetime >>> start_date = datetime(year=2012, month=11, day=3) >>> print start_date 2012-11-03 00:00:00 You can also create them from a string: >>> datestring = '10/11/2012' >>> experiment_date = datetime.strptime(datestring, '%d/%m/%Y') >>> print experiment_date 2012-11-10 00:00:00 Once you have two datetime objects you can compare them directly: >>> experiment_date > start_date True >>> print experiment_date - start_date 7 days, 0:00:00 Using this you can check whether the date from the data file is in between the start and end dates for each of the calibration files and then choose which calibration file to use. Oscar From bfishbein79 at gmail.com Tue Oct 2 23:27:15 2012 From: bfishbein79 at gmail.com (Benjamin Fishbein) Date: Tue, 2 Oct 2012 16:27:15 -0500 Subject: [Tutor] posting with urllib2 Message-ID: <2EC5F26E-5558-4EE5-BB09-1758336172D7@gmail.com> Hi. I'm really confused about which data I need to put in for posting something with urllib2. I added the action on to the website. I know that I use that for the URL parameter. But what do I need to put in dicts to pass along data? And what is the purpose of the third parameter...the delay?? Thanks Ben

Enter UPC here:




start over
-------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Wed Oct 3 00:17:57 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 2 Oct 2012 18:17:57 -0400 Subject: [Tutor] posting with urllib2 In-Reply-To: <2EC5F26E-5558-4EE5-BB09-1758336172D7@gmail.com> References: <2EC5F26E-5558-4EE5-BB09-1758336172D7@gmail.com> Message-ID: On Tue, Oct 2, 2012 at 5:27 PM, Benjamin Fishbein wrote: > Hi. I'm really confused about which data I need to put in for posting > something with urllib2. > > I added the action on to the website. I know that I use that for the URL > parameter. But what do I need to put in dicts to pass along data? And what > is the purpose of the third parameter...the delay?? > I think using requests is simpler. check it out at http://docs.python-requests.org/en/latest/ -- Joel Goldstick From akleider at sonic.net Wed Oct 3 00:19:48 2012 From: akleider at sonic.net (akleider at sonic.net) Date: Tue, 2 Oct 2012 15:19:48 -0700 Subject: [Tutor] "ImportError: _strptime not supported" Message-ID: <52b2619789ca19c592ccd5e44631b3a5.squirrel@webmail.sonic.net> The following code was recently suggested as an example of how the datetime module could be used to solve a problem. Not having access to Python at work, I found http://pythontutor.com/visualize.html thinking it would allow me to "play with Python" when I have a free moment. from datetime import datetime start_date = datetime(year=2012, month=11, day=3) print(start_date) datestring = '10/11/2012' experiment_date = datetime.strftime(datestring, '%d/%m/%Y') print(experiment_date) if experiment_date > start_date: print("Experiment_date comes after start_date.") else: print("Expriment_date does not come after start_date.") Does not complete because of: "ImportError: _strptime not supported" At first I thought perhaps strptime was a Python3 feature but both v2.7 and v3.2 result in the same ImportError. Is the problem with http://pythontutor.com/visualize.html ? Also, the leading underscore in the error message puzzles me. According to http://docs.python.org/library/datetime.html """classmethod datetime.strptime(date_string, format)""" strptime should work. From marc.tompkins at gmail.com Wed Oct 3 00:33:15 2012 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 2 Oct 2012 15:33:15 -0700 Subject: [Tutor] Fwd: "ImportError: _strptime not supported" In-Reply-To: References: <52b2619789ca19c592ccd5e44631b3a5.squirrel@webmail.sonic.net> Message-ID: Forgot to send to the list. Grrr. ---------- Forwarded message ---------- From: Marc Tompkins Date: Tue, Oct 2, 2012 at 3:32 PM Subject: Re: [Tutor] "ImportError: _strptime not supported" To: akleider at sonic.net On Tue, Oct 2, 2012 at 3:19 PM, wrote: > > The following code was recently suggested as an example of how the > datetime module could be used to solve a problem. Not having access to > Python at work, I found > http://pythontutor.com/visualize.html > thinking it would allow me to "play with Python" when I have a free moment. > > from datetime import datetime > start_date = datetime(year=2012, month=11, day=3) > print(start_date) > > datestring = '10/11/2012' > experiment_date = datetime.strftime(datestring, '%d/%m/%Y') > print(experiment_date) > > if experiment_date > start_date: > print("Experiment_date comes after start_date.") > else: > print("Expriment_date does not come after start_date.") > > Does not complete because of: > > "ImportError: _strptime not supported" > > At first I thought perhaps strptime was a Python3 feature but both v2.7 > and v3.2 result in the same ImportError. > Is the problem with http://pythontutor.com/visualize.html > ? > > Also, the leading underscore in the error message puzzles me. > > According to > http://docs.python.org/library/datetime.html > """classmethod datetime.strptime(date_string, format)""" > strptime should work. I went to visualize, and pasted in your code; I don't get that ImportError. Instead, I get > TypeError: descriptor 'strftime' requires a 'datetime.date' object but > received a 'str' on this line: > experiment_date = datetime.strftime(datestring, '%d/%m/%Y') I really have no idea where your ImportError came from, especially since the code you've posted doesn't explicitly call strptime. I suspect that, yes, it was a problem on the "visualize" site, and that they've cleared it up now. -------------- next part -------------- An HTML attachment was scrubbed... URL: From robertvstepp at gmail.com Wed Oct 3 01:25:36 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 2 Oct 2012 18:25:36 -0500 Subject: [Tutor] Civil discourse from a newbie's perspective In-Reply-To: References: Message-ID: Leam, On Tue, Oct 2, 2012 at 4:04 AM, leam hall wrote: > > Does the welcome e-mail cover any of the recommended behavior? Are there > easier ways to request participation within guidelines? > Not only does the welcome message cover this, apparently when I posted my message that started this thread I received a copy automatically of the same message, repackaged: "Auto-response for your message to the "Tutor" mailing list Python/tutor at python.org x tutor-bounces at python.org 8:34 PM (21 hours ago) to me Your message for tutor at python.org, the Python programming tutor list, has been received and is being delivered. This automated response is sent to those of you new to the Tutor list, to point out a few resources that can help with answering your own questions, or improve the chances of getting a useful answer from the other subscribers. [...]" This is the beginning of exactly the same message I received when I first joined this mailing list. Honestly, newbie or not, I do not understand why many of the posts from newcomers so routinely violate the contents of this welcome message. I know I am trying to abide by them. Laziness? Difficulties with English comprehension? Sheer stubbornness? Or something more innocent? I have no clue. But fellow newbies: Please (!!!) read the contents of the welcome message. It spells out in great detail the expectations for posting here and might help you get your questions answered much more quickly and efficiently! Cheers! boB Stepp From oscar.j.benjamin at gmail.com Wed Oct 3 01:32:29 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 3 Oct 2012 00:32:29 +0100 Subject: [Tutor] "ImportError: _strptime not supported" In-Reply-To: <52b2619789ca19c592ccd5e44631b3a5.squirrel@webmail.sonic.net> References: <52b2619789ca19c592ccd5e44631b3a5.squirrel@webmail.sonic.net> Message-ID: On 2 October 2012 23:19, wrote: > The following code was recently suggested as an example of how the > datetime module could be used to solve a problem. Not having access to > Python at work, I found > http://pythontutor.com/visualize.html > thinking it would allow me to "play with Python" when I have a free moment. There are a few websites like this. You have to bear in mind that they usually disable some functionality for security. I would expect the datetime module to work though. > > from datetime import datetime > start_date = datetime(year=2012, month=11, day=3) > print(start_date) > > datestring = '10/11/2012' > experiment_date = datetime.strftime(datestring, '%d/%m/%Y') The example I posted uses strptime not strftime. Note the 'p' in the middle instead of the 'f'. These two functions are opposites: strptime turns string objects into datetime objects and strftime does the inverse. Yes, it is silly to have functions with such similar names that are hard to distinguish visually. Unfortunately Python inherited these names from the C programming language where it was more difficult to use good names for functions. > print(experiment_date) > > if experiment_date > start_date: > print("Experiment_date comes after start_date.") > else: > print("Expriment_date does not come after start_date.") Otherwise you've got the right idea. Although I think for the original problem it should be: if experiment_date >= start_date: Note the '>=' instead of '>'. Oscar From eryksun at gmail.com Wed Oct 3 01:37:04 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 2 Oct 2012 19:37:04 -0400 Subject: [Tutor] "ImportError: _strptime not supported" In-Reply-To: <52b2619789ca19c592ccd5e44631b3a5.squirrel@webmail.sonic.net> References: <52b2619789ca19c592ccd5e44631b3a5.squirrel@webmail.sonic.net> Message-ID: On Tue, Oct 2, 2012 at 6:19 PM, wrote: > > from datetime import datetime > start_date = datetime(year=2012, month=11, day=3) > print(start_date) > > datestring = '10/11/2012' > experiment_date = datetime.strftime(datestring, '%d/%m/%Y') > print(experiment_date) > > if experiment_date > start_date: > print("Experiment_date comes after start_date.") > else: > print("Expriment_date does not come after start_date.") > > Does not complete because of: > > "ImportError: _strptime not supported" That error is surely some quirk of pythontutor.com. datetime.strftime calls time.strftime. On the other hand, datetime.strptime does import _strptime: http://hg.python.org/cpython/file/70274d53c1dd/Modules/datetimemodule.c#l3937 Did you actually use datetime.strptime? Normally you'd use strftime as method call on a datetime object: >>> from datetime import datetime >>> datestring = '10/11/2012' >>> dt = datetime.strptime(datestring, '%d/%m/%Y') >>> dt datetime.datetime(2012, 11, 10, 0, 0) >>> dt.strftime('%d/%m/%Y') '10/11/2012' From steve at pearwood.info Wed Oct 3 02:19:24 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 03 Oct 2012 10:19:24 +1000 Subject: [Tutor] getattr works sometimes In-Reply-To: References: Message-ID: <506B848C.1000306@pearwood.info> On 03/10/12 03:44, Tino Dai wrote: > Hi All, > > I'm using the get_class from: > > http://stackoverflow.com/questions/452969/does-python-have-an-equivalent-to-java-class-forname Do you mean this function? def get_class( kls ): parts = kls.split('.') module = ".".join(parts[:-1]) m = __import__( module ) for comp in parts[1:]: m = getattr(m, comp) return m At only seven lines, it is perfectly acceptable to copy it into your email for the benefit of those who are blocked from accessing the web but still have access to email. Of course it is good to give credit to the original source as well. The name is misleading, because it does not just get classes, it gets any object you like: py> get_class("math.pi") 3.141592653589793 > and the get_class class works sometime for finding modules within a > certain directory. The get_class function will find modules anywhere that they would be found by the import statement, that is, in the Python module import search path (sys.path). > If the get_class doesn't work, it throws an AttributeError. If the module does not exist, or cannot be found, get_class will raise ImportError. If the module is successfully found, but the dotted name does not exist, get_class will raise AttributeError. > The module exists in the directory, and I'm trying to debug this. Does > anybody have any hints to go about debug this? The usual advise: read the error message, it tells you what went wrong: py> get_class("datetime.datime") Traceback (most recent call last): File "", line 1, in File "", line 6, in get_class AttributeError: 'module' object has no attribute 'datime' I typed the name wrong. It's "datetime", not "datime". py> get_class("datetime.datetime") -- Steven From steve at pearwood.info Wed Oct 3 02:39:44 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 03 Oct 2012 10:39:44 +1000 Subject: [Tutor] getattr works sometimes In-Reply-To: References: Message-ID: <506B8950.1010603@pearwood.info> On 03/10/12 04:20, Tino Dai wrote: >>> and the get_class class works sometime for finding modules within a >>> certain directory. If the get_class >>> doesn't work, it throws an AttributeError. >> >> I don't really understand what you mean by this. Can you copy and >> paste the actual error message (all of it)? >> >>> >>> The module exists in the directory, and I'm trying to debug this. Does >>> anybody have any hints to go about debug >>> this? >> > > get_class('etl.transfers.bill_subject') # > etl.transfers.bill_subject does exist under the transfers directory > './leg_apps/etl/transfers/bill_subject.pyc'> I don't understand why you are using the get_class function for this. It is only useful when you don't know the name of the object until runtime. Since you know the name, I would suggest that using a regular import is better: from etl.transfers import bill_subject Imports also work differently to getattr, and I believe that is where you are running into trouble. The (wrongly named) get_class function uses getattr to extract names from a single top-level module. But that's not what you are trying to do: you are trying to extract modules from a package. Use the right tool for the job: you are trying to use a screwdriver when you need a screwdriver. My prediction is that *at some point* in your experiments, you have done something like: etl.transfers.bill_subject = bill_subject *or something with the same effect*. Perhaps the "transfers" module includes one of these lines: import bill_subject # Python 2.5 or older? from . import bill_subject Now bill_subject is an attribute of the transfers module, and getattr can see it. But you haven't done the same for related_bills, and so getattr cannot see it. Instead, use the right tool, import, which is designed for solving your problem: from etl.transfers import bill_subject from etl.transfers import related_bills should both work, unless you have removed the bill_subject.py and related_bills.py files from the etl/transfers/ directory. -- Steven From robertvstepp at gmail.com Wed Oct 3 03:50:01 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 2 Oct 2012 20:50:01 -0500 Subject: [Tutor] Civil discourse from a newbie's perspective In-Reply-To: References: Message-ID: On Tue, Oct 2, 2012 at 6:25 PM, boB Stepp wrote: > This is the beginning of exactly the same message I received when I > first joined this mailing list. Honestly, newbie or not, I do not > understand why many of the posts from newcomers so routinely violate > the contents of this welcome message. I know I am trying to abide by > them. Laziness? Difficulties with English comprehension? Sheer > stubbornness? Or something more innocent? I have no clue. But fellow > newbies: Please (!!!) read the contents of the welcome message. It > spells out in great detail the expectations for posting here and might > help you get your questions answered much more quickly and > efficiently! > On my way to Walmart and back it occurred to me that what I wrote above is somewhat harsh. I did not mean it to be so! However, it also occurred to me that maybe what I have received and am receiving is not reaching all newcomers to this list. In fact, it is NOT the welcome letter that contains the rules for good posting and tips on answering one's own questions, but an auto-generated email in response to a newcomer's first few posts. Instead of posting a copy of the email sent to me, see http://code.activestate.com/lists/python-tutor/88777/ , which appears to contain the full text of what I have received. I hope it is helpful to anyone who has not received it for some reason or other. -- Cheers! boB From steve at pearwood.info Wed Oct 3 04:01:54 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 03 Oct 2012 12:01:54 +1000 Subject: [Tutor] Civil discourse from a newbie's perspective In-Reply-To: References: Message-ID: <506B9C92.8070309@pearwood.info> On 02/10/12 22:25, Francois Dion wrote: > Even worse would be the case of a young aficionado. It is very hard > to infer the age of a person from a post. Obligatory bash.org quote: http://bash.org/?14207 For those who don't know bash.org, it contains extracts of IRC and other chat logs. Many of them are rude, or lame, but some are hilarious. -- Steven From robertvstepp at gmail.com Wed Oct 3 05:15:02 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 2 Oct 2012 22:15:02 -0500 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? Message-ID: After much diddling around I have finally settled on a text to study (Programming in Python 3, 2nd edition, by Mark Summerfield) and have defaulted to using IDLE, deferring worrying about editors/IDEs until I feel comfortable in Python. I am puzzled by the results of the following: >>> x = "Test" >>> x 'Test' >>> print(x) Test I understand that 'Test' is the stored value in memory where the single quotes designate the value as being a string data type. So it makes sense to me that just typing the object reference for the string results in including the single quotes. But why does the print() strip the quotes off? Is just as simple as normally people when performing a print just want the unadorned text, so that is the behavior built into the print function? Or is there something more subtle going on that I am totally missing? If an explanation is in one of my several books, it is currently eluding me. -- Cheers! boB From D.Wilder at F5.com Wed Oct 3 05:14:16 2012 From: D.Wilder at F5.com (Dave Wilder) Date: Wed, 3 Oct 2012 03:14:16 +0000 Subject: [Tutor] Filename match on file extensions Message-ID: Hello, Below is a snippet of a search I want to do for any file that contains the string "quarantine" in the filename. It works for picking up any file name containing "quarantine" except when "quarantine" is used as an extension. For example, the search would find quarantine.txt but fails to find txt.quarantine. I have done some investigating, but have Not been able to determine how to make it include extensions in the search. I believe I should be using something like "os.path.splitext". Does that make sense? Thanks. target = '/home' matches = 0 for (currdir,subdirs,files) in os.walk(target): for i in files: if i.lower().find("quarantine".lower()) == 0: ## Case-insensitive search for string "quarantine" in filename matches += 1 # increment number of matches print "Quarantine file",i,"found in directory",currdir if matches > 0: result = tc.FAIL else: result = 'FAIL' -------------- next part -------------- An HTML attachment was scrubbed... URL: From illusiontechniques at gmail.com Wed Oct 3 05:37:12 2012 From: illusiontechniques at gmail.com (c smith) Date: Tue, 2 Oct 2012 23:37:12 -0400 Subject: [Tutor] Filename match on file extensions In-Reply-To: References: Message-ID: Would this be a time when regex is necessary? Maybe: \b[^.]*quarantine[^.]*\.[a-zA-Z]*\b -------------- next part -------------- An HTML attachment was scrubbed... URL: From D.Wilder at F5.com Wed Oct 3 05:45:52 2012 From: D.Wilder at F5.com (Dave Wilder) Date: Wed, 3 Oct 2012 03:45:52 +0000 Subject: [Tutor] Filename match on file extensions In-Reply-To: References: Message-ID: >> Hello, >> Below is a snippet of a search I want to do for any file that contains the string "quarantine" in the filename. >> It works for picking up any file name containing "quarantine" except when "quarantine" is used as an extension. >> For example, the search would find quarantine.txt but fails to find txt.quarantine. I have done some investigating, but have >> Not been able to determine how to make it include extensions in the search. >> I believe I should be using something like "os.path.splitext". Does that make sense? Thanks. >> target = '/home' >> matches = 0 >> for (currdir,subdirs,files) in os.walk(target): >> for i in files: >> if i.lower().find("quarantine".lower()) == 0: ## Case-insensitive search for string "quarantine" in filename >> matches += 1 # increment number of matches >> print "Quarantine file",i,"found in directory",currdir >>if matches > 0: >> result = tc.FAIL >>else: >> result = 'FAIL' > Would this be a time when regex is necessary? Maybe: \b[^.]*quarantine[^.]*\.[a-zA-Z]*\b I had originally used regular expression, but thought there might be a simpler solution w/o the need for regular expressions. If that is what is needed, then so be it though. Thank you for your quick response. -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Wed Oct 3 06:53:08 2012 From: d at davea.name (Dave Angel) Date: Wed, 03 Oct 2012 00:53:08 -0400 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: References: Message-ID: <506BC4B4.3070203@davea.name> On 10/02/2012 11:15 PM, boB Stepp wrote: > After much diddling around I have finally settled on a text to study > (Programming in Python 3, 2nd edition, by Mark Summerfield) and have > defaulted to using IDLE, deferring worrying about editors/IDEs until I > feel comfortable in Python. > > I am puzzled by the results of the following: > >>>> x = "Test" >>>> x > 'Test' >>>> print(x) > Test > > I understand that 'Test' is the stored value in memory where the > single quotes designate the value as being a string data type. So it > makes sense to me that just typing the object reference for the string > results in including the single quotes. But why does the print() strip > the quotes off? Is just as simple as normally people when performing a > print just want the unadorned text, so that is the behavior built into > the print function? Or is there something more subtle going on that I > am totally missing? If an explanation is in one of my several books, > it is currently eluding me. > There are two operations supported by (most) objects that produce a string. One is exemplified by the str() function, which converts an object to a string. That's the one called implicitly by print(). This form just represents the data, in the form most likely to be needed by the end user. The other operation is repr(), which attempts to produce a string that could be used in a program to reproduce the actual object. So a repr() will have quote marks artificially added, or brackets, or commas, or whatever seems appropriate for the particular object. This is intended for the programmer's use, not for the end user. When you program x = "Test", the string object that is created does not have quote marks in it anywhere. It also doesn't care whether you produced it by single quotes, double quotes, triple quotes, or by some manipulation of one or more other objects. It has 4 characters in it. Period. If you take that same string and do a repr() on it, it will produce another string that does have some form of quotes, though not necessarily the ones used originally. In the interactive interpreter (I've never used IDLE), entering in an expression without assigning it to anything will cause the result of the expression to be displayed with repr(). Your question was about string objects, but I tried to make the explanation as generic as possible. Those two functions, str() and repr(), are used, or implied in many places. For example, if you print a list, it'll call str() on the whole list. But the list object's logic will in turn call repr() on each of its elements, and put the whole thing together with braces and commas. (Finer detail: There are special methods in the class for each object, __str__() and __repr__(), which actually have the code. But you should never call them directly, so you won't need to know about them till you start building your own classes) -- DaveA From brian.van.den.broek at gmail.com Wed Oct 3 07:00:30 2012 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Wed, 3 Oct 2012 01:00:30 -0400 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: References: Message-ID: On 2 Oct 2012 23:17, "boB Stepp" wrote: > I am puzzled by the results of the following: > > >>> x = "Test" > >>> x > 'Test' > >>> print(x) > Test > > I understand that 'Test' is the stored value in memory where the > single quotes designate the value as being a string data type. So it > makes sense to me that just typing the object reference for the string > results in including the single quotes. But why does the print() strip > the quotes off? Is just as simple as Hi boB, Under the covers, in python 2.x, print x causes the human readable string representation of x to be output by calling x.__str__. In an interactive prompt, typing x displays the python representation of x by calling x.__repr__. These can be the same or quite similar or quite different. When possible, __repr__ special methods ought to be defined so x equals eval(x.__repr__()). I believe, but don't warrant that in this regard python 3.x behave like 2.x (modulo the difference in the print syntax). Best, Brian vdB -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Wed Oct 3 07:09:08 2012 From: d at davea.name (Dave Angel) Date: Wed, 03 Oct 2012 01:09:08 -0400 Subject: [Tutor] Filename match on file extensions In-Reply-To: References: Message-ID: <506BC874.1020106@davea.name> On 10/02/2012 11:45 PM, Dave Wilder wrote: > >>> Hello, >>> Below is a snippet of a search I want to do for any file that contains the string "quarantine" in the filename. >>> It works for picking up any file name containing "quarantine" except when "quarantine" is used as an extension. >>> For example, the search would find quarantine.txt but fails to find txt.quarantine. I have done some investigating, but have >>> Not been able to determine how to make it include extensions in the search. >>> I believe I should be using something like "os.path.splitext". Does that make sense? Thanks. >>> target = '/home' >>> matches = 0 >>> for (currdir,subdirs,files) in os.walk(target): >>> for i in files: >>> if i.lower().find("quarantine".lower()) == 0: ## Case-insensitive search for string "quarantine" in filename >>> matches += 1 # increment number of matches >>> print "Quarantine file",i,"found in directory",currdir >>> if matches > 0: >>> result = tc.FAIL >>> else: >>> result = 'FAIL' > >> Would this be a time when regex is necessary? Maybe: \b[^.]*quarantine[^.]*\.[a-zA-Z]*\b > I had originally used regular expression, but thought there might be a simpler solution w/o the need for regular expressions. > > If that is what is needed, then so be it though. Thank you for your quick response. A regex cannot possibly help. Please don't make that mistake. Have you actually displayed the names it claims do not contain the quarantine string? In other words, put an else clause in? Your problem/solution is elsewhere, not in splitext, and not regex. Start by looking up the doc for the index method. It returns -1 for failure, not 0. You're explicitly checking for a string STARTing with 'quarantine'. So it'll skip files like xxxquarantine.txt as well as txt.quarantine >From your description, you should just be using 'in' operator. if "quarantine" in i.lower(): -- DaveA From steve at pearwood.info Wed Oct 3 07:49:07 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 3 Oct 2012 15:49:07 +1000 Subject: [Tutor] Filename match on file extensions In-Reply-To: References: Message-ID: <20121003054907.GA27111@ando> On Wed, Oct 03, 2012 at 03:14:16AM +0000, Dave Wilder wrote: > > Hello, > > Below is a snippet of a search I want to do for any file that contains the string "quarantine" in the filename. > It works for picking up any file name containing "quarantine" except when "quarantine" is used as an extension. Incorrect. It only picks up filenames that *begin* with "quarantine". The critical code is: i.lower().find("quarantine".lower()) == 0 which matches: quarantineblahblahblah.txt but not blahblahquarantineblah.txt The correct test for matching anywhere in the string is: "quarantine" in i.lower() By the way, it is misleading to call your loop variable "i". By convention, i is used for integer loop variables. Since your loop variable in this case is a file name, I would name it "filename": for filename in files: if "quarantine" in filename.lower(): ... Also, you might consider using the "glob" module, which is good for finding filenames matching so-called globs: "quarantine*.txt" matches file names starting with quarantine, ending with .txt, and anything at all in between. -- Steven From D.Wilder at F5.com Wed Oct 3 08:07:55 2012 From: D.Wilder at F5.com (Dave Wilder) Date: Wed, 3 Oct 2012 06:07:55 +0000 Subject: [Tutor] Filename match on file extensions In-Reply-To: <20121003054907.GA27111@ando> References: <20121003054907.GA27111@ando> Message-ID: Awesome! Thank you for the great info gentlemen. It should be an easy fix from here. -----Original Message----- From: Tutor [mailto:tutor-bounces+d.wilder=f5.com at python.org] On Behalf Of Steven D'Aprano Sent: Wednesday, October 03, 2012 1:49 AM To: tutor at python.org Subject: Re: [Tutor] Filename match on file extensions On Wed, Oct 03, 2012 at 03:14:16AM +0000, Dave Wilder wrote: > > Hello, > > Below is a snippet of a search I want to do for any file that contains the string "quarantine" in the filename. > It works for picking up any file name containing "quarantine" except when "quarantine" is used as an extension. Incorrect. It only picks up filenames that *begin* with "quarantine". The critical code is: i.lower().find("quarantine".lower()) == 0 which matches: quarantineblahblahblah.txt but not blahblahquarantineblah.txt The correct test for matching anywhere in the string is: "quarantine" in i.lower() By the way, it is misleading to call your loop variable "i". By convention, i is used for integer loop variables. Since your loop variable in this case is a file name, I would name it "filename": for filename in files: if "quarantine" in filename.lower(): ... Also, you might consider using the "glob" module, which is good for finding filenames matching so-called globs: "quarantine*.txt" matches file names starting with quarantine, ending with .txt, and anything at all in between. -- Steven _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From steve at pearwood.info Wed Oct 3 08:38:43 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 3 Oct 2012 16:38:43 +1000 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: References: Message-ID: <20121003063843.GB27111@ando> On Tue, Oct 02, 2012 at 10:15:02PM -0500, boB Stepp wrote: > I am puzzled by the results of the following: > > >>> x = "Test" > >>> x > 'Test' > >>> print(x) > Test > > I understand that 'Test' is the stored value in memory where the > single quotes designate the value as being a string data type. [...] > But why does the print() strip > the quotes off? Is just as simple as normally people when performing a > print just want the unadorned text, so that is the behavior built into > the print function? The short answer is, yes. The long answer is a bit more subtle, and rather long. I believe that you're thinking at too low a level. Or at least writing at too low a level. Forget about "values in memory" and "object reference", and just think about "objects". An object is a blob of data and code that operates on that data. It's a thing, much like the things in real life (cups, chairs, dogs) which carry state (data: the cup is full, or empty) and behaviour (dogs bark). In this case, the type of object you have is a string, and that leads you to your other bit of confusion: if sentences are made up of words, how do we distinguish words that are part of the sentence structure from words being used as the object or subject of the sentence? We write the word in quotation marks! E.g.: The above line contains the word "write". In this case, "write" is not part of the structure of the sentence, it is the subject of the sentence. Or possibly the object. My knowledge of English grammatical terms is a bit lacking, sorry. In either case, it is the *data* that the rest of the sentence operates on. Python is no different: words, text if you will, that are part of the code are written as normal: # source code class Test: pass x = Test # Test here refers to the variable Test, a class But to create a string object, you use quotation marks to tell Python that this is data, not code, please create a string object: x = "Test" # Test here refers to a string, which is data Notice that the quotation marks are *delimiters*, they mark the start and end of the string, but aren't part of the string in any way. Python knows that the object is a string because you put it in string delimiters, but the delimiters are not part of the string. Now, take a step back and consider objects in general. There are two things we might like to do to an arbitrary object: * display the object, which implicitly means turning it into a string, or at least getting some representation of that object as a string; * convert the object into a string. Python has two built-in functions for that: * repr, which takes any object and returns a string that represents that object; * str, which tries to convert an object into a string, if that makes sense. Often those will do the same thing. For example: py> str(42) == repr(42) == "42" True But not always. For example: py> from decimal import Decimal as D py> x = D("1.23") py> print(str(x)) 1.23 py> print(repr(x)) Decimal('1.23') Unfortunately, the difference between str() and repr() is kind of arbitrary and depends on the object. str() is supposed to return a "human-readable" version of the object, for display, while repr() is supposed to return a string which would work as code, but those are more guidelines than hard rules. So we have two different ways of converting an object to a string. But strings themselves are objects too. What happens there? py> s = "Hello world" # remember the quotes are delimiters, not part of the string py> print(str(s)) Hello world py> print(repr(s)) 'Hello world' str() of a string is unchanged (and why shouldn't it be? it's already a string, there's nothing to convert). But repr() of a string creates a new string showing the representation of the original string, that is, what you would need to type in source code to make that string. That means: 1) wrap the whole thing in delimiters (quotation marks) 2) escaping special characters like tabs, newlines, and binary characters. Notice that the string returned by repr() includes quote marks as part of the new string. Given the s above: py> t = repr(s) py> print(t) 'Hello world' py> t "'Hello world'" This tells us that the new string t includes single quote marks as the first and last character, so when you print it, the single quote marks are included in the output. But when you just display t interactively (see below), the delimiters are shown. Now, at the interactive interpreter, evaluating an object on its own without saving the result anywhere displays the repr() to the screen. Why repr()? Well, why not? The decision was somewhat arbitrary. print, on the other hand, displays the str() of the object directly to the screen. For strings, that means the delimiters are not shown, because they are not part of the string itself. Why str() rather than repr()? Because that's what people mostly want, and if you want the other, you can just say print(repr(obj)). Does this help, or are you more confused than ever? :-) -- Steven From cecilia.chavana at gmail.com Wed Oct 3 11:16:19 2012 From: cecilia.chavana at gmail.com (Cecilia Chavana-Bryant) Date: Wed, 3 Oct 2012 10:16:19 +0100 Subject: [Tutor] Help for Python Beginner with extracting and manipulating data from thousands of ASCII files In-Reply-To: References: Message-ID: Got it, many thanks for your help. On Tue, Oct 2, 2012 at 7:59 PM, Oscar Benjamin wrote: > Hi Cecilia, I'm sending this again as the first message was sent only > to you (I hadn't realised that your own message was sent only to me as > well). If you want to reply please reply-all to this message. > > On 1 October 2012 17:42, Cecilia Chavana-Bryant > wrote: > > On Mon, Oct 1, 2012 at 11:02 AM, Oscar Benjamin < > oscar.j.benjamin at gmail.com> > > wrote: > >> > >> On Sep 30, 2012 11:10 PM, "Cecilia Chavana-Bryant" > >> wrote: > >> > > >> > fileDate = data[6][16:26] # location of the creation date on the data > >> > files > >> > >> What format does fileDate have? I guess it's a string of text from the > file. If > >> you can convert it to a datetime (or date) object it will be easy to > >> compare with the dates as required for your calibration file. Can you > >> show us how it looks e.g. > >> > >> '12-Nov-2012' > >> or > >> '12/11/12' > >> or something else? > > > > > > Date is in the following format: dd/mm/yyyy > > The standard way to work with dates is to turn the date strings into > Python datetime objects. You can read about those here: > http://docs.python.org/library/datetime.html > > datetime objects can be create directly: > > >>> from datetime import datetime > >>> start_date = datetime(year=2012, month=11, day=3) > >>> print start_date > 2012-11-03 00:00:00 > > You can also create them from a string: > > >>> datestring = '10/11/2012' > >>> experiment_date = datetime.strptime(datestring, '%d/%m/%Y') > >>> print experiment_date > 2012-11-10 00:00:00 > > Once you have two datetime objects you can compare them directly: > > >>> experiment_date > start_date > True > >>> print experiment_date - start_date > 7 days, 0:00:00 > > Using this you can check whether the date from the data file is in > between the start and end dates for each of the calibration files and > then choose which calibration file to use. > > > Oscar > -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Wed Oct 3 13:33:50 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 3 Oct 2012 07:33:50 -0400 Subject: [Tutor] a question about maxint In-Reply-To: References: Message-ID: On Wed, Oct 3, 2012 at 1:28 AM, Katya Stolpovskaya wrote: > > Thank you for you reply, but with "long" I got the same error: > >>>> from sys import * >>>> long > > Traceback (most recent call last): > File "", line 1, in > long > NameError: name 'long' is not defined I assumed some familiarity with Python 2, which has two integer arithmetic types: int (fixed precision, using the platform's C long type) and long (arbitrary precision). The naming is a bit conflated in that a Python int uses a C long, which has nothing to do with a Python long. In Python 2.2+, arithmetic expressions with Python int types can seamlessly return a Python long. For example, on a system with a 32-bit C long: If the intermittent result never exceeds 2**31 - 1, the expression returns an int: >>> 2**30 - 1 + 2**30 2147483647 However, if the order of operations in this expression are swapped to have an intermittent result of 2**31, it returns a long (the representation ends with an 'L'): >>> 2**30 + 2**30 - 1 2147483647L Python 3, on the other hand, pretty much renamed "long" to "int" (as it similarly renamed unicode to str), so now there's only one integer type named "int". There is no built-in type named "long". But the C API preserves the original type (PyLong_Type) and function names such as PyLong_FromSsize_t: >>> from ctypes import * >>> type( pythonapi.PyLong_FromSsize_t(-1) ) PEP 237 (Unifying Long Integers and Integers) goes into more detail, but it predates the Python 3 implementation: http://www.python.org/dev/peps/pep-0237 From alan.gauld at btinternet.com Wed Oct 3 20:53:57 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 03 Oct 2012 19:53:57 +0100 Subject: [Tutor] a question about maxint In-Reply-To: References: Message-ID: On 03/10/12 12:33, eryksun wrote: > On Wed, Oct 3, 2012 at 1:28 AM, Katya Stolpovskaya >> Thank you for you reply, but with "long" I got the same error: >> >>>>> from sys import * >>>>> long >> >> Traceback (most recent call last): >> File "", line 1, in >> long >> NameError: name 'long' is not defined > > I assumed some familiarity with Python 2, which has two integer > arithmetic types: int (fixed precision, using the platform's C long > type) and long (arbitrary precision). The naming is a bit conflated in > that a Python int uses a C long, which has nothing to do with a Python > long. > In case Katya didn't follow the technical stuff the bottom line is that 'long' is not a name in Python. You should check maxsize rather than maxint but in practice you probably don't need to, just treat Python integers as being of arbitrary length: >>> bigInt = 25**45 >>> print bigInt 807793566946316088741610050849573099185363389551639556884765625 >>> The only times you really need to worry about maxsize is when interfacing to external non-python code. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eryksun at gmail.com Wed Oct 3 21:41:06 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 3 Oct 2012 15:41:06 -0400 Subject: [Tutor] a question about maxint In-Reply-To: References: Message-ID: On Wed, Oct 3, 2012 at 2:53 PM, Alan Gauld wrote: > > The only times you really need to worry about maxsize is when interfacing to > external non-python code. It's not generally a problem, but if you're on a 32-bit platform, for which sys.maxsize is 2**31 - 1, that sets the maximum length of a sequence or string. So, for example, you won't be able to read a 2 GiB file into one string (or Py3k bytes object). However, if you're on a 64-bit platform, there's nothing to worry about since sys.maxsize is 2**63 - 1. From terrence.brannon at bankofamerica.com Wed Oct 3 21:59:27 2012 From: terrence.brannon at bankofamerica.com (Brannon, Terrence) Date: Wed, 03 Oct 2012 15:59:27 -0400 Subject: [Tutor] forcing the None values of a dictionary Message-ID: <660092B20711A14FAD01C4733DD0E66712E043E5@smtp_mail.bankofamerica.com> I'm wondering if there is something I overlooked for this function I wrote... and also whether it could've been done destrictively instead of returning a new dictionary: def dictNoneValueTo(d, new_value=''): """force None values in a dictionary to a default value""" for k in d: if d[k] is None: d[k] = new_value return d ---------------------------------------------------------------------- This message w/attachments (message) is intended solely for the use of the intended recipient(s) and may contain information that is privileged, confidential or proprietary. If you are not an intended recipient, please notify the sender, and then please delete and destroy all copies and attachments, and be advised that any review or dissemination of, or the taking of any action in reliance on, the information contained in or attached to this message is prohibited. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Sender. Subject to applicable law, Sender may intercept, monitor, review and retain e-communications (EC) traveling through its networks/systems and may produce any such EC to regulators, law enforcement, in litigation and as required by law. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or free of errors or viruses. References to "Sender" are references to any subsidiary of Bank of America Corporation. Securities and Insurance Products: * Are Not FDIC Insured * Are Not Bank Guaranteed * May Lose Value * Are Not a Bank Deposit * Are Not a Condition to Any Banking Service or Activity * Are Not Insured by Any Federal Government Agency. Attachments that are part of this EC may have additional important disclosures and disclaimers, which you should read. This message is subject to terms available at the following link: http://www.bankofamerica.com/emaildisclaimer. By messaging with Sender you consent to the foregoing. -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Wed Oct 3 22:41:31 2012 From: d at davea.name (Dave Angel) Date: Wed, 03 Oct 2012 16:41:31 -0400 Subject: [Tutor] forcing the None values of a dictionary In-Reply-To: <660092B20711A14FAD01C4733DD0E66712E043E5@smtp_mail.bankofamerica.com> References: <660092B20711A14FAD01C4733DD0E66712E043E5@smtp_mail.bankofamerica.com> Message-ID: <506CA2FB.7030701@davea.name> On 10/03/2012 03:59 PM, Brannon, Terrence wrote: > I'm wondering if there is something I overlooked for this function I wrote... and also whether it could've been done destrictively instead of returning a new dictionary: > > def dictNoneValueTo(d, new_value=''): > """force None values in a dictionary to a default value""" > for k in d: > if d[k] is None: > d[k] = new_value > return d > Hard to tell what you might have overlooked. Does the function correctly implement the specification you didn't tell us about? There's no English word "destrictively" so I'm not sure what you're asking. The function certainly doesn't return a new dictionary, it returns the same one passed in. In other words, it mutates itself in place. By convention, it should return None (which would happen if you had no return statement). -- DaveA From bgailer at gmail.com Wed Oct 3 23:00:35 2012 From: bgailer at gmail.com (bob gailer) Date: Wed, 03 Oct 2012 17:00:35 -0400 Subject: [Tutor] forcing the None values of a dictionary In-Reply-To: <660092B20711A14FAD01C4733DD0E66712E043E5@smtp_mail.bankofamerica.com> References: <660092B20711A14FAD01C4733DD0E66712E043E5@smtp_mail.bankofamerica.com> Message-ID: <506CA773.9020100@gmail.com> On 10/3/2012 3:59 PM, Brannon, Terrence wrote: > > I'm wondering if there is something I overlooked for this function I > wrote... and also whether it could've been done destrictively instead > of returning a new dictionary: > I don't understand your question. Could you clarify? What does "destrictivel" mean? Why do you say it is returning a new dictionary (it simply returns the modified input dictionary)? Since d is mutable, the function changes it "in place". You could drop the return statement and call the function thusly: d = someDictionary dictNoneValueTo(d, 123) > *def* dictNoneValueTo(d, new_value=''): > *"""force None values in a dictionary to a default value"""* > *for* k *in* d: > *if* d[k] *is* *None*: > d[k] = new_value > *return* d > For what its worth please inform your lawyers that I do not read or heed any lengthy legalese at the end of emails. I think they do more to irritate recipients than to protect anything. Be warned: this message has no intention to do anything but inform. You may do anything with it you like. So there. -- Bob Gailer 919-636-4239 Chapel Hill NC -------------- next part -------------- An HTML attachment was scrubbed... URL: From eowens0124 at gmx.com Thu Oct 4 03:46:27 2012 From: eowens0124 at gmx.com (Ed Owens) Date: Wed, 3 Oct 2012 21:46:27 -0400 Subject: [Tutor] modifying lists of lists Message-ID: <007001cda1d2$12ed5440$38c7fcc0$@com> I'm just learning Python, so I apologize for a newby question. I'm trying to work with lists of lists, the lowest level of which hold one or more tuples. I've tried to condense what I've tried. The code is: #! Python 2.7 import copy list = [] for i in range(8): list.append((i, i+1)) H = [[list[0], list[2]]] I = [[list[0], list[2]]] J = [[list[0], list[2]]] # H.append(tuple) did it three times for example, and because H=I=J won't work for the example. print 'H - list with one list of tuples:', H H.append([list[1], list[3]]) I.append([list[1], list[3]]) J.append([list[1], list[3]]) print 'H - added list of tuples:', H # duplicate first list of tuples -> next in sequence H.insert(1,H[0]) print 'duplicated 1st list in list:', H # works, but can't edit the second list H[1].pop(1) print "First 2 lists can't be independently edited" print H,'\n' # try to split up the list and rebuild J = J[:1] + J[:1] + J[1:] print 'J:', J J[1].pop(1) print "Still can't independently delete tuple:\n", J # try the copy module I = I[:1] + copy.deepcopy(I[:1]) + I[1:] print '\nI:', I I[1].pop(1) print "deepcopy requred for independence:\n", I The third trial works: >>> H - list with one list of tuples: [[(0, 1), (2, 3)]] H - added list of tuples: [[(0, 1), (2, 3)], [(1, 2), (3, 4)]] duplicated 1st list in list: [[(0, 1), (2, 3)], [(0, 1), (2, 3)], [(1, 2), (3, 4)]] First 2 lists can't be independently edited [[(0, 1)], [(0, 1)], [(1, 2), (3, 4)]] J: [[(0, 1), (2, 3)], [(0, 1), (2, 3)], [(1, 2), (3, 4)]] Still can't independently delete tuple: [[(0, 1)], [(0, 1)], [(1, 2), (3, 4)]] I: [[(0, 1), (2, 3)], [(0, 1), (2, 3)], [(1, 2), (3, 4)]] deepcopy requred for independence: [[(0, 1), (2, 3)], [(0, 1)], [(1, 2), (3, 4)]] >>> Is there a better way? I've spent a lot of time looking through the documentation so I anticipated the results of the first attempt. I postulate the split approach didn't work because the assignment was a pointer to the object, in spite of the splitting up of the list. By the way, copy.copy doesn't work. Thanks for your insights. Ed -------------- next part -------------- An HTML attachment was scrubbed... URL: From existentialleo at gmail.com Thu Oct 4 03:56:43 2012 From: existentialleo at gmail.com (Leo Degon) Date: Wed, 3 Oct 2012 21:56:43 -0400 Subject: [Tutor] reloading a module Message-ID: So Ive got code that i import a module to get certain saved variables, where i edit the text file that comprises the module to edit those saved variable. My problem is I cant reload the module to access those modified variables. I was wondering how can i reload or otherwise refresh the module. python3 on linux Thank you. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Thu Oct 4 04:07:04 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 04 Oct 2012 12:07:04 +1000 Subject: [Tutor] reloading a module In-Reply-To: References: Message-ID: <506CEF48.7020001@pearwood.info> On 04/10/12 11:56, Leo Degon wrote: > So Ive got code that i import a module to get certain saved variables, > where i edit the text file that comprises the module to edit those saved > variable. My problem is I cant reload the module to access those modified > variables. > I was wondering how can i reload or otherwise refresh the module. > python3 on linux The reload function is removed from the built-ins from Python 3 because it is a bit of a trap for the careless and the beginner. But it isn't removed completely. Run this when you start up Python: from imp import reload Now you can call reload(module) as in Python 2. -- Steven From bgailer at gmail.com Thu Oct 4 04:12:12 2012 From: bgailer at gmail.com (bob gailer) Date: Wed, 03 Oct 2012 22:12:12 -0400 Subject: [Tutor] reloading a module In-Reply-To: References: Message-ID: <506CF07C.9020800@gmail.com> On 10/3/2012 9:56 PM, Leo Degon wrote: > So Ive got code that i import a module to get certain saved variables, > where i edit the text file that comprises the module to edit those > saved variable. My problem is I cant reload the module to access those > modified variables. Why not? What did you try? > I was wondering how can i reload or otherwise refresh the module. > python3 on linux > Answer: >>> import x >>> # make some changes to x.py >>> reload(x) -- Bob Gailer 919-636-4239 Chapel Hill NC From d at davea.name Thu Oct 4 04:15:20 2012 From: d at davea.name (Dave Angel) Date: Wed, 03 Oct 2012 22:15:20 -0400 Subject: [Tutor] reloading a module In-Reply-To: References: Message-ID: <506CF138.7040105@davea.name> On 10/03/2012 09:56 PM, Leo Degon wrote: > So Ive got code that i import a module to get certain saved variables, > where i edit the text file that comprises the module to edit those saved > variable. My problem is I cant reload the module to access those modified > variables. > I was wondering how can i reload or otherwise refresh the module. > python3 on linux > > Python version 3 replaced the reload(xxx) syntax with : import imp imp.reload(xxx) However, there are still lots of caveats, and reasons not to do it. This doesn't initialize the new module, it doesn't follow the imports used there, it doesn't tell other modules of the code about this new version of the module, .... The feature is there for debugging, as far as I know, and probably shouldn't be used in real code. If your design is to have these variables' default values to come from the file, then I'd suggest making it a data file, and not an import. Then you can literally save and restore those variables flexibly, and without straining the language's limits. Alternatively, just restart the process. -- DaveA From steve at pearwood.info Thu Oct 4 04:22:35 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 04 Oct 2012 12:22:35 +1000 Subject: [Tutor] modifying lists of lists In-Reply-To: <007001cda1d2$12ed5440$38c7fcc0$@com> References: <007001cda1d2$12ed5440$38c7fcc0$@com> Message-ID: <506CF2EB.5030303@pearwood.info> On 04/10/12 11:46, Ed Owens wrote: > I'm just learning Python, so I apologize for a newby question. I'm trying > to work with lists of lists, the lowest level of which hold one or more > tuples. I've tried to condense what I've tried. Hi Ed, and welcome! > The code is: I'm afraid I can't make heads nor tails of *why* you are trying this. I can guess you are testing something about copying lists, but comments like this: > # H.append(tuple) did it three times for example, and because H=I=J won't > work for the example. leave me mystified. "did it three times for example"? Did what? What counts as working? You pepper your comments with things like "doesn't work", "can't do this", "can't be edited" etc., but without knowing what you expect to happen it's hard to understand what you mean. How about if you start off with a simple example, and you show what happens when you run the code, AND what you expected to happen? To make it easy, here is my *guess* as to the sort of thing that is confusing you. py> H = [[1, 2]] py> J = [H[0]] py> print H [[1, 2]] py> print J [[1, 2]] py> H[0][0] = 99 py> print H # expected, and got, [[99, 2]] [[99, 2]] py> print J # expected [[1, 2]] [[99, 2]] Am I close? If I am, I (or one of the other tutors) will explain what is going on. Otherwise, you will have to explain what you are trying to do in more detail. -- Steven From robertvstepp at gmail.com Thu Oct 4 05:11:19 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 3 Oct 2012 22:11:19 -0500 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: <506BC4B4.3070203@davea.name> References: <506BC4B4.3070203@davea.name> Message-ID: Thanks to all who responded. There was much more going on here than I ever would have suspected. I am glad I asked the questions I did. This has been very informative. On Tue, Oct 2, 2012 at 11:53 PM, Dave Angel wrote: > > There are two operations supported by (most) objects that produce a > string. One is exemplified by the str() function, which converts an > object to a string. That's the one called implicitly by print(). This > form just represents the data, in the form most likely to be needed by > the end user. What happens if str() or repr() is not supported by a particular object? Is an exception thrown, an empty string returned or something else I am not imagining? > > The other operation is repr(), which attempts to produce a string that > could be used in a program to reproduce the actual object. So a repr() > will have quote marks artificially added, or brackets, or commas, or > whatever seems appropriate for the particular object. This is intended > for the programmer's use, not for the end user. What larger phrase does "repr" stand for? My text mentions "representational form" later in the book, which sounds similar in concept to what you are discussing. [...] > Your question was about string objects, but I tried to make the > explanation as generic as possible. Those two functions, str() and > repr(), are used, or implied in many places. For example, if you print > a list, it'll call str() on the whole list. But the list object's logic > will in turn call repr() on each of its elements, and put the whole > thing together with braces and commas. > As I go along in my study of Python will it become clear to me when and how repr() and str() are being "...used, or implied in many places"? Thanks! boB From robertvstepp at gmail.com Thu Oct 4 05:39:01 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 3 Oct 2012 22:39:01 -0500 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: <20121003063843.GB27111@ando> References: <20121003063843.GB27111@ando> Message-ID: On Wed, Oct 3, 2012 at 1:38 AM, Steven D'Aprano wrote: > The long answer is a bit more subtle, and rather long. I had initial suspicions this would be the case Thanks for yours and Dave's detailed exposition! [...] > Python is no different: words, text if you will, that are part of the > code are written as normal: > > # source code > class Test: > pass > > x = Test # Test here refers to the variable Test, a class > > But to create a string object, you use quotation marks to tell Python > that this is data, not code, please create a string object: > > x = "Test" # Test here refers to a string, which is data > > Notice that the quotation marks are *delimiters*, they mark the start > and end of the string, but aren't part of the string in any way. Python > knows that the object is a string because you put it in string > delimiters, but the delimiters are not part of the string. I was not sure if the quotes were considered part of the string or not. Thanks for the clarification. > Now, take a step back and consider objects in general. There are two > things we might like to do to an arbitrary object: > > * display the object, which implicitly means turning it into a > string, or at least getting some representation of that object > as a string; > > * convert the object into a string. > > Python has two built-in functions for that: > > * repr, which takes any object and returns a string that represents > that object; > > * str, which tries to convert an object into a string, if that makes > sense. > > Often those will do the same thing. For example: > > py> str(42) == repr(42) == "42" > True > > But not always. For example: > > py> from decimal import Decimal as D > py> x = D("1.23") > py> print(str(x)) > 1.23 > py> print(repr(x)) > Decimal('1.23') These contrasting examples are very illuminating. So in print(str(x)) the object, D("1.23"), is being converted into a readable string, which makes the most sense as 1.23. But print(repr(x)) is giving a string representation of the object as code, which is more than just 1.23, the Decimal('1.23'). Am I understanding this correctly? > Unfortunately, the difference between str() and repr() is kind of > arbitrary and depends on the object. str() is supposed to return a > "human-readable" version of the object, for display, while repr() is > supposed to return a string which would work as code, but those are more > guidelines than hard rules. Will these fine distinctions be easy for me to pick up on as I progress in my Python studies? I suspect that I am going to have to experiment with str() and repr() in each new situation to see what results. > So we have two different ways of converting an object to a string. But > strings themselves are objects too. What happens there? > > py> s = "Hello world" # remember the quotes are delimiters, not part of the string > py> print(str(s)) > Hello world > py> print(repr(s)) > 'Hello world' > > str() of a string is unchanged (and why shouldn't it be? it's already a > string, there's nothing to convert). > > But repr() of a string creates a new string showing the representation > of the original string, that is, what you would need to type in source > code to make that string. That means: > > 1) wrap the whole thing in delimiters (quotation marks) > 2) escaping special characters like tabs, newlines, and binary > characters. As to point 2), will repr() insert "\" (I am assuming Python uses a backslash like other languages to escape. I have not read about this in Python yet.) for these special characters? Will str() do the same? > Notice that the string returned by repr() includes quote marks as part > of the new string. Given the s above: > > py> t = repr(s) > py> print(t) > 'Hello world' > py> t > "'Hello world'" > > This tells us that the new string t includes single quote marks as the > first and last character, so when you print it, the single quote marks > are included in the output. But when you just display t interactively > (see below), the delimiters are shown. Another great example. I probably would have overlooked this. > Now, at the interactive interpreter, evaluating an object on its own > without saving the result anywhere displays the repr() to the screen. > Why repr()? Well, why not? The decision was somewhat arbitrary. So the designers of Python made this decision. I guess it had to be one way or the other. > print, on the other hand, displays the str() of the object directly to > the screen. For strings, that means the delimiters are not shown, > because they are not part of the string itself. Why str() rather than > repr()? Because that's what people mostly want, and if you want the > other, you can just say print(repr(obj)). So in the end it is a simple choice to give the users what they want and are already used to. > > Does this help, or are you more confused than ever? > This has been incredibly useful! Many thanks!! boB From eowens0124 at gmx.com Thu Oct 4 05:52:54 2012 From: eowens0124 at gmx.com (Ed Owens) Date: Wed, 3 Oct 2012 23:52:54 -0400 Subject: [Tutor] modifying lists of lists In-Reply-To: <506CF2EB.5030303@pearwood.info> References: <007001cda1d2$12ed5440$38c7fcc0$@com> <506CF2EB.5030303@pearwood.info> Message-ID: <008601cda1e3$bdb423c0$391c6b40$@com> You are fundamentally correct about my confusion, though I'm trying to work with tuples as the lowest level, which may not be relevant here. -----Original Message----- . py> H = [[1, 2]] py> J = [H[0]] py> print H [[1, 2]] py> print J [[1, 2]] py> H[0][0] = 99 py> print H # expected, and got, [[99, 2]] [[99, 2]] py> print J # expected [[1, 2]] [[99, 2]] -------------- Using your example: Py> Import copy py> H = [[1, 2]] py> J = [H[0]] py> print H [[1, 2]] py> print J [[1, 2]] py> H[0][1] = copy.deepcopy(H[0][0]) py> print H # expected, and got, [[1,1]] [[1,1]] py> print J # expected [[1,2]] [[1, 1]] How do I decouple these references? From eryksun at gmail.com Thu Oct 4 07:22:19 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 4 Oct 2012 01:22:19 -0400 Subject: [Tutor] modifying lists of lists In-Reply-To: <008601cda1e3$bdb423c0$391c6b40$@com> References: <007001cda1d2$12ed5440$38c7fcc0$@com> <506CF2EB.5030303@pearwood.info> <008601cda1e3$bdb423c0$391c6b40$@com> Message-ID: On Wed, Oct 3, 2012 at 11:52 PM, Ed Owens wrote: > > py> H = [[1, 2]] > py> J = [H[0]] > py> H[0][1] = copy.deepcopy(H[0][0]) > > How do I decouple these references? You can use the slice H[0][:] to get a shallow copy of the H[0] list. By "shallow copy" I mean you get a new list that contains the items of the source list. You can mutate this new list without changing the source list. Returning to your first example: >>> H = [[(0, 1), (2, 3)], [(1, 2), (3, 4)]] Insert a shallow copy of H[0] at index 1: >>> H.insert(1, H[0][:]) >>> H [[(0, 1), (2, 3)], [(0, 1), (2, 3)], [(1, 2), (3, 4)]] The new list at H[1] is independent of H[0]: >>> H[1].pop(1) (2, 3) >>> H [[(0, 1), (2, 3)], [(0, 1)], [(1, 2), (3, 4)]] From d at davea.name Thu Oct 4 13:02:26 2012 From: d at davea.name (Dave Angel) Date: Thu, 04 Oct 2012 07:02:26 -0400 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: References: <506BC4B4.3070203@davea.name> Message-ID: <506D6CC2.3000903@davea.name> On 10/03/2012 11:11 PM, boB Stepp wrote: > Thanks to all who responded. > . > What happens if str() or repr() is not supported by a particular > object? Is an exception thrown, an empty string returned or something > else I am not imagining? Let's try it and see: >>> class A:pass ... >>> a = A() >>> a <__main__.A object at 0x16ae790> This is generic information about an object with no methods at all, and in particular without a __repr__ method. It identifies the module where the class was defined, the name of the class, and the address the particular instance happens to be located at. (In CPython, that happens to be identical to id(a). I'd be happier if it would just identify the number as the id, since ordinarily, the address is of no use. BTW, as far as I know, there's no promise as to how this is formatted, so I wouldn't try to parse it with a program. >> >> What larger phrase does "repr" stand for? My text mentions >> "representational form" later in the book, which sounds similar in >> concept to what you are discussing. That would be my guess. I don't recall seeing anything about it. -- DaveA From d at davea.name Thu Oct 4 13:21:56 2012 From: d at davea.name (Dave Angel) Date: Thu, 04 Oct 2012 07:21:56 -0400 Subject: [Tutor] modifying lists of lists In-Reply-To: <008601cda1e3$bdb423c0$391c6b40$@com> References: <007001cda1d2$12ed5440$38c7fcc0$@com> <506CF2EB.5030303@pearwood.info> <008601cda1e3$bdb423c0$391c6b40$@com> Message-ID: <506D7154.1030804@davea.name> On 10/03/2012 11:52 PM, Ed Owens wrote: > You are fundamentally correct about my confusion, though I'm trying to work > with tuples as the lowest level, which may not be relevant here. The tuples are an example of an immutable object. An immutable object (which contains only immutable objects) may be safely shared without risk of the kind of problem you're having. ints would have worked just as well. Eryksun is almost correct, but I'm going to attempt to show you how to figure it out for yourself, next time. > > -----Original Message----- > . > > py> H = [[1, 2]] > py> J = [H[0]] > py> print H > [[1, 2]] > py> print J > [[1, 2]] Here, if you print id(H[0]) and id(J[0]) you'll see they're the same. You've already shown they're lists, and therefore mutable. Thus, you have a risk. H and J are different lists, each containing the same list as their content. Also try >>> H is J False >>> H[0] is J[0] True >>> Now, if we go one more level down, to H[0][0], we'll see a tuple. And although they're also identical, there's no problem there. This is how eryksun knew that a shallow copy was good enough. And for a shallow copy, using the slice notation is perfect. >>> J42 = [H[:1]] >>> print J42[0] [(1, 2)] >>> print id(J42[0]) 13964568 (Note that my shallow copy is not quite what eryksun was using. His lost one level of the nesting, by doing both the subscript and the slice.) Now, since the id is different, you won't get the problem you saw first. -- DaveA From steve at pearwood.info Thu Oct 4 13:28:32 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 04 Oct 2012 21:28:32 +1000 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: References: <20121003063843.GB27111@ando> Message-ID: <506D72E0.60402@pearwood.info> On 04/10/12 13:39, boB Stepp wrote: >> But not always. For example: >> >> py> from decimal import Decimal as D >> py> x = D("1.23") >> py> print(str(x)) >> 1.23 >> py> print(repr(x)) >> Decimal('1.23') > > These contrasting examples are very illuminating. So in print(str(x)) > the object, D("1.23"), is being converted into a readable string, > which makes the most sense as 1.23. But print(repr(x)) is giving a > string representation of the object as code, which is more than just > 1.23, the Decimal('1.23'). Am I understanding this correctly? Pretty close. In the example above, the calls to print are only there to avoid distracting you with the string delimiters, the outer quote marks. It's str() and repr() that are doing the real work. Apart from that, you've got it right. str(x) returns a human-readable version of x, which in this case is "1.23" (excluding the quote marks, of course). The designer of the Decimal class choose for repr() of a decimal to look as much as possible like the call to the class that created the object in the first place. (Or at least an equivalent call.) In this case, that is "Decimal('1.23')". >> Unfortunately, the difference between str() and repr() is kind of >> arbitrary and depends on the object. str() is supposed to return a >> "human-readable" version of the object, for display, while repr() is >> supposed to return a string which would work as code, but those are more >> guidelines than hard rules. > > Will these fine distinctions be easy for me to pick up on as I > progress in my Python studies? I suspect that I am going to have to > experiment with str() and repr() in each new situation to see what > results. *shrug* I've been programming in Python for over 10 years, and I still forget when str() is used and when repr() is used. I always have to check. But maybe that's just me. Remember, there is no hard rule that tells you what the output of str() and repr() must be (apart from strings). Different programmers have different ideas of what is useful, meaningful, or possible. [...] >> But repr() of a string creates a new string showing the representation >> of the original string, that is, what you would need to type in source >> code to make that string. That means: >> >> 1) wrap the whole thing in delimiters (quotation marks) >> 2) escaping special characters like tabs, newlines, and binary >> characters. > > As to point 2), will repr() insert "\" (I am assuming Python uses a > backslash like other languages to escape. I have not read about this > in Python yet.) for these special characters? Will str() do the same? Yes to repr(), no to str(). Remember, str() of a string is just the same string unchanged. If the input string contains a newline, the output will also contain a newline: py> s = "abc" + chr(10) + "def" py> print(s) abc def py> print(str(s)) abc def But repr() will create a new string, and escape any "non-printable" character (and a few which are printable): py> print(repr(s)) 'abc\ndef' So this shows us that instead of creating string s as I did above, by concatenating two substrings and a newline character, I could just as easily have created it in one go using a \n escape: py> t = "abc\ndef" py> s == t True Notice too that there is no difference between the two different flavours of single quote delimiters. Whether you write "a" or 'a' is entirely a matter of personal preference. Python accepts both to make it easy to input strings containing quote marks: s = "this string contains ' a single-quote" t = 'this string contains " a double-quote' Such single quote strings must start and end on the same line. On the other hand, *triple-quote* delimiters """ or ''' are used for multiline strings. They can extend over multiple lines. Now, ask me about *raw strings*, and the difference between Unicode and byte strings :) -- Steven From steve at pearwood.info Thu Oct 4 13:45:42 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 04 Oct 2012 21:45:42 +1000 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: References: <506BC4B4.3070203@davea.name> Message-ID: <506D76E6.4080401@pearwood.info> On 04/10/12 13:11, boB Stepp wrote: > What happens if str() or repr() is not supported by a particular > object? Is an exception thrown, an empty string returned or something > else I am not imagining? I don't think that is possible, at least not by accident or neglect. In Python 3, everything inherits from object, which supports both str and repr, so everything else should too: py> class MyClass: ... pass ... py> obj = MyClass() py> str(obj) '<__main__.MyClass object at 0xb7c8c9ac>' py> repr(obj) '<__main__.MyClass object at 0xb7c8c9ac>' Not terribly exciting, but at least it tells you what the object is, and gives you enough information to distinguish it from other, similar, objects. I suppose you could write a class that deliberately raised an exception when you called str() on it, in which case it would raise an exception when you called str() on it... :) Likewise for repr(). py> class Stupid: ... def __str__(self): ... raise TypeError('cannot stringify this object') ... py> obj = Stupid() py> str(obj) Traceback (most recent call last): File "", line 1, in File "", line 3, in __str__ TypeError: cannot stringify this object > What larger phrase does "repr" stand for? My text mentions > "representational form" later in the book, which sounds similar in > concept to what you are discussing. repr is short for "representation", as in "string representation". > As I go along in my study of Python will it become clear to me when > and how repr() and str() are being "...used, or implied in many > places"? Generally, print and the interactive interpreter are the only implicit string conversions. At least the only ones I can think of right now... no, wait, there's another one, error messages. print() displays the str() of the object. The interactive interpreter displays the repr() of the object. Error messages could do whatever they like. Anything else, you have to explicitly convert to a string using the form you want: s = repr(x).lower() t = str(y).replace('ss', '?') or whatever. -- Steven From bballplaya5316 at aim.com Wed Oct 3 17:38:01 2012 From: bballplaya5316 at aim.com (bballplaya5316 at aim.com) Date: Wed, 3 Oct 2012 11:38:01 -0400 (EDT) Subject: [Tutor] need help new to python Message-ID: <8CF6F9434948CA2-D98-77585@webmail-m049.sysops.aol.com> Hi im new to python and im stuck on my homework. I have to define the functions appropriately in the python file so they all test and work but im not even sure where to begin or what to I could really use some help. Ive attached the file. Thank you, Adam -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: homework1.py URL: From magicwizardstar at gmail.com Wed Oct 3 05:39:00 2012 From: magicwizardstar at gmail.com (Palice Fan) Date: Tue, 2 Oct 2012 23:39:00 -0400 Subject: [Tutor] Hello Can someone looked at my problem? stuck Message-ID: Hello i got stuck with the last bit of my programming practice. Can somebody help me? Write a program to read through a mail log, and figure out who had the most messages in the file. The program looks for ?From? lines and takes the second parameter on those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender?s address to the total number of messages for that person. After all the data has been read the program looks through the dictionary using a maximum loop (see Section 5.7.2) to find who has the most messages and how many messages the person has. Enter a file name: mbox-short.txt cwen at iupui.edu :5 Enter a file name: mbox.txt zqian at umich.edu :195 Instead of printing off a number beside the email, i got another email and i dont know how to fix it. -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: 9.4 stuck.png Type: image/png Size: 67410 bytes Desc: not available URL: From magicwizardstar at gmail.com Wed Oct 3 06:11:25 2012 From: magicwizardstar at gmail.com (medusa) Date: Tue, 2 Oct 2012 21:11:25 -0700 (PDT) Subject: [Tutor] MAXIMUM LOOP LOGIC Message-ID: <1349237485916-4990842.post@n6.nabble.com> Hello i got stuck with the last bit of my programming practice. Can somebody help me? Write a program to read through a mail log, and figure out who had the most messages in the file. The program looks for ?From? lines and takes the second parameter on those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender?s address to the total number of messages for that person. After all the data has been read the program looks through the dictionary using a maximum loop (see Section 5.7.2) to find who has the most messages and how many messages the person has. Enter a file name: mbox-short.txt cwen at iupui.edu :5 Enter a file name: mbox.txt zqian at umich.edu :195 Instead of printing off a number beside the email, i got another email and i dont know how to fix it. -- View this message in context: http://python.6.n6.nabble.com/MAXIMUM-LOOP-LOGIC-tp4990842.html Sent from the Python - tutor mailing list archive at Nabble.com. From tfahey1 at yahoo.com Thu Oct 4 17:21:18 2012 From: tfahey1 at yahoo.com (tfahey1) Date: Thu, 4 Oct 2012 08:21:18 -0700 (PDT) Subject: [Tutor] Newbie help with .pyc Message-ID: <1349364078572-4991028.post@n6.nabble.com> Hey everyone, I am a Maya user, so I only come into contact with Python scripting on a surface level, but I downloaded a python script from a CG website that I would like to check out but I am getting an error when it is run. The only file included was a .pyc file. I'm wondering if there should also be a .py file along with it? Is there anyway I could fix this script to work without a .py file? Thanks, Tim Fahey -- View this message in context: http://python.6.n6.nabble.com/Newbie-help-with-pyc-tp4991028.html Sent from the Python - tutor mailing list archive at Nabble.com. From eryksun at gmail.com Thu Oct 4 19:17:54 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 4 Oct 2012 13:17:54 -0400 Subject: [Tutor] modifying lists of lists In-Reply-To: <506D7154.1030804@davea.name> References: <007001cda1d2$12ed5440$38c7fcc0$@com> <506CF2EB.5030303@pearwood.info> <008601cda1e3$bdb423c0$391c6b40$@com> <506D7154.1030804@davea.name> Message-ID: On Thu, Oct 4, 2012 at 7:21 AM, Dave Angel wrote: > > This is how eryksun knew that a shallow copy was good enough. And for a > shallow copy, using the slice notation is perfect. > >>>> J42 = [H[:1]] >>>> print J42[0] > [(1, 2)] >>>> print id(J42[0]) > 13964568 > > (Note that my shallow copy is not quite what eryksun was using. His > lost one level of the nesting, by doing both the subscript and the slice.) > Now, since the id is different, you won't get the problem you saw first. >>> H = [[1, 2]] Copy the list [1,2] at H[0] and nest it in a list: >>> J = [H[0][:]] >>> J [[1, 2]] Modify the original list: >>> H[0][1] = H[0][0] >>> H [[1, 1]] The copy is unchanged: >>> J [[1, 2]] Seems OK to me. Then I switched to the Ed's original problem. This problem no longer uses another named list (J in the above), but it's similar. Here's H: >>> H = [[(0, 1), (2, 3)], [(1, 2), (3, 4)]] The problem is to insert a copy of list H[0] at index 1 of H. At a minimum it has to be shallow copied because later Ed wants to modify it (i.e. H[1].pop(1)) without affecting H[0]. Copying H up to index 1 would preserve an undesired nested structure, so you'd have to subscript, but then the subscripted value would be the original H[0] list. You actually have to copy H[0], either by slicing with H[0][:], or calling the list constructor with list(H[0]), or using copy.copy(H[0]). From eryksun at gmail.com Thu Oct 4 19:27:53 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 4 Oct 2012 13:27:53 -0400 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: References: <506BC4B4.3070203@davea.name> Message-ID: On Wed, Oct 3, 2012 at 11:11 PM, boB Stepp wrote: > > What happens if str() or repr() is not supported by a particular > object? Is an exception thrown, an empty string returned or something > else I am not imagining? The __str__ method inherited from "object" calls __repr__. For a class, __repr__ is inherited from type.__repr__, which returns "". For an instance, __repr__ is inherited from object.__repr__, which returns "". If you override __str__ or __repr__, you must return a string. Else the interpreter will raise a TypeError. Basic example: >>> class Test:... repr of the class: >>> repr(Test) "" repr of an instance: >>> repr(Test()) '<__main__.Test object at 0x958670c>' > As I go along in my study of Python will it become clear to me when > and how repr() and str() are being "...used, or implied in many > places"? str is Python's string type, while repr is a built-in function that returns a string suitable for debugging. You can also call str without an argument to get an empty string, i.e. str() == ''. This is similar to other built-in types: int() == 0, float() == 0.0, complex() == 0j, tuple() = (), list() = [], and dict = {}. The returned value is either 0 or empty -- and boolean False in all cases. str also takes the optional arguments "encoding" and "errors" to decode an encoded string: >>> str(b'spam', encoding='ascii') 'spam' bytes and bytearray objects have a decode() method that offers the same functionality: >>> b'spam'.decode('ascii') 'spam' But other objects that support the buffer interface might not. For example, take the following array.array with the ASCII encoded bytes of "spam": >>> arr = array.array('B', b'spam') Here's the repr: >>> arr array('B', [115, 112, 97, 109]) Without an argument str just returns the repr of the array: >>> print(arr) array('B', [115, 112, 97, 109]) (The print function calls str.) But we can tell str to treat the array as an ASCII encoded buffer: >>> print(str(arr, 'ascii')) spam From steve at pearwood.info Thu Oct 4 19:39:31 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 05 Oct 2012 03:39:31 +1000 Subject: [Tutor] Newbie help with .pyc In-Reply-To: <1349364078572-4991028.post@n6.nabble.com> References: <1349364078572-4991028.post@n6.nabble.com> Message-ID: <506DC9D3.3040209@pearwood.info> On 05/10/12 01:21, tfahey1 wrote: > Hey everyone, > > I am a Maya user, so I only come into contact with Python scripting on a > surface level, but I downloaded a python script from a CG website that I > would like to check out but I am getting an error when it is run. Would you like us to guess what error you are getting? And how you are trying to run it? I love guessing games! Nah, actually I'm lying. I hate guessing games. Please tell us exactly how you are trying to run the file, and show us the EXACT error you get. Copy and paste the error message. > The only > file included was a .pyc file. I'm wondering if there should also be a .py > file along with it? Is there anyway I could fix this script to work without > a .py file? Probably. The way to "fix" it will depend on what error you are getting. -- Steven From joel.goldstick at gmail.com Thu Oct 4 20:07:34 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Thu, 4 Oct 2012 14:07:34 -0400 Subject: [Tutor] need help new to python In-Reply-To: <8CF6F9434948CA2-D98-77585@webmail-m049.sysops.aol.com> References: <8CF6F9434948CA2-D98-77585@webmail-m049.sysops.aol.com> Message-ID: On Wed, Oct 3, 2012 at 11:38 AM, wrote: > Hi im new to python and im stuck on my homework. I have to define the > functions appropriately in the python file so they all test and work but im > not even sure where to begin or what to I could really use some help. Ive > attached the file. > > Thank you, > > Adam > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > So, start defining each function. You must have done this before this assignment. Because many of these functions use a string as the input parameter, lookup in the python docs about strings, and string methods. come back with code that works or doesn't. Include the code in your reply along with the results and the traceback -- Joel Goldstick From chigga101 at gmail.com Thu Oct 4 21:31:35 2012 From: chigga101 at gmail.com (Matthew Ngaha) Date: Thu, 4 Oct 2012 20:31:35 +0100 Subject: [Tutor] [Tkinter-discuss] displaying an image In-Reply-To: References: <207F243D-25FF-44DF-AC64-AA6B9227325C@gmail.com> Message-ID: > You need to install PIL to use the tutorial you are doing. > > http://www.pythonware.com/products/pil/ the site says " The current free version is PIL 1.1.7. This release supports Python 1.5.2 and newer, including 2.5 and 2.6. A version for 3.X will be released later. " i have python 3. all the downloads below it only show python 2 versions. am i stuck?:( From fomcl at yahoo.com Thu Oct 4 21:47:18 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 4 Oct 2012 12:47:18 -0700 (PDT) Subject: [Tutor] rounding up to the nearest multiple of 8 Message-ID: <1349380038.46988.YahooMailNeo@web110711.mail.gq1.yahoo.com> Hi, ? The function below works, but it's such a kludge! Is there a way to make this more elegant? As said in the docstring, I want to round up a given integer to the nearest multiple of 8. I was thinking of something like math.ceil. ? def _getPadding(charlen): ??? """ Helper function to replace null bytes ('\x00') at the end of ??? string values. Rounds up to the nearest multiple of 8. ??? Every string value is 8 bytes or a multiple thereof. For example, a ??? 5-character string needs a padding of 3 spaces to prevent the ??? null bytes ??? >>> dict([(charlen, _getPadding(charlen)) for charlen in range(1, 40, 7)]) ??? {1: 8, 36: 40, 8: 8, 15: 16, 22: 24, 29: 32} ??? >>> s = "I love Python" ??? >>> s.ljust(_getPadding(len(s))) ??? 'I love Python?? ' ??? """ ??? i = 0 ??? while True: ??????? i += 1 ??????? if (i * 8) > charlen: ??????????? return (i * 8) % charlen + charlen if charlen > 1 else 8 if __name__ == "__main__": ??? import doctest ??? doctest.testmod() Thanks! 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 aceway at qq.com Thu Oct 4 21:57:36 2012 From: aceway at qq.com (Aceway) Date: Fri, 5 Oct 2012 03:57:36 +0800 Subject: [Tutor] Tutor Digest, Vol 104, Issue 24 In-Reply-To: References: Message-ID: <5F144E5A-BE20-44DD-9223-1C86982CE2B8@qq.com> your last for iterater maybe should like this? for key?values in messages? if max is None or values>max: sender=key max=values ? 2012-10-5?1:13?tutor-request at python.org ??? > Send Tutor mailing list submissions to > tutor at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to > tutor-request at python.org > > You can reach the person managing the list at > tutor-owner at python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > 1. Hello Can someone looked at my problem? stuck (Palice Fan) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Tue, 2 Oct 2012 23:39:00 -0400 > From: Palice Fan > To: tutor at python.org > Subject: [Tutor] Hello Can someone looked at my problem? stuck > Message-ID: > > Content-Type: text/plain; charset="windows-1252" > > Hello > i got stuck with the last bit of my programming practice. > Can somebody help me? > Write a program to read through a mail log, and figure out who had the most > messages in the file. The program looks for ?From? lines and takes the > second parameter on > those lines as the person who sent the mail. > The program creates a Python dictionary that maps the sender?s address to > the total number of > messages for that person. > After all the data has been read the program looks through the dictionary > using a maximum loop > (see Section 5.7.2) to find who has the most messages and how many messages > the person has. > > Enter a file name: mbox-short.txt > cwen at iupui.edu :5 > Enter a file name: mbox.txt > zqian at umich.edu :195 > > Instead of printing off a number beside the email, i got another email and > i dont know how to fix it. > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: > -------------- next part -------------- > A non-text attachment was scrubbed... > Name: 9.4 stuck.png > Type: image/png > Size: 67410 bytes > Desc: not available > URL: > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > > ------------------------------ > > End of Tutor Digest, Vol 104, Issue 24 > ************************************** > From joel.goldstick at gmail.com Thu Oct 4 22:04:46 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Thu, 4 Oct 2012 16:04:46 -0400 Subject: [Tutor] rounding up to the nearest multiple of 8 In-Reply-To: <1349380038.46988.YahooMailNeo@web110711.mail.gq1.yahoo.com> References: <1349380038.46988.YahooMailNeo@web110711.mail.gq1.yahoo.com> Message-ID: On Thu, Oct 4, 2012 at 3:47 PM, Albert-Jan Roskam wrote: > Hi, > > The function below works, but it's such a kludge! Is there a way to make this more elegant? > As said in the docstring, I want to round up a given integer to the nearest multiple of 8. I was thinking > of something like math.ceil. > > def _getPadding(charlen): > """ Helper function to replace null bytes ('\x00') at the end of > string values. Rounds up to the nearest multiple of 8. > Every string value is 8 bytes or a multiple thereof. For example, a > 5-character string needs a padding of 3 spaces to prevent the > null bytes > >>> dict([(charlen, _getPadding(charlen)) for charlen in range(1, 40, 7)]) > {1: 8, 36: 40, 8: 8, 15: 16, 22: 24, 29: 32} > >>> s = "I love Python" > >>> s.ljust(_getPadding(len(s))) > 'I love Python ' > """ > i = 0 > while True: > i += 1 > if (i * 8) > charlen: > return (i * 8) % charlen + charlen if charlen > 1 else 8 > if __name__ == "__main__": > import doctest > doctest.testmod() > > Thanks! > > Regards, > Albert-Jan > > >>> my_string = "123" >>> pad = 8 - len(my_string) % 8 >>> my_string = my_string + " " * pad >>> my_string '123 ' -- Joel Goldstick From eryksun at gmail.com Thu Oct 4 22:17:47 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 4 Oct 2012 16:17:47 -0400 Subject: [Tutor] MAXIMUM LOOP LOGIC In-Reply-To: <1349237485916-4990842.post@n6.nabble.com> References: <1349237485916-4990842.post@n6.nabble.com> Message-ID: On Wed, Oct 3, 2012 at 12:11 AM, medusa wrote: > > After all the data has been read the program looks through the dictionary > using a maximum loop > > Instead of printing off a number beside the email, i got another email and i > dont know how to fix it. > A dict iterates over its keys, so your loop is finding the 'max' email address (in string sort order), which is the one starting with the letter 'z'. You need to iterate over iteritems() if you want both keys and values: for item in messages.iteritems(): "item" is a tuple containing the (key, value) pair. You can immediately unpack this tuple if you want: for email, count in messages.iteritems(): BTW, your variable named "max" is masking the built-in function "max" that will do what you want, faster and simpler -- especially since you're testing if "max is None" for every value in the loop. In general for a reducing operation such as max (or min, sum, etc), initialize to the first item of the iterable/iterator. You can get the first item of an iterator using the built-in next() function. This function has an optional 2nd argument to set a default value, which could be used as a sentry, but let's use a simple test to see if "messages" is empty instead of worrying about default values and exception handling: if messages: it = messages.iteritems() max_item = next(it) for item in it: if item[1] > max_item[1]: max_item = item print max_item[0], max_item[1] else: print "no messages" There's also the built-in function "reduce" for this kind of thing. Here's a simple example of how it works: >>> from operator import add >>> add(1, 1) 2 >>> reduce(add, [1,1,1,1,1]) 5 Here's what this does: add(1, 1) => 2 add(2, 1) => 3 add(3, 1) => 4 add(4, 1) => 5 So you could replace the reducing loop with the built-in reduce() function. First you have to define a simple function (call it "imax") to return the max of two key/value tuple items based on the value in index 1: imax = lambda a, b: b if b[1] > a[1] else a if messages: max_item = reduce(imax, messages.iteritems()) print max_item[0], max_item[1] else: print "no messages" Finally, here's a solution using "max". It's similar, but more efficient: from operator import itemgetter if messages: max_item = max(messages.iteritems(), key=itemgetter(1)) print max_item[0], max_item[1] else: print "no messages" dict.get also has a default value option, which will help to simplify the code in your first for loop. Instead of testing if email is in messages and special casing the first insertion, you can use get(email, 0): messages[email] = messages.get(email, 0) + 1 Also, using a defaultdict would be even simpler and faster: >>> from collections import defaultdict >>> messages = defaultdict(int) >>> messages['magic at gmail.com'] += 1 >>> messages defaultdict(, {'magic at gmail.com': 1}) From wprins at gmail.com Thu Oct 4 22:22:16 2012 From: wprins at gmail.com (Walter Prins) Date: Thu, 4 Oct 2012 21:22:16 +0100 Subject: [Tutor] Hello Can someone looked at my problem? stuck In-Reply-To: References: Message-ID: On 3 October 2012 04:39, Palice Fan wrote: > Hello > i got stuck with the last bit of my programming practice. > Can somebody help me? > Write a program to read through a mail log, and figure out who had the most > messages in the file. The program looks for ?From? lines and takes the > second parameter on > those lines as the person who sent the mail. > The program creates a Python dictionary that maps the sender?s address to > the total number of > messages for that person. > After all the data has been read the program looks through the dictionary > using a maximum loop > (see Section 5.7.2) to find who has the most messages and how many > messages the person has. > > Enter a file name: mbox-short.txt > cwen at iupui.edu :5 > Enter a file name: mbox.txt > zqian at umich.edu :195 > > Instead of printing off a number beside the email, i got another email and > i dont know how to fix it. > For future reference, please either include the source code in the email as text or as text attachment. A screen capture means I have to retype all your code (and in this case some data) in order to have a look at your problem. Not fun. To fix your problem you have to reverse engineer what's going on in your program. I'll try and walk you through a little thought process in figuring out what's going on to try and help you. The last statement in your program (which is where the error is apparent) prints a fixed email address followed by a value that's assigned earlier on in a loop from the "values" variable. Consequently you should carefully inspect your code and ask yourself how it's possible that an email address instead of a number is being assigned to the "values" variable and thereby eventually to the "max" variable. (By the way, note that "max" is not recommended as a variable name since max is also a built-in function in Python and so by declaring a variable with the same name you're hiding (known as shadowing) the Python function. You can see there's something special about it by the fact that IDLE colours it purple, which should tip you off.) But anyway, back to your "max" variable and "values" variable, we now look back carefully at the loop to see how or where we might be picking up email addresses when we should be getting integer counts... Let's look carefully at the loop declaration: for values in messages: Hmmm, this is looping directly over the dictionary "messages". What is returned when you iterate directly over a dict like that? (Hint, it's not the values, but the keys... e.g. the email addresses.) Add some print statements in your loop so you can see what happens when it runs, for example: print 'Starting iterating over "messages" dict' for values in messages: print 'Value of "values" this iteration =', values if max is None or values > max: print 'Updating max...' max = values print 'Value of "max" after this iteration =', max If you apply similar changes to your program and run that you'll see why the program doesn't work -- "values" is being assigned the keys (email addresses) from the dict, not the values. It should also become clear that basically "values" is also a bad choice for the items being iterated over in the messages dict and is perhaps adding to the confusion, better would be: for email_sender in messages: This would make it clear that the items being iterated over are in fact the email addresses. It's always a good idea to use descriptive specific names in your programs, not least because you yourself also need to read and understand your own code. Anyway, then later in your loop it's then obvious that you can't just do: if max is None or values > max: max = values (or if we use my suggested renaming) if max is None or email_sender > max: max = email_sender Instead you want to retrieve the actual value (count) from the dict for that specific email sender, e.g. if max is None or messages[email_sender] > max: max = messages[email_sender] ... and with that I've now basically explained the essence of your first main problem. However there remains another major flaw. Why are we assigning and outputting 'cwen at iupui.edu' as the email address with the maximum number of emails, for any input? Clearly that can't be right - if the input changes and another email address has the highest count then this code will output the wrong result. So in addition to saving the max count, you must also save the max sender in the loop. I think that's enough for now, see if you can fix your program given the above hints and if not post back again. HTH, Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From brian.van.den.broek at gmail.com Thu Oct 4 22:26:06 2012 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Thu, 4 Oct 2012 16:26:06 -0400 Subject: [Tutor] MAXIMUM LOOP LOGIC In-Reply-To: <1349237485916-4990842.post@n6.nabble.com> References: <1349237485916-4990842.post@n6.nabble.com> Message-ID: On 4 Oct 2012 13:22, "medusa" wrote: > > Hello > > i got stuck with the last bit of my programming practice. > Instead of printing off a number beside the email, i got another email and i > dont know how to fix it. > Hi, Is your code long? If not, you should include it in your message. If it is, you should spend the time to trim it down to a short chunk that demonstrates the issue. While opinions are divided, many don't like code provided via a link in tutor posts. That cuts down on the number of people willing to look at your code. By the link you gave, it seems like you've posted a screenshot or other image. Even those who are happy to follow links to code are unlikely to be willing to retype what is shown in your image file. I'd suggest posting again, this time with code. Best, Brian vdB -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Thu Oct 4 23:26:13 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 4 Oct 2012 17:26:13 -0400 Subject: [Tutor] rounding up to the nearest multiple of 8 In-Reply-To: References: <1349380038.46988.YahooMailNeo@web110711.mail.gq1.yahoo.com> Message-ID: On Thu, Oct 4, 2012 at 4:04 PM, Joel Goldstick wrote: > >>>> my_string = "123" >>>> pad = 8 - len(my_string) % 8 >>>> my_string = my_string + " " * pad >>>> my_string > '123 ' If len(my_string) is already a multiple of 8, the above sets pad to 8: >>> s = "12345678" >>> pad = 8 - len(my_string) % 8 >>> pad 8 You could special case when the modulo value is 0, or use pad % 8, but I think it's simpler to calculate "-len(s) % 8": >>> [(charlen, -charlen % 8) for charlen in range(9)] [(0, 0), (1, 7), (2, 6), (3, 5), (4, 4), (5, 3), (6, 2), (7, 1), (8, 0)] For example: >>> len_mod = lambda s, n: len(s) + (-len(s) % n) >>> s = ''; s.ljust(len_mod(s, 8)) '' >>> s = '12345678'; s.ljust(len_mod(s, 8)) '12345678' >>> s = '123456'; s.ljust(len_mod(s, 8)) '123456 ' >>> s = '123456789' >>> len_mod(s, 8) 16 >>> s.ljust(len_mod(s, 8)) '123456789 ' >>> s.ljust(len_mod(s, 9)) '123456789' From finsolut2003 at yahoo.com Thu Oct 4 23:30:20 2012 From: finsolut2003 at yahoo.com (tayo rotimi) Date: Thu, 4 Oct 2012 14:30:20 -0700 (PDT) Subject: [Tutor] I Need Help With Using Tkinter/Console/Creating GUIs In-Reply-To: <1347270014.44482.YahooMailNeo@web162301.mail.bf1.yahoo.com> References: <1347270014.44482.YahooMailNeo@web162301.mail.bf1.yahoo.com> Message-ID: <1349386220.90903.YahooMailNeo@web162302.mail.bf1.yahoo.com> Hi, I recently started learning as a python programming 'absolute beginner'. I have Python 3.2 installed on my laptop, and I have learned to a point where I need to create GUIs. I understand from the text book I am reading that all I need to have access to the Tkinter toolkits in a window-OS is double click on the Python icon to run Tkinter program directly; but I don't have Python icon on my desktop. I went to the program list and the relevant stuffs I have there are the IDLE (Python GUI) and Python (command line). Double clicking on any of these did not bring out the console described for creating GUIs. I am in a fixed. Its either the Tkinter module is not available on my Python installation or there are things I am not doing right. Please I need help, I need some practical guides on how to start creating GUI. Again, I have Python 3.2 installed on my system. ? Regards. Tayo. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Fri Oct 5 00:17:17 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 05 Oct 2012 08:17:17 +1000 Subject: [Tutor] I Need Help With Using Tkinter/Console/Creating GUIs In-Reply-To: <1349386220.90903.YahooMailNeo@web162302.mail.bf1.yahoo.com> References: <1347270014.44482.YahooMailNeo@web162301.mail.bf1.yahoo.com> <1349386220.90903.YahooMailNeo@web162302.mail.bf1.yahoo.com> Message-ID: <506E0AED.8060002@pearwood.info> On 05/10/12 07:30, tayo rotimi wrote: > Hi, > > I recently started learning as a python programming 'absolute beginner'. > I have Python 3.2 installed on my laptop, and I have learned to a point > where I need to create GUIs. I understand from the text book I am > reading that all I need to have access to the Tkinter toolkits in a > window-OS is double click on the Python icon to run Tkinter program >directly; but I don't have Python icon on my desktop. I went to the >program list and the relevant stuffs I have there are the IDLE (Python >GUI) and Python (command line). Double clicking on any of these did not > bring out the console described for creating GUIs. Then what did they do? > I am in a fixed. Its either the Tkinter module is not available on my > Python installation or there are things I am not doing right. Please I > need help, I need some practical guides on how to start creating GUI. > Again, I have Python 3.2 installed on my system. What operating system are you using? Open the Python command line, and you should see a console open with something like this: Python 3.2.2 (default, Mar 4 2012, 10:50:33) [GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >> > Type: import tkinter and press the Enter key. What happens? If you get an error, copy and paste the entire traceback, not just the error message. -- Steven From oscar.j.benjamin at gmail.com Fri Oct 5 01:26:50 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Fri, 5 Oct 2012 00:26:50 +0100 Subject: [Tutor] [Tkinter-discuss] displaying an image In-Reply-To: References: <207F243D-25FF-44DF-AC64-AA6B9227325C@gmail.com> Message-ID: On 4 October 2012 20:31, Matthew Ngaha wrote: >> You need to install PIL to use the tutorial you are doing. >> >> http://www.pythonware.com/products/pil/ > > the site says > " > The current free version is PIL 1.1.7. This release supports Python > 1.5.2 and newer, including 2.5 and 2.6. A version for 3.X will be > released later. > " > > i have python 3. all the downloads below it only show python 2 > versions. am i stuck?:( Yes. Well actually, no. You can just install Python 2.7 and use PIL with that. It might be better though to focus on things that do work in Python 3 and find a tutorial that doesn't need PIL. It's possible that PIL will never be available for Python 3. Here's a message from 2009 on the PIL mailing list (it looks like not much has changed since then): ''' The plan is to get 1.1.7 out of the door (early april) and then make a version of that available for 3.x. There's also a preliminary port of 1.1.6 available from a third party; see the mailing list archives for pointers. ''' http://mail.python.org/pipermail/image-sig/2009-March/005498.html Oscar From krush1954 at yahoo.com Fri Oct 5 05:23:53 2012 From: krush1954 at yahoo.com (ken brockman) Date: Thu, 4 Oct 2012 20:23:53 -0700 (PDT) Subject: [Tutor] Newbie help with .pyc In-Reply-To: <506DC9D3.3040209@pearwood.info> References: <1349364078572-4991028.post@n6.nabble.com> <506DC9D3.3040209@pearwood.info> Message-ID: <1349407433.4943.YahooMailNeo@web39304.mail.mud.yahoo.com> I wonder if they might be a way for some on this forum to dispense advice and help others without the totally snide and obnoxious attitude? if it is so?painfully?annoying for you to?deal?with, why?subject?yourself?to it? I?suspect?it is the sheer joy of showing others how bright you are and just?how?much?disdain?you have ?for the lower forms of life you share the planet with. Now give me a 4 paragraph long diatribe about top posting,?netiquette and loads of all?manner of?minutia on the proper way to post and?Make?sure you do it in the most?belittling way possible. ________________________________ From: Steven D'Aprano To: tutor at python.org Sent: Thursday, October 4, 2012 1:39 PM Subject: Re: [Tutor] Newbie help with .pyc On 05/10/12 01:21, tfahey1 wrote: > Hey everyone, > > I am a Maya user, so I only come into contact with Python scripting on a > surface level, but I downloaded a python script from a CG website that I > would like to check out but I am getting an error when it is run. Would you like us to guess what error you are getting? And how you are trying to run it? I love guessing games! Nah, actually I'm lying. I hate guessing games. Please tell us exactly how you are trying to run the file, and show us the EXACT error you get. Copy and paste the error message. >? The only > file included was a .pyc file. I'm wondering if there should also be a .py > file along with it? Is there anyway I could fix this script to work without > a .py file? Probably. The way to "fix" it will depend on what error you are getting. -- 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 quazi.ashfaq at gmail.com Fri Oct 5 06:17:30 2012 From: quazi.ashfaq at gmail.com (Ashfaq) Date: Fri, 5 Oct 2012 10:17:30 +0600 Subject: [Tutor] Newbie help with .pyc In-Reply-To: <1349407433.4943.YahooMailNeo@web39304.mail.mud.yahoo.com> References: <1349364078572-4991028.post@n6.nabble.com> <506DC9D3.3040209@pearwood.info> <1349407433.4943.YahooMailNeo@web39304.mail.mud.yahoo.com> Message-ID: Hey Ken, Steve has just made some innocent fun. He is eager to help the guy. Please don't be so harsh. Sincerely Ashfaq -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Fri Oct 5 06:39:54 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 5 Oct 2012 00:39:54 -0400 Subject: [Tutor] [Tkinter-discuss] displaying an image In-Reply-To: References: <207F243D-25FF-44DF-AC64-AA6B9227325C@gmail.com> Message-ID: On Thu, Oct 4, 2012 at 3:31 PM, Matthew Ngaha wrote: >> You need to install PIL to use the tutorial you are doing. >> >> http://www.pythonware.com/products/pil/ > > the site says > " > The current free version is PIL 1.1.7. This release supports Python > 1.5.2 and newer, including 2.5 and 2.6. A version for 3.X will be > released later. > " > > i have python 3. all the downloads below it only show python 2 > versions. am i stuck?:( If you're using Windows, Christoph Gholke has an installer for an unofficial port to 3.2/3.3 for 32-bit and 64-bit platforms: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil He also has the ported source code available to build PIL on a non-Windows platform. But be forewarned this is rarely a simple process. Below I've stepped through compiling the ported code on my Debian system. But your mileage will *probably* vary... Extract the source and change to the directory. Switch to your root account and update/install the development packages. I'm skipping TIFF support since Debian's libtiff4-dev package doesn't have the private header (tiffiop.h) needed for PIL's experimental TIFF support. Steps: $ sudo su # apt-get update # apt-get install build-essential python3-dev tk8.5-dev \ libjpeg8-dev liblcms1-dev libfreetype6-dev Two files need to be edited before building the extension modules. (1) As is, the library files for the optional features won't be found. You could symlink the required library files to /usr/lib, but I'd rather modify setup.py. After line 229 in setup.py, add the lib directory that Debian uses: For a 32-bit platform, add the following: add_directory(library_dirs, "/usr/lib/i386-linux-gnu") For a 64-bit platform, add the following: add_directory(library_dirs, "/usr/lib/x86_64-linux-gnu") (2) In _imagingft.c: font_getsize, someone modified the source to use "max". Original: PIXEL(self->face->size->metrics.height) Modified: PIXEL(max( self->face->size->metrics.height, self->face->bbox.yMax - self->face->bbox.yMin)) "max" isn't defined, however, so you have to add a macro. I defined it on line 21: #define max(a,b) ((a) > (b) ? a : b) With those two edits, you should be able to build the extension modules in place: # python3 setup.py build_ext -i Run the tests: # python3 selftest.py I get the following output: --- PIL CORE support ok --- TKINTER support ok --- JPEG support ok --- ZLIB (PNG/ZIP) support ok --- FREETYPE2 support ok --- LITTLECMS support ok ------------------------------ Running selftest: --- 57 tests passed. Now do the full the installation: # python3 setup.py install Then from the same directory start python3 and test showing an image: >>> from PIL import Image >>> img = Image.open('Images/lena.png') >>> img.show() From eryksun at gmail.com Fri Oct 5 06:57:53 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 5 Oct 2012 00:57:53 -0400 Subject: [Tutor] [Tkinter-discuss] displaying an image In-Reply-To: References: <207F243D-25FF-44DF-AC64-AA6B9227325C@gmail.com> Message-ID: On Fri, Oct 5, 2012 at 12:39 AM, eryksun wrote: > > # apt-get install build-essential python3-dev tk8.5-dev \ > libjpeg8-dev liblcms1-dev libfreetype6-dev I forgot zlib: # apt-get install build-essential python3-dev tk8.5-dev \ libjpeg8-dev liblcms1-dev libfreetype6-dev zlib1g-dev From steve at pearwood.info Fri Oct 5 08:54:59 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 5 Oct 2012 16:54:59 +1000 Subject: [Tutor] rounding up to the nearest multiple of 8 In-Reply-To: References: <1349380038.46988.YahooMailNeo@web110711.mail.gq1.yahoo.com> Message-ID: <20121005065459.GB14666@ando> On Thu, Oct 04, 2012 at 05:26:13PM -0400, eryksun wrote: > On Thu, Oct 4, 2012 at 4:04 PM, Joel Goldstick wrote: > > > >>>> my_string = "123" > >>>> pad = 8 - len(my_string) % 8 > >>>> my_string = my_string + " " * pad > >>>> my_string > > '123 ' > > If len(my_string) is already a multiple of 8, the above sets pad to 8: > > >>> s = "12345678" > >>> pad = 8 - len(my_string) % 8 > >>> pad > 8 Here's another way: py> from __future__ import division py> from math import ceil py> "%*s" % (int(ceil(len(mystring)/8)*8), mystring) ' 123412341234' Or left-justified: py> "%-*s" % (int(ceil(len(mystring)/8)*8), mystring) '123412341234 ' In Python 3, there is no need for the "from __future__" line. -- Steven From fomcl at yahoo.com Fri Oct 5 10:24:11 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 5 Oct 2012 01:24:11 -0700 (PDT) Subject: [Tutor] rounding up to the nearest multiple of 8 In-Reply-To: <20121005065459.GB14666@ando> References: <1349380038.46988.YahooMailNeo@web110711.mail.gq1.yahoo.com> <20121005065459.GB14666@ando> Message-ID: <1349425451.2392.YahooMailNeo@web110706.mail.gq1.yahoo.com> ----- Original Message ----- > From: Steven D'Aprano > To: tutor at python.org > Cc: > Sent: Friday, October 5, 2012 8:54 AM > Subject: Re: [Tutor] rounding up to the nearest multiple of 8 > > On Thu, Oct 04, 2012 at 05:26:13PM -0400, eryksun wrote: >> On Thu, Oct 4, 2012 at 4:04 PM, Joel Goldstick > wrote: >> > >> >>>> my_string = "123" >> >>>> pad = 8 - len(my_string) % 8 >> >>>> my_string = my_string + " " * pad >> >>>> my_string >> > '123? ? ' >> >> If len(my_string) is already a multiple of 8, the above sets pad to 8: >> >> ? ? >>> s = "12345678" >> ? ? >>> pad = 8 - len(my_string) % 8 >> ? ? >>> pad >> ? ? 8 > > Here's another way: > > > py> from __future__ import division > py> from math import ceil > py> "%*s" % (int(ceil(len(mystring)/8)*8), mystring) > '? ? 123412341234' > > > Or left-justified: > > py> "%-*s" % (int(ceil(len(mystring)/8)*8), mystring) > '123412341234? ? ' > > > In Python 3, there is no need for the "from __future__" line. Hi Steven, Eryksun, Joel, Thanks for your replies! Steven, I noticed that the "from __future__" line can be omitted if len(mystring) is divided by 8.0 (ie, by a float rather than an int). I compared the "ceil" approach to the "modulo" approach, and found that the ceil approach is 2.6 times slower than the other approach. In this case, that's a relevant difference as the padding sometimes needs to be done millions of times. import timeit ver1 = timeit.timeit(""" import math value = "1234" value = "%-*s" % (int(math.ceil(len(value)/8.0)*8), value) """) ver2 = timeit.timeit(""" value = "1234" value = value.ljust( len(value) + (-len(value) % 8) ) """) print ver1 print ver2 print ver1 / ver2 From eryksun at gmail.com Fri Oct 5 10:31:08 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 5 Oct 2012 04:31:08 -0400 Subject: [Tutor] rounding up to the nearest multiple of 8 In-Reply-To: <20121005065459.GB14666@ando> References: <1349380038.46988.YahooMailNeo@web110711.mail.gq1.yahoo.com> <20121005065459.GB14666@ando> Message-ID: On Fri, Oct 5, 2012 at 2:54 AM, Steven D'Aprano wrote: > > py> from __future__ import division > py> from math import ceil > py> "%*s" % (int(ceil(len(mystring)/8)*8), mystring) > ' 123412341234' > > > Or left-justified: > > py> "%-*s" % (int(ceil(len(mystring)/8)*8), mystring) > '123412341234 ' Or use floor division with -n: >>> lenstep = lambda s, n: -n * (len(s) // -n) >>> mystring = '123412341234' >>> "{1:<{0}s}".format(lenstep(mystring, 8), mystring) '123412341234 ' >>> "{1:>{0}s}".format(lenstep(mystring, 8), mystring) ' 123412341234' >>> "{1:^{0}s}".format(lenstep(mystring, 8), mystring) ' 123412341234 ' From pasokan at talentsprint.com Fri Oct 5 11:06:10 2012 From: pasokan at talentsprint.com (Asokan Pichai) Date: Fri, 5 Oct 2012 14:36:10 +0530 Subject: [Tutor] rounding up to the nearest multiple of 8 In-Reply-To: References: <1349380038.46988.YahooMailNeo@web110711.mail.gq1.yahoo.com> <20121005065459.GB14666@ando> Message-ID: If you are doing so many times, it may be worth precomputing the padding for a given length and adding it by a look up on the length. For example: ------------------------ SPACE = ' ' MAX = 1000 TAB = 8 paddding = [ SPACE * (n % TAB) for n in range(MAX) ] ..... s = padding[len(s)] + s ..... -------------------------- where s is string to be padded Asokan Pichai If a language is designed for non-programmers, soon only non-programs get written in it. --- Anonymouse From finsolut2003 at yahoo.com Fri Oct 5 11:06:30 2012 From: finsolut2003 at yahoo.com (tayo rotimi) Date: Fri, 5 Oct 2012 02:06:30 -0700 (PDT) Subject: [Tutor] I Need Help With Using Tkinter/Console/Creating GUIs In-Reply-To: References: Message-ID: <1349427990.95379.YahooMailNeo@web162305.mail.bf1.yahoo.com> Thank you Steven; I am now running. I just followed your hint. I later noticed the author was actually referring to an earlier version of Python, with the console not looking the same with Python 3.2.? Regards. Tayo. -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Fri Oct 5 11:07:49 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 05 Oct 2012 10:07:49 +0100 Subject: [Tutor] Newbie help with .pyc In-Reply-To: <1349407433.4943.YahooMailNeo@web39304.mail.mud.yahoo.com> References: <1349364078572-4991028.post@n6.nabble.com> <506DC9D3.3040209@pearwood.info> <1349407433.4943.YahooMailNeo@web39304.mail.mud.yahoo.com> Message-ID: On 05/10/2012 04:23, ken brockman wrote: > I wonder if they might be a way for some on this forum to dispense advice and help others without the totally snide and obnoxious attitude? if it is so painfully annoying for you to deal with, why subject yourself to it? I suspect it is the sheer joy of showing others how bright you are and just how much disdain you have for the lower forms of life you share the planet with. > > Now give me a 4 paragraph long diatribe about top posting, netiquette and loads of all manner of minutia on the proper way to post and Make sure you do it in the most belittling way possible. > > > I'm firmly with Steven here. If people cannot be bothered to do any research into how to ask they can expect snide responses. If they don't like that that they can move on, as they're paying Steven the same amount that they're paying me, zilch. -- Cheers. Mark Lawrence. From eryksun at gmail.com Fri Oct 5 11:15:16 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 5 Oct 2012 05:15:16 -0400 Subject: [Tutor] rounding up to the nearest multiple of 8 In-Reply-To: <1349425451.2392.YahooMailNeo@web110706.mail.gq1.yahoo.com> References: <1349380038.46988.YahooMailNeo@web110711.mail.gq1.yahoo.com> <20121005065459.GB14666@ando> <1349425451.2392.YahooMailNeo@web110706.mail.gq1.yahoo.com> Message-ID: On Fri, Oct 5, 2012 at 4:24 AM, Albert-Jan Roskam wrote: > > import timeit > ver1 = timeit.timeit(""" > import math > value = "1234" > value = "%-*s" % (int(math.ceil(len(value)/8.0)*8), value) > """) > ver2 = timeit.timeit(""" > value = "1234" > value = value.ljust( len(value) + (-len(value) % 8) ) > """) Try to focus a timeit run on the code that would actually run in a loop. Move global imports, constants, and class/function definitions into one or more setup strings. >>> from timeit import timeit >>> setup = "from math import ceil; value = '1234'" >>> ver1 = timeit('int(ceil(len(value)/8.0)*8)', setup=setup) >>> ver2 = timeit('len(value) + (-len(value) % 8)', setup=setup) >>> ver3 = timeit('-8 * (len(value) // -8)', setup=setup) >>> ver3 / ver2, ver3 / ver1 (0.6623768153526971, 0.2884886334856229) From alan.gauld at btinternet.com Fri Oct 5 11:30:25 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 05 Oct 2012 10:30:25 +0100 Subject: [Tutor] Newbie help with .pyc In-Reply-To: References: <1349364078572-4991028.post@n6.nabble.com> <506DC9D3.3040209@pearwood.info> <1349407433.4943.YahooMailNeo@web39304.mail.mud.yahoo.com> Message-ID: On 05/10/12 10:07, Mark Lawrence wrote: > On 05/10/2012 04:23, ken brockman wrote: >> I wonder if they might be a way for some on this forum to dispense >> advice and help others without the totally snide and obnoxious >> attitude? > > I'm firmly with Steven here. If people cannot be bothered to do any > research into how to ask they can expect snide responses. If they don't > like that that they can move on, There are valid points on both sides here. We do need to remember that not all the people coming to tutor are experienced computer users, They are often youngsters just getting started in programming and as such have no experience of research or internet communities other than Facebook etc. Technical forums are very different and they need to be guided for their own benefit. At the other end we have older folks who might be very intelligent and experienced in research but not in computer fora or email lists. These folks need to be educated in how to use technical forums but in a reasonable way. Of course if they ignore the advice given then they should not expect the sympathetic manner to continue! And then we have professionals moving from another language to python and here we would expect good questions, good formatting and the other things that show respect for their fellow professionals giving their time for free to help the community. Anything less is just laziness. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From fomcl at yahoo.com Fri Oct 5 13:23:12 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 5 Oct 2012 04:23:12 -0700 (PDT) Subject: [Tutor] rounding up to the nearest multiple of 8 In-Reply-To: References: <1349380038.46988.YahooMailNeo@web110711.mail.gq1.yahoo.com> <20121005065459.GB14666@ando> Message-ID: <1349436192.47674.YahooMailNeo@web110701.mail.gq1.yahoo.com> ----- Original Message ----- > From: Asokan Pichai > To: tutor at python.org > Cc: > Sent: Friday, October 5, 2012 11:06 AM > Subject: Re: [Tutor] rounding up to the nearest multiple of 8 > > If you are doing so many times, it may be worth precomputing the padding for a > given length and adding it by a look up on the length. > > For example: > ------------------------ > SPACE = ' ' > MAX = 1000 > TAB = 8 > paddding = [ SPACE * (n % TAB) for n in range(MAX) ] > > ..... > s = padding[len(s)] + s > ..... > -------------------------- > where s is string to be padded > > Asokan Pichai Good idea! I know that the string values can never exceed 32767 bytes. So when I combine all the advice I got here, the best way seems to be ver4, using Eryksun's ver3 to initialize a dictionary: from timeit import timeit setup = "from math import ceil; value = 1040 * '*'" setup += "; " + "padLookup = dict([(i, -8 * (i // -8)) for i in range(1, 32767+1)])" ver1 = timeit('int(ceil(len(value)/8.0)*8)', setup=setup) ver2 = timeit('len(value) + (-len(value) % 8)', setup=setup) ver3 = timeit('-8 * (len(value) // -8)', setup=setup) ver4 = timeit('padLookup[len(value)]', setup=setup) print ver1/ver4, ver2/ver4, ver3/ver4, ver4/ver4 Thanks guys! From d at davea.name Fri Oct 5 14:26:13 2012 From: d at davea.name (Dave Angel) Date: Fri, 05 Oct 2012 08:26:13 -0400 Subject: [Tutor] rounding up to the nearest multiple of 8 In-Reply-To: <1349436192.47674.YahooMailNeo@web110701.mail.gq1.yahoo.com> References: <1349380038.46988.YahooMailNeo@web110711.mail.gq1.yahoo.com> <20121005065459.GB14666@ando> <1349436192.47674.YahooMailNeo@web110701.mail.gq1.yahoo.com> Message-ID: <506ED1E5.70800@davea.name> On 10/05/2012 07:23 AM, Albert-Jan Roskam wrote: > ----- Original Message ----- > >> From: Asokan Pichai >> To: tutor at python.org >> Cc: >> Sent: Friday, October 5, 2012 11:06 AM >> Subject: Re: [Tutor] rounding up to the nearest multiple of 8 >> >> If you are doing so many times, it may be worth precomputing the padding for a >> given length and adding it by a look up on the length. >> >> For example: >> ------------------------ >> SPACE = ' ' >> MAX = 1000 >> TAB = 8 >> paddding = [ SPACE * (n % TAB) for n in range(MAX) ] >> >> ..... >> s = padding[len(s)] + s >> ..... >> -------------------------- >> where s is string to be padded >> >> Asokan Pichai > > Good idea! I know that the string values can never exceed 32767 bytes. So when I combine all the advice I got here, the best way seems to be ver4, using Eryksun's ver3 to initialize a dictionary: > > from timeit import timeit > setup = "from math import ceil; value = 1040 * '*'" > setup += "; " + "padLookup = dict([(i, -8 * (i // -8)) for i in range(1, 32767+1)])" > ver1 = timeit('int(ceil(len(value)/8.0)*8)', setup=setup) > ver2 = timeit('len(value) + (-len(value) % 8)', setup=setup) > ver3 = timeit('-8 * (len(value) // -8)', setup=setup) > ver4 = timeit('padLookup[len(value)]', setup=setup) > > print ver1/ver4, ver2/ver4, ver3/ver4, ver4/ver4 > > Thanks guys! > This is all fun, but what about the context? Your original function took an integer, not a string, and thus wasn't charged with measuring string length, possibly multiple times. Even so, each of these tests is taking around a microsecond. So are you expecting to do anything with the result? Just calling ljust() method more than doubled the time. If you actually have some code to generate the string, and/or if you're going to take the result and write it to a file, then pretty soon this function is negligible time. If it were my code, i think I'd use something like " "[-sz%8:] and either prepend or append that to my string. But if I had to do something more complex, I'd tune it to the way the string were being used. -- DaveA From rdmoores at gmail.com Fri Oct 5 15:47:42 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Fri, 5 Oct 2012 06:47:42 -0700 Subject: [Tutor] Through a glass, darkly: the datetime module Message-ID: I thought it would be useful to have a script that would tell me what the date n days from today would be. The docs () seem to get me almost there. I can compute the number of days between 2 dates, and the number of days between a date and today: Python 3.2.3 (default, Apr 11 2012, 07:12:16) [MSC v.1500 64 bit (AMD64)] import time from datetime import date, timedelta >>> date1 = date(2001, 9, 11) >>> date1 datetime.date(2001, 9, 11) >>> date2 = date(2003, 4, 15) >>> date2 datetime.date(2003, 4, 15) >>> date2 - date1 datetime.timedelta(581) >>> days_delta = date2 - date1 >>> days_delta.days 581 >>> today = date.today() >>>> today datetime.date(2012, 10, 5) >>> print(today) 2012-10-05 >>> time_to_date = today - date1 >>> time_to_date datetime.timedelta(4042) >>> print(time_to_date) 4042 days, 0:00:00 >>> print(time_to_date.days) 4042 However, brick wall: timedelta.days = 100 Traceback (most recent call last): File "", line 1, in builtins.TypeError: can't set attributes of built-in/extension type 'datetime.timedelta' Advice, please. Dick Moores From oberoc at gmail.com Fri Oct 5 16:02:31 2012 From: oberoc at gmail.com (Tino Dai) Date: Fri, 5 Oct 2012 10:02:31 -0400 Subject: [Tutor] getattr works sometimes In-Reply-To: <506B8950.1010603@pearwood.info> References: <506B8950.1010603@pearwood.info> Message-ID: On Tue, Oct 2, 2012 at 8:39 PM, Steven D'Aprano wrote: > On 03/10/12 04:20, Tino Dai wrote: > >> and the get_class class works sometime for finding modules within a >>>> certain directory. If the get_class >>>> doesn't work, it throws an AttributeError. >>>> >>> >>> I don't really understand what you mean by this. Can you copy and >>> paste the actual error message (all of it)? >>> >>> >>>> The module exists in the directory, and I'm trying to debug this. Does >>>> anybody have any hints to go about debug >>>> this? >>>> >>> >>> >> get_class('etl.transfers.bill_**subject') # >> etl.transfers.bill_subject does exist under the transfers directory >> > './leg_apps/etl/transfers/**bill_subject.pyc'> >> > > I don't understand why you are using the get_class function for this. > It is only useful when you don't know the name of the object until > runtime. Since you know the name, I would suggest that using a regular > import is better: > > from etl.transfers import bill_subject > > Imports also work differently to getattr, and I believe that is where > you are running into trouble. The (wrongly named) get_class function > uses getattr to extract names from a single top-level module. But that's > not what you are trying to do: you are trying to extract modules from > a package. > Where I was running into problems was the fact that I needed to import the modules before I could do a getattr using the get_class function. In retrospect, I'm bringing in basically all the objects anyway, so there really is no need to have this complicated set up. > > Use the right tool for the job: you are trying to use a screwdriver when > you need a screwdriver. > > My prediction is that *at some point* in your experiments, you have done > something like: > > etl.transfers.bill_subject = bill_subject > > *or something with the same effect*. Perhaps the "transfers" module > includes one of these lines: > > import bill_subject # Python 2.5 or older? > from . import bill_subject > > > Now bill_subject is an attribute of the transfers module, and getattr can > see it. But you haven't done the same for related_bills, and so getattr > cannot see it. > > Instead, use the right tool, import, which is designed for solving your > problem: > > from etl.transfers import bill_subject > from etl.transfers import related_bills > > should both work, unless you have removed the bill_subject.py and > related_bills.py files from the etl/transfers/ directory. > > Actually Steve, I don't know the correct classes to import until runtime. This is part of a bigger ETL (Extract - Transform - Load) routine, and this is the section that deals with records that didn't make it over because of some missing constraint. You are probably right in the fact that I just should import all the classes in and be done with it. But that brings me to my next question, how do I prevent from polluting the namespace with unneeded imports. Is there a way to de-import modules? Is there an idiom that pertains to this? Thanks, Tino -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Fri Oct 5 16:11:33 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 05 Oct 2012 15:11:33 +0100 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: Message-ID: On 05/10/2012 14:47, Richard D. Moores wrote: > I thought it would be useful to have a script that would tell me what > the date n days from today would be. The docs > () > seem to get me almost there. I can compute the number of days between > 2 dates, and the number of days between a date and today: > > Python 3.2.3 (default, Apr 11 2012, 07:12:16) [MSC v.1500 64 bit (AMD64)] > > import time > from datetime import date, timedelta >>>> date1 = date(2001, 9, 11) >>>> date1 > datetime.date(2001, 9, 11) >>>> date2 = date(2003, 4, 15) >>>> date2 > datetime.date(2003, 4, 15) >>>> date2 - date1 > datetime.timedelta(581) >>>> days_delta = date2 - date1 >>>> days_delta.days > 581 >>>> today = date.today() >>>>> today > datetime.date(2012, 10, 5) >>>> print(today) > 2012-10-05 >>>> time_to_date = today - date1 >>>> time_to_date > datetime.timedelta(4042) >>>> print(time_to_date) > 4042 days, 0:00:00 >>>> print(time_to_date.days) > 4042 > > However, brick wall: > > timedelta.days = 100 > Traceback (most recent call last): > File "", line 1, in > builtins.TypeError: can't set attributes of built-in/extension type > 'datetime.timedelta' > > Advice, please. > > Dick Moores > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From the docs http://docs.python.org/library/datetime.html class timedelta( [days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]]) hence timedelta(days=100) -- Cheers. Mark Lawrence. From wprins at gmail.com Fri Oct 5 16:15:19 2012 From: wprins at gmail.com (Walter Prins) Date: Fri, 5 Oct 2012 15:15:19 +0100 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: Message-ID: Hi Richard On 5 October 2012 14:47, Richard D. Moores wrote: > I thought it would be useful to have a script that would tell me what > the date n days from today would be. The docs > () > seem to get me almost there. I can compute the number of days between > 2 dates, and the number of days between a date and today: Does this hint help? >>> import datetime >>> mydate = datetime.date(2012,10,5) >>> mydate = mydate + datetime.timedelta(days=30) >>> print mydate 2012-11-04 >>> Walter From oscar.j.benjamin at gmail.com Fri Oct 5 16:16:44 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Fri, 5 Oct 2012 15:16:44 +0100 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: Message-ID: On 5 October 2012 14:47, Richard D. Moores wrote: > I thought it would be useful to have a script that would tell me what > the date n days from today would be. The docs > () > seem to get me almost there. I can compute the number of days between > 2 dates, and the number of days between a date and today: > > Python 3.2.3 (default, Apr 11 2012, 07:12:16) [MSC v.1500 64 bit (AMD64)] > > import time > from datetime import date, timedelta >>>> date1 = date(2001, 9, 11) >>>> date1 > datetime.date(2001, 9, 11) >>>> date2 = date(2003, 4, 15) >>>> date2 > datetime.date(2003, 4, 15) >>>> date2 - date1 > datetime.timedelta(581) >>>> days_delta = date2 - date1 >>>> days_delta.days > 581 >>>> today = date.today() >>>>> today > datetime.date(2012, 10, 5) >>>> print(today) > 2012-10-05 >>>> time_to_date = today - date1 >>>> time_to_date > datetime.timedelta(4042) >>>> print(time_to_date) > 4042 days, 0:00:00 >>>> print(time_to_date.days) > 4042 > > However, brick wall: > > timedelta.days = 100 > Traceback (most recent call last): > File "", line 1, in > builtins.TypeError: can't set attributes of built-in/extension type > 'datetime.timedelta' timedlta objects are immutable. Once an object is created it's values cannot be changed (much like a tuple). You need to create a new timedelta object: >>> from datetime import datetime, timedelta >>> dt = datetime.now() >>> print dt 2012-10-05 15:14:55.841000 >>> d = dt.date() >>> print d 2012-10-05 >>> td = timedelta(days=7) >>> print dt + td 2012-10-12 15:14:55.841000 >>> print d + td 2012-10-12 Oscar From fomcl at yahoo.com Fri Oct 5 16:23:01 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 5 Oct 2012 07:23:01 -0700 (PDT) Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: Message-ID: <1349446981.67533.YahooMailNeo@web110704.mail.gq1.yahoo.com> ----- Original Message ----- > From: Richard D. Moores > To: Tutor List > Cc: > Sent: Friday, October 5, 2012 3:47 PM > Subject: [Tutor] Through a glass, darkly: the datetime module > > I thought it would be useful to have a script that would tell me what > the date n days from today would be. The docs > () > seem to get me almost there. I can compute the number of days between > 2 dates, and the number of days between a date and today: > > Python 3.2.3 (default, Apr 11 2012, 07:12:16) [MSC v.1500 64 bit (AMD64)] > > import time > from datetime import date, timedelta >>>> date1 = date(2001, 9, 11) >>>> date1 > datetime.date(2001, 9, 11) >>>> date2 = date(2003, 4, 15) >>>> date2 > datetime.date(2003, 4, 15) >>>> date2 - date1 > datetime.timedelta(581) >>>> days_delta = date2 - date1 >>>> days_delta.days > 581 >>>> today = date.today() >>>>> today > datetime.date(2012, 10, 5) >>>> print(today) > 2012-10-05 >>>> time_to_date = today - date1 >>>> time_to_date > datetime.timedelta(4042) >>>> print(time_to_date) > 4042 days, 0:00:00 >>>> print(time_to_date.days) > 4042 > > However, brick wall: > > timedelta.days = 100 > Traceback (most recent call last): > ? File "", line 1, in > builtins.TypeError: can't set attributes of built-in/extension type > 'datetime.timedelta' > > Advice, please. Hello, Using Python 2.6: import datetime ndays = 10 print (datetime.datetime.today() + datetime.timedelta(days=ndays)).strftime("%Y-%m-%d") # prints? '2012-10-15' Regards, From polerx at hotmail.com Fri Oct 5 16:39:23 2012 From: polerx at hotmail.com (eddy van) Date: Fri, 5 Oct 2012 14:39:23 +0000 Subject: [Tutor] Python Install Program Message-ID: Hi Were can i download the python install program for free Thank you -------------- next part -------------- An HTML attachment was scrubbed... URL: From malaclypse2 at gmail.com Fri Oct 5 16:47:55 2012 From: malaclypse2 at gmail.com (Jerry Hill) Date: Fri, 5 Oct 2012 10:47:55 -0400 Subject: [Tutor] Python Install Program In-Reply-To: References: Message-ID: On Fri, Oct 5, 2012 at 10:39 AM, eddy van wrote: > Were can i download the python install program for free Python installers are here: http://www.python.org/download/ You'll want either version 2.7.3 or 3.3.0, depending on whether you need python 2 or python 3. The actual installer you choose will depend on the platform you're going to be running it on. If you happen to be on some flavor of linux, you're probably going to want to use your platform's package manager instead. -- Jerry From wprins at gmail.com Fri Oct 5 16:49:36 2012 From: wprins at gmail.com (Walter Prins) Date: Fri, 5 Oct 2012 15:49:36 +0100 Subject: [Tutor] Python Install Program In-Reply-To: References: Message-ID: Hi, On 5 October 2012 15:39, eddy van wrote: > Hi > > Were can i download the python install program for free If you google "download python" you'll get lots of relevant hits. That said, assuming you're using Windows, I recommend downloading one of the ActiveState Python distributions from here: http://www.activestate.com/activepython/downloads If you're going to be following a book or tutorial, it's probably best to match your the version of Python you downoad (e.g. 3.2 or 2.7) to that used in the book. Walter From fomcl at yahoo.com Fri Oct 5 21:19:58 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 5 Oct 2012 12:19:58 -0700 (PDT) Subject: [Tutor] rounding up to the nearest multiple of 8 In-Reply-To: <506ED1E5.70800@davea.name> References: <1349380038.46988.YahooMailNeo@web110711.mail.gq1.yahoo.com> <20121005065459.GB14666@ando> <1349436192.47674.YahooMailNeo@web110701.mail.gq1.yahoo.com> <506ED1E5.70800@davea.name> Message-ID: <1349464798.96779.YahooMailNeo@web110710.mail.gq1.yahoo.com> > This is all fun, but what about the context?? Your original function > took an integer, not a string, and thus wasn't charged with measuring > string length, possibly multiple times.? Even so, each of these tests is > taking around a microsecond.? So are you expecting to do anything with > the result?? Just calling ljust() method more than doubled the time.? If > you actually have some code to generate the string, and/or if you're > going to take the result and write it to a file, then pretty soon this > function is negligible time. > > If it were my code, i think I'd use something like? "? ? ? > "[-sz%8:] > and either prepend or append that to my string.? But if I had to do > something more complex, I'd tune it to the way the string were being used. > Yeah, maybe I got a little carried away. ;-) Knuth's 'premature optimization is the root of all evil' comes to mind. Then again, it was fun from an educational point of view. The context: the code is part of a program that writes spss system files (binary, .sav). These may be anything from a few hundred till millions of records. Spss knows to types of data: character and numerical. The code is only relevant for char data. If a 5M record dataset has 8 char variables, it means 40M executions of the posted code snippet. Or around 40 seconds devoted to padding. But in general there are fewer values. I'll use cProfile later to see if there are more urgent pain spots. This seemed a good candidate as the function is used so often. FWIW, here is the function, plus some of the init: def __init__(self, *args, *kwargs): ??????? self.strRange = range(1, MAXLENGTHS['SPSS_MAX_LONGSTRING'][0] + 1) ??????? self.pad_8_lookup = dict([(i, -8 * (i // -8)) for i in self.strRange]) ??? def writerow(self, record): ??????? """ This function writes one record, which is a Python list.""" ??????? convertedRecord = [] ??????? for value, varName in zip(record, self.varNames): ??????????? charlen = self.varNamesTypes[varName] ??????????? if charlen > 0: ??????????????? value = value.ljust( self.pad_8_lookup[charlen] ) ??????????? else: ??????????????? try: ??????????????????? value = float(value) ??????????????? except ValueError: ??????????????????? value = self.sysmis ??????????? convertedRecord.append(value) ??????? caseOut = struct.pack(self.structFmt, *convertedRecord) ??????? retcode = self.spssio.spssWholeCaseOut(self.fh, caseOut) ??????? if retcodes.get(retcode) != "SPSS_OK": ??????????? raise SPSSIOError("Problem writing row:\n" % " ".join(record), retcode) ??????? return retcode From bfishbein79 at gmail.com Sat Oct 6 21:00:07 2012 From: bfishbein79 at gmail.com (Benjamin Fishbein) Date: Sat, 6 Oct 2012 14:00:07 -0500 Subject: [Tutor] website is returning error when I post data Message-ID: Hello. This problem has got me stumped, and I've been trying to figure it out for more than a month. This program checks to see if a college buyback site is buying back particular books. I'm using mac OSX Here's the code. import urllib,urllib2 base_url="http://textbooks.com" action="/BuyBack-Search.php?CSID=AQ2ZJKUWKZJBSD2T2DDMKMKB" url=base_url+action name="bb_isbns" isbns="""0891917608 0307387895 0195070658 0137806922 074324754X\ 067149399x 0446556246 031604993X 0070322538""" data={name:isbns} encoded_data=urllib.urlencode(data) text=urllib2.urlopen(url,encoded_data).read() file_name="textbooks_trial.txt" file=open(file_name,"w") file.write(text) file.close() print "finished running program" print "check file 'textbooks_trial.txt'" When I go to the website(without using Python) and I paste the text (isbns) into the text box then enter it, it comes back with the error: Oops... there may be a mistake here. Sorry, your search could not be completed at this time. Please try again. (Error: BB-09-00-05) But the text I entered is still in the text entry box. When I click on the orange button ("Sell your Textbooks") a second time, it goes through. The problem is that when I try to do this through Python, it returns the code with the error, and there's no orange button to click. I tried submitting it again, hoping the second time would work, using the following code: import urllib,urllib2 text="there may be a mistake here" base_url="http://textbooks.com" action="/BuyBack-Search.php?CSID=AQ2ZJKUWKZJBSD2T2DDMKMKB" url=base_url+action name="bb_isbns" isbns="""0891917608 0307387895 0195070658 0137806922 074324754X\ 067149399x 0446556246 031604993X 0070322538""" data={name:isbns} encoded_data=urllib.urlencode(data) while "there may be a mistake here" in text: text=urllib2.urlopen(url,encoded_data).read() file_name="textbooks_trial.txt" file=open(file_name,"w") file.write(text) file.close() print "tried doing the program" print "finished running program" print "check file 'textbooks_trial.txt'" The result is an endless loop, where it prints out: "tried running the program" If anyone can figure out what I need to do, I'd be extremely grateful. Thanks, Ben -------------- next part -------------- An HTML attachment was scrubbed... URL: From selbyrowleycannon at ymail.com Sun Oct 7 00:16:14 2012 From: selbyrowleycannon at ymail.com (Selby Rowley-Cannon) Date: Sat, 6 Oct 2012 15:16:14 -0700 (PDT) Subject: [Tutor] Website form data input Message-ID: <1349561774.45118.YahooMailNeo@web140202.mail.bf1.yahoo.com> Hello,? ? ? I am aiming to write a program that inputs a list of codes into an HTML text field, one by one, entering the next code if it is incorrect, but stopping when the code is correct. I've got down putting the codes into a list, and 'for' looping though it (maybe 'while' the entered code is incorrect?) but I don't know how to enter text into a text field in a website. All help greatly?appreciated,? ? ? -Selby -------------- next part -------------- An HTML attachment was scrubbed... URL: From rdmoores at gmail.com Sun Oct 7 01:19:11 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Sat, 6 Oct 2012 16:19:11 -0700 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: Message-ID: On Fri, Oct 5, 2012 at 7:15 AM, Walter Prins wrote: > Does this hint help? > >>>> import datetime >>>> mydate = datetime.date(2012,10,5) >>>> mydate = mydate + datetime.timedelta(days=30) >>>> print mydate > 2012-11-04 Yes! Thanks to all for their rapid responses. But now I'm thinking it would be handy to not only know that, say, 500 days from today is 2014-02-18, but to know what day if the week that is. I suppose the calendar module is to be used for this, but I haven't been able to get it to work for me. So what day of the week IS 2014-02-18? The docs say calendar.weekday(year, month, day) Returns the day of the week (0 is Monday) for year (1970?...), month (1?12), day (1?31). >>> import calendar >>> calendar.weekday(2014, 2, 18) 1 That "1" means Tuesday, right? But how can I use calendar to print out that word, "TUESDAY"? Thanks Dick From d at davea.name Sun Oct 7 01:29:22 2012 From: d at davea.name (Dave Angel) Date: Sat, 06 Oct 2012 19:29:22 -0400 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: Message-ID: <5070BED2.4060501@davea.name> On 10/06/2012 07:19 PM, Richard D. Moores wrote: > On Fri, Oct 5, 2012 at 7:15 AM, Walter Prins wrote: > >> Does this hint help? >> >>>>> import datetime >>>>> mydate = datetime.date(2012,10,5) >>>>> mydate = mydate + datetime.timedelta(days=30) >>>>> print mydate >> 2012-11-04 > Yes! Thanks to all for their rapid responses. > > But now I'm thinking it would be handy to not only know that, say, 500 > days from today is 2014-02-18, but to know what day if the week that > is. I suppose the calendar module is to be used for this, but I > haven't been able to get it to work for me. So what day of the week IS > 2014-02-18? > > The docs say > calendar.weekday(year, month, day) > Returns the day of the week (0 is Monday) for year (1970?...), month > (1?12), day (1?31). > >>>> import calendar >>>> calendar.weekday(2014, 2, 18) > 1 > > That "1" means Tuesday, right? But how can I use calendar to print out > that word, "TUESDAY"? > > Thanks > To turn an integer (0-6, or whatever) into a string, just use a tuple of the same size: tran = ("MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY") i = 1 print tran[i] (prints "TUESDAY") Note that I'm not sure of the actual mapping of the integers coming out of the weekday function, so you might have to rearrange the strings above. -- DaveA From alan.gauld at btinternet.com Sun Oct 7 01:35:58 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 07 Oct 2012 00:35:58 +0100 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: Message-ID: On 07/10/12 00:19, Richard D. Moores wrote: > That "1" means Tuesday, right? But how can I use calendar to print out > that word, "TUESDAY"? days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") print '2014/2/18 is: ', days[calendar.weekday(2014, 2, 18)] HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sun Oct 7 01:41:57 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 07 Oct 2012 00:41:57 +0100 Subject: [Tutor] Website form data input In-Reply-To: <1349561774.45118.YahooMailNeo@web140202.mail.bf1.yahoo.com> References: <1349561774.45118.YahooMailNeo@web140202.mail.bf1.yahoo.com> Message-ID: On 06/10/12 23:16, Selby Rowley-Cannon wrote: > I am aiming to write a program that inputs a list of codes into an > HTML text field, ...but I don't know how to enter text into a text field in a > website. This is probably going to be much harder than you ever realized however.... You can use urllib to do this or one of the third party html processing libraries. But a lot depends on the exact structure of the web site. pyquery might be helpful too although I confess I've only looked at the docs and not actually used it yet.... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From breamoreboy at yahoo.co.uk Sun Oct 7 01:42:17 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 07 Oct 2012 00:42:17 +0100 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: Message-ID: On 07/10/2012 00:19, Richard D. Moores wrote: > On Fri, Oct 5, 2012 at 7:15 AM, Walter Prins wrote: > >> Does this hint help? >> >>>>> import datetime >>>>> mydate = datetime.date(2012,10,5) >>>>> mydate = mydate + datetime.timedelta(days=30) >>>>> print mydate >> 2012-11-04 > > Yes! Thanks to all for their rapid responses. > > But now I'm thinking it would be handy to not only know that, say, 500 > days from today is 2014-02-18, but to know what day if the week that > is. I suppose the calendar module is to be used for this, but I > haven't been able to get it to work for me. So what day of the week IS > 2014-02-18? > > The docs say > calendar.weekday(year, month, day) > Returns the day of the week (0 is Monday) for year (1970?...), month > (1?12), day (1?31). > >>>> import calendar >>>> calendar.weekday(2014, 2, 18) > 1 > > That "1" means Tuesday, right? But how can I use calendar to print out > that word, "TUESDAY"? > > Thanks > > Dick > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Use calendar.day_name. -- Cheers. Mark Lawrence. From eryksun at gmail.com Sun Oct 7 02:08:34 2012 From: eryksun at gmail.com (eryksun) Date: Sat, 6 Oct 2012 20:08:34 -0400 Subject: [Tutor] website is returning error when I post data In-Reply-To: References: Message-ID: On Sat, Oct 6, 2012 at 3:00 PM, Benjamin Fishbein wrote: > > This program checks to see if a college buyback site is buying back > particular books. > > action="/BuyBack-Search.php?CSID=AQ2ZJKUWKZJBSD2T2DDMKMKB" > > When I go to the website(without using Python) and I paste the text (isbns) > into the text box then enter it, it comes back with the error: > Oops... there may be a mistake here. > > Sorry, your search could not be completed at this time. Please try again. > (Error: BB-09-00-05) You'll need to use something like Firebug to examine the headers of the POST. CSID is based on the session. There's probably also one or more hidden fields. Consider using "mechanize": http://wwwsearch.sourceforge.net/mechanize Web scraping and automating forms is unreliable. A website isn't a documented, stable API. More importantly, scraping often violates the terms of service: http://www.textbooks.com/CustServ-Terms.php The foregoing licenses do not include any rights to: .... enable high volume, automated, electronic processes that apply to the Site or its systems; use any robot, spider, data miner, scraper or other automated means to access the Site or its systems for any purpose; .... compile, repackage, disseminate, or otherwise extract data from the Site. From eryksun at gmail.com Sun Oct 7 02:22:03 2012 From: eryksun at gmail.com (eryksun) Date: Sat, 6 Oct 2012 20:22:03 -0400 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: Message-ID: On Sat, Oct 6, 2012 at 7:19 PM, Richard D. Moores wrote: > > But now I'm thinking it would be handy to not only know that, say, 500 > days from today is 2014-02-18, but to know what day if the week that > is. >>> from datetime import date >>> date(2014, 2, 18).strftime("%A") 'Tuesday' http://docs.python.org/library/datetime#strftime-and-strptime-behavior From rdmoores at gmail.com Sun Oct 7 03:08:40 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Sat, 6 Oct 2012 18:08:40 -0700 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: Message-ID: On Sat, Oct 6, 2012 at 4:42 PM, Mark Lawrence wrote: > Use calendar.day_name. How? From rdmoores at gmail.com Sun Oct 7 03:10:57 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Sat, 6 Oct 2012 18:10:57 -0700 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: <5070BED2.4060501@davea.name> References: <5070BED2.4060501@davea.name> Message-ID: On Sat, Oct 6, 2012 at 4:29 PM, Dave Angel wrote: > To turn an integer (0-6, or whatever) into a string, just use a tuple of > the same size: > > tran = ("MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", > "SATURDAY", "SUNDAY") > i = 1 > print tran[i] > > (prints "TUESDAY") Why did you choose "tran"? Dick From steve at pearwood.info Sun Oct 7 03:13:29 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 07 Oct 2012 12:13:29 +1100 Subject: [Tutor] Website form data input In-Reply-To: <1349561774.45118.YahooMailNeo@web140202.mail.bf1.yahoo.com> References: <1349561774.45118.YahooMailNeo@web140202.mail.bf1.yahoo.com> Message-ID: <5070D739.4090400@pearwood.info> On 07/10/12 09:16, Selby Rowley-Cannon wrote: > Hello, > > I am aiming to write a program that inputs a list of codes into an > HTML text field, one by one, entering the next code if it is incorrect, >but stopping when the code is correct. I've got down putting the codes >into a list, and 'for' looping though it (maybe 'while' the entered code > is incorrect?) but I don't know how to enter text into a text field in a > website. How will you know that the code is correct? If you already know which is the correct code, why don't you just enter it the first time? If you *literally* want to enter text into a text field in a website, then you should look for the "mechanize" library, which should do what you want. If you want something else, you'll need to explain what. -- Steven From rdmoores at gmail.com Sun Oct 7 03:13:35 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Sat, 6 Oct 2012 18:13:35 -0700 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: Message-ID: On Sat, Oct 6, 2012 at 4:35 PM, Alan Gauld wrote: > On 07/10/12 00:19, Richard D. Moores wrote: > >> That "1" means Tuesday, right? But how can I use calendar to print out >> that word, "TUESDAY"? > > > days = ("Monday", > "Tuesday", > "Wednesday", > "Thursday", > "Friday", > "Saturday", > "Sunday") > > print '2014/2/18 is: ', days[calendar.weekday(2014, 2, 18)] I was thinking that that kind of thing would be built into calendar. Thanks. Dick From d at davea.name Sun Oct 7 03:42:01 2012 From: d at davea.name (Dave Angel) Date: Sat, 06 Oct 2012 21:42:01 -0400 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: <5070BED2.4060501@davea.name> Message-ID: <5070DDE9.3080506@davea.name> On 10/06/2012 09:10 PM, Richard D. Moores wrote: > On Sat, Oct 6, 2012 at 4:29 PM, Dave Angel wrote: > >> To turn an integer (0-6, or whatever) into a string, just use a tuple of >> the same size: >> >> tran = ("MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", >> "SATURDAY", "SUNDAY") >> i = 1 >> print tran[i] >> >> (prints "TUESDAY") > > Why did you choose "tran"? > > Dick > > tran was short for "translation table." It's also the name of a keyword I implemented in an interpreter over 35 years ago. Of course, Mark has pointed out that equivalent functionality is already in the calendar module i = 1 print calendar.day_name[i] day_name isn't a list or tuple, but it behaves enough like one for this purpose. -- DaveA From steve at pearwood.info Sun Oct 7 03:50:36 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 07 Oct 2012 12:50:36 +1100 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: Message-ID: <5070DFEC.5030504@pearwood.info> On 07/10/12 12:08, Richard D. Moores wrote: > On Sat, Oct 6, 2012 at 4:42 PM, Mark Lawrence wrote: > >> Use calendar.day_name. > > How? By reading the Fine Manual. http://docs.python.org/library/calendar.html#calendar.day_name which is so short that I can copy it here: calendar.day_abbr An array that represents the abbreviated days of the week in the current locale. Like other arrays, lists, tuples etc., you use it by getting the item at position n: py> import calendar py> calendar.day_name[1] 'Tuesday' -- Steven From steve at pearwood.info Sun Oct 7 04:02:48 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 07 Oct 2012 13:02:48 +1100 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: Message-ID: <5070E2C8.3020609@pearwood.info> On 07/10/12 12:13, Richard D. Moores wrote: > On Sat, Oct 6, 2012 at 4:35 PM, Alan Gauld wrote: >> On 07/10/12 00:19, Richard D. Moores wrote: >> >>> That "1" means Tuesday, right? But how can I use calendar to print out >>> that word, "TUESDAY"? >> >> >> days = ("Monday", >> "Tuesday", >> "Wednesday", >> "Thursday", >> "Friday", >> "Saturday", >> "Sunday") >> >> print '2014/2/18 is: ', days[calendar.weekday(2014, 2, 18)] > > I was thinking that that kind of thing would be built into calendar. Thanks. It is, and it is localised to whatever date system the user is using, which is a much better idea than forcing English dates. py> import calendar py> import locale py> locale.setlocale(locale.LC_ALL, 'fr_FR') # French 'fr_FR' py> calendar.day_name[1] 'mardi' py> locale.setlocale(locale.LC_ALL, 'de_DE') # German 'de_DE' py> calendar.day_name[1] 'Dienstag' py> calendar.day_name[4] 'Freitag' py> locale.setlocale(locale.LC_ALL, 'en_GB') # British English. 'en_GB' py> calendar.day_name[4] 'Friday' -- Steven From akleider at sonic.net Sun Oct 7 04:29:12 2012 From: akleider at sonic.net (akleider at sonic.net) Date: Sat, 6 Oct 2012 19:29:12 -0700 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: <5070BED2.4060501@davea.name> References: <5070BED2.4060501@davea.name> Message-ID: > On 10/06/2012 07:19 PM, Richard D. Moores wrote: >> On Fri, Oct 5, 2012 at 7:15 AM, Walter Prins wrote: >> >>> Does this hint help? >>> >>>>>> import datetime >>>>>> mydate = datetime.date(2012,10,5) >>>>>> mydate = mydate + datetime.timedelta(days=30) >>>>>> print mydate >>> 2012-11-04 >> Yes! Thanks to all for their rapid responses. >> >> But now I'm thinking it would be handy to not only know that, say, 500 >> days from today is 2014-02-18, but to know what day if the week that >> is. I suppose the calendar module is to be used for this, but I >> haven't been able to get it to work for me. So what day of the week IS >> 2014-02-18? >> >> The docs say >> calendar.weekday(year, month, day) >> Returns the day of the week (0 is Monday) for year (1970???...), month >> (1???12), day (1???31). >> >>>>> import calendar >>>>> calendar.weekday(2014, 2, 18) >> 1 >> >> That "1" means Tuesday, right? But how can I use calendar to print out >> that word, "TUESDAY"? >> >> Thanks >> > > To turn an integer (0-6, or whatever) into a string, just use a tuple of > the same size: > > tran = ("MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", > "SATURDAY", "SUNDAY") > i = 1 > print tran[i] > > (prints "TUESDAY") > > Note that I'm not sure of the actual mapping of the integers coming out > of the weekday function, so you might have to rearrange the strings above. I'm also not sure but I seem to remember that it is ("SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY") which I think is extremely clever because it gets around the problem created by the fact that some people (misguided in my view) begin the week with Sunday instead of ending with it. From rdmoores at gmail.com Sun Oct 7 04:37:39 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Sat, 6 Oct 2012 19:37:39 -0700 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: Message-ID: On Sat, Oct 6, 2012 at 5:22 PM, eryksun wrote: > >>> from datetime import date > >>> date(2014, 2, 18).strftime("%A") > 'Tuesday' > > http://docs.python.org/library/datetime#strftime-and-strptime-behavior Or for Python 3.3, . I remain bewildered. Where did these strangely named things come from, strftime and strptime? I see that >>> from datetime import date >>> date(2014, 2, 18).strftime("%A") 'Tuesday' gives me what I was after, but I need to understand it, and I understand very little of that section, "8.1.8. strftime() and strptime() Behavior". Take the first sentence in that section: "date, datetime, and time objects all support a strftime(format) method, to create a string representing the time under the control of an explicit format string. Broadly speaking, d.strftime(fmt) acts like the time module?s time.strftime(fmt, d.timetuple()) although not all objects support a timetuple() method." Total gibberish. I feel like I've hit a brick wall. Where can I go to learn to understand it? I need some very basic, specific information. Dick From rdmoores at gmail.com Sun Oct 7 04:49:37 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Sat, 6 Oct 2012 19:49:37 -0700 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: <5070DFEC.5030504@pearwood.info> References: <5070DFEC.5030504@pearwood.info> Message-ID: On Sat, Oct 6, 2012 at 6:50 PM, Steven D'Aprano wrote: > On 07/10/12 12:08, Richard D. Moores wrote: >> >> On Sat, Oct 6, 2012 at 4:42 PM, Mark Lawrence >> wrote: >> >>> Use calendar.day_name. >> >> >> How? > > > By reading the Fine Manual. > > http://docs.python.org/library/calendar.html#calendar.day_name > > which is so short that I can copy it here: > > calendar.day_abbr > An array that represents the abbreviated days of the week in > the current locale. > > > Like other arrays, lists, tuples etc., you use it by getting the > item at position n: > > py> import calendar > py> calendar.day_name[1] > 'Tuesday' Of course, I see now that I'd made a really stupid mistake. I used parentheses instead of brackets: >>> import calendar >>> calendar.day_name(1) Traceback (most recent call last): File "", line 1, in builtins.TypeError: '_localized_day' object is not callable Thanks, Dick From eryksun at gmail.com Sun Oct 7 05:42:23 2012 From: eryksun at gmail.com (eryksun) Date: Sat, 6 Oct 2012 23:42:23 -0400 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: Message-ID: On Sat, Oct 6, 2012 at 10:37 PM, Richard D. Moores wrote: > On Sat, Oct 6, 2012 at 5:22 PM, eryksun wrote: > >> >>> from datetime import date >> >>> date(2014, 2, 18).strftime("%A") >> 'Tuesday' >> >> http://docs.python.org/library/datetime#strftime-and-strptime-behavior > > I remain bewildered. Where did these strangely named things come from, > strftime and strptime? I see that These are named for the C standard library functions for [f]ormatting and [p]arsing broken-down [time] structures to and from [str]ings. For examples, see the GNU libc docs: http://www.gnu.org/software/libc/manual/html_node/Calendar-Time.html I tend to agree that in Python the names seem... oddly wonkish, and the reliance on terse formatting codes seems... oddly low-level. You just have to get used to it, like working with regular expressions and string formatting. Or use (or write your own) higher-level APIs such as calendar.day_name, which uses datetime.date.strftime: class _localized_day: # January 1, 2001, was a Monday. _days = [datetime.date(2001, 1, i+1).strftime for i in range(7)] def __init__(self, format): self.format = format def __getitem__(self, i): funcs = self._days[i] if isinstance(i, slice): return [f(self.format) for f in funcs] else: return funcs(self.format) def __len__(self): return 7 day_name = _localized_day('%A') Notice day_name is initialized with the "%A" format code, and _localized_day has a class attribute "_days" that's a list of 7 strftime bound methods. It uses these in __getitem__ to format a single day or a slice of several days: >>> calendar.day_name[:3] ['Monday', 'Tuesday', 'Wednesday'] > "date, datetime, and time objects all support a strftime(format) > method, to create a string representing the time under the control of > an explicit format string. Broadly speaking, d.strftime(fmt) acts like > the time module?s time.strftime(fmt, d.timetuple()) although not all > objects support a timetuple() method." > > Total gibberish. I feel like I've hit a brick wall. Where can I go to > learn to understand it? I need some very basic, specific information. They're comparing the datetime methods to the similarly-named methods in the time module, such as time.strftime: http://docs.python.org/library/time#time.strftime which formats an instance of time.struct_time: http://docs.python.org/library/time#time.struct_time datetime.date.timetuple() and datetime.datetime.timetuple() both return an instance of time.struct_time: http://docs.python.org/library/datetime#datetime.date.timetuple http://docs.python.org/library/datetime#datetime.datetime.timetuple Compare struct_time to a libc broken-down time structure: http://www.gnu.org/software/libc/manual/html_node/Broken_002ddown-Time.html From brian.van.den.broek at gmail.com Sun Oct 7 05:50:10 2012 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Sat, 6 Oct 2012 23:50:10 -0400 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: Message-ID: On 6 Oct 2012 22:40, "Richard D. Moores" wrote: > I remain bewildered. Where did these strangely named things come from, > strftime and strptime? I see that Hi Dick, These names carry over from well entrentched names from C. My guess is format time and print time are what they are supposed to suggest. > >>> from datetime import date > >>> date(2014, 2, 18).strftime("%A") > 'Tuesday' > > gives me what I was after, but I need to understand it, and I > understand very little of that section, "8.1.8. strftime() and > strptime() Behavior". > > Take the first sentence in that section: > "date, datetime, and time objects all support a strftime(format) > method, to create a string representing the time under the control of > an explicit format string. Broadly speaking, d.strftime(fmt) acts like > the time module?s time.strftime(fmt, d.timetuple()) although not all > objects support a timetuple() method." > > Total gibberish. I feel like I've hit a brick wall. Where can I go to > learn to understand it? I need some very basic, specific information. I expect your speaking from a place of frustration. I don't think this is the high point of the docs, but it isn't so bad as that. The strftime method of a datetime object uses a mechanism similar to string formatting to create strings displaying data from the datetime object. The docs for datetime don't provide the details, pointing instead to the docs for the very similar method of the time module. I'm on my phone so cannot easily show an example but if d is a datetime.datetime instance, d.strftime('%Y') will produce a string of d's year. Consult the time docs and see if you can get somewhere. If not, post again. Best, Brian vdB -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sun Oct 7 05:56:34 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 07 Oct 2012 14:56:34 +1100 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: Message-ID: <5070FD72.4040403@pearwood.info> On 07/10/12 13:37, Richard D. Moores wrote: > On Sat, Oct 6, 2012 at 5:22 PM, eryksun wrote: > >> >>> from datetime import date >> >>> date(2014, 2, 18).strftime("%A") >> 'Tuesday' >> >> http://docs.python.org/library/datetime#strftime-and-strptime-behavior > > Or for Python 3.3, > . > > I remain bewildered. Where did these strangely named things come from, > strftime and strptime? The C programming language on Unix systems. It is a little-known fact that Unix sys admins, and C programmers, can only type a fixed number of keys before their brains explode. Sad but true. Since nobody knows how many keys that will be, but only that it is fixed at birth, they have a horror of typing four characters when two would do. Hence the standard Unix directories /usr and /mnt instead of /user and /mount, and the Unix commands: "cp" instead of "copy" "ls" instead of "list" "rm" instead of "remove" "umount" instead of "unmount" "chown" and "chgrp" instead of "change owner" and "change group" to mention only a few. This is also why the usual answer to any question asked of a Unix sys admin is likely to be "RTFM", which is best translated as: "Everything you ask can be found in the fine instruction manual or help files already available to you, or by searching on the Internet. Although it may take you five or six months of study to understand it, and I could explain it to you in a few sentences, I fear that my brain will explode halfway through and then you won't get your answer." Hence strftime and strptime for "string format time" and "string parse time". > I see that > >>>> from datetime import date >>>> date(2014, 2, 18).strftime("%A") > 'Tuesday' > > gives me what I was after, but I need to understand it, and I > understand very little of that section, "8.1.8. strftime() and > strptime() Behavior". The thing to remember is that strftime and strptime are essentially mini-programming languages themselves, a little like regular expressions. Another way of thinking about them is as similar to Excel's custom number formats, only more extensive. py> date(2014, 2, 18).strftime("Today is %A %d of %B in the year %Y.") 'Today is Tuesday 18 of February in the year 2014.' You can find a (partial?) list of format specifiers here: http://www.faximum.com/manual.d/client.server.d/manpages.23.html strptime handles the reverse process: py> time.strptime("Today is Tuesday 18 of February in the year 2014.", ... "Today is %A %d of %B in the year %Y.") time.struct_time(tm_year=2014, tm_mon=2, tm_mday=18, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=49, tm_isdst=-1) > Take the first sentence in that section: > "date, datetime, and time objects all support a strftime(format) > method, to create a string representing the time under the control of > an explicit format string. Broadly speaking, d.strftime(fmt) acts like > the time module?s time.strftime(fmt, d.timetuple()) although not all > objects support a timetuple() method." > > Total gibberish. I feel like I've hit a brick wall. Where can I go to > learn to understand it? I need some very basic, specific information. Start at the beginning: Python has date, datetime and time objects, right? py> import datetime py> datetime.date, datetime.time, datetime.datetime (, , ) All three of those objects define a method "strftime" which takes a format code, like "%a %d of %b, %Y", and converts the date/time/datetime object into a string using that format. Whatever d is (a date, a time, a datetime), calling d.strftime with an explicit format string is like extracting the time fields into a tuple: tmp = d.timetuple() # if this actually exists and then formatting it using the master function: time.strftime(format_string, tmp) only more convenient. -- Steven From sander.sweers at gmail.com Sun Oct 7 13:12:39 2012 From: sander.sweers at gmail.com (Sander Sweers) Date: Sun, 7 Oct 2012 13:12:39 +0200 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: <5070BED2.4060501@davea.name> Message-ID: Op 7 okt. 2012 04:29 schreef het volgende: > I'm also not sure but I seem to remember that it is > ("SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", > "SATURDAY", "SUNDAY") > which I think is extremely clever because it gets around the problem > created by the fact that some people (misguided in my view) begin the week > with Sunday instead of ending with it. This has religious reasons, see http://en.wikipedia.org/wiki/Week#Systems_derived_from_the_seven-day_week. Notable exception to the rule that Sunday is the first day of the week in europe is Italy, they consider Monday the first day of the week. Greets Sander -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Sun Oct 7 14:41:34 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 07 Oct 2012 13:41:34 +0100 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: <5070DFEC.5030504@pearwood.info> References: <5070DFEC.5030504@pearwood.info> Message-ID: On 07/10/2012 02:50, Steven D'Aprano wrote: > On 07/10/12 12:08, Richard D. Moores wrote: >> On Sat, Oct 6, 2012 at 4:42 PM, Mark >> Lawrence wrote: >> >>> Use calendar.day_name. >> >> How? > > By reading the Fine Manual. > > http://docs.python.org/library/calendar.html#calendar.day_name > > which is so short that I can copy it here: > > calendar.day_abbr > An array that represents the abbreviated days of the week in > the current locale. May I suggest a vist to the opticians. Or is this another test to see that we're all paying attention? :) > > > Like other arrays, lists, tuples etc., you use it by getting the > item at position n: > > py> import calendar > py> calendar.day_name[1] > 'Tuesday' > > > -- Cheers. Mark Lawrence. From wayne at waynewerner.com Sun Oct 7 14:54:20 2012 From: wayne at waynewerner.com (Wayne Werner) Date: Sun, 7 Oct 2012 07:54:20 -0500 (CDT) Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: <5070FD72.4040403@pearwood.info> References: <5070FD72.4040403@pearwood.info> Message-ID: On Sun, 7 Oct 2012, Steven D'Aprano wrote: > It is a little-known fact that Unix sys admins, and C programmers, can > only type a fixed number of keys before their brains explode. Sad but > true. Since nobody knows how many keys that will be, but only that it is > fixed at birth, they have a horror of typing four characters when two > would do. lol. I really did, too! +1 (I think this might apply to COBOL programmers, too.) -Wayne From steve at pearwood.info Sun Oct 7 16:04:34 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 08 Oct 2012 01:04:34 +1100 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: <5070DFEC.5030504@pearwood.info> Message-ID: <50718BF2.9060909@pearwood.info> On 07/10/12 23:41, Mark Lawrence wrote: > On 07/10/2012 02:50, Steven D'Aprano wrote: >> On 07/10/12 12:08, Richard D. Moores wrote: >>> On Sat, Oct 6, 2012 at 4:42 PM, Mark >>> Lawrence wrote: >>> >>>> Use calendar.day_name. >>> >>> How? >> >> By reading the Fine Manual. >> >> http://docs.python.org/library/calendar.html#calendar.day_name >> >> which is so short that I can copy it here: >> >> calendar.day_abbr >> An array that represents the abbreviated days of the week in >> the current locale. > > May I suggest a vist to the opticians. Or is this another test to see that we're all paying attention? :) Hah, good catch! Sorry about that. -- Steven From alan.gauld at btinternet.com Sun Oct 7 16:21:18 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 07 Oct 2012 15:21:18 +0100 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: <5070FD72.4040403@pearwood.info> Message-ID: On 07/10/12 13:54, Wayne Werner wrote: >> fixed at birth, they have a horror of typing four characters when two >> would do. > > (I think this might apply to COBOL programmers, too.) > -Wayne Oh no, COBOL programmers are the opposite. The more characters they type the longer they live.... It's a matter of honour to the COBOL hacker to type in at least a thousand lines of code per day! (That'd be equivalent to about 100 if they were using Python!) compare: C programmer: c = 0; c += n; COBOL PROGRAMMER: MOVE 0 TO COUNTER, ADD AMOUNT TO COUNTER :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From chigga101 at gmail.com Sun Oct 7 19:13:55 2012 From: chigga101 at gmail.com (Matthew Ngaha) Date: Sun, 7 Oct 2012 18:13:55 +0100 Subject: [Tutor] [Tkinter-discuss] whats Style() In-Reply-To: References: <20121007174832.bf0021f3.klappnase@web.de> Message-ID: sorry i sent the email directly by mistake. Just like to say thanks once again for all the help -------------- next part -------------- An HTML attachment was scrubbed... URL: From chigga101 at gmail.com Sun Oct 7 19:14:55 2012 From: chigga101 at gmail.com (Matthew Ngaha) Date: Sun, 7 Oct 2012 18:14:55 +0100 Subject: [Tutor] [Tkinter-discuss] whats Style() In-Reply-To: References: <20121007174832.bf0021f3.klappnase@web.de> Message-ID: > > sorry i sent the email directly by mistake. Just like to say thanks once > again for all the help > this email was a mistake, meant for anything mailing section.. please ignore it -------------- next part -------------- An HTML attachment was scrubbed... URL: From arnejd5 at gmail.com Sun Oct 7 19:46:21 2012 From: arnejd5 at gmail.com (Arnej Duranovic) Date: Sun, 7 Oct 2012 12:46:21 -0500 Subject: [Tutor] string rules for 'number' Message-ID: When I type this in the python idle shell ( version 3...) : '0' <= '10' <= '9' The interpreter evaluates this as true, WHY? 10 is greater than 0 but not 9 Notice I am not using the actual numbers, they are strings...I thought that numbers being string were ordered by their numerical value but obviously they are not? Can anyone explain this to me and explain how strings with numbers in them are ordered? Thx in advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Sun Oct 7 19:56:38 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sun, 7 Oct 2012 13:56:38 -0400 Subject: [Tutor] string rules for 'number' In-Reply-To: References: Message-ID: On Sun, Oct 7, 2012 at 1:46 PM, Arnej Duranovic wrote: > When I type this in the python idle shell ( version 3...) : > '0' <= '10' <= '9' > The interpreter evaluates this as true, WHY? 10 is greater than 0 but not 9 > Notice I am not using the actual numbers, they are strings...I thought that > numbers being string were ordered by their numerical value but obviously > they are > not? Can anyone explain this to me and explain how strings with numbers in > them are ordered? > > Thx in advance. > because '0' is less than '1', and '1' is less than '9' The comparison is done on a character by character basis -- Joel Goldstick From robertvstepp at gmail.com Sun Oct 7 19:59:21 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 7 Oct 2012 12:59:21 -0500 Subject: [Tutor] string rules for 'number' In-Reply-To: References: Message-ID: On Oct 7, 2012 12:47 PM, "Arnej Duranovic" wrote: > > When I type this in the python idle shell ( version 3...) : > '0' <= '10' <= '9' > The interpreter evaluates this as true, WHY? 10 is greater than 0 but not 9 Since they are strings it looks at these character by character. Since '0' < '1' < '9' , the 0 in '10' has no effect on the order. Compare 'a' < 'ba' < 'i' . boB -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Sun Oct 7 20:20:26 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 07 Oct 2012 19:20:26 +0100 Subject: [Tutor] string rules for 'number' In-Reply-To: References: Message-ID: On 07/10/2012 18:46, Arnej Duranovic wrote: > When I type this in the python idle shell ( version 3...) : > '0' <= '10' <= '9' > The interpreter evaluates this as true, WHY? 10 is greater than 0 but not 9 > Notice I am not using the actual numbers, they are strings...I thought that > numbers being string were ordered by their numerical value but obviously > they are > not? Can anyone explain this to me and explain how strings with numbers in > them are ordered? You thought wrong :) A string is a string is a string. They might look like numbers here but that's completely irrelevant. They'll be compared lexicographically, something I'm not inclined to attempt to explain so see here http://en.wikipedia.org/wiki/Lexicographical_order Please also be careful with your terminology. Note that I've used compared. Ordered is very different, e.g. FIFO is often used for first in, first out. > > Thx in advance. > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Cheers. Mark Lawrence. From roel at roelschroeven.net Sun Oct 7 21:19:49 2012 From: roel at roelschroeven.net (Roel Schroeven) Date: Sun, 07 Oct 2012 21:19:49 +0200 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: <5070BED2.4060501@davea.name> Message-ID: Sander Sweers schreef: > > Op 7 okt. 2012 04:29 schreef > het volgende: > > I'm also not sure but I seem to remember that it is > > ("SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", > > "SATURDAY", "SUNDAY") > > which I think is extremely clever because it gets around the problem > > created by the fact that some people (misguided in my view) begin the > week > > with Sunday instead of ending with it. > > This has religious reasons, see > http://en.wikipedia.org/wiki/Week#Systems_derived_from_the_seven-day_week. > Notable exception to the rule that Sunday is the first day of the week > in europe is Italy, they consider Monday the first day of the week. As far as I know, monday is generally considered the first day of the week here in Belgium. I never knew it was different in other European countries; I thought it was America that starts the week on sunday. Was I wrong then? -- "Too often we hold fast to the cliches of our forebears. We subject all facts to a prefabricated set of interpretations. Too often we enjoy the comfort of opinion without the discomfort of thought." -- John F Kennedy roel at roelschroeven.net From sander.sweers at gmail.com Sun Oct 7 22:12:15 2012 From: sander.sweers at gmail.com (Sander Sweers) Date: Sun, 07 Oct 2012 22:12:15 +0200 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: <5070BED2.4060501@davea.name> Message-ID: <1349640735.17669.8.camel@infirit.lan> Roel Schroeven schreef op zo 07-10-2012 om 21:19 [+0200]: > Sander Sweers schreef: > > > > Op 7 okt. 2012 04:29 schreef > > het volgende: > > > I'm also not sure but I seem to remember that it is > > > ("SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", > > > "SATURDAY", "SUNDAY") > > > which I think is extremely clever because it gets around the problem > > > created by the fact that some people (misguided in my view) begin the > > week > > > with Sunday instead of ending with it. > > > > This has religious reasons, see > > http://en.wikipedia.org/wiki/Week#Systems_derived_from_the_seven-day_week. > > Notable exception to the rule that Sunday is the first day of the week > > in europe is Italy, they consider Monday the first day of the week. > > As far as I know, monday is generally considered the first day of the > week here in Belgium. I never knew it was different in other European > countries; I thought it was America that starts the week on sunday. Was > I wrong then? As far as I know also in Belgium Sunday is officially the first day of the week. Look at the calendar and check what is the leftmost day. My guess this is the same as your northern neighbor, Sunday ;-). But to be fair, _practically_ we both consider Monday the first day of the week. Greets Sander From steve at pearwood.info Mon Oct 8 00:40:52 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 08 Oct 2012 09:40:52 +1100 Subject: [Tutor] string rules for 'number' In-Reply-To: References: Message-ID: <507204F4.10805@pearwood.info> On 08/10/12 04:46, Arnej Duranovic wrote: > When I type this in the python idle shell ( version 3...) : > '0'<= '10'<= '9' > The interpreter evaluates this as true, WHY? 10 is greater than 0 but not 9 > Notice I am not using the actual numbers, they are strings...I thought that > numbers being string were ordered by their numerical value but obviously > they are not? Others have already explained that strings are ordered on a character-by- character basis, not by numeric value. But I'm interested to ask how you came to the conclusion that they were ordered by numeric value. Was there something you read somewhere that gave you that (incorrect) impression, perhaps something in the tutorial or the reference manual? If so, please tell us, and we'll have it fixed. -- Steven From estebaniza at gmail.com Mon Oct 8 00:49:58 2012 From: estebaniza at gmail.com (Esteban Izaguirre) Date: Mon, 8 Oct 2012 00:49:58 +0200 Subject: [Tutor] modulo Message-ID: Hi, I'm following coursera's learn to program: the fundamentals, which teaches programming basics in python. Our first assignement involves the modulo operator with a negative divident, and while I've managed to get to understand it enough for the purposes of the assignement with help from othe rstudents, I still don't know how the hell it works, I wouldn't know how to use modulo in another situation if it ever arised. So, i undertand how modulo works when only positive numbers are used, but how does modulo determine, that, say -15 % 14 is equal to 13? Or -20 % 100 is 20? I just don't get how modulo works, all explanations I've found online only seem to be in relation of how this applies to perl or something, can someone explain it to me? -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Mon Oct 8 00:56:35 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 08 Oct 2012 09:56:35 +1100 Subject: [Tutor] string rules for 'number' In-Reply-To: References: Message-ID: <507208A3.6090104@pearwood.info> On 08/10/12 05:20, Mark Lawrence wrote: [...] > They'll be compared lexicographically, something I'm not inclined >to attempt to explain so see here >http://en.wikipedia.org/wiki/Lexicographical_order > Please also be careful with your terminology. Note that I've used >compared. Ordered is very different, e.g. FIFO is often used for >first in, first out. Actually ordered is perfectly fine in this context. Notice that the page you link to is called lexicographical ORDER. "Compared" is a verb and refers to the act of examining the items in some sense. There are many different comparisons in Python: < > <= >= == != `is` `is not`. Ordered can mean one of two things: - that the items in question are *capable* of being placed into some order e.g. numbers can be ordered by value; complex numbers cannot; - that the items in question actually *have been* ordered. "Some order" includes: numeric order, date order, lexicographical order, insertion order, even random order! You may be conflating this with the difference between "ordered dict" and "sorted dict", where the order referred to in the first case is insertion order rather than sorted order. -- Steven From d at davea.name Mon Oct 8 01:07:15 2012 From: d at davea.name (Dave Angel) Date: Sun, 07 Oct 2012 19:07:15 -0400 Subject: [Tutor] modulo In-Reply-To: References: Message-ID: <50720B23.8090801@davea.name> On 10/07/2012 06:49 PM, Esteban Izaguirre wrote: > Hi, I'm following coursera's learn to program: the fundamentals, which > teaches programming basics in python. Our first assignement involves the > modulo operator with a negative divident, and while I've managed to get to > understand it enough for the purposes of the assignement with help from > othe rstudents, I still don't know how the hell it works, I wouldn't know > how to use modulo in another situation if it ever arised. So, i undertand > how modulo works when only positive numbers are used, but how does modulo > determine, that, say -15 % 14 is equal to 13? Or -20 % 100 is 20? I just > don't get how modulo works, all explanations I've found online only seem to > be in relation of how this applies to perl or something, can someone > explain it to me? > > There is one perfectly reasonable definition for how modulo should behave if the denominator (modulus) is positive. Python does it 'right', so I'll try to explain it in a couple of ways. (If the modulus (denominator) is negative, it makes no sense to me, so I can't even recall what Python does, and I have to look it up each time. Fortunately this is rare, and in my code, I just avoid it) Let's suppose we're talking about the days of the week, they repeat every 7 days, so it's perfectly reasonable to do arithmetic modulo 7. Just considering the nonnegative numbers, we can take them as a strip, and wrap them around a cylinder of circumference 7. So 7, 14, and 21, etc. wind up on top of 0, and they are all equal to zero, modulo 7. Likewise the 8, 15, 22, are congruent to 1, and so on. That's the part you said made sense. One way to describe it is you subtract whatever multiple of 7 you like, till you get to a value between 0 and 6. So what about negative dividends? Instead of subtracting multiples of 7, you add multiples of 7. So if you have -5, you add 7 and you get 2. -5 % 7 should yield 2, and it does. If you consider that strip representing the number line, and include the negative numbers as it's wrapped around the cylinder, clearly -1 will line up with 6, and -2 with 5, and so on. That's all informal. More formally, there are two related operations, the floor function and the mod function. In Python there's a function divmod() that gives you both results. The relationship between the floor, the modulo, the modulus (eg. 7) and the dividend is defined as: floor * modulus + modulo === dividend, and modulo is between 0 and modulus-1, inclusive. For negative modulus in python, I'd rather not go there, but I will if you specifically ask. And if you understood all the above. -- DaveA From steve at pearwood.info Mon Oct 8 01:12:06 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 08 Oct 2012 10:12:06 +1100 Subject: [Tutor] modulo In-Reply-To: References: Message-ID: <50720C46.30104@pearwood.info> Hello Esteban and welcome! On 08/10/12 09:49, Esteban Izaguirre wrote: > So, i undertand > how modulo works when only positive numbers are used, but how does modulo > determine, that, say -15 % 14 is equal to 13? Think of modulo as almost exactly the same as "remainder after division". When you say "17 % 5", 5 goes into 17 three times with remainder two, so 17 % 5 returns 2. With negative numbers it works the same way, but with a twist: we could say EITHER: a) 5 goes into -17 negative-three times with remainder negative-two; b) 5 goes into -17 negative-four times with remainder three. Why? Because we can write either of these: 5*-3 + -2 = -17 5*-4 + 3 = -17 So we have a choice. In Python's case, the people who invented the language chose the second one, b). That way the modulo is always positive or zero and never negative, which is a useful thing to have. So, back to your two examples: -2*14 + 13 = -15 so fourteen goes into negative-fifteen -2 times, with 13 remaining. = -20 -20 % 100 is 80, not 20 like you said: -1*100 + 80 = -20 so 100 goes into negative-twenty -1 times, with 80 remaining. -- Steven Or -20 % 100 is 20? I just > don't get how modulo works, all explanations I've found online only seem to > be in relation of how this applies to perl or something, can someone > explain it to me? > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From oscar.j.benjamin at gmail.com Mon Oct 8 01:16:57 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 8 Oct 2012 00:16:57 +0100 Subject: [Tutor] modulo In-Reply-To: <50720B23.8090801@davea.name> References: <50720B23.8090801@davea.name> Message-ID: On 8 October 2012 00:07, Dave Angel wrote: > On 10/07/2012 06:49 PM, Esteban Izaguirre wrote: >> Hi, I'm following coursera's learn to program: the fundamentals, which >> teaches programming basics in python. Our first assignement involves the >> modulo operator with a negative divident, and while I've managed to get to >> understand it enough for the purposes of the assignement with help from >> othe rstudents, I still don't know how the hell it works, I wouldn't know >> how to use modulo in another situation if it ever arised. So, i undertand >> how modulo works when only positive numbers are used, but how does modulo >> determine, that, say -15 % 14 is equal to 13? Or -20 % 100 is 20? I just >> don't get how modulo works, all explanations I've found online only seem to >> be in relation of how this applies to perl or something, can someone >> explain it to me? >> >> > > There is one perfectly reasonable definition for how modulo should > behave if the denominator (modulus) is positive. Python does it 'right', > so I'll try to explain it in a couple of ways. > > (If the modulus (denominator) is negative, it makes no sense to me, so I > can't even recall what Python does, and I have to look it up each time. > Fortunately this is rare, and in my code, I just avoid it) The sign of the modulo operation is always the same as the sign of the denominator: >>> 3%5 3 >>> 3%(-5) -2 >>> (-3)%5 2 >>> (-3)%(-5) -3 That way you can say that the result of the a % b is always in the range from 0 to b (not including b itself). Oscar From d at davea.name Mon Oct 8 01:24:22 2012 From: d at davea.name (Dave Angel) Date: Sun, 07 Oct 2012 19:24:22 -0400 Subject: [Tutor] modulo In-Reply-To: References: <50720B23.8090801@davea.name> Message-ID: <50720F26.9000200@davea.name> On 10/07/2012 07:16 PM, Oscar Benjamin wrote: > On 8 October 2012 00:07, Dave Angel wrote: >> On 10/07/2012 06:49 PM, Esteban Izaguirre wrote: >>> Hi, I'm following coursera's learn to program: the fundamentals, which >>> teaches programming basics in python. Our first assignement involves the >>> modulo operator with a negative divident, and while I've managed to get to >>> understand it enough for the purposes of the assignement with help from >>> othe rstudents, I still don't know how the hell it works, I wouldn't know >>> how to use modulo in another situation if it ever arised. So, i undertand >>> how modulo works when only positive numbers are used, but how does modulo >>> determine, that, say -15 % 14 is equal to 13? Or -20 % 100 is 20? I just >>> don't get how modulo works, all explanations I've found online only seem to >>> be in relation of how this applies to perl or something, can someone >>> explain it to me? >>> >>> >> >> There is one perfectly reasonable definition for how modulo should >> behave if the denominator (modulus) is positive. Python does it 'right', >> so I'll try to explain it in a couple of ways. >> >> (If the modulus (denominator) is negative, it makes no sense to me, so I >> can't even recall what Python does, and I have to look it up each time. >> Fortunately this is rare, and in my code, I just avoid it) > > The sign of the modulo operation is always the same as the sign of the > denominator: > > >>>> 3%5 > 3 >>>> 3%(-5) > -2 >>>> (-3)%5 > 2 >>>> (-3)%(-5) > -3 > > > That way you can say that the result of the a % b is always in the > range from 0 to b (not including b itself). > > It still makes no sense to me. There are at least two equally silly ways to define the results of a negative modulus, and you've properly described one of them, presumably the one that Python implements. But I've used and abused about 35 languages over the years, and each makes its own choice for this. I'd rather just call it undefined, and eliminate it. That's what we did when the hardware guys couldn't decide how the hardware was going to respond to a particular microcode bit pattern. They documented it as undefined, and I made it illegal in the microcode assembler. Fortunately, the OP isn't asking about this case, which is the other reason I didn't bother to describe what Python does. -- DaveA From d at davea.name Mon Oct 8 02:06:41 2012 From: d at davea.name (Dave Angel) Date: Sun, 07 Oct 2012 20:06:41 -0400 Subject: [Tutor] modulo In-Reply-To: <4BC0A6C2-0494-44D9-8CFC-705D5DDE3579@me.com> References: <50720B23.8090801@davea.name> <50720F26.9000200@davea.name> <4BC0A6C2-0494-44D9-8CFC-705D5DDE3579@me.com> Message-ID: <50721911.4060005@davea.name> On 10/07/2012 08:00 PM, Jan Karel Schreuder wrote: > > > On Oct 7, 2012, at 7:24 PM, Dave Angel wrote: > >>>>> >>>> >> >> It still makes no sense to me. There are at least two equally silly >> ways to define the results of a negative modulus, and you've properly >> described one of them, presumably the one that Python implements. >> >> But I've used and abused about 35 languages over the years, and each >> makes its own choice for this. I'd rather just call it undefined, and >> eliminate it. That's what we did when the hardware guys couldn't decide >> how the hardware was going to respond to a particular microcode bit >> pattern. They documented it as undefined, and I made it illegal in the >> microcode assembler. >> >> Fortunately, the OP isn't asking about this case, which is the other >> reason I didn't bother to describe what Python does. >> >> >> >> -- >> >> DaveA >> _______________________________________________ >> I'm not a professional programmer, so I might be way off base here. But what I like about Pythons modulo solution is that I can use it to right and left shift in lists or tuples, and I will link to the first element when I right shift past the last element and link to the last element when I left shift past the first element. In other words I can consider the last as a chain where the last and the first element are connected. This I find useful in surprisingly many situations. > > Certainly, but you've never had to do that with lists or tuples having negative lengths. It's a negative modulus that I'm complaining about. -- DaveA From arnejd5 at gmail.com Mon Oct 8 02:51:04 2012 From: arnejd5 at gmail.com (Arnej Duranovic) Date: Sun, 7 Oct 2012 19:51:04 -0500 Subject: [Tutor] string rules for 'number' In-Reply-To: <507208A3.6090104@pearwood.info> References: <507208A3.6090104@pearwood.info> Message-ID: Alright guys, I appreciate all your help SO much. I know understand, as the gentleman above said " A string is a string is a string" doesn't matter what is in it and they are ordered the same way...BUT this is what was going through my head. Since letters are ordered in such a way that A is less than B, for example, I thought the same applied to numbers, I was very very wrong, as you guys have pointed out. I did not read it anywhere, it was just that logic was going through my head when I was writing the code and when it said that '10' is less than '9' I was like... WUT? But thanks again for all your help, I understand how STRINGS are ordered now :P -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Mon Oct 8 04:19:47 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 7 Oct 2012 22:19:47 -0400 Subject: [Tutor] string rules for 'number' In-Reply-To: References: Message-ID: On Sun, Oct 7, 2012 at 1:46 PM, Arnej Duranovic wrote: > > When I type this in the python idle shell ( version 3...) : > '0' <= '10' <= '9' > The interpreter evaluates this as true, WHY? 10 is greater than 0 but not 9 > Notice I am not using the actual numbers, they are strings...I thought that > numbers being string were ordered by their numerical value but obviously > they are not? As a supplement to what's already been stated about string comparisons, here's a possible solution if you need a more 'natural' sort order such as '1', '5', '10', '50', '100'. You can use a regular expression to split the string into a list of (digits, nondigits) tuples (mutually exclusive) using re.findall. For example: >>> import re >>> dndre = re.compile('([0-9]+)|([^0-9]+)') >>> re.findall(dndre, 'a1b10') [('', 'a'), ('1', ''), ('', 'b'), ('10', '')] Use a list comprehension to choose either int(digits) if digits is non-empty else nondigits for each item. For example: >>> [int(d) if d else nd for d, nd in re.findall(dndre, 'a1b10')] ['a', 1, 'b', 10] Now you have a list of strings and integers that will sort 'naturally' compared to other such lists, since they compare corresponding items starting at index 0. All that's left to do is to define this operation as a key function for use as the "key" argument of sort/sorted. For example: import re def natural(item, dndre=re.compile('([0-9]+)|([^0-9]+)')): if isinstance(item, str): item = [int(d) if d else nd for d, nd in re.findall(dndre, item.lower())] return item The above transforms all strings into a list of integers and lowercase strings (if you don't want letter case to affect sort order). In Python 2.x, use "basestring" instead of "str". If you're working with bytes in Python 3.x, make sure to first decode() the items before sorting since the regular expression is only defined for strings. Regular sort: >>> sorted(['s1x', 's10x', 's5x', 's50x', 's100x']) ['s100x', 's10x', 's1x', 's50x', 's5x'] Natural sort: >>> sorted(['s1x', 's10x', 's5x', 's50x', 's100x'], key=natural) ['s1x', 's5x', 's10x', 's50x', 's100x'] Disclaimer: This is only meant to demonstrate the idea. You'll want to search around for a 'natural' sort recipe or package that handles the complexities of Unicode. It's probably not true that everything the 3.x re module considers to be a digit (the \d character class is Unicode category [Nd]) will work with the int() constructor, so instead I used [0-9] and [^0-9]. From eryksun at gmail.com Mon Oct 8 04:33:08 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 7 Oct 2012 22:33:08 -0400 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: <5070FD72.4040403@pearwood.info> References: <5070FD72.4040403@pearwood.info> Message-ID: On Sat, Oct 6, 2012 at 11:56 PM, Steven D'Aprano wrote: > > The C programming language on Unix systems. > > "ls" instead of "list" "LS" is an abbreviation for "list segments", not "list". It goes back to Multics in the late 60s and 70s. In Multics, every segment is a file, and every file is a segment (basically a memory mapped file with an 18-bit address space, and using 36-bit words -- or 1.125 MiB) or a 28-bit, page-aligned 'windowed' segment -- or so I've read. It was quite an odd system compared to what I'm used to. But it was also fairly modern for a system developed in the late 60s -- it had hot swappable processors/peripherals, paged virtual memory, dynamic linking of segments/files into a process by symbolic (file system) pathname, process security rings (e.g. ring 0 for privileged supervisor code), directories and symbolic links in the file/segment tree, user access control lists, and who knows what else (I'm still reading a lot of the old papers available at multicians.org). From wrw at mac.com Mon Oct 8 03:43:38 2012 From: wrw at mac.com (wrw at mac.com) Date: Sun, 07 Oct 2012 21:43:38 -0400 Subject: [Tutor] modulo In-Reply-To: References: Message-ID: <62A498F4-C987-43E0-BD07-D2529D7FE038@mac.com> On Oct 7, 2012, at 6:49 PM, Esteban Izaguirre wrote: > Hi, I'm following coursera's learn to program: the fundamentals, which teaches programming basics in python. Our first assignement involves the modulo operator with a negative divident, and while I've managed to get to understand it enough for the purposes of the assignement with help from othe rstudents, I still don't know how the hell it works, I wouldn't know how to use modulo in another situation if it ever arised. So, i undertand how modulo works when only positive numbers are used, but how does modulo determine, that, say -15 % 14 is equal to 13? Or -20 % 100 is 20? I just don't get how modulo works, all explanations I've found online only seem to be in relation of how this applies to perl or something, can someone explain it to me? _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor After reading all the other (good) answers, let me try one other way to think about it. First, for positive integers consider 15/12 = 1 and 15%12 = 3. So 12 * (15/12) + 15%12 = 15 and we are back where we started. In order to be able to perform the same operations on a negative dividend, it has to work the way you find puzzling. Consider: -15/12 = -2 and -15%12 = 9, which is the way it has to be in order for 12 * (-15/12) i.e. -24 plus -15%12 i.e. 9 to equal -15. -Bill From steve at pearwood.info Mon Oct 8 15:18:13 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 09 Oct 2012 00:18:13 +1100 Subject: [Tutor] string rules for 'number' In-Reply-To: References: <507208A3.6090104@pearwood.info> Message-ID: <5072D295.4070401@pearwood.info> On 08/10/12 11:51, Arnej Duranovic wrote: > Alright guys, I appreciate all your help SO much. I know understand, as the > gentleman above said " A string is a string is a string" doesn't matter > what is in it and they are ordered the same way...BUT this is what was > going through my head. Since letters are ordered in such a way that A is > less than B, for example, I thought the same applied to numbers, It does. "1" comes before "2", just like "A" comes before "B". And "12345" comes before "2", just like "Apple" comes before "B". -- Steven From ramit.prasad at jpmorgan.com Mon Oct 8 18:05:42 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Mon, 8 Oct 2012 16:05:42 +0000 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: <5070FD72.4040403@pearwood.info> References: <5070FD72.4040403@pearwood.info> Message-ID: <5B80DD153D7D744689F57F4FB69AF474166DF072@SCACMX008.exchad.jpmchase.net> Steven D'Aprano wrote: > It is a little-known fact that Unix sys admins, and C programmers, can > only type a fixed number of keys before their brains explode. Sad but > true. Since nobody knows how many keys that will be, but only that it is > fixed at birth, they have a horror of typing four characters when two > would do. Hence the standard Unix directories /usr and /mnt instead of > /user and /mount, and the Unix commands: Odd then that they eschew the usage of the mouse, a device which would prolong their lives. 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 bfishbein79 at gmail.com Mon Oct 8 18:33:50 2012 From: bfishbein79 at gmail.com (Benjamin Fishbein) Date: Mon, 8 Oct 2012 11:33:50 -0500 Subject: [Tutor] finding a number with str.find Message-ID: <50C945E2-5F9D-4D9A-9C91-EA4B3E768885@gmail.com> Is there a way to find the next character that is a digit (0-9) in a string? It seems that the str.find method can only find one particular character, rather than any character from among many. From bfishbein79 at gmail.com Mon Oct 8 18:43:58 2012 From: bfishbein79 at gmail.com (Benjamin Fishbein) Date: Mon, 8 Oct 2012 11:43:58 -0500 Subject: [Tutor] finding digit in string Message-ID: <80F2459D-B0E4-47E1-AE60-208E6401B84D@gmail.com> I figured out a solution for the question I asked on here. To find the next digit (0-9) in a string, I use: text.find("0" or "1" or "2", etc.......) But is there a more elegant way to do this? The way I found uses a lot of typing. From alan.gauld at btinternet.com Mon Oct 8 19:07:38 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 08 Oct 2012 18:07:38 +0100 Subject: [Tutor] finding a number with str.find In-Reply-To: <50C945E2-5F9D-4D9A-9C91-EA4B3E768885@gmail.com> References: <50C945E2-5F9D-4D9A-9C91-EA4B3E768885@gmail.com> Message-ID: On 08/10/12 17:33, Benjamin Fishbein wrote: > Is there a way to find the next character that is a digit (0-9) in a string? > it seems that the str.find method can only find one particular character, When looking for patterns rather than literal values you need to use regular expressions. These can get very complex very quickly but searching for a number is not too bad. One simple way (although not optimal) is to put the characters you want inside square brackets >>> import re >>> s = "a string with 7 words in it" >>> res = re.search("[0-9]", s) >>> res.group() '7' This returns something called a match object which can tell you what was found. You can then use that to find the index in the string. >>> s.index( res.group() ) 14 Having digits in a search pattern is such a common thing that there is a special syntax for that - '\d' : >>> s.index( re.search("\d", s).group() ) 14 Finally you can find all occurrences of a pattern in a string using re.findall() which returns a list of the found matches: >>> re.findall("\d",s) # only one occurrence in this case... ['7'] you can then loop through the results to locate each item. Regular expressions are way more powerful than this and there can be a tendency to overuse them. This should be resisted since they can introduce very hard to find bugs due to subtle errors in the patter specification. You can find a very gentle introduction to their other features in the re topic of my tutorial. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From breamoreboy at yahoo.co.uk Mon Oct 8 19:41:58 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 08 Oct 2012 18:41:58 +0100 Subject: [Tutor] finding digit in string In-Reply-To: <80F2459D-B0E4-47E1-AE60-208E6401B84D@gmail.com> References: <80F2459D-B0E4-47E1-AE60-208E6401B84D@gmail.com> Message-ID: On 08/10/2012 17:43, Benjamin Fishbein wrote: > I figured out a solution for the question I asked on here. > To find the next digit (0-9) in a string, I use: > text.find("0" or "1" or "2", etc.......) > But is there a more elegant way to do this? The way I found uses a lot of typing. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > How about something like:- for ch in text: if ch.isdigit(): doSomething(ch) or:- for ch in text: if '0' <= ch <= '9': doSomething(ch) If you want to use the offset for any reason use enumerate:- for i, ch in enumerate(text): etc. -- Cheers. Mark Lawrence. From alan.gauld at btinternet.com Mon Oct 8 19:52:58 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 08 Oct 2012 18:52:58 +0100 Subject: [Tutor] finding digit in string In-Reply-To: <80F2459D-B0E4-47E1-AE60-208E6401B84D@gmail.com> References: <80F2459D-B0E4-47E1-AE60-208E6401B84D@gmail.com> Message-ID: On 08/10/12 17:43, Benjamin Fishbein wrote: > I figured out a solution for the question I asked on here. > To find the next digit (0-9) in a string, I use: > text.find("0" or "1" or "2", etc.......) Are you sure that worked? It doesn't for me on Python 2.7... >>> s 'a string with 7 words in it' >>> s.find('4' or '5' or '7') -1 >>> s.find('7') 14 -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From d at davea.name Mon Oct 8 19:53:22 2012 From: d at davea.name (Dave Angel) Date: Mon, 08 Oct 2012 13:53:22 -0400 Subject: [Tutor] finding digit in string In-Reply-To: <80F2459D-B0E4-47E1-AE60-208E6401B84D@gmail.com> References: <80F2459D-B0E4-47E1-AE60-208E6401B84D@gmail.com> Message-ID: <50731312.9090803@davea.name> On 10/08/2012 12:43 PM, Benjamin Fishbein wrote: > I figured out a solution for the question I asked on here. Why then did you start a new thread, instead of responding on the same one? You didn't even use the same subject string. > To find the next digit (0-9) in a string, I use: > text.find("0" or "1" or "2", etc.......) That won't work. "0" or "1" is just "0" So you're searching for the character zero. > But is there a more elegant way to do this? The way I found uses a lot of typing. > See the other thread you started, with subject "[Tutor] finding a number with str.find" -- DaveA From bgailer at gmail.com Mon Oct 8 20:06:23 2012 From: bgailer at gmail.com (bob gailer) Date: Mon, 08 Oct 2012 14:06:23 -0400 Subject: [Tutor] finding digit in string In-Reply-To: <80F2459D-B0E4-47E1-AE60-208E6401B84D@gmail.com> References: <80F2459D-B0E4-47E1-AE60-208E6401B84D@gmail.com> Message-ID: <5073161F.9060202@gmail.com> On 10/8/2012 12:43 PM, Benjamin Fishbein wrote: > I figured out a solution for the question I asked on here. > To find the next digit (0-9) in a string, I use: > text.find("0" or "1" or "2", etc.......) > But is there a more elegant way to do this? The way I found uses a lot of typing. My way is: import string tt = string.maketrans('0123456789','0000000000') ts = asdf3456'.translate(t) # yields 'asdf0000' ts.find("0") # finds the next "0" which is in the same position as corresponding digit -- Bob Gailer 919-636-4239 Chapel Hill NC From steve at pearwood.info Mon Oct 8 20:31:12 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 09 Oct 2012 05:31:12 +1100 Subject: [Tutor] finding a number with str.find In-Reply-To: <50C945E2-5F9D-4D9A-9C91-EA4B3E768885@gmail.com> References: <50C945E2-5F9D-4D9A-9C91-EA4B3E768885@gmail.com> Message-ID: <50731BF0.9010603@pearwood.info> On 09/10/12 03:33, Benjamin Fishbein wrote: > Is there a way to find the next character that is a digit (0-9) in a >string? It seems that the str.find method can only find one particular >character, rather than any character from among many. Correct. For more complicated searching needs, either use a proper parser to parse the string as needed, or use a regular expression as a kind of "super-find". import re re.search(r'\d', 'I 8 sandwiches').start() # returns 2 Beware though: there is a saying about regular expressions, and in particular about the sort of people who reach for a reg ex to solve nearly every problem: "Some people, when faced with a problem, think 'I know, I'll use a regular expression'. Now they have two problems." -- Steven From roel at roelschroeven.net Mon Oct 8 20:50:14 2012 From: roel at roelschroeven.net (Roel Schroeven) Date: Mon, 08 Oct 2012 20:50:14 +0200 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: <1349640735.17669.8.camel@infirit.lan> References: <5070BED2.4060501@davea.name> <1349640735.17669.8.camel@infirit.lan> Message-ID: Sander Sweers schreef: > Roel Schroeven schreef op zo 07-10-2012 om 21:19 [+0200]: >> Sander Sweers schreef: >>> Op 7 okt. 2012 04:29 schreef >> > het volgende: >>> > I'm also not sure but I seem to remember that it is >>> > ("SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", >>> > "SATURDAY", "SUNDAY") >>> > which I think is extremely clever because it gets around the problem >>> > created by the fact that some people (misguided in my view) begin the >>> week >>> > with Sunday instead of ending with it. >>> >>> This has religious reasons, see >>> http://en.wikipedia.org/wiki/Week#Systems_derived_from_the_seven-day_week. >>> Notable exception to the rule that Sunday is the first day of the week >>> in europe is Italy, they consider Monday the first day of the week. >> As far as I know, monday is generally considered the first day of the >> week here in Belgium. I never knew it was different in other European >> countries; I thought it was America that starts the week on sunday. Was >> I wrong then? > > As far as I know also in Belgium Sunday is officially the first day of > the week. Look at the calendar and check what is the leftmost day. My > guess this is the same as your northern neighbor, Sunday ;-). On the contrary, I can't remember ever having seen a non-foreign calendar with weeks starting on Sunday. For example: http://www.antwerpen.be/docs/Stad/Bedrijven/Sociale_zaken/SZ_Milieu/afval/ophaalkalender_3_m.pdf I know one example doesn't proof much, but that calendar certainly looks very familiar to me, while Sunday-based calendars always feel weird. A Google images search for "kalender" also results mostly in Monday-based calendars for me. All of that doesn't say very much about which one is officially sanctioned as the first day of the week. I don't have the faintest idea where that piece of information should be available. Maybe it's not defined anywhere. Belgium, like European countries, has largely christian roots, which does suggest Sunday as the first day of the week. But I think the christian rules on such details have had very little impact on civilian life for many decades. Regards, Roel -- "Too often we hold fast to the cliches of our forebears. We subject all facts to a prefabricated set of interpretations. Too often we enjoy the comfort of opinion without the discomfort of thought." -- John F Kennedy roel at roelschroeven.net From oscar.j.benjamin at gmail.com Mon Oct 8 21:48:30 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 8 Oct 2012 20:48:30 +0100 Subject: [Tutor] string rules for 'number' In-Reply-To: References: Message-ID: On 8 October 2012 03:19, eryksun wrote: > As a supplement to what's already been stated about string > comparisons, here's a possible solution if you need a more 'natural' > sort order such as '1', '5', '10', '50', '100'. > > You can use a regular expression to split the string into a list of > (digits, nondigits) tuples (mutually exclusive) using re.findall. For > example: > > >>> import re > >>> dndre = re.compile('([0-9]+)|([^0-9]+)') > > >>> re.findall(dndre, 'a1b10') > [('', 'a'), ('1', ''), ('', 'b'), ('10', '')] > > Use a list comprehension to choose either int(digits) if digits is > non-empty else nondigits for each item. For example: > > >>> [int(d) if d else nd for d, nd in re.findall(dndre, 'a1b10')] > ['a', 1, 'b', 10] > > Now you have a list of strings and integers that will sort 'naturally' > compared to other such lists, since they compare corresponding items > starting at index 0. All that's left to do is to define this operation > as a key function for use as the "key" argument of sort/sorted. For > example: > > import re > > def natural(item, dndre=re.compile('([0-9]+)|([^0-9]+)')): > if isinstance(item, str): > item = [int(d) if d else nd for d, nd in > re.findall(dndre, item.lower())] > return item > > The above transforms all strings into a list of integers and lowercase > strings (if you don't want letter case to affect sort order). In > Python 2.x, use "basestring" instead of "str". If you're working with > bytes in Python 3.x, make sure to first decode() the items before > sorting since the regular expression is only defined for strings. > > Regular sort: > > >>> sorted(['s1x', 's10x', 's5x', 's50x', 's100x']) > ['s100x', 's10x', 's1x', 's50x', 's5x'] > > Natural sort: > > >>> sorted(['s1x', 's10x', 's5x', 's50x', 's100x'], key=natural) > ['s1x', 's5x', 's10x', 's50x', 's100x'] For simple cases like this example I tend to use: >>> natural = lambda x: (len(x), x) >>> sorted(['s1x', 's10x', 's5x', 's50x', 's100x'], key=natural) ['s1x', 's5x', 's10x', 's50x', 's100x'] Oscar From ramit.prasad at jpmorgan.com Mon Oct 8 22:11:38 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Mon, 8 Oct 2012 20:11:38 +0000 Subject: [Tutor] finding digit in string In-Reply-To: References: <80F2459D-B0E4-47E1-AE60-208E6401B84D@gmail.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF474166DF426@SCACMX008.exchad.jpmchase.net> Mark Lawrence wrote: > On 08/10/2012 17:43, Benjamin Fishbein wrote: > > I figured out a solution for the question I asked on here. > > To find the next digit (0-9) in a string, I use: > > text.find("0" or "1" or "2", etc.......) > > But is there a more elegant way to do this? The way I found uses a lot of typing. > > > > How about something like:- > > for ch in text: > if ch.isdigit(): > doSomething(ch) > > or:- > > for ch in text: > if '0' <= ch <= '9': > doSomething(ch) I am not sure that will work very well with Unicode numbers. I would assume (you know what they say about assuming) that str.isdigit() works better with international characters/numbers. >>> '1'.isdigit() True >>> '23'.isdigit() True >>> '23a'.isdigit() False for ch in text: if ch.isdigit(): # do something > > > If you want to use the offset for any reason use enumerate:- > > for i, ch in enumerate(text): > etc. > > -- > Cheers. > > Mark Lawrence. 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 Oct 8 22:59:24 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 08 Oct 2012 21:59:24 +0100 Subject: [Tutor] finding a number with str.find In-Reply-To: <50731BF0.9010603@pearwood.info> References: <50C945E2-5F9D-4D9A-9C91-EA4B3E768885@gmail.com> <50731BF0.9010603@pearwood.info> Message-ID: On 08/10/12 19:31, Steven D'Aprano wrote: > re.search(r'\d', 'I 8 sandwiches').start() > > # returns 2 I knew there was a better way that using index and group but I couldn't think what it was... start() so obvious once you see it :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eryksun at gmail.com Tue Oct 9 02:01:26 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 8 Oct 2012 20:01:26 -0400 Subject: [Tutor] finding digit in string In-Reply-To: <5B80DD153D7D744689F57F4FB69AF474166DF426@SCACMX008.exchad.jpmchase.net> References: <80F2459D-B0E4-47E1-AE60-208E6401B84D@gmail.com> <5B80DD153D7D744689F57F4FB69AF474166DF426@SCACMX008.exchad.jpmchase.net> Message-ID: On Mon, Oct 8, 2012 at 4:11 PM, Prasad, Ramit wrote: > >> for ch in text: >> if '0' <= ch <= '9': >> doSomething(ch) > > I am not sure that will work very well with Unicode numbers. I would > assume (you know what they say about assuming) that str.isdigit() > works better with international characters/numbers. In my tests below, isdigit() matches both decimal digits ('Nd') and other digits ('No'). None of the 'No' category digits works with int(). Python 2.7.3 >>> chars = [unichr(i) for i in xrange(sys.maxunicode + 1)] >>> digits = [c for c in chars if c.isdigit()] >>> digits_d = [d for d in digits if category(d) == 'Nd'] >>> digits_o = [d for d in digits if category(d) == 'No'] >>> len(digits), len(digits_d), len(digits_o) (529, 411, 118) Decimal >>> nums = [int(d) for d in digits_d] >>> [nums.count(i) for i in range(10)] [41, 42, 41, 41, 41, 41, 41, 41, 41, 41] Other >>> print u''.join(digits_o[:3] + digits_o[12:56]) ??????????????????????????????????????????????? >>> print u''.join(digits_o[67:94]) ??????????????????????????? >>> print u''.join(digits_o[3:12]) ????????? >>> int(digits_o[67]) Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'decimal' codec can't encode character u'\u2776' in position 0: invalid decimal Unicode string Python 3.2.3 >>> chars = [chr(i) for i in range(sys.maxunicode + 1)] >>> digits = [c for c in chars if c.isdigit()] >>> digits_d = [d for d in digits if category(d) == 'Nd'] >>> digits_o = [d for d in digits if category(d) == 'No'] >>> len(digits), len(digits_d), len(digits_o) (548, 420, 128) Decimal >>> nums = [int(d) for d in digits_d] >>> [nums.count(i) for i in range(10)] [42, 42, 42, 42, 42, 42, 42, 42, 42, 42] Other >>> print(*(digits_o[:3] + digits_o[13:57]), sep='') ??????????????????????????????????????????????? >>> print(*digits_o[68:95], sep='') ??????????????????????????? >>> print(*digits_o[3:12], sep='') ????????? >>> int(digits_o[68]) Traceback (most recent call last): File "", line 1, in ValueError: invalid literal for int() with base 10: '?' From steve at pearwood.info Tue Oct 9 03:48:53 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 9 Oct 2012 12:48:53 +1100 Subject: [Tutor] Through a glass, darkly: the datetime module In-Reply-To: References: <5070BED2.4060501@davea.name> <1349640735.17669.8.camel@infirit.lan> Message-ID: <20121009014853.GA27445@ando> On Mon, Oct 08, 2012 at 08:50:14PM +0200, Roel Schroeven wrote: > Sander Sweers schreef: > >As far as I know also in Belgium Sunday is officially the first day of > >the week. Look at the calendar and check what is the leftmost day. My > >guess this is the same as your northern neighbor, Sunday ;-). > > On the contrary, I can't remember ever having seen a non-foreign > calendar with weeks starting on Sunday. Depends on what you mean by "foreign" :) Here in Australia, we sometimes use Monday and sometimes Sunday as the first day of the week. My calendar applet in Xfce under Debian squeeze gives Sunday as the first day of the week. According to Wikipedia, the first day of the week can be Sunday, Monday or Saturday. Some examples picked at random: Sunday: ancient Roman, Hebrew, Greek, Arabic, Malay, Persian, etc. Monday: Polish, Russia, Serbian, Mongolian, Hungarian, Turkish, etc. Saturday: Swahili, Georgian. http://en.wikipedia.org/wiki/Names_of_the_days_of_the_week -- Steven From robertvstepp at gmail.com Tue Oct 9 04:35:35 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Mon, 8 Oct 2012 21:35:35 -0500 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: <506D72E0.60402@pearwood.info> References: <20121003063843.GB27111@ando> <506D72E0.60402@pearwood.info> Message-ID: Steve, On Thu, Oct 4, 2012 at 6:28 AM, Steven D'Aprano wrote: > > Now, ask me about *raw strings*, and the difference between Unicode > and byte strings :) How can I resist asking! I am not in chapter 2 of my study text yet, but looking ahead raw strings seem to be a method of declaring everything within the quotes to be a literal string character including the backslash escape character. Apparently this is designated by using an r before the very first quote. Can this quote be single, double or triple? I am not up (yet) on the details of Unicode that Python 3 defaults to for strings, but I believe I comprehend the general concept. Looking at the string escape table of chapter 2 it appears that Unicode characters can be either 16-bit or 32-bit. That must be a lot of potential characters! It will be interesting to look up the full Unicode tables. Quickly scanning the comparing strings section, I wonder if I should have been so quick to jump in with a couple of responses to the other thread going on recently! I don't see a mention of byte strings mentioned in the index of my text. Are these just the ASCII character set? Since I have not made it formally into this chapter yet, I don't really have specific questions, but I would be interested in anything you are willing to relate on these topics to complete my introduction to strings in Python. Or we can wait until I do get into the data types chapter that looks at these topics in detail and have specific questions. -- Cheers! boB From fomcl at yahoo.com Tue Oct 9 09:16:10 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Tue, 9 Oct 2012 00:16:10 -0700 (PDT) Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: References: <20121003063843.GB27111@ando> <506D72E0.60402@pearwood.info> Message-ID: <1349766970.53310.YahooMailNeo@web110704.mail.gq1.yahoo.com> > I am not up (yet) on the details of Unicode that Python 3 defaults to > for strings, but I believe I comprehend the general concept. Looking > at the string escape table of chapter 2 it appears that Unicode > characters can be either 16-bit or 32-bit. That must be a lot of > potential characters! ? Yes, 1,114,112 code points:?http://tinyurl.com/8pd586k The picture is part of this presentation: http://farmdev.com/talks/unicode/ From eryksun at gmail.com Tue Oct 9 11:29:44 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 9 Oct 2012 05:29:44 -0400 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: References: <20121003063843.GB27111@ando> <506D72E0.60402@pearwood.info> Message-ID: On Mon, Oct 8, 2012 at 10:35 PM, boB Stepp wrote: > > I am not up (yet) on the details of Unicode that Python 3 defaults to > for strings, but I believe I comprehend the general concept. Looking > at the string escape table of chapter 2 it appears that Unicode > characters can be either 16-bit or 32-bit. That must be a lot of > potential characters! There are 1114112 possible codes (65536 codes/plane * 17 planes), but some are reserved, and only about 10% are assigned. Here's a list by category: http://www.fileformat.info/info/unicode/category/index.htm Python 3 lets you use any Unicode letter as an identifier, including letter modifiers ("Lm") and number letters ("Nl"). For example: >>> a??b = True >>> a??b True >>> ?, ?, ?, ?, ? = range(1, 6) >>> ?, ?, ?, ?, ? (1, 2, 3, 4, 5) A potential gotcha in Unicode is the design choice to have both [C]omposed and [D]ecomposed forms of characters. For example: >>> from unicodedata import name, normalize >>> s1 = "?" >>> name(s1) 'LATIN SMALL LETTER U WITH DIAERESIS' >>> s2 = normalize("NFD", s1) >>> list(map(name, s2)) ['LATIN SMALL LETTER U', 'COMBINING DIAERESIS'] These combine as one glyph when printed: >>> print(s2) u? Different forms of the 'same' character won't compare as equal unless you first normalize them to the same form: >>> s1 == s2 False >>> normalize("NFC", s1) == normalize("NFC", s2) True > I don't see a mention of byte strings mentioned in the index of my > text. Are these just the ASCII character set? A bytes object (and its mutable cousin bytearray) is a sequence of numbers, each in the range of a byte (0-255). bytes literals start with b, such as b'spam' and can only use ASCII characters, as does the repr of bytes. Slicing returns a new bytes object, but an index or iteration returns integer values: >>> b'spam'[:3] b'spa' >>> b'spam'[0] 115 >>> list(b'spam') [115, 112, 97, 109] bytes have string methods as a convenience, such as find, split, and partition. They also have the method decode(), which uses a specified encoding such as "utf-8" to create a string from an encoded bytes sequence. From finsolut2003 at yahoo.com Tue Oct 9 18:27:43 2012 From: finsolut2003 at yahoo.com (tayo rotimi) Date: Tue, 9 Oct 2012 09:27:43 -0700 (PDT) Subject: [Tutor] Saving GUIs In-Reply-To: <1349386220.90903.YahooMailNeo@web162302.mail.bf1.yahoo.com> References: <1347270014.44482.YahooMailNeo@web162301.mail.bf1.yahoo.com> <1349386220.90903.YahooMailNeo@web162302.mail.bf1.yahoo.com> Message-ID: <1349800063.91362.YahooMailNeo@web162303.mail.bf1.yahoo.com> Hi, I have started creating GUIs. But my current challenge now is saving the GUIs created. I have observed that whenever I close the console, the GUIs also get closed. So, please what do I need to do to save the GUIs, or call them back when next needed, if they don't actually get extinguished when the console is closed? I use Python 3.2 on windows 7. Regards. Tayo. -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Tue Oct 9 18:40:08 2012 From: d at davea.name (Dave Angel) Date: Tue, 09 Oct 2012 12:40:08 -0400 Subject: [Tutor] Saving GUIs In-Reply-To: <1349800063.91362.YahooMailNeo@web162303.mail.bf1.yahoo.com> References: <1347270014.44482.YahooMailNeo@web162301.mail.bf1.yahoo.com> <1349386220.90903.YahooMailNeo@web162302.mail.bf1.yahoo.com> <1349800063.91362.YahooMailNeo@web162303.mail.bf1.yahoo.com> Message-ID: <50745368.60909@davea.name> On 10/09/2012 12:27 PM, tayo rotimi wrote: > Hi, > > > I have started creating GUIs. But my current challenge now is saving the GUIs created. I have observed that > whenever I close the console, the GUIs also get closed. So, please what do I > need to do to save the GUIs, or call them back when next needed, if they don't actually get extinguished when the console is closed? > I use Python 3.2 on windows 7. > > > Regards. > > Tayo. > First question is whether that console is desired in the first place. It may depend on which GUI library you're using (wx, tkinterf, ...) but Windows has a habit of requiring a console to be sitting around for each application, just in case it decides to display something on it. The usual workaround is to run PythonW.exe instead of Python.exe. One way to cause this is to rename the main script to have an extension of .pyw instead of .py , assuming the appropriate file associations exist. -- DaveA From alan.gauld at btinternet.com Tue Oct 9 19:14:24 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 09 Oct 2012 18:14:24 +0100 Subject: [Tutor] Saving GUIs In-Reply-To: <1349800063.91362.YahooMailNeo@web162303.mail.bf1.yahoo.com> References: <1347270014.44482.YahooMailNeo@web162301.mail.bf1.yahoo.com> <1349386220.90903.YahooMailNeo@web162302.mail.bf1.yahoo.com> <1349800063.91362.YahooMailNeo@web162303.mail.bf1.yahoo.com> Message-ID: On 09/10/12 17:27, tayo rotimi wrote: Please do not reply to an existing thread to create a new topic. Send a fresh email to tutor at python.org. Using an existing one means your messages get inserted amongst messages for the original thread making them hard to find and follow the thread. > I have started creating GUIs. What are you using to create the GUIs? Which GUI library? Is it Tkinter, wxPython or something else? > But my current challenge now is saving the GUIs created. What do you mean by saving the GUI? The GUI should normally be created in a Python program file (aka a script) which you run from the console (or in Windows by clicking on it in Windows explorer). To save the program just save the file. Are you using a development tool such as IDLE or pythonwin to create the .py program file? If so which one? > I have observed that whenever I close the console, the > GUIs also get closed. Which console are you talking about? The one where you started the program? The one where you created the program? or the one that sometimes starts when you start the GUI? > So, please what do I need to do to save the GUIs, > or call them back when next needed, if they don't actually get > extinguished when the console is closed? You need to rerun the program file, assuming you are creating one and not just typing the commands into the >>> prompt. If you are just typing them at the >>> prompt then you need to type them into a file instead. Send a reply answering the questions above and we will be in a better position to help you. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From doark at mail.com Tue Oct 9 20:00:01 2012 From: doark at mail.com (frank ernest) Date: Tue, 09 Oct 2012 14:00:01 -0400 Subject: [Tutor] I am learning python3 and would like some more python3 modules/programs on my... Message-ID: <20121009180002.14120@gmx.com> version python3.2 linux I am learning python3 and would like some more python3 modules/programs on my computer to look at and work with to learn more about python3. I have read the tutorial and some of the complete language reference. I can't tell from the package index whats a python3 module/program and whats for 2.X. I seems from the voting guide that their are not many python3 programs; did I learn python at the wrong time? If possible I would like to get involved with a python3 program despite the fact I would not be at first a good programmer (It may help you to know that python3 is my first language though I did try to learn ruby but I did not like the language much [I like python though]) unfortunately their does not seem to be much choice of what to or not to help program. On my OS their are only a few bindings to libraries the standard python installation and the python3-tools package which contains some turtle programs and examples of programming in python. To give you an idea of what I should like to work in I eventually want to create with python something like a voice recognition program though not to recognize voice but rather music. Though I intend to create more then the afore mentioned program so any branch of application programming would do, I intend to create it first. -------------- next part -------------- An HTML attachment was scrubbed... URL: From acolle00 at g.uafortsmith.edu Tue Oct 9 21:47:35 2012 From: acolle00 at g.uafortsmith.edu (Amanda Colley) Date: Tue, 9 Oct 2012 14:47:35 -0500 Subject: [Tutor] using the loop function while another Message-ID: Ok, here is my problem. If a person has two different people to order books for, then when using the loop option, How do you add the two seperate choices together for a final total of books cost? another='y' while another=='y' or another=='Y': choice() another=input('Do you have another book to order for this student? '+\ 'Enter y for yes: ') def choice(): book=int(input('Enter the book chioce you want: ' +\ 'For Hardback Enter the number 1: ' +\ 'Paperback Enter the number 2: ' +\ 'Electronic Enter the number 3: ')) num=int(input('Enter how many you need: ')) cost=0 if book==1: cost=79 elif book==2: cost=49 elif book==3: cost=19 b_total=float(cost*num) print('Your book cost is $ ',format(b_total,'.2f')) -- Amanda Colley -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Tue Oct 9 22:12:02 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 09 Oct 2012 21:12:02 +0100 Subject: [Tutor] using the loop function while another In-Reply-To: References: Message-ID: On 09/10/2012 20:47, Amanda Colley wrote: > Ok, here is my problem. If a person has two different people to order > books for, then when using the loop option, How do you add the two seperate > choices together for a final total of books cost? > > another='y' > while another=='y' or another=='Y': > choice() > another=input('Do you have another book to order for this student? > '+\ > 'Enter y for yes: ') > > def choice(): > book=int(input('Enter the book chioce you want: ' +\ > 'For Hardback Enter the number 1: ' +\ > 'Paperback Enter the number 2: ' +\ > 'Electronic Enter the number 3: ')) > num=int(input('Enter how many you need: ')) > cost=0 > if book==1: > cost=79 > elif book==2: > cost=49 > elif book==3: > cost=19 > b_total=float(cost*num) > print('Your book cost is $ ',format(b_total,'.2f')) > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Create an empty list at the top of your loop. Store your b_total in the list inside the loop with the append method. When the loop finishes use the built-in sum function to get the final total. -- Cheers. Mark Lawrence. From ramit.prasad at jpmorgan.com Tue Oct 9 23:23:07 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 9 Oct 2012 21:23:07 +0000 Subject: [Tutor] I am learning python3 and would like some more python3 modules/programs on my... In-Reply-To: <20121009180002.14120@gmx.com> References: <20121009180002.14120@gmx.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF474166E37D0@SCACMX008.exchad.jpmchase.net> frank ernest wrote: > version python3.2 linux > I am learning python3 and would like some more python3 modules/programs on my computer to look at and work with > to learn more about python3. I have read the tutorial and some of the complete language reference. I can't tell > from the package index whats a python3 module/program and whats for 2.X. I seems from the voting guide that > their are not many python3 programs; did I learn python at the wrong time? If possible I would like to get > involved with a python3 program despite the fact I would not be at first a good programmer (It may help you to > know that python3 is my first language though I did try to learn ruby but I did not like the language much [I > like python though]) unfortunately their does not seem to be much choice of what to or not to help program. On > my OS their are only a few bindings to libraries the standard python installation and the python3-tools package > which contains some turtle programs and examples of programming in python.?To give you an idea of what I should > like to work in I eventually want to create with python something like a voice recognition program though not to > recognize voice but rather music. Though I intend to create more then the afore mentioned program so any branch > of application?programming would do, I intend to create it first. Python 2 and Python 3 are pretty similar syntactically, but it can be a bit challenging for a beginner. This might help tell you how to map from 2 to 3 or vice-versa. http://docs.python.org/py3k/whatsnew/3.0.html As for choosing between Python 2 and 3, it really depends on what you want to do and if the libraries are available for Python 3 or not. For example, PIL is a popular Python image manipulation library and I believe is only Python 2 officially. Of course, if you do not need PIL then it does not matter which you pick. Given no definite goal or task to create, I would suggest learning Python 3 as I think Python 2 will only get security fixes. I have no knowledge of music/voice analysis modules but maybe this will give you a start. http://wiki.python.org/moin/PythonInMusic http://stackoverflow.com/questions/6356749/music-analysis-and-visualization You can even think about switching later if necessary. Although, it is probably easier to go from 2 to 3 due to the existence of automated conversion tools. I would imagine that even if your OS only includes a few bindings, it is probably not a big deal to compile / install your own python packages (including library bindings). IT is possible that your package manager might not name all python bindings as python-*; sometimes they are named -python. Nobody is a good programmer at first, it takes practice and experience. Good luck and feel free to ask more questions when you run into a problem. 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 Oct 10 00:24:59 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 9 Oct 2012 22:24:59 +0000 Subject: [Tutor] using the loop function while another In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF474166E39A0@SCACMX008.exchad.jpmchase.net> Mark Lawrence wrote: > On 09/10/2012 20:47, Amanda Colley wrote: > > Ok, here is my problem. If a person has two different people to order > > books for, then when using the loop option, How do you add the two seperate > > choices together for a final total of books cost? > > > > another='y' > > while another=='y' or another=='Y': > > choice() > > another=input('Do you have another book to order for this student? > > '+\ > > 'Enter y for yes: ') > > > > def choice(): > > book=int(input('Enter the book chioce you want: ' +\ > > 'For Hardback Enter the number 1: ' +\ > > 'Paperback Enter the number 2: ' +\ > > 'Electronic Enter the number 3: ')) > > num=int(input('Enter how many you need: ')) > > cost=0 > > if book==1: > > cost=79 > > elif book==2: > > cost=49 > > elif book==3: > > cost=19 > > b_total=float(cost*num) > > print('Your book cost is $ ',format(b_total,'.2f')) > > > > > > Create an empty list at the top of your loop. Store your b_total in the > list inside the loop with the append method. When the loop finishes use > the built-in sum function to get the final total. > > -- > Cheers. > > Mark Lawrence. > I would also have choice() return the "choice" and "num" and then let the function calling it assign the book price. another='y' while another=='y' or another=='Y': book_format = choice() # rename to be more descriptive e.g. get_book_format() price = price_by_format(book_format) subtotal.append(price) another=input('Do you have another book to order for this student? '+\ 'Enter y for yes: ') The backslash near the + is also unnecessary and I consider it a sign of a possible bad habit/practice. You can usually wrap your multi-line expressions with a parenthesis. input(' blah ' + 'blah2') OR x = (something * something_else + some_fucntion_call() + last_thing_here) OR if blah and ( blah != shoo and shoo != 1 and shoo < 5 and blah is not None ): print('true') 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 Oct 10 01:39:59 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 10 Oct 2012 00:39:59 +0100 Subject: [Tutor] using the loop function while another In-Reply-To: References: Message-ID: On 09/10/12 20:47, Amanda Colley wrote: > Ok, here is my problem. If a person has two different people to order > books for, then when using the loop option, How do you add the two > seperate choices together for a final total of books cost? > This looks a bit like it might be homework so I won't answer directly but will give some general comments that might help... > another='y' > while another=='y' or another=='Y': This line should not be indented. It's usually easier to convert the string being tested to a known case. Either while another.lower() == 'y': or while another.upper() == 'Y': > choice() choice does not return any values so you cannot access the data inside. You want to get back the cost (and maybe the book too!) > another=input('Do you have another book to order for this > student? '+\ > 'Enter y for yes: ') Using a line continuation character is not needed here. Using string addition is usually the wrong approach in Python. In this case you could use string literal concatenation: another=input('Do you have another book to order for this student?', 'Enter y for yes: ') or just a single long line. In case you think this will put it on two lines you are wrong, it will need an exp[licit newline character ('\n') to do that: another=input('Do you have another book to order for this student?', '\nEnter y for yes: ') or you could use triple quotes: another=input(''' Do you have another book to order for this student? Enter y for yes: ''') Lots of choices... > def choice(): > book=int(input('Enter the book chioce you want: ' +\ > 'For Hardback Enter the number 1: ' +\ > 'Paperback Enter the number 2: ' +\ > 'Electronic Enter the number 3: ')) > num=int(input('Enter how many you need: ')) > cost=0 > if book==1: > cost=79 > elif book==2: > cost=49 > elif book==3: > cost=19 while this would work I'd prefer to use an else clause to catch unexpected values explicitly rather than rely on the initialisation: cost=0 # this becomes redundant if book==1: cost=79 elif book==2: cost=49 elif book==3: cost=19 else: cost = 0 > b_total=float(cost*num) > print('Your book cost is $ ',format(b_total,'.2f')) As a general rule its better to keep presentation and logic separate so rather than printing the result inside the function return the value and the calling code can then print it out. Or store it for further processing(hint!) def choice(): # ... as above except the end: ... b_total=float(cost*num) return b_total And where you call it print('Your book cost is $ ',format(choice(),'.2f')) To do more than one book you need your loop to collect the values rather than print them (or add them as you go) then when done sum() the values and print the result. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eryksun at gmail.com Wed Oct 10 03:14:12 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 9 Oct 2012 21:14:12 -0400 Subject: [Tutor] using the loop function while another In-Reply-To: References: Message-ID: On Tue, Oct 9, 2012 at 7:39 PM, Alan Gauld wrote: > > another=input('Do you have another book to order for this student?', > '\nEnter y for yes: ') Remove the comma, and this will parse correctly. A comma in a function call is used to separate arguments. On its own a comma creates a tuple -- outside of a list or dict and assuming parentheses for precedence where necessary. > or you could use triple quotes: > > > another=input(''' > Do you have another book to order for this student? > Enter y for yes: ''') > This retains the leading spaces. You can use textwrap.dedent() to remove them: >>> print(textwrap.dedent(""" ... Do you have another book to order for this student? ... Enter y for yes: """)) Do you have another book to order for this student? Enter y for yes: From alan.gauld at btinternet.com Wed Oct 10 09:37:20 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 10 Oct 2012 08:37:20 +0100 Subject: [Tutor] using the loop function while another In-Reply-To: References: Message-ID: On 10/10/12 02:14, eryksun wrote: >> another=input('Do you have another book to order for this student?', >> '\nEnter y for yes: ') > > Remove the comma, and this will parse correctly. Oops, yes good catch, don't know where that came from. >> another=input(''' >> Do you have another book to order for this student? >> Enter y for yes: ''') > > This retains the leading spaces. You can use textwrap.dedent() to remove them: I intended the spaces but didn't know about textwrap let alone dedent(). You learn something new every day! :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From fomcl at yahoo.com Wed Oct 10 12:02:21 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 10 Oct 2012 03:02:21 -0700 (PDT) Subject: [Tutor] ctypes question Message-ID: <1349863341.94655.YahooMailNeo@web110714.mail.gq1.yahoo.com> Hi, ? I have a program that reads and writes files using ctypes. When I want it to read AND write (e.g. read a file, select some stuff and write that), the library returns a 'read-open' error. I think that the?pointer to the file handle for read and write point to the same address. To test that hypothesis, I wrote the simplified code below. Problem is, that I can't make it work, let alone come up with a solution. ;-( How do I tell ctypes to use?a particular?chunck of memory, so read and write buffers do not mutually interfere? Maybe the 'offset' parameter of ctypes.byref? ? import ctypes import platform import os import tempfile # load libraries pf = platform.platform().lower() if pf.startswith("win"): ??? libc = ctypes.cdll.msvcrt ??? fopen = libc._fdopen elif pf.startswith("lin"): ??? libc = ctypes.CDLL("libc.so.6") ??? fopen = libc.fdopen elif pf.startswith("darwin"): ??? libc = ctypes.CDLL("libc.dylib") ??? fopen = libc.fdopen else: ??? raise NotImplementedError # create test data path = tempfile.gettempdir() os.chdir(path) fn = "test.txt" lines = 100 * (100 * "*" + os.linesep) with open(fn, "wb") as f: ??? f.write(lines) ? # read test data (code does NOT work) fh = fopen(ctypes.c_char_p(fn), "rb") fhPtr = ctypes.byref(ctypes.c_int(fh)) buff = ctypes.create_string_buffer(lines) ret = libc.fread(buff, ctypes.c_int(1), ctypes.c_int(len(lines)), fhPtr) print buff.value ? # write test data (code does NOT work) fn = "somefile.txt" fh_out = fopen(ctypes.c_char_p(fn), "wb") fh_outPtr = ctypes.byref(ctypes.c_int(fh_out)) buff = ctypes.create_string_buffer(lines) ret = libc.fwrite(buff, ctypes.c_int(1), ctypes.c_int(len(lines)), fh_outPtr) ?Thanks 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? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? From __peter__ at web.de Wed Oct 10 12:41:44 2012 From: __peter__ at web.de (Peter Otten) Date: Wed, 10 Oct 2012 12:41:44 +0200 Subject: [Tutor] ctypes question References: <1349863341.94655.YahooMailNeo@web110714.mail.gq1.yahoo.com> Message-ID: Albert-Jan Roskam wrote: > I have a program that reads and writes files using ctypes. When I want it > to read AND write (e.g. read a file, select some stuff and write that), > the library returns a 'read-open' error. I think that the pointer to the > file handle for read and write point to the same address. In C fopen() returns a FILE *, open() returns an int. If your library expects a file handle, i. e. an int you can open the file in Python f = open(filename, "r+b") and pass f.fileno() to the library. > To test that > hypothesis, I wrote the simplified code below. Problem is, that I can't > make it work, let alone come up with a solution. ;-( How do I tell ctypes > to use a particular chunck of memory, so read and write buffers do not > mutually interfere? Maybe the 'offset' parameter of ctypes.byref? I think you should turn to python-list instead of tutor with problems like this. From alan.gauld at btinternet.com Wed Oct 10 13:24:31 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 10 Oct 2012 12:24:31 +0100 Subject: [Tutor] ctypes question In-Reply-To: <1349863341.94655.YahooMailNeo@web110714.mail.gq1.yahoo.com> References: <1349863341.94655.YahooMailNeo@web110714.mail.gq1.yahoo.com> Message-ID: On 10/10/12 11:02, Albert-Jan Roskam wrote: > I have a program that reads and writes files using ctypes. Any particular reason why? That's not normally something I'd expect you to need ctypes for. Unless you just want to play with ctypes... > When I want it to read AND write (e.g. read a file, select some stuff and write that), > the library returns a 'read-open' error. Can we see the error messages please? > I think that the pointer to the file handle for read and write point to the same address. > To test that hypothesis, I wrote the simplified code below. > Problem is, that I can't make it work Again, what exactly is going wrong. "can't make it work is not too helpful, we need specific descriptions of what went wrong with any error messages. > How do I tell ctypes to use a particular chunck of memory, > so read and write buffers do not mutually interfere? In general you don't, you leave all that to the OS via the C libraries. But you do need to think about what you are doing with the file. You shouldn't open the same file simultaneously for read and write. If you do need to do both use the combined 'rw' mode - but be aware that getting simultaneous read/write behaviour right is hard! > Maybe the 'offset' parameter of ctypes.byref? > > import ctypes > import platform > import os > import tempfile > > # load libraries > pf = platform.platform().lower() > if pf.startswith("win"): > libc = ctypes.cdll.msvcrt > fopen = libc._fdopen > elif pf.startswith("lin"): > libc = ctypes.CDLL("libc.so.6") > fopen = libc.fdopen > elif pf.startswith("darwin"): > libc = ctypes.CDLL("libc.dylib") > fopen = libc.fdopen > else: > raise NotImplementedError > > # create test data > path = tempfile.gettempdir() > os.chdir(path) > fn = "test.txt" > lines = 100 * (100 * "*" + os.linesep) > with open(fn, "wb") as f: > f.write(lines) > > > # read test data (code does NOT work) > fh = fopen(ctypes.c_char_p(fn), "rb") > fhPtr = ctypes.byref(ctypes.c_int(fh)) > buff = ctypes.create_string_buffer(lines) > ret = libc.fread(buff, ctypes.c_int(1), ctypes.c_int(len(lines)), fhPtr) > print buff.value > > > # write test data (code does NOT work) > fn = "somefile.txt" > fh_out = fopen(ctypes.c_char_p(fn), "wb") > fh_outPtr = ctypes.byref(ctypes.c_int(fh_out)) > buff = ctypes.create_string_buffer(lines) > ret = libc.fwrite(buff, ctypes.c_int(1), ctypes.c_int(len(lines)), fh_outPtr) Again, what does "NOT work"? Does it work if you comment out one of the blocks? I don't use ctypes much and certainly not for file handling so can't be sure if the code is correct or not - maybe ask on a ctypes forum for that... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eryksun at gmail.com Wed Oct 10 13:40:31 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 10 Oct 2012 07:40:31 -0400 Subject: [Tutor] ctypes question In-Reply-To: <1349863341.94655.YahooMailNeo@web110714.mail.gq1.yahoo.com> References: <1349863341.94655.YahooMailNeo@web110714.mail.gq1.yahoo.com> Message-ID: On Wed, Oct 10, 2012 at 6:02 AM, Albert-Jan Roskam wrote: > > elif pf.startswith("lin"): > libc = ctypes.CDLL("libc.so.6") > fopen = libc.fdopen > fh = fopen(ctypes.c_char_p(fn), "rb") > fhPtr = ctypes.byref(ctypes.c_int(fh)) > buff = ctypes.create_string_buffer(lines) > ret = libc.fread(buff, ctypes.c_int(1), ctypes.c_int(len(lines)), fhPtr) > print buff.value fdopen takes a file descriptor, not a filename. You're using it with a filename like fopen (and you even called it fopen). Also, the byref usage is incorrect. fopen() returns a pointer to a FILE. fread() needs the pointer, not a reference to the pointer. You're giving it the address of an int, but it expects to find a FILE. You can use c_void_p(fh), or set the argtypes. >>> from ctypes import * >>> import ctypes.util >>> libc = cdll.LoadLibrary(ctypes.util.find_library('c')) >>> fopen = libc.fopen >>> fread = libc.fread >>> fread.argtypes = [c_void_p, c_size_t, c_size_t, c_void_p] >>> fh = fopen("spam.txt", "rb") >>> buf = create_string_buffer(128) >>> fread(buf, 1, 128, fh) 5 >>> buf.value 'spam\n' To use fdopen, you can get a fileno() from an open Python file object: >>> f = open("spam.txt", "rb") >>> fdopen = libc.fdopen >>> fh = fdopen(f.fileno(), "rb") >>> fread(buf, 1, 128, fh) 5 From fomcl at yahoo.com Wed Oct 10 14:28:43 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 10 Oct 2012 05:28:43 -0700 (PDT) Subject: [Tutor] ctypes question In-Reply-To: References: <1349863341.94655.YahooMailNeo@web110714.mail.gq1.yahoo.com> Message-ID: <1349872123.25805.YahooMailNeo@web110716.mail.gq1.yahoo.com> ----- Original Message ----- > From: eryksun > To: Albert-Jan Roskam > Cc: Python Mailing List > Sent: Wednesday, October 10, 2012 1:40 PM > Subject: Re: [Tutor] ctypes question > > On Wed, Oct 10, 2012 at 6:02 AM, Albert-Jan Roskam > wrote: >> >> elif pf.startswith("lin"): >> ? ? libc = ctypes.CDLL("libc.so.6") >> ? ? fopen = libc.fdopen > >> fh = fopen(ctypes.c_char_p(fn), "rb") >> fhPtr = ctypes.byref(ctypes.c_int(fh)) >> buff = ctypes.create_string_buffer(lines) >> ret = libc.fread(buff, ctypes.c_int(1), ctypes.c_int(len(lines)), fhPtr) >> print buff.value > > fdopen takes a file descriptor, not a filename. You're using it with a > filename like fopen (and you even called it fopen). Also, the byref > usage is incorrect. fopen() returns a pointer to a FILE. fread() needs > the pointer, not a reference to the pointer. You're giving it the > address of an int, but it expects to find a FILE. You can use > c_void_p(fh), or set the argtypes. Aha, thanks. Yes, my total lack of knowledge of C speaks volumes here. ? > ? ? >>> from ctypes import * > ? ? >>> import ctypes.util > > ? ? >>> libc = cdll.LoadLibrary(ctypes.util.find_library('c')) That's a nice little trick! ? > ? ? >>> fopen = libc.fopen > ? ? >>> fread = libc.fread > ? ? >>> fread.argtypes = [c_void_p, c_size_t, c_size_t, c_void_p] > > ? ? >>> fh = fopen("spam.txt", "rb") > ? ? >>> buf = create_string_buffer(128) > ? ? >>> fread(buf, 1, 128, fh) > ? ? 5 > ? ? >>> buf.value > ? ? 'spam\n' > > To use fdopen, you can get a fileno() from an open Python file object: > > ? ? >>> f = open("spam.txt", "rb") > > ? ? >>> fdopen = libc.fdopen > ? ? >>> fh = fdopen(f.fileno(), "rb") > ? ? >>> fread(buf, 1, 128, fh) > ? ? 5 Very insightful. I'll modify my code an see if it might solve my original problem. At least I've learnt a lot now! ? Thanks all for replying!! From eowens0124 at gmx.com Wed Oct 10 21:52:15 2012 From: eowens0124 at gmx.com (Ed Owens) Date: Wed, 10 Oct 2012 15:52:15 -0400 Subject: [Tutor] iterating over a changing list Message-ID: <006e01cda720$c06d40f0$4147c2d0$@com> I'm trying to iterate over a list of elements, and make changes to the list in front of the element I'm currently working with. I can update the list, but the 'for' doesn't see the new element. Here's the code: import string def add_element(items, point): items = items[:point+1][:] + [['new']] + items[point+1:] return items def main(): pass itmlst = [['a'],['b']] itmcntr = 0 for itm in itmlst: cmd = '' while True: cmd = raw_input('break, add, print:') if cmd == 'break': break elif cmd == 'add': itmlst = add_element(itmlst,itmcntr) elif cmd == 'print': print 'current item:', itm else: print 'invalid' itmcntr += 1 print 'finished with', itm, 'in', itmlst print len(itmlst), 'total items in list' If I provide the inputs: [print add print break print break] at the prompt, I get this output current item: ['a'] current item: ['a'] finished with ['a'] in [['a'], ['new'], ['b']] current item: ['b'] finished with ['b'] in [['a'], ['new'], ['b']] 3 total items in list The new element got added, but it wasn't used in the iteration over the list of items. Other than setting up a counter and calling len() each loop, is there some way to have the changing list recognized within the for loop? Thanks in advance for any help. Ed -------------- next part -------------- An HTML attachment was scrubbed... URL: From bfishbein79 at gmail.com Wed Oct 10 22:35:45 2012 From: bfishbein79 at gmail.com (Benjamin Fishbein) Date: Wed, 10 Oct 2012 15:35:45 -0500 Subject: [Tutor] What can I do if I'm banned from a website?? Message-ID: I've been scraping info from a website with a url program I wrote. But now I can't open their webpage, no matter which web browser I use. I think they've somehow blocked me. How can I get back in? Is it a temporary block? And can I get in with the same computer from a different wifi? From eire1130 at gmail.com Wed Oct 10 22:42:30 2012 From: eire1130 at gmail.com (James Reynolds) Date: Wed, 10 Oct 2012 16:42:30 -0400 Subject: [Tutor] What can I do if I'm banned from a website?? In-Reply-To: References: Message-ID: On Wed, Oct 10, 2012 at 4:35 PM, Benjamin Fishbein wrote: > I've been scraping info from a website with a url program I wrote. But now > I can't open their webpage, no matter which web browser I use. I think > they've somehow blocked me. How can I get back in? Is it a temporary block? > And can I get in with the same computer from a different wifi? > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > A few thoughts: 1. Try using a proxy. 2. Ask the webadmin (nicely) to unban you 3. Use requests / urllib3 to se connection pooling 4. See if the site has an API designed for data extraction. -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Wed Oct 10 22:44:09 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 10 Oct 2012 21:44:09 +0100 Subject: [Tutor] iterating over a changing list In-Reply-To: <006e01cda720$c06d40f0$4147c2d0$@com> References: <006e01cda720$c06d40f0$4147c2d0$@com> Message-ID: On 10/10/2012 20:52, Ed Owens wrote: > I'm trying to iterate over a list of elements, and make changes to the list > in front of the element I'm currently working with. I can update the list, > but the 'for' doesn't see the new element. Here's the code: > > import string > > def add_element(items, point): > items = items[:point+1][:] + [['new']] + items[point+1:] > return items > > def main(): > pass > > itmlst = [['a'],['b']] > itmcntr = 0 > > for itm in itmlst: > cmd = '' > while True: > cmd = raw_input('break, add, print:') > if cmd == 'break': > break > elif cmd == 'add': > itmlst = add_element(itmlst,itmcntr) > elif cmd == 'print': > print 'current item:', itm > else: > print 'invalid' > itmcntr += 1 > print 'finished with', itm, 'in', itmlst > print len(itmlst), 'total items in list' > > If I provide the inputs: [print add print break print break] at the prompt, > I get this output > > current item: ['a'] > current item: ['a'] > finished with ['a'] in [['a'], ['new'], ['b']] > current item: ['b'] > finished with ['b'] in [['a'], ['new'], ['b']] > 3 total items in list > > The new element got added, but it wasn't used in the iteration over the list > of items. Other than setting up a counter and calling len() each loop, is > there some way to have the changing list recognized within the for loop? > > Thanks in advance for any help. > > Ed > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Usually handled by iterating over a copy of the list. for itm in itmlst[:]: -- Cheers. Mark Lawrence. From illusiontechniques at gmail.com Wed Oct 10 22:45:18 2012 From: illusiontechniques at gmail.com (c smith) Date: Wed, 10 Oct 2012 16:45:18 -0400 Subject: [Tutor] What can I do if I'm banned from a website?? In-Reply-To: References: Message-ID: how could someone know enough to write their own web-scraping program and NOT know that this is not about python or how to get around this problem? -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Wed Oct 10 22:53:18 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 10 Oct 2012 21:53:18 +0100 Subject: [Tutor] What can I do if I'm banned from a website?? In-Reply-To: References: Message-ID: On 10/10/2012 21:35, Benjamin Fishbein wrote: > I've been scraping info from a website with a url program I wrote. But now I can't open their webpage, no matter which web browser I use. I think they've somehow blocked me. How can I get back in? Is it a temporary block? And can I get in with the same computer from a different wifi? > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Grovel? Bribery? Threaten violence? Don't break their Ts & Cs in the first place? And what has this got to do with Python? -- Cheers. Mark Lawrence. From wprins at gmail.com Wed Oct 10 22:56:25 2012 From: wprins at gmail.com (Walter Prins) Date: Wed, 10 Oct 2012 21:56:25 +0100 Subject: [Tutor] What can I do if I'm banned from a website?? In-Reply-To: References: Message-ID: Hi On 10 October 2012 21:35, Benjamin Fishbein wrote: > I've been scraping info from a website with a url program I wrote. But now I can't open their webpage, no matter which web browser I use. I think they've somehow blocked me. How can I get back in? Is it a temporary block? And can I get in with the same computer from a different wifi? Hard to know for certain what they've done, perhaps they've blocked your IP. You can try connecting from another IP and see if that works. 2 points: 1) If you're going to be scraping websites, you should always play nice with the web-server -- throttle your requests (put some random delay between them) so they don't hammer the web-server too hard. Not doing this will enrage any webmaster. He'll be very quick to figure out why his website's being hammered, from where (the IP) and then block you. You'd probably do the same if you ran a website and you noticed some particular IP hammering your site.. 2) You should ideally always respect websites wishes regarding bots and scraping. If they don't want automated bots to be scraping them then you should really not scrape that site. And if you're going to disregard their wishes and scrape it anyway (not recommended), then all bets are off and you'll have to fly "under the radar" and ensure that your scraping app looks as much like a browser as possible (probably using modified headers that looks like what a browser will send) and behaves as much like a human operator driving a browser as possible, or you'll find yourself blocked as you've experienced above. Walter From d at davea.name Wed Oct 10 23:09:13 2012 From: d at davea.name (Dave Angel) Date: Wed, 10 Oct 2012 17:09:13 -0400 Subject: [Tutor] iterating over a changing list In-Reply-To: <006e01cda720$c06d40f0$4147c2d0$@com> References: <006e01cda720$c06d40f0$4147c2d0$@com> Message-ID: <5075E3F9.5050303@davea.name> On 10/10/2012 03:52 PM, Ed Owens wrote: > I'm trying to iterate over a list of elements, and make changes to the list > in front of the element I'm currently working with. I can update the list, > but the 'for' doesn't see the new element. Here's the code: > > import string > > def add_element(items, point): > items = items[:point+1][:] + [['new']] + items[point+1:] > return items This function doesn't change its input object at all, it's just creates and returns a new one. > def main(): > pass > > itmlst = [['a'],['b']] > itmcntr = 0 > > for itm in itmlst: > cmd = '' > while True: > cmd = raw_input('break, add, print:') > if cmd == 'break': > break > elif cmd == 'add': > itmlst = add_element(itmlst,itmcntr) Now you've created a brand new list, and bound it to the itemlst variable, but you're still iterating over the original list. > elif cmd == 'print': > print 'current item:', itm > else: > print 'invalid' > itmcntr += 1 > print 'finished with', itm, 'in', itmlst > print len(itmlst), 'total items in list' > > If I provide the inputs: [print add print break print break] at the prompt, > I get this output > > current item: ['a'] > current item: ['a'] > finished with ['a'] in [['a'], ['new'], ['b']] > current item: ['b'] > finished with ['b'] in [['a'], ['new'], ['b']] > 3 total items in list > > The new element got added, but it wasn't used in the iteration over the list > of items. Other than setting up a counter and calling len() each loop, is > there some way to have the changing list recognized within the for loop? > > Usually, people have the reverse problem, one of seeming to stutter on one of the items, or skipping one. And the cure for either of those is (as Mark tried to point out) to make a new list object to iterate over. if you're sure you want to insert items in the original list that you're looping on, then you need to change two places. One is the add_element() function needs to either use the insert() or an equivalent set of slices. use the insert() method, since it's easier. And don't bother returning the list, since you're not creating a new one. Convention is to either return a new object, or modify an object in place, but not both. The other place to change is the place where you call that function. It should NOT bind the variable to the return value, since it doesn't want a new list object. I haven't studied the rest of your code, as it didn't make much sense to me. But it's probably a simplified version of whatever you are trying to do, so that should be fine. -- DaveA From eryksun at gmail.com Wed Oct 10 23:49:19 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 10 Oct 2012 17:49:19 -0400 Subject: [Tutor] iterating over a changing list In-Reply-To: <006e01cda720$c06d40f0$4147c2d0$@com> References: <006e01cda720$c06d40f0$4147c2d0$@com> Message-ID: On Wed, Oct 10, 2012 at 3:52 PM, Ed Owens wrote: > > import string Why are you importing "string"? Most string functions one would need are methods of str/unicode. Sometimes "string" is still required, however. > def add_element(items, point): > items = items[:point+1][:] + [['new']] + items[point+1:] > return items items[:point+1][:] creates a copy of part of the list with the slice [:point+1], and then it copies the copy with the slice [:]. Redundant operations aside, this is returning a new list object. That's not going to work since the loop iterator is bound to the original list. You can't rebind a listiterator in the middle of a for loop. Also, generally avoid mutating a list while iterating over it. listiterator is just incrementing an index, so modifying the size of the list can produce nonsense (e.g. if you remove the current item, the next item will be skipped). Instead, create an empty list and append() to it. From steve at pearwood.info Thu Oct 11 02:56:42 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 11 Oct 2012 11:56:42 +1100 Subject: [Tutor] What can I do if I'm banned from a website?? In-Reply-To: References: Message-ID: <5076194A.4060808@pearwood.info> On 11/10/12 07:35, Benjamin Fishbein wrote: > I've been scraping info from a website with a url program I wrote. But >now I can't open their webpage, no matter which web browser I use. I >think they've somehow blocked me. How can I get back in? Is it a >temporary block? How the hell would we know??? Ask the people running the web site. If you have been breaking the terms and conditions of the web site, you could have broken the law (computer trespass). I don't say this because I approve of or agree with the law, but when you scrape websites with anything other than a browser, that's the chance you take. > And can I get in with the same computer from a different wifi? *rolls eyes* You've been blocked once. You want to get blocked again? A lot of this depends on what the data is, why it is put on the web in the first place, and what you intend doing with it. Wait a week and see if the block is undone. Then: * If the web site gives you an official API for fetching data, USE IT. * If not, keep to their web site T&C. If the T&C allows scraping under conditions (usually something on the lines of limiting how fast you can scrape, or at what times), OBEY THOSE CONDITIONS and don't be selfish. * If you think the webmaster will be reasonable, ask permission first. (I don't recommend that you volunteer the information that you were already blocked once.) If he's not a dick, he'll probably say yes, under conditions (again, usually to do with time and speed). * If you insist in disregarding their T&C, don't be a dick about it. Always be an ethical scraper. If the police come knocking, at least you can say that you tried to avoid any harm from your actions. It could make the difference between jail and a good behaviour bond. - Make sure you download slowly: pause for at least a few seconds between each download, or even a minute or three. - Limit the rate that you download: you might be on high speed ADSL2, but the faster you slurp files from the website, the less bandwidth they have for others. - Use a cache so you aren't hitting the website again and again for the same files. - Obey robots.txt. Consider using a random pause between (say) 0 and 90 seconds between downloads to to more accurately mimic a human using a browser. Also consider changing your user-agent. Ethical scraping suggests putting your contact details in the user-agent string. Defensive scraping suggests mimicking Internet Explorer as much as possible. More about ethical scraping: http://stackoverflow.com/questions/4384493/how-can-i-ethically-and-legally-scrape-data-from-a-public-web-site -- Steven From robertvstepp at gmail.com Thu Oct 11 03:23:08 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 10 Oct 2012 20:23:08 -0500 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: References: <20121003063843.GB27111@ando> <506D72E0.60402@pearwood.info> Message-ID: On Tue, Oct 9, 2012 at 4:29 AM, eryksun wrote: > Python 3 lets you use any Unicode letter as an identifier, including > letter modifiers ("Lm") and number letters ("Nl"). For example: > > >>> a??b = True > >>> a??b > True > > >>> ?, ?, ?, ?, ? = range(1, 6) > >>> ?, ?, ?, ?, ? > (1, 2, 3, 4, 5) Is doing this considered good programming practice? I recall there was a recent discussion about using the actual characters in formulas instead of descriptive names, where this would make more sense to people knowledgeable in the field using the formulas; however, descriptive names might be better for those who don't have that specialty knowledge. Is there a Python community consensus on how and when it is appropriate (if ever) to use Unicode characters as identifiers? > A potential gotcha in Unicode is the design choice to have both > [C]omposed and [D]ecomposed forms of characters. For example: > > >>> from unicodedata import name, normalize > > >>> s1 = "?" > >>> name(s1) > 'LATIN SMALL LETTER U WITH DIAERESIS' > > >>> s2 = normalize("NFD", s1) > >>> list(map(name, s2)) > ['LATIN SMALL LETTER U', 'COMBINING DIAERESIS'] > > These combine as one glyph when printed: > > >>> print(s2) > u? > > Different forms of the 'same' character won't compare as equal unless > you first normalize them to the same form: > > >>> s1 == s2 > False > >>> normalize("NFC", s1) == normalize("NFC", s2) > True This looks to make alphabetical sorting potentially much more complex. I will have to give this some thought once I know more. >> I don't see a mention of byte strings mentioned in the index of my >> text. Are these just the ASCII character set? After seeing your explanation below, I was able to find the relevant material in my book. It was under "bytes type" and "bytearray type". For some reason these categories did not "click" in my head as what Steve was addressing. > A bytes object (and its mutable cousin bytearray) is a sequence of > numbers, each in the range of a byte (0-255). bytes literals start > with b, such as b'spam' and can only use ASCII characters, as does the > repr of bytes. Slicing returns a new bytes object, but an index or > iteration returns integer values: > > >>> b'spam'[:3] > b'spa' > >>> b'spam'[0] > 115 > >>> list(b'spam') > [115, 112, 97, 109] > > bytes have string methods as a convenience, such as find, split, and > partition. They also have the method decode(), which uses a specified > encoding such as "utf-8" to create a string from an encoded bytes > sequence. What is the intended use of byte types? Thanks! This continues to be quite informative and this thread is greatly helping me to make better sense of the information that I am self-studying. -- Cheers! boB From steve at pearwood.info Thu Oct 11 03:44:03 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 11 Oct 2012 12:44:03 +1100 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: References: <20121003063843.GB27111@ando> <506D72E0.60402@pearwood.info> Message-ID: <50762463.8070300@pearwood.info> On 11/10/12 12:23, boB Stepp wrote: > On Tue, Oct 9, 2012 at 4:29 AM, eryksun wrote: > >> Python 3 lets you use any Unicode letter as an identifier, including >> letter modifiers ("Lm") and number letters ("Nl"). For example: >> >> >>> a??b = True >> >>> a??b >> True >> >> >>> ?, ?, ?, ?, ? = range(1, 6) >> >>> ?, ?, ?, ?, ? >> (1, 2, 3, 4, 5) > > Is doing this considered good programming practice? Not really, but it depends who is doing it and why. If you have a piece of code that is only going to be maintained by people speaking French, with French keyboards, then why not use French words for identifiers? That includes those French letters with accents. Python 3 lets you do so. Silly bits of code like ? = 4 (or worse, ? = 9) should be avoided because they are silly, not because they are illegal. That's about the same as using: eine, zwei, drei, vier, f?nf = range(1, 6) in code intended to be read by English speakers, only even harder to type. Remember that programmers *discourage* most misspellings of words (with a few exceptions, usually abbreviations): number_of_pages = 42 is preferred to: nombar_off_paiges = 42 But for non-English speakers, most languages *force* them to either write code in Foreign (foreign *to them*), or to misspell words. Allowing Unicode identifiers means that they can write in their native tongue, using correct spelling, *if they so choose*. Of course, if you want your code to be readable world-wide, stick to English :) -- Steven From steve at pearwood.info Thu Oct 11 03:50:50 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 11 Oct 2012 12:50:50 +1100 Subject: [Tutor] iterating over a changing list In-Reply-To: References: <006e01cda720$c06d40f0$4147c2d0$@com> Message-ID: <507625FA.1010102@pearwood.info> On 11/10/12 08:49, eryksun wrote: > Also, generally avoid mutating a list while iterating over it. > listiterator is just incrementing an index, so modifying the size of > the list can produce nonsense (e.g. if you remove the current item, > the next item will be skipped). Instead, create an empty list and > append() to it. If you absolutely have to modify the list you are iterating over, iterate over it backwards: # this doesn't work correctly for i in range(len(mylist)): x = mylist[i] if x < 0: del mylist[i] # this does for i in range(len(mylist)-1, -1, -1): x = mylist[i] if x < 0: del mylist[i] But really, don't do that either. Iterate over a copy, or make a new list with the items you want. It's faster and easier. -- Steven From alan.gauld at btinternet.com Thu Oct 11 09:54:53 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 11 Oct 2012 08:54:53 +0100 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: References: <20121003063843.GB27111@ando> <506D72E0.60402@pearwood.info> Message-ID: On 11/10/12 02:23, boB Stepp wrote: >> bytes have string methods as a convenience, such as find, split, and >> partition. They also have the method decode(), which uses a specified >> encoding such as "utf-8" to create a string from an encoded bytes >> sequence. > > What is the intended use of byte types? One purpose is to facilitate the handling of raw data streams such as might be read from a binary file or over a network. If you are using locale settings with 16 bit characters reading such a stream as a character string will result in you processing pairs of bytes at a time. Using a byte string you guarantee you process 8 bits at a time with no attempt at interpretation. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From breamoreboy at yahoo.co.uk Thu Oct 11 09:56:20 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 11 Oct 2012 08:56:20 +0100 Subject: [Tutor] Python Editor/IDE was Why difference between printing string & typing its object reference at the prompt? In-Reply-To: References: Message-ID: On 03/10/2012 04:15, boB Stepp wrote: > After much diddling around I have finally settled on a text to study > (Programming in Python 3, 2nd edition, by Mark Summerfield) and have > defaulted to using IDLE, deferring worrying about editors/IDEs until I > feel comfortable in Python. > I've been using Eclipse and Pydev and find it incredible. I've been converting J word code into Python and this combination has greatly eased the task. Finding errors as I edit instead of at run time makes an awesome difference to my productivity. Quite why I was happy to slag off Eclipse maybe six months ago I don't know. Does a good sized portion of humble pie make amends? -- Cheers. Mark Lawrence. From eryksun at gmail.com Thu Oct 11 10:40:52 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 11 Oct 2012 04:40:52 -0400 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: References: <20121003063843.GB27111@ando> <506D72E0.60402@pearwood.info> Message-ID: On Wed, Oct 10, 2012 at 9:23 PM, boB Stepp wrote: > >> >>> a??b = True >> >>> a??b >> True >> >> >>> ?, ?, ?, ?, ? = range(1, 6) >> >>> ?, ?, ?, ?, ? >> (1, 2, 3, 4, 5) > > Is doing this considered good programming practice? The examples were meant to highlight the absurdity of using letter modifiers and number letters in identifiers. I should have clearly stated that I think these names are bad. >> bytes have string methods as a convenience, such as find, split, and >> partition. They also have the method decode(), which uses a specified >> encoding such as "utf-8" to create a string from an encoded bytes >> sequence. > > What is the intended use of byte types? bytes objects are important for low-level data processing, such as file and socket I/O. The fundamental addressable value in a computer is a byte (at least for all common, modern computers). When you write a string to a file or socket, it has to be encoded as a sequence of bytes. For example, consider the character "?" (MATHEMATICAL DOUBLE-STRUCK DIGIT NINE) with decimal code 120801 (0x1d71e in hexadecimal): >>> ord("?") 120801 Three common ways to encode this character are as UTF-32, UTF-16, and UTF-8. The UTF-32 encoding is the UCS4 format used by strings in main memory on a "wide" build (Python 3.3 uses a more efficient scheme that uses 1, 2, or 4 bytes as required). >>> s.encode("utf-32") b'\xff\xfe\x00\x00\xe1\xd7\x01\x00' The "utf-32" string encoder also includes a byte order mark (BOM) in the first 4 bytes of the encoded sequence (0xfffe0000). The order of the BOM determines that this is a little-endian, 4-byte encoding. http://en.wikipedia.org/wiki/Endianness You can use int.from_bytes() to verify that b'\xe1\xd7\x01\x00' is the number 120801 stored as 4 bytes in little-endian order: >>> int.from_bytes(b'\xe1\xd7\x01\x00', 'little') 120801 or crunch the numbers in a generator expression: >>> sum(x * 256**i for i,x in enumerate(b'\xe1\xd7\x01\x00')) 120801 UTF-32 is an inefficient way to represent Unicode. Characters in the BMP, which are by far the most common, only require at most 2 bytes. UTF-16 uses 2 bytes for BMP codes, like the original UCS2, and a 4-byte surrogate-pair encoding for characters in the supplementary planes. Here's the character "?" encoded as UTF-16: >>> list(map(hex, s.encode('utf-16'))) ['0xff', '0xfe', '0x35', '0xd8', '0xe1', '0xdf'] Again there's a BOM, 0xfffe, which describes the order and number of bytes per code (i.e. 2 bytes, little endian). The character itself is stored as the surrogate pair [0xd835, 0xdfe1]. You can read more about surrogate pair encoding in the UTF-16 Wikipedia article: http://en.wikipedia.org/wiki/UTF-16 A "narrow" build of Python uses UCS2 + surrogates. It's not quite UTF-16 since it doesn't treat a surrogate pair as a single character for iteration, string length, and indexing. Python 3.3 eliminates narrow builds. Another common encoding is UTF-8. This maps each code to 1-4 bytes, without requiring a BOM (though the 3-byte BOM 0xefbbbf can be used when saving to a file). Since ASCII is so common, and since on many systems backward compatibility with ASCII is required, UTF-8 includes ASCII as a subset. In other words, codes below 128 are stored unmodified as a single byte. Non-ASCII codes are encoded as 2-4 bytes. See the UTF-8 Wikipedia article for the details: http://en.wikipedia.org/wiki/UTF-8#Description The character "?" requires 4 bytes in UTF-8: >>> s = "?" >>> sb = s.encode("utf-8") >>> sb b'\xf0\x9d\x9f\xa1' >>> list(sb) [240, 157, 159, 161] If you iterate over the encoded bytestring, the numbers 240, 157, 159, and 161 -- taken separately -- have no special significance. Neither does the length of 4 tell you how many characters are in the bytestring. With a decoded string, in contrast, you know how many characters it has (assuming you've normalized to "NFC" format) and can iterate through the characters in a simple for loop. If your terminal/console uses UTF-8, you can write the UTF-8 encoded bytes directly to the stdout buffer: >>> sys.stdout.buffer.write(b'\xf0\x9d\x9f\xa1' + b'\n') ? 5 This wrote 5 bytes: 4 bytes for the "?" character, plus b'\n' for a newline. Strings in Python 2 In Python 2, str is a bytestring. Iterating over a 2.x str yields single-byte characters. However, these generally aren't 'characters' at all (this goes back to the C programming language "char" type), not unless you're working with a single-byte encoding such as ASCII or Latin-1. In Python 2, unicode is a separate type and unicode literals require a u prefix to distinguish them from bytestrings, just as bytes literals in Python 3 require a b prefix to distinguish them from strings. Python 2.6 and 2.7 alias str to the name "bytes", and they support the b prefix in literals. These were added to ease porting to Python 3, but bear in mind that it's still a classic bytestring, not a bytes object. For example, in 2.x you can use ord() with an item of a bytestring, such as ord(b"ABC"[0]), but this won't work in 3.x because b"ABC"[0] returns the integer 65. On the other hand, ord(b"A") does work in 3.x. Python 2.6 also added "__future__.unicode_literals" to make string literals default to unicode without having to use the u prefix. bytestrings then require the b prefix. From d at davea.name Thu Oct 11 11:04:59 2012 From: d at davea.name (Dave Angel) Date: Thu, 11 Oct 2012 05:04:59 -0400 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: References: <20121003063843.GB27111@ando> <506D72E0.60402@pearwood.info> Message-ID: <50768BBB.9000201@davea.name> On 10/11/2012 04:40 AM, eryksun wrote: > On Wed, Oct 10, 2012 at 9:23 PM, boB Stepp wrote: > . >> What is the intended use of byte types? > > bytes objects are important for low-level data processing, such as > file and socket I/O. The fundamental addressable value in a computer > is a byte (at least for all common, modern computers). When you write > a string to a file or socket, it has to be encoded as a sequence of > bytes. > > > > Another common encoding is UTF-8. This maps each code to 1-4 bytes, Actually, the upper limit for a decoded utf-8 character is at least 6 bytes. I think it's 6, but it's no less than 6. > without requiring a BOM (though the 3-byte BOM 0xefbbbf can be used > when saving to a file). Since ASCII is so common, and since on many > systems backward compatibility with ASCII is required, UTF-8 includes > ASCII as a subset. In other words, codes below 128 are stored > unmodified as a single byte. Non-ASCII codes are encoded as 2-4 bytes. > See the UTF-8 Wikipedia article for the details: > > http://en.wikipedia.org/wiki/UTF-8#Description This shows cases for up to 6 bytes. > Three other thing worth pointing out: 1) Python didn't define all these byte formats. These are standards which exist outside of the python world, and Python lets you coexist with them. If you want to create a text file that can be seen properly by an editor that only supports utf-8, you can't output UCS-4 and expect it to come up with anything but gibberish. 2) There are many more byte formats, most of them predating Unicode entirely. Many of these are specific to a particular language or national environment, and contain just those extensions to ASCII that the particular language deems useful. Python provides encoders and decoders to many of these as well. 3) There are many things read and written in byte format that have no relationship to characters. The notion of using text formats for all data (eg. xml) is a fairly recent one. Binary files are quite common, and many devices require binary transfers to work at all. So byte strings are not necessarily strings at all. -- DaveA From eryksun at gmail.com Thu Oct 11 11:21:39 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 11 Oct 2012 05:21:39 -0400 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: <50768BBB.9000201@davea.name> References: <20121003063843.GB27111@ando> <506D72E0.60402@pearwood.info> <50768BBB.9000201@davea.name> Message-ID: On Thu, Oct 11, 2012 at 5:04 AM, Dave Angel wrote: > > Actually, the upper limit for a decoded utf-8 character is at least 6 > bytes. I think it's 6, but it's no less than 6. Yes, but what would be the point? Unicode only has 17 planes, up to code 0x10ffff. It's limited by UTF-16. > 2) There are many more byte formats, most of them predating Unicode > entirely. Many of these are specific to a particular language or > national environment, and contain just those extensions to ASCII that > the particular language deems useful. Python provides encoders and > decoders to many of these as well. I mentioned 3 common formats that can completely represent Unicode since this thread is mostly about Python 3 strings and repr -- at least it started that way. > 3) There are many things read and written in byte format that have no > relationship to characters. The notion of using text formats for all > data (eg. xml) is a fairly recent one. Binary files are quite common, > and many devices require binary transfers to work at all. So byte > strings are not necessarily strings at all. Sure, other than encoded strings, there are also more obvious examples of data represented as bytes -- at least I hope they're obvious -- such as multimedia audio/video/images, sensor data, spreadsheets, and so on. In main memory these exist as data structures/objects (bytes, but not generally in a form suitable for transmission or storage). Before being saved to files or network streams, the data is transformed to serialize and pack it as a byte stream (e.g. the struct module, or pickle which defaults to a binary protocol in Python 3), possibly compress it to a smaller size and add error correction (e.g. the gzip module), and possibly encrypt it for security (e.g. PyCrypto). From d at davea.name Thu Oct 11 11:42:34 2012 From: d at davea.name (Dave Angel) Date: Thu, 11 Oct 2012 05:42:34 -0400 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: References: <20121003063843.GB27111@ando> <506D72E0.60402@pearwood.info> <50768BBB.9000201@davea.name> Message-ID: <5076948A.5070600@davea.name> On 10/11/2012 05:21 AM, eryksun wrote: > On Thu, Oct 11, 2012 at 5:04 AM, Dave Angel wrote: >> >> Actually, the upper limit for a decoded utf-8 character is at least 6 >> bytes. I think it's 6, but it's no less than 6. > > Yes, but what would be the point? Unicode only has 17 planes, up to > code 0x10ffff. It's limited by UTF-16. More importantly, it was restricted by the 2003 rfc 3629, which I had completely missed. Last time I wrote a utf-8 encoder was before that probably about 1997. http://tools.ietf.org/html/rfc3629 Thanks for pointing it out. -- DaveA From sunil.techspk at gmail.com Thu Oct 11 13:13:21 2012 From: sunil.techspk at gmail.com (Sunil Tech) Date: Thu, 11 Oct 2012 16:43:21 +0530 Subject: [Tutor] Files Merging Message-ID: Hi all, Greetings to you... it been so helpful for me to go through your all mails & support & i wish it still continues. I have two text files. text1 contains This is from Text1 --- 1st line This is from Text1 --- 2nd line This is from Text1 --- 3rd line This is from Text1 --- 4th line This is from Text1 --- 5th line text2 contains This is from Text2 --- 1st line This is from Text2 --- 2nd line This is from Text2 --- 3rd line This is from Text2 --- 4th line This is from Text2 --- 5th line i want result in text3 like This is from Text1 --- 1st line This is from Text2 --- 1st line This is from Text1 --- 2nd line This is from Text2 --- 2nd line This is from Text1 --- 3rd line This is from Text2 --- 3rd line This is from Text1 --- 4th line This is from Text2 --- 4th line This is from Text1 --- 5th line This is from Text2 --- 5th line but condition is "should not use any loops" waiting for your reply, thank you in advance. Regards, Sunil G. -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Thu Oct 11 14:00:04 2012 From: d at davea.name (Dave Angel) Date: Thu, 11 Oct 2012 08:00:04 -0400 Subject: [Tutor] Files Merging In-Reply-To: References: Message-ID: <5076B4C4.1000407@davea.name> On 10/11/2012 07:13 AM, Sunil Tech wrote: > Hi all, > > Greetings to you... > it been so helpful for me to go through your all mails & support & i wish > it still continues. > > I have two text files. > > text1 contains > > This is from Text1 --- 1st line > This is from Text1 --- 2nd line > This is from Text1 --- 3rd line > This is from Text1 --- 4th line > This is from Text1 --- 5th line > > text2 contains > This is from Text2 --- 1st line > This is from Text2 --- 2nd line > This is from Text2 --- 3rd line > This is from Text2 --- 4th line > This is from Text2 --- 5th line > > > i want result in text3 like > > This is from Text1 --- 1st line > This is from Text2 --- 1st line > This is from Text1 --- 2nd line > This is from Text2 --- 2nd line > This is from Text1 --- 3rd line > This is from Text2 --- 3rd line > This is from Text1 --- 4th line > This is from Text2 --- 4th line > This is from Text1 --- 5th line > This is from Text2 --- 5th line > > but condition is "should not use any loops" > > waiting for your reply, > thank you in advance. > > Regards, > Sunil G. > > What are the other constraints on this homework assignment? Are list comprehensions permitted? Seems likely you can do it readily with a list comprehension using zip(). One line, total. -- DaveA From sunil.techspk at gmail.com Thu Oct 11 14:27:58 2012 From: sunil.techspk at gmail.com (Sunil Tech) Date: Thu, 11 Oct 2012 17:57:58 +0530 Subject: [Tutor] Files Merging In-Reply-To: <5076B4C4.1000407@davea.name> References: <5076B4C4.1000407@davea.name> Message-ID: i used zip(), but it gives me result in list of tuples format. But i don't get in a exact expect format (as mentioned) no loopings are allowed. On Thu, Oct 11, 2012 at 5:30 PM, Dave Angel wrote: > On 10/11/2012 07:13 AM, Sunil Tech wrote: > > Hi all, > > > > Greetings to you... > > it been so helpful for me to go through your all mails & support & i wish > > it still continues. > > > > I have two text files. > > > > text1 contains > > > > This is from Text1 --- 1st line > > This is from Text1 --- 2nd line > > This is from Text1 --- 3rd line > > This is from Text1 --- 4th line > > This is from Text1 --- 5th line > > > > text2 contains > > This is from Text2 --- 1st line > > This is from Text2 --- 2nd line > > This is from Text2 --- 3rd line > > This is from Text2 --- 4th line > > This is from Text2 --- 5th line > > > > > > i want result in text3 like > > > > This is from Text1 --- 1st line > > This is from Text2 --- 1st line > > This is from Text1 --- 2nd line > > This is from Text2 --- 2nd line > > This is from Text1 --- 3rd line > > This is from Text2 --- 3rd line > > This is from Text1 --- 4th line > > This is from Text2 --- 4th line > > This is from Text1 --- 5th line > > This is from Text2 --- 5th line > > > > but condition is "should not use any loops" > > > > waiting for your reply, > > thank you in advance. > > > > Regards, > > Sunil G. > > > > > > What are the other constraints on this homework assignment? Are list > comprehensions permitted? Seems likely you can do it readily with a > list comprehension using zip(). One line, total. > > > > -- > > DaveA > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Thu Oct 11 14:30:28 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 11 Oct 2012 08:30:28 -0400 Subject: [Tutor] Files Merging In-Reply-To: References: Message-ID: On Thu, Oct 11, 2012 at 7:13 AM, Sunil Tech wrote: > > text1 contains > This is from Text1 --- 1st line > .... > > text2 contains > This is from Text2 --- 1st line > .... > > i want result in text3 like > This is from Text1 --- 1st line > This is from Text2 --- 1st line > .... > but condition is "should not use any loops" Use itertools.zip_longest() and itertools.chain.from_iterable(), along with the text3.writelines(). zip_longest allows the files to be of different lengths. zip would terminate at the shortest number of lines, but fillvalue of zip_longest supplies a default value (set to an empty string). chain.from_iterable joins the tuples from zip_longest as one iterable to use as an argument to writelines(). This way there are no pure Python loops, or even generator expressions/comprehensions. From joel.goldstick at gmail.com Thu Oct 11 14:50:10 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Thu, 11 Oct 2012 08:50:10 -0400 Subject: [Tutor] Files Merging In-Reply-To: References: Message-ID: On Thu, Oct 11, 2012 at 8:30 AM, eryksun wrote: > On Thu, Oct 11, 2012 at 7:13 AM, Sunil Tech wrote: >> >> text1 contains >> This is from Text1 --- 1st line >> .... >> >> text2 contains >> This is from Text2 --- 1st line >> .... >> >> i want result in text3 like >> This is from Text1 --- 1st line >> This is from Text2 --- 1st line >> .... zip gets you tuples. map can operate on those tuples I just tried this: >>> x = [1,2,3] >>> y = [4,5,6] >>> def print_2(t): ... print t[0], t[1] ... >>> z = zip(x,y) >>> z [(1, 4), (2, 5), (3, 6)] >>> r = map(print_2, z) 1 4 2 5 3 6 >>> You need to write a function that writes the tuple to a file. It will look something like my print_2() function -- Joel Goldstick From sunil.techspk at gmail.com Thu Oct 11 15:51:11 2012 From: sunil.techspk at gmail.com (Sunil Tech) Date: Thu, 11 Oct 2012 19:21:11 +0530 Subject: [Tutor] Files Merging In-Reply-To: References: Message-ID: Thanks all for your immediate responses :) On Thu, Oct 11, 2012 at 6:20 PM, Joel Goldstick wrote: > On Thu, Oct 11, 2012 at 8:30 AM, eryksun wrote: > > On Thu, Oct 11, 2012 at 7:13 AM, Sunil Tech > wrote: > >> > >> text1 contains > >> This is from Text1 --- 1st line > >> .... > >> > >> text2 contains > >> This is from Text2 --- 1st line > >> .... > >> > >> i want result in text3 like > >> This is from Text1 --- 1st line > >> This is from Text2 --- 1st line > >> .... > > zip gets you tuples. map can operate on those tuples > > I just tried this: > >>> x = [1,2,3] > >>> y = [4,5,6] > >>> def print_2(t): > ... print t[0], t[1] > ... > >>> z = zip(x,y) > >>> z > [(1, 4), (2, 5), (3, 6)] > > >>> r = map(print_2, z) > 1 4 > 2 5 > 3 6 > >>> > > You need to write a function that writes the tuple to a file. It will > look something like my print_2() function > -- > Joel Goldstick > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Oct 11 19:53:44 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 11 Oct 2012 18:53:44 +0100 Subject: [Tutor] Python Editor/IDE was Why difference between printing string & typing its object reference at the prompt? In-Reply-To: References: Message-ID: On 11/10/12 08:56, Mark Lawrence wrote: > an awesome difference to my productivity. Quite why I was happy to slag > off Eclipse maybe six months ago I don't know. Does a good sized > portion of humble pie make amends? Eclipse is a heavyweight tool designed for heavyweight problems. For the average Python tutor reader it's way more than is needed. But when you do need to do some heavy lifting, especially at a project rather than file level, Eclipse is a great tool (and pydev a great plugin). -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From chigga101 at gmail.com Thu Oct 11 21:24:30 2012 From: chigga101 at gmail.com (Matthew Ngaha) Date: Thu, 11 Oct 2012 20:24:30 +0100 Subject: [Tutor] need an explanation Message-ID: i need help on 2 topics. 1) can someone please tell me what sys is doing, and why its using weird indexing? if __name__ == "__main__": A_Class(*sys.argv[1:4]).A_Class_Method() is sys able to call methods? if so why does it need indexing if it uses * . ------------------------------------------------------------------------------------------------------ 2) also i need help with zipfiles. these 2 functions are related in the same class. def __init__(self): self.zipping_directory = "unzipped-{}".format(filename) def _full_filename(self, filename): return os.path.join(self.zipping_directory, filename) def zip_files(self): file = zipfile.ZipFile(self.filename, 'w') for filename in os.listdir(self.zipping_directory): file.write(self._full_filename(filename), filename) the main thing i need help with is the last line. the zip file is writing to a file but why does it use the same argument twice? the for loop above that line returns the file from the zipping directory, which is the 2nd argument on file.write? But the 1st argument is using that same file because that is the file returned from the def _full_filename(self, filename): method. so please can someone tell me why it uses the same file argument twice in its write method? thanks for your time -------------- next part -------------- An HTML attachment was scrubbed... URL: From ramit.prasad at jpmorgan.com Thu Oct 11 22:05:03 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Thu, 11 Oct 2012 20:05:03 +0000 Subject: [Tutor] need an explanation In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF474166E9083@SCACMX008.exchad.jpmchase.net> Matthew Ngaha wrote: > i need help on 2 topics. > > 1) can someone please tell me what sys is doing, and why its using weird indexing? > > if __name__ == "__main__": > ??? A_Class(*sys.argv[1:4]).A_Class_Method() > > is sys able to call methods? if so why does it need indexing if it uses * . Sys is a module. What do you mean by calling methods? Sys does nothing unless you ask it to do something. In the above example, there are several things going on. I will try and help what is going on. 1. sys.argv[1:4] - sys.argv is the list of arguments provided to Python (E.g. For the command `python test.py argument1, argument2` sys.argv would be ['test.py', 'argument1', 'argument2']. So going back to your code. "[1:4]" is a technique called slicing. It creates a new (sliced) list with elements from sys.argv. To understand what is being sliced, puzzle through this short sample. >>>[1,2,3,4,5][1:4] [2, 3, 4] 2. *sys.argv[1:4] - The asterisk tells Python to use the list (in this case the new sliced list) as a list of arguments for some function. 3. A_Class() - Create an instance of class A_Class 4. A_Class().A_Class_Method() - Call the A_Class_Method function on the newly created instance object of class A_Class. > > > ------------------------------------------------------------------------------------------------------ > 2) also i need help with zipfiles. these 2 functions are related in the same class. > > def __init__(self): > ??? self.zipping_directory = "unzipped-{}".format(filename) > > def _full_filename(self, filename): > ??????? return os.path.join(self.zipping_directory, filename) > > def zip_files(self): > ??????? file = zipfile.ZipFile(self.filename, 'w') > ??????? for filename in os.listdir(self.zipping_directory): > ??????????? file.write(self._full_filename(filename), filename) > > the main thing i need help with is the last line. the zip file is writing to a file but why does it use the same > argument twice? the for loop above that line returns the file from the zipping directory, which is the 2nd > argument on file.write? But the 1st argument is using that same file because that is the file returned from the > def _full_filename(self, filename): method. so please can someone tell me why it uses the same file argument > twice in its write method? You can actually use Python to find out a great deal about questions like this. This is one of the reasons I like Python's interactive prompt. >>> help(zipfile.ZipFile.write ) Help on method write in module zipfile: write(self, filename, arcname=None, compress_type=None) unbound zipfile.ZipFile method Put the bytes from filename into the archive under the name arcname. So in this case, the first filename is being zipped while the second filename is the name that will be shown *inside* the zip. > > thanks for your time You are welcome. Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From d at davea.name Thu Oct 11 22:09:53 2012 From: d at davea.name (Dave Angel) Date: Thu, 11 Oct 2012 16:09:53 -0400 Subject: [Tutor] need an explanation In-Reply-To: References: Message-ID: <50772791.1000408@davea.name> On 10/11/2012 03:24 PM, Matthew Ngaha wrote: > i need help on 2 topics. > > 1) can someone please tell me what sys is doing, and why its using weird > indexing? > > if __name__ == "__main__": > A_Class(*sys.argv[1:4]).A_Class_Method() sys isn't being indexed. sys is a module (presumably you have an import somewhere above this line). In the module, there's a list argv. That list is being indexed in the common ways. When the index contains one or more colons, it's called a slice. A slice is another list of zero or more items from this list. If you don't understand slices, look it up in your book, or on python.org argv itself represents the commandline arguments passed when the script was started. argv[0] is the name of the script (more or less), and argv[1], argv[2], argv[3], and argv[4] (etc.) are parameters. You can use len(sys.argv) to see how big the list is. If A_Class_Method is really a class method, then it's a waste of time creating an instance. You might as well use A_Class.A_Class_Method() But my guess is that it's NOT a class method, just a confusing name. > is sys able to call methods? if so why does it need indexing if it uses * . Where do you see any method of sys being used? I suspect you're getting confused because there are many things on one line, and you don't know how to decompose it. That line is roughly equivalent to: args = sys.argv[1:4] # build a list of up to 3 items obj = A_Class(*args) # instantiate A_Class with up to 3 arguments obj.A_Class_Method() # call the method on that instance del args del obj > > ------------------------------------------------------------------------------------------------------ > 2) also i need help with zipfiles. these 2 functions are related in the These 3 methods, not 2 functions > same class. > > def __init__(self): > self.zipping_directory = "unzipped-{}".format(filename) > > def _full_filename(self, filename): > return os.path.join(self.zipping_directory, filename) > > def zip_files(self): > file = zipfile.ZipFile(self.filename, 'w') > for filename in os.listdir(self.zipping_directory): > file.write(self._full_filename(filename), filename) > > the main thing i need help with is the last line. the zip file is writing > to a file but why does it use the same argument twice? the for loop above > that line returns the file from the zipping directory, which is the 2nd > argument on file.write? But the 1st argument is using that same file > because that is the file returned from the def _full_filename(self, > filename): method. so please can someone tell me why it uses the same file > argument twice in its write method? The two arguments to the write are different, they are not both "filename" The first argument is the return value of the call to full_filename() In other words, it's a full path to an actual file, that will be stored in the zip. The second argument is the name that will be stored in the zipfile. See http://docs.python.org/library/zipfile.html Once again, to see what's going on, try decomposing the line you're not comfortable with: fullname = self.full_filename(filename) #perhaps here you should print both strings, to see how they differ file.write(fullname, filename) del fullname If you respond, please remember NOT to top-post. -- DaveA From breamoreboy at yahoo.co.uk Thu Oct 11 22:17:57 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 11 Oct 2012 21:17:57 +0100 Subject: [Tutor] need an explanation In-Reply-To: References: Message-ID: On 11/10/2012 20:24, Matthew Ngaha wrote: > i need help on 2 topics. > > 1) can someone please tell me what sys is doing, and why its using weird > indexing? sys isn't doing anything and the weird indexing is called slicing. > > if __name__ == "__main__": > A_Class(*sys.argv[1:4]).A_Class_Method() > > is sys able to call methods? if so why does it need indexing if it uses * . sys isn't calling anything. The second, third and fourth items from sys.argv are being used via slicing to create A_Class and then A_Class_Method is called. > > > ------------------------------------------------------------------------------------------------------ > 2) also i need help with zipfiles. these 2 functions are related in the > same class. Obviously a Monty Python fan as I see 3 methods :) > > def __init__(self): > self.zipping_directory = "unzipped-{}".format(filename) Where did filename appear from above? > > def _full_filename(self, filename): > return os.path.join(self.zipping_directory, filename) > > def zip_files(self): > file = zipfile.ZipFile(self.filename, 'w') Where is self.filename set up? > for filename in os.listdir(self.zipping_directory): > file.write(self._full_filename(filename), filename) > > the main thing i need help with is the last line. the zip file is writing > to a file but why does it use the same argument twice?the for loop above > that line returns the file from the zipping directory, which is the 2nd > argument on file.write? But the 1st argument is using that same file > because that is the file returned from the def _full_filename(self, > filename): method. so please can someone tell me why it uses the same file > argument twice in its write method? I suggest that you show us some real code that will run with some print statements in appropriate places to show us what is happening. That way you may well be able to answer your own questions and learn at the same time. > > thanks for your time > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Cheers. Mark Lawrence. From emile at fenx.com Thu Oct 11 22:27:19 2012 From: emile at fenx.com (Emile van Sebille) Date: Thu, 11 Oct 2012 13:27:19 -0700 Subject: [Tutor] need an explanation In-Reply-To: References: Message-ID: <50772BA7.60008@fenx.com> Matthew Ngaha wrote: > i need help on 2 topics. > > 1) can someone please tell me what sys is doing, and why its using weird > indexing? > > if __name__ == "__main__": > A_Class(*sys.argv[1:4]).A_Class_Method() sys is doing nothing -- argv in sys holds the command line arguments passed into python. The first sys.argv[0] is the python script being executed, and the rest sys.argv[1:] are arguments passed in to that script. specifying sys.argv[1:4] means you're picking just the three items. *sys.argv[1:4] expands those from their list form and are passed into A_Class's __init__ constructor (assuming old style classes). This instantiates an instance of that class then invokes the A_Class_Method of that instance. > > is sys able to call methods? if so why does it need indexing if it uses * . > > > ------------------------------------------------------------------------------------------------------ > 2) also i need help with zipfiles. these 2 functions are related in the > same class. > > def __init__(self): > self.zipping_directory = "unzipped-{}".format(filename) > > def _full_filename(self, filename): > return os.path.join(self.zipping_directory, filename) > > def zip_files(self): > file = zipfile.ZipFile(self.filename, 'w') > for filename in os.listdir(self.zipping_directory): > file.write(self._full_filename(filename), filename) > > the main thing i need help with is the last line. the zip file is > writing to a file but why does it use the same argument twice? the first is passed into the instance's _full_filename method and the result of that becomes the first argument passed into file.write, and the second is passed in the file.write as the second argument. HTH Emile > the for > loop above that line returns the file from the zipping directory, which > is the 2nd argument on file.write? But the 1st argument is using that > same file because that is the file returned from the def > _full_filename(self, filename): method. so please can someone tell me > why it uses the same file argument twice in its write method? > > thanks for your time > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From chigga101 at gmail.com Thu Oct 11 22:48:40 2012 From: chigga101 at gmail.com (Matthew Ngaha) Date: Thu, 11 Oct 2012 21:48:40 +0100 Subject: [Tutor] need an explanation In-Reply-To: References: Message-ID: > > > Obviously a Monty Python fan as I see 3 methods :) > lol i dont know what i was looking at.. yes its 3 methods sorry:( > >> def __init__(self): >> self.zipping_directory = "unzipped-{}".format(filename) >> > > Where did filename appear from above? > > > sorry i didnt write everything. the init method also had... self.filename = filename ... i will paste the whole code > > I suggest that you show us some real code that will run with some print > statements in appropriate places to show us what is happening. That way > you may well be able to answer your own questions and learn at the same > time. > >> > > sadly when i run the file i get an error so i dont know what to doto fix it and be able to run @ DAVE.. you said sys is a module (presumably you have an import somewhere above this line). In the module, there's a list argv. the import statements are: import sys import os import shutil import zipfile so im guessing [sys, os, shutil, zipfile] these are the arguments being passed? my mind tells me no, as these are more likely the arguments in the A_Class init method? here is he full code... i changed the names in the mail to make it clearer. so the names in the code will be different. A_Class is actually ZipReplace etc.. i cant test it because on start the program returns this error and i dont know how to fix it: ZipReplace(*sys.argv[1:4]).zip_find_replace() TypeError: __init__() takes exactly 4 positional arguments (1 given) full code: import sys import os import shutil import zipfile class ZipReplace: def __init__(self, filename, search_string, replace_string): self.filename = filename self.search_string = search_string self.replace_string = replace_string self.temp_directory = "unzipped-{}".format( filename) def _full_filename(self, filename): return os.path.join(self.temp_directory, filename) def zip_find_replace(self): self.unzip_files() self.find_replace() self.zip_files() def unzip_files(self): os.mkdir(self.temp_directory) zip = zipfile.ZipFile(self.filename) try: zip.extractall(self.temp_directory) finally: zip.close() def find_replace(self): for filename in os.listdir(self.temp_directory): with open(self._full_filename(filename)) as file: contents = file.read() contents = contents.replace( self.search_string, self.replace_string) with open(self._full_filename(filename), "w") as file: file.write(contents) def zip_files(self): file = zipfile.ZipFile(self.filename, 'w') for filename in os.listdir(self.temp_directory): file.write(self._full_filename(filename), filename) shutil.rmtree(self.temp_directory) if __name__ == "__main__": ZipReplace(*sys.argv[1:4]).zip_find_replace() is a bit too advanced for me but i now see what it does.. although i wish it didnt return an error when run. so the arguments being passed are... [os, shutil, zipfile] or [filename, search_string, return_string] ? -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Thu Oct 11 23:22:31 2012 From: d at davea.name (Dave Angel) Date: Thu, 11 Oct 2012 17:22:31 -0400 Subject: [Tutor] need an explanation In-Reply-To: References: Message-ID: <50773897.6050309@davea.name> On 10/11/2012 04:48 PM, Matthew Ngaha wrote: >> >> Obviously a Monty Python fan as I see 3 methods :) >> > lol i dont know what i was looking at.. yes its 3 methods sorry:( > > >>> def __init__(self): >>> self.zipping_directory = "unzipped-{}".format(filename) >>> >> Where did filename appear from above? >> >> >> > sorry i didnt write everything. the init method also had... > self.filename = filename ... i will paste the whole code > > >> I suggest that you show us some real code that will run with some print >> statements in appropriate places to show us what is happening. That way >> you may well be able to answer your own questions and learn at the same >> time. >> >> sadly when i run the file i get an error so i dont know what to doto fix > it and be able to run > > @ DAVE.. you said > sys is a module (presumably you have an import > somewhere above this line). In the module, there's a list argv. > > the import statements are: > > import sys > import os > import shutil > import zipfile > > so im guessing [sys, os, shutil, zipfile] these are the arguments being > passed? my mind tells me no, as these are more likely the arguments in the > A_Class init method? They aren't arguments to anything. But sys.argv would be undefined if you had not imported sys. > > here is he full code... i changed the names in the mail to make it clearer. > so the names in the code will be different. A_Class is actually ZipReplace > etc.. > > i cant test it because on start the program returns this error and i dont > know how to fix it: > > ZipReplace(*sys.argv[1:4]).zip_find_replace() > TypeError: __init__() takes exactly 4 positional arguments (1 given) When you read this, what does it tell you? Decompose the statement as I showed you, and see what the args actually are by printing them. Clearly, the error message tells you that you have a list of size 0 instead of size 3. (The self argument is implied, since you're creating an instance) I suggest you look up sys.argv to see what these arguments actually mean. If you don't supply any of them, then you'll get this error. Are you learning from a tutorial? Does it have argv in its index? Have you tried googling for python sys.argv ? When you run this script, what arguments DO you type in ? python myscript.py arg1 arg2 arg3 > > full code: > > import sys > import os > import shutil > import zipfile > > class ZipReplace: > def __init__(self, filename, search_string, replace_string): > self.filename = filename > self.search_string = search_string > self.replace_string = replace_string > self.temp_directory = "unzipped-{}".format( > filename) > > def _full_filename(self, filename): > return os.path.join(self.temp_directory, filename) > > def zip_find_replace(self): > self.unzip_files() > self.find_replace() > self.zip_files() > > def unzip_files(self): > os.mkdir(self.temp_directory) > zip = zipfile.ZipFile(self.filename) > try: > zip.extractall(self.temp_directory) > finally: > zip.close() > > def find_replace(self): > for filename in os.listdir(self.temp_directory): > with open(self._full_filename(filename)) as file: > contents = file.read() > contents = contents.replace( > self.search_string, self.replace_string) > with open(self._full_filename(filename), "w") as file: > file.write(contents) > > def zip_files(self): > file = zipfile.ZipFile(self.filename, 'w') > for filename in os.listdir(self.temp_directory): > file.write(self._full_filename(filename), filename) > shutil.rmtree(self.temp_directory) > > if __name__ == "__main__": > ZipReplace(*sys.argv[1:4]).zip_find_replace() > > is a bit too advanced for me but i now see what it does.. although i wish > it didnt return an error when run. > > so the arguments being passed are... > > [os, shutil, zipfile] or [filename, search_string, return_string] ? > Those first three are imports, not arguments to anything. And the second 3 are 3 of the formal parameters to the __init__() method. The arguments come from the slice, which comes from sys.argv, which comes from the command line. You never showed us how you run the program, so how do we know what the cmdline arguments are? -- DaveA From ramit.prasad at jpmorgan.com Thu Oct 11 23:32:51 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Thu, 11 Oct 2012 21:32:51 +0000 Subject: [Tutor] need an explanation In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF474166E923C@SCACMX008.exchad.jpmchase.net> Matthew Ngaha wrote: [snip] > @ DAVE.. you said > sys is a module (presumably you have an import > somewhere above this line). In the module, there's a list argv. > > the import statements are: > > import sys > import os > import shutil > import zipfile > > so im guessing [sys, os, shutil, zipfile]? these are the arguments being passed? my mind tells me no, as these > are more likely the arguments in the A_Class init method? > > here is he full code... i changed the names in the mail to make it clearer. so the names in the code will be > different. A_Class is actually ZipReplace etc.. > > i cant test it because on start the program returns this error and i dont know how to fix it: > > ??? ZipReplace(*sys.argv[1:4]).zip_find_replace() > TypeError: __init__() takes exactly 4 positional arguments (1 given) > I suspect that you are not giving enough arguments when running your file. It needs to be something like `python test.py arg1 arg2 arg3`. You can find out by doing a `print sys.argv[1:4]` (Python 2) or `print(sys.argv[1:4])` (Python 3). > > full code: > > import sys > import os > import shutil > import zipfile > > class ZipReplace: > ??? def __init__(self, filename, search_string, replace_string): > ??????? self.filename = filename > ??????? self.search_string = search_string > ??????? self.replace_string = replace_string > ??????? self.temp_directory = "unzipped-{}".format( > ??????????????? filename) > > ?? def _full_filename(self, filename): > ??????? return os.path.join(self.temp_directory, filename) > > ??? def zip_find_replace(self): > ??????? self.unzip_files() > ??????? self.find_replace() > ??????? self.zip_files() > > ?? def unzip_files(self): > ??????? os.mkdir(self.temp_directory) > ??????? zip = zipfile.ZipFile(self.filename) > ??????? try: > ??????????? zip.extractall(self.temp_directory) > ??????? finally: > ??????????? zip.close() > > ??? def find_replace(self): > ??????? for filename in os.listdir(self.temp_directory): > ??????????? with open(self._full_filename(filename)) as file: > ??????????????? contents = file.read() > ??????????? contents = contents.replace( > ??????????????????? self.search_string, self.replace_string) > ??????????? with open(self._full_filename(filename), "w") as file: > ??????????????? file.write(contents) > > ?? def zip_files(self): > ??????? file = zipfile.ZipFile(self.filename, 'w') > ??????? for filename in os.listdir(self.temp_directory): > ??????????? file.write(self._full_filename(filename), filename) > ??????? shutil.rmtree(self.temp_directory) > > if __name__ == "__main__": > ??? ZipReplace(*sys.argv[1:4]).zip_find_replace() > > is a bit too advanced for me but i now see what it does.. although i wish it didnt return an error when run. > > so the arguments being passed are... > > [os, shutil, zipfile] or [filename, search_string, return_string] ? The arguments to ZipReplace should be [filename, search_string, return_string]. Imports are completely separate and unrelated to arguments. Importing a library means you can access that library from your script. If you are importing a library at the module level (i.e. not in a function or a class) means that everything in that module can access that library. You should not need to pass those modules (or packages) to anything inside the script. Typically even if you need it in another script you just import it there rather than passing it. I am sure there are valid reasons for passing an import to another module, but I have not needed to ever do something like that. Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From daedae11 at 126.com Fri Oct 12 05:12:47 2012 From: daedae11 at 126.com (Dae James) Date: Fri, 12 Oct 2012 11:12:47 +0800 Subject: [Tutor] Question about language code Message-ID: <2012101211124693201210@126.com> Here is a example in "Python v2.7.2 document": >>> import locale >>> loc = locale.getlocale() # get current locale # use German locale; name might vary with platform >>> locale.setlocale(locale.LC_ALL, 'de_DE') However, the result of executing on my computer is: >>> locale.setlocale(locale.LC_ALL, 'de_DE') Traceback (most recent call last): File "", line 1, in File "C:\Python27\lib\locale.py", line 531, in setlocale return _setlocale(category, locale) locale.Error: unsupported locale setting Why is it unsuccessful? My os is Windows 7. My python version is 2.7.2. Dae James -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Fri Oct 12 06:37:31 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 12 Oct 2012 00:37:31 -0400 Subject: [Tutor] Question about language code In-Reply-To: <2012101211124693201210@126.com> References: <2012101211124693201210@126.com> Message-ID: On Thu, Oct 11, 2012 at 11:12 PM, Dae James wrote: > >>>> import locale >>>> loc = locale.getlocale() # get current locale > # use German locale; name might vary with platform >>>> locale.setlocale(locale.LC_ALL, 'de_DE') This depends on the C runtime. For Windows, see MSDN: setlocale http://msdn.microsoft.com/en-us/library/x99tb11d Language strings http://msdn.microsoft.com/en-us/library/39cwe7zf Country/Region strings http://msdn.microsoft.com/en-us/library/cdax410z Code pages http://msdn.microsoft.com/en-us/library/cc195051 For example (untested): >>> locale.setlocale(locale.LC_ALL, 'German_Germany.1252') >From the MSDN setlocale() docs: The "locale" argument can be formatted as follows: setlocale( LC_ALL, "" ); Sets the locale to the default, which is the user-default ANSI code page obtained from the operating system. setlocale( LC_ALL, ".OCP" ); Explicitly sets the locale to the current OEM code page obtained from the operating system. setlocale( LC_ALL, ".ACP" ); Sets the locale to the ANSI code page obtained from the operating system. setlocale( LC_ALL, "[lang_ctry]" ); Sets the locale to the language and country/region indicated, using the default code page obtained from the host operating system. setlocale( LC_ALL, "[lang_ctry.cp]" ); Sets the locale to the language, country/region, and code page indicated in the [lang_ctry.cp] string. You can use various combinations of language, country/region, and code page. For example: setlocale( LC_ALL, "French_Canada.1252" ); // Set code page to French Canada ANSI default setlocale( LC_ALL, "French_Canada.ACP" ); // Set code page to French Canada OEM default setlocale( LC_ALL, "French_Canada.OCP" ); From eryksun at gmail.com Fri Oct 12 07:38:01 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 12 Oct 2012 01:38:01 -0400 Subject: [Tutor] Question about language code In-Reply-To: References: <2012101211124693201210@126.com> Message-ID: On Fri, Oct 12, 2012 at 12:37 AM, eryksun wrote: > > For example (untested): > > >>> locale.setlocale(locale.LC_ALL, 'German_Germany.1252') I got around to testing the above, and it works. Also, the Python docs say "if [locale is] an iterable, it?s converted to a locale name using the locale aliasing engine." This doesn't work on Windows. It aliases a tuple of ('German_Germany', '1252') to 'de_DE.cp1252', which Windows doesn't recognize. From chigga101 at gmail.com Fri Oct 12 12:17:14 2012 From: chigga101 at gmail.com (Matthew Ngaha) Date: Fri, 12 Oct 2012 11:17:14 +0100 Subject: [Tutor] need an explanation In-Reply-To: <5B80DD153D7D744689F57F4FB69AF474166E923C@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF474166E923C@SCACMX008.exchad.jpmchase.net> Message-ID: Thanks for everyone that replied. I really gained a lot from all the input. Also thanks to Dave and Prasad for explaining why i had errors trying to run the program. I fully understand the code now and im able to run it without errors. -------------- next part -------------- An HTML attachment was scrubbed... URL: From bardockarngrim at hotmail.com Sat Oct 13 06:55:08 2012 From: bardockarngrim at hotmail.com (Brett Dailey) Date: Sat, 13 Oct 2012 00:55:08 -0400 Subject: [Tutor] Project help Message-ID: I'm working on a few projects and need some help. Here's what the first one needs to do: "Credits" 1. Create a list of strings. Make sure your program will work with any number of strings. 2. Make this text appear at the bottom of the screen and ?crawl? to the top. The crawl should be at a slow speed (~20 pixels/s) and should run at the same rate on any computer. 3. It should fade from black (near the bottom) to white, and then back to black (at the top). 4. The program should end when the user pressed escape or hits the ?Quit-box? Here's the second one: "Paint Program" 1. Display a canvas inset from the main screen. 2. Display a palette at the bottom. Allow the user to adjust the RGB current color. 3. Use the mouse scroll-wheel to adjust the brush size. 4. If the user clicks within the canvas area, draw with the current brush color/size 5. If the user presses ?s? save (just the) canvas to a file (?output.jpg?) 6. Bonus: include ?stamps? (sprites) that can be dragged onto the canvas. Any help would be greatly appreciated!! These have been giving me some trouble. Also, if you can show me some of the code that would be great! Thank you!! -------------- next part -------------- An HTML attachment was scrubbed... URL: From bardockarngrim at hotmail.com Sat Oct 13 06:57:34 2012 From: bardockarngrim at hotmail.com (Brett Dailey) Date: Sat, 13 Oct 2012 00:57:34 -0400 Subject: [Tutor] More help Message-ID: Here's another project I'm working on. 1. Create a random "signal" (600 random numbers in the range -50 to +50). Display it as shown below. 2. Create a filter variable (integer) which is allowed to go from 0 to infinity. Allow the user to change this with the up/down arrow keys. The user should have to release the key and press it again to go up / down one level. 3. Apply a "box filter" to the original signal. If filter is 0, the signal should be the same as the original If filter is 1, you should, for each signal element i (of the new signal), average the i-1, i, and i+1 elements of the original signal. If filter is 2, you should, for each signal element i (of the new signal), average the i-2, i-1, i, i+1, and i+2 elements of the original signal iv. ... Note: if the i-? or i+? element is "out of bounds", don't include it. For example, if filter is 1, and you are calculating the 0th element of the new signal, you should only average the 0th and 1th element of the original signal. Thank you ahead of time! -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeanpierreda at gmail.com Sat Oct 13 07:54:27 2012 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Sat, 13 Oct 2012 01:54:27 -0400 Subject: [Tutor] Project help In-Reply-To: References: Message-ID: Do your own homework. -- Devin On Sat, Oct 13, 2012 at 12:55 AM, Brett Dailey wrote: > I'm working on a few projects and need some help. Here's what the first one > needs to do: > > "Credits" > 1. Create a list of strings. Make sure your program will work with any > number of strings. > 2. Make this text appear at the bottom of the screen and ?crawl? to the top. > The crawl should be at a slow > speed (~20 pixels/s) and should run at the same rate on any computer. > 3. It should fade from black (near the bottom) to white, and then back to > black (at the top). > 4. The program should end when the user pressed escape or hits the > ?Quit-box? > > > Here's the second one: > > "Paint Program" > 1. Display a canvas inset from the main screen. > 2. Display a palette at the bottom. Allow the user to adjust the RGB current > color. > 3. Use the mouse scroll-wheel to adjust the brush size. > 4. If the user clicks within the canvas area, draw with the current brush > color/size > 5. If the user presses ?s? save (just the) canvas to a file (?output.jpg?) > 6. Bonus: include ?stamps? (sprites) that can be dragged onto the canvas. > > Any help would be greatly appreciated!! These have been giving me some > trouble. Also, if you can show me some of the code that would be great! > > Thank you!! From breamoreboy at yahoo.co.uk Sat Oct 13 10:04:53 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 13 Oct 2012 09:04:53 +0100 Subject: [Tutor] More help In-Reply-To: References: Message-ID: On 13/10/2012 05:57, Brett Dailey wrote: > Here's another project I'm working on. > > 1. Create a random "signal" (600 random numbers in the range -50 to +50). Display it as shown below. > 2. Create a filter variable (integer) which is allowed to go from 0 to infinity. Allow the user to change this > with the up/down arrow keys. The user should have to release the key and press it again to go up / > down one level. > 3. Apply a "box filter" to the original signal. > If filter is 0, the signal should be the same as the original > If filter is 1, you should, for each signal element i (of the new signal), average the i-1, i, and i+1 > elements of the original signal. > If filter is 2, you should, for each signal element i (of the new signal), average the i-2, i-1, i, i+1, > and i+2 elements of the original signal iv. ... > Note: if the i-? or i+? element is "out of bounds", don't include it. For example, if filter is 1, and you are calculating the 0th element of the new signal, you should only average the 0th and 1th element of the original signal. > > Thank you ahead of time! > I'm reminded of the film Coogan's Bluff. I have the Lee J Cobb role, you're Clint Eastwood. -- Cheers. Mark Lawrence. From steve at pearwood.info Sat Oct 13 10:08:19 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 13 Oct 2012 19:08:19 +1100 Subject: [Tutor] Project help In-Reply-To: References: Message-ID: <50792173.9010303@pearwood.info> On 13/10/12 15:55, Brett Dailey wrote: > I'm working on a few projects and need some help. [...] Hi Brett, and welcome! We like to see that people make an effort to solve their problems. This is a list for learning about Python the language, not a list for doing people's homework for them. Please ask *specific* questions. One question per email. Try to make them good questions rather than bad: the more specific you are, the more likely someone will know the answer and can tell you. Bad question: "Here are three different projects and thirty five different things that I need to do. I need help." (Answer: "I hope you find some.") Good question: "I have to set the colour of text to shades of grey, starting with black and fading to white. How do I do this?" Even better question: "I am using PyGame with Python 2.7 on Windows and I have a field with some text in it. I need to set the colour of each line to a different shade of gray, starting with black at the bottom of the field and fading to white at the top. Here is my third attempt to get it working, but the text still looks black to me. What am I doing wrong?" As a beginner, we're willing to cut you a lot of slack if you show that you've made an effort. If you don't, we're likely to just ignore you. Even if you have *no clue whatsoever* how to start on the question, try to ask specific questions that show you've at least tried to think about it: - how do I show text in a field? - how do I make the text scroll? - how do I change the colour of text? - how do I count the number of lines of text in the field? - I know there are 35 lines of text and the top line needs to be white and the bottom one needs to be black, but I have no clue how to make the intermediate lines fade from black to white. etc. Unfortunately, I know very little about graphics programming in Python, so I have very little clue how to do this either. But if you come back with some good questions, and preferably show us the code you've already tried, I'm sure somebody else can help you. Good luck! -- Steven From steve at pearwood.info Sat Oct 13 10:10:54 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 13 Oct 2012 19:10:54 +1100 Subject: [Tutor] Project help In-Reply-To: References: Message-ID: <5079220E.1090400@pearwood.info> On 13/10/12 16:54, Devin Jeanpierre wrote: > Do your own homework. Oooh, nasty. Next you'll be using ... sarcasm. Perhaps even ... irony. http://www.youtube.com/watch?v=Ygg2KlicnOQ http://www.youtube.com/watch?v=evj24bXakqg -- Steven From breamoreboy at yahoo.co.uk Sat Oct 13 10:11:21 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 13 Oct 2012 09:11:21 +0100 Subject: [Tutor] Project help In-Reply-To: References: Message-ID: On 13/10/2012 06:54, Devin Jeanpierre wrote: > Do your own homework. > > -- Devin > Manners if you don't mind. We don't want to drive newbies away from Python by being harsh towards them. So it's do your own homework, please :) -- Cheers. Mark Lawrence. From alan.gauld at btinternet.com Sat Oct 13 10:15:30 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 13 Oct 2012 09:15:30 +0100 Subject: [Tutor] Project help In-Reply-To: References: Message-ID: On 13/10/12 05:55, Brett Dailey wrote: > I'm working on a few projects and need some help. We don;t do homework but we can offer suggestions. But first we need more detail. What is your programming language/version/OS? What GUI framework are you using? What is your skill level? Can you use other languages? > "Credits" > 1. Create a list of strings. Make sure your program will work with any > number of strings. Can you do this bit? If not you are in deep trouble! > 2. Make this text appear at the bottom of the screen and ?crawl? to the > top. Can you create a GUI 'screen' - aka window? Can you make the text appear at the bottom? Can you make it appear at the middle and top? Can you make the text disappear? If so you have all the components you need to do the crawling bit > The crawl should be at a slow > speed (~20 pixels/s) and should run at the same rate on any computer. Can you program a delay in your GUI framework? > 3. It should fade from black (near the bottom) to white, and then back > to black (at the top). Can you change the colour of text in your GUI? > 4. The program should end when the user pressed escape or hits the > ?Quit-box? Can you program commands and bind them to keys/buttons? > Here's the second one: Lets deal with one thing at a time... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sat Oct 13 10:16:47 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 13 Oct 2012 09:16:47 +0100 Subject: [Tutor] More help In-Reply-To: References: Message-ID: On 13/10/12 05:57, Brett Dailey wrote: > Here's another project I'm working on. It's generally easier to stick to one problem at a time. Otherwise we all get confused! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From sntshkmr60 at gmail.com Sat Oct 13 19:03:56 2012 From: sntshkmr60 at gmail.com (Santosh Kumar) Date: Sat, 13 Oct 2012 22:33:56 +0530 Subject: [Tutor] How can I modify this simple script for argparse? Message-ID: Here is a sample script without argparse implementation: from sys import argv script, filename = argv foo = "This line was written by a Python script." with open(filename, 'a') as file: file.write(foo) I find argparse hard. Just give me a startup. How can I make a asgparse version? From kwpolska at gmail.com Sat Oct 13 19:13:22 2012 From: kwpolska at gmail.com (Kwpolska) Date: Sat, 13 Oct 2012 19:13:22 +0200 Subject: [Tutor] How can I modify this simple script for argparse? In-Reply-To: References: Message-ID: On Sat, Oct 13, 2012 at 7:03 PM, Santosh Kumar wrote: > Here is a sample script without argparse implementation: > > from sys import argv > script, filename = argv > > foo = "This line was written by a Python script." > > with open(filename, 'a') as file: > file.write(foo) > > > I find argparse hard. Just give me a startup. How can I make a asgparse version? > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor The documentation may be hard to understand, but the tutorial isn?t: Anyways, here you go: import argparse parser = argparse.ArgumentParser() parser.add_argument('filename', action='store') args = parser.parse_args() foo = "This line was written by a Python script." with open(args.filename, 'a') as file: file.write(foo) Note that this is the most basic version. Also, I had problems with running it as a standalone file, but it worked fine in the interactive interpreter. -- Kwpolska stop html mail | always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 From steve at pearwood.info Sat Oct 13 19:14:37 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 14 Oct 2012 04:14:37 +1100 Subject: [Tutor] How can I modify this simple script for argparse? In-Reply-To: References: Message-ID: <5079A17D.6070607@pearwood.info> On 14/10/12 04:03, Santosh Kumar wrote: > Here is a sample script without argparse implementation: > > from sys import argv > script, filename = argv > > foo = "This line was written by a Python script." > > with open(filename, 'a') as file: > file.write(foo) > > > I find argparse hard. Just give me a startup. How can I make a asgparse version? import argparse parser = argparse.ArgumentParser() parser.add_argument('filename') args = parser.parse_args() spam = "This line was written by a Python script." with open(args.filename, 'a') as file: file.write(spam) See also: http://docs.python.org/howto/argparse.html -- Steven From chigga101 at gmail.com Sat Oct 13 20:29:45 2012 From: chigga101 at gmail.com (Matthew Ngaha) Date: Sat, 13 Oct 2012 19:29:45 +0100 Subject: [Tutor] hasattr() Message-ID: im trying to understand this hasattr function. i am supposed to pass in an object and an attribute name into its parametres... so im trying to get it to return True. Here's a quick test class Test: def __init__(self): self.att = "testing" >>> e = Test() >>> hasattr(e, e.att) False >>> hasattr(e, "testing") False what can i do to make this True? my e object has an attribute att but it returns False. Any code showing it return True? the task i have been set is very difficult but i don't want to ask for help just yet, i think understanding how hasattr works might make everything else clear. -------------- next part -------------- An HTML attachment was scrubbed... URL: From thudfoo at gmail.com Sat Oct 13 20:47:37 2012 From: thudfoo at gmail.com (xDog Walker) Date: Sat, 13 Oct 2012 11:47:37 -0700 Subject: [Tutor] hasattr() In-Reply-To: References: Message-ID: <201210131147.37796.thudfoo@gmail.com> On Saturday 2012 October 13 11:29, Matthew Ngaha wrote: > >>> hasattr(e, e.att) > > False >>> hasattr(e, "att") True hasattr wants the second parameter to be a string. You gave it a string. The string you gave it was "Testing". -- Yonder nor sorghum stenches shut ladle gulls stopper torque wet strainers. From acolle00 at g.uafortsmith.edu Sat Oct 13 22:17:21 2012 From: acolle00 at g.uafortsmith.edu (Amanda Colley) Date: Sat, 13 Oct 2012 15:17:21 -0500 Subject: [Tutor] Problems ugh help please Message-ID: I am trying to add the total pounds that will be shipped. I keep getting an error of unknown format code 'f' for object of type 'str' when I try to float the pounds. and when I dont float it it gives me a total weight of when it exicutes. here is the code I have. def pounds(): h_book=weight('Hardback',2.1) p_book=weight('Paperback',1.3) print('Your total weight of book(s) at',pounds) return pounds def weight(desc,weight): print('How many',desc,'books do you want at',weight,'pounds do you want?') num=int(input()) pounds=float(num*weight) -- Amanda Colley -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Sat Oct 13 22:41:06 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 13 Oct 2012 21:41:06 +0100 Subject: [Tutor] Problems ugh help please In-Reply-To: References: Message-ID: On 13/10/2012 21:17, Amanda Colley wrote: > I am trying to add the total pounds that will be shipped. I keep getting an > error of unknown format code 'f' for object of type 'str' when I try to > float the pounds. and when I dont float it it gives me a total weight of > when it exicutes. here is the code > I have. > > def pounds(): > h_book=weight('Hardback',2.1) > p_book=weight('Paperback',1.3) > print('Your total weight of book(s) at',pounds) > return pounds > def weight(desc,weight): > print('How many',desc,'books do you want at',weight,'pounds do you > want?') > num=int(input()) > pounds=float(num*weight) > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > It helps if you show all of your code and the complete traceback. Having said that look carefully at your code. You've a function called pounds that's trying to print something called pounds and return pounds. The pounds function calls the weight function which sets something called pounds but does nothing with it. Within the pounds function you set h_book and p_book to None as this will be the default returned by weight as you haven't specified a return statement within weight. That doesn't matter as you don't use h_book and p_book anyway, at least not in the code that you've shown. Got it? I'll leave you to sort out your code. If you still have problems please come back and ask, my barks's much worse than my bite :) -- Cheers. Mark Lawrence. From alan.gauld at btinternet.com Sun Oct 14 01:10:54 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 14 Oct 2012 00:10:54 +0100 Subject: [Tutor] Problems ugh help please In-Reply-To: References: Message-ID: On 13/10/12 21:17, Amanda Colley wrote: > I am trying to add the total pounds that will be shipped. I keep getting > an error Show us the full error text do not summarize. We lose a lot of information that way and have to workj much harder to guess/figure out what is going wrong. > def pounds(): > h_book=weight('Hardback',2.1) > p_book=weight('Paperback',1.3) > print('Your total weight of book(s) at',pounds) > return pounds Your function is called pounds, and you are printing pounds so you are printing your function. You also return your function from your function. Also you assign he return value of weight to two variables which you don't use for anything. All of that is decidedly odd and almost certainly wrong. > def weight(desc,weight): > print('How many',desc,'books do you want at',weight,'pounds do you > want?') > num=int(input()) > pounds=float(num*weight) And here you have a function called weight that takes a parameter called weight. Then you print weight - which one should Python print? The function or the parameter? As a rule do not use function names as parameter or variable names. Python isn't very good at guessing games. Also you expect a return from weight in the pounds() function but you don't actually provide any return value in weight, so the default return value will be used which is: None. So before doing anything else think up some new names and try again and if you still have problems (and I suspect you will) come back to us (with the full error text!) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From crawlzone at gmail.com Sun Oct 14 01:44:30 2012 From: crawlzone at gmail.com (Ray Jones) Date: Sat, 13 Oct 2012 16:44:30 -0700 Subject: [Tutor] urllib2.urlopen() Message-ID: <5079FCDE.7030303@gmail.com> I am attempting to capture url headers and have my script make decisions based on the content of those headers. Here is what I am using in the relative portion of my script: try: urllib2.urlopen('http://myurl.org') except urllib2.HTTPError, e: In the case of authentication error, I can print e.info() and get all the relevant header information. But I don't want to print. I want the information from the instance available to use in my script. How do I accomplish that? Ray From brian.van.den.broek at gmail.com Sun Oct 14 02:09:08 2012 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Sat, 13 Oct 2012 20:09:08 -0400 Subject: [Tutor] urllib2.urlopen() In-Reply-To: <5079FCDE.7030303@gmail.com> References: <5079FCDE.7030303@gmail.com> Message-ID: On 13 October 2012 19:44, Ray Jones wrote: > I am attempting to capture url headers and have my script make decisions > based on the content of those headers. > > Here is what I am using in the relative portion of my script: > > try: > urllib2.urlopen('http://myurl.org') > except urllib2.HTTPError, e: > > In the case of authentication error, I can print e.info() and get all > the relevant header information. But I don't want to print. I want the > information from the instance available to use in my script. How do I > accomplish that? > > > Ray Hi Ray, (Sorry for the double message, Ray. I forgot to Reply to all.) I'm not very familiar with using urllib2 and I've never used it with a page which requires authentication. So, this might not sort you. But, perhaps it will get you started on how to figure it out for yourself: >>> import urllib2 >>> E = None >>> try: urllib2.urlopen('http://fdghgdshdghmyurl.org') except urllib2.URLError, e: print 42 E = e 42 >>> print dir(E) ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__getslice__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__unicode__', '__weakref__', 'args', 'errno', 'filename', 'message', 'reason', 'strerror'] >>> Best, Brian vdB From crawlzone at gmail.com Sun Oct 14 03:45:14 2012 From: crawlzone at gmail.com (Ray Jones) Date: Sat, 13 Oct 2012 18:45:14 -0700 Subject: [Tutor] urllib2.urlopen() In-Reply-To: References: <5079FCDE.7030303@gmail.com> Message-ID: <507A192A.2050405@gmail.com> On 10/13/2012 05:09 PM, Brian van den Broek wrote: > On 13 October 2012 19:44, Ray Jones wrote: >> I am attempting to capture url headers and have my script make decisions >> based on the content of those headers. >> >> Here is what I am using in the relative portion of my script: >> >> try: >> urllib2.urlopen('http://myurl.org') >> except urllib2.HTTPError, e: >> >> In the case of authentication error, I can print e.info() and get all >> the relevant header information. But I don't want to print. I want the >> information from the instance available to use in my script. How do I >> accomplish that? >> >> >> Ray > ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', > '__getattribute__', '__getitem__', '__getslice__', '__hash__', > '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', > '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', > '__subclasshook__', '__unicode__', '__weakref__', 'args', 'errno', > 'filename', 'message', 'reason', 'strerror'] Thanks for the response. I experimented some, but I am not even sure what kinds of things to try. I mostly tried things like E.__getattribute__() or print E.strerror, but nothing seemed to give me what I was looking for. Ray From steve at pearwood.info Sun Oct 14 04:30:39 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 14 Oct 2012 13:30:39 +1100 Subject: [Tutor] hasattr() In-Reply-To: References: Message-ID: <507A23CF.1060408@pearwood.info> On 14/10/12 05:29, Matthew Ngaha wrote: > im trying to understand this hasattr function. i am supposed to pass in an > object and an attribute name into its parametres... so im trying to get it > to return True. Here's a quick test > > class Test: > def __init__(self): > self.att = "testing" Okay, so you have a class that defines an attribute named "att". >>>> e = Test() >>>> hasattr(e, e.att) > False Break this down step by step. First you create an instance, "e", with an attribute e.att. Python evaluates e.att to get the string "testing", then looks up hasattr(e, "testing"). Does e have an attribute called "testing"? No. So it returns False. >>>> hasattr(e, "testing") > False Same here, except that you bypass the evaluation of e.att and enter the string "testing" manually. What you need to pass to hasattr (as well as its friends getattr and setattr) is the *name* of the attribute, not its contents: hasattr(e, "att") will return True. -- Steven From steve at pearwood.info Sun Oct 14 04:50:54 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 14 Oct 2012 13:50:54 +1100 Subject: [Tutor] urllib2.urlopen() In-Reply-To: <507A192A.2050405@gmail.com> References: <5079FCDE.7030303@gmail.com> <507A192A.2050405@gmail.com> Message-ID: <507A288E.5020006@pearwood.info> On 14/10/12 12:45, Ray Jones wrote: > On 10/13/2012 05:09 PM, Brian van den Broek wrote: >> On 13 October 2012 19:44, Ray Jones wrote: >>> I am attempting to capture url headers and have my script make decisions >>> based on the content of those headers. >>> >>> Here is what I am using in the relative portion of my script: >>> >>> try: >>> urllib2.urlopen('http://myurl.org') >>> except urllib2.HTTPError, e: Well, in this case, for that URL, the connection succeeds without authentication. It might help if you test with a URL that actually fails :) >>> In the case of authentication error, I can print e.info() and get all >>> the relevant header information. But I don't want to print. Then don't. If you can do `print e.info()`, then you can also do `info = e.info()` and inspect the info programmatically. [...] > Thanks for the response. I experimented some, but I am not even sure > what kinds of things to try. I mostly tried things like > E.__getattribute__() or print E.strerror, but nothing seemed to give me > what I was looking for. Normally you would look up the documentation for HTTPError and see what attributes it is documented to have: http://docs.python.org/library/urllib2.html#urllib2.HTTPError but unfortunately the docs are rather sparse. In this case, I strongly recommend the "urllib2 missing manual": http://www.voidspace.org.uk/python/articles/urllib2.shtml -- Steven From steve at pearwood.info Sun Oct 14 05:02:41 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 14 Oct 2012 14:02:41 +1100 Subject: [Tutor] Problems ugh help please In-Reply-To: References: Message-ID: <507A2B51.4020006@pearwood.info> On 14/10/12 07:17, Amanda Colley wrote: > I am trying to add the total pounds that will be shipped. I keep getting an > error of unknown format code 'f' for object of type 'str' when I try to > float the pounds. My guess is that here you are doing something like: "weight is {:f}".format(weight) but weight is a string, not a number. It's only a guess because you don't show us the actual error you get, or the actual code you run. Luckily I love guessing games! Actually, I don't, I hate them. Please, we're volunteering our time to help you for free. Make it easy for us to help you: always copy and paste the complete traceback, starting with the line Traceback (most recent call last): all the way to the end. >and when I dont float it it gives me a total weight of > when it exicutes. That tells me that instead of *calling* the function "pounds", you are just giving it by name. weight = pounds # makes "weight" another name for the function "pounds" What you need is to call the function: weight = pounds() Note the round brackets (parentheses for Americans), which tells Python to call the function. -- Steven From crawlzone at gmail.com Sun Oct 14 08:15:33 2012 From: crawlzone at gmail.com (Ray Jones) Date: Sat, 13 Oct 2012 23:15:33 -0700 Subject: [Tutor] urllib2.urlopen() In-Reply-To: <507A288E.5020006@pearwood.info> References: <5079FCDE.7030303@gmail.com> <507A192A.2050405@gmail.com> <507A288E.5020006@pearwood.info> Message-ID: <507A5885.4070005@gmail.com> On 10/13/2012 07:50 PM, Steven D'Aprano wrote: > On 14/10/12 12:45, Ray Jones wrote: >> On 10/13/2012 05:09 PM, Brian van den Broek wrote: >>> On 13 October 2012 19:44, Ray Jones wrote: >>>> I am attempting to capture url headers and have my script make >>>> decisions >>>> based on the content of those headers. >>>> >>>> Here is what I am using in the relative portion of my script: >>>> >>>> try: >>>> urllib2.urlopen('http://myurl.org') >>>> except urllib2.HTTPError, e: > > Well, in this case, for that URL, the connection succeeds without > authentication. It might help if you test with a URL that actually > fails :) Ya think? ;)) > >>>> In the case of authentication error, I can print e.info() and get all >>>> the relevant header information. But I don't want to print. > > Then don't. > > If you can do `print e.info()`, then you can also do `info = e.info()` > and inspect the info programmatically. > One would expect that to be true. But when I do info = e.info(), info is . When I print e.info(), I get the following: Content-Type: text/html Connection: close WWW-Authenticate: Basic realm="xxxx" Content-Length: xx I can iterate through e.info() with a 'for' loop, but all I get as a result is: connection content-type www-authenticate content-length In other words, I get the headers but not the corresponding values. The same also happens if I iterate through e.headers. > but unfortunately the docs are rather sparse. In this case, I strongly > recommend the "urllib2 missing manual": > > http://www.voidspace.org.uk/python/articles/urllib2.shtml > I checked out this site, but it didn't have any further information than I had found on another site. Any further suggestions? Ray From brian.van.den.broek at gmail.com Sun Oct 14 08:55:55 2012 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Sun, 14 Oct 2012 02:55:55 -0400 Subject: [Tutor] urllib2.urlopen() In-Reply-To: <507A5885.4070005@gmail.com> References: <5079FCDE.7030303@gmail.com> <507A192A.2050405@gmail.com> <507A288E.5020006@pearwood.info> <507A5885.4070005@gmail.com> Message-ID: On 14 October 2012 02:15, Ray Jones wrote: > On 10/13/2012 07:50 PM, Steven D'Aprano wrote: >> If you can do `print e.info()`, then you can also do `info = e.info()` >> and inspect the info programmatically. >> > One would expect that to be true. But when I do info = e.info(), info is > . > > When I print e.info(), I get the following: > > Content-Type: text/html > Connection: close > WWW-Authenticate: Basic realm="xxxx" > Content-Length: xx > > I can iterate through e.info() with a 'for' loop, but all I get as a > result is: > > connection > content-type > www-authenticate > content-length > > In other words, I get the headers but not the corresponding values. Ray, That smells rather like you are dealing with a dictionary. What happens if you try e.info()["connection"] Best, Brian vdB From crawlzone at gmail.com Sun Oct 14 09:02:38 2012 From: crawlzone at gmail.com (Ray Jones) Date: Sun, 14 Oct 2012 00:02:38 -0700 Subject: [Tutor] urllib2.urlopen() In-Reply-To: References: <5079FCDE.7030303@gmail.com> <507A192A.2050405@gmail.com> <507A288E.5020006@pearwood.info> <507A5885.4070005@gmail.com> Message-ID: <507A638E.5010006@gmail.com> On 10/13/2012 11:55 PM, Brian van den Broek wrote: > On 14 October 2012 02:15, Ray Jones wrote: >> On 10/13/2012 07:50 PM, Steven D'Aprano wrote: > > >>> If you can do `print e.info()`, then you can also do `info = e.info()` >>> and inspect the info programmatically. >>> >> One would expect that to be true. But when I do info = e.info(), info is >> . >> >> When I print e.info(), I get the following: >> >> Content-Type: text/html >> Connection: close >> WWW-Authenticate: Basic realm="xxxx" >> Content-Length: xx >> >> I can iterate through e.info() with a 'for' loop, but all I get as a >> result is: >> >> connection >> content-type >> www-authenticate >> content-length >> >> In other words, I get the headers but not the corresponding values. > Ray, > > That smells rather like you are dealing with a dictionary. What > happens if you try > e.info()["connection"] Bingo! That's it! Thank-you very much - I will now attempt to implement it. Ray From eryksun at gmail.com Sun Oct 14 11:26:19 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 14 Oct 2012 05:26:19 -0400 Subject: [Tutor] urllib2.urlopen() In-Reply-To: <507A5885.4070005@gmail.com> References: <5079FCDE.7030303@gmail.com> <507A192A.2050405@gmail.com> <507A288E.5020006@pearwood.info> <507A5885.4070005@gmail.com> Message-ID: On Sun, Oct 14, 2012 at 2:15 AM, Ray Jones wrote: > > I can iterate through e.info() with a 'for' loop, but all I get as a > result is: > > connection > content-type > www-authenticate > content-length urllib2.HTTPError inherits from both urllib2.URLError and urllib.addinfourl (see help(e)). An instance of the latter is what urlopen() returns. It would be weird, but you could catch urllib.addinfourl as the exception type (please don't). HTTPError provides a file-like interface (read, readlines, etc), plus the status code, headers, and URL. The info() method returns the "headers" attribute, which is an instance of httplib.HTTPMessage (see below). HTTPError also stores this in the attibute "hdrs". So you have 3 ways to access the headers: e.info(), e.headers, and e.hdrs. (Actually, there are more ways since e.fp is the original addinfourl instance, and e.fp.fp._sock.msg is the original HTTPResponse msg.) Look at help(e.hdrs). HTTPMessage inherits a dict interface from rfc822.Message (__getitem__, __setitem__, __delitem__, __contains__, __len__, __iter__, get, setdefault, has_key, items, keys, values). It also has the method getheaders(name) that returns a list of values in case the header appears multiple times in the message. >>> e.hdrs['connection'] 'close' >>> e.hdrs.getheaders('connection') ['close'] From crawlzone at gmail.com Sun Oct 14 11:44:33 2012 From: crawlzone at gmail.com (Ray Jones) Date: Sun, 14 Oct 2012 02:44:33 -0700 Subject: [Tutor] urllib2.urlopen() In-Reply-To: References: <5079FCDE.7030303@gmail.com> <507A192A.2050405@gmail.com> <507A288E.5020006@pearwood.info> <507A5885.4070005@gmail.com> Message-ID: <507A8981.4060907@gmail.com> On 10/14/2012 02:26 AM, eryksun wrote: > e.hdrs['connection'] 'close' > e.hdrs.getheaders('connection') ['close'] I have often used help() to find my way around imported libraries. I didn't realize it would also help with instances. That's good to know. Ray From chigga101 at gmail.com Sun Oct 14 13:01:42 2012 From: chigga101 at gmail.com (Matthew Ngaha) Date: Sun, 14 Oct 2012 12:01:42 +0100 Subject: [Tutor] hasattr() In-Reply-To: <507A23CF.1060408@pearwood.info> References: <507A23CF.1060408@pearwood.info> Message-ID: Thank you xDog and Steven. The whole assignment makes a lot of sense now after your explanations of what hasattr is doing. Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Sun Oct 14 13:48:01 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 14 Oct 2012 07:48:01 -0400 Subject: [Tutor] urllib2.urlopen() In-Reply-To: <507A8981.4060907@gmail.com> References: <5079FCDE.7030303@gmail.com> <507A192A.2050405@gmail.com> <507A288E.5020006@pearwood.info> <507A5885.4070005@gmail.com> <507A8981.4060907@gmail.com> Message-ID: On Sun, Oct 14, 2012 at 5:44 AM, Ray Jones wrote: > > I have often used help() to find my way around imported libraries. I > didn't realize it would also help with instances. That's good to know. help(e) shows info for the instance's class, such as methods and data descriptors (i.e. properties, slots) broken down by inheritance. Use dir(e) to list all attributes of an instance, or list(vars(e)) if it has a __dict__. This is helpful with an instance of HTTPError because the file-like methods are set as instance data by urllib.addbase. You wouldn't know about them based on just the help command (but the docs do say it has a file-like interface). >>> list(vars(e)) ['fp', 'fileno', 'code', 'hdrs', 'read', 'readlines', 'next', 'headers', '__iter__', 'url', 'msg', 'readline'] For example, e.read() is a method of a socket._fileobject: >>> e.read.__self__ This _fileobject is also at e.fp.fp. It wraps the socket interface of the HTTPResponse, which in turn has the header data in its msg attribute: >>> e.fp.fp >>> e.fp.fp._sock >>> e.fp.fp._sock.msg >>> e.hdrs From abasiemeka at gmail.com Sun Oct 14 14:34:26 2012 From: abasiemeka at gmail.com (Osemeka Osuagwu) Date: Sun, 14 Oct 2012 13:34:26 +0100 Subject: [Tutor] instance method call issue Message-ID: Hello people, Firstly, thank you so much for all the assistance you provide so selflessly through this medium. I come from a functional programming background and have only recently become serious with OOP. For practice, I tried to implement John Conways 'Game of Life' without a GUI and ran into some issues. I understand instance, static and class methods but I'm still struggling with when and where they should be used. In the code below, I can't seem to get around calling an instance method (__reality_check()) from within a class method (update_grid()), I'm not even too sure that update_grid() should have been a class method in the first place. Please, I need help with this and any other advice on my coding style, where I could've been more efficient etc. class Grid: '''Grid(rows = 26, columns = 26) -> Provides a world object for the cells in Game of Life''' from time import sleep from os import system from sys import platform #class data: __rows, __array, os_type = [''], [], platform @staticmethod def display_grid(generation, os_type): try: (lambda x:Grid.system('cls') if 'win' in os_type else Grid.system('clear'))(1) except: print 'Cannot display Grid' print 'Now in Generation %d' %(generation) for line in Grid.__array: print line return @staticmethod def extend_grid(thickness = 4, force_extend = False): '''extend_grid([thickness][, force_extend]) --> Extends the edges of the grid by 4 units in all directions if the boundary of the simulation comes close to the edge. Extends anyway if force_extend option is True''' pass return @classmethod def update_grid(cls, generations, speed): '''Runs the 'Life' World simulation for the specified number of generations at the given speed''' #control = input #while for times in range(generations + 1): cls.extend_grid() cls.__reality_check(Grid, 'all') cls.display_grid(times, cls.os_type) cls.sleep(speed) return #------------------------------------------------------------------------------ def __init__(self, rows = 26, col = 26): '''Create a 'rows' x 'col' grid''' Grid.__rows *= rows Grid.__array = [Grid.__rows] * col print 'A %d x %d cell World has been Created' %(rows, col) def __reality_check(self, num = 'all'): '''__reality_check([num]) -->Checks and updates the state of the cell based on the number of 'alive' neighbors''' #check if reality check is for single cell or entire array if num == 'all': for i in range(len(Grid.__array)): for j in range(len(Grid.__array[i])): self.__reality_check((i,j)) return #Neighbor count check and update for single cell(num = row, col) elif num == tuple and len(num) == 2: col, row, neighbor_count = num[1], num[0], 0 for x in range(row-1, row+2): for y in range(col-1, col+2): #count only 'alive' neighbors (includes the subject cell itself) neighbor_count = lambda x: x+1 if Grid.__array[x][y] == '##' else x #correct the neighbor count value if subject cell was counted neighbor_count = lambda x: x-1 if Grid.__array[row][col] == '##' else x #update cell(x,y) state Grid.__array[row][col] = lambda x:'##' if neighbor_count == 3 else '' else: print 'Wrong argument for reality check' return def edit_cell(self, cells, state = '##'): '''edit_cell(cells[, state]) --> Where cells is a list of tuples containing coordinates of the cells to edit''' cells = cells for eachcell in cells: Grid.__array[eachcell[0]][eachcell[1]] = state return #To be moved to a different .py file after debugging. world = Grid() world.edit_cell([(10,10), (10, 11), (10, 12), (11, 10)], '##') world.update_grid(5, 0.5) p.s. I'm not yet completely through with coding some aspects like the extend_grid() method' Thank you Abasiemeka From steve at pearwood.info Sun Oct 14 15:33:41 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 15 Oct 2012 00:33:41 +1100 Subject: [Tutor] instance method call issue In-Reply-To: References: Message-ID: <507ABF35.5040109@pearwood.info> On 14/10/12 23:34, Osemeka Osuagwu wrote: > In the code below, I can't seem to get around calling an instance > method (__reality_check()) from within a class method (update_grid()), Of course you can't :) Since the class method has no access to the instance, it cannot call instance methods, since it doesn't know which instance to use! Instead, update_grid needs to be an instance method. Of course it does! It needs to update a specific grid, right? That will be the instance. > I'm not even too sure that update_grid() should have been a class > method in the first place. Probably not. Static and class methods are rarely used, since they have no access to the instance that owns them. Class methods are generally used for alternate constructor functions, e.g. dict.fromkeys. Since it makes no difference whether you call fromkeys from the class (dict) or from an instance, it is made a classmethod. Static methods, well, 99 times out of a hundred if you think you want to use a static method, you're probably better off making it a module level function. By my count, there are over 26000 instance methods in the Python standard library, 202 class methods, and only 84 static methods. So they are rare. I've been using Python for about 15 years, and I think I could count the number of times I've used staticmethod on the fingers of one hand. > Please, I need help with this and any other advice on my coding style, > where I could've been more efficient etc. > > > class Grid: > '''Grid(rows = 26, columns = 26) -> Provides a world object > for the cells in Game of Life''' > from time import sleep > from os import system > from sys import platform Move the imports outside of the class. They should go at the start of the module. As written, your class has attributes Grid.sleep, Grid.system, etc. Since these attributes have nothing to do with Grids, they don't belong in the Grid class. Move them out. Classes are not catch-all collections of unrelated methods. That's what modules are for :) > #class data: > __rows, __array, os_type = [''], [], platform A word of advise: don't waste your time with double underscore "private" attributes. They're no more private than single underscore (private by convention only), and the name mangling can cause difficulty when debugging. There are good reasons for using double underscores like this, but you should consider them a bit more advanced. > @staticmethod > def display_grid(generation, os_type): > try: > (lambda x:Grid.system('cls') if 'win' in > os_type else Grid.system('clear'))(1) That's wrong! Your display_grid method uses the class (Grid), so it shouldn't be a static method. Move the imports to the module level, and display_grid as a regular function: def display_grid(generation, array): clear_screen() print 'Now in Generation %d' % generation for line in array: print line def clear_screen(platform=sys.platform()): # The default here is set once, and defaults to your platform. if 'win' in platform: command = 'cls' else: command = 'clear' os.system(command) Or leave display_grid as an instance method: class Grid: def display_grid(self, generation): clear_screen() print 'Now in Generation %d' % generation for line in self._array: print line Either way, clear_screen has nothing to do with grids, so it should not be a method of Grid. > except: > print 'Cannot display Grid' Never use a bare except like that. Bare except should *only* be used at the interactive interpreter, for lazy people who want to avoid typing. It should never be used non-interactively, it's simply poor practice. The problem with bare excepts is that they cover up too much. Your aim is to write code that works. If it has a bug, you need to *see* the bug so you can identify it and then fix it. Bare excepts make bugs invisible. > @staticmethod > def extend_grid(thickness = 4, force_extend = False): > '''extend_grid([thickness][, force_extend]) --> > Extends the edges of the grid by 4 units in > all directions if the boundary of the simulation comes close to the > edge. Extends anyway if force_extend option is True''' Why is this a static method? Which grid is this operating on? It should either take a grid as an argument, or extend_grid should be an instance method that operates on itself: # either this top-level function def extend_grid(grid, thickness=4, force=False): # do stuff to the grid to extend it # and then return it return grid # or make this a regular instance method class Grid: def extend_grid(self, thickness=4, force=False): # do stuff to self to extend it # no need to return anything > @classmethod > def update_grid(cls, generations, speed): Why is this a class method? > '''Runs the 'Life' World simulation for the specified > number of generations at the given speed''' > #control = input > #while > for times in range(generations + 1): This contains an off-by-one bug. If you pass generations=100 as argument, it will run 101 times instead. Instead, if you want your generations to count from 1: for times in range(1, generations + 1): ... > cls.extend_grid() > cls.__reality_check(Grid, 'all') Having made this a class method, why have you hard-coded a reference to the Grid class in it's body? > cls.sleep(speed) > return There's very rarely any need for a bare return like that. Just leave it out, Python automatically returns when it drops out the bottom of the function. [...] > def __reality_check(self, num = 'all'): [...] > print 'Wrong argument for reality check' Have you learned about raising exceptions yet? I see above you use a try...except loop, so you should know about them. Don't just print a message when your method receives an invalid argument, raise an exception: raise ValueError("expected num='all' or an integer, got %r instead" % num) > def edit_cell(self, cells, state = '##'): > '''edit_cell(cells[, state]) --> Where cells is a list > of tuples containing coordinates of the cells to edit''' > cells = cells > for eachcell in cells: > Grid.__array[eachcell[0]][eachcell[1]] = state > return This is an abuse of the class. You have instances, but they all share state. Why? Sharing state is okay if you have a reason for wanting to share state, but here there doesn't appear to be any reason for it. As you have written this, you cannot have two Grids. Actually you can, but since they both share the same state, you cannot have two DIFFERENT Grids. My advice is, forget about class and static methods completely until you are a bit more experienced with object-oriented design. -- Steven From alan.gauld at btinternet.com Sun Oct 14 17:21:38 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 14 Oct 2012 16:21:38 +0100 Subject: [Tutor] instance method call issue In-Reply-To: References: Message-ID: On 14/10/12 13:34, Osemeka Osuagwu wrote: > I understand instance, static and class methods but I'm still > struggling with when and where they should be used. You can pretty much ignore static methods, they were almost an historic 'accident' superseded, in most cases, by class methods. The number of cases where a staticmethod is really needed is very low. class methods you will rarely need but they are used when you want to do something to the entire class of objects. i.e. something that affects all instances both existing and future. Or they could be used where there are no instances (yet). Factory methods, instance selection methods, modifying shared attributes, instance pooling, are all examples. To give some kind of heuristic based on my personal usage, I use class methods in almost all my bigger projects. Such a project might have 20 or 30 (or more) classes. Out of that maybe 2 or 3 will have any class methods. Instance methods are by far the most common. These are the things you want to do to a specific instance of the class. Changes to one instance have no effect on other instances of the same class. > In the code below, I can't seem to get around calling an instance > method (__reality_check()) from within a class method (update_grid()), It's not a class method, you want to update a specific instance of a grid. update_grids() might be a class method if you wanted to, say, change the maximum buffer size used to store the grids, or cause all grids to display the word 'empty' instead of a blank. Of course to do that you would need access to all the instances to update their existing values so the constructor would need to store a reference to itself in the class somewhere (or maybe in a separate shared database). The class method would then iterate over all instances calling their instance method update_grid(). This is a common(*) class method pattern, where a plural class method calls the singular instance methods of all the instances. (*) I say common, but of course class methods are not common so its a very relative term! HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From norman at khine.net Sun Oct 14 20:05:05 2012 From: norman at khine.net (Norman Khine) Date: Sun, 14 Oct 2012 19:05:05 +0100 Subject: [Tutor] extract uri from beautiful soup string Message-ID: hello, i have this code: #!/usr/local/bin/python # -*- coding: utf-8 -*- import re import urllib2 import BeautifulSoup origin_site = 'http://DOMAIN.TLD/index.php?id=annuaire_assos&theme=0&rech=&num_page=' pages = range(1,3) for page_no in pages: print '====== %s' % page_no req = ('%s%s' % (origin_site, page_no)) user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' headers = { 'User-Agent' : user_agent } items = [] try: urllib2.urlopen(req) except urllib2.URLError, e: pass else: # do something with the page doc = urllib2.urlopen(req) soup = BeautifulSoup.BeautifulSoup(doc) infoblock = soup.findAll('tr', { "class" : "menu2" }) for item in infoblock: soup = BeautifulSoup.BeautifulSoup(str(item)) for tag in soup.recursiveChildGenerator(): if isinstance(tag,BeautifulSoup.Tag) and tag.name in ('td'): if tag.string is not None: assoc_name = (tag.string) if isinstance(tag,BeautifulSoup.Tag) and tag.name in ('u'): if tag.string is not None: assoc_theme = (tag.string) get_onclick = soup('a')[0]['onclick'] # get the 'onclick' attribute print assoc_name, get_onclick, assoc_theme this returns the following: Amiral window.open('http://DOMAIN.TLD/extranet/associations/detail-assos.php?id=3815','','toolbar=0,menubar=0,location=0,scrollbars=1,top=80,left=400,width=500,height=400');return false Culture how do i extract from the get_onclick the 'http://DOMAIN.TLD/extranet/associations/detail-assos.php?id=3815' correctly? Any advise much appreciated. -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From norman at khine.net Sun Oct 14 20:53:16 2012 From: norman at khine.net (Norman Khine) Date: Sun, 14 Oct 2012 19:53:16 +0100 Subject: [Tutor] extract uri from beautiful soup string In-Reply-To: References: Message-ID: ignore, i got it: get_url = re.compile(r"""window.open\('(.*)','','toolbar=0,""", re.DOTALL).findall ... get_onclick = str(soup('a')[0]['onclick']) # get the 'onclick' attribute urls = get_url(get_onclick) print assoc_name, urls, assoc_theme returns Amiral ['http://DOMAIN.TLD/extranet/associations/detail-assos.php?id=3815'] Culture On Sun, Oct 14, 2012 at 7:05 PM, Norman Khine wrote: > hello, i have this code: > > > #!/usr/local/bin/python > # -*- coding: utf-8 -*- > > import re > import urllib2 > import BeautifulSoup > > origin_site = 'http://DOMAIN.TLD/index.php?id=annuaire_assos&theme=0&rech=&num_page=' > > pages = range(1,3) > > for page_no in pages: > print '====== %s' % page_no > req = ('%s%s' % (origin_site, page_no)) > user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' > headers = { 'User-Agent' : user_agent } > items = [] > try: > urllib2.urlopen(req) > except urllib2.URLError, e: > pass > else: > # do something with the page > doc = urllib2.urlopen(req) > soup = BeautifulSoup.BeautifulSoup(doc) > infoblock = soup.findAll('tr', { "class" : "menu2" }) > for item in infoblock: > soup = BeautifulSoup.BeautifulSoup(str(item)) > for tag in soup.recursiveChildGenerator(): > if isinstance(tag,BeautifulSoup.Tag) and tag.name in ('td'): > if tag.string is not None: > assoc_name = (tag.string) > if isinstance(tag,BeautifulSoup.Tag) and tag.name in ('u'): > if tag.string is not None: > assoc_theme = (tag.string) > > get_onclick = soup('a')[0]['onclick'] # get the 'onclick' attribute > print assoc_name, get_onclick, assoc_theme > > > this returns the following: > > Amiral window.open('http://DOMAIN.TLD/extranet/associations/detail-assos.php?id=3815','','toolbar=0,menubar=0,location=0,scrollbars=1,top=80,left=400,width=500,height=400');return > false Culture > > how do i extract from the get_onclick the > 'http://DOMAIN.TLD/extranet/associations/detail-assos.php?id=3815' > correctly? > > Any advise much appreciated. > > > > -- > %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or > chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From steve at pearwood.info Sun Oct 14 21:42:09 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 15 Oct 2012 06:42:09 +1100 Subject: [Tutor] extract uri from beautiful soup string In-Reply-To: References: Message-ID: <507B1591.7040307@pearwood.info> On 15/10/12 05:05, Norman Khine wrote: > for page_no in pages: [...] > try: > urllib2.urlopen(req) > except urllib2.URLError, e: > pass > else: > # do something with the page > doc = urllib2.urlopen(req) This is a bug. Just because urlopen(req) succeeded a moment ago, doesn't mean that it will necessarily succeed now. This is an example of the general class of bug known as a "race condition": http://en.wikipedia.org/wiki/Race_condition Better to write this as: try: doc = urllib2.urlopen(req) except urllib2.URLError, e: pass else: # do something with the page [rest of code goes here] -- Steven From bgailer at gmail.com Sun Oct 14 23:37:19 2012 From: bgailer at gmail.com (bob gailer) Date: Sun, 14 Oct 2012 17:37:19 -0400 Subject: [Tutor] instance method call issue In-Reply-To: References: Message-ID: <507B308F.2010101@gmail.com> On 10/14/2012 8:34 AM, Osemeka Osuagwu wrote: > except: > print 'Cannot display Grid' In addition to Steve's comment I add my own gripe: When delivering an error message tell us WHY. I have seen too many messages "cannot do xxx" with no information that might help me debug. "cannot load file xxx" "Windows cannot load the installer for xxx" or totally confusing messages "error 6 trying to report error 6" "cannot do xxx to file yyy. file exists" In reality the underlying program knows a lot more about why. Could not the developers have presented that information as well? -- Bob Gailer 919-636-4239 Chapel Hill NC From norman at khine.net Mon Oct 15 00:10:12 2012 From: norman at khine.net (Norman Khine) Date: Sun, 14 Oct 2012 23:10:12 +0100 Subject: [Tutor] extract uri from beautiful soup string In-Reply-To: <507B1591.7040307@pearwood.info> References: <507B1591.7040307@pearwood.info> Message-ID: Hi thanks, i changed the code to http://pastie.org/5059153 One thing is that when I try to write the assoc_data into a CSV file, it groaks on UnicodeEncodeError: 'ascii' codec can't encode character u'\xc7' in position 0: here some sample data from the print: [u'Social', u'Action9', u'ash-nimes at aol.com', mise en place d'ateliers, d'animations hebdomadaires et ponctuelles afin de lutter contre toutes les formes d'exclusion., Mme Libert? Bisbal, 04.66.27.24.84, 3002 Rte de Courbessac, 04.66.27.24.84, 30000 NIMES, Madame BISBAL Libert?, 04.66.27.24.84, ] [u'Social', u'Adapei30', u'contact at adapi30.org', deux lieux d'echanges et d'infos des publics concernes par le probleme du handicap mental representation aupres de divers organismes d'etat et du departement., 17b, RUE CHILDEBERT, 04.66.21.21.49, 30900 NIMES, Monsieur FLUTTE Bernard, ] [u'Sport', u'Aero-club de nimes-courbessac', u'aeroclubnimes at free.fr', promouvoir , de faciliter et d'organiser la pratique de l'aviation, 65 Aerodrome de Nimes Courbessac, 04.66.28.16.00, 30000 NIMES, Monsieur VASSAL PATRICK, ] How do I change to code to take note of the latin-1 encoding? On Sun, Oct 14, 2012 at 8:42 PM, Steven D'Aprano wrote: > On 15/10/12 05:05, Norman Khine wrote: > >> for page_no in pages: > > [...] > >> try: >> urllib2.urlopen(req) >> except urllib2.URLError, e: >> pass >> else: >> # do something with the page >> doc = urllib2.urlopen(req) > > > This is a bug. Just because urlopen(req) succeeded a moment ago, doesn't > mean > that it will necessarily succeed now. > > This is an example of the general class of bug known as a "race condition": > > http://en.wikipedia.org/wiki/Race_condition > > > Better to write this as: > > > try: > doc = urllib2.urlopen(req) > > except urllib2.URLError, e: > pass > else: > # do something with the page > [rest of code goes here] > > > > > -- > Steven > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From sander.sweers at gmail.com Mon Oct 15 01:12:17 2012 From: sander.sweers at gmail.com (Sander Sweers) Date: Mon, 15 Oct 2012 01:12:17 +0200 Subject: [Tutor] extract uri from beautiful soup string In-Reply-To: References: <507B1591.7040307@pearwood.info> Message-ID: <1350256337.4004.7.camel@infirit.lan> Norman Khine schreef op zo 14-10-2012 om 23:10 [+0100]: > One thing is that when I try to write the assoc_data into a CSV file, > it groaks on > > UnicodeEncodeError: 'ascii' codec can't encode character u'\xc7' in position 0: It looks like python is doing an implicit decode/encode on one of your strings. It may be caused in codecs.open. You will have to hunt down where this implicit decode/encode is done, see http://nedbatchelder.com/text/unipain.html for more info. > here some sample data from the print: These strings don't cause any errors for me and fit in ascii. Add some print statements before your write the string to find which string is causing you grief. Greets Sander > [u'Social', u'Action9', u'ash-nimes at aol.com', > mise en place d'ateliers, d'animations hebdomadaires et ponctuelles > afin de lutter contre toutes les formes d'exclusion., Mme Libert? > Bisbal, 04.66.27.24.84, 3002 Rte de Courbessac, 04.66.27.24.84, 30000 > NIMES, Madame BISBAL Libert?, 04.66.27.24.84, ] > [u'Social', u'Adapei30', u'contact at adapi30.org', deux lieux d'echanges > et d'infos des publics concernes par le probleme du handicap mental > representation aupres de divers organismes d'etat et du departement., > 17b, RUE CHILDEBERT, 04.66.21.21.49, 30900 NIMES, Monsieur FLUTTE > Bernard, ] > [u'Sport', u'Aero-club de nimes-courbessac', u'aeroclubnimes at free.fr', > promouvoir , de faciliter et d'organiser la pratique de l'aviation, 65 > Aerodrome de Nimes Courbessac, 04.66.28.16.00, 30000 NIMES, Monsieur > VASSAL PATRICK, ] > > How do I change to code to take note of the latin-1 encoding? From norman at khine.net Mon Oct 15 01:17:42 2012 From: norman at khine.net (Norman Khine) Date: Mon, 15 Oct 2012 00:17:42 +0100 Subject: [Tutor] extract uri from beautiful soup string In-Reply-To: <1350256337.4004.7.camel@infirit.lan> References: <507B1591.7040307@pearwood.info> <1350256337.4004.7.camel@infirit.lan> Message-ID: i tried this: http://pastie.org/5059153 but now i get a Traceback (most recent call last): File "nimes_extract.py", line 75, in c.writerow([item.encode("UTF-8")]) TypeError: 'NoneType' object is not callable On Mon, Oct 15, 2012 at 12:12 AM, Sander Sweers wrote: > Norman Khine schreef op zo 14-10-2012 om 23:10 [+0100]: >> One thing is that when I try to write the assoc_data into a CSV file, >> it groaks on >> >> UnicodeEncodeError: 'ascii' codec can't encode character u'\xc7' in position 0: > > It looks like python is doing an implicit decode/encode on one of your > strings. It may be caused in codecs.open. You will have to hunt down > where this implicit decode/encode is done, see > http://nedbatchelder.com/text/unipain.html for more info. > >> here some sample data from the print: > > These strings don't cause any errors for me and fit in ascii. Add some > print statements before your write the string to find which string is > causing you grief. > > Greets > Sander > >> [u'Social', u'Action9', u'ash-nimes at aol.com', >> mise en place d'ateliers, d'animations hebdomadaires et ponctuelles >> afin de lutter contre toutes les formes d'exclusion., Mme Libert? >> Bisbal, 04.66.27.24.84, 3002 Rte de Courbessac, 04.66.27.24.84, 30000 >> NIMES, Madame BISBAL Libert?, 04.66.27.24.84, ] >> [u'Social', u'Adapei30', u'contact at adapi30.org', deux lieux d'echanges >> et d'infos des publics concernes par le probleme du handicap mental >> representation aupres de divers organismes d'etat et du departement., >> 17b, RUE CHILDEBERT, 04.66.21.21.49, 30900 NIMES, Monsieur FLUTTE >> Bernard, ] >> [u'Sport', u'Aero-club de nimes-courbessac', u'aeroclubnimes at free.fr', >> promouvoir , de faciliter et d'organiser la pratique de l'aviation, 65 >> Aerodrome de Nimes Courbessac, 04.66.28.16.00, 30000 NIMES, Monsieur >> VASSAL PATRICK, ] >> >> How do I change to code to take note of the latin-1 encoding? > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From sander.sweers at gmail.com Mon Oct 15 02:35:05 2012 From: sander.sweers at gmail.com (Sander Sweers) Date: Mon, 15 Oct 2012 02:35:05 +0200 Subject: [Tutor] extract uri from beautiful soup string In-Reply-To: References: <507B1591.7040307@pearwood.info> <1350256337.4004.7.camel@infirit.lan> Message-ID: <1350261305.4004.23.camel@infirit.lan> Please don't top post. > On Mon, Oct 15, 2012 at 12:12 AM, Sander Sweers wrote: > > Norman Khine schreef op zo 14-10-2012 om 23:10 [+0100]: > >> One thing is that when I try to write the assoc_data into a CSV file, > >> it groaks on > >> > >> UnicodeEncodeError: 'ascii' codec can't encode character u'\xc7' in position 0: > > > > It looks like python is doing an implicit decode/encode on one of your > > strings. It may be caused in codecs.open. You will have to hunt down > > where this implicit decode/encode is done, see > > http://nedbatchelder.com/text/unipain.html for more info. > > > >> here some sample data from the print: > > > > These strings don't cause any errors for me and fit in ascii. Add some > > print statements before your write the string to find which string is > > causing you grief. > Norman Khine schreef op ma 15-10-2012 om 00:17 [+0100]: > i tried this: http://pastie.org/5059153 > > but now i get a > > Traceback (most recent call last): > File "nimes_extract.py", line 75, in > c.writerow([item.encode("UTF-8")]) > TypeError: 'NoneType' object is not callable You have several str() likely to work around real bugs but now they are biting back. In your code I don;t see any use for it.. Example how str() is hiding bugs for you. >>> str(None).encode('UTF-8') 'None' >>> None.encode('UTF-8') Traceback (most recent call last): File "", line 1, in None.encode('UTF-8') AttributeError: 'NoneType' object has no attribute 'encode' Get rid of all the str() and make sure you have only unicode strings *everywhere* and start fixing all the bugs that got hidden because of it. Do make sure to watch the video as it explains the pain you are having with unicode. Greets Sander From sander.sweers at gmail.com Mon Oct 15 03:02:15 2012 From: sander.sweers at gmail.com (Sander Sweers) Date: Mon, 15 Oct 2012 03:02:15 +0200 Subject: [Tutor] extract uri from beautiful soup string In-Reply-To: <1350261305.4004.23.camel@infirit.lan> References: <507B1591.7040307@pearwood.info> <1350256337.4004.7.camel@infirit.lan> <1350261305.4004.23.camel@infirit.lan> Message-ID: <1350262935.4004.31.camel@infirit.lan> Sander Sweers schreef op ma 15-10-2012 om 02:35 [+0200]: > > On Mon, Oct 15, 2012 at 12:12 AM, Sander Sweers wrote: > > > Norman Khine schreef op zo 14-10-2012 om 23:10 [+0100]: > > Norman Khine schreef op ma 15-10-2012 om 00:17 [+0100]: > > i tried this: http://pastie.org/5059153 Btw, if I understand what you are trying to do then you can do this much more simple. I noticed that all the a tags with onclick have an href attribute of '#'. To get all of these do something like: soup.findAll('a', {'href':'#'}) Then use the attrmap eg attrMap['onclick'].split('\'')[1]. Put together that may look like the below. for i in soup.findAll('a', {'href':'#'}): if 'toolbar=0' in i.attrMap['onclick']: print i.attrMap['onclick'].split('\'')[1] Greets Sander From erwin.salinas at mavs.uta.edu Mon Oct 15 03:21:40 2012 From: erwin.salinas at mavs.uta.edu (Salinas, Erwin d) Date: Mon, 15 Oct 2012 01:21:40 +0000 Subject: [Tutor] PYTHON NEWBIE HELP! Message-ID: <1EF569B1486C5D4998A59060EF869779189F537F@CH1PRD0102MB149.prod.exchangelabs.com> HELP! Hi I'm a newbie in Python, I'm having trouble organizing the FILE into the OUTPUT BELOW. I put the file into read, then I put it on a list, and my plan is to put the name and the corresponding grades into a dictionary, using the elements on the list, but when I print the dictionary, it's not giving me the result I wanted. MY CODE SO FAR IS SHOWN BELOW THE OUTPUT. For more details of the assignment, go to this link http://ranger.uta.edu/~kamangar/Courses/CSE_1310_FA12/Homework5(DueDate.Oct.16,2012).html#Topic18 Thank you so much. FILE HM3, 80 , HM2 ,90 , ID, 1000123456,HM4,92,Last_name,Rubble ID, 1000100200,Last_name,Bunny,HM2,92,HM5,70,HM1,98, ID, 1000123300,HM1,86, HM3,100,Last_name,Duck HM4,96,Last_name,Chipmunk,HM1,86,ID,1000456456 Last_name,Simpson,HM3,70, HM1, 90 ,ID, 1000321321,Test1,90 HM1,87,Last_name,Duck,HM2, 100 ,HM4,94,ID, 1000123300 HM3,95,HM2,88,Last_name, Chipmunk ,HM1,86 , ID,1000999888 HM1, 92 ,Last_name,Simpson,ID, 1000321321,HM2,90 OUTPUT Name | ID | HM1 | HM2 | HM3 | HM4 | Avg. | ___________________________________________________________________ Bunny | 1000100200 | 98 | 92 | 0 | 0 | 47.50 | Chipmunk | 1000456456 | 86 | 0 | 0 | 96 | 45.50 | Chipmunk | 1000999888 | 86 | 88 | 95 | 0 | 67.25 | Duck | 1000123300 | 87 | 100 | 100 | 94 | 95.25 | Rubble | 1000123456 | 0 | 90 | 80 | 92 | 65.50 | Simpson | 1000321321 | 92 | 90 | 70 | 0 | 63.00 | THIS IS MY CODE SO FAR filename = open("student_grades_hm_5.txt") infile = filename.read() mix_list = infile.split() print "Name | ID | HM1 | HM2 | HM3 | HM4 | Avg. |" print "______________________________________________________" Student_one = {"Name":mix_list[9],"ID":mix_list[5],"HM1":0,"HM2":mix_list[3], "HM3":mix_list[1],"HM4":mix_list[7]} print Student_one -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Mon Oct 15 04:42:26 2012 From: bgailer at gmail.com (bob gailer) Date: Sun, 14 Oct 2012 22:42:26 -0400 Subject: [Tutor] PYTHON NEWBIE HELP! In-Reply-To: <1EF569B1486C5D4998A59060EF869779189F537F@CH1PRD0102MB149.prod.exchangelabs.com> References: <1EF569B1486C5D4998A59060EF869779189F537F@CH1PRD0102MB149.prod.exchangelabs.com> Message-ID: <507B7812.8000500@gmail.com> On 10/14/2012 9:21 PM, Salinas, Erwin d wrote: > HELP! Hi I'm a newbie in Python, welcome A few pointers regarding use of this email list: - we are a few volunteers who try to help as we can - please put your responses following the relevant text rather than at the top - please reply-all so a copy goes to the list. - please submit plain text rather than formatting or colors. - tell us what os and python version you are using - remove extraneous text - provide a meaningful subject - regarding homework > I'm having trouble organizing the FILE into the OUTPUT BELOW. I put > the file into read, then I put it on a list, and my plan is to put the > name and the corresponding grades into a dictionary, using the > elements on the list, but when I print the dictionary, it's not giving > me the result I wanted. MY CODE SO FAR IS SHOWN BELOW THE > OUTPUT. For more details of the assignment, go to this > link > http://ranger.uta.edu/~kamangar/Courses/CSE_1310_FA12/Homework5(DueDate.Oct.16,2012).html#Topic18. > Thank you so much. > > FILE > HM3, 80 , HM2 ,90 , ID, 1000123456,HM4,92,Last_name,Rubble > ID, 1000100200,Last_name,Bunny,HM2,92,HM5,70,HM1,98, > ID, 1000123300,HM1,86, HM3,100,Last_name,Duck > HM4,96,Last_name,Chipmunk,HM1,86,ID,1000456456 > Last_name,Simpson,HM3,70, HM1, 90 ,ID, 1000321321,Test1,90 > HM1,87,Last_name,Duck,HM2, 100 ,HM4,94,ID, 1000123300 > HM3,95,HM2,88,Last_name, Chipmunk ,HM1,86 , ID,1000999888 > HM1, 92 ,Last_name,Simpson,ID, 1000321321,HM2,90 > OUTPUT > Name | ID | HM1 | HM2 | HM3 | HM4 | Avg. | > ___________________________________________________________________ > Bunny | 1000100200 | 98 | 92 | 0 | 0 | 47.50 | > Chipmunk | 1000456456 | 86 | 0 | 0 | 96 | 45.50 | > Chipmunk | 1000999888 | 86 | 88 | 95 | 0 | 67.25 | > Duck | 1000123300 | 87 | 100 | 100 | 94 | 95.25 | > Rubble | 1000123456 | 0 | 90 | 80 | 92 | 65.50 | > Simpson | 1000321321 | 92 | 90 | 70 | 0 | 63.00 | > THIS IS MY CODE SO FAR > filename = open("student_grades_hm_5.txt") > infile = filename.read() > mix_list = infile.split() > print "Name | ID | HM1 | HM2 | HM3 | HM4 | Avg. |" > print "______________________________________________________" > > Student_one = > {"Name":mix_list[9],"ID":mix_list[5],"HM1":0,"HM2":mix_list[3], > "HM3":mix_list[1],"HM4":mix_list[7]} > print Student_one You seem to be missing a number of basic concepts. It is hard to tell how much help is allowed by your instructor. Did you successfully complete the preceding assignments? Since the "purpose" is to use lists, file i/o, loops and formatted print I assume you already know how to use split and dictionaries. True? What elements of the "purpose" are giving you trouble? Since I see no loop or formatted print I assume these are giving you grief. I suggest you start with a bit of pseudo-code to express the structure of the program. Your program should not refer to the keys explicitly as you have done in the line starting with Student_one = nor should it refer to the list elements by number (that implies knowledge of the structure of a particular line of the file. I also suggest you examine the results of each step by entering the lines at the interactive prompt, then displaying the value of the newly created variable. You will be surprised by what you see! My personal guess is that you have not done the prior exercises or else the instructor is expecting miracles. Do what you can to improve things then return with more questions. -- Bob Gailer 919-636-4239 Chapel Hill NC -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Mon Oct 15 09:21:09 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 15 Oct 2012 08:21:09 +0100 Subject: [Tutor] PYTHON NEWBIE HELP! In-Reply-To: <1EF569B1486C5D4998A59060EF869779189F537F@CH1PRD0102MB149.prod.exchangelabs.com> References: <1EF569B1486C5D4998A59060EF869779189F537F@CH1PRD0102MB149.prod.exchangelabs.com> Message-ID: On 15/10/2012 02:21, Salinas, Erwin d wrote: Further to the information provided by Bob Gailer, please don't shout, as in TYPING THINGS IN UPPERCASE, as it's considered rude, thanks. -- Cheers. Mark Lawrence. From alan.gauld at btinternet.com Mon Oct 15 09:49:46 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 15 Oct 2012 08:49:46 +0100 Subject: [Tutor] PYTHON NEWBIE HELP! In-Reply-To: <1EF569B1486C5D4998A59060EF869779189F537F@CH1PRD0102MB149.prod.exchangelabs.com> References: <1EF569B1486C5D4998A59060EF869779189F537F@CH1PRD0102MB149.prod.exchangelabs.com> Message-ID: On 15/10/12 02:21, Salinas, Erwin d wrote: > HELP! Hi I'm a newbie in Python, I'm having trouble organizing the FILE > into the OUTPUT BELOW. I put the file into read, then I put it on a > list, and my plan is to put the name and the corresponding grades into a > dictionary, using the elements on the list, but when I print the > dictionary, it's not giving me the result I wanted. MY CODE SO FAR IS > SHOWN BELOW THE OUTPUT. There are lots of things we could comment on but I'll highlight just a few key ones. Fix them and try again... > filename = open("student_grades_hm_5.txt") > infile = filename.read() This reads the entire file as a single string. You might find file.readlines() more useful in this case. > mix_list = infile.split() This splits the file based on whitespace. It will ignore commas so that for example your first few items will be split: ['HM3', '80' ',' 'HM2' ',90' ',' 'ID,' '1000123456,HM4,92,Last_name,Rubble'] You can see this if you print the mix_list variable. Notice that you have commas on their own and the last 'item' is a long one. You need to look more closely at how you want split to work. Check the documentation. > print "Name | ID | HM1 | HM2 | HM3 | HM4 | Avg. |" > print "______________________________________________________" > > Student_one = > {"Name":mix_list[9],"ID":mix_list[5],"HM1":0,"HM2":mix_list[3], > "HM3":mix_list[1],"HM4":mix_list[7]} This probably gives you the wrong result because of the way you used split(). Also it will only output a single line, but for testing that may all you intended? But for future reference its always worth providing a sample of the faulty output so that we don;t need to guess. And instead of just saying it's 'not giving me the result I wanted', be more specific about what you perceive as being wrong. For example 'wrong data', 'bad formatting', 'incomplete data' etc -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From wayne at waynewerner.com Mon Oct 15 13:32:05 2012 From: wayne at waynewerner.com (Wayne Werner) Date: Mon, 15 Oct 2012 06:32:05 -0500 (CDT) Subject: [Tutor] I am learning python3 and would like some more python3 modules/programs on my... In-Reply-To: <5B80DD153D7D744689F57F4FB69AF474166E37D0@SCACMX008.exchad.jpmchase.net> References: <20121009180002.14120@gmx.com> <5B80DD153D7D744689F57F4FB69AF474166E37D0@SCACMX008.exchad.jpmchase.net> Message-ID: On Tue, 9 Oct 2012, Prasad, Ramit wrote: > You can even think about switching later if necessary. Although, it is probably easier to > go from 2 to 3 due to the existence of automated conversion tools. There is actually a python 3to2.py script now. And if you pick up Python 3 you'll have much less problems because you'll develop the correct mental model for dealing with strings/bytes, so if you *do* do any Python 2 work, then noooo problem. Unless you're using Django or one of the other big packages, my recommendation is always to pick up Python3, with the caveat that many of the tutorials will be Python 2 oriented, so if they run into problems, they should ask a question here. Plus, there will be no Python 2.8. Sure, people are still developing tools in Python 2.x, but more and more are migrating to 3.x, and *many* of the larger packages are Python 3 compatible. -Wayne From norman at khine.net Mon Oct 15 19:17:54 2012 From: norman at khine.net (Norman Khine) Date: Mon, 15 Oct 2012 18:17:54 +0100 Subject: [Tutor] extract uri from beautiful soup string In-Reply-To: <1350262935.4004.31.camel@infirit.lan> References: <507B1591.7040307@pearwood.info> <1350256337.4004.7.camel@infirit.lan> <1350261305.4004.23.camel@infirit.lan> <1350262935.4004.31.camel@infirit.lan> Message-ID: On Mon, Oct 15, 2012 at 2:02 AM, Sander Sweers wrote: > Sander Sweers schreef op ma 15-10-2012 om 02:35 [+0200]: >> > On Mon, Oct 15, 2012 at 12:12 AM, Sander Sweers wrote: >> > > Norman Khine schreef op zo 14-10-2012 om 23:10 [+0100]: >> > Norman Khine schreef op ma 15-10-2012 om 00:17 [+0100]: >> > i tried this: http://pastie.org/5059153 > > Btw, if I understand what you are trying to do then you can do this much > more simple. I noticed that all the a tags with onclick have an href > attribute of '#'. To get all of these do something like: > > soup.findAll('a', {'href':'#'}) thanks, i used that > > Then use the attrmap eg attrMap['onclick'].split('\'')[1]. > > Put together that may look like the below. > > for i in soup.findAll('a', {'href':'#'}): > if 'toolbar=0' in i.attrMap['onclick']: > print i.attrMap['onclick'].split('\'')[1] i made an update: https://gist.github.com/3891927 which works based on some of the recommendations posted here. any suggestions for improvement? > > Greets > Sander > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From eryksun at gmail.com Tue Oct 16 07:40:04 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 16 Oct 2012 01:40:04 -0400 Subject: [Tutor] extract uri from beautiful soup string In-Reply-To: References: <507B1591.7040307@pearwood.info> <1350256337.4004.7.camel@infirit.lan> <1350261305.4004.23.camel@infirit.lan> <1350262935.4004.31.camel@infirit.lan> Message-ID: On Mon, Oct 15, 2012 at 1:17 PM, Norman Khine wrote: > > i made an update: https://gist.github.com/3891927 which works based on > some of the recommendations posted here. > > any suggestions for improvement? I can't make any specific recommendations about using BeautifulSoup since I haven't used it in a long time. Here are a few general suggestions. PEP 8 recommends using 4-space indents instead of tabs, if possible. Move constant definitions out of loops. For example, there's no point to repeatedly building the dict literal and assigning it to "headers" for each page_no. Also, "headers" isn't used. You need to make a Request object: http://docs.python.org/library/urllib2#urllib2.Request Don't put unnecessary parentheses (round brackets) around assigned expressions. For example, replace req = ('%s%s' % (origin_site, page_no)) ... assoc_link = (tag.string) with req = '%s%s' % (origin_site, page_no) ... assoc_link = tag.string I'd also move the "%s" into origin_site: origin_site = ('http://typo3.nimes.fr/index.php?' 'id=annuaire_assos&theme=0&rech=&num_page=%s') req = origin_site % page_no The parentheses around the string literal are necessary because it's split over two lines. (I use Gmail's webmail interface, which line wraps at 69 columns -- sometimes; it tries to be clever.) Use "continue" to avoid unnecessary nesting: for page_no in pages: ... try: doc = urllib2.urlopen(req) except urllib2.URLError, e: continue soup = BeautifulSoup.BeautifulSoup(doc) for row in soup.findAll('tr', {'class': 'menu2'}): assoc_data = [] ... for i in soup.findAll('a', {'href': '#'}): if 'associations' not in i.attrMap['onclick']: continue req = i.attrMap['onclick'].split("'")[1] try: doc = urllib2.urlopen(req) except urllib2.URLError, e: continue soup = BeautifulSoup.BeautifulSoup(doc) Don't test "if emails != []". Use "if emails" instead. A non-empty list is always True, and an empty list is always False. Finally, in several places you create an empty list and populate it with a for loop. For simple cases like these, using comprehensions and generator expressions executes faster, yet it's still easy to code and easy to understand. For example, you can replace the following: assoc_data = [] for assoc_theme in soup.findAll('u'): assoc_data.append(assoc_theme.renderContents()) for assoc_name in soup.findAll('td', {'width': '70%'}): assoc_data.append(assoc_name.renderContents()) with something like this: assoc_data = [assoc_theme.renderContents() for assoc_theme in soup.findAll('u')] assoc_data.extend(assoc_name.renderContents() for assoc_name in soup.findAll('td', {'width': '70%'})) At least to me this is just as readable as the original, and the generated code is more efficient. If, however, BeautifulSoup is the limiting factor here, the efficiency gain will be chump change in the big picture. Still, in a simple case like this it's hard to argue against using a comprehension or generator expression. In cases that use complex expressions for the value and conditions, I think it makes more sense to use a for loop, which is easier to read and debug. From abasiemeka at gmail.com Tue Oct 16 09:53:34 2012 From: abasiemeka at gmail.com (Osemeka Osuagwu) Date: Tue, 16 Oct 2012 08:53:34 +0100 Subject: [Tutor] Tutor Digest, Vol 104, Issue 60 In-Reply-To: References: Message-ID: On Sun, 14 Oct 2012 16:21:38 +0100 Alan Gauld wrote: > >> In the code below, I can't seem to get around calling an instance >> method (__reality_check()) from within a class method (update_grid()), > > Of course you can't :) > > Since the class method has no access to the instance, it cannot call > instance methods, since it doesn't know which instance to use! > > Instead, update_grid needs to be an instance method. Of course it > does! It needs to update a specific grid, right? That will be the > instance. All of a sudden it's clear as day! > > Class methods are generally used for alternate constructor functions, e.g. > dict.fromkeys. Since it makes no difference whether you call fromkeys > from the class (dict) or from an instance, it is made a classmethod. ...this makes a whole lot of sense now. >> >> class Grid: >> '''Grid(rows = 26, columns = 26) -> Provides a world object >> for the cells in Game of Life''' >> from time import sleep >> from os import system >> from sys import platform > > Move the imports outside of the class. They should go at the start of > the module. > > As written, your class has attributes Grid.sleep, Grid.system, etc. Since > these attributes have nothing to do with Grids, they don't belong in the > Grid class. Move them out. > > Classes are not catch-all collections of unrelated methods. That's > what modules are for :) I see, thanks. >> @staticmethod >> def display_grid(generation, os_type): >> try: >> (lambda x:Grid.system('cls') if 'win' in >> os_type else Grid.system('clear'))(1) > > > That's wrong! Your display_grid method uses the class (Grid), so it > shouldn't be a static method. > > Move the imports to the module level, and display_grid as a regular > function: > > def display_grid(generation, array): > clear_screen() > print 'Now in Generation %d' % generation > for line in array: > print line > > def clear_screen(platform=sys.platform()): > # The default here is set once, and defaults to your platform. > if 'win' in platform: > command = 'cls' > else: > command = 'clear' > os.system(command) > > > Or leave display_grid as an instance method: > > > class Grid: > def display_grid(self, generation): > clear_screen() > print 'Now in Generation %d' % generation > for line in self._array: > print line > > > Either way, clear_screen has nothing to do with grids, so it should > not be a method of Grid. After looking at the second option, I thought; Of course! After all it's "class Grid" so as a "display_grid" instance method makes sense. I get the logic of it now. >> except: >> print 'Cannot display Grid' > > Never use a bare except like that. Bare except should *only* be used > at the interactive interpreter, for lazy people who want to avoid > typing. It should never be used non-interactively, it's simply poor > practice. > > The problem with bare excepts is that they cover up too much. Your aim > is to write code that works. If it has a bug, you need to *see* the bug > so you can identify it and then fix it. Bare excepts make bugs > invisible. > I admit I used that to avoid dealing with 'exception tantrums' during debugging, so that the program wouldn't halt if something went wrong at that point. It was going away anyway. I'll avoid it henceforth. >> @staticmethod >> def extend_grid(thickness = 4, force_extend = False): >> '''extend_grid([thickness][, force_extend]) --> >> Extends the edges of the grid by 4 units in >> all directions if the boundary of the simulation comes close to the >> edge. Extends anyway if force_extend option is True''' > > Why is this a static method? > > Which grid is this operating on? It should either take a grid as an > argument, or extend_grid should be an instance method that operates > on itself: > > # either this top-level function > def extend_grid(grid, thickness=4, force=False): > # do stuff to the grid to extend it > # and then return it > return grid > > > # or make this a regular instance method > class Grid: > def extend_grid(self, thickness=4, force=False): > # do stuff to self to extend it > # no need to return anything Just to clarify, if I went with the top level function; then I guess I'll define a 'name' attribute for each individual grid and then pass that name in place of the 'grid' argument in your example. Is this correct? >> '''Runs the 'Life' World simulation for the specified >> number of generations at the given speed''' >> #control = input >> #while >> for times in range(generations + 1): > > This contains an off-by-one bug. If you pass generations=100 as argument, > it will run 101 times instead. > > Instead, if you want your generations to count from 1: > > for times in range(1, generations + 1): > ... thanks, silly mistake >> def edit_cell(self, cells, state = '##'): >> '''edit_cell(cells[, state]) --> Where cells is a list >> of tuples containing coordinates of the cells to edit''' >> cells = cells >> for eachcell in cells: >> Grid.__array[eachcell[0]][eachcell[1]] = state >> return > > This is an abuse of the class. You have instances, but they all share state. > Why? Sharing state is okay if you have a reason for wanting to share state, > but here there doesn't appear to be any reason for it. > > As you have written this, you cannot have two Grids. Actually you can, but > since they both share the same state, you cannot have two DIFFERENT Grids. I don't quite get this one; I intended state to be just the value of the particular cell in the concerned instance. I don't think it gets shared by all the grids. The edit method was meant as a way of seeding the initial cell configuration, so it just writes whatever it is passed in the 'state' arg over that cell location; then again, I'm probably missing something here... > My advice is, forget about class and static methods completely until you > are a bit more experienced with object-oriented design. > most definitely, and to my relief too. I really appreciate how you walked through the code bit by bit, I've learned a great deal here. thank you Steven --------------------------- On Sun, 14 Oct 2012 16:21:38 +0100 Alan Gauld wrote: > On 14/10/12 13:34, Osemeka Osuagwu wrote: > >> I understand instance, static and class methods but I'm still >> struggling with when and where they should be used. > > You can pretty much ignore static methods, they were almost an > historic 'accident' superseded, in most cases, by class methods. The > number of cases where a staticmethod is really needed is very low. > > class methods you will rarely need but they are used when you want to do > something to the entire class of objects. i.e. something that affects > all instances both existing and future. Or they could be used where > there are no instances (yet). Factory methods, instance selection > methods, modifying shared attributes, instance pooling, are all > examples. To give some kind of heuristic based on my personal usage, I > use class methods in almost all my bigger projects. Such a project might > have 20 or 30 (or more) classes. Out of that maybe 2 or 3 will have any > class methods. > > Instance methods are by far the most common. These are the things you > want to do to a specific instance of the class. Changes to one instance > have no effect on other instances of the same class. wonderful explanation; I browsed a lot of material looking for an explanation like this with real examples and distinction between the method types. Thanks, it's just what I needed. >> In the code below, I can't seem to get around calling an instance >> method (__reality_check()) from within a class method (update_grid()), > > It's not a class method, you want to update a specific instance of a > grid. update_grids() might be a class method if you wanted to, say, > change the maximum buffer size used to store the grids, or cause all > grids to display the word 'empty' instead of a blank. Of course to do > that you would need access to all the instances to update their existing > values so the constructor would need to store a reference to itself in > the class somewhere (or maybe in a separate shared database). > The class method would then iterate over all instances calling their > instance method update_grid(). This is a common(*) class method pattern, > where a plural class method calls the singular instance methods > of all the instances. > > (*) I say common, but of course class methods are not common > so its a very relative term! I see. I guess this is like what Steven said in his reply about using extend_grid as a top level function. I mean storing a reference to itself and then letting the top level method use that reference to access the (future) instance(s). > HTH > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > It sure does. Thank's a great deal. Abasiemeka. From alan.gauld at btinternet.com Tue Oct 16 11:30:54 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 16 Oct 2012 10:30:54 +0100 Subject: [Tutor] Tutor Digest, Vol 104, Issue 60 In-Reply-To: References: Message-ID: On 16/10/12 08:53, Osemeka Osuagwu wrote: >> # or make this a regular instance method >> class Grid: >> def extend_grid(self, thickness=4, force=False): >> # do stuff to self to extend it >> # no need to return anything > > Just to clarify, if I went with the top level function; then I guess > I'll define a 'name' attribute for each individual grid and then pass > that name in place of the 'grid' argument in your example. Is this > correct? Yes but then it wouldn't be OOP. You'd be back in the world of traditional procedural programming passing explicit data references around. Its much better to make it an instance method with the self reference being passed implicitly >>> def edit_cell(self, cells, state = '##'): >>> cells = cells >>> for eachcell in cells: >>> Grid.__array[eachcell[0]][eachcell[1]] = state >>> return >>... >> As you have written this, you cannot have two Grids. Actually you can, but >> since they both share the same state, you cannot have two DIFFERENT Grids. > > I don't quite get this one; I intended state to be just the value of > the particular cell in the concerned instance. I don't think it gets > shared by all the grids. Notice the line: Grid.__array[....] = state The array is shared by all instances because its an attribute of the class not the instance. You want every instance to have its own __array[] of cells. ie. self.__array[] Incidentally the cells = cells line is pointless, it does nothing. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eryksun at gmail.com Tue Oct 16 12:45:30 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 16 Oct 2012 06:45:30 -0400 Subject: [Tutor] Tutor Digest, Vol 104, Issue 60 In-Reply-To: References: Message-ID: On Tue, Oct 16, 2012 at 3:53 AM, Osemeka Osuagwu wrote: > On Sun, 14 Oct 2012 16:21:38 +0100 Alan Gauld wrote: > > Just to clarify, if I went with the top level function; then I guess > I'll define a 'name' attribute for each individual grid and then pass > that name in place of the 'grid' argument in your example. Is this > correct? No, you'd just pass a grid object, but you probably want an instance method, anyway. Perhaps some background can clarify. Here's a function and a class that have no relation: >>> def f(grid): pass >>> class Test(object): pass The __get__ method-wrapper of the function can bind it as a method of a Test object: >>> f.__get__(Test(), Test) > If f is accessed as an attribute of Test, it behaves the same as above: >>> Test.f = f >>> Test().f > The first argument of a function, when the function is intended to be a method, is conventionally called "self". The bound method created by __get__ automatically supplies this argument for you. If you instead make the function global (module level), it's best to use a name that suggests the kind of object you expect as the first argument (e.g. grid). But really, if it expects to work on a particular grid, you should make it a method of Grid. Other kinds of methods: If the instance is None, __get__ returns an unbound method: >>> f.__get__(None, Test) A classmethod binds to the class instead of an instance: >>> classmethod(f).__get__(None, Test) > >>> classmethod(f).__get__(Test(), Test) > When a function is intended to be a classmethod, it's common to name the first argument "cls". Finally, a staticmethod has no binding at all. Its __get__ method-wrapper simply returns the wrapped function: >>> staticmethod(f).__get__(None, Test) >>> staticmethod(f).__get__(Test(), Test) There's not much use for this. Refer to the Descriptor HowTo Guide for more details: http://docs.python.org/howto/descriptor.html > I don't quite get this one; I intended state to be just the value of > the particular cell in the concerned instance. I don't think it gets > shared by all the grids. The edit method was meant as a way of seeding > the initial cell configuration, so it just writes whatever it is > passed in the 'state' arg over that cell location; then again, I'm > probably missing something here... Grid.__array is used throughout the class definition. This is a class attribute. Instances will not get their own copy. Instead, create the list of lists in Grid.__init__: def __init__(self, rows=26, cols=26): '''Create a rows x cols grid''' self._array = [[''] * cols for i in range(rows)] print 'A %d x %d cell World has been Created' % (rows, cols) Note that this uses a list comprehension to create each row as a unique list. If instead it multiplied a list literal by a number, each row would be the *same* list. For example: >>> rows = [[''] * 2] * 2 >>> rows[0][0] = '##' >>> rows [['##', ''], ['##', '']] Also note that it uses _array with a single leading underscore. This is the convention for 'private' data or methods. It signals to other developers that this is not part of the public API. Using a leading double underscore causes the compiler to use name mangling in order to prevent a subclass from easily overriding an attribute; it's used infrequently. Here's how the edit_cell method changes now that _array is instance data: def edit_cell(self, cells, state='##'): '''cells is a list of tuples containing coordinates of the cells to edit ''' for row, col in cells: self._array[row][col] = state return From norman at khine.net Tue Oct 16 13:52:32 2012 From: norman at khine.net (Norman Khine) Date: Tue, 16 Oct 2012 12:52:32 +0100 Subject: [Tutor] extract uri from beautiful soup string In-Reply-To: References: <507B1591.7040307@pearwood.info> <1350256337.4004.7.camel@infirit.lan> <1350261305.4004.23.camel@infirit.lan> <1350262935.4004.31.camel@infirit.lan> Message-ID: On Tue, Oct 16, 2012 at 6:40 AM, eryksun wrote: > On Mon, Oct 15, 2012 at 1:17 PM, Norman Khine wrote: >> >> i made an update: https://gist.github.com/3891927 which works based on >> some of the recommendations posted here. >> >> any suggestions for improvement? > > I can't make any specific recommendations about using BeautifulSoup > since I haven't used it in a long time. Here are a few general > suggestions. > > PEP 8 recommends using 4-space indents instead of tabs, if possible. > > Move constant definitions out of loops. For example, there's no point > to repeatedly building the dict literal and assigning it to "headers" > for each page_no. Also, "headers" isn't used. You need to make a > Request object: > > http://docs.python.org/library/urllib2#urllib2.Request > > > Don't put unnecessary parentheses (round brackets) around assigned > expressions. For example, replace > > req = ('%s%s' % (origin_site, page_no)) > ... > assoc_link = (tag.string) > > with > > req = '%s%s' % (origin_site, page_no) > ... > assoc_link = tag.string > > I'd also move the "%s" into origin_site: > > origin_site = ('http://typo3.nimes.fr/index.php?' > 'id=annuaire_assos&theme=0&rech=&num_page=%s') > > req = origin_site % page_no > > The parentheses around the string literal are necessary because it's > split over two lines. (I use Gmail's webmail interface, which line > wraps at 69 columns -- sometimes; it tries to be clever.) > > Use "continue" to avoid unnecessary nesting: > > for page_no in pages: > ... > try: > doc = urllib2.urlopen(req) > except urllib2.URLError, e: > continue > soup = BeautifulSoup.BeautifulSoup(doc) > for row in soup.findAll('tr', {'class': 'menu2'}): > assoc_data = [] > ... > for i in soup.findAll('a', {'href': '#'}): > if 'associations' not in i.attrMap['onclick']: > continue > req = i.attrMap['onclick'].split("'")[1] > try: > doc = urllib2.urlopen(req) > except urllib2.URLError, e: > continue > soup = BeautifulSoup.BeautifulSoup(doc) > > > Don't test "if emails != []". Use "if emails" instead. A non-empty > list is always True, and an empty list is always False. > > Finally, in several places you create an empty list and populate it > with a for loop. For simple cases like these, using comprehensions and > generator expressions executes faster, yet it's still easy to code and > easy to understand. For example, you can replace the following: > > assoc_data = [] > for assoc_theme in soup.findAll('u'): > assoc_data.append(assoc_theme.renderContents()) > for assoc_name in soup.findAll('td', {'width': '70%'}): > assoc_data.append(assoc_name.renderContents()) > > with something like this: > > assoc_data = [assoc_theme.renderContents() > for assoc_theme in soup.findAll('u')] > assoc_data.extend(assoc_name.renderContents() > for assoc_name in soup.findAll('td', {'width': '70%'})) > > At least to me this is just as readable as the original, and the > generated code is more efficient. If, however, BeautifulSoup is the > limiting factor here, the efficiency gain will be chump change in the > big picture. Still, in a simple case like this it's hard to argue > against using a comprehension or generator expression. In cases that > use complex expressions for the value and conditions, I think it makes > more sense to use a for loop, which is easier to read and debug. thanks, i made the changes https://gist.github.com/3891927 -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From eryksun at gmail.com Tue Oct 16 15:48:07 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 16 Oct 2012 09:48:07 -0400 Subject: [Tutor] extract uri from beautiful soup string In-Reply-To: References: <507B1591.7040307@pearwood.info> <1350256337.4004.7.camel@infirit.lan> <1350261305.4004.23.camel@infirit.lan> <1350262935.4004.31.camel@infirit.lan> Message-ID: On Tue, Oct 16, 2012 at 7:52 AM, Norman Khine wrote: > > thanks, i made the changes https://gist.github.com/3891927 On line 67, use the result of soup.findAll directly: assoc_data.extend(assoc_cont.renderContents() for assoc_cont in soup.findAll('td', {'width': '49%', 'class': 'menu2' })) On line 72, can't the result of findAll() be subscripted, if you only want the first item? For example: assoc_tel = soup.findAll('td', {'width': '45%', 'class': 'menu2'}) assoc_data.append(assoc_tel[0].renderContents()) On line 80 you can use writerows instead: with open('nimes_assoc.csv', 'wb') as f: csv.writer(f).writerows(assoc_table) From abhishek.vit at gmail.com Tue Oct 16 18:57:24 2012 From: abhishek.vit at gmail.com (Abhishek Pratap) Date: Tue, 16 Oct 2012 09:57:24 -0700 Subject: [Tutor] managing memory large dictionaries in python Message-ID: Hi Guys For my problem I need to store 400-800 million 20 characters keys in a dictionary and do counting. This data structure takes about 60-100 Gb of RAM. I am wondering if there are slick ways to map the dictionary to a file on disk and not store it in memory but still access it as dictionary object. Speed is not the main concern in this problem and persistence is not needed as the counting will only be done once on the data. We want the script to run on smaller memory machines if possible. I did think about databases for this but intuitively it looks like a overkill coz for each key you have to first check whether it is already present and increase the count by 1 and if not then insert the key into dbase. Just want to take your opinion on this. Thanks! -Abhi From ramit.prasad at jpmorgan.com Tue Oct 16 22:03:22 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 16 Oct 2012 20:03:22 +0000 Subject: [Tutor] managing memory large dictionaries in python In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF474167184FE@SCACMX008.exchad.jpmchase.net> Abhishek Pratap wrote: > Sent: Tuesday, October 16, 2012 11:57 AM > To: tutor at python.org > Subject: [Tutor] managing memory large dictionaries in python > > Hi Guys > > For my problem I need to store 400-800 million 20 characters keys in a > dictionary and do counting. This data structure takes about 60-100 Gb > of RAM. > I am wondering if there are slick ways to map the dictionary to a file > on disk and not store it in memory but still access it as dictionary > object. Speed is not the main concern in this problem and persistence > is not needed as the counting will only be done once on the data. We > want the script to run on smaller memory machines if possible. > > I did think about databases for this but intuitively it looks like a > overkill coz for each key you have to first check whether it is > already present and increase the count by 1 and if not then insert > the key into dbase. > > Just want to take your opinion on this. > > Thanks! > -Abhi I do not think that a database would be overkill for this type of task. Your process may be trivial but the amount of data it has manage is not trivial. You can use a simple database like SQLite. Otherwise, you could create a file for each key and update the count in there. It will run on a small amount of memory but will be slower than using a db. # Pseudocode key = get_key() filename = os.path.join(directory, key) if os.path.exists(filename): # read and update count else: with open(os.path.join(directory, key), 'w') as f: f.write('1') Given that SQLite is included in Python and is easy to use, I would just use that. -Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From emile at fenx.com Wed Oct 17 00:21:43 2012 From: emile at fenx.com (emile) Date: Tue, 16 Oct 2012 15:21:43 -0700 Subject: [Tutor] managing memory large dictionaries in python In-Reply-To: <5B80DD153D7D744689F57F4FB69AF474167184FE@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF474167184FE@SCACMX008.exchad.jpmchase.net> Message-ID: On 10/16/2012 01:03 PM, Prasad, Ramit wrote: > Abhishek Pratap wrote: >> Sent: Tuesday, October 16, 2012 11:57 AM >> To: tutor at python.org >> Subject: [Tutor] managing memory large dictionaries in python >> >> Hi Guys >> >> For my problem I need to store 400-800 million 20 characters keys in a >> dictionary and do counting. This data structure takes about 60-100 Gb >> of RAM. >> I am wondering if there are slick ways to map the dictionary to a file >> on disk and not store it in memory but still access it as dictionary >> object. Speed is not the main concern in this problem and persistence >> is not needed as the counting will only be done once on the data. We >> want the script to run on smaller memory machines if possible. >> >> I did think about databases for this but intuitively it looks like a >> overkill coz for each key you have to first check whether it is >> already present and increase the count by 1 and if not then insert >> the key into dbase. >> >> Just want to take your opinion on this. >> >> Thanks! >> -Abhi > > I do not think that a database would be overkill for this type of task. Agreed. > Your process may be trivial but the amount of data it has manage is not trivial. You can use a simple database like SQLite. Otherwise, you > could create a file for each key and update the count in there. It will > run on a small amount of memory but will be slower than using a db. Well, maybe -- depends on how many unique entries exist. Most vanilla systems are going to crash (or give the appearance thereof) if you end up with millions of file entries in a directory. If a filesystem based answer is sought, I'd consider generating 16-bit CRCs per key and appending the keys to the CRC named file, then pass those, sort and do the final counting. Emile From oscar.j.benjamin at gmail.com Wed Oct 17 00:30:12 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 16 Oct 2012 23:30:12 +0100 Subject: [Tutor] managing memory large dictionaries in python In-Reply-To: <5B80DD153D7D744689F57F4FB69AF474167184FE@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF474167184FE@SCACMX008.exchad.jpmchase.net> Message-ID: On 16 October 2012 21:03, Prasad, Ramit wrote: > Abhishek Pratap wrote: >> Sent: Tuesday, October 16, 2012 11:57 AM >> To: tutor at python.org >> Subject: [Tutor] managing memory large dictionaries in python >> >> Hi Guys >> >> For my problem I need to store 400-800 million 20 characters keys in a >> dictionary and do counting. This data structure takes about 60-100 Gb >> of RAM. >> I am wondering if there are slick ways to map the dictionary to a file >> on disk and not store it in memory but still access it as dictionary >> object. Speed is not the main concern in this problem and persistence >> is not needed as the counting will only be done once on the data. We >> want the script to run on smaller memory machines if possible. >> >> I did think about databases for this but intuitively it looks like a >> overkill coz for each key you have to first check whether it is >> already present and increase the count by 1 and if not then insert >> the key into dbase. >> >> Just want to take your opinion on this. >> >> Thanks! >> -Abhi > > I do not think that a database would be overkill for this type of task. Neither do I but I think that there are also ways to make it more memory efficient without the use of disk storage. I suspect that the bulk of the memory usage is for the strings and there are often ways to store them more efficiently in memory. For example, are you using Python 3? You might be able to reduce the memory consumption by 25-50% by using byte strings instead of unicode strings (assuming that the keys are ascii). Further gains are possible if your strings only use a subset of ascii characters, as is the case for e.g. hex strings. If you can map the strings reversibly to integers with a function (rather than a dict) then you should be able to achieve a significant reduction in memory usage by using ints as the dictionary keys. A 20 character hex key can theoretically be stored with 80 bits or 10 bytes. If you could hit this limit then you would only need 4-8 GB of memory for the strings themselves (which may still be too much). > Your process may be trivial but the amount of data it has manage is not trivial. You can use a simple database like SQLite. Otherwise, you > could create a file for each key and update the count in there. It will > run on a small amount of memory but will be slower than using a db. > > # Pseudocode > key = get_key() > filename = os.path.join(directory, key) > if os.path.exists(filename): > # read and update count > else: > with open(os.path.join(directory, key), 'w') as f: > f.write('1') Depending on the file system this could require a large amount of disk space. If each file needed 4096 bytes of disk space then you would need around 2-3 TB of disk space with this solution. > > Given that SQLite is included in Python and is easy to use, I would just > use that. I would also try this. A standard database solution will likely give the least headaches in the long run. Oscar From alan.gauld at btinternet.com Wed Oct 17 02:03:21 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 17 Oct 2012 01:03:21 +0100 Subject: [Tutor] managing memory large dictionaries in python In-Reply-To: References: Message-ID: On 16/10/12 17:57, Abhishek Pratap wrote: > For my problem I need to store 400-800 million 20 characters keys in a > dictionary and do counting. This data structure takes about 60-100 Gb > of RAM. Thats a lot of records but without details of what kind of counting you plan on we can't give specific advice. > I am wondering if there are slick ways to map the dictionary to a file > on disk and not store it in memory but still access it as dictionary > object. Speed is not the main concern The trivial solution is to use shelve since that makes a file look like a dictionary. There are security issues but they don't sound like they'd be a problem. I've no idea what performance of shelve would be like with that many records though... > I did think about databases for this but intuitively it looks like a > overkill coz for each key you have to first check whether it is > already present and increase the count by 1 and if not then insert > the key into dbase. The database does all of that automatically and fast. You just need to set it up, load the data and use it - probably around 50 lines of SQL... And you don't need anything fancy for a single table database - Access, SQLite, even FoxPro... Or you could just create a big text file and process it line by line if the data fits that model. Lots of options. Personally I'd go with a database for speed, flexibility and ease of coding. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From breamoreboy at yahoo.co.uk Wed Oct 17 02:43:52 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 17 Oct 2012 01:43:52 +0100 Subject: [Tutor] managing memory large dictionaries in python In-Reply-To: References: Message-ID: On 17/10/2012 01:03, Alan Gauld wrote: > > You just need to set it up, load the data and use it - probably around > 50 lines of SQL... And you don't need anything fancy for a single table > database - Access, SQLite, even FoxPro... > For the record Access is not a database, or so some geezer called Alex Martelli reckons http://code.activestate.com/lists/python-list/48130/, so please don't shoot the messenger:) -- Cheers. Mark Lawrence. From rhettnaxel at gmail.com Wed Oct 17 04:22:54 2012 From: rhettnaxel at gmail.com (Alexander) Date: Tue, 16 Oct 2012 22:22:54 -0400 Subject: [Tutor] managing memory large dictionaries in python In-Reply-To: References: Message-ID: On Tue, Oct 16, 2012 at 20:43 EST, Mark Lawrence wrote: > For the record Access is not a database, or so some geezer called Alex > Martelli reckons http://code.activestate.com/lists/python-list/48130/, so > please don't shoot the messenger:) > Cheers. > Mark Lawrence. Mark I don't believe your response is relevant or helpful to the original post so please don't hijack. -- 7D9C597B From memilanuk at gmail.com Wed Oct 17 05:59:11 2012 From: memilanuk at gmail.com (Monte Milanuk) Date: Tue, 16 Oct 2012 20:59:11 -0700 Subject: [Tutor] CSV -> sqlite tables with foreign keys Message-ID: Hello, I'm working on a python script to take the sql script, create a sqlite3 database, create the tables, and then populate them using the info from the csv file. The sticking point seems to be creating the foreign keys between the tables I've got a data file with lines like this: "John","G.","Smith","1972-11-10","123 Any Place","Somewhere","Missouri","58932" I have an SQL script set up to create the tables with the appropriate fields. What I have so far that works is something like this: try: data = open(CSV_FILE, 'rb') reader = csv.reader(data) for row in reader: cursor.execute('''INSERT INTO people (first_name, mid_init, last_name, birth_date) VALUES (?,?,?,?)''', row[0:4]) person = cursor.lastrowid address = list(row) address.append(person) row = tuple(address) cursor.execute('''INSERT INTO physical_address (street, city, state, zip_code,person) VALUES (?,?,?,?,?)''', row[4:]) finally: data.close() It works... but from what I've found on the web, I get the distinct impression that converting from a tuple to a list and back is considered poor practice at best and generally to be avoided. I'm not really sure how else to go about this, though, when I need to split one row from a CSV file across two (or more) tables in a database, and maintain some sort of relation between them. Any suggestions? Thanks, Monte -------------- next part -------------- An HTML attachment was scrubbed... URL: From abhishek.vit at gmail.com Wed Oct 17 06:56:22 2012 From: abhishek.vit at gmail.com (Abhishek Pratap) Date: Tue, 16 Oct 2012 21:56:22 -0700 Subject: [Tutor] managing memory large dictionaries in python In-Reply-To: References: Message-ID: On Tue, Oct 16, 2012 at 7:22 PM, Alexander wrote: > On Tue, Oct 16, 2012 at 20:43 EST, Mark Lawrence > wrote: >> For the record Access is not a database, or so some geezer called Alex >> Martelli reckons http://code.activestate.com/lists/python-list/48130/, so >> please don't shoot the messenger:) >> Cheers. >> Mark Lawrence. > > Mark I don't believe your response is relevant or helpful to the > original post so please don't hijack. > > > -- > 7D9C597B > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Thanks guys..I think I will try shelve and sqlite. I dont think creating millions of files (one for each key) will make the sys admins/file system happy. Best, -Abhi From hugo.yoshi at gmail.com Wed Oct 17 08:07:20 2012 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Wed, 17 Oct 2012 08:07:20 +0200 Subject: [Tutor] CSV -> sqlite tables with foreign keys In-Reply-To: References: Message-ID: On Wed, Oct 17, 2012 at 5:59 AM, Monte Milanuk wrote: > Hello, > > I'm working on a python script to take the sql script, create a sqlite3 > database, create the tables, and then populate them using the info from the > csv file. > The sticking point seems to be creating the foreign keys between the tables > > I've got a data file with lines like this: > > "John","G.","Smith","1972-11-10","123 Any > Place","Somewhere","Missouri","58932" > > I have an SQL script set up to create the tables with the appropriate > fields. > > What I have so far that works is something like this: > > try: > data = open(CSV_FILE, 'rb') > reader = csv.reader(data) > for row in reader: > cursor.execute('''INSERT INTO people (first_name, mid_init, > last_name, birth_date) > VALUES (?,?,?,?)''', row[0:4]) > > person = cursor.lastrowid > address = list(row) > address.append(person) > row = tuple(address) > > cursor.execute('''INSERT INTO physical_address (street, city, > state, zip_code,person) > VALUES (?,?,?,?,?)''', row[4:]) > finally: > data.close() > > > It works... but from what I've found on the web, I get the distinct > impression that converting from a tuple to a list and back is considered > poor practice at best and generally to be avoided. > > I'm not really sure how else to go about this, though, when I need to > split one row from a CSV file across two (or more) tables in a database, > and maintain some sort of relation between them. > > Any suggestions? > > > Thanks, > > Monte > Well, converting to a list and back is a little superfluous if all you need to do is add a single element. You could just construct a new tuple like so: # now address would look like ("123 Any Place","Somewhere","Missouri","58932", cursor.lastrowid) address_tuple = row[4:] + (cursor.lastrowid,) cursor.execute('''INSERT INTO physical_address (street, city, state, zip_code,person) VALUES (?,?,?,?,?)''', address_tuple) Note that we put cursor.lastrowid into a 1-element tuple so we can + the two together. It might look a tad clunky but it's pretty easy to read. Hugo -------------- next part -------------- An HTML attachment was scrubbed... URL: From abasiemeka at gmail.com Wed Oct 17 08:54:32 2012 From: abasiemeka at gmail.com (Osemeka Osuagwu) Date: Wed, 17 Oct 2012 07:54:32 +0100 Subject: [Tutor] Tutor Digest, Vol 104, Issue 65 In-Reply-To: References: Message-ID: On Tue, 16 Oct 2012 10:30:54 +0100 Alan Gauld wrote > > Yes but then it wouldn't be OOP. You'd be back in the world of > traditional procedural programming passing explicit data references > around. Its much better to make it an instance method with the self > reference being passed implicitly > I guess so, implicit is better than explicit :) Thanks Alan. From daedae11 at 126.com Wed Oct 17 10:23:46 2012 From: daedae11 at 126.com (Dae James) Date: Wed, 17 Oct 2012 16:23:46 +0800 Subject: [Tutor] Why VPython can't be searched out in PyPI? Message-ID: <2012101716234618151118@126.com> I found that VPython is not in PyPI(python packet index from www.python.org). Why ? Dae James -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Wed Oct 17 10:46:20 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 17 Oct 2012 09:46:20 +0100 Subject: [Tutor] Why VPython can't be searched out in PyPI? In-Reply-To: <2012101716234618151118@126.com> References: <2012101716234618151118@126.com> Message-ID: On 17/10/2012 09:23, Dae James wrote: > I found that VPython is not in PyPI(python packet index from www.python.org). Why ? > > Dae James > The author(s) have chosen not to place it there. -- Cheers. Mark Lawrence. From breamoreboy at yahoo.co.uk Wed Oct 17 10:47:13 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 17 Oct 2012 09:47:13 +0100 Subject: [Tutor] managing memory large dictionaries in python In-Reply-To: References: Message-ID: On 17/10/2012 03:22, Alexander wrote: > On Tue, Oct 16, 2012 at 20:43 EST, Mark Lawrence > wrote: >> For the record Access is not a database, or so some geezer called Alex >> Martelli reckons http://code.activestate.com/lists/python-list/48130/, so >> please don't shoot the messenger:) >> Cheers. >> Mark Lawrence. > > Mark I don't believe your response is relevant or helpful to the > original post so please don't hijack. > > On an open forum I'll say what I like thank you. -- Cheers. Mark Lawrence. From eryksun at gmail.com Wed Oct 17 11:19:27 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 17 Oct 2012 05:19:27 -0400 Subject: [Tutor] CSV -> sqlite tables with foreign keys In-Reply-To: References: Message-ID: On Tue, Oct 16, 2012 at 11:59 PM, Monte Milanuk wrote: > > address = list(row) > address.append(person) > row = tuple(address) The rows from the csv.reader are already lists. Also, the parameter list in the 2nd argument only needs to be a sequence (e.g. tuple, list, string), or it can also be a dict if the statement uses named placeholders. > from what I've found on the web, I get the distinct impression > that converting from a tuple to a list and back is considered > poor practice at best and generally to be avoided. To update a record in a tuple you can use slicing and concatenation (+) as an alternative to creating a temporary list. A list is more applicable to homogenous data (e.g. a list of tuples, each a data record). If you want a container for a record that you can modify more efficiently, use a dict or a custom object. For the latter, look into ORMs such as Storm: https://storm.canonical.com/ > Any suggestions? I found a Stack Overflow answer that uses a table "view" combined with an "instead of" trigger to update two tables with one insert. http://stackoverflow.com/a/11715983/205580 Here's my meager attempt at an adaptation (some names have been changed to protect the innocent...): import csv import sqlite3 con = sqlite3.connect(':memory:') cur = con.cursor() cur.execute('''create table person ( id integer primary key autoincrement, firstname, midinit, lastname, birthdate) ''') cur.execute('''create table address ( id integer primary key autoincrement, person_id integer references person not null, street, city, state, zipcode) ''') cur.execute('''create view person_view as select person.firstname, person.midinit, person.lastname, person.birthdate, address.street, address.city, address.state, address.zipcode from person inner join address on person.id = address.person_id ''') cur.execute('''create trigger person_view_insert instead of insert on person_view begin insert into person (firstname, midinit, lastname, birthdate) values (new.firstname, new.midinit, new.lastname, new.birthdate); insert into address (person_id, street, city, state, zipcode) values ((select last_insert_rowid()), new.street, new.city, new.state, new.zipcode); end ''') import io data = io.BytesIO(b'''\ John,G.,Smith,1972-11-10,123 Any Place,Somewhere,Missouri,58932 Jane,L.,Jones,1971-12-20,321 Some Place,Anywhere,Kansas,12345 ''') reader = csv.reader(data) for row in reader: cur.execute('''insert into person_view (firstname, midinit, lastname, birthdate, street, city, state, zipcode) values (?,?,?,?,?,?,?,?)''', row) # output for row in cur.execute('select * from person'): print row for row in cur.execute('select * from address'): print row person table: (1, u'John', u'G.', u'Smith', u'1972-11-10') (2, u'Jane', u'L.', u'Jones', u'1971-12-20') address table: (1, 1, u'123 Any Place', u'Somewhere', u'Missouri', u'58932') (2, 2, u'321 Some Place', u'Anywhere', u'Kansas', u'12345') From dwightdhutto at gmail.com Thu Oct 11 03:58:39 2012 From: dwightdhutto at gmail.com (Dwight Hutto) Date: Wed, 10 Oct 2012 21:58:39 -0400 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: <50762463.8070300@pearwood.info> References: <20121003063843.GB27111@ando> <506D72E0.60402@pearwood.info> <50762463.8070300@pearwood.info> Message-ID: If your app has a standard usage of phrases, you can place a file in that translates a tag into a particular language phrase. if submit_tag_selection == 'english': submit = 'Submit' if submit_tag_selection == 'english': submit = 'Soumettre' Of course this could be done without the if, you would just translate the normal selections within a file with the commonly used phrases in the app, and substitute it within a parse for: x = open('translate_file_french', 'r') for line in x: if line.split('=')[0] == 'Submit': print '%s' % (line.split('=')[1]) 'Soumettre' *Untested, but should work -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com From dwightdhutto at gmail.com Wed Oct 17 03:30:43 2012 From: dwightdhutto at gmail.com (Dwight Hutto) Date: Tue, 16 Oct 2012 21:30:43 -0400 Subject: [Tutor] managing memory large dictionaries in python In-Reply-To: References: Message-ID: On Tue, Oct 16, 2012 at 12:57 PM, Abhishek Pratap wrote: > Hi Guys > > For my problem I need to store 400-800 million 20 characters keys in a > dictionary and do counting. This data structure takes about 60-100 Gb > of RAM. > I am wondering if there are slick ways to map the dictionary to a file > on disk and not store it in memory but still access it as dictionary > object. Speed is not the main concern in this problem and persistence > is not needed as the counting will only be done once on the data. We > want the script to run on smaller memory machines if possible. > > I did think about databases for this but intuitively it looks like a > overkill coz for each key you have to first check whether it is > already present and increase the count by 1 and if not then insert > the key into dbase. > > Just want to take your opinion on this. > > Thanks! > -Abhi > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor My inexperienced advice would be to begin with the storage areas available. I would begin by eliminating certain things such as: x = {'one_entry' : 1} into x = {'one_entry':1} To map, you would want maybe different db files that contain certain info within a certain range. 0-1000 entries in the first file, etc. os.walk a directory, and find the mapped file in a particular range file, then go straight to the entry needed. Make the dict one long line, and you could eliminate any /n newline chars. I could do better with more time, but that seems like a good solution at this point. Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com From dwightdhutto at gmail.com Mon Oct 8 02:16:03 2012 From: dwightdhutto at gmail.com (Dwight Hutto) Date: Sun, 7 Oct 2012 20:16:03 -0400 Subject: [Tutor] modulo In-Reply-To: <50721911.4060005@davea.name> References: <50720B23.8090801@davea.name> <50720F26.9000200@davea.name> <4BC0A6C2-0494-44D9-8CFC-705D5DDE3579@me.com> <50721911.4060005@davea.name> Message-ID: ______________________________________________ >>> I'm not a professional programmer, so I might be way off base here. You mean you haven't dealt with this subject yet... But what I like about Pythons modulo solution is that I can use it to right and left shift in lists or tuples, and I will link to the first element when I right shift past the last element and link to the last element when I left shift past the first element. In other words I can consider the last as a chain where the last and the first element are connected. This I find useful in surprisingly many situations. It's uised for, what it's used for, until you know the full lower level implementation/parsing of objects like immutables(tuples), and mutables(lists,dicts,strings,etc >> >> > Certainly, but you've never had to do that with lists or tuples having > negative lengths. It's a negative modulus that I'm complaining about. Can you show some example code here? -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com From tharunichowdary at gmail.com Mon Oct 8 17:55:09 2012 From: tharunichowdary at gmail.com (Tharuni Dheeraj) Date: Mon, 8 Oct 2012 08:55:09 -0700 Subject: [Tutor] program for a problem Message-ID: please send me the program for the following que: Write a program that asks the user for a dollar amount.It then reports the corresponding number of euros by using the current exchange rate. -- Regards, Tharuni Dheeraj -------------- next part -------------- An HTML attachment was scrubbed... URL: From syedzaidi85 at hotmail.co.uk Sat Oct 13 13:02:20 2012 From: syedzaidi85 at hotmail.co.uk (syed zaidi) Date: Sat, 13 Oct 2012 12:02:20 +0100 Subject: [Tutor] Consecutive Sequence Message-ID: Hi,I am trying to develop a python code that takes a character string as input and finds for the occurrence of letters that are occurring thrice or more consecutively.For E.g. a = 'atttttaattaaacagagtgagcagaaaat'In the output I want a list of those characters that are occuring thrice or more. like in this case outout must b out_put = ['ttttt','aaa','aaaa'] Can someone please suggest a code for this. -------------- next part -------------- An HTML attachment was scrubbed... URL: From b.lee009 at yahoo.com Sat Oct 6 05:55:00 2012 From: b.lee009 at yahoo.com (Benjamin Lee) Date: Fri, 5 Oct 2012 20:55:00 -0700 (PDT) Subject: [Tutor] Python Error Message-ID: <1349495700.53670.YahooMailNeo@web121604.mail.ne1.yahoo.com> Hello, I am using python to calculate distances across the cell.? I used this command: ?python calculate_distances.py This was the response: Traceback (most recent call last): file "calculate_distances.py" line 4, in import celltool.simple_interface as si ImportError: No module named celltool.simple_ interface I am not sure how to correct this error. Thank you for your time, Ben -------------- next part -------------- An HTML attachment was scrubbed... URL: From jan9447 at me.com Mon Oct 8 02:13:14 2012 From: jan9447 at me.com (Jan Karel Schreuder) Date: Sun, 07 Oct 2012 20:13:14 -0400 Subject: [Tutor] modulo In-Reply-To: <50721911.4060005@davea.name> References: <50720B23.8090801@davea.name> <50720F26.9000200@davea.name> <4BC0A6C2-0494-44D9-8CFC-705D5DDE3579@me.com> <50721911.4060005@davea.name> Message-ID: On Oct 7, 2012, at 8:06 PM, Dave Angel wrote: > On 10/07/2012 08:00 PM, Jan Karel Schreuder wrote: >> >> >> On Oct 7, 2012, at 7:24 PM, Dave Angel wrote: >> >>>>>> >>>>> >>> >>> It still makes no sense to me. There are at least two equally silly >>> ways to define the results of a negative modulus, and you've properly >>> described one of them, presumably the one that Python implements. >>> >>> But I've used and abused about 35 languages over the years, and each >>> makes its own choice for this. I'd rather just call it undefined, and >>> eliminate it. That's what we did when the hardware guys couldn't decide >>> how the hardware was going to respond to a particular microcode bit >>> pattern. They documented it as undefined, and I made it illegal in the >>> microcode assembler. >>> >>> Fortunately, the OP isn't asking about this case, which is the other >>> reason I didn't bother to describe what Python does. >>> >>> >>> >>> -- >>> >>> DaveA >>> _______________________________________________ >>> I'm not a professional programmer, so I might be way off base here. But what I like about Pythons modulo solution is that I can use it to right and left shift in lists or tuples, and I will link to the first element when I right shift past the last element and link to the last element when I left shift past the first element. In other words I can consider the last as a chain where the last and the first element are connected. This I find useful in surprisingly many situations. >> >> > Certainly, but you've never had to do that with lists or tuples having > negative lengths. It's a negative modulus that I'm complaining about. > > > -- > > DaveA Aha. Yes I was talking about the solution to -3%5 and found the python solution (2) useful. I'm agnostic about x% -5 From sctjkh at googlemail.com Thu Oct 11 21:29:01 2012 From: sctjkh at googlemail.com (Stephen Hooker) Date: Thu, 11 Oct 2012 20:29:01 +0100 Subject: [Tutor] Python incrementing problem Message-ID: Hello, I'm using Python 2.6 to generate switch lists for a model railroad. I had some help before, possibly from this site but have encountered another problem. I realise the code could be better written, but learning Python is a work in progress. The program works much as expected except when I try to count & store the results of a random pick from a list of rolling stock. If I try to store 1 result it works fine. If I try to store more than 1 result I get one or the other, never both. The code is below (not the whole program) This works: for i in range(1): pos = random.randrange(0,len(rolling_stock_D)) pick = rolling_stock_D[pos] for item in noDupes: if item==pick: break else: # else for the loop, executed if the loop ran to exhaustion noDupes.append(pick) for item in noDupes: print item print if item.find ("SDB") != -1: SDB_count += 1 if item.find ("DDB") != -1: DDB_count += 1 -------------------------------------------------------------------- But this doesn't: for i in range(2): pos = random.randrange(0,len(rolling_stock_OS)) pick = rolling_stock_OS[pos] for item in noDupes: if item==pick: break else: # else for the loop, executed if the loop ran to exhaustion noDupes.append(pick) for item in noDupes: print item print if item.find ("Flat") != -1: Flat_count += 1 if item.find ("Gondola") != -1: Gondola_count += 1 Any help would be much appreciated thank you Steve -------------- next part -------------- An HTML attachment was scrubbed... URL: From googcheng at gmail.com Wed Oct 17 12:29:59 2012 From: googcheng at gmail.com (Cheng) Date: Wed, 17 Oct 2012 18:29:59 +0800 Subject: [Tutor] Tutor Digest, Vol 104, Issue 69 Message-ID: <32d6i6ratjuq00s3thn4smc6.1350469799994@email.android.com> tutor-request at python.org??? >Send Tutor mailing list submissions to > tutor at python.org > >To subscribe or unsubscribe via the World Wide Web, visit > http://mail.python.org/mailman/listinfo/tutor >or, via email, send a message with subject or body 'help' to > tutor-request at python.org > >You can reach the person managing the list at > tutor-owner at python.org > >When replying, please edit your Subject line so it is more specific >than "Re: Contents of Tutor digest..." > > >Today's Topics: > > 1. Re: CSV -> sqlite tables with foreign keys (eryksun) > 2. Re: Why difference between printing string & typing its > object reference at the prompt? (Dwight Hutto) > 3. Re: managing memory large dictionaries in python (Dwight Hutto) > 4. Re: modulo (Dwight Hutto) > 5. program for a problem (Tharuni Dheeraj) > 6. Consecutive Sequence (syed zaidi) > > >---------------------------------------------------------------------- > >Message: 1 >Date: Wed, 17 Oct 2012 05:19:27 -0400 >From: eryksun >To: Monte Milanuk >Cc: Tutor at python.org >Subject: Re: [Tutor] CSV -> sqlite tables with foreign keys >Message-ID: > >Content-Type: text/plain; charset=UTF-8 > >On Tue, Oct 16, 2012 at 11:59 PM, Monte Milanuk wrote: >> >> address = list(row) >> address.append(person) >> row = tuple(address) > >The rows from the csv.reader are already lists. Also, the parameter >list in the 2nd argument only needs to be a sequence (e.g. tuple, >list, string), or it can also be a dict if the statement uses named >placeholders. > >> from what I've found on the web, I get the distinct impression >> that converting from a tuple to a list and back is considered >> poor practice at best and generally to be avoided. > >To update a record in a tuple you can use slicing and concatenation >(+) as an alternative to creating a temporary list. A list is more >applicable to homogenous data (e.g. a list of tuples, each a data >record). If you want a container for a record that you can modify more >efficiently, use a dict or a custom object. For the latter, look into >ORMs such as Storm: > >https://storm.canonical.com/ > >> Any suggestions? > >I found a Stack Overflow answer that uses a table "view" combined with >an "instead of" trigger to update two tables with one insert. > >http://stackoverflow.com/a/11715983/205580 > >Here's my meager attempt at an adaptation (some names have been >changed to protect the innocent...): > > import csv > import sqlite3 > > con = sqlite3.connect(':memory:') > cur = con.cursor() > > cur.execute('''create table person ( > id integer primary key autoincrement, > firstname, midinit, lastname, birthdate) > ''') > > cur.execute('''create table address ( > id integer primary key autoincrement, > person_id integer references person not null, > street, city, state, zipcode) > ''') > > cur.execute('''create view person_view as > select > person.firstname, person.midinit, person.lastname, > person.birthdate, address.street, address.city, > address.state, address.zipcode > from > person inner join address on person.id = address.person_id > ''') > > cur.execute('''create trigger person_view_insert > instead of insert on person_view > begin > insert into > person (firstname, midinit, lastname, birthdate) > values (new.firstname, new.midinit, new.lastname, > new.birthdate); > insert into > address (person_id, street, city, state, zipcode) > values ((select last_insert_rowid()), > new.street, new.city, new.state, new.zipcode); > end > ''') > > import io > data = io.BytesIO(b'''\ > John,G.,Smith,1972-11-10,123 Any Place,Somewhere,Missouri,58932 > Jane,L.,Jones,1971-12-20,321 Some Place,Anywhere,Kansas,12345 > ''') > > reader = csv.reader(data) > for row in reader: > cur.execute('''insert into > person_view (firstname, midinit, lastname, birthdate, > street, city, state, zipcode) > values (?,?,?,?,?,?,?,?)''', row) > > # output > for row in cur.execute('select * from person'): > print row > for row in cur.execute('select * from address'): > print row > > >person table: > > (1, u'John', u'G.', u'Smith', u'1972-11-10') > (2, u'Jane', u'L.', u'Jones', u'1971-12-20') > >address table: > > (1, 1, u'123 Any Place', u'Somewhere', u'Missouri', u'58932') > (2, 2, u'321 Some Place', u'Anywhere', u'Kansas', u'12345') > > >------------------------------ > >Message: 2 >Date: Wed, 10 Oct 2012 21:58:39 -0400 >From: Dwight Hutto >To: "Steven D'Aprano" >Cc: tutor at python.org >Subject: Re: [Tutor] Why difference between printing string & typing > its object reference at the prompt? >Message-ID: > >Content-Type: text/plain; charset=ISO-8859-1 > >If your app has a standard usage of phrases, you can place a file in >that translates a tag into a particular language phrase. > > > >if submit_tag_selection == 'english': > submit = 'Submit' >if submit_tag_selection == 'english': > submit = 'Soumettre' > >Of course this could be done without the if, you would just translate >the normal selections within a file with the commonly used phrases in >the app, and substitute it within a parse for: > >x = open('translate_file_french', 'r') >for line in x: > if line.split('=')[0] == 'Submit': > print '%s' % (line.split('=')[1]) > >'Soumettre' > >*Untested, but should work > > >-- >Best Regards, >David Hutto >CEO: http://www.hitwebdevelopment.com > > >------------------------------ > >Message: 3 >Date: Tue, 16 Oct 2012 21:30:43 -0400 >From: Dwight Hutto >To: Abhishek Pratap >Cc: tutor at python.org >Subject: Re: [Tutor] managing memory large dictionaries in python >Message-ID: > >Content-Type: text/plain; charset=ISO-8859-1 > >On Tue, Oct 16, 2012 at 12:57 PM, Abhishek Pratap > wrote: >> Hi Guys >> >> For my problem I need to store 400-800 million 20 characters keys in a >> dictionary and do counting. This data structure takes about 60-100 Gb >> of RAM. >> I am wondering if there are slick ways to map the dictionary to a file >> on disk and not store it in memory but still access it as dictionary >> object. Speed is not the main concern in this problem and persistence >> is not needed as the counting will only be done once on the data. We >> want the script to run on smaller memory machines if possible. >> >> I did think about databases for this but intuitively it looks like a >> overkill coz for each key you have to first check whether it is >> already present and increase the count by 1 and if not then insert >> the key into dbase. >> >> Just want to take your opinion on this. >> >> Thanks! >> -Abhi >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > >My inexperienced advice would be to begin with the storage areas >available. I would begin by eliminating certain things such as: > >x = {'one_entry' : 1} > >into > >x = {'one_entry':1} > >To map, you would want maybe different db files that contain certain >info within a certain range. 0-1000 entries in the first file, etc. > >os.walk a directory, and find the mapped file in a particular range >file, then go straight to the entry needed. > >Make the dict one long line, and you could eliminate any /n newline chars. > > I could do better with more time, but that seems like a good solution >at this point. > >Best Regards, >David Hutto >CEO: http://www.hitwebdevelopment.com > > >------------------------------ > >Message: 4 >Date: Sun, 7 Oct 2012 20:16:03 -0400 >From: Dwight Hutto >To: d at davea.name >Cc: "tutor at python.org" , Jan Karel Schreuder > >Subject: Re: [Tutor] modulo >Message-ID: > >Content-Type: text/plain; charset=ISO-8859-1 > >______________________________________________ >>>> I'm not a professional programmer, so I might be way off base here. > >You mean you haven't dealt with this subject yet... > >But what I like about Pythons modulo solution is that I can use it to >right and left shift in lists or tuples, and I will link to the first >element when I right shift past the last element and link to the last >element when I left shift past the first element. In other words I can >consider the last as a chain where the last and the first element are >connected. This I find useful in surprisingly many situations. > >It's uised for, what it's used for, until you know the full lower >level implementation/parsing of objects like immutables(tuples), and >mutables(lists,dicts,strings,etc > >>> >>> >> Certainly, but you've never had to do that with lists or tuples having >> negative lengths. It's a negative modulus that I'm complaining about. > >Can you show some example code here? > >-- >Best Regards, >David Hutto >CEO: http://www.hitwebdevelopment.com > > >------------------------------ > >Message: 5 >Date: Mon, 8 Oct 2012 08:55:09 -0700 >From: Tharuni Dheeraj >To: tutor at python.org >Subject: [Tutor] program for a problem >Message-ID: > >Content-Type: text/plain; charset="iso-8859-1" > >please send me the program for the following que: > >Write a program that asks the user for a dollar amount.It then reports the >corresponding number of euros by using the current exchange rate. >-- >Regards, >Tharuni Dheeraj >-------------- next part -------------- >An HTML attachment was scrubbed... >URL: > >------------------------------ > >Message: 6 >Date: Sat, 13 Oct 2012 12:02:20 +0100 >From: syed zaidi >To: >Subject: [Tutor] Consecutive Sequence >Message-ID: >Content-Type: text/plain; charset="iso-8859-1" > > >Hi,I am trying to develop a python code that takes a character string as input and finds for the occurrence of letters that are occurring thrice or more consecutively.For E.g. >a = 'atttttaattaaacagagtgagcagaaaat'In the output I want a list of those characters that are occuring thrice or more. >like in this case outout must b out_put = ['ttttt','aaa','aaaa'] >Can someone please suggest a code for this. >-------------- next part -------------- >An HTML attachment was scrubbed... >URL: > >------------------------------ > >Subject: Digest Footer > >_______________________________________________ >Tutor maillist - Tutor at python.org >http://mail.python.org/mailman/listinfo/tutor > > >------------------------------ > >End of Tutor Digest, Vol 104, Issue 69 >************************************** From eryksun at gmail.com Wed Oct 17 13:12:04 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 17 Oct 2012 07:12:04 -0400 Subject: [Tutor] Python incrementing problem In-Reply-To: References: Message-ID: On Thu, Oct 11, 2012 at 3:29 PM, Stephen Hooker wrote: > > I try to store 1 result it works fine. If I try to store more than 1 result > I get one or the other, never both. The code is below (not the whole > program) > > for i in range(2): > pos = random.randrange(0,len(rolling_stock_OS)) > pick = rolling_stock_OS[pos] > for item in noDupes: > if item==pick: > break > else: # else for the loop, executed if the loop ran to exhaustion > noDupes.append(pick) The formatting was lost, but it seems you're trying to use two for loops to do something that's easier with a single while loop, and even easier if you take a random.sample() of a set(): http://docs.python.org/release/2.6.7/library/random.html#random.sample >>> import random >>> data = [3,1,4,5,9,3,1,4,5,9] >>> random.sample(set(data), 5) [9, 3, 5, 4, 1] From steve at pearwood.info Wed Oct 17 13:19:29 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 17 Oct 2012 22:19:29 +1100 Subject: [Tutor] Why VPython can't be searched out in PyPI? In-Reply-To: <2012101716234618151118@126.com> References: <2012101716234618151118@126.com> Message-ID: <507E9441.2020807@pearwood.info> On 17/10/12 19:23, Dae James wrote: > I found that VPython is not in PyPI(python packet index from >www.python.org). Why ? You'd have to ask the author of VPython. Being on PyPI is not compulsory, nobody is going to force him to use PyPI if he doesn't want to. -- Steven From breamoreboy at yahoo.co.uk Wed Oct 17 13:28:26 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 17 Oct 2012 12:28:26 +0100 Subject: [Tutor] program for a problem In-Reply-To: References: Message-ID: On 08/10/2012 16:55, Tharuni Dheeraj wrote: > please send me the program for the following que: > > Write a program that asks the user for a dollar amount.It then reports the > corresponding number of euros by using the current exchange rate. > -- > Regards, > Tharuni Dheeraj > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Please send me a cheque for ?1000 sterling and I'll send you the program. -- Cheers. Mark Lawrence. From steve at pearwood.info Wed Oct 17 13:42:30 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 17 Oct 2012 22:42:30 +1100 Subject: [Tutor] managing memory large dictionaries in python In-Reply-To: References: Message-ID: <507E99A6.5010606@pearwood.info> On 17/10/12 12:30, Dwight Hutto wrote: > My inexperienced advice would be to begin with the storage areas > available. I would begin by eliminating certain things such as: > > x = {'one_entry' : 1} > > into > > x = {'one_entry':1} Those two entries are exactly the same. The presence of absence of spaces, or newlines, in the dict literal makes no difference to the amount of memory that the dict will use. -- Steven From d at davea.name Wed Oct 17 14:02:58 2012 From: d at davea.name (Dave Angel) Date: Wed, 17 Oct 2012 08:02:58 -0400 Subject: [Tutor] Consecutive Sequence In-Reply-To: References: Message-ID: <507E9E72.2020802@davea.name> On 10/13/2012 07:02 AM, syed zaidi wrote: > Hi,I am trying to develop a python code that takes a character string as input and finds for the occurrence of letters that are occurring thrice or more consecutively.For E.g. > a = 'atttttaattaaacagagtgagcagaaaat'In the output I want a list of those characters that are occuring thrice or more. > like in this case outout must b out_put = ['ttttt','aaa','aaaa'] > Can someone please suggest a code for this. > Is this a homework assignment where you might be expected to build a complex loop, or is it a problem you're solving where you would be allowed to use itertools.groupby ? -- DaveA From d at davea.name Wed Oct 17 14:12:37 2012 From: d at davea.name (Dave Angel) Date: Wed, 17 Oct 2012 08:12:37 -0400 Subject: [Tutor] Python Error In-Reply-To: <1349495700.53670.YahooMailNeo@web121604.mail.ne1.yahoo.com> References: <1349495700.53670.YahooMailNeo@web121604.mail.ne1.yahoo.com> Message-ID: <507EA0B5.2020600@davea.name> On 10/05/2012 11:55 PM, Benjamin Lee wrote: > Hello, > I am using python to calculate distances across the cell. > I used this command: python calculate_distances.py > This was the response: > Traceback (most recent call last): > file "calculate_distances.py" line 4, in > import celltool.simple_interface as si > ImportError: No module named celltool.simple_ interface > > > I am not sure how to correct this error. > Thank you for your time, > Ben > celltool is not part of Python. You tell us nothing about yourself or your system. Did you write this code (calculate_distances.py), or you trying to run someone else's script? If you downloaded it from somewhere, look there to see what its dependencies are. If you think you've installed the 'celltool" package, you'll need to check where it ended up, and whether that place is on your system.path. -- DaveA From eryksun at gmail.com Wed Oct 17 15:43:27 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 17 Oct 2012 09:43:27 -0400 Subject: [Tutor] Consecutive Sequence In-Reply-To: <507E9E72.2020802@davea.name> References: <507E9E72.2020802@davea.name> Message-ID: On Wed, Oct 17, 2012 at 8:02 AM, Dave Angel wrote: > > Is this a homework assignment where you might be expected to build a > complex loop, or is it a problem you're solving where you would be > allowed to use itertools.groupby ? May as well post a groupby solution. I doubt it would be accepted for homework: >>> seq = 'atttttaattaaacagagtgagcagaaaat' >>> groups = (''.join(g) for _, g in groupby(seq)) >>> [g for g in groups if len(g) > 2] ['ttttt', 'aaa', 'aaaa'] groupby() yields (key, _grouper) tuples for each group in an iterable. The default key function is lambda x: x. The _grouper objects share a common iterator, so they need to be used in the order of creation. Typically they're used immediately as the data pipes through. In the generator expression, each _grouper g is joined into a string. The list comprehension keeps strings of length greater than 2. Alternatively, you can use a regular expression: >>> [m.group() for m in re.finditer(r'(\w)\1{2,}', seq, re.U)] ['ttttt', 'aaa', 'aaaa'] \w matches an alphanumeric character or the underscore (the flag re.U expands this to Unicode). The parentheses (round brackets) mark group 1, which is the single character matched by \w. Next, this group is referenced with \1 with a repetition of {2,} (at least twice), for a total of 3 or more consecutive occurrences. From memilanuk at gmail.com Wed Oct 17 16:32:51 2012 From: memilanuk at gmail.com (Monte Milanuk) Date: Wed, 17 Oct 2012 07:32:51 -0700 Subject: [Tutor] CSV -> sqlite tables with foreign keys In-Reply-To: References: Message-ID: Thanks for the help! Not sure why I assumed that csv.reader was returning row as a tuple instead of a list... that makes that part easier ;) As for the 'INSTEAD OF' trigger on a VIEW... that *does* look pretty handy. I was trying to remember why I hadn't heard of that before, or why I hadn't looked into it. I think it has to do with MySQL not supporting triggers on views, at least not 'INSTEAD OF'. Right now I'm using sqlite, but at some point I may need to work with MySQL as well. It's getting kind of frustrating how many little things it doesn't support... -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Wed Oct 17 19:29:16 2012 From: bgailer at gmail.com (bob gailer) Date: Wed, 17 Oct 2012 13:29:16 -0400 Subject: [Tutor] program for a problem In-Reply-To: References: Message-ID: <507EEAEC.8010403@gmail.com> On 10/8/2012 11:55 AM, Tharuni Dheeraj wrote: > please send me the program for the following que: > > Write a program that asks the user for a dollar amount.It then reports > the corresponding number of euros by using the current exchange rate. As the list name (Tutor) suggests we are here to help you as you make effort and run into problems. Mark's response indicates that we will write code for you for pay. If you want help, show us what you've done and where you are stuck. Is this a homework assignment? How is one to obtain the "current exchange rate"? -- Bob Gailer 919-636-4239 Chapel Hill NC From alan.gauld at btinternet.com Wed Oct 17 20:20:25 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 17 Oct 2012 19:20:25 +0100 Subject: [Tutor] Tutor Digest, Vol 104, Issue 69 In-Reply-To: <32d6i6ratjuq00s3thn4smc6.1350469799994@email.android.com> References: <32d6i6ratjuq00s3thn4smc6.1350469799994@email.android.com> Message-ID: On 17/10/12 11:29, Cheng wrote: I couldn't see any comment or question in there. When posting could you please follow the instructions: >> >> When replying, please edit your Subject line so it is more specific >> than "Re: Contents of Tutor digest..." >> And also delete any irrelevant content. It makes it much easier for us to see what you are discussing/asking. Oh yes, and while I'm at it, please post any comments after the context (in-line posting) it makes long threads much easier to follow. Thanks, -- Alan G List moderator. From xchimeras at gmail.com Wed Oct 17 21:51:04 2012 From: xchimeras at gmail.com (Mike) Date: Wed, 17 Oct 2012 15:51:04 -0400 Subject: [Tutor] Thread question Message-ID: Hello, First, I apologize for not providing a code sample (it's on my work PC) as I have a general threading logic question. I am using Python 2.6 and the threading module (I can't upgrade to a newer version of Python). My program is command line based and is driven by the user making a selection from the menu. Basically option 1 perform a HTTP GET request to xyz.com/?get=test. The user supply's a list of target domains and the query (?get=test) is static. The list of targets can be in the thousands, so I thought threads with queues would be the best route. When my program starts I start 50 worker threads and 50 display threads. The worker threads wait for data in the input queue and handle the request. If a error is hit the data is logged in the errors queue. This works great if the user selects option 1, but I am running into problems when a user selects multiple options. Say a user selection 1 then option (2) with the targets being the same, but the query string is now ?get=check. The problem I am facing is when the threads run with option 1 and hit the initialization they are set to ?get=test i.e. self.param=param. The param is passed in to the init of the thread class. When I initialize the class from option 1 I pass in the param ?get=test and when I initialize the class with option 2 I pass in the param ?get=check. The default initialization is self.param="". Depending on which option they select first the thread will be one of the following: blank, or one of the two options. I don't know how to update the thread variable when it's running i.e.def run(self). I realize this sounds all confusing, but I am basically wondering how one would update a thread variable when it is running? Also, is it best to spawn threads each time the user make a selection or once when the program starts? I appreciate any feedback. Thank you, Mike. From steve at pearwood.info Thu Oct 18 01:37:38 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 18 Oct 2012 10:37:38 +1100 Subject: [Tutor] Thread question In-Reply-To: References: Message-ID: <507F4142.40103@pearwood.info> Mike, A brief comment before I start: your post would be a lot more readable and understandable if you were to break it up into smaller paragraphs, each of which explains one point. Otherwise it is a great wall of text which is fairly intimidating. On 18/10/12 06:51, Mike wrote: > My program is command line based and is driven by the user making a > selection from the menu. Basically option 1 perform a HTTP GET request > to xyz.com/?get=test. The user supply's a list of target domains and > the query (?get=test) is static. No it isn't. You go on to explain below that there are at least two different queries: ?get=test ?get=check and presumably no query at all (when param=''). Since the query is not static, but part of the request, it should be part of the data pushed onto the queue. Instead of queuing just the domains, queue a tuple (domain, query) or even the full URL. > This works great if the user selects option 1, but I am > running into problems when a user selects multiple options. Say a > user selection 1 then option (2) with the targets being the same, but > the query string is now ?get=check. The problem I am facing is when > the threads run with option 1 and hit the initialization they are set > to ?get=test i.e. self.param=param. The param is passed in to the init > of the thread class. When I initialize the class from option 1 I pass > in the param ?get=test and when I initialize the class with option 2 I > pass in the param ?get=check. The default initialization is > self.param="". Why is the query an attribute of the *thread*? The query is an attribute of the request, not the thread. Change your design, and the problem goes away. > Depending on which option they select first the thread > will be one of the following: blank, or one of the two options. I > don't know how to update the thread variable when it's running i.e.def > run(self). mythread.param = new_value but please don't do this. The design is wrong, you have wrongly assumed that the param is a part of the thread when it is actually a part of the request. > I realize this sounds all confusing, but I am basically > wondering how one would update a thread variable when it is running? > Also, is it best to spawn threads each time the user make a selection > or once when the program starts? I would say, neither. It is best to spawn threads when they click the "Make it go" button. Decide how many threads you need, spawn them, and let them run. -- Steven From robertvstepp at gmail.com Thu Oct 18 05:41:39 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 17 Oct 2012 22:41:39 -0500 Subject: [Tutor] Objects, object references, object values and memory addresses Message-ID: >From Programming in Python 3, 2nd edition (p. 22-23): >>> a = ["Retention", 3, None] >>> b = ["Retention", 3, None] >>> a is b False >>> b = a >>> a is b True My current understanding is as follows: On the first two lines, two separate objects are defined, stored in two separate blocks of memory. These two objects just happen to have the same value, ["Retention", 3, None], stored in two separate locations. a and b, the object references (Variables are what I used to call these.), store these two separate memory locations. Thus a is b is false. However, when the line b = a is implemented, b now references the same object (memory location) as a, which now causes a is b to be true. Is my understanding correct? On the next page the author states (after giving a string example where a and b each are assigned the string "many paths", similar to the example above): "In some cases, comparing the identity of two strings or numbers--for example, using a is b--will return True, even if each has been assigned separately as we did here. This is because some implementations of Python will reuse the same object (since the value is the same and is immutable) for the sake of efficiency..." I ask: Which implementations of Python do this? In trying to make any code I write portable across as many platforms as possible, should I avoid using the identity operator, is (and its opposite, is not), except when I wish to compare to None? -- Thanks! boB From d at davea.name Thu Oct 18 07:16:18 2012 From: d at davea.name (Dave Angel) Date: Thu, 18 Oct 2012 01:16:18 -0400 Subject: [Tutor] Objects, object references, object values and memory addresses In-Reply-To: References: Message-ID: <507F90A2.90409@davea.name> On 10/17/2012 11:41 PM, boB Stepp wrote: > >From Programming in Python 3, 2nd edition (p. 22-23): > >>>> a = ["Retention", 3, None] >>>> b = ["Retention", 3, None] >>>> a is b > False >>>> b = a >>>> a is b > True > > My current understanding is as follows: On the first two lines, two > separate objects are defined, stored in two separate blocks of memory. > These two objects just happen to have the same value, ["Retention", 3, > None], stored in two separate locations. a and b, the object > references (Variables are what I used to call these.), store these two > separate memory locations. Thus a is b is false. However, when the > line b = a is implemented, b now references the same object (memory > location) as a, which now causes a is b to be true. Is my > understanding correct? You are correct, subject to an amendment. Using the term memory addresses implies a particular implementation. CPython happens to work that way, in its current implementation. Jython happens not to. No biggie. a and b are simply bound to two different objects, and it's that difference that causes a false result. When they're bound to the same object, you get a true result. > > On the next page the author states (after giving a string example > where a and b each are assigned the string "many paths", similar to > the example above): > > "In some cases, comparing the identity of two strings or numbers--for > example, using a is b--will return True, even if each has been > assigned separately as we did here. This is because some > implementations of Python will reuse the same object (since the value > is the same and is immutable) for the sake of efficiency..." > > I ask: Which implementations of Python do this? In trying to make any > code I write portable across as many platforms as possible, should I > avoid using the identity operator, is (and its opposite, is not), > except when I wish to compare to None? > Not just comparing to None, but to any singleton object. if you're sure that you have only one instance of a particular object, for whatever reason, then it's safe to use 'is' to distinguish this object from any other object. Which implementations do this? Any of them, starting with CPython, which is probably the one you're using. Note that mutable objects that are different must be distinct, so each time you create one you'll get a unique object. It's only for immutable objects that the implementation may decide to reuse existing objects. This includes, ints, floats, strings, byte strings, tuples, etc. In the particular case of CPython, small integers are cached in this way, and so are short strings with no whitespace. How small, and exactly which strings is irrelevant to me, and hopefully to you. The point is you cannot be sure whether equal immutable objects are really just a single one, or not. If you only care about value, then definitely use == or its variants. If you are comparing against a singleton, then go ahead and use 'is'. Otherwise, beware, and expect the unexpected. -- DaveA From dangulko at hotmail.com Thu Oct 18 09:08:08 2012 From: dangulko at hotmail.com (Daniel Gulko) Date: Thu, 18 Oct 2012 07:08:08 +0000 Subject: [Tutor] Help Passing Variables Message-ID: Hi Python Tutor, I have a write a simple function named "SwapCaseAndCenter(a_string, width). The idea is to use the function swapcase and center so that when the userenters a string it centers it and swaps the case (e.g. upper to lower and vice versa). The function calls for passing in two variables "a_string, width" but I am still confused on this concept. In my code below I seem to be able to pass in the variable "a_string" and found out how to use the "center along with swapcase" functions. I am still unsure howto pass in the variable "width". In my code I have hard coded the centering but I am not sure if instead I use the variable width to determine the centering. Any suggestions, help or examples of how to do this is appreciated. def SwapCaseAndCenter(a_string): while True: a_string=raw_input("give me a word: (enter to quit): ") if a_string: print a_string.center(60).swapcase() elif not a_string: print "you did not provide a word. goodbye" break SwapCaseAndCenter(a_string) Thanks, Dan -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Oct 18 09:32:41 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 18 Oct 2012 08:32:41 +0100 Subject: [Tutor] Objects, object references, object values and memory addresses In-Reply-To: References: Message-ID: On 18/10/12 04:41, boB Stepp wrote: > From Programming in Python 3, 2nd edition (p. 22-23): > >>>> a = ["Retention", 3, None] >>>> b = ["Retention", 3, None] >>>> a is b > False >>>> b = a >>>> a is b > True > > My current understanding is as follows: On the first two lines, two > separate objects are defined, stored in two separate blocks of memory. Two separate list objects are created. In Python it is rarely helpful to think about memory usage. Python is abstracted so far from the physical machine that the connection usually depends on the creator of the interpreter rather than the language. > These two objects just happen to have the same value, ["Retention", 3, > None], stored in two separate locations. Yes, but note that its the fact that they are two separate lists that matters. Even if the contents were the same objects they would still be two lists: >>> x = [42] >>> y = 'spam' >>> z = None >>> a = [x,y,z] >>> b = [x,y,z] # different list, exact same content >>> a is b False >>> a == b True >>> a = b >>> a is b True >>> a == b True >>> > a and b, object references (Variables are what I used to call these. And most folks still do... > I ask: Which implementations of Python do this? In trying to make any > code I write portable across as many platforms as possible, should I > avoid using the identity operator, is (and its opposite, is not), > except when I wish to compare to None? The point is that you don't know. And, even if you did, the very next release might change it so you cannot rely on it. That's the real message - do not assume a particular implementation technique because it is not guaranteed to be that way or to stay that way. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu Oct 18 09:46:36 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 18 Oct 2012 08:46:36 +0100 Subject: [Tutor] Help Passing Variables In-Reply-To: References: Message-ID: On 18/10/12 08:08, Daniel Gulko wrote: > The function calls for passing in two variables "a_string, width" but I > am still confused on this concept. You just provide the list of input parameters when you define the function: >>> def add(x,y): ... return x+y ... >>> add(4,5) 9 I define add to take two parameters x and y. I then supply two corresponding arguments, 4,5 when I call add. I then use x and y inside my function (x+y) like ordinary variables. > am still unsure how to pass in the variable "width". add it to your function definition as I did for add() Then add a width value to the call to your function > centering but I am not sure if instead I use the variable width to > determine the centering. Yes, or being pedantic, you use the parameter 'width' in your call to centre() > def SwapCaseAndCenter(a_string): > while True: > a_string=raw_input("give me a word: (enter to quit): ") > if a_string: > print a_string.center(60).swapcase() > elif not a_string: > print "you did not provide a word. goodbye" > break BTW putting break here means your loop only ever executes once. Frankly I would take the loop out and prompt the user for a_string outside the function and pass the string in along with the width. I'd also return the modified string so the caller can print it. Also rather than use the elif line I'd change it to an else. The test is implied by the fact it failed the initial if test. Either a_string is True (it exists) or it is not True, there is no third option so you don't need a separate elif test. If you really want to prompt the user multiple times do that outside the function: while True: my_string=raw_input("give me a word: (enter to quit): ") if my_string: print SwapCaseAndCenter(my_string,60) else: print "you did not provide a word. goodbye" break HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eryksun at gmail.com Thu Oct 18 10:23:58 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 18 Oct 2012 04:23:58 -0400 Subject: [Tutor] Objects, object references, object values and memory addresses In-Reply-To: <507F90A2.90409@davea.name> References: <507F90A2.90409@davea.name> Message-ID: On Thu, Oct 18, 2012 at 1:16 AM, Dave Angel wrote: > > may decide to reuse existing objects. This includes, ints, floats, > strings, byte strings, tuples, etc. In the particular case of CPython, > small integers are cached in this way, and so are short strings with no > whitespace. How small, and exactly which strings is irrelevant to me, > and hopefully to you. The point is you cannot be sure whether equal > immutable objects are really just a single one, or not. In case anyone is curious about implementation trivia, CPython (2.7.3 and 3.2.3) caches integers in the closed range -5 to 256. This doesn't apply to the long() type in 2.x. Strings for attributes, and various other cases, are interned in a dict. code objects intern a limited set of string constants (alphanumeric/underscore ASCII) and also all referenced names (co_names, co_varnames, co_freevars, and co_cellvars). The built-in function intern() (moved to sys.intern in 3.x) manually interns a string, e.g. for a small efficiency gain with dict lookups: http://docs.python.org/py3k/library/sys#sys.intern http://docs.python.org/library/functions.html#intern To give a feel for how much string interning is used, here's the summary after importing ctypes in a fresh interpreter and releasing the interned strings: 3.2.3: >>> import ctypes >>> ctypes.pythonapi._Py_ReleaseInternedUnicodeStrings() releasing 3996 interned strings total size of all interned strings: 34193/0 mortal/immortal 2.7.3: >>> import ctypes >>> ctypes.pythonapi._Py_ReleaseInternedStrings() releasing 2875 interned strings total size of all interned strings: 26389/0 mortal/immortal From d at davea.name Thu Oct 18 11:28:23 2012 From: d at davea.name (Dave Angel) Date: Thu, 18 Oct 2012 05:28:23 -0400 Subject: [Tutor] Help Passing Variables In-Reply-To: References: Message-ID: <507FCBB7.1050306@davea.name> On 10/18/2012 03:08 AM, Daniel Gulko wrote: > > > > Hi Python Tutor, I have a write a simple function named "SwapCaseAndCenter(a_string, width). So why did you define it with only one formal parameter? > The idea is to use the function swapcase and center so that when the userenters a string it centers it and swaps the case (e.g. upper to lower and vice versa). The function calls for passing in two variables "a_string, width" but I am still confused on this concept. In my code below I seem to be able to pass in the variable "a_string" and found out how to use the "center along with swapcase" functions. I am still unsure howto pass in the variable "width". In my code I have hard coded the centering but I am not sure if instead I use the variable width to determine the centering. Any suggestions, help or examples of how to do this is appreciated. > def SwapCaseAndCenter(a_string): while True: As you can see, your email program thoroughly messed up this source code. Please send your messages in text form, as html frequently gets trashed as it goes through various gateways. This is a text-mailing list. > a_string=raw_input("give me a word: (enter to quit): ") > Why do you ask the user for the input, when it was already supplied as a parameter? it's best practice to separate input from calculation, and your teacher had done that in his/her specification. > if a_string: > print a_string.center(60).swapcase() > elif not a_string: > print "you did not provide a word. goodbye" Presumably you should be returning the centered/swapped string. No idea where the following break is supposed to happen. But once you eliminate the raw_input, you'll be eliminating the while True and the break. > break SwapCaseAndCenter(a_string) > Thanks, Dan -- DaveA From s.charonis at gmail.com Thu Oct 18 14:01:59 2012 From: s.charonis at gmail.com (Spyros Charonis) Date: Thu, 18 Oct 2012 13:01:59 +0100 Subject: [Tutor] indexing a list Message-ID: Hello pythoners, I have a string that I want to read in fixed-length windows. In [68]: SEQ Out[68]: 'MKAAVLTLAVLFLTGSQARHFWQQDEPPQSPWDRVKDLATVYVDVLKDSGRDYVSQFEGSALGKQLNLKLLDNWDSVTSTFSKLREQLGPVTQEFWDNLEKETEGLRQEMSKDLEEVKAKVQPYLDDFQKKWQEEMELYRQKVEPLRAELQEGARQKLHELQEKLSPLGEEMRDRARAHVDALRTHLAPYSDELRQRLAARLEALKENGGARLAEYHAKATEHLSTLSEKAKPALEDLRQ' I would like a function that reads the above string, 21 characters at a time, and checks for certain conditions, i.e. whether characters co-occur in other lists I have made. For example: x = 21 # WINDOW LENGTH In [70]: SEQ[0:x] Out[70]: 'MKAAVLTLAVLFLTGSQARHF' In [71]: SEQ[x:2*x] Out[71]: 'WQQDEPPQSPWDRVKDLATVY' In [72]: SEQ[2*x:3*x] Out[72]: 'VDVLKDSGRDYVSQFEGSALG' How could I write a function to automate this so that it does this from SEQ[0] throughout the entire sequence, i.e. until len(SEQ)? Many thanks for your time, Spyros -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Thu Oct 18 15:08:48 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 18 Oct 2012 09:08:48 -0400 Subject: [Tutor] indexing a list In-Reply-To: References: Message-ID: On Thu, Oct 18, 2012 at 8:01 AM, Spyros Charonis wrote: > > x = 21 # WINDOW LENGTH > > In [70]: SEQ[0:x] > Out[70]: 'MKAAVLTLAVLFLTGSQARHF' > > In [71]: SEQ[x:2*x] > Out[71]: 'WQQDEPPQSPWDRVKDLATVY' > > In [72]: SEQ[2*x:3*x] > Out[72]: 'VDVLKDSGRDYVSQFEGSALG' > > How could I write a function to automate this so that it does this from > SEQ[0] throughout the entire sequence, i.e. until len(SEQ)? In your examples, the lower slice limit is 0x, 1x, 2x, and so on. The upper limit is 1x, 2x, 3x, and so on. That should scream that you need a counter, or range/xrange. The lower limit of the last slice should be less than len(SEQ), such that there's at least 1 item in the last slice. So, in terms of range, the start value is 0, and the stop value is len(SEQ). To make things even simpler, range takes an optional step size. This gives you the 0x, 1x, 2x, etc for the start index of each slice. The upper bound is then i+x (corresponding to 1x, 2x, 3x, etc). For example: >>> seq = 'MKAAVLTLAVLFLTGSQARHFWQQDEPPQSPWDRVKDLATVYVDVLK' >>> x = 21 >>> for i in range(0, len(seq), x): ... print(seq[i:i+x]) ... MKAAVLTLAVLFLTGSQARHF WQQDEPPQSPWDRVKDLATVY VDVLK If you're using Python 2.x, use xrange instead of range, and "print" is a statement instead of a function. You can also use a generator expression to create a one-time iterable object that can be used in another generator, a for loop, a comprehension, or as the argument of a function that expects an iterable, such as the list() constructor: >>> chunks = (seq[i:i+x] for i in range(0, len(seq), x)) >>> list(chunks) ['MKAAVLTLAVLFLTGSQARHF', 'WQQDEPPQSPWDRVKDLATVY', 'VDVLK'] From ryan.waples at gmail.com Thu Oct 18 19:38:57 2012 From: ryan.waples at gmail.com (Ryan Waples) Date: Thu, 18 Oct 2012 10:38:57 -0700 Subject: [Tutor] Using the set.difference method with an unknown number of input iterables Message-ID: I'm struggling to understand how to understand/accomplish the following: I have an set ("a" below) and a list of sets ("not_a"), how can I pass the elements of "not_a" to set.difference() so that it it understands I want the difference between set "a" and all the rest set.difference says "Changed in version 2.6: Accepts multiple input iterables". How can I give it multiple input iterables? I get different error msgs depending on what I try, but they just tell me that there is something that I'm missing here. Thanks #Code below a = set([1,2,3,4]) b = set([2,3,4,5]) c = set([3,4,5,6]) d = set([4,5,6,7]) not_a = [b,c,d] a.difference(not_a) # I expect to return set([1]), the same as if I called: a.difference(b,c,d) -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Fri Oct 19 07:52:52 2012 From: emile at fenx.com (Emile van Sebille) Date: Thu, 18 Oct 2012 22:52:52 -0700 Subject: [Tutor] Using the set.difference method with an unknown number of input iterables In-Reply-To: References: Message-ID: On 10/18/2012 10:38 AM, Ryan Waples wrote:> I'm struggling to understand how to understand/accomplish the following: > > I have an set ("a" below) and a list of sets ("not_a"), how can I pass > the elements of "not_a" to set.difference() so that it it understands I > want the difference between set "a" and all the rest > > set.difference says "Changed in version 2.6: Accepts multiple input > iterables". > How can I give it multiple input iterables? > > I get different error msgs depending on what I try, but they just tell > me that there is something that I'm missing here. > > Thanks > > #Code below > a = set([1,2,3,4]) > b = set([2,3,4,5]) > c = set([3,4,5,6]) > d = set([4,5,6,7]) > > not_a = [b,c,d] > a.difference(not_a) Try this as a.difference(*not_a) The '*' expands the list to its individual items. HTH, Emile > > # I expect to return set([1]), the same as if I called: > a.difference(b,c,d) > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From ryan.waples at gmail.com Thu Oct 18 19:56:22 2012 From: ryan.waples at gmail.com (Ryan Waples) Date: Thu, 18 Oct 2012 10:56:22 -0700 Subject: [Tutor] Using the set.difference method with an unknown number of input iterables In-Reply-To: References: Message-ID: cheers, much appreciated -ryan On Thu, Oct 18, 2012 at 10:52 PM, Emile van Sebille wrote: > On 10/18/2012 10:38 AM, Ryan Waples wrote:> I'm struggling to understand > how to understand/accomplish the following: > > > > > I have an set ("a" below) and a list of sets ("not_a"), how can I pass > > the elements of "not_a" to set.difference() so that it it understands I > > want the difference between set "a" and all the rest > > > > set.difference says "Changed in version 2.6: Accepts multiple input > > iterables". > > How can I give it multiple input iterables? > > > > I get different error msgs depending on what I try, but they just tell > > me that there is something that I'm missing here. > > > > Thanks > > > > #Code below > > a = set([1,2,3,4]) > > b = set([2,3,4,5]) > > c = set([3,4,5,6]) > > d = set([4,5,6,7]) > > > > not_a = [b,c,d] > > a.difference(not_a) > > Try this as > > a.difference(*not_a) > > The '*' expands the list to its individual items. > > HTH, > > Emile > > > > > > > # I expect to return set([1]), the same as if I called: > > a.difference(b,c,d) > > > > > > > > > > ______________________________**_________________ > > 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 > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ramit.prasad at jpmorgan.com Thu Oct 18 20:22:40 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Thu, 18 Oct 2012 18:22:40 +0000 Subject: [Tutor] Why difference between printing string & typing its object reference at the prompt? In-Reply-To: References: <20121003063843.GB27111@ando> <506D72E0.60402@pearwood.info> <50762463.8070300@pearwood.info> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741671CF31@SCACMX008.exchad.jpmchase.net> David Hutto wrote: > If your app has a standard usage of phrases, you can place a file in > that translates a tag into a particular language phrase. > > > > if submit_tag_selection == 'english': > submit = 'Submit' > if submit_tag_selection == 'english': > submit = 'Soumettre' > > Of course this could be done without the if, you would just translate > the normal selections within a file with the commonly used phrases in > the app, and substitute it within a parse for: > > x = open('translate_file_french', 'r') > for line in x: > if line.split('=')[0] == 'Submit': > print '%s' % (line.split('=')[1]) > > 'Soumettre' > > *Untested, but should work > Now missing any context I am going to assume the topic shifted to how to do translations for a internationalized application/site. Feel free to ignore if I am wrong or OT. I would do this, but not using line splitting. I would create a (YAML) config files that contain translations of site text (i.e. "Submit"). You could do this with pickle too, but I think YAML files are better for humans to edit. text = '........' with open('translate_file_fr') as f: # parse YAML into dictionary of { text_to_replace : text_to_replace_with } # for k,v in translation_key.iteritems(): text = text.replace(k, v) Alternately you could create a python module and just import the appropriate language. translation_key = __import__('translation.' + language ) text = ''join([ '...', translation_key.submit_button_text, '...']) Of course, I have no idea the downsides of this approach as I have not had to do something like this before. I would be interested in whether this matches the standard approach and the up/down-sides to it. Ramit Prasad 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 walksloud at gmail.com Fri Oct 19 00:30:50 2012 From: walksloud at gmail.com (Andre' Walker-Loud) Date: Thu, 18 Oct 2012 15:30:50 -0700 Subject: [Tutor] good coding habits/practices Message-ID: <20657B39-DF7B-4500-B9DC-569C20D30C2F@gmail.com> Hi All, I have a general question. I have never taken a formal programming course. I have learned everything I know from office mates (earlier on) and now mostly Google, as I need it. Most everything I write is only for my own consumption. I write scripts/programs to do science research, and have never manage to make the time to take a proper class, or even work through a good python tutorial. On a matplotlib thread, someone pointed us to a PyCon talk posted on YouTube [for those interested - link below] http://www.youtube.com/watch?v=pXhcPJK5cMc This particular talk focussed on argument passing in python. I found it very enjoyable and educational. One thing I learned - I am really not aware of the coding standards out there. For example, I was not aware precisely of the argument passing standards discussed in the above talk. However, I am aware that if I were aware of these things, my ability to do my research would be improved. Especially, just being more familiar with proper programming. Also, just because of personal principles, even if my code is only ever really consumed by me, I would like it to meet higher standards of "good code" than it currently does. However, since I am still hunting for a permanent job, I can not take the time for a full proper course on programming [I still need to use the learn as I need it model]. So finally the question: is there a good SUCCINCT guide to things like the POSIX standards, and other programming standards {IEEE ... I believe), and a guide to good programming practices that I can peruse on the web to try and improve my programming skills as I work? Also - recommended (shorter) tutorials would be nice. With my current work schedule, things that can be worked through on the order of an hour or so [each lesson that is]. Thanks, Andre From alan.gauld at btinternet.com Fri Oct 19 01:17:41 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 19 Oct 2012 00:17:41 +0100 Subject: [Tutor] good coding habits/practices In-Reply-To: <20657B39-DF7B-4500-B9DC-569C20D30C2F@gmail.com> References: <20657B39-DF7B-4500-B9DC-569C20D30C2F@gmail.com> Message-ID: On 18/10/12 23:30, Andre' Walker-Loud wrote: > So finally the question: is there a good SUCCINCT guide to things > like the POSIX standards, > and other programming standards {IEEE ... I believe) The problem is there are so many of these, and some of them are hundreds of pages long in their own right. Its impossible to do a succinct guide to all of them. There might be succinct guides to particular standards but not to all, or even most of them. > a guide to good programming practices that I can peruse > on the web to try and improve my programming skills as I work? I don;t know if these are on the web but they are all short books - try your local library for a loan... Programming Pearls (vol1 & 2) The practice of Programming The pragmatic programmer And finally, not small, but definitely one of the best all-in-one guides: Code Complete > things that can be worked through on the order of an hour or so The first 3 all fit that. Code Complete is more comprehensive and the chapters are longer. But its more like doing a college course on programming practice(note not theory!) If you want something that will help with the art of programming design try www.htdp.org (also in book form) And http://mitpress.mit.edu/sicp/full-text/book/book.html for pure theory. I believe MIT still use this as their standard text. These last two are in Scheme but the principles translate to any language. Those are my favourites once you get past the basics. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From rdmoores at gmail.com Fri Oct 19 02:01:20 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 18 Oct 2012 17:01:20 -0700 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz Message-ID: Python 3.2.3 64 bit MS Windows 7 Home Premium 64-bit SP1 I see python-dateutil recommended here from time to time, so I thought I'd try it out. I downloaded python-dateutil-2.1.tar.gz from http://pypi.python.org/pypi/python-dateutil but have forgotten how to unpack a .tar.gz file. Please remind me. Thanks, Dick Moores From breamoreboy at yahoo.co.uk Fri Oct 19 02:24:08 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 19 Oct 2012 01:24:08 +0100 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: References: Message-ID: On 19/10/2012 01:01, Richard D. Moores wrote: > Python 3.2.3 64 bit > MS Windows 7 Home Premium 64-bit SP1 > > I see python-dateutil recommended here from time to time, so I thought > I'd try it out. I downloaded python-dateutil-2.1.tar.gz from > http://pypi.python.org/pypi/python-dateutil but have forgotten how to > unpack a .tar.gz file. Please remind me. > > Thanks, > > Dick Moores > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > It's easier on Windows to use pip or easy_install from the command line rather than mess around with unpacking .tar.gz files, especially if it involves having to compile anything as opposed to having a pre-built binary. -- Cheers. Mark Lawrence. From alan.gauld at btinternet.com Fri Oct 19 02:27:12 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 19 Oct 2012 01:27:12 +0100 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: References: Message-ID: On 19/10/12 01:01, Richard D. Moores wrote: > Python 3.2.3 64 bit > MS Windows 7 Home Premium 64-bit SP1 > ... > unpack a .tar.gz file. Please remind me. Winzip or similar should cope in Windows. But a tar.gz is usually intended for *nix so unless its pure python it may not work in Windows. You might want to check for a Windows installer just in case... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From breamoreboy at yahoo.co.uk Fri Oct 19 02:28:44 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 19 Oct 2012 01:28:44 +0100 Subject: [Tutor] good coding habits/practices In-Reply-To: <20657B39-DF7B-4500-B9DC-569C20D30C2F@gmail.com> References: <20657B39-DF7B-4500-B9DC-569C20D30C2F@gmail.com> Message-ID: On 18/10/2012 23:30, Andre' Walker-Loud wrote: > Hi All, > > I have a general question. I have never taken a formal programming course. I have learned everything I know from office mates (earlier on) and now mostly Google, as I need it. Most everything I write is only for my own consumption. I write scripts/programs to do science research, and have never manage to make the time to take a proper class, or even work through a good python tutorial. > > On a matplotlib thread, someone pointed us to a PyCon talk posted on YouTube [for those interested - link below] > > http://www.youtube.com/watch?v=pXhcPJK5cMc > > This particular talk focussed on argument passing in python. I found it very enjoyable and educational. > > One thing I learned - I am really not aware of the coding standards out there. For example, I was not aware precisely of the argument passing standards discussed in the above talk. > > However, I am aware that if I were aware of these things, my ability to do my research would be improved. Especially, just being more familiar with proper programming. Also, just because of personal principles, even if my code is only ever really consumed by me, I would like it to meet higher standards of "good code" than it currently does. However, since I am still hunting for a permanent job, I can not take the time for a full proper course on programming [I still need to use the learn as I need it model]. > > So finally the question: is there a good SUCCINCT guide to things like the POSIX standards, and other programming standards {IEEE ... I believe), and a guide to good programming practices that I can peruse on the web to try and improve my programming skills as I work? > > Also - recommended (shorter) tutorials would be nice. > With my current work schedule, things that can be worked through on the order of an hour or so [each lesson that is]. > > > Thanks, > > Andre > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Given the subject I suggest you take a look at PEPs 7 and 8. -- Cheers. Mark Lawrence. From rdmoores at gmail.com Fri Oct 19 02:42:31 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 18 Oct 2012 17:42:31 -0700 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: References: Message-ID: On Thu, Oct 18, 2012 at 5:27 PM, Alan Gauld wrote: > On 19/10/12 01:01, Richard D. Moores wrote: >> >> Python 3.2.3 64 bit >> MS Windows 7 Home Premium 64-bit SP1 >> ... >> >> unpack a .tar.gz file. Please remind me. > > > Winzip or similar should cope in Windows. But a tar.gz is usually intended > for *nix so unless its pure python it may not work in Windows. You might > want to check for a Windows installer just > in case... Looking at it's not clear that there is a Windows installer. Does someone know of one? Dick From rdmoores at gmail.com Fri Oct 19 03:04:10 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 18 Oct 2012 18:04:10 -0700 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: References: Message-ID: On Thu, Oct 18, 2012 at 5:27 PM, Alan Gauld wrote: > On 19/10/12 01:01, Richard D. Moores wrote: >> >> Python 3.2.3 64 bit >> MS Windows 7 Home Premium 64-bit SP1 >> ... >> >> unpack a .tar.gz file. Please remind me. By trying with Peazip I got it unpacked. Here's what the PKG-INFO file says, when opened in WordPad: ========================================= Metadata-Version: 1.1 Name: python-dateutil Version: 2.1 Summary: Extensions to the standard Python datetime module Home-page: http://labix.org/python-dateutil Author: Tomi Pievilaeinen Author-email: tomi.pievilainen at iki.fi License: Simplified BSD Description: The dateutil module provides powerful extensions to the datetime module available in the Python standard library. Platform: UNKNOWN Requires: six ======================================== Dick From breamoreboy at yahoo.co.uk Fri Oct 19 03:08:23 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 19 Oct 2012 02:08:23 +0100 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: References: Message-ID: On 19/10/2012 01:42, Richard D. Moores wrote: > On Thu, Oct 18, 2012 at 5:27 PM, Alan Gauld wrote: >> On 19/10/12 01:01, Richard D. Moores wrote: >>> >>> Python 3.2.3 64 bit >>> MS Windows 7 Home Premium 64-bit SP1 >>> ... >>> >>> unpack a .tar.gz file. Please remind me. >> >> >> Winzip or similar should cope in Windows. But a tar.gz is usually intended >> for *nix so unless its pure python it may not work in Windows. You might >> want to check for a Windows installer just >> in case... > > Looking at > it's not clear that there is a Windows installer. Does someone know of > one? > > Dick > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Could you please take a training course on how to use a search engine. First hit on google for "python dateutils install windows" is http://stackoverflow.com/questions/879156/how-to-install-python-dateutil-on-windows -- Cheers. Mark Lawrence. From rdmoores at gmail.com Fri Oct 19 03:16:35 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 18 Oct 2012 18:16:35 -0700 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: References: Message-ID: Running the example at , with Python 2.3.2 64-bit, The importing goes OK, it seems, but: Python 3.2.3 (default, Apr 11 2012, 07:12:16) [MSC v.1500 64 bit (AMD64)] Type "help", "copyright", "credits" or "license" for more information. [evaluate untitled-1.py] Traceback (most recent call last): File "C:\Program Files (x86)\Wing IDE 4.1\src\debug\tserver\_sandbox.py", line 9, in File "c:\Python32\Lib\site-packages\dateutil\parser.py", line 720, in parse return DEFAULTPARSER.parse(timestr, **kwargs) File "c:\Python32\Lib\site-packages\dateutil\parser.py", line 310, in parse raise ValueError("unknown string format") builtins.ValueError: unknown string format Dick From rdmoores at gmail.com Fri Oct 19 03:55:30 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 18 Oct 2012 18:55:30 -0700 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: References: Message-ID: On Thu, Oct 18, 2012 at 6:08 PM, Mark Lawrence wrote: > Could you please take a training course on how to use a search engine. First > hit on google for "python dateutils install windows" is > http://stackoverflow.com/questions/879156/how-to-install-python-dateutil-on-windows You're assuming I haven't searched and\or don't know how to. Still stuck. 2 problems it seems. Your link is 3 years old. It applies to python 2.6. Dick From robertvstepp at gmail.com Fri Oct 19 04:11:53 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Thu, 18 Oct 2012 21:11:53 -0500 Subject: [Tutor] IDLE shell indentation? Message-ID: >>> if zero: print(zero) else: print(phrase) Wild Swans by Jung Chang >>> Is there some special way for typing in multiline blocks of code into the shell in IDLE? The above works, but it bothers me that "else" does not line up with "if". Also, in the IDLE shell the "p" in "print" actually lines up under the "e" in "zero". However when I copied from IDLE and pasted into gmail, this alignment was changed. I am guessing it is because gmail is not using a fixed-width font. And I have yet to figure out how to get it to use one when in plaintext mode. Thanks! boB From steve at pearwood.info Fri Oct 19 04:17:58 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 19 Oct 2012 13:17:58 +1100 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: References: Message-ID: <5080B856.3060005@pearwood.info> On 19/10/12 12:16, Richard D. Moores wrote: > Running the example at > , > with Python 2.3.2 64-bit, Python TWO POINT THREE??? :-) That's like, a million years old. I think you mean three point two. > The importing goes OK, it seems, but: > > Python 3.2.3 (default, Apr 11 2012, 07:12:16) [MSC v.1500 64 bit (AMD64)] > Type "help", "copyright", "credits" or "license" for more information. > [evaluate untitled-1.py] What does that mean? What's "evaluate" in this context? What's inside "untitled.py"? You're hiding the code you actually run, and expecting us to debug it unseen. Not cool. You've been on this list long enough that you should know better. Check your code for typos. If the error persists, check it again. If it still persists, show us the actual relevant code. More below. > Traceback (most recent call last): > File "C:\Program Files (x86)\Wing IDE > 4.1\src\debug\tserver\_sandbox.py", line 9, in > File "c:\Python32\Lib\site-packages\dateutil\parser.py", line 720, in parse > return DEFAULTPARSER.parse(timestr, **kwargs) > File "c:\Python32\Lib\site-packages\dateutil\parser.py", line 310, in parse > raise ValueError("unknown string format") > builtins.ValueError: unknown string format What is the value of timestr? I note that text just before the demo code states: [quote] you want to get today's date out of the "date" unix system command. [end quote] Are you running Unix or a Unix-compatible system like Linux? Here's what `date` outputs under Unix/Linux: [steve at ando ~]$ date Fri Oct 19 13:16:40 EST 2012 What does it output on your computer? -- Steven From eryksun at gmail.com Fri Oct 19 04:21:59 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 18 Oct 2012 22:21:59 -0400 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: References: Message-ID: On Thu, Oct 18, 2012 at 9:55 PM, Richard D. Moores wrote: > On Thu, Oct 18, 2012 at 6:08 PM, Mark Lawrence wrote: > >> Could you please take a training course on how to use a search engine. First >> hit on google for "python dateutils install windows" is >> http://stackoverflow.com/questions/879156/how-to-install-python-dateutil-on-windows > > You're assuming I haven't searched and\or don't know how to. > > Still stuck. 2 problems it seems. Your link is 3 years old. It applies > to python 2.6. For Python 3, install it with Distribute (a fork of Setuptools): http://pypi.python.org/pypi/distribute/0.6.28 Installation script: http://python-distribute.org/distribute_setup.py That site seems to be down as I write this. Here's the source on Bitbucket for the 0.6.28 release: https://bitbucket.org/tarek/distribute/raw/fc379e63586ad3c6838e1bda216548ba8270b8f0/distribute_setup.py short url: http://goo.gl/R1JNJ From eryksun at gmail.com Fri Oct 19 04:46:02 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 18 Oct 2012 22:46:02 -0400 Subject: [Tutor] IDLE shell indentation? In-Reply-To: References: Message-ID: On Thu, Oct 18, 2012 at 10:11 PM, boB Stepp wrote: >>>> if zero: > print(zero) > else: > print(phrase) > > Is there some special way for typing in multiline blocks of code into > the shell in IDLE? The above works, but it bothers me that "else" does > not line up with "if". Also, in the IDLE shell the "p" in "print" > actually lines up under the "e" in "zero". However when I copied from > IDLE and pasted into gmail, this alignment was changed. I am guessing > it is because gmail is not using a fixed-width font. And I have yet to > figure out how to get it to use one when in plaintext mode. You're right that the alignment changed because of the font rendering of the tab character. In Firefox I use the following to get a monospace font in Gmail: http://userstyles.org/styles/15618/gmail-monospace-font-for-body-messages-textarea As to your main question, I use exec: >>> zero, phrase = '', 'spam' >>> exec(''' if zero: print(zero) else: print(phrase) ''') spam From eryksun at gmail.com Fri Oct 19 04:57:26 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 18 Oct 2012 22:57:26 -0400 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: References: Message-ID: On Thu, Oct 18, 2012 at 8:01 PM, Richard D. Moores wrote: > > Python 3.2.3 64 bit > MS Windows 7 Home Premium 64-bit SP1 > .... > have forgotten how to unpack a .tar.gz file. Please remind me. shutil.unpack_archive(filename, extract_dir, 'gztar') http://docs.python.org/release/3.2.3/library/shutil.html#shutil.unpack_archive From robertvstepp at gmail.com Fri Oct 19 05:23:41 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Thu, 18 Oct 2012 22:23:41 -0500 Subject: [Tutor] IDLE shell indentation? In-Reply-To: References: Message-ID: On Thu, Oct 18, 2012 at 9:46 PM, eryksun wrote: > You're right that the alignment changed because of the font rendering > of the tab character. In Firefox I use the following to get a > monospace font in Gmail: > > http://userstyles.org/styles/15618/gmail-monospace-font-for-body-messages-textarea It did not occur to me to search outside of the gmail/chrome help as I felt sure that monospace capability had to be built-in! Thanks for pointing me elsewhere. Now all is swell. > As to your main question, I use exec: > > >>> zero, phrase = '', 'spam' > >>> exec(''' > if zero: > print(zero) > else: > print(phrase) > ''') > spam This does keep the alignment I desire; however, IDLE shell's autoindent feature goes away inside the exec function. Further, my tab, which is set to 4 spaces, becomes 8 spaces at the indent; apparently the tab takes effect after where ">>> " would normally occur. Of course I can just manually type the four spaces. Alas! Perfection here is apparently unattainable! ~(:>)) boB From rdmoores at gmail.com Fri Oct 19 05:30:45 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 18 Oct 2012 20:30:45 -0700 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: <5080B856.3060005@pearwood.info> References: <5080B856.3060005@pearwood.info> Message-ID: On Thu, Oct 18, 2012 at 7:17 PM, Steven D'Aprano wrote: > On 19/10/12 12:16, Richard D. Moores wrote: >> >> Running the example at >> >> , >> with Python 2.3.2 64-bit, > > > Python TWO POINT THREE??? :-) > > That's like, a million years old. I think you mean three point two. 3.2.3 > > > >> The importing goes OK, it seems, but: >> >> Python 3.2.3 (default, Apr 11 2012, 07:12:16) [MSC v.1500 64 bit (AMD64)] >> Type "help", "copyright", "credits" or "license" for more information. >> [evaluate untitled-1.py] > > > What does that mean? > > What's "evaluate" in this context? What's inside "untitled.py"? Using my IDE, Wing 4.1, I opened a new file and made an exact copy-and-paste into it, and accepted the default filename, "untitled.py". "evaluate" is one of the ways of running a script in Wing, "Evaluate file in Python shell". I already said: Running the example at >> Traceback (most recent call last): >> File "C:\Program Files (x86)\Wing IDE >> 4.1\src\debug\tserver\_sandbox.py", line 9, in >> File "c:\Python32\Lib\site-packages\dateutil\parser.py", line 720, in >> parse >> return DEFAULTPARSER.parse(timestr, **kwargs) >> File "c:\Python32\Lib\site-packages\dateutil\parser.py", line 310, in >> parse >> raise ValueError("unknown string format") >> builtins.ValueError: unknown string format > > I note that text just before the demo code states: > > [quote] > you want to get today's date out of the "date" unix system command. > [end quote] I missed that. It's the first clue I could find anywhere that the download for 3.x (x > 0) is only for a "Unix or a Unix-compatible system like Linux". > Are you running Unix or a Unix-compatible system like Linux? No. See my original post. > Here's what > `date` > outputs under Unix/Linux: > > [steve at ando ~]$ date > Fri Oct 19 13:16:40 EST 2012 > > > What does it output on your computer? I showed you everything already. Dick From steve at pearwood.info Fri Oct 19 05:44:51 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 19 Oct 2012 14:44:51 +1100 Subject: [Tutor] IDLE shell indentation? In-Reply-To: References: Message-ID: <5080CCB3.7050909@pearwood.info> On 19/10/12 14:23, boB Stepp wrote: > This does keep the alignment I desire; however, IDLE shell's > autoindent feature goes away inside the exec function. Further, my > tab, which is set to 4 spaces, becomes 8 spaces at the indent; > apparently the tab takes effect after where ">>> " would normally > occur. Of course I can just manually type the four spaces. Alas! > Perfection here is apparently unattainable! ~(:>)) If you want a decent IDE for Python, run, don't walk, run away from IDLE. Under Linux, the vanilla Python interactive interpreter plus a few basic command line commands is *much* better than IDLE. IPython is even more powerful, and still easy to use. http://ipython.org/ Or try bpython: http://bpython-interpreter.org/ Or try ActiveState's commercial Python install, which includes a free IDE: http://www.activestate.com/activepython/downloads In my opinion, anyone using IDLE under Linux probably does it because they like pain, and anyone using it under Windows does so only because they don't know of the alternatives. -- Steven From steve at pearwood.info Fri Oct 19 05:55:08 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 19 Oct 2012 14:55:08 +1100 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: References: <5080B856.3060005@pearwood.info> Message-ID: <5080CF1C.4040802@pearwood.info> On 19/10/12 14:30, Richard D. Moores wrote: >> [quote] >> you want to get today's date out of the "date" unix system command. >> [end quote] > > I missed that. It's the first clue I could find anywhere that the > download for 3.x (x> 0) is only for a "Unix or a Unix-compatible > system like Linux". No, you have misunderstood. The *date* command, as shown, is a unix system command. But dateutils is not. The relevant lines from the demo code are: import commands now = parse(commands.getoutput("date")) which uses the commands module to send the command "date" to the operating system, run it, and get the result, and only then gets dateutil to parse the text. You can replace those lines with: now = parse("Fri Oct 19 13:16:40 EST 2012") and I expect it will work. >> Here's what `date` outputs under Unix/Linux: >> >> [steve at ando ~]$ date >> Fri Oct 19 13:16:40 EST 2012 >> >> >> What does it output on your computer? > > I showed you everything already. Have you tried running `date` at the Windows command.com (or cmd.exe, or something, I never remember which)? What does it print? My guess is that it probably prints something like: "Command not found" which clearly cannot be parsed as a date. -- Steven From eryksun at gmail.com Fri Oct 19 06:41:49 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 19 Oct 2012 00:41:49 -0400 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: <5080CF1C.4040802@pearwood.info> References: <5080B856.3060005@pearwood.info> <5080CF1C.4040802@pearwood.info> Message-ID: On Thu, Oct 18, 2012 at 11:55 PM, Steven D'Aprano wrote: > > Have you tried running `date` at the Windows command.com (or cmd.exe, > or something, I never remember which)? What does it print? > > My guess is that it probably prints something like: > > "Command not found" > > which clearly cannot be parsed as a date. Windows has separate date and time commands ('date /t' and 'time /t'), but it's simpler to use 'echo %time% %date%' in the shell. Also, the demo script isn't for Python 3.x. It uses "print" as a statement and the "commands" module, which is deprecated in 2.x and removed from 3.x. Try this instead: import sys import os import subprocess from dateutil.relativedelta import * from dateutil.easter import * from dateutil.rrule import * from dateutil.parser import * from datetime import * if sys.platform == 'win32': cmd = 'echo %time% %date%' shell = True else: cmd = 'date' shell = False datestr = subprocess.check_output(cmd, shell=shell).decode() now = parse(datestr) today = now.date() year = rrule(YEARLY,bymonth=8,bymonthday=13,byweekday=FR)[0].year rdelta = relativedelta(easter(year), today) print("Today is:", today) print("Year with next Aug 13th on a Friday is:", year) print("How far is the Easter of that year:", rdelta) print("And the Easter of that year is:", today+rdelta) From eryksun at gmail.com Fri Oct 19 07:51:42 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 19 Oct 2012 01:51:42 -0400 Subject: [Tutor] IDLE shell indentation? In-Reply-To: References: Message-ID: On Thu, Oct 18, 2012 at 11:23 PM, boB Stepp wrote: > > This does keep the alignment I desire; however, IDLE shell's > autoindent feature goes away inside the exec function. Further, my > tab, which is set to 4 spaces, becomes 8 spaces at the indent; > apparently the tab takes effect after where ">>> " would normally > occur. Of course I can just manually type the four spaces. Alas! > Perfection here is apparently unattainable! ~(:>)) The shell in IDLE doesn't replace tabs with spaces for me. I hadn't considered the loss of auto-indenting. I don't use IDLE, and only use exec() like I showed when pasting an arbitrary selection of code into a terminal. IPython's Qt console does auto-indenting and replaces tabs with 4 spaces: In [1]: zero, phrase = '', 'spam' In [2]: if zero: ...: print(zero) ...: else: ...: print(phrase) ...: spam http://ipython.org/ipython-doc/dev/interactive/qtconsole.html Also, IEP is a nice little IDE that I used to use. I think it's a vast improvement over IDLE. It works simultaneously with different interpreters (2.x, 3.x, pypy) and GUI toolkits. http://code.google.com/p/iep From rdmoores at gmail.com Fri Oct 19 08:03:14 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 18 Oct 2012 23:03:14 -0700 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: References: <5080B856.3060005@pearwood.info> <5080CF1C.4040802@pearwood.info> Message-ID: On Thu, Oct 18, 2012 at 9:41 PM, eryksun wrote: > On Thu, Oct 18, 2012 at 11:55 PM, Steven D'Aprano wrote: >> >> Have you tried running `date` at the Windows command.com (or cmd.exe, >> or something, I never remember which)? What does it print? >> >> My guess is that it probably prints something like: >> >> "Command not found" >> >> which clearly cannot be parsed as a date. > > Windows has separate date and time commands ('date /t' and 'time /t'), > but it's simpler to use 'echo %time% %date%' in the shell. > > Also, the demo script isn't for Python 3.x. It uses "print" as a > statement and the "commands" module, which is deprecated in 2.x and > removed from 3.x. > > Try this instead: > > import sys > import os > import subprocess > from dateutil.relativedelta import * > from dateutil.easter import * > from dateutil.rrule import * > from dateutil.parser import * > from datetime import * > > if sys.platform == 'win32': > cmd = 'echo %time% %date%' > shell = True > else: > cmd = 'date' > shell = False > datestr = subprocess.check_output(cmd, shell=shell).decode() > > now = parse(datestr) > today = now.date() > year = rrule(YEARLY,bymonth=8,bymonthday=13,byweekday=FR)[0].year > rdelta = relativedelta(easter(year), today) > print("Today is:", today) > print("Year with next Aug 13th on a Friday is:", year) > print("How far is the Easter of that year:", rdelta) > print("And the Easter of that year is:", today+rdelta) Yes! Thanks! Dick From alan.gauld at btinternet.com Fri Oct 19 09:00:59 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 19 Oct 2012 08:00:59 +0100 Subject: [Tutor] IDLE shell indentation? In-Reply-To: References: Message-ID: On 19/10/12 03:11, boB Stepp wrote: >>>> if zero: > print(zero) > else: > print(phrase) This is a long standing bug/feature of IDLE and gets debated regularly on the IDLE mailing list. BTW There is a more feature rich unofficial version of Idle called IdleX by Roger Serwy here: http://www.ews.illinois.edu/~serwy/idlex/ which has: * Terminal-like behavior for PyShell. The cursor is confined to the prompt and up/down arrow keys navigate the command history, similar to the original Python interpreter. * Matplotlib support for interactive figures when using the subprocess. * Tabbed editor windows with drag'n'drop reordering. * SubCodes, similar to Matlab cell mode and Sagemath cells, for quick code prototyping without restarting the shell. * Integrated reindent.py support. * Improved code navigation with Code Browser. * Cython editing and execution support. (Version 0.15.1) * Line numbers for the Editor. * Clear PyShell Window without restarting PyShell. * Simple interface for enabling/disabling extensions. * SearchBar, Squeezer, and IDLE2HTML included. (Originally by Tal Einat, Noam Raphael, and Michael Haubenwallner) But it still has the broken indentation. The last thread I saw on the topic said: --------------------- On 06/21/2012 03:39 AM, Alan Gauld wrote: > On 21/06/12 02:16, Roger Serwy wrote: > >> I could write a patch to allow ps2 prompts if there's any serious interest. > > I would love that. > > This is one of the most common issues I get from users of my web tutorial. At least one email per month from beginners confused by IDLEs indentation. There is an issue for supporting ps1 and ps2. See http://bugs.python.org/issue13657 I'll start writing a patch later today. ... ----------------------- -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From breamoreboy at yahoo.co.uk Fri Oct 19 10:55:05 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 19 Oct 2012 09:55:05 +0100 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: References: Message-ID: On 19/10/2012 02:55, Richard D. Moores wrote: > On Thu, Oct 18, 2012 at 6:08 PM, Mark Lawrence wrote: > >> Could you please take a training course on how to use a search engine. First >> hit on google for "python dateutils install windows" is >> http://stackoverflow.com/questions/879156/how-to-install-python-dateutil-on-windows > > You're assuming I haven't searched and\or don't know how to. If you don't provide data people have to assume, guess or whatever. > > Still stuck. 2 problems it seems. Your link is 3 years old. It applies > to python 2.6. So what? Why should the way that Python software gets installed vary from version to version? > > Dick > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Cheers. Mark Lawrence. From rdmoores at gmail.com Fri Oct 19 11:53:48 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Fri, 19 Oct 2012 02:53:48 -0700 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: References: Message-ID: On Fri, Oct 19, 2012 at 1:55 AM, Mark Lawrence wrote: > On 19/10/2012 02:55, Richard D. Moores wrote: >> >> On Thu, Oct 18, 2012 at 6:08 PM, Mark Lawrence >> wrote: >> >>> Could you please take a training course on how to use a search engine. >>> First >>> hit on google for "python dateutils install windows" is >>> >>> http://stackoverflow.com/questions/879156/how-to-install-python-dateutil-on-windows >> >> >> You're assuming I haven't searched and\or don't know how to. > > > If you don't provide data people have to assume, guess or whatever. No need to rush into snarkiness, however. >> Still stuck. 2 problems it seems. Your link is 3 years old. It applies >> to python 2.6. > > So what? Why should the way that Python software gets installed vary from > version to version? Try it yourself and you'll see. From eryksun at gmail.com Fri Oct 19 12:47:23 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 19 Oct 2012 06:47:23 -0400 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: References: Message-ID: On Fri, Oct 19, 2012 at 5:53 AM, Richard D. Moores wrote: > On Fri, Oct 19, 2012 at 1:55 AM, Mark Lawrence wrote: >> >> So what? Why should the way that Python software gets installed vary from >> version to version? > > Try it yourself and you'll see. The setup.py needs setuptools as per jcoon's answer. For 3.x you need to install Distribute, which is a fork of setuptools. The line "from setuptools import setup" extensively patches distutils. It then honors the option 'install_requires = ["six"]' to fetch and install the dependency (this dependency is new to version 2.1) when you run "setup.py install". Alternatively, once you have Distribute installed, you can add the Scripts directory to the Windows PATH and just "easy_install python-dateutil" in an elevated console. From rdmoores at gmail.com Fri Oct 19 12:57:52 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Fri, 19 Oct 2012 03:57:52 -0700 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: References: Message-ID: On Fri, Oct 19, 2012 at 3:47 AM, eryksun wrote: > On Fri, Oct 19, 2012 at 5:53 AM, Richard D. Moores wrote: >> On Fri, Oct 19, 2012 at 1:55 AM, Mark Lawrence wrote: >>> >>> So what? Why should the way that Python software gets installed vary from >>> version to version? >> >> Try it yourself and you'll see. > > The setup.py needs setuptools as per jcoon's answer. For 3.x you need > to install Distribute, which is a fork of setuptools. The line "from > setuptools import setup" extensively patches distutils. It then honors > the option 'install_requires = ["six"]' to fetch and install the > dependency (this dependency is new to version 2.1) when you run > "setup.py install". Alternatively, once you have Distribute installed, > you can add the Scripts directory to the Windows PATH and just > "easy_install python-dateutil" in an elevated console. Yes. Thanks to your earlier post I was able to do the installation. Dick From dangulko at hotmail.com Fri Oct 19 17:40:15 2012 From: dangulko at hotmail.com (Daniel Gulko) Date: Fri, 19 Oct 2012 15:40:15 +0000 Subject: [Tutor] Help Passing Variables In-Reply-To: References: , , , , , Message-ID: Thanks David. This has been helpful in understanding a bit more on how parameters are passed through. > Date: Thu, 18 Oct 2012 04:44:55 -0400 > Subject: Re: [Tutor] Help Passing Variables > From: dwightdhutto at gmail.com > To: dangulko at hotmail.com > CC: tutor at python.org > > #A little more complex in terms of params: > > def SwapCaseAndCenter(*kwargs): > > if upper_or_lower == "upper": > print a_string.center(center_num).upper() > > if upper_or_lower == "lower": > print a_string.center(center_num).lower() > > a_string = raw_input("Give me a word, or letter: ") > upper_or_lower = raw_input("upper, or lower character(s): ") > center_num = int(raw_input("Where should number be centered?: ")) > SwapCaseAndCenter(a_string, upper_or_lower, center_num) > > > > -- > Best Regards, > David Hutto > CEO: http://www.hitwebdevelopment.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From dangulko at hotmail.com Fri Oct 19 17:42:39 2012 From: dangulko at hotmail.com (Daniel Gulko) Date: Fri, 19 Oct 2012 15:42:39 +0000 Subject: [Tutor] Objects, object references, object values and memory addresses In-Reply-To: References: , Message-ID: Great explanation Alan. I am a newbie at Python but descriptions like this really help me better understand. Thanks again. > To: tutor at python.org > From: alan.gauld at btinternet.com > Date: Thu, 18 Oct 2012 08:32:41 +0100 > Subject: Re: [Tutor] Objects, object references, object values and memory addresses > > On 18/10/12 04:41, boB Stepp wrote: > > From Programming in Python 3, 2nd edition (p. 22-23): > > > >>>> a = ["Retention", 3, None] > >>>> b = ["Retention", 3, None] > >>>> a is b > > False > >>>> b = a > >>>> a is b > > True > > > > My current understanding is as follows: On the first two lines, two > > separate objects are defined, stored in two separate blocks of memory. > > Two separate list objects are created. In Python it is rarely helpful to > think about memory usage. Python is abstracted so far from the physical > machine that the connection usually depends on the creator of the > interpreter rather than the language. > > > These two objects just happen to have the same value, ["Retention", 3, > > None], stored in two separate locations. > > Yes, but note that its the fact that they are two separate lists that > matters. Even if the contents were the same objects they would still be > two lists: > > >>> x = [42] > >>> y = 'spam' > >>> z = None > >>> a = [x,y,z] > >>> b = [x,y,z] # different list, exact same content > >>> a is b > False > >>> a == b > True > >>> a = b > >>> a is b > True > >>> a == b > True > >>> > > > a and b, object references (Variables are what I used to call these. > > And most folks still do... > > > I ask: Which implementations of Python do this? In trying to make any > > code I write portable across as many platforms as possible, should I > > avoid using the identity operator, is (and its opposite, is not), > > except when I wish to compare to None? > > The point is that you don't know. And, even if you did, the very next > release might change it so you cannot rely on it. That's the real > message - do not assume a particular implementation technique because it > is not guaranteed to be that way or to stay that way. > > -- > 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 Fri Oct 19 18:12:20 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 19 Oct 2012 16:12:20 +0000 Subject: [Tutor] How to unpack python-dateutil-2.0.tar.gz In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF4741671E179@SCACMX008.exchad.jpmchase.net> eryksun wrote: > On Thu, Oct 18, 2012 at 8:01 PM, Richard D. Moores wrote: > > > > Python 3.2.3 64 bit > > MS Windows 7 Home Premium 64-bit SP1 > > .... > > have forgotten how to unpack a .tar.gz file. Please remind me. > > shutil.unpack_archive(filename, extract_dir, 'gztar') > > http://docs.python.org/release/3.2.3/library/shutil.html#shutil.unpack_archive Neat! I usually just use `tar xzvf filename.tar.gz`. Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From d at davea.name Fri Oct 19 23:42:10 2012 From: d at davea.name (Dave Angel) Date: Fri, 19 Oct 2012 17:42:10 -0400 Subject: [Tutor] Help Passing Variables In-Reply-To: References: , , , , , Message-ID: <5081C932.2030205@davea.name> On 10/19/2012 11:40 AM, Daniel Gulko wrote: > Thanks David. This has been helpful in understanding a bit more on how parameters are passed through. Please don't top-post. it ruins the sequence of events. Your comments above happened after the parts you quote below. So why are they not after the things they follow? There is a long-standing convention in this forum, and many others, and why let Microsoft ruin it for all of us? >> Date: Thu, 18 Oct 2012 04:44:55 -0400 >> Subject: Re: [Tutor] Help Passing Variables >> From: dwightdhutto at gmail.com >> To: dangulko at hotmail.com >> CC: tutor at python.org >> >> #A little more complex in terms of params: >> >> def SwapCaseAndCenter(*kwargs): By convention kwargs is used for keyword arguments, while the * means positional arguments. The function uses none of them, so it's all bogus. You can't learn anything useful about argument passing from this example code, except what NOT to do. >> if upper_or_lower == "upper": >> print a_string.center(center_num).upper() >> >> if upper_or_lower == "lower": >> print a_string.center(center_num).lower() >> >> To understand the most elementary aspects of function passing, let's write a whole new function, and call it a few times. Paste this into a file, and try various things, and make sure you see how they work. Or reread Alan's email, which was also correct and clear. First point, a simple function may take zero arguments, or one, or two, or fifty. We'll start with positional arguments, which means order matters. Python also supports keyword arguments, default values, and methods, none of which I'll cover here. When you define a function, you specify its formal parameters. You do that by putting the parameter names inside the parentheses. Unlike most languages, you do NOT specify the types of any of these. But the order matters, and the number matters. So if we define a function: def truncate_string(a_string, width): temp = a_string[:width] return temp That function needs to be called with two arguments, which match the two formal parameters. They do NOT have to have the same names, and in fact they might well be literal strings or ints, or variables with string and int values. print truncate_string("this is a long string", 4) should print out "this" (without the quotes, of course) name = raw_input("give me a name") trunc_name = truncate_string(name, 8) print trunc_name truncate_string("aaa") #gives error, because it has the wrong number of arguments Does this help? -- DaveA From mmistroni at gmail.com Sat Oct 20 00:05:58 2012 From: mmistroni at gmail.com (Marco Mistroni) Date: Fri, 19 Oct 2012 23:05:58 +0100 Subject: [Tutor] Segmentation Fault shenanigans with wx Gui Message-ID: Hi all i have written a wx GUI which downloads json data from a server, and populate a listbox. Every time i populate a listbox, i am receiving Segmentation Faults. I have tried to retrieve data from the URL via separate thread, and to use events, but i am still getting a segmentation fault could anyone assist pls? here's relevant code. I m running python on Ubuntu 10.04 class WxSharesGUI(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(300, 300)) self.InitUI() def InitUI(self): self.load_button = wx.Button(self, wx.ID_ANY, "load ListBox") self.clear_button = wx.Button(self, wx.ID_ANY, "clear ListBox") self.load_button.Bind(wx.EVT_BUTTON, self.load_buttonClick) self.clear_button.Bind(wx.EVT_BUTTON, self.clear_buttonClick) self.Bind(EVT_RESULT, self.OnResult) self.lock = threading.RLock() # Creating Menu bar menubar = wx.MenuBar() newsMenu = wx.Menu() newsMenu.Append(ID_EDGAR, 'Edgar News', 'Edgar news') newsMenu.Append(ID_GLOBAL, 'Global Economy', 'Global economy') newsMenu.Append(ID_ECONOMY, 'Economy Data', 'Economy data') newsMenu.Append(ID_FX, 'FX', 'Fx data') futuresItem = newsMenu.Append(ID_FUTURES, 'FUTURES', 'FUTURES') exitItem = newsMenu.Append(wx.ID_EXIT, 'Quit', 'Quit application') menubar.Append(newsMenu, '&News') mgmtMenu = wx.Menu() shareMgmtItem = mgmtMenu.Append(ID_UPDATE_SHARE, 'Share Management', 'Share Management') ptfMgmtItem = mgmtMenu.Append(ID_UPDATE_PORTFOLIO, 'Portfolio Management', 'Portfolio Mgmt') resetItem = mgmtMenu.Append(ID_RESET, 'Reset Listbox', 'Portfolio Mgmt') menubar.Append(mgmtMenu, '&Management') self.SetMenuBar(menubar) self.Bind(wx.EVT_MENU, self.OnQuit, exitItem) self.Bind(wx.EVT_MENU, self.OnFutures, futuresItem) self.Bind(wx.EVT_MENU, self.OnShareMgmt, shareMgmtItem) self.Bind(wx.EVT_MENU, self.OnPtfMgmt, ptfMgmtItem) self.listBox1 = wx.ListBox(self, wx.ID_ANY, choices=[], style=wx.LB_EXTENDED) self.listBox1.Append('MAIN') self.listBox1.Append('INDEXES') self.listBox1.Append('CURRENCIES') self.listBox1.Append('HEDGE') self.listBox1.Append('CDS SPREADS') self.listBox1.Bind(wx.EVT_LISTBOX, self.OnListBox1Listbox, id=ID_FRAME1LISTBOX1) self.Bind(wx.EVT_MENU, self.OnResetList, resetItem) hbox = wx.BoxSizer(wx.HORIZONTAL) panel = wx.Panel(self, -1) self.list = AutoWidthListCtrl(panel) self.list.InsertColumn(0, 'Ticker')#, width=150) self.list.InsertColumn(1, 'Name')#', width=100) self.list.InsertColumn(2, 'MovingAvg200')#', width=100) self.list.InsertColumn(3, 'MovingAvg50')#', width=100) self.list.InsertColumn(4, 'Price')#, width=80) self.list.InsertColumn(5, 'Previous')#, width=80) self.list.InsertColumn(6, 'Change')#, width=80) self.list.InsertColumn(7, 'TotalValue')#, width=80) self.list.InsertColumn(8, 'Position')#', width=100) hbox.Add(self.list, 2, wx.EXPAND) panel.SetSizer(hbox) self.SetSize((900, 500)) self.SetTitle('Camel App Python Gui') self.Centre() sizer = wx.GridBagSizer(vgap=8, hgap=10) # pos=(row, column) span=(rowspan, columnspan) # wx.ALL puts the specified border on all sides sizer.Add(self.load_button, pos=(0, 0), flag=wx.ALL, border=5) # listbox spans 6 rows and 2 columns sizer.Add(self.clear_button, pos=(0, 25), flag=wx.ALL, border=5) sizer.Add(self.listBox1, pos=(2, 4), span=(8, 8), flag=wx.ALL|wx.EXPAND, border=5) sizer.Add(panel, pos=(10,1),span=(10, 30), flag=wx.ALL|wx.EXPAND, border=5) self.SetSizer(sizer) #self.Fit() self.Show(True) def OnQuit(self, e): self.Close() def OnResult(self, event): print 'onResult' ptf_data = event.data for item in ptf_data['portfolio']['items']: index = self.list.InsertStringItem(sys.maxint, item['ticker']) self.list.SetStringItem(index, 1, item['name']) self.list.SetStringItem(index, 2, str(item['movingAvg200'])) self.list.SetStringItem(index, 3, str(item['movingAvg50'])) self.list.SetStringItem(index, 4, str(item['price'])) self.list.SetStringItem(index, 5, str(item['previous'])) self.list.SetStringItem(index, 6, str(item['price'] - item['previous']) ) self.list.SetStringItem(index, 7, str(item['totalValue'])) self.list.SetStringItem(index, 8, str(item['position'])) def load_buttonClick(self,e): idx = self.listBox1.GetSelections()[0] ptf = self.listBox1.GetStrings()[idx] thread.start_new_thread(self.fetch, (ptf,)) self.list.ClearAll() def fetch(self, ptf): data= {'action':'portfolio', 'portfolioName':ptf} ptf_data = jsonLoader.openJsonRequest(data) print ptf_data['portfolio']['cash'] wx.PostEvent(self, ResultEvent(data=ptf_data)) -------------- next part -------------- An HTML attachment was scrubbed... URL: From mmistroni at gmail.com Sat Oct 20 00:10:53 2012 From: mmistroni at gmail.com (Marco Mistroni) Date: Fri, 19 Oct 2012 23:10:53 +0100 Subject: [Tutor] Segmentation Fault shenanigans with wx Gui In-Reply-To: References: Message-ID: Hello i found the problem. It's calling self.list.ClearAll that causes the segmentation fault. removing the call to ClearAll fixed my problem , but i still want to clear the list before i load new data...... could anyone assist? wkr marco On Fri, Oct 19, 2012 at 11:05 PM, Marco Mistroni wrote: > Hi all > i have written a wx GUI which downloads json data from a server, and > populate a listbox. > Every time i populate a listbox, i am receiving Segmentation Faults. > I have tried to retrieve data from the URL via separate thread, and to use > events, but i am still getting a segmentation fault > > could anyone assist pls? > > here's relevant code. I m running python on Ubuntu 10.04 > > class WxSharesGUI(wx.Frame): > > > def __init__(self, parent, id, title): > wx.Frame.__init__(self, parent, id, title, size=(300, 300)) > self.InitUI() > > def InitUI(self): > self.load_button = wx.Button(self, wx.ID_ANY, "load ListBox") > self.clear_button = wx.Button(self, wx.ID_ANY, "clear ListBox") > self.load_button.Bind(wx.EVT_BUTTON, self.load_buttonClick) > self.clear_button.Bind(wx.EVT_BUTTON, self.clear_buttonClick) > self.Bind(EVT_RESULT, self.OnResult) > self.lock = threading.RLock() > > # Creating Menu bar > menubar = wx.MenuBar() > newsMenu = wx.Menu() > newsMenu.Append(ID_EDGAR, 'Edgar News', 'Edgar news') > newsMenu.Append(ID_GLOBAL, 'Global Economy', 'Global economy') > newsMenu.Append(ID_ECONOMY, 'Economy Data', 'Economy data') > newsMenu.Append(ID_FX, 'FX', 'Fx data') > futuresItem = newsMenu.Append(ID_FUTURES, 'FUTURES', 'FUTURES') > exitItem = newsMenu.Append(wx.ID_EXIT, 'Quit', 'Quit application') > menubar.Append(newsMenu, '&News') > > mgmtMenu = wx.Menu() > shareMgmtItem = mgmtMenu.Append(ID_UPDATE_SHARE, 'Share Management', > 'Share Management') > ptfMgmtItem = mgmtMenu.Append(ID_UPDATE_PORTFOLIO, 'Portfolio > Management', 'Portfolio Mgmt') > resetItem = mgmtMenu.Append(ID_RESET, 'Reset Listbox', 'Portfolio > Mgmt') > menubar.Append(mgmtMenu, '&Management') > > > self.SetMenuBar(menubar) > > self.Bind(wx.EVT_MENU, self.OnQuit, exitItem) > self.Bind(wx.EVT_MENU, self.OnFutures, futuresItem) > self.Bind(wx.EVT_MENU, self.OnShareMgmt, shareMgmtItem) > self.Bind(wx.EVT_MENU, self.OnPtfMgmt, ptfMgmtItem) > > self.listBox1 = wx.ListBox(self, wx.ID_ANY, choices=[], > style=wx.LB_EXTENDED) > self.listBox1.Append('MAIN') > self.listBox1.Append('INDEXES') > self.listBox1.Append('CURRENCIES') > self.listBox1.Append('HEDGE') > self.listBox1.Append('CDS SPREADS') > self.listBox1.Bind(wx.EVT_LISTBOX, self.OnListBox1Listbox, > id=ID_FRAME1LISTBOX1) > self.Bind(wx.EVT_MENU, self.OnResetList, resetItem) > > hbox = wx.BoxSizer(wx.HORIZONTAL) > > panel = wx.Panel(self, -1) > > self.list = AutoWidthListCtrl(panel) > self.list.InsertColumn(0, 'Ticker')#, width=150) > self.list.InsertColumn(1, 'Name')#', width=100) > self.list.InsertColumn(2, 'MovingAvg200')#', width=100) > self.list.InsertColumn(3, 'MovingAvg50')#', width=100) > self.list.InsertColumn(4, 'Price')#, width=80) > self.list.InsertColumn(5, 'Previous')#, width=80) > self.list.InsertColumn(6, 'Change')#, width=80) > self.list.InsertColumn(7, 'TotalValue')#, width=80) > self.list.InsertColumn(8, 'Position')#', width=100) > > hbox.Add(self.list, 2, wx.EXPAND) > panel.SetSizer(hbox) > > > self.SetSize((900, 500)) > self.SetTitle('Camel App Python Gui') > self.Centre() > > sizer = wx.GridBagSizer(vgap=8, hgap=10) > # pos=(row, column) span=(rowspan, columnspan) > # wx.ALL puts the specified border on all sides > sizer.Add(self.load_button, pos=(0, 0), flag=wx.ALL, border=5) > # listbox spans 6 rows and 2 columns > sizer.Add(self.clear_button, pos=(0, 25), flag=wx.ALL, border=5) > > > sizer.Add(self.listBox1, pos=(2, 4), span=(8, 8), > flag=wx.ALL|wx.EXPAND, border=5) > > sizer.Add(panel, pos=(10,1),span=(10, 30), > flag=wx.ALL|wx.EXPAND, border=5) > self.SetSizer(sizer) > #self.Fit() > > self.Show(True) > > def OnQuit(self, e): > self.Close() > > def OnResult(self, event): > print 'onResult' > ptf_data = event.data > for item in ptf_data['portfolio']['items']: > index = self.list.InsertStringItem(sys.maxint, item['ticker']) > self.list.SetStringItem(index, 1, item['name']) > self.list.SetStringItem(index, 2, str(item['movingAvg200'])) > self.list.SetStringItem(index, 3, str(item['movingAvg50'])) > self.list.SetStringItem(index, 4, str(item['price'])) > self.list.SetStringItem(index, 5, str(item['previous'])) > self.list.SetStringItem(index, 6, str(item['price'] - > item['previous']) ) > self.list.SetStringItem(index, 7, str(item['totalValue'])) > self.list.SetStringItem(index, 8, str(item['position'])) > > > > def load_buttonClick(self,e): > idx = self.listBox1.GetSelections()[0] > > ptf = self.listBox1.GetStrings()[idx] > > thread.start_new_thread(self.fetch, (ptf,)) > self.list.ClearAll() > > def fetch(self, ptf): > data= {'action':'portfolio', 'portfolioName':ptf} > ptf_data = jsonLoader.openJsonRequest(data) > print ptf_data['portfolio']['cash'] > wx.PostEvent(self, ResultEvent(data=ptf_data)) > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sat Oct 20 00:28:45 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 20 Oct 2012 09:28:45 +1100 Subject: [Tutor] Segmentation Fault shenanigans with wx Gui In-Reply-To: References: Message-ID: <5081D41D.2020405@pearwood.info> On 20/10/12 09:10, Marco Mistroni wrote: > Hello > i found the problem. It's calling self.list.ClearAll that causes the > segmentation fault. > removing the call to ClearAll fixed my problem , but i still want to clear > the list before i load new data...... > could anyone assist? This is not a question about learning Python, it is a specialised wxPython question. I expect that it is a bug in wxPython. (As a segmentation fault, it's a pretty serious bug too.) I suggest you report it on the appropriate wxPython mailing list or bug tracker. Good luck! -- Steven From alan.gauld at btinternet.com Sat Oct 20 01:58:01 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 20 Oct 2012 00:58:01 +0100 Subject: [Tutor] Segmentation Fault shenanigans with wx Gui In-Reply-To: References: Message-ID: On 19/10/12 23:05, Marco Mistroni wrote: > Hi all > i have written a wx GUI which downloads json data from a server, and > populate a listbox. ... > use events, but i am still getting a segmentation fault A segmentation fault is usually due to a fault down in the C code. With wxPython that means most likely a bug in the underlying wxWidgets libraries. Have a look in the wxWidgets forums for a known issue with your version. Or try asking there. I doubt it will be a Python problem - except in that it might be passing an integer (especially zero) where wxWidgets expects a pointer. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From wrw at mac.com Sat Oct 20 03:03:27 2012 From: wrw at mac.com (wrw at mac.com) Date: Fri, 19 Oct 2012 21:03:27 -0400 Subject: [Tutor] managing memory large dictionaries in python In-Reply-To: References: Message-ID: <6D74EC42-71F7-4D66-BE41-376DB9627234@mac.com> On Oct 16, 2012, at 12:57 PM, Abhishek Pratap wrote: > Hi Guys > > For my problem I need to store 400-800 million 20 characters keys in a > dictionary and do counting. This data structure takes about 60-100 Gb > of RAM. > I am wondering if there are slick ways to map the dictionary to a file > on disk and not store it in memory but still access it as dictionary > object. Speed is not the main concern in this problem and persistence > is not needed as the counting will only be done once on the data. We > want the script to run on smaller memory machines if possible. > > I did think about databases for this but intuitively it looks like a > overkill coz for each key you have to first check whether it is > already present and increase the count by 1 and if not then insert > the key into dbase. > > Just want to take your opinion on this. > > Thanks! > -Abhi > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor With apologies for coming to this late. Just a couple of comments - 1) If the keys were sorted before the counting operation was to take place, the problem could be run in successive batches in as small a foot print as desired. 2) Sorting in limited memory space was a highly developed art back in the early days of IBM mainframe computing (when one had to use tape rather than disk for temp store and 64k, yes "k", was a lot of memory). If you look at Knuth, Volume III: Sorting and Searching you will find several algorithms that would allow the sort to also run in as small a foot print as needed. Possibly not interesting, but just saying? Bill From malcolm.newsome at gmail.com Sun Oct 21 03:09:32 2012 From: malcolm.newsome at gmail.com (Malcolm Newsome) Date: Sat, 20 Oct 2012 20:09:32 -0500 Subject: [Tutor] map() practice In-Reply-To: References: Message-ID: <50834B4C.2000902@gmail.com> Hello all, I looked at map() tonight. I think I have a decent understanding now of "how" it works. However, I'm wondering when it is most commonly used in the real world and if you could provide some examples (I like to do a lot of web stuff...it that helps with regards to providing a context for real world application). Thanks! Malcolm From alan.gauld at btinternet.com Sun Oct 21 09:53:49 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 21 Oct 2012 08:53:49 +0100 Subject: [Tutor] map() practice In-Reply-To: <50834B4C.2000902@gmail.com> References: <50834B4C.2000902@gmail.com> Message-ID: On 21/10/12 02:09, Malcolm Newsome wrote: > Hello all, > > I looked at map() tonight. I think I have a decent understanding now of > "how" it works. However, I'm wondering when it is most commonly used in > the real world and if you could provide some examples (I like to do a > lot of web stuff...it that helps with regards to providing a context for > real world application). Do you use JQuery in your web programming? If so you should be familiar with the kind of things map() can do. You use it to make a change to a collection of objects. So you can write a function to select all the objects that need changing and then another function to change those objects. Then call map on the two functions: def getcollection():... def change(item):... map(change, getCollection()) So in a web context you might want to change the header level of every header following a div tag. In network programming I might want to change the destination address of every packet coming into the server for a particular message. map() is not used as much these days since you can often do the same thing more easily using list comprehensions or generator expressions, but before those were introduced map() was a useful convenience function. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From __peter__ at web.de Sun Oct 21 10:42:40 2012 From: __peter__ at web.de (Peter Otten) Date: Sun, 21 Oct 2012 10:42:40 +0200 Subject: [Tutor] map() practice References: <50834B4C.2000902@gmail.com> Message-ID: Malcolm Newsome wrote: > Hello all, > > I looked at map() tonight. I think I have a decent understanding now of > "how" it works. However, I'm wondering when it is most commonly used in > the real world and if you could provide some examples (I like to do a > lot of web stuff...it that helps with regards to providing a context for > real world application). map() is convenient whenever you need to apply an existing function to all items of a list: A very simple date parser using map() >>> year, month, date = map(int, "2012-10-21".split("-")) >>> year, month, date (2012, 10, 21) or a list comprehension: >>> [int(x) for x in "2012-10-21".split("-")] [2012, 10, 21] Calculating a sum of absolute values using map() >>> sum(map(abs, range(-10, 10))) 100 or a generator expression: >>> sum(abs(x) for x in range(-10, 10)) 100 Calculating the dot product of two vectors using map() >>> x = range(10) >>> y = range(0, 100, 10) >>> from operator import mul >>> sum(map(mul, x, y)) 2850 or zip() and a generator expression: >>> sum(a*b for a, b in zip(x, y)) 2850 If you want real-world applications I encourage you to use grep on the code base that interests you, e. g.: $ grep '[^a-zA-Z]map(' /usr/lib/python2.7/*.py | head /usr/lib/python2.7/argparse.py: tup = value, ', '.join(map(repr, action.choices)) /usr/lib/python2.7/ast.py: return tuple(map(_convert, node.elts)) /usr/lib/python2.7/ast.py: return list(map(_convert, node.elts)) /usr/lib/python2.7/Bastion.py: print "b._get_.func_defaults =", map(type, b._get_.func_defaults), /usr/lib/python2.7/CGIHTTPServer.py: nobody = 1 + max(map(lambda x: x[2], pwd.getpwall())) /usr/lib/python2.7/cgi.py: return map(attrgetter('value'), value) /usr/lib/python2.7/cgi.py: return map(attrgetter('value'), value) /usr/lib/python2.7/cmd.py: ", ".join(map(str, nonstrings))) /usr/lib/python2.7/codecs.py:def make_encoding_map(decoding_map): /usr/lib/python2.7/code.py: map(self.write, list) Note that it is bad style to use map() for its side effects like in the last match. It does extra work to create a list that consumes memory only to be thrown away immediatly afterwards: >>> import sys >>> lines = ["alpha\n", "beta\n", "gamma\n"] >>> map(sys.stdout.write, lines) # DON'T DO THIS alpha beta gamma [None, None, None] # BETTER >>> for line in lines: ... sys.stdout.write(line) ... alpha beta gamma # EVEN BETTER (in this particular case) >>> sys.stdout.writelines(lines) alpha beta gamma PS: In Python 3 map() has become "lazy", i. e. it works like a generator expression rather than a list comprehension. For some applications you have to replace map(...) with list(map(...)) to make the code work in Python 3. From steve at pearwood.info Sun Oct 21 11:24:56 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 21 Oct 2012 20:24:56 +1100 Subject: [Tutor] managing memory large dictionaries in python In-Reply-To: References: Message-ID: <5083BF68.7040207@pearwood.info> On 17/10/12 13:22, Alexander wrote: > On Tue, Oct 16, 2012 at 20:43 EST, Mark Lawrence > wrote: >> For the record Access is not a database, or so some geezer called Alex >> Martelli reckons http://code.activestate.com/lists/python-list/48130/, so >> please don't shoot the messenger:) >> Cheers. >> Mark Lawrence. > > Mark I don't believe your response is relevant or helpful to the > original post so please don't hijack. It's only not relevant because you cut out the context. The reply was relevant. Somebody made a statement implying that Access is a database, and Mark corrected them. I think it's dirty pool to take a comment out of context and then complain its not relevant. Now if you want to criticize Mark for being pedantic, that's another story. Me personally, I always welcome the opportunity to learn something I didn't know before, so I'd like to take this opportunity to thank Mark for mentioning the interesting fact that Access is not, in actuality, a database, but merely the front end which can talk to many different databases, including the default JET database. Not only is this pedantically true, and interesting, and useful, but it is also relevant to Python and the question being asked, since you can use Python to talk to a JET database without Access, or Access to talk to a non-JET database created by Python. It's nice to have options. It's better to know things than not to. Thanks Mark! -- Steven From steve at pearwood.info Sun Oct 21 11:47:39 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 21 Oct 2012 20:47:39 +1100 Subject: [Tutor] instance method call issue In-Reply-To: <507B308F.2010101@gmail.com> References: <507B308F.2010101@gmail.com> Message-ID: <5083C4BB.7070007@pearwood.info> Digging up a week-old email from Bob: On 15/10/12 08:37, bob gailer wrote: > On 10/14/2012 8:34 AM, Osemeka Osuagwu wrote: >> except: >> print 'Cannot display Grid' > > In addition to Steve's comment I add my own gripe: > > When delivering an error message tell us WHY. I have seen too many > messages "cannot do xxx" with no information that might help me >debug. > "cannot load file xxx" > "Windows cannot load the installer for xxx" > > or totally confusing messages > "error 6 trying to report error 6" > "cannot do xxx to file yyy. file exists" > > In reality the underlying program knows a lot more about why. Could >not the developers have presented that information as well? Of course they can, and I always try to write good, helpful error messages. Sometimes I fail :) Here is a random selection of some of my exceptions, taken entirely out of context: ValueError('error tolerances must be non-negative') StatsError('geometric mean of negative number') InvalidNumber("Invalid TFN, checkdigit does not match.") ValueError('output file already exists') TypeError('frange expects 1-4 arguments, got %d' % n) ValueError('step must not be zero') ValueError('gamma function undefined at 0 and negative integers') TypeError('expected a namespace but got a %s' % typename) TypeError('abstract method <%s> must be overridden' % func.__name__) And then there's this one... ValueError("broken") O_o -- Steven From sbjaved at gmail.com Mon Oct 22 02:01:07 2012 From: sbjaved at gmail.com (Saad Javed) Date: Mon, 22 Oct 2012 05:01:07 +0500 Subject: [Tutor] Print items from 3 lists in order Message-ID: Hi, a = ['Ron', 'Harry', 'Hermoine'] b = ['25th oct', '27th oct', '29th oct'] c = ['Charms', 'DADA', 'Potions'] I want to print like this: Ron - 25th oct Charms Harry - 27th oct DADA Hermoine - 29th oct Potions The items in each list are populated dynamically so I don't know how many items will be there in each list, every time the program runs. I tried: >>> for x in zip(a, b, c): print x But that gives: ('Ron', '25th oct', 'Charms') ('Harry', '27th oct', 'DADA') ('Hermoine', '29th oct', 'Potions') ??? Saad -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Mon Oct 22 02:09:18 2012 From: d at davea.name (Dave Angel) Date: Sun, 21 Oct 2012 20:09:18 -0400 Subject: [Tutor] Print items from 3 lists in order In-Reply-To: References: Message-ID: <50848EAE.3000309@davea.name> On 10/21/2012 08:01 PM, Saad Javed wrote: > Hi, > a = ['Ron', 'Harry', 'Hermoine'] > b = ['25th oct', '27th oct', '29th oct'] > c = ['Charms', 'DADA', 'Potions'] > I want to print like this: > Ron - 25th oct > Charms > Harry - 27th oct > DADA > Hermoine - 29th oct > Potions > > The items in each list are populated dynamically so I don't know how many > items will be there in each list, every time the program runs. > I tried: >>>> for x in zip(a, b, c): print x > But that gives: > ('Ron', '25th oct', 'Charms') > ('Harry', '27th oct', 'DADA') > ('Hermoine', '29th oct', 'Potions') > > ??? > > Saad > > Very good; you're close. instead of z, just use 3 variables: for xname, xdate, xitem in zip(a,b,c): print xname, "-", xdate print xitem or something similar. Alternatively, you could have printed z[0], z[1], and z[2]. But this way is clearer. -- DaveA From alan.gauld at btinternet.com Mon Oct 22 02:20:55 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 22 Oct 2012 01:20:55 +0100 Subject: [Tutor] Print items from 3 lists in order In-Reply-To: <50848EAE.3000309@davea.name> References: <50848EAE.3000309@davea.name> Message-ID: On 22/10/12 01:09, Dave Angel wrote: >> I tried: >>>>> for x in zip(a, b, c): print x >> But that gives: >> ('Ron', '25th oct', 'Charms') >> ('Harry', '27th oct', 'DADA') >> ('Hermoine', '29th oct', 'Potions') >> >> ??? So all you have now is a formatting problem. > Very good; you're close. instead of z, just use 3 variables: > > for xname, xdate, xitem in zip(a,b,c): > print xname, "-", xdate > print xitem > > or something similar. Which could be a format string (Python 2 format since that looks like what you are using): for x in zip(a, b, c): print "%s - %s\n%s" % x The advantage of the format string is it gives you much more control over spacing and justification of the items. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From sbjaved at gmail.com Mon Oct 22 03:44:51 2012 From: sbjaved at gmail.com (Saad Javed) Date: Mon, 22 Oct 2012 06:44:51 +0500 Subject: [Tutor] Populating a list Message-ID: My program downloads multiple entry values from the net. I'm trying to combine them in a list in a particular sequence. l = [] feed1 = urllib2.urlopen(rssPage1) tree1 = etree.parse(feed1) x = tree1.xpath("/rss/channel/item/title/text()") y = tree1.xpath("/rss/channel/item/pubDate/text()") z = tree1.xpath("/rss/channel/item/link/text()") for e, f, g in zip(x, y, z): l.append([e, f, g]) print l The problem I'm facing is that the values don't combine into a single list ("l"). It keeps printing multiple lists with three values e, f, g as the xml tree is parsed. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rhettnaxel at gmail.com Mon Oct 22 04:35:02 2012 From: rhettnaxel at gmail.com (Alexander) Date: Sun, 21 Oct 2012 22:35:02 -0400 Subject: [Tutor] Populating a list In-Reply-To: References: Message-ID: On Sun, Oct 21, 2012 at 9:44 PM, Saad Javed wrote: > My program downloads multiple entry values from the net. I'm trying to > combine them in a list in a particular sequence. > > l = [] > feed1 = urllib2.urlopen(rssPage1) > tree1 = etree.parse(feed1) > x = tree1.xpath("/rss/channel/item/title/text()") > y = tree1.xpath("/rss/channel/item/pubDate/text()") > z = tree1.xpath("/rss/channel/item/link/text()") > for e, f, g in zip(x, y, z): > l.append([e, f, g]) > print l > > The problem I'm facing is that the values don't combine into a single list > ("l"). It keeps printing multiple lists with three values e, f, g as the xml > tree is parsed. What is the rssPage1? Where is that part? And for readability I'd strong suggest using something other than a lowercase L. l and I look the same in some fonts. 7D9C597B From steve at pearwood.info Mon Oct 22 05:31:12 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 22 Oct 2012 14:31:12 +1100 Subject: [Tutor] Populating a list In-Reply-To: References: Message-ID: <20121022033111.GB10202@ando> On Mon, Oct 22, 2012 at 06:44:51AM +0500, Saad Javed wrote: > My program downloads multiple entry values from the net. I'm trying to > combine them in a list in a particular sequence. > > l = [] [snip rest of code] > l.append([e, f, g]) > The problem I'm facing is that the values don't combine into a single list > ("l"). It keeps printing multiple lists with three values e, f, g as the > xml tree is parsed. Of course it does. You tell it to: you are appending a new list with three entries, [e, f, g]. What you should do is either append each item separately: l.append(e) l.append(f) l.append(g) or use the extend method: l.extend([e, f, g]) which is a short-cut for three individual appends. -- Steven From sbjaved at gmail.com Mon Oct 22 11:31:23 2012 From: sbjaved at gmail.com (Saad Javed) Date: Mon, 22 Oct 2012 14:31:23 +0500 Subject: [Tutor] Populating a list In-Reply-To: References: Message-ID: l.extend does work. Thanks! On Monday, October 22, 2012, Saad Javed wrote: > My program downloads multiple entry values from the net. I'm trying to > combine them in a list in a particular sequence. > > l = [] > feed1 = urllib2.urlopen(rssPage1) > tree1 = etree.parse(feed1) > x = tree1.xpath("/rss/channel/item/title/text()") > y = tree1.xpath("/rss/channel/item/pubDate/text()") > z = tree1.xpath("/rss/channel/item/link/text()") > for e, f, g in zip(x, y, z): > l.append([e, f, g]) > print l > > The problem I'm facing is that the values don't combine into a single list > ("l"). It keeps printing multiple lists with three values e, f, g as the > xml tree is parsed. > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sbjaved at gmail.com Mon Oct 22 12:21:41 2012 From: sbjaved at gmail.com (Saad Javed) Date: Mon, 22 Oct 2012 15:21:41 +0500 Subject: [Tutor] Creating a list from other lists Message-ID: Hi, I'm trying to create a list (L) from items of different lists (a, b, c) but in a specific order (L = [[a1, b1, c1], [a2, b2, c2]...etc]) L = [] a = [1, 2, 3, 4] b = ['a', 'b', 'c', 'd'] c = [2009, 2010, 2011, 2012] for x, y , z in zip(a, b, c): L.extend([x, y, z]) print L But this outputs: [[1, 'a', 2009]] [[1, 'a', 2009], [2, 'b', 2010]] [[1, 'a', 2009], [2, 'b', 2010], [3, 'c', 2011]] [[1, 'a', 2009], [2, 'b', 2010], [3, 'c', 2011], [4, 'd', 2012]] I just want L = [[1, 'a', 2009], [2, 'b', 2010], [3, 'c', 2011], [4, 'd', 2012]] Saad -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Mon Oct 22 12:34:47 2012 From: wprins at gmail.com (Walter Prins) Date: Mon, 22 Oct 2012 11:34:47 +0100 Subject: [Tutor] Creating a list from other lists In-Reply-To: References: Message-ID: Hi Saad, On 22 October 2012 11:21, Saad Javed wrote: > Hi, > > I'm trying to create a list (L) from items of different lists (a, b, c) but > in a specific order (L = [[a1, b1, c1], [a2, b2, c2]...etc]) > L = [] > a = [1, 2, 3, 4] > b = ['a', 'b', 'c', 'd'] > c = [2009, 2010, 2011, 2012] > > for x, y , z in zip(a, b, c): > L.extend([x, y, z]) > print L > > But this outputs: > [[1, 'a', 2009]] > [[1, 'a', 2009], [2, 'b', 2010]] > [[1, 'a', 2009], [2, 'b', 2010], [3, 'c', 2011]] > [[1, 'a', 2009], [2, 'b', 2010], [3, 'c', 2011], [4, 'd', 2012]] > > I just want L = [[1, 'a', 2009], [2, 'b', 2010], [3, 'c', 2011], [4, 'd', > 2012]] You've already asked essentially this question on another thread -- in general please don't start new threads for the same question but continue the discussion on the original question for the sake of continuity and avoiding wasting people's time. (New readers of the new question may not be aware of the previous discussion, and may expend time and energy addressing issues which have already been discussed in the previous question.) As for your problem: You're printing the list every iteration of the loop which is why your output looks the way it does. Instead put your print statement after the loop and you'll see the list you get after the loop is in fact what you say you want. However, that doesn't actually solve your problem, because you're putting the input data into the lists in sorted order already. You should jumble up your input data to ensure you also solve the sorting aspect of this problem (or use the input data from your original problem which was jumbled up enough already.) Hint, to deal with sorting your output list, refer to the list.sort() method and the "key" parameter. See also the "Sorting HowTo" on the PythonInfo wiki by Raymond Hettinger: http://wiki.python.org/moin/HowTo/Sorting/ Hope is enough to get you going, Walter From steve at pearwood.info Mon Oct 22 12:37:40 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 22 Oct 2012 21:37:40 +1100 Subject: [Tutor] Creating a list from other lists In-Reply-To: References: Message-ID: <508521F4.1030706@pearwood.info> On 22/10/12 21:21, Saad Javed wrote: > Hi, > > I'm trying to create a list (L) from items of different lists (a, b, c) but > in a specific order (L = [[a1, b1, c1], [a2, b2, c2]...etc]) > L = [] > a = [1, 2, 3, 4] > b = ['a', 'b', 'c', 'd'] > c = [2009, 2010, 2011, 2012] > > for x, y , z in zip(a, b, c): > L.extend([x, y, z]) > print L This is not your code, because that gives a SyntaxError. Where is the indentation? Indentation is required in Python, if you leave it out, your code will not work correctly. You should have either: # Option 1 for x, y , z in zip(a, b, c): L.extend([x, y, z]) print L which will extend the list four times, and print the list four times, once each time through the loop. Or: # Option 2 for x, y , z in zip(a, b, c): L.extend([x, y, z]) print L # this is not indented, so it is outside the loop Now the list only gets printed once, after the for loop has completely finished, and you don't see the intermediate results. > But this outputs: > [[1, 'a', 2009]] > [[1, 'a', 2009], [2, 'b', 2010]] > [[1, 'a', 2009], [2, 'b', 2010], [3, 'c', 2011]] > [[1, 'a', 2009], [2, 'b', 2010], [3, 'c', 2011], [4, 'd', 2012]] > > I just want L = [[1, 'a', 2009], [2, 'b', 2010], [3, 'c', 2011], [4, 'd', > 2012]] Then don't print the list four times, only print it once. -- Steven From eryksun at gmail.com Mon Oct 22 12:52:01 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 22 Oct 2012 06:52:01 -0400 Subject: [Tutor] Creating a list from other lists In-Reply-To: <508521F4.1030706@pearwood.info> References: <508521F4.1030706@pearwood.info> Message-ID: On Mon, Oct 22, 2012 at 6:37 AM, Steven D'Aprano wrote: > On 22/10/12 21:21, Saad Javed wrote: >> >> for x, y , z in zip(a, b, c): >> L.extend([x, y, z]) >> print L > > This is not your code, because that gives a SyntaxError. Where is > the indentation? Indentation is required in Python, if you leave it > out, your code will not work correctly. It's an HTML post. Using these styled tags never translates well to the text/plain section.
for x, y , z in zip(a, b, c):
L.extend([x, y, z])
print L
From wprins at gmail.com Mon Oct 22 12:54:01 2012 From: wprins at gmail.com (Walter Prins) Date: Mon, 22 Oct 2012 11:54:01 +0100 Subject: [Tutor] Creating a list from other lists In-Reply-To: <508521F4.1030706@pearwood.info> References: <508521F4.1030706@pearwood.info> Message-ID: Hi Saad, On 22 October 2012 11:37, Steven D'Aprano wrote: > On 22/10/12 21:21, Saad Javed wrote: >> I'm trying to create a list (L) from items of different lists (a, b, c) >> but >> in a specific order (L = [[a1, b1, c1], [a2, b2, c2]...etc]) >> L = [] >> a = [1, 2, 3, 4] >> b = ['a', 'b', 'c', 'd'] >> c = [2009, 2010, 2011, 2012] >> >> for x, y , z in zip(a, b, c): >> L.extend([x, y, z]) >> print L > > > This is not your code, because that gives a SyntaxError. Where is > the indentation? Indentation is required in Python, if you leave it > out, your code will not work correctly. You should have either: Just to note: For me your (Saad's) indentation showed perfectly fine on GMail. I'm note sure why Steven didn't see it (I think he reads only plaintext email), so I'd guess perhaps your email included both HTML and plain text parts and the plain text part perhaps had the indentation stripped out and the HTML did not. Anyway point being, it's best to post plain text if you're emailing unless you absolutely must use HTML, certainly this is the best way for this mailing list. Regards, Walter From chigga101 at gmail.com Mon Oct 22 13:45:16 2012 From: chigga101 at gmail.com (Matthew Ngaha) Date: Mon, 22 Oct 2012 12:45:16 +0100 Subject: [Tutor] Which is better Practice and why Message-ID: In many of the tutorial examples ive come across, the main code's program is never at the top level, but always in a function of some sort. i understand why but, there is always a different way used to access the main code, i want to know which is the best. main() main's code #top level main() they call the main program by simply calling the main function. I've also seen a more complcated: if __name__ == '__main__': main() the 2nd one usually includes a lot more code then i showed. can you please tell me why different methods are used to access the main code? is it just preference or is one way actually better coding practice? -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeanpierreda at gmail.com Mon Oct 22 13:54:01 2012 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Mon, 22 Oct 2012 07:54:01 -0400 Subject: [Tutor] Which is better Practice and why In-Reply-To: References: Message-ID: On Mon, Oct 22, 2012 at 7:45 AM, Matthew Ngaha wrote: > the 2nd one usually includes a lot more code then i showed. can you please > tell me why different methods are used to access the main code? is it just > preference or is one way actually better coding practice? The second one is used if your importable modules are also scripts that can be executed. That's a bad idea, because it results in the same source file being executed twice, producing distinct classes that break typechecking. If you have "myprogram.py" containing the following source code, and execute it as a program, it will terminate with an uncaught MyError. class MyError(Exception): pass import myprogram try: raise myprogram.MyError except MyError: pass So, in any circumstance where you would use the second one, it's because you're in a situation where bad things are happening. So use the first one always. And if there's code in the script you want to share, put it in a separate module. -- Devin From breamoreboy at yahoo.co.uk Mon Oct 22 13:58:24 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 22 Oct 2012 12:58:24 +0100 Subject: [Tutor] Which is better Practice and why In-Reply-To: References: Message-ID: On 22/10/2012 12:45, Matthew Ngaha wrote: > In many of the tutorial examples ive come across, the main code's program > is never at the top level, but always in a function of some sort. i > understand why but, there is always a different way used to access the main > code, i want to know which is the best. > > > main() > main's code > > #top level > main() > > they call the main program by simply calling the main function. I've also > seen a more complcated: > > if __name__ == '__main__': > main() > > the 2nd one usually includes a lot more code then i showed. can you please > tell me why different methods are used to access the main code? is it just > preference or is one way actually better coding practice? > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > See this http://docs.python.org/tutorial/modules.html specifically section 6.1.1. "Executing modules as scripts". -- Cheers. Mark Lawrence. From joel.goldstick at gmail.com Mon Oct 22 13:58:51 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 22 Oct 2012 07:58:51 -0400 Subject: [Tutor] Which is better Practice and why In-Reply-To: References: Message-ID: On Mon, Oct 22, 2012 at 7:45 AM, Matthew Ngaha wrote: > In many of the tutorial examples ive come across, the main code's program is > never at the top level, but always in a function of some sort. i understand > why but, there is always a different way used to access the main code, i > want to know which is the best. > > > main() > main's code > > #top level > main() > > they call the main program by simply calling the main function. I've also > seen a more complcated: > > if __name__ == '__main__': > main() > Every module in python has a name. You have probably seen code like this: import os import sys os and sys are modules and their names are 'os' and 'sys' When you write your own program in a file 'my_code.py' and then run it by typing: python my_code.py The name of your program is '__main__' So when you write: if __name__ == '__main__': The code will only run when you run the file directly. Many times, the functions in your file could be used in other programs. In that case you might have a file called my_new_code.py In my_new_code.py you could import the other file so you could use its functions. Like this: import my_code When you import the file, it is not the __main__ code. Its name will be my_code. And so the code you have in the part I marked will never run. It only runs when you run the file directly. -- Joel Goldstick From chigga101 at gmail.com Mon Oct 22 14:58:36 2012 From: chigga101 at gmail.com (Matthew Ngaha) Date: Mon, 22 Oct 2012 13:58:36 +0100 Subject: [Tutor] Which is better Practice and why In-Reply-To: References: Message-ID: oh ok i understand it.. Thanks for the help guys -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Mon Oct 22 15:12:34 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 22 Oct 2012 09:12:34 -0400 Subject: [Tutor] Which is better Practice and why In-Reply-To: References: Message-ID: On Mon, Oct 22, 2012 at 7:54 AM, Devin Jeanpierre wrote: > > The second one is used if your importable modules are also scripts > that can be executed. > > That's a bad idea, because it results in the same source file being > executed twice, producing distinct classes that break typechecking. Here's an exception to the above. On Windows, multiprocessing requires you to gate the setup in "__main__" since the module is re-imported in each new process (win32 doesn't fork). From wprins at gmail.com Mon Oct 22 15:23:32 2012 From: wprins at gmail.com (Walter Prins) Date: Mon, 22 Oct 2012 14:23:32 +0100 Subject: [Tutor] Which is better Practice and why In-Reply-To: References: Message-ID: Devin, On 22 October 2012 12:54, Devin Jeanpierre wrote: > On Mon, Oct 22, 2012 at 7:45 AM, Matthew Ngaha wrote: >> the 2nd one usually includes a lot more code then i showed. can you please >> tell me why different methods are used to access the main code? is it just >> preference or is one way actually better coding practice? > > The second one is used if your importable modules are also scripts > that can be executed. > > That's a bad idea, because it results in the same source file being > executed twice, producing distinct classes that break typechecking. If > you have "myprogram.py" containing the following source code, and > execute it as a program, it will terminate with an uncaught MyError. > > class MyError(Exception): pass > > import myprogram > try: raise myprogram.MyError > except MyError: pass Why would you do this though? Is the source of the problem not really that you're importing a module inside itself? Why would you generally want to do this? Furthermore the 'if __name__ == "__main__"' idiom is explicitly part of Python and generally well accepted (so it seems to me at least), so the above seems like a fairly esoteric objection to its use? Thanks, Walter From oscar.j.benjamin at gmail.com Mon Oct 22 15:35:06 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 22 Oct 2012 14:35:06 +0100 Subject: [Tutor] Which is better Practice and why In-Reply-To: References: Message-ID: On 22 October 2012 12:54, Devin Jeanpierre wrote: > On Mon, Oct 22, 2012 at 7:45 AM, Matthew Ngaha wrote: Hi Devin, your context was missing the crucial part showing the "2nd one": >> they call the main program by simply calling the main function. I've also >> seen a more complcated: >> >> if __name__ == '__main__': >> main() >> the 2nd one usually includes a lot more code then i showed. can you please >> tell me why different methods are used to access the main code? is it just >> preference or is one way actually better coding practice? > > The second one is used if your importable modules are also scripts > that can be executed. > > That's a bad idea, because it results in the same source file being > executed twice, producing distinct classes that break typechecking. If > you have "myprogram.py" containing the following source code, and > execute it as a program, it will terminate with an uncaught MyError. There is nothing wrong with having a script that can also be imported (or a module that can also be executed). I do this often, particularly when I have a number of related scripts in the same folder as one another. Python allows a .py file to represent a script or a module. Python itself maintains no formal distinction between scripts and modules. Adding if __name__ == "__main__" allows your script/module to determine whether it is being executed or imported and do different things in each case. One case where this is good is to add a test suite to a module. For example my module defines some functions that can be imported by other modules. It also defines some code to test that the functions it defines are working properly. I don't want to run the tests every time I import the module but I can run them conveniently if I put them in a __name__ == "__main__" block and then execute the module as a script every now and then. Both the unittest and doctest modules supply functions specifically for use in this way. Another common case is that your module defines some functions that are of general use but you also make it possible to run the module as a script in order to perform some of the most common operations that your module performs. There are several examples in the Python standard library that do this for example pdb and cProfile. It is also sometimes useful to define a number of scripts that are in the same directory but share some code by importing one another. You need the if __name__ =="__main__" block for this. The problem that Devin is referring to only happens in certain odd situations where the script that you run ends up importing itself (perhaps indirectly). I have never personally had that problem though. Oscar From eryksun at gmail.com Mon Oct 22 17:14:55 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 22 Oct 2012 11:14:55 -0400 Subject: [Tutor] Which is better Practice and why In-Reply-To: References: Message-ID: On Mon, Oct 22, 2012 at 9:35 AM, Oscar Benjamin wrote: > > It is also sometimes useful to define a number of scripts that are in > the same directory but share some code by importing one another. You > need the if __name__ =="__main__" block for this. > > The problem that Devin is referring to only happens in certain odd > situations where the script that you run ends up importing itself > (perhaps indirectly). I have never personally had that problem though. Just to clarify that I'm following you, would you count the following as a script importing itself 'indirectly'? Assume two modules in the same directory, mod1.py and mod2.py, can both act as the main entry point, and both import each other (assume no circular import problem). Then if mod1.py runs as __main__ and imports mod2, and mod2.py imports mod1, there are 2 copies of the classes and functions defined in mod1.py. For example, there's __main__.MyError vs mod1.MyError. I agree there's no problem if no other module imports the module currently running as __main__. In that case, there's nothing technically wrong with mixing script functionality into a module that can also be imported. Self-contained test cases are a clear example of this. Also, modules can "import __main__", but it's not used frequently. I count 16 cases of this in the 2.7.3 standard library, for introspective usage such as in idlelib, cProfile, pdb, etc. From eryksun at gmail.com Mon Oct 22 17:32:35 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 22 Oct 2012 11:32:35 -0400 Subject: [Tutor] Which is better Practice and why In-Reply-To: References: Message-ID: On Mon, Oct 22, 2012 at 11:14 AM, eryksun wrote: > > Just to clarify that I'm following you, would you count the following > as a script importing itself 'indirectly'? > > Assume two modules in the same directory, mod1.py and mod2.py, can > both act as the main entry point, and both import each other (assume > no circular import problem). Then if mod1.py runs as __main__ and > imports mod2, and mod2.py imports mod1, there are 2 copies of the > classes and functions defined in mod1.py. For example, there's > __main__.MyError vs mod1.MyError. I forgot to specify that __main__ can reference mod1 via mod2.mod1, so my question is if this indirection via mod2 is what you meant by importing itself 'indirectly'. Thanks. From ramit.prasad at jpmorgan.com Mon Oct 22 18:58:44 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Mon, 22 Oct 2012 16:58:44 +0000 Subject: [Tutor] Segmentation Fault shenanigans with wx Gui In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF47416724D09@SCACMX008.exchad.jpmchase.net> Marco Mistroni wrote: > Hello > ?i found the problem. It's? calling self.list.ClearAll that causes the segmentation fault. > removing the call to? ClearAll fixed my problem , but i still want to clear the list before i? load new > data...... > could anyone assist? I think the problem is the way you populate the data. The issue with ClearAll is a side effect of that. See below for more information. > > wkr > ?marco > On Fri, Oct 19, 2012 at 11:05 PM, Marco Mistroni wrote: > Hi all > ?i have written a wx GUI which downloads json data from a server, and populate a listbox. > Every time i populate a listbox, i am receiving Segmentation Faults. > I have tried to retrieve data from the URL via separate thread, and to use events, but i am still getting a > segmentation fault > > could anyone assist pls? > > here's relevant code. I m running python on Ubuntu 10.04 > [snip] > > ??? self.list = AutoWidthListCtrl(panel)' This mailing list is for learning Python. Support and familiarity with 3rd party packages is limited. You will probably be better off asking the wxpython mailing list. Especially as the AutoWidthListCtrl is not a widget that comes with wxpython. [snip] > > ??? def OnResult(self, event): > ??? print 'onResult' > ??? ptf_data = event.data > ??? for item in ptf_data['portfolio']['items']: > ??? ??? index = self.list.InsertStringItem(sys.maxint, item['ticker']) I believe this is the source of your problem. You are using the incorrect value as the index. If you look at documentation for the base class ListCtrl [0][1] you will notice that the first argument to InsertStringItem is the insertion index. Instead you are using the same value (sys.maxint) for all items you are inserting. sys.maxint represents the maximum integer supported by the system and is not relevant for what you are trying to do. Instead use an incrementing count. rows = self.list.GetItemCount() for index, item in enumerate( ptf_data['portfolio']['items'], rows ): self.list.InsertStringItem(index, item['ticker']) [0]: http://wiki.wxpython.org/ListControls [1]: http://www.wxpython.org/docs/api/wx.ListCtrl-class.html > ??? ??? self.list.SetStringItem(index, 1, item['name']) > ??? ??? self.list.SetStringItem(index, 2, str(item['movingAvg200'])) > ??? ??? self.list.SetStringItem(index, 3, str(item['movingAvg50'])) > ??? ??? self.list.SetStringItem(index, 4, str(item['price'])) > ??? ??? self.list.SetStringItem(index, 5, str(item['previous'])) > ??? ??? self.list.SetStringItem(index, 6, str(item['price'] - item['previous']) ) > ??? ??? self.list.SetStringItem(index, 7, str(item['totalValue'])) > ??? ??? self.list.SetStringItem(index, 8, str(item['position'])) > Do let us know if the suggestion works or not. Ramit Prasad 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 livingmike at gmail.com Mon Oct 22 19:43:52 2012 From: livingmike at gmail.com (Mike McTernan) Date: Mon, 22 Oct 2012 10:43:52 -0700 Subject: [Tutor] name conventions Message-ID: Hi there, I am doing some online tutorials and have found two approaches to naming things: this_is_a_name and thisIsAName. Which one is the best practice for Python? I am a totally newbie to programming and want to make sure I start with the most common approach. Thanks, mike From eryksun at gmail.com Mon Oct 22 19:55:15 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 22 Oct 2012 13:55:15 -0400 Subject: [Tutor] name conventions In-Reply-To: References: Message-ID: On Mon, Oct 22, 2012 at 1:43 PM, Mike McTernan wrote: > Hi there, > > I am doing some online tutorials and have found two approaches to > naming things: this_is_a_name and thisIsAName. Read PEP 8, specifically the section on Naming Conventions: http://www.python.org/dev/peps/pep-0008/#naming-conventions From emile at fenx.com Mon Oct 22 19:56:12 2012 From: emile at fenx.com (Emile van Sebille) Date: Mon, 22 Oct 2012 10:56:12 -0700 Subject: [Tutor] name conventions In-Reply-To: References: Message-ID: Mike McTernan wrote: > Hi there, > > I am doing some online tutorials and have found two approaches to > naming things: this_is_a_name and thisIsAName. > > Which one is the best practice for Python? I am a totally newbie to > programming and want to make sure I start with the most common > approach. > The official word is pep 8 -- http://www.python.org/dev/peps/pep-0008/ Emile From oscar.j.benjamin at gmail.com Mon Oct 22 20:26:09 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 22 Oct 2012 19:26:09 +0100 Subject: [Tutor] Which is better Practice and why In-Reply-To: References: Message-ID: On 22 October 2012 16:14, eryksun wrote: > On Mon, Oct 22, 2012 at 9:35 AM, Oscar Benjamin > wrote: >> >> It is also sometimes useful to define a number of scripts that are in >> the same directory but share some code by importing one another. You >> need the if __name__ =="__main__" block for this. >> >> The problem that Devin is referring to only happens in certain odd >> situations where the script that you run ends up importing itself >> (perhaps indirectly). I have never personally had that problem though. > > Just to clarify that I'm following you, would you count the following > as a script importing itself 'indirectly'? Yes. > Assume two modules in the same directory, mod1.py and mod2.py, can > both act as the main entry point, and both import each other (assume > no circular import problem). They both import each other. That is a circular import and it can create problems. My advice for this issue is not "avoid importable scripts" but rather "avoid circular imports" (a good idea anyway). > Then if mod1.py runs as __main__ and > imports mod2, and mod2.py imports mod1, there are 2 copies of the > classes and functions defined in mod1.py. For example, there's > __main__.MyError vs mod1.MyError. I agree that this would be problematic. However, if mod1.py and mod2.py both import each another then neither of them is really an independent script. There is no reason why any common code should not be moved into a third module to avoid the circular import. I have used importable scripts many times and never had this problem because I have never used them with circular imports. Typically the action of running the module as a script provides a command line interface that does some common but useful thing with the code in that same module (rather than code imported from other modules). An example from a recent project: I have a module that defines functions for interacting with a particular database. It can be imported by scripts that perform computations based on the contents of the database. It can also be run as a script in which case it provides a command line interface to query/export the contents of the database. This particular module does not import any other modules within the same project so it will never have the circular import problem. There are other importable scripts that depend on the database module but they also don't use circular imports. Oscar From eryksun at gmail.com Mon Oct 22 20:57:27 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 22 Oct 2012 14:57:27 -0400 Subject: [Tutor] Which is better Practice and why In-Reply-To: References: Message-ID: On Mon, Oct 22, 2012 at 2:26 PM, Oscar Benjamin wrote: > > They both import each other. That is a circular import and it can > create problems. My advice for this issue is not "avoid importable > scripts" but rather "avoid circular imports" (a good idea anyway). I agree it's better to refactor common code to a separate module, but if you search the net you'll find lots of well-intentioned advice on how to make circular import scenarios work. You had said "define a number of scripts that are in the same directory but share some code by [importing one another]", and I wanted to be clear about how that related to your comment about the main module indirectly importing itself. Thanks for taking the time to clear that up. From robdewhirst at gmail.com Mon Oct 22 21:20:18 2012 From: robdewhirst at gmail.com (Dewhirst, Rob) Date: Mon, 22 Oct 2012 14:20:18 -0500 Subject: [Tutor] newb help reading lines from csv Message-ID: import csv ifile = open('test.csv', "r") reader = csv.reader(ifile) for row in reader: print row for row in reader: print row ifile.close() This is a simplified version of what I am trying to do - loop through a CSV file twice. Why does the second for loop not execute at all? The first one prints the rows of the file just fine. From lzantal at gmail.com Mon Oct 22 21:28:22 2012 From: lzantal at gmail.com (LZAntal) Date: Mon, 22 Oct 2012 12:28:22 -0700 Subject: [Tutor] newb help reading lines from csv In-Reply-To: References: Message-ID: Hi, On Oct 22, 2012, at 12:20 PM, "Dewhirst, Rob" wrote: > import csv > ifile = open('test.csv', "r") > reader = csv.reader(ifile) > for row in reader: > print row > for row in reader: > print row > ifile.close() > > This is a simplified version of what I am trying to do - loop through > a CSV file twice. Why does the second for loop not execute at all? > The first one prints the rows of the file just fine. > _ I believe csv module uses iterator so you need to run reader.seek(0) between the for loops Laszlo http://twitter.com/LZAntal > ______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From emile at fenx.com Mon Oct 22 21:28:16 2012 From: emile at fenx.com (Emile van Sebille) Date: Mon, 22 Oct 2012 12:28:16 -0700 Subject: [Tutor] newb help reading lines from csv In-Reply-To: References: Message-ID: Dewhirst, Rob wrote: > import csv > ifile = open('test.csv', "r") > reader = csv.reader(ifile) > for row in reader: > print row > for row in reader: > print row > ifile.close() > > This is a simplified version of what I am trying to do - loop through > a CSV file twice. Why does the second for loop not execute at all? > The first one prints the rows of the file just fine. The first pass also exhausts the input feed -- you'll need to rewind or reposition the next line pointer to the start of the file and I suspect the easiest way is to reopen the file. Emile From joel.goldstick at gmail.com Mon Oct 22 21:30:44 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 22 Oct 2012 15:30:44 -0400 Subject: [Tutor] newb help reading lines from csv In-Reply-To: References: Message-ID: On Mon, Oct 22, 2012 at 3:20 PM, Dewhirst, Rob wrote: > import csv > ifile = open('test.csv', "r") > reader = csv.reader(ifile) > for row in reader: > print row > for row in reader: > print row > ifile.close() > > This is a simplified version of what I am trying to do - loop through > a CSV file twice. Why does the second for loop not execute at all? > The first one prints the rows of the file just fine. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor You need to close the file and re open to iterate again. You can't reset the iterator back to the beginning without closing and re-opening -- Joel Goldstick From robdewhirst at gmail.com Mon Oct 22 21:35:58 2012 From: robdewhirst at gmail.com (Dewhirst, Rob) Date: Mon, 22 Oct 2012 14:35:58 -0500 Subject: [Tutor] newb help reading lines from csv In-Reply-To: <50859E31.8040207@doctors.org.uk> References: <50859E31.8040207@doctors.org.uk> Message-ID: Thanks Matt and Lazlo. I knew I must have been missing some counter not being reset. Both of those options work fine. You want to do ifile.seek(0) not reader.seek(0) at least from my testing. On Mon, Oct 22, 2012 at 2:27 PM, Matt Williams wrote: > Dear Rob, > > This caught me out as well for a long time. > > As I understand it, csv.reader is a file-reader, which iterates ONCE over > the file. There may be more elegant solutions, but I do: > > > import csv > ifile = open('test.csv', "r") > reader = csv.reader(ifile) > inData = [] > for row in reader: > inData.append[row] > ifile.close() > > you can now loop through inData to your heart's desire. > > HTH, > > Matt From eryksun at gmail.com Mon Oct 22 21:39:30 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 22 Oct 2012 15:39:30 -0400 Subject: [Tutor] newb help reading lines from csv In-Reply-To: References: Message-ID: On Mon, Oct 22, 2012 at 3:28 PM, LZAntal wrote: > On Oct 22, 2012, at 12:20 PM, "Dewhirst, Rob" wrote: > >> import csv >> ifile = open('test.csv', "r") >> reader = csv.reader(ifile) > > I believe csv module uses iterator so you need to run reader.seek(0) between the for loops You have to reset the file iterator with ifile.seek(0). You might also want a new reader if you care about the line_num attribute, but otherwise you can keep using the same reader. From mmistroni at gmail.com Mon Oct 22 21:40:52 2012 From: mmistroni at gmail.com (Marco Mistroni) Date: Mon, 22 Oct 2012 20:40:52 +0100 Subject: [Tutor] Segmentation Fault shenanigans with wx Gui In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47416724D09@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF47416724D09@SCACMX008.exchad.jpmchase.net> Message-ID: Hello Ramit yes solution worked... thanks and regards marco On Mon, Oct 22, 2012 at 5:58 PM, Prasad, Ramit wrote: > Marco Mistroni wrote: > > > Hello > > i found the problem. It's calling self.list.ClearAll that causes the > segmentation fault. > > removing the call to ClearAll fixed my problem , but i still want to > clear the list before i load new > > data...... > > could anyone assist? > > I think the problem is the way you populate the data. The issue > with ClearAll is a side effect of that. See below for more information. > > > > > wkr > > marco > > On Fri, Oct 19, 2012 at 11:05 PM, Marco Mistroni > wrote: > > Hi all > > i have written a wx GUI which downloads json data from a server, and > populate a listbox. > > Every time i populate a listbox, i am receiving Segmentation Faults. > > I have tried to retrieve data from the URL via separate thread, and to > use events, but i am still getting a > > segmentation fault > > > > could anyone assist pls? > > > > here's relevant code. I m running python on Ubuntu 10.04 > > > [snip] > > > > self.list = AutoWidthListCtrl(panel)' > > This mailing list is for learning Python. Support and familiarity > with 3rd party packages is limited. You will probably be better off > asking the wxpython mailing list. Especially as the AutoWidthListCtrl > is not a widget that comes with wxpython. > > [snip] > > > > def OnResult(self, event): > > print 'onResult' > > ptf_data = event.data > > for item in ptf_data['portfolio']['items']: > > index = self.list.InsertStringItem(sys.maxint, item['ticker']) > > I believe this is the source of your problem. You are using the > incorrect value as the index. If you look at documentation for > the base class ListCtrl [0][1] you will notice that the first > argument to InsertStringItem is the insertion index. Instead you are > using the same value (sys.maxint) for all items you are inserting. > sys.maxint represents the maximum integer supported by the system > and is not relevant for what you are trying to do. Instead use an > incrementing count. > > rows = self.list.GetItemCount() > for index, item in enumerate( ptf_data['portfolio']['items'], rows ): > self.list.InsertStringItem(index, item['ticker']) > > [0]: http://wiki.wxpython.org/ListControls > [1]: http://www.wxpython.org/docs/api/wx.ListCtrl-class.html > > > self.list.SetStringItem(index, 1, item['name']) > > self.list.SetStringItem(index, 2, str(item['movingAvg200'])) > > self.list.SetStringItem(index, 3, str(item['movingAvg50'])) > > self.list.SetStringItem(index, 4, str(item['price'])) > > self.list.SetStringItem(index, 5, str(item['previous'])) > > self.list.SetStringItem(index, 6, str(item['price'] - > item['previous']) ) > > self.list.SetStringItem(index, 7, str(item['totalValue'])) > > self.list.SetStringItem(index, 8, str(item['position'])) > > > > Do let us know if the suggestion works or not. > > Ramit Prasad > > > 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 joel.goldstick at gmail.com Mon Oct 22 21:45:02 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 22 Oct 2012 15:45:02 -0400 Subject: [Tutor] newb help reading lines from csv In-Reply-To: References: <50859E31.8040207@doctors.org.uk> Message-ID: On Mon, Oct 22, 2012 at 3:35 PM, Dewhirst, Rob wrote: > Thanks Matt and Lazlo. I knew I must have been missing some counter > not being reset. Both of those options work fine. > > You want to do ifile.seek(0) not reader.seek(0) at least from my testing. > > > On Mon, Oct 22, 2012 at 2:27 PM, Matt Williams wrote: >> Dear Rob, >> >> This caught me out as well for a long time. >> >> As I understand it, csv.reader is a file-reader, which iterates ONCE over >> the file. There may be more elegant solutions, but I do: >> >> >> import csv >> ifile = open('test.csv', "r") >> reader = csv.reader(ifile) >> inData = [] >> for row in reader: >> inData.append[row] >> ifile.close() >> >> you can now loop through inData to your heart's desire. >> >> HTH, >> >> Matt If you want to learn more about python iterators this is a good place to start: http://docs.python.org/howto/functional.html#iterators or google python iterators. They are pretty cool to understand -- Joel Goldstick From eryksun at gmail.com Mon Oct 22 21:49:51 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 22 Oct 2012 15:49:51 -0400 Subject: [Tutor] newb help reading lines from csv In-Reply-To: References: <50859E31.8040207@doctors.org.uk> Message-ID: On Mon, Oct 22, 2012 at 3:35 PM, Dewhirst, Rob wrote: > >> import csv >> ifile = open('test.csv', "r") >> reader = csv.reader(ifile) >> inData = [] >> for row in reader: >> inData.append[row] >> ifile.close() >> >> you can now loop through inData to your heart's desire. Alternatively: import csv with open('test.csv', 'rb') as ifile: inData = list(csv.reader(ifile)) From dangulko at hotmail.com Mon Oct 22 21:57:12 2012 From: dangulko at hotmail.com (Daniel Gulko) Date: Mon, 22 Oct 2012 19:57:12 +0000 Subject: [Tutor] Help - Using Sort and Join Message-ID: Hi Python Tutor, I have an issue trying to figure out how to print out final answers using sort and join functions. Assignment Specification: make a function that is a magic eight ball emulator. emulator will be a function that returns one of the possible answers. Make another function that runs the emulator over and over again until user wants to quit, printing answer each time. When user quits, present all answers that the user got again, but present them in alphabetical order. Use the join() function available for strings for this. Below is an example output from my code: ask a question (or press 'enter' to quit): will I be rich You may rely on it. ask a question (or press 'enter' to quit): will I be poor It is decidedly so. ask a question (or press 'enter' to quit): why is that Better not tell you now. As my code is written when user presses enter I simply break and print this current output: Have a nice day What I want is when user exits for my code to take all previous answers, joining them and sorting them like below: ask a question (or press 'enter' to quit): You may rely on it. It is decidedly so. Better not tell you now. My code works right now but I am missing the final part in how to join and sort all answers. Any suggestions or examples is helpful as I am very new with Python. Current code is attached for viewing. Thanks, Dan -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: MagicEightBall_ver2.py URL: From d at davea.name Mon Oct 22 22:15:03 2012 From: d at davea.name (Dave Angel) Date: Mon, 22 Oct 2012 16:15:03 -0400 Subject: [Tutor] Help - Using Sort and Join In-Reply-To: References: Message-ID: <5085A947.4040007@davea.name> On 10/22/2012 03:57 PM, Daniel Gulko wrote: > Hi Python Tutor, > > I have an issue trying to figure out how to print out final answers using sort and join functions. > > Assignment Specification: > make a function that is a magic eight ball emulator. emulator will be a function that returns one of the possible answers. Make another function that runs the emulator over and over again until user wants to quit, printing answer each time. When user quits, present all answers that the user got again, but present them in alphabetical order. Use the join() function available for strings for this. > > Below is an example output from my code: > > ask a question (or press 'enter' to quit): will I be rich > You may rely on it. > > ask a question (or press 'enter' to quit): will I be poor > It is decidedly so. > > ask a question (or press 'enter' to quit): why is that > Better not tell you now. > > As my code is written when user presses enter I simply break and print this current output: > Have a nice day > > > What I want is when user exits for my code to take all previous answers, joining them and sorting them like below: > ask a question (or press 'enter' to quit): > You may rely on it. It is decidedly so. Better not tell you now. > > My code works right now but I am missing the final part in how to join and sort all answers. Any suggestions or examples is helpful as I > am very new with Python. > > Current code is attached for viewing. > > Thanks, > > Dan > You should assume that people cannot get an attachment. Some can and some cannot. Just put the code inline, and make sure you're writing a text message. (Your current one is html, another flaw that can cause some to not get it the way you sent it) Anyway, if you want to present a summary at the end, you'll have to build a list as you go. At the beginning of your function, create an empty list. Then append to it each message you're outputting. Then when you fall out of the loop, you can manipulate that list as your assignment says, first sorting it, then using join to create a single string from the list. Also, you don't need an elif, since you already know the condition there will be true. Just use if and else, and just use a break under else. Then outside the loop, you can do as much as you like to give the final information. -- DaveA From oscar.j.benjamin at gmail.com Mon Oct 22 22:18:51 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 22 Oct 2012 21:18:51 +0100 Subject: [Tutor] Help - Using Sort and Join In-Reply-To: References: Message-ID: On 22 October 2012 20:57, Daniel Gulko wrote: > Hi Python Tutor, Hi Daniel, > I have an issue trying to figure out how to print out final answers using > sort and join functions. Do you know how to use sort and join to print a list of strings in alphabetical order? > > Assignment Specification: > make a function that is a magic eight ball emulator. emulator will be a > function that returns one of the possible answers. Make another function > that runs the emulator over and over again until user wants to quit, > printing answer each time. When user quits, present all answers that the > user got again, but present them in alphabetical order. Use the join() > function available for strings for this. > > As my code is written when user presses enter I simply break and print this > current output: > Have a nice day > > What I want is when user exits for my code to take all previous answers, > joining them and sorting them like below: > ask a question (or press 'enter' to quit): > You may rely on it. It is decidedly so. Better not tell you now. The problem is that your not saving the answers so at the point where your program ends they aren't available for you to print them. You should use a list to collect the answers as they are generated. Then you can think about how to use the sort and join functions > > My code works right now but I am missing the final part in how to join and > sort all answers. Any suggestions or examples is helpful as I > am very new with Python. > > Current code is attached for viewing. Your code is not too long to be pasted directly into the email: ''' import random def AskMagicEightBall(): answers = ("As I see it, yes.", "It is certain.", "It is decidedly so.", "Most likely.", "Outlook good.", "Signs point to yes.", "Without a doubt.", "Yes.", "Yes ? definitely.", "You may rely on it.", "Reply hazy, try again.", "Ask again later.", "Better not tell you now.", "Do not count on it.", "My reply is no.", "My sources say no.", "Outlook not so good.", "Very doubtful.") return random.choice(answers) def RunEmulator(): while True: question = raw_input("ask a question (or press 'enter' to quit): ") if question: answers=AskMagicEightBall() print answers elif not question: print "Have a nice day" break RunEmulator() ''' Oscar From ramit.prasad at jpmorgan.com Mon Oct 22 22:31:20 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Mon, 22 Oct 2012 20:31:20 +0000 Subject: [Tutor] newb help reading lines from csv In-Reply-To: References: <50859E31.8040207@doctors.org.uk> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741672521E@SCACMX008.exchad.jpmchase.net> Dewhirst, Rob wrote: > Thanks Matt and Lazlo. I knew I must have been missing some counter > not being reset. Both of those options work fine. > > You want to do ifile.seek(0) not reader.seek(0) at least from my testing. > > > On Mon, Oct 22, 2012 at 2:27 PM, Matt Williams wrote: [snip] Please do not top post. This list's etiquette is to write your response in-line or below the quoted text. Thank you. Ramit Prasad 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 Tue Oct 23 00:15:06 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 23 Oct 2012 09:15:06 +1100 Subject: [Tutor] Which is better Practice and why In-Reply-To: References: Message-ID: <5085C56A.8040701@pearwood.info> On 22/10/12 22:45, Matthew Ngaha wrote: > In many of the tutorial examples ive come across, the main code's program > is never at the top level, but always in a function of some sort. i > understand why but, there is always a different way used to access the main > code, i want to know which is the best. > > > main() > main's code > > #top level > main() Not that. That unconditionally executes main the first time you access the module, *regardless* of whether it is being run as a script or being imported. That is nearly always the wrong thing to do. It's particularly awful because importing the module for a second time will not re-run the main program. You have to exit Python, restart it, and then re-import the module again. > they call the main program by simply calling the main function. I've also > seen a more complcated: > > if __name__ == '__main__': > main() This one is best. It only runs the main function when the module is being run as a script. > the 2nd one usually includes a lot more code then i showed. Yes, that's because when people write proper scripts, they tend to write code for handling errors, checking the command line arguments, etc. When people just slap together a lazy "always execute the main function", they tend to not bother. Or perhaps that's just how I work :) -- Steven From baz at comcast.net Tue Oct 23 00:30:44 2012 From: baz at comcast.net (Bryan A. Zimmer) Date: Mon, 22 Oct 2012 17:30:44 -0500 Subject: [Tutor] Iterators, example (need help) Message-ID: <002101cdb0a4$e03cf9b0$a0b6ed10$@net> Hello, all. I am a long-time programmer with little experience in Python, but am trying to learn. The example program, attached, is a toy app with a GUI that merely prints out environment keys and their associated values. I know there are errors in the program, but I wanted to see if someone can tell me something about the iterator "magic" that this program is trying to use. The whole program was designed on, and written for cygwin (bash shell) under Windows 7. The python version is 2.6.3. I haven't tried it on other platforms but it "should" work under Windows and Linux. I want to understand what I stumbled into here in order to develop the code and use similar techniques in the future. Specifically, is it really necessary to use __iter__ and __next__ as system functions (with the double underscores)? I can program the basic program in any number of ways but was enthralled by the concept of iterators and generators. I know I missed the mark while trying to use the pythonic idiom, but maybe you can help me see where I went astray. Hopefully you will forgive my home-grown oop and GUI skills, I am from the old school of programming, prior to when these became necessities. The original inspiration for the code came from "Dive Into Python", from the section about iterators and classes, from the fibonacci2.py example. Thank you Bryan A. Zimmer -------------------------------------------------------------------- from Tkinter import * import os ignore1=''' This program works to do what I set out to do as a first step BAZ 10/19/2012 ''' class Env: ''' This class represents the environment of the current process''' def __init__(self, master=None): self.d = os.environ.keys() self.v = os.environ.values() self.i = 0 self.y = self.v[self.i] self.x = self.d[self.i] def __iter__(self): self.x = self.d[self.i] self.y = self.v[self.i] return self def __next__(self,master=None): global s1 global s2 self.i += 1 if (self.i < len(self.d)): self.x = self.d[self.i] self.y = self.v[self.i] if (self.x): print self.x, '==>', self.y s1.set(self.x) s2.set(self.y) else: print ("raising Stop Iteration") raise StopIteration return ((self.x, self.y)) class App(object): ''' This is the main GUI app''' def __init__(self,master): self.createWidgets() def createWidgets(self,master=None): global s1 global s2 self.fm1 = Frame(master) self.fm2 = Frame(master) self.fm1.pack(side = TOP, anchor=NW, fill=BOTH,expand=YES) self.fm2.pack(side = TOP, anchor=SW, fill=BOTH,expand=YES) s1=StringVar() s1.set(env.x) self.l1=Label(self.fm1,text='Var:') self.l1.pack(side=LEFT,fill=X,expand=NO,padx=10) e1=Entry(self.fm1,textvariable=s1,width=40) e1.pack(side=LEFT,fill=X,expand=YES,padx=10) self.l2=Label(self.fm2,text='Val: ') self.l2.pack(side=LEFT,fill=X,expand=NO,padx=10) s2=StringVar() s2.set(env.y) e2=Entry(self.fm2,textvariable=s2, width=40) e2.pack(side=LEFT,fill=X,expand=YES,padx=9) self.b1=Button(self.fm2,text='Next',command=env.__next__) self.b1.pack(side=TOP,anchor=S,fill=X,expand=YES,padx=10) if __name__=='__main__': env=Env() root = Tk() root.option_add('*font', ('verdana', 12, 'bold')) root.title("Pack - Example 12") app=App(root) root.mainloop() ---------------------------------------------------------------------------- ------- From ramit.prasad at jpmorgan.com Tue Oct 23 01:32:41 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Mon, 22 Oct 2012 23:32:41 +0000 Subject: [Tutor] Iterators, example (need help) In-Reply-To: <002101cdb0a4$e03cf9b0$a0b6ed10$@net> References: <002101cdb0a4$e03cf9b0$a0b6ed10$@net> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741672738D@SCACMX008.exchad.jpmchase.net> Bryan A. Zimmer wrote: > Hello, all. > > > I am a long-time programmer with little experience in Python, but am > trying to learn. The example program, attached, is a toy app with a > GUI that merely prints out environment keys and their associated > values. > > I know there are errors in the program, but I wanted to see if someone > can tell me something about the iterator "magic" that this program is > trying to use. > > The whole program was designed on, and written for cygwin (bash shell) > under Windows 7. The python version is 2.6.3. I haven't tried it on > other platforms but it "should" work under Windows and Linux. > > I want to understand what I stumbled into here in order to develop the > code and use similar techniques in the future. Specifically, is it > really necessary to use __iter__ and __next__ as system functions > (with the double underscores)? I can program the basic program in any > number of ways but was enthralled by the concept of iterators and > generators. Typically you call iter(obj) and next(iter_object) when you need to call them and not .__iter__() or .__next__(). This applies to many built-in commands like len(obj). > > I know I missed the mark while trying to use the pythonic idiom, but maybe > you can help me see where I went astray. > > Hopefully you will forgive my home-grown oop and GUI skills, I am from > the old school of programming, prior to when these became necessities. Practice (and constructive criticiscm) makes perfect. Also note, I am unfamiliar with Tkinter and am basing some comments off experience with wxpython. Some things will be different but principles should be fairly similar. > > The original inspiration for the code came from "Dive Into Python", > from the section about iterators and classes, from the fibonacci2.py > example. > > Thank you > > Bryan A. Zimmer > -------------------------------------------------------------------- > > from Tkinter import * This is a frowned upon import style as it can easily override existing names. Instead use: import Tkinter as tk # You can change "tk" to something else. # And then access everything in Tkinter as... self.fm1 = tk.Frame() # use tk or whatever you imported Tkinter "as". > import os > > ignore1=''' > > This program works to do what I set out to do as a first step > BAZ 10/19/2012 > > ''' > > > class Env: > ''' This class represents the environment of the current process''' > def __init__(self, master=None): > self.d = os.environ.keys() > self.v = os.environ.values() > self.i = 0 > self.y = self.v[self.i] > self.x = self.d[self.i] Why are you storing these values? They will not update in "realtime". Of course, environment variables may not change so this might not matter. > > > def __iter__(self): > self.x = self.d[self.i] > self.y = self.v[self.i] > return self Environment variables do not seem like something that really needs to be iterable. Although, if I was going to do that I would instead just store os.environ dictionary (instead of keys and values) and iterate over that. Also, if you are going to return self, you need to change `def __next__` to `def next`. At least, that is true for Python 2, but it may not be true in Python 3. >>> e = Env() >>> ie = iter(e) >>> next(ie) TypeError: instance has no next() method >>> for o in ie: ... print o ... TypeError: instance has no next() method >>> for o in e: ... print o ... TypeError: instance has no next() method > > > > def __next__(self,master=None): > global s1 > global s2 > > self.i += 1 This incrementing of i should probably done at the end of the loop, because otherwise you always lose what is stored in self.d[0] /self.v[0]. Or you can set self.i to -1 initially. > if (self.i < len(self.d)): > self.x = self.d[self.i] > self.y = self.v[self.i] > > if (self.x): > print self.x, '==>', self.y > s1.set(self.x) > s2.set(self.y) What are s1 and s2? Why should they be global? Why not return here directly? What happens if the key is None? > else: > print ("raising Stop Iteration") > raise StopIteration > return ((self.x, self.y)) > It seems like you are replicating the functionality for dict.items(). I suppose this is mostly a learning exercise so that is fine, otherwise I would just use a dictionary instead. > > class App(object): > ''' This is the main GUI app''' > def __init__(self,master): > self.createWidgets() > > def createWidgets(self,master=None): > global s1 > global s2 > self.fm1 = Frame(master) > self.fm2 = Frame(master) > self.fm1.pack(side = TOP, anchor=NW, fill=BOTH,expand=YES) > self.fm2.pack(side = TOP, anchor=SW, fill=BOTH,expand=YES) s1 and s2 should be passed in and not be a global variable. If you need to update the data later, then create an update method instead of looking at global variables. > > s1=StringVar() > s1.set(env.x) env is not set in the code and so I have no idea what this refers to. I suppose by the time this gets created there is an env in the global scope, but I think this is chancy and will only work from this script. You cannot reuse or import class App from another program. Instead pass env into the __init__ method. > self.l1=Label(self.fm1,text='Var:') > self.l1.pack(side=LEFT,fill=X,expand=NO,padx=10) > e1=Entry(self.fm1,textvariable=s1,width=40) > e1.pack(side=LEFT,fill=X,expand=YES,padx=10) > > self.l2=Label(self.fm2,text='Val: ') > self.l2.pack(side=LEFT,fill=X,expand=NO,padx=10) > s2=StringVar() > s2.set(env.y) > > e2=Entry(self.fm2,textvariable=s2, width=40) > e2.pack(side=LEFT,fill=X,expand=YES,padx=9) > > self.b1=Button(self.fm2,text='Next',command=env.__next__) I have not run this program, but I am pretty sure this button will not update e1 and e2. Not to mention that you need to store e1/e2 somewhere you can access later. I would just add it to self. Otherwise you will have no way of modifying the widgets. What will happen is env.__next__() will return some value, but it will return it to the default event handler where nothing will happen. You need to create a method that will call env.__next__ and do something like the following (this is untested; while the theory should be sound, putting the code together might need some more changes than listed here). self.b1=Button(self.fm2,text='Next',command=self.handle_button) def handle_button( self, event ): x,y = self.env.__next__() self.e1['text'] = x # Not sure how you set the value in Tkinter. self.e2['text'] = y > self.b1.pack(side=TOP,anchor=S,fill=X,expand=YES,padx=10) > > > if __name__=='__main__': > env=Env() > root = Tk() > root.option_add('*font', ('verdana', 12, 'bold')) > root.title("Pack - Example 12") > app=App(root) This should really have env passed into. app=App(root, env) > root.mainloop() > ---------------------------------------------------------------------------- > ------- > Ramit Prasad 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 Tue Oct 23 02:11:08 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 23 Oct 2012 01:11:08 +0100 Subject: [Tutor] Iterators, example (need help) In-Reply-To: <5B80DD153D7D744689F57F4FB69AF4741672738D@SCACMX008.exchad.jpmchase.net> References: <002101cdb0a4$e03cf9b0$a0b6ed10$@net> <5B80DD153D7D744689F57F4FB69AF4741672738D@SCACMX008.exchad.jpmchase.net> Message-ID: On 23/10/12 00:32, Prasad, Ramit wrote: Most of Ramit's comments are valid, this is just a couple of additional notes. >> from Tkinter import * > > This is a frowned upon import style as it can easily override existing > names. Instead use: > > import Tkinter as tk # You can change "tk" to something else. import * is common for Tkinter apps, but its still better in production code to use the '...as tk' style. >> import os >> >> ignore1=''' >> >> This program works to do what I set out to do as a first step >> BAZ 10/19/2012 >> >> ''' You don't need the ignore= bit. Just make it a doc string in the code. >> class Env: >> ''' This class represents the environment of the current process''' >> def __init__(self, master=None): >> self.d = os.environ.keys() >> self.v = os.environ.values() >> self.i = 0 >> self.y = self.v[self.i] >> self.x = self.d[self.i] > >> def __iter__(self): >> self.x = self.d[self.i] >> self.y = self.v[self.i] >> return self > > Environment variables do not seem like something that really > needs to be iterable. Although, if I was going to do that > I would instead just store os.environ dictionary (instead > of keys and values) and iterate over that. Agreed, I'd rather store the dict directly in the class. > What are s1 and s2? Why should they be global? Why not return > here directly? What happens if the key is None? They are defined in the later code, but I agree they don't need to be global. Accessing values across classes is bad practice. It would be better that if you really want s1,s2 to be global that you define them in global scope outside any of the class definitions. It makes them easier to find! >> else: >> print ("raising Stop Iteration") >> raise StopIteration I assume its only for debugging but printing a message and raising an exception should not be done. Its for the exception handling code to decide if a message is needed. >> class App(object): >> ''' This is the main GUI app''' >> def __init__(self,master): >> self.createWidgets() >> >> def createWidgets(self,master=None): >> global s1 >> global s2 >> self.fm1 = Frame(master) >> self.fm2 = Frame(master) >> self.fm1.pack(side = TOP, anchor=NW, fill=BOTH,expand=YES) >> self.fm2.pack(side = TOP, anchor=SW, fill=BOTH,expand=YES) > > s1 and s2 should be passed in and not be a global variable. > If you need to update the data later, then create an update > method instead of looking at global variables. > >> >> s1=StringVar() >> s1.set(env.x) > > env is not set in the code and so I have no idea what this refers to. It's in the main() function but not declared as global here which is inconsistent and confusing. Again better to put it into the class. > I suppose by the time this gets created there is an env in the global > scope, but I think this is chancy and will only work from this script. > You cannot reuse or import class App from another program. > Instead pass env into the __init__ method. > > I have not run this program, but I am pretty sure this button will > not update e1 and e2. Not to mention that you need to store e1/e2 > somewhere you can access later. This is a bit of Tkinter magic that means the entries are auto updated when the variable values change (and vice versa). That's what a StringVar does. So I think you are Ok here. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From jeanpierreda at gmail.com Tue Oct 23 02:25:12 2012 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Mon, 22 Oct 2012 20:25:12 -0400 Subject: [Tutor] Which is better Practice and why In-Reply-To: <5085C56A.8040701@pearwood.info> References: <5085C56A.8040701@pearwood.info> Message-ID: On Mon, Oct 22, 2012 at 6:15 PM, Steven D'Aprano wrote: > Not that. That unconditionally executes main the first time you access > the module, *regardless* of whether it is being run as a script or > being imported. That is nearly always the wrong thing to do. Recently I've become a fan of executable packages. In __main__.py, it's always the right thing to do. -- Devin From jeanpierreda at gmail.com Tue Oct 23 02:28:27 2012 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Mon, 22 Oct 2012 20:28:27 -0400 Subject: [Tutor] Which is better Practice and why In-Reply-To: References: Message-ID: On Mon, Oct 22, 2012 at 9:23 AM, Walter Prins wrote: > Devin, > > On 22 October 2012 12:54, Devin Jeanpierre wrote: >> On Mon, Oct 22, 2012 at 7:45 AM, Matthew Ngaha wrote: >>> the 2nd one usually includes a lot more code then i showed. can you please >>> tell me why different methods are used to access the main code? is it just >>> preference or is one way actually better coding practice? >> >> The second one is used if your importable modules are also scripts >> that can be executed. >> >> That's a bad idea, because it results in the same source file being >> executed twice, producing distinct classes that break typechecking. If >> you have "myprogram.py" containing the following source code, and >> execute it as a program, it will terminate with an uncaught MyError. >> >> class MyError(Exception): pass >> >> import myprogram >> try: raise myprogram.MyError >> except MyError: pass > > Why would you do this though? Is the source of the problem not really > that you're importing a module inside itself? Why would you generally > want to do this? Furthermore the 'if __name__ == "__main__"' idiom is > explicitly part of Python and generally well accepted (so it seems to > me at least), so the above seems like a fairly esoteric objection to > its use? It's that your module was imported, while it was executed as a script. It's fairly common for modules to import each other for various reasons. Especially if one module is executable, it might do "circular" imports for the purposes of its execution (tests or what have you), and these break because the reverse part of the "circle" actually create a new module rather than doing a circular import. This kind of behavior is pretty much impossible if your scripts are scripts, and your modules are modules. The only downside is that you have to import the code you use in your scripts. -- Devin From steve at pearwood.info Tue Oct 23 02:42:11 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 23 Oct 2012 11:42:11 +1100 Subject: [Tutor] Which is better Practice and why In-Reply-To: References: Message-ID: <5085E7E3.5080403@pearwood.info> On 22/10/12 22:54, Devin Jeanpierre wrote: > On Mon, Oct 22, 2012 at 7:45 AM, Matthew Ngaha wrote: >> the 2nd one usually includes a lot more code then i showed. can you please >> tell me why different methods are used to access the main code? is it just >> preference or is one way actually better coding practice? > > The second one is used if your importable modules are also scripts > that can be executed. > > That's a bad idea, I disagree. See below. > because it results in the same source file being executed twice, Not so. That only happens if your module is executed *and* imported simultaneously. It is perfectly safe to write a module which can be run as a script, or imported, so long as you don't do both at the same time from within a single Python process. Excluding test scripts, I find 145 modules in the Python 2.5 standard library, and 127 in the Python 3.3 std lib, that use the "if __name__ ==" idiom to be both importable and runnable as a script. > producing distinct classes that break typechecking. If > you have "myprogram.py" containing the following source code, and > execute it as a program, it will terminate with an uncaught MyError. > > class MyError(Exception): pass > > import myprogram > try: raise myprogram.MyError > except MyError: pass Then don't do that. There are very few reasons for importing a module from within itself, and those reasons are relatively advanced, e.g.: - circular imports - some types of introspection If you do that, and the module directly or indirectly imports itself while it is running as a script, you may run into trouble. But writing a dual-purpose module that is usable as an importable module or as a stand-alone script is not problematic in itself. > So, in any circumstance where you would use the second one, it's > because you're in a situation where bad things are happening. The bad situation is when you have circular imports, including the case where your module imports itself. > So use the first one always. This is contrary to the advice given in the Fine Manual: http://docs.python.org/library/__main__.html More from Python's creator, GvR, and the Effbot: http://www.artima.com/forums/flat.jsp?forum=106&thread=4829 http://effbot.org/pyfaq/tutor-what-is-if-name-main-for.htm -- Steven From steve at pearwood.info Tue Oct 23 02:43:37 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 23 Oct 2012 11:43:37 +1100 Subject: [Tutor] Iterators, example (need help) In-Reply-To: <002101cdb0a4$e03cf9b0$a0b6ed10$@net> References: <002101cdb0a4$e03cf9b0$a0b6ed10$@net> Message-ID: <5085E839.2040604@pearwood.info> On 23/10/12 09:30, Bryan A. Zimmer wrote: > I know there are errors in the program, but I wanted to see if someone > can tell me something about the iterator "magic" that this program is > trying to use. In simple terms: an iterator is a sequence of items that understands the iterator protocol. If you say to it, "iterator, please give me the next value", it will respond by giving you the next value in the sequence. Of course you don't literally talk to it, you use an object-oriented method call. Or more frequently, you simply define the appropriate "magic methods" and let Python handle the rest. There's nothing that you can do with iterators that can't be done some other way, but they are a great way to process sequences of data without needing to accumulate the data up front, such as in a list. For example, it is trivially easy to produce an iterator that returns a never-ending stream of data. Nearly all magic methods in Python have double leading and trailing underscores. There are lots of them, but 99.9% of the time you don't explicitly use them *except* to define them in your class. Then you either iterate over the object: for item in some_iterator: ... or occasionally pull out a single item: item = next(some_iterator) Iterators have two "dunder" (Double UNDERscore) magic methods: __next__ __iter__ In general, your __iter__ method will be trivially simple: def __iter__(self): return self and most of the logic will be in __next__. I see your __iter__ method is a little more complicated, but I haven't studied it in detail to see if the extra complication is justified. -- Steven From steve at pearwood.info Tue Oct 23 02:56:35 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 23 Oct 2012 11:56:35 +1100 Subject: [Tutor] Creating a list from other lists In-Reply-To: References: <508521F4.1030706@pearwood.info> Message-ID: <5085EB43.4050205@pearwood.info> On 22/10/12 21:52, eryksun wrote: > On Mon, Oct 22, 2012 at 6:37 AM, Steven D'Aprano wrote: >> On 22/10/12 21:21, Saad Javed wrote: >>> >>> for x, y , z in zip(a, b, c): >>> L.extend([x, y, z]) >>> print L >> >> This is not your code, because that gives a SyntaxError. Where is >> the indentation? Indentation is required in Python, if you leave it >> out, your code will not work correctly. > > It's an HTML post. Not according to the version of Thunderbird I am using, which shows it as a plain text email. I suspect that the HTML attachment may be invalid HTML, understandable by Gmail and possibly nothing else. Typical of Google :( -- Steven From oscar.j.benjamin at gmail.com Tue Oct 23 03:17:06 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 23 Oct 2012 02:17:06 +0100 Subject: [Tutor] Iterators, example (need help) In-Reply-To: <5085E839.2040604@pearwood.info> References: <002101cdb0a4$e03cf9b0$a0b6ed10$@net> <5085E839.2040604@pearwood.info> Message-ID: On 23 October 2012 01:43, Steven D'Aprano wrote: > In general, your __iter__ method will be trivially simple: > > def __iter__(self): > return self > > and most of the logic will be in __next__. I see your __iter__ > method is a little more complicated, but I haven't studied it > in detail to see if the extra complication is justified. The __iter__ method performs the same operation that is already performed in __init__ and __next__ so it is redundant. Probably it should reset the counter to restart iteration. However, in the provided code it is never called since the Env instance is never used in a for loop or iter() call. Oscar From jeanpierreda at gmail.com Tue Oct 23 03:17:14 2012 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Mon, 22 Oct 2012 21:17:14 -0400 Subject: [Tutor] Which is better Practice and why In-Reply-To: <5085E7E3.5080403@pearwood.info> References: <5085E7E3.5080403@pearwood.info> Message-ID: On Mon, Oct 22, 2012 at 8:42 PM, Steven D'Aprano wrote: > If you do that, and the module directly or indirectly imports itself > while it is running as a script, you may run into trouble. But writing > a dual-purpose module that is usable as an importable module or as a > stand-alone script is not problematic in itself. Yes. However, it is somewhat hard to guarantee that a module won't indirectly import itself for a large codebase. It certainly sometimes happens by accident. >> So use the first one always. > This is contrary to the advice given in the Fine Manual: > > http://docs.python.org/library/__main__.html > > > More from Python's creator, GvR, and the Effbot: > > http://www.artima.com/forums/flat.jsp?forum=106&thread=4829 > > http://effbot.org/pyfaq/tutor-what-is-if-name-main-for.htm It's very easy for me to overstate my case. Maybe I even have already, just because I wasn't taking very long to explain myself. I have concrete reasons to not use this form. It comes up very rarely, and is not a large issue. Heck, it's not even really worth making a big fuss over. But there is the occasional mess-up where it happens. I am also aware that what I prefer goes against idiom. But to me, this minor concrete use-case beats out idiom. After all, what is the benefit to following this particular idiom? I believe the answer is, "you get to use one .py file instead of two" -- and as I see it, the cases where circular imports might be a problem are precisely those where you don't care too much about the number of files, because you're writing a package anyway. As it happens, I sort of stumbled across this worldview when I was forced into separating modules from scripts with the new __main__ submodule system to make executable packages (So it's even possible for Pythonic code to forgo the name check). It really struck something in me, and I had a (subjective) realization that modules and scripts are fundamentally different, and that there's very little benefit to conflating them. I realize this isn't really a view that's mainstream for Python. Also, I realize it's not a particularly interesting realization -- any C# programmer would tell you this. And presumably, Pythonistas as a group would disagree. (As an aside, I find it interesting how much more beautiful GvR's code in that post becomes if you consider turning the common bits into a @main decorator). -- Devin From steve at pearwood.info Tue Oct 23 02:54:52 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 23 Oct 2012 11:54:52 +1100 Subject: [Tutor] Which is better Practice and why In-Reply-To: References: <5085C56A.8040701@pearwood.info> Message-ID: <5085EADC.9060405@pearwood.info> On 23/10/12 11:25, Devin Jeanpierre wrote: > On Mon, Oct 22, 2012 at 6:15 PM, Steven D'Aprano wrote: >> Not that. That unconditionally executes main the first time you access >> the module, *regardless* of whether it is being run as a script or >> being imported. That is nearly always the wrong thing to do. > > Recently I've become a fan of executable packages. In __main__.py, > it's always the right thing to do. I would disagree there too. I think that unconditionally running main is the wrong thing to do, except perhaps in the most trivial quick-and- dirty scripts. But if the script it that trivial, you might not even bother with a main function at all. package.__main__.py is designed to be run as a script, and not to be imported. But that doesn't mean that there's no good purpose for importing it. If your package is non-trivial, you ought to have tests for it, including package.__main__. Those tests will probably want to import the module, not necessarily run it as a script. -- Steven From jeanpierreda at gmail.com Tue Oct 23 03:53:08 2012 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Mon, 22 Oct 2012 21:53:08 -0400 Subject: [Tutor] Which is better Practice and why In-Reply-To: <5085EADC.9060405@pearwood.info> References: <5085C56A.8040701@pearwood.info> <5085EADC.9060405@pearwood.info> Message-ID: On Mon, Oct 22, 2012 at 8:54 PM, Steven D'Aprano wrote: >> Recently I've become a fan of executable packages. In __main__.py, >> it's always the right thing to do. > > > I would disagree there too. I think that unconditionally running main > is the wrong thing to do, except perhaps in the most trivial quick-and- > dirty scripts. But if the script it that trivial, you might not even > bother with a main function at all. > > package.__main__.py is designed to be run as a script, and not to be > imported. But that doesn't mean that there's no good purpose for > importing it. If your package is non-trivial, you ought to have tests > for it, including package.__main__. Those tests will probably want to > import the module, not necessarily run it as a script. I think you will find that it is never the case that __name__ != '__main__' in a file called "__main__.py". If I want to test something, I put it in another file. My __main__.py file will generally look something like this: from . import game game.Game().run() -- Devin From wprins at gmail.com Tue Oct 23 10:51:25 2012 From: wprins at gmail.com (Walter Prins) Date: Tue, 23 Oct 2012 09:51:25 +0100 Subject: [Tutor] Creating a list from other lists In-Reply-To: <5085EB43.4050205@pearwood.info> References: <508521F4.1030706@pearwood.info> <5085EB43.4050205@pearwood.info> Message-ID: >> It's an HTML post. > > Not according to the version of Thunderbird I am using, which shows it > as a plain text email. > > I suspect that the HTML attachment may be invalid HTML, understandable > by Gmail and possibly nothing else. Typical of Google :( Looking at the email source it clearly shows as being mimetype "multipart" with both a plaintext and HTML part. I don't think you can much criticise GMail for interpreting and attempting to render an HTML email as HTML. The biggest culprit here IMHO was the sending mail client that generated a broken text mode version of the email's intended formatting by dropping the indentation (and if the HTML is broken, generated broken HTML to boot as well.) Walter From steve at pearwood.info Tue Oct 23 11:59:04 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 23 Oct 2012 20:59:04 +1100 Subject: [Tutor] Creating a list from other lists In-Reply-To: References: <508521F4.1030706@pearwood.info> <5085EB43.4050205@pearwood.info> Message-ID: <50866A68.4060001@pearwood.info> On 23/10/12 19:51, Walter Prins wrote: >>> It's an HTML post. >> >> Not according to the version of Thunderbird I am using, which shows it >> as a plain text email. >> >> I suspect that the HTML attachment may be invalid HTML, understandable >> by Gmail and possibly nothing else. Typical of Google :( > > Looking at the email source it clearly shows as being mimetype > "multipart" with both a plaintext and HTML part. I don't think you > can much criticise GMail for interpreting and attempting to render an > HTML email as HTML. Perhaps not, but I can criticize Gmail for offering such an anti-feature in the first place. > The biggest culprit here IMHO was the sending > mail client That would be Gmail, almost certainly. > that generated a broken text mode version of the email's > intended formatting by dropping the indentation (and if the HTML is > broken, generated broken HTML to boot as well.) I don't actually know if the HTML part is broken or not. I saved it and opened it in Firefox, and Firefox rendered it as raw text showing the tags. Perhaps that just means my test was faulty. Either way though, I'm sick to the back teeth of Google-related technologies being user hostile. Whether it is the horror that Google Groups has become, Google's search engine tracking and bubbling you when you search, Google illegally installing tracking cookies AND THEN LYING ABOUT IT to the American FTC, the awfulness of their image search interface (which just about every search engine now apes), and their efforts to link everything you do on the Internet to a Google account which can be matched to your offline identity, I think their motto is now best described as "Do be evil". -- Steven From steve at pearwood.info Tue Oct 23 11:59:59 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 23 Oct 2012 20:59:59 +1100 Subject: [Tutor] Which is better Practice and why In-Reply-To: References: <5085C56A.8040701@pearwood.info> <5085EADC.9060405@pearwood.info> Message-ID: <50866A9F.3050305@pearwood.info> On 23/10/12 12:53, Devin Jeanpierre wrote: > On Mon, Oct 22, 2012 at 8:54 PM, Steven D'Aprano wrote: >> package.__main__.py is designed to be run as a script, and not to be >> imported. But that doesn't mean that there's no good purpose for >> importing it. If your package is non-trivial, you ought to have tests >> for it, including package.__main__. Those tests will probably want to >> import the module, not necessarily run it as a script. > > I think you will find that it is never the case that __name__ != > '__main__' in a file called "__main__.py". I think you'll find that you're wrong there :) [steve at ando ~]$ mkdir package [steve at ando ~]$ touch package/__init__.py [steve at ando ~]$ echo "print(__name__)" > package/__main__.py [steve at ando ~]$ python package __main__ [steve at ando ~]$ python -c "import package.__main__" package.__main__ > If I want to test something, I put it in another file. Yes. And how do you import the __main__.py module to test it, without it executing? > My __main__.py file will generally look something like this: > > from . import game > game.Game().run() Well, that's probably trivial enough that it requires no testing other than "does __main__.py exist?". -- Steven From eryksun at gmail.com Tue Oct 23 12:31:18 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 23 Oct 2012 06:31:18 -0400 Subject: [Tutor] Creating a list from other lists In-Reply-To: References: <508521F4.1030706@pearwood.info> <5085EB43.4050205@pearwood.info> Message-ID: On Tue, Oct 23, 2012 at 4:51 AM, Walter Prins wrote: > > Looking at the email source it clearly shows as being mimetype > "multipart" with both a plaintext and HTML part. I don't think you > can much criticise GMail for interpreting and attempting to render an > HTML email as HTML. The biggest culprit here IMHO was the sending > mail client that generated a broken text mode version of the email's > intended formatting by dropping the indentation (and if the HTML is > broken, generated broken HTML to boot as well.) The message looks to have been sent with Gmail's webmail client. The problem I think starts with Webkit. When the user presses in a rich text box, instead of tabbing between fields on the page, it inserts a element. I tested how Gmail handles this by creating a simple HTML file containing the following:
for x, y , z in zip(a, b, c):
L.extend([x, y, z])
print L
I rendered this in Firefox and copied it into a new rich text email. Gmail stripped out the tab characters before rendering the plain text section as follows: for x, y , z in zip(a, b, c): L.extend([x, y, z]) print L They certainly could be smarter when it comes to spans with "whitespace:pre" styling, but it's better if users would just think to use plain text when posting code. That said, plain text with Gmail has its own pitfalls. To render it correctly with a monospace font in the web client you need to use something like Stylish to hack Gmail's CSS. But, more importantly, you have to be wise to Google's line wrapping at 69 characters (no RFC requires this). It doesn't give you a live preview. Instead you have to manually wrap code at 69 columns in an IDE and paste it into the text box. Otherwise your code will probably get mangled. From jeanpierreda at gmail.com Tue Oct 23 14:11:11 2012 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Tue, 23 Oct 2012 08:11:11 -0400 Subject: [Tutor] Which is better Practice and why In-Reply-To: <50866A9F.3050305@pearwood.info> References: <5085C56A.8040701@pearwood.info> <5085EADC.9060405@pearwood.info> <50866A9F.3050305@pearwood.info> Message-ID: On Tue, Oct 23, 2012 at 5:59 AM, Steven D'Aprano wrote: > I think you'll find that you're wrong there :) Oops. >> If I want to test something, I put it in another file. > Yes. And how do you import the __main__.py module to test it, without > it executing? How do you test the body of code in the "if __name__ == '__main__'" block, without it executing? -- Devin From oscar.j.benjamin at gmail.com Tue Oct 23 14:22:27 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 23 Oct 2012 13:22:27 +0100 Subject: [Tutor] Which is better Practice and why In-Reply-To: References: <5085E7E3.5080403@pearwood.info> Message-ID: On 23 October 2012 02:17, Devin Jeanpierre wrote: > On Mon, Oct 22, 2012 at 8:42 PM, Steven D'Aprano wrote: >> If you do that, and the module directly or indirectly imports itself >> while it is running as a script, you may run into trouble. But writing >> a dual-purpose module that is usable as an importable module or as a >> stand-alone script is not problematic in itself. > > Yes. However, it is somewhat hard to guarantee that a module won't > indirectly import itself for a large codebase. It certainly sometimes > happens by accident. I guess this is the crux of the issue. If your script is part of and depends on a large codebase then you may as well place all of its code elsewhere in the codebase. The script then serves simply as an entry point and its presence in the project won't hinder your ability to move all the code it uses around to suit its relationship to the rest of the codebase. Importable scripts are useful when you have a codebase that is smaller. In particular if your codebase is one single script (a common case) then you should always use if __name__ == "__main__" unless the script is trivial and/or, as Steven says, you're feeling lazy. Doing this means that you can test your script and its functions by importing the script in the interactive interpreter and that you can reuse the script as a module in some other project If your codebase is a single module rather than a script, giving it an if __name__ == "__main__" block allows someone to perform a common task with the module (or perhaps run its tests) using the -m interpreter option without needing to turn a single file project into a double file project: $ python -m mymodule arg1 arg2 This is often better than creating a separate script to serve as a redundant entry point (redundant since the module can already serve as its own entry point). Once you have one importable script A.py you can then place another script B.py in the same folder have B import and use some of A's code. As Eryksun has said making this work both ways (having B also import from A) can lead to problems. If you find yourself wanting to do that then you have probably organised your code badly. As a solution consider moving all of the common code into one of A or B or into a new module/script C. Another factor not mentioned yet is that the use of if __name__ == "__main__" is so ubiquitous that using it in your own script communicates something to most people who read it (including yourself). If I know that X.py is intended to be used as a script and I want to read it to find out how it works, one of the first things I would do is search for that line. My natural assumption is that no non-trivial execution occurs outside of that if-block (this is almost always true if I wrote the script and it is longer than about 10 lines). Oscar From eryksun at gmail.com Tue Oct 23 15:52:13 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 23 Oct 2012 09:52:13 -0400 Subject: [Tutor] Iterators, example (need help) In-Reply-To: <002101cdb0a4$e03cf9b0$a0b6ed10$@net> References: <002101cdb0a4$e03cf9b0$a0b6ed10$@net> Message-ID: On Mon, Oct 22, 2012 at 6:30 PM, Bryan A. Zimmer wrote: > > ignore1=''' > > This program works to do what I set out to do as a first step > BAZ 10/19/2012 > > ''' If this isn't meant to be a module docstring, leave it where it is, but remove the assignment to ignore1. Unassigned triple-quoted strings are the closest thing in Python to C-style /* multi-line comments */. This works because the compiler ignores unassigned literal strings/numbers, as the following example shows: >>> code = compile('''\ ... 0 ... """ ... multi-line ... comment ... """ ... ''', filename='', mode='exec') >>> code.co_consts (None,) If the number 0 and the string were't ignored, they'd be in co_consts. This is a tuple of immutable objects used by Python bytecode, such as literal numbers, strings (including unicode), tuples, and the singleton None. 3.x adds True, False, Ellipsis, and frozenset. (I probably forgot something, but you get the idea.) If the string is meant to be a docstring, on the other hand, put it on the first line before your imports. In this special case, the compiler creates a __doc__ constant, which is assigned as an attribute of the module. Here's a skeleton of the process: >>> code = compile('"a docstring"', '', 'exec') >>> dis.dis(code) 1 0 LOAD_CONST 0 ('a docstring') 3 STORE_NAME 0 (__doc__) 6 LOAD_CONST 1 (None) 9 RETURN_VALUE >>> mod = types.ModuleType('mod') >>> vars(mod) {'__name__': 'mod', '__doc__': None} >>> exec code in vars(mod) >>> vars(mod).keys() ['__builtins__', '__name__', '__doc__'] >>> mod.__doc__ 'a docstring' Executing the code in the module also added __builtins__ to the namespace, so built-in functions and types are available. A normal import would also set the __file__ and __package__ attributes. Regarding your Env class, I hope you'd use os.environ.iteritems() in practice. > def __next__(self,master=None): In 2.x, this should be next(). In 3.x, it's __next__(). http://docs.python.org/library/stdtypes.html#iterator.next http://docs.python.org/py3k/library/stdtypes.html#iterator.__next__ StringVar objects s1 and s2 should be attributes of the App instance, not global variables. Also, as it is, since you aren't rebinding s1 or s2 in next(), there's no technical reason to declare them with the 'global' keyword in next(). http://docs.python.org/reference/simple_stmts.html#the-global-statement Don't needlessly parenthesize expressions in statements such as if, return, etc. Python doesn't require it, so it's just symbol noise. For example, use: if x: return x x, y, d, v, and i are unhelpful attribute names. I strongly recommend using descriptive names such as current_key, current_value. It helps a lot when you're updating code later on, especially if you aren't the original author. As to the next() method itself, I'd keep it simple. Return the key/value tuple if self._index < len(self.keys), and otherwise raise StopIteration. I wouldn't conflate the implementation of next() with updating the StringVar instances s1 and s2. The "Next" button can use a method defined in App to iterate the Env instance and update the strings. From baz at comcast.net Tue Oct 23 23:32:08 2012 From: baz at comcast.net (Bryan A. Zimmer) Date: Tue, 23 Oct 2012 16:32:08 -0500 Subject: [Tutor] iterators, need help Message-ID: <000c01cdb165$dae5d100$90b17300$@net> I just wanted to say thanlk you to those who took the time to answer my request for help. It is very much appreciated. Bryan A Zimmer -------------- next part -------------- An HTML attachment was scrubbed... URL: From computer_dude15 at hotmail.com Wed Oct 24 00:23:04 2012 From: computer_dude15 at hotmail.com (Matthew Dalrymple) Date: Tue, 23 Oct 2012 18:23:04 -0400 Subject: [Tutor] blackJack problem Message-ID: player1.py ----> http://pastebin.com/jzv1Hhs1blackjack.py ----> http://pastebin.com/Vj3sp3Ca ok the problem im having is with the GetBet function...i need it so you start with 1000 it will bet 5 everytime...if i win it adds 5 if i lose it subtracts 5 if we tie nothing changes. I think this is right for the most part except it says that it can call it before reference. I thought by having a value outside of the functions made the value global. Am i somewhat on the right track? any help would be appriciated... Oh i also forgot to mention that even though im not using all the parameters they have to be there because the professor wants to be able to run other peoples "player1.py" files on my game. thanks Matthew D -------------- next part -------------- An HTML attachment was scrubbed... URL: From rbeniga04 at gmail.com Wed Oct 24 00:45:17 2012 From: rbeniga04 at gmail.com (Rufino Beniga) Date: Tue, 23 Oct 2012 15:45:17 -0700 Subject: [Tutor] Retrieving numbers from a text Message-ID: *Problem*: Analyze a chaotic one-dimensional map: Write a program OneDMapAnalyze.py that reads the data in OneDMap.txt. Calculate the mean value of the iterates, printing the result to the terminal; again annotate the output so that it is understandable. Determine the fraction of iterates that have x > 0.5. Also, print out that result. *The previous problem that I have already done:* Iterate a chaotic one-dimensional map: Write a program OneDMapIterate.py that iterates the function f(x) = 4.0 x ( 1 - x ). First, set the initial condition x = 0.3. Then apply f(x) repeatedly to each new x. Iterate for 100 steps, printing on each line the iteration number and the successive x values to file OneDMap.txt. My program: #Imports math functions from math import * #Initial condition for x x = 0.3 #Opens onedmap.txt file and writes to it file = open('onedmap.txt','w') #Loop, replaces old x with new x i amount of times for i in range(1,101): x = 4.0*x*(1.0-x) #Writes contents of loop to file print >> file, 'Iterate number ', i, ': ', x #Closes file file.close() My problem is, I don't know how to get ONLY the numbers from the OneDMap.txt file. Also, assuming that I am able to do just that, how would I get them into an array? -- tell me i can't do it, then i'll prove you wrong! facebook me -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Oct 24 00:46:23 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 23 Oct 2012 23:46:23 +0100 Subject: [Tutor] blackJack problem References: Message-ID: <871ugoq01s.fsf@ubuntu.i-did-not-set--mail-host-address--so-tickle-me> Matthew Dalrymple writes: > I thought by having a value outside > of the functions made the value global. Not quite. To change the global value in a function you also need to declare it as global inside the function. Otherwise it will create a new local variable that has no effect on the global one. Also by declaring a parameter of your function with the same name as the global you confuse things. Don't do that. Am i somewhat on the right track? > > any help would be appriciated... > > Oh i also forgot to mention that even though im not using all the parameters > they have to be there because the professor wants to be able to run other > peoples "player1.py" files on my game. Interesting concept. And if the other peoples files are calling your functions with values in those parameters you will just ignore them I presume? Hmmm, that should produce some interesting results... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ Playing with gnus and possibly screwing it up!! From alan.gauld at btinternet.com Wed Oct 24 01:11:40 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 24 Oct 2012 00:11:40 +0100 Subject: [Tutor] Retrieving numbers from a text In-Reply-To: References: Message-ID: On 23/10/12 23:45, Rufino Beniga wrote: First, you realize I hope that we don;t do homework for you but we will try to help steer you the right way... So the simple answer is... > -- > tell me i can't do it, then i'll prove you wrong! OK, You can't do it. Is that enough? I thought not, read on... > *_The previous problem that I have already done:_* > Iterate a chaotic one-dimensional map: Write a program > OneDMapIterate.py that iterates the function f(x) = 4.0 x ( 1 - x ). > First, set the initial condition x = 0.3. Then apply f(x) repeatedly to > each new x. Iterate for 100 steps, printing on each line the iteration > number and the successive x values to file OneDMap.txt. > > My program: > #Writes contents of loop to file > print >> file, 'Iterate number ', i, ': ', x Note you are writing more than you were asked to to the file. This slightly complicates the second problem... but only slightly. > My problem is, I don't know how to get ONLY the numbers from the > OneDMap.txt file. Also, assuming that I am able to do just that, how > would I get them into an array? One thing at a time. Can you read a line from the file as a string? Can you split a string into its constituent parts? In this case they should be: 'Iterate number ' i ':' x Can you extract the 2nd and 4th items (do you see why writing more than you were asked complicates it?) Can you convert strings to numbers, specifically integers and floats? Now the second question: Can you create a new empty list? Can you append items to that list? Putting them together: Can you write a loop that -reads a line from the file -extracts the data you want -appends it to your list If you can do the above you have pretty much 'proved me wrong'... If not come back with more specific questions. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From nitinainani at gmail.com Wed Oct 24 01:14:10 2012 From: nitinainani at gmail.com (Nitin Ainani) Date: Tue, 23 Oct 2012 19:14:10 -0400 Subject: [Tutor] Doubt! Message-ID: Dear Sir/Madam, I am new to python I have a question. It is as follows: Suppose *s* is a variable and *s* stores empty string s="" Now if we write following statement print(s[0]) # it gives error print(s[0:]) # it does not give error why? Regards, Nitin -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Wed Oct 24 01:16:19 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 24 Oct 2012 10:16:19 +1100 Subject: [Tutor] Retrieving numbers from a text In-Reply-To: References: Message-ID: <50872543.6020304@pearwood.info> On 24/10/12 09:45, Rufino Beniga wrote: > My problem is, I don't know how to get ONLY the numbers from the > OneDMap.txt file. Also, assuming that I am able to do just that, how would > I get them into an array? What we need to see is typical contents of your file. I expect it looks something like this: iterate number 562 0.4532888 iterate number 563 0.3872701 iterate number 564 0.9100345 iterate number 565 0.0027318 iterate number 566 0.6661924 and so forth. In that case, you can read the final column by reading each line, splitting into words, and taking only the last one. Since this is homework, I'll give you the pieces you need to use, you should be able to assemble them into working code yourself. To read each line in a file: for line in open("name of file"): process line Replace "process line" with the actual code you need. To build up a list of numbers: numbers = [] numbers.append(0.3) numbers.append(0.9736) numbers.append(0.3209) Normally you would put the "numbers.append(x)" into a loop, something like this: numbers = [] for item in many_items: value = process(item) numbers.append(value) Hint: reading a file gives you a loop, one for each line. You want to process each line to get a number, then append that number to the list of numbers. You will have lines of text that look like this: line = "iterate number 563 0.3872701" which is a string, you need to extract the last column and turn it into a float. Hints: use line.split() to split the line into four words: words = line.split() print words => prints ['iterate', 'number', '563', '0.3872701'] use words[-1] to extract the last word use function float(word) to convert it from a string to a number. If you put all those pieces together, it should take no more than a dozen lines of code to read the file, and extract the final column into a list of numbers. Good luck! -- Steven From nitinainani at gmail.com Wed Oct 24 01:24:01 2012 From: nitinainani at gmail.com (Nitin Ainani) Date: Tue, 23 Oct 2012 19:24:01 -0400 Subject: [Tutor] Strings. Message-ID: Dear Sir/Madam, I am new to python I have a question. It is as follows: Suppose *s* is a variable and *s* stores empty string s="" Now if we write following statement print(s[0]) # it gives error print(s[0:]) # it does not give error print (s[13:13]) # this too does not give erro why? Regards, Nitin -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Wed Oct 24 01:36:29 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 24 Oct 2012 10:36:29 +1100 Subject: [Tutor] Doubt! In-Reply-To: References: Message-ID: <508729FD.4020009@pearwood.info> On 24/10/12 10:14, Nitin Ainani wrote: > Dear Sir/Madam, > > I am new to python I have a question. It is as follows: > > Suppose *s* is a variable and *s* stores empty string > > s="" > Now if we write following statement > > > print(s[0]) # it gives error > > > print(s[0:]) # it does not give error > > why? Because indexing a string returns the character at that index. If the index is out-of-bounds, there is nothing to return and an error will be raised: py> s = "hello world" py> s[0] 'h' py> s[6] 'w' py> s[100] Traceback (most recent call last): File "", line 1, in IndexError: string index out of range The colon : tells Python to use a slice. Slices look like: [start:end:step] where step defaults to 1 if it is not given, start defaults to 0, and end defaults to the length of the string. py> s = "hello world" py> s[0:5] 'hello' py> s[:5] 'hello' py> s[1:9] 'ello wor' If either start or end is out of bounds, the result is truncated at the ends of the string: py> s[1:100] 'ello world' If start is greater-or-equal to end, it is an empty slice, and the empty string is returned: py> s[6:3] '' So the slice s[0:] says "give me the slice of the string s starting at position 0 and extending up to the end of the string". In the case of the empty string, since the string is empty, there are no characters and every index is out-of-bounds. So s[0] will raise an error. But the slice s[0:] is an empty slice, and will return the empty string. Slicing can be considered to be a short-cut for using a for-loop and range. s[start:end:step] is like this function: def slice_copy(s, start, end=None, step=1): if end is None: end = len(s) new = [] for i in range(start, end, step): new.append(s[i]) return ''.join(new) (except that real slicing can deal with out-of-bounds start and end as well.) If you study slice_copy, you should understand why an empty slice returns the empty string. -- Steven From d at davea.name Wed Oct 24 01:39:10 2012 From: d at davea.name (Dave Angel) Date: Tue, 23 Oct 2012 19:39:10 -0400 Subject: [Tutor] Doubt! In-Reply-To: References: Message-ID: <50872A9E.5020208@davea.name> On 10/23/2012 07:14 PM, Nitin Ainani wrote: > Dear Sir/Madam, > > I am new to python I have a question. It is as follows: > > Suppose *s* is a variable and *s* stores empty string > > s="" > Now if we write following statement > > > print(s[0]) # it gives error There is no 0th character, so it's an error. If they had wanted to avoid an error, they'd have had to return None or some other flag, and that's not pythonic. > > print(s[0:]) # it does not give error A slice is more tolerant, by definition. It gives you all the items in the range you specify, and the resulting string (in this case) is equal to the original one. > why? > > -- DaveA From emile at fenx.com Wed Oct 24 01:39:25 2012 From: emile at fenx.com (emile) Date: Tue, 23 Oct 2012 16:39:25 -0700 Subject: [Tutor] Doubt! In-Reply-To: References: Message-ID: On 10/23/2012 04:14 PM, Nitin Ainani wrote: > Dear Sir/Madam, > > I am new to python I have a question. It is as follows: > > Suppose *s* is a variable and *s* stores empty string > > s="" > Now if we write following statement > > > print(s[0]) # it gives error > > > print(s[0:]) # it does not give error > > why? See http://docs.python.org/tutorial/introduction.html and in particular section 3.1.2 on Strings where you'll find "Degenerate slice indices are handled gracefully: an index that is too large is replaced by the string size, an upper bound smaller than the lower bound returns an empty string." HTH, Emile From rhettnaxel at gmail.com Wed Oct 24 01:48:27 2012 From: rhettnaxel at gmail.com (Alexander) Date: Tue, 23 Oct 2012 19:48:27 -0400 Subject: [Tutor] Strings. In-Reply-To: References: Message-ID: On Tue, Oct 23, 2012 at 7:24 PM, Nitin Ainani wrote: > Dear Sir/Madam, > > I am new to python I have a question. It is as follows: > > Suppose s is a variable and s stores empty string > > emptyString = "" #consider the string to be non-empty, that is, let's say 13 characters long (the number you chose below) string13 = "1234567890ABC" print ( string13 [ 0 ] ) >Now if we write following statement > > print ( s [ 0 ] ) # it gives error ***what error? *** The error and your example can be used to understand indexes. s [ 0 ] is asking for the value of a specific piece of s. s[0] is asking for the value at index Zero of string s. S has nothing inside of it. > > print(s[0:]) # it does not give error #what does it give? > print (s[13:13]) # this too does not give error #What is the output > why? And with your other two examples I suggest understanding slicing. slice! I want the whole pie, myPie [ 0: ] #the : around an index has a special meaning. Try a string with text and observe the output of your questions. aString [ : ] aString [ : 0 ] aStr [ 1: ] etc Check it out, mess around, get back to me. Alexander From d at davea.name Wed Oct 24 01:58:33 2012 From: d at davea.name (Dave Angel) Date: Tue, 23 Oct 2012 19:58:33 -0400 Subject: [Tutor] Strings. In-Reply-To: References: Message-ID: <50872F29.5030804@davea.name> On 10/23/2012 07:48 PM, Alexander wrote: > On Tue, Oct 23, 2012 at 7:24 PM, Nitin Ainani wrote: >> Dear Sir/Madam, >> >> I am new to python I have a question. It is as follows: >> >> Suppose s is a variable and s stores empty string >> >> emptyString = "" > #consider the string to be non-empty, that is, let's say 13 characters > long (the number you chose below) > string13 = "1234567890ABC" > print ( string13 [ 0 ] ) > >> Now if we write following statement >> >> print ( s [ 0 ] ) # it gives error > ***what error? *** > The error and your example can be used to understand indexes. > s [ 0 ] is asking for the value of a specific piece of s. > s[0] is asking for the value at index Zero of string s. > S has nothing inside of it. > >> print(s[0:]) # it does not give error > #what does it give? >> print (s[13:13]) # this too does not give error > #What is the output >> why? > And with your other two examples I suggest understanding slicing. > slice! I want the whole pie, myPie [ 0: ] #the : around an index has > a special meaning. > Try a string with text and observe the output of your questions. > aString [ : ] > aString [ : 0 ] > aStr [ 1: ] > > etc > Check it out, mess around, get back to me. > Alexander Nitin: Or even better, stick to the original thread, with the dubious title "Doubt!" What's the point of starting a second nearly-identical thread within 10 minutes? -- DaveA From breamoreboy at yahoo.co.uk Wed Oct 24 02:03:43 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 24 Oct 2012 01:03:43 +0100 Subject: [Tutor] Strings. In-Reply-To: References: Message-ID: On 24/10/2012 00:24, Nitin Ainani wrote: > Dear Sir/Madam, > > I am new to python I have a question. It is as follows: > > Suppose *s* is a variable and *s* stores empty string Pythonistas prefer the use of name and not variable. > > s="" > Now if we write following statement > > > print(s[0]) # it gives error > > > print(s[0:]) # it does not give error > print (s[13:13]) # this too does not give erro > why? This was the design decision made by Guido van Rossum many, many years ago. Other people have already given you other answers on the other thread that you started with the same question but a different subject, please don't do that, thanks. > > Regards, > Nitin > -- Cheers. Mark Lawrence. From computer_dude15 at hotmail.com Wed Oct 24 02:00:19 2012 From: computer_dude15 at hotmail.com (Matthew D) Date: Tue, 23 Oct 2012 20:00:19 -0400 Subject: [Tutor] blackJack problem In-Reply-To: <871ugoq01s.fsf@ubuntu.i-did-not-set--mail-host-address--so-tickle-me> References: , <871ugoq01s.fsf@ubuntu.i-did-not-set--mail-host-address--so-tickle-me> Message-ID: > To: tutor at python.org > From: alan.gauld at btinternet.com > Date: Tue, 23 Oct 2012 23:46:23 +0100 > Subject: Re: [Tutor] blackJack problem > > Matthew Dalrymple writes: > > > I thought by having a value outside > > of the functions made the value global. > ok...is there an easier way to write this? the reason i was calling that global value the same as the currentBet is because i need to add that value to 5...I have to start at 1000 and if i win add 5 and if i lose subtract 5 and store that value. I have been staring at this stuff for like 3 hours :/ and still cant get even a slight idea as of what should work > Not quite. > To change the global value in a function you also need to declare it as > global inside the function. Otherwise it will create a new local > variable that has no effect on the global one. > > Also by declaring a parameter of your function with the same name as the > global you confuse things. Don't do that. > > > Am i somewhat on the right track? > > > > any help would be appriciated... > > > > Oh i also forgot to mention that even though im not using all the parameters > > they have to be there because the professor wants to be able to run other > > peoples "player1.py" files on my game. > > Interesting concept. And if the other peoples files are calling your > functions with values in those parameters you will just ignore them I > presume? Hmmm, that should produce some interesting results... it should work in theory correct? > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > Playing with gnus and possibly screwing it up!! > > _______________________________________________ > 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 Wed Oct 24 02:23:30 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 24 Oct 2012 01:23:30 +0100 Subject: [Tutor] blackJack problem In-Reply-To: References: , <871ugoq01s.fsf@ubuntu.i-did-not-set--mail-host-address--so-tickle-me> Message-ID: On 24/10/12 01:00, Matthew D wrote: > > > I thought by having a value outside > > > of the functions made the value global. > > > ok...is there an easier way to write this? the reason i was calling that > global value the same as the currentBet is because i need to add that > value to 5... You need to read about global variables and namespaces in Python. But using a global name as a parameter name will not make the function use the global name as the first parameter, in fact the opposite will happen in that the parameter will hide the global value. If you want to pass the global value into your function then call it with the global name as the first argument. If you want to change the global value from your function then you need to declare the name global inside your function: ########## readMe = 42 # global var we will read changeMe=666 # global valuer we will change def foo(param1): print param1 print readMe # accesses the global value def bar(): global changeMe # use global value rather than local print 'old: ', changeMe changeMe = 99 print 'new: ', changeMe print readMe, changeMe # print original values foo(readMe) #prints 42 twice, once as param1, once as global value bar() # prints old and new values print readMe, changeMe # print after values ################## > l...still cant get even a slight idea as of what should work Try the above and variations. If you are still stuck ask here for more detail. You can also try reading the "Whats in a Name?" topic of my tutorial for a different explanation. > > > peoples "player1.py" files on my game. > > > > Interesting concept. And if the other peoples files are calling your > > functions with values in those parameters you will just ignore them I > > presume? Hmmm, that should produce some interesting results... > it should work in theory correct? No, in theory users will get unpredictable results since other people will assume that the values they pass into your functions will influence the result. They will not expect you to ignore them and apply some totally arbitrary values of your own. The theory is that the parameters in a function control the behaviour, not act as simply placeholders for random data. If your teacher wants the function to have certain parameters I suspect he wants you to use them in your function! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From computer_dude15 at hotmail.com Wed Oct 24 05:01:36 2012 From: computer_dude15 at hotmail.com (Matthew D) Date: Tue, 23 Oct 2012 23:01:36 -0400 Subject: [Tutor] blackJack problem In-Reply-To: References: , , <871ugoq01s.fsf@ubuntu.i-did-not-set--mail-host-address--so-tickle-me>, , Message-ID: Thanks Alan Gauld for your help. After taking a little break and coming back it all seemed to make sense...i understand why there is gonna be some really weird outputs to the interchanging of the player1.py files...that really could get interesting LOL...I seemed to have got the program running good now i have only a few pretty simple questions, If you have the GetBet in the player1.py file you have to pass in the total value right?...that is the only way to get the total into the function? the professor told us straight out that he doesnt really know what he is doing...he wanted us to put in (currentBet, previousBet, and win/lose). I think it only makes sense if you put in the total value in as prevousBet. also he wanted us to pass in (player_hand, dealer_hand) in as parameters for the HitStand function...why would we want to total the hand more than once if we already have the total value? wouldn't we want to just pass in those values instead of the hand itself? Im starting to think im not really learning anything from this class...if anything its just confusing me and teaching me the wrong way to go about things thanks again for your help :)> To: tutor at python.org > From: alan.gauld at btinternet.com > Date: Wed, 24 Oct 2012 01:23:30 +0100 > Subject: Re: [Tutor] blackJack problem > > On 24/10/12 01:00, Matthew D wrote: > > > > > I thought by having a value outside > > > > of the functions made the value global. > > > > > ok...is there an easier way to write this? the reason i was calling that > > global value the same as the currentBet is because i need to add that > > value to 5... > > You need to read about global variables and namespaces in Python. > > But using a global name as a parameter name will not make the function > use the global name as the first parameter, in fact the opposite will > happen in that the parameter will hide the global value. > > If you want to pass the global value into your function then call it > with the global name as the first argument. If you want to change the > global value from your function then you need to declare the name global > inside your function: > > ########## > readMe = 42 # global var we will read > changeMe=666 # global valuer we will change > > def foo(param1): > print param1 > print readMe # accesses the global value > > def bar(): > global changeMe # use global value rather than local > print 'old: ', changeMe > changeMe = 99 > print 'new: ', changeMe > > print readMe, changeMe # print original values > > foo(readMe) #prints 42 twice, once as param1, once as global value > > bar() # prints old and new values > > print readMe, changeMe # print after values > > ################## > > > l...still cant get even a slight idea as of what should work > > Try the above and variations. If you are still stuck ask here for more > detail. > > You can also try reading the "Whats in a Name?" topic of my tutorial for > a different explanation. > > > > > peoples "player1.py" files on my game. > > > > > > Interesting concept. And if the other peoples files are calling your > > > functions with values in those parameters you will just ignore them I > > > presume? Hmmm, that should produce some interesting results... > > it should work in theory correct? > > No, in theory users will get unpredictable results since other people > will assume that the values they pass into your functions will influence > the result. They will not expect you to ignore them and apply some > totally arbitrary values of your own. The theory is that the parameters > in a function control the behaviour, not act as simply placeholders for > random data. If your teacher wants the function to have certain > parameters I suspect he wants you to use them in your function! > > -- > 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 wrw at mac.com Wed Oct 24 05:18:38 2012 From: wrw at mac.com (wrw at mac.com) Date: Tue, 23 Oct 2012 23:18:38 -0400 Subject: [Tutor] blackJack problem In-Reply-To: References: <871ugoq01s.fsf@ubuntu.i-did-not-set--mail-host-address--so-tickle-me> Message-ID: On Oct 23, 2012, at 11:01 PM, Matthew D wrote: > Thanks Alan Gauld for your help. After taking a little break and coming back it all seemed to make sense...i understand why there is gonna be some really weird outputs to the interchanging of the player1.py files...that really could get interesting LOL...I seemed to have got the program running good > > now i have only a few pretty simple questions, If you have the GetBet in the player1.py file you have to pass in the total value right?...that is the only way to get the total into the function? the professor told us straight out that he doesnt really know what he is doing...he wanted us to put in (currentBet, previousBet, and win/lose). I think it only makes sense if you put in the total value in as prevousBet. also he wanted us to pass in (player_hand, dealer_hand) in as parameters for the HitStand function...why would we want to total the hand more than once if we already have the total value? wouldn't we want to just pass in those values instead of the hand itself? > > Im starting to think im not really learning anything from this class...if anything its just confusing me and teaching me the wrong way to go about things > > thanks again for your help :) The best way to learn to program is to program. Doing it in the context of a class has several advantages (even if the prof isn't the sharpest tack in the box)? 1) The class discussion of various approaches to the assignment gives you a range of possible solutions, both good and bad 2) The enforced discipline of generating a solution to the assignment makes you think 3) Learning (almost) any programming language forces you to learn how to reduce a problem to discrete tasks and then steps. Once you've learned ANY language, the next one is easier. That process iterates. 4) Python is an ideal language and environment in which to learn. That is, it's a really good starting place. Just saying? -Bill -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Wed Oct 24 05:33:14 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 24 Oct 2012 04:33:14 +0100 Subject: [Tutor] blackJack problem In-Reply-To: References: , , <871ugoq01s.fsf@ubuntu.i-did-not-set--mail-host-address--so-tickle-me>, , Message-ID: On 24/10/2012 04:01, Matthew D wrote: > big top post snipped. > Any chance of your prof teaching you *NOT* to top post and to use plain English while you're at it? -- Cheers. Mark Lawrence. From computer_dude15 at hotmail.com Wed Oct 24 05:36:02 2012 From: computer_dude15 at hotmail.com (Matthew D) Date: Tue, 23 Oct 2012 23:36:02 -0400 Subject: [Tutor] blackJack problem In-Reply-To: References: , , , <871ugoq01s.fsf@ubuntu.i-did-not-set--mail-host-address--so-tickle-me>, , , , , , Message-ID: > To: tutor at python.org > From: breamoreboy at yahoo.co.uk > Date: Wed, 24 Oct 2012 04:33:14 +0100 > Subject: Re: [Tutor] blackJack problem > > On 24/10/2012 04:01, Matthew D wrote: > > > > big top post snipped. > > > > wow was this really necessary? no need to be an ass... > Any chance of your prof teaching you *NOT* to top post and to use plain > English while you're at it? > > -- > Cheers. > > Mark Lawrence. > > _______________________________________________ > 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 breamoreboy at yahoo.co.uk Wed Oct 24 07:16:29 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 24 Oct 2012 06:16:29 +0100 Subject: [Tutor] blackJack problem In-Reply-To: References: , , , <871ugoq01s.fsf@ubuntu.i-did-not-set--mail-host-address--so-tickle-me>, , , , , , Message-ID: On 24/10/2012 04:36, Matthew D wrote: > > > > To: tutor at python.org >> From: breamoreboy at yahoo.co.uk >> Date: Wed, 24 Oct 2012 04:33:14 +0100 >> Subject: Re: [Tutor] blackJack problem >> >> On 24/10/2012 04:01, Matthew D wrote: >>> >> >> big top post snipped. >> >>> >> wow was this really necessary? no need to be an ass... If you can't be bothered to check up before you post and you can't write in plain English then yes. -- Cheers. Mark Lawrence. From alan.gauld at btinternet.com Wed Oct 24 10:01:23 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 24 Oct 2012 09:01:23 +0100 Subject: [Tutor] blackJack problem In-Reply-To: References: , , <871ugoq01s.fsf@ubuntu.i-did-not-set--mail-host-address--so-tickle-me>, , Message-ID: On 24/10/12 04:33, Mark Lawrence wrote: > On 24/10/2012 04:01, Matthew D wrote: > > big top post snipped. > > Any chance of your prof teaching you *NOT* to top post and to use plain > English while you're at it? I didn't think the top posting was a problem here. The bigger issue was that he kept the mainly irrelevant post from me in the message which uses up bandwidth. But the top posted material had very little to do with what followed so I'm not sure where else he would have put it. The English didn't seem that bad to me either, to be honest. We get much worse and cope OK, it's a python tutor list after all, not an English one. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From mortenengv at gmail.com Wed Oct 24 13:15:11 2012 From: mortenengv at gmail.com (Morten Engvoldsen) Date: Wed, 24 Oct 2012 16:45:11 +0530 Subject: [Tutor] Python input function not working in 2.7 version In-Reply-To: References: Message-ID: Hi, I am facing issue with input() of Python 2.7. When i run the program it doesn't display any line to take user input . Below is the code: def user_input() fat_grams = input("How many grams of fat are in one serving? ") total_calories = input("How many total calories are in one serving? ") print("A food product having {0} fat grams and {1} total calories",fat_grams, total_calories); return; def main(): if __name__ == "__main__": main() Also i would like to display fat_grams where {0} place holder is there and total_calories where {1} place holder is there. That is if : fat_grams = 3 total_calories = 270 Output should be: A food product having 3 fat grams and 270 total calories. But the above print function doesn't display the out put in same way. I am new to Python and i appreciate your help in advance. Look forward to hear from your team soon and have a nice day. Regards, Morten -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Wed Oct 24 13:33:11 2012 From: d at davea.name (Dave Angel) Date: Wed, 24 Oct 2012 07:33:11 -0400 Subject: [Tutor] Python input function not working in 2.7 version In-Reply-To: References: Message-ID: <5087D1F7.5050105@davea.name> On 10/24/2012 07:15 AM, Morten Engvoldsen wrote: > Hi, Hi. Welcome to the list. > I am facing issue with input() of Python 2.7. When i run the program it > doesn't display any line to take user input . Below is the code: > > def user_input() Need a trailing colon here. > fat_grams = input("How many grams of fat are in one serving? ") > total_calories = input("How many total calories are in one serving? ") > print("A food product having {0} fat grams and {1} total > calories",fat_grams, > total_calories); > return; > > def main(): > if __name__ == "__main__": main() That "if" line needs to be at top-level. Once you're inside main(), it's a little late to decide whether to call main(). def user_input(): .... def main(): user_input() if __name__ == "__main__": main() > > Also i would like to display fat_grams where {0} place holder is there and > total_calories where {1} place holder is there. > That is if : > fat_grams = 3 > total_calories = 270 > > Output should be: A food product having 3 fat grams and 270 total calories. > > But the above print function doesn't display the out put in same way. I am > new to Python and i appreciate your help in advance. Easiest way to get that is to use the format() method of str. msg = "A sentence has {0} and {1}".format(arg1, arg2) Then you can print msg. > Look forward to hear from your team soon and have a nice day. Actually, we're all volunteers here, and not part of a team. More like a community. > Regards, > Morten > > -- DaveA From breamoreboy at yahoo.co.uk Wed Oct 24 14:22:14 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 24 Oct 2012 13:22:14 +0100 Subject: [Tutor] Python input function not working in 2.7 version In-Reply-To: References: Message-ID: On 24/10/2012 12:15, Morten Engvoldsen wrote: [duplicate question snipped] Thanks for asking the same question here that you asked on c.l.py 38 minutes previously. -- Cheers. Mark Lawrence. From eryksun at gmail.com Wed Oct 24 15:16:37 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 24 Oct 2012 09:16:37 -0400 Subject: [Tutor] Python input function not working in 2.7 version In-Reply-To: References: Message-ID: On Wed, Oct 24, 2012 at 7:15 AM, Morten Engvoldsen wrote: > > I am facing issue with input() of Python 2.7 > .... > > fat_grams = input("How many grams of fat are in one serving? ") > total_calories = input("How many total calories are in one serving? ") > print("A food product having {0} fat grams and {1} total > calories",fat_grams, total_calories); > return; Avoid using input() in Python 2.x. It evaluates arbitrary expressions that can be a security risk at worst, but at least a source of bugs. Instead use raw_input(). This returns an unevaluated string. You can convert it to a number with int(): >>> grams = int(raw_input("How many grams? ")) How many grams? 28 >>> grams 28 Also, "print" is a statement in 2.x; you don't call() it as a function, not unless you import the print() function from __future__: >>> print # print an empty line >>> from __future__ import print_function >>> print # now it's a function Finally, you don't have to terminate any lines with a semicolon. Python's grammar allows it, but it's not idiomatic style. From eryksun at gmail.com Wed Oct 24 17:31:50 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 24 Oct 2012 11:31:50 -0400 Subject: [Tutor] Python input function not working in 2.7 version In-Reply-To: References: Message-ID: On Wed, Oct 24, 2012 at 10:18 AM, Morten Engvoldsen wrote: > > grams = eval(raw_input("How many grams? ")) > > Is it good practice to write code in this way. That's equivalent to using input(). http://docs.python.org/library/functions.html#input It's not generally a good practice. Sometimes it might be desirable to use eval/exec, but there needs to be a good reason. Getting a number from a user is not a good reason. Use int(raw_input()). This might raise a ValueError, so you need to learn how to handle exceptions, as covered in the tutorial: http://docs.python.org/tutorial/errors.html#handling-exceptions From sbjaved at gmail.com Wed Oct 24 18:27:30 2012 From: sbjaved at gmail.com (Saad Javed) Date: Wed, 24 Oct 2012 21:27:30 +0500 Subject: [Tutor] For - if - else loop; print selective output Message-ID: Hi, a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', '21', 'cookies']] for i in a: if (i[1] == '25' or i[1] == '26'): print 'yes' else: print 'Not found' This prints: yes not found I want it to print "yes" for each positive match but nothing for a negative match. However if all matches are negative, I want it to print "Not found" once (That bit the code already does). I do I get it to print "yes" only in a mix result situation? Saad -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Oct 24 18:36:43 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 24 Oct 2012 17:36:43 +0100 Subject: [Tutor] Python input function not working in 2.7 version In-Reply-To: References: Message-ID: On 24/10/12 13:22, Mark Lawrence wrote: > On 24/10/2012 12:15, Morten Engvoldsen wrote: > > [duplicate question snipped] > > Thanks for asking the same question here that you asked on c.l.py 38 > minutes previously. It's a reasonable question for the tutor list and it seems reasonable that somebody on c.l.p would direct him here from there. Let's be glad he asked it, that's what we are here for. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From sbjaved at gmail.com Wed Oct 24 18:43:15 2012 From: sbjaved at gmail.com (Saad Javed) Date: Wed, 24 Oct 2012 21:43:15 +0500 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: Message-ID: Let me modify this example: a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', '21', 'cookies']] for i in a: *b = i[0]* if (i[1] == '25' or i[1] == '26'): print *b* else: print 'Not found' This will output: *jimmy* *Not found* *Not found* * * How do I print positive matches (*jimmy*) only and not (*Not found*). I also want *Not found *to print only *once *if there are no positive matches. On Wednesday, October 24, 2012, Saad Javed wrote: > Hi, > > a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', > '21', 'cookies']] > for i in a: > if (i[1] == '25' or i[1] == '26'): > print 'yes' > else: > print 'Not found' > > This prints: > yes > not found > > I want it to print "yes" for each positive match but nothing for a > negative match. However if all matches are negative, I want it to print > "Not found" once (That bit the code already does). I do I get it to print > "yes" only in a mix result situation? > > Saad > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Oct 24 18:49:06 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 24 Oct 2012 17:49:06 +0100 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: Message-ID: On 24/10/12 17:27, Saad Javed wrote: > a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', > '21', 'cookies']] > for i in a: > if (i[1] == '25' or i[1] == '26'): > print 'yes' > else: > print 'Not found' > > I want it to print "yes" for each positive match but nothing for a > negative match. So far so good. > However if all matches are negative, I want it to print > "Not found" once (That bit the code already does). Only by accident, as you've seen it always prints that if the loop completes. You need to keep a record of any matches and test for that after the loop: matched = False for i in a: if .... print 'yes' matched = True if not matched: print 'Not found' I confess I'm not keen on the else part of a for loop and never use it, I think it leads to more confusion than anything. It doesn't do what most folks seem to expect, it should probably be called 'end' or something similar rather than 'else' IMHO. >>> for n in range(5): ... print n, ... else: ... print 'done!' ... 0 1 2 3 4 done! >>> for n in range(5): ... if n < 3: print n, ... else: break ... else: print 'done!' ... 0 1 2 >>> HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From sbjaved at gmail.com Wed Oct 24 18:59:09 2012 From: sbjaved at gmail.com (Saad Javed) Date: Wed, 24 Oct 2012 21:59:09 +0500 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: Message-ID: Thanks! If not matched: <---- does it mean that "if the value of matched is not true, print Not found"? print "Not found" -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Oct 24 19:19:10 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 24 Oct 2012 18:19:10 +0100 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: Message-ID: On 24/10/12 17:59, Saad Javed wrote: > Thanks! > > If not matched: <---- does it mean that "if the value of matched is not > true, print Not found"? Yes exactly. matched starts off as False and will remain that way until a match is found at which point we set it to True. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From dfjennings at gmail.com Wed Oct 24 19:19:53 2012 From: dfjennings at gmail.com (Don Jennings) Date: Wed, 24 Oct 2012 13:19:53 -0400 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: Message-ID: On Oct 24, 2012, at 12:27 PM, tutor-request at python.org wrote: > Hi, > > a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', > '21', 'cookies']] > for i in a: > if (i[1] == '25' or i[1] == '26'): > print 'yes' > else: > print 'Not found' Suggestion: use names which are meaningful. > > This prints: > yes > not found I'd be very surprised by that. What it should print is: yes Not found (Note the capital letter at the beginning of the last line.) > > I want it to print "yes" for each positive match but nothing for a negative > match. However if all matches are negative, I want it to print "Not found" > once (That bit the code already does). Yes, it prints that text, but do you understand why it does? It's not because two of the items do not match your if statement. > I do I get it to print "yes" only in > a mix result situation? While there are lots of ways to approach this exercise, I suggest that you try this: first, go through the list as you are doing now, checking if any of the items (are these ages, perhaps?) are a "positive match", appending a "yes" to another list (do you know how to create an empty list and append to it?). Then, if that list is not empty, you'll print 'yes' for every item, else you'll print the "Not found" once. For the latter, here is some code to get you started: people = ['Sally', 'Bob'] if people: print "this list contains something" else: print "this list is empty" Take care, Don From joel.goldstick at gmail.com Wed Oct 24 19:30:32 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 24 Oct 2012 13:30:32 -0400 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: Message-ID: On Wed, Oct 24, 2012 at 12:49 PM, Alan Gauld wrote: > On 24/10/12 17:27, Saad Javed wrote: > >> a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', >> '21', 'cookies']] > > >> for i in a: I would change this (which works but I think is simpler >> if (i[1] == '25' or i[1] == '26'): like this if i[1] in ('25', '26'): >> print 'yes' >> else: >> print 'Not found' >> > >> I want it to print "yes" for each positive match but nothing for a >> negative match. > > > So far so good. > > >> However if all matches are negative, I want it to print >> "Not found" once (That bit the code already does). > > > Only by accident, as you've seen it always prints that if the loop > completes. You need to keep a record of any matches and test for that after > the loop: > > matched = False > for i in a: > if .... > print 'yes' > matched = True > > if not matched: > print 'Not found' > > I confess I'm not keen on the else part of a for loop and never use it, I > think it leads to more confusion than anything. It doesn't do what most > folks seem to expect, it should probably be called 'end' or something > similar rather than 'else' IMHO. > >>>> for n in range(5): > ... print n, > ... else: > ... print 'done!' > ... > 0 1 2 3 4 done! >>>> for n in range(5): > ... if n < 3: print n, > ... else: break > ... else: print 'done!' > ... > 0 1 2 >>>> > > > HTH > > -- > 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 -- Joel Goldstick From eryksun at gmail.com Wed Oct 24 19:49:44 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 24 Oct 2012 13:49:44 -0400 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: Message-ID: On Wed, Oct 24, 2012 at 1:30 PM, Joel Goldstick wrote: > >>> if (i[1] == '25' or i[1] == '26'): > > like this > if i[1] in ('25', '26'): Using "in ['25', '26']" also checks a compiled tuple constant: >>> compile("x in ['25', '26']", '', 'eval').co_consts ('25', '26', ('25', '26')) 3.x adds frozenset support: >>> compile("x in {'25', '26'}", '', 'eval').co_consts ('25', '26', frozenset({'25', '26'})) From alan.gauld at btinternet.com Wed Oct 24 21:24:59 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 24 Oct 2012 20:24:59 +0100 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: Message-ID: On 24/10/12 18:49, eryksun wrote: > Using "in ['25', '26']" also checks a compiled tuple constant: > > >>> compile("x in ['25', '26']", '', 'eval').co_consts > ('25', '26', ('25', '26')) > > 3.x adds frozenset support: > > >>> compile("x in {'25', '26'}", '', 'eval').co_consts > ('25', '26', frozenset({'25', '26'})) I confess I don't know what that means! And if I don't I doubt if the OP will either. Can you explain the above in English please? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eryksun at gmail.com Wed Oct 24 22:10:13 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 24 Oct 2012 16:10:13 -0400 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: Message-ID: On Wed, Oct 24, 2012 at 3:24 PM, Alan Gauld wrote: > On 24/10/12 18:49, eryksun wrote: > >> Using "in ['25', '26']" also checks a compiled tuple constant: >> >> >>> compile("x in ['25', '26']", '', 'eval').co_consts >> ('25', '26', ('25', '26')) >> >> 3.x adds frozenset support: >> >> >>> compile("x in {'25', '26'}", '', 'eval').co_consts >> ('25', '26', frozenset({'25', '26'})) > > > I confess I don't know what that means! > And if I don't I doubt if the OP will either. > Can you explain the above in English please? Sorry, I was showing that the compiler (at least for CPython) special cases "in" and "not in" comparisons when the right-hand operand is a list literal of constants. Instead of going to the trouble of building a list every time the code runs, it uses a tuple that's stored in the code object's co_consts attribute. In Python 3, this optimization was extended to set literals constants. I guess I should show that in more detail by disassembling a function instead of manually compiling code. First in Python 2.7.3: >>> def func(x): ... return x in {1, 2, 3} >>> dis.dis(func) 2 0 LOAD_FAST 0 (x) 3 LOAD_CONST 1 (1) 6 LOAD_CONST 2 (2) 9 LOAD_CONST 3 (3) 12 BUILD_SET 3 15 COMPARE_OP 6 (in) 18 RETURN_VALUE Each time the function is called it has to build a set (BUILD_SET) from the constants 1, 2, and 3. Compare that to Python 3.2.3: >>> dis.dis(func) 2 0 LOAD_FAST 0 (x) 3 LOAD_CONST 4 (frozenset({1, 2, 3})) 6 COMPARE_OP 6 (in) 9 RETURN_VALUE Here the compiler stored a frozenset in the code object. So it only has to load the frozenset on the stack instead of building a new set each time the function is called. compile: http://docs.python.org/py3k/library/functions.html#compile frozenset: http://docs.python.org/py3k/library/functions.html#func-frozenset code objects: http://docs.python.org/py3k/reference/datamodel.html#index-51 The optimization is in peephole.c, PyCode_Optimize/tuple_of_constants: http://hg.python.org/cpython/file/3d0686d90f55/Python/peephole.c#l469 http://hg.python.org/cpython/file/3d0686d90f55/Python/peephole.c#l25 From mike at froward.org Wed Oct 24 22:11:49 2012 From: mike at froward.org (Mike) Date: Wed, 24 Oct 2012 15:11:49 -0500 Subject: [Tutor] string formatting Message-ID: <50884B85.4010508@froward.org> I'm in the process of learning python and migrating away from bash scripting. I'm in the process of converting my bash scripts that essentially ssh to another host via shared keys, execute commands remotely, and exit. To do this I started using paramiko but eventually decided to do it w/ subprocess since I am more familiar with that as a result of me converting other scripts. This is what I'm currently working on, the purpose of this is to ssh into another machine, make a tarball of dumped .sql files, save the tarball with the result of timestamp() as the naming convention, then it will eventually clean it up based on file age: http://dpaste.com/817874/ This is the result after executing the script: rev at omega:~/code/beavis/test$ ls bleh1.sql bleh2.sql bleh3.sql ssh-jobs.py rev at omega:~/code/beavis/test$ ./ssh-jobs.py tar: Removing leading `/' from member names /home/rev/code/beavis/test/bleh2.sql /home/rev/code/beavis/test/bleh3.sql tar: /home/rev/code/beavis/test/24.10.2012: Cannot stat: No such file or directory tar: 15\:06\:52.tgz: Cannot stat: No such file or directory tar: Exiting with failure status due to previous errors rev at omega:~/code/beavis/test$ As you can see it looks like its having issues with the way I'm using timestamp() for string formatting, as far as I can tell it is not a tar specific problem? Any help/insight on this would be greatly appreciated. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tsila.hassine at gmail.com Wed Oct 24 23:05:21 2012 From: tsila.hassine at gmail.com (Tsila Hassine) Date: Wed, 24 Oct 2012 23:05:21 +0200 Subject: [Tutor] code to generate my own text captchas Message-ID: Hello all, I am looking for simple python code that will take a given string and distort it, captcha like. it is for artistic purposes, so no verification required. I just need the image q text distortion code. Thanks!!! tsila -- ------------------------------ missdata.org -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Wed Oct 24 23:16:05 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 24 Oct 2012 22:16:05 +0100 Subject: [Tutor] string formatting In-Reply-To: <50884B85.4010508@froward.org> References: <50884B85.4010508@froward.org> Message-ID: On 24/10/2012 21:11, Mike wrote: > I'm in the process of learning python and migrating away from bash > scripting. I'm in the process of converting my bash scripts that > essentially ssh to another host via shared keys, execute commands > remotely, and exit. To do this I started using paramiko but eventually > decided to do it w/ subprocess since I am more familiar with that as a > result of me converting other scripts. > > This is what I'm currently working on, the purpose of this is to ssh > into another machine, make a tarball of dumped .sql files, save the > tarball with the result of timestamp() as the naming convention, then it > will eventually clean it up based on file age: > > http://dpaste.com/817874/ > > This is the result after executing the script: > > rev at omega:~/code/beavis/test$ ls > bleh1.sql bleh2.sql bleh3.sql ssh-jobs.py > > rev at omega:~/code/beavis/test$ ./ssh-jobs.py > tar: Removing leading `/' from member names > /home/rev/code/beavis/test/bleh2.sql > /home/rev/code/beavis/test/bleh3.sql > tar: /home/rev/code/beavis/test/24.10.2012: Cannot stat: No such file or > directory > tar: 15\:06\:52.tgz: Cannot stat: No such file or directory > tar: Exiting with failure status due to previous errors > rev at omega:~/code/beavis/test$ > > As you can see it looks like its having issues with the way I'm using > timestamp() for string formatting, as far as I can tell it is not a tar > specific problem? Any help/insight on this would be greatly appreciated. > You might as well have put your code inline, so here it is before it expires, poor code :( #!/usr/bin/env python import subprocess,time # Start with the basics. hostTarg = "localhost" dirTarg = "/home/rev/code/beavis/test" fileTarg = "/home/rev/code/beavis/test/*.sql" def timestamp(): lt = time.localtime(time.time()) return "%02d.%02d.%04d %02d:%02d:%02d" % (lt[2], lt[1], lt[0], lt[3], lt[4], lt[5]) package = "tar zcvf %s %s/%s.tgz" % (fileTarg, dirTarg, timestamp()) clean = "find %s -type f -mtime +6 -exec rm {} \;" % dirTarg def process(): subprocess.call(["ssh", hostTarg, package]) process() I'd use http://docs.python.org/library/time.html#time.strftime to do the timestamp formatting and put some print statements into the code so you know what's going on. Having said that it looks as if it's barfed on the first colon. I'll soon get corrected if I'm wrong :) -- Cheers. Mark Lawrence. From breamoreboy at yahoo.co.uk Wed Oct 24 23:20:06 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 24 Oct 2012 22:20:06 +0100 Subject: [Tutor] code to generate my own text captchas In-Reply-To: References: Message-ID: On 24/10/2012 22:05, Tsila Hassine wrote: > Hello all, > I am looking for simple python code that will take a given string and > distort it, captcha like. it is for artistic purposes, so no verification > required. I just need the image q text distortion code. > Thanks!!! > tsila > I'll send you some code in exchange for a cheque for ?1000 sterling, payable in advance :) If you can't afford my services I understand that there are several modern tools called "search engines" that help you find this sort of thing. Do you need a tutorial on how to use one of these? -- Cheers. Mark Lawrence. From eryksun at gmail.com Wed Oct 24 23:24:33 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 24 Oct 2012 17:24:33 -0400 Subject: [Tutor] string formatting In-Reply-To: <50884B85.4010508@froward.org> References: <50884B85.4010508@froward.org> Message-ID: On Wed, Oct 24, 2012 at 4:11 PM, Mike wrote: > > tar: /home/rev/code/beavis/test/24.10.2012: Cannot stat: No such file or > directory > tar: 15\:06\:52.tgz: Cannot stat: No such file or directory You have a space in the filename: lt = time.localtime(time.time()) return "%02d.%02d.%04d %02d:%02d:%02d" % (lt[2], lt[1], lt[0], lt[3], lt[4], lt[5]) You could replace it with an underscore, or add double quotes around the filename (e.g. "%s/%s.tgz"): package = 'tar zcvf %s "%s/%s.tgz"' % (fileTarg, dirTarg, timestamp()) Also, it's common to use time.strftime() to format a struct_time as a string. From modulok at gmail.com Thu Oct 25 01:04:00 2012 From: modulok at gmail.com (Modulok) Date: Wed, 24 Oct 2012 17:04:00 -0600 Subject: [Tutor] code to generate my own text captchas In-Reply-To: References: Message-ID: On 10/24/12, Tsila Hassine wrote: > Hello all, > I am looking for simple python code that will take a given string and > distort it, captcha like. it is for artistic purposes, so no verification > required. I just need the image q text distortion code. > Thanks!!! > tsila You might look into writing a shell script or even python script wrapped around ImageMagick. -Modulok- From steve at pearwood.info Thu Oct 25 01:26:09 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 25 Oct 2012 10:26:09 +1100 Subject: [Tutor] code to generate my own text captchas In-Reply-To: References: Message-ID: <50887911.6030607@pearwood.info> On 25/10/12 08:05, Tsila Hassine wrote: > Hello all, > I am looking for simple python code that will take a given string and > distort it, captcha like. You won't find any such simple code, because distorting images is not simple. This is a mailing list for learning how to program in the Python programming language, not for asking random questions that may have some vague connection to Python. For specialist questions like image distortion, you are better off asking on more specialist mailing lists. Start off by using the search engine of your choice, such as Duck Duck Go, Bing, Blekko, Google, Yahoo or Million Short, to search for Python libraries for captchas and image manipulation. Then ask your questions on the specific mailing list for that library, if it has one. If all else fails, try asking on the general Python mailing list, which has a lot more people, and you might get lucky and find somebody who can answer your question. Either: email: python-list at python.org news: comp.lang.python To get you started, there are at least two powerful image manipulation libraries for Python, PIL and PythonMagickWand. http://pypi.python.org/pypi/PIL http://public.procoders.net/PythonMagickWand/docs/html/index.html PythonMagickWand is a wrapper around ImageMagick, so this page on image distortions may help: http://www.imagemagick.org/Usage/distorts/ Good luck! P.S. I found those links with literally fifteen seconds of googling. If you don't make any effort to search out your own information first, you almost certainly will not be taken seriously with your questions. -- Steven From steve at pearwood.info Thu Oct 25 01:37:28 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 25 Oct 2012 10:37:28 +1100 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: Message-ID: <50887BB8.5070407@pearwood.info> On 25/10/12 03:49, Alan Gauld wrote: > I confess I'm not keen on the else part of a for loop and never >use it, I think it leads to more confusion than anything. It >doesn't do what most folks seem to expect, it should probably be >called 'end' or something similar rather than 'else' IMHO. I am keen on it and do use it, but I agree with the second sentence. It took me the longest time to stop thinking of for...else as meaning "run this block of code if the for loop didn't run at all". It really is an astonishingly bad choice of keyword, but it is a useful feature. -- Steven From alan.gauld at btinternet.com Thu Oct 25 02:32:52 2012 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Thu, 25 Oct 2012 01:32:52 +0100 (BST) Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: Message-ID: <1351125172.60513.YahooMailNeo@web87901.mail.ir2.yahoo.com> >>> Using "in ['25', '26']" also checks a compiled tuple constant: >>> >... >>> 3.x adds frozenset support: >>> >>>? ? ? >>> compile("x in {'25', '26'}", '', 'eval').co_consts >>>? ? ? ('25', '26', frozenset({'25', '26'})) >> >> >> I confess I don't know what that means! > >Sorry, I was showing that the compiler (at least for CPython) special >cases "in" and "not in" comparisons when the right-hand operand is a >list literal of constants. Instead of going to the trouble of building >a list every time the code runs, it uses a tuple that's stored in the >code object's co_consts attribute. >Ah, OK that makes sense now, thanks. Not sure that beginners will? make much of it but the gist is that Python will optimise the test. :-) Alan g. -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Thu Oct 25 02:45:18 2012 From: sander.sweers at gmail.com (Sander Sweers) Date: Thu, 25 Oct 2012 02:45:18 +0200 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: Message-ID: <1351125918.7952.27.camel@infirit.lan> Alan Gauld schreef op wo 24-10-2012 om 17:49 [+0100]: > I confess I'm not keen on the else part of a for loop and never use > it, > I think it leads to more confusion than anything. It doesn't do what > most folks seem to expect, it should probably be called 'end' or > something similar rather than 'else' IMHO. > > >>> for n in range(5): > ... print n, > ... else: > ... print 'done!' > ... > 0 1 2 3 4 done! > >>> for n in range(5): > ... if n < 3: print n, > ... else: break > ... else: print 'done!' > ... > 0 1 2 This confused me somewhat and after a little experimenting and looking at the docs [1] I now get what you hint at (but do not spell out :-)). The devil is in the break statement. I _try_ to describe this below based on the docs and the experimentation in the hope that other people find it useful. Loops (for and while) can have an optional else clause. The else clause is always executed *unless* you call the break statement in your loop. If you call a break statement in your loop the else clause is never executed. If the break statement is never called the else clause is executed. break statements in *nested* loops do *not* interfere with the else clause the parent loop and vice versa. It only applies to the current loop the break statement is called in. There is a good example for prime numbers in the docs. Greets Sander [1]http://docs.python.org/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops From alan.gauld at btinternet.com Thu Oct 25 09:50:27 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 25 Oct 2012 08:50:27 +0100 Subject: [Tutor] code to generate my own text captchas In-Reply-To: References: Message-ID: On 24/10/12 22:05, Tsila Hassine wrote: > Hello all, > I am looking for simple python code that will take a given string and > distort it, captcha like. it is for artistic purposes, so no > verification required. I just need the image q text distortion code. A Google search for 'python captcha generator' threw up several options including one on pypi: http://pypi.python.org/pypi/collective.captcha I don't know if it does exactly what you want but there are others. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From aru1683 at gmail.com Thu Oct 25 11:26:53 2012 From: aru1683 at gmail.com (Arumugam N) Date: Thu, 25 Oct 2012 14:56:53 +0530 Subject: [Tutor] Help on Remote File Copy & Exection Message-ID: Hi All, First of the big thanks and congrats for managing such a brilliant online community. I am new to Python and have started getting the taste of python on my day to day work. I have a requirement and i am trying to solve it using python. I am from QA. Here is what i do daily and wanted to do with python automatically. 1. Revert the snapshot of a VM used for testing. - i have automated using pysphere 2. Copy the build from share location to the VM - here i can have a python script run from the VM but is it possible to run it remotely? for example. if i run the script from Machine A, it should revert the snapshot of machine B and copy the build to Machine B from shared location. 3. Run the installer and clikc Next Button of the installation GUI. - Any idea how to automate this ? Regards, Aru -------------- next part -------------- An HTML attachment was scrubbed... URL: From oscar.j.benjamin at gmail.com Thu Oct 25 11:38:47 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 25 Oct 2012 10:38:47 +0100 Subject: [Tutor] Help on Remote File Copy & Exection In-Reply-To: References: Message-ID: On 25 October 2012 10:26, Arumugam N wrote: > Hi All, > > First of the big thanks and congrats for managing such a brilliant online > community. I am new to Python and have started getting the taste of python > on my day to day work. > > I have a requirement and i am trying to solve it using python. > > I am from QA. Here is what i do daily and wanted to do with python > automatically. > > 1. Revert the snapshot of a VM used for testing. - i have automated using > pysphere > 2. Copy the build from share location to the VM - here i can have a python > script run from the VM but is it possible to run it remotely? for example. > if i run the script from Machine A, it should revert the snapshot of machine > B and copy the build to Machine B from shared location. > 3. Run the installer and clikc Next Button of the installation GUI. - Any > idea how to automate this ? These questions are probably more suited to a different mailing list as this one is predominantly for helping in learning the elementary aspects of programming in Python. If you can perhaps ask a more specific Python question it might be appropriate here. Otherwise I suggest that you either: a) Ask on the pysphere mailing list (I have no idea what pysphere is so this may not be appropriate) b) Use a search engine to find a project that already does what you want c) Ask for general help on python-list where people may know of a good way to do what you want in Python Oscar From pasokan at talentsprint.com Thu Oct 25 12:15:48 2012 From: pasokan at talentsprint.com (Asokan Pichai) Date: Thu, 25 Oct 2012 15:45:48 +0530 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: Message-ID: <20121025101548.GA13809@talentsprint.com> On Wed, Oct 24, 2012 at 09:27:30PM +0500, Saad Javed wrote: > Hi, > > a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', > '21', 'cookies']] > for i in a: > if (i[1] == '25' or i[1] == '26'): > print 'yes' > else: > print 'Not found' > > This prints: > yes > not found > > I want it to print "yes" for each positive match but nothing for a negative > match. However if all matches are negative, I want it to print "Not found" > once (That bit the code already does). I do I get it to print "yes" only in > a mix result situation? > > Saad I am sure you have some answer(s) already. I wanted to add a different solution ------------------------ a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', '21', 'cookies']] matches = [ i[1] in ('25', '26') for i in a ] ] if not any(matches): print "Not found" else: for match in matches: if match: print "Yes" ------------------------ Asokan Pichai -- From alan.gauld at btinternet.com Thu Oct 25 19:03:56 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 25 Oct 2012 18:03:56 +0100 Subject: [Tutor] Help on Remote File Copy & Exection In-Reply-To: References: Message-ID: On 25/10/12 10:26, Arumugam N wrote: > 1. Revert the snapshot of a VM used for testing. - i have automated > using pysphere > 2. Copy the build from share location to the VM - here i can have a > python script run from the VM but is it possible to run it remotely? for > example. if i run the script from Machine A, it should revert the > snapshot of machine B and copy the build to Machine B from shared location. This should be possible using the same commands you'd use if done from a command line. Or if the VM is accessible via the network you might be able to do it that way. But given we have no information about the environment - what OS, what VM etc? Its impossible to say. Its also a bit off the normal topics for the tutor lkist > 3. Run the installer and clikc Next Button of the installation GUI. - > Any idea how to automate this ? subprocess can probably run the installer, clicking a GUI button will depend on the OS. For example in windows you could use ctypes to access the win32 API to send a mouse click message to the installer app... not trivial, but not impossible either. No idea how you'd do it on *nix or MacOS. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From mylesbroomes at hotmail.co.uk Thu Oct 25 20:15:19 2012 From: mylesbroomes at hotmail.co.uk (myles broomes) Date: Thu, 25 Oct 2012 18:15:19 +0000 Subject: [Tutor] Reading from a seperate file Message-ID: I'm trying to code a program that retrieves data from a seperate file but according to my program, the seperate file is empty when I know it clearly isn't. It's a txt file and here are its contents: 120 74 57 44 12 I thought that maybe the problem was the code I had written but even when I try and read from the file in an interactive session in the Python Shell, it does the same thing. I open it in read mode and assign to a variable like so: scoresFile=open('highScores.txt','r') But whenever I try to say read from it: scoresFile.read() It comes up with a blank string: ' ' Can anyone help me? Myles Broomes -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Thu Oct 25 20:26:30 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Thu, 25 Oct 2012 14:26:30 -0400 Subject: [Tutor] Reading from a seperate file In-Reply-To: References: Message-ID: On Thu, Oct 25, 2012 at 2:15 PM, myles broomes wrote: > > I'm trying to code a program that retrieves data from a seperate file but > according to my program, the seperate file is empty when I know it clearly > isn't. It's a txt file and here are its contents: > > 120 > 74 > 57 > 44 > 12 > > I thought that maybe the problem was the code I had written but even when I > try and read from the file in an interactive session in the Python Shell, it > does the same thing. I open it in read mode and assign to a variable like > so: > > scoresFile=open('highScores.txt','r') > > But whenever I try to say read from it: > > scoresFile.read() > > It comes up with a blank string: > > ' ' > > Can anyone help me? > > > Myles Broomes > check your spelling. check that you are in the same directory when your program runs. -- Joel Goldstick From breamoreboy at yahoo.co.uk Thu Oct 25 20:41:34 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 25 Oct 2012 19:41:34 +0100 Subject: [Tutor] Reading from a seperate file In-Reply-To: References: Message-ID: On 25/10/2012 19:15, myles broomes wrote: > > I'm trying to code a program that retrieves data from a seperate file but according to my program, the seperate file is empty when I know it clearly isn't. It's a txt file and here are its contents: 120 > 74 > 57 > 44 > 12 > I thought that maybe the problem was the code I had written but even when I try and read from the file in an interactive session in the Python Shell, it does the same thing. I open it in read mode and assign to a variable like so: scoresFile=open('highScores.txt','r') But whenever I try to say read from it: scoresFile.read() It comes up with a blank string: ' ' Can anyone help me? Myles Broomes > Works fine for me. c:\Users\Mark\MyPython>type highScores.txt 120 74 57 44 12 c:\Users\Mark\MyPython>type mytest.py from __future__ import print_function, division scoresFile=open('highScores.txt','r') print(scoresFile.read()) c:\Users\Mark\MyPython>py -3 mytest.py 120 74 57 44 12 So unless you show us exactly what you've tried we'll find it difficult if not impossible to diagnose your problem. -- Cheers. Mark Lawrence. From breamoreboy at yahoo.co.uk Thu Oct 25 20:53:33 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 25 Oct 2012 19:53:33 +0100 Subject: [Tutor] Reading from a seperate file In-Reply-To: References: Message-ID: On 25/10/2012 19:26, Joel Goldstick wrote: > On Thu, Oct 25, 2012 at 2:15 PM, myles broomes > wrote: >> >> I'm trying to code a program that retrieves data from a seperate file but >> according to my program, the seperate file is empty when I know it clearly >> isn't. It's a txt file and here are its contents: >> >> 120 >> 74 >> 57 >> 44 >> 12 >> >> I thought that maybe the problem was the code I had written but even when I >> try and read from the file in an interactive session in the Python Shell, it >> does the same thing. I open it in read mode and assign to a variable like >> so: >> >> scoresFile=open('highScores.txt','r') >> >> But whenever I try to say read from it: >> >> scoresFile.read() >> >> It comes up with a blank string: >> >> '' >> >> Can anyone help me? >> >> >> Myles Broomes >> > check your spelling. check that you are in the same directory when > your program runs. > > If you've misspelt the filename or you're in the wrong directory surely you'll get something like this depending on your OS. c:\Users\Mark>MyPython\mytest.py Traceback (most recent call last): File "C:\Users\Mark\MyPython\mytest.py", line 2, in scoresFile=open('highScores.txt','r') IOError: [Errno 2] No such file or directory: 'highScores.txt' -- Cheers. Mark Lawrence. From ramit.prasad at jpmorgan.com Thu Oct 25 21:29:29 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Thu, 25 Oct 2012 19:29:29 +0000 Subject: [Tutor] Help on Remote File Copy & Exection In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF4741672F77E@SCACMX008.exchad.jpmchase.net> Arumugam N wrote: > Hi All, > > First of the big thanks and congrats for managing such a brilliant online community. I am new to Python and have > started getting the taste of python on my day to day work. > > I have a requirement and i am trying to solve it using python. > > I am from QA. Here is what i do daily and wanted to do with python automatically. > > 1. Revert the snapshot of a VM used for testing. - i have automated using pysphere I would ask this on the pysphere discussion group: https://groups.google.com/forum/?fromgroups#!forum/pysphere > 2. Copy the build from share location to the VM - here i can have a python script run from the VM but is it > possible to run it remotely? for example. if i run the script from Machine A, it should revert the snapshot of > machine B and copy the build to Machine B from shared location. You should be able to handle file transfer through some libraries, but it depends on the OSes involved and how they are setup. You can always transfer via FTP or SSH. If Machine B is a *nix box, you can SSH in with Paramiko and then use scp/sftp/rsync to transfer and run commands to restart the VM on machine B. > 3. Run the installer and clikc Next Button of the installation GUI. - Any idea how to automate this ? No idea, sorry. > > > Regards, > Aru 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 Thu Oct 25 21:46:52 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Thu, 25 Oct 2012 19:46:52 +0000 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF4741672F7F4@SCACMX008.exchad.jpmchase.net> eryksun wrote: > On Wed, Oct 24, 2012 at 3:24 PM, Alan Gauld wrote: > > On 24/10/12 18:49, eryksun wrote: > > > >> Using "in ['25', '26']" also checks a compiled tuple constant: > >> > >> >>> compile("x in ['25', '26']", '', 'eval').co_consts > >> ('25', '26', ('25', '26')) > >> > >> 3.x adds frozenset support: > >> > >> >>> compile("x in {'25', '26'}", '', 'eval').co_consts > >> ('25', '26', frozenset({'25', '26'})) > > > > > > I confess I don't know what that means! > > And if I don't I doubt if the OP will either. > > Can you explain the above in English please? > > Sorry, I was showing that the compiler (at least for CPython) special > cases "in" and "not in" comparisons when the right-hand operand is a > list literal of constants. Instead of going to the trouble of building > a list every time the code runs, it uses a tuple that's stored in the > code object's co_consts attribute. > > In Python 3, this optimization was extended to set literals constants. > I guess I should show that in more detail by disassembling a function > instead of manually compiling code. > > First in Python 2.7.3: > > >>> def func(x): > ... return x in {1, 2, 3} > > >>> dis.dis(func) > 2 0 LOAD_FAST 0 (x) > 3 LOAD_CONST 1 (1) > 6 LOAD_CONST 2 (2) > 9 LOAD_CONST 3 (3) > 12 BUILD_SET 3 > 15 COMPARE_OP 6 (in) > 18 RETURN_VALUE > > Each time the function is called it has to build a set (BUILD_SET) > from the constants 1, 2, and 3. Compare that to Python 3.2.3: > > >>> dis.dis(func) > 2 0 LOAD_FAST 0 (x) > 3 LOAD_CONST 4 (frozenset({1, 2, 3})) > 6 COMPARE_OP 6 (in) > 9 RETURN_VALUE > > Here the compiler stored a frozenset in the code object. So it only > has to load the frozenset on the stack instead of building a new set > each time the function is called. > > compile: > http://docs.python.org/py3k/library/functions.html#compile > > frozenset: > http://docs.python.org/py3k/library/functions.html#func-frozenset > > code objects: > http://docs.python.org/py3k/reference/datamodel.html#index-51 > > The optimization is in peephole.c, PyCode_Optimize/tuple_of_constants: > > http://hg.python.org/cpython/file/3d0686d90f55/Python/peephole.c#l469 > http://hg.python.org/cpython/file/3d0686d90f55/Python/peephole.c#l25 Thank you for the very detailed (and referenced) clarification. I understood the gist of what you were trying to say in your initial post, but this certainly helps fill in the details. Do you happen to know offhand if there is a difference between `in ` vs. `in ` vs. `in `? Ramit Prasad 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 bgailer at gmail.com Thu Oct 25 22:03:57 2012 From: bgailer at gmail.com (bob gailer) Date: Thu, 25 Oct 2012 16:03:57 -0400 Subject: [Tutor] Reading from a seperate file In-Reply-To: References: Message-ID: <50899B2D.60308@gmail.com> On 10/25/2012 2:15 PM, myles broomes wrote: [snip] Try open('highScores.txt, 'rb'). -- Bob Gailer 919-636-4239 Chapel Hill NC From eryksun at gmail.com Thu Oct 25 22:16:31 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 25 Oct 2012 16:16:31 -0400 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: <5B80DD153D7D744689F57F4FB69AF4741672F7F4@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF4741672F7F4@SCACMX008.exchad.jpmchase.net> Message-ID: On Thu, Oct 25, 2012 at 3:46 PM, Prasad, Ramit wrote: > > Do you happen to know offhand if there is a difference between > `in ` vs. `in ` vs. `in `? The "in" comparison (__contains__ method) is equivalent for list and tuple. It has to search through the sequence item by item, which makes it an O(n) operation. On the other hand, a set/dict uses the hash() of an object to map it to a known location in a table (performance degrades if there are many collisions). On average, you can check if a set/dict contains an item in constant time, i.e. O(1). The amortized worst case is O(n). From oscar.j.benjamin at gmail.com Thu Oct 25 23:39:22 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 25 Oct 2012 22:39:22 +0100 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF4741672F7F4@SCACMX008.exchad.jpmchase.net> Message-ID: On 25 October 2012 21:16, eryksun wrote: > On Thu, Oct 25, 2012 at 3:46 PM, Prasad, Ramit > wrote: >> >> Do you happen to know offhand if there is a difference between >> `in ` vs. `in ` vs. `in `? > > The "in" comparison (__contains__ method) is equivalent for list and > tuple. It has to search through the sequence item by item, which makes > it an O(n) operation. On the other hand, a set/dict uses the hash() of > an object to map it to a known location in a table (performance > degrades if there are many collisions). On average, you can check if a > set/dict contains an item in constant time, i.e. O(1). The amortized > worst case is O(n). Why do you say "*amortized* worst case"? Is there an occasional worse than O(n) operation that is insignificant when amortised? At first I assumed that was a slip of the tongue but you generally seem to know an incredible amount about the implementation of CPython, so now I'm curious. Oscar From ramit.prasad at jpmorgan.com Thu Oct 25 23:55:05 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Thu, 25 Oct 2012 21:55:05 +0000 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF4741672F7F4@SCACMX008.exchad.jpmchase.net> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741672FC66@SCACMX008.exchad.jpmchase.net> eryksun wrote: > On Thu, Oct 25, 2012 at 3:46 PM, Prasad, Ramit > wrote: > > > > Do you happen to know offhand if there is a difference between > > `in ` vs. `in ` vs. `in `? > > The "in" comparison (__contains__ method) is equivalent for list and > tuple. It has to search through the sequence item by item, which makes > it an O(n) operation. On the other hand, a set/dict uses the hash() of > an object to map it to a known location in a table (performance > degrades if there are many collisions). On average, you can check if a > set/dict contains an item in constant time, i.e. O(1). The amortized > worst case is O(n). Sorry, I should have stated that I meant with regards to the optimization that loads these as a literal constant. Ramit Prasad 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 Fri Oct 26 01:08:21 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 26 Oct 2012 10:08:21 +1100 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF4741672F7F4@SCACMX008.exchad.jpmchase.net> Message-ID: <5089C665.3010405@pearwood.info> On 26/10/12 07:16, eryksun wrote: > On Thu, Oct 25, 2012 at 3:46 PM, Prasad, Ramit > wrote: >> >> Do you happen to know offhand if there is a difference between >> `in` vs. `in` vs. `in`? > > The "in" comparison (__contains__ method) is equivalent for list and > tuple. It has to search through the sequence item by item, which makes > it an O(n) operation. On the other hand, a set/dict uses the hash() of > an object to map it to a known location in a table (performance > degrades if there are many collisions). On average, you can check if a > set/dict contains an item in constant time, i.e. O(1). The amortized > worst case is O(n). To be precise: dict lookup, deletion and insertion are amortized O(1) (on average, constant time) assuming there are no hash collisions. When there are collisions, they are O(k) where k is the number of colliding items. So in the worst case where k=n (the number of items in the dict), dicts perform a little worse than lists, but in the typical case, they are usually much, much faster. "Amortized" strictly only refers to deletion and insertion. Since lookups don't modify the dict, a lookup always takes the same time. But deletions and insertions will occasionally trigger a resize of the dict, which obviously takes a lot longer than the non-resizing cases. But if you spread the average cost of the resize over many insertions or deletions, you get amortized O(1) time. -- Steven From steve at pearwood.info Fri Oct 26 01:15:37 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 26 Oct 2012 10:15:37 +1100 Subject: [Tutor] Reading from a seperate file In-Reply-To: References: Message-ID: <5089C819.2070706@pearwood.info> On 26/10/12 05:15, myles broomes wrote: > > I'm trying to code a program that retrieves data from a seperate >file but according to my program, the seperate file is empty when >I know it clearly isn't. It's a txt file and here are its contents: > 120 > 74 > 57 > 44 > 12 > I thought that maybe the problem was the code I had written but >even when I try and read from the file in an interactive session >in the Python Shell, it does the same thing. I open it in read mode >and assign to a variable like so: scoresFile=open('highScores.txt','r') >But whenever I try to say read from it: scoresFile.read() It comes > up with a blank string: ' ' Can anyone help me? > Myles Broomes The symptoms you describe suggest that you are reading from the file twice without closing the file first, or resetting the file pointer. Once the file pointer reaches the end of the file, there's nothing left to read and you get an empty string. Example: py> count = open("demo", "w").write("some text") py> f = open("demo", "r") py> f.read() 'some text' py> f.read() '' py> f.read() '' py> f.seek(0) # move the file pointer back to the start 0 py> f.read() 'some text' -- Steven From oscar.j.benjamin at gmail.com Fri Oct 26 01:57:07 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Fri, 26 Oct 2012 00:57:07 +0100 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: <5089C665.3010405@pearwood.info> References: <5B80DD153D7D744689F57F4FB69AF4741672F7F4@SCACMX008.exchad.jpmchase.net> <5089C665.3010405@pearwood.info> Message-ID: On 26 October 2012 00:08, Steven D'Aprano wrote: > On 26/10/12 07:16, eryksun wrote: >> >> On Thu, Oct 25, 2012 at 3:46 PM, Prasad, Ramit >> wrote: >>> >>> >>> Do you happen to know offhand if there is a difference between >>> `in` vs. `in` vs. `in`? >> >> >> The "in" comparison (__contains__ method) is equivalent for list and >> tuple. It has to search through the sequence item by item, which makes >> it an O(n) operation. On the other hand, a set/dict uses the hash() of >> an object to map it to a known location in a table (performance >> degrades if there are many collisions). On average, you can check if a >> set/dict contains an item in constant time, i.e. O(1). The amortized >> worst case is O(n). > > > To be precise: dict lookup, deletion and insertion are amortized O(1) > (on average, constant time) assuming there are no hash collisions. > > When there are collisions, they are O(k) where k is the number of > colliding items. So in the worst case where k=n (the number of items > in the dict), dicts perform a little worse than lists, but in the > typical case, they are usually much, much faster. The use of big-O notation above seems strange to me. The average value of k is (assuming non-pathological inputs) a constant that is independent of n (for large n). So O(k) really means average case O(1). > "Amortized" strictly only refers to deletion and insertion. Since > lookups don't modify the dict, a lookup always takes the same time. > But deletions and insertions will occasionally trigger a resize of > the dict, which obviously takes a lot longer than the non-resizing > cases. But if you spread the average cost of the resize over many > insertions or deletions, you get amortized O(1) time. You mean that the cost is only considered to be "amortised" when averaging over the cost of the qualitatively distinct operation of resizing the dict among the cost of the non-resizing insertions (does deletion ever trigger a resize?). When averaging over the cases where there are more or less hash collisions but no possibility of resizes we refer to the cost as "average case" but not "amortised". Is that what you mean? Oscar From alan.gauld at btinternet.com Fri Oct 26 02:09:25 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 26 Oct 2012 01:09:25 +0100 Subject: [Tutor] Reading from a seperate file In-Reply-To: References: Message-ID: On 25/10/12 19:15, myles broomes wrote: > but according to my program, the seperate file is empty when I know it > clearly isn't. OK, So show us the program! Otherwise we are just guessing. > Python Shell, it does the same thing. I open it in read mode and assign > to a variable like so: > > scoresFile=open('highScores.txt','r') > > But whenever I try to say read from it: > > scoresFile.read() > > It comes up with a blank string: > > ' ' How do you get a blank string? Calling read() without assigning it to a variable means the content of your file will be lost. But then what are you looking at to say its a blank string? We need to see the actual code you are running not just random fragments. > Can anyone help me? Sure, but you've got to help us first. Show us your code. And if you get an error message show us that too - all of it. And for good measure tell us the Python version and the OS. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From sander.sweers at gmail.com Fri Oct 26 18:14:35 2012 From: sander.sweers at gmail.com (Sander Sweers) Date: Fri, 26 Oct 2012 18:14:35 +0200 Subject: [Tutor] Python and xml *creation* Message-ID: <2973435.tcZpjLDrHN@infirit> Hi List, I am looking for resources on creating xml files in python. I have found some resources but they all are very basic. I find lots of resources online about parsing and searching in an xml file but not so much about creating them. Do any of you know of good online resources? I found an o'reilly book [1] do you know if it is any good? Many thank in advance, Sander [1] http://shop.oreilly.com/product/9780596001285.do From steve at pearwood.info Fri Oct 26 18:30:26 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 27 Oct 2012 03:30:26 +1100 Subject: [Tutor] Python and xml *creation* In-Reply-To: <2973435.tcZpjLDrHN@infirit> References: <2973435.tcZpjLDrHN@infirit> Message-ID: <508ABAA2.9030105@pearwood.info> On 27/10/12 03:14, Sander Sweers wrote: > Do any of you know of good online resources? I found an o'reilly book [1] do > you know if it is any good? > [1] http://shop.oreilly.com/product/9780596001285.do For those of us who have email access but no web access at the moment, or who have access to the O'Reilly site blocked, would you care to tell us which O'Reilly book that is? -- Steven From sander.sweers at gmail.com Fri Oct 26 18:33:16 2012 From: sander.sweers at gmail.com (Sander Sweers) Date: Fri, 26 Oct 2012 18:33:16 +0200 Subject: [Tutor] Python and xml *creation* In-Reply-To: <508ABAA2.9030105@pearwood.info> References: <2973435.tcZpjLDrHN@infirit> <508ABAA2.9030105@pearwood.info> Message-ID: <12068519.HXs0AtGScW@infirit> Op zaterdag 27 oktober 2012 03:30:26 schreef Steven D'Aprano: > On 27/10/12 03:14, Sander Sweers wrote: > > Do any of you know of good online resources? I found an o'reilly book [1] > > do you know if it is any good? > > > > [1] http://shop.oreilly.com/product/9780596001285.do > > For those of us who have email access but no web access at the moment, or > who have access to the O'Reilly site blocked, would you care to tell us > which O'Reilly book that is? It is Python & XML by Christopher A. Jones, Fred L. Drake Jr. Thx Sander From eryksun at gmail.com Fri Oct 26 18:58:47 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 26 Oct 2012 12:58:47 -0400 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF4741672F7F4@SCACMX008.exchad.jpmchase.net> Message-ID: Sorry, Saad; this is now *way* off topic... On Thu, Oct 25, 2012 at 5:39 PM, Oscar Benjamin wrote: > >> degrades if there are many collisions). On average, you can check if a >> set/dict contains an item in constant time, i.e. O(1). The amortized >> worst case is O(n). > > Why do you say "*amortized* worst case"? Is there an occasional worse > than O(n) operation that is insignificant when amortised? > > At first I assumed that was a slip of the tongue Yes, it was badly stated. I've only implemented "separate chaining" hash tables that use slots that are linked lists or dynamic arrays. Python uses an "open addressing" implementation. It seems to me with this implementation the worst case lookup is o(n), where n is the table size. In the worst case you've removed all but 1 key, where all the keys hash the same, and the lookup requires probing through every "dummy key" left in the table (I know this is insanely unrealistic). With separate chaining, on the other hand, the worst case lookup is O(k), where k is the number of keys. Here's a concrete example to give a feel for what the hash table looks like as keys are added and removed. First, define some structures to peek at the underlying table of a set, plus a pathological class whose instances always hash as 37: http://hg.python.org/cpython/file/70274d53c1dd/Include/setobject.h#l21 from ctypes import * Set_MINSIZE = 8 class SetEntry(Structure): _fields_ = [ ('hash', c_long), ('_key', c_void_p), ] @property def key(self): if self._key: return cast(self._key, py_object) else: return 0 class SetObject(Structure): _fields_ = [ ('ob_refcnt', c_ssize_t), ('ob_type', py_object), ('fill', c_ssize_t), ('used', c_ssize_t), ('mask', c_ssize_t), ('table', POINTER(SetEntry)), ('lookup', c_void_p), ('smalltable', SetEntry * Set_MINSIZE), ('hash', c_long), ('weakreflist', c_void_p), ] @classmethod def from_set(cls, s): if not isinstance(s, set): raise TypeError('must be a set') self = cls.from_address(id(s)) self._set = s # keep a reference return self def __iter__(self): for entrynum in xrange(self.mask + 1): entry = self.table[entrynum] yield entry.key class C(object): def __hash__(self): return 37 def __repr__(self): return 'C %d' % id(self) Add 20 of these pathological objects to a set and then remove all but 1 of them. The table now has 19 dummy keys, 1 used slot, and 12 unused slots: >>> c_list = [C() for i in range(20)] >>> s1 = set(c_list) >>> s2 = s1.copy() >>> c_keep = c_list.pop(10) >>> for c in c_list: s1.remove(c) >>> so1 = SetObject.from_set(s1) >>> list(so1) [py_object(''), py_object(''), py_object(''), 0, py_object(''), py_object(''), py_object(''), py_object(''), py_object(''), py_object(''), py_object(''), py_object(''), 0, py_object(''), py_object(''), 0, 0, 0, py_object(''), py_object(''), 0, py_object(C 3072909996), 0, 0, 0, 0, 0, py_object(''), py_object(''), py_object(''), 0, py_object('')] Since all the objects hash the same, the number of dummy keys it will have to skip before finding c_keep will vary depending on the table history. I did a few timeit tests on large pathological sets, and a lookup of the remaining key took anywhere from 4 to 25 times longer when the table was in this state. In contrast, using difference_update() resizes and cleans up the table because it's more than 1/5 dummies (it resizes down to using the 8 element "smalltable" in the set object). >>> s2.difference_update(c_list) >>> so2 = SetObject.from_set(s2) >>> list(so2) [0, 0, 0, 0, 0, py_object(C 3072909996), 0, 0] Inserting keys can also trigger a resize if 3*fill > 2*(mask + 1). In other words, it resizes if the table is more than 2/3 used, where "fill" is active+dummy and mask+1 is the table length. In this case if th numbers 3 and 12 are added to the set, they hash to the unused table slots 3 and 12 (adding more C instances would just refill the dummy slots). This increases "fill" to 22, and triggers a table resize. It's resized to the first power of 2 greater than 4*used == 4*3 = 12. That's 16 in this case. (Pedantically, it's a power of 2 times PySet_MINSIZE, which is 8.) >>> s1.add(3) >>> s1.add(12) >>> list(so1) [0, 0, 0, py_object(3), 0, py_object(C 3072967820), 0, 0, 0, 0, 0, 0, py_object(12), 0, 0, 0] From ramit.prasad at jpmorgan.com Fri Oct 26 19:00:16 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 26 Oct 2012 17:00:16 +0000 Subject: [Tutor] Python and xml *creation* In-Reply-To: <2973435.tcZpjLDrHN@infirit> References: <2973435.tcZpjLDrHN@infirit> Message-ID: <5B80DD153D7D744689F57F4FB69AF47416732277@SCACMX008.exchad.jpmchase.net> Sander Sweers wrote: > Hi List, > > I am looking for resources on creating xml files in python. I have found some > resources but they all are very basic. I find lots of resources online about > parsing and searching in an xml file but not so much about creating them. > > Do any of you know of good online resources? I found an o'reilly book [1] do > you know if it is any good? > > Many thank in advance, > Sander > [1] http://shop.oreilly.com/product/9780596001285.do Based on the most recent comment (May 2010) I would probably stick with online tutorials. """Rating 3.0 Headline: XML tools have moved on By: Kimo from:Auckland NZ About Me: Developer, Educator Verified Reviewer Pros Helpful examples Well-written Cons Obsolete Best Uses Intermediate When I first bought this in 2005 it was very helpful, especially the examples using SAX. However Frank Lundh has now written the Element Tree module which is much easier to use to parse and write XML in Python. Bottom Line: No, I would not recommend this to a friend """ Here are some tutorials for Python and XML. Your mileage will vary greatly depending on what you are trying to achieve. In general, I mostly just Google what I need to know as it tends to have more help / be updated more often than books. http://www.blog.pythonlibrary.org/2012/06/06/parsing-xml-with-python-using-lxml-objectify/ http://wiki.python.org/moin/PythonXml http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python-with-elementtree/ I hope that helps you get started. Feel free to come back when you have some code and need further help. Ramit Prasad 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 Fri Oct 26 21:38:34 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 26 Oct 2012 20:38:34 +0100 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF4741672F7F4@SCACMX008.exchad.jpmchase.net> Message-ID: On 26/10/2012 17:58, eryksun wrote: > Sorry, Saad; this is now *way* off topic... > Apart from blatant trolling nothing is off topic on any Python mailing list. That is one of the joys of reading them. Contrast that to the mailing lists associated with the group of languages that are based on the third letter of the English alphabet :) -- Cheers. Mark Lawrence. From alan.gauld at btinternet.com Sat Oct 27 00:13:27 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 26 Oct 2012 23:13:27 +0100 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF4741672F7F4@SCACMX008.exchad.jpmchase.net> Message-ID: On 26/10/12 20:38, Mark Lawrence wrote: > On 26/10/2012 17:58, eryksun wrote: >> Sorry, Saad; this is now *way* off topic... >> > Apart from blatant trolling nothing is off topic on any Python mailing > list. That's simply not true. The mailing lists mostly have clear topic areas. This one is for learning the basic python language and standard library. The fact that we allow threads to go way off-topic is an indication of the tolerance of the Python community, but there is still very definitely a theme and we have called a halt to off-topic posts in the past and doubtless will do so again. But so long as it has at least some link to the theme we generally let 'em through. But a debate on the mating habits of the lesser spotted South American warbler is probably gonna get stopped pretty soon. Unless of course it involves them getting eaten by pythons... :-) -- Alan G list moderator From dwightdhutto at gmail.com Wed Oct 17 23:34:37 2012 From: dwightdhutto at gmail.com (Dwight Hutto) Date: Wed, 17 Oct 2012 17:34:37 -0400 Subject: [Tutor] program for a problem In-Reply-To: References: Message-ID: On Mon, Oct 8, 2012 at 11:55 AM, Tharuni Dheeraj wrote: > please send me the program for the following que: > > Write a program that asks the user for a dollar amount.It then reports the > corresponding number of euros by using the current exchange rate. > -- > Regards, > Tharuni Dheeraj > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > How many euros do I get paid for this? import urllib usd_dollar_amount = raw_input(" Enter USD dollar amount: $") exchange_rate = urllib.URLopener().open("http://path_to_url_where_data_is_exracted_from.html") for line in exchange_rate: if line == line_where_usd_eur_conversion_is_listed: if eur < usd: amount_to_account_for = (1 - eur_current_value) * usd_dollar_amount amount_of_conversion = usd_dollar_amount - amount_to_account_for print amount_of_conversion -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com From dwightdhutto at gmail.com Thu Oct 18 01:18:31 2012 From: dwightdhutto at gmail.com (Dwight Hutto) Date: Wed, 17 Oct 2012 19:18:31 -0400 Subject: [Tutor] managing memory large dictionaries in python In-Reply-To: References: <507E99A6.5010606@pearwood.info> Message-ID: Now this one removes, the usage of different dicts, removes the whitespace, and uses just a whole dict, then becomes 7.6kb That's a big difference to someone parsing files, and utilizing memory storage areas. -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com -------------- next part -------------- {0:0,1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,10:10,11:11,12:12,13:13,14:14,15:15,16:16,17:17,18:18,19:19,20:20,21:21,22:22,23:23,24:24,25:25,26:26,27:27,28:28,29:29,30:30,31:31,32:32,33:33,34:34,35:35,36:36,37:37,38:38,39:39,40:40,41:41,42:42,43:43,44:44,45:45,46:46,47:47,48:48,49:49,50:50,51:51,52:52,53:53,54:54,55:55,56:56,57:57,58:58,59:59,60:60,61:61,62:62,63:63,64:64,65:65,66:66,67:67,68:68,69:69,70:70,71:71,72:72,73:73,74:74,75:75,76:76,77:77,78:78,79:79,80:80,81:81,82:82,83:83,84:84,85:85,86:86,87:87,88:88,89:89,90:90,91:91,92:92,93:93,94:94,95:95,96:96,97:97,98:98,99:99,100:100,101:101,102:102,103:103,104:104,105:105,106:106,107:107,108:108,109:109,110:110,111:111,112:112,113:113,114:114,115:115,116:116,117:117,118:118,119:119,120:120,121:121,122:122,123:123,124:124,125:125,126:126,127:127,128:128,129:129,130:130,131:131,132:132,133:133,134:134,135:135,136:136,137:137,138:138,139:139,140:140,141:141,142:142,143:143,144:144,145:145,146:146,147:147,148:148,149:149,150:150,151:151,152:152,153:153,154:154,155:155,156:156,157:157,158:158,159:159,160:160,161:161,162:162,163:163,164:164,165:165,166:166,167:167,168:168,169:169,170:170,171:171,172:172,173:173,174:174,175:175,176:176,177:177,178:178,179:179,180:180,181:181,182:182,183:183,184:184,185:185,186:186,187:187,188:188,189:189,190:190,191:191,192:192,193:193,194:194,195:195,196:196,197:197,198:198,199:199,200:200,201:201,202:202,203:203,204:204,205:205,206:206,207:207,208:208,209:209,210:210,211:211,212:212,213:213,214:214,215:215,216:216,217:217,218:218,219:219,220:220,221:221,222:222,223:223,224:224,225:225,226:226,227:227,228:228,229:229,230:230,231:231,232:232,233:233,234:234,235:235,236:236,237:237,238:238,239:239,240:240,241:241,242:242,243:243,244:244,245:245,246:246,247:247,248:248,249:249,250:250,251:251,252:252,253:253,254:254,255:255,256:256,257:257,258:258,259:259,260:260,261:261,262:262,263:263,264:264,265:265,266:266,267:267,268:268,269:269,270:270,271:271,272:272,273:273,274:274,275:275,276:276,277:277,278:278,279:279,280:280,281:281,282:282,283:283,284:284,285:285,286:286,287:287,288:288,289:289,290:290,291:291,292:292,293:293,294:294,295:295,296:296,297:297,298:298,299:299,300:300,301:301,302:302,303:303,304:304,305:305,306:306,307:307,308:308,309:309,310:310,311:311,312:312,313:313,314:314,315:315,316:316,317:317,318:318,319:319,320:320,321:321,322:322,323:323,324:324,325:325,326:326,327:327,328:328,329:329,330:330,331:331,332:332,333:333,334:334,335:335,336:336,337:337,338:338,339:339,340:340,341:341,342:342,343:343,344:344,345:345,346:346,347:347,348:348,349:349,350:350,351:351,352:352,353:353,354:354,355:355,356:356,357:357,358:358,359:359,360:360,361:361,362:362,363:363,364:364,365:365,366:366,367:367,368:368,369:369,370:370,371:371,372:372,373:373,374:374,375:375,376:376,377:377,378:378,379:379,380:380,381:381,382:382,383:383,384:384,385:385,386:386,387:387,388:388,389:389,390:390,391:391,392:392,393:393,394:394,395:395,396:396,397:397,398:398,399:399,400:400,401:401,402:402,403:403,404:404,405:405,406:406,407:407,408:408,409:409,410:410,411:411,412:412,413:413,414:414,415:415,416:416,417:417,418:418,419:419,420:420,421:421,422:422,423:423,424:424,425:425,426:426,427:427,428:428,429:429,430:430,431:431,432:432,433:433,434:434,435:435,436:436,437:437,438:438,439:439,440:440,441:441,442:442,443:443,444:444,445:445,446:446,447:447,448:448,449:449,450:450,451:451,452:452,453:453,454:454,455:455,456:456,457:457,458:458,459:459,460:460,461:461,462:462,463:463,464:464,465:465,466:466,467:467,468:468,469:469,470:470,471:471,472:472,473:473,474:474,475:475,476:476,477:477,478:478,479:479,480:480,481:481,482:482,483:483,484:484,485:485,486:486,487:487,488:488,489:489,490:490,491:491,492:492,493:493,494:494,495:495,496:496,497:497,498:498,499:499,500:500,501:501,502:502,503:503,504:504,505:505,506:506,507:507,508:508,509:509,510:510,511:511,512:512,513:513,514:514,515:515,516:516,517:517,518:518,519:519,520:520,521:521,522:522,523:523,524:524,525:525,526:526,527:527,528:528,529:529,530:530,531:531,532:532,533:533,534:534,535:535,536:536,537:537,538:538,539:539,540:540,541:541,542:542,543:543,544:544,545:545,546:546,547:547,548:548,549:549,550:550,551:551,552:552,553:553,554:554,555:555,556:556,557:557,558:558,559:559,560:560,561:561,562:562,563:563,564:564,565:565,566:566,567:567,568:568,569:569,570:570,571:571,572:572,573:573,574:574,575:575,576:576,577:577,578:578,579:579,580:580,581:581,582:582,583:583,584:584,585:585,586:586,587:587,588:588,589:589,590:590,591:591,592:592,593:593,594:594,595:595,596:596,597:597,598:598,599:599,600:600,601:601,602:602,603:603,604:604,605:605,606:606,607:607,608:608,609:609,610:610,611:611,612:612,613:613,614:614,615:615,616:616,617:617,618:618,619:619,620:620,621:621,622:622,623:623,624:624,625:625,626:626,627:627,628:628,629:629,630:630,631:631,632:632,633:633,634:634,635:635,636:636,637:637,638:638,639:639,640:640,641:641,642:642,643:643,644:644,645:645,646:646,647:647,648:648,649:649,650:650,651:651,652:652,653:653,654:654,655:655,656:656,657:657,658:658,659:659,660:660,661:661,662:662,663:663,664:664,665:665,666:666,667:667,668:668,669:669,670:670,671:671,672:672,673:673,674:674,675:675,676:676,677:677,678:678,679:679,680:680,681:681,682:682,683:683,684:684,685:685,686:686,687:687,688:688,689:689,690:690,691:691,692:692,693:693,694:694,695:695,696:696,697:697,698:698,699:699,700:700,701:701,702:702,703:703,704:704,705:705,706:706,707:707,708:708,709:709,710:710,711:711,712:712,713:713,714:714,715:715,716:716,717:717,718:718,719:719,720:720,721:721,722:722,723:723,724:724,725:725,726:726,727:727,728:728,729:729,730:730,731:731,732:732,733:733,734:734,735:735,736:736,737:737,738:738,739:739,740:740,741:741,742:742,743:743,744:744,745:745,746:746,747:747,748:748,749:749,750:750,751:751,752:752,753:753,754:754,755:755,756:756,757:757,758:758,759:759,760:760,761:761,762:762,763:763,764:764,765:765,766:766,767:767,768:768,769:769,770:770,771:771,772:772,773:773,774:774,775:775,776:776,777:777,778:778,779:779,780:780,781:781,782:782,783:783,784:784,785:785,786:786,787:787,788:788,789:789,790:790,791:791,792:792,793:793,794:794,795:795,796:796,797:797,798:798,799:799,800:800,801:801,802:802,803:803,804:804,805:805,806:806,807:807,808:808,809:809,810:810,811:811,812:812,813:813,814:814,815:815,816:816,817:817,818:818,819:819,820:820,821:821,822:822,823:823,824:824,825:825,826:826,827:827,828:828,829:829,830:830,831:831,832:832,833:833,834:834,835:835,836:836,837:837,838:838,839:839,840:840,841:841,842:842,843:843,844:844,845:845,846:846,847:847,848:848,849:849,850:850,851:851,852:852,853:853,854:854,855:855,856:856,857:857,858:858,859:859,860:860,861:861,862:862,863:863,864:864,865:865,866:866,867:867,868:868,869:869,870:870,871:871,872:872,873:873,874:874,875:875,876:876,877:877,878:878,879:879,880:880,881:881,882:882,883:883,884:884,885:885,886:886,887:887,888:888,889:889,890:890,891:891,892:892,893:893,894:894,895:895,896:896,897:897,898:898,899:899,900:900,901:901,902:902,903:903,904:904,905:905,906:906,907:907,908:908,909:909,910:910,911:911,912:912,913:913,914:914,915:915,916:916,917:917,918:918,919:919,920:920,921:921,922:922,923:923,924:924,925:925,926:926,927:927,928:928,929:929,930:930,931:931,932:932,933:933,934:934,935:935,936:936,937:937,938:938,939:939,940:940,941:941,942:942,943:943,944:944,945:945,946:946,947:947,948:948,949:949,950:950,951:951,952:952,953:953,954:954,955:955,956:956,957:957,958:958,959:959,960:960,961:961,962:962,963:963,964:964,965:965,966:966,967:967,968:968,969:969,970:970,971:971,972:972,973:973,974:974,975:975,976:976,977:977,978:978,979:979,980:980,981:981,982:982,983:983,984:984,985:985,986:986,987:987,988:988,989:989,990:990,991:991,992:992,993:993,994:994,995:995,996:996,997:997,998:998,999:999} From dwightdhutto at gmail.com Thu Oct 18 10:09:06 2012 From: dwightdhutto at gmail.com (David Hutto) Date: Thu, 18 Oct 2012 04:09:06 -0400 Subject: [Tutor] Help Passing Variables In-Reply-To: References: Message-ID: Algorithm it, and look at the instance below the function first where variables are drawn in as raw input, and comments with # are just comments, not part of the code: def SwapCaseAndCenter(a_string, upper_or_lower = None): #find if it's upper, and print if upper_or_lower == "upper": print a_string.center(center_num).upper() #find if it's upper, and print if upper_or_lower == "lower": print a_string.center(center_num).lower() #Instance of function SwapCaseAndCenter vars input #define the string to be upper/lower cased and centered at a particular point a_string = raw_input("Give me a word, or letter: ") #define if it's upper or lower upper_or_lower = raw_input("upper, or lower character(s): ") #define where it should be centered with an int cast center_num = int(raw_input("Where should number be centered?: ")) #call the function with the params, and look above to the function SwapCaseAndCenter(a_string, upper_or_lower) -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com From dwightdhutto at gmail.com Thu Oct 18 10:12:54 2012 From: dwightdhutto at gmail.com (David Hutto) Date: Thu, 18 Oct 2012 04:12:54 -0400 Subject: [Tutor] Help Passing Variables In-Reply-To: References: Message-ID: #This is the actual code: def SwapCaseAndCenter(a_string, upper_or_lower = None): if upper_or_lower == "upper": print a_string.center(center_num).upper() if upper_or_lower == "lower": print a_string.center(center_num).lower() a_string = raw_input("Give me a word, or letter: ") upper_or_lower = raw_input("upper, or lower character(s): ") center_num = int(raw_input("Where should number be centered?: ")) SwapCaseAndCenter(a_string, upper_or_lower) -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com From dwightdhutto at gmail.com Thu Oct 18 10:26:28 2012 From: dwightdhutto at gmail.com (David Hutto) Date: Thu, 18 Oct 2012 04:26:28 -0400 Subject: [Tutor] Help Passing Variables In-Reply-To: References: Message-ID: #Apologies, this is the actual code: def SwapCaseAndCenter(a_string, upper_or_lower = None, center_num = None): if upper_or_lower == "upper": print a_string.center(center_num).upper() if upper_or_lower == "lower": print a_string.center(center_num).lower() a_string = raw_input("Give me a word, or letter: ") upper_or_lower = raw_input("upper, or lower character(s): ") center_num = int(raw_input("Where should number be centered?: ")) SwapCaseAndCenter(a_string, upper_or_lower, center_num) -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com From dwightdhutto at gmail.com Thu Oct 18 10:44:55 2012 From: dwightdhutto at gmail.com (David Hutto) Date: Thu, 18 Oct 2012 04:44:55 -0400 Subject: [Tutor] Help Passing Variables In-Reply-To: References: Message-ID: #A little more complex in terms of params: def SwapCaseAndCenter(*kwargs): if upper_or_lower == "upper": print a_string.center(center_num).upper() if upper_or_lower == "lower": print a_string.center(center_num).lower() a_string = raw_input("Give me a word, or letter: ") upper_or_lower = raw_input("upper, or lower character(s): ") center_num = int(raw_input("Where should number be centered?: ")) SwapCaseAndCenter(a_string, upper_or_lower, center_num) -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com From dwightdhutto at gmail.com Wed Oct 24 02:08:50 2012 From: dwightdhutto at gmail.com (David Hutto) Date: Tue, 23 Oct 2012 20:08:50 -0400 Subject: [Tutor] Strings. In-Reply-To: References: Message-ID: On Tue, Oct 23, 2012 at 7:48 PM, Alexander wrote: > On Tue, Oct 23, 2012 at 7:24 PM, Nitin Ainani wrote: >> Dear Sir/Madam, >> >> I am new to python I have a question. It is as follows: >> >> Suppose s is a variable and s stores empty string >> >> emptyString = "" > #consider the string to be non-empty, that is, let's say 13 characters > long (the number you chose below) > string13 = "1234567890ABC" > print ( string13 [ 0 ] ) > >>Now if we write following statement >> >> print ( s [ 0 ] ) # it gives error It should say print (string13 [ 0 ]) in python 3 > ***what error? *** > The error and your example can be used to understand indexes. > s [ 0 ] is asking for the value of a specific piece of s. > s[0] is asking for the value at index Zero of string s. > S has nothing inside of it. > >> >> print(s[0:]) # it does not give error > #what does it give? >> print (s[13:13]) # this too does not give error > #What is the output >> why? > Did you at any point use s = {}? -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com From mhw at doctors.org.uk Mon Oct 22 21:27:45 2012 From: mhw at doctors.org.uk (Matt Williams) Date: Mon, 22 Oct 2012 20:27:45 +0100 Subject: [Tutor] newb help reading lines from csv In-Reply-To: References: Message-ID: <50859E31.8040207@doctors.org.uk> Dear Rob, This caught me out as well for a long time. As I understand it, csv.reader is a file-reader, which iterates ONCE over the file. There may be more elegant solutions, but I do: import csv ifile = open('test.csv', "r") reader = csv.reader(ifile) inData = [] for row in reader: inData.append[row] ifile.close() you can now loop through inData to your heart's desire. HTH, Matt From salexandre.bm at gmail.com Wed Oct 24 18:51:41 2012 From: salexandre.bm at gmail.com (Alexandre BM) Date: Wed, 24 Oct 2012 17:51:41 +0100 Subject: [Tutor] For - if - else loop; print selective output In-Reply-To: References: Message-ID: <50881C9D.8070002@gmail.com> You can break the loop when you get a positive matches, and add a variable where you can count the number of positive matches, if it's equal to zero print 'not found' On 24/10/2012 17:43, Saad Javed wrote: > Let me modify this example: > > a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', > '21', 'cookies']] > for i in a: > *b = i[0]* > if (i[1] == '25' or i[1] == '26'): > print *b* > else: > print 'Not found' > > This will output: > *jimmy* > *Not found* > *Not found* > * > * > How do I print positive matches (*jimmy*) only and not (*Not found*). > I also want *Not found *to print only /once /if there are no positive > matches. > > On Wednesday, October 24, 2012, Saad Javed wrote: > > Hi, > > a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], > ['harry', '21', 'cookies']] > for i in a: > if (i[1] == '25' or i[1] == '26'): > print 'yes' > else: > print 'Not found' > > This prints: > yes > not found > > I want it to print "yes" for each positive match but nothing for a > negative match. However if all matches are negative, I want it to > print "Not found" once (That bit the code already does). I do I > get it to print "yes" only in a mix result situation? > > Saad > > > > _______________________________________________ > 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 yahufpontius at yahoo.com Fri Oct 19 01:59:05 2012 From: yahufpontius at yahoo.com (Frank Pontius) Date: Thu, 18 Oct 2012 16:59:05 -0700 Subject: [Tutor] Help with class example Message-ID: <001d01cdad8c$8f83d220$ae8b7660$@com> Hello, I'm taking a beginners course on Python. Have class example which is to be used in H/W, which I can't get to work. Thus I figure the example is wrong. But I need this to get H/W done. Using 'import random' and random.randrange in function, returning #. import random def RandomNumberGen (): if random.randrange(0, 2) == 0: X return "tails" return "heads" Simple enough, but running in my Python 2.7.3 IDLE - it will not even run, and give syntax (red char box) error at the X above. Frank -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Sat Oct 27 00:49:38 2012 From: sander.sweers at gmail.com (Sander Sweers) Date: Sat, 27 Oct 2012 00:49:38 +0200 Subject: [Tutor] Python and xml *creation* In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47416732277@SCACMX008.exchad.jpmchase.net> References: <2973435.tcZpjLDrHN@infirit> <5B80DD153D7D744689F57F4FB69AF47416732277@SCACMX008.exchad.jpmchase.net> Message-ID: <7191338.QukqdXNcxZ@infirit> Op vrijdag 26 oktober 2012 17:00:16 schreef Prasad, Ramit: > Based on the most recent comment (May 2010) I would probably > stick with online tutorials. I generally do not trust "reviews" on the publisher's website as they have been proved to be constructed by them. > Here are some tutorials for Python and XML. Your mileage > will vary greatly depending on what you are trying to achieve. > In general, I mostly just Google what I need to know as it tends > to have more help / be updated more often than books. I did use duckduckgo and most of the online resources I found are about parsing and modifying, not creation unfortunately. > http://www.blog.pythonlibrary.org/2012/06/06/parsing-xml-with-python-using-l > xml-objectify/ http://wiki.python.org/moin/PythonXml > http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python-with-elemen > ttree/ The first and the last are about parsing/processing existying xml. I did find the page on the python wiki but untill now I missed the effbot page on elementtree (it is "hidden" almost at the bottom of the page together with the a list of books o_O). This http://effbot.org/zone/element.htm seems to be the best one I have seen so far. Thanks for the reply, Greets Sander From jeanpierreda at gmail.com Sat Oct 27 01:01:47 2012 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Fri, 26 Oct 2012 19:01:47 -0400 Subject: [Tutor] Python and xml *creation* In-Reply-To: <7191338.QukqdXNcxZ@infirit> References: <2973435.tcZpjLDrHN@infirit> <5B80DD153D7D744689F57F4FB69AF47416732277@SCACMX008.exchad.jpmchase.net> <7191338.QukqdXNcxZ@infirit> Message-ID: On Fri, Oct 26, 2012 at 6:49 PM, Sander Sweers wrote: > Op vrijdag 26 oktober 2012 17:00:16 schreef Prasad, Ramit: >> Based on the most recent comment (May 2010) I would probably >> stick with online tutorials. > > I generally do not trust "reviews" on the publisher's website as they have > been proved to be constructed by them. Why would the publisher construct a negative review? And if it did, would you distrust it and decide that the book was actually good? >> http://www.blog.pythonlibrary.org/2012/06/06/parsing-xml-with-python-using-l >> xml-objectify/ http://wiki.python.org/moin/PythonXml >> http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python-with-elemen >> ttree/ > > The first and the last are about parsing/processing existying xml. I did find > the page on the python wiki but untill now I missed the effbot page on > elementtree (it is "hidden" almost at the bottom of the page together with the > a list of books o_O). As far as lxml goes, there is also the E factory: - http://lxml.de/tutorial.html#the-e-factory - http://lxml.de/objectify.html#tree-generation-with-the-e-factory And you can create valid XML via a template via Genshi: - http://genshi.edgewall.org/wiki/GenshiTutorial -- Devin From bgailer at gmail.com Sat Oct 27 01:28:26 2012 From: bgailer at gmail.com (bob gailer) Date: Fri, 26 Oct 2012 19:28:26 -0400 Subject: [Tutor] Help with class example In-Reply-To: <001d01cdad8c$8f83d220$ae8b7660$@com> References: <001d01cdad8c$8f83d220$ae8b7660$@com> Message-ID: <508B1C9A.1020306@gmail.com> On 10/18/2012 7:59 PM, Frank Pontius wrote: > Help with class example > > Hello, > Hi - this just showed up in my mailbox. Perhaps there was a delay getting it posted. The "" around tails and heads are not ascii quote characters. They are what some word processors change ascii quotes into to meet typesetting requirements. Use a text editor, or type the code directly into IDLE. > > I'm taking a beginners course on Python. > > Have class example which is to be used in H/W, which I can't get to > work. Thus I figure the example is wrong. But I need this to get H/W > done. > > Using'import random'and random.randrange infunction,returning #. > > import random > > def RandomNumberGen (): > > if random.randrange(0, 2) == 0: > > Xreturn"tails" > > return"heads" > > Simple enough, but running in my Python 2.7.3IDLE-it will not even > run,and give syntax(red char box)error at the X above. > > Frank > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer 919-636-4239 Chapel Hill NC -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Sat Oct 27 04:54:38 2012 From: d at davea.name (Dave Angel) Date: Fri, 26 Oct 2012 22:54:38 -0400 Subject: [Tutor] Help with class example In-Reply-To: <001d01cdad8c$8f83d220$ae8b7660$@com> References: <001d01cdad8c$8f83d220$ae8b7660$@com> Message-ID: <508B4CEE.3000609@davea.name> On 10/18/2012 07:59 PM, Frank Pontius wrote: > Hello, > I'm taking a beginners course on Python. > > Have class example which is to be used in H/W, which I can't get to work. > Thus I figure the example is wrong. But I need this to get H/W done. > > Using 'import random' and random.randrange in function, returning #. > > import random > > def RandomNumberGen (): > if random.randrange(0, 2) == 0: > X return "tails" > return "heads" > > > Simple enough, but running in my Python 2.7.3 IDLE - it will not even run, > and give syntax (red char box) error at the X above. > > Frank > > So why is there an X there? As the compiler tells you, that's a syntax error. The return statement starts with an 'r' Please use only text editors to edit code, and use copy/paste to copy the actual source to your message, while composing the message in text mode. We cannot tell who has mangled the code, but only how it happens to look after the mangling is finished. If it ever goes through a word processor, or through an html message (which your is), then all bets are off. Another thing. Copy/paste the actual error message, including the traceback. Don't summarize it, or paraphrase it. -- DaveA From amin90memar at yahoo.com Sat Oct 27 21:50:01 2012 From: amin90memar at yahoo.com (Amin Memar) Date: Sat, 27 Oct 2012 12:50:01 -0700 (PDT) Subject: [Tutor] Question Message-ID: <1351367401.24299.YahooMailNeo@web160406.mail.bf1.yahoo.com> Hi there! How can I use wxpython on python 3.3.0? thanks ? Best Regards. Amin -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Oct 27 23:19:45 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 27 Oct 2012 22:19:45 +0100 Subject: [Tutor] Question In-Reply-To: <1351367401.24299.YahooMailNeo@web160406.mail.bf1.yahoo.com> References: <1351367401.24299.YahooMailNeo@web160406.mail.bf1.yahoo.com> Message-ID: On 27/10/12 20:50, Amin Memar wrote: > Hi there! > How can I use wxpython on python 3.3.0? You can't its not supported yet. So you will need to either port the code yourself or use Python 2.7. This is not uncommon with third party modules a lot of them are still on v2. It is a lot of work to convert a large code base from Python 2 to 3 and then test it thoroughly and the people doing the work are nearly all volunteers. So either find another GUI framework (like Tkinter) ) that works with Python 3 or downshift to v2.7. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From bgailer at gmail.com Sun Oct 28 01:29:36 2012 From: bgailer at gmail.com (bob gailer) Date: Sat, 27 Oct 2012 19:29:36 -0400 Subject: [Tutor] Help with class example In-Reply-To: References: Message-ID: <508C6E60.5060305@gmail.com> On 10/27/2012 4:51 PM, Frank Pontius wrote: > But, this is an if. When replying please put your response after the text it applies to, and delete all irrelevant text. In other words avoid "top-posting". Sorry I really messed up my reply! The offending line is: coin = random.randragge(0,2): > get rid of the colon. That is used only at the start a compound > statement e;g; if, else, for, while, def class, ... -- Bob Gailer 919-636-4239 Chapel Hill NC From emile at fenx.com Sun Oct 28 18:41:59 2012 From: emile at fenx.com (Emile van Sebille) Date: Sun, 28 Oct 2012 10:41:59 -0700 Subject: [Tutor] Question In-Reply-To: References: <1351367401.24299.YahooMailNeo@web160406.mail.bf1.yahoo.com> Message-ID: On 10/27/2012 2:19 PM, Alan Gauld wrote: > On 27/10/12 20:50, Amin Memar wrote: >> Hi there! >> How can I use wxpython on python 3.3.0? > > You can't its not supported yet. > So you will need to either port the code yourself or use Python 2.7. > > This is not uncommon with third party modules a lot of them are still on > v2. It is a lot of work to convert a large code base from Python 2 to 3 > and then test it thoroughly and the people doing the work are nearly all > volunteers. > > So either find another GUI framework (like Tkinter) ) that works with > Python 3 or downshift to v2.7. > Actually, it looks to me that the v3.x compatible wxpython is being rebranded as Project Phoenix -- see http://wiki.wxpython.org/ProjectPhoenix Emile From breamoreboy at yahoo.co.uk Sun Oct 28 19:40:28 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 28 Oct 2012 18:40:28 +0000 Subject: [Tutor] Question In-Reply-To: References: <1351367401.24299.YahooMailNeo@web160406.mail.bf1.yahoo.com> Message-ID: On 28/10/2012 17:41, Emile van Sebille wrote: > On 10/27/2012 2:19 PM, Alan Gauld wrote: >> On 27/10/12 20:50, Amin Memar wrote: >>> Hi there! >>> How can I use wxpython on python 3.3.0? >> >> You can't its not supported yet. >> So you will need to either port the code yourself or use Python 2.7. >> >> This is not uncommon with third party modules a lot of them are still on >> v2. It is a lot of work to convert a large code base from Python 2 to 3 >> and then test it thoroughly and the people doing the work are nearly all >> volunteers. >> >> So either find another GUI framework (like Tkinter) ) that works with >> Python 3 or downshift to v2.7. >> > > Actually, it looks to me that the v3.x compatible wxpython is being > rebranded as Project Phoenix > > -- see http://wiki.wxpython.org/ProjectPhoenix > > Emile > Thanks for the link that's extremely useful. For the benefit of all here's the link to the wxPython mailing lists, there are many references to Phoenix on the development list http://wxpython.org/maillist.php -- Cheers. Mark Lawrence. From sbeleza at gmail.com Sun Oct 28 22:37:45 2012 From: sbeleza at gmail.com (Sandra Beleza) Date: Sun, 28 Oct 2012 14:37:45 -0700 Subject: [Tutor] help with homework Message-ID: Hi, I have to write a script that asks the user for names one at a time, and accept the name only if the user did not gave it before. The script has to do this until it gets 3 unique names. So far I have this: def GetNames(): names=[] while len(names)<3: name=raw_input("Name: ") if name in names: print name, "is already in the data. Try again." if name not in names: names.append(name) names.sort() for each in names: print "Hurray for", each +"!" print However I cannot use the len() built in function. The Teacher is asking for another solution that does not use the len() t I know I can do it using the command: def Ask def GetNames(how_many): names=[] for el in range(how_many): name=raw_input("Name: ") if name in names: print name, "is already in the data. Try again." if name not in names: names.append(name) names.sort() for each in names: print "Hurray for", each +"!", print I cannot get 3 names, and it is easy to understand why (because the loop only iterates 3 times). But I don't know how to ask the user for names one at a time and to obtain 3 names and an output that looks like this: Name #1: Lewis Name #2: John Name #3: John John is already in the data. Try again. Name #3: Name #3: Chris Hurray for Chris! Hurray for John! Hurray for Lewis! Many Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Sun Oct 28 22:54:54 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 28 Oct 2012 21:54:54 +0000 Subject: [Tutor] help with homework In-Reply-To: References: Message-ID: On 28/10/2012 21:37, Sandra Beleza wrote: > Hi, > I have to write a script that asks the user for names one at a time, and > accept the name only if the user did not gave it before. The script has to > do this until it gets 3 unique names. > > So far I have this: > > def GetNames(): > names=[] > while len(names)<3: > name=raw_input("Name: ") > if name in names: > print name, "is already in the data. Try again." > if name not in names: > names.append(name) > names.sort() > > for each in names: > print "Hurray for", each +"!" > print > > However I cannot use the len() built in function. The Teacher is asking for > another solution that does not use the len() t > I know I can do it using the command: > > def Ask > > def GetNames(how_many): > names=[] > for el in range(how_many): > name=raw_input("Name: ") > if name in names: > print name, "is already in the data. Try again." > if name not in names: > names.append(name) > names.sort() > for each in names: > print "Hurray for", each +"!", > print > > > I cannot get 3 names, and it is easy to understand why (because the loop > only iterates 3 times). But I don't know how to ask the user for names one > at a time and to obtain 3 names and an output that looks like this: > Name #1: Lewis > Name #2: John > Name #3: John > John is already in the data. Try again. > Name #3: > Name #3: Chris > Hurray for Chris! Hurray for John! Hurray for Lewis! > > > Many Thanks! > In your original getNames do something like this. Initialise a counter to zero. Every time you get a valid name increment the count. If the count is three you're finished. -- Cheers. Mark Lawrence. From alan.gauld at btinternet.com Mon Oct 29 00:42:29 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 28 Oct 2012 23:42:29 +0000 Subject: [Tutor] Question In-Reply-To: References: <1351367401.24299.YahooMailNeo@web160406.mail.bf1.yahoo.com> Message-ID: On 28/10/12 17:41, Emile van Sebille wrote: >>> How can I use wxpython on python 3.3.0? > > Actually, it looks to me that the v3.x compatible wxpython is being > rebranded as Project Phoenix > > -- see http://wiki.wxpython.org/ProjectPhoenix That's good news although two names for the same toolkit will confuse things! But good to dsee progress. And a pity the wxPython home page makes no mention of Phoenix. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Mon Oct 29 00:48:42 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 28 Oct 2012 23:48:42 +0000 Subject: [Tutor] help with homework In-Reply-To: References: Message-ID: On 28/10/12 21:37, Sandra Beleza wrote: > def GetNames(): > names=[] > while len(names)<3: > name=raw_input("Name: ") > if name in names: > print name, "is already in the data. Try again." > if name not in names: > names.append(name) > names.sort() You should probably stop your function here by returning names. > > for each in names: > print "Hurray for", each +"!" > print It's good practice to keep the presentation logic outside the function. Your loop would then change to for each in getNames(): ... > However I cannot use the len() built in function. The Teacher is asking > for another solution that does not use the len() t That's bizarre. It forces you to write a bad solution. However, Mark has already given you the necessary hint. But using len() is absolutely the sane way to do this (unless you are paranoid about micro performance). -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From chigga101 at gmail.com Mon Oct 29 02:40:09 2012 From: chigga101 at gmail.com (Matthew Ngaha) Date: Mon, 29 Oct 2012 01:40:09 +0000 Subject: [Tutor] help with homework In-Reply-To: References: Message-ID: > > In your original getNames do something like this. >> Initialise a counter to zero. >> Every time you get a valid name increment the count. >> If the count is three you're finished. >> > hey i was looking at the question as im learning also. With the counter, would you use a while loop instead of a for loop, with the condition being something like, while count < 3: ? without len() the for loop option seems difficult -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Mon Oct 29 03:13:27 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 29 Oct 2012 02:13:27 +0000 Subject: [Tutor] help with homework In-Reply-To: References: Message-ID: On 29/10/2012 01:40, Matthew Ngaha wrote: >> >> In your original getNames do something like this. >>> Initialise a counter to zero. >>> Every time you get a valid name increment the count. >>> If the count is three you're finished. >>> >> hey i was looking at the question as im learning also. With the counter, > would you use a while loop instead of a for loop, with the condition being > something like, while count < 3: ? without len() the for loop option seems > difficult > Always use a while loop in this situation, regardless of whether or not teachers put stupid artificial constraints on your code, such as banning the use of len(). -- Cheers. Mark Lawrence. From sbjaved at gmail.com Mon Oct 29 07:06:14 2012 From: sbjaved at gmail.com (Saad Javed) Date: Mon, 29 Oct 2012 11:06:14 +0500 Subject: [Tutor] run with default value if input not given Message-ID: Hi, #!/usr/bin/env python import sys x = 'Saad is a boy' def main(x): a = [] b = x.split(' ') for item in b: a.append(item) print a if __name__ == '__main__': x = sys.argv[1] main(x) How can I make this program run with the default value of x if I don't specify an argument at the command line? It should do this: saad at saad:~$ python test.py "Mariam is a girl" ['Mariam', 'is', 'a', 'girl'] saad at saad:~$ python test.py ['Saad', 'is', 'a', 'boy'] But the simply running "test.py" gives: Traceback (most recent call last): File "input_test.py", line 13, in x = sys.argv[1] IndexError: list index out of range Saad -------------- next part -------------- An HTML attachment was scrubbed... URL: From sbjaved at gmail.com Mon Oct 29 07:28:05 2012 From: sbjaved at gmail.com (Saad Javed) Date: Mon, 29 Oct 2012 11:28:05 +0500 Subject: [Tutor] run with default value if input not given In-Reply-To: References: Message-ID: I've come up with this: try: sys.argv[1] x = sys.argv[1] main(x) except IndexError: main(x) It works but seems hackish. Saad On Monday, October 29, 2012, Saad Javed wrote: > Hi, > > #!/usr/bin/env python > > import sys > > x = 'Saad is a boy' > > def main(x): > a = [] > b = x.split(' ') > for item in b: > a.append(item) > print a > if __name__ == '__main__': > x = sys.argv[1] > main(x) > > > How can I make this program run with the default value of x if I don't > specify an argument at the command line? > It should do this: > > saad at saad:~$ python test.py "Mariam is a girl" > ['Mariam', 'is', 'a', 'girl'] > > saad at saad:~$ python test.py > ['Saad', 'is', 'a', 'boy'] > > But the simply running "test.py" gives: > Traceback (most recent call last): > File "input_test.py", line 13, in > x = sys.argv[1] > IndexError: list index out of range > > > Saad > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Mon Oct 29 08:31:49 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 29 Oct 2012 18:31:49 +1100 Subject: [Tutor] run with default value if input not given In-Reply-To: References: Message-ID: <20121029073149.GA31887@ando> Saad, please don't send HTML emails, as it destroys the essential formatting of your code. On Mon, Oct 29, 2012 at 11:06:14AM +0500, Saad Javed wrote: > How can I make this program run with the default value of x if I don't > specify an argument at the command line? arguments = sys.argv[1:] # get the list of command line arguments if arguments: # There are some. process(arguments) else: # There are no arguments. do_something_else() Or if you prefer, check that len(sys.argv) > 1. -- Steven From steve at pearwood.info Mon Oct 29 08:37:52 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 29 Oct 2012 18:37:52 +1100 Subject: [Tutor] run with default value if input not given In-Reply-To: References: Message-ID: <20121029073752.GB31887@ando> On Mon, Oct 29, 2012 at 11:28:05AM +0500, Saad Javed wrote: > I've come up with this: > > try: > sys.argv[1] > x = sys.argv[1] > main(x) > except IndexError: > main(x) > > It works but seems hackish. There's no need to look up sys.argv[1] twice, nor any need to write main(x) in both blocks. You can do each once only: try: # Always use the minimum code needed inside a try block. x = sys.argv[1] except IndexError: x = "some default value" main(x) Here's yet another way: args = sys.argv + ["some default value"] main(args[1]) -- Steven From __peter__ at web.de Mon Oct 29 09:14:04 2012 From: __peter__ at web.de (Peter Otten) Date: Mon, 29 Oct 2012 09:14:04 +0100 Subject: [Tutor] run with default value if input not given References: Message-ID: Saad Javed wrote: > Hi, > > #!/usr/bin/env python > > import sys > > x = 'Saad is a boy' > > def main(x): > a = [] > b = x.split(' ') > for item in b: > a.append(item) > print a > if __name__ == '__main__': > x = sys.argv[1] > main(x) > > > How can I make this program run with the default value of x if I don't > specify an argument at the command line? > It should do this: > > saad at saad:~$ python test.py "Mariam is a girl" > ['Mariam', 'is', 'a', 'girl'] > > saad at saad:~$ python test.py > ['Saad', 'is', 'a', 'boy'] > > But the simply running "test.py" gives: > Traceback (most recent call last): > File "input_test.py", line 13, in > x = sys.argv[1] > IndexError: list index out of range > > > Saad The argparse module (see ) offers a flexible way to specify command line arguments. Your program would look like this: $ cat optional_arg.py #!/usr/bin/env python import argparse def main(): parser = argparse.ArgumentParser( description="Print a sentence as a list of words") parser.add_argument( "sentence", nargs="?", default="Mary had a little lamb") args = parser.parse_args() words = args.sentence.split() print words if __name__ == "__main__": main() ...and work like this: $ ./optional_arg.py ['Mary', 'had', 'a', 'little', 'lamb'] $ ./optional_arg.py "That's all folks" ["That's", 'all', 'folks'] It would include help... $ ./optional_arg.py -h usage: optional_arg.py [-h] [sentence] Print a sentence as a list of words positional arguments: sentence optional arguments: -h, --help show this help message and exit ...and basic error reporting: $ ./optional_arg.py That\'s all folks usage: optional_arg.py [-h] [sentence] optional_arg.py: error: unrecognized arguments: all folks almost for free. So even if you find argparse too complex right now keep in mind that it exists until you are comfortable enough with Python to start making use of more parts of its standard library. From pasokan at talentsprint.com Mon Oct 29 09:37:59 2012 From: pasokan at talentsprint.com (Asokan Pichai) Date: Mon, 29 Oct 2012 14:07:59 +0530 Subject: [Tutor] help with homework In-Reply-To: References: Message-ID: [SNIPPED] > > Always use a while loop in this situation, This is excellent advice. Use a for loop in two situations: 1. Iterating a fixed(known) number of times 2. Iterating through the contents of any container Use a while loop to iterate as long as a condition applies. > regardless of whether or not > teachers put stupid artificial constraints on your code, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > such as banning the use of len(). There may be legitimate learning outcomes for a teacher to specify such conditions. In this case, learning to use a counter that is incremented under certain conditions. I would hesitate to condemn in such strong terms without knowing more background. Asokan Pichai From alan.gauld at btinternet.com Mon Oct 29 09:58:59 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 29 Oct 2012 08:58:59 +0000 Subject: [Tutor] help with homework In-Reply-To: References: Message-ID: On 29/10/12 08:37, Asokan Pichai wrote: >> teachers put stupid artificial constraints on your code, > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> such as banning the use of len(). > > There may be legitimate learning outcomes for a teacher > to specify such conditions. In that case they should think up a scenario that requires the use of the construct that is most appropriate. Teachers should never encourage bad practice(*) and that's what this example does. It's valid to put constraints such as "do not use any external modules" or to use a specific construct if that's what's being taught. But it's never right to leave the student the freedom to use any solution *except* the one that is most logical and readily available. > In this case, learning to use a counter that is incremented > under certain conditions. But there are many better cases where that solution is needed rather than using len(). This one just sounds like lazy teaching. (*) Actually enforcing bad practice one to demonstrate the problems can be valid provided its followed immediately by the best practice alternative. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From pasokan at talentsprint.com Mon Oct 29 10:15:32 2012 From: pasokan at talentsprint.com (Asokan Pichai) Date: Mon, 29 Oct 2012 14:45:32 +0530 Subject: [Tutor] help with homework In-Reply-To: References: Message-ID: On Mon, Oct 29, 2012 at 2:28 PM, Alan Gauld wrote: > On 29/10/12 08:37, Asokan Pichai wrote: > >>> teachers put stupid artificial constraints on your code, >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >>> >>> such as banning the use of len(). >> >> >> There may be legitimate learning outcomes for a teacher >> to specify such conditions. > > > In that case they should think up a scenario that requires the use of the > construct that is most appropriate. Teachers should never encourage bad > practice(*) and that's what this example does. > > It's valid to put constraints such as "do not use any external > modules" or to use a specific construct if that's what's being taught. > But it's never right to leave the student the freedom to use any solution > *except* the one that is most logical and readily available. > > >> In this case, learning to use a counter that is incremented >> under certain conditions. > > > But there are many better cases where that solution is needed rather than > using len(). This one just sounds like lazy teaching. > > (*) Actually enforcing bad practice one to demonstrate the problems > can be valid provided its followed immediately by the best practice > alternative. As a trainer, I believe using a bad example is WRONG; even to teach how not to write. Better to critique the suggested bad answers and explain why that is bad, rather than enforce a constraint that leads to a bad way and then call it out as bad explain why. That said, it *is* preferable IMO, not use such strong condemnation without knowing full background. Probably by now this is OT, so I should stop now. Asokan Pichai If a language is designed for non-programmers, soon only non-programs get written in it. --- Anonymouse From john at netcore.com.au Mon Oct 29 10:49:50 2012 From: john at netcore.com.au (John Collins) Date: Mon, 29 Oct 2012 20:49:50 +1100 Subject: [Tutor] greater precision? Message-ID: <508E513E.4000203@netcore.com.au> Hi, OS: XP Py: 2.7.2 I am running some old scripts, not written by me, that produce substantial calculated values data as decimals, 12 significant figures. AFAIK, the keys calls are; from math import pi, asin, atan2, cos, sin, sqrt from math import pi, asin, atan2, cos, sin, sqrt from crosspoint import crosspoint which are from the 1st and 2nd scripts, that are run to generate, then manipulate, the numbers (actually, polygon descriptions!). However, the 2nd script 'calls' a script called crosspoint.py, trimmed of most comments this is; # Python code to find the crossing point of two lines. # This function is optimised for big-integer or FP arithmetic: it # multiplies up to find two big numbers, and then divides them. def crosspoint(xa1,ya1,xa2,ya2,xb1,yb1,xb2,yb2): "Give the intersection point of the (possibly extrapolated) lines\n"\ "segments (xa1,ya1)-(xa2,ya2) and (xb1,yb1)-(xb2,yb2)." dxa = xa2-xa1 dya = ya2-ya1 dxb = xb2-xb1 dyb = yb2-yb1 if dya * dxb == dxa * dyb: return None if dxa == 0: return (xa1, (xa1 - xb1) * dyb / dxb + yb1) if dxb == 0: return (xb1, (xb1 - xa1) * dya / dxa + ya1) if dya == 0: return ((ya1 - yb1) * dxb / dyb + xb1, ya1) if dyb == 0: return ((yb1 - ya1) * dxa / dya + xa1, yb1) det = dya * dxb - dyb * dxa xtop = dxb * dxa * (yb1-ya1) + dya * dxb * xa1 - dyb * dxa * xb1 ytop = dya * dyb * (xa1-xb1) + dxb * dya * yb1 - dxa * dyb * ya1 return (xtop / det, ytop / det) I am not sure what == means, nor if any 'special' maths functions are called from crosspoint.py (to me, it appears not?), so, as I understand it, the from math import line *is* the 'math engine' if you will, and is the source of the 12 sig fig limit, yes? One other odd thing that occurs, is when the scripts are run, a weird, small, non ASCII file called crosspoint.pyc is created? Is this the interpreter 'compiling' crosspoint.py ( either before, during, or after?) the 2nd script calls it? Sorry to bother, but if I can squeeze out *at least* 15 sig figs, (30 or more would be better!) I'd be a happy camper! XNumbers addon for Excel allows over 200 sig figs by switching to base 256 IIRC. It is this with which I'd like to examine the output of these pyto scripts at finer resolution, if that can be done in python??? Best Regards, John Collins. From john at netcore.com.au Mon Oct 29 10:46:04 2012 From: john at netcore.com.au (John Collins) Date: Mon, 29 Oct 2012 20:46:04 +1100 Subject: [Tutor] greater precision? Message-ID: <508E505C.9030209@netcore.com.au> Hi, OS: XP Py: 2.7.2 I am running some old scripts, not written by me, that produce substantial calculated values data as decimals, 12 significant figures. AFAIK, the keys calls are; from math import pi, asin, atan2, cos, sin, sqrt from math import pi, asin, atan2, cos, sin, sqrt from crosspoint import crosspoint which are from the 1st and 2nd scripts, that are run to generate, then manipulate, the numbers (actually, polygon descriptions!). However, the 2nd script 'calls' a script called crosspoint.py, trimmed of most comments this is; # Python code to find the crossing point of two lines. # This function is optimised for big-integer or FP arithmetic: it # multiplies up to find two big numbers, and then divides them. def crosspoint(xa1,ya1,xa2,ya2,xb1,yb1,xb2,yb2): "Give the intersection point of the (possibly extrapolated) lines\n"\ "segments (xa1,ya1)-(xa2,ya2) and (xb1,yb1)-(xb2,yb2)." dxa = xa2-xa1 dya = ya2-ya1 dxb = xb2-xb1 dyb = yb2-yb1 if dya * dxb == dxa * dyb: return None if dxa == 0: return (xa1, (xa1 - xb1) * dyb / dxb + yb1) if dxb == 0: return (xb1, (xb1 - xa1) * dya / dxa + ya1) if dya == 0: return ((ya1 - yb1) * dxb / dyb + xb1, ya1) if dyb == 0: return ((yb1 - ya1) * dxa / dya + xa1, yb1) det = dya * dxb - dyb * dxa xtop = dxb * dxa * (yb1-ya1) + dya * dxb * xa1 - dyb * dxa * xb1 ytop = dya * dyb * (xa1-xb1) + dxb * dya * yb1 - dxa * dyb * ya1 return (xtop / det, ytop / det) I am not sure what == means, nor if any 'special' maths functions are called from crosspoint.py (to me, it appears not?), so, as I understand it, the from math import line *is* the 'math engine' if you will, and is the source of the 12 sig fig limit, yes? One other odd thing that occurs, is when the scripts are run, a weird, small, non ASCII file called crosspoint.pyc is created? Is this the interpreter 'compiling' crosspoint.py ( either before, during, or after?) the 2nd script calls it? Sorry to bother, but if I can squeeze out *at least* 15 sig figs, (30 or more would be better!) I'd be a happy camper! XNumbers addon for Excel allows over 200 sig figs by switching to base 256 IIRC. It is this with which I'd like to examine the output of these pyto scripts at finer resolution, if that can be done in python??? Best Regards, John Collins. From d at davea.name Mon Oct 29 11:19:55 2012 From: d at davea.name (Dave Angel) Date: Mon, 29 Oct 2012 06:19:55 -0400 Subject: [Tutor] greater precision? In-Reply-To: <508E513E.4000203@netcore.com.au> References: <508E513E.4000203@netcore.com.au> Message-ID: <508E584B.6040000@davea.name> On 10/29/2012 05:49 AM, John Collins wrote: Did you really leave two very-similar messages 3 minutes apart? Or are you using a broken gateway, like Google-groups, and it hiccoughed? > Hi, > OS: XP > Py: 2.7.2 > > I am running some old scripts, not written by me, that produce > substantial calculated values data as decimals, 12 significant > figures. AFAIK, the keys calls are; > > from math import pi, asin, atan2, cos, sin, sqrt > > from math import pi, asin, atan2, cos, sin, sqrt > from crosspoint import crosspoint > > which are from the 1st and 2nd scripts, that are run to generate, > then manipulate, the numbers (actually, polygon descriptions!). > However, the 2nd script 'calls' a script called crosspoint.py, > trimmed of most comments this is; > > # Python code to find the crossing point of two lines. > # This function is optimised for big-integer or FP arithmetic: it > # multiplies up to find two big numbers, and then divides them. > > def crosspoint(xa1,ya1,xa2,ya2,xb1,yb1,xb2,yb2): > "Give the intersection point of the (possibly extrapolated) lines\n"\ > "segments (xa1,ya1)-(xa2,ya2) and (xb1,yb1)-(xb2,yb2)." > dxa = xa2-xa1 > dya = ya2-ya1 > dxb = xb2-xb1 > dyb = yb2-yb1 > if dya * dxb == dxa * dyb: > return None > if dxa == 0: > return (xa1, (xa1 - xb1) * dyb / dxb + yb1) > if dxb == 0: > return (xb1, (xb1 - xa1) * dya / dxa + ya1) > if dya == 0: > return ((ya1 - yb1) * dxb / dyb + xb1, ya1) > if dyb == 0: > return ((yb1 - ya1) * dxa / dya + xa1, yb1) > > det = dya * dxb - dyb * dxa > xtop = dxb * dxa * (yb1-ya1) + dya * dxb * xa1 - dyb * dxa * xb1 > ytop = dya * dyb * (xa1-xb1) + dxb * dya * yb1 - dxa * dyb * ya1 > > return (xtop / det, ytop / det) > > > I am not sure what == means, nor if any 'special' maths functions are > called from crosspoint.py (to me, it appears not?), so, as I > understand it, the Without knowing the type of the arguments being passed to the crosspoint() function, we cannot say. I can GUESS that they are int, long, or float. But you can find that out by examining the source that's calling it. Judging from the comments, I might guess they're int or long, and if so, only the division being done in the function produces floats. If that's the case, then you're limited to float's precision. if they are some other type (eg. Decimal), then indeed there might be special functions being called. > > from math import > > line *is* the 'math engine' if you will, and is the source of the 12 > sig fig limit, yes? > That import gets you access to the particular symbols it imports. Normal arithmetic on floats is already built in, and doesn't need an import. I'm assuming you're using CPython, and you say it's on XP. So presumably you're running an Intel (or Intel-compatible) processor with binary floating point built-in. That processor is the limit of float values in normal use. It's good to about 18 digits. > One other odd thing that occurs, is when the scripts are run, a weird, > small, non ASCII file called crosspoint.pyc is created? Is this the > interpreter 'compiling' crosspoint.py ( either before, during, or > after?) the 2nd script calls it? > Exactly. It's a side-effect of the first import of the module. On subsequent imports, the pyc file is used, unless the py file has been modified meanwhile. > Sorry to bother, but if I can squeeze out *at least* 15 sig figs, (30 > or more would be better!) I'd be a happy camper! > XNumbers addon for Excel allows over 200 sig figs by switching to base > 256 IIRC. It is this with which I'd like to examine the output of these > pyto scripts at finer resolution, if that can be done in python??? > > Best Regards, > John Collins. > > 18 digits is what you should get if the code is as I describe. But if there are lots of fp operations you're not showing, then an error can gradually get larger. And with any finite precision, you have the risk from things such as subtracting two nearly-equal values, which will reduce the final precision. If you need more than 18, then go to the Decimal module, which lets you set the precision to arbitrary levels. You will see a substantial slowdown, of course, if you set the precision very high. if that becomes a problem, consider CPython 3.3, which has optimized that module. But try not to change too many things at once, as there are lots of changes between 2.7 and 3.3. -- DaveA From steve at pearwood.info Mon Oct 29 11:23:02 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 29 Oct 2012 21:23:02 +1100 Subject: [Tutor] greater precision? In-Reply-To: <508E505C.9030209@netcore.com.au> References: <508E505C.9030209@netcore.com.au> Message-ID: <508E5906.5060101@pearwood.info> On 29/10/12 20:46, John Collins wrote: > Hi, > OS: XP > Py: 2.7.2 > > I am running some old scripts, not written by me, that produce > substantial calculated values data as decimals, 12 significant > figures. AFAIK, the keys calls are; "Decimal" has a specific meaning in Python different to how you appear to be using it. It looks to me like you are working with floats rather than decimals. If the difference means nothing to you, don't worry too much about it at this stage. > from math import pi, asin, atan2, cos, sin, sqrt > from math import pi, asin, atan2, cos, sin, sqrt That line appears duplicated. You can delete one of those lines. > from crosspoint import crosspoint "crosspoint" appears to be a custom Python program that we know nothing about, apart from what you tell us. [...] > I am not sure what == means, In Python, "=" is used for assignment: x = 1 means "let x be a name for the value 1". "==" is used for equality testing: x == 2 returns False, because x does not equal 2; but x == 1 returns True, because x does currently equal 1. > nor if any 'special' maths functions are > called from crosspoint.py (to me, it appears not?), Nor to me. >so, as I understand it, the > > from math import > > line *is* the 'math engine' if you will, and is the source of the 12 > sig fig limit, yes? Almost. The "from math import blah..." line extracts a bunch of maths functions from the math library and makes them available to your program. The number of significant figures is more fundamental that that; your operating system understands about floating point numbers (so-called "C doubles") with 53 *binary* significant figures, or about 17 *decimal* figures. So I'm not sure why you think there are only 12. > One other odd thing that occurs, is when the scripts are run, a weird, > small, non ASCII file called crosspoint.pyc is created? Is this the > interpreter 'compiling' crosspoint.py ( either before, during, or > after?) the 2nd script calls it? Yes. > Sorry to bother, but if I can squeeze out *at least* 15 sig figs, You need to show us how the output is generated in the first place. > (30 or more would be better!) Not a chance without some major changes to your program. I can't say how major without seeing more of your program. > I'd be a happy camper! > XNumbers addon for Excel allows over 200 sig figs by switching to base > 256 IIRC. It is this with which I'd like to examine the output of these > pyto scripts at finer resolution, if that can be done in python??? Well, yes, but only with some significant changes -- Steven From john at netcore.com.au Mon Oct 29 11:32:36 2012 From: john at netcore.com.au (John Collins) Date: Mon, 29 Oct 2012 21:32:36 +1100 Subject: [Tutor] greater precision? In-Reply-To: <508E584B.6040000@davea.name> References: <508E513E.4000203@netcore.com.au> <508E584B.6040000@davea.name> Message-ID: <508E5B44.3090601@netcore.com.au> Hi Dave > Did you really leave two very-similar messages 3 minutes apart? Or are > you using a broken gateway, like Google-groups, and it hiccoughed? Sorry, I didn't intend to - flunky LH mouse microswitch! > Without knowing the type of the arguments being passed to the > crosspoint() function, we cannot say. I can GUESS that they are int, > long, or float. But you can find that out by examining the source > that's calling it. Judging from the comments, I might guess they're int > or long, and if so, only the division being done in the function > produces floats. If that's the case, then you're limited to float's > precision. if they are some other type (eg. Decimal), then indeed there > might be special functions being called. Well, the two scripts are only about an email page long each, shall I post them? > That import gets you access to the particular symbols it imports. > Normal arithmetic on floats is already built in, and doesn't need an import. Right! Thanks. > I'm assuming you're using CPython, and you say it's on XP. So > presumably you're running an Intel (or Intel-compatible) processor with > binary floating point built-in. That processor is the limit of float > values in normal use. It's good to about 18 digits. Sure, 32 bit uproc, intrinsic binary limit. > Exactly. It's a side-effect of the first import of the module. On > subsequent imports, the pyc file is used, unless the py file has been > modified meanwhile. Ah! Thanks! > 18 digits is what you should get if the code is as I describe. But if > there are lots of fp operations you're not showing, then an error can > gradually get larger. And with any finite precision, you have the risk > from things such as subtracting two nearly-equal values, which will > reduce the final precision. AFAIK, it's all FP. inputs are integers, outputs range from -2.000000000000 to 2.000000000000 > If you need more than 18, then go to the Decimal module, which lets you > set the precision to arbitrary levels. You will see a substantial > slowdown, of course, if you set the precision very high. if that > becomes a problem, consider CPython 3.3, which has optimized that > module. But try not to change too many things at once, as there are > lots of changes between 2.7 and 3.3. I think I'll need to from what you have said / pointed out - ie, for in excess of 'machine' precision, one needs to change base 10 floats to a higher base, do foo, then come back. Sounds daunting! John. From manalganesh at gmail.com Mon Oct 29 11:33:12 2012 From: manalganesh at gmail.com (Ganesh Manal) Date: Mon, 29 Oct 2012 16:03:12 +0530 Subject: [Tutor] Python Pipes Message-ID: Please give me sample python program that works with python31 Thanks & Regards, Ganesh Manal. "As soon as your dream become stronger than your doubts and fears , Your dream begins to manifest . " -------------- next part -------------- An HTML attachment was scrubbed... URL: From brian.van.den.broek at gmail.com Mon Oct 29 11:42:17 2012 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Mon, 29 Oct 2012 06:42:17 -0400 Subject: [Tutor] run with default value if input not given In-Reply-To: References: Message-ID: On 29 Oct 2012 02:30, "Saad Javed" wrote: > > I've come up with this: > > try: > sys.argv[1] > x = sys.argv[1] > main(x) > except IndexError: > main(x) > > It works but seems hackish. > > Saad Saad, The first sys.argv is not needed. Notice how i have replied below the text i am quoting? That is the preference of most on the list. Best, Brian Vdb -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Mon Oct 29 11:43:33 2012 From: __peter__ at web.de (Peter Otten) Date: Mon, 29 Oct 2012 11:43:33 +0100 Subject: [Tutor] greater precision? References: <508E505C.9030209@netcore.com.au> Message-ID: John Collins wrote: > Hi, > OS: XP > Py: 2.7.2 > > I am running some old scripts, not written by me, that produce > substantial calculated values data as decimals, 12 significant > figures. AFAIK, the keys calls are; [On modern hardware] Python uses IEEE 754 double-precision internally which gives 15 to 17 digits. But of course errors may accumulate. > def crosspoint(xa1,ya1,xa2,ya2,xb1,yb1,xb2,yb2): > "Give the intersection point of the (possibly extrapolated) lines\n"\ > "segments (xa1,ya1)-(xa2,ya2) and (xb1,yb1)-(xb2,yb2)." > dxa = xa2-xa1 > dya = ya2-ya1 > dxb = xb2-xb1 > dyb = yb2-yb1 > if dya * dxb == dxa * dyb: > return None > if dxa == 0: > return (xa1, (xa1 - xb1) * dyb / dxb + yb1) > if dxb == 0: > return (xb1, (xb1 - xa1) * dya / dxa + ya1) > if dya == 0: > return ((ya1 - yb1) * dxb / dyb + xb1, ya1) > if dyb == 0: > return ((yb1 - ya1) * dxa / dya + xa1, yb1) > > det = dya * dxb - dyb * dxa > xtop = dxb * dxa * (yb1-ya1) + dya * dxb * xa1 - dyb * dxa * xb1 > ytop = dya * dyb * (xa1-xb1) + dxb * dya * yb1 - dxa * dyb * ya1 > > return (xtop / det, ytop / det) > > > I am not sure what == means, Equality. >>> 1 == 0 False >>> 1 == 1 True Because of rounding errors this is a dangerous operation for floating point numbers: >>> print 1.0 == .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 False > nor if any 'special' maths functions are > called from crosspoint.py (to me, it appears not?), so, as I understand > it, the > > from math import > > line *is* the 'math engine' if you will, and is the source of the 12 > sig fig limit, yes? It is a toolbox rather than an engine. math is basically a wrapper around the C library, and these functions all use C's double and [most of the time] your hardware's floating point unit. > One other odd thing that occurs, is when the scripts are run, a weird, > small, non ASCII file called crosspoint.pyc is created? Is this the > interpreter 'compiling' crosspoint.py ( either before, during, or > after?) the 2nd script calls it? Yes, Python compiles the source to bytecode before it executes it, and for all modules except the main script this bytecode is cached in a .pyc file. > Sorry to bother, but if I can squeeze out *at least* 15 sig figs, (30 > or more would be better!) I'd be a happy camper! As you correctly observed crospoints() uses only +-*/ and ==, so you can feed it every type that supports these operations (this is called "duck typing"). For example: >>> from fractions import Fraction >>> from crosspoint import crosspoint >>> crosspoint(Fraction(0, 1), Fraction(1, 3), Fraction(1, 1), Fraction(1, 3), Fraction(5, 7), Fraction(0, 1), Fraction(5, 7), Fraction(1, 1)) (Fraction(5, 7), Fraction(1, 3)) >>> p = crosspoint(Fraction(0, 1), Fraction(1, 3), Fraction(1, 1), Fraction(1, 3), Fraction(5, 7), Fraction(0, 1), Fraction(5, 7), Fraction(1, 1)) >>> print "%s, %s" % p 5/7, 1/3 Yay, infinite precision ;) Of more practical relevance may be something like gmpy From __peter__ at web.de Mon Oct 29 11:47:13 2012 From: __peter__ at web.de (Peter Otten) Date: Mon, 29 Oct 2012 11:47:13 +0100 Subject: [Tutor] Python Pipes References: Message-ID: Ganesh Manal wrote: > Please give me sample python program that works with python31 $ touch sample.py $ cat sample.py $ python3 sample.py So the minimal python3 program is an empty file. From john at netcore.com.au Mon Oct 29 11:54:27 2012 From: john at netcore.com.au (John Collins) Date: Mon, 29 Oct 2012 21:54:27 +1100 Subject: [Tutor] greater precision? In-Reply-To: <508E5906.5060101@pearwood.info> References: <508E505C.9030209@netcore.com.au> <508E5906.5060101@pearwood.info> Message-ID: <508E6063.9090001@netcore.com.au> Hi Steve, Thanks. From >>>mkpoints.py 32 32.txt here's a sample of the output -0.396087591388 -0.781284022758 0.482400140683 -0.967387012461 -0.0838047084421 -0.239037944614 0.0208969821213 -0.489420208746 0.871797668848 0.887250003871 -0.258893773768 -0.38178717178 0.426352071227 -0.457758408728 -0.780180203927 0.612061168992 -0.280383142359 0.739436555016 -0.887250003871 0.258893773768 0.38178717178 -0.0971745158475 -0.994342015264 -0.0429076933695 -0.120898756509 -0.759794654167 -0.638823586113 -0.0208969821213 0.489420208746 -0.871797668848 0.482396765336 -0.834880934468 -0.265079584379 0.66383194755 0.726669941038 0.176855710123 from >>>nfaces.py 32.txt 32fa.txt here's a sample of the output point vertex_0_2_12 -0.0288974102653 -0.851924110364 0.66948446135 point vertex_13_24_27 -0.122445373457 1.00045419254 0.398644871129 point vertex_0_7_15 -0.482610585141 -0.963539328269 0.1161816686 point vertex_3_10_17 0.848541556491 -0.639691741968 -0.213520247975 point vertex_1_6_14 -1.05498774772 0.248634080415 -0.00106786881656 point vertex_13_24_31 -0.291794484064 0.826881428947 0.637135994637 point vertex_1_6_25 -1.07023716261 -0.0479998647263 0.164643927545 point vertex_3_17_28 1.05498774772 -0.248634080415 0.00106786881656 point vertex_1_15_25 -0.975134617659 -0.4675255693 -0.073154040374 point vertex_4_8_10 0.291794484064 -0.826881428947 -0.637135994637 point vertex_9_19_20 -0.400242088946 0.638705226984 -0.778897359983 normal face_30 -0.617395768043 -0.359222235879 -0.699844161834 face face_31 vertex_21_29_31 face face_31 vertex_13_21_31 face face_31 vertex_13_24_31 face face_31 vertex_6_24_31 face face_31 vertex_6_29_31 normal face_31 -0.426352071227 0.457758408728 0.780180203927 As you can see, there are exactly 12 significant figures. BTW, I did not write either script, I can post them, if you think doing so may show where it may be possible to escape to "decimal" as you say, perhaps? > "Decimal" has a specific meaning in Python different to how you > appear to be using it. It looks to me like you are working with > floats rather than decimals. Right. >> from math import pi, asin, atan2, cos, sin, sqrt >> from math import pi, asin, atan2, cos, sin, sqrt Two different scripts. > "crosspoint" appears to be a custom Python program that we know > nothing about, apart from what you tell us. I posted it in it's entirety. > "==" is used for equality testing: > x == 2 > returns False, because x does not equal 2; but > x == 1 > returns True, because x does currently equal 1. Ah! Thanks! > Almost. The "from math import blah..." line extracts a bunch of maths > functions from the math library and makes them available to your > program. I'm seeing this now. Python does minimal things, extras are called in only as needed. > The number of significant figures is more fundamental that that; your > operating system understands about floating point numbers (so-called > "C doubles") with 53 *binary* significant figures, or about 17 *decimal* > figures. So I'm not sure why you think there are only 12. Sure. But see the above, from Py 2.7.2, it's precisely 12 sig.figs.! > You need to show us how the output is generated in the first place. Done, above. > Not a chance without some major changes to your program. I was afraid of this. > I can't say how major without seeing more of your program. Well, OK, here's the first, mkpoints.py #!/usr/bin/env python # Distribute a set of points randomly across a sphere, allow them # to mutually repel and find equilibrium. import sys import string import random from math import pi, asin, atan2, cos, sin, sqrt args = sys.argv[1:] if len(args) > 0: n = string.atoi(sys.argv[1]) args = args[1:] else: n = 7 if len(args) > 0: outfile = open(args[0], "w") args = args[1:] else: outfile = sys.stdout points = [] def realprint(a): for i in range(len(a)): outfile.write(str(a[i])) if i < len(a)-1: outfile.write(" ") else: outfile.write("\n") def pprint(*a): realprint(a) for i in range(n): # Invent a randomly distributed point. # # To distribute points uniformly over a spherical surface, the # easiest thing is to invent its location in polar coordinates. # Obviously theta (longitude) must be chosen uniformly from # [0,2*pi]; phi (latitude) must be chosen in such a way that # the probability of falling within any given band of latitudes # must be proportional to the total surface area within that # band. In other words, the probability _density_ function at # any value of phi must be proportional to the circumference of # the circle around the sphere at that latitude. This in turn # is proportional to the radius out from the sphere at that # latitude, i.e. cos(phi). Hence the cumulative probability # should be proportional to the integral of that, i.e. sin(phi) # - and since we know the cumulative probability needs to be # zero at -pi/2 and 1 at +pi/2, this tells us it has to be # (1+sin(phi))/2. # # Given an arbitrary cumulative probability function, we can # select a number from the represented probability distribution # by taking a uniform number in [0,1] and applying the inverse # of the function. In this case, this means we take a number X # in [0,1], scale and translate it to obtain 2X-1, and take the # inverse sine. Conveniently, asin() does the Right Thing in # that it maps [-1,+1] into [-pi/2,pi/2]. theta = random.random() * 2*pi phi = asin(random.random() * 2 - 1) points.append((cos(theta)*cos(phi), sin(theta)*cos(phi), sin(phi))) # For the moment, my repulsion function will be simple # inverse-square, followed by a normalisation step in which we pull # each point back to the surface of the sphere. while 1: # Determine the total force acting on each point. forces = [] for i in range(len(points)): p = points[i] f = (0,0,0) ftotal = 0 for j in range(len(points)): if j == i: continue q = points[j] # Find the distance vector, and its length. dv = (p[0]-q[0], p[1]-q[1], p[2]-q[2]) dl = sqrt(dv[0]**2 + dv[1]**2 + dv[2]**2) # The force vector is dv divided by dl^3. (We divide by # dl once to make dv a unit vector, then by dl^2 to # make its length correspond to the force.) dl3 = dl ** 3 fv = (dv[0]/dl3, dv[1]/dl3, dv[2]/dl3) # Add to the total force on the point p. f = (f[0]+fv[0], f[1]+fv[1], f[2]+fv[2]) # Stick this in the forces array. forces.append(f) # Add to the running sum of the total forces/distances. ftotal = ftotal + sqrt(f[0]**2 + f[1]**2 + f[2]**2) # Scale the forces to ensure the points do not move too far in # one go. Otherwise there will be chaotic jumping around and # never any convergence. if ftotal > 0.25: fscale = 0.25 / ftotal else: fscale = 1 # Move each point, and normalise. While we do this, also track # the distance each point ends up moving. dist = 0 for i in range(len(points)): p = points[i] f = forces[i] p2 = (p[0] + f[0]*fscale, p[1] + f[1]*fscale, p[2] + f[2]*fscale) pl = sqrt(p2[0]**2 + p2[1]**2 + p2[2]**2) p2 = (p2[0] / pl, p2[1] / pl, p2[2] / pl) dv = (p[0]-p2[0], p[1]-p2[1], p[2]-p2[2]) dl = sqrt(dv[0]**2 + dv[1]**2 + dv[2]**2) dist = dist + dl points[i] = p2 # Done. Check for convergence and finish. sys.stderr.write(str(dist) + "\n") if dist < 2.10005e-16: break # Output the points. for x, y, z in points: pprint(x, y, z) The nfaces.py used on the output from this is a tad longer! > Well, yes, but only with some significant changes Well, it seems I may have to learn "decimal" python, and insert it into the above, and nfaces.py. John. From breamoreboy at yahoo.co.uk Mon Oct 29 11:57:44 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 29 Oct 2012 10:57:44 +0000 Subject: [Tutor] Python Pipes In-Reply-To: References: Message-ID: On 29/10/2012 10:33, Ganesh Manal wrote: > Please give me sample python program that works with python31 > > Thanks & Regards, > Ganesh Manal. > > "As soon as your dream become stronger than your doubts and fears , Your > dream begins to manifest . " > Please give me a signed and dated cheque with the areas for the payment left blank so that I can fill them in myself. -- Cheers. Mark Lawrence. From d at davea.name Mon Oct 29 12:05:05 2012 From: d at davea.name (Dave Angel) Date: Mon, 29 Oct 2012 07:05:05 -0400 Subject: [Tutor] greater precision? In-Reply-To: <508E5B44.3090601@netcore.com.au> References: <508E513E.4000203@netcore.com.au> <508E584B.6040000@davea.name> <508E5B44.3090601@netcore.com.au> Message-ID: <508E62E1.2070007@davea.name> On 10/29/2012 06:32 AM, John Collins wrote: > Hi Dave >> Did you really leave two very-similar messages 3 minutes apart? Or are >> you using a broken gateway, like Google-groups, and it hiccoughed? > Sorry, I didn't intend to - flunky LH mouse microswitch! I was only concerned that there was some correction in one of the messages. And since there was only 3 minutes between them, i didn't know which might be the corrected one. They showed up here out of order. I responded to the one with the later timestamp. > >> > Well, the two scripts are only about an email page long each, shall > I post them? > Just post one of them. Otherwise, things can get very confusing. >> I'm assuming you're using CPython, and you say it's on >>> Exactly. It's a side-effect of the first import of the module. On >>> subsequent imports, the pyc file is used, unless the py file has been >>> modified meanwhile. >> Ah! Thanks! >> XP. So >> presumably you're running an Intel (or Intel-compatible) processor with >> binary floating point built-in. That processor is the limit of float >> values in normal use. It's good to about 18 digits. > Sure, 32 bit uproc, intrinsic binary limit. Actually, it's 64 bits. 32 bit fp wouldn't get you anywhere near 18 digits. >> > > AFAIK, it's all FP. inputs are integers, outputs range from > -2.000000000000 to 2.000000000000 Since the inputs to that function are ints, then the output will be IEEE floats. that also means that the == comparisons in the crosspoint() function are precise. >> If you need more than 18, then go to the Decimal module, which lets you >> set the precision to arbitrary levels. You will see a substantial >> slowdown, of course, if you set the precision very high. if that >> becomes a problem, consider CPython 3.3, which has optimized that >> module. But try not to change too many things at once, as there are >> lots of changes between 2.7 and 3.3. > I think I'll need to from what you have said / pointed out - ie, for > in excess of 'machine' precision, one needs to change base 10 floats > to a higher base, do foo, then come back. Sounds daunting! Actually, the base change is already happening. Using Decimal would probably reduce the number of base changes. And if you create the numbers as Decimal objects, then they'll propagate through the crosspoint() function correctly. However, even though the decimal module supports log and sqrt, I don't know how to do trig with it. Incidentally, I didn't suggest the fractions module, since you're presumably going to be doing trig on the results, so I don't see the benefit. BTW, I misspoke earlier. The module name is decimal. The class name within that module is Decimal. -- DaveA From john at netcore.com.au Mon Oct 29 12:19:24 2012 From: john at netcore.com.au (John Collins) Date: Mon, 29 Oct 2012 22:19:24 +1100 Subject: [Tutor] greater precision? In-Reply-To: References: <508E505C.9030209@netcore.com.au> Message-ID: <508E663C.1080608@netcore.com.au> Hi Peter, Thanks. > [On modern hardware] Python uses IEEE 754 double-precision > internally which gives 15 to > 17 digits. But of course errors may accumulate. As in my previous, this script seems to output precisely 12 significant not 15 and not 17. ??? 15 and I'd be happier right now, as it matches Excel's precision there, 'as it is'. > Equality. Right, in a 1,0 sense. > Because of rounding errors this is a dangerous operation for floating point > numbers: >>>> print 1.0 == .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 > False I take it that is because binary for .1 is a repeating sequence, yes? > It is a toolbox rather than an engine. Ah ha! *Now* I'm getting it! > math is basically a wrapper around the C library, and these functions all > use C's double and [most of the time] your hardware's floating point unit. Right! > Yes, Python compiles the source to bytecode before it executes it, and for > all modules except the main script this bytecode is cached in a .pyc file. That nails it (as I was wondering, just now, why the primary scripts remained intact). > As you correctly observed crospoints() uses only +-*/ and ==, so you can > feed it every type that supports these operations (this is called "duck > typing"). For example: >>>> from fractions import Fraction >>>> from crosspoint import crosspoint >>>> crosspoint(Fraction(0, 1), Fraction(1, 3), Fraction(1, 1), Fraction(1, > 3), Fraction(5, 7), Fraction(0, 1), Fraction(5, 7), Fraction(1, 1)) > (Fraction(5, 7), Fraction(1, 3)) >>>> p = crosspoint(Fraction(0, 1), Fraction(1, 3), Fraction(1, 1), > Fraction(1, 3), Fraction(5, 7), Fraction(0, 1), Fraction(5, 7), Fraction(1, > 1)) >>>> print "%s, %s" % p > 5/7, 1/3 > > Yay, infinite precision ;) You just lost me. > Of more practical relevance may be something like gmpy > I've just had a peek. Looks like what I need. Haven't a clue if I'll understand how to 'patch it in' to the scripts. Doco read time! John. From d at davea.name Mon Oct 29 12:34:57 2012 From: d at davea.name (Dave Angel) Date: Mon, 29 Oct 2012 07:34:57 -0400 Subject: [Tutor] greater precision? In-Reply-To: <508E6063.9090001@netcore.com.au> References: <508E505C.9030209@netcore.com.au> <508E5906.5060101@pearwood.info> <508E6063.9090001@netcore.com.au> Message-ID: <508E69E1.3030308@davea.name> On 10/29/2012 06:54 AM, John Collins wrote: > Hi Steve, > Thanks. From > >>>mkpoints.py 32 32.txt > > here's a sample of the output > > -0.396087591388 -0.781284022758 0.482400140683 > -0.967387012461 -0.0838047084421 -0.239037944614 > 0.0208969821213 -0.489420208746 0.871797668848 > 0.887250003871 -0.258893773768 -0.38178717178 > 0.426352071227 -0.457758408728 -0.780180203927 > 0.612061168992 -0.280383142359 0.739436555016 > -0.887250003871 0.258893773768 0.38178717178 > -0.0971745158475 -0.994342015264 -0.0429076933695 > -0.120898756509 -0.759794654167 -0.638823586113 > -0.0208969821213 0.489420208746 -0.871797668848 > 0.482396765336 -0.834880934468 -0.265079584379 > 0.66383194755 0.726669941038 0.176855710123 > > > from > >>>nfaces.py 32.txt 32fa.txt > > here's a sample of the output > > point vertex_0_2_12 -0.0288974102653 -0.851924110364 0.66948446135 > point vertex_13_24_27 -0.122445373457 1.00045419254 0.398644871129 > point vertex_0_7_15 -0.482610585141 -0.963539328269 0.1161816686 > point vertex_3_10_17 0.848541556491 -0.639691741968 -0.213520247975 > point vertex_1_6_14 -1.05498774772 0.248634080415 -0.00106786881656 > point vertex_13_24_31 -0.291794484064 0.826881428947 0.637135994637 > point vertex_1_6_25 -1.07023716261 -0.0479998647263 0.164643927545 > point vertex_3_17_28 1.05498774772 -0.248634080415 0.00106786881656 > point vertex_1_15_25 -0.975134617659 -0.4675255693 -0.073154040374 > point vertex_4_8_10 0.291794484064 -0.826881428947 -0.637135994637 > point vertex_9_19_20 -0.400242088946 0.638705226984 -0.778897359983 > normal face_30 -0.617395768043 -0.359222235879 -0.699844161834 > face face_31 vertex_21_29_31 > face face_31 vertex_13_21_31 > face face_31 vertex_13_24_31 > face face_31 vertex_6_24_31 > face face_31 vertex_6_29_31 > normal face_31 -0.426352071227 0.457758408728 0.780180203927 > > > As you can see, there are exactly 12 significant figures. Not true. That's just what it was truncated to upon output. If you want to show 15 digits, then use formatted output instead of str() inside the realprint() function. > > > def realprint(a): > for i in range(len(a)): > outfile.write(str(a[i])) > if i < len(a)-1: > outfile.write(" ") > else: > outfile.write("\n") > output.write("{0:.15f}".format(x)) You may want to play with the format a bit to get it the way you actually want it. Note that the format will happily output nonsense digits beyond the end of the available precision, so you have to do your own figuring before deciding what to display. -- DaveA From john at netcore.com.au Mon Oct 29 12:39:59 2012 From: john at netcore.com.au (John Collins) Date: Mon, 29 Oct 2012 22:39:59 +1100 Subject: [Tutor] greater precision? In-Reply-To: <508E62E1.2070007@davea.name> References: <508E513E.4000203@netcore.com.au> <508E584B.6040000@davea.name> <508E5B44.3090601@netcore.com.au> <508E62E1.2070007@davea.name> Message-ID: <508E6B0F.4080507@netcore.com.au> Hi Dave, Thanks, more insights! > I was only concerned that there was some correction in one of the > messages. And since there was only 3 minutes between them, i didn't > know which might be the corrected one. They showed up here out of > order. I responded to the one with the later timestamp. No, mia culpa. > Just post one of them. Otherwise, things can get very confusing. Done. > Actually, it's 64 bits. 32 bit fp wouldn't get you anywhere near 18 digits. Darn, of course, you're right! Senior moment for me! > Since the inputs to that function are ints, then the output will be IEEE > floats. that also means that the == comparisons in the crosspoint() > function are precise. THAT is a very valuable insight Dave, thanks! > Actually, the base change is already happening. Using Decimal would > probably reduce the number of base changes. And if you create the > numbers as Decimal objects, then they'll propagate through the > crosspoint() function correctly. However, even though the decimal > module supports log and sqrt, I don't know how to do trig with it. Well, as trig is *core essential* to polyhedra maths, I *have* to use *lots* of it! > Incidentally, I didn't suggest the fractions module, since you're > presumably going to be doing trig on the results, so I don't see the > benefit. Yes. Core maths is +,-,*,/,sin,cos,tan (and their inverses), sqrt, ^2,^3, (ie, exponentials), etc off hand. > BTW, I misspoke earlier. The module name is decimal. The class name > within that module is Decimal. A minor thing to me, a non programmer, but I do understand that being very precise is very important to programmers, so thank you! John. From d at davea.name Mon Oct 29 12:40:39 2012 From: d at davea.name (Dave Angel) Date: Mon, 29 Oct 2012 07:40:39 -0400 Subject: [Tutor] greater precision? In-Reply-To: <508E663C.1080608@netcore.com.au> References: <508E505C.9030209@netcore.com.au> <508E663C.1080608@netcore.com.au> Message-ID: <508E6B37.70806@davea.name> On 10/29/2012 07:19 AM, John Collins wrote: > >> Equality. > Right, in a 1,0 sense. >> Because of rounding errors this is a dangerous operation for floating >> point >> numbers: >>>>> print 1.0 == .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 >> False > I take it that is because binary for .1 is a repeating sequence, yes? Yes. Since it cannot be represented exactly, you get a quantization error. > >> As you correctly observed crospoints() uses only +-*/ and ==, so you can >> feed it every type that supports these operations (this is called "duck >> typing"). For example: > >>>>> from fractions import Fraction >>>>> from crosspoint import crosspoint >>>>> crosspoint(Fraction(0, 1), Fraction(1, 3), Fraction(1, 1), >>>>> Fraction(1, >> 3), Fraction(5, 7), Fraction(0, 1), Fraction(5, 7), Fraction(1, 1)) >> (Fraction(5, 7), Fraction(1, 3)) >>>>> p = crosspoint(Fraction(0, 1), Fraction(1, 3), Fraction(1, 1), >> Fraction(1, 3), Fraction(5, 7), Fraction(0, 1), Fraction(5, 7), >> Fraction(1, >> 1)) >>>>> print "%s, %s" % p >> 5/7, 1/3 >> >> Yay, infinite precision ;) > You just lost me. If you don't use any transcendental functions, then a fraction has no quantization error. It's totally precise. -- DaveA From john at netcore.com.au Mon Oct 29 12:49:23 2012 From: john at netcore.com.au (John Collins) Date: Mon, 29 Oct 2012 22:49:23 +1100 Subject: [Tutor] greater precision? In-Reply-To: <508E69E1.3030308@davea.name> References: <508E505C.9030209@netcore.com.au> <508E5906.5060101@pearwood.info> <508E6063.9090001@netcore.com.au> <508E69E1.3030308@davea.name> Message-ID: <508E6D43.4050208@netcore.com.au> Hi Dave, > Not true. That's just what it was truncated to upon output. If you > want to show 15 digits, then use formatted output instead of str() > inside the realprint() function. BIG light bulb moment! I had no idea the str() statement was truncating! I *did* search the script for "12" or "trunc" (being a newbie!) and saw nothing. Hence my silly questions tonight! >> >> >> def realprint(a): >> for i in range(len(a)): >> outfile.write(str(a[i])) >> if i < len(a)-1: >> outfile.write(" ") >> else: >> outfile.write("\n") >> > > output.write("{0:.15f}".format(x)) > > You may want to play with the format a bit to get it the way you > actually want it. Note that the format will happily output nonsense > digits beyond the end of the available precision, so you have to do your > own figuring before deciding what to display. Hmm, well, I did modify the original script by setting the convergence test value to 2.005e-16 up from 1e-6, soooo, I 'guess' 15 sig.figs. will be OK. I'll have a go! That's 3 orders of magnitude better for a few key strokes! Great! (maybe...) John. From john at netcore.com.au Mon Oct 29 12:51:35 2012 From: john at netcore.com.au (John Collins) Date: Mon, 29 Oct 2012 22:51:35 +1100 Subject: [Tutor] greater precision? In-Reply-To: <508E6B37.70806@davea.name> References: <508E505C.9030209@netcore.com.au> <508E663C.1080608@netcore.com.au> <508E6B37.70806@davea.name> Message-ID: <508E6DC7.5030903@netcore.com.au> Hi Dave, >> You just lost me. > > If you don't use any transcendental functions, then a fraction has no > quantization error. It's totally precise. Ah ha! Another light bulb moment - two in one night! Thank you Dave, and all! John. From d at davea.name Mon Oct 29 13:00:55 2012 From: d at davea.name (Dave Angel) Date: Mon, 29 Oct 2012 08:00:55 -0400 Subject: [Tutor] greater precision? In-Reply-To: <508E6D43.4050208@netcore.com.au> References: <508E505C.9030209@netcore.com.au> <508E5906.5060101@pearwood.info> <508E6063.9090001@netcore.com.au> <508E69E1.3030308@davea.name> <508E6D43.4050208@netcore.com.au> Message-ID: <508E6FF7.1030505@davea.name> On 10/29/2012 07:49 AM, John Collins wrote: > Hi Dave, >> Not true. That's just what it was truncated to upon output. If you >> want to show 15 digits, then use formatted output instead of str() >> inside the realprint() function. > BIG light bulb moment! I had no idea the str() statement was truncating! > I *did* search the script for "12" or "trunc" (being a newbie!) and saw > nothing. Hence my silly questions tonight! Not silly at all. I didn't realize str(float) would truncate to 12 digits either. I found out by experimenting (with 2.7) in the interpreter. Note that it may differ from version to version. And that's another reason to use format. But please don't just take the format string I supplied as "right." It is if you always want 15 decimal digits to the right of the decimal point. But if you have a number like 50 thousand, then many of those digits to the right are invalid. -- DaveA From john at netcore.com.au Mon Oct 29 13:14:48 2012 From: john at netcore.com.au (John Collins) Date: Mon, 29 Oct 2012 23:14:48 +1100 Subject: [Tutor] greater precision? In-Reply-To: <508E6FF7.1030505@davea.name> References: <508E505C.9030209@netcore.com.au> <508E5906.5060101@pearwood.info> <508E6063.9090001@netcore.com.au> <508E69E1.3030308@davea.name> <508E6D43.4050208@netcore.com.au> <508E6FF7.1030505@davea.name> Message-ID: <508E7338.60300@netcore.com.au> Hi Dave, > Not silly at all. I didn't realize str(float) would truncate to 12 > digits either. I found out by experimenting (with 2.7) in the interpreter. Very gracious of you to say, and generous of you to trouble yourself to experiment - thank you very much! > Note that it may differ from version to version. And that's another > reason to use format. Excellent point! > But please don't just take the format string I supplied as "right." It > is if you always want 15 decimal digits to the right of the decimal > point. But if you have a number like 50 thousand, then many of those > digits to the right are invalid. Indeed! As you saw, some outputs are, and really must be output as integers. A 1 with a decimal point followed by 15 0's would make a real mess of the face/vertex designation table. So I have to be careful where I invoke output.write("{0:.15f}".format(x)) or similar, ie, that it operates only on the values I'd like output that way. Which raises a question. Is the command 'persistent'? In other words, if I use it early on, but then wish to output integers, has it 'died after use', do I need to 'kill it after use', or will python just default back to what it sees next? I know this sounds silly, but to me the command 'looks' like it's setting a 'hidden pyton variable' to "15f" which may need to be explicitly revoked, or not,...??? John. From steve at pearwood.info Mon Oct 29 13:10:54 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 29 Oct 2012 23:10:54 +1100 Subject: [Tutor] Python Pipes In-Reply-To: References: Message-ID: <508E724E.7090009@pearwood.info> On 29/10/12 21:33, Ganesh Manal wrote: > Please give me sample python program that works with python31 # start program print("hello world") # end program -- Steven From d at davea.name Mon Oct 29 13:29:55 2012 From: d at davea.name (Dave Angel) Date: Mon, 29 Oct 2012 08:29:55 -0400 Subject: [Tutor] greater precision? In-Reply-To: <508E7338.60300@netcore.com.au> References: <508E505C.9030209@netcore.com.au> <508E5906.5060101@pearwood.info> <508E6063.9090001@netcore.com.au> <508E69E1.3030308@davea.name> <508E6D43.4050208@netcore.com.au> <508E6FF7.1030505@davea.name> <508E7338.60300@netcore.com.au> Message-ID: <508E76C3.1040002@davea.name> On 10/29/2012 08:14 AM, John Collins wrote: > >> But please don't just take the format string I supplied as "right." It >> is if you always want 15 decimal digits to the right of the decimal >> point. But if you have a number like 50 thousand, then many of those >> digits to the right are invalid. > Indeed! As you saw, some outputs are, and really must be output as > integers. A 1 with a decimal point followed by 15 0's would make a > real mess of the face/vertex designation table. So I have to be > careful where I invoke > output.write("{0:.15f}".format(x)) > or similar, ie, that it operates only on the values I'd like output > that way. > Which raises a question. Is the command 'persistent'? In other words, > if I use it early on, but then wish to output integers, has it 'died > after use', do I need to 'kill it after use', or will python just > default back to what it sees next? I know this sounds silly, but to me > the command 'looks' like it's setting a 'hidden pyton variable' to > "15f" which may need to be explicitly revoked, or not,...??? > John. > Valid question. However, there are no hidden variables used in format. Each time you invoke the format method (it's a method of str), it starts from scratch using only its current arguments. i can't think of any sense in which 'default' fits here, either. You do really need to study the format spec, to see what other parameters may be more useful. What I supplied is great if all the digits are to the right of the decimal. Note, you can also use format on integers, in order to make a fixed number of columns, for example. Or to get leading zeroes. Or whatever. And realize that the format does not have to be "inside" the write. So you can format to a string, then post-process in some way, before sending to the write method. Incidentally, One of numpy or gmpy is probably a very good idea for you. But as I have no experience with either, it just didn't occur to me. So I'm glad Peter mentioned one of them. The only catch I know of is it's an external dependency, meaning you have to download and install it into the Python search path. -- DaveA From john at netcore.com.au Mon Oct 29 13:45:58 2012 From: john at netcore.com.au (John Collins) Date: Mon, 29 Oct 2012 23:45:58 +1100 Subject: [Tutor] greater precision? In-Reply-To: <508E76C3.1040002@davea.name> References: <508E505C.9030209@netcore.com.au> <508E5906.5060101@pearwood.info> <508E6063.9090001@netcore.com.au> <508E69E1.3030308@davea.name> <508E6D43.4050208@netcore.com.au> <508E6FF7.1030505@davea.name> <508E7338.60300@netcore.com.au> <508E76C3.1040002@davea.name> Message-ID: <508E7A86.8000202@netcore.com.au> Hi Dave, > Valid question. However, there are no hidden variables used in format. > Each time you invoke the format method (it's a method of str), it starts > from scratch using only its current arguments. i can't think of any > sense in which 'default' fits here, either. Thanks, that's somewhat of a relief. In nfaces.py which outputs like this point vertex_11_22_27 0.482610585141 0.963539328269 -0.1161816686 point vertex_11_18_28 0.894075981233 0.461195737945 0.403417680839 face face_0 vertex_0_2_29 face face_0 vertex_0_25_29 face face_0 vertex_0_15_25 face face_0 vertex_0_7_15 face face_0 vertex_0_7_12 face face_0 vertex_0_2_12 normal face_0 -0.396087591388 -0.781284022758 0.482400140683 face face_1 vertex_1_15_30 there are nested outfile.write commands with logical tests! Best now I try your suggestions - experiment, I can't break it(!), and if after a sleepless night, it's all pea soup, I'll log back in with this marvellous group of very knowledgeable and helpful folks! > You do really need to study the format spec, to see what other > parameters may be more useful. What I supplied is great if all the > digits are to the right of the decimal. Agreed. > Note, you can also use format on integers, in order to make a fixed > number of columns, for example. Or to get leading zeroes. Or whatever. Interesting... > And realize that the format does not have to be "inside" the write. So > you can format to a string, then post-process in some way, before > sending to the write method. Very interesting! > Incidentally, One of numpy or gmpy is probably a very good idea for > you. But as I have no experience with either, it just didn't occur to > me. So I'm glad Peter mentioned one of them. The only catch I know of > is it's an external dependency, meaning you have to download and install > it into the Python search path. Yes! A while back, Ramit mentioned; "P.S. If you want large amount of accuracy with number crunching, you may want to look into using fixed-precision or binary math libraries. Try looking at scipy/numpy or using Decimal instead of floating-point numbers. (This is just a suggestion and may be incorrect because I have not yet needed such accuracy. )" Which I just searched for and dug up! Right, I'm off to bash this script like a beginner, and see what emerges! :) Thanks for so many insights in one night - terrific! Wish I'd picked this language up *years* ago - it's so foreign to me now, yet so beautifully useful! Cheers, John. From brianjamesarb at gmail.com Mon Oct 29 14:21:26 2012 From: brianjamesarb at gmail.com (brian arb) Date: Mon, 29 Oct 2012 09:21:26 -0400 Subject: [Tutor] Python Pipes In-Reply-To: References: Message-ID: Suggestion that you restate your request in the form of a question that is less generic and more specific to what you are looking for. On Mon, Oct 29, 2012 at 6:33 AM, Ganesh Manal wrote: > Please give me sample python program that works with python31 > > > Thanks & Regards, > Ganesh Manal. > > "As soon as your dream become stronger than your doubts and fears , Your > dream begins to manifest . " > > _______________________________________________ > 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 Mon Oct 29 14:29:44 2012 From: emile at fenx.com (Emile van Sebille) Date: Mon, 29 Oct 2012 06:29:44 -0700 Subject: [Tutor] Python Pipes In-Reply-To: References: Message-ID: On 10/29/2012 3:33 AM, Ganesh Manal wrote: > Please give me sample python program that works with python31 Start with the tutorial at http://docs.python.org/3/tutorial/index.html It'll step you through lots of sample python scripts. Emile From oscar.j.benjamin at gmail.com Mon Oct 29 14:32:19 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 29 Oct 2012 13:32:19 +0000 Subject: [Tutor] run with default value if input not given In-Reply-To: References: Message-ID: On 29 October 2012 06:06, Saad Javed wrote: > Hi, > > #!/usr/bin/env python > > import sys > > x = 'Saad is a boy' > > def main(x): > a = [] > b = x.split(' ') > for item in b: > a.append(item) > print a > if __name__ == '__main__': > x = sys.argv[1] > main(x) > > > How can I make this program run with the default value of x if I don't > specify an argument at the command line? My preference is to use Python's default function argument mechanism to fill in the missing values: def main(x, y='default'): print(x) print(y) if __name__ == "__main__": main(sys.argv[1:]) This is the quickest way to get what you want. For a proper script, though, I would use an argument parser such as argparse. Oscar From oscar.j.benjamin at gmail.com Mon Oct 29 14:33:40 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 29 Oct 2012 13:33:40 +0000 Subject: [Tutor] run with default value if input not given In-Reply-To: References: Message-ID: On 29 October 2012 13:32, Oscar Benjamin wrote: > > def main(x, y='default'): > print(x) > print(y) > > if __name__ == "__main__": > main(sys.argv[1:]) A quick correction. That should be (note the *): if __name__ == "__main__": main(*sys.argv[1:]) Oscar From eryksun at gmail.com Mon Oct 29 16:02:35 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 29 Oct 2012 11:02:35 -0400 Subject: [Tutor] greater precision? In-Reply-To: <508E62E1.2070007@davea.name> References: <508E513E.4000203@netcore.com.au> <508E584B.6040000@davea.name> <508E5B44.3090601@netcore.com.au> <508E62E1.2070007@davea.name> Message-ID: On Mon, Oct 29, 2012 at 7:05 AM, Dave Angel wrote: > > Actually, it's 64 bits. 32 bit fp wouldn't get you anywhere near 18 digits. A double has 53 bits of precisions, which is 53*log10(2) =~ 15.955 decimal digits. However, one often sees the numbers 15 and 17 quoted for the precision. It depends. A double is guaranteed to accurately store a string with 15 decimal digits (round trip). But each 15-digit decimal string maps to many doubles: >>> from struct import unpack >>> format(unpack('d', '\x76\x99\x99\x99\x99\x99\xb9?')[0], '.15f') '0.100000000000000' >>> format(unpack('d', '\xbd\x99\x99\x99\x99\x99\xb9?')[0], '.15f') '0.100000000000000' >>> 0xbd - 0x76 + 1 # doubles that round to 0.100000000000000 72 (Note: my Intel processor is little endian, so the least significant byte is index 0 in the packed double, such as '\x76....'.) However, to exactly represent each double requires 17 decimal digits: >>> format(unpack('d', '\x76\x99\x99\x99\x99\x99\xb9?')[0], '.17f') '0.09999999999999951' >>> format(unpack('d', '\x77\x99\x99\x99\x99\x99\xb9?')[0], '.17f') '0.09999999999999952' Python says the precision is 15 decimal digits: >>> import sys >>> sys.float_info.mant_dig # bits of precision 53 >>> sys.float_info.dig # decimal digits 15 From breamoreboy at yahoo.co.uk Mon Oct 29 16:15:43 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 29 Oct 2012 15:15:43 +0000 Subject: [Tutor] greater precision? In-Reply-To: <508E6FF7.1030505@davea.name> References: <508E505C.9030209@netcore.com.au> <508E5906.5060101@pearwood.info> <508E6063.9090001@netcore.com.au> <508E69E1.3030308@davea.name> <508E6D43.4050208@netcore.com.au> <508E6FF7.1030505@davea.name> Message-ID: On 29/10/2012 12:00, Dave Angel wrote: > > Not silly at all. I didn't realize str(float) would truncate to 12 > digits either. I found out by experimenting (with 2.7) in the interpreter. > > Note that it may differ from version to version. And that's another > reason to use format. > It's 16 digits with 3.3.0. -- Cheers. Mark Lawrence. From eryksun at gmail.com Mon Oct 29 16:25:20 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 29 Oct 2012 11:25:20 -0400 Subject: [Tutor] Python Pipes In-Reply-To: References: Message-ID: On Mon, Oct 29, 2012 at 6:33 AM, Ganesh Manal wrote: > > Please give me sample python program that works with python31 Re: Python Pipes If you're looking to pipe data to, from, and between processes, look at the subprocess module: http://docs.python.org/release/3.1/library/subprocess If you want something specific to a Unix shell, look at the pipes module: http://docs.python.org/release/3.1/library/pipes If you want your own program to behave differently when stdin/stdout are piped, use isatty(): >>> import sys >>> sys.stdin.isatty() True >>> sys.stdout.isatty() True http://docs.python.org/release/3.1/library/io#io.IOBase.isatty From john at netcore.com.au Mon Oct 29 16:42:15 2012 From: john at netcore.com.au (John Collins) Date: Tue, 30 Oct 2012 02:42:15 +1100 Subject: [Tutor] greater precision? In-Reply-To: References: <508E513E.4000203@netcore.com.au> <508E584B.6040000@davea.name> <508E5B44.3090601@netcore.com.au> <508E62E1.2070007@davea.name> Message-ID: <508EA3D7.8010508@netcore.com.au> Hello, Just checked my py, and 15 was the report! Wish I had known that factoid - thank you, for a very complete coverage of the broader intrinsic 'machine' + system precision - it actually makes sense to me now - it's a calculation! On 30/10/2012 2:02 AM, eryksun wrote: > A double has 53 bits of precisions, which is 53*log10(2) =~ 15.955 > decimal digits. However, one often sees the numbers 15 and 17 quoted > for the precision. It depends. A double is guaranteed to accurately > store a string with 15 decimal digits (round trip). But each 15-digit > decimal string maps to many doubles: > > >>> from struct import unpack > > >>> format(unpack('d', '\x76\x99\x99\x99\x99\x99\xb9?')[0], '.15f') > '0.100000000000000' > >>> format(unpack('d', '\xbd\x99\x99\x99\x99\x99\xb9?')[0], '.15f') > '0.100000000000000' > > >>> 0xbd - 0x76 + 1 # doubles that round to 0.100000000000000 > 72 > > (Note: my Intel processor is little endian, so the least significant > byte is index 0 in the packed double, such as '\x76....'.) > > However, to exactly represent each double requires 17 decimal digits: > > >>> format(unpack('d', '\x76\x99\x99\x99\x99\x99\xb9?')[0], '.17f') > '0.09999999999999951' > >>> format(unpack('d', '\x77\x99\x99\x99\x99\x99\xb9?')[0], '.17f') > '0.09999999999999952' > > Python says the precision is 15 decimal digits: > > >>> import sys > >>> sys.float_info.mant_dig # bits of precision > 53 > >>> sys.float_info.dig # decimal digits > 15 > Regs, John. From breamoreboy at yahoo.co.uk Mon Oct 29 16:45:26 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 29 Oct 2012 15:45:26 +0000 Subject: [Tutor] greater precision? In-Reply-To: <508E7338.60300@netcore.com.au> References: <508E505C.9030209@netcore.com.au> <508E5906.5060101@pearwood.info> <508E6063.9090001@netcore.com.au> <508E69E1.3030308@davea.name> <508E6D43.4050208@netcore.com.au> <508E6FF7.1030505@davea.name> <508E7338.60300@netcore.com.au> Message-ID: On 29/10/2012 12:14, John Collins wrote: > Indeed! As you saw, some outputs are, and really must be output as > integers. A 1 with a decimal point followed by 15 0's would make a real > mess of the face/vertex designation table. So I have to be careful where > I invoke > output.write("{0:.15f}".format(x)) > or similar, ie, that it operates only on the values I'd like output > that way. > If you're more comfortable with C you can use printf style formatting see http://docs.python.org/2/library/stdtypes.html#string-formatting -- Cheers. Mark Lawrence. From john at netcore.com.au Mon Oct 29 16:46:51 2012 From: john at netcore.com.au (John Collins) Date: Tue, 30 Oct 2012 02:46:51 +1100 Subject: [Tutor] greater precision? In-Reply-To: References: <508E505C.9030209@netcore.com.au> <508E5906.5060101@pearwood.info> <508E6063.9090001@netcore.com.au> <508E69E1.3030308@davea.name> <508E6D43.4050208@netcore.com.au> <508E6FF7.1030505@davea.name> Message-ID: <508EA4EB.5020103@netcore.com.au> Thanks Mark. I 'stuck' with 2.7.2 for these old scripts, unless I want to totally rewrite them. They are so elegant, this would realy amount to starting from scratch. I get 15 reported, and that's going to be sufficient for these 'as they are'. For more, it's a go back to year zero exercise I feel. John. On 30/10/2012 2:15 AM, Mark Lawrence wrote: > It's 16 digits with 3.3.0. From john at netcore.com.au Mon Oct 29 16:51:10 2012 From: john at netcore.com.au (John Collins) Date: Tue, 30 Oct 2012 02:51:10 +1100 Subject: [Tutor] greater precision? In-Reply-To: References: <508E505C.9030209@netcore.com.au> <508E5906.5060101@pearwood.info> <508E6063.9090001@netcore.com.au> <508E69E1.3030308@davea.name> <508E6D43.4050208@netcore.com.au> <508E6FF7.1030505@davea.name> <508E7338.60300@netcore.com.au> Message-ID: <508EA5EE.4000502@netcore.com.au> Hi Mark, Thanks. I wouldn't know C if I fell over it. Until recently, the *only* language I'd ever used was (HP, GW)BASIC aside from Fortran 101, 3 decades ago! John. On 30/10/2012 2:45 AM, Mark Lawrence wrote: > If you're more comfortable with C you can use printf style formatting > see http://docs.python.org/2/library/stdtypes.html#string-formatting From breamoreboy at yahoo.co.uk Mon Oct 29 17:07:39 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 29 Oct 2012 16:07:39 +0000 Subject: [Tutor] greater precision? In-Reply-To: <508E6B0F.4080507@netcore.com.au> References: <508E513E.4000203@netcore.com.au> <508E584B.6040000@davea.name> <508E5B44.3090601@netcore.com.au> <508E62E1.2070007@davea.name> <508E6B0F.4080507@netcore.com.au> Message-ID: On 29/10/2012 11:39, John Collins wrote: > Hi Dave, >> BTW, I misspoke earlier. The module name is decimal. The class name >> within that module is Decimal. > A minor thing to me, a non programmer, but I do understand that being > very precise is very important to programmers, so thank you! > John. > If you had to change some code and use the Decimal class from the decimal module it would soon become a major issue as Python is case sensitive, even on Windows. Just hoping I've got my dS in the RiGhT cAsE :) -- Cheers. Mark Lawrence. From eryksun at gmail.com Mon Oct 29 17:20:15 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 29 Oct 2012 12:20:15 -0400 Subject: [Tutor] greater precision? In-Reply-To: References: <508E505C.9030209@netcore.com.au> <508E5906.5060101@pearwood.info> <508E6063.9090001@netcore.com.au> <508E69E1.3030308@davea.name> <508E6D43.4050208@netcore.com.au> <508E6FF7.1030505@davea.name> Message-ID: On Mon, Oct 29, 2012 at 11:15 AM, Mark Lawrence wrote: > On 29/10/2012 12:00, Dave Angel wrote: >> >> Not silly at all. I didn't realize str(float) would truncate to 12 >> digits either. I found out by experimenting (with 2.7) in the >> interpreter. > > It's 16 digits with 3.3.0. It was changed to use the repr in 3.2. See tp_repr/tp_str in PyFloat_Type: http://hg.python.org/cpython/file/a222a015e28d/Objects/floatobject.c#l1849 >From "What?s New In Python 3.2": http://docs.python.org/release/3.2/whatsnew/3.2#other-language-changes """ The str() of a float or complex number is now the same as its repr(). Previously, the str() form was shorter but that just caused confusion and is no longer needed now that the shortest possible repr() is displayed by default """ As repr() does, it chooses the minimum number of digits required to round exactly to the original double: repr with 1 digit: >>> pack('d', 0.1) '\x9a\x99\x99\x99\x99\x99\xb9?' >>> unpack('d', '\x9a\x99\x99\x99\x99\x99\xb9?')[0] 0.1 repr with 16 digits: >>> pack('d', 0.0999999999999995) 'v\x99\x99\x99\x99\x99\xb9?' >>> unpack('d', 'v\x99\x99\x99\x99\x99\xb9?')[0] 0.0999999999999995 repr with 17 digits: >>> pack('d', 0.10000000000000002) '\x9b\x99\x99\x99\x99\x99\xb9?' >>> unpack('d', '\x9b\x99\x99\x99\x99\x99\xb9?')[0] 0.10000000000000002 From ramit.prasad at jpmorgan.com Mon Oct 29 17:12:50 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Mon, 29 Oct 2012 16:12:50 +0000 Subject: [Tutor] Help Passing Variables In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF4741673AE2E@SCACMX008.exchad.jpmchase.net> David Hutto wrote: > #A little more complex in terms of params: > > def SwapCaseAndCenter(*kwargs): > > if upper_or_lower == "upper": > print a_string.center(center_num).upper() > > if upper_or_lower == "lower": > print a_string.center(center_num).lower() > > a_string = raw_input("Give me a word, or letter: ") > upper_or_lower = raw_input("upper, or lower character(s): ") > center_num = int(raw_input("Where should number be centered?: ")) > SwapCaseAndCenter(a_string, upper_or_lower, center_num) > > This function is using the global variables for each of its variables. To use the versions passed in you need to define the names from kwargs; the name "kwargs" is usually used in reference to a keyword arguments which should be denoted by double asterisk instead of a single one. In this case kwargs is confusingly a positional list of arguments (sometimes referred to as "args" instead). For a function called SwapCaseAndCenter, I see it doing no case "swapping" unless it refers to changing the entire string to a specific case? At the very least, no centering is being done. To format I use string formatting[0] which has its own syntax[1]. >>> '{0:^40}'.format( 'test' ) # The below line is the repr ' test ' >>> '{1:^{0}}'.format( 40, 'test' ) # Dynamically generate width ' test ' >>> '{0:^{1}}'.format( 'test', 40 ) ' test ' [0]: http://docs.python.org/2/library/string.html#formatstrings [1]: http://docs.python.org/2/library/string.html#formatspec Ramit Prasad 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 oscar.j.benjamin at gmail.com Mon Oct 29 20:26:09 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 29 Oct 2012 19:26:09 +0000 Subject: [Tutor] greater precision? In-Reply-To: <508E584B.6040000@davea.name> References: <508E513E.4000203@netcore.com.au> <508E584B.6040000@davea.name> Message-ID: On 29 October 2012 10:19, Dave Angel wrote: > On 10/29/2012 05:49 AM, John Collins wrote: >> >> Sorry to bother, but if I can squeeze out *at least* 15 sig figs, (30 >> or more would be better!) I'd be a happy camper! >> XNumbers addon for Excel allows over 200 sig figs by switching to base >> 256 IIRC. It is this with which I'd like to examine the output of these >> pyto scripts at finer resolution, if that can be done in python??? >> >> Best Regards, >> John Collins. >> >> > > 18 digits is what you should get if the code is as I describe. But if > there are lots of fp operations you're not showing, then an error can > gradually get larger. And with any finite precision, you have the risk > from things such as subtracting two nearly-equal values, which will > reduce the final precision. I wouldn't describe it as 18 digits of precision since it fails for computations that require only 17 digits of precision: >>> 1e-16 1e-16 >>> 1 + 1e-16 - 1 0.0 > > If you need more than 18, then go to the Decimal module, which lets you > set the precision to arbitrary levels. You will see a substantial > slowdown, of course, if you set the precision very high. if that > becomes a problem, consider CPython 3.3, which has optimized that > module. But try not to change too many things at once, as there are > lots of changes between 2.7 and 3.3. I don't really understand why so much precision is needed but if I were trying to improve it I would use the fractions module. The Fraction type from the fractions module has unlimited precision: >>> from fractions import Fraction >>> 1 + Fraction('1e-16') - 1 Fraction(1, 10000000000000000) >>> 1 + Fraction('1e-256') - 1 Fraction(1, 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) To use this you would do the same as for the decimal module: convert all of your numbers to Fraction type and pass the Fraction objects into the function that performs computation with them. Of course if you start out with floats and convert them to fractions then you will still only have 15 digits of precision so if you really want unlimited precision you need to convert the numbers to fractions at the point when their values are known exactly and not use floats anywhere. This means that your computations are exact but you still need to choose a precision to output the numbers in decimal (unless you're happy with fractions as output). If you want 256 digit decimal output you can do: >>> import decimal >>> decimal.getcontext().prec = 256 >>> f = 1 + Fraction('1e-255') >>> d = decimal.Decimal(f.numerator) / decimal.Decimal(f.denominator) >>> print(d) 1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 Oscar From richkappler at gmail.com Tue Oct 30 02:00:47 2012 From: richkappler at gmail.com (richard kappler) Date: Mon, 29 Oct 2012 21:00:47 -0400 Subject: [Tutor] calling a module fails Message-ID: Methinks I'm missing something obvious, but can't quite put my finger on it. If, in the interpreter, I enter the following code: def hungry(batVolt): if batVolt >94: return ("I am not hungry at the moment") elif 64 < batVolt < 95: return ("I'm starting to get hungry") else: return ("I'm hungry!") and then run hungry(98) with 98 just being an example of the many numbers I tried when testing this, I get the return I expected and all is well. If, however, I save the above in a file named hungry.py, then import hungry, I get an error, as follows: import hungry hungry(96) Traceback (most recent call last): File "", line 1, in TypeError: 'module' object is not callable So what am I missing? Someone do please point out the obvious. ;-) regards, Richard -- sic gorgiamus allos subjectatos nunc -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Tue Oct 30 02:24:26 2012 From: d at davea.name (Dave Angel) Date: Mon, 29 Oct 2012 21:24:26 -0400 Subject: [Tutor] calling a module fails In-Reply-To: References: Message-ID: <508F2C4A.2060108@davea.name> On 10/29/2012 09:00 PM, richard kappler wrote: > Methinks I'm missing something obvious, but can't quite put my finger on > it. If, in the interpreter, I enter the following code: > > def hungry(batVolt): > if batVolt >94: > return ("I am not hungry at the moment") > elif 64 < batVolt < 95: > return ("I'm starting to get hungry") > else: > return ("I'm hungry!") > > and then run > > hungry(98) > > with 98 just being an example of the many numbers I tried when testing > this, I get the return I expected and all is well. > > If, however, I save the above in a file named hungry.py, then import > hungry, I get an error, as follows: > > import hungry > hungry(96) > Traceback (most recent call last): > File "", line 1, in > TypeError: 'module' object is not callable > > So what am I missing? Someone do please point out the obvious. ;-) > > regards, Richard > > I'd recommend NOT ever calling the module by the same name as one of the functions or classes in it. When you want to call a function in an external module, you have a choice of: import mymodule mymodule.myfunction(98) or from mymodule import myfunction myfunction(98) By using the same name, it isn't obvious to you that what you need is: hungry.hungry(98) -- DaveA From steve at pearwood.info Tue Oct 30 02:28:52 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 30 Oct 2012 12:28:52 +1100 Subject: [Tutor] calling a module fails In-Reply-To: References: Message-ID: <508F2D54.3070308@pearwood.info> On 30/10/12 12:00, richard kappler wrote: [...] > If, however, I save the above in a file named hungry.py, then import > hungry, I get an error, as follows: > > import hungry > hungry(96) > Traceback (most recent call last): > File "", line 1, in > TypeError: 'module' object is not callable > > So what am I missing? Someone do please point out the obvious. ;-) You have a module called "hungry" and a function called "hungry" inside that, so to reach the inner "hungry" you need to do this: import hungry hungry.hungry(96) Which is no different from: import math math.cos(0.96) Python will never try to guess which inner function you want when you call a module directly, not even if the inner function has the same name as the module. You must always explicitly tell it which inner function you want. An alternative is this: from hungry import hungry # like "from math import cos" hungry(96) -- Steven From richkappler at gmail.com Tue Oct 30 17:56:09 2012 From: richkappler at gmail.com (richard kappler) Date: Tue, 30 Oct 2012 12:56:09 -0400 Subject: [Tutor] changing name value with function return Message-ID: If I have a variable and send it's value to a function to be modified and returned, how do I get the function return to replace the original value of the variable? Example: import random x = 50 def rndDelta(x): d = random.uniform(-10, 10) x = x + d return x When I call rndDelta, it functions as intended, I get a return that is x adjusted by some random value. This, however, I know does not actually change x. Let's say I call randDelta(x) and it returns 42.098734087, if I then type x, I still get 50. I want x to now be 42.098734087. regards, Richard -- sic gorgiamus allos subjectatos nunc -------------- next part -------------- An HTML attachment was scrubbed... URL: From timomlists at gmail.com Tue Oct 30 18:40:21 2012 From: timomlists at gmail.com (Timo) Date: Tue, 30 Oct 2012 18:40:21 +0100 Subject: [Tutor] changing name value with function return In-Reply-To: References: Message-ID: <50901105.9090604@gmail.com> Op 30-10-12 17:56, richard kappler schreef: > If I have a variable and send it's value to a function to be modified > and returned, how do I get the function return to replace the original > value of the variable? > > Example: > > import random > > x = 50 > > def rndDelta(x): > d = random.uniform(-10, 10) > x = x + d > return x > > When I call rndDelta, it functions as intended, I get a return that is > x adjusted by some random value. This, however, I know does not > actually change x. Let's say I call randDelta(x) and it returns > 42.098734087, if I then type x, I still get 50. I want x to now be > 42.098734087. > What about declaring the return value as x again? x = randDelta(x) Timo > regards, Richard > > > > -- > > sic gorgiamus allos subjectatos nunc > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From d at davea.name Tue Oct 30 19:26:04 2012 From: d at davea.name (Dave Angel) Date: Tue, 30 Oct 2012 14:26:04 -0400 Subject: [Tutor] changing name value with function return In-Reply-To: References: Message-ID: <50901BBC.7020006@davea.name> On 10/30/2012 12:56 PM, richard kappler wrote: > If I have a variable and send it's value to a function to be modified and > returned, how do I get the function return to replace the original value of > the variable? > > Example: > > import random > > x = 50 > > def rndDelta(x): > d = random.uniform(-10, 10) > x = x + d > return x > > When I call rndDelta, it functions as intended, I get a return that is x > adjusted by some random value. This, however, I know does not actually > change x. Let's say I call randDelta(x) and it returns 42.098734087, if I > then type x, I still get 50. I want x to now be 42.098734087. > > regards, Richard > > > There are two normal ways that a function may modify values in a calling function. One is to modify mutable arguments, and the other is to return a value. It's considered bad practice to do both in the same function. Anyway, since the int object is immutable, only the latter choice is available. Once you return a value, it's thrown away unless the caller does something with it. Quite common is to assign it somewhere. In your case, you want it to replace the original value of x, so: def test(): val = 50 val = rndDelta(val) When you use the same name x for both variables, you just confuse things. They are not the same thing at all, so I gave them different names. Once you're experienced enough to always know which ones correspond, then you might decide to reuse names. Notice that there's not really a variable val. There's a name val, which is bound to an int object =50 at first, then rebound to a different object, float object 42.09 whatever. We call var a variable, but the word has a different meaning than in most other languages. -- DaveA From richkappler at gmail.com Tue Oct 30 20:18:36 2012 From: richkappler at gmail.com (richard kappler) Date: Tue, 30 Oct 2012 15:18:36 -0400 Subject: [Tutor] running multiple concurrent processes Message-ID: As I sit through the aftermath of Sandy, I have resumed my personal quest to learn python. One of the things I am struggling with is running multiple processes. I read the docs on threading and am completely lost so am turning to the most excellent tutors here (and thanks for all the help, past, present and future btw!). In what ways can one run multiple concurrent processes and which would be considered the "best" way or is that case dependent? Example: I'm working on programming a robot in Python. The bot has an Arduino board that receives sensor input and sends the data to a laptop which is the "brain" of the bot via pySerial and uses this incoming sensor data to help determine state. State is used in decision making. The laptop runs a program that we'll call the Master Control Program in a nod to Tron. The bot also has a chat program, computer vision, some AI it uses to mine the web for information, several other functions. Each of these concurrent programs (thus far all python programs) must run continuously and feed data to the MCP which receives the data, makes decisions and sends out appropriate action commands such as for movement, change of state, conversation direction, what to research, etc. So, to return to the original question, how does one run multiple concurrent processes within python? regards, Richard -- sic gorgiamus allos subjectatos nunc -------------- next part -------------- An HTML attachment was scrubbed... URL: From oscar.j.benjamin at gmail.com Tue Oct 30 20:34:34 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 30 Oct 2012 19:34:34 +0000 Subject: [Tutor] running multiple concurrent processes In-Reply-To: References: Message-ID: On 30 October 2012 19:18, richard kappler wrote: > As I sit through the aftermath of Sandy, I have resumed my personal quest to > learn python. One of the things I am struggling with is running multiple > processes. I read the docs on threading and am completely lost so am turning > to the most excellent tutors here (and thanks for all the help, past, > present and future btw!). > > In what ways can one run multiple concurrent processes and which would be > considered the "best" way or is that case dependent? > > Example: > > I'm working on programming a robot in Python. The bot has an Arduino board > that receives sensor input and sends the data to a laptop which is the > "brain" of the bot via pySerial and uses this incoming sensor data to help > determine state. State is used in decision making. The laptop runs a program > that we'll call the Master Control Program in a nod to Tron. The bot also > has a chat program, computer vision, some AI it uses to mine the web for > information, several other functions. Each of these concurrent programs > (thus far all python programs) must run continuously and feed data to the > MCP which receives the data, makes decisions and sends out appropriate > action commands such as for movement, change of state, conversation > direction, what to research, etc. > > So, to return to the original question, how does one run multiple concurrent > processes within python? The obvious way would be to use the multiprocessing module: http://docs.python.org/2/library/multiprocessing.html Oscar From ramit.prasad at jpmorgan.com Tue Oct 30 21:17:25 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 30 Oct 2012 20:17:25 +0000 Subject: [Tutor] running multiple concurrent processes In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF4741674F09E@SBECMX007.exchad.jpmchase.net> richard kappler wrote: > As I sit through the aftermath of Sandy, I have resumed my personal quest to learn python. One of the things I > am struggling with is running multiple processes. I read the docs on threading and am completely lost so am > turning to the most excellent tutors here (and thanks for all the help, past, present and future btw!). > > In what ways can one run multiple concurrent processes and which would be considered the "best" way or is that > case dependent? > > Example: > > I'm working on programming a robot in Python. The bot has an Arduino board that receives sensor input and sends > the data to a laptop which is the "brain" of the bot via pySerial and uses this incoming sensor data to help > determine state. State is used in decision making. The laptop runs a program that we'll call the Master Control > Program in a nod to Tron. The bot also has a chat program, computer vision, some AI it uses to mine the web for > information, several other functions. Each of these ?concurrent programs (thus far all python programs) must run > continuously and feed data to the MCP which receives the data, makes decisions and sends out appropriate action > commands such as for movement, change of state, conversation direction, what to research, etc. > > So, to return to the original question, how does one run multiple concurrent processes within python? > > regards, Richard The typical ways to do parallel execution is to use the multiprocessing module or use some type of threading (threading module). I am not really sure what your bot does, given that it seems to include an interface to some chat program, does it really even need to do things in the background? Most chat bots can just listen and then take action only when necessary. 1. Look for chat line to take action. 2. Get data from sources (Arduino, web, etc) to decide State/action. 3. Do action. 4. Repeat 1-3. Your program does sound fairly complicated and it may very well need to run everything in parallel. If so, you could use threading with callbacks that would modify the current "state" as needed. You could also setup various programs that update a database (SQLlite) and then the MCP queries the db to decide on the action. An upside is that it is easier to (re-)start/test any single component while a downside is that monitoring needs to occur for each component. I would seriously evaluate whether you need true parallel processing or just something that is "fast enough" to seem like it. The more I think about it, the more I like the use of a SQLlite db. Ramit Prasad 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 Tue Oct 30 21:43:54 2012 From: d at davea.name (Dave Angel) Date: Tue, 30 Oct 2012 16:43:54 -0400 Subject: [Tutor] running multiple concurrent processes In-Reply-To: References: Message-ID: <50903C0A.3060605@davea.name> On 10/30/2012 03:18 PM, richard kappler wrote: > As I sit through the aftermath of Sandy, I have resumed my personal quest > to learn python. One of the things I am struggling with is running multiple > processes. I read the docs on threading and am completely lost so am > turning to the most excellent tutors here (and thanks for all the help, > past, present and future btw!). > > In what ways can one run multiple concurrent processes and which would be > considered the "best" way or is that case dependent? > > Example: > > I'm working on programming a robot in Python. The bot has an Arduino board > that receives sensor input and sends the data to a laptop which is the > "brain" of the bot via pySerial and uses this incoming sensor data to help > determine state. State is used in decision making. The laptop runs a > program that we'll call the Master Control Program in a nod to Tron. The > bot also has a chat program, computer vision, some AI it uses to mine the > web for information, several other functions. Each of these concurrent > programs (thus far all python programs) must run continuously and feed data > to the MCP which receives the data, makes decisions and sends out > appropriate action commands such as for movement, change of state, > conversation direction, what to research, etc. > > So, to return to the original question, how does one run multiple > concurrent processes within python? > > I'm only guessing about your background, so please don't take offense at the simple level of the following. You see, before you can really understand how the language features work, and what the various terms mean, you need to understand the processor and the OS. A decade or so ago, things were a bit simpler -- if we wanted a faster machine, Intel would crank up the processor clock rate, and things were faster. But eventually, it reached the point where increased clock rate became VERY expensive, and Intel (and others) came up with a different strategy. I'm going to guess you're running on some variant of the Pentium processor. The processor (cpu) has a feature called hyperthreading, meaning that for most operations, it can do two things at once. So it has two copies of the instruction pointer, and two copies of most registers. As long as neither program uses the features that aren't replicated, you can run two programs completely independently. The two programs share physical memory, hard disk, keyboard and screen, but they probably won't slow each other down very much. You may have a dual-core, or even a quad-core processor. And you may have more than one of those, if you're on a high-end server. So, as long as the processes are separate, you could run many of them at a time. The other thing that affects all of this is the operating system you're running. It has to manage these multiple processes, and make sure that things that can't be shared are correctly serialized; one task grabs a resource and others block waiting for that resource. The most visible (but not the most important) way this occurs is that separate applications draw in different windows. They share the screen, but none of them writes to the raw device, all of them go through a window manager. This is multiprocessing. And since one program can launch others, it's one way that a single "task" can be split up to use these multiple cores/cpus. The operating system deliberately keeps the separate processes very isolated, but provides a few ways for them to talk to each other: (one program can launch another, passing it arguments, and observing the return code, it can also use pipes to connect to stdin and stdout of the other program, they can open up queues, shared memory, or they can each read & write to a common file.) Such processes do NOT normally share variables, and function calls on one do not easily end up invoking code in the other. But there is a second way that two cpus can work on the same "task." If a single process is multi-THREADED, then the threads do share variables and other resources, and communication between them is easy (so easy it's difficult to get right, actually). This is theoretically much gentler on system resources, but at the cost of lots more bugs likely. Some operating systems have a feature called forking, which can theoretically give you the best of both worlds. But I'm not going to even try to explain that unless you tell me you're on a Linux or Unix type operating system. Besides, I don't know how any of Python uses such a fork; it hasn't turned out to be necessary information for me yet. Now, with CPython in particular, multithreading has a serious problem, the global lock (GIL). Since so much happens behind the scenes inside the interpreter and low-level library routines, and perhaps since most of that was written before multithreading was supported, there's a single lock that permits only one thread of a process to be working at a time. So if you break up a CPU-bound task into multiple threads, only one will run at a time, and chances are it'll run slower than if it only had one thread. Two things happen to make the GIL less painful (it's really just two manifestations of the same thing). Many times when a thread is in C code, or when it is calling some system function that blocks (eg. waiting for a network message), the GIL is deliberately released, and other threads CAN run. So writing a server that waits on many sockets, one per thread, can make good sense, both from code simplicity and from performance considerations. One other thing that's related. Most gui programs run with an event loop, which is another type of multithreading that does NOT use any special cpu or OS features. With an event loop, it's your job to make sure all transactions are reasonably small, and that each is triggered by some event. Once you understand event loops, it's simpler than either of the other approaches. Note that sometimes two or three of these approaches are combined in one system. Hope this helps, and that some of it was useful. I know that in places I oversimplified, but I think I caught the spirit of the tradeoffs. -- DaveA From richkappler at gmail.com Tue Oct 30 22:10:17 2012 From: richkappler at gmail.com (richard kappler) Date: Tue, 30 Oct 2012 17:10:17 -0400 Subject: [Tutor] running multiple concurrent processes In-Reply-To: <50903C0A.3060605@davea.name> References: <50903C0A.3060605@davea.name> Message-ID: Oscar, thanks for the link, though I must say with all due respect, if it was "obvious" I wouldn't have had to ask the question. Good link though. I suspect the reason I didn't find it is I did my searches under threading as opposed to multi-processing. Dave, no offense taken, great write-up. Now I understand more, but am quite possible even more confused, but will take what you wrote to guide me through further research. This is enough to get me started though. I'm thinking multi-threading might be the way to go, but I want to re-read the docs on multi-processing. Our internet went out for a while so I was reading that on my phone, want to read again on a big screen. For the record, I'm running Ubuntu Linux 12.04 (not quite a noob anymore, been on Ubuntu since Maverick, pretty handy with the shell) on an HP G62 which has a 2.5GHz dual core Turion II (upgraded to 8 gig memory if that matters, I'm thinking it doesn't). I'm also going to investigate running it as a monolithic program instead of several smaller ones running concurrently as suggested by another poster. Frankly I don't think that would work as well, and it would involve a great many loops that might erroneously end up nigh-continuous, so I see danger there, but will investigate. I was at one point actually thinking of setting things up on Timer from the threading module, so that parameters would update either event driven (ex: a sensor crossing a threshold) or time driven (ex: poll sensors every 30 seconds). That seems doable, but poor form to me. Rather I should say that seems a low level way out that is attractive only due to my fairly low level of Python knowledge at this point. Either threading or multi-processing seems both more elegant and more efficient. Might you explain a little more in depth on the pitfalls of threading, and also the event loop you mentioned, or point me towards some resources? As I mentioned earlier, I did read the threading docs, was pretty lost. I'm no idiot, but I'm a public school math teacher trying learn this stuff to help engage my students, not a degreed computer science major or full time programmer. As always I appreciate the responses and rich variety of help therein. regards, Richard I'm only guessing about your background, so please don't take offense at > the simple level of the following. You see, before you can really > understand how the language features work, and what the various terms > mean, you need to understand the processor and the OS. > > A decade or so ago, things were a bit simpler -- if we wanted a faster > machine, Intel would crank up the processor clock rate, and things were > faster. But eventually, it reached the point where increased clock rate > became VERY expensive, and Intel (and others) came up with a different > strategy. > > I'm going to guess you're running on some variant of the Pentium > processor. The processor (cpu) has a feature called hyperthreading, > meaning that for most operations, it can do two things at once. So it > has two copies of the instruction pointer, and two copies of most > registers. As long as neither program uses the features that aren't > replicated, you can run two programs completely independently. The two > programs share physical memory, hard disk, keyboard and screen, but they > probably won't slow each other down very much. > > You may have a dual-core, or even a quad-core processor. And you may > have more than one of those, if you're on a high-end server. So, as > long as the processes are separate, you could run many of them at a time. > > The other thing that affects all of this is the operating system you're > running. It has to manage these multiple processes, and make sure that > things that can't be shared are correctly serialized; one task grabs a > resource and others block waiting for that resource. The most visible > (but not the most important) way this occurs is that separate > applications draw in different windows. They share the screen, but none > of them writes to the raw device, all of them go through a window manager. > > This is multiprocessing. And since one program can launch others, it's > one way that a single "task" can be split up to use these multiple > cores/cpus. The operating system deliberately keeps the separate > processes very isolated, but provides a few ways for them to talk to > each other: (one program can launch another, passing it arguments, and > observing the return code, it can also use pipes to connect to stdin and > stdout of the other program, they can open up queues, shared memory, or > they can each read & write to a common file.) Such processes do NOT > normally share variables, and function calls on one do not easily end up > invoking code in the other. > > But there is a second way that two cpus can work on the same "task." If > a single process is multi-THREADED, then the threads do share variables > and other resources, and communication between them is easy (so easy > it's difficult to get right, actually). This is theoretically much > gentler on system resources, but at the cost of lots more bugs likely. > > Some operating systems have a feature called forking, which can > theoretically give you the best of both worlds. But I'm not going to > even try to explain that unless you tell me you're on a Linux or Unix > type operating system. Besides, I don't know how any of Python uses > such a fork; it hasn't turned out to be necessary information for me yet. > > Now, with CPython in particular, multithreading has a serious problem, > the global lock (GIL). Since so much happens behind the scenes inside > the interpreter and low-level library routines, and perhaps since most > of that was written before multithreading was supported, there's a > single lock that permits only one thread of a process to be working at a > time. So if you break up a CPU-bound task into multiple threads, only > one will run at a time, and chances are it'll run slower than if it only > had one thread. > > Two things happen to make the GIL less painful (it's really just two > manifestations of the same thing). Many times when a thread is in C > code, or when it is calling some system function that blocks (eg. > waiting for a network message), the GIL is deliberately released, and > other threads CAN run. So writing a server that waits on many sockets, > one per thread, can make good sense, both from code simplicity and from > performance considerations. > > One other thing that's related. Most gui programs run with an event > loop, which is another type of multithreading that does NOT use any > special cpu or OS features. With an event loop, it's your job to make > sure all transactions are reasonably small, and that each is triggered > by some event. Once you understand event loops, it's simpler than > either of the other approaches. Note that sometimes two or three of > these approaches are combined in one system. > > Hope this helps, and that some of it was useful. I know that in places > I oversimplified, but I think I caught the spirit of the tradeoffs. > > > -- > > DaveA > > -- sic gorgiamus allos subjectatos nunc -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Tue Oct 30 22:28:03 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 31 Oct 2012 08:28:03 +1100 Subject: [Tutor] changing name value with function return In-Reply-To: References: Message-ID: <50904663.6000409@pearwood.info> On 31/10/12 03:56, richard kappler wrote: > If I have a variable and send it's value to a function to be modified and > returned, how do I get the function return to replace the original value of > the variable? spam = function(spam) passes the value of "spam" to the function, then assigns the result back to "spam", replacing whatever value it previously had. -- Steven From oscar.j.benjamin at gmail.com Tue Oct 30 23:08:13 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 30 Oct 2012 22:08:13 +0000 Subject: [Tutor] running multiple concurrent processes In-Reply-To: References: <50903C0A.3060605@davea.name> Message-ID: On 30 October 2012 21:10, richard kappler wrote: > Oscar, thanks for the link, though I must say with all due respect, if it > was "obvious" I wouldn't have had to ask the question. Good link though. I > suspect the reason I didn't find it is I did my searches under threading as > opposed to multi-processing. Sorry Richard, I think you may have misunderstood. I didn't mean "this is obvious so why ask the question?". Rather I meant "there are a number of ways to do this but to me the obvious choice is multiprocessing". I probably should have added more explanation but I realised I needed to get back on with cooking dinner and thought I'd just send a quick pointer. Of course when I say it's the obvious choice that assumes that you are sure that concurrent processes is what you want. You can achieve similar things in a number of ways as Dave has described. If you're not sure which of threads and processes you want then I'll say that normally in Python threads are used to run IO-bound operations concurrently. Python's threads are no good for CPU-bound operations so multiprocessing is used in that case. Do you know which case your application falls under? Oscar From D.Wilder at F5.com Wed Oct 31 02:47:06 2012 From: D.Wilder at F5.com (Dave Wilder) Date: Wed, 31 Oct 2012 01:47:06 +0000 Subject: [Tutor] Obtaining result from a sendline Message-ID: Okay, file this under RTFM, but even after research, I cannot figure out the answer to this simple question. When I do an ssh in python (version 2.7.3) to a device, I am trying to capture the output of the command. However, all I get back is a numerical value. I am looking to get the actual output from the "ls /var/tmp" command below. This is probably readily available info, but I am just not searching on the python sites correctly. e.g. (Pdb) ssh = sshClient.pxssh() (Pdb) ssh.login(server=ip_addr, username=user, password=pswd) True (Pdb) str = ssh.sendline('ls /var/tmp') (Pdb) str 12 (Pdb) -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Wed Oct 31 03:02:07 2012 From: d at davea.name (Dave Angel) Date: Tue, 30 Oct 2012 22:02:07 -0400 Subject: [Tutor] running multiple concurrent processes In-Reply-To: References: <50903C0A.3060605@davea.name> Message-ID: <5090869F.3010109@davea.name> On 10/30/2012 05:10 PM, richard kappler wrote: > Oscar, thanks for the link, though I must say with all due respect, if it > was "obvious" I wouldn't have had to ask the question. Good link though. I > suspect the reason I didn't find it is I did my searches under threading as > opposed to multi-processing. > > Dave, no offense taken, great write-up. Now I understand more, but am quite > possible even more confused, but will take what you wrote to guide me > through further research. This is enough to get me started though. I'm > thinking multi-threading might be the way to go, but I want to re-read the > docs on multi-processing. Our internet went out for a while so I was > reading that on my phone, want to read again on a big screen. > > For the record, I'm running Ubuntu Linux 12.04 (not quite a noob anymore, > been on Ubuntu since Maverick, pretty handy with the shell) on an HP G62 > which has a 2.5GHz dual core Turion II (upgraded to 8 gig memory if that > matters, I'm thinking it doesn't). > > I'm also going to investigate running it as a monolithic program instead of > several smaller ones running concurrently as suggested by another poster. > Frankly I don't think that would work as well, and it would involve a great > many loops that might erroneously end up nigh-continuous, so I see danger > there, but will investigate. > > I was at one point actually thinking of setting things up on Timer from the > threading module, so that parameters would update either event driven (ex: > a sensor crossing a threshold) or time driven (ex: poll sensors every 30 > seconds). That seems doable, but poor form to me. Rather I should say that > seems a low level way out that is attractive only due to my fairly low > level of Python knowledge at this point. Either threading or > multi-processing seems both more elegant and more efficient. > I have no experience with PySerial, but if you can block waiting for the next "message," then that's a simple place to make a separate thread. And it beats using 30 second timers. A robot can move quite a ways in 30 seconds, before you realize that 20 seconds ago it was headed off the edge of something. > Might you explain a little more in depth on the pitfalls of threading, and > also the event loop you mentioned, or point me towards some resources? As I > mentioned earlier, I did read the threading docs, was pretty lost. I'm no > idiot, but I'm a public school math teacher trying learn this stuff to help > engage my students, not a degreed computer science major or full time > programmer. > The real problem with threading is simply that any mutable static variable should not be shared, except maybe rarely when there's at most one thread that updates it, and the others just read it. Those can still be a problem if they're containers, as one thread could be iterating over it while another changes it. In Python they're not called static, but the ones I mean are typically top-level (or module) variables, and class attributes. When the thread starts, you have a thread-object, and you can safely read and write attributes stored there, because it's pretty easy to know that they're unique to your own thread. Similarly, it's safe to use local variables within a function, and of course instance attributes of a class, providing the instance is not shared. Anyway, the trick is that when threads DO need to share modified data, you want to use a library you can trust, such as Queue. If you use other mechanisms, they need to be either VERY simple, or designed by someone experienced. The pitfall for multithreading is that sometimes you use a shared variable without even realizing it. -- DaveA From eryksun at gmail.com Wed Oct 31 06:44:01 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 31 Oct 2012 01:44:01 -0400 Subject: [Tutor] Obtaining result from a sendline In-Reply-To: References: Message-ID: On Tue, Oct 30, 2012 at 9:47 PM, Dave Wilder wrote: > > However, all I get back is a numerical value. I am looking to get the > actual output from the ?ls /var/tmp? That would be the number of bytes sent. > ssh = sshClient.pxssh() > ssh.login(server=ip_addr, username=user, password=pswd) > str = ssh.sendline('ls /var/tmp') It's not a good idea to shadow the name of the built-in type str. The pxssh object should have the methods read, read_nonblocking, readline, and readlines. But you probably want the output from between prompts. The prompt() method calls expect() to match the custom prompt, and returns False if there's a timeout (the default is 20 seconds). This sets the instance attribute "before" with the text that came before the match: ssh.sendline('ls /var/tmp') if ssh.prompt(): output = ssh.before Alternatively, if you've setup a public key on the host via ssh-keygen, you could use subprocess.check_output() instead: import subprocess cmd = ['ssh', '-l', login, hostname, 'ls /var/tmp'] output = subprocess.check_output(cmd) From steve at pearwood.info Wed Oct 31 10:35:59 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 31 Oct 2012 20:35:59 +1100 Subject: [Tutor] Obtaining result from a sendline In-Reply-To: References: Message-ID: <5090F0FF.8030106@pearwood.info> On 31/10/12 12:47, Dave Wilder wrote: > When I do an ssh in python (version 2.7.3) to a device, I am trying >to capture the output of the command. > > However, all I get back is a numerical value. I am looking to get > the actual output from the "ls /var/tmp" command below. This is >probably readily available info, but I am just not searching on the > python sites correctly. > > > > e.g. > > (Pdb) ssh = sshClient.pxssh() > (Pdb) ssh.login(server=ip_addr, username=user, password=pswd) > True > (Pdb) str = ssh.sendline('ls /var/tmp') > (Pdb) str > 12 > (Pdb) I'm rather surprised that you are using the Python debugger to experiment with this. I don't think I've seen anyone else do that before. Normally people just use the interactive shell. What's sshClient and where did it come from? When I try to import it, I get this error: py> import sshClient Traceback (most recent call last): File "", line 1, in ImportError: No module named 'sshClient' Since you haven't told us what sshClient is, and it's not a standard part of Python, that makes it a *little* difficult to tell you what the answer to your problem is. -- Steven From D.Wilder at F5.com Wed Oct 31 14:28:36 2012 From: D.Wilder at F5.com (Dave Wilder) Date: Wed, 31 Oct 2012 13:28:36 +0000 Subject: [Tutor] Obtaining result from a sendline In-Reply-To: References: Message-ID: Sorry for the poor info on my part Steven et al. I did not do my homework. I thought that sshclient was an imported module, however it is pxssh that I am importing as sshClient. import pxssh as sshClient There is no particular reason I need to debug this using the debugger to troubleshoot this. I was just using it to step through the program at the time. Thank you eryksun for the help?especially the quick response and great explanation. Both of your suggestions will work for me, particularly the 2nd one. I am not actually using ?str? for the reasons you described. I just renamed it when I emailed my question, which was probably not a good idea. Thanks, Dave -----Original Message----- From: eryksun [mailto:eryksun at gmail.com] Sent: Wednesday, October 31, 2012 1:44 AM To: Dave Wilder Cc: tutor at python.org Subject: Re: [Tutor] Obtaining result from a sendline On Tue, Oct 30, 2012 at 9:47 PM, Dave Wilder > wrote: > > However, all I get back is a numerical value. I am looking to get the > actual output from the ?ls /var/tmp? That would be the number of bytes sent. > ssh = sshClient.pxssh() > ssh.login(server=ip_addr, username=user, password=pswd) str = > ssh.sendline('ls /var/tmp') It's not a good idea to shadow the name of the built-in type str. The pxssh object should have the methods read, read_nonblocking, readline, and readlines. But you probably want the output from between prompts. The prompt() method calls expect() to match the custom prompt, and returns False if there's a timeout (the default is 20 seconds). This sets the instance attribute "before" with the text that came before the match: ssh.sendline('ls /var/tmp') if ssh.prompt(): output = ssh.before Alternatively, if you've setup a public key on the host via ssh-keygen, you could use subprocess.check_output() instead: import subprocess cmd = ['ssh', '-l', login, hostname, 'ls /var/tmp'] output = subprocess.check_output(cmd) -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Wed Oct 31 14:47:43 2012 From: d at davea.name (Dave Angel) Date: Wed, 31 Oct 2012 09:47:43 -0400 Subject: [Tutor] Obtaining result from a sendline In-Reply-To: References: Message-ID: <50912BFF.4030706@davea.name> On 10/31/2012 09:28 AM, Dave Wilder wrote: > Sorry for the poor info on my part Steven et al. I did not do my homework. > > > > I thought that sshclient was an imported module, however it is pxssh that I am importing as sshClient. > > import pxssh as sshClient > > Please don't top-post. You put your message in front of whatever you're quoting, as though you had written it earlier. it belongs after the quoted part, which should be trimmed of irrelevant parts. And of course anything after your new message is by definition irrelevant. pxssh is not a Python standard library either. Perhaps you got it from: http://pexpect.sourceforge.net/pxssh.html which in turn depends on some other pexpect module. But there are apparently other places with similarly named stuff. You really should describe your environment when asking questions. OS, python version, external libraries, and any special conditions you know of. -- DaveA From ramit.prasad at jpmorgan.com Wed Oct 31 16:25:15 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Wed, 31 Oct 2012 15:25:15 +0000 Subject: [Tutor] Obtaining result from a sendline In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF47416764710@SCACMX008.exchad.jpmchase.net> Dave Wilder wrote: > Okay, file this under RTFM, but even after research, I cannot figure out the answer to this simple question. > > When I do an ssh in python (version 2.7.3) to a device, I am trying to capture the output of the command. > However, all I get back is a numerical value.? I am looking to get the actual output from the "ls /var/tmp" > command below.? This is probably readily available info, but I am just not searching on the python sites > correctly. > > e.g. > (Pdb) ssh = sshClient.pxssh() > (Pdb) ssh.login(server=ip_addr, username=user, password=pswd) > True > (Pdb) str = ssh.sendline('ls /var/tmp') > (Pdb) str > 12 > (Pdb) Python does not come with an SSH library, which one are you using? I am guessing you are using pxssh but it is better for you to explicitly let us know any third party module and the operating system. The example they provide on the site[0] says you need to use the following to get the output. """" s.sendline ('uptime') # run a command s.prompt() # match the prompt print s.before # print everything before the prompt. """" [0] http://pexpect.sourceforge.net/pxssh.html Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From yahufpontius at yahoo.com Sat Oct 27 19:26:32 2012 From: yahufpontius at yahoo.com (Frank Pontius) Date: Sat, 27 Oct 2012 10:26:32 -0700 Subject: [Tutor] Help with class example In-Reply-To: <508B4CEE.3000609@davea.name> References: <001d01cdad8c$8f83d220$ae8b7660$@com> <508B4CEE.3000609@davea.name> Message-ID: <001501cdb468$38020f70$a8062e50$@com> Here ya go! Can't do what you want as this is a programmatic error from interrupter. Only a screen shot will tell you the full situation. I have class example which is to be used for our homework, and I copied it into IDLE and it won't work, with error below. I've tried several ways to work around this Thanks Frank -----Original Message----- From: Dave Angel [mailto:d at davea.name] Sent: Friday, October 26, 2012 7:55 PM To: Frank Pontius Cc: tutor at python.org Subject: Re: [Tutor] Help with class example On 10/18/2012 07:59 PM, Frank Pontius wrote: > Hello, > I'm taking a beginners course on Python. > > Have class example which is to be used in H/W, which I can't get to work. > Thus I figure the example is wrong. But I need this to get H/W done. > > Using 'import random' and random.randrange in function, returning #. > > import random > > def RandomNumberGen (): > if random.randrange(0, 2) == 0: > X return "tails" > return "heads" > > > Simple enough, but running in my Python 2.7.3 IDLE - it will not even > run, and give syntax (red char box) error at the X above. > > Frank > > So why is there an X there? As the compiler tells you, that's a syntax error. The return statement starts with an 'r' Please use only text editors to edit code, and use copy/paste to copy the actual source to your message, while composing the message in text mode. We cannot tell who has mangled the code, but only how it happens to look after the mangling is finished. If it ever goes through a word processor, or through an html message (which your is), then all bets are off. Another thing. Copy/paste the actual error message, including the traceback. Don't summarize it, or paraphrase it. -- DaveA -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image003.jpg Type: image/jpeg Size: 38506 bytes Desc: not available URL: From yahufpontius at yahoo.com Sat Oct 27 22:51:07 2012 From: yahufpontius at yahoo.com (Frank Pontius) Date: Sat, 27 Oct 2012 13:51:07 -0700 Subject: [Tutor] Help with class example Message-ID: But, this is an if. From my Android phone on T-Mobile. The first nationwide 4G network. bob gailer wrote: On 10/27/2012 1:26 PM, Frank Pontius wrote: Here ya go! Can?t do what you want as this is a programmatic error from interrupter.? Only a screen shot will tell you the full situation.? I have class example which is to be used for our homework, and I copied it into IDLE and it won?t work, with error below.? I?ve tried several ways to work around this ? Thanks Frank ? ? ? ? ? ? ? ? ? ? -----Original Message----- From: Dave Angel [mailto:d at davea.name] Sent: Friday, October 26, 2012 7:55 PM To: Frank Pontius Cc: tutor at python.org Subject: Re: [Tutor] Help with class example ? On 10/18/2012 07:59 PM, Frank Pontius wrote: > Hello, > I'm taking a beginners course on Python. >? > Have class example which is to be used in H/W, which I can't get to work. > Thus I figure the example is wrong.? But I need this to get H/W done. >? > Using 'import random' and random.randrange in function, returning #. >? > import random >? > def RandomNumberGen (): > ??? if random.randrange(0, 2) == 0: get rid of the colon. That is used only at the start a compound statement e;g; if, else, for, while, def class, ... >????????????? X? return "tails" > ??? return "heads" >? >? > Simple enough, but running in my Python 2.7.3 IDLE - it will not even > run, and give syntax (red char box) error at the X above. >? > Frank >? >? ? So why is there an X there?? As the compiler tells you, that's a syntax error. The return statement starts with an 'r' ? Please use only text editors to edit code, and use copy/paste to copy the actual source to your message, while composing the message in text mode.? We cannot tell who has mangled the code, but only how it happens to look after the mangling is finished.? If it ever goes through a word processor, or through an html message (which your is), then all bets are off. ? Another thing.? Copy/paste the actual error message, including the traceback.? Don't summarize it, or paraphrase it. An IDE is an exception to that request since it displays certain exceptions in a different way. ? -- Bob Gailer 919-636-4239 Chapel Hill NC -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: RAW Type: image/jpeg Size: 38506 bytes Desc: not available URL: From yahufpontius at yahoo.com Tue Oct 30 02:36:02 2012 From: yahufpontius at yahoo.com (Frank Pontius) Date: Mon, 29 Oct 2012 18:36:02 -0700 Subject: [Tutor] why different result from two similar ways Message-ID: <000401cdb63e$ec068410$c4138c30$@com> Hello, I have code that works. Then tried to move some of it into function IncrementAndRebuildInput, then result changes, I no longer have same result as when code in function was inline - why? (Function version way below) Inline version: (this correctly adds 1 to all numbers in text string, and prints it out with incremented #s): def IsNum(string): # print "IsNum string", string for char in string: #checks string groupings to be all nums if not char.isdigit(): # print "false" return False # print "true" return True def main(): text = raw_input("Type something: ") print if text: print text else: text = "I got 432 when I counted, but Jim got 433 which is a lot for only 6 cats, or were there 12 cats?" print text #string input SplitText = text.split() #makes a list from string input # print SplitText # print "Did I print LIST?" for index, element in enumerate(SplitText): if IsNum(element): #looks at every list element, checks for # num = int(element) + 1 #if #, increments it # print num # print "Bkpt8" SplitText[index] = str(num) else: pass # print "bkpt9" # NewString = " ".join(SplitText) print "bkpt10" print print SplitText print print " ".join(SplitText) print print "END" main() OUTPUT::: >>> >>> Type something: I got 432 when I counted, but Jim got 433 which is a lot for only 6 cats, or were there 12 cats? bkpt10 ['I', 'got', '433', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '434', 'which', 'is', 'a', 'lot', 'for', 'only', '7', 'cats,', 'or', 'were', 'there', '13', 'cats?'] I got 433 when I counted, but Jim got 434 which is a lot for only 7 cats, or were there 13 cats? END >>> Function version way below: (This version does not produce the same output (w/numbers incremented by 1) def IsNum(string): # print "IsNum string", string for char in string: #checks string groupings to be all nums if not char.isdigit(): # print "false" return False # print "true" return True def IncrementAndRebuildInput(text): newtext = text.split() #makes a list from string input print newtext # print "Did I print LIST?" for index, element in enumerate(newtext): if IsNum(element): #looks at every list element, checks for # num = int(element) + 1 #if #, increments it print num # print "Bkpt8" newtext[index] = str(num) print newtext print "NOWHERE" else: pass # print "bkpt9" print newtext # contains new list w/#'s incremented by 1 print "Point6" return newtext def main(): text = raw_input("Type something: ") print if text: print text else: text = "I got 432 when I counted, but Jim got 433 which is a lot for only 6 cats, or were there 12 cats?" print text #string input IncrementAndRebuildInput(text) # print "bkpt10" print print text # ********** Placing previous inline code into function changes result - what am I doing wrong? ********** print "Point7" print "".join(text) print print "END" main() OUTPUT::: >>> >>> Type something: I got 432 when I counted, but Jim got 433 which is a lot for only 6 cats, or were there 12 cats? ['I', 'got', '432', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '433', 'which', 'is', 'a', 'lot', 'for', 'only', '6', 'cats,', 'or', 'were', 'there', '12', 'cats?'] 433 ['I', 'got', '433', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '433', 'which', 'is', 'a', 'lot', 'for', 'only', '6', 'cats,', 'or', 'were', 'there', '12', 'cats?'] NOWHERE 434 ['I', 'got', '433', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '434', 'which', 'is', 'a', 'lot', 'for', 'only', '6', 'cats,', 'or', 'were', 'there', '12', 'cats?'] NOWHERE 7 ['I', 'got', '433', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '434', 'which', 'is', 'a', 'lot', 'for', 'only', '7', 'cats,', 'or', 'were', 'there', '12', 'cats?'] NOWHERE 13 ['I', 'got', '433', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '434', 'which', 'is', 'a', 'lot', 'for', 'only', '7', 'cats,', 'or', 'were', 'there', '13', 'cats?'] NOWHERE ['I', 'got', '433', 'when', 'I', 'counted,', 'but', 'Jim', 'got', '434', 'which', 'is', 'a', 'lot', 'for', 'only', '7', 'cats,', 'or', 'were', 'there', '13', 'cats?'] Point6 I got 432 when I counted, but Jim got 433 which is a lot for only 6 cats, or were there 12 cats? Point7 I got 432 when I counted, but Jim got 433 which is a lot for only 6 cats, or were there 12 cats? END >>> >>> -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Sat Oct 27 21:06:46 2012 From: bgailer at gmail.com (bob gailer) Date: Sat, 27 Oct 2012 15:06:46 -0400 Subject: [Tutor] Help with class example In-Reply-To: <001501cdb468$38020f70$a8062e50$@com> References: <001d01cdad8c$8f83d220$ae8b7660$@com> <508B4CEE.3000609@davea.name> <001501cdb468$38020f70$a8062e50$@com> Message-ID: <508C30C6.90908@gmail.com> On 10/27/2012 1:26 PM, Frank Pontius wrote: > > Here ya go! > > Can't do what you want as this is a programmatic error from > interrupter. Only a screen shot will tell you the full situation. I > have class example which is to be used for our homework, and I copied > it into IDLE and it won't work, with error below. I've tried several > ways to work around this > > Thanks > > Frank > > -----Original Message----- > From: Dave Angel [mailto:d at davea.name] > Sent: Friday, October 26, 2012 7:55 PM > To: Frank Pontius > Cc: tutor at python.org > Subject: Re: [Tutor] Help with class example > > On 10/18/2012 07:59 PM, Frank Pontius wrote: > > > Hello, > > > I'm taking a beginners course on Python. > > > > > > Have class example which is to be used in H/W, which I can't get to > work. > > > Thus I figure the example is wrong. But I need this to get H/W done. > > > > > > Using 'import random' and random.randrange in function, returning #. > > > > > > import random > > > > > > def RandomNumberGen (): > > > if random.randrange(0, 2) == 0: > get rid of the colon. That is used only at the start a compound statement e;g; if, else, for, while, def class, ... > > > X return "tails" > > > return "heads" > > > > > > > > > Simple enough, but running in my Python 2.7.3 IDLE - it will not even > > > run, and give syntax (red char box) error at the X above. > > > > > > Frank > > > > > > > > So why is there an X there? As the compiler tells you, that's a > syntax error. The return statement starts with an 'r' > > Please use only text editors to edit code, and use copy/paste to copy > the actual source to your message, while composing the message in text > mode. We cannot tell who has mangled the code, but only how it > happens to look after the mangling is finished. If it ever goes > through a word processor, or through an html message (which your is), > then all bets are off. > > Another thing. Copy/paste the actual error message, including the > traceback. Don't summarize it, or paraphrase it. > An IDE is an exception to that request since it displays certain exceptions in a different way. > > -- Bob Gailer 919-636-4239 Chapel Hill NC -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/jpeg Size: 38506 bytes Desc: not available URL: From braybebrave at gmail.com Wed Oct 31 02:01:34 2012 From: braybebrave at gmail.com (Brayden Zhao) Date: Tue, 30 Oct 2012 20:01:34 -0500 Subject: [Tutor] help on dic creation Message-ID: <0B31024E-E437-461D-BC26-AAE93E773888@gmail.com> hello! I am doing my homework now and I am kinda stuck. Could any of you help me out? Here is the homework problem: fieldict(filename) reads a file in DOT format and returns a dictionary with the DOT CMPLID, converted to an integer, as the key, and a tuple as the corresponding value for that key. The format of the tuple is: (manufacturer, date, crash, city, state) where these tuple items have the following types: manufacturer -- this comes from the MFR_NAME field in the DOT format date -- this comes from the FAILDATE field in the DOT format, but converted to a Python datetime.date object crash -- this comes from the CRASH field in the DOT format, but converted to a Python bool type (True for a crash) city -- comes from the CITY field in the DOT format state -- comes from the STATE field in the DOT format should return: fieldict("DOT500.txt")[82] ('FORD MOTOR COMPANY', datetime.date(1995, 1, 1), False, 'MARBLE HEAD', 'MA') and here are parts of the data: 1 958164 TOYOTA MOTOR CORPORATION TOYOTA LAND CRUISER 1994 19941223 N 0 0 SERVICE BRAKES, HYDRAULIC:ANTILOCK ARNOLD CA JT3DJ81W8R0 19950103 19950103 ABS SYSTEM FAILURE, AT 20MPH. TT EVOQ V 2 958156 TOYOTA MOTOR CORPORATION TOYOTA PASEO 1994 Y 19941226 N 0 0 PARKING BRAKE:CONVENTIONAL SAN JOSE CA JT2EL45U5R0 19950103 19950103 1 PARKED ON FLAT SURFACE EMERGENCY BRAKING ENGAGED VEHICLE ROLLED REARWARD. TT EVOQ V 3 958124 TOYOTA MOTOR CORPORATION TOYOTA COROLLA 1994 Y 19941128 N 0 0 AIR BAGS:FRONTAL PHOENIX AZ 19950103 19950103 UPON FRONTAL COLLISION, AIR BAG FAILED TO DEPLOY. VEHICLE CLASSIFIED AS TOTALED. PLEASE DESCRIBE DETAILS. TT EVOQ V 4 958122 NISSAN NORTH AMERICA, INC. NISSAN MAXIMA 1994 19950103 N 0 0 SUSPENSION TUCSON AZ JN1HJ01F4RT 19950103 19950103 THE STRUT WAS BAD THERE IS A NOISE ON THE PASSENGER SIDE DOOR AND THE ENGINE LIGHT MALFUNCTION. TT EVOQ V 5 958122 NISSAN NORTH AMERICA, INC. NISSAN MAXIMA 1994 19950103 N 0 0 ENGINE AND ENGINE COOLING:ENGINE TUCSON AZ JN1HJ01F4RT 19950103 19950103 THE STRUT WAS BAD THERE IS A NOISE ON THE PASSENGER SIDE DOOR AND THE ENGINE LIGHT MALFUNCTION. TT EVOQ V Here is my code and I dont know why my code is only reading the 500th line of the file. Thanks for your help! import datetime def boolean(S): if S=="Y": return True return False def fieldict(filename): D={} with open(filename) as FileObject: for lines in FileObject: linelist=lines.split('\t') Key=linelist[0] ValCity=(linelist[12]).strip() ValState=linelist[13] ValOne=linelist[2] ValTwo=linelist[6] ValThree=boolean(linelist[7]) D={Key:(ValOne, ValTwo, ValThree, ValCity,ValState)} return D print fieldict("DOT500.txt") -------------- next part -------------- An HTML attachment was scrubbed... URL: From pete at radiokinetics.com Tue Oct 30 07:56:07 2012 From: pete at radiokinetics.com (Pete) Date: Mon, 29 Oct 2012 23:56:07 -0700 Subject: [Tutor] Help with OOP! Message-ID: I'm taking this class on python at UCSC. They said this list could help. I don't' understand OOP and I'm having a hard time understanding the "scope" and why the def inside class are not like function -plus I can't get my c-brain around the implicit typing. It looks like the interpreter does not want to return a tuple. why? I don't' know if anybody can fix this, or offer a better solution to this problem. The code and assignment are attached. Pete -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: hw612.py URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: hw6.pdf Type: application/pdf Size: 332541 bytes Desc: not available URL: