Re: How to display Chinese in a list retrieved from database via python

2008-12-27 Thread Gabriel Genellina

En Sat, 27 Dec 2008 03:03:24 -0200, zxo102  escribió:
On 12月26日, 下午3时16分, "Mark Tolonen"   
wrote:


I was able to display 中文 successfully with this code:

f=open('test.html','wt')
f.write('''

test
\xd6\xd0\xce\xc4''')
f.close()


Mark,
   I have exactly copied your code into the htdocs of my Apache
server,



test
\xd6\xd0\xce\xc4

but it still shows me \xd6\xd0\xce\xc4. Any ideas?


That's not the same thing as Mark T. said.
The original was Python code to *write* a test file that you could open in
a browser. Things like "\xd6\xd0" are escape sequences interpreted by
Python, not meant to literally appear in a file. Like \n -- it means
"start a new line", one wants a new line in the output, *not* a backslash
and a letter n. "\xd6\xd0" generate *two* bytes, not eight. If the file is  
interpreted as containing latin-1 characters, you see them as ÖÐ. But due  
to the "charset=gb2312" line, those two bytes together make the ideograph  
中.


So, write the Python code (from f=open... up to f.close()) in a file and
execute it. Then open the generated test.html file. You should see the two
ideographs.

--
Gabriel Genellina

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


Re: I always wonder ...

2008-12-27 Thread Pierre-Alain Dorange
r  wrote:

> > > Go to Google groups... it looks perfect :)
> >
> > It's a usenet group here, nothing to do with google groups.
> > To get back to the group subject, what is your actual python projects ?
> 
> I am crusading to bring python scripting to Google SketchUp, do want
> to get on board?

I've other real project on the run, no crusade just programming.
Google sketchup doesn't really interest me, sorry...

-- 
Pierre-Alain Dorange

Ce message est sous licence Creative Commons "by-nc-sa-2.0"

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


Re: multiply each element of a list by a number

2008-12-27 Thread MVP

Hi!


map(multby3, (1, 2, 3, ))


...with lambda:  
   map(lambda x: x*3, [1,2,3])


@-salutations
--
Michel Claveau


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


Re: Getting a locked buffer from objects (PEP 3118)

2008-12-27 Thread Stefan Behnel
KKH wrote:
> it seems the whole locking-theme has gone from PEP 3118 (PyBUF_LOCK is
> gone). Yet the string and byte objects seem to provide locked buffers
> through PyArg_Parse arguments s*, y* and z* (documentation says so).

Might be a left-over. All locking the buffer interface itself provides is
the guarantee that the buffer will not move between getting and releasing a
buffer. Everything else must be handled at the application level.


> Could someone please clarify the situation for me: Is it save to release
> the GIL after getting a reference to a buffer-object by the arguments
> above and only talking to this buffer while the GIL is gone?

The buffer interface is a pure C level interface. All you get is a pointer
to a memory buffer plus some meta data. No Python references are involved
from that point on. So it only depends on your own code if it's safe to
free the GIL or not.


> And while we are at it :-) Is "PyArg_Parse('[syz]*'..." the only way to
> get a locked buffer-view from string/byte objects?

What kind of locking do you need?

Stefan
--
http://mail.python.org/mailman/listinfo/python-list


Parsing a sitemap library?

2008-12-27 Thread Sam
Hi...

I see an awful lot of code out there to create sitemaps using Python.

But I can't find anything to consume sitemaps using Python.  Is there
a library to convert sitemaps and sitemap indexes of sitemaps into
something more Pythonic?

I know I can write one pretty easily using e.g. Beautiful Soup or
maybe lxml, but I never like to reinvent the wheel.

Thanks
--
http://mail.python.org/mailman/listinfo/python-list


parsing csv files class

2008-12-27 Thread alex goretoy
Hello All,

I have this class that I use in one of my projects. I know it's missing
functionality and some things could have been done differently. Can you ehlp
me make this class better? What can I do to make it more resistant to error?
You can find the stdout_colours class on Google if you want it, JFGI I want
to make it more pythonic. I come from a PHP background, can you tell?

Any and all help is appreciated
-Alex

#!/usr/bin/env python
from ctypes import *
import os, sys, types, csv, urllib, urllib2, urlparse, string,stdout_colours

class parsercsvy(object):
"""Return a line from a csv file or total amount of lines"""
def __init__(self,file_name=""):
self.func_me_color="white_on_black"
self.soc=stdout_colours.stdout_colors()
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.filename = file_name
self.buffer = []
self.bufferp= []
if string.find(self.filename,"http") != -1:
resp=urllib2.urlopen(self.filename)
file=resp.read()
lfi=len(string.split(self.filename,"/"))
filename = "/tmp/"+string.split(self.filename,"/")[lfi-1]
f=open(filename,"w")
f.write(file)
f.close
self.parse(self.filename)
else:
self.parse(self.filename)
self.soc.me_him(['EXIT:',__name__],self.func_me_color)
def parse(self,filename,ret=0):
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
i = 0
try:
reader = csv.reader(file(filename, "rb"))
try:
for row in reader:
self.buffer.append(row)
s,a=[],{}

for j in range(len(self.buffer[0])):
a[self.buffer[0][j]]=row[j]
self.bufferp.append(a)
i+=1
self.total = i-1
except csv.Error, e:
sys.exit('file %s, line %d: %s' % (filename,
reader.line_num, e))
except IOError, e:
sys.exit('file %s, IOError: %s' % (filename, e))
self.soc.me_him(['EXIT:',__name__],self.func_me_color)
def index(self, index):
"""return line for index"""
self.soc.me_him(['ENTER:',__name__],self.func_me_color)

self.soc.me_him(['RETURN:',self.buffer[int(index)],__name__],self.func_me_color)
return self.buffer[int(index)]
def total(self):
"""return total number of lines in csv file"""
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.soc.me_him(['RETURN:',self.total,__name__],self.func_me_color)
return self.total
def header(self):
"""return csv header == line 0"""
self.soc.me_him(['ENTER:',__name__],self.func_me_color)

self.soc.me_him(['RETURN:',self.buffer[0],__name__],self.func_me_color)
return self.buffer[0]
def find_and_replace(self,li,fi,re):
"""
find and replace a string inside a string, return list
find_and_replace(list,find,replace)
"""
this=[]
for l in li:
#found_index=string.find(l,fi)
this.append(l.replace(fi,re))
return this
def return_buffer(self):
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.soc.me_him(['RETURN:',self.buffer,__name__],self.func_me_color)
return self.buffer
if __name__ == "__main__":
if len(sys.argv) < 1:
print "Usage: %s file"% sys.argv[0]
f=sys.argv[1]
c=csv_parser(f)
print c.bufferp
-- 
А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
--
http://mail.python.org/mailman/listinfo/python-list


Re: parsing csv files class

2008-12-27 Thread alex goretoy
I know it's messy with all those self.soc.* functions, but it works in one
of my current project. I just want to make it more pythonic I also want to
add capability for makeing csv file if I give it input like:
1234,something nice, hey this is something nice
2468,something else, something else

On Sat, Dec 27, 2008 at 4:54 AM, alex goretoy
wrote:

> Hello All,
>
> I have this class that I use in one of my projects. I know it's missing
> functionality and some things could have been done differently. Can you ehlp
> me make this class better? What can I do to make it more resistant to error?
> You can find the stdout_colours class on Google if you want it, JFGI I want
> to make it more pythonic. I come from a PHP background, can you tell?
>
> Any and all help is appreciated
> -Alex
>
> #!/usr/bin/env python
> from ctypes import *
> import os, sys, types, csv, urllib, urllib2, urlparse,
> string,stdout_colours
>
> class parsercsvy(object):
> """Return a line from a csv file or total amount of lines"""
> def __init__(self,file_name=""):
> self.func_me_color="white_on_black"
> self.soc=stdout_colours.stdout_colors()
> self.soc.me_him(['ENTER:',__name__],self.func_me_color)
> self.filename = file_name
> self.buffer = []
> self.bufferp= []
> if string.find(self.filename,"http") != -1:
> resp=urllib2.urlopen(self.filename)
> file=resp.read()
> lfi=len(string.split(self.filename,"/"))
> filename = "/tmp/"+string.split(self.filename,"/")[lfi-1]
> f=open(filename,"w")
> f.write(file)
> f.close
> self.parse(self.filename)
> else:
> self.parse(self.filename)
> self.soc.me_him(['EXIT:',__name__],self.func_me_color)
> def parse(self,filename,ret=0):
> self.soc.me_him(['ENTER:',__name__],self.func_me_color)
> i = 0
> try:
> reader = csv.reader(file(filename, "rb"))
> try:
> for row in reader:
> self.buffer.append(row)
> s,a=[],{}
>
> for j in range(len(self.buffer[0])):
> a[self.buffer[0][j]]=row[j]
> self.bufferp.append(a)
> i+=1
> self.total = i-1
> except csv.Error, e:
> sys.exit('file %s, line %d: %s' % (filename,
> reader.line_num, e))
> except IOError, e:
> sys.exit('file %s, IOError: %s' % (filename, e))
> self.soc.me_him(['EXIT:',__name__],self.func_me_color)
> def index(self, index):
> """return line for index"""
> self.soc.me_him(['ENTER:',__name__],self.func_me_color)
>
> self.soc.me_him(['RETURN:',self.buffer[int(index)],__name__],self.func_me_color)
> return self.buffer[int(index)]
> def total(self):
> """return total number of lines in csv file"""
> self.soc.me_him(['ENTER:',__name__],self.func_me_color)
> self.soc.me_him(['RETURN:',self.total,__name__],self.func_me_color)
> return self.total
> def header(self):
> """return csv header == line 0"""
> self.soc.me_him(['ENTER:',__name__],self.func_me_color)
>
> self.soc.me_him(['RETURN:',self.buffer[0],__name__],self.func_me_color)
> return self.buffer[0]
> def find_and_replace(self,li,fi,re):
> """
> find and replace a string inside a string, return list
> find_and_replace(list,find,replace)
> """
> this=[]
> for l in li:
> #found_index=string.find(l,fi)
> this.append(l.replace(fi,re))
> return this
> def return_buffer(self):
> self.soc.me_him(['ENTER:',__name__],self.func_me_color)
>
> self.soc.me_him(['RETURN:',self.buffer,__name__],self.func_me_color)
> return self.buffer
> if __name__ == "__main__":
> if len(sys.argv) < 1:
> print "Usage: %s file"% sys.argv[0]
> f=sys.argv[1]
> c=csv_parser(f)
> print c.bufferp
> --
> А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
> а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
>



-- 
А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
--
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a function to remove escape characters from a string ?

2008-12-27 Thread Stef Mientki

Steven D'Aprano wrote:

On Sat, 27 Dec 2008 01:41:40 +0100, Stef Mientki wrote:

  

Sorry if I offended someone, that was certainly not my intention. And I
guess you will be surprised, if I tell you, I don't (want) to understand
any bit of the above code ;-) Come on, the home computer was invented
about 1980. If we look at hardware, it follows the Moore's law, for
software I would expect at least 0.1 of Moore's law ;-) I hope that
clarifies my point.



No, that only makes it even more confusing. What does Moore's Law have to 
do with your willful ignorance about the existence of human languages 
other than English?


  

Nothing.
I even don't (want to) see what bits / bytes / escape sequences have to 
do with modern programming techniques,

so I certainly don't see any relation between these and human languages.

But the lack of Moore's law in software explains why we still need to 
concern about bits and bytes ;-)


cheers,
Stef


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


Re: tkinter 3.0 multiple keyboard events together

2008-12-27 Thread janislaw
On 26 Gru, 17:44, Pavel Kosina  wrote:
> janislaw napsal(a):
>
> > On 26 Gru, 05:52, Pavel Kosina  wrote:
>
> >> Is it possible to catch in an event more that one key from keyboard? In
> >> my code, I can handle always the only one, the first I press, the others
> >> are omitted. Say, I press both "4" and "8" and only "4" is catched.
>
> >> def movePlayer(event):
> >>     print (event.keysym)
>
> > Each keypress triggers another event. Fortunately there are two types
> > of events: reaction to press and release. The logic to write to
> > recognize those as simultaneous clicks is up to you :)
>
> Might you give me a little bit more? Just a link to a site where this is
> explained and showed would be OK. I really did my best but everything is
> bad.
>
> geon

Use google to find the appropriate site, or browse this site, there
are plenty of examples. You may want to examine the code I wrote to
you to catch the idea:
#--
import Tkinter
import pprint

tk = Tkinter.Tk()
f = Tkinter.Frame(tk, width=100, height=100)
msg = Tkinter.StringVar()
msg.set('Hello')
l = Tkinter.Label(f,  textvariable=msg)
l.pack()
f.pack()

keys = set()

def keyPressHandler(event):
keys.add(event.char)
display()

def keyReleaseHandler(event):
keys.remove(event.char)
display()

def display():
msg.set(str(keys))

f.bind_all('', keyPressHandler)
f.bind_all('', keyReleaseHandler)

f.mainloop()
#

Regards,
JW
--
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes & strings

2008-12-27 Thread Hendrik van Rooyen

Red Rackham  wrote:

>I would like to pass a string into a dll function.  I notice that to pass using
ctypes, it has to be a ctypes type.  >Looking at the ctypes doc page I don't see
a c_string class.

The following seems to work for me:

In the c programme:

/*
This routine outputs and inputs a symmetric block of bytes, writing
the outputs out and reading the inputs in, replacing the chars in the
original output string
*/

 unsigned char read_write (unsigned char *inputs, unsigned char *outputs, int
lenin, int lenout)
{
int i = 0;
int addr = 0;
int addrhi = 0;
int oind = 0;
char rv;
while (i < lenin)
{
addrhi = i >> 8 & 0x3f; // we can have 64 * 256 adresses in the top
6 + 8 bits
addr = i & 0xff;// this is low order
rv = put_1(addrhi|0xd0);// put out the addy
rv = put_3(addr);
rv = put_1(addrhi|0x90);// make a read strobe on bit 6
inputs[i] = get_0() ^ 255;  // read the char
rv = put_1(addr|0xd0);  // raise strobe again
i++;
}

while (i < lenin + lenout)
{
oind = i - lenin;   // index into outputs start at 0
addrhi = i >> 8 & 0x3f; // we can have 64 * 256 adresses in the top
6 + 8 bits
addr = i & 0xff;// this is low order
rv = put_1(addrhi|0xd0);// put out the addy
rv = put_3(addr);
rv = put_0(outputs[oind]);  // put out the output
rv = put_1(addrhi|0x50);// make a write strobe on bit 7
rv = put_1(addrhi|0xd0);// raise write strobe again
i++;
}
return *inputs;
}


Then in the python it is used as follows:

import sys, os, ctypes, time

io = ctypes.cdll.LoadLibrary('./lib_gpio.a')

def do_io(ins, outs): # ins and outs are normal python strings that must exist

# this routine happens to work by side effect - the ins string is
changed by the c routine

 r = io.read_write(ins,outs,len(ins),len(outs))
 return

The c routine will actually break Python's normal string
immmutability and give you back a changed ins.

It is in general not a good idea to change the passed string
like I am doing - but you wanted to know how
to pass a python string, and the outs example should get
you going - a string is an array of characters in c...


HTH

- Hendrik







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


Re: parsing csv files class

2008-12-27 Thread Gary M. Josack

alex goretoy wrote:
I know it's messy with all those self.soc.* functions, but it works in 
one of my current project. I just want to make it more pythonic I also 
want to add capability for makeing csv file if I give it input like:

1234,something nice, hey this is something nice
2468,something else, something else

On Sat, Dec 27, 2008 at 4:54 AM, alex goretoy 
mailto:aleksandr.gore...@gmail.com>> wrote:


Hello All,

I have this class that I use in one of my projects. I know it's
missing functionality and some things could have been done
differently. Can you ehlp me make this class better? What can I do
to make it more resistant to error? You can find the
stdout_colours class on Google if you want it, JFGI I want to make
it more pythonic. I come from a PHP background, can you tell?

Any and all help is appreciated
-Alex

#!/usr/bin/env python
from ctypes import *
import os, sys, types, csv, urllib, urllib2, urlparse,
string,stdout_colours

class parsercsvy(object):
"""Return a line from a csv file or total amount of lines"""
def __init__(self,file_name=""):
self.func_me_color="white_on_black"
self.soc=stdout_colours.stdout_colors()
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.filename = file_name
self.buffer = []
self.bufferp= []
if string.find(self.filename,"http") != -1:
resp=urllib2.urlopen(self.filename)
file=resp.read()
lfi=len(string.split(self.filename,"/"))
filename = "/tmp/"+string.split(self.filename,"/")[lfi-1]
f=open(filename,"w")
f.write(file)
f.close
self.parse(self.filename)
else:
self.parse(self.filename)
self.soc.me_him(['EXIT:',__name__],self.func_me_color)   
def parse(self,filename,ret=0):

self.soc.me_him(['ENTER:',__name__],self.func_me_color)
i = 0
try:
reader = csv.reader(file(filename, "rb"))
try:
for row in reader:
self.buffer.append(row)
s,a=[],{}
   
for j in range(len(self.buffer[0])):

a[self.buffer[0][j]]=row[j]
self.bufferp.append(a)
i+=1
self.total = i-1
except csv.Error, e:
sys.exit('file %s, line %d: %s' % (filename,
reader.line_num, e))
except IOError, e:
sys.exit('file %s, IOError: %s' % (filename, e))
self.soc.me_him(['EXIT:',__name__],self.func_me_color)
def index(self, index):
"""return line for index"""
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
   
self.soc.me_him(['RETURN:',self.buffer[int(index)],__name__],self.func_me_color)

return self.buffer[int(index)]
def total(self):
"""return total number of lines in csv file"""
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
   
self.soc.me_him(['RETURN:',self.total,__name__],self.func_me_color)

return self.total
def header(self):
"""return csv header == line 0"""
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
   
self.soc.me_him(['RETURN:',self.buffer[0],__name__],self.func_me_color)

return self.buffer[0]
def find_and_replace(self,li,fi,re):
"""
find and replace a string inside a string, return list
find_and_replace(list,find,replace)
"""
this=[]
for l in li:
#found_index=string.find(l,fi)
this.append(l.replace(fi,re))
return this
def return_buffer(self):
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
   
self.soc.me_him(['RETURN:',self.buffer,__name__],self.func_me_color)

return self.buffer
if __name__ == "__main__":
if len(sys.argv) < 1:
print "Usage: %s file"% sys.argv[0]
f=sys.argv[1]
c=csv_parser(f)
print c.bufferp
-- 
А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я

а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я




--
А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я


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

Do you know that there is a csv module in the standard library already?

Thanks,
Gary M. Josack
--
http://mail.python.org/mailman/listinfo/python-list


Re: tkinter 3.0 multiple keyboard events together

2008-12-27 Thread Pavel Kosina

janislaw napsal(a):

Use google to find the appropriate site, or browse this site, there
are plenty of examples. You may want to examine the code I wrote to
you to catch the idea:
#--
import Tkinter
import pprint

tk = Tkinter.Tk()
f = Tkinter.Frame(tk, width=100, height=100)
msg = Tkinter.StringVar()
msg.set('Hello')
l = Tkinter.Label(f,  textvariable=msg)
l.pack()
f.pack()

keys = set()

def keyPressHandler(event):
keys.add(event.char)
display()

def keyReleaseHandler(event):
keys.remove(event.char)
display()

def display():
msg.set(str(keys))

f.bind_all('', keyPressHandler)
f.bind_all('', keyReleaseHandler)

f.mainloop()
  


Is this really the most simple solution how to do this in Tkinter? Is 
there nothing cleaner "build inside"?


This solution has disadvantage that after you release one key, that the 
function keyPressHandler stopped to be called by the other pressed keys. 
I would have not to build moving the player in KeyPresshandler, but on 
another new function that would have to read periodically "keys" and to 
act afterwards


I really tried to search for words mentioned in this subject, but 
nothing, not even this solution, was found by me.


PK


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


Re: dummy needs help with Python

2008-12-27 Thread Tim Chase
I am trying to find somebody who can give me a simple python 
program I can use to "program by analogy".  I just want to 
read two CSV files and match them on several fields, 
manipulate some of the fields, and write a couple of output 
files.

...
Please forgive me if this is so, and take pity on a stranger 
in a strange land.


Pittsburgh is a little strange, but not *that* bad :)

Just for fun, I threw together a simple (about 30 lines) program 
to do what you describe.  Consider it a bit of slightly belated 
Christmas pity on the assumption that this isn't classwork (a 
little googling suggests that it's not homework).  It's 100% 
untested, so if it formats your hard-drive, steals your spouse, 
wrecks your truck, kicks your dog, makes a mess of your 
trailer-home, and drinks all your beer, caveat coder.  But you've 
got the source, so you can vet it...and it's even commented a bit 
for pedagogical amusement if you plan to mung with it :)


  from csv import reader
  SMALL = 'a.txt'
  OTHER = 'b.txt'
  smaller_file = {} # key->line mapping dict for the smaller file
  f_a = file(SMALL)
  r_a = reader(f_a)
  #a_headers = reader.next() # optionally discard a header row

  # build up the map in smaller_file of key->line
  for i, line in enumerate(r_a):
a1, a2, a3, a4, a5 = line # name the fields
key = f1, f3, f5
if key in smaller_file:
  print "Duplicate key [%r] in %s:%i" % (key, SMALL, i+1)
  #continue # does the 1st or 2nd win? uncomment for 1st
smaller_file[key] = line
  f_a.close()

  b = file(OTHER)
  r_b = reader(b)
  #b_headers = reader.next() # optionally discard a header row
  for i, line in enumerate(r_b):
b1, b2, b3, b4, b5, b6, b7, b8, b9 = line
key = b2, b8, b9
if key not in smaller_file:
  print "Key for line #%i (%r) not in %s" % (i+1, key, SMALL)
  continue
a1, a2, a3, a4, a5 = smaller_file[key]
# do manipulation with a[1-5]/b[1-9] here
# and do something with them
  b.close()

It makes more sense if instead of calling them a[1-5]/b[1-9], you 
actually use the field-names that may have be in the header rows 
such as


  cost_center, store, location, manager_id = line
  key = cost_center, store, location

You may also have to manipulate some of the values to make 
key-matches work, such as


  cc, store, loc, mgr = line
  cc = cc.strip().upper()
  store = store.strip().title()
  key = cc, store, loc

ensuring that you do the same manipulations for both files.

The code above reads the entire smaller file into memory and uses 
it for fast lookup.  However, if you have gargantuan files, you 
may need to process them differently.  You don't detail the 
fields/organization of the files, so if they're both sorted by 
key, you can change the algorithm to behave like the standard 
*nix "join" command.


Other asides:  you may have to tweak treatment of a header-row 
(and correspondingly the line-numbers), as well as 
conflict-handling for keys in your a.txt source if they exist, 
along with the behavior when a key can't be found in a.txt but is 
requested in b.txt (maybe set some defaults instead of logging 
the error and skipping the row?), and then lastly and most 
importantly, you have to fill in the manipulations you desire and 
then actually do something with the processed results (write them 
to a file, upload them to a database, send them via email, output 
them to a text-to-speech engine and have it speak them, etc).



I come from 30 years of mainframe programming so I understand
how computers work at a bits/bytes /machine language/ source
vs.executable/reading core dumps level,  and I can program in
a lot of languages most people using Python have never even
heard of,


If there's such urgency, I hope you resorted to simply using one 
of these multitude of other languages you know -- Even in C, this 
wouldn't be too painful as projects go (there's a phrase you 
won't hear me utter frequently).  Or maybe try your hand at it in 
pascal, shell-scripting (see the "join" command) or even assembly 
language.  Not sure I'd use Logo, Haskel, Erlang, or Prolog. :)



My problem is that I want to do this all yesterday, and the
Python text I bought is not easy to understand. I don't have
time to work my way through the online Python tutorial. 


As Rick mentioned, there are a number of free online sources for 
tutorials, books, and the like.  Dive Into Python is one of the 
classics.  Searching the archives of comp.lang.python for 
"beginner books" will yield the same thread coming up every 
couple weeks.  For future reference, if you've got time-sensitive 
projects to tackle "yesterday", it's usually not the best time to 
try and learn a new language.  Good luck in your exploration of 
Python.


-tkc




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


www.iofferkicks.com china cheap wholesale nike shoes,air jordan shoes,air force one shoes.

2008-12-27 Thread www.iofferkicks.com
Get Nike Shoes at Super Cheap Prices
Discount Nike air jordans  (www.iofferkicks.com)
Discount Nike Air Max 90 Sneakers  (www.iofferkicks.com)
Discount Nike Air Max 91 Supplier  (www.iofferkicks.com)
Discount Nike Air Max 95 Shoes Supplier  (www.iofferkicks.com)
Discount Nike Air Max 97 Trainers  (www.iofferkicks.com)
Discount Nike Air Max 2003 Wholesale  (www.iofferkicks.com)
Discount Nike Air Max 2004 Shoes Wholesale
(www.iofferkicks.com)
Discount Nike Air Max 2005 Shop  (www.iofferkicks.com)
Discount Nike Air Max 2006 Shoes Shop  (www.iofferkicks.com)
Discount Nike Air Max 360 Catalogs  (www.iofferkicks.com)
Discount Nike Air Max Ltd Shoes Catalogs  (www.iofferkicks.com)
Discount Nike Air Max Tn Men's Shoes  (www.iofferkicks.com)
Discount Nike Air Max Tn 2 Women's Shoes  (www.iofferkicks.com)
Discount Nike Air Max Tn 3 Customize  (www.iofferkicks.com)
Discount Nike Air Max Tn 4 Shoes Customize
( www.iofferkicks.com)
Discount Nike Air Max Tn 6 Supply  (www.iofferkicks.com)
Discount Nike Shox NZ Shoes Supply  (www.iofferkicks.com)
Discount Nike Shox OZ Sale  (www.iofferkicks.com)
Discount Nike Shox TL Store  (www.iofferkicks.com)
Discount Nike Shox TL 2 Shoes Store (www.iofferkicks.com)
Discount Nike Shox TL 3 Distributor (www.iofferkicks.com)
Discount Nike Shox Bmw Shoes Distributor  (www.iofferkicks.com)
Discount Nike Shox Elite Shoes Manufacturer
(www.iofferkicks.com)
Discount Nike Shox Monster Manufacturer  (www.iofferkicks.com)
Discount Nike Shox R4 Running Shoes  (www.iofferkicks.com)
Discount Nike Shox R5 Mens Shoes  (www.iofferkicks.com)
Discount Nike Shox Ride Womens Shoes (www.iofferkicks.com)
Discount Nike Shox Rival Shoes Wholesaler  (www.iofferkicks.com)
Discount Nike Shox Energia Wholesaler  (www.iofferkicks.com)
Discount Nike Shox LV Sneaker  (www.iofferkicks.com)
Discount Nike Shox Turbo Suppliers  (www.iofferkicks.com)
Discount Nike Shox Classic Shoes Suppliers
(www.iofferkicks.com)
Discount Nike Shox Dendara Trainer  (www.iofferkicks.com)
Discount Nike Air Jordan 1 Seller  (www.iofferkicks.com)
Discount Nike Air Jordan 2 Shoes Seller  (www.iofferkicks.com)
Discount Nike Air Jordan 3 Collection  (www.iofferkicks.com)
Discount Nike Air Jordan 4 Shoes Collection
(www.iofferkicks.com)
Discount Nike Air Jordan 5 Chaussure Shoes
(www.iofferkicks.com)
Discount Nike Air Jordan 6 Catalog  (www.iofferkicks.com)
Discount Nike Air Jordan 7 Shoes Catalog  (www.iofferkicks.com)
Discount Nike Air Jordan 8 Customized  (www.iofferkicks.com)
Discount Nike Air Jordan 9 Shoes Customized
(www.iofferkicks.com)
Discount Nike Air Jordan 10 Wholesalers  (www.iofferkicks.com)
Discount Nike Jordan 11 Shoes Wholesalers (www.iofferkicks.com)
Discount Nike Air Jordan 12 Factory  (www.iofferkicks.com)
Discount Nike Air Jordan 13 Shoes Factory (www.iofferkicks.com)
Discount Nike Air Jordan 14 Shoes Sell  (www.iofferkicks.com)
Discount Nike Air Jordan 16 Exporter  (www.iofferkicks.com)
Discount Nike Air Jordan 17 Shoes Exporter
(www.iofferkicks.com)
Discount Nike Air Jordan 18 Offer  (www.iofferkicks.com)
Discount Nike Air Jordan 19 Shoes Offer  (www.iofferkicks.com)
Discount Nike Air Jordan 20 Manufacture  (www.iofferkicks.com)
Discount Nike Jordan 21 Shoes Manufacture  (www.iofferkicks.com)
EMAIL:iofferki...@gmail.com
MSN :iofferki...@msn.com
--
http://mail.python.org/mailman/listinfo/python-list


Initializing GHC from Python

2008-12-27 Thread Ron de Bruijn

Hi,

We have just published a small article on how one can initialize GHC 
from Python, with only optional use of C. You can read it at  
http://gamr7.com/blog/?p=65 .


Best regards, 
Ron de Bruijn

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


Re: I always wonder ...

2008-12-27 Thread Hendrik van Rooyen
"Carl Banks"  wrote:

>If you have to followup, at least keep your reply to something short
>and witty, like, "Go away, troll".

OK will do see next post.

- Hendrik


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


Re: Python's popularity (a bit OT)

2008-12-27 Thread Hendrik van Rooyen
"r"  wrote:

>I think you missed my point Steven, I was in no way proud of the fact
>of my 9th place rating. It just proves my point to the small following
>of this group. And frankly makes me feel bad.

This spurt of high frequency posts is something that seems to happen
to most newcomers to the group. (I think - I have done no stats on it, but
I get the feeling...)

So maybe the advice is to settle down and enjoy using the resource,
because it is an amazing one - there are some hyper intelligent people
around.  Some of them have some experience.  Some of them even 
know a little python...

It also helps if you answer some of the newbie questions, as it crystallises
your own knowledge of the language. - that is the way to show how big your
cojones are.  Nobody believes your claims based on mere descriptions.

As you go along, you will find that this group will rarely let you get away
with talking nonsense - It has been my experience that my slightest error
or fumble has almost always been pitilessly pointed out.  I think it is 
because the volume is high, and people get irritated if they have to wade
through stuff like this post that has more to do with etiquette than python.

- Hendrik


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


Re: anagram finder / dict mapping questi

2008-12-27 Thread 7068316797

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


Test message - please ignore

2008-12-27 Thread skip
Working on the spam filter.  Please ignore...

S
--
http://mail.python.org/mailman/listinfo/python-list


Test message - please ignore

2008-12-27 Thread Skip Montanaro
Test post via gmane.  Please ignore.

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


Re: ctypes & strings

2008-12-27 Thread Mark Tolonen


"Hendrik van Rooyen"  wrote in message 
news:000f01c96822$5c300f00$0d00a...@hendrik...


Red Rackham  wrote:

I would like to pass a string into a dll function.  I notice that to pass 
using
ctypes, it has to be a ctypes type.  >Looking at the ctypes doc page I 
don't see

a c_string class.

The following seems to work for me:

In the c programme:

/*
   This routine outputs and inputs a symmetric block of bytes, writing
   the outputs out and reading the inputs in, replacing the chars in the
   original output string
*/

unsigned char read_write (unsigned char *inputs, unsigned char *outputs, 
int

lenin, int lenout)
   {
   int i = 0;
   int addr = 0;
   int addrhi = 0;
   int oind = 0;
   char rv;
   while (i < lenin)
   {
   addrhi = i >> 8 & 0x3f; // we can have 64 * 256 adresses in the 
top

6 + 8 bits
   addr = i & 0xff;// this is low order
   rv = put_1(addrhi|0xd0);// put out the addy
   rv = put_3(addr);
   rv = put_1(addrhi|0x90);// make a read strobe on bit 6
   inputs[i] = get_0() ^ 255;  // read the char
   rv = put_1(addr|0xd0);  // raise strobe again
   i++;
   }

   while (i < lenin + lenout)
   {
   oind = i - lenin;   // index into outputs start at 0
   addrhi = i >> 8 & 0x3f; // we can have 64 * 256 adresses in the 
top

6 + 8 bits
   addr = i & 0xff;// this is low order
   rv = put_1(addrhi|0xd0);// put out the addy
   rv = put_3(addr);
   rv = put_0(outputs[oind]);  // put out the output
   rv = put_1(addrhi|0x50);// make a write strobe on bit 7
   rv = put_1(addrhi|0xd0);// raise write strobe again
   i++;
   }
   return *inputs;
   }


Then in the python it is used as follows:

import sys, os, ctypes, time

io = ctypes.cdll.LoadLibrary('./lib_gpio.a')

def do_io(ins, outs): # ins and outs are normal python strings that must 
exist


   # this routine happens to work by side effect - the ins string is
changed by the c routine

r = io.read_write(ins,outs,len(ins),len(outs))
return

The c routine will actually break Python's normal string
immmutability and give you back a changed ins.

It is in general not a good idea to change the passed string
like I am doing - but you wanted to know how
to pass a python string, and the outs example should get
you going - a string is an array of characters in c...


Here's an example using mutable buffers:

 x.c --> x.dll ---
#include 

__declspec(dllexport)
int fill(char *buf,int len)
{
   strncpy(buf,"initialized string",len-1);
   buf[len-1]=0;
   return strlen(buf);
}
--

PythonWin 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit 
(Intel)] on win32.
Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for 
further copyright information.

from ctypes import *
x=CDLL('x.dll')
s=create_string_buffer('string',10)
s.raw

'string\x00\x00\x00\x00'

s.value

'string'

x.fill(s,len(s))

9

s.raw

'initializ\x00'

s.value

'initializ'

s=create_string_buffer('string',20)
x.fill(s,len(s))

18

s.raw

'initialized string\x00\x00'

s.value

'initialized string'

--Mark


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


Test message via Google Groups

2008-12-27 Thread Skip Montanaro
Test message - please ignore.

Skip

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


Re: parsing csv files class

2008-12-27 Thread Chris Rebert
On Sat, Dec 27, 2008 at 5:02 AM, Gary M. Josack  wrote:
> alex goretoy wrote:
>>
>> I know it's messy with all those self.soc.* functions, but it works in one
>> of my current project. I just want to make it more pythonic I also want to
>> add capability for makeing csv file if I give it input like:
>> 1234,something nice, hey this is something nice
>> 2468,something else, something else
>>
>> On Sat, Dec 27, 2008 at 4:54 AM, alex goretoy > > wrote:
>>
>>Hello All,
>>
>>I have this class that I use in one of my projects. I know it's
>>missing functionality and some things could have been done
>>differently. Can you ehlp me make this class better? What can I do
>>to make it more resistant to error? You can find the
>>stdout_colours class on Google if you want it, JFGI I want to make
>>it more pythonic. I come from a PHP background, can you tell?
>>
>>Any and all help is appreciated
>>-Alex
>>
>>#!/usr/bin/env python
>>from ctypes import *
>>import os, sys, types, csv, urllib, urllib2, urlparse,
>>string,stdout_colours
>>
>>class parsercsvy(object):
>>"""Return a line from a csv file or total amount of lines"""

> Do you know that there is a csv module in the standard library already?
>
> Thanks,
> Gary M. Josack

And here's a link to the docs for the `csv` module:
http://docs.python.org/library/csv.html

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes & strings

2008-12-27 Thread Gabriel Genellina
En Sat, 27 Dec 2008 10:54:32 -0200, Hendrik van Rooyen  
 escribió:



The c routine will actually break Python's normal string
immmutability and give you back a changed ins.


...so don't do that!
If you require a mutable string to pass to C functions, use  
ctypes.create_string_buffer()



It is in general not a good idea to change the passed string
like I am doing - but you wanted to know how
to pass a python string, and the outs example should get
you going - a string is an array of characters in c...


Hmmm, I don't think posting a potentially harmful example is actually a  
good idea...


--
Gabriel Genellina

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


[2.4.4] creating a datetime.datetime from an XML xs:dateTime

2008-12-27 Thread Martin
Hi,

subject says it all. I'd like to create a python datetime.datetime
from an xs:string[1]

I know about time.strptime and could get to a datetime.datetime from
there, but that would be error prone like:

 * are there time zones?
 * how is the year represented
 * etc, yadda yadda

so I'd rather like to find some method (hopefully within the standard
lib) that had more peer review than only me :).

thanks
martin

[1] http://www.w3.org/TR/xmlschema-2/#dateTime
-- 
http://soup.alt.delete.co.at
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher

You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: parsing csv files class

2008-12-27 Thread Gabriel Genellina
En Sat, 27 Dec 2008 11:02:13 -0200, Gary M. Josack   
escribió:

alex goretoy wrote:


I know it's messy with all those self.soc.* functions, but it works in  
one of my current project. I just want to make it more pythonic I also  
want to add capability for makeing csv file if I give it input like:

1234,something nice, hey this is something nice
2468,something else, something else



reader = csv.reader(file(filename, "rb"))
try:
for row in reader:
self.buffer.append(row)
s,a=[],{}



Do you know that there is a csv module in the standard library already?


I'd say he already knows, he even used it in the code.

To Alex G.: I didn't understand what's the purpose of this class. Probably  
you want to do too much things in the same place; looks like it has  
multiple concerns, and that's not a good idea usually.


--
Gabriel Genellina

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


Re: [2.4.4] creating a datetime.datetime from an XML xs:dateTime

2008-12-27 Thread skip

martin> subject says it all. I'd like to create a python
martin> datetime.datetime from an xs:string[1]
...
martin> so I'd rather like to find some method (hopefully within the
martin> standard lib) that had more peer review than only me :).

Not in the standard lib, but you might want to check out the dateutil
package.  Its dateutil.parser module does an excellent job parsing a wide
range of time formats.  It's a bit weak in the timezone area though.

-- 
Skip Montanaro - s...@pobox.com - http://smontanaro.dyndns.org/
--
http://mail.python.org/mailman/listinfo/python-list


Re: tkinter 3.0 multiple keyboard events together

2008-12-27 Thread janislaw
On 27 Gru, 15:08, Pavel Kosina  wrote:
> janislaw napsal(a):
> > Use google to find the appropriate site, or browse this site, there
> > are plenty of examples. You may want to examine the code I wrote to
> > you to catch the idea:
> > #--
> > import Tkinter
> > import pprint
>
> > tk = Tkinter.Tk()
> > f = Tkinter.Frame(tk, width=100, height=100)
> > msg = Tkinter.StringVar()
> > msg.set('Hello')
> > l = Tkinter.Label(f,  textvariable=msg)
> > l.pack()
> > f.pack()
>
> > keys = set()
>
> > def keyPressHandler(event):
> >     keys.add(event.char)
> >     display()
>
> > def keyReleaseHandler(event):
> >     keys.remove(event.char)
> >     display()
>
> > def display():
> >     msg.set(str(keys))
>
> > f.bind_all('', keyPressHandler)
> > f.bind_all('', keyReleaseHandler)
>
> > f.mainloop()
>
> Is this really the most simple solution how to do this in Tkinter? Is
> there nothing cleaner "build inside"?

Um, I could be only guessing what are you meant to do, unless you
describe your problem in more detailed way. I.e. describe the desired
behaviour, show code which you have, and describe the current
behaviour.

> This solution has disadvantage that after you release one key, that the
> function keyPressHandler stopped to be called by the other pressed keys.
> I would have not to build moving the player in KeyPresshandler, but on
> another new function that would have to read periodically "keys" and to
> act afterwards

Hmmm. Maybe you'd like to hook into Tkinter event loop, i.e. by custom
events if you don't like periodical polling.

>From what I am guessing, you expect that 2 keyboard events are
simultaneous and can be cached at the same time instant. Well that
would be impossible, cause all the underlying hardware is sequential.
You may have parallel stuff if you go down to VHDL and write your own
hardware, but when you have a CPU, then you'll have to stick with
writing programs. C'mon, 20 lines is not such a big deal.

Regards
JW
--
http://mail.python.org/mailman/listinfo/python-list


Re: [2.4.4] creating a datetime.datetime from an XML xs:dateTime

2008-12-27 Thread Martin
Hi,


2008/12/27  :
> Not in the standard lib, but you might want to check out the dateutil
> package.  Its dateutil.parser module does an excellent job parsing a wide
> range of time formats.  It's a bit weak in the timezone area though.

is that http://labix.org/python-dateutil, I'll have a look at it.


-- 
http://soup.alt.delete.co.at
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher

You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
--
http://mail.python.org/mailman/listinfo/python-list


[OT] game engine (as in rules not graphics)

2008-12-27 Thread Martin
Hello,

I'd like to get in touch with game development a bit. I'm not talking
about graphics but rather the game rules itself. Something like
http://en.wikipedia.org/wiki/Monopoly_(game)#Rules, is there even a
general approach to that or should I just go sketch up my rules and
try to implement them. Being totally new to this topic I don't quite
now what to search for to get some decent results that let me make a
mental link between game rules and what the best practices are to
implement them in python (or any other programming language)

thanks,
martin

-- 
http://soup.alt.delete.co.at
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher

You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: [2.4.4] creating a datetime.datetime from an XML xs:dateTime

2008-12-27 Thread skip

martin> is that http://labix.org/python-dateutil, I'll have a look at
martin> it.

Yup.  PyPI:

http://pypi.python.org/pypi/python-dateutil/1.4.1

Skip
--
http://mail.python.org/mailman/listinfo/python-list


C API: array of floats/ints from python to C and back

2008-12-27 Thread Daniel Fetchinson
I'm trying to write an extension module in C which contains a single
function with the following prototype:

void func( int N, int * arg1, int * arg2, int * ret );

Here arg1 and arg2 are length N arrays, and the function computes ret
which is also an N length array. From python I'd like to call this
function as

ret = func( [ 1, 2, 3 ], [ 2, 3, 4] )

I've read through the docs at
http://docs.python.org/extending/extending.html and also
http://docs.python.org/c-api/arg.html from which I learned how to pass
a fixed number of basic datatypes (int, float, string) from python to
C and back. What I don't know is how to pass an array back and forth.

As far as I can see PyArg_ParseTuple is the function I should use for
converting the python arguments to C but the format string passed to
PyArg_ParseTuple can only be one of the basic data types ("s", "i",
etc) or a generic python object ("O").

Does this mean that I can only pass the arrays from python to C as
generic python objects and in a later operation I need to get the
elements from this generic python object, construct a C array and pass
that to the C function? Since I assume this is a frequent operation
I'd think there are some shortcuts, aren't there? Or what's the
simplest way of doing this?

Cheers,
Daniel

-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3 and my Mac (Leopard)

2008-12-27 Thread scott

Dan wrote:
Wanted to learn python, got Mark Summerfield's new book "Programming in 
Python 3".  Having a hard time getting python 3 and IDLE working on my 
Mac with Leopard.  The mac "resources" on the python.org site seem a bit 
out of date, and don't really mention python 3.  Are there any resources 
out there?  Is the python community just not interested in Macs?  I've 
tried googling and the usual search strategies.  Any help would be 
appreciated.


DAN



Activestate has a Python 3.0 distribution for 8 or 9 platforms including 
Mac OS X:

http://www.activestate.com/Products/activepython/python3.mhtml
IDLE works well here on Leopard (calltips etc) and they have bundled 
documentation in Apple help format.


Note:
I installed ActiveTcl prior to installing Python 3.0:
http://www.activestate.com/Products/activetcl/index.mhtml
I'm not sure if this was necessary for proper IDLE function or not. Both 
ActivePython 3.0 and ActiveTcl installs were painless.

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


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Scott David Daniels

Daniel Fetchinson wrote:

I'm trying to write an extension module in C which contains a single
function with the following prototype:
void func( int N, int * arg1, int * arg2, int * ret );
Here arg1 and arg2 are length N arrays, and the function computes ret
which is also an N length array. From python I'd like to call this
function as
ret = func( [ 1, 2, 3 ], [ 2, 3, 4] )


This requirement pretty much dictates the slow answer you have.

> Does this mean that I can only pass the arrays from python to C as
> generic python objects and in a later operation I need to get the
> elements from this generic python object, construct a C array and pass
> that to the C function? ... What's the simplest way of doing this?

Either use ctypes, look into array.array, or look into numpy.array.

I'd just use numpy, myself:
import numpy
total = numpy.array([1, 2, 3]) + numpy.array([2, 3, 4])

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: multiply each element of a list by a number

2008-12-27 Thread Colin J. Williams

Méta-MCI (MVP) wrote:

Hi!


map(multby3, (1, 2, 3, ))


with lambda: map(lambda x: x*3, [1,2,3])

@-salutations


More lines but perhaps faster than numpy:

PythonWin 2.5.4 (r254:67916, Dec 23 
2008, 15:10:54) [MSC v.1310 32 bit 
(Intel)] on win32.
Portions Copyright 1994-2008 Mark 
Hammond - see 'Help/About PythonWin' for 
further copyright information.

lst= [1,2,3]
trippleList= [3*a for a in lst]
trippleList

[3, 6, 9]




Colin W.
--
http://mail.python.org/mailman/listinfo/python-list


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Daniel Fetchinson
>> I'm trying to write an extension module in C which contains a single
>> function with the following prototype:
>> void func( int N, int * arg1, int * arg2, int * ret );
>> Here arg1 and arg2 are length N arrays, and the function computes ret
>> which is also an N length array. From python I'd like to call this
>> function as
>> ret = func( [ 1, 2, 3 ], [ 2, 3, 4] )
>
> This requirement pretty much dictates the slow answer you have.

I'm not sure I understand what you mean.

>  > Does this mean that I can only pass the arrays from python to C as
>  > generic python objects and in a later operation I need to get the
>  > elements from this generic python object, construct a C array and pass
>  > that to the C function? ... What's the simplest way of doing this?
>
> Either use ctypes, look into array.array, or look into numpy.array.

I have considered using ctypes but for my needs using the C API
directly seems more reasonable. array.array and numpy.array doesn't
fit my needs since I need to do long and complicated operations on the
two (pretty large) integer arrays that would be too slow using
array.array and numpy.array (I've verified this claim by benchmarking
a numpy.array based solution).

> I'd just use numpy, myself:
>  import numpy
>  total = numpy.array([1, 2, 3]) + numpy.array([2, 3, 4])

What makes you think I want to add two arrays?

Cheers,
Daniel


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list


Re: multiply each element of a list by a number

2008-12-27 Thread skip

Colin> ... perhaps faster than numpy:
...

For extremely short lists, but not for much else:

% for n in 1 10 100 1000 1 10 ; do
>   echo "len:" $n
>   echo -n "numpy: "
>   python -m timeit -s 'import numpy ; a = numpy.array(range('$n'))' 'a*3'
>   echo -n "list: "
>   python -m timeit -s 'a = range('$n')' '[3*x for x in a]'
> done
len: 1
numpy: 10 loops, best of 3: 11.7 usec per loop
list: 100 loops, best of 3: 0.698 usec per loop
len: 10
numpy: 10 loops, best of 3: 11.7 usec per loop
list: 10 loops, best of 3: 2.94 usec per loop
len: 100
numpy: 10 loops, best of 3: 12.1 usec per loop
list: 1 loops, best of 3: 24.4 usec per loop
len: 1000
numpy: 10 loops, best of 3: 15 usec per loop
list: 1000 loops, best of 3: 224 usec per loop
len: 1
numpy: 1 loops, best of 3: 41 usec per loop
list: 100 loops, best of 3: 2.17 msec per loop
len: 10
numpy: 1000 loops, best of 3: 301 usec per loop
list: 10 loops, best of 3: 22.2 msec per loop

This is with Python 2.4.5 on Solaris 10.  YMMV.

-- 
Skip Montanaro - s...@pobox.com - http://smontanaro.dyndns.org/
--
http://mail.python.org/mailman/listinfo/python-list


Re: parsing csv files class

2008-12-27 Thread alex goretoy
It's main concern is to grab the file into the buffer for use in other
classes. The self.soc.me_him is just so I know when its ENTERING or EXITING
in that function when I run it in terminal. It's prints it in color. My main
concern right now is to add error handling. I'm still learning how to use
assert and yield, but I got try,except down pat.  All this class does is
grabs the file either from http or local storage into a buffer and I use
that in other classes. I want to add support for creating creating csv as
well. I read the doc on csv and  will try some things out soon. Thanks for
helping me.


On Sat, Dec 27, 2008 at 2:15 PM, Gabriel Genellina
wrote:

> En Sat, 27 Dec 2008 11:02:13 -0200, Gary M. Josack 
> escribió:
>
>> alex goretoy wrote:
>>
>
>  I know it's messy with all those self.soc.* functions, but it works in one
>>> of my current project. I just want to make it more pythonic I also want to
>>> add capability for makeing csv file if I give it input like:
>>> 1234,something nice, hey this is something nice
>>> 2468,something else, something else
>>>
>>
> reader = csv.reader(file(filename, "rb"))
>>>try:
>>>for row in reader:
>>>self.buffer.append(row)
>>>s,a=[],{}
>>>
>>
>  Do you know that there is a csv module in the standard library already?
>>
>
> I'd say he already knows, he even used it in the code.
>
> To Alex G.: I didn't understand what's the purpose of this class. Probably
> you want to do too much things in the same place; looks like it has multiple
> concerns, and that's not a good idea usually.
>
> --
> Gabriel Genellina
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я



-- 
А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
--
http://mail.python.org/mailman/listinfo/python-list


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Scott David Daniels

Daniel Fetchinson wrote:

I have considered using ctypes but for my needs using the C API
directly seems more reasonable. array.array and numpy.array doesn't
fit my needs since I need to do long and complicated operations on the
two (pretty large) integer arrays that would be too slow using
array.array and numpy.array (I've verified this claim by benchmarking
a numpy.array based solution).


OK, but if you go to array.array or numpy.array or ctypes, you can
create a uniformly typed (C datatype) array , which is the key to
getting any speed out of the deal.  If you insist on using python
lists, you are stuck with extracting data element by element from
its Python language wrap.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: parsing csv files class

2008-12-27 Thread alex goretoy
I might as well add a UnicodeReader and UnicodeWriter and support for
passing delimiter in from other classes.  At the same time. Anyone have a
good csv class I can pick thru? I'm planning on posting my mysql adn curl
classes that I use in my projects I just want to make them better and have
less error.

On Sat, Dec 27, 2008 at 5:18 PM, alex goretoy
wrote:

> It's main concern is to grab the file into the buffer for use in other
> classes. The self.soc.me_him is just so I know when its ENTERING or EXITING
> in that function when I run it in terminal. It's prints it in color. My main
> concern right now is to add error handling. I'm still learning how to use
> assert and yield, but I got try,except down pat.  All this class does is
> grabs the file either from http or local storage into a buffer and I use
> that in other classes. I want to add support for creating creating csv as
> well. I read the doc on csv and  will try some things out soon. Thanks for
> helping me.
>
>
> On Sat, Dec 27, 2008 at 2:15 PM, Gabriel Genellina  > wrote:
>
>> En Sat, 27 Dec 2008 11:02:13 -0200, Gary M. Josack 
>> escribió:
>>
>>> alex goretoy wrote:
>>>
>>
>>  I know it's messy with all those self.soc.* functions, but it works in
 one of my current project. I just want to make it more pythonic I also want
 to add capability for makeing csv file if I give it input like:
 1234,something nice, hey this is something nice
 2468,something else, something else

>>>
>> reader = csv.reader(file(filename, "rb"))
try:
for row in reader:
self.buffer.append(row)
s,a=[],{}

>>>
>>  Do you know that there is a csv module in the standard library already?
>>>
>>
>> I'd say he already knows, he even used it in the code.
>>
>> To Alex G.: I didn't understand what's the purpose of this class. Probably
>> you want to do too much things in the same place; looks like it has multiple
>> concerns, and that's not a good idea usually.
>>
>> --
>> Gabriel Genellina
>>
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
>
> --
> А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
> а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
>
>
>
> --
> А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
> а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
>



-- 
А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
--
http://mail.python.org/mailman/listinfo/python-list


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Daniel Fetchinson
>> I have considered using ctypes but for my needs using the C API
>> directly seems more reasonable. array.array and numpy.array doesn't
>> fit my needs since I need to do long and complicated operations on the
>> two (pretty large) integer arrays that would be too slow using
>> array.array and numpy.array (I've verified this claim by benchmarking
>> a numpy.array based solution).
>
> OK, but if you go to array.array or numpy.array or ctypes, you can
> create a uniformly typed (C datatype) array , which is the key to
> getting any speed out of the deal.  If you insist on using python
> lists, you are stuck with extracting data element by element from
> its Python language wrap.

I thought that going through the python C API will be more
cross-platform than using ctypes, but I might be wrong. Anyway, the C
API for such a simple thing as a single function doesn't seem too
scary :)

I agree that array.array is more efficient than a list but the input
for my function will come from PIL and PIL returns a list. So I have a
list to begin with which will be passed to the C function. I assume
converting the list to an array.array and passing that to the C
function doesn't make any difference in terms of speed since the
operation itself will be done in the C function anyway.

Cheers,
Daniel

-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list


how to make a class to get all forms and input fields from webpage

2008-12-27 Thread alex goretoy
Hello All,

How do I make a class for retrieving all forms and input fields on a web
page. Meaning, form name, form url,all input fields,
text,textarea,select,etc...I have something currently and it kinda works. It
also kinda works for retrieving all images on webpages. My main concer here
is to make is retrive forms and input fields into a dict that has this kinda
of outline: Maybe not exactly like this, but something to this nature. So I
can use it in other classes.

forms=
{"name":{"url":{"""input_name1":"","input_name_2","select_input":{"value1":"","value2":"selected","value3":""

This is what I have made a long time ago, I haven't touched it in a while,
but I need to make it work with some other classes. I need tit to receive
all forms and inputs for me. What is a more efficient way to do this? My
background is in PHP and I want to extend my python knowledge. I came up
with this script by Googling something a long time ago and making
modifications and additions to it. You can find the stdout_colours class on
line, JFGI. It's allows for display of caller and called function and colors
output to terminal. Thanks for your help my friends.

#!/usr/bin/python
import urllib, urllib2
import re, sys,string
import os, sys, Image
from sgmllib import SGMLParser
from urlparse import urlparse
import stdout_colours
class URLPacker(SGMLParser):

def __init__(self,colorize,caller):
self.colorize=colorize
self.caller=caller
self.reset()
def reset(self):
SGMLParser.reset(self)
self.func_me_color="white_on_black"

self.soc=stdout_colours.stdout_colors(self.colorize,self.caller)
self.soc.me_him(['ENTER:',__name__],self.func_me_color)

self.urls = {}
self.imgs = {}
self.forms = {}
self.inputs = {}
self.action = ""
self.method = ""
self.url=""
self.path=""
self.source=""
self.dirname=""
self.level=0
self.max_depth=4
self.urlRExp = re.compile('[.]htm$|[.]html$|[.]shtml$|[/]$|[.]php$',
re.IGNORECASE)
self.fileRExp = re.compile('[\/:*?"<>|]')
self.formats=[]

self.soc.me_him(['EXIT:',__name__],self.func_me_color)
def start_a(self,attrs):
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.soc.w(attrs,"red")
href = [v for k,v in attrs if k=='href']
for value in href:
if self.urls.has_key(value):
pass
else:
self.urls[value]=0

self.soc.me_him(['EXIT:',__name__],self.func_me_color)

def start_img(self,attrs):
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.soc.w(attrs,"blue")
src = [v for k,v in attrs if k=="src"]
for value in src:
if self.imgs.has_key(value):
pass
else:
self.imgs[value]=0

self.soc.me_him(['EXIT:',__name__],self.func_me_color)
def start_form(self,attrs):
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.soc.w(attrs,"green")
method = [v for k, v in attrs if k=='method']
action = [v for k,v in attrs if k=="action"]
if string.join(method,"") != "":
self.method=method[0]
else:
self.method="post"

self.action=action[0]

self.soc.w(action,"white_on_green")
self.soc.w(method,"white_on_blue")
self.forms[self.action]={}

self.forms[self.action][self.method]={}

self.soc.me_him(['EXIT:',__name__],self.func_me_color)
def start_input(self,attrs):
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.soc.w(attrs,"yellow")
name = [v for k, v in attrs if k=='name']
value = [v for k,v in attrs if k=="value"]
if string.join(name,"") !="":
if string.join(value,"")!="":
self.forms[self.action][self.method][name[0]]=value[0]
else:
self.forms[self.action][self.method][name[0]]=""
self.soc.w(self.forms,"white_on_gold")
self.soc.me_him(['EXIT:',__name__],self.func_me_color)
def url_dirname(self, url):
self.soc.me_him(['RETURN:',__name__],self.func_me_color)
#print url
return self.fileRExp.sub('_',url)
def dirname(self,url):
self.soc.me_him(['RETURN:',__name__],self.func_me_color)
return os.path.dirname(url)
def save_images(self,minsize):
if os.path.isdir(self.path+self.dirname):
return False # aborting, dir exists
else:
a=self.path+self.dirname
os.mkdir(a)
os.chdir(a)
print self.url
for img in self.imgs.keys():
loc=self.url_dirname(img)
print loc, img, a
try:
if self.imgs[img]==0:
#urllib.urlretrieve(img,loc)
#os.sy

math module for Decimals

2008-12-27 Thread jerry . carl . mi
Hi,

I have been looking for a Python module with math functions that would
both eat and spit Decimals. The standard math module eats Decimals
allright but spits floats... herefore exp(sin(Decimal())) produces exp
() of a float :-(

So far, i found:

-AJDecimalMathAdditions (http://www.ajgs.com/programming/
PythonForDownload.html)
-decimalfuncs (http://pypi.python.org/pypi/decimalfuncs/1.4)
-and dmath (http://pypi.python.org/pypi/dmath/0.9)

I tried using the AJDecimalMathAdditions, but ran into issues like dSin
(1E4) would take forever to calculate and would result in sin() >
1 ... If i understand it correctly, the program is using maclaurin
series to calculate the value and since it does not chop off all the
multiples of 2*pi, the maclaurin approximation becomes useless when
its too far from x=0.

I also ran into some issues with the decimalfuncs, but i have never
tried the dmath thing.

Now, i can go and start modifying these modules to behave more like
the standard math module but since i am neither mathematician or
programer, i really hesitate.

So my questions are:

(1) what do folks use when they need to calculate something like exp
(sin(Decimal())) or even more complex things? Any recommendations? Or
am I completely missing something?

(2) Is there any plan to provide a standard python module that would
do that? (Python 4.0? ;-)

Thanks for your comments and help,

Jerry
--
http://mail.python.org/mailman/listinfo/python-list


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Scott David Daniels

Daniel Fetchinson wrote:

 I have a list to begin with which will be passed to the C function.

> I assume converting the list to an array.array and passing that to the C

function doesn't make any difference in terms of speed since the
operation itself will be done in the C function anyway.


Right, but why bother to do the conversion in C where you'll have to
fiddle with refcounts and error propogation?  convert in python, and
go to the underlying data in C.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes & strings

2008-12-27 Thread Aaron Brady
On Dec 27, 2:03 pm, "Gabriel Genellina" 
wrote:
> En Sat, 27 Dec 2008 10:54:32 -0200, Hendrik van Rooyen  
>  escribió:
>
> > The c routine will actually break Python's normal string
> > immmutability and give you back a changed ins.
>
> ...so don't do that!
> If you require a mutable string to pass to C functions, use  
> ctypes.create_string_buffer()
>
> > It is in general not a good idea to change the passed string
> > like I am doing - but you wanted to know how
> > to pass a python string, and the outs example should get
> > you going - a string is an array of characters in c...
>
> Hmmm, I don't think posting a potentially harmful example is actually a  
> good idea...
>
> --
> Gabriel Genellina

One alternative would be to return a pair of Python strings.  You
might want to wrap the C function you have already in another one,
which allocates buffers and creates Python objects.

Hendrik is right, though.  The ctypes module allows us to break string
immutability within Python code.
--
http://mail.python.org/mailman/listinfo/python-list


Re: game engine (as in rules not graphics)

2008-12-27 Thread Aaron Brady
On Dec 27, 3:02 pm, Martin  wrote:
> Hello,
>
> I'd like to get in touch with game development a bit. I'm not talking
> about graphics but rather the game rules itself. Something 
> likehttp://en.wikipedia.org/wiki/Monopoly_(game)#Rules, is there even a
> general approach to that or should I just go sketch up my rules and
> try to implement them. Being totally new to this topic I don't quite
> now what to search for to get some decent results that let me make a
> mental link between game rules and what the best practices are to
> implement them in python (or any other programming language)
>
> thanks,
> martin

Not my expertise but here are my $0.02.  You are looking for ways to
represent rules: buying a house is legal in such and such situation,
and the formula for calculating its price is something.  You want
"predicates" such as InJail, OwnedBy, Costs.

Costs( New York Ave, 200 )
InJail( player2 )
OwnedBy( St. Charles Ave, player4 )
LegalMove( rolldie )
LegalMove( sellhouse )

There are rule-based languages out there, such as Prolog.  They are
equally expressive as Python (you can write a Turing machine in them),
but they are more convenient for representing rules in a prog.
language.

Predicates are more or less equivalent to positive assertions about
something.

NewYorkAve.cost= 200
player2.injail= True...
rolldie.islegal= True

Some predicates have to be calculated, rather than stored, but Python
descriptors go easy on you for that.  Someone else will have to tell
you how rule-based programming measures up against object-oriented
programming.  
--
http://mail.python.org/mailman/listinfo/python-list


Re: parsing csv files class

2008-12-27 Thread alex goretoy
How would I change this to work with numpy: (Would it be more feasible for
storing big files? 300-30,000 records)
 for row in reader:
self.buffer.append(row)
s,a=[],{}

for j in range(len(self.buffer[0])):
a[self.buffer[0][j]]=row[j]
self.bufferp.append(a)
i+=1
self.total = i-1

Instead of doing self.buffer.append(row)

would I initialize numpy like this: self.buffer=numpy.array() and then in
the loop do:
self.buffer.append(row) ? How do I achieve the same for lower code, where it
appends to a dict with header of each column? Thank you.

On Sat, Dec 27, 2008 at 5:31 PM, alex goretoy
wrote:

> I might as well add a UnicodeReader and UnicodeWriter and support for
> passing delimiter in from other classes.  At the same time. Anyone have a
> good csv class I can pick thru? I'm planning on posting my mysql adn curl
> classes that I use in my projects I just want to make them better and have
> less error.
>
>
> On Sat, Dec 27, 2008 at 5:18 PM, alex goretoy  > wrote:
>
>> It's main concern is to grab the file into the buffer for use in other
>> classes. The self.soc.me_him is just so I know when its ENTERING or EXITING
>> in that function when I run it in terminal. It's prints it in color. My main
>> concern right now is to add error handling. I'm still learning how to use
>> assert and yield, but I got try,except down pat.  All this class does is
>> grabs the file either from http or local storage into a buffer and I use
>> that in other classes. I want to add support for creating creating csv as
>> well. I read the doc on csv and  will try some things out soon. Thanks for
>> helping me.
>>
>>
>> On Sat, Dec 27, 2008 at 2:15 PM, Gabriel Genellina <
>> gagsl-...@yahoo.com.ar> wrote:
>>
>>> En Sat, 27 Dec 2008 11:02:13 -0200, Gary M. Josack 
>>> escribió:
>>>
 alex goretoy wrote:

>>>
>>>  I know it's messy with all those self.soc.* functions, but it works in
> one of my current project. I just want to make it more pythonic I also 
> want
> to add capability for makeing csv file if I give it input like:
> 1234,something nice, hey this is something nice
> 2468,something else, something else
>

>>> reader = csv.reader(file(filename, "rb"))
>try:
>for row in reader:
>self.buffer.append(row)
>s,a=[],{}
>

>>>  Do you know that there is a csv module in the standard library already?

>>>
>>> I'd say he already knows, he even used it in the code.
>>>
>>> To Alex G.: I didn't understand what's the purpose of this class.
>>> Probably you want to do too much things in the same place; looks like it has
>>> multiple concerns, and that's not a good idea usually.
>>>
>>> --
>>> Gabriel Genellina
>>>
>>>
>>> --
>>> http://mail.python.org/mailman/listinfo/python-list
>>>
>>
>>
>>
>> --
>> А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
>> а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
>>
>>
>>
>> --
>> А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
>> а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
>>
>
>
>
> --
> А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
> а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
>



-- 
А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
--
http://mail.python.org/mailman/listinfo/python-list


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Robert Kern

Daniel Fetchinson wrote:


I agree that array.array is more efficient than a list but the input
for my function will come from PIL and PIL returns a list. So I have a
list to begin with which will be passed to the C function.


With recent versions of PIL, numpy can create an array from an Image very 
quickly, possibly without any copying of memory.


--
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

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


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Daniel Fetchinson
>> I agree that array.array is more efficient than a list but the input
>> for my function will come from PIL and PIL returns a list. So I have a
>> list to begin with which will be passed to the C function.
>
> With recent versions of PIL, numpy can create an array from an Image very
> quickly, possibly without any copying of memory.

What exactly do you mean? (1) PIL creates a list which can be easily
converted by numpy to a numpy.array or (2) with the help of numpy one
can create a numpy.array from an image directly?

Since I will have to pass the list or numy.array to C anyway I don't
see any advantage to (1) although (2) can be useful.

Daniel

-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Aaron Brady
On Dec 27, 6:06 pm, Scott David Daniels  wrote:
> Daniel Fetchinson wrote:
> > I have a list to begin with which will be passed to the C function.
>
>  > I assume converting the list to an array.array and passing that to the C
>
> > function doesn't make any difference in terms of speed since the
> > operation itself will be done in the C function anyway.
>
> Right, but why bother to do the conversion in C where you'll have to
> fiddle with refcounts and error propogation?  convert in python, and
> go to the underlying data in C.

I think you could use ctypes.c_int* 20, which is an awesome data
type-- a 20-element array of integers.  You could also use this, which
I just discovered, and is actually moderately disturbing.

#WARNING unsafe code
>>> from ctypes import *
>>> i= c_int(42)
>>> pi= pointer(i)
>>> pi[0]
42
>>> pi[1]
0
>>> pi[2]
0
>>> pi[1]= 20
>>> pi
<__main__.LP_c_long object at 0x00A97D00>
>>> pi[1]
20

What memory is it overwriting?
--
http://mail.python.org/mailman/listinfo/python-list


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Daniel Fetchinson
On 12/27/08, Robert Kern  wrote:
> Daniel Fetchinson wrote:
>
>> I agree that array.array is more efficient than a list but the input
>> for my function will come from PIL and PIL returns a list. So I have a
>> list to begin with which will be passed to the C function.
>
> With recent versions of PIL, numpy can create an array from an Image very
> quickly, possibly without any copying of memory.
>
> --
> 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
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Hi Robert, now that we are at it let me ask something related. Now I
managed to get my lists to C, operate on them in C and convert the
result back to a python tuple. Since this is my first C program that
uses the python C API I'm sure there are refcounting issues that I'm
not considering, maybe also memory management too.

This is the function I have, the corresponding python function will
take two equal length lists of integers and the C function will
compute their sum and return the result as a python tuple.


static PyObject *func( PyObject * self, PyObject * args )
{
int j, N;
int * src1, * src2;
PyObject *list1, *list2;

list1 = PyTuple_GetItem( args, 0 );
N = PyList_Size( list1 );
src1 = ( int * ) malloc( N * sizeof( int ) );
for( j = 0; j < N; j++ )
{
src1[j] = (int)PyInt_AsLong( PyList_GetItem( list1, j ) );
}

list2 = PyTuple_GetItem( args, 1 );
N = PyList_Size( list2 );
src2 = ( int * ) malloc( N * sizeof( int ) );
for( j = 0; j < N; j++ )
{
src2[j] = (int)PyInt_AsLong( PyList_GetItem( list2, j ) );
}

PyObject * tuple;
tuple = PyTuple_New( N );
for( j = 0; j < N; j++ )
{
PyTuple_SetItem( tuple, j, PyInt_FromLong( (long)( src1[j] +
src2[j] ) ) );
}

free( src1 );
free( src2 );

return tuple;
}

Do I have to free the memory occupied by the python objects list1 and
list2? Do I have to do any refcounting for list1, list2, tuple?

Any comment on the above code will be very appreciated! If I'm pushed
in the right direction I'm a fast learner but the beginning steps are
always hard :)

Cheers,
Daniel



-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Gabriel Genellina
En Sat, 27 Dec 2008 22:54:52 -0200, Daniel Fetchinson  
 escribió:



This is the function I have, the corresponding python function will
take two equal length lists of integers and the C function will
compute their sum and return the result as a python tuple.


static PyObject *func( PyObject * self, PyObject * args )
{
int j, N;
int * src1, * src2;
PyObject *list1, *list2;

list1 = PyTuple_GetItem( args, 0 );
N = PyList_Size( list1 );
src1 = ( int * ) malloc( N * sizeof( int ) );
for( j = 0; j < N; j++ )
{
src1[j] = (int)PyInt_AsLong( PyList_GetItem( list1, j ) );
}

list2 = PyTuple_GetItem( args, 1 );
N = PyList_Size( list2 );
src2 = ( int * ) malloc( N * sizeof( int ) );
for( j = 0; j < N; j++ )
{
src2[j] = (int)PyInt_AsLong( PyList_GetItem( list2, j ) );
}

PyObject * tuple;
tuple = PyTuple_New( N );
for( j = 0; j < N; j++ )
{
PyTuple_SetItem( tuple, j, PyInt_FromLong( (long)( src1[j] +
src2[j] ) ) );
}

free( src1 );
free( src2 );

return tuple;
}


As others already said, using a Numpy array or an array.array object would  
be more efficient (and even easier - the C code gets a pointer to an array  
of integers, as usual).



Do I have to free the memory occupied by the python objects list1 and
list2?


No. Usually you don't do that for any Python object - just  
increment/decrement its reference count (using Py_INCREF/Py_DECREF).



Do I have to do any refcounting for list1, list2, tuple?


In this case list1 and list2 come from PyTuple_GetItem; the docs say it  
returns a "borrowed reference" (that is, the function doesn't increment  
the refcount itself). So you don't have to decrement it yourself (and it  
isn't necesary to increment it in the first place, because the "args"  
tuple holds a reference, so the object can't disappear until the function  
exits, at least)



Any comment on the above code will be very appreciated! If I'm pushed
in the right direction I'm a fast learner but the beginning steps are
always hard :)


You MUST check EVERY function call for errors!
And check the argument's type (how do you know it is a list?). Once you  
are sure the first parameter *is* a list, you may use the "macro" version  
of several functions, like PyList_GET_SIZE and PyList_GET_ITEM.

You should check that both lists have the same length too.
And you should check that elements are integers, or convertible to  
integers (in case of error, PyInt_AsLong returns -1 and PyErr_Occurred()  
is true)
To fill the resulting tuple, use PyTuple_SET_ITEM instead. BTW, why return  
a tuple and not a list?


--
Gabriel Genellina

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


Re: Is there a function to remove escape characters from a string ?

2008-12-27 Thread Martin
2008/12/27 Stef Mientki :
> Steven D'Aprano wrote:
>> No, that only makes it even more confusing. What does Moore's Law have to
>> do with your willful ignorance about the existence of human languages other
>> than English?
>>
> Nothing.
> I even don't (want to) see what bits / bytes / escape sequences have to do
> with modern programming techniques,
> so I certainly don't see any relation between these and human languages.
>
> But the lack of Moore's law in software explains why we still need to
> concern about bits and bytes ;-)

http://www.joelonsoftware.com/articles/Unicode.html



-- 
http://soup.alt.delete.co.at
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher

You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: math module for Decimals

2008-12-27 Thread Gabriel Genellina

En Sat, 27 Dec 2008 22:02:51 -0200,  escribió:


I have been looking for a Python module with math functions that would
both eat and spit Decimals. The standard math module eats Decimals
allright but spits floats... herefore exp(sin(Decimal())) produces exp
() of a float :-(


Which math functions? ln, log10, exp, sqrt already exist as methods of  
Decimal instances. At the end of the Decimal docs there are a few  
examples, including computing sin and cos (but apparently they naïvely use  
a McLaurin series like you noticed in other module).
There is a way to convert a float into a Decimal object with no loss in  
precision, see the FAQ (using float.as_integer_ratio(), I think such  
function was implemented for exactly this use case)
So you might convert to float, operate, and go back -- if the precision of  
"double" operands is enough for you.


--
Gabriel Genellina

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


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Robert Kern

Daniel Fetchinson wrote:

I agree that array.array is more efficient than a list but the input
for my function will come from PIL and PIL returns a list. So I have a
list to begin with which will be passed to the C function.

With recent versions of PIL, numpy can create an array from an Image very
quickly, possibly without any copying of memory.


What exactly do you mean? (1) PIL creates a list which can be easily
converted by numpy to a numpy.array or (2) with the help of numpy one
can create a numpy.array from an image directly?


(2) a = numpy.asarray(img)


Since I will have to pass the list or numy.array to C anyway I don't
see any advantage to (1) although (2) can be useful.


(1) is still somewhat useful, if you're willing to bear the cost of the numpy 
dependency. The conversion code from any Python sequence to a numpy array of the 
desired type and dimensionality is a 2-liner (function call and error check). 
The memory is managed by numpy, so all you have to do is manage the refcount of 
the array object like any other Python object.


Okay, 5-liner given C's verbosity:

intx = PyArray_FROM_OTF(pyintx, PyArray_INT, NPY_IN_ARRAY);
if (!intx) {
PyErr_SetString(PyExc_ValueError, "intx must be an array of ints");
goto fail;
}

--
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

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


Re: how to make a class to get all forms and input fields from webpage

2008-12-27 Thread Gabriel Genellina
En Sat, 27 Dec 2008 21:54:02 -0200, alex goretoy  
 escribió:



How do I make a class for retrieving all forms and input fields on a web
page. Meaning, form name, form url,all input fields,
text,textarea,select,etc...I have something currently and it kinda


I'd use BeautifulSoup: http://pypi.python.org/pypi/BeautifulSoup/

--
Gabriel Genellina

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


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Daniel Fetchinson
>> This is the function I have, the corresponding python function will
>> take two equal length lists of integers and the C function will
>> compute their sum and return the result as a python tuple.
>>
>>
>> static PyObject *func( PyObject * self, PyObject * args )
>> {
>> int j, N;
>> int * src1, * src2;
>> PyObject *list1, *list2;
>>
>> list1 = PyTuple_GetItem( args, 0 );
>> N = PyList_Size( list1 );
>> src1 = ( int * ) malloc( N * sizeof( int ) );
>> for( j = 0; j < N; j++ )
>> {
>> src1[j] = (int)PyInt_AsLong( PyList_GetItem( list1, j ) );
>> }
>>
>> list2 = PyTuple_GetItem( args, 1 );
>> N = PyList_Size( list2 );
>> src2 = ( int * ) malloc( N * sizeof( int ) );
>> for( j = 0; j < N; j++ )
>> {
>> src2[j] = (int)PyInt_AsLong( PyList_GetItem( list2, j ) );
>> }
>>
>> PyObject * tuple;
>> tuple = PyTuple_New( N );
>> for( j = 0; j < N; j++ )
>> {
>> PyTuple_SetItem( tuple, j, PyInt_FromLong( (long)( src1[j] +
>> src2[j] ) ) );
>> }
>>
>> free( src1 );
>> free( src2 );
>>
>> return tuple;
>> }
>
> As others already said, using a Numpy array or an array.array object would
> be more efficient (and even easier - the C code gets a pointer to an array
> of integers, as usual).

Thanks, I didn't know that an array.array can be passed to C code as a
C pointer to an array. So I'll use an array.array because this is
really convenient.

>> Do I have to free the memory occupied by the python objects list1 and
>> list2?
>
> No. Usually you don't do that for any Python object - just
> increment/decrement its reference count (using Py_INCREF/Py_DECREF).

Great, one headache less :)

>> Do I have to do any refcounting for list1, list2, tuple?
>
> In this case list1 and list2 come from PyTuple_GetItem; the docs say it
> returns a "borrowed reference" (that is, the function doesn't increment
> the refcount itself). So you don't have to decrement it yourself (and it
> isn't necesary to increment it in the first place, because the "args"
> tuple holds a reference, so the object can't disappear until the function
> exits, at least)
>
>> Any comment on the above code will be very appreciated! If I'm pushed
>> in the right direction I'm a fast learner but the beginning steps are
>> always hard :)
>
> You MUST check EVERY function call for errors!

Yes, I know :)

> And check the argument's type (how do you know it is a list?). Once you
> are sure the first parameter *is* a list, you may use the "macro" version
> of several functions, like PyList_GET_SIZE and PyList_GET_ITEM.

The macro versions are preferable because they are faster?

> You should check that both lists have the same length too.
> And you should check that elements are integers, or convertible to
> integers (in case of error, PyInt_AsLong returns -1 and PyErr_Occurred()
> is true)
> To fill the resulting tuple, use PyTuple_SET_ITEM instead. BTW, why return
> a tuple and not a list?

No particular reason, other than the fact that I won't need to modify
these lists/tuples from python so whenever something will not change,
I use a tuple because it's immutable. Or this is not a very good
practice? There is no difference between lists and tuples in terms of
speed I suppose (getitem, setitem, etc).

Thanks a lot,
Daniel


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Daniel Fetchinson
 I agree that array.array is more efficient than a list but the input
 for my function will come from PIL and PIL returns a list. So I have a
 list to begin with which will be passed to the C function.
>>> With recent versions of PIL, numpy can create an array from an Image very
>>> quickly, possibly without any copying of memory.
>>
>> What exactly do you mean? (1) PIL creates a list which can be easily
>> converted by numpy to a numpy.array or (2) with the help of numpy one
>> can create a numpy.array from an image directly?
>
> (2) a = numpy.asarray(img)

Thanks, I didn't know about this, maybe it will be useful.

>> Since I will have to pass the list or numy.array to C anyway I don't
>> see any advantage to (1) although (2) can be useful.
>
> (1) is still somewhat useful, if you're willing to bear the cost of the
> numpy
> dependency. The conversion code from any Python sequence to a numpy array of
> the
> desired type and dimensionality is a 2-liner (function call and error
> check).
> The memory is managed by numpy, so all you have to do is manage the refcount
> of
> the array object like any other Python object.
>
> Okay, 5-liner given C's verbosity:
>
>  intx = PyArray_FROM_OTF(pyintx, PyArray_INT, NPY_IN_ARRAY);
>  if (!intx) {
>  PyErr_SetString(PyExc_ValueError, "intx must be an array of ints");
>  goto fail;
>  }

Yes, the dependency on numpy is my main concern. If it will help with
my problem I don't mind the dependency actually, so I'll do more
detailed benchmarks first.

Thank you,
Daniel



-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes & strings

2008-12-27 Thread Gabriel Genellina
En Sat, 27 Dec 2008 22:06:23 -0200, Aaron Brady   
escribió:

On Dec 27, 2:03 pm, "Gabriel Genellina" 
wrote:

En Sat, 27 Dec 2008 10:54:32 -0200, Hendrik van Rooyen  
 escribió:

> The c routine will actually break Python's normal string
> immmutability and give you back a changed ins.

Hmmm, I don't think posting a potentially harmful example is actually a  
good idea...


Hendrik is right, though.  The ctypes module allows us to break string
immutability within Python code.


Ok, it *can* be done, but it's far from being a recommended practice...  
and I would never use such wizardry in production code.


--
Gabriel Genellina

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


Re: Python 3 and my Mac (Leopard)

2008-12-27 Thread Kevin Walzer

Dan wrote:
Wanted to learn python, got Mark Summerfield's new book "Programming in 
Python 3".  Having a hard time getting python 3 and IDLE working on my 
Mac with Leopard.  The mac "resources" on the python.org site seem a bit 
out of date, and don't really mention python 3.  Are there any resources 
out there?  Is the python community just not interested in Macs?  I've 
tried googling and the usual search strategies.  Any help would be 
appreciated.


DAN



There's a mailing list devoted to Python on the Mac:

http://mail.python.org/mailman/listinfo/pythonmac-sig

It is a pretty good resource, better than the Python wiki, more up to date.

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Gabriel Genellina
En Sun, 28 Dec 2008 00:40:52 -0200, Daniel Fetchinson  
 escribió:



You MUST check EVERY function call for errors!


Yes, I know :)



Believe me, if you don't, there is a risk of crashing the program. And  
they're a lot harder to find and fix.



And check the argument's type (how do you know it is a list?). Once you
are sure the first parameter *is* a list, you may use the "macro"  
version

of several functions, like PyList_GET_SIZE and PyList_GET_ITEM.


The macro versions are preferable because they are faster?


Yes, because they don't do any additional checking. PyList_GET_ITEM just  
retrieves the item; PyList_GetItem checks whether it is called on a list  
object, then checks if index is out of bounds, and only then goes to  
retrieve the item.



You should check that both lists have the same length too.
And you should check that elements are integers, or convertible to
integers (in case of error, PyInt_AsLong returns -1 and PyErr_Occurred()
is true)
To fill the resulting tuple, use PyTuple_SET_ITEM instead. BTW, why  
return

a tuple and not a list?


No particular reason, other than the fact that I won't need to modify
these lists/tuples from python so whenever something will not change,
I use a tuple because it's immutable. Or this is not a very good
practice? There is no difference between lists and tuples in terms of
speed I suppose (getitem, setitem, etc).


Usually lists represent an ordered collection of similar items, where  
position is not relevant (the third item is treated more or less the same  
as the tenth); by example, a list of attendants to certain event. It makes  
sense to process the whole list doing the same thing to each element, and  
it makes sense to ask "is this item in the list?."


Tuples represent an ordered collection of dissimilar items, where position  
is relevant (because it identifies each item); by example, a row from a  
database table (name, address, age, phone number), or a point (x,y,z) in  
space. It isn't common to process the whole tuple doing the same thing for  
each element, and usually it doesn't make sense to look for certain item  
in the tuple. But it does make sense to refer to individual items like  
t[2], or t.age (namedtuple, 2.6 and up)


From this point of view, lists and tuples are conceptually different -  
tuples aren't "frozen" lists. But this is just common usage, or maybe  
historical intent; of course you are free to use whatever structure you  
feel adequate.


Once the list/tuple is created and filled, there is no speed difference  
accessing the individual items. Creating an empty list that grows one  
element at a time is slow for large lists (the memory block has to be  
re-allocated and copied over evry time it gets full) but this doesn't  
happen if you provide the final size when creating the list.


--
Gabriel Genellina

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


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Daniel Fetchinson
>> This is the function I have, the corresponding python function will
>> take two equal length lists of integers and the C function will
>> compute their sum and return the result as a python tuple.
>>
>>
>> static PyObject *func( PyObject * self, PyObject * args )
>> {
>> int j, N;
>> int * src1, * src2;
>> PyObject *list1, *list2;
>>
>> list1 = PyTuple_GetItem( args, 0 );
>> N = PyList_Size( list1 );
>> src1 = ( int * ) malloc( N * sizeof( int ) );
>> for( j = 0; j < N; j++ )
>> {
>> src1[j] = (int)PyInt_AsLong( PyList_GetItem( list1, j ) );
>> }
>>
>> list2 = PyTuple_GetItem( args, 1 );
>> N = PyList_Size( list2 );
>> src2 = ( int * ) malloc( N * sizeof( int ) );
>> for( j = 0; j < N; j++ )
>> {
>> src2[j] = (int)PyInt_AsLong( PyList_GetItem( list2, j ) );
>> }
>>
>> PyObject * tuple;
>> tuple = PyTuple_New( N );
>> for( j = 0; j < N; j++ )
>> {
>> PyTuple_SetItem( tuple, j, PyInt_FromLong( (long)( src1[j] +
>> src2[j] ) ) );
>> }
>>
>> free( src1 );
>> free( src2 );
>>
>> return tuple;
>> }
>
> As others already said, using a Numpy array or an array.array object would
> be more efficient (and even easier - the C code gets a pointer to an array
> of integers, as usual).

I looked for this in the C API docs but couldn't find anything on how
to make an array.array python object appear as a pointer to integers
(or floats, etc) in C code. On

http://docs.python.org/c-api/concrete.html#sequence-objects

There is only list and tuple or maybe you mean byte array? That has
only been introduced in python 2.6 and I'm working on 2.5.

Daniel

-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Daniel Fetchinson
>>> You MUST check EVERY function call for errors!
>>
>> Yes, I know :)
>>
>
> Believe me, if you don't, there is a risk of crashing the program. And
> they're a lot harder to find and fix.

Sure, what I meant by the smiley is just that it was a quick and dirty
example, not real code. In a real code I do check these sorts of
things.

>>> And check the argument's type (how do you know it is a list?). Once you
>>> are sure the first parameter *is* a list, you may use the "macro"
>>> version
>>> of several functions, like PyList_GET_SIZE and PyList_GET_ITEM.
>>
>> The macro versions are preferable because they are faster?
>
> Yes, because they don't do any additional checking. PyList_GET_ITEM just
> retrieves the item; PyList_GetItem checks whether it is called on a list
> object, then checks if index is out of bounds, and only then goes to
> retrieve the item.

Thanks, I was wondering what the difference between these versions
are, now it's clear.

>>> You should check that both lists have the same length too.
>>> And you should check that elements are integers, or convertible to
>>> integers (in case of error, PyInt_AsLong returns -1 and PyErr_Occurred()
>>> is true)
>>> To fill the resulting tuple, use PyTuple_SET_ITEM instead. BTW, why
>>> return
>>> a tuple and not a list?
>>
>> No particular reason, other than the fact that I won't need to modify
>> these lists/tuples from python so whenever something will not change,
>> I use a tuple because it's immutable. Or this is not a very good
>> practice? There is no difference between lists and tuples in terms of
>> speed I suppose (getitem, setitem, etc).
>
> Usually lists represent an ordered collection of similar items, where
> position is not relevant (the third item is treated more or less the same
> as the tenth); by example, a list of attendants to certain event. It makes
> sense to process the whole list doing the same thing to each element, and
> it makes sense to ask "is this item in the list?."
>
> Tuples represent an ordered collection of dissimilar items, where position
> is relevant (because it identifies each item); by example, a row from a
> database table (name, address, age, phone number), or a point (x,y,z) in
> space. It isn't common to process the whole tuple doing the same thing for
> each element, and usually it doesn't make sense to look for certain item
> in the tuple. But it does make sense to refer to individual items like
> t[2], or t.age (namedtuple, 2.6 and up)
>
>  From this point of view, lists and tuples are conceptually different -
> tuples aren't "frozen" lists. But this is just common usage, or maybe
> historical intent; of course you are free to use whatever structure you
> feel adequate.
>
> Once the list/tuple is created and filled, there is no speed difference
> accessing the individual items. Creating an empty list that grows one
> element at a time is slow for large lists (the memory block has to be
> re-allocated and copied over evry time it gets full) but this doesn't
> happen if you provide the final size when creating the list.

All right, this is clear then too, I'll probably use a list.

Cheers,
Daniel


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Ivan Illarionov
On Dec 28, 12:45 am, "Daniel Fetchinson" 
wrote:
> I'm trying to write an extension module in C which contains a single
> function with the following prototype:
>
> void func( int N, int * arg1, int * arg2, int * ret );
>
> Here arg1 and arg2 are length N arrays, and the function computes ret
> which is also an N length array. From python I'd like to call this
> function as
>
> ret = func( [ 1, 2, 3 ], [ 2, 3, 4] )
>
> I've read through the docs 
> athttp://docs.python.org/extending/extending.htmland 
> alsohttp://docs.python.org/c-api/arg.htmlfrom which I learned how to pass
> a fixed number of basic datatypes (int, float, string) from python to
> C and back. What I don't know is how to pass an array back and forth.
>
> As far as I can see PyArg_ParseTuple is the function I should use for
> converting the python arguments to C but the format string passed to
> PyArg_ParseTuple can only be one of the basic data types ("s", "i",
> etc) or a generic python object ("O").
>
> Does this mean that I can only pass the arrays from python to C as
> generic python objects and in a later operation I need to get the
> elements from this generic python object, construct a C array and pass
> that to the C function? Since I assume this is a frequent operation
> I'd think there are some shortcuts, aren't there? Or what's the
> simplest way of doing this?
>
> Cheers,
> Daniel
>
> --
> Psss, psss, put it down! -http://www.cafepress.com/putitdown

Hi Daniel,

Consider using this:
http://docs.python.org/c-api/objbuffer.html

This *IS* a shortcut, real shortcut.
--
http://mail.python.org/mailman/listinfo/python-list


assignment with [:]

2008-12-27 Thread Ben Bush
Hi,

I saw this line of code on a recent post:

a1[:] = [x*3 for x in a1]

Could somebody tells me what the [:] means? I can't find it anywhere.
See context below if needed:

On Dec 26, 4:46 pm, Tim Chase  wrote:
> > What does *not* work is
> > 3 * [0,1,2]
> > As you know, this gives
> > [0,1,2,0,1,2,0,1,2]
> > What I am hoping for is
> > [0,3,6]
> > I see that I can use
> > numpy.multiply(3,range(3))
> > but this seems overkill to me.  Can you tell I am coming to Python from
> > Matlab?
>
> The common way to do this is just
>
>   a1 = [0,1,2]
>   a2 = [x * 3 for x in a1]
>
> or, if you need a1 to be done in place:
>
>   a1[:] = [x*3 for x in a1]
>
> -tkc


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


Re: how to make a class to get all forms and input fields from webpage

2008-12-27 Thread RP
Thank you.

On Sat, Dec 27, 2008 at 8:39 PM, Gabriel Genellina
wrote:

> En Sat, 27 Dec 2008 21:54:02 -0200, alex goretoy <
> aleksandr.gore...@gmail.com> escribió:
>
>  How do I make a class for retrieving all forms and input fields on a web
>> page. Meaning, form name, form url,all input fields,
>> text,textarea,select,etc...I have something currently and it kinda
>>
>
> I'd use BeautifulSoup: http://pypi.python.org/pypi/BeautifulSoup/
>
> --
> Gabriel Genellina
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
--
http://mail.python.org/mailman/listinfo/python-list


Re: assignment with [:]

2008-12-27 Thread Robert Kern

Ben Bush wrote:

Hi,

I saw this line of code on a recent post:

a1[:] = [x*3 for x in a1]

Could somebody tells me what the [:] means? I can't find it anywhere.


It's a slice assignment. When both the start and stop arguments are omitted, it 
refers to the entire sequence. In this case, it means to replace the entire 
contents of the list a1 with the value on the right-hand-side.


"a[2:4] = ..." would replace the 3rd and 4th items in the list with the sequence 
on the right-hand-side, for instance.


--
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

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


Re: strange behavior of math.sqrt() in new 3.0 version

2008-12-27 Thread Tim Roberts
Scott David Daniels  wrote:
>
>I avoid using single-letter variables except where I know the types
>from the name (so I use i, j, k, l, m, n as integers, s as string,
>and w, x, y, and z I am a little looser with (but usually float or
>complex).

It's amazing to me that Fortran continues to live on in the hearts and
minds of today's programmers.
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
--
http://mail.python.org/mailman/listinfo/python-list


Re: math.sqrt() in new 3.0 version : solution in input()

2008-12-27 Thread Tim Roberts
David Lemper wrote:
>
>I cannot find a mention of this in "The Python Tutorial 
>release 3.1"   The I&O section discusses output formatting
>and reading & writing text files.
>John pointed out its in Guido's "What's New in Python3.0"
>Indeed its mentioned in PEP 3111, near end of What's New 
>and somewhat obscurely. " raw_input() was renamed to input().
>That is the new input() function function reads a line from
>sys.stdin . . ." 

The issue, in my view, is that input() (and raw_input()) are almost
exclusively relegated to the domain of beginners and tutorials.  Real
programs almost never use them.  As a result, this change just isn't seen
to be all that important.

If you can figure out where this could have been written so that you would
have seen it, I'm sure a documentation change could be made.
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
--
http://mail.python.org/mailman/listinfo/python-list


Re: parsing csv files class

2008-12-27 Thread Tim Roberts
"alex goretoy"  wrote:
>
>class parsercsvy(object):
>"""Return a line from a csv file or total amount of lines"""
>def __init__(self,file_name=""):
>self.func_me_color="white_on_black"
>self.soc=stdout_colours.stdout_colors()
>self.soc.me_him(['ENTER:',__name__],self.func_me_color)
>self.filename = file_name
>self.buffer = []
>self.bufferp= []
>if string.find(self.filename,"http") != -1:
>resp=urllib2.urlopen(self.filename)
>file=resp.read()
>lfi=len(string.split(self.filename,"/"))
>filename = "/tmp/"+string.split(self.filename,"/")[lfi-1]

Style issue:  unless you are running Python 1.x, you virtually never need
to import the "string" module.  Also, you can always refer to the last
element of a list or tuple by using [-1]:

parts = self.filename.split( "/" )
filename = "/tmp/" + parts[-1]


>def parse(self,filename,ret=0):
>self.soc.me_him(['ENTER:',__name__],self.func_me_color)
>i = 0
>try:
>reader = csv.reader(file(filename, "rb"))
>try:
>for row in reader:
>self.buffer.append(row)
>s,a=[],{}
>
>for j in range(len(self.buffer[0])):
>a[self.buffer[0][j]]=row[j]
>self.bufferp.append(a)
>i+=1
>self.total = i-1

You might consider keeping the header line separate.

reader = csv.reader(open(filename, "rb"))
header = reader.next()
self.buffer = list(reader)
self.bufferp = [ dict( zip( header, line ) ) for line in reader ]
self.header = header

Also, you don't really need a separate "total" variable, since it's equal
to len(self.buffer).

>def total(self):
>"""return total number of lines in csv file"""
>self.soc.me_him(['ENTER:',__name__],self.func_me_color)
>self.soc.me_him(['RETURN:',self.total,__name__],self.func_me_color)
>return self.total

There's a problem here, as this was originally written.  "self.total"
starts out being a function (this one here).  But after self.parse runs,
"self.total" will be an integer, and this function is lost.  You need to
decide whether you want users to just access the self.total integer, or
force them to use the function.  In the latter case, you can change the
counter to self._total.

On the other hand, the self.total counter is unnecessary:
def total(self):
return len(self.buffer)

>def find_and_replace(self,li,fi,re):
>"""
>find and replace a string inside a string, return list
>find_and_replace(list,find,replace)
>"""
>this=[]
>for l in li:
>#found_index=string.find(l,fi)
>this.append(l.replace(fi,re))
>return this

def find_and_replace(self,li,fi,re):
return [l.replace(fi,re) for l in li]

I'm not sure why this is a member of the class; it doesn't use any of the
members.
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
--
http://mail.python.org/mailman/listinfo/python-list


Re: math module for Decimals

2008-12-27 Thread jerry . carl . mi


> Which math functions? ln, log10, exp, sqrt already exist as methods of  
> Decimal instances. At the end of the Decimal docs there are a few  
> examples, including computing sin and cos (but apparently they naïvely use  
> a McLaurin series like you noticed in other module).

Hi Gabriel - thanks! For example all goniometric functions are
missing. Or the log(x, base). Or rand(). Sure I can spend time trying
to put it all together but I thought somebody would have done that
already. It seems though that the codes that are out there are not
ready - every one of the modules i mentioned above has some issues.
Maybe I can put bits and pieces together, but if anyone knows of a
well proven module (as is), I would feel much safer using that (again
I am not a mathematician and poking into these algorithms makes me
feel like trying to fix an automatic transmission).
--
http://mail.python.org/mailman/listinfo/python-list


Need help getting MoinMoin to run under WSGI

2008-12-27 Thread Ron Garret
I successfully installed MoinMoin as a CGI according to the instructions 
on the moinmo.in site.  But when I tried to switch over to running it 
under wsgi it failed thusly:

[Sat Dec 27 21:44:14 2008] [error] [client 66.214.189.2] Traceback (most 
recent call last):
[Sat Dec 27 21:44:14 2008] [error] [client 66.214.189.2]   File 
"/www/wikis/genesisgroup/moin.wsgi", line 49, in ?
[Sat Dec 27 21:44:14 2008] [error] [client 66.214.189.2] from 
MoinMoin.server.server_wsgi import WsgiConfig, moinmoinApp
[Sat Dec 27 21:44:14 2008] [error] [client 66.214.189.2] ImportError: No 
module named MoinMoin.server.server_wsgi

The problem, I believe, is that I have both Python 2.4 and 2.5 installed 
(it's a Debian box) and MM is installed under 2.5 but WSGI is using 2.4.  
I tried to fix this by setting WSGIPythonHome but to no avail.  I can't 
figure out what to set it to.  The instructions say:

"the WSGIPythonHome directive should be used to specify the exact 
location of the Python installation corresponding to the version of 
Python compiled against"

I have two problems with this.  First, I didn't compile mod_wsgi, I got 
it pre-built as a Debian module.  Second, what does "the exact location 
of the Python installation" even mean?  Python2.5 is spread out in at 
least three different places: /usr/local/bin, /usr/lib/python2.5, and 
/usr/local/lib/python2.5.  I've tried setting WSGIPythonHome to all of 
those (and a few other things as well) and nothing worked.

Also, "the version of Python compiled against" seems very odd.  What 
does that mean?  Surely I don't have to recompile mod_wsgi every time I 
change to a new version of Python?

Help!  Thanks!

rg
--
http://mail.python.org/mailman/listinfo/python-list


Re: math module for Decimals

2008-12-27 Thread Steven D'Aprano
On Sat, 27 Dec 2008 16:02:51 -0800, jerry.carl.mi wrote:

> Hi,
> 
> I have been looking for a Python module with math functions that would
> both eat and spit Decimals. The standard math module eats Decimals
> allright but spits floats... herefore exp(sin(Decimal())) produces exp
> () of a float :-(


You can write your own wrappers. Assuming you always want to return 
decimals instead of floats:

from decimal import Decimal
import math

def exp(x):
if isinstance(x, Decimal):
return x.exp()
f = math.exp(x)
return make_decimal(f)

def sin(x):
f = math.sin(x)
return make_decimal(f)

You can get fancier if you like:

def cos(x):
try:
return x.cos()
except AttributeError:
f = math.cos(x)
return make_decimal(f)

That version will automatically use Decimal.cos() in the future, assuming 
Decimals grow a cos() method. If they don't, it will continue to work 
(although a tad slower than the sin() above.


Converting floats to decimals is tricky, because you have to decide how 
much precision you care about. You can keep all the precision of floats. 
Depending on your needs, this is either a feature or a gotcha:

>>> (1.0/3).as_integer_ratio()  # expecting (1, 3)
(6004799503160661L, 18014398509481984L)


Anyway, here are at least three ways to write make_decimal:


# Python 2.6 only
def make_decimal(f):  # exact, no loss of precision
# however, will fail for f = NAN or INF
a, b = f.as_integer_ratio()
return Decimal(a)/Decimal(b)

def make_decimal(f):
return Decimal("%r" % f)

def make_decimal(f, precision=16):
# choose how many decimal places you want to keep
return Decimal.from_float(f, precision)


You choose which suits you best.



> So far, i found:
> 
> -AJDecimalMathAdditions (http://www.ajgs.com/programming/
> PythonForDownload.html)
> -decimalfuncs (http://pypi.python.org/pypi/decimalfuncs/1.4) -and dmath
> (http://pypi.python.org/pypi/dmath/0.9)
> 
> I tried using the AJDecimalMathAdditions, but ran into issues like dSin
> (1E4) would take forever to calculate and would result in sin() > 1 ...
> If i understand it correctly, the program is using maclaurin series to
> calculate the value and since it does not chop off all the multiples of
> 2*pi, the maclaurin approximation becomes useless when its too far from
> x=0.


You might try something like this:

pi = Decimal("%r" % math.pi)
twopi = 2*pi
pi2 = pi/2


def sin(x):
#  *** UNTESTED ***
if x < 0:
return -sin(-x)
elif x > twopi:
x = remainder(x, twopi)
return sin(x)
elif x > pi:
return -sin(x - pi)
elif x > pi2:
return sin(pi - x)
else:
return AJDecimalMathAdditions.dSin(x)


You can treat cos and tan similarly but not identically.

However, you might find that it quicker and simpler to just do your maths 
calculations as floats, then convert to decimals, as in my earlier 
examples.



> I also ran into some issues with the decimalfuncs, but i have never
> tried the dmath thing.
> 
> Now, i can go and start modifying these modules to behave more like the
> standard math module but since i am neither mathematician or programer,
> i really hesitate.

And so you should. Getting numerical functions correct is damn hard.

However, if all you need is "close enough", then don't be scared -- the 
sort of tricks I've shown are probably "close enough", although you may 
want a disclaimer about not using them for running nuclear reactors or 
flying aircraft. The basic tools Python supplies are pretty good, and you 
can probably get a 95% solution with them without being an expert in 
computational mathematics.

 
> So my questions are:
> 
> (1) what do folks use when they need to calculate something like exp
> (sin(Decimal())) or even more complex things? Any recommendations? Or am
> I completely missing something?
> 
> (2) Is there any plan to provide a standard python module that would do
> that? (Python 4.0? ;-)


Guido is very reluctant to put numeric libraries in the standard library, 
precisely because it is so hard to get it right. Don't expect Python to 
become Mathematica any time soon!



-- 
Steven
--
http://mail.python.org/mailman/listinfo/python-list


python and rcp, portbind communication

2008-12-27 Thread Marco
Hello python list,

i want to write a program in python which can communicate with
rpcservices. At the first stage it shall talk with
portbind on port 111 and figure out the running rpc services. Is there
already a implementation for python to do so?

Best regards,
 marco
--
http://mail.python.org/mailman/listinfo/python-list


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Gabriel Genellina
En Sun, 28 Dec 2008 01:47:08 -0200, Daniel Fetchinson  
 escribió:


As others already said, using a Numpy array or an array.array object  
would
be more efficient (and even easier - the C code gets a pointer to an  
array

of integers, as usual).


I looked for this in the C API docs but couldn't find anything on how
to make an array.array python object appear as a pointer to integers
(or floats, etc) in C code. On

http://docs.python.org/c-api/concrete.html#sequence-objects

There is only list and tuple or maybe you mean byte array? That has
only been introduced in python 2.6 and I'm working on 2.5.


array is a library module, and isn't really part of the API. You're  
looking for the buffer protocol:  
PyObject_AsReadBuffer/PyObject_AsWriteBuffer; see  
http://docs.python.org/c-api/objbuffer.html


Given an array.array('l') (containing C long integers):

int do_something(PyObject* obj)
{
long *vec;
Py_ssize_t nbytes, nitems, i;

if (PyObject_AsReadBuffer(obj, (const void **)&vec, &nbytes) != 0)
return NULL;
nitems = nbytes/sizeof(long);
for (i=0; iFrom Python you can get "vec" and "nitems" using the buffer_info() method  
of array objects.


--
Gabriel Genellina

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


Re: math module for Decimals

2008-12-27 Thread Steven D'Aprano
On Sun, 28 Dec 2008 06:58:18 +, Steven D'Aprano wrote:

> def make_decimal(f, precision=16):
> # choose how many decimal places you want to keep return
> Decimal.from_float(f, precision)

Ah crap, I forgot that from_float() has been left out of the decimal API. 
That's very annoying.

Sorry about that.


-- 
Steven
--
http://mail.python.org/mailman/listinfo/python-list


Re: C API: array of floats/ints from python to C and back

2008-12-27 Thread Daniel Fetchinson
>>> As others already said, using a Numpy array or an array.array object
>>> would
>>> be more efficient (and even easier - the C code gets a pointer to an
>>> array
>>> of integers, as usual).
>>
>> I looked for this in the C API docs but couldn't find anything on how
>> to make an array.array python object appear as a pointer to integers
>> (or floats, etc) in C code. On
>>
>> http://docs.python.org/c-api/concrete.html#sequence-objects
>>
>> There is only list and tuple or maybe you mean byte array? That has
>> only been introduced in python 2.6 and I'm working on 2.5.
>
> array is a library module, and isn't really part of the API. You're
> looking for the buffer protocol:
> PyObject_AsReadBuffer/PyObject_AsWriteBuffer; see
> http://docs.python.org/c-api/objbuffer.html
>
> Given an array.array('l') (containing C long integers):
>
> int do_something(PyObject* obj)
> {
>  long *vec;
>  Py_ssize_t nbytes, nitems, i;
>
>  if (PyObject_AsReadBuffer(obj, (const void **)&vec, &nbytes) != 0)
>  return NULL;
>  nitems = nbytes/sizeof(long);
>  for (i=0; i/* do something with vec[i] */
>  }
>  return ret;
> }
>
>  From Python you can get "vec" and "nitems" using the buffer_info() method
> of array objects.

Thanks very much, this was very helpful!

Cheers,
Daniel

-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list