Re: Still the __new__ hell ...

2007-03-18 Thread Arnaud Delobelle
On Mar 17, 11:48 pm, Paulo da Silva <[EMAIL PROTECTED]> wrote:
> Arnaud Delobelle escreveu:> On Mar 17, 9:31 pm, Paulo da Silva <[EMAIL 
> PROTECTED]> wrote:

[snip]

> Thanks. This works exactly the way you wrote.
> Yet I am misunderstanding something. Can't pickle "see" that being
> MyDate derived from date it also has to look at variables from date?
> When do I need to do this? I am using pickle with a lot more complex
> classes without this problem.

Without the MyDate.__reduce__ method, Python uses the
datetime.date.__reduce__ method to pickle your MyDate instances, then
it uses MyDate.__new__ to unpickle them.  But your MyDate.__new__
method does not understand the format of a pickled date.  You could
also change the MyDate.__new__ method so that it does understand it,
but it wouldn't be that easy as you want it to accept a string, and
this is the format that dates are pickled in.

--
Arnaud



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


Re: Finding the insertion point in a list

2007-03-18 Thread Paul Rubin
Steve Holden <[EMAIL PROTECTED]> writes:
> >max(i for i,t in enumerate(x) if t <= y)
> > Those are actually pretty direct.
> 
> How about a solution (like the bisect one suggested almost as soon as
> this thread started) that doesn't iterate over the whole list.

Here's a Haskell-inspired one:

len(list(itertools.takewhile(lambda t: y > t, x)))

It stops iterating when it hits an element >= y.  I originally wanted
to write the above as:

len(itertools.takewhile(y.__gt__, x))

but it looks like regular numbers only support __cmp__ and not rich
comparison, and also you can't take the length of an iterator.  In
Haskell this type of thing is very natural:

   length(takeWhile (y >) x)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Webcams and python

2007-03-18 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Synt4x wrote:

> from VideoCapture import Device
> cam = Device()
> 
> while 1:
> img = cam.getImage()
> 
> Now, by doing this, my processor fires up to 100% load, which
> obviously makes my pc useless.
> 
> Is there any way or algorithm to lower the cpu load?

Just throw a small `time.sleep()` into the loop.

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


Re: lock problem

2007-03-18 Thread Gabriel Genellina
En Fri, 16 Mar 2007 19:08:14 -0300, Ritesh Raj Sarraf <[EMAIL PROTECTED]>  
escribió:

>> But you miss the fact that there is only one environment per process.
>
> Maybe there's a confusion.
> The environment variable that I'm setting has noting to do with  
> ldapsearch. I
> use the environment variable as a filename to which ldapsearch can  
> redirect its
> output. And that I do is because the output can be huge and useless.
> Then I do some pattern matching on that file and filter my data and then  
> delete
> it.
>
> If you think I still am missing something important, request you to  
> describe it.

Then, why do you use an environment variable? Just redirect the output to  
the desired file name (when you are making the command line to be executed  
by os.system)

-- 
Gabriel Genellina

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


Re: cannot start IDLE in WinXP

2007-03-18 Thread Gabriel Genellina
En Sat, 17 Mar 2007 08:09:16 -0300, imx <[EMAIL PROTECTED]> escribió:

> Enviroment: WinXP sp2, python 2.5
> problem: click IDLE using shorcut menu or run phthonw.exe directly,
> nothing happen! But running python.exe from the command line is fine.

pythonw.exe does not open a window so it's almost invisible.
IDLE now uses a socket to connect to the subprocess which runs the python  
code; perhaps your firewall is blocking that connection. Try starting IDLE  
with the -n switch.

-- 
Gabriel Genellina

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


Re: Private data

2007-03-18 Thread Gabriel Genellina
En Sat, 17 Mar 2007 13:31:01 -0300, Dustan <[EMAIL PROTECTED]>  
escribió:

> http://dustangroups.googlepages.com/privateattributesinpython
>
> This is something that I just threw together this morning, after a
> eureka moment. It's a way of creating private class attributes and
> static function variables (I'm not 100% sure if that's the correct
> terminology, but you get what I mean). I haven't tried to create
> private instance attributes, mainly because it would just be too
> difficult, and it would be awful syntax. I'm not considering actually
> using this, but I do have a couple questions about it.

I feel so dumb, but I can't see how to use it, or what's for. Perhaps an  
example?

-- 
Gabriel Genellina

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


Re: To count number of quadruplets with sum = 0

2007-03-18 Thread Jorge Godoy
"n00m" <[EMAIL PROTECTED]> writes:

> my dial-up line's too slow for downloading 4mb of shedskin-0.0.20.exe


Don't worry!  We can email it to you. :-D


-- 
Jorge Godoy  <[EMAIL PROTECTED]>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Automagically log changes in table

2007-03-18 Thread Diez B. Roggisch
George Sakkis schrieb:
> This is more in the context of Turbogears/SQLAlchemy, but if anyone
> has implemented something similar with other packages it might be
> useful to know.

SQLObject since version 0.8 lets you define event listeners for 
create/update/delete events on objects. You could hook into them.

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


Re: Private data

2007-03-18 Thread Steven D'Aprano
On Sat, 17 Mar 2007 09:31:01 -0700, Dustan wrote:

> http://dustangroups.googlepages.com/privateattributesinpython
> 
> This is something that I just threw together this morning, after a
> eureka moment. It's a way of creating private class attributes and
> static function variables (I'm not 100% sure if that's the correct
> terminology, but you get what I mean). I haven't tried to create
> private instance attributes, mainly because it would just be too
> difficult, and it would be awful syntax. I'm not considering actually
> using this, but I do have a couple questions about it.
> 
> 1. Has anyone else ever come up with something like this? I can't
> imagine I'm the only person who's ever thought of this.

I've never seen anything like this before, but then I haven't gone looking
for anything like this.



> 2. Is it possible to hack into something like this? ie, would it be
> possible to see and change these variables from client code (assuming
> the data manager has been properly removed from sight, as shown on the
> last line of class block TestPrivateClassAttributes)?

Yes.

First, an example of the code in action.

>>> import PrivateAttributes
>>> obj = PrivateAttributes.TestPrivateClassAttributes()
>>> obj.getNumInstances()
1
>>> another = PrivateAttributes.TestPrivateClassAttributes()
>>> obj.getNumInstances()
2
>>> athird = PrivateAttributes.TestPrivateClassAttributes()
>>> athird.getNumInstances()
3

The getNumInstances method reports the number of instances of the
PrivateAttributes class. There's no obvious class attribute where this
count is being kept:

>>> obj.__class__.__dict__.keys()
['__module__', 'getNumInstances', '__dict__', '__weakref__', '__doc__',
'__init__']


Here's how to hack it, and make it report wrong numbers.

>>> c = obj.getNumInstances.func_closure
>>> c[1].cell_contents.numInstances = -300
>>> 
>>> athird.getNumInstances()
-300
>>> afourth = PrivateAttributes.TestPrivateClassAttributes()
>>> athird.getNumInstances()
-299

So yes, it is absolutely hackable.

Now, I'm hardly a Python guru, but in about fifteen minutes I followed the
trail through the object chain, and found how to hack this. An real guru
would probably do it in three minutes.

I was helped a bit by having the source code. But even without the source
code, I reckon I could have done it in an hour or so, if I was motivated
enough. All the tools you need are a Python interactive session, the dir()
function and the dis module.



-- 
Steven

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


Re: Private data

2007-03-18 Thread Dustan
On Mar 18, 5:26 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Sat, 17 Mar 2007 13:31:01 -0300, Dustan <[EMAIL PROTECTED]>
> escribió:
>
> >http://dustangroups.googlepages.com/privateattributesinpython
>
> > This is something that I just threw together this morning, after a
> > eureka moment. It's a way of creating private class attributes and
> > static function variables (I'm not 100% sure if that's the correct
> > terminology, but you get what I mean). I haven't tried to create
> > private instance attributes, mainly because it would just be too
> > difficult, and it would be awful syntax. I'm not considering actually
> > using this, but I do have a couple questions about it.
>
> I feel so dumb, but I can't see how to use it, or what's for. Perhaps an
> example?
>
> --
> Gabriel Genellina

There are two examples - one demonstrating static function variables
and one demonstrating private class attributes. Both perform some kind
of counting. The function returns how many times it's been called.
Here it is:

@PrivateDataEngine(numCalls = 0)
def testPrivateStaticFunctionVariables(internalData):
"""returns the number of times this function has been called."""
internalData.numCalls += 1
return internalData.numCalls

There's a comment explaining how the decorator works on the page I
linked you to.

A common pythonic workaround for this is something like this:

def testMutableDefaultArgs(_cache = {'internalData':0}):
"""returns the number of times this function has been called."""
_cache['internalData'] += 1
return _cache['internalData']

The default argument only gets evaluated once, and since it's mutable
and being modified in the function, it magically becomes
{'internalData':1} after 1 call and {'internalData':1348372} after
1348372 calls.

The other example, explaining private class attributes, has a method
getNumInstances that returns how many instances have been created.
Here that is:

class TestPrivateClassAttributes(object):
# pca here obviously stands for Private Class Attributes.
pcaManager = PrivateDataEngine(numInstances = 0)

# Notice that the internal data is an implicit parameter that
comes first,
# even before the self parameter.
@pcaManager
def __init__(internalData, self):
internalData.numInstances += 1

@staticmethod
@pcaManager
def getNumInstances(internalData):
return internalData.numInstances

# Don't forget to delete the pcvManager, or it will have all been
in vain.
del pcaManager

I'm thinking this is going to look rather ugly when the text gets
wrapped; that's why I linked to an external page in the first place.

Notice it requires more advanced usage of the PrivateDataEngine if you
want multiple functions to have access to the same data, as you
normally would in a class.

Note also that the double-decorated method getNumInstances requires
that staticmethod be the first decorator. This is equivalent to the
code "getNumInstances = staticmethod(pcaManager(getNumInstances))". I
got an error when I tried it with pcaManager preceding staticmethod,
so my educated guess is that staticmethods are somehow specially
recognized by the class, and therefore they have to actually BE
staticmethods, not dressed over by a pcaManager.

Now that I think of it, for clarity, pcaManager may not have been a
good name for the function returned by PrivateDataEngine; pcaDecorator
would have been better, seeing as it IS a decorator. Perhaps I'll
change that soon.

The pythonic way would be:

class TestPythonsPrivateClassAttributes(object):
_numInstances = 0

def __init__(self):
# class attributes can be accessed from self.
self.numInstances += 1

@staticmethod
def getNumInstances(internalData):
return self.numInstances

It should immediately stick out as being much more concise
syntactically, and therefore much less bug-prone.

Obviously, these examples are a lot like "Hello, World!" in their
usefulness; one would very rarely want to track how many times a
function has been called.

Hope that helps.

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


Re: GCC 4.1.2 installer for Python distutils compilation

2007-03-18 Thread David Rushby
On Mar 18, 5:08 am, Giovanni Bajo <[EMAIL PROTECTED]> wrote:
> This page:
>http://www.develer.com/oss/GccWinBinaries
>
> contains a friendly Windows installer for GCC 4.1.2 (MinGW binary version),
> with full support for integrating it with Python installations so that it is
> used by distutils to compile Python extensions.

Sweet!

Even though I have access to MSVC 7.1, so I don't really need MinGW
myself, it can be unnecessarily difficult to get Windows-using
contributors started on a project that involves C extensions.  Your
contribution should improve the situation greatly.

Thanks a lot.

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


Re: Private data

2007-03-18 Thread Dustan
On Mar 18, 7:06 am, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> On Sat, 17 Mar 2007 09:31:01 -0700, Dustan wrote:
> >http://dustangroups.googlepages.com/privateattributesinpython
>
> > This is something that I just threw together this morning, after a
> > eureka moment. It's a way of creating private class attributes and
> > static function variables (I'm not 100% sure if that's the correct
> > terminology, but you get what I mean). I haven't tried to create
> > private instance attributes, mainly because it would just be too
> > difficult, and it would be awful syntax. I'm not considering actually
> > using this, but I do have a couple questions about it.
>
> > 1. Has anyone else ever come up with something like this? I can't
> > imagine I'm the only person who's ever thought of this.
>
> I've never seen anything like this before, but then I haven't gone looking
> for anything like this.
>
> > 2. Is it possible to hack into something like this? ie, would it be
> > possible to see and change these variables from client code (assuming
> > the data manager has been properly removed from sight, as shown on the
> > last line of class block TestPrivateClassAttributes)?
>
> Yes.
>
> First, an example of the code in action.
>
> >>> import PrivateAttributes
> >>> obj = PrivateAttributes.TestPrivateClassAttributes()
> >>> obj.getNumInstances()
> 1
> >>> another = PrivateAttributes.TestPrivateClassAttributes()
> >>> obj.getNumInstances()
> 2
> >>> athird = PrivateAttributes.TestPrivateClassAttributes()
> >>> athird.getNumInstances()
>
> 3
>
> The getNumInstances method reports the number of instances of the
> PrivateAttributes class. There's no obvious class attribute where this
> count is being kept:
>
> >>> obj.__class__.__dict__.keys()
>
> ['__module__', 'getNumInstances', '__dict__', '__weakref__', '__doc__',
> '__init__']
>
> Here's how to hack it, and make it report wrong numbers.
>
> >>> c = obj.getNumInstances.func_closure
> >>> c[1].cell_contents.numInstances = -300
>
> >>> athird.getNumInstances()
> -300
> >>> afourth = PrivateAttributes.TestPrivateClassAttributes()
> >>> athird.getNumInstances()
>
> -299
>
> So yes, it is absolutely hackable.

I did have a feeling that it was hackable, but had no idea how one
could possibly go about hacking it (I was starting to wonder of there
was a way to apply locals() and globals() on functions). But now I
(ehem) sorta know how it's done.

> Now, I'm hardly a Python guru, but in about fifteen minutes I followed the
> trail through the object chain, and found how to hack this. An real guru
> would probably do it in three minutes.
>
> I was helped a bit by having the source code. But even without the source
> code, I reckon I could have done it in an hour or so, if I was motivated
> enough. All the tools you need are a Python interactive session, the dir()
> function and the dis module.

I have used all of those before, but I haven't been able to fully
understand the output of the dis module; maybe that's my problem.

> --
> Steven

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


encoding - arabic(IBM 864) to UNICODE

2007-03-18 Thread Madhu Alagu
Hello,



How to convert IBM 864,IBM 420 & Nafitha(Arabic)  to UNICODE.


Thanks

Madhu Alagu

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


Re: Private data

2007-03-18 Thread Dustan
On Mar 18, 7:17 am, "Dustan" <[EMAIL PROTECTED]> wrote:
> On Mar 18, 5:26 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
> wrote:
>
>
>
> > En Sat, 17 Mar 2007 13:31:01 -0300, Dustan <[EMAIL PROTECTED]>
> > escribió:
>
> > >http://dustangroups.googlepages.com/privateattributesinpython
>
> > > This is something that I just threw together this morning, after a
> > > eureka moment. It's a way of creating private class attributes and
> > > static function variables (I'm not 100% sure if that's the correct
> > > terminology, but you get what I mean). I haven't tried to create
> > > private instance attributes, mainly because it would just be too
> > > difficult, and it would be awful syntax. I'm not considering actually
> > > using this, but I do have a couple questions about it.
>
> > I feel so dumb, but I can't see how to use it, or what's for. Perhaps an
> > example?
>
> > --
> > Gabriel Genellina
>
> There are two examples - one demonstrating static function variables
> and one demonstrating private class attributes. Both perform some kind
> of counting. The function returns how many times it's been called.
> Here it is:
>
> @PrivateDataEngine(numCalls = 0)
> def testPrivateStaticFunctionVariables(internalData):
> """returns the number of times this function has been called."""
> internalData.numCalls += 1
> return internalData.numCalls
>
> There's a comment explaining how the decorator works on the page I
> linked you to.
>
> A common pythonic workaround for this is something like this:
>
> def testMutableDefaultArgs(_cache = {'internalData':0}):
> """returns the number of times this function has been called."""
> _cache['internalData'] += 1
> return _cache['internalData']
>
> The default argument only gets evaluated once, and since it's mutable
> and being modified in the function, it magically becomes
> {'internalData':1} after 1 call and {'internalData':1348372} after
> 1348372 calls.
>
> The other example, explaining private class attributes, has a method
> getNumInstances that returns how many instances have been created.
> Here that is:
>
> class TestPrivateClassAttributes(object):
> # pca here obviously stands for Private Class Attributes.
> pcaManager = PrivateDataEngine(numInstances = 0)
>
> # Notice that the internal data is an implicit parameter that
> comes first,
> # even before the self parameter.
> @pcaManager
> def __init__(internalData, self):
> internalData.numInstances += 1
>
> @staticmethod
> @pcaManager
> def getNumInstances(internalData):
> return internalData.numInstances
>
> # Don't forget to delete the pcvManager, or it will have all been
> in vain.
> del pcaManager
>
> I'm thinking this is going to look rather ugly when the text gets
> wrapped; that's why I linked to an external page in the first place.
>
> Notice it requires more advanced usage of the PrivateDataEngine if you
> want multiple functions to have access to the same data, as you
> normally would in a class.
>
> Note also that the double-decorated method getNumInstances requires
> that staticmethod be the first decorator. This is equivalent to the
> code "getNumInstances = staticmethod(pcaManager(getNumInstances))". I
> got an error when I tried it with pcaManager preceding staticmethod,
> so my educated guess is that staticmethods are somehow specially
> recognized by the class, and therefore they have to actually BE
> staticmethods, not dressed over by a pcaManager.
>
> Now that I think of it, for clarity, pcaManager may not have been a
> good name for the function returned by PrivateDataEngine; pcaDecorator
> would have been better, seeing as it IS a decorator. Perhaps I'll
> change that soon.
>
> The pythonic way would be:
>
> class TestPythonsPrivateClassAttributes(object):
> _numInstances = 0
>
> def __init__(self):
> # class attributes can be accessed from self.
> self.numInstances += 1
>
> @staticmethod
> def getNumInstances(internalData):
> return self.numInstances
>
> It should immediately stick out as being much more concise
> syntactically, and therefore much less bug-prone.
>
> Obviously, these examples are a lot like "Hello, World!" in their
> usefulness; one would very rarely want to track how many times a
> function has been called.
>
> Hope that helps.

I forgot to note that the 'pythonic' ways are untested (and remain so,
as far as I know).

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


Re: getopt or optparse options/arguments wrapping?

2007-03-18 Thread Rocky Zhou
Well, I think I must have more explanation on my script. The script's
usage is like this:
~# fs_backup
Lack of backup identity name
program usage: fs_backup [OPTIONS] $identity
OPTIONS:
-a|--append [t/x[L]:$path, append 1 dir or file to $identity
t/x: include/exclude list, as -T/-X of 'tar'
L:file, append a batch of dirs/files to $identity by LIST
-d|--delete $path, delete 1 dir or file from $identity
-t|--type $type, specify the backup type: full/diff/incr
-D|--delete-outdated [f/d], delete outdated files/empty-dirs when
restoring.
-o 'option=value'
-w|--wrap 'wrap of tar options'
-h|--help, print this help message


For example, I want to backup /var/www/html, with excluding /var/www/
html/syssite/home/cache, and name this backup as pages, I will do
this:
sh# fs_backup -a t:/var/www/html pages
sh# fs_backup -a x:/var/www/html/syssite/home/cache pages
# These t/x have the same meaning of tar's '-T/-X'
sh# fs_backup -t full pages
sh# fs_backup -t incr pages

These will update /var/fs_backup/.table, (/var/fs_backup is 'datadir',
you can configure it to anywhere you like by editing the configuration
file, /etc/new/fs_backup.config):
pages, full, 20070204-231749, pages/pages.full.20070204-231749
pages, incr, 20070205-101011, pages/pages.incr.20070205-101011
..

May be I want add something to 'pages':
sh# fs_backup -a t:/usr/local/apache2/htdocs pages
sh# fs_backup -t full pages

Once I want recovery from the backup, I will do this:
sh# fs_rstore pages
This fs_rstore is just a symlink to fs_backup. All the files from the
last full backup will be restored.

The actually thing the script done is like this:
2007-03-18 20:32:20 fs_backup DEBUG find /mnt/file/data/ -cnewer /var/
fs_backup/work/.ts ! -type d -print >>/tmp/fs_backup.work.incr.
1174221140.0.list
2007-03-18 20:32:20 fs_backup DEBUG find /mnt/file/work/ -cnewer /var/
fs_backup/work/.ts ! -type d -print >>/tmp/fs_backup.work.incr.
1174221140.0.list
2007-03-18 20:32:20 fs_backup DEBUG tar -czp -P -f /var/fs_backup/work/
work.incr.20070318-203220.tgz -T /tmp/fs_backup.work.incr.
1174221140.0.list .identity .dirs .files

.dirs .files is just a snapshot of the current directories, which can
be used to delete-outdated files when restoring. Here I used absolute
path by using tar's -P parameter. When fs_rstore, it will do this:
command = "tar -xpz -P -f %s.tgz -T %s" % (archive, self.t_files)

maybe I want add some parameters of tar, for example, --strip-path=X,
so I hope the fs_rstore can do this:
sh# fs_rstore pages --strip-path=X
rather than:
sh# fs_rstore pages -w '--strip-path=X'

What should I do?

Thank you.

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


Re: Automagically log changes in table

2007-03-18 Thread aspineux
On 18 mar, 04:20, "George Sakkis" <[EMAIL PROTECTED]> wrote:
> On Mar 17, 7:59 pm, "aspineux" <[EMAIL PROTECTED]> wrote:
>
> > Hi
>
> > You can get this using "triggers" and "stored procedures".
> > These are SQL engine dependent! This is available for long time with
> > postgress and only from version 5 with mysql.
> > This let you write SQL code (Procedure) that will be called when
> > "trigged" by an event like inserting new row, updating rows,
> > deleting 
>
> I'd rather avoid triggers since I may have to deal with Mysql 4. Apart
> from that, how can a trigger know the current user ?

Because each user opening an web session will login to the SQL
database using it's own
login. That way you will be able to manage security at DB too. And the
trigger
will use the userid of the SQL session.

> This information
> is part of the context (e.g. an http request), not stored persistently
> somewhere. It should be doable at the framework/orm level but I'm
> rather green on Turbogears/SQLAlchemy.
>
> George

Maybe it will be easier to manage this in your web application.

it's not too dificulte to replace any

UPDATE persone
SET name = %{new_name}s
WHERE persone_id=%{personeid}

by something like

UPDATE persone
SET name = %{new_name}s, modified_by=%{user_id}s, modified_time=%
{now}s
WHERE persone_id=%{personeid}

BR

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


Re: most complete xml package for Python?

2007-03-18 Thread [EMAIL PROTECTED]
On Mar 15, 5:45 pm, "Paul Boddie" <[EMAIL PROTECTED]> wrote:


>
> I can't remember exactly how I solved this within anXML/XSLT-heavy
> Java-based framework,

I just put a single space between:

 

and then it kept the XML container-style as opposed to single-tag.

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


cheese shop: tagging and dating

2007-03-18 Thread [EMAIL PROTECTED]
The first thing I look at when examining a module is how often it is
updated. Unfortunately, the entries there dont show this. Eg:

  http://www.python.org/pypi/PySimpleXML/1.0

Second, it seems that tagging is more popular than the hierarchical
browsing method currently offered by Cheese Shop. Are there efforts
underway to support a tagging interface for the cheeseshop?

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


Re: Automagically log changes in table

2007-03-18 Thread Jorge Godoy
"aspineux" <[EMAIL PROTECTED]> writes:

> On 18 mar, 04:20, "George Sakkis" <[EMAIL PROTECTED]> wrote:
>>
>> I'd rather avoid triggers since I may have to deal with Mysql 4. Apart
>> from that, how can a trigger know the current user ?
>
> Because each user opening an web session will login to the SQL database
> using it's own login. That way you will be able to manage security at DB
> too. And the trigger will use the userid of the SQL session.

That's not common in web applications (he mentions TurboGears later).  What is
common is having a connection pool and just asking for one of the available
connections, when your app gets it, it just uses it.  After each request this
connection returns to the pool to be reused.


I dunno about SQL Alchemy (also mentioned later), but SQL Object 0.8x has some
events that can be bound so they can act is triggers on your database, but
client side.  Of course they don't have all the context as a real trigger
does, but those might be enough to avoid duplicating lots of code through the
app to set some variable.



-- 
Jorge Godoy  <[EMAIL PROTECTED]>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: encoding - arabic(IBM 864) to UNICODE

2007-03-18 Thread Jorge Godoy
"Madhu Alagu" <[EMAIL PROTECTED]> writes:

> Hello,
>
> How to convert IBM 864,IBM 420 & Nafitha(Arabic)  to UNICODE.

Your OS should have some tools for that.  On Linux I use 'iconv' to convert
from several encodings to several other encodings.

Another option is checking if Python has those encodings available (are they
standard or platform specific?) and using its own conversion method, as
explained in the docs. 

-- 
Jorge Godoy  <[EMAIL PROTECTED]>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cheese shop: tagging and dating

2007-03-18 Thread Jorge Godoy
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:

> The first thing I look at when examining a module is how often it is
> updated. Unfortunately, the entries there dont show this. Eg:
>
>   http://www.python.org/pypi/PySimpleXML/1.0
>
> Second, it seems that tagging is more popular than the hierarchical
> browsing method currently offered by Cheese Shop. Are there efforts
> underway to support a tagging interface for the cheeseshop?

I believe hierarchies are easier when there's a lot of information.  You go
browsing by topic.

If there are only tags available, you'd have to look for hundreds of tags to
see if none of the possible names for the "thing" has been used ("database",
"db", "relational", "orm", "mapper", etc.). 

Ammending the current hierarchic structure with an alternate tag interface
would be interesting, though.  But not making it the main interface or the
only one.

Tags are cool when they are few.  They are a nightmare when there are hundreds
or thousands of them to search for something.

-- 
Jorge Godoy  <[EMAIL PROTECTED]>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IDE for wxPython

2007-03-18 Thread Peter Decker
On 3/17/07, Ghirai <[EMAIL PROTECTED]> wrote:

> Can anyone suggest an IDE for wxPython?
> Or an IDE for TkInter?

Don't know about Tkinter, but for wxPython, I would suggest Dabo. You
get their visual tools, along with a more robust and consistent
wrapper around the wxPython API.

-- 

# p.d.
-- 
http://mail.python.org/mailman/listinfo/python-list


[Help]UnicodeDecodeError

2007-03-18 Thread Karl
error msg:
Mod_python error: "PythonHandler mod_python.publisher"

Traceback (most recent call last):

  File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line
299, in HandlerDispatch
result = object(req)

  File "/usr/lib/python2.3/site-packages/mod_python/publisher.py",
line 136, in handler
result = util.apply_fs_data(object, req.form, req=req)

  File "/usr/lib/python2.3/site-packages/mod_python/util.py", line
361, in apply_fs_data
return object(**args)

  File "/var/www/html/sky.py", line 38, in main
sresult = sresult.decode('zh-hk').encode('utf_8')

UnicodeDecodeError: 'big5hkscs' codec can't decode bytes in position
958-959: illegal multibyte sequence


Wt can I do, since there is some characters, is there any solutions to
solve it???

thx

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


Re: Private data

2007-03-18 Thread Dustan
On Mar 18, 7:25 am, "Dustan" <[EMAIL PROTECTED]> wrote:
> On Mar 18, 7:06 am, Steven D'Aprano
> > First, an example of the code in action.
>
> > >>> import PrivateAttributes
> > >>> obj = PrivateAttributes.TestPrivateClassAttributes()
> > >>> obj.getNumInstances()
> > 1
> > >>> another = PrivateAttributes.TestPrivateClassAttributes()
> > >>> obj.getNumInstances()
> > 2
> > >>> athird = PrivateAttributes.TestPrivateClassAttributes()
> > >>> athird.getNumInstances()
>
> > 3
>
> > The getNumInstances method reports the number of instances of the
> > PrivateAttributes class. There's no obvious class attribute where this
> > count is being kept:
>
> > >>> obj.__class__.__dict__.keys()
>
> > ['__module__', 'getNumInstances', '__dict__', '__weakref__', '__doc__',
> > '__init__']
>
> > Here's how to hack it, and make it report wrong numbers.
>
> > >>> c = obj.getNumInstances.func_closure
> > >>> c[1].cell_contents.numInstances = -300
>
> > >>> athird.getNumInstances()
> > -300
> > >>> afourth = PrivateAttributes.TestPrivateClassAttributes()
> > >>> athird.getNumInstances()
>
> > -299
>
> > So yes, it is absolutely hackable.
>
> I did have a feeling that it was hackable, but had no idea how one
> could possibly go about hacking it (I was starting to wonder of there
> was a way to apply locals() and globals() on functions). But now I
> (ehem) sorta know how it's done.
>
> > Now, I'm hardly a Python guru, but in about fifteen minutes I followed the
> > trail through the object chain, and found how to hack this. An real guru
> > would probably do it in three minutes.
>
> > I was helped a bit by having the source code. But even without the source
> > code, I reckon I could have done it in an hour or so, if I was motivated
> > enough. All the tools you need are a Python interactive session, the dir()
> > function and the dis module.
>
> I have used all of those before, but I haven't been able to fully
> understand the output of the dis module; maybe that's my problem.

Alright, perhaps you can help me out with this learning curve here. I
have seen, but not worked with, some basic assembly code, so I think I
have a vague idea of what it all means, although I have a feeling it's
not all valid assembly (on any widely used machine).

First I dis.dis'd testPrivateStaticFunctionVariables:

>>> dis.dis(testPrivateStaticFunctionVariables)
 21   0 LOAD_DEREF   0 (func)
  3 LOAD_DEREF   1 (internalData)
  6 LOAD_FAST0 (args)
  9 CALL_FUNCTION_VAR1
 12 RETURN_VALUE

At first I was a little confused by this, because there's no increment
in sight, but then I realized it was dis.dis'ing the wrapper closure
in the internalDataDecorator closure in the PrivateDataEngine function
(and then I hit myself on the head and cried out "doh!"). So that
'code' is coming from this (taken out of closure):

def wrapper(*args):
return func(internalData, *args)

So, based on what you showed me, I found my way after some failed
tries to this:

>>> dis.dis(testPrivateStaticFunctionVariables.func_closure[0].cell_contents)
 28   0 LOAD_FAST0 (internalData)
  3 DUP_TOP
  4 LOAD_ATTR0 (numCalls)
  7 LOAD_CONST   1 (1)
 10 INPLACE_ADD
 11 ROT_TWO
 12 STORE_ATTR   0 (numCalls)

 29  15 LOAD_FAST0 (internalData)
 18 LOAD_ATTR0 (numCalls)
 21 RETURN_VALUE

That's coming from this:

@PrivateDataEngine(numCalls = 0)
def testPrivateStaticFunctionVariables(internalData):
"""returns the number of times this function has been called."""
internalData.numCalls += 1
return internalData.numCalls


Here's a few questions on this output, for which I would highly
appreciate some answers:

What's the difference between 'LOAD_DEREF', 'LOAD_FAST', and
'LOAD_CONST', and, as seen at http://docs.python.org/lib/module-dis.html,
'LOAD_GLOBAL'? I can imagine that 'LOAD_GLOBAL' loads a global, but
seeing as python is such a dynamic language, how exactly is it
supposed to distinguish between them?

I don't understand the following at all: 'DUP_TOP', 'ROT_TWO'. Any
pointers?

What does 'INPLACE_ADD' mean, if not in place addition, and if it is
in place addition, why does it need to 'STORE_ATTR' afterward?

Thanks for any help!

> > --
> > Steven

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


Re: encoding - arabic(IBM 864) to UNICODE

2007-03-18 Thread Peter Otten
Madhu Alagu wrote:

> How to convert IBM 864,IBM 420 & Nafitha(Arabic)  to UNICODE.

You can treat them like every other encoding:

>>> s = '\xe1\xec\xf0\xe8\xe1' # *
>>> print s.decode("cp864") # convert from cp864 to unicode
ﻓﻌﹽﻭﻓ
>>> s.decode("cp864").encode("utf8") # convert from cp864 to utf-8
'\xef\xbb\x93\xef\xbb\x8c\xef\xb9\xbd\xef\xbb\xad\xef\xbb\x93'

To read from or write to a file you can codecs.open() which allows you to
specify an encoding.

Peter

(*) these are arbitrary characters generated by 
"".join(chr(ord(c)+128) for c in "alpha") 
since I don't know Arabic.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Private data

2007-03-18 Thread Dustan
On Mar 18, 8:21 am, "Dustan" <[EMAIL PROTECTED]> wrote:
> On Mar 18, 7:25 am, "Dustan" <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Mar 18, 7:06 am, Steven D'Aprano
> > > First, an example of the code in action.
>
> > > >>> import PrivateAttributes
> > > >>> obj = PrivateAttributes.TestPrivateClassAttributes()
> > > >>> obj.getNumInstances()
> > > 1
> > > >>> another = PrivateAttributes.TestPrivateClassAttributes()
> > > >>> obj.getNumInstances()
> > > 2
> > > >>> athird = PrivateAttributes.TestPrivateClassAttributes()
> > > >>> athird.getNumInstances()
>
> > > 3
>
> > > The getNumInstances method reports the number of instances of the
> > > PrivateAttributes class. There's no obvious class attribute where this
> > > count is being kept:
>
> > > >>> obj.__class__.__dict__.keys()
>
> > > ['__module__', 'getNumInstances', '__dict__', '__weakref__', '__doc__',
> > > '__init__']
>
> > > Here's how to hack it, and make it report wrong numbers.
>
> > > >>> c = obj.getNumInstances.func_closure
> > > >>> c[1].cell_contents.numInstances = -300
>
> > > >>> athird.getNumInstances()
> > > -300
> > > >>> afourth = PrivateAttributes.TestPrivateClassAttributes()
> > > >>> athird.getNumInstances()
>
> > > -299
>
> > > So yes, it is absolutely hackable.
>
> > I did have a feeling that it was hackable, but had no idea how one
> > could possibly go about hacking it (I was starting to wonder of there
> > was a way to apply locals() and globals() on functions). But now I
> > (ehem) sorta know how it's done.
>
> > > Now, I'm hardly a Python guru, but in about fifteen minutes I followed the
> > > trail through the object chain, and found how to hack this. An real guru
> > > would probably do it in three minutes.
>
> > > I was helped a bit by having the source code. But even without the source
> > > code, I reckon I could have done it in an hour or so, if I was motivated
> > > enough. All the tools you need are a Python interactive session, the dir()
> > > function and the dis module.
>
> > I have used all of those before, but I haven't been able to fully
> > understand the output of the dis module; maybe that's my problem.
>
> Alright, perhaps you can help me out with this learning curve here. I
> have seen, but not worked with, some basic assembly code, so I think I
> have a vague idea of what it all means, although I have a feeling it's
> not all valid assembly (on any widely used machine).
>
> First I dis.dis'd testPrivateStaticFunctionVariables:
>
> >>> dis.dis(testPrivateStaticFunctionVariables)
>
>  21   0 LOAD_DEREF   0 (func)
>   3 LOAD_DEREF   1 (internalData)
>   6 LOAD_FAST0 (args)
>   9 CALL_FUNCTION_VAR1
>  12 RETURN_VALUE
>
> At first I was a little confused by this, because there's no increment
> in sight, but then I realized it was dis.dis'ing the wrapper closure
> in the internalDataDecorator closure in the PrivateDataEngine function
> (and then I hit myself on the head and cried out "doh!"). So that
> 'code' is coming from this (taken out of closure):
>
> def wrapper(*args):
> return func(internalData, *args)
>
> So, based on what you showed me, I found my way after some failed
> tries to this:
>
> >>> dis.dis(testPrivateStaticFunctionVariables.func_closure[0].cell_contents)
>
>  28   0 LOAD_FAST0 (internalData)
>   3 DUP_TOP
>   4 LOAD_ATTR0 (numCalls)
>   7 LOAD_CONST   1 (1)
>  10 INPLACE_ADD
>  11 ROT_TWO
>  12 STORE_ATTR   0 (numCalls)
>
>  29  15 LOAD_FAST0 (internalData)
>  18 LOAD_ATTR0 (numCalls)
>  21 RETURN_VALUE
>
> That's coming from this:
>
> @PrivateDataEngine(numCalls = 0)
> def testPrivateStaticFunctionVariables(internalData):
> """returns the number of times this function has been called."""
> internalData.numCalls += 1
> return internalData.numCalls
>
> Here's a few questions on this output, for which I would highly
> appreciate some answers:
>
> What's the difference between 'LOAD_DEREF', 'LOAD_FAST', and
> 'LOAD_CONST', and, as seen athttp://docs.python.org/lib/module-dis.html,
> 'LOAD_GLOBAL'? I can imagine that 'LOAD_GLOBAL' loads a global, but
> seeing as python is such a dynamic language, how exactly is it
> supposed to distinguish between them?
>
> I don't understand the following at all: 'DUP_TOP', 'ROT_TWO'. Any
> pointers?
>
> What does 'INPLACE_ADD' mean, if not in place addition, and if it is
> in place addition, why does it need to 'STORE_ATTR' afterward?
>
> Thanks for any help!

If this had been on any other usenet group, someone would have RTFM'd
me to hell.
http://docs.python.org/lib/bytecodes.html
I might still have some more questions later, but for now, I'll look
at this.

> > > --
> > > Steven


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


Re: [Help]UnicodeDecodeError

2007-03-18 Thread Peter Otten
Karl wrote:

> error msg:
> Mod_python error: "PythonHandler mod_python.publisher"
> 
> Traceback (most recent call last):
> 
>   File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line
> 299, in HandlerDispatch
> result = object(req)
> 
>   File "/usr/lib/python2.3/site-packages/mod_python/publisher.py",
> line 136, in handler
> result = util.apply_fs_data(object, req.form, req=req)
> 
>   File "/usr/lib/python2.3/site-packages/mod_python/util.py", line
> 361, in apply_fs_data
> return object(**args)
> 
>   File "/var/www/html/sky.py", line 38, in main
> sresult = sresult.decode('zh-hk').encode('utf_8')
> 
> UnicodeDecodeError: 'big5hkscs' codec can't decode bytes in position
> 958-959: illegal multibyte sequence
> 
> 
> Wt can I do, since there is some characters, is there any solutions to
> solve it???

Other than requiring valid input? 
You can choose a more lenient decoding strategy, e. g.:

sresult = sresult.decode('zh-hk', "replace")

>>> help(s.decode)
Help on built-in function decode:

decode(...)
S.decode([encoding[,errors]]) -> object

Decodes S using the codec registered for encoding. encoding defaults
to the default encoding. errors may be given to set a different error
handling scheme. Default is 'strict' meaning that encoding errors raise
a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
as well as any other name registerd with codecs.register_error that is
able to handle UnicodeDecodeErrors.

Peter

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


screen size/resolution in win32 in python

2007-03-18 Thread adri80386
Hi:

How I can get the current screen resolution settings (screen.width and
screen.heigth in pixels) in python.

Thanks in advance

Adrian

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


Re: IDE for wxPython

2007-03-18 Thread Stef Mientki
Ghirai wrote:
> Hello python-list,
> 
> Can anyone suggest an IDE for wxPython?
> Or an IDE for TkInter?
> 
> Thanks.
> 
on this site, you can test which GUI is best suited for your needs
http://www.awaretek.com/toolkits.html
I think it might be PyhtonCard

-- 
cheers,
Stef Mientki
http://pic.flappie.nl
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Mastering Python

2007-03-18 Thread Aahz
In article <[EMAIL PROTECTED]>,
John Nagle  <[EMAIL PROTECTED]> wrote:
>Alex Martelli wrote:
>> Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
>>>
>>>Mastery and quickly are opposing terms  Took me 15 years on a job
>>>using FORTRAN 77 and I still wouldn't have called myself a master. (I'm
>>>more of a JoAT)
>> 
>> My favorite "Stars!" PRT, mind you -- but when some language interests
>> me enough, I do tend to "master" it... guess it's correlated with what
>> Brooks saw as the ideal "language lawyer" in his "surgical team"
>> approach, an intrinsic fascination with bunches of interconnected rules.
>
>Python just isn't that complicated.  The syntax is straightforward,
>and the semantics are similar to most other dynamic object-oriented
>languages.  If you know Perl or Smalltalk or LISP or JavaScript, Python
>does about what you'd expect.

Yes and no.  At the time I learned Python, I was a Perl expert (but not
a master), and I had a bunch of other languages under my belt (including
Fortran, Pascal, C, Ada).  Nevertheless, for the first month of Python,
I found that I kept having problems because I tried to make Python fit
the mold of other languages rather than accepting it on its own terms.

(Admittedly, part of my problem was that I was learning Python under
duress -- I saw no reason to learn Yet Another Scripting Language.)

Then there are all the little odd corners of Python that stand in the
way of true mastery, like what happens with refcounts and exception
tracebacks.
-- 
Aahz ([EMAIL PROTECTED])   <*> http://www.pythoncraft.com/

"Typing is cheap.  Thinking is expensive."  --Roy Smith
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cannot start IDLE in WinXP

2007-03-18 Thread imx
On 3月18日, 下午5时47分, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
> En Sat, 17 Mar 2007 08:09:16 -0300, imx <[EMAIL PROTECTED]> escribió:
>
> > Enviroment: WinXP sp2, python 2.5
> > problem: click IDLE using shorcut menu or run phthonw.exe directly,
> > nothing happen! But running python.exe from the command line is fine.
>
> pythonw.exe does not open a window so it's almost invisible.
> IDLE now uses a socket to connect to the subprocess which runs the python
> code; perhaps your firewall is blocking that connection. Try starting IDLE
> with the -n switch.
>
> --
> Gabriel Genellina

Thanks for reply, I've already reinstall xp and now IDLE is ok.

-Xiong

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

Re: Webcams and python

2007-03-18 Thread Synt4x
On 18 mar, 04:24, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> In <[EMAIL PROTECTED]>, Synt4x wrote:
> > from VideoCapture import Device
> > cam = Device()
>
> > while 1:
> > img = cam.getImage()
>
> > Now, by doing this, my processor fires up to 100% load, which
> > obviously makes my pc useless.
>
> > Is there any way or algorithm to lower the cpu load?
>
> Just throw a small `time.sleep()` into the loop.
>
> Ciao,
> Marc 'BlackJack' Rintsch

The problem with adding a sleep() instrucction is that the "video
feed" looks lagged, if you know what I mean. It looks interrupted.

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


Re: GCC 4.1.2 installer for Python distutils compilation

2007-03-18 Thread Giovanni Bajo
On 18/03/2007 13.24, David Rushby wrote:

> Even though I have access to MSVC 7.1, so I don't really need MinGW
> myself, [...]

But remember that GCC 4.1.2 is almost 4 years newer than MSVC 7.1, and I found 
it to produce more optimized code (especially for C++). Since it's a free 
alternative, it might be worth to give it a go :)
-- 
Giovanni Bajo
-- 
http://mail.python.org/mailman/listinfo/python-list


XML based programming language

2007-03-18 Thread stefaan
Hello,

I have recently had to deal with an XML-based
programming language. (The programs are
generated programmatically.)

XML leads to a "two-level" parsing problem: first
parse the xml into tokens, then parse the tokens
to grasp their meaning (based on the semantics
of the programming language).

Basically, I used elementtree as a sophisticated "lexer" and wrote a
recursive descent parser to perform the semantic analysis and
interpretation.
(It works great.)

But I keep wondering: do parser generator tools
exist that I could have used instead of writing
the recursive descent parser manually ?

Best regards,
Stefaan.

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


Re: XML based programming language

2007-03-18 Thread Diez B. Roggisch
stefaan schrieb:
> Hello,
> 
> I have recently had to deal with an XML-based
> programming language. (The programs are
> generated programmatically.)
> 
> XML leads to a "two-level" parsing problem: first
> parse the xml into tokens, then parse the tokens
> to grasp their meaning (based on the semantics
> of the programming language).
> 
> Basically, I used elementtree as a sophisticated "lexer" and wrote a
> recursive descent parser to perform the semantic analysis and
> interpretation.
> (It works great.)
> 
> But I keep wondering: do parser generator tools
> exist that I could have used instead of writing
> the recursive descent parser manually ?

You haven't written a recursive descent parser. At least not in the 
sense of the word.

A parser (recursive descent or otherwise) will take a string written in 
the language it accepts, and in the field of programming languages 
usually returns an abstract syntax tree. On which one works - for 
code-generation, interpretation, optimization.

What you wrote is usually called a reducer, the part that traverses the 
tree, rewriting it, transforming it for interpretation and whatnot.

I've been working with tools that use a XML-Schema or DTD and generate 
typed objects from it, that are capable of being deserialized from a 
XML-stream. The better of these tools generate visitors and/or matchers, 
which basically are objects that traverse the generated object tree in 
document order, via typed methods. Something like this (java pseudocode):

class Visitor {

public visit(Object o) {
  if(o instanceof Expr) {
 visit((Expr)o);
  else if(o instanceof SubExpr) {
 visit((SubExpr)o);
  }

public visit(Expr e) {
   for(SubExpr se : e.subExpressions) {
 visit(se);
   }
}

public visit(SubExpr e) {
   // not doing anything
}

}


This visitor you can then subclass, for example to create an interpreter.

All of this is theoretically possible in python, too. Using multimethods 
one can create the dispatching, and so forth.

I'm just not too convinced that it really is worth the effort. A simple 
tag-name-based dispatching scheme, together with the really nice 
ElementTree-api suffices in my eyes. Then you could do something like this:


class Visitor(ojbect):

def visit(self, node):
descent = True
if getattr(self, "visit_%s" % node.tag):
   descent = getattr(self, "visit_%s" % node.tag)(node)
if descent:
   for child in node:
   self.visit(child)


Then for an element "expr" you could define

class Foo(Visitor):
def visit_expr(self, node):
...


HTH,

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


list comprehension help

2007-03-18 Thread [EMAIL PROTECTED]
Hi
I need to process a really huge text file (4GB) and this is what i
need to do. It takes for ever to complete this. I read some where that
"list comprehension" can fast up things. Can you point out how to do
it in this case?
thanks a lot!


f = open('file.txt','r')
for line in f:
db[line.split(' ')[0]] = line.split(' ')[-1]
db.sync()
-- 
http://mail.python.org/mailman/listinfo/python-list


pyCairo

2007-03-18 Thread Jim
I'd like to experiment with pyCairo, but I'm having installation
problems.  I've downloaded cairo 1.4.0, libpng 1.2.8, zlib 1.2.3, and
pycairo 1.4.0, and I've placed the DLLs for cairo, libpng, and zlib in
c:\python25\dlls\.  However, when I run "setup.py install" I get an
error message saying that the installer can't find cairo 1.4.0.  I'm
running Python 2.5 on WinXP.  Any help will be greatly appreciated.

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


Re: list comprehension help

2007-03-18 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>,
[EMAIL PROTECTED] wrote:

> I need to process a really huge text file (4GB) and this is what i
> need to do. It takes for ever to complete this. I read some where that
> "list comprehension" can fast up things. Can you point out how to do
> it in this case?

No way I can see here.

> f = open('file.txt','r')
> for line in f:
> db[line.split(' ')[0]] = line.split(' ')[-1]
> db.sync()

You can get rid of splitting the same line twice, or use `split()` and
`rsplit()` with the `maxsplit` argument to avoid splitting the line at
*every* space character.

And if the names give the right hints `db.sync()` may be a potentially
expensive operation.  Try to call it at a lower frequency if possible.

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


Re: screen size/resolution in win32 in python

2007-03-18 Thread Tim Golden
adri80386 wrote:
> Hi:
> 
> How I can get the current screen resolution settings (screen.width and
> screen.heigth in pixels) in python.

You want the GetSystemMetrics function from the pywin32
packge:


from win32api import GetSystemMetrics
print "width =", GetSystemMetrics (0)
print "height =",GetSystemMetrics (1)


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


Re: list comprehension help

2007-03-18 Thread Daniel Nogradi
> I need to process a really huge text file (4GB) and this is what i
> need to do. It takes for ever to complete this. I read some where that
> "list comprehension" can fast up things. Can you point out how to do
> it in this case?
> thanks a lot!
>
>
> f = open('file.txt','r')
> for line in f:
> db[line.split(' ')[0]] = line.split(' ')[-1]
> db.sync()

What is db here? Looks like a dictionary but that doesn't have a sync method.
If the file is 4GB are you sure you want to store the whole thing into
memory? In case yes and you want to store it in a list then you can
use list comprehension like this:

db = [ line.split(' ')[-1] for line in open('file.txt','r') ]

or

db = [ ( line.split(' ')[0], line.split(' ')[-1] ) for line in
open('file.txt','r') ]

depending on what exactly you want to store. But reading 4GB into
memory will be slow in any case. You can use the timeit module to find
out which method is fastest.

HTH,
Daniel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list comprehension help

2007-03-18 Thread [EMAIL PROTECTED]
On 3/18/07, Daniel Nogradi <[EMAIL PROTECTED]> wrote:
> > I need to process a really huge text file (4GB) and this is what i
> > "list comprehension" can fast up things. Can you point out how to do
> > f = open('file.txt','r')
> > for line in f:
> > db[line.split(' ')[0]] = line.split(' ')[-1]
> > db.sync()
>
> What is db here? Looks like a dictionary but that doesn't have a sync method.
db is a handle for Berkely db that i open with import bsddb

import bsddb
db=bsddb.hashopen('db_filename')

> If the file is 4GB are you sure you want to store the whole thing into
> memory?
I dont want to load it in memory. Once I call the sync() function it
get synced to the disk, and it is not loaded completely.

> use list comprehension like this:
> db = [ line.split(' ')[-1] for line in open('file.txt','r') ]
> or
> db = [ ( line.split(' ')[0], line.split(' ')[-1] ) for line in
> open('file.txt','r') ]
>
> depending on what exactly you want to store.

line.split(' ')[0] is the key and line.split(' ')[-1] is the value.
THat is what I want to store.
Will the second line comprehension work in this case?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list comprehension help

2007-03-18 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Daniel Nogradi
wrote:

>> f = open('file.txt','r')
>> for line in f:
>> db[line.split(' ')[0]] = line.split(' ')[-1]
>> db.sync()
> 
> What is db here? Looks like a dictionary but that doesn't have a sync method.

Shelves (`shelve` module) have this API.  And syncing forces the changes
to be written to disks, so all caching and buffering of the operating
system is prevented.  So this may slow down the program considerably.

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


Re: list comprehension help

2007-03-18 Thread [EMAIL PROTECTED]
On 3/18/07, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> In <[EMAIL PROTECTED]>, Daniel Nogradi
> wrote:
>
> >> f = open('file.txt','r')
> >> for line in f:
> >> db[line.split(' ')[0]] = line.split(' ')[-1]
> >> db.sync()
> >
> > What is db here? Looks like a dictionary but that doesn't have a sync 
> > method.
>
> Shelves (`shelve` module) have this API.  And syncing forces the changes
> to be written to disks, so all caching and buffering of the operating
> system is prevented.  So this may slow down the program considerably.

It is a handle for bsddb

import bsddb
db=bsddb.hashopen('db_filename')
Syncing will defenitely slow down. I will slow that down. But is there
any improvement I can do to the other part the splitting and setting
the key value/pair?
thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list comprehension help

2007-03-18 Thread Daniel Nogradi
> > > I need to process a really huge text file (4GB) and this is what i
> > > "list comprehension" can fast up things. Can you point out how to do
> > > f = open('file.txt','r')
> > > for line in f:
> > > db[line.split(' ')[0]] = line.split(' ')[-1]
> > > db.sync()
> >
> > What is db here? Looks like a dictionary but that doesn't have a sync 
> > method.
>
> db is a handle for Berkely db that i open with import bsddb
>
> import bsddb
> db=bsddb.hashopen('db_filename')
>
> > If the file is 4GB are you sure you want to store the whole thing into
> > memory?
>
> I dont want to load it in memory. Once I call the sync() function it
> get synced to the disk, and it is not loaded completely.
>
> > use list comprehension like this:
> > db = [ line.split(' ')[-1] for line in open('file.txt','r') ]
> > or
> > db = [ ( line.split(' ')[0], line.split(' ')[-1] ) for line in
> > open('file.txt','r') ]
> >
> > depending on what exactly you want to store.
>
> line.split(' ')[0] is the key and line.split(' ')[-1] is the value.
> THat is what I want to store.
> Will the second line comprehension work in this case?

No, I don't think so. I gave that example because you wanted to use
list comprehension and I didn't know what db is but as the name says
it is for lists. Since the bsddb handle is not a list I'm afraid you
won't be able to use list comprehension.

This small thing will speed up your loop but since syncing is IO bound
it won't give you much (the timeit module will tell you how much you
save, if at all):

for line in f:
sp = line.split
db[sp(' ')[0]] = sp(' ')[-1]
db.sync( )

HTH,
Daniel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the insertion point in a list

2007-03-18 Thread 7stud
On Mar 18, 2:23 am, Paul Rubin  wrote:
> Steve Holden <[EMAIL PROTECTED]> writes:
> > >max(i for i,t in enumerate(x) if t <= y)
> > > Those are actually pretty direct.
>
> > How about a solution (like the bisect one suggested almost as soon as
> > this thread started) that doesn't iterate over the whole list.
>
> Here's a Haskell-inspired one:
>
> len(list(itertools.takewhile(lambda t: y > t, x)))
>

Can you explain how list() works in that statement.  I looked up
takewhile() and it returns an iterator that will automatically stop at
the insertion point?  So does list() do an internal comprehension with
the iterator?

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


Re: Finding the insertion point in a list

2007-03-18 Thread Alex Martelli
7stud <[EMAIL PROTECTED]> wrote:

> On Mar 18, 2:23 am, Paul Rubin  wrote:
> > Steve Holden <[EMAIL PROTECTED]> writes:
> > > >max(i for i,t in enumerate(x) if t <= y)
> > > > Those are actually pretty direct.
> >
> > > How about a solution (like the bisect one suggested almost as soon as
> > > this thread started) that doesn't iterate over the whole list.
> >
> > Here's a Haskell-inspired one:
> >
> > len(list(itertools.takewhile(lambda t: y > t, x)))
> 
> Can you explain how list() works in that statement.  I looked up
> takewhile() and it returns an iterator that will automatically stop at
> the insertion point?  So does list() do an internal comprehension with
> the iterator?

Any call to list(iterator) works roughly as follows:

def likelist(iterator):
  result = []
  while True:
try: result.append(iterator.next())
except StopIteration: return result


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


Re: Still the __new__ hell ...

2007-03-18 Thread Paulo da Silva
Arnaud Delobelle escreveu:
> On Mar 17, 11:48 pm, Paulo da Silva <[EMAIL PROTECTED]> wrote:
>> Arnaud Delobelle escreveu:> On Mar 17, 9:31 pm, Paulo da Silva <[EMAIL 
>> PROTECTED]> wrote:
> 
> [snip]
> 
>> Thanks. This works exactly the way you wrote.
>> Yet I am misunderstanding something. Can't pickle "see" that being
>> MyDate derived from date it also has to look at variables from date?
>> When do I need to do this? I am using pickle with a lot more complex
>> classes without this problem.
> 
> Without the MyDate.__reduce__ method, Python uses the
> datetime.date.__reduce__ method to pickle your MyDate instances, then
> it uses MyDate.__new__ to unpickle them.  But your MyDate.__new__
> method does not understand the format of a pickled date.  You could
> also change the MyDate.__new__ method so that it does understand it,
> but it wouldn't be that easy as you want it to accept a string, and
> this is the format that dates are pickled in.
> 
> --
> Arnaud
> 
> 
> 

I see now. I need to read a little further about this stuff as soon as
I get some time to do it.

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


Re: list comprehension help

2007-03-18 Thread Alex Martelli
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:

> On 3/18/07, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> > In <[EMAIL PROTECTED]>, Daniel Nogradi
> > wrote:
> >
> > >> f = open('file.txt','r')
> > >> for line in f:
> > >> db[line.split(' ')[0]] = line.split(' ')[-1]
> > >> db.sync()
> > >
> > > What is db here? Looks like a dictionary but that doesn't have a sync
> > >method.
> >
> > Shelves (`shelve` module) have this API.  And syncing forces the changes
> > to be written to disks, so all caching and buffering of the operating
> > system is prevented.  So this may slow down the program considerably.
> 
> It is a handle for bsddb
> 
> import bsddb
> db=bsddb.hashopen('db_filename')
> Syncing will defenitely slow down. I will slow that down. But is there
> any improvement I can do to the other part the splitting and setting
> the key value/pair?

Unless each line is huge, how exactly you split it to get the first and
last blank-separated word is not going to matter much.

Still, you should at least avoid repeating the splitting twice, that's
pretty obviously sheer waste: so, change that loop body to:

words = line.split(' ')
db[words[0]] = words[-1]

If some lines are huge, splitting them entirely may be far more work
than you need.  In this case, you may do two partial splits instead, one
direct and one reverse:

first_word = line.split(' ', 1)[0]
last_word = line.rsplit(' ', 1][-1]
db[first_word] = last_word

You could also try to extract the first and last words by re or direct
string manipulations, but I doubt that would buy you much, if any,
performance improvement in comparison to the partial-splits.  In the
end, only by "benchmarking" (measuring performance on sample data of
direct relevance to your application) can you find out.


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


Re: encoding - arabic(IBM 864) to UNICODE

2007-03-18 Thread Mohammad Tayseer
text_864 = "\xd1"
unicode_string = unicode(text_864, encoding="cp864") # cp864 = IBM 864

The above code is working with Python 2.4.

Unfortunately, it doesn't support IBM 420 & Nafitha. The mapping between IBM 
864 and unicode is in Lib/encodings & the file is very simple. You can add your 
own easily, if you knew how to do the decoding first ;)




Mohammad Tayseer
http://spellcoder.com/blogs/tayseer

 
-
Don't be flakey. Get Yahoo! Mail for Mobile and 
always stay connected to friends.-- 
http://mail.python.org/mailman/listinfo/python-list

Optimization problem

2007-03-18 Thread cesco
I have a set of N blocks of different lengths. The length of each
block is a multiple of a basic unit. The blocks, once lined up, make a
path of distance equal to R. Let's say we have 5 blocks with the
following lengths: N_set_lengths = (1, 3, 2, 1, 3), then the path we
cover by lining them up is equal to R = 10.
Now, each of these blocks is associated with a different metric m
(metric) which depends on the location/position that the block
occupies within the path. So a block  of length 1 will have R = 10
different m values, one for each of the 10 positions it can occupy
within the path, while a block of length 3 will have R-3+1 = 8
different m values.

Here is a graphical representation:
 

block 0:  |m0,0|m0,1|m0,2|m0,3|m0,4|m0,5|m0,6|m0,7|m0,8|m0,9|
(length 1, 10 different metrics)
 

 
---
block 1:  |m1,0|m1,1|m1,2|m1,3|m1,4|m1,5|m1,6|m1,7|   |
|  (length 3, 8 different metrics, each metric
 
---
refers to 3 consecutive units)
 
---
block 2:  |m2,0|m2,1|m2,2|m2,3|m2,4|m2,5|m2,6|m2,7|m2,8|   |
(length 2, 9 different metrics, each
 
---
referring to 2 consecutive units)
 
---
block 3:  |m3,0|m3,1|m3,2|m3,3|m3,4|m3,5|m3,6|m3,7|m3,8|m3,9|
(length 1, 10 different metrics)
 
---
 
---
block 4:  |m4,0|m4,1|m4,2|m4,3|m4,4|m4,5|m4,6|m4,7|   |
|  (length 3, 8 different metrics)
 
---

Those blocks can be allocated in fact(N) possible different ways to
cover the path (In the example considered I have 5 possible blocks to
choose from to cover the first part of the path, 4 blocks to cover the
second part of the path, and so on).

Each of these possible combinations results in a different overall
metric which we can define as the sum of the individual metrics that
each block gets according to the position occupied within the path.
There is at least one combination which is the optimum solution,
because it maximizes the overall metric. Finding such a combination is
possible but it may require a long processing time.

If, for example, the number of blocks is 20 the number of possible
combinations is expressed as 2.4*10^18. In the application I'm
considering the time is really a constraint so I'm trying to find an
algorithm which would give a near optimum solution but with a much
lower complexity.

Does anyone have suggestion on how should such an algorithm behave
(possibly considering implementation issues)?

Sorry for the lengthy description, I was just trying to be as clear as
possible.
Please, don't hesitate to  ask questions if the problem statement is
not clear.

Many thanks and regards
Francesco

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


Re: Eureka moments in Python

2007-03-18 Thread Mohammad Tayseer

> I'd be interested in hearing people's stories of Eureka moments 
> in Python, moments where you suddenly realise that some task 
> which seemed like it would be hard work was easy with Python.


I had a project in the faculty where we were supposed to implement a compiler 
for a very simple language (no functions, etc).

I was responsible for implementing the compiler itself & another colleage was 
responsible for making an editor for the language - a bonus feature. I was 
still learning Python & my colleage only heard about it. I implemented the 
language completely within 4 days. My colleage learnt Python & Tkinter and made 
a text editor with syntax highlighting & auto-indentation in 3 days. We 
implemented all the bonus features while other groups was still fighting with 
lex & yacc just to get it to *run* right. We were the highest ranked group :)

The next year, the teaching assistant refused that any project be done in 
Python because it is so easy :(

Part of the productivity boos came from pyggy, a very simple yet powerful 
parser generator that made writing the compiler clear as exactly as our course 
note said :)


Mohammad Tayseer
http://spellcoder.com/blogs/tayseer

 
-
Never miss an email again!
Yahoo! Toolbar alerts you the instant new Mail arrives. Check it out.-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Webcams and python

2007-03-18 Thread sturlamolden
On Mar 18, 3:41 pm, "Synt4x" <[EMAIL PROTECTED]> wrote:

> The problem with adding a sleep() instrucction is that the "video
> feed" looks lagged, if you know what I mean. It looks interrupted.

You could try win32api.Sleep(0) instead, which will release the
reminder of the time slice.

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


Re: How to get the previous line in a file?

2007-03-18 Thread Mel Wilson
Qilong Ren wrote:
> Hi, Shane,
> 
> Thanks for fast reply.
> 
> What I used is :
>for line in open(FILE):
>
> I don't want to store all lines in a list because sometimes the file is very 
> large. We need to store the value of the previous line in a variable. Is that 
> right? 

Almost.  Strictly, we need to bind a name to the object holding the 
previous value, so we can refer to that object later.  :)

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


Re: Webcams and python

2007-03-18 Thread Synt4x
On 18 mar, 14:17, "sturlamolden" <[EMAIL PROTECTED]> wrote:
> On Mar 18, 3:41 pm, "Synt4x" <[EMAIL PROTECTED]> wrote:
>
> > The problem with adding a sleep() instrucction is that the "video
> > feed" looks lagged, if you know what I mean. It looks interrupted.
>
> You could try win32api.Sleep(0) instead, which will release the
> reminder of the time slice.

I haven't been able to find the win32api extension, but i've read on
the web that time.sleep() calls win32api.Sleep().

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


Renaming or Overloading In Python

2007-03-18 Thread gamename
Hi,

I'm a recent convert from TCL.  One of the more powerful aspects of
TCL is the ability to rename a function at will (generally for testing
purposes).

Example from the tcl doc:

rename ::source ::theRealSource
set sourceCount 0
proc ::source args {
global sourceCount
puts "called source for the [incr sourceCount]'th time"
uplevel 1 ::theRealSource $args
}

So, is such a thing possible in Python?

Thanks,
-T

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


Re: Optimization problem

2007-03-18 Thread mkPyVS
I would suggest taking a look at the python package networkx. It is a
wonderfully created path optimization and presentation package which
has a fair amount of extensabilty.

Basic lowest cost path solutions should be able to solve your
algorithmic needs here in polynomial time (Typically N**P time..
compute time through path to the number of nodes power) which is
typically adequate for most functional needs ( < 1 sec for 20 nodes)
and presents a simple solution path unless the path you present has
many loop options or path costs which are mal-formed. Good Luck,

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


Re: Renaming or Overloading In Python

2007-03-18 Thread Diez B. Roggisch
gamename schrieb:
> Hi,
> 
> I'm a recent convert from TCL.  One of the more powerful aspects of
> TCL is the ability to rename a function at will (generally for testing
> purposes).
> 
> Example from the tcl doc:
> 
> rename ::source ::theRealSource
> set sourceCount 0
> proc ::source args {
> global sourceCount
> puts "called source for the [incr sourceCount]'th time"
> uplevel 1 ::theRealSource $args
> }
> 
> So, is such a thing possible in Python?


def foo():
 print 'foo'

bar = foo

bar()

Is it that what you mean?

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


Re: Renaming or Overloading In Python

2007-03-18 Thread Paul McGuire
On Mar 18, 2:17 pm, "gamename" <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm a recent convert from TCL.  One of the more powerful aspects of
> TCL is the ability to rename a function at will (generally for testing
> purposes).
>
> Example from the tcl doc:
>
> rename ::source ::theRealSource
> set sourceCount 0
> proc ::source args {
> global sourceCount
> puts "called source for the [incr sourceCount]'th time"
> uplevel 1 ::theRealSource $args
>
> }
>
> So, is such a thing possible in Python?
>
> Thanks,
> -T

If you do this sort of thing a lot, you might read up on Python
decorators.  Using a simple syntax, you can easily wrap a function
with debugging/logging, synchronization, or any other wrapper
behavior.  Here is your function counter implemented as a decorator:

def functionCounter(fn):
fname = fn.__name__
def tempFn(*args):
tempFn._callCount += 1
print "Calling %s for the %d'th time" % (fn.__name__,
tempFn._callCount)
fn(*args)
print "Called %s for the %d'th time" % (fn.__name__,
tempFn._callCount)
tempFn._callCount = 0
tempFn.__name__ = fn.__name__
return tempFn

@functionCounter
def foo():
print "body of foo"

foo()
foo()
foo()

Prints out:
Calling foo for the 1'th time
body of foo
Called foo for the 1'th time
Calling foo for the 2'th time
body of foo
Called foo for the 2'th time
Calling foo for the 3'th time
body of foo
Called foo for the 3'th time


What is happening here is that, at module import time, the function
functionCounter is called, passing it the function foo.  The @
decorator is a shortcut for this statement, entered after foo is
defined:

foo = functionCounter(foo)

You can find more decorators at the Python wiki:
http://wiki.python.org/moin/PythonDecoratorLibrary

-- Paul



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


Re: getopt or optparse options/arguments wrapping?

2007-03-18 Thread Steven Bethard
Rocky Zhou wrote:
> .dirs .files is just a snapshot of the current directories, which can
> be used to delete-outdated files when restoring. Here I used absolute
> path by using tar's -P parameter. When fs_rstore, it will do this:
> command = "tar -xpz -P -f %s.tgz -T %s" % (archive, self.t_files)
> 
> maybe I want add some parameters of tar, for example, --strip-path=X,
> so I hope the fs_rstore can do this:
> sh# fs_rstore pages --strip-path=X
> rather than:
> sh# fs_rstore pages -w '--strip-path=X'
> 
> What should I do?

One possibility using argparse (http://argparse.python-hosting.com/) 
would be to require the '--' pseudo-argument before all tar options. 
Then you could do something like::

 fs_rstore pages -- --strip-path=X --same-owner

That '--' pseudo-argument makes everything else after it into a 
positional argument (even if they start with '-' or '--'), so you can 
then just collect those positional arguments and add them to your 
"command".   Here's what that code might look like::

 >>> parser = argparse.ArgumentParser(prog='fs_backup')
 >>> parser.add_argument('-a', '--append', metavar='[t/x[L]:]PATH')
 >>> parser.add_argument('-d', '--delete', metavar='PATH')
 >>> parser.add_argument('-t', '--type',
 ... choices=['full', 'diff', 'incr'])
 >>> parser.add_argument('identity')
 >>> parser.add_argument('tar_options', nargs='*')
 >>> parser.parse_args('-a foo pages -- --strip-path=X')
 >>> args = parser.parse_args(
 ... '-a foo pages -- --strip-path=X --same-owner'.split())
 >>> args.append
 'foo'
 >>> args.identity
 'pages'
 >>> args.tar_options
 ['--strip-path=X', '--same-owner']
 >>> "tar %s -f %s.tgz" % (' '.join(args.tar_options), args.identity)
 'tar --strip-path=X --same-owner -f pages.tgz'

Note that all the tar options got collected in 'args.tar_options'.

You could get rid of the need for '--' by adding each tar options to 
your parser with an add_argument() call, but I suspect that's too 
tedious. I've added a feature request to argparse to make this kind of 
thing easier::
 http://argparse.python-hosting.com/ticket/28
Not sure when I'll have a chance to look into this though.

STeVe

P.S. You should be able to get similar behavior out of optparse, though 
you'll have to parse the 'args' list yourself.
-- 
http://mail.python.org/mailman/listinfo/python-list


Stuck with pyqt: can't get/read button IDs from a qbuttongroup

2007-03-18 Thread raacampbell
Hi,

I'm learning Python and QT and have set myself the task of writing a
simple calculator applet. I'm using Qt3 and with the designer have
made a button group which contains 10 buttons to allow the user to
press the digits 0 to 9.

The idea is that the button group is associated with a single slot.
When I click a button the qbuttongroup sends a signal to a slot I've
named getNumber. At the moment I just want this to read buttonGroupid
of the clciked button and write this to the screen (the buttonGroupid
is equal to the value the user wishes to select). So if the user
clicks on, say, button "7" then this digit will be printed to the
listbox. Problem is that I can't work out how to read this ID. I can't
believe it's this hard--I have RTFM, some programing experience, and
have searched google for 3 days. No joy.

The best I've managed so far is this line from the .ui.h file
void calc::getNumber(int id)
{
self.listBox1.insertItem(str(id))
}

which translates to this in the .py:
def getNumber(self,a0):
self.listBox1.insertItem(str(id))


All that does is is print the text "" to my list
box.

What am I missing?

Thanks!

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


Re: Stuck with pyqt: can't get/read button IDs from a qbuttongroup

2007-03-18 Thread raacampbell


> which translates to this in the .py:
> def getNumber(self,a0):
> self.listBox1.insertItem(str(id))
>

As is typically the way with these things, I've just solved it. I
changed the text in the file generated by pyui. It now reads:

   def getNumber(self,a0):
self.listBox1.insertItem(a0)


I suppose my question now becomes whether or not editing the .py file
in this manner is good thing to be doing. What references do people
recomend I consult with regard to this sort of thing?

Thanks,
Rob

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


Re: lock problem

2007-03-18 Thread Leo Kislov
On Mar 16, 3:08 pm, Ritesh Raj Sarraf <[EMAIL PROTECTED]> wrote:
> Leo Kislov wrote:
> > But you miss the fact that there is only one environment per process.
>
> Maybe there's a confusion.
> The environment variable that I'm setting has noting to do with ldapsearch. I
> use the environment variable as a filename to which ldapsearch can redirect 
> its
> output. And that I do is because the output can be huge and useless.
> Then I do some pattern matching on that file and filter my data and then 
> delete
> it.
>
> If you think I still am missing something important, request you to describe 
> it.

Imagine this timeline:

 os.environ['__kabc_ldap'] = '/tmp/tmp1'

 os.environ['__kabc_ldap'] = '/tmp/tmp2'
 launch ldapsearch (output goes to '/tmp/tmp2')

 launch ldapsearch (output goes to '/tmp/tmp2' over output
from ldapsearch launched from thread1)

Seems like that's what is happening to your program.

  -- Leo

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


Re: Webcams and python

2007-03-18 Thread sturlamolden
On Mar 18, 8:01 pm, "Synt4x" <[EMAIL PROTECTED]> wrote:

> I haven't been able to find the win32api extension, but i've read on
> the web that time.sleep() calls win32api.Sleep().


I tested VideoCapture using PyGame to draw the graphics. PyGame wraps
SDL which uses DirectDraw on Windows. I don't think it matters much,
but DirectX is known to be faster than e.g. GDI and DIB sections.

With 10 fps, I get around 10-15% CPU usage on my laptop. The video
does not look too bad, but it's not perfect. With 20 fps I get 30-40%
CPU usage, and the video looks very smooth. I don't think my webcam
could do any better anyway. CPU use in not anywhere near saturation.

pygame.time.delay() just calls Sleep() in the Windows API, after
setting time resolution to multimedia mode. So adjust the amount of
time you keep the thread asleep.


import VideoCapture
import pygame
from pygame.locals import *
import sys

fps = 20.0
webcam = VideoCapture.Device()
webcam.setResolution(640,480)
pygame.init()
window = pygame.display.set_mode((640,480))
pygame.display.set_caption("WebCam Demo")
screen = pygame.display.get_surface()
while True:
events = pygame.event.get()
for event in events:
if event.type == QUIT or event.type == KEYDOWN:
sys.exit(0)
im = webcam.getImage()
pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode)
screen.blit(pg_img, (0,0))
pygame.display.flip()
pygame.time.delay(int(1000 * 1.0/fps))


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


* operator--as in *args?

2007-03-18 Thread 7stud
Hi,

I can't find any documentation for the * operator when applied in
front of a name.  Is it a pointer?

What about the @ operator?

Are there python names for these operators that would make searching
for documentation on them more fruitful?

Thanks

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


Re: cheese shop: tagging and dating

2007-03-18 Thread Richard Jones
Jorge Godoy wrote:
> "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
>> The first thing I look at when examining a module is how often it is
>> updated. Unfortunately, the entries there dont show this. Eg:
>>
>>   http://www.python.org/pypi/PySimpleXML/1.0
>>
>> Second, it seems that tagging is more popular than the hierarchical
>> browsing method currently offered by Cheese Shop. Are there efforts
>> underway to support a tagging interface for the cheeseshop?
> 
> I believe hierarchies are easier when there's a lot of information.  You
> go browsing by topic.
> 
> If there are only tags available, you'd have to look for hundreds of tags
> to see if none of the possible names for the "thing" has been used
> ("database", "db", "relational", "orm", "mapper", etc.).

Jorge presents a perfect example of why I've always resisted adding
arbitrary keywords as meta-data to the Cheese Shop set.

If you want arbitrary searches like this (say you think that the database
module you want to find has "tagged" itself with "db"), use the search box.
It'll search names and descriptions. And then when you don't find a match,
you can try "database" and then "relational" and then ... or you could use
the browse interface to search using the one valid term, "Topic ::
Database"

This meta-data isn't about enabling package maintainer artistic creativity
in describing packages, it's about making packages discoverable for end
users.


Richard

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


Re: list comprehension help

2007-03-18 Thread cptnwillard
I wonder whether the following be more efficient if DB was a
dictionnary:

Splits = (line.split(' ') for line in open('file.text', 'r'))
DB = dict([(S[0], S[-1]) for S in Splits])

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


Re: * operator--as in *args?

2007-03-18 Thread Dustan
On Mar 18, 3:53 pm, "7stud" <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I can't find any documentation for the * operator when applied in
> front of a name.  Is it a pointer?
>
> What about the @ operator?
>
> Are there python names for these operators that would make searching
> for documentation on them more fruitful?
>
> Thanks

For the *star, see apply here:
http://docs.python.org/lib/non-essential-built-in-funcs.html

For example:
aFunction(*(1,2,3))
is equivalent to:
aFunction(1,2,3)

For the @ symbol, I assume you mean function decorators:
http://docs.python.org/ref/function.html

For example:
@someDecorator
def someFunc(): pass
is equivalent to:
def someFunc(): pass
someFunc = someDecorator(someFunc)

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


Building several parsing modules

2007-03-18 Thread Robert Neville
Basically, I want to create a table in html, xml, or xslt; with any
number of regular expressions; a script (Perl or Python) which reads
each table row (regex and replacement); and performs the replacement
on any file name, folder, or text file (e.g. css, php, html).  For
example, I often rename my mp3 (files); the folder holding the mp3
files; and replace these renamed values in a playlist/m3u/xml file. 

The table should hold clean regular expressions with minimal escaping.
The regular expressions would incorporate multiple lines and complex
expressions (e.i. symbolic grouping, back referencing, negative
lookahead). The table would serve as a preset file for any replacement
task. It also contains short description column for each regular
expression. The table could contain 1 to 1000 regular expressions; and
the input file could have 1000 to ten thousand lines as well. SED
would become messy here.

I am just starting out with building the logic and pseudo-code.  I am
hoping for any examples where these libraries have been applied. Links
and guides would help since I am just starting out with the language.
I need suggestions and examples on reading input by line; managing
large data sets; iterating through an xml/html structure; and various
parsing techniques. 

I built a solution in VBScript and VBA, but it had several limitations
like operating on one platform and did not have full Perl regular
expression support. In addition, it is attached to an Access database.
The solution would parse and add headers to the data. It would parse
the data with the headers and insert it into a table. It had over
fifteen modules for repetitive parsing tasks to build a importable
data set. VBScript Regexes are not as powerful as Perl or even sed.

This request is large, yet someone with command of the language could
give guidance on the basic framework to kickstart my efforts.
Basically, I need someone to say start here; then proceed to this
function; then look into these libraries; so on. 
-- 
http://mail.python.org/mailman/listinfo/python-list


python QT or python-GTK

2007-03-18 Thread Jon Van DeVries
** All the posts found in google are old.  I'm assuming new improvements have 
been made to both IDEs. **

Please correct me if I'm wrong, I'm a newbie.

1. Which one of them requires fewer lines to accomplish the same thing?
from what I understand QT it's just like Borland J-Builder. Meaning, you want a 
button, you draw it, then you double-click on it, a window opens up and you 
type events and behavior.
And with GTK, you just type everything.

2. Which one is cross platform? (Linux, MacOS, Windows,etc).

3. Which one has more widgets?

4. Which one is the easiest to pick up?  I tried perl-QT and oh boy that mother 
is cryptic.

5. Which one has a bigger support community?

6. or if you think both pythonQT and PythonGTK are junk please suggest the one 
you like/use? I work on a Linux platform with Python 2.4.

Thanks guys, I appreciate  your help.


--=  Posted using GrabIt  =
--=  Binary Usenet downloading made easy =-
-=  Get GrabIt for free from http://www.shemes.com/  =-

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


Re: python QT or python-GTK

2007-03-18 Thread Phil Thompson
On Sunday 18 March 2007 9:55 pm, Jon Van DeVries wrote:
> ** All the posts found in google are old.  I'm assuming new improvements
> have been made to both IDEs. **
>
> Please correct me if I'm wrong, I'm a newbie.
>
> 1. Which one of them requires fewer lines to accomplish the same thing?
> from what I understand QT it's just like Borland J-Builder. Meaning, you
> want a button, you draw it, then you double-click on it, a window opens up
> and you type events and behavior. And with GTK, you just type everything.
>
> 2. Which one is cross platform? (Linux, MacOS, Windows,etc).
>
> 3. Which one has more widgets?
>
> 4. Which one is the easiest to pick up?  I tried perl-QT and oh boy that
> mother is cryptic.
>
> 5. Which one has a bigger support community?
>
> 6. or if you think both pythonQT and PythonGTK are junk please suggest the
> one you like/use? I work on a Linux platform with Python 2.4.

A good start would be to appreciate that both PyQt and PyGTK are GUI toolkits. 
Neither are IDEs.

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


Re: Grep Equivalent for Python

2007-03-18 Thread tereglow
On Mar 15, 1:47 am, [EMAIL PROTECTED] (Alex Martelli) wrote:
> tereglow <[EMAIL PROTECTED]> wrote:
>
>...
>
> > server using the /proc FS.  For example, in order to obtain the amount
> > of physical memory on the server, I would do the following in shell:
>
> >grep^MemTotal /proc/meminfo | awk '{print $2}'
>
> If you would indeed do that, maybe it's also worth learning something
> more about the capabilities of your "existing" tools, since
>
>   awk '/^MemTotal/ {print $2}' /proc/meminfo
>
> is a more compact and faster way to perform exactly the same task.
>
> (You already received a ton of good responses about doing this in
> Python, but the "pipegrepinto awk instead of USING awk properly in the
> first place!" issue has been a pet peeve of mine for almost 30 years
> now, and you know what they say about old dogs + new tricks!-).
>
> Alex

I had no idea you could do that.  Thanks for the tip, I need to start
reading that awk/sed book collecting dust on my shelf!


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


Trying to print from inside a method

2007-03-18 Thread AWasilenko
I'm still in the process of learning python via a handful of books I
bought.  One book I am reading just introduced Base Class Methods.  I
found that I needed more understanding on this concept and wrote a
short test program using the Author's code as a vague reference.  My
question now really isn't Base Class related, but my question stems
from my test code so I will just post it as is.

##Test of Super() stuff
class First(object):

def __init__(self, wamba, nextel):
self.wamba = wamba
self.nextel = nextel
message1 = "This is message 1"
print  message1

def message22(self):
message2 = "This is message 2"
print message2

class Second(First):

def __init__(self, samba, fextel, sony):
super(Second, self).__init__(samba, fextel)
self.sony = sony
print "This is message 1a"


test1 = First(wamba = "Mermaid Man", nextel = "Cell Phone")
test2 = Second(samba = "Barnical Boy", fextel = "Hooopla", sony =
"Nooo Good!")

My question came up when I wanted to print message1 directly and
couldn't figure out how to do it.  I then added the message22 def
thinking that perhaps you couldn't print from constructor methods for
some reason.  I then added the print line in the message22 def, just
out of desperation, and was able to make that work, but is not what I
wanted.  Here is my copy from my IDEL attempts:

This is message 1
This is message 1
This is message 1a
>>> test1.message22()
This is message 2
>>> test2.message22()
This is message 2
>>> print test1.message22.message2

Traceback (most recent call last):
  File "", line 1, in -toplevel-
print test1.message22.message2
AttributeError: 'function' object has no attribute 'message2'
>>> print test1.message2

Traceback (most recent call last):
  File "", line 1, in -toplevel-
print test1.message2
AttributeError: 'First' object has no attribute 'message2'
>>> print First.message22.message2

Traceback (most recent call last):
  File "", line 1, in -toplevel-
print First.message22.message2
AttributeError: 'function' object has no attribute 'message2'
>>>

I know this is a very basic question but it's the stupid ones that
always get me.

- Adam

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


where function

2007-03-18 Thread vorticitywolfe
Is there a function in Python analogous to the "where" function in
IDL?

x=[0,1,2,3,4,2,8,9]
print where(x=2)

output:
[2,5]

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


fifo queue

2007-03-18 Thread drochom
hi,

how would u improve this code?

class queue:
def __init__(self, size=8):
self.space = size
self.data = [None]*self.space
self.head = 0
self.tail = 0
self.len = 0
def __len__(self): return self.len
def push(self, x):
if self.len==self.space:
self.data.extend( self.data[:self.tail] )
self.data.extend( [None]* (self.space-self.tail) )
self.tail+=self.space
self.space*=2
self.data[self.tail]=x
self.tail+=1
if self.tail==self.space:
self.tail=0
self.len+=1
def pop(self):
if self.len:
elem = self.data[self.head]
self.head+=1
if self.head==self.space:
self.head=0
return elem
def top(self):
if self.len==0:
raise Exception, 'queue is empty'
return self.data[self.head]

thx in adv.

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


Re: where function

2007-03-18 Thread bearophileHUGS
vorticitywo:
> Is there a function in Python analogous to the "where" function in
> IDL?

Python gives very elastic syntax, you can simply do:

data = [0,1,2,3,4,2,8,9]
print [pos for pos, el in enumerate(data) if el==2]

Bye,
bearophile

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


Re: where function

2007-03-18 Thread drochom
On 18 Mar, 15:19, [EMAIL PROTECTED] wrote:
> Is there a function in Python analogous to the "where" function in
> IDL?
>
> x=[0,1,2,3,4,2,8,9]
> print where(x=2)
>
> output:
> [2,5]

You can try this:

print filter( lambda x: a[x]==2, range(len(a)))

However it's not the best solution...

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


Re: fifo queue

2007-03-18 Thread Paul Rubin
Unless the queue is really large, just use the pop operation to get
stuff off the top of the queue.  That causes O(n) operations but it
should be fast if n is small.

class queue(list):
push = append
def pop(self):
return list.pop(self,0)

should do about what you wrote.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: where function

2007-03-18 Thread vorticitywolfe
On Mar 18, 10:48 pm, [EMAIL PROTECTED] wrote:
> vorticitywo:
>
> > Is there a function in Python analogous to the "where" function in
> > IDL?
>
> Python gives very elastic syntax, you can simply do:
>
> data = [0,1,2,3,4,2,8,9]
> print [pos for pos, el in enumerate(data) if el==2]
>
> Bye,
> bearophile

Thank you both, a little more cumbersome than I expected, but it does
the job! Thanks!

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


Re: where function

2007-03-18 Thread Shane Geiger
a = 
[0,1,2,3,4,2,8,9]   

   

# method 
1  

print [i for i in xrange(len(a)) if 
a[i]==2]

   

def 
where(a,val):   

   return [i for i in xrange(len(a)) if 
a[i]==val] 

   

# method 
2  

print 
where(a,2)

   






[EMAIL PROTECTED] wrote:

On Mar 18, 10:48 pm, [EMAIL PROTECTED] wrote:
  

vorticitywo:



Is there a function in Python analogous to the "where" function in
IDL?
  

Python gives very elastic syntax, you can simply do:

data = [0,1,2,3,4,2,8,9]
print [pos for pos, el in enumerate(data) if el==2]

Bye,
bearophile



Thank you both, a little more cumbersome than I expected, but it does
the job! Thanks!

  


--
Shane Geiger
IT Director
National Council on Economic Education
[EMAIL PROTECTED]  |  402-438-8958  |  http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

begin:vcard
fn:Shane Geiger
n:Geiger;Shane
org:National Council on Economic Education (NCEE)
adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States
email;internet:[EMAIL PROTECTED]
title:IT Director
tel;work:402-438-8958
x-mozilla-html:FALSE
url:http://www.ncee.net
version:2.1
end:vcard

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

Re: where function

2007-03-18 Thread Steven Bethard
[EMAIL PROTECTED] wrote:
> Is there a function in Python analogous to the "where" function in
> IDL?
> 
> x=[0,1,2,3,4,2,8,9]
> print where(x=2)
> 
> output:
> [2,5]

If you're doing a lot of this kind of thing, you probably want to use 
numpy::

 >>> import numpy
 >>> x = numpy.array([0, 1, 2, 3, 4, 2, 8, 9])
 >>> numpy.where(x == 2)
 (array([2, 5]),)

You can find numpy here:

 http://numpy.scipy.org/

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


Re: where function

2007-03-18 Thread Scott David Daniels
[EMAIL PROTECTED] wrote:
> On Mar 18, 10:48 pm, [EMAIL PROTECTED] wrote:
>> [...fine solutions to the problem as asked...]
> Thank you both, a little more cumbersome than I expected, but it does
> the job! Thanks!

The obvious simple near-equivalent is:

 data = range(33,99)
 print data.index(45)

And "generalized":
 data = [x % 9 for x in range(30)]
 result = []
 former = -1
 try:
 while True:
 former = data.index(3, former + 1)
 result.append(former)
 except ValueError:
 print result
 else:
 print 'Nothing found'

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


Re: fifo queue

2007-03-18 Thread Roel Schroeven
drochom schreef:
> hi,
> 
> how would u improve this code?
> 
> class queue:
 > ...

I think I'd use collections.deque [1] instead of using a ring buffer as 
you're doing (I think) and doing a lot of manual bookkeeping.

[1] http://docs.python.org/lib/deque-objects.html



-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

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


Re: Grep Equivalent for Python

2007-03-18 Thread Aahz
In article <[EMAIL PROTECTED]>,
tereglow <[EMAIL PROTECTED]> wrote:
>On Mar 15, 1:47 am, [EMAIL PROTECTED] (Alex Martelli) wrote:
>> tereglow <[EMAIL PROTECTED]> wrote:
>>>
>>>grep^MemTotal /proc/meminfo | awk '{print $2}'
>>
>> If you would indeed do that, maybe it's also worth learning something
>> more about the capabilities of your "existing" tools, since
>>
>>   awk '/^MemTotal/ {print $2}' /proc/meminfo
>>
>> is a more compact and faster way to perform exactly the same task.
>>
>> (You already received a ton of good responses about doing this in
>> Python, but the "pipegrepinto awk instead of USING awk properly in the
>> first place!" issue has been a pet peeve of mine for almost 30 years
>> now, and you know what they say about old dogs + new tricks!-).
>
>I had no idea you could do that.  Thanks for the tip, I need to start
>reading that awk/sed book collecting dust on my shelf!

Your other option is to completely abandon awk/sed.  I started writing
stuff like this in Turbo Pascal back in the early 80s because there
simply wasn't anything like awk/sed available for CP/M.  In the 90s, when
I needed to do similar things, I used Perl.  Now I use Python.

>From my POV, there is really no reason to learn the advanced shell
utilities.
-- 
Aahz ([EMAIL PROTECTED])   <*> http://www.pythoncraft.com/

"Typing is cheap.  Thinking is expensive."  --Roy Smith
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trying to print from inside a method

2007-03-18 Thread Alex Martelli
<[EMAIL PROTECTED]> wrote:

> I'm still in the process of learning python via a handful of books I
> bought.  One book I am reading just introduced Base Class Methods.  I

I believe your problem may be more basic: local variables, istance
attributes, and class variables, are very separate things.

> found that I needed more understanding on this concept and wrote a
> short test program using the Author's code as a vague reference.  My
> question now really isn't Base Class related, but my question stems
> from my test code so I will just post it as is.
> 
> ##Test of Super() stuff
> class First(object):
> 
> def __init__(self, wamba, nextel):
> self.wamba = wamba
> self.nextel = nextel
> message1 = "This is message 1"
> print  message1
> 
> def message22(self):
> message2 = "This is message 2"
> print message2

Here, the message1 and message2 names are LOCAL variables of the
respective methods: each disappear as soon as its method ends.

If you want to make them into INSTANCE attributes, so they'll stick
around for later, assign to self.message1 and self.message2 instead (and
of course use these composite names too if need be).

> Traceback (most recent call last):
>   File "", line 1, in -toplevel-
> print test1.message22.message2
> AttributeError: 'function' object has no attribute 'message2'

this would require making message2 an attribute of another attribute
called message22 -- i.e., the function itself (methods don't have
attributes, but their underlying functions can; the difference is that
separate instances of the method refer to the same underlying function,
so they would all share the same attribute).

IOW, you'd have in the body of message22 to write:

  self.message22.mesage2 = whatever

This would be pretty weird, but legal Python.

This would only work if you CALLED test1.message22() at some point, of
course; the method's body is not executed until you call it.  If you
want this attribute to appear w/o the need to call the method, you need
to assign it in the body of class First after the end of the def for
message22:

  def message22(self): ...whatever...

  message22.message2 = ...whatever else...

Weirder and weirder, but still legal Python.

> >>> print test1.message2
> 
> Traceback (most recent call last):
>   File "", line 1, in -toplevel-
> print test1.message2
> AttributeError: 'First' object has no attribute 'message2'

And this is the one you'd solve by having, in the method's body,

  self.message2 = ...blah blah...

but only after the method has been called (move this assignment to
__init__ if you want it to take action without need to call the method).

> >>> print First.message22.message2
> 
> Traceback (most recent call last):
>   File "", line 1, in -toplevel-
> print First.message22.message2
> AttributeError: 'function' object has no attribute 'message2'

This, again, you'd fix by assigning to the attribute of function
message22, just like the very first case.


To recap: attributes can be accessed on the objects you have assigned
them too (with some latitude: you can access on an instance an attribute
of its class or any baseclass thereof, you can access on a method an
attribute of is underlying function).  Local variables -- assignment you
do within a function (or method) to bare names -- disappear as soon as
the method (or function) is done executing.


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


Re: fifo queue

2007-03-18 Thread Alex Martelli
Paul Rubin  wrote:

> Unless the queue is really large, just use the pop operation to get
> stuff off the top of the queue.  That causes O(n) operations but it
> should be fast if n is small.
> 
> class queue(list):
> push = append
> def pop(self):
> return list.pop(self,0)
> 
> should do about what you wrote.

If it IS large, then:

import collections
class queue(collections.deque):
  push = collections.deque.append
  pop = collections.deque.popleft

could be even better.


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


Re: list comprehension help

2007-03-18 Thread George Sakkis
On Mar 18, 1:40 pm, [EMAIL PROTECTED] (Alex Martelli) wrote:
> [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> > On 3/18/07, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> > > In <[EMAIL PROTECTED]>, Daniel Nogradi
> > > wrote:
>
> > > >> f = open('file.txt','r')
> > > >> for line in f:
> > > >> db[line.split(' ')[0]] = line.split(' ')[-1]
> > > >> db.sync()
>
> > > > What is db here? Looks like a dictionary but that doesn't have a sync
> > > >method.
>
> > > Shelves (`shelve` module) have this API.  And syncing forces the changes
> > > to be written to disks, so all caching and buffering of the operating
> > > system is prevented.  So this may slow down the program considerably.
>
> > It is a handle for bsddb
>
> > import bsddb
> > db=bsddb.hashopen('db_filename')
> > Syncing will defenitely slow down. I will slow that down. But is there
> > any improvement I can do to the other part the splitting and setting
> > the key value/pair?
>
> Unless each line is huge, how exactly you split it to get the first and
> last blank-separated word is not going to matter much.
>
> Still, you should at least avoid repeating the splitting twice, that's
> pretty obviously sheer waste: so, change that loop body to:
>
> words = line.split(' ')
> db[words[0]] = words[-1]
>
> If some lines are huge, splitting them entirely may be far more work
> than you need.  In this case, you may do two partial splits instead, one
> direct and one reverse:
>
> first_word = line.split(' ', 1)[0]
> last_word = line.rsplit(' ', 1][-1]
> db[first_word] = last_word

I'd guess the following is in theory faster, though it might not make
a measurable difference:

first_word = line[:line.index(' ')]
last_word = line[line.rindex(' ')+1:]
db[first_word] = last_word

By the way, a gotcha is that the file iterator yields lines that
retain the newline character; you have to strip it off if you don't
want it, either with .rstrip('\n') or (at least on *n*x) omit the last
character:
last_word = line[line.rindex(' ')+1 : -1]

George

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


Re: list comprehension help

2007-03-18 Thread Alex Martelli
George Sakkis <[EMAIL PROTECTED]> wrote:
   ...
> > Unless each line is huge, how exactly you split it to get the first and
> > last blank-separated word is not going to matter much.
> >
> > Still, you should at least avoid repeating the splitting twice, that's
> > pretty obviously sheer waste: so, change that loop body to:
> >
> > words = line.split(' ')
> > db[words[0]] = words[-1]
> >
> > If some lines are huge, splitting them entirely may be far more work
> > than you need.  In this case, you may do two partial splits instead, one
> > direct and one reverse:
> >
> > first_word = line.split(' ', 1)[0]
> > last_word = line.rsplit(' ', 1][-1]
> > db[first_word] = last_word
> 
> I'd guess the following is in theory faster, though it might not make
> a measurable difference:
> 
> first_word = line[:line.index(' ')]
> last_word = line[line.rindex(' ')+1:]
> db[first_word] = last_word

If the lines are huge, the difference is quite measurable:

brain:~ alex$ python -mtimeit -s"line='ciao '*999" "first=line.split('
',1)[0]; line=line.rstrip(); second=line.rsplit(' ',1)[-1]"
10 loops, best of 3: 3.95 usec per loop

brain:~ alex$ python -mtimeit -s"line='ciao '*999"
"first=line[:line.index(' ')]; line=line.rstrip();
second=line[line.rindex(' ')+1:]"

100 loops, best of 3: 1.62 usec per loop

brain:~ alex$ 

So, if the 4GB file was made up, say, of 859853 such lines, using the
index/rindex approach might save a couple of seconds overall.

The lack of ,1 in the split/rsplit calls (i.e., essentially, the code
originally posted) brings the snippet time to 226 microseconds; here,
the speedup might therefore be of a couple HUNDRED seconds in all.


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


Re: list comprehension help

2007-03-18 Thread Alex Martelli
f<[EMAIL PROTECTED]> wrote:

> I wonder whether the following be more efficient if DB was a
> dictionnary:
> 
> Splits = (line.split(' ') for line in open('file.text', 'r'))
> DB = dict([(S[0], S[-1]) for S in Splits])

You'd still be doing much more splitting work (and behind-the-scene
allocation and freeing of memory) than strictly needed (only meaningful
for lines that are really big).

Also, lose the brackets immediately inside the ( ) in the second line --
no need to take up huge amounts of memory (by a list comprehension) when
you're only going to work on one item at a time anyway (so a genexp
suffices).

The observation that either the line or the S[-1] entry needs to be
subjected to a .rstrip('\n') also still holds here.

Use python -mtimeit to measure the runtimes of various alternatives --
be careful about what you place in -s (initialization, not looped-upon
and not measured) and what you don't (that _will_ be looped upon, and
measured).


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


Re: Renaming or Overloading In Python

2007-03-18 Thread Alex Martelli
gamename <[EMAIL PROTECTED]> wrote:

> Hi,
> 
> I'm a recent convert from TCL.  One of the more powerful aspects of
> TCL is the ability to rename a function at will (generally for testing
> purposes).
> 
> Example from the tcl doc:
> 
> rename ::source ::theRealSource
> set sourceCount 0
> proc ::source args {
> global sourceCount
> puts "called source for the [incr sourceCount]'th time"
> uplevel 1 ::theRealSource $args
> }
> 
> So, is such a thing possible in Python?

Assuming that source is a function previously defined in this scope, the
exactly equivalent Python snippet should be:

theRealSource = source
sourceCount = 0
def source(*args):
  global sourceCount
  sourceCount += 1
  print "called source for the %s'th time" % sourceCount
  return theRealSource(*args)

Others have already offered you other alternatives, but I thought you
might also be interested in a more direct/immediate translation.


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


Re: Trying to print from inside a method

2007-03-18 Thread AWasilenko
On Mar 18, 7:32 pm, [EMAIL PROTECTED] (Alex Martelli) wrote:
> Here, the message1 and message2 names are LOCAL variables of the
> respective methods: each disappear as soon as its method ends.
>
> If you want to make them into INSTANCE attributes, so they'll stick
> around for later, assign to self.message1 and self.message2 instead (and
> of course use these composite names too if need be).

Ah ha, the nugget of info I was forgetting!  Looking back now I can
see why the
author used the self.whatever, and why my code is not working.  When
I'm reading along
all his code looks find and dandy, makes perfect sense; but soon as I
try to do
something on my own quickly find that my understanding of how things
work is very
porous :)

> This would be pretty weird, but legal Python.
> Weirder and weirder, but still legal Python.

This is usually how most of my code turns out :(  I'm afraid if I ever
learn enough
to make a serious program I will be burned at the stake for such un-
pythonic practices :P

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


Re: python QT or python-GTK

2007-03-18 Thread Alan Franzoni
Il Sun, 18 Mar 2007 11:55:47 -1000, Jon Van DeVries ha scritto:

> Please correct me if I'm wrong, I'm a newbie.

Please understand then, that both QT and GTK+ are graphic toolkits. An IDE
is a totally different thing.

Also, please understand that Qt3 and Qt4 exist, and they're quite different
beasts. Qt4 is fairly new.

Qt is GPL or commercial, while GTK+ is LGPL. That means: if you want to use
QT, you must either distribute your app under the GPL, or buy the
commercial license, while you don't have this limit in GTK+.

Both are cross-platform, but you should check for their actual visual
performance wherever they should be employed. Also, GTK+ seems to still
need an X server on macosx (don't know about QT)

Both have IDEs to create GUIs; take a look at Glade or Gazpacho for GTK+.

Also, remember GTK+ is a community project while QT is a fully commercial
project.



-- 
Alan Franzoni <[EMAIL PROTECTED]>
-
Togli .xyz dalla mia email per contattarmi.
Remove .xyz from my address in order to contact me.
-
GPG Key Fingerprint (Key ID = FE068F3E):
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trying to print from inside a method

2007-03-18 Thread Alex Martelli
<[EMAIL PROTECTED]> wrote:
   ...
> > This would be pretty weird, but legal Python.
> > Weirder and weirder, but still legal Python.
> 
> This is usually how most of my code turns out :(  I'm afraid if I ever
> learn enough
> to make a serious program I will be burned at the stake for such un-
> pythonic practices :P

Function attributes are rarely used, because they belong to "the
function object itself", not to any given _call_ to that function, or to
any _method_ that may wrap that function, and so on.  That's what pretty
weird here -- it would not appear that you necessarily want to associate
those attributes with the function object, and yet (if you want them to
access as attributes of the function object) you do of course need to do
that.  Of course, it's somewhat hard to divine "semantic intent of the
programmer" from a totally abstract toy example, so I could be wrong!


Alex


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


Re: Private data

2007-03-18 Thread Gabriel Genellina
En Sun, 18 Mar 2007 10:21:27 -0300, Dustan <[EMAIL PROTECTED]>  
escribió:

 dis.dis(testPrivateStaticFunctionVariables)
>  21   0 LOAD_DEREF   0 (func)
>   3 LOAD_DEREF   1 (internalData)
>   6 LOAD_FAST0 (args)
>   9 CALL_FUNCTION_VAR1
>  12 RETURN_VALUE
>
> What's the difference between 'LOAD_DEREF', 'LOAD_FAST', and
> 'LOAD_CONST', and, as seen at http://docs.python.org/lib/module-dis.html,
> 'LOAD_GLOBAL'? I can imagine that 'LOAD_GLOBAL' loads a global, but
> seeing as python is such a dynamic language, how exactly is it
> supposed to distinguish between them?

The Python interpreter executes a stack-based machine. Look at  
Python/compile.c for the code generation and Python/ceval.c for the code  
execution.
LOAD_CONST pushes a constant onto the stack (all constants are previously  
built and stored as attribute co_consts of the code object). LOAD_FAST and  
LOAD_GLOBAL pushes a local or global variable reference. (The compiler  
knows whether a name is local or not, just by lexical analysis).  
LOAD_DEREF appears to be used for accessing variables inside closures, I'm  
not sure.

> I don't understand the following at all: 'DUP_TOP', 'ROT_TWO'. Any
> pointers?

They manipulate the stack. DUP_TOP duplicates the top entry; ROT_TWO swaps  
the two top entries.

> What does 'INPLACE_ADD' mean, if not in place addition, and if it is
> in place addition, why does it need to 'STORE_ATTR' afterward?

That corresponds to A += B. The compiler can't know if A has or not an  
__iadd__ method; even if A implements __iadd__, that method could return a  
different object, not always A (it *may* try to do the operation inplace,  
but that might not always be possible). When the inplace method is not  
implemented, __add__ is used (so effectively A += B is computed as A = A+B)

-- 
Gabriel Genellina

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


Re: [Edu-sig] minimum age to learn python (a.k.a graphical vs text languages)

2007-03-18 Thread Peter Chase
I used to judge science fairs in the DC area.  There were junior and 
senior divisions, and the junior division age range was from 6th to 9th 
grades.  At the time I got the occasional project written in Basic, many 
of which were very interesting.  I would think that Python would easily 
outclass Basic!  It would depend entirely upon the individual student.  HTH

Brian Blais wrote:
> Hello,
>
> I was wondering what the approximate minimum age to learn python is.  Has 
> anyone had 
> experience teaching middle school students, or elementary school students 
> Python? 
> What brought this up for me is thinking about starting a Lego robots group in 
> a local 
> middle school.  I only teach college, and have little experience with middle 
> school 
> students, so I find it hard to guess what they could actually do.  I started 
> programming when I was about 5th grade, on a Commodore VIC 20 (3.5k RAM!) in 
> basic, 
> but I don't think I am typical.  (Of course, now, you can probably infer my 
> age to 
> within 2 years!  :)  ).
>
>
> I've written something so that students can program in Python syntax to run 
> the Lego 
> Mindstorms robots.  The most commonly used language for these robotos, in the 
> middle 
> school, is Robolab which is entirely graphical.  Although a good program, I 
> find 
> there are some drawbacks:
> 1) Robolab is commercial, and not all schools can afford this above and 
> beyond the 
> price of the lego mindstorms
> 2) Robolab only runs on Mac/Windows, and not Linux, so those schools that 
> have tried 
> to save money on the operating system get whacked there too
> 3) Robolab can *only* do Lego robots.
>
> Although you learn the basic language structures (loops, branching, etc...), 
> because 
> it is graphical, Robolab doesn't translate directly.  Perhaps this is enough 
> for kids 
> to start, but perhaps one can do better.
>
> On the other hand, my pynqc tool (which uses the freely available nqc 
> language for 
> the Lego Mindstorms) is:
> 1) free (in both senses)
> 2) runs on Mac/Linux/Windows
> 3) because you use python syntax, it is easier to go and do other python 
> projects not 
> involving robots
>
> In my mind, this opens up more doors, but it is not graphical.
>
> I wanted to hear responses from people who have experience teaching 
> programming in 
> elementary/middle (or even high) school.  Do graphical languages make a big 
> difference?  Do text-based languages put up barriers to young learners?  Is 
> it no big 
> deal either way?
>
>
>   thanks,
>
>   Brian Blais
>
>   

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


Re: * operator--as in *args?

2007-03-18 Thread 7stud
On Mar 18, 3:40 pm, "Dustan" <[EMAIL PROTECTED]> wrote:
> For example:
> aFunction(*(1,2,3))
> is equivalent to:
> aFunction(1,2,3)
>

That's the context I've seen it in, but written like this:

someFunc(*args)

I played around with it a little bit, and it appears the * operator
unpacks a list, tuple, or dictionary so that each element of the
container gets assigned to a different parameter variable.  Although
with a dictionary, only the keys appear to be assigned to the
parameter variables, e.g.:

def g(a,b,c):
print a, b, c

dict = {"x":10, "y":20, "z":30}
g(*dict)

Is that right?



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


Re: * operator--as in *args?

2007-03-18 Thread Alex Martelli
7stud <[EMAIL PROTECTED]> wrote:

> On Mar 18, 3:40 pm, "Dustan" <[EMAIL PROTECTED]> wrote:
> > For example:
> > aFunction(*(1,2,3))
> > is equivalent to:
> > aFunction(1,2,3)
> >
> 
> That's the context I've seen it in, but written like this:
> 
> someFunc(*args)
> 
> I played around with it a little bit, and it appears the * operator
> unpacks a list, tuple, or dictionary so that each element of the
> container gets assigned to a different parameter variable.  Although
> with a dictionary, only the keys appear to be assigned to the
> parameter variables, e.g.:
> 
> def g(a,b,c):
> print a, b, c
> 
> dict = {"x":10, "y":20, "z":30}
> g(*dict)
> 
> Is that right?

As far as it goes, yes.  More generally, with any iterable x, the *x
construct in function call will pass as positional arguments exactly
those items which (e.g.) would be printed by the loop:
for item in x: print x

[[this applies to iterators, generators, genexps, and any other iterable
you may care to name -- not just lists, tuples, dicts, but also sets,
files open for reading [the items are the lines], etc, etc]].


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


Re: * operator--as in *args?

2007-03-18 Thread Gabriel Genellina
En Sun, 18 Mar 2007 18:40:32 -0300, Dustan <[EMAIL PROTECTED]>  
escribió:

>> I can't find any documentation for the * operator when applied in
>> front of a name.  Is it a pointer?
>
> For the *star, see apply here:
> http://docs.python.org/lib/non-essential-built-in-funcs.html

Also, you can read this section on the Tutorial:
http://docs.python.org/tut/node6.html#SECTION00670
or the more technical Language Reference:
http://docs.python.org/ref/calls.html and  
http://docs.python.org/ref/function.html

>> Are there python names for these operators that would make searching
>> for documentation on them more fruitful?

Uhhhm, none that I can think of :(

-- 
Gabriel Genellina

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


Re: * operator--as in *args?

2007-03-18 Thread Gabriel Genellina
En Sun, 18 Mar 2007 22:21:41 -0300, Alex Martelli <[EMAIL PROTECTED]> escribió:

> 7stud <[EMAIL PROTECTED]> wrote:
>
>> I played around with it a little bit, and it appears the * operator
>> unpacks a list, tuple, or dictionary so that each element of the
>> container gets assigned to a different parameter variable.  Although
>> with a dictionary, only the keys appear to be assigned to the
>> parameter variables, e.g.:
>>
>> def g(a,b,c):
>> print a, b, c
>>
>> dict = {"x":10, "y":20, "z":30}
>> g(*dict)
>>
>> Is that right?
>
> As far as it goes, yes.  More generally, with any iterable x, the *x
> construct in function call will pass as positional arguments exactly
> those items which (e.g.) would be printed by the loop:
> for item in x: print x
>
> [[this applies to iterators, generators, genexps, and any other iterable
> you may care to name -- not just lists, tuples, dicts, but also sets,
> files open for reading [the items are the lines], etc, etc]].

But the language reference says "sequence", not "iterable"  
(http://docs.python.org/ref/calls.html) and a dictionary is not a  
sequence. With Python 2.1 it was an error; it is not with 2.3 (I can't  
test with 2.2 right now)

Python 2.1.3 (#35, Apr  8 2002, 17:47:50) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> def f(*args, **kw):
...   print "args",args
...   print "kw",kw
...
>>> d = {"a":1, "b":2, "c":3}
>>> f(**d)
args ()
kw {'b': 2, 'c': 3, 'a': 1}
>>> f(*d)
Traceback (most recent call last):
   File "", line 1, in ?
TypeError: f() argument after * must be a sequence

If allowing f(*d) is actually the intended behavior, maybe the wording in  
the reference should be updated. If not, f(*d) should still raise an error.

-- 
Gabriel Genellina

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


4 bytes to int

2007-03-18 Thread Andres Martinelli

Hello. Im new in the list, and really new in python.

My simple question is:
I have an array of 4 bytes, and I want to convert it to the int they form.
How do you do that? :s

I've looking throw the web, but with no success.
Hope you can help.

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

Re: list comprehension help

2007-03-18 Thread George Sakkis
On Mar 18, 12:11 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:

> Hi
> I need to process a really huge text file (4GB) and this is what i
> need to do. It takes for ever to complete this. I read some where that
> "list comprehension" can fast up things. Can you point out how to do
> it in this case?
> thanks a lot!
>
> f = open('file.txt','r')
> for line in f:
> db[line.split(' ')[0]] = line.split(' ')[-1]
> db.sync()

You got several good suggestions; one that has not been mentioned but
makes a big (or even the biggest) difference for large/huge file is
the buffering parameter of open(). Set it to the largest value you can
afford to keep the I/O as low as possible. I'm processing 15-25 GB
files (you see "huge" is really relative ;-)) on 2-4GB RAM boxes and
setting a big buffer (1GB or more) reduces the wall time by 30 to 50%
compared to the default value. BerkeleyDB should have a buffering
option too, make sure you use it and don't synchronize on every line.

Best,
George

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


  1   2   >