Re: [Tutor] Error Checking/Defensive Programming

2012-01-26 Thread Peter Otten
Modulok wrote: > if number <= 10 and number >= 1: I like that you can spell that if 1 <= number <= 10: in Python. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

[Tutor] TypeError: only length-1 arrays can be converted to Python scalars

2012-01-26 Thread stm atoc
Hi, I have a question regarding the TypeError. I have this script as follows in Python. I have run a program in Fortran: # coding: utf-8 from pylab import * import numpy import matplotlib.pyplot as pyplot import matplotlib.mlab as mlab t=3600 N = 100 dz = 1.0 with open("act_out.list", "r") as

Re: [Tutor] TypeError: only length-1 arrays can be converted to Python scalars

2012-01-26 Thread Peter Otten
stm atoc wrote: > Hi, > > I have a question regarding the TypeError. > > I have this script as follows in Python. I have run a program in Fortran: > > # coding: utf-8 > from pylab import * > import numpy > import matplotlib.pyplot as pyplot > import matplotlib.mlab as mlab > > t=3600 > N = 100

[Tutor] Socket Programming

2012-01-26 Thread Navneet
Hi, I am trying to create a chat program.(Programs are attached) Please find the code below, where I am having the problem. def run( self ): while 1: print "Hello There " print self.descriptors # Await an event on a readable socket descriptor (srea

[Tutor] splitting the large file into small pieces and download

2012-01-26 Thread Arun Kumar
Hi all, I want to build a small download accelerator program. I want to know how to split the file to be downloaded into small pieces and assign the each small piece to a thread for downloading. -- Thanks & Regards, Arun Kumar http://clicknscroll.blogspot.com

[Tutor] Pythonic way of concatenation of elements in an array

2012-01-26 Thread spawgi
Hello, My code is - l = len(m) item = str(m[1]) for i in range(2,l): item = item + "-" + str(m[i]) This code is part of a bigger function. It works fine. But I am not happy with the way I have written it. I think there is a better (Pythonic) way to rewrite it. If anyone knows how to improve

Re: [Tutor] Pythonic way of concatenation of elements in an array

2012-01-26 Thread Steven D'Aprano
spa...@gmail.com wrote: Hello, My code is - l = len(m) item = str(m[1]) for i in range(2,l): item = item + "-" + str(m[i]) This code is part of a bigger function. It works fine. But I am not happy with the way I have written it. I think there is a better (Pythonic) way to rewrite it. If an

Re: [Tutor] splitting the large file into small pieces and download

2012-01-26 Thread Walter Prins
Hi Arun, On 26 January 2012 21:27, Arun Kumar wrote: > I want to build a small download accelerator program. I want to know how to > split the file to be downloaded into small pieces and assign the each small > piece to a thread for downloading. OK, so what else have you tried and what are you h

[Tutor] Why do you have to close files?

2012-01-26 Thread amt
Exercise 17, extra credit 6 Learn python the hard way: Find out why you had to do output.close() in the code. Code: from sys import argv from os.path import exists script, from_file, to_file = argv print "Copying from %s to %s" % (from_file, to_file) input = open(from_file) indata = input.rea

Re: [Tutor] Why do you have to close files?

2012-01-26 Thread ian douglas
On 1/26/12 3:20 PM, amt wrote: Exercise 17, extra credit 6 Learn python the hard way: Find out why you had to do output.close() in the code. Code: output.close() input.close() I don't get it. If you don't close input and output it works exactly the same as if you would close them, so why do

Re: [Tutor] Why do you have to close files?

2012-01-26 Thread Andre' Walker-Loud
>> Exercise 17, extra credit 6 Learn python the hard way: Find out why >> you had to do output.close() in the code. >> >> >> Code: >> >> output.close() >> input.close() >> >> >> I don't get it. If you don't close input and output it works exactly >> the same as if you would close them, so why

Re: [Tutor] Why do you have to close files?

2012-01-26 Thread Dave Angel
On 01/26/2012 06:20 PM, amt wrote: Exercise 17, extra credit 6 Learn python the hard way: Find out why you had to do output.close() in the code. Code: from sys import argv from os.path import exists script, from_file, to_file = argv print "Copying from %s to %s" % (from_file, to_file) input

Re: [Tutor] Why do you have to close files?

2012-01-26 Thread Steven D'Aprano
amt wrote: I don't get it. If you don't close input and output it works exactly the same as if you would close them, so why do you have to do output.close() and input.close()? (1) It is a matter of good programming practice. If you don't close them yourself, Python will eventually close them

[Tutor] Question regarding setup.py

2012-01-26 Thread sp6
Hi All, I had a question regarding installing packages that I posted a couple of days ago. But I'm re-sending the question again.. this time with output so that it is clearer. I am unable to install libraries using 'python setup.py install' Say that I'm installing a package "kando". I extr

Re: [Tutor] Why do you have to close files?

2012-01-26 Thread Alan Gauld
On 26/01/12 23:20, amt wrote: input = open(from_file) indata = input.read() ... output = open(to_file, 'w') output.write(indata) print "Alright, all done." output.close() input.close() I don't get it. If you don't close input and output it works exactly the same as if you would close them,

Re: [Tutor] Pythonic way of concatenation of elements in an array

2012-01-26 Thread spawgi
Hello Steven, Thanks a lot for the detailed answer. I will implement your suggestions. Really appreciate it. Thanks and Regards, Sumod On Fri, Jan 27, 2012 at 4:34 AM, Steven D'Aprano wrote: > spa...@gmail.com wrote: > >> Hello, >> >> My code is - >> >> l = len(m) >> item = str(m[1]) >> for i i

Re: [Tutor] Pythonic way of concatenation of elements in an array

2012-01-26 Thread Andre' Walker-Loud
Hi Steven, > (5) When assembling strings from substrings, never use repeated concatenation > using + as that can be EXTREMELY slow. Use str.join to build the string in > one assignment, instead of multiple assignments. > > Your code shown above is *very* inefficient and will be PAINFULLY slow i

Re: [Tutor] Why do you have to close files?

2012-01-26 Thread Stefan Behnel
Alan Gauld, 27.01.2012 02:16: > with open(myfile) as aFile: > # use aFile I should add that this is the shortest, safest (as in "hard to get wrong") and most readable way to open and close a file. It's worth getting used to. Stefan ___ Tutor maill