Re: Ternary operator alternative in Ptyhon

2008-06-18 Thread Allen

kretik wrote:
I'm sure this is a popular one, but after Googling for a while I 
couldn't figure out how to pull this off.


Let's say I have this initializer on a class:

def __init__(self, **params):

I'd like to short-circuit the assignment of class field values passed in 
this dictionary to something like this:


self.SomeField = \
params.has_key("mykey") ? params["mykey"] : None)

Obviously I know this is not actual Python syntax, but what would be the 
equivalent? I'm trying to avoid this, basically:


if params.has_key("mykey"):
self.SomeField = params["mykey"]
else:
self.SomeField = None

This is not a big deal of course, but I guess my main goal is to try and 
figure out of I'm not missing something more esoteric in the language 
that lets me do this.


Thanks in advance.


The syntax is a bit different, but:

result = (true_value if condition else false_value)

is how it is in Pytthon:

self.SomeField = (params['mykey'] if params.has_key('mykey') else None)



Brian Vanderburg II
--
http://mail.python.org/mailman/listinfo/python-list


Re: Buffer size when receiving data through a socket?

2008-06-18 Thread Gabriel Genellina
En Tue, 17 Jun 2008 14:32:44 -0300, John Salerno <[EMAIL PROTECTED]> escribió:

> "Gabriel Genellina" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
>> Note that most of the time you want to use the sendall() method, because
>> send() doesn't guarantee that all the data was actually sent.
>> 
>
> If I use sendall(), am I still recv'ing data with a given buffer size? What
> if I send more data than the buffer size. Is my code as written not prepared
> to handle that case? It seems like I might need to continue receiving data
> until there is no more to receive (in a loop?)...is that right?

send and recv are separate calls (they occur usually in different processes, 
even in different computers). Buffer sizes are separate too. It is posible to 
send 5K at once from one side, and require three recv calls on the other side 
to read it completely. On the other hand, if you try to read from a blocking 
socket (the default state) when no data is available, the read call will block 
(and the whole program freezes) until some data is received. There are several 
alternatives to avoid this, and surely they're explained in detail in a later 
chapter in your book...

-- 
Gabriel Genellina

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


Conflict between msvcrt and Tkinter?

2008-06-18 Thread Dick Moores

Win XP, Python 2.5.1

I'm having trouble using msvcrt.getch() in a program that also uses a 
graphics module which itself imports Tkinter. Is this to be expected?


Thanks,

Dick Moores

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


Re: Buffer size when receiving data through a socket?

2008-06-18 Thread Gabriel Genellina
En Tue, 17 Jun 2008 14:32:44 -0300, John Salerno <[EMAIL PROTECTED]> escribió:

> "Gabriel Genellina" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
>> Note that most of the time you want to use the sendall() method, because
>> send() doesn't guarantee that all the data was actually sent.
>> 
>
> If I use sendall(), am I still recv'ing data with a given buffer size? What
> if I send more data than the buffer size. Is my code as written not prepared
> to handle that case? It seems like I might need to continue receiving data
> until there is no more to receive (in a loop?)...is that right?

send and recv are separate calls (they occur usually in different processes, 
even in different computers). Buffer sizes are separate too. It is posible to 
send 5K at once from one side, and require three recv calls on the other side 
to read it completely. On the other hand, if you try to read from a blocking 
socket (the default state) when no data is available, the read call will block 
(and the whole program freezes) until some data is received. There are several 
alternatives to avoid this, and surely they're explained in detail in a later 
chapter in your book...

-- 
Gabriel Genellina

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


Re: Python GC does not work as it should be

2008-06-18 Thread Tim Roberts
"Jaimy Azle" <[EMAIL PROTECTED]> wrote:
>
>Jean-Paul Calderone wrote:
>
>> A system exception?  What's that?  C doesn't have exceptions.
>
>How could I determine it? I dont know GCC implementation, and others, but C 
>on MSVC does have it. My application were not written in C, an exception 
>raised was something like "access violation at address  on module 
>python25.dll", and MSVC debugger shows collecting state were not reset (1), 
>that is why GC would never happen.

Correct.  That error is not recoverable.  If the garbage collector crashes,
the collector is in an indeterminate state, so it is quite reasonable to
prevent it from being called again.

Here is an excellent rule: Never check for an exception that you are not
prepared to handle.
-- 
Tim Roberts, [EMAIL PROTECTED]
Providenza & Boekelheide, Inc.
--
http://mail.python.org/mailman/listinfo/python-list


Re: One more socket programming question

2008-06-18 Thread Tim Roberts
John Salerno <[EMAIL PROTECTED]> wrote:
>
>I'm now experimenting with the SocketServer class. Originally I 
>subclassed the StreamRequestHandler to make my own custom handler, but a 
>result of this seems to be that the client socket closes after it has 
>been used, instead of staying open.

Right.  "handle" is not called for one REQUEST at a time, it's called for
one CONNECTION at a time.  If you need a connection to be persistent, then
your handle() function needs to sit in a loop making recv calls until you
detect that the conversation is complete.
-- 
Tim Roberts, [EMAIL PROTECTED]
Providenza & Boekelheide, Inc.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Save turtle state?

2008-06-18 Thread Peter Otten
Allen wrote:

> I'm using the turtle module in Python.  Is there a way to save the turle
> state at any moment for recursive algorithms to easily return the turtle
> to an earlier point for another branch/etc?

Just copying the turtle seems to work:

import turtle
from copy import copy
def rec(t, n):
t.forward(n)
n = (n*4)/5
u = copy(t)
u.color("red")
u.right(45)
if n > 10:
rec(u, n)

def main():
t = turtle.Turtle()
t.color("green")
for i in range(12):
t.left(30)
rec(t, 50)

main()
raw_input()

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


Re: How do I create user-defined warnings?

2008-06-18 Thread Andrii V. Mishkovskyi
2008/6/18 Clay Hobbs <[EMAIL PROTECTED]>:
> I already know how to make user-defined exceptions, like this one:
>
>class MyException(Exception):
>pass
>
> But for a module I'm making, I would like to make a warning (so it just
> prints the warning to stderr and doesn't crash the program).  I have
> tried this:
>
>class MyWarning(Warning):
>pass
>
> And it behaves like a normal error.  Please help me, I can't figure out
> what I'm doing wrong.

Use 'warnings' module.
http://docs.python.org/lib/module-warnings.html

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



-- 
Wbr, Andrii Mishkovskyi.

He's got a heart of a little child, and he keeps it in a jar on his desk.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Annoying message when interrupting python scripts

2008-06-18 Thread geoffbache

Ben is correct in his interpretation of what I'm trying to say. The
code "should surely be changed" so that it lets a KeyboardInterrupt
exception through.

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


Re: Name lookup inside class definition

2008-06-18 Thread Bruno Desthuilliers

WaterWalk a écrit :

Hello. Consider the following two examples:
class Test1(object):
att1 = 1
def func(self):
print Test1.att1// ok


or
  print type(self).att1



class Test2(object):
att1 = 1
att2 = Test2.att1  // NameError: Name Test2 is not defined

It seems a little strange. Why a class name can be used in a method
while cannot be used in the class block itself?


class is an executable statement. The whole "class" block is first 
eval'd, then the class object is created and bound to it's name.


So when the function is called, the class statement has already been 
executed, the class object created and bound to the name Test1. But when 
the att2=Test2.att1 is executed, the class object doesn't yet exists, 
nor the name Test2.


Anyway, you don't need to refer to the class name here:

class Toto(object):
titi = 1
toto = titi + 1
--
http://mail.python.org/mailman/listinfo/python-list


Database design questions

2008-06-18 Thread David
Hi list.

I have a few database-related questions. These aren't Python-specific
questions, but some of my apps which use (or will use) these tables
are in Python :-) Let me know if I should ask this on a different
list.

Question 1: Storing app defaults.

If you have a table like this:

table1
 - id
 - field1
 - field2
 - field3

table2
 - id
 - table1_id
 - field1
 - field2
 - field3

table1 & table2 are setup as 1-to-many.

If I want to start providing user-customizable defaults to the
database (ie, we don't want apps to update database schema), is it ok
database design to add a table2 record, with a NULL table1_id field?

In other words, if table1 has no matching table2 record, then the app
will use the table2 record with a NULL table1_id field to get
defaults.

This looks messy however. Is there a better way to do it?

A few other ways I can think of:

1) Have an extra table1 record (with string fields containing
'DEFAULT'), against which the extra table2 record is linked.

2) Have a new table, just for defaults, like this:

table2_defaults
 - field1
 - field2
 - field3

Which is the cleanest way? Is there another method I should use instead?

Question 2: Backwards-compatible field addition

If you have an existing table, and apps which use it, then how do you
add new fields to the table (for new apps), but which might affect
existing apps negatively?

eg: I start with a table like this:

table1
 - id
 - field1
 - field2
 - field3

Later, I want to add a use case, where there is new behaviour, if a
new field is set in the table, like this:

table1
 - id
 - field1
 - field2
 - field3
 - field4 - NEW - if unset, do old behaviour. if set, do something else

The problem is, that existing apps (besides your new app) won't know
about field4, so they will keep using the old behaviour for new
records (where field4 is set), which you don't want.

The most obvious thing to do is to update all apps using table1, so
they also check the value of field4.

Is there another, more backwards-compatible way to add field4 for the
new behaviour, without having to update all the apps?

A few things I can think of:

1) table1 becomes a view of an updated table, with a 'WHERE field4 IS
NULL' clause.

Problem with this is that some RDBMS (Postgresql specifically) don't
let you run update statements on views.

2) Apps use stored procedures for all database access.

Maybe ok for new apps, not so much for existing apps which use regular SQL.

3) All apps use the same library for accessing database

Then you update the library and all apps automagically know about the
extra field. Again, maybe ok for new apps, not so much for existing
apps.

4) Make a new table (copy of the old one), with the extra field.

Then your app checks both tables, or just the new one if applicable.

This can work, but you may end up with a lot of app-specific tables,
where the main difference between the tables is extra columns, and
which apps use the tables.

5) Have a 'db version' column in the table. Older apps only operate on
records at or before the version the programmer knew about at the
time.

This can work, but it seems like a very non-standard, hackish way of
designing database tables. Also it's a pain for all apps to have to
hardcode a db version number.

6) Find a clever way to use table inheritance

I haven't thought it through, but here are some docs on the subject:

http://www.postgresql.org/docs/8.1/static/ddl-inherit.html

Any suggestions?

Question 3: Temporal databases

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

I haven't used them before, but I like the idea of never
deleting/updating records so you have a complete history (a bit like
source code version control).

How well do temporal databases work? Do RDBMS (like Postgresql) need
add-ons to make it effective, or can you just add extra temporal
columns to all your tables and add them to your app queries? Does this
increase app complexity and increase server load a lot?

Are there Python libraries which simplify this? (eg: add-ons for
Elixir or SQLAlchemy).

Or should apps all implement their own 'temporal data access' module,
which transparently uses the current date & time until queried for
historical data?

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


Re: Does '!=' equivelent to 'is not'

2008-06-18 Thread Gabriel Genellina
En Tue, 17 Jun 2008 23:04:16 -0300, Asun Friere <[EMAIL PROTECTED]> escribió:

> On Jun 17, 5:33 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
> wrote:
>> En Tue, 17 Jun 2008 02:25:42 -0300, Lie <[EMAIL PROTECTED]> escribió:
>
>>
>> > Basically 'a is b' and 'not(a is b)' is similar to 'id(a) == id(b)'
>> > and 'not(id(a) == id(b))'
>>
>> No.
> ...
>> ... The above statement is not. A counterexample:
>>
>> py> [] is []
>> False
>> py> id([])==id([])
>> True
>>
> But that's not what he said, he used 'a' and 'b' which are names, not
> anonymous objects.
> Fairer would be,
> a = [];b = []
> id(a) == id(b)

If you limit yourself to interpret 'a' and 'b' as actual names, yes, the 
statement is true. But I thought of them as placeholders or metasyntactic names 
- like in "abs(x) returns the absolute value of x", where x may represent *any* 
expression, not just a single name. Under this general interpretation the 
statement is not true anymore.

(This thread is getting way above 1cp...)

-- 
Gabriel Genellina

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


Google Module?

2008-06-18 Thread JulianMontez
Just started learning Python and wanted to make a program that would
retrieve files that were indexed by Google. These files can be
filtered by their extensions, nothing too difficult. :)

I wanted to know if there was a module that would allow me to access
the API easily within Python. I don't think that pyGoogle works
anymore (supposedly the SOAP API was discontinued).

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


Re: Name lookup inside class definition

2008-06-18 Thread WaterWalk
Ah, I see. Thank you all.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Database design questions

2008-06-18 Thread David
> I have a few database-related questions. These aren't Python-specific
> questions, but some of my apps which use (or will use) these tables
> are in Python :-) Let me know if I should ask this on a different
> list.

Hi list.

I've thought about this some more, and it is off-topic for the python list.

I'm going to ask this on the postgresql users list instead.

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


reading from an a gzip file

2008-06-18 Thread Nader
Hello,

I have a gzip file and I try to read from this file withe the next
statements:

 gunziped_file = gzip.GzipFile('gzip-file')
 input_file = open(gunziped_file,'r')

But I get the nezt error message:

Traceback (most recent call last):
  File "read_sfloc_files.py", line 131, in ?
input_file = open(gunziped_file,'r')
TypeError: coercing to Unicode: need string or buffer, instance found

I think that I do some mistake. Would some body tell me what is my
mistake?

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


Embedding processing module in c (winxp sp2)

2008-06-18 Thread mani
Hello every one

Did anybody tried to embed pyprocessing (http://
pyprocessing.berlios.de/) in a c app? im using python 2.4 and
pyprocessing 0.52 under winxp sp2. but it doesnt seem to work. I added
a print statement in Process.start() method after
'_current_process._children.add(self)' command in process.py file line
97 and it prints as its in a kind of loop!

here is the sample program:

#define WIN32_LEAN_AND_MEAN
#include
#include "python.h"

int main(int argc, char **argv)
{
Py_Initialize();

PyRun_SimpleString("import sys\n"
"sys.argv=['']\n" //a cheat to make processing go on!
"from processing import Process,Queue,freezeSupport\n"
"def f(q):\n"
"\timport wx\n"
"\tfrom wx.py import crust\n"
"\tapp = wx.PySimpleApp(redirect=False)\n"
"\tframe = crust.CrustFrame()\n"
"\tframe.Show()\n"
"\tapp.MainLoop()\n"
"q=Queue()\n"
"if __name__ == '__main__':\n"
"\tfreezeSupport()\n"
"\tq=Queue()\n"
"\tp = Process(target=f, args=(q,))\n"
"\tp.start()\n");

Py_Finalize();
}


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


reading from a gzip file

2008-06-18 Thread Nader
Hello,

I have a gzip file and I try to read from this file withe the next
statements:

 gunziped_file = gzip.GzipFile('gzip-file')
 input_file = open(gunziped_file,'r')

But I get the nezt error message:

Traceback (most recent call last):
  File "read_sfloc_files.py", line 131, in ?
input_file = open(gunziped_file,'r')
TypeError: coercing to Unicode: need string or buffer, instance found

I think that I do some mistake. Would some body tell me what is my
mistake?

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


Re: Temporal Databases (Database design questions)

2008-06-18 Thread M.-A. Lemburg

On 2008-06-18 09:41, David wrote:

Question 3: Temporal databases

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

I haven't used them before, but I like the idea of never
deleting/updating records so you have a complete history (a bit like
source code version control).

How well do temporal databases work? Do RDBMS (like Postgresql) need
add-ons to make it effective, or can you just add extra temporal
columns to all your tables and add them to your app queries? Does this
increase app complexity and increase server load a lot?

Are there Python libraries which simplify this? (eg: add-ons for
Elixir or SQLAlchemy).

Or should apps all implement their own 'temporal data access' module,
which transparently uses the current date & time until queried for
historical data?


You can have complete history in a database schema by:

* adding a version column
* adding a modification timestamp (and modification username,
  if that's relevant for you)
* updating the version upon INSERT and UPDATE
* have a history table for each "live" table that gets
  filled using a trigger on the version column which moves
  the inserted/updated/deleted rows into the history table
* the history table will have to have an additional column
  for storing the method of how the row got into the table
  (ie. insert/update/delete)

The main app will only use the "live" tables with the current
data. An audit tool would then provide access to the history
tables.

This works for all databases that have triggers, even SQLite.

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Jun 18 2008)
>>> Python/Zope Consulting and Support ...http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/

2008-07-07: EuroPython 2008, Vilnius, Lithuania18 days to go

 Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
--
http://mail.python.org/mailman/listinfo/python-list


Re: reading from an a gzip file

2008-06-18 Thread Matt Nordhoff
Nader wrote:
> Hello,
> 
> I have a gzip file and I try to read from this file withe the next
> statements:
> 
>  gunziped_file = gzip.GzipFile('gzip-file')
>  input_file = open(gunziped_file,'r')
> 
> But I get the nezt error message:
> 
> Traceback (most recent call last):
>   File "read_sfloc_files.py", line 131, in ?
> input_file = open(gunziped_file,'r')
> TypeError: coercing to Unicode: need string or buffer, instance found
> 
> I think that I do some mistake. Would some body tell me what is my
> mistake?
> 
> Nader

gzip.GzipFile() creates a file-like object. You don't call open() on it;
you use it directly.

$ echo foo >test
$ gzip test
$ python
>>> import gzip
>>> gfh = gzip.GzipFile('test.gz', 'rb')
>>> gfh.read()
'foo\n'
>>> gfh.close()
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: pyinotify issue

2008-06-18 Thread AndreH
On Jun 17, 12:11 pm, AndreH <[EMAIL PROTECTED]> wrote:
> On Jun 13, 3:39 pm, AndreH <[EMAIL PROTECTED]> wrote:
>
>
>
> > Good day,
>
> > I just installed pyinotify on my gentoo box.
>
> > When I test the library through "pyinotify.pv -v /tmp" under root,
> > everything works great, but when I try the same thing under my local
> > user account, I receive the following error:
> > Error: cannot watch . (WD=-1)
>
> > Not very helpful. I've tried VERBOSE=True mode, but it doens't provide
> > any additional information.
>
> > I also tried it for a directory in my home folder just to be sure it's
> > not a permission problem, but no luck.
>
> > Any ideas?
>
> > Regards,
> > Andre
>
> Ok I ended up solving my problem.
>
> pyinotify is just a wrapper for the c lib, inotif.h. Installing the
> inotify-tools package allows one to do better troubleshooting.
>
> First, my kernel version was too old and did not allow inotify to be
> executed at user-level. I bumped my kernel up to 2.6.24 and enabled
> the user-level execution flag.
>
> Then pyinotify worked once and failed for all consecutive retries.
> inotifwatch said that my "maximum number of user watches" was maxed
> out and that I should increase it under /proc/sys/fs/inotify/
> max_user_watches.
>
> Something must be wrong, since the max_user_watches was set to 8192. I
> played around with this setting (sysctl -w
> fs.inotify.max_user_watches=16843), pyinotify.py and inotifywatch, and
> finally came the conclusion that pyinotify 0.7.0 was buggy. I got hold
> of 0.7.1 which seems to have fixed this problem. Hopefully, I'm not
> speaking too soon.

I spoke too soon.

pyinotify still seems to max out my number of user watches... I get
this message when I run inotifywatch after a pyinotify operation:

Establishing watches...
Failed to watch .; upper limit on inotify watches reached!
Please increase the amount of inotify watches allowed per user via `/
proc/sys/fs/inotify/max_user_watches'.


Strange. I'll keep on troubleshooting.

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


dict order

2008-06-18 Thread Robert Bossy

Hi,

I wish to know how two dict objects are compared. By browsing the 
archives I gathered that the number of items are first compared, but if 
the two dict objects have the same number of items, then the comparison 
algorithm was not mentioned.


Note that I'm not trying to rely on this order. I'm building a 
domain-specific language where there's a data structure similar to 
python dict and I need an source of inspiration for implementing 
comparisons.


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


Getting Python exit code when calling Python script from Java program

2008-06-18 Thread Quill_Patricia
I have a Python script which is used to load data into a database. Up to
now this script has been run by customers from the Windows command
prompt using "python edg_loader.pyc". Any error messages generated are
written to a log file.  A project team working in the same company as me
here would like to use this loading utility. They write UI applications
for Windows using Java. They were able to launch the Python script from
within Java by creating a Process using Java ProcessBuilder class.
However, the way the error handling is currently implemented isn't
really suitable for use in a UI application. As I'm sure you can
appreciate it's not really feasible to tell users of a UI program to
keep checking the log files while the loading is underway!!. Ideally
they would like the Python loading utility to return an error code and
error message - the error message could then be displayed on a message
box on the UI. 
I seem to be having problems implementing this. I tried using the
sys.exit() method in my script and passed non -zero values. However the
value wasn't picked up the by Java Process.exitValue() method - it kept
picking up 0. On investigation it turned out that the exit value being
read is from python.exe process, not from the Python script. Is there
any way I can obtain the return value of a python script from a Java
program?

I did manage to get some sort of return error message. I wrote a test
message to sys.stderr in the Python script and this was picked up by
Java Process.getErrorSteam() method. 
However I would really like to get the return codes working if possible
and would appreciate any suggestions on how to implement this.

Thanks,
Patricia Quill

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


CSV variable seems to reset

2008-06-18 Thread marc wyburn
Hi, I'm using the CSV module to parse a file using

whitelistCSV_file = open("\\pathtoCSV\\whitelist.csv",'rb')
whitelistCSV = csv.reader(whitelistCSV_file)
for uname, dname, nname in whitelistCSV:
  print uname, dname, nname

The first time I run the for loop the contents of the file is
displayed.  Subsequent attempts to run the for loop I don't get any
data back and no exception.

The only way I can get the data is to run whitelistCSV_file = open("\
\pathtoCSV\\whitelist.csv",'rb') again.

I'm stumped as to how I can get around this or work out what the
problem is, I'm assuming that the 'open' command buffers the data
somewhere and that data is being wiped out by the CSV module but
surely this shouldn't happen.

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


Re: CSV variable seems to reset

2008-06-18 Thread Tim Golden

marc wyburn wrote:

Hi, I'm using the CSV module to parse a file using

whitelistCSV_file = open("\\pathtoCSV\\whitelist.csv",'rb')
whitelistCSV = csv.reader(whitelistCSV_file)
for uname, dname, nname in whitelistCSV:
  print uname, dname, nname

The first time I run the for loop the contents of the file is
displayed.  Subsequent attempts to run the for loop I don't get any
data back and no exception.


That's because the csv reader object is an iterator. You're
consuming it as you run through it. If you want to keep hold
of the contents to run again (without reinitialising it) then
pull the data into a list and iterate over that as many times
as you like:

data = list (whitelistCSV)
for uname, dname, nname in data:
 # do stuff

for uname, dname, nname in data:
 # do stuff again

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


Re: Getting Python exit code when calling Python script from Java program

2008-06-18 Thread Lie
On Jun 18, 3:54 pm, [EMAIL PROTECTED] wrote:
> I have a Python script which is used to load data into a database. Up to
> now this script has been run by customers from the Windows command
> prompt using "python edg_loader.pyc". Any error messages generated are
> written to a log file.  A project team working in the same company as me
> here would like to use this loading utility. They write UI applications
> for Windows using Java. They were able to launch the Python script from
> within Java by creating a Process using Java ProcessBuilder class.
> However, the way the error handling is currently implemented isn't
> really suitable for use in a UI application. As I'm sure you can
> appreciate it's not really feasible to tell users of a UI program to
> keep checking the log files while the loading is underway!!. Ideally
> they would like the Python loading utility to return an error code and
> error message - the error message could then be displayed on a message
> box on the UI.
> I seem to be having problems implementing this. I tried using the
> sys.exit() method in my script and passed non -zero values. However the
> value wasn't picked up the by Java Process.exitValue() method - it kept
> picking up 0. On investigation it turned out that the exit value being
> read is from python.exe process, not from the Python script. Is there
> any way I can obtain the return value of a python script from a Java
> program?
>
> I did manage to get some sort of return error message. I wrote a test
> message to sys.stderr in the Python script and this was picked up by
> Java Process.getErrorSteam() method.
> However I would really like to get the return codes working if possible
> and would appreciate any suggestions on how to implement this.
>
> Thanks,
> Patricia Quill

I'm not experienced in Java and Python, but if all else fails, you
could always create a file (or append to the log file) a special
string that indicates what the problem or whether it runs
successfully. The GUI application would always check this file after
script execution
--
http://mail.python.org/mailman/listinfo/python-list


Re: dict order

2008-06-18 Thread cokofreedom
On Jun 18, 11:22 am, Robert Bossy <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I wish to know how two dict objects are compared. By browsing the
> archives I gathered that the number of items are first compared, but if
> the two dict objects have the same number of items, then the comparison
> algorithm was not mentioned.
>
> Note that I'm not trying to rely on this order. I'm building a
> domain-specific language where there's a data structure similar to
> python dict and I need an source of inspiration for implementing
> comparisons.
>
> Thanks
> RB

I'm a little confused as to what you want. Are you asking whether two
dictionary objects have the same keys AND values, or just the Keys?

As dictionaries are unordered the best technique is to go through one
dictionary and take out a key, then see if that key exists in the
other dictionary, and if so do they share the same values.

# untested 2.5
for keys in dict_one.items():
  if keys in dict_two:
if dict_one[keys] != dict_two[keys]:
  # values are different
  else:
# key is not present

This probably isn't the most efficient way, but can quickly find
differences...
--
http://mail.python.org/mailman/listinfo/python-list


Re: PEP 372 -- Adding an ordered directory to collections

2008-06-18 Thread bearophileHUGS
Martin v. L.:
> However, I think the PEP (author) is misguided in assuming that
> making byindex() a method of odict, you get better performance than
> directly doing .items()[n] - which, as you say, you won't.

In Python 2.5 .items()[n] creates a whole list, and then takes one
item of such list.
An O(n) byindex() just scans the items to return the n-th.
So while being both O(n) in time, the .items()[n] may allocate quite
more memory.

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


Re: dict order

2008-06-18 Thread cokofreedom
On Jun 18, 12:32 pm, [EMAIL PROTECTED] wrote:
> On Jun 18, 11:22 am, Robert Bossy <[EMAIL PROTECTED]> wrote:
>
> > Hi,
>
> > I wish to know how two dict objects are compared. By browsing the
> > archives I gathered that the number of items are first compared, but if
> > the two dict objects have the same number of items, then the comparison
> > algorithm was not mentioned.
>
> > Note that I'm not trying to rely on this order. I'm building a
> > domain-specific language where there's a data structure similar to
> > python dict and I need an source of inspiration for implementing
> > comparisons.
>
> > Thanks
> > RB
>
> I'm a little confused as to what you want. Are you asking whether two
> dictionary objects have the same keys AND values, or just the Keys?
>
> As dictionaries are unordered the best technique is to go through one
> dictionary and take out a key, then see if that key exists in the
> other dictionary, and if so do they share the same values.
>
> # untested 2.5
> for keys in dict_one.items():
>   if keys in dict_two:
> if dict_one[keys] != dict_two[keys]:
>   # values are different
>   else:
> # key is not present
>
> This probably isn't the most efficient way, but can quickly find
> differences...

Whoops

for keys, values in dict_one.items():
  if keys in dict_two:
if values == dict_two[keys]:

should also work...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Multiprecision arithmetic library question.

2008-06-18 Thread Mark Wooding
Michael Press <[EMAIL PROTECTED]> wrote:

> I already compiled and installed the GNU multiprecision library
> on Mac OS X, and link to it in C programs. 
> How do I link to the library from Python? 

You know that Python already supports multiprecision integer arithmetic,
right?  If you desperately want GMP, though, there's the gmpy module
(q.g.).

-- [mdw]
--
http://mail.python.org/mailman/listinfo/python-list


Re: dict order

2008-06-18 Thread Lie
On Jun 18, 4:22 pm, Robert Bossy <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I wish to know how two dict objects are compared. By browsing the
> archives I gathered that the number of items are first compared, but if
> the two dict objects have the same number of items, then the comparison
> algorithm was not mentioned.
>
> Note that I'm not trying to rely on this order. I'm building a
> domain-specific language where there's a data structure similar to
> python dict and I need an source of inspiration for implementing
> comparisons.
>
> Thanks
> RB

Why not consider them as equal, it's simple and is basically telling
people not to rely on them because one day A might be lower than B and
the other day, A and B that hasn't been modified at all might switch
sides without warning.

Or you could just use any arbitrary way of comparison (Python Zen: In
the face of ambiguity, refuse the temptation to guess.).
--
http://mail.python.org/mailman/listinfo/python-list


Re: dict order

2008-06-18 Thread A.T.Hofkamp
On 2008-06-18, Robert Bossy <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I wish to know how two dict objects are compared. By browsing the 
> archives I gathered that the number of items are first compared, but if 
> the two dict objects have the same number of items, then the comparison 
> algorithm was not mentioned.

You could consider hashing, if the order of hashing is independent on the end
result and it is fast enough.
Different hash results are different dicts, equal hashes give you no
conclusion.

Then, you are probably down to comparing keys (since keys are unique and the
number of keys in both dicts are the same, one direction of comparing is
enough) and values.

Since dict is designed to be fast in accessing keys, the performance should be
not entirely disastreus.

> domain-specific language where there's a data structure similar to 
> python dict and I need an source of inspiration for implementing 
> comparisons.

If you have a choice in data structures, you may want to have an ordered
dictionary (or sorted dict), where keys are sorted, making it much easier to
decide about equality.
The price you pay at least is less fast dictionary modifications and possibly
also less fast key access although that also depends on the speed of hashing
the key.

There are also discussions about ordered dictionaries in Python (I believe they
are called OrderedDict or SortedDict). There is also a PEP about them.

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


Re: Getting Python exit code when calling Python script from Java program

2008-06-18 Thread A.T.Hofkamp
On 2008-06-18, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> I have a Python script which is used to load data into a database. Up to
> now this script has been run by customers from the Windows command
> prompt using "python edg_loader.pyc". Any error messages generated are
> written to a log file.  A project team working in the same company as me
> here would like to use this loading utility. They write UI applications
> for Windows using Java. They were able to launch the Python script from
> within Java by creating a Process using Java ProcessBuilder class.
> However, the way the error handling is currently implemented isn't
> really suitable for use in a UI application. As I'm sure you can
> appreciate it's not really feasible to tell users of a UI program to
> keep checking the log files while the loading is underway!!. Ideally
> they would like the Python loading utility to return an error code and
> error message - the error message could then be displayed on a message
> box on the UI. 

The first thing to do is decide whether this is a Java problem, a Python
problem, or a OS problem. Then post the question in the appropiate forum.

One simple experiment may be to write a C function that returns a non-zero exit
code, and run that as job.

> I seem to be having problems implementing this. I tried using the
> sys.exit() method in my script and passed non -zero values. However the
> value wasn't picked up the by Java Process.exitValue() method - it kept

What did the OS say?
Run a program like below in OS ('python x.py'), then query the OS about the
exit code of the program. In that way, you can narrow down your search.

> picking up 0. On investigation it turned out that the exit value being
> read is from python.exe process, not from the Python script. Is there
> any way I can obtain the return value of a python script from a Java

This is not what I see happening here:

x.py:
import sys
sys.exit(138)

%  python2.4 x.py
%  echo $?
138

as you can see, the mechanism works at my Linux system.


> Java Process.getErrorSteam() method. 
> However I would really like to get the return codes working if possible
> and would appreciate any suggestions on how to implement this.

I'd suggest to first find out where in the Java->OS->Python->OS->Java chain
things go wrong.

As for how to handle a child-process from Java, try asking in a Java news
group.

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


Re: dict order

2008-06-18 Thread Lie
On Jun 18, 5:35 pm, [EMAIL PROTECTED] wrote:
> On Jun 18, 12:32 pm, [EMAIL PROTECTED] wrote:
>
>
>
> > On Jun 18, 11:22 am, Robert Bossy <[EMAIL PROTECTED]> wrote:
>
> > > Hi,
>
> > > I wish to know how two dict objects are compared. By browsing the
> > > archives I gathered that the number of items are first compared, but if
> > > the two dict objects have the same number of items, then the comparison
> > > algorithm was not mentioned.
>
> > > Note that I'm not trying to rely on this order. I'm building a
> > > domain-specific language where there's a data structure similar to
> > > python dict and I need an source of inspiration for implementing
> > > comparisons.
>
> > > Thanks
> > > RB
>
> > I'm a little confused as to what you want. Are you asking whether two
> > dictionary objects have the same keys AND values, or just the Keys?
>
> > As dictionaries are unordered the best technique is to go through one
> > dictionary and take out a key, then see if that key exists in the
> > other dictionary, and if so do they share the same values.
>
> > # untested 2.5
> > for keys in dict_one.items():
> >   if keys in dict_two:
> >     if dict_one[keys] != dict_two[keys]:
> >       # values are different
> >   else:
> >     # key is not present
>
> > This probably isn't the most efficient way, but can quickly find
> > differences...
>
> Whoops
>
> for keys, values in dict_one.items():
>   if keys in dict_two:
>     if values == dict_two[keys]:
>
> should also work...

Whoops, I think I misunderstood the question. If what you're asking
whether two dictionary is equal (equality comparison, rather than
sorting comparison). You could do something like this:

a = [...]
b = [...]

s = set()

for bk, bv in b.iteritems():
s.add((bk, bv))

for ak, av in a.iteritems():
if not((ak, av) in s):
print 'Difference Found!'
--
http://mail.python.org/mailman/listinfo/python-list


Re: Buffer size when receiving data through a socket?

2008-06-18 Thread MRAB
On Jun 18, 7:52 am, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On Tue, 17 Jun 2008 09:39:07 -0400, "John Salerno"
> <[EMAIL PROTECTED]> declaimed the following in comp.lang.python:
>
>
>
> > > while True:
> > >    data = raw_input('> ')
> > >    if not data:
> > >        break
> > >    client_socket.send(data)
> > >    data = client_socket.recv(buffer_size)
> > >    if not data:
> > >        break
> > >    print data
>
> > > client_socket.close()
>
> > Also, is that second "if not data: break" statement necessary? It seems like
> > once you get past the first if, you don't need the second one. Of course, I
> > guses it's possible that the server could return a False value, but even
> > still, would it make sense to break out of the loop and close the connection
> > because of that?
>
>         The first if is checking for lack of interactive input -- and, as
> coded, will never break out as ANY response to the > prompt will have a
> newline attached.
>
[snip]
FYI, I've just checked:

>>> raw_input('> ')
> abc
'abc'

raw_input() doesn't put a newline on the end.
--
http://mail.python.org/mailman/listinfo/python-list


Re: PEP 372 -- Adding an ordered directory to collections

2008-06-18 Thread Armin Ronacher
Martin v. Löwis  v.loewis.de> writes:

> 
> > I think I have lost the thread here, sorry. So I explain again what I
> > mean. I think for this data structure it's important to keep all the
> > normal dict operations at the same speed. If you use a C
> > implementation vaguely similar to my pure python recipe you can
> > perform the del in O(1) too, because pairs are joined in (double)
> > linked list. But such data structure is O(n) to find the n-th item
> > inserted into the sequence.
> 
> Right. So byindex(n) would be O(n) then, right? If so, what's the
> purpose of having that method in the first place?
What's the purpose of having list.insert?

> The PEP doesn't give a rationale, but just proposes that the method
> be there. My guess is that it includes it for performance reasons.
> However, I think the PEP (author) is misguided in assuming that
> making byindex() a method of odict, you get better performance than
> directly doing .items()[n] - which, as you say, you won't.
Without byindex the only way to cherry pick an item is either doing
something like

i = od.iteritems()
for idx in xrange(offset):
value = idx.next()
return value

Or

return od.items()[offset]

One creates tons of unnecessary method calls, the other creates a full
blown list object just to throw it away later.  Both less than optimal
solutions that can be implemented in a more efficient way on the C
layer where one only has to iterate over the linked list offset times
and return the item.  And iteration for that linked list is most likely
something like "for (n = 0; n != offset; ++n) iter = iter->next".

Regards,
Armin

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

Re: reading from a gzip file

2008-06-18 Thread MRAB
On Jun 18, 10:10 am, Nader <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I have a gzip file and I try to read from this file withe the next
> statements:
>
>  gunziped_file = gzip.GzipFile('gzip-file')
>  input_file = open(gunziped_file,'r')
>
> But I get the nezt error message:
>
> Traceback (most recent call last):
>   File "read_sfloc_files.py", line 131, in ?
>     input_file = open(gunziped_file,'r')
> TypeError: coercing to Unicode: need string or buffer, instance found
>
> I think that I do some mistake. Would some body tell me what is my
> mistake?
>
You're opening the gzip file and then passing the gzip file object as
a filename to open(). The documentation for the gzip module is at
http://www.python.org/doc/lib/module-gzip.html.
--
http://mail.python.org/mailman/listinfo/python-list


Interpreting string containing \u000a

2008-06-18 Thread Francis Girard
Hi,

I have an ISO-8859-1 file containing things like
"Hello\u000d\u000aWorld", i.e. the character '\', followed by the
character 'u' and then '0', etc.

What is the easiest way to automatically translate these codes into
unicode characters ?

Thank you

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


Re: dict order

2008-06-18 Thread Robert Bossy

Lie wrote:

Whoops, I think I misunderstood the question. If what you're asking
whether two dictionary is equal (equality comparison, rather than
sorting comparison). You could do something like this:

Testing for equality and finding differences are trivial tasks indeed. 
It is the sort order I'm interested in. The meaning of the order is not 
really an issue, I'm rather looking for a consistent comparison function 
(in the __cmp__ sense) such as:

   if d1 > d2 and d2 > d3,
   then d1 > d3

I'm not sure the hashing method suggested by Albert guarantees that.

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


Re: dict order

2008-06-18 Thread Peter Otten
Robert Bossy wrote:

> I wish to know how two dict objects are compared. By browsing the
> archives I gathered that the number of items are first compared, but if
> the two dict objects have the same number of items, then the comparison
> algorithm was not mentioned.

If I interpret the comments in 

http://svn.python.org/view/python/trunk/Objects/dictobject.c?rev=64048&view=markup

correctly it's roughly

def characterize(d, e):
return min(((k, v) for k, v in d.iteritems() if k not in e or e[k] != v),
   key=lambda (k, v): k)

def dict_compare(d, e):
result = cmp(len(d), len(e))
if result:
return result
try:
ka, va = characterize(d, e)
except ValueError:
return 0
kb, vb = characterize(e, d)
return cmp(ka, kb) or cmp(va, vb)

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


Re: Interpreting string containing \u000a

2008-06-18 Thread Duncan Booth
"Francis Girard" <[EMAIL PROTECTED]> wrote:

> I have an ISO-8859-1 file containing things like
> "Hello\u000d\u000aWorld", i.e. the character '\', followed by the
> character 'u' and then '0', etc.
> 
> What is the easiest way to automatically translate these codes into
> unicode characters ?
> 

>>> s = r"Hello\u000d\u000aWorld"
>>> print s
Hello\u000d\u000aWorld
>>> s.decode('iso-8859-1').decode('unicode-escape')
u'Hello\r\nWorld'
>>> 

-- 
Duncan Booth http://kupuguy.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Interpreting string containing \u000a

2008-06-18 Thread Peter Otten
Francis Girard wrote:

> I have an ISO-8859-1 file containing things like
> "Hello\u000d\u000aWorld", i.e. the character '\', followed by the
> character 'u' and then '0', etc.
> 
> What is the easiest way to automatically translate these codes into
> unicode characters ?

If the file really contains the escape sequences use "unicode-escape" as the
encoding:

>>> "Hello\\u000d\\u000aWorld".decode("unicode-escape")
u'Hello\r\nWorld'

If it contains the raw bytes use "iso-8859-1":

>>> "Hello\x0d\x0aWorld".decode("iso-8859-1")
u'Hello\r\nWorld'

Open the file with

codecs.open(filename, encoding=encoding_as_determined_above)

instead of the builtin open().

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


Re: Interpreting string containing \u000a

2008-06-18 Thread Francis Girard
Thank you very much ! I didn't know about this 'unicode-escape'. That's
great!

Francis

2008/6/18 Duncan Booth <[EMAIL PROTECTED]>:

> "Francis Girard" <[EMAIL PROTECTED]> wrote:
>
> > I have an ISO-8859-1 file containing things like
> > "Hello\u000d\u000aWorld", i.e. the character '\', followed by the
> > character 'u' and then '0', etc.
> >
> > What is the easiest way to automatically translate these codes into
> > unicode characters ?
> >
>
> >>> s = r"Hello\u000d\u000aWorld"
> >>> print s
> Hello\u000d\u000aWorld
> >>> s.decode('iso-8859-1').decode('unicode-escape')
> u'Hello\r\nWorld'
> >>>
>
> --
> Duncan Booth http://kupuguy.blogspot.com
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list

Re: Temporal Databases (Database design questions)

2008-06-18 Thread David
On Wed, Jun 18, 2008 at 11:16 AM, M.-A. Lemburg <[EMAIL PROTECTED]> wrote:
> On 2008-06-18 09:41, David wrote:
>>
>> Question 3: Temporal databases
>>
>> http://en.wikipedia.org/wiki/Temporal_database
>>
>> I haven't used them before, but I like the idea of never
>> deleting/updating records so you have a complete history (a bit like
>> source code version control).
>>
>> How well do temporal databases work? Do RDBMS (like Postgresql) need
>> add-ons to make it effective, or can you just add extra temporal
>> columns to all your tables and add them to your app queries? Does this
>> increase app complexity and increase server load a lot?
>>
>> Are there Python libraries which simplify this? (eg: add-ons for
>> Elixir or SQLAlchemy).
>>
>> Or should apps all implement their own 'temporal data access' module,
>> which transparently uses the current date & time until queried for
>> historical data?
>
> You can have complete history in a database schema by:
>
> * adding a version column
> * adding a modification timestamp (and modification username,
>  if that's relevant for you)
> * updating the version upon INSERT and UPDATE
> * have a history table for each "live" table that gets
>  filled using a trigger on the version column which moves
>  the inserted/updated/deleted rows into the history table
> * the history table will have to have an additional column
>  for storing the method of how the row got into the table
>  (ie. insert/update/delete)
>
> The main app will only use the "live" tables with the current
> data. An audit tool would then provide access to the history
> tables.
>
> This works for all databases that have triggers, even SQLite.
>

Thanks for your reply.

How do you maintain foreign key references with this approach?

eg, you have these 4 tables:

table1
 - id
 - field1
 - field2
 - field3
 - version
 - modified

table1_history
 - id
 - field1
 - field2
 - field3
 - version
 - modified
 - updatemethod

table2
 - id
 - table1_id
 - field1
 - field2
 - field3
 - version
 - modified

table2_history
 - id
 - table1_id
 - field1
 - field2
 - field3
 - version
 - modified
 - updatemethod

Should table2_history.table1_id point to table1.id, or table1_history.id?

If table2_history.table1_id points to table1.id, then you will have
problems with when you remove records from table1.

If table2_history.table1_id points to table1_history.id, then you need
to make a table1_history entry for the new values.

Also, when you start updating records in table1, then table2 and/or
table2_history will still be pointing to old table1 records, instead
of the new value.

What this probably means, is that whenever you make any changes to
records, then:

1) Make a history entry for the record (as you described)

2) Also make new history entries for all records that depend on the
record that was updated, and for their sub-dependencies too,
recursively (even if those dependent records weren't themselves
updated). The new history record foreign keys should always point to
other historical records.

There may be cases where you can skip adding redundant records to the
history tables. But if your primary keys (in history tables) are
auto-incrementing integers, then the foreign keys (in the dependant
history tables) will all need to update in a 'cascading' way (so they
all point to records which are correct for that point in time).

This would probably also be a problem for regular temporal databases,
unless they have some built-in 'snapshot all foreign dependencies'
function.

How is this normally handled?

One method (stealing idea from git) would be for historical tables to
use 'hash' values for primary and foreign keys. The 'hash' (for
primary key) would be calculated from the other values in the record.
Foreign keys for dependent then also become hash strings, pointing to
the correct parent record.

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


Re: dict order

2008-06-18 Thread Robert Bossy

Peter Otten wrote:

Robert Bossy wrote:

  

I wish to know how two dict objects are compared. By browsing the
archives I gathered that the number of items are first compared, but if
the two dict objects have the same number of items, then the comparison
algorithm was not mentioned.



If I interpret the comments in 


http://svn.python.org/view/python/trunk/Objects/dictobject.c?rev=64048&view=markup

correctly it's roughly

def characterize(d, e):
return min(((k, v) for k, v in d.iteritems() if k not in e or e[k] != v),
   key=lambda (k, v): k)

def dict_compare(d, e):
result = cmp(len(d), len(e))
if result:
return result
try:
ka, va = characterize(d, e)
except ValueError:
return 0
kb, vb = characterize(e, d)
return cmp(ka, kb) or cmp(va, vb)
Thanks, Peter! That was exactly what I was looking for. Quite clever, I 
might add.


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


question relateding to parsing dbf files.

2008-06-18 Thread Krishnakant Mane
hello all.
I need to parse some dbf files through python.
the reason being that I have to migrate some old data from dbf files
to postgresql.
all that I need to know is if some one has got a working code sample
using dbfpy.
I found this module suitable for my work but can't figure out how to use it.
else, if there is any other module, please recommend so.
happy hacking.
Krishnakant.
--
http://mail.python.org/mailman/listinfo/python-list


Re: dict order

2008-06-18 Thread A.T.Hofkamp
On 2008-06-18, Robert Bossy <[EMAIL PROTECTED]> wrote:
> Lie wrote:
>>> Whoops, I think I misunderstood the question. If what you're asking
>>> whether two dictionary is equal (equality comparison, rather than
>>> sorting comparison). You could do something like this:
>>> 
> Testing for equality and finding differences are trivial tasks indeed. 
> It is the sort order I'm interested in. The meaning of the order is not 
> really an issue, I'm rather looking for a consistent comparison function 
> (in the __cmp__ sense) such as:
> if d1 > d2 and d2 > d3,
> then d1 > d3
>
> I'm not sure the hashing method suggested by Albert guarantees that.

I read the post as the desire to test equality between dictionary-like data
structures. With some care (see below) (and the ability to compute the hash
fast), you could use hashing as way to decide that two such structures are not
equal.

Afaik you cannot use hashing for testing order ('<' or '>'). If I gave that
impression, sorry; it was not my intention.


In the equality case, you need special care with computing the hash
value of the keys (and values), since you want to have the same hash result
of the dictionary independent of the order of the keys.
One way of achieving that is to use XOR (^) to combine hash-values of the
elements. Unfortunately, XOR may not always produce good hash values.



If you want ordered dictionaries (as in you can compare dictionaries with each
other, and decide which is larger), I'd suggest to keep the keys of the
dictionaries sorted. Dictionary comparing can then be done by comparing keys in
increasing order (for example). If you encounter two non-equal keys, you
immediately can use the order of those two keys as the order of the
dictionaries.
(ie the order of two dictionaries is decided by the order of the first
non-equal keys). This gives you the transitive property (d1 > d2 and d2 > d3
implies d1 > d3).

(and len() is a cheap first order filter here; dictionaries are ordered by
length first, and by first non-equal keys second then.)

I have used this trick to define ordered sets (which are basically dictionaries
without value part). It worked like a charm.

Sincerely,
Albert

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


Re: marshal.dumps quadratic growth and marshal.dump not allowing file-like objects

2008-06-18 Thread Aaron Watters

>
> Anywaymarshalshould not be used by user code to serialize objects.
> It's only meant for Python byte code. Please use the pickle/cPickle
> module instead.
>
> Christian

Just for yucks let me point out that marshal has
no real security concerns of interest to the non-paranoid,
whereas pickle is a security disaster waiting to happen
unless you are extremely cautious... yet again.

Sorry, I know a even a monkey learns after 3 times...

  -- Aaron Watters

===
http://www.xfeedme.com/nucular/pydistro.py/go?FREETEXT=disaster


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


Re: Google Module?

2008-06-18 Thread Mike Driscoll
On Jun 18, 2:54 am, JulianMontez <[EMAIL PROTECTED]> wrote:
> Just started learning Python and wanted to make a program that would
> retrieve files that were indexed by Google. These files can be
> filtered by their extensions, nothing too difficult. :)
>
> I wanted to know if there was a module that would allow me to access
> the API easily within Python. I don't think that pyGoogle works
> anymore (supposedly the SOAP API was discontinued).
>
> Thanks in advance!


You're probably looking for the gdata module: 
http://code.google.com/p/gdata-python-client/

It hooks into most of Google's APIs.

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


Re: question relateding to parsing dbf files.

2008-06-18 Thread Daniel Mahoney
On Wed, 18 Jun 2008 18:20:20 +0530, Krishnakant Mane wrote:

> hello all.
> I need to parse some dbf files through python.
> the reason being that I have to migrate some old data from dbf files
> to postgresql.
> all that I need to know is if some one has got a working code sample
> using dbfpy.
> I found this module suitable for my work but can't figure out how to use it.
> else, if there is any other module, please recommend so.
> happy hacking.
> Krishnakant.

It's pretty simple, but there is a small example on dbfpy's Sourceforge
page (http://dbfpy.sourceforge.net/).

Also, it's really the long way around, but ActiveState's cookbook section
has a recipe for reading and writing DBF files in Python without using
dbfpy. It's at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362715

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


ZFS bindings

2008-06-18 Thread Kris Kennaway
Is anyone aware of python bindings for ZFS?  I just want to replicate 
(or at least wrap) the command line functionality for interacting with 
snapshots etc.  Searches have turned up nothing.


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


Re: Buffer size when receiving data through a socket?

2008-06-18 Thread John Salerno
"Dennis Lee Bieber" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> The first if is checking for lack of interactive input -- and, as
> coded, will never break out as ANY response to the > prompt will have a
> newline attached.
>
> Try with raw_input("> ").strip() instead

Well, I know the first if block works properly. Pressing just ENTER will 
exit the loop and close the client socket.

> The second if is checking for empty receive block...  And since
> .recv() blocks until it has something to return (as I recall) it may not
> be of use...

Interesting point. I'm not sure if it works that way though. I *think* I 
tried sending an empty string from the server back to the client, and as 
expected it exited the loop and closed the client, which doesn't make sense 
to me, since an empty string could be perfectly valid return data.

I opted to remove the second if statement and see where that takes me. :) 


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


Re: One more socket programming question

2008-06-18 Thread John Salerno
"Tim Roberts" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> John Salerno <[EMAIL PROTECTED]> wrote:
>>
>>I'm now experimenting with the SocketServer class. Originally I
>>subclassed the StreamRequestHandler to make my own custom handler, but a
>>result of this seems to be that the client socket closes after it has
>>been used, instead of staying open.
>
> Right.  "handle" is not called for one REQUEST at a time, it's called for
> one CONNECTION at a time.  If you need a connection to be persistent, then
> your handle() function needs to sit in a loop making recv calls until you
> detect that the conversation is complete.

Ah, so I need to rewrite my handle method. I was thinking that the 
specialized setup() and/or finish() calls from StreamRequestHandler were the 
reason that the connection was closed after one use (which is why I tried 
BaseRequestHandler instead).

Thanks. 


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


Re: Does '!=' equivelent to 'is not' [drifting OT...]

2008-06-18 Thread Paul McGuire
On Jun 17, 7:09 am, Derek Martin <[EMAIL PROTECTED]> wrote:
> On Tue, Jun 17, 2008 at 04:33:03AM -0300, Gabriel Genellina wrote:
> > > Basically 'a is b' and 'not(a is b)' is similar to 'id(a) == id(b)'
> > > and 'not(id(a) == id(b))'
>
> > No.
>

>
> Saying a flat "no" alone, without qualifying your statement is
> generally interpreted as rude in English...  It's kind of like how you
> talk to children when they're too young to understand the explanation.
> Yucky.
>
Geez, man, this is Usenet.  If you want rude or condescending, the
answer would have been "No, you flatulent moron."  Or maybe the
alarmist, "No! No! No!"

I see the unqualified "No." often on this list, as a short cut for
"Your technical explanation is flawed or has overlooked a critical
point or corner case," and is usually followed by more details further
down in the post to explain what the misconception or oversight was.

Back in my college days, I would not be surprised for a professor to
respond "No." (or worse) if I offered an erroneous explanation to
another student.  The unqualified "No." may be curt, and on a more
sensitive day, one might write "No. (see below)", but as one of the
most informed and careful posters on this list, I'm inclined to give
Gabriel a little slack.

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


Re: 2d graphics - drawing a vescica piscis in Python

2008-06-18 Thread Terrence Brannon
On Jun 17, 3:45 pm, Terrence Brannon <[EMAIL PROTECTED]> wrote:
> Hello, I have written a program to draw a vescica piscis  en.wikipedia.org/wiki/Vesica_piscis>

actually, I mis-spelled the term. It should be vesica piscis or vesica
pisces. I put a "c" after the "s" -- vescica --- and that is wrong and
made it hard for me to find this post the next day.

now others will be able to search the archives and find it.
--
http://mail.python.org/mailman/listinfo/python-list


Looking for lots of words in lots of files

2008-06-18 Thread brad
Just wondering if anyone has ever solved this efficiently... not looking 
for specific solutions tho... just ideas.


I have one thousand words and one thousand files. I need to read the 
files to see if some of the words are in the files. I can stop reading a 
file once I find 10 of the words in it. It's easy for me to do this with 
a few dozen words, but a thousand words is too large for an RE and too 
inefficient to loop, etc. Any suggestions?


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


Re: Looking for lots of words in lots of files

2008-06-18 Thread Diez B. Roggisch
brad wrote:

> Just wondering if anyone has ever solved this efficiently... not looking
> for specific solutions tho... just ideas.
> 
> I have one thousand words and one thousand files. I need to read the
> files to see if some of the words are in the files. I can stop reading a
> file once I find 10 of the words in it. It's easy for me to do this with
> a few dozen words, but a thousand words is too large for an RE and too
> inefficient to loop, etc. Any suggestions?

Use an indexer, like lucene (available as pylucene) or a database that
offers word-indices.

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


Re: Looking for lots of words in lots of files

2008-06-18 Thread Calvin Spealman

Upload, wait, and google them.

Seriously tho, aside from using a real indexer, I would build a set  
of the words I'm looking for, and then loop over each file, looping  
over the words and doing quick checks for containment in the set. If  
so, add to a dict of file names to list of words found until the list  
hits 10 length. I don't think that would be a complicated solution  
and it shouldn't be terrible at performance.


If you need to run this more than once, use an indexer.

If you only need to use it once, use an indexer, so you learn how for  
next time.


On Jun 18, 2008, at 10:28 AM, brad wrote:

Just wondering if anyone has ever solved this efficiently... not  
looking for specific solutions tho... just ideas.


I have one thousand words and one thousand files. I need to read  
the files to see if some of the words are in the files. I can stop  
reading a file once I find 10 of the words in it. It's easy for me  
to do this with a few dozen words, but a thousand words is too  
large for an RE and too inefficient to loop, etc. Any suggestions?


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


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


Re: Looking for lots of words in lots of files

2008-06-18 Thread Kris Kennaway

Calvin Spealman wrote:

Upload, wait, and google them.

Seriously tho, aside from using a real indexer, I would build a set of 
the words I'm looking for, and then loop over each file, looping over 
the words and doing quick checks for containment in the set. If so, add 
to a dict of file names to list of words found until the list hits 10 
length. I don't think that would be a complicated solution and it 
shouldn't be terrible at performance.


If you need to run this more than once, use an indexer.

If you only need to use it once, use an indexer, so you learn how for 
next time.


If you can't use an indexer, and performance matters, evaluate using 
grep and a shell script.  Seriously.


grep is a couple of orders of magnitude faster at pattern matching 
strings in files (and especially regexps) than python is.  Even if you 
are invoking grep multiple times it is still likely to be faster than a 
"maximally efficient" single pass over the file in python.  This 
realization was disappointing to me :)


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


Re: Hrounding error

2008-06-18 Thread Jerry Hill
On Wed, Jun 18, 2008 at 1:47 AM,  <[EMAIL PROTECTED]> wrote:
 234 - 23234.2345
> -23000.2344
>
> This is not correct by my calculations.

Python floating point operations use the underlying C floating point
libraries which, in turn, usually rely on the hardware's floating
point implementations.  This Wikipedia article talks about how those
values are usually stored and manipulated:
http://en.wikipedia.org/wiki/IEEE_floating-point_standard

So, which IEEE double precision floating point value would you like
instead?  As far as I understand it, these are your two choices:

'ba490c020f76d6c0' = -23000.2344
'bb490c020f76d6c0' = -23000.23450002

Alternatively, investigate the Decimal module, but keep in mind that
it can have the same sorts of issues, just on different numbers.
Compare, for instance:

>>> (1.0/3.0) * 3.0
1.0

>>> from decimal import Decimal
>>> ( Decimal('1.0')/Decimal('3.0') ) * Decimal('3.0')
Decimal("0.")
>>>

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


Re: dict order

2008-06-18 Thread Kirk Strauser
At 2008-06-18T10:32:48Z, [EMAIL PROTECTED] writes:

> # untested 2.5
> for keys in dict_one.items():
>   if keys in dict_two:
> if dict_one[keys] != dict_two[keys]:
>   # values are different
>   else:
> # key is not present

That fails if there is an item in dict_two that's not in dict_one.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: question relateding to parsing dbf files.

2008-06-18 Thread Kirk Strauser
At 2008-06-18T12:50:20Z, "Krishnakant Mane" <[EMAIL PROTECTED]> writes:

> hello all.
> I need to parse some dbf files through python.
> the reason being that I have to migrate some old data from dbf files
> to postgresql.
> all that I need to know is if some one has got a working code sample
> using dbfpy.
> I found this module suitable for my work but can't figure out how to use it.
> else, if there is any other module, please recommend so.
> happy hacking.
> Krishnakant.

Does it have to by in Python?  I host this project, written in C++:
http://honeypot.net/project/xbasetopg .  If that's too complicated, I've
written a replacement in straight C but I haven't published it yet.

I use these programs to sync our legacy FoxPro database to our new
PostgreSQL servers on an hourly basis.  Syncing 4GB of data tables about 10
minutes.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for lots of words in lots of files

2008-06-18 Thread Francis Girard
Hi,

Use a suffix tree. First make yourself a suffix tree of your thousand files
and the use it.
This is a classical problem for that kind of structure.

Just search "suffix tree" or "suffix tree python" on google to find a
definition and an implementation.

(Also Jon Bentley's "Programming Pearls" is a great book to read)

Regards

Francis Girard

2008/6/18 brad <[EMAIL PROTECTED]>:

> Just wondering if anyone has ever solved this efficiently... not looking
> for specific solutions tho... just ideas.
>
> I have one thousand words and one thousand files. I need to read the files
> to see if some of the words are in the files. I can stop reading a file once
> I find 10 of the words in it. It's easy for me to do this with a few dozen
> words, but a thousand words is too large for an RE and too inefficient to
> loop, etc. Any suggestions?
>
> Thanks
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list

Re: urllib (54, 'Connection reset by peer') error

2008-06-18 Thread chrispoliquin
Thanks for the help.  The error handling worked to a certain extent
but after a while the server does seem to stop responding to my
requests.

I have a list of about 7,000 links to pages I want to parse the HTML
of (it's basically a web crawler) but after a certain number of
urlretrieve() or urlopen() calls the server just stops responding.
Anyone know of a way to get around this?  I don't own the server so I
can't make any modifications on that side.
--
http://mail.python.org/mailman/listinfo/python-list


Re: urllib (54, 'Connection reset by peer') error

2008-06-18 Thread Tim Golden

[EMAIL PROTECTED] wrote:

Thanks for the help.  The error handling worked to a certain extent
but after a while the server does seem to stop responding to my
requests.

I have a list of about 7,000 links to pages I want to parse the HTML
of (it's basically a web crawler) but after a certain number of
urlretrieve() or urlopen() calls the server just stops responding.
Anyone know of a way to get around this?  I don't own the server so I
can't make any modifications on that side.


I think someone's already mentioned this, but it's almost
certainly an explicit or implicit throttling on the remote server.
If you're pulling 7,000 pages from a single server you need to
be sure that you're within the Terms of Use of that service, or
at the least you need to contact the maintainers in courtesy to
confirm that this is acceptable.

If you don't you may well cause your IP block to be banned on
their network, which could affect others as well as yourself.

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


Re: dict order

2008-06-18 Thread cokofreedom
On Jun 18, 4:45 pm, Kirk Strauser <[EMAIL PROTECTED]> wrote:
> At 2008-06-18T10:32:48Z, [EMAIL PROTECTED] writes:
> > # untested 2.5
> > for keys in dict_one.items():
> >   if keys in dict_two:
> > if dict_one[keys] != dict_two[keys]:
> >   # values are different
> >   else:
> > # key is not present
>
> That fails if there is an item in dict_two that's not in dict_one.
> --
> Kirk Strauser
> The Day Companies

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


Re: go to specific line in text file

2008-06-18 Thread Larry Bates

Patrick David wrote:

Hello NG,

I am searching for a way to jump to a specific line in a text file, let's
say to line no. 9000.
Is there any method like file.seek() which leads me to a given line instead
of a given byte?

Hope for help
Patrick


Others have given the general answer (No), but if you are lucky enough to have 
FIXED length lines you can jump by just calculating the byte offset of the 
beginning character in line 9000 by using file.seek() and then read the bytes. 
You didn't say anything about the format of the file.


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


Re: Looking for lots of words in lots of files

2008-06-18 Thread Robert Bossy

brad wrote:
Just wondering if anyone has ever solved this efficiently... not 
looking for specific solutions tho... just ideas.


I have one thousand words and one thousand files. I need to read the 
files to see if some of the words are in the files. I can stop reading 
a file once I find 10 of the words in it. It's easy for me to do this 
with a few dozen words, but a thousand words is too large for an RE 
and too inefficient to loop, etc. Any suggestions?

The quick answer would be:
   grep -F -f WORDLIST FILE1 FILE2 ... FILE1000
where WORDLIST is a file containing the thousand words, one per line.

The more interesting answers would be to use either a suffix tree or an 
Aho-Corasick graph.


- The suffix tree is a representation of the target string (your files) 
that allows to search quickly for a word. Your problem would then be 
solved by 1) building a suffix tree for your files, and 2) search for 
each word sequentially in the suffix tree.


- The Aho-Corasick graph is a representation of the query word list that 
allows fast scanning of the words on a target string. Your problem would 
then be solved by 1) building an Aho-Corasick graph for the list of 
words, and 2) scan sequentially each file.


The preference for using either one or the other depends on some details 
of your problems: the expected size of target files, the rate of 
overlaps between words in your list (are there common prefixes), will 
you repeat the operation with another word list or another set of files, 
etc. Personally, I'd lean towards Aho-Corasick, it is a matter of taste; 
the kind of applications that comes to my mind makes it more practical.


Btw, the `grep -F -f` combo builds an Aho-Corasick graph. Also you can 
find modules for building both data structures in the python package index.


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


Re: Looking for lots of words in lots of files

2008-06-18 Thread Robert Bossy

I forgot to mention another way: put one thousand monkeys to work on it. ;)

RB

Robert Bossy wrote:

brad wrote:
Just wondering if anyone has ever solved this efficiently... not 
looking for specific solutions tho... just ideas.


I have one thousand words and one thousand files. I need to read the 
files to see if some of the words are in the files. I can stop 
reading a file once I find 10 of the words in it. It's easy for me to 
do this with a few dozen words, but a thousand words is too large for 
an RE and too inefficient to loop, etc. Any suggestions?

The quick answer would be:
   grep -F -f WORDLIST FILE1 FILE2 ... FILE1000
where WORDLIST is a file containing the thousand words, one per line.

The more interesting answers would be to use either a suffix tree or 
an Aho-Corasick graph.


- The suffix tree is a representation of the target string (your 
files) that allows to search quickly for a word. Your problem would 
then be solved by 1) building a suffix tree for your files, and 2) 
search for each word sequentially in the suffix tree.


- The Aho-Corasick graph is a representation of the query word list 
that allows fast scanning of the words on a target string. Your 
problem would then be solved by 1) building an Aho-Corasick graph for 
the list of words, and 2) scan sequentially each file.


The preference for using either one or the other depends on some 
details of your problems: the expected size of target files, the rate 
of overlaps between words in your list (are there common prefixes), 
will you repeat the operation with another word list or another set of 
files, etc. Personally, I'd lean towards Aho-Corasick, it is a matter 
of taste; the kind of applications that comes to my mind makes it more 
practical.


Btw, the `grep -F -f` combo builds an Aho-Corasick graph. Also you can 
find modules for building both data structures in the python package 
index.


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



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


Re: print problem

2008-06-18 Thread Peter Pearson
On Tue, 17 Jun 2008 04:46:38 -0300, Gabriel Genellina wrote:
> En Tue, 17 Jun 2008 04:10:41 -0300, Rich Healey escribió:
>> Gabriel Genellina wrote:
>>> En Tue, 17 Jun 2008 03:15:11 -0300, pirata <[EMAIL PROTECTED]> escribió:
>>>
 I was trying to print a dot on console every second to indicates
 running process, so I wrote, for example:

 for i in xrange(10):
 print ".",
 time.sleep(1)

 Idealy, a dot will be printed out each second. But there is nothing
 print out until after 10 seconds, all 10 dots come out together.

 I've tried lose the comma in the print statement, and it works.

 Is that because of the print statement buffer the characters until
 there is a new line character?
[snip]
>> Or just write to sys.stdout without the print wrapper..
>
> I think the output is still buffered, even if you write
> directly to sys.stdout, but I don't have a Linux box to
> test right now.

Here (Python 2.4.3), dots written to sys.stdout are saved up and
appear all at once.

However, you can flush the buffer yourself by invoking sys.stdout.flush()
immediately after either the "print" or the "sys.stdout.write".

-- 
To email me, substitute nowhere->spamcop, invalid->net.
--
http://mail.python.org/mailman/listinfo/python-list


Re: go to specific line in text file

2008-06-18 Thread Jonathan Gardner
On Jun 17, 3:10 am, Patrick David <[EMAIL PROTECTED]>
wrote:
>
> I am searching for a way to jump to a specific line in a text file, let's
> say to line no. 9000.
> Is there any method like file.seek() which leads me to a given line instead
> of a given byte?
>

As others have said, no. But if you're wondering how other file
formats do this, like BDB or PostgreSQL data files, which absolutely
have to jump to the magical spot in the file where the data is, they
keep an index at the beginning of the file that points to what byte
the data they are looking for is in.

So if it's really important to be able to do this, consider that.

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


Re: Looking for lots of words in lots of files

2008-06-18 Thread Martin P. Hellwig

Kris Kennaway wrote:



If you can't use an indexer, and performance matters, evaluate using 
grep and a shell script.  Seriously.


grep is a couple of orders of magnitude faster at pattern matching 
strings in files (and especially regexps) than python is.  Even if you 
are invoking grep multiple times it is still likely to be faster than a 
"maximally efficient" single pass over the file in python.  This 
realization was disappointing to me :)


Kris


Adding to this:
Then again, there is nothing wrong with wrapping grep from python and 
revert to a pure python 'solution' if the system has no grep.
Reinventing the wheel is usually only practical if the existing ones 
aren't round :-)


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


Re: 32 bit or 64 bit?

2008-06-18 Thread Peter Pearson
On Tue, 17 Jun 2008 08:13:40 -0400, Phil Hobbs wrote:
> [EMAIL PROTECTED] wrote:
[snip]
>> I have a physical system set up in which a body is supposed to
>> accelerate and to get very close to lightspeed, while never really
>> attaining it. After approx. 680 seconds, Python gets stuck and tells
>> me the object has passed lightspeed. I put the same equations in
>> Mathematica, again I get the same mistake around 680 seconds. So I
>> think, I have a problem with my model! Then I pump up the
>> WorkingPrecision in Mathematica to about 10. I run the same equations
>> again, and it works! At least for the first 10,000 seconds, the object
>> does not pass lightspeed.
>> I concluded that I need Python to work at a higher precision.
[snip]
> You need to change your representation.  Try redoing the algebra using 
> (c-v) as the independent variable, and calculate that.

Or represent the velocity as c*tanh(b), where b is the independent
variable.  If memory serves, this is the representation in which
constant acceleration corresponds to db/dt = constant.

-- 
To email me, substitute nowhere->spamcop, invalid->net.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does '!=' equivelent to 'is not' [drifting a little more]

2008-06-18 Thread Ethan Furman

Gabriel Genellina wrote:

(This thread is getting way above 1cp...)


What is 1cp?
--
Ethan



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


The best FREE porn on the Net

2008-06-18 Thread sexxxyy
http://rozrywka.yeba.pl/show.php?id=2737
--
http://mail.python.org/mailman/listinfo/python-list


The best FREE porn on the Net

2008-06-18 Thread sexxxyy
http://rozrywka.yeba.pl/show.php?id=2737
--
http://mail.python.org/mailman/listinfo/python-list


Re: Hrounding error

2008-06-18 Thread cooperq
On Jun 18, 8:02 am, "Jerry Hill" <[EMAIL PROTECTED]> wrote:
> On Wed, Jun 18, 2008 at 1:47 AM,  <[EMAIL PROTECTED]> wrote:
>  234 - 23234.2345
> > -23000.2344
>
> > This is not correct by my calculations.
>
> Python floating point operations use the underlying C floating point
> libraries which, in turn, usually rely on the hardware's floating
> point implementations.  This Wikipedia article talks about how those
> values are usually stored and 
> manipulated:http://en.wikipedia.org/wiki/IEEE_floating-point_standard
>
> So, which IEEE double precision floating point value would you like
> instead?  As far as I understand it, these are your two choices:
>
> 'ba490c020f76d6c0' = -23000.2344
> 'bb490c020f76d6c0' = -23000.23450002
>
> Alternatively, investigate the Decimal module, but keep in mind that
> it can have the same sorts of issues, just on different numbers.
> Compare, for instance:
>
> >>> (1.0/3.0) * 3.0
>
> 1.0
>
> >>> from decimal import Decimal
> >>> ( Decimal('1.0')/Decimal('3.0') ) * Decimal('3.0')
>
> Decimal("0.")
>
>
>
> --
> Jerry

So it seems then that python might not be very good for doing
precision floating point work, because there is a good chance its
floating points will be off by a (very small) amount?  Or is there a
way to get around this and be guaranteed an accurate answer?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Getting Python exit code when calling Python script from Java program

2008-06-18 Thread Matthew Woodcraft
In article <[EMAIL PROTECTED]>,
> I tried using the sys.exit() method in my script and passed non -zero
> values. However the value wasn't picked up the by Java
> Process.exitValue() method - it kept picking up 0. On investigation
> it turned out that the exit value being read is from python.exe
> process, not from the Python script.

I don't believe there is any such distinction. The exit status of
python.exe is the exit status determined by the script.

-M-

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


Re: Hrounding error

2008-06-18 Thread casevh
>
> So it seems then that python might not be very good for doing
> precision floating point work, because there is a good chance its
> floating points will be off by a (very small) amount?  Or is there a
> way to get around this and be guaranteed an accurate answer?- Hide quoted 
> text -
>

It's not a Python problem. That is just the behavior for floating-
point arithmetic.

casevh

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


Re: Looking for lots of words in lots of files

2008-06-18 Thread Jeff McNeil
On Jun 18, 10:29 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> brad wrote:
> > Just wondering if anyone has ever solved this efficiently... not looking
> > for specific solutions tho... just ideas.
>
> > I have one thousand words and one thousand files. I need to read the
> > files to see if some of the words are in the files. I can stop reading a
> > file once I find 10 of the words in it. It's easy for me to do this with
> > a few dozen words, but a thousand words is too large for an RE and too
> > inefficient to loop, etc. Any suggestions?
>
> Use an indexer, like lucene (available as pylucene) or a database that
> offers word-indices.
>
> Diez

I've been toying around with Nucular (http://nucular.sourceforge.net/)
a bit recently for some side projects. It's pure Python and seems to
work fairly well for my needs. I haven't pumped all that much data
into it, though.
--
http://mail.python.org/mailman/listinfo/python-list


ANN: eGenix mx Base Distribution 3.1.0

2008-06-18 Thread eGenix Team: M.-A. Lemburg



ANNOUNCING

eGenix.com mx Base Distribution

Version 3.1.0

  Open Source Python extensions providing important and useful
  services for Python programmers.


This announcement is also available on our web-site for online reading:
http://www.egenix.com/company/news/eGenix-mx-Base-Distribution-3.1.0-GA.html



ABOUT

The eGenix.com mx Base Distribution for Python is a collection of
professional quality software tools which enhance Python's usability
in many important areas such as fast text searching, date/time
processing and high speed data types.

The tools have a proven record of being portable across many Unix and
Windows platforms. You can write applications which use the tools on
Windows and then run them on Unix platforms without change due to the
consistent platform independent interfaces.

Contents of the distribution:

 * mxDateTime - Date/Time Library for Python
 * mxTextTools - Fast Text Parsing and Processing Tools for Python
 * mxProxy - Object Access Control for Python
 * mxBeeBase - On-disk B+Tree Based Database Kit for Python
 * mxURL - Flexible URL Data-Type for Python
 * mxUID - Fast Universal Identifiers for Python
 * mxStack - Fast and Memory-Efficient Stack Type for Python
 * mxQueue - Fast and Memory-Efficient Queue Type for Python
 * mxTools - Fast Everyday Helpers for Python

All available packages have proven their stability and usefulness in
many mission critical applications and various commercial settings all
around the world.

* About Python:
Python is an object-oriented Open Source programming language which
runs on all modern platforms (http://www.python.org/). By integrating
ease-of-use, clarity in coding, enterprise application connectivity
and rapid application design, Python establishes an ideal programming
platform for todays IT challenges.

* About eGenix:
eGenix is a consulting and software product company focused on
providing professional quality services and products to Python
users and developers (http://www.egenix.com/).



NEWS

The 3.1.0 release of the eGenix mx Base Distribution has a number
of enhancements over the previous version 3.0.0. Apart from a few
minor bug fixes, it provides a few new features:

Some highlights:

* mxTools now has a new mx.Tools.dlopen() function which allow
  loading shared libraries explicitly and from a specific
  path. This allows working around problems with not being able to
  dynamically set LD_LIBRARY_PATH on Unix platforms.

* mxTools can be configured to expose a new API called
  mx.Tools.setproctitle() which allows setting the process title
  on Unix platforms.

* mxBeeBase comes with a new on-disk dictionary version called
  BeeFixedLengthStringDict, which allows using keys with embedded
  \0 characters.

* mxSetup, our Python distutils extension, can now build prebuilt
  archives that no longer require the "... build --skip ..."
  command to skip the build process.  The uninstall command now
  also works for prebuilt archives and the bdist_prebuilt command
  has been enhanced to be able to build pure Python distributions
  as well.

* mxSetup now also works together with setuptools to e.g. build
  and install the packages as eggs. Run setup.py with
  --use-setuptools to enable this support.

For a more detailed description of changes, please see the respective
package documentation on our web-site.

As always, we are providing pre-compiled versions of the package for
the most popular Python platforms. For all others, you can compile the
package from source using "python setup.py install".



DOWNLOADS

The download archives and instructions for installing the packages can
be found on the eGenix mx Base Distribution page:

http://www.egenix.com/products/python/mxBase/



LICENSE

The eGenix mx Base package is distributed under the eGenix.com Public
License 1.1.0 which is a CNRI Python License style Open Source
license.  You can use the package in both commercial and
non-commercial settings without fee or charge.

The package comes with full source code



SUPPORT

Commercial support for these packages is available from eGenix.com.
Please see

http://www.egenix.com/services/support/

for details about our support offerings.

Enjoy,
--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Jun 18 2008)

Python/Zope Consulting and Support ...http://www.egenix.com/
mxODBC.Zope.Database.Adapter ...   

Importing module PIL vs beautifulSoup.

2008-06-18 Thread bsagert
I downloaded BeautifulSoup.py from http://www.crummy.com/software/BeautifulSoup/
and being a n00bie, I just placed it in my Windows c:\python25\lib\
file. When I type "import beautifulsoup" from the interactive prompt
it works like a charm. This seemed too easy in retrospect.

Then I downloaded the PIL (Python Imaging Library) module from
http://www.pythonware.com/products/pil/. Instead of a simple file that
BeautifulSoup sent me, PIL is an .exe that installed itself in c:
\python25\lib\site-packages\PIL\. However it won't load by typing
"import pil".

I know I am supposed to RTFM, but a Google search has not led to the
holy grail that Monty Python found. I realize that PIL is a package as
opposed to a simple script (and it does not include a readme file).
Thanks in advance for any help.
--
http://mail.python.org/mailman/listinfo/python-list


www.nikeadishoes.com

2008-06-18 Thread [EMAIL PROTECTED]
because our company open no long time,and we have some pro in deals
with oredr

please make you new order to us ,i think we will have a good
beginning !!

Our website: www.nikeadishoes.com

Choose your favorite products

please trust us ,have a good beginning

Email:[EMAIL PROTECTED]

MSN:[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Importing module PIL vs beautifulSoup.

2008-06-18 Thread Duncan Booth
[EMAIL PROTECTED] wrote:

> I downloaded BeautifulSoup.py from
> http://www.crummy.com/software/BeautifulSoup/ and being a n00bie, I
> just placed it in my Windows c:\python25\lib\ file. When I type
> "import beautifulsoup" from the interactive prompt it works like a
> charm. This seemed too easy in retrospect. 

It might be better if you put the file in \python25\lib\site-packages\
The same import will still work, but you probably want to avoid putting 
non-core files directly in \python25\lib.

Also, it sounds like you renamed the file: "import beautifulsoup" should 
fail (the file is supposed to be called BeautifulSoup.py). If you want to 
be able to install other software which has been written to use 
BeautifulSoup you'll need to make sure the case of the filename is correct.

> 
> Then I downloaded the PIL (Python Imaging Library) module from
> http://www.pythonware.com/products/pil/. Instead of a simple file that
> BeautifulSoup sent me, PIL is an .exe that installed itself in c:
> \python25\lib\site-packages\PIL\. However it won't load by typing
> "import pil".
> 
> I know I am supposed to RTFM, but a Google search has not led to the
> holy grail that Monty Python found. I realize that PIL is a package as
> opposed to a simple script (and it does not include a readme file).
> Thanks in advance for any help.
> 

Did you try "import PIL"? All module and package names in Python are case 
sensitive.
--
http://mail.python.org/mailman/listinfo/python-list


How to split a string containing nested commas-separated substrings

2008-06-18 Thread Robert Dodier
Hello,

I'd like to split a string by commas, but only at the "top level" so
to speak. An element can be a comma-less substring, or a
quoted string, or a substring which looks like a function call.
If some element contains commas, I don't want to split it.

Examples:

'foo, bar, baz' => 'foo' 'bar' 'baz'
'foo, "bar, baz", blurf' => 'foo' 'bar, baz' 'blurf'
'foo, bar(baz, blurf), mumble' => 'foo' 'bar(baz, blurf)' 'mumble'

Can someone suggest a suitable regular expression or other
method to split such strings?

Thank you very much for your help.

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


SPAM

2008-06-18 Thread Dantheman
SPAM
--
http://mail.python.org/mailman/listinfo/python-list


Never mind folks, n00bie here forgot Python is case sensitive!

2008-06-18 Thread bsagert
On Jun 18, 10:18 am, Duncan Booth <[EMAIL PROTECTED]>
wrote:
> [EMAIL PROTECTED] wrote:
> > I downloaded BeautifulSoup.py from
> >http://www.crummy.com/software/BeautifulSoup/and being a n00bie, I
> > just placed it in my Windows c:\python25\lib\ file. When I type
> > "import beautifulsoup" from the interactive prompt it works like a
> > charm. This seemed too easy in retrospect.
>
> It might be better if you put the file in \python25\lib\site-packages\
> The same import will still work, but you probably want to avoid putting
> non-core files directly in \python25\lib.
>
> Also, it sounds like you renamed the file: "import beautifulsoup" should
> fail (the file is supposed to be called BeautifulSoup.py). If you want to
> be able to install other software which has been written to use
> BeautifulSoup you'll need to make sure the case of the filename is correct.
>
>
>
> > Then I downloaded the PIL (Python Imaging Library) module from
> >http://www.pythonware.com/products/pil/. Instead of a simple file that
> > BeautifulSoup sent me, PIL is an .exe that installed itself in c:
> > \python25\lib\site-packages\PIL\. However it won't load by typing
> > "import pil".
>
> > I know I am supposed to RTFM, but a Google search has not led to the
> > holy grail that Monty Python found. I realize that PIL is a package as
> > opposed to a simple script (and it does not include a readme file).
> > Thanks in advance for any help.
>
> Did you try "import PIL"? All module and package names in Python are case
> sensitive.

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


Re: Importing module PIL vs beautifulSoup.

2008-06-18 Thread bsagert
On Jun 18, 10:18 am, Duncan Booth <[EMAIL PROTECTED]>
wrote:
> [EMAIL PROTECTED] wrote:
> > I downloaded BeautifulSoup.py from
> >http://www.crummy.com/software/BeautifulSoup/and being a n00bie, I
> > just placed it in my Windows c:\python25\lib\ file. When I type
> > "import beautifulsoup" from the interactive prompt it works like a
> > charm. This seemed too easy in retrospect.
>
> It might be better if you put the file in \python25\lib\site-packages\
> The same import will still work, but you probably want to avoid putting
> non-core files directly in \python25\lib.
>
> Also, it sounds like you renamed the file: "import beautifulsoup" should
> fail (the file is supposed to be called BeautifulSoup.py). If you want to
> be able to install other software which has been written to use
> BeautifulSoup you'll need to make sure the case of the filename is correct.
>
>
>
> > Then I downloaded the PIL (Python Imaging Library) module from
> >http://www.pythonware.com/products/pil/. Instead of a simple file that
> > BeautifulSoup sent me, PIL is an .exe that installed itself in c:
> > \python25\lib\site-packages\PIL\. However it won't load by typing
> > "import pil".
>
> > I know I am supposed to RTFM, but a Google search has not led to the
> > holy grail that Monty Python found. I realize that PIL is a package as
> > opposed to a simple script (and it does not include a readme file).
> > Thanks in advance for any help.
>
> Did you try "import PIL"? All module and package names in Python are case
> sensitive.

YIKES, Python is case sensitive! I knew that, says he blushing. Now it
works. Thanks Duncan. Ciao, Bill
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ternary operator alternative in Ptyhon

2008-06-18 Thread jeremie fouche

kretik a écrit :
I'm sure this is a popular one, but after Googling for a while I 
couldn't figure out how to pull this off.


I'd like to short-circuit the assignment of class field values passed in 
this dictionary to something like this:


self.SomeField = \
params.has_key("mykey") ? params["mykey"] : None)

Obviously I know this is not actual Python syntax, but what would be the 
equivalent? I'm trying to avoid this, basically:


if params.has_key("mykey"):
self.SomeField = params["mykey"]
else:
self.SomeField = None

This is not a big deal of course, but I guess my main goal is to try and 
figure out of I'm not missing something more esoteric in the language 
that lets me do this.


You can also use :
self.SomeField = params.has_key("mykey") and params["mykey"] or None

But it's not easy to read
--
Jérémie
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to split a string containing nested commas-separated substrings

2008-06-18 Thread Matimus
On Jun 18, 10:19 am, Robert Dodier <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I'd like to split a string by commas, but only at the "top level" so
> to speak. An element can be a comma-less substring, or a
> quoted string, or a substring which looks like a function call.
> If some element contains commas, I don't want to split it.
>
> Examples:
>
> 'foo, bar, baz' => 'foo' 'bar' 'baz'
> 'foo, "bar, baz", blurf' => 'foo' 'bar, baz' 'blurf'
> 'foo, bar(baz, blurf), mumble' => 'foo' 'bar(baz, blurf)' 'mumble'
>
> Can someone suggest a suitable regular expression or other
> method to split such strings?
>
> Thank you very much for your help.
>
> Robert

You might look at the shlex module. It doesn't get you 100%, but its
close:

>>> shlex.split('foo, bar, baz')
['foo,', 'bar,', 'baz']
>>> shlex.split( 'foo, "bar, baz", blurf')
['foo,', 'bar, baz,', 'blurf']
>>> shlex.split('foo, bar(baz, blurf), mumble')
['foo,', 'bar(baz,', 'blurf),', 'mumble']

Using a RE will be tricky, especially if it is possible to have
recursive nesting (which by definition REs can't handle). For a real
general purpose solution you will need to create a custom parser.
There are a couple modules out there that can help you with that.

pyparsing is one: http://pyparsing.wikispaces.com/

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


Re: Multiprecision arithmetic library question.

2008-06-18 Thread Michael Press
In article <[EMAIL PROTECTED]>,
 Mark Wooding <[EMAIL PROTECTED]> wrote:

> Michael Press <[EMAIL PROTECTED]> wrote:
> 
> > I already compiled and installed the GNU multiprecision library
> > on Mac OS X, and link to it in C programs. 
> > How do I link to the library from Python? 
> 
> You know that Python already supports multiprecision integer arithmetic,
> right?  If you desperately want GMP, though, there's the gmpy module
> (q.g.).

No, I do not know that. Define desperate. 
Does Python support the extended Euclidean algorithm
and other number theory functions?
How fast does Python multiply?
Not that the latter is particularly important,
as C is built for speed.

I've been fooling around. Ran dir(gmpy), and 
it does not show the full complement of GMP
library functions, such as the various division
functions. e.g. mpz_tdiv_qr.

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


Re: How to split a string containing nested commas-separated substrings

2008-06-18 Thread Cédric Lucantis
Hi,

Le Wednesday 18 June 2008 19:19:57 Robert Dodier, vous avez écrit :
> Hello,
>
> I'd like to split a string by commas, but only at the "top level" so
> to speak. An element can be a comma-less substring, or a
> quoted string, or a substring which looks like a function call.
> If some element contains commas, I don't want to split it.
>
> Examples:
>
> 'foo, bar, baz' => 'foo' 'bar' 'baz'
> 'foo, "bar, baz", blurf' => 'foo' 'bar, baz' 'blurf'
> 'foo, bar(baz, blurf), mumble' => 'foo' 'bar(baz, blurf)' 'mumble'
>
> Can someone suggest a suitable regular expression or other
> method to split such strings?
>

I'd do something like this (note that it doesn't check for quote/parenthesis 
mismatch and removes _all_ the quotes) :

def mysplit (string) :
pardepth = 0
quote = False
ret = ['']

for car in string :

if car == '(' : pardepth += 1
elif car == ')' : pardepth -= 1
elif car in ('"', "'") :
quote = not quote
car = '' # just if you don't want to keep the quotes

if car in ', ' and not (pardepth or quote) :
if ret[-1] != '' : ret.append('')
else :
ret[-1] += car

return ret

# test
for s in ('foo, bar, baz',
  'foo, "bar, baz", blurf',
  'foo, bar(baz, blurf), mumble') :
print "'%s' => '%s'" % (s, mysplit(s))

# result
'foo, bar, baz' => '['foo', 'bar', 'baz']'
'foo, "bar, baz", blurf' => '['foo', 'bar, baz', 'blurf']'
'foo, bar(baz, blurf), mumble' => '['foo', 'bar(baz, blurf)', 'mumble']'


-- 
Cédric Lucantis
--
http://mail.python.org/mailman/listinfo/python-list


Function argument conformity check

2008-06-18 Thread dlists . cad
Hi. I am looking for a way to check if some given set of (*args,
**kwds) conforms to the argument specification of a given function,
without calling that function.

For example, given the function foo:
def foo(a, b, c): pass

and some tuple args and some dict kwds, is there a way to tell if i
_could_ call foo(*args, **kwds) without getting an exception for those
arguments? I am hoping there is a way to do this without actually
writing out the argument logic python uses.

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


Re: Numeric type conversions

2008-06-18 Thread Lie
On Jun 18, 12:23 am, John Dann <[EMAIL PROTECTED]> wrote:
> On Tue, 17 Jun 2008 08:58:11 -0700 (PDT), MRAB
>
> <[EMAIL PROTECTED]> wrote:
> >[snip]
> >Please note that in slicing the start position is included and the end
> >position is excluded, so that should be ByteStream[12:14].
>
> Yes, I just tripped over that, in fact, hence the error in my original
> post. I suppose there must be some logic in including the start
> position but excluding the end position, though it does escape me for
> now. I can understand making a range inclusive or exclusive but not a
> mixture of the two. Suppose it's just something you have to get used
> to with Python and, no doubt, much commented on in the past.
>
> JGD

There is actually a logic to that. It's explained in the help/tutorial
file, that you should think about the index as a cursor, the index
itself doesn't point to the item itself, but to the interval between
the item.

(read this on a monospace font, or it'll look screwed up)
0   1   2   3   4   5
+---+
| A | B | C | D | E |
+---+
-5  -4  -3  -2  -1  0

So to get BCD, you say [1:4]
--
http://mail.python.org/mailman/listinfo/python-list


Re: 32 bit or 64 bit?

2008-06-18 Thread [EMAIL PROTECTED]
On Jun 17, 5:04 pm, "Richard Brodie" <[EMAIL PROTECTED]> wrote:
> <[EMAIL PROTECTED]> wrote in message
>
> news:[EMAIL PROTECTED]
>
> >That was suggested. Problem is, that sometimes the velocities are near
> >zero. So this solution, by itself, is not general enough.
>
> Maybe working in p, and delta-p would be more stable.

That's a good one. It will, however, involve complicated calculations
for obtaining v from p, so it might be slower than mpmath. I'll
consider it.
--
http://mail.python.org/mailman/listinfo/python-list


Re: 32 bit or 64 bit?

2008-06-18 Thread [EMAIL PROTECTED]
On Jun 18, 7:12 pm, Peter Pearson <[EMAIL PROTECTED]> wrote:
> On Tue, 17 Jun 2008 08:13:40 -0400, Phil Hobbs wrote:
> > [EMAIL PROTECTED] wrote:
> [snip]
> >> I have a physical system set up in which a body is supposed to
> >> accelerate and to get very close to lightspeed, while never really
> >> attaining it. After approx. 680 seconds, Python gets stuck and tells
> >> me the object has passed lightspeed. I put the same equations in
> >> Mathematica, again I get the same mistake around 680 seconds. So I
> >> think, I have a problem with my model! Then I pump up the
> >> WorkingPrecision in Mathematica to about 10. I run the same equations
> >> again, and it works! At least for the first 10,000 seconds, the object
> >> does not pass lightspeed.
> >> I concluded that I need Python to work at a higher precision.
> [snip]
> > You need to change your representation.  Try redoing the algebra using
> > (c-v) as the independent variable, and calculate that.
>
> Or represent the velocity as c*tanh(b), where b is the independent
> variable.  If memory serves, this is the representation in which
> constant acceleration corresponds to db/dt = constant.
>
> --
> To email me, substitute nowhere->spamcop, invalid->net.

See my comment to Brodie.
--
http://mail.python.org/mailman/listinfo/python-list


Re: 32 bit or 64 bit?

2008-06-18 Thread [EMAIL PROTECTED]
On Jun 18, 3:02 am, Phil Hobbs
<[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
>
> > That was suggested. Problem is, that sometimes the velocities are near
> > zero. So this solution, by itself, is not general enough.
>
> Are you sure?  I sort of doubt that you're spending zillions of
> iterations getting closer and closer to zero.   It would be worth
> actually doing the error analysis and finding out.
>
> Cheers,
>
> Phil Hobbs


See my comment to Brodie.
--
http://mail.python.org/mailman/listinfo/python-list


Re: 32 bit or 64 bit?

2008-06-18 Thread [EMAIL PROTECTED]
On Jun 18, 10:00 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> On Jun 18, 3:02 am, Phil Hobbs
>
> <[EMAIL PROTECTED]> wrote:
> > [EMAIL PROTECTED] wrote:
>
> > > That was suggested. Problem is, that sometimes the velocities are near
> > > zero. So this solution, by itself, is not general enough.
>
> > Are you sure?  I sort of doubt that you're spending zillions of
> > iterations getting closer and closer to zero.   It would be worth
> > actually doing the error analysis and finding out.
>
> > Cheers,
>
> > Phil Hobbs
>
> See my comment to Brodie.

Oops, the "See my comment to Brodie" was not intended to you.
I meant to say:

I might try your suggestion, but for now mpmath will do. Now I'm
working on implementing the electromagnetic force, so I'll get back to
this issue later.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to split a string containing nested commas-separated substrings

2008-06-18 Thread Paul McGuire
On Jun 18, 12:19 pm, Robert Dodier <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I'd like to split a string by commas, but only at the "top level" so
> to speak. An element can be a comma-less substring, or a
> quoted string, or a substring which looks like a function call.
> If some element contains commas, I don't want to split it.
>
> Examples:
>
> 'foo, bar, baz' => 'foo' 'bar' 'baz'
> 'foo, "bar, baz", blurf' => 'foo' 'bar, baz' 'blurf'
> 'foo, bar(baz, blurf), mumble' => 'foo' 'bar(baz, blurf)' 'mumble'
>
> Can someone suggest a suitable regular expression or other
> method to split such strings?
>
> Thank you very much for your help.
>
> Robert

tests = """\
foo, bar, baz
foo, "bar, baz", blurf
foo, bar(baz, blurf), mumble""".splitlines()


from pyparsing import Word, alphas, alphanums, Optional, \
Group, delimitedList, quotedString

ident = Word(alphas+"_",alphanums+"_")
func_call = Group(ident + "(" + Optional(Group(delimitedList(ident)))
+ ")")

listItem = func_call | ident | quotedString

for t in tests:
print delimitedList(listItem).parseString(t).asList()


Prints:

['foo', 'bar', 'baz']
['foo', '"bar, baz"', 'blurf']
['foo', ['bar', '(', ['baz', 'blurf'], ')'], 'mumble']


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


Re: Function argument conformity check

2008-06-18 Thread Cédric Lucantis
Hi,

Le Wednesday 18 June 2008 20:19:12 [EMAIL PROTECTED], vous avez écrit :
> Hi. I am looking for a way to check if some given set of (*args,
> **kwds) conforms to the argument specification of a given function,
> without calling that function.
>
> For example, given the function foo:
> def foo(a, b, c): pass
>
> and some tuple args and some dict kwds, is there a way to tell if i
> _could_ call foo(*args, **kwds) without getting an exception for those
> arguments? I am hoping there is a way to do this without actually
> writing out the argument logic python uses.
>

Each function object is associated to a code object which you can get with 
foo.func_code. Two of this object's attributes will help you: co_argcount and 
co_varnames. The first is the number of arguments of the function, and the 
second a list of all the local variables names, including the arguments 
(which are always the first items of the list). When some arguments have 
default values, they are stored in foo.func_defaults (and these arguments are 
always after non-default args in the co_argnames list). 

Finally, it seems that some flags are set in code.co_flags if the function 
accepts varargs like *args, **kwargs, but I don't know where these are 
defined.

Note that I never found any doc about that and merely guessed it by playing 
with func objects, so consider all this possibly wrong or subject to change.

-- 
Cédric Lucantis
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   >