Re: python - regex handling

2006-07-04 Thread Paolo Pantaleo
2006/7/4, bruce <[EMAIL PROTECTED]>:
> hi...
>
> does python provide regex handling similar to perl. can't find anything in
> the docs i've seen to indicate it does...
>
> -bruce
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
http://python.org/doc/2.4.1/lib/module-re.html

Here is the documentation about re, I think that if you spend an hour
or two reading it, you will know about everything you need.

Paolo

-- 
If you like Python as I do, you can find useful my little Python resource center
http://ppp3.co.nr
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Code design for a sub-class of built-ins

2006-07-04 Thread Steven D'Aprano
On Tue, 04 Jul 2006 18:25:14 +, Dennis Lee Bieber wrote:

>   I suspect what you really want (I'm not going to open an interpreter
> to test) is:
> 
>   def __init__(self, value, extra=None):
>   int.__init__(self, value)

Yes, that's exactly what I meant -- it was a copy-and-paste and I didn't
clean it up correctly.

Thanks,


-- 
Steven.

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


Re: Code design for a sub-class of built-ins

2006-07-04 Thread Steven D'Aprano
On Tue, 04 Jul 2006 19:26:36 +0200, Bruno Desthuilliers wrote:

> Steven D'Aprano wrote:
>> I'm having problems with sub-classes of built-in types.
>> 
>> Here is a contrived example of my subclass. It isn't supposed
>> to be practical, useful code, but it illustrates my problem.
>> 
>> class MyStr(str):
>> """Just like ordinary strings, except it exhibits special behaviour
>> for one particular value.
>> """
>> magic = "surprise"
> 
>> def __init__(self, value):
>> str.__init__(self, value)
> 
> You don't need to override __init__ here.

Perhaps not, but in a more realistic example I might need to.


>> def __len__(self):
>> if self == self.magic:
>> print "Nobody expects the Spanish Inquisition!"
>> return str.__len__(self)
>> def upper(self):
>> if self == self.magic:
>> print "Nobody expects the Spanish Inquisition!"
>> return str.upper(self)
>> # and so on for every last string method...
> 
> My my my

Exactly. Don't think I don't know this is a horrible strategy -- that's
why I'm asking for ideas for a better way to do it *wink*


>> The obvious problem is, I have to create a custom method for every
>> string method --
> 
> which makes subclassing almost useless - except to pass isinstance()
> tests.

Yep.


>> and if strings gain any new methods in some future version of Python,
>> my subclass won't exhibit the correct behaviour.
> 
> You could replace subclassing by composition/delegation using the
> __getattr__ hook, but this would break tests on type/class :(
>
> Or you could keep subclassing and use the __getattribute__ hook, but
> this is more tricky and IIRC may have negative impact on lookup perfs.
> 
> Or you could use a metaclass to decorate (appropriate) str methods with
> a decorator doing the additional stuff.
> 
> choose your poison !-)

Interesting... That will give me something to experiment with.


Thanks,


-- 
Steven.

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


RE: defining multi dimensional array

2006-07-04 Thread Steven D'Aprano
On Tue, 04 Jul 2006 22:07:59 -0700, bruce wrote:

> do i need to import anything for this.. or is it supposed to work out of the
> box..

Why don't you try it, and see if it gives you a result or raises an
exception? The exception (if any) will give you a clue what you need to do.

> and just what is it doing!

The original list comprehension is:

[[None for x in xrange(10)] for y in xrange(10)]

This is a list comprehension inside another list comprehension. Start from
the inside and work out. The inside list comp is:

[None for x in xrange(10)]

That is equivalent to:

result = []
for x in xrange(10):
result.append(None)

and it creates a list of ten None items.

The outer list comp is 

[list_comp for y in xrange(10)]

where list_comp is the inner list comprehension. It also creates a list
of ten items, where each item is [None,None,None]. But the important
thing is that each of the inner lists are DIFFERENT lists that just happen
to share the same value (ten None items), rather than the same list
repeated ten times.

-- 
Steven.

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


Re: numarray

2006-07-04 Thread Robert Kern
bruce wrote:
> robert
> 
> i did an
>  python>>> import numpy
>  a = array([['q','a'],['w','e']])
> 
> and it didn't work...
> 
> i used
>   >>from import numpy *
> 
> and it seems to accept the 'array' word.. .looks like it will work...
> 
> what's the difference between 'import numpy', and "from import numpy *"

Okay, you need to go through the tutorial first:

   http://docs.python.org/tut/tut.html

And also, you'll want to read this, too:

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

Probably the best place to get your current questions answered is the 
python-tutor mailing list.

   http://mail.python.org/mailman/listinfo/tutor

-- 
Robert Kern

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

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


RE: defining multi dimensional array

2006-07-04 Thread bruce
do i need to import anything for this.. or is it supposed to work out of the
box..

and just what is it doing!

-bruce


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf
Of tac-tics
Sent: Tuesday, July 04, 2006 9:53 PM
To: python-list@python.org
Subject: Re: defining multi dimensional array



bruce wrote:
> hi...
>
> basic question..
>
> how do i define a multi dimensional array
>
>  a[10][10]

I find that list comprehensions are useful for this.

[ [None for x in xrange(10)] for y in xrange(10)]

You could easily write a wrapper for it to clean the syntax a bit.

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

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


RE: numarray

2006-07-04 Thread bruce
robert

i did an
 python>>> import numpy
 a = array([['q','a'],['w','e']])

and it didn't work...

i used
  >>from import numpy *

and it seems to accept the 'array' word.. .looks like it will work...

what's the difference between 'import numpy', and "from import numpy *"

comments...

thanks

-bruce


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf
Of Robert Kern
Sent: Tuesday, July 04, 2006 9:42 PM
To: python-list@python.org
Subject: Re: numarray


bruce wrote:
> hi...
>
> i'm trying to find numarray.. i found the numpy on sourceforge and
> downloaded/installed..
>
> i did a
> python>> import numarray
>
> and got an error...

Never just say "I got an error." It tells us nothing. Copy-and-paste the
exact
error message. I presume, however, that you installed numpy, not numarray.
They
are not the same thing.

> the docs that i've seen point to the sourceforge area.. but i only see
> numpy.. which appears to incorporate numarray..

No, it replaces numarray.

http://www.scipy.org/NumPy

> my goal is to somehow define multi-dimensional arrays of strengs...

 >>> from numpy import *
 >>> a = array([['some', 'strings'],['in an', 'array']], dtype=object)
 >>> a
array([[some, strings],
[in an, array]], dtype=object)

--
Robert Kern

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

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

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


Re: defining multi dimensional array

2006-07-04 Thread tac-tics

bruce wrote:
> hi...
>
> basic question..
>
> how do i define a multi dimensional array
>
>  a[10][10]

I find that list comprehensions are useful for this.

[ [None for x in xrange(10)] for y in xrange(10)]

You could easily write a wrapper for it to clean the syntax a bit.

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


Re: use var to form name of object

2006-07-04 Thread gel

gel wrote:

> Hi
> I would like to pass a variable in and use it as part of a name of an
> object, something like below, where I pass the variable software into
> the function and use it as part of the name of the object so that I can
> append to it using the other vairables.  Any suggestions?
>
>
> def a_d(self,software,mac,time,extra):
> global d_software
> d_software.software.append([mac,time,extra])

I sorted it out

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


Re: numarray

2006-07-04 Thread Robert Kern
bruce wrote:
> hi...
> 
> i'm trying to find numarray.. i found the numpy on sourceforge and
> downloaded/installed..
> 
> i did a
> python>> import numarray
> 
> and got an error...

Never just say "I got an error." It tells us nothing. Copy-and-paste the exact 
error message. I presume, however, that you installed numpy, not numarray. They 
are not the same thing.

> the docs that i've seen point to the sourceforge area.. but i only see
> numpy.. which appears to incorporate numarray..

No, it replaces numarray.

http://www.scipy.org/NumPy

> my goal is to somehow define multi-dimensional arrays of strengs...

 >>> from numpy import *
 >>> a = array([['some', 'strings'],['in an', 'array']], dtype=object)
 >>> a
array([[some, strings],
[in an, array]], dtype=object)

-- 
Robert Kern

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

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


Re: [Mailman-Developers] Parsing and Rendering rfc8222

2006-07-04 Thread Mark Sapiro
Brad Knowles wrote:

>Ethan said:
>
>> I plan on using [2] to generate mbox thread indexes for rapid navigation
>> of lists. Any suggestions for more robust variants would be welcome;
>> feedback on how to handle threading for message-id-less messages would
>> also be welcome.
>
>All messages should have message-ids -- this is one of the most basic
>requirements of the Internet e-mail related RFCs.  If nothing else, the
>local MTA on the Mailman server should have provided a message-id.

I interpreted Ethan's concern to be messages that lack References: and
In-Reply-To: rather than a Message-ID: per se. Also, generating a
Message-ID: at archiving time does no good (at least in the absence of
an archive interface to allow replying to an archive post) because
it's too late to get that id into References: and/or In-Reply-To: of
email replies.

Also there is a related issue if A posts, B replies, A replies off list
to B, and B replies on list. If threading relies solely on References:
or In-Reply-To:, and either A's or B's MUA generates only In-Reply-To,
this thread is broken at the 'missing' post. I don't have any really
good suggestions for alternative threading algorithms however. I think
there was something on this not too long ago on mailman-users or maybe
mailman-developers - I looked and found what I think I remember. The
relevant post is at

and points to a description of an algorithm at
.

-- 
Mark Sapiro <[EMAIL PROTECTED]>   The highway is for gamblers,
San Francisco Bay Area, Californiabetter use your sense - B. Dylan

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


RE: defining multi dimensional array

2006-07-04 Thread bruce
i tried to do 

a1[10][10] = ['a','q']

and get an error saying a1 is not defined

-bruce


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf
Of Erik Max Francis
Sent: Tuesday, July 04, 2006 9:14 PM
To: python-list@python.org
Subject: Re: defining multi dimensional array


bruce wrote:

> basic question..
> 
> how do i define a multi dimensional array
> 
>  a[10][10]
> 
> is there a kind of a = array(10,10)

It's just a list of lists.

-- 
Erik Max Francis && [EMAIL PROTECTED] && http://www.alcyone.com/max/
  San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
   Have you ever loved somebody / Who didn't know
-- Zhane
-- 
http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


use var to form name of object

2006-07-04 Thread gel
Hi
I would like to pass a variable in and use it as part of a name of an
object, something like below, where I pass the variable software into
the function and use it as part of the name of the object so that I can
append to it using the other vairables.  Any suggestions?


def a_d(self,software,mac,time,extra):
global d_software
d_software.software.append([mac,time,extra])

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


Re: weird error messages on application exit

2006-07-04 Thread ianaré
I tried downloading and installing from the fedora site and got this:
gtk2-2.8.19-2.i386.rpm is already installed

However, the problem seems to have gone away. Not sure what happened
there.
Thank you for the reply.

gtk2-2.8.19-2.i386.rpm is already installed


Frank Millman wrote:
> ianaré wrote:
> > Running FC5 with Gnome (rather minimal install), using latest wxPython
> > version installed from wxPython.org RPMs since I needed unicode and FC5
> > only has ansi available in the package manager. Anyway the errors occur
> > every time i close out of my wxPython application, and sometimes...
> > maybe every 10 - 15 times... i need to kill the app.
> > I have no idea what all this means, could someone please point me in
> > the right direction? Thanks!
> >
> >
> >
> > (python:5584): Gdk-CRITICAL **: gdk_drawable_get_depth: assertion
> > `GDK_IS_DRAWABLE (drawable)' failed
> >
>
> I had the same problem. Here is a link to the whole story, together
> with the solution, from the wxPython mailing list archive.
> 
> http://tinyurl.com/o4jsu
> 
> Frank Millman

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


numarray

2006-07-04 Thread bruce
hi...

i'm trying to find numarray.. i found the numpy on sourceforge and
downloaded/installed..

i did a
python>> import numarray

and got an error...

the docs that i've seen point to the sourceforge area.. but i only see
numpy.. which appears to incorporate numarray..

my goal is to somehow define multi-dimensional arrays of strengs...

pointers...

thanks

-bruce

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


Re: defining multi dimensional array

2006-07-04 Thread Erik Max Francis
bruce wrote:

> basic question..
> 
> how do i define a multi dimensional array
> 
>  a[10][10]
> 
> is there a kind of a = array(10,10)

It's just a list of lists.

-- 
Erik Max Francis && [EMAIL PROTECTED] && http://www.alcyone.com/max/
  San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
   Have you ever loved somebody / Who didn't know
-- Zhane
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: defining multi dimensional array

2006-07-04 Thread bruce
update...

i need a multi dimensional array of lists...

 ie 
  [q,a,d]
  [q1,a1,d1]
  [q2,a2,d2]
  [q3,a3,d3]

which would be a (3,4) array...


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf
Of bruce
Sent: Tuesday, July 04, 2006 8:15 PM
To: python-list@python.org
Subject: defining multi dimensional array


hi...

basic question..

how do i define a multi dimensional array

 a[10][10]

is there a kind of a = array(10,10)

thanks

-bruce

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


defining multi dimensional array

2006-07-04 Thread bruce
hi...

basic question..

how do i define a multi dimensional array

 a[10][10]

is there a kind of a = array(10,10)

thanks

-bruce

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


Re: finding items that occur before or after an item in lists

2006-07-04 Thread Simon Forman
Simon Forman wrote:
> I've got a function that I'd like to improve.
>
> It takes a list of lists and a "target" element, and it returns the set
> of the items in the lists that appear either before or after the target
> item.  (Actually, it's a generator, and I use the set class outside of
> it to collect the unique items, but you get the idea. ;-) )
>
> data = [
> ['this', 'string', 'is', 'nice'],
> ['this', 'string', 'sucks'],
> ['string', 'not', 'good'],
> ['what', 'a', 'string']
> ]
>
> def f(target, ListOfLists):
> for N in ListOfLists:
> try:
> i = N.index(target)
> except ValueError:
> continue
>
> # item before target
> if i: yield N[i - 1]
>
> # item after target
> try:
> yield N[i + 1]
> except IndexError:
> pass
>
> print set(n for n in f('string', data))
>
> # Prints set(['this', 'not', 'is', 'sucks', 'a'])
>
>
> Now, this works and normally I'd be happy with this and not try to
> improve it unless I found that it was a bottleneck.  However, in this
> case I know that when a try..except statement fails (i.e. executes the
> except part of the statement) it's "slow", *and* I know that the real
> list of lists will have many lists in it in which the target item does
> not appear.
>
> That means that the first try..except statement will be executing it's
> except clause more than half the time, and probably *much* more than
> half the time.


Nevermind,  it turns out that each list in the list of lists will
*always* contain the target item,  making this task much less
interesting.

~Simon

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


Re: Easier way to save result of a function?

2006-07-04 Thread James Mitchelhill
On Tue, 4 Jul 2006 16:53:27 -0700, Alex Martelli wrote:

> James Mitchelhill <[EMAIL PROTECTED]> wrote:

>> I'm trying to write a class that analyses some data. I only want it to
>> do as much work as necessary, so it saves method results to a
>> dictionary


> For your needs, I would suggest first defining your class "normally"
> (writing the methods without memoization) and then looping on all
> methods of interest decorating them with automatic memoization; I just
> suggested a similar kind of looping for decoration purposes in my very
> latest post on another thread;-).  You won't use the _syntax_ of
> decorators, but, hey, who cares?-)
> 
> Assuming (for simplicity but with no real loss of conceptual generality)
> that all of your heavy-processing methods have names starting with 'do',
> and that each of them takes only 'self' as its argument, you could for
> example do something like:

Thanks, that's awesome! Definitely not something I'd have ever been able
to work out myself - I think I need to learn more about nested functions
and introspection.

> def memoizeMethod(cls, n, m):
>   def decorated(self):
> if n in self._memo: return self._memo[n]
> result = self._memo[n] = m(self)
> return result
>   decorated.__name__ = n
>   setattr(cls, n, decorated)  
> 
> def memoizeClass(cls):
>   cls._memo = {}
>   for n,m in inspect.getmembers(cls, inspect.ismethod):
> if not n.startswith('do'): continue
> memoizeMethod(cls, n, m)

(I've changed inspect.ismethoddescriptors to inspect.ismethod and
unindented the last line.)
 
> Now just write your MyClass with the doThis doThat methods you want, and
> right after the class statement add
> 
> memoizeClass(MyClass)
> 
> 
> Voila, that's it (untested details, but the general idea is sound;-).

Thanks again.

-- 
James Mitchelhill
[EMAIL PROTECTED]
http://disorderfeed.net
-- 
http://mail.python.org/mailman/listinfo/python-list


CSpace - call for developers!

2006-07-04 Thread K.S.Sreeram
Hi All

CSpace aims to be the next generation in realtime communication. It is
an opensource python application, which provides a platform for secure,
decentralized, user-to-user communication.

The platform provides a connect(user,service) primitive, similar to the
sockets API connect(ip,port). Applications built on top of this platform
can simply invoke connect(user,service) to establish a secure,
nat/firewall friendly connection. Currently TextChat, FileTransfer and
RemoteDesktop applications are available on CSpace.

CSpace is still a work in progress, and I would like to invite everybody
who's interested, to help in improving this product!

CSpace Website: http://cspace.in

Regards
Sreeram

PS: My CSpace KeyID is 20. Feel free to add me to your contact list and
message me!



signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: converting file formats to txt

2006-07-04 Thread BartlebyScrivener
I suspect you will have to process those formats separately. But the
good news, at least for doc files, is that there is a script in the
Python Cookbook 2Ed that does what you want for MS Word docs and
another script that does it for Open Office docs.

The scripts are 2.26 and 2.27 pages 101-102.

I think you can probably find them at the ActiveState repository also.

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

In the book, the title of the script is "Extracting Text from Microsoft
Word Documents"

It uses PyWin32 extension and COM to perform the conversion.

rd

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


Re: Easier way to save result of a function?

2006-07-04 Thread Alex Martelli
James Mitchelhill <[EMAIL PROTECTED]> wrote:

> Sorry for the clunky subject line - I have a feeling that not knowing
> the proper terms for this is part of my problem.
> 
> I'm trying to write a class that analyses some data. I only want it to
> do as much work as necessary, so it saves method results to a
> dictionary, like so:
> 
> 
> class MyClass:
> def __init__(self):
> self.data = {}
> 
> def doSomething(self):
> return self.data.setdefault("someresult", self._doSomething())
> 
> def _doSomething(self):
>  # heavy processing here
>  result = 1
>  return result
> 
> def doMore(self):
> return self.data.setdefault("moreresult", self._doMore())
> 
> def _doMore(self):
> # more heavy processing here
> return self.doSomething() + 1
> 
> 
> This also seems kind of clunky. Is there a better way to acheive this?
> Or is this the usual idiom for this kind of thing? I've looked at
> decorators as they seem like they could be used for this, but I've no
> idea how exactly, or even if they're the appropriate tool.

No, this code is calling the heavy-processing functions
*unconditionally* -- Python's semantics is to compute every argument of
a function or method before starting to execute the code of that
function or method (almost all languages behave that way, save a very
few "pure" functional languages!), and that applies to the setdefault
methods of dictionaries too.

What you want to do is called "memoization" (sometimes "caching", but
that's a very generic term used in a bazillion different situations and
you may get overwhelmed by search results if you try searching for
it!-), and you can find plenty of recipes for it in the Python Cookbook
and elsewhere on the net.

For your needs, I would suggest first defining your class "normally"
(writing the methods without memoization) and then looping on all
methods of interest decorating them with automatic memoization; I just
suggested a similar kind of looping for decoration purposes in my very
latest post on another thread;-).  You won't use the _syntax_ of
decorators, but, hey, who cares?-)

Assuming (for simplicity but with no real loss of conceptual generality)
that all of your heavy-processing methods have names starting with 'do',
and that each of them takes only 'self' as its argument, you could for
example do something like:

def memoizeMethod(cls, n, m):
  def decorated(self):
if n in self._memo: return self._memo[n]
result = self._memo[n] = m(self)
return result
  decorated.__name__ = n
  setattr(cls, n, decorated)  

def memoizeClass(cls):
  cls._memo = {}
  for n,m in inspect.getmembers(cls, inspect.ismethoddescriptors):
if not n.startswith('do'): continue
  memoizeMethod(cls, n, m)

Now just write your MyClass with the doThis doThat methods you want, and
right after the class statement add

memoizeClass(MyClass)


Voila, that's it (untested details, but the general idea is sound;-).


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


Re: fonction in python

2006-07-04 Thread John Machin
On 4/07/2006 8:06 PM, aliassaf wrote:
> Hello, 
>   
> If we write = x^2  and if I give to the program the values of x, it will
> going to calculate the values of y, and also for x.   
> 
> But it is possible ? that is if I give to the program the values of X and Y,
> it will indicate to me the relation between the two variables, in the other
> hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me
> that f (t)!!! 

Please pardon me for introducing Python-related subject matter into a 
thread devoted to curve-fitting :-)

Consider the following:

|>> [x ^ 2 for x in range(10)]
[2, 3, 0, 1, 6, 7, 4, 5, 10, 11]

Not what you wanted? Try this:

|>> [x ** 2 for x in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Cheers,
John

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


Re: Easier way to save result of a function?

2006-07-04 Thread Mike Kent
James Mitchelhill wrote:
> Sorry for the clunky subject line - I have a feeling that not knowing
> the proper terms for this is part of my problem.
>
> I'm trying to write a class that analyses some data. I only want it to
> do as much work as necessary, so it saves method results to a
> dictionary, like so:

The term for this is 'memoization'.  You can find several recipes for
this in the online Python Cookbook; for example, see
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/466320

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


Re: Code design for a sub-class of built-ins

2006-07-04 Thread Alex Martelli
Steven D'Aprano <[EMAIL PROTECTED]> wrote:
   ...
> The obvious problem is, I have to create a custom method for every string
> method -- and if strings gain any new methods in some future version of
> Python, my subclass won't exhibit the correct behaviour.

As others already suggested, automating such decoration is pretty easy;
you can do it with either a custom metaclass or a simple post-processing
of your class in a loop.  Untested details below, but the general idea
would be something like:

class SDAstr(str):
magic = 'whatever'

def _SDAdecorate(methodname, strmethod):
def decorated(self, *a, **k):
if self == self.magic: print "whatever!"
return strmethod(self, *a, **k)
setattr(SDAstr, methodname, decorated)

for methodname, strmethod in \
inspect.getmembers(str, inspect.ismethoddescriptor):
_SDAdecorate(methodname, strmethod)

and variants thereof (to provide more accurate metainformation with the
decorated methods -- name, docstring, signature, whatever; and/or to
encapsulate things in a custom metaclass; whatever).


> class MyInt(int):
> """Like an int but with special behaviour."""
> def __init__(self, value, extra):
   ...
> Looks like my __init__ isn't even being called here. Why not, and how do I
> fix this?

Override __new__, which is what invariably gets called (before __init__,
which only gets called if __new__ returns an instance of the class that
you're instantiating).


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


Easier way to save result of a function?

2006-07-04 Thread James Mitchelhill
Sorry for the clunky subject line - I have a feeling that not knowing
the proper terms for this is part of my problem.

I'm trying to write a class that analyses some data. I only want it to
do as much work as necessary, so it saves method results to a
dictionary, like so:


class MyClass:
def __init__(self):
self.data = {}

def doSomething(self):
return self.data.setdefault("someresult", self._doSomething())

def _doSomething(self):
 # heavy processing here
 result = 1
 return result

def doMore(self):
return self.data.setdefault("moreresult", self._doMore())

def _doMore(self):
# more heavy processing here
return self.doSomething() + 1


This also seems kind of clunky. Is there a better way to acheive this?
Or is this the usual idiom for this kind of thing? I've looked at
decorators as they seem like they could be used for this, but I've no
idea how exactly, or even if they're the appropriate tool.

Thanks.

-- 
James Mitchelhill
[EMAIL PROTECTED]
http://disorderfeed.net
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: For a fast implementation of Python

2006-07-04 Thread Luis M. González
Pypy aims to become (probably sometime next year) a strong candidate to
replace cpython as the main python implementation.
Its goals are very ambicious and one of them is making it fast.
Some of its developers even think it can get as fast as C, although I
think that if it can get as fast as java, it would be great.

Its other goal is making it flexible and easier to develop and mantain.

Note that the project already produced a tool for writing extensions in
rpython that are, in theory, as fast a C extensions. So we can say that
in some way, the speed goal is already achieved.

Luis

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


Re: fonction in python

2006-07-04 Thread Duncan Smith
Steven D'Aprano wrote:
> On Tue, 04 Jul 2006 03:06:37 -0700, aliassaf wrote:
> 
> 
>>Hello, 
>>  
>>If we write = x^2  and if I give to the program the values of x, it will
>>going to calculate the values of y, and also for x.   
>>
>>But it is possible ? that is if I give to the program the values of X and Y,
>>it will indicate to me the relation between the two variables, in the other
>>hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me
>>that f (t)!!! 
> 
> 
> You are asking for curve-fitting. There is a HUGE amount of work on
> curve-fitting in computer science and statistics.
> 
> Generally, you start with some data points (x, y). You generally have some
> idea of what sort of function you expect -- is it a straight line? A
> curve? What sort of curve? A polynomial, an exponential, a sine curve, a
> cubic spline, a Bezier curve?
> 
> You might like to google on "least squares curve fitting" and "linear
> regression". That's just two methods out of many.
> 
> Some curve-fitting methods also estimate the error between the predicted
> curve and the data points; you could then try all of the methods and pick
> the one with the least error.
> 

The problem being that complex enough models will fit the data
arbitrarily closely (i.e. over-fit).  The OP should take into account
any prior expectations over the type of function (as you indicate) and
apply Occam's razor (find a relatively simple model that gives a
reasonable fit to the data).

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


Re: Can anyone please analyse this program for me (write a pseudocode for it).

2006-07-04 Thread John Machin
On 5/07/2006 1:09 AM, Steven D'Aprano wrote:
> On Mon, 03 Jul 2006 06:20:36 -0700, Vusi wrote:
> 
>> /* $Id: dotquad.c 3529 2005-10-01 10:15:22Z dyoung $ */
>> /*
>>  * Copyright (c) 2003, 2004 David Young.  All rights reserved.
>>  *
>>  * This code was written by David Young.
> 
> [snip code]
> 
> Am I the only one who found it hilarious that this piece of code was made
> up of 24 lines of actual code (including lines containing only a single
> brace) but 29 lines of legalise?
> 
> 

You're not alone. The program itself is as risible as the request for it 
to be analysed ... its ratio of overhead to functionality is exceeded 
only by the legendary IEFBR14.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: missing feature classes and missing fields

2006-07-04 Thread Bruno Desthuilliers
Roel Schroeven a écrit :
(snip)
> AFAIK, shape files are files with geospatial information used by ESRI 
(snip)
> But I still can't answer the question. I don't even know if there's a 
> way for python to read shape files.

There's of course a way to read files in Python - look at the 
open( [,]) builtin function.

Now if you meant parsing, as long as there's an accurate definition of 
the file format, then it should be possible to write a Python parser for 
it.

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

Re: List Manipulation

2006-07-04 Thread Roman
below is the data I am trying to read

"04""AS0042123BO"   "AS 0042.123 ROYAL ELONG SEAT
BO" "001610""A/S Fixtures"  0   $99.00  3.70""  
"0042123"   11/20/2003
"24""AS0042001BK"   "AS 0042.001 ROYAL EL*DISC BY
MFG*BK" "001610""A/S
Fixtures"   0   $99.00  8.00"723085611663"  "0042001"   
11/20/2003
"000104""CH130TTWH" "CH 130TT   EL PLAS SEAT C/F W/C
WH" "207067""Church Seats"  12  $25.00  6.75"073088079961"  
"130TT
000"12/28/1995
"000112""CH130TTBO" "CH 130TT   EL PLAS SEAT C/F W/C
BO" "207067""Church Seats"  23  $29.00  7.50"073088079954"  
"130TT
006"02/23/1998
"000124""CH130TTSS" "CH 130TT   EL PLAS SEAT C/F W/C
SS" "207067""Church Seats"  14  $29.00  6.75"073088079985"  
"130TT 162"
"000176""XPT562""PP T562"   "201681""Price
Pfister"0   $233.50 0.00""  ""  01/22/1998
"000180""XPT564""PP T564"   "201681""Price
Pfister"0   $0.00   0.00""  ""  07/19/1996
"000224""MO5270""MO 5270*DBM*MON BIDET FCT L/HDL
CP" "204938""Moen"  0   $0.00   8.00"026508050286"  "005270"
"000236""MO5270P"   "MO 5270P*DBM*BIDET FCT LVR/HDL
PB" "204938""Moen"  0   $0.00   8.00"026508050309"  "05270P"
"000240""MO5275""MO 5275 *DBM* BIDET FCT L/HDLS
CP" "204938""Moen"  1   $0.00   8.00""  ""  
11/20/2003
"000244""MO5275P"   "MO 5275P*DBM* MON BIDET FCT
PB" "204938""Moen"  0   $0.00   8.00"026508050347"  
"05275P"01/04/1996
"000248""MO5201""MO 5201 *DBM* TRAD BIDET LVR FCT
CP" "204938""Moen"  0   $0.00   6.70"026508050354"  "5201"  
01/04/1996
"000260""MO5201P"   "MO 5201P TRAD BIDET FCT
LVR/H*DBM*B""204938""Moen"  0   $0.00   7.00"026508050378"  
"5201P" 01/04/1996
"000264""MO5201W"   "MO 5201W**DBM**IDET FCT LVR/HDL
WH" "204938""Moen"  0   $0.00   6.70"026508050385"  
"05201W"01/04/1996
"066916""FB1418AB"  "FB D2418AB  18 TOWEL BAR
AB" "220502""Liberty
Hardware"   0   $18.70  1.15"079171141898"  "D2418AB"   
04/14/1998
"066920""FBD2424AB" "FB D2424AB  24 TOWEL BAR
AB" "220502""Liberty
Hardware"   39  $20.50  1.32"079171242427"  "D2424AB"
"066956""P7341FLC"  "PP 734-1FLC*DBM* SNK FCT 1H L/SP
CP" "201681""Price
Pfister"0   $147.65 7.00"038877420218"  "7341FLC"   
04/14/1998
"066960""P7341FLW"  "PP 734-1FLW FILT SNK FCT 1H L/SP
WH" "201681""Price
Pfister"0   $157.99 7.00"038877420225"  "7341FLW"   
04/14/1998

Dennis Lee Bieber wrote:
> On 4 Jul 2006 07:01:55 -0700, "Roman" <[EMAIL PROTECTED]> declaimed
> the following in comp.lang.python:
>
> > I would appreciate it if somebody could tell me where I went wrong in
> > the following snipet:
> >
>   It would help if you gave a sample of the input data (three lines
> worth, say) AND an example of what the final output should be from those
> three lines.
>
> > for col in line:
> >p[:0].append(str(col))
>
>   As has been pointed out, as soon as you used the [:0], you created a
> local/temporary EMPTY slice of the original P, and you are appending one
> column's value to this temporary, which is then thrown away.
> import csv
>
> -=-=-=-=-=-=-=-   PROGRAM
> p = []
>
> fin = open("firearms.csv", "r")
> reader = csv.reader(fin, dialect="excel", quotechar='"', delimiter=",")
>
> for line in [reader.next() for i in range(7)]:
> p.append(line)
>
> fin.close()
> print p
> -=-=-=-=-=-=-=-   OUTPUT
> [['Category', 'Model', 'Caliber', 'Serial #', 'Description',
> 'Accessories'], ['Air', 'Daisy 717 Pistol', '.177 pellet', '', '', ''],
> ['Air', 'Daisy/NRA PowerLine 953 Rifle', '.177 pellet', 'n/a',
> 'Micrometer Peep, "Globe" front (missing alternate inserts', 'Shooting
> sling'], ['Air', 'RWS Diana Model 54 "Air King" Rifle', '.22 pellet',
> '4022395', 'Hunting grade - >900fps', '2-7x BSA AOL scope'], ['Air',
> 'Gamo/NRA', '0.177', '', 'Hunting grade - ~1000fps; NRA markings on
> barrel, stock', '4x (BSA?) AOL scope, NRA badge'], ['Air',
> 'Walther/Crossman CP99 Pistol', '.177 pellet', '', 'CO2, repeater
> (currently magazine jams trigger/safety)', ''], ['Percussion', '? New
> Orleans Ace boot-pistol', '.36 lead', '', '', '']]
> -=-=-=-=-=-=-=-   INPUT (just first seven lines)
> Category,Model,Caliber,Serial #,Description,Accessories
> Air,Daisy 717 Pistol,.177 pellet,,,
> Air,Daisy/NRA PowerLine 953 Rifle,.177 pellet,n/a,"Micrometer Peep,
> ""Globe"" front (missing alternate inserts",Shooting sling
> Air,"RWS Diana Model 54 ""Air King"" Rifle",.

Re: [Mailman-Developers] Parsing and Rendering rfc8222

2006-07-04 Thread Brad Knowles
Ethan said:

> In the interest of not reinventing the wheel, I'm looking for existing
> python (or other!) code that does the things I need. I'm also putting
> out a call for anybody who likes this sort of thing to help me out (see
> below).

Don't ignore non-Python solutions.  In particular, you should take a close
look at what Richard Barrett is doing with his ht:dig patch for the
current versions of Mailman.  At the very least, this is what most Mailman
administrators are likely to expect, and you should have some idea of what
they're going to be looking for before you throw something totally new and
unfamiliar at them.

> * mbox thread indexing on messages
>
> I plan on using [2] to generate mbox thread indexes for rapid navigation
> of lists. Any suggestions for more robust variants would be welcome;
> feedback on how to handle threading for message-id-less messages would
> also be welcome.

All messages should have message-ids -- this is one of the most basic
requirements of the Internet e-mail related RFCs.  If nothing else, the
local MTA on the Mailman server should have provided a message-id.

That said, if you still don't find one, then I think you should select a
suitable method for generating them (take a look at the code in postfix or
sendmail), and then apply that.

> * full-text indexing
>
> pylucene seems to be the obvious choice; anything else I should
> consider? Anyone know of good pylucene/web UI glue code out there?

Again, I urge you to look at the existing ht:dig stuff.  For that matter,
you should probably also look at mail-archive.com and other similar sites
to see what they've got and make sure that you either fully implement all
the same features (in a way that makes sense), or that you know which
features you're not going to implement and why.

It all comes down to knowing what the expectations are for people who are
going to be administering and using Mailman, and then being able to manage
those expectations with regards to what you're doing.

-- 
Brad Knowles, <[EMAIL PROTECTED]>

"Those who would give up essential Liberty, to purchase a little
temporary Safety, deserve neither Liberty nor Safety."

 -- Benjamin Franklin (1706-1790), reply of the Pennsylvania
 Assembly to the Governor, November 11, 1755

  LOPSA member since December 2005.  See .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to specify default directory for saving files in IDLE

2006-07-04 Thread BartlebyScrivener

JohnJohn wrote>

> Is there a way that I can have python always bring up the Save As
> dialog pointing to the directory of my choice?  In other words, I
> would like to specify what I want the default directory for saving
> files to be.
>

I use PythonWin, not IDLE. But I suspect you could use the same trick.
Create a shortcut on your desktop for starting IDLE. (find idle.py or
idle.bat, right click, send shortcut to desktop).

Now go to the desktop, right click on the idle shortcut, and fill in
the slot that says "Start In" with the directory where you save your
Python scripts. 

Hope that helps.

rd

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


request for advice - possible ElementTree nexus

2006-07-04 Thread mirandacascade
Situation is this:
1) I have inherited some python code that accepts a string object, the
contents of which is an XML document, and produces a data structure
that represents some of the content of the XML document
2) The inherited code is somewhat 'brittle' in that some well-formed
XML documents are not correctly processed by the code; the brittleness
is caused by how the parser portion of the code handles whitespace.
3) I would like to change the code to make it less brittle.  Whatever
changes I make must continue to produce the same data structure that is
currently being produced.
4) Rather than attempt to fix the parser portion of the code, I would
prefer to use ElementTree.  ElementTree handles parsing XML documents
flawlessly, so the brittle portion of the code goes away.  In addition,
the ElementTree model is very sweet to work with, so it is a relatively
easy task using the information in ElementTree to produce the same data
structure that is currently being produced.
5) The existing data structure--the structure that must be
maintained--that gets produced does NOT include any {xmlns=}
information that may appear in the source XML document.
6) Based on a review of several posts in this group, I understand why
ElementTree hanldes xmlns= information the way it does.  This
is an oversimplification, but one of the things it does is to
incorporate the {whatever} within the tag property of the element and
of any descendent elements.
7) One of the pieces of information in the data structure that gets
produced by this code is the tag...the tag in the data structure should
not have any xmlns= information.

So, given that the goal is to produce the same data structure and given
that I really want to use ElementTree, I need to find a way to remove
the xmlns= information.  It seems like there are 2 general
methods for accomplishing this:
1) before feeding the string object to the ElementTree.XML() method,
remove the xmlns= information from the string.
2) keep the xmlns= information in the string that feeds
ElementTree.XML(), but when building the data structure, ensure that
the {whatever} information in the tag property of the element should
NOT be included in the data structure.

My requests for advice are:
a) What are the pros/cons of each of the 2 general methods described
above?
b) If I want to remove the xmlns information before feeding it to the
ElementTree.XML() method, and I don't want to be aware of what is to
the right of the equal sign, what is the best way to remove all the
substrings that are of the form xmlns=?  Would this require
learning the nuances of regular expressions?
c) If I want to leave the xmlns information in the string that gets fed
to ElementTree.XML, and I want to remove the {whatever} from the tag
before building the data structure, what is the best way to find
{whatever} from the tag property...is this another case where one
should be using regular expressions?

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


Re: python - regex handling

2006-07-04 Thread Ravi Teja

bruce wrote:
> hi...
>
> does python provide regex handling similar to perl. can't find anything in
> the docs i've seen to indicate it does...
>
> -bruce

It helps to learn to search before asking for help.
Typing the words - python regex (words from your post) into Google
gives you plenty of results with the first link being the HOWTO. A
better query - python "regular expressions" would give you even more.

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


Re: Parsing and Rendering rfc8222

2006-07-04 Thread Jean-Paul Calderone
On Tue, 04 Jul 2006 15:44:03 -0400, emf <[EMAIL PROTECTED]> wrote:
>Dearest mail manipulating macaques and perambulating python
>prestidigitators,
>
>I have been blessed by the grace of Google and so am working full-time
>on improving Mailman's web UI:
>
>http://wiki.list.org/display/DEV/Summer+of+Code
>

Just pointing out that there is no RFC 8222.  I assume you mean
RFC 2822.  I wouldn't mention it but this mistake is repeated
several times on the wiki page.

Jean-Paul
http://divmod.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: built in zip function speed

2006-07-04 Thread John J. Lee
[EMAIL PROTECTED] writes:
[...]
> (Instead of numarray you can use ScyPy, numerics, etc.)
> If your data in on disk you can avoid the list=>array conversion, and
> load the data from the numerical library itself, this is probably
> almost as fast as doing the same thing in C.

Apparently if you're starting to write numerical code with Python
these days you should use numpy, not Numeric or numarray.

(Note that in old postings you'll see 'numpy' used as a synonym for
what's now strictly called 'Numeric'.  First came Numeric, then the
offshoots/rewrites numarray and scipy-core, and now numpy has come
along to re-unify the two camps -- hooray!)


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


Re: Matplotlib eps export

2006-07-04 Thread Fernando Perez
Martin Manns wrote:

> Hi,
> 
> When I use matplotlib for a scatter plot with both dots and connecting
> lines, the exported eps file is huge, if the distances between many points
> are small. I think of this as a bug, since no preview tiff is included in
> the generated eps and a variety of text processing applications (including
> OpenOffice) crash when I try to import the eps. Ghostscript takes forever,
> too. Is there anything that I can do in order to export reasonable eps
> files?

I don't know the answer to your question, but I know the probability of
getting an answer will be a LOT higher if you post on the matplotlib list. 
c.l.py is only casually monitored by the mpl devs.

Cheers,

f

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


Re: Jythonc Problem

2006-07-04 Thread Khalid Zuberi
Ian Vincent wrote:
> I cannot find a Jython newsgroup, so apologies in advance if this 
> question has been posted to the wrong group.
> 

Try the jython-users mailing list:

   http://lists.sourceforge.net/mailman/listinfo/jython-users

- kz


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


Parsing and Rendering rfc8222

2006-07-04 Thread emf
Dearest mail manipulating macaques and perambulating python 
prestidigitators,

I have been blessed by the grace of Google and so am working full-time 
on improving Mailman's web UI:

http://wiki.list.org/display/DEV/Summer+of+Code

In order to provide interfaces to archives, I believe I must perform 
some intermediary manipulation; my goal is to get the information 
contained within the .mbox files mailman generates into ElementTrees and 
other objects so as to represent them via HTML/RSS/Atom/etc.

This is slightly out of scope for the web-UI project, as it is mostly 
about data model rather than actual user interface.

In the interest of not reinventing the wheel, I'm looking for existing 
python (or other!) code that does the things I need. I'm also putting 
out a call for anybody who likes this sort of thing to help me out (see 
below).

Here's where I'm at, grouped functionally:

* Need to convert rfc8222 to xml/html

I haven't found anything substantial via searching. My next step is to 
go spelunking in MailManager code and other python-webmail packages. If 
anyone knows good trees in this forest, please clue me in.

* Want to provide feeds (rss/atom/YourMommasSyntaxFormat)

Right this second I'm planning on using pyfeed [1]; is there anything 
else I should consider?

[1] http://home.blarg.net/~steveha/pyfeed.html

* mbox thread indexing on messages

I plan on using [2] to generate mbox thread indexes for rapid navigation 
of lists. Any suggestions for more robust variants would be welcome; 
feedback on how to handle threading for message-id-less messages would 
also be welcome.

[2] http://benno.id.au/code/archiver/jwzthreading.py

* full-text indexing

pylucene seems to be the obvious choice; anything else I should 
consider? Anyone know of good pylucene/web UI glue code out there?

(eg. something that leverages knowledge of the index to provide 
suggested keywords/tag cloud and/or tab-completion)

As to help, I am first and foremost interested in someone willing to 
write renderers as above and check it in to my branch.

I am also, however, interested in any advice, suggestions, 
dear-gods-whatever-you-do-don't-do-x comments, critiques of what is 
already checked in (zero python code, much html so far), pats on the 
back or other goodies (snailmail address available on request ;)

I do ask that you send anything not related to my above question 
directly to me, bypassing the list; I'd also humbly ask you to read the 
wiki page before telling me I should do XYZ; I may already be doing it.

I'm not actually subbed to python-list because I lack the stamina, so 
please cc: me on any follow ups.

Thank you for your generous assistance,

~ethan fremen

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


Re: List Manipulation

2006-07-04 Thread Bruno Desthuilliers
Roman a écrit :


Roman, please stop top-posting and learn to quote.


> Bruno Desthuilliers wrote:
> 
>>Roman wrote:
>>(please dont top-post - corrected)
>>
>>>
>>>My intention is to create matrix based on parsed csv file. So, I
>>>would like to have a list of columns (which are also lists).
>>
>>csv = [
>>   ['L0C0', 'L0C1', 'L0C2'],
>>   ['L1C0', 'L1C1', 'L1C2'],
>>   ['L2C0', 'L2C1', 'L2C2'],
>> ]
>>
>>matrix = [[[] for l in range(len(csv))] for c in range(len(csv[0]))]
>>
>>for numline, line in enumerate(csv):
>>for numcol, col in enumerate(line):
>>matrix[numcol][numline] = col
>>
>>assert matrix == [
>>   ['L0C0', 'L1C0', 'L2C0'],
>>   ['L0C1', 'L1C1', 'L2C1'],
>>   ['L0C2', 'L1C2', 'L2C2']
>> ]
>>


 > Thanks for spending so much time  with me.  I had since made the
 > following change.
 >
 > matrix = [[[] for l in range(len(list(reader)[:10]))] for c in
 > range(len(list(reader)[7]))]

Technically; this won't work. The first call to list(reader) will 
consume reader.

It's also somewhat dumb (len(list(reader)[:10]) is always 10) and 
inefficient (you're calling list(reader) twice, when you could call it 
just once).

Instead of trying anything at random, read the fine manual and try to 
understand the example I gave you.

 > for numline, line in enumerate(reader):
 >for numcol, col in enumerate(line):
 >matrix[numcol][numline] = col
 >
 > print matrix
 >
 > I don't get mistakes anymore.  However, still nothing gets printed

If you don't print anything, nothing will be printed.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Valgrind memory-checker reports memory problems in Python

2006-07-04 Thread Tim Peters
[Nathan Bates]
> Are the Python developers running Python under Valgrind?

Please read Misc/README.valgrind (in your Python distribution).
-- 
http://mail.python.org/mailman/listinfo/python-list


Valgrind memory-checker reports memory problems in Python

2006-07-04 Thread Nathan Bates
Are the Python developers running Python under Valgrind?
If not, FYI, Valgrind is a excellent memory-checker for Linux.
Valgrind is reporting a ton of memory problems.
Worrisome are "Conditional jump or move depends on uninitialised
value(s)" errors.
I simply started the Python 2.4.2 interpreter,
then Ctrl+D, on Linux Fedora 5.


==2923== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et
al.
==2923== Using LibVEX rev 1471, a library for dynamic binary
translation.
==2923== Copyright (C) 2004-2005, and GNU GPL'd, by OpenWorks LLP.
==2923== Using valgrind-3.1.0, a dynamic binary instrumentation
framework.
==2923== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et
al.
==2923== For more details, rerun with: -v
==2923==
==2923== My PID = 2923, parent PID = 2922.  Prog and args are:
==2923==/usr/bin/python
==2923==
==2923== Conditional jump or move depends on uninitialised value(s)
==2923==at 0x331BD6096B: PyObject_Free (in
/usr/lib64/libpython2.4.so.1.0)
==2923==by 0x331BD5BFE9: (within /usr/lib64/libpython2.4.so.1.0)
==2923==by 0x331BD6555E: PyString_InternInPlace (in
/usr/lib64/libpython2.4.so.1.0)
==2923==by 0x331BD69C6E: PyString_InternFromString (in
/usr/lib64/libpython2.4.so.1.0)
==2923==by 0x331BD7053C: (within /usr/lib64/libpython2.4.so.1.0)
==2923==by 0x331BD7549F: PyType_Ready (in
/usr/lib64/libpython2.4.so.1.0)
==2923==by 0x331BD76F6E: PyType_Ready (in
/usr/lib64/libpython2.4.so.1.0)
==2923==by 0x331BD5E42F: _Py_ReadyTypes (in
/usr/lib64/libpython2.4.so.1.0)
==2923==by 0x331BDB3E6A: Py_InitializeEx (in
/usr/lib64/libpython2.4.so.1.0)
==2923==by 0x331BDB910D: Py_Main (in
/usr/lib64/libpython2.4.so.1.0)
==2923==by 0x331181D083: __libc_start_main (in /lib64/libc-2.4.so)
==2923== 
[..]

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


Re: Amara: Where's my attribute?

2006-07-04 Thread AdSR
[EMAIL PROTECTED] wrote:
> What is the actual problem you're trying to solve?  If you just want to
> force a namespace declaration in output (this is sually to support
> QNames in content) the most well-known XML hack is to create a dummy
> attribute with the needed prefix and namespace.  But this does not work
> when you're trying to force a default namespace declaration.  Then
> again, you generally can't use QNames in content with a default
> namespace declaration.  So my guess is that you somehow got way off the
> rails in your problem-solving, and you'll need to provide mre
> background if you want help.

I wanted to remove documentation elements from some XML Schema files.
The problem showed when I tried to use the stripped schemas, because
the namespace declaration for user-defined types was missing. Of
course, since these types are named and referred to in attribute
*values*, Amara had no way to know that the namespace declaration was
still needed (didn't matter if default or non-default). This is more a
problem of how XML Schema is defined against XML namespace rules, since
XML Schena uses namespaces in a context of which XML parsers aren't
normally aware.

> BTW, I recommend upgrading to Amara 1.1.7.  That branch will soon be
> 1.2, and I consider it more mature than 1.0 at this point.  The API's
> also easier:

I know, especially the insert-before/after feature :) But I ran into a
problem that I describe below and you advertised 1.0 as "stable
version", so I switched immediately.

The problem can be reproduced like this:

>>> import amara
>>> amara.parse('http://www.w3.org/2001/XMLSchema.xsd')
START DTD xs:schema -//W3C//DTD XMLSCHEMA 200102//EN XMLSchema.dtd
http://www.w3.org/2001/datatypes.dtd:99:23: Attribute 'id' already
declared
http://www.w3.org/2001/datatypes.dtd:122:23: Attribute 'id' already
declared
http://www.w3.org/2001/datatypes.dtd:130:27: Attribute 'id' already
declared
...some 40 more lines like this and then Python crashes (Windows shows
the bug-reporting dialog)

Thanks for your interest,

AdSR

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


Re: Can anyone please analyse this program for me (write a pseudocode for it).

2006-07-04 Thread BJörn Lindqvist
> > /* $Id: dotquad.c 3529 2005-10-01 10:15:22Z dyoung $ */
> > /*
> >  * Copyright (c) 2003, 2004 David Young.  All rights reserved.
> >  *
> >  * This code was written by David Young.
>
> [snip code]
>
> Am I the only one who found it hilarious that this piece of code was made
> up of 24 lines of actual code (including lines containing only a single
> brace) but 29 lines of legalise?

Good thing it was C then, had it been Python the hilariousness of the
legalese/LOC ratio would have been lethal.

-- 
mvh Björn
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: handling unicode data

2006-07-04 Thread Martin v. Löwis
Filipe wrote:
> term = row[1]
> print repr(term)
> 
> output I got in Pyscripter's interpreter window:
> 'Fran\x87a'
> 
> output I got in the command line:
> 'Fran\xd8a'
> 
> I'd expect "print" to behave differently according with the console's
> encoding, but does this mean this happens with repr() too?

repr always generates ASCII bytes. They are not effected by the
console's encoding. If you get different output, it really means
that the values are different (check ord(row[1][4]) to be sure)

What is the precise sequence of statements that you used to
set the "row" variable?

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


DBus examples?

2006-07-04 Thread jerry . levan
I am trying to learn a bit about dbus on my Fedora Core 5 laptop.

All of the python examples I find on google fail to "compile" for
one reason or another, I guess the interface is still in flux...

Does anyone know of a source for a few simple examples that can
get me started?

Does anyone know if a suitably privileged user can send a
message via the dbus that will suspend/hibernate the laptop?

Thanks

Jerry

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


Re: converting file formats to txt

2006-07-04 Thread Juho Schultz
Steven D'Aprano wrote:
> On Tue, 04 Jul 2006 06:32:13 -0700, Gaurav Agarwal wrote:
>
> > Hi,
> >
> > I wanted a script that can convert any file format (RTF/DOC/HTML/PDF/PS
> > etc) to text format.
>
> PDF is (I believe) a compressed, binary format of PS. Perhaps you should
> look at the program pdf2ps -- maybe it will help.
> 
> 
> -- 
> Steven.

Or try the program pdftotext?


-- Juho Schultz

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


Re: List Manipulation

2006-07-04 Thread Roman
Thanks for spending so much time  with me.  I had since made the
following change.

matrix = [[[] for l in range(len(list(reader)[:10]))] for c in
range(len(list(reader)[7]))]
for numline, line in enumerate(reader):
   for numcol, col in enumerate(line):
   matrix[numcol][numline] = col

print matrix

I don't get mistakes anymore.  However, still nothing gets printed
Bruno Desthuilliers wrote:
> Roman wrote:
> (please dont top-post - corrected)
> >
> > Iain King wrote:
> >
> >>Roman wrote:
> >>
> >>>I would appreciate it if somebody could tell me where I went wrong in
> >>>the following snipet:
> >>>
> (snip)
>
> >>What are you trying to do here?  p[:0]  returns a new list, of all the
> >>elements in p up to element 0 (which is of course the empty list),
> >>which is then appended to, but is not stored anywhere.  If you want to
> >>insert str(col) then use p.insert
> >>
> >>
> >>Iain
> >
> >
> > Thanks for your help
> >
> > My intention is to create matrix based on parsed csv file. So, I
> > would like to have a list of columns (which are also lists).
>
> csv = [
>['L0C0', 'L0C1', 'L0C2'],
>['L1C0', 'L1C1', 'L1C2'],
>['L2C0', 'L2C1', 'L2C2'],
>  ]
>
> matrix = [[[] for l in range(len(csv))] for c in range(len(csv[0]))]
>
> for numline, line in enumerate(csv):
> for numcol, col in enumerate(line):
> matrix[numcol][numline] = col
>
> assert matrix == [
>['L0C0', 'L1C0', 'L2C0'],
>['L0C1', 'L1C1', 'L2C1'],
>['L0C2', 'L1C2', 'L2C2']
>  ]
>
> NB : There are probably more elegant solutions.
>
> > I have made the following changes and it still doesn't work.
>
> "doesn't work" is the worst possible description of a problem...
>
> (snip)
>
> --
> bruno desthuilliers
> python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
> p in '[EMAIL PROTECTED]'.split('@')])"

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


Re: Code design for a sub-class of built-ins

2006-07-04 Thread Bruno Desthuilliers
Steven D'Aprano wrote:
> I'm having problems with sub-classes of built-in types.
> 
> Here is a contrived example of my subclass. It isn't supposed
> to be practical, useful code, but it illustrates my problem.
> 
> class MyStr(str):
> """Just like ordinary strings, except it exhibits special behaviour
> for one particular value.
> """
> magic = "surprise"

> def __init__(self, value):
> str.__init__(self, value)

You don't need to override __init__ here.


> def __len__(self):
> if self == self.magic:
> print "Nobody expects the Spanish Inquisition!"
> return str.__len__(self)
> def upper(self):
> if self == self.magic:
> print "Nobody expects the Spanish Inquisition!"
> return str.upper(self)
> # and so on for every last string method...

My my my

> 
> The obvious problem is, I have to create a custom method for every string
> method -- 

which makes subclassing almost useless - except to pass isinstance() tests.

> and if strings gain any new methods in some future version of
> Python, my subclass won't exhibit the correct behaviour.

You could replace subclassing by composition/delegation using the
__getattr__ hook, but this would break tests on type/class :(

Or you could keep subclassing and use the __getattribute__ hook, but
this is more tricky and IIRC may have negative impact on lookup perfs.

Or you could use a metaclass to decorate (appropriate) str methods with
a decorator doing the additional stuff.

choose your poison !-)

> Here's a slightly different example:
> 
> class MyInt(int):
> """Like an int but with special behaviour."""
> def __init__(self, value, extra):
> int.__init__(self, value=None)
> self.extra = extra

Won't work. You need to override the __new__ staticmethod. Look for
appropriate doc here:
http://www.python.org/download/releases/2.2.3/descrintro/#__new__
(FWIW, read the whole page)

(snip)

> Looks like my __init__ isn't even being called here. Why not, and how do I
> fix this?

cf above

> Is there a way to subclass built-in types without needing to write the
> same bit of code for each and every method? 

cf above

> Am I approaching this the wrong way? Is there a better design I could be
> using?

probably !-)

HTH
-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Replace Whole Object Through Object Method

2006-07-04 Thread digitalorganics
Point well taken, and indeed a brilliant solution. Thank you I V for
demonstrating so clearly.

I V wrote:
> On Mon, 26 Jun 2006 19:40:52 -0700, digitalorganics wrote:
> > A misuse of inheritance eh? Inheritance, like other language features,
> > is merely a tool. I happen to be using this tool to have my virtual
> > persons change roles at different points in their lifetime, as many
> > real people tend to do. Thus, at these points, B is indeed an A. What a
> > person is, whether in real life or in my program, is not static and
> > comes into definition uniquely for each moment (micro-moment, etc.) of
> > existence. Now, please, I have no intention of carrying the
> > conversation in such a silly direction, I wasn't inviting a discussion
> > on philosophy or some such. I seek to work the tools to my needs, not
> > the other way around.
>
> But thinking about the problem in the vocabulary provided by the
> programming language can be helpful in coming up with a solution. If
> inheritance tells you what an object _is_, and membership tells you what a
> role _has_, and a role is something that a person has, that suggests
> that an implementation where roles are members of a person might be
> simpler than trying to use inheritance. Like, for instance:
>
> class Role(object):
>   def __init__(self, person):
>   self.person = person
>
>
> class Worker(Role):
>   def do_work(self):
>   print self.name, "is working"
>
>
> class Employer(Role):
>
>   def __init__(self, person):
>   Role.__init__(self, person)
>   self.employees = []
>
>
>   def add_employee(self, worker):
>   self.employees.append(worker)
>
>
>   def boss_people_around(self):
>   for employee in employees:
>   print self.name, "is telling", employee.name, "what to 
> do"
>
>
> class Person(object):
>
>   def __init__(self, name):
>   self.roles = []
>   self.name = name
>
>
>   def add_role(self, role_class):
>   self.roles.append(role_class(self))
>
>
>   def forward_to_role(self, attr):
>   for role in self.roles:
>   try:
>   return getattr(role, attr)
>   except AttributeError:
>   pass
>   raise AttributeError(attr)
>
>
>   def __getattr__(self, attr):
>   self.forward_to_role(attr)
>
>
> bill = Person('Bill')
> bill.add_role(Worker)
>
> bob = Person('Bob')
> bob.add_role(Employer)
> 
> bill.work()
> bob.boss_people_around()

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


Re: Replace Whole Object Through Object Method

2006-07-04 Thread digitalorganics

Fredrik Lundh wrote:
> [EMAIL PROTECTED] wrote:
>
> > I'm working with a team that's doing social modeling, and for example,
> > I need to model workers that at some point in the program may or may
> > not also become employers. Now, I want the workers to take on all
> > behaviors and attributes of an employer in addition to their
> > pre-existing "worker" behaviors and attributes. Also, as I'm sure you
> > guessed, the workers' attributes need to retain their values at that
> > point in the program, so a brand new worker-employer object wouldn't in
> > itself do the trick.
>
> so why not just do that, instead of trying to come up with overly
> convoluted ways to abuse a class mechanism that you don't fully understand ?

Why not just do what? And the way you use the term abuse, almost as if
Python's class mechanism was someone's child...

>
> I suspect that your team would prefer if you got your inspiration from
> GoF instead of Goldberg.
> 
> 

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


Re: fonction in python

2006-07-04 Thread [EMAIL PROTECTED]

aliassaf wrote:
> Hello,
>
> If we write = x^2  and if I give to the program the values of x, it will
> going to calculate the values of y, and also for x.
>
> But it is possible ? that is if I give to the program the values of X and Y,
> it will indicate to me the relation between the two variables, in the other
> hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me
> that f (t)!!!

You can use the GMPY module to determine square & power relationships:

>>> import gmpy
>>> for n in range(20):
print n,
if gmpy.is_square(n):
print True,
else:
print False,
if gmpy.is_power(n):
print True
else:
print False


0 True True
1 True True
2 False False
3 False False
4 True True
5 False False
6 False False
7 False False
8 False True
9 True True
10 False False
11 False False
12 False False
13 False False
14 False False
15 False False
16 True True
17 False False
18 False False
19 False False

9 is both a power and a square whereas 8 is a power but not a square.


>
> Thanks
>
> --
> View this message in context: 
> http://www.nabble.com/fonction-in-python-tf1889102.html#a5164997
> Sent from the Python - python-list forum at Nabble.com.

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


Re: List Manipulation

2006-07-04 Thread Bruno Desthuilliers
Roman wrote:
(please dont top-post - corrected)
> 
> Steven D'Aprano wrote:
> 
>>On Tue, 04 Jul 2006 07:01:55 -0700, Roman wrote:
>>
>>
(snip)
>>
>>>cnt = 0
>>>p=[]
>>>reader = csv.reader(file("f:\webserver\inp.txt"), dialect="excel",
>>> quotechar="'", delimiter='\t')
>>>for line in reader:
>>>if cnt > 6:
>>>   break
>>
>>That's a very unPythonic way of doing the job. The usual way of doing
>>this would be something like this:
>>
>>for line in reader[:7]:
>># no need for the "if cnt > 6: break" clause now
>>
> I am getting
>
> TypeError: unsubscriptable object
>
> when specifying
>
> for line in reader[:7]:
>

Should have been :

for line in list(reader)[:7]:
  # code here


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])"
-- 
http://mail.python.org/mailman/listinfo/python-list


GNU date input formats in Python

2006-07-04 Thread s . lipnevich
Hi All,

I tried looking in several places, including Python's documentation,
CheeseShop, general search, and didn't find anything. Is implementation
of GNU date input formats
(http://www.gnu.org/software/tar/manual/html_node/tar_109.html)
available in Python? I'm primarily interested in parsing relative
dates/times like "next tuesday" or "-1 week."
I know that Python 2.5 now includes strptime
(http://docs.python.org/lib/module-time.html#l2h-1956), but that
function only parses prescribed formats and can't do "relative."
Thank you!

Sergey.

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


Re: Finding Return Code From GPG

2006-07-04 Thread Dennis Benzinger
Nomen Nescio wrote:
> I'm running gpg in python to verify a signature. I can see that it is
> working, because gpg is displaying the results.
> 
> But I need a way to let the python script know this. According to the gpg
> manual there is a return code from gpg of 0 if the verify is good and 1 if
> it is bad.
> 
> Can anyone tell me how I can get the python script to 'see' this return
> code?
> 

How do you run GPG? I suggest using the subprocess module 
. If you just need 
something simple then you can use its call function 
.


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


Re: python - regex handling

2006-07-04 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, bruce wrote:

> does python provide regex handling similar to perl. can't find anything in
> the docs i've seen to indicate it does...

The `re` module handles Perl compatible regexes.

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


Treasure Trooper

2006-07-04 Thread curtisdulmadge
 what you do here is you complete simple surveys and offers, some
require a credit card, but i just do all the free ones :D

its easy to do, you just register and do the offers, then click the
"done" button when you have completed it to what the instructions say,
here is the website if you want to check it out

www.treasuretrooper.com/111524 (copy and paste into browser

this is how the payment works: say in june you made 60$, then on the
15th of next month, so on july 15th you would get your money. they send
a check in the mail so yes, you have to provide your correct address,
not a fake one (dont worry they dont spam you with shit) and you will
get a check in the mail from them and you can cash it like any other
check.

so far, i have been doing this thing for 2 months and i have been payed
$156.50 total. its easy money and only takes about half an hour a day
if your really keen on it, but usually 10 minutes is fine. on their
forums i ha ve heard that people are making like $200+ i hope i can get
that much in the future. im pretty close.

once again, if you are interested in signing up, here is where you go:
www.treasuretrooper.com/111524  (copy and paste into your browser)

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


python - regex handling

2006-07-04 Thread bruce
hi...

does python provide regex handling similar to perl. can't find anything in
the docs i've seen to indicate it does...

-bruce

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


Re: List Manipulation

2006-07-04 Thread Roman
I am getting

TypeError: unsubscriptable object

when specifying

for line in reader[:7]:


Steven D'Aprano wrote:
> On Tue, 04 Jul 2006 07:01:55 -0700, Roman wrote:
>
> > I would appreciate it if somebody could tell me where I went wrong in
> > the following snipet:
> >
> > When I run I get no result
>
> What do you mean? Does it print None?
>
> > cnt = 0
> > p=[]
> > reader = csv.reader(file("f:\webserver\inp.txt"), dialect="excel",
> >  quotechar="'", delimiter='\t')
> > for line in reader:
> > if cnt > 6:
> >break
>
> That's a very unPythonic way of doing the job. The usual way of doing
> this would be something like this:
>
> for line in reader[:7]:
> # no need for the "if cnt > 6: break" clause now
>
>
> > for col in line:
> >p[:0].append(str(col))
>
> p[:0] creates a new list, which has a string appended to it, and is then
> thrown away. What are you trying to do?
>
> If you are trying to insert the new entry at the beginning of the list,
> you probably want this:
> 
> p.insert(0, str(col))

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


RE: ascii character - removing chars from string

2006-07-04 Thread bruce
thanks for your replies!!

the solution..

 dd = dd.replace(u'\xa0','')

this allows the nbsp hex representation to be replaced with a ''. i thought
i had tried this early in the process.. but i may have screwed up the
typing...

-bruce


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf
Of Steven D'Aprano
Sent: Tuesday, July 04, 2006 9:35 AM
To: python-list@python.org
Subject: RE: ascii character - removing chars from string


On Tue, 04 Jul 2006 09:01:15 -0700, bruce wrote:

> update...
>
> the error i'm getting...
>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in
> position 62: ordinal not in range(128)

Okay, now we're making progress -- we know what exception you're getting.
Now, how about telling us what you did to get that exception?


> is there a way i can tell/see what the exact char is at pos(62). i was
> assuming that it's the hex \xa0.

That's what it's saying.

> i've done the s.replace('\xa0','') with no luck.

What does that mean? What does it do? Crash? Raise an exception? Return a
string you weren't expecting? More detail please.

Here is some background you might find useful. My apologies if you already
know it:

"Ordinary" strings in Python are delimited with quote marks, either
matching " or '. At the risk of over-simplifying, these strings can
contain only single-byte characters, i.e. ordinal values 0 through 255, or
in hex, 0 through FF. The character you are having a problem with is
within that range of single bytes: ord(u'\xa0') = 160.

Notice that a string '\xa0' is a single byte; a Unicode string u'\xa0' is
a different type of object, even though it has the same value.

String methods will blindly operate on any string, regardless of what
bytes are in them. However, converting from unicode to ordinary strings is
NOT the same -- the *character* chr(160) is not a valid ASCII character,
since ASCII only uses the range chr(0) through chr(127).

If this is confusing to you, you're not alone.



--
Steven.

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

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


RE: ascii character - removing chars from string

2006-07-04 Thread Steven D'Aprano
On Tue, 04 Jul 2006 09:01:15 -0700, bruce wrote:

> update...
> 
> the error i'm getting...
> 
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in
> position 62: ordinal not in range(128)

Okay, now we're making progress -- we know what exception you're getting.
Now, how about telling us what you did to get that exception?


> is there a way i can tell/see what the exact char is at pos(62). i was
> assuming that it's the hex \xa0.

That's what it's saying.

> i've done the s.replace('\xa0','') with no luck.

What does that mean? What does it do? Crash? Raise an exception? Return a
string you weren't expecting? More detail please.

Here is some background you might find useful. My apologies if you already
know it:

"Ordinary" strings in Python are delimited with quote marks, either
matching " or '. At the risk of over-simplifying, these strings can
contain only single-byte characters, i.e. ordinal values 0 through 255, or
in hex, 0 through FF. The character you are having a problem with is
within that range of single bytes: ord(u'\xa0') = 160.

Notice that a string '\xa0' is a single byte; a Unicode string u'\xa0' is
a different type of object, even though it has the same value.

String methods will blindly operate on any string, regardless of what
bytes are in them. However, converting from unicode to ordinary strings is
NOT the same -- the *character* chr(160) is not a valid ASCII character,
since ASCII only uses the range chr(0) through chr(127).

If this is confusing to you, you're not alone.



-- 
Steven.

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


RE: ascii character - removing chars from string

2006-07-04 Thread bruce
yep!

dang phat fingers!!! thanks

everything's working as it should... 6 hours to track down this little
issue!!!

arrrgghhh..



-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf
Of Fredrik Lundh
Sent: Tuesday, July 04, 2006 9:09 AM
To: python-list@python.org
Subject: Re: ascii character - removing chars from string


bruce wrote:

> i've done the s.replace('\xa0','') with no luck.

let me guess: you wrote

 s.replace("\xa0", "")

instead of

 s = s.replace("\xa0", "")

?



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

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


Finding Return Code From GPG

2006-07-04 Thread Nomen Nescio
I'm running gpg in python to verify a signature. I can see that it is
working, because gpg is displaying the results.

But I need a way to let the python script know this. According to the gpg
manual there is a return code from gpg of 0 if the verify is good and 1 if
it is bad.

Can anyone tell me how I can get the python script to 'see' this return
code?

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


Re: Client Server Connection Program

2006-07-04 Thread diffuser78
removing int frrom host = int(sys.argv[1])  fixed the problem. Thanks
Fred!!!

Fredrik Lundh wrote:
> [EMAIL PROTECTED] wrote:
>
> > #/ usr/bin/env python
> > # filename: tmc.py (CLIENT)
> >
> > import socket
> > import sys
> >
> > s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> >
> > host = int(sys.argv[1])
> > port = int(sys.argv[2])
>
> the port number is an integer, but the host name/ip is obviously not an
> integer -- so why are you trying to convert it an integer ?
>
> > s.connect((host,port))
> > s.send(sys.argv[3])
> > i = 0
> > while True:
> > data = s.recv(100)
> > i+=1
> > if (i<5):
> > print data
> > if not data:
> > break
> > print 'received', len(data), 'bytes'
> > s.close()
> >
> >
> > Server is installed on machine 192.168.1.4 and Client is 192.168.1.2
> >
> >
> > When I start server and the server is listening I start the clinet.
> >
> > I started Client using 
> >
> > python tmc.py 192.168.1.4 2000 abcxyz
> >
> > where 2000 is the port number  and abcxyz is the string I pass to
> > server, I get the following error...
> >
> > ValueError: invalid literal for int(): 192.168.1.4
> >
> > How can I fix this ?
>
> by reading the *entire* trackback, and looking at the line it tells you
> to look at, before you post.
> 
> 

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


Re: fonction in python

2006-07-04 Thread Grant Edwards
On 2006-07-04, aliassaf <[EMAIL PROTECTED]> wrote:

> But it is possible ? that is if I give to the program the values of X and Y,
> it will indicate to me the relation between the two variables, in the other
> hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me
> that f (t)!!! 

Sort of.  There are a number of curve-fitting modules available
for Python as part of packages like ScyPy http://www.scipy.org/
and Scientific Python http://sourcesup.cru.fr/projects/scientific-py/

You generally have to provide the fitter with a function
"template" for which it can find the coefficients.  For
example, you tell the fitter that you want a polynomial of the
form  f(y) = Ax^2 + Bx + C, and the fitter will find the values
of A, B, C that best fit the data.

There are also commercial products that have lists of hundreds
of "templates" and will crunch through thme to find the ones
that provide the best fits.
 
-- 
Grant Edwards
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Tkinter, grid, in_: widget definition order significant ?!

2006-07-04 Thread Sorin Schwimmer
Hi,Working on my current project, I discovered the following behaviour: a widget defined before the window in which I try to grid it won't show up, but will take space and behave like it is there (blind typing proves it). A widget defined after the griding window will come up fine. The following example illustrates my point:from Tkinter import *r=Tk()l1=Label(r, text='first label')f=Frame(r)f.grid()l1.grid(in_=f, row=0)l2=Label(r, text='second label')l2.grid(in_=f, row=1)On my system (Gentoo Linux, Python 2.4.1) l1 (defined before f) takes spaces, but is invisible, while l2 (defined after f) is OK.That's the way it is intended to be? How would I grid stuff in dynamically created windows?Thanks for your advice,Sorin Schwimmer 
		How low will we go? Check out Yahoo! Messenger’s low  PC-to-Phone call rates.-- 
http://mail.python.org/mailman/listinfo/python-list

Re: built in zip function speed

2006-07-04 Thread Peter Otten
Peter Otten wrote:

> from numarray import array
> a = array(a)
> b = array(b)
> c = array(c)
> d = array(d)
> e = (c-d) - (a-b)*(a-b)

Oops, bearophile has already posted the same idea with better execution...
 

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


Re: ascii character - removing chars from string

2006-07-04 Thread Fredrik Lundh
bruce wrote:

> i've done the s.replace('\xa0','') with no luck.

let me guess: you wrote

 s.replace("\xa0", "")

instead of

 s = s.replace("\xa0", "")

?



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


Re: Client Server Connection Program

2006-07-04 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote:

> #/ usr/bin/env python
> # filename: tmc.py (CLIENT)
> 
> import socket
> import sys
> 
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> 
> host = int(sys.argv[1])
> port = int(sys.argv[2])

the port number is an integer, but the host name/ip is obviously not an 
integer -- so why are you trying to convert it an integer ?

> s.connect((host,port))
> s.send(sys.argv[3])
> i = 0
> while True:
> data = s.recv(100)
> i+=1
> if (i<5):
> print data
> if not data:
> break
> print 'received', len(data), 'bytes'
> s.close()
> 
> 
> Server is installed on machine 192.168.1.4 and Client is 192.168.1.2
> 
> 
> When I start server and the server is listening I start the clinet.
> 
> I started Client using 
> 
> python tmc.py 192.168.1.4 2000 abcxyz
> 
> where 2000 is the port number  and abcxyz is the string I pass to
> server, I get the following error...
> 
> ValueError: invalid literal for int(): 192.168.1.4
> 
> How can I fix this ?

by reading the *entire* trackback, and looking at the line it tells you 
to look at, before you post.



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


Re: built in zip function speed

2006-07-04 Thread Peter Otten
[EMAIL PROTECTED] wrote:

> I hope I am not being too ignorant :p but here goes... my boss has
> written a bit of python code and asked me to speed it up for him...
> I've reduced the run time from around 20 minutes to 13 (not bad I think
> ;) to speed it up further I asked him to replace a loop like this:-
> 
> 
> index = 0
> 
> for element in a:
>av = a[index]
>bv = b[index]
>cv = c[index]
>dv = d[index]
>avbv = (av-bv) * (av-bv)
>diff = cv - dv
>e.append(diff - avbv)
>index = index + 1

For /real/ speed-ups use a numerical library, e. g.

# untested
from numarray import array
a = array(a)
b = array(b)
c = array(c)
d = array(d)
e = (c-d) - (a-b)*(a-b)

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


Code design for a sub-class of built-ins

2006-07-04 Thread Steven D'Aprano
I'm having problems with sub-classes of built-in types.

Here is a contrived example of my subclass. It isn't supposed
to be practical, useful code, but it illustrates my problem.

class MyStr(str):
"""Just like ordinary strings, except it exhibits special behaviour
for one particular value.
"""
magic = "surprise"
def __init__(self, value):
str.__init__(self, value)
def __len__(self):
if self == self.magic:
print "Nobody expects the Spanish Inquisition!"
return str.__len__(self)
def upper(self):
if self == self.magic:
print "Nobody expects the Spanish Inquisition!"
return str.upper(self)
# and so on for every last string method...


The obvious problem is, I have to create a custom method for every string
method -- and if strings gain any new methods in some future version of
Python, my subclass won't exhibit the correct behaviour.

Here's a slightly different example:

class MyInt(int):
"""Like an int but with special behaviour."""
def __init__(self, value, extra):
int.__init__(self, value=None)
self.extra = extra
def __add__(self, other):
if self.extra is None:
return int.__add__(self, other)
else:
return int.__add__(self, other) + self.extra
# and again with all the other methods

This one doesn't even work!

>>> i = MyInt(5, 6)
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: int() can't convert non-string with explicit base

Looks like my __init__ isn't even being called here. Why not, and how do I
fix this?

Is there a way to subclass built-in types without needing to write the
same bit of code for each and every method? 

Am I approaching this the wrong way? Is there a better design I could be
using?

Thanks,



-- 
Steven.

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


RE: ascii character - removing chars from string

2006-07-04 Thread bruce
steven...

when you have the >>>u'hello ? world'<< in your interpreter/output, is the
'u' indicating that what you're displaying is unicode?

i pretty much tried what you have in the replace.. and i got the same error
regarding the unicodedecode error...




-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf
Of Steven D'Aprano
Sent: Tuesday, July 04, 2006 8:45 AM
To: python-list@python.org
Subject: RE: ascii character - removing chars from string


On Tue, 04 Jul 2006 08:09:53 -0700, bruce wrote:

> simon...
>
> the issue that i'm seeing is not a result of simply using the
> 'string.replace' function. it appears that there's something else going on
> in the text
>
> although i can see the nbsp in the file, the file is manipulated by a
number
> of other functions prior to me writing the information out to a file.
> somewhere the 'nbsp' is changed, so there's something else going on...
>
> however, the error i get indicates that the char 'u\xa0' is what's causing
> the issue..

As you have written it, that's not a character, it is a string of length
two. Did you perhaps mean the Unicode character u'\xa0'?

>>> len('u\xa0')
2
>>> len(u'\xa0')
1


> as far as i can determine, the string.replace can't/doesn't
> handle non-ascii chars. i'm still looking for a way to search/replace
> non-ascii chars...

Seems to work for me:

>>> c = u'\xa0'
>>> s = "hello " + c + " world"
>>> s
u'hello \xa0 world'
>>> s.replace(c, "?")
u'hello ? world'



--
Steven.

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

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


Client Server Connection Program

2006-07-04 Thread diffuser78
Hi,

I have the following code for a cline and server which I got from a
small tutorial on the web.

#!/ usr/bin/env python
# tms.py (SERVER)

import socket
import sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = int(sys.argv[1])

s.bind((host,port))
s.listen(1)
conn, addr = s.accept()
print 'client is at', addr

data = conn.recv(100)
data = data * 1000

z = raw_input()
conn.send(data)
conn.close()

AND

#/ usr/bin/env python
# filename: tmc.py (CLIENT)

import socket
import sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host = int(sys.argv[1])
port = int(sys.argv[2])

s.connect((host,port))
s.send(sys.argv[3])
i = 0
while True:
data = s.recv(100)
i+=1
if (i<5):
print data
if not data:
break
print 'received', len(data), 'bytes'
s.close()


Server is installed on machine 192.168.1.4 and Client is 192.168.1.2


When I start server and the server is listening I start the clinet.

I started Client using 

python tmc.py 192.168.1.4 2000 abcxyz

where 2000 is the port number  and abcxyz is the string I pass to
server, I get the following error...

ValueError: invalid literal for int(): 192.168.1.4

How can I fix this ? I am trying to achieve the most basic level of
networking between a client and a server.

Every help is appreciated.

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


RE: ascii character - removing chars from string

2006-07-04 Thread bruce
update...

the error i'm getting...

>>>UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in
position 62: ordinal not in range(128)

is there a way i can tell/see what the exact char is at pos(62). i was
assuming that it's the hex \xa0.

i've done the s.replace('\xa0','') with no luck.

-bruce


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf
Of Steven D'Aprano
Sent: Tuesday, July 04, 2006 8:45 AM
To: python-list@python.org
Subject: RE: ascii character - removing chars from string


On Tue, 04 Jul 2006 08:09:53 -0700, bruce wrote:

> simon...
>
> the issue that i'm seeing is not a result of simply using the
> 'string.replace' function. it appears that there's something else going on
> in the text
>
> although i can see the nbsp in the file, the file is manipulated by a
number
> of other functions prior to me writing the information out to a file.
> somewhere the 'nbsp' is changed, so there's something else going on...
>
> however, the error i get indicates that the char 'u\xa0' is what's causing
> the issue..

As you have written it, that's not a character, it is a string of length
two. Did you perhaps mean the Unicode character u'\xa0'?

>>> len('u\xa0')
2
>>> len(u'\xa0')
1


> as far as i can determine, the string.replace can't/doesn't
> handle non-ascii chars. i'm still looking for a way to search/replace
> non-ascii chars...

Seems to work for me:

>>> c = u'\xa0'
>>> s = "hello " + c + " world"
>>> s
u'hello \xa0 world'
>>> s.replace(c, "?")
u'hello ? world'



--
Steven.

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

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


Re: List Manipulation

2006-07-04 Thread Bruno Desthuilliers
Roman wrote:
(please dont top-post - corrected)
> 
> Iain King wrote:
> 
>>Roman wrote:
>>
>>>I would appreciate it if somebody could tell me where I went wrong in
>>>the following snipet:
>>>
(snip)

>>What are you trying to do here?  p[:0]  returns a new list, of all the
>>elements in p up to element 0 (which is of course the empty list),
>>which is then appended to, but is not stored anywhere.  If you want to
>>insert str(col) then use p.insert
>>
>>
>>Iain
> 
> 
> Thanks for your help
> 
> My intention is to create matrix based on parsed csv file. So, I
> would like to have a list of columns (which are also lists).

csv = [
   ['L0C0', 'L0C1', 'L0C2'],
   ['L1C0', 'L1C1', 'L1C2'],
   ['L2C0', 'L2C1', 'L2C2'],
 ]

matrix = [[[] for l in range(len(csv))] for c in range(len(csv[0]))]

for numline, line in enumerate(csv):
for numcol, col in enumerate(line):
matrix[numcol][numline] = col

assert matrix == [
   ['L0C0', 'L1C0', 'L2C0'],
   ['L0C1', 'L1C1', 'L2C1'],
   ['L0C2', 'L1C2', 'L2C2']
 ]

NB : There are probably more elegant solutions.

> I have made the following changes and it still doesn't work.

"doesn't work" is the worst possible description of a problem...

(snip)

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])"
-- 
http://mail.python.org/mailman/listinfo/python-list


Requesting advice on uploading a module

2006-07-04 Thread Anthra Norell



Hi all,
 
   I have 
invested probably 500 hours in the development of a module which I think could 
fill a gap, judging by some problems regularly discussed on this 
forum. The module is finished and I want to contribute it. A kind 
sould recently suggested I upload it to the Cheese Shop. I have made 
several attempts to do so. The deluge of arcane error and why-it-doesn't-work 
messages leave me with the impression that I have to write another 
program to dispatch the thing, beginning with studying an unfamiliar 
environment.
   The module is 
a stream editor capable of handling all 256 octets, multiple substitutions in 
one pass and multiple passes in one run. It handles multiple IO 
types and has a modular building-block architecture. It works as well on a 
command line as integrated in a program. I am very pleased with it.
  Being dramatically 
over budget timewise and keen to resume a healthier sleep pattern, I'd rather 
not have to start another project with a open time line and the name 'Uploading 
to the Cheese Shop'.
  Directions will be 
immensely appreciated. I have Python 2.4, Windows ME,  Cygwin (if that 
helps), and do almost everything in the IDLE window. My program is all 
python code.
 
Thanks
 
Frederic
   
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: converting file formats to txt

2006-07-04 Thread Gaurav Agarwal
Thanks Steven, Actually i wanted a do text processing for my office
where I can view all files in the system and use the first three to
give a summary of the document. Instead of having somebody actually
entering the summary. Seems there is no one code that can act as
convertor across formats, i'll have to check out convertors for
individual formats.

Thanks and Regards,
Gaurav Agarwal

Steven D'Aprano wrote:
> On Tue, 04 Jul 2006 06:32:13 -0700, Gaurav Agarwal wrote:
>
> > Hi,
> >
> > I wanted a script that can convert any file format (RTF/DOC/HTML/PDF/PS
> > etc) to text format.
>
> RTF, HTML and PS are already text format.
>
> DOC is a secret, closed proprietary format. It will be a lot of work
> reverse-engineering it. Perhaps you should consider using existing tools
> that already do it -- see, for example, the word processors Abiword and
> OpenOffice. They are open-source, so you can read and learn from their
> code. Alternatively, you could try some of the suggestions here:
>
> http://www.linux.com/article.pl?sid=06/02/22/201247
>
> Or you could just run through the .doc file, filtering out binary
> characters, and display just the text characters. That's a quick-and-dirty
> strategy that might help.
>
> PDF is (I believe) a compressed, binary format of PS. Perhaps you should
> look at the program pdf2ps -- maybe it will help.
>
> If you explain your needs in a little more detail, perhaps people can give
> you answers which are a little more helpful.
> 
> 
> 
> -- 
> Steven.

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


Re: built in zip function speed

2006-07-04 Thread bearophileHUGS
[EMAIL PROTECTED]:

Using Python you can do:

# Data:
l_a = [1.1, 1.2]
l_b = [2.1, 2.2]
l_c = [3.1, 3.2]
l_d = [5.1, 4.2]

from itertools import izip
l_e = [(c-d) - (a-b)*(a-b) for a,b,c,d in izip(l_a, l_b, l_c, l_d)]
print l_e

With psyco + the standard module array you can probably go quite fast,
Psyco regognizes those arrays and speeds them a lot.

But with something like this you can probably go faster:

from numarray import array
arr_a = array(l_a)
arr_b = array(l_b)
arr_c = array(l_c)
arr_d = array(l_d)
arr_e = (arr_c - arr_d) - (arr_a - arr_b)**2
print arr_e

(Instead of numarray you can use ScyPy, numerics, etc.)
If your data in on disk you can avoid the list=>array conversion, and
load the data from the numerical library itself, this is probably
almost as fast as doing the same thing in C.

Bye,
bearophile

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


RE: ascii character - removing chars from string

2006-07-04 Thread Steven D'Aprano
On Tue, 04 Jul 2006 08:09:53 -0700, bruce wrote:

> simon...
> 
> the issue that i'm seeing is not a result of simply using the
> 'string.replace' function. it appears that there's something else going on
> in the text
> 
> although i can see the nbsp in the file, the file is manipulated by a number
> of other functions prior to me writing the information out to a file.
> somewhere the 'nbsp' is changed, so there's something else going on...
> 
> however, the error i get indicates that the char 'u\xa0' is what's causing
> the issue..

As you have written it, that's not a character, it is a string of length
two. Did you perhaps mean the Unicode character u'\xa0'?

>>> len('u\xa0')
2
>>> len(u'\xa0')
1


> as far as i can determine, the string.replace can't/doesn't
> handle non-ascii chars. i'm still looking for a way to search/replace
> non-ascii chars...

Seems to work for me:

>>> c = u'\xa0'
>>> s = "hello " + c + " world"
>>> s
u'hello \xa0 world'
>>> s.replace(c, "?")
u'hello ? world'



-- 
Steven.

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


Re: built in zip function speed

2006-07-04 Thread [EMAIL PROTECTED]

Fredrik Lundh wrote:
> [EMAIL PROTECTED] wrote:
>
> > ## just for a laugh my own zip function
> > ## the joke is it runs faster than built in zip ??
>
> since it doesn't do the same thing, it's not a very good joke.
>
> > def myzip(*args):
> > index = 0
> > for elem in args[0]:
> > zipper = []
> > for arg in args:
> > zipper.append(arg[index])
> > index = index +1
> > yield zipper
> 
> 

indeed, the joke is on me ;)  I thanks for pointing it out

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


Re: regex module, or don't work as expected

2006-07-04 Thread Fredrik Lundh
Fabian Holler wrote:

> Yes thats right, but that isn't my problem.
> The problem is in the "(?=(iface)|$)" part.

no, the problem is that you're thinking "procedural string matching from 
left to right", but that's not how regular expressions work.

> I have i.e. the text:
> 
> "auto lo eth0
> iface lo inet loopback
> bla
> blub
> 
> iface eth0 inet dhcp
>  hostname debian"
> 
> 
> My regex should match the marked text.
> But it matchs the whole text starting from iface.

which is perfectly valid, since a plain "+" is greedy, and you've asked 
for "iface lo" followed by some text followed by *either* end of string 
or another "iface".  the rest of the string is a perfectly valid string.

if you want a non-greedy match, use "+?" instead.

however, if you just want the text between two string literals, it's 
often more efficient to just split the string twice:

 text = text.split("iface lo", 1)[1].split("iface", 1)[0]



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


Re: built in zip function speed

2006-07-04 Thread [EMAIL PROTECTED]
Steven D'Aprano wrote:
> On Tue, 04 Jul 2006 07:18:29 -0700, [EMAIL PROTECTED] wrote:
>
> > I hope I am not being too ignorant :p but here goes... my boss has
> > written a bit of python code and asked me to speed it up for him...
> > I've reduced the run time from around 20 minutes to 13 (not bad I think
> > ;) to speed it up further I asked him to replace a loop like this:-
> >
> >
> > index = 0
> >
> > for element in a:
> >av = a[index]
> >bv = b[index]
> >cv = c[index]
> >dv = d[index]
> >avbv = (av-bv) * (av-bv)
> >diff = cv - dv
> >e.append(diff - avbv)
> >index = index + 1
>
> This is, I think, a good case for an old-fashioned for-with-index loop:
>
> for i in len(a):
> e.append(c[i] - d[i] - (a[i] - b[i])**2)
>
> Python doesn't optimize away lines of code -- you have to do it yourself.
> Every line of Python code takes a bit of time to execute. My version uses
> 34 lines disassembled; yours takes 60 lines, almost twice as much code.
>
> (See the dis module for further details.)
>
> It's too much to hope that my code will be twice as fast as yours, but it
> should be a little faster.

indeed thanks very much :)

my tests on 4 million:-

slice (original):
7.7336758

built in zip:
36.7350001335

izip:
5.9836758

Steven slice:
4.96899986267


so overall fastest so far









>
> > (where a, b, c and d are 200,000 element float arrays)
> > to use the built in zip function.. it would seem made for this problem!
> >
> > for av, bv, cv, dv in zip(a, b, c, d):
> >avbv = (av-bv) * (av - bv)
> >diff = cv - dv
> >e.append(diff - avbv)
> >
> > however this seems to run much slower than *I* thought it would
> > (and in fact slower than slicing) I guess what I am asking is.. would
> > you expect this?
>
> Yes. zip() makes a copy of your data. It's going to take some time to copy
> 4 * 200,000 floats into one rather large list. That list is an ordinary
> Python list of objects, not an array of bytes like the array module
> uses. That means zip has to convert every one of those 800,000 floats
> into rich Python float objects. This won't matter for small sets of data,
> but with 800,000 of them, it all adds up.
>
>

I was beginning to suspect this was the case (I opened windows task
manager and noticed the memory usage) thanks for explaining it to me.




> -- 
> Steven.

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


Re: List Manipulation

2006-07-04 Thread Roman
Nothing got printed.

Could you tell me what would be pythonic version of what I am trying to
do?


Diez B. Roggisch wrote:
> > p[j] does not give you a reference to an element inside p.  It gives
> > you a new sublist containing one element from p.  You then append a
> > column to that sublist.  Then, since you do nothing more with that
> > sublist, YOU THROW IT AWAY.
>
> Not correct.
>
> p = [[]]
> p[0].append(1)
> print p
>
> yields
>
> [[1]]
>
> p[0] _gives_ you a reference to an object. If it is mutable (list are) and
> append mutates it (it does), the code is perfectly alright.
>
> I don't know what is "not working" for the OP, but actually his code works
> if one replaces the csv-reading with a generated list:
>
> cnt = 0
> p=[[], [], [], [], [], [], [], [], [], [], []]
> reader = [["column_%i" % c for c in xrange(5)] for l in xrange(7)]
> for line in reader:
> if cnt > 6:
>break
> j = 0
> for col in line:
>p[j].append(col)
>j=j+1
> cnt = cnt + 1
> print p
>
>
> You are right of course that it is the most unpythonic way imaginabe to do
> it. But it works.
> 
> Diez

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


Re: built in zip function speed

2006-07-04 Thread Rune Strand

> so fastest overall

you may experience speed-ups by using

from itertools import izip

and just use izip() instead to avoid the module namespace lookup. The
same applies for the list.append() methods. If you're appending some
million times

a_list = []
a_list_append = a_list.append
a_list_append(value)

will be faster than

a_list.append(value)

but not much.

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


Re: built in zip function speed

2006-07-04 Thread Steven D'Aprano
On Tue, 04 Jul 2006 07:18:29 -0700, [EMAIL PROTECTED] wrote:

> I hope I am not being too ignorant :p but here goes... my boss has
> written a bit of python code and asked me to speed it up for him...
> I've reduced the run time from around 20 minutes to 13 (not bad I think
> ;) to speed it up further I asked him to replace a loop like this:-
> 
> 
> index = 0
> 
> for element in a:
>av = a[index]
>bv = b[index]
>cv = c[index]
>dv = d[index]
>avbv = (av-bv) * (av-bv)
>diff = cv - dv
>e.append(diff - avbv)
>index = index + 1

This is, I think, a good case for an old-fashioned for-with-index loop:

for i in len(a):
e.append(c[i] - d[i] - (a[i] - b[i])**2)

Python doesn't optimize away lines of code -- you have to do it yourself.
Every line of Python code takes a bit of time to execute. My version uses
34 lines disassembled; yours takes 60 lines, almost twice as much code.

(See the dis module for further details.)

It's too much to hope that my code will be twice as fast as yours, but it
should be a little faster.

> (where a, b, c and d are 200,000 element float arrays)
> to use the built in zip function.. it would seem made for this problem!
> 
> for av, bv, cv, dv in zip(a, b, c, d):
>avbv = (av-bv) * (av - bv)
>diff = cv - dv
>e.append(diff - avbv)
> 
> however this seems to run much slower than *I* thought it would
> (and in fact slower than slicing) I guess what I am asking is.. would
> you expect this?

Yes. zip() makes a copy of your data. It's going to take some time to copy
4 * 200,000 floats into one rather large list. That list is an ordinary
Python list of objects, not an array of bytes like the array module
uses. That means zip has to convert every one of those 800,000 floats
into rich Python float objects. This won't matter for small sets of data,
but with 800,000 of them, it all adds up.


-- 
Steven.

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


Re: built in zip function speed

2006-07-04 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote:

> ## just for a laugh my own zip function
> ## the joke is it runs faster than built in zip ??

since it doesn't do the same thing, it's not a very good joke.

> def myzip(*args):
> index = 0
> for elem in args[0]:
> zipper = []
> for arg in args:
> zipper.append(arg[index])
> index = index +1
> yield zipper



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


Re: regex module, or don't work as expected

2006-07-04 Thread Fabian Holler
Hello Marc,

thank you for your answer.

Marc 'BlackJack' Rintsch wrote:
> In <[EMAIL PROTECTED]>, Fabian Holler wrote:

>> i have the following regex "iface lo[\w\t\n\s]+(?=(iface)|$)"
>> 
>> If "iface" don't follow after the regex "iface lo[\w\t\n\s]" the rest of
>> the text should be selected.
>> But ?=(iface) is ignored, it is always the whole texte selected.
>> What is wrong?
> 
> The ``+`` after the character class means at least one of the characters
> in the class or more.  If you have a text like:

Yes thats right, but that isn't my problem.
The problem is in the "(?=(iface)|$)" part.

I have i.e. the text:

"auto lo eth0
iface lo inet loopback
bla
blub

iface eth0 inet dhcp
 hostname debian"


My regex should match the marked text.
But it matchs the whole text starting from iface.
If there is only one iface entry, the whole text starting from iface
should be matched.

greetings

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


Re: List Manipulation

2006-07-04 Thread Diez B. Roggisch
> p[j] does not give you a reference to an element inside p.  It gives
> you a new sublist containing one element from p.  You then append a
> column to that sublist.  Then, since you do nothing more with that
> sublist, YOU THROW IT AWAY.

Not correct.

p = [[]]
p[0].append(1)
print p

yields

[[1]]

p[0] _gives_ you a reference to an object. If it is mutable (list are) and
append mutates it (it does), the code is perfectly alright.

I don't know what is "not working" for the OP, but actually his code works
if one replaces the csv-reading with a generated list:

cnt = 0
p=[[], [], [], [], [], [], [], [], [], [], []]
reader = [["column_%i" % c for c in xrange(5)] for l in xrange(7)]
for line in reader:
if cnt > 6:
   break
j = 0
for col in line:
   p[j].append(col)
   j=j+1
cnt = cnt + 1
print p


You are right of course that it is the most unpythonic way imaginabe to do
it. But it works.

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


Re: For a fast implementation of Python

2006-07-04 Thread .
Hi Terry.

I see:
http://codespeak.net/pypy/dist/pypy/doc/news.html
"...by translating RPython to Javascript..."

It isn't an implementation.

--
JavaScript implementation of Python
http://groups.google.it/group/JSython/


Terry Hancock ha scritto:

> . wrote:
>
> >  What is the fast way for a fast implementation of Python?
> >
> >  -- JavaScript implementation of Python
> >  http://groups.google.it/group/JSython/
>
> Follow the PyPy link.  The other replies were about increasing
> execution speed, not ease of implementation.
>
> Implement the "RPython" part in Javascript, then use
> the PyPy project to fill in the rest?  That way, you don't
> even have to maintain most of it.
>
> http://codespeak.net/pypy/dist/pypy/doc/news.html
>
> Cheers,
> Terry
>
>
> --
> Terry Hancock ([EMAIL PROTECTED])
> Anansi Spaceworks http://www.AnansiSpaceworks.com

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


RE: ascii character - removing chars from string

2006-07-04 Thread bruce
simon...

the issue that i'm seeing is not a result of simply using the
'string.replace' function. it appears that there's something else going on
in the text

although i can see the nbsp in the file, the file is manipulated by a number
of other functions prior to me writing the information out to a file.
somewhere the 'nbsp' is changed, so there's something else going on...

however, the error i get indicates that the char 'u\xa0' is what's causing
the issue.. as far as i can determine, the string.replace can't/doesn't
handle non-ascii chars. i'm still looking for a way to search/replace
non-ascii chars...

this would/should resolve my issue..

-bruce


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf
Of Simon Forman
Sent: Monday, July 03, 2006 11:28 PM
To: python-list@python.org
Subject: Re: ascii character - removing chars from string


bruce wrote:
> simon...
>
> the ' ' is not to be seen/viewed as text/ascii.. it's a
representation
> of a hex 'u\xa0' if i recall...

Did you not see this part of the post that you're replying to?

>  'nbsp': '\xa0',

My point was not that '\xa0' is an ascii character... It was that your
initial request was very misleading:

"i'm running into a problem where i'm seeing non-ascii chars in the
parsing i'm doing. in looking through various docs, i can't find
functions to remove/restrict strings to valid ascii chars."

That's why you got three different answers to the wrong question.

You weren't "seeing non-ascii chars" at all.  You were seeing ascii
representations of html entities that, in the case of ' ', happen
to represent non-ascii values.

>
> i'm looking to remove or replace the insances with a ' ' (space)

Simplicity:

s.replace(' ', ' ')

~Simon

"You keep using that word.  I do not think it means what you think it
means."
 -Inigo Montoya, "The Princess Bride"

>
> -bruce
>
>
> -Original Message-
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] Behalf
> Of Simon Forman
> Sent: Monday, July 03, 2006 7:17 PM
> To: python-list@python.org
> Subject: Re: ascii character - removing chars from string
>
>
> bruce wrote:
> > hi...
> >
> > update. i'm getting back html, and i'm getting strings like " foo
 "
> > which is valid HTML as the ' ' is a space.
>
> &, n, b, s, p, ;  Those are all ascii characters.
>
> > i need a way of stripping/removing the ' ' from the string
> >
> > the   needs to be treated as a single char...
> >
> >  text = "foo cat  "
> >
> >  ie ok_text = strip(text)
> >
> >  ok_text = "foo cat"
>
> Do you really want to remove those html entities?  Or would you rather
> convert them back into the actual text they represent?  Do you just
> want to deal with  's?  Or maybe the other possible entities that
> might appear also?
>
> Check out htmlentitydefs.entitydefs (see
> http://docs.python.org/lib/module-htmlentitydefs.html)  it's kind of
> ugly looking so maybe use pprint to print it:
>
> >>> import htmlentitydefs, pprint
> >>> pprint.pprint(htmlentitydefs.entitydefs)
> {'AElig': 'Æ',
>  'Aacute': 'Á',
>  'Acirc': 'Â',
> .
> .
> .
>  'nbsp': '\xa0',
> .
> .
> .
> etc...
>
>
> HTH,
> ~Simon
>
> "You keep using that word.  I do not think it means what you think it
> means."
>  -Inigo Montoya, "The Princess Bride"
>
> --
> http://mail.python.org/mailman/listinfo/python-list

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

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


Re: List Manipulation

2006-07-04 Thread Mike Kent
Roman wrote:
> Thanks for your help
>
> My intention is to create matrix based on parsed csv file.  So, I would
> like to have a list of columns (which are also lists).
>
> I have made the following changes and it still doesn't work.
>
>
> cnt = 0
> p=[[], [], [], [], [], [], [], [], [], [], []]
> reader = csv.reader(file("f:\webserver\inp.txt"), dialect="excel",
>  quotechar="'", delimiter='\t')
> for line in reader:
> if cnt > 6:
>break
> j = 0
> for col in line:
>p[j].append(col)
>j=j+1
> cnt = cnt + 1
>
> print p

p[j] does not give you a reference to an element inside p.  It gives
you a new sublist containing one element from p.  You then append a
column to that sublist.  Then, since you do nothing more with that
sublist, YOU THROW IT AWAY.

Try doing:

p[j] = p[j].append(col)

However, this will still result in inefficient code.  Since every line
you read in via the csv reader is already a list, try this (untested)
instead:

reader = csv.reader(file("f:\webserver\inp.txt"), dialect="excel",
 quotechar="'", delimiter='\t')
p = [ line for line in reader[:7] ]

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


Re: built in zip function speed

2006-07-04 Thread [EMAIL PROTECTED]

Rune Strand wrote:
> itertools.izip is usually faster than zip. You can try that.


Thanks very much

timing for itertools.izip

for av, bv, cv, dv in itertools.izip(a, b, c, d):
   avbv = (av-bv) * (av - bv)
   diff = cv - dv
   e.append(diff - avbv)


on a 4 million element aray:

slice:
8.0626376

built in zip:
36.516599

myzip:
12.032648

izip:
5.76499986649


so fastest overall

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


Re: regex module, or don't work as expected

2006-07-04 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Fabian Holler wrote:

> Howdy,
> 
> 
> i have the following regex "iface lo[\w\t\n\s]+(?=(iface)|$)"
> 
> If "iface" don't follow after the regex "iface lo[\w\t\n\s]" the rest of
> the text should be selected.
> But ?=(iface) is ignored, it is always the whole texte selected.
> What is wrong?

The ``+`` after the character class means at least one of the characters
in the class or more.  If you have a text like:

  iface lox iface

Then the it matches the space and the word ``iface`` because the space
(``\s``) and word characters (``\w``) are part of the character class and
``+`` is "greedy".  It consumes as many characters as possible and the
rest of the regex is only evaluated when there are no matches anymore.

If you want to match non-greedy then put a ``?`` after the ``+``::

  iface lo[\w\t\n\s]+?(?=(iface)|$)

Now only "iface lox " is matched in the example above.

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


Re: Can anyone please analyse this program for me (write a pseudocode for it).

2006-07-04 Thread Steven D'Aprano
On Mon, 03 Jul 2006 06:20:36 -0700, Vusi wrote:

> /* $Id: dotquad.c 3529 2005-10-01 10:15:22Z dyoung $ */
> /*
>  * Copyright (c) 2003, 2004 David Young.  All rights reserved.
>  *
>  * This code was written by David Young.

[snip code]

Am I the only one who found it hilarious that this piece of code was made
up of 24 lines of actual code (including lines containing only a single
brace) but 29 lines of legalise?


-- 
Steven.

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


Re: List Manipulation

2006-07-04 Thread Sibylle Koczian
Roman schrieb:
> I would appreciate it if somebody could tell me where I went wrong in
> the following snipet:
> 
> When I run I get no result
> 
> cnt = 0
> p=[]
> reader = csv.reader(file("f:\webserver\inp.txt"), dialect="excel",
>  quotechar="'", delimiter='\t')
> for line in reader:
> if cnt > 6:
>break
> for col in line:
>p[:0].append(str(col))

This is wrong. I'm not absolutely certain _what_ it does, but it doesn't
append anything to list p. p[:0] is an empty copy of p, you are
appending to this empty copy, not to p. What's wrong with
p.append(str(col))?

> when I change it to the following, I get rows back
> 
> cnt = 0
> p=[]
> reader = csv.reader(file("f:\webserver\inp.txt"), dialect="excel",
>  quotechar="'", delimiter='\t')
> for line in reader:
> if cnt > 6:
>break
> for col in line:
>print col
> cnt = cnt + 1
> 
> print p
> 

Here you print every single cell, but p doesn't change.

HTH
Koczian

-- 
Dr. Sibylle Koczian
Universitaetsbibliothek, Abt. Naturwiss.
D-86135 Augsburg
e-mail : [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: List Manipulation

2006-07-04 Thread Roman
Thanks for your help

My intention is to create matrix based on parsed csv file.  So, I would
like to have a list of columns (which are also lists).

I have made the following changes and it still doesn't work.


cnt = 0
p=[[], [], [], [], [], [], [], [], [], [], []]
reader = csv.reader(file("f:\webserver\inp.txt"), dialect="excel",
 quotechar="'", delimiter='\t')
for line in reader:
if cnt > 6:
   break
j = 0
for col in line:
   p[j].append(col)
   j=j+1
cnt = cnt + 1

print p

Iain King wrote:
> Roman wrote:
> > I would appreciate it if somebody could tell me where I went wrong in
> > the following snipet:
> >
> > When I run I get no result
> >
> > cnt = 0
> > p=[]
> > reader = csv.reader(file("f:\webserver\inp.txt"), dialect="excel",
> >  quotechar="'", delimiter='\t')
> > for line in reader:
> > if cnt > 6:
> >break
> > for col in line:
> >p[:0].append(str(col))
>
> What are you trying to do here?  p[:0]  returns a new list, of all the
> elements in p up to element 0 (which is of course the empty list),
> which is then appended to, but is not stored anywhere.  If you want to
> insert str(col) then use p.insert
> 
> 
> Iain

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


Re: fonction in python

2006-07-04 Thread Steven D'Aprano
On Tue, 04 Jul 2006 03:06:37 -0700, aliassaf wrote:

> 
> Hello, 
>   
> If we write = x^2  and if I give to the program the values of x, it will
> going to calculate the values of y, and also for x.   
> 
> But it is possible ? that is if I give to the program the values of X and Y,
> it will indicate to me the relation between the two variables, in the other
> hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me
> that f (t)!!! 

You are asking for curve-fitting. There is a HUGE amount of work on
curve-fitting in computer science and statistics.

Generally, you start with some data points (x, y). You generally have some
idea of what sort of function you expect -- is it a straight line? A
curve? What sort of curve? A polynomial, an exponential, a sine curve, a
cubic spline, a Bezier curve?

You might like to google on "least squares curve fitting" and "linear
regression". That's just two methods out of many.

Some curve-fitting methods also estimate the error between the predicted
curve and the data points; you could then try all of the methods and pick
the one with the least error.



-- 
Steven.

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


Re: handling unicode data

2006-07-04 Thread Filipe
Martin v. Löwis wrote:
> Filipe wrote:
> >  output ---
> > u'Fran\xd8a'
> > FranØa
> > 
> >
> > What do you think? Might it be Pymssql doing something wrong?
>
> I think the data in your database is already wrong. Are you
> sure the value in question is really "França" in the database?
>

yes, I'm pretty sure. There's an application that was built to run on
top of this database and it correctly reads as writes data to the DB. I
also used SqlServer's Query Analyzer to select the data and it
displayed fine.

I've done some more tests and I think I'm very close to finding what
the problem is. The tests I had done before were executed from the
windows command line. I tried printing the following (row[1] is a value
I selected from the database) in two distinct environments, from within
an IDE (Pyscripter)  and from the command line:

import sys
import locale
print getattr(sys.stdout,'encoding',None)
print locale.getdefaultlocale()[1]
print sys.getdefaultencoding()
term = "Fran\x87a"
print repr(term)
term = row[1]
print repr(term)

output I got in Pyscripter's interpreter window:
None
cp1252
ascii
'Fran\x87a'
'Fran\x87a'

output I got in the command line:
cp1252
cp1252
ascii
'Fran\x87a'
'Fran\xd8a'

I'd expect "print" to behave differently according with the console's
encoding, but does this mean this happens with repr() too?
in which way?

thanks,
Filipe

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


  1   2   >