discarded iterator.next() at interactive global scope doesn't bump interator??

2005-09-04 Thread Bengt Richter
Is this a well known bug that's been fixed?
I couldn't find any discussion of it, but maybe my googling's off today ;-/

 >>> def foo():
 ... it = iter(range(10))
 ... while True:
 ... i = it.next()
 ... print i
 ... if i&3==0:
 ... print 'skipping next'
 ... it.next()
 ...
 >>>

--< This looks ok:
 >>> foo()
 0
 skipping next
 2
 3
 4
 skipping next
 6
 7
 8
 skipping next
 Traceback (most recent call last):
   File "", line 1, in ?
   File "", line 4, in foo
 StopIteration
 >>>
< now at interactive global scope:
 >>> it = iter(range(10))
 >>> while True:
 ... i = it.next()
 ... print i
 ... if i&3==0:
 ... print 'skipping next'
 ... it.next()
 ...
-", line 2, in ?
 StopIteration

< but add a print, and all is well??
 >>> it = iter(range(10))
 >>> while True:
 ... i = it.next()
 ... print i
 ... if i&3==0:
 ... print 'skipping next'
 ... print '-->', it.next()
 ...
 0
 skipping next
 --> 1
 2
 3
 4
 skipping next
 --> 5
 6
 7


 8
 skipping next
 --> 9
 Traceback (most recent call last):
   File "", line 2, in ?
 StopIteration

I guess it could be in the read-eval-print loop

I have an old 2.4 on NT4 and am running it in a console window.
Python 2.4b1 (#56, Nov  3 2004, 01:47:27)
[GCC 3.2.3 (mingw special 20030504-1)] on win32
Type "help", "copyright", "credits" or "license" for more information. 

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


Re: Sockets: code works locally but fails over LAN

2005-09-04 Thread Bryan Olson
n00m wrote:
 > Bryan;
 > Look at how I corrected your the very first version
 > (see added arguments in both functions). And now it
 > really can handle multiple connections!

Ah, yes, I see. (In my defense, I had already fixed that bug in
my second version.)


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


Re: discarded iterator.next() at interactive global scope doesn't bump interator??

2005-09-04 Thread Peter Otten
Bengt Richter wrote:

[it.next() appears to be a noop in the interactive interpreter]

> I guess it could be in the read-eval-print loop

Indeed: 

>>> for i in range(5):
... 42
...
42
42
42
42
42

Whereas:

>>> for i in range(5):
... None
>>>

Every line with an expression that doesn't evaluate to None is echoed.
Therefore

it.next()

and

print it.next()

look the same when repr(it.next()) == str(it.next()).

No bug :-)

Peter

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


Re: OpenSource documentation problems

2005-09-04 Thread Paul Rubin
Steve Holden <[EMAIL PROTECTED]> writes:
> > legitimate.  Python's core developers are in a leadership position for
> > Python whether they like it or not; and users and volunteers absorb
> > the attitudes of the leaders.
> 
> So, what you are saying is because the developers (I explain in
> another post on this thread that I'm not a developer, but I *am* a
> director for the PSF), having taken the time and trouble to produce
> something that you (for want of a better example) find incredibly
> useful, are now beholden to improve it still further and make it still
> more useful, because otherwise nobody else will feel it necessary to
> create and maintain documentation of high quality?

The developers are not beholden to anyone for anything, but if they
also act as Python advocates and claiming that Python is good or
well-documented software, they're the ones responsible for backing up
the claims.  If they want to say that it's experimental/unreliable or
poorly documented, their responsibility is much lower.  But they
promote it as being suitable for critical applications.

> Bear in mind that the PSF made its very first grants last year.

OK, I thought it had been doing stuff like that for longer.  My error.

> The reason none of those grants was awarded to a documentation
> project was that the (volunteer) Grants Committee and helpers didn't
> see any documentation projects worthy of support.

It looks to me like the approach of calling for random proposals
doesn't get the right kind of proposals.  The PSF should instead make
its own list of stuff it wants done, and call for people to step
forward to do those things.  Or if there's enough funds, it should
hire some full time people and assign tasks to them.

> What Python really needs is for Python enthusiasts to understand that
> the future of the language is largely in their hands. It's easy to say
> "but I can't do this" or "I can't do that". Forget such
> negativity. Find something you *can* do to improve Python - document a
> module, chair a conference, or (as *you* already do) contribute your
> knowledge and experience to c.l.py to improve the level of Python
> awareness.

No, that's completely wrong; most of the suggestions I've made to
improve Python cannot be made by "outsiders".  I can't add modules to
the stdlib that I think belong there.  Only the core developers can do
that.  I can't add conditional expressions to the language to stop
clpy newbies from asking for them practically every other week.  Only
the BDFL can do that.  I can't phone up John Shipman and ask him on
behalf of the PSF if he'll re-license his tkinter manual so Python can
include it, because only the PSF can do that.  You get the idea.

Anyway, I don't want to be a Python developer or Python volunteer.
I'm just a user.  I do volunteer work on other projects that don't
happen to be Python.  If I had more time or energy available to do
more volunteer work, I'd still put it in those other projects and not
in Python.  Python for me is a tool, like a hammer.  If the hammers I
use keep breaking or hitting the nails the wrong way, I might contact
the manufacturer and urge various improvements, but I'm not interested
in their suggestions that I come work on improving the hammers myself.
I want to build houses, not make hammers.  I'm not bashing Python and
I'm happy to be a user, but my volunteering priorities are elsewhere.

> I have done all of these things, and I firmly believe that each could
> have been done better. Unfortunately those with superior skills chose
> not to engage the Python community. Which left it stuck with me. Poor
> Python.

I don't know that my skills are so superior, but I believe I'm
engaging the Python community.

> Most of all, realise that there is no "us" and "them". *We* are
> "them", and it's up to *us* to accept that and try to improve things.

"We" (i.e. "you") is not "me" except on an incidental basis.  See above.

> Please understand that I don't mean to belittle anyone who has made
> any kind of contribution to Python (even Xah Lee has his place, though
> I tremble to think so). I just want more Python users to understand
> that there are many contributions they could make that are well within
> their capabilities, and to get off their butts and start doing
> something!

Thanks for the pep talk but you're not telling me anything I don't
already know.  Really, I like your posts and appreciate the work
you've done, but that particular line of advice comes across as
patronizing.  I know what projects I want to allocate my volunteer
development time to and Python isn't one of them.
-- 
http://mail.python.org/mailman/listinfo/python-list


Code for generating validation codes (in images)

2005-09-04 Thread morphex
Hi,

does anyone of you know of some code I can use to generate validation
code images?

Those images you can see on various login forms used to prevent bots
for performing a brute-force attack..

Thanks,

Morten

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


Re: Code for generating validation codes (in images)

2005-09-04 Thread Paul Rubin
"morphex" <[EMAIL PROTECTED]> writes:
> does anyone of you know of some code I can use to generate validation
> code images?
> 
> Those images you can see on various login forms used to prevent bots
> for performing a brute-force attack..

http://en.wikipedia.org/wiki/CAPTCHA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Code for generating validation codes (in images)

2005-09-04 Thread Giuseppe di Sabato (aka deelan)
morphex wrote:
> Hi,
> 
> does anyone of you know of some code I can use to generate validation
> code images?
> 
> Those images you can see on various login forms used to prevent bots
> for performing a brute-force attack..

take a look at the "pycaptcha" package:


later,
deelan.

-- 
deelan, #1 fan of adriana lima!

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


Re: Code for generating validation codes (in images)

2005-09-04 Thread morphex
Great, thanks to you both.  :-)

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


Re: Proposal: add sys to __builtins__

2005-09-04 Thread Xiao Jianfeng
Paul Watson wrote:

>This sounds pretty interesting.  How about a switch to invoke this 
>handling for the one-liner crowd and those who wish to use it?
>
>  
>

>Somehow, I never heard any C programmers suggest that the default 
>processing not include the need for:
>
>#include 
>  
>
 I think it is because that we cannot modify the C language, but
 python is a language that's still evolving.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Command config, quitting, binary, Timer

2005-09-04 Thread Diez B. Roggisch
> ! import Tkinter
> ! def dogo():
> ! while 1:
> ! b.config(command=lambda:None)
> ! root = Tkinter.Tk()
> ! b = Tkinter.Button(root, text="Go", command=dogo)
> ! b.pack()
> ! root.mainloop()


I guess tkinter has to keep a name-reference pair (some days a 
discussion about this arose and /F said something like that). It 
certainly is somewhat ugly - but there might be very good reasons to do so.
> 
> Note: I have found this problem because in a small program I have a
> Start button that becomes a Stop, so the bound command must change each
> time. (Maybe there is some other solution, like hiding/unhiding the
> buttons, I don't know.)

Had that to do myself - my solution was to use the buttons text for 
swich-casing. I guess you change the buttons text anyways (otherwise 
nobody would know that its semantic changed), so do something like this 
(untetsted)

def start_stop_callback():
 if b.get_config("text") == "Start":
b.config(text="Stop")

 else:
b.config(text="Start")



> Sometimes I need something like the Timer of Java, it generates "ticks"
> and calls a function for each tick. The tick frequency can be set, or
> they can be stopped.
> 
> In another Newsgroup someone has suggested me that the callLater of
> Twisted can solve my problem, but I think it's a quite common thing, so
> I think that maybe it can be added to the threading standard module.
> 
> This is a rough Python version of mine (without comments), it's not a
> true Metronome because it counts the delay time after the end of the
> last function call. This class also seems fragile, sometimes it gives
> me problems, and I cannot use too much concurrent metronomes, etc. It's
> quite
> 
> Maybe someone can suggest me how to improve it.
> 

Instead of several metronomes, use one thread and compute the gresatest 
common divisor for all scheduled timings. Use that as sleep interval 
(your "ticks"), and count ticks for each timer. You should introduce a 
lower thrshold on tick-length, otherwise you can get into trouble.


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


Re: os.system(r"ls") prints to screen??

2005-09-04 Thread Xah Lee
Steve Holden wrote:
> This is all pretty basic stuff. Perhaps you should stop your verbal
> assault on the computer science community and start to learn the
> principles of what you are doing.

is this a supressed behavior that a human animal can finally
instinctively and justifiably release at another in a group frenzy?

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

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

documentation error

2005-09-04 Thread bill
>From 3.2 in the Reference Manual "The Standard Type Hierarchy":

"Integers
These represent elements from the mathematical set of whole
numbers."

The generally recognized definition of a 'whole number' is zero and the
positive integers.  That is to say, -1 is not a whole number.  The
documentation ought to replace "whole numbers" with "integers".

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


Re: discarded iterator.next() at interactive global scope doesn't bump interator??

2005-09-04 Thread Bengt Richter
On Sun, 04 Sep 2005 10:10:33 +0200, Peter Otten <[EMAIL PROTECTED]> wrote:

>Bengt Richter wrote:
>
>[it.next() appears to be a noop in the interactive interpreter]
>
>> I guess it could be in the read-eval-print loop
>
>Indeed: 
>
 for i in range(5):
>... 42
>...
>42
>42
>42
>42
>42
>
>Whereas:
>
 for i in range(5):
>... None

>
>Every line with an expression that doesn't evaluate to None is echoed.
>Therefore
>
>it.next()
>
>and
>
>print it.next()
>
>look the same when repr(it.next()) == str(it.next()).
>
>No bug :-)
>
D'oh. I said it, but I didn't hear myself ;-/

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


Re: Proposal: add sys to __builtins__

2005-09-04 Thread Colin J. Williams
Rick Wotnaz wrote:
> Michael Hoffman <[EMAIL PROTECTED]> wrote in
> news:[EMAIL PROTECTED]: 
> 
> 
>>What would people think about adding sys to __builtins__ so that
>>"import sys" is no longer necessary? This is something I must
>>add to every script I write that's not a one-liner since they
>>have this idiom at the bottom:
>>
>>if __name__ == "__main__":
>> sys.exit(main(sys.argv[1:]))
>>
>>Additionally, the necessity of "import sys" makes some
>>one-liners a little more unwieldy than they should be--it is
>>surely the module I am missing the most in one-liners. For
>>example, with this proposal, this inelegant one-liner:
>>
>>$ python -c "import sys; print
>>''.join(sorted(sys.stdin.readlines()))" 
>>
>>could be replaced by:
>>
>>$ python -c "print ''.join(sorted(sys.stdin.readlines()))"
>>
>>Since sys is surely the most commonly used module (it is
>>imported in 108 of 188 Python 2.4 stdlib modules on my system,
>>and certainly more than any other module), I would hope few
>>people would be affected by a namespace collision.
>>
>>Other languages (e.g. C#) always make their system namespace
>>available without needing a special import.
>>
>>In short, given the wide use of sys, its unambiguous nature, and
>>the fact that it really is built-in already, although not
>>exposed as such, I think we would be better off if sys were
>>always allowed even without an import statement.
> 
> 
> +1 here. As far as I'm concerned, both os and sys could be special-
> cased that way. That said, I would guess the likelihood of that 
> happening is 0. 
> 
+1 for both.

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


Re: 'isa' keyword

2005-09-04 Thread Colin J. Williams
Rocco Moretti wrote:
> Terry Hancock wrote:
> 
>> On Thursday 01 September 2005 07:28 am, Fuzzyman wrote:
>>
>>> What's the difference between this and ``isinstance`` ?
>>
>>
>> I must confess that an "isa" operator sounds like it would
>> have been slightly nicer syntax than the isinstance() built-in
>> function. But not enough nicer to change, IMHO.
> 
> 
> Especially conidering that checking parameters with "isinstance" is 
> considered bad form with Python's duck typing.
Rocco,

Could you elaborate on that please?

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


Re: documentation error

2005-09-04 Thread tiissa
bill wrote:
>>From 3.2 in the Reference Manual "The Standard Type Hierarchy":
> 
> "Integers
> These represent elements from the mathematical set of whole
> numbers."
> 
> The generally recognized definition of a 'whole number' is zero and the
> positive integers.

This term is ambiguous as it seems to be used for both natural numbers 
and signed numbers [1].


> That is to say, -1 is not a whole number.  The
> documentation ought to replace "whole numbers" with "integers".

Then you get a circular definition, arguably not very useful.
Why not simply precise 'signed whole numbers'?


However, it can be noted that the first instance of such types provides 
the range -2147483648, 2147483647 which cannot be mistaken for natural 
numbers.


[1] http://en.wikipedia.org/wiki/Whole_numbers
-- 
http://mail.python.org/mailman/listinfo/python-list


ANN: SIP v4.3 Released

2005-09-04 Thread Phil Thompson
Riverbank Computing is pleased to announce the release of SIP v4.3 available 
from http://www.riverbankcomputing.co.uk/sip/.

SIP is a tool for generating Python modules that wrap C or C++ libraries.  It 
is similar to SWIG.  It is used to generate PyQt and PyKDE.  Full 
documentation is available at 
http://www.river-bank.demon.co.uk/docs/sip/sipref.html.

SIP is licensed under the Python License and runs on Windows, UNIX, Linux and 
MacOS/X.  SIP requires Python v2.3 or later (SIP v3.x is available to support 
earlier versions of Python).

This release includes the following changes:

- Python's cyclic garbage collector is fully supported
- support for mapping between Python and C++ exceptions
- support for __pos__ and __abs__
- the support for accessing C structure members has been greatly improved
- C structures can have constructors and destructors defined so that they can
  be made to behave more Pythonically.

Other features of SIP include:

- support for Python new-style classes
- generated modules are quick to import, even for large libraries
- support for Qt's signal/slot mechanism
- thread support
- the ability to re-implement C++ abstract and virtual methods in Python
- the ability to define Python classes that derive from abstract C++ classes
- the ability to spread a class hierarchy across multiple Python modules
- support for C++ namespaces
- support for C++ exceptions
- support for C++ operators
- an extensible build system written in Python that supports over 50
  platform/compiler combinations.
-- 
http://mail.python.org/mailman/listinfo/python-list


Replacing large number of substrings

2005-09-04 Thread Will McGugan
Hi,

Is there a simple way of replacing a large number of substrings in a 
string? I was hoping that str.replace could take a dictionary and use it 
to replace the occurrences of the keys with the dict values, but that 
doesnt seem to be the case.

To clarify, something along these lines..

 >>> dict_replace( "a b c", dict(a="x", b="y") )
"x y c"


Regards,

Will McGugan
--
http://www.kelpiesoft.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sockets: code works locally but fails over LAN

2005-09-04 Thread n00m
Bryan Olson wrote:
> Ah, yes, I see. (In my defense, I had already fixed that bug in
> my second version.)
1.
Yes! I myself noticed that, but your 2nd version looks
a bit more verbose.
2.
This all means... what? ONLY send() vs sendall() matters?
Sometimes send() really sends ALL and my version works too!
I must give it a thorough testing!
3.
See how it looks in SQL Server Profiler (it's its utility for
tracing client/server events) WHEN I started 5 copies of .vbs.
http://free.7host02.com/n00b/SQL_Profiler.gif

Btw, without s2.shutdown(1) those vbs' do NOT disconnect from
the server (see DISCONNECT events on the gif).


The latest python version:
==
import socket, thread

sqls_host, sqls_port = '127.0.0.1', 1433
proxy_host, proxy_port = '127.0.0.1', 1434

# How I tested it:
# sqls_host, sqls_port = 'www.google.com', 80

def VB_SCRIPT(s2, cn):
 while 1:
 data = cn.recv(4096)
 if not data:
 s2.shutdown(1)
 return
 s2.sendall(data)
 print 'VB_SCRIPT:' + data + '\n'

def SQL_SERVER(s2, cn):
 while 1:
 data = s2.recv(4096)
 if not data: return
 cn.sendall(data)
 print 'SQL_SERVER:' + data + '\n'

s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s1.bind((proxy_host, proxy_port))
s1.listen(5)

while 1:
 cn, addr = s1.accept()
 s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 s2.connect((sqls_host, sqls_port))
 thread.start_new_thread(VB_SCRIPT,(s2, cn))
 thread.start_new_thread(SQL_SERVER,(s2, cn))


The vbs text:
==
Set cn = CreateObject("ADODB.Connection")
cn.Open _
"Provider=sqloledb;Data Source=127.0.0.1,1434;" & _
"Network Library=DBMSSOCN;Initial Catalog=pubs;" & _
"User ID=qwe;Password=asdasd;"
cn.Execute _
"select 'AAA';" & _
"waitfor delay '00:00:02'; raiserror('XXX',10,1) with nowait;" & _
"waitfor delay '00:00:02'; raiserror('YYY',10,1) with nowait;" & _
"waitfor delay '00:00:02'; raiserror('ZZZ',10,1) with nowait;" & _
"select 'BBB';"
cn.Close 
Set cn = Nothing

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


sizeof(long) from python

2005-09-04 Thread John

I want to know the sizeof(long) / sizeof(int) ... in C from python.
(This is to read a set of numbers output from a C Code
and can be machine dependent).

Is there an easy way except writing a C program and parsing its output?

Thanks,
--j

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


ANN: PyQt v3.15 Released

2005-09-04 Thread Phil Thompson
Riverbank Computing is pleased to announce the release of PyQt v3.14 available
from http://www.riverbankcomputing.co.uk/pyqt/.

Changes since the last release include:

- improved integration between Qt's ActiveQt framework and Python's win32com
  modules
- support for QScintilla v1.6
- support for Python's cyclic garbage collector.

PyQt is a comprehensive set of Qt bindings for the Python programming language 
and supports the same platforms as Qt.  Like Qt, PyQt is available under the 
GPL (for UNIX, Linux and MacOS/X), a commercial license (for Windows, UNIX, 
Linux and MacOS/X) and a free educational license (for Windows).

PyQt is implemented as a set of 9 extension modules containing 300 classes and 
over 5,750 functions and methods.

PyQt also includes bindings to QScintilla, the port to Qt of the Scintilla 
editor component.

PyQt can be used either as a rapid prototyping tool, or as an alternative to 
C++ for developing large Qt applications.

PyQt includes the pyuic utility which generates Python code to implement user 
interfaces created with Qt Designer in the same way that the uic utility 
generates C++ code.

Third party tools are also available - such as eric3, a comprehensive IDE 
(including an editor, debugger, class browser, integration with Qt Designer, 
re-factoring tools, unit testing tools and integration with source code 
control systems).  eric3 is written entirely using PyQt and is available from 
http://www.die-offenbachs.de/detlev/eric3.html.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sizeof(long) from python

2005-09-04 Thread Simon Percivall
Take a look at the struct module
(http://docs.python.org/lib/module-struct.html), it does what you want.

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


Re: os.system(r"ls") prints to screen??

2005-09-04 Thread Steve Holden
Xah Lee wrote:
> Steve Holden wrote:
> 
>>This is all pretty basic stuff. Perhaps you should stop your verbal
>>assault on the computer science community and start to learn the
>>principles of what you are doing.
> 
> 
> is this a supressed behavior that a human animal can finally
> instinctively and justifiably release at another in a group frenzy?
> 
Seems to me that you'd know more about frenzy than me. Having read many 
of your posts it seems to me that they are deliberately calculated to 
inflame the reader.

To be successfully so critical of others you need to demonstrate 
superior knowledge and judgment, which your original question revealed 
to be lacking.

To put it another way: "People who live in glass houses shouldn't throw 
stones".

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

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


Re: sizeof(long) from python

2005-09-04 Thread John
Thanks a lot, That solved my problem.
--j

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


DrPython debugger

2005-09-04 Thread Pandiani
Hello,
I've been experimenting with different Python IDEs since I started to
learn Python.
I must say that WingIDE is the best I tried, but unfortunately it's not
free and that 10 minutes use is killing me. So I decide to write my
Python programs in DrPython which was recommended by vegaseat.
DrPython is very pretty nad very customizable IDE.My question is how to
debug with DrPython if I have unexpected program behaviour?
Does DrPython support debugging at all? It's seems that it's not have
breakpoints. I usually switch to PythonWin to trace bugs, but I wonder
if there is some plugin for DrPython to allow debugging.
Do you know any?
By the way, I'm using version 3.10.13
Thanks

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


Re: Command config, quitting, binary, Timer

2005-09-04 Thread bearophileHUGS
Witn your suggestions and with some tests and work I've solved most of
the problems, thank you all for the comments.

Peter Hansen:

>What did you expect to happen with the infinite loop inside dogo()?<

I expected that the same memory used by the b.config(command=...) can
be used by the successive update of the command. I am ignorant about
Tkinter, but to me it seems that no new memory is really needed each
loop.

---

Dennis Lee Bieber:

>you are creating an endless number of anonymous functions; they may not be 
>garbage collected until the loop ends<

I don't know, usually the garbage collector is quite efficient :-)


>Would it not be easier to have a single callback, but change the button Label, 
>and have the callback branch based on the current label?<

Right, thank you. It's not a perfect solution, but often it's good
enough.


>No idea -- my only Tkinter program (at work, so I can't test it), doesn't do 
>anything like this.<

With more tests and some googling I have found the problem and the
solution. It seems that the problem was produced by commands like this:
self.canvas.delete(self.id)
Maybe they are called after the Tkinter stop, or window close, or
something like that.

To solve the problem I've added this to the main window:
self.root.protocol('WM_DELETE_WINDOW', self.quitAll)

And inside the self.quitAll method I fix something before calling
self.root.quit()


>I somehow suspect it is faster than a mass of push/pop and shifts...<

Your version is very interesting, thank you. I'll use this modified
version:

! _nibbles = {"0":"", "1":"0001", "2":"0010", "3":"0011",
! "4":"0100", "5":"0101", "6":"0110", "7":"0111",
! "8":"1000", "9":"1001", "A":"1010", "B":"1011",
! "C":"1100", "D":"1101", "E":"1110", "F":"",
! "-":"-"}
!
! def to_base2(number):
! result = [_nibbles[nibble] for nibble in "%X"%number]
! return int("".join(result))

This is faster than my version (but if you are using Psyco for all the
tests, for small numbers my version is faster).
This version is expecially faster for very long numbers.
But the difference between the two versions is quite little still, a C
function like the base2 conversion of GMPY is much faster, so I think
that such common operation can be added to the % formatting sintax.


>Main problem I see with this is that each call to threading.Timer() is 
>creating a NEW thread just for one tick. Lots of overhead involved. I'd create 
>a single thread whose main run method is a continuous loop, using time.sleep() 
>to suspend between ticks. .Timer() is optimized for a one-time callback. Note 
>that using _nextInterval allows me perform an adjustment for the time taken by 
>the callback function.<

Oh, very nice, you are very gentle, thank you. I'll add few suggestion
by Roggisch, and I think it will be quite good. If I obtain something
good I'll even think about putting it in the cookbook (with your names
in it).

---

Diez B. Roggisch:

>It certainly is somewhat ugly - but there might be very good reasons to do so.<

Okay.


>Instead of several metronomes, use one thread and compute the gresatest common 
>divisor for all scheduled timings. Use that as sleep interval (your "ticks"), 
>and count ticks for each timer. You should introduce a lower thrshold on 
>tick-length, otherwise you can get into trouble.<

Okay, I'll follow your suggestions.

Thank you all and hugs to Dennis Lee Bieber,
bearophile

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


Re: Replacing large number of substrings

2005-09-04 Thread tiissa
Will McGugan wrote:
> Hi,
> 
> Is there a simple way of replacing a large number of substrings in a 
> string? I was hoping that str.replace could take a dictionary and use it 
> to replace the occurrences of the keys with the dict values, but that 
> doesnt seem to be the case.

You can look at the re.sub [1] and try:

d={'a':'x', 'b':'y'}

def repl(match):
return d.get(match.group(0), '')

print re.sub("(a|b)", repl, "a b c")



>  >>> dict_replace( "a b c", dict(a="x", b="y") )
> "x y c"

Above, I gave the pattern myself but you can try to have it generated 
from the keys:


def dict_replace(s, d):
 pattern = '(%s)'%'|'.join(d.keys())
 def repl(match):
 return d.get(match.group(0), '')
 return re.sub(pattern, repl, s)


On your example, I get:

 >>> dict_replace('a b c', {'a': 'x', 'b': 'y'})
'x y c'
 >>>



[1] http://python.org/doc/2.4.1/lib/node114.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Replacing large number of substrings

2005-09-04 Thread Robert Kern
Will McGugan wrote:
> Hi,
> 
> Is there a simple way of replacing a large number of substrings in a 
> string? I was hoping that str.replace could take a dictionary and use it 
> to replace the occurrences of the keys with the dict values, but that 
> doesnt seem to be the case.
> 
> To clarify, something along these lines..
> 
>  >>> dict_replace( "a b c", dict(a="x", b="y") )
> "x y c"

(n.b. untested!)

def dict_replace(string, replacements):
for key, value in replacements.iteritems():
string = string.replace(key, value)
return string

How well this works depends on how large is "large." If "large" is
really very large, then you might want to build something using a more
suitable algorithm like the Aho-Corasick algorithm.

http://www.lehuen.com/nicolas/download/pytst/
http://hkn.eecs.berkeley.edu/~dyoo/python/ahocorasick/

-- 
Robert Kern
[EMAIL PROTECTED]

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter

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


Re: Problems with Python for Windows extensions

2005-09-04 Thread KK
thx for ur reply
u r rite that i should use a raw string, but that doesn't solve the
problem
i am q annoyed by this strange behaviour.
i tried to run the script on my friend's pc, which is python2.4 + pywin
204 + office 2000, but same thing happened

now i am thinking to generate a vbs from python and run it. i know it
is dumb but i dont know other solutions...

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


regular expression unicode character class trouble

2005-09-04 Thread Diez B. Roggisch
Hi,

I need in a unicode-environment the character-class

set("\w") - set("[0-9]")

or aplha w/o num. Any ideas how to create that? And what performance 
implications do I have to fear? I mean I guess that the characterclasses 
aren't implementet as sets, but as comparison-function that compares a 
value with certain well-defined ranges.

Regards,

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


Re: os.system(r"ls") prints to screen??

2005-09-04 Thread Xah Lee
do you know what the Muses do when a mortal challenged them?

And, please tell me exactly what capacity you hold under the official
Python organization so that i can calculate to what degree i can kiss
your ass or feign mum of your ignorance.

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

Steve Holden wrote:

> Seems to me that you'd know more about frenzy than me. Having read many
> of your posts it seems to me that they are deliberately calculated to
> inflame the reader.
>
> To be successfully so critical of others you need to demonstrate
> superior knowledge and judgment, which your original question revealed
> to be lacking.
>
> To put it another way: "People who live in glass houses shouldn't throw
> stones".

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

String from File -> List without parsing

2005-09-04 Thread Gregor Horvath
Hi,

given the dynamic nature of python I assume that there is an elegant 
solution for my problem, but I did not manage to find it.

I have a file that contains for example on line:

['147', '148', '146']

when I read the file

  f = file("I050901.ids").readlines()

I have a string

f[0] == "['147', '148', '146']"

How can I turn this string into a list

li == ['147', '148', '146']

without parsing?

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


Re: Binary Trees in Python

2005-09-04 Thread Jorgen Grahn
On Sun, 21 Aug 2005 08:52:10 -0400, François Pinard <[EMAIL PROTECTED]> wrote:
> [Jorgen Grahn]
>
>> Neither C++ nor Python has tree structures in their standard
>> libraries.  I assume that's because there is no single interface that
>> is proven to suit everybody's needs.
>
> It is already easy writing "tree constants" using recursive tuples or
> lists.  To process simple trees in Python, I usually subclass some
> Node type from list, and write the traversal methods that suit the
> application.  The sub-classing already allow for indexing sub-nodes by
> "self[index]", and iterating over all by "for subnode in self:", etc.
> In my experience, it all goes pretty easily, while staying simple.
>
> However, things related to balancing, finding paths between nodes, or
> searching for patterns, etc. may require more work.  There are surely
> a flurry of tree algorithms out there.  What are the actual needs you
> have, and would want to see covered by a library?

I have no needs, actually ... but yes, the things you mention (balancing,
traversal ...) were the ones I was thinking about.

/Jorgen

-- 
  // Jorgen GrahnR'lyeh wgah'nagl fhtagn!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problems with Python for Windows extensions

2005-09-04 Thread Kartic
Hi,

Invoking Execute as shown below works.. I have no explanation why your 
VB to Py converted code did not work.

wdFindContinue = 1
objSelection.Find.Execute('Contosa', False, True, False, False, True, 
True, wdFindContinue, True, 'Fabrikam', wdReplaceAll, False, False, 
False, False)

This finds 'Contosa' and replaces all occurances with 'Fabricam'.

For a full explanation about the arguments to Execute, look it up in the 
VBA Help.

Thanks,
-Kartic


The Great 'KK' uttered these words on 9/3/2005 11:14 AM:
> the code below is taken from M$ technet as an example on using vb
> script to do a replace all in word:
> 
> Const wdReplaceAll  = 2
> 
> Set objWord = CreateObject("Word.Application")
> objWord.Visible = True
> 
> Set objDoc =
> objWord.Documents.Open("K:\Development\Fabricbase\prod\Test.doc")
> Set objSelection = objWord.Selection
> 
> objSelection.Find.Text = "Contoso"
> objSelection.Find.Forward = True
> objSelection.Find.MatchWholeWord = True
> 
> objSelection.Find.Replacement.Text = "Fabrikam"
> objSelection.Find.Execute ,,wdReplaceAll
> 
> 
> 
> 
> I did a rewrite and made it pythonic:
> 
> from win32com.client import *
> 
> wdReplaceAll  = 2
> 
> objWord = Dispatch("Word.Application")
> objWord.Visible = True
> 
> objDoc =
> objWord.Documents.Open("K:\Development\Fabricbase\prod\Test.doc")
> objSelection = objWord.Selection
> 
> objSelection.Find.Text = "Contoso"
> objSelection.Find.Forward = True
> objSelection.Find.MatchWholeWord = True
> 
> objSelection.Find.Replacement.Text = "Fabrikam"
> objSelection.Find.Execute (Replace = wdReplaceAll)
> 
> 
> However, the document juz loaded up in word but no action was taken. I
> am using Word 2003. Any ideas?
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Managing events

2005-09-04 Thread cantabile
Thanks to you all for these answers. I'll try these ideas and post back 
comments and results.
-- 
http://mail.python.org/mailman/listinfo/python-list


dual processor

2005-09-04 Thread John Brawley
Greetings, all.
I have a program I'm trying to speed up by putting it on a new machine.
The new machine is a Compaq W6000 2.0 GHz workstation with dual XEON
processors.
I've gained about 7x speed over my old machine, which was a 300 MHz AMD
K6II, but I think there ought to be an even greater speed gain due to the
two XEONs.
However, the thought occurs that Python (2.4.1) may not have the ability to
take advantage of the dual processors, so my question:
Does it?
If not, who knows where there might be info from people trying to make
Python run 64-bit, on multiple processors?
Thanks!

John Brawley


--
peace
JB
[EMAIL PROTECTED]
http://tetrahedraverse.com
NOTE! Charter is not blocking viruses,
Therefore NO ATTACHMENTS, please;
They will not be downloaded from the Charter mail server.
__Prearrange__ any attachments, with me first.


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


Re: dual processor

2005-09-04 Thread Paul Rubin
"John Brawley" <[EMAIL PROTECTED]> writes:
> However, the thought occurs that Python (2.4.1) may not have the ability to
> take advantage of the dual processors, so my question:
> Does it?

No.

> If not, who knows where there might be info from people trying to make
> Python run 64-bit, on multiple processors?
> Thanks!

Nobody is trying it in any serious way.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String from File -> List without parsing

2005-09-04 Thread Jean-François Doyon
Gregor,

You want to use eval():

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> eval('[3,54,5]')
[3, 54, 5]
 >>>

Cheers,
J.F.

Gregor Horvath wrote:
> Hi,
> 
> given the dynamic nature of python I assume that there is an elegant 
> solution for my problem, but I did not manage to find it.
> 
> I have a file that contains for example on line:
> 
> ['147', '148', '146']
> 
> when I read the file
> 
>  f = file("I050901.ids").readlines()
> 
> I have a string
> 
> f[0] == "['147', '148', '146']"
> 
> How can I turn this string into a list
> 
> li == ['147', '148', '146']
> 
> without parsing?
> 
> -- 
> Greg
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: documentation error

2005-09-04 Thread Reinhold Birkenfeld
tiissa wrote:
> bill wrote:
>>>From 3.2 in the Reference Manual "The Standard Type Hierarchy":
>> 
>> "Integers
>> These represent elements from the mathematical set of whole
>> numbers."
>> 
>> The generally recognized definition of a 'whole number' is zero and the
>> positive integers.
> 
> This term is ambiguous as it seems to be used for both natural numbers 
> and signed numbers [1].

I cleared this up; now it reads "... set of whole numbers (positive and negative
ones)."

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


Re: regular expression unicode character class trouble

2005-09-04 Thread Steven Bethard
Diez B. Roggisch wrote:
> Hi,
> 
> I need in a unicode-environment the character-class
> 
> set("\w") - set("[0-9]")
> 
> or aplha w/o num. Any ideas how to create that?

I'd use something like r"[^_\d\W]", that is, all things that are neither 
underscores, digits or non-alphas.  In action:

py> re.findall(r'[^_\d\W]+', '42badger100x__xxA1BC')
['badger', 'x', 'xxA', 'BC']

HTH,

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


Re: Considering moving from PowerBuilder to Python

2005-09-04 Thread Norm Goertzen
Wolfgang Keller wrote:

 >> But I also really would like to find something very close to Sybase's
 >> patented datawindow  technology -- it's a real time-saver.
 >>
 >> BTW: As most readers (including me) probably don't know Powerbuilder:
 >> What is a Datawindow? Some kind of data-aware GUI widget?
 >>
 >> Sincerely,
 >>
 >> Wolfgang Keller

Exceprted from the PowerBuilder Journal...

The DataWindow is nothing more than an array of structures with many 
powerful and fast functions wrapped around it. True, one of the things 
it can do is generate SQL statements based on the data in this structure 
array, but this is by no means its only use. Many PowerBuilder 
developers think of using DataWindows or DataStores only when database 
access is involved. As a result they often create extra, unnecessary 
work. For example, I've often been astonished to see structure arrays 
declared and manipulated manually in PowerBuilder applications. What's 
the point of doing this when you can load your data into a DataStore and 
work with it using fast, precoded DataStore functions? Why loop through 
a structure array to find a particular value when you can use the Find() 
function? DataStores are structure arrays on steroids.

... end of excerpt

I really wish I could find a crystal ball and determine (once and for 
all) the best new language to use.  C#.NET is proving to be a much more 
complex system than I think it needs to be.  In my mind, the best 
language would be one that takes care of EVERYTHING that can safely 
assumed -- I should never have to write another low-level routine. 
Although the comparison is far from perfect, I remember being able to 
roll-out a small database application using R:Base in about 60 minutes 
(including 3 data entry forms and 3 reports).  Nowadays, if I use 
PowerBuilder, it takes me at least 4 hours.  In C#.NET, I'm afraid it 
will take at least 10 hours.

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


How to integrate an array?

2005-09-04 Thread Tom Kaz
I need to compute integral of some array function, something
like:

from scipy import *
f = lambda x: array( [sin(x),cos(x)] )
integrate.quad(f, 0, 1)


Unfortunately integrate.quad requires Float type.

Any ideas how to perform this?



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


is dict.copy() a deep copy or a shallow copy

2005-09-04 Thread Alex
Entering the following in the Python shell yields

>>> help(dict.copy)
Help on method_descriptor:

copy(...)
D.copy() -> a shallow copy of D

>>>

Ok, I thought a dictionary copy is a shallow copy. Not knowing exactly
what that meant I went to http://en.wikipedia.org/wiki/Deep_copy where
I could read

"...a deep copy is copy that contains the complete encapsulated data of
the original object, allowing it to be used independently of the
original object. In contrast, a shallow copy is a copy that may be
associated to data shared by the original and the copy"

Going back to Python shell I tested the following

>>> D={'Python': 'good', 'Basic': 'simple'}
>>> E=D.copy()
>>> E
{'Python': 'good', 'Basic': 'simple'}
>>> D['Basic']='oh my'
>>> D
{'Python': 'good', 'Basic': 'oh my'}
>>> E
{'Python': 'good', 'Basic': 'simple'}
>>>

Hmm, this looks like a deep copy to me?? I also tried

>>> D={'Python': 'good', 'Basic': 'simple'}
>>> E=D
>>> E
{'Python': 'good', 'Basic': 'simple'}
>>> E['Basic']='oh my'
>>> E
{'Python': 'good', 'Basic': 'oh my'}
>>> D
{'Python': 'good', 'Basic': 'oh my'}
>>>

which looks like a shallow copy to me?? So my hypothesis is that E=D is
a shallow copy while E=D.copy() is a deep copy.

So is the documentation wrong when they claim that D.copy() returns a
shallow copy of D, or did I misunderstand the difference between a deep
and shallow copy?

Thanks

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


Assigning 'nochage' to a variable.

2005-09-04 Thread [EMAIL PROTECTED]
Has anyone else felt a desire for a 'nochange' value
resembling the 'Z'-state of a electronic tri-state output?

 var1 = 17
 var1 = func1()   # func1() returns 'nochange' this time
 print var1   # prints 17

It would be equivalent to:

 var1 = 17
 var2, bool1 = func1()
 if bool1:
   var1 = var2
 print var1   # prints 17  

BR/ CJF

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


Re: dual processor

2005-09-04 Thread Jeremy Jones
John Brawley wrote:

>Greetings, all.
>I have a program I'm trying to speed up by putting it on a new machine.
>The new machine is a Compaq W6000 2.0 GHz workstation with dual XEON
>processors.
>I've gained about 7x speed over my old machine, which was a 300 MHz AMD
>K6II, but I think there ought to be an even greater speed gain due to the
>two XEONs.
>However, the thought occurs that Python (2.4.1) may not have the ability to
>take advantage of the dual processors, so my question:
>Does it?
>  
>
Sure, but you have to write the program to do it.  One Python process 
will only saturate one CPU (at a time) because of the GIL (global 
interpreter lock).  If you can break up your problem into smaller 
pieces, you can do something like start multiple processes to crunch the 
data and use shared memory (which I haven't tinkered with...yet) to pass 
data around between processes.  Or an idea I've been tinkering with 
lately is to use a BSD DB between processes as a queue just like 
Queue.Queue in the standard library does between threads.  Or you could 
use Pyro between processes.  Or CORBA. 

>If not, who knows where there might be info from people trying to make
>Python run 64-bit, on multiple processors?
>Thanks!
>
>John Brawley
>
>
>--
>peace
>JB
>[EMAIL PROTECTED]
>http://tetrahedraverse.com
>NOTE! Charter is not blocking viruses,
>Therefore NO ATTACHMENTS, please;
>They will not be downloaded from the Charter mail server.
>__Prearrange__ any attachments, with me first.
>
>
>  
>
HTH,

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


Re: documentation error

2005-09-04 Thread Bryan Olson
Reinhold Birkenfeld wrote:
 > tiissa wrote:
 >
 >>bill wrote:
 >>
 From 3.2 in the Reference Manual "The Standard Type Hierarchy":
 >>>
 >>>"Integers
 >>>These represent elements from the mathematical set of whole
 >>>numbers."
 >>>
 >>>The generally recognized definition of a 'whole number' is zero and the
 >>>positive integers.
 >>
 >>This term is ambiguous as it seems to be used for both natural numbers
 >>and signed numbers [1].
 >
 >
 > I cleared this up; now it reads "... set of whole numbers (positive 
and negative
 > ones)."

Consider deleting the sentence in which the Python doc tries to
define mathematical integers.

 Integers

 [In Python] There are three types of integers:


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


Re: How to integrate an array?

2005-09-04 Thread Robert Kern
Tom Kaz wrote:
> I need to compute integral of some array function, something
> like:
> 
> from scipy import *
> f = lambda x: array( [sin(x),cos(x)] )
> integrate.quad(f, 0, 1)
> 
> Unfortunately integrate.quad requires Float type.
> 
> Any ideas how to perform this?

Do each function separately. The routine that scipy.integrate.quad uses
adapts to the local conditions of the function (when the function is
flat, it uses fewer samples; when steep, more).

-- 
Robert Kern
[EMAIL PROTECTED]

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter

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


Re: dual processor

2005-09-04 Thread Paul Rubin
Jeremy Jones <[EMAIL PROTECTED]> writes:
> to pass data around between processes.  Or an idea I've been tinkering
> with lately is to use a BSD DB between processes as a queue just like
> Queue.Queue in the standard library does between threads.  Or you
> could use Pyro between processes.  Or CORBA.

I think that doesn't count as using a the multiple processors; it's
just multiple programs that could be on separate boxes.
Multiprocessing means shared memory.

This module might be of interest:  http://poshmodule.sf.net

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


Re: dual processor

2005-09-04 Thread Jeremy Jones
Paul Rubin wrote:

>Jeremy Jones <[EMAIL PROTECTED]> writes:
>  
>
>>to pass data around between processes.  Or an idea I've been tinkering
>>with lately is to use a BSD DB between processes as a queue just like
>>Queue.Queue in the standard library does between threads.  Or you
>>could use Pyro between processes.  Or CORBA.
>>
>>
>
>I think that doesn't count as using a the multiple processors; it's
>just multiple programs that could be on separate boxes.
>Multiprocessing means shared memory.
>  
>
I disagree.  My (very general) recommendation implies multiple 
processes, very likely multiple instances (on the consumer side) of the 
same "program".  The OP wanted to know how to get Python to "take 
advantage of the dual processors."  My recommendation does that.  Not in 
the sense of a single process fully exercising multiple CPUs, but it's 
an option, nonetheless.  So, in that respect, your initial "no" was 
correct.  But,

>This module might be of interest:  http://poshmodule.sf.net
>
>  
>
Yeah - that came to mind.  Never used it.  I need to take a peek at 
that.  This module keeps popping up in discussions like this one.

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


Re: Assigning 'nochage' to a variable.

2005-09-04 Thread Diez B. Roggisch
[EMAIL PROTECTED] wrote:
> Has anyone else felt a desire for a 'nochange' value
> resembling the 'Z'-state of a electronic tri-state output?

No. but if you must, you can emulate that using properties or 
__getattr__/__setattr__- of course then limited to classes, but writen 
c.var1 instead of var1 where c is your container - that world work, if 
the semantics you need are so commeon it really saves you effort.

class Container(object):

 def __init__(self, nc):
 """
 @param nc: a list or set of names that should not be
 overwritten when None is assigned to them.
 """
 self.nochanges = nc

 def __setattr__(self, name, value):
 if name in self.nochanges and value is not None:
self.__dict__[name] = value

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


Re: how to do this?

2005-09-04 Thread Justin Straube
Thanks Steve and Peter,

these two methods are a lot of help. Ill have to play with each in my actual 
app 
  to find which works best.

I found more info on trace_variable() at 
http://effbot.org/tkinterbook/variable.htm

and also some more info on events and bindings at
http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

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


Re: documentation error

2005-09-04 Thread Bengt Richter
On Sun, 04 Sep 2005 20:02:10 GMT, Bryan Olson <[EMAIL PROTECTED]> wrote:

>Reinhold Birkenfeld wrote:
> > tiissa wrote:
> >
> >>bill wrote:
> >>
> From 3.2 in the Reference Manual "The Standard Type Hierarchy":
> >>>
> >>>"Integers
> >>>These represent elements from the mathematical set of whole
> >>>numbers."
> >>>
> >>>The generally recognized definition of a 'whole number' is zero and the
> >>>positive integers.
> >>
> >>This term is ambiguous as it seems to be used for both natural numbers
> >>and signed numbers [1].
> >
> >
> > I cleared this up; now it reads "... set of whole numbers (positive 
>and negative
> > ones)."
>
>Consider deleting the sentence in which the Python doc tries to
>define mathematical integers.
>
> Integers
>
> [In Python] There are three types of integers:
>
This is a nice site:

http://mathworld.wolfram.com/WholeNumber.html
http://mathworld.wolfram.com/topics/Integers.html

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


Possible improvement to slice opperations.

2005-09-04 Thread Ron Adam

After considering several alternatives and trying out a few ideas with a 
  modified list object Bengt Richter posted, (Thank You), I think I've 
found a way to make slice operation (especially far end indexing) 
symmetrical and more consistent.

So to find out if this is indeed a possibility, it would be nice to get 
a few opinions at this point.  So blast away... or hopefully tell me 
what you like about it instead. ;-)

(Any suggestions or contributions to make it better would be appreciated.)

Cheers,
Ron Adam



"""
IMPROVED SLICING


Slicing is one of the best features of Python in my opinion, but
when you try to use negative index's and or negative step increments
it can be tricky and lead to unexpected results.

This topic has come up fairly often on comp.lang.python, and often 
times, the responses include:

 * Beginners should avoid negative extended slices.

 * Slices with negative step increments are for advanced
   python programmers.

 * It's not broke if you look at it in a different way.

 * You should do it a different way.

All of these and various responses similar to them are unsatisfactory in
my opinion and it's a good indicator it's not as good as it could be.
Indexing and slice operations are vary common in Python and I don't
think we should be discouraging people from learning and using them.


COMPATIBILITY
-
Because the following suggested changes will break current code,
it probably can not be implemented prior to Python 3000.

 + Direct indexing with both positive and negative values
   returns the same items as they do now.

 + Extended slices with all positive and or empty default
   values remain unchanged.

 - Extended slices with negative values return values that
   have less items than currently.

 - Slices with negative step values return entirely different
   results.


REVERSE ORDER STEPPING
--
When negative steps are used, a slice operation
does the following.  (or the equivalent)

1. reverse the list
2. cut the reversed sequence using start and stop
3. iterate forward using the absolute value of step.

* This order results in an inverse selection and I believe should be
considered a bug.

Changing the order in the following way results in a much
more predictable pattern which is both easier to understand and use.

1. cut sequence using start and stop.
2  reverse the order of the results.
3. iterate forward using the absolute value of step.


CURRENT INDEXING


Given a range [a,b,c]:

   Positive indexing

   | a | b | c |
   +---+---+---+
   0   1   2   3

   Current negative indexing.

   | a | b | c |
   +---+---+---+
  -3  -2  -1  -0


When a single index is used the item to the
right of the index for both positive and
negative index's is returned.

With slices, the items between start, and
stop index's are returned.

Accessing a range at the end of a list numerically
becomes inconvenient when negative index's are used
as the '-0'th position can not be specified numerically
with negative values.


ONES BASED NEGATIVE INDEXING

Making negative index's Ones based, and selecting
individual item to the left of negative index's would enable
addressing the end of the list numerically.

   Ones based negative index's.

   | a | b | c |
   +---+---+---+
  -4  -3  -2  -1

Then:

a[-1]-> c  # item left of index, same result as now.

a[-3:-2] -> b  # item between index's

a[-1:-1] = [d]  # insert item at the end.



USE OF '~' IN PLACE OF NEGATIVE INDEX'S
---

The '~' is the binary not symbol which when used
with integers returns the two's compliment. This
works nice with indexing from the end of a list
because convieniently ~0 == -1.

This creates a numerical symmetry between positive
indexing and '~' "End of list" indexing.

a[0]  -> first item in the list.
a[~0] -> last item in the list.

a[0:~0] -> whole list.

a[1:~1] -> center, one position from both ends.

* Note: using '~' works as described here in place of single negative 
index's in current versions of Python.  It does not work as described 
here for extended slices.

"""



# TEST LIST CLASS.
"""
A list class to Test '~' end of list indexing.

* This class modifies the slice before returning
a value. The final implementation may do this by
modifying slice objects directly or the underlying
C code of sequences.

"""

class nxlist(object):

 def __init__(self, value):
 self.value = value

 def normslc(self, slc):
 start,stop,step = slc.start, slc.stop, slc.step
 if type(start) == int and start<0:
 start = len(self.value)+start+1
 if type(stop) == int and stop<0:
 stop = len(self.value)+stop+1
 return slice(start,stop,step)

 def __getitem__(self, i):
 tp = i.__class__
 if 

Re: How to integrate an array?

2005-09-04 Thread Tom Kaz

> Do each function separately. The routine that scipy.integrate.quad uses
> adapts to the local conditions of the function (when the function is
> flat, it uses fewer samples; when steep, more).

It's not so easy  to do it separately. I want to integrate function that
includes  linalg.expm - as you see calculation the function is quite
expensive. 

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


Re: is dict.copy() a deep copy or a shallow copy

2005-09-04 Thread Max Erickson
"Alex" <[EMAIL PROTECTED]> wrote in
news:[EMAIL PROTECTED]: 
 D={'Python': 'good', 'Basic': 'simple'}
 E=D.copy()
 E
> {'Python': 'good', 'Basic': 'simple'}
 D['Basic']='oh my'
 D
> {'Python': 'good', 'Basic': 'oh my'}
 E
> {'Python': 'good', 'Basic': 'simple'}

> 
> Hmm, this looks like a deep copy to me?? I also tried
> 

It is shallow, but strings are immutable so the difference is fairly 
moot.

 D={'Python': 'good', 'Basic': 'simple'}
 E=D
 E
> {'Python': 'good', 'Basic': 'simple'}
 E['Basic']='oh my'
 E
> {'Python': 'good', 'Basic': 'oh my'}
 D
> {'Python': 'good', 'Basic': 'oh my'}

> 

Here, E and D are different names for the same object. There is no 
copy.

> which looks like a shallow copy to me?? So my hypothesis is that
> E=D is a shallow copy while E=D.copy() is a deep copy.
> 

> So is the documentation wrong when they claim that D.copy()
> returns a shallow copy of D, or did I misunderstand the
> difference between a deep and shallow copy?

Sort of, some examples:

Here d1 and d2 are copies. They can be independently changed to 
refer to different objects:

>>> d1={'a':1,'b':2}
>>> d2=d1.copy()
>>> d1['a']=3
>>> d2['b']=4
>>> d1
{'a': 3, 'b': 2}
>>> d2
{'a': 1, 'b': 4}

Again, d3 and d4 are copies, but instead of changing the objects 
they refer to, we change the contents of the objects they refer to:

>>> d3={'c':[3],'d':[4]}
>>> d4=d3.copy()
>>> d3['c'][0]=5
>>> d4['d'][0]=6
>>> d3
{'c': [5], 'd': [6]}
>>> d4
{'c': [5], 'd': [6]}

Both cases are shallow copies. In a deep copy, altering the contents 
of d3['c'] would have no impact on the contents of d4['c'].

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


Re: How to integrate an array?

2005-09-04 Thread Robert Kern
Tom Kaz wrote:

[I wrote:] (please attribute quotes)
>>Do each function separately. The routine that scipy.integrate.quad uses
>>adapts to the local conditions of the function (when the function is
>>flat, it uses fewer samples; when steep, more).
> 
> It's not so easy  to do it separately. I want to integrate function that
> includes  linalg.expm - as you see calculation the function is quite
> expensive. 

I understand, but the good integration algorithms really do need to
evaluate each dimension separately.

If you can bear algorithms that only use samples from fixed intervals,
then you can use the functions romb(), simps(), or trapz() in
scipy.integrate.

BTW, a better place to ask scipy questions would be the scipy mailing list.

http://scipy.net/mailman/listinfo/scipy-user

-- 
Robert Kern
[EMAIL PROTECTED]

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter

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


Controlling memory allocation

2005-09-04 Thread cwoolf
Let's say I have an OS that can't limit the amount of memory a process
is allowed to consume, but I want to run some Python code in a
simulated low memory environment...

Is there a way to tell Python to limit the size of it's heap?  Can you
ask it to use a function other than malloc through the C API?  Any
stunts you might recommend to achieve the same results?

Chad

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


GETTING IMAGEDATA FROM A BUTTON

2005-09-04 Thread pythonprogrammer
Does anyone know how to get pixeldata from the image of a button
without taking a screenshot?

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


GETTING IMAGEDATA FROM A BUTTON

2005-09-04 Thread pythonprogrammer
Does anyone know how to get pixeldata from the image of a button
without taking a screenshot?

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


Re: String from File -> List without parsing

2005-09-04 Thread John Machin
Gregor Horvath wrote:
> Hi,
> 
> given the dynamic nature of python I assume that there is an elegant 
> solution for my problem, but I did not manage to find it.
> 
> I have a file that contains for example on line:
> ['147', '148', '146']
> when I read the file
>  f = file("I050901.ids").readlines()

Y3K bug alert :-)

> I have a string
> f[0] == "['147', '148', '146']"

The last (or sole) line is highly likely to be "['147', '148', 
'146']\n". If there are multiple lines in the file, all but the last 
will definitely have "\n" at the end.

> How can I turn this string into a list
> li == ['147', '148', '146']
> without parsing?

Something like this:

 >>> def munch(astrg):
...return [x[1:-1] for x in astrg.rstrip("\n")[1:-1].split(", ")]
...
 >>> munch("['147', '148', '146']")
['147', '148', '146']
 >>> munch("['147', '148', '146']\n")
['147', '148', '146']
 >>> munch("['147']\n")
['147']
 >>> # Warning: various flavours of "nothing" give the same result:
 >>> munch("['']\n")
['']
 >>> munch("[]\n")
['']
 >>> munch("\n")
['']
 >>>

This assumes that the contents are no more complicated than in your example.

Some notes:

(1) You neither want nor need to use eval.

(2) What is creating files like that? If it is a Python script, consider 
writing them using the csv module; that way, other software can read 
them easily.

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


Re: Replacing large number of substrings

2005-09-04 Thread Michael J. Fromberger
In article <[EMAIL PROTECTED]>,
 Will McGugan <[EMAIL PROTECTED]> wrote:

> Hi,
> 
> Is there a simple way of replacing a large number of substrings in a 
> string? I was hoping that str.replace could take a dictionary and use it 
> to replace the occurrences of the keys with the dict values, but that 
> doesnt seem to be the case.
> 
> To clarify, something along these lines..
> 
>  >>> dict_replace( "a b c", dict(a="x", b="y") )
> "x y c"

Hi, Will,

Perhaps the following solution might appeal to you:

. import re
. 
. def replace_many(s, r):
. """Replace substrings of s.  The parameter r is a dictionary in   
. which each key is a substring of s to be replaced and the
. corresponding value is the string to replace it with.
. """
. exp = re.compile('|'.join(re.escape(x) for x in r.keys()))
. return exp.sub(lambda m: r.get(m.group()), s)

Cheers,
-M

-- 
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: GETTING IMAGEDATA FROM A BUTTON

2005-09-04 Thread Delaney, Timothy (Tim)
pythonprogrammer wrote:

> Does anyone know how to get pixeldata from the image of a button
> without taking a screenshot?

Yes.

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

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


Re: Assigning 'nochage' to a variable.

2005-09-04 Thread Terry Reedy

"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Has anyone else felt a desire for a 'nochange' value

No.

> resembling the 'Z'-state of a electronic tri-state output?

Not familiar with that.

>
> var1 = 17
> var1 = func1()   # func1() returns 'nochange' this time
> print var1   # prints 17
>
> It would be equivalent to:
>
> var1 = 17
> var2, bool1 = func1()
> if bool1:
>   var1 = var2
> print var1   # prints 17

Simpler is

var1 = 17
var2 = func1()
if var2 is not 'nochange': var1 = var2

or
var1 = func2(var1)
with func2 conditionally returning the input instead of 'nochange'.
The default return, of course, could be something other than var1.

Conclusion: you can easily do what you want with Python as it is.

Terry J. Reedy



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


Python Doc Problem Example: os.system

2005-09-04 Thread Xah Lee
Python Doc Problem Example: os.system

Xah Lee, 2005-09

today i'm trying to use Python to call shell commands. e.g. in Perl
something like

output=qx(ls)

in Python i quickly located the the function due to its
well-named-ness:

import os
os.system("ls")


however, according to the doc
http://www.python.org/doc/2.4/lib/os-process.html the os.system()
returns some esoteric unix thing, not the command output. The doc
doesn't say how to get the output of the command.

by chance someone told me that in python 2.4 the os.system is
supplanted by subprocess.call(), but this isn't mentioned in the doc!

upon finding the new doc location
http://www.python.org/doc/2.4/lib/module-subprocess.html i'm told that
this module replaces:

os.system
os.spawn*
os.popen*
popen2.*
commands.*


interesting. Since i'm not Python expert, i like to look at these. But
fuck, the incompetent doc gives ample gratis links to OpenSource this
or that or author masturbation links to remote book i don't really care
about, but here there's no link.

Problem summary:

* does not focus on the task users need to do. Instead, the doc is
oriented towards tech geeking.

* does not inform the reader at the right place where a new function is
replacing the old.

* does not provide relevant cross-links. (while provding many
irrelevant links because of OpenSource or Tech Geeking fanaticism)

Solution Suggestion:

* Add examples.

* Add cross-links to relevant modules.

* Mention and add link at the right place supplanted functions.

* Orient the doc to tasks and manifest functionalities. Think like
functional programing: input and output specification, and document
them. This will help focus and precision in the doc. Avoid prose-like
descriptions. Avoid drilling on remotely related tech/unix/C esoterica.
e.g. Do not mention as a documentation how they are implemented.
Mention implementation on the side if necessary. This way, the language
becomes focused as a independent tool (e.g. Mathematica, Java, Scheme,
emacs) (which may provide ample capabilities to interface/connect to
other technologies), instead of heavily intermixed and dependent with a
bunch of other things (unix things: Perl, Apache, shells).

-
This article is archive at:
http://xahlee.org/UnixResource_dir/writ/python_doc_os.html

 Xah
 [EMAIL PROTECTED]
∑ http://xahlee.org/

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

Re: Possible improvement to slice opperations.

2005-09-04 Thread Terry Reedy

"Ron Adam" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Slicing is one of the best features of Python in my opinion, but
> when you try to use negative index's and or negative step increments
> it can be tricky and lead to unexpected results.
>
> This topic has come up fairly often on comp.lang.python, and often times, 
> the responses include:
> * Beginners should avoid negative extended slices.
> * Slices with negative step increments are for advanced
>   python programmers.
> * It's not broke if you look at it in a different way.
> * You should do it a different way.

You omitted the slice-specific response I gave before and repeat here with 
more detail by quoting from What's New in Python 2.3.
http://www.python.org/doc/2.3/whatsnew/section-slices.html
"
15 Extended Slices
Ever since Python 1.4, the slicing syntax has supported an optional third 
``step'' or ``stride'' argument. For example, these are all legal Python 
syntax: L[1:10:2], L[:-1:1], L[::-1]. This was added to Python at the 
request of the developers of Numerical Python, which uses the third 
argument extensively. However, Python's built-in list, tuple, and string 
sequence types have never supported this feature, raising a TypeError if 
you tried it.
"
Again, extended slices were probably designed by and certainly designed for 
Numerical Python and for 7 years were used at least mainly by Numerical 
Python.  They were not designed for other users like you.  The responses 
you summarized and distain pretty much all derive from this history.

So, I am pretty sure that changes to the core would have to be upwards 
compatible with current usage.  On the other hand, your nxlist subclass of 
list seems to work pretty well now.The 2.2+ ability to do this sort of 
thing is what type subclassing was made for.

Or one could just write an extended slice function similar to your .normslc 
method.  I might even use such a thing one day.

Terry J. Reedy


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


Re: Proposal: add sys to __builtins__

2005-09-04 Thread Terry Reedy

"Colin J. Williams" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Rick Wotnaz wrote:
>> +1 here. As far as I'm concerned, both os and sys could be special-
>> cased that way. That said, I would guess the likelihood of that
>> happening is 0.
>>
> +1 for both.

Some people might prefer that math be special cased.  Or some other module. 
A neutral criterion is needed.

Terry J. Reedy



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


Re: OpenSource documentation problems

2005-09-04 Thread Aahz
In article <[EMAIL PROTECTED]>,
Steve Holden  <[EMAIL PROTECTED]> wrote:
>
>Bear in mind that the PSF made its very first grants last year. The 
>reason none of those grants was awarded to a documentation project was 
>that the (volunteer) Grants Committee and helpers didn't see any 
>documentation projects worthy of support. 

Really?  And what exactly is "Software Engineering with Python for
Scientist and Engineers" if not a documentation project?  It may not be
the kind of documentation people are talking about in this thread, but it
certainly is documentation.
-- 
Aahz ([EMAIL PROTECTED])   <*> http://www.pythoncraft.com/

The way to build large Python applications is to componentize and
loosely-couple the hell out of everything.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Doc Problem Example: os.system

2005-09-04 Thread Jeremy Jones
Xah Lee wrote:

>Python Doc Problem Example: os.system
>
>Xah Lee, 2005-09
>
>today i'm trying to use Python to call shell commands. e.g. in Perl
>something like
>
>output=qx(ls)
>
>in Python i quickly located the the function due to its
>well-named-ness:
>
>import os
>os.system("ls")
>
>
>however, according to the doc
>http://www.python.org/doc/2.4/lib/os-process.html the os.system()
>returns some esoteric unix thing, not the command output. 
>
"""

*system*(   command)

Execute the command (a string) in a subshell. This is implemented by
calling the Standard C function system(), and has the same
limitations. Changes to |posix.environ|, |sys.stdin|, etc. are not
reflected in the environment of the executed command.

On Unix, the return value is the exit status of the process encoded
in the format specified for wait(). Note that POSIX does not specify
the meaning of the return value of the C system() function, so the
return value of the Python function is system-dependent.

On Windows, the return value is that returned by the system shell
after running command, given by the Windows environment variable
COMSPEC: on *command.com* systems (Windows 95, 98 and ME) this is
always |0|; on *cmd.exe* systems (Windows NT, 2000 and XP) this is
the exit status of the command run; on systems using a non-native
shell, consult your shell documentation.

Availability: Unix, Windows.

"""

Yup.  Nothing more esoteric than a process's exit status.  That's one of 
those really tricky jargons that computer scientist idiots like to throw 
around.  You've got to watch out for those.

>The doc
>doesn't say how to get the output of the command.
>
>by chance someone told me that in python 2.4 the os.system is
>supplanted by subprocess.call(), but this isn't mentioned in the doc!
>  
>
I'm presuming you mean in the os.system docs as you mention below that 
you found such documentation.

>upon finding the new doc location
>http://www.python.org/doc/2.4/lib/module-subprocess.html i'm told that
>this module replaces:
>
>os.system
>os.spawn*
>os.popen*
>popen2.*
>commands.*
>
>
>interesting.
>
"""


  6.8 subprocess -- Subprocess management

New in version 2.4.

The subprocess module allows you to spawn new processes, connect to 
their input/output/error pipes, and obtain their return codes. This 
module intends to replace several other, older modules and functions, 
such as:

os.system
os.spawn*
os.popen*
popen2.*
commands.*

"""
Yeah.  There's a really tricky word up there in the beginning of the 
subprocess doc.  "intends".  In this context, it means that it is 
currently the plan of the Python developers to replace said modules with 
the subprocess module, *however*, that has not totally come about now.  
If the doc had said, "This module *has replaced* several others", then I 
would have to agree with you that the stated module docs should be 
updated to reflect the fact that they have been deprecated.

> Since i'm not Python expert
>
Really?

>, i like to look at these. But
>fuck, the incompetent doc gives ample gratis links to OpenSource this
>or that or author masturbation
>
OK - I just scanned through the subprocess module docs and I really 
don't see where you're getting this from.  I'll just chalk the former up 
to your bad experience with the regular expression module docs referring 
to the book "Mastering Regular Expressions."  And since you're quite the 
linguistic scholar, I'll chalk up the latter to your unique construction 
of the book title I just cited.

> links to remote book i don't really care
>about, but here there's no link.
>
>Problem summary:
>
>* does not focus on the task users need to do. Instead, the doc is
>oriented towards tech geeking.
>  
>
Are you talking about the subprocess docs?  If so, I'd like to see an 
example of what you're talking about.  Subprocess docs seem really 
straightforward, terse, and to the point.

>* does not inform the reader at the right place where a new function is
>replacing the old.
>  
>
I would leave it in the hands of the Python doc maintainers what to do 
with this since subprocess hasn't yet totally replaced the other modules.

>* does not provide relevant cross-links. (while provding many
>irrelevant links because of OpenSource or Tech Geeking fanaticism)
>  
>
I'd really like to see what you're talking about here.  I just went 
through the subprocess docs *again* and I don't see *any* links to any 
other open source anything and I don't see any "tech geeking" to use 
your jargon.  I think you're full of crap.  And I think you don't have 
the balls to reply back to this message and show me what you're talking 
about.  You're just a little boy inside, making a call to a bowling 
alley and asking if they have 15 pound balls and hanging up laughing 
after they reply "yes" and you reply "Then how do you walk!!!"  Oh, 
you're so witty.

>Solution Suggestion:
>
>* Add examples.
>  
>
Yeah, the

"""


  6.8.3

learning python

2005-09-04 Thread placid
Hi all,

I was just wondering about good books that teach python (either with
programming or no programming experience at all) ? Or some online
tutorial?

Thanks

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


Re: learning python

2005-09-04 Thread Christopher Culver
"placid" <[EMAIL PROTECTED]> writes:
> I was just wondering about good books that teach python (either with
> programming or no programming experience at all) ? Or some online
> tutorial?

Did you even bother doing a web search? "Learn Python" or "Python
tutorial" would be enough.

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


Re: documentation error

2005-09-04 Thread Bryan Olson
Bengt Richter wrote:
 > Bryan Olson wrote:
 >>Consider deleting the sentence in which the Python doc tries to
 >>define mathematical integers.

 > This is a nice site:
 >
 > http://mathworld.wolfram.com/WholeNumber.html
 > http://mathworld.wolfram.com/topics/Integers.html

So maybe:

 Integers

 Pyton offers three types of
 http://mathworld.wolfram.com/topics/Integers.html";>integers:


Or use the Wikipedia ref.

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


Re: OpenSource documentation problems

2005-09-04 Thread Steve Holden
Aahz wrote:
> In article <[EMAIL PROTECTED]>,
> Steve Holden  <[EMAIL PROTECTED]> wrote:
> 
>>Bear in mind that the PSF made its very first grants last year. The 
>>reason none of those grants was awarded to a documentation project was 
>>that the (volunteer) Grants Committee and helpers didn't see any 
>>documentation projects worthy of support. 
> 
> 
> Really?  And what exactly is "Software Engineering with Python for
> Scientist and Engineers" if not a documentation project?  It may not be
> the kind of documentation people are talking about in this thread, but it
> certainly is documentation.

Sigh. Fine. Any more nits you'd like to pick?

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

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


Re: learning python

2005-09-04 Thread placid

Christopher Culver wrote:
> "placid" <[EMAIL PROTECTED]> writes:
> > I was just wondering about good books that teach python (either with
> > programming or no programming experience at all) ? Or some online
> > tutorial?
>
> Did you even bother doing a web search? "Learn Python" or "Python
> tutorial" would be enough.

yeah, see i didnt even think of that. 

thanks man

> 
> Christopher

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


Re: learning python

2005-09-04 Thread Benji York
placid wrote:
> Christopher Culver wrote:
>>"placid" <[EMAIL PROTECTED]> writes:
>>>I was just wondering about good books that teach python (either with
>>>programming or no programming experience at all) ? Or some online
>>>tutorial?
>>
>>Did you even bother doing a web search? "Learn Python" or "Python
>>tutorial" would be enough.
> 
> 
> yeah, see i didnt even think of that. 
> 
> thanks man

That was either a very gracious way to take a public correction, or an 
expertly executed bit of sarcasm, either way, placid, I applaud you.
--
Benji York
-- 
http://mail.python.org/mailman/listinfo/python-list


Magic Optimisation

2005-09-04 Thread simonwittber
Hello People.

I've have a very tight inner loop (in a game app, so every millisecond
counts) which I have optimised below:

def loop(self):
self_pool = self.pool
self_call_exit_funcs = self.call_exit_funcs
self_pool_popleft = self.pool.popleft
self_pool_append = self.pool.append
check = self.pool.__len__
while check() > 0:
task = self_pool_popleft()
try:
task.next()
except StopIteration:
self_call_exit_funcs(task)
return
self_pool_append(task)

This style of optimisation has shaved _seconds_ from my iteration
cycle, esp. when I have many registered tasks, so this style of
optimisation is very important to me.

However, it is very ugly. Does anyone have any tips on how I could get
this optimisation to occor magically, via a decorator perhaps?

Sw.

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


Re: Possible improvement to slice opperations.

2005-09-04 Thread Patrick Maupin
> After considering several alternatives and trying out a few ideas with a
>   modified list object Bengt Richter posted, (Thank You), I think I've
> found a way to make slice operation (especially far end indexing)
> symmetrical and more consistent.

I don't know that it makes it more consistent.  I could be persuaded,
but it would have to be by real-life examples with calculated slice
indices and stride.  I do this thing all the time, and find the current
rules simple and very consistent.  Occasionally, I might wish that
things were a little different, but there is always a workaround.  I
would have to see some real code examples, of sufficient scope to see
that there are fewer workarounds with this proposal than with the
current implementation.

FWIW, there is a reasonable workaround for the case where the indices
might be negative and you would like zero or greater to mean 'end of
list'.  If "x" is the index variable, you can use the expression (x<0
and x or None) for the index value in the slice.  If you find yourself
doing this often, you can write a little function for it -- def
EndIndex(x): return x<0 and x or None.

But in real code, I fear you might need a similar helper function for
similar issues with your change.  I just don't know what those are
without more thought.

Regards,
Pat

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


Re: Magic Optimisation

2005-09-04 Thread Paul McGuire
This isn't much prettier, but what if you extract the try-except
overhead out from the while loop?  You only expect the exception to
fire one time, at the end of the list.  You can also eliminate any
localization of variables for calls that are not called in the loop,
such as self_pool (which does not seem to be used at all), and
self_call_exit_funcs.

-- Paul

def loop(self):
self_pool_popleft = self.pool.popleft
self_pool_append = self.pool.append
check = self.pool.__len__
try:
while check() > 0:
task = self_pool_popleft()
task.next()
self_pool_append(task)
except StopIteration:
self.call_exit_funcs(task) 
return

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


Re: Magic Optimisation

2005-09-04 Thread Paul Rubin
[EMAIL PROTECTED] writes:
> However, it is very ugly. Does anyone have any tips on how I could get
> this optimisation to occor magically, via a decorator perhaps?

Have you tried psyco?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dual processor

2005-09-04 Thread William Park
John Brawley <[EMAIL PROTECTED]> wrote:
> Greetings, all.  I have a program I'm trying to speed up by putting it
> on a new machine.  The new machine is a Compaq W6000 2.0 GHz
> workstation with dual XEON processors.  I've gained about 7x speed
> over my old machine, which was a 300 MHz AMD K6II, but I think there
> ought to be an even greater speed gain due to the two XEONs.  However,
> the thought occurs that Python (2.4.1) may not have the ability to
> take advantage of the dual processors, so my question: Does it?  If
> not, who knows where there might be info from people trying to make
> Python run 64-bit, on multiple processors?  Thanks!

Break up your problem into 2 independent parts, and run 2 Python
processes.  Your kernel should be SMP kernel, though.

-- 
William Park <[EMAIL PROTECTED]>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
   http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
  http://freshmeat.net/projects/bashdiff/
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Magic Optimisation

2005-09-04 Thread Delaney, Timothy (Tim)
[EMAIL PROTECTED] wrote:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/277940

For an even bigger improvement (in general), look at psyco:
http://psyco.sourceforge.net/.

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


Re: Magic Optimisation

2005-09-04 Thread simonwittber
I guess it is hard to see what the code is doing without a complete
example.

The StopIteration is actually raised by task.next(), at which point
task is removed from the list of generators (self.pool). So the
StopIteration can be raised at any time.

The specific optimisation I am after, which will clean up the code a
lot, is a way to auto-magically create self_attribute local variables
from self.attribute instance variables.

Sw.

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


Re: Magic Optimisation

2005-09-04 Thread simonwittber
Yes. It slows down the loop when there are only a few iterators in the
pool, and speeds it up when there are > 2000.

My use case involves < 1000 iterators, so psyco is not much help. It
doesn't solve the magic creation of locals from instance vars either.

Sw.

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


mod_python: what's going on here?

2005-09-04 Thread Robert J. Hansen
I'm not entirely certain comp.lang.python is the proper newsgroup for
mod_python questions, but "comp.lang.python.web" doesn't seem to exist,
so... my apologies in advance if this is considered off-topic.

I'm attempting to get mod_python 3.1.4/python 2.4.1 working on Apache
2.0.54 running under OS X.  Apache was compiled from source with a
simple

/configure --enable-so --with-mpm=worker

... followed by the make/make install dance.  mod_python was almost as
simple:

./configure --with-apxs=/usr/local/apache2/bin/apxs \
  --with-python=/sw/bin/python2.4

... followed by the requisite dance.

At this point, all's well.  The following bits were added to
httpd.conf:

LoadModule python_module /usr/local/apache2/modules/mod_python.so

   AddHandler mod_python .py
   PythonHandler mptest
   PythonDebug on


... one apachectl restart later, Apache was running fine and serving
pages.  The version string at the bottom of some static pages listed
mod_python as being present, so I reckoned that meant it was all
installed all right.  However, any attempt to serve a mod_python
script, mptest.py, from the test subdirectory results in a 500 Internal
Server Error.  Nothing gets written to error_log, but access_log
confirms the 500 was sent.

Does anyone have any experience with mod_python on OS X/Apache
environments?  Can anyone shed some light on 500s that don't leave
traces in the error logs, or what precise incantation I need to make
mod_python start serving up scripts?

Also, if this is not appropriate for this group, does anyone know of a
Python group for which this is more appropriate?

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


Re: Magic Optimisation

2005-09-04 Thread Paul Rubin
[EMAIL PROTECTED] writes:
> My use case involves < 1000 iterators, so psyco is not much help. It
> doesn't solve the magic creation of locals from instance vars either.

How about using __slots__ to put those instance vars at fixed offsets
in the pool object (self then needs to be a new-style class instance).
That might or might not avoid the dict lookups.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Possible improvement to slice opperations.

2005-09-04 Thread Ron Adam
Terry Reedy wrote:
> "Ron Adam" <[EMAIL PROTECTED]> wrote in message 
> news:[EMAIL PROTECTED]
> 
>>Slicing is one of the best features of Python in my opinion, but
>>when you try to use negative index's and or negative step increments
>>it can be tricky and lead to unexpected results.
>>
>>This topic has come up fairly often on comp.lang.python, and often times, 
>>the responses include:
>>* Beginners should avoid negative extended slices.
>>* Slices with negative step increments are for advanced
>>  python programmers.
>>* It's not broke if you look at it in a different way.
>>* You should do it a different way.
> 
> 
> You omitted the slice-specific response I gave before and repeat here with 
> more detail by quoting from What's New in Python 2.3.
> http://www.python.org/doc/2.3/whatsnew/section-slices.html
> "
> 15 Extended Slices
> Ever since Python 1.4, the slicing syntax has supported an optional third 
> ``step'' or ``stride'' argument. For example, these are all legal Python 
> syntax: L[1:10:2], L[:-1:1], L[::-1]. This was added to Python at the 
> request of the developers of Numerical Python, which uses the third 
> argument extensively. However, Python's built-in list, tuple, and string 
> sequence types have never supported this feature, raising a TypeError if 
> you tried it.
> "

I didn't omit anything,  I previously gave examples of using various 
forms of slices with the intent of showing how some of those forms are 
not symmetrical and difficult to understand.  And then you explained 
more precisely how those slices were indexed.

It would be useful to do a side by side comparison of those cases and 
similar ones to this new method.  Then maybe the benefits of making the 
changes I'm suggesting will be clearer.

I looked at the documents in Numerical Python.  From what I could tell, 
it uses the form L[::-1] quite a bit, which will still work *exactly* as 
it does now.  There were no examples of partial negative step 
operations, so I couldn't determine if/how they are used.  I'll install 
it and experiment with it and see if/how it works with Numerical Python.

After Python 3000 is released Numerical Python will need to be updated 
even without these changes.  But it is a good idea to consult with the 
Numerical Python developers on this.


> Again, extended slices were probably designed by and certainly designed for 
> Numerical Python and for 7 years were used at least mainly by Numerical 
> Python.  They were not designed for other users like you.  The responses 
> you summarized and distain pretty much all derive from this history.

Other users like me?  And what kind of user am I?

Is it really a good idea to maintain that some things shouldn't be done 
by some, but is ok for others?

I think you mean to say that extended slices with negative strides, 
should only (or primarily) be used with Numerical Python.  I however 
disagree with that view. I think they have a broader range of uses.


> So, I am pretty sure that changes to the core would have to be upwards 
> compatible with current usage.  ...

This is why I said these changes probably couldn't be made before Python 
  3000.  It is upward (backward?) compatible for the majority of use 
cases, all direct indexing operations, and all positive extended slices. 
  In Python's library, only negative extended slices will need to be 
adjusted.  As far as I could tell there are no partial negative strides 
cases that will need to be adjusted.  I wish there was a few, because 
then I could show how this would improve those cases.

Now even though this suggestion may be sound, that alone isn't reason 
enough to include it in Python 3000.  It would need clear use case 
benefits and support by users as well as support from Guido.

At this point, I'm only trying to clarify the issues and develop working 
examples.

> ... On the other hand, your nxlist subclass of list seems to work
> pretty well now. The 2.2+ ability to do this sort of thing is what
> type subclassing was made for.

Yes, it does work well, (so far)  :-)  Which is the motivation for me 
posting it.  Lets make it better.


> Or one could just write an extended slice function similar to your .normslc 
> method.  I might even use such a thing one day.

Unfortunately one can not sub-class slice objects directly.  And the ':' 
in a slice index isn't a true operator with it's own methods.  So 
changing existing slice behavior will require changing the core.  But 
for now I will write a function and a complete list sub-class and post 
it here.

Cheers,
Ron



> Terry J. Reedy
> 
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String from File -> List without parsing

2005-09-04 Thread Gregor Horvath
John Machin wrote:

>>  f = file("I050901.ids").readlines()
>  
> Y3K bug alert :-)

but then there is Python 3000 and Hurd, which solves all problems of 
this universe :-)

> Something like this:
> 
>  >>> def munch(astrg):
> ...return [x[1:-1] for x in astrg.rstrip("\n")[1:-1].split(", ")]

Thanks!

> (1) You neither want nor need to use eval.

I wanted to avoid parsing, but when parsing is an 1 liner, I think its 
the better solution.

> 
> (2) What is creating files like that? If it is a Python script, consider 
> writing them using the csv module; that way, other software can read 
> them easily.
> 

Its a Python script.
But the file is not intended to be used by another sotware, although it 
should be readable by humans. I think the python syntax is better human 
readable than csv.

After all the sense of this file is to make a list persistant quickly 
and easily and human readable. Maybe pickleing is a third option?

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


Re: Proposal: add sys to __builtins__

2005-09-04 Thread Rick Wotnaz
"Terry Reedy" <[EMAIL PROTECTED]> wrote in
news:[EMAIL PROTECTED]: 

> 
> "Colin J. Williams" <[EMAIL PROTECTED]> wrote in message 
> news:[EMAIL PROTECTED]
>> Rick Wotnaz wrote:
>>> +1 here. As far as I'm concerned, both os and sys could be
>>> special- cased that way. That said, I would guess the
>>> likelihood of that happening is 0.
>>>
>> +1 for both.
> 
> Some people might prefer that math be special cased.  Or some
> other module. A neutral criterion is needed.
> 

Actually, I don't think it's beyond reason to suggest that any 
module included with the standard distribution be special-cased in 
this way. That is, a reference to xxx.func(), without a previous 
import of xxx *could* be resolvable automatically, at least for 
those modules that can be found in a standard location. Whether 
it's a good idea to do so is another question. 

Offhand, I am not sure why it would be an insanely poor idea. It 
would mean some funky namespace building and possibly redundant 
imports, I guess. I'll certainly defer to just about anybody else's 
opinion as to the difficulty and advisability, but I believe it 
would be possible to do. 

Note that I am not saying that a reference to, say, 'argv' should 
provoke an automatic import. I don't mean that some automatic 
search for a matching function name should be done through some 
undefined module chain. I'm talking only about qualified 
references, like os.path or sys.stderr. 

As it is, a NameError is generated if the proper import has not 
been done. At the point of that error, the module name is known. 
For the programmer to fix the situation, an import statement has to 
be added to the code and then the code must be rerun. I don't see 
why it would be impossible to make that happen automatically, 
provided the module to be imported was recognized (through some 
undefined magic). I'd think the module would have to be known, 
because trying to import a nonexistent module -- "import ssy", say 
(in the event of a typo for "sys") -- would fail, so there would 
have to be a way to avoid looping on the "ssy.func()" line.

Is it worth adding that kind of complexity to execution of a Python 
program? Probably not. 

Is it even a good idea to try to make it happen automatically? 
Possibly not. For one thing, it wouldn't always be the right thing 
to do. A developer would most likely want to set up the imports 
properly, and to know when they were not correctly set up instead 
of having Python fix things quietly. There's a reason why Explicit 
is better than Implicit. 

But *could* it be done? I'd think so. 

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


Re: learning python

2005-09-04 Thread placid

Benji York wrote:
> placid wrote:
> > Christopher Culver wrote:
> >>"placid" <[EMAIL PROTECTED]> writes:
> >>>I was just wondering about good books that teach python (either with
> >>>programming or no programming experience at all) ? Or some online
> >>>tutorial?
> >>
> >>Did you even bother doing a web search? "Learn Python" or "Python
> >>tutorial" would be enough.
> >
> >
> > yeah, see i didnt even think of that.
> >
> > thanks man
>
> That was either a very gracious way to take a public correction, or an
> expertly executed bit of sarcasm, either way, placid, I applaud you.

well thanks man, no sarcasm intended at all.

Sometimes when you concentrate on complicated problems your thinking of
a complicated solution and not a simple one. Christopher gave a really
simple solution.( C does that to people i think) 

> --
> Benji York

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


Re: Possible improvement to slice opperations.

2005-09-04 Thread Ron Adam
Patrick Maupin wrote:

>>After considering several alternatives and trying out a few ideas with a
>>  modified list object Bengt Richter posted, (Thank You), I think I've
>>found a way to make slice operation (especially far end indexing)
>>symmetrical and more consistent.
> 
> 
> I don't know that it makes it more consistent.  I could be persuaded,
> but it would have to be by real-life examples with calculated slice
> indices and stride.  I do this thing all the time, and find the current
> rules simple and very consistent.  Occasionally, I might wish that
> things were a little different, but there is always a workaround.  I
> would have to see some real code examples, of sufficient scope to see
> that there are fewer workarounds with this proposal than with the
> current implementation.

I'll post a few examples once I'm sure the list object works correctly 
and then you can play around with it and try it out as well.

> FWIW, there is a reasonable workaround for the case where the indices
> might be negative and you would like zero or greater to mean 'end of
> list'.  If "x" is the index variable, you can use the expression (x<0
> and x or None) for the index value in the slice.  If you find yourself
> doing this often, you can write a little function for it -- def
> EndIndex(x): return x<0 and x or None.

Thanks, this is a good example.

Yes it does remove the need for a work around in those cases.

You still need to check for cases where you may cross the -1,0 boundary 
while incrementing or decrementing an index.  But that is an expected 
condition and easily handled with an 'if' or 'for' loop.


> But in real code, I fear you might need a similar helper function for
> similar issues with your change.  I just don't know what those are
> without more thought.
> 
> Regards,
> Pat

Thanks for the feedback, it was helpful.

Cheers,
Ron




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


Re: Magic Optimisation

2005-09-04 Thread simonwittber
> def loop(self):
> self_pool = self.pool
> self_call_exit_funcs = self.call_exit_funcs
> self_pool_popleft = self.pool.popleft
> self_pool_append = self.pool.append
> check = self.pool.__len__
> while check() > 0:
> task = self_pool_popleft()
> try:
> task.next()
> except StopIteration:
> self_call_exit_funcs(task)
> return
> self_pool_append(task)

Stupid me. the 'return' statement above should be 'continue'. Sorry for
the confusion.

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


use mod_python' session and redirect

2005-09-04 Thread wafd Alex
hi all
when i use Session object to save a object and then i use
util.redirect(req,url) to redirect a .psp.But in my psp i cann't load
session so i cann't get object from session,help me,thanks
here is my code
.py
s=Session.Session(req)
s["cur_user"]=user
s.set_timeout(18000)
s.save()
util.redirect(req, "/psp/user/edituser.psp")
edituser.psp
 <%
import com.lgd.model.allmodel as am
from mod_python import Session 
sess=Session.Session(req)
user=am.User()
uname=None
upass=None
fullname=None
email=None
if sess.is_new(): 
req.write("somethin opration")
else:
user=sess["cur_user"]
uname=user.uname
upass=user.upass
fullname=user.fullname
email=user.email
%>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Magic Optimisation

2005-09-04 Thread simonwittber
Psyco actually slowed down the code dramatically.
I've fixed up the code (replaced the erroneous return statement) and
uploaded the code for people to examine:

The test code is here: http://metaplay.dyndns.org:82/~xerian/fibres.txt

These are the run times (in seconds) of the test file.

without psyco:

0.00294482
0.03261255
0.06714886
0.87395510

with psyco.full():

0.00446651
0.05012258
0.15308657
11.23493663

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


Re: mod_python: what's going on here?

2005-09-04 Thread Steve Holden
Robert J. Hansen wrote:
> I'm not entirely certain comp.lang.python is the proper newsgroup for
> mod_python questions, but "comp.lang.python.web" doesn't seem to exist,
> so... my apologies in advance if this is considered off-topic.
> 
> I'm attempting to get mod_python 3.1.4/python 2.4.1 working on Apache
> 2.0.54 running under OS X.  Apache was compiled from source with a
> simple
> 
> /configure --enable-so --with-mpm=worker
> 
> ... followed by the make/make install dance.  mod_python was almost as
> simple:
> 
> ./configure --with-apxs=/usr/local/apache2/bin/apxs \
>   --with-python=/sw/bin/python2.4
> 
> ... followed by the requisite dance.
> 
> At this point, all's well.  The following bits were added to
> httpd.conf:
> 
> LoadModule python_module /usr/local/apache2/modules/mod_python.so
> 
>AddHandler mod_python .py
>PythonHandler mptest
>PythonDebug on
> 
> 
> ... one apachectl restart later, Apache was running fine and serving
> pages.  The version string at the bottom of some static pages listed
> mod_python as being present, so I reckoned that meant it was all
> installed all right.  However, any attempt to serve a mod_python
> script, mptest.py, from the test subdirectory results in a 500 Internal
> Server Error.  Nothing gets written to error_log, but access_log
> confirms the 500 was sent.
> 
> Does anyone have any experience with mod_python on OS X/Apache
> environments?  Can anyone shed some light on 500s that don't leave
> traces in the error logs, or what precise incantation I need to make
> mod_python start serving up scripts?
> 
> Also, if this is not appropriate for this group, does anyone know of a
> Python group for which this is more appropriate?
> 
You will probably get help on this newsgroup, but the mod-python list is 
pretty helpful, and there's a Python Web-Sig mailing list you can find 
out about at http://www.python.org/sigs/ if you want.

I'm afraid I am new to OS X (and 3,000 miles away from my Mac Mini), so 
I can't be any help directly with any Mac-dependent issues. But when I 
first started using mod_python someone (I'm afraid I don't remember who) 
advised me to use SetHandler rather than AddHandler.

Looking at my Windows httpd.conf (2.0.52, so close enough to yours) I see

#
#   #
#   MOD_PYTHON TESTING AREA #
#   #
#
#
#   Request handler
#

 AllowOverride FileInfo
 SetHandler mod_python
 PythonHandler mptest
 PythonDebug On

#
#   Filter handler
#

 AllowOverride All
 SetHandler mod_python
 #PythonHandler mptest1
 #PythonFixupHandler mptest1
 PythonLogHandler mptest1
 PythonOutputFilter mptest1 CAPITALIZE
 AddOutputFilter CAPITALIZE .txt .py
 PythonDebug On


This might at least be enough to start generating sensible error messages.

One further possibility is that Apache is using an older Python by 
default: byte codes vary from version to version, so something horrible 
might be happening as mod_python tries to execute, but that seems a bit 
far-fetched when you've got so far. I seem to remember that OS X doesn't 
supply 2.4 by default: have you added it or overwritten the default 
Python? Just another base to cover ...

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

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


ftp upload through proxy ?

2005-09-04 Thread Tonino
Hi,

I have been searching the groups for a way to upload a file using
URLLIB (2) to a ftp site using a proxy server.
I read the ftplib does not support proxy servers and I know that urllib
does support it.
I can download a file from a ftp site through the proxy - but now I
need to be able to upload.

There are some postings about file uploads using http_post but none
mentioned ftp uploads using urllib2 over proxy server ...

Has anyone done this ?  Can it be done ?

Thanks

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