re: A problem with list
Hi There are many different ways to solve the problem that you are having. The easiest if you are planning on only dealing with strings or a predictable data structure would be to do something like this: Code: ~~ #Pre: Pass in a string and the character you want it tokenized by #Post: Returns a list of all the tokens def tokenizer(str, chr=' '): #heres a useful tool just like StringTokenizer feature in Java if chr != '': chr = chr[0] else: chr = ' ' x = "" tokens = [""] z = 0 for x in str: if x != chr: tokens[z] = tokens[z] + x else: z = z + 1 tokens.append("") return tokens list = ['abc', 'def', 'xyz'] str = '' for x in list: str+=list+'.' #note this is a delimiter could be anything, if you wanted to you could re-implement the above function to work with delimiters of any length not just length 1 #save the str #load str a loadedStr loadedList = tokenizer(loadedStr, '.') #loadedList = ['abc', 'def', 'xyz'] ~~ that is one way too solve your problem another is to use some sort of real database structure, for simplicity, you could use XML example xml structure: ~~ float 3.14 0 int 3 1 string Hi i am a string 2 ~~ The first way to solve your problem is quick dirty and efficient. The XML version is highly scalable but requires a lot more code to implement. As mentioned before there are a number of other solutions to the same problem --- Cheers Tim Henderson original message: The following code ## import string MyList=['abc','def'] for i in MyList: print i ### works as I expect that is I get abc def but when I have Mylist in a file and I read it from the file it does not work as I expect. # import string ff=open('C:\\Robotp\\MyFile.txt','r') # read MyList from a file MyList=ff.read() for i in MyList: print i ### I will get [ ' a b c ' , ' d e f ' ] where my MyFile.txt looks like this: ['abc','def'] Where is a problem? Thanks for help Lad -- http://mail.python.org/mailman/listinfo/python-list
Inspect Python Classes for instance data information
Hello I want to creat a program that can inspect a set of classes that i have made and spit out a savable version of these classes. To do this I need to be able to inspect each class and get all of its instance data as well as information about a particular meathod. This is what i want it to do: class song: def __init__(self, name, artist, album, published): self.name = name self.artist = artist self.album = album self.published = published def build(self, name, artist, album, published): self.name = name self.artist = artist self.album = album self.published = published #now what i want to do with this dummy class s = song("12:51", "The Strokes", "Room on Fire", 2005) print getTextVersion(s) song string 12:51 name string The Strokes artist string Room on Fire album int 2005 published name artist album published I have no idea how to do the inspection part of this little problem. I could just have each class define its own meathod to convert to this format, but that would be a lot of work when i could just make one class that solves the problem for all classes. Is there a way to do this? cheers --- Tim Henderson mail me: [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Inspect Python Classes for instance data information
Thanks to both of you. Now that I know the correct terminology for what I want to do, I can detirmine the best way to do it. I am not sure if meta-classing is the simplist solution to this problem, however it can be the most elegant. When I have a final implimintation of this project it will be posted. cheers Tim Henderson -- http://mail.python.org/mailman/listinfo/python-list
Re: Regular Expression tools?
I am not quite sure what you want. However if you are looking for Python's regular expression module it is the re module. Just go to interactive help and view the api for it. cheers Tim Henderson -- http://mail.python.org/mailman/listinfo/python-list
should python have a sort list-map object in the std-lib?
Hi The question why are there no sorted dictionaries in python, seems to pop up with unseeming regularity. That question in itself in nonsensical sense dictionaries are hash-maps, however should python have a sorted map type object is a good question. clearly many people like have a sorted map, and sorting the keys every time seems rather wasteful, as does keeping a separate sorted list of the keys. a related question is, in python is it more efficient to a maintain a list type in sorted order by inserting each new object in the correct location (by say finding it with a binary search and then using del list[x]) or appending each new item to the end of said list and using the built-in .sort method, or sorted() function? -- Tim Henderson mail me: [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison problem
of course the more correct way is most likely the use of short circuit evaluation. so somthing along lines of if (len(item) > 0) and (item[0] == '-'): pass would probably be the correct approach. -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison problem
peter would not the more correct way to do this be short circuit evaluation. somthing along lines of if (len(item) > 0) and (item[0] == '-'): pass seems to be the more correct approach. rather than relying on slicing to return an empty string. For instance suppose you wanted to test to see if than spot contained an empty string. If you used the slice meathod you would be in a pickle. However in this particular case it seems to be clear that the use of .startswith() is the single best approach. -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie question: Tab key giving different output
i think it is telling you to use spaces btw i have no idea, it just sounds like a funny thing to happen -- http://mail.python.org/mailman/listinfo/python-list
Re: should python have a sort list-map object in the std-lib?
ahh, i hadn't thought of using a proirity queue but that is the correct solution most of the time, except i suppose when you have input that causes you to excessively reheap which could be problematic. i may still look into writing a general sorted map though, it could be useful especially if there were and easy way to set the type of functionality required with out resorting to several different types of sorted-maps. -- http://mail.python.org/mailman/listinfo/python-list
Re: sha, PyCrypto, SHA-256
On Dec 16, 2:17 pm, [EMAIL PROTECTED] wrote: > Operating system: Win XP > Vsn of Python: 2.4 > > Situation is this: Required to calcluate a message digest. The process > for calcluating the digest must use an SHA-256 algorithm. > > Questions: > 1) Is it correct that the sha module comes with python 2.4? > 2) Is it correct that the sha module that ships with python 2.4 does > NOT have the SHA-256 capability as part of the module? > 3) It looks like PyCrypto is a package that, among other things, > permits one to calculate a message digest using an SHA-256 > algorithm...is that correct? > 4) It looks like there are a couple couple possibilities available for > the download...either download the source code and run the setup which > (I'm assuming) compiles the various extension modules, or download the > pycrypto-2.0.1.win32-py2.4.zip which extracts out to a .exe; when one > runs the just-extracted .exe, it installs the stuff on one's > workstation. I'm leaning toward the second option because it seems > like most of the work has been done for me. A quick search on this > site didn't turn up anything that suggested there were problems with > running the installer. So, my question is this: are you aware of any > problems running the installer? > 5) Besides PyCrypto, are there any other Python packages that permit > one to calculate a message digest using an SHA-256 algorithm? > > Thank you. I have run that exact installer many many times and it works fine. to use SHA-256 with pycrypto: >>> from Crypto.Hash import SHA256 >>> sha = SHA256.new() >>> sha.update('message') >>> sha.hexdigest() # sha.digest gives the raw form 'ab530a13e45914982b79f9b7e3fba994cfd1f3fb22f71cea1afbf02b460c6d1d' cheers tim -- http://mail.python.org/mailman/listinfo/python-list
Re: OO question
On Jan 1, 8:09 pm, [EMAIL PROTECTED] wrote: > Lets say I have those two classes, Person and Address. How would one > implement the relationship between them? First, a Person can have one > or more addresses (or none), that could be represented as a list of > Addresses, right? But then, if I have an Address I want to see which > persons who live there, then I would have a list of Persons in each > Address. > > Is this doubly-linked way of doing it a good way of doing it, or is > there > a better "OO way" I haven't learned yet? Since you are making an address book and not a person book I would start out by making a simple list of addresses. Each address could have a "person" or a "name" field. You could then sort your list by the name of each person associated with each address. If an address has no person associated with it then you put it at the beginning (or end if you want) of the list. I think this would most closely mimic how a rolodex works. It doesn't really matter if you implement it as a list of dictionaries or a list classes I believe they would be equivalent in this case. tim -- http://mail.python.org/mailman/listinfo/python-list