Re: Sine Wave Curve Fit Question

2008-01-31 Thread Mikael Olofsson
Helmut Jarausch wrote:
> Your model is  A*sin(omega*t+alpha)  where  A and alpha are sought.
> Let T=(t_1,...,t_N)' and  Y=(y_1,..,y_N)'  your measurements (t_i,y_i)
> ( ' denotes transposition )
> 
> First, A*sin(omega*t+alpha) =
>   A*cos(alpha)*sin(omega*t) + A*sin(alpha)*cos(omega*t) =
>   B*sin(omega*t) + D*cos(omega*t)
> 
> by setting  B=A*cos(alpha)  and  D=A*sin(alpha)
> 
> Once, you have  B and D,  tan(alpha)= D/B   A=sqrt(B^2+D^2)

This is all very true, but the equation tan(alpha)=D/B may fool you. 
This may lead you to believe that alpha=arctan(D/B) is a solution, which 
is not always the case. The point (B,D) may be in any of the four 
quadrants of the plane. Assuming B!=0, the solutions to this equation 
fall into the two classes

 alpha = arctan(D/B) + 2*k*pi

and

 alpha = arctan(D/B) + (2*k+1)*pi,

where k is an integer. The sign of B tells you which class gives you the 
solution. If B is positive, the solutions are those in the first class. 
If B is negative, the solutions are instead those in the second class. 
Whithin the correct class, you may of course choose any alternative.

Then we have the case B=0. Then the sign of D determines alpha. If D is 
positive, we have alpha=pi/2, and if D is negative, we have alpha=-pi/2.

Last if both B and D are zero, any alpha will do.

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


Re: list traversal and remove

2008-01-31 Thread Arnaud Delobelle
On Jan 31, 7:49 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> I supposed the below code will print seven 2 and generate the list li
> without 2.
> Strangely it only print  four 2. If you change the number of 2 in the
> list, the results are all beyond expectation.
> I know the other way to achieve the expected goal, but why this is
> happening? Could somebody enlight me?
>
> li= [2,2,2,2,2,2,2,3,4,5,6,7,8,9]
> for x in li:
>     if x == 2:
>         print x
>         li.remove(x)

There's just been a thread on this:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/290623baaa46e72a/

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


Re: Updating documents in PyLucene

2008-01-31 Thread Jarek Zgoda
[EMAIL PROTECTED] napisał(a):

> The way that Lucene (and by extension, PyLucene) seems to work is that
> updates to documents are implemented by the user as a document
> addition (of the new version) and subsequent deletion (of the old
> version).

I'd switch the operations, first delete then add. Solr does this that
way and I decided to follow.

> My problem is that I'd like to update a number of documents which have
> their Store flag set to NO - they're indexed, but not stored. I don't
> have the original text content of these documents available anywhere
> else - is there any way for me to get this un-stored indexed data from
> the old document into the new?

I think the answer is "no", there has to be some way of identifying
records that have to be deleted. If you do not store any document UID,
you are out of luck.

Anyway, you may get some hints on lucene mailing list.

-- 
Jarek Zgoda
Skype: jzgoda | GTalk: [EMAIL PROTECTED] | voice: +48228430101

"We read Knuth so you don't have to." (Tim Peters)
-- 
http://mail.python.org/mailman/listinfo/python-list


How does one get onto planet.python.org?

2008-01-31 Thread Guyon Morée
Hi,

I'm looking for a signup link to get my blog included in 
http://planet.python.org/,
but I couldn't find one. I emailed [EMAIL PROTECTED] to no avail.

any ideas?

thanks,

Guyon
http://www.gumuz.nl/blog/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list traversal and remove

2008-01-31 Thread Steven D'Aprano
On Wed, 30 Jan 2008 23:49:46 -0800, [EMAIL PROTECTED] wrote:

> I supposed the below code will print seven 2 and generate the list li
> without 2.
> Strangely it only print  four 2. If you change the number of 2 in the
> list, the results are all beyond expectation. I know the other way to
> achieve the expected goal, but why this is happening? Could somebody
> enlight me?

Do not modify a list at the same time that you are traversing it.

If you look at the archives (say, on Google Groups, or any number of 
other places), this topic was just discussed yesterday and earlier today.

See the thread with subject line:

"Removal of element from list while traversing causes the next  element 
to be skipped"

As for why it is happening... you have code that looks like this:

for x in li:
if x == 2:
print x
li.remove(x)


Consider that "under the hood", the for-loop looks something like this:

i = 0
while i < the length of the list:
set x equal to the item in the i-th position
execute the loop block of code
i += 1

If you start deleting or inserting items in the middle of the loop, the 
index won't be pointing where you expect.



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


Re: Import of cStringIO failing with "undefined symbol: PyObject_SelfIter" on python-2.3.3-88.9

2008-01-31 Thread grbgooglefan
On Jan 10, 6:07 pm, grbgooglefan <[EMAIL PROTECTED]> wrote:
> I am importing cStringIO module in my PythonC++ embedded program.
>
> The import is failing with the following error:
> ImportError: /usr/lib/python2.3/lib-dynload/cStringIO.so: undefined
> symbol:PyObject_SelfIter
>
> I have python-2.3.3-88.9.x86 installed on my machine.
> Why is this error coming? how can I resolve this undefined symbol?
> Do I need to import anything before this?

Workaround to avoid this problem is in another post named
"PyImport_ImportModule("cStringIO") failure with undefined symbol
Options".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyImport_ImportModule("cStringIO") failure with undefined symbol

2008-01-31 Thread grbgooglefan
On Jan 11, 9:31 am, "Borse, Ganesh" <[EMAIL PROTECTED]>
wrote:
> Hi,
> Can you please guide me for the following problem?
> The call to "PyImport_ImportModule("cStringIO");" is failing with an error of 
> "undefined symbol:PyObject_SelfIter".
>
> Before importing this module, I am importing only the sys module.
>
>    Py_SetProgramName("/usr/bin/python");
>    Py_Initialize();
>    char* argv[] = { "python","-v",""};
>    PySys_SetArgv(2,argv);
>    PyRun_SimpleString("import sys");
>
>    PyObject *modStringIO = NULL;
>    // Import cStringIO module
>    modStringIO = PyImport_ImportModule("cStringIO");
>
> Should I be importing any other additional module(s) to make this import work?
>
> Please help.
>
> I am trying to use the function GetPythonErrorMessage provided in this 
> post:http://groups.google.com/group/comp.lang.python/browse_thread/thread/...
>
> Thanks in advance for your help.
> Regards.
==

Function "PyObject_SelfIter" is part of libPython.a. But when library
which is linked with libPython.a tries to import cStringIO (which is
nothing but dlopen("lib-dynload/cStringIO.so"), ld.so does not find
this symbol.
So, as a workaround, we can get the cStringIO.so also linked with the
library which has linked libPython.a.
By this we ensure that PyObject_SelfIter is already resolved in the
library.
Then at the time of importing cStringIO at runtime, this symbol is
already referenced & won't cause problems.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: REALLY simple xml reader

2008-01-31 Thread Ricardo Aráoz
Ivan Illarionov wrote:
 from xml.etree import ElementTree as et
 from decimal import Decimal

 root = et.parse('file/with/your.xml')
 debits = dict((debit.attrib['category'], 
 Decimal(debit.find('amount').text)) for debit in root.findall('debit'))

 for cat, amount in debits.items():
> ...   print '%s: %s' % (cat, amount)
> ...
> food: 24.30
> car: 909.56
> medical: 188.20
> savings: 25
> withdrawal: 40
> supplies: 10.58
> clothes: 31.19
> 

Thanks Ivan, it seems a elegant API, and easy to use.
I tried to play a little with it but unfortunately could not get it off
the ground. I kept getting
>>> root = et.fromstring(doc)
Traceback (most recent call last):
  File "", line 1, in 
  File "E:\Python25\lib\xml\etree\ElementTree.py", line 963, in XML
parser.feed(text)
  File "E:\Python25\lib\xml\etree\ElementTree.py", line 1245, in feed
self._parser.Parse(data, 0)
ExpatError: XML or text declaration not at start of entity: line 2, column 0

But it's probably my lack of knowledge on the subject. Well, I guess
there is no free ride and I'll take a look at ElementTree as soon as I
have some spare time, looks promising.
One last question. Am I right to believe "debit" would be an "et" object
of the same class as "root"?
THX
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get string printed by PyErr_Print( )?

2008-01-31 Thread grbgooglefan
On Jan 9, 8:01 pm, grbgooglefan <[EMAIL PROTECTED]> wrote:
> On Dec 19 2007, 5:55 pm, Christian Heimes <[EMAIL PROTECTED]> wrote:
> >grbgooglefanwrote:
> > > PythonC API functionPyErr_Print( ) prints an error string onto stderr
> > > if PyErr_Occurred() is true.
> > > I don't want to print this to stderr because my Python+C code is
> > > running daemon mode & won't have terminal / stderr.
> > > So, I want to retrieve the string whichPyErr_Print( ) will print.
> > > E.g.,PyErr_Print() printed following string when I tried to call
> > > setTuple with one extra argument
> > > Traceback (most recent call last):
> > >   File "", line 2, in isTacticSafe
> > > IndexError: tuple assignment index out of range
>
> > I suggest a different approach. A daemon must have a stdin, stdout and
> > stderr connected to a terminal. You can use freopen() to redirect stderr
> > and stdout to a log file and fclose() to close stdin.
>
> >http://www.gnu.org/software/libc/manual/html_mono/libc.html#Opening-S...
>
> > Christian
>
> I do not want to redirect anything to file. Because I do not want to
> avoid the disk access completely - for reading as well as writing.
> I liked the 1st option suggested by Robert Kern.
>
> Can you please explain a little bit how can I replace sys.stderr with
> StringIO or my char* buffer?
> I have to show the error message of Python code compilation &
> execution to the user on the GUI & therefore I want to capture the
> error message directly instead of logging it to file.
>
> Thanks in advance for all your help.- Hide quoted text -
>
> - Show quoted text -

Well, I managed to do it myself. Here is what I am doing now to do
this. In Python-C/C++ world, if someone wants to do the same thing,
may use this.
Note: For this, am assigning the cStringIO object to sys.stderr once I
do Py_Initialize & then use cStringIO.getvalue() everytime I get
error.
While importing cStringIO module with Python-2.3.3, I faced the
problem of "undefined symbol:PyObject_SelfIter". I could resolve that
also. I have put that note in the post which I had opened for that.
/+++
PyObject *_pPyModule;
PyObject *_pPyDictionary;
PyObject *_pPyGetValFunc;
PyObject *_pPyobStringIO;

Init(){
// Py_Initialize should have been done by now.
   PyObject *modStringIO = NULL;
   PyObject *obFuncStringIO = NULL;

   // Import cStringIO module
   modStringIO = PyImport_ImportModule("cStringIO");
   if(PyErr_Occurred() || modStringIO == NULL){
 printf("pyParserEvaluator::Init::PyImport cStringIO failed:");
 PyErr_Print();
 goto PY_INIT_ERR;
   }
   // get StringIO constructor
   obFuncStringIO = PyObject_GetAttrString(modStringIO, "StringIO");
   if(PyErr_Occurred() || obFuncStringIO == NULL){
 printf("pyParserEvaluator::Init: cant find cStringIO.StringIO:");
 PyErr_Print();
 goto PY_INIT_ERR;
   }
   // Construct cStringIO object
   _pPyobStringIO = PyObject_CallObject(obFuncStringIO, NULL);
   if(PyErr_Occurred() || _pPyobStringIO==NULL){
 printf("pyParserEvaluator::Init: cStringIO.StringIO() failed:");
 PyErr_Print();
 goto PY_INIT_ERR;
   }
   // get getvalue() method in StringIO instance
   _pPyGetValFunc = PyObject_GetAttrString(_pPyobStringIO,
"getvalue");
   if(PyErr_Occurred() || _pPyGetValFunc==NULL){
 printf("pyParserEvaluator::Init: cant find getvalue function:");
 PyErr_Print();
 goto PY_INIT_ERR;
   }
   // try assigning this object to sys.stderr
   ret = PySys_SetObject("stderr", _pPyobStringIO);
   if(ret != 0){
  printf("failed to assign _pPyobStringIO to stderr\n");
  goto PY_INIT_ERR;
   }
   return ret;

PY_INIT_ERR:
   Py_XDECREF(modStringIO);
   Py_XDECREF(obFuncStringIO);
   Py_XDECREF(_pPyobStringIO);
   Py_XDECREF(_pPyGetValFunc);
}

int _getPythonErrorMessage()
{
// call getvalue() method in StringIO instance
int ret = 0;
PyObject *obResult=NULL;
char *sresult = NULL;
obResult = PyObject_CallObject(_pPyGetValFunc, NULL);
if(PyErr_Occurred() || obResult==NULL){
   printf("getvalue() failed\n");
   ret = -1;
   goto CLEAN_AND_RETURN;
}
// did getvalue return a string?
if(!PyString_Check(obResult)){
   printf("getvalue() did not return error string\n");
   ret = -1;
   goto CLEAN_AND_RETURN;
}
// retrieve error message string from this object
if(NULL != (sresult = PyString_AsString(obResult))){
   pErrorString = strdup(sresult);
} else {
   ret = -1;
   goto CLEAN_AND_RETURN;
}
return(ret);

CLEAN_AND_RETURN:
   Py_XDECREF(obResult);
   return(ret);
}
=
-- 
http://mail.python.org/mailman/listinfo/python-list


dl module

2008-01-31 Thread Nicola Jean
Hi everyone,
I'm having a problem compiling Python2.4.4 on a 64 bits machine. It
looks like I cannot compile the dl module.
When I run test_dl.py I get the following error message:
SystemError: module dl requires sizeof(int) == sizeof(long) == sizeof(char*)

Do I need to put any special flag when I run the configure script?
Cheers
N.Jean
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: REALLY simple xml reader

2008-01-31 Thread Diez B. Roggisch
Ricardo Aráoz schrieb:
> Ivan Illarionov wrote:
> from xml.etree import ElementTree as et
> from decimal import Decimal
>
> root = et.parse('file/with/your.xml')
> debits = dict((debit.attrib['category'], 
> Decimal(debit.find('amount').text)) for debit in root.findall('debit'))
>
> for cat, amount in debits.items():
>> ...   print '%s: %s' % (cat, amount)
>> ...
>> food: 24.30
>> car: 909.56
>> medical: 188.20
>> savings: 25
>> withdrawal: 40
>> supplies: 10.58
>> clothes: 31.19
>>
> 
> Thanks Ivan, it seems a elegant API, and easy to use.
> I tried to play a little with it but unfortunately could not get it off
> the ground. I kept getting
 root = et.fromstring(doc)
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "E:\Python25\lib\xml\etree\ElementTree.py", line 963, in XML
> parser.feed(text)
>   File "E:\Python25\lib\xml\etree\ElementTree.py", line 1245, in feed
> self._parser.Parse(data, 0)
> ExpatError: XML or text declaration not at start of entity: line 2, column 0

That's a problem in your XML not being XML. Has nothing to do with 
element-tree - as one sees from the error-message "ExpatError". If you 
show it to us, we might see why.

> But it's probably my lack of knowledge on the subject. Well, I guess
> there is no free ride and I'll take a look at ElementTree as soon as I
> have some spare time, looks promising.
> One last question. Am I right to believe "debit" would be an "et" object
> of the same class as "root"?


Yes.

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


Re: Online Debugging

2008-01-31 Thread Eddie Corns
Yansky <[EMAIL PROTECTED]> writes:

>I'm trying to debug a script on my server and it's taking forever
>using print to find the error. I've tried to use the debugging
>examples on this page http://webpython.codepoint.net/debugging but
>they don't seem to be working for me.

>Is there an easier/better way to debug online scripts? I was hoping
>there might be something similar to php where it gives some error info
>and the line code.

One simple method might be something along these lines:

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

which I came across yesterday.

Eddie

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


Re: Design question - Sharing of single object by multiple processes

2008-01-31 Thread Steve Holden
Mike D wrote:
> Hello, I've just picked up the Python language and am really enjoying it.
> 
> I've just signed up to this mailing list and I'm looking forward to 
> taking part in some discussions.
> 
> My first post is a question I've been pondering for the last couple of days:
> 
> For relatively static data (such as a list), is it possible to load a 
> single instance and have many python processes access it?
> 
First there's the problem of having multiple processes access any kind 
of shared resource. So your question makes me wonder whether you mean 
processes, or threads. Are you *sure* you mean processes?

> I want to have an object in memory. This object will be loaded on 
> application start up and will contain a list of objects. These will be 
> obtained from (most likely) the file system.
> 
So "application startup" is different from "process startup"? Your 
application is a group of co-operating processes? Really?

> My reasoning is: I want to prevent a file system or database fetch each 
> time as it seems unnecessary.
> 
It might also seem unnecessary to start the Python interpreter for each 
process, but you are going to have to ...

> Is this possible? In Java I'm pretty sure this could implemented with an 
> object factory and static methods. My understanding is that python 
> invokes a separate process for each request however.
> 
Your understanding is clearly informed by some information you have 
omitted to share with us.

> If possible, what would be a good way of providing a new structure as it 
> is updated.
> 
> If anyone could give me some advice or point me in the correct direction 
> I'd really appreciate it.
> 
> Thanks in advance,
> 
More answers, please. There are mechanisms such as Pyro (Python remote 
objects) that allow inter-process method calls, but I am far from 
convinced that's what you really want (or need, anyway). Perhaps yo 
could let us know a little more about what it really is you are trying 
to do, and convince me, at least, that this isn't just a case of 
premature optimization.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: REALLY simple xml reader

2008-01-31 Thread Ricardo Aráoz
Diez B. Roggisch wrote:
> Ricardo Aráoz schrieb:
>> Thanks Ivan, it seems a elegant API, and easy to use.
>> I tried to play a little with it but unfortunately could not get it off
>> the ground. I kept getting
> root = et.fromstring(doc)
>> Traceback (most recent call last):
>>   File "", line 1, in 
>>   File "E:\Python25\lib\xml\etree\ElementTree.py", line 963, in XML
>> parser.feed(text)
>>   File "E:\Python25\lib\xml\etree\ElementTree.py", line 1245, in feed
>> self._parser.Parse(data, 0)
>> ExpatError: XML or text declaration not at start of entity: line 2, column 0
> 
> That's a problem in your XML not being XML. Has nothing to do with 
> element-tree - as one sees from the error-message "ExpatError". If you 
> show it to us, we might see why.
> 

Sure,

doc = """


expenses: january 2002

  
31.19
200213
Walking Store
shoes
  

  
1549.58
200217
Bob's Bolts
  

  
40
200218
pocket money
  

  
25
200218
  

  
188.20
200218
Boston Endodontics
cavity
  

  
10.58
2002110
Exxon Saugus
gasoline
  

  
909.56
2002114
Honda North
car repairs
  

  
24.30
2002115
Johnny Rockets
lunch
  

"""
I thought this was proper XML as it comes straight out from an O'Reilly
XML book.

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


Re: REALLY simple xml reader

2008-01-31 Thread Diez B. Roggisch
Ricardo Aráoz schrieb:
> Diez B. Roggisch wrote:
>> Ricardo Aráoz schrieb:
>>> Thanks Ivan, it seems a elegant API, and easy to use.
>>> I tried to play a little with it but unfortunately could not get it off
>>> the ground. I kept getting
>> root = et.fromstring(doc)
>>> Traceback (most recent call last):
>>>   File "", line 1, in 
>>>   File "E:\Python25\lib\xml\etree\ElementTree.py", line 963, in XML
>>> parser.feed(text)
>>>   File "E:\Python25\lib\xml\etree\ElementTree.py", line 1245, in feed
>>> self._parser.Parse(data, 0)
>>> ExpatError: XML or text declaration not at start of entity: line 2, column 0
>> That's a problem in your XML not being XML. Has nothing to do with 
>> element-tree - as one sees from the error-message "ExpatError". If you 
>> show it to us, we might see why.
>>
> 
> Sure,
> 
> doc = """
> 

It's not allowed to have a newline before the 

Put it on the line above, and things will work.

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


Re: Executing other python code

2008-01-31 Thread Matthew_WARREN

Not a python solution, but if you look into how this was achieved with Java
it may give hints to the technique needed in python;

robocode, lets you specify your robot AI as java code. You design the AI,
download a bunch of AI's written by other people and the robocode engine
simulates a battle between them all.

It's exactly what you've described, with 'ships' replaced with 'robot
tanks'

http://robocode.sourceforge.net/developerWorks.php

I'm interested in the technique used to achieve it, and if it can be done
in python.

Matt.





  
   Internet 
  
   [EMAIL PROTECTED]


   To 

python-list   
   Sent by: 
   cc 
   python-list-bounces+matthew.warren=uk.bnpparibas.com@
  
   python.org   
  Subject 

Executing other python code   
   29/01/2008 00:18 
  

  

  

  

  

  

  




I'm working on a game, and I'd like players to be able to define thier
ships with scripts. Naturally, I don't want to give them the entire
program as thier romping ground. I would like to invoke a seperate
interpreter for these files, and give it a limited subset of the
functions in my game. What is the best way to achieve this effect?
--
http://mail.python.org/mailman/listinfo/python-list



This message and any attachments (the "message") is
intended solely for the addressees and is confidential. 
If you receive this message in error, please delete it and 
immediately notify the sender. Any use not in accord with 
its purpose, any dissemination or disclosure, either whole 
or partial, is prohibited except formal approval. The internet
can not guarantee the integrity of this message. 
BNP PARIBAS (and its subsidiaries) shall (will) not 
therefore be liable for the message if modified. 
Do not print this message unless it is necessary,
consider the environment.

-

Ce message et toutes les pieces jointes (ci-apres le 
"message") sont etablis a l'intention exclusive de ses 
destinataires et sont confidentiels. Si vous recevez ce 
message par erreur, merci de le detruire et d'en avertir 
immediatement l'expediteur. Toute utilisation de ce 
message non conforme a sa destination, toute diffusion 
ou toute publication, totale ou partielle, est interdite, sauf 
autorisation expresse. L'internet ne permettant pas 
d'assurer l'integrite de ce message, BNP PARIBAS (et ses
filiales) decline(nt) toute responsabilite au titre de ce 
message, dans l'hypothese ou il aurait ete modifie.
N'imprimez ce message que si necessaire,
pensez a l'environnement.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dl module

2008-01-31 Thread Thomas Pani
There's a Debian bug for python2.2 at [1]. You can't use dl on a 64bit
machine anyway, because sizeof(int) != sizeof(long).

Cheers
Thomas Pani

[1] http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=141681

Nicola Jean wrote:
> Hi everyone,
> I'm having a problem compiling Python2.4.4 on a 64 bits machine. It
> looks like I cannot compile the dl module.
> When I run test_dl.py I get the following error message:
> SystemError: module dl requires sizeof(int) == sizeof(long) == sizeof(char*)
> 
> Do I need to put any special flag when I run the configure script?
> Cheers
> N.Jean


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


Re: Dictionary Keys question

2008-01-31 Thread Dustan
On Jan 30, 7:02 pm, FireNWater <[EMAIL PROTECTED]> wrote:
> Thank you for the explanation. . . I think I now have a (foggy)
> understanding of hash tables.  It seems to be a way to create order
> (an index) out of disorder (random numbers or characters) behind the
> scenes. .

The key thing to realize is, quite simply, don't rely on order in a
dictionary. If you do, bad things can happen. The underlying
implementation is not important to know.

But if you really do want to know, let me correct you here, and give a
perhaps clearer explanation (if not, there's no need to read any
further):

The 'order' that your speaking of is not implemented by the hash
*table*, per se, but rather by the hash function, which returns an
integer (the hash code).

The hash table takes the hash code and calculates where in its list to
place the object (as explained before, using modulo to shrink the
integer into the range of the list). If multiple items end up in the
same list, they are placed into a kind of linked list, with each node
containing an object and pointing to the next. Of course, if too many
objects end up in the same bucket, the efficiency of finding an object
in the hash table reduces to that of a linked list, so hash functions
are generally implemented to ensure a unique number (or as unique as
possible) to every object.

Python dictionaries are hash tables that automatically grow as space
is needed. While integers in the range of the list will never change
location unless the list shrinks, larger hash codes can move around
quite apparently randomly. Space available is also a factor, as others
have found out on this list. The order of a dictionary *is*
determined, but factors involved in deciding that order may appear
surprisingly mundane, and certainly variable across runs of your
program.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: REALLY simple xml reader

2008-01-31 Thread Steve Holden
Diez B. Roggisch wrote:
> Ricardo Aráoz schrieb:
>> Diez B. Roggisch wrote:
>>> Ricardo Aráoz schrieb:
 Thanks Ivan, it seems a elegant API, and easy to use.
 I tried to play a little with it but unfortunately could not get it off
 the ground. I kept getting
>>> root = et.fromstring(doc)
 Traceback (most recent call last):
   File "", line 1, in 
   File "E:\Python25\lib\xml\etree\ElementTree.py", line 963, in XML
 parser.feed(text)
   File "E:\Python25\lib\xml\etree\ElementTree.py", line 1245, in feed
 self._parser.Parse(data, 0)
 ExpatError: XML or text declaration not at start of entity: line 2, column  0
>>> That's a problem in your XML not being XML. Has nothing to do with 
>>> element-tree - as one sees from the error-message "ExpatError". If you 
>>> show it to us, we might see why.
>>>
>> Sure,
>>
>> doc = """
>> 
> 
> It's not allowed to have a newline before the 
> 
> Put it on the line above, and things will work.
> 
If you don't think that looks pretty enough just escape the first 
newline in the string constant to have the parser ignore it:

doc = """\


regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Executing other python code

2008-01-31 Thread Guyon Morée

> One simple solution would be to forbid import statements in the
> scripts, to import the scripts as modules and inject whatever
> functions you want them to be able to use in the module's namespace.


how do you 'forbid' imports?


Guyon
http://gumuz.nl
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionary Keys question

2008-01-31 Thread Ben Finney
Dustan <[EMAIL PROTECTED]> writes:

> On Jan 30, 7:02 pm, FireNWater <[EMAIL PROTECTED]> wrote:
> > Thank you for the explanation. . . I think I now have a (foggy)
> > understanding of hash tables. It seems to be a way to create order
> > (an index) out of disorder (random numbers or characters) behind
> > the scenes. .
> 
> The key thing to realize is, quite simply, don't rely on order in a
> dictionary.

The poster to which you replied is using "order" as contrasted with
"disorder". Clearly dictionaries *do* have order that can be relied
upon.

I think you're using "order" in the colloquial manner; more accurate
would be to say "don't rely on *sequence* in a dictionary".

-- 
 \ "Our task must be to free ourselves from our prison by widening |
  `\our circle of compassion to embrace all humanity and the whole |
_o__)   of nature in its beauty." —Albert Einstein |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: REALLY simple xml reader

2008-01-31 Thread Ben Finney
Steve Holden <[EMAIL PROTECTED]> writes:

> Diez B. Roggisch wrote:
> > Ricardo Aráoz schrieb:
> >> doc = """
> >> 
> >
> > It's not allowed to have a newline before the 
> >
> > Put it on the line above, and things will work.
> >
> If you don't think that looks pretty enough just escape the first
> newline in the string constant to have the parser ignore it:

Quite apart from a human thinking it's pretty or not pretty, it's *not
valid XML* if the XML declaration isn't immediately at the start of
the document http://www.w3.org/TR/xml/#sec-prolog-dtd>. Many XML
parsers will (correctly) reject such a document.

> doc = """\
> 

This is fine.

-- 
 \ "True greatness is measured by how much freedom you give to |
  `\  others, not by how much you can coerce others to do what you |
_o__)   want." —Larry Wall |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: list traversal and remove

2008-01-31 Thread [EMAIL PROTECTED]
Thank you very much for the great anwsers. You guys save my sleep
today.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python noob SOS (any [former?] Perlheads out there?)

2008-01-31 Thread A.T.Hofkamp
On 2008-01-30, grflanagan <[EMAIL PROTECTED]> wrote:
> On Jan 29, 5:39 pm, kj <[EMAIL PROTECTED]> wrote:
> For command line options I get a long way with this:
>
> [code python]
> def _getargs():
> allargs = sys.argv[1:]
> args = []
> kwargs = {}
> key = None
> while allargs:
> arg = allargs.pop(0)
> if arg.startswith('--'):
> key, arg = arg.split('=', 1)
> key = key[2:]
> elif arg.startswith('-'):
> key = arg[1:]
> if not allargs or allargs[0].startswith('-'):
> allargs.insert(0, 'yes')
> continue
> if key is None:
> args.append(arg)
> else:
> kwargs[key] = arg
> key = None
> return args, kwargs
>
> ARGS, KWARGS = _getargs()
> [/code]

Have a look at getopt (old) or optparse (newer) packages in the Python library
instead.

Sincerely,
Albert
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: REALLY simple xml reader

2008-01-31 Thread Steve Holden
Ben Finney wrote:
> Steve Holden <[EMAIL PROTECTED]> writes:
> 
>> Diez B. Roggisch wrote:
>>> Ricardo Aráoz schrieb:
 doc = """
 
>>> It's not allowed to have a newline before the 
>>>
>>> Put it on the line above, and things will work.
>>>
>> If you don't think that looks pretty enough just escape the first
>> newline in the string constant to have the parser ignore it:
> 
> Quite apart from a human thinking it's pretty or not pretty, it's *not
> valid XML* if the XML declaration isn't immediately at the start of
> the document http://www.w3.org/TR/xml/#sec-prolog-dtd>. Many XML
> parsers will (correctly) reject such a document.
> 
>> doc = """\
>> 
> 
> This is fine.
> 
Sure. The only difference in "prettiness" I was referring to was the 
difference betwee

doc = """
...

and

doc = """\

...


In other words, Python source-code prettiness.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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

Fwd: Re: Problems installing Python on server

2008-01-31 Thread jim-on-linux



> > Also be careful and setup all the paths
> > that is required for compiling various
> > Python modules etc.
> >
> > On Jan 29, 8:28 am, Yansky
>
> <[EMAIL PROTECTED]> wrote:
> > > I asked my hosting company if they
> > > would upgrade Python on my server to
> > > the latest version. They responded
> > > with:
> > >
> > > "Sorry no. We tend to stick with what
> > > comes packaged with the unix
> > > distribution to ease maintenance
> > > issues.
> > >
> > > There is nothing stopping you from
> > > running your own version of python
> > > from within your own account. Download
> > > the source and compile it and install
> > > it into your own space. Adjust the
> > > fist line of your python scripts to
> > > reflect the location of YOUR python
> > > binary:
> > >
> > > #! /home/youraccount/yourlibs/python
> > >
> > > and you should be all set."
>
> Go to the ReadME file after you unpack
> python.
> Open  and look for   "Installing".
> Read the section, it explains how to
> install on the entire system and how to
> install locally.
> "Make altinstall"  is what you are looking
> for.
>
> jim-on-linux
> http:\\www.inqvista.com
>
> > > The build instructions for Python are:
> > > To start building right away (on
> > > UNIX): type "./configure" in the
> > > current directory and when it
> > > finishes, type "make". This creates an
> > > executable "./python"; to install in
> > > usr/local, first do "su root" and then
> > > "make install".
> > >
> > > The problem is, I don't have root
> > > access to the server so I can't do the
> > > "make install". I have ubuntu on my
> > > computer, but from what I understand I
> > > can't compile it on that and upload it
> > > because the server runs Red Had and
> > > the ./configure would have made it
> > > incompatible right?
> > >
> > > So how can I build Python without root
> > > access?

Will the "make install" make my Python the
 default one? If I want to install some
 Python modules, will I need to alter their
 installation as well or will it see my
 Python version as the right one to install
 too?

Cheers.

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


Bug/Patch: Problem with xml/__init__.py when using freeze.py

2008-01-31 Thread glomde
Hi,

I tried to do freeze.py for my script that uses ElementTree.
But got the this error:

  File "/usr/lib/python2.5/xml/__init__.py", line 45, in 
_xmlplus.__path__.extend(__path__)
  AttributeError: 'str' object has no attribute 'extend'

The reason seems that _xmlplus.__path__ is a string
after freeze.py.

I fixed it by changing the import to:

try:
_xmlplus.__path__.extend(__path__)
sys.modules[__name__] = _xmlplus
except AttributeError:
pass

This might not be the correct solution but it works for me. I do not
really now
how the __path__ variable works in a freezed environment.

Best regards
/T

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


Re: Fwd: Re: Problems installing Python on server

2008-01-31 Thread Steve Holden
jim-on-linux wrote:
> 
> 
>>> Also be careful and setup all the paths
>>> that is required for compiling various
>>> Python modules etc.
>>>
>>> On Jan 29, 8:28 am, Yansky
>> <[EMAIL PROTECTED]> wrote:
 I asked my hosting company if they
 would upgrade Python on my server to
 the latest version. They responded
 with:

 "Sorry no. We tend to stick with what
 comes packaged with the unix
 distribution to ease maintenance
 issues.

 There is nothing stopping you from
 running your own version of python
 from within your own account. Download
 the source and compile it and install
 it into your own space. Adjust the
 fist line of your python scripts to
 reflect the location of YOUR python
 binary:

 #! /home/youraccount/yourlibs/python

 and you should be all set."
>> Go to the ReadME file after you unpack
>> python.
>> Open  and look for   "Installing".
>> Read the section, it explains how to
>> install on the entire system and how to
>> install locally.
>> "Make altinstall"  is what you are looking
>> for.
>>
>> jim-on-linux
>> http:\\www.inqvista.com
>>
 The build instructions for Python are:
 To start building right away (on
 UNIX): type "./configure" in the
 current directory and when it
 finishes, type "make". This creates an
 executable "./python"; to install in
 usr/local, first do "su root" and then
 "make install".

 The problem is, I don't have root
 access to the server so I can't do the
 "make install". I have ubuntu on my
 computer, but from what I understand I
 can't compile it on that and upload it
 because the server runs Red Had and
 the ./configure would have made it
 incompatible right?

 So how can I build Python without root
 access?
> 
> Will the "make install" make my Python the
>  default one? If I want to install some
>  Python modules, will I need to alter their
>  installation as well or will it see my
>  Python version as the right one to install
>  too?
> 
The "default one"? That's just the one that runs when a user enters the

 python

command, right? "make install" will install Python wherever you told the 
configure utility to build it for. "make altinstall" is a convenience 
method that (IIRC) builds for /usr/local/bin.

Generally speaking when you install an extension or other module, 
nowadays you use the command

 python setup.py install

The installation takes place in whichever copy of Python runs setup.py, 
so with a "default" /usr/bin/python and an "alternate" 
/usr/local/bin/python, to install a module in the alternate you would run

 /usr/local/bin/python setup.py install

The same is true of a Python you have installed somewhere under your 
home directory.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Sine Wave Curve Fit Question

2008-01-31 Thread Helmut Jarausch
Mikael Olofsson wrote:
> Helmut Jarausch wrote:
>> Your model is  A*sin(omega*t+alpha)  where  A and alpha are sought.
>> Let T=(t_1,...,t_N)' and  Y=(y_1,..,y_N)'  your measurements (t_i,y_i)
>> ( ' denotes transposition )
>>
>> First, A*sin(omega*t+alpha) =
>>   A*cos(alpha)*sin(omega*t) + A*sin(alpha)*cos(omega*t) =
>>   B*sin(omega*t) + D*cos(omega*t)
>>
>> by setting  B=A*cos(alpha)  and  D=A*sin(alpha)
>>
>> Once, you have  B and D,  tan(alpha)= D/B   A=sqrt(B^2+D^2)
> 
> This is all very true, but the equation tan(alpha)=D/B may fool you. 

You're right: standard Python's math library missing the function arctan2.
But you can get it from numpy or numarray.
In case of doubt just compute the residuum with different possibilities .

> This may lead you to believe that alpha=arctan(D/B) is a solution, which 
> is not always the case. The point (B,D) may be in any of the four 
> quadrants of the plane. Assuming B!=0, the solutions to this equation 
> fall into the two classes
> 
> alpha = arctan(D/B) + 2*k*pi
> 
> and
> 
> alpha = arctan(D/B) + (2*k+1)*pi,
> 
> where k is an integer. The sign of B tells you which class gives you the 
> solution. If B is positive, the solutions are those in the first class. 
> If B is negative, the solutions are instead those in the second class. 
> Whithin the correct class, you may of course choose any alternative.
> 
> Then we have the case B=0. Then the sign of D determines alpha. If D is 
> positive, we have alpha=pi/2, and if D is negative, we have alpha=-pi/2.
> 
> Last if both B and D are zero, any alpha will do.
> 
> /MiO


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fwd: Re: Problems installing Python on server

2008-01-31 Thread jim-on-linux
On Thursday 31 January 2008 09:46, 
jim-on-linux wrote:
> > > Also be careful and setup all the
> > > paths that is required for compiling
> > > various Python modules etc.
> > >
> > > On Jan 29, 8:28 am, Yansky
> >
> > <[EMAIL PROTECTED]> wrote:
> > > > I asked my hosting company if they
> > > > would upgrade Python on my server to
> > > > the latest version. They responded
> > > > with:
> > > >
> > > > "Sorry no. We tend to stick with
> > > > what comes packaged with the unix
> > > > distribution to ease maintenance
> > > > issues.
> > > >
> > > > There is nothing stopping you from
> > > > running your own version of python
> > > > from within your own account.
> > > > Download the source and compile it
> > > > and install it into your own space.
> > > > Adjust the fist line of your python
> > > > scripts to reflect the location of
> > > > YOUR python binary:
> > > >
> > > > #! /home/youraccount/yourlibs/python
> > > >
> > > > and you should be all set."
> >
> > Go to the ReadME file after you unpack
> > python.
> > Open  and look for   "Installing".
> > Read the section, it explains how to
> > install on the entire system and how to
> > install locally.
> > "Make altinstall"  is what you are
> > looking for.
> >
> > jim-on-linux
> > http:\\www.inqvista.com
> >
> > > > The build instructions for Python
> > > > are: To start building right away
> > > > (on UNIX): type "./configure" in the
> > > > current directory and when it
> > > > finishes, type "make". This creates
> > > > an executable "./python"; to install
> > > > in usr/local, first do "su root" and
> > > > then "make install".
> > > >
> > > > The problem is, I don't have root
> > > > access to the server so I can't do
> > > > the "make install". I have ubuntu on
> > > > my computer, but from what I
> > > > understand I can't compile it on
> > > > that and upload it because the
> > > > server runs Red Had and the
> > > > ./configure would have made it
> > > > incompatible right?
> > > >
> > > > So how can I build Python without
> > > > root access?
>
> Will the "make install" make my Python the
>  default one? If I want to install some
>  Python modules, will I need to alter
> their installation as well or will it see
> my Python version as the right one to
> install too?
>
> Cheers.
>From the Readme file enclose with Python;

--
" If you have a previous installation of 
Python that you don't
want to replace yet, use

make altinstall  

This installs the same set of files as "make 
install" except it
doesn't create the hard link to 
"python" named "python" and
it doesn't install the manual page at all. "
--


I installed python 2.5 
using make altinstall by going to 
 
" /usr/local/lib " 
  unpacking, then using 
   make altinstall

Folder 2.5 is created.
To add modules, as I have added PIL to my 
system, I go to;

" /usr/local/lib/python2.5/site-packages "
where I installed PIL.

installing a py module in the site-packages 
folder is where I would install any package 
unless otherwise directed.  When upgrading 
you can go to the site directory and see 
what's in there, and what has to be added to
a new upgrade.

http:\\www.inqvista.com

jim-on-linux



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


Re: Python noob SOS (any [former?] Perlheads out there?)

2008-01-31 Thread grflanagan
On Jan 31, 2:56 pm, "A.T.Hofkamp" <[EMAIL PROTECTED]> wrote:
> On 2008-01-30, grflanagan <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Jan 29, 5:39 pm, kj <[EMAIL PROTECTED]> wrote:
> > For command line options I get a long way with this:
>
> > [code python]
> > def _getargs():
> > allargs = sys.argv[1:]
> > args = []
> > kwargs = {}
> > key = None
> > while allargs:
> > arg = allargs.pop(0)
> > if arg.startswith('--'):
> > key, arg = arg.split('=', 1)
> > key = key[2:]
> > elif arg.startswith('-'):
> > key = arg[1:]
> > if not allargs or allargs[0].startswith('-'):
> > allargs.insert(0, 'yes')
> > continue
> > if key is None:
> > args.append(arg)
> > else:
> > kwargs[key] = arg
> > key = None
> > return args, kwargs
>
> > ARGS, KWARGS = _getargs()
> > [/code]
>
> Have a look at getopt (old) or optparse (newer) packages in the Python library
> instead.
>
> Sincerely,
> Albert

Admittedly I haven't used either of them, but getopt and optparse have
always looked like too much work to me. 90% of the time my '_getargs'
does what I need, and when it's insufficient I go straight to a config
file (ConfigParser or ConfigObj).

Best Regards

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


Re: REALLY simple xml reader

2008-01-31 Thread Steven D'Aprano
On Fri, 01 Feb 2008 00:40:01 +1100, Ben Finney wrote:

> Quite apart from a human thinking it's pretty or not pretty, it's *not
> valid XML* if the XML declaration isn't immediately at the start of the
> document http://www.w3.org/TR/xml/#sec-prolog-dtd>. Many XML
> parsers will (correctly) reject such a document.

You know, I'd really like to know what the designers were thinking when 
they made this decision.

"You know Bob, XML just isn't hostile enough to anyone silly enough to 
believe it's 'human-readable'. What can we do to make it more hostile?"

"Well Fred, how about making the XML declaration completely optional, so 
you can leave it out and still be vald XML, but if you include it, you're 
not allowed to precede it with semantically neutral whitespace?"

"I take my hat off to you."


This is legal XML:

"""
Hello, world!"""

and so is this:

"""
 Hello, world!"""


but not this:

""" 
Hello, world!"""


You can't get this sort of stuff except out of a committee.



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


DEADLINE Feb 4: OSCON 2008 Call for Proposals

2008-01-31 Thread Aahz
The O'Reilly Open Source Convention (OSCON) is accepting proposals for
tutorials and presentations.  The submission period ends Feb 4.

OSCON 2008 will be in Portland, Oregon July 21-25.  For more information
and to submit a proposal, see

http://conferences.oreilly.com/oscon/
-- 
Aahz ([EMAIL PROTECTED])   <*> http://www.pythoncraft.com/

"All problems in computer science can be solved by another level of 
indirection."  --Butler Lampson
-- 
http://mail.python.org/mailman/listinfo/python-list


How to identify which numbers in a list are within each others' range

2008-01-31 Thread erikcw
Hi,

I have a list of numbers each with a +/- margin of error.  I need to
identify which ones overlab each other.

For example:
55 +/- 3
20 +/- 2
17 +/- 4
60 +/- 3

#base, max, min
list = [
(55, 58, 52),
(20, 22, 18),
(17, 21, 13),
(60, 63, 57),
]

In this example the range of list[0] overlaps the range of list[3] AND
list[1] overlaps list[2]

What is the best way to in python to identify the list items that
overlap and the items that don't overlap with any other.

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


Re: Sine Wave Curve Fit Question

2008-01-31 Thread Paul Rubin
Helmut Jarausch <[EMAIL PROTECTED]> writes:
> You're right: standard Python's math library missing the function arctan2.

It's math.atan2 .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problems installing Python on server

2008-01-31 Thread Paul Boddie
On 28 Jan, 22:28, Yansky <[EMAIL PROTECTED]> wrote:
> I asked my hosting company if they would upgrade Python on my server
> to the latest version. They responded with:
>
> "Sorry no. We tend to stick with what comes packaged with the unix
> distribution to ease maintenance issues.

Which version are they running, by the way?

> There is nothing stopping you from running your own version of python
> from within your own account. Download the source and compile it and
> install it into your own space. Adjust the fist line of your python
> scripts to reflect the location of YOUR python binary:
>
> #! /home/youraccount/yourlibs/python
>
> and you should be all set."

This sounds like reasonable advice, I suppose.

> The build instructions for Python are:
> To start building right away (on UNIX): type "./configure" in the
> current directory and when it finishes, type "make". This creates an
> executable "./python"; to install in usr/local, first do "su root" and
> then "make install".
>
> The problem is, I don't have root access to the server so I can't do
> the "make install".

I think that the "su root" stuff is just there in anticipation of
people trying to configure, build and install Python without thinking
too hard about it and then finding that they get lots of errors about
installing into places they don't have permissions for. If you're
installing into a different location, you only need to have
permissions to write to that location.

> I have ubuntu on my computer, but from what I understand I can't
> compile it on that and upload it because the server
> runs Red Had and the ./configure would have made it incompatible
> right?

If you have shell access on the hosting service and they have
compilers available, you can just do the build and install there.
Building on your own computer and having the executable work on the
server is likely to be more difficult due to the usual library
versioning issues that arise between distributions - it'd be
interesting to see if anyone can suggest a solution for this involving
the LSB tools.

> So how can I build Python without root access?

Something like this:

  mkdir /home/youraccount/apps # optional - see below
  ./configure --prefix=/home/youraccount/apps
  make
  make install

Here, the apps directory in your home directory will contain the usual
UNIX directory structure that you would otherwise see in /usr:
directories such as bin, lib, share (probably), and so on. You'll find
the python executable as /home/youraccount/apps/bin/python.

Some people like to mimic the full UNIX structure and have a usr
directory (either underneath or instead of the apps directory employed
above); others prefer to have the bin, lib (and other directories) in
their home directory (thus omitting the apps directory); you get to
choose. ;-)

I hope this helps!

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


Re: How to identify which numbers in a list are within each others' range

2008-01-31 Thread Paul Rubin
erikcw <[EMAIL PROTECTED]> writes:
> What is the best way to in python to identify the list items that
> overlap and the items that don't overlap with any other.

This sounds like a homework problem, so the first thing I'll suggest
is that you figure out exactly what it means for two of those
intervals to overlap.  That should let you write a simple program that
gets the right answer, but that can run slowly if the number of lists
gets large.  The next thing to do after that is figure out how to
speed it up, if necessary.  But do the first part first.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: REALLY simple xml reader

2008-01-31 Thread Diez B. Roggisch
Steven D'Aprano schrieb:
> On Fri, 01 Feb 2008 00:40:01 +1100, Ben Finney wrote:
> 
>> Quite apart from a human thinking it's pretty or not pretty, it's *not
>> valid XML* if the XML declaration isn't immediately at the start of the
>> document http://www.w3.org/TR/xml/#sec-prolog-dtd>. Many XML
>> parsers will (correctly) reject such a document.
> 
> You know, I'd really like to know what the designers were thinking when 
> they made this decision.
> 
> "You know Bob, XML just isn't hostile enough to anyone silly enough to 
> believe it's 'human-readable'. What can we do to make it more hostile?"
> 
> "Well Fred, how about making the XML declaration completely optional, so 
> you can leave it out and still be vald XML, but if you include it, you're 
> not allowed to precede it with semantically neutral whitespace?"
> 
> "I take my hat off to you."
> 
> 
> This is legal XML:
> 
> """
> Hello, world!"""
> 
> and so is this:
> 
> """
>  Hello, world!"""
> 
> 
> but not this:
> 
> """ 
> Hello, world!"""
> 
> 
> You can't get this sort of stuff except out of a committee.


do not forget to mention that trailing whitespace is evil as well!!!

And that for some reason some characters below \x20 aren't allowed in 
XML even if it's legal utf-8 - for what reason I'd really like to know...

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


very simple Genetic Algorithm completed

2008-01-31 Thread Matthew_WARREN
Hi,

I got some help with this from here, and there's been a little bit of
discussion around GA's recently, so thought I'd post up my likey slow and
clunky version of a GA that in essence just 'evolves' a solution to 'make a
sum that evaluates to n using */+-0123456789'  it's a really simple GA that
would be useful for someone who doesn't quite get GA's to look at.

I think it's simple enough to be fairly self explanatory.

to see it come up with evolved solutions to n=1000

>>>from quickga import *
>>>evolve()

I like playing with stuff like this. I'm going to use this little toy to
investigate how mutation rates/crossover gene length, pop size etc.. etc..
interact with each other. All completely unscientifically and for my own
bemusement.

One point, it's a good idea to keep mutationrate around 1000 - 1 with
genome and population sizes of say 50 - 100. Too low and you get no
solution as the mutations  mix up the genome too much for selection
pressure to work.


...as this actually does need to go as quick as it can, and if anyone feels
like it, I'd really appreciate  picking it over on the list for
optimization. I'm not too familiar with Pthon internals, nor programming
for speed in general.


from random import randint
from operator import itemgetter


genes=['+','-','*','/','0','1','2','3','4','5','6','7','8','9']
signs=['+','-','*','/']
digits=['1','2','3','4','5','6','7','8','9']

table = {"++": "+", "+-": "-", "+*": "+", "+/": "+",
"-+": "-", "--": "+", "-*": "-", "-/": "-",
"*+": "*", "**": "*", "*/": "*",
"/+": "/", "/*": "/", "//": "/",
 "+0":"+","*0":"*","-0":"-","/0":"/"} # keep out octal literals

def rationalise_signs(s):
"""Takes the genome string and twiddles it so eval() will work as
expected
"""
prev = ''
while s != prev:
prev=s
for z in ['+','-','*','/']:
s=s.replace(z+'0',z)
for key, value in table.items():
s = s.replace(key, value)
s=s.lstrip('0')
s=s.strip('+-*/')
return s



def generate(number,length):
"""Generate the initial population of genome strings
"""
population=[]
for i in range(number):
s=rationalise_signs(''.join([
genes[randint(0,len(genes))-1] for n in range(length) ]))
population.append(s)
return population


def floatify(intstring):#So eval() be floating point.
"""kludge to ensure eval() does floating point math
"""
prev=''
while intstring != prev:
prev=intstring
for sign in signs:
for digit in digits:

intstring=intstring.replace(digit+sign,digit+'.0'+sign)
return intstring

def express(population):
"""Get the 'expression' of the genome.
"""
expressed_population=[]
for individual in population:
s=floatify(individual)
expressed_population.append((individual,eval(s)))
return expressed_population

def fitness(expressed_population,fitvalue,tolerance):
"""Test the expressed genome for fitness
"""
population_fitness=[]
sumfitness=0
for expressed_individual in expressed_population:
individual,expression=expressed_individual
fitness=abs(fitvalue-expression)
sumfitness=sumfitness+fitness
population_fitness.append((individual,expression,fitness))
avgfitness=sumfitness/len(expressed_population)
return (population_fitness,avgfitness)



def get_fittest(population_fitness,pct,full=False):
"""Quick n dirty way of selecting - top n% fittest individuals
"""
population_fitness.sort(key=itemgetter(2))#sort on fitness
npct=(len(population_fitness)/100.0)*pct
if not full:
return [ n[0] for n in population_fitness[0:int(npct)] ]
else:
return population_fitness[0:int(npct)]


def mutate(individual,rate):
"""Does what it says on the tin. Mutates per gene
if rate is 1 mutatuion rate is 1 in 1 on avg
"""
newindividual=''
for gene in individual:
if randint(0,rate)==1:
newgene=genes[randint(0,14)-1]
newindividual=newindividual+newgene
else:
newindividual=newindividual+gene
return newindividual

def breed_new(individuals,number,mutationrate):#crossover with mutation
"""simple crossover of the two genomes around a point, then mutate
"""
newpopulation=[]
num_individuals=len(individuals)
while len(newpopulation)<=number:
lady=individuals[randint(0,num_individuals-1)]
man=individuals[randint(0,num_individuals-1)]
xpoint=randint(0,100)
x

Python for mobiles

2008-01-31 Thread [EMAIL PROTECTED]
Hello,

Is there any chance that i could compile python programs to java (.jar
[java bytecode]) so that i could run them with full support (no
interpreter) in a wireless device (talking about mobiles eg. nokia,
ericsson).
I am aware of jython, however i am not elegible of finding a proper
article claiming wheather this ability is provided or not.

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


Re: REALLY simple xml reader

2008-01-31 Thread Stefan Behnel
Hi,

Steven D'Aprano wrote:
> On Fri, 01 Feb 2008 00:40:01 +1100, Ben Finney wrote:
> 
>> Quite apart from a human thinking it's pretty or not pretty, it's *not
>> valid XML* if the XML declaration isn't immediately at the start of the
>> document http://www.w3.org/TR/xml/#sec-prolog-dtd>. Many XML
>> parsers will (correctly) reject such a document.
> 
> You know, I'd really like to know what the designers were thinking when 
> they made this decision.
[had a good laugh here]
> This is legal XML:
> 
> """
> Hello, world!"""
> 
> and so is this:
> 
> """
>  Hello, world!"""
> 
> 
> but not this:
> 
> """ 
> Hello, world!"""

It's actually not that stupid. When you leave out the declaration, then the
XML is UTF-8 encoded (by spec), so normal ASCII whitespace doesn't matter.
It's just like the declaration had come *before* the whitespace, at the very
beginning of the byte stream.

But if you add a declaration, then the encoding can change for the whole
document (including the declaration!), so you have to give the parser a chance
to actually parse the declaration. How is it supposed to know that the
whitespace before the declaration *is* whitespace before it knows the encoding?

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


Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-31 Thread Matthew_WARREN

Hmm, how does this fare??

for i in range(len(a)):
  if a[i]==99: a=a[:i]+a[i+1:]


I like following your guys code noodling.  I can come up with something
that does what it appears your doing, sometimes, but as to it's relevant
merits I havent a clue :)

matt.



   
   Internet 
   
   [EMAIL PROTECTED]


To 

python-list
   Sent by: 
cc 
   python-list-bounces+matthew.warren=uk.bnpparibas.com@
   
   python.org   
   Subject 
Re: 
Removal of element from list while 
   30/01/2008 13:04 
traversing causes the next  element to be  
skipped 
   

   

   

   

   

   

   

   




On Jan 30, 2008 6:57 AM, Arnaud Delobelle <[EMAIL PROTECTED]> wrote:
> Or one could use the trick of counting from the right (untested):
>
> n = len(a)
> for i, x in enumerate(a):
> if x == 99: del a[i-n]

Or one can put on his bellbottoms, horn-rimmed glasses, and wear a mullet:

i = 0
while i < len(a):
  if a[i] == 99:
del a[i]
  else:
i += 1

--
Neil Cerutti <[EMAIL PROTECTED]>
--
http://mail.python.org/mailman/listinfo/python-list



This message and any attachments (the "message") is
intended solely for the addressees and is confidential. 
If you receive this message in error, please delete it and 
immediately notify the sender. Any use not in accord with 
its purpose, any dissemination or disclosure, either whole 
or partial, is prohibited except formal approval. The internet
can not guarantee the integrity of this message. 
BNP PARIBAS (and its subsidiaries) shall (will) not 
therefore be liable for the message if modified. 
Do not print this message unless it is necessary,
consider the environment.

-

Ce message et toutes les pieces jointes (ci-apres le 
"message") sont etablis a l'intention exclusive de ses 
destinataires et sont confidentiels. Si vous recevez ce 
message par erreur, merci de le detruire et d'en avertir 
immediatement l'expediteur. Toute utilisation de ce 
message non conforme a sa destination, toute diffusion 
ou toute publication, totale ou partielle, est interdite, sauf 
autorisation expresse. L'internet ne permettant pas 
d'assurer l'integrite de ce message, BNP PARIBAS (et ses
filiales) decline(nt) toute responsabilite au titre de ce 
message, dans l'hypothese ou il aurait ete modifie.
N'imprimez ce message que si necessaire,
pensez a l'environnement.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: REALLY simple xml reader

2008-01-31 Thread Stefan Behnel
Stefan Behnel wrote:
> Steven D'Aprano wrote:
>> On Fri, 01 Feb 2008 00:40:01 +1100, Ben Finney wrote:
>>
>>> Quite apart from a human thinking it's pretty or not pretty, it's *not
>>> valid XML* if the XML declaration isn't immediately at the start of the
>>> document http://www.w3.org/TR/xml/#sec-prolog-dtd>. Many XML
>>> parsers will (correctly) reject such a document.
>> You know, I'd really like to know what the designers were thinking when 
>> they made this decision.
> [had a good laugh here]
>> This is legal XML:
>>
>> """
>> Hello, world!"""
>>
>> and so is this:
>>
>> """
>>  Hello, world!"""
>>
>>
>> but not this:
>>
>> """ 
>> Hello, world!"""
> 
> It's actually not that stupid. When you leave out the declaration, then the
> XML is UTF-8 encoded (by spec), so normal ASCII whitespace doesn't matter.

Sorry, strip the "ASCII" here. From the XML spec POV, your example

"""
 Hello, world!"""

is exactly equivalent to

"""
 Hello, world!"""

and whitespace between the declaration and the root element is allowed. It's
just not allowed *before* the declaration, which in your case was left out,
thus implying the default declaration.

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


Re: Fw: Undeliverable Message

2008-01-31 Thread Matthew_WARREN





> Heres the code
>
> def increment(digits,symbols):
>         overflow=True
>         digitpos=-1
>         while overflow and -digitpos<=len(digits):
>                 digitsymbolindex=symbols.index(digits[digitpos])
>                 if digitsymbolindex==len(symbols)-1:
>                         overflow=True
>                         digits[digitpos]=symbols[0]
>                         digitpos=digitpos-1
>                 else:
>                         digits[digitpos]=symbols[digitsymbolindex+1]
>                         overflow=False
>         return digits
>
> Now, this works. All good. It's nice and simple.  I'm just wondering how
> anyone else might approach it?

>I (not an expert at all) have only minor comments and one question:
>comments:
>why keep setting overflow to True, if you do not touch it will not
>change.


good point.


>digitpos -= 1 is easier to read in  my mind

it's just something ive done for years, so I read it perfectly fine. It's
cause I was programming for ages before I first ever saw -= or +=  (bbc
basic didnt have 'em ;P)


>question:
>Why first extract the indices and then compare (in your if statement),
>and
>why do you not just compare the symbols?

I put the index into digitsymbolindex because it is used in more than one
place in the code, and I'd rather have the result stored than recompute it
possibly twice.

have fun!
Matt.
--
http://mail.python.org/mailman/listinfo/python-list



This message and any attachments (the "message") is
intended solely for the addressees and is confidential. 
If you receive this message in error, please delete it and 
immediately notify the sender. Any use not in accord with 
its purpose, any dissemination or disclosure, either whole 
or partial, is prohibited except formal approval. The internet
can not guarantee the integrity of this message. 
BNP PARIBAS (and its subsidiaries) shall (will) not 
therefore be liable for the message if modified. 
Do not print this message unless it is necessary,
consider the environment.

-

Ce message et toutes les pieces jointes (ci-apres le 
"message") sont etablis a l'intention exclusive de ses 
destinataires et sont confidentiels. Si vous recevez ce 
message par erreur, merci de le detruire et d'en avertir 
immediatement l'expediteur. Toute utilisation de ce 
message non conforme a sa destination, toute diffusion 
ou toute publication, totale ou partielle, est interdite, sauf 
autorisation expresse. L'internet ne permettant pas 
d'assurer l'integrite de ce message, BNP PARIBAS (et ses
filiales) decline(nt) toute responsabilite au titre de ce 
message, dans l'hypothese ou il aurait ete modifie.
N'imprimez ce message que si necessaire,
pensez a l'environnement.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python for mobiles

2008-01-31 Thread Steve Holden
[EMAIL PROTECTED] wrote:
> Hello,
> 
> Is there any chance that i could compile python programs to java (.jar
> [java bytecode]) so that i could run them with full support (no
> interpreter) in a wireless device (talking about mobiles eg. nokia,
> ericsson).
> I am aware of jython, however i am not elegible of finding a proper
> article claiming wheather this ability is provided or not.
> 
> Thanks in advance!

A better solution would surely be to get a Nokia S60 'phone, for which 
there is a native Python implementation.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


PIL linux err

2008-01-31 Thread dzizes

Hi,

I'm trying to run simple .py on linux, which is using PIL. Below error
message which I receive:

  File "/usr/lib/python2.4/site-packages/PIL/Image.py", line 960, in
histogram
self.load()
  File "/usr/lib/python2.4/site-packages/PIL/ImageFile.py", line 180, in
load
d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
  File "/usr/lib/python2.4/site-packages/PIL/Image.py", line 375, in
_getdecoder
raise IOError("decoder %s not available" % decoder_name)
IOError: decoder jpeg not available

Do you know what might be the problem?

michal

-- 
View this message in context: 
http://www.nabble.com/PIL-linux-err-tp15211773p15211773.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


sending a handmade SOAP request

2008-01-31 Thread Bernard
Hey y'all,

Is there a way to POST a handmade SOAP request *without* using any
libraries like SOAPpy? I've been having some communication trouble
with a server using the new wse3 (http://www.microsoft.com/Downloads/
details.aspx?
familyid=018A09FD-3A74-43C5-8EC1-8D789091255D&displaylang=en).

they keep on sending back this strange error :
SOAPpy.Types.faultType: http://schemas.xmlsoap.org/ws/2004/08/addressing:Action for ultimate
recipient is required but not present in the message.>

We've tried using SOAPui (http://sourceforge.net/projects/soapui/) as
well to test the web service out. this little baby builds a proper
SOAP request based on the wsdl file. we keep on hitting that error
again & again...

so what is up with that?
-- 
http://mail.python.org/mailman/listinfo/python-list


pyExcelerator - Can I read and modify an existing Excel-WorkBook?

2008-01-31 Thread Stephen Brown
Yes indeed, pyExcelerator does support reading data from excel 
spreadsheets.  An example of how to do this is included in the file 
xls2csv-gerry.py  under the directory ... pyExcelerator/examples/tools  
Syntax is pretty straight forward.  extract_all = parse_xls(filename, CP 
= None)  where CP is the encoding format used within tht file.  Uses 
same codes that xlrd uses, ie 'cp437' = US English.

All you need is the "parse_xls" command and simply let it iterate 
through your spreadsheet file.
EXAMPLE:

fname = "D:\\Directory\\myfile.xls"# an example filename
for sheet_name, values in parse_xls(fname ): # parse_xls(arg) -- 
default encoding
print " name of sheet is = ", sheet_name,

Alternatively you can extract everything in one go...

extract_all = parse_xls(fname)

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


Re: Python for mobiles

2008-01-31 Thread Shawn Milochik

A better solution would surely be to get a Nokia S60 'phone, for which 
there is a native Python implementation.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/



Steve:

Do you know if the Nokia E60i phone has this capability? Also, is wxPython 
supported?
Or are you just knowledgeable about the S60?

Thanks,
Shawn

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


Re: PIL linux err

2008-01-31 Thread John Machin
On Feb 1, 5:41 am, dzizes <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm trying to run simple .py on linux, which is using PIL. Below error
> message which I receive:
>
>   File "/usr/lib/python2.4/site-packages/PIL/Image.py", line 960, in
> histogram
> self.load()
>   File "/usr/lib/python2.4/site-packages/PIL/ImageFile.py", line 180, in
> load
> d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
>   File "/usr/lib/python2.4/site-packages/PIL/Image.py", line 375, in
> _getdecoder
> raise IOError("decoder %s not available" % decoder_name)
> IOError: decoder jpeg not available
>
> Do you know what might be the problem?

Is your googler broken? See:
http://effbot.org/zone/pil-decoder-jpeg-not-available.htm


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


helper function in a class' namespace

2008-01-31 Thread Helmut Jarausch
Hi,

the following code works fine

def Helper(M) :
   return 'H>'+M

class A(object):
   def __init__(self,Msg) :
 print Helper(Msg)

B=A('ZZ')

but the Helper function is in the module's namespace.

I'd like to put it into class A's namespace.
Note, the Helper function doesn't need access to any instance attributes.

The first variant

class A(object):
   def Helper(M) :
 return 'H>'+M
   def __init__(self,Msg) :
 print Helper(Msg)

doesn't work since Python is looking for a global function Helper (why?)

The alternative

class A(object):
   def Helper(M) :
 return 'H>'+M
   def __init__(self,Msg) :
 print A.Helper(Msg)

doesn't work either since now the first argument to A.Helper must be 'A'.

So, isn't it possible to have a helper function in the namespace of a class
without (the overhead of) passing a 'self' or a 'cls' parameter?

Probably I'm hurt by my C++ history.

Many thanks for your help,
Helmut.



-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python for mobiles

2008-01-31 Thread Chris Mellon
On Jan 31, 2008 1:16 PM, Shawn Milochik <[EMAIL PROTECTED]> wrote:
>
> A better solution would surely be to get a Nokia S60 'phone, for which
> there is a native Python implementation.
>
> regards
>   Steve
> --
> Steve Holden+1 571 484 6266   +1 800 494 3119
> Holden Web LLC  http://www.holdenweb.com/
>
>
>
> Steve:
>
> Do you know if the Nokia E60i phone has this capability? Also, is wxPython 
> supported?
> Or are you just knowledgeable about the S60?
>


wxPython is not, but there are bindings for the S60s native gui
provided. You can find out it your phone is supported a pys60.sf.net.
-- 
http://mail.python.org/mailman/listinfo/python-list


best(fastest) way to send and get lists from files

2008-01-31 Thread Abrahams, Max

I've looked into pickle, dump, load, save, readlines(), etc.

Which is the best method? Fastest? My lists tend to be around a thousand to a 
million items.

Binary and text files are both okay, text would be preferred in general unless 
there's a significant speed boost from something binary.

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


Re: helper function in a class' namespace

2008-01-31 Thread Stargaming
On Thu, 31 Jan 2008 20:51:23 +0100, Helmut Jarausch wrote:

> Hi,
> 
> the following code works fine
> 
> def Helper(M) :
>return 'H>'+M

String concatenation is generally considered unpythonic, better use 
string interpolation::

'H> %s' % (M,)

> class A(object):
>def __init__(self,Msg) :
>  print Helper(Msg)
> 
> B=A('ZZ')

Watch out your names -- they could confuse other Python programmers 
introspecting your namespaces! Names bound to objects should generally be 
lowercase (argument ``msg``, object ``b``, argument ``m``, function 
``helper``). For details on what the Python community has agreed on is 
"good style," see the `Style Guide `_.

> but the Helper function is in the module's namespace.

Where's the problem? If it is "I don't want Helper to be visible", just 
use this convention: "Names beginning with an underscore are private. 
Better keep your fingers off."

> I'd like to put it into class A's namespace. Note, the Helper function
> doesn't need access to any instance attributes.
> 
> The first variant
> 
> class A(object):
>def Helper(M) :
>  return 'H>'+M
>def __init__(self,Msg) :
>  print Helper(Msg)
> 
> doesn't work since Python is looking for a global function Helper (why?)

Because in the scope of the ``__init__`` function, the name ``Helper`` is 
not bound. It then jumps out to the global scope.

> The alternative
> 
> class A(object):
>def Helper(M) :
>  return 'H>'+M
>def __init__(self,Msg) :
>  print A.Helper(Msg)
> 
> doesn't work either since now the first argument to A.Helper must be
> 'A'.
> 
> So, isn't it possible to have a helper function in the namespace of a
> class without (the overhead of) passing a 'self' or a 'cls' parameter?

Of course it is! You can decorate certain functions with the 
`staticmethod `_ 
wrapper, which will do exactly what you wanted::

class A(object):
@staticmethod
def helper(m):
return 'H>%s' % (m,)
def __init__(self, msg):
print A.helper(msg) # or self.helper


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


Re: sending a handmade SOAP request

2008-01-31 Thread Van Gale
On Thu, 31 Jan 2008 11:03:26 -0800, Bernard wrote:

> Hey y'all,
> 
> Is there a way to POST a handmade SOAP request *without* using any
> libraries like SOAPpy?

Yes, it's quite easy to SOAP by hand.

I use Oren Tirosh's ElementBuilder class (on top of lxml instead of 
ElementTree) to build the SOAP request and the xpath capabilities in lxml 
to pull out the data I need from the response.

http://www.tothink.com/python/ElementBuilder/
http://codespeak.net/lxml/

An incomplete example for contructing a request looks something like this:

body = Element('soap:Envelope',
 { 'xmlns:soap': nss['soap']},
 Element('soap:Header'), Element('soap:Body',
   { 'xmlns:msgs': nss['msgs'] },
   Element('msgs:login',
 Element('msgs:passport',
   { 'xmlns:core': nss['core'] },
 Element('core:password', password),
 Element('core:account', account)

I use httplib2 for sending the HTTP requests:

http://code.google.com/p/httplib2/

Incomplete example:

headers['SOAPAction'] = action
headers['Content-length'] = str(len(etree.tostring(body)))
response, content = self._client.request(
   self.ns_uri, "POST",
   body=etree.tostring(body), headers=self._headers)
if response.status == 500 and not \
(response["content-type"].startswith("text/xml") and \
len(content) > 0):
raise HTTPError(response.status, content)
if response.status not in (200, 500):
raise HTTPError(response.status, content)
doc = etree.parse(StringIO(content))
if response.status == 500:
faultstring = doc.findtext(".//faultstring")
raise HTTPError(response.status, faultstring)

Now it's just a matter of using xpath expressions to dig into the "doc" 
structure for the bits you need.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PIL linux err

2008-01-31 Thread Paul McNett
dzizes wrote:

> I'm trying to run simple .py on linux, which is using PIL. Below error
> message which I receive:
> 
> IOError: decoder jpeg not available
> 
> Do you know what might be the problem?


No, but google seems to:

http://effbot.org/zone/pil-decoder-jpeg-not-available.htm



Paul

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


RE: Design question - Sharing of single object by multiple processes

2008-01-31 Thread Mike D
Steve,

Thanks for the response. My question really comes down to, as you suggested,
premature optimization.

It is more for my own understanding than a current practical use.

If an object is loaded into memory and other threads(or processes) can
recieve a pointer to this location, would this not be more efficient than to
load a new one for every unique request? Wouldn't a method such as this
prevent bottle necks in a read heavy environment?

Perhaps some kind of caching implementation is more along the lines I'm
thinking on?


> I want to have an object in memory. This object will be loaded on
> application start up and will contain a list of objects. These will be
> obtained from (most likely) the file system.
>
>>So "application startup" is different from "process startup"? Your
>>application is a group of co-operating processes? Really?

I think I should have said process start up, for example a long running
process that can supply the object to new ones, loaded on webserver start
up.

I appreciate comments, It is purely so I can understand python and program
design better.

Regards,

Mike



On Feb 1, 2008 12:09 AM, Steve Holden <[EMAIL PROTECTED]> wrote:

> Mike D wrote:
> > Hello, I've just picked up the Python language and am really enjoying
> it.
> >
> > I've just signed up to this mailing list and I'm looking forward to
> > taking part in some discussions.
> >
> > My first post is a question I've been pondering for the last couple of
> days:
> >
> > For relatively static data (such as a list), is it possible to load a
> > single instance and have many python processes access it?
> >
> First there's the problem of having multiple processes access any kind
> of shared resource. So your question makes me wonder whether you mean
> processes, or threads. Are you *sure* you mean processes?
>
> > I want to have an object in memory. This object will be loaded on
> > application start up and will contain a list of objects. These will be
> > obtained from (most likely) the file system.
> >
> So "application startup" is different from "process startup"? Your
> application is a group of co-operating processes? Really?
>
> > My reasoning is: I want to prevent a file system or database fetch each
> > time as it seems unnecessary.
> >
> It might also seem unnecessary to start the Python interpreter for each
> process, but you are going to have to ...
>
> > Is this possible? In Java I'm pretty sure this could implemented with an
> > object factory and static methods. My understanding is that python
> > invokes a separate process for each request however.
> >
> Your understanding is clearly informed by some information you have
> omitted to share with us.
>
> > If possible, what would be a good way of providing a new structure as it
> > is updated.
> >
> > If anyone could give me some advice or point me in the correct direction
> > I'd really appreciate it.
> >
> > Thanks in advance,
> >
> More answers, please. There are mechanisms such as Pyro (Python remote
> objects) that allow inter-process method calls, but I am far from
> convinced that's what you really want (or need, anyway). Perhaps yo
> could let us know a little more about what it really is you are trying
> to do, and convince me, at least, that this isn't just a case of
> premature optimization.
>
> regards
>  Steve
> --
> Steve Holden+1 571 484 6266   +1 800 494 3119
> Holden Web LLC  http://www.holdenweb.com/
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Dictionary Keys question

2008-01-31 Thread FireNWater
On Jan 31, 4:39 am, Dustan <[EMAIL PROTECTED]> wrote:
> On Jan 30, 7:02 pm, FireNWater <[EMAIL PROTECTED]> wrote:
>
> > Thank you for the explanation. . . I think I now have a (foggy)
> > understanding of hash tables.  It seems to be a way to create order

>
> The 'order' that your speaking of is not implemented by the hash
> *table*, per se, but rather by the hash function, which returns an
> integer (the hash code).
>
Dustan,

Yes, I think I understand it now. . . the order I was referring to is
provided "behind the scenes" by the return values of the hash
function.

Thanks to everyone with their explanations!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sending a handmade SOAP request

2008-01-31 Thread Stefan Behnel
Hi,

Bernard wrote:
> Is there a way to POST a handmade SOAP request *without* using any
> libraries like SOAPpy?

This might help:

http://effbot.org/zone/element-soap.htm

I didn't try, but it should also work with lxml - which might make a couple of
things a little easier, as it gives you XPath and (a lot of its) friends. For
request generation, this little feature might also be of interest:

http://codespeak.net/lxml/dev/tutorial.html#the-e-factory

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


Re: REALLY simple xml reader

2008-01-31 Thread Ben Finney
Steven D'Aprano <[EMAIL PROTECTED]> writes:

> On Fri, 01 Feb 2008 00:40:01 +1100, Ben Finney wrote:
> 
> > Quite apart from a human thinking it's pretty or not pretty, it's *not
> > valid XML* if the XML declaration isn't immediately at the start of the
> > document http://www.w3.org/TR/xml/#sec-prolog-dtd>. Many XML
> > parsers will (correctly) reject such a document.
> 
> You know, I'd really like to know what the designers were thinking when 
> they made this decision.

Probably much the same that the designers of the Unix shebang ("#!")
or countless other "figure out whether the bitstream is a specific
type" were thinking:

It's better to be as precise as possible so that failure can be
unambiguous, than to have more-complex parsing rules that lead to
ambiguity in implementation.

Also, for XML documents, they were probably thinking that the
documents will be machine-generated most of the time. As far as I can
tell, they were right in that.

Given that, I think the choice of precise parsing rules that are
simple to implement correctly (even if the rules themselves are
necessarily complex) is a better one.

-- 
 \   "Those who will not reason, are bigots, those who cannot, are |
  `\fools, and those who dare not, are slaves." —"Lord" George |
_o__)Gordon Noel Byron |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: sending a handmade SOAP request

2008-01-31 Thread Bernard
On 31 jan, 15:23, Van Gale <[EMAIL PROTECTED]> wrote:
> Yes, it's quite easy to SOAP by hand.
>
> I use Oren Tirosh's ElementBuilder class (on top of lxml instead of
> ElementTree) to build the SOAP request and the xpath capabilities in lxml
> to pull out the data I need from the response.
>
> http://www.tothink.com/python/ElementBuilder/http://codespeak.net/lxml/
>
> An incomplete example for contructing a request looks something like this:
>
> body = Element('soap:Envelope',
>  { 'xmlns:soap': nss['soap']},
>  Element('soap:Header'), Element('soap:Body',
>{ 'xmlns:msgs': nss['msgs'] },
>Element('msgs:login',
>  Element('msgs:passport',
>{ 'xmlns:core': nss['core'] },
>  Element('core:password', password),
>  Element('core:account', account)
>
> I use httplib2 for sending the HTTP requests:
>
> http://code.google.com/p/httplib2/
>
> Incomplete example:
>
> headers['SOAPAction'] = action
> headers['Content-length'] = str(len(etree.tostring(body)))
> response, content = self._client.request(
>self.ns_uri, "POST",
>body=etree.tostring(body), headers=self._headers)
> if response.status == 500 and not \
> (response["content-type"].startswith("text/xml") and \
> len(content) > 0):
> raise HTTPError(response.status, content)
> if response.status not in (200, 500):
> raise HTTPError(response.status, content)
> doc = etree.parse(StringIO(content))
> if response.status == 500:
> faultstring = doc.findtext(".//faultstring")
> raise HTTPError(response.status, faultstring)
>
> Now it's just a matter of using xpath expressions to dig into the "doc"
> structure for the bits you need.

oh my that is quite the handy answer Van Gal! I'll try it out right
now. thanks a bunch man!
-- 
http://mail.python.org/mailman/listinfo/python-list


Naive idiom questions

2008-01-31 Thread Terran Melconian
* Why are there no real mutable strings available?

I found MutableString in UserString, but further research indicates
that it is horribly inefficient and actually just wraps immutable
strings for the implementation.

I want to be able to accumulate a string with +=, not by going
through an intermediate list and then doing ''.join(), because I
think the latter is ugly.  There are also times when I'd like to use
the string as a modifiable buffer.

Is the python idiom that this is actually the Right Thing for
reasons I'm not seeing?  Is there a fundamental reason it would be
hard to implement a mutable string in cpython?

* What's the best way to initialize a list of lists?

I keep wanting to do this:

l=[[None]*5]*5

as do many other Python novices, but of course it quickly becomes
apparent why this is a bad idea as soon as one modifies a value.
The best I've found is:

l=[[None]*5 for i in range(5)]

I don't particularly like it, though.  It bothers me to have to
explain list comprehensions, which are a moderately advanced feature
conceptually, just to initialize an array.  We could get around this
if we had a defaultlist, but we don't.  Is there a more elegant
solution here?

* Is there a way to get headings in docstrings?

I want to create my own sections, like "OVERVIEW", "EXAMPLES",
"AUTHORS", "BUGS", etc.  I can't figure out any way to do this.  In
perldoc, I can easily use =head1, but I can't figure out the Python
equivalent.  At first I thought I could use (re)structuredtext, but
then it became apparent that pydoc(1) does not parse any of this
markup.  Am I missing something, or is it just not possible to
achieve this effect other than by writing separate, independent
manpages?

Thanks for any suggestions or thoughts.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Naive idiom questions

2008-01-31 Thread Grant Edwards
On 2008-01-31, Terran Melconian <[EMAIL PROTECTED]> wrote:

> * Why are there no real mutable strings available?

[...]

> I want to be able to accumulate a string with +=, not by going
> through an intermediate list and then doing ''.join(),

So what's stopping you?

>>> s = "one"
>>> s += " two"
>>> s
'one two'
>>> 

> because I think the latter is ugly.

Then don't do it. :)

> There are also times when I'd like to use the string as a
> modifiable buffer.

That would be an array of characters or bytes:

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

> Is the python idiom that this is actually the Right Thing for
> reasons I'm not seeing?

I'm not sure what you're asking.  AFAIK, the main reason that
strings are immutable is so they can be used as dict keys.

> Is there a fundamental reason it would be hard to
> implement a mutable string in cpython?

What problem would a mutable string solve that an array of
chars won't?

> * What's the best way to initialize a list of lists?

Hmm. I guess I never need to do that.

> * Is there a way to get headings in docstrings?

Docstrings?  Real Men Don't Write Docstrings!

-- 
Grant Edwards   grante Yow! Now, let's SEND OUT
  at   for QUICHE!!
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Design question - Sharing of single object by multiple processes

2008-01-31 Thread Steve Holden
Mike D wrote:
> Steve,
> 
> Thanks for the response. My question really comes down to, as you 
> suggested, premature optimization.
> 
> It is more for my own understanding than a current practical use.
> 
> If an object is loaded into memory and other threads(or processes) can 
> recieve a pointer to this location, would this not be more efficient 
> than to load a new one for every unique request? Wouldn't a method such 
> as this prevent bottle necks in a read heavy environment?
> 
In theory, yes. In practice there are problems, since modern operating 
systems are explicitly designed to place barriers between the address 
spaces of different processes.

Even supposing you could access another Python process's space freely 
you would then have issues like:
  * does a reference from a foreign process add to an
object's reference count?
  * if so, what happens if the foreign process terminates
without decrementing the reference count
  * otherwise waht happens if the owning process disposes of
the object while the foreign process stil wants to refer
to it.

I don't wish to be unkind, but these are well-known issues of 
inter-process information sharing, and while superficial solutions can 
seem easy, the more you think about and work on them the more obvious it 
becomes that these are hard problems in the general case.

Which will hopefully at least encourage you that you are addressing the 
real issues of computing, even though there's a lot to do.

 > [...]

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


char string 2 hex string

2008-01-31 Thread Antonio Chay
Hello!

I need to transform a string from a file into a hexadecimal
representation, for example:

"AAA" should be "414141"

With perl I do this with:

unpack("H*","AAA")

And with python I got this:

"".join([str(hex(ord(x)))[2:] for x in "AAA"])

But seems a little "weird" for me.

Is there another way?
Thanks in advance!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: char string 2 hex string

2008-01-31 Thread Paul Rubin
Antonio Chay <[EMAIL PROTECTED]> writes:
> "AAA" should be "414141"

'AAA'.encode('hex')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Naive idiom questions

2008-01-31 Thread Bjoern Schliessmann
Terran Melconian wrote:
> * Why are there no real mutable strings available?
> 
> I found MutableString in UserString, but further research
> indicates that it is horribly inefficient and actually just
> wraps immutable strings for the implementation.

Did you measure such impact on your application?

Also see http://www.skymind.com/~ocrow/python_string/
 
> I want to be able to accumulate a string with +=, not by going
> through an intermediate list and then doing ''.join(), because
> I think the latter is ugly.  

>>> yummy = "ham"
>>> yummy += "eggs"
>>> yummy
'hameggs'

> There are also times when I'd like to use the string as a
> modifiable buffer. 

Use a list instead. When done with modifications you may concatenate
its contents to a string using "".join(my_list).

> * What's the best way to initialize a list of lists?
> [...]
> l=[[None]*5 for i in range(5)]
> 
> I don't particularly like it, though.  It bothers me to have
> to explain list comprehensions, which are a moderately
> advanced feature conceptually, just to initialize an array. 

Look at "lower level" HLLs. In C++, you'd have to use

int my_array[5][5];

for (int i=0; i<5; ++i)
for (int j=0; j<5; j++)
my_array[i][j] = -1;

Personally, I like list comprehensions much better.

Regards,


Björn

-- 
BOFH excuse #254:

Interference from lunar radiation

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


Re: pyExcelerator - Can I read and modify an existing Excel-WorkBook?

2008-01-31 Thread John Machin
On Feb 1, 4:37 am, Stephen Brown <[EMAIL PROTECTED]> wrote:
> Yes indeed, pyExcelerator does support reading data from excel
> spreadsheets.

I presume this is an orphaned and belated reply to the 3-message
thread in July 2006 with the same subject.

> An example of how to do this is included in the file
> xls2csv-gerry.py  under the directory ... pyExcelerator/examples/tools
> Syntax is pretty straight forward.  extract_all = parse_xls(filename, CP
> = None)  where CP is the encoding format used within tht file.

The OP was aware of pyExcelerator's parse_xls, but required formatting
information:
"""
To me it seems, that pyExcelerator does not support the reading for
modification of an Excel-sheet. It allows only the "parse_xls" but I
would like to keep the "formatting" in the template.
"""

> Uses
> same codes that xlrd uses, ie 'cp437' = US English.

An interesting way of describing it. It is *not* restricted to being
a "codepage". The arg can be any Python-supported encoding that can be
passed to str.decode to convert internal 8-bit strings to unicode.

>
> All you need is the "parse_xls" command and simply let it iterate
> through your spreadsheet file.
[snip]

Current situation:

I am (sporadically) maintaining xlwt, a fork of pyExcelerator which
among other things fixes bugs and enables use with Python 2.3. It is
available from https://secure.simplistix.co.uk/svn/xlwt/trunk

xlwt.parse_xls is the same as pyExcelerator.parse_xls and thus has the
same deficiencies e.g. it reports a date as a floating-point number of
days since some more-or-less-fixed epoch and provides no indication
that the item should be interpreted as a date. It will not be
maintained, and is severely deprecated -- use xlrd instead.

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


Re: Naive idiom questions

2008-01-31 Thread Paul Rubin
Terran Melconian <[EMAIL PROTECTED]> writes:
> I want to be able to accumulate a string with +=, not by going
> through an intermediate list and then doing ''.join(), because I
> think the latter is ugly.  There are also times when I'd like to use
> the string as a modifiable buffer.

See the StringIO, cStringIO, and array modules.  

>   l=[[None]*5 for i in range(5)]

This is the usual way.

> * Is there a way to get headings in docstrings?

I think this is normally not done.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to identify which numbers in a list are within each others' range

2008-01-31 Thread Arnaud Delobelle
On Jan 31, 4:12 pm, erikcw <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I have a list of numbers each with a +/- margin of error.  I need to
> identify which ones overlab each other.
>
> For example:
> 55 +/- 3
> 20 +/- 2
> 17 +/- 4
> 60 +/- 3
>
> #base, max, min
> list = [
> (55, 58, 52),
> (20, 22, 18),
> (17, 21, 13),
> (60, 63, 57),
> ]
>
> In this example the range of list[0] overlaps the range of list[3] AND
> list[1] overlaps list[2]
>
> What is the best way to in python to identify the list items that
> overlap and the items that don't overlap with any other.

This is definitely the best way:

===
lst = [
(55, 58, 52),
(20, 22, 18),
(17, 21, 13),
(60, 63, 57),
]

from itertools import chain

def overlaps(lst):
bounds = chain(*(((x[1],i), (x[2], i)) for i,x in enumerate(lst)))
inside = {}
for x, i in sorted(bounds):
if inside.pop(i, None) is None:
for j, y in inside.iteritems():
if y != x: yield i, j
inside[i] = x

==
>>> list(overlaps(lst))
[(1, 2), (3, 0)]

--
Arnaud

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


Re: Design question - Sharing of single object by multiple processes

2008-01-31 Thread Mike D
Steve,

You raise some very good (and obvious) issues I did'nt consider. I'll look
further into this sort of implementation as I'm quite interested.

I suppose a compromise could be to load the objects from a pickle, that may
have issues in terms of updating the pickle perhaps, though it would be much
safer.

I'll continue to investigate, thanks for your input.

On Feb 1, 2008 11:00 AM, Steve Holden <[EMAIL PROTECTED]> wrote:

> Mike D wrote:
> > Steve,
> >
> > Thanks for the response. My question really comes down to, as you
> > suggested, premature optimization.
> >
> > It is more for my own understanding than a current practical use.
> >
> > If an object is loaded into memory and other threads(or processes) can
> > recieve a pointer to this location, would this not be more efficient
> > than to load a new one for every unique request? Wouldn't a method such
> > as this prevent bottle necks in a read heavy environment?
> >
> In theory, yes. In practice there are problems, since modern operating
> systems are explicitly designed to place barriers between the address
> spaces of different processes.
>
> Even supposing you could access another Python process's space freely
> you would then have issues like:
>  * does a reference from a foreign process add to an
>object's reference count?
>  * if so, what happens if the foreign process terminates
>without decrementing the reference count
>  * otherwise waht happens if the owning process disposes of
>the object while the foreign process stil wants to refer
>to it.
>
> I don't wish to be unkind, but these are well-known issues of
> inter-process information sharing, and while superficial solutions can
> seem easy, the more you think about and work on them the more obvious it
> becomes that these are hard problems in the general case.
>
> Which will hopefully at least encourage you that you are addressing the
> real issues of computing, even though there's a lot to do.
>
>  > [...]
>
> regards
>  Steve
> --
> Steve Holden+1 571 484 6266   +1 800 494 3119
> Holden Web LLC  http://www.holdenweb.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: best(fastest) way to send and get lists from files

2008-01-31 Thread Steve Holden
Abrahams, Max wrote:
> I've looked into pickle, dump, load, save, readlines(), etc.
> 
> Which is the best method? Fastest? My lists tend to be around a thousand to a 
> million items.
> 
> Binary and text files are both okay, text would be preferred in general 
> unless there's a significant speed boost from something binary.
> 
> thanks

benchmarks are the only reliable way to get this information for your 
particular tasks.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: best(fastest) way to send and get lists from files

2008-01-31 Thread Yu-Xi Lim
Abrahams, Max wrote:
> I've looked into pickle, dump, load, save, readlines(), etc.
> 
> Which is the best method? Fastest? My lists tend to be around a thousand to a 
> million items.
> 
> Binary and text files are both okay, text would be preferred in general 
> unless there's a significant speed boost from something binary.
> 
> thanks

1) Why don't you time them with the timeit module? 
http://docs.python.org/lib/module-timeit.html

Results will vary with the specific data you have, and your hardware 
speed, but if it's a lot of data, it's most likely going to be the 
latter that's the bottleneck. A compact binary format will help 
alleviate this.

If you're reading a lot of data into memory, you might have to deal with 
your OS swap/virtual memory.

2) "Best" depends on what your data is and what you're doing with it.

Are you reinventing a flat-file database? There are better solutions for 
databases.

If you're just reformatting data to pass to another program, say, for 
scientific computation, the portability may be more of an issue. Number 
crunching the resultant data may be even more time consuming such that 
the time spent writing/reading it becomes insignificant.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: helper function in a class' namespace

2008-01-31 Thread Arnaud Delobelle
On Jan 31, 10:39 pm, Steven D'Aprano <[EMAIL PROTECTED]
cybersource.com.au> wrote:
> On Thu, 31 Jan 2008 20:05:44 +, Stargaming wrote:
> > String concatenation is generally considered unpythonic, better use
> > string interpolation::
>
> >     'H> %s' % (M,)
[...]
> Also, the tuple above is totally unnecessary. 'H> %s' % M will work fine.

...except if M is a tuple:

>>> M = 'spam', 'eggs'
>>> 'H> %s' % (M,)
"H> ('spam', 'eggs')"
>>> 'H> %s' % M
Traceback (most recent call last):
  File "", line 1, in 
TypeError: not all arguments converted during string formatting


Pedantically yours,

--
Arnaud

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


Re: How to identify which numbers in a list are within each others' range

2008-01-31 Thread [EMAIL PROTECTED]
On Jan 31, 8:12 am, erikcw <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I have a list of numbers each with a +/- margin of error.  I need to
> identify which ones overlab each other.
>
> For example:
> 55 +/- 3
> 20 +/- 2
> 17 +/- 4
> 60 +/- 3
>
> #base, max, min
> list = [
> (55, 58, 52),
> (20, 22, 18),
> (17, 21, 13),
> (60, 63, 57),
> ]
>
> In this example the range of list[0] overlaps the range of list[3] AND
> list[1] overlaps list[2]
>
> What is the best way to in python to identify the list items that
> overlap and the items that don't overlap with any other.
>


One way would be to use sets and check for intersection:

for idx, s in enumerate(mysets):
for next_idx, next_s in enumerate(mysets[idx+1:]):
if s.intersection(next_s):
print "mylist[%d] and mylist[%d] intersect" % (
idx, idx + next_idx + 1 )


--
Hope this helps,
Steve




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


Re: helper function in a class' namespace

2008-01-31 Thread Arnaud Delobelle
On Jan 31, 8:05 pm, Stargaming <[EMAIL PROTECTED]> wrote:
[...]
> > class A(object):
> >    def Helper(M) :
> >      return 'H>'+M
> >    def __init__(self,Msg) :
> >      print Helper(Msg)
>
> > doesn't work since Python is looking for a global function Helper (why?)
>
> Because in the scope of the ``__init__`` function, the name ``Helper`` is
> not bound. It then jumps out to the global scope.

But it is 'Helper' bound in the scope of the *definition* of __init__,
hence you *could* write:

>>> class A(object):
... def _helper(x): return '<<%s>>' % x
... def __init__(self, msg, _helper=_helper):
... print _helper(msg)
... del _helper
...
>>> A('spam')
<>
<__main__.A object at 0x70170>

Confusingly yours,

--
Arnaud

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


Re: REALLY simple xml reader

2008-01-31 Thread Ricardo Aráoz
Diez B. Roggisch wrote:
> Ricardo Aráoz schrieb:
>> Diez B. Roggisch wrote:
>>> Ricardo Aráoz schrieb:
 Thanks Ivan, it seems a elegant API, and easy to use.
 I tried to play a little with it but unfortunately could not get it off
 the ground. I kept getting
>>> root = et.fromstring(doc)
 Traceback (most recent call last):
   File "", line 1, in 
   File "E:\Python25\lib\xml\etree\ElementTree.py", line 963, in XML
 parser.feed(text)
   File "E:\Python25\lib\xml\etree\ElementTree.py", line 1245, in feed
 self._parser.Parse(data, 0)
 ExpatError: XML or text declaration not at start of entity: line 2, column  0
>>> That's a problem in your XML not being XML. Has nothing to do with 
>>> element-tree - as one sees from the error-message "ExpatError". If you 
>>> show it to us, we might see why.
>>>
>> Sure,
>>
>> doc = """
>> 
> 
> It's not allowed to have a newline before the 
> 
> Put it on the line above, and things will work.
> 
> Diez

Worked ok. Thanks a lot to you all, I'm not using it right now but I've
had a taste and now know where to look and how to start.
Cheers


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


Re: helper function in a class' namespace

2008-01-31 Thread Steven D'Aprano
On Thu, 31 Jan 2008 20:05:44 +, Stargaming wrote:

> String concatenation is generally considered unpythonic, better use
> string interpolation::
> 
> 'H> %s' % (M,)

String concatenation is significantly faster than string interpolation.

>>> import timeit
>>> timeit.Timer("'H> %s' % M", "M = 'abcdef'").repeat()
[1.3407769203186035, 0.69128704071044922, 0.56362509727478027]
>>> timeit.Timer("'H> ' + M", "M = 'abcdef'").repeat()
[0.69647812843322754, 0.69620108604431152, 0.65643787384033203]

The danger with string concatenation comes from building up a string 
piece by piece: even though a single + is faster than a single %, a dozen 
concats will likely be MUCH slower than a single &.

Also, the tuple above is totally unnecessary. 'H> %s' % M will work fine.


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


Re: REALLY simple xml reader

2008-01-31 Thread Ivan Illarionov
> Also, for XML documents, they were probably thinking that the
> documents will be machine-generated most of the time. As far as I can
> tell, they were right in that.

If anybody has to deal with human-generated XML/HTML in Python it may
be better to use something like http://www.crummy.com/software/BeautifulSoup/

Bad XML markup is part of our life and there are great tools for this
use-case too.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to identify which numbers in a list are within each others' range

2008-01-31 Thread [EMAIL PROTECTED]
On Jan 31, 2:48 pm, "[EMAIL PROTECTED]"
<[EMAIL PROTECTED]> wrote:
> On Jan 31, 8:12 am, erikcw <[EMAIL PROTECTED]> wrote:
>

> One way would be to use sets and check for intersection:
>
> for idx, s in enumerate(mysets):
> for next_idx, next_s in enumerate(mysets[idx+1:]):
> if s.intersection(next_s):
> print "mylist[%d] and mylist[%d] intersect" % (
> idx, idx + next_idx + 1 )
>


Um, that would have been more helpful if I hadn't forgotten
to preface that with:

mylist = [(55, 58, 52), (20, 22, 18), (17, 21, 13), (60, 63, 57),]
mysets = [set(range(x[2],x[1])) for x in mylist]

--
Cheers,
Steven


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


Re: How to identify which numbers in a list are within each others' range

2008-01-31 Thread Paul Rubin
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
> mysets = [set(range(x[2],x[1])) for x in mylist]

This is pretty horrible, each set can be arbitrarily large,
i.e. if x[2] and x[1] are 0 and 100, you get a set with
a million elements.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to identify which numbers in a list are within each others' range

2008-01-31 Thread [EMAIL PROTECTED]
On Jan 31, 3:09 pm, Paul Rubin  wrote:
> "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
> > mysets = [set(range(x[2],x[1])) for x in mylist]
>
> This is pretty horrible, each set can be arbitrarily large,
> i.e. if x[2] and x[1] are 0 and 100, you get a set with
> a million elements.



True...  Any lighter-weight implementation of
sets out there?  That is, one that would defer
use of resources until actually needed --
somewhat akin to the distinction between
range and xrange, and so on.

xset?

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


Re: python modules collection

2008-01-31 Thread [EMAIL PROTECTED]
On 31 jan, 02:57, "Guilherme Polo" <[EMAIL PROTECTED]> wrote:
> 2008/1/30, J. Peng <[EMAIL PROTECTED]>:
>
> > Hello,
>
> >  Is there a site for python,which collects most kinds of python modules?
> >  like CPAN for Perl.
> >  Sometime I want to use a module,like the time/date modules,don't know
> >  where I should search from.
> >  Sorry if I have repeated this question on the list.
> >  Thanks!
>
> There ishttp://pypi.python.org/pypi
>
>

And the module index for the standard lib, of course:

http://docs.python.org/modindex.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionary Keys question

2008-01-31 Thread Dustan
On Jan 31, 7:35 am, Ben Finney <[EMAIL PROTECTED]>
wrote:
> Dustan <[EMAIL PROTECTED]> writes:
> > On Jan 30, 7:02 pm, FireNWater <[EMAIL PROTECTED]> wrote:
> > > Thank you for the explanation. . . I think I now have a (foggy)
> > > understanding of hash tables. It seems to be a way to create order
> > > (an index) out of disorder (random numbers or characters) behind
> > > the scenes. .
>
> > The key thing to realize is, quite simply, don't rely on order in a
> > dictionary.
>
> The poster to which you replied is using "order" as contrasted with
> "disorder". Clearly dictionaries *do* have order that can be relied
> upon.

He was referring to the index. So was I, as in: Don't rely on the
index, because the size of the dictionary can vary, and therefore, the
index can vary, and therefore, the programmer must recognize that the
order of looping can vary. If you're referring to the actual order of
looping, then I and every good Python Guru (of which I am not one)
disagrees with you. If not, then you're confusing the different
meanings of "order" in this context.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Standardization: Wikipedia entry

2008-01-31 Thread John Nagle
Colin J. Williams wrote:
> John Nagle wrote:
>> Paddy wrote:
>>> I would value the opinion of fellow Pythoneers who have also
>>> contributed to Wikipedia, on the issue of "Is Python Standardized".
>>> Specifically in the context of this table:
>>>   
>>> http://en.wikipedia.org/wiki/Comparison_of_programming_languages#General_comparison
>>>  
>>>
>>>   (Comparison of programming languages)
>>> And this entry in the talk page
>>>   
>>> http://en.wikipedia.org/wiki/Talk:Comparison_of_programming_languages#Standardized_Python.3F
>>>  
>>>
>>>   (Talk:Comparison of programming languages#Standardized Python?)
>>>
>>> - Thanks.
>>
>>   That's correct.  Python is not standardized by any standards body.  
>> And no
>> two implementations are even close to compiling the same language.
>>
>>   A consequence of the lack of standardization is that it discourages
>> implementations.  There are about four implementations of something like
>> Python (other than CPython), and none of them are close to being usable.
>> Letting the author of one implementation control the language discourages
>> other implementations.
>>
>>   Submitting Python 2.5 to ISO/ANSI might be a good idea.
>>
>> John Nagle
> 
> Better to wait for 3.0?
> 
 I'd argue for standardizing from 2.5, and viewing 3.x as a possible
future upgrade.

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


Re: Why the HELL has nobody answered my question !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

2008-01-31 Thread Neil Hodgson
Steve Holden wrote:
> ... 
> Look guys, I thought we'd agreed that the PSU was no longer to be

How did Steve manage to click send again after the para
-- 
http://mail.python.org/mailman/listinfo/python-list


wxEVT_SCROLL_ENDSCROLL

2008-01-31 Thread Jack Holt
Hello,

I got the following error:

Traceback (most recent call last):
  File "vplayer.py", line 15, in ?
  File "config.pyo", line 3, in ?
  File "wx\__init__.pyo", line 44, in ?
  File "wx\_core.pyo", line 3592, in ?
AttributeError: 'module' object has no attribute
'wxEVT_SCROLL_ENDSCROLL'


I never had that error before... until I messed up
with the python package
(I had to reinstall Active Python + py2exe +
wxpython). I can run my app
from the SVN with no problem. I get the error from the
compiled version -
made with py2exe.

Line 3 in config.py points to: Import wx



Help!!

Thanks, 
 -Dan



  

Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  
http://tools.search.yahoo.com/newsearch/category.php?category=shopping
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Online Debugging

2008-01-31 Thread Gabriel Genellina
En Thu, 31 Jan 2008 03:15:05 -0200, Yansky <[EMAIL PROTECTED]>  
escribió:

> I'm trying to debug a script on my server and it's taking forever
> using print to find the error. I've tried to use the debugging
> examples on this page http://webpython.codepoint.net/debugging but
> they don't seem to be working for me.
>
> Is there an easier/better way to debug online scripts? I was hoping
> there might be something similar to php where it gives some error info
> and the line code.

...and that's what the cgitb module does, among other things.
Why do you say it doesn't work for you?

-- 
Gabriel Genellina

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


Re: wxEVT_SCROLL_ENDSCROLL

2008-01-31 Thread Astan Chee
try wx.EVT_SCROLL_ENDSCROLL ?


Jack Holt wrote:
> Hello,
>
> I got the following error:
>
> Traceback (most recent call last):
>   File "vplayer.py", line 15, in ?
>   File "config.pyo", line 3, in ?
>   File "wx\__init__.pyo", line 44, in ?
>   File "wx\_core.pyo", line 3592, in ?
> AttributeError: 'module' object has no attribute
> 'wxEVT_SCROLL_ENDSCROLL'
>
>
> I never had that error before... until I messed up
> with the python package
> (I had to reinstall Active Python + py2exe +
> wxpython). I can run my app
> from the SVN with no problem. I get the error from the
> compiled version -
> made with py2exe.
>
> Line 3 in config.py points to: Import wx
>
>
>
> Help!!
>
> Thanks, 
>  -Dan
>
>
>
>   
> 
> Looking for last minute shopping deals?  
> Find them fast with Yahoo! Search.  
> http://tools.search.yahoo.com/newsearch/category.php?category=shopping
>   

Animal Logic
http://www.animallogic.com

Please think of the environment before printing this email.

This email and any attachments may be confidential and/or privileged. If you 
are not the intended recipient of this email, you must not disclose or use the 
information contained in it. Please notify the sender immediately and delete 
this document if you have received it in error. We do not guarantee this email 
is error or virus free.



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


Re: object vs class oriented -- xotcl

2008-01-31 Thread neumann
On 29 Jan., 19:22, William Pursell <[EMAIL PROTECTED]> wrote:
>  I
> believe "per object mixin" is the correct
> term for such an animal.  The first several google
> hits on that phrase all reference xotcl, so I'm
> not sure if that is an xotcl inspired vocabulary
> that isn't really standard.

well, it depends, what you mean by "standard" when it comes
to mixins. We coined the term to distinguish between per
object and per class mixins, where the per objects mixins
have much in common with the decorator design pattern (see
e.g. http://nm.wu-wien.ac.at/research/publications/xotcl-objpattern.pdf)

We have as well a paper showing that the approach based on
intersection
classes does not scale well, especially when multiple supplemental
classes should be mixed in, and some of the behavior should be as well
mixed out (see e.g. section 3.3 in 
http://nm.wu-wien.ac.at/research/publications/xotcl-mixin.pdf)

If you are interested in the matter, we have as well a recent paper
http://nm.wu-wien.ac.at/research/publications/b613.pdf providing
declarative semantics for mixins, and there is many more related
papers in the publications section of media.wu-wien.ac.at
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to identify which numbers in a list are within each others' range

2008-01-31 Thread Paul Rubin
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
> True...  Any lighter-weight implementation of
> sets out there?  That is, one that would defer
> use of resources until actually needed --
> somewhat akin to the distinction between
> range and xrange, and so on.

Don't even think of doing it that way, that solves the space problem
but leaves a speed problem.  You probably want to use something like
sort the intervals and then use the bisect module to find the
intervals intersecting a given one.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Design question - Sharing of single object by multiple processes

2008-01-31 Thread Steve Holden
Mike D wrote:
> Steve,
> 
> You raise some very good (and obvious) issues I did'nt consider. I'll 
> look further into this sort of implementation as I'm quite interested.
> 
> I suppose a compromise could be to load the objects from a pickle, that 
> may have issues in terms of updating the pickle perhaps, though it would 
> be much safer.
> 
> I'll continue to investigate, thanks for your input.
> 
No problem - I've had a long time to think about these things. You might 
also want to investigate the ConfigParser module, or Michael Foord's 
ConfigObj. In practice it would have to be a pretty complex 
configuration to make it worth pre-compiling it.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Python Standardization: Wikipedia entry

2008-01-31 Thread Terry Reedy

"John Nagle" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]


>   Submitting Python 2.5 to ISO/ANSI might be a good idea.

ANSI does not actually make standards.  It make metastandards about how to 
make standards (both style and process) and accredites US standard-making 
bodies that will follow those metastandards.  The processes require 
committee meetings and public comment periods -- a few years and some $$$. 
There in no guarantee that what would come out of such a process would be 
what went in, so 'Standard Python' might easily be a language with no 
implementations.

ANSI standards are owned by ANSI or perhaps the accrediting body.  In any 
case, electronic copies sell for $30.  They cannot legally be accessed free 
as for the docs at python.org. 



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


Re: Removal of element from list while traversing causes the next element to be skipped

2008-01-31 Thread Gabriel Genellina
En Thu, 31 Jan 2008 15:45:42 -0200, <[EMAIL PROTECTED]>  
escribió:

> Hmm, how does this fare??
>
> for i in range(len(a)):
>   if a[i]==99: a=a[:i]+a[i+1:]
>
>
> I like following your guys code noodling.  I can come up with something
> that does what it appears your doing, sometimes, but as to it's relevant
> merits I havent a clue :)

It's worse than the original `del a[i]`; not only skips over some  
elements, but you'll get an IndexError at the end

-- 
Gabriel Genellina

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


psycopg2

2008-01-31 Thread Andre' John
Hi

I am trying to do this for a Postgresql database:

conn = psycopg2.connect('host=localhost')
cur = conn.cursor()
cur.execute("SELECT * FROM names WHERE name=%s", ['S'])

, which doesn't work, and neither does

cur.execute("SELECT * FROM names WHERE name='%s'", ['S'])

or

cur.execute("SELECT * FROM names WHERE name='S'")

work. It always returns:

Traceback (most recent call last):

  File "", line 1, in 

psycopg2.ProgrammingError:  array value must start with ?{½ or 
dimension information


Though, when doing

cur.execute("SELECT * FROM names")

it works.
I am totally helpless here. Does anyone have an idea?

Cheers

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

Re: Naive idiom questions

2008-01-31 Thread Roger Miller
On Jan 31, 11:48 am, Grant Edwards <[EMAIL PROTECTED]> wrote:
>
> I'm not sure what you're asking.  AFAIK, the main reason that
> strings are immutable is so they can be used as dict keys.
>

I think its more fundamental than that.  If strings were mutable
you would be constantly worrying about whether changing a string
here might affect something there, or whether to write x = y or
x = copy.copy(y).  Or condsider


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


Re: psycopg2

2008-01-31 Thread Steve Holden
Andre' John wrote:
> Hi
> 
> I am trying to do this for a Postgresql database:
> 
> conn = psycopg2.connect('host=localhost')
> cur = conn.cursor()
> cur.execute("SELECT * FROM names WHERE name=%s", ['S'])
> 
> , which doesn't work, and neither does
> 
> cur.execute("SELECT * FROM names WHERE name='%s'", ['S'])
> 
> or
> 
> cur.execute("SELECT * FROM names WHERE name='S'")
> 
> work.

I'm more inclined to believe the first two than the third, but I suppose 
if you are telling the truth (House: "Patients always lie") then I am 
guessing you have defined your table's "names" columns to be an array type.

I haven't worked with those (since they don't confirm to the strict 
relational model I prefer to work with), but I am guessing you might try


cur.execute("SELECT * FROM names WHERE name='%s'", (['S'], ))

as this provides the necessary tuple as the second argument to execute, 
and the on;y element of the tuple is a list of a single element.

 > It always returns:
> 
> Traceback (most recent call last):
> 
>   File "", line 1, in 
> 
> psycopg2.ProgrammingError:  array value must start with ?{½ or 
> dimension information
> 
> 
> Though, when doing
> 
> cur.execute("SELECT * FROM names")
> 
> it works.
> I am totally helpless here. Does anyone have an idea?
> 
If my suggestion doesn't work, you should probably let us know more 
about the structure of your table.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


PLEASE ACCEPT MY SINCERE APOLOGIES

2008-01-31 Thread Blubaugh, David A.
To Everyone on the planet Earth,  


Please accept my apologies for

Why the Hell has nobody answered my question.


I am just trying to finish a Masters thesis that is quite beyond
anything in this world.  


David Blubaugh





 

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf
Of [EMAIL PROTECTED]
Sent: Thursday, January 31, 2008 7:30 PM
To: python-list@python.org
Subject: Python-list Digest, Vol 53, Issue 2

Send Python-list mailing list submissions to
python-list@python.org

To subscribe or unsubscribe via the World Wide Web, visit
http://mail.python.org/mailman/listinfo/python-list
or, via email, send a message with subject or body 'help' to
[EMAIL PROTECTED]

You can reach the person managing the list at
[EMAIL PROTECTED]

When replying, please edit your Subject line so it is more specific than
"Re: Contents of Python-list digest..."

This e-mail transmission contains information that is confidential and may be 
privileged.   It is intended only for the addressee(s) named above. If you 
receive this e-mail in error, please do not read, copy or disseminate it in any 
manner. If you are not the intended recipient, any disclosure, copying, 
distribution or use of the contents of this information is prohibited. Please 
reply to the message immediately by informing the sender that the message was 
misdirected. After replying, please erase it from your computer system. Your 
assistance in correcting this error is appreciated.


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


Re: Naive idiom questions

2008-01-31 Thread Terran Melconian
On 2008-02-01, Roger Miller <[EMAIL PROTECTED]> wrote:
> On Jan 31, 11:48 am, Grant Edwards <[EMAIL PROTECTED]> wrote:
>> I'm not sure what you're asking.  AFAIK, the main reason that
>> strings are immutable is so they can be used as dict keys.
>
> I think its more fundamental than that.  If strings were mutable
> you would be constantly worrying about whether changing a string
> here might affect something there, or whether to write x = y or
> x = copy.copy(y).

Sure.  I don't think any of those considerations are fundamentally
different than what we face now with lists, though, are they?  Lists can
be thought of as the mutable version of the tuple, and sets come in a
pair with frozensets so you can get whichever flavor you need for your
application.  I'm all for the default string implementation being
immutable, since it facilitations certain operations and avoids certain
classes of problems, but it isn't obvious to me why there isn't a class
of a different name, such as "mutablestring", for when one wants that.
Even having just the += operator and slicing make modifications in place
could be a win.

I guess for complete symmetry, there should then be frozendicts as well,
although I don't envision a lot of use for them at the moment.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to identify which numbers in a list are within each others' range

2008-01-31 Thread George Sakkis
On Jan 31, 7:11 pm, Paul Rubin  wrote:
> "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
> > True...  Any lighter-weight implementation of
> > sets out there?  That is, one that would defer
> > use of resources until actually needed --
> > somewhat akin to the distinction between
> > range and xrange, and so on.
>
> Don't even think of doing it that way, that solves the space problem
> but leaves a speed problem.  You probably want to use something like
> sort the intervals and then use the bisect module to find the
> intervals intersecting a given one.

I haven't actually used it but from the short description this package
seems to fit the bill: http://pypi.python.org/pypi/interval/1.0.0

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


  1   2   >