Re: Impossible to change methods with special names of instances of new-style classes?

2008-07-10 Thread Steven D'Aprano
On Tue, 08 Jul 2008 17:56:45 -0400, Joseph Barillari wrote:

> Hi python-list,
> 
> I've just started using new-style classes and am a bit confused as to
> why I can't seem to alter methods with special names (__call__, etc.) of
> new-style class instances.

[deploy weapon of mass snippage]

Here is a possible work-around:

>>> class Special(object):
... def __call__(self):
... try:
... return self.__dict__['__call__']()
... except KeyError:
... return 'foo'
...
>>> s = Special()
>>> s()
'foo'
>>> s.__call__ = lambda: 'bar'
>>> s()
'bar'



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


ANN: Mac OS X versions of Rabbyt 0.81 - A fast 2D sprite engine using OpenGL

2008-07-10 Thread Python Nutter
Just a quick announcement,

I have supplied PPC and Intel compiled eggs for the Rabbyt libary on Pypi

http://pypi.python.org/pypi/Rabbyt/

Rabbyt-0.8.1-py2.5-macosx-10.3-ppc.egg (md5)
Built on OS X 10.5.4 for PowerPC equipped Macs

Rabbyt-0.8.1-py2.5-macosx-10.3-i386.egg (md5)
Built on OS X 10.5.4 for Intel equipped Macs


If you know anything about Rabbyt or want to run any of the Rabbyt +
pyglet tutorials or demonstration programs, you can now easily install
it onto your Mac.

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


Re: a simple 'for' question

2008-07-10 Thread Tim Roberts
Ethan Furman <[EMAIL PROTECTED]> wrote:

>Ben Keshet wrote:
>> it didn't help.  it reads the pathway "as is" (see errors for both 
>> tries).  It looks like it had the write pathway the first time, but 
>> could not find it because it searched in the path/way instead of in the 
>> path\way.  thanks for trying.
>
>The form of slash ('\' vs '/') is irrelevant to Python.  At least on 
>Windows.
>
>> folders= ['1','2','3']
>> for x in folders:
>> print x # print the current folder
>> filename='Folder/%s/myfile.txt' %[x]
>^- brackets not needed

More than that, the brackets are CAUSING this problem.  The "%" formatting
operator expects to find either a single item, or a tuple containing
multiple items.  It does NOT look for a generic iterator.  In this case,
the %s will use the whole list as its parameter.  Python converts the list
to string, and the string representation of that one-item list is ['1'].

>> f=open(filename,'r')
>> 
>> gives: IOError: [Errno 2] No such file or directory: 
>> "Folder/['1']/myfile.txt"

Just like that.

>As far as the Python question of string substitution, "%s" % var is an 
>appropriate way.

Right.
-- 
Tim Roberts, [EMAIL PROTECTED]
Providenza & Boekelheide, Inc.
--
http://mail.python.org/mailman/listinfo/python-list


Re: start reading from certain line

2008-07-10 Thread Steven D'Aprano
On Wed, 09 Jul 2008 09:59:32 -0700, norseman wrote:

> I would use:
> 
> readthem= 0
> file=open(filename,'r')
> while readthem == 0:
>line=file.readline()
>if not line:
>  break
>if 'Item 1' in line:
>  readthem= 1
>  # print line  # uncomment if 'Item 1' is to be printed
> while line:
>line= file.readline()
>print line  # see note-1 below
> #  end of segment


Ouch! That's a convoluted way of doing something which is actually very 
simple. This is all you need:

outfile = open('filename', 'r')
for line in outfile:
if 'item 1' in line.lower():
print line
break
for line in outfile:
print line


If you don't like having two loops:

outfile = open('filename', 'r')
skip = True
for line in outfile:
if 'item 1' in line.lower():
skip = False
if not skip:
print line


And if you want an even shorter version:

import itertools
outfile = open('filename', 'r')
for line in itertools.dropwhile(
lambda l: 'item 1' not in l.lower(), outfile):
print line



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


Re: Impossible to change methods with special names of instances of new-style classes?

2008-07-10 Thread Bruno Desthuilliers

samwyse a écrit :

On Jul 8, 4:56 pm, Joseph Barillari <[EMAIL PROTECTED]> wrote:


My question is: did something about the way the special method names are
implemented change for new-style classes?


Just off the top of my head, I'd guess that it's due to classes
already having a default __call__ method,


>>> object.__dict__.keys()
['__setattr__', '__reduce_ex__', '__new__', '__reduce__', '__str__', 
'__getattribute__', '__class__', '__delattr__', '__repr__', '__hash__', 
'__doc__', '__init__']


No __call__ method here.


used when you instatiate.


The __call__ method used to instanciate a class is actually the 
metaclass __call__ method.


>>> class MyType(type):
... def __call__(self, *args, **kw):
... print "pikaboo"
... return type.__call__(self, *args, **kw)
...
>>> class MyClass(object):
... __metaclass__ = MyType
...
>>> MyClass()
pikaboo
<__main__.MyClass object at 0x8334eec>
>>>


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


Fwd: [ANN] Genshi 0.5.1 released

2008-07-10 Thread Jeroen Ruigrok van der Werven
We have put up eggs for various operating systems as well.

- Forwarded message from Christopher Lenz <[EMAIL PROTECTED]> -

From: Christopher Lenz <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
Subject: [ANN] Genshi 0.5.1 released
Date: Wed, 9 Jul 2008 21:44:11 +0200
X-Mailer: Apple Mail (2.926)


Genshi 0.5.1 - Jul 9, 2008
==

We're proud to present the latest release of the Genshi: 0.5.1.

Genshi is a Python library that provides an integrated set of
components for parsing, generating, and processing HTML, XML or
other textual content for output generation on the web. The major
feature is a template language.

This release contains a number of important bug fixes over the
0.5 release.


You can download the new release here:

  

Please don't hesitate to report any issues you may find with this
release:

  

For questions, comments and user discussions, please use the Genshi
mailing list:

  


What's New:
---
* Fix problem with nested match templates not being applied when
buffering on the outer `py:match` is disabled. Thanks to Erik Bray
for reporting the problem and providing a test case!
* Fix problem in `Translator` filter that would cause the translation
of text nodes to fail if the translation function returned an object
that was not directly a string, but rather something like an instance
of the `LazyProxy` class in Babel (ticket #145).
* Fix problem with match templates incorrectly being applied multiple
times.
* Includes from templates loaded via an absolute path now include the
correct file in nested directories as long if no search path has been
configured (ticket #240).
* Unbuffered match templates could result in parts of the matched
content being included in the output if the match template didn't
actually consume it via one or more calls to the `select()` function
(ticket #243).


Acknowledgments

A big thank you to everyone who tried Genshi and provided feedback,
reported bugs, and/or contributed patches!


Cheers,
--
Christopher Lenz
   cmlenz at gmx.de
   http://www.cmlenz.net/

- End forwarded message -

-- 
Jeroen Ruigrok van der Werven  / asmodai
イェルーン ラウフロック ヴァン デル ウェルヴェン
http://www.in-nomine.org/ | http://www.rangaku.org/ | GPG: 2EAC625B
Time is a twofold teacher, harsh and yet patient like no-one...
--
http://mail.python.org/mailman/listinfo/python-list

Re: Allow tab completion when inputing filepath?

2008-07-10 Thread Tim Golden

Keith Hughitt wrote:

On Jul 9, 10:18 am, Tim Golden <[EMAIL PROTECTED]> wrote:

Keith Hughitt wrote:

I've been looking around on the web for a way to do this, but so far
have not come across anything for this particular application. I have
found some ways to enable tab completion for program-related commands,
but not for system filepaths. This would be nice to have when
prompting the user to enter a file/directory location.

What platform are you on? And what kind of display?
(Console / GUI / wxPython / Qt / Web...)

TJG


Hi TJG,

Currently Unix/Console. Although I don't have any plans at the moment
to add a GUI, it would be great if a cross-platform solution existed.


Ah. Not my area then, I'm afraid :( I mostly do Windows stuff.
However, I don't remember ever coming across a standard way
of doing this, although it shouldn't be too hard to roll your own.

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


Re: Terminate a python script from linux shell / bash script

2008-07-10 Thread Piet van Oostrum
> Gros Bedo <[EMAIL PROTECTED]> (GB) wrote:

>GB> I have a question about Python and Linux shell. I have a python
>GB> program which is permanently resident in the end-user system. I'm
>GB> currently producing a RPM package, and it works nicely. The problem is
>GB> that when I uninstall it, my program keeps running in the background,
>GB> even if the files are deleted.

>GB> I know I can terminate python shell directly, but this is not a good
>GB> idea because the end-user may be working with another important python
>GB> application, and would be very angry if mine would close everything
>GB> that is using python shell.

That's not how it works. If you kill one running python script it will not
effect other python scripts. Each script has its own interpreter process
running. 

>GB> So, is there a way from the Linux shell or a bash script to terminate
>GB> just one specific Python script ? 

So just kill it.
-- 
Piet van Oostrum <[EMAIL PROTECTED]>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list


Re: error when porting C code to Python (bitwise manipulation)

2008-07-10 Thread Jordan
Well, I have figured out something that works:

def findit(u):
u += 0xe91aaa35
u1 = ~(0x - u) ^ u >> 16
u1 += ((u1 << 8) & 0x)
u1 ^= (u1 & 0x) >> 4
b  = (u1 >> 8) & 0x1ff
a  = (u1 + (u1 << 2)  & 0x) >> 19
r  = int(a) ^ hash_adjust[int(b)]
return r


I feel like this cannot possibly be the best way of doing this, but it
does work haha

If anyone would care to share a more elegant solution, that would be
great :)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Freesoftware for auto/intelligent code completing in Python

2008-07-10 Thread Ali Servet Dönmez
On Jul 10, 1:07 am, Gros Bedo <[EMAIL PROTECTED]> wrote:
> Hello,
> Ali I totally support you, neither I couldn't find any really working code 
> completion for python in a free software, and it's really a mess, at least on 
> Linux.
>
> On Windows, there is PyScripter (http://pyscripter.googlepages.com/), but it 
> is based on Delphi, and as such it's not portable. But it's a free software 
> (even if I couldn't find the sources, they say you can download them).
>
> It is said to be working on Linux via Wine, so if you really need a code 
> completion tool you could give it a try.
> _
> Téléchargez gratuitement des émoticônes pour pimenter vos conversations 
> Messengerhttp://specials.fr.msn.com/femmes/amour/emoticonesLove.aspx

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


Re: FOSS projects exhibiting clean/good OOP?

2008-07-10 Thread Phillip B Oldham
On Jul 9, 9:26 pm, "[EMAIL PROTECTED]"
<[EMAIL PROTECTED]> wrote:
> This is somewhat subjective... Some would say that Python's object
> model is fundamentally broken and crappy (not MHO, needless to say)
> that Python +  "solid OO principles" is antinomic !-)

Really? Would you happen to be able to provide any further information
on that?
--
http://mail.python.org/mailman/listinfo/python-list


Re: FOSS projects exhibiting clean/good OOP?

2008-07-10 Thread Phillip B Oldham
Thanks all - lots to go through there! :D

I'd heard previously that Trac was a nice example, or rather its core
was, but I'd also heard that there were lots of problems with it and
that they were redeveloping it from scratch?
--
http://mail.python.org/mailman/listinfo/python-list


Re: python scalability

2008-07-10 Thread Mike Hansen
> I have looked at the python
> success stories page and haven't come up with anyone quite like us.
> One of my project managers questions is: "Are we the only company in the
> world with this kind and size of project?"
> I want to say no, but am having trouble convincing myself, let alone him.
>
> If you are involved in this kind of thing please get in touch with me.

While Sage ( http://www.sagemath.org ) is more of a library than an
application, it is approximately 350k lines of Python and Cython (in
roughly a 2 to 1 ratio).  I think it has scaled surprisingly well.

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


Re: re.search much slower then grep on some regular expressions

2008-07-10 Thread Kris Kennaway

John Machin wrote:


Uh-huh ... try this, then:

http://hkn.eecs.berkeley.edu/~dyoo/python/ahocorasick/

You could use this to find the "Str" cases and the prefixes of the
"re" cases (which seem to be no more complicated than 'foo.*bar.*zot')
and use something slower like Python's re to search the remainder of
the line for 'bar.*zot'.


If it was just strings, then sure...with regexps it might be possible to 
make it work, but it doesn't sound particularly maintainable.  I will 
stick with my shell script until python gets a regexp engine of 
equivalent performance.


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


Re: FOSS projects exhibiting clean/good OOP?

2008-07-10 Thread Bruno Desthuilliers

Phillip B Oldham a écrit :

Thanks all - lots to go through there! :D

I'd heard previously that Trac was a nice example, or rather its core
was, but I'd also heard that there were lots of problems with it and
that they were redeveloping it from scratch?


Trac's plugin system is interesting, yes, but there were indeed quite a 
couple gotchas last time I used it (about 2 years ago).


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


Re: Retrieving BSTR * from a DLL

2008-07-10 Thread Andrew MacIntyre

mzdude wrote:

I need to interface with a windows DLL that has the following
signature

extern "C" void Foo( BSTR in, BSTR *out )

Code so far


from ctypes import *
import comtypes
LPBSTR = POINTER(comtypes.BSTR)

hdl = windll.MyDll.Foo
hdl.rettype = None
hdl.argtypes = [comtypes.BSTR, LPBSTR]

inStr = comtypes.BSTR(u'Some Silly String')
out = comtypes.BSTR


 out = comtypes.BSTR()


hdl(inStr,byref(out))


Traceback (most recent call last):
  File "", line 1, in 
hdl(inStr,byref(out))
TypeError: byref() argument must be a ctypes instance, not
'_ctypes.SimpleType'


comtypes.BSTR is a type; the type error makes clear you need
an instance, as above.


Also tried the following


out = comtypes.BSTR(u'')
p = pointer(out)
hdl(inStr,p)


Traceback (most recent call last):
  File "", line 1, in 
hdl(inStr,p)
ValueError: Procedure probably called with too many arguments (8 bytes
in excess)


This likely indicates that the DLL is using the C calling convention
and not the stdcall calling convention.  Use CDLL rather than WinDLL
to load the DLL.

You might like to join the ctypes-users mailing list at sourceforge.

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


Re: TypeError, I know why but not how!?

2008-07-10 Thread ssecorp
ty I came to the same conckusion in bed :)

now it works.


however since there are 400 students and some are incompatible I
shouldnt be able to generate a 200room list right?

but it works sometimes the other times i get an error. might be
because of recursion depth i never let the error finish.


i saw i also generate student 0 which shouldnt exist.




and the actual problem is generating all possible combinations right?
i mean i can easily generate one list that is made randomly so problem
solved.

why does one need to generate all possible lists? i guess there are
other problems where that might be needed so is this just a bad
example or am i missing something?


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


Re: TypeError, I know why but not how!?

2008-07-10 Thread John Machin
On Jul 10, 12:07 pm, ssecorp <[EMAIL PROTECTED]> wrote:

> pair1 = (student1,student2)
> pair2 = (student2,student1)
> if (pair1 or pair2) in incompatibles:

Apart from the problems that others have mentioned, the above
statement is NOT doing what you think it is. (pair1 or pair2) will
always produce pair1, because pair1, a tuple of length 2, cannot be
false. So the statement is equivalent to:
if pair1 in incompatibles:
What you want is:
if pair1 in incompatibles or pair2 in incompatibles:

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


Re: FOSS projects exhibiting clean/good OOP?

2008-07-10 Thread Bruno Desthuilliers

Phillip B Oldham a écrit :

On Jul 9, 9:26 pm, "[EMAIL PROTECTED]"
<[EMAIL PROTECTED]> wrote:

This is somewhat subjective... Some would say that Python's object
model is fundamentally broken and crappy (not MHO, needless to say)
that Python +  "solid OO principles" is antinomic !-)


Really? Would you happen to be able to provide any further information
on that?


Well... Some people seems to think that an object model with no 
language-enforced access restriction, no implicit 'self' reference and 
the necessity to explicitely declare 'self' as first argument of methods 
 is broken. You'll also find a couple guys complaining about Python not 
forcing you to put everything into classes, à la Java.


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


Re: FOSS projects exhibiting clean/good OOP?

2008-07-10 Thread Michele Simionato
On Jul 9, 4:38 pm, Phillip B Oldham <[EMAIL PROTECTED]> wrote:
> I'm wondering whether anyone can offer suggestions on FOSS projects/
> apps which exhibit solid OO principles, clean code, good inline
> documentation, and sound design principles?
>
> I'm devoting some time to reviewing other people's code to advance my
> skills. Its good to review bad code (of which I have more than enough
> examples) as well as good, but I'm lacking in finding good examples.
>
> Projects of varying sizes would be great.

I vote for the doctest module in the standard library.
--
http://mail.python.org/mailman/listinfo/python-list


Graphics

2008-07-10 Thread vanam
hi all
i am new to python programming a beginner. I Came to know from the
groups that "How to think like a computer scientist" is preferable for
begineers. i just looking through that i came to one section where a
sample program for generation of graphics is present.i tried to copy
the same script to the interpreter and it is showing an error i want
to know whether is there anything that has to be installed in addition
to python 2.5
below is the program
from gasp import *
begin_graphics()
Circle((200, 200), 60)
Line((100, 400), (580, 200))
Box((400, 350), 120, 100)
end_graphics()
--
http://mail.python.org/mailman/listinfo/python-list


B-Soup: broken iterator, tag a keyword?

2008-07-10 Thread Brendan
Hi there,
I have the following using Beautiful Soup:

soup = BeautifulSoup(data)
tags = soup.findAll(href=re.compile("/MER_FRS_L2_Canada/MER_FRS_\S
+gz"))
for tag in tags:
print tag['href']
print tag.parent.nextSibling.string
print tag.parent.nextSibling.nextSibling.string
print tag.parent.nextSibling.nextSibling.nextSibling.string
print
tag.parent.nextSibling.nextSibling.nextSibling.nextSibling.contents[0].string


For some reason I do not understand, using 'tag' as an iterator breaks
the code. Can someone tell me why? reading dir(soup) did not
illuminate me.

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


Re: Relative Package Import

2008-07-10 Thread Thomas

Peter Otten wrote:

Thomas wrote:


Robert Hancock wrote:

mypackage/
  __init__.py
  push/
__init__.py
 dest.py
 feed/
   __init__py
subject.py

In subject.py I have
 from ..push import dest

There is no such thing as relative package imports. See
http://www.python.org/doc/essays/packages.html


Unless you are using Python 1.5 the following document is a bit more
relevant:

http://www.python.org/dev/peps/pep-0328/


Oups, thanks for the pointer. I thought the old doc was still valid... 
(gee, there should be forward references).


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


Automatic keyword argument source change?

2008-07-10 Thread jorjun
Anyone know of a Python source code utility PSU, to automatically add
keyword arguments to method calls that don't have them? :

BEFORE

def get_total(books, binders, hinges):
return (binders.total + hinges.total - books.cost)

def print_total():
print get_total(novels, covers, brackets)

AFTER



def print_total():
-- print get_total(novels, covers, brackets)
++   print get_total(books=novels, binders=covers, hinges=brackets)

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


Re: Allow tab completion when inputing filepath?

2008-07-10 Thread Sion Arrowsmith
Keith Hughitt  <[EMAIL PROTECTED]> wrote:
>> Keith Hughitt wrote:
>> >  [ ... ] I have
>> > found some ways to enable tab completion for program-related commands,
>> > but not for system filepaths.
>Currently Unix/Console.

What's wrong with the readline module?
http://docs.python.org/lib/module-readline.html

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
   "Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
--
http://mail.python.org/mailman/listinfo/python-list

Re: Graphics

2008-07-10 Thread Michiel Overtoom
Vanam wrote...

> I want to know whether is there anything that has 
> to be installed in addition to python 2.5
>
> from gasp import *

You have to install the 'gasp' package too.

  https://launchpad.net/gasp-code/stable-0.1.x/0.1.1


-- 
"The ability of the OSS process to collect and harness
the collective IQ of thousands of individuals across
the Internet is simply amazing." - Vinod Vallopillil
http://www.catb.org/~esr/halloween/halloween4.html

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


Re: Retrieving BSTR * from a DLL

2008-07-10 Thread mzdude
On Jul 10, 6:15 am, Andrew MacIntyre <[EMAIL PROTECTED]>
wrote:

> This likely indicates that the DLL is using the C calling convention
> and not the stdcall calling convention.  Use CDLL rather than WinDLL
> to load the DLL.

using cdll got me over the calling hurdle. However, I'm not seeing the
returned BSTR.

> You might like to join the ctypes-users mailing list at sourceforge.

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


Re: python scalability

2008-07-10 Thread Bruno Desthuilliers

Tim Mitchell a écrit :

Hi All,

I work on a desktop application that has been developed using python and 
GTK (see www.leapfrog3d.com).  We have around 150k lines of python code 
(and 200k+ lines of C).  We also have a new project manager with a C# 
background who has deep concerns about the scalability of python as our 
code base continues to grow and we are looking at introducing more 
products.  I am looking for examples of other people like us (who write 
desktop apps in python) with code bases of a similar size who I can 
point to (and even better talk to) to help convince him that python is 
scalable to 300+ lines of code and beyond.


As far as I'm concerned, I'd say that if you didn't feel anything wrong 
nor spot any problem so far working on a 150 klocs project, there's just 
no need to worry. FWIW, a C# port would probably requires *way* much 
than 150 klocs.


 I have looked at the python 
success stories page and haven't come up with anyone quite like us. One 
of my project managers questions is: "Are we the only company in the 
world with this kind and size of project?"


Doing a quick count using wc -l (so the following numbers include 
comments and blank lines), Zope 2.8 is about 418 klocs, Plone 3 about 
259 klocs. Which, for the full stack, makes about 677 klocs. Even if you 
estimate a 50/50 ratio between real locs and comments/blank lines, this 
still makes something like 338 klocs.


Is that enough ?-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: User-defined exception: "global name 'TestRunError' is not defined"

2008-07-10 Thread Sion Arrowsmith
In article <[EMAIL PROTECTED]>,
 <[EMAIL PROTECTED]> wrote:
>I'm using some legacy code that has a user-defined exception in it.
>
>The top level program includes this line
>
>from TestRunError import *
>
>It also imports several other modules.  These other modules do not
>explicitly import TestRunError.  TestRunError is raised in various
>places throughout the modules.

The import line imports TestRunError into the "global" namespeace
of the top level program module *only*. It is not magically
propogated to the other modules imported -- if they want to use
TestRunError themselves, they have to import it, otherwise this
happens:

>FATAL ERROR: global name 'TestRunError' is not defined

>I realize this is kind of a silly question to ask in the general sense
>without showing more of the code, but does anyone have any suggestions
>as to the most likely causes of this error coming up?

The cause is whoever wrote the legacy code not understanding
how to program in Python.

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
   "Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
--
http://mail.python.org/mailman/listinfo/python-list

Symposium "Image Processing and Data Visualization" within the SEECCM 2009, Greece - Announce & Call for Papers

2008-07-10 Thread [EMAIL PROTECTED]
(Our apologies for cross-posting.
We appreciate if you kindly distribute this information by your co-
workers and colleagues.)

***

Symposium “Image Processing and Data Visualization”
2nd South-East European Conference on Computational Mechanics (SEECCM
2009)
Island of Rhodes, Greece, 22-24 June 2009
http://www.seeccm2009.org
(A special interest conference of the European Community on
Computational
Methods in Applied Sciences (ECCOMAS) and of the International
Association
for Computational Mechanics (IACM))

***

Dear Colleague,

Within the 2nd South-East European Conference on Computational
Mechanics (SEECCM 2009), to be held in Island of Rhodes, Greece, in
22-24 June 2009, we are organizing the Symposium “Image Processing and
Data Visualization”.

The main goal of the Symposium “Image Processing and Data
Visualization” is to provide a platform for communications among
specialists from complementary fields such as mechanics, computational
vision, mathematics, physics, informatics, computer graphics, bio-
medical-practice, psychology, clinicians and industry. Participants in
this symposium should present and discuss their proposed methods in
the corresponding fields that are related to the symposium topics and
explore the translational potentials of this emerging technological
field. Hence, this symposium should be an excellent opportunity to
refine ideas for future work and to establish constructive
cooperation.

Due to your research activities in the related fields, we are pleased
to invite you to submit your work and participate in the Symposium
“Image Processing and Data Visualization”.


Topics of interest include (but are not restricted to):
- Image Analysis;
- Image Restoration, Compression, Segmentation and Description;
- Object Tracking, Matching, Registration, Recognition and
Reconstruction;
- Visual Inspection;
- 3D Vision;
- Medical Imaging;
- Data Processing, Modelling and Analysis;
- Scientific Visualization;
- Enhanced Visualization;
- Human Computer Interaction;
- Enhanced and Virtual Reality;
- Simulation and Animation;
- Software Development for Image Processing and Data Visualization;
- Grid Computing in Image Processing and Data Visualization;
- Applications of Image Processing and Data Visualization.

Important dates and Instructions:
- Deadline for submission one page abstract: 30 November 2008;
- Authors notification: 31 December 2008;
- Final contribution submission: 28 February 2009.
- To submit your contribution, you should access the conference
website at www.seeccm2009.org and select the Symposium “Image
Processing and Data Visualization” or send your file by email to the
organizers ([EMAIL PROTECTED], [EMAIL PROTECTED]).


With kind regards,
Yours sincerely,

The Organizers,

João Manuel R. S. Tavares ([EMAIL PROTECTED])
Faculty of Engineering of University of Porto, Porto, Portugal
Christos E. Constantinou ([EMAIL PROTECTED])
Stanford University, USA
--
http://mail.python.org/mailman/listinfo/python-list


Re: python scalability

2008-07-10 Thread Michele Simionato
On Jul 10, 6:32 am, Tim Mitchell <[EMAIL PROTECTED]> wrote:
> Hi All,
>
> I work on a desktop application that has been developed using python and
> GTK (seewww.leapfrog3d.com).  We have around 150k lines of python code
> (and 200k+ lines of C).

We have bigger numbers than yours here (although not for a desktop
application)
and of course we have the problems of a large size application, but
they have
nothing to do with Python. The real problem are sociological, not
language-related.
Essentially, if a project takes 10+ years and 10+ people, with most of
the people
new, you have an issue, but this is independent from the language.
Python is helping
us at least because it is readable and the situation would be probably
be worse with
another language. But as I said the software development practices
used are more
important than the language in this context.
--
http://mail.python.org/mailman/listinfo/python-list


Re: TypeError, I know why but not how!?

2008-07-10 Thread ssecorp
I don't fully understand why I have to do this.


On Jul 10, 4:17 am, Robert Kern <[EMAIL PROTECTED]> wrote:
> ssecorp wrote:
> > Im looking into PvsNP:
> >http://www.claymath.org/millennium/P_vs_NP/
> > so I thought I'd write the program just to get a feel for it.
>
> > But I run into a problem. Why does it all the sudden return None? I
> > mean I know why the program aborts but I dont understand why the None
> > is generated all the sudden. Hitting recursion depth isn't reported
> > with that error.
>
> def validate(placed):
>      student = round(random.random()*401)
>      if student in placed:
>          # You need to explicitly return, here:
>          return validate(placed)
>      else:
>          placed.append(student)
>          return student, placed
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless enigma
>   that is made terrible by our own mad attempt to interpret it as though it 
> had
>   an underlying truth."
>    -- Umberto Eco

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


Weird lambda rebinding/reassignment without me doing it

2008-07-10 Thread ssecorp
I am never redefining the or reassigning the list when using validate
but since it spits the modified list back out that somehow means that
the modified list is part of the environment and not the old one.
i thought what happend inside a function stays inside a function
meaning what comes out is independent of what comes in.
Meaning if I want the list I send as a parameter to the function I
have to do x = func(x) and not just func(x) and x is magically what
comes out of func().
Doesnt Python have closure or that isnt what this is about?


def validate(placed):
student = round(random.random()*401)
if student in placed:
return validate(placed)
else:
placed.append(student)
return student, placed

def val(placed):
student = round(random.random()*401)
if student in placed:
return validate(placed)
else:
placed.append(student)
return student



>>> g = lambda x:validate(x)
>>> l=[]
>>> for x in range(1,10):
g(l)


(141.0, [141.0])
(19.0, [141.0, 19.0])
(86.0, [141.0, 19.0, 86.0])
(120.0, [141.0, 19.0, 86.0, 120.0])
(76.0, [141.0, 19.0, 86.0, 120.0, 76.0])
(262.0, [141.0, 19.0, 86.0, 120.0, 76.0, 262.0])
(234.0, [141.0, 19.0, 86.0, 120.0, 76.0, 262.0, 234.0])
(74.0, [141.0, 19.0, 86.0, 120.0, 76.0, 262.0, 234.0, 74.0])
(325.0, [141.0, 19.0, 86.0, 120.0, 76.0, 262.0, 234.0, 74.0, 325.0])
>>> g = lambda x:val(x)
>>> l=[]
>>> for x in range(1,10):
g(l)


183.0
33.0
315.0
244.0
308.0
168.0
146.0
378.0
297.0
>>>
--
http://mail.python.org/mailman/listinfo/python-list


Re: win32com.client (Howto edit Contacts in Outlook)

2008-07-10 Thread Bill Davy
"Tim Roberts" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> "Bill Davy" <[EMAIL PROTECTED]> wrote:
>>
>>I am trying to edit Contacts in Outlook.  This is so I can transfer 
>>numbers
>>from my address book which is an Excel spreadsheet to my mobile phone.
>
> Are you actually running Outlook?  Your news posting was made from Outlook
> Express, and Outlook Express cannot be controlled by COM (although MAPI
> works).
> -- 
> Tim Roberts, [EMAIL PROTECTED]
> Providenza & Boekelheide, Inc.


I'm not sure OL2003 can read news.  I think perhaps some later OL can (added 
tot he View menu, perhaps?).  So I use OL Express to read news.  The OL with 
which I wish to communicate is:

Application name Outlook
Version 11.0
Build 8217
Product ID 70141-700-0350904-56905
Language English (United States)
Application Path C:\Program Files\Microsoft Office\OFFICE11\
System Language English (United Kingdom)
Mail Support Not Available
Current folder Inbox
Current item Not Available

Not sure why its says "Mail Support Not Available" as all I use it for is 
email (oh, and the calendar).

Hey and ho
Bill 


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


Re: start reading from certain line

2008-07-10 Thread jstrick
Here's a simple way to do it with a minimum amount of loopiness (don't
forget to use 'try-except' or 'with' in real life):

f = open("item1.txt")

for preline in f:
if "Item 1" in preline:
print preline,
for goodline in f:
# could put an end condition with a 'break' here
print goodline,

f.close()
--
http://mail.python.org/mailman/listinfo/python-list


Re: win32com.client (Howto edit Contacts in Outlook)

2008-07-10 Thread Tim Golden

Bill Davy wrote:
I'm not sure OL2003 can read news.  I think perhaps some later OL can (added 
tot he View menu, perhaps?).  So I use OL Express to read news.  The OL with 
which I wish to communicate is:


Application name Outlook
Version 11.0
Build 8217
Product ID 70141-700-0350904-56905
Language English (United States)
Application Path C:\Program Files\Microsoft Office\OFFICE11\
System Language English (United Kingdom)
Mail Support Not Available
Current folder Inbox
Current item Not Available


Where did you get to with this, Bill? I wasn't sure if no news
was good news or whether you'd gone a different way, or
were still trying things...

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


Re: socket-module: different behaviour on windows / unix when a timeout is set

2008-07-10 Thread Grant Edwards
On 2008-07-10, A.T.Hofkamp <[EMAIL PROTECTED]> wrote:
> On 2008-07-09, Mirko Vogt <[EMAIL PROTECTED]> wrote:
>> Is that behaviour common or even documented? Found nothing.
> Second sentence in the socket module documentation:
>
> Note: Some behavior may be platform dependent, since calls are
> made to the operating system socket APIs.
>
> So yes, what you found is probably how it should behave.

I have a hard time believing that the phrase "platform
dependent" should be stretched to cover the OP's example where
the results are just plain _wrong_.

-- 
Grant Edwards   grante Yow! I'm having BEAUTIFUL
  at   THOUGHTS about the INSIPID
   visi.comWIVES of smug and wealthy
   CORPORATE LAWYERS ...
--
http://mail.python.org/mailman/listinfo/python-list


Changing self: if self is a tree how to set to a different self

2008-07-10 Thread Bart Kastermans
I am playing with some trees.  In one of the procedures I wrote
for this I am trying to change self to a different tree.  A tree
here has four members (val/type/left/right).  I found that self = SS
does not work; I have to write self.val = SS.val and the same for
the other members (as shown below).  Is there a better way to do this?

In the below self is part of a parse tree, F is the parse tree of a
function f with argument x.  If a node in the parse tree is labelled
f, we should replace it by the parse tree for the function f, F, with
the remainder of the tree substituted for the input variable for the
function f, here x.

def elimF (self):
if self.val == "f":
SS = F.copy ()
SS.subst ('x', self.left)
self.val = SS.val# from here: set self to be SS
self.type = SS.type
self.left = SS.left
self.right = SS.right# completed: set self to be SS

if self.left != None:# iterate onward, inf recursion if f
 # appears.  Would need a check in
 # real version
self.left.elimF ()
if self.right != None:
self.right.elimF ()

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


Re: Weird lambda rebinding/reassignment without me doing it

2008-07-10 Thread A.T.Hofkamp
Python doesn't use value semantics for variables but reference semantics:

a = [1]
b = a

In many languages, you'd now have 2 lists. In Python you still have one list,
and both a and b refer to it.

Now if you modify the data (the list), both variables will change

a.append(2)  # in-place modification of the list
print b  # will print [1,2]


If you don't want this, you should make a copy somewhere

a = a + [2]

or

b = a[:]


It takes a bit of getting used to, but it is very handy in practice.


Read the docs about 'immutable' and 'mutable' types.
(I expected this to be a FAQ, but I couldn't quickly find a matching entry)


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


handling unexpected exceptions in pdb

2008-07-10 Thread Simon Bierbaum

Hi all,

I'm in an interactive session in pdb, debugging my code using  
pdb.runcall. Somewhere, an exception is raised and lands uncaught on  
stdout. Is there any way of picking up this exception and at least  
read the full message, or even accessing its stack trace to determine  
where exactly within the one line I just executed it was raised? This  
is where I'm stuck:


> /usr/local/apache2/bin/Model/Database.py(287)disconnect()
(Pdb) n
FlushError: FlushErr...perly.",)
> /usr/local/apache2/bin/Model/Database.py(287)disconnect()
(Pdb) import sys
(Pdb) sys.last_traceback
*** AttributeError: 'module' object has no attribute 'last_traceback'

Thanks, Simon

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


Re: python scalability

2008-07-10 Thread Jeffrey Froman
Tim Mitchell wrote:

> One of my project managers questions is: "Are we the only company in the
> world with this kind and size of project?"

I can't provide a bigger success story personally (my largest project is
currently about 15k lines of code, eminently manageable by one person.) But
Google comes to mind. Just a feeling I get from quotes like:

"Python is big at Google ... being used for everything from build tools to
managing ads."
-- Guido Van Rossum 
http://www.artima.com/weblogs/viewpost.jsp?thread=143947

"When programming in python, one should design for scalability.  If you
design properly, the you should be able to just throw more machines at the
problem and your app should scale fine."
-- Greg Stein's PyCON keynote, paraphrased by Matt Harrison
http://panela.blog-city.com/python_at_google_greg_stein__sdforum.htm

Maybe that's not exactly the sort of scalability you're referring to, but
certainly Goolge is colossal in its deployment. I have a hard time
imagining that scalability based on "N lines of code" is going to be any
better in any other language.


Jeffrey


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


Re: Does omniORBpy 3.2 supports DII?

2008-07-10 Thread Wolfgang Keller
> My apologies if this is not the correct forum for thses quiestions, 

It's not the wrong place to ask, but you're more likely to get answers
from the omniORB mailing lists:

http://www.omniorb-support.com/mailman/listinfo

Sincerely,

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


Loading just in time

2008-07-10 Thread D'Arcy J.M. Cain
I am trying to create a utility module that only loads functions when
they are first called rather than loading everything.  I have a bunch
of files in my utility directory with individual methods and for each I
have lines like this in __init__.py:

def calc_tax(*arg, **name):
from calc_tax import calc_tax as _func_
calc_tax = _func_
return _func_(*arg, **name)

This works the first time I call utility.calc_tax but if I call it
again I get a "TypeError: 'module' object is not callable" error.  Is
there any way to do what I want or do I have to put everything back
into a single file.  Of course, I can simply change all my calls to
utility.calc_tax.calc_tax(...) but I have a lot of code to change if I
do that.

Thanks.

-- 
D'Arcy J.M. Cain <[EMAIL PROTECTED]> |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
--
http://mail.python.org/mailman/listinfo/python-list


Smal question

2008-07-10 Thread Hans Müller

Hello group,

I have some scripts sharing some common functions.
So what I'd like to have is a modern include.
Of course python does not have (with good reasons) no include statement.
But I'm too lazy to create a module which has to be installed into the 
interpreter for some functions I need to share in a project.
Is there any idea to do this ?

Thanks a lot

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


Re: Smal question

2008-07-10 Thread Nick Dumas
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

This is easy. Simply create a .py file containing all the methods you
want to share. Put this file in the same directory as the rest of your
project files. In each of your project files that requires one of the
methods, simply "import ". Python supports
impromptu module creation, which is great just for circumstances like this.

Hans Müller wrote:
> Hello group,
> 
> I have some scripts sharing some common functions.
> So what I'd like to have is a modern include.
> Of course python does not have (with good reasons) no include statement.
> But I'm too lazy to create a module which has to be installed into the
> interpreter for some functions I need to share in a project.
> Is there any idea to do this ?
> 
> Thanks a lot
> 
> Hans
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkh2JOAACgkQLMI5fndAv9gZiACeP3puvMknR2HQTOXKaNeBtC+h
tyEAn3C6g9E5U5xXE4SQ7u368xdvkRx4
=J6Sv
-END PGP SIGNATURE-
--
http://mail.python.org/mailman/listinfo/python-list


RE: Terminate a python script from linux shell / bash script

2008-07-10 Thread Gros Bedo

>>That's not how it works. If you kill one running python script it will not
>>effect other python scripts. Each script has its own interpreter process
>>running. 

>GB> So, is there a way from the Linux shell or a bash script to terminate
>GB> just one specific Python script ? 

>>So just kill it.

Yes I've seen that each python script calls its own instance of Python. But how 
to know which is the good one in bash ? Is there a command that gets the 
parameters of process, so I could use grep to select the one containing the 
name of my script ?
_
Votre contact a choisi Hotmail, l'e-mail nouvelle génération. Créez un compte. 
http://www.windowslive.fr/hotmail/default.asp
--
http://mail.python.org/mailman/listinfo/python-list


profiling question

2008-07-10 Thread Neal Becker
Just to confirm, the profiling numbers (from cProfile) do include time spent
inside my own C functions that I import as modules?

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


Re: Smal question

2008-07-10 Thread Hans Müller

Thanks a lot,

you made my day.

As often in python, it's really simple and useful !

Greetings

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


Re: Python / Windows process control

2008-07-10 Thread Tim Golden

Salim Fadhley wrote:

Does anybody know of a python module which can do process management
on Windows? The sort of thing that we might usually do with
taskmgr.exe or process explorer?

For example:

* Kill a process by ID
* Find out which process ID is locking an object in the filesystem
* Find out all the IDs of a particular .exe file
* Find all the details of a currently running process (e.g. given an
ID tell me which files it uses, niceness, runtime)


As far as I know, the closest you're going to come here is
WMI [1]. It won't do everything you ask, though. I don't know
how to find out which processes have a lock on a filesystem
object.

When you say "Find all the ids of a particular .exe file" I
assume you mean: all the processes which were started
by running that file.


import subprocess
import time

import wmi
c = wmi.WMI ()

#
# Kill a process by id
#
notepad = subprocess.Popen (["notepad.exe"])
time.sleep (1)
for process in c.Win32_Process (ProcessId=notepad.pid):
 process.Terminate ()

#
# Which process ids correspond to an .exe
#
for i in range (5): 
 subprocess.Popen (["notepad.exe"])


for process in c.Win32_Process (caption="notepad.exe"):
 print process.ProcessId

#
# _Some_ (but not all) of the information about each file
#
for process in c.Win32_Process (caption="notepad.exe"):
 print process
 process.Terminate ()



HTH

TJG

[1] http://timgolden.me.uk/python/wmi.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: re.search much slower then grep on some regular expressions

2008-07-10 Thread J. Cliff Dyer

On Wed, 2008-07-09 at 12:29 -0700, samwyse wrote:
> On Jul 8, 11:01 am, Kris Kennaway <[EMAIL PROTECTED]> wrote:
> > samwyse wrote:
> 
> > > You might want to look at Plex.
> > >http://www.cosc.canterbury.ac.nz/greg.ewing/python/Plex/
> >
> > > "Another advantage of Plex is that it compiles all of the regular
> > > expressions into a single DFA. Once that's done, the input can be
> > > processed in a time proportional to the number of characters to be
> > > scanned, and independent of the number or complexity of the regular
> > > expressions. Python's existing regular expression matchers do not have
> > > this property. "
> 
> > Hmm, unfortunately it's still orders of magnitude slower than grep in my
> > own application that involves matching lots of strings and regexps
> > against large files (I killed it after 400 seconds, compared to 1.5 for
> > grep), and that's leaving aside the much longer compilation time (over a
> > minute).  If the matching was fast then I could possibly pickle the
> > lexer though (but it's not).
> 
> That's funny, the compilation is almost instantaneous for me.
> However, I just tested it to several files, the first containing
> 4875*'a', the rest each twice the size of the previous.  And you're
> right, for each doubling of the file size, the match take four times
> as long, meaning O(n^2).  156000*'a' would probably take 8 hours.
> Here are my results:
> 
> compile_lexicon() took 0.0236021580595 secs
> test('file-0.txt') took 24.8322969831 secs
> test('file-1.txt') took 99.3956799681 secs
> test('file-2.txt') took 398.349623132 secs

Sounds like a good strategy would be to find the smallest chunk of the
file that matches can't cross, and iterate your search on units of those
chunks.  For example, if none of your regexes cross line boundaries,
search each line of the file individually.  That may help turn around
the speed degradation you're seeing.

Cheers,
Cliff

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


Re: python scalability

2008-07-10 Thread Larry Bates

Tim Mitchell wrote:

Hi All,

I work on a desktop application that has been developed using python and 
GTK (see www.leapfrog3d.com).  We have around 150k lines of python code 
(and 200k+ lines of C).  We also have a new project manager with a C# 
background who has deep concerns about the scalability of python as our 
code base continues to grow and we are looking at introducing more 
products.  I am looking for examples of other people like us (who write 
desktop apps in python) with code bases of a similar size who I can 
point to (and even better talk to) to help convince him that python is 
scalable to 300+ lines of code and beyond.  I have looked at the python 
success stories page and haven't come up with anyone quite like us. One 
of my project managers questions is: "Are we the only company in the 
world with this kind and size of project?"

I want to say no, but am having trouble convincing myself, let alone him.

If you are involved in this kind of thing please get in touch with me.

Thanks,
Tim


It is not clear what "scalability" means to a desktop application?  When maximum 
calculation speeds are required, you can drop into C libraries from Python with 
ease.  If this is about making use of multiple cores on a machine this link 
(http://wiki.python.org/moin/ParallelProcessing) covers most of the solutions 
that can be implementing in Python.


Another poster has suggested that you would need 1500K lines of C to replace 
300K lines of Python (and he may be being generous to C).  Projects are about 
more than raw speed: maintainability, speed of adding features, etc.  Python 
wins those races in most situations.  I'm sure that hand coded assembler would 
run fastest of all, but little application software is written that way these days.


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


Local User Control

2008-07-10 Thread Sparky
I don't know how feasible this is, but is it possible to have users
log in to access a local database file in such a way that allows the
program to know what user name and password they logged in with? This
would involve separate user names and passwords for each user.

Thanks for your time and help,
Sam
--
http://mail.python.org/mailman/listinfo/python-list


Re: start reading from certain line

2008-07-10 Thread Iain King
On Jul 10, 2:45 pm, jstrick <[EMAIL PROTECTED]> wrote:
> Here's a simple way to do it with a minimum amount of loopiness (don't
> forget to use 'try-except' or 'with' in real life):
>
> f = open("item1.txt")
>
> for preline in f:
>     if "Item 1" in preline:
>         print preline,
>         for goodline in f:
>             # could put an end condition with a 'break' here
>             print goodline,
>
> f.close()

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


Re: Local User Control

2008-07-10 Thread Tim Golden

Sparky wrote:

I don't know how feasible this is, but is it possible to have users
log in to access a local database file in such a way that allows the
program to know what user name and password they logged in with? This
would involve separate user names and passwords for each user.


Well, this is a question which is crying out for some
context. Are you talking about an existing database
on an existing platform? If so, which one? Are you
talking about a database youo're thinking of building?
If so, the answer's probably yes but only you can
know. Are you talking about something else altogether?

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


Re: start reading from certain line

2008-07-10 Thread Iain King
On Jul 10, 4:54 pm, Iain King <[EMAIL PROTECTED]> wrote:
> On Jul 10, 2:45 pm, jstrick <[EMAIL PROTECTED]> wrote:
>
> > Here's a simple way to do it with a minimum amount of loopiness (don't
> > forget to use 'try-except' or 'with' in real life):
>
> > f = open("item1.txt")
>
> > for preline in f:
> >     if "Item 1" in preline:
> >         print preline,
> >         for goodline in f:
> >             # could put an end condition with a 'break' here
> >             print goodline,
>
> > f.close()
>
> No

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


Re: Loading just in time

2008-07-10 Thread Larry Bates

D'Arcy J.M. Cain wrote:

I am trying to create a utility module that only loads functions when
they are first called rather than loading everything.  I have a bunch
of files in my utility directory with individual methods and for each I
have lines like this in __init__.py:

def calc_tax(*arg, **name):
from calc_tax import calc_tax as _func_
calc_tax = _func_
return _func_(*arg, **name)

This works the first time I call utility.calc_tax but if I call it
again I get a "TypeError: 'module' object is not callable" error.  Is
there any way to do what I want or do I have to put everything back
into a single file.  Of course, I can simply change all my calls to
utility.calc_tax.calc_tax(...) but I have a lot of code to change if I
do that.

Thanks.



You are stuck in a futile battle called "premature optimization".  I would 
suggest that you stop worrying about any performance you would gain from doing 
something like this.  Python has been "highly" optimized to handle imports in a 
very efficient way.  Just put your functions in a file and import them.


from myfunctions import calc_tax, ...

Then you don't have to preface the function name with the module name.

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


Re: Local User Control

2008-07-10 Thread Sparky
On Jul 10, 9:58 am, Tim Golden <[EMAIL PROTECTED]> wrote:
> Sparky wrote:
> > I don't know how feasible this is, but is it possible to have users
> > log in to access a local database file in such a way that allows the
> > program to know what user name and password they logged in with? This
> > would involve separate user names and passwords for each user.
>
> Well, this is a question which is crying out for some
> context. Are you talking about an existing database
> on an existing platform? If so, which one? Are you
> talking about a database youo're thinking of building?
> If so, the answer's probably yes but only you can
> know. Are you talking about something else altogether?
>
> TJG

Thanks for the timely response. This would be a database that I am
building myself. The question comes down to is there a feasible way to
verify a user's user name and password from inside that database.
Obviously the file would be encrypted, but if there is going to be
more than one user using it I suppose there would be a separate file
for a log-in. I am just asking for some guidance on how this would
theoretically be implemented.

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


Re: Loading just in time

2008-07-10 Thread D'Arcy J.M. Cain
On Thu, 10 Jul 2008 11:03:10 -0500
Larry Bates <[EMAIL PROTECTED]> wrote:
> D'Arcy J.M. Cain wrote:
> > def calc_tax(*arg, **name):
> > from calc_tax import calc_tax as _func_
> > calc_tax = _func_
> > return _func_(*arg, **name)

> You are stuck in a futile battle called "premature optimization".  I would 

I thought that I might be.

> suggest that you stop worrying about any performance you would gain from 
> doing 
> something like this.  Python has been "highly" optimized to handle imports in 
> a 
> very efficient way.  Just put your functions in a file and import them.

Performance optimization wasn't really my goal here.  What I was
looking for was the ability to spread the functions around different
files to manage them better from a proggrammer's POV.

Oh well.  I guess I will just combine them all again.  At least I found
some dead code, duplicate functions and just plain bad code doing this
little exercise.  :-)

-- 
D'Arcy J.M. Cain <[EMAIL PROTECTED]> |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Local User Control

2008-07-10 Thread Tim Golden

Sparky wrote:

On Jul 10, 9:58 am, Tim Golden <[EMAIL PROTECTED]> wrote:

Sparky wrote:

I don't know how feasible this is, but is it possible to have users
log in to access a local database file in such a way that allows the
program to know what user name and password they logged in with? This
would involve separate user names and passwords for each user.

Well, this is a question which is crying out for some
context. Are you talking about an existing database
on an existing platform? If so, which one? Are you
talking about a database youo're thinking of building?
If so, the answer's probably yes but only you can
know. Are you talking about something else altogether?

TJG


Thanks for the timely response. This would be a database that I am
building myself. The question comes down to is there a feasible way to
verify a user's user name and password from inside that database.
Obviously the file would be encrypted, but if there is going to be
more than one user using it I suppose there would be a separate file
for a log-in. I am just asking for some guidance on how this would
theoretically be implemented.



Maybe someone else on the list has a clearer idea than I do,
but at this distance from an implementation, all I can say is:
yes, I'm sure you can achieve some kind of user authentication.
After all, lots of other products already do. Or is your proposed
db very different?

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


Re: Weird lambda rebinding/reassignment without me doing it

2008-07-10 Thread David C. Ullrich
In article 
<[EMAIL PROTECTED]>,
 ssecorp <[EMAIL PROTECTED]> wrote:

> I am never redefining the or reassigning the list when using validate
> but since it spits the modified list back out that somehow means that
> the modified list is part of the environment and not the old one.
> i thought what happend inside a function stays inside a function
> meaning what comes out is independent of what comes in.
> Meaning if I want the list I send as a parameter to the function I
> have to do x = func(x) and not just func(x) and x is magically what
> comes out of func().

A function cannot modify the value of a global variable
(unless it specifies "global"). It doesn't reassign anything.
But in the functions below you're not reassigning a variable,
you're _modifiying_ an object. A function _can_ modify an
object you pass to it:

>>> def DoesntReassign(x):
...   x = 0
... 
>>> def Modifies(x):
...   x.append(0)
... 
>>> x=42
>>> DoesntReassign(x)
>>> x
42
>>> x=[]
>>> Modifies(x)
>>> x
[0]

Hmm, look at this:

>>> x=[]
>>> id(x)
404296
>>> Modifies(x)
>>> x
[0]
>>> id(x)
404296

'x' refers to exactly the same object before and after
the call to Modifies. The function has _modified_ that
object, but it hasn't redefined or reassigned anything.

> Doesnt Python have closure or that isnt what this is about?
> 
> 
> def validate(placed):
> student = round(random.random()*401)
> if student in placed:
> return validate(placed)
> else:
> placed.append(student)
> return student, placed
> 
> def val(placed):
> student = round(random.random()*401)
> if student in placed:
> return validate(placed)
> else:
> placed.append(student)
> return student
> 
> 
> 
> >>> g = lambda x:validate(x)
> >>> l=[]
> >>> for x in range(1,10):
>   g(l)
> 
> 
> (141.0, [141.0])
> (19.0, [141.0, 19.0])
> (86.0, [141.0, 19.0, 86.0])
> (120.0, [141.0, 19.0, 86.0, 120.0])
> (76.0, [141.0, 19.0, 86.0, 120.0, 76.0])
> (262.0, [141.0, 19.0, 86.0, 120.0, 76.0, 262.0])
> (234.0, [141.0, 19.0, 86.0, 120.0, 76.0, 262.0, 234.0])
> (74.0, [141.0, 19.0, 86.0, 120.0, 76.0, 262.0, 234.0, 74.0])
> (325.0, [141.0, 19.0, 86.0, 120.0, 76.0, 262.0, 234.0, 74.0, 325.0])
> >>> g = lambda x:val(x)
> >>> l=[]
> >>> for x in range(1,10):
>   g(l)
> 
> 
> 183.0
> 33.0
> 315.0
> 244.0
> 308.0
> 168.0
> 146.0
> 378.0
> 297.0
> >>>

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


Re: a simple 'for' question

2008-07-10 Thread Ethan Furman

Tim Roberts wrote:

Ethan Furman <[EMAIL PROTECTED]> wrote:



Ben Keshet wrote:

it didn't help.  it reads the pathway "as is" (see errors for both 
tries).  It looks like it had the write pathway the first time, but 
could not find it because it searched in the path/way instead of in the 
path\way.  thanks for trying.


The form of slash ('\' vs '/') is irrelevant to Python.  At least on 
Windows.




folders= ['1','2','3']
for x in folders:
   print x # print the current folder
   filename='Folder/%s/myfile.txt' %[x]


  ^- brackets not needed



More than that, the brackets are CAUSING this problem.  The "%" formatting
operator expects to find either a single item, or a tuple containing
multiple items.  It does NOT look for a generic iterator.  In this case,
the %s will use the whole list as its parameter.  Python converts the list
to string, and the string representation of that one-item list is ['1'].


You are, of course, correct -- the brackets are causing the displayed 
problem below.  But judging from the thread so far, there are actually 
multiple problems going on here, of which the brackets are only this one 
part.  It looks like the OP also lacks understanding regarding absolute 
and relative path names, as well as (possibly) not knowing where his 
script/interpreter is running from.






   f=open(filename,'r')

gives: IOError: [Errno 2] No such file or directory: 
"Folder/['1']/myfile.txt"



Just like that.


As far as the Python question of string substitution, "%s" % var is an 
appropriate way.



Right.


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


Re: Local User Control

2008-07-10 Thread Sparky
On Jul 10, 10:13 am, Tim Golden <[EMAIL PROTECTED]> wrote:
> Sparky wrote:
> > On Jul 10, 9:58 am, Tim Golden <[EMAIL PROTECTED]> wrote:
> >> Sparky wrote:
> >>> I don't know how feasible this is, but is it possible to have users
> >>> log in to access a local database file in such a way that allows the
> >>> program to know what user name and password they logged in with? This
> >>> would involve separate user names and passwords for each user.
> >> Well, this is a question which is crying out for some
> >> context. Are you talking about an existing database
> >> on an existing platform? If so, which one? Are you
> >> talking about a database youo're thinking of building?
> >> If so, the answer's probably yes but only you can
> >> know. Are you talking about something else altogether?
>
> >> TJG
>
> > Thanks for the timely response. This would be a database that I am
> > building myself. The question comes down to is there a feasible way to
> > verify a user's user name and password from inside that database.
> > Obviously the file would be encrypted, but if there is going to be
> > more than one user using it I suppose there would be a separate file
> > for a log-in. I am just asking for some guidance on how this would
> > theoretically be implemented.
>
> Maybe someone else on the list has a clearer idea than I do,
> but at this distance from an implementation, all I can say is:
> yes, I'm sure you can achieve some kind of user authentication.
> After all, lots of other products already do. Or is your proposed
> db very different?
>
> TJG

Thanks. I suppose I probably should get further down the road and come
back with a more specific question.

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


Re: formatting list -> comma separated

2008-07-10 Thread norseman


Robert wrote:

given d:

d = ["soep", "reeds", "ook"]

I want it to print like

soep, reeds, ook

I've come up with :

print ("%s"+", %s"*(len(d)-1)) % tuple(d)

but this fails for d = []

any (pythonic) options for this?

Robert



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



the old fashion way would be:

d = ["soep", "reeds", "ook"]
if len(d) > 0:
  print ("%s"+", %s"*(len(d)-1)) % tuple(d)

# if d= []  then print stmnt bypassed



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


Re: re.search much slower then grep on some regular expressions

2008-07-10 Thread Sebastian "lunar" Wiesner
Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]>:

> On Mon, 07 Jul 2008 16:44:22 +0200, Sebastian \"lunar\" Wiesner wrote:
> 
>> Mark Wooding <[EMAIL PROTECTED]>:
>> 
>>> Sebastian "lunar" Wiesner <[EMAIL PROTECTED]> wrote:
>>> 
 # perl -e '("a" x 10) =~ /^(ab?)*$/;'
 zsh: segmentation fault  perl -e '("a" x 10) =~ /^(ab?)*$/;'
>>> 
>>> (Did you really run that as root?)
>> 
>> How come, that you think so?
> 
> The '#' is usually the prompt of root accounts while ordinary users get a
> '$'.

Oh ... I just used "#" as a general placeholder, because I didn't want to
copy my whole two-line prompt ;)  I can assure, that I did not try this as
root ;)  and will use "$" in future ;)

-- 
Freedom is always the freedom of dissenters.
  (Rosa Luxemburg)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Smal question

2008-07-10 Thread Bruno Desthuilliers

Hans Müller a écrit :

Hello group,

I have some scripts sharing some common functions.
So what I'd like to have is a modern include.
Of course python does not have (with good reasons) no include statement.
But I'm too lazy to create a module which has to be installed into the 
interpreter for some functions I need to share in a project.


What do you mean "installed in the interpreter" ?

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


Re: ActiveState Code: the new Python Cookbook site

2008-07-10 Thread Trent Mick

Stef Mientki wrote:

one small remark,
If I want to browse 200 recipes, at 10 per page  
... please make something like 100 available per page,
are internet is fast enough nowadays.


Touche. Done:

  http://code.activestate.com/recipes/?paginate_by=100

Cheers,
Trent

--
Trent Mick
trentm at activestate.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Emacs/Python Essentials?

2008-07-10 Thread Sebastian "lunar" Wiesner
xkenneth <[EMAIL PROTECTED]>:

> What does everyone consider essential for emacs python dev?

yasnippet is worth being looked at

-- 
Freedom is always the freedom of dissenters.
  (Rosa Luxemburg)
--
http://mail.python.org/mailman/listinfo/python-list


installing any python module

2008-07-10 Thread Bhagwat Kolde
Hi all,

What is the correct process of installing any external python module?
Once we downloaded any python module,

Q1) Where this module should be placed in python installation file
structure?
Q2) How to execute setup.py file?



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

Re: variable question

2008-07-10 Thread norseman


Support Desk wrote:

I am trying to assign a variable using an if / else statement like so:

 


If condition1:

Variable = something

If condition2:

Variable = something else

Do stuff with variable.

 


But the variable assignment doesn't survive outside the if statement. Is
there any better way to assign variables using an if statement or exception
so I don't have to write two almost identical if statements. This is
probably a dumb question.






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

===

Is this test in a def?  If in-line there should be no problem if Var is 
done as this:


var= ' '
test= '2'
if test == '1':
  var= 'choice 1'
elif test == '2':
  var= 'choice 2'
elif test == '3':
  var= 'default'
#endif
#do something with var

print var
-

if inside a def:

var= 0
def xxx():
  global var
  var += 1 # whatever, if var was set to text, do text things
  #possible other commands
#enddef
# somewhere below in 'main' (runtime portion of code)

xxx()
print var  #use var, etc. etc.

-

It is ALWAYS good form to prime a var before using.



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


Re: plugins using cvs/distutils?

2008-07-10 Thread Sebastian "lunar" Wiesner
Deacon <[EMAIL PROTECTED]>:

> Hi. I have an open-source application development environment that I
> would like to enable an automated package download system for (like
> downloadable plugins), using sourceforge as its repository. My
> software will have a menu-based popup window, that will list the
> packages (Applications) available through the project distribution
> page. If a user selects a package from this window, it should be
> automatically retrieved and installed by the development environment.
> My software already has a system for automatically recognizing and
> deploying new packages, so I'm only asking for help with distribution
> logistics, I think.
> 
> I'm thinking that I will use the built-in ftplib to list and retrieve
> the files (but I'm not sure yet where to look), and then the distutils
> should probably be used in a standardized manner to actually install
> the packages, right? I've established a CVS, but I'm not aware of any
> built-in-python tools for managing the CVS, and I would rather not add
> any new user-has-to-download-dependencies for this. Is there an easier
> or alternative advisable approach? I have a linux box, should I be
> using it to build all of the platform versions? Any advice would be
> much appreciated, this is my first distribution project
> (sourceforge.net/projects/spade).

I'd use setuptools entrypoints.  This takes the task of plugin discovery
from you, as this is done by setuptools, and allows you to utilize
pypi/easy_install for plugin distribution.

-- 
Freedom is always the freedom of dissenters.
  (Rosa Luxemburg)
--
http://mail.python.org/mailman/listinfo/python-list


Idiomatic Python to convert list to dict

2008-07-10 Thread James Fassett
Hi all,

Simple question really on a best practice. I want to avoid adding
duplicates to a list.

my_list = ['a', 'b', 'c', 'd', 'e']
dup_map = {}
for item in my_list:
dup_map[item] = True

# ... sometime later

for complex_dict in large_list:
if complex_dict["char"] not in dup_map:
my_list.append(complex_dict["char"])
dup_map[complex_dict["char"]] = True

For the first part (generating the duplicate map) is there a more
idiomatic Python method for flipping the list into a dict?

Is there a better way to achieve the overall objective (as hinted at
by the above algorithm)?

Thanks in advance for any tips.

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


Re: Idiomatic Python to convert list to dict

2008-07-10 Thread Diez B. Roggisch

James Fassett schrieb:

Hi all,

Simple question really on a best practice. I want to avoid adding
duplicates to a list.

my_list = ['a', 'b', 'c', 'd', 'e']
dup_map = {}
for item in my_list:
dup_map[item] = True

# ... sometime later

for complex_dict in large_list:
if complex_dict["char"] not in dup_map:
my_list.append(complex_dict["char"])
dup_map[complex_dict["char"]] = True

For the first part (generating the duplicate map) is there a more
idiomatic Python method for flipping the list into a dict?

Is there a better way to achieve the overall objective (as hinted at
by the above algorithm)?

Thanks in advance for any tips.


Instead of a dict, use a set. It's immediatly contructable from my_list, 
and better suited for the task anyway.


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


Re: re.search much slower then grep on some regular expressions

2008-07-10 Thread Kris Kennaway

J. Cliff Dyer wrote:

On Wed, 2008-07-09 at 12:29 -0700, samwyse wrote:

On Jul 8, 11:01 am, Kris Kennaway <[EMAIL PROTECTED]> wrote:

samwyse wrote:

You might want to look at Plex.
http://www.cosc.canterbury.ac.nz/greg.ewing/python/Plex/
"Another advantage of Plex is that it compiles all of the regular
expressions into a single DFA. Once that's done, the input can be
processed in a time proportional to the number of characters to be
scanned, and independent of the number or complexity of the regular
expressions. Python's existing regular expression matchers do not have
this property. "

Hmm, unfortunately it's still orders of magnitude slower than grep in my
own application that involves matching lots of strings and regexps
against large files (I killed it after 400 seconds, compared to 1.5 for
grep), and that's leaving aside the much longer compilation time (over a
minute).  If the matching was fast then I could possibly pickle the
lexer though (but it's not).

That's funny, the compilation is almost instantaneous for me.
However, I just tested it to several files, the first containing
4875*'a', the rest each twice the size of the previous.  And you're
right, for each doubling of the file size, the match take four times
as long, meaning O(n^2).  156000*'a' would probably take 8 hours.
Here are my results:

compile_lexicon() took 0.0236021580595 secs
test('file-0.txt') took 24.8322969831 secs
test('file-1.txt') took 99.3956799681 secs
test('file-2.txt') took 398.349623132 secs


Sounds like a good strategy would be to find the smallest chunk of the
file that matches can't cross, and iterate your search on units of those
chunks.  For example, if none of your regexes cross line boundaries,
search each line of the file individually.  That may help turn around
the speed degradation you're seeing.


That's what I'm doing.  I've also tried various other things like 
mmapping the file and searching it at once, etc, but almost all of the 
time is spent in the regexp engine so optimizing other things only gives 
marginal improvement.


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


Re: Determining when a file has finished copying

2008-07-10 Thread Manuel Vazquez Acosta
Cameron Simpson wrote:
> On 09Jul2008 15:54, Ethan Furman <[EMAIL PROTECTED]> wrote:
>> The solution my team has used is to monitor the file size.  If the file  
>> has stopped growing for x amount of time (we use 45 seconds) the file is  
>> done copying.  Not elegant, but it works.
> 
> If you know that files appear in sequence (a single serial upload
> process, not multiple uploaders) you can augument this with a check
> that an additional file has started to upload, ergo the current file
> has finished. Of course, only you can decide if this might be relied upon.
> 
> Cheers,

Hum, what about the last file in the sequence?

I think polling file's size maybe a good indicator, as Ethan proposed.

Best regards,
Manuel.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Idiomatic Python to convert list to dict

2008-07-10 Thread craig75
On Jul 10, 10:06 am, James Fassett <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> Simple question really on a best practice. I want to avoid adding
> duplicates to a list.
>
> my_list = ['a', 'b', 'c', 'd', 'e']
> dup_map = {}
> for item in my_list:
>     dup_map[item] = True
>
> # ... sometime later
>
> for complex_dict in large_list:
>     if complex_dict["char"] not in dup_map:
>         my_list.append(complex_dict["char"])
>         dup_map[complex_dict["char"]] = True
>
> For the first part (generating the duplicate map) is there a more
> idiomatic Python method for flipping the list into a dict?
>
> Is there a better way to achieve the overall objective (as hinted at
> by the above algorithm)?
>
> Thanks in advance for any tips.
>
> James.

The dictionary seems like overkill here because, if I understand
correctly, the only value associated with a key is "True". So in that
case just remove all the code related to the dictionary (and
complex_dict) and you end up with

my_list = ['a', 'b', 'c', 'd', 'e']

# ... sometime later
for char in large_list:
if char not in my_list:
my_list.append(char)


However, as Diez suggests, use a set:

my_list = set(['a', 'b', 'c', 'd', 'e'])
# ... sometime later
for char in large_list:
my_list.add(char)   # will not add duplicates
--
http://mail.python.org/mailman/listinfo/python-list


Re: Idiomatic Python to convert list to dict

2008-07-10 Thread James Fassett
On Jul 10, 6:13 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> > my_list = ['a', 'b', 'c', 'd', 'e']
> > dup_map = {}
> > for item in my_list:
> >     dup_map[item] = True
>
> > # ... sometime later
>
> > for complex_dict in large_list:
> >     if complex_dict["char"] not in dup_map:
> >         my_list.append(complex_dict["char"])
> >         dup_map[complex_dict["char"]] = True
>
> Instead of a dict, use a set. It's immediatly contructable from my_list,
> and better suited for the task anyway.

Cheers,

I rewrote it similar to:

dup_map = set(['a', 'b', 'c', 'd', 'e'])

# ... sometime later

for complex_dict in large_list:
dup_map.add(complex_dict["char"])

my_list = list(dup_map)

That is a little nicer. Thanks again,
James.
--
http://mail.python.org/mailman/listinfo/python-list


Re: error when porting C code to Python (bitwise manipulation)

2008-07-10 Thread MRAB
On Jul 10, 4:56 am, Jordan <[EMAIL PROTECTED]> wrote:
> I am trying to rewrite some C source code for a poker hand evaluator
> in Python.  Putting aside all of the comments such as just using the C
> code, or using SWIG, etc.  I have been having problems with my Python
> code not responding the same way as the C version.
>
> C verison:
>
> unsigned find_fast(unsigned u)
> {
>     unsigned a, b, r;
>     u += 0xe91aaa35;
>     u ^= u >> 16;
>     u += u << 8;
>     u ^= u >> 4;
>     b  = (u >> 8) & 0x1ff;
>     a  = (u + (u << 2)) >> 19;
>     r  = a ^ hash_adjust[b];
>     return r;
>
> }
>
> my version (Python, hopefully ;)):
>
> def find_fast(u):
>     u += 0xe91aaa35
>     u ^= u >> 16
>     u += u << 8
>     u ^= u >> 4
>     b  = (u >> 8) & 0x1ff
>     a  = (u + (u << 2)) >> 19
>     r  = a ^ hash_adjust[b]
>     return r
>
> As far as I understand the unsigned instructions in C just increase
> amount of bytes the int can hold, and Python automatically converts to
> longs which have infinite size when necessary, so I am not sure why I
> am getting different results.
>
> I assume that I am missing something fairly simple here, so help a
> n00b out if you can :)
>
> Thanks in advance,
>
> jnb

You want to restrict the values to 32 bits. The result of + or << may
exceed 32 bits, so you need to mask off the excess bits afterwards.

def find_fast(u):
mask = 0x
u  = (u + 0xe91aaa35) & mask
u ^= u >> 16
u  = (u + (u << 8)) & mask # can get away with only 1 mask here
u ^= u >> 4
b  = (u >> 8) & 0x1ff
a  = ((u + (u << 2)) & mask) >> 19 # can get away with only 1 mask
here
r  = a ^ hash_adjust[b]
return r

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


using Python's AST generator for other languages

2008-07-10 Thread eliben
Hello,

I'm building a parser in Python, and while pondering on the design of
my ASTs had the idea to see what Python uses. I quickly got to the
compiler.ast module, and understood it's automatically generated. So I
went to the source, ast.txt and tools/compiler/astgen.py, where I was
this unexpected message:

"""Generate ast module from specification

This script generates the ast module from a simple specification,
which makes it easy to accomodate changes in the grammar.  This
approach would be quite reasonable if the grammar changed often.
Instead, it is rather complex to generate the appropriate code.  And
the Node interface has changed more often than the grammar.
"""

Now, to me the design of the AST in Python looks quite elegant,
especially from the point of view of the AST's user (using Visitors to
walk the AST). And astgen.py looks like a nice approach to generate
tons of boilerplate code.
So, my questions:
1) Is the compiler.ast module really employed during the compilation
of Python into .pyc files ?
2) What is the meaning of the comment in astgen.py ? Are the Python
maintainers unhappy with the design of the AST ?
3) What other approach would be recommended to generate a very
detailed AST hierarchy, if the one in astgen.py is dissapointing ?

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


Fwd: [ANN] Babel 0.9.3 released

2008-07-10 Thread Jeroen Ruigrok van der Werven
- Forwarded message from Christopher Lenz <[EMAIL PROTECTED]> -

From: Christopher Lenz <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: [ANN] Babel 0.9.3 released
Date: Thu, 10 Jul 2008 11:26:23 +0200
X-Mailer: Apple Mail (2.926)

Babel 0.9.3 - Jul 9, 2007
=

We're proud to present the latest release of the Babel: 0.9.3.

Babel is a Python library that provides an integrated collection of
utilities that assist with internationalizing and localizing Python
applications (in particular web-based applications.)

This release contains a number of bugfixes over the 0.9.2 release.

You can download the new release here:

   

Please don't hesitate to report any issues you may find with this
release:

   

For questions, comments and user discussions, please use the Babel
mailing list:

   


What's New:
---
* Fixed invalid message extraction methods causing an
UnboundLocalError.
* Extraction method specification can now use a dot instead of the
colon to separate module and function name (ticket #105).
* Fixed message catalog compilation for locales with more than two
plural forms (ticket #95).
* Fixed compilation of message catalogs for locales with more than
two plural forms where the translations were empty (ticket #97).
* The stripping of the comment tags in comments is optional now and
is done for each line in a comment.
* Added a JavaScript message extractor.
* Updated to CLDR 1.6.
* Fixed timezone calculations when formatting datetime and time
values.
* Added a `get_plural` function into the plurals module that returns
the correct plural forms for a locale as tuple.
* Added support for alias definitions in the CLDR data files, meaning
that the chance for items missing in certain locales should be greatly
reduced (ticket #68).


Acknowledgments

A big thank you to everyone who tried Babel and provided feedback,
reported bugs, and/or contributed patches!


Cheers,
Chris
--
Christopher Lenz
  cmlenz at gmx.de
  http://www.cmlenz.net/


- End forwarded message -

-- 
Jeroen Ruigrok van der Werven  / asmodai
イェルーン ラウフロック ヴァン デル ウェルヴェン
http://www.in-nomine.org/ | http://www.rangaku.org/ | GPG: 2EAC625B
No man is good enough to govern another man without the other's consent...
--
http://mail.python.org/mailman/listinfo/python-list

Re: Python and decimal character entities over 128.

2008-07-10 Thread Manuel Vazquez Acosta
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

[EMAIL PROTECTED] wrote:
> Some web feeds use decimal character entities that seem to confuse
> Python (or me). For example, the string "doesn't" may be coded as
> "doesn’t" which should produce a right leaning apostrophe.
> Python hates decimal entities beyond 128 so it chokes unless you do
> something like string.encode('utf-8'). Even then, what should have
> been a right-leaning apostrophe ends up as "’". The following script
> does just that. Look for the string "The Canuck iPhone: Apple doesnâ
> €™t care" after running it.
> 
> # coding: UTF-8
> import feedparser
> 
> s = ''
> d = feedparser.parse('http://feeds.feedburner.com/Mathewingramcom/
> work')
> title = d.feed.title
> link = d.feed.link
> for i in range(0,4):
> title = d.entries[i].title
> link = d.entries[i].link
> s += title +'\n' + link + '\n'
> 
> f = open('c:/x/test.txt', 'w')
> f.write(s.encode('utf-8'))
> f.close()
> 
> This useless script is adapted from a "useful" script. Its only
> purpose is to ask the Python community how I can deal with decimal
> entities > 128. Thanks in advance, Bill
> 
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

This is a two-fold issue: encodings/charsets and entities. Encodings are
a way to _encode_ charsets to a sequence of octets. Entities are a way
to avoid a (harder) encoding/decoding process at the expense of
readability: when you type #8217; no one actually see the intended
character, but those are easily encoded in ascii.

When dealing with multiples sources of information, like your script may
be, I always include a middleware of normalization to Python's Unicode
Type. Web sites may use whatever encoding they please.

The whole process is like this:
1. Fetch the content
2. Use whatever clue in the contents to guess the encoding used by the
document, e.g Content-type HTTP header; ; , and so on.
3. If none are present, then use chardet to guess for an acceptable decoder.
4. Decode ignoring those character that cannot be decoded.
5. The result is further processed to find entities and "decode" them to
actual Unicode characters. (See below)

You may find these helpful:
http://effbot.org/zone/unicode-objects.htm
http://www.mozilla.org/projects/intl/UniversalCharsetDetection.html
http://www.amk.ca/python/howto/unicode

This is function I have used to process entities:
[code]
from htmlentitydefs import name2codepoint
def __processhtmlentities__(text):
assert type(text) is unicode, "Non-normalized text"
html = []
(buffer, amp, text) = text.partition('&')
while amp:
html.append(buffer)
(entity, semicolon, text) = text.partition(';')
if entity[0] != '#':
if entity in name2codepoint:
html.append(unichr(name2codepoint[entity]))
else:
html.append(int(entity[1:])))
(buffer, amp, text) = text.partition('&')
html.append(buffer)
return u''.join(html)
[/code]


Best regards,
Manuel.
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkh2S+sACgkQI2zpkmcEAhil6gCgkAnRE4s5b8oQHamk6utkbAl7
m+YAoIZH2/u73hDcs0G/u294use27v17
=mXuK
-END PGP SIGNATURE-
--
http://mail.python.org/mailman/listinfo/python-list

Re: error when porting C code to Python (bitwise manipulation)

2008-07-10 Thread Jordan
On Jul 10, 1:35 pm, MRAB <[EMAIL PROTECTED]> wrote:
> On Jul 10, 4:56 am, Jordan <[EMAIL PROTECTED]> wrote:
>
>
>
> > I am trying to rewrite some C source code for a poker hand evaluator
> > in Python.  Putting aside all of the comments such as just using the C
> > code, or using SWIG, etc.  I have been having problems with my Python
> > code not responding the same way as the C version.
>
> > C verison:
>
> > unsigned find_fast(unsigned u)
> > {
> >     unsigned a, b, r;
> >     u += 0xe91aaa35;
> >     u ^= u >> 16;
> >     u += u << 8;
> >     u ^= u >> 4;
> >     b  = (u >> 8) & 0x1ff;
> >     a  = (u + (u << 2)) >> 19;
> >     r  = a ^ hash_adjust[b];
> >     return r;
>
> > }
>
> > my version (Python, hopefully ;)):
>
> > def find_fast(u):
> >     u += 0xe91aaa35
> >     u ^= u >> 16
> >     u += u << 8
> >     u ^= u >> 4
> >     b  = (u >> 8) & 0x1ff
> >     a  = (u + (u << 2)) >> 19
> >     r  = a ^ hash_adjust[b]
> >     return r
>
> > As far as I understand the unsigned instructions in C just increase
> > amount of bytes the int can hold, and Python automatically converts to
> > longs which have infinite size when necessary, so I am not sure why I
> > am getting different results.
>
> > I assume that I am missing something fairly simple here, so help a
> > n00b out if you can :)
>
> > Thanks in advance,
>
> > jnb
>
> You want to restrict the values to 32 bits. The result of + or << may
> exceed 32 bits, so you need to mask off the excess bits afterwards.
>
> def find_fast(u):
>     mask = 0x
>     u  = (u + 0xe91aaa35) & mask
>     u ^= u >> 16
>     u  = (u + (u << 8)) & mask # can get away with only 1 mask here
>     u ^= u >> 4
>     b  = (u >> 8) & 0x1ff
>     a  = ((u + (u << 2)) & mask) >> 19 # can get away with only 1 mask
> here
>     r  = a ^ hash_adjust[b]
>     return r
>
> HTH

Well, I guess there are two problemsthe masking and the fact the
in C it seems to for some reason overflow and become a negative
valuestill not sure why it does itSo the code with just
masking doesn't work, you still need some sort of weird inversion like
the ~(0x - u).weird

anyone?

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


Re: Weird lambda rebinding/reassignment without me doing it

2008-07-10 Thread Terry Reedy



David C. Ullrich wrote:
In article 
<[EMAIL PROTECTED]>,

 ssecorp <[EMAIL PROTECTED]> wrote:


I am never redefining the or reassigning the list when using validate
but since it spits the modified list back out that somehow means that
the modified list is part of the environment and not the old one.
i thought what happend inside a function stays inside a function
meaning what comes out is independent of what comes in.
Meaning if I want the list I send as a parameter to the function I
have to do x = func(x) and not just func(x) and x is magically what
comes out of func().


A function cannot modify the value of a global variable


Yes it can.
>>> a=[]
>>> def f():
a.append('yes I can')

>>> f()
>>> a
['yes I can']


(unless it specifies "global"). It doesn't reassign anything.


The statement 'global a' would allow f to *rebind* the global *name* 
'a'.  The word 'variable' should almost not be used in discussing Python 
since it is often unclear whether it refers to a name (or collection 
slot) or an object bound thereto.



But in the functions below you're not reassigning a variable,
you're _modifiying_ an object. A function _can_ modify an
object you pass to it:


It can modify any mutable object it can access.


Doesnt Python have closure or that isnt what this is about?


Python does have closures.  This is not about that.


def validate(placed):
student = round(random.random()*401)
if student in placed:
return validate(placed)
else:
placed.append(student)
return student, placed


Delete this. It is redundant with the below.


def val(placed):
student = round(random.random()*401)
if student in placed:
return validate(placed)
else:
placed.append(student)
return student


I believe this is equivalent to

def addval(placed):
  while True:
student = round(random.random()*401)
if student not in placed:
  break
  placed.append(student)
  return student

While this avoids the indefinite recursion depth problem, it does not 
avoid the indefinite time problem.  Use random.shuffle, or write your 
own version if doing this for practice.  Also consider removing the 
return statement unless you actually directly use the added value.  It 
is easier to remember that addval mutates 'placed' without the return.



g = lambda x:validate(x)


This is doubly diseased.

First, never write a 'name = lambda...' statement since it is equivalent 
to a def statement except that the resulting function object lacks a 
proper .funcname attribute.  The above only trivially abbreviates

  def g(x): return validate(x)
by 3 characters.  Another reason is that the lambda form somehow more 
often tricks people into the next mistake .


Second, never write a function (with either def or lambda) that simply 
returns a function of the argument(s).  The wrapping does nothing!  This 
is a waste of time and space for both you and the interpreter.  The 
above is functionally equivalent to

  g = validate
and if you want that, you could name the function 'g' when you define it.


l=[]
In some fonts, 'l' and '1' are nearly identical; please use something 
else for public code, which you made this to be by posting it;-)



for x in range(1,10):

g(l)

As said, the 'g' wrapper is useless.
   addval(l)

Hope this helps.

Terry Jan Reedy

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


Re: Changing self: if self is a tree how to set to a different self

2008-07-10 Thread Terry Reedy



Bart Kastermans wrote:

I am playing with some trees.  In one of the procedures I wrote
for this I am trying to change self to a different tree.  A tree
here has four members (val/type/left/right).  I found that self = SS
does not work; I have to write self.val = SS.val and the same for
the other members (as shown below).  Is there a better way to do this?

In the below self is part of a parse tree, F is the parse tree of a
function f with argument x.  If a node in the parse tree is labelled
f, we should replace it by the parse tree for the function f, F, with
the remainder of the tree substituted for the input variable for the
function f, here x.

def elimF (self):
if self.val == "f":
SS = F.copy ()
SS.subst ('x', self.left)
self.val = SS.val# from here: set self to be SS
self.type = SS.type
self.left = SS.left
self.right = SS.right# completed: set self to be SS


If you examine nodes from their parent, I believe you can do the 
substitution in one step. Something like:


for slot,child in ( ('left',self.left), ('right',self.right) ):
  if child is not None:
if child.val == 'f':
  setattr(self, slot, F.copy().subst('x', child.left))
child.elimF

where .subst returns the modified tree.



if self.left != None:# iterate onward, inf recursion if f
 # appears.  Would need a check in
 # real version
self.left.elimF ()
if self.right != None:
self.right.elimF ()


Terry Jan Reedy

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


Re: sort(cmp=func)

2008-07-10 Thread norseman


Tobiah wrote:

I have a list of objects that generate code.  Some
of them depend on others being listed first, to 
satisfy dependencies of others.


I wrote a cmp function something like this:

def dep_cmp(ob1, ob2):

if ob1.name in ob2.deps:
return -1
else:
return 1

I also tried returning 0 when there were no dependency
relationships between the objects.

This failed, because every object was not compared with
ever other.  I imagine that this is because sort assumes
that if a > b and b > c, then a > c.  In my case, this
isn't really true.  If a is not dependent on b, and
b is not dependent on c, that doesn't mean that a is not
dependent on c.

Is there a better way?

Thanks

Tobiah
** Posted from http://www.teranews.com **
--
http://mail.python.org/mailman/listinfo/python-list



==

If you are running Linux take a look at 
/lib/modules/[kernel-ver]/modules.dep


In other words, to create a list like:
this module:  depends on these
module-a  module-f, module-g

copy ALL modules to a scratch directory
ls -1 | sort >file.lst
(or dir /b/l | sort >file.lst)
concept:
#outer loop
get a filename from file.lst
create a list of all imports in it
compare filename, in turn, to each line in file
if it finds itself loop
#inner loop
otherwise open module and create 2nd list of all imports in this file
compare each item in list one to each item in list two and output
  one line for each match to imports1.lst, format:
 module name needing, module name wanted  (filenames not def names)

#outer loop2
get a line from dep1.lst
parse to f1=first-name, f2=second-name
create list of imports for f2
#inner loop2
check each import of f2 against name of f1
if there is a match, print  Circular conflict with: f1 and f2 to
  problems.lst

A sort on second filename on line combined with a visual inspection will 
often help point out any 3-way (or more) circulars.

(a needs b needs c needs a)  No guarantees, though.

You will have to resolve circulars yourself. Usually involves changing 
code.  If A needs output from B to run and B needs output from A to run 
you have an obvious circular. Rewrite/fix the code.


Perhaps make a lib file of all the imports for the project.  (Which is 
the preferred practice.)  Load it with the main logic. Thus none will be 
run until all are present. Then eliminate whatever circular nonsense 
still exists...


If problems.lst is empty all that needs to be done is for each import to 
check the dep1.lst and first load the second filename on the line for 
each first-name matching module wanted.  For a given first filename not 
being in a circular, manual ordering of the dep1.lst can be done if 
needed.  (Perhaps f1,a1:f1,b1:f1,c1  needs to be f1,c1:f1,a1:f1,b1 ? 
Simply because descriptive alpha name sort winds up being wrong load 
sequence.)


Just because a piece of code can be reused does not mean it is 
generically practical to do so.  The why of this is your current problem.


My first reaction to your original post is that you inherited something 
and are setting about cleaning it up so you can work on it.



Best of luck. Succeed and you get to claim another T-shirt. :)


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


Re: Smal question

2008-07-10 Thread Terry Reedy



Bruno Desthuilliers wrote:

Hans Müller a écrit :

Hello group,

I have some scripts sharing some common functions.
So what I'd like to have is a modern include.
Of course python does not have (with good reasons) no include statement.
But I'm too lazy to create a module which has to be installed into the 
interpreter for some functions I need to share in a project.


What do you mean "installed in the interpreter" ?


He probably meant either compiled into the interpreter or installed in 
the interpreter directory, not knowing that one can import from the 
project directory, and that the project directory gets added to the 
front of sys.path (as '.').


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


Re: Doubts about how implementing asynchronous timeouts through a heap

2008-07-10 Thread Josiah Carlson
On Jul 9, 4:13 am, "Giampaolo Rodola'" <[EMAIL PROTECTED]> wrote:
> Hi,
> I'm trying to implement an asynchronous scheduler for asyncore to call
> functions at a later time without blocking the main loop.
> The logic behind it consists in:
>
> - adding the scheduled functions into a heapified list
> - calling a "scheduler" function at every loop which checks the
> scheduled functions due to expire soonest
>
> Note that, by using a heap, the first element of the list is always
> supposed to be the one with the lower timeout.
> Here's the code I wrote:
>
> <--- snippet --->
> import heapq
> import time
> import sys
>
> delayed_map = []
>
> class delayed_call:
>     """Calls a function at a later time.
>
>     The instance returned is an object that can be used to cancel the
>     scheduled call, by calling its cancel() method.
>     It also may be rescheduled by calling delay() or reset()} methods.
>     """
>
>     def __init__(self, delay, target, *args, **kwargs):
>         """
>         - delay: the number of seconds to wait
>         - target: the callable object to call later
>         - args: the arguments to call it with
>         - kwargs: the keyword arguments to call it with
>         """
>         assert callable(target), "%s is not callable" %target
>         assert sys.maxint >= delay >= 0, "%s is not greater than or
> equal " \
>                                            "to 0 seconds" % (delay)
>         self.__delay = delay
>         self.__target = target
>         self.__args = args
>         self.__kwargs = kwargs
>         # seconds from the epoch at which to call the function
>         self.timeout = time.time() + self.__delay
>         self.cancelled = False
>         heapq.heappush(delayed_map, self)
>
>     def __le__(self, other):
>         return self.timeout <= other.timeout
>
>     def active(self):
>         """Return True if this scheduler has not been cancelled."""
>         return not self.cancelled
>
>     def call(self):
>         """Call this scheduled function."""
>         self.__target(*self.__args, **self.__kwargs)
>
>     def reset(self):
>         """Reschedule this call resetting the current countdown."""
>         assert not self.cancelled, "Already cancelled"
>         self.timeout = time.time() + self.__delay
>         if delayed_map[0] is self:
>             heapq.heapify(delayed_map)
>
>     def delay(self, seconds):
>         """Reschedule this call for a later time."""
>         assert not self.cancelled, "Already cancelled."
>         assert sys.maxint >= seconds >= 0, "%s is not greater than or
> equal " \
>                                            "to 0 seconds" %(seconds)
>         self.__delay = seconds
>         self.reset()
>
>     def cancel(self):
>         """Unschedule this call."""
>         assert not self.cancelled, "Already cancelled"
>         del self.__target, self.__args, self.__kwargs
>         if self in delayed_map:
>             if delayed_map[0] is self:
>                 delayed_map.remove(self)
>                 heapq.heapify(delayed_map)
>             else:
>                 delayed_map.remove(self)
>         self.cancelled = True
>
> def fun(arg):
>     print arg
>
> a = delayed_call(0.6, fun, '0.6')
> b = delayed_call(0.5, fun, '0.5')
> c = delayed_call(0.4, fun, '0.4')
> d = delayed_call(0.3, fun, '0.3')
> e = delayed_call(0.2, fun, '0.2')
> f = delayed_call(0.1, fun, '0.1')
>
> while delayed_map:
>     now = time.time()
>     while delayed_map and now >= delayed_map[0].timeout:
>         delayed = heapq.heappop(delayed_map)
>         try:
>             delayed.call()
>         finally:
>             if not delayed.cancelled:
>                 delayed.cancel()
>     time.sleep(0.01)
> 
>
> Here comes the questions.
> Since that the timeouts of the scheduled functions contained in the
> list can change when I reset() or cancel() them I don't know exactly
> *when* the list needs to be heapified().
> By doing some tests I came to the conclusion that I need the heapify()
> the list only when the function I reset() or cancel() is the *first of
> the list* but I'm not absolutely sure about it.
> When do you think it would be necessary calling heapify()?
> I wrote a short test suite which tests the code above and I didn't
> notice strange behaviors but since that I don't know much about the
> logic behind heaps I'd need some help.
> Thanks a lot in advance.
>
> --- Giampaolohttp://code.google.com/p/pyftpdlib/

According to a quick scan, there is some inefficiencies with your
code.  In particular, you don't need to re-heapify if the item you
need to remove is the first item; you only need to heappop().

In any case, if the Python standard library heapq module supported non-
lists as containers, then the pair heap implementation I wrote a
couple years ago would be perfect for this particular task.  Because
of the rewriting of heapq in C, without a bit of monkeypatching, we
can't re-use that implementation (which offered insert/remove o

Re: Elisp Lesson on file processing (make downloadable copy of a website)

2008-07-10 Thread Sashi
On Jul 6, 4:05 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> In this week i wrote a emacs program and tutorial that does archiving
> a website for offline reading.
> (Seehttp://xahlee.org/emacs/make_download_copy.html)

Why not use wget or curl?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Loading just in time

2008-07-10 Thread samwyse
On Jul 10, 9:45 am, "D'Arcy J.M. Cain" <[EMAIL PROTECTED]> wrote:
> I am trying to create a utility module that only loads functions when
> they are first called rather than loading everything.  I have a bunch
> of files in my utility directory with individual methods and for each I
> have lines like this in __init__.py:
>
> def calc_tax(*arg, **name):
>     from calc_tax import calc_tax as _func_
>     calc_tax = _func_
>     return _func_(*arg, **name)

This doesn't do what you think.  The line "calc_tax = _func_" is
probably modifying a local variable that is then thrown away.  I've
got a slightly different (simpler) version to illustrate:

=== main.py ===

def calc_tax(*arg, **name):
from calc_tax import calc_tax as _func_
#global calc_tax
calc_tax = _func_
print '_func_ is', repr(_func_)
print 'calc_tax is', repr(calc_tax)
return _func_(*arg, **name)

print 'before: calc_tax is', repr(calc_tax)
result = calc_tax()
print 'after: calc_tax is', repr(calc_tax)

=== calc_tax.py ===

def calc_tax(*arg, **name):
return 42

=== end of files ===

Running main.py gives this:

  before: calc_tax is 
  _func_ is 
  calc_tax is 
  after: calc_tax is 

Note that the value of calc_test is the same in the first and last
lines.

If you uncomment the line "#global calc_tax" and run it again, you get
this:

  before: calc_tax is 
  _func_ is 
  calc_tax is 
  after: calc_tax is 

Interestingly, neither version gives me a TypeError, no matter how
many times I call calc_tax.

(BTW, you might want to look up Memoization; it's very similar to what
you want to do, and might give you a way to more efficiently code
things.)
--
http://mail.python.org/mailman/listinfo/python-list


Problems Returning an HTTP 200 Ok Message

2008-07-10 Thread Guy Davidson
Hi Folks,

I'm having some issues with an small socket based server I'm writing,
and I was hoping I could get some help.

My code (attached below) us supposed to read an HTTP Post message
coming from a power meter, parse it, and return a proper HTTP 200 Ok
message. The problem is that the socket fails to send the entire
message as one message, creating a fragmented message which the power
meter then fails to read and accept.

Is there any way to force the socket to send the entire message at
once? Am I doing anything wrong? Is there an easier way to implement
this functionality?

Thanks,

Guy Davidson

Code:

# server application to read data from power meter
# listens on port 80
# receives HTTP POST messages

# author: Maria Kazandjieva <[EMAIL PROTECTED]>, Guy Davidson
<[EMAIL PROTECTED]>
# updated: July 7th, 2008: staring to add database writing
functionality

import time
import datetime #datetime for response message
from socket import * #socket module
import DataPacket #local file with data packet class
import library
import MySQLdb

#formats a proper response message

myHost = ""
myPort = 80

#response = "HTTP/1.1 200 OK\r\nDate: Fri, 20 June 2008 20:40:34 GMT\r
\nServer:SING\r\nX-Powered-By: maria\r\nContent-Length: 7\r
\nConnection:close\r\nContent-Type: text/html\r\n\r\n[0!:20]\n"

#initializing the socket:
s = socket(AF_INET, SOCK_STREAM)# create a TCP socket
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) # allow port reuse? I hope
s.bind((myHost, myPort))# bind it to the server port
s.listen(50)# allow 50 simultaneous
pending connections
print('Socket initialized')

#intializing the database
db = MySQLdb.connect(host='localhost', user='powernet',
passwd='jplicks', db='test')
cursor = db.cursor()
print('Database cursor intialized')

while 1:
print('In while 1')
connection, address = s.accept()# connection is a new socket
print('Accepted a connection')

# TODO:
# verify connection is from powernet so we don't get random
connections to port 80

# best way to get the data would be:
# read until we get all the headers
# parse Content header to get length of data
# read for that many additional bytes

while 1:
print('In while 1 2')
data = connection.recv(4096)# receive up to 4K bytes
print('First set of data receieved:')
#print(data)
'''
if data:
print data
#connection.send(response)  #send response; OK 200 
HTTP message
else:
break
'''
if data:
# second read should recieve the rest of the packet
# finding how long the content is
length = data.find('Content-Length: ')
print('Index of "Content-Length" = ' + str(length))
length = int(data[length+16:length+18])
# adding the bytes from the "Content-Type section:"
length += 51

data += connection.recv(length)
print('Second set of data received:')
print(data)

if data:
#print('Found data:')
#print data #print the string data
packet = DataPacket.DataPacket(data) #use the 
string data to
create a DataPacket object
print(packet) #print the contents of the 
object, I hope :)

#we have a packet, let's write it to the mysql 
DB
packet.mysql_insert(cursor)

#connection.send(response)  #send response; 
OK 200 HTTP
message
print('Sending response')
response = library.response_message() #get the 
response message
totalsent = 0 #initialize a variable to track 
how much we've sent
so far
while totalsent < len(response): #while we 
haven't sent everything
sent = 
connection.send(response[totalsent:]) #send whatever we
can starting where we stopped
totalsent += sent #increment the count 
of how much we've sent.

else:
print('No data')
break

else:
print('No data')
break
--
http://mail.python.org/mailman/listinfo/python-list


Re: handling unexpected exceptions in pdb

2008-07-10 Thread R. Bernstein
Simon Bierbaum <[EMAIL PROTECTED]> writes:

> Hi all,
>
> I'm in an interactive session in pdb, debugging my code using
> pdb.runcall. Somewhere, an exception is raised and lands uncaught on
> stdout. Is there any way of picking up this exception and at least
> read the full message, or even accessing its stack trace to determine
> where exactly within the one line I just executed it was raised? This
> is where I'm stuck:
>
>> /usr/local/apache2/bin/Model/Database.py(287)disconnect()
> (Pdb) n
> FlushError: FlushErr...perly.",)
>> /usr/local/apache2/bin/Model/Database.py(287)disconnect()
> (Pdb) import sys
> (Pdb) sys.last_traceback
> *** AttributeError: 'module' object has no attribute 'last_traceback'
>
> Thanks, Simon

I think basically you want runcall to be wrapped in a try block. So in pdb.py
instead of:

def runcall(*args, **kwds):
return Pdb().runcall(*args, **kwds)


Change with:

def runcall(*args, **kwds):
p=Pdb()
try:
  return p.runcall(*args, **kwds)
except: 
  traceback.print_exc()
  print "Uncaught exception. Entering post mortem debugging"
  t = sys.exc_info()[2]
  p.interaction(t.tb_frame,t)
  print "Post mortem debugger finished."
  return None

Code like this appears near the bottom of the pdb.py file. If that
works, you may want to file a bug Python report to change pdb. Also
note that one may want to do the same thing on run() and runeval()

But also if this does what you want, please file a feature request to pydb:
  http://sourceforge.net/tracker/?func=add&group_id=61395&atid=497162

and I'll probably make it happen in the next release.

This is the first time I've heard of anyone using runcall.

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


Re: sort(cmp=func)

2008-07-10 Thread Tobiah
On Wed, 09 Jul 2008 20:58:32 -0700, Daniel Fetchinson wrote:

>> I have a list of objects that generate code.  Some
>> of them depend on others being listed first, to
>> satisfy dependencies of others.
>>
>> I wrote a cmp function something like this:
>>
>> def dep_cmp(ob1, ob2):
>>  
>>  if ob1.name in ob2.deps:
>>  return -1
>>  else:
>>  return 1
>>
>> I also tried returning 0 when there were no dependency
>> relationships between the objects.
>>
>> This failed, because every object was not compared with
>> ever other.  I imagine that this is because sort assumes
>> that if a > b and b > c, then a > c.  In my case, this
>> isn't really true.  If a is not dependent on b, and
>> b is not dependent on c, that doesn't mean that a is not
>> dependent on c.
>>
>> Is there a better way?
> 
> It's only meaningful to talk about sorting if your particular
> definition of "<" is transitive. I.e. a < b and b < c should imply a <
> c. If this is not satisfied, it doesn't make sense to sort according
> to your "<" definition. It's just not a well-defined operation and no
> trick will make it work.

There is a meaningful relationship here however.  Basically, some
items must precede some other items.  There are more than one
correct orders however.  Here was my eventual solution. I had to 
compare each item with each other item, swapping on failed 
dependencies.  Recursion simplified this greatly:

def dep_sort(tables):

for this_index in range(len(tables)):
this_table = tables[this_index]
for prev_index in range(this_index):
prev_table = tables[prev_index]
if this_table.name in prev_table.deps:
tables[prev_index] = this_table
tables[this_index] = prev_table
dep_sort(tables)
return

** Posted from http://www.teranews.com **
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problems Returning an HTTP 200 Ok Message

2008-07-10 Thread Joshua Kugler
Guy Davidson wrote:

> Hi Folks,
> 
> I'm having some issues with an small socket based server I'm writing,
> and I was hoping I could get some help.
> 
> My code (attached below) us supposed to read an HTTP Post message
> coming from a power meter, parse it, and return a proper HTTP 200 Ok
> message. The problem is that the socket fails to send the entire
> message as one message, creating a fragmented message which the power
> meter then fails to read and accept.
> 
> Is there any way to force the socket to send the entire message at
> once? Am I doing anything wrong? Is there an easier way to implement
> this functionality?

I don't have a solution to the problem presented, per se, but you might have
an easier time implementing this if you use SimpleHTTPServer or
CGIHTTPServer modules.  This will take care of all the socket listening,
and a lot of other low-level details.

Hope that helps!

j

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


Re: Python with Ecmascript

2008-07-10 Thread Alan Isaac

Daniel Fetchinson wrote:

Is there a way to do similar things on linux?


NJSModule?
http://en.wikipedia.org/wiki/NJS

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


parsing incoming emails

2008-07-10 Thread Ahmed, Shakir
HI,

 

I am working on a project where I need to parse incoming emails
(Microsoft outlook)  with a specific subject into an excel file or a
Microsoft access table. 

 

I am using python for my GIS works but not sure how I can use python
script here to work with Microsoft outlook email. 

 

Any help or idea is highly appreciated.

 

Thanks

SA

 

 

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

Re: Problems Returning an HTTP 200 Ok Message

2008-07-10 Thread samwyse
On Jul 10, 1:50 pm, Guy Davidson <[EMAIL PROTECTED]> wrote:
> Hi Folks,
>
> I'm having some issues with an small socket based server I'm writing,
> and I was hoping I could get some help.
>
> My code (attached below) us supposed to read an HTTP Post message
> coming from a power meter, parse it, and return a proper HTTP 200 Ok
> message. The problem is that the socket fails to send the entire
> message as one message, creating a fragmented message which the power
> meter then fails to read and accept.
>
> Is there any way to force the socket to send the entire message at
> once? Am I doing anything wrong? Is there an easier way to implement
> this functionality?

By 'message', do you mean a single IP datagram?  In general, the
answer is no.  Each call to 'connection.send()' will (in general, see
the next paragraph) transmit as much data as will fit into a single IP
datagram, given the current MTU for the transmission circuit.  The
fact that you're calling it in a loop indicates that the data being
sent may be larger than will fit into a datagram.

Or, by 'message', do you mean a single TCP segment?  Again, the answer
is no.  Your network stack will try to make the TCP segments the right
size to fit within a single IP datagram, leading to the same result as
above.

>From your description, I get the feeling that your power meter has a
broken network stack, and you're trying to program around it.  You
need to repair the meter.
--
http://mail.python.org/mailman/listinfo/python-list


Re: parsing incoming emails

2008-07-10 Thread Terry Reedy


I am working on a project where I need to parse incoming emails 
(Microsoft outlook)  with a specific subject into an excel file or a 
Microsoft access table.


You should be able to give Outlook a rule to call a program (your Python 
one) when the subject matches whatever.


From Python, use the mail module to parse and the windows extensions to 
access Excel and perhaps Access.  There should also be a DBapi extension 
for accessing Access through sql.  Search c.l.p archives, the web, or 
wait for others to post.


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


Can this program be shortened? Measuring program-length?

2008-07-10 Thread r.e.s.
Can the following program be shortened? ...

def h(n,m):
 E=n,
 while (E!=())*m>0:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n
 return n
h(9,9)

Note:  Although it halts eventually in principle, this program can't be
expected to terminate on any machine in the universe, as it computes a 
number larger than Graham's number -- assuming Python is extended (if
necessary?) to access unbounded storage.

Besides using one-letter names and no unneeded whitespace, can something
more be done to shorten it?  ("Obfuscating" the code would be okay.) 

Also, I'm not really sure how best to measure a program's length, but 
this one is now 98 bytes long (or 102 bytes, depending on how newlines 
are handled).  Is there a better measure of program-length?

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


Re: error when porting C code to Python (bitwise manipulation)

2008-07-10 Thread Harald Luessen
On Thu, 10 Jul 2008 Jordan wrote:

>On Jul 10, 1:35 pm, MRAB <[EMAIL PROTECTED]> wrote:
>> On Jul 10, 4:56 am, Jordan <[EMAIL PROTECTED]> wrote:
>>
>>
>>
>> > I am trying to rewrite some C source code for a poker hand evaluator
>> > in Python.  Putting aside all of the comments such as just using the C
>> > code, or using SWIG, etc.  I have been having problems with my Python
>> > code not responding the same way as the C version.
>>
>> > C verison:
>>
>> > unsigned find_fast(unsigned u)
>> > {
>> >     unsigned a, b, r;
>> >     u += 0xe91aaa35;
>> >     u ^= u >> 16;
>> >     u += u << 8;
>> >     u ^= u >> 4;
>> >     b  = (u >> 8) & 0x1ff;
>> >     a  = (u + (u << 2)) >> 19;
>> >     r  = a ^ hash_adjust[b];
>> >     return r;
>>
>> > }
>>
>> > my version (Python, hopefully ;)):
>>
>> > def find_fast(u):
>> >     u += 0xe91aaa35
>> >     u ^= u >> 16
>> >     u += u << 8
>> >     u ^= u >> 4
>> >     b  = (u >> 8) & 0x1ff
>> >     a  = (u + (u << 2)) >> 19
>> >     r  = a ^ hash_adjust[b]
>> >     return r
>>
>> > As far as I understand the unsigned instructions in C just increase
>> > amount of bytes the int can hold, and Python automatically converts to
>> > longs which have infinite size when necessary, so I am not sure why I
>> > am getting different results.
>>
>> > I assume that I am missing something fairly simple here, so help a
>> > n00b out if you can :)
>>
>> > Thanks in advance,
>>
>> > jnb
>>
>> You want to restrict the values to 32 bits. The result of + or << may
>> exceed 32 bits, so you need to mask off the excess bits afterwards.
>>
>> def find_fast(u):
>>     mask = 0x
>>     u  = (u + 0xe91aaa35) & mask
>>     u ^= u >> 16
>>     u  = (u + (u << 8)) & mask # can get away with only 1 mask here
>>     u ^= u >> 4
>>     b  = (u >> 8) & 0x1ff
>>     a  = ((u + (u << 2)) & mask) >> 19 # can get away with only 1 mask
>> here
>>     r  = a ^ hash_adjust[b]
>>     return r
>>
>> HTH
>
>Well, I guess there are two problemsthe masking and the fact the
>in C it seems to for some reason overflow and become a negative
>valuestill not sure why it does itSo the code with just
>masking doesn't work, you still need some sort of weird inversion like
>the ~(0x - u).weird
>
>anyone?

In C unsigned can not be negative. Why do you believe 
the numbers are negative? If your debugger is telling 
you this thow away the debugger and use printf. 
If printf is telling you this then use the right format.
printf("%u", u); // for unsigned int u
printf("%lu", u); // for unsigned long u
printf("%x", u); 
or
printf("0x%08x", u); // to see u in hex

Harald

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


Re: You, spare time and SyntaxError

2008-07-10 Thread [EMAIL PROTECTED]
On 10 juil, 08:53, [EMAIL PROTECTED] wrote:
> > just... great !-)
>
> Thanks :)

Nope, really, I mean it. To me, there's a clear relation between code,
mathematics and poetry. I've been wanting to write some "code poems"
for a long time now but never managed to get enough time and
inspiration. While there are a couple potential errors in some path of
your code, it's still both (almost) working code and an IMHO nice poem
(ok, it's not Rimbaud or Villon or Appolinaire - pardon my french -
but it's still more than honest poetry).

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


Re: parsing incoming emails

2008-07-10 Thread Michiel Overtoom
Ahmed wrote...

> I am working on a project where I need to parse incoming emails
> (Microsoft outlook)

I'm not sure if you are able to bypass Outlook (and have Python fetch the
mail itself using poplib), but if you are, the following code might be
useful.  I use this to pry apart emails which might contain multiple MIME parts.

from email.Parser import Parser
from rfc822 import parseaddr
import poplib
import smtplib

popserver="pop.site.com"
popuser="[EMAIL PROTECTED]"
poppassword="secret"

# split a message into an header- and body part
def separate(msg):
if isinstance(msg,str):
msg=msg.split('\n')
emptyline=msg.index('')
return msg[:emptyline],msg[emptyline+1:]


# return a certain headerline from the headers
def headerline(header,tag="From: "):
for h in header:
if h.startswith(tag):
return h[len(tag)+1:]
return ""


# enumerate recursively the contents of a MIME message
# remember the first text/plain and text/html part(s) that is found
# also remember if any other parts were found (like attachments)
#
def enummimeparts(msg,extract,level=1,verbose=False):
m=Parser().parsestr(msg)
if m.is_multipart():
if verbose: print '\t'*level,'multipart'
for part in m.get_payload():
enummimeparts(part.as_string(),extract,level+1,verbose)
else:
t=m.get_content_type()
if verbose: print '\t'*level,t
if t=="text/plain":
if not "text/plain" in extract:
headers,body=separate(m.as_string())
extract["text/plain"]='\n'.join(body)
else:
extract["others"]=True
elif t=="text/html":
if not "text/html" in extract:
headers,body=separate(m.as_string())
extract["text/html"]='\n'.join(body)
else:
extract["others"]=True
else:
extract["others"]=True


# extract the first 'text/plain' and 'text/html' mime-parts from a message
def extracttext(msg):
extract={}
enummimeparts(msg,extract)
return
extract.get("text/plain",None),extract.get("text/html",None),extract.get("ot
hers",False)


def processmessage(msgnr):
# get a message from the POP server, extract the parts
response,lines,bytes=pop.retr(msgnr)
msg='\n'.join(lines)
headers,body=separate(lines)
name,fromaddress=parseaddr(headerline(headers,"From:"))
subject=headerline(headers,"Subject:")
logging.info(subject+" ("+fromaddress+")")
(plain,html,others)=extracttext(msg)
# prefer flat text; if not present in the message, fallback to HTML
content (if any)
texttoprocess=""
if plain:
texttoprocess=plain
elif html:
texttoprocess=html
# now do something useful with the text
processtext(texttoprocess)
# delete message from pop server after processing
pop.dele(msgnr)


# connect to the pop server and process all messages
logging.info("Checking pop server '%s', user '%s'" % (popserver,popuser))
pop=poplib.POP3(popserver)
pop.user(popuser)
pop.pass_(poppassword)
stat=pop.stat()
if stat[0]:
for n in range(stat[0]):
processmessage(n+1)
pop.quit()


-- 
"The ability of the OSS process to collect and harness
the collective IQ of thousands of individuals across
the Internet is simply amazing." - Vinod Vallopillil
http://www.catb.org/~esr/halloween/halloween4.html

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


  1   2   >