Re: Defending Python

2005-07-12 Thread Andrew Durdin
On 7/13/05, Jorey Bump <[EMAIL PROTECTED]> wrote:
> >>> Bruno Desthuilliers <[EMAIL PROTECTED]> wrote:
> >>>
> >>> The larch!
> 
> IT'S A TREE

... not a shrubbery?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: C API : Creating a Py_Method object from a C function.

2005-07-12 Thread Martin v. Löwis
Hugh Macdonald wrote:
> PyMethodDef *callbackFunctionDef = new PyMethodDef;
> callbackFunctionDef->ml_name = "doLoadCallback";
> callbackFunctionDef->ml_meth = &myPython_doLoadCallback;
> callbackFunctionDef->ml_flags = 1;

I think this gives a memory leak. I was rather thinking of

static PyMethodDef doLoadCallback = {
  "doLoadCallback", &myPython_doLoadCallback, 1
};

   PyObject *pyCallbackFunc = PyCFunction_New(&doLoadCallback,
NULL);

Since you have to write the C(++) functions statically, you can also
provide the PyMethodDef objects statically.

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


Re: Web App like Google

2005-07-12 Thread Godwin
Thanks for providing me with all those informative links about NLTK nad
CNL. I'll certainly look into it.

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


Re: Web App like Google

2005-07-12 Thread Godwin
Thanks for informing me about NLTK. I'll certainly look into it and
other options. Hope my dream doesn't go into the graves.

Godwin Burby

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


Re: extend for loop syntax with if expr like listcomp&genexp ?

2005-07-12 Thread Bengt Richter
On Tue, 12 Jul 2005 23:07:07 -0500, Terry Hancock <[EMAIL PROTECTED]> wrote:

>On Monday 11 July 2005 08:53 pm, Bengt Richter wrote:
>> On Tue, 12 Jul 2005 10:12:33 +1000, John Machin <[EMAIL PROTECTED]> wrote:
>> >Bengt Richter wrote:
>> >> for x in (x for x in seq if x is not None):
>> >Byzantine ...
>> Perhaps not if you wanted to enumerate the selected elements, as in
>>for i, x in enumerate(x for x in seq if x is not None):
>
>Seems like a bug waiting to happen -- wouldn't someone using that
>idiom most likely have *meant* something like this:
>
>for i,x in enumerate(seq):
>   if x is not None:
>   print "seq[%d] = %s is not None" % (i, repr(x))
>
>?
>
>But of course that's not equivalent.  It's hard to imagine a
>use case for an enumerated loop when the object being
>iterated over is anonymous (will be lost as soon as the loop
>exits).
>
Line numbers in a listing of non-None things?
Page breaks at the right places?
Filtering out '' instead of NOne from results of a string split before creating 
numbered
html names for links to non-blank text elements in rendering text as html?
I dunno, seems like at least a few possibilities for something halfway 
sensible...

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


Re: set and frozenset unit tests?

2005-07-12 Thread Jacob Page
Reinhold Birkenfeld wrote:
> Jacob Page wrote:
> 
>>I'd like to 
>>run my code through actual set and frozenset unit tests.  Does any such 
>>code exist?  Is it in pure Python?  If so, where can it be obtained?
> 
> Look at /usr/lib/python2.x/test/ (on unix platforms).

Thanks for pointing that to me.  For some reason, the Debian package for 
python 2.4 doesn't include those tests, but I acquired them from an 
alternative source.

Oye, there's quite a number of set and frozenset features that aren't 
well-documented that I now need to implement.  What a fun chore!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: breaking out of nested loop

2005-07-12 Thread Terry Hancock
On Tuesday 12 July 2005 10:28 am, Tim Golden wrote:
> [Jeremy Sanders]
> | rbt wrote:
> | 
> | > What is the appropriate way to break out of this while loop 
> | if the for
> | > loop finds a match?
> | 
> | queue discussion why Python doesn't have a "break N" statement...
> 
> 
> 
> Presumably you meant "cue discussion..."
> 
> 

Or "queue" as in, it's going to have to take a number and wait for
someone to be bored enough with the ten other language "enhancement"
threads that have gone through lately.  ;-)

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com

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


Content violation

2005-07-12 Thread NAV2_SMTP_Gateway
Content violation found in email message.

From: python-list@python.org
To: [EMAIL PROTECTED]

File(s): message.zip

Matching filename: message.zip


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


Re: Defending Python

2005-07-12 Thread Jorey Bump
>> Jorey Bump wrote:
>> 
>>> Bruno Desthuilliers <[EMAIL PROTECTED]> wrote:
>>>
 it's.
 

>>> +1 for this becoming the official name of Python 3000. ;)

Monty Python's Flying Circus used to begin with "It's..." I had read at one 
time that "It's" was one of the original names proposed for the 
troupe/show, although I can't seem to find verification.  

Of course, based on some of the concerns voiced about Python 3000, maybe 
"It's Only A Flesh Wound" would be better. :)

This was simply a nonsequitur:

>>> And now for something completely different...
>>>
>>> The larch!

IT'S A TREE
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: extend for loop syntax with if expr like listcomp&genexp ?

2005-07-12 Thread Terry Hancock
On Monday 11 July 2005 08:53 pm, Bengt Richter wrote:
> On Tue, 12 Jul 2005 10:12:33 +1000, John Machin <[EMAIL PROTECTED]> wrote:
> >Bengt Richter wrote:
> >> for x in (x for x in seq if x is not None):
> >Byzantine ...
> Perhaps not if you wanted to enumerate the selected elements, as in
>for i, x in enumerate(x for x in seq if x is not None):

Seems like a bug waiting to happen -- wouldn't someone using that
idiom most likely have *meant* something like this:

for i,x in enumerate(seq):
if x is not None:
print "seq[%d] = %s is not None" % (i, repr(x))

?

But of course that's not equivalent.  It's hard to imagine a
use case for an enumerated loop when the object being
iterated over is anonymous (will be lost as soon as the loop
exits).

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com

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


Re: Frankenstring

2005-07-12 Thread Bengt Richter
On Wed, 13 Jul 2005 03:49:16 +0200, Thomas Lotze <[EMAIL PROTECTED]> wrote:

>Scott David Daniels wrote:
>
>> Now if you want to do it for a file, you could do:
>> 
>>  for c in thefile.read():
>>  
>
>The whole point of the exercise is that seeking on a file doesn't
>influence iteration over its content. In the loop you suggest, I can
>seek() on thefile to my heart's content and will always get its content
>iterated over exactly from beginning to end. It had been read before any
>of this started, after all. Similarly, thefile.tell() will always tell me
>thefile's size or the place I last seek()'ed to instead of the position of
>the next char I will get.
>
What I suggested in my other post (untested beyond what you see, so you
may want to add to the test ):

< lotzefile.py >--
class LotzeFile(file):
BUFSIZE = 4096
def __init__(self, path, mode='r'):
self.f = file(path, mode)
self.pos = self.bufbase =  0
self.buf = ''
def __iter__(self): return self
def next(self):
if not self.buf[self.pos:]:
self.bufbase += len(self.buf)
self.pos = 0
self.buf = self.f.read(self.BUFSIZE)
if not self.buf:
self.close()
raise StopIteration
byte = self.buf[self.pos]
self.pos += 1
return byte
def seek(self, pos, ref=0):
self.f.seek(pos, ref)
self.bufbase = self.f.tell()
self.pos = 0
self.buf = ''
def tell(self):
return self.bufbase + self.pos
def close(self):
self.f.close()

def test():
f = file('lotzedata.txt','w')
for s in (' %3d'%i for i in xrange(1000)): f.write(s)
f.close()

it = iter(LotzeFile('lotzedata.txt'))

hold4=[0,0,0,0]
for i, c in enumerate(it):
hold4[i%4] = c
if i%4==3: 
print hold4
assert (i-3)/4 == int(''.join(hold4))
if i == 99: break
print it.tell()
it.seek(52)
for i in xrange(8): print it.next(),
print
it.seek(990*4)
for c in it: print c,

if __name__ == '__main__':
test()
--

Result:

[20:53] C:\pywk\clp>py24 lotze.py
[' ', ' ', ' ', '0']
[' ', ' ', ' ', '1']
[' ', ' ', ' ', '2']
[' ', ' ', ' ', '3']
[' ', ' ', ' ', '4']
[' ', ' ', ' ', '5']
[' ', ' ', ' ', '6']
[' ', ' ', ' ', '7']
[' ', ' ', ' ', '8']
[' ', ' ', ' ', '9']
[' ', ' ', '1', '0']
[' ', ' ', '1', '1']
[' ', ' ', '1', '2']
[' ', ' ', '1', '3']
[' ', ' ', '1', '4']
[' ', ' ', '1', '5']
[' ', ' ', '1', '6']
[' ', ' ', '1', '7']
[' ', ' ', '1', '8']
[' ', ' ', '1', '9']
[' ', ' ', '2', '0']
[' ', ' ', '2', '1']
[' ', ' ', '2', '2']
[' ', ' ', '2', '3']
[' ', ' ', '2', '4']
100
1 3 1 4
  9 9 0   9 9 1   9 9 2   9 9 3   9 9 4   9 9 5   9 9 6   9 9 7   9 9 8   9 9 9

I suspect you could get better performance if you made LotzeFile instances able 
to
return interators over buffer chunks and get characters from them, which would
be string iterators supplying the characters rather than the custom .next, but
the buffer chunks would have to be of some size to make that pay. Testing is
the only way to find out what the crossing point is, if you really have to.

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


Re: if not DEBUG: log = null_log

2005-07-12 Thread @(none)
Bengt Richter wrote:
> On Wed, 13 Jul 2005 11:00:14 +1000, Simon Burton <[EMAIL PROTECTED]> wrote:
> 
> 
>>Hi,
>>
>>I'm after a no-op command, so that i can redirect
>>logging commands in performance critical code.
>>
>>Something like this:
>>
>>def log(*args): print args
>>def null_log(*args): pass
>>if not DEBUG: log = null_log
>>
>>is unacceptable because of the overhead of calling
>>functions in python.
>>
> 
> I think you could make the existence of log calls dependent on
> whether you compile with an optimize flag by abusing
> an assert statement, e.g.,
> 
> assert log(some, args) or True
> 

This is a session with the -O flag, so asserts disapear:

 >>> from time import time
 >>>
 >>> def count(N=1000):
...   for i in xrange(N): i=0
...
 >>>
 >>> t=time(); count(); print time()-t

0.821492910385
 >>>
 >>> def count_call(N=1000):
...   for i in xrange(N): foo()
...
 >>> t=time(); count_call(); print time()-t
3.50276303291
 >>>
 >>> def log(): print "log"
...
 >>>
 >>> def count_assert(N=1000):
...   for i in xrange(N): assert log()
...
 >>>
 >>> t=time(); count_assert(); time()-t
0.61060500144958496

Woohoo!!


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


Re: Help with report

2005-07-12 Thread ChrisH
> You know, this is the most concise example of feature-creep in a 
> specification that I've ever seen.
> 
You're right. I'm afraid it's from too much work and too little sleep.

I'll try to be more precise next time.

Thanks to everyone for their help.
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fwd: Should I use "if" or "try" (as a matter of speed)?

2005-07-12 Thread Christopher Subich
Thomas Lotze wrote:
> Neither does it to me. What about
> 
> try:
> f=file('file_here')
> except IOError: #File doesn't exist
> error_handle
> else:
> do_setup_code
> do_stuff_with(f)
> 
> (Not that I'd want to defend Joel's article, mind you...)

That works.  I'm still not used to having 'else' available like that.  I 
wonder how Joel advocates managing in C++-likes that don't have a 
try/catch/else semantic.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fwd: Should I use "if" or "try" (as a matter of speed)?

2005-07-12 Thread Dark Cowherd
OK, I can see that the Python way of doing things is very different.
However I think Roy made a very pertinent point
"Imagine if do_setup_code or
do_stuff_with(f) unexpectedly threw an IOError for some reason totally
unrelated to the file not existing."
This is the kind of situation that the rule 'catch it on the next
line' is trying to avoid

What I didnt realise till I read Thomas comment is that the try except
had an else clause. This is nice.

But seriously, if you expected to write reasonably large business
applications with multiple people in the team and teams changing over
time what would you give as a guideline for Error handling

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


Re: if not DEBUG: log = null_log

2005-07-12 Thread Bengt Richter
On Wed, 13 Jul 2005 11:00:14 +1000, Simon Burton <[EMAIL PROTECTED]> wrote:

>
>Hi,
>
>I'm after a no-op command, so that i can redirect
>logging commands in performance critical code.
>
>Something like this:
>
>def log(*args): print args
>def null_log(*args): pass
>if not DEBUG: log = null_log
>
>is unacceptable because of the overhead of calling
>functions in python.
>
I think you could make the existence of log calls dependent on
whether you compile with an optimize flag by abusing
an assert statement, e.g.,

assert log(some, args) or True

would always make the call in debug mode, but would never raise the exception
because of the "or True", even if log return None. If you compile with 
optimization,
the entire assert statement disappears from the byte code, UIAM.

if you use

if __debug__: log(some, args)

I think[1] you still the the if-test code, though that is pretty quick
compared to a function call, so maybe you don't have to worry about it,
unless it is in a super-hot loop.

[1] I thought is saw somewhere that
if __debug__: suite
might be completely optimized away like assert, but I couldn't locate it off 
hand.
It would seem pretty safe and useful though.

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


Re: Help with inverted dictionary

2005-07-12 Thread Steven D'Aprano
On Wed, 13 Jul 2005 11:38:44 +1000, John Machin wrote:

> [EMAIL PROTECTED] wrote:
>> I will transfer eventually use a database but is there any way for now
>> you could help me make the text files?  Thank you so much.  Reece
>> 
> 
> No. There is utterly no reason why you should create 5000 or 3 text 
> files.

There is one possible reason: if it is a homework assignment, and creating
all those files is part of the assignment.

(I've seen stupider ideas, but not by much.)

-- 
Steven.

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


Re: math.nroot [was Re: A brief question.]

2005-07-12 Thread Steven D'Aprano
(All previous quoting ruthlessly snipped.)

A question for Tim Peters, as I guess he'll have the most experience in
this sort of thing.

With all the cross-platform hassles due to the various C compilers not
implementing the IEEE standard completely or correctly, I wonder how much
work would be involved for some kind soul to implement their own maths
library to do the lot, allowing Python to bypass the C libraries
altogether. 

Are you falling over laughing Tim, or thinking what a great idea?

What sort of work is needed? Is it, say, as big a job as maintaining
Python? Bigger? One weekend spent working solidly?



-- 
Steven.

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


Re: new division in Pythonwin

2005-07-12 Thread Mike Meyer
"Jive Dadson" <[EMAIL PROTECTED]> writes:

> I like to use Pythonwin as my desktop calculator.  It's bothersome
> to have to type in "from __future__ import division" into the
> interactive window every time I open it.  I've tried various ways to
> attempt to get it to import new division at startup, but no luck.  I
> can get it to execute a file, but apparently it does so in an
> environment that gets discarded, rather than in the environment of
> the interactive window.  So the "from __future__" statement has no
> effect.
>
> Any suggestions?

I'm not familiar with Pythonwin, but would expect it to honor the
PYTHONSTARTUP environment variable. That's how I get various things
loaded into my interactive pythons.

 http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fwd: Should I use "if" or "try" (as a matter of speed)?

2005-07-12 Thread Mike Meyer
Dark Cowherd <[EMAIL PROTECTED]> writes:

> But one advise that he gives which I think is of great value and is
> good practice is
> "Always catch any possible exception that might be thrown by a library
> I'm using on the same line as it is thrown and deal with it
> immediately."

Yuch. That sort of defeats the *purpose* of exceptions in Python:
letting you get on with the coding, and dealing with the errors when
it's convenient. Consider:

try:
out = file(datafile, "wb")
out.write(genData1())
out.write(genData2())
out.write(genData3())
except IOError, msg:
print >>sys.stderr, "Save failed:", msg
if os.path.exists(datafile):
   os.unlink(datafile)

I don't even want to *think* writing the try/except clause for each
line. It reminds me to much of:

if (!(out = open(datafile, "w"))) {
   /* handle errors */
   return ;
}
if (write(out, genData1()) <0) {
   /* handle errors */
   return ;
}
etc.

Generally, I treat exceptions as exceptional. I catch the ones that
require something to be changed to restore the program or environment
to a sane state - and I catch them when it's convenient. The rest I
catch globally and log, so I can figure out what happened and do
something to prevent it from happening again.

  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: if not DEBUG: log = null_log

2005-07-12 Thread Steven D'Aprano
On Wed, 13 Jul 2005 11:00:14 +1000, Simon Burton wrote:

> Hi,
> 
> I'm after a no-op command, so that i can redirect
> logging commands in performance critical code.
> 
> Something like this:
> 
> def log(*args): print args
> def null_log(*args): pass
> if not DEBUG: log = null_log
> 
> is unacceptable because of the overhead of calling
> functions in python.

Excuse my skepticism, but I'd like to see the profiling that you did that
shows this is a problem. Everybody thinks their code is performance
critical, even when it isn't.

So, let's do a test. First, I set up a fake object that requires lots of
name look-ups:

>>> class Placeholder:
... pass
...
>>> obj = Placeholder()
>>> obj.method = Placeholder()
>>> obj.method.attribute = Placeholder()
>>> obj.method.attribute.record = None

Now I run a test to time those name lookups:

>>> def test_func_overhead(n):
... loop = range(n)
... t = time.time()
... for i in loop:
... f = obj.method.attribute.record
... t = time.time() - t
... return t
...
>>> test_func_overhead(10)
0.10761499404907227
>>> test_func_overhead(10)
0.13230800628662109
>>> test_func_overhead(10)
0.11942911148071289

Now set up a test bypassing the name lookups:

>>> def test_func_no_overhead(n):
... loop = range(n)
... y = obj.method.attribute.record
... t = time.time()
... for i in loop:
... f = y
... t = time.time() - t
... return t
...
>>> test_func_no_overhead(10)
0.052425861358642578
>>> test_func_no_overhead(10)
0.042248010635375977
>>> test_func_no_overhead(10)
0.055256843566894531

So four global lookups performed 100,000 times takes 0.1 second, while a
single local lookup performed 100,000 times takes 0.05 second. Not a big
difference. Extrapolating from four lookups down to one would suggest
that global lookups are faster than local, which can't be right.

Redoing test_func_overhead to only make one lookup (change the
reference to obj.method.attribute.record to just obj) gives very
surprising results:

>>> test_func_overhead2(10)
0.041122913360595703
>>> test_func_overhead2(10)
0.037561893463134766
>>> test_func_overhead2(10)
0.020340204238891602

According to my testing, looking up a global variable is *faster* than
looking up a local. H. Caching effects perhaps? 

Try the local version again:

>>> test_func_no_overhead(10)
0.025532007217407227
>>> test_func_no_overhead(10)
0.016258001327514648
>>> test_func_no_overhead(10)
0.016184806823730469

Ah, that's better! At least now the local lookup is slightly faster
than the global. But also widely different from the first run. Function
lookup is so fast to start with, and affected by so many external factors,
that the cost-benefit equation of trying to optimize it is not very good.
There almost certainly will be more useful things for you to spend your
time on.

More comments below:-

> log ("about to slip into python feature request mode.")
> 
> Maybe this is what the PEP 336 guy was thinking of (Make None Callable).
> Obviously we don't want None to be callable, but
> what about a "Null" [1] that's callable, with any args ?
> 
> But I guess what I am really asking for is something on the bytecode
> level that tells the VM to "do nothing". 
> 
> Here's an idea: make "pass" into an expression (a value) that is callable, 
> with any args, and returns None.
> 
> log ("finished with python feature request mode.")

And how do you use this without the overhead of function calls?

Do you sprinkle your code with:

if DEBUG:
log("spam")
else:
pass("spam")

?

Why not just do this?

if DEBUG: log("spam")

Which is much simpler. Or even simpler still, put the "if DEBUG" test
inside log, since the extra time taken in calling the function is probably
lost in the noise of the rest of your code.




Not related to the logging issue:

> I recently discovered "pyc" [2], but i don't quite see how i can
> use it while maintaining python source compatability.
[snip]
> [2]: http://students.ceid.upatras.gr/~sxanth/pyc/

How fascinating. From the pyc page:

"In fact you can use pyc to re-compile your standard library and make it
about 100kB smaller."

Hmmm. Let's see now, Python 2.3.3 on Fedora Core 2:

$ du -hs /usr/lib/python2.3/
88M /usr/lib/python2.3/

Oh joy! So by using pyc, I can save 0.11% of the Python library storage
requirements!

For all I know, pyc is a very useful package, and kudos to the author for
taking the time and effort to develop it. But if this space saving is
"one good reason to use pyc" according to the author, I'm not impressed.


-- 
Steven.

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


Multivariable polynomials

2005-07-12 Thread Chris
Does anyone know of a good standalone implementation of multivariable
polynomials in python?

Thanks,
Chris

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


Fredericksburg, VA ZPUG Meeting

2005-07-12 Thread Benji York
When: July 13, 7:30-9:00 PM
Where: Zope Corp offices
(513 Prince Edward Street;  Fredericksburg, VA 22408)

Details at http://www.zope.org/Members/poster/fxbgzpug_announce_2

Hope to see you there!
--
Benji York
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Browser plug-in for Python?

2005-07-12 Thread Bill Janssen
Yes, thanks, back in the day I used Grail and played with rexec.

I notice that one of the Summer of Code projects was to recreate
rexec, but don't know if it was funded.

Presumably a Firefox plug-in for Python would restrict the execution
environment in some safe way, and provide some kind of UI toolkit API
so that the Python program could draw on the area given to the
plug-in.  If I were doing it, I'd probably add something like Joel
Bartlett's "ezd" drawing system (see
http://www.hpl.hp.com/techreports/Compaq-DEC/WRL-91-6.pdf), replacing
the Scheme used in ezd with Python.

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


Re: Frankenstring

2005-07-12 Thread Thomas Lotze
Scott David Daniels wrote:

> Now if you want to do it for a file, you could do:
> 
>  for c in thefile.read():
>  

The whole point of the exercise is that seeking on a file doesn't
influence iteration over its content. In the loop you suggest, I can
seek() on thefile to my heart's content and will always get its content
iterated over exactly from beginning to end. It had been read before any
of this started, after all. Similarly, thefile.tell() will always tell me
thefile's size or the place I last seek()'ed to instead of the position of
the next char I will get.

-- 
Thomas

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


Re: Help with inverted dictionary

2005-07-12 Thread John Machin
[EMAIL PROTECTED] wrote:
> I will transfer eventually use a database but is there any way for now
> you could help me make the text files?  Thank you so much.  Reece
> 

No. There is utterly no reason why you should create 5000 or 3 text 
files. While you are waiting to get a clue about databases, do it in 
Python, in memory. It should only take a very tiny time to suck your 
5000-fact file into memory, index the data appropriately, and do some 
queries e.g. list all facts about "lion".

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


Re: plot module

2005-07-12 Thread Robert Kern
Shankar Iyer ([EMAIL PROTECTED]) wrote:
> Hi,
> 
> I am looking for documentation on the plot module.  Does anyone know where I 
> can find this information?  Thanks.

What is "the plot module"? There are any number of Python modules for 
making plots and charts. Some of them may even be called "plot". Which 
one are you talking about?

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

2005-07-12 Thread Titi Anggono
I just use Gnuplot for plot module. 
Maybe you can visit 
http://gnuplot-py.sourceforge.net/
or the mailing list 
http://lists.sourceforge.net/lists/listinfo/gnuplot-py-users

--- "Shankar Iyer ([EMAIL PROTECTED])"
<[EMAIL PROTECTED]> wrote:

> Hi,
> 
> I am looking for documentation on the plot module. 
> Does anyone know where I can find this information? 
> Thanks.
> 
> Shankar
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: math.nroot [was Re: A brief question.]

2005-07-12 Thread Tim Peters
[Michael Hudson]
> I doubt anyone else is reading this by now, so I've trimmed quotes
> fairly ruthlessly :)

Damn -- there goes my best hope at learning how large a message gmail
can handle before blowing up .  OK, I'll cut even more.

[Michael]
>>> Can't we use the stuff defined in Appendix F and header  of
>>> C99 to help here?  I know this stuff is somewhat optional, but it's
>>> available AFAICT on the platforms I actually use (doesn't mean it
>>> works, of course).

[Tim]
>> It's entirely optional part of C99.

> Hmm, is  optional?  I'm not finding those words.  I know
> Appendix F is.

fenv.h is required, but the standard is carefully worded so that
fenv.h may not be of any actual use.  For example, a conforming
implementation can define FE_ALL_EXCEPT as 0 (meaning it doesn't
define _any_ of the (optional!) signal-name macros:  FE_DIVBYZERO,
etc).  That in turn makes feclearexcept() (& so on) pretty much
useless -- you couldn't specify any flags.

If the implementation chooses to implement the optional Appendix F,
then there are stronger requirements on what fenv.h must define.

>> Python doesn't require C99.

> Sure.  But it would be possible to, say, detect C99 floating point
> facilities at ./configure time and use them if available.

Yes.

>> The most important example of a compiler that doesn't support any of
>> that stuff is Microsoft's, although they have their own MS-specific
>> ways to spell most of it.

> OK, *that's* a serious issue.
> 
> If you had to guess, do you think it likely that MS would ship fenv.h
> in the next interation of VC++?

Sadly not.  If they wanted to do that, they had plenty of time to do
so before VC 7.1 was released (C99 ain't exactly new anymore).  As it
says on

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

MS and Borland (among others) appear to have no interest in C99.

In part I expect this is because C doesn't pay their bills nearly so
much as C++ does, and C99 isn't a standard from the C++ world.

>>> In what way does C99's fenv.h fail?  Is it just insufficiently
>>> available, or is there some conceptual lack?

>> Just that it's not universally supported.  Look at fpectlmodule.c for
>> a sample of the wildly different ways it _is_ spelled across some
>> platforms.

> C'mon, fpectlmodule.c is _old_.  Maybe I'm stupidly optimistic, but
> perhaps in the last near-decade things have got a little better here.

Ah, but as I've said before, virtually all C compilers on 754 boxes
support _some_ way to get at this stuff.  This includes gcc before C99
and fenv.h -- if the platforms represented in fpectlmodule.c were
happy to use gcc, they all could have used the older gcc spellings
(which are in fpectlmodule.c, BTW, under the __GLIBC__ #ifdef).  But
they didn't, so they're using "minority" compilers.  I used to write
compilers for a living, but I don't think this is an inside secret
anymore :  there are a lot fewer C compiler writers than there
used to be, and a lot fewer companies spending a lot less money on
developing C compilers than there used to be.

As with other parts of C99, I'd be in favor of following its lead, and
defining Py_ versions of the relevant macros and functions.  People on
non-C99 platforms who care enough can ugly-up pyport.h with whatever
their platform needs to implement the same functionality, and C99
platforms could make them simple lexical substitutions.  For example,
that's the path we took for Python's C99-workalike Py_uintptr_t and
Py_intptr_t types (although those are much easier to "fake" across
non-C99 platforms).

>> A maze of #ifdefs could work too, provided we defined a
>> PyWhatever_XYZ API to hide platform spelling details.

> Hopefully it wouldn't be that bad a maze; frankly GCC & MSVC++ covers
> more than all the cases I care about.

I'd be happy to settle for just those two at the start,  As with
threading too, Python has suffered from trying to support dozens of
unreasonable platforms, confined to the tiny subset of abilities
common to all of them.  If, e.g., HP-UX wants a good Python thread or
fp story, let HP contribute some work for a change.  I think we have
enough volunteers to work out good gcc and MSVC stories -- although I
expect libm to be an everlasting headache (+ - * are done in HW and
most boxes have fully-conforming 754 semantics for them now; but there
are no pressures like that working toward uniform libm behaviors;
division is still sometimes done in software, but the divide-by-0 is
check is already done by Python and is dead easy to do).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating anonymous functions using eval

2005-07-12 Thread Steven D'Aprano
On Tue, 12 Jul 2005 17:17:46 -0600, Joseph Garvin wrote:

> Robert Kern wrote:
> 
>>Not everyone is reading this list in a conveniently threaded 
>>form
>>  
>>
> Why not? Just about every modern newsgroup reader and e-mail app has a 
> threaded view option.

Technology as a substitute for manners is it?

I have a modern newsgroup reader. I don't like threaded views, but even
if I did, that's not the point. News servers sometimes drop posts, or the
posts expire. Sometimes news readers lose posts (that just happened to me
yesterday). Sometimes threading breaks. Quoting enough of the previous
post to establish context is the only sensible behaviour in the face of
all these potential problems.

But even that is not the point.

It is rude for people to assume that their post is so vitally important to
me that I'll drop what I'm doing to hunt back through past posts searching
for context. Even if that search is "back one post in the thread", that's
still one post too many.

In the face of that breach of manners, people may choose to respond in
many ways. Some might choose to reward the rudeness by searching previous
posts, then responding with an answer to the question. Some might choose
to just ignore the post, which has the disadvantage of leaving the
original poster no wiser and likely to repeat his behaviour. Some might
flame them, which is usually counterproductive. And some might drop some
fairly heavy hints, but that assumes the poster is capable of getting a
clue.


-- 
Steven.

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


if not DEBUG: log = null_log

2005-07-12 Thread Simon Burton

Hi,

I'm after a no-op command, so that i can redirect
logging commands in performance critical code.

Something like this:

def log(*args): print args
def null_log(*args): pass
if not DEBUG: log = null_log

is unacceptable because of the overhead of calling
functions in python.

log ("about to slip into python feature request mode.")

Maybe this is what the PEP 336 guy was thinking of (Make None Callable).
Obviously we don't want None to be callable, but
what about a "Null" [1] that's callable, with any args ?

But I guess what I am really asking for is something on the bytecode
level that tells the VM to "do nothing". 

Here's an idea: make "pass" into an expression (a value) that is callable, 
with any args, and returns None.

log ("finished with python feature request mode.")

I recently discovered "pyc" [2], but i don't quite see how i can
use it while maintaining python source compatability.

bye!

Simon.

[1]: http://occs.cs.oberlin.edu/~jwalker/nullObjPattern/
[2]: http://students.ceid.upatras.gr/~sxanth/pyc/

-- 
Simon Burton, B.Sc.
Licensed PO Box 8066
ANU Canberra 2601
Australia
Ph. 61 02 6249 6940
http://arrowtheory.com 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating anonymous functions using eval

2005-07-12 Thread Robert Kern
Joseph Garvin wrote:
> Robert Kern wrote:
> 
>>Not everyone is reading this list in a conveniently threaded 
>>form
> 
> Why not? Just about every modern newsgroup reader and e-mail app has a 
> threaded view option.

Good point. Allow me to modify my statement: not all newsreaders/email 
apps thread python-list/c.l.py conversations properly. The gateway often 
messes things up.

It's also a royal pain in the butt to have to go read another message 
just to dereference anaphora, threading or no.

-- 
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: Creating anonymous functions using eval

2005-07-12 Thread Dave Benjamin
Joseph Garvin wrote:
> Robert Kern wrote:
> 
>> Not everyone is reading this list in a conveniently threaded form
>>  
>>
> Why not? Just about every modern newsgroup reader and e-mail app has a 
> threaded view option.

My newsreader supports threading, but the first message I see in this 
thread is from Devan L. I didn't notice the "Re:" and assumed his post 
was half in the subject line and half in the body.

Sometimes I only download the 100 most recent posts, so I don't see the 
original (or referred-to) post. Other times the threading doesn't work 
properly, splitting conversations or using the wrong level of 
indentation. Also, this is both a newsgroup and a mailing list, and that 
has adverse effects on threading as well.

So, please quote. =)

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


Re: Browser plug-in for Python?

2005-07-12 Thread Jeff Epler
Back in the day there was 'grail', which was a browser in its own right.
There may also have been a plug-in for other browsers, but I don't know
any real details about them.

Python itself has deprecated the 'restricted execution' environment it
had in previous versions, because ways to break out of the jail existed
or were thought to exist, and nobody stepped forward and offered to
spend the requisite time to create and validate (even in a hand-wavy
kind of way) a new security model.

If you want to write programs in Python and run them in today's
browsers, the shortest path from here to there is jython.  Several
applet demos are available at
http://www.jython.org/applets/index.html

I have used Jython a little bit, but never seriously and not in the past
few years.  Jython implements an older version of the Python
language, corresponding to cPython 2.1 if I remember correctly.

Jeff


pgpp6YhsWNcK0.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: append one file to another

2005-07-12 Thread John Machin
[EMAIL PROTECTED] wrote:
> Hi,
> 
> I want to append one (huge) file to another (huge) file.  The current
> way I'm doing it is to do something like:
> 
> infile = open (infilename, 'r')
> filestr = infile.read()
> outfile = open(outfilename, 'a')
> outfile.write(filestr)
> 
> I wonder if there is a more efficient way doing this?

Don't wonder, like the ancient philosophers; be an empiricist :-)


> Thanks.
> 

If the files are truly huge, you run the risk of exhausting real memory 
and having to swap.

Try this:
Having opened the files,

for line in infile:
 outfile.write(line)

Otherwise look at the docs for read the method and check out the "size" 
argument.

General warnings: (1) If you want to be portable, consider text/binary 
differences. (2) Consider what to do if the last line in  
is not terminated.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to match literal backslashes read from a text file using regular expressions?

2005-07-12 Thread George Sakkis
This should give you an idea of how to go about it (needs python 2.3 or
newer):


import re
slashPattern = re.compile(r'\\(.*?)\\')

for i,line in enumerate(file("parseinput")):
print "line", i+1,
match = slashPattern.search(line)
if match:
print "matched:", match.group(1)
else:
print "did not match"

#= output ===

line 1 matched: 'di_--v*-.ga_-t
line 2 matched: 'pas-*m

#


George

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


Re: Inconsistency in hex()

2005-07-12 Thread Raymond Hettinger
[Steven D'Aprano]
> > hex() of an int appears to return lowercase hex digits, and hex() of a
> > long uppercase.

[Terry Reedy]
> Already bug-reported and fixed for 2.5 (to use lowercase, I believe).
> http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=1224347

Score another victory for the time machine ;-)


Raymond

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


Re: How to match literal backslashes read from a text file using regular expressions?

2005-07-12 Thread John Machin
[EMAIL PROTECTED] wrote:
> I'm parsing a text file to extract word definitions. For example the
> input text file contains the following content:
> 
> di.va.gate \'di_--v*-.ga_-t\ vb
> pas.sim \'pas-*m\ adv : here and there : THROUGHOUT
> 
> I am trying to obtain words between two literal backslashes (\ .. \). I
> am not able to  match words between two literal backslashes using the
> regxp - re.compile(r'\\[^\\]*\\').
> 
> Here is my sample script:
> 
> import re;

Lose the semicolons ...

> 
> #slashPattern = re.compile(re.escape(r'\\[^\\]*\\'));
> pattern = r'\\[^\\]*\\'
> slashPattern = re.compile(pattern);
> 
> fdr = file( "parseinput",'r');
> line = fdr.readline();
> 

You should upgrade so that you have a modern Python and a modern 
tutor[ial] -- then you will be writing:

for line in fdr:
 do_something_with(line)


> while (line != ""):

Lose the extraneous parentheses ...

> if (slashPattern.match(line)):

Your main problem is that you should be using the search() method, not 
the match() method. Read the section on this topic in the re docs!!

 >>> import re
 >>> pat = re.compile(r'\\[^\\]*\\')
 >>> pat.match(r'abcd \xyz\ pqr')
 >>> pat.search(r'abcd \xyz\ pqr')
<_sre.SRE_Match object at 0x00AE8988>


> print  line.rstrip()  + " <-- matches pattern " + pattern
> else:
> print  line.rstrip()  + " <-- DOES not match pattern " +
> pattern
> line = fdr.readline();
> print;
> 
> 
> --
> The output
> 
> C:\home\krishna\lang\python>python wsparsetest.py
> python wsparsetest.py
> di.va.gate \'di_--v*-.ga_-t\ vb <-- DOES not match
> pattern \\[^\\]*\\
> pas.sim \'pas-*m\ adv : here and there : THROUGHOUT <-- DOES not match
> pattern \\[^\\]*\\
> ---
> 
> What should I be doing to match those literal backslashes? 
> 
> Thanks
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Earthquake Forecasting Program July 11, 2005

2005-07-12 Thread Hank Oredson
"edgrsprj" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> "Hank Oredson" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
>> "edgrsprj" <[EMAIL PROTECTED]> wrote in message
>> news:[EMAIL PROTECTED]
>> > "edgrsprj" <[EMAIL PROTECTED]> wrote in message
>> > news:[EMAIL PROTECTED]
>> >> PROPOSED EARTHQUAKE FORECASTING
>> >> COMPUTER PROGRAM DEVELOPMENT EFFORT
>
>> What observational data are used?
>> What are the sources of that observational data?
>> How are those sources accessed?
>> Is there a database to hold historical plus current data?
>> If so, is it centralized or distributed?
>>
>> The project might be of interest if the data sources are
>> rich enough, complete enough, and current enough.
>>
>
> July 12, 2005
>
> Thanks for the response and questions.
>
> The following are my personal opinions on this.
>
>   Briefly, the amount of data available for this type of effort is
> virtually limitless.  And more of those data than most people could even
> deal with can be obtained for free.

I guess my question was not specific enough.
What I wanted was the exact sources, so I could access the data.
The exact data sets you used.

> You don't have to build a new laboratory filled with expensive equipment.
>
> A reasonably powerful computer,
> Access to the Internet,
> At least some knowledge of science
> Some computer programming ability
> And a little imagination

I'm a retired physicist with a great deal of experience in data
transformation, verification and analysis. Also plenty of computers.
Fast internet connection.

> Are all that are required.

I have all those things.

>   Researchers have been attempting to do this type of work for 
> probably
> as far back as we have historical records.  The reason that previous 
> efforts
> that I am aware of have not been successful is because two key discoveries
> needed to be made.  They are referred to on my 90-05.html Web page as the
> "Gravity Point" and "Earthquake Triggering Symmetry."  Now that those
> discoveries have been made the door should be open to tremendously rapid
> advances in our understanding of how and why earthquakes occur and how to
> forecast them.

References please, I found some simple description, but no
mathematics or references to the data sets used or the equations
you used to do your analysis. Point me to that stuff.

>   Much of this research could be easily done by computer programmers.
> You don't need to be a geophysicist.  If the data you are generating look
> statistically significant then they are probably important whether or not
> you actually understand the geophysical theories behind them.

No problem understanding the physics (geo or otherwise).
No problem writing software to do the analyses.
Might even be fun.

>   To actually forecast earthquakes using the procedure I have 
> developed
> you need both warning signal data and earthquake data along with some 
> ocean
> tide and Solid Earth Tide data.  But one of the really great parts of this
> particular research project is the fact that many of the basic discoveries
> can be made by simply comparing earthquakes with one another.  You don't
> need any warning signal data at all.  And there is certainly no shortage 
> of
> earthquake data!

Yes, I understand all that.
Where are the data sets?
Where is the description of the "procedure"?
URLs would be nice, journal article references are ok.

>   At my Web site there is a discussion of a concept called "Earthquake
> Pairs."  They are two or more earthquakes which were apparently triggered 
> in
> the same manner.  My data indicate that the two highly destructive 1998
> earthquakes in Afghanistan would represent an Earthquake Pair.  And the 
> two
> highly destructive 1999 earthquakes in Turkey would represent another 
> pair.
> Important discoveries can be made by determining what the similarities are
> between the two or more earthquakes in an Earthquake Pair and how they
> differ from other earthquakes.  And since the group of earthquake warning
> signals that I am presently working with is being controlled by the same
> forces that are responsible for earthquake triggering, significant
> discoveries regarding earthquake triggering processes could be immediately
> applied to forecasting efforts.

I would rather do my own data analysis, but for me to do that
there must be published data sets, that I can use.
Doing the various coorelations, power spectra, convolutions is easy.
So what is needed is the data sets, and the specific things you think
make prediction possible. Then I can test those things, along with
others that I might find interesting.

>   One of the reasons that geologists have not yet taken an interest in
> this particular effort could be because it is heavily reliant on celestial
> mechanics.  And most geology researchers appear to me to prefer to focus 
> on
> measuring forces within the ground.  I presently suspect that astronomers
> would be a mor

Re: Thoughts on Guido's ITC audio interview

2005-07-12 Thread Markus Wankus
Markus Wankus wrote:
> 
> My opinion - If you aren't willing to try something new, or have an 
> aversion to it in the first place, nothing we can say will change your 
> mind.

Correction...

*There are some people, who* if they aren't willing to try something 
new, or have an aversion to it in the first place, nothing we can say 
will change their mind.

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


Browser plug-in for Python?

2005-07-12 Thread Bill Janssen
Has anyone written a browser plug-in for Python, similar to the Java
plug-in that Sun has switched to for applets?

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


Re: Thoughts on Guido's ITC audio interview

2005-07-12 Thread Markus Wankus
Stephen Toledo-Brown wrote:
> Tony Meyer wrote:
> 
>>> Everyone complaining about Eclipse in this thread needs to go try 
>>> 3.1. The interface is much much much more responsive.
>>
>>
>>
>> The problem with Eclipse, IMO, is Java.  I've tried 3.1 on a WinXP 
>> machine
>> and, like just about any Java program, it's incredibly slow and a real 
>> pain
>> to use.  On a (similarly spec'd) Mac OS X Tiger machine, it runs nice and
>> smoothly and is reasonably nice to use.  I'd happily recommend that Mac
>> users try Eclipse, but never a Windows (Python) programmer.
> 
> 
> I've not tried Mac, but under both Windows and Linux on x86, I find 
> Eclipse (3.0) is slow with less than 1.25 GB of RAM, reasonably fast 
> with 1.5GB or more. Processor speed and disk speed don't seem to be 
> anywhere near as important.

I guess we all have different views on "slow".  I have been using it to 
develop a full IDE in Eclipse for over 2 years (since 2.1), and I can't 
understand where you guys are coming from.  I self-host (run a 
development Eclipse SDK, plus a Runtime - that's 2 Eclipse's 
running...sometimes 3) all day every day and it does admittedly get 
"slow", but only down when I am doing serious debugging (Eclipse 
debugging the internals of Eclipse).

I only have 512MB RAM, and a wimpy 1.3 GHz Athlon on Windows.  And BTW - 
if you used Eclipse seriously, you would know that Mac and Linux are 
inherently slower than Windows due to the SWT GUI library lagging 
performance-wise on those platforms (especially GTK on Linux), so I have 
no idea how you can resonably say that you would *never* recommend a 
Windows programmer to try Eclipse.  Those types of performance claims 
are simply not true (beyond a 10 minute evaluation), and it's just plain 
silly to say Eclipse is not usable on Windows.

My opinion - If you aren't willing to try something new, or have an 
aversion to it in the first place, nothing we can say will change your 
mind.  As for me - I'll continue to enjoy the benefits of Eclipse's 
tools - especially with PyDev coming along the way it is.

The ultimate would be for something like Jython or JPype to come to 
fruition so Eclipse plugins could be written in Python.  Now *that* 
would be something.  Actually, the *ultimate* would be to implement the 
equivalent of Eclipse in Python, but that is a pipe dream... ;o)

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


Re: Slicing every element of a list

2005-07-12 Thread John Machin
Alex Dempsey wrote:
> Recently I tried to slice every element of a list of strings. First I tried:

"slice"? Interesting terminology. Next problem you have, try posting an 
example of your input, and your expected output.

E.g.
repr(input_string): '"foo"\t"barre"\t"zot"\t"X"\n'
repr(output_list): ['foo', 'barre', 'zot']

Then folk would be able to point out that the csv module might be a tad 
more appropriate -- it gives you a general solution.

I'm presuming that the hard-coded number "5" means that you have a 
single-character field at the end that you want to ignore. Your code 
would be more robust in the face of change were you to ignore the last 
field after splitting instead of the last 5 chars before splitting.

> 
> f = open("export.xls", "r")


Aarrgghh!! a file with a ".xls" extension that you can read as a text file??


> lines = f.readlines()
> 
> for line in lines:
> line = line[1:-5]
> line = line.split('\"\t\"')
> 
> This went without returning any errors, but nothing was sliced or
> split. Next I tried:
> 
> for i in range(len(lines)):
> lines[i] = lines[i][1:-5]
> lines[i] = lines[i].split('\"\t\"')
> 
> This of course worked, but why didn't the first one work. Further why
> didn't the first one return an error?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating anonymous functions using eval

2005-07-12 Thread Joseph Garvin
Robert Kern wrote:

>Not everyone is reading this list in a conveniently threaded 
>form
>  
>
Why not? Just about every modern newsgroup reader and e-mail app has a 
threaded view option.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Inconsistency in hex()

2005-07-12 Thread Bengt Richter
On Tue, 12 Jul 2005 21:17:07 +1000, Steven D'Aprano <[EMAIL PROTECTED]> wrote:

>hex() of an int appears to return lowercase hex digits, and hex() of a
>long uppercase.
>
 hex(75)
>'0x4b'
 hex(75*256**4)
>'0x4BL'
>
>By accident or design? Apart from the aesthetic value that lowercase hex
>digits are ugly, should we care?
>
>It would also be nice if that trailing L would disappear.
>
 >>> '%010X'% 0x12345678
 '0012345678'
 >>> '%010X'% 75
 '4B'
 >>> '%010X'% (75*256**4)
 '4B'
 >>> '%010X'% (-75*256**4)
 '-4B'
 >>> '%010X'% (-75)
 '-0004B'

I've ranted about the lack of a natural format for showing
the hex of a canonical twos-complement representation of a negative number,
but I guess I'll let it go with this mention ;-)

BTW, yeah, I know it's not so hard to write
 >>> '%010X'% (-75 &0xff)
 'B5'
 >>> '%010X'% (-75*256**4 &0xff)
 'B5'
or a helper or a str subclass that does __mod__ differently but that's not with 
the batteries ;-/
Regards,
Bengt Richter
-- 
http://mail.python.org/mailman/listinfo/python-list


new division in Pythonwin

2005-07-12 Thread Jive Dadson
I like to use Pythonwin as my desktop calculator.  It's bothersome
to have to type in "from __future__ import division" into the
interactive window every time I open it.  I've tried various ways to
attempt to get it to import new division at startup, but no luck.  I
can get it to execute a file, but apparently it does so in an
environment that gets discarded, rather than in the environment of
the interactive window.  So the "from __future__" statement has no
effect.

Any suggestions?

Thankee.


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


How to match literal backslashes read from a text file using regular expressions?

2005-07-12 Thread cricfan
I'm parsing a text file to extract word definitions. For example the
input text file contains the following content:

di.va.gate \'di_--v*-.ga_-t\ vb
pas.sim \'pas-*m\ adv : here and there : THROUGHOUT

I am trying to obtain words between two literal backslashes (\ .. \). I
am not able to  match words between two literal backslashes using the
regxp - re.compile(r'\\[^\\]*\\').

Here is my sample script:

import re;

#slashPattern = re.compile(re.escape(r'\\[^\\]*\\'));
pattern = r'\\[^\\]*\\'
slashPattern = re.compile(pattern);

fdr = file( "parseinput",'r');
line = fdr.readline();

while (line != ""):
if (slashPattern.match(line)):
print  line.rstrip()  + " <-- matches pattern " + pattern
else:
print  line.rstrip()  + " <-- DOES not match pattern " +
pattern
line = fdr.readline();
print;


--
The output

C:\home\krishna\lang\python>python wsparsetest.py
python wsparsetest.py
di.va.gate \'di_--v*-.ga_-t\ vb <-- DOES not match
pattern \\[^\\]*\\
pas.sim \'pas-*m\ adv : here and there : THROUGHOUT <-- DOES not match
pattern \\[^\\]*\\
---

What should I be doing to match those literal backslashes? 

Thanks

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


Re: Earthquake Forecasting Program July 11, 2005

2005-07-12 Thread edgrsprj
"Hank Oredson" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> "edgrsprj" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > "edgrsprj" <[EMAIL PROTECTED]> wrote in message
> > news:[EMAIL PROTECTED]
> >> PROPOSED EARTHQUAKE FORECASTING
> >> COMPUTER PROGRAM DEVELOPMENT EFFORT

> What observational data are used?
> What are the sources of that observational data?
> How are those sources accessed?
> Is there a database to hold historical plus current data?
> If so, is it centralized or distributed?
>
> The project might be of interest if the data sources are
> rich enough, complete enough, and current enough.
>

July 12, 2005

Thanks for the response and questions.

The following are my personal opinions on this.

   Briefly, the amount of data available for this type of effort is
virtually limitless.  And more of those data than most people could even
deal with can be obtained for free.

You don't have to build a new laboratory filled with expensive equipment.

A reasonably powerful computer,
Access to the Internet,
At least some knowledge of science
Some computer programming ability
And a little imagination

Are all that are required.

   Researchers have been attempting to do this type of work for probably
as far back as we have historical records.  The reason that previous efforts
that I am aware of have not been successful is because two key discoveries
needed to be made.  They are referred to on my 90-05.html Web page as the
"Gravity Point" and "Earthquake Triggering Symmetry."  Now that those
discoveries have been made the door should be open to tremendously rapid
advances in our understanding of how and why earthquakes occur and how to
forecast them.

   Much of this research could be easily done by computer programmers.
You don't need to be a geophysicist.  If the data you are generating look
statistically significant then they are probably important whether or not
you actually understand the geophysical theories behind them.

   To actually forecast earthquakes using the procedure I have developed
you need both warning signal data and earthquake data along with some ocean
tide and Solid Earth Tide data.  But one of the really great parts of this
particular research project is the fact that many of the basic discoveries
can be made by simply comparing earthquakes with one another.  You don't
need any warning signal data at all.  And there is certainly no shortage of
earthquake data!

   At my Web site there is a discussion of a concept called "Earthquake
Pairs."  They are two or more earthquakes which were apparently triggered in
the same manner.  My data indicate that the two highly destructive 1998
earthquakes in Afghanistan would represent an Earthquake Pair.  And the two
highly destructive 1999 earthquakes in Turkey would represent another pair.
Important discoveries can be made by determining what the similarities are
between the two or more earthquakes in an Earthquake Pair and how they
differ from other earthquakes.  And since the group of earthquake warning
signals that I am presently working with is being controlled by the same
forces that are responsible for earthquake triggering, significant
discoveries regarding earthquake triggering processes could be immediately
applied to forecasting efforts.

   One of the reasons that geologists have not yet taken an interest in
this particular effort could be because it is heavily reliant on celestial
mechanics.  And most geology researchers appear to me to prefer to focus on
measuring forces within the ground.  I presently suspect that astronomers
would be a more likely group to take an interest in this science at first.
And I am planning to contact some of them about that.


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


Re: Frankenstring

2005-07-12 Thread Scott David Daniels
Thomas Lotze wrote:
> Hi,
> I think I need an iterator over a string of characters pulling them out
> one by one, like a usual iterator over a str does. At the same time the
> thing should allow seeking and telling like a file-like object:
> 
> 
f = frankenstring("0123456789")
for c in f:
> 
> ... print c
> ... if c == "2":
> ... break
> ... 
OK, frankenstring can be approximated by nothing:
for c in "0123456789":
 print c


Now if you want to do it for a file, you could do:

 for c in thefile.read():
 

Or, if you did not want to do a full read:
 def filechars(afile):
 for line in afile:
 for char in line:
 yield char

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


Re: Searching through a list of tuples

2005-07-12 Thread Alan Green
Peter Otten wrote:
> Repton wrote:
>
> > I often find myself storing data in a list of tuples, and I want to ask
> > questions like "what is the index of the first tuple whose 3rd element
> > is x", or "give me the first tuple whose 2nd element is y".

> >>> items = [(1, "a", 10), (2, "b", 20), (3, "c", 30)]
> >>> class Key(object):
> ... def __init__(self, key):
> ... self.key = key
> ... def __eq__(self, other):
> ... return self.key(other)
> ...
> >>> items.index(Key(lambda x: x[2] == 20))
> 1

Neat solution.

I'd add an extra kind of Key, since finding tuples where a given
position is equal to a given value seems to be the common case:

>>> class EqualKey(Key):
... def __init__(self, pos, val):
... super(EqualKey, self).__init__(lambda x: x[pos] == val)
... 
>>> items.index(EqualKey(2, 20))
1

Alan

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


Re: python parser

2005-07-12 Thread Bengt Richter
On Tue, 12 Jul 2005 13:30:14 -0700, Robert Kern <[EMAIL PROTECTED]> wrote:

>tuxlover wrote:
>> Hello everyone
>> 
>> I have to write a verilog parser in python for a class project. I was
>> wondering if all you folks could advise me on choosing the right python
>> parser module. I am not comfortable with lex/yacc and as a result find
>> myself strugging with any module which use lex/yacc syntax/philosophy.
>> pyparser looks good to me, but before I dive into it, I would really
>> appreciate feedback from members of this group
>
>A Verilog parser has been written using pyparsing at least once before, 
>so I imagine that it shouldn't be too difficult to do so again. Of 
>course, if you just need *a* Verilog parser, not necessarily one written 
>by you, you could just email the guy who wrote it and ask him for a 
>copy. Grep
>
>   http://pyparsing.sourceforge.net/
>
>for "Verilog".
>
or google for
verilog site:sourceforge.net

BTW googling for
verilog site:pyparsing.sourceforge.net
will only get one hit (maybe less if I typoed again ;-)

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


Re: Frankenstring

2005-07-12 Thread Thomas Lotze
jay graves wrote:

> see StringIO or cStringIO in the standard library.

Just as with files, iterating over them returns whole lines, which is
unfortunately not what I want.

-- 
Thomas



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


Re: Frankenstring

2005-07-12 Thread Bengt Richter
On Tue, 12 Jul 2005 22:08:55 +0200, Thomas Lotze <[EMAIL PROTECTED]> wrote:

>Hi,
>
>I think I need an iterator over a string of characters pulling them out
>one by one, like a usual iterator over a str does. At the same time the
>thing should allow seeking and telling like a file-like object:
>
 f = frankenstring("0123456789")
 for c in f:
>... print c
>... if c == "2":
>... break
>... 
>0
>1
>2
 f.tell()
>3L
 f.seek(7)
 for c in f:
>... print c
>... 
>7
>8
>9

>
>It's definitely no help that file-like objects are iterable; I do want
>to get a character, not a complete line, at a time.
>
>I can think of more than one clumsy way to implement the desired
>behaviour in Python; I'd rather like to know whether there's an
>implementation somewhere that does it fast. (Yes, it's me and speed
>considerations again; this is for a tokenizer at the core of a library,
>and I'd really like it to be fast.) I don't think there's anything like
>it in the standard library, at least not anything that would be obvious
>to me.
>
>I don't care whether this is more of a string iterator with seeking and
>telling, or a file-like object with a single-character iterator; as long
>as it does both efficiently, I'm happy.
>
>I'd even consider writing such a beast in C, albeit more as a learning
>exercise than as a worthwhile measure to speed up some code.
>
>Thanks for any hints.
>
I'd probably subclass file to buffer in good-sized chunks and override the
iteration to go by characters through the buffer, updating the buffer
when you get to its end, and overriding seek and tell to do the right thing
re the buffer and where you are in it for the character iteration via next.

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


Re: Replacing last comma in 'C1, C2, C3' with 'and' so that it reads'C1, C2 and C3'

2005-07-12 Thread Alan Green
Ric Da Force wrote:
> Hi guys,
>
> Thank you all for your input!  It was good to see so much convergence in the
> approach!

Just for divergence, you can also do this with regular expressions:

>>> import re
>>> re.sub("(.*),(.*)", r"\1 and\2", "C1, C2, C3")
'C1, C2 and C3'

Alan.

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


Re: Fwd: Should I use "if" or "try" (as a matter of speed)?

2005-07-12 Thread Roy Smith
Christopher Subich  <[EMAIL PROTECTED]> wrote:
>try:
>f = file('file_here')
>do_setup_code
>do_stuff_with(f)
>except IOError: # File doesn't exist
>error_handle

It's also a good idea to keep try blocks as small as possible, so you
know exactly where the error happened.  Imagine if do_setup_code or
do_stuff_with(f) unexpectedly threw an IOError for some reason totally
unrelated to the file not existing.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Inconsistency in hex()

2005-07-12 Thread Terry Reedy

"Steven D'Aprano" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> hex() of an int appears to return lowercase hex digits, and hex() of a
> long uppercase.

Already bug-reported and fixed for 2.5 (to use lowercase, I believe).
http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=1224347
 



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


Re: Web client, https and session management

2005-07-12 Thread Yannick Turgeon
Well... Thanks for that! The work is almost completed now! ;o)

Yannick

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


Re: Replacing last comma in 'C1, C2, C3' with 'and' so that it reads'C1, C2 and C3'

2005-07-12 Thread Bengt Richter
On Wed, 13 Jul 2005 03:47:07 +0800, "Ric Da Force" <[EMAIL PROTECTED]> wrote:

>Hi guys,
>
>Thank you all for your input!  It was good to see so much convergence in the 
>approach! Again, I think that it speaks loudly for the concise way of doing 
>thins in Python... Anyway, I have typed in all of the solutions and have 
>gained a great understanding of how to do this in future.
>
>Thanks again!
>
>Ric
>"Brian van den Broek" <[EMAIL PROTECTED]> wrote in message 
>news:[EMAIL PROTECTED]
>> Ric Da Force said unto the world upon 12/07/2005 02:43:
>>> Hi,
>>>
>>> I have a string such as 'C1, C2, C3'.   Without assuming that each bit of 
>>> text is of fixed size, what is the easiest way to change this list so 
>>> that it reads:
>>> 'C1, C2 and C3' regardless of the length of the string.
>>>
>>> Regards and sorry for the newbie question,
>>>
>>> Ric
>>
>> Hi Ric,
>>
>> the rsplit method of strings should get you going:
>>
>> >>> data = "the first bit, then the second, finally the third"
>> >>> chunks = data.rsplit(',', 1)
>> >>> chunks
>> ['the first bit, then the second', ' finally the third']
>> >>>
>>
>> Best,
>>
>> Brian vdB
>>
Or, to finish Brian's solution by inserting the ", and" in place of the "," :

 >>> data = "the first bit, then the second, finally the third"
 >>> ', and'.join(data.rsplit(',',1))
 'the first bit, then the second, and finally the third'
 >>> ', and'.join('C1, C2, C3'.rsplit(',',1))
 'C1, C2, and C3'

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


Re: Frankenstring

2005-07-12 Thread Mike C. Fletcher
Thomas Lotze wrote:

>Hi,
>
>I think I need an iterator over a string of characters pulling them out
>one by one, like a usual iterator over a str does. At the same time the
>thing should allow seeking and telling like a file-like object:
>  
>
Okay, first off, this is never going to be *fast* compared to something
coded in C and wrapped with Python.  You are dealing with every single
character as a Python object, so let's forget fast for the moment and do
a straightforward implementation:

class Franken( str ):
frankenIndex = 0
def __iter__( self ):
while self.frankenIndex < len(self):
yield self[ self.frankenIndex ]
self.frankenIndex += 1
self.frankenIndex = 0
def seek( self, index ):
self.frankenIndex = index
def tell( self, index ):
return self.frankenIndex

if __name__ == "__main__":
f = Franken( 'abcdefg' )
for c in f:
print 'char', c
if c == 'c':
break
f.seek( 5 )
l1 = list( f )
l2 = list( f )
assert l1 == [ 'f','g' ]
assert l2 == list(str(f))
print 'first list', l1
print 'second list', l2

If you want to speed it up, you can optimise for various string sizes
(eg using a slice of the string and the built-in iterator when
appropriate), but in the end, it's not going to be anywhere near as fast
as a C-engine tokeniser, so I'd personally spend more time on elegance
than on speed...

Anywho, have fun,
Mike

-- 

  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://www.vrplumber.com
  http://blog.vrplumber.com

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


Re: Creating anonymous functions using eval

2005-07-12 Thread Devan L
> You missed Steven's point which is to quote the message to which you are
> replying. Not everyone is reading this list in a conveniently threaded
> form, so you need to provide some context for them to be able to follow
> along.

Ah, sorry, I didn't quite get what he was referring to.

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


Re: python parser

2005-07-12 Thread Robert Kern
tuxlover wrote:
> Hello everyone
> 
> I have to write a verilog parser in python for a class project. I was
> wondering if all you folks could advise me on choosing the right python
> parser module. I am not comfortable with lex/yacc and as a result find
> myself strugging with any module which use lex/yacc syntax/philosophy.
> pyparser looks good to me, but before I dive into it, I would really
> appreciate feedback from members of this group

A Verilog parser has been written using pyparsing at least once before, 
so I imagine that it shouldn't be too difficult to do so again. Of 
course, if you just need *a* Verilog parser, not necessarily one written 
by you, you could just email the guy who wrote it and ask him for a 
copy. Grep

   http://pyparsing.sourceforge.net/

for "Verilog".

-- 
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: Trying to come to grips with static methods

2005-07-12 Thread Terry Reedy
| I've been doing a lot of reading about static methods in Python,

and possibly getting over-confused by the minutia of the CPython
implementation, as well as by the misnomer.  Conceptually, a 'static 
method'
is a function attribute of a class that is to be used as a function and not
as a method (where 'methods', in Python, get semi-magic first parameters).
Note that function attributes of instances are also just functions, and not
methods (which sometimes fools people), as are function attributes of
modules.

| not exactly sure what they are useful for or why they were introduced.

Completeness (as RK said), occasional real usefulness, and for C++&Java
programmers.  Python did fine without them.

| As near as I can see it, static methods are object methods that act just
| like functions.

Almost: class function/method, depending on your meaning of method.  See
above.

|Er. I always thought that object methods *were* functions,
| except they had some runtime magic that passed the object itself as the
| first argument.

Substitute class for object and class or instance for object itself and you
have it.  The runtime magic is a minor abbreviation but its major purpose 
is
inheritance.

Now you can more on to something more useful like metaclasses or decorators
;-).

Terry J. Reedy



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


Re: automatically assigning names to indexes

2005-07-12 Thread Devan L
import math
class Vector:
def __init__(self, coordinates):
self.coordinates = coordinates
self.magnitude = sum([c**2 for c in coordinates])**0.5
self.direction = getangle(Vector([1]+[0 for i in
range(len(coordinates)-1)]))
def dotproduct(self, vector):
sum([a*b for a,b in zip(self.coordinates,vector.coordinates)])
def crossproduct(self, vector, pvector):
return
pvector*self.magnitude*vector.magnitude*math.sin(self.getangle(vector))
def getangle(self, vector):
return
math.acos(self.dotproduct(vector)/(self.magnitude*vector.magnitude))
def __mul__(self, scalar):
return Vector([c*scalar for c in self.coordinates])
def __add__(self, vector):
return Vector([c+d for c,d in
zip(self.coordinates,vector.coordinates)])
def __sub__(self, vector):
return Vector([c-d for c,d in
zip(self.coordinates,vector.coordinates)])

What about this?

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


Re: Slicing every element of a list

2005-07-12 Thread Thomas Lotze
Alex Dempsey wrote:

> for line in lines:
> line = line[1:-5]
> line = line.split('\"\t\"')
> 
> This went without returning any errors, but nothing was sliced or split.
> Next I tried:
> 
> for i in range(len(lines)):
> lines[i] = lines[i][1:-5]
> lines[i] = lines[i].split('\"\t\"')
> 
> This of course worked, but why didn't the first one work.

Because when assigning to line the second time, you just make the
identifier reference a new object, you don't touch the list. This is how
one might do it without ranging over the length of the list and having
to get the lines out by element access:

for i, line in enumerate(lines):
line = line[1:-5]
lines[i] = line.split('\"\t\"')

Probably there are even better ways, this is just off the top of my
head.

> Further why
> didn't the first one return an error?

Because you didn't make any. You just discarded your results; why should
anyone stop you from burning cycles? *g

-- 
Thomas


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


Re: Fwd: Should I use "if" or "try" (as a matter of speed)?

2005-07-12 Thread Thomas Lotze
Christopher Subich wrote:

> try:
> f=file('file_here')
> except IOError: #File doesn't exist
> error_handle
> error_flag = 1
> if not error_flag:
> do_setup_code
> do_stuff_with(f)
> 
> which nests on weird, arbitrary error flags, and doesn't seem like good
> programming to me.

Neither does it to me. What about

try:
f=file('file_here')
except IOError: #File doesn't exist
error_handle
else:
do_setup_code
do_stuff_with(f)

(Not that I'd want to defend Joel's article, mind you...)

-- 
Thomas

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


plot module

2005-07-12 Thread Shankar Iyer ([EMAIL PROTECTED])
Hi,

I am looking for documentation on the plot module.  Does anyone know where I 
can find this information?  Thanks.

Shankar

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


Re: Creating anonymous functions using eval

2005-07-12 Thread Robert Kern
Devan L wrote:
> Well, the string that gets passed is more or less a function
> definition, which is then called with exec. I don't see why you'd need
> to write a string out with the function definition and then call it.
> You could just write the function.
> 
> As for the nested functions, I had been presuming that it was intended
> to use as a better function than lambda within a function. Sorry for
> the confusion.

You missed Steven's point which is to quote the message to which you are 
replying. Not everyone is reading this list in a conveniently threaded 
form, so you need to provide some context for them to be able to follow 
along.

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

2005-07-12 Thread jay graves
see StringIO or cStringIO in the standard library.

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


Re: python parser

2005-07-12 Thread matt
I recently was successful using pyparsing after messing around with ply
for a few hours.  See my blog for more details (
http://panela.blog-city.com/icfp_contest_implementation_in_python_notes.htm
).

I personally corresponded with the author and he was very helpful as
well, giving my useful critiques and feedback.  The next time I'm
parsing something more complex than a tab-delimited file (excluding xml
:)) I'll probably use pyparsing.  I found it very pythonic and easy to
use.

good luck parsing...
matt

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


Re: Environment Variable

2005-07-12 Thread Sybren Stuvel
tuxlover enlightened us with:
> No, the replies from Grant's and Sybren's do answer my question.

It would be a lot more polite to actually thank the people helping
you.

Sybren
-- 
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself? 
 Frank Zappa
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slicing every element of a list

2005-07-12 Thread Scott David Daniels
Alex Dempsey wrote:
> Recently I tried to slice every element of a list of strings. First I tried:
> 
> f = open("export.xls", "r")
> lines = f.readlines()
> 
> for line in lines:
> line = line[1:-5]
> line = line.split('\"\t\"')
> 
> This went without returning any errors, but nothing was sliced or
> split. Next I tried:
> 
> for i in range(len(lines)):
> lines[i] = lines[i][1:-5]
> lines[i] = lines[i].split('\"\t\"')
> 
> This of course worked, but why didn't the first one work. Further why
> didn't the first one return an error?

result = [line[1 : -5].split('\"\t\"') for line in lines]

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


Re: Searching through a list of tuples

2005-07-12 Thread Scott David Daniels
Peter Otten wrote:
> Repton wrote:
> 
>>I often find myself storing data in a list of tuples, and I want to ask
>>questions like "what is the index of the first tuple whose 3rd element
>>is x", 
 iter(n for n, elem in enumerate(lst) if elem[3] == x).next()

>>or "give me the first tuple whose 2nd element is y".
 iter(elem in lst if elem[3] == x).next()

Does this look any better?  At least it stops when the answer is found.

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


Frankenstring

2005-07-12 Thread Thomas Lotze
Hi,

I think I need an iterator over a string of characters pulling them out
one by one, like a usual iterator over a str does. At the same time the
thing should allow seeking and telling like a file-like object:

>>> f = frankenstring("0123456789")
>>> for c in f:
... print c
... if c == "2":
... break
... 
0
1
2
>>> f.tell()
3L
>>> f.seek(7)
>>> for c in f:
... print c
... 
7
8
9
>>>

It's definitely no help that file-like objects are iterable; I do want
to get a character, not a complete line, at a time.

I can think of more than one clumsy way to implement the desired
behaviour in Python; I'd rather like to know whether there's an
implementation somewhere that does it fast. (Yes, it's me and speed
considerations again; this is for a tokenizer at the core of a library,
and I'd really like it to be fast.) I don't think there's anything like
it in the standard library, at least not anything that would be obvious
to me.

I don't care whether this is more of a string iterator with seeking and
telling, or a file-like object with a single-character iterator; as long
as it does both efficiently, I'm happy.

I'd even consider writing such a beast in C, albeit more as a learning
exercise than as a worthwhile measure to speed up some code.

Thanks for any hints.

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


Re: Slicing every element of a list

2005-07-12 Thread Christopher Subich
Gary Herron wrote:
> Alex Dempsey wrote:
>> for line in lines:
>>line = line[1:-5]
>>line = line.split('\"\t\"')
> This, in fact, did do the operation you expected, but after creating the 
> new value and assigning it to line, you promptly threw it away. (Because 
> the loop then went back to the top and (re)assigned the next thing in 
> lines to line wiping out your nicely sliced computation in lines.)  You 
> need to *do* something with the value in line before you end the loop -- 
> but what?

As an intermediate tip, the entire loop can be written as a single list 
comprehension:

stuff = [li[1:-5].split('"\t"') for li in lines]

(You don't need to escape single quotes inside double-quoted strings, 
and vice versa.)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python parser

2005-07-12 Thread Christopher Subich
tuxlover wrote:
> I have to write a verilog parser in python for a class project. I was
> wondering if all you folks could advise me on choosing the right python
> parser module. I am not comfortable with lex/yacc and as a result find
> myself strugging with any module which use lex/yacc syntax/philosophy.
> pyparser looks good to me, but before I dive into it, I would really
> appreciate feedback from members of this group

I've had good luck with DParser for Python 
(http://staff.washington.edu/sabbey/dy_parser/index.html); in fact, it 
might even be a very easy translation from a premade Verilog grammar to 
a DParser grammar (Google search if you don't have BNF for Verilog already).

Two caevats come to mind, though; documentation isn't as newbie-friendly 
as it could be, and DParser requires a binary library -- it's not 
Python-only, which might matter for your project.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Replacing last comma in 'C1, C2, C3' with 'and' so that it reads'C1, C2 and C3'

2005-07-12 Thread Ric Da Force
Hi guys,

Thank you all for your input!  It was good to see so much convergence in the 
approach! Again, I think that it speaks loudly for the concise way of doing 
thins in Python... Anyway, I have typed in all of the solutions and have 
gained a great understanding of how to do this in future.

Thanks again!

Ric
"Brian van den Broek" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Ric Da Force said unto the world upon 12/07/2005 02:43:
>> Hi,
>>
>> I have a string such as 'C1, C2, C3'.   Without assuming that each bit of 
>> text is of fixed size, what is the easiest way to change this list so 
>> that it reads:
>> 'C1, C2 and C3' regardless of the length of the string.
>>
>> Regards and sorry for the newbie question,
>>
>> Ric
>
> Hi Ric,
>
> the rsplit method of strings should get you going:
>
> >>> data = "the first bit, then the second, finally the third"
> >>> chunks = data.rsplit(',', 1)
> >>> chunks
> ['the first bit, then the second', ' finally the third']
> >>>
>
> Best,
>
> Brian vdB
>
> 


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


Re: Fwd: Should I use "if" or "try" (as a matter of speed)?

2005-07-12 Thread Christopher Subich
Dark Cowherd wrote:
> But one advise that he gives which I think is of great value and is
> good practice is
> "Always catch any possible exception that might be thrown by a library
> I'm using on the same line as it is thrown and deal with it
> immediately."

That's fine advice, except for when it's not.  Consider the following code:

try:
f = file('file_here')
do_setup_code
do_stuff_with(f)
except IOError: # File doesn't exist
error_handle

To me, this code seems very logical and straightfoward, yet it doesn't 
catch the exception on the very next line following its generation.  It 
relies on the behavior of the rest of the try-block being skipped -- the 
"implicit goto" that Joel seems to loathe.  If we had to catch it on the 
same line, the only alternative that comes to mind is:
try: f=file('file_here')
except IOError: #File doesn't exist
error_handle
error_flag = 1
if not error_flag:
do_setup_code
do_stuff_with(f)

which nests on weird, arbitrary error flags, and doesn't seem like good 
programming to me.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Minor correction July 11, 2005

2005-07-12 Thread Hank Oredson
"edgrsprj" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> "edgrsprj" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
>> PROPOSED EARTHQUAKE FORECASTING
>> COMPUTER PROGRAM DEVELOPMENT EFFORT
>
>> it jumps strait to the display routine and uses the entered command to
> begin
>
> Should be the word "straight" instead of strait.


What observational data are used?
What are the sources of that observational data?
How are those sources accessed?
Is there a database to hold historical plus current data?
If so, is it centralized or distributed?

The project might be of interest if the data sources are
rich enough, complete enough, and current enough.

-- 

  ... Hank

http://home.earthlink.net/~horedson
http://home.earthlink.net/~w0rli


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


Re: Slicing every element of a list

2005-07-12 Thread Gary Herron
Alex Dempsey wrote:

>Recently I tried to slice every element of a list of strings. First I tried:
>
>f = open("export.xls", "r")
>lines = f.readlines()
>
>for line in lines:
>line = line[1:-5]
>line = line.split('\"\t\"')
>  
>
This, in fact, did do the operation you expected, but after creating the 
new value and assigning it to line, you promptly threw it away. (Because 
the loop then went back to the top and (re)assigned the next thing in 
lines to line wiping out your nicely sliced computation in lines.)  You 
need to *do* something with the value in line before you end the loop -- 
but what?

>This went without returning any errors, but nothing was sliced or
>split. Next I tried:
>
>for i in range(len(lines)):
>lines[i] = lines[i][1:-5]
>lines[i] = lines[i].split('\"\t\"')
>
>This of course worked, but why didn't the first one work. Further why
>didn't the first one return an error?
>  
>
Dr. Gary Herron
Digipen Institute of Technology


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


Re: Web client, https and session management

2005-07-12 Thread Gregory Piñero
How's this for an answer, it even uses yahoo in the example!

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

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


Slicing every element of a list

2005-07-12 Thread Alex Dempsey
Recently I tried to slice every element of a list of strings. First I tried:

f = open("export.xls", "r")
lines = f.readlines()

for line in lines:
line = line[1:-5]
line = line.split('\"\t\"')

This went without returning any errors, but nothing was sliced or
split. Next I tried:

for i in range(len(lines)):
lines[i] = lines[i][1:-5]
lines[i] = lines[i].split('\"\t\"')

This of course worked, but why didn't the first one work. Further why
didn't the first one return an error?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: automatically assigning names to indexes

2005-07-12 Thread George Sakkis
<[EMAIL PROTECTED]> wrote

> > And what should happen for vectors of size != 3 ? I don't think that a
> > general purpose vector class should allow it; a Vector3D subclass would
> > be more natural for this.
>
> That's the 'magic' good idea I'm looking for. I think a unified Vector
> class for all size vectors is a worthy goal!

What 'magic' ? The (x,y,z) notation is used only for 3D vectors. (x,y)
is also common for 2D and perhaps (t,x,y,z) for 4D, with t for time.
There are only 26 letters (or 52 if you allow capitals), so I don't see
how this can be generalized _usefully_ to arbitrary number of
dimensions.

George

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


Re: Tricky Dictionary Question from newbie

2005-07-12 Thread Bengt Richter
On Tue, 12 Jul 2005 11:52:41 -0400, Tim Peters <[EMAIL PROTECTED]> wrote:

>[Peter Hansen]
>...
>> I suppose I shouldn't blame setdefault() itself for being poorly named,
>
>No, you should blame Guido for that .
>
>> but it's confusing to me each time I see it in the above, because the
>> name doesn't emphasize that the value is being returned, and yet that
>> fact is arguably more important than the fact that a default is set!
>>
>> I can't think of a better name, though, although I might find "foo" less
>> confusing in the above context. :-)
>
>I wanted to call it getorset() -- so much so that even now I sometimes
>still type that instead!  The "get" part reminds me that it's fetching
>a value, same as dict.get(key, default) -- "or set"'ing it too if
>there's not already a value to "get".  If you have a fancy enough
>editor, you can teach it to replace setdefault by getorset whenever
>you type the former ;-)

But it isn't get OR set, it's
set_default_if_no_value_then_either_way_effectively_get ;-)
Regards,
Bengt Richter
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Efficiency of using long integers to hold bitmaps

2005-07-12 Thread Jack Diederich
On Wed, Jul 13, 2005 at 03:24:48AM +1000, Jeff Melvaine wrote:
> Bengt,
> 
> Thanks for your informative reply, further comments interleaved.
> 
> "Bengt Richter" <[EMAIL PROTECTED]> wrote in message 
> news:[EMAIL PROTECTED]
> > On Mon, 11 Jul 2005 02:37:21 +1000, "Jeff Melvaine" 
> > <[EMAIL PROTECTED]> wrote:
> >
> >>I note that I can write expressions like "1 << 100" and the result is 
> >>stored
> >>as a long integer, which means it is stored as an integer of arbitrary
> >>length.  I may need to use a large number of these, and am interested to
> >>know whether the storage efficiency of long integers is in danger of
> >>breaking my code if I use too many.  Would I do better to write a class 
> >>that
> >>defines bitwise operations on arrays of integers, each integer being 
> >>assumed
> >>to contain at most 32 bits?  I cannot find information in the Python 
> >>manuals
> >>for 2.4.1 that would allow me to resolve this question; perhaps the
> >>intention is that programmers should not rely on implementation details.
> >>
> >>Thanks in advance,
> >>
> > Sounds like a possible^H^H^H^H^H^H^H^Hprobable premature optimization 
> > worry ;-)
> >
> I'm writing a Sudoku solver of generic order.  The object is not to make it 
> half a millisecond faster than the guy next door's solver, but I'd like it 
> to be able to do a bit more than just the daily newspaper puzzle, e.g. 
> search for uniquely solvable puzzles with minimal numbers of clues. 
> NP-completeness will put a lid on things sooner or later, but I'd like to 
> get as far as possible before that happens.
> 

I would recommend making a hand-written C extension that does the heavy
lifting - but only in the tiny corner you need it to.  I've done this for
the ICFP[1] competition the last few years.  It is a time-limited competition
so the priorities are getting a pure-python program up and running to 
understand the problem and then making the slow parts go really fast so
you can try as many full games as possible to try out as many new strategies
as possible.

Typically this means just the "game board" representation is done in C.
You'll want the heuristics to be done in python in order to try out variations
easily.  For Sudoku the board implementation will likely have C functions
for copy(), valid() (raise ValueError), and something to return a list
of obviously legal values for a coordinate.  Passing coord tuples in and
list & dictionaries out has worked well for me (easy to use in the python
part of the program).

I keep C modules out of production code unless absolutely necessary, but
I have no qualms about using it in toy/hobby problems, especially because
the C code stays at a manageable few hundred lines for toy problems.
If you aren't much of a C guy check out pyrex[2].  In my darker days I did
C++ for a living so I much prefer writing modules by hand; python makes it
easy to do and it is faster, less buggy, and easier to debug.

-jackdied

[1] http://performancedrivers.com/icfp2002/
http://performancedrivers.com/icfp2004/
(other years I botched it badly enough I didn't make a webpage)
http://performancedrivers.com/icfp2002/icfp_module.c
http://performancedrivers.com/icfp2002/icfpBoard.c

[2] http://nz.cosc.canterbury.ac.nz/~greg/python/Pyrex/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Efficiency of using long integers to hold bitmaps

2005-07-12 Thread Bengt Richter
On Wed, 13 Jul 2005 03:24:48 +1000, "Jeff Melvaine" <[EMAIL PROTECTED]> wrote:

>Bengt,
>
>Thanks for your informative reply, further comments interleaved.

Can't reply fully now, but just had the thought that maybe some ideas
from 8-queens solvers might be useful or interesting. There is an old thread at


http://groups-beta.google.com/group/comp.lang.python/browse_frm/thread/f88f301b7578705a

that explores various ways of solving it, and uses various representations of 
the board,
including integer bit maps at the end, which turned out fastest IIRC. I'm sure 
it can still
be improved upon, and I'm not sure it will be worth your while to dig into it, 
unless you
think the problem fun, but there it is.

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


Re: removing list comprehensions in Python 3.0

2005-07-12 Thread Steven Bethard
Steven Bethard wrote:
> py> def ge(items):
> ... return (item for item in items if item)
> ...

Bengt Richter wrote:
>  >>> dis.dis(ge)
>2   0 LOAD_CONST   1 ( expression> at 02EE4FA0, file "", line 2>)
>3 MAKE_FUNCTION0
>6 LOAD_FAST0 (items)
>9 GET_ITER
>   10 CALL_FUNCTION1
>   13 RETURN_VALUE
[snip]
>  >>> dis.dis(ge.func_code.co_consts[1])
>2   0 SETUP_LOOP  28 (to 31)
>3 LOAD_FAST0 ([outmost-iterable])
>  >>6 FOR_ITER21 (to 30)
>9 STORE_FAST   1 (item)
>   12 LOAD_FAST1 (item)
>   15 JUMP_IF_FALSE8 (to 26)
>   18 POP_TOP
>   19 LOAD_FAST1 (item)
>   22 YIELD_VALUE
>   23 JUMP_ABSOLUTE6
>  >>   26 POP_TOP
>   27 JUMP_ABSOLUTE6
>  >>   30 POP_BLOCK
>  >>   31 LOAD_CONST   0 (None)
>   34 RETURN_VALUE

Outstanding.  Thanks a lot!  For comparison, here's the relevant dis.dis 
output for list comprehensions.

py> def lc(items):
... return [item for item in items if item]
...
py> dis.dis(lc)
   2   0 BUILD_LIST   0
   3 DUP_TOP
   4 STORE_FAST   1 (_[1])
   7 LOAD_FAST0 (items)
  10 GET_ITER
 >>   11 FOR_ITER24 (to 38)
  14 STORE_FAST   2 (item)
  17 LOAD_FAST2 (item)
  20 JUMP_IF_FALSE   11 (to 34)
  23 POP_TOP
  24 LOAD_FAST1 (_[1])
  27 LOAD_FAST2 (item)
  30 LIST_APPEND
  31 JUMP_ABSOLUTE   11
 >>   34 POP_TOP
  35 JUMP_ABSOLUTE   11
 >>   38 DELETE_FAST  1 (_[1])
  41 RETURN_VALUE

Interestingly, the LC code and the code of a GE's "generator-expression" 
code object look quite similar, with basically a LOAD_FAST/LIST_APPEND 
replaced by a YIELD_VALUE.

But I don't know byte code well enough to guess how the dangling local 
variable in LCs will be eliminated in Python 3.0 (as has been suggested 
a number of times).  One way to eliminate it would be (as suggested) to 
make LCs syntactic sugar for list().  But it also looks like it 
might be possible to do a DELETE_FAST with an appropriately hidden name...

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


python parser

2005-07-12 Thread tuxlover
Hello everyone

I have to write a verilog parser in python for a class project. I was
wondering if all you folks could advise me on choosing the right python
parser module. I am not comfortable with lex/yacc and as a result find
myself strugging with any module which use lex/yacc syntax/philosophy.
pyparser looks good to me, but before I dive into it, I would really
appreciate feedback from members of this group

Thanks
Tuxlover

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


Re: Inconsistency in hex()

2005-07-12 Thread Dan Bishop
Steven D'Aprano wrote:
> hex() of an int appears to return lowercase hex digits, and hex() of a
> long uppercase.
>
> >>> hex(75)
> '0x4b'
> >>> hex(75*256**4)
> '0x4BL'
>
> By accident or design? Apart from the aesthetic value that lowercase hex
> digits are ugly, should we care?
>
> It would also be nice if that trailing L would disappear.

Yes, but that can easily be worked around.

>>> def hex(x):
..."""Return the hexadecimal representation of an integer."""
...return __builtins__.hex(x).upper().rstrip('L')
...
>>> hex(75)
'0X4B'
>>> hex(75 * 256**4)
'0X4B'

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


Re: pywin32 com server "cash" question

2005-07-12 Thread Philippe C. Martin
Sorry: "Cache", not "Cash"



Philippe C. Martin wrote:

> Hi,
> 
> I just got the pywin32 "hello world" COM server to install and I did
> manage to use it from VB 6.0.
> 
> However, there are some glitches I do not comprehend:
> 
> 1) at one point I got a python runtime error telling me the
> "testcomserver" was not found - I got rid of that problem by deleteting
> the appropriate (I hope) registry and reregistering the server
> 
> 2) If I change the source of the server
> from:
> return "Hello" + 
> to:
> return "Hello my friend " + 
> 
> 
> I still get "Hello BINGO" only in my VB message box. this even if I try to
> re-register (which does not seem necessary) the server.
> 
> 
> Q.1) What could that error have meant ?
> Q.2) is there a way to "flush" whatever so my new code "Hello my friend"
> shows up ?
> 
> 
> 
> 
> The VB looks like this:
> 
> Private Sub Command1_Click()
>   Dim shdemo = CreateObject("Python.TestServer")
>   MsgBox (shdemo.Hello("BINGO"))
> End Sub
> 
> The Python server :
> 
> class HelloWorld:
> 
> _reg_clsid_ = "{7CC9F362-486D-11D1-BB48-E838A65F}"
> _reg_desc_ = "Python Test COM Server"
> _reg_progid_ = "Python.TestServer"
> _public_methods_ = ['Hello']
> _public_attrs_ = ['softspace', 'noCalls']
> _readonly_attrs_ = ['noCalls']
> #_reg_class_spec_ = "testcomserver.HelloWorld"
> 
> def __init__(self):
> 
> self.softspace = 1
> 
> self.noCalls = 0
> 
> def Hello(self, who):
> 
> self.noCalls = self.noCalls + 1
> 
> # insert "softspace" number of spaces
> 
> return "Hello My Friend " + " " * self.softspace + str(who)
> 
> if __name__=='__main__':
> # ni only for 1.4!
> import  win32com.server.register
> win32com.server.register.UseCommandLine(HelloWorld)
> import pythoncom
> print pythoncom.CreateGuid()
> 
> 
> 
> Regards,
> 
> Philippe

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


pywin32 com server "cash" question

2005-07-12 Thread Philippe C. Martin
Hi,

I just got the pywin32 "hello world" COM server to install and I did manage
to use it from VB 6.0.

However, there are some glitches I do not comprehend:

1) at one point I got a python runtime error telling me the "testcomserver"
was not found - I got rid of that problem by deleteting the appropriate (I
hope) registry and reregistering the server

2) If I change the source of the server 
from:
return "Hello" + 
to:
return "Hello my friend " + 


I still get "Hello BINGO" only in my VB message box. this even if I try to
re-register (which does not seem necessary) the server.


Q.1) What could that error have meant ?
Q.2) is there a way to "flush" whatever so my new code "Hello my friend"
shows up ?




The VB looks like this:

Private Sub Command1_Click()
  Dim shdemo = CreateObject("Python.TestServer")
  MsgBox (shdemo.Hello("BINGO"))
End Sub

The Python server :

class HelloWorld:

_reg_clsid_ = "{7CC9F362-486D-11D1-BB48-E838A65F}"
_reg_desc_ = "Python Test COM Server"
_reg_progid_ = "Python.TestServer"
_public_methods_ = ['Hello']
_public_attrs_ = ['softspace', 'noCalls']
_readonly_attrs_ = ['noCalls']
#_reg_class_spec_ = "testcomserver.HelloWorld"

def __init__(self):

self.softspace = 1

self.noCalls = 0

def Hello(self, who):

self.noCalls = self.noCalls + 1

# insert "softspace" number of spaces

return "Hello My Friend " + " " * self.softspace + str(who)

if __name__=='__main__':
# ni only for 1.4!
import  win32com.server.register 
win32com.server.register.UseCommandLine(HelloWorld)
import pythoncom
print pythoncom.CreateGuid()



Regards,

Philippe







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


Re: Environment Variable

2005-07-12 Thread tuxlover
No, the replies from Grant's and Sybren's do answer my question. I
posted twice because my browser locked itself up, and I ended up typing
twice :(

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


Re: Help with inverted dictionary

2005-07-12 Thread rorley
Thanks for the hints, I think I've figured it out.  I've only been
using Python for 2 days so I really needed the direction.  If you were
curious, this is not homework but an attempt to use the ConceptNet data
(its an MIT AI project) to make a website in a Wiki-like format that
would allow the data to be edited on the fly.  I'll ask again if I need
more help.  You guys are great.  Reece

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


Re: Efficiency of using long integers to hold bitmaps

2005-07-12 Thread Jeff Melvaine
Bengt,

Thanks for your informative reply, further comments interleaved.

"Bengt Richter" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> On Mon, 11 Jul 2005 02:37:21 +1000, "Jeff Melvaine" 
> <[EMAIL PROTECTED]> wrote:
>
>>I note that I can write expressions like "1 << 100" and the result is 
>>stored
>>as a long integer, which means it is stored as an integer of arbitrary
>>length.  I may need to use a large number of these, and am interested to
>>know whether the storage efficiency of long integers is in danger of
>>breaking my code if I use too many.  Would I do better to write a class 
>>that
>>defines bitwise operations on arrays of integers, each integer being 
>>assumed
>>to contain at most 32 bits?  I cannot find information in the Python 
>>manuals
>>for 2.4.1 that would allow me to resolve this question; perhaps the
>>intention is that programmers should not rely on implementation details.
>>
>>Thanks in advance,
>>
> Sounds like a possible^H^H^H^H^H^H^H^Hprobable premature optimization 
> worry ;-)
>
I'm writing a Sudoku solver of generic order.  The object is not to make it 
half a millisecond faster than the guy next door's solver, but I'd like it 
to be able to do a bit more than just the daily newspaper puzzle, e.g. 
search for uniquely solvable puzzles with minimal numbers of clues. 
NP-completeness will put a lid on things sooner or later, but I'd like to 
get as far as possible before that happens.

Why in Python?  All my recent professional experience is in writing Ada, 
which is not my idea of a rapid prototyping language (or a rapid deliverable 
item handover language either, for that matter :).

Why do I want it to be efficient during debugging, rather than after fine 
tuning?  I take your point, but in a sense the ability to handle large 
problems is part of the proof of concept.

> What is a "large number of these" going to amount to? How many, tops?
> And how many bits in each? How many operations between them? (Since 
> integers
> are immutable, operations mean allocation of space for new ones for 
> results
> and disposing of unused garbage ones (probably back to a special fast pool 
> for
> integers and longs)). Are you interested in a speed/memory tradeoff?
>
The algorithms that interest me most do not involve cyclic data structures, 
so I am trusting in built-in reference counts to avoid memory leaks.  At the 
moment I'm expecting to use bitmaps of constant size (81 for order 3, or 256 
for order 4) for the most numerous data items, so fragmentation should not 
be excessive.

Space was my first thought, but I also expect that the parallelism of 
bitwise operations will be reasonably time-efficient.  I would hope to be 
able to do operations more quickly on a bitmap of n bits than on a list or 
array of n integer variables with values constrained to 0 or 1.  However the 
prospect of writing "a & b" and getting multiword functionality that could 
prove the concepts was rather appealing too; almost executable pseudocode.

The effectiveness of the algorithms will determine how much time and space I 
use, but for NP-complete problems the ceiling is always too low, and one is 
constantly learning new ways to duck.

> If your bit vectors are extremely large and sparse (have only a few bits 
> "on"),
> you might consider sets (import sets and help(sets)) of the bit numbers as 
> representations.
>
> BTW, I wonder if anyone has written an ordered bit set class in C yet. I 
> was tempted ;-)
>
For symbolic Boolean algebra on systems of 729 or 4096 variables, sparse is 
the way to go, but I would want ordered sets too.  I've already implemented 
a Boolean algebra system using Python lists (oops, thank you again Raymond) 
of 32-bit bitmaps, and the calculations it does are not dazzlingly fast.  My 
question about using long integers had a different approach in mind.

> How much memory do you have? Buying more can be a pretty cheap way of 
> solving space worries
> if you are getting paid for your time.
>
512Mb.  The only time I've hit the limit so far was when I got distracted 
enough to leave out the escape condition in a small recursive function. 
Death was surprisingly rapid.

> You should be able to subclass int or long as a way of writing your 
> program in terms of
> your own bit vector class. Then you can change your mind and change the 
> underlying representation
> without changing the code that uses the api. Compared to plain longs it 
> will cost you some speed
> to keep converting results to your own type though.
>
I like to encapsulate, but I hadn't thought of that one.  Yes, it's an 
approach to getting the best of both worlds; instant performance or 
flexibility.  The thought of executable pseudocode that I translate into 
something better if necessary is not too bad for now.

> Bottom line, whether your code will "break" due to storage problems or be 
> fast enough
> will depend on numbers you didn't provide ;-)
>
Re netiquette (with thanks to other posters

Re: Help with inverted dictionary

2005-07-12 Thread rorley
Not quite homework but a special project.  Thanks for the advice.  I'll
let you know if I run into anymore stumbling blocks.  Reece

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


Re: Help with inverted dictionary

2005-07-12 Thread Dark Cowherd
As Steven said this looks too much like home work
But what the heck I am also learning python. So I wrote a small
program. A very small program. I am fairly new to Python, I am stunned
each time to see how small programs like this can be.

Since I am also learning can somebody comment if anything here is not
Pythonesque.

dictwords = dict()
for line in open('testfile.txt','r'):
   for word in line.rstrip('\n').split():
   dictwords.setdefault(word,set()).update((line.rstrip('\n'),))
for wordfound in dictwords.items():
   open(wordfound[0],'w').write('\n'.join(wordfound[1]))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Web App like Google

2005-07-12 Thread Graham Fawcett
In translating natural language to SQL, be sure you're not introducing
opportunities for SQL injection attacks. Code like

   sql = 'SELECT %s FROM %s' % (this, that)

is considered dangerous, because a well-crafted value for "that" can be
used to, e.g., delete rows from your tables, run system commands, etc.
You can save a lot of worry by using a database account with read-only
privileges, but you still have to be careful. My advice is to read up
on "sql injection" before going too public with your code.

Graham

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


Re: Why does reply to messages on this list put the sender in the To

2005-07-12 Thread Rocco Moretti
Peter Decker wrote:
> On 7/12/05, Dark Cowherd <[EMAIL PROTECTED]> wrote:
> 
> 
>>Most lists when i hit reply it puts the list address back in the To
>>address and some lists allow you to configure this.
>>
>>But in this list reply sends the mail back as a private mail and there
>>seems to be no option to configure this.
>>
> In cases where people are discussing problems and supplying solutions,
> replying to the list is essential so that as many people as possible
> can benefit from the knowledge contained in the reply. Private replies
> only benefit the lone recipient, while list replies benefit everyone
> on the list and everyone who later searches the archives.

There have been some q&a lists I've been on where the sole content of 
the list is people posting questions. Questions rarely get a response 
on-list. It makes the list practically worthless.  To top it off, the 
archive of the mailing list only lists the questions, but never the 
(private) answers. It makes Google a pain to use, as you get hits to 
people asking the same question you want, but never the answers.

Sorry, had to vent.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: automatic form filling

2005-07-12 Thread ulrice jardin
Thanks A LOT for your help!

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Inconsistency in hex()

2005-07-12 Thread [EMAIL PROTECTED]


Steven D'Aprano wrote:
> hex() of an int appears to return lowercase hex digits, and hex() of a
> long uppercase.
>
> >>> hex(75)
> '0x4b'
> >>> hex(75*256**4)
> '0x4BL'
>
> By accident or design? Apart from the aesthetic value that lowercase hex
> digits are ugly, should we care?

No, just use GMPY.

>>> from gmpy import *
>>> print digits(75,16)
0x4b
>>> print digits(75*256**4,16)
0x4b

Although it would have been nice to also get rid of the octal
hex prefix inconsistencies.

>>> for b in range(2,37):
print digits(75*256**4,b)

1001011
1010210110021101212101010
1023
20234201333002300
403551524040520
32162332321446
045400 <-- base 8 starts with leading 0
1123407355333
322122547200
11467a864463
5251a233140
244b718a01c
1183b2a2b96
85a4947a50
0x4b   <-- base 16 starts with leading 0x
2c304c17g7
1b42e32gac
ii6ie79hd
cbd35i800
8ahh6hjk6
5j31lfeje
42dma7a93
2m5e7gl80
22ja8i0d0
1e2je99kc
13lc7ana3
noclhnpk
ijflp0nb
elq24la0
blth91bl
9c00
7idw8no3
64hn2ago
5083wbdk
43zbg45c



>
> It would also be nice if that trailing L would disappear.
> 
> -- 
> Steven.

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


Re: Creating anonymous functions using eval

2005-07-12 Thread Devan L
Well, the string that gets passed is more or less a function
definition, which is then called with exec. I don't see why you'd need
to write a string out with the function definition and then call it.
You could just write the function.

As for the nested functions, I had been presuming that it was intended
to use as a better function than lambda within a function. Sorry for
the confusion.

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


  1   2   3   >