[Tutor] references and popping, caveats?
Hi all, I have the following code - >>> j = """a = { b = { c = { d = 5 e = 10 } } }""" >>> ref = [] >>> data = {} >>> ref.append(data) >>> for line in j.split('\n'): ... if "=" in line: ... (LHS, RHS) = line.split(" = ") ... if RHS == "{": ... #open group ... ref[-1][LHS] = {} ... ref.append(ref[-1][LHS]) ... else: ... ref[-1][LHS] = RHS ... else: ... #Only other value in test data is "}", so close group ... ref.pop(-1) Fairly unreadable huh. Basically, I'm using the list to store dictionary references, so as to create a bunch of nested dictionaries, working on the principle of last in, first off. I guess it's a rudimentary stack or thereabouts. Basically, if there's any caveats with popping and/or lists of references, I'm dead keen to find out before I take my prototype any further. Alternatively, if there's a far more simpler way to do this, also keen to hear it. :) (My first version had a list of keys ["a", "b", "c", "d"] and would use that to track depth. I then had a function which looked like this - if len(keyList) - 1 == -1: return elif len(keyList) - 1 == 0: return fooDict[keyList[0]] elif len(keyList) - 1 == 1: return fooDict[keyList[0]][keyList[1]] ... ... elif len(keyList) -1 == 5: return fooDict[keyList[0]][keyList[1]][keyList[2]][keyList[3]][keyList[4]][keyList[5]] And so forth Yuck. My very own coding WTF. ) Any help appreciated, Liam Clarke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help
Also, you may want to consider something like easygui - http://www.ferg.org/easygui/ if all you need is simple dialogs. You may also want to consider Pythoncard - pythoncard.sourceforge.net, it's quite capable of more elaborate GUI stuff, but it's built on wxPython, which can be a little intimidating. That said, for 90% of stuff involving GUIs, Pythoncard would be fine, and the other 10% is generally stuff Tkinter can't do anyways. Good luck, Liam Clarke On 8/10/05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Quoting Dan Deternova <[EMAIL PROTECTED]>: > > > ok. i know python and want to turn all my programs i made when i was > > learning python into Tkinter programs. i dont know how to make if > > satments and display the output like this in python: > > if cmd == quit: > > print "press exit to quit" > > i have no idea how to do that in Tkinter please help. > > Have a look at "Thinking in Tkinter": http://www.ferg.org/thinking_in_tkinter/ > > If you work through it, it should hopefully give you a better idea of how > Tkinter programs work. > > -- > John. > ___ > 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] Question on classes/instances
Hi Negroup, First off, you may want to use os.path.join to create paths - path = 'categories/%s' % self.name could be - path = os.path.join(categories, self.name) This will ensure minimum hassle if you ever want to use this across multiple OS. Also, it looks a little tidier, and IMAO, tidier code is easier to maintain. As for what you're wanting to do with the list comprehension, once again I'd use os.path.join, but otherwise your comprehension looks workable. On 8/10/05, Negroup - <[EMAIL PROTECTED]> wrote: > Hi, Sorry for the subject a bit generic. This question comes from a > simple program I'm doing. I could have a working solution, but > considering my inexperience, I'm just asking if my approach is > acceptable. > > This is a simple class to manage (actually to create and save) > categories of things. Categories are represented as directories, while > descriptions of categories are stored inside a file called > .description inside the category directory. Look at the code.. > > class Category: > def __init__(self, name=None, description=None): > self.description = description > if name: > self.name = name > else: > return 'Value expected' > > def save(self): > from os import access, F_OK, mkdir > > path = 'categories/%s' % self.name > if access(path, F_OK): > return 'Category already present' > else: > mkdir(path) > if self.description: > f = file('%s/.description' % path, 'w') > f.write(self.description) > f.close() > > and this is a simple function to get a list of categories, inside the > same module of Category: > def get_categories(): > from os import listdir > return listdir('categories') > > I would instead, that get_categories returns a list of instances of > Category class instead of a list of strings, so that I can handle > categories with proper APIs. I found this way: > > def get_categories(): > from os import listdir > # return listdir('categories') > path = 'categories' > categories = [Category(name, file('%s/%s/.description' % > (path, name)).read()) for name in listdir('categories')] > return categories > > Is it a good way to solve the problem? Otherwise, I would be glad if > you could propose better solutions. > > Thanks for any clarification. > ___ > 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] Question on classes/instances
Erk, I of course meant - path = os.path.join('categories', self.name) On 8/10/05, mailing list <[EMAIL PROTECTED]> wrote: > Hi Negroup, > > First off, you may want to use os.path.join to create paths - > > path = 'categories/%s' % self.name > > could be - > > path = os.path.join(categories, self.name) > > This will ensure minimum hassle if you ever want to use this across > multiple OS. > Also, it looks a little tidier, and IMAO, tidier code is easier to maintain. > > As for what you're wanting to do with the list comprehension, once > again I'd use os.path.join, but otherwise your comprehension looks > workable. > > On 8/10/05, Negroup - <[EMAIL PROTECTED]> wrote: > > Hi, Sorry for the subject a bit generic. This question comes from a > > simple program I'm doing. I could have a working solution, but > > considering my inexperience, I'm just asking if my approach is > > acceptable. > > > > This is a simple class to manage (actually to create and save) > > categories of things. Categories are represented as directories, while > > descriptions of categories are stored inside a file called > > .description inside the category directory. Look at the code.. > > > > class Category: > > def __init__(self, name=None, description=None): > > self.description = description > > if name: > > self.name = name > > else: > > return 'Value expected' > > > > def save(self): > > from os import access, F_OK, mkdir > > > > path = 'categories/%s' % self.name > > if access(path, F_OK): > > return 'Category already present' > > else: > > mkdir(path) > > if self.description: > > f = file('%s/.description' % path, 'w') > > f.write(self.description) > > f.close() > > > > and this is a simple function to get a list of categories, inside the > > same module of Category: > > def get_categories(): > > from os import listdir > > return listdir('categories') > > > > I would instead, that get_categories returns a list of instances of > > Category class instead of a list of strings, so that I can handle > > categories with proper APIs. I found this way: > > > > def get_categories(): > > from os import listdir > > # return listdir('categories') > > path = 'categories' > > categories = [Category(name, file('%s/%s/.description' % > > (path, name)).read()) for name in listdir('categories')] > > return categories > > > > Is it a good way to solve the problem? Otherwise, I would be glad if > > you could propose better solutions. > > > > Thanks for any clarification. > > ___ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Silly loop question
Hi all, I should know this, but it seems to elude me at present. I have a loop which is looping through a line of chars by index. So, one of these - for i in range(len(line)): Now, question is, is there a simple way to 'fast forward' index i? At the moment, I'm doing it like this - changeIndex = None al = len(line) for i in range(al): if changeIndex and (i < changeIndex): continue if line[i] == "{": nextRightBracket = line.find("}",i) #other stuff happens changeIndex = nextRightBracket + 1 As I said, pretty dumb question, so somewhat expecting to be told to read Alan's tutorial again. *grin* Regards, Liam Clarke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Silly loop question
Hehe, Yah, I should've read the tutorial again, I do apologise, it was a 3 in the morning-coffee not working question. Regards, Liam Clarke On 8/17/05, Alan G <[EMAIL PROTECTED]> wrote: > > changeIndex = None > > al = len(line) > > for i in range(al): > > And theres the problem. > Pythonprovides two loops, for is really a *foreach* so if you don't > want to process *each* item - eg by jumping forward - then you really > should use while and manage the index thataway. > > You can hack about with the indexin a for but basically its abusing > the intent of a for loop. Beter to explicitly hack about in a while > loop! > > So: > > while i < len(line): >if changeIndex and i < changeIndex > i = changeIndex > >if line[i] == "{": > nextRightBracket = line.find("}",i) > #other stuff happens > changeIndex = nextRightBracket + 1 >else: > i += 1 > > > read Alan's tutorial again. *grin* > > Umm, well the section on branching does refer to the two loops and > when > each is most appropriate, so... :-) > > Alan G > Author of the Learn to Program web tutor > http://www.freenetpages.co.uk/hp/alan.gauld > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Declaring encoding question
Hi all, I've got some source code which will be handling non-ASCII chars like umlauts and what not, and I've got a testing portion stored in the code. I get this deprecation warning when I run my code - __main__:1: DeprecationWarning: Non-ASCII character '\xfc' in file C:\Python24\testit.py on line 733, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details I'm reading this - http://www.python.org/peps/pep-0263.html Now, the non-ASCII character is in the test data, so it's not actually part of my code. Will Python be able to handle \xfc and company in data without my telling it to use a different form of encoding? When I run the code, and get my returned data, it looks like this in Pythonwin - >>> print j["landunits"].keys() ['"J\xe4ger"', '"Deutschmeister"', '"Army of Bohemia"', '"Gardegrenadiere"', '"K.u.K Armee"', '"Erzherzog"', '"Army of Italy"', '"Army of Silesia"', '"Army of Hungary"'] So J\xe4ger is actually Jäger. When I run it slightly differently - >>> for item in j["landunits"].keys(): ... print item ... "Jäger" "Deutschmeister" "Army of Bohemia" "Gardegrenadiere" "K.u.K Armee" "Erzherzog" "Army of Italy" "Army of Silesia" "Army of Hungary" It prints the umlauted 'a' fine and dandy. So, do I have to do anything to ensure that Python will continue to display the non-ASCII chars? I intend to have my output delivered via a Pythoncard/wxPython GUI, and I'm not sure if it's the Python interpreter displaying it properly, or Pythonwin making a special effort. Regards, Liam Clarke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting a list of lists aka nested lists
No-one's mentioned a Schwartzian Transform yet ; ) On 8/15/05, Kent Johnson <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > Note that in Python2.4+, you can use key= instead: > > > > def sortKey(lst): > > return lst[2] > > Quant.sort(key=sortKey) > > > > This is more effient than specifying a different comparison function, > > because > > the key function is only called once for each element. > > And instead of defining your own function you can use itertools.itemgetter() > which is intended for this purpose: > import itertools > Quant.sort(key=itertools.itemgetter(2)) > > Kent > > ___ > 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] UTf-8 to Entity
You'll need the csv module. And that should be about it, I think. The rest would just be using string methods. On 8/15/05, Diaz, Wendell <[EMAIL PROTECTED]> wrote: > > > > Hey guys, > > > > Hope you can help me on this. > > > > I want to make a python program which opens an XML (UTF-8 encoding) and do a > search & replace. It will search the Unicode and replace them with their > equivalent entity name. The program will read a look-up table (a tab > delimited text file) which list the unicodes that needs to be replace with > enity names. > > > > Example of the look-up table: > > > > &nobreakspace; [tab] 000A0 > > > > Thanks in advance, > > > > Wendell > > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] OT - email & imaplib modules
Hi all, Just musing. Even though I'm familiar with imaplib & IMAP, and most of the email modules, I still find them somewhat... obtuse to work with. The IMAP protocol is rather exposed in imaplib, and you have to read RFCs and fiddle to sort the format required for IMAP commands. For an example, I asked an acquaintance who's overjoyed at finding Ruby after working in PHP to show me how Ruby handled IMAP. Here's a Ruby line/Python line breakdown of how they compare. imap = Net::IMAP.new('mail.example.com') imap = imaplib.IMAP4('mail.example.com') imap.authenticate('LOGIN', 'joe_user', 'joes_password') imap.login('joe_user', 'joes_password') imap.select('INBOX') #switch to the inbox imap.select() #In Python it defaults to INBOX imap.search(["FROM","[EMAIL PROTECTED]"]).each do |message_id| #for each for message_id in imap.search(None, '(FROM "[EMAIL PROTECTED]")')[1][0].split(" "): response = imap.fetch(message_id, "RFC822.TEXT") #fetch the text response = imap.fetch(message_ID, "RFC822.TEXT") puts response.attr['RFC822.TEXT'] #print the text print response[1][0][1] #print the text imap.store(message_id, "+FLAGS", [:Deleted]) #flag to delete imap.store(message_id, "+FLAGS", "\Deleted") end #No Python equivalent imap.close #close mailbox, delete all flagged as delete as well (no need to imap.expunge) imap.close() imap.disconnect #close session imap.logout() I also find the nesting horrible to deal with. An imap fetch will return a tuple, where the last item is ')'. I know that this line - for message_id in imap.search(None, '(FROM "[EMAIL PROTECTED]")')[1][0].split(" ") isn't the clearest way to do it, but I was going for line for line. :) The email.Message object is also quite complex to learn. Now, I'm aware the both modules allow you to do incredibly complex things. But I'm of the opinion that simple things should be. I'm not sure what's being worked on for Py3K, but I really hope imaplib and email are. Would adding a layer over each to simplify things be bad or good? For instance, instead of IMAP using and returning IMAP specific strings, why not do stuff like imap.search("FROM", "[EMAIL PROTECTED]") and then let the object handle the brackets and quotes etc, and accept ranges of messages as a list, and why not have ranges returned as something iterable? Ditto with email.Message, how about methods to access email parts, as per the various RFC's? So calling msg.attachments() lists all attachments, or msg.format() shows what formatting the email is (text, RTF, HTML) , or msg.body() returns the BODY/TEXT portion? It's like Pythoncard, a layer of abstraction makes 90% of what you do easier, and the other 10% you can still do the manual way. I don't know, I'm also tempted to try and write those layers, if anyone would benefit from them. But yeah, would it be better or worse for people to not have to learn the niceties of RFC3501 formal syntax section to use imap? /end idle ponderings Regards, Liam Clarke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] making a table
Hi Goofball223, Just a quick thing - > for i in (0,10, 20, 30, 40, 50, 60, 70, 80, 90, 100): Have you used range() before? for i in range(10): print i 0 1 2 3 4 5 6 7 8 9 It's handy for situations like yours. Also, you could use it like this - zeroToNine = range(10) print zeroToNine [0,1,2,3,4,5,6,7,8,9] for i in zeroToNine: print i ..Just a quick mention that range(x) counts up to, but not including x. ( There's good reasons for this, which someone can explain if needed.) Also, by default it starts at zero, and increases by 1. You can change these, however. i.e. to create a list from 5 to 50 in increments of 5 - zeroToFifty = range(start = 5, stop = 55, step = 5) Remember, it'll count up to but not including stop, so if you tell it to count to two hundred in steps of 10, it'll count to 190, counting to 16 by steps of 2 will count to 14. So always set it one step value above what you want. (start defaults to 0, step defaults to 1, hence you can just use range(stop = 100) or range(100) for short.) You're looking good, however. Cheers, Liam Clarke On 9/9/05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > I would like to construct a table for my program but it does not seem to be > coming out evenly. Could someone please let me know what to do so that > everything will work out correctly? > > def main(): > print "This program shows a table of Celsius temperatures and there > Fahrenheit equivalents every 10 degrees from 0C to 100C" > print "C F" > for i in(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100): > fahrenheit = (9.0/5.0) * i + 32 > print i > print " ", > print fahrenheit > > main() > ___ > 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] How do fix this error?
raw_input takes one argument. While >>> x = 5 >>> b = 10 >>> print x, "+", b, "=", x+b will print 5 + 10 = 15, you could also have written it - >>>print str(x) + " + " + str(b) + " = " + str(x+b) Now, if I have a function - def divByTen(number): return number/10 I can call it like so - >>>divByTen(100) 10 or >>>divByTen(50+50) 10 As Python will resolve 50+50 first. However, >>>divByTen(50,50) will produce this error - TypeError: divByTen expected at most 1 arguments, got 2 See where I'm going with this? One more thing, the comma only works for the print command. On 9/17/05, Poor Yorick <[EMAIL PROTECTED]> wrote: > Nathan Pinno wrote: > > > guess = float(raw_input(a," + ",b," = ")) > > TypeError: [raw_]input expected at most 1 arguments, got 4 > > > > > guess = float(raw_input(a," + ",b," = ")) > > return answer, guess > > > hint: how many arguments are you passing to raw_input here? > > -- > Poor Yorick > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Using new style classes and __slots__
Hi all, I'm just looking for a quick runthrough on the differences between new style classes and the old ones, and how to use the new ones. Also, how exactly do you use __slots__? I've got a 6000 object collection, and apparently using __slots__ will save memory, but only those attributes specified in __slots__ can be set, which suits my project fine. Is anyone able to point me in the right direction? Much thanks, Liam Clarke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using new style classes and __slots__
Hi Kent, > > >>> class foo(object): > ... __slots__ = ['x', 'y'] > ... > >>> f=foo() > >>> f.x=1 > >>> f.y=2 > >>> f.z=3 > Traceback (most recent call last): > File "", line 1, in ? > AttributeError: 'foo' object has no attribute 'z' > > Take a look at > http://www.python.org/2.2.3/descrintro.html (search for __slots__) Thanks for that. I was tearing my hair out trying to figure out why despite the usage of __slots__ my instances still had a __dict__, and then I saw the subclass of object, and sure enough... I'm guessing that's how you get new style classes. Quite like what I'm seeing, __slots__ and that property() thing will allow me to remove all my __setattr__ and __getattr__ methods, which tend to make things crawl a wee bit. I was almost considering writing setters/getters to get around the magic methods, but I grew to dislike them after having used Java, I got annoyed at having to write setters/getters for every public variable. Of course, I was using Notepad to write the Java programmes, and then Eclipse helped me realise why Java programmers value IDE's so much, it's that automatic generation of set/gets... > Note to the list: Use of __slots__ is generally discouraged, but IIUC this is > exactly the >use case it was designed for so I think it is OK. Throwing an error on adding a new attribute is just what I need as the data I'm reading/writing has a very specific format; instead of using __setattr__ and checking that the attribute already existed, I can just let __slots__ do the work. Out of curiosity, if you're using __slots__ with new style, you no longer have __dict__. So, where does it stick data? I was using 'self.__dict__["_created"] = False' to set a flag at the start of __init__ to stop __setattr__ picking up the attributes being set from parsed data (and running all my type checks, etc.), but I won't need that anymore. I could almost hug property(). :) All in all, this has been an interesting tour around the inner workings of a class in Python. Give it another five years, I might be able to read one of those metaclass explanations and understand it. Regards, Liam Clarke PS Just for my edification, is there a builtin to determine how much memory is allocated to an object? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor