Re: [Tutor] Multiple file open

2010-08-20 Thread nitin chandra
Hello All, I am getting the "Invalid Syntax" error. I am on Python 2.6. Thanks In Advance Nitin import sys,os, fileinput FileA = raw_input('Enter CSV file with lists of Files:') try: fp6 = open(FileA,'r') except IOError: ## I am get

[Tutor] Elementtree and pretty printing in Python 2.7

2010-08-20 Thread Knacktus
Hi guys, I'm using Python 2.7 and the ElementTree standard-lib to write some xml. My output xml has no line breaks! So, it looks like that: instead of something like this: I'm aware of lxml which seems to have a pretty print option, but I would prefer to use the standard-lib Element

Re: [Tutor] Multiple file open

2010-08-20 Thread Steven D'Aprano
On Fri, 20 Aug 2010 05:28:42 pm nitin chandra wrote: > try: >fp6 = open(FileA,'r') >except IOError: You need to outdent the except line: try: fp6 = open(FileA,'r') except IOError: sys.exit('Could not open file: %s' % FileA) -- Steven D'Aprano

Re: [Tutor] Multiple file open

2010-08-20 Thread Alan Gauld
"nitin chandra" wrote FileA = raw_input('Enter CSV file with lists of Files:') try: fp6 = open(FileA,'r') except IOError: ## I am getting the following error at this line:- ## ##File "mer5Pr2.py", line 7 ##except IOError: ##^ ##SyntaxError: invalid syntax The

Re: [Tutor] List comprehension for dicts?

2010-08-20 Thread Alan Gauld
"Steven D'Aprano" wrote the purpose). No matter how fast you can perform a loop, it's always faster to avoid it altogether, so this: seq = xrange(1000) result = [] for i in seq: if i >= 10: break result.append(i%2) will be much faster than this: seq = xrange(1000) result = [i%

Re: [Tutor] flow problem with a exercise

2010-08-20 Thread Alan Gauld
"Roelof Wobben" wrote Others have pointed out the lack of indentation. I'll point out some shortcuts you can use... def is_even(argument): remainder= argument%2 if remainder == 0 : return True else : return False You can abbreviate this to just def is_even(arg): r

Re: [Tutor] Multiple file open

2010-08-20 Thread nitin chandra
Thank you, Alan and Steven. Now i am facing some thing 'different' This is even when i have not done a carriage return to create line "66". This is after i have done fp1.close() fp2.close() fp3.close() ** File "mer5Pr2.py", line 66

[Tutor] Done -- Elementtree and pretty printing in Python 2.7

2010-08-20 Thread Knacktus
Hi guys, just found "hidden" in the "Schema change in Etree"-Thread the answer to my formatting question: Quotation: " ElementTree doesn't have a way of formatting (pretty printing) XML files, so there can't be that many newline characters in the structure (they may be in the occur, though!)

Re: [Tutor] List comprehension for dicts?

2010-08-20 Thread Steven D'Aprano
On Fri, 20 Aug 2010 06:10:59 pm Alan Gauld wrote: > "Steven D'Aprano" wrote > > > the purpose). No matter how fast you can perform a loop, it's > > always faster to avoid it altogether, so this: > > > > seq = xrange(1000) > > result = [] > > for i in seq: > >if i >= 10: break > >result

Re: [Tutor] Multiple file open

2010-08-20 Thread Steven D'Aprano
On Fri, 20 Aug 2010 06:27:01 pm nitin chandra wrote: > Thank you, Alan and Steven. > > Now i am facing some thing 'different' > > This is even when i have not done a carriage return to create line > "66". This is after i have done > fp1.close() > fp2.close() > fp3.close() >

Re: [Tutor] Multiple file open

2010-08-20 Thread nitin chandra
import sys,os, fileinput FileA = raw_input('Enter CSV file with lists of Files:') try: fp6 = open(FileA,'r') except IOError: sys.exit('Could not open file: %s' % FileA) while True: lines = fp6.readline().split(",") file11 = lines[0] file12 = lines[1] file3 = lines[2] #file11 =

[Tutor] design of Point class

2010-08-20 Thread Gregory, Matthew
Hi all, I often struggle with object design and inheritance. I'd like opinions on how best to design a Point class to be used in multiple circumstances. I typically deal with geographic (either 2D or 3D) data, yet there are occasions when I need n-dimensional points as well. My thought was to

Re: [Tutor] List comprehension for dicts?

2010-08-20 Thread bob gailer
On 8/20/2010 5:44 AM, Steven D'Aprano wrote: On Fri, 20 Aug 2010 06:10:59 pm Alan Gauld wrote: "Steven D'Aprano" wrote the purpose). No matter how fast you can perform a loop, it's always faster to avoid it altogether, so this: seq = xrange(1000) result = [] for i in seq: if i>= 10:

Re: [Tutor] design of Point class

2010-08-20 Thread Alex Hall
On 8/20/10, Gregory, Matthew wrote: > Hi all, > > I often struggle with object design and inheritance. I'd like opinions on > how best to design a Point class to be used in multiple circumstances. > > I typically deal with geographic (either 2D or 3D) data, yet there are > occasions when I need n

Re: [Tutor] design of Point class

2010-08-20 Thread bob gailer
On 8/20/2010 11:45 AM, Gregory, Matthew wrote: Hi all, I often struggle with object design and inheritance. I'd like opinions on how best to design a Point class to be used in multiple circumstances. I typically deal with geographic (either 2D or 3D) data, yet there are occasions when I nee

Re: [Tutor] design of Point class

2010-08-20 Thread Wayne Werner
On Fri, Aug 20, 2010 at 10:45 AM, Gregory, Matthew < matt.greg...@oregonstate.edu> wrote: > Hi all, > > I often struggle with object design and inheritance. I'd like opinions on > how best to design a Point class to be used in multiple circumstances. > > I typically deal with geographic (either 2

Re: [Tutor] List comprehension for dicts?

2010-08-20 Thread Nitin Das
result = [i%2 for i in itertools.takewhile(lamda x:i< 10, seq)] is a good approach but it is taking little more time than the for loop over iterator. def takewhile(predicate, iterable): # takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4 for x in iterable: if predicate(x):

Re: [Tutor] design of Point class

2010-08-20 Thread Gregory, Matthew
Wayne Werner wrote: > class Point2D(PointND): >     def __init__(self, x = 0, y = 0): >         super(Point2D, self).__init__([x,y]) >         self.x = 0 >         self.y = 0 > > though you wouldn't be able to directly modify the values, or you'll > lose the distance function. You'd have to creat

Re: [Tutor] flow problem with a exercise

2010-08-20 Thread Roelof Wobben
Oke, I don''t understand it complety. return not arg%2 Why use not here ? I think that arg%2 is True not makes it false. Another question. How can I round outcome of a calculation. round ( ( t-32)/1.8) does not work because I get a message that there are two arguments. Outcome

Re: [Tutor] flow problem with a exercise

2010-08-20 Thread Wayne Werner
On Fri, Aug 20, 2010 at 2:48 PM, Roelof Wobben wrote: > Oke, > > I don''t understand it complety. > > return not arg%2 > > Why use not here ? > > I think that arg%2 is True not makes it false. > What happens when you replace arg with a value? % is modulo division, so it just returns the remaind

Re: [Tutor] Multiple file open

2010-08-20 Thread Walter Prins
Hello Nitin, On 20/08/10 16:21, nitin chandra wrote: import sys,os, fileinput [...] while True: try: line1 = fp1.readline().split(",") line2 = fp2.readline().split(",") [...] fp3.write(str3) You're missing the completion for the try/finally block. Tr

Re: [Tutor] design of Point class

2010-08-20 Thread Hugo Arts
On Fri, Aug 20, 2010 at 12:55 PM, Gregory, Matthew wrote: > Wayne Werner wrote: >> class Point2D(PointND): >>     def __init__(self, x = 0, y = 0): >>         super(Point2D, self).__init__([x,y]) >>         self.x = 0 >>         self.y = 0 >> >> though you wouldn't be able to directly modify the v

Re: [Tutor] flow problem with a exercise

2010-08-20 Thread ALAN GAULD
Please use ReplyAll when responding to posts from the tutor list. > I don''t understand it complety. > return not arg%2 >> >> Why use not here ? >> >> I think that arg%2 is True not makes it false. > >For an even number arg % 2 will be 0 which Python considers to be False. So for a True res

[Tutor] Overloaded Brackets

2010-08-20 Thread Emile van Sebille
Hi All, I just had reason the write the following: itemCodes = [UPCxref['0'+xx[:10]][0] for xx in itemUPCs] ... and it occurred to me exactly how overloaded square brackets are. This is certainly not something you want to see as a beginning python programmer, but I thought it might be an inte

Re: [Tutor] design of Point class

2010-08-20 Thread bob gailer
On 8/20/2010 1:55 PM, Gregory, Matthew wrote: Wayne Werner wrote: class Point2D(PointND): def __init__(self, x = 0, y = 0): super(Point2D, self).__init__([x,y]) self.x = 0 self.y = 0 though you wouldn't be able to directly modify the values, or you'll lose the d

Re: [Tutor] design of Point class

2010-08-20 Thread bob gailer
On 8/20/2010 1:55 PM, Gregory, Matthew wrote: Wayne Werner wrote: class Point2D(PointND): def __init__(self, x = 0, y = 0): super(Point2D, self).__init__([x,y]) self.x = 0 self.y = 0 though you wouldn't be able to directly modify the values, or you'll lose the d

Re: [Tutor] design of Point class

2010-08-20 Thread Alan Gauld
"Gregory, Matthew" wrote I typically deal with geographic (either 2D or 3D) data, yet there are occasions when I need n-dimensional points as well. Thats OK. The rub to this is that in the n-dimensional case, it probably makes most sense to store the actual coordinates as a list whereas wi

Re: [Tutor] design of Point class

2010-08-20 Thread Steven D'Aprano
On Sat, 21 Aug 2010 01:45:18 am Gregory, Matthew wrote: > Hi all, > > I often struggle with object design and inheritance. I'd like > opinions on how best to design a Point class to be used in multiple > circumstances. > > I typically deal with geographic (either 2D or 3D) data, yet there > are oc

Re: [Tutor] design of Point class

2010-08-20 Thread Steven D'Aprano
On Sat, 21 Aug 2010 08:08:56 am Alan Gauld wrote: > Every time you change the interface of inherited methods you > create for yourself extra work in converting types to match the > superclass. But that is often easier than rewriting the methods > from scratch. Every time you change the interface o

Re: [Tutor] List comprehension for dicts?

2010-08-20 Thread Steven D'Aprano
On Sat, 21 Aug 2010 01:47:12 am bob gailer wrote: > > Well yes, but I pointed out that you *can* bail out early of > > for-loops, but not list comprehensions. The whole point is with a > > list comp, you're forced to iterate over the entire sequence, even > > if you know that you're done and would

Re: [Tutor] design of Point class

2010-08-20 Thread Steven D'Aprano
On Sat, 21 Aug 2010 11:50:40 am Steven D'Aprano wrote: > On Sat, 21 Aug 2010 08:08:56 am Alan Gauld wrote: > > Every time you change the interface of inherited methods you > > create for yourself extra work in converting types to match the > > superclass. But that is often easier than rewriting the

Re: [Tutor] Multiple file open

2010-08-20 Thread nitin chandra
Thank You Very Much :) Walter. It WOrked I did the "except" closing. Thanks Nitin On Sat, Aug 21, 2010 at 2:17 AM, Walter Prins wrote: > > Hello Nitin, > > On 20/08/10 16:21, nitin chandra wrote: >> >> import sys,os, fileinput >> > > [...] >> >> while True: >>    try: >>        line1 =