Re: word shifts

2008-05-04 Thread Peter Otten
dave wrote:

> I made a function that takes a word list (one word per line, text file) 
> and searches for all the words in the list that are 'shifts' of 
> eachother.  'abc' shifted 1 is 'bcd'
> 
> Please take a look and tell me if this is a viable solution.
> 
>  def shift(word, amt):
> ans = ''
> for letter in word:
> ans = ans + chr((ord(letter) - ord('a') + amt) % 26 +
ord('a'))
> return ans
> 
> def fileshift(x):
> fin = open(x)
> d = {}
> for line in fin:
> d[line.strip()] = [1]
> for i in range(1, 26):
> ite = shift(line.strip(), i)
> if ite in d:
> print ite
> 
> 
> Any tips/suggestions/critiques greatly appreciated.. I'm trying to 
> teach myself Python (and still beginning) and would love any helpful 
> info.

My ideas: 

Move the open() call out of fileshift() and have it working with arbitrary
iterables. 
Store a normalized version of the word in the dictionary. if every word in
the dict is guaranteed to start with an "a" you can reduce the number of
tests to one.
If you have many words you can build a fast shift() function based on
str.translate().

Invoke the following script both with and without a filename argument:

from string import ascii_lowercase as chars, maketrans

tables = [maketrans(chars, chars[i:] + chars[:i]) for i in range(26)]

def shift(word, n):
return word.translate(tables[n%26])

def normalize(word):
a = word[0]
return shift(word, ord("a") - ord(a))

def findshifted(words):
d = {}
for word in words:
d.setdefault(normalize(word), []).append(word)
return d


if __name__ == "__main__":
import sys
args = sys.argv[1:]
if args:
words = (line.strip() for line in open(args[0]))
else:
import random
sample = """\
alpha
beta
gamma
delta
""".split()

words = sample + [shift(random.choice(sample), random.randrange(26))
for _ in range(10)]

items = findshifted(words).items()
items.sort()
for k, v in items:
print k, "-->", v

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

Re: word shifts

2008-05-04 Thread Arnaud Delobelle
dave <[EMAIL PROTECTED]> writes:

> Hello,
>
> I made a function that takes a word list (one word per line, text
> file) and searches for all the words in the list that are 'shifts' of
> eachother.  'abc' shifted 1 is 'bcd'
>
> Please take a look and tell me if this is a viable solution.
>
> def shift(word, amt):
>   ans = ''
>   for letter in word:
>   ans = ans + chr((ord(letter) - ord('a') + amt) % 26 + ord('a'))
>   return ans

In Python, if you want to build a string from lots of parts you can
use ''.join(parts).  I think it is considered more efficient.


>
> def fileshift(x):
>   fin = open(x)
>   d = {}
>   for line in fin:
>   d[line.strip()] = [1]
>   for i in range(1, 26):
>   ite = shift(line.strip(), i)
>   if ite in d:
>   print ite
>


You could write:

   line = line.strip()

after your for line in fin: statement, rather than strip the line twice.

Let me suggest a different solution base on the str.translate() method

from string import lowercase, maketrans

shifted_lowercase = lowercase[1:] +lowercase[0]
table = string.maketrans(lowercase, shifted_lowercase)

def shift1(line):
return line.translate(table)

def fileshift(path):
lines = set()
for line in open(path):
lines.add(line)
for i in xrange(25):
line = shift1(line)
if line in lines:
print line

(untested)

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


Re: is +=1 thread safe

2008-05-04 Thread Gary Herron

Carl Banks wrote:

On May 3, 7:44 pm, Gary Herron <[EMAIL PROTECTED]> wrote:
  

Alexander Schmolck wrote:


AlFire <[EMAIL PROTECTED]> writes:
  

The threading module already has a function to return the number of Thread
objects currently alive.
  

I have threads within threads - so it does not suit me :-(.


How about using a scalar numpy array? They are mutable, so I assume that x +=
1 should be atomic.
  

No NO NO! The only way to increment a variable in memory is through a
three step process:



Yes, this will be atomic.  It's a pretty good idea, in fact.  The
underlying increment operation is protected by the GIL; it could be
three, forty, or a hundred steps and it'd be atomic at the Python
level.

  


But... It's not!  

A simple test shows that.   I've attached a tiny test program that shows 
this extremely clearly.  Please run it and watch it fail.  In this test, 
two threads both increment a shared global variable 1 million times.  
This should end with a value of 2 million, but it rarely does so.   
(Actually it has *never* done so yet.)


The GIL makes sure no two Python threads are running at the same time,  
and from that you can conclude that Python virtual machine instructions 
are atomic, but you cannot conclude that Python statements/expression 
are atomic.  Any Python statement or expression that compiles into more 
than one OP code can be interrupted between those OP codes, and this can 
cause devastating results to the interrupted statement.


Gary Herron



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




t.py
Description: application/python
--
http://mail.python.org/mailman/listinfo/python-list

Re: Script Optimization

2008-05-04 Thread David
>  It's too long to post here (160 lines) so here's the link:
>  http://uppit.com/d2/CKOYHE/af78a6bd3e21a19d5871abb9b879/utils.py
>  (if that doesn't work: http://uppit.com/CKOYHE)
>
>  Thanks in advance,
>  lev

Neither link works for me. I get an error page "Error: invalid
download linnk". How about you send it to the list as an attachment?

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


Re: generator functions in another language

2008-05-04 Thread David
On Sun, May 4, 2008 at 1:39 AM,  <[EMAIL PROTECTED]> wrote:
> I'm actually curious if there's a way to write a generator function
>  (not a generator expression) in C, or what the simplest way to do it
>  is... besides link the Python run-time.
>  --

Here's the itertools C code:

http://svn.python.org/view/python/trunk/Modules/itertoolsmodule.c?rev=61634&view=markup

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


Re: Please help - Tkinter not doing anything

2008-05-04 Thread Protected
Good thinking. It was indented with spaces, so I replaced them with tabs.
Now I'm getting a SyntaxError: invalid syntax in root = Tk(). If I split the
code in two parts (with the second one beginning in that line) and run them
separately, I get no errors, but still nothing happens.

class Application(Frame):
def say_hi(self):
print "hi there, everyone!"

def createWidgets(self):
self.QUIT = Button(self)
self.QUIT["text"] = "QUIT"
self.QUIT["fg"]   = "red"
self.QUIT["command"] =  self.quit

self.QUIT.pack({"side": "left"})

self.hi_there = Button(self)
self.hi_there["text"] = "Hello",
self.hi_there["command"] = self.say_hi

self.hi_there.pack({"side": "left"})

def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()

var root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()

On Sun, May 4, 2008 at 12:33 PM, Q4 <[EMAIL PROTECTED]> wrote:

> On Sun, 4 May 2008 02:23:37 -0700 (PDT), Protected <[EMAIL PROTECTED]>
> wrote:
> > Hello. I'm a complete newbie trying to learn Python. I decided to try
> > some Tkinter examples, including the one from the library reference,
> > but they don't seem to do anything! Shouldn't there be, like, a
> > dialog?
> >
> > I'm running Windows XP and using IDLE. You can assume my version of
> > Python is the latest.
> > --
> > http://mail.python.org/mailman/listinfo/python-list
>
> If you just copy the code from the python doc the indentation might be
> broken.
> Send the code and I'll take a look at it.
>
> Do you get any errors?
>
> --
> My website:
>  http://www.riddergarn.dk/koder/
>
>
--
http://mail.python.org/mailman/listinfo/python-list

Fw: Script Optimization

2008-05-04 Thread David
Forwarding back to the list.

-- Forwarded message --
From: lev <[EMAIL PROTECTED]>
Date: Sun, May 4, 2008 at 10:21 AM
Subject: Re: Script Optimization
To: David <[EMAIL PROTECTED]>


On May 4, 12:32 am, David <[EMAIL PROTECTED]> wrote:
 > >  It's too long to post here (160 lines) so here's the link:
 > >  http://uppit.com/d2/CKOYHE/af78a6bd3e21a19d5871abb9b879/utils.py
 > >  (if that doesn't work:http://uppit.com/CKOYHE)
 >
 > >  Thanks in advance,
 > >  lev
 >

> Neither link works for me. I get an error page "Error: invalid
 > download linnk". How about you send it to the list as an attachment?
 >
 > David.

 Sorry.. but I don't know how to send an attachment here... I am not
 using a dedicated email client, just google groups, and the option
 seems to elude me.
 I forgot to comment it when I was writing it I am working on
 commenting it and making it more readable, and I'll post that tomorrow
 (I'm going to sleep... it's 2 in the morning)
 This is a bit long, sorry:

 #!/usr/bin/env python

 def main():
from sys import argv
from optparse import OptionParser
from os import chdir,path
fullpath = path.abspath(path.dirname(argv[0]))
chdir(fullpath[:-10])
checksums = '%s//checksums.md5' % fullpath
newdirname = ('string1', 'string2', 'string3', 'string4')
usage = "%prog [options]"
parser = OptionParser(usage)
parser.add_option('-c', '--check', action='store_true', dest='check',
 help = 'verify checksums')
parser.add_option('-r', '--rename', action='store_true',
 dest='rename', help = 'rename files to a more usable form (write
 rights needed)')
(options, args) = parser.parse_args()
if options.check:
check(checksums)
chdir(fullpath)
pass
if options.rename:
chdir('..')
rename(newdirname, checksums)
pass
if not options.check and not options.rename:
parser.print_help()
pass
return

 def check(checksums):
checksums = open(checksums, 'r')
chgfiles = {}
msngfiles = []
for fline in checksums.readlines():
line = fline.split(' *')
orig = line[0].upper()
try:
file = open(line[1].strip(),'rb')
import md5
chunk = 8196
sum = md5.new()
while(1):
chunkdata = file.read(chunk)
if not chunkdata:
break
sum.update(chunkdata)
pass
new = sum.hexdigest().upper()
file.close()
if  new == orig:
print '.',
pass
else:
chgfiles[line[1]] = (orig,new)
pass
except IOError:
msngfiles.append(line[1])
pass
pass
checksums.close()
chgfileskeys = chgfiles.keys()
chgfileskeys.sort()
msngfiles.sort()
print '\n'
if len(chgfiles) != 0:
print 'Files changed:'
for key in chgfileskeys:
print key.strip('\n'),'changed
from:\n\t',chgfiles[key][0],'to\n
 \t',chgfiles[key][1]
pass
print '\n\t',len(chgfiles),'file(s) changed.\n'
pass
if len(msngfiles) != 0:
print 'Files not found:'
for x in range(len(msngfiles)):
print '\t',msngfiles[x]
pass
print '\n\t', len(msngfiles),'file(s) not found.\n'
pass
print "\n\tChecksums Verified\n"
return

 def rename(newdirname, checksums):
from os import chdir, rename
from glob import glob
dict = md5format(checksums)
dirlist = glob('./00[1-4]string [1-4]')
dirlist.sort()
for x in range(4):
rename(dirlist[x],newdirname[x])
print '\t',dirlist[x],'renamed to:',newdirname[x]
chdir('./%s' % newdirname[x])
for oldfilename in glob('*.mp3'):
newfilename = oldfilename[3:]
rename(oldfilename,newfilename)
print '\t\t',oldfilename,'renamed to:',newfilename
pass
dict = md5fname_edit(dict,dirlist[x],newdirname[x])
pass
chdir('..')
md5write(dict, checksums)
replace('Webpage.htm', dirlist, newdirname)
print '\n\tDirectories and Files renamed.'

Please help - Tkinter not doing anything

2008-05-04 Thread Protected
Hello. I'm a complete newbie trying to learn Python. I decided to try
some Tkinter examples, including the one from the library reference,
but they don't seem to do anything! Shouldn't there be, like, a
dialog?

I'm running Windows XP and using IDLE. You can assume my version of
Python is the latest.
--
http://mail.python.org/mailman/listinfo/python-list


Re: using sqlite3 - execute vs. executemany; committing ...

2008-05-04 Thread David
> - Are there any peculiarities with using curs.executemany(...) vs. multiple
> curs.execute(...) ? I read a notice, sqlite3 does internally some caching,
> hence both should be similarly fast, but in my case executemany(...) is
> quite a bit faster

How many times are you calling execute vs a single executemany? The
python call overhead will add up for thousands of calls.

The relevant source code is here if you're interested:

http://svn.python.org/projects/python/trunk/Modules/_sqlite/cursor.c

>  Further, I am not quite sure about the standard usage of the cursor object
> and also the proper commiting the transactions and closing the connection.

Standard usage is here:

http://docs.python.org/lib/module-sqlite3.html

If the database supports transactions then cursors automatically use
transactions. Your changes only get committed when you call .commit().
Otherwise your changes are lost.

In the specific case of sqllite, some statements (like CREATE TABLE,
ALTER TABLE, etc) also cause a commit. This is probably where your
confusion comes from. Since this isn't part of the python DB API spec
(http://www.python.org/dev/peps/pep-0249/) I wouldn't rely on it.
Otherwise you will have problems with other databases.

Also, in your specific case you're using an 'in memory' sqllite db. So
there are less concerns with losing data between db sessions, etc. But
with most databases (on disk, running across the network on a server)
this becomes important.

>  Should one create a cursor of a connection and call the execute ... methods
> of the cursor -
>  or is it better to call the shortcut execute etc. methods of the Connection
> object directly (as suggested in the docs:
> http://docs.python.org/lib/node351.html (or are there specific use cases for
> both approaches)?

I suggest that you use the standard cursor methods instead, so you can
run your code against non-sqllite databases.  The performance etc
should be the same as using the direct method. Like the page says,
it's main benefit is consiseness.

>
> When the transactions should be commited? (creating, altering a table, or
> also selecting the results ?)
>  There seem to be some implicit handling of the transactions (
> http://docs.python.org/lib/sqlite3-Controlling-Transactions.html#sqlite3-Controlling-Transactions
> ); hence I am not sure about the standard usage of these methods; the same
> is true of connection.close() - or are these calls eventually unnecessary?

As a general rule, always use .commit() and .close(). Otherwise:

- No .commit() - you will lose db changes since the last commit or
"non-DML, non-query statement" (in the case of sqllite)
- No .close() - Your database connection will only close when your db
objects are garbage collected.

> conn_tags_DB = sqlite3.connect(':memory:')
>  curs = self.conn_tags_DB.cursor()
>  curs.execute('CREATE TABLE IF NOT EXISTS "%s" ("%s", UNIQUE("%s"))' %
> (self.text_name, index_col_name, index_col_name))
>  curs.execute(u'INSERT OR REPLACE INTO "%s"("%s") VALUES (?)' %
> (self.text_name, index_col_name), (0,))
>  for new_col in act_db_columns[1:]: # adds the needed columns (except of the
> first one: index_col_name)
>  curs.execute('ALTER TABLE "%s" ADD "%s" TEXT' % (self.text_name,
> new_col))
>  curs.executemany('INSERT OR REPLACE INTO "%s" VALUES (%s)' %
> (self.text_name, question_marks), tags_seq)
>  self.conn_tags_DB.commit()
>
> Are there maybe any comments or hints on a more elegant/efficient solution?
>

I think that dynamically creating schema (tables, based on text file
structure is a bad idea. A few reasons:

- This forces you to dynamically generate all your queries dynamically
- Not all strings are valid table/column names
- This forces the app to run as database administrator (maybe not
important for sqllite, but definitely an issue if you change to
another dbm).
- Potentially huge stability/security problems - text files can
potentially break system tables, overwrite users, etc, etc.

You're violating several rules on db design/usage.

I strongly recommend that you use a better database logic. ie, create
tables and records in advance (setup script, as db admin user if
applicable), then only use delete/insert/update/select statements (as
restricted user, if applicable).

If this is too much trouble, then I suggest storing your database in
regular Python structures instead, and use pickle/yaml/etc to write to
disk. Your current version uses a 'in memory' database, so the end
result is the same. You'll get a large performance boost also.

> Now, what's the usual way to access the database? Is it
> possible/wise/standard ... to leave the connection open for the subsequent
> queries during the whole run of the app; could even the cursor eventually be
> present as a class method, or should it rather be created repeatedly with
> each call?  (After populating, the db shouldn't be modified, but only read.)

It depends. If your app is simple, single threaded, then a single
connection (global or 

Re: Encoding Text

2008-05-04 Thread Tim Golden

Paul Jefferson wrote:
I'm learning this and I'm making a program which takes RSS feeds and 
processes them and then outputs them to a HTML file.
The problem I have is that some of the RSS feeds contain chachters which 
I think are outside of the ascii range as when I attempt to write the 
file containing themI get the follwoign error:


File "C:\Users\Paul\Desktop\python\Feed reader\test.py", line 201, in 


runner(1,"save.ahc")
  File "C:\Users\Paul\Desktop\python\Feed reader\test.py", line 185, in 
runner

h.write(html[countera])
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in 
position 1147: ordinal not in range(128)


For the life of me I can't figure out what I should do to stop this - I 
tried to change the html by doing a html[countera] = 
unicode(html[countera]) but it didn't seem to chaneg anything.


I assume from the traceback that html[countera] contains a unicode
object and that h is a regular file open for writing. If that's the
case then you need to do one of two things to have the string written
correctly: either open/wrap the file via the codecs module to specify
the encoding through which unicode objects should be written; or
explicitly encode the unicode objects as they're written.

If you don't do one of those two things then Python will try to
convert your unicode object naively so that it can be written to
file, and it will use the ascii codec which can't handle anything
outside the basic stuff. You've got a right-quote mark in your
unicode, probably from a Windows program.

Since the latter is easier to demonstrate from the code above:


h.write (html[countera].encode ("utf8"))


but a more general-purpose solution is probably:


import codecs

h = codecs.open ("save.ahc", "w", encoding="utf8")
.
.
h.write (html[countera])


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


Re: Open a file within an ISO in python.

2008-05-04 Thread David
On Sun, May 4, 2008 at 12:01 PM,  <[EMAIL PROTECTED]> wrote:
> Is there a way to open a file that is inside of an ISO in python?
>
>  Say I have an ISO file, danikars.iso and on the iso there is a file
>  called information.txt
>
>  I want to be able to do something like this.
>
>  [code]
>  iso_h = openiso("danikars.iso")
>  file_h = iso_h.extract("information.txt")
>  contents = file_h.read()
>  [/code]
>
>  Is there anything that will give me that functionality? Or close?
>

Under Linux you can mount iso files as loopback devices.

eg:

mkdir mnt
sudo mount -o loop file.iso ./mnt

Then the ./mnt directory will 'contain' the contents of the iso
--
http://mail.python.org/mailman/listinfo/python-list


Open a file within an ISO in python.

2008-05-04 Thread danikar
Is there a way to open a file that is inside of an ISO in python?

Say I have an ISO file, danikars.iso and on the iso there is a file
called information.txt

I want to be able to do something like this.

[code]
iso_h = openiso("danikars.iso")
file_h = iso_h.extract("information.txt")
contents = file_h.read()
[/code]

Is there anything that will give me that functionality? Or close?

Thanks,

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


Re: Please help - Tkinter not doing anything

2008-05-04 Thread Chuckk Hubbard
Try adding:

from Tkinter import *

at the beginning, and you don't need "var" in front of root=Tk(), just
"root = Tk()"   (<-without the quotes of course)
What OS are you on?  Are you running "python testapp.py" or similar to
make it run?

-Chuckk

On Sun, May 4, 2008 at 12:39 PM, Protected <[EMAIL PROTECTED]> wrote:
> Good thinking. It was indented with spaces, so I replaced them with tabs.
> Now I'm getting a SyntaxError: invalid syntax in root = Tk(). If I split the
> code in two parts (with the second one beginning in that line) and run them
> separately, I get no errors, but still nothing happens.
>
> class Application(Frame):
> def say_hi(self):
> print "hi there, everyone!"
>
> def createWidgets(self):
> self.QUIT = Button(self)
> self.QUIT["text"] = "QUIT"
>  self.QUIT["fg"]   = "red"
> self.QUIT["command"] =  self.quit
>
> self.QUIT.pack({"side": "left"})
>
> self.hi_there = Button(self)
>  self.hi_there["text"] = "Hello",
> self.hi_there["command"] = self.say_hi
>
> self.hi_there.pack({"side": "left"})
>
> def __init__(self, master=None):
>  Frame.__init__(self, master)
> self.pack()
> self.createWidgets()
>
> var root = Tk()
> app = Application(master=root)
> app.mainloop()
> root.destroy()
>
> On Sun, May 4, 2008 at 12:33 PM, Q4 <[EMAIL PROTECTED]> wrote:
>
> > On Sun, 4 May 2008 02:23:37 -0700 (PDT), Protected <[EMAIL PROTECTED]>
> > wrote:
> >
> >
> >
> > > Hello. I'm a complete newbie trying to learn Python. I decided to try
> > > some Tkinter examples, including the one from the library reference,
> > > but they don't seem to do anything! Shouldn't there be, like, a
> > > dialog?
> > >
> > > I'm running Windows XP and using IDLE. You can assume my version of
> > > Python is the latest.
> > > --
> > > http://mail.python.org/mailman/listinfo/python-list
> >
> > If you just copy the code from the python doc the indentation might be
> > broken.
> > Send the code and I'll take a look at it.
> >
> > Do you get any errors?
> >
> > --
> > My website:
> >  http://www.riddergarn.dk/koder/
> >
> >
>
>
> --
>  http://mail.python.org/mailman/listinfo/python-list
>



-- 
http://www.badmuthahubbard.com
--
http://mail.python.org/mailman/listinfo/python-list


ANN: eric 4.1.3 released

2008-05-04 Thread Detlev Offenbach
Hi,

I'd like to inform everybody about the immediate availability of eric
v4.1.3. This is a bug fix release.

It is available via http://www.die-offenbachs.de/eric/index.html.

What is eric?
-
eric is a Python IDE written using PyQt4 and QScintilla2. It comes with
all batteries included. For details please see the above link.

Regards,
Detlev
-- 
Detlev Offenbach
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Light slices + COW

2008-05-04 Thread bearophileHUGS
David:

> What do you mean by best possible? Most efficient? Most readable?

What's a good wine? It's not easy to define what's "good/best". In
such context it's a complex balance of correct, short, fast and
readable (and more, because you need to define a context. This context
refers to Psyco too).


> And why don't you use islice?

You are right, but the purpose of light slices that I have suggested
is to avoid using islice so often. And currently Psyco doesn't digest
itertools well.


> D compiles to efficient machine code so Python is at a disadvantage
> even if you use the same syntax (see my first example). You can make
> the Python version faster, but beware of premature optimization.

This time I don't agree with this "premature optimization" thing. My
original Python version is just 5 lines long, it's readable enough,
and it's a part of a large library of mine of similar functions, they
must be fast because I use them all the time as building blocks in
programs.


> What I'dlike to see is a rope[1] module for Python.

People have already suggested it, and there's even an implementation
to replace Python strings. It was refused because... I don't know why,
maybe its implementation was too much more complex than the current
one, and it wasn't faster in all situations (and I think Guido wants
data structures that try to replace the basic built-in ones to be
faster in all situations and user-transparent too).

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: Please help - Tkinter not doing anything

2008-05-04 Thread Protected
I had previously ran the import line. I prepended it to the example
code I'm trying to run every time but it did not help, still nothing
happens. With or without var before 'root'. I'm pasting the code in
IDLE and using Windows XP as written in the first post.

On May 4, 11:04 am, "Chuckk Hubbard" <[EMAIL PROTECTED]>
wrote:
> Try adding:
>
> from Tkinter import *
>
> at the beginning, and you don't need "var" in front of root=Tk(), just
> "root = Tk()"   (<-without the quotes of course)
> What OS are you on?  Are you running "python testapp.py" or similar to
> make it run?
>
> -Chuckk
>
>
>
> On Sun, May 4, 2008 at 12:39 PM, Protected <[EMAIL PROTECTED]> wrote:
> > Good thinking. It was indented with spaces, so I replaced them with tabs.
> > Now I'm getting a SyntaxError: invalid syntax in root = Tk(). If I split the
> > code in two parts (with the second one beginning in that line) and run them
> > separately, I get no errors, but still nothing happens.
>
> > class Application(Frame):
> > def say_hi(self):
> > print "hi there, everyone!"
>
> > def createWidgets(self):
> > self.QUIT = Button(self)
> > self.QUIT["text"] = "QUIT"
> >  self.QUIT["fg"]   = "red"
> > self.QUIT["command"] =  self.quit
>
> > self.QUIT.pack({"side": "left"})
>
> > self.hi_there = Button(self)
> >  self.hi_there["text"] = "Hello",
> > self.hi_there["command"] = self.say_hi
>
> > self.hi_there.pack({"side": "left"})
>
> > def __init__(self, master=None):
> >  Frame.__init__(self, master)
> > self.pack()
> > self.createWidgets()
>
> > var root = Tk()
> > app = Application(master=root)
> > app.mainloop()
> > root.destroy()
>
> > On Sun, May 4, 2008 at 12:33 PM, Q4 <[EMAIL PROTECTED]> wrote:
>
> > > On Sun, 4 May 2008 02:23:37 -0700 (PDT), Protected <[EMAIL PROTECTED]>
> > > wrote:
>
> > > > Hello. I'm a complete newbie trying to learn Python. I decided to try
> > > > some Tkinter examples, including the one from the library reference,
> > > > but they don't seem to do anything! Shouldn't there be, like, a
> > > > dialog?
>
> > > > I'm running Windows XP and using IDLE. You can assume my version of
> > > > Python is the latest.
> > > > --
> > > >http://mail.python.org/mailman/listinfo/python-list
>
> > > If you just copy the code from the python doc the indentation might be
> > > broken.
> > > Send the code and I'll take a look at it.
>
> > > Do you get any errors?
>
> > > --
> > > My website:
> > >  http://www.riddergarn.dk/koder/
>
> > --
> >  http://mail.python.org/mailman/listinfo/python-list
>
> --http://www.badmuthahubbard.com

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


Re: is +=1 thread safe

2008-05-04 Thread Bjoern Schliessmann
Gary Herron wrote:

> No NO NO!   The only way to increment a variable in memory is
> through a three step process:
> 
>   Load a register from a memory location
>   Increment the register
>   Store the value back into memory.

I suggest you read "Intel 64 and IA-32 Architectures Software
Developer's Manual" (2A, Instruction Set Reference A-M). INC and
DEC can have register *or* memory location as operand, and can be
executed atomically even in multiprocessor environments (LOCK
prefix).
 
> Ages ago, there were architectures that would do an increment on a
> memory location in an atomic way, but with the current (RISC)
> architectures these are three separate operations.

That may be true for RISC architectures. Unfortunately, all 8086
based CPUs are CISC.

Regards,


Björn

-- 
BOFH excuse #277:

Your Flux Capacitor has gone bad.

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


Re: word shifts

2008-05-04 Thread bearophileHUGS
George Sakkis:
> A faster algorithm is to create a 'key' for each word, defined as the
> tuple of ord differences (modulo 26) of consecutive characters.

Very nice solution, it uses the same strategy used to find anagrams,
where keys are
"".join(sorted(word))
Such general strategy to look for a possible invariant key for the
subsets of the required solutions is quite useful.

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: about bsddb module

2008-05-04 Thread Jorge Godoy
cocobear wrote:

> On 5月3日, 下午7时17分, cocobear <[EMAIL PROTECTED]> wrote:
>> How to deal with multiple databases in an file. I want to get the
>> content of several databases.

(...)
 
> Anybody can help me?

I believe you can only have one database per file with the Python
abstraction...  But you can try this (straight from Google for "sleepycat
multiple databases" and sleepycat came from the docs for bsddb):

http://www.nextgen6.net/docs/db4.1.25/api_java/db_open.html

and pay attention to the details here:

http://forums.oracle.com/forums/thread.jspa?threadID=581528&tstart=15



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

Re: generator functions in another language

2008-05-04 Thread castironpi
On May 4, 12:21 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Sun, 04 May 2008 01:08:34 -0300, Marc 'BlackJack' Rintsch <[EMAIL 
> PROTECTED]> escribió:
>
> > On Sat, 03 May 2008 16:39:43 -0700, castironpi wrote:
>
> >> I'm actually curious if there's a way to write a generator function
> >> (not a generator expression) in C, or what the simplest way to do it
> >> is... besides link the Python run-time.
>
> > The reference implementation of Python is written in C, so obviously there
> > must be a way to write something like generators in C.
>
> Yes and no. Generators are tied to frames, and frames execute Python code, 
> not C. There is no simple way to write generators in C, but there are some 
> generator-like examples in the itertools module.
> See this 
> threadhttp://groups.google.com/group/comp.lang.python/browse_thread/thread/...
>
> --
> Gabriel Genellina

Gabriel,
How did your attempt turn out from last May?  At first look, it's
outside the scope of Python, but it is not the scope of C
necessarily.  Generators offer a lot of simplicity (which I haven't
read about extensively, but am starting to see) that could gain some
reputation for Python.  What is the midpoint at which C could meet
Python?

There is no such thing as a 'frame' per se in C; byte code is
integral.  As there is no such thing as suspended state without
frames, and no such thing as generators without suspended state.  It's
a hard thing to Google for without knowing the prior terminology for
the work that's already been done on them in C.  What work is there?
Are any devs interested in pursuing it?

The frame implementation.

http://svn.python.org/projects/python/trunk/Include/frameobject.h
http://svn.python.org/projects/python/trunk/Objects/frameobject.c

The generator code.

http://svn.python.org/projects/python/trunk/Include/genobject.h
http://svn.python.org/projects/python/trunk/Objects/genobject.c

I used Microsoft's search engine (python frame generator
site:svn.python.org , links 3 and 5) to find it.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Please help - Tkinter not doing anything

2008-05-04 Thread s0suk3
On May 4, 5:22 am, Protected <[EMAIL PROTECTED]> wrote:
> I had previously ran the import line. I prepended it to the example
> code I'm trying to run every time but it did not help, still nothing
> happens. With or without var before 'root'. I'm pasting the code in
> IDLE and using Windows XP as written in the first post.
>

Tkinter doesn't work if you type the statements in IDLE. I don't
remember the specifics of it, but essentially it doesn't work because
IDLE is itself a Tkinter app. You have to type it at the Python
command line or save it in a file. BTW, if you're on Windows, you
should definitely check wxPython. I used to work with Tkinter,
struggling with it all the time only to get a lame result most of the
time. Then I switched to wxPython, and in the same week I was learning
it I did a better GUI than I ever did in Tkinter (with months of
work!). I feel it makes it easier to make your program have a better
structure and design, and thus lets you focus on the actual task of
the program, rather than in the GUI itself. Plus, on Windows, you'll
get the widgets to look more natively, like any good quality
application there is on Windows.
--
http://mail.python.org/mailman/listinfo/python-list


Re: saving a webpage's links to the hard disk

2008-05-04 Thread castironpi
On May 4, 12:33 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Sun, 04 May 2008 01:33:45 -0300, Jetus <[EMAIL PROTECTED]> escribió:
>
> > Is there a good place to look to see where I can find some code that
> > will help me to save webpage's links to the local drive, after I have
> > used urllib2 to retrieve the page?
> > Many times I have to view these pages when I do not have access to the
> > internet.
>
> Don't reinvent the wheel and use wgethttp://en.wikipedia.org/wiki/Wget
>
> --
> Gabriel Genellina

A lot of the functionality is already present.

import urllib
urllib.urlretrieve( 'http://python.org/', 'main.htm' )
from htmllib import HTMLParser
from formatter import NullFormatter
parser= HTMLParser( NullFormatter( ) )
parser.feed( open( 'main.htm' ).read( ) )
import urlparse
for a in parser.anchorlist:
print urlparse.urljoin( 'http://python.org/', a )

Output snipped:

...
http://python.org/psf/
http://python.org/dev/
http://python.org/links/
http://python.org/download/releases/2.5.2
http://docs.python.org/
http://python.org/ftp/python/2.5.2/python-2.5.2.msi
...
--
http://mail.python.org/mailman/listinfo/python-list


ISBN Barecode reader in Python?

2008-05-04 Thread Joseph
All: I have written a program to query Amazon with ISBN and get the
book details. I would like to extend so that I can read ISBN from the
barcode (I will take a photo of the same using webcam or mobile). Are
there any opensource/free SDK doing the same? As it is a hobby
project, I don't like to spend money on the SDK.

Thank you all for your suggestions/time,
Joseph
http://www.jjude.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: word shifts

2008-05-04 Thread Arnaud Delobelle
[EMAIL PROTECTED] writes:

> George Sakkis:
>> A faster algorithm is to create a 'key' for each word, defined as the
>> tuple of ord differences (modulo 26) of consecutive characters.
>
> Very nice solution, it uses the same strategy used to find anagrams,
> where keys are
> "".join(sorted(word))
> Such general strategy to look for a possible invariant key for the
> subsets of the required solutions is quite useful.

Or even:

def get_key(word):
initial = ord(word[0])
return tuple((ord(x) - initial) % 26 for x in word[1:])

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


Re: Please help - Tkinter not doing anything

2008-05-04 Thread Protected
On May 4, 12:18 pm, [EMAIL PROTECTED] wrote:
> On May 4, 5:22 am, Protected <[EMAIL PROTECTED]> wrote:
>
> > I had previously ran the import line. I prepended it to the example
> > code I'm trying to run every time but it did not help, still nothing
> > happens. With or without var before 'root'. I'm pasting the code in
> > IDLE and using Windows XP as written in the first post.
>
> Tkinter doesn't work if you type the statements in IDLE. I don't
> remember the specifics of it, but essentially it doesn't work because
> IDLE is itself a Tkinter app. You have to type it at the Python
> command line or save it in a file. BTW, if you're on Windows, you
> should definitely check wxPython. I used to work with Tkinter,
> struggling with it all the time only to get a lame result most of the
> time. Then I switched to wxPython, and in the same week I was learning
> it I did a better GUI than I ever did in Tkinter (with months of
> work!). I feel it makes it easier to make your program have a better
> structure and design, and thus lets you focus on the actual task of
> the program, rather than in the GUI itself. Plus, on Windows, you'll
> get the widgets to look more natively, like any good quality
> application there is on Windows.

Ah, but is it cross platform?

(Thanks for letting me know about IDLE.)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Script Optimization

2008-05-04 Thread David
On Sun, May 4, 2008 at 4:43 AM, lev <[EMAIL PROTECTED]> wrote:
> Can anyone provide some advice/suggestions to make a script more
>  precise/efficient/concise, etc.?

Hi, I started tidying up the script a bit, but there are some parts I
don't understand or look buggy. So I'm forwarding you the version I
have so far. Look for the comments with my e-mail address in them for
more information.

If I have time I'll tidy up the script some more when I have more info
about those issues.

Here are the changes I made to your version:

* Remove newlines introduced by email
* Move imports to start of file
* Change indentation from 8 spaces to 4
* Move main() to bottom of script
* Remove useless "pass" and "return" lines
* Temporarily change broken "chdir" line
* Split lines so they fit into 80 chars
* Add spaces after commas
* Use path.join instead of string interpolation
* rename rename() to rename_md5() because rename() shadows a function
imported from os.
* Rename vars shadowing imported names
* Improve logic for checking when to print help
* Create emtpy md5 listing file if one doesn't exist
* Add a comment for a dodgy-looking section

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


Re: Script Optimization

2008-05-04 Thread David
Eh, I forgot the attachment.
#!/usr/bin/env python

import md5
from glob import glob
from optparse import OptionParser
from os import chdir, path, rename, remove
from sys import argv

def check(checksums):
checksums = open(checksums, 'r')
chgfiles = {}
msngfiles = []
for fline in checksums.readlines():
line = fline.split(' *')
orig = line[0].upper()
try:
file_ = open(line[1].strip(),'rb')
chunk = 8196
sum_ = md5.new()
while(1):
chunkdata = file_.read(chunk)
if not chunkdata:
break
sum_.update(chunkdata)
new = sum_.hexdigest().upper()
file_.close()
if  new == orig:
print '.',
else:
chgfiles[line[1]] = (orig, new)
except IOError:
msngfiles.append(line[1])
checksums.close()
chgfileskeys = chgfiles.keys()
chgfileskeys.sort()
msngfiles.sort()
print '\n'
if len(chgfiles) != 0:
print 'Files changed:'
for key in chgfileskeys:
print key.strip('\n'), 'changed from:\n\t', chgfiles[key][0], \
  'to\n\t', chgfiles[key][1]
print '\n\t', len(chgfiles), 'file(s) changed.\n'
if len(msngfiles) != 0:
print 'Files not found:'
for x in range(len(msngfiles)):
print '\t', msngfiles[x]
print '\n\t', len(msngfiles), 'file(s) not found.\n'
print "\n\tChecksums Verified\n"

def rename_md5(newdirname, checksums):
dict_ = md5format(checksums)

# David <[EMAIL PROTECTED]>:
# What's going on here? Looks like you assume there are always
# 4 files (001string 1, 0001string 2, etc), which you rename to
# string1, string2, etc. This logic is unsafe because (1) those files
# might not exist, and (2) if some files exist but not others then you
# rename them from eg: 0001string 2 (if 0001string 1 does not exist) to
# sting1. I don't know if this is ententional.

dirlist = glob('./00[1-4]string [1-4]')
dirlist.sort()
for x in range(4):
rename(dirlist[x], newdirname[x])
print '\t', dirlist[x], 'renamed to:', newdirname[x]
chdir('./%s' % newdirname[x])
for oldfilename in glob('*.mp3'):
newfilename = oldfilename[3:]
rename(oldfilename, newfilename)
print '\t\t', oldfilename, 'renamed to:', newfilename
dict_ = md5fname_edit(dict_, dirlist[x], newdirname[x])
chdir('..')
md5write(dict_, checksums)
replace('Webpage.htm', dirlist, newdirname)
print '\n\tDirectories and Files renamed.'

def md5format(checksums):
dict_ = {}
checksums = open(checksums, 'r')
for line in checksums.readlines():
splitline = line.split(' *')
dict_[splitline[1]] = (splitline[0], splitline[1].split('\\'))
checksums.close()
return dict_

def md5fname_edit(dict_, olddir, newdir):
for x in dict_.keys():
if dict_[x][1][0] == olddir[2:]:
dict_[x] = (dict_[x][0], [newdir, dict_[x][1][1]])
if dict_[x][1][1][0] == '0':
dict_[x] = (dict_[x][0], [dict_[x][1][0], dict_[x][1][1][3:]])
return dict_

def md5write(dict_, checksums):
keys = dict_.keys()
keys.sort()
checksums = open(checksums, 'w')
for x in keys:
try:
checksums.writelines('%s *%s/%s' % (dict_[x][0], dict_[x][1][0],
dict_[x][1][1]))
except IndexError:
checksums.writelines('%s *%s' % (dict_[x][0], dict_[x][1][0]))

def replace(file_, oldlist, newlist):
new = open(file_, 'r').read()
for x in range(4):
new = new.replace(oldlist[x][2:], newlist[x], 1)
remove(file_)
file_ = open(file_, 'w', len(new))
file_.write(new)

def main():
fullpath = path.abspath(path.dirname(argv[0]))

# David <[EMAIL PROTECTED]>:
# This is broken: what path are you trying to change to here?
# - Assuming for the moment that you want to change to the same dir as
#   the script.
# TODO: Fix this.
#chdir(fullpath[:-10])
chdir(fullpath)
checksums = path.join(fullpath, 'checksums.md5')
newdirname = ('string1', 'string2', 'string3', 'string4')
usage = "%prog [options]"
parser = OptionParser(usage)
parser.add_option('-c', '--check', action='store_true', dest='check',
  help='verify checksums')
parser.add_option('-r', '--rename', action='store_true', dest='rename',
  help='rename files to a more usable form (write rights '
   'needed)')

(options, args) = parser.parse_args()

# Generate an empty checksums file if one doesn't already exist. Otherwise
# the logic has problems later:
if not path.isfile(checksums):
f = open(checksums, 'w')
f.close()

did_something = False # Set to true if some logi

matplotlib pylab plot() BadWindow error

2008-05-04 Thread oyinbo55
I am trying to use the pylab plot command on my laptop running Ubuntu
6.06 (Dapper).  Although the plot command works fine on my XP desktop
at work, I cannot open the plot window on the laptop.  I edited
matplotlibrc to change interactive: to "True".  In idle, I entered the
commands:
 >>> from pylab import *
 >>> plot([1,2,3])

The terminal window closed abruptly when I typed the closing
parenthesis.
I have been warned not to use the show() command in interactive mode.
I tried:
 [EMAIL PROTECTED]:~$ idle-python2.4 -n
In idle -n, I entered the sam two commands.  This time I got an error
message:
 The program 'idle-python2.4' received an X Window System error.
 This probably reflects a bug in the program.
 The error was 'BadWindow (invalid Window parameter)'.
   (Details: serial 2875 error_code 3 request_code 15 minor_code
0)
   (Note to programmers: normally, X errors are reported
asynchronously;
   that is, you will receive the error a while after causing it.
   To debug your program, run it with the --sync command line
   option to change this behavior. You can then get a meaningful
   backtrace from your debugger if you break on the gdk_x_error()
function.)

Using the Python prompt without idle, I got no error message, but the
window simply didn't open:

 [EMAIL PROTECTED]:~$ python
 Python 2.4.3 (#2, Mar  7 2008, 01:58:20)
 [GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
 Type "help", "copyright", "credits" or "license" for more
information.
 >>> from pylab import *
 >>> plot([1,2,3])
 []

Have I skipped a step?

Thanks for all the great information on your group.

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


please remove me from this list

2008-05-04 Thread Vladimir Kropylev
please remove me from this list, thanks
--
http://mail.python.org/mailman/listinfo/python-list


Re: Light slices + COW

2008-05-04 Thread David
>  > D compiles to efficient machine code so Python is at a disadvantage
>  > even if you use the same syntax (see my first example). You can make
>  > the Python version faster, but beware of premature optimization.
>
>  This time I don't agree with this "premature optimization" thing. My
>  original Python version is just 5 lines long, it's readable enough,
>  and it's a part of a large library of mine of similar functions, they
>  must be fast because I use them all the time as building blocks in
>  programs.
>

Have you looked into the 'numpy' libraries? Those have highly
optimized array/numeric processing functions. Also the have a concept
of getting 'views' when you slice, rather than a full copy.

I also suggest benchmarking your apps which use your libs and see
where the bottlenecks are. Then you can move those to external
compiled modules (c/pyrex/cython/shedskin/etc). You can keep your
slower Python version around as a reference implementation.

>  > What I'dlike to see is a rope[1] module for Python.
>
>  People have already suggested it, and there's even an implementation
>  to replace Python strings. It was refused because... I don't know why,
>  maybe its implementation was too much more complex than the current
>  one, and it wasn't faster in all situations (and I think Guido wants
>  data structures that try to replace the basic built-in ones to be
>  faster in all situations and user-transparent too).

I'd be happy if there was a separate 'rope' class that you could wrap
arbitrary long sequences in when you need to (the same way that STL
has it separate to the string class, even though the string class has
a lot of other optimizations (internal ref counts, etc, to avoid
unnecessary copies)). Do you know one?

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


Re: is +=1 thread safe

2008-05-04 Thread Alexander Schmolck
Gary Herron <[EMAIL PROTECTED]> writes:

> But... It's not!  
>
> A simple test shows that.   I've attached a tiny test program that shows this
> extremely clearly.  Please run it and watch it fail.

In [7]: run ~/tmp/t.py
final count: 200
  should be: 200

(I took the liberty to correct your test to actually do what I said, namely
use a numpy.array; just replace ``count = 0`` with ``import numpy; count =
numpy.array(0)``).


'as

p.s. please don't send me copies to on-list replies, at least not without
explicitly mentioning the fact -- I've got better things to do then guessing
whether something was meant to be off-list or not.
--
http://mail.python.org/mailman/listinfo/python-list


Re: please remove me from this list

2008-05-04 Thread David
On Sun, May 4, 2008 at 2:48 PM, Vladimir Kropylev <[EMAIL PROTECTED]> wrote:
> please remove me from this list, thanks
>  --
>  http://mail.python.org/mailman/listinfo/python-list
>

Follow that link (http://mail.python.org/mailman/listinfo/python-list)
and see the "Python-list Subscribers" section.

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


Re: ISBN Barecode reader in Python?

2008-05-04 Thread David
On Sun, May 4, 2008 at 1:41 PM, Joseph <[EMAIL PROTECTED]> wrote:
> All: I have written a program to query Amazon with ISBN and get the
>  book details. I would like to extend so that I can read ISBN from the
>  barcode (I will take a photo of the same using webcam or mobile). Are
>  there any opensource/free SDK doing the same? As it is a hobby
>  project, I don't like to spend money on the SDK.
>

I Googled a bit and found this:

http://jocr.sourceforge.net/index.html

According to the docs it should be able to scan bar codes.

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


SQLObject 0.9.6

2008-05-04 Thread Oleg Broytmann
Hello!

I'm pleased to announce version 0.9.6, a minor bug fix release of SQLObject.


What is SQLObject
=

SQLObject is an object-relational mapper.  Your database tables are described
as classes, and rows are instances of those classes.  SQLObject is meant to be
easy to use and quick to get started with.

SQLObject supports a number of backends: MySQL, PostgreSQL, SQLite,
Firebird, Sybase, MSSQL and MaxDB (also known as SAPDB).


Where is SQLObject
==

Site:
http://sqlobject.org

Development:
http://sqlobject.org/devel/

Mailing list:
https://lists.sourceforge.net/mailman/listinfo/sqlobject-discuss

Archives:
http://news.gmane.org/gmane.comp.python.sqlobject

Download:
http://cheeseshop.python.org/pypi/SQLObject/0.9.6

News and changes:
http://sqlobject.org/News.html


What's New
==

News since 0.9.5


Bug Fixes
~

* A bug in inheritable delColumn() that doesn't remove properties was fixed.

* A minor bug was fixed in col.py - the registry must be passed to findClass().

* Reverted the patch declarative.threadSafeMethod() - it causes more harm
  then good.

For a more complete list, please see the news:
http://sqlobject.org/News.html

Oleg.
-- 
 Oleg Broytmannhttp://phd.pp.ru/[EMAIL PROTECTED]
   Programmers don't die, they just GOSUB without RETURN.
--
http://mail.python.org/mailman/listinfo/python-list


SQLObject 0.10.1

2008-05-04 Thread Oleg Broytmann
Hello!

I'm pleased to announce version 0.10.1, a bugfix release of 0.10 branch
of SQLObject.


What is SQLObject
=

SQLObject is an object-relational mapper.  Your database tables are described
as classes, and rows are instances of those classes.  SQLObject is meant to be
easy to use and quick to get started with.

SQLObject supports a number of backends: MySQL, PostgreSQL, SQLite,
Firebird, Sybase, MSSQL and MaxDB (also known as SAPDB).


Where is SQLObject
==

Site:
http://sqlobject.org

Development:
http://sqlobject.org/devel/

Mailing list:
https://lists.sourceforge.net/mailman/listinfo/sqlobject-discuss

Archives:
http://news.gmane.org/gmane.comp.python.sqlobject

Download:
http://cheeseshop.python.org/pypi/SQLObject/0.10.1

News and changes:
http://sqlobject.org/News.html


What's New
==

News since 0.10.0
-

Bug Fixes
~

* Fixed a bug: limit doesn't work in sqlbuilder.Select.

* A bug in inheritable delColumn() that doesn't remove properties was fixed.

* A minor bug was fixed in col.py - the registry must be passed to findClass().

* Reverted the patch declarative.threadSafeMethod() - it causes more harm
  then good.

For a more complete list, please see the news:
http://sqlobject.org/News.html

Oleg.
-- 
 Oleg Broytmannhttp://phd.pp.ru/[EMAIL PROTECTED]
   Programmers don't die, they just GOSUB without RETURN.
--
http://mail.python.org/mailman/listinfo/python-list


Image grab in Python

2008-05-04 Thread Valerio Valerio
Hi,

anyone know a Python library to grab the size of a selected image region
(the selection will be made with the mouse), that work in Linux ?

I found a module to do that in the PIL library but only work in Windows -
http://www.pythonware.com/library/pil/handbook/imagegrab.htm

Thanks in advance.

Best regards,

-- 
Valério Valério

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

Re: Image grab in Python

2008-05-04 Thread David
On Sun, May 4, 2008 at 4:25 PM, Valerio Valerio <[EMAIL PROTECTED]> wrote:
> Hi,
>
> anyone know a Python library to grab the size of a selected image region
> (the selection will be made with the mouse), that work in Linux ?

You might be able to use this tool:

http://freshmeat.net/projects/gtkshots/

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


Determine socket family at runtime

2008-05-04 Thread Giampaolo Rodola'
Hi there,
since the socket.socket.family attribute has been introduced only in
Python 2.5 and I need to have my application to be backward compatible
with Python 2.3 and 2.4 I'd like to know how could I determine the
family of a socket.socket instance which may be AF_INET or AF_INET6.
Is there some kind of getsockopt() directive I could use?
For now I've been able to determine the family by using:

# self.socket = a connected socket.socket instance
ip, port = self.socket.getsockname()[0:2]
af = socket.getaddrinfo(ip, port)[0][0]

...but I'd like to know if some other solution is preferable.



Thanks.


--- Giampaolo
http://code.google.com/p/pyftpdlib
--
http://mail.python.org/mailman/listinfo/python-list


Re: Colors for Rows

2008-05-04 Thread Scott David Daniels

D'Arcy J.M. Cain wrote:

On Tue, 29 Apr 2008 15:03:23 -0400
"J. Cliff Dyer" <[EMAIL PROTECTED]> wrote:

Or, if you aren't sure how many colors you'll be using, try the more
robust:

bg[z % len(bg)]


Good point although I would have calculated the length once at the
start rather than each time through the loop.


You may be surprised to find out that len(foo) is typically a
retrieval question, not so much a calculation.  Of course, measure
anything you care about, and special methods may do much more, but
in general, go for the simplest, clearest code you can write, and
then speed it up when it needs it.

I'd use len(bg) above unless my code was littered with calls to
len of the same thing, in which case it might it might be clearer
to choose a good name for len(bg).

--Scott David Daniels
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list


python-list@python.org

2008-05-04 Thread joop renes
hi,
i hope this is the right list for the following question of a c++
hacker,python newbie. i have a library in c++ to which i want to add a
python GUI and other python stuff.The library has multithreading
components, while python uses a reference counted memory model. Usually
mixing reference counting with multithreading is frowned upon, at least
in the c++ world. what am i to expect in the python world, if i bring
multithreading c++ into it? just performance penalty or total disaster?
best regards
joop renes.


 

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


Writing a aiff audio file problem?

2008-05-04 Thread jym
Hi all

I can not creat a usable audio file in the aiff format. The example code
below creates a wav file that I can listen to just fine. However, I just get
noise when I listen to the resulting aiff file. Is this a byte order
problem? I am using Python 2.5.2 on Windows XP. Any guidance on this issue
would be appreciated.

Thanks

jym

import pyaudio
import wave
import sys
import aifc

chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 8000
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output"

p = pyaudio.PyAudio()

stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = chunk)

print "* recording"
all = []
for i in range(0, RATE / chunk * RECORD_SECONDS):
data = stream.read(chunk)
all.append(data)
print "* done recording"

stream.close()
p.terminate()

#write data to AIFF file
data = ''.join(all)
af = aifc.open(WAVE_OUTPUT_FILENAME + ".aiff", 'wb')
af.setnchannels(CHANNELS)
af.setsampwidth(p.get_sample_size(FORMAT))
af.setframerate(RATE)
af.writeframes(data)
af.close()

# write data to WAVE file
wf = wave.open(WAVE_OUTPUT_FILENAME + ".wav", 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(data)
wf.close()
--
http://mail.python.org/mailman/listinfo/python-list

Re: pyqt4: trouble making custom widget appear in mainwindow

2008-05-04 Thread Phil Thompson
On Sunday 04 May 2008, Lance Gamet wrote:
> Hi,
> about 3 years ago I did a bit of Qt3 and 4 on C++, now I am trying to do
> something in python.
>
> A QMainWindow subclass as my mainwindow. Its basically a frame with no
> widgets of its own.
>
> Several QWidget subclasses each representing a mode of the program, these
> widgets should appear in the mainwindow depending on what part of the
> program is being used.
>
> I have made the main window and a couple of widgets with designer-qt4 and
> generate a Ui_Whatever.py with pyuic4 and subclass that.
>
> My problem is I cant get my widgets to appear in the main window.
>
> My MainWindow.py looks like (imports etc skipped):
>
> class MainWindow(QMainWindow, Ui_MainWindow):
> def __init___(self):
> QMainWindow.__init__(self)
> self.setupUi(self)
>
> def newTeamWizardMode(self):
> Widget = QWidget()
> self.newTeamWizard = CreateNewTeamWizard(Widget)
> self.newTeamWizard.setGeometry(QRect(0,0,90, 160))
> self.newTeamWizard.setObjectName("newTeamWizard")
> self.newTeamWizard.show()
>
> My CreateNewTeamWizard is defined like so:
>
> class CreateNewTeamWizard(QWidget, Ui_CreateNewTeamWizard):
> def __init___(self, parent = None):
> QWidget.__init__(self, parent)
> self.setupUi(self)
>
> In my main program i make the main window and try and add the widget like
> this:
>
> app = Qt.QApplication(sys.argv)
> mainWindow = MainWindow()
> app.connect(app, Qt.SIGNAL("lastWindowClosed()"), app, Qt.SLOT("quit
> ()"))
> mainWindow.newTeamWizardMode()
> mainWindow.show()
>
> One thing i tried was changing the CreateNewTeamWizard in
> MainWindow.newTeamWizardMode to CreateNewTeamWizard(Widget, self) to
> provide the main window as the parent but I get the following error:
>
> self.newTeamWizard = CreateNewTeamWizard(Widget, self)
> TypeError: argument 2 of QWidget() has an invalid type
>
> I have looked for examples, but everything I have found is a bit too
> simple. Creating a single window or widget, displaying it as the programs
> main window and exiting when the window is closed.
>
> I havent found anything to tell me how to make a widget appear in a
> mainwindow how I want.
>
> Any tips?

I think QMainWindow.setCentralWidget() is what you are looking for.

> If someone can point me to a nice open source program that uses pyqt4
> that might help.

The eric4 IDE probably covers most things you'll ever need.

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


Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-04 Thread hdante
On May 3, 7:05 pm, sturlamolden <[EMAIL PROTECTED]> wrote:
> On May 3, 10:13 pm, hdante <[EMAIL PROTECTED]> wrote:
>
> >  I believe that moving this to third party could be better. What about
> > numpy ? Doesn't it already have something similar ?
>
> Yes, Kahan summation makes sence for numpy arrays. But the problem
> with this algorithm is optimizing compilers. The programmer will be

 No, optimizing compilers shouldn't discard floating point operations
by default, since it changes program behavior. If they do, at least
there should be a flag to turn it off.

> forced to use tricks like inline assembly to get around the optimizer.
> If not, the optimizer would remove the desired features of the
> algorithm. But then we have a serious portability problem.

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


Re: Image grab in Python

2008-05-04 Thread Valerio Valerio
I can grab the image,  I need a way of grab the region size with the mouse,
a easy way of the user select a region of the image to analyze, something
like the  "Rectangle selection tool" of gimp.

Regards,

-- 
Valério Valério

http://www.valeriovalerio.org

2008/5/4 David <[EMAIL PROTECTED]>:

> On Sun, May 4, 2008 at 4:25 PM, Valerio Valerio <[EMAIL PROTECTED]> wrote:
> > Hi,
> >
> > anyone know a Python library to grab the size of a selected image region
> > (the selection will be made with the mouse), that work in Linux ?
>
> You might be able to use this tool:
>
> http://freshmeat.net/projects/gtkshots/
>
> David.
>
--
http://mail.python.org/mailman/listinfo/python-list

PyPy Berlin Sprint, 17th - 22nd May 2008

2008-05-04 Thread Carl Friedrich Bolz
=
 PyPy Berlin Sprint (17-22nd May 2008)
=

The next PyPy sprint will be in the crashed `c-base space station`_,
Berlin, Germany, Earth, Solar System.  This is a fully public sprint:
newcomers (from all planets) and topics other than those proposed below
are welcome.

.. _`c-base space station`: http://www.c-base.org/


--
Goals and topics of the sprint
--

  - work on PyPy's JIT generator: we are refactoring parts of the
compiling logic, in ways that may also allow generating better
machine code for loops (people or aliens with knowledge on
compilers and SSA, welcome)

  - work on the SPy VM, PyPy's Squeak implementation, particularly the
graphics capabilities

  - work on PyPy's GameBoy emulator, which also needs graphics support

  - trying some large pure-Python applications or libraries on PyPy and
fixing the resulting bugs. Possibilities are Zope 3, Django and
others.

* We are open to all sorts of other tasks during the sprint, just
  propose something.

---
Location & Accomodation
---

The sprint will take place in the c-base in Berlin. The address is::

c-base e.V.
Rungestrasse 20
10179 Berlin

To get there, take the U-Bahn or S-Bahn to the station
"Jannowitzbrücke". See here_ for a map to get to c-base from there.

.. _here:
http://maps.google.com/maps?f=q&hl=en&geocode=&q=c-base+berlin&ie=UTF8&ll=52.515464,13.408985&spn=0.020449,0.057335&z=15&iwloc=A

If you want to come, please register via by svn:

  http://codespeak.net/svn/pypy/extradoc/sprintinfo/berlin-2008/people.txt

or write a mail to the pypy-sprint mailing list if you do not yet have
check-in rights:

  http://codespeak.net/mailman/listinfo/pypy-sprint

Of course, especially given the location, it's ok to show up even if you
didn't register.  (The c-base has probably got the plans of Guido's
famous time machine anyway, so you can register last week.)


---
Exact times
---

The sprint will be from 17th to 22nd of May 2008. We will start
sprinting every day at 10. An introduction will be given on the first
day, if there is interest.
___
pypy-sprint mailing list
[EMAIL PROTECTED]
http://codespeak.net/mailman/listinfo/pypy-sprint

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


Re: is +=1 thread safe

2008-05-04 Thread Marc Christiansen
Alexander Schmolck <[EMAIL PROTECTED]> wrote:
> Gary Herron <[EMAIL PROTECTED]> writes:
> 
>> But... It's not!  
>>
>> A simple test shows that.   I've attached a tiny test program that
>> shows this extremely clearly.  Please run it and watch it fail.
> 
> In [7]: run ~/tmp/t.py
> final count: 200
>   should be: 200
> 
> (I took the liberty to correct your test to actually do what I said, namely
> use a numpy.array; just replace ``count = 0`` with ``import numpy; count =
> numpy.array(0)``).

I did the same change. Here are the results of four runs:

final count: 180
  should be: 200

final count: 196
  should be: 200

final count: 1999123
  should be: 200

final count: 178
  should be: 200

This is on a AMD64 X2 using Python 2.5.2 and numpy 1.0.4.

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


Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-04 Thread Duncan Booth
Szabolcs Horvát <[EMAIL PROTECTED]> wrote:

> I thought that it would be very nice if the built-in sum() function used 
> this algorithm by default.  Has this been brought up before?  Would this 
> have any disadvantages (apart from a slight performance impact, but 
> Python is a high-level language anyway ...)?

There's a thread you might find interesting:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/9eda29faf92f532e/027cef7d4429aa3a

In that thread I suggested that Python ought to implement sum by adding 
together each pair of values, then each pair of results and so on. This 
means that for input values of a similar magnitude the intermediate results 
stay balanced. The implementation doesn't actually do all the first level 
sums first, it builds up a stack containing the sum of 2^k, 2^(k-1)...2 
values. Also it works for other types as well, and for strings or lists has 
the advantage of minimising the number of large string or list concatenations.

If you follow that thread far enough you'll find a post by Jussi Salmela 
which shows that summing adjacent pairs performs as well as Kahan summation
(he says 'almost as good' but in fact the only time they get different 
results the 'correct' answer is 50.05 and Kahan and sumpairs get 
the two nearest representable results either side: 50.048 and 
50.054 respectively).

I never did get round to re-coding my sumpairs() function in C, I would be 
interested to see just how much overhead it introduces (and I suspect it
would be faster than the existing sum in some cases such as for lists).
--
http://mail.python.org/mailman/listinfo/python-list


default gettext localedir on windows

2008-05-04 Thread ZeeGeek
Hi, what's the default localedir for gettext module on windows? In
Linux, it's /usr/share/locale. Where should I put the *.mo file in
order to make the translation work?

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


Re: is +=1 thread safe

2008-05-04 Thread Gary Herron

Alexander Schmolck wrote:

Gary Herron <[EMAIL PROTECTED]> writes:

  
But... It's not!  


A simple test shows that.   I've attached a tiny test program that shows this
extremely clearly.  Please run it and watch it fail.



In [7]: run ~/tmp/t.py
final count: 200
  should be: 200

(I took the liberty to correct your test to actually do what I said, namely
use a numpy.array; just replace ``count = 0`` with ``import numpy; count =
numpy.array(0)``).
  


The test was meant to simulate the OP's problem, but even with your 
suggestion of using numpy, it *still* fails!  Just start increasing the 
number of increments by a factor of 2, 4, 10 until it fails:


final count: 198
 should be: 200

final count: 597
 should be: 600

final count: 5995068
 should be: 600

(Surprisingly, using numpy makes this test *much* slower, meaning the 
increment executes many more instructions, which implies hitting a 
thread context switch at exactly the critical point is much less 
common.   But it can happen if you run the test long enough.)


I am running this on a Core2 Duo CPU, but the GIL should prevent that 
from affecting the result of the run.


I've reattached the file with the numpy change (and a larger loop 
counter) for others to try.


Gary Herron



'as

p.s. please don't send me copies to on-list replies, at least not without
explicitly mentioning the fact -- I've got better things to do then guessing
whether something was meant to be off-list or not.
  

My apologies.

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




t.py
Description: application/python
--
http://mail.python.org/mailman/listinfo/python-list

Re: Determine socket family at runtime

2008-05-04 Thread Wojciech Walczak
2008/5/4, Giampaolo Rodola' <[EMAIL PROTECTED]>:
>  For now I've been able to determine the family by using:
>
>  # self.socket = a connected socket.socket instance
>  ip, port = self.socket.getsockname()[0:2]
>  af = socket.getaddrinfo(ip, port)[0][0]
>
>  ...but I'd like to know if some other solution is preferable.

Nope, there is none. Using getaddrinfo() to check address family
is the de facto standard.

-- 
Regards,
Wojtek Walczak
http://www.stud.umk.pl/~wojtekwa/
--
http://mail.python.org/mailman/listinfo/python-list


Weird import problem

2008-05-04 Thread Anton81
I have a directory structure like

NS/dir1/file1.py
NS/dir2/file2.py

if in the python shell I type

import NS.dir1.file1

it works, however typing

import NS.dir2.file2

fails with

ImportError: No module named dir2.file2

Any ideas what could go wrong?
Directory permissions seem to be OK.

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


Re: Weird import problem

2008-05-04 Thread Diez B. Roggisch

Anton81 schrieb:

I have a directory structure like

NS/dir1/file1.py
NS/dir2/file2.py



This *must* be wrong or at least not the full directory listing - please 
read


http://docs.python.org/tut/node8.html


if in the python shell I type

import NS.dir1.file1

it works, however typing

import NS.dir2.file2

fails with

ImportError: No module named dir2.file2

Any ideas what could go wrong?
Directory permissions seem to be OK.


Missing __init__.py in the dir2?

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


Re: Determine socket family at runtime

2008-05-04 Thread Francesco Bochicchio
On Sun, 04 May 2008 08:49:55 -0700, Giampaolo Rodola' wrote:

> Hi there,
> since the socket.socket.family attribute has been introduced only in
> Python 2.5 and I need to have my application to be backward compatible
> with Python 2.3 and 2.4 I'd like to know how could I determine the
> family of a socket.socket instance which may be AF_INET or AF_INET6.
> Is there some kind of getsockopt() directive I could use?
> For now I've been able to determine the family by using:
> 
> # self.socket = a connected socket.socket instance
> ip, port = self.socket.getsockname()[0:2]
> af = socket.getaddrinfo(ip, port)[0][0]
> 
> ...but I'd like to know if some other solution is preferable.
> 
> 
> 
> Thanks.
> 
> 
> --- Giampaolo
> http://code.google.com/p/pyftpdlib

Ciao,

what about wrapping the socket type and  adding a 'family' attribute
to the base socket class? Something like:

class SocketWrapper(socket.socket):
def __init__(self, family, type, proto=0):
socket.socket.__init__(self, family, type, proto)
self.family = family

then you have just to create the sockets with SocketWrapper insetead of
socket.socket. For the rest of your code it would not matter, and then you 
are sure to always have a .family attribute.

Ciao

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


Real Time Midi File Playback - Reading and Writing midi at the same time

2008-05-04 Thread Gilly
Hi
I am trying to create an application that uses some form of input to
create a midi file.
I would like for this to be a 'real time' process. In other words, I
want to be able to begin playing the midi file before I finish writing
it, and continue writing as it plays.

I would really appreciate any help possible on this matter.

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


Re: default gettext localedir on windows

2008-05-04 Thread Thorsten Kampe
* ZeeGeek (Sun, 4 May 2008 08:59:05 -0700 (PDT))
> Hi, what's the default localedir for gettext module on windows? In
> Linux, it's /usr/share/locale. Where should I put the *.mo file in
> order to make the translation work?

%PYTHONHOME%\share\locale

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


Re: Image grab in Python

2008-05-04 Thread David
On Sun, May 4, 2008 at 5:28 PM, Valerio Valerio <[EMAIL PROTECTED]> wrote:
> I can grab the image,  I need a way of grab the region size with the mouse,
> a easy way of the user select a region of the image to analyze, something
> like the  "Rectangle selection tool" of gimp.
>

I assume what you want to do is allow the user to drag the mouse
across your desktop, and your program, running in the background,
picks this up? As opposed to the user making a rectangular selection
entirely within your app.

I think the simplest way to do this is to capture the entire screen,
then show it as an image on a window covering the entire screen (eg:
fullscreen mode, or a regular window without decorations).  Then
listen for mouse events. There should be a way to show the 'rectangle
elastic band' at the same time. Otherwise just draw a rectangle
between where the user first clicked and where the mouse is now (until
they release the mouse button).

Another way would be to listen to all events sent through X, and act
based on the mouse events. VNC does something similar.

A good starting point would be the Python xlib libraries:

http://python-xlib.sourceforge.net/

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


Re: Image grab in Python

2008-05-04 Thread David
>
>  Another way would be to listen to all events sent through X, and act
>  based on the mouse events. VNC does something similar.
>

See the 'record_demo.py' example that comes with python-xlib.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Image grab in Python

2008-05-04 Thread Valerio Valerio
Hi,

2008/5/4 David <[EMAIL PROTECTED]>:

> On Sun, May 4, 2008 at 5:28 PM, Valerio Valerio <[EMAIL PROTECTED]> wrote:
> > I can grab the image,  I need a way of grab the region size with the
> mouse,
> > a easy way of the user select a region of the image to analyze,
> something
> > like the  "Rectangle selection tool" of gimp.
> >
>
> I assume what you want to do is allow the user to drag the mouse
> across your desktop, and your program, running in the background,
> picks this up? As opposed to the user making a rectangular selection
> entirely within your app.


What I want is display a window with a image, the user select a region of
the image, and the region value is passed to my program, my program slice
the image region, and analyze it.

I think the Xlib libraries can help in my task, thanks for the help :)

Cheers,

-- 
Valério Valério

http://www.valeriovalerio.org


>
> I think the simplest way to do this is to capture the entire screen,
> then show it as an image on a window covering the entire screen (eg:
> fullscreen mode, or a regular window without decorations).  Then
> listen for mouse events. There should be a way to show the 'rectangle
> elastic band' at the same time. Otherwise just draw a rectangle
> between where the user first clicked and where the mouse is now (until
> they release the mouse button).
>
> Another way would be to listen to all events sent through X, and act
> based on the mouse events. VNC does something similar.
>
> A good starting point would be the Python xlib libraries:
>
> http://python-xlib.sourceforge.net/
>
> David.
>
--
http://mail.python.org/mailman/listinfo/python-list

Re: default gettext localedir on windows

2008-05-04 Thread ZeeGeek
On May 5, 1:16 am, Thorsten Kampe <[EMAIL PROTECTED]> wrote:
> * ZeeGeek (Sun, 4 May 2008 08:59:05 -0700 (PDT))
>
> > Hi, what's the default localedir for gettext module on windows? In
> > Linux, it's /usr/share/locale. Where should I put the *.mo file in
> > order to make the translation work?
>
> %PYTHONHOME%\share\locale

I tried moving the *.mo file into %PYTHONHOME%\share\locale\zh_CN
\LC_MESSAGES, but still no luck. The following is the code snippet I
use:

import gettext
gettext.install('testprogram', unicode = True)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Image grab in Python

2008-05-04 Thread David
> What I want is display a window with a image, the user select a region of
> the image, and the region value is passed to my program, my program slice
> the image region, and analyze it.
>

If it's your apps own window, then getting a rectangle selected by the
user is simple.

1) Make skeleton x/gtk/wx/qt/etc app
2) Add code to show an image
3) Add code to capture mouse events
4) Add code to show the 'rubber band' rectangle over the mouse selection area
5) When the user releases the mouse, then extract the part of the
rectangle between where a mouse button was clicked and where it was
released.

All this would be in the docs for the Python x/gtk/wx/qt/etc libs.
Where is the difficulty?

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


Re: Real Time Midi File Playback - Reading and Writing midi at the same time

2008-05-04 Thread David
On Sun, May 4, 2008 at 7:11 PM, Gilly <[EMAIL PROTECTED]> wrote:
> Hi
>  I am trying to create an application that uses some form of input to
>  create a midi file.
>  I would like for this to be a 'real time' process. In other words, I
>  want to be able to begin playing the midi file before I finish writing
>  it, and continue writing as it plays.

Have you tried the MIDI libraries listed on this page?

http://wiki.python.org/moin/PythonInMusic

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


Re: is +=1 thread safe

2008-05-04 Thread Carl Banks
On May 4, 12:03 pm, Gary Herron <[EMAIL PROTECTED]> wrote:
> Alexander Schmolck wrote:
> > Gary Herron <[EMAIL PROTECTED]> writes:
>
> >> But... It's not!
>
> >> A simple test shows that.   I've attached a tiny test program that shows 
> >> this
> >> extremely clearly.  Please run it and watch it fail.
>
> > In [7]: run ~/tmp/t.py
> > final count: 200
> >   should be: 200
>
> > (I took the liberty to correct your test to actually do what I said, namely
> > use a numpy.array; just replace ``count = 0`` with ``import numpy; count =
> > numpy.array(0)``).
>
> The test was meant to simulate the OP's problem, but even with your
> suggestion of using numpy, it *still* fails!


Ok, so numpy scalars don't support += atomically.  Thank you for your
skepticism in discovering this.

However, what I said was not wholly untrue: code in C extensions is
protected by the GIL and thus not interruptable, unless it either
releases the GIL, or calls back into Python code (which is apparently
what numpy scalars do).

To illustrate, a small extension module is included below.  Just to
make things interesting, I added a long busy loop in between reading
and setting the counter, just to give the other thread maximum
opportunity to cut in.

If you were to compile it and run the test, you would find that it
works perfectly.


===
/* atomic.c */

#include 
#include 

typedef struct {
PyObject_HEAD
long value;
} AtomicCounterObject;

static int init(AtomicCounterObject* self, PyObject* args,
  PyObject* kwargs) {
self->value = 0;
return 0;
}

static PyObject* iadd(AtomicCounterObject* self, PyObject* inc) {
long incval = PyInt_AsLong(inc);
long store, i;
static int bigarray[10];
if (incval == -1 && PyErr_Occurred()) return 0;

store = self->value;

/* Give the thread plenty of time to interrupt */
for (i = 0; i < 10; i++) bigarray[i]++;

self->value = store + incval;

return (PyObject*)self;
}

static PyObject* toint(AtomicCounterObject* self) {
return PyInt_FromLong(self->value);
}

static PyNumberMethods ac_as_number = {
0, /*nb_add*/
0, /*nb_subtract*/
0, /*nb_multiply*/
0, /*nb_divide*/
0, /*nb_remainder*/
0, /*nb_divmod*/
0, /*nb_power*/
0, /*nb_negative*/
0, /*nb_positive*/
0, /*nb_absolute*/
0, /*nb_nonzero*/
0, /*nb_invert*/
0, /*nb_lshift*/
0, /*nb_rshift*/
0, /*nb_and*/
0, /*nb_xor*/
0, /*nb_or*/
0, /*nb_coerce*/
(unaryfunc)toint,  /*nb_int*/
0, /*nb_long*/
0, /*nb_float*/
0, /*nb_oct*/
0, /*nb_hex*/
(binaryfunc)iadd,  /*nb_inplace_add*/
0, /*nb_inplace_subtract*/
0, /*nb_inplace_multiply*/
0, /*nb_inplace_divide*/
0, /*nb_inplace_remainder*/
0, /*nb_inplace_power*/
0, /*nb_inplace_lshift*/
0, /*nb_inplace_rshift*/
0, /*nb_inplace_and*/
0, /*nb_inplace_xor*/
0, /*nb_inplace_or*/
0, /* nb_floor_divide */
0, /* nb_true_divide */
0, /* nb_inplace_floor_divide */
0, /* nb_inplace_true_divide */
0, /* nb_index */
};

static PyTypeObject AtomicCounterType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"AtomicCounter",   /*tp_name*/
sizeof(AtomicCounterObject),   /*tp_basicsize*/
0, /*tp_itemsize*/
0, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
&ac_as_number, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/

Re: Image grab in Python

2008-05-04 Thread Valerio Valerio
2008/5/4 David <[EMAIL PROTECTED]>:

> > What I want is display a window with a image, the user select a region
> of
> > the image, and the region value is passed to my program, my program
> slice
> > the image region, and analyze it.
> >
>
> If it's your apps own window, then getting a rectangle selected by the
> user is simple.
>
> 1) Make skeleton x/gtk/wx/qt/etc app
> 2) Add code to show an image
> 3) Add code to capture mouse events
> 4) Add code to show the 'rubber band' rectangle over the mouse selection
> area
> 5) When the user releases the mouse, then extract the part of the
> rectangle between where a mouse button was clicked and where it was
> released.
>
> All this would be in the docs for the Python x/gtk/wx/qt/etc libs.
> Where is the difficulty?


Right now I don't have any difficulty :)
I just asked for a library to do that, if exist I don't have to write all
the things from scratch (the selection part), but I am able to do my tasks
easily with Xlib or Pygame.

Thanks for the help.

Cheers,

-- 
Valério Valério

http://www.valeriovalerio.org


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

Re: HTTP Error code Info

2008-05-04 Thread David
> block.  Is there any better way to get the HTTP Error code using urllib2
> lib. Something like know the  exact  response number (200, 404 etc) without
> the above block.

Python libraries usually throw exceptions to indicate error conditions.

If this is a problem in your app then can write a wrapper function
which captures the HTTPError exception, extracts the code, and returns
it to the caller.

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


Re: is +=1 thread safe

2008-05-04 Thread Carl Banks
On May 4, 2:13 pm, Carl Banks <[EMAIL PROTECTED]> wrote:
> However, what I said was not wholly untrue: code in C extensions is
> protected by the GIL and thus not interruptable, unless it either
> releases the GIL, or calls back into Python code (which is apparently
> what numpy scalars do).

And, because I know that someone will be nitpicky: yes, technically it
is interruptable at that point, but other Python threads trying to
execute the same piece of code are not allowed to run.


> print 'final count:', count

This should be "int(count)".


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


Re: Real Time Midi File Playback - Reading and Writing midi at the same time

2008-05-04 Thread Gilly
On May 4, 9:14 pm, David <[EMAIL PROTECTED]> wrote:
> On Sun, May 4, 2008 at 7:11 PM, Gilly <[EMAIL PROTECTED]> wrote:
> > Hi
> >  I am trying to create an application that uses some form of input to
> >  create a midi file.
> >  I would like for this to be a 'real time' process. In other words, I
> >  want to be able to begin playing the midi file before I finish writing
> >  it, and continue writing as it plays.
>
> Have you tried the MIDI libraries listed on this page?
>
> http://wiki.python.org/moin/PythonInMusic
>
> David.

Yes. I haven't found anything that would help me out...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Determine socket family at runtime

2008-05-04 Thread Giampaolo Rodola'
On 4 Mag, 19:18, Francesco Bochicchio <[EMAIL PROTECTED]> wrote:
> On Sun, 04 May 2008 08:49:55 -0700, Giampaolo Rodola' wrote:
> > Hi there,
> > since the socket.socket.family attribute has been introduced only in
> > Python 2.5 and I need to have my application to be backward compatible
> > with Python 2.3 and 2.4 I'd like to know how could I determine the
> > family of a socket.socket instance which may be AF_INET or AF_INET6.
> > Is there some kind of getsockopt() directive I could use?
> > For now I've been able to determine the family by using:
>
> > # self.socket = a connected socket.socket instance
> > ip, port = self.socket.getsockname()[0:2]
> > af = socket.getaddrinfo(ip, port)[0][0]
>
> > ...but I'd like to know if some other solution is preferable.
>
> > Thanks.
>
> > --- Giampaolo
> >http://code.google.com/p/pyftpdlib
>
> Ciao,
>
> what about wrapping the socket type and  adding a 'family' attribute
> to the base socket class? Something like:
>
> class SocketWrapper(socket.socket):    def __init__(self, family, type, 
> proto=0):             socket.socket.__init__(self, family, type, proto)       
>        self.family = family
>
> then you have just to create the sockets with SocketWrapper insetead of
> socket.socket. For the rest of your code it would not matter, and then you
> are sure to always have a .family attribute.
>
> Ciao
> 
> FB- Nascondi testo tra virgolette -
>
> - Mostra testo tra virgolette -

Ciao,
I don't think that it would be possible since the socket I'm talking
about is returned by socket.socket.accept().


--- Giampaolo
http://code.google.com/p/pyftpdlib
--
http://mail.python.org/mailman/listinfo/python-list


Re: is +=1 thread safe

2008-05-04 Thread Gary Herron

Carl Banks wrote:

On May 4, 12:03 pm, Gary Herron <[EMAIL PROTECTED]> wrote:
  

Alexander Schmolck wrote:


Gary Herron <[EMAIL PROTECTED]> writes:
  

But... It's not!

A simple test shows that.   I've attached a tiny test program that shows this

extremely clearly.  Please run it and watch it fail.


In [7]: run ~/tmp/t.py
final count: 200
  should be: 200
  
(I took the liberty to correct your test to actually do what I said, namely

use a numpy.array; just replace ``count = 0`` with ``import numpy; count =
numpy.array(0)``).
  

The test was meant to simulate the OP's problem, but even with your
suggestion of using numpy, it *still* fails!




Ok, so numpy scalars don't support += atomically.  Thank you for your
skepticism in discovering this.
  


You're welcome.

However, what I said was not wholly untrue: code in C extensions is
protected by the GIL and thus not interruptable, unless it either
releases the GIL, or calls back into Python code (which is apparently
what numpy scalars do).
  


Yikes.   Thanks for that.

However, the upshot of all of this is that one must maintain extreme 
skepticism.   Unless you know both your Python code and any extension 
modules you call, and you know them at a level necessary to find such 
details, you must conclude that any operation, no matter how atomic it 
my look, may in fact not be atomic. 


Gary Herron



To illustrate, a small extension module is included below.  Just to
make things interesting, I added a long busy loop in between reading
and setting the counter, just to give the other thread maximum
opportunity to cut in.

If you were to compile it and run the test, you would find that it
works perfectly.


===
/* atomic.c */

#include 
#include 

typedef struct {
PyObject_HEAD
long value;
} AtomicCounterObject;

static int init(AtomicCounterObject* self, PyObject* args,
  PyObject* kwargs) {
self->value = 0;
return 0;
}

static PyObject* iadd(AtomicCounterObject* self, PyObject* inc) {
long incval = PyInt_AsLong(inc);
long store, i;
static int bigarray[10];
if (incval == -1 && PyErr_Occurred()) return 0;

store = self->value;

/* Give the thread plenty of time to interrupt */
for (i = 0; i < 10; i++) bigarray[i]++;

self->value = store + incval;

return (PyObject*)self;
}

static PyObject* toint(AtomicCounterObject* self) {
return PyInt_FromLong(self->value);
}

static PyNumberMethods ac_as_number = {
0, /*nb_add*/
0, /*nb_subtract*/
0, /*nb_multiply*/
0, /*nb_divide*/
0, /*nb_remainder*/
0, /*nb_divmod*/
0, /*nb_power*/
0, /*nb_negative*/
0, /*nb_positive*/
0, /*nb_absolute*/
0, /*nb_nonzero*/
0, /*nb_invert*/
0, /*nb_lshift*/
0, /*nb_rshift*/
0, /*nb_and*/
0, /*nb_xor*/
0, /*nb_or*/
0, /*nb_coerce*/
(unaryfunc)toint,  /*nb_int*/
0, /*nb_long*/
0, /*nb_float*/
0, /*nb_oct*/
0, /*nb_hex*/
(binaryfunc)iadd,  /*nb_inplace_add*/
0, /*nb_inplace_subtract*/
0, /*nb_inplace_multiply*/
0, /*nb_inplace_divide*/
0, /*nb_inplace_remainder*/
0, /*nb_inplace_power*/
0, /*nb_inplace_lshift*/
0, /*nb_inplace_rshift*/
0, /*nb_inplace_and*/
0, /*nb_inplace_xor*/
0, /*nb_inplace_or*/
0, /* nb_floor_divide */
0, /* nb_true_divide */
0, /* nb_inplace_floor_divide */
0, /* nb_inplace_true_divide */
0, /* nb_index */
};

static PyTypeObject AtomicCounterType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"AtomicCounter",   /*tp_name*/
sizeof(AtomicCounterObject),   /*tp_basicsize*/
0, /*tp_itemsize*/
0, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/

Re: Real Time Midi File Playback - Reading and Writing midi at the same time

2008-05-04 Thread Diez B. Roggisch

Gilly schrieb:

On May 4, 9:14 pm, David <[EMAIL PROTECTED]> wrote:

On Sun, May 4, 2008 at 7:11 PM, Gilly <[EMAIL PROTECTED]> wrote:

Hi
 I am trying to create an application that uses some form of input to
 create a midi file.
 I would like for this to be a 'real time' process. In other words, I
 want to be able to begin playing the midi file before I finish writing
 it, and continue writing as it plays.

Have you tried the MIDI libraries listed on this page?

http://wiki.python.org/moin/PythonInMusic

David.


Yes. I haven't found anything that would help me out...


You didn't provide enough information. who is consuming the midi-files 
for example.


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


Re: Script Optimization

2008-05-04 Thread lev
> * Remove newlines introduced by email
> * Move imports to start of file
used imports of the edited script you sent.
> * Change indentation from 8 spaces to 4
I like using tabs because of the text editor I use, the script at
the end is with 4 though.
> * Move main() to bottom of script
> * Remove useless "pass" and "return" lines
I replaced the return nothing lines with passes, but I like
keeping them in case the indentation is ever lost - makes it easy to
go back to original indentation
> * Temporarily change broken "chdir" line
removed as many instances of chdir as possible (a few useless ones
to accomodate the functions - changed functions to not chdir as much),
that line seems to work... I made it in case the script is launched
with say: 'python somedir\someotherdir\script.py' rather than 'python
script.py', because I need it to work in it's own and parent
directory.
> * Split lines so they fit into 80 chars
> * Add spaces after commas
> * Use path.join instead of string interpolation
in all cases when possible - done
> * rename rename() to rename_md5() because rename() shadows a function
> imported from os.
renamed all functions to more understandable names (without
collisions)
> * Rename vars shadowing imported names
renamed almost all vars to more understandable names
> * Improve logic for checking when to print help
the example you gave me does pretty much the exact same thing as
before... (the options are either false or true depending on if the
argument was used, if false for both then no logic was done and help
is shown, which would be exactly the same if the did_something var
remained false.
> * Create emtpy md5 listing file if one doesn't exist
I intended it to be a script to help ripping a specific mp3cd to
disk, not necessarily create checksum files, because i intend to
include the checksums file.
> * Add a comment for a dodgy-looking section
The 4 folders to be renamed are intentional (this is for a
specific mp3cd with 4 album folders)

I added comments to explain what I was doing with the dictionary[x][1]
[1][0], and also what the indexes for the strings are used for ([3:]
to remove the 001 in 001Track.mp3, etc.)


Thanks for the advice so far,
lev

#!/usr/bin/env python

import md5
from glob import glob
from optparse import OptionParser
from os import chdir, path, rename, remove
from sys import argv, exit

def verify_checksum_set(checksums):
checksums = open(checksums, 'r')
changed_files = {}
missing_files = []
for fline in checksums.readlines():
line = fline.split(' *')
original_sum = line[0].upper()
try:
new_sum = calculate_checksum(line[1].strip())
if  new_sum == original_sum:
print '.',
pass
else:
changed_files[line[1]] = (original_sum, new_sum)
pass
except IOError:
missing_files.append(line[1])
pass
pass
checksums.close()
changed_files_keys = changed_files.keys()
changed_files_keys.sort()
missing_files.sort()
print '\n'
if len(changed_files) != 0:
print 'File(s) changed:'
for key in changed_files_keys:
print key.strip('\n'), 'changed from:\n\t',
changed_files[key][0], \
'to\n\t', changed_files[key][1]
pass
print '\n\t', len(changed_files), 'file(s) changed.\n'
pass
if len(missing_files) != 0:
print 'File(s) not found:'
for x in range(len(missing_files)):
print '\t', missing_files[x]
pass
print '\n\t', len(missing_files), 'file(s) not found.\n'
pass
if not len(changed_files) and not len(missing_files):
print "\n\tChecksums Verified\n"
pass
pass

def calculate_checksum(file_name):
file_to_check = open(file_name, 'rb')
chunk = 8196
checksum = md5.new()
while (True):
chunkdata = file_to_check.read(chunk)
if not chunkdata:
break
checksum.update(chunkdata)
pass
file_to_check.close()
return checksum.hexdigest().upper()

def rename_file_set(new_dir_names, checksums):
file_info = md5format(checksums)
dirlist = glob('00[1-4]Volume [1-4]')
dirlist.sort()
for x in range(4):
rename(dirlist[x], new_dir_names[x])
print '\t', dirlist[x], 'renamed to:', new_dir_names[x]
chdir(new_dir_names[x])
for old_file_name in glob ('*.mp3'):
# old_file_name[3:] is part of removing numbering:
'001Track ...'
new_file_name = old_file_name[3:]
rename(old_file_name, new_file_name)
print '\t\t', old_file_name, 'renamed to:', new_file_name
pass
chdir('..')
file_info = md5file_name_edit(file_info,dirlist[x],
new_dir_names[x])
pass
md5write(file_info, checksums)
replace_strings('The American Century.htm', dirlis

Re: Real Time Midi File Playback - Reading and Writing midi at the same time

2008-05-04 Thread Max M

Gilly skrev:

Hi
I am trying to create an application that uses some form of input to
create a midi file.
I would like for this to be a 'real time' process. In other words, I
want to be able to begin playing the midi file before I finish writing
it, and continue writing as it plays.



Perhaps csound can help with this. It has a lot of midi, realtime and 
python stuff.



--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science

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



Re: is +=1 thread safe

2008-05-04 Thread Aahz
In article <[EMAIL PROTECTED]>,
Gary Herron  <[EMAIL PROTECTED]> wrote:
>
>However, the upshot of all of this is that one must maintain extreme 
>skepticism.   Unless you know both your Python code and any extension 
>modules you call, and you know them at a level necessary to find such 
>details, you must conclude that any operation, no matter how atomic it 
>my look, may in fact not be atomic. 

Absolutely.  Note that your statement is insufficiently encompassing:
any Python library code might easily use classes that define special
methods that cause GIL release.

This is why those of us who champion correct use of threads say that the
only easy way to create working threaded programs is to pass objects
around in Queues and never never never use an object in multiple threads.
-- 
Aahz ([EMAIL PROTECTED])   <*> http://www.pythoncraft.com/

Help a hearing-impaired person: http://rule6.info/hearing.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: ISBN Barecode reader in Python?

2008-05-04 Thread Nick Craig-Wood
Joseph <[EMAIL PROTECTED]> wrote:
>  All: I have written a program to query Amazon with ISBN and get the
>  book details. I would like to extend so that I can read ISBN from the
>  barcode (I will take a photo of the same using webcam or mobile). Are
>  there any opensource/free SDK doing the same? As it is a hobby
>  project, I don't like to spend money on the SDK.

Pick yourself up a cue-cat barcode reader, eg from here or ebay

  http://www.librarything.com/cuecat

These appear as a keyboard and "type" the barcode in to your program.

Cheap and effective.

-- 
Nick Craig-Wood <[EMAIL PROTECTED]> -- http://www.craig-wood.com/nick
--
http://mail.python.org/mailman/listinfo/python-list


Re: Real Time Midi File Playback - Reading and Writing midi at the same time

2008-05-04 Thread Chuckk Hubbard
Threading was recommended to me as a way to time things:
http://docs.python.org/lib/timer-objects.html
Dunno if that helps you.
-Chuckk

On Sun, May 4, 2008 at 8:11 PM, Gilly <[EMAIL PROTECTED]> wrote:
> Hi
>  I am trying to create an application that uses some form of input to
>  create a midi file.
>  I would like for this to be a 'real time' process. In other words, I
>  want to be able to begin playing the midi file before I finish writing
>  it, and continue writing as it plays.
>
>  I would really appreciate any help possible on this matter.
>
>  Thanks!!
>  --
>  http://mail.python.org/mailman/listinfo/python-list
>



-- 
http://www.badmuthahubbard.com
--
http://mail.python.org/mailman/listinfo/python-list


pygame.mixer.load not working

2008-05-04 Thread globalrev
import pygame

pygame.mixer.music.load(example1.mp3)
pygame.mixer.music.play(loops=1, start=0.0)


Traceback (most recent call last):
  File "C:\Python25\myPrograms\pygameProgs\musicexp.py", line 3, in

pygame.mixer.music.load(example1.mp3)
NameError: name 'example1' is not defined

example1 is in the same directory as the script
--
http://mail.python.org/mailman/listinfo/python-list


Re: Light slices + COW

2008-05-04 Thread castironpi
On May 4, 7:49 am, David <[EMAIL PROTECTED]> wrote:
> >  > D compiles to efficient machine code so Python is at a disadvantage
> >  > even if you use the same syntax (see my first example). You can make
> >  > the Python version faster, but beware of premature optimization.
>
> >  This time I don't agree with this "premature optimization" thing. My
> >  original Python version is just 5 lines long, it's readable enough,
> >  and it's a part of a large library of mine of similar functions, they
> >  must be fast because I use them all the time as building blocks in
> >  programs.
>
> Have you looked into the 'numpy' libraries? Those have highly
> optimized array/numeric processing functions. Also the have a concept
> of getting 'views' when you slice, rather than a full copy.
>
> I also suggest benchmarking your apps which use your libs and see
> where the bottlenecks are. Then you can move those to external
> compiled modules (c/pyrex/cython/shedskin/etc). You can keep your
> slower Python version around as a reference implementation.
>
> >  > What I'dlike to see is a rope[1] module for Python.
>
> >  People have already suggested it, and there's even an implementation
> >  to replace Python strings. It was refused because... I don't know why,
> >  maybe its implementation was too much more complex than the current
> >  one, and it wasn't faster in all situations (and I think Guido wants
> >  data structures that try to replace the basic built-in ones to be
> >  faster in all situations and user-transparent too).
>
> I'd be happy if there was a separate 'rope' class that you could wrap
> arbitrary long sequences in when you need to (the same way that STL
> has it separate to the string class, even though the string class has
> a lot of other optimizations (internal ref counts, etc, to avoid
> unnecessary copies)). Do you know one?
>
> David.

Persistence on the rope class might be trivial; that is, a constant
number of disk modifications be made per state modification.
--
http://mail.python.org/mailman/listinfo/python-list


Re: using sqlite3 - execute vs. executemany; committing ...

2008-05-04 Thread Vlastimil Brom
>
>
> Thank you very much for your comprehensive answer and detailed
informations, David; I really appreciate it!

As for the number of items, there would be approx. 34 000 calls of execute()
in my present code, in the
final version probably more; I think executmany is more efficient
here, if there aren't any drawbacks.
You are right, I should definitely use the standard approaches also usable
in other databases, rather than relying on the specific behaviour of sqlite3
(althought for the current task - an embeded, lightweight, in memory db,
sqlite seems to be an obvious choice).

Thanks for your comments on the concept,that's exactly, what I needed;
actually my
current version using sqlite3 is a reimplementation; the previous
script used nested dicts (as
the most suitable built-in data structure, I knew
of). In this older version the lookups for a tag
values given the text index were ok (as the indices are keys in the main
dict), however the other way (retrieving the index given a certain
combination of tag values)
is much harder and requires nested loops over the entire data on each query (or
alternatively multiple parallel dicts "keyed" on the available tag values).
I thought a database would perform much better in such cases (and the
preliminary tests confirm this); however now I see the possible risks too.
I'll look into other possible approaches. There will be clearly a
finite set of the
column names, hence it is possible to define the db at the beginning
and fill it with values only
after parsing the texts; the problem is, this can't be done untill the
texts are available, I just thought, a more generic
approach would be be more easily extensible and also a bit simpler.

As for the usage of the db in my
app, everything is simple, single threaded,  the parsing the text to get the
tags with their values are done in the program itself, hence it can be
controlled, as well as the text sources themselves; furthermore, there won't
be any explicit user queries of
SQL, all of them are programmatic - e.g. a text is displayed on the
screen and the information about the given line should
be retrieved and displayed (like chapter, verse
nr.) or vice versa - the user selects some property (e.g. beginning of
a certain chapter) and the text should be
scrolled to make the corresponding position visible.

Just for information, what are valid table and
column names in sqlite? (Unfortunately, I couldn't find any reference for
that (maybe except the reserved sqlite_master);  as quoted names, everything
I tried, worked
fine, also whitespace, various unicode characters etc.; of course, I
can imagine, that e.g. supplying a quote would
cause problems ... )

Thanks once more for your help,
  Vlasta
--
http://mail.python.org/mailman/listinfo/python-list

unicode newbie - printing mixed languages to the terminal

2008-05-04 Thread David
Hi list.

I've never used unicode in a Python script before, but I need to now.
I'm not sure where to start. I'm hoping that a kind soul can help me
out here.

My current (almost non-existant) knowledge of unicode:

>From the docs I know about the unicode string type, and how to declare
string types. What I don't understand yet is what encodings are and
when you'd want/need to use them. What I'd like is to just be able to
print out unicode strings in mixed languages, and they'd appear on the
terminal the same way they get shown in a web browser (when you have
appropriate fonts installed), without any fuss.

Here is an example of how I'd like my script to work:

$ ./test.py

Random hiragana: 
Random romaji: kakikukeko

Is this possible?

>From my limited knowledge, I *think* I need to do to things:

1) In my Python script, run .encode() on my unicode variable before
printing it out (I assume I need to encode into Japanese)

Question: How does this work when you have multiple languages in a
single unicode string? Do you need to separate them into separate
strings (one per language) and print them separately?

Or is it the case that you can (unlike a web browser) *only*
display/print one language at a time? (I really want mixed language -
English AND Japanese).

2) Setup the terminal to display the output. From various online docs
it looks like I need to set the LANG environment variable to Japanese,
and then start konsole (or gnome-terminal if that will work better).
But again, it looks like this limits me to 1 language.

If what I want to do is very hard, I'll output html instead and view
it in a web browser. But I'd prefer to use the terminal instead if
possible :-)

Thanks in advance.

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


Re: Real Time Midi File Playback - Reading and Writing midi at the same time

2008-05-04 Thread Ken Starks

Gilly wrote:

Hi
I am trying to create an application that uses some form of input to
create a midi file.
I would like for this to be a 'real time' process. In other words, I
want to be able to begin playing the midi file before I finish writing
it, and continue writing as it plays.

I would really appreciate any help possible on this matter.

Thanks!!

Not python, but have you come across XMidi (http://www.palserv.com/XMidi/) ?

It is used in the Apache Cocoon project, which has a
Midi 'block'.
This allows you to go from an XML file, or other
source, to XMidi (an XML version of MIDI), and then
to Quicktime, which you can listen to in your browser.

I'm afraid I don't know whether the source can be
streaming XML or whether you have to reach the end
of the XML before it starts to play.

If you can use streaming XML, you should be able to
generate it from python. Foursuite has a streaming
XML class, for example.



A quick synopsis on the cocoon site says:

What is the MIDI block?

The MIDI block currently gives you an XMidiGenerator to generate an XML 
representation of any MIDI file (called XMidi by its author Peter Loeb). 
There is also the XMidiSerializer to render XMidi back as a MIDI file. I 
have used XSLT to provide some basic musical manipulations such as 
transposition, and inversion. Retrograde is harder, but I shall see what 
I can come up with. Hopefully I shall also add some transformers to 
generate SVG visualisations of the XMidi, starting with normal western 
musical notation.

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


Re: pygame.mixer.load not working

2008-05-04 Thread Diez B. Roggisch

globalrev schrieb:

import pygame

pygame.mixer.music.load(example1.mp3)
pygame.mixer.music.play(loops=1, start=0.0)


Traceback (most recent call last):
  File "C:\Python25\myPrograms\pygameProgs\musicexp.py", line 3, in

pygame.mixer.music.load(example1.mp3)
NameError: name 'example1' is not defined

example1 is in the same directory as the script


http://docs.python.org/tut/node5.html#SECTION00512

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


Re: unicode newbie - printing mixed languages to the terminal

2008-05-04 Thread Carsten Haese

David wrote:

Hi list.

I've never used unicode in a Python script before, but I need to now.
I'm not sure where to start. I'm hoping that a kind soul can help me
out here.

My current (almost non-existant) knowledge of unicode:


From the docs I know about the unicode string type, and how to declare

string types. What I don't understand yet is what encodings are and
when you'd want/need to use them. What I'd like is to just be able to
print out unicode strings in mixed languages, and they'd appear on the
terminal the same way they get shown in a web browser (when you have
appropriate fonts installed), without any fuss.

Here is an example of how I'd like my script to work:

$ ./test.py

Random hiragana: 
Random romaji: kakikukeko

Is this possible?


From my limited knowledge, I *think* I need to do to things:


1) In my Python script, run .encode() on my unicode variable before
printing it out (I assume I need to encode into Japanese)

Question: How does this work when you have multiple languages in a
single unicode string? Do you need to separate them into separate
strings (one per language) and print them separately?

Or is it the case that you can (unlike a web browser) *only*
display/print one language at a time? (I really want mixed language -
English AND Japanese).

2) Setup the terminal to display the output. From various online docs
it looks like I need to set the LANG environment variable to Japanese,
and then start konsole (or gnome-terminal if that will work better).
But again, it looks like this limits me to 1 language.

If what I want to do is very hard, I'll output html instead and view
it in a web browser. But I'd prefer to use the terminal instead if
possible :-)


I suggest you read http://www.amk.ca/python/howto/unicode to demystify 
what Unicode is and does, and how to use it in Python.


Printing text from different languages is possible if and only if the 
output device (terminal, in this case) supports a character encoding 
that accommodates all the characters you wish to print. UTF-8 is a 
fairly ubiquitous candidate that fits that criteria, since it 
encompasses Unicode in its entirety (as opposed to latin-1, for example, 
which only includes a very small subset of Unicode).


HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
--
http://mail.python.org/mailman/listinfo/python-list


Can I install Python 2.4 and 2.5 ?

2008-05-04 Thread adolfo
I am reviewing various visualization programs (Scipy, PYGNL, etc) and
IDE´s. Some run on Python 2.4 others in 2.5.

Can I have both installed at the same time if I don´t run them
concurrently?

Now in Windows XP soon on Ubuntu 8

Appreciating your help,

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


Re: Can I install Python 2.4 and 2.5 ?

2008-05-04 Thread Diez B. Roggisch

adolfo schrieb:

I am reviewing various visualization programs (Scipy, PYGNL, etc) and
IDE´s. Some run on Python 2.4 others in 2.5.

Can I have both installed at the same time if I don´t run them
concurrently?

Now in Windows XP soon on Ubuntu 8

Appreciating your help,


Yes you can. However you need to install 3rd-party-modules separately 
for each version - e.g. scipy - if you want to use them with both.


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


Re: Please help - Tkinter not doing anything

2008-05-04 Thread rynt
WxWidgets, Tkinter, PyQT are all cross platform.  Also have a look at
http://wiki.python.org/moin/GuiProgramming
for more GUI frameworks.

RCB

>On May 4, 4:59 am, Protected <[EMAIL PROTECTED]> wrote:
> On May 4, 12:18 pm, [EMAIL PROTECTED] wrote:
>
>
>
>
>
> > On May 4, 5:22 am, Protected <[EMAIL PROTECTED]> wrote:
>
> > > I had previously ran the import line. I prepended it to the example
> > > code I'm trying to run every time but it did not help, still nothing
> > > happens. With or without var before 'root'. I'm pasting the code in
> > > IDLE and using Windows XP as written in the first post.
>
> > Tkinter doesn't work if you type the statements in IDLE. I don't
> > remember the specifics of it, but essentially it doesn't work because
> > IDLE is itself a Tkinter app. You have to type it at the Python
> > command line or save it in a file. BTW, if you're on Windows, you
> > should definitely check wxPython. I used to work with Tkinter,
> > struggling with it all the time only to get a lame result most of the
> > time. Then I switched to wxPython, and in the same week I was learning
> > it I did a better GUI than I ever did in Tkinter (with months of
> > work!). I feel it makes it easier to make your program have a better
> > structure and design, and thus lets you focus on the actual task of
> > the program, rather than in the GUI itself. Plus, on Windows, you'll
> > get the widgets to look more natively, like any good quality
> > application there is on Windows.
>
> Ah, but is it cross platform?
>
> (Thanks for letting me know about IDLE.)- Hide quoted text -
>
> - Show quoted text -

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


Re: Can I install Python 2.4 and 2.5 ?

2008-05-04 Thread Chuckk Hubbard
I have both python2.4 and python2.5 as executables, and "python" as a
symbolic link to python2.4.  This is the default setup if you install
both from the Debian repositories, so probably with Ubuntu as well.
If you like you can change "python" to point to either one (or to any
other program, if you really want).  I don't have my win install in
front of me at the moment, but I know I do have both there as well.
-Chuckk

On Mon, May 5, 2008 at 1:01 AM, adolfo <[EMAIL PROTECTED]> wrote:
> I am reviewing various visualization programs (Scipy, PYGNL, etc) and
>  IDE´s. Some run on Python 2.4 others in 2.5.
>
>  Can I have both installed at the same time if I don´t run them
>  concurrently?
>
>  Now in Windows XP soon on Ubuntu 8
>
>  Appreciating your help,
>
>  Adolfo
>  --
>  http://mail.python.org/mailman/listinfo/python-list
>



-- 
http://www.badmuthahubbard.com
--
http://mail.python.org/mailman/listinfo/python-list

Re: using sqlite3 - execute vs. executemany; committing ...

2008-05-04 Thread David
> >
> Thank you very much for your comprehensive answer and detailed informations,
> David; I really appreciate it!

You're welcome.

>
> As for the number of items, there would be approx. 34 000 calls of execute()
> in my present code, in the final version probably more; I think executmany
> is more efficient here, if there aren't any drawbacks.

executemany is probably a good idea here. If memory becomes a problem
at some point (eg: millions of lines) you'll probably want to use an
on-disk database (I suggest postgresql), and read in batches of say
1000, which you save to the database with a single executemany.

>  I thought a database would perform much better in such cases (and the
> preliminary tests confirm this);

It sounds like your Python code has some serious (algorithm or data
structure) problems. Python code (with appropriate dictionaries,
caches, memoization etc) should always be able to perform better than
an in-memory SQL database (even optimized with indexes, etc). You
definitely need to check that. You don't even create any db indexes
(in the code you gave), but sqllite still performs better than your
structs which do use dicts!

Basically what you're saying is that (the equivalent, but with SQL
overhead) of a set of lists containing tuples, iwthout any dicts for
lookup, performs better than your original sructure.

Unless you actually need a database (too much data for memory, or you
need persistant data, possibly for sharing between apps) I suggest
sticking to Python structures, and optimizing your structures and
look-up algorithms.

One reasonable reason for you to use a database would be if
maintaining your dicts (for fast lookup) became too complicated and
you wanted the DBM to keep indexes automatically updated for you.

> however now I see the possible risks too.
> I'll look into other possible approaches. There will be clearly a finite set
> of the column names, hence it is possible to define the db at the beginning
> and fill it with values only after parsing the texts; the problem is, this
> can't be done untill the texts are available, I just thought, a more generic
> approach would be be more easily extensible and also a bit simpler.

If you don't know in advance what the fields are, then how does your
python app work? Are all queries in the user interface (that refer to
this database) all arbitrary and initiated by a human?

Also, how do you setup foreign relationships between tables, and
indexes (for performance), if you don't know what fields are going to
be present?

Maybe this would be more clear (to me) if you gave a short example of
the data, with a note or two to explain where there are performance
problems.

> the parsing the text to get the tags with their values are done in the
> program itself

What kind of tags? From your description it sounds like you have
markers inside the text (basically bookmarks with arbitrary metadata,
hence your need for dynamic schema), which the user can query, so they
can quickly jump between parts of the text, based on their search
results. Is this about right?

> Just for information, what are valid table and column names in sqlite?
> (Unfortunately, I couldn't find any reference for that (maybe except the
> reserved sqlite_master);  as quoted names, everything I tried, worked fine,
> also whitespace, various unicode characters etc.; of course, I can imagine,

I'm not sure about that.

If you want to be safe:

1) Add a prefix to your table and field names
2) 'normalise' all table and field names (eg: convert to lower case,
remove non-alphabetic characters, etc).
3) Make sure that you don't get the same 'normalised' name for 2
different incoming strings.
4) Be prepared to scrap the schema-generating approach if your app's
database requirements change (eg: you need to share the db with other
apps which have their own tables that you don't want to stomp over).

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


Re: word shifts

2008-05-04 Thread Gabriel Genellina
En Sun, 04 May 2008 03:35:05 -0300, George Sakkis <[EMAIL PROTECTED]> escribió:
> On May 4, 2:04 am, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
>> En Sun, 04 May 2008 02:17:07 -0300, dave <[EMAIL PROTECTED]> escribió:
>>
>> > I made a function that takes a word list (one word per line, text file)
>> > and searches for all the words in the list that are 'shifts' of
>> > eachother.  'abc' shifted 1 is 'bcd'
>>
>> But I'd use a different algorithm. Instead of generating all posible shifts 
>> for a given word, I'd substract the newly read word from each previous words 
>> (here, "substract two words" means substract the character code for 
>> corresponding positions, modulo 26). Shifted words, when substracted, have 
>> the same number on all positions.
>
> A faster algorithm is to create a 'key' for each word, defined as the
> tuple of ord differences (modulo 26) of consecutive characters. E.g.
> the key of 'acf' is (2,3); 'c' is 2 positions after 'a' and 'f' 3
> positions after 'c'. Shifted words (and only these) have the same key.

Much better! I like it.

-- 
Gabriel Genellina

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


Re: Please help - Tkinter not doing anything

2008-05-04 Thread Scott David Daniels

[EMAIL PROTECTED] wrote:

On May 4, 5:22 am, Protected <[EMAIL PROTECTED]> wrote:

 I'm pasting the code in IDLE and using Windows XP


Tkinter doesn't work if you type the statements in IDLE it doesn't 

> work because IDLE is itself a Tkinter app.

Actually, _because_ IDLE is a Tkinter app,you can use it to
experiment with Tkinter in a very useful way.  The trick is that
you need to start idle with the "-n" (No subprocesses) switch.
This is so useful that I build a shortcut on my desktop to do exactly
this.  Using this shortcut you can see the effect of each action as
you type it, giving you an idea of exactly what must happen.

I'm assuming you are using Python 2.5 here (other versions have
related recipes, but slightly more complicated).
Right click on the desktop, and choose "New Shortcut."

For "Choose the location of the item", browse to (or type in)
C:\Python25\pythonw.exe
Then (after your next) pick a name for your shortcut.
Right click on the resulting shortcut, and go to "properties"
Change the "Target" entry from:
C:\Python25\pythonw.exe
to:
C:\Python25\pythonw.exe -m idlelib.idle -n
Also, if you are like me, you'll want to change the "Start in" directory
to wherever you work on python code --so imports of your own stuff "just
work", and so "File open'" and "Save As" default to a "nice" directory.

The Idle you get with this "corrupts" more easily, since the "-n" says
"no subprocess."  Things like restarting the shell don't work.  _But_
Tkinter stuff is available (there is already a running mainloop).  You
can interactively do simple things a step at a time and watch the
results.  It is a _great_ way to experiment with Tkinter.


--Scott David Daniels
[EMAIL PROTECTED]

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


Python Poker

2008-05-04 Thread Chunky
hey Jeff Sandys sandysj at juno.com 

did you manage to get your game up for download it sounds really interesting i 
love "home made games"
--
http://mail.python.org/mailman/listinfo/python-list

Are rank noobs tolerated, here?

2008-05-04 Thread notbob

I'm trying to learn how to program.  I'm using:
  
How to Think Like a Computer Scientist

Learning with Python
2nd Edition

Am I likely to receive any help, here, or is there another irc, forum, etc,
that might better serve a complete amateur such as myself.  Thnx.

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


Re: generator functions in another language

2008-05-04 Thread Gabriel Genellina
En Sun, 04 May 2008 08:11:35 -0300, <[EMAIL PROTECTED]> escribió:

> On May 4, 12:21 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
> wrote:
>> En Sun, 04 May 2008 01:08:34 -0300, Marc 'BlackJack' Rintsch <[EMAIL 
>> PROTECTED]> escribió:
>>
>> > On Sat, 03 May 2008 16:39:43 -0700, castironpi wrote:
>>
>> >> I'm actually curious if there's a way to write a generator function
>> >> (not a generator expression) in C, or what the simplest way to do it
>> >> is... besides link the Python run-time.
>>
>> > The reference implementation of Python is written in C, so obviously there
>> > must be a way to write something like generators in C.
>>
>> Yes and no. Generators are tied to frames, and frames execute Python code, 
>> not C. There is no simple way to write generators in C, but there are some 
>> generator-like examples in the itertools module.
>> See this 
>> threadhttp://groups.google.com/group/comp.lang.python/browse_thread/thread/...
>
> Gabriel,
> How did your attempt turn out from last May?  At first look, it's
> outside the scope of Python, but it is not the scope of C
> necessarily.  Generators offer a lot of simplicity (which I haven't
> read about extensively, but am starting to see) that could gain some
> reputation for Python.  What is the midpoint at which C could meet
> Python?
>
> There is no such thing as a 'frame' per se in C; byte code is
> integral.  As there is no such thing as suspended state without
> frames, and no such thing as generators without suspended state.  It's
> a hard thing to Google for without knowing the prior terminology for
> the work that's already been done on them in C.  What work is there?
> Are any devs interested in pursuing it?

The frame and generator implementations are very tightly coupled to Python 
code, they aren't useful for implementing generators in C. Don't bother to read 
them.

Unfortunately, converting a C function into something like a generator isn't as 
easy as using the "yield" statement... Although the idea is simple, the 
implementation may be hard sometimes: You have to wrap the function within an 
object, maintain all state information into that object, and do all computation 
in the "next" method. Also, __iter__ should return itself so it can be called 
as an iterator.
(those objects are sometimes called "functors" 
 not the same meaning as functors 
in Mathematics)

All examples that I have at hand are propietary code so I can't post them. The 
itertools module may be used as reference - "cycle" and "chain" are the easiest 
I think, although they might be *too* easy to understand the structure. 
"groupby" is a more complex example but at the same time harder to understand. 
See http://svn.python.org/projects/python/trunk/Modules/itertoolsmodule.c

Ok, I'll try to use Python code as an example. A generator for Fibonacci 
numbers:

def fibo():
 a = b = 1
 while True:
 a, b = b, a+b
 yield b

We can convert that function into this object; it should be written in C, not 
Python, but the idea is the same:

class fibo:
 def __init__(self):
 self.a = 1
 self.b = 1
 def next(self):
 temp = self.a + self.b
 self.a = self.b
 self.b = temp
 return temp
 def __iter__(self):
 return self

It behaves exactly the same as the generator above; we can even use the same 
code to test it:

py> for n in fibo():
...   if n>100: break
...   print n
...
2
3
5
8
13
21
34
55
89

Converting that class into C code should be straightforward. And then you have 
a generator-like function written in C.

-- 
Gabriel Genellina

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


Re: Are rank noobs tolerated, here?

2008-05-04 Thread Glenn Hutchings
notbob <[EMAIL PROTECTED]> writes:
> Am I likely to receive any help, here, or is there another irc, forum, etc,
> that might better serve a complete amateur such as myself.  Thnx.

You're very likely to receive help here.  Or at the very least, people will
point you at the best place to get it.  Fire away!

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


Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-04 Thread Gabriel Genellina
En Sun, 04 May 2008 12:58:25 -0300, Duncan Booth <[EMAIL PROTECTED]> escribió:

> Szabolcs Horvát <[EMAIL PROTECTED]> wrote:
>
>> I thought that it would be very nice if the built-in sum() function used
>> this algorithm by default.  Has this been brought up before?  Would this
>> have any disadvantages (apart from a slight performance impact, but
>> Python is a high-level language anyway ...)?
>
> There's a thread you might find interesting:
>
> http://groups.google.com/group/comp.lang.python/browse_thread/thread/9eda29faf92f532e/027cef7d4429aa3a
>
> In that thread I suggested that Python ought to implement sum by adding
> together each pair of values, then each pair of results and so on. This
> means that for input values of a similar magnitude the intermediate results
> stay balanced. The implementation doesn't actually do all the first level

Python doesn't require __add__ to be associative, so this should not be used as 
a general sum replacement. But if you know that you're adding floating point 
numbers you can use whatever algorithm best fits you. Or use numpy arrays; I 
think they implement Kahan summation or a similar algorithm.

-- 
Gabriel Genellina

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


Re: Are rank noobs tolerated, here?

2008-05-04 Thread Patrick Mullen
There is also the python tutor list:

http://mail.python.org/mailman/listinfo/tutor

Which is more geared toward beginners.  Although I am subscribed to both
lists, and they are both matched by the same filter for me so I wont know
the difference...  But there may be people who are not subscribed to both
lists and don't care to listen to beginner discussions.  I wouldn't know.

There is python irc as well: #python on irc.freenode.net.  If you just have
a quick question you may be better off trying there first.
--
http://mail.python.org/mailman/listinfo/python-list

Numpy not found

2008-05-04 Thread adolfo


I downloaded and installed Phyton 2.52 (it works), numpy-1.0.4.win32-
py2.5, and scipy-0.6.0.win32-py2.5

I can´t get Numpy to show up at Python´s  IDLE, or command line. If I
do:

 import Numeric
# I get
 Traceback (most recent call last):
  File "", line 1, in 
import Numeric
ImportError: No module named Numeric

And if I do:
 import Numeric *
# I get
SyntaxError: invalid syntax

I restarted the machine and double clicked in the same file
numpy-1.0.4.win32-py2.5 again, it asked whether I wanted to reinstall
or repair, I chose repair. I tried the same commands again after this
but I got the same results.

Next, I went o the Python25 "site packages"  subdirectory and there I
found "setup.py" and "readme.txt" files. I did  run the setup with no
changes and the readme file suggested to run the setup.py scrip. No
changes.

Any help with this mater will be much appreciated,

Adolfo




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


Where are Tkinter event.type constants defined?

2008-05-04 Thread Noah
I'm trying to match against Event.type for KeyPress and ButtonPress.
Currently I'm using integer constants (2 and 4). Are these constants
defined anywhere? The docs talk about KeyPress and ButtonPress, but I
don't see them in any of the Tkinter source files. Are these just
magic values that come out of the TK side of things and are not
defined in Python? Code like this makes me think I'm doing something
wrong:

if event.type == 2:
handle_key_press (event.char)
elif event.type == 4:
do_something_for_button ()
else:
pass # unknown event

(I understand that usually you would bind these function so that they
are called as a callback.)

I don't mind defining the constants myself. I just want to make sure
that I'm not missing something already done for me. Does anyone happen
to have a complete list of Event.type constants?

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


Re: Are rank noobs tolerated, here?

2008-05-04 Thread CM
On May 4, 7:45 pm, notbob <[EMAIL PROTECTED]> wrote:
> I'm trying to learn how to program.  I'm using:
>
> How to Think Like a Computer Scientist
>
> Learning with Python
> 2nd Edition
>
> Am I likely to receive any help, here, or is there another irc, forum, etc,
> that might better serve a complete amateur such as myself.  Thnx.
>
> nb

Another good place to be aware of is the Python Tutor List, some great
people helping out there.  You can search the archive and/or subscribe
to
the list here:
http://www.nabble.com/Python---tutor-f2981.html

I've found learning from scratch is aided if you know what sorts of
things
you hope to do with programming and then work from within that, using
issues
germane to that area as building blocks.
--
http://mail.python.org/mailman/listinfo/python-list


Re: is +=1 thread safe

2008-05-04 Thread Alexander Schmolck
Gary Herron <[EMAIL PROTECTED]> writes:

> The test was meant to simulate the OP's problem, but even with your suggestion
> of using numpy, it *still* fails!  

Well, although I haven't tested it extensively, it doesn't appear to fail for
me, with numpy 1.02 and an AMD Athlon(tm) XP 2800+ under linux 2.6, so it is
possible that numpy's implementation has changed wrt. to GIL usage (or that
the platform makes a difference, but that seems less likely):

In [26]: run ~/tmp/t.py
final count: 200
  should be: 200

In [27]: run ~/tmp/t.py
final count: 200
  should be: 200

In [28]: run ~/tmp/t.py
final count: 200
  should be: 200

In [29]: run ~/tmp/t.py
final count: 200
  should be: 200

In [30]: run ~/tmp/t.py
final count: 1000
  should be: 1000

I don't claim to have deep knowledge of threading/GIL issues under python, but
my understanding is that you can certainly write an extension that does this
atomically if required (although the bytecode for immutable and mutable is the
same, I think the problem in the immutable case is that the rebinding that
occurs after the inplaceadd step is to a new object, and hence not a no-op).
This isn't to distract from your and Aahz point downthread: this is inherently
a very brittle way to go about things. On the other hand if you have some
already dodgy legacy code (as the OP indicated) that you want to make work
with a minimum of fuzz, it might still be a reasonable option.

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


Re: Numpy not found

2008-05-04 Thread Glenn Hutchings
adolfo <[EMAIL PROTECTED]> writes:

> I downloaded and installed Phyton 2.52 (it works), numpy-1.0.4.win32-
> py2.5, and scipy-0.6.0.win32-py2.5
>
> I can´t get Numpy to show up at Python´s  IDLE, or command line. If I
> do:
>
> import Numeric
> # I get
> Traceback (most recent call last):
>   File "", line 1, in 
> import Numeric
> ImportError: No module named Numeric

Try 'import numpy' instead.  Numeric is an earlier incarnation of numpy --
it has (mostly) the same interface, but is a different package.  (Just to
confuse things even further, there's also another old one, called
numarray).

> And if I do:
> import Numeric *
> # I get
> SyntaxError: invalid syntax

The proper syntax for that is (assuming you want numpy instead) 'from numpy
import *'.

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


Re: Are rank noobs tolerated, here?

2008-05-04 Thread Benjamin
On May 4, 6:45 pm, notbob <[EMAIL PROTECTED]> wrote:
> I'm trying to learn how to program.  I'm using:
>
> How to Think Like a Computer Scientist
>
> Learning with Python
> 2nd Edition
>
> Am I likely to receive any help, here, or is there another irc, forum, etc,
> that might better serve a complete amateur such as myself.  Thnx.

Everybody is very nice on IRC, in forums, and here. We don't bite that
hard!
>
> nb

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


  1   2   >