From mehgcap at gmail.com Fri Oct 1 00:32:40 2010 From: mehgcap at gmail.com (Alex Hall) Date: Thu, 30 Sep 2010 18:32:40 -0400 Subject: [Tutor] just what does read() return? Message-ID: Hi all, I have a parser class which is supposed to take a text file and parse it. I will then do more with the resulting data. The file is in a particular format, specified by my professor, though this is not homework (it will be used to do homework later). The file is in the format: l vx vy z vx vy z where l is either D or U and x, y, and z are numbers. Anyway, I have the following lines: f=open(self.file, "r") self.original=f.read() #I thought self.original would now be a string of all the data in self.file txt=str(self.original).split(r"\n+") #create an array where elements are lines in file print txt I fully expected to see txt be an array of strings since I figured self.original would have been split on one or more new lines. It turns out, though, that I get this instead: ['l\nvx vy z\nvx vy z'] How is it that txt is not an array of the lines in the file, but instead still holds \n characters? I thought the manual said read() returns a string: "To read a file's contents, call f.read(size), which reads some quantity of data and returns it as a string. size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned; it's your problem if the file is twice as large as your machine's memory. Otherwise, at most size bytes are read and returned. If the end of the file has been reached, f.read() will return an empty string ( ""). " I know I can use f.readline(), and I was doing that before and it all worked fine. However, I saw that I was reading the file twice and, in the interest of good practice if I ever have this sort of project with a huge file, I thought I would try to be more efficient and read it once. I will use self.original later again, so I need it either way, and I figured I could use it since I had already read the file to get it. TIA. -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From delegbede at dudupay.com Fri Oct 1 00:38:31 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Thu, 30 Sep 2010 23:38:31 +0100 Subject: [Tutor] python Task Message-ID: Kindly help me with the following tasks. You may want to start with explanations for me and then pseudo-codes, I should be able to take it from there. They are exercises from deitel how to program for Python. I have done a lot of thinking and its almost discouraging me. Please help. Thanks. 4.4 An integer greater than 1 is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime numbers, but 4, 6, 8 and 9 are not. a) Write a function that determines whether a number is prime. b) Use this function in a program that determines and prints all the prime numbers between 2 and 1,000. c) Initially, you might think that n/2 is the upper limit for which you must test to see whether a number is prime, but you need go only as high as the square root of n. Rewrite the program and run it both ways to show that you get the same result. 4.5 An integer number is said to be a perfect number if the sum of its factors, including 1 (but not the number itself), is equal to the number. For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write a function perfect that determines whether parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000. Print the factors of each perfect number to confirm that the number is indeed perfect. Challenge the power of your computer by testing numbers much larger than 1000. 4.7 Write a program that plays the game of ?guess the number? as follows: Your program chooses the number to be guessed by selecting an integer at random in the range 1 to 1000. The program then displays I have a number between 1 and 1000. Can you guess my number? Please type your first guess. The player then types a first guess. The program responds with one of the following: 1. Excellent! You guessed the number! Would you like to play again (y or n)? 2. Too low. Try again. 3. Too high. Try again. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program should keep telling the player Too high or Too low to help the player ?zero in? on the correct answer. After a game ends, the program should prompt the user to enter "y" to play again or "n" to exit the game. 4.8 (Towers of Hanoi) Every budding computer scientist must grapple with certain classic problems. The Towers of Hanoi (see Fig. 4.23) is one of the most famous of these. Legend has it that, in a temple in the Far East, priests are attempting to move a stack of disks from one peg to another. The initial stack had 64 disks threaded onto one peg and arranged from bottom to top by decreasing size. The priests are attempting to move the stack from this peg to a second peg, under the constraints that exactly one disk is moved at a time and that at no time may a larger disk be placed above a smaller disk. A third peg is available for holding disks temporarily. Supposedly, the world will end when the priests complete their task, so there is little incentive for us to facilitate their efforts. Let us assume that the priests are attempting to move the disks from peg 1 to peg 3. We wish to develop an algorithm that will print the precise sequence of peg-to-peg disk transfers. If we were to approach this problem with conventional methods, we would rapidly find ourselves hopelessly knotted up in managing the disks. Instead, if we attack the problem with recursion in mind, it immediately becomes tractable. Moving n disks can be viewed in terms of moving only n - 1 disks (hence, the recursion), as follows: a) Move n - 1 disks from peg 1 to peg 2, using peg 3 as a temporary holding area. b) Move the last disk (the largest) from peg 1 to peg 3. c) Move the n - 1 disks from peg 2 to peg 3, using peg 1 as a temporary holding area. The process ends when the last task involves moving n = 1 disk, i.e., the base case. This is accomplished trivially by moving the disk without the need for a temporary holding area. Write a program to solve the Towers of Hanoi problem. Use a recursive function with four parameters: a) The number of disks to be moved b) The peg on which these disks are initially threaded c) The peg to which this stack of disks is to be moved d) The peg to be used as a temporary holding area Your program should print the precise instructions it will take to move the disks from the starting peg to the destination peg. For example, to move a stack of three disks from peg 1 to peg 3, your program should print the following series of moves: 1 ? 3 (This means move one disk from peg 1 to peg 3.) 1 ? 2 3 ? 2 1 ? 3 2 ? 1 2? 3 1? 3 -- Sent from my mobile device Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development From missive at hotmail.com Fri Oct 1 00:37:58 2010 From: missive at hotmail.com (Lee Harr) Date: Fri, 1 Oct 2010 03:07:58 +0430 Subject: [Tutor] (de)serialization questions Message-ID: > I have data about zip codes, street and city names (and perhaps later also of > street numbers). I made a dictionary of the form {zipcode: (street, city)} One dictionary with all of the data? That does not seem like it will work. What happens when 2 addresses have the same zip code? > Are there forms of object permanence that do not read all data into memory? How about a file? You could write your data one record per line, and then read it in one line at a time also. You could also use a csv file, or maybe use a database like sqlite. From mehgcap at gmail.com Fri Oct 1 00:49:31 2010 From: mehgcap at gmail.com (Alex Hall) Date: Thu, 30 Sep 2010 18:49:31 -0400 Subject: [Tutor] just what does read() return? In-Reply-To: References: Message-ID: On 9/30/10, Walter Prins wrote: > On 30 September 2010 23:32, Alex Hall wrote: > >> txt=str(self.original).split(r"\n+") #create an array where elements >> > > OK, consider this Python shell session: > >>>> s = "line1\nline2" >>>> s.split() > ['line1', 'line2'] >>>> s.split(r"\n+") > ['line1\nline2'] > > Hmm, so split doesn't like that seperator. > > Taking a step back -- It looks like you're trying to specify a regular > expression as a split string. A string object's split method doesn't > support regular expressions. The split function in the "re" module however > does. Ah-ha!! re.split(r"\n+", self.original) That did it, and my program once again runs as expected. Thanks! > > HTH Very much. > > Walter > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From steve at alchemy.com Fri Oct 1 00:55:39 2010 From: steve at alchemy.com (Steve Willoughby) Date: Thu, 30 Sep 2010 15:55:39 -0700 Subject: [Tutor] just what does read() return? In-Reply-To: References: Message-ID: <4CA5156B.1050404@alchemy.com> On 30-Sep-10 15:49, Alex Hall wrote: > re.split(r"\n+", self.original) > That did it, and my program once again runs as expected. Thanks! If you don't need blank lines stripped out (r'\n+' considers multiple consecutive \n characters to be a single record separator), you can avoid the regex and just use the normal string split: list_of_lines = giant_string.split('\n') From savtech138 at gmail.com Fri Oct 1 01:26:16 2010 From: savtech138 at gmail.com (Russell Smith) Date: Thu, 30 Sep 2010 16:26:16 -0700 Subject: [Tutor] Help - server health check reporter Message-ID: Hey guys, I?m trying to put together a script using urllib2, smtplib and stripogram/html2text which will use a healthcheck url, read the response after loading it and then email me the results without any unwanted html tags. I was able to do that but, when I get a timeout on one of the servers the script breaks with an error response. I?m very new to python and I don?t know how to take that error and have it sent as well, instead of breaking the script. Any advice or links I can check out that might be helpful? I appreciate it. I can include the python code if needed though at this point I'm sure it is not very "pythonic". {smile} Regards, -russ -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Fri Oct 1 01:32:14 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 1 Oct 2010 09:32:14 +1000 Subject: [Tutor] Help - server health check reporter In-Reply-To: References: Message-ID: <201010010932.15004.steve@pearwood.info> On Fri, 1 Oct 2010 09:26:16 am Russell Smith wrote: > I?m trying to put together a script using urllib2, smtplib and > stripogram/html2text which will use a healthcheck url, read the > response after loading it and then email me the results without any > unwanted html tags. I was able to do that but, when I get a timeout > on one of the servers the script breaks with an error response. Could you copy and paste the exact error message, including the full traceback? Also, what version of Python are you using? > I?m > very new to python and I don?t know how to take that error and have > it sent as well, instead of breaking the script. Any advice or links > I can check out that might be helpful? I appreciate it. try: code that times out goes here except Exception as error: do something with error Obviously this isn't exactly Python code, but I can be more specific once you've answered the above two questions. -- Steven D'Aprano From steve at pearwood.info Fri Oct 1 01:45:29 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 1 Oct 2010 09:45:29 +1000 Subject: [Tutor] python Task In-Reply-To: References: Message-ID: <201010010945.29356.steve@pearwood.info> On Fri, 1 Oct 2010 08:38:31 am Dipo Elegbede wrote: > 4.4 An integer greater than 1 is said to be prime if it is divisible > by only 1 and itself. For example, > 2, 3, 5 and 7 are prime numbers, but 4, 6, 8 and 9 are not. > a) Write a function that determines whether a number is prime. The first thing you need to know is that you can find out the remainder after division by using the % operator. For example: 10 % 5 => 0 # 10 = 5*2 11 % 5 => 1 # 11 = 5*2 + 1 12 % 5 => 2 # 12 = 5*2 + 2 and so forth. So you can check whether a number is divisible by another with: if n % divisor == 0: print "n is exactly divisible by divisor" else: print "n is not divisible by divisor" So to find out whether a number n is prime, you need to check whether it is divisible by each whole number starting from 2: for divisor in range(2, n): if n is divisible by divisor then n is not prime # outside the loop print "prime" The above does a LOT of unnecessary work. Some improvements: * Once you've discovered that a number is not prime, you can exit the loop. * There's no need to test every number up to n-1. Think about what the greatest number you need to test would be. * If a number isn't divisible by 2, then it can't possibly be divisible by 4, 6, 8, 10, ... either. So apart from 2, there's no need to check even numbers. -- Steven D'Aprano From steve at pearwood.info Fri Oct 1 01:48:53 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 1 Oct 2010 09:48:53 +1000 Subject: [Tutor] just what does read() return? In-Reply-To: References: Message-ID: <201010010948.53536.steve@pearwood.info> On Fri, 1 Oct 2010 08:49:31 am Alex Hall wrote: > Ah-ha!! > re.split(r"\n+", self.original) > That did it, and my program once again runs as expected. Thanks! There is no need to crack that tiny peanut with the 40 lb sledgehammer of a regular expression. list_of_lines = string.split('\n') Much faster, simpler, and does the job. To get rid of empty lines: list_of_lines = filter(None, string.split('\n')) -- Steven D'Aprano From alan.gauld at btinternet.com Fri Oct 1 01:59:06 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 1 Oct 2010 00:59:06 +0100 Subject: [Tutor] inheritance problem References: Message-ID: "Roelof Wobben" wrote > So i have this programm now : > class Deck: > def __init__(self): > self.cards = [] > for suit in range(4): > for rank in range(1, 14): > self.cards.append(Card(suit, rank)) > > def deal(self, hands, num_cards=999): > num_hands = len(hands) > for i in range(num_cards): > if self.is_empty(): break # break if out of cards > card = self.pop() # take the top card > hand = hands[i % num_hands] # whose turn is next? > hand.add(card) # add the card to the hand > > def shuffle(self): > import random > num_cards = len(self.cards) > for i in range(num_cards): > j = random.randrange(i, num_cards) > self.cards[i], self.cards[j] = self.cards[j], > self.cards[i] > > def remove(self, card): > if card in self.cards: > self.cards.remove(card) > return True > else: > return False > def is_empty(self): > return (len(self.cards) == 0) > But now Im getting this error message: > > Traceback (most recent call last): > File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 126, > in > game.deck.deal([hand], 13) > File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 24, > in deal > card = self.pop() # take the top card > AttributeError: Deck instance has no attribute 'pop' > > > What went wrong here. The error tells you what is wrong, the Deck has no attribute called pop. Can you see a pop anywhere? So if the code you copied has an error how should it assign a card? Where are the cards stored? How would you get the first card from the collection? Does that work? Think about what the error is telling you and think about how you would fix it. If you don't understand what the error is saying then tell us and we can explain it, but in this case its pretty clearly stated and is one you have seen before. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Fri Oct 1 02:01:48 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 1 Oct 2010 10:01:48 +1000 Subject: [Tutor] just what does read() return? In-Reply-To: References: Message-ID: <201010011001.49028.steve@pearwood.info> On Fri, 1 Oct 2010 08:32:40 am Alex Hall wrote: > I fully expected to see txt be an array of strings since I figured > self.original would have been split on one or more new lines. It > turns out, though, that I get this instead: > ['l\nvx vy z\nvx vy z'] There's no need to call str() on something that already is a string. Admittedly it doesn't do much harm, but it is confusing for the person reading, who may be fooled into thinking that perhaps the argument wasn't a string in the first place. The string split method doesn't interpret its argument as a regular expression. r'\n+' has no special meaning here. It's just three literal characters backslash, the letter n, and the plus sign. split() tries to split on that substring, and since your data doesn't include that combination anywhere, returns a list containing a single item: >>> "abcde".split("ZZZ") ['abcde'] > How is it that txt is not an array of the lines in the file, but > instead still holds \n characters? I thought the manual said read() > returns a string: It does return a string. It is a string including the newline characters. [...] > I know I can use f.readline(), and I was doing that before and it all > worked fine. However, I saw that I was reading the file twice and, in > the interest of good practice if I ever have this sort of project > with a huge file, I thought I would try to be more efficient and read > it once. You think that keeping a huge file in memory *all the time* is more efficient? It's the other way around -- when dealing with *small* files you can afford to keep it in memory. When dealing with huge files, you need to re-write your program to deal with the file a piece at a time. (This is often a good strategy for small files as well, but it is essential for huge ones.) Of course, "small" and "huge" is relative to the technology of the day. I remember when 1MB was huge. These days, huge would mean gigabytes. Small would be anything under a few tens of megabytes. -- Steven D'Aprano From steve at pearwood.info Fri Oct 1 02:06:07 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 1 Oct 2010 10:06:07 +1000 Subject: [Tutor] Runnig a windows.exe from python In-Reply-To: References: Message-ID: <201010011006.08075.steve@pearwood.info> On Fri, 1 Oct 2010 04:05:08 am Susana Iraiis Delgado Rodriguez wrote: > Hello ! > > I apoligize for the format of my last message, I didn't realize that > putting my text in bold format will show those asterisks. > By the way you're Alan, for the pythom module I first had to read my > arguments and passed them to the os.system(), it worked. Now when I > execute this command, in my normal DOS Windows, I must change to > another path before I run the .exe, I don't know how to tell this to > my python module. os.chrdir(path) changes to a new path. -- Steven D'Aprano From steve at pearwood.info Fri Oct 1 02:12:02 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 1 Oct 2010 10:12:02 +1000 Subject: [Tutor] Doubly linked list In-Reply-To: References: Message-ID: <201010011012.03185.steve@pearwood.info> On Thu, 30 Sep 2010 11:24:06 pm T MURPHY wrote: > I was having trouble, where on the python site can i find steps on > classes Google is your friend: googling for "python tutorial" brings up lots of tutorials that will help. Here's the first one: http://docs.python.org/tutorial/ > and making a doubly linked list, is there a help option for > linked lists. Python doesn't supply a doubly-linked list, but it's easy to make your own. Here's a singly-linked list for you: class Node(object): def __init__(self, data, next=None): self.data = data self.next = next mylist = Node("a") mylist.next = Node("b", Node("c")) node = mylist while node is not None: print node.data node = node.next -- Steven D'Aprano From steve at pearwood.info Fri Oct 1 02:16:22 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 1 Oct 2010 10:16:22 +1000 Subject: [Tutor] Printing prime numbers In-Reply-To: References: <361937526-1285847825-cardhu_decombobulator_blackberry.rim.net-239086245-@bda308.bisx.produk.on.blackberry> Message-ID: <201010011016.23068.steve@pearwood.info> On Thu, 30 Sep 2010 11:52:49 pm Emmanuel Ruellan wrote: > On Thu, Sep 30, 2010 at 1:57 PM, wrote: > > 1 is prime > > One is /not/ prime. There's no need to apologize for pedantry. Saying that one is prime is like saying that 2 is an odd number, or that glass is a type of meat. -- Steven D'Aprano From g.nius.ck at gmail.com Fri Oct 1 02:24:50 2010 From: g.nius.ck at gmail.com (Chris King) Date: Thu, 30 Sep 2010 20:24:50 -0400 Subject: [Tutor] Mutable Properties Message-ID: <4CA52A52.5000302@gmail.com> Dear Tutors, I noticed that when you make a property to represent a mutable value *class Obj(object): def get(self): print 'Get' return self.prop def set(self, new): print 'Set' self.prop = new prop = property(get, set) test = Obj() test.prop = ['Hello'] * and then try and use one of its methods. *test.prop.append('World') *It will treat it as a get, not a set. *Output: Get *Even thou you are basically changing its value. *Before: ['Hello'] After: ['Hello', 'World'] *I know this happens because I'm not technically setting it to something else, just getting one of its properties. I was wondering how to make it fire off set to for certain methods. Sincerely, Me -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Fri Oct 1 02:43:48 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 1 Oct 2010 10:43:48 +1000 Subject: [Tutor] Mutable Properties In-Reply-To: <4CA52A52.5000302@gmail.com> References: <4CA52A52.5000302@gmail.com> Message-ID: <201010011043.49254.steve@pearwood.info> On Fri, 1 Oct 2010 10:24:50 am Chris King wrote: > Dear Tutors, > I noticed that when you make a property to represent a mutable > value > > *class Obj(object): > def get(self): > print 'Get' > return self.prop > def set(self, new): > print 'Set' > self.prop = new > prop = property(get, set) > > test = Obj() > test.prop = ['Hello'] > * > and then try and use one of its methods. > > *test.prop.append('World') > > *It will treat it as a get, not a set. That's because it is a get. You get the property, and then you call one of its methods. How could it be any different? The object stored in the property could be anything. How can the class know which methods modify it in place, and which ones don't? test.prop.sort() Did this modify the list or not? > *Output: Get > > *Even thou you are basically changing its value. But changing a mutable value means modifying it in place, not re-assigning it. This is no different from: mylist = test.prop # this is a get mylist.append("something") # change it in place Since there is no assignment to prop, there is no set. > *Before: ['Hello'] > After: ['Hello', 'World'] > > *I know this happens because I'm not technically setting it to > something else, just getting one of its properties. > I was wondering how to make it fire off set to for certain methods. You can't. What problem are you trying to solve? There is likely another way to solve it. (Not necessarily an easy way, but we'll see.) -- Steven D'Aprano From g.nius.ck at gmail.com Fri Oct 1 03:24:16 2010 From: g.nius.ck at gmail.com (Chris King) Date: Thu, 30 Sep 2010 21:24:16 -0400 Subject: [Tutor] Mutable Properties In-Reply-To: <201010011043.49254.steve@pearwood.info> References: <4CA52A52.5000302@gmail.com> <201010011043.49254.steve@pearwood.info> Message-ID: <4CA53840.2020809@gmail.com> On 9/30/2010 8:43 PM, Steven D'Aprano wrote: > On Fri, 1 Oct 2010 10:24:50 am Chris King wrote: >> Dear Tutors, >> I noticed that when you make a property to represent a mutable >> value >> >> *class Obj(object): >> def get(self): >> print 'Get' >> return self.prop >> def set(self, new): >> print 'Set' >> self.prop = new >> prop = property(get, set) >> >> test = Obj() >> test.prop = ['Hello'] >> * >> and then try and use one of its methods. >> >> *test.prop.append('World') >> >> *It will treat it as a get, not a set. > That's because it is a get. You get the property, and then you call one > of its methods. How could it be any different? > > The object stored in the property could be anything. How can the class > know which methods modify it in place, and which ones don't? > > test.prop.sort() > > Did this modify the list or not? > > >> *Output: Get >> >> *Even thou you are basically changing its value. > But changing a mutable value means modifying it in place, not > re-assigning it. This is no different from: > > mylist = test.prop # this is a get > mylist.append("something") # change it in place > > Since there is no assignment to prop, there is no set. > > > >> *Before: ['Hello'] >> After: ['Hello', 'World'] >> >> *I know this happens because I'm not technically setting it to >> something else, just getting one of its properties. >> I was wondering how to make it fire off set to for certain methods. > > You can't. > > What problem are you trying to solve? There is likely another way to > solve it. (Not necessarily an easy way, but we'll see.) > > > So I'll have to create my own object which modifies each method to fire off the set method if the old form and the new form are unequal, there is no easy way? From mehgcap at gmail.com Fri Oct 1 03:54:33 2010 From: mehgcap at gmail.com (Alex Hall) Date: Thu, 30 Sep 2010 21:54:33 -0400 Subject: [Tutor] just what does read() return? In-Reply-To: <201010011001.49028.steve@pearwood.info> References: <201010011001.49028.steve@pearwood.info> Message-ID: On 9/30/10, Steven D'Aprano wrote: > On Fri, 1 Oct 2010 08:32:40 am Alex Hall wrote: > >> I fully expected to see txt be an array of strings since I figured >> self.original would have been split on one or more new lines. It >> turns out, though, that I get this instead: >> ['l\nvx vy z\nvx vy z'] > > There's no need to call str() on something that already is a string. > Admittedly it doesn't do much harm, but it is confusing for the person > reading, who may be fooled into thinking that perhaps the argument > wasn't a string in the first place. Agreed. I was having some (unrelated) trouble and was desperate enough to start forcing things to the data type I needed, just in case. > > The string split method doesn't interpret its argument as a regular > expression. r'\n+' has no special meaning here. It's just three literal > characters backslash, the letter n, and the plus sign. split() tries to > split on that substring, and since your data doesn't include that > combination anywhere, returns a list containing a single item: > >>>> "abcde".split("ZZZ") > ['abcde'] Yes, that makes sense. > >> How is it that txt is not an array of the lines in the file, but >> instead still holds \n characters? I thought the manual said read() >> returns a string: > > It does return a string. It is a string including the newline > characters. > > > [...] >> I know I can use f.readline(), and I was doing that before and it all >> worked fine. However, I saw that I was reading the file twice and, in >> the interest of good practice if I ever have this sort of project >> with a huge file, I thought I would try to be more efficient and read >> it once. > > You think that keeping a huge file in memory *all the time* is more > efficient? Ah, I see what you mean now. I work with the data later, so you are saying that it would be better to just read the file as necessary, then then, when I need the file's data later, just read it again. > It's the other way around -- when dealing with *small* files > you can afford to keep it in memory. When dealing with huge files, you > need to re-write your program to deal with the file a piece at a time. > (This is often a good strategy for small files as well, but it is > essential for huge ones.) > > Of course, "small" and "huge" is relative to the technology of the day. > I remember when 1MB was huge. These days, huge would mean gigabytes. > Small would be anything under a few tens of megabytes. > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From steve at pearwood.info Fri Oct 1 04:16:56 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 1 Oct 2010 12:16:56 +1000 Subject: [Tutor] Mutable Properties In-Reply-To: <4CA53840.2020809@gmail.com> References: <4CA52A52.5000302@gmail.com> <201010011043.49254.steve@pearwood.info> <4CA53840.2020809@gmail.com> Message-ID: <201010011216.56794.steve@pearwood.info> On Fri, 1 Oct 2010 11:24:16 am Chris King wrote: > > What problem are you trying to solve? There is likely another way > > to solve it. (Not necessarily an easy way, but we'll see.) > > So I'll have to create my own object which modifies each method to > fire off the set method if the old form and the new form are unequal, > there is no easy way? I repeat... what problem are you trying to solve? Calling the set method for something that *doesn't* set a property doesn't sound like a solution to a problem to me. It sounds like a problem itself. -- Steven D'Aprano From davea at ieee.org Fri Oct 1 04:21:45 2010 From: davea at ieee.org (Dave Angel) Date: Thu, 30 Sep 2010 22:21:45 -0400 Subject: [Tutor] Mutable Properties In-Reply-To: <4CA53840.2020809@gmail.com> References: <4CA52A52.5000302@gmail.com> <201010011043.49254.steve@pearwood.info> <4CA53840.2020809@gmail.com> Message-ID: <4CA545B9.5060800@ieee.org> On 2:59 PM, Chris King wrote: > On 9/30/2010 8:43 PM, Steven D'Aprano wrote: >> >> >> >> What problem are you trying to solve? There is likely another way to >> solve it. (Not necessarily an easy way, but we'll see.) >> >> >> > So I'll have to create my own object which modifies each method to > fire off the set method if the old form and the new form are unequal, > there is no easy way? > Try rephrasing that in English. Or just answer Chris's question. What problem are you trying to solve? The system will call the get() method if you're accessing the existing object, whether to look at it or modify it. It'll call the set() method if you're binding a new object to the attribute. DaveA From mehgcap at gmail.com Fri Oct 1 04:45:38 2010 From: mehgcap at gmail.com (Alex Hall) Date: Thu, 30 Sep 2010 22:45:38 -0400 Subject: [Tutor] regexp: a bit lost Message-ID: Hi, once again... I have a regexp that I am trying to use to make sure a line matches the format: [c*]n [c*]n n where c* is (optionally) 0 or more non-numeric characters and n is any numeric character. The spacing should not matter. These should pass: v1 v2 5 2 someword7 3 while these should not: word 2 3 1 2 Here is my test: s=re.search(r"[\d+\s+\d+\s+\d]", l) if s: #do stuff However: 1. this seems to pass with *any* string, even when l is a single character. This causes many problems and cannot happen since I have to ignore any strings not formatted as described above. So if I have for a in b: s=re.search(r"[\d+\s+\d+\s+\d]", l) if s: c.append(a) then c will have every string in b, even if the string being examined looks nothing like the pattern I am after. 2. How would I make my regexp able to match 0-n characters? I know to use \D*, but I am not sure about brackets or parentheses for putting the \D* into the parent expression (the \d\s one). 3. Once I get the above working, I will need a way of pulling the characters out of the string and sticking them somewhere. For example, if the string were v9 v10 15 I would want an array: n=[9, 10, 15] but the array would be created from a regexp. This has to be possible, but none of the manuals or tutorials on regexp say just how this is done. Mentions are made of groups, but nothing explicit (to me at least). -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From grflanagan at gmail.com Fri Oct 1 07:20:35 2010 From: grflanagan at gmail.com (Gerard Flanagan) Date: Fri, 01 Oct 2010 06:20:35 +0100 Subject: [Tutor] regexp: a bit lost In-Reply-To: References: Message-ID: Alex Hall wrote: > Hi, once again... > I have a regexp that I am trying to use to make sure a line matches the format: > [c*]n [c*]n n > where c* is (optionally) 0 or more non-numeric characters and n is any > numeric character. The spacing should not matter. These should pass: > v1 v2 5 > 2 someword7 3 > > while these should not: > word 2 3 > 1 2 > > Here is my test: > s=re.search(r"[\d+\s+\d+\s+\d]", l) > if s: #do stuff > > However: > 1. this seems to pass with *any* string, even when l is a single > character. This causes many problems and cannot happen since I have to [...] You want to match a whole line, so you should use re.match not re.search. See the docs: http://docs.python.org/library/re.html#matching-vs-searching You can also use re.split in this case: yes = """ v1 v2 5 2 someword7 3 """.splitlines() yes = [line for line in yes if line.strip()] import re pattern = "(\w*\d\s+?)" # there may be a better pattern than this rx = re.compile(pattern) for line in yes: print [part for part in rx.split(line) if part] From steve at pearwood.info Fri Oct 1 07:33:27 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 1 Oct 2010 15:33:27 +1000 Subject: [Tutor] regexp: a bit lost In-Reply-To: References: Message-ID: <201010011533.28202.steve@pearwood.info> On Fri, 1 Oct 2010 12:45:38 pm Alex Hall wrote: > Hi, once again... > I have a regexp that I am trying to use to make sure a line matches > the format: [c*]n [c*]n n > where c* is (optionally) 0 or more non-numeric characters and n is > any numeric character. The spacing should not matter. These should > pass: v1 v2 5 > 2 someword7 3 > > while these should not: > word 2 3 > 1 2 > > Here is my test: > s=re.search(r"[\d+\s+\d+\s+\d]", l) Try this instead: re.search(r'\d+\s+\D*\d+\s+\d', l) This searches for: one or more digits at least one whitespace char (space, tab, etc) zero or more non-digits at least one digit at least one whitespace exactly one digit > However: > 1. this seems to pass with *any* string, even when l is a single > character. This causes many problems [...] I'm sure it does. You don't have to convince us that if the regular expression is broken, the rest of your code has a problem. That's a given. It's enough to know that the regex doesn't do what you need it to do. > 3. Once I get the above working, I will need a way of pulling the > characters out of the string and sticking them somewhere. For > example, if the string were > v9 v10 15 > I would want an array: > n=[9, 10, 15] Modify the regex to be this: r'(\d+)\s+\D*(\d+)\s+(\d)' and then query the groups of the match object that is returned: >>> mo = re.search(r'(\d+)\s+\D*(\d+)\s+(\d)', 'spam42 eggs23 9') >>> mo.groups() ('42', '23', '9') Don't forget that mo will be None if the regex doesn't match, and don't forget that the items returned are strings. -- Steven D'Aprano From grflanagan at gmail.com Fri Oct 1 07:50:25 2010 From: grflanagan at gmail.com (Gerard Flanagan) Date: Fri, 01 Oct 2010 06:50:25 +0100 Subject: [Tutor] regexp: a bit lost In-Reply-To: References: Message-ID: with coffee: yes = """ v1 v2 5 2 someword7 3 """.splitlines()[1:] no = """ word 2 3 1 2 """.splitlines()[1:] import re pattern = "(\w*\d\s+?)(\w*\d\s+?)(\d)$" rx = re.compile(pattern) for line in yes: m = rx.match(line) assert m print([part.rstrip() for part in m.groups()]) for line in no: m = rx.match(line) assert not m From rwobben at hotmail.com Fri Oct 1 08:19:29 2010 From: rwobben at hotmail.com (Roelof Wobben) Date: Fri, 1 Oct 2010 06:19:29 +0000 Subject: [Tutor] inheritance problem In-Reply-To: References: , Message-ID: ---------------------------------------- > To: tutor at python.org > From: alan.gauld at btinternet.com > Date: Fri, 1 Oct 2010 00:59:06 +0100 > Subject: Re: [Tutor] inheritance problem > > "Roelof Wobben" wrote > >> So i have this programm now : > >> class Deck: >> def __init__(self): >> self.cards = [] >> for suit in range(4): >> for rank in range(1, 14): >> self.cards.append(Card(suit, rank)) >> >> def deal(self, hands, num_cards=999): >> num_hands = len(hands) >> for i in range(num_cards): >> if self.is_empty(): break # break if out of cards >> card = self.pop() # take the top card >> hand = hands[i % num_hands] # whose turn is next? >> hand.add(card) # add the card to the hand >> >> def shuffle(self): >> import random >> num_cards = len(self.cards) >> for i in range(num_cards): >> j = random.randrange(i, num_cards) >> self.cards[i], self.cards[j] = self.cards[j], >> self.cards[i] >> >> def remove(self, card): >> if card in self.cards: >> self.cards.remove(card) >> return True >> else: >> return False >> def is_empty(self): >> return (len(self.cards) == 0) > >> But now Im getting this error message: >> >> Traceback (most recent call last): >> File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 126, >> in >> game.deck.deal([hand], 13) >> File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 24, >> in deal >> card = self.pop() # take the top card >> AttributeError: Deck instance has no attribute 'pop' >> >> >> What went wrong here. > > The error tells you what is wrong, the Deck has no attribute called > pop. > Can you see a pop anywhere? I only see a pop here : card = self.pop() # take the top card but no function called pop. > So if the code you copied has an error how should it assign a card? I assigned a card by this code : def deal(self, hands, num_cards=999): num_hands = len(hands) for i in range(num_cards): if self.is_empty(): break # break if out of cards card = self.pop() # take the top card hand = hands[i % num_hands] # whose turn is next? hand.add(card) # add the card to the hand > Where are the cards stored? How would you get the first card from > the collection? Does that work? The cards are stored in a directory named cards. The hands are stored in a directory named hands > > Think about what the error is telling you and think about how you > would fix it. If you don't understand what the error is saying then > tell > us and we can explain it, but in this case its pretty clearly stated > and is one you have seen before. The error is telling me that the function pop does not exist for a directory, So solution is to find out which object contains pop and then change the code. From rwobben at hotmail.com Fri Oct 1 09:18:38 2010 From: rwobben at hotmail.com (Roelof Wobben) Date: Fri, 1 Oct 2010 07:18:38 +0000 Subject: [Tutor] inheritance problem In-Reply-To: References: , , , Message-ID: ---------------------------------------- > From: rwobben at hotmail.com > To: alan.gauld at btinternet.com; tutor at python.org > Date: Fri, 1 Oct 2010 06:19:29 +0000 > Subject: Re: [Tutor] inheritance problem > > > > > ---------------------------------------- >> To: tutor at python.org >> From: alan.gauld at btinternet.com >> Date: Fri, 1 Oct 2010 00:59:06 +0100 >> Subject: Re: [Tutor] inheritance problem >> >> "Roelof Wobben" wrote >> >>> So i have this programm now : >> >>> class Deck: >>> def __init__(self): >>> self.cards = [] >>> for suit in range(4): >>> for rank in range(1, 14): >>> self.cards.append(Card(suit, rank)) >>> >>> def deal(self, hands, num_cards=999): >>> num_hands = len(hands) >>> for i in range(num_cards): >>> if self.is_empty(): break # break if out of cards >>> card = self.pop() # take the top card >>> hand = hands[i % num_hands] # whose turn is next? >>> hand.add(card) # add the card to the hand >>> >>> def shuffle(self): >>> import random >>> num_cards = len(self.cards) >>> for i in range(num_cards): >>> j = random.randrange(i, num_cards) >>> self.cards[i], self.cards[j] = self.cards[j], >>> self.cards[i] >>> >>> def remove(self, card): >>> if card in self.cards: >>> self.cards.remove(card) >>> return True >>> else: >>> return False >>> def is_empty(self): >>> return (len(self.cards) == 0) >> >>> But now Im getting this error message: >>> >>> Traceback (most recent call last): >>> File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 126, >>> in >>> game.deck.deal([hand], 13) >>> File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 24, >>> in deal >>> card = self.pop() # take the top card >>> AttributeError: Deck instance has no attribute 'pop' >>> >>> >>> What went wrong here. >> >> The error tells you what is wrong, the Deck has no attribute called >> pop. >> Can you see a pop anywhere? > > I only see a pop here : card = self.pop() # take the top card > but no function called pop. > > >> So if the code you copied has an error how should it assign a card? > > > I assigned a card by this code : > > def deal(self, hands, num_cards=999): > num_hands = len(hands) > for i in range(num_cards): > if self.is_empty(): break # break if out of cards > card = self.pop() # take the top card > hand = hands[i % num_hands] # whose turn is next? > hand.add(card) # add the card to the hand > > > >> Where are the cards stored? How would you get the first card from >> the collection? Does that work? > > The cards are stored in a directory named cards. > The hands are stored in a directory named hands > >> >> Think about what the error is telling you and think about how you >> would fix it. If you don't understand what the error is saying then >> tell >> us and we can explain it, but in this case its pretty clearly stated >> and is one you have seen before. > > > The error is telling me that the function pop does not exist for a directory, > So solution is to find out which object contains pop and then change the code. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Hello, I make a big mistake. Both card and hands are lists and pop can be used on a list. So as Joel said in this rule card = self.pop() there is no list named so I think it must be self.hands.pop() Roelof From alan.gauld at btinternet.com Fri Oct 1 09:29:33 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 1 Oct 2010 08:29:33 +0100 Subject: [Tutor] inheritance problem References: , , , Message-ID: "Roelof Wobben" wrote >>> So if the code you copied has an error how should it assign a >>> card? >> >> I assigned a card by this code : >> >> def deal(self, hands, num_cards=999): >> num_hands = len(hands) >> for i in range(num_cards): >> if self.is_empty(): break # break if out of cards >> card = self.pop() # take the top card >> hand = hands[i % num_hands] # whose turn is next? >> hand.add(card) # add the card to the hand >> No this code assigns several HANDS not just cards. The code that assigns a card is inside this function, but you need to narrow it down to the specific lines assigning the card. >>> Where are the cards stored? How would you get the first card from >>> the collection? Does that work? >> >> The cards are stored in a directory named cards. Correct. So how do you get a card from the collection stored in self.cards? >> The error is telling me that the function pop does not exist for a >> directory, >> So solution is to find out which object contains pop and then >> change the code. Correct > I make a big mistake. > Both card and hands are lists and pop can be used on a list. That's not a mistake, lists are objects too. > So as Joel said in this rule card = self.pop() there is no list > named so I think it must be self.hands.pop() If you want to assign a card and cards are stored in self.cards why would you use self.hands.pop()? Surely it should be self.cards.pop()? HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From rwobben at hotmail.com Fri Oct 1 09:39:39 2010 From: rwobben at hotmail.com (Roelof Wobben) Date: Fri, 1 Oct 2010 07:39:39 +0000 Subject: [Tutor] inheritance problem In-Reply-To: References: , , , , , , , Message-ID: >>> >>> I assigned a card by this code : >>> >>> def deal(self, hands, num_cards=999): >>> num_hands = len(hands) >>> for i in range(num_cards): >>> if self.is_empty(): break # break if out of cards >>> card = self.pop() # take the top card >>> hand = hands[i % num_hands] # whose turn is next? >>> hand.add(card) # add the card to the hand >>> > > No this code assigns several HANDS not just cards. > The code that assigns a card is inside this function, but > you need to narrow it down to the specific lines assigning the card. > Oke, The lines that assign card is : hand = hands[i % num_hands] # whose turn is next? hand.add(card) # add the card to the hand The first one takes care to who a card is assigned and the second one assigned it to that person. >>>> Where are the cards stored? How would you get the first card from >>>> the collection? Does that work? >>> >>> The cards are stored in a directory named cards. > > Correct. So how do you get a card from the collection stored > in self.cards? This is not correct. Card is a list and not a directory. See this : self.cards = [] >> I make a big mistake. >> Both card and hands are lists and pop can be used on a list. > > That's not a mistake, lists are objects too. oke, but I confused two objects and that is a mistake. > >> So as Joel said in this rule card = self.pop() there is no list >> named so I think it must be self.hands.pop() > > If you want to assign a card and cards are stored in self.cards > why would you use self.hands.pop()? > Surely it should be self.cards.pop()? It is. I know that because self.hands is not working. Now Im busy to make this difficult exercise of writing print_hands() But thanks for the time and explantion. OOP is difficult to understand I find. Roelof From alan.gauld at btinternet.com Fri Oct 1 09:46:11 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 1 Oct 2010 08:46:11 +0100 Subject: [Tutor] Mutable Properties References: <4CA52A52.5000302@gmail.com> Message-ID: "Chris King" wrote > I noticed that when you make a property to represent a mutable > value > > *class Obj(object): > prop = property(get, set) > test.prop = ['Hello'] Let's be clear, here you are assigning a list object to your property. > and then try and use one of its methods. > > *test.prop.append('World') And here you use a method of that list object. The property is just a name that refers to the list object. You are not using a method of the property you are using a method of the object to which the property refers. > *It will treat it as a get, not a set. Because it is a get. Your line of code is a shorthand way of writing theList = test.prop theList.append('World') > *Even thou you are basically changing its value. You are not changing the property's value, it is still the same list object. The property has done exactly what you asked it to do - get you the list object so that you can perform an append operation on it. > *I know this happens because I'm not technically setting it to > something > else, just getting one of its properties. You are getting the object you assigned because that's what you asked it to do. Python is not psychic, it cannot guess what you will do with the property value. > I was wondering how to make it fire off set to for certain methods. The property mechanism only works for access to the property value. It has no way of knowing what you will do with that value once you get it. What you need to do is create your own kind of list that prints get/set when you access the list. It is the list you are manipulating not the test class. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Fri Oct 1 09:54:35 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 1 Oct 2010 08:54:35 +0100 Subject: [Tutor] inheritance problem References: , , , , , , , Message-ID: "Roelof Wobben" wrote >>>> The cards are stored in a directory named cards. >> >> Correct. So how do you get a card from the collection stored >> in self.cards? > > This is not correct. Card is a list and not a directory. See this : > self.cards = [] OK, if thats the mistake you made then you made two mistakes. 1) cards is a list not a directory and 2) directory is something on your hard disk not a Python data type, you meant dictionary I assume? :-) > Now Im busy to make this difficult exercise of writing print_hands() > But thanks for the time and explantion. > OOP is difficult to understand I find. OOP is a step up from functions its true but you need to work to understand it or you will continue to get confused by trivial errors. Alan G From timomlists at gmail.com Fri Oct 1 11:19:52 2010 From: timomlists at gmail.com (Timo) Date: Fri, 1 Oct 2010 11:19:52 +0200 Subject: [Tutor] Connecting my users Message-ID: Hello, I have the following idea, but no clue how to implement this. I want my users to be able to connect to other users (with the same program) and chat with them and exchange files. Ideally without server interaction. Now I heard about peer-to-peer and bittorrent protocol etc. But don't know where to start or that I'm even at the right path. So if anyone can point me into the right direction of a framework or protocol I should use for this, it would be appreciated. Cheers, Timo -------------- next part -------------- An HTML attachment was scrubbed... URL: From nitinpawar432 at gmail.com Fri Oct 1 11:25:01 2010 From: nitinpawar432 at gmail.com (Nitin Pawar) Date: Fri, 1 Oct 2010 14:55:01 +0530 Subject: [Tutor] Connecting my users In-Reply-To: References: Message-ID: have a look at this http://bytes.com/topic/python/answers/826973-peer-peer-chat-program On Fri, Oct 1, 2010 at 2:49 PM, Timo wrote: > Hello, > > I have the following idea, but no clue how to implement this. > I want my users to be able to connect to other users (with the same > program) and chat with them and exchange files. Ideally without server > interaction. Now I heard about peer-to-peer and bittorrent protocol etc. But > don't know where to start or that I'm even at the right path. > So if anyone can point me into the right direction of a framework or > protocol I should use for this, it would be appreciated. > > Cheers, > Timo > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- Nitin Pawar -------------- next part -------------- An HTML attachment was scrubbed... URL: From songbird42371 at gmail.com Fri Oct 1 09:49:33 2010 From: songbird42371 at gmail.com (Colleen Glaeser) Date: Fri, 1 Oct 2010 02:49:33 -0500 Subject: [Tutor] Coin Toss Problems Message-ID: Dear Tutors, I have an assignment for class due in about 6 hours and I've been working on it for about 3 hours already, getting nowhere. My assignment is as follows: In this exercise, you will write several functions and use them in different ways. Follow the specifications; I insist. First, write a function that simulates flipping a coin. This function needs no parameters. When called, it should return either ?HEADS? or ?TAILS?. Those are strings. That?s *return*, not print. When that function is working, write another function to exercise it. This function should have one parameter, a positive number. This function will call the previous function that number of times, and count the number of heads. When finished, it should *return* (not print) the number of heads. Now, write code to test it. I want you to try three times. There are three well-known experiments that I want you to replicate: The French naturalist Count Buffon (1707-1788) tossed a coin 4040 times. Results: 2048 heads, or proportion 2048/4040 = 0.5069 for heads. Around 1900, the English statistician Karl Pearson tossed a coin 24,000 times. Result: 12,012 heads, a proportion of 0.5005. While imprisoned by the Germans during World War II, the South African mathematician John Kerrich tossed a coin 10,000 times. Result: 5067 heads, a proportion of 0.5067. For each of these, print out enough to explain the result, not just a number. One more thing to do: run the Pearson experiment 24000 times. Report the number of times that the count of heads exceeds the number of tails. I have gotten a few steps in with my programming, and have completed this, which will give me heads / tails for as many times as I want. However, it just lists either "Heads" or "Tails in a column. import random def coinToss(): flip = random.randrange(2) if flip == 0: return "Heads" else: return "Tails" def multicoinToss(tosses): for i in range(tosses): print (coinToss()) print (multicoinToss(20)) However, no matter how many ways I try to alter my code, I just can't work in some way or some accumulator pattern to add up my number of heads or tails! Can anybody help me figure out how to do it? I've found codes that work with accumulator coin tosses, but they are based on a preset number of tosses and have nothing to do with returns, etc. in them. Gah, I hope somebody here can help save me. :( -- Colleen Glaeser songbird42371 at gmail.com 636.357.8519 -------------- next part -------------- An HTML attachment was scrubbed... URL: From songbird42371 at gmail.com Fri Oct 1 09:49:33 2010 From: songbird42371 at gmail.com (Colleen Glaeser) Date: Fri, 1 Oct 2010 02:49:33 -0500 Subject: [Tutor] Coin Toss Problems Message-ID: Dear Tutors, I have an assignment for class due in about 6 hours and I've been working on it for about 3 hours already, getting nowhere. My assignment is as follows: In this exercise, you will write several functions and use them in different ways. Follow the specifications; I insist. First, write a function that simulates flipping a coin. This function needs no parameters. When called, it should return either ?HEADS? or ?TAILS?. Those are strings. That?s *return*, not print. When that function is working, write another function to exercise it. This function should have one parameter, a positive number. This function will call the previous function that number of times, and count the number of heads. When finished, it should *return* (not print) the number of heads. Now, write code to test it. I want you to try three times. There are three well-known experiments that I want you to replicate: The French naturalist Count Buffon (1707-1788) tossed a coin 4040 times. Results: 2048 heads, or proportion 2048/4040 = 0.5069 for heads. Around 1900, the English statistician Karl Pearson tossed a coin 24,000 times. Result: 12,012 heads, a proportion of 0.5005. While imprisoned by the Germans during World War II, the South African mathematician John Kerrich tossed a coin 10,000 times. Result: 5067 heads, a proportion of 0.5067. For each of these, print out enough to explain the result, not just a number. One more thing to do: run the Pearson experiment 24000 times. Report the number of times that the count of heads exceeds the number of tails. I have gotten a few steps in with my programming, and have completed this, which will give me heads / tails for as many times as I want. However, it just lists either "Heads" or "Tails in a column. import random def coinToss(): flip = random.randrange(2) if flip == 0: return "Heads" else: return "Tails" def multicoinToss(tosses): for i in range(tosses): print (coinToss()) print (multicoinToss(20)) However, no matter how many ways I try to alter my code, I just can't work in some way or some accumulator pattern to add up my number of heads or tails! Can anybody help me figure out how to do it? I've found codes that work with accumulator coin tosses, but they are based on a preset number of tosses and have nothing to do with returns, etc. in them. Gah, I hope somebody here can help save me. :( -- Colleen Glaeser songbird42371 at gmail.com 636.357.8519 -------------- next part -------------- An HTML attachment was scrubbed... URL: From venefyxatu+python at gmail.com Fri Oct 1 12:47:42 2010 From: venefyxatu+python at gmail.com (Erik H.) Date: Fri, 1 Oct 2010 12:47:42 +0200 Subject: [Tutor] Coin Toss Problems In-Reply-To: References: Message-ID: <20101001104724.GA5861@lap008.cereusace.be> On Fri, Oct 01, 2010 at 02:49:33 -0500, Colleen Glaeser wrote: > > > First, write a function that simulates flipping a coin. This function needs > no parameters. When called, it should return either ?HEADS? or ?TAILS?. Those > are strings. That?s *return*, not print. Looks good, though I prefer random.choice(["HEADS", "TAILS"]) to an if structure, just because it's shorter. I'm sure endless debates can be held about various ways to do this though :-) > > When that function is working, write another function to exercise it. This > function should have one parameter, a positive number. This function will > call the previous function that number of times, and count the number of > heads. When finished, it should *return* (not print) the number of heads. Your multiCoinToss function prints every coin toss. What it should do, according to this, is count the heads and return that number. Toss a coin, check if it's heads and if so count it. End by returning the counter. Btw, you might want to raise an error if your function receives a negative number. > > For each of these, print out enough to explain the result, not just a > number. > You'll want to calculate the ratio for every one of your head counts. Hint: print "Ratio %f" % heads/tosses.0 > > One more thing to do: run the Pearson experiment 24000 times. Report the > number of times that the count of heads exceeds the number of tails. > Once again, increment a counter :-) Hope that gets you on your way! Erik -- Erik Heeren E-mail: venefyxatu at gmail.com PGP ID: 0xC5FE6DEB http://blog.venefyxatu.be -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From delegbede at dudupay.com Fri Oct 1 12:52:54 2010 From: delegbede at dudupay.com (delegbede at dudupay.com) Date: Fri, 1 Oct 2010 10:52:54 +0000 Subject: [Tutor] Coin Toss Problems In-Reply-To: References: Message-ID: <432989115-1285930375-cardhu_decombobulator_blackberry.rim.net-1434736909-@bda308.bisx.produk.on.blackberry> This is also a learner's advice, so just give it a shot. In the first function, set HEADS to 0 and TAILS to 0. Then if flip==0, heads=heads+1 Return Heads. Do same for Tails and see if it makes any sense. Regards. Sent from my BlackBerry wireless device from MTN -----Original Message----- From: Colleen Glaeser Sender: tutor-bounces+delegbede=dudupay.com at python.org Date: Fri, 1 Oct 2010 02:49:33 To: Subject: [Tutor] Coin Toss Problems _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Fri Oct 1 13:05:03 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Fri, 1 Oct 2010 04:05:03 -0700 (PDT) Subject: [Tutor] inheritance problem In-Reply-To: References: , , , , , , , , , , , , , Message-ID: <647618.97867.qm@web86705.mail.ird.yahoo.com> > > OOP is a step up from functions its true but you need to work to > > understand it or you will continue to get confused by trivial errors. > > Then appearently I don't work hard enough. > Im still getting confuse by this example. > > I wrote this piece : > > def print_hands(self): > for kaart in self.hands: > print kaart > return When describing methods please tell us which class you are adding them to. Use the same trick your tutorial uses with ellipses: class ????: ... def print_hands(self): for kaart in self.hands: print kaart return In your case I assume you added it to the OldMaidGame class? > game = CardGame() Here you create an instance of CardGame... > hand = OldMaidHand("frank") > deck = Deck() > spel = OldMaidGame() Now you have an OldMaid hand a deck and an OldMaid game. You never use the deck? > game.deck.deal([hand], 13) Now you use the original CardGame instance to deal the OldMaidHand. > OldMaidGame.print_hands(spel) > > But now Im getting this error : > > AttributeError: OldMaidGame instance has no attribute 'hands' And that is true. When does the OldMaidGame create a hands attribute? Hint: It has no init method and the inherited CardGame init only creates a deck so it can't be when the instance is created. (This is why I said its always good to create an init method!) > def remove_all_matches(self): > count = 0 > for hand in self.hands: > count = count + hand.remove_matches() > return count > > So OldMaidGame has a attribute self.hands. Sometimes but not always. This means the class is "stateful" and is generally a bad design pattern. > I think Im giving up on OOP To be fair to you I don't think this example is a very good example of OOP. It is overly complex and disguising the points that its trying to teach with a quite convoluted set of interactions. This would be fine as a case study at the end of a series of topics on OOP, but to use this to teach inheritance is not helpful IMHO. The author starts off by saying: " The primary advantage of this feature is that you can add new methods to a class without modifying the existing class. ..." I don't think thats the primary advantage and in fact you are better if you do not add new methods but simply modify the existing ones! "....Also, inheritance can facilitate code reuse, since you can customize the behavior of parent classes without having to modify them. " This is the real power of inheritance, although not just for code reuse but to enable polymorphism. Which unfortunately the author does not even mention, let alone describe. "... inheritance can make programs difficult to read. When a method is invoked, it is sometimes not clear where to find its definition. The relevant code may be scattered among several modules. " The author appears to be attempting to illustrate this last feature of inheritance rather than the benefits! I suspect you may be better off finding a simpler introduction to OOP and coming back to this once you understand the basics more clearly. Alan G. http://www.alan-g.me.uk/ From norman at khine.net Fri Oct 1 13:34:01 2010 From: norman at khine.net (Norman Khine) Date: Fri, 1 Oct 2010 13:34:01 +0200 Subject: [Tutor] regex advise + email validate Message-ID: hello, i have this code http://pastie.org/1193091 i would like to extend this so that it validates TLD's such as .travel and .museum, i can do this by changing {2,4} to {2,7} but this sort of defeats the purpose of validating the correct email address. are there any python libraries available to use for this? thanks -- ?u?op ?p?sdn p,u?n? p??o? ??? ??s no? '?u???? s???? ??? pu? '?u??uo? ?q s,??? ??? %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From norman at khine.net Fri Oct 1 14:27:12 2010 From: norman at khine.net (Norman Khine) Date: Fri, 1 Oct 2010 14:27:12 +0200 Subject: [Tutor] regex advise + email validate In-Reply-To: <4CA5CEEC.3080503@compuscan.co.za> References: <4CA5CEEC.3080503@compuscan.co.za> Message-ID: thanks for the links, i was just looking at the regular-expressions.info site. On Fri, Oct 1, 2010 at 2:07 PM, Christian Witts wrote: > On 01/10/2010 13:34, Norman Khine wrote: >> >> hello, i have this code >> >> http://pastie.org/1193091 >> >> i would like to extend this so that it validates TLD's such as .travel >> and .museum, i can do this by changing {2,4} to {2,7} but this sort of >> defeats the purpose of validating the correct email address. >> >> are there any python libraries available to use for this? >> >> thanks >> >> > > Well you could look at the implementation of RFC 2822 [1] on > regular-expressions.info [2] and use the more practical implementation under > the "The Official Standard: RFC 2822" section. > > For python packages you can look at fv_email [3]. > > [1] http://tools.ietf.org/html/rfc2822#section-3.4.1 > [2] http://www.regular-expressions.info/email.html > [3] http://pypi.python.org/pypi/fv_email/0.9 > > -- > Kind Regards, > Christian Witts > > > -- ?u?op ?p?sdn p,u?n? p??o? ??? ??s no? '?u???? s???? ??? pu? '?u??uo? ?q s,??? ??? %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From steve at pearwood.info Fri Oct 1 15:20:51 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 1 Oct 2010 23:20:51 +1000 Subject: [Tutor] regex advise + email validate In-Reply-To: References: Message-ID: <201010012320.52429.steve@pearwood.info> On Fri, 1 Oct 2010 09:34:01 pm Norman Khine wrote: > hello, i have this code > > http://pastie.org/1193091 > > i would like to extend this so that it validates TLD's such as > .travel and .museum, i can do this by changing {2,4} to {2,7} but > this sort of defeats the purpose of validating the correct email > address. The only good advice for using regular expressions to validate emails addresses is... Don't. Just don't even try. The only way to validate an email address is to actually try to send email to it and see if it can be delivered. That is the ONLY way to know if an address is valid. First off, even if you could easily detect invalid addresses -- and you can't, but for the sake of the argument let's pretend you can -- then this doesn't help you at all. fred at example.com is syntactically valid, but I guarantee that it will *never* be deliverable. asgfkagfkdgfkasdfg at hdsgfjdshgfjhsdfg.com is syntactically correct, and it *could* be a real address, but if you can actually deliver mail to it, I'll eat my hat. If you absolutely must try to detect syntactically invalid addresses, the most you should bother is to check that the string isn't blank. If you don't care about local addresses, you can also check that it contains at least one @ sign. (A little known fact is that email addresses can contain multiple @ signs.) Other than that, leave it up to the mail server to validate the address -- which it does by trying to deliver mail to it. Somebody has created a Perl regex to validate *some* email addresses. Even this one doesn't accept all valid addresses, although it comes close: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html Read it and weep. See here for more info: http://northernplanets.blogspot.com/2007/03/how-not-to-validate-email-addresses.html This is exactly the sort of thing that you should avoid like the plague: http://www.regular-expressions.info/email.html This is full of bad advice. This clown arrogantly claims that his regex "matches any email address". It doesn't. He then goes on to admit "my claim only holds true when one accepts my definition of what a valid email address really is". Oh really? What about the RFC that *defines* what email addresses are? Shouldn't that count for more than the misinformed opinion of somebody who arrogantly dismisses bug reports for his regex because it "matches 99% of the email addresses in use today"? 99% sounds like a lot, but if you have 20,000 people use your software, that's 200 whose valid email address will be misidentified. He goes on to admit that his regex wrongly rejects .museum addresses, but he considers that acceptable. He seriously suggests that it would be a good idea for your program to list all the TLDs, and even all the country codes, even though "by the time you read this, the list might already be out of date". This is shonky programming. Avoid it like poison. -- Steven D'Aprano From norman at khine.net Fri Oct 1 15:39:29 2010 From: norman at khine.net (Norman Khine) Date: Fri, 1 Oct 2010 15:39:29 +0200 Subject: [Tutor] regex advise + email validate In-Reply-To: <201010012320.52429.steve@pearwood.info> References: <201010012320.52429.steve@pearwood.info> Message-ID: hi steven, thanks for the in-depth info, yes i am aware that email validation is not full proof until you actually send an email and then validate that you have a response, i guess dnspython helps a little in this respect and integration with OpenID and the like. for my needs i just wanted to find a simple way to include in the regex the .travel TLD i suppose international tld's would not be supported either http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains#Internationalized_country_code_top-level_domains using the regex. On Fri, Oct 1, 2010 at 3:20 PM, Steven D'Aprano wrote: > On Fri, 1 Oct 2010 09:34:01 pm Norman Khine wrote: >> hello, i have this code >> >> http://pastie.org/1193091 >> >> i would like to extend this so that it validates TLD's such as >> .travel and .museum, i can do this by changing {2,4} to {2,7} but >> this sort of defeats the purpose of validating the correct email >> address. > > The only good advice for using regular expressions to validate emails > addresses is... > > Don't. > > Just don't even try. > > The only way to validate an email address is to actually try to send > email to it and see if it can be delivered. That is the ONLY way to > know if an address is valid. > > First off, even if you could easily detect invalid addresses -- and you > can't, but for the sake of the argument let's pretend you can -- then > this doesn't help you at all. fred at example.com is syntactically valid, > but I guarantee that it will *never* be deliverable. > > asgfkagfkdgfkasdfg at hdsgfjdshgfjhsdfg.com is syntactically correct, and > it *could* be a real address, but if you can actually deliver mail to > it, I'll eat my hat. > > If you absolutely must try to detect syntactically invalid addresses, > the most you should bother is to check that the string isn't blank. If > you don't care about local addresses, you can also check that it > contains at least one @ sign. (A little known fact is that email > addresses can contain multiple @ signs.) Other than that, leave it up > to the mail server to validate the address -- which it does by trying > to deliver mail to it. > > Somebody has created a Perl regex to validate *some* email addresses. > Even this one doesn't accept all valid addresses, although it comes > close: > > http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html > > Read it and weep. > > See here for more info: > > http://northernplanets.blogspot.com/2007/03/how-not-to-validate-email-addresses.html > > This is exactly the sort of thing that you should avoid like the plague: > > http://www.regular-expressions.info/email.html > > This is full of bad advice. This clown arrogantly claims that his > regex "matches any email address". It doesn't. He then goes on to > admit "my claim only holds true when one accepts my definition of what > a valid email address really is". Oh really? What about the RFC that > *defines* what email addresses are? Shouldn't that count for more than > the misinformed opinion of somebody who arrogantly dismisses bug > reports for his regex because it "matches 99% of the email addresses in > use today"? > > 99% sounds like a lot, but if you have 20,000 people use your software, > that's 200 whose valid email address will be misidentified. > > He goes on to admit that his regex wrongly rejects .museum addresses, > but he considers that acceptable. He seriously suggests that it would > be a good idea for your program to list all the TLDs, and even all the > country codes, even though "by the time you read this, the list might > already be out of date". > > This is shonky programming. Avoid it like poison. > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- ?u?op ?p?sdn p,u?n? p??o? ??? ??s no? '?u???? s???? ??? pu? '?u??uo? ?q s,??? ??? %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From alan.gauld at btinternet.com Fri Oct 1 16:10:02 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 1 Oct 2010 15:10:02 +0100 Subject: [Tutor] Coin Toss Problems References: <432989115-1285930375-cardhu_decombobulator_blackberry.rim.net-1434736909-@bda308.bisx.produk.on.blackberry> Message-ID: wrote > This is also a learner's advice, so just give it a shot. > In the first function, set HEADS to 0 and TAILS to 0. In general thats good advice but Colleen was explicitly told to return HEADS and TAILS as strings. But using strings doesn't add much complexity overall in this case. Alan G. From alan.gauld at btinternet.com Fri Oct 1 16:15:13 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 1 Oct 2010 15:15:13 +0100 Subject: [Tutor] Coin Toss Problems References: Message-ID: "Colleen Glaeser" wrote > First, write a function that simulates flipping a coin. This > function needs > no parameters. When called, it should return either ?HEADS? or > ?TAILS?. > Those are strings. That?s *return*, not print. OK, You've done this > When that function is working, write another function to exercise > it. And you have almost done this > function should have one parameter, a positive number. This > function will > call the previous function that number of times, and count the > number of > heads. When finished, it should *return* (not print) the number of > heads. But not quite... > def multicoinToss(tosses): > for i in range(tosses): So far so good > print (coinToss()) But instead of printing you want to count heads Do you know how to test if the result of your function is "HEADS"? Do you know how to increment a counter - ie add one to it? Do you know how to return that value from your function at the end of the for loop? You should do, because you have done something very similar to 1 and 3 in your coinToss function. > Can anybody help me figure out how to do it? Because its homework we won;t give you the answer just some hints. Have another go and post the result if it doesn't work. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From mehgcap at gmail.com Fri Oct 1 17:14:27 2010 From: mehgcap at gmail.com (Alex Hall) Date: Fri, 1 Oct 2010 11:14:27 -0400 Subject: [Tutor] regexp: a bit lost In-Reply-To: <201010011533.28202.steve@pearwood.info> References: <201010011533.28202.steve@pearwood.info> Message-ID: On 10/1/10, Steven D'Aprano wrote: > On Fri, 1 Oct 2010 12:45:38 pm Alex Hall wrote: >> Hi, once again... >> I have a regexp that I am trying to use to make sure a line matches >> the format: [c*]n [c*]n n >> where c* is (optionally) 0 or more non-numeric characters and n is >> any numeric character. The spacing should not matter. These should >> pass: v1 v2 5 >> 2 someword7 3 >> >> while these should not: >> word 2 3 >> 1 2 >> >> Here is my test: >> s=re.search(r"[\d+\s+\d+\s+\d]", l) > > Try this instead: > > re.search(r'\d+\s+\D*\d+\s+\d', l) > > This searches for: > one or more digits > at least one whitespace char (space, tab, etc) > zero or more non-digits > at least one digit > at least one whitespace > exactly one digit Makes sense. > > >> However: >> 1. this seems to pass with *any* string, even when l is a single >> character. This causes many problems > [...] > > I'm sure it does. > > You don't have to convince us that if the regular expression is broken, > the rest of your code has a problem. That's a given. It's enough to > know that the regex doesn't do what you need it to do. Understood. My intent was to ask why my regexp would match anything at all. > > >> 3. Once I get the above working, I will need a way of pulling the >> characters out of the string and sticking them somewhere. For >> example, if the string were >> v9 v10 15 >> I would want an array: >> n=[9, 10, 15] > > > Modify the regex to be this: > > r'(\d+)\s+\D*(\d+)\s+(\d)' > > and then query the groups of the match object that is returned: > >>>> mo = re.search(r'(\d+)\s+\D*(\d+)\s+(\d)', 'spam42 eggs23 9') >>>> mo.groups() > ('42', '23', '9') > > Don't forget that mo will be None if the regex doesn't match, and don't > forget that the items returned are strings. Alright that worked perfectly, after a lot of calls to int()! I also finally understand what a group is and, at a basic level, how to use it. I have wondered how to extract matched text from a string for a long time, and this has finally answered that. Thanks! > > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From fomcl at yahoo.com Fri Oct 1 20:08:22 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 1 Oct 2010 11:08:22 -0700 (PDT) Subject: [Tutor] (de)serialization questions In-Reply-To: References: Message-ID: <933247.41889.qm@web110704.mail.gq1.yahoo.com> Hi Lee, Thanks for your response. Maybe my main question is as follows: what permanent object is most suitable to store a large amount of entries (maybe too many to fit into the computer's memory), which can be looked up very fast. Eventually, I want to create two objects: 1-one to look up street name and city using zip code 2-one to look up zip code using street name, apartment number and city I stored object1 in a marshalled dictionary. Its length is about 450.000 (I live in Holland, not THAT many streets). Look-ups are incredibly fast (it has to, because it's part of an autocompletion feature of a data entry program). I haven't got the street number data needed for object2 yet, but it's going to be much larger. Many streets have different zip codes for odd or even numbers, or the zip codes are divided into street number ranges (for long streets). You suggest to simply use a file. I like simple solutions, but doesn't that, by definition, require a slow, linear search? I chose a dictionary for object1 because hash tables are very fast in terms of lookup times. Another option might be to use the bisect module. Not sure if using a binary search algoritm is much faster with these still relatively small numbers. It's interesting for sure, but I don't know whether using bisect would justify the added amount of complexity and bug risks. Funny you should mention sqlite: I was just considering it yesterday. Gosh, Python has so much interesting stuff to offer! Cheers!! Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ________________________________ From: Lee Harr To: tutor at python.org Sent: Fri, October 1, 2010 12:37:58 AM Subject: Re: [Tutor] (de)serialization questions > I have data about zip codes, street and city names (and perhaps later also of > street numbers). I made a dictionary of the form {zipcode: (street, city)} One dictionary with all of the data? That does not seem like it will work. What happens when 2 addresses have the same zip code? > Are there forms of object permanence that do not read all data into memory? How about a file? You could write your data one record per line, and then read it in one line at a time also. You could also use a csv file, or maybe use a database like sqlite. _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From aeneas24 at priest.com Fri Oct 1 22:31:42 2010 From: aeneas24 at priest.com (aeneas24 at priest.com) Date: Fri, 01 Oct 2010 16:31:42 -0400 Subject: [Tutor] Getting total counts Message-ID: <8CD2FBECAFF8883-83C-18F87@web-mmc-m05.sysops.aol.com> Hi, I have created a csv file that lists how often each word in the Internet Movie Database occurs with different star-ratings and in different genres. The input file looks something like this--since movies can have multiple genres, there are three genre rows. (This is fake, simplified data.) ID | Genre1 | Genre2 | Genre3 | Star-rating | Word | Count film1 Drama Thriller Western 1 the 20 film2 Comedy Musical NA 2 the 20 film3 Musical History Biography 1 the 20 film4 Drama Thriller Western 1 the 10 film5 Drama Thriller Western 9 the 20 I can get the program to tell me how many occurrence of "the" there are in Thrillers (50), how many "the"'s in 1-stars (50), and how many 1-star drama "the"'s there are (30). But I need to be able to expand beyond a particular word and say "how many words total are in "Drama"? How many total words are in 1-star ratings? How many words are there in the whole corpus? On these all-word totals, I'm stumped. What I've done so far: I used shelve() to store my input csv in a database format. Here's how I get count information so far: def get_word_count(word, db, genre=None, rating=None): c = 0 vals = db[word] for val in vals: if not genre and not rating: c += val['count'] elif genre and not rating: if genre in val['genres']: c += val['count'] elif rating and not genre: if rating == val['rating']: c += val['count'] else: if rating == val['rating'] and genre in val['genres']: c += val['count'] return c (I think there's something a little wrong with the rating stuff, here, but this code generally works and produces the right counts.) With "get_word_count" I can do stuff like this to figure out how many times "the" appears in a particular genre. vals=db[word] for val in vals: genre_ct_for_word = get_word_count(word, db, genre, rating=None) return genre_ct_for_word I've tried to extend this thinking to get TOTAL genre/rating counts for all words, but it doesn't work. I get a type error saying that string indices must be integers. I'm not sure how to overcome this. # Doesn't work: def get_full_rating_count(db, rating=None): full_rating_ct = 0 vals = db for val in vals: if not rating: full_rating_ct += val['count'] elif rating == val['rating']: if rating == val['rating']: # Um, I know this looks dumb, but in the other code it seems to be necessary for things to work. full_rating_ct += val['count'] return full_rating_ct Can anyone suggest how to do this? Thanks! Tyler Background for the curious: What I really want to know is which words are over- or under-represented in different Genre x Rating categories. "The" should be flat, but something like "wow" should be over-represented in 1-star and 10-star ratings and under-represented in 5-star ratings. Something like "gross" may be over-represented in low-star ratings for romances but if grossness is a good thing in horror movies, then we'll see "gross" over-represented in HIGH-star ratings for horror. To figure out over-representation and under-representation I need to compare "observed" counts to "expected" counts. The expected counts are probabilities and they require me to understand how many words I have in the whole corpus and how many words in each rating category and how many words in each genre category. -------------- next part -------------- An HTML attachment was scrubbed... URL: From missive at hotmail.com Sat Oct 2 00:56:21 2010 From: missive at hotmail.com (Lee Harr) Date: Sat, 2 Oct 2010 03:26:21 +0430 Subject: [Tutor] (de)serialization questions Message-ID: >>> I have data about zip codes, street and city names (and perhaps later also of >>> street numbers). I made a dictionary of the form {zipcode: (street, city)} >> >> One dictionary with all of the data? >> >> That does not seem like it will work. What happens when >> 2 addresses have the same zip code? You did not answer this question. Did you think about it? > Maybe my main question is as follows: what permanent object is most suitable to > store a large amount of entries (maybe too many to fit into the computer's > memory), which can be looked up very fast. One thing about Python is that you don't normally need to think about how your objects are stored (memory management). It's an advantage in the normal case -- you just use the most convenient object, and if it's fast enough and small enough you're good to go. Of course, that means that if it is not fast enough, or not small enough, then you've got to do a bit more work to do. > Eventually, I want to create two objects: > 1-one to look up street name and city using zip code So... you want to have a function like: def addresses_by_zip(zipcode): ??? '''returns list of all addresses in the given zipcode''' ??? .... > 2-one to look up zip code using street name, apartment number and city and another one like: def zip_by_address(street_name, apt, city): ??? '''returns the zipcode for the given street name, apartment, and city''' ??? .... To me, it sounds like a job for a database (at least behind the scenes), but you could try just creating a custom Python object that holds these things: class Address(object): ??? street_number = '345' ??? street_name = 'Main St' ??? apt = 'B' ??? city = 'Springfield' ??? zipcode = '99999' Then create another object that holds a collection of these addresses and has methods addresses_by_zip(self, zipcode) and zip_by_address(self, street_number, street_name, apt, city) > I stored object1 in a marshalled dictionary. Its length is about 450.000 (I live > in Holland, not THAT many streets). Look-ups are incredibly fast (it has to, > because it's part of an autocompletion feature of a data entry program). I > haven't got the street number data needed for object2 yet, but it's going to be > much larger. Many streets have different zip codes for odd or even numbers, or > the zip codes are divided into street number ranges (for long streets). Remember that you don't want to try to optimize too soon. Build a simple working system and see what happens. If it is too slow or takes up too much memory, fix it. > You suggest to simply use a file. I like simple solutions, but doesn't that, by > definition, require a slow, linear search? You could create an index, but then any database will already have an indexing function built in. I'm not saying that rolling your own custom database is a bad idea, but if you are trying to get some work done (and not just playing around and learning Python) then it's probably better to use something that is already proven to work. If you have some code you are trying out, but are not sure you are going the right way, post it and let people take a look at it. From steve at pearwood.info Sat Oct 2 02:19:21 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 2 Oct 2010 10:19:21 +1000 Subject: [Tutor] regexp: a bit lost In-Reply-To: References: <201010011533.28202.steve@pearwood.info> Message-ID: <201010021019.21909.steve@pearwood.info> On Sat, 2 Oct 2010 01:14:27 am Alex Hall wrote: > >> Here is my test: > >> s=re.search(r"[\d+\s+\d+\s+\d]", l) > > > > Try this instead: > > > > re.search(r'\d+\s+\D*\d+\s+\d', l) [...] > Understood. My intent was to ask why my regexp would match anything > at all. Square brackets create a character set, so your regex tests for a string that contains a single character matching a digit (\d), a plus sign (+) or a whitespace character (\s). The additional \d + \s in the square brackets are redundant and don't add anything. -- Steven D'Aprano From mehgcap at gmail.com Sat Oct 2 02:47:29 2010 From: mehgcap at gmail.com (Alex Hall) Date: Fri, 1 Oct 2010 20:47:29 -0400 Subject: [Tutor] regexp: a bit lost In-Reply-To: <201010021019.21909.steve@pearwood.info> References: <201010011533.28202.steve@pearwood.info> <201010021019.21909.steve@pearwood.info> Message-ID: On 10/1/10, Steven D'Aprano wrote: > On Sat, 2 Oct 2010 01:14:27 am Alex Hall wrote: >> >> Here is my test: >> >> s=re.search(r"[\d+\s+\d+\s+\d]", l) >> > >> > Try this instead: >> > >> > re.search(r'\d+\s+\D*\d+\s+\d', l) > [...] >> Understood. My intent was to ask why my regexp would match anything >> at all. > > Square brackets create a character set, so your regex tests for a string > that contains a single character matching a digit (\d), a plus sign (+) > or a whitespace character (\s). The additional \d + \s in the square > brackets are redundant and don't add anything. Oh, that explains it then. :) Thanks. > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From alan.gauld at btinternet.com Sat Oct 2 03:01:40 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 2 Oct 2010 02:01:40 +0100 Subject: [Tutor] (de)serialization questions References: <933247.41889.qm@web110704.mail.gq1.yahoo.com> Message-ID: "Albert-Jan Roskam" wrote > Maybe my main question is as follows: what permanent object is most > suitable to > store a large amount of entries (maybe too many to fit into the > computer's > memory), which can be looked up very fast. It depends on the nature of the object and the lookup but in general a database would be the best solution. For special (heirarchical) data an LDAP directory may be more appropriate. Otherwise you are looking at a custom designed file structure. > Eventually, I want to create two objects: > 1-one to look up street name and city using zip code > 2-one to look up zip code using street name, apartment number and > city For this a simple relational database wouldbe best. SQLlite should do and is part of the standard library. It can also be used in memory for faster speed with smaller data sets. > You suggest to simply use a file. I like simple solutions, but > doesn't that, by > definition, require a slow, linear search? No, you can use random access provided yopu can relate the key to the location - thats what databases do for you under the covers. > Funny you should mention sqlite: I was just considering it > yesterday. Gosh, > Python has so much interesting stuff to offer! Sqlite operating in-memory would be a good solution for you I think. You can get a basic tutorial on Sqllite and python in the databases topic of my tutorial... HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Sat Oct 2 03:13:04 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 2 Oct 2010 11:13:04 +1000 Subject: [Tutor] Getting total counts In-Reply-To: <8CD2FBECAFF8883-83C-18F87@web-mmc-m05.sysops.aol.com> References: <8CD2FBECAFF8883-83C-18F87@web-mmc-m05.sysops.aol.com> Message-ID: <201010021113.04679.steve@pearwood.info> On Sat, 2 Oct 2010 06:31:42 am aeneas24 at priest.com wrote: > Hi, > > I have created a csv file that lists how often each word in the > Internet Movie Database occurs with different star-ratings and in > different genres. I would have thought that IMDB would probably have already made that information available? http://www.imdb.com/interfaces > The input file looks something like this--since > movies can have multiple genres, there are three genre rows. (This is > fake, simplified data.) [...] > I can get the program to tell me how many occurrence of "the" there > are in Thrillers (50), how many "the"'s in 1-stars (50), and how many > 1-star drama "the"'s there are (30). But I need to be able to expand > beyond a particular word and say "how many words total are in > "Drama"? How many total words are in 1-star ratings? How many words > are there in the whole corpus? On these all-word totals, I'm stumped. The headings of your data look like this: ID | Genre1 | Genre2 | Genre3 | Star-rating | Word | Count and you want to map words to genres. Can you tell us how big the CSV file is? Depending on its size, you may need to use on-disk storage (perhaps shelve, as you're already doing) but for illustration purposes I'll assume it all fits in memory and just use regular dicts. I'm going to create a table that stores the counts for each word versus the genre: Genre | the | scary | silly | exciting | ... ------------------------------------------------ Western | 934 | 3 | 5 | 256 | Thriller | 899 | 145 | 84 | 732 | Comedy | 523 | 1 | 672 | 47 | ... To do this using dicts, I'm going to use a dict for genres: genre_table = {"Western": table_of_words, ...} and each table_of_words will look like: {'the': 934, 'scary': 3, 'silly': 5, ...} Let's start with a helper function and table to store the data. # Initialise the table. genres = {} def add_word(genre, word, count): genre = genre.title() # force "gEnRe" to "Genre" word = word.lower() # force "wOrD" to "word" count = int(count) row = genres.get(genre, {}) n = row.get(word, 0) row[word] = n + count genres[genre] = row We can simplify this code by using the confusingly named, but useful, setdefault method of dicts: def add_word(genre, word, count): genre = genre.title() word = word.lower() count = int(count) row = genres.setdefault(genre, {}) row[word] = row.get(word, 0) + count Now let's process the CSV file. I'm afraid I can't remember how the CSV module works, and I'm too lazy to look it up, so this is pseudo-code rather than Python: for row in csv file: genre1 = get column Genre1 genre2 = get column Genre2 genre3 = get column Genre3 word = get column Word count = get column Count add_word(genre1, word, count) add_word(genre2, word, count) add_word(genre3, word, count) Now we can easily query our table for useful information: # list of unique words for the Western genre genres["Western"].keys() # count of unique words for the Romance genre len(genres["Romance"]) # number of times "underdog" is used in Sports movies genres["Sport"]["underdog"] # total count of words for the Comedy genre sum(genres["Comedy"].values()) Do you want to do lookups efficiently the other way as well? It's easy to add another table: Word | Western | Thriller | ... ------------------------------------------------ the | 934 | 899 | scary | 3 | 145 | ... Add a second global table: genres = {} words = {} and modify the helper function: def add_word(genre, word, count): genre = genre.title() word = word.lower() count = int(count) # Add word to the genres table. row = genres.setdefault(genre, {}) row[word] = row.get(word, 0) + count # And to the words table. row = words.setdefault(word, {}) row[genre] = row.get(genre, 0) + count -- Steven D'Aprano From rwobben at hotmail.com Sat Oct 2 10:35:13 2010 From: rwobben at hotmail.com (Roelof Wobben) Date: Sat, 2 Oct 2010 08:35:13 +0000 Subject: [Tutor] data question Message-ID: Hello, As a test I would write a programm where a user can input game-data like home-team, away-team, home-score, away-score) and makes a ranking of it. And I'm not looking of a OOP solution because im not comfertle with OOP. Now my question is : In which datatype can I put this data in. I thought myself of a dictonary of tuples. Regards, Roelof From timomlists at gmail.com Sat Oct 2 12:06:14 2010 From: timomlists at gmail.com (Timo) Date: Sat, 02 Oct 2010 12:06:14 +0200 Subject: [Tutor] Connecting my users In-Reply-To: References: Message-ID: <4CA70416.2050205@gmail.com> On 01-10-10 11:25, Nitin Pawar wrote: > have a look at this > > http://bytes.com/topic/python/answers/826973-peer-peer-chat-program Thanks, but that still uses a server. And even one that I can't control! If it has to be with server interaction, than as little as possible is preferred and option to put it on my own. Cheers, Timo > > On Fri, Oct 1, 2010 at 2:49 PM, Timo > wrote: > > Hello, > > I have the following idea, but no clue how to implement this. > I want my users to be able to connect to other users (with the > same program) and chat with them and exchange files. Ideally > without server interaction. Now I heard about peer-to-peer and > bittorrent protocol etc. But don't know where to start or that I'm > even at the right path. > So if anyone can point me into the right direction of a framework > or protocol I should use for this, it would be appreciated. > > Cheers, > Timo > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > > > -- > Nitin Pawar > From alan.gauld at btinternet.com Sat Oct 2 15:10:25 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 2 Oct 2010 14:10:25 +0100 Subject: [Tutor] data question References: Message-ID: "Roelof Wobben" wrote > As a test I would write a programm where a user can input game-data > like home-team, away-team, home-score, away-score) and makes a > ranking of it. > In which datatype can I put this data in. > > I thought myself of a dictonary of tuples. A dictionary would be good for the basic data but I assume there are more than one of these data items? If so what do they represent? How would they be used? We need a bit more information, even some sample datya might help. It could be a list of dictionaries or even a dictionary of dictionaries. Alan G. From rwobben at hotmail.com Sat Oct 2 15:10:03 2010 From: rwobben at hotmail.com (Roelof Wobben) Date: Sat, 2 Oct 2010 13:10:03 +0000 Subject: [Tutor] data question In-Reply-To: References: , <007801cb6232$18d1a370$4a74ea50$@rr.com>, Message-ID: ---------------------------------------- > From: rwobben at hotmail.com > To: bermanrl at cfl.rr.com > Subject: RE: [Tutor] data question > Date: Sat, 2 Oct 2010 13:09:23 +0000 > > > > > ---------------------------------------- >> From: bermanrl at cfl.rr.com >> To: rwobben at hotmail.com; tutor at python.org >> Subject: RE: [Tutor] data question >> Date: Sat, 2 Oct 2010 09:02:41 -0400 >> >> >> >>> -----Original Message----- >>> From: tutor-bounces+bermanrl=cfl.rr.com at python.org [mailto:tutor- >>> bounces+bermanrl=cfl.rr.com at python.org] On Behalf Of Roelof Wobben >>> Sent: Saturday, October 02, 2010 4:35 AM >>> To: tutor at python.org >>> Subject: [Tutor] data question >>> >>> >>> >>> Hello, >> >>> Now my question is : >>> >>> In which datatype can I put this data in. >>> >> Perhaps a simple SQLlite database? >> http://zetcode.com/databases/sqlitetutorial/ >> >> Hope this helps, >> >> Robert >> >> >> >>> Regards, >>> >>> Roelof >>> >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> To unsubscribe or change subscription options: >>> http://mail.python.org/mailman/listinfo/tutor >> Oke, Oke, there I can save the input data. But I have also need a data model for team, played_games, game_points, made_points and againts_points. So I think it cannot be done without using a class for games and one for ranking. And figuring out how I can store the game-data in a text or database file. Roelof From rwobben at hotmail.com Sat Oct 2 15:15:33 2010 From: rwobben at hotmail.com (Roelof Wobben) Date: Sat, 2 Oct 2010 13:15:33 +0000 Subject: [Tutor] data question In-Reply-To: References: , Message-ID: ---------------------------------------- > To: tutor at python.org > From: alan.gauld at btinternet.com > Date: Sat, 2 Oct 2010 14:10:25 +0100 > Subject: Re: [Tutor] data question > > > "Roelof Wobben" wrote > >> As a test I would write a programm where a user can input game-data >> like home-team, away-team, home-score, away-score) and makes a >> ranking of it. > >> In which datatype can I put this data in. >> >> I thought myself of a dictonary of tuples. > > A dictionary would be good for the basic data but I assume there > are more than one of these data items? If so what do they represent? > How would they be used? > > We need a bit more information, even some sample datya might help. > > It could be a list of dictionaries or even a dictionary of > dictionaries. > > Alan G. > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Hello Alan, What I meant was this: Let's say we have a tournament of 4 teams A,B,C,D They played this games. A - B 20 - 30 C - D 23 - 67 These data must be stored so I can make a module which make a ranking like this : 1) C 1 - 2 76 - 23 2) B 1 - 2 30 - 20 I hope you know what I wan Roelof From rwobben at hotmail.com Sat Oct 2 15:40:21 2010 From: rwobben at hotmail.com (Roelof Wobben) Date: Sat, 2 Oct 2010 13:40:21 +0000 Subject: [Tutor] data question In-Reply-To: <580321.35313.qm@web86705.mail.ird.yahoo.com> References: , , <580321.35313.qm@web86705.mail.ird.yahoo.com> Message-ID: hello, Still one question. Every game is a dictonary/tuple ? Regards, Roelof ---------------------------------------- > Date: Sat, 2 Oct 2010 06:24:16 -0700 > From: alan.gauld at btinternet.com > Subject: Re: [Tutor] data question > To: rwobben at hotmail.com > > OK, So you want a list of games (possibly one per tournament > if you need to compare across tournies) where for each game > you store: homeTeam, awayTeam and scores. > > Each game could be represented as either a tuple or a dictionary > depending on how you want to extract the data: > > game['home'] or game[0] > > The list should probably be a list... :-) > > Try defining a short sample database in Python then try > extracting some values to get a feel for what suits you > best. Create the data in a module(easier to edit mistakes/changes) > Then use the>>> prompt to import the data module and play > around with it. > > That way it's easy to change from lists to tuples to dictionaries > and see what works. > > Ultimately this may even be best done using a database but > that's probably a step too far for you just now. > > Alan Gauld > Author of the Learn To Program website > http://www.alan-g.me.uk/ > > > > > ----- Original Message ---- >> From: Roelof Wobben >> To: alan.gauld at btinternet.com; tutor at python.org >> Sent: Saturday, 2 October, 2010 14:15:33 >> Subject: RE: [Tutor] data question >> >> >> >> >> ---------------------------------------- >>> To: tutor at python.org >>> From: alan.gauld at btinternet.com >>> Date: Sat, 2 Oct 2010 14:10:25 +0100 >>> Subject: Re: [Tutor] data question >>> >>> >>> "Roelof Wobben" wrote >>> >>>> As a test I would write a programm where a user can input game-data >>>> like home-team, away-team, home-score, away-score) and makes a >>>> ranking of it. >>> >>>> In which datatype can I put this data in. >>>> >>>> I thought myself of a dictonary of tuples. >>> >>> A dictionary would be good for the basic data but I assume there >>> are more than one of these data items? If so what do they represent? >>> How would they be used? >>> >>> We need a bit more information, even some sample datya might help. >>> >>> It could be a list of dictionaries or even a dictionary of >>> dictionaries. >>> >>> Alan G. >>> >>> >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> To unsubscribe or change subscription options: >>> http://mail.python.org/mailman/listinfo/tutor >> >> Hello Alan, >> >> What I meant was this: >> >> Let's say we have a tournament of 4 teams A,B,C,D >> >> They played this games. >> >> A - B 20 - 30 >> C - D 23 - 67 >> >> These data must be stored so I can make a module which make a ranking like >>this : >> >> 1) C 1 - 2 76 - 23 >> 2) B 1 - 2 30 - 20 >> >> I hope you know what I wan >> >> >> Roelof >> From bermanrl at cfl.rr.com Sat Oct 2 15:02:41 2010 From: bermanrl at cfl.rr.com (Robert Berman) Date: Sat, 2 Oct 2010 09:02:41 -0400 Subject: [Tutor] data question In-Reply-To: References: Message-ID: <007801cb6232$18d1a370$4a74ea50$@rr.com> > -----Original Message----- > From: tutor-bounces+bermanrl=cfl.rr.com at python.org [mailto:tutor- > bounces+bermanrl=cfl.rr.com at python.org] On Behalf Of Roelof Wobben > Sent: Saturday, October 02, 2010 4:35 AM > To: tutor at python.org > Subject: [Tutor] data question > > > > Hello, > Now my question is : > > In which datatype can I put this data in. > Perhaps a simple SQLlite database? http://zetcode.com/databases/sqlitetutorial/ Hope this helps, Robert > Regards, > > Roelof > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From bgailer at gmail.com Sat Oct 2 17:02:36 2010 From: bgailer at gmail.com (bob gailer) Date: Sat, 02 Oct 2010 11:02:36 -0400 Subject: [Tutor] Coin Toss Problems In-Reply-To: <432989115-1285930375-cardhu_decombobulator_blackberry.rim.net-1434736909-@bda308.bisx.produk.on.blackberry> References: <432989115-1285930375-cardhu_decombobulator_blackberry.rim.net-1434736909-@bda308.bisx.produk.on.blackberry> Message-ID: <4CA7498C.5070002@gmail.com> On 10/1/2010 6:52 AM, delegbede at dudupay.com wrote: > This is also a learner's advice, so just give it a shot. > In the first function, set HEADS to 0 and TAILS to 0. > Then if flip==0, heads=heads+1 > Return Heads. > Do same for Tails and see if it makes any sense. > Regards. Isn't Python case sensitive? -- Bob Gailer 919-636-4239 Chapel Hill NC From bgailer at gmail.com Sat Oct 2 17:17:39 2010 From: bgailer at gmail.com (bob gailer) Date: Sat, 02 Oct 2010 11:17:39 -0400 Subject: [Tutor] list comprehension, efficiency? In-Reply-To: References: Message-ID: <4CA74D13.5060902@gmail.com> [snip] I ran dis on a for loop and the equivalent comprehension. I was surprised to see almost identical code. I had assumed (and now wish for) that a comprehension would be a primitive written in C and thus much faster! -- Bob Gailer 919-636-4239 Chapel Hill NC From josep.m.fontana at gmail.com Sat Oct 2 17:56:53 2010 From: josep.m.fontana at gmail.com (Josep M. Fontana) Date: Sat, 2 Oct 2010 17:56:53 +0200 Subject: [Tutor] Using contents of a document to change file names Message-ID: Hi, This is my first posting to this list. Perhaps this has a very easy answer but before deciding to post this message I consulted a bunch of Python manuals and on-line reference documents to no avail. I would be very grateful if someone could lend me a hand with this. Here's the problem I want to solve. I have a lot of files with the following name structure: A-01-namex.txt A-02-namey.txt ... N-09-namez.txt These are different text documents that I want to process for an NLP project I'm starting. Each one of the texts belongs to a different century and it is important to be able to include the information about the century in the name of the file as well as inside the text. Then I have another text file containing information about the century each one of the texts was written. This document has the following structure: A-01, 1278 A-02, 1501 ... N-09, 1384 What I would like to do is to write a little script that would do the following: . Read each row of the text containing information about the centuries each one of the texts was written . Change the name of the file whose name starts with the code in the first column in the following way A-01-namex.txt --> A-01-namex_13-2.txt Where 13-1 means: 13th 2nd half. Obviously this information would com from the second column in the text: 1278 (the first two digits + 1 = century; if the 3rd and 4th digits > 50, then 2; if < 50 then 1) Then in the same script or in a new one, I would need to open each one of the texts and add information about the century they were written on the first line preceded by some symbol (e.g @13-2) I've found a lot of information about changing file names (so I know that I should be importing the os module), but none of the examples that were cited involved getting the information for the file changing operation from the contents of a document. As you can imagine, I'm pretty green in Python programming and I was hoping the learn by doing method would work. I need to get on with this project, though, and I'm kind of stuck. Any help you guys can give me will be very helpful. Josep M. -------------- next part -------------- An HTML attachment was scrubbed... URL: From edwardlang at optonline.net Sat Oct 2 17:40:27 2010 From: edwardlang at optonline.net (Edward Lang) Date: Sat, 02 Oct 2010 11:40:27 -0400 Subject: [Tutor] Tutor Digest, Vol 80, Issue 11 Message-ID: tutor-request at python.org wrote: >Send Tutor mailing list submissions to > tutor at python.org > >To subscribe or unsubscribe via the World Wide Web, visit > http://mail.python.org/mailman/listinfo/tutor >or, via email, send a message with subject or body 'help' to > tutor-request at python.org > >You can reach the person managing the list at > tutor-owner at python.org > >When replying, please edit your Subject line so it is more specific >than "Re: Contents of Tutor digest..." > > >Today's Topics: > > 1. Re: Connecting my users (Timo) > 2. Re: data question (Alan Gauld) > 3. Re: data question (Roelof Wobben) > 4. Re: data question (Roelof Wobben) > 5. Re: data question (Roelof Wobben) > 6. Re: data question (Robert Berman) > 7. Re: Coin Toss Problems (bob gailer) > > >---------------------------------------------------------------------- > >Message: 1 >Date: Sat, 02 Oct 2010 12:06:14 +0200 >From: Timo >To: Nitin Pawar >Cc: tutor at python.org >Subject: Re: [Tutor] Connecting my users >Message-ID: <4CA70416.2050205 at gmail.com> >Content-Type: text/plain; charset=ISO-8859-1; format=flowed > >On 01-10-10 11:25, Nitin Pawar wrote: >> have a look at this >> >> http://bytes.com/topic/python/answers/826973-peer-peer-chat-program > >Thanks, but that still uses a server. And even one that I can't control! >If it has to be with server interaction, than as little as possible is >preferred and option to put it on my own. > >Cheers, >Timo > >> >> On Fri, Oct 1, 2010 at 2:49 PM, Timo > > wrote: >> >> Hello, >> >> I have the following idea, but no clue how to implement this. >> I want my users to be able to connect to other users (with the >> same program) and chat with them and exchange files. Ideally >> without server interaction. Now I heard about peer-to-peer and >> bittorrent protocol etc. But don't know where to start or that I'm >> even at the right path. >> So if anyone can point me into the right direction of a framework >> or protocol I should use for this, it would be appreciated. >> >> Cheers, >> Timo >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> >> >> >> -- >> Nitin Pawar >> > > > >------------------------------ > >Message: 2 >Date: Sat, 2 Oct 2010 14:10:25 +0100 >From: "Alan Gauld" >To: tutor at python.org >Subject: Re: [Tutor] data question >Message-ID: >Content-Type: text/plain; format=flowed; charset="iso-8859-1"; > reply-type=original > > >"Roelof Wobben" wrote > >> As a test I would write a programm where a user can input game-data >> like home-team, away-team, home-score, away-score) and makes a >> ranking of it. > >> In which datatype can I put this data in. >> >> I thought myself of a dictonary of tuples. > >A dictionary would be good for the basic data but I assume there >are more than one of these data items? If so what do they represent? >How would they be used? > >We need a bit more information, even some sample datya might help. > >It could be a list of dictionaries or even a dictionary of >dictionaries. > >Alan G. > > > > >------------------------------ > >Message: 3 >Date: Sat, 2 Oct 2010 13:10:03 +0000 >From: Roelof Wobben >To: >Subject: Re: [Tutor] data question >Message-ID: >Content-Type: text/plain; charset="iso-8859-1" > > > > >---------------------------------------- >> From: rwobben at hotmail.com >> To: bermanrl at cfl.rr.com >> Subject: RE: [Tutor] data question >> Date: Sat, 2 Oct 2010 13:09:23 +0000 >> >> >> >> >> ---------------------------------------- >>> From: bermanrl at cfl.rr.com >>> To: rwobben at hotmail.com; tutor at python.org >>> Subject: RE: [Tutor] data question >>> Date: Sat, 2 Oct 2010 09:02:41 -0400 >>> >>> >>> >>>> -----Original Message----- >>>> From: tutor-bounces+bermanrl=cfl.rr.com at python.org [mailto:tutor- >>>> bounces+bermanrl=cfl.rr.com at python.org] On Behalf Of Roelof Wobben >>>> Sent: Saturday, October 02, 2010 4:35 AM >>>> To: tutor at python.org >>>> Subject: [Tutor] data question >>>> >>>> >>>> >>>> Hello, >>> >>>> Now my question is : >>>> >>>> In which datatype can I put this data in. >>>> >>> Perhaps a simple SQLlite database? >>> http://zetcode.com/databases/sqlitetutorial/ >>> >>> Hope this helps, >>> >>> Robert >>> >>> >>> >>>> Regards, >>>> >>>> Roelof >>>> >>>> _______________________________________________ >>>> Tutor maillist - Tutor at python.org >>>> To unsubscribe or change subscription options: >>>> http://mail.python.org/mailman/listinfo/tutor >>> > >Oke, > >Oke, there I can save the input data. >But I have also need a data model for team, played_games, game_points, made_points and againts_points. >So I think it cannot be done without using a class for games and one for ranking. >And figuring out how I can store the game-data in a text or database file. > >Roelof > >------------------------------ > >Message: 4 >Date: Sat, 2 Oct 2010 13:15:33 +0000 >From: Roelof Wobben >To: , >Subject: Re: [Tutor] data question >Message-ID: >Content-Type: text/plain; charset="iso-8859-1" > > > > >---------------------------------------- >> To: tutor at python.org >> From: alan.gauld at btinternet.com >> Date: Sat, 2 Oct 2010 14:10:25 +0100 >> Subject: Re: [Tutor] data question >> >> >> "Roelof Wobben" wrote >> >>> As a test I would write a programm where a user can input game-data >>> like home-team, away-team, home-score, away-score) and makes a >>> ranking of it. >> >>> In which datatype can I put this data in. >>> >>> I thought myself of a dictonary of tuples. >> >> A dictionary would be good for the basic data but I assume there >> are more than one of these data items? If so what do they represent? >> How would they be used? >> >> We need a bit more information, even some sample datya might help. >> >> It could be a list of dictionaries or even a dictionary of >> dictionaries. >> >> Alan G. >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > >Hello Alan, > >What I meant was this: > >Let's say we have a tournament of 4 teams A,B,C,D > >They played this games. > >A - B 20 - 30 >C - D 23 - 67 > >These data must be stored so I can make a module which make a ranking like this : > >1) C 1 - 2 76 - 23 >2) B 1 - 2 30 - 20 > >I hope you know what I wan > > >Roelof > > >------------------------------ > >Message: 5 >Date: Sat, 2 Oct 2010 13:40:21 +0000 >From: Roelof Wobben >To: , >Subject: Re: [Tutor] data question >Message-ID: >Content-Type: text/plain; charset="iso-8859-1" > > >hello, > >Still one question. > >Every game is a dictonary/tuple ? > >Regards, > >Roelof > >---------------------------------------- >> Date: Sat, 2 Oct 2010 06:24:16 -0700 >> From: alan.gauld at btinternet.com >> Subject: Re: [Tutor] data question >> To: rwobben at hotmail.com >> >> OK, So you want a list of games (possibly one per tournament >> if you need to compare across tournies) where for each game >> you store: homeTeam, awayTeam and scores. >> >> Each game could be represented as either a tuple or a dictionary >> depending on how you want to extract the data: >> >> game['home'] or game[0] >> >> The list should probably be a list... :-) >> >> Try defining a short sample database in Python then try >> extracting some values to get a feel for what suits you >> best. Create the data in a module(easier to edit mistakes/changes) >> Then use the>>> prompt to import the data module and play >> around with it. >> >> That way it's easy to change from lists to tuples to dictionaries >> and see what works. >> >> Ultimately this may even be best done using a database but >> that's probably a step too far for you just now. >> >> Alan Gauld >> Author of the Learn To Program website >> http://www.alan-g.me.uk/ >> >> >> >> >> ----- Original Message ---- >>> From: Roelof Wobben >>> To: alan.gauld at btinternet.com; tutor at python.org >>> Sent: Saturday, 2 October, 2010 14:15:33 >>> Subject: RE: [Tutor] data question >>> >>> >>> >>> >>> ---------------------------------------- >>>> To: tutor at python.org >>>> From: alan.gauld at btinternet.com >>>> Date: Sat, 2 Oct 2010 14:10:25 +0100 >>>> Subject: Re: [Tutor] data question >>>> >>>> >>>> "Roelof Wobben" wrote >>>> >>>>> As a test I would write a programm where a user can input game-data >>>>> like home-team, away-team, home-score, away-score) and makes a >>>>> ranking of it. >>>> >>>>> In which datatype can I put this data in. >>>>> >>>>> I thought myself of a dictonary of tuples. >>>> >>>> A dictionary would be good for the basic data but I assume there >>>> are more than one of these data items? If so what do they represent? >>>> How would they be used? >>>> >>>> We need a bit more information, even some sample datya might help. >>>> >>>> It could be a list of dictionaries or even a dictionary of >>>> dictionaries. >>>> >>>> Alan G. >>>> >>>> >>>> _______________________________________________ >>>> Tutor maillist - Tutor at python.org >>>> To unsubscribe or change subscription options: >>>> http://mail.python.org/mailman/listinfo/tutor >>> >>> Hello Alan, >>> >>> What I meant was this: >>> >>> Let's say we have a tournament of 4 teams A,B,C,D >>> >>> They played this games. >>> >>> A - B 20 - 30 >>> C - D 23 - 67 >>> >>> These data must be stored so I can make a module which make a ranking like >>>this : >>> >>> 1) C 1 - 2 76 - 23 >>> 2) B 1 - 2 30 - 20 >>> >>> I hope you know what I wan >>> >>> >>> Roelof >>> > >------------------------------ > >Message: 6 >Date: Sat, 2 Oct 2010 09:02:41 -0400 >From: "Robert Berman" >To: "'Roelof Wobben'" , >Subject: Re: [Tutor] data question >Message-ID: <007801cb6232$18d1a370$4a74ea50$@rr.com> >Content-Type: text/plain; charset="us-ascii" > > > >> -----Original Message----- >> From: tutor-bounces+bermanrl=cfl.rr.com at python.org [mailto:tutor- >> bounces+bermanrl=cfl.rr.com at python.org] On Behalf Of Roelof Wobben >> Sent: Saturday, October 02, 2010 4:35 AM >> To: tutor at python.org >> Subject: [Tutor] data question >> >> >> >> Hello, > >> Now my question is : >> >> In which datatype can I put this data in. >> >Perhaps a simple SQLlite database? >http://zetcode.com/databases/sqlitetutorial/ > >Hope this helps, > >Robert > > > >> Regards, >> >> Roelof >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > > >------------------------------ > >Message: 7 >Date: Sat, 02 Oct 2010 11:02:36 -0400 >From: bob gailer >To: tutor at python.org >Subject: Re: [Tutor] Coin Toss Problems >Message-ID: <4CA7498C.5070002 at gmail.com> >Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > On 10/1/2010 6:52 AM, delegbede at dudupay.com wrote: >> This is also a learner's advice, so just give it a shot. >> In the first function, set HEADS to 0 and TAILS to 0. >> Then if flip==0, heads=heads+1 >> Return Heads. >> Do same for Tails and see if it makes any sense. >> Regards. > >Isn't Python case sensitive? > > >-- >Bob Gailer >919-636-4239 >Chapel Hill NC > > > >------------------------------ > >_______________________________________________ >Tutor maillist - Tutor at python.org >http://mail.python.org/mailman/listinfo/tutor > > >End of Tutor Digest, Vol 80, Issue 11 >************************************* From edwardlang at optonline.net Sat Oct 2 17:40:37 2010 From: edwardlang at optonline.net (Edward Lang) Date: Sat, 02 Oct 2010 11:40:37 -0400 Subject: [Tutor] Tutor Digest, Vol 80, Issue 11 Message-ID: <9vhfhn3u850yjg3rvrclhb28.1286034037492@email.android.com> tutor-request at python.org wrote: >Send Tutor mailing list submissions to > tutor at python.org > >To subscribe or unsubscribe via the World Wide Web, visit > http://mail.python.org/mailman/listinfo/tutor >or, via email, send a message with subject or body 'help' to > tutor-request at python.org > >You can reach the person managing the list at > tutor-owner at python.org > >When replying, please edit your Subject line so it is more specific >than "Re: Contents of Tutor digest..." > > >Today's Topics: > > 1. Re: Connecting my users (Timo) > 2. Re: data question (Alan Gauld) > 3. Re: data question (Roelof Wobben) > 4. Re: data question (Roelof Wobben) > 5. Re: data question (Roelof Wobben) > 6. Re: data question (Robert Berman) > 7. Re: Coin Toss Problems (bob gailer) > > >---------------------------------------------------------------------- > >Message: 1 >Date: Sat, 02 Oct 2010 12:06:14 +0200 >From: Timo >To: Nitin Pawar >Cc: tutor at python.org >Subject: Re: [Tutor] Connecting my users >Message-ID: <4CA70416.2050205 at gmail.com> >Content-Type: text/plain; charset=ISO-8859-1; format=flowed > >On 01-10-10 11:25, Nitin Pawar wrote: >> have a look at this >> >> http://bytes.com/topic/python/answers/826973-peer-peer-chat-program > >Thanks, but that still uses a server. And even one that I can't control! >If it has to be with server interaction, than as little as possible is >preferred and option to put it on my own. > >Cheers, >Timo > >> >> On Fri, Oct 1, 2010 at 2:49 PM, Timo > > wrote: >> >> Hello, >> >> I have the following idea, but no clue how to implement this. >> I want my users to be able to connect to other users (with the >> same program) and chat with them and exchange files. Ideally >> without server interaction. Now I heard about peer-to-peer and >> bittorrent protocol etc. But don't know where to start or that I'm >> even at the right path. >> So if anyone can point me into the right direction of a framework >> or protocol I should use for this, it would be appreciated. >> >> Cheers, >> Timo >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> >> >> >> -- >> Nitin Pawar >> > > > >------------------------------ > >Message: 2 >Date: Sat, 2 Oct 2010 14:10:25 +0100 >From: "Alan Gauld" >To: tutor at python.org >Subject: Re: [Tutor] data question >Message-ID: >Content-Type: text/plain; format=flowed; charset="iso-8859-1"; > reply-type=original > > >"Roelof Wobben" wrote > >> As a test I would write a programm where a user can input game-data >> like home-team, away-team, home-score, away-score) and makes a >> ranking of it. > >> In which datatype can I put this data in. >> >> I thought myself of a dictonary of tuples. > >A dictionary would be good for the basic data but I assume there >are more than one of these data items? If so what do they represent? >How would they be used? > >We need a bit more information, even some sample datya might help. > >It could be a list of dictionaries or even a dictionary of >dictionaries. > >Alan G. > > > > >------------------------------ > >Message: 3 >Date: Sat, 2 Oct 2010 13:10:03 +0000 >From: Roelof Wobben >To: >Subject: Re: [Tutor] data question >Message-ID: >Content-Type: text/plain; charset="iso-8859-1" > > > > >---------------------------------------- >> From: rwobben at hotmail.com >> To: bermanrl at cfl.rr.com >> Subject: RE: [Tutor] data question >> Date: Sat, 2 Oct 2010 13:09:23 +0000 >> >> >> >> >> ---------------------------------------- >>> From: bermanrl at cfl.rr.com >>> To: rwobben at hotmail.com; tutor at python.org >>> Subject: RE: [Tutor] data question >>> Date: Sat, 2 Oct 2010 09:02:41 -0400 >>> >>> >>> >>>> -----Original Message----- >>>> From: tutor-bounces+bermanrl=cfl.rr.com at python.org [mailto:tutor- >>>> bounces+bermanrl=cfl.rr.com at python.org] On Behalf Of Roelof Wobben >>>> Sent: Saturday, October 02, 2010 4:35 AM >>>> To: tutor at python.org >>>> Subject: [Tutor] data question >>>> >>>> >>>> >>>> Hello, >>> >>>> Now my question is : >>>> >>>> In which datatype can I put this data in. >>>> >>> Perhaps a simple SQLlite database? >>> http://zetcode.com/databases/sqlitetutorial/ >>> >>> Hope this helps, >>> >>> Robert >>> >>> >>> >>>> Regards, >>>> >>>> Roelof >>>> >>>> _______________________________________________ >>>> Tutor maillist - Tutor at python.org >>>> To unsubscribe or change subscription options: >>>> http://mail.python.org/mailman/listinfo/tutor >>> > >Oke, > >Oke, there I can save the input data. >But I have also need a data model for team, played_games, game_points, made_points and againts_points. >So I think it cannot be done without using a class for games and one for ranking. >And figuring out how I can store the game-data in a text or database file. > >Roelof > >------------------------------ > >Message: 4 >Date: Sat, 2 Oct 2010 13:15:33 +0000 >From: Roelof Wobben >To: , >Subject: Re: [Tutor] data question >Message-ID: >Content-Type: text/plain; charset="iso-8859-1" > > > > >---------------------------------------- >> To: tutor at python.org >> From: alan.gauld at btinternet.com >> Date: Sat, 2 Oct 2010 14:10:25 +0100 >> Subject: Re: [Tutor] data question >> >> >> "Roelof Wobben" wrote >> >>> As a test I would write a programm where a user can input game-data >>> like home-team, away-team, home-score, away-score) and makes a >>> ranking of it. >> >>> In which datatype can I put this data in. >>> >>> I thought myself of a dictonary of tuples. >> >> A dictionary would be good for the basic data but I assume there >> are more than one of these data items? If so what do they represent? >> How would they be used? >> >> We need a bit more information, even some sample datya might help. >> >> It could be a list of dictionaries or even a dictionary of >> dictionaries. >> >> Alan G. >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > >Hello Alan, > >What I meant was this: > >Let's say we have a tournament of 4 teams A,B,C,D > >They played this games. > >A - B 20 - 30 >C - D 23 - 67 > >These data must be stored so I can make a module which make a ranking like this : > >1) C 1 - 2 76 - 23 >2) B 1 - 2 30 - 20 > >I hope you know what I wan > > >Roelof > > >------------------------------ > >Message: 5 >Date: Sat, 2 Oct 2010 13:40:21 +0000 >From: Roelof Wobben >To: , >Subject: Re: [Tutor] data question >Message-ID: >Content-Type: text/plain; charset="iso-8859-1" > > >hello, > >Still one question. > >Every game is a dictonary/tuple ? > >Regards, > >Roelof > >---------------------------------------- >> Date: Sat, 2 Oct 2010 06:24:16 -0700 >> From: alan.gauld at btinternet.com >> Subject: Re: [Tutor] data question >> To: rwobben at hotmail.com >> >> OK, So you want a list of games (possibly one per tournament >> if you need to compare across tournies) where for each game >> you store: homeTeam, awayTeam and scores. >> >> Each game could be represented as either a tuple or a dictionary >> depending on how you want to extract the data: >> >> game['home'] or game[0] >> >> The list should probably be a list... :-) >> >> Try defining a short sample database in Python then try >> extracting some values to get a feel for what suits you >> best. Create the data in a module(easier to edit mistakes/changes) >> Then use the>>> prompt to import the data module and play >> around with it. >> >> That way it's easy to change from lists to tuples to dictionaries >> and see what works. >> >> Ultimately this may even be best done using a database but >> that's probably a step too far for you just now. >> >> Alan Gauld >> Author of the Learn To Program website >> http://www.alan-g.me.uk/ >> >> >> >> >> ----- Original Message ---- >>> From: Roelof Wobben >>> To: alan.gauld at btinternet.com; tutor at python.org >>> Sent: Saturday, 2 October, 2010 14:15:33 >>> Subject: RE: [Tutor] data question >>> >>> >>> >>> >>> ---------------------------------------- >>>> To: tutor at python.org >>>> From: alan.gauld at btinternet.com >>>> Date: Sat, 2 Oct 2010 14:10:25 +0100 >>>> Subject: Re: [Tutor] data question >>>> >>>> >>>> "Roelof Wobben" wrote >>>> >>>>> As a test I would write a programm where a user can input game-data >>>>> like home-team, away-team, home-score, away-score) and makes a >>>>> ranking of it. >>>> >>>>> In which datatype can I put this data in. >>>>> >>>>> I thought myself of a dictonary of tuples. >>>> >>>> A dictionary would be good for the basic data but I assume there >>>> are more than one of these data items? If so what do they represent? >>>> How would they be used? >>>> >>>> We need a bit more information, even some sample datya might help. >>>> >>>> It could be a list of dictionaries or even a dictionary of >>>> dictionaries. >>>> >>>> Alan G. >>>> >>>> >>>> _______________________________________________ >>>> Tutor maillist - Tutor at python.org >>>> To unsubscribe or change subscription options: >>>> http://mail.python.org/mailman/listinfo/tutor >>> >>> Hello Alan, >>> >>> What I meant was this: >>> >>> Let's say we have a tournament of 4 teams A,B,C,D >>> >>> They played this games. >>> >>> A - B 20 - 30 >>> C - D 23 - 67 >>> >>> These data must be stored so I can make a module which make a ranking like >>>this : >>> >>> 1) C 1 - 2 76 - 23 >>> 2) B 1 - 2 30 - 20 >>> >>> I hope you know what I wan >>> >>> >>> Roelof >>> > >------------------------------ > >Message: 6 >Date: Sat, 2 Oct 2010 09:02:41 -0400 >From: "Robert Berman" >To: "'Roelof Wobben'" , >Subject: Re: [Tutor] data question >Message-ID: <007801cb6232$18d1a370$4a74ea50$@rr.com> >Content-Type: text/plain; charset="us-ascii" > > > >> -----Original Message----- >> From: tutor-bounces+bermanrl=cfl.rr.com at python.org [mailto:tutor- >> bounces+bermanrl=cfl.rr.com at python.org] On Behalf Of Roelof Wobben >> Sent: Saturday, October 02, 2010 4:35 AM >> To: tutor at python.org >> Subject: [Tutor] data question >> >> >> >> Hello, > >> Now my question is : >> >> In which datatype can I put this data in. >> >Perhaps a simple SQLlite database? >http://zetcode.com/databases/sqlitetutorial/ > >Hope this helps, > >Robert > > > >> Regards, >> >> Roelof >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > > >------------------------------ > >Message: 7 >Date: Sat, 02 Oct 2010 11:02:36 -0400 >From: bob gailer >To: tutor at python.org >Subject: Re: [Tutor] Coin Toss Problems >Message-ID: <4CA7498C.5070002 at gmail.com> >Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > On 10/1/2010 6:52 AM, delegbede at dudupay.com wrote: >> This is also a learner's advice, so just give it a shot. >> In the first function, set HEADS to 0 and TAILS to 0. >> Then if flip==0, heads=heads+1 >> Return Heads. >> Do same for Tails and see if it makes any sense. >> Regards. > >Isn't Python case sensitive? > > >-- >Bob Gailer >919-636-4239 >Chapel Hill NC > > > >------------------------------ > >_______________________________________________ >Tutor maillist - Tutor at python.org >http://mail.python.org/mailman/listinfo/tutor > > >End of Tutor Digest, Vol 80, Issue 11 >************************************* From edwardlang at optonline.net Sat Oct 2 17:44:37 2010 From: edwardlang at optonline.net (Edward Lang) Date: Sat, 02 Oct 2010 11:44:37 -0400 Subject: [Tutor] Tutor Digest, Vol 80, Issue 11 Message-ID: tutor-request at python.org wrote: >Send Tutor mailing list submissions to > tutor at python.org > >To subscribe or unsubscribe via the World Wide Web, visit > http://mail.python.org/mailman/listinfo/tutor >or, via email, send a message with subject or body 'help' to > tutor-request at python.org > >You can reach the person managing the list at > tutor-owner at python.org > >When replying, please edit your Subject line so it is more specific >than "Re: Contents of Tutor digest..." > > >Today's Topics: > > 1. Re: Connecting my users (Timo) > 2. Re: data question (Alan Gauld) > 3. Re: data question (Roelof Wobben) > 4. Re: data question (Roelof Wobben) > 5. Re: data question (Roelof Wobben) > 6. Re: data question (Robert Berman) > 7. Re: Coin Toss Problems (bob gailer) > > >---------------------------------------------------------------------- > >Message: 1 >Date: Sat, 02 Oct 2010 12:06:14 +0200 >From: Timo >To: Nitin Pawar >Cc: tutor at python.org >Subject: Re: [Tutor] Connecting my users >Message-ID: <4CA70416.2050205 at gmail.com> >Content-Type: text/plain; charset=ISO-8859-1; format=flowed > >On 01-10-10 11:25, Nitin Pawar wrote: >> have a look at this >> >> http://bytes.com/topic/python/answers/826973-peer-peer-chat-program > >Thanks, but that still uses a server. And even one that I can't control! >If it has to be with server interaction, than as little as possible is >preferred and option to put it on my own. > >Cheers, >Timo > >> >> On Fri, Oct 1, 2010 at 2:49 PM, Timo > > wrote: >> >> Hello, >> >> I have the following idea, but no clue how to implement this. >> I want my users to be able to connect to other users (with the >> same program) and chat with them and exchange files. Ideally >> without server interaction. Now I heard about peer-to-peer and >> bittorrent protocol etc. But don't know where to start or that I'm >> even at the right path. >> So if anyone can point me into the right direction of a framework >> or protocol I should use for this, it would be appreciated. >> >> Cheers, >> Timo >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> >> >> >> -- >> Nitin Pawar >> > > > >------------------------------ > >Message: 2 >Date: Sat, 2 Oct 2010 14:10:25 +0100 >From: "Alan Gauld" >To: tutor at python.org >Subject: Re: [Tutor] data question >Message-ID: >Content-Type: text/plain; format=flowed; charset="iso-8859-1"; > reply-type=original > > >"Roelof Wobben" wrote > >> As a test I would write a programm where a user can input game-data >> like home-team, away-team, home-score, away-score) and makes a >> ranking of it. > >> In which datatype can I put this data in. >> >> I thought myself of a dictonary of tuples. > >A dictionary would be good for the basic data but I assume there >are more than one of these data items? If so what do they represent? >How would they be used? > >We need a bit more information, even some sample datya might help. > >It could be a list of dictionaries or even a dictionary of >dictionaries. > >Alan G. > > > > >------------------------------ > >Message: 3 >Date: Sat, 2 Oct 2010 13:10:03 +0000 >From: Roelof Wobben >To: >Subject: Re: [Tutor] data question >Message-ID: >Content-Type: text/plain; charset="iso-8859-1" > > > > >---------------------------------------- >> From: rwobben at hotmail.com >> To: bermanrl at cfl.rr.com >> Subject: RE: [Tutor] data question >> Date: Sat, 2 Oct 2010 13:09:23 +0000 >> >> >> >> >> ---------------------------------------- >>> From: bermanrl at cfl.rr.com >>> To: rwobben at hotmail.com; tutor at python.org >>> Subject: RE: [Tutor] data question >>> Date: Sat, 2 Oct 2010 09:02:41 -0400 >>> >>> >>> >>>> -----Original Message----- >>>> From: tutor-bounces+bermanrl=cfl.rr.com at python.org [mailto:tutor- >>>> bounces+bermanrl=cfl.rr.com at python.org] On Behalf Of Roelof Wobben >>>> Sent: Saturday, October 02, 2010 4:35 AM >>>> To: tutor at python.org >>>> Subject: [Tutor] data question >>>> >>>> >>>> >>>> Hello, >>> >>>> Now my question is : >>>> >>>> In which datatype can I put this data in. >>>> >>> Perhaps a simple SQLlite database? >>> http://zetcode.com/databases/sqlitetutorial/ >>> >>> Hope this helps, >>> >>> Robert >>> >>> >>> >>>> Regards, >>>> >>>> Roelof >>>> >>>> _______________________________________________ >>>> Tutor maillist - Tutor at python.org >>>> To unsubscribe or change subscription options: >>>> http://mail.python.org/mailman/listinfo/tutor >>> > >Oke, > >Oke, there I can save the input data. >But I have also need a data model for team, played_games, game_points, made_points and againts_points. >So I think it cannot be done without using a class for games and one for ranking. >And figuring out how I can store the game-data in a text or database file. > >Roelof > >------------------------------ > >Message: 4 >Date: Sat, 2 Oct 2010 13:15:33 +0000 >From: Roelof Wobben >To: , >Subject: Re: [Tutor] data question >Message-ID: >Content-Type: text/plain; charset="iso-8859-1" > > > > >---------------------------------------- >> To: tutor at python.org >> From: alan.gauld at btinternet.com >> Date: Sat, 2 Oct 2010 14:10:25 +0100 >> Subject: Re: [Tutor] data question >> >> >> "Roelof Wobben" wrote >> >>> As a test I would write a programm where a user can input game-data >>> like home-team, away-team, home-score, away-score) and makes a >>> ranking of it. >> >>> In which datatype can I put this data in. >>> >>> I thought myself of a dictonary of tuples. >> >> A dictionary would be good for the basic data but I assume there >> are more than one of these data items? If so what do they represent? >> How would they be used? >> >> We need a bit more information, even some sample datya might help. >> >> It could be a list of dictionaries or even a dictionary of >> dictionaries. >> >> Alan G. >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > >Hello Alan, > >What I meant was this: > >Let's say we have a tournament of 4 teams A,B,C,D > >They played this games. > >A - B 20 - 30 >C - D 23 - 67 > >These data must be stored so I can make a module which make a ranking like this : > >1) C 1 - 2 76 - 23 >2) B 1 - 2 30 - 20 > >I hope you know what I wan > > >Roelof > > >------------------------------ > >Message: 5 >Date: Sat, 2 Oct 2010 13:40:21 +0000 >From: Roelof Wobben >To: , >Subject: Re: [Tutor] data question >Message-ID: >Content-Type: text/plain; charset="iso-8859-1" > > >hello, > >Still one question. > >Every game is a dictonary/tuple ? > >Regards, > >Roelof > >---------------------------------------- >> Date: Sat, 2 Oct 2010 06:24:16 -0700 >> From: alan.gauld at btinternet.com >> Subject: Re: [Tutor] data question >> To: rwobben at hotmail.com >> >> OK, So you want a list of games (possibly one per tournament >> if you need to compare across tournies) where for each game >> you store: homeTeam, awayTeam and scores. >> >> Each game could be represented as either a tuple or a dictionary >> depending on how you want to extract the data: >> >> game['home'] or game[0] >> >> The list should probably be a list... :-) >> >> Try defining a short sample database in Python then try >> extracting some values to get a feel for what suits you >> best. Create the data in a module(easier to edit mistakes/changes) >> Then use the>>> prompt to import the data module and play >> around with it. >> >> That way it's easy to change from lists to tuples to dictionaries >> and see what works. >> >> Ultimately this may even be best done using a database but >> that's probably a step too far for you just now. >> >> Alan Gauld >> Author of the Learn To Program website >> http://www.alan-g.me.uk/ >> >> >> >> >> ----- Original Message ---- >>> From: Roelof Wobben >>> To: alan.gauld at btinternet.com; tutor at python.org >>> Sent: Saturday, 2 October, 2010 14:15:33 >>> Subject: RE: [Tutor] data question >>> >>> >>> >>> >>> ---------------------------------------- >>>> To: tutor at python.org >>>> From: alan.gauld at btinternet.com >>>> Date: Sat, 2 Oct 2010 14:10:25 +0100 >>>> Subject: Re: [Tutor] data question >>>> >>>> >>>> "Roelof Wobben" wrote >>>> >>>>> As a test I would write a programm where a user can input game-data >>>>> like home-team, away-team, home-score, away-score) and makes a >>>>> ranking of it. >>>> >>>>> In which datatype can I put this data in. >>>>> >>>>> I thought myself of a dictonary of tuples. >>>> >>>> A dictionary would be good for the basic data but I assume there >>>> are more than one of these data items? If so what do they represent? >>>> How would they be used? >>>> >>>> We need a bit more information, even some sample datya might help. >>>> >>>> It could be a list of dictionaries or even a dictionary of >>>> dictionaries. >>>> >>>> Alan G. >>>> >>>> >>>> _______________________________________________ >>>> Tutor maillist - Tutor at python.org >>>> To unsubscribe or change subscription options: >>>> http://mail.python.org/mailman/listinfo/tutor >>> >>> Hello Alan, >>> >>> What I meant was this: >>> >>> Let's say we have a tournament of 4 teams A,B,C,D >>> >>> They played this games. >>> >>> A - B 20 - 30 >>> C - D 23 - 67 >>> >>> These data must be stored so I can make a module which make a ranking like >>>this : >>> >>> 1) C 1 - 2 76 - 23 >>> 2) B 1 - 2 30 - 20 >>> >>> I hope you know what I wan >>> >>> >>> Roelof >>> > >------------------------------ > >Message: 6 >Date: Sat, 2 Oct 2010 09:02:41 -0400 >From: "Robert Berman" >To: "'Roelof Wobben'" , >Subject: Re: [Tutor] data question >Message-ID: <007801cb6232$18d1a370$4a74ea50$@rr.com> >Content-Type: text/plain; charset="us-ascii" > > > >> -----Original Message----- >> From: tutor-bounces+bermanrl=cfl.rr.com at python.org [mailto:tutor- >> bounces+bermanrl=cfl.rr.com at python.org] On Behalf Of Roelof Wobben >> Sent: Saturday, October 02, 2010 4:35 AM >> To: tutor at python.org >> Subject: [Tutor] data question >> >> >> >> Hello, > >> Now my question is : >> >> In which datatype can I put this data in. >> >Perhaps a simple SQLlite database? >http://zetcode.com/databases/sqlitetutorial/ > >Hope this helps, > >Robert > > > >> Regards, >> >> Roelof >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > > >------------------------------ > >Message: 7 >Date: Sat, 02 Oct 2010 11:02:36 -0400 >From: bob gailer >To: tutor at python.org >Subject: Re: [Tutor] Coin Toss Problems >Message-ID: <4CA7498C.5070002 at gmail.com> >Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > On 10/1/2010 6:52 AM, delegbede at dudupay.com wrote: >> This is also a learner's advice, so just give it a shot. >> In the first function, set HEADS to 0 and TAILS to 0. >> Then if flip==0, heads=heads+1 >> Return Heads. >> Do same for Tails and see if it makes any sense. >> Regards. > >Isn't Python case sensitive? > > >-- >Bob Gailer >919-636-4239 >Chapel Hill NC > > > >------------------------------ > >_______________________________________________ >Tutor maillist - Tutor at python.org >http://mail.python.org/mailman/listinfo/tutor > > >End of Tutor Digest, Vol 80, Issue 11 >************************************* From roberto03 at gmail.com Sat Oct 2 18:43:26 2010 From: roberto03 at gmail.com (roberto) Date: Sat, 2 Oct 2010 18:43:26 +0200 Subject: [Tutor] function error In-Reply-To: <102031.75642.qm@web86707.mail.ird.yahoo.com> References: <38D9DADF-E1A8-4BDC-8FF8-5F48C5510B23@gmail.com> <102031.75642.qm@web86707.mail.ird.yahoo.com> Message-ID: On Thu, Sep 30, 2010 at 1:45 PM, ALAN GAULD wrote: > Copy the code into a text file with a name ending in .py - lets call it > myfile.py for now > (if you have not already done so) > > From a bash prompt type > > $ python myfile.py > > Then cut n paste any error messages into an email to the list well, actually i corrected the code so that the function outOfBounds() is directly called (as you can see hereafter); but if you have time to run the code you'll see the same strange behavior as me (maybe): when the arrow hits anyone of the window borders, it gets stuck back and forth indefinitely and never starts the path again thank you in advance ############################################################################## import turtle, random def checkForward(distance): old_position = turtle.position() turtle._pen.up() # no show/hide turtle methods in my turtle module ! turtle.forward(distance) forward_failed = outOfBounds() turtle.setx(old_position[0]); turtle.sety(old_position[1]) turtle._pen.down() # no show/hide turtle methods in my turtle module ! if outOfBounds() == 'false': turtle.forward(distance) def stuck(): return forward_failed def outOfBounds(): if (abs(turtle.position()[0]) > turtle.window_height()/2) or (abs(turtle.position()[1]) > turtle.window_width()/2): return "true" else: return "false" def randomMove2(d1, d2, a1, a2): while 1: turtle.left(random.uniform(a1,a2)) checkForward(random.uniform(d1,d2)) if outOfBounds() == 'true': turtle.right(180) ############################################################################# -- roberto From rwobben at hotmail.com Sat Oct 2 18:55:42 2010 From: rwobben at hotmail.com (Roelof Wobben) Date: Sat, 2 Oct 2010 16:55:42 +0000 Subject: [Tutor] data question In-Reply-To: References: , , , <580321.35313.qm@web86705.mail.ird.yahoo.com>, Message-ID: Hello Alan, I think this is what can work : http://www.daniweb.com/code/snippet216750.html I will try it. Roelof ---------------------------------------- > From: rwobben at hotmail.com > To: alan.gauld at btinternet.com; tutor at python.org > Subject: RE: [Tutor] data question > Date: Sat, 2 Oct 2010 13:40:21 +0000 > > > hello, > > Still one question. > > Every game is a dictonary/tuple ? > > Regards, > > Roelof > > ---------------------------------------- >> Date: Sat, 2 Oct 2010 06:24:16 -0700 >> From: alan.gauld at btinternet.com >> Subject: Re: [Tutor] data question >> To: rwobben at hotmail.com >> >> OK, So you want a list of games (possibly one per tournament >> if you need to compare across tournies) where for each game >> you store: homeTeam, awayTeam and scores. >> >> Each game could be represented as either a tuple or a dictionary >> depending on how you want to extract the data: >> >> game['home'] or game[0] >> >> The list should probably be a list... :-) >> >> Try defining a short sample database in Python then try >> extracting some values to get a feel for what suits you >> best. Create the data in a module(easier to edit mistakes/changes) >> Then use the>>> prompt to import the data module and play >> around with it. >> >> That way it's easy to change from lists to tuples to dictionaries >> and see what works. >> >> Ultimately this may even be best done using a database but >> that's probably a step too far for you just now. >> >> Alan Gauld >> Author of the Learn To Program website >> http://www.alan-g.me.uk/ >> >> >> >> >> ----- Original Message ---- >>> From: Roelof Wobben >>> To: alan.gauld at btinternet.com; tutor at python.org >>> Sent: Saturday, 2 October, 2010 14:15:33 >>> Subject: RE: [Tutor] data question >>> >>> >>> >>> >>> ---------------------------------------- >>>> To: tutor at python.org >>>> From: alan.gauld at btinternet.com >>>> Date: Sat, 2 Oct 2010 14:10:25 +0100 >>>> Subject: Re: [Tutor] data question >>>> >>>> >>>> "Roelof Wobben" wrote >>>> >>>>> As a test I would write a programm where a user can input game-data >>>>> like home-team, away-team, home-score, away-score) and makes a >>>>> ranking of it. >>>> >>>>> In which datatype can I put this data in. >>>>> >>>>> I thought myself of a dictonary of tuples. >>>> >>>> A dictionary would be good for the basic data but I assume there >>>> are more than one of these data items? If so what do they represent? >>>> How would they be used? >>>> >>>> We need a bit more information, even some sample datya might help. >>>> >>>> It could be a list of dictionaries or even a dictionary of >>>> dictionaries. >>>> >>>> Alan G. >>>> >>>> >>>> _______________________________________________ >>>> Tutor maillist - Tutor at python.org >>>> To unsubscribe or change subscription options: >>>> http://mail.python.org/mailman/listinfo/tutor >>> >>> Hello Alan, >>> >>> What I meant was this: >>> >>> Let's say we have a tournament of 4 teams A,B,C,D >>> >>> They played this games. >>> >>> A - B 20 - 30 >>> C - D 23 - 67 >>> >>> These data must be stored so I can make a module which make a ranking like >>>this : >>> >>> 1) C 1 - 2 76 - 23 >>> 2) B 1 - 2 30 - 20 >>> >>> I hope you know what I wan >>> >>> >>> Roelof >>> From gregbair at gmail.com Sat Oct 2 19:04:53 2010 From: gregbair at gmail.com (Greg) Date: Sat, 2 Oct 2010 13:04:53 -0400 Subject: [Tutor] Using contents of a document to change file names In-Reply-To: References: Message-ID: On Sat, Oct 2, 2010 at 11:56 AM, Josep M. Fontana wrote: > Then I have another text file containing information about the century each > one of the texts was written. This document has the following structure: > > A-01, 1278 > A-02, 1501 > ... > N-09, 1384 > > To process this, I'd check out the CSV (Comma Separated Values) module. What I would like to do is to write a little script that would do the > following: > > . Read each row of the text containing information about the centuries each > one of the texts was written > . Change the name of the file whose name starts with the code in the first > column in the following way > > A-01-namex.txt --> A-01-namex_13-2.txt > > Where 13-1 means: 13th 2nd half. Obviously this information would com > from the second column in the text: 1278 (the first two digits + 1 = > century; if the 3rd and 4th digits > 50, then 2; if < 50 then 1) > > Then in the same script or in a new one, I would need to open each one of > the texts and add information about the century they were written on the > first line preceded by some symbol (e.g @13-2) > > I've found a lot of information about changing file names (so I know that I > should be importing the os module), but none of the examples that were cited > involved getting the information for the file changing operation from the > contents of a document. If it were me, I'd build a string using the information you want, then add ".txt" or whatever to the end of it, then use the os module to change the filenames. -- Greg Bair gregbair at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Sat Oct 2 19:16:13 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sat, 2 Oct 2010 13:16:13 -0400 Subject: [Tutor] function error In-Reply-To: References: <38D9DADF-E1A8-4BDC-8FF8-5F48C5510B23@gmail.com> <102031.75642.qm@web86707.mail.ird.yahoo.com> Message-ID: On Sat, Oct 2, 2010 at 12:43 PM, roberto wrote: > On Thu, Sep 30, 2010 at 1:45 PM, ALAN GAULD wrote: >> Copy the code into a text file with a name ending in .py - lets call it >> myfile.py for now >> (if you have not already done so) >> >> From a bash prompt type >> >> $ python myfile.py >> >> Then cut n paste any error messages into an email to the list > > well, actually i corrected the code so that the function outOfBounds() > is directly called (as you can see hereafter); > but if you have time to run the code you'll see the same strange > behavior as me (maybe): > when the arrow hits anyone of the window borders, it gets stuck back > and forth indefinitely and never starts the path again > > thank you in advance > ############################################################################## > import turtle, random > > def checkForward(distance): > ? ? ? ?old_position = turtle.position() > ? ? ? ?turtle._pen.up() > ? ? ? ?# no show/hide turtle methods in my turtle module ! > ? ? ? ?turtle.forward(distance) > ? ? ? ?forward_failed = outOfBounds() > ? ? ? ?turtle.setx(old_position[0]); turtle.sety(old_position[1]) > ? ? ? ?turtle._pen.down() > ? ? ? ?# no show/hide turtle methods in my turtle module ! > ? ? ? ?if outOfBounds() == 'false': > ? ? ? ? ? ? ? ?turtle.forward(distance) > > def stuck(): > ? ? ? ?return forward_failed > > def outOfBounds(): > ? ? ? ?if (abs(turtle.position()[0]) > turtle.window_height()/2) or > (abs(turtle.position()[1]) > turtle.window_width()/2): > ? ? ? ? ? ? ? ?return "true" > ? ? ? ?else: > ? ? ? ? ? ? ? ?return "false" > > def randomMove2(d1, d2, a1, a2): > ? ? ? ? while 1: > ? ? ? ? ? ? ? ? turtle.left(random.uniform(a1,a2)) > ? ? ? ? ? ? ? ? checkForward(random.uniform(d1,d2)) > ? ? ? ? ? ? ? ? if outOfBounds() == 'true': > ? ? ? ? ? ? ? ? ? ? ? ? turtle.right(180) > ############################################################################# I copied your code between the hash lines and get this: File "my_turtle.py", line 19 if (abs(turtle.position()[0]) > turtle.window_height()/2) or ^ SyntaxError: invalid syntax Is that all of your code? it seems to be cut off -- Joel Goldstick From emile at fenx.com Sat Oct 2 19:18:53 2010 From: emile at fenx.com (Emile van Sebille) Date: Sat, 02 Oct 2010 10:18:53 -0700 Subject: [Tutor] Using contents of a document to change file names In-Reply-To: References: Message-ID: On 10/2/2010 8:56 AM Josep M. Fontana said... > Hi, > > This is my first posting to this list. Perhaps this has a very easy answer > but before deciding to post this message I consulted a bunch of Python > manuals and on-line reference documents to no avail. I would be very > grateful if someone could lend me a hand with this. > Hi Josep, Break the problem into pieces. Conceptually, you'll need to: -a- get the list of file names to change then for each -b- determine the new name -c- rename the file For -a- you'll need glob. For -c- use os.rename. -b- is a bit more involved. To break -b- down: -b1- break out the x-xx portion of the file name -b2- look up the corresponding year in the other file -b3- convert the year to the century-half structure -b4- put the pieces together to form the new file name For -b2- I'd suggest building a dictionary from your second files contents as a first step to facilitate the subsequent lookups. You haven't included any code and we can't gauge how far along you are, so follow up and post the code you write as you go and we'll be better able to help out. Hope this is enough to get you started, Emile > Here's the problem I want to solve. I have a lot of files with the following > name structure: > > A-01-namex.txt > A-02-namey.txt > ... > N-09-namez.txt > > These are different text documents that I want to process for an NLP project > I'm starting. Each one of the texts belongs to a different century and it is > important to be able to include the information about the century in the > name of the file as well as inside the text. > > Then I have another text file containing information about the century each > one of the texts was written. This document has the following structure: > > A-01, 1278 > A-02, 1501 > ... > N-09, 1384 > > What I would like to do is to write a little script that would do the > following: > > . Read each row of the text containing information about the centuries each > one of the texts was written > . Change the name of the file whose name starts with the code in the first > column in the following way > > A-01-namex.txt --> A-01-namex_13-2.txt > > Where 13-1 means: 13th 2nd half. Obviously this information would com > from the second column in the text: 1278 (the first two digits + 1 = > century; if the 3rd and 4th digits> 50, then 2; if< 50 then 1) > > Then in the same script or in a new one, I would need to open each one of > the texts and add information about the century they were written on the > first line preceded by some symbol (e.g @13-2) > > I've found a lot of information about changing file names (so I know that I > should be importing the os module), but none of the examples that were cited > involved getting the information for the file changing operation from the > contents of a document. > > As you can imagine, I'm pretty green in Python programming and I was hoping > the learn by doing method would work. I need to get on with this project, > though, and I'm kind of stuck. Any help you guys can give me will be very > helpful. > > Josep M. > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From emile at fenx.com Sat Oct 2 19:32:21 2010 From: emile at fenx.com (Emile van Sebille) Date: Sat, 02 Oct 2010 10:32:21 -0700 Subject: [Tutor] function error In-Reply-To: References: <38D9DADF-E1A8-4BDC-8FF8-5F48C5510B23@gmail.com> <102031.75642.qm@web86707.mail.ird.yahoo.com> Message-ID: On 10/2/2010 10:16 AM Joel Goldstick said... >> ############################################################################## >> import turtle, random >> >> def checkForward(distance): >> old_position = turtle.position() >> turtle._pen.up() >> # no show/hide turtle methods in my turtle module ! >> turtle.forward(distance) >> forward_failed = outOfBounds() >> turtle.setx(old_position[0]); turtle.sety(old_position[1]) >> turtle._pen.down() >> # no show/hide turtle methods in my turtle module ! >> if outOfBounds() == 'false': >> turtle.forward(distance) >> >> def stuck(): >> return forward_failed >> >> def outOfBounds(): >> if (abs(turtle.position()[0])> turtle.window_height()/2) or >> (abs(turtle.position()[1])> turtle.window_width()/2): >> return "true" >> else: >> return "false" >> >> def randomMove2(d1, d2, a1, a2): >> while 1: >> turtle.left(random.uniform(a1,a2)) >> checkForward(random.uniform(d1,d2)) >> if outOfBounds() == 'true': >> turtle.right(180) >> ############################################################################# > > I copied your code between the hash lines and get this: > > File "my_turtle.py", line 19 > if (abs(turtle.position()[0])> turtle.window_height()/2) or > ^ > SyntaxError: invalid syntax > > > Is that all of your code? it seems to be cut off Do you see it now? Your reply contained the 10 ten lines or so of code that follows. Emile From steve at alchemy.com Sat Oct 2 19:45:43 2010 From: steve at alchemy.com (Steve Willoughby) Date: Sat, 02 Oct 2010 10:45:43 -0700 Subject: [Tutor] function error In-Reply-To: References: <38D9DADF-E1A8-4BDC-8FF8-5F48C5510B23@gmail.com> <102031.75642.qm@web86707.mail.ird.yahoo.com> Message-ID: <4CA76FC7.80902@alchemy.com> On 02-Oct-10 10:32, Emile van Sebille wrote: >> File "my_turtle.py", line 19 >> if (abs(turtle.position()[0])> turtle.window_height()/2) or >> ^ >> SyntaxError: invalid syntax How does Python know that the next line is the continuation of your if statement, instead of the beginning of a new line of code? You didn't do anything to indicate continuation, so it takes this as an incomplete statement. "If ... stuff ... or... (or what??)" You need to END that line with a backslash if (abs(...height()/2) or \ ...continuation...: or else make the line break INSIDE a set of parentheses, for Python to keep looking to the next line(s) to complete the statement. From emile at fenx.com Sat Oct 2 20:42:16 2010 From: emile at fenx.com (Emile van Sebille) Date: Sat, 02 Oct 2010 11:42:16 -0700 Subject: [Tutor] function error In-Reply-To: <4CA76FC7.80902@alchemy.com> References: <38D9DADF-E1A8-4BDC-8FF8-5F48C5510B23@gmail.com> <102031.75642.qm@web86707.mail.ird.yahoo.com> <4CA76FC7.80902@alchemy.com> Message-ID: On 10/2/2010 10:45 AM Steve Willoughby said... > On 02-Oct-10 10:32, Emile van Sebille wrote: Well, not really -- this is the OPs code. I was responding to Joel's comment of not seeing the entire post. >>> File "my_turtle.py", line 19 >>> if (abs(turtle.position()[0]) > turtle.window_height()/2) or >>> ^ >>> SyntaxError: invalid syntax > > How does Python know that the next line is the continuation of your if > statement, instead of the beginning of a new line of code? Because of the paren count -- I didn't actually parse the OPs code and so I assumed that, but now I'll assume the line break probably resulted post send. Continuing-to-assume-ly y'rs, Emile Here's the code fragment now paren'd... import turtle, random def checkForward(distance): old_position = turtle.position() turtle._pen.up() # no show/hide turtle methods in my turtle module ! turtle.forward(distance) forward_failed = outOfBounds() turtle.setx(old_position[0]); turtle.sety(old_position[1]) turtle._pen.down() # no show/hide turtle methods in my turtle module ! if outOfBounds() == 'false': turtle.forward(distance) def stuck(): return forward_failed def outOfBounds(): if ((abs(turtle.position()[0])>turtle.window_height()/2) or (abs(turtle.position()[1])> turtle.window_width()/2)): return "true" else: return "false" def randomMove2(d1, d2, a1, a2): while 1: turtle.left(random.uniform(a1,a2)) checkForward(random.uniform(d1,d2)) if outOfBounds() == 'true': turtle.right(180) From roberto03 at gmail.com Sat Oct 2 20:47:45 2010 From: roberto03 at gmail.com (roberto) Date: Sat, 2 Oct 2010 20:47:45 +0200 Subject: [Tutor] function error In-Reply-To: <4CA76FC7.80902@alchemy.com> References: <38D9DADF-E1A8-4BDC-8FF8-5F48C5510B23@gmail.com> <102031.75642.qm@web86707.mail.ird.yahoo.com> <4CA76FC7.80902@alchemy.com> Message-ID: On Sat, Oct 2, 2010 at 7:45 PM, Steve Willoughby wrote: > On 02-Oct-10 10:32, Emile van Sebille wrote: >>> >>> File "my_turtle.py", line 19 >>> if (abs(turtle.position()[0])> turtle.window_height()/2) or >>> ^ >>> SyntaxError: invalid syntax > > How does Python know that the next line is the continuation of your if > statement, instead of the beginning of a new line of code? ?You didn't do > anything to indicate continuation, so it takes this as an incomplete > statement. "If ... stuff ... or... (or what??)" > it's a simple editing problem : my editor and my python idle don't ever complain about that problem, sorry -- roberto From joel.goldstick at gmail.com Sat Oct 2 21:13:15 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sat, 2 Oct 2010 15:13:15 -0400 Subject: [Tutor] Using contents of a document to change file names In-Reply-To: References: Message-ID: On Sat, Oct 2, 2010 at 1:18 PM, Emile van Sebille wrote: > On 10/2/2010 8:56 AM Josep M. Fontana said... > > Hi, >> >> This is my first posting to this list. Perhaps this has a very easy answer >> but before deciding to post this message I consulted a bunch of Python >> manuals and on-line reference documents to no avail. I would be very >> grateful if someone could lend me a hand with this. >> >> > Hi Josep, > > Break the problem into pieces. > > Conceptually, you'll need to: > > -a- get the list of file names to change then for each > -b- determine the new name > -c- rename the file > > For -a- you'll need glob. For -c- use os.rename. -b- is a bit more > involved. To break -b- down: > > -b1- break out the x-xx portion of the file name > -b2- look up the corresponding year in the other file > -b3- convert the year to the century-half structure > -b4- put the pieces together to form the new file name > > For -b2- I'd suggest building a dictionary from your second files contents > as a first step to facilitate the subsequent lookups. > > You haven't included any code and we can't gauge how far along you are, so > follow up and post the code you write as you go and we'll be better able to > help out. > > Hope this is enough to get you started, > > Emile > > I echo Emile's excellent description. He didn't speak to changing what is in the file. I would read from the first file a line at a time and concatinate the citation date information to that string, then write the string to a second file that is named according to your desired rule. Then copy the rest of the text to the new file. When you are done, save the new file and delete the original if you don't need it any longer. > > > > > Here's the problem I want to solve. I have a lot of files with the >> following >> name structure: >> >> A-01-namex.txt >> A-02-namey.txt >> ... >> N-09-namez.txt >> >> These are different text documents that I want to process for an NLP >> project >> I'm starting. Each one of the texts belongs to a different century and it >> is >> important to be able to include the information about the century in the >> name of the file as well as inside the text. >> >> Then I have another text file containing information about the century >> each >> one of the texts was written. This document has the following structure: >> >> A-01, 1278 >> A-02, 1501 >> ... >> N-09, 1384 >> >> What I would like to do is to write a little script that would do the >> following: >> >> . Read each row of the text containing information about the centuries >> each >> one of the texts was written >> . Change the name of the file whose name starts with the code in the first >> column in the following way >> >> A-01-namex.txt --> A-01-namex_13-2.txt >> >> Where 13-1 means: 13th 2nd half. Obviously this information would com >> from the second column in the text: 1278 (the first two digits + 1 = >> century; if the 3rd and 4th digits> 50, then 2; if< 50 then 1) >> >> Then in the same script or in a new one, I would need to open each one of >> the texts and add information about the century they were written on the >> first line preceded by some symbol (e.g @13-2) >> >> I've found a lot of information about changing file names (so I know that >> I >> should be importing the os module), but none of the examples that were >> cited >> involved getting the information for the file changing operation from the >> contents of a document. >> >> As you can imagine, I'm pretty green in Python programming and I was >> hoping >> the learn by doing method would work. I need to get on with this project, >> though, and I'm kind of stuck. Any help you guys can give me will be very >> helpful. >> >> Josep M. >> >> >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From g.nius.ck at gmail.com Sat Oct 2 21:32:50 2010 From: g.nius.ck at gmail.com (Chris King) Date: Sat, 02 Oct 2010 15:32:50 -0400 Subject: [Tutor] Networking Message-ID: <4CA788E2.4060006@gmail.com> Dear Tutors, I have attached my 2 programs for networking. It uses socket and SocketServer, but it just simplifies it even more. The problem is it won't work. The Client raises the error, (with trace back) Traceback (most recent call last): File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 34, in print client.recv() File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 16, in recv return self.__sock.recv(1024) error: [Errno 10053] An established connection was aborted by the software in your host machine The server seems to get an error of some sort, but also seems to catch and print it. It prints ---------------------------------------- Exception happened during processing of request from ('127.0.0.1', 1424) Traceback (most recent call last): File "C:\Python26\lib\SocketServer.py", line 281, in _handle_request_noblock self.process_request(request, client_address) File "C:\Python26\lib\SocketServer.py", line 307, in process_request self.finish_request(request, client_address) File "C:\Python26\lib\SocketServer.py", line 320, in finish_request self.RequestHandlerClass(request, client_address, self) TypeError: 'NoneType' object is not callable ---------------------------------------- I look at both of the documentations of socket and SocketServer, but I couldn't firgue it out. I don't know much about networking. Please Help Sincerely, Me -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: server.py Type: text/x-python Size: 701 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: client.py Type: text/x-python Size: 1346 bytes Desc: not available URL: From evert.rol at gmail.com Sat Oct 2 21:40:33 2010 From: evert.rol at gmail.com (Evert Rol) Date: Sat, 2 Oct 2010 21:40:33 +0200 Subject: [Tutor] Networking In-Reply-To: <4CA788E2.4060006@gmail.com> References: <4CA788E2.4060006@gmail.com> Message-ID: <33D037A9-0230-42B3-9AFC-A1884F00AE88@gmail.com> > Dear Tutors, > I have attached my 2 programs for networking. It uses socket and SocketServer, but it just simplifies it even more. The problem is it won't work. The Client raises the error, (with trace back) > Traceback (most recent call last): > File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 34, in > print client.recv() > File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 16, in recv > return self.__sock.recv(1024) > error: [Errno 10053] An established connection was aborted by the software in your host machine > The server seems to get an error of some sort, but also seems to catch and print it. It prints > ---------------------------------------- > Exception happened during processing of request from ('127.0.0.1', 1424) > Traceback (most recent call last): > File "C:\Python26\lib\SocketServer.py", line 281, in _handle_request_noblock > self.process_request(request, client_address) > File "C:\Python26\lib\SocketServer.py", line 307, in process_request > self.finish_request(request, client_address) > File "C:\Python26\lib\SocketServer.py", line 320, in finish_request > self.RequestHandlerClass(request, client_address, self) > TypeError: 'NoneType' object is not callable > ---------------------------------------- > I look at both of the documentations of socket and SocketServer, but I couldn't firgue it out. I don't know much about networking. Please Help I don't know much about networking, less so about it on Windows; also, I've only scanned quickly through the server script, but I notice your create_handler() function doesn't return anything. I guess it needs to return the Handler class. The example in the Python docs doesn't use a factory function, but instead directly puts the class as the second argument to TCPServer. So the second argument needs to be a class, but since your create_handler() function returns nothing, you presumably get this NoneType exception. Evert From alan.gauld at btinternet.com Sat Oct 2 22:34:52 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Sat, 2 Oct 2010 20:34:52 +0000 (GMT) Subject: [Tutor] function error In-Reply-To: References: <38D9DADF-E1A8-4BDC-8FF8-5F48C5510B23@gmail.com> <102031.75642.qm@web86707.mail.ird.yahoo.com> Message-ID: <314947.3484.qm@web86707.mail.ird.yahoo.com> > ############################################################################## > import turtle, random > > def checkForward(distance): > old_position = turtle.position() > turtle._pen.up() > turtle.forward(distance) > forward_failed = outOfBounds() you set forward failed but never use it? > turtle.setx(old_position[0]); turtle.sety(old_position[1]) > turtle._pen.down() > if outOfBounds() == 'false': > turtle.forward(distance) If you change outOfVBounds to return a real boolean result you can just say if outOfBounds(): etc... > def stuck(): > return forward_failed forward failed does not exist here it is a local variable in checkForward. If you want to use it like this you need to create it as a global variable outside any of the function bodies. > def outOfBounds(): > if (abs(turtle.position()[0]) > turtle.window_height()/2) or > (abs(turtle.position()[1]) > turtle.window_width()/2): > return "true" > else: > return "false" You can miss the if statement and just return the expression, that will give you a real True/False boolean result which is easier to work with than strings. return (abs(turtle.position()[0]) > turtle.window_height()/2) or (abs(turtle.position()[1]) > turtle.window_width()/2) However does this test check for out of bounds in all directions? What happens if the turtle is travelling west/south rather than east/north? It may work but I don't see it. > def randomMove2(d1, d2, a1, a2): > while 1: This is usually written as while True: nowadays > turtle.left(random.uniform(a1,a2)) > checkForward(random.uniform(d1,d2)) > if outOfBounds() == 'true': > turtle.right(180) I don;t know iof any of that solves the probnl;em but it might tidy the code a little HTH, Alan G. From angel1storage at yahoo.com Sat Oct 2 17:22:42 2010 From: angel1storage at yahoo.com (a a) Date: Sat, 2 Oct 2010 08:22:42 -0700 (PDT) Subject: [Tutor] how to learn python from scratch, (for simpe people)? Message-ID: <177858.17258.qm@web37304.mail.mud.yahoo.com> Dear Friends, I am writing for some assistence from an expert. I give you a short background to understand my need. (for your information I work with a macbook normally), but I do have desktop in Microsoft) I am not a programmer. My work has to do with old manuscripts and Philosophy. But recently, being in need of building a web site I did learn alone some html, dreamweaver, css and I made two web sites who came out pretty good. So, I have done of it my hobby, since I do like web design. But now I would like to go further, although remaining always an hobby. I would like to learn some programming, since I wish to build a program to run a database. Mybe more in the future if this goes well. I have some text, several books, text format. I want to make a program to vew and research these texts. Perhaps on a cd and on a desktop. On the internet I have read that an easy to learn and yet powerful language to start with is python. Now my questions: 1) Is python enough and complete to build a simple program of the kind deskribed above? 2) Will I be able to build standalone program with it? 2) where do I start learning? SOMETHING AS SIMPE AS POSSIBLE! Thank you very much Jerome -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Oct 2 22:40:41 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Sat, 2 Oct 2010 20:40:41 +0000 (GMT) Subject: [Tutor] data question In-Reply-To: References: , , , <580321.35313.qm@web86705.mail.ird.yahoo.com>, Message-ID: <443912.44029.qm@web86701.mail.ird.yahoo.com> > I think this is what can work : http://www.daniweb.com/code/snippet216750.html > > I will try it. Good, that is exactly the kind of struicture I had in mind. Just put the data structure in a module, forget about the code and import it to the >>> prompt and play with it till you are sure you know how to extract any piece of data you need, iterate over the structure, add/delete from it etc. HTH, Alan G. From aeneas24 at priest.com Sat Oct 2 23:29:29 2010 From: aeneas24 at priest.com (aeneas24 at priest.com) Date: Sat, 02 Oct 2010 17:29:29 -0400 Subject: [Tutor] Getting total counts (Steven D'Aprano) In-Reply-To: References: Message-ID: <8CD3090081B903A-F88-65BB@web-mmc-d01.sysops.aol.com> Thanks very much for the extensive comments, Steve. I can get the code you wrote to work on my toy data, but my real input data is actually contained in 10 files that are about 1.5 GB each--when I try to run the code on one of those files, everything freezes. To solve this, I tried just having the data write to a different csv file: lines = csv.reader(file(src_filename)) csv_writer = csv.writer(file(output_filename, 'w')) for line in lines: doc, g1, g2, g3, rating, ratingmax, reviewer, helpful, h_total, word, count = line row = [add_word(g1, word, count), add_word(g2, word, count), add_word(g3, word, count)] csv_writer.writerow(row) This doesn't work--I think there are problems in how the iterations happen. But my guess is that converting from one CSV to another isn't going to be as efficient as creating a shelve database. I have some code that works to create a db when I release it on a small subset of my data, but when I try to turn one of the 1.5 GB files into a db, it can't do it. I don't understand why it works for small data and not big (it makes sense to me that your table approach might choke on big amounts of data--but why the shelve() code below?) I think these are the big things I'm trying to get the code to do: - Get my giant CSV files into a useful format, probably a db (can do for small amounts of data, but not large) - Extract genre and star-rating information about particular words from the db (I seem to be able to do this) - Get total counts for all words in each genre, and for all words in each star-rating category (your table approach works on small data, but I can't get it to scale) def csv2shelve(src_filename, shelve_filename): # I open the shelve file for writing. if os.path.exists(shelve_filename): os.remove(shelve_filename) # I create the shelve db. db = shelve.open(shelve_filename, writeback=True) # The writeback stuff is a little confusing in the help pages, maybe this is a problem? # I open the src file. lines = csv.reader(file(src_filename)) for line in lines: doc, g1, g2, g3, rating, word, count = line if word not in db: db[word] = [] try: rating = int(rating) except: pass db[word].append({ "genres":{g1:True, g2:True, g3:True}, "rating":rating, "count":int(count) }) db.close() Thanks again, Steve. (And everyone/anyone else.) Tyler -----Original Message----- From: tutor-request at python.org To: tutor at python.org Sent: Sat, Oct 2, 2010 1:36 am Subject: Tutor Digest, Vol 80, Issue 10 Send Tutor mailing list submissions to tutor at python.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.python.org/mailman/listinfo/tutor r, via email, send a message with subject or body 'help' to tutor-request at python.org You can reach the person managing the list at tutor-owner at python.org When replying, please edit your Subject line so it is more specific han "Re: Contents of Tutor digest..." oday's Topics: 1. Re: (de)serialization questions (Lee Harr) 2. Re: regexp: a bit lost (Steven D'Aprano) 3. Re: regexp: a bit lost (Alex Hall) 4. Re: (de)serialization questions (Alan Gauld) 5. Re: Getting total counts (Steven D'Aprano) 6. data question (Roelof Wobben) --------------------------------------------------------------------- Message: 1 ate: Sat, 2 Oct 2010 03:26:21 +0430 rom: Lee Harr o: ubject: Re: [Tutor] (de)serialization questions essage-ID: ontent-Type: text/plain; charset="windows-1256" >> I have data about zip codes, street and city names (and perhaps later also f >> street numbers). I made a dictionary of the form {zipcode: (street, city)} > > One dictionary with all of the data? > > That does not seem like it will work. What happens when > 2 addresses have the same zip code? You did not answer this question. Did you think about it? Maybe my main question is as follows: what permanent object is most suitable o store a large amount of entries (maybe too many to fit into the computer's memory), which can be looked up very fast. One thing about Python is that you don't normally need to hink about how your objects are stored (memory management). It's an advantage in the normal case -- you just use the most onvenient object, and if it's fast enough and small enough ou're good to go. Of course, that means that if it is not fast enough, or not mall enough, then you've got to do a bit more work to do. Eventually, I want to create two objects: 1-one to look up street name and city using zip code So... you want to have a function like: def addresses_by_zip(zipcode): ?? '''returns list of all addresses in the given zipcode''' ?? .... 2-one to look up zip code using street name, apartment number and city and another one like: def zip_by_address(street_name, apt, city): ?? '''returns the zipcode for the given street name, apartment, and city''' ?? .... o me, it sounds like a job for a database (at least behind the scenes), ut you could try just creating a custom Python object that holds hese things: class Address(object): ?? street_number = '345' ?? street_name = 'Main St' ?? apt = 'B' ?? city = 'Springfield' ?? zipcode = '99999' Then create another object that holds a collection of these addresses nd has methods addresses_by_zip(self, zipcode) and ip_by_address(self, street_number, street_name, apt, city) I stored object1 in a marshalled dictionary. Its length is about 450.000 (I ive in Holland, not THAT many streets). Look-ups are incredibly fast (it has to, because it's part of an autocompletion feature of a data entry program). I haven't got the street number data needed for object2 yet, but it's going to e much larger. Many streets have different zip codes for odd or even numbers, or the zip codes are divided into street number ranges (for long streets). Remember that you don't want to try to optimize too soon. Build a simple working system and see what happens. If it s too slow or takes up too much memory, fix it. You suggest to simply use a file. I like simple solutions, but doesn't that, y definition, require a slow, linear search? You could create an index, but then any database will already have n indexing function built in. I'm not saying that rolling your own custom database is a bad idea, ut if you are trying to get some work done (and not just playing around nd learning Python) then it's probably better to use something that is lready proven to work. f you have some code you are trying out, but are not sure you re going the right way, post it and let people take a look at it. ----------------------------- Message: 2 ate: Sat, 2 Oct 2010 10:19:21 +1000 rom: Steven D'Aprano o: Python Tutor ubject: Re: [Tutor] regexp: a bit lost essage-ID: <201010021019.21909.steve at pearwood.info> ontent-Type: text/plain; charset="iso-8859-1" On Sat, 2 Oct 2010 01:14:27 am Alex Hall wrote: >> Here is my test: >> s=re.search(r"[\d+\s+\d+\s+\d]", l) > > Try this instead: > > re.search(r'\d+\s+\D*\d+\s+\d', l) ...] Understood. My intent was to ask why my regexp would match anything at all. Square brackets create a character set, so your regex tests for a string hat contains a single character matching a digit (\d), a plus sign (+) r a whitespace character (\s). The additional \d + \s in the square rackets are redundant and don't add anything. -- teven D'Aprano ----------------------------- Message: 3 ate: Fri, 1 Oct 2010 20:47:29 -0400 rom: Alex Hall o: "Steven D'Aprano" c: Python Tutor ubject: Re: [Tutor] regexp: a bit lost essage-ID: ontent-Type: text/plain; charset=ISO-8859-1 On 10/1/10, Steven D'Aprano wrote: On Sat, 2 Oct 2010 01:14:27 am Alex Hall wrote: > >> Here is my test: > >> s=re.search(r"[\d+\s+\d+\s+\d]", l) > > > > Try this instead: > > > > re.search(r'\d+\s+\D*\d+\s+\d', l) [...] > Understood. My intent was to ask why my regexp would match anything > at all. Square brackets create a character set, so your regex tests for a string that contains a single character matching a digit (\d), a plus sign (+) or a whitespace character (\s). The additional \d + \s in the square brackets are redundant and don't add anything. h, that explains it then. :) Thanks. -- Steven D'Aprano _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor - ave a great day, lex (msg sent from GMail website) ehgcap at gmail.com; http://www.facebook.com/mehgcap ----------------------------- Message: 4 ate: Sat, 2 Oct 2010 02:01:40 +0100 rom: "Alan Gauld" o: tutor at python.org ubject: Re: [Tutor] (de)serialization questions essage-ID: ontent-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Albert-Jan Roskam" wrote > Maybe my main question is as follows: what permanent object is most suitable to store a large amount of entries (maybe too many to fit into the computer's memory), which can be looked up very fast. It depends on the nature of the object and the lookup but in general database would be the best solution. For special (heirarchical) ata an LDAP directory may be more appropriate. Otherwise you are looking at a custom designed file structure. > Eventually, I want to create two objects: 1-one to look up street name and city using zip code 2-one to look up zip code using street name, apartment number and city For this a simple relational database wouldbe best. QLlite should do and is part of the standard library. t can also be used in memory for faster speed with smaller data sets. > You suggest to simply use a file. I like simple solutions, but doesn't that, by definition, require a slow, linear search? No, you can use random access provided yopu can relate the key to the ocation - thats what databases do for you under the covers. > Funny you should mention sqlite: I was just considering it yesterday. Gosh, Python has so much interesting stuff to offer! Sqlite operating in-memory would be a good solution for you I think. You can get a basic tutorial on Sqllite and python in the databases opic f my tutorial... HTH, - lan Gauld uthor of the Learn to Program web site ttp://www.alan-g.me.uk/ ----------------------------- Message: 5 ate: Sat, 2 Oct 2010 11:13:04 +1000 rom: Steven D'Aprano o: tutor at python.org ubject: Re: [Tutor] Getting total counts essage-ID: <201010021113.04679.steve at pearwood.info> ontent-Type: text/plain; charset="utf-8" On Sat, 2 Oct 2010 06:31:42 am aeneas24 at priest.com wrote: Hi, I have created a csv file that lists how often each word in the Internet Movie Database occurs with different star-ratings and in different genres. I would have thought that IMDB would probably have already made that nformation available? http://www.imdb.com/interfaces The input file looks something like this--since movies can have multiple genres, there are three genre rows. (This is fake, simplified data.) ...] I can get the program to tell me how many occurrence of "the" there are in Thrillers (50), how many "the"'s in 1-stars (50), and how many 1-star drama "the"'s there are (30). But I need to be able to expand beyond a particular word and say "how many words total are in "Drama"? How many total words are in 1-star ratings? How many words are there in the whole corpus? On these all-word totals, I'm stumped. The headings of your data look like this: ID | Genre1 | Genre2 | Genre3 | Star-rating | Word | Count and you want to map words to genres. Can you tell us how big the CSV ile is? Depending on its size, you may need to use on-disk storage perhaps shelve, as you're already doing) but for illustration purposes 'll assume it all fits in memory and just use regular dicts. I'm going o create a table that stores the counts for each word versus the enre: enre | the | scary | silly | exciting | ... ----------------------------------------------- estern | 934 | 3 | 5 | 256 | hriller | 899 | 145 | 84 | 732 | omedy | 523 | 1 | 672 | 47 | .. To do this using dicts, I'm going to use a dict for genres: genre_table = {"Western": table_of_words, ...} and each table_of_words will look like: {'the': 934, 'scary': 3, 'silly': 5, ...} et's start with a helper function and table to store the data. # Initialise the table. enres = {} def add_word(genre, word, count): genre = genre.title() # force "gEnRe" to "Genre" word = word.lower() # force "wOrD" to "word" count = int(count) row = genres.get(genre, {}) n = row.get(word, 0) row[word] = n + count genres[genre] = row e can simplify this code by using the confusingly named, but useful, etdefault method of dicts: def add_word(genre, word, count): genre = genre.title() word = word.lower() count = int(count) row = genres.setdefault(genre, {}) row[word] = row.get(word, 0) + count Now let's process the CSV file. I'm afraid I can't remember how the CSV odule works, and I'm too lazy to look it up, so this is pseudo-code ather than Python: for row in csv file: genre1 = get column Genre1 genre2 = get column Genre2 genre3 = get column Genre3 word = get column Word count = get column Count add_word(genre1, word, count) add_word(genre2, word, count) add_word(genre3, word, count) ow we can easily query our table for useful information: # list of unique words for the Western genre enres["Western"].keys() count of unique words for the Romance genre en(genres["Romance"]) number of times "underdog" is used in Sports movies enres["Sport"]["underdog"] total count of words for the Comedy genre um(genres["Comedy"].values()) Do you want to do lookups efficiently the other way as well? It's easy o add another table: Word | Western | Thriller | ... ----------------------------------------------- he | 934 | 899 | cary | 3 | 145 | .. dd a second global table: genres = {} ords = {} nd modify the helper function: def add_word(genre, word, count): genre = genre.title() word = word.lower() count = int(count) # Add word to the genres table. row = genres.setdefault(genre, {}) row[word] = row.get(word, 0) + count # And to the words table. row = words.setdefault(word, {}) row[genre] = row.get(genre, 0) + count - teven D'Aprano ----------------------------- Message: 6 ate: Sat, 2 Oct 2010 08:35:13 +0000 rom: Roelof Wobben o: ubject: [Tutor] data question essage-ID: ontent-Type: text/plain; charset="iso-8859-1" Hello, s a test I would write a programm where a user can input game-data like ome-team, away-team, home-score, away-score) and makes a ranking of it. And I'm ot looking of a OOP solution because im not comfertle with OOP. ow my question is : n which datatype can I put this data in. thought myself of a dictonary of tuples. egards, oelof ------------------------------ _______________________________________________ utor maillist - Tutor at python.org ttp://mail.python.org/mailman/listinfo/tutor nd of Tutor Digest, Vol 80, Issue 10 ************************************ -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Sun Oct 3 01:21:44 2010 From: emile at fenx.com (Emile van Sebille) Date: Sat, 02 Oct 2010 16:21:44 -0700 Subject: [Tutor] how to learn python from scratch, (for simpe people)? In-Reply-To: <177858.17258.qm@web37304.mail.mud.yahoo.com> References: <177858.17258.qm@web37304.mail.mud.yahoo.com> Message-ID: On 10/2/2010 8:22 AM a a said... > Dear Friends, > > I am writing for some assistence from an expert. > I give you a short background to understand my need. > (for your information I work with a macbook normally), but I do have desktop in Microsoft) > I am not a programmer. My work has to do with old manuscripts and Philosophy. > But recently, being in need of building a web site I did learn alone some html, dreamweaver, css and I made two web sites who came out pretty good. > So, I have done of it my hobby, since I do like web design. > But now I would like to go further, although remaining always an hobby. > I would like to learn some programming, since I wish to build a program to run a database. Mybe more in the future if this goes well. > I have some text, several books, text format. I want to make a program to vew and research these texts. Perhaps on a cd and on a desktop. > > On the internet I have read that an easy to learn and yet powerful language to start with is python. > Now my questions: > 1) Is python enough and complete to build a simple program of the kind deskribed above? > 2) Will I be able to build standalone program with it? > 2) where do I start learning? SOMETHING AS SIMPE AS POSSIBLE! > > Thank you very much > Jerome > > I'd suggest you check out Alan's site at http://www.freenetpages.co.uk/hp/alan.gauld/ He hangs out here and you'll be able to get any additional clarification you need. You might also find this helpful http://diveintopython.org/toc/index.html Finally, i'd suggest you start with python2 (vs python3) -- most of the info you'll find on the net is python2 and not always up to date for python3. Good luck! Emile From steve at pearwood.info Sun Oct 3 01:28:17 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 3 Oct 2010 09:28:17 +1000 Subject: [Tutor] Connecting my users In-Reply-To: <4CA70416.2050205@gmail.com> References: <4CA70416.2050205@gmail.com> Message-ID: <201010031028.18762.steve@pearwood.info> On Sat, 2 Oct 2010 08:06:14 pm Timo wrote: > On 01-10-10 11:25, Nitin Pawar wrote: > > have a look at this > > > > http://bytes.com/topic/python/answers/826973-peer-peer-chat-program > > Thanks, but that still uses a server. And even one that I can't > control! If it has to be with server interaction, than as little as > possible is preferred and option to put it on my own. You obviously didn't read the article carefully enough. It describes a peer-to-peer chat program, and I quote from the project page: "kaishi is a chat program without any central servers. Currently all users connected to kaishi are on the same level of the network, meaning no user has more control over the others." -- Steven D'Aprano From emile at fenx.com Sun Oct 3 01:29:24 2010 From: emile at fenx.com (Emile van Sebille) Date: Sat, 02 Oct 2010 16:29:24 -0700 Subject: [Tutor] how to learn python from scratch, (for simpe people)? In-Reply-To: References: <177858.17258.qm@web37304.mail.mud.yahoo.com> Message-ID: On 10/2/2010 4:21 PM Emile van Sebille said... > On 10/2/2010 8:22 AM a a said... >> Now my questions: >> 1) Is python enough and complete to build a simple program of the kind >> deskribed above? Yes >> 2) Will I be able to build standalone program with it? Yes Sorry - Forget to answer your questions. Python's a great choice. Regards, Emile From alan.gauld at btinternet.com Sun Oct 3 01:46:34 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 3 Oct 2010 00:46:34 +0100 Subject: [Tutor] data question References: , <007801cb6232$18d1a370$4a74ea50$@rr.com>, Message-ID: "Roelof Wobben" wrote >>> Perhaps a simple SQLlite database? > Oke, there I can save the input data. > But I have also need a data model for team, played_games, > game_points, > made_points and againts_points. > So I think it cannot be done without using a class for games and one > for ranking. If you use a database you won't need any classes. All you need is the data model in the database and some SQL queries to extract and manipulate the date to produce your reports. But using a SQL database may be more than you can cope with just now IMHO, I'd stick to the basic Python for now. Alan G. From alan.gauld at btinternet.com Sun Oct 3 01:56:24 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 3 Oct 2010 00:56:24 +0100 Subject: [Tutor] how to learn python from scratch, (for simpe people)? References: <177858.17258.qm@web37304.mail.mud.yahoo.com> Message-ID: "a a" wrote > ...building a web site I did learn alone some html, dreamweaver, css > But now I would like to go further, although remaining always an > hobby. > I would like to learn some programming, since I wish to build a > program > to run a database. Mybe more in the future if this goes well. > Now my questions: > 1) Is python enough and complete to build a simple program of the > kind deskribed above? Python is nearly perfect for what you want. > 2) Will I be able to build standalone program with it? That depends on how you define standalone. In the same way that Visual Basic, Java and any of the .Net family of languages do standalone then yes you can. But you cannot build native executables, you need to have a Python interpreter installed ( there are programs that fake it by bundling the interpreter into the final .exe file but they are non trivial to use.) But if you mean can you create a program that can be launched just by double clicking it in Windows Explorer, then yes you can do that. But ultimately, for web based apps its irrelevant since the code all runs on the server not the users computer. > 2) where do I start learning? SOMETHING AS SIMPE AS POSSIBLE! I don't usually like pushing my own tutorial because there are lots of others but in this case I think it would actually be a good choice given your background and aspirations. Reasons are: 1) It covers Python for absolute beginners 2) It also covers JavaScript which is nearly essential for client side(browser) web programming 3) It covers database work and networking fundamentals (which very few other beginners tutorials do) 4) I read this list so can answer questions"! :-) As to Python v2 or v3, I normally recommend v2 since v3 still lags in support for 3rd party tools but I actually think that for your purposes you can go with either. You will ultimately want to learn a Web framework like Django and that will be a bigger step than moving between Python versions. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Sun Oct 3 02:02:09 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 3 Oct 2010 10:02:09 +1000 Subject: [Tutor] list comprehension, efficiency? In-Reply-To: <4CA74D13.5060902@gmail.com> References: <4CA74D13.5060902@gmail.com> Message-ID: <201010031102.09581.steve@pearwood.info> On Sun, 3 Oct 2010 01:17:39 am bob gailer wrote: > I ran dis on a for loop and the equivalent comprehension. > > I was surprised to see almost identical code. > > I had assumed (and now wish for) that a comprehension would be a > primitive written in C and thus much faster! How could it be? A list comp is syntactic sugar. It needs to perform the same steps as a for-loop, and call an arbitrary Python expression. It's not like map() that takes a function object. Unless you had a separate primitive for every imaginable Python expression -- which is impossible -- list comps need to generate relatively similar code to for-loops because they do relatively similar things. Besides, in recent versions of Python the speed of for-loops is quite good. The bottleneck is rarely the loop itself, but the work you do in the loop or the size of the loop. -- Steven D'Aprano From alan.gauld at btinternet.com Sun Oct 3 02:04:30 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 3 Oct 2010 01:04:30 +0100 Subject: [Tutor] Getting total counts (Steven D'Aprano) References: <8CD3090081B903A-F88-65BB@web-mmc-d01.sysops.aol.com> Message-ID: wrote > I can get the code you wrote to work on my toy data, but my real > input data is actually contained in 10 files that are about 1.5 GB > each--when I try to run the code on one of those files, everything > freezes. Fot those kind of volumes I'd go for a SQL database every time! (SQLlite might be OK but I'd be tempted to go to something even beefier, like MySQL, PostGres or Firebird) > To solve this, I tried just having the data write to a different csv > file: For huge data volumes sequential files like csv are always going to be slow. You need random access, and a full blown database will probably be the best bet IMHO. > But my guess is that converting from one CSV to another isn't > going to be as efficient as creating a shelve database. A shelve is fine for very simple lookups but its still basically a flat file. And the minute you need to access by anything other than the single key you are back to sequential processing. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sun Oct 3 02:11:40 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 3 Oct 2010 01:11:40 +0100 Subject: [Tutor] Smart posting - Was: Re: Tutor Digest, Vol 80, Issue 11 References: Message-ID: "Edward Lang" wrote As the standard header advice says in the very mail that you posted: >>When replying, please edit your Subject line so it is more specific >>than "Re: Contents of Tutor digest..." >> And also.... >>Today's Topics: >> >> 1. Re: Connecting my users (Timo) >> 2. Re: data question (Alan Gauld) >> 3. Re: data question (Roelof Wobben) >> 4. Re: data question (Roelof Wobben) >> 5. Re: data question (Roelof Wobben) >> 6. Re: data question (Robert Berman) >> 7. Re: Coin Toss Problems (bob gailer) Please, do not post the entire digest. We don't have time to wade through it looking for the few lines you may have added or commented upon. You have a delete key. Please use it. And while I'm having a rant, please do not top-post. (I don;t know if you are guilty of this particular crime because I couldn't see any new content in your mail, but it is a common problem...) Cut out everything but what is relevant and post your comments underneath. I know it's contrary to normal business practice but it's a lot easier to work with on a busy mailing list. Thankyou, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sun Oct 3 02:17:30 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 3 Oct 2010 01:17:30 +0100 Subject: [Tutor] Using contents of a document to change file names References: Message-ID: "Josep M. Fontana" wrote > Here's the problem I want to solve. I have a lot of files with the > following > name structure: > > A-01-namex.txt > Then I have another text file containing information about the > century each > one of the texts was written. This document has the following > structure: > > A-01, 1278 > > A-01-namex.txt --> A-01-namex_13-2.txt > > Where 13-1 means: 13th 2nd half. Obviously this information would > com > from the second column in the text: 1278 (the first two digits + 1 = > century; if the 3rd and 4th digits > 50, then 2; if < 50 then 1) > > Then in the same script or in a new one, I would need to open each > one of > the texts and add information about the century they were written on > the > first line preceded by some symbol (e.g @13-2) If you break it into discrete steps it is all petty basic file and string processing. You can rename the file using the os module You can construct the filename using string methods You can get the data to put in the filename by reading a file You can add the new century data by writing a file. Now tell us which of those operations is giving you the headache? Or is it the issue of building the overall program structure? Once we know more about where your specific problem lies we can give you a better answer. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Sun Oct 3 02:19:45 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 3 Oct 2010 10:19:45 +1000 Subject: [Tutor] Getting total counts (Steven D'Aprano) In-Reply-To: <8CD3090081B903A-F88-65BB@web-mmc-d01.sysops.aol.com> References: <8CD3090081B903A-F88-65BB@web-mmc-d01.sysops.aol.com> Message-ID: <201010031119.46268.steve@pearwood.info> On Sun, 3 Oct 2010 08:29:29 am aeneas24 at priest.com wrote: > Thanks very much for the extensive comments, Steve. I can get the > code you wrote to work on my toy data, but my real input data is > actually contained in 10 files that are about 1.5 GB each--when I try > to run the code on one of those files, everything freezes. Do you have 15 GB of memory? Actually, more, due to the overhead, the operating system, and so forth. If not, then I'm not surprised that things freeze. > To solve this, I tried just having the data write to a different csv > file: A 15GB csv file is not the right approach. > This doesn't work--I think there are problems in how the iterations > happen. But my guess is that converting from one CSV to another isn't > going to be as efficient as creating a shelve database. shelve is not an industrial strength database. It's a serialised dictionary. The source code for shelve is ~100 lines of code, plus comments and docstrings. Now Python is good, but it's not *that* good that it can create a full-strength database in 100 lines of code. I expect that at the very least you will need to use SQLite. With 15 GB of data, you may even find that SQLite isn't powerful enough and you may need a full blown Postgresql or MySQL database. P.S. Please don't quote the entire digest when replying: > -----Original Message----- > From: tutor-request at python.org > To: tutor at python.org > Sent: Sat, Oct 2, 2010 1:36 am > Subject: Tutor Digest, Vol 80, Issue 10 [snip approx 150 irrelevant lines] -- Steven D'Aprano From prologic at shortcircuit.net.au Sun Oct 3 02:45:30 2010 From: prologic at shortcircuit.net.au (James Mills) Date: Sun, 3 Oct 2010 10:45:30 +1000 Subject: [Tutor] Smart posting - Was: Re: Tutor Digest, Vol 80, Issue 11 In-Reply-To: References: Message-ID: On Sun, Oct 3, 2010 at 10:11 AM, Alan Gauld wrote: > Please, do not post the entire digest. We don't have time to wade through it > looking for the few lines you may have added or commented upon. I'm actually really surprised anyone actually uses and subscribes to mailing lists and wants to get digests. It's far easier (for me at least) to just get each email as it comes in. --James -- -- James Mills -- -- "Problems are solved by method" From steve at pearwood.info Sun Oct 3 07:03:42 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 3 Oct 2010 15:03:42 +1000 Subject: [Tutor] (de)serialization questions In-Reply-To: <933247.41889.qm@web110704.mail.gq1.yahoo.com> References: <933247.41889.qm@web110704.mail.gq1.yahoo.com> Message-ID: <201010031603.43237.steve@pearwood.info> On Sat, 2 Oct 2010 04:08:22 am Albert-Jan Roskam wrote: > Maybe my main question is as follows: what permanent object is most > suitable to store a large amount of entries (maybe too many to fit > into the computer's memory), which can be looked up very fast. "Very fast" and "cannot fit in main memory" do not go together. Reading from disk is 100-1000 times slower than reading from main memory, and writing to disk even slower. The best you can hope for is "fast enough". And that means using a database. Which database will depend on what data you are storing, how much there is, what you plan to do with it, and many other questions. > Eventually, I want to create two objects: > 1-one to look up street name and city using zip code > 2-one to look up zip code using street name, apartment number and > city > > I stored object1 in a marshalled dictionary. Its length is about > 450.000 (I live in Holland, not THAT many streets). That will probably fit in main memory. If not, add some more memory. Even if each address takes 1000 bytes of memory, that's only 450 MB in total. Double it for the second object and add 100 MB of overhead and you've only got 1 GB in total -- well within the capabilities of a server, or even a high-end desktop. Put 4 GB of the fastest memory you can find into the machine, and with luck it will be fine. But the only way to tell for sure will be to try it, and see how much memory it takes. Then consider what will happen if you double the number of records. Consider using pickle instead of marshal. marshal is really only intended for Python byte-code files. [...] > You suggest to simply use a file. I like simple solutions, but > doesn't that, by definition, require a slow, linear search? I chose a > dictionary for object1 because hash tables are very fast in terms of > lookup times. Another option might be to use the bisect module. Not > sure if using a binary search algoritm is much faster with these > still relatively small numbers. It's interesting for sure, but I > don't know whether using bisect would justify the added amount of > complexity and bug risks. Oh come on now, bisect is not that complicated! Yes, there's a small amount of added complexity, but it's still a simple algorithm. If you're seriously considering a linear search over 450 thousand items just to avoid doing a binary search, there's something seriously wrong. Is it worth it? Log base 2 of 450000 is less than 19, so the average binary search will take 19 lookups. The average linear search will take 225000 lookups. I'm confident that will be worth it. -- Steven D'Aprano From norman at khine.net Sun Oct 3 08:56:18 2010 From: norman at khine.net (Norman Khine) Date: Sun, 3 Oct 2010 08:56:18 +0200 Subject: [Tutor] retrieve google translate sound files through python Message-ID: does anyone know how i can extend this example http://stackoverflow.com/questions/1136604/how-do-i-use-the-json-google-translate-api and be able to download the sound file produced by the translation? thanks norman -- ?u?op ?p?sdn p,u?n? p??o? ??? ??s no? '?u???? s???? ??? pu? '?u??uo? ?q s,??? ??? %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From kirotawa at gmail.com Sun Oct 3 04:26:31 2010 From: kirotawa at gmail.com (leo kirotawa) Date: Sat, 2 Oct 2010 23:26:31 -0300 Subject: [Tutor] Connecting my users In-Reply-To: <201010031028.18762.steve@pearwood.info> References: <4CA70416.2050205@gmail.com> <201010031028.18762.steve@pearwood.info> Message-ID: Hi, I asked to my mate college and he said that: The ichat use bonjour(avahi/zeroconf) no server, has chat and exchange file, but should be in the same net level. There are too the milticas chatts, like this in python: http://my.opera.com/manojsheokand666/blog/chat-application-in-python. On Sat, Oct 2, 2010 at 8:28 PM, Steven D'Aprano wrote: > On Sat, 2 Oct 2010 08:06:14 pm Timo wrote: > > On 01-10-10 11:25, Nitin Pawar wrote: > > > have a look at this > > > > > > http://bytes.com/topic/python/answers/826973-peer-peer-chat-program > > > > Thanks, but that still uses a server. And even one that I can't > > control! If it has to be with server interaction, than as little as > > possible is preferred and option to put it on my own. > > You obviously didn't read the article carefully enough. It describes a > peer-to-peer chat program, and I quote from the project page: > > "kaishi is a chat program without any central servers. Currently all > users connected to kaishi are on the same level of the network, meaning > no user has more control over the others." > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Le?nidas S. Barbosa (Kirotawa) [DesenvolvedorWeb/CEFET/RN] [Ci?ncias da Computa??o/UFRN] [p?s-graduando em Intelig?ncia Computacional/Processamento Gr?fico /UFRN [Estudante de japon?s n?vel Intermedi?rio I - Japanese Student] [Desenvolvedor em python, PyGame] blog nerd: corecode.wordpress.com/ blog music: essenaomanja.blogspot.com blog tirinhas: elminiche.wordpress.com/ "Mais s?bio ? aquele que sabe que n?o sabe" (S?crates) ????????? ??????????????. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sun Oct 3 09:52:50 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 3 Oct 2010 08:52:50 +0100 Subject: [Tutor] Smart posting - Was: Re: Tutor Digest, Vol 80, Issue 11 References: Message-ID: "James Mills" wrote > I'm actually really surprised anyone actually uses and subscribes to > mailing lists and wants to get digests. It's far easier (for me at > least) > to just get each email as it comes in. I use digests for several mailing lists - and used to use it on the tutorlist untilGMane made it available as a news feed which is better still. The advantage of a digest is that I can read it all in one go rather than having to search for the mails intermingled with all the other stuff that comes in (around 200 mails per day of which list mail is low in priority) If there are 50 messages a day from tutor - which can happen - then that would push my inbox load up to 250/day but using a digest means it is only 201 - far easier to scan and search. I could do the same by setting up a rule to auto file it in a folder but the digest effectively does that for me and I'm lazy by nature :-) So digest is good, but posting the whole digest is bad! My personal view. Alan G. From bgailer at gmail.com Sun Oct 3 15:22:37 2010 From: bgailer at gmail.com (bob gailer) Date: Sun, 03 Oct 2010 09:22:37 -0400 Subject: [Tutor] list comprehension, efficiency? In-Reply-To: <201010031102.09581.steve@pearwood.info> References: <4CA74D13.5060902@gmail.com> <201010031102.09581.steve@pearwood.info> Message-ID: <4CA8839D.5090307@gmail.com> On 10/2/2010 8:02 PM, Steven D'Aprano wrote: > On Sun, 3 Oct 2010 01:17:39 am bob gailer wrote: > >> I ran dis on a for loop and the equivalent comprehension. >> >> I was surprised to see almost identical code. >> >> I had assumed (and now wish for) that a comprehension would be a >> primitive written in C and thus much faster! > How could it be? A list comp is syntactic sugar. It needs to perform the > same steps as a for-loop, and call an arbitrary Python expression. It's > not like map() that takes a function object. Unless you had a separate > primitive for every imaginable Python expression -- which is > impossible -- list comps need to generate relatively similar code to > for-loops because they do relatively similar things. Thank you. I needed that! > Besides, in recent versions of Python the speed of for-loops is quite > good. The bottleneck is rarely the loop itself, but the work you do in > the loop or the size of the loop. -- Bob Gailer 919-636-4239 Chapel Hill NC From norman at khine.net Sun Oct 3 21:44:54 2010 From: norman at khine.net (Norman Khine) Date: Sun, 3 Oct 2010 21:44:54 +0200 Subject: [Tutor] subprocess.call warning Message-ID: hello, from the docs http://docs.python.org/library/subprocess.html i see there is a WARNING about deadlock when using the subprocess.call. in my code i have this http://pastie.org/1197024 the first calls the 'sox' library which joins all the .wav files into one file and then i use the 'wav2swf' library to create a SWF output of the file. can the code be improved? thanks -- ?u?op ?p?sdn p,u?n? p??o? ??? ??s no? '?u???? s???? ??? pu? '?u??uo? ?q s,??? ??? %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From fomcl at yahoo.com Sun Oct 3 21:57:12 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sun, 3 Oct 2010 12:57:12 -0700 (PDT) Subject: [Tutor] (de)serialization questions Message-ID: <828487.45552.qm@web110712.mail.gq1.yahoo.com> Hi Lee, Alan and Steven, Thank you very much for your replies! First, Lee: >> That does not seem like it will work. What happens when >> 2 addresses have the same zip code? --> Sorry I didn't answer that before. When the zipcode is known, that's not a problem. The data typist simply has to enter the zip code and the street number and voil?, the street name and city name appear. A big time saver. When the zipcode is the UNknown, indeed I need street name, apt number, and city to get the right zip code. Without the street number, I might end up with a list of zip codes. But having no street number would automatically invalidate the given address. We couldn't possibly mail a letter without having the apt. number! I just ordered a book on sqlite this morning (http://www.amazon.com/SQLite-Chris-Newman/dp/067232685X/ref=sr_1_2?ie=UTF8&s=books&qid=1256736664&sr=1-2) It indeed seems like the way to go, also in the wider context of the program. It makes much more sense to maintain one database table instead of 3 csv files for the three data typists' output. Alan: I forwarded your book to my office address. I'll print and read it! Btw, your private website is nice too. Nice pictures! Do you recognize where this was taken:http://yfrog.com/n0scotland046j .You're lucky to live in a beautiful place like Scotland Cheers!! Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ________________________________ From: Lee Harr To: tutor at python.org Sent: Sat, October 2, 2010 12:56:21 AM Subject: Re: [Tutor] (de)serialization questions >>> I have data about zip codes, street and city names (and perhaps later also of >>> street numbers). I made a dictionary of the form {zipcode: (street, city)} >> >> One dictionary with all of the data? >> >> That does not seem like it will work. What happens when >> 2 addresses have the same zip code? You did not answer this question. Did you think about it? > Maybe my main question is as follows: what permanent object is most suitable to > store a large amount of entries (maybe too many to fit into the computer's > memory), which can be looked up very fast. One thing about Python is that you don't normally need to think about how your objects are stored (memory management). It's an advantage in the normal case -- you just use the most convenient object, and if it's fast enough and small enough you're good to go. Of course, that means that if it is not fast enough, or not small enough, then you've got to do a bit more work to do. > Eventually, I want to create two objects: > 1-one to look up street name and city using zip code So... you want to have a function like: def addresses_by_zip(zipcode): '''returns list of all addresses in the given zipcode''' .... > 2-one to look up zip code using street name, apartment number and city and another one like: def zip_by_address(street_name, apt, city): '''returns the zipcode for the given street name, apartment, and city''' .... To me, it sounds like a job for a database (at least behind the scenes), but you could try just creating a custom Python object that holds these things: class Address(object): street_number = '345' street_name = 'Main St' apt = 'B' city = 'Springfield' zipcode = '99999' Then create another object that holds a collection of these addresses and has methods addresses_by_zip(self, zipcode) and zip_by_address(self, street_number, street_name, apt, city) > I stored object1 in a marshalled dictionary. Its length is about 450.000 (I >live > in Holland, not THAT many streets). Look-ups are incredibly fast (it has to, > because it's part of an autocompletion feature of a data entry program). I > haven't got the street number data needed for object2 yet, but it's going to be > much larger. Many streets have different zip codes for odd or even numbers, or > the zip codes are divided into street number ranges (for long streets). Remember that you don't want to try to optimize too soon. Build a simple working system and see what happens. If it is too slow or takes up too much memory, fix it. > You suggest to simply use a file. I like simple solutions, but doesn't that, by > definition, require a slow, linear search? You could create an index, but then any database will already have an indexing function built in. I'm not saying that rolling your own custom database is a bad idea, but if you are trying to get some work done (and not just playing around and learning Python) then it's probably better to use something that is already proven to work. If you have some code you are trying out, but are not sure you are going the right way, post it and let people take a look at it. _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From smokefloat at gmail.com Sun Oct 3 23:33:07 2010 From: smokefloat at gmail.com (David Hutto) Date: Sun, 3 Oct 2010 17:33:07 -0400 Subject: [Tutor] Matching relational data Message-ID: I'm creating an app that charts/graphs data. The mapping of the graphs is the 'easy' part with matplotlib, and wx. My question relates to the alignment of the data to be processed. Let's say I have three sets of 24 hr graphs with the same time steps: -the position of the sun -the temp. -local powerplant energy consumption A human could perceive the relations that when it's wintertime, cold and the sun goes down, heaters are turned on and energy consumption goes up, and the opposite in summer when it the sun comes up. My problem is how to compare and make the program perceive the relation. So, I think what I'm asking is if there is anything similar to this in use that anyone knows of, or if anyone has encountered a similar problem, and what approach they may have taken? TIA, David From webtourist at gmail.com Mon Oct 4 00:06:39 2010 From: webtourist at gmail.com (Robert) Date: Sun, 3 Oct 2010 18:06:39 -0400 Subject: [Tutor] Change to Class-level Variable Message-ID: Why is "f1" not affected by the Class-level variable change below ? >>> class Foo( object ): ... myid = 'Foo' ... def __init__( self ): ... pass ... >>> f1 = Foo() >>> f2 = Foo() >>> f1.myid = 'Bar' >>> Foo.myid = 'SPAM' >>> f1.myid <----------------------- Why is "f1" not affected by the Class variable change ? 'Bar' >>> f2.myid 'SPAM' >>> f4 = Foo() >>> f4.myid 'SPAM' >>> From steve at pearwood.info Mon Oct 4 00:33:24 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 4 Oct 2010 08:33:24 +1000 Subject: [Tutor] Change to Class-level Variable In-Reply-To: References: Message-ID: <201010040933.26537.steve@pearwood.info> On Mon, 4 Oct 2010 09:06:39 am Robert wrote: > Why is "f1" not affected by the Class-level variable change below ? The Python terminology is "attribute", not variable. You have class attributes and instance attributes. > >>> class Foo( object ): > ... myid = 'Foo' > ... def __init__( self ): > ... pass That __init__ method does nothing. Leave it out: class Foo(object): myid = 'Foo' > >>> f1 = Foo() > >>> f2 = Foo() > >>> f1.myid = 'Bar' This creates an instance attribute called myid which masks the class attribute. > >>> Foo.myid = 'SPAM' This directly changes the class attribute. Think of it this way: when you lookup an attribute, Python searches: * the instance * the class * any superclasses in that order, and returns the first matching value it finds. When you set an attribute, Python follows the same pattern, but naturally the first attempt (instance) will always succeed. (Note: this may not be what Python *actually* does, but conceptually it has always helped me reason about the behaviour.) Python won't let you accidentally modify a class attribute. You have to do it explicitly. Here are three ways to do it: type(f1).myid = 'Bar' f1.__class__.myid = 'Bar' Foo.myid = 'Bar' (You might be able to do some sort of metaclass magic to change this behaviour, but consider that *very* advanced.) -- Steven D'Aprano From steve at pearwood.info Mon Oct 4 00:37:57 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 4 Oct 2010 08:37:57 +1000 Subject: [Tutor] Matching relational data In-Reply-To: References: Message-ID: <201010040937.57674.steve@pearwood.info> On Mon, 4 Oct 2010 08:33:07 am David Hutto wrote: > I'm creating an app that charts/graphs data. The mapping of the > graphs is the 'easy' part with matplotlib, > and wx. My question relates to the alignment of the data to be > processed. > > Let's say I have three sets of 24 hr graphs with the same time steps: > > -the position of the sun > -the temp. > -local powerplant energy consumption > > > A human could perceive the relations that when it's wintertime, cold > and the sun goes down, heaters are turned on > and energy consumption goes up, and the opposite in summer when it > the sun comes up. > My problem is how to compare and make the program perceive the > relation. This is a statistics problem, not a programming problem. Or rather, parts of it *uses* programming to solve the statistics problem. My statistics isn't good enough to tell you how to find correlations between three independent variables, but I can break the problem into a simpler one: find the correlation between two variables, temperature and energy consumption. Without understanding how the data was generated, I'm not entirely sure how to set the data up, but here's one approach: (1) Plot the relationship between: x = temperature y = power consumption where x is the independent variable and y is the dependent variable. (2) Look at the graph. Can you see any obvious pattern? If all the data points are scattered randomly around the graph, there you can be fairly sure that there is no correlation and you can go straight on to calculating the correlation coefficient to make sure. (3) But if the graph clearly appears to be made of separate sections, AND those sections correlate to physical differences due to the time of day (position of the sun), then you need to break the data set into multiple data sets and work on each one individually. E.g. if the graph forms a straight line pointing DOWN for the hours 11pm to 5am, and a straight line pointing UP for the hours 5am to 11pm, and you can think of a physical reason why this is plausible, then you would be justified in separating out the data into two independent sets: 5am-11pm, 11pm-5am. If you want to have the program do this part for you, this is a VERY hard problem. You're essentially wanting to write an artifical intelligence system capable of picking out statistical correlations from data. Such software does exist. It tends to cost hundreds of thousands of dollars, or millions. Good luck writing your own! (4) Otherwise feel free to simplify the problem by just investigating the relationship between temperature and power consumption during (say) daylight hours. (5) However you decide to proceed, you should now have one (or more) x-y graph. First step is to decide whether there is any correlation at all. If there is not, you can stop there. Calculate the correlation coefficient, r. r will be a number between -1 and 1. r=1 means a perfect positive correlation; r=-1 means a perfect negative correlation. r=0 means no correlation at all. (6) Decide whether the correlation is meaningful. I don't remember how to do this -- consult your statistics text books. If it's not meaningful, then you are done -- there's no statistically valid relationship between the variables. (7) Otherwise, you want to calculate the line of best fit (or possibly some other curve, but let's stick to straight lines for now) for the data. The line of best fit may be complicated to calculate, and it may not be justified statistically, so start off with something simpler which (hopefully!) is nearly as good -- a linear regression line. This calculates a line that statistically matches your data. (8) Technically, you can calculate a regression line for *any* data, even if it clearly doesn't form a line. That's why you are checking the correlation coefficient to decide whether it is sensible or not. By now any *real* statisticians reading this will be horrified :) What I've described is essentially the most basic, "Stats 101 for Dummies" level. Have fun! -- Steven D'Aprano From steve at pearwood.info Mon Oct 4 00:47:11 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 4 Oct 2010 08:47:11 +1000 Subject: [Tutor] subprocess.call warning In-Reply-To: References: Message-ID: <201010040947.11832.steve@pearwood.info> On Mon, 4 Oct 2010 06:44:54 am Norman Khine wrote: > hello, from the docs http://docs.python.org/library/subprocess.html i > see there is a WARNING about deadlock when using the subprocess.call. > in my code i have this > > http://pastie.org/1197024 The warning says: Like Popen.wait(), this will deadlock when using stdout=PIPE and/or stderr=PIPE and the child process generates enough output to a pipe such that it blocks waiting for the OS pipe buffer to accept more data. Since you don't set either stdout or stderr to PIPE, you shouldn't have to worry about a deadlock. > the first calls the 'sox' library which joins all the .wav files into > one file and then i use the 'wav2swf' library to create a SWF output > of the file. > > can the code be improved? Code can always be improved :-) What do you consider an improvement? Easy to maintain in the future? Faster? Smaller? What does get_abspath do? It sounds like it *may* be your own version of os.path.abspath. I'm not sure why you have a list called "imgtext" that doesn't contain text. It seems to be a list of partial file names rather than "image text". Your code, as given, can't work, because you call isupper() on integers 1 2 and 3. That will fail. I'd probably prefer to generate the filenames differently: def fix_filename(fname, base=sound_path): fname = str(fname) if fname.isupper(): fname = "upper_%s" % fname.lower() return os.path.join(base, fname + '.wav') and then call it: filenames = ['A', 'b', 'c', 'D', 1, 2, 3] for name in filenames: name = fix_filename(name) sox_filenames.append(name) -- Steven D'Aprano From g.nius.ck at gmail.com Mon Oct 4 01:20:15 2010 From: g.nius.ck at gmail.com (Chris King) Date: Sun, 03 Oct 2010 19:20:15 -0400 Subject: [Tutor] Networking In-Reply-To: <33D037A9-0230-42B3-9AFC-A1884F00AE88@gmail.com> References: <4CA788E2.4060006@gmail.com> <33D037A9-0230-42B3-9AFC-A1884F00AE88@gmail.com> Message-ID: <4CA90FAF.1070002@gmail.com> On 10/2/2010 3:40 PM, Evert Rol wrote: >> Dear Tutors, >> I have attached my 2 programs for networking. It uses socket and SocketServer, but it just simplifies it even more. The problem is it won't work. The Client raises the error, (with trace back) >> Traceback (most recent call last): >> File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 34, in >> print client.recv() >> File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 16, in recv >> return self.__sock.recv(1024) >> error: [Errno 10053] An established connection was aborted by the software in your host machine >> The server seems to get an error of some sort, but also seems to catch and print it. It prints >> ---------------------------------------- >> Exception happened during processing of request from ('127.0.0.1', 1424) >> Traceback (most recent call last): >> File "C:\Python26\lib\SocketServer.py", line 281, in _handle_request_noblock >> self.process_request(request, client_address) >> File "C:\Python26\lib\SocketServer.py", line 307, in process_request >> self.finish_request(request, client_address) >> File "C:\Python26\lib\SocketServer.py", line 320, in finish_request >> self.RequestHandlerClass(request, client_address, self) >> TypeError: 'NoneType' object is not callable >> ---------------------------------------- >> I look at both of the documentations of socket and SocketServer, but I couldn't firgue it out. I don't know much about networking. Please Help > I don't know much about networking, less so about it on Windows; also, I've only scanned quickly through the server script, but I notice your create_handler() function doesn't return anything. I guess it needs to return the Handler class. > The example in the Python docs doesn't use a factory function, but instead directly puts the class as the second argument to TCPServer. > So the second argument needs to be a class, but since your create_handler() function returns nothing, you presumably get this NoneType exception. > > Evert > Yeah, that could be a problem. It should return the class. From alan.gauld at btinternet.com Mon Oct 4 01:46:27 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 4 Oct 2010 00:46:27 +0100 Subject: [Tutor] (de)serialization questions References: <828487.45552.qm@web110712.mail.gq1.yahoo.com> Message-ID: "Albert-Jan Roskam" wrote > It makes much more sense to maintain one database table instead of 3 > csv files > for the three data typists' output. To be pedantic you will probably want several tables but they will all be in one file... :-) > Btw, your private website is nice too. Nice pictures! Well done, not many people notice that section :-) I keep meaning to add another dozen or so pages, but finding time.... > Do you recognize where this was taken: > http://yfrog.com/n0scotland046j . Could be any of a dozen places but if pushed I'll guess the Rannoch Moor. Maybe the top end of Glencoe? But the layer of low cloud wipes out too much to be sure. > You're lucky to live in a beautiful place like Scotland I think so :-) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Mon Oct 4 01:52:02 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 4 Oct 2010 00:52:02 +0100 Subject: [Tutor] Matching relational data References: Message-ID: "David Hutto" wrote > My problem is how to compare and make the program perceive the > relation. So, I think what I'm asking is if there is anything > similar > to this in use that anyone knows of, or if anyone has encountered a > similar > problem, and what approach they may have taken? Its a stats problem. One of the best programming stats tools is R http://en.wikipedia.org/wiki/R_%28programming_language%29 There is a python bionding for R too. Although R may be overkill for what you want, but at least you'll know the theory and math are correct! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Mon Oct 4 01:58:18 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 4 Oct 2010 00:58:18 +0100 Subject: [Tutor] Change to Class-level Variable References: Message-ID: "Robert" wrote >>>> class Foo( object ): > ... myid = 'Foo' > ... def __init__( self ): > ... pass > ... >>>> f1 = Foo() >>>> f2 = Foo() >>>> f1.myid = 'Bar' This creates a myid instance variable in f1 which hides the class variable. You should always modify class variables via the class not an instance (you can read them either way but I prefer to use the class for both read and write) >>>> Foo.myid = 'SPAM' >>>> f1.myid <----------------------- Why is "f1" not affected >>>> by the Class variable change ? > 'Bar' Because this is accessing the instance variable not the class one. You can get at the class variable from the instance usind __class__ but usually you don'tneed to. You know the class in context or you want the instance data not the class data. >>> f1.__class__.myid 'SPAM' HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From smokefloat at gmail.com Mon Oct 4 04:20:35 2010 From: smokefloat at gmail.com (David Hutto) Date: Sun, 3 Oct 2010 22:20:35 -0400 Subject: [Tutor] Matching relational data In-Reply-To: <201010040937.57674.steve@pearwood.info> References: <201010040937.57674.steve@pearwood.info> Message-ID: On Sun, Oct 3, 2010 at 6:37 PM, Steven D'Aprano wrote: > On Mon, 4 Oct 2010 08:33:07 am David Hutto wrote: >> I'm creating an app that charts/graphs data. The mapping of the >> graphs is the 'easy' part with matplotlib, >> and wx. My question relates to the alignment of the data to be >> processed. >> >> Let's say I have three sets of 24 hr graphs with the same time steps: >> >> -the position of the sun >> -the temp. >> -local powerplant energy consumption >> >> >> A human could perceive the relations that when it's wintertime, cold >> and the sun goes down, heaters are turned on >> and energy consumption goes up, and the opposite in summer when it >> the sun comes up. >> My problem is how to compare and make the program perceive the >> relation. > > This is a statistics problem, not a programming problem. Or rather, > parts of it *uses* programming to solve the statistics problem. > > My statistics isn't good enough to tell you how to find correlations > between three independent variables, but I can break the problem into a > simpler one: find the correlation between two variables, temperature > and energy consumption. This was the initial starting point, but I thought that the comparing multiples should set the tone for how the data is interpreted, but you're right, I should start with two, and then give relation to relation within the 2 object compared structure. So if x and y are compared and related, then it makes since that if x and b are compared and related, that b and y are related in some way because they have a in common in terms of 2 object comparison relationals. or: (see below for comparative % based statistic analysis algorithm) x and y = related x and b = related eg. y and b = related but that gives the origin and the end comparison paradox of my end desires for the program. Do I compare the end object to all or do random 2 coordinate list comparisons and match the data over corresponding timesteps, then eliminate the list of comparable based on a hierarchy of matches, in other words? if x relates to y and x relates to b: So I have(really rough pseudo code for time constraints): list1 = [+,+,+,+,-,+,-,+,-,+] list2 = [-,+,+,+,-,+,-,+,-,+] Above I have a 90% match to timestep increments/decrements, that over say, one minute periods, x and y both increased or decreased together 90% of the time, or the opposite, that they diverged 90% of the time. > > Without understanding how the data was generated, I'm not entirely sure > how to set the data up, but here's one approach: > > (1) Plot the relationship between: > ? ?x = temperature > ? ?y = power consumption > > ? ?where x is the independent variable and y is the dependent variable. > > (2) Look at the graph. Can you see any obvious pattern? If all the data > ? ?points are scattered randomly around the graph, there you can be > ? ?fairly sure that there is no correlation and you can go straight on > ? ?to calculating the correlation coefficient to make sure. > > (3) But if the graph clearly appears to be made of separate sections, > ? ?AND those sections correlate to physical differences due to the time > ? ?of day (position of the sun), then you need to break the data set > ? ?into multiple data sets and work on each one individually. > > ? ?E.g. if the graph forms a straight line pointing DOWN for the hours > ? ?11pm to 5am, and a straight line pointing UP for the hours 5am > ? ?to 11pm, and you can think of a physical reason why this is > ? ?plausible, then you would be justified in separating out the data > ? ?into two independent sets: 5am-11pm, 11pm-5am. > > ? ?If you want to have the program do this part for you, this is a VERY > ? ?hard problem. You're essentially wanting to write an artifical > ? ?intelligence system capable of picking out statistical correlations > ? ?from data. Such software does exist. It tends to cost hundreds of > ? ?thousands of dollars, or millions. Good luck writing your own! > > (4) Otherwise feel free to simplify the problem by just investigating > ? ?the relationship between temperature and power consumption during > ? ?(say) daylight hours. > > (5) However you decide to proceed, you should now have one (or more) x-y > ? ?graph. First step is to decide whether there is any correlation at > ? ?all. If there is not, you can stop there. Calculate the correlation > ? ?coefficient, r. r will be a number between -1 and 1. r=1 means a > ? ?perfect positive correlation; r=-1 means a perfect negative > ? ?correlation. r=0 means no correlation at all. > > (6) Decide whether the correlation is meaningful. I don't remember how > ? ?to do this -- consult your statistics text books. If it's not > ? ?meaningful, then you are done -- there's no statistically valid > ? ?relationship between the variables. > > (7) Otherwise, you want to calculate the line of best fit (or possibly > ? ?some other curve, but let's stick to straight lines for now) for the > ? ?data. The line of best fit may be complicated to calculate, and it > ? ?may not be justified statistically, so start off with something > ? ?simpler which (hopefully!) is nearly as good -- a linear regression > ? ?line. This calculates a line that statistically matches your data. > > (8) Technically, you can calculate a regression line for *any* data, > ? ?even if it clearly doesn't form a line. That's why you are checking > ? ?the correlation coefficient to decide whether it is sensible or not. > > > By now any *real* statisticians reading this will be horrified :) What > I've described is essentially the most basic, "Stats 101 for Dummies" > level. > > Have fun! > > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From ajarncolin at gmail.com Mon Oct 4 08:26:43 2010 From: ajarncolin at gmail.com (col speed) Date: Mon, 4 Oct 2010 13:26:43 +0700 Subject: [Tutor] Pythonic nested lists Message-ID: Message: 6 Date: Tue, 28 Sep 2010 13:15:26 +0700 From: col speed To: tutor at python.org Subject: [Tutor] Pythonic nested lists Message-ID: Content-Type: text/plain; charset="iso-8859-1" Hi all, I've been trying to write a programme that solves sudoku problems for a while now. I'm getting close, but would like to ask a few questions about the most pythonic way of doing some things. I've decided to create nested lists (row order, column order and square order) which correspond with dictionary keys - the values of which are the numbers given in the puzzle - so I can loop through the dict in different orders. I think the first two are OK, but the square order list seems extremely messy and I would love some pointers. What I have is as follows: roworder = [[str(j)+str(i) for i in range(9)] for j in range(9)] colorder = zip(*roworder) #here comes the problem! start, index = 0,0 sqorder = [] for i in range(9): sqorder.append([]) for j in range(start, start+3): sqorder[i].extend(roworder[j][index:index+3]) if index == 6: index = 0 else: index += 3 if i == 2 or i == 5: start += 3 Any comments at all would be gratefully received. Thanks in advance Colin _______________________________________________________________________ HI again, I realise that I should have included more information in the above e-mail. Here goes: a/ I have a 9*9 nested list called "rows", which contains the given numbers in a sudoku puzzle - with "0"s where the blanks go. b/ I create roworder (roworder = [[str(j)+str(i) for i in range(9)] for j in range(9)]) - [["00" --> "08"], ["10" --> "18"] --> ["80" --> "88"]] c/ I populate a dictionary ("dic") with keys from "roworder" and values from "rows" d/ I loop through the dict, changing any value"0" to be set(range(1, 10))- a list of "possible numbers". e/ Then I do: for i in roworder: notPoss = set([dic[j] for j in i if type(dic[j]) == int]) for k in j: if type(dic[k]) == set: dic[k].difference_update(notPoss) thus removing the numbers that exist in the row from the "possible numbers" I need to do this for the columns and squares aswell, which is why I do: colorder = zip(*roworder) - (create a nested list "column wise" rather than "row wise") and want to create a "square order" list - which is what the above mess does. I hope this clarifies things. Thanks again Colin -------------- next part -------------- An HTML attachment was scrubbed... URL: From norman at khine.net Mon Oct 4 08:42:16 2010 From: norman at khine.net (Norman Khine) Date: Mon, 4 Oct 2010 08:42:16 +0200 Subject: [Tutor] subprocess.call warning In-Reply-To: <201010040947.11832.steve@pearwood.info> References: <201010040947.11832.steve@pearwood.info> Message-ID: thank you for the reply On Mon, Oct 4, 2010 at 12:47 AM, Steven D'Aprano wrote: > On Mon, 4 Oct 2010 06:44:54 am Norman Khine wrote: >> hello, from the docs http://docs.python.org/library/subprocess.html i >> see there is a WARNING about deadlock when using the subprocess.call. >> in my code i have this >> >> http://pastie.org/1197024 > > > The warning says: > > ? ?Like Popen.wait(), this will deadlock when using stdout=PIPE > ? ?and/or stderr=PIPE and the child process generates enough > ? ?output to a pipe such that it blocks waiting for the OS pipe > ? ?buffer to accept more data. > > Since you don't set either stdout or stderr to PIPE, you shouldn't have > to worry about a deadlock. ah ok > > >> the first calls the 'sox' library which joins all the .wav files into >> one file and then i use the 'wav2swf' library to create a SWF output >> of the file. >> >> can the code be improved? > > Code can always be improved :-) What do you consider an improvement? > Easy to maintain in the future? Faster? Smaller? to make it faster. > > What does get_abspath do? It sounds like it *may* be your own version of > os.path.abspath. i am using the iTools python library http://docs.hforge.org/itools/web.html#public-api http://git.hforge.org/?p=itools.git;a=blob;f=core/utils.py > > I'm not sure why you have a list called "imgtext" that doesn't contain > text. It seems to be a list of partial file names rather than "image > text". the code is for my own implementation of a captcha to see how python interacts with system libraries such as SOX and WAV2SWF and of course to learn. http://pastie.org/1197919 but yes, it is 'text' so i will change it in the next version. > > Your code, as given, can't work, because you call isupper() on integers > 1 2 and 3. That will fail. > > I'd probably prefer to generate the filenames differently: > > def fix_filename(fname, base=sound_path): > ? ?fname = str(fname) > ? ?if fname.isupper(): > ? ? ? ?fname = "upper_%s" % fname.lower() > ? ?return os.path.join(base, fname + '.wav') > > and then call it: > > filenames = ['A', 'b', 'c', 'D', 1, 2, 3] > for name in filenames: > ? ?name = fix_filename(name) > ? ?sox_filenames.append(name) > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- ?u?op ?p?sdn p,u?n? p??o? ??? ??s no? '?u???? s???? ??? pu? '?u??uo? ?q s,??? ??? %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From alan.gauld at btinternet.com Mon Oct 4 09:17:04 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 4 Oct 2010 08:17:04 +0100 Subject: [Tutor] Pythonic nested lists References: Message-ID: "col speed" wrote > > HI again, I realise that I should have included more information in > the > above e-mail. Here goes: > > a/ I have a 9*9 nested list called "rows", which contains the given > numbers > in a sudoku puzzle - with "0"s where the blanks go. > > b/ I create roworder (roworder = [[str(j)+str(i) for i in range(9)] > for j in > range(9)]) - [["00" --> "08"], ["10" --> "18"] --> ["80" --> > "88"]] > > c/ I populate a dictionary ("dic") with keys from "roworder" and > values from > "rows" > > d/ I loop through the dict, changing any value"0" to be set(range(1, > 10))- a > list of "possible numbers". > > e/ Then I do: > for i in roworder: > notPoss = set([dic[j] for j in i if type(dic[j]) == int]) > for k in j: > if type(dic[k]) == set: > dic[k].difference_update(notPoss) > > thus removing the numbers that exist in the row from the "possible > numbers" > > I need to do this for the columns and squares aswell, which is why I > do: That approach should work. Personally I'd probably create a Square class and bae my data on 9 squares. Each square can return a row(of 3 items) given an index and a column(of 3 items) given an index. class Square: def __init__(self, size=3): self.cells = [0 for n in range(size*size)] ... def __str__(self):... def getRow(self,n): ... def getCol(self,n):... def setCell(self,x,y,value) def check(self):... Thus I'd build my rows and columns dynamically from the squares rather than the other way round. It just feels more natural to me. But your approach should work too. However I confess I haven't studied you code in detail. Is there any specific problem or question you have or are you looking for general feedback? HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From fomcl at yahoo.com Mon Oct 4 11:03:37 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Mon, 4 Oct 2010 02:03:37 -0700 (PDT) Subject: [Tutor] (de)serialization questions In-Reply-To: Message-ID: <778702.79399.qm@web110710.mail.gq1.yahoo.com> Hi Alan, Wow, I'm impressed! You're right, it's near Glencoe! Coming from Edingburgh it's a couple of miles just before Glencoe. Marvellous place! Cheers!! Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --- On Mon, 10/4/10, Alan Gauld wrote: From: Alan Gauld Subject: Re: [Tutor] (de)serialization questions To: tutor at python.org Date: Monday, October 4, 2010, 1:46 AM "Albert-Jan Roskam" wrote > It makes much more sense to maintain one database table instead of 3 csv files > for the three data typists' output. To be pedantic you will probably want several tables but they will all be in one file... :-) > Btw, your private website is nice too. Nice pictures! Well done, not many people notice that section :-) I keep meaning to add another dozen or so pages, but finding time.... > Do you recognize where this was taken: > http://yfrog.com/n0scotland046j . Could be any of a dozen places but if pushed I'll guess the Rannoch Moor. Maybe the top end of Glencoe? But the layer of low cloud wipes out too much to be sure. > You're lucky to live in a beautiful place like Scotland I think so :-) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From susana.delgado_s at utzmg.edu.mx Mon Oct 4 16:04:19 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Mon, 4 Oct 2010 09:04:19 -0500 Subject: [Tutor] EXECUTING PYTHON AND SQL STAMENTS Message-ID: I'm developing a module to execute an external command. The module executes the command, but in order to make my code useful I have to enter some sql staments. This is my code: from dbf import * from osgeo import ogr import os import sys def call(): print "Ingresa el nombre para el nuevo mapa" arg1 = "R1G-GEODESIA2.shp" print arg1 print "Ingresa la condicion" arg2 = "LAYER = 'R1G-GEODESIA'" print arg2 print "Ingresa el nombre del mapa original" arg3 = 'C:/Python26/tapalpa_05_plani_point.shp' print arg3 os.system('"C:/Archivos de programa/FWTools2.4.7/setfw"') os.system('"C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr.exe"'+" " +"arg1" +" "+ "-where" +" "+ "arg2" +" " +"arg3") call() The problem is that when I run the module it throws the error: Unable to open datasource`arg3' with the following drivers. ESRI Shapefile MapInfo File UK .NTFSDTS TIGER S57 DGN VRT REC Memory BNA CSV NAS GML GPX KML GeoJSON Interlis 1 Interlis 2 GMT SQLite ODBC PGeo OGDI PostgreSQL MySQL XPlane AVCBin AVCE00 DXF Geoconcept GeoRSS GPSTrackMaker VFK Can you help me please? -------------- next part -------------- An HTML attachment was scrubbed... URL: From norman at khine.net Mon Oct 4 16:32:12 2010 From: norman at khine.net (Norman Khine) Date: Mon, 4 Oct 2010 16:32:12 +0200 Subject: [Tutor] EXECUTING PYTHON AND SQL STAMENTS In-Reply-To: References: Message-ID: On Mon, Oct 4, 2010 at 4:04 PM, Susana Iraiis Delgado Rodriguez wrote: > I'm developing a module to execute an external command. The module executes > the command, but in order to make my code useful I have to enter some sql > staments. This is my code: > from dbf import * > from osgeo import ogr > import os > import sys > def call(): > ????? print "Ingresa el nombre para el nuevo mapa" > ????? arg1 = "R1G-GEODESIA2.shp" > ????? print arg1 > ????? print "Ingresa la condicion" > ????? arg2 = "LAYER = 'R1G-GEODESIA'" > ????? print arg2 > ????? print "Ingresa el nombre del mapa original" > ????? arg3 = 'C:/Python26/tapalpa_05_plani_point.shp' > ????? print arg3 > ????? os.system('"C:/Archivos de programa/FWTools2.4.7/setfw"') > ????? os.system('"C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr.exe"'+" " > +"arg1" +" "+ "-where" +" "+ "arg2" +" " +"arg3") > call() > The?problem?is that when I run the module it throws the error: > Unable to open datasource`arg3' with the following drivers. maybe the path to arg3 is incorrect? did arg1 shapefile load without a problem? > ESRI Shapefile > MapInfo File > UK .NTFSDTS > TIGER > S57 > DGN > VRT > REC > Memory > BNA > CSV > NAS > GML > GPX > KML > GeoJSON > Interlis 1 > Interlis 2 > GMT > SQLite > ODBC > PGeo > OGDI > PostgreSQL > MySQL > XPlane > AVCBin > AVCE00 > DXF > Geoconcept > GeoRSS > GPSTrackMaker > VFK > Can you help me please? > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- ?u?op ?p?sdn p,u?n? p??o? ??? ??s no? '?u???? s???? ??? pu? '?u??uo? ?q s,??? ??? %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From steve at alchemy.com Mon Oct 4 16:42:20 2010 From: steve at alchemy.com (Steve Willoughby) Date: Mon, 04 Oct 2010 07:42:20 -0700 Subject: [Tutor] EXECUTING PYTHON AND SQL STAMENTS In-Reply-To: References: Message-ID: <4CA9E7CC.8040004@alchemy.com> On 04-Oct-10 07:32, Norman Khine wrote: > On Mon, Oct 4, 2010 at 4:04 PM, Susana Iraiis Delgado Rodriguez > wrote: >> os.system('"C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr.exe"'+" " >> +"arg1" +" "+ "-where" +" "+ "arg2" +" " +"arg3") You're including the text "arg1" in the command, not the value of the arg1 variable. (Likewise for arg2 and arg3). There are some unnecessary extra strings in there too. Your os.system() call, slightly simplified, is: os.system('"C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr.exe "+"arg1" +" -where "+"arg2" +" " +"arg3") but what I think you meant was os.system('"C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr.exe "+arg1 +" -where "+arg2 +" " +arg3) There are even better ways to accomplish this task too, but I'm focusing on what looks like you ran into with this first. From smokefloat at gmail.com Mon Oct 4 17:04:09 2010 From: smokefloat at gmail.com (David Hutto) Date: Mon, 4 Oct 2010 11:04:09 -0400 Subject: [Tutor] Matching relational data In-Reply-To: References: Message-ID: Thanks for your replies, and reminding me of statistics(probably a quick crash course). The data I plan to use would be equally charted by a constant timestep in it's movement(type of movement graphed being dictated by the object being measured) So I thought that maybe the easiest way would be something like the following: a = ['+','-','+','-','+','-','+','-','+','-','+'] b = ['-','+','-','+','-','-','-','+','-','-','-'] count = 0 lena = len(a) lenb = len(b) if lena == lenb: for num in range(0,lena): if a[num] == b[num]: print 'match' count += 1 else: print 'Lists are not same length for comparison' per = (100/lena) print count * per, '% match' The plus and minus signs would represent the movement over an equal amount of time(say one minute/hour) Although I'm going to the statistics for the rest of this, anyone who has an improved solution let me know. Thanks, David From steve at pearwood.info Mon Oct 4 18:08:17 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 5 Oct 2010 03:08:17 +1100 Subject: [Tutor] Matching relational data In-Reply-To: References: Message-ID: <201010050308.18000.steve@pearwood.info> On Tue, 5 Oct 2010 02:04:09 am David Hutto wrote: > a = ['+','-','+','-','+','-','+','-','+','-','+'] > b = ['-','+','-','+','-','-','-','+','-','-','-'] > > count = 0 > lena = len(a) > lenb = len(b) > if lena == lenb: > for num in range(0,lena): > if a[num] == b[num]: > print 'match' > count += 1 > else: > print 'Lists are not same length for comparison' > per = (100/lena) > print count * per, '% match' a = '+-+-+-+-+-+' b = '-+-+---+---' if len(a) != len(b): raise ValueError('lists are not the same length') count = sum(ca == cb for (ca, cb) in zip(a, b)) -- Steven D'Aprano From wallenpb at gmail.com Mon Oct 4 19:56:12 2010 From: wallenpb at gmail.com (Bill Allen) Date: Mon, 4 Oct 2010 12:56:12 -0500 Subject: [Tutor] EXECUTING PYTHON AND SQL STAMENTS In-Reply-To: References: Message-ID: On Mon, Oct 4, 2010 at 9:04 AM, Susana Iraiis Delgado Rodriguez < susana.delgado_s at utzmg.edu.mx> wrote: > I'm developing a module to execute an external command. The module executes > the command, but in order to make my code useful I have to enter some sql > staments. This is my code: > Question, what database system are you trying to access? Python, via its standard library, and also by easily available 3rd party libraries. has ample facilities to allow for database access without having to do external OS calls. You may have a perfectly valid reason for designing your code the way you have, but I thought this was worth asking. --Bill > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Tue Oct 5 00:31:46 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 5 Oct 2010 09:31:46 +1100 Subject: [Tutor] Matching relational data In-Reply-To: References: Message-ID: <201010050931.47364.steve@pearwood.info> On Mon, 4 Oct 2010 10:52:02 am Alan Gauld wrote: > One of the best programming stats tools is R > > http://en.wikipedia.org/wiki/R_%28programming_language%29 > > There is a python bionding for R too. > > Although R may be overkill for what you want, but at least > you'll know the theory and math are correct! Or you could use numpy and scipy, which are rapidly becoming the choice for numeric and scientific applications over R. -- Steven D'Aprano From steve at pearwood.info Tue Oct 5 00:40:19 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 5 Oct 2010 09:40:19 +1100 Subject: [Tutor] subprocess.call warning In-Reply-To: References: <201010040947.11832.steve@pearwood.info> Message-ID: <201010050940.19687.steve@pearwood.info> On Mon, 4 Oct 2010 05:42:16 pm Norman Khine wrote: > >> the first calls the 'sox' library which joins all the .wav files > >> into one file and then i use the 'wav2swf' library to create a SWF > >> output of the file. > >> > >> can the code be improved? > > > > Code can always be improved :-) What do you consider an > > improvement? Easy to maintain in the future? Faster? Smaller? > > to make it faster. Faster??? Given that you are manipulating multiple wav files and joining them together into a swf file, I can't believe that you seriously believe that the Python code is likely to be the bottleneck. If it takes ten seconds to process the wav files into a swf file, who cares if you speed the Python code up from 0.02 seconds to 0.01 seconds? But I could be wrong... I look forward to seeing the profiling results from your code. -- Steven D'Aprano From crusier at gmail.com Tue Oct 5 04:01:07 2010 From: crusier at gmail.com (Crusier) Date: Tue, 5 Oct 2010 10:01:07 +0800 Subject: [Tutor] Downloading data from web Message-ID: I am trying to extract web data and put it into a database for analysis.I am just wondering what is the best way to do it and I will try to dig up information from there. Please help me how can I kick start this project. Cheers, Hank From smokefloat at gmail.com Tue Oct 5 05:00:33 2010 From: smokefloat at gmail.com (David Hutto) Date: Mon, 4 Oct 2010 23:00:33 -0400 Subject: [Tutor] Matching relational data In-Reply-To: <201010050931.47364.steve@pearwood.info> References: <201010050931.47364.steve@pearwood.info> Message-ID: On Mon, Oct 4, 2010 at 6:31 PM, Steven D'Aprano wrote: > On Mon, 4 Oct 2010 10:52:02 am Alan Gauld wrote: > >> One of the best programming stats tools is R >> >> http://en.wikipedia.org/wiki/R_%28programming_language%29 >> >> There is a python bionding for R too. >> >> Although R may be overkill for what you want, but at least >> you'll know the theory and math are correct! > > Or you could use numpy and scipy, which are rapidly becoming the choice > for numeric and scientific applications over R. > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > I'm amazed that you didn't catch the fact that it doesn't report that above fifty is a type of correlation and below is a match of correlation as well. Divergence is a pattern as well. Golly why didn't you pick up on that buddy, pal? Although I like the improved you gave, would you agree that readability would be better inclined to additions due to readability at an elementary level of python, but something a statistician could add on too, with limited python experience, but still contribute to the code? Seriously OT though, I ask in the same respect that i want the 'purer' aspect to review, but deny it in the aspect that it does hinder the open source mentality(from what I can see): but here's a more refined correlation pattern to review, in the thought process tha I can make million dollar oftware in the privacy of my home, and more importantly by 'myself'(with a little help from my friends) 50 % < begins to match mergence 50% > begins to match divergence 0 = matches perfect divergence 100 = matches perfect mergence From emile at fenx.com Tue Oct 5 05:31:36 2010 From: emile at fenx.com (Emile van Sebille) Date: Mon, 04 Oct 2010 20:31:36 -0700 Subject: [Tutor] Downloading data from web In-Reply-To: References: Message-ID: On 10/4/2010 7:01 PM Crusier said... > I am trying to extract web data and put it into a database for > analysis.I am just wondering what is the best way to do it and I will > try to dig up information from there. > > Please help me how can I kick start this project. Check out beautiful soup. http://www.crummy.com/software/BeautifulSoup/ Emile From kaushalshriyan at gmail.com Tue Oct 5 12:54:42 2010 From: kaushalshriyan at gmail.com (Kaushal Shriyan) Date: Tue, 5 Oct 2010 16:24:42 +0530 Subject: [Tutor] perl or python Message-ID: Hi Is it better to learn Perl or Python since i can manage only writing simple bash shell scripts. Please suggest/guide. Thanks and Regards Kaushal From steve at pearwood.info Tue Oct 5 13:18:20 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 5 Oct 2010 22:18:20 +1100 Subject: [Tutor] perl or python In-Reply-To: References: Message-ID: <201010052218.21154.steve@pearwood.info> On Tue, 5 Oct 2010 09:54:42 pm Kaushal Shriyan wrote: > Hi > > Is it better to learn Perl or Python since i can manage only writing > simple bash shell scripts. > Please suggest/guide. This is a mailing list for Python, filled with people who like and use Python. What do you think we're going to recommend? These resources try to be even-handed. They might help: http://www.python.org/doc/essays/comparisons.html http://danvk.org/josephus.html http://wiki.python.org/moin/LanguageComparisons And here is an article by the Perl hacker and programming guru Eric Raymond: http://www.linuxjournal.com/article/3882 -- Steven D'Aprano From susana.delgado_s at utzmg.edu.mx Tue Oct 5 15:50:22 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Tue, 5 Oct 2010 08:50:22 -0500 Subject: [Tutor] EXECUTING PYTHON AND SQL STAMENTS Message-ID: Hello Norman: Thank you for taking the time to answer. I already changed my os.system() for your code. I got an error, when I executed this: os.system(" 'C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr.exe ' "+arg1 +" -where "+arg2 +" " +arg3) it throws me that "C:/Archivos" is not recognized as an executable external or internal command, programm or file. If you really have other opton to fix my problem I'll be thankful because I don't have any idea to make this code useful. Thank you -------------- next part -------------- An HTML attachment was scrubbed... URL: From susana.delgado_s at utzmg.edu.mx Tue Oct 5 17:57:03 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Tue, 5 Oct 2010 10:57:03 -0500 Subject: [Tutor] EXECUTING PYTHON AND SQL STAMENTS In-Reply-To: References: Message-ID: Hello, I already solved the problem, I change all the code, instead of using os.system I changed to subprocess.Popen() and it worked fine: import shlex, subprocess def process(): print "Ingresa en el siguiente orden:" print "Nombre del nuevo mapa.shp Nombre de la capa Nombre del mapa original" command_line = raw_input() args = shlex.split(command_line) p = subprocess.Popen(['C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr', args[0], '-where', args[1], args[2]]) if p: print "Mapa generado" process() Now the user has to enter 3 arguments an finally it worked. I have a question, how can I tell the user if p execute ok? because even thouhg I entered wrong parameters, it prints "Mapa generado". This line should only appears if the arguments are acceptable. 2010/10/5 Susana Iraiis Delgado Rodriguez > Hello Norman: > > Thank you for taking the time to answer. I already changed my os.system() > for your code. I got an error, when I executed this: > os.system(" 'C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr.exe ' > "+arg1 +" -where "+arg2 +" " +arg3) > it throws me that "C:/Archivos" is not recognized as an executable > external or internal command, programm or file. > If you really have other opton to fix my problem I'll be thankful because I > don't have any idea to make this code useful. > Thank you > -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Tue Oct 5 18:16:51 2010 From: wprins at gmail.com (Walter Prins) Date: Tue, 5 Oct 2010 17:16:51 +0100 Subject: [Tutor] EXECUTING PYTHON AND SQL STAMENTS In-Reply-To: References: Message-ID: Thank you for taking the time to answer. I already changed my os.system() for your code. I got an error, when I executed this: > os.system(" 'C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr.exe ' "+arg1 > +" -where "+arg2 +" " +arg3) > it throws me that "C:/Archivos" is not recognized as an executable > external or internal command, programm or file. > If you really have other opton to fix my problem I'll be thankful because I > don't have any idea to make this code useful. > > The error message suggests the OS is seeing "C:/Archivos" as the command, as opposed to the entire path to ogr2ogr.exe, which implies some quoting issue/quotes being stripped off/lost somewhere along the line. Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Oct 5 20:22:08 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 5 Oct 2010 19:22:08 +0100 Subject: [Tutor] EXECUTING PYTHON AND SQL STAMENTS References: Message-ID: "Susana Iraiis Delgado Rodriguez" wrote > it throws me that "C:/Archivos" is not recognized as an executable > external > or internal command, programm or file. You can only use / in paths used by Python. You are passing this to the CMD processor via os.system so CMD complains. It expects / to indicate a command option (/? for example) You need to use \ in paths passed to CMD. HTH, Alan G. PS I just noticed you switched to subprocess so this is now somewhat academic! From walksloud at gmail.com Tue Oct 5 21:52:01 2010 From: walksloud at gmail.com (Andre' Walker-Loud) Date: Tue, 5 Oct 2010 12:52:01 -0700 Subject: [Tutor] specifying precision with scientific notation Message-ID: Hi All, I want to print scientific numbers with a specified number of decimal places. However, I want the number printed to be dynamically determined by the data. Example: > a = 0.00762921383941 > ea = 0.000830132912068 > a / ea 9.190352205653852 By default, I will print the uncertainty ("ea") with two significant digits. In this example, the central value is about 10 times larger than the uncertainty, so I want to print it with 3 significant figures. So I want to do something like > p = int(("%.1e" % (a / ea)).split('e')[-1]) # grab the power in the scientific notation (is there a better way?) > if p >= 0: > print('%.' + str(int(2+p)) +'e +- %.1e' % (a, ea)) > else: print('%.2e +- %.1e' % (a, ea)) (desired output): 7.63e-03 +- 8.3e-04 but this fails. And I haven't figured out how to get this to work. Seems like it should be simple. Any help? Thanks, Andre From evert.rol at gmail.com Tue Oct 5 22:15:00 2010 From: evert.rol at gmail.com (Evert Rol) Date: Tue, 5 Oct 2010 22:15:00 +0200 Subject: [Tutor] specifying precision with scientific notation In-Reply-To: References: Message-ID: > I want to print scientific numbers with a specified number of decimal places. However, I want the number printed to be dynamically determined by the data. Example: > >> a = 0.00762921383941 >> ea = 0.000830132912068 >> a / ea > 9.190352205653852 > > By default, I will print the uncertainty ("ea") with two significant digits. In this example, the central value is about 10 times larger than the uncertainty, so I want to print it with 3 significant figures. Note that 'specified number of decimal places' != 'number of significant digits'. But it's fairly obvious you mean significant digits here. But that aside, perhaps Python's decimal module can help you to ease things: http://docs.python.org/library/decimal.html > So I want to do something like > >> p = int(("%.1e" % (a / ea)).split('e')[-1]) # grab the power in the scientific notation (is there a better way?) >> if p >= 0: >> print('%.' + str(int(2+p)) +'e +- %.1e' % (a, ea)) >> else: > print('%.2e +- %.1e' % (a, ea)) > > (desired output): 7.63e-03 +- 8.3e-04 Personally, I would print this as 7.63e-03 +- 0.83e-03, which shows the precision a bit better. But that's just a matter of aesthetics, and would make things even more complicated (then again, since you are printing numbers and formatting them, you are concerned about aesthetics). But if the decimal module can't make it easier for you, I don't really think there's an easy way of doing this. Though you may want to make things slightly more convenient by creating your own Float class (inheriting from float), or even a Value class that has a number and an error. Then override the the __str__ method and you should be done for any number you print, while all the other operations can work as float (for the first class at least), or similar to float (second class). Cheers, Evert > but this fails. And I haven't figured out how to get this to work. Seems like it should be simple. > > Any help? > > > Thanks, > > Andre From tvsm at hotmail.com Tue Oct 5 20:16:00 2010 From: tvsm at hotmail.com (T MURPHY) Date: Tue, 5 Oct 2010 14:16:00 -0400 Subject: [Tutor] creating a class Message-ID: how do i go about creating a class in python. From prologic at shortcircuit.net.au Wed Oct 6 00:36:02 2010 From: prologic at shortcircuit.net.au (James Mills) Date: Wed, 6 Oct 2010 08:36:02 +1000 Subject: [Tutor] creating a class In-Reply-To: References: Message-ID: On Wed, Oct 6, 2010 at 4:16 AM, T MURPHY wrote: > how do i go about creating a class in python. By using the "class" keyword. Example: class Fruit(object): def __init__(self, name) self.name = name class Apple(Fruit): def __init__(self): super(Apple, self).__init__("apple") apple = Apple() print apple.name For more information, I suggest you start reading the python tutorial (1) cheers James 1. http://docs.python.org/tutorial/? -- -- James Mills -- -- "Problems are solved by method" From alan.gauld at btinternet.com Wed Oct 6 00:43:19 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 5 Oct 2010 23:43:19 +0100 Subject: [Tutor] specifying precision with scientific notation References: Message-ID: "Andre' Walker-Loud" wrote >> a = 0.00762921383941 >> ea = 0.000830132912068 >> a / ea > 9.190352205653852 > > By default, I will print the uncertainty ("ea") with two significant > digits. > In this example, the central value is about 10 times larger than the > uncertainty, so I want to print it with 3 significant figures. I don't understand why the difference but if the deciding factor is related to the ratio why bother with all the string stuff? Just use the ratio directly... >> p = int(("%.1e" % (a / ea)).split('e')[-1]) # grab the power in the >> scientific notation (is there a better way?) >> if p >= 0: Why not just limit = 10 n = 3 if a/ea <= limit else n = 2 # or whatever expression is needed to calculate n fmt = "%.%de + %.1e" % n else: print fmt % (a, ea) But I suspect I'm missing something in your reasoning about what size of n you want. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Wed Oct 6 00:46:01 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 5 Oct 2010 23:46:01 +0100 Subject: [Tutor] creating a class References: Message-ID: "T MURPHY" wrote > how do i go about creating a class in python. class C: pass is the simplest way. But it's not very useful, being empty. But most tutorials discuss OOP, which one are you using? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From amonroe at columbus.rr.com Wed Oct 6 00:58:08 2010 From: amonroe at columbus.rr.com (R. Alan Monroe) Date: Tue, 5 Oct 2010 18:58:08 -0400 Subject: [Tutor] perl or python In-Reply-To: References: Message-ID: <782084170819.20101005185808@columbus.rr.com> > Is it better to learn Perl or Python since i can manage only writing > simple bash shell scripts. > Please suggest/guide. Do you already have both installed (seeing bash, I bet you're on some version of Linux, so it's likely you do). You could try writing a very simple "guess my number" game or some other very simple program in both to see which you like best. Also, this might help you compare them: http://www.99-bottles-of-beer.net/p.html Alan From walksloud at gmail.com Wed Oct 6 02:51:49 2010 From: walksloud at gmail.com (Andre' Walker-Loud) Date: Tue, 5 Oct 2010 17:51:49 -0700 Subject: [Tutor] specifying precision with scientific notation In-Reply-To: References: Message-ID: <769762BB-B346-4AF7-B389-C1F6FB986EC8@gmail.com> Hi Alan, The point I can not get to work is > fmt = "%.%de + %.1e" % n else: when I try this, I get (python 2.6.5, OS X 10.6) > n = 3; fmt = "%.%de + %.1e" % n; fmt '%de + 3.0e+00' But something like the "%.%de " %n is exactly what I am looking for - if I could get it to work. Thanks, Andre On Oct 5, 2010, at 3:43 PM, Alan Gauld wrote: > "Andre' Walker-Loud" wrote > >>> a = 0.00762921383941 >>> ea = 0.000830132912068 >>> a / ea >> 9.190352205653852 >> >> By default, I will print the uncertainty ("ea") with two significant digits. >> In this example, the central value is about 10 times larger than the >> uncertainty, so I want to print it with 3 significant figures. > > I don't understand why the difference but if the deciding factor is related > to the ratio why bother with all the string stuff? Just use the ratio directly... > >>> p = int(("%.1e" % (a / ea)).split('e')[-1]) # grab the power in the scientific notation (is there a better way?) >>> if p >= 0: > > Why not just > > limit = 10 > n = 3 if a/ea <= limit else n = 2 # or whatever expression is needed to calculate n > fmt = "%.%de + %.1e" % n else: > > print fmt % (a, ea) > > > But I suspect I'm missing something in your reasoning about what size of n you want. > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From walksloud at gmail.com Wed Oct 6 07:16:39 2010 From: walksloud at gmail.com (Andre' Walker-Loud) Date: Tue, 5 Oct 2010 22:16:39 -0700 Subject: [Tutor] specifying precision with scientific notation In-Reply-To: References: <769762BB-B346-4AF7-B389-C1F6FB986EC8@gmail.com> Message-ID: <2D76FD61-D3A2-46B5-AB00-5A216B18E3ED@gmail.com> Hi Wayne, Yes - that helps. I missed the correct combination of parentheses. Now I am trying to improve my aesthetics, as Evert suggested earlier > Personally, I would print this as 7.63e-03 +- 0.83e-03, which shows the precision a bit better. But that's just a matter of aesthetics, and would make things even more complicated (then again, since you are printing numbers and formatting them, you are concerned about aesthetics). I agree with him here, and would like to match this style. I have been trying to figure out if you can specify the "mantissa" and exponent in scientific notation, by reading in the string.format section of python, but have been unsuccessful. Is there an easy way to do this? To be precise, I now have > a = 0.0762921383941; ea = 0.000830132912068 > p = int(("%.1e" % (a / ea)).split('e')[-1]) > print(('%.' + str(int(2+p)) +'e +- %.1e') % (a, ea)) 7.629e-02 +- 8.3e-04 and instead I would like this to print 7.629e-02 +- 0.083e-02 I could imagine writing a little function that does all this, but am hoping there is a quick (and dirty) way to just force the scientific notation to into this format - I guess by forcing the power of the exponent. Thanks, Andre On Oct 5, 2010, at 6:39 PM, Wayne Werner wrote: > On Tue, Oct 5, 2010 at 7:51 PM, Andre' Walker-Loud wrote: > Hi Alan, > > The point I can not get to work is > > > fmt = "%.%de + %.1e" % n else: > > when I try this, I get (python 2.6.5, OS X 10.6) > > > n = 3; fmt = "%.%de + %.1e" % n; fmt > '%de + 3.0e+00' > > But something like the "%.%de " %n is exactly what I am looking for - if I could get it to work. > > > a = 0.00762921383941 > ea = 0.000830132912068 > > > p = int(("%.1e" % (a / ea)).split('e')[-1]) # grab the power in the scientific n > otation (is there a better way?) > if p >= 0: > print(('%.' + str(int(2+p)) +'e +- %.1e') % (a, ea)) > else: > print('%.2e +- %.1e' % (a, ea)) > #(desired output): 7.63e-03 +- 8.3e-04 > > > This works for me - I added some extra parenthesis because the original was giving me this error: > > Traceback (most recent call last): > File "exponent.py", line 7, in > print('%.' + str(int(2+p)) +'e +- %.1e' % (a, ea)) > TypeError: not all arguments converted during string formatting > > HTH, > Wayne > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Oct 6 09:26:51 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Wed, 6 Oct 2010 07:26:51 +0000 (GMT) Subject: [Tutor] specifying precision with scientific notation In-Reply-To: <769762BB-B346-4AF7-B389-C1F6FB986EC8@gmail.com> References: <769762BB-B346-4AF7-B389-C1F6FB986EC8@gmail.com> Message-ID: <618801.12420.qm@web86701.mail.ird.yahoo.com> > But something like the "%.%de " %n is exactly what I am looking for - if I >could get it to work. Sorry my bad, I missed a % sign: >>> n=5 >>> "%%.%de" % n '%.5e' You need two %% to create a % in the output. Alan G. From delegbede at dudupay.com Wed Oct 6 11:25:44 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Wed, 6 Oct 2010 10:25:44 +0100 Subject: [Tutor] CGI HELP Message-ID: Hi all, I wrote this code as an example from a book: #!c:\Python26\python.exe # Program displaying CGI environment variables. import os import cgi def printHeader(title): print """Content-type: text/html %s """ % title rowNumber = 0 backgroundColour = "white" printHeader("Environmental Variables") print """""" # print table of cgi variables and values for item in os.environ.keys(): rowNumber += 1 if rowNumber % 2 == 0: # even row numbers are white backgroundColour = "white" else: # odd row numbers are grey backgroundColour = "lightgrey" print """
%s%s""" %(backgroundColour, cgi.escape(item), cgi.escape(os.environ[item])) print """
""" It was supposed to return a list of environmental variables in a given order. Sadly only the header displayed. I have looked through over and over, please what am i doing wrong. This is from Deitel How to program python, Chapter 6. thanks and best regards. -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development -------------- next part -------------- An HTML attachment was scrubbed... URL: From delegbede at dudupay.com Wed Oct 6 11:38:55 2010 From: delegbede at dudupay.com (Dipo Elegbede) Date: Wed, 6 Oct 2010 10:38:55 +0100 Subject: [Tutor] cgi help Message-ID: here is the code: http://pastebin.com/K2Fxq6ac kindly help me figure out my mistake. It is supposed to return a table of environmental variables but it returns only the header and a blank page. thanks -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Wed Oct 6 11:54:03 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 6 Oct 2010 05:54:03 -0400 Subject: [Tutor] CGI HELP In-Reply-To: References: Message-ID: On Wed, Oct 6, 2010 at 5:25 AM, Dipo Elegbede wrote: > Hi all, > > I wrote this code as an example from a book: > > #!c:\Python26\python.exe > # Program displaying CGI environment variables. > > import os > import cgi > > def printHeader(title): > print """Content-type: text/html > > > "-//W3C//DTD XHTML 1.0 Strict//EN" > "DTD/xhtml1-strict.dtd"> > > %s > > """ % title > > rowNumber = 0 > backgroundColour = "white" > > printHeader("Environmental Variables") > print """""" > > # print table of cgi variables and values > for item in os.environ.keys(): > rowNumber += 1 > > if rowNumber % 2 == 0: # even row numbers are white > backgroundColour = "white" > else: # odd row numbers are grey > backgroundColour = "lightgrey" > > print """ >
%s%s""" %(backgroundColour, > cgi.escape(item), cgi.escape(os.environ[item])) > print """
""" > > It was supposed to return a list of environmental variables in a given > order. > Sadly only the header displayed. > I have looked through over and over, please what am i doing wrong. > This is from Deitel How to program python, Chapter 6. > > thanks and best regards. > -- > did you lose the indentation in copying code to email? -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From delegbede at dudupay.com Wed Oct 6 11:59:01 2010 From: delegbede at dudupay.com (delegbede at dudupay.com) Date: Wed, 6 Oct 2010 09:59:01 +0000 Subject: [Tutor] CGI HELP In-Reply-To: References: Message-ID: <1359223843-1286359141-cardhu_decombobulator_blackberry.rim.net-2053004156-@bda2349.bisx.produk.on.blackberry> Yes, so I did a pastebin and resent to the group. Thanks. Sent from my BlackBerry wireless device from MTN -----Original Message----- From: Joel Goldstick Sender: tutor-bounces+delegbede=dudupay.com at python.org Date: Wed, 6 Oct 2010 05:54:03 To: tutor Subject: Re: [Tutor] CGI HELP _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From crusier at gmail.com Wed Oct 6 12:06:03 2010 From: crusier at gmail.com (Crusier) Date: Wed, 6 Oct 2010 18:06:03 +0800 Subject: [Tutor] Scrapy vs. Beautiful Soup Message-ID: Hi Emile, I have also found that there is something called Scrapy. Please kindly comment on it. Which one is easier to use compared with Beautiful Soup? Thanks in advance. Cheers, Hank From davea at ieee.org Wed Oct 6 13:30:42 2010 From: davea at ieee.org (Dave Angel) Date: Wed, 06 Oct 2010 07:30:42 -0400 Subject: [Tutor] cgi help In-Reply-To: References: Message-ID: <4CAC5DE2.9040409@ieee.org> On 2:59 PM, Dipo Elegbede wrote: > here is the code: > > http://pastebin.com/K2Fxq6ac > > kindly help me figure out my mistake. > > It is supposed to return a table of environmental variables but it returns > only the header and a blank page. > > thanks > Care to define "return" ? How are you running this code? How are you trapping and displaying the exception it gets when run? Can you show us the traceback? Are you looking at the output in a web browser? Which one? And are you doing a view-Source, or whatever your browser offers? A CGI program is intended to run on a web server, and debugging it can be very difficult. Are you sure you want this as a beginner project? If so, then I'd start by saying you should run it locally, till you get rid of all the errors you'll see that way. By inspection, I can see that you should get more than the header, you should also get the element, then it'll crash on the bogus assignment of the uninitialized variable rowNumber. When I run it locally, I get: Content-type: text/html Environmental Variables
Traceback (most recent call last): File "M:\cgitest.py", line 27, in rowNumber += 1 NameError: name 'rowNumber' is not defined You don't initialize the global variable rowNumber to anything, but just increment it. You do initialize a local variable in your function with the same name, but nothing is ever done with that, and of course it goes away when the function ends. Same with the backgroundColor local. To fix this problem you'll need to initialize rowNumber before the for statement. DaveA From delegbede at dudupay.com Wed Oct 6 13:58:09 2010 From: delegbede at dudupay.com (delegbede at dudupay.com) Date: Wed, 6 Oct 2010 11:58:09 +0000 Subject: [Tutor] cgi help Message-ID: <1066763168-1286366288-cardhu_decombobulator_blackberry.rim.net-1447257271-@bda2349.bisx.produk.on.blackberry> Got it fixed. Thanks y'all. Regards, ------Original Message------ From: Dave Angel To: Dipo Elegbede Cc: tutor Subject: Re: [Tutor] cgi help Sent: Oct 6, 2010 12:30 On 2:59 PM, Dipo Elegbede wrote: > here is the code: > > http://pastebin.com/K2Fxq6ac > > kindly help me figure out my mistake. > > It is supposed to return a table of environmental variables but it returns > only the header and a blank page. > > thanks > Care to define "return" ? How are you running this code? How are you trapping and displaying the exception it gets when run? Can you show us the traceback? Are you looking at the output in a web browser? Which one? And are you doing a view-Source, or whatever your browser offers? A CGI program is intended to run on a web server, and debugging it can be very difficult. Are you sure you want this as a beginner project? If so, then I'd start by saying you should run it locally, till you get rid of all the errors you'll see that way. By inspection, I can see that you should get more than the header, you should also get the
element, then it'll crash on the bogus assignment of the uninitialized variable rowNumber. When I run it locally, I get: Content-type: text/html Environmental Variables
Traceback (most recent call last): File "M:\cgitest.py", line 27, in rowNumber += 1 NameError: name 'rowNumber' is not defined You don't initialize the global variable rowNumber to anything, but just increment it. You do initialize a local variable in your function with the same name, but nothing is ever done with that, and of course it goes away when the function ends. Same with the backgroundColor local. To fix this problem you'll need to initialize rowNumber before the for statement. DaveA Sent from my BlackBerry wireless device from MTN From norman at khine.net Wed Oct 6 15:26:28 2010 From: norman at khine.net (Norman Khine) Date: Wed, 6 Oct 2010 15:26:28 +0200 Subject: [Tutor] EXECUTING PYTHON AND SQL STAMENTS In-Reply-To: References: Message-ID: hello On Tue, Oct 5, 2010 at 5:57 PM, Susana Iraiis Delgado Rodriguez wrote: > Hello, I already solved the problem, I change all the code, instead of using > os.system I changed to subprocess.Popen() and it worked fine: > import shlex, subprocess > def process(): > ???? print "Ingresa en el siguiente orden:" > ???? print "Nombre del nuevo mapa.shp?Nombre de la capa?Nombre del mapa > original" > ???? command_line = raw_input() > ?????args = shlex.split(command_line) > ???? p = subprocess.Popen(['C:/Archivos de > programa/FWTools2.4.7/bin/ogr2ogr', args[0], '-where', args[1], args[2]]) > ?if p: > ??print "Mapa generado" > process() > > Now the user has to enter 3 arguments an finally it worked. I have a > question, how can I tell the user if p execute ok? because even thouhg I > entered wrong parameters, it prints "Mapa generado". This line should only > appears if the arguments are acceptable. it is better to validate the user input arguments before you execute the subprocess > > 2010/10/5 Susana Iraiis Delgado Rodriguez >> >> Hello Norman: >> >> Thank you for taking the time to answer. I already changed my os.system() >> for your code. I got an error, when I executed this: >> os.system(" 'C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr.exe ' "+arg1 >> +" -where "+arg2 +" " +arg3) >> it throws me that "C:/Archivos" ?is not recognized as?an executable >> external or internal command, programm or file. >> If you really have other opton to fix my problem I'll be thankful because >> I don't have any idea to make this code useful. >> Thank you > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- ?u?op ?p?sdn p,u?n? p??o? ??? ??s no? '?u???? s???? ??? pu? '?u??uo? ?q s,??? ??? %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From eduardo.susan at gmail.com Wed Oct 6 18:25:56 2010 From: eduardo.susan at gmail.com (Eduardo Vieira) Date: Wed, 6 Oct 2010 10:25:56 -0600 Subject: [Tutor] how to extract data only after a certain condition is met Message-ID: The other day I was writing a script to extract data from a file from the line where a text is found to the end of the file. The same functionality is this sed script: '1,/regexp/'d I couldn't put my head to work around this and came up with a solution using list slicing. But how can I do that? I was experimenting with a simple list and I came up with this. I wonder if I shouldn't you a "while" statement, but how? a = ['m', 'a', 'r', 'i', 'g', 'o', 'l', 'd'] b = True for letter in a: if letter != 'i' and b: continue elif letter == 'i': b = False else: print letter Ok. This works, but I wonder if I shouldn't you a "while" statement, but how? Of course this solution is simpler: extracted = a[a.index("i")+1:] But I didn't want to build a list in memory with "readlines()" in the case of a file. Thanks for your guidance, Eduardo From __peter__ at web.de Wed Oct 6 19:12:52 2010 From: __peter__ at web.de (Peter Otten) Date: Wed, 06 Oct 2010 19:12:52 +0200 Subject: [Tutor] how to extract data only after a certain condition is met References: Message-ID: Eduardo Vieira wrote: > The other day I was writing a script to extract data from a file from > the line where a text is found to the end of the file. The same > functionality is this sed script: > '1,/regexp/'d > I couldn't put my head to work around this and came up with a solution > using list slicing. But how can I do that? I was experimenting with a > simple list and I came up with this. I wonder if I shouldn't you a > "while" statement, but how? > > a = ['m', 'a', 'r', 'i', 'g', 'o', 'l', 'd'] > b = True > > for letter in a: > if letter != 'i' and b: > continue > elif letter == 'i': > b = False > else: > print letter > > Ok. This works, but I wonder if I shouldn't you a "while" statement, but > how? I would use two for loops: >>> a = ['m', 'a', 'r', 'i', 'g', 'o', 'l', 'd'] >>> ai = iter(a) # make a list iterator >>> for letter in ai: ... if letter == "i": break ... >>> for letter in ai: ... print letter ... g o l d Normally a list iterator is created implicitly by writing for item in some_list: ... but here you have to make one explicitly because you want to reuse it in the second loop. Alternatively, the itertools module has the building blocks for this and similar problems: >>> from itertools import dropwhile, islice >>> def not_an_i(letter): ... return letter != "i" ... >>> for letter in dropwhile(not_an_i, a): ... print letter ... i g o l d OK, let's shave off the first item in the dropwhile(...) sequence: >>> for letter in islice(dropwhile(not_an_i, a), 1, None): ... print letter ... g o l d Peter From emmanuel.ruellan at laposte.net Wed Oct 6 19:29:45 2010 From: emmanuel.ruellan at laposte.net (Emmanuel Ruellan) Date: Wed, 6 Oct 2010 19:29:45 +0200 Subject: [Tutor] pymssql and encoding Message-ID: Hi tutors, I'm trying to fetch data from an MS SQL Server database with pymssql, but non-ascii results get garbled. >>> import pymssql >>> conn = pymssql.connect(user='sa', password=myPassword, host=server, database=targetDatabase, as_dict=True) >>> curs = conn.cursor() >>> curs.execute("select CardName from OCRD where CardCode = 'C00056'") >>> resultAsDict = curs.fetchall()[0] >>> customerName = resultAsDict['CardName'] >>> print customerName Immobili?re (whatever) >>> customerName 'Immobili\x8are (whatever)' There should be a small E with a grave accent (?) instead of the capital S with a caron (?) I'm getting. I've tried applying various encodings, but to no avail: >>> print customerName.decode('latin-1') Immobili?re (whatever) >>> print customerName.decode('utf-8') Traceback (most recent call last): File "", line 1, in print customerName.decode('utf-8') File "D:\Python26\lib\encodings\utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError: 'utf8' codec can't decode byte 0x8a in position 8: unexpected code byte >>> print customerName.decode('utf-7') Traceback (most recent call last): File "", line 1, in print customerName.decode('utf-7') File "D:\Python26\lib\encodings\utf_7.py", line 12, in decode return codecs.utf_7_decode(input, errors, True) UnicodeDecodeError: 'utf7' codec can't decode byte 0x8a in position 8: unexpected special character When executed from MS SQL Server's Management Studio, the same query returns "Immobili?re (whatever)", with an '?', as it should. The field in question is of type nvarchar, with collation SQL_Latin1_General_CP850_CI_AS. Do you have an idea what the actual encoding of the string might be? -------------- next part -------------- An HTML attachment was scrubbed... URL: From knacktus at googlemail.com Wed Oct 6 19:32:56 2010 From: knacktus at googlemail.com (Knacktus) Date: Wed, 06 Oct 2010 19:32:56 +0200 Subject: [Tutor] how to extract data only after a certain condition is met In-Reply-To: References: Message-ID: <4CACB2C8.5000605@googlemail.com> Am 06.10.2010 18:25, schrieb Eduardo Vieira: > The other day I was writing a script to extract data from a file from > the line where a text is found to the end of the file. The same > functionality is this sed script: > '1,/regexp/'d > I couldn't put my head to work around this and came up with a solution > using list slicing. But how can I do that? I was experimenting with a > simple list and I came up with this. I wonder if I shouldn't you a > "while" statement, but how? > > a = ['m', 'a', 'r', 'i', 'g', 'o', 'l', 'd'] > b = True > > for letter in a: > if letter != 'i' and b: > continue > elif letter == 'i': > b = False > else: > print letter > > Ok. This works, but I wonder if I shouldn't you a "while" statement, but how? Why would you want to use a while-loop? You would need to somehow stop the iteration (by catching some EOF Exception or the like). I think it's fine to use a for-loop as you have a predefined fixed number of iterations. I think your approach is OK. Easy to understand. But what if there's a second "i" after the first? In your solution all "i" are skipped. Also, I would choose clearer names: letters = ['m', 'a', 'r', 'i', 'g', 'o', 'l', 'd', 'i', 'n', 'i', 'o'] skip_letter = True for letter in letters: if letter == 'i' and skip_letter: skip_letter = False continue # if you don't want the first occurrence of "i" if not skip_letter: print letter Cheers, Jan From alan.gauld at btinternet.com Wed Oct 6 20:07:08 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 6 Oct 2010 19:07:08 +0100 Subject: [Tutor] Scrapy vs. Beautiful Soup References: Message-ID: "Crusier" wrote > I have also found that there is something called Scrapy. Please > kindly > comment on it. Which one is easier to use compared with Beautiful > Soup? Well I can safely say that Beautiful Soup will be exactly the same as Beauitiful Soup to use! :-) As for scrapy I'd never heard of it, but a quick look at the tutorial suggests its a different kind of bbeast entirely. For a start you don't run it from Python you run it under scrapy - which is no doubt some kind of wrap around python, but exactly how you would integrate scrapy code within a bigger python project is not immediately clear. Whereas BS is just another module you can import as usual. Now it is possible that if all you want to do is scrape data from a web site then scrapy may be all you need and it may be easier than BS - it certainly didn't look too hard provided you are compfortable defining XPath statements - but thats not exactly a common skill! If you altready know XPath then scrapy would definitely be worth a try, if not I'd stick with BS. Just my opinion based on a very quick glance at the scrapy site. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Wed Oct 6 20:18:09 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 6 Oct 2010 19:18:09 +0100 Subject: [Tutor] how to extract data only after a certain condition is met References: Message-ID: "Eduardo Vieira" wrote > The other day I was writing a script to extract data from a file > from > the line where a text is found to the end of the file. The standard pattern here is to use a sentinel, in pseudo code: def checkLine(line, start='',end=''): if (start in line) or (end in line): return True else: return False startPattern = 'some string (or regex)' endPattern = 'a concluding string or regex' sentinel = False while True read line from file sentinel = checkLine(line, startPattern, endPattern) if sentinel: processLine(line) You can simplify or complexify that in many ways, and you can add a break check to speed it up if you only expect to process a few lines. And checkLine can be as simple or as complex as you like. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From emile at fenx.com Wed Oct 6 20:50:31 2010 From: emile at fenx.com (Emile van Sebille) Date: Wed, 06 Oct 2010 11:50:31 -0700 Subject: [Tutor] how to extract data only after a certain condition is met In-Reply-To: References: Message-ID: On 10/6/2010 9:25 AM Eduardo Vieira said... > Of course this solution is simpler: > extracted = a[a.index("i")+1:] > But I didn't want to build a list in memory with "readlines()" in the > case of a file. This is what I do unless the files are _really big_ For-me-really-big-is-over-200Mb-ish-ly y'rs, Emile From joel.goldstick at gmail.com Wed Oct 6 20:58:36 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 6 Oct 2010 14:58:36 -0400 Subject: [Tutor] how to extract data only after a certain condition is met In-Reply-To: References: Message-ID: On Wed, Oct 6, 2010 at 2:50 PM, Emile van Sebille wrote: > On 10/6/2010 9:25 AM Eduardo Vieira said... > > > > Of course this solution is simpler: >> extracted = a[a.index("i")+1:] >> But I didn't want to build a list in memory with "readlines()" in the >> case of a file. >> > > This is what I do unless the files are _really big_ > > For-me-really-big-is-over-200Mb-ish-ly y'rs, > > Emile > > Why not loop with readline() and then the slice. That way only one line at > time in memory > -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Wed Oct 6 21:10:24 2010 From: emile at fenx.com (Emile van Sebille) Date: Wed, 06 Oct 2010 12:10:24 -0700 Subject: [Tutor] how to extract data only after a certain condition is met In-Reply-To: References: Message-ID: On 10/6/2010 11:58 AM Joel Goldstick said... > On Wed, Oct 6, 2010 at 2:50 PM, Emile van Sebille wrote: > >> On 10/6/2010 9:25 AM Eduardo Vieira said... >> >> >> >> Of course this solution is simpler: >>> extracted = a[a.index("i")+1:] >>> But I didn't want to build a list in memory with "readlines()" in the >>> case of a file. >>> >> >> This is what I do unless the files are _really big_ >> >> For-me-really-big-is-over-200Mb-ish-ly y'rs, >> >> Emile >> > Why not loop with readline() and then the slice. That way only one line at > time in memory > Because I'd consider that a premature optimization. I don't commonly worry about managing the memory footprint until there's a reason to. I've found that you can work to minimize the footprint, but as it's often indeterminate, you can't really control it. So I don't. Emile From ranceh at gmail.com Thu Oct 7 03:09:31 2010 From: ranceh at gmail.com (Rance Hall) Date: Wed, 6 Oct 2010 20:09:31 -0500 Subject: [Tutor] validating user input for cli app Message-ID: I have the following scenario, during a cli app a function is called whose purpose is to get enough info from user to create a database record. Clearly the input values need to be validated. How do I handle the situation where validation fails, but the answer to the question is required. so far I have something like this def moduser(clientid): if not len(clientid) == 0: client = getclient(clientid) # this is a database call to get the existing record for modification, entering a zero length string assumes its a new record instead of a mod. if len(client) == 0: print("client not found") # more code here to wait a second and then go back to the menu selection that started this function. clienttype = input("Enter client type, there are two choices, Commercial or Individual\n choose C or I ") ******* My question is about what goes here ******* ...... return This is where life gets confusing, if clienttype is not a c or an I (case insensitive) then I have to ask the question again or offer a way out of my function There are several questions being asked here, about name, address, etc., each element has its own validation rules, and each question needs and answer I know how to write the if statements to decide if the data entered is valid or not, but I need a way to deal with what happens when it is NOT valid. I'd like to be able to just ask the question again, and re-validate the new input or offer a "press q to quit" option q to quit is no big deal, but it doesn't make sense to ME to clutter up the code with a whole set of functions that each just ask one question and return a value. Especially since when my app is done there will likely be over a hundred of these type of one or two line functions. the names will be impossible to keep up with. Whats more, debugging and future upgrades will be a major PITA. The old GOTO syntax that everybody hates was ideal for this type of thing, if validation fails, just goto the question again and re-ask it. Based on my Internet research it looks like the only way you get GOTO capability is with an April Fools joke in 2004. So how do you do this? Rance From emmanuel.ruellan at laposte.net Thu Oct 7 00:14:15 2010 From: emmanuel.ruellan at laposte.net (Emmanuel Ruellan) Date: Thu, 7 Oct 2010 00:14:15 +0200 Subject: [Tutor] pymssql and encoding In-Reply-To: <1024A0C9-E8FB-4516-AAF0-B7E12C7EA581@gmail.com> References: <1024A0C9-E8FB-4516-AAF0-B7E12C7EA581@gmail.com> Message-ID: Perfect! Thanks Evert. I realise now that I don't fully grasp the concepts of encoding and collation and their relationship. -- Emmanuel On Wed, Oct 6, 2010 at 10:38 PM, Evert Rol wrote: > > > > >>> print customerName > > Immobili?re (whatever) > > >>> customerName > > 'Immobili\x8are (whatever)' > > > > There should be a small E with a grave accent (?) instead of the capital > S with a caron (?) I'm getting. > > > > I've tried applying various encodings, but to no avail: > > > > > > When executed from MS SQL Server's Management Studio, the same query > returns "Immobili?re (whatever)", with an '?', as it should. > > > > The field in question is of type nvarchar, with collation > SQL_Latin1_General_CP850_CI_AS. > > Well, you're SQL server seems to use CP850, which is (almost) the first > encoding I tried: > > >>> name= 'Immobili\x8are' > >>> name > 'Immobili\x8are' > >>> print name.decode('cp850') > Immobili?re > >>> > > Seems to work for me. > > Evert > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rbridges928 at gmail.com Thu Oct 7 03:41:07 2010 From: rbridges928 at gmail.com (Ryan Bridges) Date: Wed, 6 Oct 2010 21:41:07 -0400 Subject: [Tutor] Prime Numbers Message-ID: Hi, I'm using Python 2.6.2 and I need to write a code so that when I use an input, it will say if the number is prime or not. How do I do this? -------------- next part -------------- An HTML attachment was scrubbed... URL: From shantanoo at gmail.com Thu Oct 7 11:59:52 2010 From: shantanoo at gmail.com (=?UTF-8?B?4KS24KSC4KSk4KSo4KWCIChTaGFudGFub28p?=) Date: Thu, 7 Oct 2010 15:29:52 +0530 Subject: [Tutor] Prime Numbers In-Reply-To: References: Message-ID: On Thu, Oct 7, 2010 at 07:11, Ryan Bridges wrote: > Hi, > I'm using Python 2.6.2 and I need to write a code so that when I use an > input, it will say if the number is prime or not. How do I do this? > > Following links would be useful: http://en.wikipedia.org/wiki/Prime_number http://en.wikipedia.org/wiki/Primality_test once you decide which algorithm you want to implement, you should be able to get the python code in less than week's time :). HTH. regards, shantanoo -------------- next part -------------- An HTML attachment was scrubbed... URL: From evert.rol at gmail.com Wed Oct 6 22:38:18 2010 From: evert.rol at gmail.com (Evert Rol) Date: Wed, 6 Oct 2010 22:38:18 +0200 Subject: [Tutor] pymssql and encoding In-Reply-To: References: Message-ID: <1024A0C9-E8FB-4516-AAF0-B7E12C7EA581@gmail.com> > >>> print customerName > Immobili?re (whatever) > >>> customerName > 'Immobili\x8are (whatever)' > > There should be a small E with a grave accent (?) instead of the capital S with a caron (?) I'm getting. > > I've tried applying various encodings, but to no avail: > > When executed from MS SQL Server's Management Studio, the same query returns "Immobili?re (whatever)", with an '?', as it should. > > The field in question is of type nvarchar, with collation SQL_Latin1_General_CP850_CI_AS. Well, you're SQL server seems to use CP850, which is (almost) the first encoding I tried: >>> name= 'Immobili\x8are' >>> name 'Immobili\x8are' >>> print name.decode('cp850') Immobili?re >>> Seems to work for me. Evert From steve at pearwood.info Thu Oct 7 12:29:12 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 7 Oct 2010 21:29:12 +1100 Subject: [Tutor] Prime Numbers In-Reply-To: References: Message-ID: <201010072129.13232.steve@pearwood.info> On Thu, 7 Oct 2010 12:41:07 pm Ryan Bridges wrote: > Hi, > I'm using Python 2.6.2 and I need to write a code so that when I use > an input, it will say if the number is prime or not. How do I do > this? Using the keyboard is probably the best way, although at a pinch you could copy and paste individual letters from another document. We won't do your home work for you, but if you ask specific questions, we will be happy to help. -- Steven D'Aprano From steve at pearwood.info Thu Oct 7 12:42:08 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 7 Oct 2010 21:42:08 +1100 Subject: [Tutor] validating user input for cli app In-Reply-To: References: Message-ID: <201010072142.08665.steve@pearwood.info> On Thu, 7 Oct 2010 12:09:31 pm Rance Hall wrote: > I know how to write the if statements to decide if the data entered > is valid or not, but I need a way to deal with what happens when it > is NOT valid. > > I'd like to be able to just ask the question again, and re-validate > the new input or offer a "press q to quit" option Here is one way: def ask_again(prompt, condition): if not prompt.endswith(" "): prompt = prompt + " " while True: # loop forever response = input(prompt) # use raw_input in Python 2.x response = response.strip() if response.lower() == 'q': raise SysExit('user quit') if condition(response): return response else: print("I'm sorry, that is invalid. Please try again.") And an example of it in use: >>> ask_again("Enter two letters ", lambda s: len(s) == 2) Enter two letters abcd I'm sorry, that is invalid. Please try again. Enter two letters a I'm sorry, that is invalid. Please try again. Enter two letters xy 'xy' >>> >>> ask_again("Please enter a number", str.isdigit) Please enter a number fg I'm sorry, that is invalid. Please try again. Please enter a number yu I'm sorry, that is invalid. Please try again. Please enter a number 56 '56' >>> You can build in as much intelligence as you want, depending on how much effort you care to put into it. For instance, you might like to escalate the error messages: "Please try again." "I'm sorry, your answer is incorrect, please try again." "You must enter a number like 42 or 75. Please try again." "Hey dummy, don't you know what a number is?" although of course this requires more programming effort. And naturally the condition functions can be as simple, or as complicated, as you need. -- Steven D'Aprano From jojo.mwebaze at gmail.com Thu Oct 7 13:41:58 2010 From: jojo.mwebaze at gmail.com (Jojo Mwebaze) Date: Thu, 7 Oct 2010 13:41:58 +0200 Subject: [Tutor] wrap methods for logging purposes Message-ID: I used a the recipe (http://aspn.activestate.com/ASPN/Coo.../Recipe/198078) used to wrap methods for logging purposes. logging classes. But it does seem to work well with classes inherit form other classes - i get recursion errors! Here is an example of the classes .. class Base(MetaClass): class BFrame(BaseFrame): def __init__(self, pathname=''): Base.__init__(self, pathname = pathname) When i instatiate BiasFrame() below is the OUTPUT logmethod __init__ () {} logmethod __init__ () {'pathname': ''} logmethod __init__ () {'pathname': ''} logmethod __init__ () {'pathname': ''} logmethod __init__ () {'pathname': ''} . . RuntimeError: maximum recursion depth exceeded in cmp It starts with the __init__ of BFrame, that's correct. In the __init__ of BFrame is a call to Base.__init__ with the pathname as argument. That is the second call to __init__ in the output, but the class is wrong ! Instead of the __init__ of Base the __init__ of BFrame is called. is there any way i can correct this? -- += Johnson -------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From evert.rol at gmail.com Thu Oct 7 14:10:37 2010 From: evert.rol at gmail.com (Evert Rol) Date: Thu, 7 Oct 2010 14:10:37 +0200 Subject: [Tutor] wrap methods for logging purposes In-Reply-To: References: Message-ID: <522A7DFC-44DB-401F-BA02-7988361D916C@gmail.com> > I used a the recipe (http://aspn.activestate.com/ASPN/Coo.../Recipe/198078) used to wrap methods for logging purposes. logging classes. But it does seem to work well with classes inherit form other classes - i get recursion errors! > > Here is an example of the classes .. > > class Base(MetaClass): > > class BFrame(BaseFrame): > def __init__(self, pathname=''): > Base.__init__(self, pathname = pathname) > > When i instatiate BiasFrame() Sorry, but I can't see how this code example above can work; - there is no definition of class Base. There's not even 'pass' statement! - Where is MetaClass's definition? - you instantiate BiasFrame, but that's not defined. - BFrame inherits from BaseFrame, which I don't see defined inherit. - I definitely would advise against calling Base.__init__ if you inherit BFrame from BaseFrame: BaseFrame.__init__ makes more sense. But rather, use super instead. In Python 2: super(Base, self).__init__(pathname=pathname) Evert > > below is the OUTPUT > > logmethod __init__ () {} > logmethod __init__ () {'pathname': ''} > logmethod __init__ () {'pathname': ''} > logmethod __init__ () {'pathname': ''} > logmethod __init__ () {'pathname': ''} > . > . > RuntimeError: maximum recursion depth exceeded in cmp > > It starts with the __init__ of BFrame, that's correct. In the __init__ of BFrame is a call to Base.__init__ with the pathname as argument. That is the second call to __init__ in the output, but the class is wrong ! Instead of the __init__ of Base the __init__ of BFrame is called. > > is there any way i can correct this? > > -- > += Johnson > -------------------------------------- _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Thu Oct 7 15:45:56 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 7 Oct 2010 14:45:56 +0100 Subject: [Tutor] validating user input for cli app References: Message-ID: "Rance Hall" wrote > I'd like to be able to just ask the question again, and re-validate > the new input or offer a "press q to quit" option Thats what loops are for... > The old GOTO syntax that everybody hates was ideal for this type of > thing, if validation fails, just goto the question again and re-ask > it. Nope, it wasn't ideal and thats why it was removed from most languages. Structured Loops are better. > So how do you do this? You either write a loop that cycles until you get valid input or you could create a heirarchy of Field classes(*) that know how to request input and validate it. If the data is invalid they request input again - which takes us back to loops... (*) Or even a single Validator class that takes a validation function as an input parameter.... One possible loop would look something like: a = b = c = None dataOK = False while not dataOK: if not a: a = raw_input('Enter a: ') if not validA(a): a = None continue if not b: b = raw_input('Enter b: ') if not validB(b): b = None continue if not c: c = raw_input('Enter a: ') if not validC(c): c = None continue And since the input blocks are repeated you could even convert them to a function if you really cared or had a lot of fields. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From susana.delgado_s at utzmg.edu.mx Thu Oct 7 15:55:04 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Thu, 7 Oct 2010 08:55:04 -0500 Subject: [Tutor] PYTHON QUOTES ISSUE Message-ID: Hello members: How can I write a statement to execute the following: C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr R1G-GEODESIA.shp -where "LAYER = 'R1G-GEODESIA'" tapalpa_05_plani_point.dbf, I want my uotput to look like this. Instead I'm getting this C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr T21-PUENTES.shp -where LAYER=+line tapalpa_05_plani_line.shp In miy code line is a string given by the user: for line in open("unico.txt", "r").readlines(): p = subprocess.Popen(['C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "LAYER='line'", b+'.shp']) Any suggestions? -------------- next part -------------- An HTML attachment was scrubbed... URL: From jdeltoro1973 at gmail.com Thu Oct 7 17:23:27 2010 From: jdeltoro1973 at gmail.com (Juan Jose Del Toro) Date: Thu, 7 Oct 2010 10:23:27 -0500 Subject: [Tutor] IDE for Python Message-ID: Dear List; In your experience what is the best IDE for Python? I've used SPE and IDLE, I've also seen people using Eclipse but which one do you recommend? -- ?Saludos! / Greetings! Juan Jos? Del Toro M. jdeltoro1973 at gmail.com Guadalajara, Jalisco MEXICO -------------- next part -------------- An HTML attachment was scrubbed... URL: From beachkidken at gmail.com Thu Oct 7 18:00:12 2010 From: beachkidken at gmail.com (Ken Green) Date: Thu, 07 Oct 2010 12:00:12 -0400 Subject: [Tutor] IDE for Python In-Reply-To: References: Message-ID: <4CADEE8C.5090506@gmail.com> I have been using Geany under Ubuntu 10.04. I rarely use IDLE. Ken On 10/07/2010 11:23 AM, Juan Jose Del Toro wrote: > Dear List; > > In your experience what is the best IDE for Python? > > I've used SPE and IDLE, I've also seen people using Eclipse but which > one do you recommend? > > -- > ?Saludos! / Greetings! > Juan Jos? Del Toro M. > jdeltoro1973 at gmail.com > Guadalajara, Jalisco MEXICO > From alan.gauld at btinternet.com Thu Oct 7 18:23:10 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 7 Oct 2010 17:23:10 +0100 Subject: [Tutor] IDE for Python References: Message-ID: "Juan Jose Del Toro" wrote > In your experience what is the best IDE for Python? In my experience its vim and a couple of command shells. But that's to do with a)what I use Python for and b) many years experience using vim It may not work for you (and you could substitute emacs for vim if your fingers 'speak' emacs). > I've used SPE and IDLE, These are chalk and cheese. If you just want to write a few basic scripts then IDLE is a good choice. SPE is better if you want tov do GUI work or bigger projects. > I've also seen people using Eclipse PyDev on Eclipse is great if you already use Eclipse or if you are doing multi-language projects - and have a big modern PC... > but which one do you recommend? Whatever best matches what you are trying to do and your existing experience. And that may be more than one! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From taserian at gmail.com Thu Oct 7 18:48:25 2010 From: taserian at gmail.com (taserian) Date: Thu, 7 Oct 2010 12:48:25 -0400 Subject: [Tutor] PYTHON QUOTES ISSUE In-Reply-To: References: Message-ID: I'm adding some line breaks to make your text a little more readable. On Thu, Oct 7, 2010 at 9:55 AM, Susana Iraiis Delgado Rodriguez < susana.delgado_s at utzmg.edu.mx> wrote: > Hello members: > > How can I write a statement to execute the following: > > C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr R1G-GEODESIA.shp -where > "LAYER = 'R1G-GEODESIA'" tapalpa_05_plani_point.dbf > > I want my uotput to look like this. > Instead I'm getting this > > C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr T21-PUENTES.shp -where > LAYER=+line tapalpa_05_plani_line.shp > > In miy code line is a string given by the user: > > for line in open("unico.txt", "r").readlines(): > p = subprocess.Popen(['C:/Archivos de > programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "LAYER='line'", > b+'.shp']) > > Any suggestions? > Without knowing specifics about what the subprocess.Popen function is expecting as parameters, I can only speculate, but it seems that the following *might* work (for extremely generous values of "*might*"): for line in open("unico.txt", "r").readlines(): p = subprocess.Popen(['C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "\"LAYER='" + line + "'\"", b+'.shp']) Details about where the changes are: "\"LAYER='" + line + "'\"" Quote to begin the literal: " An escaped quote (1) so that there's a quote inside the literal: \" Some of the text that's meant to be unchanging: LAYER=" Close Quote: " Add the content of the variable "line" from the unico.txt file: + line + Add another literal, composed of just the closing escaped quote to close the quote above (1) : "\"" See if this works, and let us know how it turns out. Antonio Rodriguez -------------- next part -------------- An HTML attachment was scrubbed... URL: From taserian at gmail.com Thu Oct 7 19:15:03 2010 From: taserian at gmail.com (taserian) Date: Thu, 7 Oct 2010 13:15:03 -0400 Subject: [Tutor] PYTHON QUOTES ISSUE In-Reply-To: References: Message-ID: On Thu, Oct 7, 2010 at 12:48 PM, taserian wrote: > I'm adding some line breaks to make your text a little more readable. > > On Thu, Oct 7, 2010 at 9:55 AM, Susana Iraiis Delgado Rodriguez < > susana.delgado_s at utzmg.edu.mx> wrote: > > Hello members: >> >> How can I write a statement to execute the following: >> > > >> C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr R1G-GEODESIA.shp -where >> "LAYER = 'R1G-GEODESIA'" tapalpa_05_plani_point.dbf >> > > >> I want my uotput to look like this. >> Instead I'm getting this >> > > >> C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr T21-PUENTES.shp -where >> LAYER=+line tapalpa_05_plani_line.shp >> > > >> In miy code line is a string given by the user: >> >> for line in open("unico.txt", "r").readlines(): >> p = subprocess.Popen(['C:/Archivos de >> programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "LAYER='line'", >> b+'.shp']) >> >> Any suggestions? >> > > Without knowing specifics about what the subprocess.Popen function is > expecting as parameters, I can only speculate, but it seems that the > following *might* work (for extremely generous values of "*might*"): > > for line in open("unico.txt", "r").readlines(): > p = subprocess.Popen(['C:/Archivos de > programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "\"LAYER='" + > line + "'\"", b+'.shp']) > > Details about where the changes are: > "\"LAYER='" + line + "'\"" > Begin corrections (corrections start with a *) Quote to begin the literal: " > An escaped quote (1) so that there's a quote inside the literal: \" > Some of the text that's meant to be unchanging: LAYER= > *Single Quote (2) to be included in the literal (which doesn't need to be escaped): ' > Close Quote: " > Add the content of the variable "line" from the unico.txt file: + line + > *Add another literal, composed of the single quote that closes (2) above, then the closing escaped quote to close (1) : "'\"" > > See if this works, and let us know how it turns out. > > Antonio Rodriguez > End of corrections. Antonio Rodriguez -------------- next part -------------- An HTML attachment was scrubbed... URL: From roberto03 at gmail.com Thu Oct 7 19:19:24 2010 From: roberto03 at gmail.com (roberto) Date: Thu, 7 Oct 2010 19:19:24 +0200 Subject: [Tutor] new turtle module Message-ID: hello, i want to use the turtle module described in http://docs.python.org/library/turtle.html since the module in my python2.5 is a little bit outdated; is it correct to overwrite the turtle.py and turtle.pyc files in my current '/usr/lib/python2.5/lib-tk/' directory with the ones holding the same names unzipped from turtleDemo folder 'TurtleDemo-Python2.x' by author Gregor Lindl ? Thank you ! -- roberto From matlocs at odscompanies.com Thu Oct 7 18:59:02 2010 From: matlocs at odscompanies.com (Shawn Matlock) Date: Thu, 7 Oct 2010 09:59:02 -0700 Subject: [Tutor] Non-ASCII Message-ID: <1160CE2227C1694BA39144335BC3502536077CEFCC@MAIL.pdx.odshp.com> Hello, I'm going through an online tutorial for Jython (www.jython.org). I can't find a place to ask a question on that site so I thought I'd try here. I believe the code is supposed to traverse a directory, identifying file types. The script is failing with the following message: File "", line None SyntaxError: Non-ASCII character in file 'H:\workspace\test\src\root\nested\example.py', but no encoding declared; see http://www.python.org/peps/pep-0263.html for details Thank you, Shawn import os, sys from stat import * def walktree(top, callback): '''recursively descend the directory tree rooted at top, calling the callback function for each regular file''' for f in os.listdir(top): pathname = os.path.join(top, f) mode = os.stat(pathname)[ST_MODE] if S_ISDIR(mode): # It's a directory, recurse into it walktree(pathname, callback) elif S_ISREG(mode): # It's a file, call the callback function callback(pathname) else: # Unknown file type, print a message print 'Skipping %s' % pathname def visitfile(file): print 'visiting', file if __name__ == '__main__': walktree(sys.argv[1], visitfile) From vceder at canterburyschool.org Thu Oct 7 20:07:58 2010 From: vceder at canterburyschool.org (Vern Ceder) Date: Thu, 7 Oct 2010 14:07:58 -0400 Subject: [Tutor] new turtle module In-Reply-To: References: Message-ID: On Thu, Oct 7, 2010 at 1:19 PM, roberto wrote: > hello, > i want to use the turtle module described in > http://docs.python.org/library/turtle.html > > since the module in my python2.5 is a little bit outdated; > > is it correct to overwrite the turtle.py and turtle.pyc files in my current > '/usr/lib/python2.5/lib-tk/' > directory with the ones holding the same names unzipped from > turtleDemo folder 'TurtleDemo-Python2.x' by author Gregor Lindl ? > > Yes, that should work with Python 2.5, but probably not with any earlier versions. Cheers, Vern Ceder > Thank you ! > -- > roberto > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- This time for sure! -Bullwinkle J. Moose ----------------------------- Vern Ceder, Director of Technology Canterbury School, 3210 Smith Road, Ft Wayne, IN 46804 vceder at canterburyschool.org; 260-436-0746; FAX: 260-436-5137 The Quick Python Book, 2nd Ed - http://bit.ly/bRsWDW -------------- next part -------------- An HTML attachment was scrubbed... URL: From knacktus at googlemail.com Thu Oct 7 20:08:10 2010 From: knacktus at googlemail.com (Knacktus) Date: Thu, 07 Oct 2010 20:08:10 +0200 Subject: [Tutor] IDE for Python In-Reply-To: References: Message-ID: <4CAE0C8A.4040203@googlemail.com> Am 07.10.2010 17:23, schrieb Juan Jose Del Toro: > Dear List; > > In your experience what is the best IDE for Python? > I'm using Wing IDE. Very good overall package. I like especially the debug probe, which is like an interactive shell in the current stack. To me it's a good balance between features and learning curve. The only thing I really miss is refactoring support. That's why I'm currently checking out PyCharm, which is about to be released (currently release candidate). It's from the company that created IntelliJ. PyCharm is in my opinion by far the most feature-rich Python IDE, looks very impressive so far. The only drawback is that it's written in Java and has a Swing GUI ;-) (ouuch, it hurts your eyes for a while but you get over it, once you discover all those wicked features ;-)). But Wing isn't excactly eye-candy either. Both are commercial, but if you code a lot it's worth it. Check out the offerings. (I think both are free for Open Source Projects.) I also tried the free PyDev (an Eclipse plugin), which is very complete as well, but I don't get along with the Eclipse world. So, check out Wing and PyCharm. Cheers, JJ From susana.delgado_s at utzmg.edu.mx Thu Oct 7 20:19:42 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Thu, 7 Oct 2010 13:19:42 -0500 Subject: [Tutor] PYTHON QUOTES ISSUE In-Reply-To: References: Message-ID: Hello taserian and Antonio! Thank you both for taking the time to answer my question. With taserian's code it gives me the next output: C... ogr2ogr T21-PUENTESshp -where "LAYER=T21-PUENTES" tapalpa_05_plani_line.shp but the output I need is: C... ogr2ogr T21-PUENTESshp -where "LAYER=' T21-PUENTES' " tapalpa_05_plani_line.shp I did the Antonio's suggested corrections, and I got the string I wanted, now the problem is that my subprocess doesn't work properly, I'll give a look and see whats wrong with it. 2010/10/7 taserian > On Thu, Oct 7, 2010 at 12:48 PM, taserian wrote: > >> I'm adding some line breaks to make your text a little more readable. >> >> On Thu, Oct 7, 2010 at 9:55 AM, Susana Iraiis Delgado Rodriguez < >> susana.delgado_s at utzmg.edu.mx> wrote: >> >> Hello members: >>> >>> How can I write a statement to execute the following: >>> >> >> >>> C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr R1G-GEODESIA.shp -where >>> "LAYER = 'R1G-GEODESIA'" tapalpa_05_plani_point.dbf >>> >> >> >>> I want my uotput to look like this. >>> Instead I'm getting this >>> >> >> >>> C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr T21-PUENTES.shp -where >>> LAYER=+line tapalpa_05_plani_line.shp >>> >> >> >>> In miy code line is a string given by the user: >>> >>> for line in open("unico.txt", "r").readlines(): >>> p = subprocess.Popen(['C:/Archivos de >>> programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "LAYER='line'", >>> b+'.shp']) >>> >>> Any suggestions? >>> >> >> Without knowing specifics about what the subprocess.Popen function is >> expecting as parameters, I can only speculate, but it seems that the >> following *might* work (for extremely generous values of "*might*"): >> >> for line in open("unico.txt", "r").readlines(): >> p = subprocess.Popen(['C:/Archivos de >> programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "\"LAYER='" + >> line + "'\"", b+'.shp']) >> >> Details about where the changes are: >> "\"LAYER='" + line + "'\"" >> > > Begin corrections (corrections start with a *) > > Quote to begin the literal: " >> An escaped quote (1) so that there's a quote inside the literal: \" >> Some of the text that's meant to be unchanging: LAYER= >> > *Single Quote (2) to be included in the literal (which doesn't need to be > escaped): ' > >> Close Quote: " >> > Add the content of the variable "line" from the unico.txt file: + line + >> > *Add another literal, composed of the single quote that closes (2) above, > then the closing escaped quote to close (1) : "'\"" > > >> >> See if this works, and let us know how it turns out. >> >> Antonio Rodriguez >> > > End of corrections. > > Antonio Rodriguez > -------------- next part -------------- An HTML attachment was scrubbed... URL: From evert.rol at gmail.com Thu Oct 7 21:33:51 2010 From: evert.rol at gmail.com (Evert Rol) Date: Thu, 7 Oct 2010 21:33:51 +0200 Subject: [Tutor] Non-ASCII In-Reply-To: <1160CE2227C1694BA39144335BC3502536077CEFCC@MAIL.pdx.odshp.com> References: <1160CE2227C1694BA39144335BC3502536077CEFCC@MAIL.pdx.odshp.com> Message-ID: > I'm going through an online tutorial for Jython (www.jython.org). I can't find a place to ask a question on that site so I thought I'd try here. I believe the code is supposed to traverse a directory, identifying file types. The script is failing with the following message: > > File "", line None > SyntaxError: Non-ASCII character in file 'H:\workspace\test\src\root\nested\example.py', but no encoding declared; see http://www.python.org/peps/pep-0263.html for details Normally, this (obviously) means you have some non-ASCII characters (accented characters would already do this) in your source code. Since this is just a byte code at the lowest level, it needs to be decoded into something sensible, which you specify using an encoding. Since Python refuses to guess which encoding you want to use (the same character code could result in totally different characters for different encodings), it's bailing out instead of continuing (the exception being that by default, the Python interpreter assumes your encoding is ASCII, and happily proceeds with that until it comes across a character code outside the ASCII range). In your code below, however, I don't see any non-ASCII characters. So my best guess is that there is some invisible control-character outside the ASCII range that's causing this error message. This could happen because of a particular type of text-editor/IDE you're using. The odd thing I find about the SyntaxError is actually that there is no line number mentioned. In fact, 'File "", line None' is a very weird indication for telling where the error occurred. Might again be something to do with your IDE. I've tried your code by simply copy-pasting, and that works fine for me. So the control-character didn't reach my mail program. In which case you could try to copy-past the code from this reply into a new file and see if that's gotten rid of the control-character (presuming that caused the problem). Of course, if it's eg a known problem in Jython, I can't really tell you, because I simply don't know Jython. But try clean code for a start. HTH, Evert > > Thank you, > Shawn > > import os, sys from stat import * def walktree(top, callback): '''recursively descend the directory tree rooted at top, calling the callback function for each regular file''' for f in os.listdir(top): pathname = os.path.join(top, f) mode = os.stat(pathname)[ST_MODE] if S_ISDIR(mode): # It's a directory, recurse into it walktree(pathname, callback) elif S_ISREG(mode): # It's a file, call the callback function callback(pathname) else: # Unknown file type, print a message print 'Skipping %s' % pathname def visitfile(file): print 'visiting', file if __name__ == '__main__': walktree(sys.argv[1], visitfile) > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From flebber.crue at gmail.com Fri Oct 8 01:01:31 2010 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Fri, 8 Oct 2010 10:01:31 +1100 Subject: [Tutor] IDE for Python Message-ID: > Message: 5 > Date: Thu, 07 Oct 2010 20:08:10 +0200 > From: Knacktus > To: tutor at python.org > Subject: Re: [Tutor] IDE for Python > Message-ID: <4CAE0C8A.4040203 at googlemail.com> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > Am 07.10.2010 17:23, schrieb Juan Jose Del Toro: > > Dear List; > > > > In your experience what is the best IDE for Python? > > > I'm using Wing IDE. Very good overall package. I like especially the > debug probe, which is like an interactive shell in the current stack. To > me it's a good balance between features and learning curve. The only > thing I really miss is refactoring support. > That's why I'm currently checking out PyCharm, which is about to be > released (currently release candidate). It's from the company that > created IntelliJ. PyCharm is in my opinion by far the most feature-rich > Python IDE, looks very impressive so far. The only drawback is that it's > written in Java and has a Swing GUI ;-) (ouuch, it hurts your eyes for a > while but you get over it, once you discover all those wicked features > ;-)). But Wing isn't excactly eye-candy either. > Both are commercial, but if you code a lot it's worth it. Check out the > offerings. (I think both are free for Open Source Projects.) > > I also tried the free PyDev (an Eclipse plugin), which is very complete > as well, but I don't get along with the Eclipse world. > > So, check out Wing and PyCharm. > > Cheers, > > JJ > > > ------------------------------ > > I really like Spe, Stani's Python editor found here http://pythonide.blogspot.com/ . It really manages to keep everything clear open and accessible whilst still providing a tonne of features and support. There are some intro videos avaiable here. http://showmedo.com/videotutorials/series?name=PythonDevelopmentWithSPE . I have never seen Pycharm as JJ suggested so am going to check it out, I only recently installed Eclipse Helios with Aptana 3 which includes Pydev and Django support so I can't really offer an indepth opinion but it is an open source community with a lot of support which if you are learning am ide as well as a language could prove very helpful. A lot of editors don't have much in the way of documentation or community which I think is important. Another verygood option I like and have used a lot is DrPython http://drpython.sourceforge.net/. Ultimately though for keep the ide learning curve low and providing power I still go for Spe, if I had the money I would definitely look at wing ide. Cheers Sayth -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Oct 7 18:31:05 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 7 Oct 2010 17:31:05 +0100 Subject: [Tutor] PYTHON QUOTES ISSUE References: Message-ID: "Susana Iraiis Delgado Rodriguez" wrote > How can I write a statement to execute the following: > C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr > R1G-GEODESIA.shp -where > "LAYER = 'R1G-GEODESIA'" tapalpa_05_plani_point.dbf, > I want my uotput to look like this. erm, like what? The command string above? So where is this output coming from? Or are you talking about the output from the command? > Instead I'm getting this > C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr > T21-PUENTES.shp -where LAYER=+line tapalpa_05_plani_line.shp The difference is that you seem to be getting +line as a literal string instead of the variables value. Is that the problem? > In miy code line is a string given by the user: > > for line in open("unico.txt", "r").readlines(): You donlt need the readlines you can iterate over the open file. > p = subprocess.Popen(['C:/Archivos de > programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', > "LAYER='line'", > b+'.shp']) Notice that you have quotes around the line following LAYER= > Any suggestions? remove the quotes? And shift the double quotes after the = sign. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Fri Oct 8 01:11:45 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 8 Oct 2010 00:11:45 +0100 Subject: [Tutor] new turtle module References: Message-ID: "roberto" wrote > is it correct to overwrite the turtle.py and turtle.pyc files I'd overwrite the .py file but get Python to generate a new .pyc for you just to ensure compatibility. just type import turtle at the >>> prompt. HTH, Alan G From mark at martialfit.net Fri Oct 8 01:42:24 2010 From: mark at martialfit.net (Mark Weil) Date: Fri, 8 Oct 2010 06:42:24 +0700 Subject: [Tutor] IDE for Python In-Reply-To: References: Message-ID: There's also eric. It's geared towards pyqt slightly, but I do a lot of wxpython development in it as well. It's got project management and svn plugins, too. http://eric-ide.python-projects.org/ On Fri, Oct 8, 2010 at 6:01 AM, Sayth Renshaw wrote: > > Message: 5 >> Date: Thu, 07 Oct 2010 20:08:10 +0200 >> From: Knacktus >> To: tutor at python.org >> Subject: Re: [Tutor] IDE for Python >> Message-ID: <4CAE0C8A.4040203 at googlemail.com> >> Content-Type: text/plain; charset=ISO-8859-1; format=flowed >> >> >> Am 07.10.2010 17:23, schrieb Juan Jose Del Toro: >> > Dear List; >> > >> > In your experience what is the best IDE for Python? >> > >> I'm using Wing IDE. Very good overall package. I like especially the >> debug probe, which is like an interactive shell in the current stack. To >> me it's a good balance between features and learning curve. The only >> thing I really miss is refactoring support. >> That's why I'm currently checking out PyCharm, which is about to be >> released (currently release candidate). It's from the company that >> created IntelliJ. PyCharm is in my opinion by far the most feature-rich >> Python IDE, looks very impressive so far. The only drawback is that it's >> written in Java and has a Swing GUI ;-) (ouuch, it hurts your eyes for a >> while but you get over it, once you discover all those wicked features >> ;-)). But Wing isn't excactly eye-candy either. >> Both are commercial, but if you code a lot it's worth it. Check out the >> offerings. (I think both are free for Open Source Projects.) >> >> I also tried the free PyDev (an Eclipse plugin), which is very complete >> as well, but I don't get along with the Eclipse world. >> >> So, check out Wing and PyCharm. >> >> Cheers, >> >> JJ >> >> >> ------------------------------ >> >> > I really like Spe, Stani's Python editor found here > http://pythonide.blogspot.com/ . > > It really manages to keep everything clear open and accessible whilst still > providing a tonne of features and support. There are some intro videos > avaiable here. > http://showmedo.com/videotutorials/series?name=PythonDevelopmentWithSPE . > > I have never seen Pycharm as JJ suggested so am going to check it out, I > only recently installed Eclipse Helios with Aptana 3 which includes Pydev > and Django support so I can't really offer an indepth opinion but it is an > open source community with a lot of support which if you are learning am ide > as well as a language could prove very helpful. A lot of editors don't have > much in the way of documentation or community which I think is important. > > Another verygood option I like and have used a lot is DrPython > http://drpython.sourceforge.net/. > > Ultimately though for keep the ide learning curve low and providing power I > still go for Spe, if I had the money I would definitely look at wing ide. > > Cheers > > Sayth > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From augdawg09 at gmail.com Fri Oct 8 03:01:59 2010 From: augdawg09 at gmail.com (aug dawg) Date: Thu, 7 Oct 2010 21:01:59 -0400 Subject: [Tutor] IDE for Python In-Reply-To: References: Message-ID: I like gedit alot. It has a nice amount of plugins, like a filebrowser and various embedded terminals. Check it out. It's available for all platforms. It's at gedit.org. On Thu, Oct 7, 2010 at 7:42 PM, Mark Weil wrote: > There's also eric. It's geared towards pyqt slightly, but I do a lot of > wxpython development in it as well. > It's got project management and svn plugins, too. > http://eric-ide.python-projects.org/ > > On Fri, Oct 8, 2010 at 6:01 AM, Sayth Renshaw wrote: > >> >> Message: 5 >>> Date: Thu, 07 Oct 2010 20:08:10 +0200 >>> From: Knacktus >>> To: tutor at python.org >>> Subject: Re: [Tutor] IDE for Python >>> Message-ID: <4CAE0C8A.4040203 at googlemail.com> >>> Content-Type: text/plain; charset=ISO-8859-1; format=flowed >>> >>> >>> Am 07.10.2010 17:23, schrieb Juan Jose Del Toro: >>> > Dear List; >>> > >>> > In your experience what is the best IDE for Python? >>> > >>> I'm using Wing IDE. Very good overall package. I like especially the >>> debug probe, which is like an interactive shell in the current stack. To >>> me it's a good balance between features and learning curve. The only >>> thing I really miss is refactoring support. >>> That's why I'm currently checking out PyCharm, which is about to be >>> released (currently release candidate). It's from the company that >>> created IntelliJ. PyCharm is in my opinion by far the most feature-rich >>> Python IDE, looks very impressive so far. The only drawback is that it's >>> written in Java and has a Swing GUI ;-) (ouuch, it hurts your eyes for a >>> while but you get over it, once you discover all those wicked features >>> ;-)). But Wing isn't excactly eye-candy either. >>> Both are commercial, but if you code a lot it's worth it. Check out the >>> offerings. (I think both are free for Open Source Projects.) >>> >>> I also tried the free PyDev (an Eclipse plugin), which is very complete >>> as well, but I don't get along with the Eclipse world. >>> >>> So, check out Wing and PyCharm. >>> >>> Cheers, >>> >>> JJ >>> >>> >>> ------------------------------ >>> >>> >> I really like Spe, Stani's Python editor found here >> http://pythonide.blogspot.com/ . >> >> It really manages to keep everything clear open and accessible whilst >> still providing a tonne of features and support. There are some intro videos >> avaiable here. >> http://showmedo.com/videotutorials/series?name=PythonDevelopmentWithSPE . >> >> I have never seen Pycharm as JJ suggested so am going to check it out, I >> only recently installed Eclipse Helios with Aptana 3 which includes Pydev >> and Django support so I can't really offer an indepth opinion but it is an >> open source community with a lot of support which if you are learning am ide >> as well as a language could prove very helpful. A lot of editors don't have >> much in the way of documentation or community which I think is important. >> >> Another verygood option I like and have used a lot is DrPython >> http://drpython.sourceforge.net/. >> >> Ultimately though for keep the ide learning curve low and providing power >> I still go for Spe, if I had the money I would definitely look at wing ide. >> >> Cheers >> >> Sayth >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rwobben at hotmail.com Fri Oct 8 09:39:54 2010 From: rwobben at hotmail.com (Roelof Wobben) Date: Fri, 8 Oct 2010 07:39:54 +0000 Subject: [Tutor] list of dict question Message-ID: Hello, I have this programm : tournooi = [{'thuis': 'A','uit': "B",'thuisscore': 20, 'uitscore': 15},{'thuis': 'C','uit': "D",'thuisscore': 80, 'uitscore': 40}] stand = [] tussen_thuis = {} tussen_uit = {} for wedstrijd in tournooi : if wedstrijd['thuis'] in stand : print "True" else : tussen_thuis['ploeg'] = wedstrijd['thuis'] tussen_thuis['wedstrijden'] = 1 if wedstrijd['thuisscore']> wedstrijd['uitscore']: tussen_thuis['punten'] = 2 else: tussen_thuis['punten'] = 0 tussen_thuis['voor'] = wedstrijd['thuisscore'] tussen_thuis ['tegen'] = wedstrijd['uitscore'] stand.append(tussen_thuis) if wedstrijd['uit'] in stand : print "True" else : tussen_uit['ploeg'] = wedstrijd['uit'] tussen_uit['wedstrijden'] = 1 if wedstrijd['thuisscore'] < wedstrijd['uitscore']: tussen_uit['punten'] = 2 else: tussen_uit['punten'] = 0 tussen_uit['tegen'] = wedstrijd['thuisscore'] tussen_uit ['voor'] = wedstrijd['uitscore'] stand.append(tussen_uit) It gives this output : [{'punten': 2, 'tegen': 40, 'wedstrijden': 1, 'voor': 80, 'ploeg': 'C'}, {'punten': 0, 'tegen': 80, 'wedstrijden': 1, 'voor': 40, 'ploeg': 'D'}, {'punten': 2, 'tegen': 40, 'wedstrijden': 1, 'voor': 80, 'ploeg': 'C'}, {'punten': 0, 'tegen': 80, 'wedstrijden': 1, 'voor': 40, 'ploeg': 'D'}] So the data of A and B are overwriting by C and D. How can I prevent this ? Roelof From alan.gauld at btinternet.com Fri Oct 8 10:02:05 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 8 Oct 2010 09:02:05 +0100 Subject: [Tutor] list of dict question References: Message-ID: "Roelof Wobben" wrote > I have this programm : > > tournooi = [{'thuis': 'A','uit': "B",'thuisscore': 20, 'uitscore': > 15},{'thuis': 'C','uit': "D",'thuisscore': 80, 'uitscore': 40}] > stand = [] > tussen_thuis = {} > tussen_uit = {} Here you create your dictionary objects. You never create any more dictionary objects so these are the only ones you have. > for wedstrijd in tournooi : > if wedstrijd['thuis'] in stand : > print "True" stand is a list of dictionaries so this will never be True. > else : > tussen_thuis['ploeg'] = wedstrijd['thuis'] > tussen_thuis['wedstrijden'] = 1 > if wedstrijd['thuisscore']> wedstrijd['uitscore']: > tussen_thuis['punten'] = 2 > else: > tussen_thuis['punten'] = 0 > > tussen_thuis['voor'] = wedstrijd['thuisscore'] > tussen_thuis ['tegen'] = wedstrijd['uitscore'] > stand.append(tussen_thuis) Here you append the dictionary into stand > if wedstrijd['uit'] in stand : > print "True" But stand is a list with a dictionary inside so this test cannot be true since wedstrijg['uit'] is not a dictionary. > else : > tussen_uit['ploeg'] = wedstrijd['uit'] > tussen_uit['wedstrijden'] = 1 > if wedstrijd['thuisscore'] < wedstrijd['uitscore']: > tussen_uit['punten'] = 2 > else: > tussen_uit['punten'] = 0 > tussen_uit['tegen'] = wedstrijd['thuisscore'] > tussen_uit ['voor'] = wedstrijd['uitscore'] > stand.append(tussen_uit) Now you append a second dictionary to stand. On the next iteration you overwrite those two dictionaries with new values then append them to the list again. So you wind up with 2 copies of the updated dictionaries. > So the data of A and B are overwriting by C and D. > How can I prevent this ? You need to create new dictionaries for each iteration of the loop. Move the dictionary creation lines inside the loop. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From rwobben at hotmail.com Fri Oct 8 13:37:07 2010 From: rwobben at hotmail.com (Roelof Wobben) Date: Fri, 8 Oct 2010 11:37:07 +0000 Subject: [Tutor] list of dict question In-Reply-To: References: , Message-ID: ---------------------------------------- > To: tutor at python.org > From: alan.gauld at btinternet.com > Date: Fri, 8 Oct 2010 09:02:05 +0100 > Subject: Re: [Tutor] list of dict question > > > "Roelof Wobben" wrote > >> I have this programm : >> >> tournooi = [{'thuis': 'A','uit': "B",'thuisscore': 20, 'uitscore': >> 15},{'thuis': 'C','uit': "D",'thuisscore': 80, 'uitscore': 40}] >> stand = [] >> tussen_thuis = {} >> tussen_uit = {} > > Here you create your dictionary objects. > You never create any more dictionary objects so these are the only > ones you have. > >> for wedstrijd in tournooi : >> if wedstrijd['thuis'] in stand : >> print "True" > > stand is a list of dictionaries so this will never be True. > >> else : >> tussen_thuis['ploeg'] = wedstrijd['thuis'] >> tussen_thuis['wedstrijden'] = 1 >> if wedstrijd['thuisscore']> wedstrijd['uitscore']: >> tussen_thuis['punten'] = 2 >> else: >> tussen_thuis['punten'] = 0 >> >> tussen_thuis['voor'] = wedstrijd['thuisscore'] >> tussen_thuis ['tegen'] = wedstrijd['uitscore'] >> stand.append(tussen_thuis) > > Here you append the dictionary into stand > >> if wedstrijd['uit'] in stand : >> print "True" > > But stand is a list with a dictionary inside so this test > cannot be true since wedstrijg['uit'] is not a dictionary. > >> else : >> tussen_uit['ploeg'] = wedstrijd['uit'] >> tussen_uit['wedstrijden'] = 1 >> if wedstrijd['thuisscore'] < wedstrijd['uitscore']: >> tussen_uit['punten'] = 2 >> else: >> tussen_uit['punten'] = 0 >> tussen_uit['tegen'] = wedstrijd['thuisscore'] >> tussen_uit ['voor'] = wedstrijd['uitscore'] >> stand.append(tussen_uit) > > Now you append a second dictionary to stand. > > On the next iteration you overwrite those two dictionaries > with new values then append them to the list again. > So you wind up with 2 copies of the updated dictionaries. > >> So the data of A and B are overwriting by C and D. >> How can I prevent this ? > > You need to create new dictionaries for each iteration of the loop. > Move the dictionary creation lines inside the loop. > > HTH, > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Hello Alan, Thank you. Now i can work on a script which can check if a team exist in standen. Roelof From fal at libero.it Fri Oct 8 13:40:04 2010 From: fal at libero.it (Francesco Loffredo) Date: Fri, 08 Oct 2010 13:40:04 +0200 Subject: [Tutor] list of dict question In-Reply-To: References: Message-ID: <4CAF0314.3040303@libero.it> Il 08/10/2010 10.02, Alan Gauld ha scritto: > > "Roelof Wobben" wrote > >> I have this programm : >> >> tournooi = [{'thuis': 'A','uit': "B",'thuisscore': 20, 'uitscore': > ... >> for wedstrijd in tournooi : >> if wedstrijd['thuis'] in stand : >> print "True" > > stand is a list of dictionaries so this will never be True. >... >> if wedstrijd['uit'] in stand : >> print "True" > > But stand is a list with a dictionary inside so this test > cannot be true since wedstrijg['uit'] is not a dictionary. > I'll say the same another way: you are searching an apple in a container of baskets, one of which MAY CONTAIN an apple. But you only see baskets, and none of those can BE an apple! My two cents: the following might be still new for you, but this is a way to check if wedstrijd['thuis'] is in stand: if wedstrijd['thuis'] in [u['ploeg'] for u in stand] where you build a temporary list of 'ploeg's in stand and check whether wedstrijd['thuis'] is found there. >... > On the next iteration you overwrite those two dictionaries > with new values then append them to the list again. > So you wind up with 2 copies of the updated dictionaries. > ... This is difficult for me too: why does this happen? Or, more correctly, why should this happen? How can you save the current contents of a dictionary in a list, making sure that the saved values won't change if you update the dict? You tell Roelof that the dictionary must be created at every loop, but if so, where goes the elegance of myDictList.append(UpdateIt(myDict)) ??? Francesco (puzzled) -------------- next part -------------- Nessun virus nel messaggio in uscita. Controllato da AVG - www.avg.com Versione: 9.0.862 / Database dei virus: 271.1.1/3182 - Data di rilascio: 10/07/10 08:34:00 From zebra05 at gmail.com Fri Oct 8 14:51:37 2010 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Fri, 8 Oct 2010 14:51:37 +0200 Subject: [Tutor] Rounding a Python float to the nearest half integer Message-ID: Hi folks, Supposing I had the float 4.4348 and I wished to round it off to the nearest half-integer upwards or downwards, how would I go about it? Many thanks... -- Regards, Sithembewena Lloyd Dube http://www.lloyddube.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From zebra05 at gmail.com Fri Oct 8 14:58:30 2010 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Fri, 8 Oct 2010 14:58:30 +0200 Subject: [Tutor] Rounding a Python float to the nearest half integer In-Reply-To: References: Message-ID: I realise that one cannot have a half integer :) I meant how would one round off to the first decimal nearest to either 0.5, or a whole number. Ugh...does anyone get what I'm trying to articulate? :) On Fri, Oct 8, 2010 at 2:51 PM, Sithembewena Lloyd Dube wrote: > Hi folks, > > Supposing I had the float 4.4348 and I wished to round it off to the > nearest half-integer upwards or downwards, how would I go about it? > > Many thanks... > > -- > Regards, > Sithembewena Lloyd Dube > http://www.lloyddube.com > -- Regards, Sithembewena Lloyd Dube http://www.lloyddube.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From waynejwerner at gmail.com Fri Oct 8 15:00:10 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Fri, 8 Oct 2010 08:00:10 -0500 Subject: [Tutor] Rounding a Python float to the nearest half integer In-Reply-To: References: Message-ID: On Fri, Oct 8, 2010 at 7:51 AM, Sithembewena Lloyd Dube wrote: > Hi folks, > > Supposing I had the float 4.4348 and I wished to round it off to the > nearest half-integer upwards or downwards, how would I go about it? > You can use round: round(4.4348) -> 4.0 But if you want to specify the behavior you can do something like this: x = 4.4348 if x % 1 >= 0.5: x = x/1+1 else: x = x/1 HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From evert.rol at gmail.com Fri Oct 8 15:00:59 2010 From: evert.rol at gmail.com (Evert Rol) Date: Fri, 8 Oct 2010 15:00:59 +0200 Subject: [Tutor] Rounding a Python float to the nearest half integer In-Reply-To: References: Message-ID: > I realise that one cannot have a half integer :) I meant how would one round off to the first decimal nearest to either 0.5, or a whole number. > > Ugh...does anyone get what I'm trying to articulate? :) Multiply by 2, round(), divide by 2? > > On Fri, Oct 8, 2010 at 2:51 PM, Sithembewena Lloyd Dube wrote: > Hi folks, > > Supposing I had the float 4.4348 and I wished to round it off to the nearest half-integer upwards or downwards, how would I go about it? > > Many thanks... > > -- From joel.goldstick at gmail.com Fri Oct 8 15:35:25 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Fri, 8 Oct 2010 09:35:25 -0400 Subject: [Tutor] Rounding a Python float to the nearest half integer In-Reply-To: References: Message-ID: On Fri, Oct 8, 2010 at 9:00 AM, Evert Rol wrote: > > I realise that one cannot have a half integer :) I meant how would one > round off to the first decimal nearest to either 0.5, or a whole number. > > > > Ugh...does anyone get what I'm trying to articulate? :) > > Multiply by 2, round(), divide by 2? > That sounds like a good idea: > > >>> n = [1.0 + x/10.0 for x in range(10)] # get some numbers to test > >>> n > [1.0, 1.1000000000000001, 1.2, 1.3, 1.3999999999999999, 1.5, > 1.6000000000000001, 1.7, 1.8, 1.8999999999999999] > >>> r = [round(2*x)/2.0 for x in n] # double, round, then divide by 2.0 > >>> r > [1.0, 1.0, 1.0, 1.5, 1.5, 1.5, 1.5, 1.5, 2.0, 2.0] > >>> > > -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From zebra05 at gmail.com Fri Oct 8 15:39:35 2010 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Fri, 8 Oct 2010 15:39:35 +0200 Subject: [Tutor] Rounding a Python float to the nearest half integer In-Reply-To: References: Message-ID: Thanks everyone, I need to round to the nearest half (finally occured). Made some chnages to Wayne's code as follows: x = 4.4348 if x % 1 >= 0.5: round(x) # gives 5.0 if the the value of the expression x % 1 exceeds 0.5 else: x = round(x) + 0.5 # gives 4.5, as in this case. Many thanks! On Fri, Oct 8, 2010 at 3:00 PM, Evert Rol wrote: > > I realise that one cannot have a half integer :) I meant how would one > round off to the first decimal nearest to either 0.5, or a whole number. > > > > Ugh...does anyone get what I'm trying to articulate? :) > > Multiply by 2, round(), divide by 2? > > > > > > On Fri, Oct 8, 2010 at 2:51 PM, Sithembewena Lloyd Dube < > zebra05 at gmail.com> wrote: > > Hi folks, > > > > Supposing I had the float 4.4348 and I wished to round it off to the > nearest half-integer upwards or downwards, how would I go about it? > > > > Many thanks... > > > > -- > -- Regards, Sithembewena Lloyd Dube http://www.lloyddube.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From zebra05 at gmail.com Fri Oct 8 15:56:28 2010 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Fri, 8 Oct 2010 15:56:28 +0200 Subject: [Tutor] Rounding a Python float to the nearest half integer In-Reply-To: References: Message-ID: @Evert, I didn't figure out that your response was a solution, thought it was a question. Must be coffee time :P I tried it and, for instance, the rounded value (9) / 2 gave me 4.0 Couldn't get it until I noticed that @Joel divided the roudned figure by a decimal 2.0. That gave 4.5, which is what I was looking for. Thanks to all. On Fri, Oct 8, 2010 at 3:35 PM, Joel Goldstick wrote: > > > On Fri, Oct 8, 2010 at 9:00 AM, Evert Rol wrote: > >> > I realise that one cannot have a half integer :) I meant how would one >> round off to the first decimal nearest to either 0.5, or a whole number. >> > >> > Ugh...does anyone get what I'm trying to articulate? :) >> >> Multiply by 2, round(), divide by 2? >> > > That sounds like a good idea: > >> >> >>> n = [1.0 + x/10.0 for x in range(10)] # get some numbers to test >> >>> n >> [1.0, 1.1000000000000001, 1.2, 1.3, 1.3999999999999999, 1.5, >> 1.6000000000000001, 1.7, 1.8, 1.8999999999999999] >> >>> r = [round(2*x)/2.0 for x in n] # double, round, then divide by 2.0 >> >>> r >> [1.0, 1.0, 1.0, 1.5, 1.5, 1.5, 1.5, 1.5, 2.0, 2.0] >> >>> >> >> > -- > Joel Goldstick > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- Regards, Sithembewena Lloyd Dube http://www.lloyddube.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From susana.delgado_s at utzmg.edu.mx Fri Oct 8 16:27:25 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Fri, 8 Oct 2010 09:27:25 -0500 Subject: [Tutor] PYTHON QUOTES ISSUE In-Reply-To: References: Message-ID: Hi Alan: The ouput is coming from a cicle and some functions that I vae to do to execute an ogr2ogr command, in this output I ask the user for the name of a file and then make a module to get to the subprocess part: import shlex, subprocess, sys from dbf import * def process(): #Read dbfile status 100% a = open ("capas.txt","w+") print 'Enter the shapefile name' b = raw_input() print '\n' dbf = Dbf(b+".dbf",new=False) for rec in dbf: for fldName in dbf.fieldNames: if fldName == 'LAYER': l=() l=rec[fldName] a.write(l) a.write("\n") a.close() ##Eliminate duplicate lines from the txt into a new txt, status:100% a = open ("capas.txt","r") catalogo = open ("unico.txt","w") unique = set(a.read().split("\n")) catalogo.write("".join([line + "\n" for line in unique])) catalogo.close() a.close() ##Execute ogr2ogr command, status:75% for line in open("unico.txt", "r"): p = subprocess.Popen(['C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "\"LAYER='"+line+"'\"" , b+'.shp']) But when I executed it shows me an error in the layer's name: >>> ERROR 1: Failed to identify field:LAYER= ERROR 1: Failed to create file .shp file. ERROR 4: Failed to open Shapefile `0 .shp'. I think the erros showed up because some of the layer's values are 0 and ' ', and obsviously you can't create a file from nothing on it. But I don`t know how to validate if a layer's value is equals to 0 or ' ', any idea what I'm doing wrong or how to fix it? 2010/10/7 Susana Iraiis Delgado Rodriguez > Hello taserian and Antonio! > > Thank you both for taking the time to answer my question. With taserian's > code it gives me the next output: > C... ogr2ogr T21-PUENTESshp -where "LAYER=T21-PUENTES" > tapalpa_05_plani_line.shp > but the output I need is: > C... ogr2ogr T21-PUENTESshp -where "LAYER=' T21-PUENTES' " > tapalpa_05_plani_line.shp > > I did the Antonio's suggested corrections, and I got the string I wanted, > now the problem is that my subprocess doesn't work properly, I'll give a > look and see whats wrong with it. > > > 2010/10/7 taserian > > On Thu, Oct 7, 2010 at 12:48 PM, taserian wrote: >> >>> I'm adding some line breaks to make your text a little more readable. >>> >>> On Thu, Oct 7, 2010 at 9:55 AM, Susana Iraiis Delgado Rodriguez < >>> susana.delgado_s at utzmg.edu.mx> wrote: >>> >>> Hello members: >>>> >>>> How can I write a statement to execute the following: >>>> >>> >>> >>>> C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr R1G-GEODESIA.shp -where >>>> "LAYER = 'R1G-GEODESIA'" tapalpa_05_plani_point.dbf >>>> >>> >>> >>>> I want my uotput to look like this. >>>> Instead I'm getting this >>>> >>> >>> >>>> C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr T21-PUENTES.shp -where >>>> LAYER=+line tapalpa_05_plani_line.shp >>>> >>> >>> >>>> In miy code line is a string given by the user: >>>> >>>> for line in open("unico.txt", "r").readlines(): >>>> p = subprocess.Popen(['C:/Archivos de >>>> programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "LAYER='line'", >>>> b+'.shp']) >>>> >>>> Any suggestions? >>>> >>> >>> Without knowing specifics about what the subprocess.Popen function is >>> expecting as parameters, I can only speculate, but it seems that the >>> following *might* work (for extremely generous values of "*might*"): >>> >>> for line in open("unico.txt", "r").readlines(): >>> p = subprocess.Popen(['C:/Archivos de >>> programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "\"LAYER='" + >>> line + "'\"", b+'.shp']) >>> >>> Details about where the changes are: >>> "\"LAYER='" + line + "'\"" >>> >> >> Begin corrections (corrections start with a *) >> >> Quote to begin the literal: " >>> An escaped quote (1) so that there's a quote inside the literal: \" >>> Some of the text that's meant to be unchanging: LAYER= >>> >> *Single Quote (2) to be included in the literal (which doesn't need to be >> escaped): ' >> >>> Close Quote: " >>> >> Add the content of the variable "line" from the unico.txt file: + line >>> + >>> >> *Add another literal, composed of the single quote that closes (2) above, >> then the closing escaped quote to close (1) : "'\"" >> >> >>> >>> See if this works, and let us know how it turns out. >>> >>> Antonio Rodriguez >>> >> >> End of corrections. >> >> Antonio Rodriguez >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From susana.delgado_s at utzmg.edu.mx Fri Oct 8 16:37:21 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Fri, 8 Oct 2010 09:37:21 -0500 Subject: [Tutor] PYTHON QUOTES ISSUE In-Reply-To: References: Message-ID: Besides, If I print the query, I have the next output: C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr U3B-BARDA .shp -where "LAYER='U3B-BARDA '" tapalpa_05_plani_line.shp C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr N2H-TEMP .shp -where "LAYER='N2H-TEMP '" tapalpa_05_plani_line.shp C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr U3C-CERCA .shp -where "LAYER='U3C-CERCA '" tapalpa_05_plani_line.shp.......... I thought it was a console print issue, but when I run the module from Eclipse, it shows the same enter space, so I think it maybe that the line is not together as it is a statement!! 2010/10/8 Susana Iraiis Delgado Rodriguez > Hi Alan: > > The ouput is coming from a cicle and some functions that I vae to do to > execute an ogr2ogr command, in this output I ask the user for the name of a > file and then make a module to get to the subprocess part: > > import shlex, subprocess, sys > from dbf import * > def process(): > #Read dbfile status 100% > a = open ("capas.txt","w+") > print 'Enter the shapefile name' > b = raw_input() > print '\n' > dbf = Dbf(b+".dbf",new=False) > > for rec in dbf: > for fldName in dbf.fieldNames: > if fldName == 'LAYER': > l=() > l=rec[fldName] > a.write(l) > a.write("\n") > a.close() > > ##Eliminate duplicate lines from the txt into a new txt, status:100% > a = open ("capas.txt","r") > catalogo = open ("unico.txt","w") > unique = set(a.read().split("\n")) > catalogo.write("".join([line + "\n" for line in unique])) > catalogo.close() > a.close() > > ##Execute ogr2ogr command, status:75% > for line in open("unico.txt", "r"): > > p = subprocess.Popen(['C:/Archivos de > programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', > "\"LAYER='"+line+"'\"" , b+'.shp']) > > But when I executed it shows me an error in the layer's name: > > >>> ERROR 1: Failed to identify field:LAYER= > ERROR 1: Failed to create file .shp file. > ERROR 4: Failed to open Shapefile `0 > .shp'. > > I think the erros showed up because some of the layer's values are 0 and ' > ', and obsviously you can't create a file from nothing on it. But I don`t > know how to validate if a layer's value is equals to 0 or ' ', any idea what > I'm doing wrong or how to fix it? > > > 2010/10/7 Susana Iraiis Delgado Rodriguez > > Hello taserian and Antonio! >> >> Thank you both for taking the time to answer my question. With taserian's >> code it gives me the next output: >> C... ogr2ogr T21-PUENTESshp -where "LAYER=T21-PUENTES" >> tapalpa_05_plani_line.shp >> but the output I need is: >> C... ogr2ogr T21-PUENTESshp -where "LAYER=' T21-PUENTES' " >> tapalpa_05_plani_line.shp >> >> I did the Antonio's suggested corrections, and I got the string I wanted, >> now the problem is that my subprocess doesn't work properly, I'll give a >> look and see whats wrong with it. >> >> >> 2010/10/7 taserian >> >> On Thu, Oct 7, 2010 at 12:48 PM, taserian wrote: >>> >>>> I'm adding some line breaks to make your text a little more readable. >>>> >>>> On Thu, Oct 7, 2010 at 9:55 AM, Susana Iraiis Delgado Rodriguez < >>>> susana.delgado_s at utzmg.edu.mx> wrote: >>>> >>>> Hello members: >>>>> >>>>> How can I write a statement to execute the following: >>>>> >>>> >>>> >>>>> C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr R1G-GEODESIA.shp >>>>> -where "LAYER = 'R1G-GEODESIA'" tapalpa_05_plani_point.dbf >>>>> >>>> >>>> >>>>> I want my uotput to look like this. >>>>> Instead I'm getting this >>>>> >>>> >>>> >>>>> C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr T21-PUENTES.shp -where >>>>> LAYER=+line tapalpa_05_plani_line.shp >>>>> >>>> >>>> >>>>> In miy code line is a string given by the user: >>>>> >>>>> for line in open("unico.txt", "r").readlines(): >>>>> p = subprocess.Popen(['C:/Archivos de >>>>> programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "LAYER='line'", >>>>> b+'.shp']) >>>>> >>>>> Any suggestions? >>>>> >>>> >>>> Without knowing specifics about what the subprocess.Popen function is >>>> expecting as parameters, I can only speculate, but it seems that the >>>> following *might* work (for extremely generous values of "*might*"): >>>> >>>> for line in open("unico.txt", "r").readlines(): >>>> p = subprocess.Popen(['C:/Archivos de >>>> programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "\"LAYER='" + >>>> line + "'\"", b+'.shp']) >>>> >>>> Details about where the changes are: >>>> "\"LAYER='" + line + "'\"" >>>> >>> >>> Begin corrections (corrections start with a *) >>> >>> Quote to begin the literal: " >>>> An escaped quote (1) so that there's a quote inside the literal: \" >>>> Some of the text that's meant to be unchanging: LAYER= >>>> >>> *Single Quote (2) to be included in the literal (which doesn't need to be >>> escaped): ' >>> >>>> Close Quote: " >>>> >>> Add the content of the variable "line" from the unico.txt file: + line >>>> + >>>> >>> *Add another literal, composed of the single quote that closes (2) above, >>> then the closing escaped quote to close (1) : "'\"" >>> >>> >>>> >>>> See if this works, and let us know how it turns out. >>>> >>>> Antonio Rodriguez >>>> >>> >>> End of corrections. >>> >>> Antonio Rodriguez >>> >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From evert.rol at gmail.com Fri Oct 8 16:50:03 2010 From: evert.rol at gmail.com (Evert Rol) Date: Fri, 8 Oct 2010 16:50:03 +0200 Subject: [Tutor] Rounding a Python float to the nearest half integer In-Reply-To: References: Message-ID: > @Evert, I didn't figure out that your response was a solution, thought it was a question. Must be coffee time :P > > I tried it and, for instance, the rounded value (9) / 2 gave me 4.0 Couldn't get it until I noticed that @Joel divided the roudned figure by a decimal 2.0. That gave 4.5, which is what I was looking for. How do you get a rounded value of 9 (integer)? Round() returns a float, afaik; ie, you wouldn't the the 2.0, but just 2. (In fact, your return value is 4.0, which for integer division wouldn't work. So something was odd there, but I can't think of what.) Also, perhaps better to use from __future__ import division to prevent these mistakes. Cheers, Evert > > I realise that one cannot have a half integer :) I meant how would one round off to the first decimal nearest to either 0.5, or a whole number. > > > > Ugh...does anyone get what I'm trying to articulate? :) > > Multiply by 2, round(), divide by 2? > > That sounds like a good idea: > > >>> n = [1.0 + x/10.0 for x in range(10)] # get some numbers to test > >>> n > [1.0, 1.1000000000000001, 1.2, 1.3, 1.3999999999999999, 1.5, 1.6000000000000001, 1.7, 1.8, 1.8999999999999999] > >>> r = [round(2*x)/2.0 for x in n] # double, round, then divide by 2.0 > >>> r > [1.0, 1.0, 1.0, 1.5, 1.5, 1.5, 1.5, 1.5, 2.0, 2.0] > >>> > > > -- > Joel Goldstick > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > > > -- > Regards, > Sithembewena Lloyd Dube > http://www.lloyddube.com > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From rwobben at hotmail.com Fri Oct 8 16:54:57 2010 From: rwobben at hotmail.com (Roelof Wobben) Date: Fri, 8 Oct 2010 14:54:57 +0000 Subject: [Tutor] FW: list of dict question In-Reply-To: References: , , <4CAF0314.3040303@libero.it>, Message-ID: ---------------------------------------- > From: rwobben at hotmail.com > To: fal at libero.it > Subject: RE: [Tutor] list of dict question > Date: Fri, 8 Oct 2010 14:53:53 +0000 > > > > > ---------------------------------------- > Date: Fri, 8 Oct 2010 13:40:04 +0200 > From: fal at libero.it > To: tutor at python.org > Subject: Re: [Tutor] list of dict question > > > Il 08/10/2010 10.02, Alan Gauld ha scritto: >> >> "Roelof Wobben" wrote >> >>> I have this programm : >>> >>> tournooi = [{'thuis': 'A','uit': "B",'thuisscore': 20, 'uitscore': >> ... >>> for wedstrijd in tournooi : >>> if wedstrijd['thuis'] in stand : >>> print "True" >> >> stand is a list of dictionaries so this will never be True. >>... >>> if wedstrijd['uit'] in stand : >>> print "True" >> >> But stand is a list with a dictionary inside so this test >> cannot be true since wedstrijg['uit'] is not a dictionary. >> > I'll say the same another way: you are searching an apple in a container > of baskets, one of which MAY CONTAIN an apple. But you only see baskets, > and none of those can BE an apple! > My two cents: the following might be still new for you, but this is a > way to check if wedstrijd['thuis'] is in stand: > > if wedstrijd['thuis'] in [u['ploeg'] for u in stand] > > where you build a temporary list of 'ploeg's in stand and check whether > wedstrijd['thuis'] is found there. > >>... >> On the next iteration you overwrite those two dictionaries >> with new values then append them to the list again. >> So you wind up with 2 copies of the updated dictionaries. >> ... > This is difficult for me too: why does this happen? Or, more correctly, > why should this happen? How can you save the current contents of a > dictionary in a list, making sure that the saved values won't change if > you update the dict? > You tell Roelof that the dictionary must be created at every loop, but > if so, where goes the elegance of > myDictList.append(UpdateIt(myDict)) > ??? > > Francesco (puzzled) > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Hello Franceso, Thank you for the answer. Now find ot how i can find the dict which contains a team. I thinking now of something like this. teller = 1 For wedstrijd in tournooi : if wedstrijd['thuis'] != stand ['ploeg'] : teller = teller + 1 stand[teller]['wedstrijd'] += 1 Could this work ? Roelof From matlocs at odscompanies.com Fri Oct 8 17:32:18 2010 From: matlocs at odscompanies.com (Shawn Matlock) Date: Fri, 8 Oct 2010 08:32:18 -0700 Subject: [Tutor] Non-ASCII In-Reply-To: References: <1160CE2227C1694BA39144335BC3502536077CEFCC@MAIL.pdx.odshp.com> Message-ID: <1160CE2227C1694BA39144335BC3502536077CF364@MAIL.pdx.odshp.com> Evert, You're guess was right, copying the code from the email message worked. My IDE - Eclipse/PyDev - must have been hiding the control character. I had tried copying my code out to a text editor and back before asking for help, but it didn't work. Perhaps I didn't "select all" before pasting the code back. Thank you for the explanation. Shawn -----Original Message----- From: Evert Rol [mailto:evert.rol at gmail.com] Sent: Thursday, October 07, 2010 12:34 PM To: Shawn Matlock Cc: tutor at python.org Subject: Re: [Tutor] Non-ASCII > I'm going through an online tutorial for Jython (www.jython.org). I can't find a place to ask a question on that site so I thought I'd try here. I believe the code is supposed to traverse a directory, identifying file types. The script is failing with the following message: > > File "", line None > SyntaxError: Non-ASCII character in file 'H:\workspace\test\src\root\nested\example.py', but no encoding declared; see http://www.python.org/peps/pep-0263.html for details Normally, this (obviously) means you have some non-ASCII characters (accented characters would already do this) in your source code. Since this is just a byte code at the lowest level, it needs to be decoded into something sensible, which you specify using an encoding. Since Python refuses to guess which encoding you want to use (the same character code could result in totally different characters for different encodings), it's bailing out instead of continuing (the exception being that by default, the Python interpreter assumes your encoding is ASCII, and happily proceeds with that until it comes across a character code outside the ASCII range). In your code below, however, I don't see any non-ASCII characters. So my best guess is that there is some invisible control-character outside the ASCII range that's causing this error message. This could happen because of a particular type of text-editor/IDE you're using. The odd thing I find about the SyntaxError is actually that there is no line number mentioned. In fact, 'File "", line None' is a very weird indication for telling where the error occurred. Might again be something to do with your IDE. I've tried your code by simply copy-pasting, and that works fine for me. So the control-character didn't reach my mail program. In which case you could try to copy-past the code from this reply into a new file and see if that's gotten rid of the control-character (presuming that caused the problem). Of course, if it's eg a known problem in Jython, I can't really tell you, because I simply don't know Jython. But try clean code for a start. HTH, Evert > > Thank you, > Shawn > > import os, sys from stat import * def walktree(top, callback): '''recursively descend the directory tree rooted at top, calling the callback function for each regular file''' for f in os.listdir(top): pathname = os.path.join(top, f) mode = os.stat(pathname)[ST_MODE] if S_ISDIR(mode): # It's a directory, recurse into it walktree(pathname, callback) elif S_ISREG(mode): # It's a file, call the callback function callback(pathname) else: # Unknown file type, print a message print 'Skipping %s' % pathname def visitfile(file): print 'visiting', file if __name__ == '__main__': walktree(sys.argv[1], visitfile) > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From taserian at gmail.com Fri Oct 8 18:05:47 2010 From: taserian at gmail.com (taserian) Date: Fri, 8 Oct 2010 12:05:47 -0400 Subject: [Tutor] PYTHON QUOTES ISSUE In-Reply-To: References: Message-ID: On Fri, Oct 8, 2010 at 10:27 AM, Susana Iraiis Delgado Rodriguez < susana.delgado_s at utzmg.edu.mx> wrote: > Hi Alan: > > The ouput is coming from a cicle and some functions that I vae to do to > execute an ogr2ogr command, in this output I ask the user for the name of a > file and then make a module to get to the subprocess part: > > ##Eliminate duplicate lines from the txt into a new txt, status:100% > a = open ("capas.txt","r") > catalogo = open ("unico.txt","w") > unique = set(a.read().split("\n")) > catalogo.write("".join([line + "\n" for line in unique])) > catalogo.close() > a.close() > I don't see how this is eliminating duplicate lines. To me it looks like it's just copying the contents of capas.txt into unico.txt and adding extra \n (line breaks), which might be what's breaking your next line. > > ##Execute ogr2ogr command, status:75% > for line in open("unico.txt", "r"): > p = subprocess.Popen(['C:/Archivos de > programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', > "\"LAYER='"+line+"'\"" , b+'.shp']) > > But when I executed it shows me an error in the layer's name: > > >>> ERROR 1: Failed to identify field:LAYER= > ERROR 1: Failed to create file .shp file. > ERROR 4: Failed to open Shapefile `0 > .shp'. > > I think the erros showed up because some of the layer's values are 0 and ' > ', and obsviously you can't create a file from nothing on it. But I don`t > know how to validate if a layer's value is equals to 0 or ' ', any idea what > I'm doing wrong or how to fix it? > Also, in your next e-mail, I noticed you're getting carriage returns (line breaks in your output). Since you're adding those line breaks above, you want to remove them before using them somewhere else. p = subprocess.Popen(['C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr', line.replace("\n", "") +'.shp', '-where', "\"LAYER='"+ line.replace("\n", "") +"'\"" , b+'.shp']) In the code above, I've changed the references to the variable *line* so that the \n's are replaced by 0-length strings. That should remove them from the Popen arguments. Antonio Rodriguez -------------- next part -------------- An HTML attachment was scrubbed... URL: From fal at libero.it Fri Oct 8 19:00:35 2010 From: fal at libero.it (Francesco Loffredo) Date: Fri, 08 Oct 2010 19:00:35 +0200 Subject: [Tutor] FW: list of dict question In-Reply-To: References: , , <4CAF0314.3040303@libero.it>, Message-ID: <4CAF4E33.5020100@libero.it> On 08/10/2010 16.54, Roelof Wobben wrote: > ... > Hello Franceso, > > Thank you for the answer. You're welcome. > Now find ot how i can find the dict which contains a team. > I thinking now of something like this. > > teller = 1 > For wedstrijd in tournooi : > if wedstrijd['thuis'] != stand ['ploeg'] : > teller = teller + 1 > stand[teller]['wedstrijd'] += 1 > > Could this work ? I'm afraid it cannot, Roelof. In your loop, you are searching many teams (if I translated well, I don't even know what language you speak) in the wrong place. If you try the following at a Python shell prompt: >>> stand = [{'punten': 2, 'tegen': 40, 'wedstrijden': 1, 'voor': 80, 'ploeg': 'C'}, {'punten': 0, 'tegen': 80, 'wedstrijden': 1, 'voor': 40, 'ploeg': 'D'}, {'punten': 2, 'tegen': 40, 'wedstrijden': 1, 'voor': 80, 'ploeg': 'C'}, {'punten': 0, 'tegen':80, 'wedstrijden': 1, 'voor': 40, 'ploeg': 'D'}] >>> stand['ploeg'] Traceback (most recent call last): File "", line 1, in TypeError: list indices must be integers, not str >>> you'll see that stand['ploeg'] doesn't exist. What does exist is, for example, stand[0]['ploeg'], or stand[3]['ploeg']. This is because stand is a list, and it just contains dictionaries. See my previous example with apples and baskets. So, if you are searching stand for all elements NOT CONTAINING a given team, you should search wedstrijd['thuis'] in the 'ploeg' element of EACH element of stand, something like: teller = 1 for wedstrijd in tournooi: for roelof in range(len(stand)): if wedstrijd['thuis'] != stand[roelof]['ploeg']: teller = teller + 1 > stand[teller]['wedstrijd'] += 1 This cannot work, either, because no element in stand contains an element whose key is 'wedstrijd', and I don't understand why you should update that element of stand whose index is teller. In this program, teller ends up containing the TOTAL number of elements in stand not containing the "thuis" of EACH "wedstrijd" of turnooi. I don't understand what do you want to do with this statement. It's very probable that teller becomes much larger than len(stand), and so stand[teller] will raise an exception... > > Roelof Francesco -------------- next part -------------- Nessun virus nel messaggio in uscita. Controllato da AVG - www.avg.com Versione: 9.0.862 / Database dei virus: 271.1.1/3182 - Data di rilascio: 10/07/10 08:34:00 From steve at pearwood.info Fri Oct 8 19:57:56 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 9 Oct 2010 04:57:56 +1100 Subject: [Tutor] IDE for Python In-Reply-To: References: Message-ID: <201010090457.57712.steve@pearwood.info> On Fri, 8 Oct 2010 02:23:27 am Juan Jose Del Toro wrote: > Dear List; > > In your experience what is the best IDE for Python? None of them. I use a good editor in one window (I prefer Kate for larger projects, although Kwrite is good enough for single modules or scripts) and a good xterm in another. I can open as many tabs as I need in the xterm. I have at least one interactive Python session open, plus another tab for running unit tests or doc tests. The only thing I miss having is integrated version control, and consequently I tend to be lazy and avoid using it unless forced. -- Steven D'Aprano From susana.delgado_s at utzmg.edu.mx Fri Oct 8 21:34:44 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Fri, 8 Oct 2010 14:34:44 -0500 Subject: [Tutor] WRITING XLS FROM OS.WALK() Message-ID: Hello members: I developed a Python module to make a list which contains all the files ending with .shp and .dbf extensions, I have solved this already, but now I want to write an excel file from it. The file should show the full path from the found files. This is the code: import os a = open ("directorio.xls","w") allfiles = [] #store all files found for root,dir,files in os.walk("C:\\"): filelist = [ os.path.join(root,fi) for fi in files if fi.endswith(".shp") or fi.endswith(".dbf") ] for f in filelist: allfiles.append(f) for i in allfiles: print i a.write(i) a.write("\n") With the code above, I have the print and the .xls file with this information in it, the issue here is that my file doesn't have the complete information that I got in the console. Any idea? The last line from excel is C:\Python26 -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Fri Oct 8 23:08:29 2010 From: wprins at gmail.com (Walter Prins) Date: Fri, 8 Oct 2010 22:08:29 +0100 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: References: Message-ID: On 8 October 2010 20:34, Susana Iraiis Delgado Rodriguez < susana.delgado_s at utzmg.edu.mx> wrote: > Hello members: > I developed a Python module to make a list which contains all the files > ending with .shp and .dbf extensions, I have solved this already, but now I > want to write an excel file from it. The file should show the full path from > the found files. This is the code: > > import os > a = open ("directorio.xls","w") > Excel (.xls) is not a text format. What you've written opens "directorio.xls" as a text file. If you want to write an Excel format file, have a look at the xlwt (and the xlrt for reading) Python modules. See http://www.python-excel.org/ Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From waynejwerner at gmail.com Fri Oct 8 23:44:46 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Fri, 8 Oct 2010 16:44:46 -0500 Subject: [Tutor] Rounding a Python float to the nearest half integer In-Reply-To: References: Message-ID: On Fri, Oct 8, 2010 at 7:58 AM, Sithembewena Lloyd Dube wrote: > I realise that one cannot have a half integer :) I meant how would one > round off to the first decimal nearest to either 0.5, or a whole number. > > Ugh...does anyone get what I'm trying to articulate? :) sample input/output cases are always useful. -Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Sat Oct 9 00:05:52 2010 From: emile at fenx.com (Emile van Sebille) Date: Fri, 08 Oct 2010 15:05:52 -0700 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: References: Message-ID: On 10/8/2010 12:34 PM Susana Iraiis Delgado Rodriguez said... > Hello members: > I developed a Python module to make a list which contains all the files > ending with .shp and .dbf extensions, I have solved this already, but now I > want to write an excel file from it. The file should show the full path from > the found files. This is the code: > > import os > a = open ("directorio.xls","w") > allfiles = [] #store all files found > for root,dir,files in os.walk("C:\\"): > filelist = [ os.path.join(root,fi) for fi in files if > fi.endswith(".shp") or fi.endswith(".dbf") ] > for f in filelist: > allfiles.append(f) > for i in allfiles: > print i > a.write(i) > a.write("\n") > > With the code above, I have the print and the .xls file with > this information in it, the issue here is that my file doesn't have the > complete information that I got in the console. Any idea? The last line from > excel is C:\Python26 > You may find that finishing with a.flush() and a.close() fixes your problem. Also, it appears that you're doing more than is required -- specifically, looping through filelist appending its members to allfiles can be done more simply with append, so where you're saying: filelist = [... you could say allfiles.extend([... and forget about filelist entirely. HTH, Emile From hgfernan at gmail.com Sat Oct 9 00:41:01 2010 From: hgfernan at gmail.com (Hilton Fernandes) Date: Fri, 8 Oct 2010 19:41:01 -0300 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: References: Message-ID: Hi ! Being Python as rich in libraries, probably there's already a library to create .XLS files. Before finding that, you can try the CSV format: simply put a comma to separate any fields you want in your values. And of course, a comma will finish your line. That way, Excel or any other spreadsheet program will understand what you want them to understand. All the best, hilton On Fri, Oct 8, 2010 at 6:08 PM, Walter Prins wrote: > > > On 8 October 2010 20:34, Susana Iraiis Delgado Rodriguez > wrote: >> >> Hello members: >> I developed a Python module to make a list which contains all the files >> ending with .shp and .dbf extensions, I have solved this already, but now I >> want to write an excel file from it. The file should show the full path from >> the found files. This is the code: >> >> import os >> a = open ("directorio.xls","w") > > > Excel (.xls) is not a text format.? What you've written opens > "directorio.xls" as a text file. > > If you want to write an Excel format file, have a look at the xlwt (and the > xlrt for reading) Python modules. See http://www.python-excel.org/ > > Walter > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > From steve at pearwood.info Sat Oct 9 05:55:47 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 9 Oct 2010 14:55:47 +1100 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: References: Message-ID: <201010091455.48393.steve@pearwood.info> On Sat, 9 Oct 2010 06:34:44 am Susana Iraiis Delgado Rodriguez wrote: > Hello members: > I developed a Python module to make a list which contains all the > files ending with .shp and .dbf extensions, I have solved this > already, I'm sorry to tell you that you've just reinvented the wheel. This was already solved, a long, long time ago. It is called the glob module: >>> import glob >>> glob.glob("/home/steve/*.jpg") ['/home/steve/hoversonic.jpg', '/home/steve/seperated_at_birth.jpg'] >>> glob.glob("/home/steve/*.txt") ['/home/steve/woss.txt', '/home/steve/file.txt', '/home/steve/post.txt'] You should use that. It works, it is tested and thoroughly debugged, and it is powerful. > but now I want to write an excel file from it. The file > should show the full path from the found files. Excel files are a proprietary, secret, binary file format. There is a Python project to allow reading and writing Excel files, but since it has to reverse-engineer the secret format, there's no guarantee that it will work. Having said that, I believe that it is very reliable, but I've never used it myself. Google on "python read write excel files" for more information. However, if your only aim is to make the data available to Excel, and you don't care what sort of file you use, the best way is the standard interchange format between spreadsheet applications, the comma- separated value file, or CSV. This is a plain-text file, and Python comes with a module for reading and writing them. From the interactive interpreter, run this for more information: import csv help(csv) > This is the code: > > import os > a = open ("directorio.xls","w") Just because you name a file .xls doesn't make it an actual Excel file, any more than taking a JPEG and renaming it "word.exe" would turn it into the Microsoft Word application. > allfiles = [] #store all files found > for root,dir,files in os.walk("C:\\"): > filelist = [ os.path.join(root,fi) for fi in files if > fi.endswith(".shp") or fi.endswith(".dbf") ] This isn't your actual code. I know this, because the indentation is broken and it gives a syntax error. > for f in filelist: > allfiles.append(f) This is better written as: allfiles.extend(filelist) but of course it is better to use the glob module. > for i in allfiles: > print i > a.write(i) > a.write("\n") The name "i" is normally used for integers, not file names. It would be better to write that as: for filename in allfiles: print filename a.write(filename + '\n') -- Steven D'Aprano From smokefloat at gmail.com Sat Oct 9 06:15:35 2010 From: smokefloat at gmail.com (David Hutto) Date: Sat, 9 Oct 2010 00:15:35 -0400 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: <201010091455.48393.steve@pearwood.info> References: <201010091455.48393.steve@pearwood.info> Message-ID: On Fri, Oct 8, 2010 at 11:55 PM, Steven D'Aprano wrote: > On Sat, 9 Oct 2010 06:34:44 am Susana Iraiis Delgado Rodriguez wrote: >> Hello members: >> I developed a Python module to make a list which contains all the >> files ending with .shp and .dbf extensions, I have solved this >> already, > > > I'm sorry to tell you that you've just reinvented the wheel. This was > already solved, a long, long time ago. It is called the glob module: Hey, buddy pal. Isn't it true that newbs should take of advantage of the fact that you have to solve the problem pythonically(as in by yourself), even if the function already exists? If I get the gist of thinikig like a programmer. > >>>> import glob >>>> glob.glob("/home/steve/*.jpg") > ['/home/steve/hoversonic.jpg', '/home/steve/seperated_at_birth.jpg'] >>>> glob.glob("/home/steve/*.txt") > ['/home/steve/woss.txt', '/home/steve/file.txt', '/home/steve/post.txt'] > > > You should use that. It works, it is tested and thoroughly debugged, and > it is powerful. Certainly so, but not as powerful as the individual's ingenuity in solving the problem at hand without foreknowledge of the 'known' solution. > > >> but now I want to write an excel file from it. The file >> should show the full path from the found files. > > > Excel files are a proprietary, secret, binary file format. There is a > Python project to allow reading and writing Excel files, but since it > has to reverse-engineer the secret format, there's no guarantee that it > will work. Having said that, I believe that it is very reliable, but > I've never used it myself. > > Google on "python read write excel files" for more information. > > However, if your only aim is to make the data available to Excel, and > you don't care what sort of file you use, the best way is the standard > interchange format between spreadsheet applications, the comma- > separated value file, or CSV. This is a plain-text file, and Python > comes with a module for reading and writing them. From the interactive > interpreter, run this for more information: > > import csv > help(csv) > > > >> This is the code: >> >> import os >> a = open ("directorio.xls","w") > > Just because you name a file .xls doesn't make it an actual Excel file, > any more than taking a JPEG and renaming it "word.exe" would turn it > into the Microsoft Word application. > > >> allfiles = [] #store all files found >> ? ? ?for root,dir,files in os.walk("C:\\"): >> ? ? ? ? ? ?filelist = [ os.path.join(root,fi) for fi in files if >> fi.endswith(".shp") or fi.endswith(".dbf") ] > > > This isn't your actual code. I know this, because the indentation is > broken and it gives a syntax error. > >> ? ? ? ? ? ?for f in filelist: >> ? ? ? ? ? ? ? ? allfiles.append(f) > > This is better written as: > > allfiles.extend(filelist) > > > but of course it is better to use the glob module. > >> for i in allfiles: >> ? ? ? print i >> ? ? ? a.write(i) >> ? ? ? a.write("\n") > > The name "i" is normally used for integers, not file names. It would be > better to write that as: > > for filename in allfiles: > ? ?print filename > ? ?a.write(filename + '\n') > > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > No problems here yet though buddy. From steve at pearwood.info Sat Oct 9 08:12:36 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 9 Oct 2010 17:12:36 +1100 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: References: <201010091455.48393.steve@pearwood.info> Message-ID: <201010091712.38651.steve@pearwood.info> On Sat, 9 Oct 2010 03:15:35 pm you wrote: > > You should use that. It works, it is tested and thoroughly > > debugged, and it is powerful. > > Certainly so, but not as powerful as the individual's ingenuity in > solving the problem at hand without foreknowledge of the 'known' > solution. I suppose you made your own computer, smelting your own ores to get the metals and creating your own plastics from oil you dug up yourself, right? And then you wrote your own operating system, and wrote your own programming language which just happened to be exactly the same as Python in every possible way. My uncle once went to the doctor complaining about general ill-health. The doctor sent him to a specialist, who examined him for five minutes, ran a blood sample through a little hand-held device, and two minutes later said "You've got such-and-such a disease. Here's my bill for $500." My uncle got all indignant. "$500? You've hardly done anything! Why should you get so much just because you've got a tool that does the work for you?" The specialist replied "The bill is $10 for my time, and $490 for knowing which was the right tool to use." -- Steven D'Aprano From smokefloat at gmail.com Sat Oct 9 08:23:38 2010 From: smokefloat at gmail.com (David Hutto) Date: Sat, 9 Oct 2010 02:23:38 -0400 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: <201010091712.38651.steve@pearwood.info> References: <201010091455.48393.steve@pearwood.info> <201010091712.38651.steve@pearwood.info> Message-ID: On Sat, Oct 9, 2010 at 2:12 AM, Steven D'Aprano wrote: > On Sat, 9 Oct 2010 03:15:35 pm you wrote: > >> > You should use that. It works, it is tested and thoroughly >> > debugged, and it is powerful. >> >> Certainly so, but not as powerful as the individual's ingenuity in >> solving the problem at hand without foreknowledge of the 'known' >> solution. > > I suppose you made your own computer, smelting your own ores to get the > metals and creating your own plastics from oil you dug up yourself, Sometimes breaking it down to bear essentials is wht you need, and sometimes, you need to take advantage of the 'frosting' a language provides > right? And then you wrote your own operating system, and wrote your own > programming language which just happened to be exactly the same as > Python in every possible way. Not in every way. but a way that makes sense to a demographic, just the way python overwrites(wraps faster languages within it's compiled, then .pyc's it to compiled), several other languages that are the same or even more advocated to the new computer scientist. > > My uncle once went to the doctor complaining about general ill-health. > The doctor sent him to a specialist, who examined him for five minutes, > ran a blood sample through a little hand-held device, and two minutes > later said "You've got such-and-such a disease. Here's my bill for > $500." Specialists always charge extra, and this is new how? > > My uncle got all indignant. "$500? You've hardly done anything! Why > should you get so much just because you've got a tool that does the > work for you?" > > The specialist replied "The bill is $10 for my time, and $490 for > knowing which was the right tool to use." A hammer can do the same in some instances, just the same as a pair of wire cutters tapping the same nail in. Both get the job done, but experience dictates the knowledge one uses for the fastest result, eg. the price one is paid for the time they can do it in->project vs per hour. > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From smokefloat at gmail.com Sat Oct 9 08:32:29 2010 From: smokefloat at gmail.com (David Hutto) Date: Sat, 9 Oct 2010 02:32:29 -0400 Subject: [Tutor] Interpolation Message-ID: Maybe I missed it, even in the google searches/manuals, but does someone know of an introduction to python interpolation that show the different forms of % , as in %s = string, and %d = digit(i think, or correct me). TIA, David From smokefloat at gmail.com Sat Oct 9 08:39:27 2010 From: smokefloat at gmail.com (David Hutto) Date: Sat, 9 Oct 2010 02:39:27 -0400 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: References: <201010091455.48393.steve@pearwood.info> <201010091712.38651.steve@pearwood.info> Message-ID: Here's a little 'anomaly' though, programmers say hammer when the truth is, that modern construction should use pneumatic tools, as in air hammer. From fal at libero.it Sat Oct 9 09:05:57 2010 From: fal at libero.it (Francesco Loffredo) Date: Sat, 09 Oct 2010 09:05:57 +0200 Subject: [Tutor] list of dict question In-Reply-To: <4CAF0314.3040303@libero.it> References: <4CAF0314.3040303@libero.it> Message-ID: <4CB01455.4000708@libero.it> Alan's answer to Roelof made me think... On 08/10/2010 13.40, Francesco Loffredo wrote: > Il 08/10/2010 10.02, Alan Gauld ha scritto: >> >> "Roelof Wobben" wrote >> >>> I have this programm : >>> >>> tournooi = [{'thuis': 'A','uit': "B",'thuisscore': 20, 'uitscore': >> ... This was your answer to Roelof: >> On the next iteration you overwrite those two dictionaries >> with new values then append them to the list again. >> So you wind up with 2 copies of the updated dictionaries. >> ... > This is difficult for me too: why does this happen? Or, more correctly, > why should this happen? How can you save the current contents of a > dictionary in a list, making sure that the saved values won't change if > you update the dict? > You tell Roelof that the dictionary must be created at every loop, but > if so, where goes the elegance of > myDictList.append(UpdateIt(myDict)) > ??? > > Francesco (puzzled) -------------- next part -------------- Nessun virus nel messaggio in uscita. Controllato da AVG - www.avg.com Versione: 9.0.862 / Database dei virus: 271.1.1/3184 - Data di rilascio: 10/08/10 08:34:00 From fal at libero.it Sat Oct 9 09:33:02 2010 From: fal at libero.it (Francesco Loffredo) Date: Sat, 09 Oct 2010 09:33:02 +0200 Subject: [Tutor] FW: list of dict question In-Reply-To: References: , , , <4CAF0314.3040303@libero.it>, , , , <4CAF4E33.5020100@libero.it> Message-ID: <4CB01AAE.3060900@libero.it> On 08/10/2010 19.20, Roelof Wobben wrote: >... > Oke, > > What I try to achieve is this : > > 1) Look if a team is known in stand. > 2) If no, take care that the team is known in stand (this part I have written with your help) > 3) if yes, make wedstrijden one more so wedstrijden is equal to number of played games of that team > and update the rest of the data > > With 3 I was thinking about this steps. > > 1) First search where the team is in stand. You are very near to this: when you searched the team in stand, you could as well take note of what was your index in stand at the point where you found it. If you didn't find it, you have just added that team to stand, so its position must be stand[len(stand)-1] (here a point about multi-user environments and the possibility of stand being modified on-the-fly should be added, but let's ignore it for now) > 2) Update the data Once you have a position, AND THE NUMBER OF PLAYED GAMES, this is trivial: stand[position][wedstrijden] = numPlayedGames You shoul NOT add one to wedstrijden, because every time that you run your program, for whatever reason, you are updating the record. And maybe you just wanted to test it! If you count the number of played games every time, you are sure that the correct number is written on stand, even if you run the program thousands of times. I imagine that you can count the played games searching in tournooi, but I'm not sure of it. You should keep that count somewhere. > > Roelof Francesco -------------- next part -------------- Nessun virus nel messaggio in uscita. Controllato da AVG - www.avg.com Versione: 9.0.862 / Database dei virus: 271.1.1/3184 - Data di rilascio: 10/08/10 08:34:00 From steve at pearwood.info Sat Oct 9 09:37:13 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 9 Oct 2010 18:37:13 +1100 Subject: [Tutor] list of dict question In-Reply-To: <4CB01455.4000708@libero.it> References: <4CAF0314.3040303@libero.it> <4CB01455.4000708@libero.it> Message-ID: <201010091837.14448.steve@pearwood.info> On Sat, 9 Oct 2010 06:05:57 pm Francesco Loffredo wrote: > Alan's answer to Roelof made me think... I'm sorry, I don't know what your question is. You seem to have quoted various bits and pieces of text from earlier emails (text beginning with > signs). Apart from the sentence beginning with "Alan's answer to Roelof...", and another sentence "This was your answer to Roelof", everything in your post was a quote (started with a > sign). So what is your question about lists of dicts? -- Steven D'Aprano From steve at pearwood.info Sat Oct 9 09:41:11 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 9 Oct 2010 18:41:11 +1100 Subject: [Tutor] Interpolation In-Reply-To: References: Message-ID: <201010091841.11815.steve@pearwood.info> On Sat, 9 Oct 2010 05:32:29 pm David Hutto wrote: > Maybe I missed it, even in the google searches/manuals, but does > someone know of an introduction to python interpolation that show the > different forms of % > , as in %s = string, and %d = digit(i think, or correct me). http://docs.python.org/library/stdtypes.html#string-formatting -- Steven D'Aprano From fal at libero.it Sat Oct 9 09:54:56 2010 From: fal at libero.it (Francesco Loffredo) Date: Sat, 09 Oct 2010 09:54:56 +0200 Subject: [Tutor] list of dict question In-Reply-To: <201010091837.14448.steve@pearwood.info> References: <4CAF0314.3040303@libero.it> <4CB01455.4000708@libero.it> <201010091837.14448.steve@pearwood.info> Message-ID: <4CB01FD0.2050400@libero.it> On 09/10/2010 9.37, Steven D'Aprano wrote: > On Sat, 9 Oct 2010 06:05:57 pm Francesco Loffredo wrote: >> Alan's answer to Roelof made me think... > > I'm sorry, I don't know what your question is. You seem to have quoted > various bits and pieces of text from earlier emails (text beginning > with> signs). Apart from the sentence beginning with "Alan's answer to > Roelof...", and another sentence "This was your answer to Roelof", > everything in your post was a quote (started with a> sign). So what is > your question about lists of dicts? Right, I forgot to delete the > signs... I was re-posting a question I asked (to Alan) in a previous post (while answering to Roelof). Here is my question: > ... > On the next iteration you overwrite those two dictionaries > with new values then append them to the list again. > So you wind up with 2 copies of the updated dictionaries. > ... This is difficult for me too: why does this happen? Or, more correctly, why should this happen? How can you save the current contents of a dictionary in a list, making sure that the saved values won't change if you update the dict? You tell Roelof that the dictionary must be created at every loop, but if so, where goes the elegance of myDictList.append(UpdateIt(myDict)) ??? Francesco -------------- next part -------------- Nessun virus nel messaggio in uscita. Controllato da AVG - www.avg.com Versione: 9.0.862 / Database dei virus: 271.1.1/3184 - Data di rilascio: 10/08/10 08:34:00 From alan.gauld at btinternet.com Sat Oct 9 10:12:28 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 9 Oct 2010 09:12:28 +0100 Subject: [Tutor] WRITING XLS FROM OS.WALK() References: <201010091455.48393.steve@pearwood.info> Message-ID: "David Hutto" wrote > > I'm sorry to tell you that you've just reinvented the wheel. This > > was > > already solved, a long, long time ago. It is called the glob > > module: > > Hey, buddy pal. Isn't it true that newbs should take of advantage > of > the fact that you have to solve the problem pythonically Thats a matter of great dispute. There is an old school that says you should learn to program in assembler (or even microcode) then move to C and then to Python(or similar) and finally to 4G languages. Then there is a new school that says life is too short, learn to program like a professional - use the highest level language you can and leverage the libraries. Personally I would say for the hobbyist, new-school is best. The real skill to learn is finding the best library and figuring out how to use it. [For a pro doing a 4 year computing course then there is still a lot to be said in starting from scratch and building up - they may have to do it that way someday on new hardware or creating a new OS, but an amateur is never likely to be building from scratch...] > > You should use that. It works, it is tested and thoroughly > > debugged, and > > it is powerful. > > Certainly so, but not as powerful as the individual's ingenuity in > solving the problem at hand without foreknowledge of the 'known' > solution. That depends on how good the student is. It is very unlikely that the student will come up with anything close to globs power and flexibility on their first attempt. So if they want a learning exercise it might be an interesting task, but if they want to actually achieve a result they should use glob. Software reuse is a big issue in the industry just now and a lot of effort is being spent in getting software engineers out of the "not invented here" mentality and into the reuse mentality. So encouraging beginners to get into the habit of "scavenge and adapt" is actually in line with current industry thinking. Just as an alternative view... :-) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sat Oct 9 10:19:06 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 9 Oct 2010 09:19:06 +0100 Subject: [Tutor] WRITING XLS FROM OS.WALK() References: <201010091455.48393.steve@pearwood.info> <201010091712.38651.steve@pearwood.info> Message-ID: "Steven D'Aprano" wrote > The specialist replied "The bill is $10 for my time, and $490 for > knowing which was the right tool to use." Interesting modern variant on the version I heard.... A man's washing machine broke down so he called the plumber. The plumber looks at it for a few minutes then takes a hammer out of his toolkit. He gently taps a fixing and the machine starts working. He presents a bill for $50. The man is shocked, you only tapped it with a hammer how can you justify that? The plumber amends the bill: $1 for hitting with hammer, $49 for knowing where to hit, and how hard. Alan G. From alan.gauld at btinternet.com Sat Oct 9 10:25:21 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 9 Oct 2010 09:25:21 +0100 Subject: [Tutor] list of dict question References: <4CAF0314.3040303@libero.it><4CB01455.4000708@libero.it><201010091837.14448.steve@pearwood.info> <4CB01FD0.2050400@libero.it> Message-ID: "Francesco Loffredo" wrote > > On the next iteration you overwrite those two dictionaries > > with new values then append them to the list again. > > So you wind up with 2 copies of the updated dictionaries. > > ... > This is difficult for me too: why does this happen? Or, more > correctly, > why should this happen? It happens because you can't store two objects in one. There is only one dictionary. If you change its value you change its value. > How can you save the current contents of a dictionary in a list, > making sure that the saved values won't change if you update the > dict? You need to save a copy of the dictionary. ie Create a new dictionary. If you put a box of white eggs in your shopping basket you cannot put brown eggs into that box and expect to still have the white ones as well. You need to get two boxes! > You tell Roelof that the dictionary must be created at every loop, > but > if so, where goes the elegance of > myDictList.append(UpdateIt(myDict)) I don't understand what you mean here. What elegance? Can you give a slightly fuller example? You can update an existing dictionary in a list, but it doesn't preserve the original vesion any more than replacing white eggs with brown preserves the white ones. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From jed at jedsmith.org Fri Oct 8 23:02:18 2010 From: jed at jedsmith.org (Jed Smith) Date: Fri, 8 Oct 2010 17:02:18 -0400 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: References: Message-ID: On Fri, Oct 8, 2010 at 3:34 PM, Susana Iraiis Delgado Rodriguez wrote: > for i in allfiles: > ???? ?print i > ????? a.write(i) > ????? a.write("\n") Are you sure you want to write bare data to a file ending in .xls? That might be confusing Excel, unless it has grown a sizable brain with respect to file handling. You might investigate the csv module that Python offers [1], and use it to write CSV. Excel can import that very easily. Hell, simply writing a list of filenames delimited by \n might be importable into Excel. [1]: http://docs.python.org/library/csv.html -- Jed Smith jed at jedsmith.org From smokefloat at gmail.com Sat Oct 9 16:56:49 2010 From: smokefloat at gmail.com (David Hutto) Date: Sat, 9 Oct 2010 10:56:49 -0400 Subject: [Tutor] Interpolation In-Reply-To: <201010091841.11815.steve@pearwood.info> References: <201010091841.11815.steve@pearwood.info> Message-ID: Thanks buddy pal. From smokefloat at gmail.com Sat Oct 9 18:13:42 2010 From: smokefloat at gmail.com (David Hutto) Date: Sat, 9 Oct 2010 12:13:42 -0400 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: References: <201010091455.48393.steve@pearwood.info> Message-ID: On Sat, Oct 9, 2010 at 4:12 AM, Alan Gauld wrote: > "David Hutto" wrote > >> > I'm sorry to tell you that you've just reinvented the wheel. This > was >> > already solved, a long, long time ago. It is called the glob > module: >> >> Hey, ?buddy pal. Isn't it true that newbs should take of advantage of >> the fact that you have to solve the problem pythonically > > Thats a matter of great dispute. There is an old school that says you > should learn to program in assembler (or even microcode) then move > to C and then to Python(or similar) and finally to 4G languages. Even old schooler though would be that we're just directing electrical flow from an ac outlet through a dc converter and streaming it through the circuit board though. Which is not as easy as it sounds! > Then there is a new school that says life is too short, learn to > program like a professional - use the highest level language you > can and leverage the libraries. > > Personally I would say for the hobbyist, new-school is best. The real > skill to learn is finding the best library and figuring out how to use it. But I think higher level should be for productivity, but low level for higher knowledge of the productivity. > [For a pro doing a 4 year computing course then there is still a lot > to be said in starting from ?scratch and building up - they may > have to do it that way someday on new hardware or creating a new OS, > but an amateur is never likely to be building from scratch...] > Unless you're a technological masochist. >> > You should use that. It works, it is tested and thoroughly > debugged, >> > and >> > ?it is powerful. >> >> Certainly so, but not as powerful as the individual's ingenuity in >> solving the problem at hand without foreknowledge of the 'known' >> solution. > > That depends on how good the student is. It is very unlikely > that the student will come up with anything close to globs > power and flexibility on their first attempt. So if they want a learning > exercise it might be an interesting task, but if they want to actually > achieve a result they should use glob. > > Software reuse is a big issue in the industry just now and a lot > of effort is being spent in getting software engineers out of the > "not invented here" mentality and into the reuse mentality. So > encouraging beginners to get into the habit of "scavenge and > adapt" is actually in line with current industry thinking. Then I'm apparently an industry thinker. > > Just as an alternative view... :-) > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From smokefloat at gmail.com Sat Oct 9 18:42:12 2010 From: smokefloat at gmail.com (David Hutto) Date: Sat, 9 Oct 2010 12:42:12 -0400 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: References: <201010091455.48393.steve@pearwood.info> Message-ID: And in other news, is it better to be a programmer, or an electrician first? From ahmedn82 at hotmail.com Sat Oct 9 18:55:46 2010 From: ahmedn82 at hotmail.com (Ahmed AL-Masri) Date: Sun, 10 Oct 2010 00:55:46 +0800 Subject: [Tutor] OpenMP Message-ID: Hi, I have searched about how to use openMP using python and I couldn't fine any helpful info. anyone can help me on this. My idea is that to use the different processer core for each section. for ex. lets say I have two loops and I want to test in which processer go through. ## core one should handle this a=[] for I in range (100): s= 10*I a.append( s) ## core two should handle this b=[] for f in range (100): h= 10*f b.append( h) ## collecting the data in one data file data=[a, b] my question is how to do that and is it possible in python to go from line to line after give the order to core one to process than we go to next core for the second process.. Really interesting to speed up the process based on no of CPUs that we have using python. Looking forward to seeing your suggestions, Best regards, Ahmed -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Sat Oct 9 18:59:26 2010 From: emile at fenx.com (Emile van Sebille) Date: Sat, 09 Oct 2010 09:59:26 -0700 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: <201010091455.48393.steve@pearwood.info> References: <201010091455.48393.steve@pearwood.info> Message-ID: On 10/8/2010 8:55 PM Steven D'Aprano said... > I'm sorry to tell you that you've just reinvented the wheel. This was > already solved, a long, long time ago. It is called the glob module: > Only if glob now descends into the file system... which is why you'd choose os.walk instead. Emile From evert.rol at gmail.com Sat Oct 9 19:07:49 2010 From: evert.rol at gmail.com (Evert Rol) Date: Sat, 9 Oct 2010 19:07:49 +0200 Subject: [Tutor] OpenMP In-Reply-To: References: Message-ID: > Hi, > I have searched about how to use openMP using python and I couldn't fine any helpful info. anyone can help me on this. No openMP, but iPython could do what you want (parallel and some distributed computing): http://ipython.scipy.org/doc/stable/html/ Evert > > My idea is that to use the different processer core for each section. > for ex. > lets say I have two loops and I want to test in which processer go through. > ## core one should handle this > a=[] > for I in range (100): > s= 10*I > a.append( s) > > ## core two should handle this > b=[] > for f in range (100): > h= 10*f > b.append( h) > > ## collecting the data in one data file > data=[a, b] > > my question is how to do that and is it possible in python to go from line to line after give the order to core one to process than we go to next core for the second process.. > > Really interesting to speed up the process based on no of CPUs that we have using python. > Looking forward to seeing your suggestions, > Best regards, > Ahmed > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From emile at fenx.com Sat Oct 9 19:10:20 2010 From: emile at fenx.com (Emile van Sebille) Date: Sat, 09 Oct 2010 10:10:20 -0700 Subject: [Tutor] OpenMP In-Reply-To: References: Message-ID: On 10/9/2010 9:55 AM Ahmed AL-Masri said... > Hi, > I have searched about how to use openMP using python and I couldn't fine any helpful info. anyone can help me on this. > > My idea is that to use the different processer core for each section. > for ex. > lets say I have two loops and I want to test in which processer go through. > ## core one should handle this > a=[] > for I in range (100): > s= 10*I > a.append( s) > > ## core two should handle this > b=[] > for f in range (100): > h= 10*f > b.append( h) > > ## collecting the data in one data file > data=[a, b] > > my question is how to do that and is it possible in python to go from line to line after give the order to core one to process than we go to next core for the second process.. > > Really interesting to speed up the process based on no of CPUs that we have using python. > Looking forward to seeing your suggestions, Each python process is limited to running on a single core, but multiple processes can run in different cores. I'm not aware of a way to direct which core a process will be run under, but perhaps there's a OS method of doing so. For more information on the python limitations, google around for python and GIL (global interpreter lock). HTH, Emile From alan.gauld at btinternet.com Sat Oct 9 19:50:40 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 9 Oct 2010 18:50:40 +0100 Subject: [Tutor] OpenMP References: Message-ID: "Ahmed AL-Masri" wrote > say I have two loops and I want to test in which processer go > through... > my question is how to do that and is it possible in python No, its not possible to do that explicitly and it shouldn't be. The OS (and interpreter if applicable) is best placed to decide how the CPUs should spend their time. An application directly controlling the CPUs is only sane if its an embedded type application in sole control of the box. As a python programmer the best you can do is give the OS some helpful hints like using threads. So in your case you could put the two loops in separate threads and hope the OS decides to run those threads on separate cores. Now, the bad news is that so far as I know the python interpreter does not expose its threading model to the OS to even if you use threads the interpreter itself will still be running on a single CPU core. :-( Or is that one of the deeper fixes in Python v3? Does anyone know? > Really interesting to speed up the process based on no > of CPUs that we have using python. Its extremely unlikely you would succeed using that approach, you are much better off focussing on the higher level optimisation of your algorithms and logical partitioning along with minmising disk and network access. These are the real performamce issues not CPU core usage. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sat Oct 9 19:54:25 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 9 Oct 2010 18:54:25 +0100 Subject: [Tutor] WRITING XLS FROM OS.WALK() References: <201010091455.48393.steve@pearwood.info> Message-ID: "David Hutto" wrote > And in other news, is it better to be a programmer, or an > electrician first? Well, I started as an electronic engineer writing software, then became a software engineer writing software for electronic engineers then gradually the software got bigger and more abstract and now I virtually never even think about the electronics.... In fact nowadays I spend most of my time discussing high level design where I'm not even thinking too much about the actual code, the focus is all on module structures, frameworks and library definitions etc. Playing with Python helps keep me grounded :-) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sat Oct 9 19:58:59 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 9 Oct 2010 18:58:59 +0100 Subject: [Tutor] WRITING XLS FROM OS.WALK() References: <201010091455.48393.steve@pearwood.info> Message-ID: "David Hutto" wrote >> should learn to program in assembler (or even microcode) then move >>to C and then to Python(or similar) and finally to 4G languages. > > Even old schooler though would be that we're just directing > electrical > flow from an ac outlet through a dc converter and streaming it > through > the circuit board though. Which is not as easy as it sounds! Yeah, but I don't think anyone would claim that was programming! :-) Although I did first learn my boolean logic by having to translate it into switch circuits - real mechanical toggle switches not electonics... AND => two switches in series OR = two switches in parallel NOT = an "off" switch - ie. on by default etc/... We then moved to relays and finally diodes and resistors... But that was training us in electronics not programming. Alan G. From smokefloat at gmail.com Sat Oct 9 20:23:57 2010 From: smokefloat at gmail.com (David Hutto) Date: Sat, 9 Oct 2010 14:23:57 -0400 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: References: <201010091455.48393.steve@pearwood.info> Message-ID: On Sat, Oct 9, 2010 at 1:58 PM, Alan Gauld wrote: > > "David Hutto" wrote >>> >>> should learn to program in assembler (or even microcode) then move >>> to C and then to Python(or similar) and finally to 4G languages. >> >> Even old schooler though would be that we're just directing electrical >> flow from an ac outlet through a dc converter and streaming it through >> the circuit board though. Which is not as easy as it sounds! > > Yeah, but I don't think anyone would claim that was programming! :-) > > Although I did first learn my boolean logic by having to translate > it into switch circuits - real mechanical toggle switches not > electonics... > AND => two switches in series > OR = two switches in parallel > NOT = an "off" switch - ie. on by default > etc/... > > We then moved to relays and finally diodes and resistors... > > But that was training us in electronics not programming. > How can you distinguish the two, once learned though? I don't know C but I do know python, and the what's below the BIOS(muy basically). > Alan G. > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From smokefloat at gmail.com Sat Oct 9 20:30:02 2010 From: smokefloat at gmail.com (David Hutto) Date: Sat, 9 Oct 2010 14:30:02 -0400 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: References: <201010091455.48393.steve@pearwood.info> Message-ID: On Sat, Oct 9, 2010 at 2:23 PM, David Hutto wrote: > On Sat, Oct 9, 2010 at 1:58 PM, Alan Gauld wrote: >> >> "David Hutto" wrote >>>> >>>> should learn to program in assembler (or even microcode) then move >>>> to C and then to Python(or similar) and finally to 4G languages. >>> >>> Even old schooler though would be that we're just directing electrical >>> flow from an ac outlet through a dc converter and streaming it through >>> the circuit board though. Which is not as easy as it sounds! >> >> Yeah, but I don't think anyone would claim that was programming! :-) >> >> Although I did first learn my boolean logic by having to translate >> it into switch circuits - real mechanical toggle switches not >> electonics... >> AND => two switches in series >> OR = two switches in parallel >> NOT = an "off" switch - ie. on by default >> etc/... >> >> We then moved to relays and finally diodes and resistors... >> >> But that was training us in electronics not programming. >> > > How can you distinguish the two, once learned though? I don't know C > but I do know python, and the what's below the BIOS(muy basically). Not How, but maybe , more importantly, why? To avoid the low-level programming confusion, or for more control over user gui's(Meaning client wants)? > > >> Alan G. >> >> >> _______________________________________________ >> Tutor maillist ?- ?Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > From smokefloat at gmail.com Sat Oct 9 20:32:42 2010 From: smokefloat at gmail.com (David Hutto) Date: Sat, 9 Oct 2010 14:32:42 -0400 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: References: <201010091455.48393.steve@pearwood.info> Message-ID: I'll put my questions in a knew thread if this is considered hijacking the OP. From alan.gauld at btinternet.com Sun Oct 10 01:38:05 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 10 Oct 2010 00:38:05 +0100 Subject: [Tutor] WRITING XLS FROM OS.WALK() References: <201010091455.48393.steve@pearwood.info> Message-ID: "David Hutto" wrote > I'll put my questions in a knew thread if this is considered > hijacking the OP. Well, it doesn''t have much to do with XLS files thats for sure so yes, if you are interested in further pursuing approaches to teaching programming a separate thread is probably better. :-) Alan G. From alan.gauld at btinternet.com Sun Oct 10 01:45:56 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Sun, 10 Oct 2010 00:45:56 +0100 (BST) Subject: [Tutor] OpenMP In-Reply-To: References: Message-ID: <985647.19911.qm@web86707.mail.ird.yahoo.com> Forwarding to the liist. Please use Reply All on responses. Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ ----- Original Message ---- > From: Ahmed AL-Masri > To: Alan Gauld > Sent: Saturday, 9 October, 2010 19:50:58 > Subject: Re: [Tutor] OpenMP > > Thanks for fast responding. I will try to use the threads and see how the > performance would be.. actually I am using that for my artificial neural > network and the problem is regarding to the ANN limitation when I used a big > no of inputs. so one way to overcome this problem is by distributing and now > I have like 3 networks in my system with slow processing. May be parallel > could have little effort. Depending on how long running these processes are you may be able to separate them out completely into separate server processes in true client server mode. Effectively creating a network process for each network then have a load balances so that each incoming request gets sent to one of the server processes. That way you can scale linearly by adding moreservers as required (the load balancer starting a new process each time the number of active requests passes a given limit.) This is how many uindustrial scale databases handle high processing loads, each SQL request is validated and if OK passed to a query server, the queries are distributed over the available servers to ensure even loading and each server can run on separate CPUs as needed (but still controlled by the OS). The downside of this is of course the complexity of writing the loadbalancer which must track all incoming requests and which server they are on and also handle the responses from the servers when complete, making sure they go back to the right source. Its a little bit like writing a web server... or at least the listener part. HTH, Alan G. From bkjones at gmail.com Sun Oct 10 03:58:36 2010 From: bkjones at gmail.com (Brian Jones) Date: Sat, 9 Oct 2010 21:58:36 -0400 Subject: [Tutor] IDE for Python In-Reply-To: References: Message-ID: On Thu, Oct 7, 2010 at 11:23 AM, Juan Jose Del Toro wrote: > Dear List; > > In your experience what is the best IDE for Python? > > I've used SPE and IDLE, I've also seen people using Eclipse but which one > do you recommend? > There is no 'best for Python'. IDEs are made to please people, not languages. I've been looking for a good Python IDE for a long time. Actually, I've been looking for the IDE kool-aid. I always felt like I was missing something because I was using vim while all the cool kids were using IDEs. So I've tried different ones, several times, over the past several years. While I still use vim quite a lot (with some plugins like a class browser), I've recently discovered that PyCharm fits my vim-altered brain quite well. It has by far the best vim emulation mode ever, it understands unit tests and will run them for you, good Git integration (and others, so I hear), and it generally does a good job of staying out of my way when I just want to dive down the rabbit hole and code. :) If it's too heavy for your taste, I used to use Komodo Edit when I wanted something more than vim. It's lighter (in features and process 'weight'), but it's pretty good. good luck. > -- > ?Saludos! / Greetings! > Juan Jos? Del Toro M. > jdeltoro1973 at gmail.com > Guadalajara, Jalisco MEXICO > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- Brian K. Jones My Blog http://www.protocolostomy.com Follow me http://twitter.com/bkjones -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sun Oct 10 04:19:18 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 10 Oct 2010 13:19:18 +1100 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: References: <201010091455.48393.steve@pearwood.info> Message-ID: <201010101319.18804.steve@pearwood.info> On Sun, 10 Oct 2010 03:59:26 am Emile van Sebille wrote: > On 10/8/2010 8:55 PM Steven D'Aprano said... > > > I'm sorry to tell you that you've just reinvented the wheel. This > > was already solved, a long, long time ago. It is called the glob > > module: > > Only if glob now descends into the file system... which is why you'd > choose os.walk instead. Do you mean you want to point at a single directory and have it search any and all subdirectories, no matter how deeply nested? Well, yes, that would be a good use-case for os.walk, and yes, I completely missed it, so I withdraw my comment that Susana had re-invented the wheel. Mea culpa, sorry Susana for any confusion I caused. -- Steven D'Aprano From ahmedn82 at hotmail.com Sun Oct 10 06:45:59 2010 From: ahmedn82 at hotmail.com (Ahmed AL-Masri) Date: Sun, 10 Oct 2010 12:45:59 +0800 Subject: [Tutor] OpenMP In-Reply-To: <985647.19911.qm@web86707.mail.ird.yahoo.com> References: <985647.19911.qm@web86707.mail.ird.yahoo.com> Message-ID: Thanks for fast responding. I will try to use the threads and see how the performance would be.. actually I am using that for my artificial neural network and the problem is regarding to the ANN limitation when I used a big no of inputs. so one way to overcome this problem is by distributing and now I have like 3 networks in my system with slow processing. May be parallel could have little effort. > Depending on how long running these processes are you may be able > to separate them out completely into separate server processes > in true client server mode. Effectively creating a network process for > each network then have a load balances so that each incoming > request gets sent to one of the server processes. That way you > can scale linearly by adding moreservers as required (the load > balancer starting a new process each time the number of active > requests passes a given limit.) This is how many uindustrial scale > databases handle high processing loads, each SQL request is validated > and if OK passed to a query server, the queries are distributed over > the available servers to ensure even loading and each server can > run on separate CPUs as needed (but still controlled by the OS). > > The downside of this is of course the complexity of writing the > loadbalancer which must track all incoming requests and which > server they are on and also handle the responses from the servers > when complete, making sure they go back to the right source. > Its a little bit like writing a web server... or at least the listener > part. > > HTH, > > Alan G. > > From alan.gauld at btinternet.com Sun Oct 10 10:17:38 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 10 Oct 2010 09:17:38 +0100 Subject: [Tutor] WRITING XLS FROM OS.WALK() References: <201010091455.48393.steve@pearwood.info> <201010101319.18804.steve@pearwood.info> Message-ID: "Steven D'Aprano" wrote >> Only if glob now descends into the file system... which is why >> you'd >> choose os.walk instead. > Do you mean you want to point at a single directory and have it > search > any and all subdirectories, no matter how deeply nested? Well, yes, > that would be a good use-case for os.walk, and yes, I completely > missed > it, so I withdraw my comment that Susana had re-invented the wheel. I think the solution should use both. os.walk to traverse the directories and glob to build the file list at each level. Each tool has its role. I didn't see anything untoward in recommending glob to replace the use of the string based list comp for selection of the files, I just assemed you meant in conjunction with os.walk!. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From emile at fenx.com Sun Oct 10 17:51:07 2010 From: emile at fenx.com (Emile van Sebille) Date: Sun, 10 Oct 2010 08:51:07 -0700 Subject: [Tutor] OpenMP In-Reply-To: References: <985647.19911.qm@web86707.mail.ird.yahoo.com> Message-ID: On 10/9/2010 9:45 PM Ahmed AL-Masri said... > > Thanks for fast responding. I will try to use the threads and see how the > performance would be.. actually I am using that for my artificial neural > network and the problem is regarding to the ANN limitation when I used a > big > no of inputs. so one way to overcome this problem is by distributing and > now > I have like 3 networks in my system with slow processing. May be parallel > could have little effort. You may also find stackless helpful at http://www.stackless.com which forms the backend of EVE Online and claims "large scale shared state simulation across tens of thousands of CPUs" Emile From ahmedn82 at hotmail.com Sun Oct 10 19:21:12 2010 From: ahmedn82 at hotmail.com (Ahmed AL-Masri) Date: Mon, 11 Oct 2010 01:21:12 +0800 Subject: [Tutor] OpenMP In-Reply-To: References: <985647.19911.qm@web86707.mail.ird.yahoo.com> Message-ID: Thanks a lots. it's really helpful,, I am reading it -------------------------------------------------- From: "Emile van Sebille" Sent: Sunday, October 10, 2010 11:51 PM To: Subject: Re: [Tutor] OpenMP > On 10/9/2010 9:45 PM Ahmed AL-Masri said... >> >> Thanks for fast responding. I will try to use the threads and see how the >> performance would be.. actually I am using that for my artificial neural >> network and the problem is regarding to the ANN limitation when I used a >> big >> no of inputs. so one way to overcome this problem is by distributing and >> now >> I have like 3 networks in my system with slow processing. May be parallel >> could have little effort. > > > You may also find stackless helpful at http://www.stackless.com which > forms the backend of EVE Online and claims "large scale shared state > simulation across tens of thousands of CPUs" > > Emile > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From fal at libero.it Sun Oct 10 20:21:28 2010 From: fal at libero.it (Francesco Loffredo) Date: Sun, 10 Oct 2010 20:21:28 +0200 Subject: [Tutor] list of dict question In-Reply-To: References: <4CAF0314.3040303@libero.it><4CB01455.4000708@libero.it><201010091837.14448.steve@pearwood.info> <4CB01FD0.2050400@libero.it> Message-ID: <4CB20428.5040309@libero.it> On 09/10/2010 10.25, Alan Gauld wrote: > "Francesco Loffredo" wrote > >> > On the next iteration you overwrite those two dictionaries >> > with new values then append them to the list again. >> > So you wind up with 2 copies of the updated dictionaries. >> > ... >> This is difficult for me too: why does this happen? Or, more correctly, >> why should this happen? > > It happens because you can't store two objects in one. > There is only one dictionary. If you change its value you > change its value. > >> How can you save the current contents of a dictionary in a list, >> making sure that the saved values won't change if you update the dict? > > You need to save a copy of the dictionary. ie Create a new dictionary. > If you put a box of white eggs in your shopping basket you cannot put > brown eggs into that box and expect to still have the white ones as well. > You need to get two boxes! I obviously haven't been clear in my question, sure I know that if you change one object you lose the previous contents, but (hope this makes my question clear) why should the append method of the list store a pointer to the dictionary, rather then a copy of that dictionary? If it did, Roelof's code would work perfectly, and you could store in a list all the subsequent changes of a dictionary without calling them with different names. For example (and this should make my previous example a bit fuller), if you had a dictionary containing the current status of a system, and you wanted to keep an history of that status (well, I'd use a file, but let's imagine you had to use a list), you could simply add the (newer version of the) dictionary to the list: >> myDictList.append(UpdateIt(myDict)) This would be much more elegant and readable than creating a different dictionary (meaning a different name) for every state of the said system, and making sure you never repeat a name twice, or you'll lose the state you saved some time ago... I understand that if .append() stored a copy of the dict in the list, you will end up with lots of copies and a huge amount of memory used by your list, but that's exactly what will happen if you make those copies yourself. But you wouldn't have to devise a way to generate a new name for the dictionary every time you need to update it. Thank you for your help Francesco -------------- next part -------------- Nessun virus nel messaggio in uscita. Controllato da AVG - www.avg.com Versione: 9.0.862 / Database dei virus: 271.1.1/3186 - Data di rilascio: 10/09/10 08:34:00 From josep.m.fontana at gmail.com Sun Oct 10 21:35:01 2010 From: josep.m.fontana at gmail.com (Josep M. Fontana) Date: Sun, 10 Oct 2010 21:35:01 +0200 Subject: [Tutor] how to extract data only after a certain condition is met Message-ID: Hi, First let me apologize for taking so long to acknowledge your answers and to thank you (Eduardo, Peter, Greg, Emile, Joel and Alan, sorry if I left anyone) for your help and your time. One of the reasons I took so long in responding (besides having gotten busy with some urgent matters related to my work) is that I was a bit embarrassed at realizing how poorly I had defined my problem. As Alan said, I should at least have told you which operations were giving me a headache. So I went back to my Python reference books to try to write some code and thus be able to define my problems more precisely. Only after I did that, I said to myself, I would come back to the list with more specific questions. The only problem is that doing this made me painfully aware of how little Python I know. Well, actually my problem is not so much that I don't know Python as that I have very little experience programming in general. Some years ago I learned a little Perl and basically I used it to do some text manipulation using regular expressions but that's all my experience. In order to learn Python, I read a book called "Beginning Python: From Novice to Professional" and I was hoping that just by starting to use the knowledge I had supposedly acquired by reading that book to solve real problems related to my project I would learn. But this turned out to be much more difficult than I had expected. Perhaps if I had worked through the excellent book/tutorial Alan has written (of which I was not aware when I started), I would be better prepared to confront this problem. Anyway (sorry for the long intro), since Emile laid out the problem very clearly, I will use his outline to point out the problems I'm having: Emile says: -------------- Conceptually, you'll need to: -a- get the list of file names to change then for each -b- determine the new name -c- rename the file For -a- you'll need glob. For -c- use os.rename. -b- is a bit more involved. To break -b- down: -b1- break out the x-xx portion of the file name -b2- look up the corresponding year in the other file -b3- convert the year to the century-half structure -b4- put the pieces together to form the new file name For -b2- I'd suggest building a dictionary from your second files contents as a first step to facilitate the subsequent lookups. --------------------- OK. Let's start with -b- . My first problem is that I don't really know how to go about building a dictionary from the file with the comma separated values. I've discovered that if I use a file method called 'readlines' I can create a list whose elements would be each of the lines contained in the document with all the codes followed by comma followed by the year. Thus if I do: fileNameCentury = open(r '/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNamesYears.txt' ).readlines() Where 'FileNamesYears.txt' is the document with the following info: A-01, 1278 A-02, 1501 ... N-09, 1384 I get a list of the form ['A-01,1374\rA-02,1499\rA-05,1449\rA-06,1374\rA-09, ...] Would this be a good first step to creating a dictionary? It seems to me that I should be able to iterate over this list in some way and make the substring before the comma the key and the substring after the comma its value. The problem is that I don't know how. Reading the book I read has not prepared me for this. I have the feeling that all the pieces of knowledge I need to solve the problem where there, but I don't know how to put them together. Greg mentioned the csv module. I checked the references but I could not see any way in which I could create a dictionary using that module, either. Once I have the dictionary built, what I would have to do is use the os module (or would it be the glob module?) to get a list of the file names I want to change and build another loop that would iterate over those file names and, if the first part of the name (possibly represented by a regular expression of the form r'[A-Z]-[0-9]+') matches one of the keys in the dictionary, then a) it would get the value for that key, b) would do the numerical calculation to determine whether it is the first part of the century or the second part and c) would insert the string representing this result right before the extension .txt. In the abstract it sounds easy, but I don't even know how to start. Doing some testing with glob I see that it returns a list of strings representing the whole paths to all the files whose names I want to manipulate. But in the reference documents that I have consulted, I see no way to change those names. How do I go about inserting the information about the century right before the substring '.txt'? As you see, I am very green. My embarrassment at realizing how basic my problems were made me delay writing another message but I decided that if I don't do it, I will never learn. Again, thanks so much for all your help. Josep M. > Message: 2 > Date: Sat, 2 Oct 2010 17:56:53 +0200 > From: "Josep M. Fontana" > To: tutor at python.org > Subject: [Tutor] Using contents of a document to change file names > Message-ID: > > > > Content-Type: text/plain; charset="iso-8859-1" > > Hi, > > This is my first posting to this list. Perhaps this has a very easy answer > but before deciding to post this message I consulted a bunch of Python > manuals and on-line reference documents to no avail. I would be very > grateful if someone could lend me a hand with this. > > Here's the problem I want to solve. I have a lot of files with the > following > name structure: > > A-01-namex.txt > A-02-namey.txt > ... > N-09-namez.txt > > These are different text documents that I want to process for an NLP > project > I'm starting. Each one of the texts belongs to a different century and it > is > important to be able to include the information about the century in the > name of the file as well as inside the text. > > Then I have another text file containing information about the century each > one of the texts was written. This document has the following structure: > > A-01, 1278 > A-02, 1501 > ... > N-09, 1384 > > What I would like to do is to write a little script that would do the > following: > > . Read each row of the text containing information about the centuries each > one of the texts was written > . Change the name of the file whose name starts with the code in the first > column in the following way > > A-01-namex.txt --> A-01-namex_13-2.txt > > Where 13-1 means: 13th 2nd half. Obviously this information would com > from the second column in the text: 1278 (the first two digits + 1 = > century; if the 3rd and 4th digits > 50, then 2; if < 50 then 1) > > Then in the same script or in a new one, I would need to open each one of > the texts and add information about the century they were written on the > first line preceded by some symbol (e.g @13-2) > > I've found a lot of information about changing file names (so I know that I > should be importing the os module), but none of the examples that were > cited > involved getting the information for the file changing operation from the > contents of a document. > > As you can imagine, I'm pretty green in Python programming and I was hoping > the learn by doing method would work. I need to get on with this project, > though, and I'm kind of stuck. Any help you guys can give me will be very > helpful. > > Josep M. > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Sun Oct 10 22:54:16 2010 From: emile at fenx.com (Emile van Sebille) Date: Sun, 10 Oct 2010 13:54:16 -0700 Subject: [Tutor] how to extract data only after a certain condition is met In-Reply-To: References: Message-ID: On 10/10/2010 12:35 PM Josep M. Fontana said... > OK. Let's start with -b- . My first problem is that I don't really know how > to go about building a dictionary from the file with the comma separated > values. I've discovered that if I use a file method called 'readlines' I can > create a list whose elements would be each of the lines contained in the > document with all the codes followed by comma followed by the year. Thus if > I do: > > fileNameCentury = open(r > '/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNamesYears.txt' > ).readlines() > > Where 'FileNamesYears.txt' is the document with the following info: > > A-01, 1278 > A-02, 1501 > ... > N-09, 1384 > > I get a list of the form ['A-01,1374\rA-02,1499\rA-05,1449\rA-06,1374\rA-09, > ...] > > Would this be a good first step to creating a dictionary? Hmmm... It looks like you got a single string -- is that the output from read and not readlines? I also see you're just getting \r which is the Mac line terminator. Are you on a Mac, or was 'FileNamesYears.txt' created on a Mac?. Python's readlines tries to be smart about which line terminator to expect, so if there's a mismatch you could have issues related to that. I would have expected you'd get something more like: ['A-01,1374\r','A-02,1499\r','A-05,1449\r','A-06,1374\r','A-09, ...] In any case, as you're getting a single string, you can split a string into pieces, for example, print "1\r2\r3\r4\r5".split("\r"). That way you can force creation of a list of strings following the format "X-NN,YYYY" each of which can be further split with xxx.split(","). Note as well that you can assign the results of split to variable names. For example, ky,val = "A-01, 1278".split(",") sets ky to A-01 and val to 1278. So, you should be able to create an empty dict, and for each line in your file set the dict entry for that line. Why don't you start there and show us what you get. HTH, Emile From alan.gauld at btinternet.com Sun Oct 10 23:04:30 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 10 Oct 2010 22:04:30 +0100 Subject: [Tutor] list of dict question References: <4CAF0314.3040303@libero.it><4CB01455.4000708@libero.it><201010091837.14448.steve@pearwood.info> <4CB01FD0.2050400@libero.it> <4CB20428.5040309@libero.it> Message-ID: "Francesco Loffredo" wrote > did, Roelof's code would work perfectly, and you could store in a > list > all the subsequent changes of a dictionary without calling them with > different names. You don;'t need dfifferent names. Provided the name creates a new object inside the loop you can reuse the same name. Roeloff's problem was that he only created one object, outside his loop. lst = [] for n in range(3): obj = {} obj[n] = str(n) lst.append(obj) Creats a list of 3 distinct dictionaries but only uses one name - obj. > I understand that if .append() stored a copy of the dict in the > list, > you will end up with lots of copies and a huge amount of memory used > by > your list, but that's exactly what will happen if you make those > copies > yourself. But you wouldn't have to devise a way to generate a new > name > for the dictionary every time you need to update it. Python uses references throughout, what you are suggesting would be a change to the normal way that Python uses names. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From bgailer at gmail.com Sun Oct 10 23:28:09 2010 From: bgailer at gmail.com (bob gailer) Date: Sun, 10 Oct 2010 17:28:09 -0400 Subject: [Tutor] how to extract data only after a certain condition is met In-Reply-To: References: Message-ID: <4CB22FE9.3050701@gmail.com> Emile beat me to it, but here goes anyway... On 10/10/2010 3:35 PM, Josep M. Fontana wrote: > Hi, > > First let me apologize for taking so long to acknowledge your answers > and to thank you (Eduardo, Peter, Greg, Emile, Joel and Alan, sorry if > I left anyone) for your help and your time. > > One of the reasons I took so long in responding (besides having gotten > busy with some urgent matters related to my work) is that I was a bit > embarrassed at realizing how poorly I had defined my problem. > As Alan said, I should at least have told you which operations were > giving me a headache. So I went back to my Python reference books to > try to write some code and thus be able to define my problems more > precisely. Only after I did that, I said to myself, I would come back > to the list with more specific questions. > > The only problem is that doing this made me painfully aware of how > little Python I know. Well, actually my problem is not so much that I > don't know Python as that I have very little experience programming in > general. Some years ago I learned a little Perl and basically I used > it to do some text manipulation using regular expressions but that's > all my experience. In order to learn Python, I read a book called > "Beginning Python: From Novice to Professional" and I was hoping that > just by starting to use the knowledge I had supposedly acquired by > reading that book to solve real problems related to my project I would > learn. But this turned out to be much more difficult than I had > expected. Perhaps if I had worked through the excellent book/tutorial > Alan has written (of which I was not aware when I started), I would be > better prepared to confront this problem. > > Anyway (sorry for the long intro), since Emile laid out the problem > very clearly, I will use his outline to point out the problems I'm having: > > Emile says: > -------------- > Conceptually, you'll need to: > > -a- get the list of file names to change then for each > -b- determine the new name > -c- rename the file > > For -a- you'll need glob. For -c- use os.rename. -b- is a bit more > involved. To break -b- down: > > -b1- break out the x-xx portion of the file name > -b2- look up the corresponding year in the other file > -b3- convert the year to the century-half structure > -b4- put the pieces together to form the new file name > > For -b2- I'd suggest building a dictionary from your second files > contents as a first step to facilitate the subsequent lookups. > > --------------------- > > OK. Let's start with -b- . My first problem is that I don't really > know how to go about building a dictionary from the file with the > comma separated values. I've discovered that if I use a file method > called 'readlines' I can create a list whose elements would be each of > the lines contained in the document with all the codes followed by > comma followed by the year. Thus if I do: > > fileNameCentury = > open(r'/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNamesYears.txt').readlines() > > > Where 'FileNamesYears.txt' is the document with the following info: > > A-01, 1278 > A-02, 1501 > ... > N-09, 1384 > > I get a list of the > form ['A-01,1374\rA-02,1499\rA-05,1449\rA-06,1374\rA-09, ...] > I'm guessing that you are running on a Linux system and that the file came from a Mac. This is based on the fact that \r appears in the string instead of acting as a line separator. Regardless - dct = {} fileNameCentury = fileNameCentury.split('\r') # gives you ['A-01,1374', 'A-02,1499', 'A-05,1449', 'A-06,1374', 'A-09, ...] for pair in fileNameCentury: key,value = pair.split(',') dct[key] = value > Greg mentioned the csv module. I checked the references but I could > not see any way in which I could create a dictionary using that module. > True - the csv reader is just another way to get the list of pairs. > > > Once I have the dictionary built, what I would have to do is use the > os module (or would it be the glob module?) to get a list of the file > names I want to change and build another loop that would iterate over > those file names and, if the first part of the name (possibly > represented by a regular expression of the form r'[A-Z]-[0-9]+') > matches one of the keys in the dictionary, then a) it would get the > value for that key, b) would do the numerical calculation to determine > whether it is the first part of the century or the second part and c) > would insert the string representing this result right before the > extension .txt. > > In the abstract it sounds easy, but I don't even know how to start. > Doing some testing with glob I see that it returns a list of strings > representing the whole paths to all the files whose names I want to > manipulate. But in the reference documents that I have consulted, I > see no way to change those names. How do I go about inserting the > information about the century right before the substring '.txt'? > Suppose fn = "blah.txt" fn2 = f > > As you see, I am very green. My embarrassment at realizing how basic > my problems were made me delay writing another message but I decided > that if I don't do it, I will never learn. > > Again, thanks so much for all your help. > > Josep M. > > Message: 2 > Date: Sat, 2 Oct 2010 17:56:53 +0200 > From: "Josep M. Fontana" > > To: tutor at python.org > Subject: [Tutor] Using contents of a document to change file names > Message-ID: > > > Content-Type: text/plain; charset="iso-8859-1" > > Hi, > > This is my first posting to this list. Perhaps this has a very > easy answer > but before deciding to post this message I consulted a bunch of Python > manuals and on-line reference documents to no avail. I would be very > grateful if someone could lend me a hand with this. > > Here's the problem I want to solve. I have a lot of files with the > following > name structure: > > A-01-namex.txt > A-02-namey.txt > ... > N-09-namez.txt > > These are different text documents that I want to process for an > NLP project > I'm starting. Each one of the texts belongs to a different century > and it is > important to be able to include the information about the century > in the > name of the file as well as inside the text. > > Then I have another text file containing information about the > century each > one of the texts was written. This document has the following > structure: > > A-01, 1278 > A-02, 1501 > ... > N-09, 1384 > > What I would like to do is to write a little script that would do the > following: > > . Read each row of the text containing information about the > centuries each > one of the texts was written > . Change the name of the file whose name starts with the code in > the first > column in the following way > > A-01-namex.txt --> A-01-namex_13-2.txt > > Where 13-1 means: 13th 2nd half. Obviously this information > would com > from the second column in the text: 1278 (the first two digits + 1 = > century; if the 3rd and 4th digits > 50, then 2; if < 50 then 1) > > Then in the same script or in a new one, I would need to open each > one of > the texts and add information about the century they were written > on the > first line preceded by some symbol (e.g @13-2) > > I've found a lot of information about changing file names (so I > know that I > should be importing the os module), but none of the examples that > were cited > involved getting the information for the file changing operation > from the > contents of a document. > > As you can imagine, I'm pretty green in Python programming and I > was hoping > the learn by doing method would work. I need to get on with this > project, > though, and I'm kind of stuck. Any help you guys can give me will > be very > helpful. > > Josep M. > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer 919-636-4239 Chapel Hill NC -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Mon Oct 11 02:11:40 2010 From: davea at ieee.org (Dave Angel) Date: Sun, 10 Oct 2010 20:11:40 -0400 Subject: [Tutor] list of dict question In-Reply-To: References: <4CAF0314.3040303@libero.it><4CB01455.4000708@libero.it><201010091837.14448.steve@pearwood.info> <4CB01FD0.2050400@libero.it> <4CB20428.5040309@libero.it> Message-ID: <4CB2563C.5030908@ieee.org> On 2:59 PM, Alan Gauld wrote: > > "Francesco Loffredo" wrote > >> did, Roelof's code would work perfectly, and you could store in a list >> all the subsequent changes of a dictionary without calling them with >> different names. > > You don;'t need dfifferent names. Provided the name creates a > new object inside the loop you can reuse the same name. > Roeloff's problem was that he only created one object, outside > his loop. > > lst = [] > for n in range(3): > obj = {} > obj[n] = str(n) > lst.append(obj) > > Creats a list of 3 distinct dictionaries but only uses one name - obj. > >> I understand that if .append() stored a copy of the dict in the list, >> you will end up with lots of copies and a huge amount of memory used by >> your list, but that's exactly what will happen if you make those copies >> yourself. But you wouldn't have to devise a way to generate a new name >> for the dictionary every time you need to update it. > > Python uses references throughout, what you are suggesting would > be a change to the normal way that Python uses names. > > HTH, > Probably more importantly, if a language only implemented copies, you couldn't have references without some different syntax. On the other hand, with Python, everything's a reference, and if you want a copy, you make one when you need it. You never do for immutables, and only you know when you need it for mutable objects. DaveA From jbiquez at icsmx.com Mon Oct 11 03:31:59 2010 From: jbiquez at icsmx.com (Jorge Biquez) Date: Sun, 10 Oct 2010 20:31:59 -0500 Subject: [Tutor] Ide To use? Other extras to install? Message-ID: <201010110139.o9B1dR1s005897@krusty.intranet.com.mx> Hello all. I am new to Python. I have been reading and studying most of the information I have found on the site. I really like what I have seen until now. I was wondering if you can help on this. I am in the process of leaving Windows as my environment and moving to Ubuntu or a Mac. For a few months I will have to continue working under windows until I finish my moving process. Anyway, I would like to start using and IDE that I can install at least in Windows and Linux . What would be the IDE you recommend me to install that would be almost transparent to be using in both platforms? What extras do you recommend me to install? (plug ins, libraries, etc) My needs are very simple now. My learning project is to have one of my websites running under the web, working with a database behind, nothing too complicated to start. Thanks in advance for all your help. Jorge Biquez From denisg640 at gmail.com Mon Oct 11 06:24:13 2010 From: denisg640 at gmail.com (Denis Gomes) Date: Mon, 11 Oct 2010 00:24:13 -0400 Subject: [Tutor] Hiding Superclass Methods Message-ID: <1286771053.4229.10.camel@joachim> Hi Everyone, I have a basic python question. I am writing an n dimensional vector class by inheriting from the builtin python list object. I want to be able to hide the parent object's methods in the derived class instances. I know I can overload the method in the derived class and raise some sort of an implementation error but that is not what I had in mind. I am also not looking to use numpy. This is strictly for learning purposes. Is there a way to hide superclass methods in python? Thanks to all, Denis From knacktus at googlemail.com Mon Oct 11 08:25:24 2010 From: knacktus at googlemail.com (Knacktus) Date: Mon, 11 Oct 2010 08:25:24 +0200 Subject: [Tutor] Hiding Superclass Methods In-Reply-To: <1286771053.4229.10.camel@joachim> References: <1286771053.4229.10.camel@joachim> Message-ID: <4CB2ADD4.8060200@googlemail.com> Am 11.10.2010 06:24, schrieb Denis Gomes: > Hi Everyone, > > I have a basic python question. I am writing an n dimensional vector > class by inheriting from the builtin python list object. I want to be > able to hide the parent object's methods in the derived class instances. Why inheriting then? Another approach to reusing exisiting methods is to wrap them into your newly defined methods. Here you would not inherit from a list, but create a list in your class (composition). E.g. class MyVector(object): def __init__(self): self.data = [] def append_data(self, new_data): self.data.append(new_data) Actually I use this all the time. And I used this before I knew about inheritance. Inheritance makes sence, when you want to reuse (almost) all methods of the superclass, like in GUI toolkits, where you typically have a base widget as superclass of a all other widgets. HTH, Jan > I know I can overload the method in the derived class and raise some > sort of an implementation error but that is not what I had in mind. I am > also not looking to use numpy. This is strictly for learning purposes. > Is there a way to hide superclass methods in python? > > Thanks to all, > Denis > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Mon Oct 11 10:06:14 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 11 Oct 2010 09:06:14 +0100 Subject: [Tutor] Ide To use? Other extras to install? References: <201010110139.o9B1dR1s005897@krusty.intranet.com.mx> Message-ID: "Jorge Biquez" wrote > I am in the process of leaving Windows as my environment and moving > to Ubuntu or a Mac. For a few months I will have to continue working > under windows until I finish my moving process. Anyway, I would like > to start using and IDE that I can install at least in Windows and > Linux . > > What would be the IDE you recommend me to install that would be > almost transparent to be using in both platforms? See the recent thread on IDEs. If you are not comfortable with the "3 window IDE(*)" - which is probably the most natural one to use in Linux - then I'd say go with Eclipse and PyDev plugin because it works pretty much identically across OS's. Also Eclipse supports multiple languages so you can do your HTML, SQL and Python all in one tool. (*) - An editor window, a testing terminal and a >>> terminal. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From stefan_ml at behnel.de Mon Oct 11 10:06:41 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 11 Oct 2010 10:06:41 +0200 Subject: [Tutor] OpenMP In-Reply-To: References: Message-ID: Alan Gauld, 09.10.2010 19:50: > Now, the bad news is that so far as I know the python interpreter > does not expose its threading model to the OS to even if you use > threads the interpreter itself will still be running on a single CPU > core. :-( Python's threads are native system threads. They can run fully concurrent on a multiprocessor system with the only exception that only one thread can use the interpreter at a time. However, everything that runs in C code can run in parallel and I/O will usually also work in parallel (which is the main use case of threads anyway). Stefan From alan.gauld at btinternet.com Mon Oct 11 10:13:00 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 11 Oct 2010 09:13:00 +0100 Subject: [Tutor] Hiding Superclass Methods References: <1286771053.4229.10.camel@joachim> Message-ID: "Denis Gomes" wrote > I have a basic python question. I am writing an n dimensional > vector > class by inheriting from the builtin python list object. I want to > be > able to hide the parent object's methods in the derived class > instances. Doing so would break the Liskofff Substitution Principle which says you should be able to use your subclass anywhere that the parent class can be used. This is a very bad thing! If you want to expose a reduced set of operations, rather than an extended set, then inheritance is the wriong solution. You should consider using delegation instead. Create a list indside your class and forward any requests for the operations you do want to the list object. The only snag with delegation in this scenartio is that you have to write an awful lot of one-liner methods. To get round that you can use setattr() and getattr() combined with a list of allowed operation names. Check the operation is in the list and then forward the request by name. That can save a lot of coding! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From zebra05 at gmail.com Mon Oct 11 10:21:32 2010 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Mon, 11 Oct 2010 10:21:32 +0200 Subject: [Tutor] Rounding a Python float to the nearest half integer In-Reply-To: References: Message-ID: Thanks everyone. On Fri, Oct 8, 2010 at 11:44 PM, Wayne Werner wrote: > On Fri, Oct 8, 2010 at 7:58 AM, Sithembewena Lloyd Dube > wrote: > >> I realise that one cannot have a half integer :) I meant how would one >> round off to the first decimal nearest to either 0.5, or a whole number. >> >> Ugh...does anyone get what I'm trying to articulate? :) > > > sample input/output cases are always useful. > > -Wayne > -- Regards, Sithembewena Lloyd Dube http://www.lloyddube.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From shantanoo at gmail.com Mon Oct 11 10:52:27 2010 From: shantanoo at gmail.com (=?UTF-8?B?4KS24KSC4KSk4KSo4KWCIChTaGFudGFub28p?=) Date: Mon, 11 Oct 2010 14:22:27 +0530 Subject: [Tutor] Rounding a Python float to the nearest half integer In-Reply-To: References: Message-ID: === round(...) round(number[, ndigits]) -> floating point number Round a number to a given precision in decimal digits (default 0 digits). This always returns a floating point number. Precision may be negative. === >>> round(1.23423,2) 1.23 >>> round(1.23623,2) 1.24 HTH. On Mon, Oct 11, 2010 at 13:51, Sithembewena Lloyd Dube wrote: > Thanks everyone. > > > On Fri, Oct 8, 2010 at 11:44 PM, Wayne Werner wrote: > >> On Fri, Oct 8, 2010 at 7:58 AM, Sithembewena Lloyd Dube < >> zebra05 at gmail.com> wrote: >> >>> I realise that one cannot have a half integer :) I meant how would one >>> round off to the first decimal nearest to either 0.5, or a whole number. >>> >>> Ugh...does anyone get what I'm trying to articulate? :) >> >> >> sample input/output cases are always useful. >> >> -Wayne >> > > > > -- > Regards, > Sithembewena Lloyd Dube > http://www.lloyddube.com > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From zebra05 at gmail.com Mon Oct 11 11:03:12 2010 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Mon, 11 Oct 2010 11:03:12 +0200 Subject: [Tutor] Rounding a Python float to the nearest half integer In-Reply-To: References: Message-ID: Thanks Shantanoo, finally got the hang of this. 2010/10/11 ????? (Shantanoo) > === > round(...) > round(number[, ndigits]) -> floating point number > > Round a number to a given precision in decimal digits (default 0 > digits). > This always returns a floating point number. Precision may be > negative. > === > > >>> round(1.23423,2) > 1.23 > >>> round(1.23623,2) > 1.24 > > > HTH. > > On Mon, Oct 11, 2010 at 13:51, Sithembewena Lloyd Dube wrote: > >> Thanks everyone. >> >> >> On Fri, Oct 8, 2010 at 11:44 PM, Wayne Werner wrote: >> >>> On Fri, Oct 8, 2010 at 7:58 AM, Sithembewena Lloyd Dube < >>> zebra05 at gmail.com> wrote: >>> >>>> I realise that one cannot have a half integer :) I meant how would one >>>> round off to the first decimal nearest to either 0.5, or a whole number. >>>> >>>> Ugh...does anyone get what I'm trying to articulate? :) >>> >>> >>> sample input/output cases are always useful. >>> >>> -Wayne >>> >> >> >> >> -- >> Regards, >> Sithembewena Lloyd Dube >> http://www.lloyddube.com >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > -- Regards, Sithembewena Lloyd Dube http://www.lloyddube.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From fal at libero.it Mon Oct 11 11:07:38 2010 From: fal at libero.it (Francesco Loffredo) Date: Mon, 11 Oct 2010 11:07:38 +0200 Subject: [Tutor] list of dict question In-Reply-To: <4CB2563C.5030908@ieee.org> References: <4CAF0314.3040303@libero.it><4CB01455.4000708@libero.it><201010091837.14448.steve@pearwood.info> <4CB01FD0.2050400@libero.it> <4CB20428.5040309@libero.it> <4CB2563C.5030908@ieee.org> Message-ID: <4CB2D3DA.5080909@libero.it> Thank you, Alan and Dave, for your spotting this weak point in my understanding of Python! On 11/10/2010 2.11, Dave Angel wrote: > On 2:59 PM, Alan Gauld wrote: >> >> "Francesco Loffredo" wrote >> >>> did, Roelof's code would work perfectly, and you could store in a list >>> all the subsequent changes of a dictionary without calling them with >>> different names. >> >> You don;'t need dfifferent names. Provided the name creates a >> new object inside the loop you can reuse the same name. >> Roeloff's problem was that he only created one object, outside >> his loop. >> >> lst = [] >> for n in range(3): >> obj = {} I didn't know that this creates a new obj if obj already exists, I thought it would just update it. That's my mistake. Does this mean that if I write: obj = {} obj = {} obj = {} then I'll create 3 different dictionaries, with the name obj referring to the third? >> obj[n] = str(n) >> lst.append(obj) >> >> Creats a list of 3 distinct dictionaries but only uses one name - obj. Ok, this does not lose too much in elegance. >>> I understand that if .append() stored a copy of the dict in the list, >>> you will end up with lots of copies and a huge amount of memory used by >>> your list, but that's exactly what will happen if you make those copies >>> yourself. But you wouldn't have to devise a way to generate a new name >>> for the dictionary every time you need to update it. >> >> Python uses references throughout, what you are suggesting would >> be a change to the normal way that Python uses names. I needed a hint about what is "the normal way that Python uses names", thank you. >> > Probably more importantly, if a language only implemented copies, you > couldn't have references without some different syntax. On the other > hand, with Python, everything's a reference, and if you want a copy, you > make one when you need it. You never do for immutables, and only you > know when you need it for mutable objects. > > DaveA Thank you too, Dave. I thought that to make a copy you would have to create a different name, and this sounded too cumbersome for me... Francesco -------------- next part -------------- Nessun virus nel messaggio in uscita. Controllato da AVG - www.avg.com Versione: 9.0.862 / Database dei virus: 271.1.1/3188 - Data di rilascio: 10/10/10 08:34:00 From josep.m.fontana at gmail.com Mon Oct 11 13:46:53 2010 From: josep.m.fontana at gmail.com (Josep M. Fontana) Date: Mon, 11 Oct 2010 13:46:53 +0200 Subject: [Tutor] Using contents of a document to change file names, (was Re: how to extract data only after a certain ...) Message-ID: Sorry about the confusion with the subject line. I was receiving messages in digest mode and I copied and pasted the wrong heading in my previous message. Now I have written the heading corresponding to my initial message. I have also changed the settings for this list from the digest mode to the default mode because it is easier to manage if you are participating in threads. OK, first thanks Emile and Bob for your help. Both of you noticed that the following line of code returned a string instead of a list as it would be expected from using .readlines(): open(r'/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNamesYears.txt').readlines() returns --> ['A-01,1374\rA-02,1499\rA-05,1449\rA-06,1374\rA-09, ...'] Yes, I had not noticed it but this is what I get. You guessed correctly that I am using a Mac. Just in case it might be useful, I'm also using PyDev in Eclipse (I figured since I'm learning to program, I can start using an IDE that will grow with my programming skills). I tried your suggestion of using .split() to get around the problem but I still cannot move forward. I don't know if my implementation of your suggestion is the correct one but here's the problem I'm having. When I do the following: ----------------- fileNameCentury = open(r '/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNamesYears.txt' .split('\r')) dct = {} for pair in fileNameCentury: key,value = pair.split(',') dct[key] = value print dct -------------- I get the following long error message: ------------------- pydev debugger: warning: psyco not available for speedups (the debugger will still work correctly, but a bit slower) pydev debugger: starting Traceback (most recent call last): File "/Applications/eclipse/plugins/org.python.pydev.debug_1.6.3.2010100513/pysrc/pydevd.py", line 1145, in debugger.run(setup['file'], None, None) File "/Applications/eclipse/plugins/org.python.pydev.debug_1.6.3.2010100513/pysrc/pydevd.py", line 916, in run execfile(file, globals, locals) #execute the script File "/Volumes/DATA/Documents/workspace/GCA/src/file_name_change.py", line 2, in fileNameCentury = open(r'/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNamesYears.txt'.split('\n')) TypeError: coercing to Unicode: need string or buffer, list found ------------ Before reporting this problem, I did some research on the newline problems and I saw that you can set the mode in open() to 'U' to handle similar problems. So I tried the following: >>>fileNameCentury = open(r '/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNamesYears.txt', "U") >>>output = fileNameCentury.readlines() >>>print output Interestingly I get closer to the solution but with a little twist: ['A-01,1374\n', 'A-02,1499\n', 'A-05,1449\n', 'A-06,1374\n', 'A-09,1449\n', 'B-01,1299\n', 'B-02,1299\n', 'B-06,1349\n'...] That is, now I do get a list but as you can see I get the newline character as part of each one of the strings in the list. This is pretty weird. Is this a general problem with Macs? Josep M. From: Emile van Sebille To: tutor at python.org On 10/10/2010 12:35 PM Josep M. Fontana said... > > fileNameCentury = open(r > '/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNamesYears.txt' > ).readlines() > > Where 'FileNamesYears.txt' is the document with the following info: > > A-01, 1278 > A-02, 1501 > ... > N-09, 1384 > > I get a list of the form ['A-01,1374\rA-02,1499\rA-05,1449\rA-06,1374\rA-09, > ...] > > Would this be a good first step to creating a dictionary? Hmmm... It looks like you got a single string -- is that the output from read and not readlines? I also see you're just getting \r which is the Mac line terminator. Are you on a Mac, or was 'FileNamesYears.txt' created on a Mac?. Python's readlines tries to be smart about which line terminator to expect, so if there's a mismatch you could have issues related to that. I would have expected you'd get something more like: ['A-01,1374\r','A-02,1499\r','A-05,1449\r','A-06,1374\r','A-09, ...] In any case, as you're getting a single string, you can split a string into pieces, for example, print "1\r2\r3\r4\r5".split("\r"). That way you can force creation of a list of strings following the format "X-NN,YYYY" each of which can be further split with xxx.split(","). Note as well that you can assign the results of split to variable names. For example, ky,val = "A-01, 1278".split(",") sets ky to A-01 and val to 1278. So, you should be able to create an empty dict, and for each line in your file set the dict entry for that line. Why don't you start there and show us what you get. HTH, Emile -------------- next part -------------- An HTML attachment was scrubbed... URL: From fal at libero.it Mon Oct 11 13:50:33 2010 From: fal at libero.it (Francesco Loffredo) Date: Mon, 11 Oct 2010 13:50:33 +0200 Subject: [Tutor] Basics In-Reply-To: References: Message-ID: <4CB2FA09.9070709@libero.it> On 28/09/2010 12.05, Alan Gauld wrote: > If you are already fairly experienced you might find the Python Challenge > web site a fun way to learn too. www.pythonchallenge.com I didn't know of this site, it's VERY interesting and funny! Thank you, Alan. Francesco -------------- next part -------------- Nessun virus nel messaggio in uscita. Controllato da AVG - www.avg.com Versione: 9.0.862 / Database dei virus: 271.1.1/3188 - Data di rilascio: 10/10/10 08:34:00 From cwitts at compuscan.co.za Mon Oct 11 14:23:37 2010 From: cwitts at compuscan.co.za (Christian Witts) Date: Mon, 11 Oct 2010 14:23:37 +0200 Subject: [Tutor] Using contents of a document to change file names, (was Re: how to extract data only after a certain ...) In-Reply-To: References: Message-ID: <4CB301C9.1070207@compuscan.co.za> On 11/10/2010 13:46, Josep M. Fontana wrote: > I tried your suggestion of using .split() to get around the problem > but I still cannot move forward. I don't know if my implementation of > your suggestion is the correct one but here's the problem I'm having. > When I do the following: > > ----------------- > > fileNameCentury = > open(r'/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNamesYears.txt'.split('\r')) > dct = {} > for pair in fileNameCentury: > key,value = pair.split(',') > dct[key] = value > print dct > > -------------- > > I get the following long error message: > > fileNameCentury = > open(r'/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNamesYears.txt'.split('\n')) > > > TypeError: coercing to Unicode: need string or buffer, list found > > ------------ What you should be doing is: fileNameCentury = open('/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNamesYears.txt', 'r') dct = {} for line in fileNameCentury: #File objects have built-in iteration key, value = line.strip().split(',') dct[key] = value What you were doing originally was splitting the input filename for the open function (hence the error message stating `need string or buffer, list found`. If you wish to read in the entire file and then split it on newline characters you would do fileObject.read().splitlines() but it is more efficient to create your file object and just iterate over it (that way there is only 1 line at a time stored in memory and you're not reading in the entire file first). It's not a Mac problem, just a problem with how you were going about it. Hope that helps. -- Kind Regards, Christian Witts From susana.delgado_s at utzmg.edu.mx Mon Oct 11 15:37:28 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Mon, 11 Oct 2010 08:37:28 -0500 Subject: [Tutor] WRITING XLS FROM OS.WALK() Message-ID: Hello members! Thank you all of you for taking the time to answer to my question. I didn?t expect it will create the controversy we just had in this topic. I don't mind for the comments of re-inventing the wheel, I'm new in python, documentation is good but programmers' experience is even better. The intention of using the os.walk() is because I'm creating a sort of directory crawler to find every file with the extensions I'm looking for, so am I in the rigth direction? The other question is about the excel files management, if I want to manipulate this data to work it in Excel should I look for an excel library in python right? -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Mon Oct 11 16:23:32 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 11 Oct 2010 10:23:32 -0400 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: References: Message-ID: On Mon, Oct 11, 2010 at 9:37 AM, Susana Iraiis Delgado Rodriguez < susana.delgado_s at utzmg.edu.mx> wrote: > Hello members! > > Thank you all of you for taking the time to answer to my question. I didn?t > expect it will create the controversy we just had in this topic. I don't > mind for the comments of re-inventing the wheel, I'm new in python, > documentation is good but programmers' experience is even better. > > The intention of using the os.walk() is because I'm creating a sort of > directory crawler to find every file with the extensions I'm looking for, so > am I in the rigth direction? > The other question is about the excel files management, if I want to > manipulate this data to work it in Excel should I look for an excel library > in python right? > > > > I would write the file as csv. Then, excel, or any other spreadsheet can read the file. Python offers csv support, its easy to understand, produces small files, and being a text format, it is easy to debug problems. -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From denisg640 at gmail.com Mon Oct 11 16:29:35 2010 From: denisg640 at gmail.com (Denis Gomes) Date: Mon, 11 Oct 2010 10:29:35 -0400 Subject: [Tutor] Hiding Superclass Methods In-Reply-To: References: <1286771053.4229.10.camel@joachim> Message-ID: Thank you both for your responses. I do have one other question if I use the method both of you describe. How do I go about implementing slicing and indexing for an object in python? A list object innately has them and that is really why I wanted to use it. I would appreciate it if you can point me to something. Denis On Mon, Oct 11, 2010 at 4:13 AM, Alan Gauld wrote: > > "Denis Gomes" wrote > > > I have a basic python question. I am writing an n dimensional vector >> class by inheriting from the builtin python list object. I want to be >> able to hide the parent object's methods in the derived class instances. >> > > Doing so would break the Liskofff Substitution Principle which says > you should be able to use your subclass anywhere that the parent > class can be used. This is a very bad thing! > > If you want to expose a reduced set of operations, rather than an > extended set, then inheritance is the wriong solution. You should > consider using delegation instead. Create a list indside your class > and forward any requests for the operations you do want to the > list object. > > The only snag with delegation in this scenartio is that you have > to write an awful lot of one-liner methods. To get round that > you can use setattr() and getattr() combined with a list of allowed > operation names. Check the operation is in the list and then > forward the request by name. That can save a lot of coding! > > HTH, > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From adam.jtm30 at gmail.com Mon Oct 11 16:55:10 2010 From: adam.jtm30 at gmail.com (Adam Bark) Date: Mon, 11 Oct 2010 15:55:10 +0100 Subject: [Tutor] Hiding Superclass Methods In-Reply-To: References: <1286771053.4229.10.camel@joachim> Message-ID: <4CB3254E.4090009@gmail.com> On 11/10/10 15:29, Denis Gomes wrote: > Thank you both for your responses. I do have one other question if I > use the method both of you describe. How do I go about implementing > slicing and indexing for an object in python? A list object innately > has them and that is really why I wanted to use it. I would > appreciate it if you can point me to something. > Denis You can use __getslice__, __setslice__ etc. methods. They're detailed in the list docstrings. Here's an example of using __getslice__ >>> dir([]) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>> print [].__getslice__.__doc__ x.__getslice__(i, j) <==> x[i:j] Use of negative indices is not supported. >>> print [].__setslice__.__doc__ x.__setslice__(i, j, y) <==> x[i:j]=y Use of negative indices is not supported. >>> class TestSlice(object): ... def __getslice__(self, i, j): ... print i, j ... >>> t = TestSlice() >>> t[2:3] 2 3 HTH, Adam. From dkuhlman at rexx.com Mon Oct 11 16:16:47 2010 From: dkuhlman at rexx.com (Dave Kuhlman) Date: Mon, 11 Oct 2010 07:16:47 -0700 Subject: [Tutor] Ide To use? Other extras to install? In-Reply-To: References: <201010110139.o9B1dR1s005897@krusty.intranet.com.mx> Message-ID: <20101011141647.GA92396@cutter.rexx.com> On Mon, Oct 11, 2010 at 09:06:14AM +0100, Alan Gauld wrote: > > "Jorge Biquez" wrote > > >I am in the process of leaving Windows as my environment and moving > >to Ubuntu or a Mac. For a few months I will have to continue working > >under windows until I finish my moving process. Anyway, I would like > >to start using and IDE that I can install at least in Windows and > >Linux . > > > >What would be the IDE you recommend me to install that would be > >almost transparent to be using in both platforms? > > See the recent thread on IDEs. Here is a comparison of Python IDEs: http://www.infoworld.com/d/developer-world/infoworld-review-nine-fine-python-development-tools-374 This is a multi-page article. The subsequence pages give some details about each IDE. - Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman From dkuhlman at rexx.com Mon Oct 11 16:23:17 2010 From: dkuhlman at rexx.com (Dave Kuhlman) Date: Mon, 11 Oct 2010 07:23:17 -0700 Subject: [Tutor] Hiding Superclass Methods In-Reply-To: <4CB2ADD4.8060200@googlemail.com> References: <1286771053.4229.10.camel@joachim> <4CB2ADD4.8060200@googlemail.com> Message-ID: <20101011142317.GB92396@cutter.rexx.com> On Mon, Oct 11, 2010 at 08:25:24AM +0200, Knacktus wrote: > > Am 11.10.2010 06:24, schrieb Denis Gomes: > >Hi Everyone, > > > > I have a basic python question. I am writing an n dimensional vector > >class by inheriting from the builtin python list object. I want to be > >able to hide the parent object's methods in the derived class instances. > Why inheriting then? > Another approach to reusing exisiting methods is to wrap them into your > newly defined methods. Here you would not inherit from a list, but > create a list in your class (composition). E.g. > > class MyVector(object): > > def __init__(self): > self.data = [] > > def append_data(self, new_data): > self.data.append(new_data) > > Actually I use this all the time. And I used this before I knew about > inheritance. It's a good technique. And, it's called delegation. For more on delegation, see this: http://en.wikipedia.org/wiki/Delegation_(programming) Also see Alan's message in this same thread. - Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman From wprins at gmail.com Mon Oct 11 17:31:07 2010 From: wprins at gmail.com (Walter Prins) Date: Mon, 11 Oct 2010 16:31:07 +0100 Subject: [Tutor] Hiding Superclass Methods In-Reply-To: <4CB3254E.4090009@gmail.com> References: <1286771053.4229.10.camel@joachim> <4CB3254E.4090009@gmail.com> Message-ID: On 11 October 2010 15:55, Adam Bark wrote: > You can use __getslice__, __setslice__ etc. methods. They're detailed in > the list docstrings. Here's an example of using __getslice__ > > >>> dir([]) > ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', > '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', > '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', > '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', > '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', > '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', > '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', > 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] > >>> print [].__getslice__.__doc__ > x.__getslice__(i, j) <==> x[i:j] > Just to add, you might try using "help()" on the various objects and methods (also objects) to see more contextual and/or formatted information, e.g. help([]) or help([].__getslice__) etc. Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Mon Oct 11 17:39:45 2010 From: wprins at gmail.com (Walter Prins) Date: Mon, 11 Oct 2010 16:39:45 +0100 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: References: Message-ID: On 11 October 2010 14:37, Susana Iraiis Delgado Rodriguez < susana.delgado_s at utzmg.edu.mx> wrote: > The other question is about the excel files management, if I want to > manipulate this data to work it in Excel should I look for an excel library > in python right? > > Yes. I already mentioned one (in my view good) option in my original reply to you. The xlwt module works quite well for generating Excel files (with expectable limitations) from any platform that Python's available on (e.g. including non-Windows.) and thus does not require Excel to be available on the machine you're producing the file on. If however you are running on Windows and have Excel installed, you could consider driving the real Excel via COM automation, which will guarantee you get desired results including formatting, charts etc, and will ensure you have full access to all the functionality Excel exposes via its COM object model. If your requirements on the other hand simple enough then Joel's suggestion to use CSV is probably preferable. (The KISS principle: "Keep It Small & Simple") There's also the possibility to look into generating .xlsx files (e.g. XML based Office format files.) Theoretically you should be able to generate the correctly structured XML independent of Excel and have it open in Excel. I suspect that may be rather painful though (have never tried this myself) andam almost reluctant to even mention this option, so caveat emptor. Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From denisg640 at gmail.com Mon Oct 11 18:06:14 2010 From: denisg640 at gmail.com (Denis Gomes) Date: Mon, 11 Oct 2010 12:06:14 -0400 Subject: [Tutor] Hiding Superclass Methods In-Reply-To: References: <1286771053.4229.10.camel@joachim> <4CB3254E.4090009@gmail.com> Message-ID: I understand where to go from here. Thanks to all who responded. Appreciate it. Denis -------------- next part -------------- An HTML attachment was scrubbed... URL: From jed at jedsmith.org Mon Oct 11 15:53:30 2010 From: jed at jedsmith.org (Jed Smith) Date: Mon, 11 Oct 2010 09:53:30 -0400 Subject: [Tutor] Ide To use? Other extras to install? In-Reply-To: <201010110139.o9B1dR1s005897@krusty.intranet.com.mx> References: <201010110139.o9B1dR1s005897@krusty.intranet.com.mx> Message-ID: On Sun, Oct 10, 2010 at 9:31 PM, Jorge Biquez wrote: > What would be the IDE you recommend me to install that would be almost > transparent to be using in both platforms? For transparency on multiple platforms, I need to reiterate the suggestion to use the "traditional" IDE - editor, interactive, testing. You're not going to find a comfortable experience, in my opinion, on multiple platforms. Something is always compromised out to make the product platform-agnostic. On Mac OS X, I've been extremely pleased with TextMate, simply for its rsync+ssh bundle (which I have bound to Shift+Cmd+S). I still have the interactive IPython and testing terminal, but when I'm ready to send code up to the server, Shift+Cmd+S. I don't use much else from TextMate. TextMate also has a save-when-I-lose-focus feature which is quite useful. > What extras do you recommend me to install? (plug ins, libraries, etc) IPython. Very, very IPython. -- Jed Smith jed at jedsmith.org From prdpsbhat at gmail.com Mon Oct 11 13:56:55 2010 From: prdpsbhat at gmail.com (p4ddy) Date: Mon, 11 Oct 2010 04:56:55 -0700 (PDT) Subject: [Tutor] Help installing HLS Player Message-ID: <29933359.post@talk.nabble.com> Hi, I have a HLS Player written in python but whenever I try to install it, I get this error "ImportError: No module named setuptools" So I downloaded setuptools-0.6c11.zip and tried to install it, again i got this error "ImportError: No module named distutils.util" So I went back and downloaded python2.4-devel-2.4-1pydotorg.i386.rpm and installed it. I have python2.4 installed in the machine (linux) but apparently python2.4-devel didn't find it and gave dependency error. So I installed it using "rpm -i --nodeps python2.4-devel-2.4-1pydotorg.i386.rpm" and it got installed. Now when i went back to install setuptools, i m getting the same problem :( I m pretty new to python, so can anybody help me on this? I'm pretty frustrated already !! -- View this message in context: http://old.nabble.com/Help-installing-HLS-Player-tp29933359p29933359.html Sent from the Python - tutor mailing list archive at Nabble.com. From waynejwerner at gmail.com Mon Oct 11 19:10:08 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Mon, 11 Oct 2010 12:10:08 -0500 Subject: [Tutor] Help installing HLS Player In-Reply-To: <29933359.post@talk.nabble.com> References: <29933359.post@talk.nabble.com> Message-ID: On Mon, Oct 11, 2010 at 6:56 AM, p4ddy wrote: > > Hi, > > I have a HLS Player written in python but whenever I try to install it, I > get this error > > "ImportError: No module named setuptools" > > So I downloaded setuptools-0.6c11.zip and tried to install it, again i got > this error > > "ImportError: No module named distutils.util" > > So I went back and downloaded python2.4-devel-2.4-1pydotorg.i386.rpm and > installed it. > > I have python2.4 installed in the machine (linux) but apparently > python2.4-devel didn't find it and gave dependency error. So I installed it > using What version of linux are you using? Python 2.4 is insanely old, and you should have some type of package manager. There is very little reason to be manually installing from rpm and zip file. That being said, this question has *very* little to do with Python and almost everything to do with linux, and even though many of us are familiar with it, you'll probably have more success asking your local Linux Users Group (LUG): http://www.linux.org/groups/ HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From aeneas24 at priest.com Mon Oct 11 19:10:11 2010 From: aeneas24 at priest.com (aeneas24 at priest.com) Date: Mon, 11 Oct 2010 13:10:11 -0400 Subject: [Tutor] If/else in Python 2.6.5 vs. Python 2.4.3 Message-ID: <8CD377E4C87F8F1-11AC-114E@web-mmc-m09.sysops.aol.com> Hi, I have code that works fine when I run it on Python 2.6.5, but I get an "invalid syntax" error in Python 2.4.3. I'm hoping you can help me fix it. The line in question splits a chunk of semi-colon separated words into separate elements. rgenre = re.split(r';', rf.info["genre"] if "genre" in rf.info else [] I get a syntax error at "if" in 2.4.3. I tried doing the following but it doesn't work (I don't understand why). if "genre" in rf.info: rgenre = re.split(r';', rf.info["genre"]) else: [] And I tried this, too: if "genre" in rf.info: rgenre = re.split(r';', rf.info["genre"]) if "genre" not in rf.info: [] Both of these cause problems later on in the program, specifically "UnboundLocalError: local variable 'rgenre' referenced before assignment", about this line in the code (where each of these is one of the 30 possible genres): rg1, rg2, rg3, rg4, rg5, rg6, rg7, rg8, rg9, rg10, rg11, rg12, rg13, rg14, rg15, rg16, rg17, rg18, rg19, rg20, rg21, rg22, rg23, rg24, rg25, rg26, rg27, rg28, rg29, rg30= rgenre + ["NA"]*(30-len(rgenre[:30])) Thanks for any help--I gave a little extra information, but I'm hoping there's a simple rewrite of the first line I gave that'll work in Python 2.4.3. Thanks, Tyler -------------- next part -------------- An HTML attachment was scrubbed... URL: From susana.delgado_s at utzmg.edu.mx Mon Oct 11 19:11:51 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Mon, 11 Oct 2010 12:11:51 -0500 Subject: [Tutor] WRITING XLS FROM OS.WALK() In-Reply-To: References: Message-ID: Hello Walter and members: I'm working with csv to handle excel, my other was useful to do my new cvs file. When I open the output file, it has all the information I need, but all of my lines have the filepath separate by comes, even though it's supposed to be a complete string; for example: C,:,\,i,n,d,i,c,e,.,s,h,p all the others have the same format. Besides I need a row to put in each cell the column names, with: c.writerow(["Archivo","x_min","x_max","y_min","y_max","geometria","num_ele","prj","estructura bd","extent","fecha_modificacion","maquina_host"]). But all tha names gather in the same row and I want my pathfile to place under the "Archivo" column, but I couldn't do it. 2010/10/11 Walter Prins > > > On 11 October 2010 14:37, Susana Iraiis Delgado Rodriguez < > susana.delgado_s at utzmg.edu.mx> wrote: > >> The other question is about the excel files management, if I want to >> manipulate this data to work it in Excel should I look for an excel library >> in python right? >> >> > Yes. I already mentioned one (in my view good) option in my original reply > to you. The xlwt module works quite well for generating Excel files (with > expectable limitations) from any platform that Python's available on (e.g. > including non-Windows.) and thus does not require Excel to be available on > the machine you're producing the file on. > > If however you are running on Windows and have Excel installed, you could > consider driving the real Excel via COM automation, which will guarantee you > get desired results including formatting, charts etc, and will ensure you > have full access to all the functionality Excel exposes via its COM object > model. > > If your requirements on the other hand simple enough then Joel's suggestion > to use CSV is probably preferable. (The KISS principle: "Keep It Small & > Simple") > > There's also the possibility to look into generating .xlsx files (e.g. XML > based Office format files.) Theoretically you should be able to generate > the correctly structured XML independent of Excel and have it open in > Excel. I suspect that may be rather painful though (have never tried this > myself) andam almost reluctant to even mention this option, so caveat > emptor. > > Walter > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Oct 11 19:12:26 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 11 Oct 2010 18:12:26 +0100 Subject: [Tutor] Using contents of a document to change file names, (was Re: how to extract data only after a certain ...) References: Message-ID: "Josep M. Fontana" wrote > I tried your suggestion of using .split() to get around the problem > but I > still cannot move forward. > fileNameCentury = open(r > '/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNamesYears.txt' > .split('\r')) You are trying to split the filename not the contents of the file! > Interestingly I get closer to the solution but with a little twist: > > ['A-01,1374\n', 'A-02,1499\n', 'A-05,1449\n', 'A-06,1374\n', > 'A-09,1449\n', > 'B-01,1299\n', 'B-02,1299\n', 'B-06,1349\n'...] > > That is, now I do get a list but as you can see I get the newline > character > as part of each one of the strings in the list. This is pretty > weird. Is > this a general problem with Macs? No, this is what you would expect. Reading from a file will give you the \n character, you can use strip() (or rstrip()) to remove it. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Mon Oct 11 19:23:28 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 11 Oct 2010 18:23:28 +0100 Subject: [Tutor] list of dict question References: <4CAF0314.3040303@libero.it><4CB01455.4000708@libero.it><201010091837.14448.steve@pearwood.info> <4CB01FD0.2050400@libero.it> <4CB20428.5040309@libero.it> <4CB2563C.5030908@ieee.org> <4CB2D3DA.5080909@libero.it> Message-ID: "Francesco Loffredo" wrote >>> lst = [] >>> for n in range(3): >>> obj = {} > I didn't know that this creates a new obj if obj already exists, I > thought it would just update it. That's my mistake. Yes you have to remember that in Python, unlike C etc, names are not aliases for memory locations. They are keys into a namespace dictionary. So the value that the dictionary refers to can change for any given name - even the type of object can change. > Does this mean that if I write: > obj = {} > obj = {} > obj = {} > then I'll create 3 different dictionaries, with the name obj > referring > to the third? Yes and the first and second will be garbage collected since there is nothing referring to them now. >>> obj[n] = str(n) >>> lst.append(obj) >>> >>> Creats a list of 3 distinct dictionaries but only uses one name - >>> obj. > Ok, this does not lose too much in elegance. Yes and I could have made it even more elegant with: lst = [] for n in range(3): lst.append({n:str(n)}) # no explicit object name needed at all! And even more tight with: lst = [ {n:str(n)} for n in range(3)] HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From waynejwerner at gmail.com Mon Oct 11 19:46:10 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Mon, 11 Oct 2010 12:46:10 -0500 Subject: [Tutor] If/else in Python 2.6.5 vs. Python 2.4.3 In-Reply-To: <8CD377E4C87F8F1-11AC-114E@web-mmc-m09.sysops.aol.com> References: <8CD377E4C87F8F1-11AC-114E@web-mmc-m09.sysops.aol.com> Message-ID: On Mon, Oct 11, 2010 at 12:10 PM, wrote: > > > rgenre = re.split(r';', rf.info["genre"] if "genre" in rf.info else [] > > I get a syntax error at "if" in 2.4.3. > That's because you're using the ternary operator that was not available in 2.4: http://www.python.org/dev/peps/pep-0308/ > > I tried doing the following but it doesn't work (I don't understand why). > > if "genre" in rf.info: > rgenre = re.split(r';', rf.info["genre"]) > else: > [] > > > And I tried this, too: > > > if "genre" in rf.info: > rgenre = re.split(r';', rf.info["genre"]) > if "genre" not in rf.info: > [] > > Both of these cause problems later on in the program, specifically > "UnboundLocalError: local variable 'rgenre' referenced before assignment", > about this line in the code (where each of these is one of the 30 possible > genres): > The problem isn't with your logic expression - it's because on the else case you never assign anything to rgenre, you simply state 'empty list' - not 'set rgenre to be an empty list'. if 'genre' in rf.info: rgenre = re.split(r';', rf.info['genre']) else: rgenre = [] will work on all(?) versions of python - and 2.4 certainly. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From susana.delgado_s at utzmg.edu.mx Mon Oct 11 22:08:15 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Mon, 11 Oct 2010 15:08:15 -0500 Subject: [Tutor] Attempt to overwrite excel cell Python Message-ID: Hello members: I'm using xlwt from python to work with excel, I'm developing some sort of system crawler to look for all the files ending with .shp, I took a look to csv, but I didn't understand it. Now, I have to create a .xls where people can save information from shp files, but first I have to create the excel and add a row for column names: import os from xlwt import Workbook wb = Workbook() ws = wb.add_sheet('shp') ws.row(0).write(0,'archivo') ws.row(0).write(1,'x_min') ws.row(0).write(2,'y_min') ws.row(0).write(3,'x_max') ws.row(0).write(4,'y_max') ws.row(0).write(5,'geometria') ws.row(0).write(6,'num_elem') ws.row(0).write(7,'prj') ws.row(0).write(8,'estrutura bd') ws.row(0).write(9,'extent') ws.row(0).write(10,'fecha_modificacion') ws.row(0).write(11,'maquina_host') Then I have to do the crawler part to have the directory I want: allfiles = [] #store all files found for root,dir,files in os.walk("C:\\"): filelist = [ os.path.join(root,fi) for fi in files if fi.endswith(".shp") ] for f in filelist: allfiles.append(f) Now I want to get the name of the file and write it uder the column "Archivo", the variable i has the name of each file: for i in allfiles: print i ws.row(1).write(0,i) wb.save('shp.xls') But when I run it, it throws me the error: *Exception*: *Attempt to overwrite cell*: sheetname='shp' rowx=1 colx=0, don't know what is wrong -------------- next part -------------- An HTML attachment was scrubbed... URL: From jlu at hep.anl.gov Mon Oct 11 18:19:22 2010 From: jlu at hep.anl.gov (Jack Uretsky) Date: Mon, 11 Oct 2010 11:19:22 -0500 (CDT) Subject: [Tutor] dictionary and more In-Reply-To: References: <985647.19911.qm@web86707.mail.ird.yahoo.com> Message-ID: Hi- I apologize for the size of the attached file, but the question would make little sense if I made substantial deletions. The program gives the following error: Traceback (most recent call last): File "/Javastuff/python/Glau_Is.py", line 131, in Adestin[state]() TypeError: 'NoneType' object is not callable This is after the RETA prinout from function A_1, so that seems to be called correctly. The black screen is not part of the problem, I am not including the .jpeg files. Line 131 is the one that reads Adestin[state]() and the program prints (correctly) state=1 before the traceback. Thanks in advance for any insights. JLU "Trust me. I have a lot of experience at this." General Custer's unremembered message to his men, just before leading them into the Little Big Horn Valley -------------- next part -------------- import pygame from pygame.locals import * from sys import exit import time import random from math import * global gamma gamma = .25 global RET RET = [0,0] global state global r def A_1(): state =1 r = 1 - gamma f = random.randrange(3) g = f+2 #destination RET = (g,r) print "RETA!=", RET def A_2(): state = 2 global r2 r2 = 1. + gamma f = random.randrange(3) if f == 0: g = 1 #destination elif f == 1: g,r2 = 7,1. elif f == 2: g,r2 = 6,1 else: print "alarm 2" RET = [g,r2] def A_3(): state =3 global r3 r3 = 1. + gamma f = random.randrange(3) if f == 1: g = 1 elif f == 0: g,r3 = 7, 1. elif f == 2: g, r3 = 5, 1 else: print "alarm 3" RET = [g, r3] def A_4(): state = 4 global r4 r4 = 1. + gamma f = random.randrange(3) if f == 2: g = 1 elif f == 0: g,r4 = 6,1 elif f == 1: g,r4 = 5,1 else: print "alarm 4" RET = [g, r4] def A_5(): state =5 global r5 r5 = 1. + gamma f = random.randrange(3) if f == 0: g = 8 elif f == 1: g,r5 = 4,1 elif f == 2: g,r5 = 3,1 else: print "alarm 5" RET = [g, r5] def A_6(): state = 6 global r6 r6 = 1. + gamma f= random.randrange(3) if f == 1: g = 8 elif f == 0: g,r6 = 4,1 elif f == 2: g,r6 = 3,1 else: print "alarm 6" RET = [g, r6] def A_7(): state = 7 global r7 r7 = 1. + gamma f = random.randrange(3) if f == 2: g = 8 elif f == 0: g,r7 = 3,1 elif f == 1: g,r7 = 2,1 else: print "alarm 7" RET = [g, r7] def A_8(): state = 8 global r8 r8 = 1. - gamma f = random.randrange(3) g = f + 5 RET = [g, r8] #dictionaries Adestin = {1: A_1(), 2 : A_2(), 3 : A_3(), 4: A_4(),5 : A_5(), 6 : A_6(), 7: A_7(), 8 : A_8()} apics = {1: 'a_1.jpg', 2: 'a_2.jpg', 3 : 'a_3.jpg', 4: 'a_4.jpg', 5: 'a_5.jpg', 6: 'a_6.jpg', 7 : 'a_7.jpg', 8 : 'a_8.jpg'} state = 1 #set initial state pygame.init() screen = pygame.display.set_mode((640,480), 0, 32) pygame.event.pump() back1 =apics[state] back = pygame.image.load(back1).convert() screen.blit(back, (0,0)) pygame.display.update() RET=(1,1-gamma) k=0 while k <= 5: k = k+1 back1 = apics[state] print "state=", state Adestin[state]() print RET, k destin = RET[0] pois = RET[1] back = pygame.image.load(back1).convert() screen.blit(back, (0,0)) pygame.display.update() state = destin print "state=",destin time.sleep(5) print "end" From alan.gauld at btinternet.com Mon Oct 11 22:57:00 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 11 Oct 2010 21:57:00 +0100 Subject: [Tutor] dictionary and more References: <985647.19911.qm@web86707.mail.ird.yahoo.com> Message-ID: "Jack Uretsky" wrote Please do not reply to an existing thread when starting a new topic. This messes up threaded newsreaders and several archives. Start a new topic with a newmessage. > I apologize for the size of the attached file, but the question > would make little sense if I made substantial deletions. > The program gives the following error: > Traceback (most recent call last): > File "/Javastuff/python/Glau_Is.py", line 131, in > Adestin[state]() > TypeError: 'NoneType' object is not callable This says that Adestin[state] is None. So lets see how Adestin gets initialised.... Its a function call, which returns None. So Python is correct. You want to store the function names (more accurately the function objects) so you do not want the () after the name when you initialise the dictionary. Here is an example: def f1(): print 'f1' def f2(): print 'f2' dct = {'1': f1, '2': f2} # notice no parens, only a reference for key in dct: dct[key]() # use the parens here to call the func HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From emile at fenx.com Mon Oct 11 23:03:17 2010 From: emile at fenx.com (Emile van Sebille) Date: Mon, 11 Oct 2010 14:03:17 -0700 Subject: [Tutor] dictionary and more In-Reply-To: References: <985647.19911.qm@web86707.mail.ird.yahoo.com> Message-ID: On 10/11/2010 9:19 AM Jack Uretsky said... > Hi- > I apologize for the size of the attached file, but the question would > make little sense if I made substantial deletions. > The program gives the following error: > Traceback (most recent call last): > File "/Javastuff/python/Glau_Is.py", line 131, in > Adestin[state]() > TypeError: 'NoneType' object is not callable > This is after the RETA prinout from function A_1, so that seems to be > called correctly. The black screen is not part of the problem, I am not > including the .jpeg files. Line 131 is the one that reads > Adestin[state]() > and the program prints (correctly) state=1 before the traceback. Then Adestin[1] is None causing TypeError: 'NoneType' object is not callable. You can verify that by printing Adestin[1] where you're currently printing state. Then you'd discover that when you're building Adestin you're assigning the _results_ of the functions to your dictionary. You probably want to leave off the parens when declaring the functions. Further, your functions probably need to return something -- add return statements if you want the results. HTH, Emile From davea at ieee.org Mon Oct 11 23:26:55 2010 From: davea at ieee.org (Dave Angel) Date: Mon, 11 Oct 2010 17:26:55 -0400 Subject: [Tutor] If/else in Python 2.6.5 vs. Python 2.4.3 In-Reply-To: <8CD377E4C87F8F1-11AC-114E@web-mmc-m09.sysops.aol.com> References: <8CD377E4C87F8F1-11AC-114E@web-mmc-m09.sysops.aol.com> Message-ID: <4CB3811F.2080407@ieee.org> On 2:59 PM, aeneas24 at priest.com wrote: > Hi, > > I have code that works fine when I run it on Python 2.6.5, but I get an "invalid syntax" error in Python 2.4.3. I'm hoping you can help me fix it. > > The line in question splits a chunk of semi-colon separated words into separate elements. > > rgenre = re.split(r';', rf.info["genre"] if "genre" in rf.info else [] > > I get a syntax error at "if" in 2.4.3. > > I tried doing the following but it doesn't work (I don't understand why). > > > > if "genre" in rf.info: > rgenre = re.split(r';', rf.info["genre"]) > else: > [] > > close. The else clause should read: rgenre = [] > And I tried this, too: > > > if "genre" in rf.info: > rgenre = re.split(r';', rf.info["genre"]) > if "genre" not in rf.info: > [] > > Both of these cause problems later on in the program, specifically "UnboundLocalError: local variable 'rgenre' referenced before assignment", about this line in the code (where each of these is one of the 30 possible genres): > > rg1, rg2, rg3, rg4, rg5, rg6, rg7, rg8, rg9, rg10, rg11, rg12, rg13, rg14, rg15, rg16, rg17, rg18, rg19, rg20, rg21, rg22, rg23, rg24, rg25, rg26, rg27, rg28, rg29, rg30= rgenre + ["NA"]*(30-len(rgenre[:30])) > > Thanks for any help--I gave a little extra information, but I'm hoping there's a simple rewrite of the first line I gave that'll work in Python 2.4.3. > > Thanks, > > Tyler > From steve at pearwood.info Tue Oct 12 00:41:19 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 12 Oct 2010 09:41:19 +1100 Subject: [Tutor] Attempt to overwrite excel cell Python In-Reply-To: References: Message-ID: <201010120941.20242.steve@pearwood.info> On Tue, 12 Oct 2010 07:08:15 am Susana Iraiis Delgado Rodriguez wrote: > But when I run it, it throws me the error: > *Exception*: *Attempt to overwrite cell*: sheetname='shp' rowx=1 > colx=0, don't know what is wrong Please don't summarise the error. Copy and paste the exact error message, including the full traceback. I suspect that you might need to read the documentation of the xlwt project and see what it says. Perhaps the cell you are trying to overwrite is locked? -- Steven D'Aprano From steve at pearwood.info Tue Oct 12 00:47:48 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 12 Oct 2010 09:47:48 +1100 Subject: [Tutor] dictionary and more In-Reply-To: References: Message-ID: <201010120947.48805.steve@pearwood.info> On Tue, 12 Oct 2010 03:19:22 am Jack Uretsky wrote: > Hi- > I apologize for the size of the attached file, but the question > would make little sense if I made substantial deletions. > The program gives the following error: > Traceback (most recent call last): > File "/Javastuff/python/Glau_Is.py", line 131, in > Adestin[state]() > TypeError: 'NoneType' object is not callable The question makes perfect sense without even looking at the attached file. Adestin[state] returns None, which you attempt to call as a function. The questions you should be asking yourself are: - what value am I expecting it to contain? - where in my code should that value be set? - why is it setting None instead? Armed with these three questions, you now know how to debug the problem yourself. -- Steven D'Aprano From steve at pearwood.info Tue Oct 12 00:52:34 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 12 Oct 2010 09:52:34 +1100 Subject: [Tutor] Hiding Superclass Methods In-Reply-To: <4CB3254E.4090009@gmail.com> References: <1286771053.4229.10.camel@joachim> <4CB3254E.4090009@gmail.com> Message-ID: <201010120952.34611.steve@pearwood.info> On Tue, 12 Oct 2010 01:55:10 am Adam Bark wrote: > On 11/10/10 15:29, Denis Gomes wrote: > > Thank you both for your responses. I do have one other question if > > I use the method both of you describe. How do I go about > > implementing slicing and indexing for an object in python? A list > > object innately has them and that is really why I wanted to use it. > > I would appreciate it if you can point me to something. > > Denis > > You can use __getslice__, __setslice__ etc. methods. They're detailed > in the list docstrings. Here's an example of using __getslice__ __getslice __setslice__ and __delslice__ have been deprecated since version 2.0, seven versions ago. http://docs.python.org/reference/datamodel.html#object.__getslice__ You should use __getitem__ __setitem__ and __delitem__, and detect if your argument is an integer or a slice object, and behave accordingly. http://docs.python.org/reference/datamodel.html#object.__getitem__ -- Steven D'Aprano From artists00nly at gmail.com Tue Oct 12 00:39:11 2010 From: artists00nly at gmail.com (Will Geary) Date: Mon, 11 Oct 2010 23:39:11 +0100 Subject: [Tutor] Multiple regex replacements, lists and for. Message-ID: Hello all, I'm new to python and inexperienced in programming but I'm trying hard. I have a shell script that I'm converting over to python. Part of the script replaces some lines of text. I can do this in python, and get the output I want, but so far only using sed. Here's an example script: import subprocess, re list = ['Apples the color red', 'Sky i am the blue color', 'Grass the colour green is here', 'Sky i am the blue color'] def oldway(): sed_replacements = """ s/\(^\w*\).*red/\\1:RED/ s/\(^\w*\).*blue.*/\\1:BLUE/""" sed = subprocess.Popen(['sed', sed_replacements], stdin=subprocess.PIPE, stdout=subprocess.PIPE) data = sed.communicate("\n".join(list))[:-1] for x in data: print x oldway(); """ This produces: >>>Apples:RED >>>Sky:BLUE >>>Grass the colour green is here >>>Sky:BLUE Which is what I want""" print "---------------" def withoutsed(): replacements = [ (r'.*red', 'RED'), (r'.*blue.*', 'BLUE')] for z in list: for x,y in replacements: if re.match(x, z): print re.sub(x,y,z) break else: print z withoutsed(); """ Produces: >>>RED >>>Sky i am the blue color >>>BLUE >>>Grass the colour green is here >>>Grass the colour green is here >>>Sky i am the blue color >>>BLUE Duplicate printing + other mess = I went wrong""" I understand that it's doing what I tell it to, and that my for and if statements are wrong. What I want to do is replace matching lines and print them, and also print the non-matching lines. Can somebody please point me in the right direction? Any other python pointers or help much appreciated, Will. From g.nius.ck at gmail.com Tue Oct 12 01:45:43 2010 From: g.nius.ck at gmail.com (Chris King) Date: Mon, 11 Oct 2010 19:45:43 -0400 Subject: [Tutor] Networking In-Reply-To: <4CA90FAF.1070002@gmail.com> References: <4CA788E2.4060006@gmail.com> <33D037A9-0230-42B3-9AFC-A1884F00AE88@gmail.com> <4CA90FAF.1070002@gmail.com> Message-ID: <4CB3A1A7.9080804@gmail.com> On 10/3/2010 7:20 PM, Chris King wrote: > On 10/2/2010 3:40 PM, Evert Rol wrote: >>> Dear Tutors, >>> I have attached my 2 programs for networking. It uses socket >>> and SocketServer, but it just simplifies it even more. The problem >>> is it won't work. The Client raises the error, (with trace back) >>> Traceback (most recent call last): >>> File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line >>> 34, in >>> print client.recv() >>> File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line >>> 16, in recv >>> return self.__sock.recv(1024) >>> error: [Errno 10053] An established connection was aborted by the >>> software in your host machine >>> The server seems to get an error of some sort, but also seems to >>> catch and print it. It prints >>> ---------------------------------------- >>> Exception happened during processing of request from ('127.0.0.1', >>> 1424) >>> Traceback (most recent call last): >>> File "C:\Python26\lib\SocketServer.py", line 281, in >>> _handle_request_noblock >>> self.process_request(request, client_address) >>> File "C:\Python26\lib\SocketServer.py", line 307, in process_request >>> self.finish_request(request, client_address) >>> File "C:\Python26\lib\SocketServer.py", line 320, in finish_request >>> self.RequestHandlerClass(request, client_address, self) >>> TypeError: 'NoneType' object is not callable >>> ---------------------------------------- >>> I look at both of the documentations of socket and SocketServer, but >>> I couldn't firgue it out. I don't know much about networking. Please >>> Help >> I don't know much about networking, less so about it on Windows; >> also, I've only scanned quickly through the server script, but I >> notice your create_handler() function doesn't return anything. I >> guess it needs to return the Handler class. >> The example in the Python docs doesn't use a factory function, but >> instead directly puts the class as the second argument to TCPServer. >> So the second argument needs to be a class, but since your >> create_handler() function returns nothing, you presumably get this >> NoneType exception. >> >> Evert >> > Yeah, that could be a problem. It should return the class. Now it won't send more than once. The Error the clients raises is: Traceback (most recent call last): File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 34, in print client.recv() File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 16, in recv return self.__sock.recv(1024) error: [Errno 10053] An established connection was aborted by the software in your host machine The server is blissfully unaware of its crashing clients, and keeps on going to help more clients to there dooms. The client can receive data multiple times, but just can send twice. Please help. -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: server.py Type: text/x-python Size: 2148 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: client.py Type: text/x-python Size: 1346 bytes Desc: not available URL: From adam.jtm30 at gmail.com Tue Oct 12 02:36:59 2010 From: adam.jtm30 at gmail.com (Adam Bark) Date: Tue, 12 Oct 2010 01:36:59 +0100 Subject: [Tutor] Hiding Superclass Methods In-Reply-To: <201010120952.34611.steve@pearwood.info> References: <1286771053.4229.10.camel@joachim> <4CB3254E.4090009@gmail.com> <201010120952.34611.steve@pearwood.info> Message-ID: <4CB3ADAB.1060104@gmail.com> On 11/10/10 23:52, Steven D'Aprano wrote: > On Tue, 12 Oct 2010 01:55:10 am Adam Bark wrote: > >> On 11/10/10 15:29, Denis Gomes wrote: >> >>> Thank you both for your responses. I do have one other question if >>> I use the method both of you describe. How do I go about >>> implementing slicing and indexing for an object in python? A list >>> object innately has them and that is really why I wanted to use it. >>> I would appreciate it if you can point me to something. >>> Denis >>> >> You can use __getslice__, __setslice__ etc. methods. They're detailed >> in the list docstrings. Here's an example of using __getslice__ >> > __getslice __setslice__ and __delslice__ have been deprecated since > version 2.0, seven versions ago. > > http://docs.python.org/reference/datamodel.html#object.__getslice__ > > You should use __getitem__ __setitem__ and __delitem__, and detect if > your argument is an integer or a slice object, and behave accordingly. > > http://docs.python.org/reference/datamodel.html#object.__getitem__ > > Oh right, 2.6 doesn't say anything about it in the docstrings. From wprins at gmail.com Tue Oct 12 03:49:44 2010 From: wprins at gmail.com (Walter Prins) Date: Tue, 12 Oct 2010 02:49:44 +0100 Subject: [Tutor] Attempt to overwrite excel cell Python In-Reply-To: References: Message-ID: Hi Susana, On 11 October 2010 21:08, Susana Iraiis Delgado Rodriguez < susana.delgado_s at utzmg.edu.mx> wrote: > > *Exception*: *Attempt to overwrite cell*: sheetname='shp' rowx=1 colx=0, > don't know what is wrong > > This is a default of xlwt behaviour for writing Excel files. It assumes it would generally be an unintended mistake to write a cell then overwrite it later with something else hence it by default raises an exception when this happens. Anyway, you can override this behaviour and enable cell overwriting by adding the parameter "cell_overwrite_ok=True" when you create a worksheet. However I think you probably are just accidentally (and erroneously) overwriting the same row accidentally, so the error is probably meaningful in your case. Wahtever the case, I've taken the liberty to knock what you posted into shape a bi -- The following script now works (well it finds files and doesn't generate the error you reported.) I've also tidied up the structure and formatting as you'll see: http://pastebin.com/Kc9N3DAC Hope that helps, Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From evert.rol at gmail.com Tue Oct 12 09:02:37 2010 From: evert.rol at gmail.com (Evert Rol) Date: Tue, 12 Oct 2010 09:02:37 +0200 Subject: [Tutor] Multiple regex replacements, lists and for. In-Reply-To: References: Message-ID: <73D3ECB3-5F27-4D5D-99B8-9831B0C43386@gmail.com> > I'm new to python and inexperienced in programming but I'm trying hard. > I have a shell script that I'm converting over to python. > Part of the script replaces some lines of text. > I can do this in python, and get the output I want, but so far only using sed. > Here's an example script: > > import subprocess, re > list = ['Apples the color red', 'Sky i am the blue color', 'Grass the > colour green is here', 'Sky i am the blue color'] > > def oldway(): > sed_replacements = """ > s/\(^\w*\).*red/\\1:RED/ > s/\(^\w*\).*blue.*/\\1:BLUE/""" > sed = subprocess.Popen(['sed', sed_replacements], > stdin=subprocess.PIPE, stdout=subprocess.PIPE) > data = sed.communicate("\n".join(list))[:-1] > for x in data: > print x > oldway(); > > """ This produces: > >>>> Apples:RED >>>> Sky:BLUE >>>> Grass the colour green is here >>>> Sky:BLUE > > Which is what I want""" > > print "---------------" > > def withoutsed(): > replacements = [ > (r'.*red', 'RED'), > (r'.*blue.*', 'BLUE')] > for z in list: > for x,y in replacements: > if re.match(x, z): > print re.sub(x,y,z) > break > else: > print z > withoutsed(); > > """ Produces: > >>>> RED >>>> Sky i am the blue color >>>> BLUE >>>> Grass the colour green is here >>>> Grass the colour green is here >>>> Sky i am the blue color >>>> BLUE > > Duplicate printing + other mess = I went wrong""" > > I understand that it's doing what I tell it to, and that my for and if > statements are wrong. You should make your Python regex more like sed. re.sub() always returns a string, either changed or unchanged. So you can "pipe" the two necessary re.sub() onto each other, like you do for sed: re.sub(replacement, replacement, re.sub(replacement, replacement, string). That removes the inner for loop, because you can do all the replacements in one go. re.sub() will return the original string if there was no replacement (again like sed), so you can remove the if-statement with the re.match: re.sub() will leave the 'Grass' sentence untouched, but still print it. Lastly, in your sed expression, you're catching the first non-whitespace characters and substitute them in the replacements, but you don't do in re.sub(). Again, this is practically the same format, the only difference being that in Python regexes, you don't need to escape the grouping parentheses. I can give you the full solution, but I hope this is pointer in the right direction is good enough. All in all, your code can be as efficient in Python as in sed. Cheers, Evert Oh, btw: semi-colons at the end of a statement in Python are allowed, but redundant, and look kind of, well, wrong. > What I want to do is replace matching lines and print them, and also > print the non-matching lines. > Can somebody please point me in the right direction? > > Any other python pointers or help much appreciated, > > Will. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From fal at libero.it Tue Oct 12 11:47:55 2010 From: fal at libero.it (Francesco Loffredo) Date: Tue, 12 Oct 2010 11:47:55 +0200 Subject: [Tutor] list of dict question In-Reply-To: References: <4CAF0314.3040303@libero.it><4CB01455.4000708@libero.it><201010091837.14448.steve@pearwood.info> <4CB01FD0.2050400@libero.it> <4CB20428.5040309@libero.it> <4CB2563C.5030908@ieee.org> <4CB2D3DA.5080909@libero.it> Message-ID: <4CB42ECB.903@libero.it> On 11/10/2010 19.23, Alan Gauld wrote: > ... > HTH, Sure it did! Very enlightening, Alan. THANK YOU! -------------- next part -------------- Nessun virus nel messaggio in uscita. Controllato da AVG - www.avg.com Versione: 9.0.862 / Database dei virus: 271.1.1/3190 - Data di rilascio: 10/11/10 08:34:00 From smokefloat at gmail.com Tue Oct 12 13:41:36 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 12 Oct 2010 07:41:36 -0400 Subject: [Tutor] SQLite3 DB Field Alphabetizing Message-ID: When calling a sqlite3 db file in python 2.6 on Ubuntu, I get the following when the items are taken from the db tuple, lstripped('u'), and added to a list. ['.hjvkjgfkj/bdgfkjbg', 'bbbbbbbbbbbbuuzzzzzzzzz', 'jgkgyckghc', 'kfhhv ', 'khfhf', 'test', 'test10', 'test2', 'test3', 'test346w43', 'test4', 'test5', 'test6', 'test7', 'test7uyuy', 'test8', 'test9', 'testgraph', ';s;juf;sfkh', 'zzrerhshhjrs'] My question is, why is everything except [:-2] in alphabetical order? It doesn't really matter(at this point), for my purposes, but I'd like to know when they changed the abc's to xy;z's? Thanks, David From bgailer at gmail.com Tue Oct 12 14:28:18 2010 From: bgailer at gmail.com (bob gailer) Date: Tue, 12 Oct 2010 08:28:18 -0400 Subject: [Tutor] SQLite3 DB Field Alphabetizing In-Reply-To: References: Message-ID: <4CB45462.7020603@gmail.com> On 10/12/2010 7:41 AM, David Hutto wrote: > When calling a sqlite3 db file Calling? How did you do that? I presume a sql select statement. True? If so please post the statement. Else what do you mean by call? > in python 2.6 on Ubuntu, I get the > following when the items are taken from the db tuple, lstripped('u'), > and added to a list. > > ['.hjvkjgfkj/bdgfkjbg', 'bbbbbbbbbbbbuuzzzzzzzzz', 'jgkgyckghc', > 'kfhhv ', 'khfhf', 'test', 'test10', 'test2', 'test3', 'test346w43', > 'test4', 'test5', 'test6', 'test7', 'test7uyuy', 'test8', 'test9', > 'testgraph', ';s;juf;sfkh', 'zzrerhshhjrs'] > > My question is, why is everything except [:-2] in alphabetical order? > It doesn't really matter(at this point), for my purposes, but I'd like > to know when they changed the abc's to xy;z's? -- Bob Gailer 919-636-4239 Chapel Hill NC From rwobben at hotmail.com Tue Oct 12 14:40:17 2010 From: rwobben at hotmail.com (Roelof Wobben) Date: Tue, 12 Oct 2010 12:40:17 +0000 Subject: [Tutor] urllib problem Message-ID: Hoi, I have this programm : import urllib import re f = urllib.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=6") inhoud = f.read() f.close() nummer = re.search('[0-9]', inhoud) volgende = int(nummer.group()) teller = 1 while teller <= 3 : url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=" + str(volgende) f = urllib.urlopen(url) inhoud = f.read() f.close() nummer = re.search('[0-9]', inhoud) print "nummer is", nummer.group() volgende = int(nummer.group()) print volgende teller = teller + 1 but now the url changes but volgende not. What do I have done wrong ? Roelof From steve at pearwood.info Tue Oct 12 14:55:05 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 12 Oct 2010 23:55:05 +1100 Subject: [Tutor] SQLite3 DB Field Alphabetizing In-Reply-To: References: Message-ID: <201010122355.06713.steve@pearwood.info> On Tue, 12 Oct 2010 10:41:36 pm David Hutto wrote: > When calling a sqlite3 db file in python 2.6 on Ubuntu, I get the > following when the items are taken from the db tuple, lstripped('u'), > and added to a list. > > ['.hjvkjgfkj/bdgfkjbg', 'bbbbbbbbbbbbuuzzzzzzzzz', 'jgkgyckghc', > 'kfhhv ', 'khfhf', 'test', 'test10', 'test2', 'test3', 'test346w43', > 'test4', 'test5', 'test6', 'test7', 'test7uyuy', 'test8', 'test9', > 'testgraph', ';s;juf;sfkh', 'zzrerhshhjrs'] > > My question is, why is everything except [:-2] in alphabetical order? *shrug* Perhaps they were added to the DB in alphabetical order, except for the entry starting with a semi-colon? Who supplied the database and how did they insert the data? Perhaps it's some artifact of whatever internal optimizations the DB uses? I expect the DB table is something like a hash table, only more complicated, so it's not impossible that the hashing function used ends up storing items in almost-but-not-quite alphabetical order. Maybe it's just a fluke. They have to be in *some* order. Since you haven't told us how you added them to the list, perhaps you deliberately added them in that order and are trying to trick us. In which case, HA! I see through your cunning trick! *wink* -- Steven D'Aprano From evert.rol at gmail.com Tue Oct 12 14:54:34 2010 From: evert.rol at gmail.com (Evert Rol) Date: Tue, 12 Oct 2010 14:54:34 +0200 Subject: [Tutor] urllib problem In-Reply-To: References: Message-ID: <5B4CCDBC-C6B8-4C03-87E7-FBF622549A00@gmail.com> > I have this program : > > import urllib > import re > f = urllib.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=6") > inhoud = f.read() > f.close() > nummer = re.search('[0-9]', inhoud) > volgende = int(nummer.group()) > teller = 1 > while teller <= 3 : > url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=" + str(volgende) > f = urllib.urlopen(url) > inhoud = f.read() > f.close() > nummer = re.search('[0-9]', inhoud) > print "nummer is", nummer.group() > volgende = int(nummer.group()) > print volgende > teller = teller + 1 > > but now the url changes but volgende not. I think number will change; *unless* you happen to retrieve the same number every time, even when you access a different url. What is the result when you run this program, ie, the output of your print statements (then, also, print url)? And, how can url change, but volgende not? Since url depends on volgende. Btw, it may be better to use parentheses in your regular expression to explicitly group whatever you want to match, though the above will work (since it groups the whole match). But Python has this "Explicit is better than implicit" thing. Cheers, Evert From steve at pearwood.info Tue Oct 12 14:58:03 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 12 Oct 2010 23:58:03 +1100 Subject: [Tutor] urllib problem In-Reply-To: References: Message-ID: <201010122358.03905.steve@pearwood.info> On Tue, 12 Oct 2010 11:40:17 pm Roelof Wobben wrote: > Hoi, > > I have this programm : > > import urllib > import re > f = > urllib.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.php? >nothing=6") inhoud = f.read() > f.close() > nummer = re.search('[0-9]', inhoud) > volgende = int(nummer.group()) > teller = 1 > while teller <= 3 : > url = > "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=" + > str(volgende) f = urllib.urlopen(url) > inhoud = f.read() > f.close() > nummer = re.search('[0-9]', inhoud) > print "nummer is", nummer.group() > volgende = int(nummer.group()) > print volgende > teller = teller + 1 > > but now the url changes but volgende not. > What do I have done wrong ? Each time through the loop, you set volgende to the same result: nummer = re.search('[0-9]', inhoud) volgende = int(nummer.group()) Since inhoud never changes, and the search never changes, the search result never changes, and volgende never changes. -- Steven D'Aprano From smokefloat at gmail.com Tue Oct 12 15:02:52 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 12 Oct 2010 09:02:52 -0400 Subject: [Tutor] SQLite3 DB Field Alphabetizing In-Reply-To: <4CB45462.7020603@gmail.com> References: <4CB45462.7020603@gmail.com> Message-ID: Sorry about that, I there might have been an obvious reason. *Note that the I invented the *.bob file before you replied. import sqlite3 as lite class db(object): def onNewProjSQLDB(self): self.con = lite.connect('/home/david/pythonfiles/pythonscripts/roughdraftapps/dataplot3/projdir/basicobjb.bob') self.cur = self.con.cursor() self.orderbygname = self.cur.execute('''select * from %s order by graphname''' % ('projectbob')) self.liorder = [] for graphname in self.orderbygname: self.liorder.append(str(graphname[0]).lstrip('u')) print self.liorder # Save (commit) the changes self.con.commit() self.cur.close() self.con.close() db = db() db.onNewProjSQLDB() From smokefloat at gmail.com Tue Oct 12 15:07:40 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 12 Oct 2010 09:07:40 -0400 Subject: [Tutor] SQLite3 DB Field Alphabetizing In-Reply-To: <201010122355.06713.steve@pearwood.info> References: <201010122355.06713.steve@pearwood.info> Message-ID: No trickery, I've been adding entries at random all day(scout's honor ii||i). But the above shows the code I used, and the first shows the entries added at random while testing and retrieving, and it shows it alphabetically any other time. I thought it might have to do with a character or several in combination. From smokefloat at gmail.com Tue Oct 12 15:38:40 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 12 Oct 2010 09:38:40 -0400 Subject: [Tutor] SQLite3 DB Field Alphabetizing In-Reply-To: References: <4CB45462.7020603@gmail.com> Message-ID: On Tue, Oct 12, 2010 at 9:02 AM, David Hutto wrote: > Sorry about that, I there might have been an obvious reason. > *Note that the I invented the *.bob file before you replied. Apparently, I am to .bob, what Al Gore is to the internet. > > import sqlite3 as lite > class db(object): > ? ? ? ?def onNewProjSQLDB(self): > ? ? ? ? ? ? ? ?self.con = lite.connect('/home/david/pythonfiles/pythonscripts/roughdraftapps/dataplot3/projdir/basicobjb.bob') > ? ? ? ? ? ? ? ?self.cur = self.con.cursor() > ? ? ? ? ? ? ? ?self.orderbygname = self.cur.execute('''select * from %s order by > graphname''' % ('projectbob')) > ? ? ? ? ? ? ? ?self.liorder = [] > ? ? ? ? ? ? ? ?for graphname in self.orderbygname: > ? ? ? ? ? ? ? ? ? ? ? ?self.liorder.append(str(graphname[0]).lstrip('u')) > ? ? ? ? ? ? ? ?print self.liorder > ? ? ? ? ? ? ? ?# Save (commit) the changes > ? ? ? ? ? ? ? ?self.con.commit() > > ? ? ? ? ? ? ? ?self.cur.close() > ? ? ? ? ? ? ? ?self.con.close() > db = db() > db.onNewProjSQLDB() > From alan.gauld at btinternet.com Tue Oct 12 15:59:25 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 12 Oct 2010 14:59:25 +0100 Subject: [Tutor] SQLite3 DB Field Alphabetizing References: Message-ID: "David Hutto" wrote > My question is, why is everything except [:-2] in alphabetical > order? > It doesn't really matter(at this point), for my purposes, but I'd > like > to know when they changed the abc's to xy;z's? Without seeing the SQL we can't be sure. By default SQL does not guarantee any order. But if you specify an ORDER BY clause then it should be ordered as specified. Did you include an ORDER BY? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From smokefloat at gmail.com Tue Oct 12 16:02:42 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 12 Oct 2010 10:02:42 -0400 Subject: [Tutor] SQLite3 DB Field Alphabetizing In-Reply-To: References: Message-ID: > > Did you include an ORDER BY? See three posts above, line 6. > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From alan.gauld at btinternet.com Tue Oct 12 16:23:50 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 12 Oct 2010 15:23:50 +0100 Subject: [Tutor] SQLite3 DB Field Alphabetizing References: Message-ID: "David Hutto" wrote > > Did you include an ORDER BY? > > See three posts above, line 6. Looks like our posts crossed in transit :-) So you order by graphname, and you only have a single field of that name? But then when you put it into the list you only use part of graphname converted to a string and stripped: str(graphname[0]).lstrip('u')) So what did the field look like before you modified it? Since that is what determines the way that ORDER BY ordered it? And what is graphname[0] before you convert it to a string? (It looks like it is already a string?!) BTW. Its unusual to have to extract sub fields from a SQL result since to maximise the power of SQL its better to store all "subfields" in separate fields of the table. One of the complexities of debugging any database program is that almost as much depends on the data structures and content as it does on the code. Without seeing the data that is beintg operated upon its hard to be precise. What happens if you execute the SQL at the sqlite interactive prompt? Does it give the same result? PS. You don't need the commit() for a SELECT statement, you aren't changing anything. Alan G. From steve at pearwood.info Tue Oct 12 16:51:16 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 13 Oct 2010 01:51:16 +1100 Subject: [Tutor] urllib problem In-Reply-To: <201010122358.03905.steve@pearwood.info> References: <201010122358.03905.steve@pearwood.info> Message-ID: <201010130151.16436.steve@pearwood.info> On Tue, 12 Oct 2010 11:58:03 pm Steven D'Aprano wrote: > On Tue, 12 Oct 2010 11:40:17 pm Roelof Wobben wrote: > > Hoi, > > > > I have this programm : > > > > import urllib > > import re > > f = > > urllib.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.ph > >p? nothing=6") inhoud = f.read() > > f.close() > > nummer = re.search('[0-9]', inhoud) > > volgende = int(nummer.group()) > > teller = 1 > > while teller <= 3 : > > url = > > "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=" + > > str(volgende) f = urllib.urlopen(url) > > inhoud = f.read() > > f.close() > > nummer = re.search('[0-9]', inhoud) > > print "nummer is", nummer.group() > > volgende = int(nummer.group()) > > print volgende > > teller = teller + 1 > > > > but now the url changes but volgende not. > > What do I have done wrong ? > > Each time through the loop, you set volgende to the same result: > > nummer = re.search('[0-9]', inhoud) > volgende = int(nummer.group()) > > Since inhoud never changes, and the search never changes, the search > result never changes, and volgende never changes. Wait, sorry, inhoud should change... I missed the line inhoud = f.read() My mistake, sorry about that. However, I can now see what is going wrong. Your regular expression only looks for a single digit: re.search('[0-9]', inhoud) If you want any number of digits, you need '[0-9]+' instead. Starting from the first URL: >>> f = urllib.urlopen( ... "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=6") >>> inhoud = f.read() >>> f.close() >>> print inhoud and the next nothing is 87599 but: >>> nummer = re.search('[0-9]', inhoud) >>> nummer.group() '8' See, you only get the first digit. Then looking up the page with nothing=8 gives a first digit starting with 5, and then you get stuck on 5 forever: >>> urllib.urlopen( ... "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=8").read() 'and the next nothing is 59212' >>> urllib.urlopen( ... "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=5").read() 'and the next nothing is 51716' You need to add a + to the regular expression, which means "one or more digits" instead of "a single digit". -- Steven D'Aprano From rwobben at hotmail.com Tue Oct 12 16:50:21 2010 From: rwobben at hotmail.com (Roelof Wobben) Date: Tue, 12 Oct 2010 14:50:21 +0000 Subject: [Tutor] urllib problem In-Reply-To: <201010122358.03905.steve@pearwood.info> References: , <201010122358.03905.steve@pearwood.info> Message-ID: ---------------------------------------- > From: steve at pearwood.info > To: tutor at python.org > Date: Tue, 12 Oct 2010 23:58:03 +1100 > Subject: Re: [Tutor] urllib problem > > On Tue, 12 Oct 2010 11:40:17 pm Roelof Wobben wrote: >> Hoi, >> >> I have this programm : >> >> import urllib >> import re >> f = >> urllib.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.php? >>nothing=6") inhoud = f.read() >> f.close() >> nummer = re.search('[0-9]', inhoud) >> volgende = int(nummer.group()) >> teller = 1 >> while teller <= 3 : >> url = >> "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=" + >> str(volgende) f = urllib.urlopen(url) >> inhoud = f.read() >> f.close() >> nummer = re.search('[0-9]', inhoud) >> print "nummer is", nummer.group() >> volgende = int(nummer.group()) >> print volgende >> teller = teller + 1 >> >> but now the url changes but volgende not. >> What do I have done wrong ? > > Each time through the loop, you set volgende to the same result: > > nummer = re.search('[0-9]', inhoud) > volgende = int(nummer.group()) > > Since inhoud never changes, and the search never changes, the search > result never changes, and volgende never changes. > > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Hello, Here is the output when I print every step in the beginning : inhoud : and the next nothing is 87599 nummer is 8 volgende is 8 and here is the output in the loop : url is: http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=8 inhoud is and the next nothing is 59212 nummer is 5 2ste run: url is: http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=5 inhoud is and the next nothing is 51716 nummer is 5 3ste run: url is: http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=5 inhoud is and the next nothing is 51716 nummer is 5 4ste run: I see the problem. It only takes the first number of the nothing. So I have to look how to solve that. Roelof From joel.goldstick at gmail.com Tue Oct 12 16:57:39 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 12 Oct 2010 10:57:39 -0400 Subject: [Tutor] SQLite3 DB Field Alphabetizing In-Reply-To: References: Message-ID: On Tue, Oct 12, 2010 at 10:23 AM, Alan Gauld wrote: > > "David Hutto" wrote > > > Did you include an ORDER BY? >> >> See three posts above, line 6. >> > > > Just a guess. You strip the letter u from your list items. Is there a letter u in each of them? You are sorting on the item before you strip the u. So, if the one strange item in your list has a u in front, but no others do, it will end up ordering as in your results. > for graphname in self.orderbygname: > self.liorder.append(str(graphname[0]).lstrip('u')) > > __________________________________________ > -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From rwobben at hotmail.com Tue Oct 12 17:44:50 2010 From: rwobben at hotmail.com (Roelof Wobben) Date: Tue, 12 Oct 2010 15:44:50 +0000 Subject: [Tutor] urllib problem In-Reply-To: <201010130151.16436.steve@pearwood.info> References: , <201010122358.03905.steve@pearwood.info>, <201010130151.16436.steve@pearwood.info> Message-ID: ---------------------------------------- > From: steve at pearwood.info > To: tutor at python.org > Date: Wed, 13 Oct 2010 01:51:16 +1100 > Subject: Re: [Tutor] urllib problem > > On Tue, 12 Oct 2010 11:58:03 pm Steven D'Aprano wrote: > > On Tue, 12 Oct 2010 11:40:17 pm Roelof Wobben wrote: > > > Hoi, > > > > > > I have this programm : > > > > > > import urllib > > > import re > > > f = > > > urllib.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.ph > > >p? nothing=6") inhoud = f.read() > > > f.close() > > > nummer = re.search('[0-9]', inhoud) > > > volgende = int(nummer.group()) > > > teller = 1 > > > while teller <= 3 : > > > url = > > > "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=" + > > > str(volgende) f = urllib.urlopen(url) > > > inhoud = f.read() > > > f.close() > > > nummer = re.search('[0-9]', inhoud) > > > print "nummer is", nummer.group() > > > volgende = int(nummer.group()) > > > print volgende > > > teller = teller + 1 > > > > > > but now the url changes but volgende not. > > > What do I have done wrong ? > > > > Each time through the loop, you set volgende to the same result: > > > > nummer = re.search('[0-9]', inhoud) > > volgende = int(nummer.group()) > > > > Since inhoud never changes, and the search never changes, the search > > result never changes, and volgende never changes. > > Wait, sorry, inhoud should change... I missed the line inhoud = f.read() > > My mistake, sorry about that. However, I can now see what is going > wrong. Your regular expression only looks for a single digit: > > re.search('[0-9]', inhoud) > > If you want any number of digits, you need '[0-9]+' instead. > > > Starting from the first URL: > > >>> f = urllib.urlopen( > ... "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=6") > >>> inhoud = f.read() > >>> f.close() > >>> print inhoud > and the next nothing is 87599 > > > but: > > >>> nummer = re.search('[0-9]', inhoud) > >>> nummer.group() > '8' > > See, you only get the first digit. Then looking up the page with > nothing=8 gives a first digit starting with 5, and then you get stuck > on 5 forever: > > >>> urllib.urlopen( > ... "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=8").read() > 'and the next nothing is 59212' > >>> urllib.urlopen( > ... "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=5").read() > 'and the next nothing is 51716' > > > You need to add a + to the regular expression, which means "one or more > digits" instead of "a single digit". > > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Hoi Steven, Finally solved this puzzle. Now the next one of the 33 puzzles. Roelof From rwobben at hotmail.com Tue Oct 12 19:35:09 2010 From: rwobben at hotmail.com (Roelof Wobben) Date: Tue, 12 Oct 2010 17:35:09 +0000 Subject: [Tutor] pickle problem Message-ID: Hello, I have this code : import urllib import pickle image = urllib.URLopener() image.retrieve("http://www.pythonchallenge.com/pc/def/peak.html","banner.p" ) plaatje = open("banner.p", "rb") plaatje2 = pickle.load(plaatje) But it gives this output : Traceback (most recent call last): File "C:\Users\wobben\workspace\oefeningen\src\test2.py", line 7, in plaatje2 = pickle.load(plaatje) File "C:\Python27\lib\pickle.py", line 1378, in load return Unpickler(file).load() File "C:\Python27\lib\pickle.py", line 858, in load dispatch[key](self) KeyError: '<' What does this mean ? Roelof From josep.m.fontana at gmail.com Tue Oct 12 19:52:24 2010 From: josep.m.fontana at gmail.com (Josep M. Fontana) Date: Tue, 12 Oct 2010 19:52:24 +0200 Subject: [Tutor] Using contents of a document to change file names, (was Re: how to extract data only after a certain ...) In-Reply-To: <4CB301C9.1070207@compuscan.co.za> References: <4CB301C9.1070207@compuscan.co.za> Message-ID: Thanks Chris and Alan, OK, I see. Now that I managed to build the dictionary, I did a print to confirm that indeed the dictionary was created and it had the intended contents and I was surprised to see that the order of the items in it was totally changed. So the text file from which the dictionary was created was sorted in alphabetical order ('A-01,1334', 'A-02,1234',...'I-01,1334'...), but when I print the dictionary, I get: {'I-02': '1399', 'I-01': '1374', 'D-09': '1524', 'I-07': '1399' .....} I don't think this will be a problem for what I want to do next but I'm curious to know why the order is all changed in a way that doesn't seem to be very intuitive. Josep M. On Mon, Oct 11, 2010 at 2:23 PM, Christian Witts wrote: > > What you should be doing is: > > fileNameCentury = > open('/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNamesYears.txt', > 'r') > dct = {} > for line in fileNameCentury: #File objects have built-in iteration > key, value = line.strip().split(',') > dct[key] = value > > Hope that helps. > It did. A great deal! Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Tue Oct 12 20:37:27 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 12 Oct 2010 14:37:27 -0400 Subject: [Tutor] Using contents of a document to change file names, (was Re: how to extract data only after a certain ...) In-Reply-To: References: <4CB301C9.1070207@compuscan.co.za> Message-ID: On Tue, Oct 12, 2010 at 1:52 PM, Josep M. Fontana wrote: > Thanks Chris and Alan, > > OK, I see. Now that I managed to build the dictionary, I did a print to > confirm that indeed the dictionary was created and it had the intended > contents and I was surprised to see that the order of the items in it was > totally changed. So the text file from which the dictionary was created was > sorted in alphabetical order ('A-01,1334', 'A-02,1234',...'I-01,1334'...), > but when I print the dictionary, I get: {'I-02': '1399', 'I-01': '1374', > 'D-09': '1524', 'I-07': '1399' .....} > > I don't think this will be a problem for what I want to do next but I'm > curious to know why the order is all changed in a way that doesn't seem to > be very intuitive. > Here is a discussion of how to iterate over a dictionary in sorted order: http://stackoverflow.com/questions/364519/in-python-how-to-i-iterate-over-a-dictionary-in-sorted-order -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Tue Oct 12 21:15:57 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 12 Oct 2010 15:15:57 -0400 Subject: [Tutor] Using contents of a document to change file names, (was Re: how to extract data only after a certain ...) In-Reply-To: References: <4CB301C9.1070207@compuscan.co.za> Message-ID: On Tue, Oct 12, 2010 at 2:46 PM, Josep M. Fontana wrote: > > > On Tue, Oct 12, 2010 at 8:37 PM, Joel Goldstick wrote: > >> >> >> On Tue, Oct 12, 2010 at 1:52 PM, Josep M. Fontana < >> josep.m.fontana at gmail.com> wrote: >> >>> Thanks Chris and Alan, >>> >>> OK, I see. Now that I managed to build the dictionary, I did a print to >>> confirm that indeed the dictionary was created and it had the intended >>> contents and I was surprised to see that the order of the items in it was >>> totally changed. So the text file from which the dictionary was created was >>> sorted in alphabetical order ('A-01,1334', 'A-02,1234',...'I-01,1334'...), >>> but when I print the dictionary, I get: {'I-02': '1399', 'I-01': '1374', >>> 'D-09': '1524', 'I-07': '1399' .....} >>> >>> I don't think this will be a problem for what I want to do next but I'm >>> curious to know why the order is all changed in a way that doesn't seem to >>> be very intuitive. >>> >> >> Here is a discussion of how to iterate over a dictionary in sorted order: >> >> >> http://stackoverflow.com/questions/364519/in-python-how-to-i-iterate-over-a-dictionary-in-sorted-order >> > > > Thanks very much, Joel, for your quick response. This information might be > useful but my question was really more out of curiosity to know how Python > works. I don't understand why a dictionary that was created by iterating > over a list that was sorted turns out to be unsorted. I would not have been > surprised if the order was reversed, but the order that I got makes no sense > to me. What is the logic of the order in which the items in this dictionary > are arranged? > > JM > > >> >> Well, dictionaries use keys which are stored in hash tables. A dictionary is a mapping. It doesn't require an order. It just requires that given a key, it will return a value. While I don't know enough about this to be instructive, it makes looking up values faster. When the dictionary is retrieved, its order depends on the hashed values rather than the keys themself. If you play around with a dictionary, adding new key-value pairs, then displaying the dict, you might find that the order changes in no particular way. Here is a wikipedia article. http://en.wikipedia.org/wiki/Hash_table A list, on the other hand IS ordered (its a squence), and so when you add or remove values from a list, the order is maintained. -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Tue Oct 12 21:39:19 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Tue, 12 Oct 2010 21:39:19 +0200 Subject: [Tutor] Using contents of a document to change file names, (was Re: how to extract data only after a certain ...) In-Reply-To: References: <4CB301C9.1070207@compuscan.co.za> Message-ID: On 12 October 2010 21:15, Joel Goldstick wrote: > When the dictionary is retrieved, its order depends on the hashed values > rather than the keys themself. If (big IF here) you really need an ordered dict you can use the OrderedDict from the collections module. However this will only guarantee *insertion* order (except for existing keys). Do make sure you read pep 372 [1]. Greets Sander [1] http://www.python.org/dev/peps/pep-0372/ From joel.goldstick at gmail.com Tue Oct 12 21:53:22 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 12 Oct 2010 15:53:22 -0400 Subject: [Tutor] Using contents of a document to change file names, (was Re: how to extract data only after a certain ...) In-Reply-To: References: <4CB301C9.1070207@compuscan.co.za> Message-ID: On Tue, Oct 12, 2010 at 3:39 PM, Sander Sweers wrote: > On 12 October 2010 21:15, Joel Goldstick wrote: > > When the dictionary is retrieved, its order depends on the hashed values > > rather than the keys themself. > > If (big IF here) you really need an ordered dict you can use the > OrderedDict from the collections module. However this will only > guarantee *insertion* order (except for existing keys). Do make sure > you read pep 372 [1]. > > Greets > Sander > > [1] http://www.python.org/dev/peps/pep-0372/ > _______________________________________________ > It seems a common practice to create an ordered list of the keys, then iterate over the list to operate on the dictionary. -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From bvivek1978 at gmail.com Tue Oct 12 22:04:17 2010 From: bvivek1978 at gmail.com (b vivek) Date: Wed, 13 Oct 2010 01:34:17 +0530 Subject: [Tutor] Compiling python and SQlite3 prob on ubuntu 10.10 Message-ID: Hi, I am using the recently released python10.10 and wanted to install python2.5.4 on my system to do some app engine development, and this is what I did:- 1.I downloaded Python-2.5.4.tgz.tar from the python site. 2.cd Downloads tar -xvzf Python-2.5.4.tgz.tar cd Python-2.5.4 ./configure --prefix=/usr/local/python2.5.4 make make test sudo make install sudo ln -s /usr/local/python2.5.4/bin/python /usr/bin/python2.5.4 However after installing it, when I try using it , I get the below error:- File "/home/vivek/google_appengine/google/appengine/datastore/datastore_sqlite_stub.py", line 52, in import sqlite3 File "/usr/local/python2.5.4/lib/python2.5/sqlite3/__init__.py", line 24, in from dbapi2 import * File "/usr/local/python2.5.4/lib/python2.5/sqlite3/dbapi2.py", line 27, in from _sqlite3 import * ImportError: No module named _sqlite3 How do I get to solve this. Need someone to help me here please! -------------- next part -------------- An HTML attachment was scrubbed... URL: From knacktus at googlemail.com Tue Oct 12 22:04:55 2010 From: knacktus at googlemail.com (Knacktus) Date: Tue, 12 Oct 2010 22:04:55 +0200 Subject: [Tutor] pickle problem In-Reply-To: References: Message-ID: <4CB4BF67.8010200@googlemail.com> Am 12.10.2010 19:35, schrieb Roelof Wobben: > > > Hello, > > I have this code : > > import urllib > import pickle > > image = urllib.URLopener() > image.retrieve("http://www.pythonchallenge.com/pc/def/peak.html","banner.p" ) > plaatje = open("banner.p", "rb") > plaatje2 = pickle.load(plaatje) > > But it gives this output : > > Traceback (most recent call last): > File "C:\Users\wobben\workspace\oefeningen\src\test2.py", line 7, in > plaatje2 = pickle.load(plaatje) > File "C:\Python27\lib\pickle.py", line 1378, in load > return Unpickler(file).load() > File "C:\Python27\lib\pickle.py", line 858, in load > dispatch[key](self) > KeyError: '<' > The last error indicates that a python module (pickle.py) tried to access a dictionary (dispatch) with the key "<". The expected value seems to be a callable as indicated by the (self). But there's no key "<" in it. So, probably, this module was called with improper data. If you follow up your stack trace to where the error in your code occurs, you'll see that something is wrong with "unpickling" the data in your plaatje file. Obviously, the data could be passed to the pickle module. So no read error on the file. What's left? There's probably an issue with the data in the file. Now, you have to know that pickling reads and writes Python objects in a certain format. That means you can only load data with pickling, which was created by pickling. Question: Is your data in "banner.p" properly formatted pickling data? Note: There're different formats for pickling. Check the docs or a very nice source for examples of the Python Standard lib: PyMOTW (Google first hit); there's an article about pickling among others. But first should be the standard documentation. > > What does this mean ? > > Roelof > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From adam.jtm30 at gmail.com Tue Oct 12 22:08:12 2010 From: adam.jtm30 at gmail.com (Adam Bark) Date: Tue, 12 Oct 2010 21:08:12 +0100 Subject: [Tutor] pickle problem In-Reply-To: References: Message-ID: <4CB4C02C.8000307@gmail.com> On 12/10/10 18:35, Roelof Wobben wrote: > Hello, > > I have this code : > > import urllib > import pickle > > image = urllib.URLopener() > image.retrieve("http://www.pythonchallenge.com/pc/def/peak.html","banner.p" ) > plaatje = open("banner.p", "rb") > plaatje2 = pickle.load(plaatje) > > But it gives this output : > > Traceback (most recent call last): > File "C:\Users\wobben\workspace\oefeningen\src\test2.py", line 7, in > plaatje2 = pickle.load(plaatje) > File "C:\Python27\lib\pickle.py", line 1378, in load > return Unpickler(file).load() > File "C:\Python27\lib\pickle.py", line 858, in load > dispatch[key](self) > KeyError: '<' > > > What does this mean ? > It means that it's trying to access a sequence with the key '<' but it's not working. It looks like the problem is you're trying to open peak.html as a pickle but it is actually an html file. HTH, Adam. From joel.goldstick at gmail.com Tue Oct 12 22:32:22 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 12 Oct 2010 16:32:22 -0400 Subject: [Tutor] pickle problem In-Reply-To: References: Message-ID: On Tue, Oct 12, 2010 at 1:35 PM, Roelof Wobben wrote: > > > Hello, > > I have this code : > > import urllib > import pickle > > image = urllib.URLopener() > image.retrieve("http://www.pythonchallenge.com/pc/def/peak.html","banner.p" > ) > plaatje = open("banner.p", "rb") > plaatje2 = pickle.load(plaatje) > > But it gives this output : > > Traceback (most recent call last): > File "C:\Users\wobben\workspace\oefeningen\src\test2.py", line 7, in > > plaatje2 = pickle.load(plaatje) > File "C:\Python27\lib\pickle.py", line 1378, in load > return Unpickler(file).load() > File "C:\Python27\lib\pickle.py", line 858, in load > dispatch[key](self) > KeyError: '<' > > > What does this mean ? > I have heard of pickle, but never used it. I looked it up in Learning Python (O'Reilley p 236). The example they give shows that you need to pickle your object, and put it in a file with pickle(dump(D,F) where D is a dictionary in the example and F is a file object. You then can retrieve it with pickle.load(F). The thing you are trying to load is a file that was a copy of the contents of the url. It is html, and not 'pickled' I think you are missing a line or two of code from your example tutorial > > Roelof > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Wed Oct 13 01:05:52 2010 From: wprins at gmail.com (Walter Prins) Date: Wed, 13 Oct 2010 00:05:52 +0100 Subject: [Tutor] pickle problem In-Reply-To: References: Message-ID: On 12 October 2010 18:35, Roelof Wobben wrote: > image = urllib.URLopener() > image.retrieve("http://www.pythonchallenge.com/pc/def/peak.html","banner.p" > ) > OK firstly, image is an URLopener, so whatever URL you specify, that's the file that it will download. (I would therefore suggest you give it a slightly better name next time -- you're after all not even going to be downloading an image so the name "image" is arguably just confusing.) Now, back to the problem. Look closely, what file are you downloading? Right, you're downloading "http://www.pythonchallenge.com/pc/def/peak.html" However... that's the HTML page itself! So, what you're doing is to download the "peak.html" HTML file from the site, and save it locally as "banner.p"... But... what you really want is the "banner.p" file *on the website*. (As alluded to in the page source, if you view the web page above in your browser...) So..., you need to construct the URL to the "banner.p" pickle file on the website and then retrieve it. (Hint: Just replace peak.html with banner.p in the page URL) You don't even have to do this with Python, you can fetch the pickle file by hand with your browser first if you want, just punch the URL into your browser and watch it download the banner.p file. (But, it's fun to do it in Python! ) Apologies if I've given away too much! Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Oct 13 02:13:19 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 13 Oct 2010 01:13:19 +0100 Subject: [Tutor] urllib problem References: , <201010122358.03905.steve@pearwood.info>, <201010130151.16436.steve@pearwood.info> Message-ID: "Roelof Wobben" wrote > Finally solved this puzzle. > Now the next one of the 33 puzzles. Don;t be surprised if you get stuck. Python Challenge is quite tricky and is deliberately designed to make you explore parts of the standard library you might not otherwise find. Expect to do a lot of reading in the documebntation. Its really targeted at intermediate rather than novice programmers IMHO. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Wed Oct 13 02:27:52 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 13 Oct 2010 01:27:52 +0100 Subject: [Tutor] pickle problem References: Message-ID: "Roelof Wobben" wrote > image = urllib.URLopener() > image.retrieve("http://www.pythonchallenge.com/pc/def/peak.html","banner.p" > ) Roelof, Please do not post a series of request for help on each of the Python Challenge puzzles. They are intended to be challenging and to force you to consult the documentation so that you learn how to use the modules. But asking for help on this list will simply give the puzzles away to anyone else on the list who may be trying them or intending to try them in the future. > plaatje = open("banner.p", "rb") > plaatje2 = pickle.load(plaatje) You are using the pickle module so have you read the documentation on pickle? Have you read examples in other tutorials on how to use pickle? Have you experimented with pickle and got it to work outside the context of the challenge? > What does this mean ? It means you are not using pickle properly. Please read the documentation. Sorry to sound harsh but this is different to you trying to understand a tutorial and its exercises. This is supposed to be a challenge for you personally not the tutor list en-masse. If you get really stuck then ask for hints, but please do your homework first. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From smokefloat at gmail.com Wed Oct 13 07:25:55 2010 From: smokefloat at gmail.com (David Hutto) Date: Wed, 13 Oct 2010 01:25:55 -0400 Subject: [Tutor] SQLite3 DB Field Alphabetizing In-Reply-To: References: Message-ID: On Tue, Oct 12, 2010 at 10:23 AM, Alan Gauld wrote: > > "David Hutto" wrote > >> > Did you include an ORDER BY? >> >> See three posts above, line 6. > > Looks like our posts crossed in transit :-) > > So you order by graphname, and you only have a single field of that name? > But then when you put it into the list you only use part of graphname > converted > to a string and stripped: This is actually an example derived from the code I used it in. The full code is here: http://pastebin.com/9H54fEYb I forgot to give the names in the link, but the .py files are seperated, and should be named as follows. The first module should be named dataplotmain.py The second should be named dpappfunc.py The third should be named mainpanel.py and the dbfile should be in the directory placed on line 58 of mainpanel.py It's still in rough draft form, but feel free to comment, and not some will be taken out, and much more placed in. > > str(graphname[0]).lstrip('u')) > > So what did the field look like before you modified it? Since that is what > determines the way that ORDER BY ordered it? And what is > graphname[0] before you convert it to a string? (It looks like it > is already a string?!) Below is the output before converting: self.liorder.append(graphname) to self.liorder.append(str(graphname[0]).lstrip('u')) [(u'.hjvkjgfkj/bdgfkjbg', u''), (u'bbbbbbbbbbbbuuzzzzzzzzz', u'Pie Chart'), (u'jgkgyckghc', u''), (u'kfhhv ', u''), (u'khfhf', u''), (u'test', u''), (u'test10', u''), (u'test2', u'Dashed/Dotted'), (u'test3', u'Pie Chart'), (u'test346w43', u''), (u'test4', u'Scatter Plot'), (u'test5', u''), (u'test6', u''), (u'test7', u''), (u'test7uyuy', u''), (u'test8', u''), (u'test9', u''), (u'testgraph', u''), (u'u;s;juf;sfkh', u''), (u'zzrerhshhjrs', u'')] > > BTW. > Its unusual to have to extract sub fields from a SQL result since to > maximise the power of SQL its better to store all "subfields" in > separate fields of the table. Almost to that part. As you can see from the full script in the link above, it's still early on, so I just got the insert into db portion in, and was working on accessing it, before placing in the overall data structure I want to use. > > One of the complexities of debugging any database program is that > almost as much depends on the data structures and content as it > does on the code. Without seeing the data that is beintg operated > upon its hard to be precise. The above code should clarify more than my original posting. > > What happens if you execute the SQL at the sqlite interactive > prompt? Does it give the same result? haven't used the sqlite prompt yet, Haven't needed to yet. > > > PS. You don't need the commit() for a SELECT statement, you aren't > changing anything. Consider it removed. > > > Alan G. > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From smokefloat at gmail.com Wed Oct 13 07:44:34 2010 From: smokefloat at gmail.com (David Hutto) Date: Wed, 13 Oct 2010 01:44:34 -0400 Subject: [Tutor] SQLite3 DB Field Alphabetizing In-Reply-To: References: Message-ID: Quick note: forgot to add that the initial db file is setup with the following: import sqlite3 as lite class db(object): def onNewProjSQLDB(self): self.con = lite.connect('/home/david/pythonfiles/pythonscripts/roughdraftapps/dataplot3/db/dpdb.db') self.cur = self.con.cursor() #rows and columns are singular in project, and position is individual to graph/overlayed graphs self.cur.execute('''create table projectbob(graphname, typeograph )''') # Save (commit) the changes self.con.commit() self.cur.close() self.con.close() db = db() db.onNewProjSQLDB() From alan.gauld at btinternet.com Wed Oct 13 10:01:44 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 13 Oct 2010 09:01:44 +0100 Subject: [Tutor] SQLite3 DB Field Alphabetizing References: Message-ID: "David Hutto" wrote > Below is the output before converting: > > [(u'.hjvkjgfkj/bdgfkjbg', u''), (u'bbbbbbbbbbbbuuzzzzzzzzz', u'Pie > Chart'), (u'jgkgyckghc', u''), (u'kfhhv ', u''), (u'khfhf', u''), > (u'test', u''), (u'test10', u''), (u'test2', u'Dashed/Dotted'), > (u'test3', u'Pie Chart'), (u'test346w43', u''), (u'test4', u'Scatter > Plot'), (u'test5', u''), (u'test6', u''), (u'test7', u''), > (u'test7uyuy', u''), (u'test8', u''), (u'test9', u''), > (u'testgraph', > u''), (u'u;s;juf;sfkh', u''), (u'zzrerhshhjrs', u'')] And there is the answer to your question. The string that you thought started with ; actually starts with a 'u' But you strip the 'u;' off that entry so it appears to be badly ordered. So the next question is why do you strip the 'u' off? I suspect the answer to that also lies here. I suspect you think you need to strip all the 'u's from the front of these results but in fact those 'u's lie outside the string, they are a part of Python's representation of a unicode string, not a part of the string. So if you remove the call to strip() your code will return the result you expected. > > What happens if you execute the SQL at the sqlite interactive > > prompt? Does it give the same result? > > haven't used the sqlite prompt yet, Haven't needed to yet. You should. Consider it like the >>> prompt in Python, an invaluable aid in experimenting and developing working SQL statements. Not to use the interactive prompts while writing your code is to lose a great deal of the productivity power of Python and SQLite. Why debug a whole script when you can debug a single line with instant feedback? HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From joel.goldstick at gmail.com Wed Oct 13 14:17:29 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 13 Oct 2010 08:17:29 -0400 Subject: [Tutor] SQLite3 DB Field Alphabetizing In-Reply-To: References: Message-ID: On Wed, Oct 13, 2010 at 4:01 AM, Alan Gauld wrote: > "David Hutto" wrote > > Below is the output before converting: >> >> [(u'.hjvkjgfkj/bdgfkjbg', u''), (u'bbbbbbbbbbbbuuzzzzzzzzz', u'Pie >> Chart'), (u'jgkgyckghc', u''), (u'kfhhv ', u''), (u'khfhf', u''), >> (u'test', u''), (u'test10', u''), (u'test2', u'Dashed/Dotted'), >> (u'test3', u'Pie Chart'), (u'test346w43', u''), (u'test4', u'Scatter >> Plot'), (u'test5', u''), (u'test6', u''), (u'test7', u''), >> (u'test7uyuy', u''), (u'test8', u''), (u'test9', u''), (u'testgraph', >> u''), (u'u;s;juf;sfkh', u''), (u'zzrerhshhjrs', u'')] >> > > And there is the answer to your question. > The string that you thought started with ; actually starts with a 'u' > But you strip the 'u;' off that entry so it appears to be badly ordered. > > So the next question is why do you strip the 'u' off? > > I suspect the answer to that also lies here. > I suspect you think you need to strip all the 'u's from the front of > these results but in fact those 'u's lie outside the string, they > are a part of Python's representation of a unicode string, > not a part of the string. > > So if you remove the call to strip() your code will return > the result you expected. > > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > Not to belabor the point too much, I plucked one of your strings out and printed it. Looking at your data, all those u' prefixes you thought you were removing were (in a sense) never really there! The only u you removed was the one near the end that was followed with a semicolon. print u'zzrerhshhjrs' zzrerhshhjrs -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From smokefloat at gmail.com Wed Oct 13 15:13:50 2010 From: smokefloat at gmail.com (David Hutto) Date: Wed, 13 Oct 2010 09:13:50 -0400 Subject: [Tutor] SQLite3 DB Field Alphabetizing In-Reply-To: References: Message-ID: On Wed, Oct 13, 2010 at 8:17 AM, Joel Goldstick wrote: > > > On Wed, Oct 13, 2010 at 4:01 AM, Alan Gauld > wrote: >> >> "David Hutto" wrote >> >>> Below is the output before converting: >>> >>> [(u'.hjvkjgfkj/bdgfkjbg', u''), (u'bbbbbbbbbbbbuuzzzzzzzzz', u'Pie >>> Chart'), (u'jgkgyckghc', u''), (u'kfhhv ', u''), (u'khfhf', u''), >>> (u'test', u''), (u'test10', u''), (u'test2', u'Dashed/Dotted'), >>> (u'test3', u'Pie Chart'), (u'test346w43', u''), (u'test4', u'Scatter >>> Plot'), (u'test5', u''), (u'test6', u''), (u'test7', u''), >>> (u'test7uyuy', u''), (u'test8', u''), (u'test9', u''), (u'testgraph', >>> u''), (u'u;s;juf;sfkh', u''), (u'zzrerhshhjrs', u'')] >> >> And there is the answer to your question. >> The string that you thought started with ; actually starts with a 'u' >> But you strip the 'u;' off that entry so it appears to be badly ordered. >> >> So the next question is why do you strip the 'u' off? >> >> I suspect the answer to that also lies here. >> I suspect you think you need to strip all the 'u's from the front of >> these results but in fact those 'u's lie outside the string, they >> are a part of Python's representation of a unicode string, >> not a part of the string. >> >> So if you remove the call to strip() your code will return >> the result you expected. >> >> >> -- >> Alan Gauld >> Author of the Learn to Program web site >> http://www.alan-g.me.uk/ >> >> > Not to belabor the point too much, I plucked one of your strings out and > printed it.? Looking at your data, all those u' prefixes you thought you > were removing were (in a sense) never really there!? The only u you removed > was the one near the end that was followed with a semicolon. > I see it now. I knew that the u outside ' ' is the coding for the string, but I thought I had to strip it before using it since that was how it showed up. The bug of course would be that graphs that start with u would go to the second letter, but the u would still be used in alphabetization, because the alphabetizing is prior to stripping. Thanks, for the help with that. > > print u'zzrerhshhjrs' > zzrerhshhjrs > > -- > Joel Goldstick > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > From steve at pearwood.info Wed Oct 13 15:27:36 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 14 Oct 2010 00:27:36 +1100 Subject: [Tutor] SQLite3 DB Field Alphabetizing In-Reply-To: References: Message-ID: <201010140027.37297.steve@pearwood.info> On Thu, 14 Oct 2010 12:13:50 am David Hutto wrote: > I see it now. I knew that the u outside ' ' is the coding for the > string, but I thought I had to strip it before using it since that > was how it showed up. The bug of course would be that graphs that > start with u would go to the second letter, but the u would still be > used in alphabetization, because the alphabetizing is prior to > stripping. No, the u is not part of the string, it is part of the *syntax* for the string, just like the quotation marks. The first character of "abc" is a and not ", and the first character of u"abc" is also a and not u. Another way to think of it... the u" " of Unicode strings is just a delimiter, just like the [ ] of lists or { } of dicts -- it's not part of the string/list/dict. In Python 3 this confusion is lessened. Unicode strings (characters) are written using just " as the delimiter (or ' if you prefer), instead of u" " as used by Python 2. Byte strings are written using b" " instead. This makes the common case (text strings) simple, and the uncommon case (byte strings) more complicated. -- Steven D'Aprano From smokefloat at gmail.com Wed Oct 13 19:35:22 2010 From: smokefloat at gmail.com (David Hutto) Date: Wed, 13 Oct 2010 13:35:22 -0400 Subject: [Tutor] SQLite3 DB Field Alphabetizing In-Reply-To: <201010140027.37297.steve@pearwood.info> References: <201010140027.37297.steve@pearwood.info> Message-ID: On Wed, Oct 13, 2010 at 9:27 AM, Steven D'Aprano wrote: > On Thu, 14 Oct 2010 12:13:50 am David Hutto wrote: >> I see it now. I knew that the u outside ' ' is the coding for the >> string, but I thought I had to strip it before using it since that >> was how it showed up. The bug of course would be that graphs that >> start with u would go to the second letter, but the u would still be >> used in alphabetization, because the alphabetizing is prior to >> stripping. > > No, the u is not part of the string, it is part of the *syntax* for the > string, just like the quotation marks. The first character of "abc" is > a and not ", and the first character of u"abc" is also a and not u. > > Another way to think of it... the u" " of Unicode strings is just a > delimiter, just like the [ ] of lists or { } of dicts -- it's not part > of the string/list/dict. I know, that why I said above. >>I knew that the u outside ' ' is the coding for the string, . > > In Python 3 this confusion is lessened. Unicode strings (characters) are > written using just " as the delimiter (or ' if you prefer), instead of > u" " as used by Python 2. Byte strings are written using b" " instead. > This makes the common case (text strings) simple, and the uncommon case > (byte strings) more complicated. Maybe it's how I said it here that was confusing: >but the u would still be used in alphabetization, because the alphabetizing is prior to stripping by 'u' here, i meant that the only table I used with a u as the first letter(i.e. u'u;table') came back as ';table'. So I was seeing the aftermath of stripping u, but no others were stripped except that on. And since I had placed gibberish in at random, I didn't remember the 'u' in the actual table name. > > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > But thanks for the help buddy pal. From ghashsnaga at gmail.com Wed Oct 13 22:16:21 2010 From: ghashsnaga at gmail.com (Ara Kooser) Date: Wed, 13 Oct 2010 14:16:21 -0600 Subject: [Tutor] Merging Text Files Message-ID: Hello all, I am working on merging two text files with fields separated by commas. The files are in this format: File ONE: *Species, Protein ID, E value, Length* Streptomyces sp. AA4, ZP_05482482, 2.8293600000000001e-140, 5256, Streptomyces sp. AA4, ZP_05482482, 8.0333299999999997e-138, 5256, Streptomyces sp. AA4, ZP_05482482, 1.08889e-124, 5256, Streptomyces sp. AA4, ZP_07281899, 2.9253900000000001e-140, 5260, File TWO: *Protein ID, Locus Tag, Start/Stop* ZP_05482482, StAA4_010100030484, complement(NZ_ACEV01000078.1:25146..40916) ZP_07281899, SSMG_05939, complement(NZ_GG657746.1:6565974..6581756) I looked around for other posts about merging text files and I have this program: one = open("final.txt",'r') two = open("final_gen.txt",'r') merge = open("merged.txt",'w') merge.write("Species, Locus_Tag, E_value, Length, Start/Stop\n") for line in one: print(line.rstrip() + two.readline().strip()) merge.write(str([line.rstrip() + two.readline().strip()])) merge.write("\n") merge.close() inc = file("merged.txt","r") outc = open("final_merge.txt","w") for line in inc: line = line.replace('[','') line = line.replace(']','') line = line.replace('{','') line = line.replace('}','') outc.write(line) inc.close() outc.close() one.close() two.close() This does merge the files. Streptomyces sp. AA4, ZP_05482482, 2.8293600000000001e-140, 5256,ZP_05482482, StAA4_010100030484, complement(NZ_ACEV01000078.1:25146..40916) Streptomyces sp. AA4, ZP_05482482, 8.0333299999999997e-138, 5256,ZP_05477599, StAA4_010100005861, NZ_ACEV01000013.1:86730..102047 But file one has multiple instances of the same Protein ID such as ZP_05482482. So the data doesn't line up anymore. I would like the program to search for each Protein ID number and write the entry from file 2 in each place and then move on to the next ID number. Example of desired output: Streptomyces sp. AA4, ZP_05482482, StAA4_010100030484, 2.8293600000000001e-140, 5256, complement(NZ_ACEV01000078.1:25146..40916) Streptomyces sp. AA4, ZP_05482482, StAA4_010100030484, 8.0333299999999997e-138, 5256, complement(NZ_ACEV01000078.1:25146..40916) I was thinking about writing the text files into a dictionary and then searching for each ID and then insert the content from file TWO into where the IDs match. But I am not sure how to start. Is there a more pythony way to go about doing this? Thank you for your time and help. Regards, Ara -- Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis an sub cardine glacialis ursae. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rob at jackiewicz.ca Wed Oct 13 22:30:56 2010 From: rob at jackiewicz.ca (Robert Jackiewicz) Date: Wed, 13 Oct 2010 20:30:56 +0000 (UTC) Subject: [Tutor] Merging Text Files References: Message-ID: On Wed, 13 Oct 2010 14:16:21 -0600, Ara Kooser wrote: > Hello all, > > I am working on merging two text files with fields separated by > commas. > The files are in this format: > > File ONE: > *Species, Protein ID, E value, Length* Streptomyces sp. AA4, > ZP_05482482, 2.8293600000000001e-140, 5256, Streptomyces sp. AA4, > ZP_05482482, 8.0333299999999997e-138, 5256, Streptomyces sp. AA4, > ZP_05482482, 1.08889e-124, 5256, Streptomyces sp. AA4, ZP_07281899, > 2.9253900000000001e-140, 5260, > > File TWO: > *Protein ID, Locus Tag, Start/Stop* > ZP_05482482, StAA4_010100030484, > complement(NZ_ACEV01000078.1:25146..40916) ZP_07281899, SSMG_05939, > complement(NZ_GG657746.1:6565974..6581756) > > I looked around for other posts about merging text files and I have this > program: > one = open("final.txt",'r') > two = open("final_gen.txt",'r') > > merge = open("merged.txt",'w') > merge.write("Species, Locus_Tag, E_value, Length, Start/Stop\n") > > for line in one: > print(line.rstrip() + two.readline().strip()) > merge.write(str([line.rstrip() + two.readline().strip()])) > merge.write("\n") > merge.close() > > inc = file("merged.txt","r") > outc = open("final_merge.txt","w") > for line in inc: > line = line.replace('[','') > line = line.replace(']','') > line = line.replace('{','') > line = line.replace('}','') > outc.write(line) > > inc.close() > outc.close() > one.close() > two.close() > > This does merge the files. > Streptomyces sp. AA4, ZP_05482482, 2.8293600000000001e-140, > 5256,ZP_05482482, StAA4_010100030484, > complement(NZ_ACEV01000078.1:25146..40916) Streptomyces sp. AA4, > ZP_05482482, 8.0333299999999997e-138, 5256,ZP_05477599, > StAA4_010100005861, NZ_ACEV01000013.1:86730..102047 > > But file one has multiple instances of the same Protein ID such as > ZP_05482482. So the data doesn't line up anymore. I would like the > program to search for each Protein ID number and write the entry from > file 2 in each place and then move on to the next ID number. > > Example of desired output: > Streptomyces sp. AA4, ZP_05482482, StAA4_010100030484, > 2.8293600000000001e-140, 5256, > complement(NZ_ACEV01000078.1:25146..40916) Streptomyces sp. AA4, > ZP_05482482, StAA4_010100030484, 8.0333299999999997e-138, 5256, > complement(NZ_ACEV01000078.1:25146..40916) > > I was thinking about writing the text files into a dictionary and then > searching for each ID and then insert the content from file TWO into > where the IDs match. But I am not sure how to start. Is there a more > pythony way to go about doing this? > > Thank you for your time and help. > > Regards, > Ara Why don't you try using the csv library which is part of the standard python library to parse you files. It allows simple and efficient manipulation of comma separated value files. -Rob Jackiewicz From emile at fenx.com Wed Oct 13 23:14:59 2010 From: emile at fenx.com (Emile van Sebille) Date: Wed, 13 Oct 2010 14:14:59 -0700 Subject: [Tutor] Merging Text Files In-Reply-To: References: Message-ID: On 10/13/2010 1:16 PM Ara Kooser said... > Hello all, > > I am working on merging two text files with fields separated by commas. > The files are in this format: > > File ONE: > *Species, Protein ID, E value, Length* > Streptomyces sp. AA4, ZP_05482482, 2.8293600000000001e-140, 5256, > Streptomyces sp. AA4, ZP_05482482, 8.0333299999999997e-138, 5256, > Streptomyces sp. AA4, ZP_05482482, 1.08889e-124, 5256, > Streptomyces sp. AA4, ZP_07281899, 2.9253900000000001e-140, 5260, > > File TWO: > *Protein ID, Locus Tag, Start/Stop* > ZP_05482482, StAA4_010100030484, complement(NZ_ACEV01000078.1:25146..40916) > ZP_07281899, SSMG_05939, complement(NZ_GG657746.1:6565974..6581756) > > I looked around for other posts about merging text files and I have this > program: > one = open("final.txt",'r') > two = open("final_gen.txt",'r') > > merge = open("merged.txt",'w') > merge.write("Species, Locus_Tag, E_value, Length, Start/Stop\n") > > for line in one: > print(line.rstrip() + two.readline().strip()) > merge.write(str([line.rstrip() + two.readline().strip()])) > merge.write("\n") > merge.close() > > inc = file("merged.txt","r") > outc = open("final_merge.txt","w") > for line in inc: > line = line.replace('[','') > line = line.replace(']','') > line = line.replace('{','') > line = line.replace('}','') > outc.write(line) > > inc.close() > outc.close() > one.close() > two.close() > > This does merge the files. > Streptomyces sp. AA4, ZP_05482482, 2.8293600000000001e-140, > 5256,ZP_05482482, StAA4_010100030484, > complement(NZ_ACEV01000078.1:25146..40916) > Streptomyces sp. AA4, ZP_05482482, 8.0333299999999997e-138, > 5256,ZP_05477599, StAA4_010100005861, NZ_ACEV01000013.1:86730..102047 > > But file one has multiple instances of the same Protein ID such as > ZP_05482482. So the data doesn't line up anymore. I would like the program > to search for each Protein ID number and write the entry from file 2 in each > place and then move on to the next ID number. > > Example of desired output: > Streptomyces sp. AA4, ZP_05482482, StAA4_010100030484, > 2.8293600000000001e-140, 5256, complement(NZ_ACEV01000078.1:25146..40916) > Streptomyces sp. AA4, ZP_05482482, StAA4_010100030484, > 8.0333299999999997e-138, 5256, complement(NZ_ACEV01000078.1:25146..40916) > > I was thinking about writing the text files into a dictionary and then > searching for each ID and then insert the content from file TWO into where > the IDs match. But I am not sure how to start. Is there a more pythony way > to go about doing this? > I would read in file two and build a dict from the Protein IDs, then pass file one, break out the Protein ID, and write the concatenated result out. Something like: [pyseudocode] PIDs = {} for proteinVals in FileTwo: ID = proteinVals.split()[0] PIDS[ID]=proteinVals for eachline in FileOne: ID = proteinVals.split()[1] rslt = "%s,%s" % (eachline,PIDS[ID]) outfile.write(rslt] HTH, Emile From mhw at doctors.net.uk Wed Oct 13 22:56:39 2010 From: mhw at doctors.net.uk (Matt Williams) Date: Wed, 13 Oct 2010 21:56:39 +0100 Subject: [Tutor] Merging Text Files In-Reply-To: References: Message-ID: <4CB61D07.90203@doctors.net.uk> Dear Ara, I have been working on something similar. In the end I used a dictionary for each line in the file, and stored data from each file in a different set. I then matched using one (or more) element from each dictionary. This is really very close doing a join in a database, though, and if I had more time you might want to explore that route (csv -> sqlite, manipulate using sqlobject/ sqlalchemy/ django/ etc.) the csv module has some good facilities for reading/ writing csv files. However, as yet I don't think it, or csvutilities, lets you do the sort of merging you say. HTH, Matt Robert Jackiewicz wrote: > On Wed, 13 Oct 2010 14:16:21 -0600, Ara Kooser wrote: > > >> Hello all, >> >> I am working on merging two text files with fields separated by >> commas. >> The files are in this format: >> >> File ONE: >> *Species, Protein ID, E value, Length* Streptomyces sp. AA4, >> ZP_05482482, 2.8293600000000001e-140, 5256, Streptomyces sp. AA4, >> ZP_05482482, 8.0333299999999997e-138, 5256, Streptomyces sp. AA4, >> ZP_05482482, 1.08889e-124, 5256, Streptomyces sp. AA4, ZP_07281899, >> 2.9253900000000001e-140, 5260, >> >> File TWO: >> *Protein ID, Locus Tag, Start/Stop* >> ZP_05482482, StAA4_010100030484, >> complement(NZ_ACEV01000078.1:25146..40916) ZP_07281899, SSMG_05939, >> complement(NZ_GG657746.1:6565974..6581756) >> >> I looked around for other posts about merging text files and I have this >> program: >> one = open("final.txt",'r') >> two = open("final_gen.txt",'r') >> >> merge = open("merged.txt",'w') >> merge.write("Species, Locus_Tag, E_value, Length, Start/Stop\n") >> >> for line in one: >> print(line.rstrip() + two.readline().strip()) >> merge.write(str([line.rstrip() + two.readline().strip()])) >> merge.write("\n") >> merge.close() >> >> inc = file("merged.txt","r") >> outc = open("final_merge.txt","w") >> for line in inc: >> line = line.replace('[','') >> line = line.replace(']','') >> line = line.replace('{','') >> line = line.replace('}','') >> outc.write(line) >> >> inc.close() >> outc.close() >> one.close() >> two.close() >> >> This does merge the files. >> Streptomyces sp. AA4, ZP_05482482, 2.8293600000000001e-140, >> 5256,ZP_05482482, StAA4_010100030484, >> complement(NZ_ACEV01000078.1:25146..40916) Streptomyces sp. AA4, >> ZP_05482482, 8.0333299999999997e-138, 5256,ZP_05477599, >> StAA4_010100005861, NZ_ACEV01000013.1:86730..102047 >> >> But file one has multiple instances of the same Protein ID such as >> ZP_05482482. So the data doesn't line up anymore. I would like the >> program to search for each Protein ID number and write the entry from >> file 2 in each place and then move on to the next ID number. >> >> Example of desired output: >> Streptomyces sp. AA4, ZP_05482482, StAA4_010100030484, >> 2.8293600000000001e-140, 5256, >> complement(NZ_ACEV01000078.1:25146..40916) Streptomyces sp. AA4, >> ZP_05482482, StAA4_010100030484, 8.0333299999999997e-138, 5256, >> complement(NZ_ACEV01000078.1:25146..40916) >> >> I was thinking about writing the text files into a dictionary and then >> searching for each ID and then insert the content from file TWO into >> where the IDs match. But I am not sure how to start. Is there a more >> pythony way to go about doing this? >> >> Thank you for your time and help. >> >> Regards, >> Ara >> > > Why don't you try using the csv library which is part of the standard > python library to parse you files. It allows simple and efficient > manipulation of comma separated value files. > > -Rob Jackiewicz > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From alan.gauld at btinternet.com Thu Oct 14 01:15:44 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 14 Oct 2010 00:15:44 +0100 Subject: [Tutor] Merging Text Files References: Message-ID: "Ara Kooser" wrote > I was thinking about writing the text files into a dictionary and > then > searching for each ID and then insert the content from file TWO into > where > the IDs match. But I am not sure how to start. Is there a more > pythony way > to go about doing this? Thats exactly how I would do it provided the data volumes fitr in memory, otherwise the same technique but using a simple single table database with SqlLite. Depending on what you want to do with the data after merging a database might even be the best solution since it will enable you to perform complex queries/filters etc on the final data. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From wallenpb at gmail.com Thu Oct 14 04:56:59 2010 From: wallenpb at gmail.com (Bill Allen) Date: Wed, 13 Oct 2010 21:56:59 -0500 Subject: [Tutor] Ide To use? Other extras to install? In-Reply-To: References: <201010110139.o9B1dR1s005897@krusty.intranet.com.mx> Message-ID: On Mon, Oct 11, 2010 at 8:53 AM, Jed Smith wrote: > On Sun, Oct 10, 2010 at 9:31 PM, Jorge Biquez wrote: > > > What would be the IDE you recommend me to install that would be almost > > transparent to be using in both platforms? > > > I personally best like the one that is most closely associated with Python, that is IDLE. It looks and works the same whether I am in Windows or Linux. It is not real fancy, but it does has a good text editor with reasonable features and the interactive window for testing and program run. It also supports enough debugging functions to be helpful. It is very simple and often comes with the Python distribution. (comes with the Windows distribution, separate install on Linux) Just my 2 cents worth. A buddy of mine prefers to program using the Kate editor on Linux. I sometimes use gedit on Linux and Notepad++ on Windows. In the end, the best IDE/editor is the one you know and feel comfortable with. I suggest you start with IDLE and see if you find some reason not to like it. --Bill -------------- next part -------------- An HTML attachment was scrubbed... URL: From ghashsnaga at gmail.com Thu Oct 14 05:42:14 2010 From: ghashsnaga at gmail.com (Ara Kooser) Date: Wed, 13 Oct 2010 21:42:14 -0600 Subject: [Tutor] Merging Text Files Message-ID: Thank you for all of the advice. I am going to try the dictionary route first thing tomorrow. This code is a part of larger code theat: 1) quires the BLAST database using BioPython 2) parses the data using BioPython, 3) dumps to text files 4) then merges the text files and sorts them. Somewhere down stream this goes into a spreadsheet. Regards, Ara -- Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis an sub cardine glacialis ursae. -------------- next part -------------- An HTML attachment was scrubbed... URL: From delegbede at dudupay.com Thu Oct 14 08:12:48 2010 From: delegbede at dudupay.com (delegbede at dudupay.com) Date: Thu, 14 Oct 2010 06:12:48 +0000 Subject: [Tutor] Zip Command Message-ID: <1228587550-1287036769-cardhu_decombobulator_blackberry.rim.net-1213632773-@bda2349.bisx.produk.on.blackberry> Hi all, Does window os have a zip command for python like linux and unix. If yes, please what is it. If no, please what does it use to create a zip folder. I am working on an example in byte of python but the zip command is from linux but I use windows. Thank you. Sent from my BlackBerry wireless device from MTN From alan.gauld at btinternet.com Thu Oct 14 09:14:40 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 14 Oct 2010 08:14:40 +0100 Subject: [Tutor] Zip Command References: <1228587550-1287036769-cardhu_decombobulator_blackberry.rim.net-1213632773-@bda2349.bisx.produk.on.blackberry> Message-ID: wrote > Does window os have a zip command for python like linux and unix. > If yes, please what is it. Yes, zip > If no, please what does it use to create a zip folder. But the zip command does nmot create a "zip folder" - in fact I'm not even sure what you mean by a zip folder. I know about zip files but not zip folders... To create zip files you need to use the zipfile module. > I am working on an example in byte of python but the > zip command is from linux but I use windows. The zip commansd is a standard command and is used for joining two lists. The zipfile module is a standard module that should work across platforms. But I have no idea which you are referring to from Byte of Python, sorry. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From delegbede at dudupay.com Thu Oct 14 09:46:36 2010 From: delegbede at dudupay.com (delegbede at dudupay.com) Date: Thu, 14 Oct 2010 07:46:36 +0000 Subject: [Tutor] Zip Command In-Reply-To: References: <1228587550-1287036769-cardhu_decombobulator_blackberry.rim.net-1213632773-@bda2349.bisx.produk.on.blackberry> Message-ID: <986846810-1287042397-cardhu_decombobulator_blackberry.rim.net-498430001-@bda2349.bisx.produk.on.blackberry> Thanks to start with. A byte f python is a book by Swaroop C.H. It is also a book for beginners. Back to the module for zip, do I have to import it into my code for use? It actually works on two lists as you rightly said. I am supposed to create a back up files from 2 directories into another seperate directory on my PC and I want the backup wrapped in a zip folder. Thanks. Sent from my BlackBerry wireless device from MTN -----Original Message----- From: "Alan Gauld" Sender: tutor-bounces+delegbede=dudupay.com at python.org Date: Thu, 14 Oct 2010 08:14:40 To: Subject: Re: [Tutor] Zip Command wrote > Does window os have a zip command for python like linux and unix. > If yes, please what is it. Yes, zip > If no, please what does it use to create a zip folder. But the zip command does nmot create a "zip folder" - in fact I'm not even sure what you mean by a zip folder. I know about zip files but not zip folders... To create zip files you need to use the zipfile module. > I am working on an example in byte of python but the > zip command is from linux but I use windows. The zip commansd is a standard command and is used for joining two lists. The zipfile module is a standard module that should work across platforms. But I have no idea which you are referring to from Byte of Python, sorry. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From hanlie.pretorius at gmail.com Thu Oct 14 10:18:05 2010 From: hanlie.pretorius at gmail.com (Hanlie Pretorius) Date: Thu, 14 Oct 2010 10:18:05 +0200 Subject: [Tutor] Writing elements of an array to a file using CSV module Message-ID: Hi, I have a numpy array ('data') that is the result of reading a netCDF file, and it typically looks like this: array([ 0., 0., 0., 0.], dtype=float32) I want to write this, after a date and time, to a CSV file, so the CSV file would have the entry: 2000-02-01,09:00,0.0,0.0,0.0,0.0 The code I'm using to test it, is: # file_details[0] is the date and file_details[1] is the time writer.writerow(('%s' % (file_details[0]),'%s' % (file_details[1]),'%f' % (data[0]))) In my opinion, this should give an output line: 2000-02-01,09:00,0.0 Instead, I get an error TypeError: float argument required, not numpy.ndarray Can someone please explain to me why data[0] is giving a numpy array rather than one element? I don't understand it, since I get: >>> data[0] 0.0 Thanks Hanlie From wprins at gmail.com Thu Oct 14 10:29:30 2010 From: wprins at gmail.com (Walter Prins) Date: Thu, 14 Oct 2010 09:29:30 +0100 Subject: [Tutor] Zip Command In-Reply-To: <1228587550-1287036769-cardhu_decombobulator_blackberry.rim.net-1213632773-@bda2349.bisx.produk.on.blackberry> References: <1228587550-1287036769-cardhu_decombobulator_blackberry.rim.net-1213632773-@bda2349.bisx.produk.on.blackberry> Message-ID: On 14 October 2010 07:12, wrote: > Does window os have a zip command for python like linux and unix. > If yes, please what is it. > If no, please what does it use to create a zip folder. > I am working on an example in byte of python but the zip command is from > linux but I use windows. > Thank you. > Windows does not have a command line wrapper for it's zipped folders functionality (which as you probably know should more properly be called zip files as that's what they are, notwithstanding the fact that Windows Explorer makes them look like folders in the UI.) Several third party command line versions exist however, including proprietary ones like Winzip and open source ones. (Google yielded for example this: http://www.pcreview.co.uk/forums/thread-103518.php ) However, you should perhaps have a look at Python's "zipfile" module which should be able to create zip files as well. See e.g. http://docs.python.org/release/2.5.2/lib/module-zipfile.html Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From evert.rol at gmail.com Thu Oct 14 10:28:19 2010 From: evert.rol at gmail.com (Evert Rol) Date: Thu, 14 Oct 2010 10:28:19 +0200 Subject: [Tutor] Writing elements of an array to a file using CSV module In-Reply-To: References: Message-ID: <67273B9F-6FA4-418F-AE98-22AC2F23602C@gmail.com> > I have a numpy array ('data') that is the result of reading a netCDF > file, and it typically looks like this: > > array([ 0., 0., 0., 0.], dtype=float32) > > > I want to write this, after a date and time, to a CSV file, so the CSV > file would have the entry: > > 2000-02-01,09:00,0.0,0.0,0.0,0.0 > > > The code I'm using to test it, is: > > # file_details[0] is the date and file_details[1] is the time > writer.writerow(('%s' % (file_details[0]),'%s' % > (file_details[1]),'%f' % (data[0]))) > > > In my opinion, this should give an output line: > > 2000-02-01,09:00,0.0 > > > Instead, I get an error > > TypeError: float argument required, not numpy.ndarray > > > > Can someone please explain to me why data[0] is giving a numpy array > rather than one element? I don't understand it, since I get: > >>>> data[0] > 0.0 This means very little, since a string '0.0' will show the same output when printed: >>> print '0.0' 0.0 The exception says 'TypeError', so try: >>> type(data[0]) Which is indeed what you specified at the start using dtype=float32. The standard string formatting doesn't know what to do with a numpy.float32, so you'll need to convert it to a float: >>> '%f' % float(data[0]) '0.000000' (use proper float formatting to get at '0.0', which is what you apparently want.) Evert From delegbede at dudupay.com Thu Oct 14 10:35:51 2010 From: delegbede at dudupay.com (delegbede at dudupay.com) Date: Thu, 14 Oct 2010 08:35:51 +0000 Subject: [Tutor] Zip Command In-Reply-To: References: <1228587550-1287036769-cardhu_decombobulator_blackberry.rim.net-1213632773-@bda2349.bisx.produk.on.blackberry> Message-ID: <383069869-1287045352-cardhu_decombobulator_blackberry.rim.net-435533859-@bda2349.bisx.produk.on.blackberry> I have the Winzip so I can actually view zip folders. What I want is for my backups to be wrapped in such folder. Thanks Walter Sent from my BlackBerry wireless device from MTN -----Original Message----- From: Walter Prins Date: Thu, 14 Oct 2010 09:29:30 To: Cc: tutor Subject: Re: [Tutor] Zip Command On 14 October 2010 07:12, wrote: > Does window os have a zip command for python like linux and unix. > If yes, please what is it. > If no, please what does it use to create a zip folder. > I am working on an example in byte of python but the zip command is from > linux but I use windows. > Thank you. > Windows does not have a command line wrapper for it's zipped folders functionality (which as you probably know should more properly be called zip files as that's what they are, notwithstanding the fact that Windows Explorer makes them look like folders in the UI.) Several third party command line versions exist however, including proprietary ones like Winzip and open source ones. (Google yielded for example this: http://www.pcreview.co.uk/forums/thread-103518.php ) However, you should perhaps have a look at Python's "zipfile" module which should be able to create zip files as well. See e.g. http://docs.python.org/release/2.5.2/lib/module-zipfile.html Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Oct 14 10:51:16 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 14 Oct 2010 09:51:16 +0100 Subject: [Tutor] Zip Command References: <1228587550-1287036769-cardhu_decombobulator_blackberry.rim.net-1213632773-@bda2349.bisx.produk.on.blackberry> <986846810-1287042397-cardhu_decombobulator_blackberry.rim.net-498430001-@bda2349.bisx.produk.on.blackberry> Message-ID: wrote > Back to the module for zip, do I have to import it into my code for > use? > It actually works on two lists as you rightly said. zip is a command so does not need to be imported, but it does not do what you want. You need the zipfile module which does need to be imported. > I am supposed to create a back up files from 2 directories > into another seperate directory on my PC and I want the > backup wrapped in a zip folder. What you are calling a zip folder is in fact a zip file, it just so happens that Windows displays it as a folder in explorer. So if you use the zipfile module you can create a zip file from the contents of your folders. But using zipfile will require you to read the documentation carefully. OK I found the Byte of Python pages you are reading. This uses the ziip command from the OS not the Python zip command so it's a different issue. There are numerous zip utilities you can use on Windows but you will need to download them and install them. Some options include winzip - shareware gzip - aka gunzip GNU opensource version - probably your best bet. You will need to change the commandline from the one in the book to match the tool you choose. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu Oct 14 10:57:57 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 14 Oct 2010 09:57:57 +0100 Subject: [Tutor] Writing elements of an array to a file using CSV module References: Message-ID: "Hanlie Pretorius" wrote > # file_details[0] is the date and file_details[1] is the time > writer.writerow(('%s' % (file_details[0]),'%s' % > (file_details[1]),'%f' % (data[0]))) > > Instead, I get an error > TypeError: float argument required, not numpy.ndarray > Try using %s instead of %f just to see what gets printed (%s should print just about anything you throw at it...) Also you don;t really need string formatting here it would probably be clearer to just do: writer.writerow( ( file_details[0], file_details[1], str(data[0]) ) ) > Can someone please explain to me why data[0] is giving a numpy array > rather than one element? I don't understand it, since I get: > >>>> data[0] > 0.0 No idea and since you haven't shown us where data comes from it would be pure speculation. Thats why I'm suggesting using str() or %s to see what Python thinks data[0] really is... HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From wprins at gmail.com Thu Oct 14 11:04:13 2010 From: wprins at gmail.com (Walter Prins) Date: Thu, 14 Oct 2010 10:04:13 +0100 Subject: [Tutor] Zip Command In-Reply-To: References: <1228587550-1287036769-cardhu_decombobulator_blackberry.rim.net-1213632773-@bda2349.bisx.produk.on.blackberry> <986846810-1287042397-cardhu_decombobulator_blackberry.rim.net-498430001-@bda2349.bisx.produk.on.blackberry> Message-ID: Alan, On 14 October 2010 09:51, Alan Gauld wrote: > gzip - aka gunzip GNU opensource version - probably your best bet. > This is not correct. Gzip zip's format is not the same as the pkzip related format used by Winzip and other "zip" utilities. Gzip also only compresses, it does not deal with multiple files. Probably the best known open source implemention of the pk related zip format archivers is Info zip's "zip" and "unzip" packages, available on most platforms. Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From hanlie.pretorius at gmail.com Thu Oct 14 13:02:22 2010 From: hanlie.pretorius at gmail.com (Hanlie Pretorius) Date: Thu, 14 Oct 2010 13:02:22 +0200 Subject: [Tutor] Writing elements of an array to a file using CSV module In-Reply-To: <67273B9F-6FA4-418F-AE98-22AC2F23602C@gmail.com> References: <67273B9F-6FA4-418F-AE98-22AC2F23602C@gmail.com> Message-ID: Hi Evert, Thanks for the reply. I've tried your suggestion and am still getting an error (posted below code). Since my posting, I've also added the code to convert the numpy array to a list. My complete script is: ----- # -*- coding: utf-8 -*- import pupynere import os import csv import glob def get_file_details(filename): sub1='3B42.' sub2='.nc' extract=filename.split(sub1)[-1].split(sub2)[0] elements=extract.split('.') ure={'0':'00:00:00', '3':'03:00:00', '6':'06:00:00', '9':'09:00:00', '12':'12:00:00', '15':'15:00:00', '18':'18:00:00', '21':'21:00:00'} date='20'+elements[0] time=ure[elements[1]] return[date,time] def get_rain_data(filename): # trmm blocks covering study area rows=[87,86,85,86] cols=[833,833,833,834] # open file file = pupynere.netcdf_file(filename,'r') # read data precipitation=file.variables['precipitation'] # close file file.close() # read values for blocks of interest precip=precipitation[rows,cols] # convert numpy array to list precip=precip.tolist() return[precip] # get list of .nc files in folder os.chdir('F:\\Hanlie\\UCT\\M.Sc\\Data\\TRMM\\2000\\02_Februarie') filepattern='*.nc' nc_files=glob.glob(filepattern) # write to CSV-file with spaces as delimiter csv.register_dialect('spaces', delimiter=' ') outfilename="trmm_c83a_feb2000.txt" out_text=open(outfilename, 'wb') writer = csv.writer(out_text,dialect='spaces') for file in nc_files: file_details=get_file_details(file) data=get_rain_data(file) writer.writerow(('%s' % (file_details[0]),'%s' % (file_details[1]),'%f' % float(data[0]))) out_text.close() ----- I now get the error: ----- Traceback (most recent call last): File "F:\Hanlie\UCT\M.Sc\Data\TRMM\lees_netcdf_per_trmm_blok_skryf_vir_pcraster.py", line 76, in writer.writerow(('%s' % (file_details[0]),'%s' % (file_details[1]),'%f' % float(data[0]))) TypeError: float() argument must be a string or a number ----- I tried the other suggestion on the list (posted by Alan Gould), namely to replace the %f formatting string with %s in the write.writerow statement: ----- writer.writerow( ( file_details[0], file_details[1], str(data[0]) ) ) ----- This produces a file with lines such as: ----- 20000201 00:00:00 "[0.0, 0.0, 0.0, 0.0]" ----- I seem to have a list within a list, I'm not sure why. I could write the list elements to variables before I write to the file, but it doesn't seem like a very elegant solution. Can anyone see why I have such a nested data structure? Or is the problem something completely different? Thanks Hanlie 2010/10/14, Evert Rol : >> I have a numpy array ('data') that is the result of reading a netCDF >> file, and it typically looks like this: >> >> array([ 0., 0., 0., 0.], dtype=float32) >> >> >> I want to write this, after a date and time, to a CSV file, so the CSV >> file would have the entry: >> >> 2000-02-01,09:00,0.0,0.0,0.0,0.0 >> >> >> The code I'm using to test it, is: >> >> # file_details[0] is the date and file_details[1] is the time >> writer.writerow(('%s' % (file_details[0]),'%s' % >> (file_details[1]),'%f' % (data[0]))) >> >> >> In my opinion, this should give an output line: >> >> 2000-02-01,09:00,0.0 >> >> >> Instead, I get an error >> >> TypeError: float argument required, not numpy.ndarray >> >> >> >> Can someone please explain to me why data[0] is giving a numpy array >> rather than one element? I don't understand it, since I get: >> >>>>> data[0] >> 0.0 > > This means very little, since a string '0.0' will show the same output when > printed: >>>> print '0.0' > 0.0 > > The exception says 'TypeError', so try: > >>>> type(data[0]) > > > Which is indeed what you specified at the start using dtype=float32. > > The standard string formatting doesn't know what to do with a numpy.float32, > so you'll need to convert it to a float: > >>>> '%f' % float(data[0]) > '0.000000' > > (use proper float formatting to get at '0.0', which is what you apparently > want.) > > > Evert > > From evert.rol at gmail.com Thu Oct 14 13:08:44 2010 From: evert.rol at gmail.com (Evert Rol) Date: Thu, 14 Oct 2010 13:08:44 +0200 Subject: [Tutor] Writing elements of an array to a file using CSV module In-Reply-To: References: <67273B9F-6FA4-418F-AE98-22AC2F23602C@gmail.com> Message-ID: <36AA2AE3-AAE1-4208-B867-000A2067D010@gmail.com> > Thanks for the reply. I've tried your suggestion and am still getting > an error (posted below code). Since my posting, I've also added the > code to convert the numpy array to a list. My complete script is: > > ----- > # -*- coding: utf-8 -*- > > import pupynere > import os > import csv > import glob > > def get_file_details(filename): > sub1='3B42.' > sub2='.nc' > extract=filename.split(sub1)[-1].split(sub2)[0] > elements=extract.split('.') > ure={'0':'00:00:00', > '3':'03:00:00', > '6':'06:00:00', > '9':'09:00:00', > '12':'12:00:00', > '15':'15:00:00', > '18':'18:00:00', > '21':'21:00:00'} > date='20'+elements[0] > time=ure[elements[1]] > return[date,time] > > def get_rain_data(filename): > # trmm blocks covering study area > rows=[87,86,85,86] > cols=[833,833,833,834] > > # open file > file = pupynere.netcdf_file(filename,'r') > # read data > precipitation=file.variables['precipitation'] > # close file file.close() > > # read values for blocks of interest > precip=precipitation[rows,cols] > # convert numpy array to list > precip=precip.tolist() > return[precip] You just converted precip to a list (precip.tolist()), then, in the next line, you're turning that into a list. So you get a list within a list, which results in the errors below. Exactly as you hinted at yourself below. (In fact, if you now would do >>> data[0] [0.0, 0.0, 0.0, 0.0] because this is the representation (repr). Not the 0.0 you previously showed.) Once you get rid of the extra list, you can use either float(data[0]) and %f (if you require detailed formatting) or just simply %s, as Alan suggested. Evert > > > # get list of .nc files in folder > os.chdir('F:\\Hanlie\\UCT\\M.Sc\\Data\\TRMM\\2000\\02_Februarie') > filepattern='*.nc' > nc_files=glob.glob(filepattern) > > # write to CSV-file with spaces as delimiter > csv.register_dialect('spaces', delimiter=' ') > outfilename="trmm_c83a_feb2000.txt" > out_text=open(outfilename, 'wb') > writer = csv.writer(out_text,dialect='spaces') > > for file in nc_files: > file_details=get_file_details(file) > data=get_rain_data(file) > writer.writerow(('%s' % (file_details[0]),'%s' % > (file_details[1]),'%f' % float(data[0]))) > > out_text.close() > ----- > > I now get the error: > ----- > Traceback (most recent call last): > File "F:\Hanlie\UCT\M.Sc\Data\TRMM\lees_netcdf_per_trmm_blok_skryf_vir_pcraster.py", > line 76, in > writer.writerow(('%s' % (file_details[0]),'%s' % > (file_details[1]),'%f' % float(data[0]))) > TypeError: float() argument must be a string or a number > ----- > > I tried the other suggestion on the list (posted by Alan Gould), > namely to replace the %f formatting string with %s in the > write.writerow statement: > ----- > writer.writerow( ( file_details[0], file_details[1], str(data[0]) ) ) > ----- > > This produces a file with lines such as: > ----- > 20000201 00:00:00 "[0.0, 0.0, 0.0, 0.0]" > ----- > > I seem to have a list within a list, I'm not sure why. I could write > the list elements to variables before I write to the file, but it > doesn't seem like a very elegant solution. > > Can anyone see why I have such a nested data structure? Or is the > problem something completely different? > > Thanks > Hanlie > > > 2010/10/14, Evert Rol : >>> I have a numpy array ('data') that is the result of reading a netCDF >>> file, and it typically looks like this: >>> >>> array([ 0., 0., 0., 0.], dtype=float32) >>> >>> >>> I want to write this, after a date and time, to a CSV file, so the CSV >>> file would have the entry: >>> >>> 2000-02-01,09:00,0.0,0.0,0.0,0.0 >>> >>> >>> The code I'm using to test it, is: >>> >>> # file_details[0] is the date and file_details[1] is the time >>> writer.writerow(('%s' % (file_details[0]),'%s' % >>> (file_details[1]),'%f' % (data[0]))) >>> >>> >>> In my opinion, this should give an output line: >>> >>> 2000-02-01,09:00,0.0 >>> >>> >>> Instead, I get an error >>> >>> TypeError: float argument required, not numpy.ndarray >>> >>> >>> >>> Can someone please explain to me why data[0] is giving a numpy array >>> rather than one element? I don't understand it, since I get: >>> >>>>>> data[0] >>> 0.0 >> >> This means very little, since a string '0.0' will show the same output when >> printed: >>>>> print '0.0' >> 0.0 >> >> The exception says 'TypeError', so try: >> >>>>> type(data[0]) >> >> >> Which is indeed what you specified at the start using dtype=float32. >> >> The standard string formatting doesn't know what to do with a numpy.float32, >> so you'll need to convert it to a float: >> >>>>> '%f' % float(data[0]) >> '0.000000' >> >> (use proper float formatting to get at '0.0', which is what you apparently >> want.) >> >> >> Evert >> >> From hanlie.pretorius at gmail.com Thu Oct 14 13:22:46 2010 From: hanlie.pretorius at gmail.com (Hanlie Pretorius) Date: Thu, 14 Oct 2010 13:22:46 +0200 Subject: [Tutor] Writing elements of an array to a file using CSV module In-Reply-To: <36AA2AE3-AAE1-4208-B867-000A2067D010@gmail.com> References: <67273B9F-6FA4-418F-AE98-22AC2F23602C@gmail.com> <36AA2AE3-AAE1-4208-B867-000A2067D010@gmail.com> Message-ID: Ok, now I see what I did. Legacy from when the get_rain_data function returned precipitation and error. I forgot to remove the brackets from the 'return [precip]' statement. It works perfectly now. Thanks for all the help. 2010/10/14, Evert Rol : >> Thanks for the reply. I've tried your suggestion and am still getting >> an error (posted below code). Since my posting, I've also added the >> code to convert the numpy array to a list. My complete script is: >> >> ----- >> # -*- coding: utf-8 -*- >> >> import pupynere >> import os >> import csv >> import glob >> >> def get_file_details(filename): >> sub1='3B42.' >> sub2='.nc' >> extract=filename.split(sub1)[-1].split(sub2)[0] >> elements=extract.split('.') >> ure={'0':'00:00:00', >> '3':'03:00:00', >> '6':'06:00:00', >> '9':'09:00:00', >> '12':'12:00:00', >> '15':'15:00:00', >> '18':'18:00:00', >> '21':'21:00:00'} >> date='20'+elements[0] >> time=ure[elements[1]] >> return[date,time] >> >> def get_rain_data(filename): >> # trmm blocks covering study area >> rows=[87,86,85,86] >> cols=[833,833,833,834] >> >> # open file >> file = pupynere.netcdf_file(filename,'r') >> # read data >> precipitation=file.variables['precipitation'] >> # close file file.close() >> >> # read values for blocks of interest >> precip=precipitation[rows,cols] >> # convert numpy array to list >> precip=precip.tolist() >> return[precip] > > You just converted precip to a list (precip.tolist()), then, in the next > line, you're turning that into a list. > So you get a list within a list, which results in the errors below. Exactly > as you hinted at yourself below. > > (In fact, if you now would do >>>> data[0] > [0.0, 0.0, 0.0, 0.0] > because this is the representation (repr). Not the 0.0 you previously > showed.) > > Once you get rid of the extra list, you can use either float(data[0]) and %f > (if you require detailed formatting) or just simply %s, as Alan suggested. > > Evert > > > >> >> >> # get list of .nc files in folder >> os.chdir('F:\\Hanlie\\UCT\\M.Sc\\Data\\TRMM\\2000\\02_Februarie') >> filepattern='*.nc' >> nc_files=glob.glob(filepattern) >> >> # write to CSV-file with spaces as delimiter >> csv.register_dialect('spaces', delimiter=' ') >> outfilename="trmm_c83a_feb2000.txt" >> out_text=open(outfilename, 'wb') >> writer = csv.writer(out_text,dialect='spaces') >> >> for file in nc_files: >> file_details=get_file_details(file) >> data=get_rain_data(file) >> writer.writerow(('%s' % (file_details[0]),'%s' % >> (file_details[1]),'%f' % float(data[0]))) >> >> out_text.close() >> ----- >> >> I now get the error: >> ----- >> Traceback (most recent call last): >> File >> "F:\Hanlie\UCT\M.Sc\Data\TRMM\lees_netcdf_per_trmm_blok_skryf_vir_pcraster.py", >> line 76, in >> writer.writerow(('%s' % (file_details[0]),'%s' % >> (file_details[1]),'%f' % float(data[0]))) >> TypeError: float() argument must be a string or a number >> ----- >> >> I tried the other suggestion on the list (posted by Alan Gould), >> namely to replace the %f formatting string with %s in the >> write.writerow statement: >> ----- >> writer.writerow( ( file_details[0], file_details[1], str(data[0]) ) ) >> ----- >> >> This produces a file with lines such as: >> ----- >> 20000201 00:00:00 "[0.0, 0.0, 0.0, 0.0]" >> ----- >> >> I seem to have a list within a list, I'm not sure why. I could write >> the list elements to variables before I write to the file, but it >> doesn't seem like a very elegant solution. >> >> Can anyone see why I have such a nested data structure? Or is the >> problem something completely different? >> >> Thanks >> Hanlie >> >> >> 2010/10/14, Evert Rol : >>>> I have a numpy array ('data') that is the result of reading a netCDF >>>> file, and it typically looks like this: >>>> >>>> array([ 0., 0., 0., 0.], dtype=float32) >>>> >>>> >>>> I want to write this, after a date and time, to a CSV file, so the CSV >>>> file would have the entry: >>>> >>>> 2000-02-01,09:00,0.0,0.0,0.0,0.0 >>>> >>>> >>>> The code I'm using to test it, is: >>>> >>>> # file_details[0] is the date and file_details[1] is the time >>>> writer.writerow(('%s' % (file_details[0]),'%s' % >>>> (file_details[1]),'%f' % (data[0]))) >>>> >>>> >>>> In my opinion, this should give an output line: >>>> >>>> 2000-02-01,09:00,0.0 >>>> >>>> >>>> Instead, I get an error >>>> >>>> TypeError: float argument required, not numpy.ndarray >>>> >>>> >>>> >>>> Can someone please explain to me why data[0] is giving a numpy array >>>> rather than one element? I don't understand it, since I get: >>>> >>>>>>> data[0] >>>> 0.0 >>> >>> This means very little, since a string '0.0' will show the same output >>> when >>> printed: >>>>>> print '0.0' >>> 0.0 >>> >>> The exception says 'TypeError', so try: >>> >>>>>> type(data[0]) >>> >>> >>> Which is indeed what you specified at the start using dtype=float32. >>> >>> The standard string formatting doesn't know what to do with a >>> numpy.float32, >>> so you'll need to convert it to a float: >>> >>>>>> '%f' % float(data[0]) >>> '0.000000' >>> >>> (use proper float formatting to get at '0.0', which is what you >>> apparently >>> want.) >>> >>> >>> Evert >>> >>> > > From susana.delgado_s at utzmg.edu.mx Thu Oct 14 15:13:43 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Thu, 14 Oct 2010 08:13:43 -0500 Subject: [Tutor] Attempt to overwrite excel cell Python In-Reply-To: References: Message-ID: Hi Walter! I already looked at the code you suggested me to accomplish my goal, I made some changes on it to match mine, your code is very neat and nice. Here are some lines I changed: - First I did the file search file_list = [] folders = None for root, folders, files in os.walk( "C:\\" ): file_list.extend(os.path.join(root,fi) for fi in files if fi.endswith(".shp")) - Then I create the excel, and asign the writing to the cells: wksht = wrkbk.add_sheet('shp') wksht.row(0).write(0,'archivo') wksht.row(0).write(1,'x_min') wksht.row(0).write(2,'y_min') wksht.row(0).write(3,'x_max') wksht.row(0).write(4,'y_max') wksht.row(0).write(5,'geometria') wksht.row(0).write(6,'num_elem') wksht.row(0).write(7,'prj') wksht.row(0).write(8,'estrutura bd') wksht.row(0).write(9,'extent') wksht.row(0).write(10,'fecha_modificacion') wksht.row(0).write(11,'maquina_host') for row, filepath in enumerate(file_list, start=1): wksht.row(row).write(0, filepath) wrkbk.save('shp.xls') SEARCH_PATH = os.getcwd() TARGET_FILE = os.path.realpath('shp.xls') print "Searching in", SEARCH_PATH, "and writing to", TARGET_FILE print "Finding files..." print "Writing Excel file..." print "Done." 2010/10/13 Susana Iraiis Delgado Rodriguez > Hi Walter! > > I already looked at the code you suggested me to accomplish my goal, I made > some changes on it to match mine, your code is very neat and nice. Here are > some lines I changed: > > - First I did the file search > > file_list = [] > folders = None > for root, folders, files in os.walk( "C:\\" ): > file_list.extend(os.path.join(root,fi) for fi in files if > fi.endswith(".shp")) > > - Then I create the excel, and asign the writing to the cells: > > wksht = wrkbk.add_sheet('shp') > wksht.row(0).write(0,'archivo') > wksht.row(0).write(1,'x_min') > wksht.row(0).write(2,'y_min') > wksht.row(0).write(3,'x_max') > wksht.row(0).write(4,'y_max') > wksht.row(0).write(5,'geometria') > wksht.row(0).write(6,'num_elem') > wksht.row(0).write(7,'prj') > wksht.row(0).write(8,'estrutura bd') > wksht.row(0).write(9,'extent') > wksht.row(0).write(10,'fecha_modificacion') > wksht.row(0).write(11,'maquina_host') > > > for row, filepath in enumerate(file_list, start=1): > wksht.row(row).write(0, filepath) > wrkbk.save('shp.xls') > > > SEARCH_PATH = os.getcwd() > TARGET_FILE = os.path.realpath('shp.xls') > print "Searching in", SEARCH_PATH, "and writing to", TARGET_FILE > print "Finding files..." > print "Writing Excel file..." > print "Done." > > 2010/10/12 Walter Prins > > Hi just a quick note -- I included my own recursive call in the code I >> posted last night which is obviously superflous as os.walk() already >> recurses by itself. So if you looked at it already you may want to look at >> it again. >> >> Walter >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rwobben at hotmail.com Thu Oct 14 15:50:29 2010 From: rwobben at hotmail.com (Roelof Wobben) Date: Thu, 14 Oct 2010 13:50:29 +0000 Subject: [Tutor] join question Message-ID: Hello, I found this answer to a problem for me : print ''.join([zf.getinfo('%s.txt' % p).comment for p in zpp]) So I thought that this would be the same : for p in zpp: test = zf.getinfo(p).comment print ''.join(test) But it seems not to work Can anyone explain why not ? Roelof From hugo.yoshi at gmail.com Thu Oct 14 16:08:50 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Thu, 14 Oct 2010 16:08:50 +0200 Subject: [Tutor] join question In-Reply-To: References: Message-ID: On Thu, Oct 14, 2010 at 3:50 PM, Roelof Wobben wrote: > > > Hello, > > I found this answer to a problem for me : > > > print ''.join([zf.getinfo('%s.txt' % p).comment for p in zpp]) > Look at the argument given to join here. It is a list comprehension. The result of a list comprehension is a list. You're calling join once, with a list as argument. > So I thought that this would be the same : > > for p in zpp: > ? ? ?test = zf.getinfo(p).comment > ? ? ?print ''.join(test) > Now look at the argument to join again. It is not a list, but a single element of a list. Also, you're calling join for every element, instead of just once. That's not the same thing, is it? > But it seems not to work > > Can anyone explain why not ? > > Roelof > From joel.goldstick at gmail.com Thu Oct 14 16:14:10 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Thu, 14 Oct 2010 10:14:10 -0400 Subject: [Tutor] join question In-Reply-To: References: Message-ID: On Thu, Oct 14, 2010 at 9:50 AM, Roelof Wobben wrote: > > > Hello, > > I found this answer to a problem for me : > > > print ''.join([zf.getinfo('%s.txt' % p).comment for p in zpp]) > > So I thought that this would be the same : > > for p in zpp: > test = zf.getinfo(p).comment > print ''.join(test) > > But it seems not to work > > Can anyone explain why not ? > the argument for getinfo() in the first case is a str(p) + ".txt" in the second case it is just p > > Roelof > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Thu Oct 14 16:16:28 2010 From: emile at fenx.com (Emile van Sebille) Date: Thu, 14 Oct 2010 07:16:28 -0700 Subject: [Tutor] join question In-Reply-To: References: Message-ID: On 10/14/2010 6:50 AM Roelof Wobben said... > > > Hello, > > I found this answer to a problem for me : > > > print ''.join([zf.getinfo('%s.txt' % p).comment for p in zpp]) > > So I thought that this would be the same : > > for p in zpp: > test = zf.getinfo(p).comment This isn't transcribed properly test = zf.getinfo('%s.txt' % p).comment > print ''.join(test) > > But it seems not to work > > Can anyone explain why not ? > > Roelof > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From smokefloat at gmail.com Thu Oct 14 16:14:54 2010 From: smokefloat at gmail.com (David Hutto) Date: Thu, 14 Oct 2010 10:14:54 -0400 Subject: [Tutor] Converting from unicode to nonstring Message-ID: Hey Buddy Pals, I receive the following output from a sqlite db (u'graph1', u'Line', u'222', u'BLUE', u'1,2,3,4', u'True', u'0,5,0,10') Which is a tuple of unicode strings. From this I need to place portions of the tuple into other fields, but not as unicode strings, but literals no ''. For example if I have the following line: self.lines = self.newplot.plot([1,2,3,4]) And I use the following to replace [1,2,3,4] with self.plot from line 13 in belows iteration: self.orderbygname = self.cur.execute('''select * from %s order by graphname''' % ('graphs')) self.orderlist = [] for name in self.orderbygname: self.orderlist.append(name) self.fig = Figure() for line in self.orderlist: print line #print line[0],' and ', line[1] self.graphname = line[0] self.graphtype = line[1] self.position = line[2] self.bgcolor = line[3] self.plot = line[4] self.grid = line[5] self.x_y_axis_range = line[6] I get the error: ValueError: invalid literal for float(): 1,2,3,4 So my question is, how do I convert the unicode '' to a plain nonstring insert? Thanks, David From bgailer at gmail.com Thu Oct 14 16:48:37 2010 From: bgailer at gmail.com (bob gailer) Date: Thu, 14 Oct 2010 10:48:37 -0400 Subject: [Tutor] join question In-Reply-To: References: Message-ID: <4CB71845.90609@gmail.com> > > Hello, > > I found this answer to a problem for me : > > > print ''.join([zf.getinfo('%s.txt' % p).comment for p in zpp]) > > So I thought that this would be the same : > > for p in zpp: > test = zf.getinfo(p).comment > print ''.join(test) > > But it seems not to work > > Can anyone explain why not ? > Probably. Please refer to my earlier comments regarding program walkthroughs. AGAIN AS WE HAVE SAID "seems not to work" is much less helpful. Tell us the expected output and the result you got. This will be my LAST attempt to help you unless you do your homework and post useful information. Else I will disappear from your life. But maybe you will appreciate that? (It took me less than a minute to spot the problem. You too can gain that skill, but it will take work.) -- Bob Gailer 919-636-4239 Chapel Hill NC From ghashsnaga at gmail.com Thu Oct 14 16:48:19 2010 From: ghashsnaga at gmail.com (Ara Kooser) Date: Thu, 14 Oct 2010 08:48:19 -0600 Subject: [Tutor] Merging Text Files Message-ID: Morning all, I took the pseudocode that Emile provided and tried to write a python program. I may have taken the pseudocode to literally. So what I wrote was this: xml = open("final.txt",'r') gen = open("final_gen.txt",'r') PIDS = {} for proteinVals in gen: ID = proteinVals.split()[0] PIDS[ID] = proteinVals print PIDS for line in xml: ID = proteinVals.split()[1] rslt = "%s,%s"% (line,PIDS[ID]) print rslt So the first part I get. I read in gen that has this format as a text file: *Protein ID, Locus Tag, Start/Stop* ZP_05482482, StAA4_010100030484, complement(NZ_ACEV01000078.1:25146..40916) ZP_07281899, SSMG_05939, complement(NZ_GG657746.1:6565974..6581756) ZP_05477599, StAA4_010100005861, NZ_ACEV01000013.1:86730..102047 ... Put that into a dictionary with a key that is the Protein ID at position 0 in the dictionary. The second part reads in the file xml which has this format: *Species, Protein ID, E Value, Length* Streptomyces sp. AA4, ZP_05482482, 2.8293600000000001e-140, 5256, Streptomyces sp. AA4, ZP_05482482, 8.0333299999999997e-138, 5256, Streptomyces sp. AA4, ZP_05482482, 1.08889e-124, 5256, Streptomyces sp. AA4, ZP_07281899, 2.9253900000000001e-140, 5260, Streptomyces sp. AA4, ZP_07281899, 8.2369599999999995e-138, 5260, .... *same protein id multiple entries The program splits the file and does something with the 1 position which is the proten id in the xml file. After that I am not really sure what is happening. I can't remember what the %s means. Something with a string? When this runs I get the following error: Traceback (most recent call last): File "/Users/ara/Desktop/biopy_programs/merge2.py", line 18, in rslt = "%s,%s"% (line,PIDS[ID]) KeyError: 'StAA4_010100017400,' >From what I can tell it's not happy about the dictionary key. In the end I am looking for a way to merge these two files and for each protein ID add the locus tag and start/stop like this: *Species, Protein ID, Locus Tag, E Value, Length*, *Start/Stop* Streptomyces sp. AA4, ZP_05482482, StAA4_010100030484, 2.8293600000000001e-140, 5256, complement(NZ_ACEV01000078.1:25146..40916) Streptomyces sp. AA4, ZP_05482482, StAA4_010100030484, 8.0333299999999997e-138, 5256, complement(NZ_ACEV01000078.1:25146..40916) Streptomyces sp. AA4, ZP_05482482, StAA4_010100030484, 1.08889e-124, 5256, complement(NZ_ACEV01000078.1:25146..40916) Streptomyces sp. AA4, ZP_07281899, SSMG_05939, 2.9253900000000001e-140, 5260, complement(NZ_GG657746.1:6565974..6581756) Streptomyces sp. AA4, ZP_07281899, SSMG_05939, 8.2369599999999995e-138, 5260, complement(NZ_GG657746.1:6565974..6581756) Do you have any suggestions for how to proceed. It feels like I am getting closer. :) Note: When I change this part of the code to 0 for line in xml: ID = proteinVals.split()[0] rslt = "%s,%s"% (line,PIDS[ID]) print rslt I get the following output: Streptomyces sp. AA4, ZP_05482482, 8.0333299999999997e-138, 5256, ,ZP_05479896, StAA4_010100017400, NZ_ACEV01000043.1:241968..>242983 Streptomyces sp. AA4, ZP_05482482, 1.08889e-124, 5256, ,ZP_05479896, StAA4_010100017400, NZ_ACEV01000043.1:241968..>242983 Streptomyces sp. AA4, ZP_07281899, 2.9253900000000001e-140, 5260, ,ZP_05479896, StAA4_010100017400, NZ_ACEV01000043.1:241968..>242983 Which seems closer but all it's doing is repeating the same Locus Tag and Start/Stop for each entry. Thank you! Ara -- Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis an sub cardine glacialis ursae. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ghashsnaga at gmail.com Thu Oct 14 16:48:37 2010 From: ghashsnaga at gmail.com (Ara Kooser) Date: Thu, 14 Oct 2010 08:48:37 -0600 Subject: [Tutor] Merging Text Files Message-ID: Morning all, I took the pseudocode that Emile provided and tried to write a python program. I may have taken the pseudocode to literally. So what I wrote was this: xml = open("final.txt",'r') gen = open("final_gen.txt",'r') PIDS = {} for proteinVals in gen: ID = proteinVals.split()[0] PIDS[ID] = proteinVals print PIDS for line in xml: ID = proteinVals.split()[1] rslt = "%s,%s"% (line,PIDS[ID]) print rslt So the first part I get. I read in gen that has this format as a text file: *Protein ID, Locus Tag, Start/Stop* ZP_05482482, StAA4_010100030484, complement(NZ_ACEV01000078.1:25146..40916) ZP_07281899, SSMG_05939, complement(NZ_GG657746.1:6565974..6581756) ZP_05477599, StAA4_010100005861, NZ_ACEV01000013.1:86730..102047 ... Put that into a dictionary with a key that is the Protein ID at position 0 in the dictionary. The second part reads in the file xml which has this format: *Species, Protein ID, E Value, Length* Streptomyces sp. AA4, ZP_05482482, 2.8293600000000001e-140, 5256, Streptomyces sp. AA4, ZP_05482482, 8.0333299999999997e-138, 5256, Streptomyces sp. AA4, ZP_05482482, 1.08889e-124, 5256, Streptomyces sp. AA4, ZP_07281899, 2.9253900000000001e-140, 5260, Streptomyces sp. AA4, ZP_07281899, 8.2369599999999995e-138, 5260, .... *same protein id multiple entries The program splits the file and does something with the 1 position which is the proten id in the xml file. After that I am not really sure what is happening. I can't remember what the %s means. Something with a string? When this runs I get the following error: Traceback (most recent call last): File "/Users/ara/Desktop/biopy_programs/merge2.py", line 18, in rslt = "%s,%s"% (line,PIDS[ID]) KeyError: 'StAA4_010100017400,' >From what I can tell it's not happy about the dictionary key. In the end I am looking for a way to merge these two files and for each protein ID add the locus tag and start/stop like this: *Species, Protein ID, Locus Tag, E Value, Length*, *Start/Stop* Streptomyces sp. AA4, ZP_05482482, StAA4_010100030484, 2.8293600000000001e-140, 5256, complement(NZ_ACEV01000078.1:25146..40916) Streptomyces sp. AA4, ZP_05482482, StAA4_010100030484, 8.0333299999999997e-138, 5256, complement(NZ_ACEV01000078.1:25146..40916) Streptomyces sp. AA4, ZP_05482482, StAA4_010100030484, 1.08889e-124, 5256, complement(NZ_ACEV01000078.1:25146..40916) Streptomyces sp. AA4, ZP_07281899, SSMG_05939, 2.9253900000000001e-140, 5260, complement(NZ_GG657746.1:6565974..6581756) Streptomyces sp. AA4, ZP_07281899, SSMG_05939, 8.2369599999999995e-138, 5260, complement(NZ_GG657746.1:6565974..6581756) Do you have any suggestions for how to proceed. It feels like I am getting closer. :) Note: When I change this part of the code to 0 for line in xml: ID = proteinVals.split()[0] rslt = "%s,%s"% (line,PIDS[ID]) print rslt I get the following output: Streptomyces sp. AA4, ZP_05482482, 8.0333299999999997e-138, 5256, ,ZP_05479896, StAA4_010100017400, NZ_ACEV01000043.1:241968..>242983 Streptomyces sp. AA4, ZP_05482482, 1.08889e-124, 5256, ,ZP_05479896, StAA4_010100017400, NZ_ACEV01000043.1:241968..>242983 Streptomyces sp. AA4, ZP_07281899, 2.9253900000000001e-140, 5260, ,ZP_05479896, StAA4_010100017400, NZ_ACEV01000043.1:241968..>242983 Which seems closer but all it's doing is repeating the same Locus Tag and Start/Stop for each entry. Thank you! Ara -- Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis an sub cardine glacialis ursae. -------------- next part -------------- An HTML attachment was scrubbed... URL: From smokefloat at gmail.com Thu Oct 14 17:16:27 2010 From: smokefloat at gmail.com (David Hutto) Date: Thu, 14 Oct 2010 11:16:27 -0400 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: Message-ID: On Thu, Oct 14, 2010 at 10:14 AM, David Hutto wrote: > Hey Buddy Pals, > > I receive the following output from a sqlite db > > (u'graph1', u'Line', u'222', u'BLUE', u'1,2,3,4', u'True', u'0,5,0,10') > > Which is a tuple of unicode strings. From this I > need to place portions of the tuple into other fields, > but not as unicode strings, but literals no ''. > > For example if I have the following line: > > self.lines = self.newplot.plot([1,2,3,4]) > > And I use the following to replace [1,2,3,4] with > self.plot from line 13 in belows iteration: > > ? ? ? ? ? ? ? ?self.orderbygname = self.cur.execute('''select * from %s order by > graphname''' % ('graphs')) > ? ? ? ? ? ? ? ?self.orderlist = [] > ? ? ? ? ? ? ? ?for name in self.orderbygname: > ? ? ? ? ? ? ? ? ? ? ? ?self.orderlist.append(name) > ? ? ? ? ? ? ? ?self.fig = Figure() > ? ? ? ? ? ? ? ?for line in self.orderlist: > ? ? ? ? ? ? ? ? ? ? ? ?print line > ? ? ? ? ? ? ? ? ? ? ? ?#print line[0],' and ', line[1] > ? ? ? ? ? ? ? ? ? ? ? ?self.graphname = line[0] > ? ? ? ? ? ? ? ? ? ? ? ?self.graphtype = line[1] > ? ? ? ? ? ? ? ? ? ? ? ?self.position = line[2] > ? ? ? ? ? ? ? ? ? ? ? ?self.bgcolor = line[3] > ? ? ? ? ? ? ? ? ? ? ? ?self.plot = line[4] > ? ? ? ? ? ? ? ? ? ? ? ?self.grid = line[5] > ? ? ? ? ? ? ? ? ? ? ? ?self.x_y_axis_range = line[6] > > I get the error: > > ValueError: invalid literal for float(): 1,2,3,4# Which is a deceptive error, because it means that the u'value here' has not been converted to value, not the string 'value', but the actual. It works if I place the same value in manually, converting it is the actual problem. > > So my question is, how do I convert the unicode '' to a plain nonstring insert? Or somehow append to the u'string' an escape character \ for the ''? > > Thanks, > David > From davidheiserca at gmail.com Thu Oct 14 17:28:49 2010 From: davidheiserca at gmail.com (davidheiserca at gmail.com) Date: Thu, 14 Oct 2010 08:28:49 -0700 Subject: [Tutor] join question References: Message-ID: <792D227B852B41548FB0EEC6DE72D92E@dheiser> "join" operates on lists. It "joins" the elements of the list using the leading character or string as the delimiter. In this case it is NUL. Try putting a character or string, like 'XX\n' in front of the ".join" in both places. It should illustrate what's really happening. "XX\n".join(...) In the original case, "join" is presented with a list. In the second case, "join" is presented with a string. But Python happily converts it to a list of characters. ----- Original Message ----- From: "Roelof Wobben" To: Sent: Thursday, October 14, 2010 6:50 AM Subject: [Tutor] join question > > > Hello, > > I found this answer to a problem for me : > > > print ''.join([zf.getinfo('%s.txt' % p).comment for p in zpp]) > > So I thought that this would be the same : > > for p in zpp: > test = zf.getinfo(p).comment > print ''.join(test) > > But it seems not to work > > Can anyone explain why not ? > > Roelof > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From emile at fenx.com Thu Oct 14 17:35:56 2010 From: emile at fenx.com (Emile van Sebille) Date: Thu, 14 Oct 2010 08:35:56 -0700 Subject: [Tutor] Merging Text Files In-Reply-To: References: Message-ID: On 10/14/2010 7:48 AM Ara Kooser said... > Morning all, > > I took the pseudocode that Emile provided and tried to write a python > program. I may have taken the pseudocode to literally. > > So what I wrote was this: > xml = open("final.txt",'r') > gen = open("final_gen.txt",'r') > > PIDS = {} > for proteinVals in gen: > ID = proteinVals.split()[0] > PIDS[ID] = proteinVals > > print PIDS > > for line in xml: > ID = proteinVals.split()[1] > rslt = "%s,%s"% (line,PIDS[ID]) > print rslt Without reading much further, what happens when you use the loop variable here instead of the prio loop variable? Change the second line to ID = line.split()[1] and see what you get. Emile From alan.gauld at btinternet.com Thu Oct 14 17:32:54 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Thu, 14 Oct 2010 16:32:54 +0100 (BST) Subject: [Tutor] Zip Command In-Reply-To: References: <1228587550-1287036769-cardhu_decombobulator_blackberry.rim.net-1213632773-@bda2349.bisx.produk.on.blackberry> <986846810-1287042397-cardhu_decombobulator_blackberry.rim.net-498430001-@bda2349.bisx.produk.on.blackberry> Message-ID: <44195.77712.qm@web86704.mail.ird.yahoo.com> > >gzip - aka gunzip GNU opensource version - probably your best bet. >> >This is not correct. Gzip zip's format is not the same as the pkzip related >format used by Winzip and other "zip" utilities. Gzip also only compresses, it >does not deal with multiple files. > > >Ah yes, I forgot gzip doesn't archive multiple files, it expects to work with >tar... I didn't know the format was different though! OK, forget gzip... :-( Alan G. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at alchemy.com Thu Oct 14 17:43:55 2010 From: steve at alchemy.com (Steve Willoughby) Date: Thu, 14 Oct 2010 08:43:55 -0700 Subject: [Tutor] Zip Command In-Reply-To: <44195.77712.qm@web86704.mail.ird.yahoo.com> References: <1228587550-1287036769-cardhu_decombobulator_blackberry.rim.net-1213632773-@bda2349.bisx.produk.on.blackberry> <986846810-1287042397-cardhu_decombobulator_blackberry.rim.net-498430001-@bda2349.bisx.produk.on.blackberry> <44195.77712.qm@web86704.mail.ird.yahoo.com> Message-ID: <4CB7253B.9010103@alchemy.com> On 14-Oct-10 08:32, ALAN GAULD wrote: > > > gzip - aka gunzip GNU opensource version - probably your best bet. > > > This is not correct. Gzip zip's format is not the same as the pkzip > related format used by Winzip and other "zip" utilities. Gzip also > only compresses, it does not deal with multiple files. > > Ah yes, I forgot gzip doesn't archive multiple files, it expects to work > with tar... > I didn't know the format was different though! > > OK, forget gzip... :-( There is a unix "zip" program which is compatible with the various *zip programs in Windows (WinZip, etc). In fact, the popular Windows *zip programs these days (WinZip being one I know of) will read gzipped tar archive files too, so you can either use the Unix version of "zip" or "unzip" or you can use the Python modules to write compressed tar files and have windows programs open them. From evert.rol at gmail.com Thu Oct 14 18:49:20 2010 From: evert.rol at gmail.com (Evert Rol) Date: Thu, 14 Oct 2010 18:49:20 +0200 Subject: [Tutor] Networking In-Reply-To: <4CB3A1A7.9080804@gmail.com> References: <4CA788E2.4060006@gmail.com> <33D037A9-0230-42B3-9AFC-A1884F00AE88@gmail.com> <4CA90FAF.1070002@gmail.com> <4CB3A1A7.9080804@gmail.com> Message-ID: Hi Chris, Bit hard to comment on the actual code, as it was in attachments, but the important bit is here: class Handler(SocketServer.BaseRequestHandler): #the handler '''A handler which calls %s in the handle method.'''%handle_func def handle(self): #the handle method self.data = self.request.recv(1024) #the data self.client = self.request #the client handle_func(self) #call the handle method giving self (I did a bit of reformatting, since I had to try the code myself, because I didn't spot the error immediately.) In essence, your server accepts the client connection, then proceeds to the handle function, where it performs the necessary actions. It will *not* call the handle function for each set of data it receives, it just stays there. So you'll need a while loop in your handle function that keeps on processing the client data, until there is not client data anymore (EOF or similar), then return. Evert >>>> Dear Tutors, >>>> I have attached my 2 programs for networking. It uses socket and SocketServer, but it just simplifies it even more. The problem is it won't work. The Client raises the error, (with trace back) >>>> Traceback (most recent call last): >>>> File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 34, in >>>> print client.recv() >>>> File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 16, in recv >>>> return self.__sock.recv(1024) >>>> error: [Errno 10053] An established connection was aborted by the software in your host machine >>>> The server seems to get an error of some sort, but also seems to catch and print it. It prints >>>> ---------------------------------------- >>>> Exception happened during processing of request from ('127.0.0.1', 1424) >>>> Traceback (most recent call last): >>>> File "C:\Python26\lib\SocketServer.py", line 281, in _handle_request_noblock >>>> self.process_request(request, client_address) >>>> File "C:\Python26\lib\SocketServer.py", line 307, in process_request >>>> self.finish_request(request, client_address) >>>> File "C:\Python26\lib\SocketServer.py", line 320, in finish_request >>>> self.RequestHandlerClass(request, client_address, self) >>>> TypeError: 'NoneType' object is not callable >>>> ---------------------------------------- >>>> I look at both of the documentations of socket and SocketServer, but I couldn't firgue it out. I don't know much about networking. Please Help >>> I don't know much about networking, less so about it on Windows; also, I've only scanned quickly through the server script, but I notice your create_handler() function doesn't return anything. I guess it needs to return the Handler class. >>> The example in the Python docs doesn't use a factory function, but instead directly puts the class as the second argument to TCPServer. >>> So the second argument needs to be a class, but since your create_handler() function returns nothing, you presumably get this NoneType exception. >>> >>> Evert >>> >> Yeah, that could be a problem. It should return the class. > Now it won't send more than once. The Error the clients raises is: > Traceback (most recent call last): > File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 34, in > print client.recv() > File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 16, in recv > return self.__sock.recv(1024) > error: [Errno 10053] An established connection was aborted by the software in your host machine > The server is blissfully unaware of its crashing clients, and keeps on going to help more clients to there dooms. The client can receive data multiple times, but just can send twice. Please help. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From smokefloat at gmail.com Thu Oct 14 19:43:46 2010 From: smokefloat at gmail.com (David Hutto) Date: Thu, 14 Oct 2010 13:43:46 -0400 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: Message-ID: On Thu, Oct 14, 2010 at 11:16 AM, David Hutto wrote: > On Thu, Oct 14, 2010 at 10:14 AM, David Hutto wrote: >> Hey Buddy Pals, >> >> I receive the following output from a sqlite db >> >> (u'graph1', u'Line', u'222', u'BLUE', u'1,2,3,4', u'True', u'0,5,0,10') >> >> Which is a tuple of unicode strings. From this I >> need to place portions of the tuple into other fields, >> but not as unicode strings, but literals no ''. >> >> For example if I have the following line: >> >> self.lines = self.newplot.plot([1,2,3,4]) >> >> And I use the following to replace [1,2,3,4] with >> self.plot from line 13 in belows iteration: >> >> ? ? ? ? ? ? ? ?self.orderbygname = self.cur.execute('''select * from %s order by >> graphname''' % ('graphs')) >> ? ? ? ? ? ? ? ?self.orderlist = [] >> ? ? ? ? ? ? ? ?for name in self.orderbygname: >> ? ? ? ? ? ? ? ? ? ? ? ?self.orderlist.append(name) >> ? ? ? ? ? ? ? ?self.fig = Figure() >> ? ? ? ? ? ? ? ?for line in self.orderlist: >> ? ? ? ? ? ? ? ? ? ? ? ?print line >> ? ? ? ? ? ? ? ? ? ? ? ?#print line[0],' and ', line[1] >> ? ? ? ? ? ? ? ? ? ? ? ?self.graphname = line[0] >> ? ? ? ? ? ? ? ? ? ? ? ?self.graphtype = line[1] >> ? ? ? ? ? ? ? ? ? ? ? ?self.position = line[2] >> ? ? ? ? ? ? ? ? ? ? ? ?self.bgcolor = line[3] >> ? ? ? ? ? ? ? ? ? ? ? ?self.plot = line[4] >> ? ? ? ? ? ? ? ? ? ? ? ?self.grid = line[5] >> ? ? ? ? ? ? ? ? ? ? ? ?self.x_y_axis_range = line[6] >> Fixed with: self.lines = self.newplot.plot(eval(self.plot)) From sander.sweers at gmail.com Thu Oct 14 20:19:57 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Thu, 14 Oct 2010 20:19:57 +0200 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: Message-ID: On 14 October 2010 16:14, David Hutto wrote: > (u'graph1', u'Line', u'222', u'BLUE', u'1,2,3,4', u'True', u'0,5,0,10') > > Which is a tuple of unicode strings. From this I > need to place portions of the tuple into other fields, > but not as unicode strings, but literals no ''. > > For example if I have the following line: > > self.lines = self.newplot.plot([1,2,3,4]) So you want convert string u'1,2,3,4' to a list of ints [1,2,3,4]? Then the below will work. [int(n) for n in u'1,2,3,4'.replace(',', '')] Greets Sander From smokefloat at gmail.com Thu Oct 14 20:29:58 2010 From: smokefloat at gmail.com (David Hutto) Date: Thu, 14 Oct 2010 14:29:58 -0400 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: Message-ID: On Thu, Oct 14, 2010 at 2:19 PM, Sander Sweers wrote: > On 14 October 2010 16:14, David Hutto wrote: >> (u'graph1', u'Line', u'222', u'BLUE', u'1,2,3,4', u'True', u'0,5,0,10') >> >> Which is a tuple of unicode strings. From this I >> need to place portions of the tuple into other fields, >> but not as unicode strings, but literals no ''. >> >> For example if I have the following line: >> >> self.lines = self.newplot.plot([1,2,3,4]) > > So you want convert string u'1,2,3,4' to a list of ints [1,2,3,4]? > Then the below will work. > > [int(n) for n in u'1,2,3,4'.replace(',', '')] > Actually, I needed it to be converted to something without a string attached to it. See a post above, and it was fixed by eval(), Thanks though. And I'm sure at some point this morning in a moment of frustration rather than logic, I tried your approach. From ademlookes at gmail.com Thu Oct 14 20:40:31 2010 From: ademlookes at gmail.com (Adam Lucas) Date: Thu, 14 Oct 2010 13:40:31 -0500 Subject: [Tutor] Merging Text Files In-Reply-To: References: Message-ID: Either way; nest the for loops and index with protein IDs or dictionary one file and write the other with matches to the dictionary: non-python pseudocode: for every line in TWO: get the first protein ID for every line in ONE: if the second protein ID is the same as the first: perform the string merging and write it to the file else: pass to the next protein ID in ONE --OR-- for every line in ONE: make a dictionary with a key = to the protein ID and the value, the rest for every line in TWO: if the dictionary has the same protein ID: perform the string merging and write to the file I'm inferring an 'inner join' (drop non-matches), for an 'outer/right join' (keep everything in TWO) initialize a 'matchmade' variable in the inner loop and if no matches are made, write the protein to the merged file with null values. If you plan on querying or sharing the newly organized dataset use a database. If this file is going to into a workflow, it probably wants text. I'd probably do both. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ademlookes at gmail.com Thu Oct 14 20:43:42 2010 From: ademlookes at gmail.com (Adam Lucas) Date: Thu, 14 Oct 2010 13:43:42 -0500 Subject: [Tutor] Merging Text Files In-Reply-To: References: Message-ID: Whoops: 1) dictionary.has_key() ??? 2) I don't know if it's a typo or oversight, but there's a comma in you dictionary key, line.split(',')[0]. 3) Forget the database if it's part of a larger workflow unless your job is to adapt a biological workflow database for your lab. On Thu, Oct 14, 2010 at 09:48, Ara Kooser wrote: > Morning all, > > I took the pseudocode that Emile provided and tried to write a python > program. I may have taken the pseudocode to literally. > > So what I wrote was this: > xml = open("final.txt",'r') > gen = open("final_gen.txt",'r') > > PIDS = {} > for proteinVals in gen: > > ID = proteinVals.split()[0] > PIDS[ID] = proteinVals > > print PIDS > > for line in xml: > ID = proteinVals.split()[1] > rslt = "%s,%s"% (line,PIDS[ID]) > print rslt > > So the first part I get. I read in gen that has this format as a text file: > > *Protein ID, Locus Tag, Start/Stop* > ZP_05482482, StAA4_010100030484, complement(NZ_ACEV01000078.1:25146..40916) > ZP_07281899, SSMG_05939, complement(NZ_GG657746.1:6565974..6581756) > ZP_05477599, StAA4_010100005861, NZ_ACEV01000013.1:86730..102047 > ... > Put that into a dictionary with a key that is the Protein ID at position 0 > in the dictionary. > > The second part reads in the file xml which has this format: > > *Species, Protein ID, E Value, Length* > Streptomyces sp. AA4, ZP_05482482, 2.8293600000000001e-140, 5256, > Streptomyces sp. AA4, ZP_05482482, 8.0333299999999997e-138, 5256, > Streptomyces sp. AA4, ZP_05482482, 1.08889e-124, 5256, > Streptomyces sp. AA4, ZP_07281899, 2.9253900000000001e-140, 5260, > Streptomyces sp. AA4, ZP_07281899, 8.2369599999999995e-138, 5260, > .... > *same protein id multiple entries > > The program splits the file and does something with the 1 position which is > the proten id in the xml file. After that I am not really sure what is > happening. I can't remember what the %s means. Something with a string? > > When this runs I get the following error: > Traceback (most recent call last): > File "/Users/ara/Desktop/biopy_programs/merge2.py", line 18, in > rslt = "%s,%s"% (line,PIDS[ID]) > KeyError: 'StAA4_010100017400,' > > From what I can tell it's not happy about the dictionary key. > > In the end I am looking for a way to merge these two files and for each > protein ID add the locus tag and start/stop like this: > *Species, Protein ID, Locus Tag, E Value, Length*, *Start/Stop* > > Streptomyces sp. AA4, ZP_05482482, StAA4_010100030484, > 2.8293600000000001e-140, 5256, complement(NZ_ACEV01000078.1:25146..40916) > Streptomyces sp. AA4, ZP_05482482, StAA4_010100030484, > 8.0333299999999997e-138, 5256, complement(NZ_ACEV01000078.1:25146..40916) > Streptomyces sp. AA4, ZP_05482482, StAA4_010100030484, 1.08889e-124, 5256, > complement(NZ_ACEV01000078.1:25146..40916) > Streptomyces sp. AA4, ZP_07281899, SSMG_05939, 2.9253900000000001e-140, > 5260, complement(NZ_GG657746.1:6565974..6581756) > Streptomyces sp. AA4, ZP_07281899, SSMG_05939, 8.2369599999999995e-138, > 5260, complement(NZ_GG657746.1:6565974..6581756) > > Do you have any suggestions for how to proceed. It feels like I am getting > closer. :) > > > Note: > When I change this part of the code to 0 > for line in xml: > ID = proteinVals.split()[0] > rslt = "%s,%s"% (line,PIDS[ID]) > print rslt > > I get the following output: > Streptomyces sp. AA4, ZP_05482482, 8.0333299999999997e-138, 5256, > ,ZP_05479896, StAA4_010100017400, NZ_ACEV01000043.1:241968..>242983 > > > Streptomyces sp. AA4, ZP_05482482, 1.08889e-124, 5256, > ,ZP_05479896, StAA4_010100017400, NZ_ACEV01000043.1:241968..>242983 > > > Streptomyces sp. AA4, ZP_07281899, 2.9253900000000001e-140, 5260, > ,ZP_05479896, StAA4_010100017400, NZ_ACEV01000043.1:241968..>242983 > > Which seems closer but all it's doing is repeating the same Locus Tag and > Start/Stop for each entry. > > Thank you! > > Ara > > > -- > Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis an > sub cardine glacialis ursae. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom. --Clifford Stoll -------------- next part -------------- An HTML attachment was scrubbed... URL: From adam.jtm30 at gmail.com Thu Oct 14 20:58:56 2010 From: adam.jtm30 at gmail.com (Adam Bark) Date: Thu, 14 Oct 2010 19:58:56 +0100 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: Message-ID: <4CB752F0.6050008@gmail.com> On 14/10/10 19:29, David Hutto wrote: > On Thu, Oct 14, 2010 at 2:19 PM, Sander Sweers wrote: > >> On 14 October 2010 16:14, David Hutto wrote: >> >>> (u'graph1', u'Line', u'222', u'BLUE', u'1,2,3,4', u'True', u'0,5,0,10') >>> >>> Which is a tuple of unicode strings. From this I >>> need to place portions of the tuple into other fields, >>> but not as unicode strings, but literals no ''. >>> >>> For example if I have the following line: >>> >>> self.lines = self.newplot.plot([1,2,3,4]) >>> >> So you want convert string u'1,2,3,4' to a list of ints [1,2,3,4]? >> Then the below will work. >> >> [int(n) for n in u'1,2,3,4'.replace(',', '')] >> >> > Actually, I needed it to be converted to something without a string > attached to it. See a post above, and it was fixed by eval(), > > Thanks though. And I'm sure at some point this morning in a moment of > frustration rather than logic, I tried your approach. > What do you mean by "without a string attached to it"? Also using eval could be dangerous unless you're sure the stuff coming out of your dbase is safe. From sander.sweers at gmail.com Thu Oct 14 21:02:50 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Thu, 14 Oct 2010 21:02:50 +0200 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: Message-ID: On 14 October 2010 20:29, David Hutto wrote: > Actually, I needed it to be converted to something without a string > attached to it. See a post above, and it was fixed by eval(), Using eval is a big security risk and is generally not recommended for any production code. What do you think eval() return for your string? A tuple of ints which for your use case serves the same purpose as using the list comprehension. If you really want (you really don't) to use eval() then at least use the safe one from the ast mode. >>> from ast import literal_eval >>> literal_eval(u'1,2,3,4') (1, 2, 3, 4) >>> eval(u'1,2,3,4') (1, 2, 3, 4) As you can see for your use case both return a tuple of ints. Greets Sander From sander.sweers at gmail.com Thu Oct 14 21:06:27 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Thu, 14 Oct 2010 21:06:27 +0200 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: Message-ID: On 14 October 2010 21:02, Sander Sweers wrote: > If you really want (you really don't) to use eval() then at least use Oops, hit send to soon. "(you really don't)" should have been "(you really don't need to use it)". Greets Sander From ghashsnaga at gmail.com Thu Oct 14 21:07:11 2010 From: ghashsnaga at gmail.com (Ara Kooser) Date: Thu, 14 Oct 2010 13:07:11 -0600 Subject: [Tutor] Merging Text Files Message-ID: Emile, I modified the code to this: for line in xml: ID = line.split()[1] rslt = "%s,%s"% (line,PIDS[ID]) print rslt and ended up with this error: Traceback (most recent call last): File "/Users/ara/Desktop/biopy_programs/merge2.py", line 16, in rslt = "%s,%s"% (line,PIDS[ID]) KeyError: 'sp.' Ara -- Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis an sub cardine glacialis ursae. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ghashsnaga at gmail.com Thu Oct 14 21:17:33 2010 From: ghashsnaga at gmail.com (Ara Kooser) Date: Thu, 14 Oct 2010 13:17:33 -0600 Subject: [Tutor] Merging Text Files In-Reply-To: References: Message-ID: Adam, I am going to try and sort through the pseudocode you provided to see if I can get things up and running that way has well. This a part of a larger workflow thing and needs to be in the format that I have. Sticking all this into a database is down the road a ways. *for every line in ONE:* * make a dictionary with a key = to the protein ID and the value, the rest* I am pretty sure I have this correct by doing this: xml = open("final.txt",'r') gen = open("final_gen.txt",'r') PIDS = {} for proteinVals in xml: ID = proteinVals.split()[0] PIDS[ID] = proteinVals print PIDS For this part: *for every line in TWO:* * if the dictionary has the same protein ID:* * perform the string merging and write to the file* For this part I would need to do something like: for line in gen: ID = proteinVals.split()[0] if ID in PIDS: (I need to check for the key?) ???How do you merge lines being read in to a previous dictionary else: move on to next number Thanks! ara -- Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis an sub cardine glacialis ursae. -------------- next part -------------- An HTML attachment was scrubbed... URL: From smokefloat at gmail.com Thu Oct 14 21:21:45 2010 From: smokefloat at gmail.com (David Hutto) Date: Thu, 14 Oct 2010 15:21:45 -0400 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: <4CB752F0.6050008@gmail.com> References: <4CB752F0.6050008@gmail.com> Message-ID: On Thu, Oct 14, 2010 at 2:58 PM, Adam Bark wrote: > On 14/10/10 19:29, David Hutto wrote: >> >> On Thu, Oct 14, 2010 at 2:19 PM, Sander Sweers >> ?wrote: >> >>> >>> On 14 October 2010 16:14, David Hutto ?wrote: >>> >>>> >>>> (u'graph1', u'Line', u'222', u'BLUE', u'1,2,3,4', u'True', u'0,5,0,10') >>>> >>>> Which is a tuple of unicode strings. From this I >>>> need to place portions of the tuple into other fields, >>>> but not as unicode strings, but literals no ''. >>>> >>>> For example if I have the following line: >>>> >>>> self.lines = self.newplot.plot([1,2,3,4]) >>>> >>> >>> So you want convert string u'1,2,3,4' to a list of ints [1,2,3,4]? >>> Then the below will work. >>> >>> [int(n) for n in u'1,2,3,4'.replace(',', '')] >>> >>> >> >> Actually, I needed it to be converted to something without a string >> attached to it. See a post above, and it was fixed by eval(), >> >> Thanks though. And I'm sure at some point this morning in a moment of >> frustration rather than logic, I tried your approach. >> > > What do you mean by "without a string attached to it"? > Also using eval could be dangerous unless you're sure the stuff coming out > of your dbase is safe. > Read the above posts and it should be explanation enough. From adam.jtm30 at gmail.com Thu Oct 14 21:24:33 2010 From: adam.jtm30 at gmail.com (Adam Bark) Date: Thu, 14 Oct 2010 20:24:33 +0100 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: <4CB752F0.6050008@gmail.com> Message-ID: <4CB758F1.6050203@gmail.com> On 14/10/10 20:21, David Hutto wrote: > On Thu, Oct 14, 2010 at 2:58 PM, Adam Bark wrote: > >> >>> Actually, I needed it to be converted to something without a string >>> attached to it. See a post above, and it was fixed by eval(), >>> >>> Thanks though. And I'm sure at some point this morning in a moment of >>> frustration rather than logic, I tried your approach. >>> >>> >> What do you mean by "without a string attached to it"? >> Also using eval could be dangerous unless you're sure the stuff coming out >> of your dbase is safe. >> >> > Read the above posts and it should be explanation enough. > I did read them and it's really not clear. From smokefloat at gmail.com Thu Oct 14 21:25:04 2010 From: smokefloat at gmail.com (David Hutto) Date: Thu, 14 Oct 2010 15:25:04 -0400 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: Message-ID: On Thu, Oct 14, 2010 at 3:02 PM, Sander Sweers wrote: > On 14 October 2010 20:29, David Hutto wrote: >> Actually, I needed it to be converted to something without a string >> attached to it. See a post above, and it was fixed by eval(), > > Using eval is a big security risk and is generally not recommended for > any production code. What do you think eval() return for your string? > A tuple of ints which for your use case serves the same purpose as > using the list comprehension. > > If you really want (you really don't) to use eval() then at least use > the safe one from the ast mode. > >>>> from ast import literal_eval >>>> literal_eval(u'1,2,3,4') > (1, 2, 3, 4) >>>> eval(u'1,2,3,4') > (1, 2, 3, 4) > > As you can see for your use case both return a tuple of ints. > > Greets > Sander > Thanks for pointing that out. I'll definitely note that for future reference, but this was for a somewhat personal data project app, So it's not necessary to worry about insertion of data other than my own inputs. From joel.goldstick at gmail.com Thu Oct 14 21:27:01 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Thu, 14 Oct 2010 15:27:01 -0400 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: Message-ID: To take a string of comma separated integers and convert to a list of floats: >>> x = u'1,2,3,4' >>> y = x.split(',') >>> z = [float(f) for f in y] >>> z [1.0, 2.0, 3.0, 4.0] >>> -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From smokefloat at gmail.com Thu Oct 14 21:31:39 2010 From: smokefloat at gmail.com (David Hutto) Date: Thu, 14 Oct 2010 15:31:39 -0400 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: <4CB758F1.6050203@gmail.com> References: <4CB752F0.6050008@gmail.com> <4CB758F1.6050203@gmail.com> Message-ID: On Thu, Oct 14, 2010 at 3:24 PM, Adam Bark wrote: > On 14/10/10 20:21, David Hutto wrote: >> >> On Thu, Oct 14, 2010 at 2:58 PM, Adam Bark ?wrote: >> >>> >>>> Actually, I needed it to be converted to something without a string >>>> attached to it. See a post above, and it was fixed by eval(), >>>> >>>> Thanks though. And I'm sure at some point this morning in a moment of >>>> frustration rather than logic, I tried your approach. >>>> >>>> >>> >>> What do you mean by "without a string attached to it"? >>> Also using eval could be dangerous unless you're sure the stuff coming >>> out >>> of your dbase is safe. >>> >>> >> >> Read the above posts and it should be explanation enough. >> > > I did read them and it's really not clear. > I needed to have: self.lines = self.newplot.plot(self.plot) from the data I brought in I got the following when the variable above(self.plot) was inserted: self.lines = self.newplot.plot(u'plot') So by applying eval: self.lines = self.newplot.plot(eval(self.plot)) It then inserted the following when the variable self.plot was used: self.lines = self.newplot.plot(eval(plot) no u'stringhere' in the second version with eval around the variable. I hope that makes it clearer. Otherwise I might have to let you borrow my Windex to clean the screen. From smokefloat at gmail.com Thu Oct 14 21:33:27 2010 From: smokefloat at gmail.com (David Hutto) Date: Thu, 14 Oct 2010 15:33:27 -0400 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: <4CB752F0.6050008@gmail.com> <4CB758F1.6050203@gmail.com> Message-ID: On Thu, Oct 14, 2010 at 3:31 PM, David Hutto wrote: > On Thu, Oct 14, 2010 at 3:24 PM, Adam Bark wrote: >> On 14/10/10 20:21, David Hutto wrote: >>> >>> On Thu, Oct 14, 2010 at 2:58 PM, Adam Bark ?wrote: >>> >>>> >>>>> Actually, I needed it to be converted to something without a string >>>>> attached to it. See a post above, and it was fixed by eval(), >>>>> >>>>> Thanks though. And I'm sure at some point this morning in a moment of >>>>> frustration rather than logic, I tried your approach. >>>>> >>>>> >>>> >>>> What do you mean by "without a string attached to it"? >>>> Also using eval could be dangerous unless you're sure the stuff coming >>>> out >>>> of your dbase is safe. >>>> >>>> >>> >>> Read the above posts and it should be explanation enough. >>> >> >> I did read them and it's really not clear. >> > > I needed to have: > self.lines = self.newplot.plot(self.plot) > > from the data I brought in I got the following when the variable > above(self.plot) was inserted: > > self.lines = self.newplot.plot(u'plot') > > So by applying eval: > > self.lines = self.newplot.plot(eval(self.plot)) > > It then inserted the following ?when the variable self.plot was used: > > self.lines = self.newplot.plot(eval(plot) > > no u'stringhere' in the second version with eval around the variable. > > > I hope that makes it clearer. Otherwise I might have to let you borrow > my Windex to clean the screen. > In other words I needed (1,2,3,4) not u'(1,2,3,4)' to be inserted for variable self.plot From adam.jtm30 at gmail.com Thu Oct 14 21:39:37 2010 From: adam.jtm30 at gmail.com (Adam Bark) Date: Thu, 14 Oct 2010 20:39:37 +0100 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: <4CB752F0.6050008@gmail.com> <4CB758F1.6050203@gmail.com> Message-ID: <4CB75C79.6050409@gmail.com> On 14/10/10 20:33, David Hutto wrote: > On Thu, Oct 14, 2010 at 3:31 PM, David Hutto wrote: > >> On Thu, Oct 14, 2010 at 3:24 PM, Adam Bark wrote: >> >>> On 14/10/10 20:21, David Hutto wrote: >>> >>>> On Thu, Oct 14, 2010 at 2:58 PM, Adam Bark wrote: >>>> >>>> >>>>> >>>>>> Actually, I needed it to be converted to something without a string >>>>>> attached to it. See a post above, and it was fixed by eval(), >>>>>> >>>>>> Thanks though. And I'm sure at some point this morning in a moment of >>>>>> frustration rather than logic, I tried your approach. >>>>>> >>>>>> >>>>>> >>>>> What do you mean by "without a string attached to it"? >>>>> Also using eval could be dangerous unless you're sure the stuff coming >>>>> out >>>>> of your dbase is safe. >>>>> >>>>> >>>>> >>>> Read the above posts and it should be explanation enough. >>>> >>>> >>> I did read them and it's really not clear. >>> >>> >> I needed to have: >> self.lines = self.newplot.plot(self.plot) >> >> from the data I brought in I got the following when the variable >> above(self.plot) was inserted: >> >> self.lines = self.newplot.plot(u'plot') >> >> So by applying eval: >> >> self.lines = self.newplot.plot(eval(self.plot)) >> >> It then inserted the following when the variable self.plot was used: >> >> self.lines = self.newplot.plot(eval(plot) >> >> no u'stringhere' in the second version with eval around the variable. >> >> >> I hope that makes it clearer. Otherwise I might have to let you borrow >> my Windex to clean the screen. >> >> > In other words I needed (1,2,3,4) not u'(1,2,3,4)' to be inserted for > variable self.plot > In which case the code Sanders sent you is a much more sensible way of recovering your data. Evalling the string u'plot' still doesn't make much sense to me though. I think I may have been overestimating the quality of your code. From ademlookes at gmail.com Thu Oct 14 22:12:53 2010 From: ademlookes at gmail.com (Adam Lucas) Date: Thu, 14 Oct 2010 15:12:53 -0500 Subject: [Tutor] Merging Text Files In-Reply-To: References: Message-ID: I sent both emails and may have confused things: 1. PIDS.has_key(ID) returns True/False. you need to make sure the dictionary has the key before you fetch PIDS[NotAKey] and get a KeyError. 2. line.split() splits at and removes whitespace, leaving commas. line.split(",") splits at and removes commas. On Thu, Oct 14, 2010 at 13:43, Adam Lucas wrote: > Whoops: > > 1) dictionary.has_key() ??? > 2) I don't know if it's a typo or oversight, but there's a comma in you > dictionary key, line.split(',')[0]. > 3) Forget the database if it's part of a larger workflow unless your job is > to adapt a biological workflow database for your lab. > > > > On Thu, Oct 14, 2010 at 09:48, Ara Kooser wrote: > >> Morning all, >> >> I took the pseudocode that Emile provided and tried to write a python >> program. I may have taken the pseudocode to literally. >> >> So what I wrote was this: >> xml = open("final.txt",'r') >> gen = open("final_gen.txt",'r') >> >> PIDS = {} >> for proteinVals in gen: >> >> ID = proteinVals.split()[0] >> PIDS[ID] = proteinVals >> >> print PIDS >> >> for line in xml: >> ID = proteinVals.split()[1] >> rslt = "%s,%s"% (line,PIDS[ID]) >> print rslt >> >> So the first part I get. I read in gen that has this format as a text >> file: >> >> *Protein ID, Locus Tag, Start/Stop* >> ZP_05482482, StAA4_010100030484, >> complement(NZ_ACEV01000078.1:25146..40916) >> ZP_07281899, SSMG_05939, complement(NZ_GG657746.1:6565974..6581756) >> ZP_05477599, StAA4_010100005861, NZ_ACEV01000013.1:86730..102047 >> ... >> Put that into a dictionary with a key that is the Protein ID at position 0 >> in the dictionary. >> >> The second part reads in the file xml which has this format: >> >> *Species, Protein ID, E Value, Length* >> Streptomyces sp. AA4, ZP_05482482, 2.8293600000000001e-140, 5256, >> Streptomyces sp. AA4, ZP_05482482, 8.0333299999999997e-138, 5256, >> Streptomyces sp. AA4, ZP_05482482, 1.08889e-124, 5256, >> Streptomyces sp. AA4, ZP_07281899, 2.9253900000000001e-140, 5260, >> Streptomyces sp. AA4, ZP_07281899, 8.2369599999999995e-138, 5260, >> .... >> *same protein id multiple entries >> >> The program splits the file and does something with the 1 position which >> is the proten id in the xml file. After that I am not really sure what is >> happening. I can't remember what the %s means. Something with a string? >> >> When this runs I get the following error: >> Traceback (most recent call last): >> File "/Users/ara/Desktop/biopy_programs/merge2.py", line 18, in >> rslt = "%s,%s"% (line,PIDS[ID]) >> KeyError: 'StAA4_010100017400,' >> >> From what I can tell it's not happy about the dictionary key. >> >> In the end I am looking for a way to merge these two files and for each >> protein ID add the locus tag and start/stop like this: >> *Species, Protein ID, Locus Tag, E Value, Length*, *Start/Stop* >> >> Streptomyces sp. AA4, ZP_05482482, StAA4_010100030484, >> 2.8293600000000001e-140, 5256, complement(NZ_ACEV01000078.1:25146..40916) >> Streptomyces sp. AA4, ZP_05482482, StAA4_010100030484, >> 8.0333299999999997e-138, 5256, complement(NZ_ACEV01000078.1:25146..40916) >> Streptomyces sp. AA4, ZP_05482482, StAA4_010100030484, 1.08889e-124, 5256, >> complement(NZ_ACEV01000078.1:25146..40916) >> Streptomyces sp. AA4, ZP_07281899, SSMG_05939, 2.9253900000000001e-140, >> 5260, complement(NZ_GG657746.1:6565974..6581756) >> Streptomyces sp. AA4, ZP_07281899, SSMG_05939, 8.2369599999999995e-138, >> 5260, complement(NZ_GG657746.1:6565974..6581756) >> >> Do you have any suggestions for how to proceed. It feels like I am getting >> closer. :) >> >> >> Note: >> When I change this part of the code to 0 >> for line in xml: >> ID = proteinVals.split()[0] >> rslt = "%s,%s"% (line,PIDS[ID]) >> print rslt >> >> I get the following output: >> Streptomyces sp. AA4, ZP_05482482, 8.0333299999999997e-138, 5256, >> ,ZP_05479896, StAA4_010100017400, NZ_ACEV01000043.1:241968..>242983 >> >> >> Streptomyces sp. AA4, ZP_05482482, 1.08889e-124, 5256, >> ,ZP_05479896, StAA4_010100017400, NZ_ACEV01000043.1:241968..>242983 >> >> >> Streptomyces sp. AA4, ZP_07281899, 2.9253900000000001e-140, 5260, >> ,ZP_05479896, StAA4_010100017400, NZ_ACEV01000043.1:241968..>242983 >> >> Which seems closer but all it's doing is repeating the same Locus Tag and >> Start/Stop for each entry. >> >> Thank you! >> >> Ara >> >> >> -- >> Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis an >> sub cardine glacialis ursae. >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > > > -- > Data is not information, information is not knowledge, knowledge is not > understanding, understanding is not wisdom. > --Clifford Stoll > -- Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom. --Clifford Stoll -------------- next part -------------- An HTML attachment was scrubbed... URL: From ghashsnaga at gmail.com Thu Oct 14 22:44:45 2010 From: ghashsnaga at gmail.com (Ara Kooser) Date: Thu, 14 Oct 2010 14:44:45 -0600 Subject: [Tutor] Merging Text Files Message-ID: Hey all, It's mostly solved. The program prints out to the screen just fine except for the new line return. Here is what I ended up using: ######## #Merges two files into one using dictionaries ######## xml = open("final.txt",'r') gen = open("final_gen.txt",'r') PIDS = {} for proteinVals in gen: ID = proteinVals.split(',')[0] PIDS[ID] = proteinVals print PIDS for line in xml: ID = line.split(',')[1] IDs = ID.strip() rslt = "%s,%s"% (line,PIDS[IDs]) print rslt When I write this to a file what's the best way to take out the new line? Thanks again for everyone's help and pseudocode. It's been about 4 years since my last programming project! ara -- Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis an sub cardine glacialis ursae. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Oct 15 00:43:17 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 14 Oct 2010 23:43:17 +0100 Subject: [Tutor] join question References: Message-ID: "Roelof Wobben" wrote > print ''.join([zf.getinfo('%s.txt' % p).comment for p in zpp]) > > So I thought that this would be the same : > > for p in zpp: > test = zf.getinfo(p).comment > print ''.join(test) > > But it seems not to work > > Can anyone explain why not ? Because it's not the same. test in your version is only a single item. In the original its a list of items. So to write similar code explicitly you need: lst = [] for p in zpp: test = zf.getinfo(p).comment lst.append(test) print ''.join(lst) HTH, Alan G. From joel.goldstick at gmail.com Fri Oct 15 00:55:50 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Thu, 14 Oct 2010 18:55:50 -0400 Subject: [Tutor] join question In-Reply-To: References: Message-ID: On Thu, Oct 14, 2010 at 6:43 PM, Alan Gauld wrote: > > "Roelof Wobben" wrote > > > > print ''.join([zf.getinfo('%s.txt' % p).comment for p in zpp]) >> >> So I thought that this would be the same : >> >> for p in zpp: >> test = zf.getinfo(p).comment >> print ''.join(test) >> >> But it seems not to work >> >> Can anyone explain why not ? >> > > Because it's not the same. test in your version is only a single item. > In the original its a list of items. So to write similar code explicitly > you need: > > lst = [] > > for p in zpp: > test = zf.getinfo(p).comment > lst.append(test) > print ''.join(lst) > > HTH, > > Alan G. > > There are two problems. Alan shows the solution to the joining of each element of the list, but also you need to have the correct argument for getinfo. In your first example you append ".txt" to p, but in the loop example you fail to append it. See my earlier post -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Oct 15 00:56:11 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 14 Oct 2010 23:56:11 +0100 Subject: [Tutor] Converting from unicode to nonstring References: <4CB752F0.6050008@gmail.com><4CB758F1.6050203@gmail.com> Message-ID: "David Hutto" wrote > In other words I needed (1,2,3,4) not u'(1,2,3,4)' to be inserted > for variable self.plot You appear to be making this much more difficult than it needs to be. The values you retrieve from the database are strings (forget about the unicode aspect its not really relevant here) just as if you had used raw_input to read them from the user. How would you convert a string received from raw_input() to a series of numbers? Would you have used eval() or just split the string and called int() or float()? Something like: inp = raw_input("Enter a string of numbers separated by commas") nums = [int(n) for n in inp.split(',')] eval() should always be considered a last resort because of the potential for causing damage and the extremely obscure errors it can throw up with badly formed input. I think you are letting the unicode representation spook you into doing things in a way you wouldn't if it was normal raw_input you were using. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Fri Oct 15 01:00:11 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 15 Oct 2010 00:00:11 +0100 Subject: [Tutor] Converting from unicode to nonstring References: Message-ID: "David Hutto" wrote > it's not necessary to worry about insertion of data other than my > own > inputs. But can you be sure that you won't accidentally mistype something that eval can read as valid code but that does something unexpected - even if its only throw an apparently bizarre error dump at you... Relying on eval() reading your database content as if it were Python code - which is what it does - is a risky strategy. But, heh, it's your data, your project, so as long as you understand the risks then it's your decision. :-) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From g.nius.ck at gmail.com Fri Oct 15 03:22:51 2010 From: g.nius.ck at gmail.com (chris) Date: Thu, 14 Oct 2010 21:22:51 -0400 Subject: [Tutor] Networking In-Reply-To: References: <4CA788E2.4060006@gmail.com> <33D037A9-0230-42B3-9AFC-A1884F00AE88@gmail.com> <4CA90FAF.1070002@gmail.com> <4CB3A1A7.9080804@gmail.com> Message-ID: <4CB7ACEB.1050405@gmail.com> On 10/14/2010 12:49 PM, Evert Rol wrote: > Hi Chris, > > Bit hard to comment on the actual code, as it was in attachments, but the important bit is here: > > class Handler(SocketServer.BaseRequestHandler): #the handler > '''A handler which calls %s in the handle method.'''%handle_func > def handle(self): #the handle method > self.data = self.request.recv(1024) #the data > self.client = self.request #the client > handle_func(self) #call the handle method giving self > > > (I did a bit of reformatting, since I had to try the code myself, because I didn't spot the error immediately.) > > In essence, your server accepts the client connection, then proceeds to the handle function, where it performs the necessary actions. It will *not* call the handle function for each set of data it receives, it just stays there. > So you'll need a while loop in your handle function that keeps on processing the client data, until there is not client data anymore (EOF or similar), then return. > > > Evert > > > > >>>>> Dear Tutors, >>>>> I have attached my 2 programs for networking. It uses socket and SocketServer, but it just simplifies it even more. The problem is it won't work. The Client raises the error, (with trace back) >>>>> Traceback (most recent call last): >>>>> File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 34, in >>>>> print client.recv() >>>>> File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 16, in recv >>>>> return self.__sock.recv(1024) >>>>> error: [Errno 10053] An established connection was aborted by the software in your host machine >>>>> The server seems to get an error of some sort, but also seems to catch and print it. It prints >>>>> ---------------------------------------- >>>>> Exception happened during processing of request from ('127.0.0.1', 1424) >>>>> Traceback (most recent call last): >>>>> File "C:\Python26\lib\SocketServer.py", line 281, in _handle_request_noblock >>>>> self.process_request(request, client_address) >>>>> File "C:\Python26\lib\SocketServer.py", line 307, in process_request >>>>> self.finish_request(request, client_address) >>>>> File "C:\Python26\lib\SocketServer.py", line 320, in finish_request >>>>> self.RequestHandlerClass(request, client_address, self) >>>>> TypeError: 'NoneType' object is not callable >>>>> ---------------------------------------- >>>>> I look at both of the documentations of socket and SocketServer, but I couldn't firgue it out. I don't know much about networking. Please Help >>>>> >>>> I don't know much about networking, less so about it on Windows; also, I've only scanned quickly through the server script, but I notice your create_handler() function doesn't return anything. I guess it needs to return the Handler class. >>>> The example in the Python docs doesn't use a factory function, but instead directly puts the class as the second argument to TCPServer. >>>> So the second argument needs to be a class, but since your create_handler() function returns nothing, you presumably get this NoneType exception. >>>> >>>> Evert >>>> >>>> >>> Yeah, that could be a problem. It should return the class. >>> >> Now it won't send more than once. The Error the clients raises is: >> Traceback (most recent call last): >> File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 34, in >> print client.recv() >> File "G:\My Dropbox\My Dropbox\Chris\Not done\client.py", line 16, in recv >> return self.__sock.recv(1024) >> error: [Errno 10053] An established connection was aborted by the software in your host machine >> The server is blissfully unaware of its crashing clients, and keeps on going to help more clients to there dooms. The client can receive data multiple times, but just can send twice. Please help. >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > But what if I want it to serve one client, go to another and then go back. How does that work? From prologic at shortcircuit.net.au Fri Oct 15 03:28:24 2010 From: prologic at shortcircuit.net.au (James Mills) Date: Fri, 15 Oct 2010 11:28:24 +1000 Subject: [Tutor] Networking In-Reply-To: <4CB7ACEB.1050405@gmail.com> References: <4CA788E2.4060006@gmail.com> <33D037A9-0230-42B3-9AFC-A1884F00AE88@gmail.com> <4CA90FAF.1070002@gmail.com> <4CB3A1A7.9080804@gmail.com> <4CB7ACEB.1050405@gmail.com> Message-ID: On Fri, Oct 15, 2010 at 11:22 AM, chris wrote: >> But what if I want it to serve one client, go to another and then go back. >> How does that work? You do some I/O multi-plexing or multi-processing/threading. You might want to do some reading on this. cheers James -- -- James Mills -- -- "Problems are solved by method" From steve at pearwood.info Fri Oct 15 04:09:36 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 15 Oct 2010 13:09:36 +1100 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: Message-ID: <201010151309.37352.steve@pearwood.info> On Fri, 15 Oct 2010 04:43:46 am David Hutto wrote: > Fixed with: > > self.lines = self.newplot.plot(eval(self.plot)) Anytime you use eval, chances are that it isn't fixed at all, but just badly and dangerously papered over. I really, really wish that eval and exec weren't built-ins. They're powerful but dangerous and slow, and making them built-ins just encourages newbies to use them inappropriately. -- Steven D'Aprano From steve at pearwood.info Fri Oct 15 04:27:31 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 15 Oct 2010 13:27:31 +1100 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: Message-ID: <201010151327.31512.steve@pearwood.info> On Fri, 15 Oct 2010 05:19:57 am Sander Sweers wrote: > So you want convert string u'1,2,3,4' to a list of ints [1,2,3,4]? > Then the below will work. > > [int(n) for n in u'1,2,3,4'.replace(',', '')] That will break if you have u'100,2,3,4'. Better is: >>> s = '1, 200 , -3,4' # or whatever >>> [int(x) for x in s.split(',')] [1, 200, -3, 4] -- Steven D'Aprano From mehgcap at gmail.com Fri Oct 15 04:30:56 2010 From: mehgcap at gmail.com (Alex Hall) Date: Thu, 14 Oct 2010 22:30:56 -0400 Subject: [Tutor] trying it again: p2p for game Message-ID: Hi all, Alright: after a few months letting it rest, I am trying this Battleship thing again. I found a p2p library (attached) and have tried to implement it (p2p.py). However, I am always getting the same error: errno2: no connection could be made because the target machine actively refused it. I suspect my ip addresses are at fault. I am on a public network here at school, and I keep getting 130... addresses. However, my friend several cities away cannot use 130, since that is local, right? I am not sure where to get my ip; that is, where to get an ip that, coupled with the hard-coded port number, will let my computer connect to his and vice versa. This does not work for two instances on my computer, it does not work for my computer and my friend's (different networks), and it does not work for my friend when he runs two computers on the same network and tries to connect the two... always that same error once we try to connect. This library came with no documentation, and I suspect it was not intended as a public release, but it is the only p2p file I could find. I asked the professor here who taught networking, but she just gave me some Microsoft pages on p2p (helpful for concepts, but not Python-specific), and no one here knows Python well (CS is not a popular degree here anyway, so there are not too many of us). If anyone can spot what is going on, that would be wonderful. Sorry to keep sending messages about this - I know it must be frustrating for those who keep responding - but I am hoping that coming at it from a p2p angle, instead of a server/client angle, will help to clear things up... -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap -------------- next part -------------- #!/usr/bin/python # btpeer.py import socket import struct import threading import time import traceback def btdebug( msg ): """ Prints a messsage to the screen with the name of the current thread """ print "[%s] %s" % ( str(threading.currentThread().getName()), msg ) #============================================================================== class BTPeer: """ Implements the core functionality that might be used by a peer in a P2P network. """ #-------------------------------------------------------------------------- def __init__( self, maxpeers, serverport, myid=None, serverhost = None ): #-------------------------------------------------------------------------- """ Initializes a peer servent (sic.) with the ability to catalog information for up to maxpeers number of peers (maxpeers may be set to 0 to allow unlimited number of peers), listening on a given server port , with a given canonical peer name (id) and host address. If not supplied, the host address (serverhost) will be determined by attempting to connect to an Internet host like Google. """ self.debug = 0 self.maxpeers = int(maxpeers) self.serverport = int(serverport) if serverhost: self.serverhost = serverhost else: self.__initserverhost() if myid: self.myid = myid else: self.myid = '%s:%d' % (self.serverhost, self.serverport) self.peerlock = threading.Lock() # ensure proper access to # peers list (maybe better to use # threading.RLock (reentrant)) self.peers = {} # peerid ==> (host, port) mapping self.shutdown = False # used to stop the main loop self.handlers = {} self.router = None #-------------------------------------------------------------------------- def __initserverhost( self ): #-------------------------------------------------------------------------- """ Attempt to connect to an Internet host in order to determine the local machine's IP address. """ s = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) s.connect( ( "www.google.com", 80 ) ) self.serverhost = s.getsockname()[0] s.close() #-------------------------------------------------------------------------- def __debug( self, msg ): #-------------------------------------------------------------------------- if self.debug: btdebug( msg ) #-------------------------------------------------------------------------- def __handlepeer( self, clientsock ): #-------------------------------------------------------------------------- """ handlepeer( new socket connection ) -> () Dispatches messages from the socket connection """ self.__debug( 'New child ' + str(threading.currentThread().getName()) ) self.__debug( 'Connected ' + str(clientsock.getpeername()) ) host, port = clientsock.getpeername() peerconn = BTPeerConnection( None, host, port, clientsock, debug=False ) try: msgtype, msgdata = peerconn.recvdata() if msgtype: msgtype = msgtype.upper() if msgtype not in self.handlers: self.__debug( 'Not handled: %s: %s' % (msgtype, msgdata) ) else: self.__debug( 'Handling peer msg: %s: %s' % (msgtype, msgdata) ) self.handlers[ msgtype ]( peerconn, msgdata ) except KeyboardInterrupt: raise except: if self.debug: traceback.print_exc() self.__debug( 'Disconnecting ' + str(clientsock.getpeername()) ) peerconn.close() # end handlepeer method #-------------------------------------------------------------------------- def __runstabilizer( self, stabilizer, delay ): #-------------------------------------------------------------------------- while not self.shutdown: stabilizer() time.sleep( delay ) #-------------------------------------------------------------------------- def setmyid( self, myid ): #-------------------------------------------------------------------------- self.myid = myid #-------------------------------------------------------------------------- def startstabilizer( self, stabilizer, delay ): #-------------------------------------------------------------------------- """ Registers and starts a stabilizer function with this peer. The function will be activated every seconds. """ t = threading.Thread( target = self.__runstabilizer, args = [ stabilizer, delay ] ) t.start() #-------------------------------------------------------------------------- def addhandler( self, msgtype, handler ): #-------------------------------------------------------------------------- """ Registers the handler for the given message type with this peer """ assert len(msgtype) == 4 self.handlers[ msgtype ] = handler #-------------------------------------------------------------------------- def addrouter( self, router ): #-------------------------------------------------------------------------- """ Registers a routing function with this peer. The setup of routing is as follows: This peer maintains a list of other known peers (in self.peers). The routing function should take the name of a peer (which may not necessarily be present in self.peers) and decide which of the known peers a message should be routed to next in order to (hopefully) reach the desired peer. The router function should return a tuple of three values: (next-peer-id, host, port). If the message cannot be routed, the next-peer-id should be None. """ self.router = router #-------------------------------------------------------------------------- def addpeer( self, peerid, host, port ): #-------------------------------------------------------------------------- """ Adds a peer name and host:port mapping to the known list of peers. """ if peerid not in self.peers and (self.maxpeers == 0 or len(self.peers) < self.maxpeers): self.peers[ peerid ] = (host, int(port)) return True else: return False #-------------------------------------------------------------------------- def getpeer( self, peerid ): #-------------------------------------------------------------------------- """ Returns the (host, port) tuple for the given peer name """ assert peerid in self.peers # maybe make this just a return NULL? return self.peers[ peerid ] #-------------------------------------------------------------------------- def removepeer( self, peerid ): #-------------------------------------------------------------------------- """ Removes peer information from the known list of peers. """ if peerid in self.peers: del self.peers[ peerid ] #-------------------------------------------------------------------------- def addpeerat( self, loc, peerid, host, port ): #-------------------------------------------------------------------------- """ Inserts a peer's information at a specific position in the list of peers. The functions addpeerat, getpeerat, and removepeerat should not be used concurrently with addpeer, getpeer, and/or removepeer. """ self.peers[ loc ] = (peerid, host, int(port)) #-------------------------------------------------------------------------- def getpeerat( self, loc ): #-------------------------------------------------------------------------- if loc not in self.peers: return None return self.peers[ loc ] #-------------------------------------------------------------------------- def removepeerat( self, loc ): #-------------------------------------------------------------------------- removepeer( self, loc ) #-------------------------------------------------------------------------- def getpeerids( self ): #-------------------------------------------------------------------------- """ Return a list of all known peer id's. """ return self.peers.keys() #-------------------------------------------------------------------------- def numberofpeers( self ): #-------------------------------------------------------------------------- """ Return the number of known peer's. """ return len(self.peers) #-------------------------------------------------------------------------- def maxpeersreached( self ): #-------------------------------------------------------------------------- """ Returns whether the maximum limit of names has been added to the list of known peers. Always returns True if maxpeers is set to 0. """ assert self.maxpeers == 0 or len(self.peers) <= self.maxpeers return self.maxpeers > 0 and len(self.peers) == self.maxpeers #-------------------------------------------------------------------------- def makeserversocket( self, port, backlog=5 ): #-------------------------------------------------------------------------- """ Constructs and prepares a server socket listening on the given port. """ s = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) s.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 ) s.bind( ( '', port ) ) s.listen( backlog ) return s #-------------------------------------------------------------------------- def sendtopeer( self, peerid, msgtype, msgdata, waitreply=True ): #-------------------------------------------------------------------------- """ sendtopeer( peer id, message type, message data, wait for a reply ) -> [ ( reply type, reply data ), ... ] Send a message to the identified peer. In order to decide how to send the message, the router handler for this peer will be called. If no router function has been registered, it will not work. The router function should provide the next immediate peer to whom the message should be forwarded. The peer's reply, if it is expected, will be returned. Returns None if the message could not be routed. """ if self.router: nextpid, host, port = self.router( peerid ) if not self.router or not nextpid: self.__debug( 'Unable to route %s to %s' % (msgtype, peerid) ) return None #host,port = self.peers[nextpid] return self.connectandsend( host, port, msgtype, msgdata, pid=nextpid, waitreply=waitreply ) #-------------------------------------------------------------------------- def connectandsend( self, host, port, msgtype, msgdata, pid=None, waitreply=True ): #-------------------------------------------------------------------------- """ connectandsend( host, port, message type, message data, peer id, wait for a reply ) -> [ ( reply type, reply data ), ... ] Connects and sends a message to the specified host:port. The host's reply, if expected, will be returned as a list of tuples. """ msgreply = [] try: peerconn = BTPeerConnection( pid, host, port, debug=self.debug ) peerconn.senddata( msgtype, msgdata ) self.__debug( 'Sent %s: %s' % (pid, msgtype) ) if waitreply: onereply = peerconn.recvdata() while (onereply != (None,None)): msgreply.append( onereply ) self.__debug( 'Got reply %s: %s' % ( pid, str(msgreply) ) ) onereply = peerconn.recvdata() peerconn.close() except KeyboardInterrupt: raise except: if self.debug: traceback.print_exc() return msgreply # end connectsend method #-------------------------------------------------------------------------- def checklivepeers( self ): #-------------------------------------------------------------------------- """ Attempts to ping all currently known peers in order to ensure that they are still active. Removes any from the peer list that do not reply. This function can be used as a simple stabilizer. """ todelete = [] for pid in self.peers: isconnected = False try: self.__debug( 'Check live %s' % pid ) host,port = self.peers[pid] peerconn = BTPeerConnection( pid, host, port, debug=self.debug ) peerconn.senddata( 'PING', '' ) isconnected = True except: todelete.append( pid ) if isconnected: peerconn.close() self.peerlock.acquire() try: for pid in todelete: if pid in self.peers: del self.peers[pid] finally: self.peerlock.release() # end checklivepeers method #-------------------------------------------------------------------------- def mainloop( self ): #-------------------------------------------------------------------------- s = self.makeserversocket( self.serverport ) s.settimeout(10) self.__debug( 'Server started: %s (%s:%d)' % ( self.myid, self.serverhost, self.serverport ) ) while not self.shutdown: try: self.__debug( 'Listening for connections...' ) clientsock, clientaddr = s.accept() clientsock.settimeout(None) t = threading.Thread( target = self.__handlepeer, args = [ clientsock ] ) t.start() except KeyboardInterrupt: print 'KeyboardInterrupt: stopping mainloop' self.shutdown = True continue except: if self.debug: traceback.print_exc() continue # end while loop self.__debug( 'Main loop exiting' ) s.close() # end mainloop method # end BTPeer class # ********************************************************** class BTPeerConnection: #-------------------------------------------------------------------------- def __init__( self, peerid, host, port, sock=None, debug=False ): #-------------------------------------------------------------------------- # any exceptions thrown upwards self.id = peerid self.debug = debug if not sock: self.s = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) self.s.connect( ( host, int(port) ) ) else: self.s = sock self.sd = self.s.makefile( 'rw', 0 ) #-------------------------------------------------------------------------- def __makemsg( self, msgtype, msgdata ): #-------------------------------------------------------------------------- msglen = len(msgdata) msg = struct.pack( "!4sL%ds" % msglen, msgtype, msglen, msgdata ) return msg #-------------------------------------------------------------------------- def __debug( self, msg ): #-------------------------------------------------------------------------- if self.debug: btdebug( msg ) #-------------------------------------------------------------------------- def senddata( self, msgtype, msgdata ): #-------------------------------------------------------------------------- """ senddata( message type, message data ) -> boolean status Send a message through a peer connection. Returns True on success or False if there was an error. """ try: msg = self.__makemsg( msgtype, msgdata ) self.sd.write( msg ) self.sd.flush() except KeyboardInterrupt: raise except: if self.debug: traceback.print_exc() return False return True #-------------------------------------------------------------------------- def recvdata( self ): #-------------------------------------------------------------------------- """ recvdata() -> (msgtype, msgdata) Receive a message from a peer connection. Returns (None, None) if there was any error. """ try: msgtype = self.sd.read( 4 ) if not msgtype: return (None, None) lenstr = self.sd.read( 4 ) msglen = int(struct.unpack( "!L", lenstr )[0]) msg = "" while len(msg) != msglen: data = self.sd.read( min(2048, msglen - len(msg)) ) if not len(data): break msg += data if len(msg) != msglen: return (None, None) except KeyboardInterrupt: raise except: if self.debug: traceback.print_exc() return (None, None) return ( msgtype, msg ) # end recvdata method #-------------------------------------------------------------------------- def close( self ): #-------------------------------------------------------------------------- """ close() Close the peer connection. The send and recv methods will not work after this call. """ self.s.close() self.s = None self.sd = None #-------------------------------------------------------------------------- def __str__( self ): #-------------------------------------------------------------------------- return "|%s|" % peerid -------------- next part -------------- import threading import btpeer def test(): print("Test received!") #end def class peer(btpeer.BTPeer): def __init__(self, *a, **w): btpeer.BTPeer.__init__(self, *a, **w) #end def port=5321 me=peer(0, port) me.debug=1 me.handlers={"test":test} name=raw_input("Enter your name: ") me.setmyid(name) print("Your external IP is "+str(me.serverhost)+". Give that to your opponent. We will now try to connect to your opponent.") name=raw_input("What name is your opponent using? (CAPS MATTER!) ") ip=raw_input("Enter the IP of your opponent: ") me.addpeer(name, ip, port) print("connecting to "+name+"...") op=me.getpeer(name) conn=btpeer.BTPeerConnection(name, op[0], op[1]) me.mainloop() #wait for incoming connections, maybe? No docs for lib! print("Sending test string...") conn.senddata("test", "Testing connection.") From steve at pearwood.info Fri Oct 15 04:57:33 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 15 Oct 2010 13:57:33 +1100 Subject: [Tutor] trying it again: p2p for game In-Reply-To: References: Message-ID: <201010151357.33545.steve@pearwood.info> On Fri, 15 Oct 2010 01:30:56 pm Alex Hall wrote: > Hi all, > Alright: after a few months letting it rest, I am trying this > Battleship thing again. I found a p2p library (attached) and have > tried to implement it (p2p.py). However, I am always getting the same > error: errno2: no connection could be made because the target machine > actively refused it. None of this is a Python problem. You could be using any language. > I suspect my ip addresses are at fault. I am on a public network here > at school, and I keep getting 130... addresses. However, my friend > several cities away cannot use 130, since that is local, right? I am > not sure where to get my ip; that is, where to get an ip that, > coupled with the hard-coded port number, will let my computer connect > to his and vice versa. http://whatismyipaddress.com/ Also, be prepared to diagnose firewall issues next. If you're trying to connect from a computer in one school to a computer in another school, both firewalls will almost certainly try to stop you. -- Steven D'Aprano From mehgcap at gmail.com Fri Oct 15 05:55:27 2010 From: mehgcap at gmail.com (Alex Hall) Date: Thu, 14 Oct 2010 23:55:27 -0400 Subject: [Tutor] trying it again: p2p for game In-Reply-To: <201010151357.33545.steve@pearwood.info> References: <201010151357.33545.steve@pearwood.info> Message-ID: On 10/14/10, Steven D'Aprano wrote: > On Fri, 15 Oct 2010 01:30:56 pm Alex Hall wrote: >> Hi all, >> Alright: after a few months letting it rest, I am trying this >> Battleship thing again. I found a p2p library (attached) and have >> tried to implement it (p2p.py). However, I am always getting the same >> error: errno2: no connection could be made because the target machine >> actively refused it. > > None of this is a Python problem. You could be using any language. > >> I suspect my ip addresses are at fault. I am on a public network here >> at school, and I keep getting 130... addresses. However, my friend >> several cities away cannot use 130, since that is local, right? I am >> not sure where to get my ip; that is, where to get an ip that, >> coupled with the hard-coded port number, will let my computer connect >> to his and vice versa. > > http://whatismyipaddress.com/ This, too, reports a 130... address and places me in the central hub for all state university traffic, not my own city. I am not surprised by this inaccuracy. > > Also, be prepared to diagnose firewall issues next. If you're trying to > connect from a computer in one school to a computer in another school, > both firewalls will almost certainly try to stop you. Hmmm... I hate firewalls. :) Still, it failed on my friend's network when he (said he had) disabled all firewalls and other potential problem programs for the network... > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From evert.rol at gmail.com Fri Oct 15 07:06:38 2010 From: evert.rol at gmail.com (Evert Rol) Date: Fri, 15 Oct 2010 07:06:38 +0200 Subject: [Tutor] Networking In-Reply-To: References: <4CA788E2.4060006@gmail.com> <33D037A9-0230-42B3-9AFC-A1884F00AE88@gmail.com> <4CA90FAF.1070002@gmail.com> <4CB3A1A7.9080804@gmail.com> <4CB7ACEB.1050405@gmail.com> Message-ID: <9951F29F-8C2D-4A3F-8917-7C9A4C001051@gmail.com> >>> But what if I want it to serve one client, go to another and then go back. >>> How does that work? > > You do some I/O multi-plexing or multi-processing/threading. > > You might want to do some reading on this. The very last example on http://docs.python.org/library/socketserver.html may help you. Or perhaps the asyncore/asynchat modules. Evert From songbird42371 at gmail.com Fri Oct 15 06:09:57 2010 From: songbird42371 at gmail.com (Colleen Glaeser) Date: Thu, 14 Oct 2010 23:09:57 -0500 Subject: [Tutor] Statistic-Program Problems! Please Help Quickly! Message-ID: Dear tutors, I am in a beginning-level computer science class in college and am running into problems with an assignment. The assignment is as follows: Statisticians are fond of drawing regression lines. In statistics and other fields where people analyze lots of data, one of the most commonly used regression lines is called the ?least squares line.? This is the line that is supposed to best fit the set of data points, in the sense that it minimizes the squared vertical distances between the points and the line. Why this should be a good fit is beyond the scope of this assignment. Presume that you have a collection of n two-dimensional data points. I?ll give it as a list of lists, where each of the lists inside represents one data point. Data :[ [x1, y1], [x2, y2], [x3, y3], ?, [xn, yn]] Compute the following The regression line is then given by where m and b may be obtained by and Your task is to compute the m and b (slope and intercept, respectively) for a set of data. You have to analyze the data as given, not count or add anything yourself. Your program should do everything, even figure out how many data points there are. Here?s your data: First set: [ [3, 1], [4, 3], [6, 4], [7, 6], [8, 8], [9, 8] ] Second set: [ [63, 11], [22, 7.5], [63, 11], [28, 10], [151, 12], [108, 10], [18, 8], [115, 10], [31,7], [44, 9] ] Find m and b, then calculate an estimate for x = 5 using the first data set. That is, plug in 5 for x and see what y you get. For the second set, try x = 95. Turn in: code, m, b, and the estimates for both data sets. *********************************************************************************************************************** There?s an easy way to walk through the data and extract the values you need. Use a for loop. Try this: for item in data: [x, y] = item print(x) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ For extra credit: draw a scatter plot of the data, and draw in the least squares line. Scale the window to fit, making it a bit wider and higher than the data requires, so that some of the points are near but not on the edges of the window. Then sketch in the regression line. Note that you should calculate the window size based on the data ? don?t set them yourself; find the max and min values for x and y. You can print the scatter plot, or point me toward your web page. In any case, show me the code. So far, my program is as follows: Data = [[3,1],[4,3],[6, 4],[7, 6],[8, 8],[9, 8]] def X(): accX = 0 for item in Data: [x,y] = item accX = accX + x print (accX) def Y(): accY = 0 for item in Data: [x,y] = item accY = accY + y print (accY) def P(): accXY = 0 for item in Data: [x,y] = item accXY = accXY + (y*x) print (accXY) def Q(): accX2 = 0 for item in Data: [x,y] = item accX2 = accX2 + (x**2) print (accX2) X() Y() P() Q() def B(): ((Y() * Q()) - (P() * X())) / ((6 * Q()) - (X()**2)) def M(): ((Y() * Q()) - (P() * X())) / (X() * Q()) B() M() Now, my functions for X, Y, P, and Q are correct, but I have a couple of problems when it comes to continuing. First of all, despite what my teacher has told me, my method for trying to multiply X,Y,P, and Q's results in the functions for B and M are not working. I'm not sure if there is a way to make functions into variables or how to solve this problem. Second, I am confused as to what my teacher means to do when it comes to inputting different values of x. Find m and b, then calculate an estimate for x = 5 using the first data set. That is, plug in 5 for x and see what y you get. For the second set, try x = 95. Turn in: code, m, b, and the estimates for both data sets. I mean, I know I need to calculate the line of best fit for the data sets using B and M, but what in the world is x supposed to do and where does it go? How do I program this? This is especially harder since I've never taken a proper stat class before. Thank you all so much! -- Colleen Glaeser songbird42371 at gmail.com 636.357.8519 -------------- next part -------------- An HTML attachment was scrubbed... URL: From songbird42371 at gmail.com Fri Oct 15 06:11:39 2010 From: songbird42371 at gmail.com (Colleen Glaeser) Date: Thu, 14 Oct 2010 23:11:39 -0500 Subject: [Tutor] Statistic-Program Problems! Please Help Quickly! In-Reply-To: References: Message-ID: BTW, the error message my program gives me for the B and M functions is: Traceback (most recent call last): File "I:\Lab 7 wierd stat data.py", line 49, in B() File "I:\Lab 7 wierd stat data.py", line 44, in B ((Y() * Q()) - (P() * X())) / ((6 * Q()) - (X()**2)) TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType' On Thu, Oct 14, 2010 at 11:09 PM, Colleen Glaeser wrote: > Dear tutors, > > I am in a beginning-level computer science class in college and am running > into problems with an assignment. > > The assignment is as follows: > > Statisticians are fond of drawing regression lines. In statistics and > other fields where people analyze lots of data, one of the most commonly > used regression lines is called the ?least squares line.? This is the line > that is supposed to best fit the set of data points, in the sense that it > minimizes the squared vertical distances between the points and the line. > Why this should be a good fit is beyond the scope of this assignment. > > Presume that you have a collection of n two-dimensional data points. I?ll > give it as a list of lists, where each of the lists inside represents one > data point. > > Data :[ [x1, y1], [x2, y2], [x3, y3], ?, [xn, yn]] > > Compute the following > > The regression line is then given by > > where m and b may be obtained by > > and > > Your task is to compute the m and b (slope and intercept, respectively) for > a set of data. You have to analyze the data as given, not count or add > anything yourself. Your program should do everything, even figure out how > many data points there are. > > Here?s your data: > > First set: [ [3, 1], [4, 3], [6, 4], [7, 6], [8, 8], [9, 8] ] > > Second set: [ [63, 11], [22, 7.5], [63, 11], [28, 10], [151, 12], [108, > 10], [18, 8], [115, 10], [31,7], [44, 9] ] > > Find m and b, then calculate an estimate for x = 5 using the first data > set. That is, plug in 5 for x and see what y you get. For the second > set, try x = 95. > > Turn in: code, m, b, and the estimates for both data sets. > > > > > *********************************************************************************************************************** > > There?s an easy way to walk through the data and extract the values you > need. Use a for loop. Try this: > > for item in data: > > [x, y] = item > > print(x) > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > For extra credit: draw a scatter plot of the data, and draw in the least > squares line. Scale the window to fit, making it a bit wider and higher > than the data requires, so that some of the points are near but not on the > edges of the window. Then sketch in the regression line. Note that you > should calculate the window size based on the data ? don?t set them > yourself; find the max and min values for x and y. You can print the > scatter plot, or point me toward your web page. In any case, show me the > code. > > > > So far, my program is as follows: > > Data = [[3,1],[4,3],[6, 4],[7, 6],[8, 8],[9, 8]] > > def X(): > accX = 0 > for item in Data: > [x,y] = item > > accX = accX + x > print (accX) > > > def Y(): > accY = 0 > for item in Data: > [x,y] = item > > accY = accY + y > print (accY) > > def P(): > accXY = 0 > for item in Data: > [x,y] = item > > accXY = accXY + (y*x) > print (accXY) > > def Q(): > accX2 = 0 > for item in Data: > [x,y] = item > > accX2 = accX2 + (x**2) > print (accX2) > > X() > Y() > P() > Q() > > > > def B(): > ((Y() * Q()) - (P() * X())) / ((6 * Q()) - (X()**2)) > > def M(): > ((Y() * Q()) - (P() * X())) / (X() * Q()) > > B() > M() > > Now, my functions for X, Y, P, and Q are correct, but I have a couple of > problems when it comes to continuing. First of all, despite what my teacher > has told me, my method for trying to multiply X,Y,P, and Q's results in the > functions for B and M are not working. I'm not sure if there is a way to > make functions into variables or how to solve this problem. > > Second, I am confused as to what my teacher means to do when it comes to > inputting different values of x. > > Find m and b, then calculate an estimate for x = 5 using the first data > set. That is, plug in 5 for x and see what y you get. For the second > set, try x = 95. > > Turn in: code, m, b, and the estimates for both data sets. > > > I mean, I know I need to calculate the line of best fit for the data sets > using B and M, but what in the world is x supposed to do and where does it > go? How do I program this? This is especially harder since I've never > taken a proper stat class before. > > Thank you all so much! > > -- > Colleen Glaeser > songbird42371 at gmail.com > 636.357.8519 > -- Colleen Glaeser songbird42371 at gmail.com 636.357.8519 -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Oct 15 08:33:46 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 15 Oct 2010 07:33:46 +0100 Subject: [Tutor] Statistic-Program Problems! Please Help Quickly! References: Message-ID: "Colleen Glaeser" wrote > I am in a beginning-level computer science class in college > and am running into problems with an assignment. Because its homework we can't give you a fiull solution but can give hints so... > The regression line is then given by > > where m and b may be obtained by Thre appears to be something missing? > So far, my program is as follows: > > Data = [[3,1],[4,3],[6, 4],[7, 6],[8, 8],[9, 8]] > > def X(): > accX = 0 > for item in Data: > [x,y] = item > > accX = accX + x > print (accX) Your function has several issues. You don't take any data in or give any data out so the results are stored in local variables (x,y, accX) which are thrown away when the function exits. It also only ever works on the global value Data. It would be better to pass Data in as a parameter and to return the results so you can use them outside the function. Also X() is not a very descriptive name for your function. It will be much easier to figute out what your program does if you use descriptive names. A good name for this one might be accX? > def Y(): All of the above applies here too. Also this is so similar to the previous function that might it be possible to do it with one function that takes a parameter to determine which coordinate to sum? It couldbe caloled sumCoord() and the def would look like: def sumCoord(data, coord): > def P(): > def Q(): And again for these functions. > def B(): > ((Y() * Q()) - (P() * X())) / ((6 * Q()) - (X()**2)) Because your functions don;t return any values the multiplications have no values to multiply. As a general rule its best to keep print statements out of functions (except for debugging) and print the results instead. Thus you could do print X() print B() etc. > functions for B and M are not working. I'm not sure if there is a > way to > make functions into variables or how to solve this problem. You need to get your functions to return their results so that you can store them in variables(or use them directly in your calculation) > Second, I am confused as to what my teacher means to do > when it comes to inputting different values of x. Another problem for another email :-) Take a look at the functions and modules topic of my tutor for a reminder of how to write function. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Fri Oct 15 08:54:08 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 15 Oct 2010 17:54:08 +1100 Subject: [Tutor] Statistic-Program Problems! Please Help Quickly! In-Reply-To: References: Message-ID: <201010151754.09111.steve@pearwood.info> On Fri, 15 Oct 2010 03:09:57 pm Colleen Glaeser wrote: [...] > So far, my program is as follows: > > Data = [[3,1],[4,3],[6, 4],[7, 6],[8, 8],[9, 8]] > > def X(): > accX = 0 > for item in Data: > [x,y] = item > accX = accX + x > print (accX) I see four problems with this function. The first three are minor, stylistic problems. The fourth is the biggy -- it stops your function from working correctly. (1) The name X doesn't mean anything. Functions should have a name that tells the reader what the function does. In this case, I suggest "sumx", since it sums the x values. (2) The function gets its input from a global variable, Data. This isn't *wrong*, exactly, but it's usually inconvenient and leads to trouble later on. It is often better to pass the Data as an argument to the function, especially if you have more than one set of data to use. Example: when you call the "len" function, to get the length of a list, you do this: len(my_list) # pass the list as an argument to the function rather than this: Data = my_list len() # only looks at the global variable Data (3) We can simplify the for-loop a little bit. Instead of this: for item in Data: [x,y] = item ... you can do this: for x,y in Data: ... Since item is not used anywhere else, this saves time and code. (4) This is a real problem. Your function doesn't return a result, it merely prints it to the screen. This means it's impossible to store the result for use later on, or to do calculations with it, or anything like that. This is why you get the error message: TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType' The error is a bit cryptic, but it means that when you try multiplying Y() * Q() later on, neither function returns anything, so Python tries multiplying None and None, which is impossible. Fixing these three minor issues and one major problem, I get: def sumx(data): accumulator = 0 for (x,y) in data: accumulator += x return accumulator To use this, you might do this: my_first_data_set = [ [1,2], [3,4], [5,6] ] my_second_data_set = [ [3,4], [5,6], [100, 0] ] a = sumx(my_first_data_set) b = sumx(my_second_data_set) print "Sum of x from first set:", a print "Sum of x from second set:", b which should print: Sum of x from first set: 9 Sum of x from second set: 108 You should make similar adjustments to Y() and the other functions, and then see how the assignment goes. -- Steven D'Aprano From prologic at shortcircuit.net.au Fri Oct 15 09:04:01 2010 From: prologic at shortcircuit.net.au (James Mills) Date: Fri, 15 Oct 2010 17:04:01 +1000 Subject: [Tutor] Networking In-Reply-To: <9951F29F-8C2D-4A3F-8917-7C9A4C001051@gmail.com> References: <4CA788E2.4060006@gmail.com> <33D037A9-0230-42B3-9AFC-A1884F00AE88@gmail.com> <4CA90FAF.1070002@gmail.com> <4CB3A1A7.9080804@gmail.com> <4CB7ACEB.1050405@gmail.com> <9951F29F-8C2D-4A3F-8917-7C9A4C001051@gmail.com> Message-ID: On Fri, Oct 15, 2010 at 3:06 PM, Evert Rol wrote: > The very last example on http://docs.python.org/library/socketserver.html may help you. > Or perhaps the asyncore/asynchat modules. Alternatively Twisted (1) or circuits (2) cheers James 1. http://twistedmatrix.com/trac/ 2. http://bitbucket.org/prologic/circuits/wiki/Home -- -- James Mills -- -- "Problems are solved by method" From steve at pearwood.info Fri Oct 15 09:15:16 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 15 Oct 2010 18:15:16 +1100 Subject: [Tutor] Statistic-Program Problems! Please Help Quickly! In-Reply-To: References: Message-ID: <201010151815.16692.steve@pearwood.info> On Fri, 15 Oct 2010 03:09:57 pm Colleen Glaeser wrote: > Now, my functions for X, Y, P, and Q are correct, but I have a couple > of problems when it comes to continuing. First of all, despite what > my teacher has told me, my method for trying to multiply X,Y,P, and > Q's results in the functions for B and M are not working. In case it wasn't clear from my earlier email, this is because the functions X, Y, P and Q don't return anything, they just print their result. > I'm not > sure if there is a way to make functions into variables or how to > solve this problem. > > Second, I am confused as to what my teacher means to do when it comes > to inputting different values of x. The formula for a straight line looks like: y = m*x + b where x and y are variables, and m and b are the slope and intercept of the line. m and b are parameters: you set them once, to choose a line, and then calculate with the variables. When you change the parameters, you change the line. You pick m and b (say, m=2 an b=3) to get a formula y = 2*x + 3, and then you can put in different values of x to get different values of y: x 2*x+3 = y ------------ 0 2*0+3 = 3 1 2*1+3 = 5 2 2*2+3 = 7 etc. In this assignment, the values for m and b aren't chosen arbitrarily, but calculated from the data sets using the functions that you called X(), Y(), P() etc. Bringing this back to Python, you first have to calculate your regression parameters m and b: m = ... # something goes here b = ... # something else goes here Then you need to turn them into a function that takes x as an argument and returns y. How to do this? I know earlier I suggested that global variables should be avoided. I stand by that, but in this case I think that here you should use globals. There are alternatives, but they are more advanced techniques, and at this early stage keeping it simple is a good thing. So I might write a function like this: def line(x): return m*x+b and use it like this: y = line(5) # try it with x=5 This returns the value of y that goes with the value of x=5, for the regression line you calculated earlier. Then you move on to the second set of data: m = ... # something goes here b = ... # something else goes here y = line(95) which calculates the y that goes with x=95 for the new regression line. -- Steven D'Aprano From rwobben at hotmail.com Fri Oct 15 09:37:27 2010 From: rwobben at hotmail.com (Roelof Wobben) Date: Fri, 15 Oct 2010 07:37:27 +0000 Subject: [Tutor] join question In-Reply-To: References: , , Message-ID: Hello, Solved it. Problem was that I used fileinfo instead of getinfo. Thanks for the help. Roelof Date: Thu, 14 Oct 2010 18:55:50 -0400 > From: joel.goldstick at gmail.com > To: tutor at python.org > Subject: Re: [Tutor] join question > > > > On Thu, Oct 14, 2010 at 6:43 PM, Alan Gauld > > wrote: > > "Roelof Wobben" > wrote > > > > print ''.join([zf.getinfo('%s.txt' % p).comment for p in zpp]) > > So I thought that this would be the same : > > for p in zpp: > test = zf.getinfo(p).comment > print ''.join(test) > > But it seems not to work > > Can anyone explain why not ? > > Because it's not the same. test in your version is only a single item. > In the original its a list of items. So to write similar code > explicitly you need: > > lst = [] > > for p in zpp: > test = zf.getinfo(p).comment > lst.append(test) > print ''.join(lst) > > HTH, > > Alan G. > > > There are two problems. Alan shows the solution to the joining of each > element of the list, but also you need to have the correct argument for > getinfo. In your first example you append ".txt" to p, but in the loop > example you fail to append it. See my earlier post > > -- > Joel Goldstick > > > _______________________________________________ Tutor maillist - > Tutor at python.org To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From vince at vinces.ca Fri Oct 15 08:22:45 2010 From: vince at vinces.ca (Vince Spicer) Date: Fri, 15 Oct 2010 00:22:45 -0600 Subject: [Tutor] Statistic-Program Problems! Please Help Quickly! In-Reply-To: References: Message-ID: On Thu, Oct 14, 2010 at 10:11 PM, Colleen Glaeser wrote: > BTW, the error message my program gives me for the B and M functions is: > > Traceback (most recent call last): > File "I:\Lab 7 wierd stat data.py", line 49, in > B() > File "I:\Lab 7 wierd stat data.py", line 44, in B > > ((Y() * Q()) - (P() * X())) / ((6 * Q()) - (X()**2)) > TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType' > > On Thu, Oct 14, 2010 at 11:09 PM, Colleen Glaeser > wrote: > >> Dear tutors, >> >> I am in a beginning-level computer science class in college and am running >> into problems with an assignment. >> >> The assignment is as follows: >> >> Statisticians are fond of drawing regression lines. In statistics and >> other fields where people analyze lots of data, one of the most commonly >> used regression lines is called the ?least squares line.? This is the line >> that is supposed to best fit the set of data points, in the sense that it >> minimizes the squared vertical distances between the points and the line. >> Why this should be a good fit is beyond the scope of this assignment. >> >> Presume that you have a collection of n two-dimensional data points. I?ll >> give it as a list of lists, where each of the lists inside represents one >> data point. >> >> Data :[ [x1, y1], [x2, y2], [x3, y3], ?, [xn, yn]] >> >> Compute the following >> >> The regression line is then given by >> >> where m and b may be obtained by >> >> and >> >> Your task is to compute the m and b (slope and intercept, respectively) >> for a set of data. You have to analyze the data as given, not count or >> add anything yourself. Your program should do everything, even figure >> out how many data points there are. >> >> Here?s your data: >> >> First set: [ [3, 1], [4, 3], [6, 4], [7, 6], [8, 8], [9, 8] ] >> >> Second set: [ [63, 11], [22, 7.5], [63, 11], [28, 10], [151, 12], [108, >> 10], [18, 8], [115, 10], [31,7], [44, 9] ] >> >> Find m and b, then calculate an estimate for x = 5 using the first data >> set. That is, plug in 5 for x and see what y you get. For the second >> set, try x = 95. >> >> Turn in: code, m, b, and the estimates for both data sets. >> >> >> >> >> *********************************************************************************************************************** >> >> There?s an easy way to walk through the data and extract the values you >> need. Use a for loop. Try this: >> >> for item in data: >> >> [x, y] = item >> >> print(x) >> >> >> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >> >> For extra credit: draw a scatter plot of the data, and draw in the least >> squares line. Scale the window to fit, making it a bit wider and higher >> than the data requires, so that some of the points are near but not on the >> edges of the window. Then sketch in the regression line. Note that you >> should calculate the window size based on the data ? don?t set them >> yourself; find the max and min values for x and y. You can print the >> scatter plot, or point me toward your web page. In any case, show me the >> code. >> >> >> >> So far, my program is as follows: >> >> Data = [[3,1],[4,3],[6, 4],[7, 6],[8, 8],[9, 8]] >> >> def X(): >> accX = 0 >> for item in Data: >> [x,y] = item >> >> accX = accX + x >> print (accX) >> >> >> def Y(): >> accY = 0 >> for item in Data: >> [x,y] = item >> >> accY = accY + y >> print (accY) >> >> def P(): >> accXY = 0 >> for item in Data: >> [x,y] = item >> >> accXY = accXY + (y*x) >> print (accXY) >> >> def Q(): >> accX2 = 0 >> for item in Data: >> [x,y] = item >> >> accX2 = accX2 + (x**2) >> print (accX2) >> >> X() >> Y() >> P() >> Q() >> >> >> >> def B(): >> ((Y() * Q()) - (P() * X())) / ((6 * Q()) - (X()**2)) >> >> def M(): >> ((Y() * Q()) - (P() * X())) / (X() * Q()) >> >> B() >> M() >> >> Now, my functions for X, Y, P, and Q are correct, but I have a couple of >> problems when it comes to continuing. First of all, despite what my teacher >> has told me, my method for trying to multiply X,Y,P, and Q's results in the >> functions for B and M are not working. I'm not sure if there is a way to >> make functions into variables or how to solve this problem. >> >> Second, I am confused as to what my teacher means to do when it comes to >> inputting different values of x. >> >> Find m and b, then calculate an estimate for x = 5 using the first data >> set. That is, plug in 5 for x and see what y you get. For the second >> set, try x = 95. >> >> Turn in: code, m, b, and the estimates for both data sets. >> >> >> I mean, I know I need to calculate the line of best fit for the data sets >> using B and M, but what in the world is x supposed to do and where does it >> go? How do I program this? This is especially harder since I've never >> taken a proper stat class before. >> >> Thank you all so much! >> >> -- >> Colleen Glaeser >> songbird42371 at gmail.com >> 636.357.8519 >> > > > > -- > Colleen Glaeser > songbird42371 at gmail.com > 636.357.8519 > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > You might want to look that returning data http://docs.python.org/library/functions.html -- Vince -- Sent from Ubuntu -------------- next part -------------- An HTML attachment was scrubbed... URL: From smokefloat at gmail.com Fri Oct 15 12:26:48 2010 From: smokefloat at gmail.com (David Hutto) Date: Fri, 15 Oct 2010 06:26:48 -0400 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: <201010151327.31512.steve@pearwood.info> References: <201010151327.31512.steve@pearwood.info> Message-ID: Ok, Let me restate and hopefully further clarify. 1. I have a field for a wxpython app using matplotlib to display 2. I have a sqlite3 db which I'm retrieving information from 3. The sqlitle data is returned as unicode: u'field' 4. The portion of the matplotlib code is filled in, in a for x in y: 5. in plot(self.plot), self.plot is the variable I'm using from the unicoded db field comes in from sqlite as u'[1,2,3,4]', which places a string in quotes in that variables place: plot(u'[1,2,3,4]') 6. the plot(eval(self.plot)), changes the variable from the u'[1,2,3,4]' to just [1,2,3,4] 7 As stated somewhere above, the float error has nothing to do with the probel, only the fact that it was used as if I had placed '' around the necessary data from the db field. 8. If anyone has a way better than eval to convert the u'field' when replacing a variable so that self.plot = [1,2,3,4] instead of self.plot = u'[1,2,3,4]' Let me know, meanwhile I'll be reviewing the replies more thoroughly, now that I've had a nap. Thanks, David From smokefloat at gmail.com Fri Oct 15 13:52:49 2010 From: smokefloat at gmail.com (David Hutto) Date: Fri, 15 Oct 2010 07:52:49 -0400 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: Message-ID: On Thu, Oct 14, 2010 at 3:02 PM, Sander Sweers wrote: > On 14 October 2010 20:29, David Hutto wrote: >> Actually, I needed it to be converted to something without a string >> attached to it. See a post above, and it was fixed by eval(), > > Using eval is a big security risk and is generally not recommended for > any production code. What do you think eval() return for your string? > A tuple of ints which for your use case serves the same purpose as > using the list comprehension. > > If you really want (you really don't) to use eval() then at least use > the safe one from the ast mode. > >>>> from ast import literal_eval >>>> literal_eval(u'1,2,3,4') > (1, 2, 3, 4) >>>> eval(u'1,2,3,4') > (1, 2, 3, 4) > > As you can see for your use case both return a tuple of ints. > > Greets > Sander > After reading http://bugs.python.org/issue7935, I'll probably use literal_eval. Thanks for pointing literal out. From smokefloat at gmail.com Fri Oct 15 13:53:07 2010 From: smokefloat at gmail.com (David Hutto) Date: Fri, 15 Oct 2010 07:53:07 -0400 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: <4CB752F0.6050008@gmail.com> References: <4CB752F0.6050008@gmail.com> Message-ID: On Thu, Oct 14, 2010 at 2:58 PM, Adam Bark wrote: > On 14/10/10 19:29, David Hutto wrote: >> >> On Thu, Oct 14, 2010 at 2:19 PM, Sander Sweers >> ?wrote: >> >>> >>> On 14 October 2010 16:14, David Hutto ?wrote: >>> >>>> >>>> (u'graph1', u'Line', u'222', u'BLUE', u'1,2,3,4', u'True', u'0,5,0,10') >>>> >>>> Which is a tuple of unicode strings. From this I >>>> need to place portions of the tuple into other fields, >>>> but not as unicode strings, but literals no ''. >>>> >>>> For example if I have the following line: >>>> >>>> self.lines = self.newplot.plot([1,2,3,4]) >>>> >>> >>> So you want convert string u'1,2,3,4' to a list of ints [1,2,3,4]? >>> Then the below will work. >>> >>> [int(n) for n in u'1,2,3,4'.replace(',', '')] >>> >>> >> >> Actually, I needed it to be converted to something without a string >> attached to it. See a post above, and it was fixed by eval(), >> >> Thanks though. And I'm sure at some point this morning in a moment of >> frustration rather than logic, I tried your approach. >> > > What do you mean by "without a string attached to it"? > Also using eval could be dangerous unless you're sure the stuff coming out > of your dbase is safe. > At this point I create the db 'manually', so the dat in their is what I put in. At a later time I'll use other db's, so I guess it's better to make sure it works under all circumstances. From smokefloat at gmail.com Fri Oct 15 13:53:21 2010 From: smokefloat at gmail.com (David Hutto) Date: Fri, 15 Oct 2010 07:53:21 -0400 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: <4CB75C79.6050409@gmail.com> References: <4CB752F0.6050008@gmail.com> <4CB758F1.6050203@gmail.com> <4CB75C79.6050409@gmail.com> Message-ID: > In which case the code Sanders sent you is a much more sensible way of > recovering your data. Evalling the string u'plot' still doesn't make much > sense to me though. But it's the only thing working correctly though, so until I have a better solution, I'm forced to deal with any bugs in interpreting the data coming in that may occur, and account for those after the fact for now. I think I may have been overestimating the quality of > your code. > Probably so. You should see the whole thing, I'll let yall tear that apart later though =) From smokefloat at gmail.com Fri Oct 15 13:53:37 2010 From: smokefloat at gmail.com (David Hutto) Date: Fri, 15 Oct 2010 07:53:37 -0400 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: <4CB752F0.6050008@gmail.com> <4CB758F1.6050203@gmail.com> Message-ID: On Thu, Oct 14, 2010 at 6:56 PM, Alan Gauld wrote: > > "David Hutto" wrote > >> In other words I needed (1,2,3,4) not u'(1,2,3,4)' to be inserted >> for variable self.plot > > You appear to be making this much more difficult than it needs to be. > The values you retrieve from the database are strings (forget about > the unicode aspect its not really relevant here) just as if you had > used raw_input to read them from the user. > > How would you convert a string received from raw_input() to a series > of numbers? Would you have used eval() or just split the string and > called int() or float()? Something like: > > inp = raw_input("Enter a string of numbers separated by commas") > nums = [int(n) for n in inp.split(',')] > > eval() should always be considered a last resort because of the > potential for causing damage and the extremely obscure errors > it can throw up with badly formed input. > > I think you are letting the unicode representation spook you into doing > things in a way you wouldn't if it was normal raw_input you were using. Not spook, but I couldn't think of another way of placing it into a variable for a function within the script, without it using the literal value from the db, (without generating a blank.py file, and rewriting the whole class each time for the changes in the value), and use it in the code without it being a literally evaluated u'string' the way it came from the db > > HTH, > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From smokefloat at gmail.com Fri Oct 15 13:53:50 2010 From: smokefloat at gmail.com (David Hutto) Date: Fri, 15 Oct 2010 07:53:50 -0400 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: Message-ID: On Thu, Oct 14, 2010 at 7:00 PM, Alan Gauld wrote: > > "David Hutto" wrote > >> it's not necessary to worry about insertion of data other than my own >> inputs. > > But can you be sure that you won't accidentally mistype something > that eval can read as valid code but that does something unexpected > - even if its only throw an apparently bizarre error dump at you... probably not, because I haven't had much experience with using eval. > > Relying on eval() reading your database content as if it were Python > code - which is what it does - is a risky strategy. > > But, heh, it's your data, your project, so as long as you understand > the risks then it's your decision. :-) It's not that I want faulty data, it was the only thing thus far I'd tried that worked under the generalized circumstances I'm testing it under. > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From smokefloat at gmail.com Fri Oct 15 13:54:05 2010 From: smokefloat at gmail.com (David Hutto) Date: Fri, 15 Oct 2010 07:54:05 -0400 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: <201010151309.37352.steve@pearwood.info> References: <201010151309.37352.steve@pearwood.info> Message-ID: On Thu, Oct 14, 2010 at 10:09 PM, Steven D'Aprano wrote: > On Fri, 15 Oct 2010 04:43:46 am David Hutto wrote: > >> Fixed with: >> >> self.lines = self.newplot.plot(eval(self.plot)) > > Anytime you use eval, chances are that it isn't fixed at all, but just > badly and dangerously papered over. > > I really, really wish that eval and exec weren't built-ins. They're > powerful but dangerous and slow, and making them built-ins just > encourages newbies to use them inappropriately. If inappropriately means that it works as expected:). But seriously, I haven't found another way, than in thought, about what alan said about raw_input-running it through their instead, but I haven't tried yet this morning. > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From joel.goldstick at gmail.com Fri Oct 15 13:55:28 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Fri, 15 Oct 2010 07:55:28 -0400 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: <201010151327.31512.steve@pearwood.info> Message-ID: On Fri, Oct 15, 2010 at 6:26 AM, David Hutto wrote: > Ok, Let me restate and hopefully further clarify. > > 1. I have a field for a wxpython app using matplotlib to display > > 2. I have a sqlite3 db which I'm retrieving information from > > 3. The sqlitle data is returned as unicode: u'field' > > 4. The portion of the matplotlib code is filled in, in a for x in y: > > 5. in plot(self.plot), self.plot is the variable I'm using from the > unicoded db > field comes in from sqlite as u'[1,2,3,4]', which places a string in quotes > in > that variables place: > > plot(u'[1,2,3,4]') > > 6. the plot(eval(self.plot)), changes the variable from the u'[1,2,3,4]' > to just [1,2,3,4] > > 7 As stated somewhere above, the float error has nothing to do with > the probel, only the fact that it was used as if I had placed '' > around the necessary data from the db field. > > 8. If anyone has a way better than eval to convert the u'field' when > replacing a variable so that > > self.plot = [1,2,3,4] > > instead of > > self.plot = u'[1,2,3,4]' > > > Let me know, meanwhile I'll be reviewing the replies more thoroughly, > now that I've had a nap. > > > Thanks, > David > This will do it > So you want convert string u'1,2,3,4' to a list of ints [1,2,3,4]? > Then the below will work. > > [int(n) for n in u'1,2,3,4'.replace(',', '')] > > Greets > Sander > or this: >>> s = '1, 200 , -3,4' # or whatever >>> [int(x) for x in s.split(',')] [1, 200, -3, 4] or this: To take a string of comma separated integers and convert to a list of ints: >>> x = u'1,2,3,4' >>> y = x.split(',') >>> z = [int(f) for f in y] >>> z [1.0, 2.0, 3.0, 4.0] >>> -- Joel Goldstick You can forget about the u' in front. Its a directive to the interpreter that the following string is in unicode. In your case u'1,2,3,4' is the same as '1,2,3,4'. All of the above examples do this: 1. split the string up into 4 different strings in a tuple by removing the commas :: ('1', '2', '3', '4') 2. convert the individual string vales to integers and put them in a list :: [1, 2, 3, 4] -- Steven D'Aprano Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Fri Oct 15 14:36:07 2010 From: __peter__ at web.de (Peter Otten) Date: Fri, 15 Oct 2010 14:36:07 +0200 Subject: [Tutor] Converting from unicode to nonstring References: Message-ID: David Hutto wrote: > Hey Buddy Pals, ? > I receive the following output from a sqlite db > > (u'graph1', u'Line', u'222', u'BLUE', u'1,2,3,4', u'True', u'0,5,0,10') How did the string u"1,2,3,4" get into the database in the first place? The sqlite3 module offers a mechanism to convert data from and to Python (semi-)transparently: import sqlite3 import json sqlite3.register_adapter(list, json.dumps) sqlite3.register_converter("list", json.loads) db = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES) cursor = db.cursor() cursor.execute("create table data (value list)") cursor.execute("insert into data values (?)", ([11,22,33],)) for row in cursor.execute("select value from data"): print row See also: http://docs.python.org/library/sqlite3.html#converting-sqlite-values-to- custom-python-types Peter From davea at ieee.org Fri Oct 15 14:51:15 2010 From: davea at ieee.org (Dave Angel) Date: Fri, 15 Oct 2010 08:51:15 -0400 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: <201010151327.31512.steve@pearwood.info> Message-ID: <4CB84E43.6090906@ieee.org> On 2:59 PM, David Hutto wrote: > Ok, Let me restate and hopefully further clarify. > > 1. I have a field for a wxpython app using matplotlib to display > > 2. I have a sqlite3 db which I'm retrieving information from > > 3. The sqlitle data is returned as unicode: u'field' > > 4. The portion of the matplotlib code is filled in, in a for x in y: > > 5. in plot(self.plot), self.plot is the variable I'm using from the unicoded db > field comes in from sqlite as u'[1,2,3,4]', which places a string in quotes in > that variables place: > > plot(u'[1,2,3,4]') > > Your point #5 shows that you still have the wrong handle on what a literal string is. When you write in code mystring = u"abc" The u and the quote symbols are *not* in "the variables place". The "variables place" contains a count, three characters, and some meta information. The time you need the quotes are when you want to specify those three characters in your source code. The time you get the quotes back is when you use repr(mystring) to display it. If you use print mystring you don't see any quotes or 'u', nor do you when you call the function str(mystring). So I'll try to explain some terminology. When you run the source code mystring = u"abc" the interpreter (with help from the compiler) builds an object of type string, with contents of those three letters. That object is bound to the name mystring. Nowhere is a u or a quote to be found. Now, the question is how to turn that string object into a list, since apparently plot() is looking for a list. Judging from the error message, it may also be happy to take a float. So it'd be good to find out just what types of objects it's willing to accept. One of the first respondents on the thread showed how to convert this particular string into a list, but he made the implicit assumption that the values in the list were single-digit integer values. I haven't seen you ever define what the possible values are. But guessing that they are supposed to be a list containing a nonzero number of floats, separated by commas, you could do something like (untested): result = [float(strvalue) for strvalue in mystring.split(",")] or, more straightforward: strvalues = mystring.split(",") # get a list of strings, split by the comma result = [] for strvalue in strvalues: result.append(float(strvalue)) #convert to float Notice, nothing about quotes, or unicode anywhere in the logic. DaveA From steve at pearwood.info Fri Oct 15 15:37:57 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 16 Oct 2010 00:37:57 +1100 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: References: <201010151327.31512.steve@pearwood.info> Message-ID: <201010160037.57958.steve@pearwood.info> On Fri, 15 Oct 2010 09:26:48 pm David Hutto wrote: > Ok, Let me restate and hopefully further clarify. > > 1. I have a field for a wxpython app using matplotlib to display > 2. I have a sqlite3 db which I'm retrieving information from Both of those points are irrelevant. > 3. The sqlitle data is returned as unicode: u'field' Semi-relevant. What's important is that you have data as strings. It could be coming from a text file: data = open("my data.txt").read() or from the user: data = raw_input("Enter some data: ") or any other source that gives a string. It makes no difference where it comes from, the only thing that is important is that it is a string. > 4. The portion of the matplotlib code is filled in, in a for x in y: I don't really understand this sentence. What do you mean, portion of code is filled in? But in any case, it's irrelevant that you are doing something in a for loop. For loop, while loop, using the data once only, who cares? > 5. in plot(self.plot), self.plot is the variable I'm using from the > unicoded db field comes in from sqlite as u'[1,2,3,4]', which places > a string in quotes in that variables place: > > plot(u'[1,2,3,4]') Your sentence above is a bit convoluted, but I *think* this is what you're trying to say: - you take the string (which originally comes from the database, but it doesn't matter where it comes from) and store it in self.plot; - you pass self.plot to a function plot(); and - since self.plot is a string, you are passing a string to the function plot(). Presumably this is a problem, because plot() expects a list of ints, not a string. > 6. the plot(eval(self.plot)), changes the variable from the > u'[1,2,3,4]' to just [1,2,3,4] Well, yes it does, so long as the string is perfectly formed, and so long as it doesn't contain anything unexpected. It is also slow, and unnecessary, and a bad habit to get into unless you know exactly what you are doing. > 7 As stated somewhere above, the float error has nothing to do with > the probel, only the fact that it was used as if I had placed '' > around the necessary data from the db field. Huh? > 8. If anyone has a way better than eval to convert the u'field' when > replacing a variable so that > > self.plot = [1,2,3,4] > > instead of > > self.plot = u'[1,2,3,4]' And now, finally, after a dozen or more emails and 8 points (most of which are irrelevant!) we finally come to the real problem: "I have a unicode string that looks like u'[1,2,3,4]' and I want to convert it into a list [1, 2, 3, 4]. How do I do that?" There are at least three solutions to this: (1) eval. The benefit of eval is that it is a built-in, so you don't have to do any programming. The downsides are that: - it is very powerful, so it can do too much; - it is dangerous if you can't trust the source of the data; - because it does so much, it's a little slow; - because it does so much, it will happily accept data that *should* give you an error; - on the other hand, it's also quite finicky about what it does accept, and when it fails, the error messages may be cryptic. (2) ast.literal_eval. The benefit of this is that it is a standard library function starting in Python 2.6, so all you need do is import the ast module. The downsides are: - it is very powerful, so it can do too much; - because it does so much, it will happily accept data that *should* give you an error; - on the other hand, it's also quite finicky about what it does accept, and when it fails, the error messages may be cryptic. (3) Write your own converter. The benefit of this is that you can make it as flexible or as finicky as you like. The downside is that you have to write it. But that's not actually very hard, and we can make sure that we get a nice error message in the event of a problem: def str_to_list(s): """Convert a string that looks like a list to a list of ints.""" s = s.strip() # ignore leading and trailing spaces if not (s.startswith("[") and s.endswith("]")): raise ValueError("string does not look like a list") s = s[1:-1] # throw away the [ and ] s = s.replace(",", " ") result = [] try: for word in s.split(): result.append(int(word)) except ValueError: raise ValueError("item `%s` does not look like an int" % word) return result >>> str_to_list(u' [1 , 2, 3, 15 , -3, 26,1, 7 ] ') [1, 2, 3, 15, -3, 26, 1, 7] If you pass it faulty data, it gives you a nice error message: >>> str_to_list( u'{1:2}') Traceback (most recent call last): File "", line 1, in File "", line 5, in str_to_list ValueError: string does not look like a list >>> str_to_list(u'[1,2,3,None]') Traceback (most recent call last): File "", line 1, in File "", line 13, in str_to_list ValueError: item `None` does not look like an int Compared to eval and ast.literal_eval, both of which do too much: >>> eval(u'{1:2}') {1: 2} >>> eval(u'[1,2,3,None]') [1, 2, 3, None] >>> ast.literal_eval(u'{1:2}') {1: 2} >>> ast.literal_eval(u'[1,2,3,None]') [1, 2, 3, None] -- Steven D'Aprano From ahmedn82 at hotmail.com Fri Oct 15 18:42:42 2010 From: ahmedn82 at hotmail.com (Ahmed AL-Masri) Date: Sat, 16 Oct 2010 00:42:42 +0800 Subject: [Tutor] Converting from unicode to nonstring In-Reply-To: <201010160037.57958.steve@pearwood.info> References: <201010151327.31512.steve@pearwood.info> <201010160037.57958.steve@pearwood.info> Message-ID: hi I faced this problem before and people of tutor helped me to solve it by only one line ex. a=input_raw("your non string")## I input 1,2,3,4 5 6 7 or u[1,2,3,4] a.replace(",", " ","[","]","u").split() ## you can put any kind of unicode that you expect inside the " " a=map(float, a) print a >> [1,2,3,4,5,6,7] -------------------------------------------------- From: "Steven D'Aprano" Sent: Friday, October 15, 2010 9:37 PM To: "Python Tutor" Subject: Re: [Tutor] Converting from unicode to nonstring > On Fri, 15 Oct 2010 09:26:48 pm David Hutto wrote: >> Ok, Let me restate and hopefully further clarify. >> >> 1. I have a field for a wxpython app using matplotlib to display >> 2. I have a sqlite3 db which I'm retrieving information from > > Both of those points are irrelevant. > > >> 3. The sqlitle data is returned as unicode: u'field' > > Semi-relevant. What's important is that you have data as strings. It > could be coming from a text file: > > data = open("my data.txt").read() > > or from the user: > > data = raw_input("Enter some data: ") > > or any other source that gives a string. It makes no difference where it > comes from, the only thing that is important is that it is a string. > > >> 4. The portion of the matplotlib code is filled in, in a for x in y: > > I don't really understand this sentence. What do you mean, portion of > code is filled in? > > But in any case, it's irrelevant that you are doing something in a for > loop. For loop, while loop, using the data once only, who cares? > > >> 5. in plot(self.plot), self.plot is the variable I'm using from the >> unicoded db field comes in from sqlite as u'[1,2,3,4]', which places >> a string in quotes in that variables place: >> >> plot(u'[1,2,3,4]') > > Your sentence above is a bit convoluted, but I *think* this is what > you're trying to say: > > - you take the string (which originally comes from the database, but it > doesn't matter where it comes from) and store it in self.plot; > > - you pass self.plot to a function plot(); and > > - since self.plot is a string, you are passing a string to the function > plot(). > > Presumably this is a problem, because plot() expects a list of ints, not > a string. > > >> 6. the plot(eval(self.plot)), changes the variable from the >> u'[1,2,3,4]' to just [1,2,3,4] > > Well, yes it does, so long as the string is perfectly formed, and so > long as it doesn't contain anything unexpected. > > It is also slow, and unnecessary, and a bad habit to get into unless you > know exactly what you are doing. > > >> 7 As stated somewhere above, the float error has nothing to do with >> the probel, only the fact that it was used as if I had placed '' >> around the necessary data from the db field. > > Huh? > > >> 8. If anyone has a way better than eval to convert the u'field' when >> replacing a variable so that >> >> self.plot = [1,2,3,4] >> >> instead of >> >> self.plot = u'[1,2,3,4]' > > And now, finally, after a dozen or more emails and 8 points (most of > which are irrelevant!) we finally come to the real problem: > > "I have a unicode string that looks like u'[1,2,3,4]' and I want to > convert it into a list [1, 2, 3, 4]. How do I do that?" > > There are at least three solutions to this: > > (1) eval. The benefit of eval is that it is a built-in, so you don't > have to do any programming. The downsides are that: > > - it is very powerful, so it can do too much; > - it is dangerous if you can't trust the source of the data; > - because it does so much, it's a little slow; > - because it does so much, it will happily accept data that *should* > give you an error; > - on the other hand, it's also quite finicky about what it does accept, > and when it fails, the error messages may be cryptic. > > > (2) ast.literal_eval. The benefit of this is that it is a standard > library function starting in Python 2.6, so all you need do is import > the ast module. The downsides are: > > - it is very powerful, so it can do too much; > - because it does so much, it will happily accept data that *should* > give you an error; > - on the other hand, it's also quite finicky about what it does accept, > and when it fails, the error messages may be cryptic. > > > (3) Write your own converter. The benefit of this is that you can make > it as flexible or as finicky as you like. The downside is that you have > to write it. But that's not actually very hard, and we can make sure > that we get a nice error message in the event of a problem: > > > def str_to_list(s): > """Convert a string that looks like a list to a list of ints.""" > s = s.strip() # ignore leading and trailing spaces > if not (s.startswith("[") and s.endswith("]")): > raise ValueError("string does not look like a list") > s = s[1:-1] # throw away the [ and ] > s = s.replace(",", " ") > result = [] > try: > for word in s.split(): > result.append(int(word)) > except ValueError: > raise ValueError("item `%s` does not look like an int" % word) > return result > > >>>> str_to_list(u' [1 , 2, 3, 15 , -3, 26,1, 7 ] ') > [1, 2, 3, 15, -3, 26, 1, 7] > > > If you pass it faulty data, it gives you a nice error message: > >>>> str_to_list( u'{1:2}') > Traceback (most recent call last): > File "", line 1, in > File "", line 5, in str_to_list > ValueError: string does not look like a list > >>>> str_to_list(u'[1,2,3,None]') > Traceback (most recent call last): > File "", line 1, in > File "", line 13, in str_to_list > ValueError: item `None` does not look like an int > > > Compared to eval and ast.literal_eval, both of which do too much: > >>>> eval(u'{1:2}') > {1: 2} >>>> eval(u'[1,2,3,None]') > [1, 2, 3, None] > >>>> ast.literal_eval(u'{1:2}') > {1: 2} >>>> ast.literal_eval(u'[1,2,3,None]') > [1, 2, 3, None] > > > -- > Steven D'Aprano > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From tim at johnsons-web.com Fri Oct 15 23:20:37 2010 From: tim at johnsons-web.com (Tim Johnson) Date: Fri, 15 Oct 2010 13:20:37 -0800 Subject: [Tutor] 'module' object has no attribute (setting a class attribute) Message-ID: <20101015212037.GD30422@johnsons-web.com> My intention is to set a class attribute so that any number of instantiations will have this value. the module is tmpl.py. the class is tmpl. if from the script I do this: import tmpl tmpl.tmpl.templatepath = kbLib.templatepath I get error message: 'module' object has no attribute 'templatepath' I ended up getting completely befuddled, and then realized that the problem was the right side of the assignment statement. I.E. I had a typo on the right and python was not informing me of *which* module didn't have the attribute I have two questions regarding this: 1)Am I using the correct method to set a class attribute? 2)Is there a system configuration that would cause the AttributeError exception to print out the module name. (In this cause it was 'kbLib' not 'tmpl'. thanks -- Tim tim at johnsons-web.com or akwebsoft.com http://www.akwebsoft.com From paulanon at gmail.com Fri Oct 15 23:29:31 2010 From: paulanon at gmail.com (Paul) Date: Fri, 15 Oct 2010 14:29:31 -0700 Subject: [Tutor] Moving my C++ code to Python? Message-ID: Hi there, I've been using C/C++ for many years (python, just reading about it). I have a software written in C/C++ but considering porting most of it to python, as it seems like it's a better choice for decision making portion of the code. I'm also thinking about having a 'matlab' like interface for reading, processing, and writing. In my current C++ code, I would read data into a vector of structs (which contains other simple vectors, strings, and structs) and it can be as large as 500MB to 2GB. The data gets processed (requires random access). The result is then written out. I would like to make modules for python. The problem is that the vector of structs that is very large. First, is it possible to pass such structures around to and from python and C/C++? What would be the overhead cost of having a large structure that needs to be passed to and from the C/C++ modules? # I imagine I'd use the newly written software this way: >>> import c_stuff # my C/C++ module >>> largestuff = c_stuff.read(file) # read from disk >>> c_stuff.process(largestuff, someparams1) # processing, requires random access to the vector >>> c_stuff.process(largestuff, someparams2) ... >>> c_stuff.process(largestuff, someparams10000) # the whole thing may take a few minutes to days >>> >>> import python_stuff # some module written in python to process the data as well >>> >>> python_stuff.process(largestuff, otherparams1) # It's important that this data can be read (and I hope written) by python code >>> python_stuff.process(largestuff, otherparams2) >>> >>> c_stuff.write(largestuff) #write result Thank you in advance, Paul From smokefloat at gmail.com Sat Oct 16 00:07:26 2010 From: smokefloat at gmail.com (David Hutto) Date: Fri, 15 Oct 2010 18:07:26 -0400 Subject: [Tutor] Moving my C++ code to Python? In-Reply-To: References: Message-ID: This isn't an answer to your question, but I'm doing about the same in reverse(it sounds like) as you're doing. More directly just processing data for 2-d/3d graphs using matplotlib and wxpython. So, I was wondering if you think it's similar to those ends, and good enough to let someone else look at yet? From paulanon at gmail.com Sat Oct 16 00:39:52 2010 From: paulanon at gmail.com (Paul) Date: Fri, 15 Oct 2010 15:39:52 -0700 Subject: [Tutor] Moving my C++ code to Python? In-Reply-To: References: Message-ID: On Fri, Oct 15, 2010 at 3:07 PM, David Hutto wrote: > This isn't an answer to your question, but I'm doing about the same in > reverse(it sounds like) as you're doing. More directly just processing > data for 2-d/3d graphs using matplotlib and wxpython. So, I was I'm afraid it is a different problem. However, matplotlib and xwpython seem like good tools I may use, once I know my porting is possible (and hopefully easy). > wondering if you think it's similar to those ends, and good enough to > let someone else look at yet? Not sure what you mean by "someone else look at yet?" Thanks, Paul From alan.gauld at btinternet.com Sat Oct 16 02:03:04 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 16 Oct 2010 01:03:04 +0100 Subject: [Tutor] Moving my C++ code to Python? References: Message-ID: "Paul" wrote > I would like to make modules for python. The problem is that the > vector of structs that is very large. First, is it possible to pass > such structures around to and from python and C/C++? Yes, Try looking at SWIG for one approach to making C/C++ code accesible to Python which may work for you > What would be the overhead cost of having a large structure > that needs to be passed to and from the C/C++ modules? I'm no expert so can't say for sure but since Pythion tends to operate with references I'd hope not too much. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From cfuller084 at thinkingplanet.net Sat Oct 16 05:51:31 2010 From: cfuller084 at thinkingplanet.net (Chris Fuller) Date: Fri, 15 Oct 2010 22:51:31 -0500 Subject: [Tutor] Moving my C++ code to Python? In-Reply-To: References: Message-ID: <201010152251.32385.cfuller084@thinkingplanet.net> SWIG supports opaque pointers that you can pass into and out of Python without any problems. Working with SWIG isn't that bad for basic stuff, although it can get complicated if you need to have it interpret exotica like references, structures, or such for Python. Having a good C++ background should cut a lot of the difficulty out of that, though. The documentation is pretty good, although finding what you need can involve a lot of scrolling around in long web pages. http://www.swig.org/ If you are going to use a command line interface, you might check out these modules from the standard library: http://docs.python.org/library/cmd.html http://docs.python.org/library/shlex.html You might also see if numpy can replace some of your C++ code. It's fast, and integrated into Python. http://numpy.scipy.org/ Cheers On Friday 15 October 2010, Paul wrote: > I would like to make modules for python. The problem is that the > vector of structs that is very large. First, is it possible to pass > such structures around to and from python and C/C++? What would be > the overhead cost of having a large structure that needs to be passed > to and from the C/C++ modules? From steve at pearwood.info Sat Oct 16 08:33:34 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 16 Oct 2010 17:33:34 +1100 Subject: [Tutor] 'module' object has no attribute (setting a class attribute) In-Reply-To: <20101015212037.GD30422@johnsons-web.com> References: <20101015212037.GD30422@johnsons-web.com> Message-ID: <201010161733.35251.steve@pearwood.info> On Sat, 16 Oct 2010 08:20:37 am Tim Johnson wrote: > I have two questions regarding this: > 1)Am I using the correct method to set a class attribute? Yes. The way to set a class attribute is the same way that you set an attribute on *any* object: some_object.attribute_name = something In your case, some_object is the class object tmpl.tmpl (Unlike some other languages, classes in Python are themselves objects.) > 2)Is there a system configuration that would cause the > AttributeError exception to print out the module name. > (In this cause it was 'kbLib' not 'tmpl'. No. The right way to fix this problem is to fix the error message, not to create a system configuration option. What would you call it? give_useless_error_messages = OFF *wink* The error messages in Python are not part of the language, but implementation details. Unfortunately CPython (the version you are almost certainly using) does have a few lousy error messages. However, in this case it is fairly straightforward to guess what the problem was. The error message you got was: 'module' object has no attribute 'templatepath' On the left hand side, you had the module object tmpl, but you weren't trying to set tmpl.templatepath (module object . attribute name), you were trying tmpl.tmpl.templatepath (module object . class object . attribute name). If the left hand side was the problem, you would probably have got this error instead: 'module' object has no attribute 'tmpl' (There are other errors you could have got, but they're fairly obscure and/or unusual.) -- Steven D'Aprano From vince at vinces.ca Fri Oct 15 08:22:45 2010 From: vince at vinces.ca (Vince Spicer) Date: Fri, 15 Oct 2010 00:22:45 -0600 Subject: [Tutor] Statistic-Program Problems! Please Help Quickly! In-Reply-To: References: Message-ID: On Thu, Oct 14, 2010 at 10:11 PM, Colleen Glaeser wrote: > BTW, the error message my program gives me for the B and M functions is: > > Traceback (most recent call last): > File "I:\Lab 7 wierd stat data.py", line 49, in > B() > File "I:\Lab 7 wierd stat data.py", line 44, in B > > ((Y() * Q()) - (P() * X())) / ((6 * Q()) - (X()**2)) > TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType' > > On Thu, Oct 14, 2010 at 11:09 PM, Colleen Glaeser > wrote: > >> Dear tutors, >> >> I am in a beginning-level computer science class in college and am running >> into problems with an assignment. >> >> The assignment is as follows: >> >> Statisticians are fond of drawing regression lines. In statistics and >> other fields where people analyze lots of data, one of the most commonly >> used regression lines is called the ?least squares line.? This is the line >> that is supposed to best fit the set of data points, in the sense that it >> minimizes the squared vertical distances between the points and the line. >> Why this should be a good fit is beyond the scope of this assignment. >> >> Presume that you have a collection of n two-dimensional data points. I?ll >> give it as a list of lists, where each of the lists inside represents one >> data point. >> >> Data :[ [x1, y1], [x2, y2], [x3, y3], ?, [xn, yn]] >> >> Compute the following >> >> The regression line is then given by >> >> where m and b may be obtained by >> >> and >> >> Your task is to compute the m and b (slope and intercept, respectively) >> for a set of data. You have to analyze the data as given, not count or >> add anything yourself. Your program should do everything, even figure >> out how many data points there are. >> >> Here?s your data: >> >> First set: [ [3, 1], [4, 3], [6, 4], [7, 6], [8, 8], [9, 8] ] >> >> Second set: [ [63, 11], [22, 7.5], [63, 11], [28, 10], [151, 12], [108, >> 10], [18, 8], [115, 10], [31,7], [44, 9] ] >> >> Find m and b, then calculate an estimate for x = 5 using the first data >> set. That is, plug in 5 for x and see what y you get. For the second >> set, try x = 95. >> >> Turn in: code, m, b, and the estimates for both data sets. >> >> >> >> >> *********************************************************************************************************************** >> >> There?s an easy way to walk through the data and extract the values you >> need. Use a for loop. Try this: >> >> for item in data: >> >> [x, y] = item >> >> print(x) >> >> >> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >> >> For extra credit: draw a scatter plot of the data, and draw in the least >> squares line. Scale the window to fit, making it a bit wider and higher >> than the data requires, so that some of the points are near but not on the >> edges of the window. Then sketch in the regression line. Note that you >> should calculate the window size based on the data ? don?t set them >> yourself; find the max and min values for x and y. You can print the >> scatter plot, or point me toward your web page. In any case, show me the >> code. >> >> >> >> So far, my program is as follows: >> >> Data = [[3,1],[4,3],[6, 4],[7, 6],[8, 8],[9, 8]] >> >> def X(): >> accX = 0 >> for item in Data: >> [x,y] = item >> >> accX = accX + x >> print (accX) >> >> >> def Y(): >> accY = 0 >> for item in Data: >> [x,y] = item >> >> accY = accY + y >> print (accY) >> >> def P(): >> accXY = 0 >> for item in Data: >> [x,y] = item >> >> accXY = accXY + (y*x) >> print (accXY) >> >> def Q(): >> accX2 = 0 >> for item in Data: >> [x,y] = item >> >> accX2 = accX2 + (x**2) >> print (accX2) >> >> X() >> Y() >> P() >> Q() >> >> >> >> def B(): >> ((Y() * Q()) - (P() * X())) / ((6 * Q()) - (X()**2)) >> >> def M(): >> ((Y() * Q()) - (P() * X())) / (X() * Q()) >> >> B() >> M() >> >> Now, my functions for X, Y, P, and Q are correct, but I have a couple of >> problems when it comes to continuing. First of all, despite what my teacher >> has told me, my method for trying to multiply X,Y,P, and Q's results in the >> functions for B and M are not working. I'm not sure if there is a way to >> make functions into variables or how to solve this problem. >> >> Second, I am confused as to what my teacher means to do when it comes to >> inputting different values of x. >> >> Find m and b, then calculate an estimate for x = 5 using the first data >> set. That is, plug in 5 for x and see what y you get. For the second >> set, try x = 95. >> >> Turn in: code, m, b, and the estimates for both data sets. >> >> >> I mean, I know I need to calculate the line of best fit for the data sets >> using B and M, but what in the world is x supposed to do and where does it >> go? How do I program this? This is especially harder since I've never >> taken a proper stat class before. >> >> Thank you all so much! >> >> -- >> Colleen Glaeser >> songbird42371 at gmail.com >> 636.357.8519 >> > > > > -- > Colleen Glaeser > songbird42371 at gmail.com > 636.357.8519 > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > You might want to look that returning data http://docs.python.org/library/functions.html -- Vince -- Sent from Ubuntu -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sat Oct 16 09:02:20 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 16 Oct 2010 18:02:20 +1100 Subject: [Tutor] Statistic-Program Problems! Please Help Quickly! In-Reply-To: References: Message-ID: <201010161802.20976.steve@pearwood.info> On Fri, 15 Oct 2010 05:22:45 pm Vince Spicer wrote: [trim nearly 200 lines of quoted text] > You might want to look that returning data > http://docs.python.org/library/functions.html Vince, would you mind trimming your responses in future? There's no need to quote the entire conversation just to add a one sentence response at the very end. Just leave enough to establish context, and anything you are *directly* responding to. Thank you. -- Steven D'Aprano From stefan_ml at behnel.de Sat Oct 16 10:34:54 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 16 Oct 2010 10:34:54 +0200 Subject: [Tutor] Moving my C++ code to Python? In-Reply-To: References: Message-ID: Paul, 15.10.2010 23:29: > I have a software written in C/C++ but considering porting most of it > to python, as it seems like it's a better choice for decision making > portion of the code. Write a wrapper first. > I'm also thinking about having a 'matlab' like > interface for reading, processing, and writing. Sure, why not. For example, the Sage math system has an interactive console and a web app (the Sage notebook) for that purpose. > In my current C++ code, I would read data into a vector of structs > (which contains other simple vectors, strings, and structs) and it can > be as large as 500MB to 2GB. The data gets processed (requires random > access). The result is then written out. > > I would like to make modules for python. The problem is that the > vector of structs that is very large. First, is it possible to pass > such structures around to and from python and C/C++? Easily. Your best bet is to keep the working code in C++ and write a wrapper in Cython. That will make it easy to write the plain wrapper code, and to give it a pythonic look-and-feel where you want it. It's certainly a lot more fun than digging your way through SWIG and also a lot more powerful. http://cython.org/ > What would be > the overhead cost of having a large structure that needs to be passed > to and from the C/C++ modules? Pretty low, but depends on the level of granularity. Calling from Python to C++ for each element may kill you, but a higher level interface will usually work very well and is easy to write in Cython. Stefan From smokefloat at gmail.com Sat Oct 16 12:30:38 2010 From: smokefloat at gmail.com (David Hutto) Date: Sat, 16 Oct 2010 06:30:38 -0400 Subject: [Tutor] Moving my C++ code to Python? In-Reply-To: References: Message-ID: On Fri, Oct 15, 2010 at 6:39 PM, Paul wrote: > On Fri, Oct 15, 2010 at 3:07 PM, David Hutto wrote: >> This isn't an answer to your question, but I'm doing about the same in >> reverse(it sounds like) as you're doing. More directly just processing >> data for 2-d/3d graphs using matplotlib and wxpython. So, I was > > I'm afraid it is a different problem. ?However, matplotlib and > xwpython seem like good tools I may use, once I know my porting is > possible (and hopefully easy). > >> wondering if you think it's similar to those ends, and good enough to >> let someone else look at yet? > > Not sure what you mean by "someone else look at yet?" I see code like writing a story, or book, you don't want anyone to critique it(or in my case learn from it) until you have a good enough rough draft. > > Thanks, Paul > From davea at ieee.org Sat Oct 16 13:43:15 2010 From: davea at ieee.org (Dave Angel) Date: Sat, 16 Oct 2010 07:43:15 -0400 Subject: [Tutor] 'module' object has no attribute (setting a class attribute) In-Reply-To: <20101015212037.GD30422@johnsons-web.com> References: <20101015212037.GD30422@johnsons-web.com> Message-ID: <4CB98FD3.30803@ieee.org> On 2:59 PM, Tim Johnson wrote: > My intention is to set a class attribute so that any number of > instantiations will have this value. > > the module is tmpl.py. the class is tmpl. > > if from the script I do this: > import tmpl > tmpl.tmpl.templatepath = kbLib.templatepath > > I get error message: > 'module' object has no attribute 'templatepath' > > I ended up getting completely befuddled, and then realized that the > problem was the right side of the assignment statement. I.E. I had > a typo on the right and python was not informing me of *which* > module didn't have the attribute > > I have two questions regarding this: > 1)Am I using the correct method to set a class attribute? > 2)Is there a system configuration that would cause the > AttributeError exception to print out the module name. > (In this cause it was 'kbLib' not 'tmpl'. > > thanks 1) The code is correct. But it'd be much clearer if you followed conventions and named your class with a leading uppercase. So the module would be called tmpl, and the class would be called Tmpl. 2) For the general case, this error is about fetching attributes from arbitrary objects. Such an object is not always bound to a single name, so it reports the object's type, rather than its name. In this particular case, a module has a unique name, so the message could have been more helpful. But it's not clear how the error display logic could have readily known that. Two things could help you figure it for yourself more quickly. a) On the left side of such an assignment, this error can only refer to attributes other than the last, since the last one is being created here, and it wouldn't matter if it already existed. b) You could refactor the line, and see where the error message moves to. Let's try that: a = kbLib.templatepath tmpl.Ttmpl.templatepath = a If the line that now gets the error had still been confusing, you could try to refactor that in turn. DaveA From tim042849 at gmail.com Sat Oct 16 17:35:28 2010 From: tim042849 at gmail.com (Tim Johnson) Date: Sat, 16 Oct 2010 07:35:28 -0800 Subject: [Tutor] 'module' object has no attribute (setting a class attribute) In-Reply-To: <4CB98FD3.30803@ieee.org> References: <20101015212037.GD30422@johnsons-web.com> <4CB98FD3.30803@ieee.org> Message-ID: <20101016153528.GD5345@johnsons-web.com> * Dave Angel [101016 03:45]: > > 1) The code is correct. But it'd be much clearer if you followed > conventions and named your class with a leading uppercase. So the > module would be called tmpl, and the class would be called Tmpl. I didn't know there was such a convention. Serves me right for being self-taught and self-employed > 2) For the general case, this error is about fetching attributes from > arbitrary objects. Such an object is not always bound to a single name, > so it reports the object's type, rather than its name. In this > particular case, a module has a unique name, so the message could have > been more helpful. But it's not clear how the error display logic could > have readily known that. Ah! Understood. > Two things could help you figure it for yourself more quickly. a) On > the left side of such an assignment, this error can only refer to > attributes other than the last, since the last one is being created > here, and it wouldn't matter if it already existed. b) You could > refactor the line, and see where the error message moves to. Let's try > that: Good trick. > > a = kbLib.templatepath > tmpl.Ttmpl.templatepath = a > > If the line that now gets the error had still been confusing, you could > try to refactor that in turn. thanks -- Tim tim at johnsons-web.com or akwebsoft.com http://www.akwebsoft.com From emile at fenx.com Sat Oct 16 22:19:39 2010 From: emile at fenx.com (Emile van Sebille) Date: Sat, 16 Oct 2010 13:19:39 -0700 Subject: [Tutor] 'module' object has no attribute (setting a class attribute) In-Reply-To: <20101016153528.GD5345@johnsons-web.com> References: <20101015212037.GD30422@johnsons-web.com> <4CB98FD3.30803@ieee.org> <20101016153528.GD5345@johnsons-web.com> Message-ID: On 10/16/2010 8:35 AM Tim Johnson said... > * Dave Angel [101016 03:45]: >> >> 1) The code is correct. But it'd be much clearer if you followed >> conventions and named your class with a leading uppercase. So the >> module would be called tmpl, and the class would be called Tmpl. > I didn't know there was such a convention. Serves me right > for being self-taught and self-employed Pep 8 is the generally accepted style guide. http://www.python.org/dev/peps/pep-0008/ Although there are few recommendations I don't follow, it's good to write in the style most everyone else strives for. Emile From alan.gauld at btinternet.com Sun Oct 17 02:14:06 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 17 Oct 2010 01:14:06 +0100 Subject: [Tutor] 'module' object has no attribute (setting aclass attribute) References: <20101015212037.GD30422@johnsons-web.com> <4CB98FD3.30803@ieee.org> <20101016153528.GD5345@johnsons-web.com> Message-ID: "Tim Johnson" wrote >> conventions and named your class with a leading uppercase. ... > I didn't know there was such a convention. Its not universally observed but it is common, not just in Python but in most OOP languages. It goes back at least as far as SmallTalk80, and maybe even to Simula67 the original OOP language... (NB In both cases the number refers to the year of issue!) Alan G. From zebra05 at gmail.com Sun Oct 17 22:56:18 2010 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Sun, 17 Oct 2010 22:56:18 +0200 Subject: [Tutor] Moving my C++ code to Python? In-Reply-To: References: Message-ID: Also have a look at Boost.Python: http://www.boost.org/doc/libs/1_44_0/libs/python/doc/index.html On Fri, Oct 15, 2010 at 11:29 PM, Paul wrote: > Hi there, > > I've been using C/C++ for many years (python, just reading about it). > > I have a software written in C/C++ but considering porting most of it > to python, as it seems like it's a better choice for decision making > portion of the code. I'm also thinking about having a 'matlab' like > interface for reading, processing, and writing. > > In my current C++ code, I would read data into a vector of structs > (which contains other simple vectors, strings, and structs) and it can > be as large as 500MB to 2GB. The data gets processed (requires random > access). The result is then written out. > > I would like to make modules for python. The problem is that the > vector of structs that is very large. First, is it possible to pass > such structures around to and from python and C/C++? What would be > the overhead cost of having a large structure that needs to be passed > to and from the C/C++ modules? > > # I imagine I'd use the newly written software this way: > >>> import c_stuff # my C/C++ module > >>> largestuff = c_stuff.read(file) # read from disk > >>> c_stuff.process(largestuff, someparams1) # processing, requires random > access to the vector > >>> c_stuff.process(largestuff, someparams2) > ... > >>> c_stuff.process(largestuff, someparams10000) # the whole thing may take > a few minutes to days > >>> > >>> import python_stuff # some module written in python to process the data > as well > >>> > >>> python_stuff.process(largestuff, otherparams1) # It's important that > this data can be read (and I hope written) by python code > >>> python_stuff.process(largestuff, otherparams2) > >>> > >>> c_stuff.write(largestuff) #write result > > Thank you in advance, > Paul > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Regards, Sithembewena Lloyd Dube http://www.lloyddube.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From zebra05 at gmail.com Sun Oct 17 22:56:18 2010 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Sun, 17 Oct 2010 22:56:18 +0200 Subject: [Tutor] Moving my C++ code to Python? In-Reply-To: References: Message-ID: Also have a look at Boost.Python: http://www.boost.org/doc/libs/1_44_0/libs/python/doc/index.html On Fri, Oct 15, 2010 at 11:29 PM, Paul wrote: > Hi there, > > I've been using C/C++ for many years (python, just reading about it). > > I have a software written in C/C++ but considering porting most of it > to python, as it seems like it's a better choice for decision making > portion of the code. I'm also thinking about having a 'matlab' like > interface for reading, processing, and writing. > > In my current C++ code, I would read data into a vector of structs > (which contains other simple vectors, strings, and structs) and it can > be as large as 500MB to 2GB. The data gets processed (requires random > access). The result is then written out. > > I would like to make modules for python. The problem is that the > vector of structs that is very large. First, is it possible to pass > such structures around to and from python and C/C++? What would be > the overhead cost of having a large structure that needs to be passed > to and from the C/C++ modules? > > # I imagine I'd use the newly written software this way: > >>> import c_stuff # my C/C++ module > >>> largestuff = c_stuff.read(file) # read from disk > >>> c_stuff.process(largestuff, someparams1) # processing, requires random > access to the vector > >>> c_stuff.process(largestuff, someparams2) > ... > >>> c_stuff.process(largestuff, someparams10000) # the whole thing may take > a few minutes to days > >>> > >>> import python_stuff # some module written in python to process the data > as well > >>> > >>> python_stuff.process(largestuff, otherparams1) # It's important that > this data can be read (and I hope written) by python code > >>> python_stuff.process(largestuff, otherparams2) > >>> > >>> c_stuff.write(largestuff) #write result > > Thank you in advance, > Paul > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Regards, Sithembewena Lloyd Dube http://www.lloyddube.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From paulanon at gmail.com Sun Oct 17 23:29:30 2010 From: paulanon at gmail.com (Paul) Date: Sun, 17 Oct 2010 14:29:30 -0700 Subject: [Tutor] Moving my C++ code to Python? In-Reply-To: References: Message-ID: Thank you all for your kind suggestions. It seem that cython and boost.python both look like a good suggestions; SWIG seems like a good tool, but may not meet my requirements. Quick google seems to suggest that boost.python is 7 times slower [1], however. Any thoughts about speed performance between the two? [1] http://blog.chrischou.org/2010/02/28/simple-benchmark-between-cython-and-boost-python/ Thank you, Paul And a link I found today. [2] http://koichitamura.blogspot.com/2008/06/various-ways-to-integrate-python-and-c.html On Sun, Oct 17, 2010 at 1:56 PM, Sithembewena Lloyd Dube wrote: > Also have a look at Boost.Python: > > http://www.boost.org/doc/libs/1_44_0/libs/python/doc/index.html > > On Fri, Oct 15, 2010 at 11:29 PM, Paul wrote: >> >> Hi there, >> >> I've been using C/C++ for many years (python, just reading about it). >> >> I have a software written in C/C++ but considering porting most of it >> to python, as it seems like it's a better choice for decision making >> portion of the code. ?I'm also thinking about having a 'matlab' like >> interface for reading, processing, and writing. >> >> In my current C++ code, I would read data into a vector of structs >> (which contains other simple vectors, strings, and structs) and it can >> be as large as 500MB to 2GB. ?The data gets processed (requires random >> access). ?The result is then written out. >> >> I would like to make modules for python. ?The problem is that the >> vector of structs that is very large. ?First, is it possible to pass >> such structures around to and from python and C/C++? ?What would be >> the overhead cost of having a large structure that needs to be passed >> to and from the C/C++ modules? >> >> # I imagine I'd use the newly written software this way: >> >>> import c_stuff ?# my C/C++ module >> >>> largestuff = c_stuff.read(file) ?# read from disk >> >>> c_stuff.process(largestuff, someparams1) ?# processing, requires >> >>> random access to the vector >> >>> c_stuff.process(largestuff, someparams2) >> ... >> >>> c_stuff.process(largestuff, someparams10000) # the whole thing may >> >>> take a few minutes to days >> >>> >> >>> import python_stuff # some module written in python to process the >> >>> data as well >> >>> >> >>> python_stuff.process(largestuff, otherparams1) ?# It's important that >> >>> this data can be read (and I hope written) by python code >> >>> python_stuff.process(largestuff, otherparams2) >> >>> >> >>> c_stuff.write(largestuff) #write result >> >> Thank you in advance, >> Paul >> _______________________________________________ >> Tutor maillist ?- ?Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > > > -- > Regards, > Sithembewena Lloyd Dube > http://www.lloyddube.com > From zebra05 at gmail.com Mon Oct 18 00:05:25 2010 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Mon, 18 Oct 2010 00:05:25 +0200 Subject: [Tutor] Moving my C++ code to Python? In-Reply-To: References: Message-ID: Tis a pleasure, Paul. have no experience with Cython vs Boost.Python benchmarking, but a real world user of the latter had something to say at http://www.python.org/about/quotes/ (last review). On Sun, Oct 17, 2010 at 11:29 PM, Paul wrote: > Thank you all for your kind suggestions. It seem that cython and > boost.python both look like a good suggestions; SWIG seems like a good > tool, but may not meet my requirements. > Quick google seems to suggest that boost.python is 7 times slower [1], > however. Any thoughts about speed performance between the two? > > [1] > http://blog.chrischou.org/2010/02/28/simple-benchmark-between-cython-and-boost-python/ > > Thank you, > Paul > > And a link I found today. > [2] > http://koichitamura.blogspot.com/2008/06/various-ways-to-integrate-python-and-c.html > > On Sun, Oct 17, 2010 at 1:56 PM, Sithembewena Lloyd Dube > wrote: > > Also have a look at Boost.Python: > > > > http://www.boost.org/doc/libs/1_44_0/libs/python/doc/index.html > > > > On Fri, Oct 15, 2010 at 11:29 PM, Paul wrote: > >> > >> Hi there, > >> > >> I've been using C/C++ for many years (python, just reading about it). > >> > >> I have a software written in C/C++ but considering porting most of it > >> to python, as it seems like it's a better choice for decision making > >> portion of the code. I'm also thinking about having a 'matlab' like > >> interface for reading, processing, and writing. > >> > >> In my current C++ code, I would read data into a vector of structs > >> (which contains other simple vectors, strings, and structs) and it can > >> be as large as 500MB to 2GB. The data gets processed (requires random > >> access). The result is then written out. > >> > >> I would like to make modules for python. The problem is that the > >> vector of structs that is very large. First, is it possible to pass > >> such structures around to and from python and C/C++? What would be > >> the overhead cost of having a large structure that needs to be passed > >> to and from the C/C++ modules? > >> > >> # I imagine I'd use the newly written software this way: > >> >>> import c_stuff # my C/C++ module > >> >>> largestuff = c_stuff.read(file) # read from disk > >> >>> c_stuff.process(largestuff, someparams1) # processing, requires > >> >>> random access to the vector > >> >>> c_stuff.process(largestuff, someparams2) > >> ... > >> >>> c_stuff.process(largestuff, someparams10000) # the whole thing may > >> >>> take a few minutes to days > >> >>> > >> >>> import python_stuff # some module written in python to process the > >> >>> data as well > >> >>> > >> >>> python_stuff.process(largestuff, otherparams1) # It's important > that > >> >>> this data can be read (and I hope written) by python code > >> >>> python_stuff.process(largestuff, otherparams2) > >> >>> > >> >>> c_stuff.write(largestuff) #write result > >> > >> Thank you in advance, > >> Paul > >> _______________________________________________ > >> Tutor maillist - Tutor at python.org > >> To unsubscribe or change subscription options: > >> http://mail.python.org/mailman/listinfo/tutor > > > > > > > > -- > > Regards, > > Sithembewena Lloyd Dube > > http://www.lloyddube.com > > > -- Regards, Sithembewena Lloyd Dube http://www.lloyddube.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan_ml at behnel.de Mon Oct 18 08:21:39 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 18 Oct 2010 08:21:39 +0200 Subject: [Tutor] Moving my C++ code to Python? In-Reply-To: References: Message-ID: Paul, 17.10.2010 23:29: > Thank you all for your kind suggestions. It seem that cython and > boost.python both look like a good suggestions; SWIG seems like a good > tool, but may not meet my requirements. > Quick google seems to suggest that boost.python is 7 times slower [1], > however. Any thoughts about speed performance between the two? Cython is an optimising compiler. It does various tweaks to your code that make it run faster. So it's no surprise that it's faster than any other Python wrapping tool out there. http://behnel.de/cgi-bin/weblog_basic/index.php?p=38 http://behnel.de/cycppbench/ Stefan From raquel.adams at gmail.com Tue Oct 19 00:34:19 2010 From: raquel.adams at gmail.com (Raquel) Date: Mon, 18 Oct 2010 16:34:19 -0600 Subject: [Tutor] High Low Game Message-ID: Hi, I am new to Python, and have hit a WALL. Any help is appreciated! Below is what I have so far, for the "High Low Game"...when I try to run it, nothing happens, so I have been unable to finish it. I am sure it is something simple... #assignment 2 part 1 high low def main(): print " Pick a number between 1 and 1000 and I will try to guess it" print "in no more than 10 tries. After each guess, enter 0 if I" print "got it right, -1 if I need to guess lower, and 1 if I need to" print "guess higher." high=1000 low=1 tries=1 while high > low: ave=(high+low)/2 print "My guess is", ave, guess=int(raw_input("Please enter '-1','0',or '1'):")) print guess if guess == 0: print "That took 1", tries, "guesses." elif guess == -1: print "I will guess lower." elif guess == 1: print "I will guess higher." else: print "Pick a number between 1 and 1000 and I will try to guess it" print "in no more than 10 tries. After each guess, enter 0 if I" print "got it right, -1 if I need to guess lower, and 1 if I need to" print "guess higher." main() -------------- next part -------------- An HTML attachment was scrubbed... URL: From mehgcap at gmail.com Tue Oct 19 00:44:50 2010 From: mehgcap at gmail.com (Alex Hall) Date: Mon, 18 Oct 2010 18:44:50 -0400 Subject: [Tutor] pydoc? Message-ID: Hi all, Below is part of an email I got from someone in reply to a question about a program called brlapi. I am on Windows. Can someone explain what is going on here? > Hmm... I am relatively new to Python and have not found this pydoc. Well, I don't know how this is supposed to work on Windows. On Linux, you simply run pydoc brlapi in a shell. Alternatively, you can have a look in brltty's source code, in Bindings/Python/brlapi.pyx, which is the source for that pydoc. -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From smokefloat at gmail.com Tue Oct 19 01:36:00 2010 From: smokefloat at gmail.com (David Hutto) Date: Mon, 18 Oct 2010 19:36:00 -0400 Subject: [Tutor] High Low Game In-Reply-To: References: Message-ID: On Mon, Oct 18, 2010 at 6:34 PM, Raquel wrote: > Hi, > I am new to Python, and have hit a WALL. ?Any help is appreciated! ? Below > is what I have so far, for the "High Low Game"...when I try to run it, > nothing happens, so I have been unable to finish it. ?I am sure it is > something simple... > #assignment 2 part 1 high low > def main(): > ?? ? print " Pick a number between 1 and 1000 and I will try to guess it" > ?? ? print "in no more than 10 tries. After each guess, enter 0 if I" > ?? ? print "got it right, -1 if I need to guess lower, and 1 if I need to" > ?? ? print "guess higher." > high=1000 > low=1 > tries=1 > while high > low: > ?? ? ave=(high+low)/2 > ?? ? print "My guess is", ave, > ?? ? guess=int(raw_input("Please enter '-1','0',or '1'):")) > ?? ? print guess > ?? ? if guess == 0: > ?? ? ? ? ?print "That took 1", tries, "guesses." > > ?? ? elif guess == -1: > ?? ? ? ? ?print "I will guess lower." > ?? ? elif guess == 1: > ?? ? ? ? ?print "I will guess higher." > ?? ? else: > ?? ? ? ? ?print "Pick a number between 1 and 1000 and I will try to guess > it" > ?? ? ? ? ?print "in no more than 10 tries. ?After each guess, enter 0 if I" > ?? ? ? ? ?print "got it right, -1 if I need to guess lower, and 1 if I need > to" > ?? ? ? ? ?print "guess higher." main() > move main() outside the while loop. This will get the car started, but haven't looked for other flaws in the engine > > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > From smokefloat at gmail.com Tue Oct 19 01:36:57 2010 From: smokefloat at gmail.com (David Hutto) Date: Mon, 18 Oct 2010 19:36:57 -0400 Subject: [Tutor] High Low Game In-Reply-To: References: Message-ID: More importantly, outside the function it calls. From alan.gauld at btinternet.com Tue Oct 19 01:37:47 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 19 Oct 2010 00:37:47 +0100 Subject: [Tutor] High Low Game References: Message-ID: "Raquel" wrote > I am new to Python, and have hit a WALL. Any help is appreciated! > Below > is what I have so far, for the "High Low Game"...when I try to run > it, > nothing happens, so I have been unable to finish it. I am sure it > is > something simple... First some questions: 1) What OS are you using? 2) What version of Python 3) How are you trying to run the program? 4) When you say "nothing happensd" is that absolutely true? Is anything displayed, even fleetingly? Second, some comments on your code: > def main(): > print " Pick a number between 1 and 1000 and I will try to guess > it" > print "in no more than 10 tries. After each guess, enter 0 if I" > print "got it right, -1 if I need to guess lower, and 1 if I > need to" > print "guess higher." You could do this with a single print statement and a triple quoted string. And if you made it a variable you wouldn't even need a function, just print the prompt message: prompt = """ Pick a number between 1 and 1000 and I will try to guess it in no more than 10 tries. After each guess, enter 0 if I got it right, -1 if I need to guess lower, and 1 if I need to guess higher. """ print prompt > high=1000 > low=1 > tries=1 > > while high > low: > ave=(high+low)/2 > print "My guess is", ave, Your guesses will always be the same since you never change high or low... For the same reason you will never exit the while loop. > guess=int(raw_input("Please enter '-1','0',or '1'):")) > print guess > if guess == 0: > print "That took 1", tries, "guesses." I don't think you want the 1 in there. tries should suffice? Also you might want to force an exit from the while loop here? > elif guess == -1: > print "I will guess lower." > elif guess == 1: > print "I will guess higher." Nice promise but you only ever guess lower. You need to move the guess creation code into the appropriate if/else block. > else: > print "Pick a number between 1 and 1000 and I will try to > guess > it" > print "in no more than 10 tries. After each guess, enter 0 > if I" > print "got it right, -1 if I need to guess lower, and 1 if > I need > to" > print "guess higher." You don't need this since you print it in the next line anyway! > main() This could become the "print prompt" line... While there are flaws in the logic it looks like it should work after a fashion, aso that brings me back to the question of how you are running it?. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue Oct 19 01:47:14 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 19 Oct 2010 00:47:14 +0100 Subject: [Tutor] pydoc? References: Message-ID: "Alex Hall" wrote > about a program called brlapi. I am on Windows. Can someone explain > what is going on here? > >> Hmm... I am relatively new to Python and have not found this pydoc. > > Well, I don't know how this is supposed to work on Windows. On > Linux, > you simply run > > pydoc brlapi > pydoc lives in C:\PythonXX\Tools\scripts And the best way to run it is probably to use the GUI so cd to the folder and run python pydocgui.pyw Or create a shortcut to do the same... Basically it will provide you with a prettified web version of the help() output! For that reason I tend not to use it much... YMMV HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From jbiquez at icsmx.com Tue Oct 19 05:29:23 2010 From: jbiquez at icsmx.com (Jorge Biquez) Date: Mon, 18 Oct 2010 22:29:23 -0500 Subject: [Tutor] Learning with Open Source Applications. Message-ID: <201010190337.o9J3b49Z032000@krusty.intranet.com.mx> Hello all. I am sorry for the cross posting but I really would like to hear comments from experience people in Python. I am new to Python, not new in programming. I am leaving the PHP path and moving to Python. When I was learning PHP it was very useful to learn to install OpenSource solutions , implemented them and study how they work, learn how to modify them. For example I used e-commerce shopping cart solution and learned a lot. Can you recommend, similar solutions, not only for shopping cart but any other subject is good also so I can follow the same schema of learning ? I mean, solutions you consider are very well written and that are examples of what a good Python applications should be written? I know there are tons of applications but would like to hear advice based on experience if possible. Thanks. Thanks in advance. Jorge Biquez From tim at johnsons-web.com Tue Oct 19 21:56:06 2010 From: tim at johnsons-web.com (Tim Johnson) Date: Tue, 19 Oct 2010 11:56:06 -0800 Subject: [Tutor] Requesting restricted URL (further authentication requested) Message-ID: <20101019195606.GC2669@johnsons-web.com> I've written the following function which successfully gets an authenticated URL: def getRestrictedURL(authName,URL,log,pswd): auth_handler = urllib2.HTTPBasicAuthHandler() auth_handler.add_password(authName, URL,log,pswd) opener = urllib2.build_opener(auth_handler) urllib2.install_opener(opener) return opener.open(URL).read() # But, alas, any links in content 'beneath' the URL # require additional authentication. Any way around this? Thanks -- Tim tim at johnsons-web.com or akwebsoft.com http://www.akwebsoft.com From vince at vinces.ca Tue Oct 19 22:04:32 2010 From: vince at vinces.ca (Vince Spicer) Date: Tue, 19 Oct 2010 14:04:32 -0600 Subject: [Tutor] Requesting restricted URL (further authentication requested) In-Reply-To: <20101019195606.GC2669@johnsons-web.com> References: <20101019195606.GC2669@johnsons-web.com> Message-ID: On Tue, Oct 19, 2010 at 1:56 PM, Tim Johnson wrote: > I've written the following function which successfully gets an > authenticated URL: > def getRestrictedURL(authName,URL,log,pswd): > auth_handler = urllib2.HTTPBasicAuthHandler() > auth_handler.add_password(authName, URL,log,pswd) > opener = urllib2.build_opener(auth_handler) > urllib2.install_opener(opener) > return opener.open(URL).read() > # But, alas, any links in content 'beneath' the URL > # require additional authentication. > Any way around this? > Thanks > -- > Tim > tim at johnsons-web.com or akwebsoft.com > http://www.akwebsoft.com > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Tim, Unless you are tied to the standard library I would recommend looking at httplib2 http://code.google.com/p/httplib2/ This handles your authentication and connection much better then the standard urllib. -- Vince Spicer -- Sent from Ubuntu -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Tue Oct 19 23:02:02 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Tue, 19 Oct 2010 23:02:02 +0200 Subject: [Tutor] Requesting restricted URL (further authentication requested) In-Reply-To: <20101019195606.GC2669@johnsons-web.com> References: <20101019195606.GC2669@johnsons-web.com> Message-ID: On 19 October 2010 21:56, Tim Johnson wrote: > I've written the following function which successfully gets an > authenticated URL: > def getRestrictedURL(authName,URL,log,pswd): > ? ? ? ?auth_handler = urllib2.HTTPBasicAuthHandler() > ? ? ? ?auth_handler.add_password(authName, URL,log,pswd) > ? ? ? ?opener = urllib2.build_opener(auth_handler) > ? ? ? ?urllib2.install_opener(opener) > ? ? ? ?return opener.open(URL).read() The above should be: data = opener.open(URL).read() #does the initial authentication. return opener #return the opener object not the data you read. > # But, alas, any links in content 'beneath' the URL > # require additional authentication. You are almost there. In order to authenticate initially you indeed need to read the page. But then what you want is to return the opener object, not the page you read which you do currently. The opener object holds the authentication data and this opener object you will need to use for every new url you want to read, opener.open(). Depending on what you want to do with the data you might want to split this up into 2 functions. One that will authenticate and another that read the url and returns the data. The second function would need to be passed the opener object which the first one returned. Greets Sander From tim at johnsons-web.com Tue Oct 19 23:29:52 2010 From: tim at johnsons-web.com (Tim Johnson) Date: Tue, 19 Oct 2010 13:29:52 -0800 Subject: [Tutor] Requesting restricted URL (further authentication requested) In-Reply-To: References: <20101019195606.GC2669@johnsons-web.com> Message-ID: <20101019212952.GD2669@johnsons-web.com> * Vince Spicer [101019 12:25]: > On Tue, Oct 19, 2010 at 1:56 PM, Tim Johnson wrote: > > > Tim, > > Unless you are tied to the standard library I would recommend looking at > > httplib2 http://code.google.com/p/httplib2/ > > This handles your authentication and connection much better then the > standard urllib. Thanks Vince, I will give that a try. I'd like to keep things simpler than a global opener as per the other response. I'll let you all know how that goes. cheers -- Tim tim at johnsons-web.com or akwebsoft.com http://www.akwebsoft.com From matthewnunes at hotmail.com Tue Oct 19 23:02:43 2010 From: matthewnunes at hotmail.com (Matthew Nunes) Date: Wed, 20 Oct 2010 03:02:43 +0600 Subject: [Tutor] Problem with python Message-ID: To whom it may concern, Hi, I've just started learning how to program in python using Allan B. Downy's book "How to think like a computer scientist" and it explained something in the recursion chapter which have still been unable to understand. It wrote a piece of code for the factorial function in math for example 3! is 3 * 2 * 1. I cannot understand the how it claimed the code executed, and logically it makes no sense to me. Please explain it to me, as I have spent hours trying to get my head around it, as the book(in my opinion) did not explain it well. Here it is: def factorial(n): if n == 0: return 1 else: recurse = factorial(n-1) result = n * recurse return result If there is and easier piece of code that you know of that can be used for factorial, if you could please also tell me that. Thank you for your time. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Oct 20 00:45:22 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 19 Oct 2010 23:45:22 +0100 Subject: [Tutor] Problem with python References: Message-ID: "Matthew Nunes" wrote... > ...something in the recursion chapter which have still been unable > to understand. It wrote a piece of code for the factorial function > .... Please explain it to me, as I have spent hours trying to get > my head around it, Recursion is one of the more baffling topics when you first encounter it so don't be surprised if it takes a couple of goes to "get it". It even trips up people who have been using it for years. For an alternative explanation of recursion try my tutorial (in the Advanced Topics section, under recursion(!)). I walk through factorial cycle by cycle, see if that explanation helps. If it does you can read a bit more more about it and its uses in the Functional Programming topic. If not, try forming some more specific questions around the bits that puzzle you most. The more specific the question the greater the chance of an answer... HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From emmanuel.ruellan at laposte.net Wed Oct 20 01:26:46 2010 From: emmanuel.ruellan at laposte.net (Emmanuel Ruellan) Date: Wed, 20 Oct 2010 01:26:46 +0200 Subject: [Tutor] Problem with python In-Reply-To: References: Message-ID: On Tue, Oct 19, 2010 at 11:02 PM, Matthew Nunes wrote: > > It wrote a piece of code for the factorial function in math for example 3! > is 3 * 2 * 1. I cannot understand the how it claimed the code executed, and > logically it makes no sense to me. > > I suggest you follow the algorithm yourself, with a pencil and a sheet of paper. Substitute various numerical values for n, starting with zero. For example: For n=0, the body of the function becomes: if 0 == 0: return 1 else: recurse = factorial(0-1) result = 0 * recurse return result What result do you get? For n=1, it gets a little bit tricky, because the function calls itself: if 1 == 0: return 1 else: recurse = factorial(1-1) result = 1 * recurse return result You'd like an easier method to calculate factorials? >>> from math import factorial >>> print factorial(4) 24 -- Emmanuel -------------- next part -------------- An HTML attachment was scrubbed... URL: From waynejwerner at gmail.com Wed Oct 20 01:39:17 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Tue, 19 Oct 2010 18:39:17 -0500 Subject: [Tutor] Problem with python In-Reply-To: References: Message-ID: On Tue, Oct 19, 2010 at 4:02 PM, Matthew Nunes wrote: > To whom it may concern, > > Hi, I've just started learning how to > program in python using Allan B. Downy's book "How to think like a computer > scientist" and it explained something in the recursion chapter which have > still been unable to understand. It wrote a piece of code for the factorial > function in math for example 3! is 3 * 2 * 1. I cannot understand the how it > claimed the code executed, and logically it makes no sense to me. Please > explain it to me, as I have spent hours trying to get my head around it, as > the book(in my opinion) did not explain it well. Here it is: > That's a pretty solid book. This example uses recursion - for a great example of recursion, search Google for recursion But we can examine the code: This is a function that takes one parameter named n > def factorial(n): > If n is 0 then return 1 (since 0! = 1) > if n == 0: > > return 1 > > else: > If n > 0 then the definition of a factorial is n * factorial(n-1) - which we do here in two steps > recurse = factorial(n-1) > > result = n * recurse > And then we return the result > return result > > If there is and easier piece of code that you know of that can be used for > factorial, if you could please also tell me that. > This is probably the very easiest recursive example to get your head around because the definition of factorial is recursive. However, it's a *terribly* inefficient way to compute the factorial of a number. The most efficient way for something like 3 is this: import math math.factorial(3) Using the built-in functions are always better. However, if you're wanting a code example you can do this one: def factorial(n): if n == 0: n = 1 fact = 1 for x in xrange(1, n+1): fact = fact * x # This could be replaced by fact *= x return fact Or you could do something a little more advanced: def factorial(n): if n == 0: n = 1 return reduce(lambda x,y: x*y, xrange(1,n+1)) But that's probably a little beyond your comprehension level at this point. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From fomcl at yahoo.com Wed Oct 20 11:05:06 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 20 Oct 2010 02:05:06 -0700 (PDT) Subject: [Tutor] updating a Csv file Message-ID: <345467.46359.qm@web110711.mail.gq1.yahoo.com> Hi all, How can I update a csv file? I've written the code below, but it does not work ("Error: line contains NULL byte"). I've never tried opening a file in read and write mode (r+) at the same time before, so I suspect that this is the culprit. Should I instead just open one file, write to another, throw away the original and rename the new file? That seems inefficient. import csv, string def updateLine(idVar, idValue, myCsv, newLine): ??? f = open(myCsv, "r+") ??? r = csv.reader(f) ??? w = csv.writer(f) ??? header = r.next() ??? idPos = header.index(idVar) ??? for row? in r: ??????? if row[idPos] == idValue: ??????????? row = newLine ??????????? w.writerow(row) ??? f.close() updateLine(idVar = "Id", ?????????? idValue = "hawxgXvbfu", ?????????? myCsv = "c:/temp/someCsv.csv", ?????????? newLine = [ch for ch in string.letters[0:9]]) Cheers!! Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Wed Oct 20 11:47:42 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 20 Oct 2010 20:47:42 +1100 Subject: [Tutor] updating a Csv file In-Reply-To: <345467.46359.qm@web110711.mail.gq1.yahoo.com> References: <345467.46359.qm@web110711.mail.gq1.yahoo.com> Message-ID: <201010202047.42432.steve@pearwood.info> On Wed, 20 Oct 2010 08:05:06 pm Albert-Jan Roskam wrote: > Hi all, > > > > How can I update a csv file? I've written the code below, but it does > not work ("Error: line contains NULL byte"). Please copy and paste (DO NOT re-type, summarize, paraphrase or repeat from memory) the ACTUAL exception, including the traceback. Based on the message shown completely out of context, I'm guessing that you're trying to stuff binary data into a CSV file, which needs plain text. But there doesn't seem to be anything in your code snippet that includes binary data, so I suspect that the code you are actually running is not the same as the code you think you are running. Either that or you have modified string.letters to include things which are not letters. It might help if you mention what version of Python you're using, and the platform. [...] > ?????????? newLine = [ch for ch in string.letters[0:9]] What's wrong with this? newline = list(string.letters[0:9]) -- Steven D'Aprano From steve at pearwood.info Wed Oct 20 12:13:39 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 20 Oct 2010 21:13:39 +1100 Subject: [Tutor] Problem with python In-Reply-To: References: Message-ID: <201010202113.39815.steve@pearwood.info> On Wed, 20 Oct 2010 08:02:43 am Matthew Nunes wrote: > I cannot understand the how it claimed the code executed, and > logically it makes no sense to me. Please explain it to me, as I > have spent hours trying to get my head around it, as the book(in my > opinion) did not explain it well. Here it is: > def factorial(n): > if n == 0: > return 1 > else: > recurse = factorial(n-1) > result = n * recurse > return result To be pedantic, the above code will raise a SyntaxError, because you've ignored or lost the indentation. Fortunately in this case it's easy to fix: def factorial(n): if n == 0: return 1 else: recurse = factorial(n-1) result = n * recurse return result Let's start with an easy example: factorial(0). When Python sees factorial(0), it does this: Call factorial with argument 0: * if n == 0 <-- this is true, so execute the "if" block: return 1 so the function factorial(0) returns the value 1. All that goes on behind the scenes. What *we* see is: >>> factorial(0) 1 That part is easy. Now, the recursive part. What does Python do when you call factorial(1)? Call factorial with argument 1: * if n == 0 <-- this is false, so execute the "else" block: recurse = factorial(n-1) Here Python is calling a function. It happens to be the same function, but Python doesn't care about that, and neither should you. So Python simply continues: Call factorial with argument 1-1 = 0: Now, we've already seen this above, but Python does the simplest thing that could work: it calculates the answer from scratch: if n == 0 <-- this is true, so execute the "if" block: return 1 Now Python has a value it can assign to recurse, and so it can continue: recurse = factorial(n-1) = factorial(0) = 1 result = n*recurse = 1*1 = 1 return 1 Wait a second... a couple of lines back, I said n was 1, then I said it was 0, and now I'm claiming it is 1 again! What gives? The difference is that each call to factorial() gets its own local variable, n. When you call factorial(1), Python ends up calling the function twice: once with n=1, then BEFORE that calculation is finished, with n=0, then it falls back to the unfinished calculation. Here's an example that might explain things better: def factorial(n): print("Starting factorial calculation with n = %d" % n) if n == 0: print("Done with n = %d -- returning 1" % n) return 1 else: print("About to call factorial again...") recurse = factorial(n-1) result = n*recurse print("Done with n = %d -- returning %d" % (n, result)) return result And here it is in action: >>> factorial(0) Starting factorial calculation with n = 0 Done with n = 0 -- returning 1 1 >>> factorial(1) Starting factorial calculation with n = 1 About to call factorial again... Starting factorial calculation with n = 0 Done with n = 0 -- returning 1 Done with n = 1 -- returning 1 1 >>> >>> factorial(2) Starting factorial calculation with n = 2 About to call factorial again... Starting factorial calculation with n = 1 About to call factorial again... Starting factorial calculation with n = 0 Done with n = 0 -- returning 1 Done with n = 1 -- returning 1 Done with n = 2 -- returning 2 2 >>> >>> >>> factorial(5) Starting factorial calculation with n = 5 About to call factorial again... Starting factorial calculation with n = 4 About to call factorial again... Starting factorial calculation with n = 3 About to call factorial again... Starting factorial calculation with n = 2 About to call factorial again... Starting factorial calculation with n = 1 About to call factorial again... Starting factorial calculation with n = 0 Done with n = 0 -- returning 1 Done with n = 1 -- returning 1 Done with n = 2 -- returning 2 Done with n = 3 -- returning 6 Done with n = 4 -- returning 24 Done with n = 5 -- returning 120 120 Notice the pattern of n values: the *first* n value that is used is the *last* value to be finished with. Hope this helps! -- Steven D'Aprano From davea at ieee.org Wed Oct 20 12:23:09 2010 From: davea at ieee.org (Dave Angel) Date: Wed, 20 Oct 2010 06:23:09 -0400 Subject: [Tutor] updating a Csv file In-Reply-To: <345467.46359.qm@web110711.mail.gq1.yahoo.com> References: <345467.46359.qm@web110711.mail.gq1.yahoo.com> Message-ID: <4CBEC30D.9010109@ieee.org> On 2:59 PM, Albert-Jan Roskam wrote: > Hi all, > > > > How can I update a csv file? I've written the code below, but it does > not work ("Error: line contains NULL byte"). I've never tried opening a > file in read and write mode (r+) at the same time before, so I suspect > that this is the culprit. Should I instead just open one file, write to > another, throw away the original and rename the new file? That seems > inefficient. > > > > import csv, string > > def updateLine(idVar, idValue, myCsv, newLine): > f = open(myCsv, "r+") > r = csv.reader(f) > w = csv.writer(f) > header = r.next() > idPos = header.index(idVar) > for row in r: > if row[idPos] == idValue: > row = newLine > w.writerow(row) > f.close() > > updateLine(idVar = "Id", > idValue = "hawxgXvbfu", > myCsv = "c:/temp/someCsv.csv", > newLine = [ch for ch in string.letters[0:9]]) > > Cheers!! > > Albert-Jan > In general, you don't want to update anything in place, unless the new items are guaranteed to be the same size as the old. So when you're looping through a list, if you replace one item with another, no problem, but if you insert or delete, then you should be doing it on a copy. In the file, the byte is your unit of measure. So if what you're writing might be a different size (smaller or larger), then don't do it in place. You may not be able to anyway, if the csv module can't handle it. For example, the file object 'f' has a position, which is set by either read or write. csv may assume that they can count on that position not changing from outside influences, in which case even same-size updates could fail. DaveA From norman at khine.net Wed Oct 20 15:03:48 2010 From: norman at khine.net (Norman Khine) Date: Wed, 20 Oct 2010 15:03:48 +0200 Subject: [Tutor] get_browser() in python Message-ID: is there an equivalent module like the php's get_browser() http://php.net/manual/en/function.get-browser.php function? thanks -- ?u?op ?p?sdn p,u?n? p??o? ??? ??s no? '?u???? s???? ??? pu? '?u??uo? ?q s,??? ??? %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From fomcl at yahoo.com Wed Oct 20 15:29:13 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 20 Oct 2010 06:29:13 -0700 (PDT) Subject: [Tutor] updating a Csv file In-Reply-To: <4CBEC30D.9010109@ieee.org> Message-ID: <315987.26531.qm@web110703.mail.gq1.yahoo.com> Hi Steven and Dave, Thanks for replying to me. Steven, I pasted the traceback below, along with some version info. I'm using Windows XP. I used the very same code as the one I posted before, including the string.letters bit. Btw, thanks for the little 'list' tip. Dave, what you say makes sense. I tried updating the old values with new values of exactly the same length (10 characters), but that didn't work either (I simply used [ch * 10 for ch in string.letters[0:9]] --list trick not possible here ;-)). I pasted the contents of my test csv file below the traceback. Anyway, thanks again for your time! Python 2.6 (r26:66721, Oct? 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. ? IDLE 2.6????? >>> Traceback (most recent call last): ? File "C:\Documents and Settings\UserXP\Bureaublad\test.py", line 18, in ??? newLine = [ch for ch in string.letters[0:9]]) ? File "C:\Documents and Settings\UserXP\Bureaublad\test.py", line 10, in updateLine ??? for row? in r: Error: line contains NULL byte >>> Id,NaamInstelling,Naam3,Straat,HuisNr,Postcode,Plaats,dummy MQrrSzDboW,XWvxiqlrEp,ERQewcVYva,ppBXnpeCOs,HTmVvHRVhH,KHvjNHIYeM,bcEMrYPmuB,w hawxgXvbfu,VqiCmTSwdD,GUcoNnXUyL,LJexEROxrN,aPIuRapjDS,YUNJHBmCsQ,mQWbajBxKm,ww JSIXUYMxMt,CNebFXwmtZ,GTHQMyYUwT,XgRdYuFtfY,WyIeoiqqnC,SpbJWgDsHo,ZEuIXNujUd,www hawxgXvbfu,VqiCmTSwdD,GUcoNnXUyL,LJexEROxrN,aPIuRapjDS,YUNJHBmCsQ,mQWbajBxKm,ww Cheers!! Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --- On Wed, 10/20/10, Dave Angel wrote: From: Dave Angel Subject: Re: [Tutor] updating a Csv file To: "Albert-Jan Roskam" Cc: "Python Mailing List" Date: Wednesday, October 20, 2010, 12:23 PM ? On 2:59 PM, Albert-Jan Roskam wrote: > Hi all, > > > > How can I update a csv file? I've written the code below, but it does > not work ("Error: line contains NULL byte"). I've never tried opening a > file in read and write mode (r+) at the same time before, so I suspect > that this is the culprit. Should I instead just open one file, write to > another, throw away the original and rename the new file? That seems > inefficient. > > > > import csv, string > > def updateLine(idVar, idValue, myCsv, newLine): >? ? ? f = open(myCsv, "r+") >? ? ? r = csv.reader(f) >? ? ? w = csv.writer(f) >? ? ? header = r.next() >? ? ? idPos = header.index(idVar) >? ? ? for row? in r: >? ? ? ? ? if row[idPos] == idValue: >? ? ? ? ? ? ? row = newLine >? ? ? ? ? ? ? w.writerow(row) >? ? ? f.close() > > updateLine(idVar = "Id", >? ? ? ? ? ???idValue = "hawxgXvbfu", >? ? ? ? ? ???myCsv = "c:/temp/someCsv.csv", >? ? ? ? ? ???newLine = [ch for ch in string.letters[0:9]]) > > Cheers!! > > Albert-Jan > In general, you don't want to update anything in place, unless the new items are guaranteed to be the same size as the old.? So when you're looping through a list, if you replace one item with another, no problem, but if you insert or delete, then you should be doing it on a copy. In the file, the byte is your unit of measure.? So if what you're writing might be a different size (smaller or larger), then don't do it in place.? You may not be able to anyway, if the csv module can't handle it.? For example, the file object 'f' has a position, which is set by either read or write.? csv may assume that they can count on that position not changing from outside influences, in which case even same-size updates could fail. DaveA -------------- next part -------------- An HTML attachment was scrubbed... URL: From norman at khine.net Wed Oct 20 16:40:11 2010 From: norman at khine.net (Norman Khine) Date: Wed, 20 Oct 2010 16:40:11 +0200 Subject: [Tutor] get_browser() in python In-Reply-To: References: Message-ID: thanks On Wed, Oct 20, 2010 at 4:12 PM, Greg wrote: > Forgot to send to list. > > On Wed, Oct 20, 2010 at 10:12 AM, Greg wrote: >> On Wed, Oct 20, 2010 at 9:03 AM, Norman Khine wrote: >>> is there an equivalent module like the php's get_browser() >>> http://php.net/manual/en/function.get-browser.php function? >>> >>> thanks >>> >>> -- >> >> get_browser() simply gets a browser's user agent string then looks it >> up in a file. ?So if you can have your app get the http headers (the >> User-Agent header), you can do this yourself. >> >> -- >> Greg Bair >> gregbair at gmail.com >> > > > > -- > Greg Bair > gregbair at gmail.com > -- ?u?op ?p?sdn p,u?n? p??o? ??? ??s no? '?u???? s???? ??? pu? '?u??uo? ?q s,??? ??? %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From autumnc90 at gmail.com Wed Oct 20 18:13:37 2010 From: autumnc90 at gmail.com (Autumn Cutter) Date: Wed, 20 Oct 2010 12:13:37 -0400 Subject: [Tutor] Writing Scripts. Message-ID: I just started learning Python last night and I'm a little stuck on how to write a Python script. I'm using Python 2.5.0 on a Mac OS X 10.6.4. I've tried using programs like Idle, Applescript and Textmate but I'm still unable to write a script and run it successfully. I'm wondering if I'm using the write program or if I understand correctly how to write and run scripts. Help! :( From shantanoo at gmail.com Wed Oct 20 18:46:23 2010 From: shantanoo at gmail.com (=?utf-8?B?4KS24KSC4KSk4KSo4KWC?=) Date: Wed, 20 Oct 2010 22:16:23 +0530 Subject: [Tutor] Writing Scripts. In-Reply-To: References: Message-ID: <4A01B37D-FE40-4BFE-916C-171F89A23017@gmail.com> Save the file. e.g. my_test_prog.py. Open 'terminal'. Run following command 'python my_test_prog.py' On my 10.6.4 version of Mac OSX, I get following version of python. === $ /usr/bin/python Python 2.6.1 (r261:67515, Dec 17 2009, 00:59:15) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin === On 20-Oct-2010, at 9:43 PM, Autumn Cutter wrote: > I just started learning Python last night and I'm a little stuck on how to write a Python script. > I'm using Python 2.5.0 on a Mac OS X 10.6.4. I've tried using programs like Idle, Applescript and Textmate but I'm still unable to write a script and run it successfully. I'm wondering if I'm using the write program or if I understand correctly how to write and run scripts. Help! :( > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Wed Oct 20 18:56:38 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 20 Oct 2010 17:56:38 +0100 Subject: [Tutor] Writing Scripts. References: Message-ID: "Autumn Cutter" wrote >I just started learning Python last night and I'm a little stuck on >how to write a Python script. > I'm using Python 2.5.0 on a Mac OS X 10.6.4. To create the program you use any standard text editor - although one that supports programming will be better. So you can use any of the programs you listed. All you need to do is create a new file containg your python code and save it with a sensible name and end it in .py So you could create a file called hello.py which contains the single line: print "Hello" After you save it open the Terminal application and navigate to the folder where you saved the file. ( ISTR that you can do that by dragging a folder from Finder onto the Terminal... if I'm dreaming use the cd command...) Now type python hello.py at the Unix prompt to get the code to run. You should see hello printed on screen. Try that and if it doesn't work tell us what happened, including the exact text of any error messages you see. There are better ways to run the programs once you get itall set up properly but for now we will be best to stick to basics! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Wed Oct 20 19:11:20 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 20 Oct 2010 18:11:20 +0100 Subject: [Tutor] updating a Csv file References: <4CBEC30D.9010109@ieee.org> <315987.26531.qm@web110703.mail.gq1.yahoo.com> Message-ID: "Albert-Jan Roskam" wrote > I tried updating the old values with new values of exactly the same > length (10 characters), > but that didn't work either > newLine = [ch for ch in string.letters[0:9]]) > Error: line contains NULL byte > Id,NaamInstelling,Naam3,Straat,HuisNr,Postcode,Plaats,dummy There are 8 field headingss here but newline is a list with 9 values... > MQrrSzDboW,XWvxiqlrEp,ERQewcVYva,ppBXnpeCOs,HTmVvHRVhH,KHvjNHIYeM,bcEMrYPmuB,w And this has 8 fields, but they are not all single characters, nor are they all 10 characters so I'm not sure how you think you are writing out the same number of characters? > ...Should I instead just open one file, write to > another, throw away the original and rename the new file? That seems > inefficient. This is by far the most common design pattern. It is safer and allows you to roll back to the old version in the event of an error (if you save the original as a .bak or similar). HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From ghashsnaga at gmail.com Wed Oct 20 20:06:11 2010 From: ghashsnaga at gmail.com (Ara Kooser) Date: Wed, 20 Oct 2010 12:06:11 -0600 Subject: [Tutor] csv DictReader/Writer question Message-ID: Hello all, I have a csv file (from a previous code output). It looks like this: Species2, Protein ID, E_value, Length, Hit From, Hit to, Protein ID2, Locus Tag, Start/Stop, Species Streptomyces sp. AA4, ZP_05482482, 2.8293600000000001e-140, 5256, 1824, 2249\n, ZP_05482482, StAA4_0101000304844, complement(NZ_ACEV01000078.1:25146..40916)4, Streptomyces sp. AA4: 0\n Streptomyces sp. AA4, ZP_05482482, 8.0333299999999997e-138, 5256, 123, 547\n, ZP_05482482, StAA4_0101000304844, complement(NZ_ACEV01000078.1:25146..40916)4, Streptomyces sp. AA4: 0\n Streptomyces sp. AA4, ZP_05482482, 1.08889e-124, 5256, 3539, 3956\n, ZP_05482482, StAA4_0101000304844, complement(NZ_ACEV01000078.1:25146..40916)4, Streptomyces sp. AA4: 0\n .... I want to removing certain sections in each line so I wrote this code using csv.DictWriter: import csv data = csv.DictReader(open('strep_aa.txt')) for x in data: del x['Species2'] del x[' Protein ID2'] print x When it prints to the screen everything works great: {' Hit From': ' 1824', ' Hit to': ' 2249\\n', ' Protein ID': ' ZP_05482482', ' Locus Tag': ' StAA4_0101000304844', ' Start/Stop': ' complement(NZ_ACEV01000078.1:25146..40916)4', ' Species': ' Streptomyces sp. AA4: 0\\n', ' Length': ' 5256', ' E_value': ' 2.8293600000000001e-140'} {' Hit From': ' 123', ' Hit to': ' 547\\n', ' Protein ID': ' ZP_05482482', ' Locus Tag': ' StAA4_0101000304844', ' Start/Stop': ' complement(NZ_ACEV01000078.1:25146..40916)4', ' Species': ' Streptomyces sp. AA4: 0\\n', ' Length': ' 5256', ' E_value': ' 8.0333299999999997e-138'} What I don't know how to do is the export this back out a csv file and rearrange each key as a column header so it work look like this: Species Protein ID E Value ..... I thought csv.DictWriter would be the way to go since it writes dictionaries to text files. I was wondering how do I go about doing this? I don't really understand the syntax. Thank you! Ara -- Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis an sub cardine glacialis ursae. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dkuhlman at rexx.com Wed Oct 20 23:03:15 2010 From: dkuhlman at rexx.com (Dave Kuhlman) Date: Wed, 20 Oct 2010 14:03:15 -0700 Subject: [Tutor] Problem with python In-Reply-To: References: Message-ID: <20101020210315.GA55297@cutter.rexx.com> On Wed, Oct 20, 2010 at 03:02:43AM +0600, Matthew Nunes wrote: > > To whom it may concern, > > Hi, I've just started learning how > to program in python using Allan B. Downy's book "How to think like a > computer scientist" and it explained something in the recursion > chapter which have still been unable to understand. It wrote a piece > of code for the factorial function in math for example 3! is 3 * 2 * > 1. I cannot understand the how it claimed the code executed, and > logically it makes no sense to me. Please explain it to me, as I have > spent hours trying to get my head around it, as the book(in my > opinion) did not explain it well. Here it is: > > > def factorial(n): > > if n == 0: > > return 1 > > else: > > recurse = factorial(n-1) > > result = n * recurse > return result > > If there is and easier piece of code that you know of that can be used > for factorial, if you could please also tell me that. > > Thank you for your time. The other replies have helped you understand that recursive function. But, recursion gets especially interesting when it is applied to recursive data structures, for example data whose structure is the same as the data that it contains. A tree structure whose nodes all have the same structure is a good example. The code that follows constructs a tree out of objects that are instances of class Node, then walks that tree and prints it out. This code provides two recursive ways to walk and display that tree: (1) the "show" method in class Node and (2) the "shownode" function. Often processing recursive structures like trees is trivial with a recursive function or method. Keep in mind that this code is well behaved only when the data structure we apply it to has a reasonable maximum depth. Hope this example helps. - Dave # ============================================================ class Node(object): def __init__(self, data, children=None): self.data = data if children is None: self.children = [] else: self.children = children def show(self, level): print '%sdata: %s' % (' ' * level, self.data, ) for child in self.children: child.show(level + 1) def shownode(node, level): print '%sdata: %s' % (' ' * level, node.data, ) for child in node.children: child.show(level + 1) def test(): n1 = Node('aaaa') n2 = Node('bbbb') n3 = Node('cccc') n4 = Node('dddd') n5 = Node('eeee', [n1, n2]) n6 = Node('ffff', [n3, n4]) n7 = Node('gggg', [n5, n6]) n7.show(0) print '=' * 20 shownode(n7, 0) test() # ============================================================ -- Dave Kuhlman http://www.rexx.com/~dkuhlman From timomlists at gmail.com Thu Oct 21 00:15:28 2010 From: timomlists at gmail.com (Timo) Date: Thu, 21 Oct 2010 00:15:28 +0200 Subject: [Tutor] Stopping a webservice Message-ID: Hello, I have written a Python script that constantly checks for incoming connections. It is a class and runs a while loop until shutdown is set to True. Something like: class Connection(object): def __init__(self): self.shutdown = False def mainloop(self): while not self.shutdown: print "We keep checking..." I have written a GUI and when I click the Quit-button, I do "connection.shutdown = True", which works great. The plans are to run this on my webserver. I created a webpage with a start and stop button, the starting works great. But how could I remember this class to call "class.shutdown = True" any given time when I press the stop button? Cheers, Timo -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Oct 21 00:50:24 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 20 Oct 2010 23:50:24 +0100 Subject: [Tutor] Stopping a webservice References: Message-ID: "Timo" wrote > I have written a GUI and when I click the Quit-button, I do > "connection.shutdown = True", which works great. > > The plans are to run this on my webserver. I created a webpage with > a start > and stop button, the starting works great. But how could I remember > this > class to call "class.shutdown = True" any given time when I press > the stop > button? There arec several ways but one would be to pass the connection object ID as a cookie and then when Quiting usend the cookie data to the server which ises it to referenbce the connection object - a dictionary might be useful here... Thats assuming you havf multiple connection objects running, maybe even one per client sesssion. If its a global conmnection objerct then just store it as a global object (maybe in its own module imported by all other modules?) There are several other options but those are probably the two simplest depending on your use case. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From benderjacob44 at gmail.com Thu Oct 21 01:30:32 2010 From: benderjacob44 at gmail.com (Jacob Bender) Date: Wed, 20 Oct 2010 19:30:32 -0400 Subject: [Tutor] LCM Message-ID: <4CBF7B98.1060706@gmail.com> Hello all, I was wondering if you could please help me with an issue I'm having with my program. I'm not fond of LCM(Least Common Multiples), and I decided to make a program that deals with them. Here's the code: thing = raw_input("What is the first number?\n\n") thing2 = raw_input("What is the second number?\n\n") int(thing) int(thing2) x = 1 thing3 = x/thing thing4 = thing2/x def calc(): while True: if isinstance(thing3, int) == True: if isinstance(thing4, int)== True: print "The LCM is" print x raw_input() break else: x + 1 calc() else: x + 1 calc() calc() The only problem is that when I tell it that thing3 = x/thing it gives an error. Here it is: Traceback (most recent call last): File "C:/Documents and Settings/Family/My Documents/LCM.py", line 10, in thing3 = x/thing TypeError: unsupported operand type(s) for /: 'int' and 'str' Same with thing4... Can you please help! Thanks in advance... From christopher.henk at allisontransmission.com Thu Oct 21 02:01:59 2010 From: christopher.henk at allisontransmission.com (christopher.henk at allisontransmission.com) Date: Wed, 20 Oct 2010 20:01:59 -0400 Subject: [Tutor] LCM In-Reply-To: <4CBF7B98.1060706@gmail.com> Message-ID: benderjacob44 at gmail.com wrote on 10/20/2010 07:30:32 PM: > > thing = raw_input("What is the first number?\n\n") > > thing2 = raw_input("What is the second number?\n\n") > > int(thing) > int(thing2) > The two lines above do the calculation of turning your input strings into int's but do not save the value anywhere. You want to assign the calculation to a variable (can reuse thing and thing2 if you wish) and then use that calculation later on. > > The only problem is that when I tell it that thing3 = x/thing it gives > an error. Here it is: > > Traceback (most recent call last): > File "C:/Documents and Settings/Family/My Documents/LCM.py", line 10, > in > thing3 = x/thing > TypeError: unsupported operand type(s) for /: 'int' and 'str' the error here is telling you that thing is a string and python cannot divide an int by a string. > > Same with thing4... > > Can you please help! Thanks in advance... I didn't check the rest of the program. But that should at least get you moving along. Chris -------------- next part -------------- An HTML attachment was scrubbed... URL: From anand.shashwat at gmail.com Thu Oct 21 03:39:59 2010 From: anand.shashwat at gmail.com (Shashwat Anand) Date: Thu, 21 Oct 2010 07:09:59 +0530 Subject: [Tutor] LCM In-Reply-To: <4CBF7B98.1060706@gmail.com> References: <4CBF7B98.1060706@gmail.com> Message-ID: On Thu, Oct 21, 2010 at 5:00 AM, Jacob Bender wrote: > Hello all, > > I was wondering if you could please help me with an issue I'm having with > my program. I'm not fond of LCM(Least Common Multiples), and I decided to > make a program that deals with them. Here's the code: > > thing = raw_input("What is the first number?\n\n") > > thing2 = raw_input("What is the second number?\n\n") > > int(thing) > int(thing2) > > x = 1 > > thing3 = x/thing > thing4 = thing2/x > > def calc(): > while True: > > if isinstance(thing3, int) == True: > if isinstance(thing4, int)== True: > print "The LCM is" > print x > raw_input() > break > else: > x + 1 > calc() > > > else: > x + 1 > calc() > > calc() > > Why this much bother. Use: import fractions solution = int(thing) * int(thing2) / ( fraction.gcd(int(thing), int(thing2)) ) The property being used is for two numbers a,b ; lcm(a, b)* gcd(a, b) = a * b -- ~l0nwlf -------------- next part -------------- An HTML attachment was scrubbed... URL: From racingdirttracksteven666 at hotmail.com Thu Oct 21 04:38:10 2010 From: racingdirttracksteven666 at hotmail.com (steven nickerson) Date: Wed, 20 Oct 2010 23:38:10 -0300 Subject: [Tutor] Importing photo-python Message-ID: I would like to somehow import and output a photo to the screen via tkinter window not very described sorry, I just would like to know how i can get photos into python ex. .gif (In photo format not video), .jpg ect. -------------- next part -------------- An HTML attachment was scrubbed... URL: From mishradeepak945 at gmail.com Thu Oct 21 06:44:10 2010 From: mishradeepak945 at gmail.com (deepak mishra) Date: Thu, 21 Oct 2010 10:14:10 +0530 Subject: [Tutor] deepak mishra has invited you to open a Google mail account Message-ID: I've been using Gmail and thought you might like to try it out. Here's an invitation to create an account. You're Invited to Gmail! deepak mishra has invited you to open a Gmail account. Gmail is Google's free email service, built on the idea that email can be intuitive, efficient, and fun. Gmail has: *Less spam* Keep unwanted messages out of your inbox with Google's innovative technology. *Lots of space* Enough storage so that you'll never have to delete another message. *Built-in chat* Text or video chat with deepak mishra and other friends in real time. *Mobile access* Get your email anywhere with Gmail on your mobile phone. You can even import your contacts and email from Yahoo!, Hotmail, AOL, or any other web mail or POP accounts. Once you create your account, deepak mishra will be notified of your new Gmail address so you can stay in touch. Learn moreor get started ! Sign up Google Inc. | 1600 Ampitheatre Parkway | Mountain View, California 94043 -------------- next part -------------- An HTML attachment was scrubbed... URL: From connor.cn at gmail.com Thu Oct 21 08:55:48 2010 From: connor.cn at gmail.com (Connor Neblett) Date: Thu, 21 Oct 2010 08:55:48 +0200 Subject: [Tutor] Writing Scripts. In-Reply-To: <4A01B37D-FE40-4BFE-916C-171F89A23017@gmail.com> References: <4A01B37D-FE40-4BFE-916C-171F89A23017@gmail.com> Message-ID: Alternately, I believe that MacPython comes packaged with an application called "Python Launher". If you right click a python file (.py) once this is app is installed, you can select "Open With -> Python Launcher" This method is more for people that are uncomfortable with the command line (Terminal.app) As a side note, I would recommend installing a newer version of Python. (3.1.2) On Oct 20, 2010, at 6:46 PM, ????? wrote: > Save the file. e.g. my_test_prog.py. Open 'terminal'. Run following command > > 'python my_test_prog.py' > > On my 10.6.4 version of Mac OSX, I get following version of python. > === > $ /usr/bin/python > Python 2.6.1 (r261:67515, Dec 17 2009, 00:59:15) > [GCC 4.2.1 (Apple Inc. build 5646)] on darwin > === > > > On 20-Oct-2010, at 9:43 PM, Autumn Cutter wrote: > >> I just started learning Python last night and I'm a little stuck on how to write a Python script. >> I'm using Python 2.5.0 on a Mac OS X 10.6.4. I've tried using programs like Idle, Applescript and Textmate but I'm still unable to write a script and run it successfully. I'm wondering if I'm using the write program or if I understand correctly how to write and run scripts. Help! :( >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Thu Oct 21 10:05:23 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 21 Oct 2010 09:05:23 +0100 Subject: [Tutor] Importing photo-python References: Message-ID: "steven nickerson" wrote i > I would like to somehow import and output a photo to the screen via > tkinter window Check out the PhotoImage widget. It can be inserted into a Text field or Canvas. HTH, Alan G. From alan.gauld at btinternet.com Thu Oct 21 10:14:39 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 21 Oct 2010 09:14:39 +0100 Subject: [Tutor] LCM References: <4CBF7B98.1060706@gmail.com> Message-ID: "Jacob Bender" wrote > decided to make a program that deals with them. Here's the code: > > thing = raw_input("What is the first number?\n\n") > thing2 = raw_input("What is the second number?\n\n") > > int(thing) > int(thing2) This does nothing useful. It converts the strings to numbers which are immediately thrown away because they are not assigned to anything. > x = 1 > > thing3 = x/thing > thing4 = thing2/x This will likely fail because things are still strings. But even if they were numbers itwould be clearer in this case to not use x, just put the 1 directly in the calculation. > def calc(): > while True: > > if isinstance(thing3, int) == True: > if isinstance(thing4, int)== True: > print "The LCM is" > print x So any time thing3 and 4 are integers x will be 1 > raw_input() > break > else: > x + 1 Again this does nothing useful because it adds one to x then throws away the value > calc() This will cause an infinite loop (until Python runs out of recursion space). I'm not even sure what you think this will achieve? > else: > x + 1 > calc() See above... > The only problem is that when I tell it that thing3 = x/thing it > gives an error. Here it is: > > Traceback (most recent call last): > File "C:/Documents and Settings/Family/My Documents/LCM.py", line > 10, in > thing3 = x/thing > TypeError: unsupported operand type(s) for /: 'int' and 'str' See above comments. You need to retyhink your algorithm and pay attention to assigning results to variables. And once you have done that you may want to make x a parameter of calc(), or at least declare it a global variable. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From __peter__ at web.de Thu Oct 21 10:24:10 2010 From: __peter__ at web.de (Peter Otten) Date: Thu, 21 Oct 2010 10:24:10 +0200 Subject: [Tutor] csv DictReader/Writer question References: Message-ID: Ara Kooser wrote: > Hello all, > > I have a csv file (from a previous code output). It looks like this: > Species2, Protein ID, E_value, Length, Hit From, Hit to, Protein ID2, > Locus Tag, Start/Stop, Species > Streptomyces sp. AA4, ZP_05482482, 2.8293600000000001e-140, 5256, > 1824, > 2249\n, ZP_05482482, StAA4_0101000304844, > complement(NZ_ACEV01000078.1:25146..40916)4, Streptomyces sp. AA4: 0\n > Streptomyces sp. AA4, ZP_05482482, 8.0333299999999997e-138, 5256, 123, > 547\n, ZP_05482482, StAA4_0101000304844, > complement(NZ_ACEV01000078.1:25146..40916)4, Streptomyces sp. AA4: 0\n > Streptomyces sp. AA4, ZP_05482482, 1.08889e-124, 5256, 3539, 3956\n, > ZP_05482482, StAA4_0101000304844, > complement(NZ_ACEV01000078.1:25146..40916)4, Streptomyces sp. AA4: 0\n > .... > > I want to removing certain sections in each line so I wrote this code > using csv.DictWriter: > import csv > data = csv.DictReader(open('strep_aa.txt')) > > for x in data: > del x['Species2'] > del x[' Protein ID2'] > print x > > When it prints to the screen everything works great: > {' Hit From': ' 1824', ' Hit to': ' 2249\\n', ' Protein ID': ' > ZP_05482482', ' Locus Tag': ' StAA4_0101000304844', ' Start/Stop': ' > complement(NZ_ACEV01000078.1:25146..40916)4', ' Species': ' Streptomyces > sp. AA4: 0\\n', ' Length': ' 5256', ' E_value': ' > 2.8293600000000001e-140'} > {' Hit From': ' 123', ' Hit to': ' 547\\n', ' Protein ID': ' > ZP_05482482', ' Locus Tag': ' StAA4_0101000304844', ' Start/Stop': ' > complement(NZ_ACEV01000078.1:25146..40916)4', ' Species': ' Streptomyces > sp. AA4: 0\\n', ' Length': ' 5256', ' E_value': ' > 8.0333299999999997e-138'} > > What I don't know how to do is the export this back out a csv file and > rearrange each key as a column header so it work look like this: > Species Protein ID E Value ..... > > I thought csv.DictWriter would be the way to go since it writes > dictionaries to text files. I was wondering how do I go about doing this? > I don't really understand the syntax. It's not about syntax, you have to read the docs for the csv module carefully to find the information that is relevant for your problem. For starters you could try to find out what the skipinitialspace and extrasaction keyword parameters are supposed to do in the example below: with open(source, "rb") as instream: reader = csv.DictReader(instream, skipinitialspace=True) destfieldnames = list(reader.fieldnames) destfieldnames.remove("Species2") destfieldnames.remove("Protein ID2") with open(dest, "wb") as outstream: writer = csv.DictWriter(outstream, destfieldnames, extrasaction="ignore") writer.writer.writerow(destfieldnames) writer.writerows(reader) Can you find the line where I'm writing the headers to the destination file? Peter From rdmoores at gmail.com Thu Oct 21 14:42:03 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 21 Oct 2010 05:42:03 -0700 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? Message-ID: Traceback (most recent call last): File "c:\P26Working\test_urllib2_21a.py", line 148, in unchanged_count, higher_count, lower_count, secs = sleep_seconds_control(unchanged_count, higher_count, lower_count, secs) TypeError: 'int' object is not iterable I'm working on a script that keeps track of the USD -> Japanese Yen exchange rate. I'm experimenting with adding functionality that changes the seconds to sleep between web scrapes, depending on consecutive outputs of no change in the exchange rate. Please see the code at I'm at a loss as to what the error means. Thanks, Dick Moores From mail at timgolden.me.uk Thu Oct 21 14:52:17 2010 From: mail at timgolden.me.uk (Tim Golden) Date: Thu, 21 Oct 2010 13:52:17 +0100 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: References: Message-ID: <4CC03781.4010100@timgolden.me.uk> On 21/10/2010 13:42, Richard D. Moores wrote: > Traceback (most recent call last): > File "c:\P26Working\test_urllib2_21a.py", line 148, in > unchanged_count, higher_count, lower_count, secs = > sleep_seconds_control(unchanged_count, higher_count, lower_count, > secs) > TypeError: 'int' object is not iterable > > I'm working on a script that keeps track of the USD -> Japanese Yen > exchange rate. I'm experimenting with adding functionality that > changes the seconds to sleep between web scrapes, depending on > consecutive outputs of no change in the exchange rate. Please see the > code at sleep_seconds_control is returning an integer. You're trying to assign that integer to the four names: unchanged_count, higher_count, lower_count, secs Python therefore tries to iterate over the integer to allocate one item to each of the four name. And it can't. Because it's an integer TJG From emmanuel.ruellan at laposte.net Thu Oct 21 15:00:36 2010 From: emmanuel.ruellan at laposte.net (Emmanuel Ruellan) Date: Thu, 21 Oct 2010 15:00:36 +0200 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: References: Message-ID: On Thu, Oct 21, 2010 at 2:42 PM, Richard D. Moores wrote: Traceback (most recent call last): File "c:\P26Working\test_urllib2_ > > 21a.py", line 148, in > unchanged_count, higher_count, lower_count, secs = > sleep_seconds_control(unchanged_count, higher_count, lower_count, > secs) > TypeError: 'int' object is not iterable > > I'm working on a script that keeps track of the USD -> Japanese Yen > exchange rate. I'm experimenting with adding functionality that > changes the seconds to sleep between web scrapes, depending on > consecutive outputs of no change in the exchange rate. Please see the > code at > > I'm at a loss as to what the error means. In line 148, you are trying to unpack the value returned by function sleep_seconds_control, as if it were returning a tuple, a list or some sort of iterable, whereas in lines 44 to 49, you defined it as returning a single value. -- Emmanuel -------------- next part -------------- An HTML attachment was scrubbed... URL: From eloyz.email at gmail.com Thu Oct 21 15:05:53 2010 From: eloyz.email at gmail.com (Eloy Zuniga Jr.) Date: Thu, 21 Oct 2010 08:05:53 -0500 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: References: Message-ID: One of your variables (e.g. unchanged_count, higher_count, lower_count, secs) is an integer and your method expects a sequence (iterable) value. Examples of iterable values: 1. str 2. unicode 3. list 4. tuple 5. xrange and range On Thu, Oct 21, 2010 at 7:42 AM, Richard D. Moores wrote: > Traceback (most recent call last): > File "c:\P26Working\test_urllib2_21a.py", line 148, in > unchanged_count, higher_count, lower_count, secs = > sleep_seconds_control(unchanged_count, higher_count, lower_count, > secs) > TypeError: 'int' object is not iterable > > I'm working on a script that keeps track of the USD -> Japanese Yen > exchange rate. I'm experimenting with adding functionality that > changes the seconds to sleep between web scrapes, depending on > consecutive outputs of no change in the exchange rate. Please see the > code at > > I'm at a loss as to what the error means. > > Thanks, > > Dick Moores > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Eloy Zuniga Jr. www.eloyz.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From eloyz.email at gmail.com Thu Oct 21 15:09:16 2010 From: eloyz.email at gmail.com (Eloy Zuniga Jr.) Date: Thu, 21 Oct 2010 08:09:16 -0500 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: <4CC03781.4010100@timgolden.me.uk> References: <4CC03781.4010100@timgolden.me.uk> Message-ID: Forget what I said. Tim's right. Return an iterable from within your method not an integer. On Thu, Oct 21, 2010 at 7:52 AM, Tim Golden wrote: > On 21/10/2010 13:42, Richard D. Moores wrote: > >> Traceback (most recent call last): >> File "c:\P26Working\test_urllib2_21a.py", line 148, in >> unchanged_count, higher_count, lower_count, secs = >> sleep_seconds_control(unchanged_count, higher_count, lower_count, >> secs) >> TypeError: 'int' object is not iterable >> >> I'm working on a script that keeps track of the USD -> Japanese Yen >> exchange rate. I'm experimenting with adding functionality that >> changes the seconds to sleep between web scrapes, depending on >> consecutive outputs of no change in the exchange rate. Please see the >> code at >> > > sleep_seconds_control is returning an integer. > > You're trying to assign that integer to the four names: > > unchanged_count, higher_count, lower_count, secs > > Python therefore tries to iterate over the integer to > allocate one item to each of the four name. > > And it can't. Because it's an integer > > TJG > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Eloy Zuniga Jr. www.eloyz.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From cwitts at compuscan.co.za Thu Oct 21 15:12:31 2010 From: cwitts at compuscan.co.za (Christian Witts) Date: Thu, 21 Oct 2010 15:12:31 +0200 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: References: Message-ID: <4CC03C3F.9090803@compuscan.co.za> On 21/10/2010 14:42, Richard D. Moores wrote: > Traceback (most recent call last): > File "c:\P26Working\test_urllib2_21a.py", line 148, in > unchanged_count, higher_count, lower_count, secs = > sleep_seconds_control(unchanged_count, higher_count, lower_count, > secs) > TypeError: 'int' object is not iterable > > I'm working on a script that keeps track of the USD -> Japanese Yen > exchange rate. I'm experimenting with adding functionality that > changes the seconds to sleep between web scrapes, depending on > consecutive outputs of no change in the exchange rate. Please see the > code at > > I'm at a loss as to what the error means. > > Thanks, > > Dick Moores > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > You are expecting 4 items back from the sleep_seconds_control function but you only return 1 (seconds). The left-hand side should either just be `secs = ...` or you should change your function to return all 4 items. What the error means is that the interpreter is trying to iterate over the results from your function in order to divide it into the 4 variables on the left-hand side. -- Kind Regards, Christian Witts From alan.gauld at btinternet.com Thu Oct 21 16:39:37 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Thu, 21 Oct 2010 15:39:37 +0100 (BST) Subject: [Tutor] Stopping a webservice In-Reply-To: References: Message-ID: <131389.83189.qm@web86704.mail.ird.yahoo.com> Forwarding to list.... "Timo" wrote >> >> >>The plans are to run this on my webserver. I created a webpage with a start >>>and stop button, the starting works great. But how could I remember this >>>class to call "class.shutdown = True" any given time when I press the stop >>>button? >>> > There arec several ways but one would be to pass the connection >>object ID as a cookie and then when Quiting usend the cookie data to the >>server which ises it to referenbce the connection object - a dictionary >>might be useful here... >> Hello Alan, Could you give me a bit more information please? On my webpage, when I press start, the following is run: def start_server(name, port): connection = Connection(name, port) So when I close this page, I loose "connection". How could I put this in a dictionary or cookie for later reference? I'm not sure I understand your issue? What part of 'put this in a dictionary' do you not understand? mydict[(port,name)] = connection would be one way... And when you say on your web page the following is run... Do you mean the function is executed on the browser? Or do you mean you submit some information to the server which executes the code? If so that begs the question which web framework you are using? Somehow you need to create a mechanism for retrieving the connection. That retrieval must be based on some kind of name/key that can be stored or recreated by the web client. A dictionary is only one possibility. Alan G. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Oct 21 16:43:05 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 21 Oct 2010 15:43:05 +0100 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? References: Message-ID: "Richard D. Moores" wrote > unchanged_count, higher_count, lower_count, secs = > sleep_seconds_control(unchanged_count, higher_count, lower_count, > secs) > TypeError: 'int' object is not iterable > > I'm at a loss as to what the error means. a.. def sleep_seconds_control(unchanged_count, higher_count, lower_count, secs): a.. if unchanged_count >= 2: a.. secs += 5 a.. else: a.. secs = 10 a.. return secs You return secs You try to assign that single value to 4 variables. Python tries to do it and finds that it can't break secs into 4 bits because its not a sequence(not iterable). HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From rdmoores at gmail.com Thu Oct 21 18:09:27 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 21 Oct 2010 09:09:27 -0700 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: References: Message-ID: Wow! What great help! Thanks! In case anyone's interested, the script now works the way I wanted: . It should work for anyone with 2.6 or 2.7 I'd appreciate constructive criticism. Dick From ghashsnaga at gmail.com Thu Oct 21 22:50:41 2010 From: ghashsnaga at gmail.com (Ara Kooser) Date: Thu, 21 Oct 2010 14:50:41 -0600 Subject: [Tutor] csv DictReader/Writer question Message-ID: Peter, Thank you for your response. I did try reading the documentation but I missed something (or several somethings in this case). So what I see in the code you supplied is: with open(source, "rb") as instream: reader = csv.DictReader(instream, skipinitialspace=True) destfieldnames = list(reader.fieldnames) destfieldnames.remove("Species2") destfieldnames.remove("Protein ID2") So this reads the csv file in but I need to run it to see what skipinitialspace does. Then it reads in the header line and removes the Species2 and Protein ID2. Does this remove all the data associated with those columns? For some reason I thought I had to bring these into a dictionary to manipulate them. with open(dest, "wb") as outstream: writer = csv.DictWriter(outstream, destfieldnames,extrasaction="ignore") writer.writer.writerow(destfieldnames) writer.writerows(reader) I think the first line after the open writes the field names to the file and the follow lines write the data is that correct? I am going to run the code as soon as I get home. ara -- Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis an sub cardine glacialis ursae. -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Thu Oct 21 23:21:13 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Thu, 21 Oct 2010 23:21:13 +0200 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: References: Message-ID: On 21 October 2010 18:09, Richard D. Moores wrote: > In case anyone's interested, the script now works the way I wanted: > . It should work for anyone > with 2.6 or 2.7 > > I'd appreciate constructive criticism. Instead of all the zeros2float craziness use string formatting. It will take care of rounding and the number of decimal places. >>> import datetime >>> lowz = 81.7499997345 >>> timestamp = datetime.datetime.now().strftime("%H:%M:%S") >>> print "NEW LOW: %.4f at %s" % (lowz, timestamp) NEW LOW: 81.7500 at 22:55:13 If you want to control the number of decimal places in the string formatting do something like: >>> i = 3 >>> print ("NEW LOW: %%.%sf at %%s" % i) % (lowz, timestamp) NEW LOW: 81.750 at 22:55:13 >>> i = 6 >>> print ("NEW LOW: %%.%sf at %%s" % i) % (lowz, timestamp) NEW LOW: 81.750000 at 22:55:13 Greets Sander From rdmoores at gmail.com Fri Oct 22 00:44:33 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 21 Oct 2010 15:44:33 -0700 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: References: Message-ID: On Thu, Oct 21, 2010 at 14:21, Sander Sweers wrote: > On 21 October 2010 18:09, Richard D. Moores wrote: >> I'd appreciate constructive criticism. > > Instead of all the zeros2float craziness use string formatting. It > will take care of rounding and the number of decimal places. > >>>> import datetime >>>> lowz = 81.7499997345 >>>> timestamp = datetime.datetime.now().strftime("%H:%M:%S") >>>> print "NEW LOW: %.4f at %s" % (lowz, timestamp) > NEW LOW: 81.7500 at 22:55:13 > > If you want to control the number of decimal places in the string > formatting do something like: > >>>> i = 3 >>>> print ("NEW LOW: %%.%sf at %%s" % i) % (lowz, timestamp) > NEW LOW: 81.750 at 22:55:13 >>>> i = 6 >>>> print ("NEW LOW: %%.%sf at %%s" % i) % (lowz, timestamp) > NEW LOW: 81.750000 at 22:55:13 Thanks very much for the string formatting instruction. Dick From rdmoores at gmail.com Fri Oct 22 15:42:50 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Fri, 22 Oct 2010 06:42:50 -0700 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: References: Message-ID: On Thu, Oct 21, 2010 at 15:44, Richard D. Moores wrote: >> If you want to control the number of decimal places in the string >> formatting do something like: >> >>>>> i = 3 >>>>> print ("NEW LOW: %%.%sf at %%s" % i) % (lowz, timestamp) >> NEW LOW: 81.750 at 22:55:13 >>>>> i = 6 >>>>> print ("NEW LOW: %%.%sf at %%s" % i) % (lowz, timestamp) >> NEW LOW: 81.750000 at 22:55:13 > > Thanks very much for the string formatting instruction. So I wrote a function: def float2n_decimals(floatt, n): """ Given a float (floatt), return floatt to n decimal places. E.g., with n = 2, 81.34567 -> 81.35 """ return ("%%.%sf" % n) % floatt which works fine, but a question remains: n is an integer. Why the 's' in '%sf'? (Also, please suggest a better name for this function.) See this latest revision at . Dick From smokefloat at gmail.com Fri Oct 22 15:47:40 2010 From: smokefloat at gmail.com (David Hutto) Date: Fri, 22 Oct 2010 09:47:40 -0400 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: References: Message-ID: On Fri, Oct 22, 2010 at 9:42 AM, Richard D. Moores wrote: > On Thu, Oct 21, 2010 at 15:44, Richard D. Moores wrote: >>> If you want to control the number of decimal places in the string >>> formatting do something like: >>> >>>>>> i = 3 >>>>>> print ("NEW LOW: %%.%sf at %%s" % i) % (lowz, timestamp) >>> NEW LOW: 81.750 at 22:55:13 >>>>>> i = 6 >>>>>> print ("NEW LOW: %%.%sf at %%s" % i) % (lowz, timestamp) >>> NEW LOW: 81.750000 at 22:55:13 >> >> Thanks very much for the string formatting instruction. > > So I wrote a function: > > def float2n_decimals(floatt, n): > ? ?""" > ? ?Given a float (floatt), return floatt to n decimal places. > > ? ?E.g., with n = 2, 81.34567 -> 81.35 > ? ?""" > ? ?return ("%%.%sf" % n) % floatt > > which works fine, but a question remains: n is an integer. Why the 's' > in '%sf'? Right here: http://docs.python.org/release/2.5.2/lib/typesseq-strings.html > > (Also, please suggest a better name for this function.) > > See this latest revision at . > > Dick > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From rdmoores at gmail.com Fri Oct 22 15:55:08 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Fri, 22 Oct 2010 06:55:08 -0700 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: References: Message-ID: On Fri, Oct 22, 2010 at 06:47, David Hutto wrote: >> which works fine, but a question remains: n is an integer. Why the 's' >> in '%sf'? > > Right here: > http://docs.python.org/release/2.5.2/lib/typesseq-strings.html Sorry, but I don't see the answer to my question there. Dick From __peter__ at web.de Fri Oct 22 16:14:40 2010 From: __peter__ at web.de (Peter Otten) Date: Fri, 22 Oct 2010 16:14:40 +0200 Subject: [Tutor] csv DictReader/Writer question References: Message-ID: Ara Kooser wrote: > Thank you for your response. I did try reading the documentation but I > missed something (or several somethings in this case). So what I see in > the code you supplied is: > > with open(source, "rb") as instream: > reader = csv.DictReader(instream, skipinitialspace=True) > > destfieldnames = list(reader.fieldnames) > destfieldnames.remove("Species2") > destfieldnames.remove("Protein ID2") > > So this reads the csv file in but I need to run it to see what > skipinitialspace does. Your csv header looks like Field1, Field2, Field3, ... When you feed that to the DictReader you get fieldnames ["Field1", " Field2", " Field3", ...] skipinitialspace advises the DictReader to remove the leading spaces, i. e. you get ["Field1", "Field2", "Field3", ...] instead. > Then it reads in the header line and removes the > Species2 and Protein ID2. Does this remove all the data associated with > those columns? For some reason I thought I had to bring these into a > dictionary to manipulate them. destfieldnames is the list of field names that will be written to the output file. I construct it by making a copy of the list of field names of the source and then removing the two names of the columns that you don't want in the output. Alternatively you could use a constant list like destfieldnames = ["Field2", "Field5, "Field7"] to handpick the columns. > with open(dest, "wb") as outstream: > writer = csv.DictWriter(outstream, > destfieldnames,extrasaction="ignore") The following line uses the csv.writer instance wrapped by the csv.DictWriter to write the header. > writer.writer.writerow(destfieldnames) The line below iterates over the rows in the source file and writes them into the output file. > writer.writerows(reader) A more verbose way to achieve the same thing would be for row in reader: writer.writerow(row) Remember that row is a dictionary that has items that shall not be copied into the output? By default the DictWriter raises an exception if it encounters such extra items. But we told it to silently ignore them with extrasaction="ignore". > I think the first line after the open writes the field names to the file > and the follow lines write the data is that correct? I am going to run the > code as soon as I get home. Come back if you have more questions. Peter From smokefloat at gmail.com Fri Oct 22 16:17:26 2010 From: smokefloat at gmail.com (David Hutto) Date: Fri, 22 Oct 2010 10:17:26 -0400 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: References: Message-ID: On Fri, Oct 22, 2010 at 9:55 AM, Richard D. Moores wrote: > On Fri, Oct 22, 2010 at 06:47, David Hutto wrote: >>> which works fine, but a question remains: n is an integer. Why the 's' >>> in '%sf'? >> >> Right here: >> http://docs.python.org/release/2.5.2/lib/typesseq-strings.html > > Sorry, but I don't see the answer to my question there. Usually the docs explain it better than I do, which is why I prefer references. But short answer is, if you'll look in the chart on the page given above, it states : %s String (converts any python object using str()). So each % tells the string what to expect while interlacing that which is given at the end. of the string after the final % > > Dick > From smokefloat at gmail.com Fri Oct 22 16:19:11 2010 From: smokefloat at gmail.com (David Hutto) Date: Fri, 22 Oct 2010 10:19:11 -0400 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: References: Message-ID: If I understand what i just said correctly, it just means it tells the string what type to convert from when placing it into the final result. From jeffh at pona.net Fri Oct 22 16:54:47 2010 From: jeffh at pona.net (Jeff Honey) Date: Fri, 22 Oct 2010 10:54:47 -0400 Subject: [Tutor] URL test function. Message-ID: <67B4366BA7B3E14586958A5F4F06972727645D0068@IAD2MBX03.mex02.mlsrvr.com> I am trying to create a function to plug into a restart script I'm creating. I can't seem to successfully loop through making an http connection, if it fails to retry, if it succeeds, to end and continue with its parent function. I'm using the Fabric project's module and including my functions in fabfile.py. the most rudimentary I've got right now is this: def is_ready(): with settings( warn_only=True ): try: urllib2.urlopen('http://'+env.host_string+'\:8080') except urllib2.URLError: time.sleep(10) urllib2.urlopen('http://'+env.host_string+'\:8080') I am trying to get the 'host_string' environment variable to plug in here but not sure if the parent function will pass along that information nor if I can call it like this. Additionally, I have never used try/except so I don't know if this will just try twice and quit. As it is now, it raises URLError and bombs out. -- ?????????????????? ? kyoboku kazeoshi ? ?????????????????? From bgailer at gmail.com Fri Oct 22 18:53:45 2010 From: bgailer at gmail.com (bob gailer) Date: Fri, 22 Oct 2010 12:53:45 -0400 Subject: [Tutor] URL test function. In-Reply-To: <67B4366BA7B3E14586958A5F4F06972727645D0068@IAD2MBX03.mex02.mlsrvr.com> References: <67B4366BA7B3E14586958A5F4F06972727645D0068@IAD2MBX03.mex02.mlsrvr.com> Message-ID: <4CC1C199.4000606@gmail.com> On 10/22/2010 10:54 AM, Jeff Honey wrote: > I am trying to create a function to plug into a restart script I'm creating. I can't seem to successfully loop through making an http connection, if it fails to retry, if it succeeds, to end and continue with its parent function. > > I'm using the Fabric project's module and including my functions in fabfile.py. > > the most rudimentary I've got right now is this: > > > > def is_ready(): > with settings( > warn_only=True > ): > try: > urllib2.urlopen('http://'+env.host_string+'\:8080') > except urllib2.URLError: > time.sleep(10) > urllib2.urlopen('http://'+env.host_string+'\:8080') > > > I am trying to get the 'host_string' environment variable to plug in here but not sure if the parent function will pass along that information nor if I can call it like this. I don't understand this part. Please clarify or show us the parent function. > Additionally, I have never used try/except so I don't know if this will just try twice and quit. As it is now, it raises URLError and bombs out. Whenever you want to repeat something you need either a loop or recursion. A loop is appropriate here. while True: try: urllib2.urlopen('http://'+env.host_string+'\:8080') except urllib2.URLError: time.sleep(10) else: break -- Bob Gailer 919-636-4239 Chapel Hill NC From technopolitica at gmail.com Fri Oct 22 19:05:59 2010 From: technopolitica at gmail.com (M. George Hansen) Date: Fri, 22 Oct 2010 10:05:59 -0700 Subject: [Tutor] URL test function. Message-ID: > def is_ready(): > > ?with settings( > > ?warn_only=True > > ?): > > ?try: > > ??urllib2.urlopen('http://'+env.host_string+'\:8080') > > ?except urllib2.URLError: > > ??time.sleep(10) > > ??urllib2.urlopen('http://'+env.host_string+'\:8080') > > I am trying to get the 'host_string' environment variable to plug in here but not sure if the parent function will pass along that information nor if I can call it like this. Additionally, I have never used try/except so I don't know if this will just try twice and quit. As it is now, it raises URLError and bombs out. Your try..except block does execute the urlopen function twice (provided the first time fails). The problem is that if the second attempt also fails and raises an exception within the except clause there is no handler for the exception and it will probably stop execution. I assume that the "settings" context manager may have some means of turning exceptions into warnings, hence the "warn_only=True" argument, but for some reason isn't handling URLError exceptions. You could use nested try..except clauses like so: def is_ready(): with settings(warn_only=True): try: urllib2.urlopen('http://'+env.host_string+'\:8080') except urllib2.URLError: time.sleep(10) try: urllib2.urlopen('http://'+env.host_string+'\:8080') except urllib2.URLError: pass # Or log a warning message, etc. But this is rather ugly syntax. Instead, I'd suggest using a while loop: def is_ready(): max_tries = 2 try_n = 0 with settings(warn_only=True): while(True): try_n += 1 try: urllib2.urlopen('http://'+env.host_string+'\:8080') except urllib2.URLError: if (try_n < max_tries): # We'll wait and try again... time.sleep(10) else: # Maximum number of tries exceeded! Print a warning message and break # out of the loop break else: # It worked! So get out of the while loop break This is much more explicit and will allow you to easily change the maximum number of tries you will allow. From alan.gauld at btinternet.com Fri Oct 22 20:27:57 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 22 Oct 2010 19:27:57 +0100 Subject: [Tutor] What does "TypeError: 'int' object is not iterable"mean? References: Message-ID: "Richard D. Moores" wrote > return ("%%.%sf" % n) % floatt > > which works fine, but a question remains: n is an integer. Why the > 's' > in '%sf'? Its arbitrary. You could use %d just as easily. %s will put the numner into the string, so will %d. %d is probably a bit more picky about what it inserts - so might actually be a better choice in this case since float2n(1.234567, 3.5) Doesn't work as you might hope... but changing it to use %d might - depending on your definition of better! And what you might hope! :-) OTOH %s handles this better: float2n(1.234567, '3') maybe... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From royhink at gmail.com Fri Oct 22 20:52:06 2010 From: royhink at gmail.com (Roy Hinkelman) Date: Fri, 22 Oct 2010 11:52:06 -0700 Subject: [Tutor] Problem Passing VARs to Python from PHP & capturing return string Message-ID: My script doesn't want to recognize the variables from the exec() command in PHP. Plus, it won't capture the results of the script. This Python script works in IDLE, and I've got some testing code in there. One Known Unsolved Issue: I put Python in C:\Program Files\Python26\python.exe and have tried $command = "C:\Program Files\Python26\python.exe include/weatherFeed.py -c $city -s $state"; to no avail. I added .py to Windows IIS Web Service Extensions and to the Application Configuration. Any help appreciated. [code] #!C:\Program Files\Python26\python.exe -u # Current weather feed # # Weather page sources: http://www.weather.gov/ # Capture relevant data, strip unusable stuff out, fix URLs, display in our HTML page # import program modules import mechanize, re, urllib2, sys # 'standard' set from BeautifulSoup import BeautifulSoup as B_S query = 'Chicago, IL' #test to make sure script works! city = 'Establish' state = 'Variables' count = 0 for ea in sys.argv: if ea == '-c': city = sys.argv[count+1] elif ea == '-s': state = sys.argv[count+1] count+=1 cityState = city + ', ' + state _URL = "http://www.weather.gov/" #_URL = " http://forecast.weather.gov/MapClick.php?CityName=Novato&state=CA&site=MTR&lat=38.1032&lon=-122.63 " br=mechanize.Browser() br.open( _URL ) br.select_form( nr=1 ) #assuming form is 2nd form on page br['inputstring'] = query html = br.submit() _soup = B_S(html) # finding the correct table _step1 = _soup.findAll('table', limit=7)[6] col = _step1.findAll('td') _thumb = '
' + str(col[0]) + str(col[1]) + '
Forecast for ' + query + '
' + str(sys.argv) + '
' _thumb = _thumb.replace( '11%','50%' ) _thumb = _thumb.replace( '/images/', 'images/weather/' ) #write to txt file TEST _temp = 'D:\\Inetpub\\AtoZ\\hometown\\include\\weatherFeed_TEMP.txt' temp = open( _temp, 'w' ) temp.write( _thumb ) temp.close() #print _thumb [/code] And my PHP: [code]

Python Weather Feed for ' . $city . ', ' . $state . '

'; ob_start(); $command = "include/weatherFeed.py -c " . $city . "-s " . $state; exec($command); $content=ob_get_contents(); ob_end_clean(); echo 'Content: ' . $content . '
'; echo 'Result: ' . $result . '
'; echo 'Data: ' . $data . '
'; include('include\weatherFeed_TEMP.txt') ?> -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Fri Oct 22 21:13:13 2010 From: bgailer at gmail.com (bob gailer) Date: Fri, 22 Oct 2010 15:13:13 -0400 Subject: [Tutor] Problem Passing VARs to Python from PHP & capturing return string In-Reply-To: References: Message-ID: <4CC1E249.5020005@gmail.com> On 10/22/2010 2:52 PM, Roy Hinkelman wrote: > > My script doesn't want to recognize the variables from the exec() > command in PHP. Plus, it won't capture the results of the script. > This is a pretty vague description of the problem. Please provide explicit details. > > > This Python script works in IDLE, and I've got some testing code in there. > > One Known Unsolved Issue: > I put Python in C:\Program Files\Python26\python.exe and have tried > $command = "C:\Program Files\Python26\python.exe > include/weatherFeed.py -c $city -s $state"; > to no avail. > The full path to Python must be in quotes (due to the space in the path). I don't know enough Perl to tell you how to do this. In Python I would: command = '"C:\Program Files\Python26\python.exe" include/weatherFeed.py -c %s -s %s' % (city, state) [snip] -- Bob Gailer 919-636-4239 Chapel Hill NC -------------- next part -------------- An HTML attachment was scrubbed... URL: From rdmoores at gmail.com Fri Oct 22 22:56:51 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Fri, 22 Oct 2010 13:56:51 -0700 Subject: [Tutor] What does "TypeError: 'int' object is not iterable"mean? In-Reply-To: References: Message-ID: On Fri, Oct 22, 2010 at 11:27, Alan Gauld wrote: > > "Richard D. Moores" wrote > >> ? return ("%%.%sf" % n) % floatt >> >> which works fine, but a question remains: n is an integer. Why the 's' >> in '%sf'? > > Its arbitrary. You could use %d just as easily. > %s will put the numner into the string, so will %d. > %d is probably a bit more picky about what it inserts - so might > actually be a better choice in this case since > > float2n(1.234567, 3.5) > > Doesn't work as you might hope... but changing it to use %d might - > depending > on your definition of better! And what you might hope! :-) > > OTOH %s handles this better: > > float2n(1.234567, '3') If I use n = 2.5 and replace %s with %d, my function is def float2n_decimals(floatt, n): """ Given a float (floatt), return floatt to n decimal places. E.g., with n = 2, 81.34567 -> 81.35 """ return ("%%.%df" % n) % floatt This works (the d converts the 2.5 to an int, 2), but I get a deprecation warning: "DeprecationWarning: integer argument expected, got float rate = rate1 = round(float(rate2),n)" Of course, n = 2.5 makes no sense in my script. Dick From rhinkelman at worldtradepress.com Fri Oct 22 20:28:31 2010 From: rhinkelman at worldtradepress.com (Roy Hinkelman) Date: Fri, 22 Oct 2010 11:28:31 -0700 Subject: [Tutor] Problem Passing VARs to Python from PHP & capturing return string Message-ID: <000f01cb7216$ef863c70$ce92b550$@com> My script doesn't want to recognize the variables from the exec() command in PHP. Plus, it won't capture the results of the script. This Python script works in IDLE, and I've got some testing code in there. One Known Unsolved Issue: I put Python in C:\Program Files\Python26\python.exe and have tried $command = "C:\Program Files\Python26\python.exe include/weatherFeed.py -c $city -s $state"; to no avail. I added .py to Windows IIS Web Service Extensions and to the Application Configuration. Any help appreciated. [code] #!C:\Program Files\Python26\python.exe -u # Current weather feed # # Weather page sources: http://www.weather.gov/ # Capture relevant data, strip unusable stuff out, fix URLs, display in our HTML page # import program modules import mechanize, re, urllib2, sys # 'standard' set from BeautifulSoup import BeautifulSoup as B_S query = 'Chicago, IL' #test to make sure script works! city = 'Establish' state = 'Variables' count = 0 for ea in sys.argv: if ea == '-c': city = sys.argv[count+1] elif ea == '-s': state = sys.argv[count+1] count+=1 cityState = city + ', ' + state _URL = "http://www.weather.gov/" #_URL = "http://forecast.weather.gov/MapClick.php?CityName=Novato&state=CA&site=MTR& lat=38.1032&lon=-122.63" br=mechanize.Browser() br.open( _URL ) br.select_form( nr=1 ) #assuming form is 2nd form on page br['inputstring'] = query html = br.submit() _soup = B_S(html) # finding the correct table _step1 = _soup.findAll('table', limit=7)[6] col = _step1.findAll('td') _thumb = '' + str(col[0]) + str(col[1]) + '
Forecast for ' + query + '
' + str(sys.argv) + '
' _thumb = _thumb.replace( '11%','50%' ) _thumb = _thumb.replace( '/images/', 'images/weather/' ) #write to txt file TEST _temp = 'D:\\Inetpub\\AtoZ\\hometown\\include\\weatherFeed_TEMP.txt' temp = open( _temp, 'w' ) temp.write( _thumb ) temp.close() #print _thumb [/code] And my PHP: [code]

Python Weather Feed for ' . $city . ', ' . $state . '

'; ob_start(); $command = "include/weatherFeed.py -c " . $city . "-s " . $state; exec($command); $content=ob_get_contents(); ob_end_clean(); echo 'Content: ' . $content . '
'; echo 'Result: ' . $result . '
'; echo 'Data: ' . $data . '
'; include('include\weatherFeed_TEMP.txt') ?> Roy Hinkelman Technical Services Website Development & Management 707-774-7411 roy at worldtradepress.com ________________________________ www.WorldTradePress.com (main website) www.StockMapAgency.com (3700+ Antique & Modern Maps) www.BestCountryReports.com (country reports for 175 countries) www.GiantMapArt.com (giant wall maps) www.WorldTradeRef.com (trade and logistics) www.GlobalRoadWarrior.com (175-country database) www.AtoZMapsOnline.com (worlds largest map database) www.AtoZtheUSA.com (extensive state facts database) -------------- next part -------------- An HTML attachment was scrubbed... URL: From tmantjg at yahoo.com Fri Oct 22 20:49:07 2010 From: tmantjg at yahoo.com (Terry Green) Date: Fri, 22 Oct 2010 11:49:07 -0700 Subject: [Tutor] Syntax Error Messages Message-ID: <159AB371D15F4D24998D5517192D5453@terryPC> Am new to Python, and having difficulty with Error Messages I 'm using Python 3.1 And PyScripter as my editor I want to process a comma delimited file one line at a time and Interact with the fields within each line. I found this script when looking at the CVS module and loaded It into PyScripter, but get: Syntax Error: Invalid Syntax Cannot figure out why and Googleing for help doesn't help Any ideas? import csv, sys filename = "some.csv" reader = csv.reader(open(filename, "rb")) try: for row in reader: print (row) except csv.Error, e: sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e)) thanks, Terry Green -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sat Oct 23 02:23:08 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 23 Oct 2010 11:23:08 +1100 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: References: Message-ID: <201010231123.08702.steve@pearwood.info> On Sat, 23 Oct 2010 12:42:50 am Richard D. Moores wrote: > So I wrote a function: > > def float2n_decimals(floatt, n): > """ > Given a float (floatt), return floatt to n decimal places. > > E.g., with n = 2, 81.34567 -> 81.35 > """ > return ("%%.%sf" % n) % floatt > > which works fine, float2n_decimals(x, 3) is better written in place as: "%.*f" % (3, x) There's no need for a function for something so simple. > but a question remains: n is an integer. Why the > 's' in '%sf'? Your function might be more clear if you split the formatting into two lines: template = "%%.%sf" % n return template % floatt The first line builds a template string from n. Now it becomes clear what %s is for: it's to insert the n as a string into the template. %d would express the intention of the function better. By the way, the function docstring is seriously misleading. It describes a completely different function: > Given a float (floatt), return floatt to n decimal places. > > E.g., with n = 2, 81.34567 -> 81.35 It does nothing of the sort! What you are describing is the built-in function round: >>> print round(81.34567, 2) 81.35 What your function does is return a string, not a float. Besides, given a float, where does n come from? A global variable? There is a convention for giving examples that you should follow. Putting the three of these together, your docstring should say something like: Given a float floatt and an integer n, returns floatt formatted as a string to n decimal places. >>> float2n_decimals(2, 81.34567) '81.35' (BTW, I really hate the name "floatt". It makes me think you're stuttering.) -- Steven D'Aprano From hugo.yoshi at gmail.com Sat Oct 23 02:35:37 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Sat, 23 Oct 2010 02:35:37 +0200 Subject: [Tutor] Syntax Error Messages In-Reply-To: <159AB371D15F4D24998D5517192D5453@terryPC> References: <159AB371D15F4D24998D5517192D5453@terryPC> Message-ID: On Fri, Oct 22, 2010 at 8:49 PM, Terry Green wrote: > Am new to Python, and having difficulty with Error Messages > > I ?m using Python 3.1 > > And PyScripter as my editor > > > > I want to process a comma delimited file one line at a time and > Interact with the fields within each line. > I found this script when looking at the CVS module and loaded > It into PyScripter, but get: Syntax Error: Invalid Syntax > Cannot figure out why and Googleing for help doesn?t help > > Any ideas? > > import csv, sys > > filename = "some.csv" > > reader = csv.reader(open(filename, "rb")) > > try: > > ??? for row in reader: > > ??????? print (row) > > except csv.Error, e: > > ??? sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e)) > > > thanks, > Terry Green > You should have gotten more information than just that error. The offending line should also be printed, along with an estimate of where exactly the error occurred. Please copy-paste *all* information the traceback gives you, unless it is too long (a good rule of thumb is "would *I* read all this just to help some stranger out?"). The more information we have to work with, the better. In any case, that python code was written for python 2.x, not 3.x. If you change "except csv.Error, e:" to "except csv.Error as e:" it should work. Hugo From steve at pearwood.info Sat Oct 23 02:45:47 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 23 Oct 2010 11:45:47 +1100 Subject: [Tutor] URL test function. In-Reply-To: <67B4366BA7B3E14586958A5F4F06972727645D0068@IAD2MBX03.mex02.mlsrvr.com> References: <67B4366BA7B3E14586958A5F4F06972727645D0068@IAD2MBX03.mex02.mlsrvr.com> Message-ID: <201010231145.48068.steve@pearwood.info> On Sat, 23 Oct 2010 01:54:47 am Jeff Honey wrote: > def is_ready(): > with settings( > warn_only=True > ): > try: > urllib2.urlopen('http://'+env.host_string+'\:8080') > except urllib2.URLError: > time.sleep(10) > urllib2.urlopen('http://'+env.host_string+'\:8080') That will only try twice, then give up with an exception. > I am trying to get the 'host_string' environment variable to plug in > here but not sure if the parent function will pass along that > information nor if I can call it like this. Have you tried it to see what happens? > Additionally, I have > never used try/except so I don't know if this will just try twice and > quit. As it is now, it raises URLError and bombs out. What does the error say? I would try something like this: def urlopen_retry(url, maxretries=10, time_to_wait=10): """Call urlopen on a url. If the call fails, try again up to a maximum of maxretries attempts, or until it succeeds. Delay between attempts by time_to_wait seconds, increasing each time.""" if maxretries < 1: return None for i in range(maxretries): try: return urllib2.urlopen(url) except urllib2.URLError: # Delay by (e.g.) 10 seconds, then 20, then 30, then ... time.sleep(time_to_wait*(i+1)) raise # re-raise the last exception and then call it with: def is_ready(): url = 'http://' + env.host_string + '\:8080' with settings(warn_only=True): return urlopen_retry(url) Hope this helps. -- Steven D'Aprano From steve at pearwood.info Sat Oct 23 02:57:38 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 23 Oct 2010 11:57:38 +1100 Subject: [Tutor] Problem Passing VARs to Python from PHP & capturing return string In-Reply-To: References: Message-ID: <201010231157.38937.steve@pearwood.info> On Sat, 23 Oct 2010 05:52:06 am Roy Hinkelman wrote: > My script doesn't want to recognize the variables from the exec() > command in PHP. Plus, it won't capture the results of the script. "My script"? What are you talking about? You have at least two -- a PHP script and a Python script. If you're having problems with the PHP exec() command, that's probably a problem with the PHP script and you need to take it up with a PHP list. > This Python script works in IDLE, and I've got some testing code in > there. If it works fine, why are you showing it to us? > One Known Unsolved Issue: > I put Python in C:\Program Files\Python26\python.exe and have tried > $command = "C:\Program Files\Python26\python.exe > include/weatherFeed.py -c $city -s $state"; > to no avail. Unfortunately my crystal ball is broken, and I have a court order prohibiting me from reading people's minds, so I'm afraid I'm going to have to insist that you actually explain *what the problem is* instead of expecting us to guess. What is the unsolved issue? What do you expect the above line to do? What does it do instead? > I added .py to Windows IIS Web Service Extensions and to the > Application Configuration. How exciting! And what happened then? -- Steven D'Aprano From steve at pearwood.info Sat Oct 23 03:04:32 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 23 Oct 2010 12:04:32 +1100 Subject: [Tutor] Syntax Error Messages In-Reply-To: <159AB371D15F4D24998D5517192D5453@terryPC> References: <159AB371D15F4D24998D5517192D5453@terryPC> Message-ID: <201010231204.32623.steve@pearwood.info> On Sat, 23 Oct 2010 05:49:07 am Terry Green wrote: > I found this script when looking at the CVS module and loaded > It into PyScripter, but get: Syntax Error: Invalid Syntax > Cannot figure out why and Googleing for help doesn't help > > Any ideas? No, no, don't show us the actual error that you got! I LOVE guessing games. .... Nah, I'm actually lying, I hate guessing games. Please COPY AND PASTE (do NOT retype, summarise, paraphrase or write out from memory) the EXACT error message you get. It will include the line causing the syntax error, like this: >>> print "abc File "", line 1 print "abc ^ SyntaxError: EOL while scanning string literal We need to see the entire message, not just a vague description that it's a syntax error. -- Steven D'Aprano From smokefloat at gmail.com Sat Oct 23 04:42:27 2010 From: smokefloat at gmail.com (David Hutto) Date: Fri, 22 Oct 2010 22:42:27 -0400 Subject: [Tutor] Syntax Error Messages In-Reply-To: <201010231204.32623.steve@pearwood.info> References: <159AB371D15F4D24998D5517192D5453@terryPC> <201010231204.32623.steve@pearwood.info> Message-ID: What my buddy pal is saying, is that you should start at the beginning. I first downloaded x version of python to x operating system, then I tried this tutorial with these explicit modules/requirements(which I may or not have) then progress to the smaller aspects, like this won't iterate, or that doesn't coagulate with this, etc. After asking a few initially awkwardly ignorant questions, you might know as much as me...How to use the basic library, with, a few exceptions in personal taste. From rdmoores at gmail.com Sat Oct 23 06:48:29 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Fri, 22 Oct 2010 21:48:29 -0700 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: <201010231123.08702.steve@pearwood.info> References: <201010231123.08702.steve@pearwood.info> Message-ID: It's great to have you chime in, Steven. I do wish you would stop pulling your punches, however. ;) On Fri, Oct 22, 2010 at 17:23, Steven D'Aprano wrote: > On Sat, 23 Oct 2010 12:42:50 am Richard D. Moores wrote: > >> So I wrote a function: >> >> def float2n_decimals(floatt, n): >> ? ? """ >> ? ? Given a float (floatt), return floatt to n decimal places. >> >> ? ? E.g., with n = 2, 81.34567 -> 81.35 >> ? ? """ >> ? ? return ("%%.%sf" % n) % floatt >> >> which works fine, > > > float2n_decimals(x, 3) > > is better written in place as: > > "%.*f" % (3, x) > > There's no need for a function for something so simple. Yes, but I needed one for ("%%.%sf" % n) % floatt . >> but a question remains: n is an integer. Why the >> 's' in '%sf'? > > Your function might be more clear if you split the formatting into two > lines: > > template = "%%.%sf" % n > return template % floatt > > The first line builds a template string from n. Now it becomes clear > what %s is for: it's to insert the n as a string into the template. %d > would express the intention of the function better. > OK, I'll do that. > > By the way, the function docstring is seriously misleading. It describes > a completely different function: > >> ? ? Given a float (floatt), return floatt to n decimal places. >> >> ? ? E.g., with n = 2, 81.34567 -> 81.35 > > It does nothing of the sort! What you are describing is the built-in > function round: > >>>> print round(81.34567, 2) > 81.35 > > What your function does is return a string, not a float. OK, point well-taken. > Besides, given > a float, where does n come from? A global variable? Yes. See line 5 of . This version incorporates all your suggestions. > There is a convention for giving examples that you should follow. > Putting the three of these together, your docstring should say > something like: > > ? ?Given a float floatt and an integer n, returns floatt formatted > ? ?as a string to n decimal places. > > ? ?>>> float2n_decimals(2, 81.34567) > ? ?'81.35' I've never seen that convention, but I'll try to follow it. > > (BTW, I really hate the name "floatt". It makes me think you're > stuttering.) I'll use "x" instead. Anything you'd like to say about the rest of the script? Thanks, Steven. Dick From nicolaiheitz at googlemail.com Sat Oct 23 03:30:59 2010 From: nicolaiheitz at googlemail.com (Nicolai Heitz) Date: Fri, 22 Oct 2010 18:30:59 -0700 Subject: [Tutor] problems with numdifftools Message-ID: <4CC23AD3.7020200@googlemail.com> Hey, I am not sure if you are the right persons to contact but if not I would appreciate a short notice and maybe an address where I can find help. I am using Python 2.6 and the following packages: 1) numpy 2) scipy 3) numdifftools I am a physicist and started to use Python 2-3 weeks ago. I want to use Python to calculate the eigenvalues of the Hamiltonian given in the code. Therefore I have to do the following steps: 1) define the Hamiltonian or the potential respectively (--> function: potential) 2) minimize the potential ( I am using scipy.optimize.fmin to calculate the minimum of the potential) (2b) Expand the Hamiltonian around the minimum position. This step is not done in the code because it is only necessary for the purpose that one understand the physical background and why one have to do step 3) 3) Calculate the Hessian matrix of the potential, that means calculate the second derivatives of the potential at the point of the minimum position (--> numdifftools.Hessian) 4) Calculate the eigenvalues of the Hessian (-->scipy.linalg.eigvals) The problem can be solved analytically except of the calculation of the minima in step 2: Now I have the following problem: The Hessian matrix evaluated at the minimum position is wrong. What I checked so far: 1) The potential seems to be calculated correctly and the minimum position for two ions (N=2) is fine. 2) The Hessian matrix calculation works fine for several other functions. 3) The Hessian matrix is correct when I set the Coulomb interaction to zero (C=0) 4) The Hessian matrix is correct when I set C to some simple arbitrary integer numbers like 1, 5 ,8 .... 5) The Hesse matrix gives the correct number of the first part of the diagonal elements even with C=2.3e-28 ! But in that case the offdiagonal elements and the part of the diagonal element which refers to the second derivative in the Coulomb term is wrong! The offdiagonal term should be C/(abs(x_1-x_2))**3 = 2.6e-12 but it is 3.4e-24! 5) In principal Python can divide those numbers (2.3e-28)/(5.6e-6)**3 6) I played around with the accurateness of the calculation of the Hessian by varying the arguments numTerms, method and metOrder but that didn't change anything in my case. My source code is attached. Please keep in mind that I just started programing and Python is my first Language. I tried to do my best to comment every single step to make the code readable. Here it comes: # import a tool to use / as a symbol for normal division from __future__ import division #import system data import sys, os #Loading the required packages import scipy as sp import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import numdifftools as nd # and subpackages from scipy import * from scipy import linalg, optimize, constants #----------------------------------------------------------------------------------------- # Hamiltonian H=sum_i(p_i^2/(2m)+ 1/2 * m * w^2 x_i^2)+ sum_(i!=j)(a/|x_i-x_j|) #----------------------------------------------------------------------------------------- class classicalHamiltonian: def __init__(self): self.N = 2 #N is a scalar, it's the number of ions in the chain f = 1000000 #f is a scalar, it's the trap frequency self.w = 2*pi*f #w is a scalar, it's the angular velocity corresponding to the trap frequency self.C = (4*pi*constants.epsilon_0)**(-1)*constants.e**2 #C is a scalar, it's the Coulomb constant times the electronic charge in SI self.m = 39.96*1.66*10**(-27) #m is the mass of a single trapped ion in the chain #~ print self.w #~ print self.C #~ print self.w**2 * self.m def potential(self, positionvector): #Defines the potential that is going to be minimized x= positionvector #x is an 1-d array (vector) of lenght N that contains the positions of the N ions w= self.w C= self.C m= self.m #~ m=1 #~ #C=1 #~ w=5 #First we consider the potential of the harmonic osszilator Vx = 0.5 * m * (w**2) * sum(x**2) for i in range(len(x)): for j in range(len(x)): if j >i: Vx = Vx + C * (abs(x[i]-x[j]))**(-1) #then we add the coulomb interaction return Vx def initialposition(self): #Defines the initial position as an estimate for the minimize process N= self.N x_0 = r_[-(N-1)/2:(N-1)/2:N*1j] return x_0 def normal_modes(self, eigenvalues): #the computed eigenvalues of the matrix Vx are of the form (normal_modes)^2*m. m = self.m normal_modes = sqrt(eigenvalues/m) return normal_modes #C=(4*pi*constants.epsilon_0)**(-1)*constants.e**2 c=classicalHamiltonian() #print c.potential(array([-0.5, 0.5])) xopt = optimize.fmin(c.potential, c.initialposition(), xtol = 1e-10) hessian = nd.Hessian(c.potential) eigenvalues = linalg.eigvals(hessian(xopt)) normal_modes = c.normal_modes(eigenvalues) print '\n' print '--------- Results--------' , '\n' print 'groundstate positions=', xopt #print 'groundstate difference=' , xopt[0]-xopt[1] #print 'offdiagonal elements = ', print 'Hessian = ', hessian(xopt) print 'eigenvalues = ', eigenvalues print 'normal modes = ', normal_modes I appreciate any kind of help. Thanks a lot in advance Nicolai ps: If you have any questions or need some more information please let me know. From ajarncolin at gmail.com Sat Oct 23 11:28:23 2010 From: ajarncolin at gmail.com (col speed) Date: Sat, 23 Oct 2010 16:28:23 +0700 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? Message-ID: > > Message: 7 > Date: Fri, 22 Oct 2010 21:48:29 -0700 > From: "Richard D. Moores" > To: "Steven D'Aprano" > Cc: tutor at python.org > Subject: Re: [Tutor] What does "TypeError: 'int' object is not > iterable" mean? > Message-ID: > > > > Content-Type: text/plain; charset=UTF-8 > > It's great to have you chime in, Steven. I do wish you would stop > pulling your punches, however. ;) > > <-snip> > I've never seen that convention, but I'll try to follow it. > > > > > (BTW, I really hate the name "floatt". It makes me think you're > > stuttering.) > > I'll use "x" instead. Anything you'd like to say about the rest of the > script? > > Thanks, Steven. > > Dick > > > ------------------------------ > Excuse me, I'm not a professional. Rather than "x", I would use "float_" or > even "not_int", as mentioned in PEP8: > If a function argument's name clashes with a reserved keyword, it is generally better to append a single trailing underscore rather than use an abbreviation or spelling corruption. Thus "print_" is better than "prnt". (Perhaps better is to avoid such clashes by using a synonym.) Steven knows his subject, please don't answer like this. -- if not ErrorMessage: check_all() >>>check_all.__doc__ " noErrorMessage != correctAnswer" -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Sat Oct 23 12:43:07 2010 From: davea at ieee.org (Dave Angel) Date: Sat, 23 Oct 2010 06:43:07 -0400 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: References: <201010231123.08702.steve@pearwood.info> Message-ID: <4CC2BC3B.6030605@ieee.org> On 2:59 PM, Richard D. Moores wrote: > It's great to have you chime in, Steven. I do wish you would stop > pulling your punches, however. ;) > > On Fri, Oct 22, 2010 at 17:23, Steven D'Aprano wrote: >> On Sat, 23 Oct 2010 12:42:50 am Richard D. Moores wrote: >> >>> So I wrote a function: >>> >>> def float2n_decimals(floatt, n): >>> """ >>> Given a float (floatt), return floatt to n decimal places. >>> >>> E.g., with n =, 81.34567 -> 81.35 >>> """ >>> return ("%%.%sf" % n) % floatt >>> >>> which works fine, >> >> float2n_decimals(x, 3) >> >> is better written in place as: >> >> "%.*f" % (3, x) >> >> There's no need for a function for something so simple. > Yes, but I needed one for ("%%.%sf" % n) % floatt . > > > Sometimes Steven's style can be a bit caustic, but there's almost always a few important nuggets. In this case, you missed the one that your formatting is unnecessarily complicated, at least if you have a recent enough Python version. In particular, "%.*f" % (n, myfloat) will convert myfloat to a string, and use n as the precision, just as your more complex expression. The asterisk is the magic character, that says use n as the precision field. This syntax was available at least in 2.3, so unless you need to use an older version, there's not much need for the two-stage template system. DaveA From steve at pearwood.info Sat Oct 23 13:08:47 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 23 Oct 2010 22:08:47 +1100 Subject: [Tutor] problems with numdifftools In-Reply-To: <4CC23AD3.7020200@googlemail.com> References: <4CC23AD3.7020200@googlemail.com> Message-ID: <201010232208.48660.steve@pearwood.info> On Sat, 23 Oct 2010 12:30:59 pm Nicolai Heitz wrote: > Hey, > > I am not sure if you are the right persons to contact but if not I > would appreciate a short notice and maybe an address where I can find > help. I'd try the numpy mailing list: http://www.scipy.org/Mailing_Lists [...] > 5) The Hesse matrix gives the correct number of the first part of the > diagonal elements even with C=2.3e-28 ! But in that case the > offdiagonal elements and the part of the diagonal element which > refers to the second derivative in the Coulomb term is wrong! The > offdiagonal term should be C/(abs(x_1-x_2))**3 = 2.6e-12 but it is > 3.4e-24! Yes, I think this is definitely a question for the numpy community :) > 5) In principal Python can divide those numbers (2.3e-28)/(5.6e-6)**3 I get half the result you suggest above: >>> (2.3e-28)/(5.6e-6)**3 1.3096756559766764e-12 which agrees with my HP-48GX, and the result after simplifying by hand to 2.3e-10/175.616. So I think that is the correct result, not 2.6e-12. > My source code is attached. Please keep in mind that I just started > programing and Python is my first Language. I tried to do my best to > comment every single step to make the code readable. Here it comes: Comments are good, but too many comments hurt readability. Don't feel that you need to comment every single line. Comments like: x = x+1 # increment x by 1 are just noise. But this is a good comment: x = x+1 # add correction for wind-chill factor. > self.m = 39.96*1.66*10**(-27) ? ? ? ? ? ? ? ? ? ?#m is the mass > of a single trapped ion in the chain I wonder why you calculate m with an explicit exponentiation operator, instead of a floating point exponent? The difference is very small, so it probably doesn't matter: >>> 39.96*1.66*10**(-27) 6.6333600000000006e-26 >>> 39.96*1.66e-27 6.6333599999999994e-26 > for i in range(len(x)): > for j in range(len(x)): > if j >i: > Vx = Vx + C * (abs(x[i]-x[j]))**(-1) #then we > add the coulomb interaction You can simplify that nested loop and addition: for i in range(len(x)): for j in range(i+1, len(x)): Vx += C * (abs(x[i]-x[j]))**(-1) # add the coulomb interaction The function range takes up to three integer arguments: range(start, end, step) where start and step are optional, defaulting to 0 and 1 respectively. By starting the inner loop at i+1, you guarantee that j is always > i and therefore you can skip the test. The other change is that instead of Vx = Vx + something you can write: Vx += something -- Steven D'Aprano From steve at pearwood.info Sat Oct 23 13:20:53 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 23 Oct 2010 22:20:53 +1100 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: <4CC2BC3B.6030605@ieee.org> References: <4CC2BC3B.6030605@ieee.org> Message-ID: <201010232220.53836.steve@pearwood.info> On Sat, 23 Oct 2010 09:43:07 pm Dave Angel wrote: > On 2:59 PM, Richard D. Moores wrote: [...] > >> float2n_decimals(x, 3) > >> > >> is better written in place as: > >> > >> "%.*f" % (3, x) > >> > >> There's no need for a function for something so simple. > > > > Yes, but I needed one for ("%%.%sf" % n) % floatt . > > > > > > Sometimes Steven's style can be a bit caustic, I admit that I'm inordinately fond of sarcasm when people write posts saying "it doesn't work" without any further information, but what did I say that was caustic in this case? "There's no need for a function for something so simple" isn't caustic. Or sarcastic. Or nasty in any way. > but there's almost > always a few important nuggets. In this case, you missed the one > that your formatting is unnecessarily complicated, at least if you > have a recent enough Python version. > > In particular, > "%.*f" % (n, myfloat) > > will convert myfloat to a string, and use n as the precision, just as > your more complex expression. The asterisk is the magic character, > that says use n as the precision field. > > This syntax was available at least in 2.3, so unless you need to use > an older version, there's not much need for the two-stage template > system. It would have to be a *very* old version. The use of * as the width parameter in format strings goes back to the Dark Ages of Python 1.5: [steve at sylar ~]$ python1.5 Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> "%.*f" % (3, 1.2345678) '1.235' I believe this is a virtual copy of string formatting from C, in which case it probably goes back to the 80s or even the 70s. -- Steven D'Aprano From rdmoores at gmail.com Sat Oct 23 14:09:44 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Sat, 23 Oct 2010 05:09:44 -0700 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: <4CC2BC3B.6030605@ieee.org> References: <201010231123.08702.steve@pearwood.info> <4CC2BC3B.6030605@ieee.org> Message-ID: On Sat, Oct 23, 2010 at 03:43, Dave Angel wrote: > On 2:59 PM, Richard D. Moores wrote: >> >> It's great to have you chime in, Steven. I do wish you would stop >> pulling your punches, however. ;) >> >> On Fri, Oct 22, 2010 at 17:23, Steven D'Aprano >> ?wrote: >>> float2n_decimals(x, 3) >>> >>> is better written in place as: >>> >>> "%.*f" % (3, x) >>> >>> There's no need for a function for something so simple. >> >> Yes, but I needed one for ("%%.%sf" % n) % floatt . >> >> >> > Sometimes Steven's style can be a bit caustic, but there's almost always a > few important nuggets. Absolutely there are! And I have no problem with his style. I just couldn't hold back what I intended to be a gentle jab of sarcasm. Dangerous in email--especially an email list. > ?In this case, you missed the one that your > formatting is unnecessarily complicated, at least if you have a recent > enough Python version. > > In particular, > ? ? "%.*f" % (n, myfloat) > > will convert myfloat to a string, and use n as the precision, just as your > more complex expression. ?The asterisk is the magic character, that says use > n as the precision field. Thanks for that. Actually, I missed that nugget because Steven had it as ====================== float2n_decimals(x, 3) is better written in place as: "%.*f" % (3, x) ====================== I didn't pick up on it because I wanted to use 'n' where he had the '3'. I didn't realize that your "%.*f" % (n, myfloat) was possible. > This syntax was available at least in 2.3, so unless you need to use an > older version, there's not much need for the two-stage template system. Thanks, Dave, Dick From alan.gauld at btinternet.com Sat Oct 23 14:38:54 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 23 Oct 2010 13:38:54 +0100 Subject: [Tutor] What does "TypeError: 'int' object is not iterable"mean? References: <4CC2BC3B.6030605@ieee.org> <201010232220.53836.steve@pearwood.info> Message-ID: "Steven D'Aprano" wrote > It would have to be a *very* old version. The use of * as the width > parameter in format strings goes back to the Dark Ages of Python > 1.5: > ... > I believe this is a virtual copy of string formatting from C, in > which > case it probably goes back to the 80s or even the 70s. This got me wondering so I dug out my copy of the original K&R (1978) and there is no mention of * in the format specifiers description. (There is for scanf but there it is a field suppressor) I don't have an ANSI C version of K&R at home so can't say if it got added as part of the ANSI C process - around 1990? - but neither of my ANSI C books mention it either. So it may not be a part of C after all. Interesting, I wonder where it did first appear? Alan G. From adam.jtm30 at gmail.com Sat Oct 23 16:07:54 2010 From: adam.jtm30 at gmail.com (Adam Bark) Date: Sat, 23 Oct 2010 15:07:54 +0100 Subject: [Tutor] What does "TypeError: 'int' object is not iterable"mean? In-Reply-To: References: <4CC2BC3B.6030605@ieee.org> <201010232220.53836.steve@pearwood.info> Message-ID: <4CC2EC3A.8050301@gmail.com> On 23/10/10 13:38, Alan Gauld wrote: > > "Steven D'Aprano" wrote > >> It would have to be a *very* old version. The use of * as the width >> parameter in format strings goes back to the Dark Ages of Python 1.5: >> ... >> I believe this is a virtual copy of string formatting from C, in which >> case it probably goes back to the 80s or even the 70s. > > This got me wondering so I dug out my copy of the original K&R (1978) > and there is no mention of * in the format specifiers description. > (There is for scanf but there it is a field suppressor) > > I don't have an ANSI C version of K&R at home so can't say if it got > added as part of the ANSI C process - around 1990? - but neither > of my ANSI C books mention it either. > > So it may not be a part of C after all. > > Interesting, I wonder where it did first appear? > > Alan G. It is indeed in the ANSI version of K&R. "Width or precision or both may be specified as *, in which case the value is computed by converting the next argument(s), which must be int." (The C Programming Language; B. W. Kernighan, D. M. Ritchie; 1988) From emile at fenx.com Sat Oct 23 19:05:12 2010 From: emile at fenx.com (Emile van Sebille) Date: Sat, 23 Oct 2010 10:05:12 -0700 Subject: [Tutor] Problem Passing VARs to Python from PHP & capturing return string In-Reply-To: <000f01cb7216$ef863c70$ce92b550$@com> References: <000f01cb7216$ef863c70$ce92b550$@com> Message-ID: On 10/22/2010 11:28 AM Roy Hinkelman said... > My script doesn't want to recognize the variables from the exec() command in > PHP. Plus, it won't capture the results of the script. > > This Python script works in IDLE, and I've got some testing code in there. > If your python code works and you're having trouble with the php invocation and return values, isn't this more of a php issue? Most of us here do our web serving with python... Emile From wgwinder at gmail.com Sun Oct 24 00:03:47 2010 From: wgwinder at gmail.com (winder@interchange.ubc.ca) Date: Sat, 23 Oct 2010 15:03:47 -0700 Subject: [Tutor] searching for newlines does not work! Message-ID: This is driving me batty! In the interactive window, I can use string.replace on newlines for some strings and not for others. Here is what work for newlines: bill at bill-laptop:~$ python Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> test=r"a \n b \n c \n" >>> test.replace(r"\n","***") 'a *** b *** c ***' Yay! So far, so good. But now I put in a string with newlines, test2, using the triple quote method : >>> test2=""" ... a ... b ... c ... """ >>> test2.replace(r"\n","***") '\na\nb\nc\n' >>> test2 '\na\nb\nc\n' Boo! It does not work. So there here is my question: Why does test work but test2 does not? (And: Why me?) Thanks for any suggestions, Bill -------------- next part -------------- An HTML attachment was scrubbed... URL: From adam.jtm30 at gmail.com Sun Oct 24 00:09:35 2010 From: adam.jtm30 at gmail.com (Adam Bark) Date: Sat, 23 Oct 2010 23:09:35 +0100 Subject: [Tutor] searching for newlines does not work! In-Reply-To: References: Message-ID: <4CC35D1F.90503@gmail.com> On 23/10/10 23:03, winder at interchange.ubc.ca wrote: > This is driving me batty! > > In the interactive window, I can use string.replace on newlines for > some strings and not for others. > > Here is what work for newlines: > > bill at bill-laptop:~$ python > Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) > [GCC 4.4.3] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> test=r"a \n b \n c \n" > >>> test.replace(r"\n","***") > 'a *** b *** c ***' > > Yay! So far, so good. But now I put in a string with newlines, test2, > using the triple quote method : > > >>> test2=""" > ... a > ... b > ... c > ... """ > >>> test2.replace(r"\n","***") > '\na\nb\nc\n' > >>> test2 > '\na\nb\nc\n' > > Boo! It does not work. So there here is my question: Why does test > work but test2 does not? (And: Why me?) > > Thanks for any suggestions, > > Bill The problem is your use of 'r' which means raw string. This means that rather than /n meaning new line it literally means the character '/' followed by the character 'n'. In your first example you put the characters into the string test and replaced the characters. The second example you have actual newlines and are searching for the characters "/n" which don't exist in your string. HTH, Adam. From jlu at hep.anl.gov Sat Oct 23 19:52:13 2010 From: jlu at hep.anl.gov (Jack Uretsky) Date: Sat, 23 Oct 2010 12:52:13 -0500 (CDT) Subject: [Tutor] Writing Scripts. In-Reply-To: References: Message-ID: Hi- Python is case sensitive. If you type "IDLE" (without the quotes) in a terminal window (not Idle) you should, after a few seconds, get a separate screen on which to type. Regards, Jack "Trust me. I have a lot of experience at this." General Custer's unremembered message to his men, just before leading them into the Little Big Horn Valley On Wed, 20 Oct 2010, Autumn Cutter wrote: > I just started learning Python last night and I'm a little stuck on how to write a Python script. > I'm using Python 2.5.0 on a Mac OS X 10.6.4. I've tried using programs like Idle, Applescript and Textmate but I'm still unable to write a script and run it successfully. I'm wondering if I'm using the write program or if I understand correctly how to write and run scripts. Help! :( > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From alan.gauld at btinternet.com Sun Oct 24 00:47:44 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 23 Oct 2010 23:47:44 +0100 Subject: [Tutor] searching for newlines does not work! References: <4CC35D1F.90503@gmail.com> Message-ID: "Adam Bark" wrote >> >>> test=r"a \n b \n c \n" >> >>> test.replace(r"\n","***") >> 'a *** b *** c ***' >> >> >>> test2=""" >> ... a >> ... b >> ... c >> ... """ >> >>> test2.replace(r"\n","***") >> '\na\nb\nc\n' > In your first example you put the characters into the string test > and replaced the characters. > The second example you have actual newlines and are searching for > the characters "/n" which don't exist in your string. Just to further emphasise this point try printing your strings (using print()) and you will see the difference: >>> test = r"a \n b \n c \n" # using raw 'r' >>> test2 = """ ... a ... b ... c ... """ >>> test3 = "a \n b \n c \n" # no raw 'r' >>> print(test) a \n b \n c \n >>> print( test2) a b c >>> print(test3) a b c >>> HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From wgwinder at gmail.com Sun Oct 24 02:16:34 2010 From: wgwinder at gmail.com (winder@interchange.ubc.ca) Date: Sat, 23 Oct 2010 17:16:34 -0700 Subject: [Tutor] searching for newlines does not work! In-Reply-To: References: <4CC35D1F.90503@gmail.com> Message-ID: (Hope this reply makes it back to the Tutor list; I didn't see how the list would know this is a reply... I'm trying a reply-all) This is where I say Doh! and Thanks! I realize now where my fuzzy-headedness come from. I generally use re expressions. If these were re search strings, the raw would be necessary. But they aren't re expressions; they are string expressions. So r is as in wrong, wrong, wrong! So, I was stuck in re mode, unable to shift out to string mode. (Doh!) Thank you for steering this lost lamb back into the python fold.... Best, Bill On Sat, Oct 23, 2010 at 3:47 PM, Alan Gauld wrote: > > "Adam Bark" wrote > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From natfin007 at msn.com Sun Oct 24 06:33:04 2010 From: natfin007 at msn.com (Nathan Finney) Date: Sun, 24 Oct 2010 05:33:04 +0100 Subject: [Tutor] Scripting-Puzzle Pirates Message-ID: Hey, So I got bored of having to do a repeated task on this game, YPP Puzzle Pirates and I was wondering if it was possible to script it. My task would be to start at a dock, click on the port arrow, choose a ship (a different one each time and in order preferably), go to its hold, select materials to be moved and move them to a set ship (the same one each time), then return to the starting position. If this is possible would I first need a set of the games coding (which uses javascript) to be obtained so it could be read and a script used upon it. Thank you Yours faithfully, Nathan Finney. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sun Oct 24 10:10:15 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 24 Oct 2010 09:10:15 +0100 Subject: [Tutor] Scripting-Puzzle Pirates References: Message-ID: "Nathan Finney" wrote > So I got bored of having to do a repeated task on this game, > YPP Puzzle Pirates and I was wondering if it was possible > to script it. No idea, never heard of it. > If this is possible would I first need a set of the games coding > (which uses javascript) to be obtained so it could be read and > a script used upon it. Probably, but as this is a Python mailing list you are probably asking in the wrong place. Try a Javascript forum or better still, one dedicated to the game in question. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From lie.1296 at gmail.com Sun Oct 24 12:05:30 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 24 Oct 2010 21:05:30 +1100 Subject: [Tutor] Scripting-Puzzle Pirates In-Reply-To: References: Message-ID: On 10/24/10 15:33, Nathan Finney wrote: > Hey, > > So I got bored of having to do a repeated task on this game, YPP Puzzle > Pirates and I was wondering if it was possible to script it. Even if you can (hint: no, you can't), most games consider writing scripts to do tasks as botting, i.e. cheating. > My task > would be to start at a dock, click on the port arrow, choose a ship (a > different one each time and in order preferably), go to its hold, select > materials to be moved and move them to a set ship (the same one each > time), then return to the starting position. Now, that would be stealing or just rude, even if you do it manually. > If this is possible would I first need a set of the games coding (which > uses javascript) to be obtained so it could be read and a script used > upon it. Java is not the same as Javascript. Puzzle Pirate is a Java game. From rdmoores at gmail.com Sun Oct 24 13:32:43 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Sun, 24 Oct 2010 04:32:43 -0700 Subject: [Tutor] What's the best way to model an unfair coin? Message-ID: What's the best way to model an unfair coin? This is one way to do it, I suppose: Create a list containing only 'H's and 'T's. If you want the coin to have the probability of a head to be 6/11, ['H', 'H', 'H', 'H', 'H', 'H', 'T', 'T', 'T', 'T', 'T'] is the list to use. Use random.choice on the list, for a 6/11 heads probability. See . That's the only way I can think of. But surely there's a better, more general solution. What if the probability I want is an irrational number, such as 1/e? Sure, I can calculate a fraction that's as close to that irrational number as I want, but.. Am I missing something that's already there in Python 2.6 or 3.1 (the 2 I have)? Dick Moores From evert.rol at gmail.com Sun Oct 24 13:54:16 2010 From: evert.rol at gmail.com (Evert Rol) Date: Sun, 24 Oct 2010 13:54:16 +0200 Subject: [Tutor] What's the best way to model an unfair coin? In-Reply-To: References: Message-ID: <1F4296E5-B373-43FD-B2A1-5B175CCE7263@gmail.com> > What's the best way to model an unfair coin? > > This is one way to do it, I suppose: Create a list containing only > 'H's and 'T's. If you want the coin to have the probability of a head > to be 6/11, > > ['H', 'H', 'H', 'H', 'H', 'H', 'T', 'T', 'T', 'T', 'T'] > > is the list to use. Use random.choice on the list, for a 6/11 heads > probability. > > See . > > That's the only way I can think of. But surely there's a better, more > general solution. What if the probability I want is an irrational > number, such as 1/e? Sure, I can calculate a fraction that's as close > to that irrational number as I want, but.. My statistics might be too rusty to have this correct, but I would do something similar as you have now, just not for integer numbers. Assuming you only want True or False, you can use a uniform distribution, through random.random(), and see if the result is lower or higher than your probability. Eg: return random.random() < 1/e or return random.random() < 6/11. will return True or False with your specificied probability. Again, I just might be overlooking something in the statistics. Cheers, Evert Btw, to be pedantic, 1/e is not an irrational number, just a real number. i/e would be. > > Am I missing something that's already there in Python 2.6 or 3.1 (the > 2 I have)? > > Dick Moores > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From rdmoores at gmail.com Sun Oct 24 13:56:11 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Sun, 24 Oct 2010 04:56:11 -0700 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: References: Message-ID: On Sat, Oct 23, 2010 at 02:28, col speed wrote: > > >> >> >> Message: 7 >> Date: Fri, 22 Oct 2010 21:48:29 -0700 >> From: "Richard D. Moores" >> To: "Steven D'Aprano" >> Cc: tutor at python.org >> Subject: Re: [Tutor] What does "TypeError: 'int' object is not >> ? ? ? ?iterable" ? ? ? mean? >> Message-ID: >> ? ? ? ? >> Content-Type: text/plain; charset=UTF-8 >> >> It's great to have you chime in, Steven. I do wish you would stop >> pulling your punches, however. ;) >> >> <-snip> >> I've never seen that convention, but I'll try to follow it. >> >> > >> > (BTW, I really hate the name "floatt". It makes me think you're >> > stuttering.) >> >> I'll use "x" instead. Anything you'd like to say about the rest of the >> script? >> >> Thanks, Steven. >> >> Dick >> >> >> ------------------------------ >> Excuse me, I'm not a professional. Rather than "x", I would use "float_" >> or even "not_int", as mentioned in PEP8: > > If a function argument's name clashes with a reserved keyword, it is > generally better to append a single trailing underscore rather than > use > an abbreviation or spelling corruption. Thus "print_" is better than > > "prnt". (Perhaps better is to avoid such clashes by using a synonym.) > Steven knows his subject, please don't answer like this. And I have a request of you: Please don't change the Subject header when you reply. I didn't see your post with it's important suggestion until just now. Thanks for the quote from PEP8. I went with "myfloat", on Dave Angel's suggestion, but float_ looks good as well. 'not_int' is not so good, because many kinds of objects are "not_int"s. As for Steven, you're absolutely correct -- and I've learned a lot from him. Dave Angel wrote: > Sometimes Steven's style can be a bit caustic, but there's almost always a > few important nuggets. and I replied: >Absolutely there are! And I have no problem with his style. I just >couldn't hold back what I intended to be a gentle jab of sarcasm. >Dangerous in email--especially an email list. Dick From steve at pearwood.info Sun Oct 24 14:17:13 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 24 Oct 2010 23:17:13 +1100 Subject: [Tutor] What's the best way to model an unfair coin? In-Reply-To: References: Message-ID: <4CC423C9.5050500@pearwood.info> Richard D. Moores wrote: > What's the best way to model an unfair coin? Let probability of heads = p, where 0 <= p <= 1 Then probability of tails = 1-p. if random.random() <= p: print("got heads") else: print("got tails") [...] > That's the only way I can think of. But surely there's a better, more > general solution. What if the probability I want is an irrational > number, such as 1/e? Sure, I can calculate a fraction that's as close > to that irrational number as I want, but.. Well, technically speaking all floats in Python are rational numbers, since they're base-2 floating point numbers. But the difference between (say) float pi and the true irrational number ? is around about 0.0000000000000001, so close enough for most purposes. -- Steven From alan.gauld at btinternet.com Sun Oct 24 14:18:52 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 24 Oct 2010 13:18:52 +0100 Subject: [Tutor] What's the best way to model an unfair coin? References: Message-ID: "Richard D. Moores" wrote > 'H's and 'T's. If you want the coin to have the probability of a > head > to be 6/11, > > ['H', 'H', 'H', 'H', 'H', 'H', 'T', 'T', 'T', 'T', 'T'] > > is the list to use. Use random.choice on the list, for a 6/11 heads > probability. That will work but as you say is not very general. You could write a function that takers the desired probablity as an input and returns a result based on that. The simplest way its to generate a random number between 0 and 1 and compare to the required probability expressed as a decimal fraction. In pseudo code: def coinToss(prob = 0.5): rand = random() if rand >= prob: return True else: return False print "Heads" if coinToss(6/11) else "Tails" > Am I missing something that's already there in Python 2.6 or 3.1 > (the > 2 I have)? There may well be functions in some of the modules that do it but the approach above suffices for most applications. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From evert.rol at gmail.com Sun Oct 24 14:24:14 2010 From: evert.rol at gmail.com (Evert Rol) Date: Sun, 24 Oct 2010 14:24:14 +0200 Subject: [Tutor] What's the best way to model an unfair coin? In-Reply-To: References: <1F4296E5-B373-43FD-B2A1-5B175CCE7263@gmail.com> Message-ID: <640D24EC-1CA0-47FA-83F3-72A8E6CD5DB5@gmail.com> > Btw, to be pedantic, 1/e is not an irrational number, just a real number. i/e would be. My bad: irrational != imaginary. And real = irrational. Things are definitely a bit rusty... From steve at pearwood.info Sun Oct 24 14:43:30 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 24 Oct 2010 23:43:30 +1100 Subject: [Tutor] What's the best way to model an unfair coin? In-Reply-To: <1F4296E5-B373-43FD-B2A1-5B175CCE7263@gmail.com> References: <1F4296E5-B373-43FD-B2A1-5B175CCE7263@gmail.com> Message-ID: <4CC429F2.3040905@pearwood.info> Evert Rol wrote: > Btw, to be pedantic, 1/e is not an irrational number, just a real number. i/e would be. Actually, Richard is correct. Like ?, e and 1/e are irrational numbers. "Irrational" means the number is not rational, in the sense of *ratio*, not sanity :) There is no exact ratio of integers which give *exactly* ?, e, 1/e, or many other numbers, although you can get as close an approximation as you like. Hence they are known as irrationals. Many surds, like square root of 2, are irrational. Not all of them though: sqrt(4) is rational. Mathematicians deal with a hierarchy of numbers: Natural numbers: 1, 2, 3, 4, ... Integers: 0, 1, -1, 2, -2, 3, -3, ... Rationals: numbers that can be written as ratios of two integers, e.g. 1/2, 3/7, ... Irrationals: numbers that can't be written as such ratios. The rationals and the irrationals together make up the real numbers. "Real" in the sense that when you measure real quantities like distance or weight, you get a real number. The number system has been extended to "imaginary" numbers, complex numbers, quaternions, surreal numbers, and other more exotic families. Python includes support for complex numbers, using "j" as the symbol for the imaginary unit: >>> 2 + 3j # real number + imaginary number makes a complex number (2+3j) (Mathematicians will be more familiar with the symbol i, but electrical engineers usually use j.) -- Steven From rdmoores at gmail.com Sun Oct 24 14:45:56 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Sun, 24 Oct 2010 05:45:56 -0700 Subject: [Tutor] Problem with python In-Reply-To: <201010202113.39815.steve@pearwood.info> References: <201010202113.39815.steve@pearwood.info> Message-ID: On Wed, Oct 20, 2010 at 03:13, Steven D'Aprano wrote: > Let's start with an easy example: factorial(0). When Python sees > factorial(0), it does this: > > Call factorial with argument 0: > * if n == 0 <-- this is true, so execute the "if" block: > ? ?return 1 > > so the function factorial(0) returns the value 1. All that goes on > behind the scenes. What *we* see is: > >>>> factorial(0) > 1 > > That part is easy. Now, the recursive part. What does Python do when you > call factorial(1)? > > Call factorial with argument 1: > * if n == 0 <-- this is false, so execute the "else" block: > ? ?recurse = factorial(n-1) > > Here Python is calling a function. It happens to be the same function, > but Python doesn't care about that, and neither should you. So Python > simply continues: > > ? ?Call factorial with argument 1-1 = 0: > > Now, we've already seen this above, but Python does the simplest thing > that could work: it calculates the answer from scratch: > I won't quote the whole thing, which should be enshrined somewhere. I just wanted to note that Steven is a great teacher! Dick Moores (teacher, retired) From rdmoores at gmail.com Sun Oct 24 15:33:09 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Sun, 24 Oct 2010 06:33:09 -0700 Subject: [Tutor] What's the best way to model an unfair coin? In-Reply-To: <4CC423C9.5050500@pearwood.info> References: <4CC423C9.5050500@pearwood.info> Message-ID: On Sun, Oct 24, 2010 at 05:17, Steven D'Aprano wrote: > Let probability of heads = p, where 0 <= p <= 1 > Then probability of tails = 1-p. > > if random.random() <= p: print("got heads") > else: print("got tails") My thanks to Evert, Steven, and Alan. I should have thought of that solution. Actually, I used the unfair coin model as the simplest example of the kind of thing I want to do -- which is to model the USD->Yen exchange rate. I want the next quote to vary in a controlled random way, by assigning probabilities to various possible changes in the rate. See . So I assign probability 1/40 to a change of plus or minus .05; 3/40 to .04; 5/40 to .03, etc. An analogy to this would be an unfair 6-sided die, with each side assigned probabilities slightly differing from 1/6 (but totaling 1, of course). I can't see off-hand how to apply the Evert-Steven-Alan solution to these, but is it possible? I'd just like a yes or no -- if yes, I'd like to try to work it out myself. Thanks, Dick From evert.rol at gmail.com Sun Oct 24 15:36:18 2010 From: evert.rol at gmail.com (Evert Rol) Date: Sun, 24 Oct 2010 15:36:18 +0200 Subject: [Tutor] What's the best way to model an unfair coin? In-Reply-To: References: <4CC423C9.5050500@pearwood.info> Message-ID: > Actually, I used the unfair coin model as the simplest example of the > kind of thing I want to do -- which is to model the USD->Yen exchange > rate. I want the next quote to vary in a controlled random way, by > assigning probabilities to various possible changes in the rate. See > . So I assign probability 1/40 > to a change of plus or minus .05; 3/40 to .04; 5/40 to .03, etc. > > An analogy to this would be an unfair 6-sided die, with each side > assigned probabilities slightly differing from 1/6 (but totaling 1, of > course). I can't see off-hand how to apply the Evert-Steven-Alan > solution to these, but is it possible? I'd just like a yes or no -- if > yes, I'd like to try to work it out myself. Yes, and in a very similar way. Cheers, Evert From rdmoores at gmail.com Sun Oct 24 16:36:21 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Sun, 24 Oct 2010 07:36:21 -0700 Subject: [Tutor] What's the best way to model an unfair coin? In-Reply-To: References: <4CC423C9.5050500@pearwood.info> Message-ID: On Sun, Oct 24, 2010 at 06:36, Evert Rol wrote: >> Actually, I used the unfair coin model as the simplest example of the >> kind of thing I want to do -- which is to model the USD->Yen exchange >> rate. I want the next quote to vary in a controlled random way, by >> assigning probabilities to various possible changes in the rate. See >> . So I assign probability 1/40 >> to a change of plus or minus .05; 3/40 to .04; 5/40 to .03, etc. >> >> An analogy to this would be an unfair 6-sided die, with each side >> assigned probabilities slightly differing from 1/6 (but totaling 1, of >> course). I can't see off-hand how to apply the Evert-Steven-Alan >> solution to these, but is it possible? I'd just like a yes or no -- if >> yes, I'd like to try to work it out myself. > > Yes, and in a very similar way. Thanks, Evert. I'm on it. Dick From artclamedia at yahoo.com Sun Oct 24 17:35:46 2010 From: artclamedia at yahoo.com (Art Cla Media) Date: Sun, 24 Oct 2010 08:35:46 -0700 (PDT) Subject: [Tutor] Tutor Digest, Vol 80, Issue 108 In-Reply-To: Message-ID: <489142.90541.qm@web114409.mail.gq1.yahoo.com> mai date? in pula? mea? cu masurile? tale? mai acrit? la coie? mai termina? in plm cu? ele --- On Sun, 10/24/10, tutor-request at python.org wrote: From: tutor-request at python.org Subject: Tutor Digest, Vol 80, Issue 108 To: tutor at python.org Date: Sunday, October 24, 2010, 12:25 PM Send Tutor mailing list submissions to ??? tutor at python.org To subscribe or unsubscribe via the World Wide Web, visit ??? http://mail.python.org/mailman/listinfo/tutor or, via email, send a message with subject or body 'help' to ??? tutor-request at python.org You can reach the person managing the list at ??? tutor-owner at python.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Tutor digest..." Today's Topics: ???1. Re: Scripting-Puzzle Pirates (Lie Ryan) ???2. What's the best way to model an unfair coin? (Richard D. Moores) ???3. Re: What's the best way to model an unfair coin? (Evert Rol) ???4. Re: What does "TypeError: 'int' object is not iterable"??? mean? ? ? ? (Richard D. Moores) ???5. Re: What's the best way to model an unfair coin? (Steven D'Aprano) ???6. Re: What's the best way to model an unfair coin? (Alan Gauld) ???7. Re: What's the best way to model an unfair coin? (Evert Rol) ---------------------------------------------------------------------- Message: 1 Date: Sun, 24 Oct 2010 21:05:30 +1100 From: Lie Ryan To: tutor at python.org Subject: Re: [Tutor] Scripting-Puzzle Pirates Message-ID: Content-Type: text/plain; charset=ISO-8859-1 On 10/24/10 15:33, Nathan Finney wrote: > Hey, >? > So I got bored of having to do a repeated task on this game, YPP Puzzle > Pirates and I was wondering if it was possible to script it. Even if you can (hint: no, you can't), most games consider writing scripts to do tasks as botting, i.e. cheating. > My task > would be to start at a dock, click on the port arrow, choose a ship (a > different one each time and in order preferably), go to its hold, select > materials to be moved and move them to a set ship (the same one each > time), then return to the starting position. Now, that would be stealing or just rude, even if you do it manually. > If this is possible would I first need a set of the games coding (which > uses javascript) to be obtained so it could be read and a script used > upon it. Java is not the same as Javascript. Puzzle Pirate is a Java game. ------------------------------ Message: 2 Date: Sun, 24 Oct 2010 04:32:43 -0700 From: "Richard D. Moores" To: tutor at python.org Subject: [Tutor] What's the best way to model an unfair coin? Message-ID: ??? Content-Type: text/plain; charset=UTF-8 What's the best way to model an unfair coin? This is one way to do it, I suppose: Create a list containing only 'H's and 'T's. If you want the coin to have the probability of a head to be 6/11, ['H', 'H', 'H', 'H', 'H', 'H', 'T', 'T', 'T', 'T', 'T'] is the list to use. Use random.choice on the list, for a 6/11 heads probability. See . That's the only way I can think of. But surely there's a better, more general solution. What if the probability I want is an irrational number, such as 1/e? Sure, I can calculate a fraction that's as close to that irrational number as I want, but.. Am I missing something that's already there in Python 2.6 or 3.1 (the 2 I have)? Dick Moores ------------------------------ Message: 3 Date: Sun, 24 Oct 2010 13:54:16 +0200 From: Evert Rol To: "Richard D. Moores" Cc: tutor at python.org Subject: Re: [Tutor] What's the best way to model an unfair coin? Message-ID: <1F4296E5-B373-43FD-B2A1-5B175CCE7263 at gmail.com> Content-Type: text/plain; charset=us-ascii > What's the best way to model an unfair coin? > > This is one way to do it, I suppose: Create a list containing only > 'H's and 'T's. If you want the coin to have the probability of a head > to be 6/11, > > ['H', 'H', 'H', 'H', 'H', 'H', 'T', 'T', 'T', 'T', 'T'] > > is the list to use. Use random.choice on the list, for a 6/11 heads > probability. > > See . > > That's the only way I can think of. But surely there's a better, more > general solution. What if the probability I want is an irrational > number, such as 1/e? Sure, I can calculate a fraction that's as close > to that irrational number as I want, but.. My statistics might be too rusty to have this correct, but I would do something similar as you have now, just not for integer numbers. Assuming you only want True or False, you can use a uniform distribution, through random.random(), and see if the result is lower or higher than your probability. Eg: return random.random() < 1/e or return random.random() < 6/11. will return True or False with your specificied probability. Again, I just might be overlooking something in the statistics. Cheers, ? Evert Btw, to be pedantic, 1/e is not an irrational number, just a real number. i/e would be. > > Am I missing something that's already there in Python 2.6 or 3.1 (the > 2 I have)? > > Dick Moores > _______________________________________________ > Tutor maillist? -? Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ------------------------------ Message: 4 Date: Sun, 24 Oct 2010 04:56:11 -0700 From: "Richard D. Moores" To: col speed Cc: tutor at python.org Subject: Re: [Tutor] What does "TypeError: 'int' object is not ??? iterable"??? mean? Message-ID: ??? Content-Type: text/plain; charset=UTF-8 On Sat, Oct 23, 2010 at 02:28, col speed wrote: > > >> >> >> Message: 7 >> Date: Fri, 22 Oct 2010 21:48:29 -0700 >> From: "Richard D. Moores" >> To: "Steven D'Aprano" >> Cc: tutor at python.org >> Subject: Re: [Tutor] What does "TypeError: 'int' object is not >> ? ? ? ?iterable" ? ? ? mean? >> Message-ID: >> ? ? ? ? >> Content-Type: text/plain; charset=UTF-8 >> >> It's great to have you chime in, Steven. I do wish you would stop >> pulling your punches, however. ;) >> >> <-snip> >> I've never seen that convention, but I'll try to follow it. >> >> > >> > (BTW, I really hate the name "floatt". It makes me think you're >> > stuttering.) >> >> I'll use "x" instead. Anything you'd like to say about the rest of the >> script? >> >> Thanks, Steven. >> >> Dick >> >> >> ------------------------------ >> Excuse me, I'm not a professional. Rather than "x", I would use "float_" >> or even "not_int", as mentioned in PEP8: > > If a function argument's name clashes with a reserved keyword, it is >? ? ???generally better to append a single trailing underscore rather than > use >? ? ???an abbreviation or spelling corruption.? Thus "print_" is better than > > "prnt". (Perhaps better is to avoid such clashes by using a synonym.) > Steven knows his subject, please don't answer like this. And I have a request of you: Please don't change the Subject header when you reply. I didn't see your post with it's important suggestion until just now. Thanks for the quote from PEP8.? I went with "myfloat", on Dave Angel's suggestion, but float_ looks good as well. 'not_int' is not so good, because many kinds of objects are "not_int"s. As for Steven, you're absolutely correct -- and I've learned a lot from him. Dave Angel wrote: > Sometimes Steven's style can be a bit caustic, but there's almost always a > few important nuggets. and I replied: >Absolutely there are! And I have no problem with his style. I just >couldn't hold back what I intended to be a gentle jab of sarcasm. >Dangerous in email--especially an email list. Dick ------------------------------ Message: 5 Date: Sun, 24 Oct 2010 23:17:13 +1100 From: Steven D'Aprano To: tutor at python.org Subject: Re: [Tutor] What's the best way to model an unfair coin? Message-ID: <4CC423C9.5050500 at pearwood.info> Content-Type: text/plain; charset=UTF-8; format=flowed Richard D. Moores wrote: > What's the best way to model an unfair coin? Let probability of heads = p, where 0 <= p <= 1 Then probability of tails = 1-p. if random.random() <= p: print("got heads") else: print("got tails") [...] > That's the only way I can think of. But surely there's a better, more > general solution. What if the probability I want is an irrational > number, such as 1/e? Sure, I can calculate a fraction that's as close > to that irrational number as I want, but.. Well, technically speaking all floats in Python are rational numbers, since they're base-2 floating point numbers. But the difference between (say) float pi and the true irrational number ? is around about 0.0000000000000001, so close enough for most purposes. -- Steven ------------------------------ Message: 6 Date: Sun, 24 Oct 2010 13:18:52 +0100 From: "Alan Gauld" To: tutor at python.org Subject: Re: [Tutor] What's the best way to model an unfair coin? Message-ID: Content-Type: text/plain; format=flowed; charset="iso-8859-1"; ??? reply-type=original "Richard D. Moores" wrote > 'H's and 'T's. If you want the coin to have the probability of a > head > to be 6/11, > > ['H', 'H', 'H', 'H', 'H', 'H', 'T', 'T', 'T', 'T', 'T'] > > is the list to use. Use random.choice on the list, for a 6/11 heads > probability. That will work but as you say is not very general. You could write a function that takers the desired probablity as an input and returns a result based on that. The simplest way its to generate a random number between 0 and 1 and compare to the required probability expressed as a decimal fraction. In pseudo code: def coinToss(prob = 0.5): ? ? rand = random() ? ? if rand >= prob: return True ? ? else: return False print "Heads" if coinToss(6/11) else "Tails" > Am I missing something that's already there in Python 2.6 or 3.1 > (the > 2 I have)? There may well be functions in some of the modules that do it but the approach above suffices for most applications. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ------------------------------ Message: 7 Date: Sun, 24 Oct 2010 14:24:14 +0200 From: Evert Rol To: "Richard D. Moores" ,??? python mail list ??? Subject: Re: [Tutor] What's the best way to model an unfair coin? Message-ID: <640D24EC-1CA0-47FA-83F3-72A8E6CD5DB5 at gmail.com> Content-Type: text/plain; charset=us-ascii > Btw, to be pedantic, 1/e is not an irrational number, just a real number. i/e would be. My bad: irrational != imaginary. And real = irrational. Things are definitely a bit rusty... ------------------------------ _______________________________________________ Tutor maillist? -? Tutor at python.org http://mail.python.org/mailman/listinfo/tutor End of Tutor Digest, Vol 80, Issue 108 ************************************** -------------- next part -------------- An HTML attachment was scrubbed... URL: From rdmoores at gmail.com Sun Oct 24 17:36:14 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Sun, 24 Oct 2010 08:36:14 -0700 Subject: [Tutor] What's the best way to model an unfair coin? In-Reply-To: References: <4CC423C9.5050500@pearwood.info> Message-ID: Here's my model unfair die. Dick From ljmamoreira at gmail.com Sun Oct 24 17:46:22 2010 From: ljmamoreira at gmail.com (Jose Amoreira) Date: Sun, 24 Oct 2010 16:46:22 +0100 Subject: [Tutor] What's the best way to model an unfair coin? In-Reply-To: References: Message-ID: <201010241646.22465.ljmamoreira@gmail.com> On Sunday, October 24, 2010 01:18:52 pm Alan Gauld wrote: > In pseudo code: > > def coinToss(prob = 0.5): > rand = random() > if rand >= prob: return True > else: return False > > print "Heads" if coinToss(6/11) else "Tails" > The only problem with this snippet is integer division: 6/11=0, at least in Python 2.6, so that the final line will always print "Heads". But wait! This is pseudo code! Ah, OK. Then 6/11=0.545454..., and Alan was right (as usual). This pseudo code snippet is almost Python code. It looks like Python, it smells like Python, it even runs as Python, if you import random from random beforehand. Python really is executable pseudo code! Cheers Jose Amoreira From lie.1296 at gmail.com Sun Oct 24 17:05:24 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 25 Oct 2010 02:05:24 +1100 Subject: [Tutor] What's the best way to model an unfair coin? In-Reply-To: <201010241646.22465.ljmamoreira@gmail.com> References: <201010241646.22465.ljmamoreira@gmail.com> Message-ID: On 10/25/10 02:46, Jose Amoreira wrote: > On Sunday, October 24, 2010 01:18:52 pm Alan Gauld wrote: > >> In pseudo code: >> >> def coinToss(prob = 0.5): >> rand = random() >> if rand >= prob: return True >> else: return False >> >> print "Heads" if coinToss(6/11) else "Tails" >> > > The only problem with this snippet is integer division: 6/11=0, at least in > Python 2.6, so that the final line will always print "Heads". > > But wait! This is pseudo code! Ah, OK. Then 6/11=0.545454..., and Alan was > right (as usual). Except for the missing import (which is traditionally omitted in mailing list discussions when the context makes it clear), Alan's snippet is correct in Python 3, which defaults to real division. Though, I'd probably writes it differently: def coinToss(prob = 0.5): rand = random() return rand >= prob or even: def coinToss(prob = 0.5): return random() >= prob From lie.1296 at gmail.com Sun Oct 24 17:33:55 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 25 Oct 2010 02:33:55 +1100 Subject: [Tutor] What does "TypeError: 'int' object is not iterable" mean? In-Reply-To: References: Message-ID: On 10/23/10 01:19, David Hutto wrote: > If I understand what i just said correctly, it just means it tells the > string what type to convert from when placing it into the final > result. basically, when doing this %-interpolation, python does this: ("NEW LOW: %%.%sf at %%s" % i) % (lowz, timestamp) do the first interpolation: "NEW LOW: %%.%sf at %%s" % i it's to be noted that %% is replaced by a single %, and %s is replaced by the right argument of %, so if i == 5 it now becomes: "NEW LOW: %.5sf at %s" % (lowz, timestamp) and now do the second interpolation, lowz is formatted with %.5f which means a floating value (f) with 5 decimal place, and timestamp is inserted in place of %s; so if you have lowz = 81.345678901234 and timestamp = "last year": "NEW LOW: 81.34567 at last year" However, as Steven noted, the complex, 2-phase interpolation can be simplified using the '*' decimal place specifier: ("NEW LOW: %%.%sf at %%s" % i) % (lowz, timestamp) # is equivalent with: "NEW LOW: %.*f at %s" % (i, lowz, timestamp) and Steven also remarked that now the interpolation is so simple, there is very little benefit in separating it into a different function. From steve at pearwood.info Sun Oct 24 18:22:40 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 25 Oct 2010 03:22:40 +1100 Subject: [Tutor] What's the best way to model an unfair coin? In-Reply-To: References: <4CC423C9.5050500@pearwood.info> Message-ID: <4CC45D50.80406@pearwood.info> Richard D. Moores wrote: > Actually, I used the unfair coin model as the simplest example of the > kind of thing I want to do -- which is to model the USD->Yen exchange > rate. I want the next quote to vary in a controlled random way, by > assigning probabilities to various possible changes in the rate. See > . So I assign probability 1/40 > to a change of plus or minus .05; 3/40 to .04; 5/40 to .03, etc. Another approach you might take is to model the change as a normal distribution (bell curve probability) rather than uniform. This is probably more realistic. It would make most sense to have it symmetrical around zero, so you want a random number with a normal distribution, a mean of zero, and a standard deviation yet to be determined. To determine the standard deviation, use this rule of thumb: for a normal (bell) curve, approximately 68% of events are plus or minus one standard deviation from the mean; 95% are plus or minus two std deviations; and 99.7% are plus or minus three std deviations. So if you decide that 99.7% of the time the change in exchange rate should be less than 1.00, for example, that corresponds to a std deviation of 0.333. You then generate a normally-distributed value using the random module, round it to two decimal places to correspond to cents, and Bob's your uncle. -- Steven From rdmoores at gmail.com Sun Oct 24 19:13:57 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Sun, 24 Oct 2010 10:13:57 -0700 Subject: [Tutor] What's the best way to model an unfair coin? In-Reply-To: <4CC45D50.80406@pearwood.info> References: <4CC423C9.5050500@pearwood.info> <4CC45D50.80406@pearwood.info> Message-ID: On Sun, Oct 24, 2010 at 09:22, Steven D'Aprano wrote: > Richard D. Moores wrote: > >> Actually, I used the unfair coin model as the simplest example of the >> kind of thing I want to do -- which is to model the USD->Yen exchange >> rate. I want the next quote to vary in a controlled random way, by >> assigning probabilities to various possible changes in the rate. See >> . So I assign probability 1/40 >> to a change of plus or minus .05; 3/40 to .04; 5/40 to .03, etc. > > Another approach you might take is to model the change as a normal > distribution (bell curve probability) rather than uniform. This is probably > more realistic. It would make most sense to have it symmetrical around zero, > so you want a random number with a normal distribution, a mean of zero, and > a standard deviation yet to be determined. > > To determine the standard deviation, use this rule of thumb: for a normal > (bell) curve, approximately 68% of events are plus or minus one standard > deviation from the mean; 95% are plus or minus two std deviations; and 99.7% > are plus or minus three std deviations. > > So if you decide that 99.7% of the time the change in exchange rate should > be less than 1.00, for example, that corresponds to a std deviation of > 0.333. > > You then generate a normally-distributed value using the random module, > round it to two decimal places to correspond to cents, and Bob's your uncle. Ah, use random.normalvariate(mu, sigma), or random.gauss(mu, sigma) where mu is the mean, sigma the standard deviation. Great idea. Thanks! Bob IS my uncle! How'd you know? (First I'd heard that expression. Interesting. ) Dick From josep.m.fontana at gmail.com Sun Oct 24 20:16:19 2010 From: josep.m.fontana at gmail.com (Josep M. Fontana) Date: Sun, 24 Oct 2010 20:16:19 +0200 Subject: [Tutor] Problems with partial string matching Message-ID: Hi, As I said in another message with the heading "Using contents of a document to change file names", I'm trying to learn Python "by doing" and I was working on a little project where I had to change the names of the files in a directory according to some codes contained in a CSV file. With the help of the participants in this list I managed to overcome some of the first obstacles I found and managed to create a dictionary out of the structured contents of the CSV file. Unfortunately, I didn't have much time to continue working on my project and I didn't get back to the script until now. I have encountered another hurdle, though, which doesn't allow me to go on. The problem might be *very* simple to solve but I've spent the last couple of hours checking manuals and doing searches on the internet without having much success. What I'm trying to do now is to use the dictionary I created (with entries such as {'I-02': '1399', 'I-01': '1374',...}) to iterate over the file names I want to change and do the necessary string substitutions. If one of the keys in the dictionary matches the code that is found at the beginning of every file, then the value of the dictionary representing the year in which the text was written is appended at the end of the file name. Here is what I've done so far: ------------------------------ import os, sys, glob, re fileNameYear = open(r'/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNameYear.txt', "U").readlines() name_year = {} for line in fileNameYear: #File objects have built-in iteration name, year = line.strip().split(',') name_year[name] = year #effectively creates the dictionary by creating keys with the element 'name' returned by the loop and assigning them values corresponding to the element 'year' --> !d[key] = value" means Set d[key] to value. os.getcwd() os.chdir('/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1') file_names = glob.glob('*.txt') for name_of_file in file_names: if name_of_file.startswith(name): re.sub('__', '__' + year, name_of_file) #The files have names such as 'B-13-Viatges_Marco_Polo__.txt' so the first argument in re.sub() is the string '__' which should be replaced by the same string followed by the string corresponding to the year value in the dictionary (also a string) ------------------------------ I run this and I don't get any errors. The names of the files in the directory, however, are not changed. What am I doing wrong? As always, your help is greatly appreciated. Josep M. From joel.goldstick at gmail.com Sun Oct 24 20:45:04 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sun, 24 Oct 2010 14:45:04 -0400 Subject: [Tutor] Problems with partial string matching In-Reply-To: References: Message-ID: On Sun, Oct 24, 2010 at 2:16 PM, Josep M. Fontana wrote: > Hi, > > As I said in another message with the heading "Using contents of a > document to change file names", I'm trying to learn Python "by doing" > and I was working on a little project where I had to change the names > of the files in a directory according to some codes contained in a CSV > file. With the help of the participants in this list I managed to > overcome some of the first obstacles I found and managed to create a > dictionary out of the structured contents of the CSV file. > > Unfortunately, I didn't have much time to continue working on my > project and I didn't get back to the script until now. I have > encountered another hurdle, though, which doesn't allow me to go on. > The problem might be *very* simple to solve but I've spent the last > couple of hours checking manuals and doing searches on the internet > without having much success. > > What I'm trying to do now is to use the dictionary I created (with > entries such as {'I-02': '1399', 'I-01': '1374',...}) to iterate over > the file names I want to change and do the necessary string > substitutions. If one of the keys in the dictionary matches the code > that is found at the beginning of every file, then the value of the > dictionary representing the year in which the text was written is > appended at the end of the file name. > > Here is what I've done so far: > ------------------------------ > import os, sys, glob, re > fileNameYear = > open(r'/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNameYear.txt', > "U").readlines() > name_year = {} > for line in fileNameYear: #File objects have built-in iteration > name, year = line.strip().split(',') > name_year[name] = year #effectively creates the dictionary by > creating keys with the element 'name' returned by the loop and > assigning them values corresponding to the element 'year' --> !d[key] > = value" means Set d[key] to value. > os.getcwd() > os.chdir('/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1') > file_names = glob.glob('*.txt') > for name_of_file in file_names: > if name_of_file.startswith(name): > re.sub('__', '__' + year, name_of_file) #The files have names > such as 'B-13-Viatges_Marco_Polo__.txt' so the first argument in > re.sub() is the string '__' which should be replaced by the same > string followed by the string corresponding to the year value in the > dictionary (also a string) > ------------------------------ > > I run this and I don't get any errors. The names of the files in the > directory, however, are not changed. What am I doing wrong? > > As always, your help is greatly appreciated. > > > Josep M. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > You are not renaming the file, just the string named name_of_file. Check out os.rename -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Sun Oct 24 21:52:59 2010 From: davea at ieee.org (Dave Angel) Date: Sun, 24 Oct 2010 15:52:59 -0400 Subject: [Tutor] Problems with partial string matching In-Reply-To: References: Message-ID: <4CC48E9B.6080302@ieee.org> On 2:59 PM, Josep M. Fontana wrote: > Hi, > > As I said in another message with the heading "Using contents of a > document to change file names", I'm trying to learn Python "by doing" > and I was working on a little project where I had to change the names > > I run this and I don't get any errors. The names of the files in the > directory, however, are not changed. What am I doing wrong? > > As always, your help is greatly appreciated. > > > Josep M. > You call re.sub(), but don't do anything with the result. Where do you call os.rename() ? DaveA From rdmoores at gmail.com Sun Oct 24 22:17:51 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Sun, 24 Oct 2010 13:17:51 -0700 Subject: [Tutor] What is random.triangular() used for Message-ID: In looking through the doc on the random module I came across random.triangular(): ======================================== random.triangular(low, high, mode) Return a random floating point number N such that low <= N <= high and with the specified mode between those bounds. The low and high bounds default to zero and one. The mode argument defaults to the midpoint between the bounds, giving a symmetric distribution. New in version 2.6. ======================================== I fooled around with it a bit and wrote this rough demo: The skewing and centering possible by varying the mode, are obvious, but how is random.trangular() actually used? Dick Moores From emile at fenx.com Sun Oct 24 22:29:44 2010 From: emile at fenx.com (Emile van Sebille) Date: Sun, 24 Oct 2010 13:29:44 -0700 Subject: [Tutor] What is random.triangular() used for In-Reply-To: References: Message-ID: On 10/24/2010 1:17 PM Richard D. Moores said... > In looking through the doc on the random module I came across > random.triangular(): > > ======================================== > random.triangular(low, high, mode) > Return a random floating point number N such that low<= N<= high and > with the specified mode between those bounds. The low and high bounds > default to zero and one. The mode argument defaults to the midpoint > between the bounds, giving a symmetric distribution. > > New in version 2.6. > ======================================== > > I fooled around with it a bit and wrote this rough demo: > > > The skewing and centering possible by varying the mode, are obvious, > but how is random.trangular() actually used? > From people who would know found at http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.triangular.html """The triangular distribution is often used in ill-defined problems where the underlying distribution is not known, but some knowledge of the limits and mode exists. Often it is used in simulations.""" HTH, Emile From python at bdurham.com Sun Oct 24 22:43:50 2010 From: python at bdurham.com (python at bdurham.com) Date: Sun, 24 Oct 2010 16:43:50 -0400 Subject: [Tutor] Problem with python In-Reply-To: References: <201010202113.39815.steve@pearwood.info> Message-ID: <1287953030.28014.1401702921@webmail.messagingengine.com> > I just wanted to note that Steven is a great teacher! +1 Malcolm From rdmoores at gmail.com Sun Oct 24 22:57:51 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Sun, 24 Oct 2010 13:57:51 -0700 Subject: [Tutor] What is random.triangular() used for In-Reply-To: References: Message-ID: On Sun, Oct 24, 2010 at 13:29, Emile van Sebille wrote: > From people who would know found at > http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.triangular.html > > > """The triangular distribution is often used in ill-defined problems where > the underlying distribution is not known, but some knowledge of the limits > and mode exists. Often it is used in simulations.""" Emile, from the source code link on that page, I got # Draw values from the distribution and plot the histogram: import matplotlib.pyplot as plt h = plt.hist(np.random.triangular(-3, 0, 8, 100000), bins=200, normed=True) plt.show() Which when I run it I get Traceback (most recent call last): File "c:\P26Working\untitled-8.py", line 2, in h = plt.hist(np.random.triangular(-3, 0, 8, 100000), bins=200, normed=True) NameError: name 'np' is not defined Process terminated with an exit code of 1 The import goes OK: >>> import matplotlib.pyplot as plt >>> and apparently I have numpy, if that's relevant: >>> import numpy >>> but where do I get np? Dick From adam.jtm30 at gmail.com Sun Oct 24 23:13:14 2010 From: adam.jtm30 at gmail.com (Adam Bark) Date: Sun, 24 Oct 2010 22:13:14 +0100 Subject: [Tutor] What is random.triangular() used for In-Reply-To: References: Message-ID: <4CC4A16A.9050203@gmail.com> On 24/10/10 21:57, Richard D. Moores wrote: > On Sun, Oct 24, 2010 at 13:29, Emile van Sebille wrote: > > >> From people who would know found at >> http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.triangular.html >> >> >> """The triangular distribution is often used in ill-defined problems where >> the underlying distribution is not known, but some knowledge of the limits >> and mode exists. Often it is used in simulations.""" >> > Emile, > > from the source code link on that page, I got > > # Draw values from the distribution and plot the histogram: > > import matplotlib.pyplot as plt > h = plt.hist(np.random.triangular(-3, 0, 8, 100000), bins=200, > normed=True) > plt.show() > > Which when I run it I get > Traceback (most recent call last): > File "c:\P26Working\untitled-8.py", line 2, in > h = plt.hist(np.random.triangular(-3, 0, 8, 100000), bins=200, normed=True) > NameError: name 'np' is not defined > Process terminated with an exit code of 1 > > The import goes OK: > > >>>> import matplotlib.pyplot as plt >>>> >>>> > and apparently I have numpy, if that's relevant: > >>>> import numpy >>>> >>>> > but where do I get np? > > Dick > I think he may be using ipython but putting an "import numpy as np" should get that code to work. HTH Adam. From steve at pearwood.info Sun Oct 24 23:48:56 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 25 Oct 2010 08:48:56 +1100 Subject: [Tutor] What is random.triangular() used for In-Reply-To: References: Message-ID: <4CC4A9C8.60608@pearwood.info> Richard D. Moores wrote: > NameError: name 'np' is not defined [...] > but where do I get np? I believe that it is common in the scientific python world to do this: import numpy as np and then use np.FUNCTION instead of numpy.FUNCTION. -- Steven From rdmoores at gmail.com Mon Oct 25 01:13:38 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Sun, 24 Oct 2010 16:13:38 -0700 Subject: [Tutor] What is random.triangular() used for In-Reply-To: <4CC4A9C8.60608@pearwood.info> References: <4CC4A9C8.60608@pearwood.info> Message-ID: On Sun, Oct 24, 2010 at 14:48, Steven D'Aprano wrote: > Richard D. Moores wrote: >> >> NameError: name 'np' is not defined > > [...] >> >> but where do I get np? > > > I believe that it is common in the scientific python world to do this: > > import numpy as np > > and then use np.FUNCTION instead of numpy.FUNCTION. Yup. That was it. Beautiful! Dick From washakie at gmail.com Mon Oct 25 01:27:51 2010 From: washakie at gmail.com (John) Date: Mon, 25 Oct 2010 01:27:51 +0200 Subject: [Tutor] Writing Scripts. In-Reply-To: References: Message-ID: Autumn, Here's a basic script, if you save this in a file called hello.py and type 'python hello.py' at the prompt, or as others are saying using the python launcher, you should get some output. hth, john SCRIPT (include lines below here): #!/usr/bin/env python import os user = os.environ.get('USER') print "Hello %s!" % (user) ==== NOW SOME COMMENTS ==== the first line is somewhat 'unix or linux' dependent, but it tells your 'shell' to call the file using python the next line "import os" is a python import statement, telling python to import the os module the next line "user os.environ.get('USER')" is call a method (get) from the environ class of the os module and returns the name of the logged in user to the variable 'user' the next line is doing a few things. First it is calling the built in function 'print' to print a string "Hello %s!". However, the "%s" does something fancy. This is string substitution, so by having the variable 'user' after the second '%' sign, it replaces "%s" with that variable. Don't worry, it will come fast! Have fun with it!! On Wed, Oct 20, 2010 at 6:13 PM, Autumn Cutter wrote: > I just started learning Python last night and I'm a little stuck on how to write a Python script. > I'm using Python 2.5.0 on a Mac OS X 10.6.4. I've tried using programs like Idle, Applescript and Textmate but I'm still unable to write a script and run it successfully. I'm wondering if I'm using the write program or if I understand correctly how to write and run scripts. Help! :( > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Configuration `````````````````````````` Plone 2.5.3-final, CMF-1.6.4, Zope (Zope 2.9.7-final, python 2.4.4, linux2), Python 2.6 PIL 1.1.6 Mailman 2.1.9 Postfix 2.4.5 Procmail v3.22 2001/09/10 From davea at ieee.org Mon Oct 25 01:38:43 2010 From: davea at ieee.org (Dave Angel) Date: Sun, 24 Oct 2010 19:38:43 -0400 Subject: [Tutor] What is random.triangular() used for In-Reply-To: References: Message-ID: <4CC4C383.5010009@ieee.org> On 2:59 PM, Richard D. Moores wrote: > On Sun, Oct 24, 2010 at 13:29, Emile van Sebille wrote: > >> From people who would know found at >> http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.triangular.html >> >> >> """The triangular distribution is often used in ill-defined problems where >> the underlying distribution is not known, but some knowledge of the limits >> and mode exists. Often it is used in simulations.""" > Emile, > > from the source code link on that page, I got > > # Draw values from the distribution and plot the histogram: > > import matplotlib.pyplot as plt > h = plt.hist(np.random.triangular(-3, 0, 8, 100000), bins=200, > normed=True) > plt.show() > > Which when I run it I get > Traceback (most recent call last): > File "c:\P26Working\untitled-8.py", line 2, in > h = plt.hist(np.random.triangular(-3, 0, 8, 100000), bins=200, normed=True) > NameError: name 'np' is not defined > Process terminated with an exit code of 1 > > The import goes OK: > >>>> import matplotlib.pyplot as plt >>>> > and apparently I have numpy, if that's relevant: >>>> import numpy >>>> > but where do I get np? > > Dick > import numpy as np see http://matplotlib.sourceforge.net/plot_directive/mpl_examples/pylab_examples/histogram_demo.py for one example. I don't know anything about matplotlib as a whole, this was just a lucky shot DaveA From rdmoores at gmail.com Mon Oct 25 01:54:08 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Sun, 24 Oct 2010 16:54:08 -0700 Subject: [Tutor] What is random.triangular() used for In-Reply-To: <4CC4C383.5010009@ieee.org> References: <4CC4C383.5010009@ieee.org> Message-ID: On Sun, Oct 24, 2010 at 16:38, Dave Angel wrote: > import numpy as np > > see > http://matplotlib.sourceforge.net/plot_directive/mpl_examples/pylab_examples/histogram_demo.py > for one example. ?I don't know anything about matplotlib as a whole, this > was just a lucky shot Thanks, Dave. That's lovely: . I gotta learn this. Dick From mehgcap at gmail.com Mon Oct 25 03:20:49 2010 From: mehgcap at gmail.com (Alex Hall) Date: Sun, 24 Oct 2010 21:20:49 -0400 Subject: [Tutor] 2.6 vs 2.7: package compatibility? Message-ID: Hi all, I want to run a certain program from source. One dependency, Durus, keeps giving me an error that no one can figure out. Someone said that it will work if I use 2.7 instead of 2.6, but a lot of packages I have installed work only on 2.6. I know I can install both, but here is the question: all these packages that say they need 2.6... would they work on 2.7? That is, is the 2.6 the only version of python with which they will work, or just the minimum version (I know about the incompatibility between 2.x and 3). I would rather have just one version of python running; it makes keeping track of what packages I have installed easier, and I never could get my idea of two system variables, one for each version, to work properly. TIA. -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From mail at timgolden.me.uk Mon Oct 25 09:16:22 2010 From: mail at timgolden.me.uk (Tim Golden) Date: Mon, 25 Oct 2010 08:16:22 +0100 Subject: [Tutor] 2.6 vs 2.7: package compatibility? In-Reply-To: References: Message-ID: <4CC52EC6.2050007@timgolden.me.uk> On 25/10/2010 02:20, Alex Hall wrote: > Hi all, > I want to run a certain program from source. One dependency, Durus, > keeps giving me an error that no one can figure out. Someone said that > it will work if I use 2.7 instead of 2.6, but a lot of packages I have > installed work only on 2.6. I know I can install both, but here is the > question: all these packages that say they need 2.6... would they work > on 2.7? I'm going to assume that you're running on Windows -- because you usually are :) The answer, then, is that pure python modules would need to be reinstalled into (or at least made visible to) the newer Python version, but that extension modules would need to be recompiled against the newer version -- possibly making use of a precompiled binary. Just to try, I've compiled Durus without issue on Python 2.6 from the tarball on their website: http://www.mems-exchange.org/software/durus/Durus-3.9.tar.gz but it's not clear from your message above whether it's the build which is the issue or some aspect of using it. Can you clarify? I'm quite happy to post up an .msi or something for you to use if that'll help. TJG From mehgcap at gmail.com Mon Oct 25 14:13:07 2010 From: mehgcap at gmail.com (Alex Hall) Date: Mon, 25 Oct 2010 08:13:07 -0400 Subject: [Tutor] 2.6 vs 2.7: package compatibility? In-Reply-To: <4CC52EC6.2050007@timgolden.me.uk> References: <4CC52EC6.2050007@timgolden.me.uk> Message-ID: On 10/25/10, Tim Golden wrote: > On 25/10/2010 02:20, Alex Hall wrote: >> Hi all, >> I want to run a certain program from source. One dependency, Durus, >> keeps giving me an error that no one can figure out. Someone said that >> it will work if I use 2.7 instead of 2.6, but a lot of packages I have >> installed work only on 2.6. I know I can install both, but here is the >> question: all these packages that say they need 2.6... would they work >> on 2.7? > > I'm going to assume that you're running on Windows -- because > you usually are :) The answer, then, is that pure python modules > would need to be reinstalled into (or at least made visible to) > the newer Python version, but that extension modules would need > to be recompiled against the newer version -- possibly making use > of a precompiled binary. When I ran the source of the program (http://www.qwitter-client.net) on 2.7 it worked, but it mentioned adding c:\python26\lib\site-packages to sys.path. I am not sure if this means it is accessing packages from 2.6... > > Just to try, I've compiled Durus without issue on Python 2.6 > from the tarball on their website: > > http://www.mems-exchange.org/software/durus/Durus-3.9.tar.gz > > but it's not clear from your message above whether it's the > build which is the issue or some aspect of using it. Can > you clarify? I'm quite happy to post up an .msi or something > for you to use if that'll help. It tells me that persistent_dict does not exist, when it clearly does. Another user had the exact same problem when running the source on 2.6, but he had no problem when running 2.7. If you had an msi to install Durus for 2.6 specifically, it would be interesting to see if the persistent_dict error went away... > > TJG > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From mail at timgolden.me.uk Mon Oct 25 14:23:05 2010 From: mail at timgolden.me.uk (Tim Golden) Date: Mon, 25 Oct 2010 13:23:05 +0100 Subject: [Tutor] 2.6 vs 2.7: package compatibility? In-Reply-To: References: <4CC52EC6.2050007@timgolden.me.uk> Message-ID: <4CC576A9.5090506@timgolden.me.uk> > It tells me that persistent_dict does not exist, when it clearly does. > Another user had the exact same problem when running the source on > 2.6, but he had no problem when running 2.7. If you had an msi to > install Durus for 2.6 specifically, it would be interesting to see if > the persistent_dict error went away... http://timgolden.me.uk/python/downloads/Durus-3.9.win32-py2.6.msi TJG From mehgcap at gmail.com Mon Oct 25 14:35:52 2010 From: mehgcap at gmail.com (Alex Hall) Date: Mon, 25 Oct 2010 08:35:52 -0400 Subject: [Tutor] 2.6 vs 2.7: package compatibility? In-Reply-To: <4CC576A9.5090506@timgolden.me.uk> References: <4CC52EC6.2050007@timgolden.me.uk> <4CC576A9.5090506@timgolden.me.uk> Message-ID: On 10/25/10, Tim Golden wrote: >> It tells me that persistent_dict does not exist, when it clearly does. >> Another user had the exact same problem when running the source on >> 2.6, but he had no problem when running 2.7. If you had an msi to >> install Durus for 2.6 specifically, it would be interesting to see if >> the persistent_dict error went away... > > http://timgolden.me.uk/python/downloads/Durus-3.9.win32-py2.6.msi > > TJG > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Thanks! I get an error in the source, but it is related to the program, not Durus; there seems to be no Durus error anymore. > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From rdmoores at gmail.com Mon Oct 25 14:46:27 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Mon, 25 Oct 2010 05:46:27 -0700 Subject: [Tutor] Need help with converting script using 2.6's urllib2 to Python 3.1 Message-ID: The lines below are the essence of a 2.6 script that gets the current USD/yen quote. I'd like to convert the script to 3.1, but I can't understand the docs for the 3.1 urllib module. Please someone tell me what to do. (Converting 'print rate' to 'print(rate)' I understand.) import urllib2 a = urllib2.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN').read(20500) b = a[19000:20500] idx_pricewrap = b.find('pricewrap') context = b[idx_pricewrap:idx_pricewrap+80] idx_bgLast = context.find('bgLast') rate = context[idx_bgLast+8:idx_bgLast+15] print rate Thanks, Dick Moores From susana.delgado_s at utzmg.edu.mx Mon Oct 25 17:10:32 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Mon, 25 Oct 2010 10:10:32 -0500 Subject: [Tutor] Python and excel writer: 'ascii' codec can't decode byte 0xf3 in position 49 Message-ID: Hello members: I'm starting an script for geospatial information using python and gdal binaries, but I have problems saving the data I get into an excel shee. At the beginning I thought it was because the characters the files have for some records, but I run the code in a way it saves the path for each file I'm looking for. The script throws th error: Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import imagen Traceback (most recent call last): File "", line 1, in File "imagen.py", line 20, in wrkbk.save('imagen.xls') File "C:\Python26\lib\site-packages\xlwt\Workbook.py", line 634, in save doc.save(filename, self.get_biff_data()) File "C:\Python26\lib\site-packages\xlwt\Workbook.py", line 609, in get_biff_d ata shared_str_table = self.__sst_rec() File "C:\Python26\lib\site-packages\xlwt\Workbook.py", line 571, in __sst_rec return self.__sst.get_biff_record() File "C:\Python26\lib\site-packages\xlwt\BIFFRecords.py", line 53, in get_biff _record self._add_to_sst(s) File "C:\Python26\lib\site-packages\xlwt\BIFFRecords.py", line 66, in _add_to_ sst u_str = upack2(s, self.encoding) File "C:\Python26\lib\site-packages\xlwt\UnicodeUtils.py", line 50, in upack2 us = unicode(s, encoding) UnicodeDecodeError: 'ascii' codec can't decode byte 0xf3 in position 49: ordinal not in range(128) >>> My code is: import os, time, socket from xlwt import Workbook from osgeo import gdal from osgeo.gdalconst import * from PIL import Image gdal.AllRegister() file_list = [] folders = None # look in this (root) folder for files with specified extension for root, folders, files in os.walk( "C:\\" ): file_list.extend(os.path.join(root,fi) for fi in files if fi.endswith(".tif") or fi.endswith(".tiff") or fi.endswith(".gtif") or fi.endswith(".ecw") or fi.endswith(".bil") or fi.endswith(".til") or fi.endswith(".jpeg")) wrkbk = Workbook() wksht = wrkbk.add_sheet('rasters') wksht.row(0).write(0,'ruta') for row, filepath in enumerate(file_list, start=1): #Llenar lista de archivos y ruta wksht.row(row).write(0, filepath) wrkbk.save('imagen.xls') Is there a way I can manage this error? -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Mon Oct 25 17:38:12 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Mon, 25 Oct 2010 17:38:12 +0200 Subject: [Tutor] Need help with converting script using 2.6's urllib2 to Python 3.1 In-Reply-To: References: Message-ID: On 25 October 2010 14:46, Richard D. Moores wrote: > I'd like to convert the script to 3.1, but I can't understand the docs > for the 3.1 urllib module. Please someone tell me what to do. > (Converting ? 'print rate' ? to ? 'print(rate)' ? I understand.) Have you actually tried reading the documentation? The _very_ first section of the urllib documentation we have urllib.request.urlopen [1]. Which looks to me what you are looking for as a replacement to urllib2.urlopen(). Some *untested* inline comments below. > import urllib2 from urllib import request > a = urllib2.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN').read(20500) a = request.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN').read(20500) > b = a[19000:20500] > idx_pricewrap = b.find('pricewrap') > context = b[idx_pricewrap:idx_pricewrap+80] > idx_bgLast = context.find('bgLast') > rate = context[idx_bgLast+8:idx_bgLast+15] > print rate print(rate) Also http://docs.python.org/library/2to3.html discusses the automated tool for converting python code. Greets Sander [1] http://docs.python.org/py3k/library/urllib.request.html#urllib.request.urlopen From kty1104 at gmail.com Mon Oct 25 17:54:20 2010 From: kty1104 at gmail.com (=?UTF-8?B?6rmA7YOc7Jyk?=) Date: Tue, 26 Oct 2010 00:54:20 +0900 Subject: [Tutor] connect through the ip sharers Message-ID: hello there are two computer. one is in here, and another is in other country. both of two computer connected to internet by IP sharer. I want to make program that connect both computer and send and receive data. but my trying fails again and agian it works when I try to connect each program in my computer but the other computer case, it fails your little aid would be very helpful for me thanks in advanced I've been sent similar question before but I still have no idea how to fix it so I submit similar question again sorry. -------------- next part -------------- An HTML attachment was scrubbed... URL: From bermanrl at cfl.rr.com Mon Oct 25 18:00:47 2010 From: bermanrl at cfl.rr.com (Robert Berman) Date: Mon, 25 Oct 2010 12:00:47 -0400 Subject: [Tutor] connect through the ip sharers In-Reply-To: References: Message-ID: <014401cb745d$c9cce4e0$5d66aea0$@rr.com> Hi, Since your post appears to be discussing a networking problem rather than a problem with the Python language you might have far better results posting to a group more attuned to network problems. Good luck, Robert From: tutor-bounces+bermanrl=cfl.rr.com at python.org [mailto:tutor-bounces+bermanrl=cfl.rr.com at python.org] On Behalf Of ??? Sent: Monday, October 25, 2010 11:54 AM To: tutor at python.org Subject: [Tutor] connect through the ip sharers hello there are two computer. one is in here, and another is in other country. both of two computer connected to internet by IP sharer. I want to make program that connect both computer and send and receive data. but my trying fails again and agian it works when I try to connect each program in my computer but the other computer case, it fails your little aid would be very helpful for me thanks in advanced I've been sent similar question before but I still have no idea how to fix it so I submit similar question again sorry. _____ I am using the Free version of SPAMfighter . SPAMfighter has removed 16 of my spam emails to date. Do you have a slow PC? Try free scan! -------------- next part -------------- An HTML attachment was scrubbed... URL: From rdmoores at gmail.com Mon Oct 25 18:19:38 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Mon, 25 Oct 2010 09:19:38 -0700 Subject: [Tutor] Need help with converting script using 2.6's urllib2 to Python 3.1 In-Reply-To: References: Message-ID: On Mon, Oct 25, 2010 at 08:38, Sander Sweers wrote: > Have you actually tried reading the documentation? Of course. Though I can see why you wondered. >The _very_ first > section of the urllib documentation we have urllib.request.urlopen > [1]. Which looks to me what you are looking for as a replacement to > urllib2.urlopen(). What I tried were these 3 lines: import urllib a = urllib.request.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN').read(20500) print(a[123:140]) which got me Traceback (most recent call last): File "c:\P31Working\test_urllib.py", line 2, in a = urllib.request.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN').read(20500) AttributeError: 'module' object has no attribute 'request' Process terminated with an exit code of 1 Doing it your way, from urllib import request a = request.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN').read(20500) print(a[123:140]) succeeds. Why? Anyway, thanks. Dick From rdmoores at gmail.com Mon Oct 25 19:33:11 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Mon, 25 Oct 2010 10:33:11 -0700 Subject: [Tutor] Need help with converting script using 2.6's urllib2 to Python 3.1 In-Reply-To: References: Message-ID: And trying 2to3.py, which I've used successfully before, gets C:\P26Working\Finished\For2to3>2to3 -w -f urllib dollar2yen_rate_simple.py Traceback (most recent call last): File "C:\P26Working\Finished\For2to3\2to3.py", line 2, in from lib2to3.main import main File "C:\P26Working\Finished\For2to3\lib2to3\main.py", line 34 except os.error, err: ^ SyntaxError: invalid syntax dollar2yen_rate_simple.py can be seen at Also, Running this code in Python 3.1: from urllib import request a = request.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN').read(20500) b = a[19000:20500] idx_pricewrap = b.find('pricewrap') context = b[idx_pricewrap:idx_pricewrap+80] idx_bgLast = context.find('bgLast') rate = context[idx_bgLast+8:idx_bgLast+15] print(rate) gets Traceback (most recent call last): File "c:\P31Working\test_urllib.py", line 4, in idx_pricewrap = b.find('pricewrap') TypeError: expected an object with the buffer interface Process terminated with an exit code of 1 I have NO idea what that error means. Dick From sander.sweers at gmail.com Mon Oct 25 20:10:14 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Mon, 25 Oct 2010 20:10:14 +0200 Subject: [Tutor] Need help with converting script using 2.6's urllib2 to Python 3.1 In-Reply-To: References: Message-ID: On 25 October 2010 18:19, Richard D. Moores wrote: > Doing it your way, > > from urllib import request > a = request.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN').read(20500) > print(a[123:140]) > > succeeds. Why? Not sure how this exactly works but this is what I know. The difference is that urllib is now a directory with files in it while urllib2 was a single file. When you import on a a single file module normally all functions and classes are available to you. Now that this is a directory some magic (or not?) has to happen in __init__.py (read I also don't know). But I am sure someone here will be able to explain this properly :-). Greets Sander From rdmoores at gmail.com Mon Oct 25 23:11:15 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Mon, 25 Oct 2010 14:11:15 -0700 Subject: [Tutor] Need help with converting script using 2.6's urllib2 to Python 3.1 In-Reply-To: References: Message-ID: So I finally find a relevant example in the docs: The first example gave me some understanding and led me to revising my code to import urllib.request f = urllib.request.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN') a = f.read(20500).decode('utf-8') b = a[19000:20500] idx_pricewrap = b.find('pricewrap') context = b[idx_pricewrap:idx_pricewrap+80] idx_bgLast = context.find('bgLast') rate = context[idx_bgLast+8:idx_bgLast+15] print(rate) which works like a charm. Dick From scarolan at gmail.com Tue Oct 26 00:01:34 2010 From: scarolan at gmail.com (Sean Carolan) Date: Mon, 25 Oct 2010 17:01:34 -0500 Subject: [Tutor] How to pass a python variable to subprocess.call? Message-ID: I'm rewriting a bunch of my bash scripts to get some python practice. There are some instances where python doesn't have the built-in text processing that I need, so I'm using subprocess.call. How can I pass a python variable to these subprocesses? For example: mydir = "/usr/local/bin" subprocess.call("ls -l /usr/local/bin", shell=True) How do I replace /usr/local/bin in the subprocess call with the mydir variable? From abhijeet.1989 at gmail.com Tue Oct 26 00:19:53 2010 From: abhijeet.1989 at gmail.com (Abhijeet Rastogi) Date: Tue, 26 Oct 2010 03:49:53 +0530 Subject: [Tutor] How to pass a python variable to subprocess.call? In-Reply-To: References: Message-ID: On Tue, Oct 26, 2010 at 3:31 AM, Sean Carolan wrote: > I'm rewriting a bunch of my bash scripts to get some python practice. > There are some instances where python doesn't have the built-in text > processing that I need, so I'm using subprocess.call. How can I pass > a python variable to these subprocesses? For example: > > mydir = "/usr/local/bin" > subprocess.call("ls -l /usr/local/bin", shell=True) > > subprocess.call("ls -l "+mydir,shell=True) will do the job. In python, we can simply concatenate the strings like that. How do I replace /usr/local/bin in the subprocess call with the mydir > variable? > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Abhijeet Rastogi (shadyabhi) http://www.google.com/profiles/abhijeet.1989 -------------- next part -------------- An HTML attachment was scrubbed... URL: From bill at celestial.net Tue Oct 26 00:08:32 2010 From: bill at celestial.net (Bill Campbell) Date: Mon, 25 Oct 2010 15:08:32 -0700 Subject: [Tutor] How to pass a python variable to subprocess.call? In-Reply-To: References: Message-ID: <20101025220832.GA25941@ayn.mi.celestial.com> On Mon, Oct 25, 2010, Sean Carolan wrote: >I'm rewriting a bunch of my bash scripts to get some python practice. >There are some instances where python doesn't have the built-in text >processing that I need, so I'm using subprocess.call. How can I pass >a python variable to these subprocesses? For example: > >mydir = "/usr/local/bin" >subprocess.call("ls -l /usr/local/bin", shell=True) > >How do I replace /usr/local/bin in the subprocess call with the mydir variable? subprocess.call("ls -l '%s'" % mydir, shell=True) Bill -- INTERNET: bill at celestial.com Bill Campbell; Celestial Software LLC URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way Voice: (206) 236-1676 Mercer Island, WA 98040-0820 Fax: (206) 232-9186 Skype: jwccsllc (206) 855-5792 Whatever the State saith is a lie; whatever it hath is a theft. -- Nietzsche From steve at alchemy.com Tue Oct 26 00:33:52 2010 From: steve at alchemy.com (Steve Willoughby) Date: Mon, 25 Oct 2010 15:33:52 -0700 Subject: [Tutor] How to pass a python variable to subprocess.call? In-Reply-To: References: Message-ID: <4CC605D0.50309@alchemy.com> On 25-Oct-10 15:19, Abhijeet Rastogi wrote: > subprocess.call("ls -l "+mydir,shell=True) > will do the job. In python, we can simply concatenate the strings like that. > > How do I replace /usr/local/bin in the subprocess call with the > mydir variable? It's often preferable (for reasons ranging from security to coding style) to use the array call style for the subprocess methods, so instead of a string which must then be interpreted by the shell (and allowing for unintended interpretation of shell characters like quotes and wildcards), you instead pass a list of values for the new process, so the shell is never even involved: subprocess.call(['ls', '-l', mydir]) From bermanrl at cfl.rr.com Mon Oct 25 23:26:54 2010 From: bermanrl at cfl.rr.com (Robert Berman) Date: Mon, 25 Oct 2010 17:26:54 -0400 Subject: [Tutor] connect through the ip sharers In-Reply-To: References: <014401cb745d$c9cce4e0$5d66aea0$@rr.com> Message-ID: <01a301cb748b$58ac15a0$0a0440e0$@rr.com> Hi, As you well know networking opens a Pandora?s box of information so I tried using Google looking for ?computer connectivity? which has a wide range of topics. This may be a very good starting point you might wish to follow. The alternative is again using Google to look for ?networking news groups?. Also, when replying to information received by member(s) of a news group it is best to include the newsgroup when replying. Usually, hitting ?reply to all? will do the trick. Hope this helps, Robert From: ??? [mailto:kty1104 at gmail.com] Sent: Monday, October 25, 2010 12:07 PM To: Robert Berman Subject: Re: [Tutor] connect through the ip sharers could you tell me what group or mailing list is more attuned to network problems? 2010/10/26 Robert Berman Hi, Since your post appears to be discussing a networking problem rather than a problem with the Python language you might have far better results posting to a group more attuned to network problems. Good luck, Robert From: tutor-bounces+bermanrl=cfl.rr.com at python.org [mailto:tutor-bounces+bermanrl =cfl.rr.com at python.org] On Behalf Of ??? Sent: Monday, October 25, 2010 11:54 AM To: tutor at python.org Subject: [Tutor] connect through the ip sharers hello there are two computer. one is in here, and another is in other country. both of two computer connected to internet by IP sharer. I want to make program that connect both computer and send and receive data. but my trying fails again and agian it works when I try to connect each program in my computer but the other computer case, it fails your little aid would be very helpful for me thanks in advanced I've been sent similar question before but I still have no idea how to fix it so I submit similar question again sorry. _____ I am using the Free version of SPAMfighter . SPAMfighter has removed 16 of my spam emails to date. Do you have a slow PC? Try free scan! _____ I am using the Free version of SPAMfighter . SPAMfighter has removed 16 of my spam emails to date. Do you have a slow PC? Try free scan! -------------- next part -------------- An HTML attachment was scrubbed... URL: From mehgcap at gmail.com Tue Oct 26 04:46:52 2010 From: mehgcap at gmail.com (Alex Hall) Date: Mon, 25 Oct 2010 22:46:52 -0400 Subject: [Tutor] decorators (the "at" sign)? Message-ID: Hi all, Now that I am able to run the source code of an open source application I hope to one day help develop, I am trying to understand how it works. One thing I keep seeing is an at sign followed by a word, usually (maybe always) immediately preceeding a function definition. For example, and I know this exact code will not make much sense, but it gives the idea: class Bing(Messages, Updating, Dismissable): @set_index def get_url(self, index=None): return self.storage[index]['Url'] What is the "@set_index" for? Specifically, what is the at sign doing? Google was only able to provide me with a very vague idea of what is going on, though it seems to crop up a lot in classmethod and staticmethod calls (not sure about those either). I read PEP 318, but it was not much help since I am coming at this having no idea what I am looking at. The PEP did explain why I have never run into this before, though - it is apparently specific to Python. I see this sort of thing all over this source code so it seems like a good idea to get exactly what it is for. TIA! -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From prologic at shortcircuit.net.au Tue Oct 26 05:08:32 2010 From: prologic at shortcircuit.net.au (James Mills) Date: Tue, 26 Oct 2010 13:08:32 +1000 Subject: [Tutor] decorators (the "at" sign)? In-Reply-To: References: Message-ID: On Tue, Oct 26, 2010 at 12:46 PM, Alex Hall wrote: > ?@set_index > ?def get_url(self, index=None): > ?return self.storage[index]['Url'] Decorators in python are used (almost as convenience) to wrap functions/methods for various purposes. It might be to do some logging before and after the actual function is called, or setup static methods, etc. The "@" symbol was chosen for this, and in the above example the following is happening: The method get_url(...) is being decorated (as opposed to being wrapped) with the set_index(...) function. (Without knowing too much more about the code you're looking at...) set_index(...) would be doing something with the get_url(...) method then returning it. The best example of this is the following from PEP 318 (1): def onexit(f): import atexit atexit.register(f) return f @onexit def func(): ... This has a similar form to the code you're studying and registers func with atexit hooks so that when the application terminates, func(...) gets called. Hope this helps, cheers James 1. http://www.python.org/dev/peps/pep-0318/ -- -- James Mills -- -- "Problems are solved by method" From lie.1296 at gmail.com Tue Oct 26 14:30:32 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 26 Oct 2010 23:30:32 +1100 Subject: [Tutor] decorators (the "at" sign)? In-Reply-To: References: Message-ID: On 10/26/10 13:46, Alex Hall wrote: > Hi all, > Now that I am able to run the source code of an open source > application I hope to one day help develop, I am trying to understand > how it works. One thing I keep seeing is an at sign followed by a > word, usually (maybe always) immediately preceeding a function > definition. For example, and I know this exact code will not make much > sense, but it gives the idea: > class Bing(Messages, Updating, Dismissable): > > @set_index > def get_url(self, index=None): > return self.storage[index]['Url'] > > What is the "@set_index" for? Specifically, what is the at sign doing? > Google was only able to provide me with a very vague idea of what is > going on, though it seems to crop up a lot in classmethod and > staticmethod calls (not sure about those either). I read PEP 318, but > it was not much help since I am coming at this having no idea what I > am looking at. The PEP did explain why I have never run into this > before, though - it is apparently specific to Python. I see this sort > of thing all over this source code so it seems like a good idea to get > exactly what it is for. TIA! The decorator syntax is really just a shorthand, from this: @decorator def func(arg): pass is equivalent to: def func(arg): pass func = decorator(func) basically, a decorator is a function that takes an function as a parameter/callable and (usually) returns another function/callable as return value. A slightly advanced usage of decorator: def trace(func): """ trace will print the func's name, the number of times func have been called, the arguments it's called with, and the return value of func whenever func is called. """ # define an inner function def _decorator(arg): print ("The %sth call of %s with arg: %s" % (_decorator._numcall, func.__name__, arg)) _decorator._numcall += 1 ret = func(arg) print "finished", func.__name__, "returns:", ret return ret # this is used to store the number of times # the decorated function is called _decorator._numcall = 0 # disable the decorator when debugging is enabled if __debug__: # or: return _decorator if __debug__ else func return _decorator else: return func @trace def twice(arg): return 2 * arg # the @trace makes it as if you do this: # twice = trace(twice) $ # let's start the program $ python decor.py The 0th call of twice() with arg: 30 finished twice() returns: 60 The 1th call of twice() with arg: 3 finished twice() returns: 6 The 2th call of twice() with arg: 4 finished twice() returns: 8 74 $ $ # now call it with debugging disabled: $ python -O decor.py 74 another nifty use of decorator is for event handling for a hypothetical GUI toolkit (I've yet to actually see a toolkit that uses this syntax yet): @button.on_click def shoot(button): ... @window.on_keypress('p') def pause(key): ... other built-in functions designed for decorator syntax is @property, @classmethod, and @instancemethod. Decorator can become extremely powerful when combined with the descriptor protocol (.__get__, .__set__). From scarolan at gmail.com Tue Oct 26 15:22:33 2010 From: scarolan at gmail.com (Sean Carolan) Date: Tue, 26 Oct 2010 08:22:33 -0500 Subject: [Tutor] subprocess Popen PIPE error Message-ID: What am I doing wrong here? >>> from subprocess import Popen, PIPE >>> cmd = 'ls -l' >>> p = Popen(cmd, stdout=PIPE, stderr=PIPE) Traceback (most recent call last): File "", line 1, in ? File "/usr/lib64/python2.4/subprocess.py", line 550, in __init__ errread, errwrite) File "/usr/lib64/python2.4/subprocess.py", line 993, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory From abhijeet.1989 at gmail.com Tue Oct 26 15:44:33 2010 From: abhijeet.1989 at gmail.com (Abhijeet Rastogi) Date: Tue, 26 Oct 2010 19:14:33 +0530 Subject: [Tutor] subprocess Popen PIPE error In-Reply-To: References: Message-ID: Please read the documentation of Popen. You cannot pass arguments like that. >>> from subprocess import Popen,PIPE >>> import shlex >>> cmd="ls -l" >>> cmd=shlex.split(cmd) >>> p = Popen(cmd,stdout=PIPE,stderr=PIPE) or simply that means >>> p = Popen(['ls','-l'],stdout=PIPE,stderr=PIPE) Hope I have made my point clear. The problem occuring is that there is no binary that has name as "ls -l". Python cannot understand that "-l" is an argument until you pass it this way. On Tue, Oct 26, 2010 at 6:52 PM, Sean Carolan wrote: > What am I doing wrong here? > > >>> from subprocess import Popen, PIPE > >>> cmd = 'ls -l' > >>> p = Popen(cmd, stdout=PIPE, stderr=PIPE) > Traceback (most recent call last): > File "", line 1, in ? > File "/usr/lib64/python2.4/subprocess.py", line 550, in __init__ > errread, errwrite) > File "/usr/lib64/python2.4/subprocess.py", line 993, in _execute_child > raise child_exception > OSError: [Errno 2] No such file or directory > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Abhijeet Rastogi (shadyabhi) http://www.google.com/profiles/abhijeet.1989 -------------- next part -------------- An HTML attachment was scrubbed... URL: From abhijeet.1989 at gmail.com Tue Oct 26 18:39:27 2010 From: abhijeet.1989 at gmail.com (Abhijeet Rastogi) Date: Tue, 26 Oct 2010 22:09:27 +0530 Subject: [Tutor] subprocess Popen PIPE error In-Reply-To: References: Message-ID: Ya. Do it using python. Why do you want to use bash when you already have python? See, do something like this:- import os alldirs = os.listdir("/path/to/dir") DIRS = [] #Only the dirs you are interested in for i in alldirs: if i.find("deploy") is -1: L.append(i) if i.find("TEMPLATE") is -1: L.append(i) #Now L contains all the required dirs. Hope it helps. On Tue, Oct 26, 2010 at 9:19 PM, Sean Carolan wrote: > > Here is the bash one-liner that generates my list, > > with one name per line: > > > > ls -d */ | grep -v -E 'deploy|TEMPLATE' | sed 's/\///' > > > > How would you get the output of this into a python list that could > > then be used in the script? Please forgive my ignorance; I've read > > through the documentation but am still not clear on this aspect. > > Would this be easier to accomplish using os.listdir()?. Basically I > want to make a list of directories, excluding files and the > directories or files containing "deploy" and "TEMPLATE". > -- Abhijeet Rastogi (shadyabhi) http://www.google.com/profiles/abhijeet.1989 -------------- next part -------------- An HTML attachment was scrubbed... URL: From royhink at gmail.com Tue Oct 26 21:55:30 2010 From: royhink at gmail.com (Roy Hinkelman) Date: Tue, 26 Oct 2010 12:55:30 -0700 Subject: [Tutor] Problem Passing VARs to Python from PHP & capturing return string Message-ID: I am posting here as well as a PHP list since I am now getting an odd python error. Rance: Thanks for the note. I get the same error with system or exec or passthru Now, this is very strange. I made the command line string more explicit, and now it recognizes the .py script but returns a python error: Content: Data: ImportError: No module named mechanize Command: C:\WINDOWS\system32\cmd.exe C:\Program%20Files\Python26\python.exe /c D:\Inetpub\AtoZ\hometown\include\weatherFeed.py -c Tampa -s FL My python script will run correctly in IDE shell and from command line. I used easy_install to install mechanize. It seems to be recognizing my other modules (BeautifulSoup, re, urllib2, sys). So, it seems that the script is being activated by the exec command since I am now getting a python error. But, why would I get a python error when it will run from command line? Here is my PHP: [code] OPTION 1

Python Weather Feed for ' . $city . ', ' . $state . '

'; ob_start(); $command = "C:\WINDOWS\system32\cmd.exe C:\Program%20Files\Python26\python.exe /c D:\Inetpub\AtoZ\hometown\include\weatherFeed.py -c " . $city . " -s " . $state; $data = exec($command . " 2>&1", $result); $content=ob_get_contents(); ob_end_clean(); echo 'Content: ' . $content . '
'; echo 'Result: ' . $result . '
'; echo 'Data: ' . $data . '
'; echo 'Command: ' . $command . '
'; include('include\weatherFeed_TEMP.txt') ?> [/code] and I've modified my python, making it a function [code] #!C:\Program Files\Python26\python.exe -u # Current weather feed # # Weather page sources: http://www.weather.gov/ # Capture relevant data, strip unusable stuff out, fix URLs, display in our HTML page # import program modules from BeautifulSoup import BeautifulSoup as B_S import re, urllib2, sys, mechanize # 'standard' set ### Return small weather table def weatherSmall(c,s): cityState = c + ', ' + s query = 'Chicago, IL' #Test!!! _URL = "http://www.weather.gov/" br=mechanize.Browser() br.open( _URL ) br.select_form( nr=1 ) #assuming form is 2nd form on page br['inputstring'] = query html = br.submit() _soup = B_S(html) # finding the correct table _step1 = _soup.findAll('table', limit=7)[6] col = _step1.findAll('td') # sys.argv is included below as a test. _thumb = '' + str(col[0]) + str(col[1]) + '
Forecast for ' + query + '
' + str(sys.argv) + '
' _thumb = _thumb.replace( '11%','50%' ) _thumb = _thumb.replace( '/images/', 'images/weather/' ) #write to txt file TEST _temp = 'D:\\Inetpub\\AtoZ\\hometown\\include\\weatherFeed_TEMP.txt' temp = open( _temp, 'w' ) temp.write( _thumb ) temp.close() return _thumb city = 'Establish' state = 'Variables' count = 0 for ea in sys.argv: if ea == '-c': city = sys.argv[count+1] elif ea == '-s': state = sys.argv[count+1] count+=1 _result = str(weatherSmall(city,state)) #print _result [/code] Roy Hinkelman Technical Services Website Development & Management 707-774-7411 roy at worldtradepress.com ________________________________ -----Original Message----- From: Rance Hall [mailto:ranceh at gmail.com] Sent: Saturday, October 23, 2010 10:25 AM To: roy at worldtradepress.com Subject: Re: [Tutor] Problem Passing VARs to Python from PHP & capturing return string On Fri, Oct 22, 2010 at 1:28 PM, Roy Hinkelman < rhinkelman at worldtradepress.com> wrote: > My script doesn't want to recognize the variables from the exec() > command in PHP. Plus, it won't capture the results of the script. > > This Python script works in IDLE, and I've got some testing code in there. > > One Known Unsolved Issue: > I put Python in C:\Program Files\Python26\python.exe and have tried > $command = "C:\Program Files\Python26\python.exe > include/weatherFeed.py -c $city -s $state"; to no avail. > > I added .py to Windows IIS Web Service Extensions and to the > Application Configuration. > > Any help appreciated. > Ok, so you wrote a Python script to create a text file, and you want PHP to include that text file in a web page. I'm assuming that your Python is working on its own. I also see you are running this from a windows server. According to the PHP site the exec function takes option arguments that you are not using: string exec ( string $command [, array &$output [, int &$return_var ]] ) Might I suggest that you switch from exec to system() documented here: http://www.php.net/manual/en/function.system.php string system ( string $command [, int &$return_var ] ) Since it doesn't appear that your Python script has any output except for the text file, it might be better to call it from system() because system() does not care about the output of the called program. May I also suggest that you actually track the return_var somewhere. You can check the status of the return_var to ensure that the system() call competed successfully or not before including the text file, on the off chance that it isn't there. Someone else has already suggested to you that this is a Python list and you should probably take this to the PHP list. I agree, but the teacher in me just couldn't leave the answer as "take it to another forum" without some guidance. Good luck. Rance -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Tue Oct 26 22:15:04 2010 From: emile at fenx.com (Emile van Sebille) Date: Tue, 26 Oct 2010 13:15:04 -0700 Subject: [Tutor] Problem Passing VARs to Python from PHP & capturing return string In-Reply-To: References: Message-ID: On 10/26/2010 12:55 PM Roy Hinkelman said... > I am posting here as well as a PHP list since I am now getting an odd python > error. The traceback of the python error would help us diagnose the problem but it's not been included. Can you paste in the actual traceback? Emile From mass02gh at yahoo.ca Tue Oct 26 22:20:21 2010 From: mass02gh at yahoo.ca (masawudu bature) Date: Tue, 26 Oct 2010 13:20:21 -0700 (PDT) Subject: [Tutor] Summing up numbers in a file Message-ID: <203712.43705.qm@web58506.mail.re3.yahoo.com> How do I sum all occurrences of? a number in a file? I have a file with a bunch of numbers, each on it's own line: 5 3 11 3 7 3 5 5 11 7 7 ... How do i sum them up so that the output will be , 5 :? 15 3 :?? 9 11:? 22 7 :?? 21 -------------- next part -------------- An HTML attachment was scrubbed... URL: From adam.jtm30 at gmail.com Tue Oct 26 22:34:06 2010 From: adam.jtm30 at gmail.com (Adam Bark) Date: Tue, 26 Oct 2010 21:34:06 +0100 Subject: [Tutor] Summing up numbers in a file In-Reply-To: <203712.43705.qm@web58506.mail.re3.yahoo.com> References: <203712.43705.qm@web58506.mail.re3.yahoo.com> Message-ID: <4CC73B3E.9040506@gmail.com> On 26/10/10 21:20, masawudu bature wrote: > How do I sum all occurrences of a number in a file? > I have a file with a bunch of numbers, each on it's own line: > 5 > 3 > 11 > 3 > 7 > 3 > 5 > 5 > 11 > 7 > 7 > ... > How do i sum them up so that the output will be , > 5 : 15 > 3 : 9 > 11: 22 > 7 : 21 > Assuming you know how to iterate through a file, you could try saving the number of occurrences of each number in the file into a dictionary. Something like: if num in my_dict: my_dict[num] += 1 else: my_dict[num] = 1 HTH. Adam. -------------- next part -------------- An HTML attachment was scrubbed... URL: From bermanrl at cfl.rr.com Tue Oct 26 22:35:09 2010 From: bermanrl at cfl.rr.com (Robert Berman) Date: Tue, 26 Oct 2010 16:35:09 -0400 Subject: [Tutor] Summing up numbers in a file In-Reply-To: <203712.43705.qm@web58506.mail.re3.yahoo.com> References: <203712.43705.qm@web58506.mail.re3.yahoo.com> Message-ID: <03f701cb754d$48d60bf0$da8223d0$@rr.com> This certainly looks like a classroom assignment. Please make an attempt to solve the problem on your own. If the program fails, please copy the code you have written and the error messages from the traceback and someone here will be more than happy to help you work your way through the problem. Good luck, Robert From: tutor-bounces+bermanrl=cfl.rr.com at python.org [mailto:tutor-bounces+bermanrl=cfl.rr.com at python.org] On Behalf Of masawudu bature Sent: Tuesday, October 26, 2010 4:20 PM To: tutor at python.org Subject: [Tutor] Summing up numbers in a file How do I sum all occurrences of a number in a file? I have a file with a bunch of numbers, each on it's own line: 5 3 11 3 7 3 5 5 11 7 7 ... How do i sum them up so that the output will be , 5 : 15 3 : 9 11: 22 7 : 21 _____ I am using the Free version of SPAMfighter . SPAMfighter has removed 16 of my spam emails to date. Do you have a slow PC? Try free scan! -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Tue Oct 26 22:38:51 2010 From: emile at fenx.com (Emile van Sebille) Date: Tue, 26 Oct 2010 13:38:51 -0700 Subject: [Tutor] Summing up numbers in a file In-Reply-To: <203712.43705.qm@web58506.mail.re3.yahoo.com> References: <203712.43705.qm@web58506.mail.re3.yahoo.com> Message-ID: On 10/26/2010 1:20 PM masawudu bature said... > How do I sum all occurrences of a number in a file? > I have a file with a bunch of numbers, each on it's own line: > > 5 > 3 > 11 > 3 > 7 > 3 > 5 > 5 > 11 > 7 > 7 > ... > How do i sum them up so that the output will be , > 5 : 15 > 3 : 9 > 11: 22 > 7 : 21 > Well, you'll need to read the lines of the file in and group them together. Then, for each group, print both the number and the result when multiplied by the count. To do this, you'll need the name of the file and use open to read it in. I'd suggest using a dict to accumulate the groups, and a loop to print the results. What have you got so far? Emile From royhink at gmail.com Tue Oct 26 23:11:58 2010 From: royhink at gmail.com (Roy Hinkelman) Date: Tue, 26 Oct 2010 14:11:58 -0700 Subject: [Tutor] Passing VARs to Python from PHP & capturing return string Message-ID: I got it working. As several people mentioned, it was a permissions issue. When I tried the command line, I had admin access, but from the web page, I had only 'Users'. I had to manually change permissions for both the Python module 'mechanize' as well as the txt file the script was writing to. Now, exec() and passthru() are both working, as in, passing the argument to python and executing the script. Output Buffer (ob), however, is not. I still need to write to a temp file. Thanks for everyone's help. Roy -------------- next part -------------- An HTML attachment was scrubbed... URL: From rhinkelman at worldtradepress.com Tue Oct 26 20:54:04 2010 From: rhinkelman at worldtradepress.com (Roy Hinkelman) Date: Tue, 26 Oct 2010 11:54:04 -0700 Subject: [Tutor] Problem Passing VARs to Python from PHP & capturing return string In-Reply-To: References: <000f01cb7216$ef863c70$ce92b550$@com> Message-ID: <000f01cb753f$297e3ec0$7c7abc40$@com> I am posting here as well as a PHP list since I am now getting an odd python error. Rance: Thanks for the note. I get the same error with system or exec or passthru Now, this is very strange. I made the command line string more explicit, and now it recognizes the .py script but returns a python error: Content: Data: ImportError: No module named mechanize Command: C:\WINDOWS\system32\cmd.exe C:\Program%20Files\Python26\python.exe /c D:\Inetpub\AtoZ\hometown\include\weatherFeed.py -c Tampa -s FL My python script will run correctly in IDE shell and from command line. I used easy_install to install mechanize. It seems to be recognizing my other modules (BeautifulSoup, re, urllib2, sys). So, it seems that the script is being activated by the exec command since I am now getting a python error. But, why would I get a python error when it will run from command line? Here is my PHP: [code] OPTION 1

Python Weather Feed for ' . $city . ', ' . $state . '

'; ob_start(); $command = "C:\WINDOWS\system32\cmd.exe C:\Program%20Files\Python26\python.exe /c D:\Inetpub\AtoZ\hometown\include\weatherFeed.py -c " . $city . " -s " . $state; $data = exec($command . " 2>&1", $result); $content=ob_get_contents(); ob_end_clean(); echo 'Content: ' . $content . '
'; echo 'Result: ' . $result . '
'; echo 'Data: ' . $data . '
'; echo 'Command: ' . $command . '
'; include('include\weatherFeed_TEMP.txt') ?> [/code] and I've modified my python, making it a function [code] #!C:\Program Files\Python26\python.exe -u # Current weather feed # # Weather page sources: http://www.weather.gov/ # Capture relevant data, strip unusable stuff out, fix URLs, display in our HTML page # import program modules from BeautifulSoup import BeautifulSoup as B_S import re, urllib2, sys, mechanize # 'standard' set ### Return small weather table def weatherSmall(c,s): cityState = c + ', ' + s query = 'Chicago, IL' #Test!!! _URL = "http://www.weather.gov/" br=mechanize.Browser() br.open( _URL ) br.select_form( nr=1 ) #assuming form is 2nd form on page br['inputstring'] = query html = br.submit() _soup = B_S(html) # finding the correct table _step1 = _soup.findAll('table', limit=7)[6] col = _step1.findAll('td') # sys.argv is included below as a test. _thumb = '' + str(col[0]) + str(col[1]) + '
Forecast for ' + query + '
' + str(sys.argv) + '
' _thumb = _thumb.replace( '11%','50%' ) _thumb = _thumb.replace( '/images/', 'images/weather/' ) #write to txt file TEST _temp = 'D:\\Inetpub\\AtoZ\\hometown\\include\\weatherFeed_TEMP.txt' temp = open( _temp, 'w' ) temp.write( _thumb ) temp.close() return _thumb city = 'Establish' state = 'Variables' count = 0 for ea in sys.argv: if ea == '-c': city = sys.argv[count+1] elif ea == '-s': state = sys.argv[count+1] count+=1 _result = str(weatherSmall(city,state)) #print _result [/code] Roy Hinkelman Technical Services Website Development & Management 707-774-7411 roy at worldtradepress.com ________________________________ www.WorldTradePress.com (main website) www.StockMapAgency.com (3700+ Antique & Modern Maps) www.BestCountryReports.com (country reports for 175 countries) www.GiantMapArt.com (giant wall maps) www.WorldTradeRef.com (trade and logistics) www.GlobalRoadWarrior.com (175-country database) www.AtoZMapsOnline.com (worlds largest map database) www.AtoZtheUSA.com (extensive state facts database) -----Original Message----- From: Rance Hall [mailto:ranceh at gmail.com] Sent: Saturday, October 23, 2010 10:25 AM To: roy at worldtradepress.com Subject: Re: [Tutor] Problem Passing VARs to Python from PHP & capturing return string On Fri, Oct 22, 2010 at 1:28 PM, Roy Hinkelman wrote: > My script doesn't want to recognize the variables from the exec() command in > PHP. Plus, it won't capture the results of the script. > > This Python script works in IDLE, and I've got some testing code in there. > > One Known Unsolved Issue: > I put Python in C:\Program Files\Python26\python.exe and have tried > $command = "C:\Program Files\Python26\python.exe include/weatherFeed.py -c > $city -s $state"; > to no avail. > > I added .py to Windows IIS Web Service Extensions and to the Application > Configuration. > > Any help appreciated. > Ok, so you wrote a Python script to create a text file, and you want PHP to include that text file in a web page. I'm assuming that your Python is working on its own. I also see you are running this from a windows server. According to the PHP site the exec function takes option arguments that you are not using: string exec ( string $command [, array &$output [, int &$return_var ]] ) Might I suggest that you switch from exec to system() documented here: http://www.php.net/manual/en/function.system.php string system ( string $command [, int &$return_var ] ) Since it doesn't appear that your Python script has any output except for the text file, it might be better to call it from system() because system() does not care about the output of the called program. May I also suggest that you actually track the return_var somewhere. You can check the status of the return_var to ensure that the system() call competed successfully or not before including the text file, on the off chance that it isn't there. Someone else has already suggested to you that this is a Python list and you should probably take this to the PHP list. I agree, but the teacher in me just couldn't leave the answer as "take it to another forum" without some guidance. Good luck. Rance No virus found in this incoming message. Checked by AVG - www.avg.com Version: 9.0.862 / Virus Database: 271.1.1/3218 - Release Date: 10/24/10 23:34:00 From nicolaiheitz at gmx.de Tue Oct 26 20:59:40 2010 From: nicolaiheitz at gmx.de (Bolli) Date: Tue, 26 Oct 2010 11:59:40 -0700 (PDT) Subject: [Tutor] problems with numdifftools In-Reply-To: <201010232208.48660.steve@pearwood.info> References: <4CC23AD3.7020200@googlemail.com> <201010232208.48660.steve@pearwood.info> Message-ID: <30060567.post@talk.nabble.com> Hey, First of all thanks for your response. I am also new to this forum so I expected to receive an email if somebody replies to my question. Therefore I just read it today.... > I am not sure if you are the right persons to contact but if not I > would appreciate a short notice and maybe an address where I can find > help. I'd try the numpy mailing list: http://www.scipy.org/Mailing_Lists Alright. I will try that. > 5) In principal Python can divide those numbers (2.3e-28)/(5.6e-6)**3 I get half the result you suggest above: >>> (2.3e-28)/(5.6e-6)**3 1.3096756559766764e-12 which agrees with my HP-48GX, and the result after simplifying by hand to 2.3e-10/175.616. So I think that is the correct result, not 2.6e-12. You are totally right. I forgot to write down a factor of 2. I am so sorry about it!!!!!! > My source code is attached. Please keep in mind that I just started > programing and Python is my first Language. I tried to do my best to > comment every single step to make the code readable. Here it comes: Comments are good, but too many comments hurt readability. Don't feel that you need to comment every single line. Comments like: x = x+1 # increment x by 1 are just noise. But this is a good comment: x = x+1 # add correction for wind-chill factor. > self.m = 39.96*1.66*10**(-27) ? ? ? ? ? ? ? ? ? ?#m is the mass > of a single trapped ion in the chain I wonder why you calculate m with an explicit exponentiation operator, instead of a floating point exponent? The difference is very small, so it probably doesn't matter: >>> 39.96*1.66*10**(-27) 6.6333600000000006e-26 >>> 39.96*1.66e-27 6.6333599999999994e-26 Haha, you are right. Thats just because I am a physicist and used to write it down that way. I changed that! > for i in range(len(x)): > for j in range(len(x)): > if j >i: > Vx = Vx + C * (abs(x[i]-x[j]))**(-1) #then we > add the coulomb interaction You can simplify that nested loop and addition: for i in range(len(x)): for j in range(i+1, len(x)): Vx += C * (abs(x[i]-x[j]))**(-1) # add the coulomb interaction The function range takes up to three integer arguments: range(start, end, step) where start and step are optional, defaulting to 0 and 1 respectively. By starting the inner loop at i+1, you guarantee that j is always > i and therefore you can skip the test. The other change is that instead of Vx = Vx + something you can write: Vx += something Thanks. I got it. In the beginning it is just hard to see every simplification. But I understand what you were suggesting and I corrected it. Sometimes a program evolves also physically. So I added the while loop because I found out later that I need it. -- Steven D'Aprano _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- View this message in context: http://old.nabble.com/-Tutor--problems-with-numdifftools-tp30034615p30060567.html Sent from the Python - tutor mailing list archive at Nabble.com. From siren99 at yahoo.com Wed Oct 27 13:33:42 2010 From: siren99 at yahoo.com (Siren Saren) Date: Wed, 27 Oct 2010 04:33:42 -0700 (PDT) Subject: [Tutor] decorators (the "at" sign)? In-Reply-To: Message-ID: <650465.16412.qm@web44716.mail.sp1.yahoo.com> Alex, Many people have trouble wrapping their minds around decorators.? I couldn't find a decent explanation either until reading an allegedly 'advanced' python book (b/c 'advanced' is in the title, I guess). An easy explanation might be sufficient, if you don't intend to use them yourself.? A decorator is a way of changing a function's behavior.? Most of the time you see a decorator, the person using it could have merely rewritten the function or method itself and given it some additional capability or modified it in some way.? But, as you may have noticed, programmers prefer to 'abstract' when they can, so they can avoid 'duplicating code.'? (These are buzzwords you'll encounter a lot). Let's say your program needs to set an index in a lot of different functions, and let's further imagine that setting an index is either more than a line of code or that the index itself may change over the development cycle, or that the index will vary according to some simple pattern consistently defined in these other functions. To avoid writing the same code over and over and having the value of index set in many different places, the developers chose instead to write the code once and refer to it in all the other functions through a decorator, which takes all the functions, modifies them so they get the index values they need, and sets them back in their places (more or less). If you want the more complicated answer, I think I can take a reasonable shot at showing how this works too and making an example.? But you may just want a general description.? Also, I'm only about 4 months into learning to program so you may prefer a more expert opinion.? Cheers, Soren --- On Tue, 10/26/10, Alex Hall wrote: From: Alex Hall Subject: [Tutor] decorators (the "at" sign)? To: "tutor" Date: Tuesday, October 26, 2010, 2:46 AM Hi all, Now that I am able to run the source code of an open source application I hope to one day help develop, I am trying to understand how it works. One thing I keep seeing is an at sign followed by a word, usually (maybe always) immediately preceeding a function definition. For example, and I know this exact code will not make much sense, but it gives the idea: class Bing(Messages, Updating, Dismissable): @set_index def get_url(self, index=None): ? return self.storage[index]['Url'] What is the "@set_index" for? Specifically, what is the at sign doing? Google was only able to provide me with a very vague idea of what is going on, though it seems to crop up a lot in classmethod and staticmethod calls (not sure about those either). I read PEP 318, but it was not much help since I am coming at this having no idea what I am looking at. The PEP did explain why I have never run into this before, though - it is apparently specific to Python. I see this sort of thing all over this source code so it seems like a good idea to get exactly what it is for. TIA! -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Wed Oct 27 14:42:33 2010 From: davea at ieee.org (Dave Angel) Date: Wed, 27 Oct 2010 08:42:33 -0400 Subject: [Tutor] Problem Passing VARs to Python from PHP & capturing return string In-Reply-To: <000f01cb753f$297e3ec0$7c7abc40$@com> References: <000f01cb7216$ef863c70$ce92b550$@com> <000f01cb753f$297e3ec0$7c7abc40$@com> Message-ID: <4CC81E39.6090201@ieee.org> On 2:59 PM, Roy Hinkelman wrote: > I am posting here as well as a PHP list since I am now getting an odd python > error. > > Rance: Thanks for the note. I get the same error with system or exec or > passthru > > Now, this is very strange. > > I made the command line string more explicit, and now it recognizes the .py > script but returns a python error: > > Content: > Data: ImportError: No module named mechanize > Command: C:\WINDOWS\system32\cmd.exe C:\Program%20Files\Python26\python.exe > /c D:\Inetpub\AtoZ\hometown\include\weatherFeed.py -c Tampa -s FL > > My python script will run correctly in IDE shell and from command line. I > used easy_install to install mechanize. It seems to be recognizing my other > modules (BeautifulSoup, re, urllib2, sys). > > So, it seems that the script is being activated by the exec command since I > am now getting a python error. But, why would I get a python error when it > will run from command line? > > Here is my PHP: > [code] > error_reporting(E_ALL); > > $city = 'Tampa'; > $state = 'FL'; > echo '

OPTION 1

Python Weather Feed for ' . $city . ', > ' . $state .'

'; > > ob_start(); > $command = "C:\WINDOWS\system32\cmd.exe > C:\Program%20Files\Python26\python.exe /c > D:\Inetpub\AtoZ\hometown\include\weatherFeed.py -c " . $city . " -s " . > $state; > > $data = exec($command . " 2>&1", $result); > $content=ob_get_contents(); > ob_end_clean(); > > echo 'Content: ' . $content . '
'; > echo 'Result: ' . $result . '
'; > echo 'Data: ' . $data . '
'; > echo 'Command: ' . $command . '
'; > include('include\weatherFeed_TEMP.txt') > ?> > [/code] > > and I've modified my python, making it a function > [code] > #!C:\Program Files\Python26\python.exe -u > # Current weather feed > # # Weather page sources: http://www.weather.gov/ > # Capture relevant data, strip unusable stuff out, fix URLs, display in our > HTML page > > # import program modules > from BeautifulSoup import BeautifulSoup as B_S > import re, urllib2, sys, mechanize # 'standard' set > > ### Return small weather table > def weatherSmall(c,s): > cityState = c + ', ' + s > query = 'Chicago, IL' #Test!!! > _URL = "http://www.weather.gov/" > > br=mechanize.Browser() > br.open( _URL ) > br.select_form( nr=1 ) #assuming form is 2nd form on page > br['inputstring'] = query > html = br.submit() > > _soup = B_S(html) > > # finding the correct table > _step1 = _soup.findAll('table', limit=7)[6] > col = _step1.findAll('td') > # sys.argv is included below as a test. > _thumb = '' + str(col[0]) + str(col[1]) + > '
Forecast for ' + query + '
' + > str(sys.argv) + '
' > _thumb = _thumb.replace( '11%','50%' ) > _thumb = _thumb.replace( '/images/', 'images/weather/' ) > > #write to txt file TEST > _temp = 'D:\\Inetpub\\AtoZ\\hometown\\include\\weatherFeed_TEMP.txt' > temp = open( _temp, 'w' ) > temp.write( _thumb ) > temp.close() > > return _thumb > > city = 'Establish' > state = 'Variables' > count = 0 > for ea in sys.argv: > if ea == '-c': > city = sys.argv[count+1] > elif ea == '-s': > state = sys.argv[count+1] > count+=1 > _result = str(weatherSmall(city,state)) > #print _result > [/code] > > Roy Hinkelman > I don't know anything about PHP, so take the following for what it's worth. So where is mechanize.py ? The import error means that it can't be found. Yet if you run the program standalone it's finding it? My guess is that it depends on the current directory when you enter the script. First thing to do is to split the import line, so that the import mechanize follows the others on its own line. Then you can put some debugging lines in between them, perhaps saving off the os.curdir and sys.path values. I suspect the difference between running under php and from command line is in one of those. DaveA From rdmoores at gmail.com Wed Oct 27 18:29:05 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Wed, 27 Oct 2010 09:29:05 -0700 Subject: [Tutor] The Format Specification Mini-Language of Python 3.1 Message-ID: I finally have learned the bare bones of the Format Specification Mini-Language of Python 3.1+, and thought I'd share something of what I've learned with fellow Tutorees who might be interested. I find this "mini-language" to be a refreshing change from the formatting in use in 2.6, etc. But when I first saw it in Learning Python, 4th ed., I was put off by it and never went back until I needed to yesterday. It was the docs themselves that had the best explanation and examples. So here are those links: < http://docs.python.org/py3k/library/string.html#format-specification-mini-language > I wanted to have the columns of output from my dollar2yen_rate_simple_for_python31.py line up correctly, and found, after a bit of experimentation, how to do it. If you run it (with Python 3.1+ (not 3.0)) , you'll see that the new formatting works perfectly -- the minus signs don't produce a jaggedness in those two columns 3 and 4 any more. Here's a screen shot of some lines of output: I found that formatting a number as a percentage is easy with the mini-language. Here's an example I whipped up: >>> a = 123.345 >>> b = 567 >>> a/b 0.21753968253968253 >>> print('{:.2%}'.format(a/b)) 21.75% In addition, if you are interested in doing some simple web scraping with 3.1, highlighted lines 6, 25, 26 in my code show what worked for me with what at first was very hard to understand until I found the examples in the docs for the use of urllib.request: < http://docs.python.org/py3k/library/urllib.request.html?highlight=urllib#examples> I've heard that a better way is to use BeautifulSoup (< http://www.crummy.com/software/BeautifulSoup/>), but figuring that out is a bit of a ways down my TODO list. Dick Moores -------------- next part -------------- An HTML attachment was scrubbed... URL: From scarolan at gmail.com Wed Oct 27 18:45:23 2010 From: scarolan at gmail.com (Sean Carolan) Date: Wed, 27 Oct 2010 11:45:23 -0500 Subject: [Tutor] Which string operation is best to extract text from database string? Message-ID: Forgive me for the newbie question; I'm sure there must be an easy way to do this in python: I have a database string that I'm grabbing from a file with readlines(): db.url=jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = server.company.com)(PORT =1521)) (ADDRESS = (PROTOCOL = TCP)(HOST = server.company.com)(PORT = 1521)) (ADDRESS = (PROTOCOL =TCP)(HOST = server.company.com)(PORT = 1521)) (LOAD_BALANCE = ON) (FAILOVER = ON) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = PROD2) (FAILOVER_MODE = (TYPE = SELECT) (METHOD = BASIC) (RETRIES = 180) (DELAY = 5)))) I want to extract the service name, PROD2 from the string. Normally I'd do this with sed or awk, but I'm trying to get away from using OS-dependent tools with Popen, etc. What would be the most efficient way to grab "PROD2" from this string of text? From emile at fenx.com Wed Oct 27 19:49:41 2010 From: emile at fenx.com (Emile van Sebille) Date: Wed, 27 Oct 2010 10:49:41 -0700 Subject: [Tutor] Which string operation is best to extract text from database string? In-Reply-To: References: Message-ID: On 10/27/2010 9:45 AM Sean Carolan said... > Forgive me for the newbie question; I'm sure there must be an easy way > to do this in python: > > I have a database string that I'm grabbing from a file with readlines(): > > db.url=jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS = (PROTOCOL = > TCP)(HOST = server.company.com)(PORT =1521)) (ADDRESS = (PROTOCOL = > TCP)(HOST = server.company.com)(PORT = 1521)) (ADDRESS = (PROTOCOL > =TCP)(HOST = server.company.com)(PORT = 1521)) (LOAD_BALANCE = ON) > (FAILOVER = ON) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = > PROD2) (FAILOVER_MODE = (TYPE = SELECT) (METHOD = BASIC) (RETRIES = > 180) (DELAY = 5)))) > > I want to extract the service name, PROD2 from the string. Normally > I'd do this with sed or awk, but I'm trying to get away from using > OS-dependent tools with Popen, etc. What would be the most efficient > way to grab "PROD2" from this string of text? Assuming you've got that string in variable text, and that "SERVICE_NAME =" is there to be found, I'd do something like servicename = text.split("SERVICE_NAME =")[1].split(")")[0] Emile From susana.delgado_s at utzmg.edu.mx Wed Oct 27 21:42:26 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Wed, 27 Oct 2010 14:42:26 -0500 Subject: [Tutor] Python xlwt unicoderror Message-ID: I'm starting to use xlwt to create excel files, my excel sheet is designed to get information from a python module. The data came from a raster file, the problem is that some kind of sign or character doesn't allow the file to be written. This is my script: import sys, os, time, socket, codecs from xlwt import Workbook from osgeo import gdal from osgeo.gdalconst import * from PIL import Image gdal.AllRegister() file_list = [] folders = None # look in this (root) folder for files with specified extension for root, folders, files in os.walk( "C:\\" ): file_list.extend(os.path.join(root,fi) for fi in files if fi.endswith(".tif") or fi.endswith(".tiff") or fi.endswith(".gtif") or fi.endswith(".ecw") or fi.endswith(".bil") or fi.endswith(".til") or fi.endswith(".jpeg") or fi.endswith(".img") or fi.endswith(".jpg")) wrkbk = Workbook() #Add named parameter to allow overwriting cells: cell_overwrite_ok=True wksht = wrkbk.add_sheet('raster') wksht.row(0).write(0,'ruta') wksht.row(0).write(1,'nombre') wksht.row(0).write(2,'x_min') wksht.row(0).write(3,'x_max') wksht.row(0).write(4,'y_min') wksht.row(0).write(5,'y_max') wksht.row(0).write(6,'ancho_pixel') wksht.row(0).write(7,'alto_pixel') wksht.row(0).write(8,'num_filas') wksht.row(0).write(9,'num_columnas') wksht.row(0).write(10,'num_bandas') wksht.row(0).write(11,'proyeccion') wksht.row(0).write(12,'fecha_modificacion') wksht.row(0).write(13,'maquina_host') wksht.row(0).write(14,'usuario') for row, filepath in enumerate(file_list, start=1): #Llenar lista de archivos y ruta unicode(filepath, errors='ignore') wksht.row(row).write(0, filepath) #Escribir solo el nombre del archivo (ruta, filename) = os.path.split(filepath) wksht.row(row).write(1, filename) unicode(filename, errors='ignore') # Obtener coordenadas x/y dataset = gdal.Open(filepath, GA_ReadOnly) if dataset is None: print 'Could not open...' +filename #iNFORMACIoN GEOREFERENCIA else: xOrigin, yOrigin, pixelWidth, pixelHeight, a, b = dataset.GetGeoTransform() geotransform = dataset.GetGeoTransform( #Obtener coordenadas x_min = geotransform[0] wksht.row(row).write(2,x_min) y_max = geotransform[3] wksht.row(row).write(5,y_max) pixel_width = geotransform[1] wksht.row(row).write(6,pixel_width) pixel_height = geotransform[5] wksht.row(row).write(7,pixel_height) #Obtener tamano de la imagen rows = dataset.RasterYSize cols = dataset.RasterXSize bands = dataset.RasterCount wksht.row(row).write(8,rows) wksht.row(row).write(9,cols) wksht.row(row).write(10,bands) x_max= (pixel_width - x_min) / pixel_width y_min = (y_max - x_min) / pixel_height wksht.row(row).write(3,x_max) wksht.row(row).write(4,y_min) #Obtener la proyeccion n = os.path.splitext(filepath) tif = n[0]+'.tif' tiff = n[0]+'.tiff' gtif = n[0]+'.gtif' ecw = n[0]+'.ecw' bil = n[0]+'.bil' til = n[0]+'.til' img = n[0]+'.img' jpg = n[0]+'.jpg' jpeg = n[0]+'.jpeg' if os.path.lexists(tif) or os.path.lexists(tiff) or os.path.lexists(gtif) or os.path.lexists(ecw) or os.path.lexists(bil) or os.path.lexists(til) or os.path.lexists(jpg) or os.path.lexists(jpeg): wksht.row(row).write(11, dataset.GetProjection()) #print filename else: wksht.row(row).write(11, "No tiene proyeccion") #Obtner el nombre del host del archivo wksht.row(row).write(13, socket.gethostname()) #Obtener usuario wksht.row(row).write(14,os.environ.get("USERNAME")) #Obtener fecha de modificacion t = time.strftime("%m/%d/%Y %I:%M:%S %p",time.localtime(os.path.getmtime(filepath))) wksht.row(row).write(15, t) codecs.EncodedFile(wrkbk, "UTF8",errors='ignore') wrkbk.save('rasters.xls') SEARCH_PATH = os.getcwd() TARGET_FILE = os.path.realpath('shp.xls') print "Buscando en", SEARCH_PATH, "and writing to", TARGET_FILE print "Encontrando archivos..." print "Escribiendo archivo de Excel..." print "Listo." The error is: Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import crawler_img ERROR 4: `C:\Archivos de programa\WinAce\menuimg.img' not recognised as a suppor ted file format. Could not open...menuimg.img ERROR 4: `C:\Archivos de programa\WinAce\toolimg.img' not recognised as a suppor ted file format. Could not open...toolimg.img ERROR 4: `C:\Documents and Settings\sinventario\Configuraci?n local\Archivos tem porales de Internet\Content.IE5\46MPK9B5\visitor[6].jpg' not recognised as a sup ported file format. Could not open...visitor[6].jpg ERROR 4: `C:\Documents and Settings\sinventario\Configuraci?n local\Archivos tem porales de Internet\Content.IE5\46MPK9B5\visitor[7].jpg' not recognised as a sup ported file format. Could not open...visitor[7].jpg ERROR 4: `C:\Documents and Settings\sinventario\Configuraci?n local\Archivos tem porales de Internet\Content.IE5\46MPK9B5\visitor[8].jpg' not recognised as a sup ported file format. Could not open...visitor[8].jpg ERROR 4: `C:\Documents and Settings\sinventario\Configuraci?n local\Archivos tem porales de Internet\Content.IE5\6LF1W0O1\visitor[1].jpg' not recognised as a sup ported file format. Could not open...visitor[1].jpg ERROR 4: `C:\Documents and Settings\sinventario\Configuraci?n local\Archivos tem porales de Internet\Content.IE5\6LF1W0O1\visitor[2].jpg' not recognised as a sup ported file format. Could not open...visitor[2].jpg ERROR 4: `C:\Documents and Settings\sinventario\Configuraci?n local\Archivos tem porales de Internet\Content.IE5\MZ2Z7NIK\visitor[1].jpg' not recognised as a sup ported file format. Could not open...visitor[1].jpg ERROR 4: `C:\Documents and Settings\sinventario\Configuraci?n local\Archivos tem porales de Internet\Content.IE5\MZ2Z7NIK\visitor[2].jpg' not recognised as a sup ported file format. Could not open...visitor[2].jpg ERROR 4: `C:\Documents and Settings\sinventario\Configuraci?n local\Archivos tem porales de Internet\Content.IE5\PBD68S04\safe_imageCAO4JFN0.jpg' does not exist in the file system, and is not recognised as a supported dataset name. Could not open...safe_imageCAO4JFN0.jpg ERROR 4: `C:\Documents and Settings\sinventario\Configuraci?n local\Archivos tem porales de Internet\Content.IE5\UQM8VB8W\visitor[1].jpg' not recognised as a sup ported file format. Could not open...visitor[1].jpg ERROR 4: `C:\Documents and Settings\sinventario\Configuraci?n local\Archivos tem porales de Internet\Content.IE5\UQM8VB8W\visitor[2].jpg' not recognised as a sup ported file format. Could not open...visitor[2].jpg ERROR 4: `C:\Documents and Settings\sinventario\Configuraci?n local\Archivos tem porales de Internet\Content.IE5\X6Q03YQD\visitor[1].jpg' not recognised as a sup ported file format. Could not open...visitor[1].jpg ERROR 4: `C:\Documents and Settings\sinventario\Escritorio\Kosmo-2.0\bin\ext\sex tante\help\es\es.unex.sextante.morphometry.surfaceSpecificPoints\clasificacion_f ormas_terreno.jpg' not recognised as a supported file format. Could not open...clasificacion_formas_terreno.jpg ERROR 4: `C:\Documents and Settings\sinventario\workspace\libs\sextante\help\es\ es.unex.sextante.morphometry.surfaceSpecificPoints\clasificacion_formas_terreno. jpg' not recognised as a supported file format. Could not open...clasificacion_formas_terreno.jpg ERROR 4: `C:\Kosmo_Desktop_2.0_RC1_src\libs\sextante\help\es\ es.unex.sextante.mo rphometry.surfaceSpecificPoints\clasificacion_formas_terreno.jpg' not recognised as a supported file format. Could not open...clasificacion_formas_terreno.jpg ERROR 4: `C:\WINDOWS\ServicePackFiles\i386\netwlan5.img' not recognised as a sup ported file format. Could not open...netwlan5.img ERROR 4: `C:\WINDOWS\system32\drivers\netwlan5.img' not recognised as a supporte d file format. Could not open...netwlan5.img ERROR 4: `C:\workspace\libs\sextante\help\es\es.unex.sextante.morphometry.surfac eSpecificPoints\clasificacion_formas_terreno.jpg' not recognised as a supported file format. Could not open...clasificacion_formas_terreno.jpg Traceback (most recent call last): File "", line 1, in File "crawler_img.py", line 118, in wrkbk.save('rasters.xls') File "C:\Python26\lib\site-packages\xlwt\Workbook.py", line 634, in save doc.save(filename, self.get_biff_data()) File "C:\Python26\lib\site-packages\xlwt\Workbook.py", line 609, in get_biff_d ata shared_str_table = self.__sst_rec() File "C:\Python26\lib\site-packages\xlwt\Workbook.py", line 571, in __sst_rec return self.__sst.get_biff_record() File "C:\Python26\lib\site-packages\xlwt\BIFFRecords.py", line 53, in get_biff _record self._add_to_sst(s) File "C:\Python26\lib\site-packages\xlwt\BIFFRecords.py", line 66, in _add_to_ sst u_str = upack2(s, self.encoding) File "C:\Python26\lib\site-packages\xlwt\UnicodeUtils.py", line 50, in upack2 us = unicode(s, encoding) UnicodeDecodeError: 'ascii' codec can't decode byte 0xed in position 79: ordinal not in range(128) >>> -------------- next part -------------- An HTML attachment was scrubbed... URL: From scarolan at gmail.com Wed Oct 27 21:57:48 2010 From: scarolan at gmail.com (Sean Carolan) Date: Wed, 27 Oct 2010 14:57:48 -0500 Subject: [Tutor] os.listdir for symbolic links? Message-ID: Is there an easy way to find the target of a symbolic link on a Linux system with python? I'm looking for something like the output of the bash command: "ls -l" From lie.1296 at gmail.com Wed Oct 27 21:48:06 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 28 Oct 2010 06:48:06 +1100 Subject: [Tutor] os.listdir for symbolic links? In-Reply-To: References: Message-ID: On 10/28/10 06:57, Sean Carolan wrote: > Is there an easy way to find the target of a symbolic link on a Linux > system with python? I'm looking for something like the output of the > bash command: "ls -l" you're looking for >>> os.path.realpath('/bin/uncompress') '/bin/gunzip' From mehgcap at gmail.com Wed Oct 27 23:46:32 2010 From: mehgcap at gmail.com (Alex Hall) Date: Wed, 27 Oct 2010 17:46:32 -0400 Subject: [Tutor] decorators (the "at" sign)? In-Reply-To: <650465.16412.qm@web44716.mail.sp1.yahoo.com> References: <650465.16412.qm@web44716.mail.sp1.yahoo.com> Message-ID: Thanks to all for the explanations. I think I understand how this works, basically. I doubt I will use the concept anytime soon, but I think I get it enough to follow what is happening in the source code of the application I am examining. On 10/27/10, Siren Saren wrote: > Alex, > > Many people have trouble wrapping their minds around decorators.? I couldn't > find a decent explanation either until reading an allegedly 'advanced' > python book (b/c 'advanced' is in the title, I guess). > > An easy explanation might be sufficient, if you don't intend to use them > yourself.? A decorator is a way of changing a function's behavior.? Most of > the time you see a decorator, the person using it could have merely > rewritten the function or method itself and given it some additional > capability or modified it in some way.? But, as you may have noticed, > programmers prefer to 'abstract' when they can, so they can avoid > 'duplicating code.'? (These are buzzwords you'll encounter a lot). > > Let's say your program needs to set an index in a lot of different > functions, and let's further imagine that setting an index is either more > than a line of code or that the index itself may change over the development > cycle, or that the index will vary according to some simple pattern > consistently defined in these other functions. > > To avoid writing the same code over and over and having the value of index > set in many different places, the developers chose instead to write the code > once and refer to it in all the other functions through a decorator, which > takes all the functions, modifies them so they get the index values they > need, and sets them back in their places (more or less). > > If you want the more complicated answer, I think I can take a reasonable > shot at showing how this works too and making an example.? But you may just > want a general description.? Also, I'm only about 4 months into learning to > program so you may prefer a more expert opinion. > > Cheers, > > Soren > > --- On Tue, 10/26/10, Alex Hall wrote: > > From: Alex Hall > Subject: [Tutor] decorators (the "at" sign)? > To: "tutor" > Date: Tuesday, October 26, 2010, 2:46 AM > > Hi all, > Now that I am able to run the source code of an open source > application I hope to one day help develop, I am trying to understand > how it works. One thing I keep seeing is an at sign followed by a > word, usually (maybe always) immediately preceeding a function > definition. For example, and I know this exact code will not make much > sense, but it gives the idea: > class Bing(Messages, Updating, Dismissable): > > @set_index > def get_url(self, index=None): > ? return self.storage[index]['Url'] > > What is the "@set_index" for? Specifically, what is the at sign doing? > Google was only able to provide me with a very vague idea of what is > going on, though it seems to crop up a lot in classmethod and > staticmethod calls (not sure about those either). I read PEP 318, but > it was not much help since I am coming at this having no idea what I > am looking at. The PEP did explain why I have never run into this > before, though - it is apparently specific to Python. I see this sort > of thing all over this source code so it seems like a good idea to get > exactly what it is for. TIA! > > > -- > Have a great day, > Alex (msg sent from GMail website) > mehgcap at gmail.com; http://www.facebook.com/mehgcap > _______________________________________________ > Tutor maillist? -? Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From rdmoores at gmail.com Thu Oct 28 02:43:08 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Wed, 27 Oct 2010 17:43:08 -0700 Subject: [Tutor] The Format Specification Mini-Language of Python 3.1 In-Reply-To: References: Message-ID: On Wed, Oct 27, 2010 at 09:29, Richard D. Moores wrote: > > In addition, if you are interested in doing some simple web scraping with > 3.1, highlighted lines 6, 25, 26 in my code show what worked for me with > what at first was very hard to understand until I found the examples in the > docs for the use of urllib.request: > Thought I'd report that there is a "HOWTO Fetch Internet Resources Using The urllib Package" at Dick From jojo.mwebaze at gmail.com Thu Oct 28 11:52:44 2010 From: jojo.mwebaze at gmail.com (Jojo Mwebaze) Date: Thu, 28 Oct 2010 11:52:44 +0200 Subject: [Tutor] compare two souce files Message-ID: Hello Tutor I would like to compare two souce code files but ignoring doc strings, comments and space (and perharps in future statement by statement comparision) e.g class Foo def foo(): # prints my name return 'my name' class Boo def boo(): print 'my name' Want to check if Foo and Boo are the same. At functional level one can use boo.func_code.co_code == foo.func_code.co_code What is the class level equivlant of this? cheers -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Thu Oct 28 13:22:25 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 28 Oct 2010 22:22:25 +1100 Subject: [Tutor] compare two souce files In-Reply-To: References: Message-ID: <4CC95CF1.1080905@pearwood.info> Jojo Mwebaze wrote: > Hello Tutor > > I would like to compare two souce code files but ignoring doc strings, > comments and space (and perharps in future statement by statement > comparision) > > e.g > > class Foo > def foo(): > # prints my name > return 'my name' > > class Boo > def boo(): > print 'my name' > > Want to check if Foo and Boo are the same. At functional level one can use > > boo.func_code.co_code == foo.func_code.co_code > > What is the class level equivlant of this? Foo.foo.func_code.co_code == Boo.boo.func_code.co_code To check if the classes are equivalent, something like this will get you started: if dir(Foo) == dir(Boo): for attr in dir(Foo): if getattr(Foo, attr) != getattr(Boo, attr): print "Unequal" break -- Steven From scarolan at gmail.com Thu Oct 28 21:34:12 2010 From: scarolan at gmail.com (Sean Carolan) Date: Thu, 28 Oct 2010 14:34:12 -0500 Subject: [Tutor] Python: 27 times faster than bash Message-ID: I rewrote a bash script that gathers data from a bunch of text files. The script uses grep, sed, and awk to parse the data and takes around 5.5 seconds to run. After rewriting the script in python, it now takes a little over 0.2 seconds to run. Thanks to those of you who helped me with some questions on proper syntax. From tmantjg at yahoo.com Wed Oct 27 20:04:45 2010 From: tmantjg at yahoo.com (Terry Green) Date: Wed, 27 Oct 2010 11:04:45 -0700 Subject: [Tutor] (no subject) Message-ID: <7A7261BCA9D740F4BCBA33C1D40A6A09@terryPC> Need Help!] def main(): pass if __name__ == '__main__': main() postPos=words[3] f = open ("c:/users/terry/downloads/tup1012k/tup1012x.drf","r") lines = f.readlines() for line in lines: words = line.split(",") print (words[3],postPos) close = f When I run the above script, the field postPos doesn't match the input! postPost should be the same as words[3] can't guess why? any help? *** Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32. *** >>> 1 3 2 3 3 3 >>> Terry Green -------------- next part -------------- An HTML attachment was scrubbed... URL: From rabidpoobear at gmail.com Thu Oct 28 21:54:37 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Thu, 28 Oct 2010 14:54:37 -0500 Subject: [Tutor] (no subject) In-Reply-To: <7A7261BCA9D740F4BCBA33C1D40A6A09@terryPC> References: <7A7261BCA9D740F4BCBA33C1D40A6A09@terryPC> Message-ID: <917DF123-9426-4898-B10F-3A54FBA42CC5@gmail.com> This code shouldn't even actually execute, you should get an indexerror exception. Close your IDE and try it again. Should make the issue more clear. ----------------------------- Sent from a mobile device with a bad e-mail client. ----------------------------- On Oct 27, 2010, at 1:04 PM, "Terry Green" wrote: > Need Help!] > > > > def main(): > > pass > > > > if __name__ == '__main__': > > main() > > postPos=words[3] > > f = open ("c:/users/terry/downloads/tup1012k/tup1012x.drf","r") > > lines = f.readlines() > > for line in lines: > > words = line.split(",") > > print (words[3],postPos) > > close = f > > > > When I run the above script, the field postPos doesn't match the input! > > postPost should be the same as words[3] > > can't guess why? > > any help? > > > > *** Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32. *** > > >>> > > 1 3 > > 2 3 > > 3 3 > > >>> > > > > Terry Green > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Fri Oct 29 03:11:04 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 29 Oct 2010 12:11:04 +1100 Subject: [Tutor] (no subject) In-Reply-To: <7A7261BCA9D740F4BCBA33C1D40A6A09@terryPC> References: <7A7261BCA9D740F4BCBA33C1D40A6A09@terryPC> Message-ID: <4CCA1F28.5020503@pearwood.info> Terry Green wrote: > def main(): > pass Useless function, does nothing. Why is it here? > if __name__ == '__main__': > main() Also does nothing useful. Why is it here? > postPos=words[3] This like will fail with NameError, because words is not defined anywhere. This is not the code you are running. We can't tell what is wrong with the code you run when you show us something completely different. Please send us the *actual* code you run, not something you've re-typed from memory, or copied and pasted in bits and pieces from different places. By the way, why are you calling a *word* "postPos"? Pos normally is an abbreviation for *position*, which will be a number. To say: postPos = words[3] makes postPos a word, not a position. So the name is misleading. You should change it. > f = open ("c:/users/terry/downloads/tup1012k/tup1012x.drf","r") > lines = f.readlines() > for line in lines: > words = line.split(",") > print (words[3],postPos) > > close = f I'm pretty sure that's not what you want. This line "close = f" will define a new variable called "close", which is equal to the open file object "f". What you want is to call the close method of the file object: f.close() > When I run the above script, the field postPos doesn't match the input! > postPost should be the same as words[3] You only set postPos once, outside the loop, and then never update it inside the loop. Follow the execution chain by hand: lines = ["a,b,c,d,e", "f,g,h,i,j,k"] Somehow postPos gets set to something before the loop starts. I don't know how, because you haven't shown us that part of the code. But let's say postPos is set to "z", just as an example. So you enter the for-loop: line gets set to "a,b,c,d,e" words get sets to ["a", "b", "c", "d", "e"] print words[3], postPos => prints "d", "z" line gets set to "f,g,h,i,j,k" words get sets to ["f", "g", "h", "i", "j", "k"] print words[3], postPos => prints "i", "z" -- Steven From rabidpoobear at gmail.com Fri Oct 29 03:23:06 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Thu, 28 Oct 2010 20:23:06 -0500 Subject: [Tutor] (no subject) In-Reply-To: <4CCA1F28.5020503@pearwood.info> References: <7A7261BCA9D740F4BCBA33C1D40A6A09@terryPC> <4CCA1F28.5020503@pearwood.info> Message-ID: On Thu, Oct 28, 2010 at 8:11 PM, Steven D'Aprano wrote: > >> postPos=words[3] > > This like will fail with NameError, because words is not defined anywhere. > > This is not the code you are running. We can't tell what is wrong with the > code you run when you show us something completely different. > > Please send us the *actual* code you run, not something you've re-typed from > memory, or copied and pasted in bits and pieces from different places. > Actually Steven, if you are using IDLE (as a lot of new programmers are), the code by default does NOT execute in a subprocess but instead in the same interpreter IDLE is running. Therefore it can actually have variables stick around from previous executions. So if he didn't have that line in his code the first time he executed, then words would be defined. Then the second time perhaps he added that line, and in that case it will actually succeed because an old value of 'words' is floating around. It also causes issues because if you import a module from the interpreter and then use it in your code, it will work only during that instance, but if you try to run the code standalone it will fail when you try to use the module. I know that sounds weird and that's why IDLE should be configured to run as a subprocess. From steve at pearwood.info Fri Oct 29 04:55:58 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 29 Oct 2010 13:55:58 +1100 Subject: [Tutor] (no subject) In-Reply-To: References: <7A7261BCA9D740F4BCBA33C1D40A6A09@terryPC> <4CCA1F28.5020503@pearwood.info> Message-ID: <4CCA37BE.80605@pearwood.info> Luke Paireepinart wrote: > On Thu, Oct 28, 2010 at 8:11 PM, Steven D'Aprano wrote: >>> postPos=words[3] >> This like will fail with NameError, because words is not defined anywhere. >> >> This is not the code you are running. We can't tell what is wrong with the >> code you run when you show us something completely different. >> >> Please send us the *actual* code you run, not something you've re-typed from >> memory, or copied and pasted in bits and pieces from different places. >> > Actually Steven, if you are using IDLE (as a lot of new programmers > are), the code by default does NOT execute in a subprocess but instead > in the same interpreter IDLE is running. Therefore it can actually > have variables stick around from previous executions. And? That's no different from running code in the default Python interactive interpreter. If you have run code that has an effect on the results you are getting, and don't show that code, then the code you have run is not the code you have shown. Just like I said :) > So if he didn't > have that line in his code the first time he executed, then words > would be defined. Then the second time perhaps he added that line, > and in that case it will actually succeed because an old value of > 'words' is floating around. Exactly. The code the Original Poster *actually* executed included a line that defined words. In this case, any extra code is probably innocuous, but next time, who knows? I then went on to identify the likely cause of the problem -- the OP fails to redefine postPos each time through the loop. But can I be sure? No, because I don't know what else the OP did. I can't think of anything realistic that might have an effect, but perhaps that's my failure of imagination. When you have a problem with code, it is *vital* that you know what code is actually being executed. There are very few programming tasks harder than trying to debug code that doesn't actually contain any bugs, or contains bugs different from the ones you are seeing, because the code you are actually executing is something different from what you think you are executing. You know the joke about the drunk who lost his keys in the park, and then spends the next few hours searching under the light pole in the street because the light is better there? That's what it can be like. > It also causes issues because if you import a module from the > interpreter and then use it in your code, it will work only during > that instance, but if you try to run the code standalone it will fail > when you try to use the module. > > I know that sounds weird and that's why IDLE should be configured to > run as a subprocess. Regardless of whether you are using IDLE, some other IDE, or just the Python interpreter, you should never post code until you are sure that it is the actual code that is being executed. If that means quitting IDLE and starting it again in a fresh session, then do so. For anything more than six or eight lines of code, I never trust I understand it until I've seen it run in a fresh environment. -- Steven From rdmoores at gmail.com Fri Oct 29 08:07:28 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 28 Oct 2010 23:07:28 -0700 Subject: [Tutor] How to install BeautifulSoup? Message-ID: 64-bit Vista Python 2.6.4 I just downloaded BeautifulSoup-3.0.8.1, which is a folder containing setup.py. Does anyone know how to run setup,py? I've already tried C:\Python26\Lib\site-packages\BeautifulSoup-3.0.8.1>setup.py File "C:\Python26\Lib\site-packages\BeautifulSoup-3.0.8.1\setup.py", line 22 print "Unit tests have failed!" ^ and C:\Python26\Lib\site-packages\BeautifulSoup-3.0.8.1>python setup.py File "setup.py", line 22 print "Unit tests have failed!" ^ Thanks, Dick Moores -------------- next part -------------- An HTML attachment was scrubbed... URL: From rdmoores at gmail.com Fri Oct 29 08:32:00 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 28 Oct 2010 23:32:00 -0700 Subject: [Tutor] How to install BeautifulSoup? In-Reply-To: References: Message-ID: On Thu, Oct 28, 2010 at 23:18, Abhijeet Rastogi wrote: > > In command prompt, do something like > > $python setup.py install Yes, that did it. Thanks! Dick From bala.biophysics at gmail.com Fri Oct 29 11:45:18 2010 From: bala.biophysics at gmail.com (Bala subramanian) Date: Fri, 29 Oct 2010 11:45:18 +0200 Subject: [Tutor] module for clustering Message-ID: Friends, I have X and Y points and i want to cluster these points in 2D space, Kindly suggest if there is any module that is loaded with python to do that or any other package for the same. Thanks, Bala -------------- next part -------------- An HTML attachment was scrubbed... URL: From waynejwerner at gmail.com Fri Oct 29 12:54:39 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Fri, 29 Oct 2010 05:54:39 -0500 Subject: [Tutor] module for clustering In-Reply-To: References: Message-ID: On Fri, Oct 29, 2010 at 4:45 AM, Bala subramanian wrote: > Friends, > I have X and Y points and i want to cluster these points in 2D space, > Kindly suggest if there is any module that is loaded with python to do that > or any other package for the same. > matplotlib is pretty much amazing in that regard. If you're using Windows, I highly suggest downloading Python(x,y) from www.pythonxy.com - it's a package of python and other tools specifically designed for scientific computing, and as the biophysics in your email address suggests, I presume you will probably find many of the provided tools very helpful. It includes matplotlib, which is the foremost graphing module in Python that I know of, and is quite advanced, including capability for 3d graphs. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From scarolan at gmail.com Fri Oct 29 16:09:50 2010 From: scarolan at gmail.com (Sean Carolan) Date: Fri, 29 Oct 2010 09:09:50 -0500 Subject: [Tutor] Python: 27 times faster than bash In-Reply-To: References: <20101029135350.GA339@chicago.blisses.org> Message-ID: > Can we see a version of your script? How about just a couple snippets...there's no proprietary data but I want to be on the safe side. ?The original script consisted of a bunch of functions similar to the one below. ?When I first wrote this I didn't know how to use sed very well, so I used variable substitution to strip out the data I wanted: So this function in bash: function gettomcatserver () { # Strip out the short hostname ? ? ? ?remoteserver=$(grep remote.server.host $adminfile) ? ? ? ?remoteserver=${remoteserver##*=} ? ? ? ?remoteserver=${remoteserver%%.*} ? ? ? ?echo $remoteserver } Became this function in python: def gettomcatserver( customername ): ? ?for line in open(adminfile).readlines(): ? ? ? ?if line.startswith("remote.server.host="): ? ? ? ? ? ?return line.split("remote.server.host=")[1].split(".example.com")[0] The total script runs several functions like this for each $customername; I was quite impressed with the difference in speed. From susana.delgado_s at utzmg.edu.mx Fri Oct 29 18:49:11 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Fri, 29 Oct 2010 11:49:11 -0500 Subject: [Tutor] If statement isn't working Message-ID: Hello members: I'm trying to validate the existence of a file by an if statement, but it isn't working correctly. The module is a crawler that writes to excel some attributes from the files it finds. folders = None # look in this (root) folder for files with specified extension for root, folders, files in os.walk( "C:\\" ): file_list.extend(os.path.join(root,fi) for fi in files if fi.endswith(".shp")) wrkbk = Workbook() #Add named parameter to allow overwriting cells: cell_overwrite_ok=True wksht = wrkbk.add_sheet('shp') #... add the headings to the columns wksht.row(0).write(0,'ruta') wksht.row(0).write(1,'archivo') #write the rows for row, filepath in enumerate(file_list, start=1): wksht.row(row).write(0, filepath) (ruta, filename) = os.path.split(filepath) wksht.row(row).write(1, filename) #Here I`m splitting the string filename in base and extension n = os.path.splitext(filename) #Getting the base and prj as its extension p = n[0]+'.prj' #validate if p exists if os.path.lexists(p): wksht.row(row).write(9, 1) prj_text = open(p, 'r').read() wksht.row(row).write(8,prj_text) else: wksht.row(row).write(9, 0) wksht.row(row).write(8, 'Sin prj, no se puede determinar la proyeccion') The issue is that it just identifies 3 prj and I have more, befores I added prj_text = open(p, 'r').read() wksht.row(row).write(8,prj_text) it worked fine and write the correct prj, but now it doesn't What am I doing wrong? Hope you can help me -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Oct 30 02:51:29 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 30 Oct 2010 01:51:29 +0100 Subject: [Tutor] (no subject) References: <7A7261BCA9D740F4BCBA33C1D40A6A09@terryPC><4CCA1F28.5020503@pearwood.info> <4CCA37BE.80605@pearwood.info> Message-ID: "Steven D'Aprano" wrote > is actually being executed. There are very few programming tasks > harder than trying to debug code that doesn't actually contain any > bugs, or contains bugs different from the ones you are seeing, > because the code you are actually executing is something different > from what you think you are executing. To illustrate with a true story (back in the days when you had to build and maintain your own compilers!): Take a C compiler source code and modify it so it produces faulty executable code but does not crash or otherwise report an error. Compile the now faulty compiler source code with the old (ie working) compiler. Fix the source code bug. Use the new (now broken) compiler to compile the now perfect source code to produce a broken compiler with a slightly different defect. Now use the resulting compiler to recompile the 'perfect' source code. Now figure out why none of your executables work as expected. That took us nearly 2 weeks to figure out... :-( (And made us very thankful for source code version control!) Alan G. From davea at ieee.org Sat Oct 30 13:08:25 2010 From: davea at ieee.org (Dave Angel) Date: Sat, 30 Oct 2010 07:08:25 -0400 Subject: [Tutor] (no subject) In-Reply-To: References: <7A7261BCA9D740F4BCBA33C1D40A6A09@terryPC><4CCA1F28.5020503@pearwood.info> <4CCA37BE.80605@pearwood.info> Message-ID: <4CCBFCA9.1080003@ieee.org> On 2:59 PM, Alan Gauld wrote: > > "Steven D'Aprano" wrote > >> is actually being executed. There are very few programming tasks >> harder than trying to debug code that doesn't actually contain any >> bugs, or contains bugs different from the ones you are seeing, >> because the code you are actually executing is something different >> from what you think you are executing. > > To illustrate with a true story (back in the days when you had to build > and maintain your own compilers!): > > Take a C compiler source code and modify it so it produces faulty > executable code but does not crash or otherwise report an error. > Compile the now faulty compiler source code with the old (ie working) > compiler. > Fix the source code bug. > Use the new (now broken) compiler to compile the now perfect source > code to produce a broken compiler with a slightly different defect. > Now use the resulting compiler to recompile the 'perfect' source code. > Now figure out why none of your executables work as expected. > > That took us nearly 2 weeks to figure out... :-( > (And made us very thankful for source code version control!) > > Alan G. > That sounds remarkably parallel to a speech given by Ken Thompson: http://cm.bell-labs.com/who/ken/trust.html though there the point was that a trojan could be inserted into a compiler (or other programming tool) that would survive repair of the source code. There has been at least one viruse which has been credited to this approach. http://www.symantec.com/connect/blogs/interesting-case-induc-virus DaveA From tim at johnsons-web.com Sat Oct 30 21:32:12 2010 From: tim at johnsons-web.com (Tim Johnson) Date: Sat, 30 Oct 2010 11:32:12 -0800 Subject: [Tutor] possible to run a python script from non-cgi? Message-ID: <20101030193212.GB10493@johnsons-web.com> FYI: I am working in a linux environment with python 2.6.5 am an experienced web developer with 8 years in python, but :) I have never tried this trick before: I note that with the right .htaccess file, I can run a php file, from a non-cgi location. Example: On my machine, my wwwroot is at /home/http/, I have /home/http/php/test/index.php and I have run index.php as http://localhost/php/test/ (again with the correct .hataccess). Is it possible to run a python script this way? thanks tj -- Tim tim at johnsons-web.com or akwebsoft.com http://www.akwebsoft.com From evert.rol at gmail.com Sat Oct 30 23:15:54 2010 From: evert.rol at gmail.com (Evert Rol) Date: Sat, 30 Oct 2010 23:15:54 +0200 Subject: [Tutor] possible to run a python script from non-cgi? In-Reply-To: <20101030193212.GB10493@johnsons-web.com> References: <20101030193212.GB10493@johnsons-web.com> Message-ID: > FYI: I am working in a linux environment with python 2.6.5 > am an experienced web developer with 8 years in python, but > :) I have never tried this trick before: > > I note that with the right .htaccess file, I can run a php file, > from a non-cgi location. > Example: On my machine, my wwwroot is at /home/http/, I have > /home/http/php/test/index.php and I have run index.php as > http://localhost/php/test/ (again with the correct .hataccess). > > Is it possible to run a python script this way? I wouldn't think so, because index.php is not run as a cgi-script. Whether Python will be interpreted correctly depends entirely on the configuration of your webserver, in particular whether you're using/loading the correct modules. But if configured correctly, yes, you can 'run' Python scripts. Mod_wsgi arranges this for you, and this is generally how things like Django run. But the easiest way to find out is to try it out, right? I also guess this question might be better answered on the forum corresponding to your webserver (Apache?), since it appears to deal more with the server setup than actually with Python. Those are my best guesses/ideas. HTH, Evert From shantanoo at gmail.com Sat Oct 30 23:56:40 2010 From: shantanoo at gmail.com (=?utf-8?B?4KS24KSC4KSk4KSo4KWC?=) Date: Sun, 31 Oct 2010 03:26:40 +0530 Subject: [Tutor] possible to run a python script from non-cgi? In-Reply-To: <20101030193212.GB10493@johnsons-web.com> References: <20101030193212.GB10493@johnsons-web.com> Message-ID: Hi Tim, Reply inline. On 31-Oct-2010, at 1:02 AM, Tim Johnson wrote: > FYI: I am working in a linux environment with python 2.6.5 > am an experienced web developer with 8 years in python, but > :) I have never tried this trick before: > > I note that with the right .htaccess file, I can run a php file, > from a non-cgi location. > Example: On my machine, my wwwroot is at /home/http/, I have > /home/http/php/test/index.php and I have run index.php as > http://localhost/php/test/ (again with the correct .hataccess). > > Is it possible to run a python script this way? Have not tried it, but should be possible. Following link could be helpful. http://docs.python.org/library/cgi.html From http://en.wikipedia.org/wiki/Common_Gateway_Interface === From the Web server's point of view, certain locators, e.g. http://www.example.com/wiki.cgi, are defined as corresponding to a program to execute via CGI. When a request for the URL is received, the corresponding program is executed. Web servers often have a cgi-bin/ directory at the base of their directory tree to hold executable files called with CGI. === HTH. -- shantanoo From tim at johnsons-web.com Sun Oct 31 00:08:00 2010 From: tim at johnsons-web.com (Tim Johnson) Date: Sat, 30 Oct 2010 14:08:00 -0800 Subject: [Tutor] possible to run a python script from non-cgi? In-Reply-To: References: <20101030193212.GB10493@johnsons-web.com> Message-ID: <20101030220800.GA13875@johnsons-web.com> * Evert Rol [101030 13:23]: > > FYI: I am working in a linux environment with python 2.6.5 > > am an experienced web developer with 8 years in python, but > > :) I have never tried this trick before: > > > > I note that with the right .htaccess file, I can run a php file, > > from a non-cgi location. > > Example: On my machine, my wwwroot is at /home/http/, I have > > /home/http/php/test/index.php and I have run index.php as > > http://localhost/php/test/ (again with the correct .hataccess). > > > > Is it possible to run a python script this way? > > I wouldn't think so, because index.php is not run as a cgi-script. > Whether Python will be interpreted correctly depends entirely on the configuration of your webserver, in particular whether you're using/loading the correct modules. > But if configured correctly, yes, you can 'run' Python scripts. Mod_wsgi arranges this for you, and this is generally how things like Django run. That's the keyword - 'mod_wsgi' > But the easiest way to find out is to try it out, right? I did, didn't work. > I also guess this question might be better answered on the forum > corresponding to your webserver (Apache?), since it appears to > deal more with the server setup than actually with Python. Understood. Thanks -- Tim tim at johnsons-web.com or akwebsoft.com http://www.akwebsoft.com From benderjacob44 at gmail.com Sun Oct 31 00:44:28 2010 From: benderjacob44 at gmail.com (Jacob Bender) Date: Sat, 30 Oct 2010 18:44:28 -0400 Subject: [Tutor] (no subject) Message-ID: Dear Tutors, I was wondering how I could make an AI for creatures that run around, and try to survive. Like polyworld. The real problem is making the code connection to make them learn. Can you please help? Thanks From oberoc at gmail.com Sun Oct 31 01:00:36 2010 From: oberoc at gmail.com (Tino Dai) Date: Sat, 30 Oct 2010 19:00:36 -0400 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On Sat, Oct 30, 2010 at 6:44 PM, Jacob Bender wrote: > Dear Tutors, > > I was wondering how I could make an AI for creatures that run > around, and try to survive. Like polyworld. The real problem is making > the code connection to make them learn. Can you please help? > > Thanks > Hi Jacob, That is a really general question. Usually people have a specific question and/or some code they are having problems with. I don't know your level of Python knowledge, but you should check out http://wiki.hacdc.org/index.php/NARG for more information. They are doing stuff along the lines of what you are asking for. HTH, Tino -------------- next part -------------- An HTML attachment was scrubbed... URL: From tim at johnsons-web.com Sun Oct 31 01:17:12 2010 From: tim at johnsons-web.com (Tim Johnson) Date: Sat, 30 Oct 2010 15:17:12 -0800 Subject: [Tutor] possible to run a python script from non-cgi? In-Reply-To: References: <20101030193212.GB10493@johnsons-web.com> Message-ID: <20101030231712.GB13875@johnsons-web.com> * ????? [101030 14:08]: > Hi Tim, Reply inline. Sorry, I don't understand the preceding line. > On 31-Oct-2010, at 1:02 AM, Tim Johnson wrote: > > > FYI: I am working in a linux environment with python 2.6.5 am an > > experienced web developer with 8 years in python, but :) I have > > never tried this trick before: > > > > I note that with the right .htaccess file, I can run a php file, > > from a non-cgi location. Example: On my machine, my wwwroot is > > at /home/http/, I have /home/http/php/test/index.php and I have > > run index.php as http://localhost/php/test/ (again with the > > correct .hataccess). > > > > Is it possible to run a python script this way? > > > Following link could be helpful. > http://docs.python.org/library/cgi.html > > From http://en.wikipedia.org/wiki/Common_Gateway_Interface > From the Web server's point of view, certain locators, e.g. > http://www.example.com/wiki.cgi, are defined as corresponding to a > program to execute via CGI. When a request for the URL is > received, the corresponding program is executed. > Web servers often have a cgi-bin/ directory at the base of their > directory tree to hold executable files called with CGI. I am familiar with the cgi interface, I've earned a living as a web programmer for 15 years and written several CGI APIs from scratch, as well as building my own python API on top of the standard python cgi module. However, I was approached by a client with a whole lot of python code that has grown like topsy without a formal framework like django or a CMS, for that matter. The client wanted to convert to what he called "clean URLs" not showing a cgi-bin path or a file extension. I was well-advised by the previous respondant to post to an apache forum or mailing list and he also made reference to mod_wsgi, which likely will eventually be implemented on this project. For the time being, I did a little work-around: 1)In my /etc/apache2/sites-available/default I placed the following entry: """ ScriptAlias /reg/ /home/http/py/ AllowOverride all Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all """ Note1: This is ubuntu 10.04, apache2 config directories differen with linux distros. Note2: The scriptalias entry is *in addition to* the more standardized entry creating a cgi-bin path, which I also have, as apache2 supports multiple script aliases. 2)I placed the following .htaccess file at /home/http/py/ """ RewriteEngine on RewriteCond $1 !^(index\.py|images|css|js|robots\.txt|favicon\.ico) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ ./index.py/$1 [L,QSA] """ Disclaimer: I don't do a lot of apache configs and I don't know beans about .htaccess protocols (always more to learn). - and I placed an executable called dispatch.py in the same directory. Now I can point my browser at http://localhost/reg/dispatch -with no cgi-bin and no file extension - and get a response. So I now can make my customer happy and hold the fort so to speak until I get up to speed on either django or mode_wsgi or both. Perhaps this info is of interest to others. thanks for the replies. -- Tim tim at johnsons-web.com or akwebsoft.com http://www.akwebsoft.com From tim at johnsons-web.com Sun Oct 31 01:31:15 2010 From: tim at johnsons-web.com (Tim Johnson) Date: Sat, 30 Oct 2010 15:31:15 -0800 Subject: [Tutor] possible to run a python script from non-cgi? In-Reply-To: <20101030231712.GB13875@johnsons-web.com> References: <20101030193212.GB10493@johnsons-web.com> <20101030231712.GB13875@johnsons-web.com> Message-ID: <20101030233115.GC13875@johnsons-web.com> * Tim Johnson [101030 15:24]: I've taken this one step further: .htaccess => RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ ./index.py/$1 [L,QSA] now I get the response from http://localhost/reg/ cheers -- Tim tim at johnsons-web.com or akwebsoft.com http://www.akwebsoft.com From kirotawa at gmail.com Sun Oct 31 00:57:11 2010 From: kirotawa at gmail.com (leo kirotawa) Date: Sat, 30 Oct 2010 19:57:11 -0300 Subject: [Tutor] (no subject) Message-ID: Jacob, Look about steering behaviours [1], I believe this what you find. [1] http://www.red3d.com/cwr/steer/ -- Le?nidas S. Barbosa (Kirotawa) [DesenvolvedorWeb/CEFET/RN] [Ci?ncias da Computa??o/UFRN] [p?s-graduando em Intelig?ncia Computacional/Processamento Gr?fico /UFRN [Estudante de japon?s n?vel Intermedi?rio I - Japanese Student] [Desenvolvedor em python, PyGame] blog nerd: corecode.wordpress.com/ blog music: essenaomanja.blogspot.com blog tirinhas: elminiche.wordpress.com/ "Mais s?bio ? aquele que sabe que n?o sabe" (S?crates) ????????? ??????????????. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tmantjg at yahoo.com Sun Oct 31 01:12:11 2010 From: tmantjg at yahoo.com (Terry Green) Date: Sat, 30 Oct 2010 16:12:11 -0700 Subject: [Tutor] Stumped Again Message-ID: Am running this Script and cannot figure out how to close my files, Keep getting msg: Attribute Error: '_csv.writer' object has no attribute 'close' Why? import csv testOutput = csv.writer(open('c:/users/terry/downloads/tup1012k/tup1012.csv', 'w'), delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC) csvreader = csv.reader(open("c:/users/terry/downloads/tup1012k/tup1012x.drf","r"),delimi ter=',') for row in csvreader: test=('test4') track=row[0] rdate=row[1] race=row[2] postPos=row[3] entry=row[4] distance=row[5] surface=row[6] Reserved=row[7] raceType=row[8] ageSex=row[9] todaysRaceClassification=row[10] purse=row[11] claimingPrice=row[12] jockey=row[32] jockeySts = int(row[34]) jockeyWins = int(row[35]) horseName=row[44] daysSinceLR=row[223] try: jockeyPct=round((jockeyWins/jockeySts)*100) except ZeroDivisionError: print (track,race,postPos,horseName,jockey,jockeyWins,jockeySts,0) else: print (track,race,postPos,horseName,jockey,jockeyWins,jockeySts,jockeyPct) testOutput.writerow((track,race,postPos,horseName,jockey,jockeyWins,jockeySt s,jockeyPct,'\n')) testOutput.close() csvreader.close() Please Help!!!!! Terry Green -------------- next part -------------- An HTML attachment was scrubbed... URL: From bkjones at gmail.com Sun Oct 31 02:08:59 2010 From: bkjones at gmail.com (Brian Jones) Date: Sat, 30 Oct 2010 21:08:59 -0400 Subject: [Tutor] Stumped Again In-Reply-To: References: Message-ID: On Sat, Oct 30, 2010 at 7:12 PM, Terry Green wrote: > *Am running this Script and cannot figure out how to close my files,* > > *Keep getting msg: Attribute Error: ?_csv.writer? object has no attribute > ?close?* > > *Why?* > Because csv.writer objects don't have a close() method. Files do :) > testOutput = > csv.writer(open('c:/users/terry/downloads/tup1012k/tup1012.csv', 'w'), > See this line? testOutput is a csv.writer object, which you pass a file to. How about creating a file handle first, and passing that in, like this: testOutputFile = open('c:/users/terry/downloads/whatever.csv', 'w') testOutputCSV = csv.writer(testOutputFile) <....do stuff...> testOutputFile.close() You can also use the 'with' statement: with open('c:/users/terry/whatever.csv', 'w') as myfile: testOutputCSV = csv.writer(myfile) <....do stuff...> The 'with' statement is used with ContextManager objects, and files implement the ContextManager protocol which (among other things) means that the file will be closed in the above snippet without you having to explicitly call close() on it. hth. brian > delimiter=',', > > quotechar='"', quoting=csv.QUOTE_NONNUMERIC) > > csvreader = > csv.reader(open("c:/users/terry/downloads/tup1012k/tup1012x.drf","r"),delimiter=',') > > for row in csvreader: > > test=('test4') > > track=row[0] > > rdate=row[1] > > race=row[2] > > postPos=row[3] > > entry=row[4] > > distance=row[5] > > surface=row[6] > > Reserved=row[7] > > raceType=row[8] > > ageSex=row[9] > > todaysRaceClassification=row[10] > > purse=row[11] > > claimingPrice=row[12] > > jockey=row[32] > > jockeySts = int(row[34]) > > jockeyWins = int(row[35]) > > horseName=row[44] > > daysSinceLR=row[223] > > try: > > jockeyPct=round((jockeyWins/jockeySts)*100) > > except ZeroDivisionError: > > print > (track,race,postPos,horseName,jockey,jockeyWins,jockeySts,0) > > else: > > print > (track,race,postPos,horseName,jockey,jockeyWins,jockeySts,jockeyPct) > > > testOutput.writerow((track,race,postPos,horseName,jockey,jockeyWins,jockeySts,jockeyPct,'\n')) > > testOutput.close() > > csvreader.close() > > > > Please Help!!!!! > > > > Terry Green > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- Brian K. Jones My Blog http://www.protocolostomy.com Follow me http://twitter.com/bkjones -------------- next part -------------- An HTML attachment was scrubbed... URL: From g.nius.ck at gmail.com Sun Oct 31 02:11:22 2010 From: g.nius.ck at gmail.com (Chris King) Date: Sat, 30 Oct 2010 21:11:22 -0400 Subject: [Tutor] File transfer Message-ID: <4CCCC23A.4040906@gmail.com> Dear Tutors, How would I send a file from one computer to another. I have modules which can send simple objects, such as dictionaries with simple objects in it. They can't send files thou. Please help. Sincerely, Me -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: server.py URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: client.py URL: From steve at pearwood.info Sun Oct 31 02:21:06 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 31 Oct 2010 12:21:06 +1100 Subject: [Tutor] Stumped Again In-Reply-To: References: Message-ID: <4CCCC482.9060506@pearwood.info> Terry Green wrote: > Am running this Script and cannot figure out how to close my files, > > Keep getting msg: Attribute Error: '_csv.writer' object has no attribute > 'close' > > Why? Lesson one: we're not mind readers. To be able to give you useful advise, we need to see the ACTUAL error and not a summary taken out of context. Otherwise we're left with making sarcastic replies like "because it doesn't have an attribute called 'close'", which makes us feel good but doesn't help you. Python prints a traceback showing the exact error, including the full traceback of where it happens. Please COPY AND PASTE the FULL traceback. Do NOT summarize it, re-type it from memory, describe it in your own words, or otherwise change it in any way. The only exception is if the traceback is *huge*, you should say so, and only show the last few entries. If we need to see the rest, we'll tell you. Lesson two: we rarely need to, and never want to, see your entire script. Best practice is for you to simplify the problem to the smallest piece of code that still fails in the same way. This has three advantages: (1) By working through the problem yourself, 7 out of 10 times you'll work out what the problem is yourself. You will have learned a valuable problem-solving skill. (2) By making the problem small, rather than big, you improve the chances that others will have the time and motivation to solve it for you. (3) You make our life easier, which means we will be kindly disposed to you and be more likely to help. In this case, I think I can solve the problem very easily, using the interactive interpreter: >>> import csv >>> help(csv.writer.close) Traceback (most recent call last): File "", line 1, in AttributeError: 'builtin_function_or_method' object has no attribute 'close' csv.writer doesn't have a close method, just because that's how it is. There's nothing to close. What you need to close is the file object you pass to the csv.writer. import csv fp = open('c:/users/terry/downloads/tup1012k/tup1012.csv', 'w') output = csv.writer(fp) # do stuff with output # ... # then close the underlying file object, not the writer. fp.close() -- Steven From kb1pkl at aim.com Sun Oct 31 03:08:50 2010 From: kb1pkl at aim.com (Corey Richardson) Date: Sat, 30 Oct 2010 22:08:50 -0400 Subject: [Tutor] File transfer In-Reply-To: <4CCCC23A.4040906@gmail.com> References: <4CCCC23A.4040906@gmail.com> Message-ID: <4CCCCFB2.4090900@aim.com> If you can send a list, have the list [name, data] where name is the file name and data is the raw binary of the file, contained in a string. On 10/30/2010 9:11 PM, Chris King wrote: > Dear Tutors, > How would I send a file from one computer to another. I have > modules which can send simple objects, such as dictionaries with > simple objects in it. They can't send files thou. Please help. > > Sincerely, > Me > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sun Oct 31 10:13:34 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 31 Oct 2010 09:13:34 -0000 Subject: [Tutor] File transfer References: <4CCCC23A.4040906@gmail.com> Message-ID: "Chris King" wrote > How would I send a file from one computer to another. I have > modules which can send simple objects, such as dictionaries with > simple > objects in it. They can't send files thou. Please help. Can you use ftp? Thats the easiest way... Python has an ftp module. The only snag is the other end needs to be running an ftp server. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Sun Oct 31 10:17:44 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 31 Oct 2010 20:17:44 +1100 Subject: [Tutor] File transfer In-Reply-To: <4CCCCFB2.4090900@aim.com> References: <4CCCC23A.4040906@gmail.com> <4CCCCFB2.4090900@aim.com> Message-ID: <4CCD3438.9050509@pearwood.info> Corey Richardson wrote: > If you can send a list, have the list [name, data] where name is the > file name and data is the raw binary of the file, contained in a string. How do you send a list? -- Steven From guandalino at gmail.com Sun Oct 31 12:02:33 2010 From: guandalino at gmail.com (dave p. guandalino) Date: Sun, 31 Oct 2010 12:02:33 +0100 Subject: [Tutor] How to notify/handle an error? Message-ID: Which of the following ways is better to handle something wrong? Many thanks. # First: def is_valid_project(): # Do checks and valorize is_a_valid_project accordingly return is_a_valid_project # True / False # caller side if is_valid_project(): pass # do stuffs with valid project else: print "error" # Second solution: def is_valid_project(): # Do checks and valorize is_a_valid_project accordingly if not is_a_valid_project: raise NotAValidProject # caller side try: is_valid_project() pass # do stuffs with valid project except NotAValidProject: print "error" -------------- next part -------------- An HTML attachment was scrubbed... URL: From benderjacob44 at gmail.com Sun Oct 31 13:40:09 2010 From: benderjacob44 at gmail.com (Jacob Bender) Date: Sun, 31 Oct 2010 08:40:09 -0400 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: Thanks, and I'm looking it over... On 10/30/10, Tino Dai wrote: > On Sat, Oct 30, 2010 at 6:44 PM, Jacob Bender > wrote: > >> Dear Tutors, >> >> I was wondering how I could make an AI for creatures that run >> around, and try to survive. Like polyworld. The real problem is making >> the code connection to make them learn. Can you please help? >> >> Thanks >> > > Hi Jacob, > > That is a really general question. Usually people have a specific > question and/or some code > they are having problems with. I don't know your level of Python knowledge, > but you should check > out http://wiki.hacdc.org/index.php/NARG for more information. They are > doing stuff along the > lines of what you are asking for. > > HTH, > Tino > From benderjacob44 at gmail.com Sun Oct 31 13:52:25 2010 From: benderjacob44 at gmail.com (Jacob Bender) Date: Sun, 31 Oct 2010 08:52:25 -0400 Subject: [Tutor] (no subject) Message-ID: I have a few more ?'s. I was trying to make a program for ubuntu that does one function: if ctrl + Alt + "t" is pressed, to shut down the computer. There are only two problems. I want it to run in the background, so nobody knows it's there. I do know how to make it run at startup, so that isn't a problem. I found pygame, so I could say if ctrl +Alt + "t" is pressed to do something, but a black window would always have to be open, which leads to my first problem. I do know how a .pyw works, but it still rests in the Task Manager at the bottom of the screen. Thanks! From steve at pearwood.info Sun Oct 31 13:57:04 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 31 Oct 2010 23:57:04 +1100 Subject: [Tutor] How to notify/handle an error? In-Reply-To: References: Message-ID: <4CCD67A0.8020705@pearwood.info> dave p. guandalino wrote: > Which of the following ways is better to handle something wrong? Many > thanks. It depends on what you are trying to do. > # First: > def is_valid_project(): > # Do checks and valorize is_a_valid_project accordingly > return is_a_valid_project # True / False Do you mean "validate" rather than "valorize"? > # caller side > if is_valid_project(): > pass # do stuffs with valid project > else: > print "error" This is not the best approach. Never (well, *almost* never) use print for printing error messages. Here the caller should do one of these: # caller #1 if is_valid_project(): do_something_with_valid_project() else: do_something_else() # caller #2 if is_valid_project(): do_something_with_valid_project() else: # this is a fatal error, bail out raise SomeException() This general technique is called "Look before you leap" -- you're checking whether something is safe before attempting to do it. If it is safe, you continue, otherwise you take an alternative action (often just to raise an error and stop processing). > # Second solution: > def is_valid_project(): > # Do checks and valorize is_a_valid_project accordingly > if not is_a_valid_project: > raise NotAValidProject > > # caller side > try: > is_valid_project() > pass # do stuffs with valid project > except NotAValidProject: > print "error" This general technique is called "Easier to ask forgiveness than permission" -- just try to do what you want, and if it fails, catch the exception and do something else. Again, you should almost never catch an exception just to print an error message. Very rare exception to this rule: at the *very top level* of an application, you might catch the exception, log the technical details, display a user-friendly message to the user, and then exit the application. When you use raise, Python does nearly all of this for you. It won't log the failure, and the message is a technical traceback aimed at programmers rather than end-users, but it prints an error message and exits the application. Better would be: # caller #3 is_valid_project() # will raise an exception if there is a problem do_something_with_valid_project() # caller #4 try: is_valid_project() except NotAValidProject: do_something_else else: # no exception occurred do_something_with_valid_project() In Python, try blocks are very efficient, but actually catching an exception is moderately costly. So the general rule is, if you expect most cases to succeed, and failures to be rare, it is better to use a try...except block and the "Easier to ask forgiveness" strategy. If you expect failures to be common, it is better to "Look Before You Leap". -- Steven From steve at pearwood.info Sun Oct 31 14:11:46 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 01 Nov 2010 00:11:46 +1100 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <4CCD6B12.7010109@pearwood.info> Jacob Bender wrote: > I have a few more ?'s. Well, don't keep them a secret, tell us what they are! What are your questions? > I was trying to make a program for ubuntu that > does one function: if ctrl + Alt + "t" is pressed, to shut down the > computer. There are only two problems. I want it to run in the > background, so nobody knows it's there. I do know how to make it run > at startup, so that isn't a problem. I found pygame, so I could say if > ctrl +Alt + "t" is pressed to do something, but a black window would > always have to be open, which leads to my first problem. I do know how > a .pyw works, but it still rests in the Task Manager at the bottom of > the screen. Thanks! Well, I personally don't see the point, since your system almost certainly will have ctrl-alt-delete as shutdown keys. I'm glad you're so excited by your project that you felt you had to tell us about it. By the way, .pyw files are for Windows. They don't have any special powers in Ubuntu. You also might find that the best way to change the shutdown key combination is through /etc/inittab rather than writing a Python program to do it. -- Steven From steve at pearwood.info Sun Oct 31 14:16:14 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 01 Nov 2010 00:16:14 +1100 Subject: [Tutor] How to notify/handle an error? In-Reply-To: <4CCD67A0.8020705@pearwood.info> References: <4CCD67A0.8020705@pearwood.info> Message-ID: <4CCD6C1E.20106@pearwood.info> Steven D'Aprano wrote: > Never (well, *almost* never) use print for printing error messages. More information here: http://en.wikipedia.org/wiki/Error_hiding -- Steven From siren99 at yahoo.com Sun Oct 31 15:49:10 2010 From: siren99 at yahoo.com (Siren Saren) Date: Sun, 31 Oct 2010 07:49:10 -0700 (PDT) Subject: [Tutor] decorators (the "at" sign)? In-Reply-To: Message-ID: <611308.454.qm@web44707.mail.sp1.yahoo.com> Thanks for this very helpful post, Lie.? I find decorators quite interesting and am always looking for new ways to understand and use them.? Your trace function is fun, and I added it to my decorator library.? In response to your point about event-handling in GUIs: I haven't used pyqt that extensively yet, but it does have a decorator-based syntax for event-handling, which is more or less what you described.? Its concept of events may be slightly different -- signals and slots -- but it's just a layer of abstraction on 'mouse-right-button-click-event,' so hopefully not unwelcome.? I've been impressed with them so far.? I hate GUI programming, and even got into programming in part to avoid GUIs, but I find it easy enough to make something with pyqt (using decorators) that a customized GUI is a decent alternative to just making command line scripts, especially if your working with anything visual that you'd like to display. Cheers, Soren --- On Tue, 10/26/10, Lie Ryan wrote: From: Lie Ryan Subject: Re: [Tutor] decorators (the "at" sign)? To: tutor at python.org Date: Tuesday, October 26, 2010, 12:30 PM On 10/26/10 13:46, Alex Hall wrote: > Hi all, > Now that I am able to run the source code of an open source > application I hope to one day help develop, I am trying to understand > how it works. One thing I keep seeing is an at sign followed by a > word, usually (maybe always) immediately preceeding a function > definition. For example, and I know this exact code will not make much > sense, but it gives the idea: > class Bing(Messages, Updating, Dismissable): > >? @set_index >? def get_url(self, index=None): >???return self.storage[index]['Url'] > > What is the "@set_index" for? Specifically, what is the at sign doing? > Google was only able to provide me with a very vague idea of what is > going on, though it seems to crop up a lot in classmethod and > staticmethod calls (not sure about those either). I read PEP 318, but > it was not much help since I am coming at this having no idea what I > am looking at. The PEP did explain why I have never run into this > before, though - it is apparently specific to Python. I see this sort > of thing all over this source code so it seems like a good idea to get > exactly what it is for. TIA! The decorator syntax is really just a shorthand, from this: @decorator def func(arg): ? ? pass is equivalent to: def func(arg): ? ? pass func = decorator(func) basically, a decorator is a function that takes an function as a parameter/callable and (usually) returns another function/callable as return value. A slightly advanced usage of decorator: def trace(func): ? ? """ trace will print the func's name, the number of times ? ? ? ? func have been called, the arguments it's called with, ? ? ? ? and the return value of func whenever func is called. ? ? """ ? ? # define an inner function ? ? def _decorator(arg): ? ? ? ? print ("The %sth call of %s with arg: %s" % ? ? ? ? ? ? (_decorator._numcall, func.__name__, arg)) ? ? ? ? _decorator._numcall += 1 ? ? ? ? ret = func(arg) ? ? ? ? print "finished", func.__name__, "returns:", ret ? ? ? ? return ret ? ? # this is used to store the number of times ? ? # the decorated function is called ? ? _decorator._numcall = 0 ? ? # disable the decorator when debugging is enabled ? ? if __debug__: # or: return _decorator if __debug__ else func ? ? ? ? return _decorator ? ? else: ? ? ? ? return func @trace def twice(arg): ? ? return 2 * arg # the @trace makes it as if you do this: # twice = trace(twice) $ # let's start the program $ python decor.py The 0th call of twice() with arg: 30 finished twice() returns: 60 The 1th call of twice() with arg: 3 finished twice() returns: 6 The 2th call of twice() with arg: 4 finished twice() returns: 8 74 $ $ # now call it with debugging disabled: $ python -O decor.py 74 another nifty use of decorator is for event handling for a hypothetical GUI toolkit (I've yet to actually see a toolkit that uses this syntax yet): @button.on_click def shoot(button): ? ? ... @window.on_keypress('p') def pause(key): ? ? ... other built-in functions designed for decorator syntax is @property, @classmethod, and @instancemethod. Decorator can become extremely powerful when combined with the descriptor protocol (.__get__, .__set__). _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From g.nius.ck at gmail.com Sun Oct 31 17:06:18 2010 From: g.nius.ck at gmail.com (Chris King) Date: Sun, 31 Oct 2010 12:06:18 -0400 Subject: [Tutor] File transfer In-Reply-To: <4CCD9339.3030809@aim.com> References: <4CCCC23A.4040906@gmail.com> <4CCCCFB2.4090900@aim.com> <4CCD9097.8080300@gmail.com> <4CCD9339.3030809@aim.com> Message-ID: <4CCD93FA.6040501@gmail.com> On 10/31/2010 12:03 PM, Corey Richardson wrote: > > > On 10/31/2010 11:51 AM, Chris King wrote: >> On 10/30/2010 10:08 PM, Corey Richardson wrote: >>> If you can send a list, have the list [name, data] where name is the >>> file name and data is the raw binary of the file, contained in a string. >>> >>> On 10/30/2010 9:11 PM, Chris King wrote: >>>> Dear Tutors, >>>> How would I send a file from one computer to another. I have >>>> modules which can send simple objects, such as dictionaries with >>>> simple objects in it. They can't send files thou. Please help. >>>> >>>> Sincerely, >>>> Me >>>> >>>> >>>> _______________________________________________ >>>> Tutor maillist -Tutor at python.org >>>> To unsubscribe or change subscription options: >>>> http://mail.python.org/mailman/listinfo/tutor >> how do I get raw binary from a file, or preferably a folder? > In order to send a folder, you would have to zip it up using the > zipfile module.http://docs.python.org/library/zipfile.html > > To read from a file, you open it, and then read() it into a string > like this: > for line in file: > string += string + file.readline() > > That is the basic concept, but it should carry you far. I don't think readline will work an image. How do you get raw binary from a zip? Also make sure you do reply to the tutor list too, not just me. -------------- next part -------------- An HTML attachment was scrubbed... URL: From kb1pkl at aim.com Sun Oct 31 17:10:15 2010 From: kb1pkl at aim.com (Corey Richardson) Date: Sun, 31 Oct 2010 12:10:15 -0400 Subject: [Tutor] File transfer In-Reply-To: <4CCD93FA.6040501@gmail.com> References: <4CCCC23A.4040906@gmail.com> <4CCCCFB2.4090900@aim.com> <4CCD9097.8080300@gmail.com> <4CCD9339.3030809@aim.com> <4CCD93FA.6040501@gmail.com> Message-ID: <4CCD94E7.3030500@aim.com> On 10/31/2010 12:06 PM, Chris King wrote: > On 10/31/2010 12:03 PM, Corey Richardson wrote: >> >> >> On 10/31/2010 11:51 AM, Chris King wrote: >>> On 10/30/2010 10:08 PM, Corey Richardson wrote: >>>> If you can send a list, have the list [name, data] where name is >>>> the file name and data is the raw binary of the file, contained in >>>> a string. >>>> >>>> On 10/30/2010 9:11 PM, Chris King wrote: >>>>> Dear Tutors, >>>>> How would I send a file from one computer to another. I have >>>>> modules which can send simple objects, such as dictionaries with >>>>> simple objects in it. They can't send files thou. Please help. >>>>> >>>>> Sincerely, >>>>> Me >>>>> >>>>> >>>>> _______________________________________________ >>>>> Tutor maillist -Tutor at python.org >>>>> To unsubscribe or change subscription options: >>>>> http://mail.python.org/mailman/listinfo/tutor >>> how do I get raw binary from a file, or preferably a folder? >> In order to send a folder, you would have to zip it up using the >> zipfile module.http://docs.python.org/library/zipfile.html >> >> To read from a file, you open it, and then read() it into a string >> like this: >> for line in file: >> string += string + file.readline() >> >> That is the basic concept, but it should carry you far. > I don't think readline will work an image. How do you get raw binary > from a zip? Also make sure you do reply to the tutor list too, not > just me. If you want me to reply to the whole list, try sending the message to the whole list. Readlines will not work for an image. You need to open the file with the "rb" flag, why don't you read more into file I/O? http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sun Oct 31 17:52:01 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 31 Oct 2010 16:52:01 -0000 Subject: [Tutor] How to notify/handle an error? References: Message-ID: "dave p. guandalino" wrote > Which of the following ways is better to handle something wrong? Its not really a questin of "better" but a question of more idiomatic. > def is_valid_project(): > # Do checks and valorize is_a_valid_project accordingly > return is_a_valid_project # True / False This is a good return value given the functions name - it suggests it is a predicate so should return a boolean result. > def is_valid_project(): > # Do checks and valorize is_a_valid_project accordingly > if not is_a_valid_project: > raise NotAValidProject This is not showing what the return value is but one assumes it is True if the project is valid, but raises an exception if not valid. That mixed response is not good for a predicate. But if you renamed the function to validate_project(p) that returns None if OK and raises an exception for an invalid project that might be considered acceptable.That would resuult in code like: try: validate_project(p) pass # do stuff with project p except InvalidProjectError: HandleError() But a lot depends on what the tests look like. True exception based error handling would simply assume the project was valid, proceed to process it and only raise an exception if the processing failed. This probably obviates the need for the check function entirely... So... > try: > is_valid_project() > pass # do stuffs with valid project > except NotAValidProject: > print "error" becomes try: pass # do stuffs with valid project except : HandleError() HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From ahmedn82 at hotmail.com Sat Oct 30 20:37:26 2010 From: ahmedn82 at hotmail.com (Ahmed AL-Masri) Date: Sun, 31 Oct 2010 02:37:26 +0800 Subject: [Tutor] gauge of the fram Message-ID: Hi all, sorry for long message but just read the red highlighted lines I am facing a problem with gauge update. I want to return a value from another script file but the problem is this value locate inside a loop. example : the mean running script : m=0 # this function to return any value from different script def return_values(value): global m m= value #print m return value def create(parent): return ANN(parent) [wxID_ANN, wxID_ANNBUTTON1, wxID_ANNFILEBROWSEBUTTON1, ... so on [wxID_ANNMENUFILEEXIT, wxID_ANNMENUFILEOPEN, wxID_ANNMENUFILESAVE, ] = [wx.NewId() for _init_coll_menuFile_Items in range(3)] [wxID_ANNMENUHELPABOUT] = [wx.NewId() for _init_coll_menuHelp_Items in range(1)] class ANN(wx.Frame): def _init_coll_menuBar1_Menus(self, parent): # generated method, don't edit parent.Append(menu=self.menuFile, title='File') parent.Append(menu=self.menuHelp, title='Help') def _init_coll_menuHelp_Items(self, parent): # generated method, don't edit def _init_ctrls(self, prnt): # generated method, don't edit wx.Frame.__init__(self, id=wxID_ANN, name='ANN', parent=prnt, pos=wx.Point(386, 130), size=wx.Size(715, 565), style=wx.DEFAULT_FRAME_STYLE, title='Artificial Neural Network') self._init_utils() self.SetClientSize(wx.Size(699, 527)) self.SetMenuBar(self.menuBar1) self.panel1 = wx.Panel(id=wxID_ANNPANEL1, name='panel1', parent=self, pos=wx.Point(0, 0), size=wx.Size(699, 484), style=wx.TAB_TRAVERSAL) self.gauge1 = wx.Gauge(id=wxID_ANNGAUGE1, name='gauge1', parent=self.panel2, pos=wx.Point(200, 112), range=100, size=wx.Size(100, 28), style=wx.GA_HORIZONTAL) self.gauge1.SetValue(0) self.gauge1.SetLabel('') and so on ... def OnNormtrainingButton(self, event): self.data= self.fileBrowseButton1.GetValue() target= self.fileBrowseButton2.GetValue() import normalization self.gauge1.Value=0 no= normalization.Norm() no.read_norm_data(data) #self.gauge1.Value= print m the second script is: normalization.py def read_norm_data(self, inputsData): self.pat=[] tragauge=0;totalrange= float(len(open(inputsData).readlines())) #for the gauge process while True: # read the input data line = f.readline() if len(line)==0: break inputs=line.split() self.inputs=map(float,inputs) #print self.inputs # for the training data norm gauge process self.traininggauge = ((tragauge+1)/totalrange)*100 tragauge=tragauge+1 ann.return_values(self.traininggauge) ############# HERE is the gauge from the last line I need this value to go in side the mean frame and update the gauge. I can return the value into the mean file but couldn?t make it for the gauge cuz the gauge is inside the ANN class which I couldn?t go inside it. I am trying to do that by make it as a global varible and update but it?s keep give me the same initial value m=0. may be I can get it by sending the value and the gauge into a new script and update the gauge but I think this is not a proper way. Any idea! Best regards, -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sun Oct 31 23:42:45 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 01 Nov 2010 09:42:45 +1100 Subject: [Tutor] File transfer In-Reply-To: <4CCD93FA.6040501@gmail.com> References: <4CCCC23A.4040906@gmail.com> <4CCCCFB2.4090900@aim.com> <4CCD9097.8080300@gmail.com> <4CCD9339.3030809@aim.com> <4CCD93FA.6040501@gmail.com> Message-ID: <4CCDF0E5.4050701@pearwood.info> Chris King quoted Corey Richardson: > On 10/31/2010 12:03 PM, Corey Richardson wrote: [...] >> To read from a file, you open it, and then read() it into a string >> like this: >> for line in file: >> string += string + file.readline() Aiieeee! Worst way to read from a file *EVAR*!!! Seriously. Don't do this. This is just *wrong*. (1) You're mixing file iteration (which already reads line by line) with readline(), which would end in every second line going missing. Fortunately Python doesn't let you do this: >>> for line in file: ... print file.readline() ... Traceback (most recent call last): File "", line 2, in ValueError: Mixing iteration and read methods would lose data (2) Even if Python let you do it, it would be slow. Never write any loop with repeated string concatenation: result = '' for string in list_of_strings: result += string # No! Never do this! BAD BAD BAD!!! This can, depending on the version of Python, the operating system, and various other factors, end up being thousands of times slower than the alternative: result = ''.join(list_of_strings) I'm not exaggerating. There was a bug reported in the Python HTTP library about six(?) months ago where Python was taking half an hour to read a file that Internet Explorer or wget could read in under a second. You might be lucky and never notice the poor performance, but one of your users will. This is a Shlemiel the Painter algorithm: http://www.joelonsoftware.com/articles/fog0000000319.html Under some circumstances, *some* versions of Python can correct for the poor performance and optimize it to run quickly, but not all versions, and even the ones that do sometimes run into operating system dependent problems that lead to terrible performance. Don't write Shlemiel the Painter code. The right way to read chunks of data from a file is with the read method: fp = open("filename", "rb") # open in binary mode data = fp.read() # read the whole file fp.close() If the file is large, and you want to read it in small chunks, read() takes a number of optional arguments including how many bytes to read: fp.read(64) # read 64 bytes If you want to read text files in lines, you can use the readline() method, which reads up to and including the next end of line; or readlines() which returns a list of each line; or just iterate over the file to get Chris King went on to ask: > I don't think readline will work an image. How do you get raw binary > from a zip? Also make sure you do reply to the tutor list too, not just me. readline() works fine on binary files, including images, but it won't be useful because binary files aren't split into lines. readline() reads until end-of-line, which varies according to the operating system you are running, but often is \n. A binary file may or may not contain any end-of-line characters. If it does, then readline() will read up to the next EOL perfectly fine: f.readline() => '\x23\x01\0=#%\xff\n' and if it doesn't, readline() will happily read the entire file all the way to the end: '\x23\x01\0=#%\xff3m.\x02\0\xa0\0\0\0+)\0\x03c!