[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
[Tutor] Socket connection
Hello. 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? This works like a dream: >>> from socket import * >>> import socket >>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) >>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) >>> s.connect(('whois.ripe.net', 43)) >>> s.send("%s \n\n" % 'vg.no') 14 >>> data = s.recv(8196) But if I thereafter do this: >>> s.send("%s \n\n" % 'dagbladet.no') 11 >>> data = s.recv(8196) Traceback (most recent call last): File "", line 1, in ? error: (10053, 'Software caused connection abort') It doesn't go quite as well. I saw on http://www.amk.ca/python/howto/sockets/ that the socket is supposed to get destroyed after one use. But, if I try to connect again: >>> s.connect(('whois.ripe.net', 43)) Traceback (most recent call last): File "", line 1, in ? File "", line 1, in connect error: (10056, 'Socket is already connected') It is already connected. So it seems like I have to start again at the beginning: >>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) >>> s.connect(('whois.ripe.net', 43)) .. But, I have 300 requests. Should I do this 300 times? And thereafter end with: >>> s.shutdown(1) >>> s.close() Or should I do this each time? Or is there some other way to keep it open so that I can ask 300 times? Thanks in advance -- 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
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>}>dr>>startroom>l[0, 0]startroom>dig n(0, 1)>n>dr>>nextroom>rooms[<__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>}>save>quitTraceback (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 way>l[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(ha
[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"?> mailto:[EMAIL PROTECTED]> " PRODUCT_NAME="Scintilla" FILE_VERSION="1.56" ORIGINAL_FILENAME="Scintilla.DLL" INTERNAL_NAME="Scintilla" LEGAL_COPYRIGHT="Copyright 1998-2003 by Neil Hodgson" VERFILEDATEHI="0x0" VERFILEDATELO="0x0" VERFILEOS="0x40004" VERFILETYPE="0x1" MODULE_TYPE="WIN32" PE_CHECKSUM="0x0" LINKER_VERSION="0x0" UPTO_BIN_FILE_VERSION="1.5.6.0" UPTO_BIN_PRODUCT_VERSION="1.5.6.0" LINK_DATE="10/24/2005 08:49:52" UPTO_LINK_DATE="10/24/2005 08:49:52" VER_LANGUAGE="English (United States) [0x409]" /> I would apppreciate any help. Thanks Ashwini --- Dr.AShwini Mishra Univ. of Massachusetts Medical school Two biotech ,suite119 373 Plantation st,Worcester MaA01605 Tel:508-856-1087 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Words alignment tool
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. In bioinformatics world, this is 100% identical to Protein or DNA alignment. Example: If there are 3 sequences DNA1,2 and 3 with their sequences: DNA1: ATTTAA DNA2: ATAT DNA3: TAATAATAA DNA1 ATTTAA DNA2 A TA T DNA3 TA AtAAT AA These 3 sequences are aligned by introducing gaps. However, in DNA and protein sequence alignments more complex algorithms and treatment is done so as to make a better scoring alignment. However, unfortunately I cannot apply these algorithms/programs to my data, because these programs are made for DNA and protein sequences. I googled for some word matchers. There are programs available however, they align them without itroducing gaps. So ultimately I cannot see the common items clearly lined up (I guess I may be wrong here, it might be better also). My question to the community is, are there any such programs that would generate a multiple alignments on user defined data. I am sure that the idea of multiple algorithms might have been extended to other areas of science, economics or LINGUISTICS. Could any one help me if I can align my data. I have a total of 50 unique words (SPR-1, SPR-2, SPR-3 likewise but no exactly the order and digit). For some Control elements I have 120 such words in a row (consider this of a sequence with 120 words). So if I have to do this in excel I will spend the rest of my happy life doing that :-) However, to show I tried to do that and pasted it below ( derailed completely). So, dear respected members do you have any suggestions of any such programs that I can use in this world of CS. Thank you. S Contr1 SPR-10 SPR-15 SPR-101 SPR-106 SPR-138 SPR-139 SPR-140 SPR-144 SPR-148 contr2 SPR-1 SPR-10 SPR-101 SPR-130 SPR-138 SPR-139 SPR-142 SPR-144 SPR-148 contr3 SPR-15 SPR-16 SPR-17 SPR-106 SPR-130 SPR-135 SPR-139 SPR-144 SPR-181 __ Start your day with Yahoo! - Make it your home page! http://www.yahoo.com/r/hs ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
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] my text adventure, saving and restoring
thanks that was very helpful. i added all that stuff because i was trying to figure out some way of getting at the descriptions and exits. that is the part i'm stuck on. but knowing that world is all i need is good because i can focus my efforts better. thanks. > 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 ___ 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] my text adventure
i have finally got save and restoring to work. here is my code for posterity. :) i was (i think) putting the player in a room that wasnt the room from the pickle. but somehow the same room. anyway comments welcome. 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) 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') sword = 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() p.location = world[0,0] while 1: p.do()else: room1 = Room([0,0]) p.location = room1 while 1: p.do() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Words alignment tool
On Sun, 4 Dec 2005, Srinivas Iyyer wrote: > Contr1SPR-10 SPR-101 SPR-125 SPR-137 SPR-139 SPR-143 > contr2SPR-1 SPR-15 SPR-126 SPR-128 SPR-141 SPR-148 > contr3SPR-106 SPR-130 SPR-135 SPR-138 SPR-139 SPR-145 > contr4SPR-124 SPR-125 SPR-130 SPR-139 SPR-144 SPR-148 Hi Srinivas, I'd strongly recommend changing the data representation from a line-oriented to a more structured view. Each line in your data above appears to describe a conceptual set of tuples: (control_number, spr_number) For example, we can think of the line: Contr1 SPR-10 SPR-101 SPR-125 SPR-137 SPR-139 SPR-143 as an encoding for the set of tuples written below (The notation I use below is mathematical and not meant to be interpreted as Python.): { (Contr1, SPR-10), (Contr1, SPR-101), (Contr1, SPR-125), (Contr1, SPR-137), (Contr1, SPR-139), (Contr1, SPR-143) } I'm not sure if I'm seeing everything, but from what I can tell so far, your data cries out to be held in a relational database. I agree with Kent: you do not need to "align" anything. If, within your sequence, each element has to be unique in that sequence, then your "alignment" problem transforms into a simpler table lookup problem. That is, if all your data looks like: 1: A B D E 2: A C F 3: A B C D where no line can have repeated characters, then that data can be transformed into a simple tablular representation, conceptually as: A B C D E F 1 | x | x | | x | x | | 2 | x | | x | | | x | 3 | x | x | x | x | | | So unless there's something here that you're not telling us, there's no need for any complicated alignment algorithms: we just start off with an empty table, and then for each tuple, check the corresponding entry in the table. Then when we need to look for common elements, we just scan across a row or column of the table. BLAST is cool, but, like regular expressions, it's not the answer to every string problem. If you want to implement code to do the above, it's not difficult, but you really should use an SQL database to do this. As a bioinformatician, it would be in your best interest to know SQL, because otherwise, you'll end up trying to reinvent tools that have already been written for you. A good book on introductory relational database usage is "The Practical SQL Handbook: Using Structured Query Language" by Judith Bowman, Sandra Emerson, and Marcy Darnovsky. Good luck to you. ___ 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