[Tutor] Top Programming Languages of 2013
http://www.redcanary.ca/view/top-programming Dick Moores ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Random module help
Thank you, this is great. Alan Gauld wrote: Norman Khine [EMAIL PROTECTED] wrote q_keys.sort() a = [q[x] for x in q_keys] a = [random.choice(q[x]) for x in q_keys] So from this how do I choose a random element and produce a new dictionary like for example: See above. -- Norman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a code question, but don't know question's name
Happy Deer [EMAIL PROTECTED] wrote def getdata(varlist): eventually I have a variable called data, which have exactly the same number of columns as the name of variables in varlist. Say varlist=['var1','var2','var3']. I want to assign var1=data[:,0], var2=data[:,1], var3=data[:2] and return var1, var2, var3. It doesn't matter what you call the variables inside the function, that won't affect what they are called after you return the values. The notation data[;,0] doesn't make sense and is an error in Python. I#m not sure what you think it does. I assume you simply mean data[0]? Now, to your functions purpose. If the varlist contains 7 names, do you still only want to return var1, var2, var3? Or do you want to return all 7 values? If so then just return data... If not do you need to find the index of var1,var2,var3 in varlist and return the corresponding values from data? Its not clear to me exactly what the function is supposed to do. And without any code it's hard to guess. But when I write the function, I only know I want to assign data[:,0] to a variable which has a name as varlist[0]. How to write this? I repeat that any variable names you define inside the function will not be visible outside the function. If you want to pass the name back to the outside world you should return a name/value tuple or a dictionary eg. {'var1' : 42, 'var2': 66, 'var3' 99} HTH -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Top Programming Languages of 2013
Dick Moores [EMAIL PROTECTED] wrote http://www.redcanary.ca/view/top-programming Interesting, but I'm not sure what the criteria for top is. Is it a measure of power, popularity, usage? Scary that HTML/CSS should be so high though given its not a programming language at all! Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Top Programming Languages of 2013
Alan Gauld wrote: Dick Moores [EMAIL PROTECTED] wrote http://www.redcanary.ca/view/top-programming Interesting, but I'm not sure what the criteria for top is. Is it a measure of power, popularity, usage? Scary that HTML/CSS should be so high though given its not a programming language at all! Alan G. Besides all that, we are programmers not fashion models. Who cares which is the top language, leave that for the top models and such. Our trade is code, not fashion. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Top Programming Languages of 2013
On 07/10/2007, Ricardo Aráoz [EMAIL PROTECTED] wrote: Alan Gauld wrote: Dick Moores [EMAIL PROTECTED] wrote http://www.redcanary.ca/view/top-programming Interesting, but I'm not sure what the criteria for top is. Is it a measure of power, popularity, usage? Scary that HTML/CSS should be so high though given its not a programming language at all! Alan G. Besides all that, we are programmers not fashion models. Who cares which is the top language, leave that for the top models and such. Our trade is code, not fashion. It appears from the original survey page that it is the most popular by employer demand so it probably does matter if you want a job in that sort of area http://www.redcanary.ca/view/top-10-programming ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Top Programming Languages of 2013
What are these stats based on? On 10/7/07, Alan Gauld [EMAIL PROTECTED] wrote: Dick Moores [EMAIL PROTECTED] wrote http://www.redcanary.ca/view/top-programming Interesting, but I'm not sure what the criteria for top is. Is it a measure of power, popularity, usage? Scary that HTML/CSS should be so high though given its not a programming language at all! Alan G. -- http://mail.python.org/mailman/listinfo/python-list -- http://www.goldwatches.com/mens/cufflinks.html http://www.jewelerslounge.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a code question, but don't know question's name
Alan Gauld wrote: The notation data[;,0] doesn't make sense and is an error in Python. I#m not sure what you think it does. I assume you simply mean data[0]? [:,0] is an extended slice, not an error: http://docs.python.org/ref/slicings.html It is used in Numeric/numpy to select from multidimensional arrays. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] largest and smallest numbers
Christopher Spears wrote: One of the exercises from Core Python Programmng (2nd Edition) asks me to determine the largest and smallest integers, float, and complex numbers my system can handle. Using python.org and Google, I have discovered my system's largest and smallest ingtegers: import sys sys.maxint 2147483647 -sys.maxint - 1 -2147483648 How can I find the largest float and complex numbers? There is an extensive discussion starting here: http://homepages.cwi.nl/%7Esteven/enquire.html It also points to float.h. On my computer that is at /usr/includes/float.h and includes among other things #define __FLT_MIN__ 1.17549435e-38F #define __FLT_MAX__ 3.40282347e+38F Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a code question, but don't know question's name
Kent Johnson [EMAIL PROTECTED] wrote The notation data[;,0] doesn't make sense and is an error in Python. [:,0] is an extended slice, not an error: http://docs.python.org/ref/slicings.html Really? I got an error from the interpreter. d[:,0] Traceback (most recent call last): File input, line 1, in ? TypeError: list indices must be integers I thought extended slices were double colon?... but I see that's actually a long slice Following the link I get the somewhat obscure explanation: The semantics for an extended slicing are as follows. The primary must evaluate to a mapping object, This bit I understand :-) and it is indexed with a key that is constructed from the slice list, as follows. If the slice list contains at least one comma, the key is a tuple containing the conversion of the slice items; This seems to be the case here, but where is the tuple? Is it the whole list and the zeroth item? If i try testing anything I just get an error. When were these introduced? I am on v2.4.3 otherwise, the conversion of the lone slice item is the key. The conversion of a slice item that is an expression is that expression. The conversion of an ellipsis slice item is the built-in Ellipsis object. The conversion of a proper slice is a slice object (see section 3.2) whose start, stop and step attributes are the values of the expressions given as lower bound, upper bound and stride, respectively, substituting None for missing expressions. And most of that I don't understand. Can someone who does give some examples of this and what you would use it for? It is used in Numeric/numpy to select from multidimensional arrays. I don't use either, but since its part of the languyage I assiume it can be used in normal Python lists too? please? -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a code question, but don't know question's name
Alan Gauld wrote: Kent Johnson [EMAIL PROTECTED] wrote The notation data[;,0] doesn't make sense and is an error in Python. [:,0] is an extended slice, not an error: http://docs.python.org/ref/slicings.html Really? I got an error from the interpreter. d[:,0] Traceback (most recent call last): File input, line 1, in ? TypeError: list indices must be integers Ok. What I meant is, it is not a *syntax* error. Following the link I get the somewhat obscure explanation: The semantics for an extended slicing are as follows. The primary must evaluate to a mapping object, This bit I understand :-) and it is indexed with a key that is constructed from the slice list, as follows. If the slice list contains at least one comma, the key is a tuple containing the conversion of the slice items; This seems to be the case here, but where is the tuple? The tuple is the parameter passed to __getitem__(): In [1]: class sliced(object): ...: def __getitem__(self, item): ...: print repr(item) ...: ...: In [2]: s=sliced() In [3]: s[3] 3 In [4]: s[1:2:3] slice(1, 2, 3) In [5]: s[:,0] (slice(None, None, None), 0) If i try testing anything I just get an error. There's the rub. Extended slicing is not supported by any built-in containers. That's why you get a TypeError when you try it with a list. When were these introduced? I am on v2.4.3 At least since 2.0 :-) http://www.python.org/doc/2.0/ref/slicings.html otherwise, the conversion of the lone slice item is the key. The conversion of a slice item that is an expression is that expression. The conversion of an ellipsis slice item is the built-in Ellipsis object. The conversion of a proper slice is a slice object (see section 3.2) whose start, stop and step attributes are the values of the expressions given as lower bound, upper bound and stride, respectively, substituting None for missing expressions. And most of that I don't understand. It is saying what the values passed to __getitem__() are: either a plain object (probably an integer); an Ellipsis object, if the parameter is ...: In [6]: s[...] Ellipsis or a slice object, or a tuple of such things. Can someone who does give some examples of this and what you would use it for? It is used in Numeric/numpy to select from multidimensional arrays. See the numpy docs for examples. I don't use either, but since its part of the languyage I assiume it can be used in normal Python lists too? No. It can be used for user-defined classes but it is not supported by any built-ins. IIUC this feature was added specifically for Numeric. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a code question, but don't know question's name
Kent Johnson wrote: Alan Gauld wrote: Kent Johnson [EMAIL PROTECTED] wrote The notation data[;,0] doesn't make sense and is an error in Python. [:,0] is an extended slice, not an error: http://docs.python.org/ref/slicings.html We discussed this quite a bit last July: http://mail.python.org/pipermail/tutor/2007-July/055814.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] File Writing Permission?
Hi everyone, I'm working on making a Debian package for a game I'm working on, and it works fine, but I've run into a problem with my script now. The script is placed in /usr/bin so all users on the system have access, but even though it will run fine, I can't save files to that directory, so the saving feature doesn't work and the rest of my game is crippled now. Any ideas on how I could progress? I tried appending ~/ to the filename for that copy of the script, but it just makes the terminal window close without warning. Any help would be appreciated. def savegame(fname): # This part isn't necessary, I just wanted to see if it would save to the user's home folder if os.name == posix: fname = ~/ + fname infile = open(fname,w) # Save the maps mapchanges.append([6,1,rrmap]) mapchanges.append([6,2,treemap]) picklelist = [choins, inventory, book, mapchanges, event, cdata] pickle.dump(picklelist,infile) # Delete the saved maps because they will just get in the way later del mapchanges[len(mapchanges)-1] del mapchanges[len(mapchanges)-1] infile.close() David Millar http://www.thegriddle.net/python/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Logging with proper format
Hello tutors, I am adding logging to a program I am writing. I have some messages I want to log that are rather long. The problem I am running into is that when the line is more than the 80 character line recommendation and I split it across 2 lines with \, the output is affected. Example code: logger.info(Checked %s records in %s seconds, yielding an average of \ %s seconds per record. % (len(self.data), duration, avgquery) ) The current output: 2007-10-07 14:49:42,902 - ipinfo - INFO - Checked 4 records in 0.0698790550232 seconds, yielding an average of 0.0174697637558 seconds per record. Desired output would be: 2007-10-07 14:49:42,902 - ipinfo - INFO - Checked 4 records in 0.0698790550232 seconds, yielding an average of 0.0174697637558 seconds per record. So what is the best way to break long code lines and still preserve desired output? Thanks, Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a code question, but don't know question's name
Thank all for the discussion. Maybe I can separate my question into two. First, I have experience in Matlab, where I can use eval. I wonder whether someone knows about it. Second, if I just want to return data[:,1], ...data[:,-1] separately without knowing ahead how many columns data has. What should I do? On 10/7/07, Kent Johnson [EMAIL PROTECTED] wrote: Kent Johnson wrote: Alan Gauld wrote: Kent Johnson [EMAIL PROTECTED] wrote The notation data[;,0] doesn't make sense and is an error in Python. [:,0] is an extended slice, not an error: http://docs.python.org/ref/slicings.html We discussed this quite a bit last July: http://mail.python.org/pipermail/tutor/2007-July/055814.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Logging with proper format
Sent this almost an hour ago, did not get it from the list yet. No idea why, but sending again... -- Hello tutors, I am adding logging to a program I am writing. I have some messages I want to log that are rather long. The problem I am running into is that when the line is more than the 80 character line recommendation and I split it across 2 lines with \, the output is affected. Example code: logger.info(Checked %s records in %s seconds, yielding an average of \ %s seconds per record. % (len(self.data), duration, avgquery) ) The current output: 2007-10-07 14:49:42,902 - ipinfo - INFO - Checked 4 records in 0.0698790550232 seconds, yielding an average of 0.0174697637558 seconds per record. Desired output would be: 2007-10-07 14:49:42,902 - ipinfo - INFO - Checked 4 records in 0.0698790550232 seconds, yielding an average of 0.0174697637558 seconds per record. So what is the best way to break long code lines and still preserve desired output? Thanks, Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] File Writing Permission?
David Millar wrote: Hi everyone, I'm working on making a Debian package for a game I'm working on, and it works fine, but I've run into a problem with my script now. The script is placed in /usr/bin so all users on the system have access, but even though it will run fine, I can't save files to that directory, so the saving feature doesn't work and the rest of my game is crippled now. Any ideas on how I could progress? I tried appending ~/ to the filename for that copy of the script, but it just makes the terminal window close without warning. Any help would be appreciated. def savegame(fname): # This part isn't necessary, I just wanted to see if it would save to the user's home folder if os.name http://os.name == posix: fname = ~/ + fname Hi Dave, Python doesn't immediately do tilde expansion on filenames, you have to ask for it explicitly. Normally this is done by your shell, but since most modern shells do it, it's easy to assume it's an inherent property of filenames and not an added feature. Simply change: fname = ~/ + fname to: fname = os.path.expanduser( os.path.join( ~, fname ) ) Hope that helps, e. infile = open(fname,w) # Save the maps mapchanges.append([6,1,rrmap]) mapchanges.append([6,2,treemap]) picklelist = [choins, inventory, book, mapchanges, event, cdata] pickle.dump(picklelist,infile) # Delete the saved maps because they will just get in the way later del mapchanges[len(mapchanges)-1] del mapchanges[len(mapchanges)-1] infile.close() David Millar http://www.thegriddle.net/python/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] corresponding command in Python to eval in Matlab
Dear all- I wonder whether there is a way in Python which can do what eval in Matlab does. Say, varlist is a 1 by k tuple/list, which contains strings for variable names. For example, varlist=['var1','var2',...'vark'] data is a n by k matrix. I want to assign each column in data to each variable in varlist and get var1=data[:,1]... vark=data[:,k] Anyone knows how to do this? Fangwen ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a code question, but don't know question's name
Happy Deer wrote: Thank all for the discussion. Maybe I can separate my question into two. First, I have experience in Matlab, where I can use eval. I wonder whether someone knows about it. Second, if I just want to return data[:,1], ...data[:,-1] separately without knowing ahead how many columns data has. What should I do? I'm still unsure if you even need extended slicing. Given: data = [ 1, 2, 3, 4, 5, 6 ] What do you expect data[:,1] and data[:,-1] to return? On 10/7/07, *Kent Johnson* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Kent Johnson wrote: Alan Gauld wrote: Kent Johnson [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote The notation data[;,0] doesn't make sense and is an error in Python. [:,0] is an extended slice, not an error: http://docs.python.org/ref/slicings.html We discussed this quite a bit last July: http://mail.python.org/pipermail/tutor/2007-July/055814.html Kent ___ Tutor maillist - Tutor@python.org mailto:Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ 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] a code question, but don't know question's name
I am writing a function getdata for other people, and I want others can use the function as follows. var1,var2, var3=getdata(..., ['var1','var2','var3'],...) If I can not return data column by column, I can not get the above, right? On 10/7/07, Eric Brunson [EMAIL PROTECTED] wrote: Happy Deer wrote: Thank all for the discussion. Maybe I can separate my question into two. First, I have experience in Matlab, where I can use eval. I wonder whether someone knows about it. Second, if I just want to return data[:,1], ...data[:,-1] separately without knowing ahead how many columns data has. What should I do? I'm still unsure if you even need extended slicing. Given: data = [ 1, 2, 3, 4, 5, 6 ] What do you expect data[:,1] and data[:,-1] to return? On 10/7/07, *Kent Johnson* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Kent Johnson wrote: Alan Gauld wrote: Kent Johnson [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote The notation data[;,0] doesn't make sense and is an error in Python. [:,0] is an extended slice, not an error: http://docs.python.org/ref/slicings.html We discussed this quite a bit last July: http://mail.python.org/pipermail/tutor/2007-July/055814.html Kent ___ Tutor maillist - Tutor@python.org mailto:Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ 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] File Writing Permission?
if os.name == posix: fname = ~/ + fname infile = open(fname,w) you must expand '~' before open: fname = os.path.join('~',fname) fname = os.path.expanduser( fname ) infile = open(fname,'w') ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Logging with proper format
dont indent the line after '\', that means 0 indent s = 'hello\ boy' ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a code question, but don't know question's name
Happy Deer wrote: First, I have experience in Matlab, where I can use eval. I wonder whether someone knows about it. Python has an eval() function but it's use is discouraged, there is usually a better way. It would help if you would give us more context for your problem. Second, if I just want to return data[:,1], ...data[:,-1] separately without knowing ahead how many columns data has. What should I do? I'm assuming data is a numpy or Numeric array though it would help if you would confirm that. In [10]: from numpy import array In [11]: a = array([[1,2,3],[4,5,6]]) a.shape gives the dimensions; a.shape[1] is the second dimension In [15]: a.shape Out[15]: (2, 3) In [18]: a.shape[1] Out[18]: 3 You can use a list comprehension to make a list of the columns. In [19]: [ a[:,i] for i in range(1, a.shape[1])] Out[19]: [array([2, 5]), array([3, 6])] Note you asked for data[:,1], ...data[:,-1] which does not include the first column. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a code question, but don't know question's name
Happy Deer wrote: I am writing a function getdata for other people, and I want others can use the function as follows. var1,var2, var3=getdata(..., ['var1','var2','var3'],...) If I can not return data column by column, I can not get the above, right? Given: data = [ 1, 2, 3, 4, 5, 6 ] What do you expect data[:,1] and data[:,-1] to return? On 10/7/07, *Eric Brunson* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Happy Deer wrote: Thank all for the discussion. Maybe I can separate my question into two. First, I have experience in Matlab, where I can use eval. I wonder whether someone knows about it. Second, if I just want to return data[:,1], ...data[:,-1] separately without knowing ahead how many columns data has. What should I do? I'm still unsure if you even need extended slicing. Given: data = [ 1, 2, 3, 4, 5, 6 ] What do you expect data[:,1] and data[:,-1] to return? On 10/7/07, *Kent Johnson* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Kent Johnson wrote: Alan Gauld wrote: Kent Johnson [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote The notation data[;,0] doesn't make sense and is an error in Python. [:,0] is an extended slice, not an error: http://docs.python.org/ref/slicings.html We discussed this quite a bit last July: http://mail.python.org/pipermail/tutor/2007-July/055814.html Kent ___ Tutor maillist - Tutor@python.org mailto:Tutor@python.org mailto:Tutor@python.org mailto:Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org mailto:Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] corresponding command in Python to eval in Matlab
A good python coder would probably not choose to pollute his name space like that. My choice would be to assign the elements of data to a dictionary indexed by the strings in varlist like this: vardict = dict( zip( varlist, data ) ) and reference var1 as: vardict['var1'] Happy Deer wrote: Dear all- I wonder whether there is a way in Python which can do what eval in Matlab does. Say, varlist is a 1 by k tuple/list, which contains strings for variable names. For example, varlist=['var1','var2',...'vark'] data is a n by k matrix. I want to assign each column in data to each variable in varlist and get var1=data[:,1]... vark=data[:,k] Anyone knows how to do this? Fangwen ___ 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] Logging with proper format
claxo wrote: dont indent the line after '\', that means 0 indent s = 'hello\ boy' Or, arguably better: s = '''hello boy''' ___ 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] Logging with proper format
wormwood_3 wrote: Hello tutors, I am adding logging to a program I am writing. I have some messages I want to log that are rather long. The problem I am running into is that when the line is more than the 80 character line recommendation and I split it across 2 lines with \, the output is affected. Example code: logger.info(Checked %s records in %s seconds, yielding an average of \ %s seconds per record. % (len(self.data), duration, avgquery) ) - this is white space in your string! Try this: logger.info(Checked %s records in %s seconds, yielding an \ average of \ %s seconds per record. % (len(self.data), duration, avgquery) ) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a code question, but don't know question's name
Happy Deer wrote: I am writing a function getdata for other people, and I want others can use the function as follows. var1,var2, var3=getdata(..., ['var1','var2','var3'],...) There is no need to pass the variable names to getdata. Have you read any Python tutorials? There are several good ones listed here: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers If I can not return data column by column, I can not get the above, right? You can just return the columns: def getdata(data): return data[:,0], data[:,1], data[:,2] Kent On 10/7/07, *Eric Brunson* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Happy Deer wrote: Thank all for the discussion. Maybe I can separate my question into two. First, I have experience in Matlab, where I can use eval. I wonder whether someone knows about it. Second, if I just want to return data[:,1], ...data[:,-1] separately without knowing ahead how many columns data has. What should I do? I'm still unsure if you even need extended slicing. Given: data = [ 1, 2, 3, 4, 5, 6 ] What do you expect data[:,1] and data[:,-1] to return? On 10/7/07, *Kent Johnson* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Kent Johnson wrote: Alan Gauld wrote: Kent Johnson [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote The notation data[;,0] doesn't make sense and is an error in Python. [:,0] is an extended slice, not an error: http://docs.python.org/ref/slicings.html We discussed this quite a bit last July: http://mail.python.org/pipermail/tutor/2007-July/055814.html Kent ___ Tutor maillist - Tutor@python.org mailto:Tutor@python.org mailto:Tutor@python.org mailto:Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org mailto:Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] corresponding command in Python to eval in Matlab
However, I didn't actually answer your question. As Kent has already mentioned, eval is quite dangerous in python and to be avoided when possible. I think it would be safer to do something like this: l = locals() for x, y in zip( varlist, data ): l[x] = y or, more tersely: [ locals()[x] = y for x, y in zip( varlist, data ) ] This will accomplish what you're trying to do, but a dict really gives you greater functionality than assigning to local variables. Python is *not* Matlab. Functions that have the same name are not necessarily equivalent. Plus, you should start to think in Python rather than thinking in Matlab and transliterating. Eric Brunson wrote: A good python coder would probably not choose to pollute his name space like that. My choice would be to assign the elements of data to a dictionary indexed by the strings in varlist like this: vardict = dict( zip( varlist, data ) ) and reference var1 as: vardict['var1'] Happy Deer wrote: Dear all- I wonder whether there is a way in Python which can do what eval in Matlab does. Say, varlist is a 1 by k tuple/list, which contains strings for variable names. For example, varlist=['var1','var2',...'vark'] data is a n by k matrix. I want to assign each column in data to each variable in varlist and get var1=data[:,1]... vark=data[:,k] Anyone knows how to do this? Fangwen ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ 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] corresponding command in Python to eval in Matlab
Happy Deer wrote: Dear all- I wonder whether there is a way in Python which can do what eval in Matlab does. Say, varlist is a 1 by k tuple/list, which contains strings for variable names. For example, varlist=['var1','var2',...'vark'] data is a n by k matrix. I want to assign each column in data to each variable in varlist and get var1=data[:,1]... vark=data[:,k] A better way to do this is to make a dictionary that holds the values: values = dict(var1=data[:,1]... vark=data[:,k]) or perhaps just a list with the columns as I have already shown you in a separate email. Python is not Matlab, it will serve you well in the long run to learn the Python way of Python instead of trying to write Matlab programs in Python. Python's support for lists and dictionaries is very strong and useful. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Logging with proper format
Eric Brunson wrote: claxo wrote: dont indent the line after '\', that means 0 indent s = 'hello\ boy' Or, arguably better: s = '''hello boy''' That is a different string, it contains a newline, the original does not: In [20]: s = 'hello\ : boy' In [21]: s2 = '''hello : boy''' In [22]: s==s2 Out[22]: False In [23]: print s helloboy In [24]: print s2 hello boy Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] corresponding command in Python to eval in Matlab
Eric Brunson wrote: However, I didn't actually answer your question. As Kent has already mentioned, eval is quite dangerous in python and to be avoided when possible. I think it would be safer to do something like this: l = locals() for x, y in zip( varlist, data ): l[x] = y or, more tersely: [ locals()[x] = y for x, y in zip( varlist, data ) ] This will accomplish what you're trying to do, but a dict really gives you greater functionality than assigning to local variables. This will only work at global scope where locals() is globals() and writable. In function scope assigning to locals()[...] does not change the local namespace: In [27]: def foo(): : locals()['x'] = 1 : print x : : In [28]: foo() Traceback (most recent call last): File ipython console, line 1, in module File ipython console, line 3, in foo type 'exceptions.NameError': global name 'x' is not defined Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Logging with proper format
logger.info(Checked %s records in %s seconds, yielding an average of \ %s seconds per record. % (len(self.data), duration, avgquery) ) ^ Remove these spaces. It makes the source code look weird, but the output will be correct. Alan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a code question, but don't know question's name
On Sunday 07 October 2007 21:29, Happy Deer wrote: Thank all for the discussion. Maybe I can separate my question into two. First, I have experience in Matlab, where I can use eval. I wonder whether someone knows about it. I suspect you are using Numpy, you should subscribe to the Numpy mailing-list. There are people that can compare Numpy to Matlab. Probably you should also subscribe to the Scipy mailing list(s). http://www.scipy.org/Mailing_Lists Second, if I just want to return data[:,1], ...data[:,-1] separately without knowing ahead how many columns data has. What should I do? [EMAIL PROTECTED]:~ ipython --pylab much text deleted In [1]:a = array([[1,2,3], [4,5,6]]) In [2]:a Out[2]: array([[1, 2, 3], [4, 5, 6]]) In [3]:a[:,0] Out[3]:array([1, 4]) In [4]:a.shape Out[4]:(2, 3) In [6]:for i in range(a.shape[1]): .6.: print a[:,i] .6.: [1 4] [2 5] [3 6] If you want a[:,i] be/stay a column vector use the matrix class. The mat constructor function even has Matlab like syntax. In [8]:import numpy as np In [9]:a = np.mat(1,2,3; 4,5,6) In [11]:a Out[11]: matrix([[1, 2, 3], [4, 5, 6]]) In [12]:a[:,0] Out[12]: matrix([[1], [4]]) I shortened the Ipython session a little, you don't want to see my syntax errors I guess. HTH, Eike. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Logging with proper format
On Sunday 07 October 2007 22:32, Kent Johnson wrote: Eric Brunson wrote: claxo wrote: dont indent the line after '\', that means 0 indent s = 'hello\ boy' Or, arguably better: s = '''hello boy''' And there is even a third way:-) s = hello \ ... world. s 'hello world.' Two strings that are adjacent to each other, are concatenated into one string; like in C++. Regards, Eike. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a code question, but don't know question's name
I have written the exact same reply. Sorry for that! I should have read the other replies first. Eike. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Permission Report
Hello all, I have a tree of code on a machine which has been tweaked and fiddled with over several months, and which passes tests. I have the same codebase in a new virtual machine. A shell hack[0] shows me that the permissions are very different between the two. I could use rsync or something to synchronise them, but I would like to produce a report of the sort: Change file: foo from 755 to 775 So I can try to work out why this is necessary. I'm not sure how best to proceed - I guess walk through the filesystem gathering info using stat, then do the same on the new system, and compare. Or are there some clever modules I could use? S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Permission Report
Stephen Nelson-Smith [EMAIL PROTECTED] wrote I could use rsync or something to synchronise them, but I would like to produce a report of the sort: Change file: foo from 755 to 775 I'm not sure how best to proceed - I guess walk through the filesystem gathering info using stat, then do the same on the new system, and compare. Yes, os.walk and os.stat should do what you want. There is a dircmp module that may do what you want but I've not used it. Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Updating MySQL Database
Hello all, I have a script which takes data from a file or MySQL DB, looks up some stuff, then can print results to console or file. I would also like it to be able to update a MySQL database with the results. Does anyone have any ideas on how to do this? I can update records just fine, but what is the best way to do LOTS of updates aside from running an update statement per record? Using that method, for example, assuming I have a list of results, each line of the form ip,fqdn: for line in inputlist: updatequery = update resultstable set fqdn = line.split(,)[1] where ip = line.split(,)[0]; connection = MySQLdb.connect(db=self.todatabase, host=self.host, user=self.user, passwd=self.passwd, port=int(self.port)) cursor = connection.cursor() cursor.execute(updatequery) queryresults = cursor.fetchall() cursor.close() connection.close() But this means making a connection and query for every line of results, which is a lot. Any ideas on optimization? Thanks, Sam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Updating MySQL Database
wormwood_3 wrote: Hello all, I have a script which takes data from a file or MySQL DB, looks up some stuff, then can print results to console or file. I would also like it to be able to update a MySQL database with the results. Does anyone have any ideas on how to do this? I can update records just fine, but what is the best way to do LOTS of updates aside from running an update statement per record? Using that method, for example, assuming I have a list of results, each line of the form ip,fqdn: for line in inputlist: updatequery = update resultstable set fqdn = line.split(,)[1] where ip = line.split(,)[0]; connection = MySQLdb.connect(db=self.todatabase, host=self.host, user=self.user, passwd=self.passwd, port=int(self.port)) cursor = connection.cursor() cursor.execute(updatequery) queryresults = cursor.fetchall() cursor.close() connection.close() But this means making a connection and query for every line of results, which is a lot. Any ideas on optimization? Thanks, Sam Haven't worked with DBs in Python yet but I guess it can't be too different. What if you put the connect/disconnect outside your loop? connection = MySQLdb.connect(db=self.todatabase, host=self.host, user=self.user, passwd=self.passwd, port=int(self.port)) cursor = connection.cursor() for line in inputlist: updatequery = update resultstable set fqdn = line.split(,)[1] where ip = line.split(,)[0]; cursor.execute(updatequery) queryresults = cursor.fetchall() cursor.close() connection.close() Do you need to do the cursor.fetchall()? AFAIK an update will return no data. You might want to use transactions too (outside the loop). BTW, I think your updatequery not properly configured, MySQL supports (?) variable substitution (and even if it didn't your updatequery is wrong, the variables should be outside the string with a %). HTH ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Permission Report
Alan Gauld wrote: There is a dircmp module that may do what you want but I've not used it. It was deprecated in Python 2.0 and removed in 2.5. From a quick look it might provide a useful framework but it doesn't compare permissions. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Updating MySQL Database
wormwood_3 wrote: Hello all, I have a script which takes data from a file or MySQL DB, looks up some stuff, then can print results to console or file. I would also like it to be able to update a MySQL database with the results. Does anyone have any ideas on how to do this? I can update records just fine, but what is the best way to do LOTS of updates aside from running an update statement per record? Using that method, for example, assuming I have a list of results, each line of the form ip,fqdn: for line in inputlist: updatequery = update resultstable set fqdn = line.split(,)[1] where ip = line.split(,)[0]; This doesn't look like real code, is it missing some quotes and +? It also looks like you are embedding the data in the SQL command, this is very bad practice, it opens you to SQL injection attacks and doesn't correctly handle data with special characters. You should pass the parameters in a separate list. connection = MySQLdb.connect(db=self.todatabase, host=self.host, user=self.user, passwd=self.passwd, port=int(self.port)) cursor = connection.cursor() cursor.execute(updatequery) queryresults = cursor.fetchall() cursor.close() connection.close() But this means making a connection and query for every line of results, which is a lot. Any ideas on optimization? There is no need to make a new connection and cursor for each query, you can reuse the same cursor, just put the code to aquire and close them outside the loop. Also take a look at cursor.executemany(). I'm not familiar with MySQL but there is probably a way to run commands from a file, that may be faster than doing it from Python. You could build a file with all the required commands. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Updating MySQL Database
On Sun, Oct 07, 2007 at 06:07:45PM -0700, wormwood_3 wrote: Hello all, I have a script which takes data from a file or MySQL DB, looks up some stuff, then can print results to console or file. I would also like it to be able to update a MySQL database with the results. Does anyone have any ideas on how to do this? I can update records just fine, but what is the best way to do LOTS of updates aside from running an update statement per record? Using that method, for example, assuming I have a list of results, each line of the form ip,fqdn: for line in inputlist: updatequery = update resultstable set fqdn = line.split(,)[1] where ip = line.split(,)[0]; connection = MySQLdb.connect(db=self.todatabase, host=self.host, user=self.user, passwd=self.passwd, port=int(self.port)) cursor = connection.cursor() cursor.execute(updatequery) queryresults = cursor.fetchall() cursor.close() connection.close() But this means making a connection and query for every line of results, which is a lot. Any ideas on optimization? Good grief! Open the connection and create the cursor ONCE, use it as many times as you need, and then close it (them). Closing the connection automatically closes the cursor. CAUTION: the following is not fully tested but I believe it to be correct. connection = MySQLdb.connect(db=self.todatabase,host-self.host, user=self.user, passwd=self.passwd, port=int(self.port)) cursor = connection.cursor() for line in inputlist: (ip,fqdn) = line.split(',') updatequery = update resultstable set %s where ip = %s % (fqdn,ip) cursor.execute(updatequery) connection.close() Alternatively you could do: connection = MySQLdb.connect(db=self.todatabase,host-self.host, user=self.user, passwd=self.passwd, port=int(self.port)) cursor = connection.cursor() updatequery = update resultstable set %s where ip = %s for line in inputlist: vals = list(line.split(',')) vals.reverse() cursor.execute(updatequery,vals) connection.close() I think the second version is more efficient. or you could do: connection = MySQLdb.connect(db=self.todatabase,host-self.host, user=self.user, passwd=self.passwd, port=int(self.port)) cursor = connection.cursor() updatequery = update resultstable set %s where ip = %s for line in inputlist: (ip,fqdn) = line.split(',') cursor.execute(updatequery,(fqdn,ip)) connection.close() Note that for these last two there is a comma between the arguments to cursor.execute() rather than a percent. -- I am only one. But I am one. I cannot do everything but I can do something. And I will not let what I cannot do interfere with what I can do. -- Edward E. Hule Rick Pasotto[EMAIL PROTECTED]http://www.niof.net ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Updating MySQL Database
Hey Sam, wormwood_3 wrote: Hello all, I have a script which takes data from a file or MySQL DB, looks up some stuff, then can print results to console or file. I would also like it to be able to update a MySQL database with the results. Does anyone have any ideas on how to do this? I can update records just fine, but what is the best way to do LOTS of updates aside from running an update statement per record? I recommend not worrying about speed on your first iteration of your script. I'd first define 'too slow', then write the script the way that feels intuitive to you. If your script passes your threshold of 'too slow', then look at optimizing it. If optimization is really necessary, I'd look into .executemany(). If that is still too slow for you then I'd consider writing the update SQL to a file and then calling mysql, passing it the sql file your script created. I found this last approach the fastest for a data import script I once wrote for importing millions of records to a PostgreSQL database. -E ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] File Writing Permission?
Thanks - it still took a bit more tweaking because of how I wrote a few things, but the saving works fine now. I've been having trouble with finding a version of the curses library for Windows that I can get working, so I just used os.path.expanduser no matter the system. :/ Dave On 10/7/07, claxo [EMAIL PROTECTED] wrote: if os.name == posix: fname = ~/ + fname infile = open(fname,w) you must expand '~' before open: fname = os.path.join('~',fname) fname = os.path.expanduser( fname ) infile = open(fname,'w') ___ 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] Updating MySQL Database
Rick Pasotto wrote: (ip,fqdn) = line.split(',') updatequery = update resultstable set %s where ip = %s % (fqdn,ip) cursor.execute(updatequery) connection.close() Alternatively you could do: connection = MySQLdb.connect(db=self.todatabase,host-self.host, user=self.user, passwd=self.passwd, port=int(self.port)) cursor = connection.cursor() updatequery = update resultstable set %s where ip = %s for line in inputlist: vals = list(line.split(',')) vals.reverse() cursor.execute(updatequery,vals) connection.close() I think the second version is more efficient. Not only is it more efficient, I'd say it's the ONLY way you should be doing this. using % to splice the data values into the SQL query string itself is fraught with problems, SQL injection vulnerability and getting proper data syntax being the most obvious ones. Letting the MySQL module do it for you, by using %s (and ONLY %s regardless of data type!) where you want data to go, and supplying a list of values to cursor.execute(), lets the library code properly escape and quote the data as appropriate. It understands things like datetime, float, string, None, etc. natively which is a big win. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor