Re: parse a csv file into a text file

2014-02-06 Thread Zhen Zhang
On Wednesday, February 5, 2014 7:17:17 PM UTC-5, Asaf Las wrote:
> On Thursday, February 6, 2014 2:10:16 AM UTC+2, Zhen Zhang wrote:
> 
> > Hi, every one.
> 
> > Zhen
> 
> str_t = '3520005,"Toronto (Ont.)",C  
> ,F,2503281,2481494,F,F,0.9,1040597,979330,630.1763,3972.4,1' 
> 
> list_t = str_t.split(',')
> 
> print(list_t)
> 
> print("split result ", list_t[1], list_t[5])
> 
> print(list_t[1].split('"')[1])

Thanks for the reply,
I did not get the line 
str_t = '3520005,"Toronto (Ont.)",C  
,F,2503281,2481494,F,F,0.9,1040597,979330,630.1763,3972.4,1' 

I am processing a entire file not a line, so should i do 
str_t=line? maybe

list_t = str_t.split(',')
I think you are trying to spit a line into list.

but the line is already a list format right? that is why it allows me to do
something like line[1].
but I am not sure.
-- 
https://mail.python.org/mailman/listinfo/python-list


TypeError: 'list' object is not callable

2014-02-06 Thread wilsonmonde
import csv

date1 = []
open = []
high = []
low = []
close = []
data = []
with open("C:/Documents and Settings/wilson/My 
Documents/Downloads/execution.csv", "rb") as csvfile:
fastreader = csv.reader(csvfile, delimiter = ",", skipinitialspace=True)
count = 0
for row in fastreader:
date1.append(row[0])
count = count + 1


TypeError: 'list' object is not callable
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parse a csv file into a text file

2014-02-06 Thread Zhen Zhang
On Wednesday, February 5, 2014 7:34:57 PM UTC-5, MRAB wrote:
> On 2014-02-06 00:10, Zhen Zhang wrote:
> 
> > Hi, every one.
> 
> >
> 
> > I am a second year EE student.
> 
> > I just started learning python for my project.
> 
> >
> 
> > I intend to parse a csv file with a format like
> 
> >
> 
> > 3520005,"Toronto (Ont.)",C  
> > ,F,2503281,2481494,F,F,0.9,1040597,979330,630.1763,3972.4,1
> 
> > 2466023,"Montréal (Que.)",V  
> > ,F,1620693,1583590,T,F,2.3,787060,743204,365.1303,4438.7,2
> 
> > 5915022,"Vancouver (B.C.)",CY 
> > ,F,578041,545671,F,F,5.9,273804,253212,114.7133,5039.0,8
> 
> > 3519038,"Richmond Hill (Ont.)",T  
> > ,F,162704,132030,F,F,23.2,53028,51000,100.8917,1612.7,28
> 
> >
> 
> > into a text file like the following
> 
> >
> 
> > Toronto 2503281
> 
> > Montreal 1620693
> 
> > Vancouver 578041
> 
> >
> 
> > I am extracting the 1st and 5th column and save it into a text file.
> 
> >
> 
> > This is what i have so far.
> 
> >
> 
> >
> 
> > [code]
> 
> >
> 
> > import csv
> 
> > file = open('raw.csv')
> 
> > reader = csv.reader(file)
> 
> >
> 
> > f = open('NicelyDone.text','w')
> 
> >
> 
> > for line in reader:
> 
> >f.write("%s %s"%line[1],%line[5])
> 
> >
> 
> > [/code]
> 
> >
> 
> > This is not working for me, I was able to extract the data from the csv 
> > file as line[1],line[5]. (I am able to print it out)
> 
> > But I dont know how to write it to a .text file in the format i wanted.
> 
> >
> 
> % is an operator. When used with a format string on its left, its
> 
> arguments go on its right. In the general case, those arguments should
> 
> be put in a tuple, although if there's only one argument and it's not a
> 
> tuple, you can write just that argument:
> 
> 
> 
>  f.write("%s %s" % (line[1], line[5]))
> 
> 
> 
> > Also, I have to process the first column eg, "Toronto (Ont.)" into 
> > "Toronto".
> 
> > I am familiar with the function find(), I assume that i could extract 
> > Toronto out of Toronto(Ont.) using "(" as the stopping character,
> 
> > but based on my research , I have no idea how to use it and ask it to 
> > return me the string(Toronto).
> 
> >
> 
> Use find to tell you the index of the "(" (if there isn't one then
> 
> it'll return -1) and then slice the string to get the part preceding it.
> 
> 
> 
> Another way is to use the "partition" method.
> 
> 
> 
> Also, have a look at the "strip"/"lstrip"/"rstrip" methods.
> 
> 
> 
> > Here is my question:
> 
> > 1:What is the data format for line[1], if it is string how come 
> > f.write()does not work. if it is not string, how do i convert it to a 
> > string?
> 
> > 2:How do i extract the word Toronto out of Toronto(Ont) into a string form 
> > using find() or other methods.
> 
> >
> 
> > My thinking is that I could add those 2 string together like c=a+' ' +b, 
> > that would give me the format i wanted.
> 
> > So i can use f.write() to write into a file  ;)
> 
> >
> 
> > Sorry if my questions sounds too easy or stupid.
> 
> >
> 
> > Thanks ahead
> 
> >
> 
> > Zhen
> 
> >

Thanks for the reply, especially the tuple parts,
I was not familiar with this data format, 
but i guess i should :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to reduce bugs due to incorrect indentation

2014-02-06 Thread Terry Reedy

On 2/5/2014 10:02 PM, msus...@gmail.com wrote:


if a == 1:
 x = y
else:
 x = z
y = z + y
z = z + 1

While editing this file I accidentally pushed TAB on the line with 'y = z + y'.


In this particular case, remove the indentation with
x = y if a == 1 else z
and indenting the next line is  syntax error.
--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Re: parse a csv file into a text file

2014-02-06 Thread Zhen Zhang
On Wednesday, February 5, 2014 7:46:04 PM UTC-5, Tim Chase wrote:
> On 2014-02-05 16:10, Zhen Zhang wrote:
> 
> > import csv
> 
> > file = open('raw.csv')
> 
> 
> 
> Asaf recommended using string methods to split the file.  Keep doing
> 
> what you're doing (using the csv module), as it attends to a lot of
> 
> edge-cases that will trip you up otherwise.  I learned this the hard
> 
> way several years into my Python career. :-)
> 
> 
> 
> > reader = csv.reader(file)
> 
> > 
> 
> > f = open('NicelyDone.text','w')
> 
> > 
> 
> > for line in reader:
> 
> >   f.write("%s %s"%line[1],%line[5])
> 
> 
> 
> Here, I'd start by naming the pieces that you get, so do
> 
> 
> 
>   for line in reader:
> 
> location = line[1]
> 
> value = line[5]
> 
> 
> 
> > Also, I have to process the first column eg, "Toronto (Ont.)" into
> 
> > "Toronto". I am familiar with the function find(), I assume that i
> 
> > could extract Toronto out of Toronto(Ont.) using "(" as the
> 
> > stopping character, but based on my research , I have no idea how
> 
> > to use it and ask it to return me the string(Toronto).
> 
> 
> 
> You can use the .split() method to split a string, so you could do
> 
> something like
> 
> 
> 
>   if '(' in location:
> 
> bits = location.split('(')
> 
> # at this point, bits = ['Toronto ', 'Ont.)']
> 
> location = bits[0].strip() # also strip it to remove whitespace
> 
> 
> 
> > 1:What is the data format for line[1], if it is string how come
> 
> > f.write()does not work. if it is not string, how do i convert it to
> 
> > a string?
> 
> 
> 
> The problem is not that "it is not a string" but that you passing
> 
> multiple parameters, the second of which is invalid Python because it
> 
> has an extra percent-sign.  First create the one string that you
> 
> want to output:
> 
> 
> 
>   output = "%s %s\n" % (location, bits)
> 
> 
> 
> and then write it out to the file:
> 
> 
> 
>   f.write(output)
> 
> 
> 
> rather than trying to do it all in one pass.
> 
> 
> 
> -tkc

Hi Tim,

Thanks for the reply,

Does the split make a list or tuple?

also,

when  i do location=line[1],
it gives me a error even though the program did run correctly and output the 
correct file.
 location=line[1]
 IndexError: list index out of range 

when i do print line[1], there is no error.
it is really strange
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parse a csv file into a text file

2014-02-06 Thread Zhen Zhang
On Wednesday, February 5, 2014 7:57:26 PM UTC-5, Dave Angel wrote:
> Zhen Zhang  Wrote in message:
> 
> > Hi, every one.
> 
> > 
> 
> > I am a second year EE student.
> 
> > I just started learning python for my project.
> 
> > 
> 
> > I intend to parse a csv file with a format like 
> 
> > 
> 
> > 3520005,"Toronto (Ont.)",C  
> > ,F,2503281,2481494,F,F,0.9,1040597,979330,630.1763,3972.4,1
> 
> > 2466023,"Montréal (Que.)",V  
> > ,F,1620693,1583590,T,F,2.3,787060,743204,365.1303,4438.7,2
> 
> > 5915022,"Vancouver (B.C.)",CY 
> > ,F,578041,545671,F,F,5.9,273804,253212,114.7133,5039.0,8
> 
> > 3519038,"Richmond Hill (Ont.)",T  
> > ,F,162704,132030,F,F,23.2,53028,51000,100.8917,1612.7,28
> 
> > 
> 
> > into a text file like the following
> 
> > 
> 
> > Toronto 2503281
> 
> > Montreal 1620693
> 
> > Vancouver 578041
> 
> > 
> 
> > I am extracting the 1st and 5th column and save it into a text file.
> 
> 
> 
> Looks to me like columns 1 and 6.
> 
> 
> 
> > 
> 
> > This is what i have so far.
> 
> > 
> 
> > 
> 
> > [code]
> 
> > 
> 
> > import csv
> 
> > file = open('raw.csv')
> 
> > reader = csv.reader(file)
> 
> > 
> 
> > f = open('NicelyDone.text','w')
> 
> > 
> 
> > for line in reader:
> 
> >   f.write("%s %s"%line[1],%line[5])
> 
> 
> 
> Why not use print to file f? The approach for redirection is
> 
>  different between python 2 and 3, and you neglected to say which
> 
>  you're using. 
> 
> > 
> 
> 
> 
> > 
> 
> > My thinking is that I could add those 2 string together like c=a+' ' +b, 
> > that would give me the format i wanted.
> 
> 
> 
> And don't forget the "\n" at end of line. 
> 
> 
> 
> > So i can use f.write() to write into a file  ;) 
> 
> 
> 
> Or use print, which defaults to adding in a newline.
> 
> 
> 
> > Sorry if my questions sounds too easy or stupid.
> 
> > 
> 
> 
> 
> Not in the least.
> 
> 
> 
> > 
> 
> > 
> 
> 
> 
> 
> 
> -- 
> 
> DaveA

Hi Dave  Thanks for the reply,
I am currently running python 2.7.

Yes, i thought there must be a print function in python like fprint in C++ that 
allows you to print into a file directly.
But i google about "print string into text file" I got answers using f.write() 
instead. :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parse a csv file into a text file

2014-02-06 Thread Asaf Las
On Thursday, February 6, 2014 9:52:43 AM UTC+2, Zhen Zhang wrote:
> On Wednesday, February 5, 2014 7:33:00 PM UTC-5, Roy Smith wrote:
> I failed to figure out why.

OK, you had to look to what i posted second time. The first one is 
irrelevant. Note that file was emulated using StringIO. in your 
case it will be file name. 
You can grab script below and run directly as python script:

< start of script
import io
import csv

str_t = '''3520005,"Toronto (Ont.)",C  
,F,2503281,2481494,F,F,0.9,1040597,979330,630.1763,3972.4,1
2466023,"Montréal (Que.)",V  
,F,1620693,1583590,T,F,2.3,787060,743204,365.1303,4438.7,2
5915022,"Vancouver (B.C.)",CY 
,F,578041,545671,F,F,5.9,273804,253212,114.7133,5039.0,8
3519038,"Richmond Hill (Ont.)",T  
,F,162704,132030,F,F,23.2,53028,51000,100.8917,1612.7,28 '''

file_t = io.StringIO(str_t)

csv_t = csv.reader(file_t, delimiter = ',')
for row in csv_t: 
print("split result ", row[1].strip('"').split('(')[0] , row[5])


<- end of script
Output must be (i got it after run): 

split result  Toronto  2481494
split result  Montréal  1583590
split result  Vancouver  545671
split result  Richmond Hill  132030



row[1].strip('"').split('(')[0]is City name 
row[5] is digits at pos 5 wished



Both are strings, so save them later into file. 
Regarding this one - you can split operations as below to see what is 
happening:
row[1]
row[1].strip('"')
row[1].strip('"').split('(')
row[1].strip('"').split('(')[0]

Have a nice day 

/Asaf
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: TypeError: 'list' object is not callable

2014-02-06 Thread Peter Otten
wilsonmo...@gmail.com wrote:

> TypeError: 'list' object is not callable

Hint:

> open = []

[...]

> with open(..., "rb") as csvfile:


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parse a csv file into a text file

2014-02-06 Thread Asaf Las
On Thursday, February 6, 2014 10:15:14 AM UTC+2, Asaf Las wrote:
> On Thursday, February 6, 2014 9:52:43 AM UTC+2, Zhen Zhang wrote:
> case it will be file name. 

little correction not a file name - file object, file_t is result from open()
as you did in your example


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parse a csv file into a text file

2014-02-06 Thread Jussi Piitulainen
Zhen Zhang writes:
...
> I am currently running python 2.7.
> 
> Yes, i thought there must be a print function in python like fprint
> in C++ that allows you to print into a file directly.
>
> But i google about "print string into text file" I got answers using
> f.write() instead. :)

Indeed. The first Python hit for me with that query was the tutorial
page on I/O in Python 2, and it does exactly that.


That page does refer to the spec of the print statement, where you can
find the way to redirect the output to a file, but you need to be able
to read formal syntax specifications like this:

print_stmt ::=  "print" ([expression ("," expression)* [","]]
| ">>" expression [("," expression)+ [","]])

The relevant pattern is the second alternative, after the vertical
bar, which can be instantiated this way:

  print >> f, e0, e1

There is one object f with a .write method, and one or more
expressions whose values get written using f.write; the effect of an
optional comma at end is also specified there. Not tutorial-level.


But I use the newer print function even if I have to use 2.7,
something like this:

  from __future__ import print_function
  f = open("test.txt", "w")
  print("hello?", "see me?", file=f)
  f.close()

It does a modest amount of formatting: the value of the keyword
argument sep is written between the values, and the value of end is
written at end.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: TypeError: 'list' object is not callable

2014-02-06 Thread Gary Herron

On 02/06/2014 12:01 AM, wilsonmo...@gmail.com wrote:

import csv

date1 = []
open = []
high = []
low = []
close = []
data = []
with open("C:/Documents and Settings/wilson/My Documents/Downloads/execution.csv", 
"rb") as csvfile:
fastreader = csv.reader(csvfile, delimiter = ",", skipinitialspace=True)
count = 0
for row in fastreader:
date1.append(row[0])
count = count + 1


TypeError: 'list' object is not callable


I'd be glad to help, but I'm not interested in guessing.  Pleas take the 
time to tell us what line produced that error?  That is: cut and paste 
the *full* traceback instead of hiding useful information when you are 
asking for help.


Gary Herron

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: TypeError: 'list' object is not callable

2014-02-06 Thread wilsonmonde
Peter Otten於 2014年2月6日星期四UTC+8下午4時22分45秒寫道:
> wilsonmo...@gmail.com wrote:
> 
> 
> 
> > TypeError: 'list' object is not callable
> 
> 
> 
> Hint:
> 
> 
> 
> > open = []
> 
> 
> 
> [...]
> 
> 
> 
> > with open(..., "rb") as csvfile:


i follow in
http://www.dyinglovegrape.com/data_analysis/part1/1da3.php

still have error

what is the correct writing?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: TypeError: 'list' object is not callable

2014-02-06 Thread Asaf Las
On Thursday, February 6, 2014 11:11:13 AM UTC+2, wilso...@gmail.com wrote:
> i follow in
> http://www.dyinglovegrape.com/data_analysis/part1/1da3.php
> still have error
> what is the correct writing?

give another name to list 'open' at line 'open= []'
change it to dopen or whatever. you make name conflict with 
builtin function open(). 
Names can't be used freely.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: TypeError: 'list' object is not callable

2014-02-06 Thread Jussi Piitulainen
wilsonmo...@gmail.com writes:
> Peter Otten wrote:
> > wilsonmo...@gmail.com wrote:
> > 
> > > TypeError: 'list' object is not callable
> > 
> > Hint:
> > 
> > > open = []
> > 
> > [...]
>
> > > with open(..., "rb") as csvfile:
> 
> i follow in
> http://www.dyinglovegrape.com/data_analysis/part1/1da3.php
> 
> still have error
> 
> what is the correct writing?

One way:

   # open = []
   with open(..., "rb") as csvfile:

Commenting out the assignment statement prevents it from doing the
damage before you try to access the original value of open.

Another way:

   with [](..., "rb") as csvfile:

This doesn't work any better but it makes the error stand out.

Yet another way:

   avaa = open
   open = []
   with avaa(..., "rb") as csvfile:

That is, save the original value of open in another variable, which I
here called avaa.

The best way is to omit the whole assignment altogether. Were you
using the list called open for something?

Oh, one more way!

   with open(..., "rb") as csvfile:
  ...
   open = []

That is, only shoot yourself in the foot after the work has been done!
Though, really, best not do it at all. The page you referred to,
doesn't.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Finding size of Variable

2014-02-06 Thread wxjmfauth
Le mercredi 5 février 2014 12:44:47 UTC+1, Chris Angelico a écrit :
> On Wed, Feb 5, 2014 at 10:00 PM, Steven D'Aprano
> 
>  wrote:
> 
> >> where stopWords.txt is a file of size 4KB
> 
> >
> 
> > My guess is that if you split a 4K file into words, then put the words
> 
> > into a list, you'll probably end up with 6-8K in memory.
> 
> 
> 
> I'd guess rather more; Python strings have a fair bit of fixed
> 
> overhead, so with a whole lot of small strings, it will get more
> 
> costly.
> 
> 
> 
> >>> sys.version
> 
> '3.4.0b2 (v3.4.0b2:ba32913eb13e, Jan  5 2014, 16:23:43) [MSC v.1600 32
> 
> bit (Intel)]'
> 
> >>> sys.getsizeof("asdf")
> 
> 29
> 
> 
> 
> "Stop words" tend to be short, rather than long, words, so I'd look at
> 
> an average of 2-3 letters per word. Assuming they're separated by
> 
> spaces or newlines, that means there'll be roughly a thousand of them
> 
> in the file, for about 25K of overhead. A bit less if the words are
> 
> longer, but still quite a bit. (Byte strings have slightly less
> 
> overhead, 17 bytes apiece, but still quite a bit.)
> 
> 
> 
> ChrisA

>>> sum([sys.getsizeof(c) for c in ['a']])
26
>>> sum([sys.getsizeof(c) for c in ['a', 'a EURO']])
68
>>> sum([sys.getsizeof(c) for c in ['a', 'a EURO', 'aa EURO']])
112
>>> sum([sys.getsizeof(c) for c in ['a', 'a EURO', 'aa EURO', 'aaa EURO']])
158
>>> sum([sys.getsizeof(c) for c in ['a', 'a EURO', 'aa EURO', 'aaa EURO', 
>>> ' EURO']])
238
>>> 
>>> 
>>> sum([sys.getsizeof(c.encode('utf-32-be')) for c in ['a']])
21
>>> sum([sys.getsizeof(c.encode('utf-32-be')) for c in ['a', 'a EURO']])
46
>>> sum([sys.getsizeof(c.encode('utf-32-be')) for c in ['a', 'a EURO', 'aa 
>>> EURO']])
75
>>> sum([sys.getsizeof(c.encode('utf-32-be')) for c in ['a', 'a EURO', 'aa 
>>> EURO', 'aaa EURO']])
108
>>> sum([sys.getsizeof(c.encode('utf-32-be')) for c in ['a', 'a EURO', 'aa 
>>> EURO', 'aaa EURO', ' EURO']])
209
>>> 
>>> 
>>> sum([sys.getsizeof(c) for c in ['a', 'a EURO', 'aa EURO']*3])
336
>>> sum([sys.getsizeof(c) for c in ['aa EURO aa EURO']*3])
150
>>> sum([sys.getsizeof(c.encode('utf-32')) for c in ['a', 'a EURO', 'aa 
>>> EURO']*3])
261
>>> sum([sys.getsizeof(c.encode('utf-32')) for c in ['aa EURO aa EURO']*3])
135
>>>

jmf
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Finding size of Variable

2014-02-06 Thread Ned Batchelder

On 2/6/14 5:15 AM, wxjmfa...@gmail.com wrote:



sum([sys.getsizeof(c) for c in ['a', 'a EURO', 'aa EURO']*3])

336

sum([sys.getsizeof(c) for c in ['aa EURO aa EURO']*3])

150

sum([sys.getsizeof(c.encode('utf-32')) for c in ['a', 'a EURO', 'aa EURO']*3])

261

sum([sys.getsizeof(c.encode('utf-32')) for c in ['aa EURO aa EURO']*3])

135




jmf



JMF, we've told you I-don't-know-how-many-times to stop this. 
Seriously: think hard about what your purpose is in sending these absurd 
benchmarks.  I guarantee you are not accomplishing it.


--
Ned Batchelder, http://nedbatchelder.com

--
https://mail.python.org/mailman/listinfo/python-list


Re: parse a csv file into a text file

2014-02-06 Thread Dave Angel
 Zhen Zhang  Wrote in message:
> 

> I am currently running python 2.7.
> 
> Yes, i thought there must be a print function in python like fprint in C++ 
> that allows you to print into a file directly.
> But i google about "print string into text file" I got answers using 
> f.write() instead. :)
> -- 
> 
> 
In python 2.x,
 
Instead of 
   f.write (a + " " + b)
you can use
   print >> f, a, b



-- 
DaveA

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parse a csv file into a text file

2014-02-06 Thread Dave Angel
 Dave Angel  Wrote in message:
>  Zhen Zhang  Wrote in message:
>> 
> 
>> I am currently running python 2.7.
>> 
>> Yes, i thought there must be a print function in python like fprint in C++ 
>> that allows you to print into a file directly.
>> But i google about "print string into text file" I got answers using 
>> f.write() instead. :)
>> -- 
>> 
Oops. Forgot the newline. 

> In python 2.x,
>  
> Instead of 
>f.write (a + " " + b)
 f.write (a  + " " + b + "\n")
> you can use
>print >> f, a, b
> 

print will add in the space and newline,  just as it does to
 sys.stdout.


-- 
DaveA

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [OT] Usage of U+00B6 PILCROW SIGN (was: generator slides review and Python doc (+/- text bug))

2014-02-06 Thread Rustom Mody
On Tuesday, February 4, 2014 8:51:25 PM UTC+5:30, jmf wrote:

> Useless and really ugly.

Evidently one can do worse:

http://www.pip-installer.org/en/latest/installing.html#requirements
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parse a csv file into a text file

2014-02-06 Thread MRAB
On 2014-02-06 07:52, Zhen Zhang wrote:> On Wednesday, February 5, 2014 
7:33:00 PM UTC-5, Roy Smith wrote:

>> In article <5c268845-003f-4e24-b27a-c89e9fbfc...@googlegroups.com>,
>>  Zhen Zhang  wrote:
>>
>> > [code]
>> >
>> > import csv
>> > file = open('raw.csv')
>> > reader = csv.reader(file)
>> >
>> > f = open('NicelyDone.text','w')
>> >
>> > for line in reader:
>> >   f.write("%s %s"%line[1],%line[5])
>> >
>> > [/code]
>>
>> Are you using Python 2 or 3?
>>
>> > Here is my question:
>> > 1:What is the data format for line[1],
>>
>> That's something you can easily figure out by printing out the
>> intermediate values.  Try something like:
>>
>> > for line in reader:
>> >   print type(line[1]), repr(line(1))
>>
>> See if that prints what you expect.
>>
>> > how come f.write() does not work.
>>
>> What does "does not work" mean?  What does get written to the file?
>> Or do you get some sort of error?
>>
>> I'm pretty sure I see your error, but I'm trying to lead you to being
>> able to diagnose it yourself :-)
>
> Hi Roy ,
>
> Thank you so much for the reply,
> I am currenly running python 2.7
>
> i run the
>   print type(line[1]), repr(line(1))
> It tells me that 'list object is not callable
>
"line" is a list and within repr you're using (...) (parentheses)
instead of [...] (square brackets).

It might be clearer if you call the variable "row" because the CSV
reader returns rows, and each row is a list of strings.

> It seems the entire line is a data type of list instead of a data
> type of "line" as i thought.
>
> The line[1] is a string element of list after all.
>
> f.write("%s %s %s" %(output,location,output))works great,
> as MRAB mentioned, I have to do write it in term of tuples.
>
> This is the code I am currently using
>
> for line in reader:
>   location ="%s"%(line[1])
>   if '(' in location:
>  # at this point, bits = ['Toronto ', 'Ont.)']
>  bits = location.split('(')
>  location = bits[0].strip()
>   output = "%s %s\n" %(location,line[5])
>   f.write("%s" %(output))
>
A 1-tuple (a tuple containing one item) is:

(item, )

It's actually the comma that makes it a tuple (except for the 0-tuple
"()"); it's just that it's often necessary to wrap it in (...), and
people then think it's those that are making it a tuple, but it's not!

> It extracts desired information into a text file as i wanted.
> however, the python program gives me a Error after the execution.
>   location="%s"%(line[1])
>   IndexError: list index out of range
>
> I failed to figure out why.
>
What is the value of "line" at that point?
--
https://mail.python.org/mailman/listinfo/python-list


Re: parse a csv file into a text file

2014-02-06 Thread Rustom Mody
On Thursday, February 6, 2014 6:46:37 PM UTC+5:30, MRAB wrote:
> 
> It's actually the comma that makes it a tuple (except for the 0-tuple
> "()"); it's just that it's often necessary to wrap it in (...), and
> people then think it's those that are making it a tuple, but it's not!

Interesting viewpoint -- didn't know that!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [OT] Usage of U+00B6 PILCROW SIGN (was: generator slides review and Python doc (+/- text bug))

2014-02-06 Thread wxjmfauth
Le jeudi 6 février 2014 13:23:03 UTC+1, Rustom Mody a écrit :
> On Tuesday, February 4, 2014 8:51:25 PM UTC+5:30, jmf wrote:
> 
> 
> 
> > Useless and really ugly.
> 
> 
> 
> Evidently one can do worse:
> 
> 
> 
> http://www.pip-installer.org/en/latest/installing.html#requirements

or http://cx-freeze.readthedocs.org/en/latest/index.html
-- 
https://mail.python.org/mailman/listinfo/python-list


python and matlab

2014-02-06 Thread Sam
is it able to utilize functions written in Python in Matlab?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Finding size of Variable

2014-02-06 Thread wxjmfauth
Le jeudi 6 février 2014 12:10:08 UTC+1, Ned Batchelder a écrit :
> On 2/6/14 5:15 AM, wxjmfa...@gmail.com wrote:
> 
> 
> 
> 
> 
>  sum([sys.getsizeof(c) for c in ['a', 'a EURO', 'aa EURO']*3])
> 
> > 336
> 
>  sum([sys.getsizeof(c) for c in ['aa EURO aa EURO']*3])
> 
> > 150
> 
>  sum([sys.getsizeof(c.encode('utf-32')) for c in ['a', 'a EURO', 'aa 
>  EURO']*3])
> 
> > 261
> 
>  sum([sys.getsizeof(c.encode('utf-32')) for c in ['aa EURO aa EURO']*3])
> 
> > 135
> 
> 
> 
> >
> 
> > jmf
> 
> >
> 
> 
> 
> JMF, we've told you I-don't-know-how-many-times to stop this. 
> 
> Seriously: think hard about what your purpose is in sending these absurd 
> 
> benchmarks.  I guarantee you are not accomplishing it.
> 
> 
> 
> -- 
> 
> Ned Batchelder, http://nedbatchelder.com

Sorry, I'm only pointing you may lose memory when
working with short strings as it was explained.
I really, very really, do not see what is absurd
or obsure in:

>>> sys.getsizeof('abc' + 'EURO')
46
>>> sys.getsizeof(('abc' + 'EURO').encode('utf-32'))
37

I apologize for the " a EURO" which should have
been a real "EURO". No idea, what's happend.

jmf

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python and matlab

2014-02-06 Thread Sturla Molden
Sam  wrote:
> is it able to utilize functions written in Python in Matlab?

Yes, if you embed the Python interpreter in a MEX-file.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parse a csv file into a text file

2014-02-06 Thread Neil Cerutti
On 2014-02-06, Zhen Zhang  wrote:
> Hi, every one.
>
> I am a second year EE student.
> I just started learning python for my project.
>
> I intend to parse a csv file with a format like 
>
> 3520005,"Toronto (Ont.)",C > 
> ,F,2503281,2481494,F,F,0.9,1040597,979330,630.1763,3972.4,1
[...]
 into a text file like the following
>
> Toronto 2503281
[...]
> This is what i have so far.
>
>
> [code]
>
> import csv
> file = open('raw.csv')

You must open the file in binary mode, as that is what the csv
module expects in Python 2.7. newline handling can be enscrewed
if you forget.

file = open('raw.csv', 'b')

-- 
Neil Cerutti

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Finding size of Variable

2014-02-06 Thread wxjmfauth
Some mysterious problem with the "euro".
Let's take a real "French" char.
>>> sys.getsizeof('abc' + 'œ')
46
>>> sys.getsizeof(('abc' + 'œ').encode('utf-32'))
37

or a "German" char, ẞ

>>> sys.getsizeof('abc' + '\N{LATIN CAPITAL LETTER SHARP S}')
46
>>> sys.getsizeof(('abc' + '\N{LATIN CAPITAL LETTER SHARP S}').encode('utf-32'))
37



-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python and matlab

2014-02-06 Thread Sam Adams
On Thursday, February 6, 2014 8:55:09 AM UTC-5, Sturla Molden wrote:
> Sam  wrote:
> 
> > is it able to utilize functions written in Python in Matlab?
> 
> 
> 
> Yes, if you embed the Python interpreter in a MEX-file.

Thanks Sturla, could you please explain in more details, I am new to Python :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to reduce bugs due to incorrect indentation

2014-02-06 Thread Grant Edwards
On 2014-02-06, msus...@gmail.com  wrote:
> I had a bug in a Python script recently. The code in question was something 
> along the lines of:
>
> if a == 1:
> x = y
> else:
> x = z
> y = z + y
> z = z + 1
>
> While editing this file I accidentally pushed TAB on the line 
> with 'y = z + y'.
>
> Any suggestion on how to avoid this type of error in the future?

The best advice is to pay closer attention to what you're doing.  Look
at the code while you're editing, not your fingers.  Before you commit
the change, spend some time looking carefully at it to verify that
changes were intentional.

-- 
Grant Edwards   grant.b.edwardsYow! I left my WALLET in
  at   the BATHROOM!!
  gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [OT] Usage of U+00B6 PILCROW SIGN (was: generator slides review and Python doc (+/- text bug))

2014-02-06 Thread Chris Angelico
On Thu, Feb 6, 2014 at 11:23 PM, Rustom Mody  wrote:
> On Tuesday, February 4, 2014 8:51:25 PM UTC+5:30, jmf wrote:
>
>> Useless and really ugly.
>
> Evidently one can do worse:
>
> http://www.pip-installer.org/en/latest/installing.html#requirements

Aside from using a little "chain link" icon rather than a
typographer's symbol, that looks exactly the same. How's it worse?

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parse a csv file into a text file

2014-02-06 Thread Mark Lawrence

On 06/02/2014 14:02, Neil Cerutti wrote:


You must open the file in binary mode, as that is what the csv
module expects in Python 2.7. newline handling can be enscrewed
if you forget.

file = open('raw.csv', 'b')



I've never opened a file in binary mode to read with the csv module 
using any Python version.  Where does it state that you must do this?


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

--
https://mail.python.org/mailman/listinfo/python-list


Re: parse a csv file into a text file

2014-02-06 Thread Tim Chase
On 2014-02-06 17:40, Mark Lawrence wrote:
> On 06/02/2014 14:02, Neil Cerutti wrote:
> >
> > You must open the file in binary mode, as that is what the csv
> > module expects in Python 2.7. newline handling can be enscrewed
> > if you forget.
> >
> > file = open('raw.csv', 'b')
> >
> 
> I've never opened a file in binary mode to read with the csv module 
> using any Python version.  Where does it state that you must do
> this?

While the docs don't currently say anything about it, all the
examples at [1] use 'rb' or 'wb' when opening the file.  I've long
wondered about that.  Especially as I've passed non-file objects like
lists/iterators to the csv.reader/csv.DictReader and had them work
just fine (and would be a little perturbed if they broke).

-tkc

[1] http://docs.python.org/2/library/csv.html
-- 
https://mail.python.org/mailman/listinfo/python-list


Using virtualenv to bypass sudoer issues

2014-02-06 Thread Jean-Michel Pichavant
Greetings, 


Assuming I have a debian workstation for which I don't have any sudo rights, i 
n order to be able to install / remove python packages, should I be using 
virtualenv ? Is it a suited solution ? 


JM 







-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parse a csv file into a text file

2014-02-06 Thread Tim Golden

On 06/02/2014 17:40, Mark Lawrence wrote:

On 06/02/2014 14:02, Neil Cerutti wrote:


You must open the file in binary mode, as that is what the csv
module expects in Python 2.7. newline handling can be enscrewed
if you forget.

file = open('raw.csv', 'b')



I've never opened a file in binary mode to read with the csv module
using any Python version.  Where does it state that you must do this?



If you don't, you tend to get interleaved blank lines. (Presumably 
unless your .csv is using \n-only linefeeds).


TJG
--
https://mail.python.org/mailman/listinfo/python-list


Logging from a multiprocess application

2014-02-06 Thread Joseph L. Casale
I have a module that has one operation that benefits greatly from being 
multiprocessed.
Its a console based module and as such I have a stream handler and filter 
associated to
the console, obviously the mp based instances need special handling, so I have 
been
experimenting with a socket server in a thread in order for the remaining 
application to
carry on.

How have others tackled this problem? The portion of the code made to use 
multiprocessing
can not be switched to threading as it performs worse than simply serializing 
each task.

Thanks,
jlc
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parse a csv file into a text file

2014-02-06 Thread Neil Cerutti
On 2014-02-06, Tim Chase  wrote:
> On 2014-02-06 17:40, Mark Lawrence wrote:
>> On 06/02/2014 14:02, Neil Cerutti wrote:
>> >
>> > You must open the file in binary mode, as that is what the csv
>> > module expects in Python 2.7. newline handling can be enscrewed
>> > if you forget.
>> >
>> > file = open('raw.csv', 'b')
>> >
>> 
>> I've never opened a file in binary mode to read with the csv module 
>> using any Python version.  Where does it state that you must do
>> this?
>
> While the docs don't currently say anything about it, all the
> examples at [1] use 'rb' or 'wb' when opening the file.  I've
> long wondered about that.  Especially as I've passed non-file
> objects like lists/iterators to the csv.reader/csv.DictReader
> and had them work just fine (and would be a little perturbed if
> they broke).

They do actually mention it.

From: http://docs.python.org/2/library/csv.html

  csv.reader(csvfile, dialect='excel', **fmtparams)

  Return a reader object which will iterate over lines in the
  given csvfile. csvfile can be any object which supports the
  iterator protocol and returns a string each time its next()
  method is called — file objects and list objects are both
  suitable. If csvfile is a file object, it must be opened with
  the ‘b’ flag on platforms where that makes a difference. 

So it's stipulated only for file objects on systems where it
might make a difference.

-- 
Neil Cerutti

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parse a csv file into a text file

2014-02-06 Thread Tim Chase
[first, it looks like you're posting via Google Groups which
annoyingly double-spaces everything in your reply. It's possible to
work around this, but you might want to subscribe via email or an
actual newsgroup client.  You can read more at
https://wiki.python.org/moin/GoogleGroupsPython ]

On 2014-02-06 00:07, Zhen Zhang wrote:
> Does the split make a list or tuple?

In this case, it happens to return a list, which you can check with

  print type("one two three".split())

However, also in this case, it doesn't matter, since either indexes
just fine.

> when  i do location=line[1],
> it gives me a error even though the program did run correctly and
> output the correct file. location=line[1]
>  IndexError: list index out of range 

Then it looks like you've got a blank line that doesn't actually have
data in it, so when it tries index into it, the only thing there is
[0], not [1].  As the message suggests :)

-tkc


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parse a csv file into a text file

2014-02-06 Thread Tim Chase
On 2014-02-06 18:34, Neil Cerutti wrote:
> They do actually mention it.
> 
> From: http://docs.python.org/2/library/csv.html
> 
>   If csvfile is a file object, it must be opened with
>   the ‘b’ flag on platforms where that makes a difference. 
> 
> So it's stipulated only for file objects on systems where it
> might make a difference.

Ah, I *knew* I'd read that somewhere but my searches in firefox (for
"binary", "rb" and "wb") didn't manage to catch that particular
instance.  Thanks for disinterring that.

-tkc


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to reduce bugs due to incorrect indentation

2014-02-06 Thread msustik
Thanks for all the suggestions!
Best,
-Matyas
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [OT] Usage of U+00B6 PILCROW SIGN

2014-02-06 Thread Robert Kern

On 2014-02-06 17:23, Chris Angelico wrote:

On Thu, Feb 6, 2014 at 11:23 PM, Rustom Mody  wrote:

On Tuesday, February 4, 2014 8:51:25 PM UTC+5:30, jmf wrote:


Useless and really ugly.


Evidently one can do worse:

http://www.pip-installer.org/en/latest/installing.html#requirements


Aside from using a little "chain link" icon rather than a
typographer's symbol, that looks exactly the same. How's it worse?


When I looked at it earlier today, I got a default "cannot find this glyph" box 
instead of the chain icon. I assumed that is what Rustom was referring to. It's 
working for me now.


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

--
https://mail.python.org/mailman/listinfo/python-list


Re: Logging from a multiprocess application

2014-02-06 Thread Mark Betz
On Thursday, February 6, 2014 1:24:17 PM UTC-5, Joseph L. Casale wrote:
> I have a module that has one operation that benefits greatly from being 
> multiprocessed.
> Its a console based module and as such I have a stream handler and filter 
> associated to
> the console, obviously the mp based instances need special handling, so I 
> have been
> experimenting with a socket server in a thread in order for the remaining 
> application to
> carry on.
> 
> How have others tackled this problem? The portion of the code made to use 
> multiprocessing
> can not be switched to threading as it performs worse than simply serializing 
> each task.
> 
> Thanks,
> jlc

Maybe check out logstash (http://logstash.net/). It's part of elastic search 
now. Using the python-logstash module 
(https://pypi.python.org/pypi/python-logstash) you can either log directly to 
the logstash daemon using the provided event formatter and http, or you can do 
as I did and set up a redis server to host a list. Logstash has an included 
redis input plugin that will blpop a list to get events. I use the formatter 
from the logstash module to format the events and then write them into redis as 
json. Logstash picks them up from redis and indexes them into its embedded 
elastic search instance. You can then connect to the embedded kibana web server 
to view a dashboard with event information. It's pretty cool and doesn't take 
long to set up.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to reduce bugs due to incorrect indentation

2014-02-06 Thread Roel Schroeven

msus...@gmail.com schreef:

I had a bug in a Python script recently. The code in question was something 
along the lines of:

if a == 1:
x = y
else:
x = z
y = z + y
z = z + 1

While editing this file I accidentally pushed TAB on the line with 'y = z + y'.

My changes were elsewhere and I did not notice the above one line change when I 
looked at the diffs before commit. I should have noticed it...

It was rare that a was 1 and therefore the problem did not show up for a while. 
(I know I should have had tests exercising all cases...)

When the bug showed up, it was kind of difficult to remember what was the 
original intent. Fortunately, looking at old versions allowed me to find the 
problem commit and the bug.

Any suggestion on how to avoid this type of error in the future?


My suggestion: configure your editor to insert the appropriate amount of 
spaces instead of a tab when you press the tab key.



Best regards,
Roel

--
"Met een spitsvondig citaat bewijs je niets."
-- Voltaire

--
https://mail.python.org/mailman/listinfo/python-list


Re: how to reduce bugs due to incorrect indentation

2014-02-06 Thread Larry Martell
On Thu, Feb 6, 2014 at 3:29 PM, Roel Schroeven  wrote:
> msus...@gmail.com schreef:
>>
>> I had a bug in a Python script recently. The code in question was
>> something along the lines of:
>>
>> if a == 1:
>> x = y
>> else:
>> x = z
>> y = z + y
>> z = z + 1
>>
>> While editing this file I accidentally pushed TAB on the line with 'y = z
>> + y'.
>>
>> My changes were elsewhere and I did not notice the above one line change
>> when I looked at the diffs before commit. I should have noticed it...
>>
>> It was rare that a was 1 and therefore the problem did not show up for a
>> while. (I know I should have had tests exercising all cases...)
>>
>> When the bug showed up, it was kind of difficult to remember what was the
>> original intent. Fortunately, looking at old versions allowed me to find the
>> problem commit and the bug.
>>
>> Any suggestion on how to avoid this type of error in the future?
>
>
> My suggestion: configure your editor to insert the appropriate amount of
> spaces instead of a tab when you press the tab key.

+1 - tabs are evil.
-- 
https://mail.python.org/mailman/listinfo/python-list


ANN: A new version (0.3.6) of python-gnupg has been released.

2014-02-06 Thread Vinay Sajip
A new version of the Python module which wraps GnuPG has been
released.

What Changed?
=
This is an enhancement and bug-fix release, but the bug-fixes
include some security improvements, so all users are encouraged
to upgrade. See the project website ( http://code.google.com/p/python-gnupg/ )
for more information. Summary:

Enabled fast random tests on gpg as well as gpg2.
Avoided deleting temporary file to preserve its permissions.
Avoided writing passphrase to log.
Added export-minimal and armor options when exporting keys.
Added verify_data() method to allow verification of signatures in memory.
Regularised end-of-line characters in ths source code.
Rectified problems with earlier fix for shell injection.

The current version passes all tests on Windows (CPython 2.4, 2.5,
2.6, 3.1, 2.7 and Jython 2.5.1) and Ubuntu (CPython 2.4, 2.5, 2.6,
2.7, 3.0, 3.1, 3.2). On Windows, GnuPG 1.4.11 has been used for the
tests.

What Does It Do?

The gnupg module allows Python programs to make use of the
functionality provided by the Gnu Privacy Guard (abbreviated GPG or
GnuPG). Using this module, Python programs can encrypt and decrypt
data, digitally sign documents and verify digital signatures, manage
(generate, list and delete) encryption keys, using proven Public Key
Infrastructure (PKI) encryption technology based on OpenPGP.

This module is expected to be used with Python versions >= 2.4, as it
makes use of the subprocess module which appeared in that version of
Python. This module is a newer version derived from earlier work by
Andrew Kuchling, Richard Jones and Steve Traugott.

A test suite using unittest is included with the source distribution.

Simple usage:

>>> import gnupg
>>> gpg = gnupg.GPG(gnupghome='/path/to/keyring/directory')
>>> gpg.list_keys()
[{
  ...
  'fingerprint': 'F819EE7705497D73E3CCEE65197D5DAC68F1AAB2',
  'keyid': '197D5DAC68F1AAB2',
  'length': '1024',
  'type': 'pub',
  'uids': ['', 'Gary Gross (A test user) ']},
 {
  ...
  'fingerprint': '37F24DD4B918CC264D4F31D60C5FEFA7A921FC4A',
  'keyid': '0C5FEFA7A921FC4A',
  'length': '1024',
  ...
  'uids': ['', 'Danny Davis (A test user) ']}]
>>> encrypted = gpg.encrypt("Hello, world!", ['0C5FEFA7A921FC4A'])
>>> str(encrypted)
'-BEGIN PGP MESSAGE-\nVersion: GnuPG v1.4.9 (GNU/Linux)\n
\nhQIOA/6NHMDTXUwcEAf
...
-END PGP MESSAGE-\n'
>>> decrypted = gpg.decrypt(str(encrypted), passphrase='secret')
>>> str(decrypted)
'Hello, world!'
>>> signed = gpg.sign("Goodbye, world!", passphrase='secret')
>>> verified = gpg.verify(str(signed))
>>> print "Verified" if verified else "Not verified"
'Verified'

For more information, visit http://code.google.com/p/python-gnupg/ -
as always, your feedback is most welcome (especially bug reports,
patches and suggestions for improvement). Enjoy!

Cheers

Vinay Sajip
Red Dove Consultants Ltd.

-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Logging from a multiprocess application

2014-02-06 Thread Joseph L. Casale
> Maybe check out logstash (http://logstash.net/).

That looks pretty slick, I am constrained to using something provided by the 
packaged modules
in this scenario.

I think I have it pretty close except for the fact that the 
LogRecordStreamHandler from the cookbook
excepts when the sending process ends, aside from the obvious, I am sure there 
is a more robust
performance oriented approach to this. I've yet to work with the SocketServer 
module so I am lacking
any experience.

Thanks for the pointer,
jlc
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ANN: A new version (0.3.6) of python-gnupg has been released.

2014-02-06 Thread Asaf Las
On Thursday, February 6, 2014 10:53:59 PM UTC+2, Vinay Sajip wrote:
> A new version of the Python module which wraps GnuPG has been
> released.
> Cheers
> 
> Vinay Sajip
> 
> Red Dove Consultants Ltd.

Hi

Good job!
 
One question - is this package runs executable when particular function 
must be invoked in GnuPG? library calls?

Regards

Asaf
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to reduce bugs due to incorrect indentation

2014-02-06 Thread Ethan Furman

On 02/06/2014 12:36 PM, Larry Martell wrote:

On Thu, Feb 6, 2014 at 3:29 PM, Roel Schroeven wrote:

msus...@gmail.com schreef:


While editing this file I accidentally pushed TAB on the line with 'y = z
+ y'.


My suggestion: configure your editor to insert the appropriate amount of
spaces instead of a tab when you press the tab key.


+1 - tabs are evil.


Tabs are not evil, and an argument can be made that tabs are better (a decent editor can be configured to show x many 
spaces per tab, then users could decide how much indentation they preferred to see... but I digress).


Using spaces instead of tabs would also have not prevented the error that Msustik encountered, and for that matter we 
don't know whether he was using tabs or spaces in his source file, only that he hit the Tab key -- surely you are not 
suggesting everyone rip out their tab key and just hit the space bar four times for each level of indentation?  ;)


--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list


Re: how to reduce bugs due to incorrect indentation

2014-02-06 Thread Larry Martell
On Thu, Feb 6, 2014 at 4:32 PM, Ethan Furman  wrote:
> On 02/06/2014 12:36 PM, Larry Martell wrote:
>>
>> On Thu, Feb 6, 2014 at 3:29 PM, Roel Schroeven wrote:
>>>
>>> msus...@gmail.com schreef:


 While editing this file I accidentally pushed TAB on the line with 'y =
 z
 + y'.
>>>
>>>
>>> My suggestion: configure your editor to insert the appropriate amount of
>>> spaces instead of a tab when you press the tab key.
>>
>>
>> +1 - tabs are evil.
>
>
> Tabs are not evil, and an argument can be made that tabs are better (a
> decent editor can be configured to show x many spaces per tab, then users
> could decide how much indentation they preferred to see... but I digress).
>
> Using spaces instead of tabs would also have not prevented the error that
> Msustik encountered, and for that matter we don't know whether he was using
> tabs or spaces in his source file, only that he hit the Tab key -- surely
> you are not suggesting everyone rip out their tab key and just hit the space
> bar four times for each level of indentation?  ;)

The Tab key is not evil, it's the tab character (Ctrl-I). I have been
bitten by this many time when I had to work on a program written by
another. They had their tab stops set at 5 or 6, mine is set at 4, or
they did not have expandtab set, but I did. So you get either a script
that looks misaligned, but works, or one that does not look misaligned
but doesn't work. When I have to pick up someone else's script the
first thing I do is replace the tabs with spaces.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to reduce bugs due to incorrect indentation

2014-02-06 Thread Chris Angelico
On Fri, Feb 7, 2014 at 9:09 AM, Larry Martell  wrote:
> The Tab key is not evil, it's the tab character (Ctrl-I). I have been
> bitten by this many time when I had to work on a program written by
> another. They had their tab stops set at 5 or 6, mine is set at 4, or
> they did not have expandtab set, but I did. So you get either a script
> that looks misaligned, but works, or one that does not look misaligned
> but doesn't work. When I have to pick up someone else's script the
> first thing I do is replace the tabs with spaces.

All you've proven is that *mixing* spaces and tabs is evil. It's like
arguing that oil is evil because, when you mix it with water, weird
stuff happens. But that doesn't mean I want to fry my bacon in water.

Mmm, bacon.

Sorry. I'm back now. Ahem. Arguably, a better fix is to replace spaces
with tabs, because they're more obvious. But mainly, just be
consistent. Whatever one file uses, it uses exclusively. It'd be
pretty easy to create a git commit hook that checks files for leading
indentation and rejects the commit if it's mismatched; I would guess
the same is true in Mercurial.

But none of this would solve the OP's original issue. Whether it's a
tab or spaces, unexpectedly indenting a line of code is a problem.
It's no different from accidentally hitting Ctrl-T in SciTE and
reordering two lines, when one line depends on the other. It's a bug.
So you look at your commits before you make them (to give yourself a
chance to catch it quickly), and you make sure you can always look
back over your commits (in case you didn't catch it quickly). Much
better than blaming the characters involved. Poor innocent U+0009.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to reduce bugs due to incorrect indentation

2014-02-06 Thread Asaf Las
On Friday, February 7, 2014 12:30:17 AM UTC+2, Chris Angelico wrote:
> On Fri, Feb 7, 2014 at 9:09 AM, Larry Martell  wrote:
> 
> > The Tab key is not evil, it's the tab character (Ctrl-I). I have been
> > bitten by this many time when I had to work on a program written by
> > another. They had their tab stops set at 5 or 6, mine is set at 4, or
> > they did not have expandtab set, but I did. So you get either a script
> > that looks misaligned, but works, or one that does not look misaligned
> > but doesn't work. When I have to pick up someone else's script the
> > first thing I do is replace the tabs with spaces.
> 
> All you've proven is that *mixing* spaces and tabs is evil. It's like
> arguing that oil is evil because, when you mix it with water, weird
> stuff happens. But that doesn't mean I want to fry my bacon in water.
> Mmm, bacon.
> Sorry. I'm back now. Ahem. Arguably, a better fix is to replace spaces
> with tabs, because they're more obvious. But mainly, just be
> consistent. Whatever one file uses, it uses exclusively. It'd be
> pretty easy to create a git commit hook that checks files for leading
> indentation and rejects the commit if it's mismatched; I would guess
> the same is true in Mercurial.
> 
> But none of this would solve the OP's original issue. Whether it's a
> tab or spaces, unexpectedly indenting a line of code is a problem.
> It's no different from accidentally hitting Ctrl-T in SciTE and
> reordering two lines, when one line depends on the other. It's a bug.
> So you look at your commits before you make them (to give yourself a
> chance to catch it quickly), and you make sure you can always look
> back over your commits (in case you didn't catch it quickly). Much
> better than blaming the characters involved. Poor innocent U+0009.
> ChrisA

pep8 pushed \t to dark side in Python. 

though it is better that spaces sometimes. let say someone's indented code
with 2 spaces and user is comfortable with 4. if \t then it is done by 
editor's conf without touching code.

/Asaf
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ANN: A new version (0.3.6) of python-gnupg has been released.

2014-02-06 Thread Piet van Oostrum
Vinay Sajip  writes:

> A new version of the Python module which wraps GnuPG has been
> released.
>
There seem to be 2 gnupg modules for Python. The other one has version number 
1.2.5. Very confusing!
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Question about `list.insert`

2014-02-06 Thread cool-RR
Hi,

I'm curious. If I append an item to a list from the left using `list.insert`, 
will Python always move the entire list one item to the right (which can be 
super-slow) or will it check first to see whether it can just allocate more 
memory to the left of the list and put the item there, saving a lot of 
resources?


Thanks,
Ram.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to reduce bugs due to incorrect indentation

2014-02-06 Thread Chris Angelico
On Fri, Feb 7, 2014 at 10:01 AM, Asaf Las  wrote:
> pep8 pushed \t to dark side in Python.

Only for the Python stdlib, and only because a decision has to be made
one way or the other. I believe Guido stated at one point that there
was only a very weak push toward "spaces only" rather than "tabs
only", just that it had to be one of those.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about `list.insert`

2014-02-06 Thread Terry Reedy

On 2/6/2014 6:59 PM, cool-RR wrote:

Hi,

I'm curious. If I append an item to a list from the left using
`list.insert`, will Python always move the entire list one item to
the right (which can be super-slow) or will it check first to see
whether it can just allocate more memory to the left of the list and
put the item there, saving a lot of resources?


"It depends on the implementation"

Assume O(n), which I am sure it is for CPython,  but easy enough to check.

--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Re: Question about `list.insert`

2014-02-06 Thread MRAB

On 2014-02-06 23:59, cool-RR wrote:

Hi,

I'm curious. If I append an item to a list from the left using
`list.insert`, will Python always move the entire list one item to
the right (which can be super-slow) or will it check first to see
whether it can just allocate more memory to the left of the list and
put the item there, saving a lot of resources?


If it needs more space it resizes. It then moves the items.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7.6 help with white spaces?

2014-02-06 Thread Roy Smith
In article ,
 Scott W Dunning  wrote:

> I am having trouble figuring out how to remove spacesŠ.
> 
> Assume variables exist for minutes and seconds. Each variable is an integer. 
> How would you create a string in the format,
> 
> 3:11
> 
> with no spaces. where 3 is minutes and 11 is seconds.
> 
> 
> Obviously when IŠ
> 
> print minutes, ³:², seconds
> 
> I get 3 : 11
> 
> how can I get it to print with no spaces?   
> 
> 3:11

print "%d:%02d" % (minutes, seconds)

The "02" for the seconds specifier makes sure you get exactly two 
digits, with a leading zero if needed.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about `list.insert`

2014-02-06 Thread Asaf Las
On Friday, February 7, 2014 5:00:56 AM UTC+2, Roy Smith wrote:
> In article ,
> 
>  Dave Angel wrote:
> > list does not promise better than O(1) behavior
> I'm not aware of any list implementations, in any language, that 
> promises better than O(1) behavior for any operations.  Perhaps there is 
> O(j), where you just imagine the operation was performed?

Archimedes once said "Give me whereon to stand and I will move the earth"
he said that for lists, specifically he meant "give me enough RAM and 
list will perform O(1)." 

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about `list.insert`

2014-02-06 Thread Rustom Mody
On Friday, February 7, 2014 8:44:43 AM UTC+5:30, Chris Angelico wrote:
> On Fri, Feb 7, 2014 at 2:00 PM, Roy Smith  wrote:
> >  Dave Angel wrote:
> >> list does not promise better than O(1) behavior
> > I'm not aware of any list implementations, in any language, that
> > promises better than O(1) behavior for any operations.  Perhaps there is
> > O(j), where you just imagine the operation was performed?

> I have a printer that executes in O(1/N) time, where N is the number
> of marbles the sysadmin (me!) has lost. The less sanity I have, the
> more printouts it produces. And the less printouts it produces, the
> more marbles I lose trying to figure out WHY? WHY? WHY?!?

Heh! Nice to know I have company!

Thought I was the only one who lost hair at the printer's
free-paper-munificience
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about `list.insert`

2014-02-06 Thread Rustom Mody
On Friday, February 7, 2014 8:30:56 AM UTC+5:30, Roy Smith wrote:
>  Dave Angel wrote:
> 
> > list does not promise better than O(1) behavior
> 
> I'm not aware of any list implementations, in any language, that 
> promises better than O(1) behavior for any operations.  Perhaps there is 
> O(j), where you just imagine the operation was performed?

I believe Oracle has such a product on offer
The price too is by imagination
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about `list.insert`

2014-02-06 Thread Chris Angelico
On Fri, Feb 7, 2014 at 2:00 PM, Roy Smith  wrote:
> In article ,
>  Dave Angel  wrote:
>
>> list does not promise better than O(1) behavior
>
> I'm not aware of any list implementations, in any language, that
> promises better than O(1) behavior for any operations.  Perhaps there is
> O(j), where you just imagine the operation was performed?

I have a printer that executes in O(1/N) time, where N is the number
of marbles the sysadmin (me!) has lost. The less sanity I have, the
more printouts it produces. And the less printouts it produces, the
more marbles I lose trying to figure out WHY? WHY? WHY?!?

Okay, I'm done ranting about Windows and 1990s accounting packages and
modern PostScript printers.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about `list.insert`

2014-02-06 Thread Chris Angelico
On Fri, Feb 7, 2014 at 2:29 PM, Rustom Mody  wrote:
> On Friday, February 7, 2014 8:44:43 AM UTC+5:30, Chris Angelico wrote:
>> On Fri, Feb 7, 2014 at 2:00 PM, Roy Smith  wrote:
>> >  Dave Angel wrote:
>> >> list does not promise better than O(1) behavior
>> > I'm not aware of any list implementations, in any language, that
>> > promises better than O(1) behavior for any operations.  Perhaps there is
>> > O(j), where you just imagine the operation was performed?
>
>> I have a printer that executes in O(1/N) time, where N is the number
>> of marbles the sysadmin (me!) has lost. The less sanity I have, the
>> more printouts it produces. And the less printouts it produces, the
>> more marbles I lose trying to figure out WHY? WHY? WHY?!?
>
> Heh! Nice to know I have company!
>
> Thought I was the only one who lost hair at the printer's
> free-paper-munificience

See, here's the deal. I have a nice, modern, CUPS-based printer. It
supports all the modern protocols (IPP, JetDirect, whatever), and when
I point one of my Linux boxes in its direction and say "Print", a
sheet of paper comes out in fairly short order. Nice, right? Okay, now
let's mix in the problem ingredients.

We have an ancient accounting package, supporting a rather old and
winding-down business. It's not worth upgrading to a newer version of
the program (that costs money), and certainly not worth migrating to a
completely different program (that takes time), because the business
just isn't operating at that level any more. It's a Windows 16-bit
application.

The physical hardware is decently modern, and is running Debian Jessie
(the latest, not even stable yet, because I wanted something else
relating to scanning - that's a separate point). Debian is running
VirtualBox, and inside the VM is running OS/2 (eComStation). OS/2 will
run the old accounting package in a Win-OS/2 session.

Now, OS/2 will talk to the printer. I can fire up DeScribe Word
Processor, type in some text, hit Print, and a sheet of paper comes
out with a representation of that text. The recent versions of OS/2
are quite good at that. But Windows? Windows? Oh no. It won't be that
courteous. No, it claims to have printed the document, and everything
I can see suggests that the data has passed through on its way to the
printer, but no paper comes out. My best guess is that there's some
flaw in the PostScript engine in Windows, because I can generate
output successfully by a complex dance involving freezing the
printer's output, printing from Windows, printing an unrelated
document (anything at all) from OS/2, and then releasing all the
printed data at once.

Current suggestions awaiting experimentation including slaughtering a
black goat at midnight, waving a rubber chicken, and throwing salt
over my shoulder in the direction of the full moon.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Python 2.7.6 help with white spaces?

2014-02-06 Thread Scott W Dunning
I am having trouble figuring out how to remove spaces….

Assume variables exist for minutes and seconds. Each variable is an integer. 
How would you create a string in the format,

3:11

with no spaces. where 3 is minutes and 11 is seconds.


Obviously when I…

print minutes, “:”, seconds

I get 3 : 11

how can I get it to print with no spaces?   

3:11

I’m VERY new to python and coding in general and this is a question for a class 
I’m in for a test review.  So, the most basic answer would be appreciated.  

Thanks for any help!!

Scott


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to reduce bugs due to incorrect indentation

2014-02-06 Thread msustik
On Thursday, February 6, 2014 12:29:36 PM UTC-8, Roel Schroeven wrote:
> 
> My suggestion: configure your editor to insert the appropriate amount of 
> 
> spaces instead of a tab when you press the tab key.

You misunderstood the problem, but managed to start a Tab war! :-)

My emacs inserts 4 spaces in python mode when I press the tab key. Python uses 
the indentation to decide how many lines following the else are executed in the 
else branch or after the else branch (always that is).

By pressing inadvertently the Tab key I changed the semantics of the code while 
it is still syntactically correct and PEP8 compliant. This was indeed a user 
error and my question was towards practices reducing the chance of this 
happening again.

Based on the responses I arrived to the conclusion that there is no better 
solution than trying to be careful and have good testing suites.

It would be possible to disable the Tab key completely and type in the spaces 
all the time. (It is much less likely that one would press the space bar 
accidentally four times or hold it down to get 4 spaces by mistake.)

Unfortunately this means giving up the indentation help of the editor and that 
will slow down coding. It will also lead to many indentation mistakes during 
development (most of which will be caught right away however. Maybe a coloring 
of the background based on tab position could assist in this.

I also considered adding an extra blank line after the if-else block (similarly 
for loops) in the hope that it would reduce the chance of missing an 
inadvertent indentation after the block.

However, this defeats somewhat the python paradigm that got rid of closing 
braces and endif-s etc. used in other languages to allow more compact code.

-Matyas


> 
> 
> 
> 
> 
> Best regards,
> 
> Roel
> 
> 
> 
> -- 
> 
> "Met een spitsvondig citaat bewijs je niets."
> 
>  -- Voltaire

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7.6 help with white spaces?

2014-02-06 Thread Scott W Dunning
Is this what you’re talking about?  

minutes = “3”
seconds = “11”

print int(minutes), ”:" int(seconds)

That’s what you mean by turning a string into an int right?  Not sure how to 
add strings together though.



On Feb 6, 2014, at 6:37 PM, Chris Angelico  wrote:

> On Fri, Feb 7, 2014 at 12:22 PM, Scott W Dunning  wrote:
>> Assume variables exist for minutes and seconds. Each variable is an integer.
>> How would you create a string in the format,
>> 
>> 3:11
>> 
>> with no spaces. where 3 is minutes and 11 is seconds.
>> 
>> 
>> Obviously when I…
>> 
>> print minutes, “:”, seconds
>> 
>> I get 3 : 11
>> 
>> how can I get it to print with no spaces?
>> 
>> 3:11
> 
> The print statement isn't right for what you're doing here. You need
> to create a single string with that result, which you could then print
> out.
> 
> There are two easy ways to do this. First one is to create a string
> from the number of minutes, create a string from the number of
> seconds, and concatenate them, with a third string (the colon) in
> between. Do you know how to turn an integer into a string, and do you
> know how to add strings together?
> 
> The second way is to format the values directly into a single string.
> You can use printf codes for this. Check this out:
> 
> http://docs.python.org/2/library/stdtypes.html#string-formatting-operations
> 
> ChrisA
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7.6 help with white spaces?

2014-02-06 Thread Scott W Dunning
what exactly is the “%d:%02d”% saying?  



On Feb 6, 2014, at 6:25 PM, Roy Smith  wrote:

> In article ,
> Scott W Dunning  wrote:
> 
>> I am having trouble figuring out how to remove spacesŠ.
>> 
>> Assume variables exist for minutes and seconds. Each variable is an integer. 
>> How would you create a string in the format,
>> 
>> 3:11
>> 
>> with no spaces. where 3 is minutes and 11 is seconds.
>> 
>> 
>> Obviously when IŠ
>> 
>> print minutes, ³:², seconds
>> 
>> I get 3 : 11
>> 
>> how can I get it to print with no spaces?   
>> 
>> 3:11
> 
> print "%d:%02d" % (minutes, seconds)
> 
> The "02" for the seconds specifier makes sure you get exactly two 
> digits, with a leading zero if needed.
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about `list.insert`

2014-02-06 Thread Gregory Ewing

Roy Smith wrote:

O(-1).  In Soviet Russia, operation performs you!


It's rumoured that the PSU is developing a time
machine module that can achieve O(-n), but
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7.6 help with white spaces?

2014-02-06 Thread Roy Smith
On Feb 6, 2014, at 11:12 PM, Scott W Dunning wrote:

> what exactly is the “%d:%02d”% saying?  

Python uses string format specifiers similar to C's printf()

%d means, "convert an integer to a decimal string"

%2d means the same, plus, "make the result 2 columns wide"

and, finally, %02d means, "and, if it would normally be less than 2 columns, 
zero-pad it on the left".

So, putting that all together, we've got:

"%d" --> decimal integer
":" --> a literal ":"
"%02d" --> another decimal integer, this time zero-padded to two columns


> 
> 
> 
> On Feb 6, 2014, at 6:25 PM, Roy Smith  wrote:
> 
>> In article ,
>> Scott W Dunning  wrote:
>> 
>>> I am having trouble figuring out how to remove spacesŠ.
>>> 
>>> Assume variables exist for minutes and seconds. Each variable is an 
>>> integer. 
>>> How would you create a string in the format,
>>> 
>>> 3:11
>>> 
>>> with no spaces. where 3 is minutes and 11 is seconds.
>>> 
>>> 
>>> Obviously when IŠ
>>> 
>>> print minutes, ³:², seconds
>>> 
>>> I get 3 : 11
>>> 
>>> how can I get it to print with no spaces?   
>>> 
>>> 3:11
>> 
>> print "%d:%02d" % (minutes, seconds)
>> 
>> The "02" for the seconds specifier makes sure you get exactly two 
>> digits, with a leading zero if needed.
>> -- 
>> https://mail.python.org/mailman/listinfo/python-list
> 


--
Roy Smith
r...@panix.com



-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about `list.insert`

2014-02-06 Thread Chris Angelico
On Fri, Feb 7, 2014 at 2:11 PM, Tim Chase  wrote:
> On 2014-02-06 22:00, Roy Smith wrote:
>> > list does not promise better than O(1) behavior
>>
>> I'm not aware of any list implementations, in any language, that
>> promises better than O(1) behavior for any operations.  Perhaps
>> there is O(j), where you just imagine the operation was performed?
>
> Pish...there's always O(0) which is achieved by *not* performing some
> unneeded operation ;-)
>
> -tkc

Ooh! Ooh! I got it!

class fast_list(list):
def append(self,obj,randrange=random.randrange):
if len(self) and randrange(len(self)): return
return super().append(obj)

Repeated appends to this list are amortized O(0)!

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: trivial import question

2014-02-06 Thread John Ladasky
On Wednesday, February 5, 2014 9:52:33 AM UTC-8, nevets...@gmail.com wrote:
> The underscore relative to a prfixed abbb. Is to be noted

Reviving a fourteen year-old thread?

That has to be some kind of record.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [OT] Usage of U+00B6 PILCROW SIGN

2014-02-06 Thread Rustom Mody
On Friday, February 7, 2014 12:59:19 AM UTC+5:30, Robert Kern wrote:

> When I looked at it earlier today, I got a default "cannot find this glyph" 
> box 
> instead of the chain icon. I assumed that is what Rustom was referring to. 
> It's  working for me now.

Yes I was getting unicode number-boxes; which is now working
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about `list.insert`

2014-02-06 Thread Roy Smith
In article ,
 Tim Chase  wrote:

> On 2014-02-06 22:00, Roy Smith wrote:
> > > list does not promise better than O(1) behavior  
> > 
> > I'm not aware of any list implementations, in any language, that 
> > promises better than O(1) behavior for any operations.  Perhaps
> > there is O(j), where you just imagine the operation was performed?
> 
> Pish...there's always O(0) which is achieved by *not* performing some
> unneeded operation ;-)

O(-1).  In Soviet Russia, operation performs you!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about `list.insert`

2014-02-06 Thread Roy Smith
In article ,
 Dave Angel  wrote:

> list does not promise better than O(1) behavior

I'm not aware of any list implementations, in any language, that 
promises better than O(1) behavior for any operations.  Perhaps there is 
O(j), where you just imagine the operation was performed?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7.6 help with white spaces?

2014-02-06 Thread Chris Angelico
On Fri, Feb 7, 2014 at 3:09 PM, Scott W Dunning  wrote:
> Is this what you’re talking about?
>
> minutes = “3”
> seconds = “11”
>
> print int(minutes), ”:" int(seconds)
>
> That’s what you mean by turning a string into an int right?  Not sure how to 
> add strings together though.
>

Well, that's what I would have meant, if I'd talked about turning a
string into an int :) Now, can you do the opposite? Turn an int into a
string? It's done in very much the same way.

Adding strings together is done in the same way as adding integers,
lists, or anything else:

>>> 'foo' + 'bar'
'foobar'

And remember this, whatever else you do: The interactive interpreter
is your friend. Keep it handy. On Windows, you'll find IDLE in your
Start menu - it's awesome for this sort of thing. On Linux, you might
need to explicitly install IDLE. Either way, you can also just type
"python" at the command line, and you'll get something that lets you
type stuff in and see the result, like I showed above; it's called a
Read-Evaluate-Print Loop, or REPL.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to reduce bugs due to incorrect indentation

2014-02-06 Thread Chris Angelico
On Fri, Feb 7, 2014 at 12:20 PM,   wrote:
> It would be possible to disable the Tab key completely and type in the spaces 
> all the time. (It is much less likely that one would press the space bar 
> accidentally four times or hold it down to get 4 spaces by mistake.)
>
> Unfortunately this means giving up the indentation help of the editor and 
> that will slow down coding. It will also lead to many indentation mistakes 
> during development (most of which will be caught right away however. Maybe a 
> coloring of the background based on tab position could assist in this.
>

I don't know that it'd really help much anyway. You might reduce one
chance of making errors by hitting a single key, but at the cost of
stupid syntactic salt (indentation requires hitting a key four times?
No thanks), and your fingers would just get used to
whack-whack-whack-whack. No change.

You can spend all your time trying to warp your coding style around
preventing this bug or that bug from happening, or you can just
acknowledge that bugs WILL happen and handle them after the event.
(Hence, source control.) Suppose you come up with a solution to the
accidental-indentation problem. What are you going to do about this
one?

def foo(bar):
if not bar: bat = [0]
for x in bar:
print(len(bar),x)

Now, why is your empty-list handling not working? Oh, there was a
typo. How are you going to deal with that? Well, you could bring in
C-style variable declarations; then you'd get an immediate error
('bat' is undeclared), but somehow I don't think most Python
programmers would prefer this :) Now personally, I do quite like
declared variables, because they allow infinitely-nested scoping, and
I find that feature worth the effort of declaring all my locals; but
it's a tradeoff, and I wouldn't go to that level of effort *just* to
catch typos in variable names. What if there had been a 'bat' at a
higher scope? Then the typo just means the code does something else
wrong. No fundamental difference.

There was a scheme posted to this list a little while ago to have
variable names shown in different colors, which might have helped. (I
disagree with the author's idea that similar names should be in
similar colors - I think that similar names should be in DISsimilar
colors, specifically to catch this sort of error. But anyway.) That's
a theory that might help... but it still might not. And what if your
error is in a literal string that later gets parsed? No, there's no
way that you can catch everything beforehand.

Bugs happen. Find 'em, fix 'em.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7.6 help with white spaces?

2014-02-06 Thread Chris Angelico
On Fri, Feb 7, 2014 at 12:22 PM, Scott W Dunning  wrote:
> Assume variables exist for minutes and seconds. Each variable is an integer.
> How would you create a string in the format,
>
> 3:11
>
> with no spaces. where 3 is minutes and 11 is seconds.
>
>
> Obviously when I…
>
> print minutes, “:”, seconds
>
> I get 3 : 11
>
> how can I get it to print with no spaces?
>
> 3:11

The print statement isn't right for what you're doing here. You need
to create a single string with that result, which you could then print
out.

There are two easy ways to do this. First one is to create a string
from the number of minutes, create a string from the number of
seconds, and concatenate them, with a third string (the colon) in
between. Do you know how to turn an integer into a string, and do you
know how to add strings together?

The second way is to format the values directly into a single string.
You can use printf codes for this. Check this out:

http://docs.python.org/2/library/stdtypes.html#string-formatting-operations

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about `list.insert`

2014-02-06 Thread Dan Stromberg
On Thu, Feb 6, 2014 at 3:59 PM, cool-RR  wrote:
> Hi,
>
> I'm curious. If I append an item to a list from the left using `list.insert`, 
> will Python always move the entire list one item to the right (which can be 
> super-slow) or will it check first to see whether it can just allocate more 
> memory to the left of the list and put the item there, saving a lot of 
> resources?

I'm pretty sure it'll slide all the existing elements right one
position, and add at the leftmost position just opened up - assuming
you're inserting at position 0.

As was already mentioned, collections.deque is good for this sort of
thing.  It's implemented as a fancy doubly-linked list. Or rather, a
doubly-linked list of smallish arrays/lists.

For a singly-linked list:
http://stackoverflow.com/questions/280243/python-linked-list
http://stromberg.dnsalias.org/~strombrg/linked-list/

HTH
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7.6 help with white spaces?

2014-02-06 Thread Scott W Dunning
Oops, thought you said turning a str into an int.  Yeah, I’m actually using 
Idle on a mac.  I keep trying differnt things but, I’m just very new to this.  

Is this what you’re talking about?

minutes=3
seconds=11

print str(minutes) + ‘:’ + str(seconds)

Unfortunately I keep getting an error.  Also, not really clear on the reasoning 
behind changing the int to a string?  




On Feb 6, 2014, at 9:45 PM, Chris Angelico  wrote:

> On Fri, Feb 7, 2014 at 3:09 PM, Scott W Dunning  wrote:
>> Is this what you’re talking about?
>> 
>> minutes = “3”
>> seconds = “11”
>> 
>> print int(minutes), ”:" int(seconds)
>> 
>> That’s what you mean by turning a string into an int right?  Not sure how to 
>> add strings together though.
>> 
> 
> Well, that's what I would have meant, if I'd talked about turning a
> string into an int :) Now, can you do the opposite? Turn an int into a
> string? It's done in very much the same way.
> 
> Adding strings together is done in the same way as adding integers,
> lists, or anything else:
> 
 'foo' + 'bar'
> 'foobar'
> 
> And remember this, whatever else you do: The interactive interpreter
> is your friend. Keep it handy. On Windows, you'll find IDLE in your
> Start menu - it's awesome for this sort of thing. On Linux, you might
> need to explicitly install IDLE. Either way, you can also just type
> "python" at the command line, and you'll get something that lets you
> type stuff in and see the result, like I showed above; it's called a
> Read-Evaluate-Print Loop, or REPL.
> 
> ChrisA
> -- 
> https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about `list.insert`

2014-02-06 Thread Terry Reedy

On 2/6/2014 7:42 PM, MRAB wrote:

On 2014-02-06 23:59, cool-RR wrote:

Hi,

I'm curious. If I append an item to a list from the left using
`list.insert`, will Python always move the entire list one item to
the right (which can be super-slow) or will it check first to see
whether it can just allocate more memory to the left of the list and
put the item there, saving a lot of resources?


If it needs more space it resizes. It then moves the items.


The OP apparently knows that there is usually extra space at the right 
(end), so that reallocation is usually not needed. He wanted to know 
whether extra space is also kept at the left (beginning) to make left 
appends as efficient as right appends. This has been proposed and the 
answer was to use collections.deque if it really matters. CPython lists 
are asymmetric re-sizable stacks


--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Re: Question about `list.insert`

2014-02-06 Thread Tim Chase
On 2014-02-06 22:00, Roy Smith wrote:
> > list does not promise better than O(1) behavior  
> 
> I'm not aware of any list implementations, in any language, that 
> promises better than O(1) behavior for any operations.  Perhaps
> there is O(j), where you just imagine the operation was performed?

Pish...there's always O(0) which is achieved by *not* performing some
unneeded operation ;-)

-tkc



-- 
https://mail.python.org/mailman/listinfo/python-list


Re:Question about `list.insert`

2014-02-06 Thread Dave Angel
 cool-RR  Wrote in message:
> Hi,
> 
> I'm curious. If I append an item to a list from the left using `list.insert`, 
> will Python always move the entire list one item to the right (which can be 
> super-slow) or will it check first to see whether it can just allocate more 
> memory to the left of the list and put the item there, saving a lot of 
> resources?
> 

Excellent question.  list does not promise better than O (1)
 behavior,  and CPython in particular will copy, I'm pretty
 sure.

However that's exactly what collections.deque is for.

-- 
DaveA

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7.6 help with white spaces?

2014-02-06 Thread Chris Angelico
On Fri, Feb 7, 2014 at 4:01 PM, Scott W Dunning  wrote:
> Oops, thought you said turning a str into an int.  Yeah, I’m actually using 
> Idle on a mac.  I keep trying differnt things but, I’m just very new to this.
>
> Is this what you’re talking about?
>
> minutes=3
> seconds=11
>
> print str(minutes) + ‘:’ + str(seconds)
>
> Unfortunately I keep getting an error.  Also, not really clear on the 
> reasoning behind changing the int to a string?

Here's another fundamental of Python: Error messages are really REALLY
useful. In this case, I can figure out what's going on without seeing
it, but normally, post the whole traceback. The problem here is
because you're using "smart quotes" of some sort. Use ordinary
apostrophes and double quotes, and it'll work fine.

As to the reason for changing the int to a string... try skipping it!
Then you'll know. You'll get a nice tidy error, pointing you to the
exact cause of the problem.

>>> print minutes + ':' + seconds

See what happens!

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question about `list.insert`

2014-02-06 Thread Asaf Las
On Friday, February 7, 2014 6:52:24 AM UTC+2, Dan Stromberg wrote:
> On Thu, Feb 6, 2014 at 3:59 PM, cool-RR  wrote:
> 
> I'm pretty sure it'll slide all the existing elements right one
> position, and add at the leftmost position just opened up - assuming
> you're inserting at position 0.
> 
> As was already mentioned, collections.deque is good for this sort of
> thing.  It's implemented as a fancy doubly-linked list. Or rather, a
> doubly-linked list of smallish arrays/lists.
> For a singly-linked list:
> http://stackoverflow.com/questions/280243/python-linked-list
> http://stromberg.dnsalias.org/~strombrg/linked-list/
> 
> HTH

the Py list is just 2 members (as declared in corresponding header)
structure where only one double pointer serves addressing towards most 
probably dynamically allocated array of pointers towards python 
objects. so adding could expensive as it should copy all pointers 
in array into new bigger one during expanding as array needs to be 
contiguous. so it looks more array than list. but i could be wrong in 
my conclusion.

/Asaf
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7.6 help with white spaces?

2014-02-06 Thread Ben Finney
Scott W Dunning  writes:

> I keep trying differnt things but, I’m just very new to this.

A few things that will make your communications more fruitful:

* Please don't top-post. Trim the quoted material to the parts relevant
  for your response, and respond inline like a normal discussion.

  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style>

* Ensure that your message composer doesn't munge your text. The code
  you show us needs to be *exactly* what the Python interpreter will
  see; turn off anything which replaces one character with another (such
  as quotation marks), or wraps the code lines.

* When asking about an error, *show us* the error by pasting exactly
  what the error is, including the part where it shows what line raised
  the exception.

* Tell us what you expected the code to do, and how its behaviour is
  violating your expectations.

Thanks for joining discussions here, hopefully you can become a good
part of this community.

-- 
 \  “If I haven't seen as far as others, it is because giants were |
  `\   standing on my shoulders.” —Hal Abelson |
_o__)  |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python and matlab

2014-02-06 Thread Sturla Molden
Sam Adams  wrote:

> Thanks Sturla, could you please explain in more details, I am new to Python :)

All the information you need to extend or embed Python is in the docs. 

Apart from that, why do you need Matlab? A distro like Enthought Canopy or
Anaconda has all the tools you will ever need for scientific programming.
Embedding Python is almost never the right solution to a problem. But if
you really need Matlab, you will be far better off by calling Matlab engine
from Python (e.g. using ctypes or Cython). 

There are also packages like mlab and matlabwrap that will help you embed
Matlab in Python:

   https://pypi.python.org/pypi/mlab
   http://mlabwrap.sourceforge.net

Apart from that, you can also invoke Python from Matlab like this:

   arg1 = 'script.py';
   s = system(sprintf('python %s', arg1));

or 

   arg1 = 'some Python code';
   s = system(sprintf('python -c "%s"', arg1));

Or if you are on Windows, you can use pywin32 to create an ActiveX server
with Python, and invoke that from Matlab.


Sturla

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7.6 help with white spaces?

2014-02-06 Thread Scott W Dunning

> 
> A few things that will make your communications more fruitful:
> 
> * Please don't top-post. Trim the quoted material to the parts relevant
>  for your response, and respond inline like a normal discussion.

Oh, ok sorry about that.  Like this?  

> 
> * Ensure that your message composer doesn't munge your text.

Anyway to check on my mail composer to see it it muges my text?   Also should I 
erase everything else in the message that I’m not responding to?  

Thanks for your advice.


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7.6 help with white spaces?

2014-02-06 Thread Ben Finney
Scott W Dunning  writes:

> > * Please don't top-post. Trim the quoted material to the parts
> > relevant for your response, and respond inline like a normal
> > discussion.
>
> Oh, ok sorry about that.  Like this?  

Yes. You also need to preserve the attribution lines (the lines inserted
for each quoted message like “Fred Nurk wrote:”, or similar) at each
level so we can see who wrote what.

> > * Ensure that your message composer doesn't munge your text.
>
> Anyway to check on my mail composer to see it it muges my text?

You should consult the documentation for whatever you're using to
compose a message, looking for things like “auto-correct” or anything
else which suggests it will automatically change what you type.

> Also should I erase everything else in the message that I’m not
> responding to?

Yes. All that should remain is the quoted text you're responding to, the
attribution lines for whatever you've quoted, and what you write
yourself.

> Thanks for your advice.

No problems, thanks for the effort!

-- 
 \ “Geeks like to think that they can ignore politics. You can |
  `\leave politics alone, but politics won't leave you alone.” |
_o__)—Richard Stallman, 2002-07-26 |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python and matlab

2014-02-06 Thread Rustom Mody
On Friday, February 7, 2014 10:58:26 AM UTC+5:30, Sturla Molden wrote:
> Sam Adams  wrote:

> > Thanks Sturla, could you please explain in more details, I am new to Python 
> > :)

> All the information you need to extend or embed Python is in the docs. 

> Apart from that, why do you need Matlab? A distro like Enthought Canopy or
> Anaconda has all the tools you will ever need for scientific programming.
> Embedding Python is almost never the right solution to a problem. But if
> you really need Matlab, you will be far better off by calling Matlab engine
> from Python (e.g. using ctypes or Cython). 

Dont know the software involved so only some general comments

When you connect two disparate pieces of significant software there is
inherently an impedance mismatch.

What Sturla is probably saying is that the matmab-python imp-mismatch is 
so high that jumping across is almost certainly not worth the trouble.

And BTW have you seen sage?
http://www.sagemath.org/
-- 
https://mail.python.org/mailman/listinfo/python-list