[Tutor] Search theory for luddites?
Hi all, Going to build a repository for my code snippets (and I know that pre-existing solutions exist, but I like to roll my own to see what I can learn.), and just wondering on how to best store it and search it. My current planned approach is to have each file added read and keywords extracted (i.e. variable names, libraries imported etc.) and used as keys in a dictionary, and each key points to a list of files containing it (files will be stored in a zip or similar). i.e. words = { imaplib: [X.py], smtplib: [X.py, Y.py] } And if I do a search for smtplib or imaplib it'll return X.py first, as it contains both, and Y.py second. Is there a more efficient approach? I don't think a relational DB really suits this sort of task at present, but I'm wondering about search times. I've googled for search theory, and hit c2's wiki up, but I'm having no luck at finding applicable, understandable theory... a lot of stuff about trees and nodes and paths through graphs, but I can't correlate that to my problem. (I can see how graph pathfinding could relat e to routing a connection over a network, though.) Can anyone recommend a good introduction to the theory of searching? I really need to take some Computer Science courses. Regards, Liam Clarke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Command line arguments passing
My question is: when invoking a program with, let's say, a filename containing spaces as a parameter: myprog -file Long name What does sys.argv hold in this case? What did you get when you tried it? I ran: ###test.py ## import sys print sys.argv ### python test.py foo long name bar and got ['testargv.py', 'foo', 'long name', 'bar'] What did you get? Since its much less typing to try it than to ask the question I assume you must have had a problem with it? Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld and got I am specifically interested in whether argv[2]==\Long or argv[2]==Long name, that is, if the shell does the processing or I need to do it in the program. Also, I need to know if most environments do the same (I wouldn't want to be caught pants down while porting this to Windows). Many thanks in advance for your valuable suggestions and apologies if I have misposted, Vlad ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Command line arguments passing
On Sat, 2005-12-03 at 17:23 -0800, Danny Yoo wrote: My question is: when invoking a program with, let's say, a filename containing spaces as a parameter: myprog -file Long name What does sys.argv hold in this case? I am specifically interested in whether argv[2]==\Long or argv[2]==Long name, Hi Vlad, What you're asking is a platform-specific thing. I believe it should do what you're expecting --- Long name should be a pulled together as a single argument in sys.argv. But it's not Python that's pulling Long name together: it's your operating system's command line shell that's doing this. For example, on Windows, the following pages: http://www.microsoft.com/technet/community/columns/scripts/sg0704.mspx http://www.microsoft.com/technet/archive/winntas/deploy/shellscr.mspx talk about how Windows does command line argument parsing. (Search those pages for the word quote, and you'll see a mention of this.) And the details on the role of quoting arguments is simliar for Unix shells like 'bash' or 'tcsh'. For example, for the bash shell: http://www.gnu.org/software/bash/manual/bashref.html#SEC8 So all Python knows is that it's getting an array of strings: it doesn't even see the original line that the user typed at the command line prompt; it instead gets something that has already been partially digested by your command line shell. Hope this helps! Thanks, that's what I was looking for -- a multiplatform reference, since I can't test scripts on Windows just yet. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Search theory for luddites?
Is there a more efficient approach? I don't think a relational DB really suits this sort of task at present, Why not? If its a big data set then a proper database will be more effective. The advantages are that you can define many more parameters for searching - author, date, number of accesses, version, etc Also it allows easy use of wildcards and ranges in your searches. And search times will be more close to constant for large volumes. It will also make generating stats for your own use easier. If you keep the structure simple and only store paths/filenames rather than trying to store the files then a relational database seems perfectly reasonable. Another approach to consider would be an LDAP Directory, but I'd go with a database for this one if volumes are to grow to more than a few hundred items. Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] my text adventure
thanks. i had actually coded this almost exactly the same. i'll try to make my questions more specific. i am able to pickle and restore world. which is a dictionary of coordinates : room objects. when i look at the savefile that pickle generates i can see all my descriptions and exits. however when i reload my world gets back all the rooms that were created with dig. but the rooms don't have their exits or descriptions. is pickle the right tool for this? can i pickle.dump more than one thing to the same savefile? or how could i go about creating a structure that holds room, coords, descriptions and exits? and then pickle and unpickle that. or would it be better to write my own functions to write everything to a file and not use pickle at all? eventually my program will have many more things to keep track of and i am thinking that the saving and restoring of a programs state (is that correct usage?) could be important in many different programs. - Original Message - From: Dan Lowe To: david Cc: tutor@python.org Sent: Sunday, December 04, 2005 1:24 AM Subject: Re: [Tutor] my text adventure On Dec 3, 2005, at 9:40 PM, david wrote: sorry i forgot a subject line. i have looked at the pickle module and was able to pickle world. but i can't figure how to restore everything. import pickle def save_game(state, filename): file = open(filename, 'w') pickle.dump(state, file) file.close() def load_game(filename): file = open(filename, 'r') state = pickle.load(file) file.close() return state save_game(world, 'mygame') world = load_game('mygame') -- logic (n.): the art of being wrong with confidence. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] spam, eggs, and my text adventure
david wrote: hello :) i have some questions that are more about programming in general than python specifically. suppose, for whatever reason, you had a burning desire to write a simple text adventure. how would you start? would you just start writing some code? Alan G and I had a brief discussion about this recently - see the thread that starts here: http://mail.python.org/pipermail/tutor/2005-November/043602.html i knew i wanted a room structure or object and that i wanted to be able to create the rooms from within the game. so i thought i will keep track of rooms in a dictionary and use xy coordinates as keys and rooms as values. and rooms can keep track of their exits in a dictionary too. then all i would have to do is write some code to create rooms and move through them. anyway i am stuck so please find a question in there somewhere. So actually you didn't just start writing code, you thought about the objects and data structures you might need to use to solve the problem before you started writing code. That's pretty much what I do too. For simple scripts incremental development can work well - just write a little code, run it to see if it works, repeat. This works well if - running the script provides immediate feedback so you know whether it is working or not (this is not true of your adventure game) - the script is not likely to get big enough to require significant refactoring (reorganization) in its lifetime (this is hard to know when you start but generally it applies to one-off scripts and very simple utilities) For any other kind of program, I find a test-driven approach works very well, especially when I don't have a good understanding of the problem or how to solve it. I think of a small part of the problem that I understand, write a test for the code that will solve it, and run the test. Of course the test fails, I haven't written the code yet! So I write the code to make the test pass. Frequently reassess whether the code I have written is the best code I could write to solve the problem *as I currently understand it* as expressed in the current tests. Repeat until done. James Shore recently described this process well in his blog: http://www.jamesshore.com/Blog/Red-Green-Refactor.html (though I think Michael Feather's guidelines are too strict) By the way, for anyone reading who hasn't tried TDD (test-driven development), I really recommend you do. For me it was a profound shift to a way of working that is productive and satisfying. The constant feedback of working tests is a clear indication that I have accomplished something. At the end of a coding session I have great confidence that I have created working code - I have tested every part of it many times. At the end of a project, when I deliver to QA, I have great confidence that I have created working code - I have tested every part of it many times! And it's fun - the constant feedback of yes, it works...yes, it works is like having someone constantly saying, that's good. Being able to refactor without fear is priceless. Until I tried TDD I didn't realize how much I was programming in fear of breaking something. Without automated tests, it is easy to break something. Whenever I wanted to refactor something I had to weigh the benefits of the refactoring against the chance of breaking something. Now I just make the change and run the tests to see if I broke anything, then go and fix it. Uncle Bob Martin has a great article that describes some of the benefits: http://www.butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Command line arguments passing
I don't have Python on Windows and didn't want to make any assumptions about the way the Windows shell passes parameters. Sorry again for being trivial. On Sun, 2005-12-04 at 09:00 +, Alan Gauld wrote: My question is: when invoking a program with, let's say, a filename containing spaces as a parameter: myprog -file Long name What does sys.argv hold in this case? What did you get when you tried it? I ran: ###test.py ## import sys print sys.argv ### python test.py foo long name bar and got ['testargv.py', 'foo', 'long name', 'bar'] What did you get? Since its much less typing to try it than to ask the question I assume you must have had a problem with it? Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld and got I am specifically interested in whether argv[2]==\Long or argv[2]==Long name, that is, if the shell does the processing or I need to do it in the program. Also, I need to know if most environments do the same (I wouldn't want to be caught pants down while porting this to Windows). Many thanks in advance for your valuable suggestions and apologies if I have misposted, Vlad ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Unicode trouble
Might the problem only be related to Win32com, not Python since Python prints it without trouble? That's another issue. First you need to know what you are starting with. You really should read this: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) http://www.joelonsoftware.com/articles/Unicode.html Kent Thanks a lot for your help. I did actually get it to work. It didn't have to do with the characters, but the flags that I set for Word. But, I did learn a few things about characters in the process as well -- This email has been scanned for viruses spam by Decna as - www.decna.no Denne e-posten er sjekket for virus spam av Decna as - www.decna.no ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] (no subject)
i added a list of rooms and rooms.append(self) to the Room initialization. then i run my program and create one room. there should now be two rooms. when i look at rooms i have three rooms! where did this other room come from? anyway, thanks for taking time to look at my code. import sysimport stringimport pickleimport os.path world = {}rooms = []class Room: def __init__(self,coords): self.contents = [] self.description = '' self.coords = coords world[tuple(coords)] = self rooms.append(self) self.exits = {} def nextdoor(self,direction): if direction == 'n': nextdoor = (self.coords[0], self.coords[1] + 1) return list(nextdoor) elif direction == 's': nextdoor = list((self.coords[0], self.coords[1] - 1)) return nextdoor elif direction == 'e': nextdoor = list((self.coords[0] +1, self.coords[1])) return nextdoor elif direction == 'w': nextdoor = (self.coords[0] -1, self.coords[1]) return list(nextdoor) class Player: def __init__(self,name): self.name = name self.location = None self.inventory = [] self.wielded = None def look(self): print self.location.coords print self.location.description def move(self,direction): type(direction) if self.location.exits.has_key(direction): self.location = self.location.exits[direction] else: print 'alas, you cannot go that way' def wield(self,what): self.wielded = what def wear(self,what): pass def take(self,what): pass def drop(self,what): pass def dig(self,direction): target = tuple(self.location.nextdoor(direction)) print target if self.location.exits.has_key(target): print 'there is already an exit to that room' elif world.has_key(target): print 'already a room there, attempt to make exits' self.location.exits[direction] = Room(target) world[target].exits[opdir(direction)] = self.location else: world[target]=Room(target) self.location.exits[direction] = Room(target) world[target].exits[opdir(direction)] = self.location def describeroom(self): self.location.description = raw_input('') def save(self): f = open('savefile', 'w') pickle.dump(world,f) f.close() def do(self): cmd = string.split(raw_input('')) verb = cmd[0] if len(cmd) 1: target = cmd[1] if verb == 'l': self.look() elif verb in ['n','s','e','w']: self.move(verb) elif verb == 'quit': sys.exit() elif verb == 'i': for a in self.inventory: print a.name elif verb == 'dig': self.dig(target) elif verb == 'dr': self.describeroom() elif verb == 'save': self.save() else: print 'what?' class Thing: def __init__(self,name): self.name = name def opdir(direction): if direction == 'n': return 's' if direction == 's': return 'n' if direction == 'e': return 'w' if direction == 'w': return 'e' p = Player('david')room1 = Room([0,0]) p.location = room1sword = Thing('sword')hat = Thing('hat')p.inventory.append(sword)p.inventory.append(hat) if os.path.isfile('savefile'): f = open('savefile','r') world = pickle.load(f) f.close() while 1: p.do()else: while 1: p.do() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] my text adventure
i fixed this myself ! i think i can get everything working now so please disregard previous messages. if you haven't already. :) - Original Message - From: david To: tutor@python.org Sent: Sunday, December 04, 2005 10:06 AM Subject: [Tutor] (no subject) i added a list of rooms and rooms.append(self) to the Room initialization. then i run my program and create one room. there should now be two rooms. when i look at rooms i have three rooms! where did this other room come from? anyway, thanks for taking time to look at my code. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
then i run my program and create one room. there should now be two rooms. when i look at rooms i have three rooms! where did this other room come from? Dunno but have uyou tried asking it about itself using the debugger? Call the description methjod or look at the coordinates... One wee observation: rooms = [] class Room: def __init__(self,coords): self.contents = [] self.description = '' self.coords = coords world[tuple(coords)] = self rooms.append(self) self.exits = {} I'd either make rooms a class variable which can be accessed from outside the class with Room.rooms or I'd make the append operation a responsibility of the calling client. Otherwise every time you try to use the Room class in another program you will need to create a rooms global variable. Since the purpose of rooms is to keep a register of all the rooms in existence I'd argue it should be a class variable. Just a matter of style it doesn't really affect the operation here. And certainly doesn't fix your extra room problem! Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Search theory for luddites?
My current planned approach is to have each file added read and keywords extracted (i.e. variable names, libraries imported etc.) and used as keys in a dictionary, and each key points to a list of files containing it (files will be stored in a zip or similar). i.e. words = { imaplib: [X.py], smtplib: [X.py, Y.py] } And if I do a search for smtplib or imaplib it'll return X.py first, as it contains both, and Y.py second. Is there a more efficient approach? I don't think a relational DB really suits this sort of task at present, but I'm wondering about search times. Hi Liam, That sounds fine, and it's a popular approach. What you have there is known as an inverted index: http://www.nist.gov/dads/HTML/invertedIndex.html and it's the way that many search engines know how to link up keywords to their respective documents. I've googled for search theory, and hit c2's wiki up, but I'm having no luck at finding applicable, understandable theory... a lot of stuff about trees and nodes and paths through graphs, but I can't correlate that to my problem. There's a really short-and-sweet book called Understanding Search Engines: Mathematical Modeling and Text Retrieval that covers some core ideas: http://www.cs.utk.edu/~berry/usebook/ I'd heartily recommend that one; they do talk about the theory, but from a practitioner's point of view, so it's quite readable. And it is very thin and easy to carry. *grin* ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] my text adventure
On Sun, 4 Dec 2005, david wrote: i fixed this myself ! i think i can get everything working now so please disregard previous messages. if you haven't already. :) That's great! By the way, you asked a while back if trying to write a text-adventure game was a good way to learn how to program: I think you're finding the answer to be: Yes. *grin* ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Socket connection
I need to get some whois-info from whois.ripe.net. I have been able to connect and get the info that I need, but I would like to know what is the most 'polite' way of doing so. I need to do quite a few whois lookups (from my server logs) and don't want to risk creating more hassle for their server than necessary. So, what I would like to know, is what is the best way of doing lots of requests? Hello, It's really specific to the protocol: a protocol will define if it's ok or not to send multiple requests per connection. According to RFC 3912: http://www.rfc-editor.org/rfc/rfc3912.txt here is their high-level overview of the WHOIS protocol: 2. Protocol Specification A WHOIS server listens on TCP port 43 for requests from WHOIS clients. The WHOIS client makes a text request to the WHOIS server, then the WHOIS server replies with text content. All requests are terminated with ASCII CR and then ASCII LF. The response might contain more than one line of text, so the presence of ASCII CR or ASCII LF characters does not indicate the end of the response. The WHOIS server closes its connection as soon as the output is finished. The closed TCP connection is the indication to the client that the response has been received. So unfortunately it does look like you'll have to reestablish the connection after each request: the whois protocol as defined here doesn't appear to allow multiple requests per connection. Hope this helps! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] my text adventure, saving and restoring
when i restore from the pickle i can see my exits and descriptions are still there. but look won't see the description and move can't move to the next room. i am stumped. what is going on here? how can i fix it? please help? IDLE 1.1.2 No Subprocess rooms[__main__.Room instance at 0x00E08EB8] {}world{(0, 0): __main__.Room instance at 0x00E08EB8}drstartrooml[0, 0]startroomdig n(0, 1)ndrnextroomrooms[__main__.Room instance at 0x00E08EB8, __main__.Room instance at 0x00E7BFD0]startroom{'n': __main__.Room instance at 0x00E7BFD0}nextroom{'s': __main__.Room instance at 0x00E08EB8}world{(0, 1): __main__.Room instance at 0x00E7BFD0, (0, 0): __main__.Room instance at 0x00E08EB8}savequitTraceback (most recent call last): File "C:\Documents and Settings\david\Desktop\t.py", line 150, in ? p.do() File "C:\Documents and Settings\david\Desktop\t.py", line 93, in do sys.exit()SystemExit rooms[__main__.Room instance at 0x00E13F30, __main__.Room instance at 0x00E13698]startroom{'n': __main__.Room instance at 0x00E13698}nextroom{'s': __main__.Room instance at 0x00E13F30}world{(0, 1): __main__.Room instance at 0x00E13FA8, (0, 0): __main__.Room instance at 0x00E132B0}nalas, you cannot go that wayl[0, 0] import sysimport stringimport pickleimport os.path world = {}rooms = []class Room: def __init__(self,coords): self.contents = [] self.description = '' self.coords = coords world[tuple(coords)] = self rooms.append(self) self.exits = {} def nextdoor(self,direction): if direction == 'n': nextdoor = (self.coords[0], self.coords[1] + 1) return list(nextdoor) elif direction == 's': nextdoor = list((self.coords[0], self.coords[1] - 1)) return nextdoor elif direction == 'e': nextdoor = list((self.coords[0] +1, self.coords[1])) return nextdoor elif direction == 'w': nextdoor = (self.coords[0] -1, self.coords[1]) return list(nextdoor) class Player: def __init__(self,name): self.name = name self.location = None self.inventory = [] self.wielded = None def look(self): print self.location.coords print self.location.description def move(self,direction): type(direction) if self.location.exits.has_key(direction): self.location = self.location.exits[direction] else: print 'alas, you cannot go that way' def wield(self,what): self.wielded = what def wear(self,what): pass def take(self,what): pass def drop(self,what): pass def dig(self,direction): target = tuple(self.location.nextdoor(direction)) print target if self.location.exits.has_key(target): print 'there is already an exit to that room' elif world.has_key(target): print 'already a room there, attempt to make exits' self.location.exits[direction] = world[target] world[target].exits[opdir(direction)] = self.location else: world[target]=Room(target) self.location.exits[direction] = world[target] world[target].exits[opdir(direction)] = self.location def describeroom(self): self.location.description = raw_input('') def save(self): f = open('savefile', 'w') pickle.dump(world,f) pickle.dump(rooms,f) for i in rooms: pickle.dump(i,f) f.close() def do(self): cmd = string.split(raw_input('')) verb = cmd[0] if len(cmd) 1: target = cmd[1] if verb == 'l': self.look() elif verb in ['n','s','e','w']: self.move(verb) elif verb == 'quit': sys.exit() elif verb == 'i': for a in self.inventory: print a.name elif verb == 'dig': self.dig(target) elif verb == 'dr': self.describeroom() elif verb == 'save': self.save() elif verb == 'world': print world elif verb == 'rooms': print rooms for i in rooms: print i.description print i.exits else: print 'what?' class Thing: def __init__(self,name): self.name = name def opdir(direction): if direction == 'n': return 's' if direction == 's': return 'n' if direction == 'e': return 'w' if direction == 'w': return 'e' p = Player('david')room1 = Room([0,0]) p.location = room1sword = Thing('sword')hat = Thing('hat')p.inventory.append(sword)p.inventory.append(hat) if os.path.isfile('savefile'): f = open('savefile','r') world = pickle.load(f) rooms = pickle.load(f) for i in rooms: i = pickle.load(f) f.close() while 1: p.do()else: while 1: p.do() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] script run problem
Hi everyone, I am not able to run any script in python ver2.4.2. under window xp.I get the following error messagepython.exe has encountered a problem and needs to be closed. Though path for python.exe is correct.Error meeasge generated by MS office is ?xml version=1.0 encoding=UTF-16? DATABASE EXE NAME=python.exe FILTER=GRABMI_FILTER_PRIVACY MATCHING_FILE NAME=python.exe SIZE=4608 CHECKSUM=0xA079BCF1 MODULE_TYPE=WIN32 PE_CHECKSUM=0x0 LINKER_VERSION=0x0 LINK_DATE=09/28/2005 10:41:14 UPTO_LINK_DATE=09/28/2005 10:41:14 / MATCHING_FILE NAME=pythonw.exe SIZE=5120 CHECKSUM=0x9B4108F MODULE_TYPE=WIN32 PE_CHECKSUM=0x0 LINKER_VERSION=0x0 LINK_DATE=09/28/2005 10:41:17 UPTO_LINK_DATE=09/28/2005 10:41:17 / MATCHING_FILE NAME=Removepywin32.exe SIZE=61440 CHECKSUM=0x538175D5 MODULE_TYPE=WIN32 PE_CHECKSUM=0x0 LINKER_VERSION=0x0 LINK_DATE=02/03/2005 20:46:47 UPTO_LINK_DATE=02/03/2005 20:46:47 / MATCHING_FILE NAME=w9xpopen.exe SIZE=4608 CHECKSUM=0xF6BCA328 MODULE_TYPE=WIN32 PE_CHECKSUM=0x0 LINKER_VERSION=0x0 LINK_DATE=09/28/2005 10:41:15 UPTO_LINK_DATE=09/28/2005 10:41:15 / MATCHING_FILE NAME=DLLs\tcl84.dll SIZE=568320 CHECKSUM=0x2FBB71CD BIN_FILE_VERSION=8.4.2.7 BIN_PRODUCT_VERSION=8.4.2.7 PRODUCT_VERSION=8.4.7 FILE_DESCRIPTION=Tcl DLL COMPANY_NAME=ActiveState Corporation PRODUCT_NAME=Tcl 8.4 for Windows FILE_VERSION=8.4.7 ORIGINAL_FILENAME=tcl84.dll LEGAL_COPYRIGHT=Copyright © 2001 by ActiveState Corporation, et al VERFILEDATEHI=0x0 VERFILEDATELO=0x0 VERFILEOS=0x4 VERFILETYPE=0x2 MODULE_TYPE=WIN32 PE_CHECKSUM=0x99001 LINKER_VERSION=0x0 UPTO_BIN_FILE_VERSION=8.4.2.7 UPTO_BIN_PRODUCT_VERSION=8.4.2.7 LINK_DATE=08/30/2004 09:00:40 UPTO_LINK_DATE=08/30/2004 09:00:40 VER_LANGUAGE=English (United States) [0x409] / MATCHING_FILE NAME=DLLs\tclpip84.dll SIZE=5120 CHECKSUM=0xB489B43B MODULE_TYPE=WIN32 PE_CHECKSUM=0xE7AF LINKER_VERSION=0x0 LINK_DATE=08/30/2004 09:00:40 UPTO_LINK_DATE=08/30/2004 09:00:40 / MATCHING_FILE NAME=DLLs\tix8184.dll SIZE=274432 CHECKSUM=0x3CFBDE17 MODULE_TYPE=WIN32 PE_CHECKSUM=0x43E90 LINKER_VERSION=0x0 LINK_DATE=08/31/2004 16:12:05 UPTO_LINK_DATE=08/31/2004 16:12:05 / MATCHING_FILE NAME=DLLs\tk84.dll SIZE=1031168 CHECKSUM=0x9584A8F2 BIN_FILE_VERSION=8.4.2.7 BIN_PRODUCT_VERSION=8.4.2.7 PRODUCT_VERSION=8.4.7 FILE_DESCRIPTION=Tk DLL COMPANY_NAME=ActiveState Corporation PRODUCT_NAME=Tk 8.4 for Windows FILE_VERSION=8.4.7 ORIGINAL_FILENAME=tk84.dll LEGAL_COPYRIGHT=Copyright © 2001 by ActiveState Corporation, et al VERFILEDATEHI=0x0 VERFILEDATELO=0x0 VERFILEOS=0x4 VERFILETYPE=0x2 MODULE_TYPE=WIN32 PE_CHECKSUM=0x109DFC LINKER_VERSION=0x0 UPTO_BIN_FILE_VERSION=8.4.2.7 UPTO_BIN_PRODUCT_VERSION=8.4.2.7 LINK_DATE=08/30/2004 09:04:16 UPTO_LINK_DATE=08/30/2004 09:04:16 VER_LANGUAGE=English (United States) [0x409] / MATCHING_FILE NAME=Lib\distutils\command\wininst-6.exe SIZE=61440 CHECKSUM=0xA53DA7E0 MODULE_TYPE=WIN32 PE_CHECKSUM=0x0 LINKER_VERSION=0x0 LINK_DATE=02/03/2005 20:40:38 UPTO_LINK_DATE=02/03/2005 20:40:38 / MATCHING_FILE NAME=Lib\distutils\command\wininst-7.1.exe SIZE=61440 CHECKSUM=0x916D9F80 MODULE_TYPE=WIN32 PE_CHECKSUM=0x0 LINKER_VERSION=0x0 LINK_DATE=02/03/2005 20:41:15 UPTO_LINK_DATE=02/03/2005 20:41:15 / MATCHING_FILE NAME=Lib\site-packages\isapi\PyISAPI_loader.dll SIZE=40960 CHECKSUM=0xFC84C01A BIN_FILE_VERSION=2.4.205.0 BIN_PRODUCT_VERSION=2.4.205.0 PRODUCT_VERSION=2.4.205.0 FILE_DESCRIPTION= COMPANY_NAME= PRODUCT_NAME=Pywin32 FILE_VERSION=2.4.205.0 ORIGINAL_FILENAME=PyISAPI_loader.dll INTERNAL_NAME=PyISAPI_loader.dll LEGAL_COPYRIGHT= VERFILEDATEHI=0x0 VERFILEDATELO=0x0 VERFILEOS=0x40004 VERFILETYPE=0x2 MODULE_TYPE=WIN32 PE_CHECKSUM=0x0 LINKER_VERSION=0x0 UPTO_BIN_FILE_VERSION=2.4.205.0 UPTO_BIN_PRODUCT_VERSION=2.4.205.0 LINK_DATE=10/24/2005 08:49:34 UPTO_LINK_DATE=10/24/2005 08:49:34 VER_LANGUAGE=English (United States) [0x409] / MATCHING_FILE NAME=Lib\site-packages\pythonwin\Pythonwin.exe SIZE=15360 CHECKSUM=0x62FF0184 BIN_FILE_VERSION=2.4.205.0 BIN_PRODUCT_VERSION=2.4.205.0 PRODUCT_VERSION=2.4.205.0 FILE_DESCRIPTION= COMPANY_NAME= PRODUCT_NAME=Pywin32 FILE_VERSION=2.4.205.0 ORIGINAL_FILENAME=Pythonwin.exe INTERNAL_NAME=Pythonwin.exe LEGAL_COPYRIGHT= VERFILEDATEHI=0x0 VERFILEDATELO=0x0 VERFILEOS=0x40004 VERFILETYPE=0x1 MODULE_TYPE=WIN32 PE_CHECKSUM=0x0 LINKER_VERSION=0x0 UPTO_BIN_FILE_VERSION=2.4.205.0 UPTO_BIN_PRODUCT_VERSION=2.4.205.0 LINK_DATE=10/24/2005 08:49:38 UPTO_LINK_DATE=10/24/2005 08:49:38 VER_LANGUAGE=English (United States) [0x409] / MATCHING_FILE NAME=Lib\site-packages\pythonwin\scintilla.dll SIZE=247808 CHECKSUM=0xA219BADB BIN_FILE_VERSION=1.5.6.0 BIN_PRODUCT_VERSION=1.5.6.0 PRODUCT_VERSION=1.56 FILE_DESCRIPTION=Scintilla.DLL - a Source Editing Component COMPANY_NAME=Neil Hodgson [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] PRODUCT_NAME=Scintilla FILE_VERSION=1.56 ORIGINAL_FILENAME=Scintilla.DLL INTERNAL_NAME=Scintilla LEGAL_COPYRIGHT=Copyright
Re: [Tutor] my text adventure, saving and restoring
On 05/12/05, david [EMAIL PROTECTED] wrote: when i restore from the pickle i can see my exits and descriptions are still there. def save(self): f = open('savefile', 'w') pickle.dump(world,f) pickle.dump(rooms,f) for i in rooms: pickle.dump(i,f) f.close() Hi David, You appear to be saving your information multiple times. 'world' is a dictionary with Room instances as its values. Those same Room instances occur in 'rooms'. So, when you run your save method, python does this: 1. Write information to the pickle file telling python to construct a dictionary. This includes telling python how to construct the Room instances. 2. Write information to the pickle file telling python to construct a list. This includes telling python how to construct the Room instances. 3. For each Room instance, write information to the pickle file telling python how to construct it. Now, when you unpickle the information, python does this: 1. Build a dictionary to be the world. Build all the Rooms (because the Rooms were the values of this dictionary). 2. Build a list of Rooms. Build all the Rooms (because the Rooms were the contents of this list). 3. Build all the Rooms again, and do nothing with them. Initially, your Room isntances in world were the same as the Rooms in rooms. But after unpickling, python builds the Rooms separately for each data structure, and so they end up different. Let me try some ASCII art: [view this in a monospaced font, eg, by cutting-and-pasting to Notepad] Before: world: (0,0) |- Room0; (0,1) |- Room1; (1,1) |- Room2 /--//---/ /--/ / / / /--\/--\/--\ | Room || Room || Room | |object||object||object| \--/\--/\--/ // /---//--/ rooms: [ RoomA, RoomB, RoomC ] After: world: (0,0) |- Room0; (0,1) |- Room1; (1,1) |- Room2 /--//---/ /--/ / / / /--\/--\/--\ | Room || Room || Room | |object||object||object| \--/\--/\--/ rooms: [ RoomA, RoomB, RoomC ] \\ \---\\--\ /--\/--\/--\ | Room || Room || Room | |object||object||object| \--/\--/\--/ You could make your save() method a lot simpler; something like this: def save(self): f = open('savefile', 'w') pickle.dump(world,f) f.close() This is all you need, because 'world' contains all the information about your world. Then, when you load the data back, you will need to figure out some way of building your 'rooms' data structure from 'world'. Does this help? -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Words alignment tool
Srinivas Iyyer wrote: Dear Expert programmers, I aplogise if this mail is out of context here. I have a list of elements like these: Contr1 SPR-10 SPR-101 SPR-125 SPR-137 SPR-139 SPR-143 contr2 SPR-1 SPR-15 SPR-126 SPR-128 SPR-141 SPR-148 contr3 SPR-106 SPR-130 SPR-135 SPR-138 SPR-139 SPR-145 contr4 SPR-124 SPR-125 SPR-130 SPR-139 SPR-144 SPR-148 There are several common elements prefixed with SPR-. Although these elements are sorted in asecending order row wise, the common elements are difficult to spot. One has to look for common elements by eyeballing. It would be wonderful if these elements are aligned properly by inserting gaps. I think this is much easier than the bioinformatics problem because your sequence elements are unique and sorted, and you don't have very much data. One approach is to create pairs that look like ('SPR-10', 'Contr1') for all the data. These pairs can be put into one big list and sorted, then grouped by the first element to get what you want. Python 2.4 has the groupby() function which makes it easy to do the grouping. For example: data = '''Contr1SPR-10 SPR-101 SPR-125 SPR-137 SPR-139 SPR-143 contr2 SPR-1 SPR-15 SPR-126 SPR-128 SPR-141 SPR-148 contr3 SPR-106 SPR-130 SPR-135 SPR-138 SPR-139 SPR-145 contr4 SPR-124 SPR-125 SPR-130 SPR-139 SPR-144 SPR-148'''.splitlines() import itertools, operator pairs = [] # This will be a list of all the pairs like ('SPR-10', 'Contr1') for line in data: items = line.split() name, items = items[0], items[1:] # now name is the first item on the line, items is a list of all the rest # add the pairs for this line to the main list pairs.extend( (item, name) for item in items) pairs.sort() # Sort the list to bring the first items together # groupby() will return a sequence of key, group pairs where the key is the # first element of the group for k, g in itertools.groupby(pairs, operator.itemgetter(0)): print k, [ name for item, name in g ] The output of this program is SPR-1 ['contr2'] SPR-10 ['Contr1'] SPR-101 ['Contr1'] SPR-106 ['contr3'] SPR-124 ['contr4'] SPR-125 ['Contr1', 'contr4'] SPR-126 ['contr2'] SPR-128 ['contr2'] SPR-130 ['contr3', 'contr4'] SPR-135 ['contr3'] SPR-137 ['Contr1'] SPR-138 ['contr3'] SPR-139 ['Contr1', 'contr3', 'contr4'] SPR-141 ['contr2'] SPR-143 ['Contr1'] SPR-144 ['contr4'] SPR-145 ['contr3'] SPR-148 ['contr2', 'contr4'] SPR-15 ['contr2'] Converting this to a horizontal display is still a little tricky but I'll leave that for you. I should probably explain more about groupby() and itemgetter() but not tonight... Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Problems padding a string with 0's while deconcatonating
Hello everyone, this is my first time using the tutor e-mail, so if I mess up any common format or something just let me know :). Alright, for a little background, I'm in the process of writing a merkle-hellman cryptosystem for my CS15 class. I'm doing fine till I get to the decatonation of the long binary string into n-strings which will then be used to match up to the public key. I have the following code: def binaryString(b, n): s=[] j=0 while j len(b): temp = b[j:j+n] s = s + [ temp ] j = j + n return s which works for all intents and purposes, but I can't figure out a way to get it to pad the left of the string with zeros so that each of the subsets is n bits long. As is, it will deconcatonate a given string into n-substrings, but not all of the substrings are n bits long. Any help would be greatly appreciated and sorry again if I didn't include anything I should. -Josh ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor