Re: [Tutor] odd behavior when renaming a file
On 09/05/12 20:26, Joel Goldstick wrote: import os def pre_process(): if os.path.isfile('revelex.csv'): os.rename('revelex.csv', 'revelex.tmp') print Renamed ok else: print Exiting, no revelex.csv file available exit() out_file = open('revelex.csv', 'w') # etc. When I run the code above it works file if run from the file. But when I import it and run it from another file it renames the file but then prints Exiting, no revelex.csv file available I don;t know the reason but are you sure you want to open the file that you have just renamed? def pre_process(): if os.path.isfile('revelex.csv'): os.rename('revelex.csv', 'revelex.tmp') ... out_file = open('revelex.csv', 'w') # etc. I would expect the open() to fail... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] odd behavior when renaming a file
On Wed, May 9, 2012 at 5:21 PM, Peter Otten __pete...@web.de wrote: Joel Goldstick wrote: import os def pre_process(): if os.path.isfile('revelex.csv'): os.rename('revelex.csv', 'revelex.tmp') print Renamed ok else: print Exiting, no revelex.csv file available exit() out_file = open('revelex.csv', 'w') # etc. if __name__ == '__main__': pre_process() When I run the code above it works file if run from the file. But when I import it and run it from another file it renames the file but then prints Exiting, no revelex.csv file available Add print os.getcwd() to your code, you are probably in the wrong directory. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Thanks for the hint. I am in the right directory, but I failed to notice that I renamed the file before I entered my function. Funny how a night of sleep can make dumb mistakes pop out. -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] odd behavior when renaming a file
On 05/10/2012 12:56 PM, Alan Gauld wrote: On 09/05/12 20:26, Joel Goldstick wrote: import os def pre_process(): if os.path.isfile('revelex.csv'): os.rename('revelex.csv', 'revelex.tmp') print Renamed ok else: print Exiting, no revelex.csv file available exit() out_file = open('revelex.csv', 'w') # etc. When I run the code above it works file if run from the file. But when I import it and run it from another file it renames the file but then prints Exiting, no revelex.csv file available I don;t know the reason but are you sure you want to open the file that you have just renamed? def pre_process(): if os.path.isfile('revelex.csv'): os.rename('revelex.csv', 'revelex.tmp') ... out_file = open('revelex.csv', 'w') # etc. I would expect the open() to fail... But he's opening it for WRITE, so it gets created just fine. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] odd behavior when renaming a file
On Thu, May 10, 2012 at 4:18 PM, Dave Angel d...@davea.name wrote: On 05/10/2012 12:56 PM, Alan Gauld wrote: On 09/05/12 20:26, Joel Goldstick wrote: import os def pre_process(): if os.path.isfile('revelex.csv'): os.rename('revelex.csv', 'revelex.tmp') print Renamed ok else: print Exiting, no revelex.csv file available exit() out_file = open('revelex.csv', 'w') # etc. When I run the code above it works file if run from the file. But when I import it and run it from another file it renames the file but then prints Exiting, no revelex.csv file available I don;t know the reason but are you sure you want to open the file that you have just renamed? def pre_process(): if os.path.isfile('revelex.csv'): os.rename('revelex.csv', 'revelex.tmp') ... out_file = open('revelex.csv', 'w') # etc. I would expect the open() to fail... But he's opening it for WRITE, so it gets created just fine. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor I have to process a csv file from a business partner. Oddly (?) they don't quote text fields, and the Title field sometimes contains commas. So I wrote some code to count the commas in each line and if there were too many, I removed the extras and wrote the cleaned up file to the original filename for the rest of what I have to with that data -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] odd behavior when renaming a file
On 10/05/12 21:18, Dave Angel wrote: out_file = open('revelex.csv', 'w') # etc. I would expect the open() to fail... But he's opening it for WRITE, so it gets created just fine. Ah yes, I didn't spot that. :-) Too busy looking for a possible cause of a missing file message... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] odd behavior when renaming a file
I have to process a csv file from a business partner. Oddly (?) they don't quote text fields, and the Title field sometimes contains commas. So I wrote some code to count the commas in each line and if there were too many, I removed the extras and wrote the cleaned up file to the original filename for the rest of what I have to with that data That is terrible (of them). How do you determine which comma is the extra? Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] odd behavior when renaming a file
On Thu, May 10, 2012 at 5:05 PM, Prasad, Ramit ramit.pra...@jpmorgan.com wrote: I have to process a csv file from a business partner. Oddly (?) they don't quote text fields, and the Title field sometimes contains commas. So I wrote some code to count the commas in each line and if there were too many, I removed the extras and wrote the cleaned up file to the original filename for the rest of what I have to with that data That is terrible (of them). How do you determine which comma is the extra? Yes, it is kinda disheartening, but I don't know if they distribute this file to others, and changing it might have ramifications. I take the line and split it on commas. Then get the length of the list. If its greater than 8, I append the item in the list that is after the Title field (I don't have the code with me -- i think its the 6th item) to the field that is the beginning of the title. Then I remove (pop) that field. Lather, rinse, repeat until there are 8 elements in the list. Then I join the list back with commas and write to the output file. Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] odd behavior when renaming a file
Hi, On 9 May 2012 20:26, Joel Goldstick joel.goldst...@gmail.com wrote: import os def pre_process(): if os.path.isfile('revelex.csv'): os.rename('revelex.csv', 'revelex.tmp') print Renamed ok else: print Exiting, no revelex.csv file available exit() out_file = open('revelex.csv', 'w') # etc. if __name__ == '__main__': pre_process() When I run the code above it works file if run from the file. But when I import it and run it from another file it renames the file but then prints Exiting, no revelex.csv file available Can you post where/how you call this from another file? Anyway, it sounds like the pre_process() routine is being called twice, somehow. On the first call the file is renamed. Then on the second call, of course the file is not there anymore (as it's been renamed) and thus it prints the Exiting message. Best, Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] odd behavior when renaming a file
Joel Goldstick wrote: import os def pre_process(): if os.path.isfile('revelex.csv'): os.rename('revelex.csv', 'revelex.tmp') print Renamed ok else: print Exiting, no revelex.csv file available exit() out_file = open('revelex.csv', 'w') # etc. if __name__ == '__main__': pre_process() When I run the code above it works file if run from the file. But when I import it and run it from another file it renames the file but then prints Exiting, no revelex.csv file available Add print os.getcwd() to your code, you are probably in the wrong directory. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Odd behavior with eval, list comps, and name lookup
John wrote: I noticed some odd behavior relating to eval(). First, a baseline case for behavior: def test(): ... x = 5 ... return [a for a in range(10) if a == x] ... test() [5] So far so good. Now let's try eval: c = compile('[a for a in range(10) if a == x]', '', 'single') eval(c, globals(), {'x': 5}) Traceback (most recent call last): File stdin, line 1, in module File , line 1, in module File , line 1, in listcomp NameError: global name 'x' is not defined Looks like 'x' is searched for only among the globals, bypassing the locals. The same behavior is seen between exec and eval, with and without compile, and between 3.1.3 and 3.2rc2. Given simpler code without a list comp, the locals are used just fine: c = compile('a=5; a==x', '', 'single') eval(c, {}, {'x': 5}) True Could anyone help me understand these scoping rules? Thanks. Except for class definitions the compiler statically determines whether a variable is global or local (or a closure). List comprehensions and generator expressions are realized as functions; therefore they have their own local scope that you cannot provide via eval/exec. You could argue that in the following example exec(x = 2; print([a for a in range(3) if a == x]), {}, {}) Traceback (most recent call last): File stdin, line 1, in module File string, line 1, in module File string, line 1, in listcomp NameError: global name 'x' is not defined the assignment x = 2 should give the compiler a clue that x is supposed to be a local name, just like in def f(): x = 2 print([a for a in range(3) if a == x]) My *guess* is that this is an implementation restriction rather than a conscious decision. It works for the common case of module-level code because there local and global namespace are identical: exec(x = 2; print([a for a in range(3) if a == x]), {}) [2] Peter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Odd result from function call
On Fri, Jan 7, 2011 at 11:38 AM, Ben Ganzfried ben.ganzfr...@gmail.comwrote: When I call one of my functions from the shell (ie compare(10, 5)) it produces the correct output. However, when I run the program after calling the method later in the script, the result is bizarre. I'm curious why the wrong result is printed. Here is an example: def compare(x,y): if x y: print (x, is less than , y) print(x is , x, y is , y) elif x y: print(x, is greater than , y) else: print(x, and , y, are equal.) x = input(First x is: ) y = input(First y is: ) print(x is , x) print(y is , y) compare(x,y) a = input(Second x is: ) b = input(Second y is: ) print(x is , a) print(y is , b) compare(a,b) c = input(Third x is: ) d = input(Third y is: ) print(x is , c) print(y is , d) compare(c,d) Sample (and incorrect) output w/ 10, 5: First x is: 10 First y is: 5 x is 10 y is 5 10 is less than 5 x is 10 y is 5 Second x is: When I do simply compare(10, 5) from the shell, I get the correct output (ie 10 is greater than 5). I had thought I had narrowed the problem down to the fact that when I run the script only the first digit is counted-- however, it seems as if only the first digit is counted (ie anything starting w/ a 9 will be greater than anything starting with a 1 (even if the numbers are 9 and 1324234)), and THEN, the second digit is counted (such that 89 is correctly identified at 81). Anyway I'm wondering: 1) Why does the script run correctly when I simply call the function from the shell but not when I try to call the function from within the script? 2) What is actually going on such that only the first digit is being evaluated? That is, the interpreter knows that x is 10 and y is 5-- and yet, for some reason the 5 is being tested against the 1 and since 5 is bigger than 1, it concludes that 5 is greater than 10. thanks! Ben ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Ben It would appear that you are comparing string in your script and not integers a= int(input(...)) -- Vince Spicer Developer ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] odd bug
try adding: print current block:, one_char_box print zeros: , zero_count to the lines just below your if statement that you think is called only once. This way you know how many times its called and you might be able to find the error. Chris Henk Allison Transmission phone: 317.242.2569 fax: 317.242.3469 e-mail: [EMAIL PROTECTED] elis aeris [EMAIL PROTECTED] Sent by: [EMAIL PROTECTED] 07/20/2007 02:36 AM To Luke Paireepinart [EMAIL PROTECTED] cc python tutor tutor@python.org Subject [Tutor] odd bug I ran the code attached at the end of the email, it' supposed to out put a string of characters, yet I got only this ### 2.4.3.3.8.5. definition check: 3 ### now, it's proper output, however, this part got run only once, when it's supposed to run multiple times to produce more portions like the one above, i don't know why this part got run only once: when that happens if box_v == 0: zero_count = zero_count + 1 if zero_count == 2: zero_count = 0 if one_char_box in loc_window_loc_definition: print one_char_box box_string = box_string + str(loc_window_loc_definition [one_char_box]) print definition check: print box_string ## for b in range(1,50,1): ## Next point to check x = radar_loc_window_xx + b y = radar_loc_window_yy ## omit 0 when there are 2 zeros, and check def when that happens if box_v == 0: zero_count = zero_count + 1 if zero_count == 2: zero_count = 0 if one_char_box in loc_window_loc_definition: print one_char_box box_string = box_string + str(loc_window_loc_definition [one_char_box]) print definition check: print box_string else: one_char_box = one_char_box + str(box_v) + . box_v = 0 for a in range(0,10,1): r, g, b = pixels[1030*(y-a) + x] if g r and g b: box_v = box_v + 1 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] odd
for x in range(5,10): print x and OP was 5 6 7 8 9 why is that? shouldn't it print t 6 7 8 9 10? no. the (well, one) syntax for range() is (start, stop) where it counts starting from 'start' up to but not including 'stop'. if you're familiar with C/C++ (or PHP or Java), it's similar to the counting loop, for (int i=5; i 10; i++), which counts 5, 6, 7, 8, 9. hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Core Python Programming, Prentice Hall, (c)2007,2001 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] odd
* elis aeris [EMAIL PROTECTED] [2007-07-19 08:51]: I ran this for x in range(5,10): print x and OP was 5 6 7 8 9 why is that? shouldn't it print 5 6 7 8 9 10? That is the expected behaviour, per the documentation: http://docs.python.org/lib/built-in-funcs.html#l2h-58 -- David Rock [EMAIL PROTECTED] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] odd
from Guido's tutorial: The given end point is never part of the generated list; range(10) generates a list of 10 values, the legal indices for items of a sequence of length 10. It is possible to let the range start at another number, or to specify a different increment (even negative; sometimes this is called the `step') On 7/19/07, elis aeris [EMAIL PROTECTED] wrote: I ran this for x in range(5,10): print x and OP was 5 6 7 8 9 why is that? shouldn't it print t 6 7 8 9 10? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] odd behavior within __init__
On Apr 14, 2005, at 12:58, Orri Ganel wrote: a = Node(1) b = Node(a) 12932600 12932600 1 id(b) 12960632 Any ideas on why this happens, or suggestions as to how to implement the behavior I'm looking for (in which b and a would refer to the same object, have the same id, etc.), would be greatly appreciated. Well, if you want b and a to refer to the same object, just use b = a. Everything is a reference in Python, make use of this feature. (at that point, I expect Alan to drop in and explain why what I said is not entirely accurate, but good enough ;) ) -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] odd behavior within __init__
Orri Ganel wrote: Hello all, As part of a project i'm doing (mostly for the fun of it), I have a class which creates a sort of wrapper around any object to make it suitable for use in a custom container. However, if the class receives an already wrapped object, I want it to just return the object (same id and everything as the original). Now, the following seems to work in the __init__ method (due to output), but then it disappears as soon as the __init__ method is left: class Node: ... def __init__(self, cargo=None, prev=None, next=None, nod=False): x.__init__(...) initializes x; see x.__class__.__doc__ for signature if not isinstance(cargo, Node) or nod: self.cargo = cargo self.prev = prev self.next = next else: self = cargo print id(self), id(cargo) print self.cargo a = Node(1) b = Node(a) 12932600 12932600 1 id(b) 12960632 Any ideas on why this happens, or suggestions as to how to implement the behavior I'm looking for (in which b and a would refer to the same object, have the same id, etc.), would be greatly appreciated. Thanks in advance, Orri Orri, Maybe you could use a factory. It would allow you to simplify your Node class, and encapsulate the instantiation behavior you want outside the class. class Node(object): def __init__(self,cargo=None,prev=None,next=None): self.cargo = cargo self.prev = prev self.next = next def factory(cargo=None,prev=None,next=None): if isinstance(cargo,Node): return cargo else: return Node(cargo,prev,next) n1 = factory(1) print id(n1) n2 = factory(n1) print id(n2) Good luck, Rich ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] odd behavior within __init__
On 4/14/05, Rich Krauter [EMAIL PROTECTED] wrote: Maybe you could use a factory. It would allow you to simplify your Node class, and encapsulate the instantiation behavior you want outside the class. Thanks for the suggestion; I think that's what I'll do. On 4/14/05, Max Noel [EMAIL PROTECTED] wrote: Well, if you want b and a to refer to the same object, just use b = a. If you'll look at my code, you'll see that I *did* try that approach, and it did not persist past __init__ for some reason: class Node: ... def __init__(self, cargo=None, prev=None, next=None, nod=False): x.__init__(...) initializes x; see x.__class__.__doc__ for signature if not isinstance(cargo, Node) or nod: self.cargo = cargo self.prev = prev self.next = next else: # self = cargo ## see? # print id(self), id(cargo) print self.cargo a = Node(1) b = Node(a) 12932600 12932600 1 id(b) 12960632 -- Email: singingxduck AT gmail DOT com AIM: singingxduck Programming Python for the fun of it. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Odd problem with variable substitution and command execution
Robert, Andrew wrote: Hi Everyone, I am trying to do an MQ inquiry but I am having mixed results. If I do the command direct via a print statement like the one below, it works, print 'Queue Description:\t' , q.inquire(CMQC.MQCA_Q_DESC) When I try to cycle through an array of command line supplied keys, it fails. while counter arg_array_size: arg1=valid_keys[sys.argv[counter]] arg2 = 'q.inquire(CMQC.'+valid_keys[sys.argv[counter]]+')' print arg1, ,arg2 counter = counter +1 You are confusing strings with executable code. What you are doing here is essentially print 'MQCA_Q_DESC' , 'q.inquire(CMQC.MQCA_Q_DESC)' Note the quotes around the second string - not very exciting. You could use eval() to get around this but there are better ways. You just need to get an attribute of CMQC by name, then call q.inquire() with that attribute. Something like this should work: while counter arg_array_size: arg1=valid_keys[sys.argv[counter]] # if arg1 is the string MQCA_Q_DESC then the next line is the same # as saying queryParam = CMQC.MQCA_Q_DESC queryParam = getattr(CMQC, arg1) # Now we can use queryParam directly arg2 = q.inquire(queryParam) print arg1, ,arg2 counter = counter +1 I have no way to test this so write back if you have trouble with it. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor