Re: how to get the thighest bit position in big integers?

2008-10-28 Thread Nick Mellor
On Oct 6, 3:40 am, Gary Herron <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > Hi,
>
> > I'm using python to develop some proof-of-concept code for a
> > cryptographic application. My code makes extended use of python's
> > native bignum capabilities.
>
> > In many cryptographic applications there is the need for a function
> > 'get_highest_bit_num' that returns the position number of the highest
> > set bit of a given integer. For example:
>
> >    get_highest_bit_num( (1 << 159))     == 159
> >    get_highest_bit_num( (1 << 160) - 1) == 159
> >    get_highest_bit_num( (1 << 160))     == 160
>
> How about a binary search?
>
>
>
> >>> from bisect import bisect
> >>> BITS = [1< >>> bisect(BITS, 1<<159)
> 159
> >>> bisect(BITS, 1<<160-1)
> 159
> >>> bisect(BITS, 1<<160)
> 160
>
> I have no clue if this is faster or not.  The comparison function used
> in the search is (potentially) slow, but the search is O(log n) on the
> number of bits rather than O(n), so its worth a test.
>
> If you run timing test, let us know the results.
>
> Gary Herron
>
> > I implemented this the following way:
>
> > def get_highest_bit_num(r):
> >     i = -1
> >     while r > 0:
> >         r >>= 1
> >         i = i + 1
> >     return i
>
> > This works, but it is a very unsatisfying solution, because it is so
> > slow.
>
> > My second try was using the math.log function:
>
> > import math
> > r = (1 << 160) - 1
> > print highest_bit_num(r)              # prints out 159
> > print math.floor(math.log(r, 2))      # prints out 160.0
>
> > We see that math.log can only serve as a heuristic for the highest bit
> > position. For small r, for example r = (1 << 16) - 1, the result from
> > math.log(, 2) is correct, for big r it isn't any more.
>
> > My question to the group: Does anyone know of a non-hackish way to
> > determine the required bit position in python? I know that my two
> > ideas
> > can be combined to get something working. But is there a *better* way,
> > that isn't that hackish?
>
> > cheers,
> > mmg
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
>


The following might be worth a try. It's faster the fewer set bits
there are in the original number, and lightning fast if there are only
one or two:

def get_highest_bit_num(i):
while i>0: highestbit, i = i, i & (i-1)
return highestbit

>>> highestbit(1<<31)
2147483648L
>>> highestbit(1<<4)
16
>>> highestbit(3<<4)
32

Note that it returns the value of the highest bit, not its position.

All it's doing is successively ANDing i with (i-1) until i is zero,
then returning the previous value of i.

It works because i & (i-1) has a useful property: it returns i less
its least significant set bit:

i=6 (binary 110) => i & (i-1)==4 (binary 100)
i=3328 => (binary 1101__) then i & (i-1)==3072 (binary
1100__)

(underscores for readability)

As far as I know there isn't another piece of logic that helps you
extract the most significant bit as simply :-)

Best wishes,

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


Re: How to get high precision timer in python?

2008-10-28 Thread Tim Roberts
"" <[EMAIL PROTECTED]> wrote:

>2008/10/29 James Mills <[EMAIL PROTECTED]>:
>> On Wed, Oct 29, 2008 at 12:14 PM,  <[EMAIL PROTECTED]> wrote:
>>> I use python2.5 in WindowsXP. If using time.time() as timer, it seems
>>
>> On the win32 platform should you not
>> be using time.clock vs. time.time ?
>
>Well, whatelse can I use?

I'm not sure you understood what he was saying.  time.time() and
time.clock() can both be used for elapsed timing, but because of a fluke of
implementation, time.time() is more precise on Linux, and time.clock() is
more precise on Windows.

So, use time.clock().
-- 
Tim Roberts, [EMAIL PROTECTED]
Providenza & Boekelheide, Inc.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the instance reference of an object

2008-10-28 Thread Douglas Alan
Joe Strout <[EMAIL PROTECTED]> writes:

> There are only the two cases, which Greg quite succinctly and
> accurately described above.  One is by value, the other is by
> reference.  Python quite clearly uses by value.

You make a grave error in asserting that there are only two cases.
Algol, for instance, used call-by-name, which is neither call-by-value
or call-by-reference.  There are a number of other evaluation
strategies.  For a primer on the subject see the following Wikipedia
page:

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

CLU used the termed "call-by-sharing" for the evaluation strategy
shared by Python, Lisp, CLU, Java, Ruby, and JavaScript, etc.

It should be noted that the Wikipedia page does not document
"call-by-sharing", in specific and refers to Python's strategy as a
type of call-by-value.  It also notes that call-by-value is not a
single evaluation strategy, but rather a family of evaluation
strategies, and that the version of the strategy used by Java (and
hence Python) shares features with call-by-reference strategies.

Consequently, many people prefer to use a different name from
"call-by-value" for Python/Java/Lisp's strategy in order to avoid
confusion.  In any case, no one can disagree with the fact that the
evaluation strategy used by Python et. al., differs significantly from
the call-by-value evaluation strategy used by C and the like, whatever
you wish to call it.

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


Re: Unpacking byte strings from a file of unknown size

2008-10-28 Thread Mark

> this code python interprets as:
>
>  data = myfile.read(10)
>  for chunk in data:
>  .
>

Aha - now that you put it that way it makes sense.  And thanks to all
who replied - I'll try out the other suggestions too.

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


Graphical object browser

2008-10-28 Thread Jason
Hi,

I've spent all day looking for a graphical object browser for Python
2.5 under Debian GNU/Linux, ie. something I can just drop into my code
with an import and a "browse(my_object)" statement. So far I've only
found intractable GUI toolkits or obsolete, non-functional scripts.

I was wondering if there is a simple tool like this out there
somewhere. Preferably one that doesn't involve me installing a massive
IDE, but I can't really be picky.

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


Re: print_function and unicode_literals cannot be used at the same time?

2008-10-28 Thread Benjamin
On Oct 27, 1:00 pm, Terry Reedy <[EMAIL PROTECTED]> wrote:
> Gabriel Genellina wrote:
> > En Sun, 26 Oct 2008 12:13:08 -0200, Christian Heimes <[EMAIL PROTECTED]>
> > escribió:
>
> >> ?? wrote:
> >>> Any ideas?
> >>>  Code 1:
> >>>  from __future__ import print_function, unicode_literals
> >>> import sys
> >>> print(type('HELLO, WORLD!'), file=sys.stderr)
>
> >> You have to do each future import in a separate line:
>
> >>  >>> from __future__ import unicode_literals
> >>  >>> from __future__ import print_function
> >>  >>> print(type(""), file=sys.stderr)
> >> 
>
> > That's a bug, isn't it? The language reference explicitely allows it:
> >http://docs.python.org/reference/simple_stmts.html#future-statements
>
> Yes, and Benjamin Peterson already submitted a patch because of this 
> thread.http://bugs.python.org/issue4209

It will be fixed in 2.6.1.
--
http://mail.python.org/mailman/listinfo/python-list


[Novice]Installing eric4 with python 2.6

2008-10-28 Thread Saurabh Agrawal
Hi,

I am fairly new to python. Coming from a world of IDEs, I wanted a fancier
one than IDLE. Hence, I downloaded eric4. When I tried to compile it, it
asked for PyQt4, which I again downloaded (exe for Windows XP) and installed
it. It seems to be properly installed as I can invoke "import PyQt4" from
IDLE without any complaints.

However, when I try to install eric4, I am getting the following error:

D:\Python26\eric4-4.2.2a>python install.py
Sorry, please install PyQt4.
Error: Module use of python25.dll conflicts with this version of Python.

So, won't eric4 work with python 2.6? I have searched on the net for this,
but am unable to find any solution.

I am new to python, so please excuse if this has an obvious solution.
Thanks.

OS: Windows XP SP2.

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


Re: How to get high precision timer in python?

2008-10-28 Thread 甜瓜
^_^ Oh!  I did not read the document for time.clock before,
and supposed it was same with time.time().

Thank you very much.

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


Re: How to get high precision timer in python?

2008-10-28 Thread James Mills
2008/10/29 甜瓜 <[EMAIL PROTECTED]>:
> 2008/10/29 James Mills <[EMAIL PROTECTED]>:
>> On Wed, Oct 29, 2008 at 12:14 PM, 甜瓜 <[EMAIL PROTECTED]> wrote:
>>> I use python2.5 in WindowsXP. If using time.time() as timer, it seems
>>
>> On the win32 platform should you not
>> be using time.clock vs. time.time ?
>>
>
> Well, whatelse can I use?

try time.clock

cheers
James

-- 
--
-- "Problems are solved by method"
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to get high precision timer in python?

2008-10-28 Thread 甜瓜
2008/10/29 James Mills <[EMAIL PROTECTED]>:
> On Wed, Oct 29, 2008 at 12:14 PM, 甜瓜 <[EMAIL PROTECTED]> wrote:
>> I use python2.5 in WindowsXP. If using time.time() as timer, it seems
>
> On the win32 platform should you not
> be using time.clock vs. time.time ?
>

Well, whatelse can I use?

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


Re: How to get high precision timer in python?

2008-10-28 Thread James Mills
On Wed, Oct 29, 2008 at 12:14 PM, 甜瓜 <[EMAIL PROTECTED]> wrote:
> I use python2.5 in WindowsXP. If using time.time() as timer, it seems

On the win32 platform should you not
be using time.clock vs. time.time ?

--JamesMills

-- 
--
-- "Problems are solved by method"
--
http://mail.python.org/mailman/listinfo/python-list


How to get high precision timer in python?

2008-10-28 Thread 甜瓜
Howdy,

I use python2.5 in WindowsXP. If using time.time() as timer, it seems
the maximum precision is about 10-12ms. Maybe this is caused by
the time slice defined in process scheduler. However, in my project,
I have to get timer percision up to 1ms. What should I do?  Do I have
to call Win32 API? Or simply promote python thread priority?

Regards,

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


Re: gl Multiple versions of python

2008-10-28 Thread James Mills
On Wed, Oct 29, 2008 at 11:50 AM, Glenn Linderman <[EMAIL PROTECTED]> wrote:
>>> When using multiple versions of Python co-installed on the same system,
>>> what happens with local .pyc files?  If the .py is loaded with a
>>> different version of Python, is the .pyc rebuilt (even if the .py hasn't
>>> changed)?

Worth having a look at virtualenv

> Also for test code, is there a way to test the version of python which is
> executing the code?  Something like
>
> if __name__ == "__main__"


#!/usr/bin/env python

import sys

def main():
   print sys.version
   print sys.subversion
   print sys.hexversion
   print sys.api_version
   print sys.version_info

if __name__ == "__main__":
   main()



2.5.2 (r252:60911, Oct 27 2008, 14:12:15)
[GCC 4.2.4 (CRUX)]
('CPython', 'tags/r252', '60911')
33882864
1013
(2, 5, 2, 'final', 0)


Read the sys documentation for the meaning
of the attributes used aboave.

cheers
James

-- 
--
-- "Problems are solved by method"
--
http://mail.python.org/mailman/listinfo/python-list


Re: Contracts for Python

2008-10-28 Thread alex23
On Oct 29, 3:47 am, "Paulo J. Matos" <[EMAIL PROTECTED]> wrote:
> I am wondering if there is any work on contracts for Python. I could
> only find PEP316, however, I am wondering if there is any official
> support for it already (tools I mean), and if it is or if it will be
> officially supported in any of the next releases of Python.

It's possible to get a simplistic design-by-contract approach without
external libs by using 'assert'.

Here's a modified example from PEP 316:

class circbuf:

def __init__(self, leng):
"""Construct an empty circular buffer."""

# pre
assert leng > 0, "pre: length not positive"

...

# post
assert self.is_empty(), "post: buffer not empty"
assert len(self.buf) == leng, "post: buffer length incorrect"
--
http://mail.python.org/mailman/listinfo/python-list


Re: import foo vs. python -m foo

2008-10-28 Thread Asun Friere
On Oct 29, 7:35 am, Hrvoje Niksic <[EMAIL PROTECTED]> wrote:

> It probably contains buggy code inside "if __name__ == '__main__': ...".

Or the code contains a run-time error?


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


Earn $$$ Without Investment.

2008-10-28 Thread chinu
hai,
i am srinu from india. i am sending a blog url for yours use.


Right side Of The Blog Awsurvey Banner will appear.

click on the banner and get a free signup with 6$ bonus and you will
get more surveys.
once you have completed one survey you will get minimem 4$ and more

left side of the blog home based jobs will appear
click on the ads you will get more details about to choose your job.

you willnot satisfy to choose your job
you will type jobs or sports or jewelery etc on
search box field .and click on search.
then you will find what resuilts are you want.




click on the blog and get more information to choose yours job.

the blog url is:


   http://wealthinonline.blogspot.com/



 goodluck


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


Re: How to get an object's name as a string?

2008-10-28 Thread alex23
On Oct 29, 12:41 am, Shannon Mayne <[EMAIL PROTECTED]> wrote:
> I would like to create objects with algorithmically determined names
> based on other object names and use object names for general algorithm
> input.

The simplest and best option here is to store the objects in a
dictionary with their keys being the generated names.

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


Re: Using threads to quit the main python process

2008-10-28 Thread James Mills
On Wed, Oct 29, 2008 at 10:42 AM, sharpblade <[EMAIL PROTECTED]> wrote:
> Is there a way I can use threads to quit the main python process?
> In brief, I have a python script that starts when my computer starts. It
> chooses a random wallpaper background out of a specified path, and sets it
> as the desktop wallpaper. It also hooks some windows hot keys so I can cycle
> through all the wallpapers, or choose another one, or quit it, by using F8,
> F7 and F6 respectively.
> However, I would quite like the script to self-terminate if no user input is
> received after X seconds. At first this seemed simple - Create a separate
> thread that used time.sleep() to sleep for X seconds, then run a callback.
> The callback would check a simple Boolean (Set to True if a hot key is
> pressed, set to False at start of the script), and if the Boolean was False,
> it would simply run exit(), and this would close the window.
>
> However, it is not that simple. The callback and the thread work fine, but
> the exit() seems to close the THREAD, not the main process. I have tried
> sys.exit(), and some other forms I found on the Internet (Raising exceptions
> and setting the thread to a daemon), but none seemed to close the actual
> interpreter. I tried using the .join() function, but this called an
> exception that told me the thread could not be joined.
>
> Here is my threaded_test.py code: http://pastebin.com/f6060d15a

I prefer not to use threads if I can help it. Here's
a simple little example that doesn't use threads
at all and uses an event-driven approach instead
using my circuits library [1].

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: set sw=3 sts=3 ts=3

from circuits import listener, Event, Component, Timer, Manager
from circuits.lib.io import Stdin

class Test(Component):

  @listener("stop")
  def onSTOP(self):
 raise SystemExit, 0

  @listener("stdin:read")
  def onINPUT(self, data):
 print data

def main():
  manager = Manager()
  stdin = Stdin()
  test = Test()
  timer = Timer(5, Event(), "stop")

  manager += stdin
  manager += test
  manager += timer

  while True:
 try:
manager.flush()
stdin.poll()
timer.poll()
 except SystemExit:
break
 except KeyboardInterrupt:
break

if __name__ == "__main__":
  main()


cheers
James

[1] http://hg.softcircuit.com.au/projects/circuits/
http://trac.softcircuit.com.au/circuits/

--
--
-- "Problems are solved by method"
--
http://mail.python.org/mailman/listinfo/python-list


Re: calling python from lisp

2008-10-28 Thread Kaz Kylheku
["Followup-To:" header set to comp.lang.lisp.]
On 2008-10-29, Martin Rubey <[EMAIL PROTECTED]> wrote:
> Dear all,
>
> I'm trying to call from common lisp functions written for Sage
> (www.sagemath.org), which in turn is written in python. 

Maybe those functions will work under CLPython?

CLPython is different from python-on-lisp; it's front-end for Lisp that
understands Python syntax, and provides Python run-time support.

If the Python code can be proted to CLPython, it gets compiled,
and you can call it much more directly from other Lisp code.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Sorting a list

2008-10-28 Thread duncan smith
RC wrote:
> unsortedList = list(["XYZ","ABC"])
> 
> sortedList = unsortedList.sort()
> print sortedList
> 
> 
> Why this return None?

Because the sort method sorts the list in place (and returns None).

> How do I get return as ["ABC", "XYZ"]?

>>> unsortedList = ["XYZ","ABC"]
>>> unsortedList.sort()
>>> unsortedList
['ABC', 'XYZ']

or if you want a distinct sorted list (leaving the original list unchanged)

>>> unsortedList = ["XYZ","ABC"]
>>> sortedList = sorted(unsortedList)
>>> unsortedList
['XYZ', 'ABC']
>>> sortedList
['ABC', 'XYZ']
>>>

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


Re: How to get an object's name as a string?

2008-10-28 Thread Joe Strout

On Oct 28, 2008, at 6:58 PM, Steve Holden wrote:


Objects in Python *don't* have names. Period. In Python we don't
normally talk about variables anyway, except when speaking loosely, we
talk about binding names. But please don't let this start another  
round

of "Python programmers don't know how to describe the language". You
have already made your opinions on that score more than clear.


As have I, I suppose, and I'll try to quit engaging in that argument  
in the future.



l = []
l.append(l)
del l

What's the name of the list formerly known as "l"?


Hey, that's a very nice little demonstration of an orphaned object.   
Thanks for sharing it!


Best,
- Joe

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


Re: gl Multiple versions of python

2008-10-28 Thread Steve Holden
Glenn Linderman wrote:
> When using multiple versions of Python co-installed on the same system,
> what happens with local .pyc files?  If the .py is loaded with a
> different version of Python, is the .pyc rebuilt (even if the .py hasn't
> changed)?
> 
> Or must one manually delete the .pyc files?
> 
They are automatically rebuilt if they are from a different version of
Python. This is why it isn't a good idea to share library modules
between different versions.

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

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


Re: How to get an object's name as a string?

2008-10-28 Thread Joe Strout

On Oct 28, 2008, at 4:45 PM, Steven D'Aprano wrote:


What do you mean by the "name" of an object?  Objects don't generally
have names, unless you explicitly define a .name property and assign
them names.

(Variables have names, of course, but a variable isn't an object --  
it's

just a reference to an object.  Many variables may refer to the same
object, so it doesn't make any sense to ask for the name of THE  
variable

which may be referring to an object at the moment.)


That explanation makes no sense. Given the assignment:

x = 57

if the name of x isn't 'x', then what on earth can it possibly mean to
ask for the name of a variable?


Perhaps you're skimming rather than reading carefully?  Variables do  
have names, as I pointed out, and the name of x is indeed 'x'.  But  
that's not what the OP was asking for -- in your example, he'd be  
asking for the name of 57 (expecting it to be 'x').  Numbers don't  
have names; objects don't have names; variables have names, and may  
refer to numbers or objects.



In languages like Python, the term "variable" is misleading and
confusing.


Oh good grief.  Now you're going to try to discard the standard term  
"variable" as well?


All right then, if you really insist on making Python more mysterious  
by making up new terms for perfectly ordinary and standard programming  
concepts, then I suggest the following:


variable: "ariablevay"
value: "aluvay"
reference: "eferencevay"
call-by-value: "allcay-ibay-aluvay"
call-by-reference: (no term needed, since Python doesn't have it)

There.  Now we've got a simple mapping from standard terminology to  
properly mystical Python-culture terms that are nonetheless easy to  
learn.  Agreed?


Best,
- Joe

P.S. Shannon: don't listen to Steven.  He's out to confuse you and  
make Python seem much harder and complex than it really is.


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


Re: How to get an object's name as a string?

2008-10-28 Thread Steve Holden
Steven D'Aprano wrote:
> On Tue, 28 Oct 2008 09:15:50 -0600, Joe Strout wrote:
> 
>> On Oct 28, 2008, at 8:41 AM, Shannon Mayne wrote:
>>
>>> I would like to create objects with algorithmically determined names
>>> based on other object names and use object names for general algorithm
>>> input.
>> What do you mean by the "name" of an object?  Objects don't generally
>> have names, unless you explicitly define a .name property and assign
>> them names.
>>
>> (Variables have names, of course, but a variable isn't an object -- it's
>> just a reference to an object.  Many variables may refer to the same
>> object, so it doesn't make any sense to ask for the name of THE variable
>> which may be referring to an object at the moment.)
> 
> That explanation makes no sense. Given the assignment:
> 
> x = 57
> 
> if the name of x isn't 'x', then what on earth can it possibly mean to 
> ask for the name of a variable?
> 
He didn't ask for the name of a variable, he asked for the name of an
object. You may choose to equate them, but they aren't the same thing.

> In languages like Python, the term "variable" is misleading and 
> confusing. Python's programming model has objects (values), and names. 
> Best to use language that describes what Python actually does, rather 
> than use language that describes what other languages do.
> 
Objects in Python *don't* have names. Period. In Python we don't
normally talk about variables anyway, except when speaking loosely, we
talk about binding names. But please don't let this start another round
of "Python programmers don't know how to describe the language". You
have already made your opinions on that score more than clear.

l = []
l.append(l)
del l

What's the name of the list formerly known as "l"?

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

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


Using threads to quit the main python process

2008-10-28 Thread sharpblade
Is there a way I can use threads to quit the main python process?
In brief, I have a python script that starts when my computer starts. It
chooses a random wallpaper background out of a specified path, and sets it
as the desktop wallpaper. It also hooks some windows hot keys so I can cycle
through all the wallpapers, or choose another one, or quit it, by using F8,
F7 and F6 respectively.
However, I would quite like the script to self-terminate if no user input is
received after X seconds. At first this seemed simple - Create a separate
thread that used time.sleep() to sleep for X seconds, then run a callback.
The callback would check a simple Boolean (Set to True if a hot key is
pressed, set to False at start of the script), and if the Boolean was False,
it would simply run exit(), and this would close the window.

However, it is not that simple. The callback and the thread work fine, but
the exit() seems to close the THREAD, not the main process. I have tried
sys.exit(), and some other forms I found on the Internet (Raising exceptions
and setting the thread to a daemon), but none seemed to close the actual
interpreter. I tried using the .join() function, but this called an
exception that told me the thread could not be joined.

Here is my threaded_test.py code: http://pastebin.com/f6060d15a

Thanks for reading,
~Tom
--
http://mail.python.org/mailman/listinfo/python-list


calling python from lisp

2008-10-28 Thread Martin Rubey
Dear all,

I'm trying to call from common lisp functions written for Sage
(www.sagemath.org), which in turn is written in python.  To do so, I tried
http://common-lisp.net/project/python-on-lisp/.  It was quite easy to get it to
run and do some simple things with python.

However, I was unable to get sage to run within it.

If I start my local python 2.5 and follow (roughly)
http://www.sagemath.org/doc/tut/node55.html

it works nicely:

[EMAIL PROTECTED]:~/Documents/sage-3.1.4/local/bin$ . ./sage-env
[EMAIL PROTECTED]:~/Documents/sage-3.1.4/local/bin$ /usr/bin/python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> from sage.all import *
>>> var('z')
z
>>> integrate(1/z, z)
log(z)
>>>


But not so from within python-on-lisp.  Below the complete log.  But actually,
the first warning occurs already when importing sys (I have little python
experience and no idea what sys contains)

sys:1: RuntimeWarning: Python C API version mismatch for module pol: This 
Python has API version 1013, module pol has version 1011.

As far as I understand python on lisp, it provides an interface to the python
library:

(cffi:define-foreign-library python-library
  (:darwin (:framework "Python"))
  (:unix (:or "libpython2.5.so.1.0" "libpython2.4.so.1.0"
"libpython2.3.so.1.0"))
  (:windows (:or "python25.dll" "python24.dll" "python23.dll") )
  (t (:default "libpython")))

Sage comes with it's own python, however, without a library.  On the otherhand,
above I demonstrated that sage also works with the python interpreter that
comes with kubuntu.  (maybe it needs the interpreter, and the library is not
sufficient?)

Help would be greatly appreciated.  

Martin

[EMAIL PROTECTED]:~/Documents/sage-3.1.4/local/bin$ sbcl
This is SBCL 1.0.11.debian, an implementation of ANSI Common Lisp.
More information about SBCL is available at .

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.
* (asdf:operate 'asdf:load-op :pythononlisp)

; loading system definition from /home/martin/.sbcl/systems/pythononlisp.asd
; into #
; registering # as PYTHONONLISP
; loading system definition from /home/martin/.sbcl/systems/cffi.asd into
; #
; registering # as CFFI
; loading system definition from /home/martin/.sbcl/systems/babel.asd into
; #
; registering # as BABEL
; loading system definition from /home/martin/.sbcl/systems/alexandria.asd into
; #
; registering # as ALEXANDRIA
; loading system definition from
; /home/martin/.sbcl/systems/trivial-features.asd into #
; registering # as TRIVIAL-FEATURES

...

; compilation unit finished
;   caught 11 STYLE-WARNING conditions
;   printed 4 notes
NIL
* (py::py-repl)
Welcome to the Python-on-lisp REPL emulator - enter on a blank line to quit
import sys
sys:1: RuntimeWarning: Python C API version mismatch for module pol: This 
Python has API version 1013, module pol has version 1011.
>>> If you can see this, Python is loaded and working
from sage.all import *
>>> Traceback (most recent call last):
  File "", line 9, in 
  File
; 
"/home/martin/Documents/sage-3.1.4/local/lib/python2.5/site-packages/sage/all.py",
; line 58, in 
from sage.misc.all   import * # takes a while
  File
; 
"/home/martin/Documents/sage-3.1.4/local/lib/python2.5/site-packages/sage/misc/all.py",
; line 16, in 
from sage_timeit_class import timeit
  File "sage_timeit_class.pyx", line 3, in sage.misc.sage_timeit_class
; (sage/misc/sage_timeit_class.c:523)
  File
; 
"/home/martin/Documents/sage-3.1.4/local/lib/python2.5/site-packages/sage/misc/sage_timeit.py",
; line 12, in 
import timeit as timeit_, time, math, preparser, interpreter
  File
; 
"/home/martin/Documents/sage-3.1.4/local/lib/python2.5/site-packages/sage/misc/interpreter.py",
; line 99, in 
import IPython.ipapi
  File
; 
"/home/martin/Documents/sage-3.1.4/local/lib/python2.5/site-packages/IPython/__init__.py",
; line 57, in 
__import__(name,glob,loc,[])
  File
; 
"/home/martin/Documents/sage-3.1.4/local/lib/python2.5/site-packages/IPython/ipstruct.py",
; line 22, in 
from IPython.genutils import list2dict2
  File
; 
"/home/martin/Documents/sage-3.1.4/local/lib/python2.5/site-packages/IPython/genutils.py",
; line 116, in 
Term = IOTerm()
  File
; 
"/home/martin/Documents/sage-3.1.4/local/lib/python2.5/site-packages/IPython/genutils.py",
; line 112, in __init__
self.cout = IOStream(cout,sys.stdout)
  File
; 
"/home/martin/Documents/sage-3.1.4/local/lib/python2.5/site-packages/IPython/genutils.py",
; line 80, in __init__
self.flush = stream.flush
AttributeError: Sout instance has no attribute 'flush'
--
http://mail.python.org/mailman/listinfo/python-list


Re: Tkinter: How to get Label wraplength functionality in Text Box

2008-10-28 Thread Guilherme Polo
On 10/28/08, Mudcat <[EMAIL PROTECTED]> wrote:
> I've tried quite a few things to get this correct but have hit a
>  couple of sticking points that I can't figure out. I need to ge the
>  Text box to function like the 'wraplength' option in a Label.
>
>  I've been able to adjust the height of the text by calculating the
>  number of lines needed to display the text. That's fairly simple. I
>  know the number of characters in the text and width of the box (which
>  is static). From that point I can calculate how many lines of display
>  is needed and resize it.
>
>  The problem arises when I use the 'wrap' option of the Text Box so the
>  words aren't chopped off. Once the wrapping is done there are dead
>  spaces left at the end of the lines which are ignored when the char
>  count is done. As a result sometimes the last line is not shown. I can
>  always just add +1 to the number, but then sometimes I get an empty
>  line. Space is at a premium in this app, so I have to cut everything
>  down to use only what's necessary.
>
>  So does anyone know how add in those extra spaces to get this to
>  adjust correctly? (Or if there is another way to get the Text Box to
>  automatically adjust it's size that I'm not aware of?)
>

Are you looking for something like the new "count" command for the
text widget in tk 8.5 ? "count" can count the number of logical lines
(irrespective of wrapping), display lines (counts one for each time a
line wraps) and some other things.


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


-- 
-- Guilherme H. Polo Goncalves
--
http://mail.python.org/mailman/listinfo/python-list


urlsplit() and windows paths

2008-10-28 Thread Patrick Mézard
Hello,

Why does urlparse.urlsplit() succeed here ?


"""
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) 
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import urlparse
>>> urlparse.urlsplit('c:\\foo\\bar')
('c', '', '\\foo\\bar', '', '')
"""

Documentations (of urlparse(), referenced by urlsplit()) states:

"""
Parse a URL into six components, returning a 6-tuple. This corresponds to the 
general structure of a URL: scheme://netloc/path;parameters?query#fragment.
"""

What kind of URLs does it parse not containing "://" ?

--
Patrick Mézard
--
http://mail.python.org/mailman/listinfo/python-list


Re: Deviation from object-relational mapping (pySQLFace)

2008-10-28 Thread sulyokpeti
On okt. 22, 06:27, huy <[EMAIL PROTECTED]> wrote:
> On Oct 12, 11:19 am, [EMAIL PROTECTED] wrote:
>
> > I have made a simple python module to handle SQL 
> > databases:https://fedorahosted.org/pySQLFace/wiki
> > Its goal to separate relational database stuff (SQL) from algorythmic
> > code (python). A SQLFace is a facade initialized with a configuration
> > file (XML). It provides callable command objects for each sql query.
> > The call substitutes template variables with its parameters, and
> > returns the result of the query.
> > I would like to get some opinions on this approach.
> > Thanks.
>
> Best use of XML for SQL generation/use I have seen is Ibatis SQLMAPS.
>
> This focuses on the right things i.e queries and mapping values to/
> from objects.
>
> It would be great if python had such a tool.
>
> Huy

I have looked into Ibatis SQLMAPS. It claims that its biggest
advantage is simplicity over other frameworks and ORMs. If you look
into my examples, you will see what simplicity is.

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


Re: How to get an object's name as a string?

2008-10-28 Thread Steven D'Aprano
On Tue, 28 Oct 2008 09:15:50 -0600, Joe Strout wrote:

> On Oct 28, 2008, at 8:41 AM, Shannon Mayne wrote:
> 
>> I would like to create objects with algorithmically determined names
>> based on other object names and use object names for general algorithm
>> input.
> 
> What do you mean by the "name" of an object?  Objects don't generally
> have names, unless you explicitly define a .name property and assign
> them names.
> 
> (Variables have names, of course, but a variable isn't an object -- it's
> just a reference to an object.  Many variables may refer to the same
> object, so it doesn't make any sense to ask for the name of THE variable
> which may be referring to an object at the moment.)

That explanation makes no sense. Given the assignment:

x = 57

if the name of x isn't 'x', then what on earth can it possibly mean to 
ask for the name of a variable?

In languages like Python, the term "variable" is misleading and 
confusing. Python's programming model has objects (values), and names. 
Best to use language that describes what Python actually does, rather 
than use language that describes what other languages do.




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


Re: Extracting name strings from assigned objects

2008-10-28 Thread Steven D'Aprano
On Tue, 28 Oct 2008 09:23:46 -0700, Shannon Mayne wrote:

> I would like to ask how one might obtain the assigned name of an
> assigned object as a string. I would like to use object names as an
> algorithmic input.
> 
> 
> To demonstrate... So if i have:
> 
>>>foo = {}
> 
> what can I do to the object 'foo' so as to return the string 'foo'?

There's a lot of confusion there. The object 'foo' is a string, and it 
doesn't occur anywhere in your code. The name foo is not an object, it is 
a name. Perhaps you meant the object {} (an empty dict)?

The object {} doesn't know what names (note plural) it has been bound 
too. It could be bound to one name:

foo = {}

or many names:

foo = bar = baz = flib = {}

or no names at all:

{}


Names are not properties of objects. You can't do what you are trying to 
do. If you tell us why you want to do this, perhaps we can suggest a way 
to do it.



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


Re: Finding the instance reference of an object

2008-10-28 Thread Steven D'Aprano
On Tue, 28 Oct 2008 09:59:57 -0600, Joe Strout wrote:

> There are only the two cases, which Greg quite succinctly and accurately
> described above.  One is by value, the other is by reference.  Python
> quite clearly uses by value.

That is absolute nonsense, based on the idiotic assumption that 
programmers should care more about an arbitrary reference to a value than 
to the value itself.

I'm sure I've quoted the excellent effbot before, but he deserves 
repeating:

[quote]
well, I guess you can, in theory, value an artificial number assigned
to an object as much as the object itself.

"Joe, I think our son might be lost in the woods"
"Don't worry, I have his social security number"
[end quote]

As I wrote yesterday:

The value of a Python name is the Python object assigned to it, not an 
arbitrary memory location that points to the object. Even you would 
consider it obfuscatory if I executed this code:

x = "Norwegian Blue"

and then insisted that the value of x was "3086179808L, but if I run that 
line of code again it could get another value, and naturally if you run 
it on your computer you're almost certain to get a different value".

By your definition of "value=reference", the above is perfectly correct, 
and utterly, completely pointless, useless and unhelpful. It's rather 
like listing the ingredients of a cake as "Atoms". Technically true, but 
missing the point.

Once we discard the unhelpful assumption that value=reference, your 
entire argument falls apart.



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


Re: explanation of values, references, assignment, and method calls

2008-10-28 Thread Aaron Brady
On Oct 28, 11:30 am, Bruno Desthuilliers  wrote:
> Joe Strout a écrit :
>
> > I've tried to write up this topic in a clear, step-by-step manner, with
> > the help of diagrams and short examples from several different OOP
> > languages.  I hope it will help clear up the confusion that seems to be
> > pervading the Python community
>
> May I suggesthttp://effbot.org/zone/call-by-object.htm?

Joe, Fredrik's is more brutal than yours.  Both good, depending on
what you like in leaders.  Yes, some people like brutal leaders.
--
http://mail.python.org/mailman/listinfo/python-list


Re: 2.6, 3.0, and truly independent intepreters

2008-10-28 Thread Martin v. Löwis
> Because then we're back into the GIL not permitting threads efficient
> core use on CPU bound scripts running on other threads (when they
> otherwise could).

Why do you think so? For C code that is carefully written, the GIL
allows *very well* to write CPU bound scripts running on other threads.
(please do get back to Jesse's original remark in case you have lost
the thread :-)

> An example would be python scripts that generate video programatically
> using an initial set of params and use an in-house C module to
> construct frame (which in turn make and modify python C objects that
> wrap to intricate codec related data structures).  Suppose you wanted
> to render 3 of these at the same time, one on each thread (3
> threads).  With the GIL in place, these threads can't anywhere close
> to their potential.  Your response thus far is that the C module
> should release the GIL before it commences its heavy lifting.  Well,
> the problem is that if during its heavy lifting it needs to call back
> into its interpreter.

So it should reacquire the GIL then. Assuming the other threads
all do their heavy lifting, it should immediately get the GIL,
fetch some data, release the GIL, and continue to do heavy lifting.
If it's truly CPU-bound, I hope it doesn't spend most of its time
in Python API, but in true computation.

> It's turns out that this isn't an exotic case
> at all: there's a *ton* of utility gained by making calls back into
> the interpreter. The best example is that since code more easily
> maintained in python than in C, a lot of the module "utility" code is
> likely to be in python.

You should really reconsider writing performance-critical code in
Python. Regardless of the issue under discussion, a lot of performance
can be gained by using "flattened" data structures, less pointer,
less reference counting, less objects, and so on - in the inner loops
of the computation. You didn't reveal what *specific* computation you
perform, so it's difficult to give specific advise.

> Unsurprisingly, this is the situation myself
> and many others are in: where we want to subsequently use the
> interpreter within the C module (so, as I understand it, the proposal
> to have the C module release the GIL unfortunately doesn't work as a
> general solution).

Not if you do the actual computation in Python, no. However, this
subthread started with Jesse's remark that you *can* release the GIL
in C code.

Again, if you do heavy-lifting in Python, you should consider to rewrite
the performance-critical parts in C. You may find that the need for
multiple CPUs goes even away.

> I appreciate your arguments these a PyC concept is a lot of work with
> some careful design work, but let's not kill the discussion just
> because of that.

Any discussion in this newsgroup is futile, except when it either
a) leads to a solution that is already possible, and the OP didn't
envision, or
b) is followed up by code contributions from one of the participants.

If neither is likely to result, killing the discussion is the most
productive thing we can do.

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


Re: Do I need a lock here?

2008-10-28 Thread Aaron Brady
On Oct 28, 3:29 pm, jasiu85 <[EMAIL PROTECTED]> wrote:
> On Oct 27, 10:12 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
>
>
>
> > jasiu85 schrieb:
>
> > > Hey,
>
> > > Please take a look at the code of the two threads below:
>
> > > COMMON_DICT = {}
>
> > > def thread_1():
> > >     global COMMON_DICT
> > >     local_dict = prepare_dict()
> > >     COMMON_DICT = local_dict
>
> > > def thread_2():
> > >     global COMMON_DICT
> > >     local_dict = COMMON_DICT
> > >     use_dict(local_dict)
>
> > > Do I need a lock to protect the COMMON_DICT dictionary? AFAIK bytecode
> > > operations are atomic and in each thread there's only one crucial
> > > bytecode op: STORE_NAME in the first thread and LOAD_NAME in the
> > > second one. So I suspect that everything will work just fine. Am I
> > > right?
>
> > Depending on what you mean by "right".
>
> > The above is not enough to judge what is really happening. But depending
> > on the execution order, thread_1 overwrites the reference in COMMON_DICT
> > *after* thread_2 has altered it.
>
> > But it won't crash or anything. If that's right for you.
>
> > Diez
>
> The second thread doesn't alter the dictionary, it only reads it. Even
> if the first thread puts a reference to a new dictionary into the
> COMMON_DICT variable while the second thread is reading the old
> dictionary, such sematics is fine for me. I'm seeking for a kind of
> Producer/Consumer pattern. So the first thread produces some data in a
> form of a dictionary. The second thread reads it from time to time.
> It's sufficient for me if a dictionary is "lost" because the first
> thread overwrites the COMMON_DICT variable before the second thread
> tries to read it. It's also sufficient for me if the first thread
> updates COMMON_DICT variable while the second thread is reading the
> previous copy of the dictionary. So from my point of view the most
> critical parts are these two lines:
>
> COMMON_DICT = local_dict # in the first thread
> local_dict = COMMON_DICT # in the second thread
>
> Can these two instructions by any chance interfere with each other in
> a way that will crash either of the threads?
>
> I hope I made myself a little bit more clear :).
>
> Thanks!!
>
> Mike

I vote for OK, in this /very/ /particular/ /case/, but what would you
accept as evidence?

These pointer values start out different and become the same, but both
are dictionaries.

10490912 10490912
10490912 10490912
10490912 10490624
10490912 10490624
10490624
10490624 10490624
10490624 10490624
10490624 10490624

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


Re: Finding the instance reference of an object

2008-10-28 Thread Aaron Brady
On Oct 27, 2:11 pm, Joe Strout <[EMAIL PROTECTED]> wrote:
> On Oct 27, 2008, at 12:19 PM, [EMAIL PROTECTED] wrote:
>
> > I think this "uncontrived" example addresses the C/Python difference
> > fairly directly (both were tested):
>
> That's correct, but of course, C is a decades-old language barely a  
> step above assembler.  For a fair comparison, pick any modern OOP  
> language, including the C derivatives (C++, Objective-C, Java), and  
> compare a Python object to an object in that language, rather than to  
> a struct.
>
> > In Python, variables are just names/aliases for *references* to
> > objects, not names for the objects/values themselves. The variable
> > names themselves do not correspond directly to the objects' memory
> > locations.
>
> Exactly!  In C++, this would be like a pointer variable.  In Java, RB,  
> or .NET, it's like an object reference.
>
> > While yes, technically, it is true that those reference
> > values must be stored somewhere in memory, *that* is the
> > implementation detail.
>
> Agreed.  (And this was my point in response to someone arguing that no  
> such location exists.)
>
> > But is is not the *locations* of these
> > references (i.e., the locations of the Python *variables*) that are
> > copied around, it is the references themselves (the locations of the
> > Python *objects*) that are copied.
>
> Right.  The variables contain object references; these object  
> references are copied (not referenced!) when you pass them into a  
> function.  That's call by value.

snip.

What do you propose the name of the method of passing parameters
should be for the following function 'f' in C?

struct T {
  int a;
  int b;
  int c;
};

void f( T t );

Given a 4-byte system word, how many bytes do you think are copied to
the stack upon calling 'f'?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Database specialized in storing directed graphs?

2008-10-28 Thread Aaron Brady
On Oct 27, 7:32 pm, Carl Banks <[EMAIL PROTECTED]> wrote:
> I was wondering if anyone had any advice on this.
>
> This is not to study graph theory; I'm using the graph to represent a
> problem domain.  The graphs could be arbitrarily large, and could
> easily have millions of nodes, and most nodes have a substantial
> amount of data associated with them.  Obviously I don't want a whole
> such graph in memory at once, so libraries the just deal with in-
> memory graphs are out.
>
> I know I could implement this with a relational DB, and I'd be fine
> with a library that was built on top of one.  But I'm hoping for
> specialzed behavior useful for graphs.
>
> For example, suppose I have a set of nodes called A.  It would be
> useful to know if any of these nodes are connected by paths that
> include no nodes in A.  I could, of course, do that by reading from
> the database and following the paths, but I clearly don't want to do
> that.  I would want instead to somehow cache the outside connectedness
> relationship between different nodes of A.  But then what happens if
> something changes elsewhere.  How is the cache for A notified, and can
> it be updated efficiently using graph theorems, rather than
> regenerated?
>
> It's very tricky; that's why I hope someone else has done it.
>
> I'm guessing no.
>
> Carl Banks

Are you just looking for a persistent graph?  The usual options are
'shelve', 'sqllite', 'mmap', and 'ctypes.Structure'.  Or did you need
a special structure for the 'outside connectedness' properties?

Storing that is the usual time-space tradeoff.  (Actually, I think
that term refers to something slightly distinct.  This would be more
properly termed read-time/write-time trade off, roughly.)

Assuming you pick an RDB, you'd have a table of vertices and a table
of edges.  Did you want 'set A' to be a table and persist between runs
of the program?  If not, just keep a set of 2-tuples of nodes that
satisfy that property.  I think the set S you're after is: all x,y
such that x is a vertex in A, y is a vertex in A, and there exists a P
such that P is a path, P starts at x, P ends at y, and vertex v in P
implies that v is x, v is y, or v is not in A.  I don't know if you
can cache any information about A that gives you any shortcuts in
calculating S, or what the running time or running space of the
fastest/smallest algorithm is.  At worst, it's O( |V| * |E| ) on each
change to V or E.  Unverified.

You might be able to store the shortest path in a mapping from 2-
tuples to paths.  Or better yet, map each node in A to the set of all
nodes reachable from it only entering A once.  Then, you might save
time on either adding a node/vertex to A or G, or removing one from A
or G, or both.  Maybe mapping each node in G to that relative set.
You didn't say whether the graph was directed and/or cyclic.

A good place to start would be, if you add a node to G, can S
decrease?  No, because the original path P still exists.  If you
remove one from A, still in G, S can stay the same size, might
decrease.  If you remove a node from G, not in A, same, etc.
--
http://mail.python.org/mailman/listinfo/python-list


Tkinter: How to get Label wraplength functionality in Text Box

2008-10-28 Thread Mudcat
I've tried quite a few things to get this correct but have hit a
couple of sticking points that I can't figure out. I need to ge the
Text box to function like the 'wraplength' option in a Label.

I've been able to adjust the height of the text by calculating the
number of lines needed to display the text. That's fairly simple. I
know the number of characters in the text and width of the box (which
is static). From that point I can calculate how many lines of display
is needed and resize it.

The problem arises when I use the 'wrap' option of the Text Box so the
words aren't chopped off. Once the wrapping is done there are dead
spaces left at the end of the lines which are ignored when the char
count is done. As a result sometimes the last line is not shown. I can
always just add +1 to the number, but then sometimes I get an empty
line. Space is at a premium in this app, so I have to cut everything
down to use only what's necessary.

So does anyone know how add in those extra spaces to get this to
adjust correctly? (Or if there is another way to get the Text Box to
automatically adjust it's size that I'm not aware of?)

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


Re: import foo vs. python -m foo

2008-10-28 Thread Hrvoje Niksic
Simon Bierbaum <[EMAIL PROTECTED]> writes:

> Hi all,
>
> what is the difference between saying "import foo" in an interactive
> prompt and starting one using "python -m foo"? The -m switch is not
> covered in the man page, is it even officially supported?

My copy of the man page states:

-m module-name
Searches sys.path for the named module and runs the corresponding
.py file as a script.

> I'm asking because one of my modules fails on import in the second
> version but succeeds in the first.

It probably contains buggy code inside "if __name__ == '__main__': ...".
--
http://mail.python.org/mailman/listinfo/python-list


Re: Do I need a lock here?

2008-10-28 Thread jasiu85
On Oct 27, 10:12 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> jasiu85 schrieb:
>
>
>
> > Hey,
>
> > Please take a look at the code of the two threads below:
>
> > COMMON_DICT = {}
>
> > def thread_1():
> >     global COMMON_DICT
> >     local_dict = prepare_dict()
> >     COMMON_DICT = local_dict
>
> > def thread_2():
> >     global COMMON_DICT
> >     local_dict = COMMON_DICT
> >     use_dict(local_dict)
>
> > Do I need a lock to protect the COMMON_DICT dictionary? AFAIK bytecode
> > operations are atomic and in each thread there's only one crucial
> > bytecode op: STORE_NAME in the first thread and LOAD_NAME in the
> > second one. So I suspect that everything will work just fine. Am I
> > right?
>
> Depending on what you mean by "right".
>
> The above is not enough to judge what is really happening. But depending
> on the execution order, thread_1 overwrites the reference in COMMON_DICT
> *after* thread_2 has altered it.
>
> But it won't crash or anything. If that's right for you.
>
> Diez

The second thread doesn't alter the dictionary, it only reads it. Even
if the first thread puts a reference to a new dictionary into the
COMMON_DICT variable while the second thread is reading the old
dictionary, such sematics is fine for me. I'm seeking for a kind of
Producer/Consumer pattern. So the first thread produces some data in a
form of a dictionary. The second thread reads it from time to time.
It's sufficient for me if a dictionary is "lost" because the first
thread overwrites the COMMON_DICT variable before the second thread
tries to read it. It's also sufficient for me if the first thread
updates COMMON_DICT variable while the second thread is reading the
previous copy of the dictionary. So from my point of view the most
critical parts are these two lines:

COMMON_DICT = local_dict # in the first thread
local_dict = COMMON_DICT # in the second thread

Can these two instructions by any chance interfere with each other in
a way that will crash either of the threads?

I hope I made myself a little bit more clear :).

Thanks!!

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


Re: HTML File Parsing

2008-10-28 Thread Stefan Behnel
Felipe De Bene wrote:
> I'm having problems parsing an HTML file with the following syntax :
> 
> 
> User ID
> Name BGCOLOR='#c0c0c0'>Date
> and so on
> 
> whenever I feed the parser with such file I get the error :
> 
> HTMLParser.HTMLParseError: bad end tag: "", at
> line 515, column 45

Your HTML page is not HTML, i.e. it is broken. Python's HTMLParser is not made
for parsing broken HTML. However, you can use the parse of lxml.html to fix up
your HTML for you.

http://codespeak.net/lxml/

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


Re: parse a table in HTML page.

2008-10-28 Thread Stefan Behnel
antonio_wn8 wrote:
> I have a need to read and parse a table in HTML page.
> 
> I’m using the following script:
> http://trac.davidgrant.ca/browser/src/python/misc/siteuptime/TableParser.py
> 
> It works fine  aside from  link in href.
> 
> Example:
> 
> String to parse:
> elognormal text
> 
> Output:
> [[['elog', 'normal text']]]

You should try lxml.html. It gives you various tools like XPath to look for
specific elements and helper functions to find the links in an HTML document.

http://codespeak.net/lxml/

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


Re: 2.6, 3.0, and truly independent intepreters

2008-10-28 Thread Rhamphoryncus
On Oct 28, 9:30 am, "Andy O'Meara" <[EMAIL PROTECTED]> wrote:
> On Oct 25, 9:46 am, "M.-A. Lemburg" <[EMAIL PROTECTED]> wrote:
>
> > These discussion pop up every year or so and I think that most of them
> > are not really all that necessary, since the GIL isn't all that bad.
>
> Thing is, if the topic keeps coming up, then that may be an indicator
> that change is truly needed.  Someone much wiser than me once shared
> that a measure of the usefulness and quality of a package (or API) is
> how easily it can be added to an application--of any flavors--without
> the application needing to change.
>
> So in the rising world of idle cores and worker threads, I do see an
> increasing concern over the GIL.  Although I recognize that the debate
> is lengthy, heated, and has strong arguments on both sides, my reading
> on the issue makes me feel like there's a bias for the pro-GIL side
> because of the volume of design and coding work associated with
> considering various alternatives (such as Glenn's "Py*" concepts).
> And I DO respect and appreciate where the pro-GIL people come from:
> who the heck wants to do all that work and recoding so that a tiny
> percent of developers can benefit?  And my best response is that as
> unfortunate as it is, python needs to be more multi-threaded app-
> friendly if we hope to attract the next generation of app developers
> that want to just drop python into their app (and not have to change
> their app around python).  For example, Lua has that property, as
> evidenced by its rapidly growing presence in commercial software
> (Blizzard uses it heavily, for example).
>
>
>
> > Furthermore, there are lots of ways to tune the CPython VM to make
> > it more or less responsive to thread switches via the various sys.set*()
> > functions in the sys module.
>
> > Most computing or I/O intense C extensions, built-in modules and object
> > implementations already release the GIL for you, so it usually doesn't
> > get in the way all that often.
>
> The main issue I take there is that it's often highly useful for C
> modules to make subsequent calls back into the interpreter. I suppose
> the response to that is to call the GIL before reentry, but it just
> seems to be more code and responsibility in scenarios where it's no
> necessary.  Although that code and protocol may come easy to veteran
> CPython developers, let's not forget that an important goal is to
> attract new developers and companies to the scene, where they get
> their thread-independent code up and running using python without any
> unexpected reengineering.  Again, why are companies choosing Lua over
> Python when it comes to an easy and flexible drop-in interpreter?  And
> please take my points here to be exploratory, and not hostile or
> accusatory, in nature.
>
> Andy

Okay, here's the bottom line:
* This is not about the GIL.  This is about *completely* isolated
interpreters; most of the time when we want to remove the GIL we want
a single interpreter with lots of shared data.
* Your use case, although not common, is not extraordinarily rare
either.  It'd be nice to support.
* If CPython had supported it all along we would continue to maintain
it.
* However, since it's not supported today, it's not worth the time
invested, API incompatibility, and general breakage it would imply.
* Although it's far more work than just solving your problem, if I
were to remove the GIL I'd go all the way and allow shared objects.

So there's really only two options here:
* get a short-term bodge that works, like hacking the 3rd party
library to use your shared-memory allocator.  Should be far less work
than hacking all of CPython.
* invest yourself in solving the *entire* problem (GIL removal with
shared python objects).
--
http://mail.python.org/mailman/listinfo/python-list


Re: big objects and avoiding deepcopy?

2008-10-28 Thread Daniel da Silva
http://groups.google.com/group/perl.perl6.language/msg/b0cfa757f0ce1cfd?pli=1

: )


On Mon, Oct 27, 2008 at 1:12 PM, <[EMAIL PROTECTED]> wrote:

> Robert Kern:
> > This is similar to implementing "Undo" functionality in applications.<
>
> In a quite-high-level language (like Python, but not necessarily in
> Python itself) it may become eventually advantageous to add some (even
> limited) built-in form of undo. Both to give a simpler way to
> implement a undo functionality into user-level programs (that is to
> implement the Undo command in a program with GUI), but more
> importantly to help the programmer too in some more general
> programming tasks.
>
> So it's probably a feature we'll see in the next generation of high-
> level languages, among few other features that today are missing in
> Python (only sometimes missing for performance reasons).
>
> Bye,
> bearophile
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Daniel da Silva
(240) 678 - 4686
GSFC, GES-DISC 610.2
University of Maryland
--
http://mail.python.org/mailman/listinfo/python-list


HTML File Parsing

2008-10-28 Thread Felipe De Bene
I'm having problems parsing an HTML file with the following syntax :


User ID
NameDate
and so on

whenever I feed the parser with such file I get the error :

Traceback (most recent call last):
  File "C:\Documents and Settings\Administrator\My Documents\workspace
\thread\src\parser.py", line 91, in 
p.parse(thechange)
  File "C:\Documents and Settings\Administrator\My Documents\workspace
\thread\src\parser.py", line 16, in parse
self.feed(s)
  File "C:\Python25\lib\HTMLParser.py", line 110, in feed
self.goahead(0)
  File "C:\Python25\lib\HTMLParser.py", line 152, in goahead
k = self.parse_endtag(i)
  File "C:\Python25\lib\HTMLParser.py", line 316, in parse_endtag
self.error("bad end tag: %r" % (rawdata[i:j],))
  File "C:\Python25\lib\HTMLParser.py", line 117, in error
raise HTMLParseError(message, self.getpos())
HTMLParser.HTMLParseError: bad end tag: "", at
line 515, column 45

Googling around I've found a solution to a similar situation, over and
over again :
http://64.233.169.104/search?q=cache:zOmjwM_sGBcJ:coding.derkeiler.com/pdf/Archive/Python/comp.lang.python/2006-02/msg00026.pdf+CDATA_CONTENT_ELEMENTS&hl=pt-BR&ct=clnk&cd=5&gl=br&client=firefox-a

but coding :

you can disable proper parsing by setting the CDATA_CONTENT_ELEMENTS
attribute on the parser instance, before you start parsing. by
default, it is
set to
CDATA_CONTENT_ELEMENTS = ("script", "style")
setting it to an empty tuple disables HTML-compliant handling for
these
elements:
p = HTMLParser()
p.CDATA_CONTENT_ELEMENTS = ()
p.feed(f.read())

didn't solve my problem. I've made a little modification then to
HTMLParser.py instead that solved the problem, as follows:
original: endtagfind = re.compile('')
my version : endtagfind = re.compile('')

it worked ok for all the files I needed and also for a different file
I also parse using the same library. I know it might sound stupid but
I was just wondering if there's a better way of solving that problem
than just modifying the standard library. Any clue ?

thx in advance,
Felipe.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the instance reference of an object

2008-10-28 Thread Dale Roberts
On Oct 28, 11:59 am, Joe Strout <[EMAIL PROTECTED]> wrote:
> ...
>
> There are only the two cases, which Greg quite succinctly and  
> accurately described above.  One is by value, the other is by  
> reference.  Python quite clearly uses by value.  Parameters are  
> expressions that are evaluated, and the resulting value copied into  
> the formal parameter, pure and simple.  The continued attempts to  
> obfuscate this is pointless and wrong.
>
> Best,
> - Joe

5 + 3

What is the "value" of that expression in Python? Can you tell me?

99.99% of programmers (who do not have this thread as context) will
say that the value is 8. But you say the value is the memory address
of the resulting object created when the + operator is applied to the
5 object and the 3 object. That is the "value" that is copied.

Okay, you can have it that way, but every time you explain to someone
that Python passes "By Value", you will have to add the additional
baggage that, oh, by the way, there is a completely different meaning
for "value" in Python than what you are used to.

Then the questions and puzzled looks will start...

And when they tell their friend that Joe The Programmer said it's Pass
By Value, your additional context may not be present any longer, and
the friend will be very confused.

In my opinion, best just to head it off and call it something
different so as not to confuse.

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


Re: question about the textwrap module

2008-10-28 Thread TP
Gabriel Genellina wrote:

> You may pre-process your text (stripping redundant whitespace) before
> using textwrap:

Thanks Gabriel for your answers!
I finally have subclassed textwrap.TextWrapper.

Julien


-- 
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+,\'Z
(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
--
http://mail.python.org/mailman/listinfo/python-list


Re: ignoring NAs in a dataset with numpy

2008-10-28 Thread Robert Kern

mrafi wrote:

Hey All,
I am working with numpy 
I have a data set with a lot of nan values, and i want to calculate standard

deviation
is there a direct function to do it for example nansum(or something like
this),
to calculate standard deviation ignoring nan values??


You will want to ask numpy questions on the numpy-discussion mailing list.

  http://www.scipy.org/Mailing_Lists

scipy.stats.nanstd() implements this. Additionally, data[~isnan(data)].std() 
works unless if you need to apply the std() along just one axis.


--
Robert Kern

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

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


Re: big objects and avoiding deepcopy?

2008-10-28 Thread Aaron Brady
On Oct 27, 1:10 pm, Terry Reedy <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > Robert Kern:
> >> This is similar to implementing "Undo" functionality in applications.<
>
> > In a quite-high-level language (like Python, but not necessarily in
> > Python itself) it may become eventually advantageous to add some (even
> > limited) built-in form of undo.
>
> Right now, I believe, one could develop an undo module with undolist and
> undodict classes that wrap instance mutations with an append to an
> undoinfo list.

The trick would be forking instances, so that you could operate on
separate histories/branches independently.  Not necessary for most
undo purposes, of course.
--
http://mail.python.org/mailman/listinfo/python-list


import foo vs. python -m foo

2008-10-28 Thread Simon Bierbaum

Hi all,

what is the difference between saying "import foo" in an interactive  
prompt and starting one using "python -m foo"? The -m switch is not  
covered in the man page, is it even officially supported? I'm asking  
because one of my modules fails on import in the second version but  
succeeds in the first.


Thanks, Simon

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


Re: Database specialized in storing directed graphs?

2008-10-28 Thread flyingfrog
Don't really know if this will be useful but i'd try pytables:
http://www.pytables.org/moin
it deals very well with every kind of hierarchical data sets, doesn't
matter the size.
It will let you load only significant data, and you'll be able to
query your data.
It's built on top of HDF5 libraries but exposes a very friendly
pythonic interface.
Surely you'll still have to implement all of the graph logic yourself
but this could be a good starting point.
Hope it helps

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


Re: Return lines in file that match string

2008-10-28 Thread Chris Rebert
On Tue, Oct 28, 2008 at 11:37 AM, Travis Kirstine
<[EMAIL PROTECTED]> wrote:
> I am new to python and could use some help with a fairly easy task.  I
> would like to return all lines in a file that have the string
> '' to a list.
>

from __future__ import with_statement

with open('path/to/file') as f:
desired_lines = [line.strip() for line in f if "" in line]

For your reference, that uses the "with statement" and "list comprehensions".
It would also be advisable for you to read through the fine tutorial
at: http://docs.python.org/tutorial/index.html

Cheers,
Chris

> Regards,
>
> --
> Travis K.
>
> Toronto, Canada
> 
> "She knows there's no success like failure
> And that failure's no success at all."
> -Bob Dylan-
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Contracts for Python

2008-10-28 Thread Chris Rebert
On Tue, Oct 28, 2008 at 10:47 AM, Paulo J. Matos <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I am wondering if there is any work on contracts for Python. I could
> only find PEP316, however, I am wondering if there is any official
> support for it already (tools I mean), and if it is or if it will be
> officially supported in any of the next releases of Python.

No, it's not officially supported and there are currently no plans to
support it, though as PEP 316 is Deferred and not Rejected, it's
theoretically possible that it could be Accepted and added to the
language some day.
At any rate, there are several third-party libraries available that
allow you to use Design by Contract in Python, albeit without
language-level support.
See http://pypi.python.org/pypi?%3Aaction=search&term=contract&submit=search
for a list

Cheers,
Chris

>
> Cheers,
> --
> Paulo Jorge Matos - pocmatos at gmail.com
> Webpage: http://www.personal.soton.ac.uk/pocm
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: 404 not found on for Python 2.6 Itanium

2008-10-28 Thread Christian Heimes

[EMAIL PROTECTED] wrote:

Anyone know where else I can download 2.6 for x64 windows?


x64 is AMD64 aka X64_86 and not the Itanium version. Itanium is IA64. We 
don't build Python for IA64 anymore.


Christian

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


Re: Unit Testing: a couple of questions

2008-10-28 Thread Marco Bizzarri
On Tue, Oct 28, 2008 at 3:56 PM, Emanuele D'Arrigo <[EMAIL PROTECTED]> wrote:
> Hi everybody,
>
> I'm just having a go with Unit Testing for the first time and my
> feeling about it in short is: Neat!
>
> I'm a bit worried about the time it's taking me to develop the tests
> but after only a day or so I'm already much faster than when I started
> with it and the code is already much improved in terms of robustness.
> A couple of "philosophical" questions have emerged in the process.
>
> 1) Granularity
> Given a simple class
>
> class myClass():
>def __init__(self, data):
>__data = data;
>
>def getData(self):
>return __data
>
>def setData(self, data):
> __data = data
>
> I've been wondering: where do I stop in terms of testing for things
> that could go wrong? In this case for example, it might be reasonable
> to expand the class to make sure it only receives integers and test
> accordingly, i.e.:
>
>def setData(self, data):
>try:
> data = int(data)
>except ValueError:
>raise ValueError("Argument received cannot be converted to
> integer: " + data)
>
> But would it be reasonable to test also for the assignment operators?
> After all, if, for some strange reason, there isn't enough memory,
> couldn't even __data = data potentially fail?
>
> 2) Testing in isolation
> I'm not entirely clear on this point. I can see how I need to test
> each path of the program flow separately. But should a test -only-
> rely on the object being tested and mock ones in supporting roles?


IMHO, don't do that. Mocking everything and anything can become
incredibly complex. Wait to do it. As usual, experience plays a big
role on this; start mocking on what will make your test *simpler* and
easier to write and to understand, for you today and for you tomorrow
(and remember that you tomorrow is a different one from you today).

Before writing a mock, ask yourself: can I use a real object? The both
of you know the behaviour of the real object; you hadn't to think
about what some you yesterday wrote inside it.


> I.e. would this be wrong if SupportObject is not a mockup?
>
> def testObjectToBeTested _forReallyBadError(self):
>supportObject = SupportObject()
>objectToBeTested = ObjectToBeTested()
>result =  objectToBeTested.addSupportObject(supportObject)
>self.failIf(result != kSuccess, "Support Object could not be
> added!")

> I can see how if the SupportObject class had a bug introduced in it,
> this test would fail even though it has nothing to do with the
> ObjectToBeTested class.

> However, creating mock objects can be quite an
> overhead (?). I'm wondering if there is a threshold, even a fuzzy one,
> under which it isn't worth doing and a test like the one above is
> "good enough".



> What do you guys think?
>
> Manu
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Creating mock objects in python can be quite easy; even more if you're
on a greenfield project, and not with some thousands or ten of
thousands of legacy code around. So, the burden is not that much. If
you end using more than a couple of mocks in one test, most probably
there is something wrong, and it would be better if you reconsider
your design.

Also, think if you really need mock object in your tests. Can't it be
easier to use a "real" object? Unless creating it is really complex,
and making it show the behaviour you want it to show is hard or
unclear, you can use it directly.

You wont' use a mock for a datetime object, am I right?

Moreover, don't fight your problem (integrating your objects) at the
wrong level (unit testing); you'll need integration tests, where you
use your *REAL* object end-to-end, maybe even talking with other
subsystems, in order to check that you are working for real. And
there,maybe, you could discover that your discipline failed and you
didn't changed the signature of the method of a mock object after you
changed it in the real one. It happens, so, put your tests in place to
catch this problem.

Regards
Marco

-- 
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/
--
http://mail.python.org/mailman/listinfo/python-list


Return lines in file that match string

2008-10-28 Thread Travis Kirstine
I am new to python and could use some help with a fairly easy task.  I
would like to return all lines in a file that have the string
'' to a list.

Regards,

-- 
Travis K.

Toronto, Canada

"She knows there's no success like failure
And that failure's no success at all."
-Bob Dylan-

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


zipping a directory

2008-10-28 Thread Callie Bertsche
Hey Python-ers,
I apologize if this is covered in the archives; I think I saw one
instance of it but I couldn't get the solution to work out.
I'm working on zipping an entire directory to one zip file. I can zip a
flat group of files, but when my code encounters a hierarchy of folders,
for some reason it starts to zip everything on my desktop (instead of
everything inside one folder directory on desktop).  Then it goes into
an infinite loop and barfs.
 
Here's what I have so far:
 
import zipfile
import glob, os
 
def main():
 
directory = "test\*"
(success,filename)=createZipFile(directory);
if success == 1:
print "Operation completed. All files written to zip."
else:
print "Operation failed."
 
def createZipFile(directory):
zippedHelp = zipfile.ZipFile("help.zip", "w" ) 

for entity in directory:
if os.path.isfile(entity):
 
zippedHelp.write(entity,os.path.basename(entity),zipfile.ZIP_DEFLATED)
else:
addFolderToZip(zippedHelp,entity)
 
zippedHelp.close()
return (1,zippedHelp)
 

def addFolderToZip(zippedHelp,folder):
 
for file in folder:
if os.path.isfile(file):
zippedHelp.write(file, os.path.basename(file),
zipfile.ZIP_DEFLATED)
elif os.path.isdir(file):
addFolderToZip(zippedHelp,file)
 

main()
 
Thanks for any help!! :)
Callie
--
http://mail.python.org/mailman/listinfo/python-list


Specifying an IP address for outbound connections?

2008-10-28 Thread erikcw
Python seems to default to the main system IP for outbound connections
(such as urllib), but I want to bind to one of my other IPs for
outbound connections.

Any ideas?

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


404 not found on for Python 2.6 Itanium

2008-10-28 Thread csgrimes1
Anyone know where else I can download 2.6 for x64 windows?

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


Contracts for Python

2008-10-28 Thread Paulo J. Matos
Hi all,

I am wondering if there is any work on contracts for Python. I could
only find PEP316, however, I am wondering if there is any official
support for it already (tools I mean), and if it is or if it will be
officially supported in any of the next releases of Python.

Cheers,
-- 
Paulo Jorge Matos - pocmatos at gmail.com
Webpage: http://www.personal.soton.ac.uk/pocm
--
http://mail.python.org/mailman/listinfo/python-list


Re: Web crawler on python

2008-10-28 Thread Alex
On Oct 26, 9:54 pm, sonich <[EMAIL PROTECTED]> wrote:
> I need simple web crawler,
> I found Ruya, but it's seems not currently maintained.
> Does anybody know good web crawler on python or with python interface?

You should try Orchid http://pypi.python.org/pypi/Orchid/1.1
 or you can have a look at my project on launchpad
https://code.launchpad.net/~esaurito/jazz-crawler/experimental.
It's a single site crawler but you can easily modified it.

Bye.

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


Re: dictionary

2008-10-28 Thread D'Arcy J.M. Cain
On Tue, 28 Oct 2008 10:41:20 -0400
Steve Holden <[EMAIL PROTECTED]> wrote:
> Sorry, you are thinking of bistable multivibrators. I was talking about
> the Wiliams tube store:
> 
>   http://ed-thelen.org/comp-hist/SEAC-Williams-tube-desc.html

Very cool.  Yes, I was thinking of something else.  However, Williams'
first attempt at storing memory on a CRT (1946) stored only one bit.

> I don't ever remember programming to cope with equipment failure,
> however. Did you make that bit up?

No, I read it many years ago.  However, I cannot find the reference
(try searching for anything computer related and "shopping cart.") so I
cannot check the story's veracity.

-- 
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: Unit Testing: a couple of questions

2008-10-28 Thread Mel
Emanuele D'Arrigo wrote:
> I'm a bit worried about the time it's taking me to develop the tests
> but after only a day or so I'm already much faster than when I started
> with it and the code is already much improved in terms of robustness.
> A couple of "philosophical" questions have emerged in the process.
[ ... ]
> But would it be reasonable to test also for the assignment operators?
> After all, if, for some strange reason, there isn't enough memory,
> couldn't even __data = data potentially fail?

I would say, don't test for anything you can't fix.  If you have (what the
agile-programming people call) a "user story" describing how your program
will recover from out-of-memory, then test whether that recovery is carried
out.  Otherwise forget it.

Best think of unit tests as your sub-program spec written in a different
form: i.e. as executable programs.  You don't test for "things that can go
wrong", you test for behaviour that your program must exhibit.
> 
> 2) Testing in isolation
> I'm not entirely clear on this point. I can see how I need to test
> each path of the program flow separately. But should a test -only-
> rely on the object being tested and mock ones in supporting roles?
> I.e. would this be wrong if SupportObject is not a mockup?
> 
> def testObjectToBeTested _forReallyBadError(self):
> supportObject = SupportObject()
> objectToBeTested = ObjectToBeTested()
> result =  objectToBeTested.addSupportObject(supportObject)
> self.failIf(result != kSuccess, "Support Object could not be
> added!")
> 
> I can see how if the SupportObject class had a bug introduced in it,
> this test would fail even though it has nothing to do with the
> ObjectToBeTested class. However, creating mock objects can be quite an
> overhead (?). I'm wondering if there is a threshold, even a fuzzy one,
> under which it isn't worth doing and a test like the one above is
> "good enough".

I have heard people talk, matter-of-factly about support objects that got so
complicated that they needed unit tests of their own.  Of course, anything
can be done in a ridiculous way, but good comprehensive unit tests give you
the eternal assurance that your program is behaving properly, through all
maintenance and modification.  That's worth a lot.

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


Re: Python suitable for Midi ?

2008-10-28 Thread Derek Martin
On Tue, Oct 28, 2008 at 06:54:57PM +0200, Chuckk Hubbard wrote:
> The problem I've run into is that I can't set the audio to a higher
> priority than the GUI (Tkinter).  If I move the mouse over the app, no
> matter what, I get audio dropouts.  AFAICT this is the same for all
> Python, regardless of what modules one uses: you can't assign system
> priorities to different threads.  If you're planning to pipe MIDI to
> another app for playback, maybe it won't be an issue for you.

FWIW...  You could take your own advice, and devide your application
in two: one process manages the GUI, and the second is a back-end
process that plays the MIDI.  Your GUI can even launch the back end,
which will inherit the priority of the GUI, after which the GUI can
reduce its own priority (the priority of the back end will not be
affected by the change)...


-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpIZwcieNKvY.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: Extracting name strings from assigned objects

2008-10-28 Thread Simon Brunning
2008/10/28 Shannon Mayne <[EMAIL PROTECTED]>:
> I would like to ask how one might obtain the assigned name of an
> assigned object as a string.

That's in the FAQ: .

-- 
Cheers,
Simon B.
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the instance reference of an object

2008-10-28 Thread Dale Roberts
On Oct 28, 2:33 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Tue, 28 Oct 2008 01:16:04 -0200, Dale Roberts <[EMAIL PROTECTED]>  
> escribió:
>
>
>
> > So, then, what to tell a C++ programmer about how Python passes  
> > arguments? You say: tell them Python only passes by value. I disagree,  
> > because I think that would confuse them. Rather than try to map C++  
> > conventions onto Python, I think it is more useful to just tell them how  
> > it really works. Maybe a few statements like this:
>
> >    All values in Python are objects, from simple integers up to complex
> >    user-defined classes.
>
> >    An assignment in Python binds a variable name to an object. The
> >    internal "value" of the variable is the memory address of an object,
> >    and can be seen with id(var), but is rarely needed in practice.
>
> >    The "value" that gets passed in a Python function call is the address
> >    of an object (the id()).
>
> >    When making a function call, myfunc(var), the value of id(var) can
> >    never be changed by the function.
>
> > Not sure if these are the best. To get into much more detail, you have  
> > to start explaining mutable and immutable objects and such.
>
> I don't think the above explanation is desirable, nor needed. Objects in  
> Python don't have an "address" - it's just a CPython implementation  
> detail. The fact that id() returns that address is just an implementation  
> detail too. The calling mechanism should be explained without refering to  
> those irrelevant details.
>
> --
> Gabriel Genellina


I agree that it was a feeble and ill-advised attempt, and would like
to strike those lines from the record...

But the rest of the post is strong and accurate, I think, and coming
from a mainly C/C++ background, visualizing these object references
being passed around does help improve my understanding of Python's
*behavior* (and it apparently helps Joe too).

[May I refer to you as "Joe The Programmer" in my rhetoric? Are you a
"licensed" programmer making over $250K/year? ;-)]

If asked by a C++ programmer about Python's argument passing, I will
go with the Pass By Object explanation (which is why I called my
Python routine above ByObject()). Although there will be more
'splaining to do, at least it will give the C++ programmer pause, and
help them realize that there is something a little different that they
need to stop and try to understand.

If I just say "Python is Pass By Value, Period" without further
explanation, they will expect things to work as in my ByValue()
example above (where the caller's object contents cannot be changed),
and that is incorrect. And they may go and tell someone else, without
the detailed explanation, that "Dale says it's Pass By Value", and
I'll get blamed when their function surprisingly changes the contents
of the caller's object.

If I just say "Python is Pass By Reference", that is wrong too. As Joe
The Programmer points out, they will expect that the calling
*variable* itself can be changed, and that is wrong too.

They need to understand that Python does not map neatly, directly, and
completely to their C++ experience of Pass By Value (which involves
copying a variable), or Pass By Reference (which involves taking the
address of a variable).

The Key Concept in for a C++ programmer looking at Python is that,
unlike in C++, **Variables Cannot "Contain" Values**. All values in
Python are objects, and all variables in Python simply point to, or
are bound to, or refer to, these objects. The variables themselves do
not contain the objects - if you must (like Joe the Programmer), you
can say that all Python variables *contain* an object reference, and
it is these references that are passed around. Unlike C++, an object
reference is the ONLY thing that a Python variable can contain. A
function parameter is always passed as a reference to an object, not a
reference to a variable, or a copy of a variable or object.

And that is where the confusion arises. When people say ByRef or
ByVal, they usually mean by Reference to (address) or Value of (copy)
the *contents* of the passed variable. But, PYTHON VARIABLES DO NOT
"CONTAIN" VALUES (sorry for the shouting, but it is the most important
point here), so ByRef and ByVal lose their commonly accepted meanings.

In C++, ByValue requires a copy (and Python does not copy). In C++,
ByReference requires the address of a *variable* (an "lvalue"), and
variables do not have accessible addresses in Python.

ByObject, in contrast, requires neither (unless, like Joe The
Programmer, you consider the "value" of a variable to be the id(),
which is not what most people expect when they say "x=5" - they expect
the "value" to be 5). ByObject simply passes an object reference. Nice
and neat.

I think with that explanation (avoiding any mention of memory, or
object id's) will help them understand things better.

As you point out, this is a very old topic, but I guess each newcomer
to Python has to figure i

Re: Python suitable for Midi ?

2008-10-28 Thread Chuckk Hubbard
I'm writing a sequencer in Python, although it's microtonal and not
MIDI.  I'm using the Python bindings for the Csound API, all the
timing, MIDI, OSC, etc. stuff, essentially all but the GUI
capabilities, having been done by the Csound developers already.
Documentation is here and there, and Csound is another language to
learn, but it's one way to go about it.
I'm about to try to recode my app to calculate tempo and note timing
internally and send real-time notes to Csound, instead of having
Csound do it all.
The problem I've run into is that I can't set the audio to a higher
priority than the GUI (Tkinter).  If I move the mouse over the app, no
matter what, I get audio dropouts.  AFAICT this is the same for all
Python, regardless of what modules one uses: you can't assign system
priorities to different threads.  If you're planning to pipe MIDI to
another app for playback, maybe it won't be an issue for you.
Good luck!

-Chuckk

On Tue, Oct 28, 2008 at 11:26 AM, Protocol <[EMAIL PROTECTED]> wrote:
> Hello all
>
> Is Python suitable for building a multi-track midi sequencer (with a
> gui), that would run on windows / mac ? I fail to find sufficient
> information on this, being a newbie and all. Furthermore, i found
> references on Python not being really able of multi-threading, that
> further adds to the confusion.
>
> Please assist.
>
> Panos
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



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


Re: Database specialized in storing directed graphs?

2008-10-28 Thread bearophileHUGS
Sorry Carl Banks for the answering delay, there are problems in Google
Groups.

> This is not to study graph theory; I'm using the graph to represent a
> problem domain.  The graphs could be arbitrarily large, and could
> easily have millions of nodes, and most nodes have a substantial
> amount of data associated with them.  Obviously I don't want a whole
> such graph in memory at once, so libraries the just deal with in-
> memory graphs are out.

It doesn't sound a problem for modern PCs.
I think you can keep the whole graph topology in RAM, and the node
data on disk (for example in a file or in DB). The topology is
represented by the arcs (you have said nothing regarding arc data, so
I assume it's absent) and the nodes (in RAM you just need a 32 bit
unsigned integer to represent the index of the node that is stored on
disk. If memory becomes tight you can use just 3 bytes (2 ^ 24 = 16
millions different nodes) for the nodes, but generally the memory
required for the nodes is very little compared to the memory necessary
to store the arcs).

You haven't said how many arcs there are, total or average for node,
and if such arcs are directed or undirected.

Anyway, using my Graph class (that stores each arc twice), this takes
about 1 minute and 1.3 GB of RAM (1 million nodes, 10 arcs for node):

from graph import Graph
from random import randrange
g = Graph()
N = 100
g.addNodes(xrange(N))
for i in xrange(N * 10):
g.addArc(randrange(N), randrange(N))


You have said "could easily have millions of nodes", and every arc may
have tens of arcs or more.
("arbitrarily large" is an unsolvable problem because there are always
limits in your program, there's no program that's able to work on an
"arbitrarily large" dataset), so Python data structures become too
much costly for the RAM. With a lower level language like D/C/C++ you
can manage a bigger graph. You can use Boost Graph, but a homemade
graph structure may suffice too for your purposes.

For the problem you have explained I think a very simple graph
representation may suffice: an array of integer pairs (starting_index,
len) (starting_index can be a pointer too), where len is the number of
outbound arcs of the node n, plus an array of indexes/pointers that
list the outbound arcs. If memory gets tight you can split this second
array in two, and use an array of bytes for the lengths (if you have
more than 256 outbound arcs you may need a short). Note that if you
use indexes then a Python array.array (or Numpy) suffices.

In this situation if nnodes = 10_000_000 and narcs/node = 40 (total
nodes = 40 * 10_000_000):
nnodes * narcs * 4 + nnodes * (4 + 4) = 1_680_000_000 bytes, that is
often available on modern PCs.

On a 64 bit machine the indexes take the same memory, but pointers
twice that.

In "memory saving" mode:
nnodes * narcs * 3 + nnodes * (3 + 1) = 1_240_000_000 bytes.

A more handy compromise is:
nnodes * narcs * 3 + nnodes * (4 + 4) = 1_280_000_000 bytes.

> But then what happens if something changes elsewhere.

Regarding the data structure, if you use arrays like I have explained,
if your updates aren't much frequent then you can allocate small extra
arrays to store more arcs coming out a node (but to do this you
probably may enjoy using pointers instead of indexes). When you have a
lot of updated you can save all to disk, and then regenerate the whole
data structure.

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


Re: Database specialized in storing directed graphs?

2008-10-28 Thread George Sakkis
On Oct 27, 8:32 pm, Carl Banks <[EMAIL PROTECTED]> wrote:

> I was wondering if anyone had any advice on this.
>
> This is not to study graph theory; I'm using the graph to represent a
> problem domain.  The graphs could be arbitrarily large, and could
> easily have millions of nodes, and most nodes have a substantial
> amount of data associated with them.  Obviously I don't want a whole
> such graph in memory at once, so libraries the just deal with in-
> memory graphs are out.
>
> I know I could implement this with a relational DB, and I'd be fine
> with a library that was built on top of one.  But I'm hoping for
> specialzed behavior useful for graphs.

If you're looking for FOSS, the Boost graph library [1] or its
parallel extension [2] is probably your best bet; it also comes with
Python bindings but they are not maintained any more. For commercial
solutions, Star-P [3] seems an interesting platform, with bindings to
Matlab and Python. Freebase [4] is apparently built on a special graph
database but unfortunately only the stored data are available, not the
DB source code.

George

[1] http://www.boost.org/doc/libs/1_36_0/libs/graph/doc/index.html
[2] http://www.osl.iu.edu/research/pbgl/
[3] http://www.interactivesupercomputing.com/success/sparsematrices.php
[4] http://www.freebase.com/help/faq#q7
--
http://mail.python.org/mailman/listinfo/python-list


Re: using modules in destructors

2008-10-28 Thread MRAB
On Oct 27, 5:40 pm, Scott David Daniels <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > It seems to me that deleting local instances before imported modules
> > would solve the problem. Is it not possible for the interpreter to get
> > this right? Or are there cases where this would break stuff.
>
> > It seems rather unpythonic for the __del__() method to become
> > unpredictable at exit.
>
> What would you have different? How would you define the end of a
> Python program?  Would all modules and globals remain, and then
> darkness?  Would the modules get removed in alphabetical order?
> 
>
I suppose the modules could be removed in the reverse order in which
they were loaded, excepting cross-references (two objects from
different modules referring to their own modules and each other). Even
neglecting modules, if two objects have destructors and refer to each
other, which should be collected first?

> The core of the problem is that there is ((and possibly can be)
> no good definition of what happens after SystemExit leaves the
> __main__ module.
>
> --Scott David Daniels
> [EMAIL PROTECTED]

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


Re: explanation of values, references, assignment, and method calls

2008-10-28 Thread Bruno Desthuilliers

Joe Strout a écrit :
I've tried to write up this topic in a clear, step-by-step manner, with 
the help of diagrams and short examples from several different OOP 
languages.  I hope it will help clear up the confusion that seems to be 
pervading the Python community


May I suggest http://effbot.org/zone/call-by-object.htm ?


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


Extracting name strings from assigned objects

2008-10-28 Thread Shannon Mayne
I would like to ask how one might obtain the assigned name of an
assigned object as a string. I would like to use object names as an
algorithmic input.


To demonstrate... So if i have:

>>foo = {}

what can I do to the object 'foo' so as to return the string 'foo'?

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


Re: Unit Testing: a couple of questions

2008-10-28 Thread Orestis Markou
For the first bit, a colleague has recently asked the philosophical
question, "How do you test what happens when the power goes down?" :)

In other words, only test the bits that your code does. If you want to
provide type checking, then yes, you have to test that.

It's fair to assume that everything that is *not* your code will work
as expected. If you do find 3rd-party bugs that affect you, then you
should write a unit test that exposes that bug. When they fix it, your
unittest fails, prompting you to remove any workarounds you had.

On Tue, Oct 28, 2008 at 2:56 PM, Emanuele D'Arrigo <[EMAIL PROTECTED]> wrote:
> Hi everybody,
>
> I'm just having a go with Unit Testing for the first time and my
> feeling about it in short is: Neat!
>
> I'm a bit worried about the time it's taking me to develop the tests
> but after only a day or so I'm already much faster than when I started
> with it and the code is already much improved in terms of robustness.
> A couple of "philosophical" questions have emerged in the process.
>
> 1) Granularity
> Given a simple class
>
> class myClass():
>def __init__(self, data):
>__data = data;
>
>def getData(self):
>return __data
>
>def setData(self, data):
> __data = data
>
> I've been wondering: where do I stop in terms of testing for things
> that could go wrong? In this case for example, it might be reasonable
> to expand the class to make sure it only receives integers and test
> accordingly, i.e.:
>
>def setData(self, data):
>try:
> data = int(data)
>except ValueError:
>raise ValueError("Argument received cannot be converted to
> integer: " + data)
>
> But would it be reasonable to test also for the assignment operators?
> After all, if, for some strange reason, there isn't enough memory,
> couldn't even __data = data potentially fail?
>
> 2) Testing in isolation
> I'm not entirely clear on this point. I can see how I need to test
> each path of the program flow separately. But should a test -only-
> rely on the object being tested and mock ones in supporting roles?
> I.e. would this be wrong if SupportObject is not a mockup?
>
> def testObjectToBeTested _forReallyBadError(self):
>supportObject = SupportObject()
>objectToBeTested = ObjectToBeTested()
>result =  objectToBeTested.addSupportObject(supportObject)
>self.failIf(result != kSuccess, "Support Object could not be
> added!")
>
> I can see how if the SupportObject class had a bug introduced in it,
> this test would fail even though it has nothing to do with the
> ObjectToBeTested class. However, creating mock objects can be quite an
> overhead (?). I'm wondering if there is a threshold, even a fuzzy one,
> under which it isn't worth doing and a test like the one above is
> "good enough".
>
> What do you guys think?
>
> Manu
> --
> http://mail.python.org/mailman/listinfo/python-list
>



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


explanation of values, references, assignment, and method calls

2008-10-28 Thread Joe Strout
I've tried to write up this topic in a clear, step-by-step manner,  
with the help of diagrams and short examples from several different  
OOP languages.  I hope it will help clear up the confusion that seems  
to be pervading the Python community (and which is far more rare in  
the other language communities, despite them having the very same  
semantics).


   

It's still a bit rough around the edges, and probably contains some  
typos, as I finished it late last night and haven't even had a chance  
to proof-read it yet.  But I may not get another chance to work on it  
for a few days, so I wanted to get it out there now.  I welcome your  
feedback.


Best,
- Joe


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


Re: 2.6, 3.0, and truly independent intepreters

2008-10-28 Thread Andy O'Meara
On Oct 27, 10:55 pm, Glenn Linderman <[EMAIL PROTECTED]> wrote:


> And I think we still are miscommunicating!  Or maybe communicating anyway!
>
> So when you said "object", I actually don't know whether you meant
> Python object or something else.  I assumed Python object, which may not
> have been correct... but read on, I think the stuff below clears it up.
>
>
> Then when you mentioned thousands of objects, I imagined thousands of
> Python objects, and somehow transforming the blob into same... and back
> again.  

My apologies to you and others here on my use of "objects" -- I'm use
the term generically and mean it to *not* refer to python objects (for
the all the reasons discussed here).  Python only makes up a small
part of our app, hence my habit of "objects" to refer to other APIs'
allocated and opaque objects (including our own and OS APIs).  For all
the reasons we've discussed, in our world, python objects don't travel
around outside of our python C modules -- when python objects need to
be passed to other parts of the app, they're converted into their non-
python (portable) equivalents (ints, floats, buffers, etc--but most of
the time, the objects are PyCObjects, so they can enter and leave a
python context with negligible overhead). I venture to say this is
pretty standard when any industry app uses a package (such as python),
for various reasons:
   - Portability/Future (e.g.  if we do decode to drop Python and go
with Lua, the changes are limited to only one region of code).
   - Sanity (having any API's objects show up in places "far away"
goes against easy-to-follow code).
   - MT flexibility (because we always never use static/global
storage, we have all kinds of options when it comes to
multithreading).  For example, recall that by throwing python in
multiple dynamic libs, we were able to achieve the GIL-less
interpreter independence that we want (albeit ghetto and a pain).



Andy



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


Re: list versions of all installed modules

2008-10-28 Thread Scott David Daniels

John [H2O] wrote:
Is there a quick way to list the version of each installed module? 


import sys
for name, module in sorted(sys.modules.items()):
if hasattr(module, '__version__'):
print name, module.__version__

Of course if you add __VERSION__, VERSION, and version, you
may get more.

--Scott David Daniels
[EMAIL PROTECTED]

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


Re: parse a table in HTML page.

2008-10-28 Thread Thomas Guettler
Have you looked at beautiful soup?
http://www.crummy.com/software/BeautifulSoup/

antonio_wn8 schrieb:
> Hi all,
> I have a need to read and parse a table in HTML page.
> 
> I’m using the following script:
> http://trac.davidgrant.ca/browser/src/python/misc/siteuptime/TableParser.py
> 
> It works fine  aside from  link in href.
> 
> Example:
> 
> String to parse:
> elognormal text
> 
> Output:
> [[['elog', 'normal text']]]
> 
> as you can see it misses the info about href...
> how can get this information 'vaffa.html'?



-- 
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
--
http://mail.python.org/mailman/listinfo/python-list


Re: Unit Testing: a couple of questions

2008-10-28 Thread Antoine De Groote
I'm wondering if don't want your class to look something like this:

class myClass():
def __init__(self, data):
self.__data = data

def getData(self):
return self.__data

def setData(self, data):
 self.__data = data

For the rest I'll let the experts argue, I don't have a lot of
experience with unit testing either... Shame on me! ;-)

Regards,
antoine

Emanuele D'Arrigo wrote:
> Hi everybody,
> 
> I'm just having a go with Unit Testing for the first time and my
> feeling about it in short is: Neat!
> 
> I'm a bit worried about the time it's taking me to develop the tests
> but after only a day or so I'm already much faster than when I started
> with it and the code is already much improved in terms of robustness.
> A couple of "philosophical" questions have emerged in the process.
> 
> 1) Granularity
> Given a simple class
> 
> class myClass():
> def __init__(self, data):
> __data = data;
> 
> def getData(self):
> return __data
> 
> def setData(self, data):
>  __data = data
> 
> I've been wondering: where do I stop in terms of testing for things
> that could go wrong? In this case for example, it might be reasonable
> to expand the class to make sure it only receives integers and test
> accordingly, i.e.:
> 
> def setData(self, data):
> try:
>  data = int(data)
> except ValueError:
> raise ValueError("Argument received cannot be converted to
> integer: " + data)
> 
> But would it be reasonable to test also for the assignment operators?
> After all, if, for some strange reason, there isn't enough memory,
> couldn't even __data = data potentially fail?
> 
> 2) Testing in isolation
> I'm not entirely clear on this point. I can see how I need to test
> each path of the program flow separately. But should a test -only-
> rely on the object being tested and mock ones in supporting roles?
> I.e. would this be wrong if SupportObject is not a mockup?
> 
> def testObjectToBeTested _forReallyBadError(self):
> supportObject = SupportObject()
> objectToBeTested = ObjectToBeTested()
> result =  objectToBeTested.addSupportObject(supportObject)
> self.failIf(result != kSuccess, "Support Object could not be
> added!")
> 
> I can see how if the SupportObject class had a bug introduced in it,
> this test would fail even though it has nothing to do with the
> ObjectToBeTested class. However, creating mock objects can be quite an
> overhead (?). I'm wondering if there is a threshold, even a fuzzy one,
> under which it isn't worth doing and a test like the one above is
> "good enough".
> 
> What do you guys think?
> 
> Manu
--
http://mail.python.org/mailman/listinfo/python-list


Re: list versions of all installed modules

2008-10-28 Thread Gerhard Häring

John [H2O] wrote:
Is there a quick way to list the version of each installed module? 


$ sudo easy_install yolk
$ yolk -l

-- Gerhard

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


Re: dictionary

2008-10-28 Thread Hendrik van Rooyen

 "Steve Holden" <[EMAIL PROTECTED]> wrote:

> 
> I don't ever remember programming to cope with equipment failure,
> however. Did you make that bit up?

Not at the bit level in my case - but I do remember doing silly things 
like multiplying after a divide (all integer arithmetic) to make sure the 
answer was right.

And we routinely had a checksum record at the end of all files that one
would check when reading in - it was made by adding everything in every
record as a big binary number - almost a kind of vertical parity. And it 
caught stuff out too.  Then you would just re run, and the error would 
often go away.

It has left me permanently scarred.

- Hendrik

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


Re: Need advice/suggestion for small project

2008-10-28 Thread Scott David Daniels

AEB wrote:

Hello, my supervisor has requested that I write a program to display
2D waves in 3D using Python.  Basically I have a time series file that
has the heights of the wave over time steps, and I want to extrude
these waves lengthwise so that they appear in a 3D manor.  I am not
very familiar with Python development, could someone point me in a
good direction, and maybe comment if this is a little to ambitious for
a first time python user?

I do have moderate programming experience in other languages (java, c/c
++, vb.net).

You might want to take a look at VPython.  It is probably the easiest
way to get to 3-D in Python.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python barcode decoding

2008-10-28 Thread Kirk Strauser
At 2008-10-24T17:05:25Z, Robocop <[EMAIL PROTECTED]> writes:

> Does anyone know of any decent (open source or commercial) python
> barcode recognition tools or libraries.  I need to read barcodes from
> pdfs or images, so it will involve some OCR algorithm.  I also only
> need to read the code 93 symbology, so it doesn't have to be very
> fancy.  The most important thing to me is that it outputs in some
> python friendly way, or ideally that it is written in python.  Any
> tips would be great!

How precise are these images?  I mean, are they computer-generated, or
scanned in from other material?

If they're nice and crisp, I wrote a simple and pretty quick decoder at 
http://pypi.python.org/pypi/Daycos/1.0 .  Look in the
Daycos.Imageproc.Barcode module.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the instance reference of an object

2008-10-28 Thread Joe Strout

On Oct 27, 2008, at 11:28 PM, Gabriel Genellina wrote:

En Tue, 28 Oct 2008 00:58:10 -0200, greg  
<[EMAIL PROTECTED]> escribió:



Let's look at the definitions of the terms:

(1) Call by value: The actual parameter is an expression. It is
   evaluated and the result is assigned to the formal parameter.
   Subsequent assignments to the formal parameter do not affect
   the actual parameter.

(2) Call by reference: The actual parameter is an lvalue. The
   formal parameter becomes an alias for the actual parameter,
   so that assigning to the formal parameter has the same
   effect as assigning to the actual parameter.

Seems to me that (1) describes exactly how parameter passing
works in Python. So why insist that it's *not* call by value?


Greg is right on the money here.  It really is as simple as that.

Those definitions are only applicable to unstructured, primitive  
types, where the only relevant operations are "get value" and  
"assign value".


Nonsense.  They apply to any type.  See here for an introduction to  
the topic in VB.NET:


  http://www.homeandlearn.co.uk/net/nets9p4.html

The example shown uses an Integer, but guess what -- you can pass ANY  
type either ByRef or ByVal, including object references (which are, of  
course, quite common in VB.NET).  The default is ByVal, which means  
that (as in Python) the actual parameter is an expression, and no  
assignments to it can affect the actual parameter.  It DOES NOT MATTER  
that both the formal parameter and the actual parameter may refer to  
the same object, and if that object is mutable, you can mutate that  
object.


But ByRef is another option, and if you pass an object reference  
ByRef, then the formal parameter is an alias for the actual parameter,  
and assignments to it change the actual parameter.  Python doesn't  
have this feature (nor much need for it, given its other features,  
like tuple packing/unpacking).  So, parameter passing in ByRef is  
clearly exactly the same as the default "ByVal" parameter passing in  
VB.NET... as well as Java, RB, C++ (when you remember that an object  
reference in modern languages is like a pointer in C++), and every  
other OOP language I've looked into.


Some of those languages, like Python, don't have different modes, and  
so the language designers had no call to give their one mode a name.   
So let's look at those that did have such a need, like VB.NET and  
REALbasic.  What's the default mode called?  Why, it's "ByVal".  Even  
when that value is an object reference.  This is short for "by value"  
-- it's not short for "by sharing" or "by object" or any other such  
silliness.  No such terms are needed.


There are only the two cases, which Greg quite succinctly and  
accurately described above.  One is by value, the other is by  
reference.  Python quite clearly uses by value.  Parameters are  
expressions that are evaluated, and the resulting value copied into  
the formal parameter, pure and simple.  The continued attempts to  
obfuscate this is pointless and wrong.


Best,
- Joe


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


Re: Unpacking byte strings from a file of unknown size

2008-10-28 Thread Scott David Daniels

Mark wrote:

Thanks I tested your solution and that works.

One of the things that didn't work was
for chunk in myfile.read(10):
info1, info2, info3 = struct.unpack('

this code python interprets as:

data = myfile.read(10)
for chunk in data:
.


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


Re: Question about scope

2008-10-28 Thread Kirk Strauser
At 2008-10-24T01:08:12Z, Pat <[EMAIL PROTECTED]> writes:

> ---> myGlobals.py file:
>
> class myGlobals():
> remote_device_enabled = bool
>
> ---> my initialize.py file:
>
> from myGlobals import *
> def initialize():
> myGlobals.remote_device_enabled = True
>
> ---> my main.py file:
>
> import from myGlobals import *
> RDE =  myGlobals.remote_device_enabled
>
> def main():
> if RDE:# this will not give me the correct value
> process_device()

If you *really* want to organize your settings like this, may I suggest:

---> myGlobals.py file:

# This does nothing more than create a placeholder
remote_device_enabled = None

---> my initialize.py file:

import myGlobals
def initialize():
myGlobals.remote_device_enabled = True

---> my main.py file:

import myGlobals
import initialize
initialize.initialize()

def main():
if myGlobals.remote_device_enabled:
process_device()


-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.5 + sqlite full text search possible?

2008-10-28 Thread Gerhard Häring

Guillermo wrote:

Hi!

Is it possible to use the full-text module of SQLite with the sqlite3
module? I've done a bit of investigation and it seems the stand-alone
distribution of SQLite is compiled without it, 


Yes, though compiling using the amalgamation and defining 
SQLITE_ENABLE_FTS3 helps.



and so does the version bundled with Python.


True.

I'm too lazy to build a SQLite3 DLL with FTS enabled, I'm pretty sure 
those can be found on the net.


But I've just patched pysqlite with one line:

+ext.define_macros.append(("SQLITE_ENABLE_FTS3", "1"))   # 
build with fulltext search enabled


which helped its super-crazy script mkwin32.py to build Windows binaries 
 *on Linux* that fetch the SQLite amalgamation and build Windows 
binaries for Python 2.3, 2.4 and 2.5 (no 2.6, yet).


Just go here 
http://oss.itsystementwicklung.de/download/pysqlite/2.5/2.5.0/win32_fts/


download and install the binary for Python 2.5 and off you go:


from pysqlite2 import dbapi2 as sqlite3

con = sqlite3.connect(":memory:")

# example from SQLite wiki
con.execute("create virtual table recipe using fts3(name, ingredients)")
con.executescript("""
insert into recipe (name, ingredients) values ('broccoli stew', 'broccoli 
peppers cheese tomatoes');
insert into recipe (name, ingredients) values ('pumpkin stew', 'pumpkin 
onions garlic celery');
insert into recipe (name, ingredients) values ('broccoli pie', 'broccoli 
cheese onions flour');
insert into recipe (name, ingredients) values ('pumpkin pie', 'pumpkin 
sugar flour butter');
""")
for row in con.execute("select rowid, name, ingredients from recipe where name match 
'pie'"):
print row


-- Gerhard


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


Re: 2.6, 3.0, and truly independent intepreters

2008-10-28 Thread Andy O'Meara
On Oct 25, 9:46 am, "M.-A. Lemburg" <[EMAIL PROTECTED]> wrote:
> These discussion pop up every year or so and I think that most of them
> are not really all that necessary, since the GIL isn't all that bad.
>

Thing is, if the topic keeps coming up, then that may be an indicator
that change is truly needed.  Someone much wiser than me once shared
that a measure of the usefulness and quality of a package (or API) is
how easily it can be added to an application--of any flavors--without
the application needing to change.

So in the rising world of idle cores and worker threads, I do see an
increasing concern over the GIL.  Although I recognize that the debate
is lengthy, heated, and has strong arguments on both sides, my reading
on the issue makes me feel like there's a bias for the pro-GIL side
because of the volume of design and coding work associated with
considering various alternatives (such as Glenn's "Py*" concepts).
And I DO respect and appreciate where the pro-GIL people come from:
who the heck wants to do all that work and recoding so that a tiny
percent of developers can benefit?  And my best response is that as
unfortunate as it is, python needs to be more multi-threaded app-
friendly if we hope to attract the next generation of app developers
that want to just drop python into their app (and not have to change
their app around python).  For example, Lua has that property, as
evidenced by its rapidly growing presence in commercial software
(Blizzard uses it heavily, for example).

>
> Furthermore, there are lots of ways to tune the CPython VM to make
> it more or less responsive to thread switches via the various sys.set*()
> functions in the sys module.
>
> Most computing or I/O intense C extensions, built-in modules and object
> implementations already release the GIL for you, so it usually doesn't
> get in the way all that often.


The main issue I take there is that it's often highly useful for C
modules to make subsequent calls back into the interpreter. I suppose
the response to that is to call the GIL before reentry, but it just
seems to be more code and responsibility in scenarios where it's no
necessary.  Although that code and protocol may come easy to veteran
CPython developers, let's not forget that an important goal is to
attract new developers and companies to the scene, where they get
their thread-independent code up and running using python without any
unexpected reengineering.  Again, why are companies choosing Lua over
Python when it comes to an easy and flexible drop-in interpreter?  And
please take my points here to be exploratory, and not hostile or
accusatory, in nature.


Andy


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


Re: Python 2.5 + sqlite full text search possible?

2008-10-28 Thread Guilherme Polo
On 10/28/08, Guillermo <[EMAIL PROTECTED]> wrote:
> Hi!
>
>  Is it possible to use the full-text module of SQLite with the sqlite3
>  module? I've done a bit of investigation and it seems the stand-alone
>  distribution of SQLite is compiled without it, and so does the version
>  bundled with Python.
>

Support for loading extensions was only added in pysqlite 2.5, so you
will have to install it.

Python 2.5 and 2.6 comes with pysqlite 2.3.2, future 3.0 will come
with pysqlite 2.4.1.


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


-- 
-- Guilherme H. Polo Goncalves
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to get an object's name as a string?

2008-10-28 Thread Joe Strout

On Oct 28, 2008, at 8:41 AM, Shannon Mayne wrote:


I would like to create objects with algorithmically determined names
based on other object names and use object names for general algorithm
input.


What do you mean by the "name" of an object?  Objects don't generally  
have names, unless you explicitly define a .name property and assign  
them names.


(Variables have names, of course, but a variable isn't an object --  
it's just a reference to an object.  Many variables may refer to the  
same object, so it doesn't make any sense to ask for the name of THE  
variable which may be referring to an object at the moment.)



How would one extract the name of an object from an object instance as
a string.  I would think that it is stored as an attribute of the
object but successive 'dir()' calles haven't found me the attribute
with the namestring.


As noted above, there is no built-in name attribute.  Define one,  
perhaps like this:


class Foo():
 def __init__(name):
   self.name = name

Now your Foo objects have a name attribute, and if "x" is a reference  
to such an object, you would access that as "x.name".


It's still unclear what you intend to do with these, but if at some  
point you want to access objects by their names (from user input or  
whatever), then you'll also need a dictionary to map names to  
objects.  So to your __init__ function, you might add something like  
this:


   name_map[name] = self

where name_map was initialized to {} at the top of the file.  Then you  
can use name_map to look up any object of this class by name.   
Remember that this will keep these objects from automatically  
disappearing when there are no other references (other than the map)  
to them.  If that's a problem, explicitly remove them from the map  
when you know you're done with them, or use weak references.


Best,
- Joe

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


Re: dictionary

2008-10-28 Thread D'Arcy J.M. Cain
On Tue, 28 Oct 2008 10:06:31 -0400
Steve Holden <[EMAIL PROTECTED]> wrote:
> Bizarre though it may sound in this age of integrated circuits there
> really was a storage device that used a cathode ray tube to store (IIRC)
> a kilobit of information. It detected, by the use of a capacitance plate

A kilobit?  One tube would carry one bit.  Imagine the size of today's
computers if we were still using them.  In those days you were lucky to
get 4 Kbytes of core memory.

In those days they would have techs walking back and forth along
pathways inside the memory banks with shopping carts full of tubes
replacing them as they burned out.  Programs had to be prepared to deal
with the fact that bits could go dead at any time and functions would
run multiple times and hold an election to determine the correct answer.

-- 
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


list versions of all installed modules

2008-10-28 Thread John [H2O]

Is there a quick way to list the version of each installed module? 
-- 
View this message in context: 
http://www.nabble.com/list-versions-of-all-installed-modules-tp20204095p20204095.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


ignoring NAs in a dataset with numpy

2008-10-28 Thread mrafi

Hey All,
I am working with numpy 
I have a data set with a lot of nan values, and i want to calculate standard
deviation
is there a direct function to do it for example nansum(or something like
this),
to calculate standard deviation ignoring nan values??
Thanks
Rafi
-- 
View this message in context: 
http://www.nabble.com/ignoring-NAs-in-a-dataset-with-numpy-tp20202823p20202823.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: 2.6, 3.0, and truly independent intepreters

2008-10-28 Thread Greg Ewing

Glenn Linderman wrote:

So your 50% number is just a scare tactic, it would seem, based on wild 
guesses.  Was there really any benefit to the comment?


All I was really trying to say is that it would be a
mistake to assume that the overhead will be negligible,
as that would be just as much a wild guess as 50%.

--
Greg

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


Re: 2.6, 3.0, and truly independent intepreters

2008-10-28 Thread Andy O'Meara
On Oct 27, 4:05 am, "Martin v. Löwis" <[EMAIL PROTECTED]> wrote:
> Andy O'Meara wrote:


>
> > Well, when you're talking about large, intricate data structures
> > (which include opaque OS object refs that use process-associated
> > allocators), even a shared memory region between the child process and
> > the parent can't do the job.  Otherwise, please describe in detail how
> > I'd get an opaque OS object (e.g. an OS ref that refers to memory-
> > resident video) from the child process back to the parent process.
>
> WHAT PARENT PROCESS? "In the same address space", to me, means
> "a single process only, not multiple processes, and no parent process
> anywhere". If you have just multiple threads, the notion of passing
> data from a "child process" back to the "parent process" is
> meaningless.

I know...  I was just responding to you and others here keep beating
the "fork" drum.  I just trying make it clear that a shared address
space is the only way to go.  Ok, good, so we're in agreement that
threads is the only way to deal with the "intricate and complex" data
set issue in a performance-centric application.

>
> > Again, the big picture that I'm trying to plant here is that there
> > really is a serious need for truly independent interpreters/contexts
> > in a shared address space.
>
> I understand that this is your mission in this thread. However, why
> is that your problem? Why can't you just use the existing (limited)
> multiple-interpreters machinery, and solve your problems with that?

Because then we're back into the GIL not permitting threads efficient
core use on CPU bound scripts running on other threads (when they
otherwise could).  Just so we're on the same page, "when they
otherwise could" is relevant here because that's the important given:
that each interpreter ("context") truly never has any context with
others.

An example would be python scripts that generate video programatically
using an initial set of params and use an in-house C module to
construct frame (which in turn make and modify python C objects that
wrap to intricate codec related data structures).  Suppose you wanted
to render 3 of these at the same time, one on each thread (3
threads).  With the GIL in place, these threads can't anywhere close
to their potential.  Your response thus far is that the C module
should release the GIL before it commences its heavy lifting.  Well,
the problem is that if during its heavy lifting it needs to call back
into its interpreter.  It's turns out that this isn't an exotic case
at all: there's a *ton* of utility gained by making calls back into
the interpreter. The best example is that since code more easily
maintained in python than in C, a lot of the module "utility" code is
likely to be in python.  Unsurprisingly, this is the situation myself
and many others are in: where we want to subsequently use the
interpreter within the C module (so, as I understand it, the proposal
to have the C module release the GIL unfortunately doesn't work as a
general solution).

>
> > For most
> > industry-caliber packages, the expectation and convention (unless
> > documented otherwise) is that the app can make as many contexts as its
> > wants in whatever threads it wants because the convention is that the
> > app is must (a) never use one context's objects in another context,
> > and (b) never use a context at the same time from more than one
> > thread.  That's all I'm really trying to look at here.
>
> And that's indeed the case for Python, too. The app can make as many
> subinterpreters as it wants to, and it must not pass objects from one
> subinterpreter to another one, nor should it use a single interpreter
> from more than one thread (although that is actually supported by
> Python - but it surely won't hurt if you restrict yourself to a single
> thread per interpreter).
>

I'm not following you there...  I thought we're all in agreement that
the existing C modules are FAR from being reentrant, regularly making
use of static/global objects. The point I had made before is that
other industry-caliber packages specifically don't have restrictions
in *any* way.

I appreciate your arguments these a PyC concept is a lot of work with
some careful design work, but let's not kill the discussion just
because of that.  The fact remains that the video encoding scenario
described above is a pretty reasonable situation, and as more people
are commenting in this thread, there's an increasing need to offer
apps more flexibility when it comes to multi-threaded use.


Andy




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


Re: How to get an object's name as a string?

2008-10-28 Thread Larry Bates

Shannon Mayne wrote:

I would like to create objects with algorithmically determined names
based on other object names and use object names for general algorithm
input.

How would one extract the name of an object from an object instance as
a string. I would think that it is stored as an attribute of the
object but successive 'dir()' calles haven't found me the attribute
with the namestring.

My thanks!



Once again (there have been many posts on this subject).  Objects can have more 
than one name in Python.  Therefore there is not a one-to-one correspondence 
between an object instance and name(s) that point to it.


Example:

a = myObject()
b = a
c = a

now a, b, c all point to the same object.  How would you define it's "name".

You are certainly free to store a name as an attribute to the object, but the 
linkage would then be purely logical.


Example:

objects = []
objects.append(myObject('a'))
#
# Find object with name == 'a'
#
obj = None
for object in objects:
if object.name == 'a':
   obj = object


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


Unit Testing: a couple of questions

2008-10-28 Thread Emanuele D'Arrigo
Hi everybody,

I'm just having a go with Unit Testing for the first time and my
feeling about it in short is: Neat!

I'm a bit worried about the time it's taking me to develop the tests
but after only a day or so I'm already much faster than when I started
with it and the code is already much improved in terms of robustness.
A couple of "philosophical" questions have emerged in the process.

1) Granularity
Given a simple class

class myClass():
def __init__(self, data):
__data = data;

def getData(self):
return __data

def setData(self, data):
 __data = data

I've been wondering: where do I stop in terms of testing for things
that could go wrong? In this case for example, it might be reasonable
to expand the class to make sure it only receives integers and test
accordingly, i.e.:

def setData(self, data):
try:
 data = int(data)
except ValueError:
raise ValueError("Argument received cannot be converted to
integer: " + data)

But would it be reasonable to test also for the assignment operators?
After all, if, for some strange reason, there isn't enough memory,
couldn't even __data = data potentially fail?

2) Testing in isolation
I'm not entirely clear on this point. I can see how I need to test
each path of the program flow separately. But should a test -only-
rely on the object being tested and mock ones in supporting roles?
I.e. would this be wrong if SupportObject is not a mockup?

def testObjectToBeTested _forReallyBadError(self):
supportObject = SupportObject()
objectToBeTested = ObjectToBeTested()
result =  objectToBeTested.addSupportObject(supportObject)
self.failIf(result != kSuccess, "Support Object could not be
added!")

I can see how if the SupportObject class had a bug introduced in it,
this test would fail even though it has nothing to do with the
ObjectToBeTested class. However, creating mock objects can be quite an
overhead (?). I'm wondering if there is a threshold, even a fuzzy one,
under which it isn't worth doing and a test like the one above is
"good enough".

What do you guys think?

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


parse a table in HTML page.

2008-10-28 Thread antonio_wn8
Hi all,
I have a need to read and parse a table in HTML page.

I’m using the following script:
http://trac.davidgrant.ca/browser/src/python/misc/siteuptime/TableParser.py

It works fine  aside from  link in href.

Example:

String to parse:
elognormal text

Output:
[[['elog', 'normal text']]]

as you can see it misses the info about href...
how can get this information 'vaffa.html'?

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


Re: Unpacking byte strings from a file of unknown size

2008-10-28 Thread Mark
Thanks I tested your solution and that works.

One of the things that didn't work was
for chunk in myfile.read(10):
info1, info2, info3 = struct.unpack('http://mail.python.org/mailman/listinfo/python-list


How to get an object's name as a string?

2008-10-28 Thread Shannon Mayne
I would like to create objects with algorithmically determined names
based on other object names and use object names for general algorithm
input.

How would one extract the name of an object from an object instance as
a string. I would think that it is stored as an attribute of the
object but successive 'dir()' calles haven't found me the attribute
with the namestring.

My thanks!

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


Re: dictionary

2008-10-28 Thread Steve Holden
D'Arcy J.M. Cain wrote:
> On Tue, 28 Oct 2008 10:06:31 -0400
> Steve Holden <[EMAIL PROTECTED]> wrote:
>> Bizarre though it may sound in this age of integrated circuits there
>> really was a storage device that used a cathode ray tube to store (IIRC)
>> a kilobit of information. It detected, by the use of a capacitance plate
> 
> A kilobit?  One tube would carry one bit.  Imagine the size of today's
> computers if we were still using them.  In those days you were lucky to
> get 4 Kbytes of core memory.
> 
> In those days they would have techs walking back and forth along
> pathways inside the memory banks with shopping carts full of tubes
> replacing them as they burned out.  Programs had to be prepared to deal
> with the fact that bits could go dead at any time and functions would
> run multiple times and hold an election to determine the correct answer.
> 
Sorry, you are thinking of bistable multivibrators. I was talking about
the Wiliams tube store:

  http://ed-thelen.org/comp-hist/SEAC-Williams-tube-desc.html

which I now see stored a half-kilobit in its SEAC implementation. They
built them to be cheaper and more compact than the single-tube bit store
(the single tube actually held two triodes in it), which would be used
for accumulators and the like - the fast stuff.

I don't ever remember programming to cope with equipment failure,
however. Did you make that bit up?

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

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


  1   2   >