Re: [Tutor] Doubts galore.
On Thu, Jun 10, 2010 at 09:43:34PM +0530, prasad rao wrote: > > It would probably be cleaner to use one-line > > conditional statements (sparingly) where they make > > sense on their own, but not to mix multi-line and > > single line styles in the same if-else structure. > > I am a newbie.I couldn't understand that comment. So occasionally it's cleaner and clearer to put the condition and statement on one line like if x>0: print x But often you need more than one line, so you make a block structure: if x>0: do_stuff() do_more_stuff() If you have multiple blocks in an if-elif-elif-...-else structure, don't go back and forth between one-line and multi-line blocks, be consistent, so instead of something like if x>0: do_one_thing() elif x<0: do_something_else(-x) something_entirely_different(x**2) elif x==0 and y>0: do_something_else(y) yet_another_function(y**3) else: raise ValueError('values for x and y not in allowed range') you should be consistent and write this as: if x>0: do_one_thing() elif x<0: do_something_else(-x) something_entirely_different(x**2) elif x==0 and y>0: do_something_else(y) yet_another_function(y**3) else: raise ValueError('values for x and y not in allowed range') -- Steve Willoughby| Using billion-dollar satellites st...@alchemy.com | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Doubts galore.
> Never NEVER compare for equality with None. > > What you want is: > if doc is None and data is None: > > Also, what is "sys.agv1"? Did you mean sys.argv[1]? yes > >elif doc: > > data=open(doc,'r').read() > > data_c= binascii.hexlify(data) > >else:data_c= binascii.hexlify(data) > >if doc: > > q=tempfile.TemporaryFile() > > q.write(data_c) > > os.rename(q,doc) > > return > >return data_c > It would probably be cleaner to use one-line > conditional statements (sparingly) where they make > sense on their own, but not to mix multi-line and > single line styles in the same if-else structure. I am a newbie.I couldn't understand that comment. > I'm not sure the logical flow through there > does what you think it does, though. > > cript(doc='./language.txt') > > Traceback (most recent call last): > > File "", line 1, in > > File "", line 10, in cript > > TypeError: coercing to Unicode: need string or buffer, file found > Is this the actual code or did you retype it? I just copied from .py file and pasted. > It has some typos which makes me wonder. If you copy/paste > the actual code that can remove any confusion introduced > by simple typing mistakes, so we are sure we're all looking > at the same thing here. > > > > 1)Why I got the above error message with the above function?How to > correct it? > > > The binascii.hexlify() function converts a binary data string into > hexadecimal digits. You didn't give it a data string to work from, > you gave it an open file object. You'll need to actually read the > data from the file and give that to the function. > > > > 2)Is it reasonable to have 2 if blocks in a function as above? > > > Sure > > > > 3)Dose the tempfile create a fileobject on harddisk or in memory(Dose it > save my > > file as I expect it to do) > > > It creates a TEMPORARY file. That means you can expect it to > exist on disk until you close it, and then if at all possible, > it will automatically be destroyed for you. Hence "temporary". > Depending on your platform, while there will be a physical disk > file, it might not even show up in a directory or be openable by > other applications. > > If you want a file to not be temporary, use open() to create it. > Steve Willoughby| Using billion-dollar satellites > st...@alchemy.com | to hunt for Tupperware. Thanks for the reply..Now I will try to correct my code. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Doubts galore.
On 06/11/10 01:14, prasad rao wrote: > Hi > > def cript(doc=None,data =None): >if doc==None and data==None:doc=sys.agv1 >elif doc: > data=open(doc,'r').read() > data_c= binascii.hexlify(data) >else:data_c= binascii.hexlify(data) >if doc: > q=tempfile.TemporaryFile() > q.write(data_c) > os.rename(q,doc) > return >return data_c > > > cript(doc='./language.txt') > Traceback (most recent call last): > File "", line 1, in > File "", line 10, in cript > TypeError: coercing to Unicode: need string or buffer, file found > > 1)Why I got the above error message with the above function?How to correct > it? The error message points out here: # line 10 os.rename(q,doc) ^^^ scanning a few lines earlier, we saw: q=tempfile.TemporaryFile() os.rename() doesn't rename a file object; os.rename() takes two string arguments. > 2)Is it reasonable to have 2 if blocks in a function as above? This is where boolean algebra can help. An elif-block will only be run when all the if-elif blocks before it evaluates to false. IOW, your code is equivalent to this: if doc==None and data==None: doc=sys.agv1 if not (doc==None and data==None) and doc: data=open(doc,'r').read() data_c= binascii.hexlify(data) except that "doc==None and data==None" is only evaluated once. So, using boolean algebra (assuming a reasonable definition of == and !=): not (doc==None and data==None) and doc (doc!=None or data!=None) and doc now depending on the intent of "doc" and "doc != None"; it may be possible to simplify that to only: data != None. Note that this is all assuming a reasonable definition of ==, !=, not, etc and assuming no side effects and assuming that the checks are equally lightweight. > 3)Dose the tempfile create a fileobject on harddisk or in memory(Dose it save > my > file as I expect it to do) AFAIK, tempfile creates a file in harddisk, but I've never used tempfile, so don't quote me on that. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Doubts galore.
On Thu, Jun 10, 2010 at 08:44:56PM +0530, prasad rao wrote: > Hi > > def cript(doc=None,data =None): >if doc==None and data==None:doc=sys.agv1 Never NEVER compare for equality with None. What you want is: if doc is None and data is None: Also, what is "sys.agv1"? Did you mean sys.argv[1]? >elif doc: > data=open(doc,'r').read() > data_c= binascii.hexlify(data) >else:data_c= binascii.hexlify(data) >if doc: > q=tempfile.TemporaryFile() > q.write(data_c) > os.rename(q,doc) > return >return data_c It would probably be cleaner to use one-line conditional statements (sparingly) where they make sense on their own, but not to mix multi-line and single line styles in the same if-else structure. I'm not sure the logical flow through there does what you think it does, though. > cript(doc='./language.txt') > Traceback (most recent call last): > File "", line 1, in > File "", line 10, in cript > TypeError: coercing to Unicode: need string or buffer, file found > Is this the actual code or did you retype it? It has some typos which makes me wonder. If you copy/paste the actual code that can remove any confusion introduced by simple typing mistakes, so we are sure we're all looking at the same thing here. > 1)Why I got the above error message with the above function?How to correct > it? The binascii.hexlify() function converts a binary data string into hexadecimal digits. You didn't give it a data string to work from, you gave it an open file object. You'll need to actually read the data from the file and give that to the function. > 2)Is it reasonable to have 2 if blocks in a function as above? Sure > 3)Dose the tempfile create a fileobject on harddisk or in memory(Dose it save > my > file as I expect it to do) It creates a TEMPORARY file. That means you can expect it to exist on disk until you close it, and then if at all possible, it will automatically be destroyed for you. Hence "temporary". Depending on your platform, while there will be a physical disk file, it might not even show up in a directory or be openable by other applications. If you want a file to not be temporary, use open() to create it. > > Please someone enlighten me. > > Prasad > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Steve Willoughby| Using billion-dollar satellites st...@alchemy.com | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor