Re: Network game using mysql

2009-04-15 Thread Kushal Kumaran
2009/4/15 João Abrantes :
> Good evening,
>
> I am making an online game that stores its data in a mysql database. The
> thing is that I can't allow the players to interact directly with the
> important tables of database (they could cheat if I give them access) so I
> only allow them to write on a table named commands and then a python program
> running on the server will interpreter that command and update the important
> tables. For example, a user that wants to build an house writes on the
> commands table 'build house', and then my python program takes the resources
> of the user and gives him an house.  The thing is that I need to have a
> program reading the command list in a infinite while loop and I don't know
> if there is a better way to do this, I am afraid that if the command list
> gets to crowed my python program may not handle all the commands in time.
> And if I put two programs reading the same command list sometimes they
> handle the command twice..
>

It does seem like you need a queue, with as many readers as required
depending on load.  Take a look at pyprocessing.  I think it is
included in the standard library as of Python 2.6, with the name
multiprocessing.

Do the clients connect directly to your database over the network?

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


Re: Beginner: Portable Python, BeautifulSoup & ScrapeNFeed

2009-04-15 Thread Brian
On Ubuntu:

sudo apt-get install python-pyrss2gen python-beautifulsoup # download
ScrapeNFeed

Python:
Not sure what's wrong with this but it's most of the code you'll need:
---
from urllib import urlopen
from BeautifulSoup import BeautifulSoup
from PyRSS2Gen import RSSItem, Guid
import ScrapeNFeed
import re

url='http://jobs.spb.ca.gov/exams_title.cfm'
job_html = urlopen(url).read()
job_soup = BeautifulSoup(job_html)
jobs = job_soup.findAll('strong', text=re.compile('.*RESEARCH.*'))

class JobFeed(ScrapeNFeed.ScrapedFeed):
def HTML2RSS(self, headers, body):
items = [RSSItem(title=job,
 link=url,
 description=job_soup.h2.string.strip())
 for job in jobs]

self.addRSSItems(jobs)

JobFeed.load(job_soup.title.string.strip(),
 url,
 'jobs.rss',
 'jobs.pickle',
 managingEditor='',
 )






On Tue, Apr 14, 2009 at 4:17 PM, Joe Larson  wrote:

> Hello list!
>
> I am a Python Beginner. I thought a good beginning project would be to use
> the Portable Python environment http://www.portablepython.com/ with
> Beautiful Soup http://www.crummy.com/software/BeautifulSoup/ and Scrape
> 'N' Feed http://www.crummy.com/software/ScrapeNFeed/ to create and RSS
> feed of this page http://jobs.spb.ca.gov/exams_title.cfm - ideally
> filtering just for positions with the string 'Research Analyst'.
>
> In my day job I work on the Windows OS (hence the Portable Python) - at
> home I use Ubuntu but also carry Portable Ubuntu as well.
>
> I just wanted to shoot this to the list - see if anyone had any suggestions
> or tips. I'm reading O'Reilly's Learning Python and The Python Tutorial, but
> it's still very challenging as this is my first programming language. Thanks
> all! Sincerely ~ joelar
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Measuring time of a telnet connect

2009-04-15 Thread Fabian Lanze
Hi!

I want to realize the following: I want to measure the time it takes to
build up a telnet connection to a known host (ip/port). I tried to use
subprocess.Popen to build the telnet connection and then to catch the
"Trying to connect..." and "Connection established" output. But this
didn't work - both had always the same timestamp (time.time()) - i think
that's due to some buffers.

Has anyone an idea how to solve this?

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


Re: Using Python after a few years of Ruby

2009-04-15 Thread Tim Hoffman
Hi


For a rake alternative have a look at aap http://www.a-a-p.org/
It won't be a drop in replacement but, does provide similiar
functionality
( i have used to drive delphi, visualstudio, borland c and bunch of
other stuff
build scripts to make a complete windows desktop app) and
automagically build and deploy
60 custom plone sites - in fact all sorts of things.

There are plenty of python web frameworks, some have quite different
approaches,
what suits you will depend very much on your own bias, interest.

T


On Apr 14, 3:01 pm, [email protected] wrote:
> Although I'm not 100% new to Python, most of my experience using high-
> level languages is with Ruby. I had a job doing Rails web development
> a little ways back and I really enjoyed it. At my current workplace
> though, we're looking at using Python and I'm trying to get back into
> the Python "groove" as it were.
>
> I've got plenty of materials to get me up to speed on the mechanics of
> the language, but I was wondering about the equivalent of some tools I
> was used to using in Ruby. If there's not anything that's a one-to-one
> equivalent I totally understand, I'm just looking for some pointers
> here to get me started. :)
>
> 1) Rake - is there an equivalent of Rake? I've seen a bit about SCons,
> and it looks really nice, but it seems geared towards being a Make
> replacement for C/C++ rather than something that's used to work with
> Python itself. Is there anything like a Python build tool? (Or do I
> even need something like that? I haven't worked with any large Python
> systems, just little things here and there.)
>
> 2) Gems - I've seen a bit about Eggs, but they don't seem to have
> anywhere near the official status gems do for Ruby. Are there any
> "package management" things like this for Python, or do you usually
> just grab the code you need as-is?
>
> 3) Web frameworks - yeah, I realize there are tons of these, but are
> TurboGears, Django, and Zope still the big ones? I've seen a lot about
> Pylons, is that a separate framework or is it a ... well, frame that
> other things are built on? (TG seems to be related to Pylons at a
> glance?)
>
> 4) Unit Test frameworks - If there's a behavioral test suite like
> RSpec that's be awesome, but I'd be happy to settle for a good, solid
> unit testing system.
>
> Thanks for any advice!

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


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-15 Thread M.-A. Lemburg
On 2009-04-15 02:32, P.J. Eby wrote:
> At 10:59 PM 4/14/2009 +0200, M.-A. Lemburg wrote:
>> You are missing the point: When breaking up a large package that lives in
>> site-packages into smaller distribution bundles, you don't need namespace
>> packages at all, so the PEP doesn't apply.
>>
>> The way this works is by having a base distribution bundle that includes
>> the needed __init__.py file and a set of extension bundles the add
>> other files to the same directory (without including another copy of
>> __init__.py). The extension bundles include a dependency on the base
>> package to make sure that it always gets installed first.
> 
> If we're going to keep that practice, there's no point to having the
> PEP: all three methods (base+extensions, pkgutil, setuptools) all work
> just fine as they are, with no changes to importing or the stdlib.

Again: the PEP is about creating a standard for namespace
packages. It's not about making namespace packages easy to use for
Linux distribution maintainers. Instead, it's targeting *developers*
that want to enable shipping a single package in multiple, separate
pieces, giving the user the freedom to the select the ones she needs.

Of course, this is possible today using various other techniques. The
point is that there is no standard for namespace packages and that's
what the PEP is trying to solve.

> In particular, without the feature of being able to drop that practice,
> there would be no reason for setuptools to adopt the PEP.  That's why
> I'm -1 on your proposal: it's actually inferior to the methods we
> already have today.

It's simpler and more in line with the Python Zen, not inferior.

You are free not to support it in setuptools - the methods
implemented in setuptools will continue to work as they are,
but continue to require support code and, over time, no longer
be compatible with other tools building upon the standard
defined in the PEP.

In the end, it's the user that decides: whether to go with a
standard or not.

-- 
Marc-Andre Lemburg
eGenix.com

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

2009-03-19: Released mxODBC.Connect 1.0.1  http://python.egenix.com/

::: Try our new mxODBC.Connect Python Database Interface 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://www.egenix.com/company/contact/
--
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess: reading from stdout hangs process termination, waiting for ENTER keyboard signal

2009-04-15 Thread giohappy
On 14 Apr, 18:52, MRAB  wrote:
> giohappywrote:
> > Hello everyone.
> > I'm trying to use subprocess module to launch a Windows console
> > application. The application prints some results to standard output
> > and then waits for the user to press any key to terminte. I can't
> > control this behaviour, as the application is not mine...
> > I'm stuck at the very first lines of my code. I'm trying to force
> > process termination (even with proc.terminate()), and it works only if
> > I don't read from stdout. If I do proc.stdout.read() the process
> > hangs, and I have to manually press the keyboard to interrupt it.
> > Probably it's due a low-level handle that is kept on the process
> > stdout, waiting for the keypress event...
>
> > How can I solve it?
> > Giovanni
>
> > --- Code excerpt---
>
> > proc = subprocess.Popen('the_app.exe',
> >                        shell=True,
> >                        stdout=subprocess.PIPE,
> >                        )
> > #stdout_value = proc.communicate()[0]
> > stdout_value = proc.stdout.read()
> > PROCESS_TERMINATE = 1
> > handle = win32api.OpenProcess(PROCESS_TERMINATE, False, proc.pid)
> > win32api.TerminateProcess(handle, -1)
> > win32api.CloseHandle(handle)
> > print stdout_value
>
> Try this:
>
> proc = subprocess.Popen('the_app.exe',
>                         shell=True,
>                         stdin=subprocess.PIPE,
>                         stdout=subprocess.PIPE,
>                         )
> stdout_value = proc.communicate("\n")[0]

MRAB, I've tried that too but no result... I still have to press a
keybord key to terminate (the classical "Press any key to continue")
--
http://mail.python.org/mailman/listinfo/python-list


Re: Modifying the value of a float-like object

2009-04-15 Thread Peter Otten
[email protected] wrote:

> Now, I would like to get the uncertainty on the result, even though we
> have no idea of what quantities are used in lattice_spacing() for the
> calculation (it can be attribute that are floats, attributes that are
> FloatWithUncert, module globals defined as FloatWithUncert, etc.).

I think this is your main problem. What you describe is a source of hard to
find bugs and a maintenance nightmare.

I also don't understand what you want to do with the results. Consider a
simple example

a = 1 +- 0.1
b = 1 +- 0.1

Now let's calculate a + b. With my approach you get

a + b = 2 +- 0.14

i. e. the result falls in the interval 1.86...2.14 with the same likelihood
that a and b are within 0.9...1.1. You don't get 2 +- 0.2 because the
errors sometimes cancel out.

With your approach you get (some permutation of)

a + b = [1.9, 1.9, 2.0, 2.0, 2.1, 2.1]

What is that supposed to mean? Let's assume you have one additional variable

c = 42 +- 7 # whatever

Your result will become

a + b = [1.9, 1.9, 2.0, 2.0, 2.0, 2.0, 2.0, 2.1, 2.1]

Hmm...

Here's the code I used to get that result:

class F(object):
all = []
def __init__(self, value, err):
self.value = value
self.err = err
self.shift = 0
self.all.append(self)

def __float__(self):
return self.value + self.err * self.shift

def __add__(self, other):
return float(self) + float(other)
__radd__ = __add__
def __mul__(self, other):
return float(self) * float(other)
__rmul__ = __mul__

def __str__(self):
return "F(%s)" % float(self)

a = F(1.0, 0.1)
b = F(1.0, 0.1)
unused = F(42.0, 7.0)

results = []
for f in F.all:
for f.shift in [-1, 0, 1]:
print "a = %s, b = %s, a+b = %s" % (a, b, a + b)
results.append(a+b)
f.shift = 0
print "[%s]" % ", ".join("%.1f" % r for r in sorted(results))

If you add enough arithmetic operations it might even "work" with your
codebase.

Peter

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


Re: Measuring time of a telnet connect

2009-04-15 Thread Jack diederich
On Wed, Apr 15, 2009 at 3:41 AM, Fabian Lanze
 wrote:
>
> I want to realize the following: I want to measure the time it takes to
> build up a telnet connection to a known host (ip/port). I tried to use
> subprocess.Popen to build the telnet connection and then to catch the
> "Trying to connect..." and "Connection established" output. But this
> didn't work - both had always the same timestamp (time.time()) - i think
> that's due to some buffers.
>
> Has anyone an idea how to solve this?

import time
import telnetlib

start = time.time()
conn = telnetlib.Telnet('localhost', 80)
print time.time() - start

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


Re: Modifying the value of a float-like object

2009-04-15 Thread Eric . Le . Bigot
Ben F., you're right on the money!  You expressed exactly what I'm
looking for.  Why should I want this?  because the place in the code
where (foo, baz) is calculated has _no idea_ of what foo and baz are,
of where they were defined, etc.; on the other hand, the floatref
class can keep track of the values that were constructed, and I'm
looking for a way of modifying them from its perspective
(floatref.change_value(index, new_value)).

Steven, t

To Steven: Thank you for your suggestion about checking
UserString.MutableString.
I do understand that mutable objects can be surprising, but, again,
Python programmers constantly use such objects (lists, dicts,): I
don't think that manipulating a mutable float is more of a problem
than using a list, for a Python programmer.
As for your idea of "straight-forward interval arithmetic", it's a
good one, but I'd have to redefine lots of functions from the math
module, to use them explicitly in my code, etc.: this is heavy; I was
looking for a light-weight alternative, where all calculations are
expressed in Python as if all numbers were regular floats, and where
you don't have to redefine any mathematical operation.  In other
words, I'm looking for a robust way of implementing the derive()
function of Peter without knowing anything about which function uses
which "numbers with an uncertainty": the equivalent of Peter's derive
() would simply successively change all "numbers with uncertainty"
defined so far and see how the result of a given calculation varies--
the variables that are not used in the calculated expression don't
change the result, for instance.  I understand that this is
computationally inefficient (too many variables might be changed), but
I don't need speed, only robustness in the propagation of errors, and
a simple and very legible calculation code, and ideally with only a
few lines of Python.

Anyway, Steven gave me hope that some kind of mutable float is
possible in Python, and Ben perfectly expressed my need.  Again,
alternatively, any module that handles in a convenient manner
calculations with error propagation would be great, but I have not
found any single such module!


On Apr 15, 6:17 am, Ben Finney  wrote:
> Steven D'Aprano  writes:
> > On Tue, 14 Apr 2009 06:03:58 -0700, Eric.Le.Bigot wrote:
> > > The goal is to maintain a list [x, y,…] of these float-like
> > > objects, and to modify their value on the fly (with something like
> > > x.value = 3.14) so that any expression like "x
> > > +y" uses the new value.
>
> > Why is that the goal? If you want to change the value of x, just
> > change the value of x.
>
> As you're aware (but Eric may not be), Python doesn't have “change the
> value of x”. The closest would be to re-bind the name ‘x’ to a different
> value, which would not be what Eric is asking for.
>
> If I understand correctly, Eric wants something with the following
> behaviour:
>
>     >>> foo = [floatref(3.14), floatref(1.41)]
>     >>> bar = foo[0]
>     >>> baz = foo[1]
>     >>> foo
>     [3.14, 1.41]
>     >>> (bar, baz)
>     (3.14, 1.41)
>
>     >>> foo[1].changevalue(1.62)
>     >>> foo
>     [3.14, 1.62]
>     >>> (bar, baz)
>     (3.14, 1.62)
>
> and is asking how to get such a ‘floatref’.
>
> --
>  \        “If you go parachuting, and your parachute doesn't open, and |
>   `\        you friends are all watching you fall, I think a funny gag |
> _o__)             would be to pretend you were swimming.” —Jack Handey |
> Ben Finney

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


Re: how to know argument name with which a function of extended c called

2009-04-15 Thread rahul
On Apr 14, 6:24 pm, John Machin  wrote:
> On Apr 14, 10:35 pm, rahul  wrote:
>
> > Hi,
> >   i need to write a 'c extension function' in this function i need to
> > change argument value with  which this function called.
>
> The appropriate way for a function to give output is to return a
> value, or a tuple of values.
>
> example:
>
> def get_next_token(input_buffer, offset):
>    """get next lexical token, starting at offset
>       return (the_token, new offset)"""
>    length = find_len_of_token_somehow(input_buffer, offset)
>    new_offset = offset + length
>    return input_buffer[offset:new_offset], new_offset
>
> and you can call it by
>    token, pos = get_next_token(buff, pos)
>    return input
>
> >   ie,
> >          if a python code like
> >             import changeValue as c
> >             arg="old value"
> >             c.changeValue(arg)
> >             print arg
>
> Fortunately, you can't construct such a thing in Python or in a C
> extension. Consider the following:
>
> print "two", 2
> c.changeValue(2)
> print "two maybe", 2
>
> What would you want to it to print the second time?
> two maybe new value?
>
>
>
> >  then it print "new value"
>
> >  i write code like this..
>
> > static PyObject *changeValue(PyObject *self,PyObject *args){
> >         PyObject *sampleObj, *m ;
> >         char *argName;
>
> >       if (!PyArg_ParseTuple(args, "O", &sampleObj)){
> >                 return NULL;
> >       }
>
> >    m = PyImport_AddModule("__main__");
>
> This means you are assuming/hoping this function will be called only
> from the main script ...
>
> >    PyObject_SetAttrString(m, argName, "new value");
>
> Even if you know the name, you have the problem that it is changing
> the __main__ module's globals ... but the arg could be local or it
> could be an expression ...
>
> >    return Py_BuildValue("");
>
> > }
>
> > But for this i need to know the argument name with which this function
> > called .
> > Is this possible to know argument name in extended c function? if yes,
> > than how i can do it???
>
> No, it's not possible to know the argument name (without help from the
> caller e.g. keyword args), it may not even have a name, it may have
> multiple names ... this is just another variation of the old "what is
> the name of my object" FAQ.
>
> Why don't you tell us what you are trying to achieve (a higher-level
> goal than "I need to poke some value at/into some variable of doubtful
> name and unknowable location"), and then we might be able to give you
> some ideas.
>
> HTH,
> John




Hi John,
   thanks for your great full information,
 But, In my project anyhow i have to change argument value in some
cases. can we pass pointer  of an variable in extended c function.

  like
 import changeValue as c
arg="old value"
c.changeValue(&arg)
print arg

if yes, then how pointer of this variable handle through extended c
function. and how we can change the value through pointer.


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


json-rpc in Python

2009-04-15 Thread Roland Hedberg

Hi!

Anyone got information on python implementations of JSON-RPC ?

I'd like to run a jsonrpc server using mod_python in an Apache server.

I know about, and have used for a while, Jan-Klaas Kollhof's  
implementation which used to be at http://json-rpc.org/, but isn't  
accessible anymore.


Therefor I have started to look elsewhere.

I have found a couple of implementations:

http://www.simple-is-better.org/rpc/
looks OK but seems to be built to work as a stand-alone server

http://www.freenet.org.nz/dojo/pyjson/
should work with 2.6 ? Is it maintained ?

http://python.cx.hu/qxjsonrpc/
Which according to the documentation does not conform to the JSON-RPC  
specification


Have I missed any ?

What are people using ?

-- Roland


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


Re: Measuring time of a telnet connect

2009-04-15 Thread Fabian Lanze

Jack diederich schrieb:

Has anyone an idea how to solve this?


import time
import telnetlib

start = time.time()
conn = telnetlib.Telnet('localhost', 80)
print time.time() - start


Perfect! Thank you..
--
http://mail.python.org/mailman/listinfo/python-list


Python interpreters in threads crash the application

2009-04-15 Thread grbgooglefan
I have C application in which I have instantiated Python interpreter
in each worker thread.

When I start the program it crashes at different places in Python code
but program never finishes normally.

One such core dump is given below.

Can you please help me in following queries?
1) Can we not use Python interpreters per thread instead of having a
common shared Python interpreter at global level/ scope?
2) Why is this crash happening? Do I need to use locks in Python
interpreter?
 Will that make the multiple threads work in serialized way?


pflags core:-
data model = _ILP32  flags = MSACCT|MSFORK
 /1:flags = STOPPED
why = PR_SUSPENDED
 /2:flags = STOPPED
why = PR_SUSPENDED
 /3:flags = STOPPED  lwp_park(0x4,0x0,0x0)
why = PR_SUSPENDED
 /4:flags = 0
sigmask = 0xbefc,0x  cursig = SIGSEGV
 /5:flags = STOPPED  lwp_park(0x4,0x0,0x0)
why = PR_SUSPENDED
 /6:flags = STOPPED  lwp_park(0x4,0x0,0x0)
why = PR_SUSPENDED
 /7:flags = STOPPED  lwp_mutex_timedlock(0xff3f0a10,0x0)
why = PR_SUSPENDED
 /8:flags = STOPPED  lwp_park(0x4,0x0,0x0)
why = PR_SUSPENDED
 /9:flags = STOPPED  lwp_park(0x4,0x0,0x0)
why = PR_SUSPENDED
 /10:   flags = STOPPED
why = PR_SUSPENDED
 /11:   flags = STOPPED  lwp_park(0x4,0x0,0x0)
why = PR_SUSPENDED

pstack core:
-  lwp# 1 / thread# 1  
 000110e8 main (1, ffbff2d4, ffbff2dc, 21400, feec0200, feec0240)
+ 50
 00010c10 _start   (0, 0, 0, 0, 0, 0) + 108
-  lwp# 2 / thread# 2  
 ff20877c string_dealloc (fed7bbfc, 53bc, ff2f6ba0, e40e0, ff2f6ba0,
ff2dec08)
 ff213b44 PyString_InternFromString (ff316e68, 0, 0, ff316e68, e40c0,
2000) + 20
 ff2262f8 init_slotdefs (b8964, 0, 2000, 2204, 0, 0) + 58
 ff2266b8 add_operators (ff2f76c0, 1c00, b8568, 5000, 2000, 21b0) + 1c
 ff21fe28 PyType_Ready (ff2f76c0, bef10, 0, 0, 5124, 0) + 134
 ff21fd88 PyType_Ready (ff2f7438, 1, de358, ff3b3904, 5124, 5284) + 94
 ff2008c8 _Py_ReadyTypes (0, ff308ea4, 121c20, fefc6964, 21788,
ff06b3d4) + 1c
 ff287cc8 Py_InitializeEx (0, 0, 4000, 0, 0, ff2dec08) + 234
 00010c4c __1cMinterpthread6Fpv_0_ (0, fed7c000, 0, 0, 10c38, 0) + 14
 ff0400b0 _lwp_start (0, 0, 0, 0, 0, 0)
-  lwp# 3 / thread# 3  
 ff040154 __lwp_park (0, 0, ff06c348, 0, 0, 1) + 14
 ff02415c _flockget (ff06c348, 21878, 441b4, ff3b3904, ff06c308,
21878) + 94
 ff018364 printf   (112a4, 21878, 0, 21896, ff068284, ff2dec08) + 5c
 00010c8c __1cMinterpthread6Fpv_0_ (0, fec7c000, 0, 0, 10c38, 0) + 54
 ff0400b0 _lwp_start (0, 0, 0, 0, 0, 0)
-  lwp# 4 / thread# 4  
 fefb0c90 strlen   (ff2bdee8, feafbcf4, 4000, 73, 2e, 4000) + 50
 ff274924 PyErr_Format (ff2ebb2c, ff2bdee8, 0, 1400, 1780, ff2dec08) +
18
 ff1feeb8 PyObject_HashNotImplemented (e4020, 0, 5000, 53bc, ff2f6ba0,
0) + 34
 ff1f8420 PyDict_GetItem (23a50, e4020, 2000, cb28c, ff2dec08, 0)
+ 64
 ff213a2c PyString_InternInPlace (feafbe3c, 53bc, ff2f6ba0, e4020,
ff2f6ba0, ff2dec08) + b4
 ff1fb6b4 PyDict_SetItemString (239c0, 114a3, e3030, 21896, ff2dec08,
1b) + 20
 00010cd8 __1cMinterpthread6Fpv_0_ (0, feafc000, 0, 0, 10c38, 0) + a0
 ff0400b0 _lwp_start (0, 0, 0, 0, 0, 0)
-  lwp# 5 / thread# 5  
 ff040154 __lwp_park (0, 0, ff06c348, 0, 0, 1) + 14
 ff02415c _flockget (ff06c348, 21878, 441b4, 0, ff06c308, 21878) + 94
 ff018364 printf   (112a4, 21878, 0, 21896, ff068284, ff2dec08) + 5c
 00010c8c __1cMinterpthread6Fpv_0_ (0, fe9fc000, 0, 0, 10c38, 0) + 54
 ff0400b0 _lwp_start (0, 0, 0, 0, 0, 0)
-  lwp# 6 / thread# 6  
 ff040154 __lwp_park (0, 0, ff06c348, 0, 0, 1) + 14
 ff02415c _flockget (ff06c348, 21878, 441b4, ff29afcc, ff06c308,
21878) + 94
 ff018364 printf   (112a4, 21878, 0, 21896, ff068284, ff2dec08) + 5c
 00010c8c __1cMinterpthread6Fpv_0_ (0, fe8fc000, 0, 0, 10c38, 0) + 54
 ff0400b0 _lwp_start (0, 0, 0, 0, 0, 0)
-  lwp# 7 / thread# 7  
 ff3d7e68 ___lwp_mutex_timedlock (0, 1, ff3c3f98, ff3ee980, 2ae64,
ff3ee268) + c
 ff3cc60c elf_bndr (ff3f1b98, 132c, ff1f0840, e3030, ff3f06d0, 0) + 28
 ff3b3904 elf_rtbndr (ff1f0840, 3d, 1, 92492400, ff2f339c, 8) + 10
 ff2e4060  (e3020, ff2f2314, 0, 0, 0, 0)
 ff1f0840 PyLong_FromLong (0, 21878, ff06c348, 21896, ff2dec08, 1b) +
44
 00010cc8 __1cMinterpthread6Fpv_0_ (0, fe7fc000, 0, 0, 10c38, 0) + 90
 ff0400b0 _lwp_start (0, 0, 0, 0, 0, 0)
-  lwp# 8 / thread# 8  
 ff040154 __lwp_park (0, 0, ff06c348, 0, 0, 1) + 14
 ff038db0 mutex_lock_internal (ff06c348, 0, 1, 0, 10, ff06cbc0) + 5d0
 ff02415c _flockget (ff06c348, 21878, 441b4, ff29afcc, ff06c308,
21878) + 94
 ff018364 printf   (112a4, 21878, 0, 

Re: segmentation fault while using ctypes

2009-04-15 Thread Diez B. Roggisch
> 
> Thanks Diez,
> 
> I used the gdb but it just crashed and kicked my out of gdb prompt.
> how can I get a stack trace?

That's odd, has never happened for me before. Can you show us what you do
exactly, and what gdb & co say?

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


Re: Modifying the value of a float-like object

2009-04-15 Thread Dan Goodman

[email protected] wrote:

Hello,

Is there a way to easily build an object that behaves exactly like a
float, but whose value can be changed?  The goal is to maintain a list
[x, y,…] of these float-like objects, and to modify their value on the
fly (with something like x.value = 3.14) so that any expression like "x
+y" uses the new value.


Hi Eric,

Numpy's array object can do something like what you want:

In [27]: x=array(0.0)

In [28]: print x, sin(x)
0.0 0.0

In [29]: x.itemset(pi/2)

In [30]: print x, sin(x)
1.57079632679 1.0

Not sure if this a recommended way of using array or not, but it seems 
to work. The problem is that any calculation you do with such an object 
will result in a float, and not another numpy array (although inplace 
operations work):


In [40]: print (x*2).__class__


In [41]: x *= 2

In [42]: print x.__class__


So it's not a perfect solution, but it might be OK for what you need.

Dan

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


Re: how to know argument name with which a function of extended c called

2009-04-15 Thread John Machin
On Apr 15, 6:13 pm, rahul  wrote:
> On Apr 14, 6:24 pm, John Machin  wrote:
>
>
>
> > On Apr 14, 10:35 pm, rahul  wrote:
>
> > > Hi,
> > >   i need to write a 'c extension function' in this function i need to
> > > change argument value with  which this function called.
>
> > The appropriate way for a function to give output is to return a
> > value, or a tuple of values.
>
> > example:
>
> > def get_next_token(input_buffer, offset):
> >    """get next lexical token, starting at offset
> >       return (the_token, new offset)"""
> >    length = find_len_of_token_somehow(input_buffer, offset)
> >    new_offset = offset + length
> >    return input_buffer[offset:new_offset], new_offset
>
> > and you can call it by
> >    token, pos = get_next_token(buff, pos)
> >    return input
>
> > >   ie,
> > >          if a python code like
> > >             import changeValue as c
> > >             arg="old value"
> > >             c.changeValue(arg)
> > >             print arg
>
> > Fortunately, you can't construct such a thing in Python or in a C
> > extension. Consider the following:
>
> > print "two", 2
> > c.changeValue(2)
> > print "two maybe", 2
>
> > What would you want to it to print the second time?
> > two maybe new value?
>
> > >  then it print "new value"
>
> > >  i write code like this..
>
> > > static PyObject *changeValue(PyObject *self,PyObject *args){
> > >         PyObject *sampleObj, *m ;
> > >         char *argName;
>
> > >       if (!PyArg_ParseTuple(args, "O", &sampleObj)){
> > >                 return NULL;
> > >       }
>
> > >    m = PyImport_AddModule("__main__");
>
> > This means you are assuming/hoping this function will be called only
> > from the main script ...
>
> > >    PyObject_SetAttrString(m, argName, "new value");
>
> > Even if you know the name, you have the problem that it is changing
> > the __main__ module's globals ... but the arg could be local or it
> > could be an expression ...
>
> > >    return Py_BuildValue("");
>
> > > }
>
> > > But for this i need to know the argument name with which this function
> > > called .
> > > Is this possible to know argument name in extended c function? if yes,
> > > than how i can do it???
>
> > No, it's not possible to know the argument name (without help from the
> > caller e.g. keyword args), it may not even have a name, it may have
> > multiple names ... this is just another variation of the old "what is
> > the name of my object" FAQ.
>
> > Why don't you tell us what you are trying to achieve (a higher-level
> > goal than "I need to poke some value at/into some variable of doubtful
> > name and unknowable location"), and then we might be able to give you
> > some ideas.
>
> > HTH,
> > John
>
> Hi John,
>    thanks for your great full information,
>      But, In my project anyhow i have to change argument value in some
> cases.

I say again: You can't do that. If you care to explain AT A HIGHER
LEVEL than "change argument value" what you are trying to achieve,
then some help might be available.

If you are translating from C to Python and you have a function that
is expected to be called like func(in1, &out2, &inout3) then you will
have to write the function as
   def func(in1, inout3):
  # calculate out2
  # calculate new_inout3
  return out2, new_inout3
(or the equivalent in a C-extension) and the caller will need to be
changed to do
   out2, inout3 = func(in1, inout3)

Alternatively, leave the function in C and provide a wrapper function
in C that bridges the different argument-passing conventions. There
are various options for doing this -- the Cython/boost/ctypes/etc
gurus can help with this once you've explained what your real
requirement is.

> can we pass pointer  of an variable in extended c function.

Python doesn't have "variables". It has objects, which have zero, one,
or many names. The names exist in namespaces of various types: local
to a function/method, attribute of a class, attribute of a class
instance, global to a module, ... there is no unified "address"
concept; the concept "&arg" has no meaning and no means of realising
it as a vehicle for doing the equivalent of C's "*pointer =
new_value".

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


Re: Modifying the value of a float-like object

2009-04-15 Thread Eric . Le . Bigot
To Peter: What I had in mind was to implement your calc() function;
you could do something similar with your loop in the previous post by
replacing "for f.shift..." by "f.shift = 1"; this would give you 2
values, which you can combine with your unused variable in order to
obtain the same value as with your calc() function.

To Peter and Steven: I now realize that the interval arithmetic that
you propose is not trivial, if one wants optimal confidence intervals:
correlations between uncertainties are very hard to handle.  The most
robust method is to implement Peter's calc() method.  But again, when
calculations are not done through explicit argument passing, as in
Peter's example, mutable floats are useful!
--
http://mail.python.org/mailman/listinfo/python-list


Something weird about re.finditer()

2009-04-15 Thread Gilles Ganault
Hello

I stumbled upon something funny while downloading web pages and
trying to extract one or more blocks from a page: Even though Python
seems to return at least one block, it doesn't actually enter the for
loop:

==
re_block = re.compile('before (.+?) after',re.I|re.S|re.M)

#Here, get web page and put it into "response"

blocks = None
blocks = re_block.finditer(response)
if blocks == None:
print "No block found"
else:
print "Before blocks"
for block in blocks:
#Never displayed!
print "In blocks"
==

Since "blocks" is no longer set to None after calling finditer()...
but doesn't contain a single block... what does it contain then?

Thank you for any tip.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Something weird about re.finditer()

2009-04-15 Thread John Machin
On Apr 15, 6:46 pm, Gilles Ganault  wrote:
> Hello
>
>         I stumbled upon something funny while downloading web pages and
> trying to extract one or more blocks from a page: Even though Python
> seems to return at least one block, it doesn't actually enter the for
> loop:
>
> ==
> re_block = re.compile('before (.+?) after',re.I|re.S|re.M)
>
> #Here, get web page and put it into "response"
>
> blocks = None
> blocks = re_block.finditer(response)
> if blocks == None:
>         print "No block found"
> else:
>         print "Before blocks"
>         for block in blocks:
>                 #Never displayed!
>                 print "In blocks"
> ==
>
> Since "blocks" is no longer set to None after calling finditer()...
> but doesn't contain a single block... what does it contain then?
>
> Thank you for any tip.

Tip 0: contemplate what type you could infer from the name findITER
Tip 1: Read the manual to see what type is returned by re.finditer
(or do import re; help(re.finditer))
Tip 2: Append
   , type(blocks)
to the relevant print statements in your above code, and inspect the
output.

Metatip 0: Following the tips can be done rapidly without any need for
an internet connection.

Meta**2tip 0: The Tips and the Metatip can be applied to many things,
not just re.finditer.

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


Re: Something weird about re.finditer()

2009-04-15 Thread Peter Otten
Gilles Ganault wrote:

> I stumbled upon something funny while downloading web pages and
> trying to extract one or more blocks from a page: Even though Python
> seems to return at least one block, it doesn't actually enter the for
> loop:
> 
> ==
> re_block = re.compile('before (.+?) after',re.I|re.S|re.M)
> 
> #Here, get web page and put it into "response"
> 
> blocks = None
> blocks = re_block.finditer(response)
> if blocks == None:
> print "No block found"
> else:
> print "Before blocks"
> for block in blocks:
> #Never displayed!
> print "In blocks"
> ==
> 
> Since "blocks" is no longer set to None after calling finditer()...
> but doesn't contain a single block... what does it contain then?

This is by design. When there are no matches re.finditer() returns an empty
iterator, not None.

Change your code to something like

has_matches = False
for match in re_block.finditer(response):
if not has_matches:
has_matches = True
print "before blocks"
print "in blocks"
if not has_matches:
print "no block found"

or

match = None
for match in re_block.finditer(response):
print "in blocks"
if match is None:
print "no block found"

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


Re: Something weird about re.finditer()

2009-04-15 Thread Steven D'Aprano
On Wed, 15 Apr 2009 10:46:28 +0200, Gilles Ganault wrote:

> Since "blocks" is no longer set to None after calling finditer()... but
> doesn't contain a single block... what does it contain then?

It probably took you twenty times more time and effort to ask the 
question than it would have to look for yourself.


>>> import re
>>> re_block = re.compile('before (.+?) after',re.I|re.S|re.M)
>>> x = re_block.finditer("nothing to see here")
>>> x is None
False
>>> x

>>> list(x)
[]




BTW, testing for None with == is not recommended, because one day 
somebody might pass your function some strange object that compares equal 
to None. Although it wouldn't have solved your problem, the recommended 
way to test if an object is None is with the `is` operator.



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


Re: sharing/swapping items between lists

2009-04-15 Thread Aaron Brady
On Apr 14, 9:45 pm, Ross  wrote:
> On Apr 14, 7:18 pm, Aaron Brady  wrote:
>
>
>
> > On Apr 14, 7:01 pm, Aaron Brady  wrote:
>
> > > On Apr 14, 12:37 pm, Ross  wrote:
>
> > > > On Apr 14, 10:34 am, Ross  wrote:
>
> > > > > On Apr 14, 5:57 am, [email protected] (Aahz) wrote:
>
> > > > > > In article 
> > > > > > ,
>
> > > > > > Ross   wrote:
> > > > > > >On Apr 13, 9:08=A0am, [email protected] (Aahz) wrote:
> > > > > > >> In article 
> > > > > > >>  > > > > > >com>,
> > > > > > >> Ross =A0 wrote:
>
> > > > > > >>>I'm sorry...my example was probably a bad one. A better example 
> > > > > > >>>of
> > > > > > >>>output I would like would be something like [[1,2],[3,4],[5,6]] 
> > > > > > >>>and
> > > > > > >>>then for the leftovers list [7,8,9,10 etc]. What I'm trying to 
> > > > > > >>>do is
> > > > > > >>>produce some sort of round robin algorithm for tennis that is
> > > > > > >>>constrained by the number of courts available each week. So if 
> > > > > > >>>there
> > > > > > >>>are only 3 courts available for a singles league and 10 people 
> > > > > > >>>have
> > > > > > >>>signed up, 4 players will have a bye each week. I want my 
> > > > > > >>>algorithm to
> > > > > > >>>produce unique matchups each week and also give each player the 
> > > > > > >>>same
> > > > > > >>>angle?
>
> > > > > > >> How about Googling for "round robin algorithm python"? ;-)
>
> > > > > > >I have the basic algorithm and it works fine...I'm just having 
> > > > > > >trouble
> > > > > > >adding another parameter to it that allows for court constraints 
> > > > > > >and
> > > > > > >bye weeks.
>
> > > > > > You'll need to give us more information, then.  Why don't you start 
> > > > > > with
> > > > > > the core algorithm you're using?
> > > > > > --
> > > > > > Aahz ([email protected])           <*>        
> > > > > > http://www.pythoncraft.com/
>
> > > > > > Why is this newsgroup different from all other newsgroups?
>
> > > > > Here's the core algorithm I'm using:
>
> > > > > >>> def round_robin(teams,rounds):
>
> > > > >         if len(teams)%2:
> > > > >                 teams.append(None)
> > > > >         mid = len(teams) //2
> > > > >         for i in range(rounds):
> > > > >                 yield zip(teams[:mid], teams[mid:])
> > > > >                 teams = teams[0:1] + teams[mid:mid+1] + 
> > > > > teams[1:mid-1]+teams[mid
> > > > > +1:]+teams[mid-1:mid]
>
> > > > > >>> if __name__== '__main__':
>
> > > > >         rounds = 15
> > > > >         teams = range(16)
> > > > >         for round in round_robin(teams,rounds):
> > > > >                 print round
>
> > > > fyi rounds=15 and teams =range(16) was just test code I was playing
> > > > around with...they could theoretically be anything.
>
> > > Here is an idea.  Create a list of all possible pairs, using
> > > itertools.combinations.  You'll notice everyone gets equal play time
> > > and equal time against each other on a pair-by-pair basis.  Then, call
> > > random.shuffle until one player isn't playing on two courts in one
> > > day.
>
> > This might take a long time.  Not that I can guarantee that a depth-
> > first-search would be any faster, or that a breadth-first-search would
> > run faster *and* run in available memory.  
>
> I have a sub-optimal solution that I'm playing with now. Since my
> output is a list of tuples and looks something like this (if there
> were 16 teams and 15 rounds), I could designate a each nth tuple in
> each round as a bye, but since the 1st item in my list remains fixed,
> it's suboptimal. For example, you could say every 4th (and/or 3rd ,
> 5th, etc depending on how many available cts) tuple in each round gets
> a bye and pop() it from the list...:

>From what I understood, you want each player to play each other player
exactly once, with no empty courts until the last round.  Your
solution doesn't achieve that if you simply omit a choice of matches,
even if you omit them fairly.  How to omit them fairly is a separate
question.

I observe that your tuples follow a pattern.  Each court is a match
between the first slot on the previous court on the previous week, and
the second slot on the previous court on the next week, with the
exceptions of the first and last courts.  The same player always plays
on court one against the second slot on the second court on the
previous week (and hence (?), first slot on second court on the next
week), and the last court is always between the second slot on the
last court on the next week and the first slot on the previous court
on the previous week.  IN other words, it follows a zig zag.

Your idea of omitting a given court, including the last but except the
first, omits each player twice, and two opponents of each player.

If you try to finish the schedule, it will take at least three more
weeks.  Arranging the second column:

5, 3
14, 7
10, 12
2, 8
6, 4
13, 15
9, 11

11, 13
15, 6
4, 2
8, 10
12, 14
7, 5
3, 1

1, 9

There is guaranteed to be one left over.  From what I read about your
a

Re: Automatically generating arithmetic operations for a subclass

2009-04-15 Thread Steven D'Aprano
On Tue, 14 Apr 2009 19:24:56 +0200, Sebastian Wiesner wrote:

>> Is there a trick or Pythonic idiom to make arithmetic operations on a
>> class return the same type, without having to manually specify each
>> method? I'm using Python 2.5, so anything related to ABCs are not an
>> option.
>> 
>> Does anyone have any suggestions?
> 
> Metaclasses can be used for this purpuse, see the example for a Roman
> number type [1]
> 
> [1] http://paste.pocoo.org/show/97258/


That's an interesting solution. I like it.


Thanks to all who responded, I see that there's no best practice to get 
what I want, so I'll do some experimentation.



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


Re: subprocess: reading from stdout hangs process termination, waiting for ENTER keyboard signal

2009-04-15 Thread Kushal Kumaran
On Wed, Apr 15, 2009 at 1:20 PM, giohappy  wrote:
> On 14 Apr, 18:52, MRAB  wrote:
>> giohappywrote:
>> > Hello everyone.
>> > I'm trying to use subprocess module to launch a Windows console
>> > application. The application prints some results to standard output
>> > and then waits for the user to press any key to terminte. I can't
>> > control this behaviour, as the application is not mine...
>> > I'm stuck at the very first lines of my code. I'm trying to force
>> > process termination (even with proc.terminate()), and it works only if
>> > I don't read from stdout. If I do proc.stdout.read() the process
>> > hangs, and I have to manually press the keyboard to interrupt it.
>> > Probably it's due a low-level handle that is kept on the process
>> > stdout, waiting for the keypress event...
>>
>> > How can I solve it?
>> > Giovanni
>>
>> > --- Code excerpt---
>>
>> > proc = subprocess.Popen('the_app.exe',
>> >                        shell=True,
>> >                        stdout=subprocess.PIPE,
>> >                        )
>> > #stdout_value = proc.communicate()[0]
>> > stdout_value = proc.stdout.read()
>> > PROCESS_TERMINATE = 1
>> > handle = win32api.OpenProcess(PROCESS_TERMINATE, False, proc.pid)
>> > win32api.TerminateProcess(handle, -1)
>> > win32api.CloseHandle(handle)
>> > print stdout_value
>>
>> Try this:
>>
>> proc = subprocess.Popen('the_app.exe',
>>                         shell=True,
>>                         stdin=subprocess.PIPE,
>>                         stdout=subprocess.PIPE,
>>                         )
>> stdout_value = proc.communicate("\n")[0]
>
> MRAB, I've tried that too but no result... I still have to press a
> keybord key to terminate (the classical "Press any key to continue")

If it actually is "Press any key to continue" rather than "Press Enter
to continue", it is likely directly using the console using available
low-level APIs, rather than reading from stdin.  AFAIK, subprocess
cannot handle that.

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


Re: Modifying the value of a float-like object

2009-04-15 Thread Eric . Le . Bigot
Dan, wow!  This looks extremely promising!


I initially tried to create a Float_ref class that inherits from
numpy.array, so that objects of the new class behave like numpy.array
in calculations, but also contain an "uncertainty" atribute, but this
is apparently not allowed ("cannot create 'builtin_function_or_method'
instances").

So I thought I'd directly add an attribute to a numpy.array: "x =
numpy.array(3.14); x.uncertainty = 0.01", but this is not allowed
either.

Thus, it is not obvious for me to extend the 1x1 numpy.array object so
that it also contains an "uncertainty" attribute.


However, I think your suggestion can largely solve the original
problem, with a small price to pay: numbers with uncertainties can be
defined in a module that keeps declared values in a list of
numpy.arrays, and the associated uncertainties in another list: after
"registering" many "mutable float" objects, the modules would contain
the following lists:

>>> mutable_floats = [ numpy.array(3.14), numpy.array(1.41),...]
>>> uncertainties  = [ 0.01,  0.01 ,...]

The floatref class could instead be a simple function that returns the
last numpy.array created, after adding an element to both the
mutable_float and uncertainties lists.  What we lose here is that it's
hard to get the uncertainty associated to a given variable (this is
obviously possible if no other variable has the same value, though).

Dan, you gave me hope that precise error propagation calculations can
be done by repeated calls to unmodified calculation code designed for
floats!  Thanks!

On Apr 15, 10:44 am, Dan Goodman  wrote:
> [email protected] wrote:
> > Hello,
>
> > Is there a way to easily build an object that behaves exactly like a
> > float, but whose value can be changed?  The goal is to maintain a list
> > [x, y,…] of these float-like objects, and to modify their value on the
> > fly (with something like x.value = 3.14) so that any expression like "x
> > +y" uses the new value.
>
> Hi Eric,
>
> Numpy's array object can do something like what you want:
>
> In [27]: x=array(0.0)
>
> In [28]: print x, sin(x)
> 0.0 0.0
>
> In [29]: x.itemset(pi/2)
>
> In [30]: print x, sin(x)
> 1.57079632679 1.0
>
> Not sure if this a recommended way of using array or not, but it seems
> to work. The problem is that any calculation you do with such an object
> will result in a float, and not another numpy array (although inplace
> operations work):
>
> In [40]: print (x*2).__class__
> 
>
> In [41]: x *= 2
>
> In [42]: print x.__class__
> 
>
> So it's not a perfect solution, but it might be OK for what you need.
>
> Dan

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


Re: Re: Modifying the value of a float-like object

2009-04-15 Thread Dave Angel

[email protected] wrote:

Thanks Dave for your thoughtful remarks, which you sent right when I
was writing a response to the previous posts.

I was wondering about a kind "mutable float"; so you're right, it's
not fully a float, because it's mutable.  I'd like to have an object
that behaves like a float in numerical calculations.  I understand
that mutability would require to handle such objects with care, as in
your example, but Python programmers are used to this with any mutable
object.  All my calculations use "constants with an uncertainty" (or
regular floats).

There are many such calculations in my code, and I'd like it to be
very clean.  The first idea I mentioned (using "x()") is essentially
what you propose with using "x[0]" in calculations.

So, it looks like it's not possible to have float-like objects
(calculation/formula-wise) that are mutable?!  and it also looks like
the price to pay for the mutability is to have to write "heavier"
versions of any formula that uses these "special floats" (that carry
an uncertainty): "x[0]+y[0]*sin(...)", "x()+y()", "float(x)+float
(y)",...  which, by the way, essentially prevents any float-like
object from being used as a float in formulas that you don't write
yourself.

This does not look good.  Python is failing me!!! :/  I heard that
this would be easy to do in Ruby (not confirmed, though)...

More ideas and thoughts would still be most welcome!

On Apr 14, 8:45 pm, Dave Angel  wrote:
  

[email protected] wrote:


It looks like what is needed here are a kind of "mutable float".  Is
there a simple way of creating such a type?  I don't mind changing the
value through x.value =3 instead of x = 1.23... :)
  
On Apr 14, 3:03 pm, [email protected] wrote:
  

Hello,

Is there a way to easily build an object that behaves exactly like a

float, but whose value can be changed?  The goal is to maintain a list
[x, y,…] of these float-like objects, and to modify their value on the
fly (with something like x.value =4) so that any expression like "x
+y" uses the new value.

I thought of two solutions, both of which I can't make to work:

1) Use a class that inherits from float.  This takes care of the

"behave like float" part.  But is it possible to change the value of
the float associated with an instance?  That is, is it possible to
do:  "x =loat(1.23); x.change_value(3.14)" so that x's float value
becomes 3.14?

2) The other possibility I thought of was: use a class that defines a

'value' member (x.value).  This takes care of the "value can be
changed" part.  But is it possible/easy to make it fully behave like a
float (including when passed to functions like math.sin)?

Alternatively, I'd be happy with a way of handling numerical

uncertainties in Python calculations (such as in "calculate the value
and uncertainty of a*sin(b) knowing that a=+/- 0.1 and b=1.00 +/-
0.01").

Any idea would be much appreciated!


The answer to your original question is no.  If the value can be changed, then 
it doesn't behave like a float.  And that's not just a pedantic answer, it's a 
serious consideration.

You have to decide what characteristics of a float you need to mimic, and which ones you 
don't care about, and which ones you want to change.  Only after having a pretty good 
handle on those answers can you pick a "best" implementation.

Let's call this new type a nfloat, and let's assume you have a function that 
returns one.  That might be a constructor, but it may not, so we're keeping our 
options open.
  myval =ewfunction(42.0)

What do you want to happen when you execute   b =yval ?   Presumably
you want them to be "equal" but in what sense?  Suppose you then change
one of them with your suggested attribute/method.
 myval.value =ewfunction("aaa")

is b the same as it was (like a float would be), or is b also changed?

Do you need lots of functions to work on one of these nfloats, or could
you use them as follows:
 sin( b.value )

Instead of using .value to change the underlying float, how about if you
use [0] ?  Just use a list of size 1.




  
OK, your other recent message clarified for me some of what you're 
after. Seems to me you're going to have a specific list of values, which 
you want to be able to individually tweak each time you do the 
calculations. And you want to have a name for each of those values, so 
that the formulas look pretty straightforward.


I'm not convinced this is the best approach for uncertainty 
calculations, but if those numbers do form a fairly easily specified 
list (and one that's not dynamic), what you probably want is an object 
that's a reference to a list item, while the list item is itself a 
float. And you want the object to support many of the usual floating 
point operations. So why not define a single static list (could be a 
class object), and define a class whose instances contain an index into 
tha

Re: Modifying the value of a float-like object

2009-04-15 Thread Eric . Le . Bigot
Steven, I'd appreciate if you could refrain from criticizing so
bluntly so many points.  I'd be great if you trusted me more for
knowing what I'm talking about; I've been a programmer for 25 years,
now, and I pretty well know what my own code looks like!  I appreciate
your input, but please soften your style!

For instance, for your information, to respond only to your first
point (below), I'd like to be more precise: I don't want a function
that calculates a result (such as f() in Peter's code above) to
explicitly contain code for handling uncertainties.  This is what I
had in mind when I said, perhaps inadequately, that "the place in the
code where (foo, baz) is calculated has no idea (...) of where they
were defined, etc.".  Thus, either f() manipulates numbers without
knowing that it is actually performing error calculations when doing
x*y, or some code calls f() and does the job of calculating the
uncertainty.  The first method does not work, for many reasons: (1)
correlated errors are really hard to take into account, (2) the 2
modules you list are interesting, but none of them gives the standard
deviation of an arbitrary mathematical expression.

Steven, there is obviously a strong misunderstanding problem here.
For instance, I do not "conflate four different issues as if they were
one".  There are, however, various issues raised in this stimulating
thread, and many paths are worth exploring.

It looks to me like Dan has paved the way to a good solution to the
initial problem: calculate the uncertainty on any calculation that was
written for code that works with floats, without modifying this code;
the method consists essentially in performing Peter's calc()
calculation, but changing the values of the quantities used through
references (as Ben described).  But, again, I don't yet see how to
unambiguously keep track of the uncertainty associated to a numerical
value.  I'll try this, to see if the devil does lie in the details,
here. :)

The other, approximate solution, would be to coerce with float() any
parameter that can carry an uncertainty, in calculations (numbers with
uncertainty would have a __float__() method that returns a result that
can be changed after instantiation); but this would modify (and make
less legible) existing code.



On Apr 15, 10:59 am, Steven D'Aprano
 wrote:
> On Wed, 15 Apr 2009 01:05:33 -0700, Eric.Le.Bigot wrote:
> > Ben F., you're right on the money!  You expressed exactly what I'm
> > looking for.  Why should I want this?  because the place in the code
> > where (foo, baz) is calculated has _no idea_ of what foo and baz are, of
> > where they were defined, etc.;
>
> This makes no sense. The piece of code which calculates (foo, baz) knows
> EXACTLY what foo and baz are, because it has just calculated them. It has
> them *right there*. If it wants to know what they are, it can just look
> at them:
>
> def calculate():
>     # lots of calculations
>     result = (foo, baz)
>     # what is foo?
>     foo
>     # what is baz?
>     baz
>     # what are their types?
>     type(foo)
>     type(baz)
>
> As for where they were defined... why do you care where they were
> defined? What's important is *what they are*, and you know what they are,
> because you have them, right there.


> As far as I can tell, you are conflating four different issues as if they
> were one:
>
> (1) how to calculate and propagate errors and do interval arithmetic;
>
> (2) how to use the functions in the math module with arguments which
> aren't floats;
>
> (3) how to make a mutable float type;
>
> (4) how to have the result of a calculation magically change when you
> change the arguments, without re-doing the calculation (at least not
> explicitly re-doing the calculation).
>
> (1) - (3) are reasonable; (3) is I think pointless but not actively
> harmful; but (4) is just creating a world of pain and almost-impossible
> to track down bugs.
>
> > Anyway, Steven gave me hope that some kind of mutable float is possible
> > in Python, and Ben perfectly expressed my need.  Again, alternatively,
> > any module that handles in a convenient manner calculations with error
> > propagation would be great, but I have not found any single such module!
>
> http://pyinterval.googlecode.com/svn/trunk/html/index.html
>
> http://docs.sympy.org/modules/mpmath/intervals.html
>
> You should also read 
> this:http://conference.scipy.org/proceedings/SciPy2008/paper_3/
>
> --
> Steven

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


Re: Re: Modifying the value of a float-like object

2009-04-15 Thread Dave Angel

Steven D'Aprano wrote:

On Tue, 14 Apr 2009 14:45:47 -0400, Dave Angel wrote:

  

The answer to your original question is no.  If the value can be
changed, then it doesn't behave like a float.  And that's not just a
pedantic answer, it's a serious consideration.



Oh nonsense. Many programming languages have mutable floats.


  
That's irrelevant.  Python doesn't.  So introducing one will quite 
likely alter the OP's code's behavior.  It doesn't matter if it's 
possible, it matters whether the existing code's behavior might change, 
and of course if a future maintainer might have trouble making sense of it.


BTW, just what languages have mutable floats?  I don't think I've come 
across any since Fortran (on a CDC 6400), and I don't think that was 
deliberate.  In that implementation it was possible to change the value 
of a literal 2.0 to something else.  I remember writing a trivial 
program that printed the value of 2.0 + 2.0 as 5.0.  It was about 1971, 
I believe.  I know I haven't used Fortran since 1973.


If you're going to use the fact that many languages pass arguments by 
reference, then you should realize that a reference to a float is a 
different kind of variable than a float.  And although that language 
might use the terminology of mutable, it wouldn't mean the same thing 
that mutable does in Python.


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


Re: Modifying the value of a float-like object

2009-04-15 Thread Eric . Le . Bigot
To Dave A. and Piet: I appreciate your taking the time to make
suggestions.  I understand that there is a hitch in the approach that
you describe, which I would like to insist on: how do you handle
functions that use math.sin(), for instance?  numpy does this kind of
magic, but I'm not sure it's wise to spend time reproducing it.  I'd
to not have to modify any calculation code, so that it is legible and
general (in particular, so that it can work with Python floats).  And
again, interval arithmetic fails to produce real standard deviations,
because of correlations between uncertainties.

Hence my idea to have mutable floats, that would be changed in some
_external_ error calculation routine (i.e. a routine which is
completely independent from the calculation f()), in a way equivalent
to Peter's derive() function--except that the values used in f() are
only accessible through a list of objects, as in Ben's post.  I'll
implement this with 1x1 numpy.array objects, even though this will
mean that numbers will not have an unambiguous uncertainty attribute,
as I mentioned in a previous post.  I'll post here the result of my
investigations.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Modifying the value of a float-like object

2009-04-15 Thread Steven D'Aprano
On Wed, 15 Apr 2009 01:05:33 -0700, Eric.Le.Bigot wrote:

> Ben F., you're right on the money!  You expressed exactly what I'm
> looking for.  Why should I want this?  because the place in the code
> where (foo, baz) is calculated has _no idea_ of what foo and baz are, of
> where they were defined, etc.; 


This makes no sense. The piece of code which calculates (foo, baz) knows 
EXACTLY what foo and baz are, because it has just calculated them. It has 
them *right there*. If it wants to know what they are, it can just look 
at them:

def calculate():
# lots of calculations
result = (foo, baz)
# what is foo?
foo
# what is baz?
baz
# what are their types?
type(foo)
type(baz)

As for where they were defined... why do you care where they were 
defined? What's important is *what they are*, and you know what they are, 
because you have them, right there.


> on the other hand, the floatref class can
> keep track of the values that were constructed, 

What advantage does that give you? I can think of big disadvantages: it 
is complicated and inefficient and will lead to bugs in your code.


[...]
> To Steven: Thank you for your suggestion about checking
> UserString.MutableString.
> I do understand that mutable objects can be surprising, but, again,
> Python programmers constantly use such objects (lists, dicts,): I
> don't think that manipulating a mutable float is more of a problem than
> using a list, for a Python programmer.

Lists and floats tend to be used in completely different ways.

I don't have (much) problem with the idea of having mutable floats per 
se, although I think they are unnecessary. But even given mutable floats, 
the use you seem to want to put them will lead you into trouble.


> As for your idea of
> "straight-forward interval arithmetic", it's a good one, but I'd have to
> redefine lots of functions from the math module, to use them explicitly
> in my code, etc.:

But you need to do that anyway, because the math module doesn't calculate 
the error estimates that you need. Given some FloatWithError x, if you 
call math.sin(x) the math.sin() function makes no effort to calculate an 
error term.


> this is heavy; I was looking for a light-weight
> alternative, where all calculations are expressed in Python as if all
> numbers were regular floats, and where you don't have to redefine any
> mathematical operation.  In other words, I'm looking for a robust way of
> implementing the derive() function of Peter without knowing anything
> about which function uses which "numbers with an uncertainty": the
> equivalent of Peter's derive () would simply successively change all
> "numbers with uncertainty" defined so far and see how the result of a
> given calculation varies-- the variables that are not used in the
> calculated expression don't change the result, for instance.  I
> understand that this is computationally inefficient (too many variables
> might be changed), but I don't need speed, only robustness in the
> propagation of errors, and a simple and very legible calculation code,
> and ideally with only a few lines of Python.

Nothing you have described gives me any confidence in the robustness of 
your solution. In fact the opposite: given the description of what you 
are planning, I'd have ZERO confidence in ANY calculation you did, 
because I'd wonder how all the other calculations being performed were 
effecting the calculations you've already done.

As far as I can tell, you are conflating four different issues as if they 
were one:

(1) how to calculate and propagate errors and do interval arithmetic;

(2) how to use the functions in the math module with arguments which 
aren't floats;

(3) how to make a mutable float type;

(4) how to have the result of a calculation magically change when you 
change the arguments, without re-doing the calculation (at least not 
explicitly re-doing the calculation).

(1) - (3) are reasonable; (3) is I think pointless but not actively 
harmful; but (4) is just creating a world of pain and almost-impossible 
to track down bugs.


> Anyway, Steven gave me hope that some kind of mutable float is possible
> in Python, and Ben perfectly expressed my need.  Again, alternatively,
> any module that handles in a convenient manner calculations with error
> propagation would be great, but I have not found any single such module!


http://pyinterval.googlecode.com/svn/trunk/html/index.html

http://docs.sympy.org/modules/mpmath/intervals.html

You should also read this:
http://conference.scipy.org/proceedings/SciPy2008/paper_3/



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


Re: ANN: PyGUI 2.0.1

2009-04-15 Thread fyaqq
I got the same error too.
On 4月14日, 上午2时47分, Andrew MacKeith  wrote:
> Greg Ewing wrote:
> >PyGUI2.0.1 is available:
>
> >  http://www.cosc.canterbury.ac.nz/greg.ewing/python_gui/
>
> > Fixes some problems in setup.py affecting installation
> > on Linux and Windows.
>
> > What isPyGUI?
> > --
>
> >PyGUIis a cross-platform GUI toolkit designed to be lightweight
> > and have a highly Pythonic API.
>
> I installedPyGUI-2.0.1 on Windows (32 bit, non Administrator access).
> I got the following error in 2.0.1 when attempting the BlobEdit example:
>
> D:\users\omh\runabq\pyGUI>c:\python26\python
> Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] 
> on win32
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> import pickle
>  >>> from GUI import Application,  ScrollableView, Document, Window, \
> ...   FileType, Cursor, rgb
>    File "", line 1, in 
>    File "c:\python26\Lib\site-packages\GUI\__init__.py", line 78, in 
> __getattr__
>      traceback.print_stack()
> Failed to import 'Application' from Applications
> Traceback (most recent call last):
>    File "c:\python26\Lib\site-packages\GUI\__init__.py", line 69, in 
> __getattr__
>      module = __import__(modname, self.__dict__, locals(), [name])
>    File "c:\python26\Lib\site-packages\GUI\Win32\Applications.py", line 8, in 
> 
>      import Components, Windows
>    File "c:\python26\Lib\site-packages\GUI\Win32\Components.py", line 12, in 
> 
>      from Win32 import win_none
>    File "c:\python26\lib\site-packages\GUI\Win32\__init__.py", line 2, in 
> 
>      raise ImportError("This should not be imported.")
> ImportError: This should not be imported.
>
> I presume that one should be able to import GUI.Win32 if I need to import 
> win_none from it.
>
> If I comment out line 2, I get the following error:
> ...
>    File "c:\python26\Lib\site-packages\GUI\Win32\Components.py", line 12, in 
> 
>      from Win32 import win_none
> ImportError: cannot import name win_none
>
> The directory structure that was installed is as follows:
> c:\python26\Lib\site-packages\GUI
> c:\python26\Lib\site-packages\GUI\Generic
> c:\python26\Lib\site-packages\GUI\Resources
> c:\python26\Lib\site-packages\GUI\Resources\cursors
> c:\python26\Lib\site-packages\GUI\Win32
>
> Andrew MacKeith

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


Re: subprocess: reading from stdout hangs process termination, waiting for ENTER keyboard signal

2009-04-15 Thread giohappy
On 15 Apr, 11:20, Kushal Kumaran  wrote:
> On Wed, Apr 15, 2009 at 1:20 PM,giohappy wrote:
> > On 14 Apr, 18:52, MRAB  wrote:
> >> giohappywrote:
> >> > Hello everyone.
> >> > I'm trying to use subprocess module to launch a Windows console
> >> > application. The application prints some results to standard output
> >> > and then waits for the user to press any key to terminte. I can't
> >> > control this behaviour, as the application is not mine...
> >> > I'm stuck at the very first lines of my code. I'm trying to force
> >> > process termination (even with proc.terminate()), and it works only if
> >> > I don't read from stdout. If I do proc.stdout.read() the process
> >> > hangs, and I have to manually press the keyboard to interrupt it.
> >> > Probably it's due a low-level handle that is kept on the process
> >> > stdout, waiting for the keypress event...
>
> >> > How can I solve it?
> >> > Giovanni
>
> >> > --- Code excerpt---
>
> >> > proc = subprocess.Popen('the_app.exe',
> >> >                        shell=True,
> >> >                        stdout=subprocess.PIPE,
> >> >                        )
> >> > #stdout_value = proc.communicate()[0]
> >> > stdout_value = proc.stdout.read()
> >> > PROCESS_TERMINATE = 1
> >> > handle = win32api.OpenProcess(PROCESS_TERMINATE, False, proc.pid)
> >> > win32api.TerminateProcess(handle, -1)
> >> > win32api.CloseHandle(handle)
> >> > print stdout_value
>
> >> Try this:
>
> >> proc = subprocess.Popen('the_app.exe',
> >>                         shell=True,
> >>                         stdin=subprocess.PIPE,
> >>                         stdout=subprocess.PIPE,
> >>                         )
> >> stdout_value = proc.communicate("\n")[0]
>
> > MRAB, I've tried that too but no result... I still have to press a
> > keybord key to terminate (the classical "Press any key to continue")
>
> If it actually is "Press any key to continue" rather than "Press Enter
> to continue", it is likely directly using the console using available
> low-level APIs, rather than reading from stdin.  AFAIK, subprocess
> cannot handle that.
>
> --
> kushal

If also tried with SendKeys [1], wich uses the windows.h keybd_event
(), but it doesn't work... Ok, I leave this try, and look for a way to
wrap the application in a bat file, hoping to succesfuly simulate the
keypress event inside it.

[1] http://www.rutherfurd.net/python/sendkeys/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Modifying the value of a float-like object

2009-04-15 Thread Piet van Oostrum
> [email protected] (ELB) wrote:

[snip]

>ELB> A couple of ideas I had:

>ELB> 1) Define a FloatWithUncert object, but get instance values as x(), as
>ELB> in "x()+y()".  The code is relatively legible.  'x' is mutable.  But
>ELB> formulas don't look so good, and you can't drop a float replacement
>ELB> for 'x', as floats are not callable.

>ELB> 2) Write all expressions that could contain FloatWithUncert objects
>ELB> with a 'float()' wrapper ("float(x)+float(y)"), after defining the
>ELB> FloatWithUncert.__float__() method.  FloatWithUncert would be
>ELB> mutable.  The code is a little bit heavy, but it is explicit.  'x'
>ELB> could be a pure float.

Would this be what you want?

class Float(object):
def __init__(self, value, uncert=0.0):
self.value = value
self.uncert = uncert

def __float__(self):
return self.value

def __str__(self):
return str(self.value)

def __add__(self, other):
return self.value + other

__radd__ = __add__

# etc for all other arithmetic and logical operators (but this can be
# automated, see the thread "Automatically generating arithmetic
# operations for a subclass")

>>> x = Float(3.14, 0.01)
>>> print x
3.14
>>> from math import sin
>>> sin(x)
0.0015926529164868282
>>> x.value = -3.14
>>> print x
-3.14
>>> sin(x)
-0.0015926529164868282
>>> x + 1
-2.1401
>>> y = Float(7.0, 0.3)
>>> print y
7.0
>>> x + y
3.8599
>>> 

If you write your own functions they might have to coerce your Float
objects to real floats ocassionally by using float(param) instead of
param. 
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to know argument name with which a function of extended c called

2009-04-15 Thread Aaron Brady
On Apr 15, 3:52 am, John Machin  wrote:
> On Apr 15, 6:13 pm, rahul  wrote:
>
>
>
> > On Apr 14, 6:24 pm, John Machin  wrote:
>
> > > On Apr 14, 10:35 pm, rahul  wrote:
>
> > > > Hi,
> > > >   i need to write a 'c extension function' in this function i need to
> > > > change argument value with  which this function called.
>
> > > The appropriate way for a function to give output is to return a
> > > value, or a tuple of values.
>
> > > example:
>
> > > def get_next_token(input_buffer, offset):
> > >    """get next lexical token, starting at offset
> > >       return (the_token, new offset)"""
> > >    length = find_len_of_token_somehow(input_buffer, offset)
> > >    new_offset = offset + length
> > >    return input_buffer[offset:new_offset], new_offset
>
> > > and you can call it by
> > >    token, pos = get_next_token(buff, pos)
> > >    return input
>
> > > >   ie,
> > > >          if a python code like
> > > >             import changeValue as c
> > > >             arg="old value"
> > > >             c.changeValue(arg)
> > > >             print arg
>
> > > Fortunately, you can't construct such a thing in Python or in a C
> > > extension. Consider the following:
>
> > > print "two", 2
> > > c.changeValue(2)
> > > print "two maybe", 2
>
> > > What would you want to it to print the second time?
> > > two maybe new value?
>
> > > >  then it print "new value"
>
> > > >  i write code like this..
>
> > > > static PyObject *changeValue(PyObject *self,PyObject *args){
> > > >         PyObject *sampleObj, *m ;
> > > >         char *argName;
>
> > > >       if (!PyArg_ParseTuple(args, "O", &sampleObj)){
> > > >                 return NULL;
> > > >       }
>
> > > >    m = PyImport_AddModule("__main__");
>
> > > This means you are assuming/hoping this function will be called only
> > > from the main script ...
>
> > > >    PyObject_SetAttrString(m, argName, "new value");
>
> > > Even if you know the name, you have the problem that it is changing
> > > the __main__ module's globals ... but the arg could be local or it
> > > could be an expression ...
>
> > > >    return Py_BuildValue("");
>
> > > > }
>
> > > > But for this i need to know the argument name with which this function
> > > > called .
> > > > Is this possible to know argument name in extended c function? if yes,
> > > > than how i can do it???
>
> > > No, it's not possible to know the argument name (without help from the
> > > caller e.g. keyword args), it may not even have a name, it may have
> > > multiple names ... this is just another variation of the old "what is
> > > the name of my object" FAQ.
>
> > > Why don't you tell us what you are trying to achieve (a higher-level
> > > goal than "I need to poke some value at/into some variable of doubtful
> > > name and unknowable location"), and then we might be able to give you
> > > some ideas.
>
> > > HTH,
> > > John
>
> > Hi John,
> >    thanks for your great full information,
> >      But, In my project anyhow i have to change argument value in some
> > cases.
>
> I say again: You can't do that. If you care to explain AT A HIGHER
> LEVEL than "change argument value" what you are trying to achieve,
> then some help might be available.
>
> If you are translating from C to Python and you have a function that
> is expected to be called like func(in1, &out2, &inout3) then you will
> have to write the function as
>    def func(in1, inout3):
>       # calculate out2
>       # calculate new_inout3
>       return out2, new_inout3
> (or the equivalent in a C-extension) and the caller will need to be
> changed to do
>    out2, inout3 = func(in1, inout3)
>
> Alternatively, leave the function in C and provide a wrapper function
> in C that bridges the different argument-passing conventions. There
> are various options for doing this -- the Cython/boost/ctypes/etc
> gurus can help with this once you've explained what your real
> requirement is.
>
> > can we pass pointer  of an variable in extended c function.
>
> Python doesn't have "variables". It has objects, which have zero, one,
> or many names. The names exist in namespaces of various types: local
> to a function/method, attribute of a class, attribute of a class
> instance, global to a module, ... there is no unified "address"
> concept; the concept "&arg" has no meaning and no means of realising
> it as a vehicle for doing the equivalent of C's "*pointer =
> new_value".
>
> HTH,
> John

There is a limited subset of cases where a function can modify its
argument.  The first of course is with mutable types, but the function
would not operate successfully on future types.  If it only needs to
handle one mutable type, then it's straightforward.

The second is with the 'ctypes.byref' function, which only accepts
ctypes types and structures, but can perform the swap, again, if the
types are known.

The generic solution is to just retrieve your value from the same
place in a container every time, i.e. 'Globals.val1', which you can
read and write at will.  The con

Re: Something weird about re.finditer()

2009-04-15 Thread Jeremiah Dodds
On Wed, Apr 15, 2009 at 9:46 AM, Gilles Ganault  wrote:

> Hello
>
>I stumbled upon something funny while downloading web pages and
> trying to extract one or more blocks from a page: Even though Python
> seems to return at least one block, it doesn't actually enter the for
> loop:
>
> ==
> re_block = re.compile('before (.+?) after',re.I|re.S|re.M)
>
> #Here, get web page and put it into "response"
>
> blocks = None
> blocks = re_block.finditer(response)
> if blocks == None:
>print "No block found"
> else:
>print "Before blocks"
>for block in blocks:
>#Never displayed!
>print "In blocks"
> ==
>
> Since "blocks" is no longer set to None after calling finditer()...
> but doesn't contain a single block... what does it contain then?
>
> Thank you for any tip.
> --
> http://mail.python.org/mailman/listinfo/python-list
>


It contains an iterator. It's just an iterator with nothing in it:

In [232]: import re

In [233]: re_block = re.compile('before (.+?) after',re.I|re.S|re.M)

In [234]: blocks = None

In [235]: blocks = re_block.finditer('Hi There Im not going to match
anything')

In [236]: blocks
Out[236]: 

In [237]: blocks == None
Out[237]: False

In [238]: for block in blocks:
   .: print block
   .:

In [239]: type(blocks)
Out[239]: 

In [241]: blocks.next()
---
StopIteration Traceback (most recent call last)

/home/jeremiah/ in ()

StopIteration:


Maybe you should just use a different method, like findall, where you can
check the length of it. Or, if you don't need to do anything special when
there aren't any blocks, you could continue like this.
--
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess: reading from stdout hangs process termination, waiting for ENTER keyboard signal

2009-04-15 Thread giohappy
On 15 Apr, 11:38, giohappy  wrote:
> On 15 Apr, 11:20, Kushal Kumaran  wrote:
>
>
>
> > On Wed, Apr 15, 2009 at 1:20 PM,giohappy wrote:
> > > On 14 Apr, 18:52, MRAB  wrote:
> > >> giohappywrote:
> > >> > Hello everyone.
> > >> > I'm trying to use subprocess module to launch a Windows console
> > >> > application. The application prints some results to standard output
> > >> > and then waits for the user to press any key to terminte. I can't
> > >> > control this behaviour, as the application is not mine...
> > >> > I'm stuck at the very first lines of my code. I'm trying to force
> > >> > process termination (even with proc.terminate()), and it works only if
> > >> > I don't read from stdout. If I do proc.stdout.read() the process
> > >> > hangs, and I have to manually press the keyboard to interrupt it.
> > >> > Probably it's due a low-level handle that is kept on the process
> > >> > stdout, waiting for the keypress event...
>
> > >> > How can I solve it?
> > >> > Giovanni
>
> > >> > --- Code excerpt---
>
> > >> > proc = subprocess.Popen('the_app.exe',
> > >> >                        shell=True,
> > >> >                        stdout=subprocess.PIPE,
> > >> >                        )
> > >> > #stdout_value = proc.communicate()[0]
> > >> > stdout_value = proc.stdout.read()
> > >> > PROCESS_TERMINATE = 1
> > >> > handle = win32api.OpenProcess(PROCESS_TERMINATE, False, proc.pid)
> > >> > win32api.TerminateProcess(handle, -1)
> > >> > win32api.CloseHandle(handle)
> > >> > print stdout_value
>
> > >> Try this:
>
> > >> proc = subprocess.Popen('the_app.exe',
> > >>                         shell=True,
> > >>                         stdin=subprocess.PIPE,
> > >>                         stdout=subprocess.PIPE,
> > >>                         )
> > >> stdout_value = proc.communicate("\n")[0]
>
> > > MRAB, I've tried that too but no result... I still have to press a
> > > keybord key to terminate (the classical "Press any key to continue")
>
> > If it actually is "Press any key to continue" rather than "Press Enter
> > to continue", it is likely directly using the console using available
> > low-level APIs, rather than reading from stdin.  AFAIK, subprocess
> > cannot handle that.
>
> > --
> > kushal
>
> If also tried with SendKeys [1], wich uses the windows.h keybd_event
> (), but it doesn't work... Ok, I leave this try, and look for a way to
> wrap the application in a bat file, hoping to succesfuly simulate the
> keypress event inside it.
>
> [1]http://www.rutherfurd.net/python/sendkeys/

Sorry, SendKeys is the solution. I was using it in the wronk place in
my script...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Something weird about re.finditer()

2009-04-15 Thread Justin Ezequiel
On Apr 15, 4:46 pm, Gilles Ganault  wrote:
> re_block = re.compile('before (.+?) after',re.I|re.S|re.M)
>
> #Here, get web page and put it into "response"
>
> blocks = None
> blocks = re_block.finditer(response)
> if blocks == None:
>         print "No block found"
> else:
>         print "Before blocks"
>         for block in blocks:
>                 #Never displayed!
>                 print "In blocks"
> ==
>
> Since "blocks" is no longer set to None after calling finditer()...
> but doesn't contain a single block... what does it contain then?
>
> Thank you for any tip.

because finditer returns a generator which in your case just happens
to be empty

>>> import re
>>> patt = re.compile('foo')
>>> gen = patt.finditer('bar')
>>> gen is None
False
>>> gen == None
False
>>> gen

>>> list(gen)
[]
>>>

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


Re: binary file compare...

2009-04-15 Thread Steven D'Aprano
On Wed, 15 Apr 2009 07:54:20 +0200, Martin wrote:

>> Perhaps I'm being dim, but how else are you going to decide if two
>> files are the same unless you compare the bytes in the files?
> 
> I'd say checksums, just about every download relies on checksums to
> verify you do have indeed the same file.

The checksum does look at every byte in each file. Checksumming isn't a 
way to avoid looking at each byte of the two files, it is a way of 
mapping all the bytes to a single number.



>> You could hash them and compare the hashes, but that's a lot more work
>> than just comparing the two byte streams.
> 
> hashing is not exactly much mork in it's simplest form it's 2 lines per
> file.

Hashing is a *lot* more work than just comparing two bytes. The MD5 
checksum has been specifically designed to be fast and compact, and the 
algorithm is still complicated:

http://en.wikipedia.org/wiki/MD5#Pseudocode

The reference implementation is here:

http://www.fastsum.com/rfc1321.php#APPENDIXA

SHA-1 is even more complicated still:

http://en.wikipedia.org/wiki/SHA_hash_functions#SHA-1_pseudocode


Just because *calling* some checksum function is easy doesn't make the 
checksum function itself simple. They do a LOT more work than just a 
simple comparison between bytes, and that's totally unnecessary work if 
you are making a one-off comparison of two local files.



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


Re: 3D plotting in a GUI

2009-04-15 Thread Almar Klein
Hi,
I'm actually busy writing a visualization package based on OpenGL. The
interface
is similar to matplotlib and Matlab. You can do regular plotting with it,
but is also
aimed at 3D visualization, including volume rendering.

It's not quite finished yet, so I'm not sure if it'll help you right now...
But maybe in
a few weeks I might release a first version. I want it to work with py2exe
myself,
so I hope to make that available out of the box.

You also mentioned fft, smoothing, etc. I would advice you to use scipy for
that.

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


Equivalent to C bitmasks and enumerations

2009-04-15 Thread Ulrich Eckhardt
Greetings!

I'm currently using Python to implement a set of tests for code that is
otherwise written in C. This code was wrapped using Boost.Python and is
then loaded into Python as module.

What I often have in C is this:

  // bitfield (several flags combined)
  #define STATUS_OVERTEMP 1u
  #define STATUS_ON_FIRE 2u
  #define STATUS_BORED 4u
  unsigned get_status(void);

  // enumeration (distinct values from a set)
  enum color { color_red=1, color_green=13, color_mauve=42 };
  enum color get_color(void);

What I'm looking for is a suggestion how to handle this in Python. Note that
technically, this works without problem, I'm rather looking for stylistic
advise. What I currently have is these (note: I'm retyping this, so ignore
any syntax errors, please):

 STATUS_OVERTEMP = 1
 STATUS_ON_FIRE = 2
 STATUS_BORED = 4
 def status_as_string(st):
 tmp = []
 if st&STATUS_OVERTEMP:
 tmp.append("OVERTEMP")
 if st&STATUS_ON_FIRE:
 tmp.append("ON_FIRE")
 if st&STATUS_BORED:
 tmp.append("BORED")
 return "|".join(tmp)

 COLOR_RED = 1
 COLOR_GREEN = 13
 COLOR_MAUVE = 42
 def color_as_string(c):
 names = {
 COLOR_RED:"RED",
 COLOR_GREEN:"GREEN",
 COLOR_MAUVE:"MAUVE"}
 return names[c];

Further, I also tried defining a separate class:

 class Color(int):
 RED = 1
 GREEN = 13
 MAUVE = 42
 names = { RED:"RED", GREEN:"GREEN", MAUVE:"MAUVE"}

 def __str__(self):
 return names[c];
 ...
  

To be a bit more explicit about what I would like, here is an example how I
would like to use it:

  c = Color.RED
  type(c) # should yield class Color
  str(c) # should yield "RED"
  Color(999) # should signal the invalid color value


Any other suggestions how to structure this or rewrite this?

thanks!

Uli

  
-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

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


Re: Modifying the value of a float-like object

2009-04-15 Thread Arnaud Delobelle
[email protected] writes:

> As for your idea of "straight-forward interval arithmetic", it's a
> good one, but I'd have to redefine lots of functions from the math
> module, to use them explicitly in my code, etc.: this is heavy; I was
> looking for a light-weight alternative, where all calculations are
> expressed in Python as if all numbers were regular floats, and where
> you don't have to redefine any mathematical operation.  In other
> words, I'm looking for a robust way of implementing the derive()
> function of Peter without knowing anything about which function uses
> which "numbers with an uncertainty": the equivalent of Peter's derive
> () would simply successively change all "numbers with uncertainty"
> defined so far and see how the result of a given calculation varies--
> the variables that are not used in the calculated expression don't
> change the result, for instance.  I understand that this is
> computationally inefficient (too many variables might be changed), but
> I don't need speed, only robustness in the propagation of errors, and
> a simple and very legible calculation code, and ideally with only a
> few lines of Python.

I still don't understand why you need mutable floats.

Here is a suggestion, based on Petter Otten's Value class and his derive
and calc functions.  I've modified Value slightly so that it implements
unary - and binary +, *, -, /, **. The 'for name in dir(math)' loop at
the end wraps each function in the math module in valueified (defined
below) so that they accept floats or Values.

 uncert.py ---

def valueified(f):
"""
Change a function that accepts floats to a function that accepts
any of float or Value.
"""
def decorated(*args):
ret = calc(f, map(Value, args))
return ret if ret.err else ret.value
return decorated

class Value(object): 
def __init__(self, value, err=0):
if isinstance(value, Value):
value, err = value.value, value.err
self.value = float(value) 
self.err = float(err) 
def __repr__(self): 
return "%r +- %r" % (self.value, self.err) 
__neg__ = valueified(float.__neg__)
for op in 'add', 'sub', 'mul', 'div', 'pow':
for r in '', 'r':
exec """__%s__ = valueified(float.__%s__)""" % (r+op, r+op)
del op, r

def derive(f, values, i, eps=1e-5):
x1 = f(*values) 
values = list(values) 
values[i] += eps 
x2 = f(*values) 
return (x2-x1)/eps 

def calc(f, args): 
values = [v.value for v in args] 
errs = [v.err for v in args] 
sigma = 0 
for i, (v, e) in enumerate(zip(values, errs)): 
x = derive(f, values, i)*e 
sigma += x*x 
return Value(f(*values), sigma**0.5)

builtinf = type(sum)
import math
for name in dir(math):
obj = getattr(math, name)
if isinstance(obj, builtinf):
setattr(math, name,  valueified(obj))

-- end of uncert.py ---

Example:

marigold:junk arno$ python -i uncert.py 
>>> a = Value(2, 0.1)
>>> b = Value(7, 2)
>>> a+b
9.0 +- 2.0024984393742682
>>> a*b
14.0 +- 4.0607881007017825
>>> from math import *
>>> sin(a)
0.90929742682568171 +- 0.041615138303141563
>>> sin(2)
0.90929742682568171
>>> 2*a
4.0 +- 0.200131024
>>> 2**b
128.0 +- 177.44629319622615
>>> a/b
0.2857142857142857 +- 0.082873111674175479
>>> sqrt(a**2+b**2)
7.2801098892805181 +- 1.9232454006952127
>>> hypot(a, b)
7.2801098892805181 +- 1.9232442191491188
>>> #etc

Isn't this what you need?

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


Re: Modifying the value of a float-like object

2009-04-15 Thread Eric . Le . Bigot
It looks like Dan found what is in effect a mutable float
(numpy.array).

Now, with respect to the initial problem of having mutable floats that
also contain an uncertainty attribute, I'd like to note that
numpy.ndarray can be subclassed: it now looks possible to create a
mutable float class that also contains an uncertainty attribute!

So, I'll see how/whether this can be implemented without pain...  I'll
leave a message here when I've got news!

Thanks again everybody for helping me out!

On Apr 15, 10:44 am, Dan Goodman  wrote:
> [email protected] wrote:
> > Hello,
>
> > Is there a way to easily build an object that behaves exactly like a
> > float, but whose value can be changed?  The goal is to maintain a list
> > [x, y,…] of these float-like objects, and to modify their value on the
> > fly (with something like x.value = 3.14) so that any expression like "x
> > +y" uses the new value.
>
> Hi Eric,
>
> Numpy's array object can do something like what you want:
>
> In [27]: x=array(0.0)
>
> In [28]: print x, sin(x)
> 0.0 0.0
>
> In [29]: x.itemset(pi/2)
>
> In [30]: print x, sin(x)
> 1.57079632679 1.0
>
> Not sure if this a recommended way of using array or not, but it seems
> to work. The problem is that any calculation you do with such an object
> will result in a float, and not another numpy array (although inplace
> operations work):
>
> In [40]: print (x*2).__class__
> 
>
> In [41]: x *= 2
>
> In [42]: print x.__class__
> 
>
> So it's not a perfect solution, but it might be OK for what you need.
>
> Dan

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


Re: HTML Conversion in python

2009-04-15 Thread [email protected]
On Apr 13, 5:43 pm, Aaron Watters  wrote:
> On Apr 13, 1:47 am, "[email protected]"
>
>  wrote:
> > Hello All,
> >             Is there any library defined in Python which can convert a
> > given text file into a html page. Basically, I require functions for
> > creating tables or filling background colours for the html pages etc
> > instead of writing each and every tag in my script..
>
> > Thank you,
> > Venu Madhav.
>
> At the risk of being annoying, I just announced
> a suite of tools that includes a methodology for
> building complex pages by combining simple page fragments.
>
> Please have a look at the tutorial
>  http://aaron.oirt.rutgers.edu/myapp/docs/W1100.tutorial
>
> This is the configuration source for the tutorial page
>
> {{use-url "comments"}}
>    {{section title}} Tutorial {{/section}}
>    {{section summary}}
>       Walking through some examples will help you learn
>       the organization and capabilities of the whiff
>       package.
>    {{/section}}
>    {{section body}}
>       These tutorials hope to introduce WHIFF by discussing a number
>       of use cases, beginning with simple ones and progressing to
> examples
>       with greater complexity.
>       
>       (UNDER CONSTRUCTION)
>    {{/section}}
> {{/use-url}}
>
> The page generated from this source is much
> more complex than that (take a look).  The
> "comments" URL points to another WHIFF template
> which defines the scaffolding for all WHIFF
> documentation pages.  The "comments" page in turn
> uses other fragments and scaffoldings...
>
> There are many other
> HTML generation approaches as indicated by the
> Wiki link previously.  As far as I
> know the WHIFF approach is the most "compositional".
> Please correct me if I'm wrong.
>
> Thanks, -- Aaron Watters
>
> ===
> 'To join the Guild I had to kill somebody,
> cruelly, with no mercy, and without reason
> -- so I killed the dwarf, because she was
> really annoying anyway.'
>   -- World of Warfare talk, overheard.

cheetah module will be very use full for yours requirement.

http://www.cheetahtemplate.org/index.html

Easily HTML pages can be develoed.
We will create a temples for each HTML pages and HTML pages will  be
generated.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Interrupt Python C API

2009-04-15 Thread Piet van Oostrum
> [email protected] (g) wrote:

>g> Hi,
>g> I just have a design problem and don't know how to solve it. I call a
>g> function which
>g> executes a simple "PyRun_String(...)" command.

>g> imagine the script while 1: pass is executed so the app would hang. Is
>g> there any chance
>g> to break out this PyRun_String-function? I just searched the forums
>g> for that stuff
>g> but these information are very rare.

>g> Thanks for any suggestions.

I think PyRun_String is similar to exec. If you are on a Unix system you
can use the alarm signal. Here is a pure Python example, but I suspect
you can translate this to C in a straightforward manner.

import signal

class AlarmError(Exception):
pass

def handler(signum, frame):
raise AlarmError, "command lasts too long"

signal.signal(signal.SIGALRM, handler)

def execute(command, timeout):
signal.alarm(timeout); 
try:
exec(command)
except AlarmError, e:
print e
print 'Aborted "%s"' % (command,)
print "continue work"

print "The everlasting command"
execute("while 1: pass", 10)
print "The End"


-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Calling user defined functions from a different script..

2009-04-15 Thread [email protected]
Hello all,
  I have a situation where I need to call functions present in
a different script whose hierarchy is something like below:

C:\Pythonlib\uitl\script1.py {Place where my functions definitions are
present}

C:\Scripts\script2.py { Place where function calls are made}

   In such a situation how can I import functions from
script1.py and use it in script2.py.


Please let me know if you need any further details.

Thank you,
Venu Madhav.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Calling user defined functions from a different script..

2009-04-15 Thread John Machin
On Apr 15, 9:33 pm, "[email protected]"
 wrote:
> Hello all,
>           I have a situation where I need to call functions present in
> a different script whose hierarchy is something like below:
>
> C:\Pythonlib\uitl\script1.py {Place where my functions definitions are
> present}
>
> C:\Scripts\script2.py { Place where function calls are made}
>
>            In such a situation how can I import functions from
> script1.py and use it in script2.py.

http://docs.python.org/tutorial/modules.html#the-module-search-path


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


Re: sharing/swapping items between lists

2009-04-15 Thread samwyse
On Apr 14, 7:01 pm, Aaron Brady  wrote:
> Here is an idea.  Create a list of all possible pairs, using
> itertools.combinations.  You'll notice everyone gets equal play time
> and equal time against each other on a pair-by-pair basis.  Then, call
> random.shuffle until one player isn't playing on two courts in one
> day.
>
> It has the disadvantage that you might end up with player A playing
> lots early on and rarely at the end, and B rarely early on and lots at
> the end.  Perhaps you could generate a few to several correct
> solutions, then choose the most evenly distributed.  Does this make
> sense?

Here's my idea:  generate all possible pairs:
>>> import itertools
>>> players = [chr(c) for c in xrange(ord('a'),ord('z')+1)]
>>> all_pairs = list(itertools.combinations(players,2))

partition the list:
>>> def choose_nonoverlapping(pairs):
chosen, leftover, used = list(), list(), list()
for p in pairs:
a, b = p
if a in used or b in used:
leftover.append(p)
else:
chosen.append(p)
used.append(a)
used.append(b)
return chosen, leftover

>>> court_count = 10
>>> week_count = 10
>>> pairs = all_pairs
>>> for week in xrange(week_count):
print 'week', week+1
this_week, pairs = choose_nonoverlapping(pairs)
print ', '.join(map(lambda t: ' vs '.join(t), this_week
[:court_count]))
pairs[0:0] = this_week[court_count:]


week 1
a vs b, c vs d, e vs f, g vs h, i vs j, k vs l, m vs n, o vs p, q vs
r, s vs t
week 2
u vs v, w vs x, y vs z, a vs c, b vs d, e vs g, f vs h, i vs k, j vs
l, m vs o
week 3
n vs p, q vs s, r vs t, a vs d, b vs c, e vs h, f vs g, i vs l, j vs
k, m vs u
week 4
o vs v, w vs y, x vs z, a vs e, b vs f, c vs g, d vs h, i vs m, j vs
n, k vs p
week 5
l vs q, r vs s, t vs u, a vs f, b vs e, c vs h, d vs g, i vs n, j vs
m, k vs o
week 6
p vs v, w vs z, x vs y, a vs g, b vs h, c vs e, d vs f, i vs o, j vs
q, k vs m
week 7
l vs n, r vs u, a vs h, b vs g, c vs f, d vs e, i vs p, j vs o, k vs
q, m vs s
week 8
t vs v, a vs i, b vs j, c vs k, d vs l, e vs m, f vs n, g vs o, h vs
p, q vs u
week 9
r vs w, s vs x, a vs j, b vs i, c vs l, d vs k, e vs n, f vs m, g vs
p, h vs o
week 10
q vs t, u vs y, v vs z, a vs k, b vs l, c vs i, d vs j, e vs o, f vs
p, g vs m
--
http://mail.python.org/mailman/listinfo/python-list


need to start a new project , can python do all that ?

2009-04-15 Thread Deep_Feelings
I want to start programming a new program (electronic health care
center) in python and before start learning python i wanna make sure
that python does have all the features i need to accomplish this
project so i wanna ask you does python able to support these
features :

1- cross platform (windows + linux)
2- mysql database access
3- 2D graphs (curves)
4- support of international languages
5- can access a scanner and input pictures from it.

and possibly be able to import data from labratory machines (such as
CBC machines) to automatically register patient investigations result
into the system (not mandatory)

so can python (with any freely available libraries) do all that and
still be cross platform ?

thankx in advance :)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Calling user defined functions from a different script..

2009-04-15 Thread Esmail

I think taking a look at

   sys.path.append()

might help.

Esmail

[email protected] wrote:

Hello all,
  I have a situation where I need to call functions present in
a different script whose hierarchy is something like below:

C:\Pythonlib\uitl\script1.py {Place where my functions definitions are
present}

C:\Scripts\script2.py { Place where function calls are made}

   In such a situation how can I import functions from
script1.py and use it in script2.py.



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


Re: Modifying the value of a float-like object

2009-04-15 Thread Piet van Oostrum
> [email protected] (ELB) wrote:

>ELB> To Dave A. and Piet: I appreciate your taking the time to make
>ELB> suggestions.  I understand that there is a hitch in the approach that
>ELB> you describe, which I would like to insist on: how do you handle
>ELB> functions that use math.sin(), for instance?  numpy does this kind of
>ELB> magic, but I'm not sure it's wise to spend time reproducing it.  I'd
>ELB> to not have to modify any calculation code, so that it is legible and
>ELB> general (in particular, so that it can work with Python floats).  And
>ELB> again, interval arithmetic fails to produce real standard deviations,
>ELB> because of correlations between uncertainties.

Mine does allow you to use math.sin(x) as you can see. Dave's also I
think. His solution is similar to mine but he puts the list stuff in it
whereas I came up with only the float.

In my solution I think you can mostly use normal formula's with the
Float objects, although there may be some functions that give problems
for example if they have explicit tests like type(x) == float. Why don't
you give it a try?
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: need to start a new project , can python do all that ?

2009-04-15 Thread Brendon Wickham
Python is a programming language, and like practically any programming
language it can do all those things. I'm not sure your requirements
are based on a full understanding of the implications. A "health care
center' cannot be made with a few "drag and drop", "plug and play"
modules that you manipulate within a GUI.

For example, importing pathology results: there is no international
standard for how such results should be formed. You will need to ask
the suppliers of the results to work out how they're going to give it
to you. And they will all be different. You will most likely need a
firm understanding of HL7 v2 and 3, XML, XSL (and probably web
services) etc.

In terms of cross platform, in the health context it's pointless being
anything other than a web application (IMHO).


Cheers,

Brendon


2009/4/15 Deep_Feelings :
> I want to start programming a new program (electronic health care
> center) in python and before start learning python i wanna make sure
> that python does have all the features i need to accomplish this
> project so i wanna ask you does python able to support these
> features :
>
> 1- cross platform (windows + linux)
> 2- mysql database access
> 3- 2D graphs (curves)
> 4- support of international languages
> 5- can access a scanner and input pictures from it.
>
> and possibly be able to import data from labratory machines (such as
> CBC machines) to automatically register patient investigations result
> into the system (not mandatory)
>
> so can python (with any freely available libraries) do all that and
> still be cross platform ?
>
> thankx in advance :)
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: need to start a new project , can python do all that ?

2009-04-15 Thread Chris Rebert
On Wed, Apr 15, 2009 at 5:22 AM, Deep_Feelings  wrote:
> I want to start programming a new program (electronic health care
> center) in python and before start learning python i wanna make sure
> that python does have all the features i need to accomplish this
> project so i wanna ask you does python able to support these
> features :
>
> 1- cross platform (windows + linux)
Yes, Python is cross-platform. Did you glance at python.org ?

> 2- mysql database access
http://mysql-python.sourceforge.net/

> 3- 2D graphs (curves)
http://matplotlib.sourceforge.net/

> 4- support of international languages
Yes, it supports Unicode.

> 5- can access a scanner and input pictures from it.
I was unable to locate anything, but some GUI library might have bindings.

> and possibly be able to import data from labratory machines (such as
> CBC machines) to automatically register patient investigations result
> into the system (not mandatory)
Way too vague for me to google for.

Cheers,
Chris
-- 
I have a blog:
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Calling user defined functions from a different script..

2009-04-15 Thread Aaron Brady
On Apr 15, 6:33 am, "[email protected]"
 wrote:
> Hello all,
>           I have a situation where I need to call functions present in
> a different script whose hierarchy is something like below:
>
> C:\Pythonlib\uitl\script1.py {Place where my functions definitions are
> present}
>
> C:\Scripts\script2.py { Place where function calls are made}
>
>            In such a situation how can I import functions from
> script1.py and use it in script2.py.
>
> Please let me know if you need any further details.
>
> Thank you,
> Venu Madhav.

>From what I understand, you just want a certain line in a certain
file.  The line is the path to the directory you want to import from,
pythonlib\util.  The file is {python_direcotry\}Lib\site-packages
\locals.pth.

Here's mine:

C:\Programs\Python25\Lib\site-packages>type locals.pth
/Documents and Settings/usr/My Documents/
/Documents and Settings/usr/Desktop/working/imptest/

If you want to import multiple directories, you need another file
which will be blank.  The blank file goes in the directory you will be
importing.  It is called '__init__.py'.

Mine:

 Directory of C:\Documents and Settings\usr\My Documents\Lib

04/08/2008  03:35 PM 0 __init__.py

Then I can do:

C:\Programs\Python25>python
Python 2.5.4 (r254:67916, Dec 23 2008
Type "help", "copyright", "credits" o
>>> import Lib

I'm not sure about other versions than 2.5.
--
http://mail.python.org/mailman/listinfo/python-list


Re: need to start a new project , can python do all that ?

2009-04-15 Thread Krishnakant
On Wed, 2009-04-15 at 05:22 -0700, Deep_Feelings wrote:
> I want to start programming a new program (electronic health care
> center) in python and before start learning python i wanna make sure
> that python does have all the features i need to accomplish this
> project so i wanna ask you does python able to support these
> features :
> 
Python of course fits such projects and due to the fact that it is fast
and zippi, it becomes the bes choice.
> 1- cross platform (windows + linux)
Very much.
> 2- mysql database access
The python-mysql module complies with the python db api and talks to
mysql.
As a side note, postgresql is a really scalable, robust and powerful
database and python has a very matured library for talking to it as
well. 
> 3- 2D graphs (curves)
Possible with many libraries.
> 4- support of international languages
> 5- can access a scanner and input pictures from it.
> 
Internationalisation is there in almost every language these days and
python is no exception.

happy hacking.
Krishnakant.


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


Re: Modifying the value of a float-like object

2009-04-15 Thread Eric . Le . Bigot
Arnaud, your code is very interesting!

On Apr 15, 1:00 pm, Arnaud Delobelle  wrote:
> I still don't understand why you need mutable floats.

Here is why: the code that your proposed (or any code that does
straightforward error propagation, for that matter) does not generally
calculate uncertainties correctly:

>>> a=Value(1, 0.1)
>>> a-a
0.0 +- 0.14142135623823598

The correct result is obviously 0.0 +- 0.0.  This is the effect of
what I referred to in previous posts as "correlated errors".

Anyway, I learned some interesting stuff about what you can do in
Python, thanks to your code! :)
--
http://mail.python.org/mailman/listinfo/python-list


Re: need to start a new project , can python do all that ?

2009-04-15 Thread Tim Rowe
2009/4/15 Deep_Feelings :
> I want to start programming a new program (electronic health care
> center) in python and before start learning python i wanna make sure
> that python does have all the features i need to accomplish this
> project so i wanna ask you does python able to support these
> features :
>
> 1- cross platform (windows + linux)
> 2- mysql database access
> 3- 2D graphs (curves)
> 4- support of international languages
> 5- can access a scanner and input pictures from it.
>
> and possibly be able to import data from labratory machines (such as
> CBC machines) to automatically register patient investigations result
> into the system (not mandatory)

What are the safety and security requirements? If you're handling
patient investigation results then there are certainly security issues
because of patient confidentiality, and there may be safety issues
(could a software fault contribute to a patient receiving incorrect
treatment, or failing to receive necessary treatment?)

You almost certainly need to contact the appropriate regulatory
authority to check whether they have any requirements for languages in
such applications (and for specific development processes), or you
could find yourself either with an application you can't use or a very
big lawsuit and possibly jail if it goes wrong.

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


Re: sharing/swapping items between lists

2009-04-15 Thread Aaron Brady
On Apr 15, 6:57 am, samwyse  wrote:
> On Apr 14, 7:01 pm, Aaron Brady  wrote:
>
> > Here is an idea.  Create a list of all possible pairs, using
> > itertools.combinations.  You'll notice everyone gets equal play time
> > and equal time against each other on a pair-by-pair basis.  Then, call
> > random.shuffle until one player isn't playing on two courts in one
> > day.
>
> > It has the disadvantage that you might end up with player A playing
> > lots early on and rarely at the end, and B rarely early on and lots at
> > the end.  Perhaps you could generate a few to several correct
> > solutions, then choose the most evenly distributed.  Does this make
> > sense?
>
> Here's my idea:  generate all possible pairs:
>
> >>> import itertools
> >>> players = [chr(c) for c in xrange(ord('a'),ord('z')+1)]
> >>> all_pairs = list(itertools.combinations(players,2))
>
> partition the list:>>> def choose_nonoverlapping(pairs):
>
>         chosen, leftover, used = list(), list(), list()
>         for p in pairs:
>                 a, b = p
>                 if a in used or b in used:
>                         leftover.append(p)
>                 else:
>                         chosen.append(p)
>                         used.append(a)
>                         used.append(b)
>         return chosen, leftover
>
> >>> court_count = 10
> >>> week_count = 10
> >>> pairs = all_pairs
> >>> for week in xrange(week_count):
>
>         print 'week', week+1
>         this_week, pairs = choose_nonoverlapping(pairs)
>         print ', '.join(map(lambda t: ' vs '.join(t), this_week
> [:court_count]))
>         pairs[0:0] = this_week[court_count:]
>
snip

Your idea arrives at a sub-optimal solution on players= 'abcdef',
court_count= 3.

Correct, by hand (5 weeks):

ab cd ef
ac be df
ad ce bf
ae bd cf
af bc de

Program (7 weeks):

week 1
a vs b, c vs d, e vs f
week 2
a vs c, b vs d
week 3
a vs d, b vs c
week 4
a vs e, b vs f
week 5
a vs f, b vs e
week 6
c vs e, d vs f
week 7
c vs f, d vs e

However, you do correctly arrive at all the combinations, in better
than the naive 'one pair per week' solution.  Further, you produced
the correct solution for players= 'abcdef', for court_count= 1 and
court_count= 2, which I also tested.  Damage report?
--
http://mail.python.org/mailman/listinfo/python-list


Python unit tests.

2009-04-15 Thread alessiogiovanni . baroni
Hi to all,
how do I read the info at http://coverage.livinglogic.de ? For
example, in http://coverage.livinglogic.de/Objects/funcobject.c.html,
the count field, what it means?

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


Re: binary file compare...

2009-04-15 Thread Martin
On Wed, Apr 15, 2009 at 11:03 AM, Steven D'Aprano
 wrote:
> The checksum does look at every byte in each file. Checksumming isn't a
> way to avoid looking at each byte of the two files, it is a way of
> mapping all the bytes to a single number.

My understanding of the original question was a way to determine
wether 2 files are equal or not. Creating a checksum of 1-n files and
comparing those checksums IMHO is a valid way to do that. I know it's
a (one way) mapping between a (possibly) longer byte sequence and
another one, how does checksumming not take each byte in the original
sequence into account.

I'd still say rather burn CPU cycles than development hours (if I got
the question right), if not then with binary files you will have to
find some way of representing differences between the 2 files in a
readable manner anyway.

> Hashing is a *lot* more work than just comparing two bytes. The MD5
> checksum has been specifically designed to be fast and compact, and the
> algorithm is still complicated:

I know that the various checksum algorithms aren't exactly cheap, but
I do think that just to know wether 2 files are different a solution
which takes 5mins to implement wins against a lengthy discussion which
optimizes too early wins hands down.

regards,
martin

-- 
http://soup.alt.delete.co.at
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher

You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: Modifying the value of a float-like object

2009-04-15 Thread Eric . Le . Bigot
Thanks, Piet!  Before reading your post, I did not know that defining
__float__() was enough for math.sin() to be able to calculate
something!

To summarize my current understanding for the original problem:

- Mutable floats seem to be the only way of performing (correct)
uncertainty calculations without modifying the code of the modules in
which the calculations are done (in particular when we call object
methods that perform calculations whose parameters are _not passed as
arguments by the caller_ [Peter's post shows a simpler, less general
case]).

- A mutable float can be obtained through a numpy.ndarray object (see
Dan's post), which can be subclassed so as to add an "uncertainty"
attribute.  Pros: the way the mutable float behaves in calculation is
taken care of by numpy.  Cons: this requires numpy.

- A mutable float can also be created directly (see Piet's post), and
the regular float behavior can be quite well (or fully?) approximated
by defining a __float__() member, as well as implementing the basic
methods of Python's floats (addition, etc.).  Pros: this is clean
Python.  Cons: as Piet noted, one might have to explicitly use float
(x) in some expressions.

I'd like to add (in particular for those who think that mutable floats
are dangerous beasts to play with) that the example "x = y" given in
previous posts (where "y" is a number with uncertainty) actually gives
the intended behavior when calculating "x-y" with its uncertainty,
which would not be the case if "y" was copied by value into "x".  When
it comes to calculating uncertainties, this example shows that
mutability is actually a good thing.

Well, that's two implementations to be tried!  I'll get back to this
thread with the results!  Thanks for the lively thread!

On Apr 15, 1:41 pm, Piet van Oostrum  wrote:
> In my solution I think you can mostly use normal formula's with the
> Float objects, although there may be some functions that give problems
> for example if they have explicit tests like type(x) == float. Why don't
> you give it a try?
> --
> Piet van Oostrum 
> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
> Private email: [email protected]

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


Lambda alternative?

2009-04-15 Thread mousemeat
Hello all,

I really like the lambda function for a particular task i have: I am
writing some simulation software, working with many different
materials. Each material with many different properties, some are
temperature dependent, others aren't, some are defined by a function,
some are a lookup table, some are constant. Here is a simplified
version:

class material(object):
def __init__(self,density):
self.density=density

airdensity=lambda T:10/(287*T)
air=material(airdensity)

steeldensity=lambda T:interp(T,[0,1000],[7856,7813])
steel=material(steeldensity)

rockdensity=lambda T:5000
rock=material(rockdensity)

I really like doing things this way, because i can then define other
properties within the class and i can always use the syntax:
specificgravity_at_273K=stone.specificgravity(273), whether
specificgravity is a function or a parameter. (not sure thats the
right nomenclature, but i think you'll know what i mean.)

But, lambda functions can't be pickled. I would like to pickle my
objects, and i would really like to use parallel python (which
requires pickling).

The best way i can see to do that is to make a new class which
inheirits the 'material' class for each physical material i want to
model, but surely i should be creating instances rather than classes.

Is this going to present some new problems that anyone can forsee? Can
anyone think of a better way to do this?  I keep reading that lambda
functions aren't particularly pythonic and that there are always
alternatives, but they seem very elegant to me.

Thanks for your time,

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


Re: sharing/swapping items between lists

2009-04-15 Thread Aaron Brady
On Apr 14, 9:45 pm, Ross  wrote:
> On Apr 14, 7:18 pm, Aaron Brady  wrote:
>
>
>
> > On Apr 14, 7:01 pm, Aaron Brady  wrote:
>
> > > On Apr 14, 12:37 pm, Ross  wrote:
>
> > > > On Apr 14, 10:34 am, Ross  wrote:
>
> > > > > On Apr 14, 5:57 am, [email protected] (Aahz) wrote:
>
> > > > > > In article 
> > > > > > ,
>
> > > > > > Ross   wrote:
> > > > > > >On Apr 13, 9:08=A0am, [email protected] (Aahz) wrote:
> > > > > > >> In article 
> > > > > > >>  > > > > > >com>,
> > > > > > >> Ross =A0 wrote:
>
> > > > > > >>>I'm sorry...my example was probably a bad one. A better example 
> > > > > > >>>of
> > > > > > >>>output I would like would be something like [[1,2],[3,4],[5,6]] 
> > > > > > >>>and
> > > > > > >>>then for the leftovers list [7,8,9,10 etc]. What I'm trying to 
> > > > > > >>>do is
> > > > > > >>>produce some sort of round robin algorithm for tennis that is
> > > > > > >>>constrained by the number of courts available each week. So if 
> > > > > > >>>there
> > > > > > >>>are only 3 courts available for a singles league and 10 people 
> > > > > > >>>have
> > > > > > >>>signed up, 4 players will have a bye each week. I want my 
> > > > > > >>>algorithm to
> > > > > > >>>produce unique matchups each week and also give each player the 
> > > > > > >>>same
> > > > > > >>>angle?
>
> > > > > > >> How about Googling for "round robin algorithm python"? ;-)
>
> > > > > > >I have the basic algorithm and it works fine...I'm just having 
> > > > > > >trouble
> > > > > > >adding another parameter to it that allows for court constraints 
> > > > > > >and
> > > > > > >bye weeks.
>
> > > > > > You'll need to give us more information, then.  Why don't you start 
> > > > > > with
> > > > > > the core algorithm you're using?
> > > > > > --
> > > > > > Aahz ([email protected])           <*>        
> > > > > > http://www.pythoncraft.com/
>
> > > > > > Why is this newsgroup different from all other newsgroups?
>
> > > > > Here's the core algorithm I'm using:
>
> > > > > >>> def round_robin(teams,rounds):
>
> > > > >         if len(teams)%2:
> > > > >                 teams.append(None)
> > > > >         mid = len(teams) //2
> > > > >         for i in range(rounds):
> > > > >                 yield zip(teams[:mid], teams[mid:])
> > > > >                 teams = teams[0:1] + teams[mid:mid+1] + 
> > > > > teams[1:mid-1]+teams[mid
> > > > > +1:]+teams[mid-1:mid]
>
> > > > > >>> if __name__== '__main__':
>
> > > > >         rounds = 15
> > > > >         teams = range(16)
> > > > >         for round in round_robin(teams,rounds):
> > > > >                 print round
>
> > > > fyi rounds=15 and teams =range(16) was just test code I was playing
> > > > around with...they could theoretically be anything.
>
> > > Here is an idea.  Create a list of all possible pairs, using
> > > itertools.combinations.  You'll notice everyone gets equal play time
> > > and equal time against each other on a pair-by-pair basis.  Then, call
> > > random.shuffle until one player isn't playing on two courts in one
> > > day.
>
> > This might take a long time.  Not that I can guarantee that a depth-
> > first-search would be any faster, or that a breadth-first-search would
> > run faster *and* run in available memory.  
>
> I have a sub-optimal solution that I'm playing with now. Since my
> output is a list of tuples and looks something like this (if there
> were 16 teams and 15 rounds), I could designate a each nth tuple in
> each round as a bye, but since the 1st item in my list remains fixed,
> it's suboptimal. For example, you could say every 4th (and/or 3rd ,
> 5th, etc depending on how many available cts) tuple in each round gets
> a bye and pop() it from the list...:


The randomizing solution isn't quite suitable for 16 teams.  With 5
teams/1 court, and 5 teams/2 courts, 6 teams/2 courts, the solution
comes within seconds.  For 7 teams/3 courts, the solution takes a few
minutes.  (1 GHz.) Here's the code.  I doubt it's adequate, but it
still could be faster, and is definitely less stressful, than by
hand.  For any long-running calculations, save your results.

from itertools import combinations, permutations
import string
import random as ran
def circulate( num_players, num_courts ):
''' 2 players per court '''
assert num_players<= 26, '26 players max'
assert num_players> 2* num_courts, 'no randomization needed'
player_set= set( string.ascii_lowercase[ :num_players ] )
combs= list( ''.join( x ) for x in combinations( player_set, 2 ) )

#print( len( list( permutations( combs ) ) ) )

iteration= 0
while 1:
ran.shuffle( combs )
#print( combs )
ok= True
for i in range( 0, len( combs ), num_courts ):
cur= ''.join( combs[ i: i+ num_courts ] )
#print( cur )
if len( set( cur ) )!= len( cur ): # any dupes in round
ok= False
break
if ok:
return [ combs[ i: i+ num_courts ] for i in range( 0, len
( co

Re: binary file compare...

2009-04-15 Thread Grant Edwards
On 2009-04-15, Martin  wrote:
> Hi,
>
> On Mon, Apr 13, 2009 at 10:03 PM, Grant Edwards  wrote:
>> On 2009-04-13, SpreadTooThin  wrote:
>>
>>> I want to compare two binary files and see if they are the same.
>>> I see the filecmp.cmp function but I don't get a warm fuzzy feeling
>>> that it is doing a byte by byte comparison of two files to see if they
>>> are they same.
>>
>> Perhaps I'm being dim, but how else are you going to decide if
>> two files are the same unless you compare the bytes in the
>> files?
>
> I'd say checksums, just about every download relies on checksums to
> verify you do have indeed the same file.

That's slower than a byte-by-byte compare.

>> You could hash them and compare the hashes, but that's a lot
>> more work than just comparing the two byte streams.
>
> hashing is not exactly much mork in it's simplest form it's 2
> lines per file.

I meant a lot more CPU time/cycles.

-- 
Grant Edwards   grante Yow! Was my SOY LOAF left
  at   out in th'RAIN?  It tastes
   visi.comREAL GOOD!!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Modifying the value of a float-like object

2009-04-15 Thread Steven D'Aprano
On Wed, 15 Apr 2009 05:59:39 -0400, Dave Angel wrote:

> Steven D'Aprano wrote:
>> On Tue, 14 Apr 2009 14:45:47 -0400, Dave Angel wrote:
>>
>>
>>> The answer to your original question is no.  If the value can be
>>> changed, then it doesn't behave like a float.  And that's not just a
>>> pedantic answer, it's a serious consideration.
>>> 
>>> 
>> Oh nonsense. Many programming languages have mutable floats.
>>
>>
>>
> That's irrelevant.  Python doesn't.  So introducing one will quite
> likely alter the OP's code's behavior.  It doesn't matter if it's
> possible, it matters whether the existing code's behavior might change,
> and of course if a future maintainer might have trouble making sense of
> it.

What are you talking about? Python introduces new types quite frequently. 
We now have sets, frozensets, rationals, decimals and namedtuples, and 
we'll soon be getting ordereddicts and probably others as well. People 
create new types in their code *all the time*, using the class statement. 
If the OP wants to create a MutableFloat type, that's his prerogative.


> BTW, just what languages have mutable floats?

C and Pascal. Probably Java. As far as I know, any language with 
variables. Here's a sample piece of Pascal code which demonstrates that 
assignment to a float mutates the value in a fixed memory location:


program main(input, output);
  var
x: real;  {called 'float' in other languages}
ptr: ^real;

begin
  x := 23.0;
  ptr := @x;  {point ptr to the location of x}
  writeln('Before: x = ', x, '; address = ', integer(ptr));
  x := 42.0;
  ptr := @x;
  writeln('After: x = ', x, '; address = ', integer(ptr));
end.


Executing it, I get:

Before: x =  2.300e+01; address = 134692368
After: x =  4.200e+01; address = 134692368

The equivalent in Python is this:

>>> x = 23.0
>>> print 'Before: x = ', x, '; address = ', id(x)
Before: x =  23.0 ; address =  147599964
>>> x = 42.0
>>> print 'After: x = ', x, '; address = ', id(x)
After: x =  42.0 ; address =  147599980

As you can see, x becomes bound to a completely different float object.


> I don't think I've come
> across any since Fortran (on a CDC 6400), and I don't think that was
> deliberate.  In that implementation it was possible to change the value
> of a literal 2.0 to something else.

Who is talking about changing the values of float literals? That would be 
very silly.


> If you're going to use the fact that many languages pass arguments by
> reference, 

Nobody is talking about parameter passing conventions.


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


Re: binary file compare...

2009-04-15 Thread Grant Edwards
On 2009-04-15, Martin  wrote:
> On Wed, Apr 15, 2009 at 11:03 AM, Steven D'Aprano

> I'd still say rather burn CPU cycles than development hours (if I got
> the question right),

_Hours_?  Calling the file compare module takes  _one_line_of_code_.

Implementing a file compare from scratch takes about a half
dozen lines of code.

> if not then with binary files you will have to find some way
> of representing differences between the 2 files in a readable
> manner anyway.

 1) Who said anything about a readable representation of the
differences?

 2) How does a checksum provide that?

>> Hashing is a *lot* more work than just comparing two bytes.
>> The MD5 checksum has been specifically designed to be fast and
>> compact, and the algorithm is still complicated:
>
> I know that the various checksum algorithms aren't exactly
> cheap, but I do think that just to know wether 2 files are
> different a solution which takes 5mins to implement wins
> against a lengthy discussion

Bah.  A direct compare is trivial.  The discussion of which
checksum to use, how to implement it, and how reliable it is
will be far longer than any discussion over a direct
comparison.

> which optimizes too early wins hands down.

Optimizes too early?  Comparing the bytes is the simplest and
most direct, obvious solution.  It takes one line of code to
call the file compare module.  Implementing it from scratch
takes about five lines of code.

We all rail against premature optimization, but using a
checksum instead of a direct comparison is premature
unoptimization.  ;)

-- 
Grant Edwards   grante Yow! Hmmm ... A hash-singer
  at   and a cross-eyed guy were
   visi.comSLEEPING on a deserted
   island, when ...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python unit tests.

2009-04-15 Thread skip
> how do I read the info at http://coverage.livinglogic.de ? For
> example, in http://coverage.livinglogic.de/Objects/funcobject.c.html,
> the count field, what it means?

Most likely it's the number of times that line was executed.  Strong support
for that comes from the fact that the lines with a count of 0 are
highlighted, implying they weren't executed at all.

-- 
Skip Montanaro - [email protected] - http://www.smontanaro.net/
"XML sucks, dictionaries rock" - Dave Beazley
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to create a virtual serial port?

2009-04-15 Thread Grant Edwards
On 2009-04-15, Dennis Lee Bieber  wrote:
> On Sun, 12 Apr 2009 03:20:30 + (UTC), JanC 
> declaimed the following in gmane.comp.python.general:
>
>> 
>> USB *is* a serial port... that's what the "S" stands for.  ;)
>>
>   It is a serial protocol, but it is a shared bus-based protocol, in
> contrast to the 1<>1 protocol used by serial ports.

in contrast to the 1<>1 protocol used by _most_ serial ports.  

There are plenty of bus architectures based on serial ports
(and generally one-master, many-slave, just like USB).

-- 
Grant Edwards   grante Yow! World War Three can
  at   be averted by adherence
   visi.comto a strictly enforced
   dress code!
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-15 Thread P.J. Eby

At 09:51 AM 4/15/2009 +0200, M.-A. Lemburg wrote:

On 2009-04-15 02:32, P.J. Eby wrote:
> At 10:59 PM 4/14/2009 +0200, M.-A. Lemburg wrote:
>> You are missing the point: When breaking up a large package that lives in
>> site-packages into smaller distribution bundles, you don't need namespace
>> packages at all, so the PEP doesn't apply.
>>
>> The way this works is by having a base distribution bundle that includes
>> the needed __init__.py file and a set of extension bundles the add
>> other files to the same directory (without including another copy of
>> __init__.py). The extension bundles include a dependency on the base
>> package to make sure that it always gets installed first.
>
> If we're going to keep that practice, there's no point to having the
> PEP: all three methods (base+extensions, pkgutil, setuptools) all work
> just fine as they are, with no changes to importing or the stdlib.

Again: the PEP is about creating a standard for namespace
packages. It's not about making namespace packages easy to use for
Linux distribution maintainers. Instead, it's targeting *developers*
that want to enable shipping a single package in multiple, separate
pieces, giving the user the freedom to the select the ones she needs.

Of course, this is possible today using various other techniques. The
point is that there is no standard for namespace packages and that's
what the PEP is trying to solve.

> In particular, without the feature of being able to drop that practice,
> there would be no reason for setuptools to adopt the PEP.  That's why
> I'm -1 on your proposal: it's actually inferior to the methods we
> already have today.

It's simpler and more in line with the Python Zen, not inferior.

You are free not to support it in setuptools - the methods
implemented in setuptools will continue to work as they are,
but continue to require support code and, over time, no longer
be compatible with other tools building upon the standard
defined in the PEP.

In the end, it's the user that decides: whether to go with a
standard or not.


Up until this point, I've been trying to help you understand the use 
cases, but it's clear now that you already understand them, you just 
don't care.


That wouldn't be a problem if you just stayed on the sidelines, 
instead of actively working to make those use cases more difficult 
for everyone else than they already are.


Anyway, since you clearly understand precisely what you're doing, I'm 
now going to stop trying to explain things, as my responses are 
apparently just encouraging you, and possibly convincing bystanders 
that there's some genuine controversy here as well.


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


Re: Lambda alternative?

2009-04-15 Thread Paul Rubin
[email protected] writes:
> But, lambda functions can't be pickled. I would like to pickle my
> objects, and i would really like to use parallel python (which
> requires pickling).

Maybe you could use marshal instead of pickle.  Make sure to read the
documentation carefully--there are some pitfalls that can bite you.
--
http://mail.python.org/mailman/listinfo/python-list


Re: need to start a new project , can python do all that ?

2009-04-15 Thread Deep_Feelings
On Apr 15, 4:05 pm, Tim Rowe  wrote:
> 2009/4/15 Deep_Feelings :
>
> > I want to start programming a new program (electronic health care
> > center) in python and before start learning python i wanna make sure
> > that python does have all the features i need to accomplish this
> > project so i wanna ask you does python able to support these
> > features :
>
> > 1- cross platform (windows + linux)
> > 2- mysql database access
> > 3- 2D graphs (curves)
> > 4- support of international languages
> > 5- can access a scanner and input pictures from it.
>
> > and possibly be able to import data from labratory machines (such as
> > CBC machines) to automatically register patient investigations result
> > into the system (not mandatory)
>
> What are the safety and security requirements? If you're handling
> patient investigation results then there are certainly security issues
> because of patient confidentiality, and there may be safety issues
> (could a software fault contribute to a patient receiving incorrect
> treatment, or failing to receive necessary treatment?)
>
> You almost certainly need to contact the appropriate regulatory
> authority to check whether they have any requirements for languages in
> such applications (and for specific development processes), or you
> could find yourself either with an application you can't use or a very
> big lawsuit and possibly jail if it goes wrong.
>
> --
> Tim Rowe

thank you so much ,rest assured that the code will me tested very well
(in real world situation) before using it.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Lambda alternative?

2009-04-15 Thread Duncan Booth
[email protected] wrote:

> class material(object):
> def __init__(self,density):
> self.density=density
> 
> airdensity=lambda T:10/(287*T)
> air=material(airdensity)

What exactly is your objection to using a named function here?

def airdensity(T):
return 10/(287*T)
air=material(airdensity)

Surely that would solve your pickle problem *and* give you more meaningful 
tracebacks in your exceptions?

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


Question to python C API

2009-04-15 Thread Andreas Otto
Hi,

  I have the following question ...

  I write a custom "*.init" method and expect a variable number or arguments

  This are my questions:

  1.I need something like a for loop to analyse this arguments
For now I try the "PyArg_ParseTupleAndKeywords" but i missing somehing 
like an Object-Array return value as "format" 

  2.can i combine variable args with keywords ?
because it will fit into my spec to use both together

  3.I want to retrieve a bool object as int value, where is a format "O"
and "O!" for a type object  HOWTO put the type into the function 
from
above
  
  
mfg

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


Re: Modifying the value of a float-like object

2009-04-15 Thread Piet van Oostrum
I think the term 'mutable float' is causing a lot of confusion. My
solution I wouldn't call a mutable float, but a float container that
tries to act like a float in a context where this is required.

Another solution would have been to use automatically dereferencing
pointers but that is something Python does not support. That also would
be very close to C++'s ref (&) parameters.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Lambda alternative?

2009-04-15 Thread Paul Rubin
Duncan Booth  writes:
> What exactly is your objection to using a named function here?...
> Surely that would solve your pickle problem *and* give you more meaningful 
> tracebacks in your exceptions?

I don't think it would help the pickle problem.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Lambda alternative?

2009-04-15 Thread Piet van Oostrum
> [email protected] (m) wrote:

>m> Hello all,
>m> I really like the lambda function for a particular task i have: I am
>m> writing some simulation software, working with many different
>m> materials. Each material with many different properties, some are
>m> temperature dependent, others aren't, some are defined by a function,
>m> some are a lookup table, some are constant. Here is a simplified
>m> version:

>m> class material(object):
>m> def __init__(self,density):
>m> self.density=density

>m> airdensity=lambda T:10/(287*T)
>m> air=material(airdensity)

>m> steeldensity=lambda T:interp(T,[0,1000],[7856,7813])
>m> steel=material(steeldensity)

>m> rockdensity=lambda T:5000
>m> rock=material(rockdensity)

The following isn't much different (only 6 chars more per def):
  
def airdensity(T): return 10/(287*T)
air=material(airdensity)

def steeldensity(T): return interp(T,[0,1000],[7856,7813])
steel=material(steeldensity)

def rockdensity(T): return 5000
rock=material(rockdensity)

-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
--
http://mail.python.org/mailman/listinfo/python-list


pyprocessing and exceptions

2009-04-15 Thread garyrob
Hi,

We're still using Python 2.5 so this question is about the
pyprocessing module rather than the multiprocessing module, but I'm
guessing the answer is the same.

I tend to use the Pool() object to create slave processes. If
something goes wrong in the slave, an exception is raised there, which
is then raised in the master or parent process, which is great.

The problem is that if the master aborts due to the exception, it
doesn't show the usual stack trace info for the slave, which would
show (among other things) the line number the error occurred on.
Instead, it shows the line in the master where work was sent to the
slave (such as a call to pool.map()).

I'm wondering what the recommended way is to write code that will
reveal what went wrong in the slave. One obvious possibility is to
have functions that are invoked in the slave incorporate their own
exception handling that prints a stack trace. But I'd rather handle
this issue in the master, rather than have to handle it in every
function in the slave module that the master may invoke.

Is there a way to do that? If not, what's the recommended approach?

Thanks,
Gary


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


Re: Modifying the value of a float-like object

2009-04-15 Thread Arnaud Delobelle
[email protected] writes:

> Arnaud, your code is very interesting!
>
> On Apr 15, 1:00 pm, Arnaud Delobelle  wrote:
>> I still don't understand why you need mutable floats.
>
> Here is why: the code that your proposed (or any code that does
> straightforward error propagation, for that matter) does not generally
> calculate uncertainties correctly:
>
 a=Value(1, 0.1)
 a-a
> 0.0 +- 0.14142135623823598
>
> The correct result is obviously 0.0 +- 0.0.  This is the effect of
> what I referred to in previous posts as "correlated errors".
>
> Anyway, I learned some interesting stuff about what you can do in
> Python, thanks to your code! :)

I still don't think mutable floats are necessary.  Here is an approach
below - I'll let the code speak because I have to do some shopping!

It still relies on Peter Otten's method for error calculation - which I
trust is good as my numerical analysis is to say the least very rusty!

-- uncert2.py --
def uexpr(x):
return x if isinstance(x, UBase) else UVal(x)

def uified(f):
def decorated(*args):
args = map(uexpr, args)
basis = set()
for arg in args:
basis |= arg.basis
uf = lambda values: f(*(x.f(values) for x in args))
ret = UExpr(basis, uf)
return ret if ret.err else ret.value
return decorated

class UBase(object):
def __repr__(self): 
return "%r +- %r" % (self.value, self.err) 
def __hash__(self):
return id(self)
__neg__ = uified(float.__neg__)
for op in 'add', 'sub', 'mul', 'div', 'pow':
for r in '', 'r':
exec """__%s__ = uified(float.__%s__)""" % (r+op, r+op)
del op, r

class UVal(UBase): 
def __init__(self, value, err=0):
if isinstance(value, UVal):
value, err = value.value, value.err
self.value = value 
self.err = err 
self.basis = set([self])
def f(self, values):
return values[self]


class UExpr(UBase):
def __init__(self, basis, f):
self.basis = basis
self.f = f
self.calc()
def derive(self, i, eps=1e-5):
values = dict((x, x.value) for x in self.basis)
values[i] += eps 
x2 = self.f(values) 
return (x2-self.value)/eps 
def calc(self): 
sigma = 0
values = dict((x, x.value) for x in self.basis)
self.value = self.f(values)
for i in self.basis:
x = self.derive(i)*i.err 
sigma += x*x 
self.err = sigma**0.5


builtinf = type(sum)
import math
for name in dir(math):
obj = getattr(math, name)
if isinstance(obj, builtinf):
setattr(math, name,  uified(obj))


Example:

marigold:junk arno$ python -i uncert2.py 
>>> a = UVal(2.0, 0.1)
>>> b = UVal(10.0, 1)
>>> a + a
4.0 +- 0.200131024
>>> a*b
20.0 +- 2.2360679774310253
>>> a - a
0.0
>>> a / a
1.0
>>> from math import *
>>> pow(b, a)
100.0 +- 30.499219977998791
>>> sin(a) - tan(b)
0.26093659936659497 +- 1.4209904731243463
>>> sin(a)*sin(a) + cos(a)*cos(a)
1.0
>>> sin(a)/cos(a) - tan(a)
0.0
>>> a*b - b*a
0.0
>>> # Etc...

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


RELEASED Python 2.6.2

2009-04-15 Thread Barry Warsaw
On behalf of the Python community, I'm happy to announce the  
availability of Python 2.6.2.  This is the latest production-ready  
version in the Python 2.6 series.  Dozens of issues have been fixed  
since Python 2.6.1 was released back in December.  Please see the NEWS  
file for all the gory details.


http://www.python.org/download/releases/2.6.2/NEWS.txt

For more information on Python 2.6 in general, please see

 http://docs.python.org/dev/whatsnew/2.6.html

Source tarballs, Windows installers, and (soon) Mac OS X disk images  
can be downloaded from the Python 2.6.2 page:


http://www.python.org/download/releases/2.6.2/

Please report bugs for any Python version in the Python tracker:

http://bugs.python.org

Enjoy,
-Barry

Barry Warsaw
[email protected]
Python 2.6/3.0 Release Manager
(on behalf of the entire python-dev team)



PGP.sig
Description: This is a digitally signed message part
--
http://mail.python.org/mailman/listinfo/python-list


Re: Lambda alternative?

2009-04-15 Thread mousemeat
On 15 Apr, 16:27, Piet van Oostrum  wrote:
> > [email protected] (m) wrote:
> >m> Hello all,
> >m> I really like the lambda function for a particular task i have: I am
> >m> writing some simulation software, working with many different
> >m> materials. Each material with many different properties, some are
> >m> temperature dependent, others aren't, some are defined by a function,
> >m> some are a lookup table, some are constant. Here is a simplified
> >m> version:
> >m> class material(object):
> >m>     def __init__(self,density):
> >m>         self.density=density
> >m> airdensity=lambda T:10/(287*T)
> >m> air=material(airdensity)
> >m> steeldensity=lambda T:interp(T,[0,1000],[7856,7813])
> >m> steel=material(steeldensity)
> >m> rockdensity=lambda T:5000
> >m> rock=material(rockdensity)
>
> The following isn't much different (only 6 chars more per def):
>
> def airdensity(T): return 10/(287*T)
> air=material(airdensity)
>
> def steeldensity(T): return interp(T,[0,1000],[7856,7813])
> steel=material(steeldensity)
>
> def rockdensity(T): return 5000
> rock=material(rockdensity)
>
> --
> Piet van Oostrum 
> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
> Private email: [email protected] Hide quoted text -
>
> - Show quoted text -

Will i then be able to pickle 'rock', and any object that may contain
'rock' as one of it's fields?  I'm not sure that i will, but i'll give
it a shot.
--
http://mail.python.org/mailman/listinfo/python-list


Re: iPython help, Docstring [source file open failed]

2009-04-15 Thread P. Kaminski
On 15 Kwi, 03:27, alex23  wrote:
> On Apr 15, 5:35 am, Przemyslaw Kaminski  wrote:
>
> > You may want to try:
> > import pydoc
> > b = pydoc.render_doc(timedelta)
> > print b
>
> Isn't this exactly the same output you get from typing 'help
> (timedelta)' though?

Well, from this:

Lets say I want more information about timedelta right at the shell
(as oppose lookup at Python Doc).

I understood that Kegan wanted python documentation, as in help, but
without the pager, right at the shell. Correct me if I'm wrong.
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-15 Thread Aahz
[much quote-trimming, the following is intended to just give the gist,
but the bits quoted below are not in directe response to each other]

On Wed, Apr 15, 2009, P.J. Eby wrote:
> At 09:51 AM 4/15/2009 +0200, M.-A. Lemburg wrote:
>> 
>>  [...]
>> Again: the PEP is about creating a standard for namespace
>> packages. It's not about making namespace packages easy to use for
>> Linux distribution maintainers. Instead, it's targeting *developers*
>> that want to enable shipping a single package in multiple, separate
>> pieces, giving the user the freedom to the select the ones she needs.
>>  [...]
>
>  [...]
> Anyway, since you clearly understand precisely what you're doing, I'm  
> now going to stop trying to explain things, as my responses are  
> apparently just encouraging you, and possibly convincing bystanders that 
> there's some genuine controversy here as well.

For the benefit of us bystanders, could you summarize your vote at this
point?  Given the PEP's intended goals, if you do not oppose the PEP, are
there any changes you think should be made?
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

Why is this newsgroup different from all other newsgroups?
--
http://mail.python.org/mailman/listinfo/python-list


Re: sharing/swapping items between lists

2009-04-15 Thread samwyse
On Apr 15, 8:13 am, Aaron Brady  wrote:
> On Apr 15, 6:57 am, samwyse  wrote:
>
> > Here's my idea:  generate all possible pairs:
>
> > >>> import itertools
> > >>> players = [chr(c) for c in xrange(ord('a'),ord('z')+1)]
> > >>> all_pairs = list(itertools.combinations(players,2))
>
> > partition the list:
> > >>> def choose_nonoverlapping(pairs):
> > chosen, leftover, used = list(), list(), list()
> > for p in pairs:
> > a, b = p
> > if a in used or b in used:
> > leftover.append(p)
> > else:
> > chosen.append(p)
> > used.append(a)
> > used.append(b)
> > return chosen, leftover
>
> > >>> court_count = 10
> > >>> week_count = 10
> > >>> pairs = all_pairs
> > >>> for week in xrange(week_count):
> > print 'week', week+1
> > this_week, pairs = choose_nonoverlapping(pairs)
> > print ', '.join(map(lambda t: ' vs '.join(t), this_week
> > [:court_count]))
> > pairs[0:0] = this_week[court_count:]
>
> snip
>
> Your idea arrives at a sub-optimal solution on players= 'abcdef',
> court_count= 3.
>
> Correct, by hand (5 weeks):
>
> ab cd ef
> ac be df
> ad ce bf
> ae bd cf
> af bc de
>
> Program (7 weeks):
>
[snip]
>
> However, you do correctly arrive at all the combinations, in better
> than the naive 'one pair per week' solution.  Further, you produced
> the correct solution for players= 'abcdef', for court_count= 1 and
> court_count= 2, which I also tested.  Damage report?

It does work better when there are a limited number of courts, but
since that was in the original description, I didn't worry too much.

One could product several random shuffles of the list of combinations
and see which produced the shortest list of results.  That would add
indeterminancy without guaranteeing an optimal result.  But I think
that other people have algorithms for that case, so I'm not too
worried.

I've tried generalizing to competitions  with more than two player
(e.g. the Pinewood Derby, where up four cars are in each heat), but
the algorithm falls apart, mostly due to the way
itertools.combinations returns its results.
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-15 Thread M.-A. Lemburg
On 2009-04-15 16:44, P.J. Eby wrote:
> At 09:51 AM 4/15/2009 +0200, M.-A. Lemburg wrote:
>> On 2009-04-15 02:32, P.J. Eby wrote:
>> > At 10:59 PM 4/14/2009 +0200, M.-A. Lemburg wrote:
>> >> You are missing the point: When breaking up a large package that
>> lives in
>> >> site-packages into smaller distribution bundles, you don't need
>> namespace
>> >> packages at all, so the PEP doesn't apply.
>> >>
>> >> The way this works is by having a base distribution bundle that
>> includes
>> >> the needed __init__.py file and a set of extension bundles the add
>> >> other files to the same directory (without including another copy of
>> >> __init__.py). The extension bundles include a dependency on the base
>> >> package to make sure that it always gets installed first.
>> >
>> > If we're going to keep that practice, there's no point to having the
>> > PEP: all three methods (base+extensions, pkgutil, setuptools) all work
>> > just fine as they are, with no changes to importing or the stdlib.
>>
>> Again: the PEP is about creating a standard for namespace
>> packages. It's not about making namespace packages easy to use for
>> Linux distribution maintainers. Instead, it's targeting *developers*
>> that want to enable shipping a single package in multiple, separate
>> pieces, giving the user the freedom to the select the ones she needs.
>>
>> Of course, this is possible today using various other techniques. The
>> point is that there is no standard for namespace packages and that's
>> what the PEP is trying to solve.
>>
>> > In particular, without the feature of being able to drop that practice,
>> > there would be no reason for setuptools to adopt the PEP.  That's why
>> > I'm -1 on your proposal: it's actually inferior to the methods we
>> > already have today.
>>
>> It's simpler and more in line with the Python Zen, not inferior.
>>
>> You are free not to support it in setuptools - the methods
>> implemented in setuptools will continue to work as they are,
>> but continue to require support code and, over time, no longer
>> be compatible with other tools building upon the standard
>> defined in the PEP.
>>
>> In the end, it's the user that decides: whether to go with a
>> standard or not.
> 
> Up until this point, I've been trying to help you understand the use
> cases, but it's clear now that you already understand them, you just
> don't care.
> 
> That wouldn't be a problem if you just stayed on the sidelines, instead
> of actively working to make those use cases more difficult for everyone
> else than they already are.
> 
> Anyway, since you clearly understand precisely what you're doing, I'm
> now going to stop trying to explain things, as my responses are
> apparently just encouraging you, and possibly convincing bystanders that
> there's some genuine controversy here as well.

Hopefully, bystanders will understand that the one single use case
you are always emphasizing, namely that of Linux distribution maintainers
trying to change the package installation layout, is really a rather
uncommon and rare use case.

It is true that I do understand what the namespace package idea is
all about. I've been active in Python package development since they
were first added to Python as a new built-in import feature in Python 1.5
and have been distributing packages with package add-ons for more than
a decade...

For some history, have a look at:

http://www.python.org/doc/essays/packages.html

Also note how that essay discourages the use of .pth files:

"""
If the package really requires adding one or more directories on sys.path (e.g.
because it has not yet been structured to support dotted-name import), a "path
configuration file" named package.pth can be placed in either the site-python or
site-packages directory.
...
A typical installation should have no or very few .pth files or something is
wrong, and if you need to play with the search order, something is very wrong.
"""

Back to the PEP:

The much more common use case is that of wanting to have a base package
installation which optional add-ons that live in the same logical
package namespace.

The PEP provides a way to solve this use case by giving both developers
and users a standard at hand which they can follow without having to
rely on some non-standard helpers and across Python implementations.

My proposal tries to solve this without adding yet another .pth
file like mechanism - hopefully in the spirit of the original Python
package idea.

-- 
Marc-Andre Lemburg
eGenix.com

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

2009-03-19: Released mxODBC.Connect 1.0.1  http://python.egenix.com/

::: Try our new mxODBC.Connect Python Data

Python Bug Day on April 23

2009-04-15 Thread Georg Brandl
Hi,

I'd like to announce that there will be a Python Bug Day on April 23.
As always, this is a perfect opportunity to get involved in Python
development, or bring your own issues to attention, discuss them and
(hopefully) resolve them together with the core developers.

We will coordinate over IRC, in #python-dev on irc.freenode.net,
and the Wiki page http://wiki.python.org/moin/PythonBugDay has all
important information and a short list of steps how to get set up.

Please spread the word!

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


Re: sharing/swapping items between lists

2009-04-15 Thread samwyse
On Apr 15, 8:56 am, Aaron Brady  wrote:
>
> The randomizing solution isn't quite suitable for 16 teams.  With 5
> teams/1 court, and 5 teams/2 courts, 6 teams/2 courts, the solution
> comes within seconds.  For 7 teams/3 courts, the solution takes a few
> minutes.

7 teams/3 courts is the same as 8 teams/4 courts, where the extra
player is named "bye".  In other words, it's an uncontrained problem.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python and XML Help

2009-04-15 Thread Scott David Daniels

ookrin wrote:

 I am still learning. And it's not that I won't take the advice
for using ElementTree, I just currently don't know anything about it.
I just didn't want to say, "I have no idea what you're talking about!"
to Scott cause I figured that would be rude, but I guess so is not
saying anything, sorry. So I'll need to read up on it before I
actually try to do anything with that approach.

Seeing the errors - I changed the two classes to this:
  ... [some code that actually says a _little_ bit about his target] 


If you had done a better job of asking your question, you'd have found a
quick answer.  That is why everyone points people at smartquestions, not
to pick on them.

Presuming you have an xml structure like:
txt = ''' something 
Some contents. 
   Other contents.
   '''
import xml.etree.ElementTree as ET
structure = ET.fromstring(xmls)
for node in structure.findall('def/name'):
print '%s: %s / %s: %s' % (node.tag, node.attrib['name'],
  node.attrib['characterID'], node.text)

or a file named 'my.xml' with the same contents:
import xml.etree.ElementTree as ET
structure = ET.parse('my.xml')
for node in structure.findall('def/name'):
print '%s: %s / %s: %s' % (node.tag, node.attrib['name'],
  node.attrib['characterID'], node.text)


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


Re: Python Bug Day on April 23

2009-04-15 Thread MRAB

Georg Brandl wrote:

Hi,

I'd like to announce that there will be a Python Bug Day on April 23.
As always, this is a perfect opportunity to get involved in Python
development, or bring your own issues to attention, discuss them and
(hopefully) resolve them together with the core developers.

We will coordinate over IRC, in #python-dev on irc.freenode.net,
and the Wiki page http://wiki.python.org/moin/PythonBugDay has all
important information and a short list of steps how to get set up.

Please spread the word!

Georg


First bug! The webpage says Saturday, April 25, 2009.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Lambda alternative?

2009-04-15 Thread Duncan Booth
Paul Rubin  wrote:

> Duncan Booth  writes:
>> What exactly is your objection to using a named function here?...
>> Surely that would solve your pickle problem *and* give you more
>> meaningful tracebacks in your exceptions?
> 
> I don't think it would help the pickle problem.

Why wouldn't you think that?

class material(object):
def __init__(self,density):
self.density=density

def airdensity(T):
return 10/(287*T)
air=material(airdensity)

steeldensity=lambda T:interp(T,[0,1000],[7856,7813])
steel=material(steeldensity)

from pickle import dumps, loads

dumped = dumps(air)
t = loads(dumped)
print t # works fine

dumped = dumps(steel) # pickle.PicklingError: Can't pickle  at 0x02C78A58>: it's not found as __main__.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Lambda alternative?

2009-04-15 Thread Duncan Booth
mousemeat  wrote:

> Will i then be able to pickle 'rock', and any object that may contain
> 'rock' as one of it's fields?  I'm not sure that i will, but i'll give
> it a shot.
> 
So long as the functions are defined at module level, not nested inside 
some other namespace it will work fine.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Question to python C API

2009-04-15 Thread Stefan Behnel
Andreas Otto wrote:
>   I have the following question ...
> 
>   I write a custom "*.init" method and expect a variable number or arguments

What's a "*.init" method? Do you mean SomeType.__init__() ?


>   This are my questions:
> 
>   1.I need something like a for loop to analyse this arguments
> For now I try the "PyArg_ParseTupleAndKeywords" but i missing 
> somehing 
> like an Object-Array return value as "format" 
> 
>   2.can i combine variable args with keywords ?
> because it will fit into my spec to use both together
> 
>   3.I want to retrieve a bool object as int value, where is a format "O"
> and "O!" for a type object  HOWTO put the type into the function 
> from
> above

You might want to take a look at Cython. It allows you to write C
extensions in a Python-like language, so all of the above become trivial, e.g.

cdef class MyExtensionType(object):
def __init__(self, bint some_c_bool):
 print type(self) is MyExtensionType
 print some_c_bool == 1

Cython will translate this to efficient C code for you.

http://cython.org/

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


Re: binary file compare...

2009-04-15 Thread Nigel Rantor

Martin wrote:

On Wed, Apr 15, 2009 at 11:03 AM, Steven D'Aprano
 wrote:

The checksum does look at every byte in each file. Checksumming isn't a
way to avoid looking at each byte of the two files, it is a way of
mapping all the bytes to a single number.


My understanding of the original question was a way to determine
wether 2 files are equal or not. Creating a checksum of 1-n files and
comparing those checksums IMHO is a valid way to do that. I know it's
a (one way) mapping between a (possibly) longer byte sequence and
another one, how does checksumming not take each byte in the original
sequence into account.


The fact that two md5 hashes are equal does not mean that the sources 
they were generated from are equal. To do that you must still perform a 
byte-by-byte comparison which is much less work for the processor than 
generating an md5 or sha hash.


If you insist on using a hashing algorithm to determine the equivalence 
of two files you will eventually realise that it is a flawed plan 
because you will eventually find two files with different contents that 
nonetheless hash to the same value.


The more files you test with the quicker you will find out this basic truth.

This is not complex, it's a simple fact about how hashing algorithms work.

  n

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


Re: binary file compare...

2009-04-15 Thread Nigel Rantor

Grant Edwards wrote:

We all rail against premature optimization, but using a
checksum instead of a direct comparison is premature
unoptimization.  ;)


And more than that, will provide false positives for some inputs.

So, basically it's a worse-than-useless approach for determining if two 
files are the same.


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


Re: Modifying the value of a float-like object

2009-04-15 Thread Suraj Barkale
  spectro.jussieu.fr> writes:
> 
> Hello,
> 
> Is there a way to easily build an object that behaves exactly like a
> float, but whose value can be changed?  The goal is to maintain a list
> [x, y,…] of these float-like objects, and to modify their value on the
> fly (with something like x.value = 3.14) so that any expression like "x
> +y" uses the new value.
> 

Have you looked at sympy (http://code.google.com/p/sympy/)? It implements
symbolic mathematics so you can define the equations & put in the values to get
the result. You should be able to derive from the Symbol class to implement the
accuracy behavior.

Regards,
Suraj

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


Re: Play sound at wanted frequency

2009-04-15 Thread Scott David Daniels

Diez B. Roggisch wrote:

Matteo schrieb:

I need to playback a sound on a linux machine of a pre-determined
frequency like, say, 440 Hz. How can I do that with python? I found
the ossaudiodev package, but it says that the ossaudiodev.write()
method accepts data as a raw string. It doesn't explain what the
string should be like, and the oss documentation is mainly about C++.

Do you have any tips to share?


Try pygame. You can create samples with it, and play them.

See e.g. this: http://www.pygame.org/docs/ref/sndarray.html


And realize that if you can get that to work, you can always cut into
pygame to see how _it_ accomplishes your task, and mimic that.

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


Re: Using Python after a few years of Ruby

2009-04-15 Thread Aaron Watters
On Apr 15, 3:49 am, Tim Hoffman  wrote:

> There are plenty of python web frameworks, some have quite different
> approaches,
> what suits you will depend very much on your own bias, interest.

I've had a lot of luck with WHIFF
( http://whiff.sourceforge.net )
Of course I wrote it and just released it publicly
recently.

I'm a bit disappointed because I know a lot of people have looked
at the docs and demos and there have been a number of downloads,
but I haven't gotten one comment yet :( zilch nada.  (There is
one comment attached to one of the docs, but that was me testing
the comments feature :) ).

Back in the old days you could at least count on someone yelling
at you about how your design was such a bad idea

  -- Aaron Watters

===
HELP! HELP! I'M BEING REPRESSED!
--
http://mail.python.org/mailman/listinfo/python-list


Re: binary file compare...

2009-04-15 Thread SpreadTooThin
On Apr 15, 8:04 am, Grant Edwards  wrote:
> On 2009-04-15, Martin  wrote:
>
>
>
> > Hi,
>
> > On Mon, Apr 13, 2009 at 10:03 PM, Grant Edwards  wrote:
> >> On 2009-04-13, SpreadTooThin  wrote:
>
> >>> I want to compare two binary files and see if they are the same.
> >>> I see the filecmp.cmp function but I don't get a warm fuzzy feeling
> >>> that it is doing a byte by byte comparison of two files to see if they
> >>> are they same.
>
> >> Perhaps I'm being dim, but how else are you going to decide if
> >> two files are the same unless you compare the bytes in the
> >> files?
>
> > I'd say checksums, just about every download relies on checksums to
> > verify you do have indeed the same file.
>
> That's slower than a byte-by-byte compare.
>
> >> You could hash them and compare the hashes, but that's a lot
> >> more work than just comparing the two byte streams.
>
> > hashing is not exactly much mork in it's simplest form it's 2
> > lines per file.
>
> I meant a lot more CPU time/cycles.
>
> --
> Grant Edwards                   grante             Yow! Was my SOY LOAF left
>                                   at               out in th'RAIN?  It tastes
>                                visi.com            REAL GOOD!!

I'd like to add my 2 cents here.. (Thats 1.8 cents US)
All I was trying to get was a clarification of the documentation of
the cmp method.
It isn't clear.

byte by byte comparison is good enough for me as long as there are no
cache issues.
a check sum is not good because it doesn't guarantee that  1 + 2 + 3
== 3 + 2 + 1
a crc of any sort is more work than a byte by byte comparison and
doesn't give you any more information.


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


Re: How to query object of GUI?

2009-04-15 Thread Scott David Daniels

John McMonagle wrote:

Muddy Coder wrote:

I need to query the ID of GUI, in Tkinter, but don't know how to do
it. This is my code: ...


Pass the name of the label as an argument to the callback.  Below is a
short working example:
...
lab = 'test'
menubar.add_command(label=lab, command=lambda l=lab: do_menu(l))
lab = 'quit'
menubar.add_command(label=lab, command=lambda l=lab: do_menu(l))

> ...

Another way to do the same thing (a way to "avoid" lambda):
from functools import partial
...
class MyGUI(object):
...
def make_menu(self):
top = Menu(self)
menObj = Menu(top)
for n, lab in enumerate(read_from_database()):
menObj.add_command(label=lab,
command=partial(self.do_menu, n, lab))

def do_menu(self, sequence, label):
# here you know it was the (sequence+1)th entry named label
...

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


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-15 Thread P.J. Eby

At 09:10 AM 4/15/2009 -0700, Aahz wrote:

For the benefit of us bystanders, could you summarize your vote at this
point?  Given the PEP's intended goals, if you do not oppose the PEP, are
there any changes you think should be made?


I'm +1 on Martin's original version of the PEP, subject to the point 
brought up by someone that .pkg should be changed to a different extension.


I'm -1 on all of MAL's proposed revisions, as IMO they are a step 
backwards: they "standardize" an approach that will create problems 
that don't need to exist, and don't exist now.  Martin's proposal is 
an improvement on the status quo, Marc's proposal is a dis-improvement.


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


Re: [Python-Dev] PEP 382: Namespace Packages

2009-04-15 Thread P.J. Eby

At 06:15 PM 4/15/2009 +0200, M.-A. Lemburg wrote:

The much more common use case is that of wanting to have a base package
installation which optional add-ons that live in the same logical
package namespace.


Please see the large number of Zope and PEAK distributions on PyPI as 
minimal examples that disprove this being the common use case.  I 
expect you will find a fair number of others, as well.


In these cases, there is NO "base package"...  the entire point of 
using namespace packages for these distributions is that a "base 
package" is neither necessary nor desirable.


In other words, the "base package" scenario is the exception these 
days, not the rule.  I actually know specifically of only one other 
such package besides your mx.* case, the logilab ll.* package.


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


Re: segmentation fault while using ctypes

2009-04-15 Thread sanket
On Apr 14, 9:00 pm, [email protected] wrote:
> On Apr 15, 12:39 am, sanket  wrote:
>
>
>
> > Hello All,
>
> > I am dealing with this weird bug.
> > I have a function in C and I have written python bindings for it using
> > ctypes.
>
> > I can call this function for couple of times and then suddenly it
> > gives me seg fault.
> > But I can call same function from a C code for any number of times.
>
> > I cannot get what's going on.
>
> > here is my code.
>
> > /**/
> > /* C Function I am calling */
> > int get_hash(char *filename,int rate,int ch,unsigned char* hash,
> > unsigned int* hash_size,short* avg_f,short* avg_d){
>
> > /* some variable declarations here */
> > fp = fopen(filename,"rb");
>
> > data = (signed short *)malloc(sizeof(signed short) * N_BLOCKS);
>
> > whereami = WAVE_HEADER_SIZE;
> > while((!feof(fp)) && (fp_more == 1) && !ferror(fp)){
> >      fp_data_size = fread(data,sizeof(signed short),N_BLOCKS,fp);
> >      whereami += fp_data_size;
> >      fp_more = fp_feed_short(fooid,data,fp_data_size); // call to some
> > library funtion
> >  } //end while
>
> > /* some arithmetic calculations here */
>
> >   n = my_fp_calculate(fooid,audio_length,fp_fingerprint,&fit,&dom);
>
> >   if (data != NULL)
> >       free(data)
> >   fclose(fp)
> >   return n;
>
> > }
>
> > /* END OF C FUNCTION
> > */
> > 
> > Python code
> > -
> > from ctypes import *
> > lib = cdll.LoadLibrary("/usr/lib/libclient.so")
>
> > def my_func(filename,rate,ch):
> >     hash = (c_ubyte * 424)()
> >     hash_size = c_uint()
> >     avg_f = c_short(0)
> >     avg_d = c_short(0)
> >     n = lib.get_hash(filename,rate,ch,hash,byref(hash_size),byref
> > (avg_f),byref(avg_d))
> >     hash = None
>
> > def main():
> >     for filename in os.listdir(MY_DIR):
> >             print filename
> >             my_func(filename,100,10)
> >             print
> > ""
>
> > if __name__ == "__main__":
> >     main()
>
> > == END OF PYTHON CODE ==
>
> > Thank you in advance,
> > sanket
>
> You don't show how the C function make use of parameters hash,
> hash_size, avg_f, avg_d. Since you pass them by address,
> I guess that are output parameters, but how do they get written? In
> particular,make sure you don't write into hash more
> than the  424 bytes you allocated for it in the calling python
> code ...
>
> Ciao
> 
> FB

Yes, I am making sure that hash wouldn't be written more that 424
bytes in to it.
This is something really weird. I can call this function from C as
many times as I want but not from python

I tracked down the problem and came to know that call to
my_fp_calculate causes the seg fault.
I will debug it and let you guys know.

Thank you all so much for the help. I appreciate it.
sanket
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   >