Re: using target words from arrays in regex, pythons version of perls 'map'

2006-05-15 Thread Paul McGuire
"Dennis Lee Bieber" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> On Mon, 15 May 2006 19:41:39 -0500, Lance Hoffmeyer
> <[EMAIL PROTECTED]> declaimed the following in comp.lang.python:
> > I would have something similar to perl above:
> >
> >
> > targets = ['OVERALL RATING',
> >'CLOTHING', ITEMS',
> >'ACCESSORIES',
> >'SHOES',
> >'FINE JEWELRY']
> >
> > PROPOSED CODE:
> >
> > match2 = re.search(targets[i].*?(?:(\d{1,3}\.\d)\s+){3} ', file2);m[i] =
match2.group(1)
> >
> >
> I don't do regex's, and I also don't find packing multiple statements on
one line attractive.

I concur - this kind of multiple-statements-per-line-ishness feels
gratuitous.  Yes, I know they line up nicely when all 6 statements are
printed out together, but the repetition of "mNN = match2.group(1) ;print
mNN" should tell you that this might be better done with a for loop.  DRY.

>
> However... Why don't you basically do what you say you do in
> Python... Substitute you targets into the expression while inside a
> loop...
>
> targets = [ "OVERALL RATING",
> "CLOTHING",
> "ITEMS",
> "ACCESSORIES",
> "SHOES",
> "FINE JEWELRY" ]
>
> results = []
> for t in target:
> m2 = re.search("%s.*?(?:(\d{1,3}\.\d)\s+){3}" % t, file2)
> results.append(m2.group(1))
> -- 


# by contrast, here is a reasonably Pythonic one-liner, if one-liner it must
be
results = [ re.search(r"%s.*?(?:(\d{1,3}\.\d)\s+){3}" % t, file2).group(1)
for t in targets ]

# or for improved readability (sometimes 2 lines are better than 1):
reSearchFunc = lamdba tt,ff : re.search(tt + r".*?(?:(\d{1,3}\.\d)\s+){3}",
ff).group(1)
results = [ reSearchFunc(t,file2) for t in targets ]


Resisting-the-urge-to-plug-pyparsing-ly yours,
-- Paul


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


Using python for a CAD program

2006-05-15 Thread 63q2o4i02
Hi, I'm interested in using python to start writing a CAD program for
electrical design.  I just got done reading Steven Rubin's book, I've
used "real" EDA tools, and I have an MSEE, so I know what I *want* at
the end of this; I just have never taken on a programming task of this
magnitude.  I've seen that some are using python as a utility language
for existing CAD environments, and I've also found some guy who's
writing a 2d drafting tool with python, which is cool.  I've been
playing with python recently and so far have a SPICE parser
half-written (it'll do production rules, but no syntax trees yet...)
I'm wondering if the python experts can provide some opinion on
python's efficiency/utility in the following areas, and perhaps where
some things will need to be in C++ and/or pyrex:

1.  Databases.  I don't mean sql type database, but design databases,
which contain all the components for any given design, including
schematics, layout, simulation parameters, etc.  I'm not concerned
about python's ability to do fancy data structures, but I'm wondering
how it'll go in terms of efficiency when things get really big.  If the
alternative is home-brewed C++ linked-lists, attributes, arrays, binary
trees, memory management, etc., it looks like python's ability to do
all this neatly without all the bugs waiting to happen if I did this
myself are well worth the efficiency loss that may result from its
uber-generality.

2.  GUI.  Yes, I know you can do guis with qt, gtk, tkinter, etc.  I'm
talking of fancy guis that do alpha blending, animations, nice
shading/gradients, etc. in a quick, smooth, and slick way, such that
moving a scroll bar or jiggling the mouse yields fast game-like
response time, and which gives this program the feeling that you're
actually in the 21st century... ie this is an *interactive*
environment, and I f***king hate crass-hatching, so real colors is a
must.  Can this be portable between linux and windows?  Is it possible
to "do the whole thing" in opengl, even the 2d stuff?  I guess I dont
know enough about guis here.  My fear is that my app will turn into
x-hell if I try to do more than 8-bit colors in linux, with flashing
background windows and complaints from the server about bit planes.
Maybe I need to play with linux a bit more from the development side
before commenting on this, since I get the feeling I'm working on old
information here...

3.  Computational stuff.  I imagine the really heavy computing needs to
be done in c++, especially if I'm stupid enough to write my own
simulator, which my brain seems to want to do (I'm imagining a small
homer simpson brain with a mind of its own telling me what to do...).
But what about other things like rules and constraints about layout,
routing, compaction, undo/redo, etc?  These aren't really
computationally expensive, I don't think, but do munge about quite a
bit with the database.  Is it likely I'll write something faster in
C++?  (answer: no).

4.  Programmability.  I imagine this is a slam-dunk, relatively
speaking, to have a python interpreter as part-and-parcel of the design
system.  But how do I customize the command window to do custom things
like adding special shortcuts (eg '?' brings up help, or help on
commands a la Matlab), or making an API into the CAD program?  I'm not
sure conceptually how to merge an embedded python interpreter into the
rest of the CAD program in an intelligent way, so some stuff is
exposed, but not all, and which interacts dynamically with the
graphical portion so that, for instance, context-sensitive help is
available in the console window, or so you can access information about
the currently selected object, or so you can do stuff from the command
line that is based on your current graphical context.

5.  Threads and parallelism.  One of the things about this is that I'd
like there to be some real-time simulator running so when you change
the values of parts, the output changes, and I'd like to be able to
take advantage of mulitple processors, or dual-core, or whatever.  I've
seen fake multi-threading in python (read about it, but I haven't done
it), but that doesn't really use any extra cycles from a separate
processor.

So why am I thinking about this?  I've used a few  "real" CAD systems,
and from using them, it's obvious the modus is to kludge shit  together
over 20 years and screw the user experience.  So basically I'd like to
take over the world with my nifty new python-based cad system because
it's so beautiful and fun to use, and so flexible that my time to
market for any given new idea is 5x shorter than the existing tools.

Any comments on the above from people who've actually written stuff
would be greatly appreciated! :)

thanks
ms

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


Re: Multiple inheritance : waht does this error mean ?

2006-05-15 Thread Peter Otten
[EMAIL PROTECTED] wrote:

> I am using Python 2.4.3
> 
class K(object,list):
>...: pass
>...:
> 
> Traceback (most recent call last):
>   File "", line 1, in ?
> TypeError: Error when calling the metaclass bases
> Cannot create a consistent method resolution
> order (MRO) for bases object, list

class K(object, list): pass

specifies that a method should be looked up first in object, then in list.
As list inherits from object, its method resolution order 

>>> list.__mro__
(, )

specifies that a method should be looked up first in list, then in object.
Python refuses to resolve the conflict for you, but you can do it manually:

>>> class K2(list, object): pass
...
>>> K2.__mro__
(, , )

Now K2, list, and object can agree to look for a method in K2, then list,
then object. However, you get the same effect by just inheriting from list:

>>> class K3(list): pass
...
>>> K3.__mro__
(, , )

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


Re: How to guess the language of a given textstring?

2006-05-15 Thread Roman

Hi

This is what I'm looking for.
Thank you.

Roman


gene tani schrieb:

> Roman wrote:
> > Does anybody know an easy way (or tool) to guess the language of a
> > given text string?
> >
>
>
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/355807
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/326576

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


Re: using target words from arrays in regex, pythons version of perls 'map'

2006-05-15 Thread John Machin
Would you believe "steps 3 & 4"?

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


Re: Large Dictionaries

2006-05-15 Thread Maric Michaud
Le Lundi 15 Mai 2006 21:07, Diez B. Roggisch a écrit :
> > d={}.fromkeys(xrange(5*10**6)) ?
>
> That is a totally different beast. You don't want to insert arbitrary
> keys, you want the internal hash-array to be of the right size.

But you can replace the xrange part by any generator function you want.

def get_mykeys(..)
...
yield key

I just the wonder if the time consuming part is the memory allocation of hash 
table (key by key) or the hash operation itself.

I don't see a use case where a python programmer should need a 
dictionnary "that will be probably big" but can't predict what keys will be 
in.

-- 
_

Maric Michaud
_

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using target words from arrays in regex, pythons version of perls 'map'

2006-05-15 Thread John Machin
Think about how well the above solutions scale as len(targets)
increases.

1. Make "targets" a set, not a list.
2. Have *ONE* regex which includes a bracketed match for a generic
target e.g. ([A-Z\s]+)
3. Do *ONE* regex search, not 1 per target
4. If successful, check if the bracketed gizmoid is in the set of
targets.

It's not 100% apparent whether it is possible that there can be more
than one target in the inappropriately named file2 (it is a string,
isn't it?). If so, then  write your own findall-like loop wrapped
around steps 2 & 3 above. Compile the regex in advance.

HTH,
John

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


Re: Why does stack.inspect take so long?

2006-05-15 Thread 63q2o4i02

Tim Peters wrote:
> [EMAIL PROTECTED]
> > Hi, I've written a top-down recursive decent parser for SPICE circuit
> > descriptions.  For debugging purposes, I wanted each production
...
> >  Any clues?
>

> It should go much faster to use a function that doesn't crawl the
> entire call stack.  For example,
>
> >>> import sys, inspect
> >>> def name_of_caller():
> ... return inspect.getframeinfo(sys._getframe(1), context=0)[2]
> >>> def f():
> ... print "my name is", name_of_caller()
> >>> f()
> my name is f
>
> name_of_caller() takes time independent of the call-stack depth.
>
> The "context=0" is to avoid wasting time sucking up and packaging
> source-code lines you don't want anyway.


Cool, thanks.  I'll try that.   I didn't realize there were other ways
of getting it.

ms

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


Re: using target words from arrays in regex, pythons version of perls 'map'

2006-05-15 Thread Paddy
I don't like string interpolation within REs, it pops me out of 'RE
mode' as I scan the line.

Maybe creating a dict of matchobjects could  be used in the larger
context?:
  dict( [(t, re.search(t+regexp_tail, file2) for t in targets] )

(untested).

- Pad.

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


Re: Decrypt DES by password

2006-05-15 Thread Thomas Dybdahl Ahle
Den Mon, 15 May 2006 11:32:47 -0700. skrev Paul Rubin:
> Thomas Dybdahl Ahle <[EMAIL PROTECTED]> writes:
>> byte[] array2 = bytes1.CryptDeriveKey("DES", "MD5", 0, array1);
>> > Anybody know how to do this in python?
> 
> I'm not aware of a standard that says how CryptDeriveKey is supposed
> to work.  Or rather, there are multiple possible standard ways to do
> it.  If you can find an exact specification, or C# source code that
> does it, it will probably be straightforward to reimplement in Python.

I tried to find out how the monofolks did it, but also they didn't know
the algorithms. Maybe I should find a windows computer with .net, create
the key and base64 encode it, so that I could take it to the python
program...

But thanks for the excample anyways. Maybe I can use it, if I need to
encrypt something myself, another time.

-- 
Programmers should realize their critical importance and responsibility in a
world gone digital. They are in many ways similar to the priests and monks of
Europe's Dark Ages; they are the only ones with the training and insight
to read and interpret the "scripture" of this age.

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


Re: Aggregate funuctions broken in MySQLdb?

2006-05-15 Thread Lorenzo
In article <[EMAIL PROTECTED]>,
 Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:

> On Mon, 15 May 2006 20:14:29 GMT, John Salerno
> <[EMAIL PROTECTED]> declaimed the following in comp.lang.python:
> 
> > Lorenzo Thurman wrote:
> > > Thanks, that was my problem. Can you point me to some documentation on 
> > > MySQLdb? I've been googling to get answers and that obviously has not 
> > > been working.
> > 
> > I've been looking into this too lately, and finding thorough docs for it 
> > is hard. Have you seen these yet: 
> > http://sourceforge.net/docman/?group_id=22307
> 
>   For the most part, it follows the DB-API 2 specifications. The
> subject of this thread (aggregates) would have occurred with ANY db-api
> compliant adapter, even plain ODBC -- since it was a misunderstanding
> that .execute() returns the status code (typically # of records
> affected by the query), and .fetchnnn() is needed to obtain the data
> values. This misunderstanding is not specific to use of aggregates as
> any "select..." statement functions this way.
> 
>   Most divergences from the db-api specifications should be
> determinable by looking at the sources of the python portion of the
> adapter; or by looking at the features of the underlying RDBM.

Thanks, you are correct. I have done similar database things using PHP 
and Perl to connect to databases, and I felt like DUH, when I got my 
first reply, but there are times when one cannot see the forest for the 
trees, so to speak. Better docs can help.

-- 
"My Break-Dancing days are over, but there's always the Funky Chicken"
--The Full Monty
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Aggregate funuctions broken in MySQLdb?

2006-05-15 Thread Lorenzo
In article <[EMAIL PROTECTED]>,
 John Salerno <[EMAIL PROTECTED]> wrote:

> http://sourceforge.net/docman/?group_id=22307

Yes, I did, but I did not find them thorough enough.

-- 
"My Break-Dancing days are over, but there's always the Funky Chicken"
--The Full Monty
-- 
http://mail.python.org/mailman/listinfo/python-list


Far from complete

2006-05-15 Thread Kay Schluehr
Section 2.3 of the Python 2.5. tutorial

"The following sections describe the standard types that are built into
the interpreter. Historically, Python's built-in types have differed
from user-defined types because it was not possible to use the built-in
types as the basis for object-oriented inheritance. With the 2.2
release this situation has started to change, although the intended
unification of user-defined and built-in types is as yet far from
complete. "

"Far from complete"? Have I missed something?

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


Re: Multiple inheritance : waht does this error mean ?

2006-05-15 Thread John Machin
Question for you: what do you think that "class K(object,list):"
means?? What are you trying to achieve? What do you plan to do with K?
If you want it to be a subclass of list, all you have to do is "class
K(list):".

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


Argument Decorators Enhancement?

2006-05-15 Thread birchb
Guido has proposed a syntax for type annotations in Python-3000.
Example:

def foo(x: t1, y: t2) -> t3:
...body...


http://www.artima.com/weblogs/viewpost.jsp?thread=87182

The types are dynamic and there is significant execution of code prior
to the function body being called. Which tends to make the above closer
to a decorator than a static type declaration. That being so, it
occurred to me that perhaps an argument decorator would be more general
and perhaps less restrictive. The example using decorators:

def foo(@T1 x, @t2 y) @ t3:
...body...

The compiler would do something like this:

def foo__(x, y):  # original function
...body...

def foo(x, y):# wrapper function
x = T1.__decarg__(foo_, x)
y = t2.__decarg__(foo_, y)
r = foo__(x, y)
return t3.__decreturn___(foo__, r)

More examples:

def func(@between(1,400) x) @bool :   #  between checks input range

def func(@NotNone x):#  x must be a real object

def func(@long x) @coerce(float):   # coerce forces type conversion

def func(@sequence s, @function fn) @sequence: # more abstract types

To someone used to reading decorators, the @ character is a flag that
some kind of dynamic magic is being run. So IMHO using @ here too is
consistent with its existing usage.

Argument decorators would be an object (not a function) with methods
corresponding to their usage:

class T1(object):
 def __decarg__(self, function, arg):  # returns arg
 def __decreturn__(self, function, arg):  # returns arg

This will allow existing types to be used if they implement these
methods:

def bar(@int x, @float y) @float :
...body...

Because this syntax is just a decorator, you could use or abuse it as
you wished.

So you could define and use your own type-checking classes and
semantics:

def bar(@StrictlyInt x, @DuckCompatibleSequence seq) :
...body...

Since the decorator is an *object*, not a function, we could add other
methods for use by IDEs for intellisense etc.

Any views on this? Something to put forward to the python-3000 list
maybe?

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


Re: Multiple inheritance : waht does this error mean ?

2006-05-15 Thread Ben Finney
[EMAIL PROTECTED] writes:

> >>>class K(object,list):
>...: pass
>...:
> 
> Traceback (most recent call last):
>   File "", line 1, in ?
> TypeError: Error when calling the metaclass bases
> Cannot create a consistent method resolution
> order (MRO) for bases object, list

The class 'object' is already the base class of 'list', and of every
other basic type.

Python is saying that you need to decide what you want; inheritance
from 'object', or inheritance from something that already inherits
from 'object'.

-- 
 \   "People come up to me and say, 'Emo, do people really come up |
  `\ to you?'"  -- Emo Philips |
_o__)  |
Ben Finney

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


Multiple inheritance : waht does this error mean ?

2006-05-15 Thread jnair
I am using Python 2.4.3

>>>class K(object,list):
   ...: pass
   ...:

Traceback (most recent call last):
  File "", line 1, in ?
TypeError: Error when calling the metaclass bases
Cannot create a consistent method resolution
order (MRO) for bases object, list


Regards
Jitendra Nair

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


Re: C API: getting sys.argv

2006-05-15 Thread [EMAIL PROTECTED]

John Machin wrote:
> > PyObject *_argv = PyImport_ImportModule("sys.argv");
>
> What does the name of the function tell you? You can't do that in one
> hit. Start with
> PyObject *_sys = PyImport_ImportModule("sys");
> then you need to get the module's argv attribute.

I just figured this out, doh.  Thanks.

> However a better design might be have your extension module have an arg
> which can be *any* list of strings. That makes it more flexible, at the
> expense of making the caller pass sys.argv. But it would save you
> having to muck about with importing "sys", then plucking out the
> module's argv attribute.

but this is great advice.

Thanks!

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


Re: C API: getting sys.argv

2006-05-15 Thread John Machin
> PyObject *_argv = PyImport_ImportModule("sys.argv");

What does the name of the function tell you? You can't do that in one
hit. Start with
PyObject *_sys = PyImport_ImportModule("sys");
then you need to get the module's argv attribute.

However a better design might be have your extension module have an arg
which can be *any* list of strings. That makes it more flexible, at the
expense of making the caller pass sys.argv. But it would save you
having to muck about with importing "sys", then plucking out the
module's argv attribute.

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


Re: Large Dictionaries

2006-05-15 Thread Chris Foote
Aahz wrote:
> In article <[EMAIL PROTECTED]>,
> Roy Smith  <[EMAIL PROTECTED]> wrote:
>> In article <[EMAIL PROTECTED]>, Chris Foote <[EMAIL PROTECTED]> 
>> wrote:
>>> I have the need to store a large (10M) number of keys in a hash table,
>>> based on a tuple of (long_integer, integer).  The standard python
>>> dictionary works well for small numbers of keys, but starts to
>>> perform badly for me inserting roughly 5M keys:
>>>
>>> # keys   dictionary  metakit   (both using psyco)
>>> --   --  ---
>>> 1M8.8s 22.2s
>>> 2M   24.0s 43.7s
>>> 5M  115.3s105.4s
>> Are those clock times or CPU times?
> 
> And what are these times measuring?

The loading of a file into a dictionary.  i.e. no lookup operations.

 > Don't forget that adding keys
> requires resizing the dict, which is a moderately expensive operation.

Yep, that's why I probably need a dictionary where I can pre-specify
an approximate size at the time of its creation.

> Once the dict is constructed, lookup times should be quite good.

Very good indeed with Psyco!

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Large Dictionaries

2006-05-15 Thread Chris Foote
Roy Smith wrote:
> In article <[EMAIL PROTECTED]>, Chris Foote <[EMAIL PROTECTED]> 
> wrote:
> 
>> I have the need to store a large (10M) number of keys in a hash table,
>> based on a tuple of (long_integer, integer).  The standard python
>> dictionary works well for small numbers of keys, but starts to
>> perform badly for me inserting roughly 5M keys:
>>
>> # keys   dictionary  metakit   (both using psyco)
>> --   --  ---
>> 1M8.8s 22.2s
>> 2M   24.0s 43.7s
>> 5M  115.3s105.4s
> 
> Are those clock times or CPU times?

User + system CPU time.

> How much memory is your process using and how much is available on your 
> machine?

A very large amount of space is consumed, which is why I didn't give 
times for the 10M version which would have eaten my 1G of RAM and
into swap :-)

> I'm guessing a integer takes 4 bytes and a long integer takes roughly one 
> byte per two decimal digits.  Plus a few more bytes to bundle them up into 
> a tuple.  You've probably got something like 20 bytes per key, so 5M of 
> them is 100 meg just for the keys.
> 
> To get reasonable hashing performance, the hash table needs to be maybe 
> half full, and figure a hash key is 4 bytes, so that's another 40 meg for 
> the hash table itself.
> 
> Plus whatever the values stored in the dictionary take up.  Even if you're 
> not storing any values (i.e., there's probably 4 bytes for a null pointer 
> (or reference to None), so that's another 40 meg.
> 
> These are all vague guesses, based on what I think are probably 
> conservative estimates of what various objects must take up in memory, but 
> you see where this is going.  We're already up to 180 meg.

Yep, that size sounds about right (my dictionary values are all None).

> I wonder if the 
> whole problem is that you're just paging yourself to death.  A few minutes 
> watching your system memory performance with ps or top while your program 
> is running might give you some idea if this is the case.

Definitely not.

Thanks anyway,

Chris

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


Re: Why does stack.inspect take so long?

2006-05-15 Thread Tim Peters
[EMAIL PROTECTED]
> Hi, I've written a top-down recursive decent parser for SPICE circuit
> descriptions.  For debugging purposes, I wanted each production
> rule/function to know what its own name was, so at the beginning of
> each rule/function, I make a call to inspect.stack()[0][3] (I think...)
> and that fetches the name from someplace.  However, with a bunch of
> test text input, this takes ~10 seconds to run.  When I comment out the
> inspect.stack() function from each production/function, the program
> speeds up to 0.04s, or 250x !!  I loaded up the profiler to see what
> was there, and although I don't understand the output entirely, it
> looks like the stack frame functions are used in disproportionately
> large amounts compared to other often-used functions.  This is a
> *recursive* parser, so it's being called a lot even for simple things.
> I'm wondering still why there is a 250x speed up when I comment it out.
>  Any clues?

Look at the source code (inspect.py, function `stack()`).  It
obviously takes time proportional to the total depth of the call
stack, and for a recursive-descent parser that's likely to be highly
non-trivial most of the time.

It should go much faster to use a function that doesn't crawl the
entire call stack.  For example,

>>> import sys, inspect
>>> def name_of_caller():
... return inspect.getframeinfo(sys._getframe(1), context=0)[2]
>>> def f():
... print "my name is", name_of_caller()
>>> f()
my name is f

name_of_caller() takes time independent of the call-stack depth.

The "context=0" is to avoid wasting time sucking up and packaging
source-code lines you don't want anyway.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A critic of Guido's blog on Python's lambda

2006-05-15 Thread Rob Warnock
Ken Tilton  <[EMAIL PROTECTED]> wrote:
+---
| Having the reference implementation is the only thing that makes this 
| conceivably doable in a summer. What you are missing is something I have 
| often also gotten wrong: the core, cool functionality always comes easy 
| in the proof-of-concept stage. We make these ridiculous extrapolations 
| from that to a shipped product and come in five times over budget.
+---

Or as Fred Brooks said it in "The Mythical Man-Month" [paraphrased],
if a program takes one unit of effort, a programming *system* takes
three units of effort, and a programming systems *product* takes nine
units of effort.


-Rob

-
Rob Warnock <[EMAIL PROTECTED]>
627 26th Avenue http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607

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


C API: getting sys.argv

2006-05-15 Thread [EMAIL PROTECTED]
Hi,

How would one go about getting sys.argv fom within the context of a C
API extention module?  I want to pass it along to C library that I'm
calling from my module code.

TIA~

I'm trying to load the sys module using:

PyObject *_argv = PyImport_ImportModule("sys.argv");

but it is coming back NULL...

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


Why does stack.inspect take so long?

2006-05-15 Thread 63q2o4i02
Hi, I've written a top-down recursive decent parser for SPICE circuit
descriptions.  For debugging purposes, I wanted each production
rule/function to know what its own name was, so at the beginning of
each rule/function, I make a call to inspect.stack()[0][3] (I think...)
and that fetches the name from someplace.  However, with a bunch of
test text input, this takes ~10 seconds to run.  When I comment out the
inspect.stack() function from each production/function, the program
speeds up to 0.04s, or 250x !!  I loaded up the profiler to see what
was there, and although I don't understand the output entirely, it
looks like the stack frame functions are used in disproportionately
large amounts compared to other often-used functions.  This is a
*recursive* parser, so it's being called a lot even for simple things.
I'm wondering still why there is a 250x speed up when I comment it out.
 Any clues?

thanks
ms

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


Re: Python modules

2006-05-15 Thread Robert Kern
Shirley Li wrote:
> Dear Python experts,
> 
> I'm a beginner of Python programming.I have a question about Python
> modules.
> 
> With the following:
> =
> import sys, os, string, math
> from Numeric import *
> from Matrix import *
> =
> at the beginning of my Python script, it complained that "ImportError:
> No module named Numeric" when I run the script.
> 
> Could someone tell me what's wrong?

You didn't install the Numeric module or didn't install it correctly.

Also, please note that the Numeric module (and the Matrix module that comes with
it) have been replaced with the numpy package. Since you are just getting
started, please start with numpy.

  http://numeric.scipy.org

-- 
Robert Kern

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

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


Python modules

2006-05-15 Thread Shirley Li
Dear Python experts,  I'm a beginner of Python programming.    I have a question about Python modules.  With the following: = import sys, os, string, math from Numeric import * from Matrix import * = at the beginning of my Python script, it complained that "ImportError: No module named Numeric" when I run the script.  Could someone tell me what's wrong?  Any help/info will be greatly appreciated.  Shirley  
		Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2¢/min or less.-- 
http://mail.python.org/mailman/listinfo/python-list

using target words from arrays in regex, pythons version of perls 'map'

2006-05-15 Thread Lance Hoffmeyer
Hey all, in perl I was able to use the same regular expression multiple times 
changing one part of it
via a previously defined array and put the results into a new array

IN PERL:

my @targets = ('OVERALL RATING',
   'CLOTHING', '
ITEMS',
   'ACCESSORIES',
   'SHOES',
   'FINE JEWELRY');

my @JA13 = map {
$file2 =~/$_.*?(?:(\d{1,3}\.\d)\s+){3}/s;
} @targets;



So, in python instead of

match2 = re.search('OVEWRALL RATING.*?(?:(\d{1,3}\.\d)\s+){3} ', file2);m01 = 
match2.group(1) ;print m01
match2 = re.search('CLOTHING.*?(?:(\d{1,3}\.\d)\s+){3} ', file2);   m02 = 
match2.group(1) ;print m02
match2 = re.search('ITEMS.*?(?:(\d{1,3}\.\d)\s+){3} ', file2);  m03 = 
match2.group(1) ;print m03
match2 = re.search('ACCESSORIES.*?(?:(\d{1,3}\.\d)\s+){3} ', file2);m04 = 
match2.group(1) ;print m04
match2 = re.search('SHOES.*?(?:(\d{1,3}\.\d)\s+){3} ', file2);  m05 = 
match2.group(1) ;print m05
match2 = re.search('FINE JEWELRY.*?(?:(\d{1,3}\.\d)\s+){3} ', file2);   m06 = 
match2.group(1) ;print m06


I would have something similar to perl above:


targets = ['OVERALL RATING',
   'CLOTHING', ITEMS',
   'ACCESSORIES',
   'SHOES',
   'FINE JEWELRY']

PROPOSED CODE:

match2 = re.search(targets[i].*?(?:(\d{1,3}\.\d)\s+){3} ', file2);m[i] = 
match2.group(1)




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


Re: Large Dictionaries

2006-05-15 Thread lcaamano
Sounds like PyTables could be useful.

http://www.pytables.org

--
lpc

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


Re: Tabs versus Spaces in Source Code

2006-05-15 Thread achates
Harry George wrote:

> The reason is simple: People get confused, and accidentally get the
> wrong tab indents when they move among editors or among settings on
> the same editor.

People certainly do get confused. I'm always amazed that so many
people, even amongst those who manage to make a living out of writing
software, are unable to grasp the concept of a tab. But then a lot of
code is badly written, so maybe it figures.

A tab is not equivalent to a number of spaces. It is a character
signifying an indent, just like the newline character signifies the end
of a line. If your editor automatically converts tabs to spaces (i.e.
you are unable to create source files containing tabs) then either it's
broken or you have misconfigured it. Either way you probably shouldn't
be using it to write code.

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


Re: Large Dictionaries

2006-05-15 Thread John Machin
1. Why is two minutes to insert 5M keys "bad" for you? What would be
"good"? What would be good/bad look-up times? Have you measured the
typical look-up time? How often is the dict creation  required to be
done? How often does the data change? Is multi-user access required for
(a) look-up (b) updating? Have you considered loading the dict from a
pickle?
2. Assuming your code that is creating the dict looks in essence like
this:
adict = {}
for k, v in some_iterable:
adict[k] = v
then any non-linear behaviour can only be in the actual CPython
insertion code. Psyco can't help you there. Psyco *may* help with the
linear part, *if* you have enough memory. What are the corresponding
times without Psyco? In any case, if your code isn't (conceptually)
that simple, then try cutting away the cruft and measuring again.
3. Which version of Python? What OS? OK, psyco -> Intel x86, but what
chip exactly? How much free memory?
4. Consider printing time-so-far results, say every 100K keys. Multiple
step-ups might indicate dict resizings. A dog-leg probably means
running out of memory. Why "roughly" 5M keys???
5. How large are your long_integers?
6. What is the nature of the value associated with each key?
7. Have you experimented with key = a * 2 ** 32 + b instead of key =
(a, b)?

HTH,
John

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


Re: taking qoutes in arguments

2006-05-15 Thread Ben Finney
"Lee Caine" <[EMAIL PROTECTED]> writes:

> Im new to python and am in need of a little help

Possibly also new to command-line shells?

> Im attempting to write a program that finds and replaces text in all
> files in a given directory.
> example of running the program with arguments
> >python far.py /home/lee/Documents/ find replace

> I have managed to get it all working apart from one thing, if the
> find or replace arguments

> contain quotes e.g( content="somat" ),  it doesnt work,

When asking questions about behaviour, please describe the behaviour
and how it differs from your expectations. "It doesn't work" is
utterly useless as a description.

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

> so I print the string and it shows as content=somat, without the
> quotes.

This is because your command-line shell (which one are you using?)
needs to do its own work on the command you type before Python is even
asked.

For most command shells, the arguments for a command are separated by
spaces, and double-quotes can be used to tell the shell that spaces
are part of an argument.

$ ls foo bar# lists two files, "foo" and "bar"
$ ls "foo bar"  # lists one file, "foo bar"

The shell parses the command line and determines what the arguments
are, stripping the quotes after figuring out where the arguments begin
and end. The command receives each argument in an array element,
without the quotes.

Because of this, to get actual quote characters, you need to tell the
shell which quote characters are *not* to be interpreted this way,
usually with an escape character like the backslash.

> So i tryed this...
> >python far.py /home/lee/Documents content=\"somat\" content=\"somat else\"

Yep, just like that.

> and this works, but I would like a way of not having to do this when
> i run the program

Your shell may also understand single-quoted arguments, which remove a
lot of the special meanings for characters (like double quote
characters).

$ ls '"foo bar"'# lists one file, '"foo bar"'

Read the manual for your command shell, to understand better what
characters are special and how to get those characters literally in an
argument.

Hope that helps.

-- 
 \"My doctor told me to stop having intimate dinners for four. |
  `\Unless there are three other people."  -- Orson Welles |
_o__)  |
Ben Finney

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

Re: How to guess the language of a given textstring?

2006-05-15 Thread gene tani

Roman wrote:
> Does anybody know an easy way (or tool) to guess the language of a
> given text string?
>


http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/355807
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/326576

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


Re: How to guess the language of a given textstring?

2006-05-15 Thread Lonnie Princehouse
A search to see how many words from the text belong to
english/german/spanish common word dictionaries would be an easy way to
get a crude guess at the language.

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


Re: keyword help in Pythonwin interpreter

2006-05-15 Thread BartlebyScrivener
And others (including Fredrik Lundh) have found the same bug, it seems.

http://mail.python.org/pipermail/python-list/2005-September/297157.html

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


How to guess the language of a given textstring?

2006-05-15 Thread Roman
Does anybody know an easy way (or tool) to guess the language of a
given text string?

e.g.
Feeding in "This is an example."  --> should return "english" or ISO
code
Feeding in  "Das ist ein Beispiel." --> should return "german" or ISO
code
Feeding in "Esto es un ejemplo." --> should return "spanish" or ISO
code

I would prefer something more lightweight than using nltk/corpus/...

And it's ok if the success ratio is just about 90% or so.

Roman

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


Re: [PATCHES] [HACKERS] Iterating generator from C (PostgreSQL's pl/python RETUN

2006-05-15 Thread Hannu Krosing
Ühel kenal päeval, E, 2006-05-15 kell 17:21, kirjutas Tom Lane:
> Hannu Krosing <[EMAIL PROTECTED]> writes:
> >> Sven Suursoho wrote:
> >>> As for testing in actual pl/python build environment, we had objections 
> >>> from 
> >>> leading postgresql Tom Lane that even if we do test it at build time, 
> >>> a determined DBA may substitute a buggy python.so later and still crash 
> >>> her DB instance.
> 
> The above is a straw-man depiction of my point.

Sure ;)

> What I said was that just
> because python is up-to-date on the system where plpython is *compiled*
> does not mean it'll be up-to-date on the system where plpython is *used*.

Would running an external program at pl init time and testing for its
crash status be a good broken lib test for plpython ?

> With the increasing popularity of prebuilt packages (rpm, deb, etc),
> I think it's folly to use a build-time check for this.

I guess most packaging systems can require some minimal version of
dependencies. 

And in general newer versions are less buggy than old ones.

So i guess that some combination of build-time/run-time tests,
documentation and packager education should take care of 99% of
concerns ?

Hopefully we can just ignore the "determined DBA" failure mode and move
forward with including the patch. Or do you think that trying to hack in
the extra python frame to all/some builds to ward of potentially broken
libs would still be something to go for ?

-- 

Hannu Krosing
Database Architect
Skype Technologies OÜ
Akadeemia tee 21 F, Tallinn, 12618, Estonia

Skype me:  callto:hkrosing
Get Skype for free:  http://www.skype.com

NOTICE: This communication contains privileged or other confidential
information. If you have received it in error, please advise the sender
by reply email and immediately delete the message and any attachments
without copying or disclosing the contents.

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

Re: my cryptogram program

2006-05-15 Thread Bruno Desthuilliers
Paul Rubin a écrit :
> Bruno Desthuilliers <[EMAIL PROTECTED]> writes:
> 
>>def convert_quote(quote):
>>   return make_code(quote).split('|', 1)
> 
> 
> I thought about suggesting that, but it's semantically different than
> the original since it fails to raise an error if no vertical bar is
> present.  

Yeps, true.

> I don't know if that's good or bad.

At least someone reading this may learn about the max_split param of 
str.split() !-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: count items in generator

2006-05-15 Thread Bruno Desthuilliers
George Sakkis a écrit :
(snip)
> def length(iterable):
> try: return len(iterable)
> except:

except TypeError:

> i = 0
> for x in iterable: i += 1
> return i
> 
(snip)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tabs versus Spaces in Source Code

2006-05-15 Thread Peter Decker
On 5/15/06, Edward Elliott <[EMAIL PROTECTED]> wrote:

> If such tools are lacking, use substitutes in the meantime.  Don't allow any
> code to be checked in where a line departs more than one tab indentation
> level from its neighbors.  It's not perfect, but it eliminates the worst
> offenses.  Good enough often is.

I've recently adopted the Dabo convention of using two indent levels
to indicate continued lines, which helps distinguish such lines from
the indented blocks below them. But other than that case, one tab it
is!
-- 

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


Re: [HACKERS] Iterating generator from C (PostgreSQL's pl/python RETUN

2006-05-15 Thread Tom Lane
Hannu Krosing <[EMAIL PROTECTED]> writes:
>> Sven Suursoho wrote:
>>> As for testing in actual pl/python build environment, we had objections 
>>> from 
>>> leading postgresql Tom Lane that even if we do test it at build time, 
>>> a determined DBA may substitute a buggy python.so later and still crash her 
>>> DB instance.

The above is a straw-man depiction of my point.  What I said was that just
because python is up-to-date on the system where plpython is *compiled*
does not mean it'll be up-to-date on the system where plpython is *used*.
With the increasing popularity of prebuilt packages (rpm, deb, etc),
I think it's folly to use a build-time check for this.

regards, tom lane
-- 
http://mail.python.org/mailman/listinfo/python-list


HTTPServer and ThreadingMixIn

2006-05-15 Thread crowell
Hello,

I am having trouble getting the ThreadingMixIn to do what I want.
Looking over the docs (and some old code I wrote which successfully
does what I want), I arrived at the following:

import time
import SocketServer
import BaseHTTPServer

class TheServer(SocketServer.ThreadingMixIn,
BaseHTTPServer.HTTPServer):
pass

class TheHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
print "sleeping..."
time.sleep(10)
print "awake!"

if __name__ == "__main__":
theServer = TheServer(('', 8083), TheHandler)
theServer.serve_forever()

I would like this server to print "sleeping..." when someone make a
request, waits 10 seconds, then prints "awake!"  This works, but it
does not exhibit threading behavior; i.e. it handles the requests in
sequential order:
sleeping...
awake!
sleeping...
awake!

Is there some problem with using the time.sleep() method?  Or have I
done something else wrong?  Sorry for asking such a simple question,
but it seems I am unable to pinpoint my error.

Thanks.

--Rob

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


Re: regular expression error ?

2006-05-15 Thread Bruno Desthuilliers
Lance Hoffmeyer a écrit :
> why isn't re.search recognizing "file"?  I can print the contents of "file".
> 
> 
> import win32com.client
> import re
> 
> D=MSWord.Documents.Open('C:/test/Test.doc')
> file=D.Content
> print file
> 
> match = re.search('(Table ?)', file)
> if match:
>print "Table Number is: ", match.group(1)
> 
> 
> Traceback (most recent call last):
>   File "test.py", line 21, in ?
> match = re.search('(Table ?)', file)
>   File "C:\Python24\Lib\sre.py", line 134, in search
> return _compile(pattern, flags).search(string)
> TypeError: expected string or buffer

The fact that you can print 'file' doesn't mean it's a string. You may 
want to try this:

(...)
# let's see what it really is:
print type(D.Content), repr(D.Content)
# and try to make it usable for re:
file = str(D.Content)
(...)

NB : not tested, I don't have Word on my Linix box !-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: my cryptogram program

2006-05-15 Thread Paul Rubin
Bruno Desthuilliers <[EMAIL PROTECTED]> writes:
> def convert_quote(quote):
>return make_code(quote).split('|', 1)

I thought about suggesting that, but it's semantically different than
the original since it fails to raise an error if no vertical bar is
present.  I don't know if that's good or bad.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: my cryptogram program

2006-05-15 Thread Paul Rubin
John Salerno <[EMAIL PROTECTED]> writes:
> def convert_quote(quote):
>  coded_quote = make_code(quote)
>  author = coded_quote.split('|')[1]
>  quote = coded_quote.split('|')[0]
>  return quote, author

I think it's a little bit ugly (plus inefficient) to split the quote twice.
You can use:

  def convert_quote(quote):
 coded_quote = make_code(quote)
 author, quote = coded_quote.split('|')
 return quote, author

> def make_code(original):
>  original_letters = make_set(original)
>  new_letters = list(string.ascii_uppercase)
>  while True:
>  random.shuffle(new_letters)
>  trans_letters = ''.join(new_letters)[:len(original_letters)]
>  if test_code(original_letters, trans_letters):
>  trans_table = string.maketrans(original_letters, trans_letters)
>  break
>  return original.translate(trans_table)

You're trying to make sure that no character maps to itself in the
cryptogram.  I'm not sure if that's one of the "rules".  If not, you
might like to know that it's a cryptographic weakness, not that you're
attempting strong cryptography ;-).  But the WW2 Enigma machine had
that characteristic as part of its design, and that was used to break it.

It also looks like upper and lower case letters in the input are
treated as separate.  For example, "George" become "abcdef" while
"george" becomes "abcde".  Did you want that?  I don't think it can be
right, because "trans_letters" has at most 26 characters, but there
can be 52 separate original letters (26 upper and 26 lower).

As for the efficiency of the above algorithm, well, look at what
happens after you shuffle the alphabet: the probability that any given
character maps to something other than itself is 25/26.  That means
the probability that N letters all map to something other than
themselves is (25/26)**N.  If N=26, this is about 0.36, so on average
you'll shuffle about three times, which is not too bad, if you're just
doing something casual.  Note that this is close to 1/e.  I'll let you
figure out the reason.  Of course there is a chance you'll have to
shuffle 4 times, 10 times, 1000 times, etc.  You might like to
calculate those probabilities and decide whether it's worth thinking
up a more efficient algorithm, that's possibly more complex and
therefore more likely to have bugs, to burn additional development
time, etc.  How bad is it if occasional instances take a long time to
generate?  This is the kind of tradeoff you always have to spend time
pondering if you're doing a large scale application.

> def make_set(original):
>  original_set = set(original)
>  punc_space = string.punctuation + string.whitespace
>  for char in punc_space:
>  if char in original_set:
>  original_set.remove(char)
>  return ''.join(original_set)

I think I'd write something like:

  def make_set(original):
  return set(strings.ascii_uppercase) & set(original.upper())

That is, convert the original string to uppercase, make a set from it,
and then intersect that set with the set of uppercase letters.  This
seems more direct.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: my cryptogram program

2006-05-15 Thread Bruno Desthuilliers
John Salerno a écrit :
> Alrighty, here is what I've come up with. Any suggestions for tweaks or 
> speed-boosting efficiency? Just kidding. :)

Too late ! You asked for it, you get it !-)

> Maybe the weakest part is how the code is determined (just shuffling the 
> letters in the alphabet). Perhaps there's a better way? Although it 
> seems effective to me
> 
> 
> import string
> import random
> import itertools
> 
> def convert_quote(quote):
> coded_quote = make_code(quote)
> author = coded_quote.split('|')[1]
> quote = coded_quote.split('|')[0]
> return quote, author

def convert_quote(quote):
   return make_code(quote).split('|', 1)

> 
> def make_code(original):
> original_letters = make_set(original)
> new_letters = list(string.ascii_uppercase)
> while True:
> random.shuffle(new_letters)
> trans_letters = ''.join(new_letters)[:len(original_letters)]
> if test_code(original_letters, trans_letters):
> trans_table = string.maketrans(original_letters, trans_letters)
> break
> return original.translate(trans_table)
> 
> def make_set(original):
> original_set = set(original)
> punc_space = string.punctuation + string.whitespace
> for char in punc_space:
> if char in original_set:
> original_set.remove(char)
> return ''.join(original_set)

PUNCT_SPACE_SET = set(string.punctuation + string.whitespace)
def make_set(original):
   return ''.join(set(original) - PUNCT_SPACE_SET)


BTW, the name make_set is a bit misleading, since the function returns a 
string... FWIW, some other functions could benefit from better naming too:
- convert_quote() takes a "|" string and returns a 
(, ) pair (given the name, I would expect a 
"| " string as output)
- make_code() doesn't actually 'make a code' - it really encrypt it's input


> 
> def test_code(original_letters, trans_letters):
> for pair in itertools.izip(original_letters, trans_letters):
> if pair[0] == pair[1]:
> return False
> return True

If you really want "speed-boosting efficiency", you may try this instead:
def test_code(original_letters, trans_letters)
 # assert len(original_letters) == len(trans_letters), \
 #"original_letters and trans_letters should be of equal len"
 for i in range(original_letters):
 if original_letters[i] == trans_letters[i]:
 return False
 return True

> if __name__ == '__main__':
> print convert_quote("The past is not dead. In fact, it's not even 
> past.|William Faulkner")

My 2 cents...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Test professionalism (was: count items in generator)

2006-05-15 Thread Timothy Grant
On 5/15/06, Cameron Laird <[EMAIL PROTECTED]> wrote:
> In article <[EMAIL PROTECTED]>,
> Delaney, Timothy (Tim) <[EMAIL PROTECTED]> wrote:
> .
> .
> .
> >That's exactly my point. Assuming your test coverage is good, such an
> >error would be caught by the MemoryError. An infinite loop should also
> >be caught by timing out the tests, but that's much more dependent on the
> >test harness.
> >
> >Tim Delaney
>
> Gulp.  OK, you've got me curious:  how many people habitually frame
> their unit tests with resource constraints?  I think I take testing
> seriously, and certainly also am involved with resource limits often,
> but I confess I've never aimed to write all my tests in terms of
> bounds on time (and presumably memory and ...).  You've got me
> thinking, Tim.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

I'm a huge proponent of unittest and believe I take them very
seriously also. I try never to write a line of code unless I have a
test to prove I need it.

I have written tests that take into account resource constraints, but
ONLY when I've written code that, while passing all tests, shows
resource consumption problems.

Creating resource contstraint tests out of the gate *may* fall into
the category of premature optimization.

-- 
Stand Fast,
tjg.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Large Dictionaries

2006-05-15 Thread bearophileHUGS
Roy Smith:
> I am befuddled as to why
> people thought creating a dict by keyword would be a useful thing to
> do, but left out (and, indeed, eliminated the chance to add the syntax
> later) the obviously useful ability to hint at the size.

Adding the keyword syntax doesn't require much memory to a Python
programmer because it's the same syntax used elsewhere. While adding
another different method to dicts increases their API. Python is
usually designed to have smaller APIs, to help programmers remember
most things.

I agree that a reserve method to Python dicts/sets can be a little
useful, but Python programmers are usually more concerned about doing
things in a short programmer time and short code space, than telling
the computer how to do such things in the faster way (C++ is more for
speed so the reserve of the STL hashes can be more useful).

Bye,
bearophile

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


RE: Iterating generator from C (PostgreSQL's pl/python RETUN SETOF/RECORD iterator support broken on RedHat buggy libs)

2006-05-15 Thread Hannu Krosing
Sorry for cross-posting, but this IS a cross-platform issue.

Christian Tismer tismer at stackless.com  wrote:
> Sven Suursoho wrote:
> 
> >>>  Is there any way to rewrite following program to handle returned 
> >>> generator  without hitting this bug?
> 
> The only way I can think of getting around this is to make
> sure that there is always a running python frame.
> This would be possible if you can control how the
> extension is called.

What would be the easiest way to hold a "always running" python frame ?

The actual calls to iterator come through a pl/python framework, which can hold 
some 
state - like some inter-call dictionaries - so keeping also a simple outer 
frame 
there may be possible.

What danger (apart from general uglyness) may lurk there in keeping this frame ?

> > Unfortunately, this is not an option because I can't control used 
> > environment: I'm trying to improve PostgreSQL's stored procedure 
> > language PL/Python and this software can be used everywhere.
> 
> Then there is no other way than to report the bug, get the
> fix back-ported and nagging the PL/Python maintainers
> to update things after the fix.

Unfortunately there is no 'after the fix', as the fix must happen outside 
postgresql/plpython development and should also run on oldish systems.

The reason we want to get some workaround for that bug is the need 
to overcome resistance from core postgreSQL developers to inclusion of our 
plpython enchancements to postgreSQLs bundled plpython due to one specific use 
of our generic enchancement (using a built-in generator, usually a function 
with yield) 
on buggy RedHat's bundled plpython.so causing crashes.

Also, PL/Python has been in minimal maintenance mode for many years, with 
basically only immediate bugfixing going on. 

We at Skype (that is currently Sven Suursoho) are the first ones doing 
active development on it after Andrew Bosma dropped development many years 
ago once he got just the very basic stuff working.

> Also a test should be added which is probably missing since a while.

Test where ? In python.so build process, so that RedHat will spot it and 
fix their RPM builds ? 

As for testing in actual pl/python build environment, we had objections from 
leading postgresql Tom Lane that even if we do test it at build time, 
a determined DBA may substitute a buggy python.so later and still crash her DB 
instance.

Do you have any ideas, how to test for buggy asserts in python runtime 
environment without actually triggering the assert ?

Then we could introduce some (uglish) special handling for generators in 
pl/pythons iterator.

> I'd put a warning somewhere that generators are broken in
> debug mode, file an issue as well, but avoid trying to hack
> around this. It would make the bug even more resistent :-)

We have been trying to advocate such approach, but so far with modest results :(

-- 

Hannu Krosing
Database Architect
Skype Technologies OÜ
Akadeemia tee 21 F, Tallinn, 12618, Estonia

Skype me:  callto:hkrosing
Get Skype for free:  http://www.skype.com


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

Re: common practice for creating utility functions?

2006-05-15 Thread Bruno Desthuilliers
John Salerno a écrit :
> John Salerno wrote:
> 
>> John Salerno wrote:
>>
>>> Just a quickie for today
>>
>>
>> Another quick one I'll combine in this thread: How can I create two 
>> separate conditions in a for loop?
>>
>> Such as this, which doesn't seem to work beyond string.punctuation:
>>
>> for char in string.punctuation or string.whitespace:
> 
> 
> I tried this:
> 
> punc_space = string.punctuation + string.whitespace
> for char in punc_space:

Or if you want to save memory (which is not a problem in this example) :

import itertools
for char in itertools.chain(string.punctuation, string.whitespace):
   ...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: common practice for creating utility functions?

2006-05-15 Thread Bruno Desthuilliers
Paul Rubin a écrit :
> John Salerno <[EMAIL PROTECTED]> writes:
> 
>>So my first question is this: should I make a Cryptogram class for
>>this, or are functions fine? If the latter, then back to my original
>>point: can I do something like this:
>>
>>def convert_quote(quote):
>> return make_code(quote)
> 
> 
> It's fine to do that if it expresses your intentions more clearly.

Then it would be better to just alias it:

# def convert_quote(quote):
# return make_code(quote)
convert_quote = make_code

About the "fine to do" part, remember that Python's function calls are 
rather expansive...
-- 
http://mail.python.org/mailman/listinfo/python-list


regular expression error ?

2006-05-15 Thread Lance Hoffmeyer
why isn't re.search recognizing "file"?  I can print the contents of "file".


import win32com.client
import re

D=MSWord.Documents.Open('C:/test/Test.doc')
file=D.Content
print file

match = re.search('(Table ?)', file)
if match:
   print "Table Number is: ", match.group(1)


Traceback (most recent call last):
  File "test.py", line 21, in ?
match = re.search('(Table ?)', file)
  File "C:\Python24\Lib\sre.py", line 134, in search
return _compile(pattern, flags).search(string)
TypeError: expected string or buffer
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: common practice for creating utility functions?

2006-05-15 Thread Bruno Desthuilliers
John Salerno a écrit :
> Just a quickie for today: Is it common (and also preferred, which are 
> two different things!) to create a function that has the sole job of 
> calling another function?

(pasted):
 > def convert_quote(quote):
 > return make_code(quote)
 >
 > Or does it not make sense to have a function just call another function?

The only use case I can think of for this extra level of indirection 
would be to isolate your code from an implementation detail that may 
change later. But since it's quite easy to do so by just binding the 
original function to a new name, your example's sole result is to slow 
down your program.

It may also happen that you find yourself in such a situation due to 
heavy refactoring, and cannot immediatly get rid of convert_quote 
without breaking client code, but then again, the Right Thing (tm) to do 
would be to alias it:

# def convert_quote(quote):
# return make_code(quote)
convert_quote = make_quote

So to answer your question, it's certainly not prefered, and hopefully 
very uncommon !-)


> Example: for fun and exercise, I'm creating a program that takes a quote 
> and converts it into a cryptogram. Right now I have four functions:
> 
> convert_quote -- the main function that starts it all
> make_code -- makes and returns the cryptogram
> make_set -- called from make_code, converts the quote into a set so each 
> letter gets only one coded letter
> test_sets -- makes sure a letter isn't assigned to itself
> 
> So my first question is this: should I make a Cryptogram class for this, 
> or are functions fine?

classes are useful to maintain state. If you don't have a state to 
maintain, you probably don't need a class !-)

Note BTW that Python functions are objects (instances of class 
function), and that Python let you write your own callables (just make 
your class implement the __call__() magic method).

My 2 cents
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Aggregate funuctions broken in MySQLdb?

2006-05-15 Thread John Salerno
Lorenzo Thurman wrote:
> Thanks, that was my problem. Can you point me to some documentation on 
> MySQLdb? I've been googling to get answers and that obviously has not 
> been working.

I've been looking into this too lately, and finding thorough docs for it 
is hard. Have you seen these yet: 
http://sourceforge.net/docman/?group_id=22307
-- 
http://mail.python.org/mailman/listinfo/python-list


my cryptogram program

2006-05-15 Thread John Salerno
Alrighty, here is what I've come up with. Any suggestions for tweaks or 
speed-boosting efficiency? Just kidding. :)

Maybe the weakest part is how the code is determined (just shuffling the 
letters in the alphabet). Perhaps there's a better way? Although it 
seems effective to me


import string
import random
import itertools

def convert_quote(quote):
 coded_quote = make_code(quote)
 author = coded_quote.split('|')[1]
 quote = coded_quote.split('|')[0]
 return quote, author

def make_code(original):
 original_letters = make_set(original)
 new_letters = list(string.ascii_uppercase)
 while True:
 random.shuffle(new_letters)
 trans_letters = ''.join(new_letters)[:len(original_letters)]
 if test_code(original_letters, trans_letters):
 trans_table = string.maketrans(original_letters, trans_letters)
 break
 return original.translate(trans_table)

def make_set(original):
 original_set = set(original)
 punc_space = string.punctuation + string.whitespace
 for char in punc_space:
 if char in original_set:
 original_set.remove(char)
 return ''.join(original_set)

def test_code(original_letters, trans_letters):
 for pair in itertools.izip(original_letters, trans_letters):
 if pair[0] == pair[1]:
 return False
 return True

if __name__ == '__main__':
 print convert_quote("The past is not dead. In fact, it's not even 
past.|William Faulkner")
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: common practice for creating utility functions?

2006-05-15 Thread John Salerno
Scott David Daniels wrote:
> John Salerno wrote:
>>> How can I create two separate conditions in a for loop?
>> ... I tried this:
>>
>> punc_space = string.punctuation + string.whitespace
>> for char in punc_space:
> 
> That's probably best.  If the sources are not so simple, you could use:
> 
> import itertools
> 
> for char in itertools.chain(onesource, anothersource, yetanother):
> ...
> 
> --Scott David Daniels
> [EMAIL PROTECTED]

Cool. Iterators are fun! :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Aggregate funuctions broken in MySQLdb?

2006-05-15 Thread Lou Losee
Try these:http://sourceforge.net/docman/?group_id=22307and for the Python DB API overall:http://www.python.org/dev/peps/pep-0249/
LouOn 5/15/06, Lorenzo Thurman <[EMAIL PROTECTED]> wrote:
Thanks, that was my problem. Can you point me to some documentation onMySQLdb? I've been googling to get answers and that obviously has notbeen working.In article <
[EMAIL PROTECTED]>, "Wade Leftwich" <[EMAIL PROTECTED]> wrote:> Works fine for me, and I certainly hope MySQLdb is ready for prime
> time, because I use the heck out of it. Maybe you're getting fooled by> the fact that cursor.execute() returns the count of result rows. To> actually see the result rows, you have to say cursor.fetchone
() or> fetchall() -->> In [34]: cur.execute("select article_id from articles limit 10")> Out[34]: 10L>> In [35]: cur.fetchall()> Out[35]: ((3L,), (4L,), (5L,), (6L,), (7L,), (8L,), (9L,), (10L,),
> (11L,), (12L,))>> In [36]: cur.execute("select count(article_id) from articles where> article_id < 13")> Out[36]: 1L>> In [37]: cur.fetchall()> Out[37]: ((10L,),)
>> In [38]: cur.execute("select sum(article_id) from articles where> article_id < 13")> Out[38]: 1L>> In [39]: cur.fetchone()> Out[39]: (75.0,)>> In [40]: 
cur.execute("select avg(article_id) from articles where> article_id < 13")> Out[40]: 1L>> In [41]: cur.fetchone()> Out[41]: (7.5,)--
http://mail.python.org/mailman/listinfo/python-list-- Artificial Intelligence is no match for Natural Stupidity
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Aggregate funuctions broken in MySQLdb?

2006-05-15 Thread Lorenzo Thurman
Thanks, that was my problem. Can you point me to some documentation on 
MySQLdb? I've been googling to get answers and that obviously has not 
been working.




In article <[EMAIL PROTECTED]>,
 "Wade Leftwich" <[EMAIL PROTECTED]> wrote:

> Works fine for me, and I certainly hope MySQLdb is ready for prime
> time, because I use the heck out of it. Maybe you're getting fooled by
> the fact that cursor.execute() returns the count of result rows. To
> actually see the result rows, you have to say cursor.fetchone() or
> fetchall() --
> 
> In [34]: cur.execute("select article_id from articles limit 10")
> Out[34]: 10L
> 
> In [35]: cur.fetchall()
> Out[35]: ((3L,), (4L,), (5L,), (6L,), (7L,), (8L,), (9L,), (10L,),
> (11L,), (12L,))
> 
> In [36]: cur.execute("select count(article_id) from articles where
> article_id < 13")
> Out[36]: 1L
> 
> In [37]: cur.fetchall()
> Out[37]: ((10L,),)
> 
> In [38]: cur.execute("select sum(article_id) from articles where
> article_id < 13")
> Out[38]: 1L
> 
> In [39]: cur.fetchone()
> Out[39]: (75.0,)
> 
> In [40]: cur.execute("select avg(article_id) from articles where
> article_id < 13")
> Out[40]: 1L
> 
> In [41]: cur.fetchone()
> Out[41]: (7.5,)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: common practice for creating utility functions?

2006-05-15 Thread Scott David Daniels
John Salerno wrote:
>> How can I create two separate conditions in a for loop?
>... 
> I tried this:
> 
> punc_space = string.punctuation + string.whitespace
> for char in punc_space:

That's probably best.  If the sources are not so simple, you could use:

 import itertools

 for char in itertools.chain(onesource, anothersource, yetanother):
 ...

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


Re: A critic of Guido's blog on Python's lambda

2006-05-15 Thread Ken Tilton


Lasse Rasinen wrote:
> [I trimmed some of the newsgroups away; this mostly concerns Python and Lisp]
> 
> Ken Tilton <[EMAIL PROTECTED]> writes:
> 
> 
>>Lasse Rasinen wrote:
>>
>>>Ken Tilton <[EMAIL PROTECTED]> writes:
>>>
>>>
>if any concepts have survived to the Python version. Since Python's object
>model is sufficiently different, the system is based on rules being
>defined per-class...

That will be a total disaster for PyCells, if true. But I do not think it
is. You just need a constructor that takes some slot initializers, and
initialize the slots to one of: a normal value; an InputCell itself
initialized with a starting value, if only nil; or a RuledCell itself
initialized with a lambda.
>>>
>>>Hmm, just tried it:
>>>
>>>[snip example]
>>>
>>>So it does work out-of-the-box ;-)
>>
>>So why exactly did you say that the differences in the object model made
>>it impossible? I was really stunned by that claim. And you sounded so
>>confident. What went wrong there? It was trivial, right? How did you miss
>>that?
> 
> 
> Simple: I didn't think to try that before you asked.
> 
> I did not say the differences in the object model made it impossible, I
> said the system is based on rules defined per-class. 

Oh, please: "Since Python's object model is sufficiently different, the 
system is based on rules being defined per-class".

> I also think(*) that while one would have the option to define
> per-instance rules, in a Python implementation one would structure the
> code so that common rules would be class-related,..

What has Python got to do with it? What you describing is the way every 
OO system other than a prototype-based system works. It is why OO failed 
to deliver on the Grail of object reuse: every time you need different 
behavior, you need a new subclass. Literal values only go so far in 
making instances authorable. But when an instance can have a rule with 
itself as an argument, and when instances exist in a runtime hierarchy 
navigable up and down such that rules have effectively global scope, 
yowza, now you have authorability, amd now you have object reuse.


> and the per-instance
> rules would be used less frequently, used only when you absolutely need
> them.

You will see, but not until you have the capability. Then you will 
discover you absolutely need them all the time. GUIs are dynamic things, 
so you cannot just author a widget with a literal value, it has to be a 
rule sensitive to the state of other GUI elements and the model itself, 
which might be changing underfoot in response to I/O events.

I happened to be discussing this just now over in comp.lang.tcl, 
comparing Cells with Actions, a kindred package motivated by this:

"Two large problems exist for developers of such user interfaces. One is 
the need to constantly synchronize the controls with the ever-changing 
state of the application or data. When no text is selected, for example, 
the cut and copy buttons should be disabled.

"Another problem is that as programs evolve over time it can become 
tedious and error prone to update the parts of the code that act upon 
these controls."

Found here:

http://www.tcl.tk/community/tcl2004/Papers/BryanOakley/oakley.pdf

> 
> (*) Unfounded Gut Feeling(TM); if your project is successful, we can
> revisit this prediction in September ;-) 

No need for gut feelings. Anyone working in GUIs knows the problem 
(stated by Mr Oakley above) and many partial solutions exist, such as 
Tcl/Tk's builtin mechanisms for automatic state management. They called 
the company ActiveState for a reason, you know. :)

One person did his homework. vasilsi margioulas sent me cells-gtk, a 
two-week marriage of cells and Gtk derived from my Cells-Tk effort. He 
was curious if Cells would be useful. he decided, yes. :)


> 
> 
PyCells looks like it will be a project for SoC2006, so you may as well
relax.
>>>
>>>You really want to start a SoC project on something that takes about two
>>>weeks ...
>>
>>You sound so confident. :)
> 
> 
> Indeed. Having reread the post I do think the tone was possibly a tad
> arrogant. However:
> 
> 
>>A new test suite, documentation (a first, there is none now), a full port
>>of Cells in all their ten years of sophisticated evolution and variety
>>(no, not your stupid pet trick), and as a demo project an entire
>>cells-driven GUI, probably a port of my new Celtk (+ Cells Tk) work, all
>>in a language without macros, without special variables, with a more
>>modest OO system, and limited first class functions... oh, I think we'll
>>keep him busy. :)
> 
> 
> I did not know all this. The list above does sound like a full summer of
> work ;)
> 
> I assumed that PyCells referred to the core dependency tracking module
> which (even now) does not sound like a such huge task, especially when one
> has the reference implementation ;-)

I think the real problem here is that you have no idea how fast twelve 
weeks can go while programming. T

Re: common practice for creating utility functions?

2006-05-15 Thread BartlebyScrivener
QOTW

"Programming is not just creating strings of instructions for a
computer to execute.  It's also 'literary' in that you are trying to
communicate a program structure to other humans reading the code." Paul
Rubin

rpd

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


Re: common practice for creating utility functions?

2006-05-15 Thread John Salerno
John Salerno wrote:
> John Salerno wrote:
>> Just a quickie for today
> 
> Another quick one I'll combine in this thread: How can I create two 
> separate conditions in a for loop?
> 
> Such as this, which doesn't seem to work beyond string.punctuation:
> 
> for char in string.punctuation or string.whitespace:

I tried this:

punc_space = string.punctuation + string.whitespace
for char in punc_space:
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: common practice for creating utility functions?

2006-05-15 Thread John Salerno
Dan Sommers wrote:

> Perhaps I'm "old school," but I don't bother with classes unless I'm
> going to end up with multiple instances (or I'm pushed into a corner by,
> e.g., a GUI framework).

Thanks to all of you! In fact, I sort of came to this same conclusion. I 
started with a class, but realized there wasn't much of a point since 
I'm just doing some 'work' and returning the cryptogram.

I might try the other suggestion too about convert_quote = make_code, 
that might be ok. Or I can simply call make_code.

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


Re: OOP and Tkinter

2006-05-15 Thread Diez B. Roggisch
Ronny Mandal schrieb:
> Thanks, but the problem was my undentation, after making one indent,
> it worked as expected. :)

I seriously doubt that. I guess you didn't show us the real code. But 
there is no way that an instance variable can be accessed on type-level 
in a derived class (or from anywhere, for that matter). So I presume you 
have some misconceptions about pythons OO-model that will sooner or 
later bite you - for example if you want to have several Front-objects.

Diez


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


Re: common practice for creating utility functions?

2006-05-15 Thread John Salerno
John Salerno wrote:
> Just a quickie for today

Another quick one I'll combine in this thread: How can I create two 
separate conditions in a for loop?

Such as this, which doesn't seem to work beyond string.punctuation:

for char in string.punctuation or string.whitespace:
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Large Dictionaries

2006-05-15 Thread Diez B. Roggisch
Maric Michaud schrieb:
> Le Lundi 15 Mai 2006 19:24, Roy Smith a écrit :
>> d = {}
>> d.reserve (10*1000*1000)
> 
> d={}.fromkeys(xrange(5*10**6)) ?

That is a totally different beast. You don't want to insert arbitrary 
keys, you want the internal hash-array to be of the right size.

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


Re: OOP and Tkinter

2006-05-15 Thread Kent Johnson
Ronny Mandal wrote:
> file front_ui.py:
> 
> class Front(object):
> _images = [] # Holds image refs to prevent GC
> def __init__(self, root):
> # Widget Initialization
> self._listbox_1 = Tkinter.Listbox(root,
> height = 0,
> width = 0,
>   ...
>   )
> 

> other file:
> 
> from Front_ui import Front
> 
> class CustomFront(Front):
>  Front._listbox_1.insert( 0, 'foo' ) 
> 
> ...
> ...
>   File "H:\My Documents\Komodo\Front.py", line 63, in CustomFront
> Front._listbox_1.insert( 0, foo' )
> AttributeError: type object 'Front' has no attribute '_listbox_1'
> 
> 
> i.e., it cannot find the listbox! Strange, both files is in the same
> folder. What is wrong here?

_listbox_1 is an instance attribute, not a class attribute. You need to 
refer to self._listbox_1 from a CustomFront method, or change _listbox_1 
to a class attribute if that is what you really want.

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


Re: common practice for creating utility functions?

2006-05-15 Thread Scott David Daniels
John Salerno wrote:
> ...Is it common ...[and preferred] to create a function that has the sole job 
> of 
> calling another function?
> 
> Example: ... cryptogram. Right now I have four functions:
> 
> convert_quote -- the main function that starts it all
> make_code -- makes and returns the cryptogram
> make_set -- called from make_code, converts the quote into a set so each 
> letter gets only one coded letter
> test_sets -- makes sure a letter isn't assigned to itself
> 
> So my first question is this: should I make a Cryptogram class for this, 
> or are functions fine? 
Functions are just fine.  I'd use a class if they wanted to share state.

> ... can I do something like this:
> def convert_quote(quote):
> return make_code(quote)
> Or does it not make sense to have a function just call another function?
Obviously you _can_ do that.  I wouldn't, however.  If (to you) the four
functions above "mean" something different, I'd implement convert_quote
with:
  convert_quote = make_code

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


Re: OOP and Tkinter

2006-05-15 Thread Ronny Mandal

Thanks, but the problem was my undentation, after making one indent,
it worked as expected. :)


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


Re: OOP and Tkinter

2006-05-15 Thread Diez B. Roggisch
Ronny Mandal schrieb:
> Hello.
> 
> I am stuck; provided is the code and error-msg:
> 
> file front_ui.py:
> 
> class Front(object):
> _images = [] # Holds image refs to prevent GC
> def __init__(self, root):
> 
> 
> # Widget Initialization
> self._listbox_1 = Tkinter.Listbox(root,
> height = 0,
> width = 0,
>   ...
>   )
> 
> 
> 
> 
> 
> 
> other file:
> 
> from Front_ui import Front
> 
> class CustomFront(Front):
> 
> 
>  Front._listbox_1.insert( 0, 'foo' ) 
> 
> ...
> ...
>   File "H:\My Documents\Komodo\Front.py", line 63, in CustomFront
> Front._listbox_1.insert( 0, foo' )
> AttributeError: type object 'Front' has no attribute '_listbox_1'
> 
> 
> i.e., it cannot find the listbox! Strange, both files is in the same
> folder. What is wrong here?

That you refer to the type/class Front with expressions like Front.<...> 
instead to an _instance_ of Front. You need to do things like this:

class CustomFront(Front):
  def __init__(self, root):
  super(CustomFront, self).__init__(root) #now the super-classes 
instance variables are there!
  self._listbox_1.insert( 0, 'foo' )




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


Re: common practice for creating utility functions?

2006-05-15 Thread Dan Sommers
On Mon, 15 May 2006 18:26:01 GMT,
John Salerno <[EMAIL PROTECTED]> wrote:

> So my first question is this: should I make a Cryptogram class for
> this, or are functions fine? ...

Perhaps I'm "old school," but I don't bother with classes unless I'm
going to end up with multiple instances (or I'm pushed into a corner by,
e.g., a GUI framework).

> ... If the latter, then back to my original point:  can I do something
> like this:

> def convert_quote(quote):
> return make_code(quote)

Of course you can.  Or, since this is python, you can also do this:

convert_quote = make_quote

> Or does it not make sense to have a function just call another
> function?

If there's a good design-level reason (like keeping certain objects or
classes unaware of others, or leaving room for something you know you
will add later), then there's nothing wrong with a function consisting
solely of another function call.  If you end up with a lot of those tiny
functions, though, and they persist through multiple development cycles,
then you may be making a systematic mistake in your design.

Regards,
Dan

-- 
Dan Sommers

"I wish people would die in alphabetical order." -- My wife, the genealogist
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: common practice for creating utility functions?

2006-05-15 Thread Paul Rubin
John Salerno <[EMAIL PROTECTED]> writes:
> So my first question is this: should I make a Cryptogram class for
> this, or are functions fine? If the latter, then back to my original
> point: can I do something like this:
> 
> def convert_quote(quote):
>  return make_code(quote)

It's fine to do that if it expresses your intentions more clearly.
It's also normal to write a call for a function you haven't written
yet:

   some_quote = convert_quote(another_quote)

where you write convert_quote afterwards.  It may not be til you
actually write convert_quote that you notice that it simply calls
another function.  If that happens, it's fine.

Also, you don't need to make a class if you don't need to do OOP-like
things with the instances.  But, sometimes, making a class also
clarifies your intentions.  For example, making a class means it
becomes much simpler to have multiple cryptograms active at the same
time, if you want to do that.

Programming is not just creating strings of instructions for a
computer to execute.  It's also "literary" in that you are trying to
communicate a program structure to other humans reading the code.

Someone once asked the famous bike racing champion Eddy Merckx (he was
sort of the Lance Armstrong of the 1970's and then some) how to become
a good racer.  His answer was "ride a lot".  It's the same way with
coding.  As you get more experienced these questions get easier to
answer for yourself.  But always, do whatever works, and if your code
gets messy or hits technical snags, ask yourself what alternative
approaches avoid those snags, and thereby develop a sense of what to
do.
-- 
http://mail.python.org/mailman/listinfo/python-list


OOP and Tkinter

2006-05-15 Thread Ronny Mandal
Hello.

I am stuck; provided is the code and error-msg:

file front_ui.py:

class Front(object):
_images = [] # Holds image refs to prevent GC
def __init__(self, root):


# Widget Initialization
self._listbox_1 = Tkinter.Listbox(root,
height = 0,
width = 0,
...
)






other file:

from Front_ui import Front

class CustomFront(Front):


 Front._listbox_1.insert( 0, 'foo' ) 

...
...
  File "H:\My Documents\Komodo\Front.py", line 63, in CustomFront
Front._listbox_1.insert( 0, foo' )
AttributeError: type object 'Front' has no attribute '_listbox_1'


i.e., it cannot find the listbox! Strange, both files is in the same
folder. What is wrong here?


Thanks,

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


Re: A critic of Guido's blog on Python's lambda

2006-05-15 Thread Lasse Rasinen
[I trimmed some of the newsgroups away; this mostly concerns Python and Lisp]

Ken Tilton <[EMAIL PROTECTED]> writes:

> Lasse Rasinen wrote:
> > Ken Tilton <[EMAIL PROTECTED]> writes:
> >
> >>>if any concepts have survived to the Python version. Since Python's object
> >>>model is sufficiently different, the system is based on rules being
> >>>defined per-class...
> >>
> >>That will be a total disaster for PyCells, if true. But I do not think it
> >>is. You just need a constructor that takes some slot initializers, and
> >>initialize the slots to one of: a normal value; an InputCell itself
> >>initialized with a starting value, if only nil; or a RuledCell itself
> >>initialized with a lambda.
> > Hmm, just tried it:
> >
> > [snip example]
> >
> > So it does work out-of-the-box ;-)
> 
> So why exactly did you say that the differences in the object model made
> it impossible? I was really stunned by that claim. And you sounded so
> confident. What went wrong there? It was trivial, right? How did you miss
> that?

Simple: I didn't think to try that before you asked.

I did not say the differences in the object model made it impossible, I
said the system is based on rules defined per-class. My goal was to
explore the how one would go about defining data flow in Python, which is
why I concentrated on class-level definitions first.

The situation is similar to functions in classes. Normally you'd define
them like this:

class X:
  def function(self, ...):
...

However, you can just as well set them, even on per instance basis:
x = X()
x.another_function = lambda self, x: return x+2


I also think(*) that while one would have the option to define
per-instance rules, in a Python implementation one would structure the
code so that common rules would be class-related, and the per-instance
rules would be used less frequently, used only when you absolutely need
them.

(*) Unfounded Gut Feeling(TM); if your project is successful, we can
revisit this prediction in September ;-) 

> >>PyCells looks like it will be a project for SoC2006, so you may as well
> >>relax.
> > You really want to start a SoC project on something that takes about two
> > weeks ...
> 
> You sound so confident. :)

Indeed. Having reread the post I do think the tone was possibly a tad
arrogant. However:

> A new test suite, documentation (a first, there is none now), a full port
> of Cells in all their ten years of sophisticated evolution and variety
> (no, not your stupid pet trick), and as a demo project an entire
> cells-driven GUI, probably a port of my new Celtk (+ Cells Tk) work, all
> in a language without macros, without special variables, with a more
> modest OO system, and limited first class functions... oh, I think we'll
> keep him busy. :)

I did not know all this. The list above does sound like a full summer of
work ;)

I assumed that PyCells referred to the core dependency tracking module
which (even now) does not sound like a such huge task, especially when one
has the reference implementation ;-)

As I said above, I was mostly concerned with exploring how the data flow
system would work, so I haven't implemented the various different types of
cells in Cells. So there would obviously be a bit more work if one is
implementing all that, but still, two caffeine and youth powered student
weeks can achieve a lot ;-)

What "your stupid pet trick" would be referring to? The use of the
__getattribute__ method (which is quite close to SLOT-VALUE-USING-CLASS),
or the use of @decorator syntax to reduce typing and redundancy?

> Now since you are such a genius, maybe you can help with something. Trust
> me on this: this is one place where macros would be able to hide a ton of
> implementation wiring it does no one any good to look at, and actually
> turns into a maintenance nightmare whenever Cells might get revised.

I'd probably use decorators a lot, since they let you play around with
function objects. If they are suitably chosen and designed, the interface
should stay pretty stable even while the wiring is changed.

(Decorator background:

The regular python Python function definition would be
as follows:

def foo(x,y,z):
  ...

would be in CL (if CL were Lisp-1 with symbol-function-or-value) more or less:

(setf (symbol-function-or-value 'foo) (lambda (x y z) ...)

The decorated syntax would be:

@magic_decorator
def foo(x,y,z):
  ...

and the translation:
(setf (symbol-function-or-value 'foo)
 (funcall magic-decorator #'(lambda (x y z)
  ...)))
)
-- 
Lasse Rasinen
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Decrypt DES by password

2006-05-15 Thread Paul Rubin
Thomas Dybdahl Ahle <[EMAIL PROTECTED]> writes:
> byte[] array2 = bytes1.CryptDeriveKey("DES", "MD5", 0, array1);
> > Anybody know how to do this in python?

I'm not aware of a standard that says how CryptDeriveKey is supposed
to work.  Or rather, there are multiple possible standard ways to do
it.  If you can find an exact specification, or C# source code that
does it, it will probably be straightforward to reimplement in Python.

If you want to just do something generic and don't need to
interoperate with a C# application that uses CryptDeriveKey, the
following should be ok:

   import hmac
   password = 'the big sekrit password goes here'

   key1 = hmac.HMAC(password, '1').digest()[:8]   # get 8 bytes

And if you need additional keys, such as for triple DES:

   key2 = hmac.HMAC(password, '2').digest()[:8]   # get 8 bytes
   key3 = hmac.HMAC(password, '3').digest()[:8]   # get 8 bytes

If you want to be fancier you could try PKCS5 KDF2:

  http://www.rsasecurity.com/rsalabs/node.asp?id=2127

CryptDeriveKey may in fact be doing something like this.
-- 
http://mail.python.org/mailman/listinfo/python-list


common practice for creating utility functions?

2006-05-15 Thread John Salerno
Just a quickie for today: Is it common (and also preferred, which are 
two different things!) to create a function that has the sole job of 
calling another function?

Example: for fun and exercise, I'm creating a program that takes a quote 
and converts it into a cryptogram. Right now I have four functions:

convert_quote -- the main function that starts it all
make_code -- makes and returns the cryptogram
make_set -- called from make_code, converts the quote into a set so each 
letter gets only one coded letter
test_sets -- makes sure a letter isn't assigned to itself

So my first question is this: should I make a Cryptogram class for this, 
or are functions fine? If the latter, then back to my original point: 
can I do something like this:

def convert_quote(quote):
 return make_code(quote)

Or does it not make sense to have a function just call another function?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A critic of Guido's blog on Python's lambda

2006-05-15 Thread Ken Tilton


Ken Tilton wrote:
> 
> 
> Ben wrote:
> 
>>
>> Nothing you have described sounds that complicated, and you never come
>> up with concrete objections to other peoples code (apart that it took
>> 10 years to write in Lisp, so it must be really hard)
> 
> 
> Oh, now I have to spend an hour dissecting any code you people toss-off 
> that does no more than pick the low-hanging fruit? I do not spend enough 
> time on Usenet already? :)

I want to clarify something. I did look at the code. It was the same 
thing we had with Cells after four-five hours. Yet the author admitted 
he had looked at the Cells source, so he should have known he had not 
implemented, inter alia, synapses, kid-slotting, ephemerals, optional 
laziness, and worst of all he had omitted the data integrity mechanism 
encapsulated by with-integrity. In the next exchange we discover he 
missed the ability to author Python instances individually while 
mistakenly thinking it was impossible.

Exactly how much time am I supposed to spend on someone not willing to 
spend enough time to understand Cells? I recognize, tho, a kindred 
spirit more interested in writing their own code than reading and 
understanding someone else's. :)

You too are more eager to flame me over misperceived slights to The 
Sacred Python than in Python having a wicked cool constraints package, 
and I am wasting too much time on you. I recognize, tho, a fellow Usenet 
dlamewar enthusiast. :)

kenny

-- 
Cells: http://common-lisp.net/project/cells/

"Have you ever been in a relationship?"
Attorney for Mary Winkler, confessed killer of her
minister husband, when asked if the couple had
marital problems.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Large Dictionaries

2006-05-15 Thread Paul McGuire
"Claudio Grondi" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Chris Foote wrote:
> > Hi all.
> >
> > I have the need to store a large (10M) number of keys in a hash table,
> > based on a tuple of (long_integer, integer).  The standard python
> > dictionary works well for small numbers of keys, but starts to
> > perform badly for me inserting roughly 5M keys:
> >
> > # keys   dictionary  metakit   (both using psyco)
> > --   --  ---
> > 1M8.8s 22.2s
> > 2M   24.0s 43.7s
> > 5M  115.3s105.4s
> >
> > Has anyone written a fast hash module which is more optimal for
> > large datasets ?
> >
> > p.s. Disk-based DBs are out of the question because most
> > key lookups will result in a miss, and lookup time is
> > critical for this application.
> >
> > Cheers,
> > Chris
> Python Bindings (\Python24\Lib\bsddb vers. 4.3.0) and the DLL for
> BerkeleyDB (\Python24\DLLs\_bsddb.pyd vers. 4.2.52) are included in the
> standard Python 2.4 distribution.
>
> "Berkeley DB was  20 times faster  than other databases.  It has the
> operational speed of  a main memory database, the startup and  shut down
> speed of a  disk-resident database, and does not have the  overhead  of
> a client-server inter-process communication."
> Ray  Van Tassle,  Senior  Staff Engineer, Motorola
>
> Please let me/us know if it is what you are looking for.
>
> Claudio

sqlite also supports an in-memory database - use pysqlite
(http://initd.org/tracker/pysqlite/wiki) to access this from Python.

-- Paul


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


Re: do/while structure needed

2006-05-15 Thread John Salerno
Terry Reedy wrote:
> "John Salerno" <[EMAIL PROTECTED]> wrote in message 
> news:[EMAIL PROTECTED]
>> Thanks, that looks pretty good. Although I have to say, a do/while
>> structure is the much more obvious way. I wonder why it hasn't been
>> added to the language.
> 
> Been suggested many times, been considered, and rejected.  Easily similated 
> with while True: ... if exit_condition: break which also solves 'loop and a 
> half' problem, which is more common. Requires new keyword for little gain. 
> Etc.

I know, I remember a discussion about it a while back. But IMO, at 
least, a do/while structure seems much cleaner and more readable than a 
while loop with an embedded if and break statement.
-- 
http://mail.python.org/mailman/listinfo/python-list


Dr. Dobb's Python-URL! - weekly Python news and links (May 15)

2006-05-15 Thread Peter Otten
QOTW:  "It seems if you lurk here [on comp.lang.python] long enough you 
eventually get all you[r] questions answered without even asking!" - Ted Landis

"We're going to learn from Python. JavaScript is pretty close to Python"
- Brendan Eich


Scott David Daniels shows how to find all occurrences of a string in
another string without using regular expressions.
http://groups.google.com/group/comp.lang.python/msg/f19cdf6de899c755

Learn more about Python string formating by watching James Tauber as he 
develops a simple templating system in twelve steps. 
http://jtauber.com/2006/05/templates.html

Steven Bethard extends optparse's OptionParser to check for positional 
arguments.
http://groups.google.com/group/comp.lang.python/msg/9c363a089d70fcbb

Miki Tebeka and Cameron Laid use the CherryPy web framework and Cheetah 
template engine to build a small addressbook application.
http://www.unixreview.com/documents/s=10075/ur0604h/

Alex Martelli calculates the "length" of a generator. Whether it would
be a good idea to incorporate the approach into the built-in len()
function is discussed in the same thread.
http://groups.google.com/group/comp.lang.python/msg/3ec6fd2119192bd0

Pythonthreads has an interview with Titus Brown, developer of twill, a
tool for automated web crawling and scraping.

http://www.pythonthreads.com/articles/interviews/python-community-is-extremely-active-in-building-agile-testing-tools..html

Brendan Eich, the creator of JavaScript, draws inspiration from
Python for the evolution of his language.
http://www.pcwelt.de/news/englishnews/137850/index.html



Everything Python-related you want is probably one or two clicks away in
these pages:

Python.org's Python Language Website is the traditional
center of Pythonia
http://www.python.org
Notice especially the master FAQ
http://www.python.org/doc/FAQ.html

PythonWare complements the digest you're reading with the
marvelous daily python url
 http://www.pythonware.com/daily
Mygale is a news-gathering webcrawler that specializes in (new)
World-Wide Web articles related to Python.
 http://www.awaretek.com/nowak/mygale.html
While cosmetically similar, Mygale and the Daily Python-URL
are utterly different in their technologies and generally in
their results.

For far, FAR more Python reading than any one mind should
absorb, much of it quite interesting, several pages index
much of the universe of Pybloggers.
http://lowlife.jp/cgi-bin/moin.cgi/PythonProgrammersWeblog
http://www.planetpython.org/
http://mechanicalcat.net/pyblagg.html

comp.lang.python.announce announces new Python software.  Be
sure to scan this newsgroup weekly.

http://groups.google.com/groups?oi=djq&as_ugroup=comp.lang.python.announce

Python411 indexes "podcasts ... to help people learn Python ..."
Updates appear more-than-weekly:
http://www.awaretek.com/python/index.html

Steve Bethard, Tim Lesher, and Tony Meyer continue the marvelous
tradition early borne by Andrew Kuchling, Michael Hudson and Brett
Cannon of intelligently summarizing action on the python-dev mailing
list once every other week.
http://www.python.org/dev/summary/

The Python Package Index catalogues packages.
http://www.python.org/pypi/

The somewhat older Vaults of Parnassus ambitiously collects references
to all sorts of Python resources.
http://www.vex.net/~x/parnassus/

Much of Python's real work takes place on Special-Interest Group
mailing lists
http://www.python.org/sigs/

Python Success Stories--from air-traffic control to on-line
match-making--can inspire you or decision-makers to whom you're
subject with a vision of what the language makes practical.
http://www.pythonology.com/success

The Python Software Foundation (PSF) has replaced the Python
Consortium as an independent nexus of activity.  It has official
responsibility for Python's development and maintenance.
http://www.python.org/psf/
Among the ways you can support PSF is with a donation.
http://www.python.org/psf/donate.html

Kurt B. Kaiser publishes a weekly report on faults and patches.
http://www.google.com/groups?as_usubject=weekly%20python%20patch

Although unmaintained since 2002, the Cetus collection of Python
hyperlinks retains a few gems.
http://www.cetus-links.org/oo_python.html

Python FAQTS
http://python.faqts.com/

The Cookbook is a collaborative effort to capture useful and
interesting recipes.
http://aspn.activestate.com/ASPN/Cookbook/Python

Among several Python-oriented RSS/RDF feeds available are

Re: Tabs versus Spaces in Source Code

2006-05-15 Thread Edward Elliott
Brian Quinlan wrote:

> The problem with tabs is that people use tabs for alignment e.g.
> 
> def foo():
>->query = """SELECT *
>->  ->  ->   FROM sometable
>->  ->  ->   WHERE condition"""

Sure it's a problem.  When programmers do bad things, what is your response? 
Slap his nose and say 'Bad, don't do that'?  Or take away his toys so he
can't do that?  Therein lies your answer to tabs or spaces.  Both are
rational.

> Of course, a very disciplined group of people could be trained to
> never use tabs except to align with the current block level but, in
> practice, that doesn't work. Therefore tabs are bad.

Consistency is always hard for people and easy for machines.  If you make it
a machine task instead of a people task, it can easily work in practice,
just like Python easily enforces no mixing of tabs and spaces with the -tt
flag.  Get editors that use different (background) colors for tabs and
spaces so you can easily pick them out even when the above alignment
problem isn't noticeable.  Use editors that automatically pick the right
character when indenting: tabs to the level of indentation of the current
block, then spaces afterward for line continuations.  Run the code through
parsers that detect and flag inconsistencies on check-in.

If such tools are lacking, use substitutes in the meantime.  Don't allow any
code to be checked in where a line departs more than one tab indentation
level from its neighbors.  It's not perfect, but it eliminates the worst
offenses.  Good enough often is.

Not saying you should do this, just pointing out how tabs are viable.

-- 
Edward Elliott
UC Berkeley School of Law (Boalt Hall)
complangpython at eddeye dot net
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: keyword help in Pythonwin interpreter

2006-05-15 Thread BartlebyScrivener
Peter,

I filed a bug report.

Thanks,

rick

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


Re: Tabs versus Spaces in Source Code

2006-05-15 Thread gregarican
Peter Decker wrote:

> Funny, I was going to say that the problem is when the author prefers
> a font with a differntly-sized space. Some of us got rid of editing in
> fixed-width fonts when we left Fortran.

Don't know what all of the hub-bub here is regarding tab/space
indentation. My punched cards break down such matters as in quite fine
fashion :-/

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


Re: Large Dictionaries

2006-05-15 Thread Maric Michaud
Le Lundi 15 Mai 2006 19:24, Roy Smith a écrit :
> d = {}
> d.reserve (10*1000*1000)

d={}.fromkeys(xrange(5*10**6)) ?

-- 
_

Maric Michaud
_

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tabs versus Spaces in Source Code

2006-05-15 Thread Peter Decker
On 5/15/06, Brian Quinlan <[EMAIL PROTECTED]> wrote:

> The problem with tabs is that people use tabs for alignment e.g.
>
> def foo():
>->query = """SELECT *
>->  ->  ->   FROM sometable
>->  ->  ->   WHERE condition"""
>
> Now I change my editor to use 8-space tabs and the code is all messed
> up. Of course, a very disciplined group of people could be trained to
> never use tabs except to align with the current block level but, in
> practice, that doesn't work. Therefore tabs are bad.

And those of us who hate cutesy alignment like that think that people
who do it are therefore bad.

Spaces look like crap, too, when using proportional fonts.

-- 

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


Re: do/while structure needed

2006-05-15 Thread Terry Reedy

"John Salerno" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Thanks, that looks pretty good. Although I have to say, a do/while
> structure is the much more obvious way. I wonder why it hasn't been
> added to the language.

Been suggested many times, been considered, and rejected.  Easily similated 
with while True: ... if exit_condition: break which also solves 'loop and a 
half' problem, which is more common. Requires new keyword for little gain. 
Etc.

tjr



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


Re: Tabs versus Spaces in Source Code

2006-05-15 Thread Brian Quinlan
Edward Elliott wrote:
> Tab is not 4 spaces.  Tab is 1 level of indentation.  The confusion that
> tabs equals some fixed width, or can/should be set to some fixed width, is
> the entire problem hampering their use.  It implies that conversion between
> tabs and spaces is straightforward when it is not.  They are not comparable
> entities.

The problem with tabs is that people use tabs for alignment e.g.

def foo():
   ->query = """SELECT *
   ->  ->  ->   FROM sometable
   ->  ->  ->   WHERE condition"""

Now I change my editor to use 8-space tabs and the code is all messed 
up. Of course, a very disciplined group of people could be trained to 
never use tabs except to align with the current block level but, in 
practice, that doesn't work. Therefore tabs are bad.

Cheers,
Brian
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: saving file permission denied on windows

2006-05-15 Thread Larry Bates
[EMAIL PROTECTED] wrote:
> I'm making a small program which takes a folder with images and
> generates optimized normal-sized images and thumbnails using Python
> Imaging Lbrary (PIL). The problem is here:
> 
> os.mkdir(self.output)
> 
> img = Image.open(os.path.join(self.dir,file))
> img = img.resize(self.imgSize)
> # and here comes the error
> img.save(self.output, "JPEG", optimize=1)
> 
> IOError: [Errno 13] Permission Denied
> "D:\\Szymek\\python\\pythumb\\images\\proba"
> 
> I don't know what's going on, I didn't have any problems with it in the
> past. I tried to save it to a pre-made directory but the effect is the
> same, so I don't think it's connected with os.mkdir.
> 
I think you have a logic problem.

You can't save to a folder name, you must save to a file.  Note
that O/S is saying that permission is denied to the folder name
stored in self.output:

"D:\\Szymek\\python\\pythumb\\images\\proba"

You most likely meant:

img.save(os.path.join(self.output, file), "JPEG", optimize=1)

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


Re: Large Dictionaries

2006-05-15 Thread Roy Smith
Aahz <[EMAIL PROTECTED]> wrote:
> Don't forget that adding keys requires resizing the dict, which is a
> moderately expensive operation.

My guess would be that each resize grows the table by a constant
factor, which IIRC, works out to amortized O(n).  It's not as good as
creating the dict the right size in the first place, but it's really
not that bad.  Somewhat more troubling is that it can lead to memory
fragmentation problems.

I don't understand why Python doesn't have a way to give a size hint
when creating a dict.  It seems so obvious to be able to say "d = dict
(size=10*1000*1000)" if you know beforehand that's how many keys
you're going to add.

Looking at the docs, I'm astounded to discover that you can indeed do
exactly that, but you get {size:1000}.  I am befuddled as to why
people thought creating a dict by keyword would be a useful thing to
do, but left out (and, indeed, eliminated the chance to add the syntax
later) the obviously useful ability to hint at the size.  I suppose we
could still add:

d = {}
d.reserve (10*1000*1000)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A critic of Guido's blog on Python's lambda

2006-05-15 Thread Ken Tilton


Ben wrote:
> 
> Nothing you have described sounds that complicated, and you never come
> up with concrete objections to other peoples code (apart that it took
> 10 years to write in Lisp, so it must be really hard)

Oh, now I have to spend an hour dissecting any code you people toss-off 
that does no more than pick the low-hanging fruit? I do not spend enough 
time on Usenet already? :)

> 
> Why are you running a SoC project for PyCells...

You do not even know what Cells are and have not taken the trouble to 
understand, so i will save my breath. Pythonistas will love PyCells, I 
promise. Please recall that it is not just me, there is a ton of prior 
and current art.

> if you dislike the
> language so much.

There is a difference between disliking a language and thinking PyCells 
might end up persuading folks that macros and/or true lambda might be 
worth the trouble to extend the language.

Try to think a little more precisely, OK? Thx.

> People who do like Python can implement it if they
> need it (which I haven't seen any good examples that they do)
> 
> Please don't force a student to create a macro system just to port a
> system to Python,

You are getting hysterical, sit down, breathe. I asked a question, 
because (unlike you) I can see where this is going. But as you say...

> There are already plenty of ways to hide
> complicated functionality,

I know. And that is why the mentor is a Pythonista, not me. I made a 
simple inquiry as to the options available should Python have trouble 
hiding the wiring. just looking ahead a little (as are the student and 
mentor). Something wrong with thinking ahead a few moves?

You on the other hand have made up your mind about something you admit 
you do not understand, have now ascribed to me a half dozen sentiments I 
do not hold, and are feeling absolutely miserable because you think this 
is a flamewar.

No, we are just discussing language synatx and how it impacts language 
semantics, which has led inadvertently to a few of us starting an SoC 
project to put together a Python version of a very successful dataflow 
hack I did for Lisp.

I use it every day, and it just plain makes me smile. I wrote more code 
than you can imagine Before Cells, and have now used them intensively 
and in more ways than you can imagince since. Even if the wiring cannot 
be hidden, the productivity win will be trememndous. Note that this 
translates ineluctably to "programming will be more fun".

Even you will love them.

:)

kenny

-- 
Cells: http://common-lisp.net/project/cells/

"Have you ever been in a relationship?"
Attorney for Mary Winkler, confessed killer of her
minister husband, when asked if the couple had
marital problems.
-- 
http://mail.python.org/mailman/listinfo/python-list


ANN: Vancouver Python Workshop

2006-05-15 Thread Brian Quinlan
Vancouver Python Workshop
=

Building on the huge success of the 2004 Vancouver Python Workshop, the
Vancouver Python and Zope User Group is pleased to announce the 2006
Vancouver Python Workshop.

The conference will begin with keynote addresses on August 4st. Further
talks (and tutorials for beginners) will take place on August 5th and
6th. The Vancouver Python Workshop is a community organized conference
designed for both the beginner and for the experienced Python programmer
with:

  * tutorials for beginning programmers
  * advanced lectures for Python experts
  * case studies of Python in action
  * after-hours social events
  * informative keynote speakers
  * tracks on multimedia, Web development, education and more


More information see: http://www.vanpyz.org/conference/
or contact Brian Quinlan at: [EMAIL PROTECTED]

Vancouver
=

In addition to the opportunity to learn and socialize with fellow
Pythonistas, the Vancouver Python Workshop also gives visitors the
opportunity to visit one of the most extraordinary cities in the world
(1). For more information about traveling to Vancouver, see:

http://www.vanpyz.org/conference/vancouver.html
http://www.tourismvancouver.com
http://en.wikipedia.org/wiki/Vancouver

Important dates
===

Talk proposals accepted: May 15th to June 15th
Early registration (discounted): May 22nd to June 30th
Normal registration: from July 1st
Keynotes: August 4th
Conference and tutorial dates: August 5th and 6th

(1) http://news.bbc.co.uk/2/hi/business/2299119.stm

Cheers,
Brian





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


Re: Tabs versus Spaces in Source Code

2006-05-15 Thread Edward Elliott
Harry George wrote:

> This has been discussed repeatedly, and the answer is "If you only
> work alone, never use anyone else's code and no one ever uses your
> codes, then do as you please. 

The answer is "Do what works best for your project".  Smart people can agree
on and use whatever convention they want without trouble.  The key is
consistency.

> Otherwise use tab-is-4-spaces." 

Tab is not 4 spaces.  Tab is 1 level of indentation.  The confusion that
tabs equals some fixed width, or can/should be set to some fixed width, is
the entire problem hampering their use.  It implies that conversion between
tabs and spaces is straightforward when it is not.  They are not comparable
entities.
 
> When you do Agile Programming with people using emacs, vim, nedit,
> xedit, wordpad, eclipse, and who knows what else, the 4-spaces rule is
> necessary for survival.

IOW reward programmers for being sloppy and using poor tools.  Anyone who
programs in wordpad/xedit has far bigger problems than worrying about tabs
vs spaces (as do projects who let people program in wordpad/xedit). 
Editors which are designed for programming handle tabs and spaces cleanly.

> The reason is simple: People get confused, and accidentally get the
> wrong tab indents when they move among editors or among settings on
> the same editor.  

Sounds like PEBCAK to me. :) If everyone uses tabs for indent, then it
doesn't matter if Joe's vim showed them as 3 spaces while Mary's emacs
showed them at 6.  You can't get the 'wrong tab indents' when everything is
a tab indent.  Mixing tabs and spaces is where you get problems.

> In most languages this is an irritation, requiring 
> some cleanup.  In Python it is a disaster requiring re-inventing the
> coded algorithms.

Use the -tt flag to the Python interpreter and you'll catch inconsistencies
immediately.  Voila, no disaster.  Again, you're not complaining about
using tabs, you're complaining about mixing tabs and spaces.  I completely
agree with you that the latter is way too much hassle to even attempt.

All I'm saying is that using tabs on their own is perfectly viable and a bit
cleaner.  I'm not trying to force that preference on anyone else, just get
everyone to recognize that one is just as rational and salubrious as the
other.

-- 
Edward Elliott
UC Berkeley School of Law (Boalt Hall)
complangpython at eddeye dot net
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding yesterday's date with datetime

2006-05-15 Thread Michael J. Fromberger
In article <[EMAIL PROTECTED]>,
 "Thierry Lam" <[EMAIL PROTECTED]> wrote:

> Is there an easy way to determine the yesterday's date(year-month-day)
> from the python datetime library if I know today's date?

from datetime import datetime, timedelta

today = datetime.today()
yesterday = today - timedelta(1)

# See the .month, .day, and .year fields of yesterday

Cheers,
-M

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


Re: Large Dictionaries

2006-05-15 Thread Aahz
In article <[EMAIL PROTECTED]>,
Roy Smith  <[EMAIL PROTECTED]> wrote:
>In article <[EMAIL PROTECTED]>, Chris Foote <[EMAIL PROTECTED]> 
>wrote:
>> 
>> I have the need to store a large (10M) number of keys in a hash table,
>> based on a tuple of (long_integer, integer).  The standard python
>> dictionary works well for small numbers of keys, but starts to
>> perform badly for me inserting roughly 5M keys:
>> 
>> # keys   dictionary  metakit   (both using psyco)
>> --   --  ---
>> 1M8.8s 22.2s
>> 2M   24.0s 43.7s
>> 5M  115.3s105.4s
>
>Are those clock times or CPU times?

And what are these times measuring?  Don't forget that adding keys
requires resizing the dict, which is a moderately expensive operation.
Once the dict is constructed, lookup times should be quite good.
-- 
Aahz ([EMAIL PROTECTED])   <*> http://www.pythoncraft.com/

"I saw `cout' being shifted "Hello world" times to the left and stopped
right there."  --Steve Gonedes
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tabs versus Spaces in Source Code

2006-05-15 Thread Peter Decker
On 5/15/06, Chris Klaiber <[EMAIL PROTECTED]> wrote:

> The problem comes when the author prefers a smaller tab width than what my
> editor is set to. Sure, I could change it for that file, but what if I'm
> reading a whole directory? Sure, I could change the default setting in my
> editor, but what if I'm browsing multiple projects in the same day? Sure, I
> could find a way to set the tab width based on the directory I'm currently
> in, but by now I'm annoyed and simply replacing tabs with spaces is a far
> simpler solution that requires zero configuration on my part.

Funny, I was going to say that the problem is when the author prefers
a font with a differntly-sized space. Some of us got rid of editing in
fixed-width fonts when we left Fortran.

-- 

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


Re: py on windows Mobile 5.0?

2006-05-15 Thread Gonzalo Monzón
Bell, Kevin escribió:

>Does anyone know if/how to go about using python on a windows mobile 5.0
>PDA?
>
>Kevin
>
>
>  
>
Hi Bell,

Yes, there's PythonCE (Python 2.4.3 port) for Windows CE / ARM, and 
seems to run with mobile 5.  Please search in the pythonce mail list and 
you'll got the answers about how to go. I attach here the last revision 
announcement in the maillist (May 1, 2006):

A new bug fix release of Python for Windows CE is available. Download it 
here:

http://sourceforge.net/project/showfiles.php?group_id=104228

The release notes are linked from this page, but a direct link is:

http://sourceforge.net/project/shownotes.php?release_id=413801&group_id=104228


Luke
___
PythonCE mailing list
PythonCE@python.org
http://mail.python.org/mailman/listinfo/pythonce



Regards,
Gonzalo Monzón
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List and order

2006-05-15 Thread Peter Otten
Nic wrote:

> The only problem is that from:
> 12
> 22
> 21
> In spite of writing
> 12 12 22
> it writes
> 12 21 22
> Do you know how is it possible to delete also this last trouble?

I thought that the two 12 were a typo, but it seems you want to reorder the
nodes inside an edge, too. Here's a fix that normalizes the edge before
sorting the list of edges:

edges = [sorted((ddeg[u], ddeg[v])) for u, v in G.edges()]
edges.sort()
for a, b in edges:
print a, b,
print

Peter


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


help with perl2python regular expressions

2006-05-15 Thread Lance Hoffmeyer
Hey all,

I am trying to convert some old perl scripts I have to python.
Here is a snippit of the regex I used in perl.  I am trying to
figure out the equivalent code in python.  Can someone help?

my $file =$Word->ActiveDocument->Content->Text;
$file =~ /TABLE\s+1.*?JCP.*?MEAN.*?(?:(\d{1,3}\.\d\d)\s+){1}/s;
my $P05A008 = $1;

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


Re: Finding yesterday's date with datetime

2006-05-15 Thread [EMAIL PROTECTED]
Try something like this...

HTH.  A.


from datetime import *

d1 = datetime( year=2006, month=5, day=15)
d2 = datetime.now()

for d in [d1,d2]:
yest = d - timedelta(days =1 )

print "%s -> %s" % (d, yest)

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


Re: List and order

2006-05-15 Thread Nic
Many thanks.
Both the cases are OK.
The only problem is that from:
12
22
21
In spite of writing
12 12 22
it writes
12 21 22
Do you know how is it possible to delete also this last trouble?
Thanks a bunch,
Nic

"Peter Otten" <[EMAIL PROTECTED]> ha scritto nel messaggio 
news:[EMAIL PROTECTED]
> Nic wrote:
>
>> I tried to insert and execute the code, but the following error happens:
>>
>> Traceback (most recent call last):
>>   File "grafodna.py", line 10, in ?
>> edges.sort(key = lambda u, v: (ddeg(u), ddeg(v)))
>> TypeError: () takes exactly 2 arguments (1 given)
>>
>> Do you know how is it possible to delete it?
>
> Note that
>
> lambda a, b: ...
>
> takes two arguments while
>
> lambda (a, b): ...
>
> takes one argument which must be a sequence (list, string, generator,...) 
> of
> two items.
>
> So the above should probably be
>
> edges = list(G.edges())
> edges.sort(key=lambda (u, v): (ddeg[u], ddeg[v]))
> for u, v in edges:
>print ddeg[u], ddeg[v],
> print
>
>
> Here's how I would do it:
>
> edges = [(ddeg[u], ddeg[v]) for u, v in G.edges()]
> edges.sort()
> for a, b in edges:
>print a, b,
> print
>
> (all untested)
>
> Peter 


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


  1   2   >