Re: lxml: traverse xml tree and retrieve element based on an attribute

2009-05-30 Thread Stefan Behnel
byron wrote:
> I am using the lxml.etree library to validate an xml instance file
> with a specified schema that contains the data types of each element.
> This is some of the internals of a function that extracts the
> elements:
> 
> schema_doc = etree.parse(schema_fn)
> schema = etree.XMLSchema(schema_doc)
> 
> context = etree.iterparse(xml_fn, events=('start', 'end'),
> schema=schema)
> 
> # get root
> event, root = context.next()
> 
> for event, elem in context:
> if event == 'end' and elem.tag == self.tag:
> yield elem
> root.clear()

Note that you cannot modify the root element during iterparse() in
lxml.etree. It seems to work for you here, but it's not safe. Here's a
better way to do this.

http://www.ibm.com/developerworks/xml/library/x-hiperfparse/#N100FF

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


Browser based Canvas UI?

2009-05-30 Thread Ken Seehart
A couple years ago I stumbled upon an interesting technology but I can't 
seem to find it, and I can remember what it is called.  Unfortunately 
this makes it difficult to search for.  I am am aware of several partial 
matches (items that meet a subset of the requirement listed below).  
Does anyone know what does /all/ of the following?


1. Works on at least FF and IE on XP and Linux out of the box, probably 
others


2. Does not require /any/ plugin download at all of any kind to view 
(this disqualifies flash, svg, silverlight, java, and others)


3. If you go to the web page for the first time on a freshly installed 
operating system, without admin privileges, you will see the 
functionality listed below immediately, and with no downloaded plugins 
and installers.  (I apologize for the redundancy, but I want to 
preemptively avoid a flood of non-applicable responses).


4. Graphics, including sprite animation

5. Dynamic response to mouse motion: dragging sprites for example

6. Programmable in Python, of course

Hints from what I can recall:
- Built from javascript as it's raw material under the hood (after all, 
it can't very well be anything else given requirements 1,2,3)
- Seems quite magical since I didn't know the necessary graphical raw 
materials existed in javascript
- I think it's based on Ajax, but I can't seem to find a relevant python 
demo of it due to too much clutter in my google searches


Ken

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


Re: Python, Tkinter and popen problem

2009-05-30 Thread Piet van Oostrum
> norseman  (n) wrote:

>n> Piet van Oostrum wrote:
 norseman  (n) wrote:
>>> 
>n> I have tried both and Popen2.popen2().
>n> os.popen runs both way, contrary to docs.
>>> 
>>> What do you mean `os.popen runs both way'?

>n> It reads from child while console writes directly to child -  thus
>n> eliminating the problem of coding a pass through from master.

Yes, but that is not `both way': popen connects the parent to the child
through a pipe. The pipe works one way: from the child to the parent
with 'r' (default), from the parent to the child with 'w'. You can't
communicate the other way through the pipe. So the communication from
the parent process to the child through the popen is ONE WAY. If you
want TWO WAY communication you can use popen2, or better use
subprocess.Popen.

A child process always inherits stdin, stdout and stderr from the parent
unless you change that (e.g. by redirecting to a pipe, like popen does
for one of them). It doesn't matter whether you use os.popen,
subprocess.Popen, os.system, or os.fork to create the child process. So
in your case if the parent inputs from the console, so does the child.
But note: this is not communication from the parent process to the
child, but from YOU to the child. So the parent-child communication is
ONE WAY.

>n> "...
>>> doesn't work as the iterator for a file, including pipes, does a
>>> read ahead (see the doc on file.next()) and therefore is not suitable
>>> for interactive use.
>n> ..."

>n> If I understand you the above can be stated as:

>n> The above does not work as an iterator for any file type, including
>n> pipes, but it does do read aheads  and therefore is not suitable for
>n> interactive use.

For files in general it is no problem because the contents of the file
is not interactively generated. Read ahead on a file works as long as
you do'nt use readline() on the file in between the iterator actions.
For a socket it could be the same problem if the other side generates
the output interactively.

>n> If that is correct then read ahead is simply buffered file reads (grab a
>n> chunk, parcel it out on demand) - yes?

I don't know, I have looked into the source code but it isn't clear to
me. I noticed that iteration didn't work and then looked up the
documentation. It talks about read ahead for efficiency.
 
>n> As for "... not suitable for interactive ..." Really?  Except for
>n> special purpose use the current interactive components are all buffered
>n> for read ahead use.  Check the actual code for your keyboard, your mouse
>n> and so forth. It's the read ahead that allows faster time to completion.
>n> It's why C-code has the putch function.

Yes, but they only read what is available. The iterator apparently tries
to read more and then has to wait.

>n> Yes - Sync IS the bigger hammer!  If that is what is needed - so be it.
>n> All character readers (byte at a time) should obey a flush(). Depending
>n> on type, code for the reader controls whether or not it flushes
>n> incomplete "lines" in the in-buffer(s). Proper implementation limits lost
>n> data on system crash.

I don't understand what you say here. As I told before it has nothing to
do with sync(). Also for reading there is no flush(); the flush() is
done on the other side of the pipe. Yes, and sync() has to do with
system crashes but that is not what we are talking about in this thread.

>n> In trying to use flush at the master side I keep getting messages
>n> indicating strings (completed or not) are not flushable.  Strange practice.

If you print to the console in the master the flush is done
automatically.

>n> ---
>n> from subprocess import Popen, PIPE
>n> xx = Popen(["z6.py"], stdout=PIPE).stdout

>n> while True:
>n> line = xx.readline()
>n> if not line: break
>n> print "\t" + line,
>n> ---
>n> DOES WORK on Python 2.5.2 on Slackware 10.2 - THANK YOU VERY MUCH!!!
>n> Isn't working on Windows. error message comes as one of two forms.
>n>   1- %1 not found   #as shown above
>n>   2- file not found #as ...["python z6.py"]...
>n> same#as #2 even with full paths given

That should be Popen(["python", "z6.py"], stdout=PIPE).stdout
And then with both python and z6.py given as full paths.

>n> I get the impression subprocess ignores system things on Windows.
>n> The routines it purposes to replace do use them.  At any rate, subprocess
>n> is NOT consistent across platforms.

subprocess is, but Windows isn't. On Unix-like systems, the python
command is usually in your PATH, so just giving "python" works. In
Windows PATH is underused, and commands like python often are not in
PATH, unless you as a user has adapted the path, or maybe there is an
installation option to adapt the PATH. The reason probably is that in
Windows almost nobody uses the command line but only clicks and the PATH
is not relevant.

>n> Some questions:
>n> 1) "...], stdout=PIPE).stdout
>n>^^  why the double use?

It is not a

Re: Browser based Canvas UI?

2009-05-30 Thread CTO
On May 30, 4:12 am, Ken Seehart  wrote:
> A couple years ago I stumbled upon an interesting technology but I can't
> seem to find it, and I can remember what it is called.  Unfortunately
> this makes it difficult to search for.  I am am aware of several partial
> matches (items that meet a subset of the requirement listed below).  
> Does anyone know what does /all/ of the following?
>
> 1. Works on at least FF and IE on XP and Linux out of the box, probably
> others
>
> 2. Does not require /any/ plugin download at all of any kind to view
> (this disqualifies flash, svg, silverlight, java, and others)
>
> 3. If you go to the web page for the first time on a freshly installed
> operating system, without admin privileges, you will see the
> functionality listed below immediately, and with no downloaded plugins
> and installers.  (I apologize for the redundancy, but I want to
> preemptively avoid a flood of non-applicable responses).
>
> 4. Graphics, including sprite animation
>
> 5. Dynamic response to mouse motion: dragging sprites for example
>
> 6. Programmable in Python, of course
>
> Hints from what I can recall:
> - Built from javascript as it's raw material under the hood (after all,
> it can't very well be anything else given requirements 1,2,3)
> - Seems quite magical since I didn't know the necessary graphical raw
> materials existed in javascript
> - I think it's based on Ajax, but I can't seem to find a relevant python
> demo of it due to too much clutter in my google searches
>
> Ken

Probably thinking of Pyjamas- http://pyjamas.sourceforge.net/>.
It lets you interact with canvas- http://www.blobsallad.se/>-
without writing any javascript.

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


Re: How does Python's OOP feel?

2009-05-30 Thread Teguh Iskanto
On Sat, May 30, 2009 at 2:50 PM, Benjamin Kaplan  wrote:

>
>
> On Fri, May 29, 2009 at 9:41 PM, Lie Ryan  wrote:
>
>> Ikon wrote:
>> > I'm rather new to Python. I have PHP for my main language and I do
>> > some Java. They all have a very strict OO schema. As I red through
>> > Python's tutorial it seams it has nothing of those rules. No statical,
>> > abstract classes, functions, or variables.
>> >
>> > I wish someone, who has experience in both Java/PHP/C# and Python
>> > would tell me how mush better/worse is it to program in a language
>> > that misses most of the OO parts!
>>
>> import antigravity
>
>
> You forgot the link. http://www.xkcd.com/353/
>


ha ha ha ha +1
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to reuse TCP listening socket immediately after it was connected at least once?

2009-05-30 Thread Thomas Bellman
Lawrence D'Oliveiro  wrote:

> In message , Thomas Bellman wrote:

>> Speaking as a sysadmin, running applications for production,
>> programs not using SO_REUSEADDR should be taken out and shot.

>> Not using SO_REUSEADDR means forcing a service interruption of
>> half an hour (IIRC) if for some reason the service must be
>> restarted, or having to reboot the entire machine.

> No, you do not recall correctly.

*Tests*  It seems to be 100 seconds in Fedora 9 and 60 seconds in
Solaris 10.  OK, that amount of time is not totally horrible, in
many cases just annoying.  Still much longer for an interruption
of service that could have been just 1-2 seconds.

However, I *have* used systems where it took much longer.  It was
slightly more than ten years ago, under an earlier version of
Solaris 2, problably 2.4.  It may be that it only took that long
under certain circumstances that the application we used always
triggered, but we did have to wait several tens of minutes.  It
was way faster to reboot the machine than waiting for the sockets
to time out.

> And anybody wanting to reboot a machine to
> work around a "problem" like that should be taken out and shot.

We weren't exactly keen on rebooting the machine, but it was the
fastest way of getting out of that situation that we could figure
out.  How *should* we have dealt with it in your opinion?


-- 
Thomas Bellman,   Lysator Computer Club,   Linköping University,  Sweden
"God is real, but Jesus is an integer."  !  bellman @ lysator.liu.se
 !  Make Love -- Nicht Wahr!
-- 
http://mail.python.org/mailman/listinfo/python-list


Q: finding distance between 2 time's

2009-05-30 Thread martin
I made this little script (below) to look througt a dir to see if
there are any files newer than .e.g. 1 hour.
I have the loop through the dir working and can retreive file time as
well as present time.
both time variables are in the format returned by time.localtime()

My question:
How do I find the difference between such two time variables, to
calculate the 'age' of the file?

:-) Martin

--8<-- Code begin -

import os, time

def buildList(directory):
listing = os.listdir(directory)
for x in listing:
x = os.path.join(directory, x)
if os.path.isdir(x):
print ('dir -> %s') % x
if os.path.isfile(x):
tstF = time.localtime(os.path.getmtime(x))
nSize = os.path.getsize(x)
print ('fil -> %s %d @ %s') % (x, nSize, time.asctime
(tstF))
return 0

tstN = time.localtime()
print tstN
print "Time now: %s" % time.asctime(tstN)
buildList('C:\Martin\Work\Work_Eclipse\.metadata')


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


Re: How does Python's OOP feel?

2009-05-30 Thread Ken Seehart

Benjamin Kaplan wrote:



On Fri, May 29, 2009 at 9:41 PM, Lie Ryan > wrote:


Ikon wrote:
> I'm rather new to Python. I have PHP for my main language and I do
> some Java. They all have a very strict OO schema. As I red through
> Python's tutorial it seams it has nothing of those rules. No
statical,
> abstract classes, functions, or variables.
>
> I wish someone, who has experience in both Java/PHP/C# and Python
> would tell me how mush better/worse is it to program in a language
> that misses most of the OO parts!

Some of the concepts that you are looking for might be present in Python 
but may look different than you expect.   If you are defining OO in 
terms of rules and constraints, Python might come up short.   But if you 
are looking for the /benefits/ of OOP, Python does quite well.


I have experience with all of those languages.  More Java than PHP and 
C#, so I can talk about Python and Java.


Originally, the definition of OOP was about concepts such as loose 
coupling, reusable components, focusing on things rather than 
procedures, etc.  Later the prevalent definition became more about what 
you are not allowed to do than how to do what you want to do.  So now we 
put more stress on ideas like strict encapsulation.  For example, some 
purists even suggest that you can't claim to implement encapsulation 
unless you guarantee that there is no possible way to determine 
implementation details from outside.  Python "fails" here because it has 
introspective features that allow you to bypass the limitations that are 
deemed necessary for some artificially strict definition of OOP.  For 
example strictly speaking we don't have private members, but we can 
start a name with underscore or double-underscore which for all 
practical purposes gives all the advantages of private members if we 
assume a certain level of maturity from our programmers.  A less 
restrictive notion of encapsulation might be that objects can be managed 
without the need to know the implementation details.


If you think in terms of the enabling qualities of OOP, but not the 
constraints, Python does in fact score quite high.  We have multiple 
inheritance, abstraction (in the non-fascist sense), encapsulation 
(again in the non-fascist sense), really easy polymorphism, and plenty 
of support for decoupling.


When people claim that python is not OO, they are for the most part only 
able to cite what python fails to prevent you from doing, not what 
python fails to allow you to do.  At the same time, I must admit that 
the constraints imposed by stricter languages, such as compile time 
detection of certain errors, and performance benefits associated with 
static typing.  Fortunately, tools such as pylint and psyco can close 
these two respective gaps to a significant degree.


Okay, enough theory and straw man arguments :)  Here's how it /feels/ to me.

What is missing from Python, as compared to Java, is an oppressive 
system to prevent you from writing bugs no matter what.  As it turn out, 
I found that I was quite capable of writing bugs in Java in spite of 
it's extreme efforts to save me from myself.  I also found that Java 
requires a great deal more lines of code to get to the desired results.  
Most of the lines of code are about making the compiler happy.  If I try 
to do anything metaprogrammingish in Java, I soon start pulling my hair 
out.  After switching to mostly python, I have grown nearly all of my 
hair back.


Once the Java compiler is happy with the syntax, certain kinds of bugs 
are prevented that might exist in corresponding python code.  This is a 
win for Java.  However, because the code is more complex in Java, and 
because it is harder to add clever introspective debugging 
instrumentation, the remaining bugs are harder to find.  I believe that 
all other things being equal, less code is more robust than more code.


I highly recommend using pylint, BTW.  If you use pylint, you are just 
about as safe as you would be with a more oppressive language.


When it is all done, I find that I can get to a bug free, scalable, 
flexible, object oriented result much more quickly in Python than in 
Java, but YMMV.


In summary, Python has all of the capabilities of OOP, but lacks some of 
the constraints of OOP.  This lack of constraints has it's advantages 
and disadvantages, but on the whole, I like it.  What else can I say?


See also:
 http://www.ferg.org/projects/python_java_side-by-side.html

Ken


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


Re: Q: finding distance between 2 time's

2009-05-30 Thread jkv
Hi Martin,

What i usally do is to convert the two times to seconds since epoch and
compare the two values in seconds. You can use time.mktime to convert
localtime to seconds since epoch.

I added a few lines to your script, and now it ought to only print files
newer than 3601 seconds (3600 seconds is one hour).
The modification to you script is just some quick code, im pretty sure
on can compress it a bit.

Regards,
Johnny

script:

import os, time

def buildList(directory):
listing = os.listdir(directory)
for x in listing:
x = os.path.join(directory, x)
if os.path.isdir(x):
print ('dir -> %s') % x
if os.path.isfile(x):
current_time = time.localtime()
tstF = time.localtime(os.path.getmtime(x))
#convert the current time to seconds since epoch
current_epoch=time.mktime(current_time)
#convert the timestamp on the file to seconds since epoch
file_epoch = time.mktime(tstF)
nSize = os.path.getsize(x)
time_difference = current_epoch - file_epoch
#if file newer than one hour print a line
if time_difference < 3601:
  print ('NEW FILE -> %s %d @ %s') % (x, nSize, time.asctime
(tstF))
   return 0

tstN = time.localtime()
print tstN
print "Time now: %s" % time.asctime(tstN)
buildList('/')


mar...@hvidberg.net wrote:
> I made this little script (below) to look througt a dir to see if
> there are any files newer than .e.g. 1 hour.
> I have the loop through the dir working and can retreive file time as
> well as present time.
> both time variables are in the format returned by time.localtime()
>
> My question:
> How do I find the difference between such two time variables, to
> calculate the 'age' of the file?
>
> :-) Martin
>
> --8<-- Code begin -
>
> import os, time
>
> def buildList(directory):
> listing = os.listdir(directory)
> for x in listing:
> x = os.path.join(directory, x)
> if os.path.isdir(x):
> print ('dir -> %s') % x
> if os.path.isfile(x):
> tstF = time.localtime(os.path.getmtime(x))
> nSize = os.path.getsize(x)
> print ('fil -> %s %d @ %s') % (x, nSize, time.asctime
> (tstF))
> return 0
>
> tstN = time.localtime()
> print tstN
> print "Time now: %s" % time.asctime(tstN)
> buildList('C:\Martin\Work\Work_Eclipse\.metadata')
>
>
>   


-- 
Regards,
jkv
http://unixcluster.dk/public.key

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


Re: Questions about regex

2009-05-30 Thread bearophileHUGS
Jared.S., even if a regex doesn't look like a program, it's like a
small program written in a strange language. And you have to test and
comment your programs.
So I suggest you to program in a more tidy way, and add unit tests
(doctests may suffice here) to your regexes, you can also use the
verbose mode and comment them, and you can even indent their sub-parts
as pieces of a program.
You must test all your bricks (in python, not in TextMate) before
using them to build something bigger.

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


Re: Browser based Canvas UI?

2009-05-30 Thread Ken Seehart

CTO wrote:

On May 30, 4:12 am, Ken Seehart  wrote:
  

A couple years ago I stumbled upon an interesting technology but I can't
seem to find it, and I can remember what it is called.  Unfortunately
this makes it difficult to search for.  I am am aware of several partial
matches (items that meet a subset of the requirement listed below).  
Does anyone know what does /all/ of the following?


1. Works on at least FF and IE on XP and Linux out of the box, probably
others

2. Does not require /any/ plugin download at all of any kind to view
(this disqualifies flash, svg, silverlight, java, and others)

3. If you go to the web page for the first time on a freshly installed
operating system, without admin privileges, you will see the
functionality listed below immediately, and with no downloaded plugins
and installers.  (I apologize for the redundancy, but I want to
preemptively avoid a flood of non-applicable responses).

4. Graphics, including sprite animation

5. Dynamic response to mouse motion: dragging sprites for example

6. Programmable in Python, of course

Hints from what I can recall:
- Built from javascript as it's raw material under the hood (after all,
it can't very well be anything else given requirements 1,2,3)
- Seems quite magical since I didn't know the necessary graphical raw
materials existed in javascript
- I think it's based on Ajax, but I can't seem to find a relevant python
demo of it due to too much clutter in my google searches

Ken



Probably thinking of Pyjamas- http://pyjamas.sourceforge.net/>.
It lets you interact with canvas- http://www.blobsallad.se/>-
without writing any javascript.

Geremy Condra
  

Yeah, looks like pyjamas is probably it.  Thanks.

OMG,  http://www.blobsallad.se is way too cool, .but it's javascript, so 
I'm assuming the idea is that it could have been done in python and 
converted to javascript using pyjamas.


Anyway, I think I need to be more specific about the sprites:

7. Supports drawing semi-transparent bitmap sprites (preferably png)

Anyone happen to know where I can find an online example of animating 
with sprites (within the constraints of 1-7 above)?


Thanks,
- Ken

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


Re: Q: finding distance between 2 time's

2009-05-30 Thread martin
Thanks both

The first answer is quite instuctive, the other one might be the one
I'll use in the code, it's nicely compact and clear.

So 2x thanks.

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


Re: What text editor is everyone using for Python

2009-05-30 Thread Hendrik van Rooyen
"Lie Ryan"  wrote:
> norseman wrote:
> > Suggestion:
> > Take a look at the top two most used OS you use and learn the default
> > (most often available) text editors that come with them.
> 
> Which means Notepad on Windows?

you could live dangerously and use WordPad...

- Hendrik

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


Re: Q: finding distance between 2 time's

2009-05-30 Thread Steven D'Aprano
On Sat, 30 May 2009 12:06:55 +0200, jkv wrote:

> I added a few lines to your script, and now it ought to only print files
> newer than 3601 seconds (3600 seconds is one hour). 
...
> #if file newer than one hour print a line 
> if time_difference < 3601:

That's a potential off-by-one error. That may print files that are older 
than one hour. Admittedly, they'll be off by less than one second, but if 
you're going to write code, write correct code. The right test is:

if time_difference <= 3600:

(and you may even want to deal with files that have a *negative* time 
difference, e.g. they were created apparently in the future).

This is particularly necessary if you use time.time() to generate the 
current time, since that returns fractions of a second. But even if you 
don't, it's still the right thing to do: it's defensive programming. 
Rather than assume that all file systems store timestamps accurate only 
to a second, assume that some file system, somewhere, will be accurate to 
fractions of a second, and code accordingly.

That way, no matter what the file system does, your code will still do 
the right thing, and (in this case) it doesn't even cost you anything.



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


Re: Questions about regex

2009-05-30 Thread Steven D'Aprano
On Fri, 29 May 2009 11:26:07 -0700, Jared.S.Bauer wrote:

> Hello,
> 
> I'm new to python and I'm having problems with a regular expression. I
> use textmate as my editor and when I run the regex in textmate it works
> fine, but when I run it as part of the script it freezes. Could anyone
> help me figure out why this is happening and how to fix it.


Sure. To figure out why it is happening, the first thing you must do is 
figure out *what* is happening. So first you have to isolate the fault: 
what part of your script is freezing?

I'm going to assume that it is the regex:

> #The two following lines are the ones giving me the problems
>   text = re.sub("w:(.|\s)*?\n", "", text) 
>   text = re.sub("UnhideWhenUsed=(.|\s)*?\n", "", text)

What happens when you call those two lines in isolation, away from the 
rest of your script? (Obviously you need to initialise a value for text.)
Do they still freeze?

For example, I can do this:

>>> text = "Nobodyw: \n expects the Spanish Inquisition!"
>>> text = re.sub("w:(.|\s)*?\n", "", text)
>>> text = re.sub("UnhideWhenUsed=(.|\s)*?\n", "", text)
>>> text
'Nobody expects the Spanish Inquisition!'

and it doesn't freeze. It works fine.

I suspect that your problem is that the regex hasn't actually *frozen*, 
it's just taking a very, very long time to complete. My guess is that it 
probably has something to do with:

(.|\s)*?

This says, "Match any number of, but as few as possible, of any character 
or whitespace". This will match newlines as well, so the regular 
expression engine will need to do backtracking, which means it will be 
slow for large amounts of data. You want to reduce the amount of 
backtracking that's needed!

I *guess* that what you probably want is:

w:.*?\n

which will match the letter 'w' followed by ':' followed by the shortest 
number of arbitrary characters, including spaces *but not newlines*, 
followed by a newline.

The second regex will probably need a similar change made.

But don't take my word for it: I'm not a regex expert. But isolate the 
fault, identify when it is happening (for all input data, or only for 
large amounts of data?), and then you have a shot at fixing it.



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


Re: What text editor is everyone using for Python

2009-05-30 Thread edexter
On May 30, 6:28 am, "Hendrik van Rooyen"  wrote:
> "Lie Ryan"  wrote:
> > norseman wrote:
> > > Suggestion:
> > > Take a look at the top two most used OS you use and learn the default
> > > (most often available) text editors that come with them.
>
> > Which means Notepad on Windows?
>
> you could live dangerously and use WordPad...
>
> - Hendrik

I will sometimes use word pad but i perfer syntax highlighting.. I
have been tempted to fork out some cash what I would be after is to be
able to define my syntax file, the ability to use code snippits, the
ability to add tools and compilers...  until then I use spe, vim,
emacs, boa constructor, I had a couple of other ones but I probily
won't miss them that much.. The python to c compiler coming of age is
something I am looking foward to and is something I would like to have
supported in my editor (shedskin it is all coming back to me now).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Questions about regex

2009-05-30 Thread Rob Williscroft
 wrote in news:fe9f707f-aaf3-4ca6-859a-5b0c63904fc0
@s28g2000vbp.googlegroups.com in comp.lang.python:


>  text = re.sub('(\<(/?[^\>]+)\>)', "", text)#remove the HTML
> 

Python has a /r/ (raw) string literal type for regex's:

  text = re.sub( r'(\<(/?[^\>]+)\>)', "", text )

In raw strings python doesn't process backslash escape sequences
so r\n' is the 2 char' string '\\n' (a backslash folowed by an 'n').

Without that your pattern  string would need to be writen as:

  '(\\<(/?[^\\>]+)\\>)'

IOW backslashes need to be doubled up or python will process them
before they are passed to re.sub.

Also this seems to be some non-python dialect of regular expression
language, Pythons re's don't need to escape < and >.

http://docs.python.org/library/re.html

The grouping operators, '(' and ')', appear to be unnessasery,
so altogether this 1 line should probably be:

  text = re.sub( r']+>', '', text )

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Python and Glade: window not showing

2009-05-30 Thread Sven Arduwie
Can anyone help a python newbie and tell me why the simple window I
created in Glade is not showing?

This is the XML generated by Glade 3:


  
  
  
True


  

  


And this is the Python code:
#!/usr/bin/env python

import pygtk
pygtk.require("2.0")
import gtk

class HelloWorld(object):
def getWindow(self):
return self.window

def setWindow(self, window):
self.window = window

window = property(getWindow, setWindow)

def __init__(self):
builder = gtk.Builder()
builder.add_from_file("helloWorld.glade")
builder.connect_signals({"on_helloWorld_destroy" :
self.onHelloWorldDestroy})
self.window = builder.get_object("helloWorld")
self.window.show()

def onHelloWorldDestroy(self):
pass

I ran this in a terminal on Ubuntu 9.04 like this:
s...@dell:~$ cd ./gvfs/python\ on\ sven/
s...@dell:~/gvfs/python on sven$ python ./helloWorld.py
s...@dell:~/gvfs/python on sven$
-- 
http://mail.python.org/mailman/listinfo/python-list


PyQt4 + WebKit

2009-05-30 Thread dudekksoft
Hello,

I need to grab clicked links in QWebView. Everything is fine when I
use linkClicked() signal. LinkDelegationPolicy is set to
DelegateAllLinks and there is a problem. If some site has Javascript
my procedure receives QUrl from linkClicked, next calls:

webView.setUrl(url) #Where "url" is received QUrl

Certainly this method doesn't work. Site loads again (in most cases
it's called http://mywwwsite.net/#). As I use
QWebPage::DelegateExternalLinks it happens the same, because my
procedure grabs links with Javascript. I don't know how to cope with
this problem. Maybe it's possible to set own policy in
QWebPage.LinkDelegationPolicy? Or maybe some other method to grab
protocol, host(if external or not) when user clicks link in webView?

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


Compiling and transporting modules/libraries in python

2009-05-30 Thread Abe
Hi all -
  I hope this is a simple question, but I've been running in circles
trying to figure it out myself.  Is there a good way to wrap up a
library (e.g. numpy or matplotlib) so that I can use it on another
machine without actually installing it?

I use python at a home office and in a university computer lab,
but I don't have the administrative rights to install libraries on the
lab computers.  It would be really nice if there were a way I could
put, say, all of numpy into a file "my_numpy.pyc" and treat it as a
single (large) module.

thanks
-Abe

PS - If the answer somehow involves compileall, could you spell it out
for me?  I haven't been able to figure how to make compileall work for
this.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to ask smart questions question

2009-05-30 Thread Aahz
In article <960af9f3-c445-4d6d-b277-76a123ee4...@s28g2000vbp.googlegroups.com>,
Carl Banks   wrote:
>On May 26, 7:48=A0am, Gary Herron  wrote:
>>
>> The proper response to a question like this has to be
>> =A0 =A0http://www.catb.org/~esr/faqs/smart-questions.html
>> as anything else is complete guesswork.
>
>Is there a Cliff's Notes version of this?
>
>I may be a cynic but I would think the people who ask bad questions
>are the same sort of people who won't have the patience to sit though
>this behemoth of an essay.

Perhaps linking directly to this would help:

http://www.catb.org/~esr/faqs/smart-questions.html#before

The table of contents contains a good summary for actually asking
questions.  Overall, I'm inclined to agree with people who argue that the
smart questions essay is a good filter; someone unwilling to put in the
work of reading and understanding it is also unwilling to put in the
effort to ask good questions.  I mean, I re-read most of that essay once
every couple of years myself.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

my-python-code-runs-5x-faster-this-month-thanks-to-dumping-$2K-
on-a-new-machine-ly y'rs  - tim
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MySQLdb 1.2.2 + python 2.6.2

2009-05-30 Thread Piet van Oostrum
> monogeo  (m) wrote:

>m> Hi all,
>m> Are MySQLdb 1.2.2 and python 2.6.2 compatible? I went to
>m> http://sourceforge.net/project/showfiles.php?group_id=22307, it
>m> doesn't say it is compatible or not.

>m> When trying to install MySQLdb 1.2.2 on my machine which is running
>m> python 2.6.2 and windows XP, I get this error below.

>m> I am able to install MySQLdb 1.2.2 successfully on another machine
>m> which is running python 2.5.4 and windows XP.

>m> Please help if you have any idea.

Try the 1.2.3 release candidate
http://sourceforge.net/project/showfiles.php?group_id=22307&package_id=34790&release_id=672239
 
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


[RELEASED] Python 3.1 Release Candidate 1

2009-05-30 Thread Benjamin Peterson
On behalf of the Python development team, I'm happy to announce the first
release candidate of Python 3.1.

Python 3.1 focuses on the stabilization and optimization of the features and
changes that Python 3.0 introduced.  For example, the new I/O system has been
rewritten in C for speed.  File system APIs that use unicode strings now handle
paths with undecodable bytes in them. Other features include an ordered
dictionary implementation, a condensed syntax for nested with statements, and
support for ttk Tile in Tkinter.  For a more extensive list of changes in 3.1,
see http://doc.python.org/dev/py3k/whatsnew/3.1.html or Misc/NEWS in the Python
distribution.

This is a release candidate, and as such, we do not recommend use in production
environments.  However, please take this opportunity to test the release with
your libraries or applications.  This will hopefully discover bugs before the
final release and allow you to determine how changes in 3.1 might impact you.
If you find things broken or incorrect, please submit a bug report at

 http://bugs.python.org

For more information and downloadable distributions, see the Python 3.1 website:

 http://www.python.org/download/releases/3.1/

See PEP 375 for release schedule details:

 http://www.python.org/dev/peps/pep-0375/



Enjoy,
-- Benjamin

Benjamin Peterson
benjamin at python.org
Release Manager
(on behalf of the entire python-dev team and 3.1's contributors)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: CRLF when doing os.system("ls -l") while using curses !!!

2009-05-30 Thread lsk040365
On May 29, 6:35 pm, Piet van Oostrum  wrote:
> > lkenne...@gmail.com (l) wrote:
> >l> Here is the code and as you can see for yourself, the output is not
> >l> coming out on the screen with CRLF like it should.  How do I fix this?
>
> Don't use curses.
>
> Curses puts the terminal in raw mode (more or less) which doesn't
> translate the newline character into CRLF. If you use curses you are
> supposed to do all output through curses. But the os.system goes
> directly to the screen, outside of curses (because it is another
> process).
> You could catch the output of ls -l with a PIPE and then write the
> output to the curses screen with addstr. But a curses screen has a
> limited length, whereas your ls -l output may be larger so then you must
> implement some form of scrolling.
>
> >l> import curses, os
> >l> screen = curses.initscr()
> >l> os.system("ls -l")
> >l> curses.endwin()
>
> --
> Piet van Oostrum 
> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
> Private email: p...@vanoostrum.org

Can you provide me with some sort of example possibly--I have been
fighting this for a while and I wouldn't be using curses but I have
designed the program with the extended ascii characters thereby making
a nice UI...?

Thanks in advance,
Lee
-- 
http://mail.python.org/mailman/listinfo/python-list


hash and __eq__

2009-05-30 Thread Aaron Brady
I am writing a mapping object, and I want to ask about the details of
__hash__ and __eq__.  IIUC if I understand correctly, the Python
dict's keys' hash codes are looked up first in O( 1 ), then all the
matching hash entries are compared on equality in O( n ).  That is,
the hash code just really narrows down the search.  Am I correct?

P.S.  I always feel like my posts should start like, "A mapping object
am writing I."  Not too many verbs you can do that with in English.
What if I did it?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python and Glade: window not showing

2009-05-30 Thread Sven Arduwie
On 30 mei, 17:02, Sven Arduwie  wrote:
> Can anyone help a python newbie and tell me why the simple window I
> created in Glade is not showing?
>
> This is the XML generated by Glade 3:
> 
> 
>   
>   
>   
>     True
>     
>     
>       
>     
>   
> 
>
> And this is the Python code:
> #!/usr/bin/env python
>
> import pygtk
> pygtk.require("2.0")
> import gtk
>
> class HelloWorld(object):
>         def getWindow(self):
>                 return self.window
>
>         def setWindow(self, window):
>                 self.window = window
>
>         window = property(getWindow, setWindow)
>
>         def __init__(self):
>                 builder = gtk.Builder()
>                 builder.add_from_file("helloWorld.glade")
>                 builder.connect_signals({"on_helloWorld_destroy" :
> self.onHelloWorldDestroy})
>                 self.window = builder.get_object("helloWorld")
>                 self.window.show()
>
>         def onHelloWorldDestroy(self):
>                 pass
>
> I ran this in a terminal on Ubuntu 9.04 like this:
> s...@dell:~$ cd ./gvfs/python\ on\ sven/
> s...@dell:~/gvfs/python on sven$ python ./helloWorld.py
> s...@dell:~/gvfs/python on sven$

Okay I'm mad at myself for forgetting this:

if __name__ == "__main__":
helloWorld = HelloWorld()
gtk.main()

When I add that, a new problem arises: the terminal floods with:
  File "./helloWorld.py", line 12, in setWindow
self.window = window
  File "./helloWorld.py", line 12, in setWindow
self.window = window
  File "./helloWorld.py", line 12, in setWindow
self.window = window
ad infinitum
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python and Glade: window not showing

2009-05-30 Thread Dave Angel

Sven Arduwie wrote:

On 30 mei, 17:02, Sven Arduwie  wrote:
  

Can anyone help a python newbie and tell me why the simple window I
created in Glade is not showing?

This is the XML generated by Glade 3:


  
  
  
True


  

  


And this is the Python code:
#!/usr/bin/env python

import pygtk
pygtk.require("2.0")
import gtk

class HelloWorld(object):
def getWindow(self):
return self.window

def setWindow(self, window):
self.window =indow

window =roperty(getWindow, setWindow)

def __init__(self):
builder =tk.Builder()
builder.add_from_file("helloWorld.glade")
builder.connect_signals({"on_helloWorld_destroy" :
self.onHelloWorldDestroy})
self.window =uilder.get_object("helloWorld")
self.window.show()

def onHelloWorldDestroy(self):
pass

I ran this in a terminal on Ubuntu 9.04 like this:
s...@dell:~$ cd ./gvfs/python\ on\ sven/
s...@dell:~/gvfs/python on sven$ python ./helloWorld.py
s...@dell:~/gvfs/python on sven$



Okay I'm mad at myself for forgetting this:

if __name__ ="__main__":
helloWorld =elloWorld()
gtk.main()

When I add that, a new problem arises: the terminal floods with:
  File "./helloWorld.py", line 12, in setWindow
self.window =indow
  File "./helloWorld.py", line 12, in setWindow
self.window =indow
  File "./helloWorld.py", line 12, in setWindow
self.window =indow
ad infinitum

  
You have infinite recursion because setWindow is defined indirectly in 
terms of itself.  It uses the property 'window', which is defined to use 
setWindow.


The cure for it is simple.  If you want to have a private data 
attribute, use a leading underscore.  Don't call it the same thing that 
the public is going to use.


class HelloWorld(object):
def getWindow(self):
return self._window

def setWindow(self, window):
self._window = window

window = property(getWindow, setWindow)

def __init__(self):
builder = gtk.Builder()
builder.add_from_file("helloWorld.glade")
builder.connect_signals({"on_helloWorld_destroy" :
self.onHelloWorldDestroy})
self._window = builder.get_object("helloWorld")
self._window.show()

def onHelloWorldDestroy(self):
pass


(untested)

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


Re: Python and Glade: window not showing

2009-05-30 Thread Sven Arduwie
On 30 mei, 21:02, Dave Angel  wrote:
> Sven Arduwie wrote:
> > On 30 mei, 17:02, Sven Arduwie  wrote:
>
> >> Can anyone help a python newbie and tell me why the simple window I
> >> created in Glade is not showing?
>
> >> This is the XML generated by Glade 3:
> >> 
> >> 
> >>   
> >>   
> >>   
> >>     True
> >>     
> >>     
> >>       
> >>     
> >>   
> >> 
>
> >> And this is the Python code:
> >> #!/usr/bin/env python
>
> >> import pygtk
> >> pygtk.require("2.0")
> >> import gtk
>
> >> class HelloWorld(object):
> >>         def getWindow(self):
> >>                 return self.window
>
> >>         def setWindow(self, window):
> >>                 self.window =indow
>
> >>         window =roperty(getWindow, setWindow)
>
> >>         def __init__(self):
> >>                 builder =tk.Builder()
> >>                 builder.add_from_file("helloWorld.glade")
> >>                 builder.connect_signals({"on_helloWorld_destroy" :
> >> self.onHelloWorldDestroy})
> >>                 self.window =uilder.get_object("helloWorld")
> >>                 self.window.show()
>
> >>         def onHelloWorldDestroy(self):
> >>                 pass
>
> >> I ran this in a terminal on Ubuntu 9.04 like this:
> >> s...@dell:~$ cd ./gvfs/python\ on\ sven/
> >> s...@dell:~/gvfs/python on sven$ python ./helloWorld.py
> >> s...@dell:~/gvfs/python on sven$
>
> > Okay I'm mad at myself for forgetting this:
>
> > if __name__ ="__main__":
> >    helloWorld =elloWorld()
> >    gtk.main()
>
> > When I add that, a new problem arises: the terminal floods with:
> >   File "./helloWorld.py", line 12, in setWindow
> >     self.window =indow
> >   File "./helloWorld.py", line 12, in setWindow
> >     self.window =indow
> >   File "./helloWorld.py", line 12, in setWindow
> >     self.window =indow
> > ad infinitum
>
> You have infinite recursion because setWindow is defined indirectly in
> terms of itself.  It uses the property 'window', which is defined to use
> setWindow.
>
> The cure for it is simple.  If you want to have a private data
> attribute, use a leading underscore.  Don't call it the same thing that
> the public is going to use.
>
> class HelloWorld(object):
>          def getWindow(self):
>                  return self._window
>
>          def setWindow(self, window):
>                  self._window = window
>
>          window = property(getWindow, setWindow)
>
>          def __init__(self):
>                  builder = gtk.Builder()
>                  builder.add_from_file("helloWorld.glade")
>                  builder.connect_signals({"on_helloWorld_destroy" :
>  self.onHelloWorldDestroy})
>                  self._window = builder.get_object("helloWorld")
>                  self._window.show()
>
>          def onHelloWorldDestroy(self):
>                  pass
>
> (untested)

That solved the problem, thanks!

I assume that the getWindow and setWindow can be bypassed by using the
_window property directly and that Python has no visibility keywords
like private or protected. Sort of like PHP 4. (Not that I want to
compare Python to anything like that mess, lol ;))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: hash and __eq__

2009-05-30 Thread Aaron Brady
On May 30, 12:11 pm, Dennis Lee Bieber  wrote:
> On Sat, 30 May 2009 11:20:47 -0700 (PDT), Aaron Brady
>  declaimed the following in
> gmane.comp.python.general:
>
> > P.S.  I always feel like my posts should start like, "A mapping object
> > am writing I."  Not too many verbs you can do that with in English.
> > What if I did it?
>
>         You turn into a short fuzzy large eared critter in a robe with a
> light-saber?

A fuzzy large eared critter turn I into.

"Ending sentences in prepositions is nonsense up with which I will not
put." -Churchill

I'm mimicking the dict type with an SQL table (something to which I
alluded some time ago).  I'm going to 'select' on the hash code, then
(*THEN*) revive the keys and compare on equality.  The values don't
need to be revived.

P.S.  def revive!
-- 
http://mail.python.org/mailman/listinfo/python-list


IndexError: tuple index out of range

2009-05-30 Thread Invert
My simple python script gives me an error when I try to execute it.  Here is
the error message:

dan...@ibex:~/Desktop/python$ python3 str_format2Daniel.py
> Traceback (most recent call last):
>   File "str_format2Daniel.py", line 8, in 
> print ("{0} is {1} years old {3} and ".format(name, age, append))
> IndexError: tuple index out of range
> dan...@ibex:~/Desktop/python$
>

Here is my script:

#!/usr/bin/python
> # Filename: str_format2.py
>
> age = 23
> name = 'Daniel'
> append = '6 months'
>
> print ("{0} is {1} years old {3} and ".format(name, age, append))
> print ('{0} will be an elite python programmer in {2}.'.format(name,
> append))


What am I doing wrong?
-- 
http://mail.python.org/mailman/listinfo/python-list


how to free memory allocated by a function call via ctypes

2009-05-30 Thread Tzury Bar Yochay
Hi,

Suppose I have the following function
char *generateMessage(char *sender, char *reciever, char *message) ;

now in c I would normally do

char *msg = generateMessage(sender, reciever, message);
// do something
free(msg);

My question is how do I free the memory allocated when I call this
function using ctypes
I could not find this in the documentation for python 2.5

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


Re: CRLF when doing os.system("ls -l") while using curses !!!

2009-05-30 Thread Piet van Oostrum
> lsk040365  (l) wrote:

>l> Can you provide me with some sort of example possibly--I have been
>l> fighting this for a while and I wouldn't be using curses but I have
>l> designed the program with the extended ascii characters thereby making
>l> a nice UI...?

Nice UI This is so 70's :=)

import curses, os
screen = curses.initscr()

height, width = screen.getmaxyx()
print height, width

# popen is deprecated: better switch to subprocess.Popen
ls = os.popen("ls -l")
while True:
screen.clear()
screen.addstr(0, 0, "DIRECTORY LISTING")

# display as many lines as will fit on the screen
for i in range(1, height-1):
line = ls.readline()
if not line: break
screen.addstr(i, 0, line.rstrip())

if line: # we have more to expect
screen.addstr(height-1, 0, "PLEASE HIT RETURN FOR NEXT SCREEN OR q TO 
STOP")
screen.refresh()
key = screen.getkey()
if key.lower() == 'q':
break
else: # end of listing reached
screen.addstr(height-1, 0, "** END OF LISTING ** HIT RETURN")
screen.refresh()
screen.getkey()
break

curses.endwin()

-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: hash and __eq__

2009-05-30 Thread Piet van Oostrum
> Aaron Brady  (AB) wrote:

>AB> I am writing a mapping object, and I want to ask about the details of
>AB> __hash__ and __eq__.  IIUC if I understand correctly, the Python
>AB> dict's keys' hash codes are looked up first in O( 1 ), then all the
>AB> matching hash entries are compared on equality in O( n ).  That is,
>AB> the hash code just really narrows down the search.  Am I correct?

The O(1) part is correct. The O(n) is incorrect if n as usual is taken
to be the number of keys in the dict. But it is true if n is the number
of keys in the `bucket' that belongs to the hash value. The average
complexity of looking up a key is O(1).

See http://wiki.python.org/moin/DictionaryKeys
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IndexError: tuple index out of range

2009-05-30 Thread MRAB

Invert wrote:
My simple python script gives me an error when I try to execute it.  
Here is the error message:


dan...@ibex:~/Desktop/python$ python3 str_format2Daniel.py
Traceback (most recent call last):
  File "str_format2Daniel.py", line 8, in 
print ("{0} is {1} years old {3} and ".format(name, age, append))
IndexError: tuple index out of range
dan...@ibex:~/Desktop/python$


Here is my script:

#!/usr/bin/python
# Filename: str_format2.py

age = 23
name = 'Daniel'
append = '6 months'

print ("{0} is {1} years old {3} and ".format(name, age, append))


That should be:

print ("{0} is {1} years old {2} and ".format(name, age, append))


print ('{0} will be an elite python programmer in {2}.'.format(name,
append))


What am I doing wrong?




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


Re: how to free memory allocated by a function call via ctypes

2009-05-30 Thread Benjamin Peterson
Tzury Bar Yochay  gmail.com> writes:
> 
> My question is how do I free the memory allocated when I call this
> function using ctypes

The return type of ctypes.create_string_buffer() will call free() when it is
garbage collected.




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


decoding keyboard input when using curses

2009-05-30 Thread Arnaud Delobelle
Hi all,

I am looking for advice on how to use unicode with curses.  First I will
explain my understanding of how curses deals with keyboard input and how
it differs with what I would like.

The curses module has a window.getch() function to capture keyboard
input.  This function returns an integer which is more or less:

* a byte if the key which was pressed is a printable character (e.g. a,
  F, &);

* an integer > 255 if it is a special key, e.g. if you press KEY_UP it
  returns 259.

As far as I know, curses is totally unicode unaware, so if the key
pressed is printable but not ASCII, the getch() function will return one
or more bytes depending on the encoding in the terminal.

E.g. given utf-8 encoding, if I press the key 'é' on my keyboard (which
encoded as '\xc3\xa9' in utf-8), I will need two calls to getch() to get
this: the first one will return 0xC3 and the second one 0xA9.

Instead of getting a stream of bytes and special keycodes (with value >
255) from getch(), what I want is a stream of *unicode characters* and
special keycodes.

So, still assuming utf-8 encoding in the terminal, if I type:

Té[KEY_UP]ça

iterating call to the getch() function will give me this sequence of
integers:

84, 195, 169, 259,   195, 167, 97
T-  é---  KEY_UP ç---  a-

But what I want to get this stream instead:

u'T', u'é', 259, u'ç', u'a'


I can pipe the stream of output from getch() directly through an
instance of codecs.getreader('utf-8') because getch() sometimes returns
the integer values of the 'special keys'.

Now I will present to you the solution I have come up with so far.  I am
really unsure whether it is a good way to solve this problem as both
unicode and curses still feel quite mysterious to me.  What I would
appreciate is some advice on how to do it better - or someone to point
out that I have a gross misunderstanding of what is going on!

This has been tested in Python 2.5

 uctest.py --
# -*- coding:utf-8 -*-

import codecs
import curses

# This gives the return codes given by curses.window.getch() when
# "Té[KEY_UP]ça" is typed in a terminal with utf-8 encoding:

codes = map(ord, "Té") + [curses.KEY_UP]  + map(ord, "ça")


# This class defines a file-like object from a curses window 'win'
# whose read() function will return the next byte (as a character)
# given by win.getch() if it's a byte or return the empty string and
# set the code attribute to the value of win.getch().

# It is not used in this test, The Stream class below is used
# instead.

class CursesStream(object):
def __init__(self, win):
self.getch = self.win.getch
def read(self):
c = self.getch()
if c == -1:
self.code = None
return ''
elif c > 255:
self.code = c
return ''
else:
return chr(c)

# This class simulates CursesStream above with a predefined list of
# keycodes to return - handy for testing.

class Stream(object):
def __init__(self, codes):
self.codes = iter(codes)
def read(self):
try:
c = self.codes.next()
except StopIteration:
self.code = None
return ''
if c > 255:
self.code = c
return ''
else:
return chr(c)

def getkeys(stream, encoding):
'''Given a CursesStream object and an encoding, yield the decoded
unicode characters and special keycodes that curses sends'''
read = codecs.getreader(encoding)(stream).read
while True:
c = read()
if c:
yield c
elif stream.code is None:
return
else:
yield stream.code


# Test getkeys with

for c in getkeys(Stream(codes), 'utf-8'):
if isinstance(c, unicode):
print 'Char\t', c
else:
print 'Code\t', c

 running uctest.py --

marigold:junk arno$ python uctest.py 
CharT
Charé
Code259
Charç
Chara

Thanks if you have read this far!

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


Re: decoding keyboard input when using curses

2009-05-30 Thread Arnaud Delobelle
Arnaud Delobelle  writes:
[...]
> I can pipe the stream of output from getch() directly through an
^^^ I mean *can't*

> instance of codecs.getreader('utf-8') because getch() sometimes returns
> the integer values of the 'special keys'.
[...]

I reread my post 3 times before sending it, honest!

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


Re: hash and __eq__

2009-05-30 Thread Arnaud Delobelle
Piet van Oostrum  writes:

>> Aaron Brady  (AB) wrote:
>
>>AB> I am writing a mapping object, and I want to ask about the details of
>>AB> __hash__ and __eq__.  IIUC if I understand correctly, the Python
>>AB> dict's keys' hash codes are looked up first in O( 1 ), then all the
>>AB> matching hash entries are compared on equality in O( n ).  That is,
>>AB> the hash code just really narrows down the search.  Am I correct?
>
> The O(1) part is correct. The O(n) is incorrect if n as usual is taken
> to be the number of keys in the dict. But it is true if n is the number
> of keys in the `bucket' that belongs to the hash value. The average
> complexity of looking up a key is O(1).

As all the keys could be in the same bucket, O(n) seems correct to me
(with n the total number of keys).

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


Re: scripting and uploading in Python

2009-05-30 Thread Aahz
In article <3c1f0711-8da8-4610-bf8e-679eff0fa...@21g2000vbk.googlegroups.com>,
Mark Tarver   wrote:
>
>Generally I'd like to bring myself up to speed on scripting in
>Python.   Any good reads - dead tree or otherwise?

Assuming you have any programming experience, start with the Python
tutorial:

http://docs.python.org/tutorial/index.html
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

my-python-code-runs-5x-faster-this-month-thanks-to-dumping-$2K-
on-a-new-machine-ly y'rs  - tim
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: multiprocessing / forking memory usage

2009-05-30 Thread Aahz
In article ,
Randall Smith   wrote:
>
>I'm trying to get a grasp on how memory usage is affected when forking 
>as the multiprocessing module does.  I've got a program with a parent 
>process using wx and other memory intensive modules. It spawns child 
>processes (by forking) that should be very lean (no wx required, etc). 
>Based on inspection using "ps v" and psutil, the memory usage (rss) is 
>much higher than I would expect for the subprocess.

One option if you're concerned about memory usage is to os.exec() another
program after forking, which will overlay the current process.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

my-python-code-runs-5x-faster-this-month-thanks-to-dumping-$2K-
on-a-new-machine-ly y'rs  - tim
-- 
http://mail.python.org/mailman/listinfo/python-list


Python 2.6 for Windows 64-bit AMD

2009-05-30 Thread dineshv
I upgraded from Python 2.5.4 to Python 2.6.2 under the Windows 64-bit
AMD version, but no external libraries (eg. pyparsing and Numpy 1.3)
work.  I noticed a few odd things:

i.  pyparsing could not find an entry for Python 2.6.2 in the Wondows
Registry
ii. Python 2.6.2 only allows per-machine installation instead of per-
user and per-machine.

Can anyone shed any light on what's up with this build of Python
2.6.2?

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


Re: Q: finding distance between 2 time's

2009-05-30 Thread jkv
mar...@hvidberg.net wrote:
> Thanks both
>
> The first answer is quite instuctive, the other one might be the one
> I'll use in t
I didn't receive the other answer, could you please forward it to me?
> So 2x thanks.
You are welcome.

I took another look at your code, and you can compress it all to a if
"oneliner":
(and thanks to Steven for the <= reminder)

if os.path.isfile(x):
nSize = os.path.getsize(x)
#if oneliner
if time.mktime(time.localtime()) -
time.mktime(time.localtime(os.path.getmtime(x))) <= 3600:
  print ('HT fil -> %s %d @ %s') % (x, nSize, time.asctime)

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


Re: hash and __eq__

2009-05-30 Thread Terry Reedy

Aaron Brady wrote:

I am writing a mapping object, and I want to ask about the details of
__hash__ and __eq__.  IIUC if I understand correctly, the Python
dict's keys' hash codes are looked up first in O( 1 ), then all the
matching hash entries are compared on equality in O( n ).  That is,
the hash code just really narrows down the search.  Am I correct?


Yes.  Even if your mapping object did a linear O(n) search with the hash 
value, only testing for equality after finding an equal hash value would 
be faster.  Your own mapping object can do whatever you want it to do 
internally, as long as it provides the appropriate interface.  You 
could, for instance, make a map that allowed unhashable lists, 
(unfrozen) sets, and dicts as heys by using len() as a surrogate for hash().


tjr

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


Re: IndexError: tuple index out of range

2009-05-30 Thread Terry Reedy

MRAB wrote:

Invert wrote:
My simple python script gives me an error when I try to execute it.  
Here is the error message:


dan...@ibex:~/Desktop/python$ python3 str_format2Daniel.py
Traceback (most recent call last):
  File "str_format2Daniel.py", line 8, in 
print ("{0} is {1} years old {3} and ".format(name, age, append))
IndexError: tuple index out of range
dan...@ibex:~/Desktop/python$


Here is my script:

#!/usr/bin/python
# Filename: str_format2.py

age = 23
name = 'Daniel'
append = '6 months'

print ("{0} is {1} years old {3} and ".format(name, age, append))


That should be:

print ("{0} is {1} years old {2} and ".format(name, age, append))


Actually
print ("{0} is {1} years old and {2}".format(name, age, append))




print ('{0} will be an elite python programmer in {2}.'.format(name,
append))


And the latter should replace 2 with 1.


What am I doing wrong?


Using position indexes that are too large.  In Py3.1, .format will 
auto-number fields, so the print statements could be


print("{} is {} years old and {}".format(name, age, append))
print ('{} will be an elite python programmer in {}.'.format(name, append))

Terry Jan Reedy


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


Re: hash and __eq__

2009-05-30 Thread Robert Kern

On 2009-05-30 17:29, Terry Reedy wrote:

Aaron Brady wrote:

I am writing a mapping object, and I want to ask about the details of
__hash__ and __eq__. IIUC if I understand correctly, the Python
dict's keys' hash codes are looked up first in O( 1 ), then all the
matching hash entries are compared on equality in O( n ). That is,
the hash code just really narrows down the search. Am I correct?


Yes. Even if your mapping object did a linear O(n) search with the hash
value, only testing for equality after finding an equal hash value would
be faster. Your own mapping object can do whatever you want it to do
internally, as long as it provides the appropriate interface. You could,
for instance, make a map that allowed unhashable lists, (unfrozen) sets,
and dicts as heys by using len() as a surrogate for hash().


Only as long as you don't modify the length of those objects serving as keys in 
the meantime. But if you're careful about that, knock yourself out.


The important property to maintain is that if two keys compare equal to each 
other, then their hashes should equal each other. The reverse does not have to 
be true.


--
Robert Kern

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

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


Re: Python 2.6 for Windows 64-bit AMD

2009-05-30 Thread Terry Reedy

dineshv wrote:

I upgraded from Python 2.5.4 to Python 2.6.2 under the Windows 64-bit
AMD version, but no external libraries (eg. pyparsing and Numpy 1.3)
work.  I noticed a few odd things:

i.  pyparsing could not find an entry for Python 2.6.2 in the Wondows
Registry


I suspect that you did not make 2.6 the default installation, so it did 
not make the registry entry pyparsing was looking for.  If pyparsing has 
any compiled C, you need a binary compiled and linked for 2.6.



ii. Python 2.6.2 only allows per-machine installation instead of per-
user and per-machine.


I would not be surprised if per-user installation was dropped because of 
problems, especially with Vista.



Can anyone shed any light on what's up with this build of Python
2.6.2?


Probably built by M.L.Loewis, who may chime in.

tjr


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


Re: Python 2.6 for Windows 64-bit AMD

2009-05-30 Thread Martin v. Löwis
> Can anyone shed any light on what's up with this build of Python
> 2.6.2?

You probably tried to install the 32-bit version of PyParsing;
this cannot work with the 64-bit version of Python. You either
need to obtain a 64-bit version of pyparsing, or install the
32-bit version of Python.

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


Re: hash and __eq__

2009-05-30 Thread Piet van Oostrum
> Arnaud Delobelle  (AD) wrote:

>AD> Piet van Oostrum  writes:
 Aaron Brady  (AB) wrote:
>>> 
>AB> I am writing a mapping object, and I want to ask about the details of
>AB> __hash__ and __eq__.  IIUC if I understand correctly, the Python
>AB> dict's keys' hash codes are looked up first in O( 1 ), then all the
>AB> matching hash entries are compared on equality in O( n ).  That is,
>AB> the hash code just really narrows down the search.  Am I correct?
>>> 
>>> The O(1) part is correct. The O(n) is incorrect if n as usual is taken
>>> to be the number of keys in the dict. But it is true if n is the number
>>> of keys in the `bucket' that belongs to the hash value. The average
>>> complexity of looking up a key is O(1).

>AD> As all the keys could be in the same bucket, O(n) seems correct to me
>AD> (with n the total number of keys).

For worst case only (mainly with a badly designed hash function). 
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Metaclass mystery

2009-05-30 Thread LittleGrasshopper
I am experimenting with metaclasses, trying to figure out how things
are put together. At the moment I am baffled by the output of the
following code:


"""
Output is:

instance of metaclass MyMeta being created
(, )
instance of metaclass MyNewMeta being created
instance of metaclass MyMeta being created    Why this?
(, )

"""

class MyMeta(type):
def __new__(meta, classname, bases, classDict):
print 'instance of metaclass MyMeta being created'
return type.__new__(meta, classname, bases, classDict)

class MyNewMeta(type):
def __new__(meta, classname, bases, classDict):
print 'instance of metaclass MyNewMeta being created'
return type(classname, bases, classDict)

"""
Notice that a metaclass can be a factory function:
def f(classname, bases, classDict):
return type(classname, bases, classDict)

class MyClass(object):
__metaclass__ = f
"""

class MyClass(object):
__metaclass__ = MyMeta

print (MyClass.__class__, MyClass.__metaclass__)

class MySubClass(MyClass):
__metaclass__ = MyNewMeta

print (MySubClass.__class__, MySubClass.__metaclass__)

Honestly, I don't know why this line:
instance of metaclass MyMeta being created    Why this?
is being output when the MySubClass class object is instantiated.
MyNewMeta's __new__ method simply instantiates type directly (which I
know shouldn't be done, but I'm just experimenting and trying to
understand the code's output.)

I would really appreciate some ideas.
-- 
http://mail.python.org/mailman/listinfo/python-list


how to find the last decorator of a chain

2009-05-30 Thread Gabriel
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256

Hi,

I have something like this:

@render(format="a")
@render(format="b")
@
def view(format, data):
  return data

Each render will do something with 'data' if format match, and nothing
if not.

But if there is no more renders to eval, the last one is the default,
and must run even if the format doesn't match.

In my understanding this equivalent to:

render('a',
 render('b',
  view(***)))

Is there any way to know, in this case, that 'a' is the 'default' format?

PS: the number of renders could be 1 or more.

- --
Kind regards.
-BEGIN PGP SIGNATURE-
Version: GnuPG v2.0.9 (GNU/Linux)

iEYEAREIAAYFAkohvjoACgkQNHr4BkRe3pJZQgCgqxL7Qq7/vqDQMLkGQs5emWgH
nbMAn2vzY0xGjG2xhOkxAf8hmERc8R5r
=wWbB
-END PGP SIGNATURE-
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Q: finding distance between 2 time's

2009-05-30 Thread John Machin
On May 30, 7:33 pm, mar...@hvidberg.net wrote:
> I made this little script (below) to look througt a dir to see if
> there are any files newer than .e.g. 1 hour.
> I have the loop through the dir working and can retreive file time as
> well as present time.
> both time variables are in the format returned by time.localtime()
>
> My question:
> How do I find the difference between such two time variables, to
> calculate the 'age' of the file?

http://en.wikipedia.org/wiki/Subtraction

This technique in well worth learning, as it can be applied in many
real-world situations, sometimes not even requiring a computer. For
example:

The letter was postmarked on the 18th of May; it was delivered on the
29th of May; how many days was it in transit? You give a $10 note for
an article priced at $6.35; how much change should you get?

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


Re: how to find the last decorator of a chain

2009-05-30 Thread Benjamin Peterson
Gabriel  opensuse.org> writes:

> In my understanding this equivalent to:
> 
> render('a',
>  render('b',
>   view(***)))
> 
> Is there any way to know, in this case, that 'a' is the 'default' format?

You could set an attribute on the function indicating the default.





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


Re: PyQt4 + WebKit

2009-05-30 Thread David Boddie
On Saturday 30 May 2009 17:39, dudekks...@gmail.com wrote:

> I need to grab clicked links in QWebView. Everything is fine when I
> use linkClicked() signal. LinkDelegationPolicy is set to
> DelegateAllLinks and there is a problem. If some site has Javascript
> my procedure receives QUrl from linkClicked, next calls:
> 
> webView.setUrl(url) #Where "url" is received QUrl
> 
> Certainly this method doesn't work. Site loads again (in most cases
> it's called http://mywwwsite.net/#).

OK, so if I understand correctly, you don't know how to handle these
special links if you use a delegation policy, and you would prefer it
if they were handled by WebKit.

> As I use 
> QWebPage::DelegateExternalLinks it happens the same, because my
> procedure grabs links with Javascript. I don't know how to cope with
> this problem. Maybe it's possible to set own policy in
> QWebPage.LinkDelegationPolicy? Or maybe some other method to grab
> protocol, host(if external or not) when user clicks link in webView?

So, you only want to handle certain links, and pass on to WebKit those which
you can't handle? Is that correct?

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


Re: Metaclass mystery

2009-05-30 Thread LittleGrasshopper
On May 30, 4:01 pm, LittleGrasshopper  wrote:
> I am experimenting with metaclasses, trying to figure out how things
> are put together. At the moment I am baffled by the output of the
> following code:
>
> 
> """
> Output is:
>
> instance of metaclass MyMeta being created
> (, )
> instance of metaclass MyNewMeta being created
> instance of metaclass MyMeta being created    Why this?
> (, )
>
> """
>
> class MyMeta(type):
>     def __new__(meta, classname, bases, classDict):
>         print 'instance of metaclass MyMeta being created'
>         return type.__new__(meta, classname, bases, classDict)
>
> class MyNewMeta(type):
>     def __new__(meta, classname, bases, classDict):
>         print 'instance of metaclass MyNewMeta being created'
>         return type(classname, bases, classDict)
>
> """
> Notice that a metaclass can be a factory function:
> def f(classname, bases, classDict):
>     return type(classname, bases, classDict)
>
> class MyClass(object):
>     __metaclass__ = f
> """
>
> class MyClass(object):
>     __metaclass__ = MyMeta
>
> print (MyClass.__class__, MyClass.__metaclass__)
>
> class MySubClass(MyClass):
>     __metaclass__ = MyNewMeta
>
> print (MySubClass.__class__, MySubClass.__metaclass__)
> 
> Honestly, I don't know why this line:
> instance of metaclass MyMeta being created    Why this?
> is being output when the MySubClass class object is instantiated.
> MyNewMeta's __new__ method simply instantiates type directly (which I
> know shouldn't be done, but I'm just experimenting and trying to
> understand the code's output.)
>
> I would really appreciate some ideas.

This is my working theory:

return type(classname, bases, classDict), in MyNewMeta.__new__(),
actually calls type.__new__(type, classname, bases, classDict). I
think the magic happens in this method call. This method must look at
bases and notice that MySubClass extends MyClass, and that MyClass'
type is MyMeta, so instead of instantiating a 'type' object it decides
to instantiate a 'MyMeta' object, which accounts for the output.

Seriously, metaclasses are making my brain hurt. How do people like
Michele Simionato and David Mertz figure these things out? Does it all
come to looking at the C source code for the CPython interpreter?

Brain hurts, seriously.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Q: finding distance between 2 time's

2009-05-30 Thread John Machin
On May 31, 7:37 am, jkv  wrote:
> mar...@hvidberg.net wrote:
> > Thanks both
>
> > The first answer is quite instuctive, the other one might be the one
> > I'll use in t
>
> I didn't receive the other answer, could you please forward it to me?> So 2x 
> thanks.
>
> You are welcome.
>
> I took another look at your code, and you can compress it all to a if
> "oneliner":
> (and thanks to Steven for the <= reminder)
>
>         if os.path.isfile(x):
>             nSize = os.path.getsize(x)
>             #if oneliner
>             if time.mktime(time.localtime()) -
> time.mktime(time.localtime(os.path.getmtime(x))) <= 3600:
>               print ('HT fil -> %s %d @ %s') % (x, nSize, time.asctime)

There is no virtue in onelinedness if the code is unclear and/or
incorrect.

Problems:
(1) Converting UTC time to local time and back again??

  > import time
  You are in a maze of twisty little functions, all alike.

So it can pay to do things a step at a time, and leave a note of your
intentions at each step.

(2) Consider grabbing the "current time" once instead of each for each
file

(3) asctime ... you are missing (an_argument)

import os, time, sys

def buildList(directory):
listing = os.listdir(directory)
now_utc_secs = time.time()
for x in os.listdir(directory):
x = os.path.join(directory, x)
if os.path.isdir(x):
print ('dir -> %s') % x
elif os.path.isfile(x):
mtime_utc_secs = os.path.getmtime(x)
if now_utc_secs - mtime_utc_secs <= 3600.0:
nSize = os.path.getsize(x)
mtime_local_tuple = time.localtime(mtime_utc_secs)
mtime_local_text = time.asctime(mtime_local_tuple)
print ('fil -> %s %d @ %s') % (x, nSize,
mtime_local_text)

tstN = time.localtime()
print tstN
print "Time now: %s" % time.asctime(tstN)
buildList(sys.argv[1])

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


Re: Metaclass mystery

2009-05-30 Thread Carl Banks
On May 30, 5:32 pm, LittleGrasshopper  wrote:
> On May 30, 4:01 pm, LittleGrasshopper  wrote:
>
>
>
> > I am experimenting with metaclasses, trying to figure out how things
> > are put together. At the moment I am baffled by the output of the
> > following code:
>
> > 
> > """
> > Output is:
>
> > instance of metaclass MyMeta being created
> > (, )
> > instance of metaclass MyNewMeta being created
> > instance of metaclass MyMeta being created    Why this?
> > (, )
>
> > """
>
> > class MyMeta(type):
> >     def __new__(meta, classname, bases, classDict):
> >         print 'instance of metaclass MyMeta being created'
> >         return type.__new__(meta, classname, bases, classDict)
>
> > class MyNewMeta(type):
> >     def __new__(meta, classname, bases, classDict):
> >         print 'instance of metaclass MyNewMeta being created'
> >         return type(classname, bases, classDict)
>
> > """
> > Notice that a metaclass can be a factory function:
> > def f(classname, bases, classDict):
> >     return type(classname, bases, classDict)
>
> > class MyClass(object):
> >     __metaclass__ = f
> > """
>
> > class MyClass(object):
> >     __metaclass__ = MyMeta
>
> > print (MyClass.__class__, MyClass.__metaclass__)
>
> > class MySubClass(MyClass):
> >     __metaclass__ = MyNewMeta
>
> > print (MySubClass.__class__, MySubClass.__metaclass__)
> > 
> > Honestly, I don't know why this line:
> > instance of metaclass MyMeta being created    Why this?
> > is being output when the MySubClass class object is instantiated.
> > MyNewMeta's __new__ method simply instantiates type directly (which I
> > know shouldn't be done, but I'm just experimenting and trying to
> > understand the code's output.)
>
> > I would really appreciate some ideas.
>
> This is my working theory:
>
> return type(classname, bases, classDict), in MyNewMeta.__new__(),
> actually calls type.__new__(type, classname, bases, classDict). I
> think the magic happens in this method call. This method must look at
> bases and notice that MySubClass extends MyClass, and that MyClass'
> type is MyMeta, so instead of instantiating a 'type' object it decides
> to instantiate a 'MyMeta' object, which accounts for the output.

That's correct.  A type's metaclass has to be a not-necessarily proper
superclass of the all the bases' metaclasses.

Whenever possible type() will figure the most derived metaclass of all
the bases and use that as the metaclass, but sometimes it can't be
done.  Consider the following:


class AMeta(type):
pass

class A(object):
__metaclass__ = AMeta

class BMeta(type):
pass

class B(object):
__metaclass__ = BMeta

class C(A,B):
pass  # this will raise exception

class CMeta(AMeta,BMeta):
pass

class C(A,B):
__metaclass__ = CMeta # this will work ok



> Seriously, metaclasses are making my brain hurt. How do people like
> Michele Simionato and David Mertz figure these things out? Does it all
> come to looking at the C source code for the CPython interpreter?
>
> Brain hurts, seriously.

I actually did rely on looking at the C source.  There's a surprising
amount of checking involving metaclass, layout, special methods, and
so on that is involved when creating a new type.


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


Re: Metaclass mystery

2009-05-30 Thread LittleGrasshopper
On May 30, 6:15 pm, Carl Banks  wrote:
> On May 30, 5:32 pm, LittleGrasshopper  wrote:
>
>
>
> > On May 30, 4:01 pm, LittleGrasshopper  wrote:
>
> > > I am experimenting with metaclasses, trying to figure out how things
> > > are put together. At the moment I am baffled by the output of the
> > > following code:
>
> > > 
> > > """
> > > Output is:
>
> > > instance of metaclass MyMeta being created
> > > (, )
> > > instance of metaclass MyNewMeta being created
> > > instance of metaclass MyMeta being created    Why this?
> > > (, )
>
> > > """
>
> > > class MyMeta(type):
> > >     def __new__(meta, classname, bases, classDict):
> > >         print 'instance of metaclass MyMeta being created'
> > >         return type.__new__(meta, classname, bases, classDict)
>
> > > class MyNewMeta(type):
> > >     def __new__(meta, classname, bases, classDict):
> > >         print 'instance of metaclass MyNewMeta being created'
> > >         return type(classname, bases, classDict)
>
> > > """
> > > Notice that a metaclass can be a factory function:
> > > def f(classname, bases, classDict):
> > >     return type(classname, bases, classDict)
>
> > > class MyClass(object):
> > >     __metaclass__ = f
> > > """
>
> > > class MyClass(object):
> > >     __metaclass__ = MyMeta
>
> > > print (MyClass.__class__, MyClass.__metaclass__)
>
> > > class MySubClass(MyClass):
> > >     __metaclass__ = MyNewMeta
>
> > > print (MySubClass.__class__, MySubClass.__metaclass__)
> > > 
> > > Honestly, I don't know why this line:
> > > instance of metaclass MyMeta being created    Why this?
> > > is being output when the MySubClass class object is instantiated.
> > > MyNewMeta's __new__ method simply instantiates type directly (which I
> > > know shouldn't be done, but I'm just experimenting and trying to
> > > understand the code's output.)
>
> > > I would really appreciate some ideas.
>
> > This is my working theory:
>
> > return type(classname, bases, classDict), in MyNewMeta.__new__(),
> > actually calls type.__new__(type, classname, bases, classDict). I
> > think the magic happens in this method call. This method must look at
> > bases and notice that MySubClass extends MyClass, and that MyClass'
> > type is MyMeta, so instead of instantiating a 'type' object it decides
> > to instantiate a 'MyMeta' object, which accounts for the output.
>
> That's correct.  A type's metaclass has to be a not-necessarily proper
> superclass of the all the bases' metaclasses.
>
> Whenever possible type() will figure the most derived metaclass of all
> the bases and use that as the metaclass, but sometimes it can't be
> done.  Consider the following:
>
> class AMeta(type):
>     pass
>
> class A(object):
>     __metaclass__ = AMeta
>
> class BMeta(type):
>     pass
>
> class B(object):
>     __metaclass__ = BMeta
>
> class C(A,B):
>     pass  # this will raise exception
>
> class CMeta(AMeta,BMeta):
>     pass
>
> class C(A,B):
>     __metaclass__ = CMeta # this will work ok
>
> > Seriously, metaclasses are making my brain hurt. How do people like
> > Michele Simionato and David Mertz figure these things out? Does it all
> > come to looking at the C source code for the CPython interpreter?
>
> > Brain hurts, seriously.
>
> I actually did rely on looking at the C source.  There's a surprising
> amount of checking involving metaclass, layout, special methods, and
> so on that is involved when creating a new type.
>
> Carl Banks

Thanks a lot, Carl. It's funny that you would post that example, as
just 15 minutes ago I wrote almost the exact same code in my testing:

class XMeta(type):
pass

class YMeta(type):
pass

class X(object):
__metaclass__ = XMeta

class Y(object):
__metaclass__ = YMeta

"""
This causes an error, since there is no leafmost metaclass in the set
{XMeta, YMeta}:

class Z(X, Y):
pass
"""

"""
The way to solve this is to create a common derived metaclass and use
that
one for class z:
"""

class ZMeta(XMeta, YMeta):
pass

class Z(X, Y):
__metaclass__ = ZMeta

Looking at the C code sounds like a daunting task, but I might take a
plunge and give it a try, since it seems that guessing via
experimental results is not the most optimal way to go about it.

Thank you again.
-- 
http://mail.python.org/mailman/listinfo/python-list


Release PyInstaller exe installer using distutils for windows.

2009-05-30 Thread 鎌土記良
I created pyInstaller exe installer using distutils only for windows and
upload onto the following URL.
It makes you to build windows executable file with setup.py's setup
function.
Setup function is unmounted function on original pyInstaller package,
and this installer enables UPX exe archive functions as default.

http://moco.sakura.ne.jp/wp-content/uploads/2009/05/pyinstaller-13win32.exe

If you want to use this package,
Please write following code on setup.py.

from pyInstaller import *
setup('testre.py',onefile=True,name='test')


Thank you.

// kr2: nkam...@gmail.com

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


Re: What text editor is everyone using for Python

2009-05-30 Thread Lawrence D'Oliveiro
In message , Lie Ryan wrote:

> norseman wrote:
>
>> Suggestion:
>> Take a look at the top two most used OS you use and learn the default
>> (most often available) text editors that come with them.
> 
> Which means Notepad on Windows?

Or you could take a Linux preinstallation on a Live CD/DVD or USB stick. 
There's no Windows system so brain-dead it can't be fixed with a simple 
ctrl-alt-del. :)

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


Re: DB-API execute params, am I missing something?

2009-05-30 Thread Lawrence D'Oliveiro
In message <55520c08-5b02-4231-
b0f3-74eadecd6...@g1g2000yqh.googlegroups.com>, John Machin wrote:

> ... suggest a better way.



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


(windows) How to get information as string from a GreenHills project?

2009-05-30 Thread Jebel
I use python to start a GreenHills project in command line. And the
GreenHills project create a string which is include the information
that needed to be analyzed. How Can I return this string from
GreenHills project to python?

Thanks a lot.

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


Challenge supporting custom deepcopy with inheritance

2009-05-30 Thread Michael H . Goldwasser

I've been playing around recently with customizing the deepcopy
semantics within an inheritance hierarchy, and run across the
following hurdle.

Assume that class B inherits from class A, and that class A has
legitimately customized its deepcopy semantics (but in a way that is
not known to class B).  If we want a deepcopy of B to be defined so
that relevant state inherited from A is copied as would be done for
class A, and with B able to control how to deepcopy the extra state
that it introduces.  I cannot immediately find a general way to
properly implement the deepcopy of B.

To make the discussion tangible, I include an outrageously artificial
example in the code fragment below.  In this code, class A implements
__deepcopy__ to intentionally create a clone that has a reversed
version of a list instance.  The beginning of the main test
demonstrates that an instance of A can successfully be cloned with this
semantics.  But the attempt to deepcopy an instance of B fails (the
current failure is because B has not implemented __deepcopy__ and the
inherited version of A.__deepcopy__ assumes the wrong constructor
signature).

The question is how class B should defined so that its instances can
"inherit" this deepcopy semantic.  Ideally, I'd like to find a recipe
for class B that works regardless of how A accomplishes its semantics.
That is, I can find ways to rewrite A to be more supportive of B's
goal, but I'd like to know if there is a general solution that could
be used if the author of B doesn't have control over A's
implementation.

Here are some initial thoughts:

 * I'm only concerned with single inheritance (thankfully)

 * If A had relied on use of __getstate__ and __setstate__ to affect
   the desired change, then the inheritance works as desired.  But I
   have an application where I want to leave the default pickling
   behavior alone, and only to change what happens with clones.

 * If we did not use inheritance, but instead had a class B that had
   an A instance in its state, this is easy. We would call deepcopy
   on the A instance to get an acceptable copy of that instance for
   the new B.  But in the example below, we insist on B being a
   subtype of A.

 * If A.__deepcopy__ had started
dup = A(self.__aTag)
   rather than
dup = self.__class__(self.__aTag)

   then the following perverse approach for B.__deepcopy__ succeeds:

def __deepcopy__(self, memo={}):
tempBTag = self.__bTag
tempBList = self.__bList
del self.__bTag # hide these attributes to avoid risk
del self.__bList# that they affect A.__deepcopy__
dupA = A.__deepcopy__(self, memo)
self.__bTag = tempBTag
self.__bList = tempBList
dup = B.__new__(B)
memo[id(self)] = dup
dup.__dict__ = dupA.__dict__
dup.__bTag = copy.deepcopy(self.__bTag, memo)
dup.__bList = copy.deepcopy(self.__bList, memo)
return dup

but this again presumes that we have some control over the
strategy employed by class A, it relies on the fact that
A.__deepcopy__ succeeded when the first parameter was actually an
instance of class B rather than class A, and it seems like a
perverse strategy on the whole, even if it does succeed on this
example.  Also, there could be a flaw n that dupA may recursively
have included a reference back to that instance, yet we'd need
such a reference to be to dup not dupA in the end.

I'm hoping that I've just missed a cleaner way to do this.
I welcome any comments, discussion, or suggestions.

With regard,
Michael

---
import copy

class A(object):
def __init__(self, aTag):
self.__aTag = aTag
self.__aList = []

def addA(self, val):
self.__aList.append(val)

def __repr__(self):
return (
"aTag: " + self.__aTag +
"\naList ("+str(id(self.__aList))+"): " + repr(self.__aList)
)

def __deepcopy__(self, memo={}):
dup = self.__class__(self.__aTag)
memo[id(self)] = dup
dup.__aList = copy.deepcopy(self.__aList, memo)
dup.__aList.reverse()
return dup

class B(A):
def __init__(self, aTag, bTag):
A.__init__(self, aTag)
self.__bTag = bTag
self.__bList = []

def addB(self, val):
self.__bList.append(val)

def __repr__(self):
return (
A.__repr__(self) +
"\nbTag: " + self.__bTag +
"\nbList ("+str(id(self.__bList))+"): " + repr(self.__bList)
)

#  How can B support a deepcopy that provides deepcopy of bTag and
#  bList while getting the semantics of A's deepcopy for
#  attributes defined by A?


if __name__ == '__main__':
print "Test of class A\n==="
foo = A('alice')
foo.addA(1)
foo.addA(2)

bar = copy.deepcopy(foo)

Have a variable column length in printf

2009-05-30 Thread Cameron Pulsford
Hello all, I'm trying to pretty print a list, so I am doing something like
print '%3d' % integer

only I would like that 3 to be a variable, instead of hardcoded. Is this
possible, or are there any other ways to accomplish this? Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: decoding keyboard input when using curses

2009-05-30 Thread Chris Jones
On Sat, May 30, 2009 at 04:55:19PM EDT, Arnaud Delobelle wrote:

> Hi all,

Disclaimer: I am not familiar with the curses python implementation and
I'm neither an ncurses nor a "unicode" expert by a long shot.

:-)

> I am looking for advice on how to use unicode with curses.  First I will
> explain my understanding of how curses deals with keyboard input and how
> it differs with what I would like.
> 
> The curses module has a window.getch() function to capture keyboard
> input.  This function returns an integer which is more or less:
> 
> * a byte if the key which was pressed is a printable character (e.g. a,
>   F, &);
> 
> * an integer > 255 if it is a special key, e.g. if you press KEY_UP it
>   returns 259.

The getch(3NCURSES) function returns an integer. Provide it's large
enough to accomodate the highest possible value, the actual size in
bytes of the integer should be irrelevant.

> As far as I know, curses is totally unicode unaware, 

My impression is that rather than "unicode unaware", it is "unicode
transparent" - or (nitpicking) "UTF8 transparent" - since I'm not sure
other flavors of unicode are supported.

> so if the key pressed is printable but not ASCII, 

.. nitpicking again, but ASCII is a 7-bit encoding: 0-127.

> the getch() function will return one or more bytes depending on the
> encoding in the terminal.

I don't know about the python implementation, but my guess is that it
should closely follow the underlying ncurses API - so the above is
basically correct, although it's not a question of the number of bytes
but rather the returned range of integers - if your locale is en.US then
that should be 0-255.. if it is en_US.utf8 the range is considerably
larger.

> E.g. given utf-8 encoding, if I press the key 'é' on my keyboard (which
> encoded as '\xc3\xa9' in utf-8), I will need two calls to getch() to get
> this: the first one will return 0xC3 and the second one 0xA9.

No. A single call to getch() will grab your " é" and return 0xc3a9,
decimal 50089.

> Instead of getting a stream of bytes and special keycodes (with value >
> 255) from getch(), what I want is a stream of *unicode characters* and
> special keycodes.

This is what getch(3NCURSES) does: it returns the integer value of one
"unicode character".

Likewise, I would assume that looping over the python equivalent of
getch() will not return a stream of bytes but rather a "stream" of
integers that map one to one to the "unicode characters" that were
entered at the terminal.

Note: I am only familiar with languages such as English, Spanish,
French, etc. where only one terminal cell is used for each glyph. My
understanding is that things get somewhat more complicated with
languages that require so-called "wide characters" - two terminal cells
per character, but that's a different issue.

> So, still assuming utf-8 encoding in the terminal, if I type:
> 
> Té[KEY_UP]ça
> 
> iterating call to the getch() function will give me this sequence of
> integers:
> 
> 84, 195, 169, 259,   195, 167, 97
> T-  é---  KEY_UP ç---  a-
> 
> But what I want to get this stream instead:
> 
> u'T', u'é', 259, u'ç', u'a'

No, for the above, getch() will return:

 84, 50089, 259, 50087, 97

.. which is "functionally" equivalent to:

 u'T', u'é', 259, u'ç', u'a'

[..]

So shouldn't this issue boil down to just a matter of casting the
integers to the "u" data type?

This short snippet may help clarify the above:

---
#include 
#include 
#include 
#include 
#include 

int unichar;

int main(int argc, char *argv[])
{
  setlocale(LC_ALL, "en_US.UTF.8");/* make sure UTF8   */
  initscr();   /* start curses mode*/
  raw();
  keypad(stdscr, TRUE);/* pass special keys*/
  unichar = getch();   /* read terminal*/

  mvprintw(24, 0, "Key pressed is = %4x ", unichar);

  refresh();
  getch(); /* wait */
  endwin();/* leave curses mode*/
  return 0;
}
---

Hopefully you have access to a C compiler:

$ gcc -lncurses uni00.c -o uni00

Hope this helps... Whatever the case, please keep me posted.

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


Re: hash and __eq__

2009-05-30 Thread Arnaud Delobelle
Piet van Oostrum  writes:

>> Arnaud Delobelle  (AD) wrote:
>
>>AD> Piet van Oostrum  writes:
> Aaron Brady  (AB) wrote:
 
>>AB> I am writing a mapping object, and I want to ask about the details of
>>AB> __hash__ and __eq__.  IIUC if I understand correctly, the Python
>>AB> dict's keys' hash codes are looked up first in O( 1 ), then all the
>>AB> matching hash entries are compared on equality in O( n ).  That is,
>>AB> the hash code just really narrows down the search.  Am I correct?
 
 The O(1) part is correct. The O(n) is incorrect if n as usual is taken
 to be the number of keys in the dict. But it is true if n is the number
 of keys in the `bucket' that belongs to the hash value. The average
 complexity of looking up a key is O(1).
>
>>AD> As all the keys could be in the same bucket, O(n) seems correct to me
>>AD> (with n the total number of keys).
>
> For worst case only (mainly with a badly designed hash function). 

... or a cleverly designed data set!

AFAIK, 'complexity' means 'worst case complexity' unless otherwise
stated.

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