config files in python

2008-05-04 Thread sandipm
Hi,
 In my application, I have some configurable information which is used
by different processes. currently I have stored configration in a
conf.py file as name=value pairs, and I am importing conf.py file to
use this variable. it works well

import conf
print conf.SomeVariable

but if I need to change some configuration parameteres,  it would need
me to restart processes.

I want to store this data in some conf file (txt) and would like to
use it same way as I am using these variables as defined in py
files.

one solution I can think of is writing data as a dictionary into conf
file. and then by reading data, apply eval on that data. and update
local dict? but this is not a good solution

any pointers?

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


Re: Script using generators produces different results when invoked as a CGI

2008-05-04 Thread Gabriel Genellina
En Mon, 05 May 2008 00:31:45 -0300, Barclay, Ken <[EMAIL PROTECTED]> escribió:

> I attended David Beazley's awe-inspiring tutorial on the use of
> generators in systems programming:
>http://www.dabeaz.com/generators/
> http://www.dabeaz.com/generators/>
>I used his approach to write a web tool that can display search results
> from different log files. But the resulting script produced fewer
> results when invoked as CGI than it did when run from the command line,
> and I can't figure out why.

> Problem: For small sets of files this works great. But when I had 19Meg
> worth of log files in a test directory, the script would return the
> correct number of matching lines (288) only when it was invoked directly
> from the command line. When invoked from a CGI script, it returns 220
> lines instead (written to the "tempfile", below.) I don't know where
> that limit is coming from. If more logs are added to the test directory,
> the result is always the same 220 lines.
>I'm using Python 2.5.1 on Red Hat Linux 3.2.3-47. Below is the whole
> script I was testing with. It's using hard-coded values in place of ones
> I'll be getting from an HTML form (generated with HTMLgen) presented to
> the user.
>There are no exceptions or errors of any kind. Any pointers on what
> might be happening here would be welcome!

No entry in the error log, on the web server? (Apache?)
Perhaps you're hitting some internal limit of the cgi process, either memory or 
cpu time per request or temp file size...
Are you sure the script runs to completion? Output a message at the end, to be 
sure.

-- 
Gabriel Genellina

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


Re: logger output

2008-05-04 Thread Gabriel Genellina
En Mon, 05 May 2008 00:33:12 -0300, skunkwerk <[EMAIL PROTECTED]> escribió:

> i'm redirecting the stdout & stderr of my python program to a log.
> Tests i've done on a simple program with print statements, etc. work
> fine.  however, in my actual program i get weird output like this:
>
> 2008-05-04 20:20:44,790 DEBUG Grabbing message from queue, if any
> 2008-05-04 20:20:44,790 DEBUG DEBUG:doit:Grabbing message from queue,
> if any
> 2008-05-04 20:20:44,790 DEBUG DEBUG:doit:DEBUG:doit:Grabbing message
> from queue, if any
> 2008-05-04 20:20:44,790 DEBUG
> DEBUG:doit:DEBUG:doit:DEBUG:doit:Grabbing message from queue, if any
>
>
> class write2Log:
>   def write(self, x):
>   if x!='\n':
>   logger.debug(str(x))
>
> any ideas what might be causing the problems?  some of the messages
> being output are quite long - might this be a problem?

Try this simplified example and see by yourself:

import sys

class Write2Log:
 def write(self, x):
 sys.__stdout__.write('[%s]' % x)

sys.stdout = Write2Log()

print "Hello world!"
age = 27
name = "John"
print "My name is", name, "and I am", age, "years old."

-- 
Gabriel Genellina

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


USB HID documentation?

2008-05-04 Thread ETP
I have a little robot project I'm programming in python using the
Lynxmotion SSC-32 servo controller via serial.  I'd like to use a USB
game controller (PS2 style) to control the robot.

I've read a number of threads about the difficulty of using USB
without extensive understanding of the way it works, but a few have
stated that HID is different somehow.  I definitely do not have any
USB knowledge, and I don't care to get into that.  However, if USB-HID
is much simpler I would love to learn.

I have the python extensions for windows:
 http://sourceforge.net/project/showfiles.php?group_id=78018
but I haven't found any documentation on how to implement HID in
python.  Can anyone give me some examples or links?

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



Re: generator functions in another language

2008-05-04 Thread Gabriel Genellina
En Mon, 05 May 2008 00:09:02 -0300, hdante <[EMAIL PROTECTED]> escribió:

>  Isn't this guy a bot ? :-) It's learning fast. I believe there is a
> "frame" in C, composed of its stack and globals. For generators in C,
> you may look for "coroutines". For example, see:
>
> http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
>
>  A sample code follows:
>
> #define crBegin static int state=0; switch(state) { case 0:
> #define crReturn(i,x) do { state=i; return x; case i:; } while (0)
> #define crFinish }
> int function(void) {
> static int i;
> crBegin;
> for (i = 0; i < 10; i++)
> crReturn(1, i);
> crFinish;
> }
>
>  The "suspended state" is saved in the static variable. It's not
> necessary to save the complete "frame", only the suspended state.

Quoting the author himself, "this is the worst piece of C hackery ever seen"

-- 
Gabriel Genellina

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


Re: Truncate beginning of a file

2008-05-04 Thread Ben Finney
[EMAIL PROTECTED] writes:

> file.truncate(X) will truncate the file to at most X bytes (i.e.
> leave the first X bytes of the file and throw away the rest).

Yes, by (IIRC) asking the filesystem to do this in a simple operation.
Most (all?) filesystems support this operation either directly or with
little effort.

> Is there a way to throw away, say, the first X bytes of the file,
> and leave the rest? (Without opening the same file for reading,
> reading and processing, overwriting the file with the new processed
> data, etc.)

I don't know of any filesystem that supports this operation without
reading the file data. The built-in 'file' type doesn't support it.

-- 
 \   "It's easy to play any musical instrument: all you have to do |
  `\   is touch the right key at the right time and the instrument |
_o__) will play itself."  -- Johann Sebastian Bach |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


Re: Truncate beginning of a file

2008-05-04 Thread Marc 'BlackJack' Rintsch
On Sun, 04 May 2008 22:06:42 -0700, s0suk3 wrote:

> file.truncate(X) will truncate the file to at most X bytes (i.e. leave
> the first X bytes of the file and throw away the rest). Is there a way
> to throw away, say, the first X bytes of the file, and leave the rest?
> (Without opening the same file for reading, reading and processing,
> overwriting the file with the new processed data, etc.)

No.

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list


Truncate beginning of a file

2008-05-04 Thread s0suk3
file.truncate(X) will truncate the file to at most X bytes (i.e. leave
the first X bytes of the file and throw away the rest). Is there a way
to throw away, say, the first X bytes of the file, and leave the rest?
(Without opening the same file for reading, reading and processing,
overwriting the file with the new processed data, etc.)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Script Optimization

2008-05-04 Thread Gabriel Genellina
En Sun, 04 May 2008 17:01:15 -0300, lev <[EMAIL PROTECTED]> escribió:

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

Can't you configure it to use 4 spaces per indent - and not use "hard" tabs?

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

I can't think of a case when only indentation "is lost" - if you have a crash 
or something, normally you lose much more than indentation... Simple backups or 
a SCM system like cvs/svn will help. So I don't see the usefulness of those 
"pass" statements; I think that after some time using Python you'll consider 
them just garbage, as everyone else.

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

You can determine the directory where the script resides using

import os
basedir = os.path.dirname(os.path.abspath(__file__))

This way it doesn't matter how it was launched. But execute the above code as 
soon as possible (before any chdir)

> checksums = open(checksums, 'r')
> for fline in checksums.readlines():

You can directly iterate over the file:

 for fline in checksums:

(readlines() reads the whole file contents in memory; I guess this is not an 
issue here, but in other cases it may be an important difference)
Although it's perfectly valid, I would not reccomend using the same name for 
two different things (checksums refers to the file name *and* the file itself)

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

You don't have to copy the keys and sort; use the sorted() builtin:

 for key in sorted(changed_files.iterkeys()):

Also, "if len(changed_files) != 0" is usually written as:

 if changed_files:

The same for missing_files.

> for x in range(len(missing_files)):
> print '\t', missing_files[x]

That construct range(len(somelist)) is very rarely used. Either you don't need 
the index, and write:

for missing_file in missing_files:
 print '\t', missing_file

Or you want the index too, and write:

for i, missing_file in enumerate(missing_files):
 print '%2d: %s' % (i, missing_file)

> def calculate_checksum(file_name):
> file_to_check = open(file_name, 'rb')
> chunk = 8196

Any reason to use such number? 8K is 8192; you could use 8*1024 if you don't 
remember the value. I usually write 1024*1024 when I want exactly 1M.

-- 
Gabriel Genellina

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


python-list@python.org

2008-05-04 Thread Gabriel Genellina
En Sun, 04 May 2008 11:56:14 -0300, joop renes <[EMAIL PROTECTED]> escribió:

> 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

Python objects are reference counted, *and* you can have many threads running. 
This is not a problem in itself; Python has a Global Interpreter Lock (GIL) 
that ensures that addref/releases are properly handled. Only one thread at a 
time can execute Python code; but your C++ library can use as many threads as 
you want - as long as they don't call Python code again without reacquiring the 
GIL.

-- 
Gabriel Genellina

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


Script using generators produces different results when invoked as a CGI

2008-05-04 Thread Barclay, Ken
Hello,
 
I attended David Beazley's awe-inspiring tutorial on the use of
generators in systems programming:
 
http://www.dabeaz.com/generators/
http://www.dabeaz.com/generators/> 
 
I used his approach to write a web tool that can display search results
from different log files. But the resulting script produced fewer
results when invoked as CGI than it did when run from the command line,
and I can't figure out why.
 
He showed how to 'pipeline' generators together in a form of declarative
programming. For example, put the following generators together to grep
lines out of a set of files:
 
# Generate a list of files
lognames = gen_find("*.bz2","/tmp/bz2_alldata/pa_new")
# Yield a sequence of file objects that have been suitably opened
logfiles = gen_open(lognames)
# Concatenate multiple generators into a single sequence
loglines = gen_cat(logfiles)
# Grep a sequence of lines that match a re pattern
searchlines = gen_grep(r'fried',loglines)
 
The functions are only a few lines each:

def gen_open(filenames):
for name in filenames:
if name.endswith(".bz2"):
yield bz2.BZ2File(name)

def gen_cat(sources):
for s in sources:
for item in s:
yield item
 
import re
def gen_grep(pat,lines):
patc = re.compile(pat)
for line in lines:
if patc.search(line): yield line
 
Since they're generators, processing the data doesn't start until you
kick off the iteration on the final generator.
 
Problem: For small sets of files this works great. But when I had 19Meg
worth of log files in a test directory, the script would return the
correct number of matching lines (288) only when it was invoked directly
from the command line. When invoked from a CGI script, it returns 220
lines instead (written to the "tempfile", below.) I don't know where
that limit is coming from. If more logs are added to the test directory,
the result is always the same 220 lines.
 
I'm using Python 2.5.1 on Red Hat Linux 3.2.3-47. Below is the whole
script I was testing with. It's using hard-coded values in place of ones
I'll be getting from an HTML form (generated with HTMLgen) presented to
the user.
 
There are no exceptions or errors of any kind. Any pointers on what
might be happening here would be welcome!
 
Thanks
Ken
 
#!/usr/local/bin/python
 
import tempfile
from genfind import  gen_find
from genopen import  gen_open
from gencat  import  gen_cat
from gengrep import  gen_grep
 
tf = tempfile.mkstemp()
tmpfile = open(tf[1],'w')
 
lognames = gen_find("*.bz2","/tmp/bz2_alldata/pa_new")
logfiles = gen_open(lognames)
loglines = gen_cat(logfiles)
 
searchlines = gen_grep(r'fried',loglines)
for line in searchlines:
print >> tmpfile, line,
tmpfile.close()

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

logger output

2008-05-04 Thread skunkwerk
i'm redirecting the stdout & stderr of my python program to a log.
Tests i've done on a simple program with print statements, etc. work
fine.  however, in my actual program i get weird output like this:

2008-05-04 20:20:44,790 DEBUG Grabbing message from queue, if any
2008-05-04 20:20:44,790 DEBUG DEBUG:doit:Grabbing message from queue,
if any
2008-05-04 20:20:44,790 DEBUG DEBUG:doit:DEBUG:doit:Grabbing message
from queue, if any
2008-05-04 20:20:44,790 DEBUG
DEBUG:doit:DEBUG:doit:DEBUG:doit:Grabbing message from queue, if any

followed by:
2008-05-04 20:20:44,815 DEBUG DEBUG:doit:Traceback (most recent call
last):
2008-05-04 20:20:44,815 DEBUG DEBUG:doit:DEBUG:doit:Traceback (most
recent call last):
2008-05-04 20:20:44,815 DEBUG
DEBUG:doit:DEBUG:doit:DEBUG:doit:Traceback (most recent call last):

the code I'm using for the log stuff:

import logging
logger = logging.getLogger('doit')
hdlr = logging.FileHandler('/home/imran/doit.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG)

class write2Log:
def write(self, x):
if x!='\n':
logger.debug(str(x))

sys.stdout = write2Log()
sys.stderr= write2Log()

any ideas what might be causing the problems?  some of the messages
being output are quite long - might this be a problem?

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


Re: Are rank noobs tolerated, here?

2008-05-04 Thread Ben Finney
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.

This forum is particularly welcoming to newbies, I've found. Go ahead
and start a new thread for any Python question.

One caveat: When you do compose a message asking a question, please
make the Subject field of your message meaningful by itself and don't
put anything like "Newbie question" in there. If it's a newbie
question, we'll figure that out for ourselves, and it shouldn't affect
whether or not it gets answered.

(You haven't done anything like that, but I'm noting it in advance and
because I hope *other* newbies will also see this and take note.)

-- 
 \"I'd take the awe of understanding over the awe of ignorance |
  `\   any day."  -- Douglas Adams |
_o__)  |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


Re: generator functions in another language

2008-05-04 Thread Terry Reedy
A 'generator function' -- a function that when called returns a generator 
object, a specific type of iterator,  is a rather Python specific concept. 
Better to think,  I think, in terms of writing an iterator 'class' (in C, 
struct with associated function).  Looking at the implementation of two of 
the itertools in the link below (previously posted), I could see the 
'template' that each was based on and that would be the basis for writing 
another.  (And I have not programmed C for a decade.  Raymond's code is 
quite clear.)

=
[Outlook Express does not 'quote' this post properly]

"Gabriel Genellina" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

 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

=
Terry again: one can think of a generator function as an abbreviation of an 
iterator class.  Calling the gen. func. produces an iterator instance just 
like calling the class object.
=
[back to Gabriel]
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.

==



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


Re: generator functions in another language

2008-05-04 Thread hdante
On May 4, 8:11 am, [EMAIL PROTECTED] wrote:
> 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.hhttp://svn.python.org/projects/python/trunk/Objects/frameobject.c
>
> The generator code.
>
> http://svn.python.org/projects/python/trunk/Include/genobject.hhttp://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.

 Isn't this guy a bot ? :-) It's learning fast. I believe there is a
"frame" in C, composed of its stack and globals. For generators in C,
you may look for "coroutines". For example, see:

http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html

 A sample code follows:

#define crBegin static int state=0; switch(state) { case 0:
#define crReturn(i,x) do { state=i; return x; case i:; } while (0)
#define crFinish }
int function(void) {
static int i;
crBegin;
for (i = 0; i < 10; i++)
crReturn(1, i);
crFinish;
}

 The "suspended state" is saved in the static variable. It's not
necessary to save the complete "frame", only the suspended state.

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


Re: Please help - Tkinter not doing anything

2008-05-04 Thread s0suk3
On May 4, 6: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.)

Of course. I forgot to say about that. It is cross platform, like most
things in Python. I'd say it's actually more cross platform than
Tkinter, because of the looks of both in different platforms. In
Tkinter, you can get all the widgets to work in all the platforms it
supports, but the widgets look so different on different platforms,
that at the end you end up modifying the code anyway so that the
controls look better on the platform that you port the program to.
With what I've done with wxPython, the controls look great on any of
the platforms I've tried my programs in. Another thing: wxPython is a
port of the wxWidgets toolkit which is for C++. It has also been
ported to a bunch of other languages like Ruby, Perl, C#, etc; so if
you learn any of the languages which wxWidgets has been ported to, you
can use the toolkit in the same way that you've been doing with
Python, except that with the syntax of the other language.
--
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


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


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


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


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

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


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


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

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


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


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


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


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

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


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


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


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


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


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


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

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


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

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


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

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


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


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


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

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


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


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


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


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


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


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

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


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


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


  1   2   >