Re: [Tutor] Binary Real to Decimal
Le Sun, 29 Mar 2009 22:42:43 -0500, Chris Castillo s'exprima ainsi: > myinput = raw_input("Please enter a binary real number: ") > myinput = myinput.split(".") > > binstr1 = myinput[0] > binstr2 = myinput[1] > > decnum1 = 0 > decnum2 = 0 > > for i in binstr1: > decnum1 = decnum1 * 2 + int(i) > > for k in binstr2: > decnum2 = decnum2 * 2 + int(k) > > > > print "\nThe binary real number ", binstr1, ".", binstr2, " converts to ", > decnum1,".",decnum2," in decimal." > > > that is what I have so far but I need to create a condition where I need > only 10 sufficient numbers from the variable decnum2. I know I need > something like > if len(decnum2) > 11: > decnum2 = decnum2[0:11] > > but I keep getting unsubscriptable errors. I know it has to do with types > but it's late and I just need some help. thank you in advance. - chris This is a symptom that you mistake numeral values for textual ones. Your loops for k in binstr2: decnum2 = decnum2 * 2 + int(k) build integers. They are numbers for python, not text snippets that happen to represent numbers for humans. When you print (decnum1,".",decnum2), python silently converts both integers to standard representations of integers (eg 123 --> "123") that happen to be what you expect to see. Then if you join together two integer representations with '.' as glue, sure you will get something that looks like a (representation of a) real number -- but this is only superficial appearance. You have never built a real real (!) number (type float). You cannot use the result to compute anything, for instance. To get the result, you need to process together decnum1 (integral part) and decnum2 (fractional part) into a single (real) number. Then print the result. Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Shelve doesn't free up memory
Hello, I have a PyGTK application where the user is able to click on a button, then a new dialog pops up with a treeview and the program fills this view with data from a shelve. Everything works, the data is being added to the treeview. The only problem is, that when I close the dialog, it doesn't free the memory. So when I click the button again, the same amount of memory is added being used. Then close the window, click again, again the same amount of memory, and so on... Now it only takes about 5 to 8 mb, but this shouldn't be. I do call the shelve.close(), but that doesn't do it apparently. Do I need to do something when I close my window? Here are parts of the code: # Result window => Called when button is clicked import Results class ResultWindow: def __init__(self): # Build the dialog and the treeview... self.get_results() def get_results(self): self.liststore.clear() for person in self.persons: dics = Results.read_result(person) if not dics: continue for dic in dics: date = dic['date'] point = dic['point'] place = dic['place'] out = dic['out'] self.liststore.append([date, point, place, out]) # Results file import shelve def read_result(person): results = [] s = shelve.open(RESULTFILE) try: results = s[person] except KeyError: #print "No results for this person" pass finally: s.close() return results ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] how are unicode chars represented?
Everything is in the title ;-) (Is it kind of integers representing the code point?) Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] __callattr__ ?
Hello, Is there something like a __callattr__ magic method that would catch either unknown (like __getattr__) or all (like __getattribute__) method calls? If not, how would you do that? Also if not, do you know why we have __getattr__, __setattr__, but no __callattr__? Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how are unicode chars represented?
On Mon, Mar 30, 2009 at 3:36 AM, spir wrote: > Everything is in the title ;-) > (Is it kind of integers representing the code point?) Unicode is represented as 16-bit integers. I'm not sure, but I don't think Python has support for surrogate pairs, i.e. characters outside the BMP. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Left Alignment -- Tkinter
Title: Signature.html I'm tossing in the towel on this one. I have an acceptable way to put in a few lines like: Latitude BOX Longitude BOX Instead of prototyping it separately from the main program, I just went directly to the author's code, and put the lines in the common style he used. It got Latitude lined up to the left along with reasonable positions for the other entries he used in rows above and below where I'm working. The Longitude BOX pair is spread a bit more than I would like. It'll be OK. It's really too bad that someone doesn't write a fully comprehensible description of grid. Grayson comes close, and maybe even closer than I can imagine, since I only have his chapter 5 to go on. Putting out $50 to get the whole book (digital form) on material that is dated does not seem appropriate. The other choice is the Python Org recommended (old) Tk/Tcl book, but do I need to spend 3-6 weeks installing and learning to use it, so that I can translate it into Tkinter? Not really. Otherwise, this is too much guess work and experimentation. The Tkinter newsgroup is pretty low traffic to get any timely help. Maybe things will be better with the new Tkinter. This does not look like rocket science. Just missing parts of the story. -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) “Life is one damn thing after another." -- Mark Twain ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] __callattr__ ?
On Mon, Mar 30, 2009 at 5:51 AM, spir wrote: > Hello, > > Is there something like a __callattr__ magic method that would catch either > unknown (like __getattr__) or all (like __getattribute__) method calls? > If not, how would you do that? Also if not, do you know why we have > __getattr__, __setattr__, but no __callattr__? Methods are just callable attributes. If you have for example class Foo(object): def sayFoo(self): print 'foo' f = Foo() then sayFoo is an attribute of class Foo. When you then write f.sayFoo() what that means is, - look up the sayFoo attribute on object f (returning the class attribute since f has no sayFoo attribute itself) - call the object that results So, to intercept calls to unknown methods you use __getattr__ or __getattribute__ just as for other attributes. For example, In [10]: class Foo(object): : def __getattr__(self, name): : def show(): : print 'called', name : return show In [18]: f = Foo() In [19]: f.superduper() called superduper Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] installation of scipy
Friends i installed scipy in fedora10 using yum. when i import stats module in it, i got the following warning. someone pls englihten me on this. >>> from scipy import stats /usr/lib/python2.5/site-packages/scipy/sparse/linalg/dsolve/linsolve.py:20: DeprecationWarning: scipy.sparse.linalg.dsolve.umfpack will be removed, install scikits.umfpack instead ' install scikits.umfpack instead', DeprecationWarning ) Thanks, Bala ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Binary Real to Decimal
I don't know what "sufficient numbers" means, but perhaps it's "significant digits" that was intended. And you have to decide if you want ten digits to the right of the decimal point, or ten significant digits in the whole number. That determines whether you want to round decnum2 or the final value you get from combining decnum1 and decnum2. But you have two other problems to address before you do any rounding, I'll address only the first. 1) The present value displayed are simply wrong. 2) You need to combine the two integers decnum1 and decnum2 into a single real. If the user enters1.1010 you display 1 . 10 which is not the correct decimal representation of the value entered. 1.1010 binary is 1.625 in decimal. A few more samples: 11.0001 is not 3.1 but is actually 3.0625 110.1 is not6.1 but is actually 6.03125 110.00011 is not6.3 but is actually 6.09375 In particular, your logic doesn't take into account the number of digits entered to the right of the binary point. decnum2 must be divided by some power of two to get the right value. Once you get a useful representation for the decnum2 part, it should be obvious how to combine the two. Chris Castillo wrote: myinput = raw_input("Please enter a binary real number: ") myinput = myinput.split(".") binstr1 = myinput[0] binstr2 = myinput[1] decnum1 = 0 decnum2 = 0 for i in binstr1: decnum1 = decnum1 * 2 + int(i) for k in binstr2: decnum2 = decnum2 * 2 + int(k) print "\nThe binary real number ", binstr1, ".", binstr2, " converts to ", decnum1,".",decnum2," in decimal." that is what I have so far but I need to create a condition where I need only 10 sufficient numbers from the variable decnum2. I know I need something like if len(decnum2) > 11: decnum2 = decnum2[0:11] but I keep getting unsubscriptable errors. I know it has to do with types but it's late and I just need some help. thank you in advance. - chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] installation of scipy
hi! Bala. On Mon, Mar 30, 2009 at 3:57 PM, Bala subramanian wrote: > Friends > i installed scipy in fedora10 using yum. when i import stats module in it, i > got the following warning. someone pls englihten me on this. > from scipy import stats > /usr/lib/python2.5/site-packages/scipy/sparse/linalg/dsolve/linsolve.py:20: > DeprecationWarning: scipy.sparse.linalg.dsolve.umfpack will be removed, > install scikits.umfpack instead > ' install scikits.umfpack instead', DeprecationWarning ) > deprecation is the way in python through which the developers warn the user or the other developers that, a certain feature that has been mentioned, would be removed or not available from the next release. so as of now, just enjoy & ignore the error. by the next release cycle, developers of scipy will need remove the deprecated code or use the suggested packages in the deprecation warning. > Thanks, > Bala > > ___ > Tutor maillist - tu...@python.org > http://mail.python.org/mailman/listinfo/tutor > > -- Regards, Arun Tomar blog: http://linuxguy.in website: http://www.solutionenterprises.co.in ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Shelve doesn't free up memory
Timo wrote: # Results file import shelve def read_result(person): results = [] s = shelve.open(RESULTFILE) try: results = s[person] Maybe passing this out prevents s from being garbage collected? Emile except KeyError: #print "No results for this person" pass finally: s.close() return results ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Left Alignment -- Tkinter
On Sun, Mar 29, 2009 at 9:30 AM, Wayne Watson wrote: > I'm looking at the NM Tech Tkinter ref, pages 5-6, on the grid > method. See pages 84-88 of Lundh. Nothing. It does not show that method. > Search of the pdf doc shows nothing. Are these sources too old? effbot does > have it. Yes, it's pretty decent. I've used it before. It looks like it may > be the newest, 2005, of the three, although NM Tech seems to get updated > pretty often. 5/2007, but I think there was a recent update. Maybe they > don't want the students to use it. > I haven't really looked at a lot of tkinter refs. Effbot tends to have most of the info I need - it just takes a little playing around and sometimes google or the python list to figure out what part I'm missing. > >> What I've discovered is that I did not really understand the role of >> sticky, and the bounds of the label. I thought sticky=W meant put the >> blasted label to the left margin. What sticky means, according to Grayson's >> chapter 5 on the web, is that it allows the widget to stretch when the >> larger window is resized. Knowing the boundaries with color coding can help >> understand that, and other oddities. Label seems to always center the text. >> Changing the label's width and height achieves interesting insights. I tried >> anchor with Label and it does interesting things. The length of the text >> messes with matters. >> > > http://effbot.org/tkinterbook/label.htm > > The justify option will change the alignment of text in the label. > > It didn't move the text in the label at all. There's got to be some padding > on either end I'm missing. > Actually, it turns out to be my mistake at not clearly reading the documentation he has for the justify option. Justify is for multiple lines of text, anchor will anchor the text. Try with width=30, anchor=W and you should see what you're looking for. > Interestingly, I had set the width of the label to 12, and the color > version showed gray to the left and right of the text, with the text in the > center. I removed width, and the left-right spaces disappeared, but the text > was still centered. Well, OK, the selected width, which matches the length > of the text, really doesn't allow for justification. Foiled again. It seems > like the width for the frame container for the latitude+BOX needs to be > specified to give latitude some ability to go left. A column for latitude > and one for BOX? Beats me. Back to exploration after I finish this > response. > It appears you're correct - when I used anchor with padx it was ignored, but when I changed padx to width it worked as expected. I'm not sure why it does or doesn't, and I haven't had a lot of time for a lot of experimenting, and now I have class so I'll just have to leave it for now. HTH, Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] incrementing one minute
I need to add one minute to a string that has a date and a time in MMDDHHMM format. e.g: 200903281346 should become 200903281347 the following script converts the string into time and adds one minute; but somehow I also add an hour and I don't understand why. import time #set the initial time as a string and convert it into time format: fromtimestring = '200903281346' fromtimetime = time.strptime(fromtimestring, "%Y%m%d%H%M") #convert this time format into UNIX time (seconds since the start of UNIX time): fromtimeseconds = time.mktime(fromtimetime) #add 60 seconds and reformat the result into the MMDDHHMM format totimeseconds = fromtimeseconds + 60 totimetime = time.gmtime(totimeseconds) # convert the new time into a string: totimestring = time.strftime("%Y%m%d%H%M", totimetime) #print the results: print (fromtimestring) print (fromtimetime) print (totimetime) print (totimestring) any help or suggestions would be much appreciated. Payo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] incrementing one minute
I fixed it by re-setting my system clock to GMT. ... it seems a bit of a botch but it works. Payo On Mon, Mar 30, 2009 at 4:52 PM, pa yo wrote: > I need to add one minute to a string that has a date and a time in > MMDDHHMM format. > e.g: 200903281346 should become 200903281347 > > the following script converts the string into time and adds one > minute; but somehow I also add an hour and I don't understand why. > > > > import time > > #set the initial time as a string and convert it into time format: > fromtimestring = '200903281346' > fromtimetime = time.strptime(fromtimestring, "%Y%m%d%H%M") > #convert this time format into UNIX time (seconds since the start of UNIX > time): > fromtimeseconds = time.mktime(fromtimetime) > #add 60 seconds and reformat the result into the MMDDHHMM format > totimeseconds = fromtimeseconds + 60 > totimetime = time.gmtime(totimeseconds) > # convert the new time into a string: > totimestring = time.strftime("%Y%m%d%H%M", totimetime) > > #print the results: > print (fromtimestring) > print (fromtimetime) > print (totimetime) > print (totimestring) > > > > any help or suggestions would be much appreciated. > > > Payo > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] incrementing one minute
2009/3/30 pa yo : > I need to add one minute to a string that has a date and a time in > MMDDHHMM format. > e.g: 200903281346 should become 200903281347 > > the following script converts the string into time and adds one > minute; but somehow I also add an hour and I don't understand why. > > > > import time > > #set the initial time as a string and convert it into time format: > fromtimestring = '200903281346' > fromtimetime = time.strptime(fromtimestring, "%Y%m%d%H%M") > #convert this time format into UNIX time (seconds since the start of UNIX > time): > fromtimeseconds = time.mktime(fromtimetime) > #add 60 seconds and reformat the result into the MMDDHHMM format > totimeseconds = fromtimeseconds + 60 > totimetime = time.gmtime(totimeseconds) > # convert the new time into a string: > totimestring = time.strftime("%Y%m%d%H%M", totimetime) > > #print the results: > print (fromtimestring) > print (fromtimetime) > print (totimetime) > print (totimestring) > > > > any help or suggestions would be much appreciated. > > > Payo > ___ > Tutor maillist - tu...@python.org > http://mail.python.org/mailman/listinfo/tutor > When does it add an hour? Is it only on specific input strings, or is it arbitrary? If its the former, what input strings does it happen on? Can you spot any pattern to the inputs that it occurs on? It's possible that it's getting confused with dates either side of the British Summertime change. Also, your variable names are rather cryptic... you might consider using studlyCapsWithCapitalInitialLetters or underscores_between_words. -- Richard "Roadie Rich" Lovely, part of the JNP|UK Famile www.theJNP.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] incrementing one minute
2009/3/30 pa yo : > I need to add one minute to a string that has a date and a time in > MMDDHHMM format. > e.g: 200903281346 should become 200903281347 > > the following script converts the string into time and adds one > minute; but somehow I also add an hour and I don't understand why. > > > > import time > > #set the initial time as a string and convert it into time format: > fromtimestring = '200903281346' > fromtimetime = time.strptime(fromtimestring, "%Y%m%d%H%M") > #convert this time format into UNIX time (seconds since the start of UNIX > time): > fromtimeseconds = time.mktime(fromtimetime) > #add 60 seconds and reformat the result into the MMDDHHMM format > totimeseconds = fromtimeseconds + 60 > totimetime = time.gmtime(totimeseconds) > # convert the new time into a string: > totimestring = time.strftime("%Y%m%d%H%M", totimetime) > > #print the results: > print (fromtimestring) > print (fromtimetime) > print (totimetime) > print (totimestring) > > > > any help or suggestions would be much appreciated. You could do this with datetime and timedelta from the datetime module. >>> from datetime import datetime, timedelta >>> fromtimetime = datetime.strptime('200903281346', '%Y%m%d%H%M') >>> fromtimetime datetime.datetime(2009, 3, 28, 13, 46) >>> delta = timedelta(seconds=60) >>> delta datetime.timedelta(0, 60) >>> fromtimetime + delta datetime.datetime(2009, 3, 28, 13, 47) >>> datetime.strftime(fromtimetime + delta, '%Y%m%d%H%M') '200903281347' Greets Sander ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] installation of scipy
More specifically, in this case, numpy.stats should be used instead of scipy.stats You will not see the deprecation warning with numpy.stats On Mon, Mar 30, 2009 at 5:15 PM, Arun Tomar wrote: > hi! > Bala. > > On Mon, Mar 30, 2009 at 3:57 PM, Bala subramanian > wrote: > > Friends > > i installed scipy in fedora10 using yum. when i import stats module in > it, i > > got the following warning. someone pls englihten me on this. > > > from scipy import stats > > > /usr/lib/python2.5/site-packages/scipy/sparse/linalg/dsolve/linsolve.py:20: > > DeprecationWarning: scipy.sparse.linalg.dsolve.umfpack will be removed, > > install scikits.umfpack instead > > ' install scikits.umfpack instead', DeprecationWarning ) > > > > deprecation is the way in python through which the developers warn the > user or the other developers that, a certain feature that has been > mentioned, would be removed or not available from the next release. > > so as of now, just enjoy & ignore the error. by the next release > cycle, developers of scipy will need remove the deprecated code or use > the suggested packages in the deprecation warning. > > Thanks, > > Bala > > > > ___ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > > > > > -- > Regards, > Arun Tomar > blog: http://linuxguy.in > website: http://www.solutionenterprises.co.in > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] incrementing one minute
mktime() and gmtime() are not inverses of each other. The first assumes local time, and the latter gmt (or utc). So unless you happen to be in England, and not in daylight savings time, you'd expect a problem. mktime() is documented as the inverse of localtime(), according to the docs. I'd assume they'd both make the same time adjustments for your location. However, there's some ambiguity if the time you're looking at is in standard time, while you're currently in daylight savings. So I'd look for an answer that only used UTC (or Greenwich Mean time). Try time.gmtime(), and calendar.timegm() pa yo wrote: I need to add one minute to a string that has a date and a time in MMDDHHMM format. e.g: 200903281346 should become 200903281347 the following script converts the string into time and adds one minute; but somehow I also add an hour and I don't understand why. import time #set the initial time as a string and convert it into time format: fromtimestring = '200903281346' fromtimetime = time.strptime(fromtimestring, "%Y%m%d%H%M") #convert this time format into UNIX time (seconds since the start of UNIX time): fromtimeseconds = time.mktime(fromtimetime) #add 60 seconds and reformat the result into the MMDDHHMM format totimeseconds = fromtimeseconds + 60 totimetime = time.gmtime(totimeseconds) # convert the new time into a string: totimestring = time.strftime("%Y%m%d%H%M", totimetime) #print the results: print (fromtimestring) print (fromtimetime) print (totimetime) print (totimestring) any help or suggestions would be much appreciated. Payo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] incrementing one minute
This is the second post I've seen on this homework assignment. You might look at the Python List for other ideas. mktime() and gmtime() are not inverses of each other. The first assumes local time, and the latter gmt (or utc). So unless you happen to be in England, and not in daylight savings time, you'd expect a problem. mktime() is documented as the inverse of localtime(), according to the docs. I'd assume they'd both make the same time adjustments for your location. However, there's some ambiguity if the time you're looking at is in standard time, while you're currently in daylight savings. So I'd look for an answer that only used UTC (or Greenwich Mean time). Try time.gmtime(), and calendar.timegm() pa yo wrote: I need to add one minute to a string that has a date and a time in MMDDHHMM format. e.g: 200903281346 should become 200903281347 the following script converts the string into time and adds one minute; but somehow I also add an hour and I don't understand why. import time #set the initial time as a string and convert it into time format: fromtimestring = '200903281346' fromtimetime = time.strptime(fromtimestring, "%Y%m%d%H%M") #convert this time format into UNIX time (seconds since the start of UNIX time): fromtimeseconds = time.mktime(fromtimetime) #add 60 seconds and reformat the result into the MMDDHHMM format totimeseconds = fromtimeseconds + 60 totimetime = time.gmtime(totimeseconds) # convert the new time into a string: totimestring = time.strftime("%Y%m%d%H%M", totimetime) #print the results: print (fromtimestring) print (fromtimetime) print (totimetime) print (totimestring) any help or suggestions would be much appreciated. Payo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] incrementing one minute
I am trying to filter Open Street Map nodes from http://planet.openstreetmap.org/minute/ ... into wikimark up for Yellowikis (http://www.yellowikis.org) I work from home - but this isn't a homework assignment. :-) Paul Y On Mon, Mar 30, 2009 at 5:52 PM, Dave Angel wrote: > This is the second post I've seen on this homework assignment. You might > look at the Python List for other ideas. > > mktime() and gmtime() are not inverses of each other. The first assumes > local time, and the latter gmt (or utc). So unless you happen to be in > England, and not in daylight savings time, you'd expect a problem. > > mktime() is documented as the inverse of localtime(), according to the docs. > I'd assume they'd both make the same time adjustments for your location. > However, there's some ambiguity if the time you're looking at is in > standard time, while you're currently in daylight savings. So I'd look for > an answer that only used UTC (or Greenwich Mean time). > > Try time.gmtime(), and calendar.timegm() > > > pa yo wrote: > >> I need to add one minute to a string that has a date and a time in >> MMDDHHMM format. >> e.g: 200903281346 should become 200903281347 >> >> the following script converts the string into time and adds one >> minute; but somehow I also add an hour and I don't understand why. >> >> >> >> import time >> >> #set the initial time as a string and convert it into time format: >> fromtimestring = '200903281346' >> fromtimetime = time.strptime(fromtimestring, "%Y%m%d%H%M") >> #convert this time format into UNIX time (seconds since the start of UNIX >> time): >> fromtimeseconds = time.mktime(fromtimetime) >> #add 60 seconds and reformat the result into the MMDDHHMM format >> totimeseconds = fromtimeseconds + 60 >> totimetime = time.gmtime(totimeseconds) >> # convert the new time into a string: >> totimestring = time.strftime("%Y%m%d%H%M", totimetime) >> >> #print the results: >> print (fromtimestring) >> print (fromtimetime) >> print (totimetime) >> print (totimestring) >> >> >> >> any help or suggestions would be much appreciated. >> >> >> Payo >> >> > > ___ > Tutor maillist - tu...@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] range() fractional increment
1) I feel dumb for asking this. 2) I looked for 20 minutes and didn't find an answer Trying to make a drawLine function in a 2d array. example: x row = 25 : col = 10 x row = 26 : col = 10.3 x row = 27 : col = 10.6 0x000 row = 28 : col = 11 0x000 row = 29 : col = 11.3 0x000 row = 30 : col = 11.6 00x00 row = 31 : col = 12 for row in range(25,31,1): for col in range(10,12, 0.3): #<- Crash Bang doesn't work 0.3 = zero = infinite loop? so then I tried... >>> c = 10 >>> while(c < 12.3): ... print c ... c += 1.0/3.0 ... 10 10.33 10.67 11.0 11.33 11.67 12.0 is there no way to do it with a range function (and have it still look like you're not on crack)? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] range() fractional increment
2009/3/31 james carnell : > for row in range(25,31,1): > for col in range(10,12, 0.3): #<- Crash Bang doesn't work 0.3 = zero = > infinite loop? > [...] > is there no way to do it with a range function (and have it still look like > you're not on crack)? Well, you could do this: >>> [float(x)/3 for x in range(30, 37)] [10.0, 10.334, 10.666, 11.0, 11.334, 11.666, 12.0] Or even: >>> [math.floor(10*float(x)/3)/10 for x in range(30, 37)] [10.0, 10.301, 10.6, 11.0, 11.301, 11.6, 12.0] However, the builtin range() only works with integers. I think there is a range() function in the python cookbook that will do fractional step sizes. But really, there's nothing wrong with your while loop. -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] range() fractional increment
John Fouhy wrote: 2009/3/31 james carnell : for row in range(25,31,1): for col in range(10,12, 0.3): #<- Crash Bang doesn't work 0.3 = zero = infinite loop? [...] is there no way to do it with a range function (and have it still look like you're not on crack)? Well, you could do this: [float(x)/3 for x in range(30, 37)] [10.0, 10.334, 10.666, 11.0, 11.334, 11.666, 12.0] Or even: [math.floor(10*float(x)/3)/10 for x in range(30, 37)] [10.0, 10.301, 10.6, 11.0, 11.301, 11.6, 12.0] However, the builtin range() only works with integers. I think there is a range() function in the python cookbook that will do fractional step sizes. But really, there's nothing wrong with your while loop. You could boil your own range function (beware: binary floating point may be imprecise) def frange(start, stop, step): width = stop - start n = round(width / step) return [start + step*i for i in xrange(n)] or returning generator instead (may be preferable if the size of the list is extremely large): def frange(start, stop, step): width = stop - start n = round(width / step) return (start + step*i for i in xrange(n)) slightly obscured version: def frange(start, stop, step): return (start + step*i for i in range(round((stop - start) / step))) more complex homebrew frange may return a class that really emulates xrange, by implementing __getitem__, iterator protocol, generator protocol, and all the sugar without ever creating a real list. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how are unicode chars represented?
"Kent Johnson" wrote in message news:1c2a2c590903300352t2bd3f1a7j5f37703cf1c3...@mail.gmail.com... On Mon, Mar 30, 2009 at 3:36 AM, spir wrote: Everything is in the title ;-) (Is it kind of integers representing the code point?) Unicode is represented as 16-bit integers. I'm not sure, but I don't think Python has support for surrogate pairs, i.e. characters outside the BMP. Unicode is simply code points. How the code points are represented internally is another matter. The below code is from a 16-bit Unicode build of Python but should look exactly the same on a 32-bit Unicode build; however, the internal representation is different. Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. x=u'\U00012345' x.encode('utf8') '\xf0\x92\x8d\x85' However, I wonder if this should be considered a bug. I would think the length of a Unicode string should be the number of code points in the string, which for my string above should be 1. Anyone have a 32-bit Unicode build of Python handy? This exposes the implementation as UTF-16. len(x) 2 x[0] u'\ud808' x[1] u'\udf45' -Mark ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor