Re: [Tutor] Array slices in python with negative step
On Mon, 11 Jan 2010 21:50:10 +0530 vishwajeet singh wrote: > Hello All, > > I am bit confuse how the slice works in case a negative step is supplied > a = 'abcde' > a[::-1] gives me edcba > > but [4:0:-1] gives me edcb > > while searching net I came accross something which said the following: > > If a negative stride is specified and the starting or stopping indices are > omitted, they default to ``end of axis'' and ``beginning of axis'' > respectively. > In my case 4 is end of axis and 0 is begining of the axis so a[::-1] should > be equivalent to [4:0:-1] but result which I get in both the cases does not > validate. Your interpretation is wrong. In your case, 4 is start index, 0 is end index (as obviously shown by the result you get). Step -1 lets you step backwards, but this does not change the fact that the end-index is the second argument of the slice, right? Now, python work with (right-side) half-open intervals, meaning that the second border is excluded (s[0:4] would return "abcd"). Which means in your case you won't get a[0] in the result. Hope I'm clear... It can be a bit misleading at start but, as it's consistent, once you get used to it all works fine ;-) Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Searching in a file
On Wed, 13 Jan 2010 23:05:11 - "Alan Gauld" wrote: > But a third option is to use a split and apply it to the whole file as > a string thereby breaking the file into as many chunks as start with > a line containing 'NEW'... Why not simply a regex pattern starting with "NEW" and ending with '\n'? import re pat = re.compile("""NEW(.+)\n""") source = "abc\defNEWghi\njkl\nmno\nNEWpqr\nstu\n" print pat.findall(source) # ==> ['ghi', 'pqr'] Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Keeping a list of attributes of a certain type
On Wed, 13 Jan 2010 20:15:21 -0800 "Guilherme P. de Freitas" wrote: > Ok, I got something that seems to work for me. Any comments are welcome. > > > class Member(object): > def __init__(self): > pass > > > class Body(object): > def __init__(self): > self.members = [] > > def __setattr__(self, obj, value): > if isinstance(value, Member): > self.members.append(obj) > object.__setattr__(self, obj, value) > else: > object.__setattr__(self, obj, value) > > def __delattr__(self, obj): > if isinstance(getattr(self, obj), Member): > self.members.remove(obj) > object.__delattr__(self, obj) > else: > object.__delattr__(self, obj) Seems perfectly ok to me, except for if...else constructs in place of simple if's: an optional statement is added in case obj is of type Member; this is not an alternative. if isinstance(value, Member): self.members.append(obj) object.__setattr__(self, obj, value)# in both cases Same for __delattr__, indeed. Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Html entities, beautiful soup and unicode
On Tue, 19 Jan 2010 08:49:27 +0100 andy wrote: > Hi people > > I'm using beautiful soup to rip the uk headlines from the uk bbc page. > This works rather well but there is the problem of html entities which > appear in the xml feed. > Is there an elegant/simple way to convert them into the "standard" > output? By this I mean £ going to  ? or do i have to use regexp? > and where does unicode fit into all of this? Ha, ha! What do you mean exactly, convert them into the "standard" output? What form do you expect, and to do what? Maybe your aim is to replace number-coded html entities in a python string by real characters in a given format, to be able to output them. Then one way may be to use a simple regex and replace with a custom function. Eg: import re def rep(result): string = result.group() # "xx;" n = int(string[2:-1]) uchar = unichr(n) # matching unicode char # for you dest format may be iso-8859-2 ? return unicode.encode(uchar, "utf-8") # format-encoded char source = "xxx¡xxxÂxxxÿxxx" pat = re.compile("""\d+;""") print pat.sub(rep, source) Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] combinatorics problem: assembling array
On Thu, 21 Jan 2010 10:06:40 +0100 markus kossner wrote: > Dear Pythonics, > I have a rather algorithmic problem that obviously made a knot in my brain: > > Assume we have to build up all the arrays that are possible if we have a > nested array > containing an array of integers that are allowed for each single > position. For example > the nested array > ( > (1,2,3,89), > (3,5,8), > (19,30,7,100,210,1,44) > ) > would define all the arrays of length 3 that can be enumerated using > the > array of possible numbers for each position. > Beforehand I will have no Idea of the length of the arrays. How do I > enumerate all the possible > arrays with python? > > Thanks for getting the knot out of my brain, > Markus A loop over first set of numbers, with an inner loop over second set of numbers, with an inner loop over third set of numbers. Deep inside the loops, just feed a list of combinations. Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is it pythonesque
On Sun, 24 Jan 2010 11:13:37 -0500 "Robert Berman" wrote: > Good morning, > > > > Given the following code snippets: > > > > def getuserinput(): > > while True: > > s1 = raw_input('Enter fraction as N,D or 0,0 to exit>>') > > delim = s1.find(',') > > if delim < 0: > > print 'invalid user input' > > else: > > n = int(s1[0:delim]) > > d = int(s1[delim+1::]) > > return n,d > > > > def main(): > > while True: > > n,d = getuserinput() > > if n == 0 or d == 0: return 0 > > > > n and d are always returned provided the input is given as n,d. n/d will > print an error message and the loop will reiterate until the user format is > correct. Please note there is no true ending return for getuserinput() as it > is hung off an if statement and if by some chance it breaks, it should > return None which will abort the program. While I suspect this style is > devious, is it dangerous or 'wrong' to use. Comments and other renditions > are most welcome. > > > > Thank you, > > > > Robert Berman -0- Blank lines between statament is certainly not pythonesque ;-) -1- Why not chose '/' as delimiter? -2- You may check there is a single delimiter. -3- You may consider a user-friendly (eg ''), rather than a programmer-friendly ("0,0") code for breking the loop. It'd cost you a pair of lines of code. For instance: FRACT = '/' def getUserInput(): while True: s1 = raw_input('Enter fraction as N/D or enter to exit > 1') if s1 == '': return None# will trigger exit in main nDelims = s1.count(FRACT) # only 1 is valid if nDelims == 1: n,d = s1.split(FRACT) return int(n),int(d) # remaining issue print 'invalid user input' getUserInput() An issue issue remains, namely that what around FRACT may not be valid integers. Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The magic parentheses
On Mon, 25 Jan 2010 01:06:45 - "Alan Gauld" wrote: > > "Lie Ryan" wrote > > >> and used print, I thought they would be considered the same whether as > >> a variable, or as a direct line, guess not. > > what is equivalent: > > print (a, b, c) > > > > and > > x = a, b, c > > print x > > > > both construct a tuple and prints a,b,c as tuple > > Not quite: > > >>> a = 1 > >>> b = 2 > >>> c = 3 > >>> x = a,b,c > >>> print a,b,c > 1 2 3 > >>> print x > (1, 2, 3) > >>> > > The first form prints multiple values the second prints the repr of a > single tuple value. The output is different. > > Alan G. Lie's example actually was: >>> a,b,c = 1,2,3 >>> print (a,b,c) # here parenthesized (1, 2, 3) >>> x = a,b,c >>> print x (1, 2, 3) Both output values are tuples. Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] get (x)range borders
Hello, Is there a way to retrieve an (x)range's borders? (Eg if I want to print out "m..n" instead of "xrange(m, n)".) Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] get (x)range borders
On Thu, 28 Jan 2010 13:37:28 +0100 Alan Plum wrote: > You mean like this? > > >>> m = 20 > >>> n = 30 > >>> a = xrange(m, n) > >>> a > xrange(20, 30) > >>> a[0] > 20 > >>> a[-1] > 29 !!! Did not even think at that (looked for attributes of xrange instances). Thank you. Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] hash value input
Hello, What actually is hashed when a data item is used a dict key? If possible, I would also like some clues on the method used to produce the hash value. (Maybe a pointer to the the relevant part of the python source, if clear and/or commented.) The reason why I ask is the well known limitation for dict keys to be immutable data (same for sets, indeed). This seems to imply that the value itself is used as source for the hash func. Or am I wrong on this? I recently discovered that Lua uses the data's address (read: id) as input to the hash func. This allows Lua tables (a kind of more versatile associative array) to use _anything_ as key, since the id is guaranteed not to change, per definition. [Aside this consideration, hashing addresses ensures a constant type as input (so allows a specific efficient hash method) and the simplest possible one.] So, how does python do this? Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] hash value input
On Fri, 29 Jan 2010 08:23:37 -0800 Emile van Sebille wrote: > > So, how does python do this? > > > > Start here... > > http://effbot.org/zone/python-hash.htm Great, thank you! From the above pointed page: === For ordinary integers, the hash value is simply the integer itself (unless it’s -1). class int: def __hash__(self): value = self if value == -1: value == -2 return value === I'm surprised of this, for this should create as many indexes (in the underlying array actually holding the values) as there are integer keys. With possibly huge holes in the array. Actually, there will certainly be a predefined number of indexes N, and the integers be further "modulo-ed" N. Or what? I would love to know how to sensibly chose the number of indexes. Pointers welcome (my searches did not bring any clues on the topic). > Emile Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem creating a search
On Sat, 30 Jan 2010 19:45:04 -0600 Luke Paireepinart wrote: > The key here is the "for line in infile" will not keep the whole file in > memory ... provided the file is structured in lines. (Also, the searched term should not be nore hold a newline). Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] parse text file
On Mon, 1 Feb 2010 00:43:59 +0100 Norman Khine wrote: > but this does not take into account of data which has negative values just add \-? in front of \d+ Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] parse text file
On Mon, 1 Feb 2010 16:30:02 +0100 Norman Khine wrote: > On Mon, Feb 1, 2010 at 1:19 PM, Kent Johnson wrote: > > On Mon, Feb 1, 2010 at 6:29 AM, Norman Khine wrote: > > > >> thanks, what about the whitespace problem? > > > > \s* will match any amount of whitespace includin newlines. > > thank you, this worked well. > > here is the code: > > ### > import re > file=open('producers_google_map_code.txt', 'r') > data = repr( file.read().decode('utf-8') ) > > block = re.compile(r"""openInfoWindowHtml\(.*?\\ticon: myIcon\\n""") > b = block.findall(data) > block_list = [] > for html in b: > namespace = {} > t = re.compile(r"""(.*)<\/strong>""") > title = t.findall(html) > for item in title: > namespace['title'] = item > u = re.compile(r"""a href=\"\/(.*)\">En savoir plus""") > url = u.findall(html) > for item in url: > namespace['url'] = item > g = re.compile(r"""GLatLng\((\-?\d+\.\d*)\,\\n\s*(\-?\d+\.\d*)\)""") > lat = g.findall(html) > for item in lat: > namespace['LatLng'] = item > block_list.append(namespace) > > ### > > can this be made better? The 3 regex patterns are constants: they can be put out of the loop. You may also rename b to blocks, and find a more a more accurate name for block_list; eg block_records, where record = set of (named) fields. A short desc and/or example of the overall and partial data formats can greatly help later review, since regex patterns alone are hard to decode. The def of "namespace" would be clearer imo in a single line: namespace = {title:t, url:url, lat:g} This also reveals a kind of name confusion, doesn't it? Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] parse text file
On Tue, 2 Feb 2010 22:56:22 +0100 Norman Khine wrote: > i am no expert, but there seems to be a bigger difference. > > with repr(), i get: > Sat\\xe9re Maw\\xe9 > > where as you get > > Sat\xc3\xa9re Maw\xc3\xa9 > > repr()'s > é == \\xe9 > whereas on your version > é == \xc3\xa9 This is a rather complicated issue mixing python str, unicode string, and their repr(). Kent is right in that the *python string* "\xc3\xa9" is the utf8 formatted representation of 'é' (2 bytes). While \xe9 is the *unicode code* for 'é', which should only appear in a unicode string. So: unicode.encode(u"\u00e9", "utf8") == "\xc3\xa9" or more simply: u"\u00e9".encode("utf8") == "\xc3\xa9" Conversely: unicode("\xc3\xa9", "utf8") == u"\u00e9" -- decoding The question is: what do you want to do with the result? You'll need either the utf8 form "\xc3\xa9" (for output) or the unicode string u"\u00e9" (for processing). But what you actually get is a kind of mix, actually the (python str) repr of a unicode string. > also, i still get an empty list when i run the code as suggested. ? Strange. Have you checked the re.DOTALL? (else regex patterns stop matching at \n by default) Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help with random.randint
On Wed, 03 Feb 2010 11:21:56 +0800 David wrote: > Hello list, > > I thought this was easy even for me, but I was wrong, I guess. > Here is what I want to do: take two random numbers between 1 and 99, and > put them into a list. > > import random > terms = [] > for i in range(2): > terms = random.randint(1, 99) > print terms > > This prints just one number (the last one generated in the loop?) Yo, terms now refers to a simple integer. What you want is instead: for i in range(2): term = random.randint(1, 99) terms.append(term) # put new item at end of list print terms > So I tried to change line 4 to the following: > terms += random.randint(1, 99) This is equivalent to terms = terms + random.randint(1, 99) Because the first operand is a list, python won't do an addition (yes, the operator can be misleading, I would personly prefer eg '++' to avoid ambiguity), but a so-called "concatenation" (yes, that word is ugly ;-). This "glues" together 2 sequences (lists or strings). In this case, you get an error because the second operand is not a sequence. Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help with random.randint (cont. -- now: pseudo code)
On Wed, 03 Feb 2010 14:12:42 +0800 David wrote: > Hello Benno, list, > > thanks for those clarifications, which, well, clarify things ;-) > > This is my latest creation: > > import random > > def createTerms(): > terms = [] > for i in range(2): > terms.append(random.randint(1, 99)) > j = terms[0] > k = terms[1] > print "%3d\nx%2d" % (j, k) > > createTerms() I find j,k = terms clearer. (This will automagically unpack items from terms.) > Which works. However, it merely prints a multiplication task. Anyway, > this was just a prelude. In the end, I want far more, namely to create, > ask, and verify some multiplication questions. Here is my pseudo code of > this little project: > > > > pool = [] > correct = 0 > incorrect = 0 > > def createQuestions: > generate all multiplication combinations possible > append as tuple to pool > eliminate 'mirrored doubles' (i.e. 7x12 and 12x7) Unneeded. Instead building all combinations looping with j=1..n & k=1..n, directly avoid duplicates using j=1..n & k=j..n. (not 100% sure, though) > randomize pool > > def askQuestions: > for question in pool: > calculate solution > take answer from user > if user answer == solution: > correct += 1 > remove question from pool > else: > incorrect += 1 > > def showStats: > print number of questions asked > print number of questions answered correctly > print percentage of correct answers > > def askFaultyAnswers: > answer = raw_input("Try again the incorrect questions? (y/n) ") > if answer == "y": > aksQuestions() > else: > break > > > createQuestions() > askQuestions() > showStats() > askFaultyAnswers() > print "good-bye!" > > > > I think it is sensible to > > * first create all possible solutions, then > * kick out doublettes, and only then > * ask questions > > I have some questions though: > > Is the overall structure and flow of this program okay? What are the > main problems you can spot immediately? > > In the very end I would like to take this code as a basis for a wxPython > program. Are there any structural requirements I am violating here? You should from start on organize your code into funcs that will be so-called "callback functions", meaning functions that will be called by user actions (typically button press). Hope it's clear. This does not prevent an OO structure of the code, indeed, the funcs can well be object methods if this matches the problem. > If I want to limit the number of questions asked, say to 20, would I > operate with slicing methods on the randomised pool? ? > How would you go about showing stats for the second run (i.e. the > FaultyAnswers)? Right now I am thinking of setting the global variables > correct and incorrect to 0 from _within_ askFaultyAnswers; then I would > run showStats() also from _within_ askFaultyAnswers. Good idea? I would have a kind of overall "UserTest" object with methods generating and storing test data (numbers), asking questions and getting answers, recording and outputing results. This is an OO point of view. You may organise things differently (but even then an OO pov sometimes helps structuring a pb). Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] language aid
On Thu, 4 Feb 2010 07:39:29 -0800 (PST) Albert-Jan Roskam wrote: > Hi, > > A dictionary (associative array of keys and values) seems a good datatype to > use. > vocab = {} > vocab[frenchword] = englishword > > For instance: > >>> vocab = {"aimer": "love"} > >>> vocab > {'aimer': 'love'} > >>> vocab["parler"] = "speak" > >>> vocab > {'aimer': 'love', 'parler': 'speak'} > >>> for engword, frword in vocab.iteritems(): > print "%s - %s" % (engword, frword) > aimer - love > parler - speak > > But if one word has different meanings in the other language, you may need to > use a list of words as the values. > > Cheers!! > Albert-Jan Sure, a dict is the obvious choice. For saving into file, if the app is to be used internally, you can even print it in the form of a python dict (with the '{}', ':' & ',') so that reading the dict data is just importing: import french_english Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] language aid
On Thu, 04 Feb 2010 16:11:33 + Owain Clarke wrote: > But if one word has different meanings in the other language, you may need to > use a list of words as the values. ? You can have a more sophisticated structure for you dict. For instance, "love" is both a noun and a verb, and each has several acception. The value for each word may be structured to reflect this possibility. Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] correcting an Active State Recipe for conversion to ordinal
On Thu, 4 Feb 2010 12:11:24 -0500 Serdar Tumgoren wrote: > Hi folks, > > A few months back I posted my first (and only) "recipe" to > ActiveState. It was just a little function to convert an integer or > its string representation to an ordinal value: 1 to 1st, 2 to 2nd, > etc. > > Not sure if this really qualifies as a recipe, per se, but it was a > handy little function that I needed but couldn't find in Pythonic > version elsewhere on the web (perhaps because it's so simple?). The > inspiration for the function was a similar one in Django and some Java > code I found online. So I figured I'd share the code once I got it > working. > > I just noticed, however, that in the comments section of the > ActiveState recipe that someone is getting incorrect results for > certain numbers (11 and 12, specifically). > > But when I use the code on my own machine it still works fine. So I > was hoping that you all could help me "crowdsource" the issue. If you > have the time and inclination, could you look at the code and tell me > if and where I've gone wrong? And of course, if there's a simpler way > to perform the conversion I'd be glad to update the recipe. I > certainly don't want something out in the wilds of the Web that's > incorrect, inelegant or just plain confusing. > > Here's the link to the recipe: > > http://code.activestate.com/recipes/576888/ > > Your advice, as always, is appreciated. > > Regards, > Serdar No time to search for the issue, but here are some trials (hole from 10 --> 19): for i in range(21): print "%s\t: %s" %(i,ordinal(i)) for i in (-1,22,33,99,100,101,199,200,999,1000): print "%s\t: %s" %(i,ordinal(i)) ==> 0 : 0th 1 : 1st 2 : 2nd 3 : 3rd 4 : 4th 5 : 5th 6 : 6th 7 : 7th 8 : 8th 9 : 9th 10 : None 11 : None 12 : None 13 : None 14 : None 15 : None 16 : None 17 : None 18 : None 19 : None 20 : 20th -1 : -1th 22 : 22nd 33 : 33rd 99 : 99th 100 : 100th 101 : 101st 102 : 102nd 103 : 103rd 199 : 199th 200 : 200th 999 : 999th 1000: 1000th Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] language aid
On Thu, 04 Feb 2010 16:11:33 + Owain Clarke wrote: > But if one word has different meanings in the other language, you may need to > use a list of words as the values. ? You can have a more sophisticated structure for you dict. For instance, "love" is both a noun and a verb, and each has several acception. The value for each word may be structured to reflect this possibility. Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] language aid (various)
On Fri, 05 Feb 2010 11:29:30 + Owain Clarke wrote: > On Thu, Feb 4, 2010 at 5:43 AM, Owain Clarke wrote: > > > >> My question is, that if I proceed like this I will end up with a single > >> list > >> of potentially several hundred strings of the form "frword:engword". In > >> terms of performance, is this a reasonable way to do it, or will the > >> program > >> increasingly slow down? > >> > > From: "Carnell, James E" > > > >> A dictionary (associative array of keys and values) seems a good > >> datatype to use. vocab = {} vocab[frenchword]?= englishword > >> ? > >> > > ... > > > >> Cheers!! > >> Albert-Jan > >> > > > > Sure, a dict is the obvious choice. For saving into file, if the app is > > to be used internally, you can even print it in the form of a python > > dict (with the '{}', ':' & ',') so that reading the dict data is just > > importing: > > import french_english > > > > Denis > > > > I 3rd the dictionary choice. They (for me at least) aren't as clean on > > the computer screen as arrays, but once you get good at it you can even > > have multiple definitions and weights for how relevant that word is. You > > (in the future when you get comfortable with dictionaries) can take it > > into networkx or something and draw pictures of it, and really start > > messing around with it (using subnetworks to try and get context > > information). Google has some tech talks on youtube concerning Language > > Processing using networks etc if that kind of thing interests you. > > > > Sincerely, > > > > Bad answer man > > > What a helpful forum - much thanks to all who've commented. Seems to be > a bit of a consensus here about dictionaries. Let me just restate my > reluctance, using examples from Spanish. > > esperar = to hope > esperar = to wait > tambien = too [i.e. also] > demasiado = too [i.e. excessive] > > So there are repeats in both languages. I would like to end up with a > file which I can use to generate flash cards, either to or from English, > and I suppose I want the flexibility to have 1 word with 1 definition. > > Having said that, I obviously recognise the expertise of the group, so I > will probably pursue this option. > > Owain If you store simple one-to-one pairs, then it will be both more difficult and less efficient to retrieve several matching forms. I guess you'd better store in a dict all possible matches for each single form, eg {..., "esperar":["to hope", "to wait", ...} The issue is how to output that in a clear and practicle format. But this point is about the same whatever your choice about storing (and retrieving) information. The cleanest way to cope with this may be to subtype dict and establish a custom output format using the __str__ "magic" method. You can also use __repr__ for development feedback. Also, your application can be much more complex than it seems at first sight. Look at the hierarchy of acceptions in a paper dict: from word nature to style variant. I would reproduce that in my storage and output formats. Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] "else:" triggering a Syntax Error
On Sat, 6 Feb 2010 20:18:53 +0900 Benno Lang wrote: > > if ((self.start_dot.x > self.end_dot.x) and (self.start_dot.y != > > self.end_dot.y)): > (1) Are you sure you want all those superfluous parentheses? @ Benno: I do not find the _inner_ ones superfluous at all... @ Stijn: When you ask about an error, please copy-paste the (whole) error message to the list, together with (the relevant part of) the code. I will help us not only "diagnosing" the issue, but also showing you how to interpret python error messages. Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] why inline-only string literals?
Hello, I recently wrote a parser for a (game scripting) language in which ordinary strings inside quotes can hold (literal) newlines: s = "Hello, how do you do?" So, I wonder why most languages do not allow that from scratch; and some, like python, need a special syntax for multi-line strings. This is no parsing issue: instead, the pattern is simpler for it does need to refuse newlines! Note: Like python, this languages takes end-of-line (outside quotes) as end-of-statement token. Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Variable: From input and output
On Fri, 5 Feb 2010 14:14:49 -0800 (PST) ssiverling wrote: > > Greetings, > > Thanks, and I will try and not to toppost. You just did it right now ;-) Also, a good practice is to cut out irrelevant parts of previous massage(s) in thread. Both work together to make threads fast/easy/pleasant to follow. Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why inline-only string literals?
[sorry, Steve, first replied to sender instead of list] On Sun, 7 Feb 2010 09:54:12 -0800 Steve Willoughby wrote: > I believe it's a deliberate design decision, [...] > So by making you explicitly state when you wanted multi-line strings, > it makes it easier to spot this common mistake as well as making > your intent more clear when just looking at the code. Thank you. I guess this really makes sense. Or rather it did make sense at the time of python design. Nowadays most editors (even not programming editors) are able to _very_ clearly show such errors (when I add a quote, the whole rest of the code becomes a string ;-). It seems such a change would be backwards-compatible, no? I thought there may be a (for me) hidden issue due to lexer+parser separation. My parser was PEG-based, so with a single grammar and a single pass. I'm not used to reason about lexers and token strings. But since python does have multi-line strings, anyway... Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string to list
On Wed, 10 Feb 2010 11:26:25 + Owain Clarke wrote: > Please excuse the obviousness of my question (if it is), but I have > searched the documentation for how to generate a list e.g. [(1,2), > (3,4)] from a string "[(1,2), (3,4)]". I wonder if someone could point > me in the right direction. Use eval. Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15) [GCC 4.4.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print eval("[(1,2), (3,4)]") [(1, 2), (3, 4)] But -1- This need problem shows a design issue, unless you try this only for exploring python. -2- One should avoid doing this and using eval() in other circumstances because of security issues: if the code-string comes from external source (data file, network...), there is no way to ensure what it actually does. In a general case, one should _parse_ the text (which probably would not be a python literal, anyway) to _construct_ resulting python data. Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple Stats on Apache Logs
On Thu, 11 Feb 2010 09:56:51 + Lao Mao wrote: > Hi, > > I have 3 servers which generate about 2G of webserver logfiles in a day. > These are available on my machine over NFS. > > I would like to draw up some stats which shows, for a given keyword, how > many times it appears in the logs, per hour, over the previous week. > > So the behavior might be: > > $ ./webstats --keyword downloader > > Which would read from the logs (which it has access to) and produce > something like: > > Monday: > : 12 > 0100: 17 > > etc > > I'm not sure how best to get started. My initial idea would be to filter > the logs first, pulling out the lines with matching keywords, then check the > timestamp - maybe incrementing a dictionary if the logfile was within a > certain time? > > I'm not looking for people to write it for me, but I'd appreciate some > guidance as the the approach and algorithm. Also what the simplest > presentation model would be. Or even if it would make sense to stick it in > a database! I'll post back my progress. As your logfile in rather big, I would iterate line per line using "for line in file". Check each line to determine whether (1) it has changed hour (2) it holds the given keyword. For the presentation, it depends on your expectation! Also, are keywords constants (= predefined at coding time)? You may think at a python (nested) dict: keywordStats = {} ... keywordStats[one_keyword] = { Monday: [12, 17, ...] , ... } (hours in 24 format (actually 0..23) are implicit keys of the lists) This lets open the opportunity to read the info back into the machine... Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
tutor@python.org
On Sat, 13 Feb 2010 13:58:34 -0500 David Abbott wrote: > I am attempting to understand this little program that converts a > network byte order 32-bit integer to a dotted quad ip address. > > #!/usr/bin/python > # Filename : int2ip.py > > MAX_IP = 0xL > ip = 2130706433 > > def int2ip(l): > if MAX_IP < l < 0: > raise TypeError, "expected int between 0 and %d inclusive" % > MAX_IP > return '%d.%d.%d.%d' % (l>>24 & 255, l>>16 & 255, l>>8 & 255, l & > 255) > > result = int2ip(ip) > print result > > I don't understand the l>>24 & 255. > > from the docs; > Right Shift a >> b rshift(a, b) > Bitwise And a & b and_(a, b) > > thanks > In addition to Steve's excellent explanation: Shifting to the left n bits is equivalent to multiplying by 2^n. Shifting to the right n bits is equivalent to dividing by 2^n. Shifting to the right 8 bits is thus equivalent to dividing by 2^8=256; which means making each octet (in a long integer) one level less significant. AND-ing is equivalent to masking (this is actually called a mask operating) all bits wich are not 1 in the mask. So AND-ing with =255 masks all bits except the ones of the least significant octet. If you represent a 32-bit integer as 4 octets, or 8 hex digits, the picture is easier to catch: n = 0x12345678 # 0x12345678 = abcd (b=0x34=52) # to get b: temp = n >> 16 # 0x1234 = 00ab b = temp & 255 # 0x0034 = 000b You can perform the same operation without bit-level operators, using modulo and integer division. To understand this, again consider the "abcd" representation: this means a 32-bit integer can be seen as 4-digit number written in base 256 (yes!). Division by 256^n gets rid of n lowest digits, moving down all other ones. Modulo 256^n gets rid of digits in higher position than n. (This may help and fully catch the sense of "base n"). n = 0x12345678 # 0x12345678 = abcd (b=0x34) # to get b: temp = n // (256**2)# 0x1234 = 00ab b = temp % 256 # 0x0034 = 000b (examples untested) Bit operations are by far faster, indeed. Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
tutor@python.org
On Sat, 13 Feb 2010 23:17:27 +0100 spir wrote: > On Sat, 13 Feb 2010 13:58:34 -0500 > David Abbott wrote: > > > I am attempting to understand this little program that converts a > > network byte order 32-bit integer to a dotted quad ip address. > > > > #!/usr/bin/python > > # Filename : int2ip.py > > > > MAX_IP = 0xL > > ip = 2130706433 > > > > def int2ip(l): > > if MAX_IP < l < 0: > > raise TypeError, "expected int between 0 and %d inclusive" % > > MAX_IP > > return '%d.%d.%d.%d' % (l>>24 & 255, l>>16 & 255, l>>8 & 255, l & > > 255) PS: in "l>>24 & 255", the & operation is useless, since all 24 higher bits are already thrown away by the shift: >>> 0x12345678 >> 24 18 # = 0x12 Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] file exception & behaviour (was: Getting caller name...)
On Sun, 14 Feb 2010 12:33:09 +0100 patrice laporte wrote: > 2010/2/13 Luke Paireepinart > > > > > > > On Sat, Feb 13, 2010 at 9:56 AM, patrice laporte > > wrote: > > > >> > >> Hi, > >> > >> Being in an exeption of my own, I want to print the name of the caller, > >> and I'm looking for a way to have it. > >> > >> Could you tell us exactly why you want to do this? It seems sort of > > strange. Also couldn't you pass the caller as an argument to your function? > > > > > Hi, > > I don't know if it's strange, maybe, so tell me why. Or maybe it's me > Maybe the fact I'm à pure C coder for a longtime prevent me from thinking in > Python, maybe my brain is only capable of thinking and viewing in C. and > maybe there's an obvious way to solve my problem that I can't see yet... or > maybe it's because I'm French, genetically programed to live in the past ? > ... so this mailing list is my starting point to improve my Python > understanding. > > And now, something different : what I want to do, and why. > > I got a class that takes a file name in its __init__ method (it could be > elsewhere, but why not here ?). Then, somewhere in that class, a method will > do something with that file name, such as "try to open that file". > > If the file do esn't exist, bing ! I got an exception "I/O Error n°2 : file > doesn't exist". > > That's nice, I of course catch this exception, but it's not enough for the > user : What file are we talking about ? And how to tell the user what is > that file, and make him understand he tell the app to use a file that > doesn't exist ? > And this is not enough for developer : where that error happened ? what > class ? what method ? > > There is many solution, and mine is : > > - don't check for the existence of the file in __init__ > - don't use a default value for the file name parameter in __init__ : coder > MUST provide it. > - try/catch open(this_file_name) and if I got an IOErro exception, I > re-raise my own exception with : >- the name of the class where that IOError occured >- the name of the method in that class that make that error occured >- the name of the file the method tried to opened >- some other info to help diagnostic > > Then my exception object can : >- log in a file dedicated to the team (so, just me for the moment) >- and/or log in a file dedicated to the user > > And then the app can popup a message box or something like that. > > Currently, I'm thinking about "how to get that class/method name", easily, > and make something usefull with that. > > I don't want to write the name of the method and the class each time I need > to use my exception class, and I don't want to modify that name each time > I'm doing a refactoring operation : I just want to do something like : > >except IOError, (errno, errmes): > raise myexceptioniwanttobenice ("IO Error %d -> %s ('%s')" % > (errno, errmes, self.__csv) ) > > And then I print somewhere a message like : > > Error in csv2db.getColumnFromCsv : IO Error 2 -> No such file or > directory ('toto') > > What I don't want is to write something like : > > except IOError, (errno, errmes): > raise myexceptioniwanttobenice ("cvs2db.getColumnFromCsv IO > Error %d -> %s ('%s')" % (errno, errmes, self.__csv) ) > > I don't want because it's to much to write, and if I change the class name, > I don't want to rewrite all my call to change that name. > > So, I was looking for a way to retrieve : > The name of the class the object that throw the exception belongs to > The name of the method in that class > > And I finally found those recipe with the sys._getframe call. > > My first solution, currently, is (I didn't yet rewrite it with the solution > with the traceback module) : > > class argError(Exception): > def __init__(self, callerClass, errorMessage): > self.__class = callerClass.__class__.__name__ > self.__method = sys._getframe(1).f_code.co_name > self.__message = errorMessage > > def __str__(self): > return "Error in %s.%s : %s" % (self.__class, self.__method, > self.__message) > > And how I use it : > >try: > f = open(self.__csv) > except IOError, (errno, errmes): > raise argError (self, "IO Error %d -> %s ('%s')" % (errno, > errmes, self.__csv) ) > > I now will : > - rewrite it with the solution with traceback module > - read about that module and find how to get rid of the need to pass "self" > as an argument in the call "raise argError (*self*, "IO Error %d -> %s > ('%s')" % (errno, errmes, self.__csv) )". That call enable me to get the > name of the class. > > All of that is a way to me to experiment Python, I try a lot of thing to > understand, and then I will make a choice. > > But now, it's time to lunch. Did I gave you enough information about what I > want ? > > Patrice First, here is a kind of implementation of your specification. I just wr
tutor@python.org
Just reviewed this post and wonder whether the explanation is clear for anyone else as myself ;-) Denis On Sat, 13 Feb 2010 23:17:27 +0100 spir wrote: > On Sat, 13 Feb 2010 13:58:34 -0500 > David Abbott wrote: > > > I am attempting to understand this little program that converts a > > network byte order 32-bit integer to a dotted quad ip address. > > > > #!/usr/bin/python > > # Filename : int2ip.py > > > > MAX_IP = 0xL > > ip = 2130706433 > > > > def int2ip(l): > > if MAX_IP < l < 0: > > raise TypeError, "expected int between 0 and %d inclusive" % > > MAX_IP > > return '%d.%d.%d.%d' % (l>>24 & 255, l>>16 & 255, l>>8 & 255, l & > > 255) > > > > result = int2ip(ip) > > print result > > > > I don't understand the l>>24 & 255. > > > > from the docs; > > Right Shift a >> b rshift(a, b) > > Bitwise And a & b and_(a, b) > > > > thanks > > > > In addition to Steve's excellent explanation: > Shifting to the left n bits is equivalent to multiplying by 2^n. Shifting to > the right n bits is equivalent to dividing by 2^n. Shifting to the right 8 > bits is thus equivalent to dividing by 2^8=256; which means making each octet > (in a long integer) one level less significant. > AND-ing is equivalent to masking (this is actually called a mask operating) > all bits wich are not 1 in the mask. So AND-ing with =255 masks all > bits except the ones of the least significant octet. > If you represent a 32-bit integer as 4 octets, or 8 hex digits, the picture > is easier to catch: > n = 0x12345678# 0x12345678 = abcd (b=0x34=52) > # to get b: > temp = n >> 16# 0x1234 = 00ab > b = temp & 255# 0x0034 = 000b > > You can perform the same operation without bit-level operators, using modulo > and integer division. To understand this, again consider the "abcd" > representation: this means a 32-bit integer can be seen as 4-digit number > written in base 256 (yes!). Division by 256^n gets rid of n lowest digits, > moving down all other ones. Modulo 256^n gets rid of digits in higher > position than n. (This may help and fully catch the sense of "base n"). > n = 0x12345678# 0x12345678 = abcd (b=0x34) > # to get b: > temp = n // (256**2) # 0x1234 = 00ab > b = temp % 256# 0x0034 = 000b > > (examples untested) > Bit operations are by far faster, indeed. > > > Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
tutor@python.org
On Sun, 14 Feb 2010 09:16:18 - "Alan Gauld" wrote: > In the case in point the & 255 keeps the coding style consistent > and provides an extra measure of protection against unexpected > oddities so I would keep it in there. You're right on that, Kent. My comment was rather wrong. Especially for consistent code I would now keep it, too. Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] pure symbol -- __subtype__
Hello, I was lately implementing a kind of "pure symbol" type. What I call pure symbols is these kinds of constants that refer to pure "idea", so that they have no real obvious value. We usually _arbitrarily_ give them as value an int, a string, a boolean, an empty object: BLACK, WHITE = False, True GUARD_FLAG = object() N,S,W,E = 1,2,3,4 READ,WRITE = 'r','w' ... Such symbols, especially the ones we give boolean values, often match the use of C preprocessor flags: #define GUARD_FLAG ... #ifdef GUARD_FLAG ... When there is a set of symbols, they match Pascal enumerations: var direction: (N, S, W, E); They are often used as func parameters. f = open("foo.txt", WRITE) The latter case is so common that numerous (built-in or third-party) libraries define a whole load of "pure symbol" constant values to be used as func arguments: pattern = re.compile(format, re.MULTILINE) This is very heavy, indeed. But alternatives I can imagine are worse: * Use literal (unnamed) values: illegible. * Use unprefixed names: pollutes global namespace. I cannot find any good solution for this issue. This is my first question. These pure symbol are, I guess, a very close notion to the one of "nominals" (see http://en.wikipedia.org/wiki/Nominal_number). And in fact pascal enums are nominal types. So, I wrote this for isolated symbols: class Nominal(object): count = 0 def __init__(self, value=None): self.type = self.__class__ self.type.count += 1 # no need to restrict user-provided value, if any, to natural integer self.value = value if value is not None else self.type.count def __str__(self): typeName = self.type.__name__ return "%s:(%s)" %(typeName,self.value) x,y,z = Nominal(),Nominal(),Nominal() print x,y,z # Nominal:(1) Nominal:(2) Nominal:(3) The type here can only be Nominal; and the value is not really needed, indeed, but it can be useful in some cases. Especially, this type can be the base of derived Nominal types, i.e. pascal-like enums. Like in Pascal, making the instances comparable can be very handy: def __lt__(self, other): assert(isinstance(other, Nominal)) return self.value < other.value class CardSuite(Nominal): pass club,diamond,heart,spade = CardSuite(),CardSuite(),CardSuite(),CardSuite() print club,diamond,heart,spade # CardSuite:(4) CardSuite:(5) CardSuite:(6) CardSuite:(7) print(diamond < heart) # True An issue is that a subtupe should start with count==0. I could not find any way to do that, so I ended up writing a subtype factory. But this goes against the language, for the user cannot use anymore the dedicated idiom "class CardSuite(Nominal)". Also, the type's name has to be *stupidly* passed as argument. So, the content of the method itself clearly shows how artificial this solution is: @classmethod def subtype(cls, name): class DummyName(cls): pass DummyName.count = 0 DummyName.__name__ = name return X CardSuite = Nominal.subtype("CardSuite") club,diamond,heart,spade = CardSuite(),CardSuite(),CardSuite(),CardSuite() print club,diamond,heart,spade # CardSuite:(1) CardSuite:(2) CardSuite:(3) CardSuite:(4) print(diamond < heart) # True Actually, what I need is a kind of __subtype__ magic method that acts for subtyping the same way __init__ does for instanciation. Then, I could write: @staticmethod def __subtype__(subtype): subtype.count = 0 (Note that here I do not need a classmethod, as staticmethod is enough.) So, do you see any other solution? (I have probably overlooked some) And, what do you think of __subtype__? Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pure symbol -- __subtype__
PS: see also on the topic: http://en.wikipedia.org/wiki/Enumerated_type On Thu, 18 Feb 2010 10:13:33 +0100 spir wrote: > Hello, > > I was lately implementing a kind of "pure symbol" type. What I call pure > symbols is these kinds of constants that refer to pure "idea", so that they > have no real obvious value. We usually _arbitrarily_ give them as value an > int, a string, a boolean, an empty object: >BLACK, WHITE = False, True >GUARD_FLAG = object() >N,S,W,E = 1,2,3,4 >READ,WRITE = 'r','w' >... > > Such symbols, especially the ones we give boolean values, often match the use > of C preprocessor flags: >#define GUARD_FLAG >... >#ifdef GUARD_FLAG ... > When there is a set of symbols, they match Pascal enumerations: >var > direction: (N, S, W, E); > They are often used as func parameters. >f = open("foo.txt", WRITE) > The latter case is so common that numerous (built-in or third-party) > libraries define a whole load of "pure symbol" constant values to be used as > func arguments: >pattern = re.compile(format, re.MULTILINE) > > This is very heavy, indeed. But alternatives I can imagine are worse: > * Use literal (unnamed) values: illegible. > * Use unprefixed names: pollutes global namespace. > I cannot find any good solution for this issue. This is my first question. > > These pure symbol are, I guess, a very close notion to the one of "nominals" > (see http://en.wikipedia.org/wiki/Nominal_number). And in fact pascal enums > are nominal types. So, I wrote this for isolated symbols: > class Nominal(object): > count = 0 > def __init__(self, value=None): > self.type = self.__class__ > self.type.count += 1 > # no need to restrict user-provided value, if any, to natural integer > self.value = value if value is not None else self.type.count > def __str__(self): > typeName = self.type.__name__ > return "%s:(%s)" %(typeName,self.value) > x,y,z = Nominal(),Nominal(),Nominal() > print x,y,z # Nominal:(1) Nominal:(2) Nominal:(3) > > The type here can only be Nominal; and the value is not really needed, > indeed, but it can be useful in some cases. Especially, this type can be the > base of derived Nominal types, i.e. pascal-like enums. Like in Pascal, making > the instances comparable can be very handy: > def __lt__(self, other): > assert(isinstance(other, Nominal)) > return self.value < other.value > class CardSuite(Nominal): pass > club,diamond,heart,spade = CardSuite(),CardSuite(),CardSuite(),CardSuite() > print club,diamond,heart,spade# CardSuite:(4) CardSuite:(5) > CardSuite:(6) CardSuite:(7) > print(diamond < heart)# True > > An issue is that a subtupe should start with count==0. I could not find any > way to do that, so I ended up writing a subtype factory. But this goes > against the language, for the user cannot use anymore the dedicated idiom > "class CardSuite(Nominal)". Also, the type's name has to be *stupidly* passed > as argument. So, the content of the method itself clearly shows how > artificial this solution is: > @classmethod > def subtype(cls, name): > class DummyName(cls): pass > DummyName.count = 0 > DummyName.__name__ = name > return X > CardSuite = Nominal.subtype("CardSuite") > club,diamond,heart,spade = CardSuite(),CardSuite(),CardSuite(),CardSuite() > print club,diamond,heart,spade# CardSuite:(1) CardSuite:(2) > CardSuite:(3) CardSuite:(4) > print(diamond < heart)# True > > Actually, what I need is a kind of __subtype__ magic method that acts for > subtyping the same way __init__ does for instanciation. Then, I could write: > @staticmethod > def __subtype__(subtype): > subtype.count = 0 > (Note that here I do not need a classmethod, as staticmethod is enough.) > > So, do you see any other solution? (I have probably overlooked some) > And, what do you think of __subtype__? > > Denis > > > la vita e estrany > > http://spir.wikidot.com/ la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and algorithms
On Thu, 18 Feb 2010 09:11:22 -0500 Kent Johnson wrote: > It's true that solving a problem often involves creating an algorithm > in a broad sense. The formal study of algorithms studies specific > techniques and algorithms that have proven to be useful to solve many > hard problems. In my experience most programming problems do not > require use of these formal algorithms, at least not explicitly. Hello, I would say that what is commonly called "algorithm" in computer science is a given class of possible algorithm that can (more easily) be formally expressed. Especially in mathematical terms. The reason why these are much more studied. But algorithmics can also be more generally understood as the "art & technique" of software design. Then, every programming task involves algorithmics. This may also be called "modelizing", a term than imo sensibly suggests how similar it is to the job of scientists. Modelizing is hard and hard to study because close to infinitely various and complex. Improving one's skills in this field is a whole life's yoga ;-) "I want to get a clearer mind"; "I want to become more lucid". An extremely big, difficult and rich book on the topic of thinking complexity is "la méthode" by http://en.wikipedia.org/wiki/Edgar_Morin (I don't have references to the english version). Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Replacing part of a URL
On Sun, 21 Feb 2010 11:25:31 +1100 Steven D'Aprano wrote: > "Some people, when confronted with a problem, think 'I know, I'll use > regular expressions.' Now they have two problems." -- Jamie Zawinski ;-) la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python file escaping issue?
Just a little complement to Steven's excellent explanation: On Mon, 22 Feb 2010 10:01:06 +1100 Steven D'Aprano wrote: [...] > So if you write a pathname like this: > > >>> path = 'C:\datafile.txt' > >>> print path > C:\datafile.txt > >>> len(path) > 15 > > It *seems* to work, because \d is left as backlash-d. But then you do > this, and wonder why you can't open the file: I consider this misleading, since it can only confuse newcomers. Maybe "lonely" single backslashes (not forming a "code" with following character(s)) should be invalid. Meaning literal backslashes would always be doubled (in plain, non-raw, strings). What do you think? > But if the escape is not a special character: > > >>> s = 'abc\dz' # nothing special > >>> print s > abc\dz > >>> print repr(s) > 'abc\\dz' > >>> len(s) > 6 > > The double backslash is part of the *display* of the string, like the > quotation marks, and not part of the string itself. The string itself > only has a single backslash and no quote marks. This "display" is commonly called "representation", thus the name of the function repr(). It is a string representation *for the programmer* only, both on input and output: * to allow one writing, in code itself, string literal constants containing special characters, in a practical manner (eg file pathes/names) * to allow one checking the actual content of string values, at testing time The so-called interactive interpreter outputs representations by default. An extreme case: >>> s = "\\" >>> s '\\' >>> print s, len(s) \ 1 >>> print repr(s), len(repr(s)) '\\' 4 >>> The string holds 1 char; its representation (also a string, indeed) holds 4. > The best advice is to remember that Windows allows both forward and > backwards slashes as the path separator, and just write all your paths > using the forward slash: > > 'C:/directory/' > 'C:textfile.txt' Another solution is to take the habit to always escape '\' by doubling it. Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What Editori?
On Tue, 23 Feb 2010 17:46:24 +0100 Giorgio wrote: > Definitely i just use pyscripter because it has the py idle integrated in > the window. > > It's very useful! Most editors have an integreated console that allow typing commands, launching the interactice interpreter, and running progs all without quitting the editor. I use geany which is imo good for every language... and has the absolutely necessary duplicate feature ;-) (*). Its only drawback is syntax highlighting must be set by editing config files. Denis (*) Does anyone know another editor that has it? la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] common (sub)attributes
Hello, Just a really basic note: Classes are often used to hold common or default attribute. Then, some of these attrs may get custom values for individual objects of a given type. Simply by overriding the attr on this object (see code below). But this will not only if the attr is a top-level one; not if it is itself part of a composite object. In the latter case, the (sub)attribute is still shared, so the change affects everybody. So, we must redefine the whole top-level attr instead. Trivial, but not obvious for me ;-) === sample code === #!/usr/bin/env python # coding: utf-8 # case x is top-level attr class C(object): x = 0 y = 0 a = 'u' def __str__ (self) : return "C(%s,%s,%s)" %(self.x,self.y,self.a) c1 = C() ; c2 = C() c1.x = 1# change x for c1 only c1.a = 'v' # change a for c1 only print c1,c2 # ==> C(1,0,v) C(0,0,u) # case x is element of composite attr class P: x = 0 y = 0 class C(object): p = P() a = 'u' def __str__ (self) : return "C(%s,%s,%s)" %(self.p.x,self.p.y,self.a) c1 = C() ; c2 = C() c1.p.x = 1 # change x for c1, but x is actually shared c1.a = 'v' # change a for c1 only print c1,c2 # ==> C(1,0,v) C(1,0,u) Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Cannot open SQLite database
On Wed, 24 Feb 2010 09:38:56 +0100 Timo wrote: > Hello all, my program uses SQLite as database and everything worked fine > until now. > I have over 3000 downloads of my program, but I got 2 mails this week > from people who get this error: > > OperationalError: unable to open database file > > I searched the net, and all problems seem to be of a not writeable > directory or passing a tilde to connect to the database. > As far as I know, this isn't the case with my program. > Here is some code: > > > # Get the application data folder > # This will return: C:\Documents and Settings\user\Application Data\myapp > PREFDIR = os.path.join(os.environ['APPDATA'], 'myapp') > > # Connect to the database > def db_connect(): > conn = sqlite3.connect(os.path.join(PREFDIR, 'myapp.db')) # This > line gives the error > conn.text_factory = str > cursor = conn.cursor() > return (conn, cursor) I would first add debug lines print os.path.join(PREFDIR, 'myapp.db') print os.path.exists(os.path.join(PREFDIR, 'myapp.db')) print open(os.path.join(PREFDIR, 'myapp.db'), 'r') print open(os.path.join(PREFDIR, 'myapp.db'), 'a') conn = sqlite3.connect(os.path.join(PREFDIR, 'myapp.db')) to check name, existence, read access right, write access right, of the file. Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Top posters for 2009
On Thu, 25 Feb 2010 21:53:24 -0500 Kent Johnson wrote: > 2009 (7730 posts, 709 posters) > > Alan Gauld 969 (12.5%) > Kent Johnson 804 (10.4%) > Dave Angel 254 (3.3%) > spir 254 (3.3%) > Wayne Watson 222 (2.9%) I hope Dave is as thin as I am (*). Denis (*) for us to be able to climb on the same step of the podium ;-) -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Top posters for 2009
On Fri, 26 Feb 2010 09:27:04 +0530 Shashwat Anand wrote: > @Kent: thanks for the script. It is kool. > > Here is 2010 list of Top-20 (as of now): > > 2010 (1155 posts, 204 posters) > > Alan Gauld 127 (11.0%) > Kent Johnson 108 (9.4%) > spir 52 (4.5%) > Wayne Watson 46 (4.0%) > Luke Paireepinart 32 (2.8%) > Shashwat Anand 30 (2.6%) > Wayne Werner 29 (2.5%) > Steven D'Aprano 28 (2.4%) I think and hope Steven will fast climb up. His posts beeing so accurate and informative for me. Denis -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] parsing a "chunked" text file
On Mon, 1 Mar 2010 22:22:43 -0800 Andrew Fithian wrote: > Hi tutor, > > I have a large text file that has chunks of data like this: > > headerA n1 > line 1 > line 2 > ... > line n1 > headerB n2 > line 1 > line 2 > ... > line n2 > > Where each chunk is a header and the lines that follow it (up to the next > header). A header has the number of lines in the chunk as its second field. > > I would like to turn this file into a dictionary like: > dict = {'headerA':[line 1, line 2, ... , line n1], 'headerB':[line1, line 2, > ... , line n2]} > > Is there a way to do this with a dictionary comprehension or do I have to > iterate over the file with a "while 1" loop? The nice way would be to split the source into a list of chunk texts. But there seems to be no easy way to do this without traversing the source. If the source is generated, just add blank lines (so that the sep is '\n\n'). Then a dict comp can map items using any makeChunk() func. If this is not doable, I would traverse lines using a "while n < s" loop, where n is current line # & and s the size of lines. Denis -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why is the max size so low in this mail list?
On Wed, 03 Mar 2010 02:38:57 +1100 Lie Ryan wrote: > On 03/02/2010 04:13 AM, Wayne Watson wrote: > > See Subject. 40K here, but other Python lists allow for larger (total) > > sizes. > > I don't know, I've never realized it; that's an indication that the 40K > limit is reasonable, at least to me. A pocket book typically holds ~ 1000 chars per page. Even with a long thread accumulation (because people do not always cut off irrelevant parts), 40K is more than enough. This post holds 651 chars (spaces, signature & this comment all included). Denis -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Encoding
On Wed, 3 Mar 2010 16:32:01 +0100 Giorgio wrote: > Uff, encoding is a very painful thing in programming. For sure, but it's true for any kind of data, not only text :-) Think at music or images *formats*. The issue is a bit obscured for text but the use of the mysterious, _cryptic_ (!), word "encoding". When editing an image using a software tool, there is a live representation of the image in memory (say, a plain pixel 2D array), which is probably what the developper found most practicle for image processing. [text processing in python: unicode string type] When the job is finished, you can choose between various formats (png, gif, jpeg..) to save and or transfer it. [text: utf-8/16/32, iso-8859-*, ascii...]. Conversely, if you to edit an existing image, the software needs to convert back from the file format into its internal representation; the format need to be indicated in file, or by the user, or guessed. The only difference with text is that there is no builtin image or sound representation _type_ in python -- only because text and sound are domain specific data while text is needed everywhere. Denis -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Encoding
On Wed, 3 Mar 2010 20:44:51 +0100 Giorgio wrote: > Please let me post the third update O_o. You can forgot other 2, i'll put > them into this email. > > --- > >>> s = "ciao è ciao" > >>> print s > ciao è ciao > >>> s.encode('utf-8') > > Traceback (most recent call last): > File "", line 1, in > s.encode('utf-8') > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 5: > ordinal not in range(128) > --- > > I am getting more and more confused. What you enter on the terminal prompt is text, encoded in a format (ascii, latin*, utf*,...) that probably depends on your system locale. As this format is always a sequence of bytes, python stores it as a plain str: >>> s = "ciao è ciao" >>> s,type(s) ('ciao \xc3\xa8 ciao', ) My system is parametered in utf8. c3-a8 is the repr of 'é' in utf8. It needs 2 bytes because of the rules of utf8 itself. Right? To get a python unicode string, it must be decoded from its format, for me utf8: >>> u = s.decode("utf8") >>> u,type(u) (u'ciao \xe8 ciao', ) e8 is the unicode code for 'è' (decimal 232). You can check that in tables. It needs here one byte only because 232<255. [comparison with php] > Ok, now, the point is: you (and the manual) said that this line: > > s = u"giorgio è giorgio" > > will convert the string as unicode. Yes and no: it will convert it *into* a string, in the sense of a python representation for universal text. When seeing u"..." , python will automagically *decode* the part in "...", taking as source format the one you indicate in a pseudo-comment on top of you code file, eg: # coding: utf8 Else I guess the default is the system's locale format? Or ascii? Someone knows? So, in my case u"giorgio è giorgio" is equivalent to "giorgio è giorgio".decode("utf8"): >>> u1 = u"giorgio è giorgio" >>> u2 = "giorgio è giorgio".decode("utf8") >>> u1,u2 (u'giorgio \xe8 giorgio', u'giorgio \xe8 giorgio') >>> u1 == u2 True > But also said that the part between "" > will be encoded with my editor BEFORE getting encoded in unicode by python. will be encoded with my editor BEFORE getting encoded in unicode by python --> will be encoded *by* my editor BEFORE getting *decoded* *into* unicode by python > So please pay attention to this example: > > My editor is working in UTF8. I create this: > > c = "giorgio è giorgio" // This will be an UTF8 string because of the file's > encoding Right. > d = unicode(c) // This will be an unicode string > e = c.encode() // How will be encoded this string? If PY is working like PHP > this will be an utf8 string. Have you tried it? >>> c = "giorgio è giorgio" >>> d = unicode(c) Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 8: ordinal not in range(128) Now, tell us why! (the answer is below *) > Can you help me? > > Thankyou VERY much > > Giorgio Denis (*) You don't tell which format the source string is encoded in. By default, python uses ascii (I know, it's stupid) which max code is 127. So, 'é' is not accepted. Now, if I give a format, all works fine: >>> d = unicode(c,"utf8") >>> d u'giorgio \xe8 giorgio' Note: unicode(c,format) is an alias for c.decode(format): >>> c.decode("utf8") u'giorgio \xe8 giorgio' la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] object representation
Hello, In python like in most languages, I guess, objects (at least composite ones -- I don't know about ints, for instance -- someone knows?) are internally represented as associative arrays. Python associative arrays are dicts, which in turn are implemented as hash tables. Correct? Does this mean that the associative arrays representing objects are implemented like python dicts, thus hash tables? I was wondering about the question because I guess the constraints are quite different: * Dict keys are of any type, including heterogeneous (mixed). Object keys are names, ie a subset of strings. * Object keys are very stable, typically they hardly change; usually only values change. Dicts often are created empty and fed in a loop. * Objects are small mappings: entries are explicitely written in code (*). Dicts can be of any size, only limited by memory; they are often fed by computation. * In addition, dict keys can be variables, while object keys rarely are: they are literal constants (*). So, I guess the best implementations for objects and dicts may be quite different. i wonder about alternatives for objects, in particuliar trie and variants: http://en.wikipedia.org/wiki/Trie, because they are specialised for associative arrays which keys are strings. denis PS: Would someone point me to typical hash funcs for string keys, and the one used in python? (*) Except for rare cases using setattr(obj,k,v) or obj.__dict__[k]=v. -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] lazy? vs not lazy? and yielding
On Wed, 03 Mar 2010 15:41:34 -0500 Dave Angel wrote: > John wrote: > > Hi, > > > > I just read a few pages of tutorial on list comprehenion and generator > > expression. From what I gather the difference is "[ ]" and "( )" at the > > ends, better memory usage and the something the tutorial labeled as "lazy > > evaluation". So a generator 'yields'. But what is it yielding too? > > > > John > > > > > A list comprehension builds a whole list at one time. So if the list > needed is large enough in size, it'll never finish, and besides, you'll > run out of memory and crash. A generator expression builds a function > instead which *acts* like a list, but actually doesn't build the values > till you ask for them. To append on Dave's explanation, let us a take special case: when the sequence is potentially infinite. Imagine you want a function (actually here a generator) to yield the sequence of squares of natural without any limit. The stop point will happen in the code using the generator. You could write it eg like that (this is just sample code): def squares(): squares.n = min if min while True: squares.n += 1 n = squares.n yield n*n squares.n = 0 Then, you can use it for instance to get the first 9 squares that happen to be multiples of 3. nineTripleSquares = [] for sq in squares(): if sq%3 == 0: nineTripleSquares.append(sq) if len(nineTripleSquares) == 9: break print nineTripleSquares # ==> [9, 36, 81, 144, 225, 324, 441, 576, 729] A list can hardly be used here, instead of a generator, because we do not know how long the list should be so that we get 9 final items. We can modify squares to give it borders: def squares(min=None, max=None): squares.n = min-1 if (min is not None) else 0 squares.max = max while True: squares.n += 1 n = squares.n if (squares.max is not None) and (n > squares.max): raise StopIteration yield n*n squares.n = 0 tripleSquares = [] for sq in squares(7,27): if sq%3 == 0: tripleSquares.append(sq) print tripleSquares # [81, 144, 225, 324, 441, 576, 729] A more object-oriented vaersion would look like: class Squares(object): def __init__(self, min, max): self.n = min-1 if (min is not None) else 0 self.max = max def next(self): self.n += 1 n = self.n if (self.max is not None) and (n > self.max): raise StopIteration return n*n def __iter__(self): return self tripleSquares = [] for sq in Squares(7,27): if sq%3 == 0: tripleSquares.append(sq) print tripleSquares # ==> same result This defines a new type of "iterable" objects. When you use "for item in obj" on such object, python looks for the magic method __iter__ to find its iterator: here, itself. Then it will repetedly call the iterator's next method to get each item. Denis -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Encoding
On Thu, 4 Mar 2010 15:13:44 +0100 Giorgio wrote: > Thankyou. > > You have clarificated many things in those emails. Due to high numbers of > messages i won't quote everything. > > So, as i can clearly understand reading last spir's post, python gets > strings encoded by my editor and to convert them to unicode i need to > specify HOW they're encoded. This makes clear this example: > > c = "giorgio è giorgio" > d = c.decode("utf8") > > I create an utf8 string, and to convert it into unicode i need to tell > python that the string IS utf8. > > Just don't understand why in my Windows XP computer in Python IDLE doesn't > work: > > >>> RESTART > > >>> > >>> c = "giorgio è giorgio" > >>> c > 'giorgio \xe8 giorgio' > >>> d = c.decode("utf8") > > Traceback (most recent call last): > File "", line 1, in > d = c.decode("utf8") > File "C:\Python26\lib\encodings\utf_8.py", line 16, in decode > return codecs.utf_8_decode(input, errors, True) > UnicodeDecodeError: 'utf8' codec can't decode bytes in position 8-10: > invalid data > >>> How do you know your win XP terminal is configured to deal with text using utf8? Why do you think it should? Don't know much about windows, but I've read they have their own character sets (and format?). So, probably, if you haven't personalized it, it won't. (Conversely, I guess Macs use utf8 as default. Someone confirms?) In other words, c is not a piece of text in utf8. > In IDLE options i've set encoding to UTF8 of course. I also have some linux > servers where i can try the IDLE but Putty doesn't seem to support UTF8. > > But, let's continue: > > In that example i've specified UTF8 in the decode method. If i hadn't set it > python would have taken the one i specified in the second line of the file, > right? > > As last point, i can't understand why this works: > > >>> a = u"giorgio è giorgio" > >>> a > u'giorgio \xe8 giorgio' This trial uses the default format of your system. It does the same as a = "giorgio è giorgio".encode(default_format) It's a shorcut for ustring *literals* (constants), directly expressed by the programmer. In source code, it would use the format specified on top of the file. > And this one doesn't: > > >>> a = "giorgio è giorgio" > >>> b = unicode(a) > > Traceback (most recent call last): > File "", line 1, in > b = unicode(a) > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 8: > ordinal not in range(128) This trial uses ascii because you give no format (yes, it can be seen as a flaw). It does the same as a = "giorgio è giorgio".encode("ascii") > >>> > > The second doesn't work because i have not told python how the string was > encoded. But in the first too i haven't specified the encoding O_O. > > Thankyou again for your help. > > Giorgio Denis -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] lazy? vs not lazy? and yielding
On Thu, 4 Mar 2010 21:57:18 +0100 Andreas Kostyrka wrote: I would rather write it: > x_it = iter(x) # get an iterator for x > try: > while True: >i = x_it.next() >print i > except StopIteration: > pass x_it = iter(x) # get an iterator for x while True: try: i = x_it.next() except StopIteration: break else: print i (The trial is about getting each new element. The exception breaks the loop. But maybe it's only me.) Denis -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Instantiating a list of strings into a list of classes
On Fri, 5 Mar 2010 17:38:08 -0800 Daryl V wrote: > I have a csv list of data, of the form: > plot, utmN83_X, utmN83_Y, plot_radius_m > Spring1,348545,3589235,13.2 > etc. [...] > What I want to do is use the first entry in that row (row[0]) as the > variable name for the instantiated class. There are several solution, for design & implementation. (1) You can do some trick to create vars as you explain. But it's ugly and in my opinion wrong: because the set of data build a kind of whole, thus should have global name, say "data". (2) Build a dictionary which keys are the names: name = row[0] data[name] = value ... v = data[name] This is much better because its standard programming and the data are properly packed. Right? (3) Build a custom composite object which attributes are named the way you want. Python does not have a syntax to set attributes with variable names, but provides a builtin func for this: name = row[0] setattr(data, name, value) ... v = data.name It may look a bit contorsed, but again it's because python does not have a syntax for this. I would go for the latter -- it may be a question of style. Denis -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] recursive generator
Hello, Is it possible at all to have a recursive generator? I think at a iterator for a recursive data structure (here, a trie). The following code does not work: it only yields a single value. Like if child.__iter__() were never called. def __iter__(self): ''' Iteration on (key,value) pairs. ''' print '*', if self.holdsEntry: yield (self.key,self.value) for child in self.children: print "<", child.__iter__() print ">", raise StopIteration With the debug prints in code above, "for e in t: print e" outputs: * ('', 0) < > < > < > < > < > < > < > < > print len(t.children) # --> 9 Why is no child.__iter__() executed at all? I imagine this can be caused by the special feature of a generator recording current execution point. (But then, is it at all possible to have a call in a generator? Or does the issue only appear whan the callee is a generator itself?) Else, the must be an obvious error in my code. Denis -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Encoding
On Sun, 7 Mar 2010 13:23:12 +0100 Giorgio wrote: > One more question: Amazon SimpleDB only accepts UTF8. [...] > filestream = file.read() > filetoput = filestream.encode('utf-8') No! What is the content of the file? Do you think it can be a pure python representation of a unicode text? uContent = inFile.read().decode(***format***) outFile.write(uContent.encode('utf-8')) input -->decode--> process -->encode--> output This gives me an idea: when working with unicode, it would be cool to have an optional format parameter for file.read() and write. So, the above would be: uContent = inFile.read(***format***) outFile.write(uContent, 'utf-8') Or, maybe even better, the format could be given as third parameter of file open(); then any read or write operation would directly convert from/to the said format. What do you all think? denis -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] __iter__: one obvious way to do it
Hello, Below 6 working way to implement __iter__ for a container here simulated with a plain inner list. Sure, the example is a bit artificial ;-) 1. __iter__ returns a generator _expression_ 2. __iter__ *is* a generator 3. __iter__ returns a generator (this one is a bit weird, i guess) 4. __iter__ returns self, its own iterator via next() 5. __iter__ returns an external iterator object 6. __iter__ returns iter() of a collection built just on time (this one is really contrived) Also, one can always traverse the collection (already existing or built then) itself if it not quasi-infinite (no __iter__ at all). "There should be one-- and preferably only one --obvious way to do it" http://www.python.org/dev/peps/pep-0020/ I understand some ways fit given use cases better -- or worse. But it's difficult to find the best one, or even a proper one in a given case. Also, generation and iteration are rather abstract notions. And it's too much variety for me. I am lost in this field. I would enjoy a commented and examplified overview. Denis -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] __iter__: one obvious way to do it
[sorry, forgot the code] Hello, Below 6 working way to implement __iter__ for a container here simulated with a plain inner list. Sure, the example is a bit artificial ;-) 1. __iter__ returns a generator _expression_ 2. __iter__ *is* a generator 3. __iter__ returns a generator (this one is a bit weird, i guess) 4. __iter__ returns self, its own iterator via next() 5. __iter__ returns an external iterator object 6. __iter__ returns iter() of a collection built just on time (this one is really contrived) Also, one can always traverse the collection (already existing or built then) itself if it not quasi-infinite (no __iter__ at all). "There should be one-- and preferably only one --obvious way to do it" http://www.python.org/dev/peps/pep-0020/ I understand some ways fit given use cases better -- or worse. But it's difficult to find the best one, or even a proper one in a given case. Also, generation and iteration are rather abstract notions. And it's too much variety for me. I am lost in this field. I would enjoy a commented and examplified overview. Denis - class Seq(object): def __init__(self, *items): self.items = list(items) def next(self): if self.i < len(self.items): x = pair(self.items[self.i]) self.i += 1 return x raise StopIteration def pairs(l): for n in l.items: yield pair(n) raise StopIteration def __iter__(self): return (pair(n) for n in self.items) def __iter__(self): for n in self.items: yield pair(n) raise StopIteration def __iter__(self): return self.pairs() def __iter__(self): self.i=0 return self def __iter__(self): return Iter(self) def __iter__(self): return iter(tuple([pair(n) for n in self.items])) def pair(n): return "%s:%s" %(n,chr(n+96)) class Iter(object): def __init__(self, l): self.i=0 self.items = l.items def __iter__(self): return self def next(self): if self.i < len(self.items): x = pair(self.items[self.i]) self.i += 1 return x raise StopIteration l = Seq(1,2,3) for x in l: print x,# ==> 1:a 2:b 3:c la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] recursive generator
On Sun, 7 Mar 2010 17:20:07 +0100 Hugo Arts wrote: > On Sun, Mar 7, 2010 at 1:58 PM, spir wrote: > > Hello, > > > > Is it possible at all to have a recursive generator? I think at a iterator > > for a recursive data structure (here, a trie). The following code does not > > work: it only yields a single value. Like if child.__iter__() were never > > called. > > > > def __iter__(self): > > ''' Iteration on (key,value) pairs. ''' > > print '*', > > if self.holdsEntry: > > yield (self.key,self.value) > > for child in self.children: > > print "<", > > child.__iter__() > > print ">", > > raise StopIteration > > > > With the debug prints in code above, "for e in t: print e" outputs: > > > > * ('', 0) > > < > < > < > < > < > < > < > < > > > > > print len(t.children) # --> 9 > > > > Why is no child.__iter__() executed at all? I imagine this can be caused by > > the special feature of a generator recording current execution point. (But > > then, is it at all possible to have a call in a generator? Or does the > > issue only appear whan the callee is a generator itself?) Else, the must be > > an obvious error in my code. > > > > > > Denis > > remember that child.__iter__ returns a generator object. Merely > calling the function won't do anything. To actually get elements from > a generator object, you need to call next() on the returned iterator, > or iterate over it in another way (e.g. a for loop). I would write > this method more or less like so: > > from itertools import chain > > def __iter__(self): > this_entry = [(self.key, self.value)] is self.holds_entry else [] > return chain(this_entry, *[iter(c) for c in self.children]) > > (Warning! untested! I'm pretty sure chain will work like this, but you > might have to do a little tweaking) @ Hugo & Steven Thank you very much. I'll study your proposals as soon as I can, then tell you about the results. In the meanwhile, I (recursively) constructed the collection of entries and returned iter(collection). [This works, indeed, but I also want do know how to properly build a recursive iterator.] Denis -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Passing nested structures to fcntl.ioctl
On Mon, 8 Mar 2010 11:57:35 +0530 (IST) "Noufal Ibrahim" wrote: > Hello everyone, > I have some code that's calling fcntl.ioctl. I need to pass a nested > structure as the 3rd argument of the function. Something like this > > typedef struct coordinates{ > int x; > int y; > } coordinates; > > typedef struct point{ > int amp; > coordinates* coord; > } point; > >How would I do this? I need to allocate a point structure and then a > coordinate structure and link them up together via the coord member of > point, fill them up with values and pass it to the fcntl.ioctl. The > actual IOCTL implementation will do the indirection to read out the > data. > >I can't use ctypes since I'm working on an embedded system with an ARM > core that doesn't have ctypes. The struct module doesn't seem to help > due to the indirection. > >Any ideas? I'm personally planning to write a small C extension that > creates the structure based on a specification in memory and then > passes that to the IOCTL call directly but that looks like some work. I > was also told to take a look at Cython which basically seems to be an > easy way of doing what I'm planning. Is there a quicker/easier way? > > Thanks. If your point is to implement a type (point) in C, for performance certainly, and reuse it from a dynamic language, then I'm not sure python is the best choice (*). Python programmers code in python e basta. C-python bindings are far from beeing simplistic. http://wiki.cython.org/WrappingCorCpp Denis (*) Lua instead is well-known for this kind of stuff; especially on particuliar hardware like ARM. Lua's own implementation is 100% plain ANSI C standard and Lua-C bindings are probably as straightforward as they can be. http://lua-users.org/wiki/BindingCodeToLua -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Printing time without "if" statement
On Mon, 8 Mar 2010 18:03:12 +1100 Steven D'Aprano wrote: > On Mon, 8 Mar 2010 03:38:49 pm Elisha Rosensweig wrote: > > Hi, > > > > I have an event-based simulator written in Python (of course). It > > takes a while to run, and I want to have messages printed every so > > often to the screen, indicating the simulation time that has passed. > > Currently, every event that occurs follows the following code > > snippet: > [...] > > This seems stupid to me - why should I have to check each round if > > the time has been passed? I was thinking that there might be a > > simpler way, using threading, but I have zero experience with > > threading, so need your help. The idea was to have a simple thread > > that waited X time (CPU cycles, say) and then read the "current_time" > > variable and printed it. > > > > Any simple (simpler?) solutions to this? > > That's a brilliant idea. I think I will steal it for my own code :) > > (Why didn't I think of it myself? Probably because I almost never use > threads...) > > Anyway, here's something that should help: > > > import time > import threading > > class Clock(threading.Thread): > def __init__(self, *args, **kwargs): > super(Clock, self).__init__(*args, **kwargs) > self.finished = False > def start(self): > print "Clock %s started at %s" % (self.getName(), time.ctime()) > super(Clock, self).start() > def run(self): > while 1: > if self.finished: > break > print "Clock %s still alive at %s" % ( > self.getName(), time.ctime()) > time.sleep(2) > print "Clock %s quit at %s" % (self.getName(), time.ctime()) > def quit(self): > print "Clock %s asked to quit at %s" % ( > self.getName(), time.ctime()) > self.finished = True > > def do_work(): > clock = Clock(name="clock-1") > clock.start() > # Start processing something hard. > for i in xrange(8): > print "Processing %d..." % i > # Simulate real work with a sleep. > time.sleep(0.75) > clock.quit() > > > > And in action: > > >>> do_work() > Clock clock-1 started at Mon Mar 8 17:40:42 2010 > Processing 0... > Clock clock-1 still alive at Mon Mar 8 17:40:43 2010 > Processing 1... > Processing 2... > Clock clock-1 still alive at Mon Mar 8 17:40:45 2010 > Processing 3... > Processing 4... > Processing 5... > Clock clock-1 still alive at Mon Mar 8 17:40:47 2010 > Processing 6... > Processing 7... > Clock clock-1 still alive at Mon Mar 8 17:40:49 2010 > Clock clock-1 asked to quit at Mon Mar 8 17:40:49 2010 > >>> Clock clock-1 quit at Mon Mar 8 17:40:51 2010 > > >>> > > > There's a bit of a display artifact in the interactive interpreter, when > the final quit message is printed: the interpreter doesn't notice it > needs to redraw the prompt. But other than that, it should be fine. Hello, really interesting, indeed. I also have no idea about threading in python. But as timer is concerned, I would prefere something like: import time import whatever_to_ensure_run_in_separate_thread class Timer(whatever_to_ensure_run_in_separate_thread): UNIT = 1/100# µs def __init__(self, delay, action, cyclic=False): self.delay = delay self.action = action self.cyclic = cyclic self.goon = True def run(self): if self.cyclic: while self.goon: self.cycle() else: self.cycle def cycle(self): self.wait() self.action() def wait(self): delay.sleep(self.delay * self.UNIT) Or even better, launch a signal in main thread -- which is supposed to be an event cycle. This is how things work in the field of automation: class Timer(whatever_to_ensure_run_in_separate_thread): UNIT = 1/100# µs def __init__(self, delay, signal, resetDelay=1, cyclic=False): self.delay = delay self.signal = signal self.resetDelay = resetDelay self.cyclic = cyclic self.goon = True def run(self): if self.cyclic: while self.goon: self.cycle() else: self.cycle def cycle(self): self.wait(self.delay) self.signal = True self.wait(self.resetDelay) self.signal = False def wait(self, delay): delay.sleep(delay * self.UNIT) # main cycle: t1 = Timer(...) ... if t1_signal: do_t1_action (Actually, in python, the signal may be the index of a Timer.signals boolean array, incremented with each new instance.) Denis -- la vita e estrany spir
[Tutor] variable inheritance
Hello, Say I have a Tree type that may be based on 2 kinds of Node-s. Both conceptually and practically, a tree itself is a node, namely the top/root one. But a tree also has some additional tree-level behaviour, which is independant of the kind of node. Say, the node type performs all the underlying tree mechanics. Thus the Tree type looks like: class Tree(_Node): def __init__(self, args): _Node.__init__(self) ... ... tree-level methods ... The issue is the actual kind of node is not only initialised through "_Node.__init__(self)": it must first feature as super class in "class Tree(Node)". (Otherwise I get an error about wrong type of self.) I tried to play with __new__ in Tree and Node types, but this error seems unavoidable. Maybe I did not do it right. I can indeed have __new__ return an instance of either Node type, but then for any reason it "forgets" it is also a tree (so that calling any tree method fails). I there a way to do this right? Workaround ideas: * Make the root node an attribute of tree: unsatisfying. * Create a common tree type and 2 specialised ones: class TreeN(Tree,NodeN) def __init__(self, args): _NodeN.__init__(self) Tree.__init__(self, args) # ... e basta ... Denis -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] matching words from a text to keys in a dictionary
On Mon, 8 Mar 2010 08:05:54 -0800 (PST) Karjer Jdfjdf wrote: > I want to compare words in a text to a dictionary with values attached to the > words. > > The dictionary looks like: > { word1: [1,2,3] word2: [2,3,4,a,b ] ... } And how does your source text look like? (we have half of the data) And please print (part of) your dict out (instead of providing erroneous data). > I'm trying to find a way to achieve this, but I'm having trouble getting > corrects results. > > If I do the def below, nothing is matched. > > def searchWord(text, dictionary): > text = text.split() > for word in text: > print word print word, word in dictionary # would be useful for debug > if word in dictionary: > value = dictionary[str(word)] word is already a string -- or should be ;-) > else: > value = None > return w What is w? This is your main issue, I guess... [...] Denis -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Making Regular Expressions readable
On Mon, 8 Mar 2010 16:12:35 + Stephen Nelson-Smith wrote: > Hi, > > I've written this today: > > #!/usr/bin/env python > import re > > pattern = > r'(?P^(-|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(, > [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})*){1}) > (?P(\S*)) (?P(\S*)) > (?P(\[[^\]]+\])) > (?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?) > (?P(\S*)) (?P(\S*)) > (?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?) > (?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)( > )?(?P(\"([^"\\]*(?:\\.[^"\\]*)*)\")?)' [...] > It works fine, but it looks pretty unreadable and unmaintainable to > anyone who hasn't spent all day writing regular expressions. ;-) > I remember reading about verbose regular expressions. Would these help? Yes, read the doc and tutorials about python regexes, you'll find some useful features. > How could I make the above more maintainable? Maybe you can be interested in the following (seems to me it definitely fits your case): I wrote a kind of "SuperPattern" ;-) extension that allows writing, testing and composing sub-regexes. I used {name} since this is illegal in a normal regex. Intended use was something like: import re ; Pattern = re.compile person = Pattern(r"""...""") email = Pattern(r"""...""") phone = Pattern(r"""...""") entry = SuperPattern(scope, r"""{person}:\s+{email}\s*--\s*{phone}""") Don't remember the details, but it was a very simple type to write (meta-using re, indeed). The only point is that the constructor must be passed the scope (typically locals(), else use globals() by default) where to find the sub-patterns. Each regex pattern in fact has a .pattern attr (not sure of the name) that actually holds its format string: so composing a superpattern is just replacing a subpattern's name by its format. > S. > Denis -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] for _ in
On Tue, 9 Mar 2010 11:25:19 +0800 Joson wrote: > if labels is None: labels = [OrderedSet() for _ in xrange(ndim)] > .. > > It's a library named "divisi". "OrderedSet" is a string set defined in this > lib. "ndim" is integer. What's the meaning of "for _ in" ? > > Joson [OrderedSet() for _ in xrange(n)] builds a list of ndim empty ordered sets. '_' is the index, but is unused; '_' is commonly used as required, but unused, variable name (not only in python). This is just a tricky python idiom, because there is no builtin syntax to do this. [OrderedSet()] * ndim would build a list with ndim times the *same* set, so that if one item is changed, all are changed. The above syntax instread creates *distinct* sets. Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15) [GCC 4.4.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> lists = [list()] *3 >>> lists [[], [], []] >>> list1 = lists[1] >>> list1.append('*') >>> lists [['*'], ['*'], ['*']] >>> lists = [list() for _ in range(3)] # _ is unused >>> lists [[], [], []] >>> list1 = lists[1] >>> list1.append('*') >>> lists [[], ['*'], []] >>> lists = [list()] * 3 >>> for i in range(len(lists)) : lists[i].append(i) ... >>> lists [[0, 1, 2], [0, 1, 2], [0, 1, 2]] >>> lists = [list() for i in range(3)] >>> for i in range(len(lists)) : lists[i].append(i) ... >>> lists [[0], [1], [2]] Denis -- la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] use of __new__
Hello, I need a custom unicode subtype (with additional methods). This will not be directly used by the user, instead it is just for internal purpose. I would like the type to be able to cope with either a byte str or a unicode str as argument. In the first case, it needs to be first decoded. I cannot do it in __init__ because unicode will first try to decode it as ascii, which fails in the general case. So, I must have my own __new__. The issue is the object (self) is then a unicode one instead of my own type. class Unicode(unicode): Unicode.FORMAT = "utf8" def __new__(self, text, format=None): # text can be str or unicode format = Unicode.FORMAT if format is None else format if isinstance(text,str): text = text.decode(format) return text ... x = Unicode("abc") # --> unicode, not Unicode Denis la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem with turtle
On Thu, 11 Mar 2010 10:11:28 -0500 Marco Rompré wrote: > Hi! I am relatively new to python and turtle and I really need your help. > > Here what I wanted turtle to do: I created a function named carré which > would draw a square with 3 different parameters (color, size, angle). > I did the same for a function > named triangle that would draw an equilateral triangle with the same > parameters > With the help of these 2 > functions I wanted turtle to draw a yellow square then lift my pointer let a > space and then draw a blue triangle five times in a row. > > Here's my code: ignore my comments > > def carre(taille, couleur, angle): > "fonction qui dessine un carré de taille et de couleur déterminées" > color(couleur) > c=0 > while c<4: > left(angle) > forward(taille) > right(90) > c = c+1 What is angle? Define its sense in the doc string. If it's the general orientation of the "carré", then you should use it only once, at start. > def triangle(taille, couleur, angle): > "fonction qui dessine un triangle équilatéral de taille, de couleur et > d'angle déterminés" > color(couleur) > c=0 > while c<3: > left(angle) > forward(taille) > right(120) > c = c+1 Idem. > from dessins_tortue import * > > n=0 > while n < 10 : > down() # abaisser le crayon > carre(25, 'yellow', 0) # tracer un carré > up() > forward(30) > triangle(90, 'blue',0) > n = n + 1 Last line is mis-indented (maybe copy-paste problem). > If I am to vague I wanted to do successfully exercise 7.6 in Gérard Swinnen > tutorial for Python > > It was supposed to look like this ??? Denis la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] use of __new__
On Fri, 12 Mar 2010 12:27:02 +1100 Steven D'Aprano wrote: > On Fri, 12 Mar 2010 11:53:16 am Steven D'Aprano wrote: > > > I have tried to match the behaviour of the built-in unicode as close > > as I am able. See here: > > http://docs.python.org/library/functions.html#unicode > > And by doing so, I entirely forgot that you want to change the default > encoding from 'ascii' to 'utf-8'! Oops. Sorry about that. > > Try changing this bit: > > > else: # Never attempt decoding. > > obj = super(Unicode, cls).__new__(Unicode, string) I get it! The call to new must pass the target class as argument: obj = unicode.__new__(Unicode, string, encoding) ^ while I passed unicode instead. I guess this is the bit that decides on the final class of the return obj. > to this: > > # Untested > else: > if isinstance(string, unicode): > # Don't do any decoding. > obj = super(Unicode, cls).__new__(Unicode, string) > else: > if encoding is None: encoding = cls._ENCODING > if errors is None: errors = cls._ERRORS > obj = super(Unicode, cls).__new__( > Unicode, string, encoding, errors) Thank you again, Steven. Denis la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] %s %r with cutom type
Hello again, A different issue. On the custom Unicode type discussed in another thread, I have overloaded __str__ and __repr__ to get encoded byte strings (here with debug prints & special formats to distinguish from builtin forms): class Unicode(unicode): ENCODING = "utf8" def __new__(self, string='', encoding=None): if isinstance(string,str): encoding = Unicode.ENCODING if encoding is None else encoding string = string.decode(encoding) return unicode.__new__(Unicode, string) def __repr__(self): print '+', return '"%s"' %(self.__str__()) def __str__(self): print '*', return '`'+ self.encode(Unicode.ENCODING) + '`' An issue happens in particuliar cases, when using both %s and %r: s = "éâÄ" us = Unicode(s) # str print us, print str(us), print us.__str__(), print "%s" %us # repr print repr(us), print us.__repr__(), print "%r" %us # both print "%s %r" %(us,us) ==> éâÄ * `éâÄ` * `éâÄ` éâÄ + * "`éâÄ`" + * "`éâÄ`" + * "`éâÄ`" + * Traceback (most recent call last): File "Unicode.py", line 38, in print "%s%r" %(us,us) UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2: ordinal not in range(128) Note that Unicode.__str__ is called neither by "print us", nore by %s. What happens? Why does the issue only occur when using both format %s & %s? If I replace the last line by "print "%s %r" %(str(us),us)", all works fine. But then what's the point with %s? And why doesn't print alone call __str__? Denis ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Visual Python programming and decompilers?
On Fri, 12 Mar 2010 10:19:39 +0200 Ludolph wrote: > Hi Guys > > I posted the following message on my local pug mailing list and > someone recommended I post it here. > > At work I have been exposed to a Agile Platform called OutSystems. It > allows you to visually program your web applications > http://i.imgur.com/r2F0i.png and I find the idea very intriguing. > > So I have started to play around with the idea on how will I be able > to visually represent Python code as in the above image and then allow > the programmer to change some of the flow/code/logic visually and then > get it back as python source code. I don't know if this have been > tried before and after some googling I can't find anything like this, > so maybe I'm just lacking basic googling skills or a python solution > like the above does not exist yet. > > If anybody knows of such solution please let me know, so that I don't > spend a lot of time recreating the wheel. There has been (and probably still are) numerous projects around visual programming. > Otherwise help me out on the following problem: > I decided I can use byteplay3 http://pypi.python.org/pypi/byteplay/ to > disassemble the code to workable objects, It even allows me to rebuild > the objects to bytecode. So if I define patterns on how python > interrupts the source code to bytecode I can visually represent this > and also so convert my visual representations back to bytecode. > > The only problem I have at the moment is how will I get this bytecode > back to python source code. I have googled for python decompiler but > only found old projects like unpyc, decompyle and some online > services. I would like to know if anybody know of a well maintained or > at least recent module that can help me accomplish the above > mentioned, because I'm hoping I can implement this in Python 3.1. > > So any input or suggestion would be greatly appreciated. Don't understand why you work at the bytecode level. > Kind Regards, > > -- > Ludolph Neethling Denis la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] use of __new__
On Fri, 12 Mar 2010 22:56:37 +1100 Steven D'Aprano wrote: > You might be tempted to change the first reference to Unicode to cls as > well, but sadly that does not work. The reason is complicated, and to > be honest I don't remember it, but you will probably find it by > googling for "python super gotchas". Thank to your explanations, I may now have a guess on this :-) class BASE(root): def __new__(cls, ...): ... obj = super(BASE, cls).__new__(cls, *args) Let's say BASE is itself sub classed, but sub classes don't define their own __new__. How else ensure that the root class (the one actually creating new objects), is (one of) BASE's own base(s)? BASE beeing the top of a custom class tree, it's a kind of constant for its sub classes. Is this really a flaw, or instead a logical necessity? Denis la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] %s %r with cutom type
On Sat, 13 Mar 2010 13:50:55 +1100 Steven D'Aprano wrote: > On Fri, 12 Mar 2010 10:29:17 pm spir wrote: > > Hello again, > > > > A different issue. On the custom Unicode type discussed in another > > thread, I have overloaded __str__ and __repr__ to get encoded byte > > strings (here with debug prints & special formats to distinguish from > > builtin forms): > [...] > > Note that Unicode.__str__ is called neither by "print us", nore by > > %s. What happens? Why does the issue only occur when using both > > format %s & %s? > > The print statement understands how to directly print strings > (byte-strings and unicode-strings) and doesn't call your __str__ > method. > > http://docs.python.org/reference/simple_stmts.html#the-print-statement Right. But then how to print out customized strings? > As for string interpolation, I have reported this as a bug: > > http://bugs.python.org/issue8128 Yes, at least the actual behaviour should be clear and properly documented. And it should be the same for str and unicode type, and their subclasses. But I cannot see the advantage of not calling str(): this only prevents customization -- or use of string interpolation with cutomized string types. > I have some additional comments on your class below: > > > > class Unicode(unicode): > > ENCODING = "utf8" > > def __new__(self, string='', encoding=None): > > This is broken according to the Liskov substitution principle. > > http://en.wikipedia.org/wiki/Liskov_substitution_principle > > The short summary: subclasses should only ever *add* functionality, they > should never take it away. > > The unicode type has a function signature that accepts an encoding and > an errors argument, but you've missed errors. All right, I'll have a closer look to the semantics of unicode's error arg and see if it makes sense in my case. Notes for the following comments of yours: (1) What I posted is test code written only to show the issue. (eg debug prints are not in the original code) (2) This class is intended for a kind parsing and string processing library (think at pyparsing, but designed very differently). It should work only with unicode string, so convert source and every bit of string in pattern defs (eg for literal match). __str__ and __repr__ are intended for feedback (programmer test and user information, in both cases mainly error messages). __repr__ should normally not be used, I wrote it rather for completion. [...] > > if isinstance(string,str): > > encoding = Unicode.ENCODING if encoding is None else > > encoding string = string.decode(encoding) > > return unicode.__new__(Unicode, string) > > def __repr__(self): > > print '+', > > return '"%s"' %(self.__str__()) > > This may be a problem. Why are you making your unicode class pretend to > be a byte-string? (This answer rather for __str__) Not to pollute output. Eg parse tree nodes (= match results) show like: integer:[sign:- digit:123] > Ideally, the output of repr(obj) should follow this rule: > > eval(repr(obj)) == obj > > For instance, for built-in unicode strings: > > >>> u"éâÄ" == eval(repr(u"éâÄ")) > True > but for your subclass, us != eval(repr(us)). So again, code that works > perfectly with built-in unicode objects will fail with your subclass. > > Ideally, repr of your class should return a string like: > > "Unicode('...')" I 100% agree with your comment and this what I do in general. But it does not make much sense in my case, I guess. When I'm rather sure __repr__ will not normally be used, then I will probably rewrite to show Unicode("..."). > > def __str__(self): > > print '*', > > return '`'+ self.encode(Unicode.ENCODING) + '`' > > What's the purpose of the print statements in the __str__ and __repr__ > methods? Note (1). > Again, unless you have a good reason to do different, you are best to > just inherit __str__ from unicode. Anything else is strongly > discouraged. Note (2). > > An issue happens in particuliar cases, when using both %s and %r: > > > > s = "éâÄ" > > This may be a problem. "éâÄ" is not a valid str, because it contains > non-ASCII characters. It's just a test case (note (1)) for non-ascii input, precisely. > As far as I know, the behaviour of stuffing unicode characters into > byte-strings is not well-defined in Python, and will depend on external > factors like the terminal you are running
Re: [Tutor] Proper way to use **kwargs?
On Mon, 15 Mar 2010 05:38:43 -0700 (PDT) Karjer Jdfjdf wrote: > I want to use **kwargs to check a list of conditions (if true do this, if > false do nothing) besides required parameters ( in sample a and b). > Sometimes I want to add a Python object (in example a dictionary and a list). > Below is my first **kwargs-brew. > > ###START > > def function_with_kwargs(a, b, **kwargs): > options = { > 'logfile' : None, > 'door' : 'kitchendoor', > 'roof' : 'tiles', > 'mydict' : None, > 'mylist' : None, } > options.update(kwargs) The default set of options is a constant independant of the function and of its (other) arguments. So, I would rather define it outside. I find it clearer. from copy import copy OPTIONS = { 'logfile' : None, 'door' : 'kitchendoor', 'roof' : 'tiles', 'mydict': None, 'mylist': None, } def function_with_kwargs(a, b, **kwargs): options = copy(OPTIONS) options.update(kwargs) print options (Must be copied to avoid the defaults to be altered by update.) > logfile = options.get('logfile') > if logfile == None: > print "No logging" > else: > print "Logging" You don't need to use get, use options['logfile'] instead. Get is only useful if you use its default_value parameter. This would indeed be a good alternative to a global default dict for options: logfile = options.get('logfile', None) ... roof = options.get('roof', 'tiles') or maybe better the general pattern: x = options.get('x', OPTIONS['x']) > mydict = options.get('mydict') > if mydict == None: > print "Do nothing with dictionary" > else: > print "Do something with dictionary" > > mylist = options.get('mylist') > if mylist == None: > print "Do nothing with list" > else: > print "Do something with list" > > print "END OF FUNCTION\n" > > > somedict = { 'a': '1', 'b': '2', } > somelist = ['1', '2'] > > #DO SOMETHING > function_with_kwargs(1, 2, logfile='log.txt', door='frontdoor', > mydict=somedict, mylist=somelist) > > #DO NOTHING > function_with_kwargs(1, 2, door='frontdoor') > > > > ### END > > I have 2 questions about this code: > > 1. Can I use this in Python 3 ? I'm not sure if I can use **kwargs in Python > 3 because it uses the "apply" builtin (if I understand it correctly) > > "At this writing, both apply and the special call syntax described in this > section can be used freely in Python 2.5, but it seems likely that apply > may go away in Python 3.0. If you wish to future-proof your code, use > the equivalent special call syntax, not apply." > > 2. Are there better ways to achieve what I want to do? In this case, I would use a general config object, which attributes are individual parameters. You need to first define a fake Config class (because for any reason we cannot alter direct instances of object). Denis la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] module.class.method in logging.debug
On Mon, 15 Mar 2010 13:26:27 -0400 Brian Jones wrote: > I tried a couple of things I could think of, and successfully got the module > and class without issue, but the one missing piece was the method name. All methods and funcs (and types) have a __name__ attr. from types import FunctionType as Function def f1(): pass def f2(): pass def f3(): pass for (name,obj) in vars().items(): if type(obj) is Function: print obj.__name__, ==> f1 f2 f3 Denis la vita e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Self-intro and two short newbie questions
On Thu, 18 Mar 2010 22:42:05 +1100 Kevin Kirton wrote: > My questions are: how long would you estimate it to take and how > complicated would it be to create the following as Python programs? (I > know it varies depending on the person, but for example, how long > would it take _you_?) My opinion: The time required to make a working app (with exactly the same features, in the same language) by 2 trained programmers can easily range from 1 to 10. The time required to make a given app by a given programmer can easily range from 1 to 10 depending on the level of sophistication. "Devil hides in the details." ;-) Denis vit e estrany spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Misc question about scoping
On Thu, 3 Jun 2010 11:50:42 -0400 Tino Dai wrote: > Hi All, > > Is there a way to express this: > isThumbnail = False > if size == "thumbnail": > isThumbnail = True > > like this: > [ isThumbnail = True if size == "thumbnail" isThumbnail = False ] > and the scoping extending to one level above without resorting to the > global keyword? If you are not in a function, then your question does not make sense for python. If you are in a function, then isThumbnail must have been defined before and you must use global, yes. Thus, maybe no need for complicated expression: isThumbnail = False; def f(size): global isThumbnail if size == "thumbnail": isThumbnail = True > Thanks, > Tino -- vit esse estrany ☣ spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sockets, servers, clients, broadcasts...?
On Thu, 3 Jun 2010 18:03:34 -0400 Alex Hall wrote: > Hi all, > I am a CS major, so I have had the required networking class. I get > the principles of networking, sockets, and packets, but I have never > had to actually implement any such principles in any program. Now I > have this Battleship game (not a school assignment, just a summer > project) that I am trying to make playable over the internet, since I > have not written the AI and playing Battleship against oneself is > rather boring. > > Right now I am just trying to figure out how to implement something > like the following: > *you start the program and select "online game" > *you select "server" or "client" (say you choose "server") > *somehow, your instance of the program starts up a server that > broadcasts something; your enemy has selected "client", and is now > (SOMEHOW) listening for the signal your server is broadcasting > *the signal is picked up, and, SOMEHOW, you and your opponent connect > and can start sending and receiving data. > > First, how does the client know where to look for the server? I am not > above popping up the server's ip and making the client type it in, but > a better solution would be great. > How do I make it so that the client can find the server correctly? The > above IP is one thing, but are ports important here? Not a big deal if > they are, but I am not sure. Does someone have an example of this > process? I'm far to be a specialist in this field, so this is just reasoning by watching your requirements. First, such a game seems to match peer-to-peer relation. This would be different if you implemented complicated game logic, like in the case of an AI playing. For peers to connect, a general solution is them to register on a dedicated public interface (the same can be used to publish a server's IP, indeed). Then, your app first reads data there to know which (address,port) pair(s) are available. But since you seem to be still exploring data exchange issues, you'd better concentrate on this basic mechanism first, choose and try one of numerous possible solutions, then only address higher-level problems. In the meanwhile, just put IPs and ports in a config file or even hardcode them as constants. About client-server, as said above, this model does not really seem to match your use case, I guess. But this is still an interesting alternative. I would set my own post as server and players as clients. So, when playing myself, I would be a local client. This indeed allows two other guys playing using your post as server (and you maybe watching the game ;-). But this does not make much sense if the server does not have any relevant info to deliver (eg playing hints). Denis vit esse estrany ☣ spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Misc question about scoping
On Fri, 4 Jun 2010 12:26:20 -0400 Tino Dai wrote: > Also could you give me some instances > where a generator > would be used in a real situation? I have already read the stuff on > doc.python.org about > generators. Sure, generally speaking in the programming world, documentation misses the first and principle step: *purpose* :-) Why is that stuff intended for? My point of view is as follows (I don't mean it's _the_ answer): Generators are useful when not all potential values need be generated (or possibly not all). Meaning you perform some task until a condition is met, so you don't need all possible map values. A mapping or list comprehension instead always creates them all. A particuliar case where a generator is necessary is the one of an "open", unlimited, series, defined by eg a formula, such as cubes. Such a potentially infinite series is only broken by a loop break: def cubes(first): n = first while True: yield n ** 3 n += 1 for cube in cubes(1): if cube > 999: break else: print(cube), Denis vit esse estrany ☣ spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] treeTraversal, nested return?
On Fri, 4 Jun 2010 10:15:26 -0700 (PDT) jjcr...@uw.edu wrote: > All, > > any observations might be helpful. For the display of database contents I > have the following problem: > > Database querys will return data to me in tuples of the following sort: > each tuple has a unique id, a parent id, and a type. > > a parent id of 0 indicates that the element has no parent. > not all data elements have children > image elements have no children and must have a parent > > So the contents of the db is a tree of sorts: > > (1, 0, work) > (555, 1, image) > (556, 1, work) > (557, 556, image) > (558, 556, work) > (559, 558, image) > (560, 558, work) > (561, 556, image) > (562, 556, image) > (563, 1, work) > (564, 563, work) > (565, 1, work) > > I have a function that will traverse this tree, summoning lists of tuples > at each leaf. Well and good; it took a lot of trial and error, but it was > a fine education in tree traversal. > > def makeHeirarchy(id): > id = parentPath(id)[-1] > rootimages = getImages(id[0]) > rootworks = getWorks(id[0]) > heirarchy = treeTraversal(rootworks, [id, rootimages]) > return heirarchy > > ## which calls this recursive function: > > def treeTraversal(listIn,result): > for x in listIn: > if not getWorks(x[0]): > result.append(x) > result.append(getImages(x[0])) > else: > result.append(x) > result.append(getImages(x[0])) > treeTraversal(getWorks(x[0]), result) > return result > > My problem is that this returns the tuples to me in a flat list. > I've been trying to work out how I can get it to return some sort of > nested structure: nested lists, dictionaries, or html thus: For an indented output, you're simply forgetting to inform the recursive function aobut current indent level. Example (note: a nested sequence is not a tree: it has no root): SPACE , WIDTH, NODE, NL = " " , 4 , "" , '\n' def indentOutput(s, level=0): # fake root if level == 0: text = NODE else: text = "" # children level += 1 offset = level * SPACE * WIDTH for element in s: if isinstance(element, (tuple,list)): text += "%s%s%s%s" %(NL,offset,NODE , indentOutput(element, level)) else: text += "%s%s%s" %(NL,offset,element) return text s = (1,(2,3),4,(5,(6,7),8),9) print indentOutput(s) 1 2 3 4 5 6 7 8 9 Denis vit esse estrany ☣ spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Lisphon
On Fri, 4 Jun 2010 22:33:19 +0200 Hugo Arts wrote: > On Fri, Jun 4, 2010 at 6:26 PM, Tino Dai wrote: > > > > LOL, it's actually on the list of things to do. And hear that one will > > become a > > better programmer once they learn LISP. > > > > I most certainly did. There are very few languages as conceptually > pure as this one. If you ever get it on the top of your list, check > out these links. [1] is the venerable book "structure and > interpretation of computer programs," commonly regarded as one of the > best introductory programming books ever written. It teaches scheme. > [2] is a set of excellent lectures of said book, by its writers. They > are very good teachers. > > [1] http://mitpress.mit.edu/sicp/ > [2] http://groups.csail.mit.edu/mac/classes/6.001/abelson-sussman-lectures/ Thanks for the pointer, Hugo, did not know these lectures. If you do not know lisperati, you *must* follow the pointer, if only for the home page: http://lisperati.com/. Also, history of Lisp: http://www-formal.stanford.edu/jmc/history/lisp/lisp.html Denis vit esse estrany ☣ spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple PassGen
Le Tue, 10 Feb 2009 10:26:54 -0500, pyt...@bdurham.com a écrit : > IDLE 2.6.1 > >>> from __future__ import print_function > >>> print( 3, 4 ) > 3 4 lol! -- la vida e estranya ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Inserting special characters into urlencoded string
Le Tue, 10 Feb 2009 18:08:26 +0100, pa yo a écrit : > Novice programmer here. > > I am using urllib.urlencode to post content to a web page: > > ... > >>Title = "First Steps" > >>Text = "Hello World." > >>Username = "Payo2000" > >>Content = Text + "From: " + Username > >>SubmitText = urllib.urlencode(dict(Action = 'submit', Headline = Title, > >>Textbox = Content)) > >>Submit = opener.open('http://www.website.com/index.php?', SubmitText) > > This works fine to produce in this: > > "Hello World From Payo2000" > > ...on the page. > > However I can't work out how to add a linefeed (urlencode: %0A) in > front of Username so that I get: > > "Hello World. > From: Payo2000" > > Any hints, tips or suggestions welcome! In python (and many other languages) some special characters that are not easy to represent have a code: LF : \n CR : \r TAB : \t So just add \n to to first line to add an eol: Text = "Hello World.\n" [Beware that inside python \n actually both means char #10 and 'newline', transparently, whatever the os uses as 'newline' code for text files. Very handy.] Alternatively, as you seem to be used to web codes, you can have hexa representation \x0a or even octal \012 Denis - la vida e estranya ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reply All Dilemma of This List
Le Tue, 10 Feb 2009 12:20:51 -0500, Brian Mathis a écrit : > This war has been raging since the dawn of mailing lists, and you're > not likely to get a resolution now either. Newer mail user agents have a "to list" reply button. End of war? -- la vida e estranya ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Keeping Dictonary Entries Ordered
Le Thu, 12 Feb 2009 09:25:22 -, "Alan Gauld" a écrit : > > config_names = {"start_time : '18:00:00', 'gray_scale' : True, > > "long": 120.00} > > > > If I iterate over it, the entries will appear in any order, as > > opposed to > > what I see above. However, in the config file, I'd like to keep them > > in the order above. > > Thats tricky! Dictionaries are not sorted and do not retain insertion > order. Had to do something similar. I just subtyped dict to add a separate list a keys. Initiated with the possible initial arguments, then updated by setattr. Very few code lines. Then Ordict had a simplissim method to return values (and pairs) the keylist's order. The only issue was rather a semantic one: should a value change keep the keylist unchanged or move the key to the last position? Denis -- la vida e estranya ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Keeping Dictonary Entries Ordered
Le Fri, 13 Feb 2009 15:41:01 +1300, John Fouhy a écrit : > 2009/2/13 Eric Dorsey : > > Alan, can you give a short snippet of what that would look like? I was > > trying to code out some idea of how you'd retain insertion order using > > another dict or a list and didn't get anywhere. > > Here's something basic: > > class o_dict(dict): > def __init__(self, *args, **kw): > dict.__init__(self, *args, **kw) > self.__keylist = [] > > def __setitem__(self, key, val): > dict.__setitem__(self, key, val) > if key not in self.__keylist: > self.__keylist.append(key) > > def __iter__(self): > return iter(self.__keylist) > > It will do the right thing if you do 'for key in odict:', but not for > things like iteritems(). It will break if you delete keys from the > dictionary, and the 'key not in self.__keylist' test will get slow if > you have lots and lots of keys. It will also not do what you want if > you initialise it as something like: o_dict(foo=1, bar=2) An issue is that obviously you can't initialize an ordered dict from another dict or a set of keyword arguments, for those are unordered. So I simply took **kwargs off the __init__ method. And let as single possible init stuff a list of (key,value) pairs. Also, as a built_in dict accepts only a single arg, there is no need for *arg: the seq of pairs must be contained in a list or tuple. From the pairs can then the keylist be properly initialised. This led me to something similar to the following: class Ordict(dict): def __init__(self, pairs): dict.__init__(self, pairs) self._keylist = [k for (k,v) in pairs] def __setitem__(self, key, val): dict.__setitem__(self, key, val) if key not in self._keylist: self._keylist.append(key) def __iter__(self): return iter(self._keylist) def __str__(self): pair_texts = ["%s:%s" % (k,self[k]) for k in self._keylist] return "[%s]" % ", ".join(pair_texts) od = Ordict([('a',1), ('d',4), ('b',2)]) od[' '] = 0 ; od['c'] = 3 print od._keylist for k in od: print "%s: %s " % (k,od[k]), print; print od ==> ['a', 'd', 'b', ' ', 'c'] a: 1d: 4b: 2 : 0c: 3 [a:1, d:4, b:2, :0, c:3] I remember there were some more minor issues. Denis -- la vida e estranya ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exec "self.abc=22" ?
Le Mon, 16 Feb 2009 12:01:24 -0800, Marc Tompkins a écrit : > On Mon, Feb 16, 2009 at 11:58 AM, Wayne Watson > wrote: > > > Python doesn't like the code in the Subject (unqualified exec not allowed > > in function). but easily likes self.abc="22". However, I'd like to assemble > > the assignment as a string, as shown in Subject, and execute it. Is there a > > way to do this? > > -- > > > > varName = "abc" > varValue = 22 > setattr(self, varName, varValue) >>> exec("name='22'") >>> name '22' >>> exec("name=\"22\"") >>> name '22' -- la vida e estranya ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Add readline capabilities to a Python build 2.6 on Ubuntu
Le Mon, 16 Feb 2009 22:34:23 -0700, Eric Dorsey a écrit : > Greetings Tutor: > I've managed to install Python 2.6 on my Ubuntu VM from source, however, it > looks as though I missed something important along the way. My 2.6 > interpreter does not have readline support (example: I cant hit up arrow to > repeat the last command) Is there a way to add this functionality now? > > Or alternative, if I just rebuild it, does anyone know the flag or verbage > to get readline support in? As far as I know, it's built-in by default. But I have 2.5.2, not 2.6. Have you tried to import it, just to check? s...@o:~/prog/io$ python Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import readline >>> readline denis -- la vida e estranya ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Proxies/Interceptors for file-like objects
Le Wed, 18 Feb 2009 22:01:34 -0500, Kent Johnson s'exprima ainsi: > Hmm. I guess this is Python 3? In 2.6, open is a function and trying > to subclass it gives an error: > > In [10]: open > Out[10]: > > In [11]: class myopen(open): pass > >: > > TypeError: Error when calling the metaclass bases > cannot create 'builtin_function_or_method' instances But... why not use the proper type instead: class F(file): def __init__(self, file_name): self.file_name = file_name f = F("/home/prog/io/test.py") print dir(f) print isinstance(f,types.FileType) ==> ['__class__', '__delattr__', '__dict__', '__doc__', '__enter__', '__exit__', '__getattribute__', '__hash__', '__init__', '__iter__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'close', 'closed', 'encoding', 'file_name', 'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines'] True Then, overload the proper method the call of which you need to catch. Or there is a "detail" I do not "catch" ;-) Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Standardizing on Unicode and utf8
Le Fri, 20 Feb 2009 02:52:27 -0800, "Dinesh B Vadhia" s'exprima ainsi: > We want to standardize on unicode and utf8 and would like to clarify and > verify their use to minimize encode()/decode()'ing: > > 1. Python source files > Use the header: # -*- coding: utf8 -*- You don't even need fancy decoration: # coding: utf-8 is enough. Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Default list arguments in __init__
Le Sat, 21 Feb 2009 11:38:49 -0800, Moos Heintzen s'exprima ainsi: > Hi, > > This behavior was totally unexpected. I only caught it because it was > the only thing I changed. > > >>> class foo: > ... def __init__(self, lst=[]): > ... self.items = lst > ... > >>> f1 = foo() > >>> f1.items > [] > >>> f1.items.append(1) > >>> f2 = foo() > >>> f2.items > [1] > > Huh? lst is a reference to the *same list* every instance? Yop! Default args are evaluated once and only once at func def time. Very common trap, indeed! Note that this has nothing to do with __init__, nore with methods specifically. You can reproduce your example with a "free" function. > I guess I have to do it like this. It seems to work. (i.e. every foo > instance with default lst now has a unique new list.) > > def__init__(self, lst=None): > self.items = lst or [] This is the right remedy. Except that I would write self.items = [] if lst is None else lst to avoid "tricking" with bools (personal taste). denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] calling user defined function
Le Sun, 22 Feb 2009 22:21:22 +0100, roberto s'exprima ainsi: > hello > i have a question which i didn't solved yet: > i can define a function using the text editor provided by IDLE 3.0; > then i'd like to call this function from the python prompt > > but when i try to do it, python warns me that function doesn't exist > of course if i define the function directly using the >>> prompt, > after that everything is fine > > may you tell me where i have to save the file that defines the > function is order to use it later ? > is it a problem of path ? I guess what you mean is how to run your program? If you write it inside the editor (as opposed to the interactive window where you get the prompt), then all you need is save (where ever you like it) and run (menu or F5). The program's output, if any, will then happen inside the interactive window. You can also "run" a program or module by importing it from inside the interactive window, using >>> import filenamewithoutdotpy Further runs/imports during the same session must however be expressed with >>> reload(filenamewithoutdotpy) Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regex help
Le Mon, 23 Feb 2009 06:45:23 -0500, Kent Johnson s'exprima ainsi: > On Sun, Feb 22, 2009 at 10:49 PM, ish_ling wrote: > > I have a string: > > > >'a b c h' > > > > I would like a regex to recursively match all alpha letters that are > > between . That is, I would like the following list of > > matches: > > > >['d', 'e', 'f', 'i', 'j'] > > > > I do not want the 'g' or the 'k' matched. > > > > I have figured out how to do this in a multiple-step process, but I would > > like to do it in one step using only one regex (if possible). My multiple > > step process is first to use the regex > > > >'(?<=H )[a-z][^H]+(?!H)' > > I would use a slightly different regex, it seems more explicit to me. > r'' You can even probably exclude leading & trailing spaces from match: r'' returns ['d e f', 'i j'] > Kent > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create dictionary from csv data
Le Mon, 23 Feb 2009 14:41:10 +0100, Norman Khine s'exprima ainsi: > Hello, > > I have this csv file: > > $ cat licences.csv > "1","Air Travel Organisation Licence (ATOL)\n Operates Inclusive Tours (IT)" > "2","Air Travel Organisation Licence (ATOL)\n Appointed Agents of IATA > (IATA)" > "3", "Association of British Travel Agents (ABTA) No. 56542\n Air Travel > Organisation Licence (ATOL)\n Appointed Agents of IATA (IATA)\n > Incentive Travel & Meet. Association (ITMA)" I have the impression that the CSV module is here helpless. Yes, it parses the data, but you need only a subset of it that may be harder to extract. I would do the following (all untested): -0- Read in the file as a single string. > I would like to create a set of unique values for all the memberships. i.e. > > ATOL > IT > ABTA > etc.. -1- Use re.findall with a pattern like r'\((\w+)\)' to get the company codes, then built a set out of the result list > and also I would like to extract the No. 56542 -2- idem, with r'No. (\d+)' (maybe set is not necessary) > and lastly I would like to map each record to the set of unique > membership values, so that: > > I have a dictionary like: > > {0: ['1', '('ATOL', 'IT')'], > 1: ['2','('ATOL', 'IATA')'], > 2: ['3','('ABTA', 'ATOL', 'IATA', 'ITMA')']} (The dict looks strange...) -3- Now "splitlines" the string, and on each line * read ordinal number (maybe useless actually) * read again the codes I dont know what your dict is worthful for, as the keys are simple ordinals. It's a masked list, actually. Unless you want instead {['1':['ATOL', 'IT'], '2':['ATOL', 'IATA'], '3':['ABTA', 'ATOL', 'IATA', 'ITMA']} But here the keys are still predictable ordinals. denis -- la vita e estrany > Here is what I have so far: > > >>> import csv > >>> inputFile = open(str("licences.csv"), 'r') > >>> outputDic = {} > >>> keyIndex = 0 > >>> fileReader = csv.reader(inputFile) > >>> for line in fileReader: > ... outputDic[keyIndex] = line > ... keyIndex+=1 > ... > >>> print outputDic > {0: ['2', 'Air Travel Organisation Licence (ATOL) Appointed Agents of > IATA (IATA)'], 1: ['3', ' "Association of British Travel Agents (ABTA) > No. 56542 Air Travel'], 2: ['Organisation Licence (ATOL) Appointed > Agents of IATA (IATA) Incentive Travel & Meet. Association (ITMA)"']} > > So basically I would like to keep only the data in the brackets, i.e. > (ABTA) etc.. > > Cheers > > Norman > ___ > 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] using re to build dictionary
Le Tue, 24 Feb 2009 12:48:51 +0100, Norman Khine s'exprima ainsi: > Hello, > From my previous post on create dictionary from csv, i have broken the > problem further and wanted the lists feedback if it could be done better: > > >>> s = 'Association of British Travel Agents (ABTA) No. 56542\nAir > Travel Organisation Licence (ATOL)\nAppointed Agents of IATA > (IATA)\nIncentive Travel & Meet. Association (ITMA)' > >>> licences = re.split("\n+", s) > >>> licence_list = [re.split("\((\w+)\)", licence) for licence in licences] > >>> association = [] > >>> for x in licence_list: > ... for y in x: > ... if y.isupper(): > ...association.append(y) > ... > >>> association > ['ABTA', 'ATOL', 'IATA', 'ITMA'] > > > In my string 's', I have 'No. 56542', how would I extract the '56542' > and map it against the 'ABTA' so that I can have a dictionary for example: > > >>> my_dictionary = {'ABTA': '56542', 'ATOL': '', 'IATA': '', 'ITMA': ''} > >>> > > > Here is what I have so far: > > >>> my_dictionary = {} > > >>> for x in licence_list: > ... for y in x: > ... if y.isupper(): > ... my_dictionary[y] = y > ... > >>> my_dictionary > {'ABTA': 'ABTA', 'IATA': 'IATA', 'ITMA': 'ITMA', 'ATOL': 'ATOL'} > > This is wrong as the values should be the 'decimal' i.e. 56542 that is > in the licence_list. > > here is where I miss the point as in my licence_list, not all items have > a code, all but one are empty, for my usecase, I still need to create > the dictionary so that it is in the form: > > >>> my_dictionary = {'ABTA': '56542', 'ATOL': '', 'IATA': '', 'ITMA': ''} > > Any advise much appreciated. > > Norman I had a similar problem once. The nice solution was -- I think, don't take this for granted I have no time to verify -- simply using multiple group with re.findall again. Build a rule like: r'.+(code-pattern).+(number_pattern).+\n+' Then the results will be a list of tuples like [ (code1, n1), (code2, n2), ... ] where some numbers will be missing. from this it's straightforward to instantiate a dict, maybe using a default None value for n/a numbers. Someone will probably infirm or confirm this method. denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Accessing callers context from callee method
Le Tue, 24 Feb 2009 10:53:29 -0800, mobiledream...@gmail.com s'exprima ainsi: > when i call a method foo from another method func. can i access func context > variables or locals() from foo > so > def func(): > i=10 > foo() > > in foo, can i access func's local variables on in this case i > Thanks a lot def func(): i=10 foo(i) -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Passing perimeters in dictionary values?
Le Tue, 24 Feb 2009 14:03:09 -0500, nathan virgil s'exprima ainsi: > I'm experimenting with OOP using the Critter Caretaker script from Python > Programming for the Absolute Beginner as my basis. I've noticed that a > dictionary/function combo is a great way to handle menus, and so I've > adapted the menu to read as: > > > selection = raw_input("Choice: ") > choices = {"0":quit, "1":crit.talk, "2":crit.eat, "3":crit.play} > choice = choices[selection] > choice() This is (to my taste) very good programming practice. The only detail is that in this precise case, as keys are ordinals starting at 0, you can use a simple list instead ;-) and choice = choices[int(selection)] > so that I can call methods from a dictionary, instead of having an > excruciatingly long if structure. Unfortunately, the problem I'm running > into with this is that I can't pass any perimeters through the dictionary. I > can't figure out how, for example, I could have an option that calls > crit.eat(2) and another that calls crit.eat(4). The only thing I can think > of is going back to the if structure, but my instinct tells me that this is > a Bad Idea. What can I do? There is a syntactic trick for this, commonly called *args. You can call a function and pass it a variable holding a 'pack' of arguments prefixed with '*' so that the args will be automagically unpacked into the call message. Below an example: def sum(a,b): return a+b arg_list = (1,2) func_calls = {1:sum(*arg_list)} print func_calls[1] ==> 3 Like if I had written sum(1,2) -- except that the arg_list can now be unknown at design time! Conversely, when you want a function to accept an arbitrary number of args, you can write its def using the * trick: def sum(*values): s = 0 for v in values : s+= v return s print sum(1), sum(1,2), sum(1,2,3) ==> 1, 3, 6 There is a similar trick for named arguments using ** denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Passing perimeters in dictionary values?
Le Wed, 25 Feb 2009 00:01:49 -, "Alan Gauld" s'exprima ainsi: > > "nathan virgil" wrote > > > I'm not familiar with lambdas yet, and I don't think this book will > > introduce me to them; they aren't listed in the index, anyway. > > lambda is just a fancy mathematical name for a simple > concept - its a block of code, like the body of a function. > In Python its even simpler, it is an expression. And we > store that expression and make it callable. > > > def funcname(aParam): > return > > is the same as > > funcname = lambda aParam: > > In some languages, including some Lisp dialects function > definitions are syntactic sugar for exactly that kind of lambda > assignment. Some even make it explicit as in: > > (define func >( lambda (aParam) > (expression using aParam))) In io (a wonderful prototype-based languages which syntax reflects the code structure (read: parse tree) like in Lisp), you would have: method_name := method(aParam, ) for instance: perimeter := method(radius, 2 * PI * radius) average := method(values, sum := values sum sum / values count ) [Above: there is not '.' -- return value needs not be explicit] > In Python its more like the other way around, functions are > defined using def and lambdas are really syntax wrapped > around that. > > lambdas are often offputting to beginners but they are useful > in avoiding lots of small oneliner type functions (and the resultant > namespace clutter) and they are very important in understanding > the theory behind computing (ie. Lambda calculus). > > HTH, > > -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to instantiate a class
Le Thu, 26 Feb 2009 12:38:27 +0530, Abhishek Kumar s'exprima ainsi: > hello list, > > Below is the sample code of a class. > > > import > > Class ABC: > def __init__(self,a,b,c): > statement 1 > statement 2 > def func(x,y): >statement 1 >statement 2 > > Here is the question: > > how to make an object of this class abc = ABC(1,2,3) >and how to use the methods listed > in the class. abc.func(foo,bar) > do i need to create the object in a separate file or in the same file > it can be done ?? Both are possible. If not in the same file, you must first import the file (module) where ABC is defined, then ABC will behave like an attribute of the module; or just the name ABC from this file, in which case ABC will be locally know: import ABCFile abc = ABCFile.ABC(1,2,3) or from ABCFile import ABC abc = ABC(1,2,3) denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] learning new features of python
Le Thu, 26 Feb 2009 18:16:59 + (GMT), ALAN GAULD s'exprima ainsi: > > > >> Similaraly in Alan Guald Learn to program link, he has given > > >> information on opening a file with file() and open() functions. > > > > > > And in V3 they took that back out again :-( > > > > ?? open() is in V3. file() is not. > > That's right, they have reverted to how it was in Python 1.X > I changed all my pages to use file() in v2.2 then changed them > to use a mixtyuure of file() and open() in 2.4 and now I'm changing > them all back to open() for v3! :-( > > Alan G. Does someone know why this choice has been made? After all, the result is a file object, so imo it's consistent to use file() like for any other type. Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class instance understanding = None
Le Fri, 27 Feb 2009 00:06:59 -0500, David s'exprima ainsi: > Hi Everyone, > I go through the archived [Tutor] mail list to find programs others have > tried to do. I found one that would keep track of a petty cash fund. > please point out my misunderstanding. > Here is what I started with; > [...] > > > results; > The current date is: 02/27/09 > balance = 100.0 > withdraw = None > deposit = None > > expected results; > The current date is: 02/27/09 > balance = 100.0 > withdraw = 50.0 > deposit = 100.0 > > thanks, > -david So, what is your question, actually? If you do not want None, replace it with 0.0. None is good as a flag value. Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class instance understanding = None
Le Fri, 27 Feb 2009 00:06:59 -0500, David s'exprima ainsi: > Hi Everyone, > I go through the archived [Tutor] mail list to find programs others have > tried to do. I found one that would keep track of a petty cash fund. > please point out my misunderstanding. > Here is what I started with; > > #!/usr/bin/python > > from reportlab.lib.normalDate import ND > #import cPickle as p > #import pprint > > today = ND() > > class Account: > def __init__(self, initial): > self.balance = initial > def deposit(self, amt): > self.balance = self.balance + amt > def withdraw(self, amt): > self.balance = self.balance - amt > def getbalance(self): > return self.balance > print 'The current date is: ', today.formatUS() > > > data = float('100.00') > a = Account(data) > p = a.getbalance() > print 'balance = ', p > remove_data = float('50.00') > w = a.withdraw(remove_data) > print "withdraw = ", w > add_data = float('50.00') > add = a.deposit(add_data) > print "deposit = ", add > > > results; > The current date is: 02/27/09 > balance = 100.0 > withdraw = None > deposit = None > > expected results; > The current date is: 02/27/09 > balance = 100.0 > withdraw = 50.0 > deposit = 100.0 > > thanks, > -david PS: I guess you misunderstand the methods withdraw and deposit: If you read the code (read it again), they *do* something, meaning they process an *action*. Understand they name as verbs. As a consequence they do not return anything result (they do not *make* anything). They are like Pascal procedures. So that you should state instead: w = float('50.00') ###w = a.withdraw(remove_data) print "withdraw = ", w denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Extract strings from a text file
Le Thu, 26 Feb 2009 21:53:43 -0800, Mohamed Hassan s'exprima ainsi: > Hi all, > > I am new to Python and still trying to figure out some things. Here is the > situation: > > There is a text file that looks like this: > > text text text Joseph > text text text text text text text text text text text > text text text text text text text text text text text > text text text text text text text text text text text > text text text text text text text text text text text > text text text text text text text text text text text > text text text text text text text text text text text > text text text text text text text text text text text > text text text Joseph Smith > text text text 1 > text text text 0 > > > This text file is very long, however all the entries in it looks the same at > the above. > > What I am trying to do is: > > 1. I need to extract the name and the full name from this text file. For > example: ( ID is Joseph & Full name is Joseph Smith). > > > - I am thinking I need to write something that will check the whole text > file line by line which I have done already. > - Now what I am trying to figure out is : How can I write a function that > will check to see if the line contains the word ID between < > then copy the > letters after > until > and dump it to a text file. > > Can somebody help please. I know this might soudn easy for some people, but > again I am new to Python and still figuring out things. > > Thank you This is a typical text parsing job. There are tools for that. However, probably we would need a bit more information about the real text structure, and first of all what you wish to do with it later, to point you to the most appropriate tool. I guess that there is a higher level structure that nests IDs, names, rights etc in a section and that you will need to keep them together for further process. Anyway for a startup exploration you can use regular expressions (regex) to extract individual data item. For instance: from re import compile as Pattern pattern = Pattern(r""".*(.+)<.+>.*""") line = "text text text Joseph" print pattern.findall(line) text = """\ text text text Joseph text text text Jodia text text text Joobawap """ print pattern.findall(text) ==> ['Joseph'] ['Joseph', 'Jodia', 'Joobawap'] There is a nice tutorial on regexes somewhere (you will easily find). Key points on this example are: r""".*(.+)<.+>.*""" * the pattern between """...""" expresses the overall format to be matched * all what is between (...) will be extracted by findall * '.' mean 'any character'; '*' means zero or more of what is just before; '+' mean one or more of what is just before. So the pattern will look for chains that contains a sequence formed of: 1. possible start chars 2. literally 3. one or more chars -- to return 4. something between <...> 5. possible end chars Denis -- la vita e estrany ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor