Re: Why do Pythoneers reinvent the wheel?

2005-09-10 Thread Tim Daneliuk
Stefano Masini wrote:

SNIP

 I wonder how many people (including myself) have implemented their own
 versions of such modules, at least once in their pythonic life. I
 indeed have my own odict (even same name! :). My own pathutils
 (different name, but same stuff). My own validate... and so forth.

As someone who implemented their own configuration mini-language
with validation, blah, blah, blah (http://www.tundraware.com/Software/tconfpy/)
I can give you a number of reasons - all valid for different people at
different times:

1) The existing tool is inadequate for the task at hand and OO subclassing
is overrated/overhyped to fix this problem.  Even when you override
base classes with your own stuff, you're still stuck with the larger
*architecture* of the original design.   You really can't subclass
your way out of that, hence new tools to do old things spring into
being.

2) It's a learning exercise.

3) You don't trust the quality of the code for existing modules.
(Not that *I* have this problem :-p  but some people might.)


-- 

Tim Daneliuk [EMAIL PROTECTED]
PGP Key: http://www.tundraware.com/PGP/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python compiled?

2005-09-10 Thread Tim Roberts
billiejoex [EMAIL PROTECTED] wrote:

Hi all. I'm sorry for a noob question like this but I'll try to ask it 
anyway.
One of the greatest problem that may discourage a new user to choose Python 
language is it's interpreted nature.

I doubt it.  C#, VB.NET, VBscript, Javascript and Perl have not suffered
from being interpreted.
-- 
- Tim Roberts, [EMAIL PROTECTED]
  Providenza  Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: nested tuples

2005-09-10 Thread Tim Roberts
Luis P. Mendes [EMAIL PROTECTED] wrote:

I'm trying to solve this problem:

suppose I'm reading a csv file and want to create a tuple of all those
rows and values, like ((row1value1, row1value2, row1value3),(row2value1,
row2value2, row2value3),..., (rowNvalue1, rowNvalue2, rowNvalue3))

I haven't found the way to do it just using tuples.  How can I do it?

Nevertheless, I can solve it like this:
a=[]

for row in reader:
~   elem = (row[0],row[1],row[2])
~   a.append(elem)

which will result in a list of tuples: [(row1value1, row1value2,
row1value3),(row2value1, row2value2, row2value3),..., (rowNvalue1,
rowNvalue2, rowNvalue3)]

Then, I get what I want with tuple(a).

Why?  What is it about the list of tuples that you don't like?
Philosophically, it's more in line with Guido's separation of list and
tuple.
-- 
- Tim Roberts, [EMAIL PROTECTED]
  Providenza  Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do Pythoneers reinvent the wheel?

2005-09-10 Thread Stefano Masini
On 10 Sep 2005 02:10:59 EDT, Tim Daneliuk [EMAIL PROTECTED] wrote:
 As someone who implemented their own configuration mini-language
 with validation, blah, blah, blah 
 (http://www.tundraware.com/Software/tconfpy/)

Well, a configuration mini language with validation and blahs is not
exactly what I would call _simple_... :) so maybe it doesn't even fit
into my idea of testing-stdlib, or quick and dirty section of the
manual (see my other post).
But certainly it would be worth mentioning in the list of available
solutions under the subsection Configuration files handling.

 1) The existing tool is inadequate for the task at hand and OO subclassing
 is overrated/overhyped to fix this problem.  Even when you override
 base classes with your own stuff, you're still stuck with the larger
 *architecture* of the original design.   You really can't subclass
 your way out of that, hence new tools to do old things spring into
 being.

That's true, but usually only when the original design if too simple
comparing to the complexity of the problem. Instead a very general
solution can usually be subclassed to easily handle a simpler problem.
You still have to actually understand the general and complex design
in order to be able to write subclasses, so maybe one can be tempted
to punt on it, and write its own simple solution. But in this case it
would just be enough to propose a few solutions in the testing-stdlib:
a) one simple implementation for simple problems, easy to understand,
but limited.
b) one complex implementation for complex problems, 
c) one simplified implementation for simple problems, easy to
understand, but subclassed from a complex model, that leaves room for
more understanding and extension just in case one needs more power.

I fully understand the difficulty of reusing code, as it always forces
you to a learning curve and coming to compromises. But I've also
wasted a lot of time reinventing the wheel and later found stuff I
could have happily lived with if I only had known.

 2) It's a learning exercise.

Well, so we might as well learn a little more and rewrite os.path, the
time module and pickle. Right? :)

 3) You don't trust the quality of the code for existing modules.
 (Not that *I* have this problem :-p  but some people might.)

That's a good point, but it really boils down to being a wise
programmer on one side, being able to discern the Good from the Bad,
and an active community on the other side, able to provide good
solutions and improve them.
If either one is missing, then a lot of bad stuff can happen, and we
can't really take community decisions basing on the assumption that
programmers won't be able to understand, or that the community won't
be able to provide. So we might as well assume that we have good
programmers and an active community.
Which I think is true, by the way!
So, let's talk about a way to more effectively present available
solutions to our good programmers! :)

cheers,
stefano
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python compiled?

2005-09-10 Thread Paul Rubin
Tim Roberts [EMAIL PROTECTED] writes:
 I doubt it.  C#, VB.NET, VBscript, Javascript and Perl have not suffered
 from being interpreted.

Are you kidding?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do Pythoneers reinvent the wheel?

2005-09-10 Thread Tim Daneliuk
Stefano Masini wrote:

 On 10 Sep 2005 02:10:59 EDT, Tim Daneliuk [EMAIL PROTECTED] wrote:
 
As someone who implemented their own configuration mini-language
with validation, blah, blah, blah 
(http://www.tundraware.com/Software/tconfpy/)
 
 
 Well, a configuration mini language with validation and blahs is not
 exactly what I would call _simple_... :) so maybe it doesn't even fit

It's actually not *that* complicated.  Then again, the code is not
as elegant as is might be.

 
1) The existing tool is inadequate for the task at hand and OO subclassing
is overrated/overhyped to fix this problem.  Even when you override
base classes with your own stuff, you're still stuck with the larger
*architecture* of the original design.   You really can't subclass
your way out of that, hence new tools to do old things spring into
being.
 
 
 That's true, but usually only when the original design if too simple
 comparing to the complexity of the problem. Instead a very general
 solution can usually be subclassed to easily handle a simpler problem.
 You still have to actually understand the general and complex design
 in order to be able to write subclasses, so maybe one can be tempted
 to punt on it, and write its own simple solution. But in this case it

The problem is that for a lot of interesting problems, you don't know
the generic big-picture stuff until you've hacked around at small
specific examples. This is one of the deepest flaws in the gestalt of
OO, IMHO. Good OO requires just what you suggest - and understanding of
generics, specific applications, and just what to factor. But in the
early going of new problems, you simply don't know enough. For the
record, I think Python is magnificent both in allowing you to work
quickly in the poking around stage of things, and then later to create
the more elegant fully-featured architectures.

One other point here: In the commericial world, especially, software
tends to be a direct reflection of the organization's *processes*.
Commercial institutions distinguish themselves from one another (in an
attempt to create competitive advantage) by customizing and tuning these
business processes - well, the successful companies do, anyway.  For
example, Wal-Mart is really a supply chain management company, not a
consumer goods retailer.  It is their supply chain expertise and IT
systems that have knocked their competitors well into 2nd place.  And
here's the important point: These distinguishing business processes are
unique and proprietary *by intent*. This means that generic software
frameworks are unlikely to serve them well as written. I realize this is
all at a level of complexity above what you had in mind, but it's easy
to forget that a significant portion of the world likes/needs/benefits
from things that are *not* particularly generic.  This is thus reflected
in the software they write.


2) It's a learning exercise.
 
 
 Well, so we might as well learn a little more and rewrite os.path, the
 time module and pickle. Right? :)

I'm not deeply committed to that level of education at the moment :P

 
 
3) You don't trust the quality of the code for existing modules.
(Not that *I* have this problem :-p  but some people might.)


 So, let's talk about a way to more effectively present available
 solutions to our good programmers! :)

Grappa?

-- 

Tim Daneliuk [EMAIL PROTECTED]
PGP Key: http://www.tundraware.com/PGP/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do Pythoneers reinvent the wheel?

2005-09-10 Thread Kay Schluehr
Tim Daneliuk wrote:

 1) The existing tool is inadequate for the task at hand and OO subclassing
 is overrated/overhyped to fix this problem. Even when you override
 base classes with your own stuff, you're still stuck with the larger
 *architecture* of the original design. You really can't subclass
 your way out of that, hence new tools to do old things spring into
 being.

Allthough I do think that you are completely wrong in principle there
is some true point in your statement: refactoring a foreign ill
designed tool that nevertheless provides some nice functionality but is
not mentioned for being extendable by 3-rd party developers is often
harder than writing a nice and even though inextendable tool on your
own. That's independent of the language allthough I tend to think that
C and Python programmers are more alike in their crude pragmatism than
Java or Haskell programmers ( some might object that it is a bit unfair
to equate Java and Haskell programmers, because no one ever claimed
that the latter need code-generators and no intelligence to do their
work ).

Kay

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


Re: Create new instance of Python class in C

2005-09-10 Thread Sybren Stuvel
phil hunt enlightened us with:
 Why do you need to maske lots of copies?

The puzzles are stored in a NxN list of strings. The strings contain
all the numerals that that block can contain. So a 9x9 puzzle contains
81 strings 123456789 when it's empty.

My creation function picks a block that isn't a given, and fixes it to
one of it's possible values. It then tries to solve the puzzle. If it
works, it's done creating the puzzle. If it doesn't work, it starts
over again in a recursive manner.

The solver solves the puzzle in-place. That means that if I want to
keep the original puzzle (the one that could be solved), I have to
make a copy.

 And when you say lots of what numbers do you mean? 100? 100?

That depends a lot. The parts picks a block that isn't a given and
fixes it to one if it's possible values are both randomized.
Sometimes it's 100, sometimes it's 5.

 The reason I ask is I recently wrote a program to solve Sudoku
 puzzles, and IIRC it didn't make copies at all.

My solver doesn't create copies either. The creator does.

I think I'll change my algorithm to just solve the puzzle and don't
care about the original minimal puzzle that could be solved. I'll
create a fully solved puzzle, and then use another routine to remove
givens at random until the required number of givens is left. Of
course, still maintaining solvability.

That last part would require copying, but not as much as in the
current situation.

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


Re: Why do Pythoneers reinvent the wheel?

2005-09-10 Thread Stefano Masini
On 10 Sep 2005 03:16:02 EDT, Tim Daneliuk [EMAIL PROTECTED] wrote:
 frameworks are unlikely to serve them well as written. I realize this is
 all at a level of complexity above what you had in mind, but it's easy
 to forget that a significant portion of the world likes/needs/benefits
 from things that are *not* particularly generic.  This is thus reflected
 in the software they write.

In my opinion this has got more to deal with the open source vs.
proprietary debate, that I wouldn't like to talk about, since it's
somewhat marginal.

What I was pointing out is well summarized in the subject: Why do
Pythoneers reinvent the wheel?
Reinventing the wheel (too much) is Bad for both the open source
community and industry. It's bad for development in general. I got the
feeling that in the specific case of Python the ultimate reason for
this tendency in also the same reason why this language is so much
better that others for, say, fast prototyping and exploration of new
ideas: it's simple.

So, without taking anything out of python, I'm wondering if a richer
and less formal alternative standard library would help forming a
common grounds where programmers could start from in order to build
better and reinvent less.

If such an aid to _general_ problem solving is indeed missing (I might
be wrong) from the current state of python, I don't really think the
reason is related to industry. I would look for reasons elsewhere,
like it beeing difficult to come out with effective decisional support
in an open source community, or something like this. I can certainly
see the challenge of who and how should decide what goes in the
library, and what not.

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


execute commands and return output

2005-09-10 Thread billiejoex
Hi all. I'm searching for a portable (working on *nix and win32) function 
that executes a system command and encapsulate its output into a string.
Searching for the web I found this:

os.popen('command').read()

It is perfect but when che command return an error the funciotn returns an 
empy string.
Does it is possible to return stdout and stderr too?

Best regards



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


Re: List of integers L.I.S. (SPOILER)

2005-09-10 Thread n00m

Bryan Olson wrote:
 Could be. Yet you did write:
 It's incredibly fast!
I just was obliged to exclaim It's incredibly fast!
because I THOUGHT your first version handled ALL TEN
testcases from the input. But the code read from the
*20-lines* input *ONLY 2* its first lines.

Usually they place heavy data testcase(s) at the end
of the (whole) input. Like this:

3
2 3 1
7
4 5 6 1 2 7 3
...
...
...
10
456 2 6789 ... ... ... ... ... 55444 1 ... 234

Surely producing an answer for list [2, 3, 1] will
be incredibly fast for ANY language and for ANY
algorithm.

 My first version bombed for the zero-length sequence. That was a
 mistake, sorry, but it may not be one of their test-cases.
In my turn I can bet there's not an empty sequence testcase in the
input.

 I
 wonder how many of the accepted entries would perform properly.
Info of such kind they keep in secret (along with what the input
data are).

One more thing.
They (the e-judge's admins) are not gods and they don't warrant
that if they put 9 sec timelimit for a problem then this problem
can be solved in all accepted languages (e.g. in Python).

 I never intended to submit this program for competition.
Competition is not quite relevant word here. It just LOOKS as
if it is a regular competetion. There nobody blames anybody.
Moreover, judging on my own experience, there nobody is even
interested in anybody. It's just a fun (but very useful fun).

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


Re: nested tuples

2005-09-10 Thread Luis P. Mendes
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1


|
| Why?  What is it about the list of tuples that you don't like?
| Philosophically, it's more in line with Guido's separation of list and
| tuple.
I'm not saying that I don't like, I was just curious to know if there
was a way to do it using exclusively tuples.

Regards,

Luis Mendes
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDIqgSHn4UHCY8rB8RAps/AJ905JXc5naxJYWLA0JLd0ZaJfQQWACeLXHJ
pLE9nmMH+k81ybbB1Otj0hg=
=2/LC
-END PGP SIGNATURE-
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do Pythoneers reinvent the wheel?

2005-09-10 Thread Tim Daneliuk
Kay Schluehr wrote:

 Tim Daneliuk wrote:
 
 
1) The existing tool is inadequate for the task at hand and OO subclassing
is overrated/overhyped to fix this problem. Even when you override
base classes with your own stuff, you're still stuck with the larger
*architecture* of the original design. You really can't subclass
your way out of that, hence new tools to do old things spring into
being.
 
 
 Allthough I do think that you are completely wrong in principle there
 is some true point in your statement: refactoring a foreign ill
  designed tool that nevertheless provides some nice functionality but is
  not mentioned for being extendable by 3-rd party developers is often
  harder than writing a nice and even though inextendable tool on your own

It has nothing to do with being ill designed, though that too would
pose a (different) problem. It has to do with the fact that all
realworld tools are a tradeoff between pragmatism and generic elegance.
This tradeoff yields a tool/module/library/program with some POV about
what problem it was solving. If the problem you wish to solve is not in
that same space, you can inherit, subclass and do all the usual OO
voodoo you like, you're not going to get clean results.

On a more general note, for all the promises made over 3 decades about
how OO was the answer to our problems, we have yet to see quantum
improvements in code quality and productivity even though OO is now the
thing everyone is supposed to subscribe to. In part, that's because it
is profoundly difficult to see the most generic/factorable pieces of a
problem until you've worked with it for a long time. Once you get past
the a mammal is an animal level of problems, OO starts to
self-destruct pretty quickly as the inheritance hierarchies get so
complex no mere mortal can grasp them all. This is exactly Java's
disease at the moment. It has become a large steaming pile of object
inheritance which cannot be completely grokked by a single person. In
effect, the traditional problem of finding algorithms of appropriate
complexity gets transformed into a what should my inheritance hierarchy
be problem.

IMHO, one of Python's greatest virtues is its ability to shift paradigms
in mid-program so that you can use the model that best fits your problem
space. IOW, Python is an OO language that doesn't jam it down your
throat, you can mix OO with imperative, functional, and list processing
coding models simultaneously.

In my view, the doctrinaire', indeed religious, adherence to OO purity
has harmed our discipline considerably. Python was a nice breath of
fresh air when I discovered it exactly because it does not have this
slavish committment to an exclusively OO model.



-- 

Tim Daneliuk [EMAIL PROTECTED]
PGP Key: http://www.tundraware.com/PGP/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do Pythoneers reinvent the wheel?

2005-09-10 Thread Tim Daneliuk
Stefano Masini wrote:

 On 10 Sep 2005 03:16:02 EDT, Tim Daneliuk [EMAIL PROTECTED] wrote:
 
frameworks are unlikely to serve them well as written. I realize this is
all at a level of complexity above what you had in mind, but it's easy
to forget that a significant portion of the world likes/needs/benefits
from things that are *not* particularly generic.  This is thus reflected
in the software they write.
 
 
 In my opinion this has got more to deal with the open source vs.
 proprietary debate, that I wouldn't like to talk about, since it's
 somewhat marginal.

I think the point I was trying to make was there are times when
a generic factoring of reusable code is unimportant since the code
is so purpose-built that doing a refactoring makes no sense.

 
 What I was pointing out is well summarized in the subject: Why do
 Pythoneers reinvent the wheel?
 Reinventing the wheel (too much) is Bad for both the open source
 community and industry. It's bad for development in general. I got the

I don't share your conviction on this point.  Reinventing the wheel
makes the wheel smoother, lighter, stronger, and rounder.  Well,
it *can* do this.   Of far greater import (I think) is whether
any particular implementation is fit to run across a breadth of
platforms.  To me, a signficant benefit of Python is that I am
mostly able to program the same way across Unix, Windows, Mac
and so on.

SNIP

 If such an aid to _general_ problem solving is indeed missing (I might
 be wrong) from the current state of python, I don't really think the
 reason is related to industry. I would look for reasons elsewhere,
 like it beeing difficult to come out with effective decisional support
 in an open source community, or something like this. I can certainly
 see the challenge of who and how should decide what goes in the
 library, and what not.


This is too abstract for me to grasp - but I admit to be old and feeble ;)

I think what you see today in the standard library are two core ideas:
1) Modules that are more-or-less pass-through wrappers for the common
APIs found in Unix and 2) Modules needed commonly to do the things that
applications do like manipulate data structures or preserve active
objects on backing store. If what you want here is for everyone to agree
on a common set of these and stick exclusively to them, I think you will
be sorely disappointed. OTOH, if someone has a better/faster/smarter
reimplementation of what exists, I think you'd find the community open
to embracing incremental improvement. But there is always going to be
the case of what happened when I wrote 'tconfpy'. The existing
configuration module was nice, but nowhere near the power of what I
wanted, so I wrote something that suited me exactly (well ... sort of,
'tconfpy2' is in my head at the moment).  If the community embraced
it as a core part of their work, I'd be delighted (and surprised), but
I don't need for that to happen in order for that module to have value
to *me*, even though it does not displace the existing stuff.


-- 

Tim Daneliuk [EMAIL PROTECTED]
PGP Key: http://www.tundraware.com/PGP/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: execute commands and return output

2005-09-10 Thread Leif K-Brooks
billiejoex wrote:
 Hi all. I'm searching for a portable (working on *nix and win32) function 
 that executes a system command and encapsulate its output into a string.
 Searching for the web I found this:
 
 os.popen('command').read()
 
 It is perfect but when che command return an error the funciotn returns an 
 empy string.
 Does it is possible to return stdout and stderr too?

Use subprocess:

from subprocess import Popen, PIPE
proc = Popen(['command', 'arg', 'arg'], stdout=PIPE, stderr=PIPE)
return_code = proc.wait()
if return_code == 0:
print Success:\n%s % proc.stdout.read()
else:
print Failure %s:\n%s % (return_code, proc.stderr.read())
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: execute commands and return output

2005-09-10 Thread Fredrik Lundh
billiejoex wrote:

 Hi all. I'm searching for a portable (working on *nix and win32) function 
 that executes a system command and encapsulate its 
 output into a string.
 Searching for the web I found this:

 os.popen('command').read()

 It is perfect but when che command return an error the funciotn returns an 
 empy string.
 Does it is possible to return stdout and stderr too?

see the variations popen2, popen3 and popen4:

http://docs.python.org/lib/os-newstreams.html

or use the subprocess module:

http://docs.python.org/lib/module-subprocess.html

/F 



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


Re: execute commands and return output

2005-09-10 Thread billiejoex
Thank you for your help but I'm searching a different way.
Moreover it doesn't work always (for exaple: try a 'dir' command).
Because of I'm implementing a remote shell  the 
[[os.popen('command').read()]] rapresents the best for me because it can 
also accepts arguments direclty  (for example: 
os.popen('netstat -a -n -o').read() and this is a great advantage.
I was looking at sys.stdout and sys.stderr. Can they be helpful?

Cheers
 Use subprocess:

 from subprocess import Popen, PIPE
 proc = Popen(['command', 'arg', 'arg'], stdout=PIPE, stderr=PIPE)
 return_code = proc.wait()
 if return_code == 0:
print Success:\n%s % proc.stdout.read()
 else:
print Failure %s:\n%s % (return_code, proc.stderr.read()) 


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


Re: execute commands and return output

2005-09-10 Thread Do Re Mi chel La Si Do
Hi !

Look (the doc for) Popen2, Popen3, Popen4  Subprocess

@-salutations

Michel Claveau 


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


Re: execute commands and return output

2005-09-10 Thread Fredrik Lundh
billiejoex wrote:

 Moreover it doesn't work always (for exaple: try a 'dir' command).

why use os.system(dir) when Python already offers things
like os.listdir, os.walk, and glob.glob ?

/F 



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


Re: Python Design Principles

2005-09-10 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote:

 After all, the fact that Python is not strongly typed and is
 interpreted rather than compiled

if you think those are facts, you don't understand how Python
works.

(hint: Python's both strongly typed *and* compiled)

 sacrificing programmer producitivity

the ability to save a newline here and there doesn't affect the
programmer productivity in any way whatsoever.

/F 



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


Re: Why do Pythoneers reinvent the wheel?

2005-09-10 Thread A.M. Kuchling
On Sat, 10 Sep 2005 08:53:24 +0200, 
Stefano Masini [EMAIL PROTECTED] wrote:
 Well, so we might as well learn a little more and rewrite os.path, the
 time module and pickle. Right? :)

And in fact people have done all of these:
os.path: path.py (http://www.jorendorff.com/articles/python/path/)
time: mxDateTime, the stdlib's datetime.
pickle: XML serialization, YAML.

 So, let's talk about a way to more effectively present available
 solutions to our good programmers! :)

PEP 206 (http://www.python.org/peps/pep-0206.html) suggests assembling an
advanced library for particular problem domains (e.g. web programming, 
scientific programming), and then providing a script that pulls the relevant
packages off PyPI.  I'd like to hear suggestions of application domains and
of the packages that should be included.

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


Re: execute commands and return output

2005-09-10 Thread Leif K-Brooks
billiejoex wrote:
 Thank you for your help but I'm searching a different way.
 Moreover it doesn't work always (for exaple: try a 'dir' command).
 Because of I'm implementing a remote shell  the 
 [[os.popen('command').read()]] rapresents the best for me because it can 
 also accepts arguments direclty  (for example: 
 os.popen('netstat -a -n -o').read() and this is a great advantage.

If you really need shell evaluation, try subprocess.Popen('foo',
shell=True) instead.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: global interpreter lock

2005-09-10 Thread Paul Rubin
Michael Sparks [EMAIL PROTECTED] writes:
  But I think to do it on Erlang's scale, Python needs user-level
  microthreads and not just OS threads.  
 
 You've just described Kamaelia* BTW, except substitute micro-thread
 with generator :-) (Also we call the queues outboxes and inboxes, and 
 the combination of a generator in a class with inboxes and outboxes
 components)
* http://kamaelia.sf.net/

I don't see how generators substitute for microthreads.  In your example
from another post:

   class encoder(component):
  def __init__(self, **args):
  self.encoder = unbreakable_encryption.encoder(**args)
  def main(self):
 while 1:
 if self.dataReady(inbox):
data = self.recv(inbox)
encoded = self.encoder.encode(data)
self.send(encoded, outbox)
 yield 1

You've got the main method creating a generator that has its own
event loop that yields after each event it processes.  Notice the kludge

 if self.dataReady(inbox):
data = self.recv(inbox)

instead of just saying something like:

data = self.get_an_event(inbox)

where .get_an_event blocks (i.e. yields) if no event is pending.
The reason for that is that Python generators aren't really coroutines
and you can't yield except from the top level function in the generator.

In that particular example, the yield is only at the end, so the
generator isn't doing anything that an ordinary function closure
couldn't:

   def main(self):
   def run_event():
   if self.dataReady(inbox):
  data = self.recv(inbox)
  encoded = self.encoder.encode(data)
  self.send(encoded, outbox)
   return run_event

Now instead of calling .next on a generator every time you want to let
your microthread run, just call the run_event function that main has
returned.  However, I suppose there's times when you'd want to read an
event, do something with it, yield, read another event, and do
something different with it, before looping.  In that case you can use
yields in different parts of that state machine.  But it's not that
big a deal; you could just use multiple functions otherwise.

All in all, maybe I'm missing something but I don't see generators as
being that much help here.  With first-class continuations like
Stackless used to have, the story would be different, of course.

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


Re: Using Python with COM to communicate with proprietary Windows software

2005-09-10 Thread Andrew MacIntyre
On Fri, 09 Sep 2005 08:36:00 +0200, Thomas Heller [EMAIL PROTECTED]
wrote:

{...}

(I have released and announced this 3 weeks ago, but haven't got a
single feedback.  So it seems the need to access custom interfaces is
very low.)

I have downloaded it and am trying to find the time to play with it 
(unsuccessfully so far).

As someone working with a large, complex, COM library with minimal 
IDispatch support, I'm really looking forward to this.

However, at the moment I'm limited to Python 2.2 and ctypes 0.6.3 (which 
is allowing me to get the job done!!).

Regardless, I thank you for what you have released!

Cheers,
Andrew.

-
Andrew I MacIntyre These thoughts are mine alone...
E-mail: [EMAIL PROTECTED]  (pref) | Snail: PO Box 370
[EMAIL PROTECTED] (alt) |Belconnen ACT 2616
Web:http://www.andymac.org/   |Australia
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: execute commands and return output

2005-09-10 Thread tiissa
billiejoex wrote:
 Hi all. I'm searching for a portable (working on *nix and win32) function 
 that executes a system command and encapsulate its output into a string.
 Searching for the web I found this:
 
 os.popen('command').read()
 
 It is perfect but when che command return an error the funciotn returns an 
 empy string.
 Does it is possible to return stdout and stderr too?

You may want to look at the subprocess [1] module and its Popen class [2].

[1] http://python.org/doc/2.4.1/lib/module-subprocess.html
[2] http://python.org/doc/2.4.1/lib/node234.html

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


Re: Why do Pythoneers reinvent the wheel?

2005-09-10 Thread Fuzzyman

Michael Amrhein wrote:
 Stefano Masini schrieb:
  On 8 Sep 2005 08:24:50 -0700, Fuzzyman [EMAIL PROTECTED] wrote:
 
 What is pythonutils ?
 =
 ConfigObj - simple config file handling
 validate - validation and type conversion system
 listquote - string to list conversion
 StandOut - simple logging and output control object
 pathutils - for working with paths and files
 cgiutils - cgi helpers
 urlpath - functions for handling URLs
 odict - Ordered Dictionary Class
 
 
  Fuzzyman, your post reminded me of something I can't stop thinking
  about. Please don't take this as a critique on your work. I place
  myself on the same side of yours.
  I just wanted to share this thought with everybody had an opinion about it.
 
  I wonder how many people (including myself) have implemented their own
  versions of such modules, at least once in their pythonic life. I
  indeed have my own odict (even same name! :). My own pathutils
  (different name, but same stuff). My own validate... and so forth.
 
  This is just too bad.
  There are a few ares where everybody seems to be implementing their
  own stuff over and over: logging, file handling, ordered dictionaries,
  data serialization, and maybe a few more.
  I don't know what's the ultimate problem, but I think there are 3 main 
  reasons:
  1) poor communication inside the community (mhm... arguable)
  2) lack of a rich standard library (I heard this more than once)
  3) python is such an easy language that the I'll do it myself evil
  side lying hidden inside each one of us comes up a little too often,
  and prevents from spending more time on research of what's available.
  [snip..]
 Did you take a look at pyPI (http://www.python.org/pypi) ?
 At least you'd find another odict ...

Oh right. Where ?

I remember when I started coding in Python (about two years ago) in one
of my first projects I ended up re-implementing some stuff that is in
the standard library. The standard library is *fairly* big - but the
'Python blessed' modules idea sounds good.

I've often had the problem of having to assess multiple third party
libraries/frameworks and decide which of several alternatives is going
to be best for me - without really having the information on which to
base a decision (and nor the time to try them all out). Web templating
and web application frameworks are particularly difficult in this area.

If a module is in the standard library then *most* developers will
*first* use that - and only if it's not suitable look for something
else.

All the best,

Fuzzyman
http://www.voidspace.org.uk/python

 ;-) Michael

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


Re: Why do Pythoneers reinvent the wheel?

2005-09-10 Thread Martin P. Hellwig
Stefano Masini wrote:
cut reinventing wheel example

Although I'm not experienced enough to comment on python stuff itself I 
do know that in general there are 2 reasons that  people reinvent the wheel:
- They didn't know of the existence of the first wheel
- They have different roads
Those reasons can even be combined.

The more difficult it is to create a new wheel the bigger the chance is 
that you:
- Search longer for fitting technologies
- Adapt your road

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


Re: Why do Pythoneers reinvent the wheel?

2005-09-10 Thread Paul Boddie
A.M. Kuchling wrote:
 PEP 206 (http://www.python.org/peps/pep-0206.html) suggests assembling an
 advanced library for particular problem domains (e.g. web programming,
 scientific programming), and then providing a script that pulls the relevant
 packages off PyPI.  I'd like to hear suggestions of application domains and
 of the packages that should be included.

I'm not against pointing people in what I consider to be the right
direction, but PEP 206 seems to be quite the lobbying instrument for
people to fast-track pet projects into the standard library (or some
super-distribution), perhaps repeating some of the mistakes cited in
that document with regard to suitability. Meanwhile, in several areas,
some of the pressing needs of the standard library would remain
unaddressed if left to some kind of Pythonic popularity contest; for
example, everyone likes to dish out their favourite soundbites and
insults about DOM-based XML APIs, but just leaving minidom in the
library in slow motion maintenance mode whilst advocating more
Pythonic APIs doesn't help Python's interoperability with (or
relevance to) the wider development community.

The standard library is all about providing acceptable solutions so
that other people aren't inclined or forced to write their own. Every
developer should look at their repertoire of packages and consider
which ones wouldn't need to exist if the standard library had been
better. For me, if there had been a decent collection of Web
application objects in the standard library, I wouldn't have created
WebStack; if I didn't have to insist on PyXML and then provide patches
for it in order to let others run software I created, I wouldn't have
created libxml2dom.

PEP 206 is an interesting idea but dangerous because as a PEP it
promotes a seemingly purely informational guide to some kind of edict,
and (speaking from experience) since a comprehensive topic guide to any
reasonable number of packages and solutions is probably too much work
that no-one really wants to do anyway, the likelihood of subjective
popularity criteria influencing the selection of presented software
means that the result may be considerably flawed. Although I see that a
common trend these days is to form some kind of narrow consensus, hype
it repeatedly and, in the name of one cause, to push another agenda
entirely, all whilst ignoring the original problems that got people
thinking in the first place, I am quite sure that as a respected Python
contributor this was not a goal of yours in writing the PEP. However,
we should all be aware of the risks of picking favourites, even if the
level of dispute around those favourites is likely to be much lower for
some packages than for others.

This overly harsh criticism really brings me to ask: what happened to
the maintenance and promotion of the python.org topic guides? Or do
people only read PEPs these days?

Paul

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


Re: The penis is way too delicate for masturbation

2005-09-10 Thread Tux Wonder-Dog
Chucky  Janica wrote:

 Once upon a time - for example, 1 Sep 2005 05:32:54 -0700 - there was
 this guy, or something, called [EMAIL PROTECTED], and they made us
 all feel better by saying the following stuff:
 
.
 
 That penis, more to the point, is too small for masturbation.
 
 
 
 
 CJ
 
Actually, I thought it was an advertisment for docking irons, you know, the
same as they use for docking sheeps' tails ...

I'll bet this spammer's got Castration Envy.
-- 
Good, late in to more rewarding well.  Well, you tonight.  And I was
lookintelligent woman of Ming home.  I trust you with a tender silence.  I
get a word into my hands, a different and unbelike, probably - 'she
fortunate fat woman', wrong word.  I think to me, I justupid.
Let not emacs meta-X dissociate-press write your romantic dialogs...!!!
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: simple problem with os.rename() parameters - path with spaces

2005-09-10 Thread Tom
Peter Hansen wrote:
 Tom wrote:
 
I'm having a problem using a path with spaces as a parameter to 
os.rename() in a program on WinXP.

This works fine at the command line (where the folder c:\aa bb exists)

  os.rename( c\aa bb, c:\cc dd );
 

But, I can't get it to work in my program, eg.

print SrcDir
print NewDir
os.rename( SrcDir, NewDir );

when I run this I get something like this:

e:\\music\\Joni Mitchell\\ogg-8
e:\\music.ogg\\Joni Mitchell\\ogg-8
 
 
 What kind of device is drive E: ?  Are you certain it allows spaces in 
 filenames?  Can you do the same renaming *using the same drive* at the 
 command line or from Explorer?
 
 -Peter

Drive E: is removable, so I was careful to verify that that was a factor 
in the problem.

Yes, I can do the same renaming, with the same drive, at the command line.

I think I put the emphasis in the wrong place in my question.  This 
isn't really about os.rename().  It is about putting a filename with 
spaces into a string object and then using it as a parameter to an 'os' 
command.

Tom.

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


Re: simple problem with os.rename() parameters - path with spaces

2005-09-10 Thread Tom
Yes, I am sure about those things.
I've tried shutil.move and got the same result.
Forward slash?  I'll give that a try and report back here if it works.

Thanks,
Tom.

Larry Bates wrote:
 Are you sure the source directory exists and you
 have rights to rename it?  Because the rename works
 for me.
 
 But you may want to look at shutil.move and/or
 use forward slashes (they work under Windows)
 
 -Larry Bates
 
 
 Tom wrote:
 
I'm having a problem using a path with spaces as a parameter to
os.rename() in a program on WinXP.

This works fine at the command line (where the folder c:\aa bb exists)


os.rename( c\aa bb, c:\cc dd );


But, I can't get it to work in my program, eg.

print SrcDir
print NewDir
os.rename( SrcDir, NewDir );

when I run this I get something like this:

e:\\music\\Joni Mitchell\\ogg-8
e:\\music.ogg\\Joni Mitchell\\ogg-8

Traceback (most recent call last):
  File E:\Music\MoveMusic.py, line 64, in ?
main();
...
  File E:\Music\MoveMusic.py, line 49, in Visit
os.mkdir( NewDir );
OSError: [Errno 22] Invalid argument: 'e:\\music.ogg\\Joni
Mitchell\\ogg-8'

I've tried different combinations of single backslash vs. double
backslash, and quoted vs. non-quoted, but it always fails.

The problem is not specific to os.rename.  If I instead use mkdir(
SrcDir ) I get the same problem.

Thanks,
Tom.


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


How to handle very large MIME Messages with the email package?

2005-09-10 Thread [EMAIL PROTECTED]
Looking at the email package, it seems all the MIMExxx classes takes
string but not file object as the payload. I need to handle very large
MIME messages say up to 100M. And possibly many of them. Is email
package sufficient. If not is it possible to extend it?

Looking at a previous thread it seems the standard library is not
sufficient.

http://groups.google.com/group/comp.lang.python/browse_frm/thread/7d1754c1ead54f33/5b6bd38a049e7f6c?q=mime+email+file+objectrnum=5hl=en#5b6bd38a049e7f6c

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


Re: nested tuples

2005-09-10 Thread Terry Reedy

Luis P. Mendes [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 | Why?  What is it about the list of tuples that you don't like?
 | Philosophically, it's more in line with Guido's separation of list and
 | tuple.
 I'm not saying that I don't like, I was just curious to know if there
 was a way to do it using exclusively tuples.

A list of lists can be built top down.  A tuple of tuples must be built 
bottom up, and each tuple must be built with one call to tuple().  But 
tuple(it) will take any iterable, so write, say, a generator that yields 
lower-level tuples.

Terry J. Reedy



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


Re: Python compiled?

2005-09-10 Thread Terry Reedy

Tim Roberts [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 billiejoex [EMAIL PROTECTED] wrote:

Hi all. I'm sorry for a noob question like this but I'll try to ask it
anyway.
One of the greatest problem that may discourage a new user to choose 
Python
language is it's interpreted nature.

 I doubt it.  C#, VB.NET, VBscript, Javascript and Perl have not suffered
 from being interpreted.

Nor has 386 'machine language' suffered from being interpreted, at a deeper 
level, by microcode.

tjr



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


Re: What's the difference between VAR and _VAR_?

2005-09-10 Thread Michael Ekstrand
On 8 Sep 2005 22:48:05 -0700
Johnny Lee [EMAIL PROTECTED] wrote:
 I thought there must be something special when you named a VAR with
 '_' the first character. Maybe it's just a programming style and I had
 thought too much...

It is just a programming style issue. In Python, variables and functions
beginning with '_' are regarded by convention to be semi-private;
essentially the Python equivalent of 'protected'. Since Python has no
formal access specifications, conventions like this have been adopted.
Likewise, '__' is private. Code using your code can bypass this, of
course, but it's just a signal to the programmer 'avoid using this; it
is an internal detail that may change.'

Incidentally, epydoc regards all items beginning with '_' as private,
and hides them from the public documentation it generates.

And, in one project I'm working on, I use underscores to
base/background/helper modules in a mechanism where modules are
dynamically loaded (to prevent name collisions, since no dynamically
loaded module will begin with an underscore).

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


Re: OpenSource documentation problems

2005-09-10 Thread Michael Ekstrand
On Fri, 09 Sep 2005 18:16:36 -0400
Mike Meyer [EMAIL PROTECTED] wrote:
 You need a better browser. Mine - at least on Unix - have an option to
 dump textareas into text files, invoke my favorite editor on them, and
 then read the file back in when the editor exits. Assuming i'm not
 running the browser in that editor in the first place.

Which browser might this be? I am curious.

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


Re: simple problem with os.rename() parameters - path with spaces

2005-09-10 Thread Peter Hansen
Tom wrote:
 Drive E: is removable, so I was careful to verify that that was a factor 
 in the problem.
 
 Yes, I can do the same renaming, with the same drive, at the command line.
 
 I think I put the emphasis in the wrong place in my question.  This 
 isn't really about os.rename().  It is about putting a filename with 
 spaces into a string object and then using it as a parameter to an 'os' 
 command.

That can't really be all there is to the issue, since other people can 
successfully use spaces in filenames passed to 'os' commands including 
os.rename.  There must be something special with _your_ situation.  What 
else could it be except the file system?

Oh... wait, I see now.  Look closely at your error message:

Traceback (most recent call last):
   File E:\Music\MoveMusic.py, line 64, in ?
 main();
...
   File E:\Music\MoveMusic.py, line 49, in Visit
 os.mkdir( NewDir );
OSError: [Errno 22] Invalid argument: 'e:\\music.ogg\\Joni 
Mitchell\\ogg-8'

Where do you think those double quotation marks came from?  What happens 
if you try the following instead of using the variables you were trying 
to use?

os.rename(e:\\music\\Joni Mitchell\\ogg-8,
 e:\\music.ogg\\Joni Mitchell\\ogg-8)

Now try it with this and observe how you get (I predict) the same error 
message as you originally got, and note what your mistake was:

os.rename('e:\\music\\Joni Mitchell\\ogg-8',
 'e:\\music.ogg\\Joni Mitchell\\ogg-8')

(To avoid confusion, cut and paste the above lines rather than 
attempting to retype them.)

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


Re: Why do Pythoneers reinvent the wheel?

2005-09-10 Thread Mike Meyer
Stefano Masini [EMAIL PROTECTED] writes:
 It would be great if there was a section in the Python manual like this:

 Quick and Dirty: Commonly needed tricks for real applications

 1. odict
 2. file system management
 3. xml (de)serialization
 4. ...

 Each section would describe the problem and list one or a few
 recommended implementations. All available under __testing_stdlib__.
 Appoint somebody as the BDFL and make the process of updating the
 testing stdlib democratic enough to allow for more evolution freedom
 than the stable stdlib.

Why are you reinvinting the wheel?

Things that are very similar to this already exist. There are a number
of code repositories/listings around - PyPI being the obvious example,
because it's linked to from python.org. The Python cookbook site is
closer to what you're talking about, though.

And the latter reveals the problem with your suggestion - this
section would pretty much outweigh the rest of the
documentation. Clearly, it should be a separate book - but we've
already got that.

However, a list of these isn't in the manual. The only thing I could
find was a page on the Wiki about How to publish a module. That
doesn't seem likely to be found by someone trying to find code.

I think the manual does need a section on how to find code other than
the library. But where do you put it?

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do Pythoneers reinvent the wheel?

2005-09-10 Thread Aahz
In article [EMAIL PROTECTED],
Tim Daneliuk  [EMAIL PROTECTED] wrote:

IMHO, one of Python's greatest virtues is its ability to shift paradigms
in mid-program so that you can use the model that best fits your problem
space. IOW, Python is an OO language that doesn't jam it down your
throat, you can mix OO with imperative, functional, and list processing
coding models simultaneously.

In my view, the doctrinaire', indeed religious, adherence to OO purity
has harmed our discipline considerably. Python was a nice breath of
fresh air when I discovered it exactly because it does not have this
slavish committment to an exclusively OO model.

+1 QOTW
-- 
Aahz ([EMAIL PROTECTED])   * http://www.pythoncraft.com/

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


Re: Why do Pythoneers reinvent the wheel?

2005-09-10 Thread Aahz
In article [EMAIL PROTECTED],
Dave Brueck  [EMAIL PROTECTED] wrote:

Many projects (Python-related or not) often seem to lack precisely
what has helped Python itself evolve so well - a single person with
decision power who is also trusted enough to make good decisions, such
that when disagreements arise they don't typically end in the project
being forked (the number of times people disagreed but continued to
contribute to Python is far higher than the number of times they left
to form Prothon, Ruby, and so on).

In the end, domain-specific BDFLs and their projects just might have
to buble to the top on their own, so maybe the best thing to do is
find the project you think is the best and then begin contributing and
promoting it.

You've got a point there -- reStructuredText seems to be succeeding
precisely in part because David Goodger is the BDFNow.
-- 
Aahz ([EMAIL PROTECTED])   * http://www.pythoncraft.com/

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


Re: execute commands and return output

2005-09-10 Thread billiejoex
Thanks for suggestions.
I'll try one of these solutions soon. 


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


Unusual Python Sighting

2005-09-10 Thread Scott David Daniels
In article:
 http://humorix.org/articles/2005/08/notice/

 ... 6. The use of semantically significant whitespace in Python®
 programs might be protected by intellectual property laws in
 five (5) countries, 

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


Re: OpenSource documentation problems

2005-09-10 Thread Aahz
In article [EMAIL PROTECTED],
Michael Ekstrand  [EMAIL PROTECTED] wrote:
On Fri, 09 Sep 2005 18:16:36 -0400
Mike Meyer [EMAIL PROTECTED] wrote:

 You need a better browser. Mine - at least on Unix - have an option to
 dump textareas into text files, invoke my favorite editor on them, and
 then read the file back in when the editor exits. Assuming i'm not
 running the browser in that editor in the first place.

Which browser might this be? I am curious.

Lynx
-- 
Aahz ([EMAIL PROTECTED])   * http://www.pythoncraft.com/

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


IDisatch and pythoncom

2005-09-10 Thread Manu
Hi,
I have a COM component server that just exposes the IDispatch interface 
but when you use it in a VB app you can access other method. I think the 
way  to access the other methods( as does it VBA for ex)  is to use 
Invoke but don't know how to do it in python.
Say for ex i want to convert this very simple example with only 
IDispatch interface .

import win32com.client
o = win32com.client.Dispactch('Excel.Application')
o.WorkBooks.Add()

Thx for your help
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python versus Perl

2005-09-10 Thread Thorsten Kampe
* Dieter Vanderelst (2005-09-06 18:03 +0100)
 I'm currently comparing Python versus Perl to use in a project that 
 involved a lot of text processing. I'm trying to determine what the most 
 efficient language would be for our purposes. I have to admit that, 
 although I'm very familiar with Python, I'm complete Perl noob (and I 
 hope to stay one) which is reflected in my questions.
 
 I know that the web offers a lot of resources on Python/Perl 
 differences. But I couldn't find a satisfying answer to my questions:
 
 1 - How does the speed of execution of Perl compares to that of Python?

Of course Python is faster than Perl. It's the same reason why
Mercedes are faster than BMWs (or was it the other way round?).
 
 2 - Regular Expressions are a valuable tool in text processing. I have 
 noticed that Regular Expressions are executed very fast in Python. Does 
 anybody know whether Python executes RE faster than Perl does?

Again: this question doesn't make sense. It's up to you to write your
Regular Expressions fast.
 
 3 - In my opinion Python is very well suited for text processing. Does 
 Perl have any advantages over Python in the field of textprocessing 
 (like a larger standard library maybe).
 
 I hope somebody can answer my questions. Of course, every remark and tip 
 on Python/Perl in texprocessing is most welcome.

http://gnosis.cx/TPiP/

In case regular expression operations prove to be a genuinely
  problematic performance bottleneck in an application, there are
  four steps you should take in speeding things up. Try these in
  order:

  1.  Think about whether there is a way to simplify the regular
  expressions involved. Most especially, is it possible to
  reduce the likelihood of backtracking during pattern
  matching? You should always test your beliefs about such
  simplification, however; performance characteristics rarely
  turn out exactly as you expect.

  2.  Consider whether regular expressions are -really- needed
  for the problem at hand. With surprising frequency, faster
  and simpler operations in the [string] module (or,
  occasionally, in other modules) do what needs to be done.
  Actually, this step can often come earlier than the first
  one.

  3.  Write the search or transformation in a faster and
  lower-level engine, especially [mx.TextTools]. Low-level
  modules will inevitably involve more work and considerably
  more intense thinking about the problem. But
  order-of-magnitude speed gains are often possible for the
  work.

  4.  Code the application (or the relevant parts of it) in a
  different programming language. If speed is the absolutely
  first consideration in an application, Assembly, C, or C++
  are going to win. Tools like swig--while outside the scope
  of this book--can help you create custom extension modules
  to perform bottleneck operations. There is a chance also
  that if the problem -really must- be solved with regular
  expressions that Perl's engine will be faster (but not
  always, by any means).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OpenSource documentation problems

2005-09-10 Thread Mike Meyer
Michael Ekstrand [EMAIL PROTECTED] writes:

 On Fri, 09 Sep 2005 18:16:36 -0400
 Mike Meyer [EMAIL PROTECTED] wrote:
 You need a better browser. Mine - at least on Unix - have an option to
 dump textareas into text files, invoke my favorite editor on them, and
 then read the file back in when the editor exits. Assuming i'm not
 running the browser in that editor in the first place.

 Which browser might this be? I am curious.

w3m has this feature. emacs-w3m uses w3m as a back end, rendering into
an emacs buffer, and gets this by default. And as I noted in the
message you quoted, there's an extension available for FireFox that
gives you the ability to invoke an external editor on the text in a
textarea.

The first browser I ever saw that did this was iBrowse. It attached a
button to each text area that invoked an external editor on the
contents of the textarea. iBrowse is my all time favorite browser. The
list of desirable features was simply amazing, and I've never seen
it's equal. The mozilla family comes close if you consider all the
extensions, but iBrowse had them built in - and still rendered faster
than a text only browser on the same platform. I doubt if I'll ever
see it's like again.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


pydoc search in browser

2005-09-10 Thread Sakcee
Hi

I am a newbie to pydoc and I need some help. I would really appreciate
if some one can tell me how to add search box in a page generated by
pydoc,

e.g. I want to add serch like the one in pydoc.org 

thanks
sakcee

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


Re: Using Python with COM to communicate with proprietary Windows software

2005-09-10 Thread Joakim Persson
On Fri, 09 Sep 2005 08:36:00 +0200, Thomas Heller [EMAIL PROTECTED]
wrote:

Sounds like a perfect job for comtypes, which is a COM library
implemented in pure Python, based on ctypes.  comtypes should make it
easy to access custom (non-dispatch derived) com interfaces, or the
vtable based part of dual interfaces - it would be good however, if you
have a type library for the interfaces.

http://sourceforge.net/projects/comtypes/

No docs yet, but there are tests included which should get you started.

(I have released and announced this 3 weeks ago, but haven't got a
single feedback.  So it seems the need to access custom interfaces is
very low.)

Thommas

After some testing today, it does seem to do exactly what I wanted --
I can now access the custom but IDispatch-like COM interface that I
couldn't access with win32com (or with Java + jawin). It might be
possible in other ways, but using comtypes was definitely the most
painfree way (everything, including the return values from the
methods, worked as expected). Thank you very much. 

Of course, this does not complete my task -- although I can now use my
interface to send messages and commands to the big log tool, I still
need to implement a COM server and pass a pointer to its interface
through one of the messages to the com server to be able to receive
data: 

BridgeInterface.StartLogging(filename)   --- works fine, didn't work
before
BridgeInterface.Advise(ptr)  --- Now, I need to create a new
interface for receiving the data sent from the log application, so
that I can (at first) print it

This _shouldn't_ be too difficult -- I know which methods must be
implemented (basically just some kind of event handling to deal with
randomly arriving log points, should be implemented as onMsg() on my
COM server side, and some other similar methods), but I don't really
know how. I have tried doing simple COM servers using win32com, but is
it equally possible to implement such a simple thing in comtypes? I
didn't find any server side examples in comtypes, but perhaps there is
a way?

-- 
Joakim Persson
M.Sc student, CS/E @ LTH
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IDisatch and pythoncom

2005-09-10 Thread Do Re Mi chel La Si Do
Hi !


Perhaps :  .Dispactch  vs  .Dispatch  ?


@-salutations

Michel Claveau


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


Re: Unusual Python Sighting

2005-09-10 Thread [EMAIL PROTECTED]

Scott David Daniels wrote:
 In article:
  http://humorix.org/articles/2005/08/notice/

  ... 6. The use of semantically significant whitespace in Python®
  programs might be protected by intellectual property laws in
  five (5) countries, 

 --Scott David Daniels
 [EMAIL PROTECTED]

It appears you indented the quotation.

I'm going to have to report you.

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


Re: encryption with python

2005-09-10 Thread Kirk Job Sluder
Steven D'Aprano [EMAIL PROTECTED] writes:

 On Wed, 07 Sep 2005 14:31:03 -0700, jlocc wrote:
 
  Basically I will like to combine a social security number (9 digits)
  and a birth date (8 digits, could be padded to be 9) and obtain a new
  'student number'. It would be better if the original numbers can't be
  traced back, they will be kept in a database anyways. Hope this is a
  bit more specific, thanks!!!
 
 
 There are one-way encryption functions where the result can't easily be
 traced back to the input, but why do you need the input anyway? 

Well, there is a form of security design that involves one-way
encryption of confidential information.  You might want to be able to
search on SSN, but not have the actual SSN stored in the database.  So,
you are prepared to deal with the inevetable, I lost my
password/student ID, can you still look up my records?  

Don't think it applies in this case, but might in some other cases.

 
 -- 
 Steven.
 

-- 
Kirk Job-Sluder
The square-jawed homunculi of Tommy Hilfinger ads make every day an
existential holocaust.  --Scary Go Round
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python versus Perl

2005-09-10 Thread Roy Smith
Dieter Vanderelst [EMAIL PROTECTED] wrote:
 1 - How does the speed of execution of Perl compares to that of Python?

To a first-order approximation, Perl and Python run at the same speed.  
They are both interpreted languages with roughly the same kind of control 
flow and data structures.  The two have wildly different kinds of syntax, 
but the underlying machinery is fairly similar.

Don't make a decision on which to use based on execution speed.  If you 
feel compelled to ignore this advice, then don't make the decision until 
you've coded your application in both languages and done careful 
benchmarking with realistic data for your application domain.

 I have noticed that Regular Expressions are executed very fast in 
 Python. Does anybody know whether Python executes RE faster than Perl 
 does?

Same answer as above -- for a first cut, assume they are the same speed.  
If you insist on a better answer, do measurements on real data.

The big advantage the Python has over Perl is speed of development and ease 
of maintenance (i.e. a year from now, you can still understand what your 
own code does).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do Pythoneers reinvent the wheel?

2005-09-10 Thread Bengt Richter
On Sat, 10 Sep 2005 16:55:51 +0200, Martin P. Hellwig [EMAIL PROTECTED] 
wrote:

Stefano Masini wrote:
cut reinventing wheel example

Although I'm not experienced enough to comment on python stuff itself I 
do know that in general there are 2 reasons that  people reinvent the wheel:
- They didn't know of the existence of the first wheel
- They have different roads

 - They want the feeling that they are in the same league as the original 
inventor ;-)

Those reasons can even be combined.

The more difficult it is to create a new wheel the bigger the chance is 
that you:
- Search longer for fitting technologies
- Adapt your road
 - Think more carefully about ego satisfaction cost/benefit vs getting the job 
done ;-)

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


Re: Python compiled?

2005-09-10 Thread Robert Kern
Terry Reedy wrote:
 Tim Roberts [EMAIL PROTECTED] wrote in message 
 news:[EMAIL PROTECTED]
 
billiejoex [EMAIL PROTECTED] wrote:

Hi all. I'm sorry for a noob question like this but I'll try to ask it
anyway.
One of the greatest problem that may discourage a new user to choose 
Python
language is it's interpreted nature.

I doubt it.  C#, VB.NET, VBscript, Javascript and Perl have not suffered
from being interpreted.
 
 Nor has 386 'machine language' suffered from being interpreted, at a deeper 
 level, by microcode.

I think both you and Paul may be missing Tim's point. I don't think he's
talking about suffering in technical respects, like speed. He's
talking about popularity.

-- 
Robert Kern
[EMAIL PROTECTED]

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

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


Find roots of ill-conditioned polynomials

2005-09-10 Thread Raymond L. Buvel
If you are using the root finder in Numeric, and are having problems,
check out the root finder in the ratfun module.  My testing indicates
that it will give the exact roots of a Wilkinson polynomial of degree
100.  For more information see

http://calcrpnpy.sourceforge.net/ratfun.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: encryption with python

2005-09-10 Thread Kirk Job Sluder
Paul Rubin http://[EMAIL PROTECTED] writes:

 Kirk Job Sluder [EMAIL PROTECTED] writes:
  Well, there is a form of security design that involves one-way
  encryption of confidential information.  You might want to be able to
  search on SSN, but not have the actual SSN stored in the database.  So,
  you are prepared to deal with the inevetable, I lost my
  password/student ID, can you still look up my records?  
 
 The minute you provide a way to do that without secret keys, you have
 a security hole.

Providing any kind of access to data involves creating a security hole.
This is the biggest flaw in most discussions of computer security.  Too
much of it depends on everyone remembering (and using) unique
cryptographically strong keys.

You have a client on the phone who needs access to information, but has
forgotten or lost the 10-digit unique ID and the PIN you gave them two
years ago.  How do you provide that client with the information he or
she needs?  This is the kind of dilemma that one-way encryption is
designed to make a tiny bit safer. 

SSNs + some other secret (such as mother's maiden name) is certainly
crappy security.  However, I don't think we are going to see widespread
adoption of anything better in the near future.  

But even if we go with more secure authentication tokens, there is usually
no reason to store the authentication token in plaintext.   

  SSN's are 9 digits which means there are 1 billion
 of them.  If there are 100,000 hashed SSN's in the database, the
 attacker (since this is clpy) can read them all into a Python dict.
 S/he then starts generating SSN's at random and hashing them and
 checking whether those hashes appear in the dict.  Doing stuff like
 iterated hashes to slow the attacker down doesn't help that much: the
 attacker needs to hash only 10,000 or so SSN's to be likely to hit one
 that's in the dict.  If the attacker can hash all 10**9 SSN's, which
 isn't all that terribly many, every SSN in the database spills.

Of course, an additional step I didn't mention was that in actual
practice the SSNs would be hashed with a strong random secret key. But
from my point of view, the possibility for dictionary attacks is pretty
much unavoidable as long as we are dealing just with memorized tokens.

We've been bitching, whining and moaning about the small keyspace and
poor quality of what users are willing to memorize for 20 years.  We can
complain about it for the next 10 which is about how long it will take
for any kind of alternative to be adopted.  I still think that one-way
hashing of authentication secrets is better than plain-text storage.

 Bottom line: to keep confidential stuff secure, you need actual security.

The only way to keep confidential stuff secure is to shred it, burn it,
and grind the ashes.  

I think the fundamental problem is that that most customers don't want
actual security.  They want to be able to get their information by
calling a phone number and saying a few words/phrases they memorized in
childhood.  Given the current market, it seems to be cheaper to deal
with breaks after the fact than to expect more from customers.

-- 
Kirk Job-Sluder
The square-jawed homunculi of Tommy Hilfinger ads make every day an
existential holocaust.  --Scary Go Round
-- 
http://mail.python.org/mailman/listinfo/python-list


calling command line programs?

2005-09-10 Thread Yevgeniy (Eugene) Medynskiy
Hi all,

This is probably a very newbie question, but after searching google and 
docs @ python.org I can't find an answer, so maybe someone would be able 
to help?

I'd like to call command-line functions from my python script (like you 
would in perl using backticks)... Is there a way of doing this? And if 
so, how does the environment get treated (I have some variables in my 
env that the programs I'd be calling need to see).

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


class 'Exception', unable to use 'super' to call superclass initializer

2005-09-10 Thread chriss
Hi,

environment: Python 2.4, GNU/Linux, kernel 2.6.12.2

having subclassed 'Exception' I'm trying to call the initialiser
__init__(...) of the superclass Exception with 'super(..).__init__(..)' .
However, trying to do so results in a
'TypeError: super() argument 1 must be type, not classobj'.

Now, if I use 'Exception.__init__(..)' instad of super(..)... ,everything
works just as one would expect.

Why does 'super(..).__init__(..)' fail?


thank you for any suggestions
chriss



Here is some example code to illustrate the point:


class WorkingException(Exception):

def __init__(self, message):
# works as I would expect
Exception.__init__(self, message)


class BrokenException(Exception):

def __init__(self, message):
# fails with a typeError
super(BrokenException, self).__init__(self, message)


# - case 1 -
try:
raise WorkingException(Hello WorkingException)

except WorkingException, e:
print e


# - case 3 -
 
try:
raise BrokenException(Hello BrokenException)


except BrokenException, e:
print e



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


Re: encryption with python

2005-09-10 Thread Paul Rubin
Kirk Job Sluder [EMAIL PROTECTED] writes:
 You have a client on the phone who needs access to information, but has
 forgotten or lost the 10-digit unique ID and the PIN you gave them two
 years ago.  How do you provide that client with the information he or
 she needs?  This is the kind of dilemma that one-way encryption is
 designed to make a tiny bit safer. 

You need secret keys then, and you need to secure them.  If you have a
secure secret key K, you can store something like HMAC(K, SSN) and
that is pretty safe from offline attacks.

 Of course, an additional step I didn't mention was that in actual
 practice the SSNs would be hashed with a strong random secret key.

But now you have to maintain that secret key and its secrecy, which is
not a trivial task.  It's not an unsolveable problem but you can't
handwave it.

We're told there is already a secure database in the picture
somewhere, or at least one that unescapeably contains cleartext SSN's,
so that's the system that should assign the ID numbers and handle
SSN-based queries.

 I think the fundamental problem is that that most customers don't
 want actual security.  They want to be able to get their information
 by calling a phone number and saying a few words/phrases they
 memorized in childhood.  

A voice exemplar stored at enrollment time plus a question or two like
what classes did you take last term could easily give a pretty good
clue that the person saying the words/phrases is the legitimate
student.

 Given the current market, it seems to be
 cheaper to deal with breaks after the fact than to expect more from
 customers.

Customers legitimately want actual security without having to care how
hash functions work, just like they want safe transportation without
having to care about how jet engine turbopumps work.  Air travel is
pretty safe because if the airline fails to maintain the turbopumps
and a plane goes down, there is hell to pay.  There is huge legal and
financial incentive for travel vendors (airlines) to not cut corners
with airplane safety.  But vendors who deploy incompetently designed
IT systems full of confidential data resulting in massive privacy
breaches face no liability at all.  

There is no financial incentive for them to do it right, so they
instead spend the money on more marketing or on executive massages or
whatever, and supply lousy security.  THAT is the fundamental problem.
-- 
http://mail.python.org/mailman/listinfo/python-list


Make Even More $$$$$ with! GreenZap! Get $25.00 for signing up/$5 for thoose u refer

2005-09-10 Thread Make More With GreenZap (Better than PayPal)get $25.00/signing up + $5.00/thoose you refer!!!
Make $10,000 in 30 days with GreenZap|Get Paid $25.00/signin up!
 get paid $5.00/thoose you refer!!! Pay Automated!!!

 This really does work and is also totally legal and free for anyone to
join.

 Sign up now for free and get $25 instantly into your new account.

 Why this works

 Unlike the PayPal $5 programs, this doesn't even cost you $5 to join
 in, just 2 minutes of your time.
 Sign up at this link

 www.greenzap.com/peso4sho
 $25 will be put into your account for free.

 Great, you've made $25 in just 2 minutes, not a bad start.

 Why not turn it into $10,000 or more in just 30 days.

 Believe me that's a very realistic target.

 If you're still not convinced try it for the sole reason of proving
 yourself right for thinking it won't work.

 But I warn you now, don't start complaining to me with all the money
 you're making to why I didn't let you know about this sooner.


 Here's the reason why the're doing this.

 Pay Pal is their biggest competitor worth $6-8 billion dollars.

 Pay Pal has between 60-80 million members. In other words, each of
 those members is worth about $100 to Pay Pal.

 GreenZap understands this and is willing to pay for its members in
 advance.
 That's where the initial $25 comes in as well as the community
 rewards of anywhere from $5-30 more dollars.


 This is an ingenious way to quickly build a loyal community of
 GreenZap members.
 But remember the're not going to give out the $25 forever.
 It's only for their pre-registration campaign, so act quickly.


 They are cheaper to use than PayPal too.

 $1.00 per transaction fee

 Compare $1.00 to PayPal's [1.9%-2.9% + $0.30] transaction fees and its
 easy to see who's focusing on offering value to their community and
who's not.


 Is GreenZap Really Giving Away FREE Money?


 Yes. Think of it this way; at $1.00/transaction, GreenZap will
 eventually get their money back once you use your GreenZap account to
 transact 25 times.


 How Can You Profit From The Battle Of GreenZap and PayPal?

 Get started now



 1. GET AN EMAIL ACCOUNTI ASSUME YOU ALREADY HAVE ONE...


 2.GO TO www.greenzap.com/peso4sho

 3. SIGN UP FOR AN ACCOUNT.
 IT'S TOTALLY FREE AND THEY WILL FUND YOU $25
 INSTANTLY INTO YOUR ACCOUNT JUST FOR SIGNING UP.


 4. TAKE THAT $25 AND USE IT AS FOLLOWS: CLICK ON THE SEND MONEY TAB.


 5.ENTER THE EMAIL ADDRESS IN THE NUMBER ONE POSITION.


 Here's the current list:


 1. [EMAIL PROTECTED]

 2. [EMAIL PROTECTED]

 3. [EMAIL PROTECTED]



SEND THE $25 DOLLARS TO THE FIRST NAME WITH A NOTE SAYING ADD ME TO
YOUR MAILING LIST.

 THEN SEND AN EMAIL TO THE NUMBER THREE POSITION SAYING THANKS I'VE
JOINED.


 THIS SECTION IS IMPORTANT SINCE THIS IS WHAT
 MAKES THIS LEGAL.
 YOU CAN SIGN UP FROM ANYWHERE IN THE WORLD AND ONLY ONE
 ACCOUNT PER PERSON CAN BE OPENED WITH THE GREENZAP SYSTEM SO NO ONE IS
ABLE TO SIGN UP FOR LOADS OF ACCOUNTS AND SEND THE $25 TO THEMSELVES.


 6. COPY/ PASTE THIS LETTER INTO A WORD PROCESSOR. TAKE THE #1 EMAIL
OFF AND PUT YOUR EMAIL YOU SIGNED UP WITH INTO THE #3 POSITION.

 7. LOG ON TO GOOGLE OR YAHOO AND TYPE IN MONEY MAKING MESSAGE BOARD OR
BUSINESS OPPORTUNITY... ETC. POST THE NEW COPY OF THE LETTER WITH YOUR
NAME IN THE 3 SPOT TO LOADS OF MESSAGING BOARDS. THE MORE THE BETTER.


 YOU CAN ALSO EMAIL THIS LETTER TO ANYONE YOU KNOW, HOW ABOUT SENDING
IT TO THE EMAIL ADDRESSES THAT SEND YOU OFFERS. IT'S SIMPLETHE
MORE YOU SEND OUT THE MORE MONEY YOU WILL MAKE.
 AIM TO SEND IT TO AT LEAST 40 PEOPLE.




 PLEASE DO NOT TRY AND CHEAT BY PUTTING YOUR EMAIL AT THE TOP BECAUSE
YOU WILL NOT MAKE A LOT OF MONEY LIKE THAT. REMEMBER YOUR EMAIL ADDRESS
WILL BE DELETED FROM THE TOP BY THE NEXT PERSON WHO RECEIVES THIS
LETTER. BY STARING AT THE BOTTOM AND BEING MOVED UP WHEN IT'S EMAILED
TO OTHERS YOUR EMAIL ADDRESS IT'S EXPOSED TO MANY THOUSANDS MORE AT
THE
 TOP THAN IF YOU CHEAT.


 That the response rate to this is VERY HIGH goes without saying,
really
 Who else would give you $25 for free and let you use it to make
 thousands of dollars. You can maybe use the money you make from this
program
 to start you own business or spend this weekend down at your local
auto
 showroom and test drive some cars. Have fun however you spend it!


 www.greenzap.com/peso4sho

 Enjoy making lots and lots and lots of money!


 If you don't try it you will never know.

 Good fortunes to you and yours.

 Peso, success  wealth!
 [EMAIL PROTECTED]*PLEASE ADD ME TO YOUR GreenZap LISTS*






GZAccepted_Badge2.gif

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


Re: calling command line programs?

2005-09-10 Thread Grant Edwards
On 2005-09-11, Yevgeniy (Eugene) Medynskiy [EMAIL PROTECTED] wrote:

 This is probably a very newbie question, but after searching
 google and docs @ python.org I can't find an answer, so maybe
 someone would be able to help?

 I'd like to call command-line functions from my python script
 (like you would in perl using backticks)... Is there a way of
 doing this? And if so, how does the environment get treated (I
 have some variables in my env that the programs I'd be calling
 need to see).

Take a look at os.popen, os.spawn, or the popen2, and
subprocess modules.

That last one seems to be gaining popularity.

-- 
Grant Edwards   grante Yow!  PEGGY FLEMING is
  at   stealing BASKET BALLS to
   visi.comfeed the babies in VERMONT.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: class 'Exception', unable to use 'super' to call superclass initializer

2005-09-10 Thread Robert Kern
chriss wrote:
 Hi,
 
 environment: Python 2.4, GNU/Linux, kernel 2.6.12.2
 
 having subclassed 'Exception' I'm trying to call the initialiser
 __init__(...) of the superclass Exception with 'super(..).__init__(..)' .
 However, trying to do so results in a
 'TypeError: super() argument 1 must be type, not classobj'.
 
 Now, if I use 'Exception.__init__(..)' instad of super(..)... ,everything
 works just as one would expect.
 
 Why does 'super(..).__init__(..)' fail?

AFAICT, the Exception hierarchy are still old-style classes while
super() only works on new-style classes.

-- 
Robert Kern
[EMAIL PROTECTED]

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

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


Re: class 'Exception', unable to use 'super' to call superclass initializer

2005-09-10 Thread Peter Hansen
chriss wrote:
 Hi,
 
 environment: Python 2.4, GNU/Linux, kernel 2.6.12.2
 
 having subclassed 'Exception' I'm trying to call the initialiser
 __init__(...) of the superclass Exception with 'super(..).__init__(..)' .
 However, trying to do so results in a
 'TypeError: super() argument 1 must be type, not classobj'.
 
 Now, if I use 'Exception.__init__(..)' instad of super(..)... ,everything
 works just as one would expect.
 
 Why does 'super(..).__init__(..)' fail?

Exceptions do not inherit from 'object'; they are old-style classes.

super() can be used only with new-style classes (which subclass 'object').

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


Re: calling command line programs?

2005-09-10 Thread Robert Kern
Yevgeniy (Eugene) Medynskiy wrote:
 Hi all,
 
 This is probably a very newbie question, but after searching google and 
 docs @ python.org I can't find an answer, so maybe someone would be able 
 to help?

http://docs.python.org/lib/module-subprocess.html

-- 
Robert Kern
[EMAIL PROTECTED]

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

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


Re: encryption with python

2005-09-10 Thread Kirk Job Sluder
Paul Rubin http://[EMAIL PROTECTED] writes:

 Kirk Job Sluder [EMAIL PROTECTED] writes:
 We're told there is already a secure database in the picture
 somewhere, or at least one that unescapeably contains cleartext SSN's,
 so that's the system that should assign the ID numbers and handle
 SSN-based queries.

Well, IMO just having cleartext SSNs is questionable practice unless you
need those SSNs to report to some other agency that takes SSNs.  And
even so, you might want to limit access to plaintext SSNs to a limited
group, and give access to the hashed SSNs as a search key to a different
group.

  I think the fundamental problem is that that most customers don't
  want actual security.  They want to be able to get their information
  by calling a phone number and saying a few words/phrases they
  memorized in childhood.  
 
 A voice exemplar stored at enrollment time plus a question or two like
 what classes did you take last term could easily give a pretty good
 clue that the person saying the words/phrases is the legitimate
 student.

In my experience the typical student has trouble remembering what
happened last week, much less last term.  In addition, universities
frequently need to field questions from people who were students years
ago.  

Are voice exemplars at that stage yet?  

 Customers legitimately want actual security without having to care how
 hash functions work, just like they want safe transportation without
 having to care about how jet engine turbopumps work.  Air travel is
 pretty safe because if the airline fails to maintain the turbopumps
 and a plane goes down, there is hell to pay.  There is huge legal and
 financial incentive for travel vendors (airlines) to not cut corners
 with airplane safety.  But vendors who deploy incompetently designed
 IT systems full of confidential data resulting in massive privacy
 breaches face no liability at all.  

I'm more than happy to agree to disagree on this, but I see it
differently.  In aviation there certainly is a bit of risk-benefit
analysis going on in thinking about whether the cost of a given safety
is justified given the benefits in risk reduction.  

Likewise, credit companies are currently making money hand-over-fist.
If an identity is compromised, it's cheaper for them to just close the
account, refund the money, and do their own fraud investigation after
the fact.  Meanwhile, for every person who gets stung, there are a
hundred wanting convenience.  In addition, the losses due to bad
cryptographic implementation appear to be trivial compared to the losses
due to social engineering.  

-- 
Kirk Job-Sluder
The square-jawed homunculi of Tommy Hilfinger ads make every day an
existential holocaust.  --Scary Go Round
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: calling command line programs?

2005-09-10 Thread chriss
Grant Edwards wrote:

 On 2005-09-11, Yevgeniy (Eugene) Medynskiy [EMAIL PROTECTED] wrote:
 
 This is probably a very newbie question, but after searching
 google and docs @ python.org I can't find an answer, so maybe
 someone would be able to help?

 I'd like to call command-line functions from my python script
 (like you would in perl using backticks)... Is there a way of
 doing this? And if so, how does the environment get treated (I
 have some variables in my env that the programs I'd be calling
 need to see).
 
 Take a look at os.popen, os.spawn, or the popen2, and
 subprocess modules.
 
 That last one seems to be gaining popularity.
 

The suggested modules and functions have been deprecated according to the
python 2.4 docs. The doc suggests to use the functions in the 'subprocess'
module.

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


Re: class 'Exception', unable to use 'super' to call superclass initializer

2005-09-10 Thread chriss
Peter Hansen wrote:

 chriss wrote:
 Hi,
 
 environment: Python 2.4, GNU/Linux, kernel 2.6.12.2
 
 having subclassed 'Exception' I'm trying to call the initialiser
 __init__(...) of the superclass Exception with 'super(..).__init__(..)' .
 However, trying to do so results in a
 'TypeError: super() argument 1 must be type, not classobj'.
 
 Now, if I use 'Exception.__init__(..)' instad of super(..)... ,everything
 works just as one would expect.
 
 Why does 'super(..).__init__(..)' fail?
 
 Exceptions do not inherit from 'object'; they are old-style classes.
 
 super() can be used only with new-style classes (which subclass 'object').
 
 -Peter

That explains it all right. 
Thank you very much for your answer.

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


Re: encryption with python

2005-09-10 Thread Robert Kern
Kirk Job Sluder wrote:
 Paul Rubin http://[EMAIL PROTECTED] writes:
 
Kirk Job Sluder [EMAIL PROTECTED] writes:
We're told there is already a secure database in the picture
somewhere, or at least one that unescapeably contains cleartext SSN's,
so that's the system that should assign the ID numbers and handle
SSN-based queries.
 
 Well, IMO just having cleartext SSNs is questionable practice unless you
 need those SSNs to report to some other agency that takes SSNs. 

Colleges generally do have such needs.

-- 
Robert Kern
[EMAIL PROTECTED]

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

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


Re: encryption with python

2005-09-10 Thread James Stroud
On Saturday 10 September 2005 14:01, Kirk Job Sluder wrote:
 Providing any kind of access to data involves creating a security hole.
 This is the biggest flaw in most discussions of computer security.

On 9/9/05 Steven D'Aprano wrote:
 There are one-way encryption functions where the result can't easily be
 traced back to the input, but why do you need the input anyway? Here is my
 quick-and-dirty student ID algorithm:

I have invented the perfect security protocol that solves a major problem with 
the one-time-pad. The problem with most one-time-pad protocols is that you 
still need to have the pad around, creating a major security hole. I have 
solved that problem here. It has all of the steps of the usual one-time-pad 
plus an extra step.

1. Generate a random number the size of your data.
2. XOR your data with it.
3. Destroy the original data.

Here is the additional step:

4. Destroy the random number.

You can see now that no adversary can resonably reconstruct the plain text. 
This protocol might be terribly inconvenient, though, because it makes the 
origina data unaccessible. Oh well, just a necessary byproduct of 
theoritcally perfect security.

I hereby place this algorithm in the public domain. Use it freely.

James

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: encryption with python

2005-09-10 Thread Paul Rubin
Kirk Job Sluder [EMAIL PROTECTED] writes:
 I'm more than happy to agree to disagree on this, but I see it
 differently.  In aviation there certainly is a bit of risk-benefit
 analysis going on in thinking about whether the cost of a given safety
 is justified given the benefits in risk reduction.  
 
 Likewise, credit companies are currently making money hand-over-fist.
 If an identity is compromised, it's cheaper for them to just close the
 account, refund the money, and do their own fraud investigation after
 the fact.

You don't get it.  Refunding the money improperly charged on a single
card doesn't begin to compensate for the hassle of undoing an identity
theft.  If airlines worked the way you're suggesting the credit
industry should work, and a plane went down, the airline would be off
the hook by refunding your estate the price of your ticket.  It's only
because they face much further-reaching liability than that, that they
pay so much attention to safety.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: encryption with python

2005-09-10 Thread Ron Adam
Kirk Job Sluder wrote:

 The only way to keep confidential stuff secure is to shred it, burn it,
 and grind the ashes.  
 
 I think the fundamental problem is that that most customers don't want
 actual security.  They want to be able to get their information by
 calling a phone number and saying a few words/phrases they memorized in
 childhood.  Given the current market, it seems to be cheaper to deal
 with breaks after the fact than to expect more from customers.

Security = Privacy in this context, and most customers do want privacy.

But also in this case, you are referring to two party security 
situations, where the data is shared between a service provider and a 
service consumer.

I would think that any n digit random number not already in the data 
base would work for an id along with a randomly generated password that 
the student can change if they want.  The service provider has full 
access to the data with their own set of id's and passwords, so in the 
case of a lost id, they can just look it up using the customers name 
and/or ssn, or whatever they decide is appropriate. In the case of a 
lost password, they can reset it and get another randomly generated 
password.

Or am I missing something?

Cheers,
Ron

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


Re: simple problem with os.rename() parameters - path with spaces

2005-09-10 Thread Tom
Peter Hansen wrote:
 Tom wrote:
 
Drive E: is removable, so I was careful to verify that that was a factor 
in the problem.

Yes, I can do the same renaming, with the same drive, at the command line.

I think I put the emphasis in the wrong place in my question.  This 
isn't really about os.rename().  It is about putting a filename with 
spaces into a string object and then using it as a parameter to an 'os' 
command.
 
 
 That can't really be all there is to the issue, since other people can 
 successfully use spaces in filenames passed to 'os' commands including 
 os.rename.  There must be something special with _your_ situation.  What 
 else could it be except the file system?
 
 Oh... wait, I see now.  Look closely at your error message:
 
 Traceback (most recent call last):
File E:\Music\MoveMusic.py, line 64, in ?
  main();
 ...
File E:\Music\MoveMusic.py, line 49, in Visit
  os.mkdir( NewDir );
 OSError: [Errno 22] Invalid argument: 'e:\\music.ogg\\Joni 
 Mitchell\\ogg-8'
 
 Where do you think those double quotation marks came from?  What happens 
 if you try the following instead of using the variables you were trying 
 to use?
 
 os.rename(e:\\music\\Joni Mitchell\\ogg-8,
  e:\\music.ogg\\Joni Mitchell\\ogg-8)
 
 Now try it with this and observe how you get (I predict) the same error 
 message as you originally got, and note what your mistake was:
 
 os.rename('e:\\music\\Joni Mitchell\\ogg-8',
  'e:\\music.ogg\\Joni Mitchell\\ogg-8')

This produced the msg:
OSError: [Errno 22] Invalid argument

I'm now using forward slashes instead of backslashes - this simplifies 
things a bit.

The problem seems to be that I'm trying to create more than one 
directory at a time.  In the above example, the dir 'Joni Mitchell' 
doesn't exist.

The functions that I'm calling (os.rename and shutil.move) use mkdir, 
not makedirs.  The solution is for me to use makedirs with all of the 
path except the leaf before I move/rename the old dir.

Thanks for your help,
Tom.


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


Re: List of integers L.I.S. (SPOILER)

2005-09-10 Thread n00m
Bryan;
My own version also timed out.
And now I can tell: it's incredibly SLOW.
Nevertheless it would be interesting to compare
speed of my code against yours. I can't do it myself
because my Python is of 2.3.4 version.

Just uncomment your part.


import bisect

def oops(w,a,b):
for m in w:
j=bisect.bisect_left(a,m)
a.insert(j,m)
b.insert(j,max(b[:j]+[0])+1)

def n00m(n,w):
a,b=[],[]
oops(w,a,b)
v=map(lambda x: -x, w[::-1])
c,d=[],[]
oops(v,c,d)
e=map(sum, zip(b, d[::-1]))
mx=max(e)
f=[]
for i in xrange(n):
if e[i]==mx:
f.append(i+1)
print len(f)


def one_way(seq):
n = len(seq)
dominators = [n + 1] * (n * 1)
score = [None] * n
end = 0
for (i, x) in enumerate(seq):
low, high = 0, end
while high - low  10:
mid = (low + high)  1
if dominators[mid]  x:
low = mid + 1
else:
high = mid + 1
while dominators[low]  x:
low += 1
dominators[low] = x
score[i] = low
end = max(end, low + 1)
return score

def supernumbers(seq):
forscore = one_way(seq)
opposite = [len(seq) - x for x  in reversed(seq)]
backscore = reversed(one_way(opposite))
score = map(sum, zip(forscore, backscore))
winner = max(score + [0])
return sorted([seq[i] for i in range(len(seq)) if score[i] ==
winner])

def b_olson(sequence):
supers = supernumbers(sequence)
print len(supers)



import random, time
n=5
w=range(1,n+1)
random.shuffle(w)

t=time.time()
n00m(n,w)
print 'n00m:',time.time()-t


t=time.time()
b_olson(w)
print 'b_olson:',time.time()-t


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


Re: Why do Pythoneers reinvent the wheel?

2005-09-10 Thread Tim Daneliuk
Dennis Lee Bieber wrote:

 On 10 Sep 2005 05:36:08 EDT, Tim Daneliuk [EMAIL PROTECTED]
 declaimed the following in comp.lang.python:
 
 
 
On a more general note, for all the promises made over 3 decades about
how OO was the answer to our problems, we have yet to see quantum
 
 
   OO goes back /that/ far? (2 decades, yes, I might even go 2.5
 decades for academia G). My college hadn't even started structured
 programming (beyond COBOL's PERFORM statement) by the time I graduated
 in 1980. Well, okay... SmallTalk... But for most of the real world, OO
 became a known concept with C++ mid to late 80s.
 

OO ideas predate C++ considerably.  The idea of encapsulation and
abstract data types goes back to the 1960s IIRC.  I should point
out that OO isn't particularly worse than other paradigms for
claiming to be The One True Thing.  It's been going on for
almost a half century.   I've commented on this previously:

 http://www.tundraware.com/Technology/Bullet/

-- 

Tim Daneliuk [EMAIL PROTECTED]
PGP Key: http://www.tundraware.com/PGP/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: First release of Shed Skin, a Python-to-C++ compiler.

2005-09-10 Thread Paul Rubin
Mark Dufour [EMAIL PROTECTED] writes:
 After nine months of hard work, I am proud to introduce my baby to the
 world: an experimental Python-to-C++ compiler. 

Wow, looks really cool.  But why that instead of Pypy?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: encryption with python

2005-09-10 Thread Marc 'BlackJack' Rintsch
In [EMAIL PROTECTED], Steven
D'Aprano wrote:

 last_number_used = 12345
 usable_IDs = []
 
 def make_studentID():
 global last_number_used
 global usable_IDs
 if not usable_IDs:
 # generate another batch of IDs in random order
 usable_IDs = range(last_number_used, last_number_used + 1000)
- usable_IDs.sort(random.random())
+ random.shuffle(usable_IDs)
 last_number_used += 1000
 return usable_IDs.pop()

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


Re: encryption with python

2005-09-10 Thread James Stroud
On Saturday 10 September 2005 15:02, Ron Adam wrote:
 Kirk Job Sluder wrote:
 I would think that any n digit random number not already in the data
 base would work for an id along with a randomly generated password that
 the student can change if they want.  The service provider has full
 access to the data with their own set of id's and passwords, so in the
 case of a lost id, they can just look it up using the customers name
 and/or ssn, or whatever they decide is appropriate. In the case of a
 lost password, they can reset it and get another randomly generated
 password.

 Or am I missing something?

Yes and no. Yes, you are theoretically correct. No, I don't think you have the 
OP's original needs in mind (though I am mostly guessing here).  The OP was 
obviously a TA who needed to assign students a number so that they could 
anonymously check their publicly posted grades and also so that he could do 
some internal record keeping. 


But, I'm thinking no one remembers college here anymore. 

When I was in college (and when I TA'd) security was kind of flimsy. TAs kept 
all records of SS#s, etc. (etc. includes birthdays here) in a gradebook (or 
the rich ones kept them on a 5 1/4 floppy). Grades were reported publicly by 
full SS#s, usually on a centralized cork-board. That was back in the 
good-ole-days, before financial fraud was euphemised to identity theft.

When I TA'd several years later, grades were reported by the last n digits of 
the SS#. Some very security conscious TAs--or was it just me? I think it was 
just me--solicited pass phrases from each student and grades were reported 
based on the student generated pass phrase--and not on SS# or the like. These 
phrases usually came in the form of Buffs1 or Kitty1979 (the latter 
possibly revealing some information about a birthday, perhaps?). Some 
students didn't submit pass phrases, for whatever reason. I think I did the 
less convenient of the two most reasonable options, which was to withold 
reporting the grade to the student until they gave me a phrase. The other 
option was to use a default pass phrase of the last n digits of the SS#.

The idea of combining ID information and encrypting it to create another ID is 
a quantum leap beyond the primitive last n digits of the SS#. Does it beat, 
in theoretical terms, assigning random numbers? No. And it certainly doesn't 
beat, in theoretical terms, my improved one-time-pad protocol (see my 
previous email). I challenge even the most capable cryptographer to beat my 
improved one-time-pad protocol for security (Oh wait, here it is: 1. Destroy 
Data.) But it is convenient, especially if you discard the original 
identifying information and store just the hashes. And as far as collisions 
go, even if a class of 10,000 gives a 1% chance of collision, who is going to 
TA a class of 10,000 students. If you can promise that kind of enrolment for 
any department, much less any single class, there is a job in an Economics 
department waiting for you out there, my friend.

So what would be the alternative to ID information generated IDs? Have a 3xDES 
encrypted database with the SS# and birthday stored as plain-text? Better 
keep the encryption protocol secret! Oops. Screwed up already. I figured out 
the encryption protocol: Encrypt database with 3xDES using a secret key. 
Dang, security through obscurity. All they have to do is to get that secret 
key and all those records are easily readable.

The point is that *something has to be kept secret* for encryption security to 
work. Theoretically best would be a passphrase, or a passphrase to a really 
big key. So, perhaps we could modify the algorithm from a few messages back, 
in order to address the (assumed) *practical* considerations of the OP's 
original query:

import sha
def encrypt(x,y, password):
def _dosha(v): return sha.new(str(v)+str(password)).hexdigest()
return int(_dosha(_dosha(x)+_dosha(y))[5:13],16)

So now what is the criticism? That its still a secret algorithm because the 
password is secret?

James


-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: encryption with python

2005-09-10 Thread Paul Rubin
James Stroud [EMAIL PROTECTED] writes:
 Yes and no. Yes, you are theoretically correct. No, I don't think
 you have the OP's original needs in mind (though I am mostly
 guessing here).  The OP was obviously a TA who needed to assign
 students a number so that they could anonymously check their
 publicly posted grades and also so that he could do some internal
 record keeping.

If that's all it's about, it's not a big deal.  If it's for some central
administrative database that's more of a target, more care is warranted.

 The idea of combining ID information and encrypting it to create

The info to be combined was the student's birthdate.  Why would the TA
have access to either that or the SSN?

 import sha
 def encrypt(x,y, password):
 def _dosha(v): return sha.new(str(v)+str(password)).hexdigest()
 return int(_dosha(_dosha(x)+_dosha(y))[5:13],16)
 
 So now what is the criticism? That its still a secret algorithm
 because the password is secret?

That's sort of reasonable as long as the password really is secret and
you don't mind a small chance of two students getting the same ID
number once in a while.  If the password is something that a TA types
into a laptop when entering grades and which goes away after the
course ends, it's not such a big deal.  If it's a long-term key that
has to stay resident in a 24/7 server through the students' entire
time at the university and beyond, then the algorithm is the trivial
part and keeping the key secret is a specialized problem in its own
right.  For example, financial institutions use special, tamper
resistant hardware modules for the purpose.

Could the OP please say what the exact application is?  That might get
more useful responses if the question still matters.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: encryption with python

2005-09-10 Thread James Stroud
On Saturday 10 September 2005 16:30, Paul Rubin wrote:
 The info to be combined was the student's birthdate.  Why would the TA
 have access to either that or the SSN?

Speaking as a former TA, we had all that and a little more, if I remember 
correctly. The why aspect is a little beyond me.

James

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


[pyparsing] make sure entire string was parsed

2005-09-10 Thread Steven Bethard
How do I make sure that my entire string was parsed when I call a 
pyparsing element's parseString method?  Here's a dramatically 
simplified version of my problem:

py import pyparsing as pp
py match = pp.Word(pp.nums)
py def parse_num(s, loc, toks):
... n, = toks
... return int(n) + 10
...
py match.setParseAction(parse_num)
W:(0123...)
py match.parseString('121abc')
([131], {})

I want to know (somehow) that when I called match.parseString(), there 
was some of the string left over (in this case, 'abc') after the parse 
was complete.  How can I do this?  (I don't think I can do character 
counting; all my internal setParseAction() functions return non-strings).

STeVe

P.S.  FWIW, I've included the real code below.  I need to throw an 
exception when I call the parseString method of cls._root_node or 
cls._root_nodes and the entire string is not consumed.

--
# some character classes
printables_trans = _pp.printables.translate
word_chars = printables_trans(_id_trans, '()')
syn_tag_chars = printables_trans(_id_trans, '()-=')
func_tag_chars = printables_trans(_id_trans, '()-=0123456789')

# basic tag components
sep = _pp.Literal('-').leaveWhitespace()
alt_sep = _pp.Literal('=').leaveWhitespace()
special_word = _pp.Combine(sep + _pp.Word(syn_tag_chars) + sep)
supp_sep = (alt_sep | sep).suppress()
syn_word = _pp.Word(syn_tag_chars).leaveWhitespace()
func_word = _pp.Word(func_tag_chars).leaveWhitespace()
id_word = _pp.Word(_pp.nums).leaveWhitespace()

# the different tag types
special_tag = special_word.setResultsName('tag')
syn_tag = syn_word.setResultsName('tag')
func_tags = _pp.ZeroOrMore(supp_sep + func_word)
func_tags = func_tags.setResultsName('funcs')
id_tag = _pp.Optional(supp_sep + id_word).setResultsName('id')
tags = special_tag | (syn_tag + func_tags + id_tag)
def get_tag(orig_string, tokens_start, tokens):
 tokens = dict(tokens)
 tag = tokens.pop('tag')
 if tag == '-NONE-':
 tag = None
 functions = list(tokens.pop('funcs', []))
 id = tokens.pop('id', None)
 return [dict(tag=tag, functions=functions, id=id)]
tags.setParseAction(get_tag)

# node parentheses
start = _pp.Literal('(').suppress()
end = _pp.Literal(')').suppress()

# words
word = _pp.Word(word_chars).setResultsName('word')

# leaf nodes
leaf_node = tags + _pp.Optional(word)
def get_leaf_node(orig_string, tokens_start, tokens):
 try:
 tag_dict, word = tokens
 word = cls._unescape(word)
 except ValueError:
 tag_dict, = tokens
 word = None
 return cls(word=word, **tag_dict)
leaf_node.setParseAction(get_leaf_node)

# node, recursive
node = _pp.Forward()

# branch nodes
branch_node = tags + _pp.OneOrMore(node)
def get_branch_node(orig_string, tokens_start, tokens):
 return cls(children=tokens[1:], **tokens[0])
branch_node.setParseAction(get_branch_node)

# node, recursive
node  start + (branch_node | leaf_node) + end

# root node may have additional parentheses
cls._root_node = node | start + node + end
cls._root_nodes = _pp.OneOrMore(cls._root_node)
-- 
http://mail.python.org/mailman/listinfo/python-list


Fun with decorators and unification dispatch

2005-09-10 Thread talin at acm dot org
Been playing around a bit more with developing my python inference
engine, and I thought it might be of general interest (plus, I am
finding the criticism useful).

My original goal was to brush up on my math skills. Now, I've long felt
that the best way to learn a language *thoroughly* is to write a
compiler for it. So why not write a compiler for math?

However, algebra and calculus aren't just about evaluating an
expression and getting the answer, they are about *manipulating*
mathematical expressions, applying transformations to them. Systems
that do this (such as Mathematica and Yacas) are called computer
algebra systems, so I decided to see how hard it would be to implement
one in Python.

A computer algebra system is essentially a specialized kind of expert
system, in other words it is a set of transformation rules, where an
input expression is matched against a database of patterns, and the
result of the database query determines what transformation is to be
made.

Most such systems start by implementing their own, specialized
programming language, but I wanted instead to see if I could add a
inference engine capability to Python itself.

So what I have is a dispatch mechanism that maintains a database of
Python functions, keyed by the input pattern. Unlike multimethod
systems, the input patterns are not merely a list of types, but can be
trees containing both constants and variables.

Here's a trivial example, using the classic recursive algorithm for
computing the factorial of a number:

# Declare that Factor is a generic function
Factorial = Function()

# Define Factorial( 0 )
@Arity( 0 )
def Factorial():
return 1

# Define Factorial( x )
@Arity( MatchInteger.x )
def Factorial( x ):
return x * Factorial( x - 1 )

print Factorial( 12 )

Function produces an instance of a generic function, which is a
callable object. When called, it searches its list of patterns to
determine the function to dispatch.

The Arity decorator adds the function as a special case of the
generic function. It adds the specific function to the generic's
internal pattern database, and also returns the generic function as its
return result, so that that (in this case) the name Factorial is
bound to the generic function object.

MatchInteger is a class that produces a matching object, otherwise
known as a bound variable. In this case, the variable's name is x.
(It overloads the __getattr__ method to get the variable name.) When
the pattern matcher encounters a matching object, it attempts to bind
the corresponding portion of the expression to that variable. It does
this by adding the mapping of variable name to value into a dictionary
of variable bindings.

When the function is called, the dictionary of variable bindings is
expanded and passed to the function (i.e. func( *bindings )), so that
the variable that was bound to x now gets passed in as the x
parameter of the function.

MatchInteger itself is a type of qualified match (that is, a variable
that only binds under certain conditions), and could be defined as:

MatchInteger = QualifiedMatch( lambda x: type( x ) is int )

(Although it is not in fact defined this way.) QualifiedMatch takes a
list of matching preducates, which are applied to the expression before
binding can take place.

Here's a more complex example, which is a set of rules for simplifying
an expression:

Simplify = Function()

# x = x
@Arity( MatchAny.x )
def Simplify( x ):
return x

# x * 0 = 0
@Arity( ( Multiply, MatchAny.x, 0 ) )
def Simplify( x ):
return 0

# x * 1 = x
@Arity( ( Multiply, MatchAny.x, 1 ) )
def Simplify( x ):
return Simplify( x )

# x + 0 = x
@Arity( ( Add, MatchAny.x, 0 ) )
def Simplify( x ):
return Simplify( x )

# x + x = 2x
@Arity( ( Add, MatchAny.x, MatchAny.x ) )
def Simplify( x ):
return (Multiply, 2, Simplify( x ) )

# General recursion rule
@Arity( ( MatchAny.f, MatchAny.x, MatchAny.y ) )
def Simplify( f, x, y ):
return ( Simplify( f ), Simplify( x ), Simplify( y ) )

And in fact if I call the function:

print Pretty( Simplify( Parse( (x + 2) * 1 ) ) )
print Pretty( Simplify( Parse( x * 1 + 0 ) ) )
print Pretty( Simplify( Parse( y + y ) ) )
print Pretty( Simplify( Parse( (x + y) + (x + y) ) ) )

It prints:

x + 2
x
2 * y
2 * (x + y)

The argument matcher tries to prioritize matches so that more specific
matches (i.e. containing more constants) are matched before more
general matches. This is perhaps too unsophisticated a scheme, but it
seems to have worked so far.

The pattern matcher also looks to see if the object being matched has
the commute or associate property. If it finds commute it
attempts to match against all posssible permutations of the input
arguments (with special optimized logic for functions of one and two
arguments). If the associate property is found, it first tries to
flatten the expression (transforming (+ a (+ b c)) into (+ a b c), and
then generating all possible partitions of the arguments into two sets,
before attempting 

Re: Why do Pythoneers reinvent the wheel?

2005-09-10 Thread François Pinard
[Tim Daneliuk]

 OO ideas predate C++ considerably.  The idea of encapsulation and
 abstract data types goes back to the 1960s IIRC.

Did not Simula-67 have it all already?

When C++ came along, much later, I asked someone knowledgeable in the
field of language design what was his opinion about C++.  He answered
very laconically: Simula-- . And this was not far from fully true:
Simula had many virtues which are still missing from C++.

Moreover, a language like Simula cannot be made up of thin air, it only
crystallizes a long maturation of many trends.  The term OO may have
been coined later, but the concepts were already there.  In computer
science, I often saw old concepts resurrecting with new names, and then
mistaken for recent inventions.  New ideas are not so frequent...

-- 
François Pinard   http://pinard.progiciels-bpi.ca
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fun with decorators and unification dispatch

2005-09-10 Thread Paul Rubin
talin at acm dot org [EMAIL PROTECTED] writes:
 # Declare that Factor is a generic function
 Factorial = Function()

Was the comment a typo for Factorial?

 # Define Factorial( 0 )
 @Arity( 0 )
 def Factorial():
 return 1

Overriding old definition of Factorial

 # Define Factorial( x )
 @Arity( MatchInteger.x )
 def Factorial( x ):
 return x * Factorial( x - 1 )

Overriding it again

 print Factorial( 12 )

I'm confused, how did it know what to do?  Are you using some reflection
hack in the MatchInteger decorator to figure out the function name?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python compiled?

2005-09-10 Thread Terry Reedy

Robert Kern [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 Terry Reedy wrote:
 Nor has 386 'machine language' suffered from being interpreted, at a 
 deeper
 level, by microcode.

 I think both you and Paul may be missing Tim's point. I don't think he's
 talking about suffering in technical respects, like speed. He's
 talking about popularity.

I knew that ;-)  and meant that ;;--)).

Of course, many people don't know that 'machine code' is often now 
interpreted -- nor that Python code is compiled to something by all current 
implementations.  Perhaps we should call the CPython 'interpreter' the 
CPython interpreter-compiler that it is.

Terry J. Reedy
 



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


Expected Value

2005-09-10 Thread George
How would I get the expected value out of this information. I have
tried many times to understand this but am unable to.

  The function expectP(z) computes E(X) for the random variable
representing a sample over the probability generated by pf for the
set of discrete items [1,10]. The constant c is needed so that the
probability sum to 1.0, as required by the definition of probability.
The function pf(r,c,x) returns the probability that item x will be
equal to a randomly chosen variable, denoted by P(x) or P(X=x) in
mathematical texts, where c is the constant mentioned above and r is
needed because we are considering a power law distribution.

The function expectP(z) computes E(X) with r=z, using pf(r,c,x) where x
ranges over the set of discrete items in [1,10]

The program:

def norm(r):
  calculate normalization factor for a given exponent
  # the function for this distribution is  P(x) = c*(x**-r)
  # and the job of this norm function is to calculate c based
  # on the range [1,10**5]
  sum = 0.0
  for i in range(1,1+10**5):
 # final sum would be more accurate by summing from small values
 # to large ones, but this is just a homework, so sum 1,2,..10**5
 sum += float(i)**-r
  return 1.0/sum

def pf(r,c,x):
  return a power-law probability for a given value
  # the function for this distribution is  P(x) = c*(x**-r)
  # where the constant c is such that it normalizes the distribution
  return c*(float(x)**-r)

#- between these lines, define expectP() function
-


#- end of expectP() definition


def showExpectP(limit):
  display ExpectP(limit) by rounding down to nearest integer
  k = expectP(limit)
  return int(k)

if __name__ == '__main__':
  import doctest, sys 
  doctest.testmod(sys.modules[__name__])

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


Re: Fun with decorators and unification dispatch

2005-09-10 Thread talin at acm dot org
Yes, it was a typo.

Even thought the function has not yet been bound to the name
Factorial when it calls the decorator, the function's __name__
attribute is set to it, so I use that to look up the name of the
generic.

Here''s the source for Arity:

def Arity( *pattern ):
A function decorator that defines a specific arity of a generic
function. This registers the
specific implementation with the generic function (which must be in
the global scope.)

def inner( f ):
if isinstance( f, Function ):
generic = f
f = generic.last_defined
else:
name = f.__name__
if not name in f.func_globals:
raise Exception( Generic function  + name +  has not
been defined. )
generic = f.func_globals[ name ]
generic.name = name
generic.last_defined = f
generic.add_arity( pattern, f )
return generic
return inner

There's a couple of kludges here:

1) The Generic doesn't know its own name until you define at least one
specialization for it. Otherwise, you would have to say:

Factorial = Function( Factorial )

which I consider verbose and redundant.

2) The whole last_defined thing is to allow multiple arities for a
single specialized function. Since the arity normally returns the
generic, and not the specialized func, as the return value, that means
that any additional decorators will be applied to the generic rather
than the specialized func. last_defined is a kludge for getting around
that, but only for decorators that understand the situation.

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


Re: calling command line programs?

2005-09-10 Thread Grant Edwards
On 2005-09-10, chriss [EMAIL PROTECTED] wrote:

 Take a look at os.popen, os.spawn, or the popen2, and
 subprocess modules.
 
 That last one seems to be gaining popularity.

 The suggested modules and functions have been deprecated according to the
 python 2.4 docs. The doc suggests to use the functions in the 'subprocess'
 module.

The subprocess module is depricated?

-- 
Grant Edwards   grante Yow!  Did you move a lot
  at   of KOREAN STEAK KNIVES this
   visi.comtrip, Dingy?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to handle very large MIME Messages with the email package?

2005-09-10 Thread Terry Reedy

[EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 Looking at the email package, it seems all the MIMExxx classes takes
 string but not file object as the payload. I need to handle very large
 MIME messages say up to 100M. And possibly many of them. Is email
 package sufficient. If not is it possible to extend it?

I presume so.  I am also pretty sure that someone has revised parts of the 
email package.  When the next PyDev summary (for the last half of August) 
appears, you can check.  If so, you might possibly ask the reviser if such 
an extension has been done or planned.

Terry J. Reedy



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


Re: encryption with python

2005-09-10 Thread Ron Adam
James Stroud wrote:
 On Saturday 10 September 2005 15:02, Ron Adam wrote:
 
Kirk Job Sluder wrote:
I would think that any n digit random number not already in the data
base would work for an id along with a randomly generated password that
the student can change if they want.  The service provider has full
access to the data with their own set of id's and passwords, so in the
case of a lost id, they can just look it up using the customers name
and/or ssn, or whatever they decide is appropriate. In the case of a
lost password, they can reset it and get another randomly generated
password.

Or am I missing something?
 
 
 Yes and no. Yes, you are theoretically correct. No, I don't think you have 
 the 
 OP's original needs in mind (though I am mostly guessing here).  The OP was 
 obviously a TA who needed to assign students a number so that they could 
 anonymously check their publicly posted grades and also so that he could do 
 some internal record keeping. 
 
 But, I'm thinking no one remembers college here anymore. 

Last semester I took, I was able to check my grades by logging into a 
web page with my student ID and using a password.  The password default 
was my SSN, we could change it. In any case students have read only 
access and are not able to change anything.  Not a big deal and very 
little personal information was visible.  If any one would have bothered 
to look they would have simply found out I had very good grades.  shrug


 The point is that *something has to be kept secret* for encryption security 
 to 
 work. Theoretically best would be a passphrase, or a passphrase to a really 
 big key. So, perhaps we could modify the algorithm from a few messages back, 
 in order to address the (assumed) *practical* considerations of the OP's 
 original query:

The actual database files should not be directly reachable, except by 
the appropriate data base administrators, it should send and retrieve 
information based on the users access rights via a server.

Is this a case where each account is encrypted with a different key in 
addition to the access rights given to each user?

Cheers,
Ron



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


Re: Python versus Perl

2005-09-10 Thread Terry Reedy

Roy Smith [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 Dieter Vanderelst [EMAIL PROTECTED] wrote:
 1 - How does the speed of execution of Perl compares to that of Python?

 To a first-order approximation, Perl and Python run at the same speed.

'Speed of execution' is a feature of an inplementation, not of languages 
themselves.  Different implementations of Python (for instance, CPython 
versus CPython+Psyco) can vary in speed by more than a factor of 10 for 
particular blocks of Python code.

(Yes, I know you are comparing the stock standard implementations, but my 
point still stands.)

 They are both interpreted languages.

To be useful, every language has to be interpreted sometime by something. 
In the narrow technical sense that I presume you mean, 'interpretation' 
versus 'compilation' is again an implementation feature, not a language 
feature.  As far as I know, neither Perl nor Python has an implementation 
that directly interprets in the way that Basic or tokenized Basic once was.

I am being picky because various people have claimed that Python suffers in 
popularity because it is known as an 'interpreted language'.  So maybe 
advocates should be more careful than we have been to not reinforce the 
misunderstanding.

Terry J. Reedy



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


Re: Escaping certain characters

2005-09-10 Thread Kent Johnson
Jan Danielsson wrote:
 Robert Kern wrote:
 [---]
 
Hmm... On second thought, I need to escape more characters.

Is there no other way to escape characters in strings?

Which characters?
 
 
I need to escape '\n', '', '[' and ']'. I finally went with a few of
 these:
 string.replace('\n', '\\n')
 string.replace('', '\\')

You might like this recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330

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


Re: Python linear algebra module -- requesting comments on interface

2005-09-10 Thread D H
Terry Reedy wrote:
 [EMAIL PROTECTED] wrote in message 
 news:[EMAIL PROTECTED]
 
The module will be public domain.
 
 
 Various lawyers have suggested that either you cannot do that (is US) or 
 that you should not do that.  (You know the joke -- ask two lawyers and you 
 get three opinions -- but all depends on your country of residence.)

Well he can do it, but you are right, it is best not too.
If anything, using an open source license will encourage people to share 
back any additions or bug fixes they make.
ANTLR for example was public domain (still is for version 2), but 
switched to BSD for version 3: http://www.antlr.org/license.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: make sure entire string was parsed

2005-09-10 Thread Paul McGuire
Steven -

Thanks for giving pyparsing a try!  To see whether your input text
consumes the whole string, add a StringEnd() element to the end of your
BNF.  Then if there is more text after the parsed text, parseString
will throw a ParseException.

I notice you call leaveWhitespace on several of your parse elements, so
you may have to rstrip() the input text before calling parseString.  I
am curious whether leaveWhitespace is really necessary for your
grammar.  If it is, you can usually just call leaveWhitespace on the
root element, and this will propagate to all the sub elements.

Lastly, you may get caught up with operator precedence, I think your
node assignment statement may need to change from
node  start + (branch_node | leaf_node) + end
to
node  (start + (branch_node | leaf_node) + end)

HTH, 
-- Paul

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


Re: List of integers L.I.S. (SPOILER)

2005-09-10 Thread Tim Peters
[Bryan Olson, on the problem at
http://spoj.sphere.pl/problems/SUPPER/
]
 I never intended to submit this program for competition. The
 contest ranks in speed order, and there is no way Python can
 compete with truly-compiled languages on such low-level code.
 I'd bet money that the algorithm I used (coded in C) can run
 with the winners. I also think I'd wager that the Python version
 outright trumps them on code size.

Oh, it's not that bad wink.  I took a stab at a Python program for
this, and it passed (3.44 seconds).  It just barely made it onto the
list of best solutions, which I also guess is ranked by elapsed
runtime.  The Java program right above it took 2.91 seconds, but ate
more than 27x as much RAM ;-)

I didn't make any effort to speed this, beyond picking a reasonable
algorithm, so maybe someone else can slash the runtime (while I
usually enjoy such silliness, I can't make time for it at present). 
I'll include the code below.

Alas, without access to the input data they use, it's hard to guess
what might be important in their data.  On my home box, chewing over
random 100,000-element permutations took less than a second each
(including the time to generate them); I'm pretty sure they're using
slower HW than mine (3.4 GHz P5).

 My first version bombed for the zero-length sequence. That was a
 mistake, sorry, but it may not be one of their test-cases. I
 wonder how many of the accepted entries would perform properly.

No idea here, and didn't even think about it.

Notes:  the `all` returned by crack() is a list such that all[i] is
list of all (index, value) pairs such that the longest increasing
subsequence ending with `value` is of length i+1; `value` is at index
`index` in the input permutation.  The maximal LISs thus end with the
values in all[-1].  findall() iterates backwards over `all`, to
accumulate all the values that appear in _some_ maximal LIS.  There's
clearly wasted work in findall() (if someone is looking for an
algorithmic point to attack).  Curiously, no use is made of that
values are integers, outside of input and output; any values with a
total ordering would work fine in crack() and findall().


# http://spoj.sphere.pl/problems/SUPPER/

def crack(xs):
from bisect import bisect_right as find
smallest = []
all = []
n = 0
for index, x in enumerate(xs):
i = find(smallest, x)
if i == n:
smallest.append(x)
all.append([(index, x)])
n += 1
else:
all[i].append((index, x))
if x  smallest[i]:
smallest[i] = x
return all

def findall(all):
constraints = all[-1]
allints = [pair[1] for pair in constraints]
for i in xrange(len(all) - 2, -1, -1):
survivors = []
for pair in all[i]:
index, value = pair
for index_limit, value_limit in constraints:
if index  index_limit and value  value_limit:
survivors.append(pair)
allints.append(value)
break
constraints = survivors
return sorted(allints)

def main():
import sys
while 1:
n = sys.stdin.readline()
if not n:
break
n = int(n)
perm = map(int, sys.stdin.readline().split())
assert n == len(perm)
supers = findall(crack(perm))
perm = None # just to free memory
print len(supers)
print  .join(map(str, supers))

if __name__ == __main__:
main()

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


Re: encryption with python

2005-09-10 Thread Kirk Job Sluder
Paul Rubin http://[EMAIL PROTECTED] writes:

 Kirk Job Sluder [EMAIL PROTECTED] writes:
  Likewise, credit companies are currently making money hand-over-fist.
  If an identity is compromised, it's cheaper for them to just close the
  account, refund the money, and do their own fraud investigation after
  the fact.
 
 You don't get it.  Refunding the money improperly charged on a single
 card doesn't begin to compensate for the hassle of undoing an identity
 theft.  If airlines worked the way you're suggesting the credit
 industry should work, and a plane went down, the airline would be off
 the hook by refunding your estate the price of your ticket.  It's only
 because they face much further-reaching liability than that, that they
 pay so much attention to safety.

Oh, I'm not suggesting the credit industry should work that way.  I'm
just saying that's the way they will work as long as they can push off
the costs for dealing with problems onto interest rates and other fees.  


-- 
Kirk Job-Sluder
The square-jawed homunculi of Tommy Hilfinger ads make every day an
existential holocaust.  --Scary Go Round
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: First release of Shed Skin, a Python-to-C++ compiler.

2005-09-10 Thread adDoc's networker Phil
experimental Python-to-C++ compiler.why that instead of Pypy?

. pypy compiles to llvm (low-level virtual machine) bytecode
which is obviously not as fast as the native code coming from c++ compilers;
but the primary mission of pypy 
is just having a python system that is 
written in something like python rather than c or c++
. there is no reason why the pypy project can't have a .NET architecture 
instead of the java-like arrangement I assume it has now
. without such a pypy.NET system,
shedskin is offering a service that pypy can't yet provide:
a ( python - c++ )-conversion allows me to 
smoothly integrate python contributions 
with my already-staggering c++ library
. I'm not suggesting that pypy should be another
Mono rewritten in python,
because the essential mission of the .NET architecture 
is being able to compile 
any language of the user`s choice,
to some intermediate language designed to be 
far more efficiently compiled to 
any machine language of the user`s choice
than any human-readable language such as c++ 
. perhaps llvm bytecode can serve as such an intermediate language?
then llvm could be the new c++ (our defacto IL (intermediate language))
and shedskin (python - IL=c++) could then be replaced by 
the combination of pypy (python - IL=llvm)
and some incentive for all target platforms 
to develope a highly optimized 
( llvm - native code)-compiler 
-- assuming also, that there is available 
a highly optimized ( c++ - llvm bytecode )-compiler .

-- American Dream Documentshttp://www.geocities.com/amerdreamdocs/home/(real opportunity starts with real documentation)
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: simple problem with os.rename() parameters - path with spaces

2005-09-10 Thread Peter Hansen
Tom wrote:
 Peter Hansen wrote:
 Where do you think those double quotation marks came from?  What 
 happens if you try the following instead of using the variables you 
 were trying to use?

 os.rename(e:\\music\\Joni Mitchell\\ogg-8,
  e:\\music.ogg\\Joni Mitchell\\ogg-8)

 Now try it with this and observe how you get (I predict) the same 
 error message as you originally got, and note what your mistake was:

 os.rename('e:\\music\\Joni Mitchell\\ogg-8',
  'e:\\music.ogg\\Joni Mitchell\\ogg-8')
 
 
 This produced the msg:
 OSError: [Errno 22] Invalid argument

Presumably this means the second one, whereas for the first you got a 
different message?  The latter is clearly invalid, since paths can't 
contain quotation marks.  The former would work provided the folder 
music.ogg/Joni Mitchell existed.

 The problem seems to be that I'm trying to create more than one 
 directory at a time.  In the above example, the dir 'Joni Mitchell' 
 doesn't exist.

If that were true, and the only problem, you would get a different 
error: OSError: [Errno 2] No such file or directory

 The functions that I'm calling (os.rename and shutil.move) use mkdir, 
 not makedirs.  The solution is for me to use makedirs with all of the 
 path except the leaf before I move/rename the old dir.

Regardless of the issue with error messages, that sounds like it does 
explain your problem.  Great! :-)

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


Re: encryption with python

2005-09-10 Thread Kirk Job Sluder
Ron Adam [EMAIL PROTECTED] writes:

 Kirk Job Sluder wrote:
 
  They want to be able to get their information by
  calling a phone number and saying a few words/phrases they memorized in
  childhood.  Given the current market, it seems to be cheaper to deal
  with breaks after the fact than to expect more from customers.
 
 I would think that any n digit random number not already in the data
 base would work for an id along with a randomly generated password
 that the student can change if they want.  The service provider has
 full access to the data with their own set of id's and passwords, so
 in the case of a lost id, they can just look it up using the customers
 name and/or ssn, or whatever they decide is appropriate. In the case
 of a lost password, they can reset it and get another randomly
 generated password.
 
 Or am I missing something?

Not really.  My suggestion is that in many cases, if the data is being
used only as a backup password or authentication token, there is no need
for that data to be stored in plaintext.  For example, with the
ubiquitous mother's maiden name * there is frequently no need to
actually have Smith, Jones, or Gunderson in the database.
bf65d781795bb91ee731d25f9a68a5aeb7172bc7 serves the same purpose.

There are other cases where one-way anonymity is better than a table
linking people to randomly generated userIDs.  I'd rather use
cryptographic hashes for research databases than keep a table matching
people to random numbers hanging around.  But I'm weird that way.  

* I think mother's maiden name is a really poor method for backup
  authentication because for a fair number of people in the U.S., it
  will be identical to their current surname, and for the rest, it's
  trivial to discover.  

 
 Cheers,
 Ron
 

-- 
Kirk Job-Sluder
The square-jawed homunculi of Tommy Hilfinger ads make every day an
existential holocaust.  --Scary Go Round
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about consistency in python language

2005-09-10 Thread Steve Holden
Kay Schluehr wrote:
 Mike Meyer wrote:
 
 
Yes, but the function sorted is more useful than a list method
sorted in a duck typing language.
 
 
 I don't see what this has to do with duck typing? sorted() is simply
 a generic function accepting different types. I'm not aware that
 sorted() requires a specific interface of those types it accepts.
 
Just because you aren't aware of something doesn't stop it being true. 
The argument must be iterable, and there's a specific protocol for that.
 
The function sorted works on all iterators. I can do:

Ah, so you *were* aware of it.

def t(n):
  for i in range(n):
yield i
...
print sorted(t(5))

and have it work.

If sorted were a method of a class - the it'd have to be implemented
again for every class iterable class. Either that, or you'd have to
create an abstract parent of all iterable classes to add it to - which
seems more appropriate for a BD language than Python.
 
 
 Instead of extending a class hierarchy it might even be possible to
 hook a trait into the class by means of a __traits__ attribute.
 
 http://fsl.cs.uiuc.edu/~mhills/presentations/TraitsPresentation.pdf
 
 Generators as well as lists and tuples would provide a sortable trait.
 The sorted() function could remain available for convenience.
 
The advantage being ... ? Perhaps you have just discovered a really 
interesting hammer, and are seeing this problem as a nail?
 
And even if you do add the abstract class, how do you make my example
work without explictly converting the iterator to a list type?
 
 
 I don't know how sorted() is implemented? A naive implementation would
 in fact be nothing else then:
 
 def sorted(iter):
 l = list(iter)
 l.sort()
 return l
 
 Kay
 
That would indeed be a naïve implementation. The implementation is, of 
course, an implementation detail ;-) In this case it requires that 
sort() then provides all the magic - the need for magic doesn't go away!

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

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


getting words from readline

2005-09-10 Thread Adam

How is best to extract word strings from a 
line = infile.readline()  

I wish to quickly check the first word of 
each line of a text file. 

Indeed, How do I break a lineinput() line 
into component words (separate by spaces etc) ?

Should I be looking at; 
Re Parser Slice StringIO ? 


Any help appreciated. 




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


  1   2   >