Re: Best way to convert sequence of bytes to long integer

2010-01-20 Thread Stefan Behnel
Steven D'Aprano, 20.01.2010 08:36:
> I have a byte string (Python 2.x string), e.g.:
> 
> s = "g%$f yg\n1\05"
> assert len(s) == 10
> 
> I wish to convert it to a long integer, treating it as base-256. 
> Currently I'm using:
> 
> def makelong(s):
> n = 0
> for c in s:
> n *= 256
> n += ord(c)
> return n
> 
> 
> which gives:
> 
 makelong(s)
> 487088900085839492165893L
> 
> 
> Is this the best way, or have I missed some standard library function?

Have you checked if the struct module offers anything here?

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


Re: Performance of lists vs. list comprehensions

2010-01-20 Thread Alf P. Steinbach

* Steven D'Aprano:

On Wed, 20 Jan 2010 05:25:22 +0100, Alf P. Steinbach wrote:


* Steven D'Aprano:

On Tue, 19 Jan 2010 16:20:42 -0500, Gerald Britton wrote:


That's surprising. I wouldn't implement it that way at all.  I'd use a
dynamically-expanding buffer as I suggested.  That way you get a
single pass and don't have to calculate anything before you begin.  In
the best case, you'd use half the memory (the result just fits in the
buffer after its last expansion and no saved tuple).  In the worst
case, the memory use is about the same (you just expanded the buffer
using a 2x expansion rule then you hit the last item).

In the worst case, you waste 50% of the memory allocated.

Yes. That is a good argument for not doing the expanding buffer thing.
But such buffers may be generally present anyway, resulting from
optimization of "+".



As near as I can determine, the CPython optimization you are referring to 
doesn't use the "double the buffer when needed" technique. It operates on 
a completely different strategy. As near as I can tell (as a non-C 
speaker), it re-sizes the string in place to the size actually needed, 
thus reducing the amount of copying needed.


The optimization patch is here:

http://bugs.python.org/issue980695

and some history (including Guido's opposition to the patch) here:

http://mail.python.org/pipermail/python-dev/2004-August/046686.html

Nevertheless, the patch is now part of CPython since 2.4, but must be 
considered an implementation-specific optimization. It doesn't apply to 
Jython, and likely other implementations.


Provided that the CPython code is that code then you're right, it only calls 
PyObject_REALLOC, depending on that operation having (amortized) constant time.


And PyObject_REALLOC can in principle achieve that by relying on paging, e.g. 
using the OS' virtual memory allocator, instead of doubling and copying.


However, I'm baffled by the code I find via Google Code search; there 
PyObject_REALLOC simply calls malloc and copies, which if that were the code 
used in CPython 2.4  and greater, and if the '+' code is the code that you refer 
to above, would produce O(n^2) time for the first '+' example.




Using CPython 2.6.4 in Windows XP:

[snip time measurements]

Here the linear increase of times indicate that the "+" is being
optimized using an expanding buffer for the string.


Be careful of jumping to the conclusion from timings that a certain 
algorithm is used. All you can really tell is that, whatever the 
algorithm is, it has such-and-such big-oh behaviour.


Well, as for intended meaning you're right about that, it needs not be a 
doubling buffer.




If only the minimal
space was allocated each time then one would expect O(n^2) behavior
instead of the apparently O(n) above.


I don't follow that reasoning.


The reasoning assumes independent allocations rather than reallocation 
(extension of existing allocation).


Then quadratic time for the example follows from sum(range(n)) = (n^2-n)/2 of 
the sizes of the data copied.


But with PyObject_REALLOC as essentially constant time also 'join' could take 
advantage of this. :-)




Cheers,

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


Re: Best way to convert sequence of bytes to long integer

2010-01-20 Thread Peter Otten
Steven D'Aprano wrote:

> I have a byte string (Python 2.x string), e.g.:
> 
> s = "g%$f yg\n1\05"
> assert len(s) == 10
> 
> I wish to convert it to a long integer, treating it as base-256.
> Currently I'm using:
> 
> def makelong(s):
> n = 0
> for c in s:
> n *= 256
> n += ord(c)
> return n
> 
> 
> which gives:
> 
 makelong(s)
> 487088900085839492165893L
> 
> 
> Is this the best way, or have I missed some standard library function?

The pickle module uses

>>> import binascii
>>> s = "g%$f yg\n1\05"
>>> long(binascii.hexlify(s), 16)
487088900085839492165893L

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


how to check if a module is importable?

2010-01-20 Thread Robert P. J. Day

  finally getting back to clawing my way thru the python 3 book so
probably a number of newbie questions coming up.  first one -- can i
check if a module is importable (equivalently, exists on sys.path, i
assume) without trying to import it first?

  i can see that i can use try/except and just run "import" -- is that
the accepted way to test?

rday
--


Robert P. J. Day   Waterloo, Ontario, CANADA

Linux Consulting, Training and Kernel Pedantry.

Web page:  http://crashcourse.ca
Twitter:   http://twitter.com/rpjday

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


Re: Best way to convert sequence of bytes to long integer

2010-01-20 Thread Mark Dickinson
On Jan 20, 7:36 am, Steven D'Aprano  wrote:
> I have a byte string (Python 2.x string), e.g.:
>
> s = "g%$f yg\n1\05"
> assert len(s) == 10
>
> I wish to convert it to a long integer, treating it as base-256.

Not that it helps you right now, but provided someone finds the time,
there should be an int/long method for this in Python 2.7.  It's
already implemented for Python 3.2, so it just needs backporting.

Python 3.2a0 (py3k:77612, Jan 20 2010, 09:04:15)
[GCC 4.0.1 (Apple Inc. build 5490)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> int.from_bytes(b"g%$f yg\n1\05", 'big')
487088900085839492165893

Until then, Peter Otten's solution is about as close as you can get, I
think.

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


Re: setattr() oddness

2010-01-20 Thread M.-A. Lemburg
Dieter Maurer wrote:
> Steven D'Aprano  writes on 18 Jan 2010 
> 06:47:59 GMT:
>> On Mon, 18 Jan 2010 07:25:58 +0100, Dieter Maurer wrote:
>>
>>> Lie Ryan  writes on Sat, 16 Jan 2010 19:37:29 +1100:
 On 01/16/10 10:10, Sean DiZazzo wrote:
> Interesting.  I can understand the "would take time" argument, but I
> don't see any legitimate use case for an attribute only accessible
> via getattr().  Well, at least not a pythonic use case.

 mostly for people (ab)using attributes instead of dictionary.
>>>
>>> Here is one use case:
>>>
>>>  A query application. Queries are described by complex query objects.
>>>  For efficiency reasons, query results should be cached. For this, it is
>>>  not unnatural to use query objects as cache keys. Then, query objects
>>>  must not get changed in an uncontrolled way. I use "__setattr__" to
>>>  control access to the objects.
>>
>>
>> (1) Wouldn't it be more natural to store these query keys in a list or 
>> dictionary rather than as attributes on an object?
>>
>> e.g. instead of:
>>
>> cache.__setattr__('complex query object', value)
>>
>> use:
>>
>> cache['complex query object'] = value
> 
> Few will use "cache.__setattr__(...)" but "cache.attr = ..." which
> is nicer than "cache['attr'] = ...".
> 
> Moreover, it is not the cache but the query of which I want to protect
> modification. My cache indeed uses "cache[query_object] = ...".
> But I want to prevent "query_object" from being changed after a potential
> caching.

You might want to look at mxProxy which provides object-level
access control:

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

See the "Access Protocol" in the documentation:

http://www.egenix.com/products/python/mxBase/mxProxy/doc/#_Toc162774446

>> (2) How does __setattr__ let you control access to the object? If a user 
>> wants to modify the cache, and they know the complex query object, what's 
>> stopping them from using __setattr__ too?
> 
> In my specific case, "__setattr__" prevents all modifications via attribute
> assignment. The class uses "__dict__" access to set attributes when
> it knows it is still safe.
> 
> Of course, this is no real protection against attackers (which could
> use "__dict__" as well). It only protects against accidental change
> of query objects.
> 
> 
> Meanwhile, I remembered a more important use case for "__setattr__":
> providing for transparent persistancy. The "ZODB" (Zope Object DataBase)
> customizes "__setattr__" in order to intercept object modifications
> and register automatically that the change needs to be persisted at
> the next transaction commit.

-- 
Marc-Andre Lemburg
eGenix.com

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


::: Try our new mxODBC.Connect Python Database Interface for free ! 


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


Re: how to check if a module is importable?

2010-01-20 Thread Jean-Michel Pichavant

Robert P. J. Day wrote:

  finally getting back to clawing my way thru the python 3 book so
probably a number of newbie questions coming up.  first one -- can i
check if a module is importable (equivalently, exists on sys.path, i
assume) without trying to import it first?

  i can see that i can use try/except and just run "import" -- is that
the accepted way to test?

rday
--
  

AFAIK, that is why ImportError is for.

try:
   import module
except ImportError:
   # handle me
   pass

is commonly used.

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


Memory usage problem of twisted server

2010-01-20 Thread Victor Lin
Hi,

I encountered an increasing memory usage problem of my twisted server.
I have posted a question on stackoverflow:
http://stackoverflow.com/questions/2100192/how-to-find-the-source-of-increasing-memory-usage-of-a-twisted-server

I have read the article "Improving Python's Memory Allocator" (
http://evanjones.ca/memoryallocator/ ) and Python Memory Management
( http://evanjones.ca/python-memory.html ). And I now know little
about how Python manages memory. I am wondering, is that the
increasing memory usage problem of my audio broadcasting caused by the
how python manage memory?

In my server, there are lots of audio data chunks, they are not all in
fixed size. For example, the size of audio stream might looks like
this:

...
chunk size 822
chunk size 878
chunk size 1690
chunk size 1659
chunk size 1643
chunk size 1952
chunk size 373
chunk size 763
chunk size 1535
chunk size 1665
...

All of those chunks are str object. The size of chunks depend on mp3
encoder. You can see most the size of those chunks is bigger than 256
bytes, so that Python uses malloc to allocate spaces for those chunks.
In my mind, I saw those chunks occupy different place in heap, all of
them are not in fixed size, so that even when the chunk is freed, it
is difficult for malloc to find the right place for the new chunk. As
the result, there are so many chunks placed in large range of heap, it
is difficult for OS to swap pages out. Then the RSS is always high.

Is that my guessing correct? How can I monitor the memory allocation
of Python? Is there any available tools for this? Or is that modifying
the Python source code and recompiling the only way to monitor
allocation of memory?

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


Re: Performance of lists vs. list comprehensions

2010-01-20 Thread Dave Angel

Steven D'Aprano wrote:
A million items is not a lot of data. Depending on the size of each 
object, that might be as little as 4 MB of data:


  

L = ['' for _ in xrange(10**6)]
sys.getsizeof(L)


4348732


Note that this sys.getsizeof() is only saying the size of the list, not 
the size of the objects referenced in the list.  So that number doesn't 
depend on "the size of the object."


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


examining an initial, pristine python3 shell session

2010-01-20 Thread Robert P. J. Day

  still working my way through "dive into python 3" and i've already
been asked to give a newbie tutorial on it -- blind leading the blind,
as it were.  that should be hilarious.

  i'll be using python 3 and it occurred to me that it would be
educational (at least for me :-) to display what an initial p3 shell
session looks like before doing any imports whatsoever.  as in, i run
"python3" on my fedora box and, at the ">>>" prompt, i want to show
what's already there for the new user.

  from what little i know so far, i'd start with:

>>> __name__
'__main__'
>>>

to display the name of the current scope(?).  backing up a bit, i
could run either of:

>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> globals()
{'__builtins__': , '__name__':
'__main__', '__doc__': None, '__package__': None}
>>>

  then i might go a bit further to examine some of *those* objects.  i
admit it might seem a bit dry, but i think it would be handy to have a
handle on what a clean shell session looks like before starting to
import things, then seeing how that importing changes the session
before getting down to actual programming.

  what other useful commands might i run immediately after starting a
session whose output would be informative?  i can certainly poke at
some of those objects to see them in more detail.  i'm just curious
what others might recommend.  thanks.

rday
--


Robert P. J. Day   Waterloo, Ontario, CANADA

Linux Consulting, Training and Kernel Pedantry.

Web page:  http://crashcourse.ca
Twitter:   http://twitter.com/rpjday

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


Re: substitution

2010-01-20 Thread Peter Otten
Wyrmskull wrote:

> Your flowing pythonicity (if len(x):) gave me lots of inspiration.

Actually, instead of

if len(pairs): ...

that should have been just

if pairs: ...

When I started writing the function I initially wanted to special-case 
len(pairs) == 1. The len() call is a superfluous leftover.

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


Re: Best way to convert sequence of bytes to long integer

2010-01-20 Thread Paul Rubin
Steven D'Aprano  writes:
> s = "g%$f yg\n1\05"
> Is this the best way, or have I missed some standard library function?

It is really a shame that there is not a standard library function
for this.  I usually do it using hexadecimal conversion:

  d = int(s.encode('hex'), 16)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: twenty years ago Guido created Python

2010-01-20 Thread Stefan Behnel
n00m, 06.01.2010 01:25:
> My mom a little girl wsas pursued by Germans alsatians dogs in a
> forest
> Germans just made a fun of it.
> They screamead Zurich and laughed They laughed at her scare and you
> now teach me. Now her hands tremble froom malntrination
> She ate ate at WWII grass. And you teach me?

Sorry, this thread wasn't long enough yet to put Godwin's law in action.

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


Re: examining an initial, pristine python3 shell session

2010-01-20 Thread Alf P. Steinbach

* Robert P. J. Day:

  still working my way through "dive into python 3" and i've already
been asked to give a newbie tutorial on it -- blind leading the blind,
as it were.  that should be hilarious.

  i'll be using python 3 and it occurred to me that it would be
educational (at least for me :-) to display what an initial p3 shell
session looks like before doing any imports whatsoever.  as in, i run
"python3" on my fedora box and, at the ">>>" prompt, i want to show
what's already there for the new user.

  from what little i know so far, i'd start with:


__name__

'__main__'

to display the name of the current scope(?).  backing up a bit, i
could run either of:


dir()

['__builtins__', '__doc__', '__name__', '__package__']

globals()

{'__builtins__': , '__name__':
'__main__', '__doc__': None, '__package__': None}

  then i might go a bit further to examine some of *those* objects.  i
admit it might seem a bit dry, but i think it would be handy to have a
handle on what a clean shell session looks like before starting to
import things, then seeing how that importing changes the session
before getting down to actual programming.

  what other useful commands might i run immediately after starting a
session whose output would be informative?  i can certainly poke at
some of those objects to see them in more detail.  i'm just curious
what others might recommend.  thanks.


That depends on what you mean by "newbie".

If it's someone who knows a little bit of programming but is new to Python, then 
'help' would definitely be about the first thing I'd show her.


But if it's someone who doesn't even know anything about programming, then I'd 
recommend (blatant plug) http://tinyurl.com/programmingbookP3>  --  its 
first two chapters are constructed around complete, concrete examples. However, 
you would have to adapt just the *sense* of the first chapter, which is only 
about tool usage, to *nix, since it's written for Windows. I'd not dive into 
'help' for the someone who doesn't know anything because it gets technical 
pretty fast, and because she will get back to that on her own when it's time.


Whatever you do, and whatever the background of the newbie, do introduce turtle 
graphics right away.


The ch 2 of the above reference contains some t.g. examples that you might use 
(initial silly figures, graphs of functions, recursive figures). It doesn't go 
into the turtle module objects. But if objects are what you want to show right 
away, then I think the turtle module is great also for that, because those 
objects are simple and can be easily explored.



Cheers & hth.,

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


Re: Best way to convert sequence of bytes to long integer

2010-01-20 Thread Mark Dickinson
On Jan 20, 7:36 am, Steven D'Aprano  wrote:
> I have a byte string (Python 2.x string), e.g.:
>
> s = "g%$f yg\n1\05"
> assert len(s) == 10
>
> I wish to convert it to a long integer, treating it as base-256.

By the way, are you willing to divulge what you're using this
functionality for?  When trying to hack out the API for int.to_bytes
and int.from_bytes on the mailing list and bug tracker, some of the
discussion about use-cases seemed a little too much like guesswork;
I'd be curious to find out about real-life use-cases.

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


Re: how to check if a module is importable?

2010-01-20 Thread Lie Ryan
On 01/20/10 19:58, Robert P. J. Day wrote:
> 
>   finally getting back to clawing my way thru the python 3 book so
> probably a number of newbie questions coming up.  first one -- can i
> check if a module is importable (equivalently, exists on sys.path, i
> assume) without trying to import it first?
> 
>   i can see that i can use try/except and just run "import" -- is that
> the accepted way to test?

check imp.find_module()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: examining an initial, pristine python3 shell session

2010-01-20 Thread Alf P. Steinbach

* Alf P. Steinbach:

* Robert P. J. Day:

  still working my way through "dive into python 3" and i've already
been asked to give a newbie tutorial on it -- blind leading the blind,
as it were.  that should be hilarious.

  i'll be using python 3 and it occurred to me that it would be
educational (at least for me :-) to display what an initial p3 shell
session looks like before doing any imports whatsoever.  as in, i run
"python3" on my fedora box and, at the ">>>" prompt, i want to show
what's already there for the new user.

  from what little i know so far, i'd start with:


__name__

'__main__'

to display the name of the current scope(?).  backing up a bit, i
could run either of:


dir()

['__builtins__', '__doc__', '__name__', '__package__']

globals()

{'__builtins__': , '__name__':
'__main__', '__doc__': None, '__package__': None}

  then i might go a bit further to examine some of *those* objects.  i
admit it might seem a bit dry, but i think it would be handy to have a
handle on what a clean shell session looks like before starting to
import things, then seeing how that importing changes the session
before getting down to actual programming.

  what other useful commands might i run immediately after starting a
session whose output would be informative?  i can certainly poke at
some of those objects to see them in more detail.  i'm just curious
what others might recommend.  thanks.


That depends on what you mean by "newbie".

If it's someone who knows a little bit of programming but is new to 
Python, then 'help' would definitely be about the first thing I'd show her.


He he... Try "help antigravity". :-)


But if it's someone who doesn't even know anything about programming, 
then I'd recommend (blatant plug) http://tinyurl.com/programmingbookP3>  --  its first two chapters are 
constructed around complete, concrete examples. However, you would have 
to adapt just the *sense* of the first chapter, which is only about tool 
usage, to *nix, since it's written for Windows. I'd not dive into 'help' 
for the someone who doesn't know anything because it gets technical 
pretty fast, and because she will get back to that on her own when it's 
time.


Whatever you do, and whatever the background of the newbie, do introduce 
turtle graphics right away.


The ch 2 of the above reference contains some t.g. examples that you 
might use (initial silly figures, graphs of functions, recursive 
figures). It doesn't go into the turtle module objects. But if objects 
are what you want to show right away, then I think the turtle module is 
great also for that, because those objects are simple and can be easily 
explored.



Cheers & hth.,

- Alf

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


Re: how to check if a module is importable?

2010-01-20 Thread Robert P. J. Day
On Thu, 21 Jan 2010, Lie Ryan wrote:

> On 01/20/10 19:58, Robert P. J. Day wrote:
> >
> >   finally getting back to clawing my way thru the python 3 book so
> > probably a number of newbie questions coming up.  first one -- can i
> > check if a module is importable (equivalently, exists on sys.path, i
> > assume) without trying to import it first?
> >
> >   i can see that i can use try/except and just run "import" -- is that
> > the accepted way to test?
>
> check imp.find_module()

  thanks, that's what i was looking for.  i'm sure i'll run across
that shortly as i keep working my way thru this python 3 book.

rday
--


Robert P. J. Day   Waterloo, Ontario, CANADA

Linux Consulting, Training and Kernel Pedantry.

Web page:  http://crashcourse.ca
Twitter:   http://twitter.com/rpjday

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


Re: examining an initial, pristine python3 shell session

2010-01-20 Thread Robert P. J. Day
On Wed, 20 Jan 2010, Alf P. Steinbach wrote:

> * Robert P. J. Day:

   ... snip ...

> >   what other useful commands might i run immediately after
> > starting a session whose output would be informative?  i can
> > certainly poke at some of those objects to see them in more
> > detail.  i'm just curious what others might recommend.  thanks.
>
> That depends on what you mean by "newbie".
>
> If it's someone who knows a little bit of programming but is new to
> Python, then 'help' would definitely be about the first thing I'd
> show her.
>
> But if it's someone who doesn't even know anything about
> programming, then I'd recommend (blatant plug)  http://tinyurl.com/programmingbookP3> -- its first two chapters are
> constructed around complete, concrete examples. However, you would
> have to adapt just the *sense* of the first chapter, which is only
> about tool usage, to *nix, since it's written for Windows. I'd not
> dive into 'help' for the someone who doesn't know anything because
> it gets technical pretty fast, and because she will get back to that
> on her own when it's time.
>
> Whatever you do, and whatever the background of the newbie, do
> introduce turtle graphics right away.

  ah, thank you, i appreciate that reference.  i'm expecting the small
audience to be relatively tech-savvy with OO dev experience, and i'm
betting that some of them will be wondering right off the very first
thing *i* was wondering -- when i start that python3 shell and get
dumped into it, what am i looking at?  which is why i wanted to
collect a few commands to give the attendees at least a vague idea of
what was already there.

  i've collected the following to start with:

>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> globals()
... stuff ...

then move on to examine *those* things:

>>> type(dir)

>>> type(globals)

>>> type(__builtins__)


zoom in a bit further and pick on specific examples:

>>> dir(__builtins__)
... lots of output ...

and so on.  as i said, i know it looks dry but i figure i can take 5
minutes or so just to lay out the terrain and what a shell session
looks like before you do *anything* with it.  and i'm betting most of
my audience will appreciate getting that high-level view before
launching into some programming.  they'll just want to know the
initial session setup before they start importing stuff into it.

rday
--


Robert P. J. Day   Waterloo, Ontario, CANADA

Linux Consulting, Training and Kernel Pedantry.

Web page:  http://crashcourse.ca
Twitter:   http://twitter.com/rpjday

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


Re: examining an initial, pristine python3 shell session

2010-01-20 Thread Lie Ryan
On 01/20/10 22:59, Robert P. J. Day wrote:
> then i might go a bit further to examine some of *those* objects.  i
> admit it might seem a bit dry, but i think it would be handy to have a
> handle on what a clean shell session looks like before starting to
> import things, then seeing how that importing changes the session
> before getting down to actual programming.

I would recommend to teach these introspection capabilities *after* some
basic programming. If you tell them these dir(), globals(), __name__,
etc before even getting into basic python, your precious newbies would
sit there thinking "what the hell is he talking about?"
-- 
http://mail.python.org/mailman/listinfo/python-list


Interesting Problem

2010-01-20 Thread Victor Subervi
Hi;
I think I finally have an interesting problem for y'all. I need to import a
script from a lower dir, forcing me to change dirs:

cwd = os.getcwd()
os.chdir('%s/..' % cwd)
sys.path.append(os.getcwd())
from templateFrame import top, bottom
os.chdir(cwd)

Because I've found I must do my form evaluations *before* changing dir as
above, I am forced to call these values as globals:

form = cgi.FieldStorage()
store = form.getfirst('store')
cat = form.getfirst('cat')
id = form.getfirst('id')
pkg = form.getfirst('pkg')
patientID = form.getfirst('patientID')

Now, apparently because of python's problem with globals, when I call "id"
as follows:

cursor.execute('select ProductID from productsPackages where PackageID=%s' %
id)

I get the following error:

/var/www/html/angrynates.com/cart/Store_frame2.py
  135   cursor.close()
  136   bottom()
  137
  138 Store_frame2()
  139
Store_frame2 = 
 /var/www/html/angrynates.com/cart/Store_frame2.py in Store_frame2()
  119 printAProduct()
  120   else:
  121 cursor.execute('select ProductID from productsPackages where
PackageID=%s' % id)
  122 for id in [itm[0] for itm in cursor]:
  123   printAProduct(id)
global cursor = , cursor.execute = >, global id = '1'

UnboundLocalError: local variable 'id' referenced before assignment
  args = ("local variable 'id' referenced before assignment",)

Full code below. Please advise.
TIA,
beno

#! /usr/bin/python

import string
import cgitb; cgitb.enable()
import cgi
import MySQLdb
import sys,os
from sets import Set
import fpformat
cwd = os.getcwd()
sys.path.append(cwd)
from login import login
from particulars import truncate

form = cgi.FieldStorage()
store = form.getfirst('store')
cat = form.getfirst('cat')
id = form.getfirst('id')
pkg = form.getfirst('pkg')
patientID = form.getfirst('patientID')

try:
  browser = form.getfirst('browser', 'all')
except:
  browser = headers()

os.chdir('%s/..' % cwd)
sys.path.append(os.getcwd())
from templateFrame import top, bottom
os.chdir(cwd)

ourFile = string.split(__file__, "/")
p = ourFile[len(ourFile) - 1]
p = p[: - 9]
site = ourFile[4][:-10]
if site != '':
  site = site[:-1]

user, passwd, db, host = login()
db = MySQLdb.connect(host, user, passwd, db)
cursor= db.cursor()

cursor.execute('describe %s;' % store)
descr = cursor.fetchall()
cols = []
for elt in descr:
  cols.append(elt[0])

def printAProduct(id=id):
  lastID = ''
  cursor.execute('select * from %s where ID="%s"' % (store, id))
  vals = cursor.fetchone()
  prodDict = dict(zip(cols, vals))
  for col, item in prodDict.iteritems():
if col == 'ID':
  print '' % item
try: # Stores such as products (but not prescriptions) will pass through
  from particulars import ourOptions
  if col in ourOptions():
print '' % (col, item)
except:
  pass
if col[:3] != 'pic':
  notSet = 1
  if isinstance(item, (str, int, long, float, long, complex, unicode,
list, buffer, xrange, tuple)):
print '%s: %s\n' % (col, item)
  else:
try:
  html = "%s: " % (col, col)
  notSet = 0
  for itm in item:
try:
  color, number = itm.split('_')
  html += "%s" % (itm, color)
except:
  html += "%s" % (itm, itm)
  html += ""
  print html
except:
  pass
#  if notSet == 1:
#if len(col) > 49:
#  colX = col[:50] + '...'
#   else:
# colX = col
#   print '%s: %s\n' % (colX, item)
elif col == 'pic1':
  try:
if (item != None):
#  if (item != None) and (len(item > 0)):
  print '\n' % (store, col[3:], id, store, col[3:], id, store, col[3:],
id)
  except TypeError:
raise
  except:
raise
#  i += 1
#  try:
#content = item[x].tostring()
# pic = "tmp" + str(i) + ".jpg"
#try:
#  os.remove(pic)
#except:
#  pass
#img = open(pic, "w")
#img.write(content)
#print '' % pic
#img.close()
#  except:
#pass

def Store_frame2():
  top(browser, p)
  i = 0
  print '\n'
  print ''
  print "" % store
  print ""
  if pkg == 'no':
printAProduct()
  else:
cursor.execute('select ProductID from productsPackages where
PackageID=%s' % id)
for id in [itm[0] for itm in cursor]:
  printAProduct(id)
  if store == 'prescriptions':
print ""
print ""
  elif truncate()[store] != '':
pass
  else:
print "Quantity: "
print ""
  print ''
  print '\n'
  print '\n'
  cursor.close()
  bottom()

Store_frame2()


-- 
The Logos has come to bear
http://logos.13gems.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Something More Elegant

2010-01-20 Thread Victor Subervi
On Sun, Jan 17, 2010 at 5:36 PM, Dennis Lee Bieber wrote:

>I don't think that would be efficient, considering that "the above"
> entails what would be something like three or four courses all by
> themselves (Database Analysis, Database Design, SQL [these three are
> independent of any particular implementation language, though Design and
> SQL may be impacted by the database engine one is using -- for example,
> foreign key integrity & cascading deletes are not enforced by MySQL
> MyISAM tables, but can be enforced using InnoDB tables; SQLite doesn't
> have user access levels, but all client/server engines do, etc.), and
> then a course on Python with an emphasis on Web applications [since I
> recall you are trying to generate HTML at some stage]) the only example
> I'd be able to give would require me writing a complete application --
> the last time I supplied a large snippet of pseudo-code it took over a
> month of daily questions and replies to fix your attempts to modify it;
> most problems coming down to not understanding the algorithm, how to use
> parameterized queries, etc.
>

LOL! Yeah... <:-}

>
>The shortest hints I can demonstrate -- using a text console for
> input, AND in pseudo-code (I've not ensured this will run) would be:
>
> DON'T:
> tname = raw_input("Enter the name of the table to be searched: ")
> fname = raw_input("Enter the name of the field in %s to be searched: "
>% tname)
> fvalue = raw_input("Enter the value of %s defining the desired data: "
>% fname)
>
> SQL = """select * from %s
>where %s like "%%%s%% % (tname, fname, fvalue)
> crs.execute(SQL)
>
>
> DO
> tables = [  "Table1",
>"Table2",
>"Table3"]
> fields = {  "Table1" :  [   "t1field1",
>"t1field2",
>"t1field3"  ],
>"Table2" :  [   "t2field1",
>"t2field2",
>"t2field3"  ],
>"Table3" :  [   "t3field1",
>"t3field2",
>"t3field3"  ]
> }
>
> for i, t in enumerate(tables):
>print "%s\t%s" % (i, t)
> tnum = int(raw_input(
>"Enter the number of the table to be searched: "))
> tname = tables[tnum]
>
> for i, f in enumerate(fields[tname]):
>print "%s\t%s" % (i, f)
> fnum = int(raw_input(
>"Enter the name of the field in %s to be searched: "
>% tname)
> fname = fields[tname][fnum]
>
> fvalue = raw_input("Enter the value of %s defining the desired data: "
>% fname)
>
> SQL = """select * from %s
>where %s like %%s""" % (tname, fname)
> crs.execute(SQL, ("%"+fvalue+"%",))
>
>In a real application, one would use the ability of the database
> engine itself to retrieve the list of table names, and the fields for
> each table -- that way the code would not need to be changed if the
> database has additions or deletions of tables/fields.
>
>Also note that I did NOT include exception handling -- all those
> raw_input() in the "DO" version should have some exception handling (the
> first two might not be given numeric data so the int() fails, or the
> integer supplied may not be in the range of indices for the list
> look-ups; all three might get an EOF entry by the user trying to abort
> the look-up.
>
>Fancier versions would build multi-table joins, or allow for AND (or
> OR) clauses in the WHERE, using a list of table.field/value pairs.
>

Thanks. I think I followed that :/
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Changing var names

2010-01-20 Thread Victor Subervi
On Mon, Jan 18, 2010 at 10:10 AM, Jean-Michel Pichavant <
[email protected]> wrote:

> Victor Subervi wrote:
>
>> On Fri, Jan 15, 2010 at 3:15 PM, Adam Tauno Williams <
>> [email protected] > wrote:
>>
>>On Fri, 2010-01-15 at 13:27 -0400, Victor Subervi wrote:
>>> Hi;
>>> Well it took me *less than a day* to fix the following problems:
>>> -- bare excepts (accidentally left a couple I think)
>>> -- sql injection attacks
>>> -- recreating tables to make them more reasonable
>>> Now, I believe someone once mentioned that there is some s/w
>>tool out
>>> there for changing the names of my variables more easily than going
>>> through every script one-by-one. If so, please share with me again
>>> what it is.
>>
>>This process is called 'refactoring' [a good term to Google], and
>>every
>>decent IDE provides some support [if it doesn't, it isn't a "decent"
>>IDE]
>>
>>
>> Thanks. I'll work with Eric, I guess, once I get my Mac.
>> beno
>>
> Here are 2 well known IDEs, that help with refactoring (and many other
> things).
> http://netbeans.org/
> http://www.eclipse.org/
>
> Both supports python.
>

Thanks.

>
> I personnaly will stick to vim :o)
>

I'll keep that in mind. I haven't used any WYSIWIGS to date.
Thanks,
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Using jython to call python procedures/methods

2010-01-20 Thread KB
Hi there,

I have an application that only publishes a Java API. I can use jython
to access java classes, but jython currently (to the best of my
knowledge) does not support numpy/scipy.

Ideally I would like to have jython call a "native" python routine
where I have the numpy/scipy procedures already written.

Does anyone have any experience with this? Is it possible?

I had toyed with the idea of having jython/java write the data to a
file/database and then manually kick off the python process, but
ideally I would like this as automated as possible.

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


turtle, ipython, and pylab don't mix

2010-01-20 Thread Brian Blais

Hello,

I am noticing a hard crash of the ipython interpreter, with the pylab  
option, with the turtle module.  I am on Win XP, Enthought Edition  
6.0.0 which uses Python 2.6.  I can't reproduce it without the pylab  
option, nor on the Mac (with an earlier Enthought Edition).  The  
crash happens only when I try to move the turtle window, after it has  
drawn.  The following commands are enough to give the resulting error.


In [1]: from turtle import *

In [2]: pendown()

In [3]: forward(10)

In [4]: Fatal Python error: PyEval_RestoreThread: NULL tstate

This application has requested the Runtime to terminate it in an  
unusual way.

Please contact the application's support team for more information.


Any ideas?  Of course, I'll use it without the pylab option, but that  
means that there are two icons I need instead of one.  Minor  
inconvenience for me, but a bit more of a pain for my students.



thanks,

Brian Blais


--
Brian Blais
[email protected]
http://web.bryant.edu/~bblais
http://bblais.blogspot.com/



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


Re: turtle, ipython, and pylab don't mix

2010-01-20 Thread Robert Kern

On 2010-01-20 08:40 AM, Brian Blais wrote:

Hello,

I am noticing a hard crash of the ipython interpreter, with the pylab
option, with the turtle module. I am on Win XP, Enthought Edition 6.0.0
which uses Python 2.6. I can't reproduce it without the pylab option,
nor on the Mac (with an earlier Enthought Edition). The crash happens
only when I try to move the turtle window, after it has drawn. The
following commands are enough to give the resulting error.

In [1]: from turtle import *

In [2]: pendown()

In [3]: forward(10)

In [4]: Fatal Python error: PyEval_RestoreThread: NULL tstate

This application has requested the Runtime to terminate it in an unusual
way.
Please contact the application's support team for more information.


Any ideas? Of course, I'll use it without the pylab option, but that
means that there are two icons I need instead of one. Minor
inconvenience for me, but a bit more of a pain for my students.


The IPython list is probably a better place for this question. Is suspect that 
the problem is the mixing of GUIs. turtle uses Tk, but I suspect that your 
configured matplotlib backend is WxAgg. "ipython -pylab" will bring up a wx 
application as the main thread for execution of the code you type. When you try 
to bring in another GUI event loop, something goes awry.


--
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: Using jython to call python procedures/methods

2010-01-20 Thread Kurt Smith
On Wed, Jan 20, 2010 at 9:32 AM, KB  wrote:
> Hi there,
>
> I have an application that only publishes a Java API. I can use jython
> to access java classes, but jython currently (to the best of my
> knowledge) does not support numpy/scipy.
>
> Ideally I would like to have jython call a "native" python routine
> where I have the numpy/scipy procedures already written.
>
> Does anyone have any experience with this? Is it possible?

I have no experience with these technologies, and others can point you
to more detailed info, but you could try using XML-RPC (see the docs
for the xmlrpclib module in the standard library) or SOAP (Google it).

These would be better than rolling your own.

Perhaps there's a more Pythonic solution though?

Kurt

>
> I had toyed with the idea of having jython/java write the data to a
> file/database and then manually kick off the python process, but
> ideally I would like this as automated as possible.
>
> Thanks in advance.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: multiprocessing problems

2010-01-20 Thread DoxaLogos
On Jan 19, 10:33 am, DoxaLogos  wrote:
> On Jan 19, 10:26 am, Adam Tauno Williams 
> wrote:
>
>
>
> > > I decided to play around with the multiprocessing module, and I'm
> > > having some strange side effects that I can't explain.  It makes me
> > > wonder if I'm just overlooking something obvious or not.  Basically, I
> > > have a script parses through a lot of files doing search and replace
> > > on key strings inside the file.  I decided the split the work up on
> > > multiple processes on each processor core (4 total).  I've tried many
> > > various ways doing this form using pool to calling out separate
> > > processes, but the result has been the same: computer crashes from
> > > endless process spawn.
>
> > Are you hitting a ulimit error?  The number of processes you can create
> > is probably limited.
>
> > TIP: close os.stdin on your subprocesses.
>
> > > Here's the guts of my latest incarnation.
> > > def ProcessBatch(files):
> > >     p = []
> > >     for file in files:
> > >         p.append(Process(target=ProcessFile,args=file))
> > >     for x in p:
> > >         x.start()
> > >     for x in p:
> > >         x.join()
> > >     p = []
> > >     return
> > > Now, the function calling ProcessBatch looks like this:
> > > def ReplaceIt(files):
> > >     processFiles = []
> > >     for replacefile in files:
> > >         if(CheckSkipFile(replacefile)):
> > >             processFiles.append(replacefile)
> > >             if(len(processFiles) == 4):
> > >                 ProcessBatch(processFiles)
> > >                 processFiles = []
> > >     #check for left over files once main loop is done and process them
> > >     if(len(processFiles) > 0):
> > >         ProcessBatch(processFiles)
>
> > According to this you will create files is sets of four, but an unknown
> > number of sets of four.
>
> What would be the proper way to only do a set of 4, stop, then do
> another set of 4?  I'm trying to only 4 files at time before doing
> another set of 4.

I found out my problems.  One thing I did was followed the test queue
example in the documentation, but the biggest problem turned out to be
a pool instantiated globally in my script was causing most of the
endless process spawn, even with the "if __name__ == "__main__":"
block.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interesting Problem

2010-01-20 Thread Stephen Hansen
On Wed, Jan 20, 2010 at 7:19 AM, Victor Subervi wrote:

> Hi;
> I think I finally have an interesting problem for y'all. I need to import a
> script from a lower dir, forcing me to change dirs:
>

Don't do that. If you must, then the correct way to do it is to adjust your
sys.path and not change directory. "sys.path.append('..')" or whatever.

But I really would just re-organize your code so you don't need such things.
Have a single top-level directory that's on your path, put a blank
__init__.py in all your directories, and use absolute imports everywhere.

from myapp.templateFrame import top, bottom
from myapp.some_directory.some_file import this, that

etc.



> Now, apparently because of python's problem with globals, when I call "id"
> as follows:
>
>
Python does not have problems with globals. You are repeatedly re-using the
same variable names, causing confusion and errors; when you say 'id' in that
line of code, Python doesn't think you are talking about the "id" that is
global. It thinks you are talking about the "id" that is local-- that you
define one line beneath.

Local has precedence over global. Don't shadow global variables (and 'id' is
a bad name, as it shadows a builtin variable)


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


Re: Interesting Problem

2010-01-20 Thread MRAB

Victor Subervi wrote:

Hi;
I think I finally have an interesting problem for y'all. I need to 
import a script from a lower dir, forcing me to change dirs:


cwd = os.getcwd()
os.chdir('%s/..' % cwd)
sys.path.append(os.getcwd())
from templateFrame import top, bottom
os.chdir(cwd)


There's no need to actually change current directory. Just do this:

sys.path.append(os.path.dirname(os.getcwd()))
from templateFrame import top, bottom

Because I've found I must do my form evaluations *before* changing dir 
as above, I am forced to call these values as globals:


form = cgi.FieldStorage()
store = form.getfirst('store')
cat = form.getfirst('cat')
id = form.getfirst('id')
pkg = form.getfirst('pkg')
patientID = form.getfirst('patientID')

Now, apparently because of python's problem with globals, when I call 
"id" as follows:


cursor.execute('select ProductID from productsPackages where 
PackageID=%s' % id)


I get the following error:

/var/www/html/angrynates.com/cart/Store_frame2.py 


  135   cursor.close()
  136   bottom()
  137
  138 Store_frame2()
  139
Store_frame2 = 
 /var/www/html/angrynates.com/cart/Store_frame2.py 
 in Store_frame2()

  119 printAProduct()
  120   else:
  121 cursor.execute('select ProductID from productsPackages where 
PackageID=%s' % id)

  122 for id in [itm[0] for itm in cursor]:
  123   printAProduct(id)
global cursor = , cursor.execute = method Cursor.execute of >, global id = '1'


UnboundLocalError: local variable 'id' referenced before assignment
  args = ("local variable 'id' referenced before assignment",)

Full code below. Please advise.


Line 121 refers to 'id', but line 122 assigns to 'id' (the 'for' loop).

In a function, if you assign to a variable then that variable defaults
to being local. You're trying to use the local variable before assigning
a value to it. Having a local variable called 'id' means that you can't
refer to the global variable of the same name.

You're also assigning to 'i' on line 113, but not referring to it
elsewhere in the function.
--
http://mail.python.org/mailman/listinfo/python-list


Object Integer mapping

2010-01-20 Thread NighterNet
Need help on python version 3.1.x. I can't seem to know what going on
a bit. The code that I check that the hash is different every time. Is
there a way to make the hash the same? I using as to check the class
is the same variables but if it different variable in the class that
it add to the array and return the index.


# Generic Object->Integer mapping
# the object must be usable as a dictionary key
class ObjMap:
def __init__(self):
self.dict = {}
self.next = 0
def get(self, obj):
if obj in self.dict:
return self.dict[obj]
else:
id = self.next
self.next = self.next + 1
self.dict[obj] = id
return id

def items(self):
getval = operator.itemgetter(0)
getkey = operator.itemgetter(1)
return map(getval, sorted(self.dict.items(), key=getkey))


class Point:
def __init__(self):
self.x = 0
self.y = 0
self.z = 0

points = ObjMap()

Testme = Point()
Testme.x = 0
print(points.get(Testme))
Testme2 = Point()
Testme2.y = 1
print(points.get(Testme2))
Testme3 = Point()
Testme3.y = 1
print(points.get(Testme3))
Ttestme4 = Point()
Ttestme4.y = 1
print( points.get(Ttestme4))

It keep adding new array from this but doesn't match hash. Need help
on how to fix this class code.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using jython to call python procedures/methods

2010-01-20 Thread KB
Hmmm, XML is an interesting angle... I'll noodle it...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interesting Problem

2010-01-20 Thread Gary Herron

Victor Subervi wrote:

Hi;
I think I finally have an interesting problem for y'all. I need to 
import a script from a lower dir, forcing me to change dirs:


Wait a moment...  Your assumption that you need to change directories is 
invalid.  (And that means everything you do below this point unnecessary.)


Python provides many ways to easily import from arbitrary directories:

* Before run time, modify the environment variable
   PYTHONPATH with the desired directory. 
   Then import as normal.


* At run time, import sys and append the desired
   path to sys.path, and again import as normal.

* Check out the "imp" module which allows you to
   import from files specified via a path.

Straighten that out, then we can examine the following (suspicious) clams:
* that you must evaluate *before* some import
* that you must use globals
* python's problem with globals

Gary Herron






cwd = os.getcwd()
os.chdir('%s/..' % cwd)
sys.path.append(os.getcwd())
from templateFrame import top, bottom
os.chdir(cwd)

Because I've found I must do my form evaluations *before* changing dir 
as above, I am forced to call these values as globals:


form = cgi.FieldStorage()
store = form.getfirst('store')
cat = form.getfirst('cat')
id = form.getfirst('id')
pkg = form.getfirst('pkg')
patientID = form.getfirst('patientID')

Now, apparently because of python's problem with globals, when I call 
"id" as follows:


cursor.execute('select ProductID from productsPackages where 
PackageID=%s' % id)


I get the following error:

/var/www/html/angrynates.com/cart/Store_frame2.py 


  135   cursor.close()
  136   bottom()
  137
  138 Store_frame2()
  139
Store_frame2 = 
 /var/www/html/angrynates.com/cart/Store_frame2.py 
 in Store_frame2()

  119 printAProduct()
  120   else:
  121 cursor.execute('select ProductID from productsPackages where 
PackageID=%s' % id)

  122 for id in [itm[0] for itm in cursor]:
  123   printAProduct(id)
global cursor = , cursor.execute = 
>, 
global id = '1'


UnboundLocalError: local variable 'id' referenced before assignment
  args = ("local variable 'id' referenced before assignment",)

Full code below. Please advise.
TIA,
beno

#! /usr/bin/python

import string
import cgitb; cgitb.enable()
import cgi
import MySQLdb
import sys,os
from sets import Set
import fpformat
cwd = os.getcwd()
sys.path.append(cwd)
from login import login
from particulars import truncate

form = cgi.FieldStorage()
store = form.getfirst('store')
cat = form.getfirst('cat')
id = form.getfirst('id')
pkg = form.getfirst('pkg')
patientID = form.getfirst('patientID')

try:
  browser = form.getfirst('browser', 'all')
except:
  browser = headers()

os.chdir('%s/..' % cwd)
sys.path.append(os.getcwd())
from templateFrame import top, bottom
os.chdir(cwd)

ourFile = string.split(__file__, "/")
p = ourFile[len(ourFile) - 1]
p = p[: - 9]
site = ourFile[4][:-10]
if site != '':
  site = site[:-1]

user, passwd, db, host = login()
db = MySQLdb.connect(host, user, passwd, db)
cursor= db.cursor()

cursor.execute('describe %s;' % store)
descr = cursor.fetchall()
cols = []
for elt in descr:
  cols.append(elt[0])

def printAProduct(id=id):
  lastID = ''
  cursor.execute('select * from %s where ID="%s"' % (store, id))
  vals = cursor.fetchone()
  prodDict = dict(zip(cols, vals))
  for col, item in prodDict.iteritems():
if col == 'ID':
  print '' % item
try: # Stores such as products (but not prescriptions) will pass 
through

  from particulars import ourOptions
  if col in ourOptions():
print '' % (col, item)
except:
  pass
if col[:3] != 'pic':
  notSet = 1
  if isinstance(item, (str, int, long, float, long, complex, 
unicode, list, buffer, xrange, tuple)):

print '%s: %s\n' % (col, item)
  else:
try:
  html = "%s: " % (col, col)
  notSet = 0
  for itm in item:
try:
  color, number = itm.split('_')
  html += "%s" % (itm, color)
except:
  html += "%s" % (itm, itm)
  html += ""
  print html
except:
  pass
#  if notSet == 1:
#if len(col) > 49:
#  colX = col[:50] + '...'
#   else:
# colX = col
#   print '%s: %s\n' % (colX, item)
elif col == 'pic1':
  try:
if (item != None):
#  if (item != None) and (len(item > 0)):
  print 'class="highslide" href="getpic.py?store=%s&pic=%s&id=%s" 
onclick="return hs.expand(this)">src="getpic.py?store=%s&pic=%s&id=%s" width="100" height="80" alt="" 
align="left" border="0" title="Click to enlarge" style="border: 
0px">\n' % (store, col[3:], id, store, col[3:], 
id, store, col[3:], id)

  except TypeError:
raise
  except:
raise
#  i += 1
#  try:
#content = item[x].tostring()
# pic = "tmp" + str(i) + ".jpg"
#try:
# 

Re: Interesting Problem

2010-01-20 Thread James Matthews
Also as a side point... It's best to anonymize the code as best you can so
people cannot attach your code to your site (IP theft and security risk)

On Wed, Jan 20, 2010 at 8:38 AM, MRAB  wrote:

> Victor Subervi wrote:
>
>> Hi;
>> I think I finally have an interesting problem for y'all. I need to import
>> a script from a lower dir, forcing me to change dirs:
>>
>> cwd = os.getcwd()
>> os.chdir('%s/..' % cwd)
>> sys.path.append(os.getcwd())
>> from templateFrame import top, bottom
>> os.chdir(cwd)
>>
>>  There's no need to actually change current directory. Just do this:
>
> sys.path.append(os.path.dirname(os.getcwd()))
>
> from templateFrame import top, bottom
>
>  Because I've found I must do my form evaluations *before* changing dir as
>> above, I am forced to call these values as globals:
>>
>> form = cgi.FieldStorage()
>> store = form.getfirst('store')
>> cat = form.getfirst('cat')
>> id = form.getfirst('id')
>> pkg = form.getfirst('pkg')
>> patientID = form.getfirst('patientID')
>>
>> Now, apparently because of python's problem with globals, when I call "id"
>> as follows:
>>
>> cursor.execute('select ProductID from productsPackages where PackageID=%s'
>> % id)
>>
>> I get the following error:
>>
>> /var/www/html/angrynates.com/cart/Store_frame2.py <
>> http://angrynates.com/cart/Store_frame2.py>
>>
>>  135   cursor.close()
>>  136   bottom()
>>  137
>>  138 Store_frame2()
>>  139
>> Store_frame2 = 
>>  /var/www/html/angrynates.com/cart/Store_frame2.py <
>> http://angrynates.com/cart/Store_frame2.py> in Store_frame2()
>>
>>  119 printAProduct()
>>  120   else:
>>  121 cursor.execute('select ProductID from productsPackages where
>> PackageID=%s' % id)
>>  122 for id in [itm[0] for itm in cursor]:
>>  123   printAProduct(id)
>> global cursor = , cursor.execute = > method Cursor.execute of >, global id = '1'
>>
>> UnboundLocalError: local variable 'id' referenced before assignment
>>  args = ("local variable 'id' referenced before assignment",)
>>
>> Full code below. Please advise.
>>
>>  Line 121 refers to 'id', but line 122 assigns to 'id' (the 'for' loop).
>
> In a function, if you assign to a variable then that variable defaults
> to being local. You're trying to use the local variable before assigning
> a value to it. Having a local variable called 'id' means that you can't
> refer to the global variable of the same name.
>
> You're also assigning to 'i' on line 113, but not referring to it
> elsewhere in the function.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
http://www.astorandblack.com

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


Re: Interesting Problem

2010-01-20 Thread Victor Subervi
On Wed, Jan 20, 2010 at 12:30 PM, Stephen Hansen wrote:

> On Wed, Jan 20, 2010 at 7:19 AM, Victor Subervi 
> wrote:
>
>> Hi;
>> I think I finally have an interesting problem for y'all. I need to import
>> a script from a lower dir, forcing me to change dirs:
>>
>
> Don't do that. If you must, then the correct way to do it is to adjust your
> sys.path and not change directory. "sys.path.append('..')" or whatever.
>
> But I really would just re-organize your code so you don't need such
> things. Have a single top-level directory that's on your path, put a blank
> __init__.py in all your directories, and use absolute imports everywhere.
>
> from myapp.templateFrame import top, bottom
> from myapp.some_directory.some_file import this, that
>
> etc.
>

I didn't know I could do that. Thanks!

>
>
>
>> Now, apparently because of python's problem with globals, when I call "id"
>> as follows:
>>
>>
> Python does not have problems with globals. You are repeatedly re-using the
> same variable names, causing confusion and errors; when you say 'id' in that
> line of code, Python doesn't think you are talking about the "id" that is
> global. It thinks you are talking about the "id" that is local-- that you
> define one line beneath.
>

I didn't think that was a problem because it cascaded. Thanks, that fixed
it!
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Object Integer mapping

2010-01-20 Thread Richard Thomas
On Jan 20, 4:43 pm, NighterNet  wrote:
> Need help on python version 3.1.x. I can't seem to know what going on
> a bit. The code that I check that the hash is different every time. Is
> there a way to make the hash the same? I using as to check the class
> is the same variables but if it different variable in the class that
> it add to the array and return the index.
>
> # Generic Object->Integer mapping
> # the object must be usable as a dictionary key
> class ObjMap:
>         def __init__(self):
>                 self.dict = {}
>                 self.next = 0
>         def get(self, obj):
>                 if obj in self.dict:
>                         return self.dict[obj]
>                 else:
>                         id = self.next
>                         self.next = self.next + 1
>                         self.dict[obj] = id
>                         return id
>
>         def items(self):
>                 getval = operator.itemgetter(0)
>                 getkey = operator.itemgetter(1)
>                 return map(getval, sorted(self.dict.items(), key=getkey))
>
> class Point:
>         def __init__(self):
>                 self.x = 0
>                 self.y = 0
>                 self.z = 0
>
> points = ObjMap()
>
> Testme = Point()
> Testme.x = 0
> print(points.get(Testme))
> Testme2 = Point()
> Testme2.y = 1
> print(points.get(Testme2))
> Testme3 = Point()
> Testme3.y = 1
> print(points.get(Testme3))
> Ttestme4 = Point()
> Ttestme4.y = 1
> print( points.get(Ttestme4))
>
> It keep adding new array from this but doesn't match hash. Need help
> on how to fix this class code.

You need to define how the Point class hashes. You can do this by
writing a __hash__ method like so:

class Point(object):
...
def __hash__(self):
return hash(self.x) ^ hash(self.y) ^ hash(self.z)

However you will also need to define how the Point class relates as
equal using the __eq__ or __cmp__ methods.

class Point(object):
...
def __eq__(self, other):
return self.x == other.x and self.y == other.y and self.z ==
other.z

This addition makes sure that if two points a equivalent then they
count as the same key in the dictionary.

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


Re: Object Integer mapping

2010-01-20 Thread Robert Kern

On 2010-01-20 11:18 AM, Richard Thomas wrote:

On Jan 20, 4:43 pm, NighterNet  wrote:

Need help on python version 3.1.x. I can't seem to know what going on
a bit. The code that I check that the hash is different every time. Is
there a way to make the hash the same? I using as to check the class
is the same variables but if it different variable in the class that
it add to the array and return the index.

# Generic Object->Integer mapping
# the object must be usable as a dictionary key
class ObjMap:
 def __init__(self):
 self.dict = {}
 self.next = 0
 def get(self, obj):
 if obj in self.dict:
 return self.dict[obj]
 else:
 id = self.next
 self.next = self.next + 1
 self.dict[obj] = id
 return id

 def items(self):
 getval = operator.itemgetter(0)
 getkey = operator.itemgetter(1)
 return map(getval, sorted(self.dict.items(), key=getkey))

class Point:
 def __init__(self):
 self.x = 0
 self.y = 0
 self.z = 0

points = ObjMap()

Testme = Point()
Testme.x = 0
print(points.get(Testme))
Testme2 = Point()
Testme2.y = 1
print(points.get(Testme2))
Testme3 = Point()
Testme3.y = 1
print(points.get(Testme3))
Ttestme4 = Point()
Ttestme4.y = 1
print( points.get(Ttestme4))

It keep adding new array from this but doesn't match hash. Need help
on how to fix this class code.


You need to define how the Point class hashes. You can do this by
writing a __hash__ method like so:

class Point(object):
 ...
 def __hash__(self):
 return hash(self.x) ^ hash(self.y) ^ hash(self.z)

However you will also need to define how the Point class relates as
equal using the __eq__ or __cmp__ methods.

class Point(object):
 ...
 def __eq__(self, other):
 return self.x == other.x and self.y == other.y and self.z ==
other.z

This addition makes sure that if two points a equivalent then they
count as the same key in the dictionary.


I recommend a simpler approach that duplicates less code and makes it easier to 
maintain the __hash__/__eq__ invariants:


class Point(object):
...
def _key(self):
# The type(self).__name__ bit is optional, but usually handy.
return (type(self).__name__, self.x, self.y, self.z)

def __hash__(self):
return hash(self._key())

def __eq__(self, other):
if not hasattr(other, '_key'):
return False
return self._key() == other._key()

It is also worth noting that once one allows hashing by value, one should treat 
the objects as immutable, so one should not change the attributes as the OP does 
in his example.


--
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: Interesting Problem

2010-01-20 Thread MRAB

James Matthews wrote:
Also as a side point... It's best to anonymize the code as best you can 
so people cannot attach your code to your site (IP theft and security risk)



[snip]
Security risk, yes; IP theft, not so much. ;-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to convert sequence of bytes to long integer

2010-01-20 Thread Alf P. Steinbach

* Mark Dickinson:

On Jan 20, 7:36 am, Steven D'Aprano  wrote:

I have a byte string (Python 2.x string), e.g.:

s = "g%$f yg\n1\05"
assert len(s) == 10

I wish to convert it to a long integer, treating it as base-256.


By the way, are you willing to divulge what you're using this
functionality for?  When trying to hack out the API for int.to_bytes
and int.from_bytes on the mailing list and bug tracker, some of the
discussion about use-cases seemed a little too much like guesswork;
I'd be curious to find out about real-life use-cases.


One possibility is that Steven wants to apply bitlevel and/or arithmetic 
operations for e.g. some custom cryptographic thing. The string that he uses as 
example is not completely unlike a message digest.


Another possibility is custom serialization/deserialization.

But it would be nice to know what it's actually about...



Cheers,

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


* operator in python tutorial

2010-01-20 Thread ben
Hello,

I am following through the python tutorial which gets to a line that
uses the * operator with zip(). I searched and searched but could find
no information on the operator or how to use it in general. The
example from the tut is as follows:
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> zipped
[(1, 4), (2, 5), (3, 6)]
>>> x2, y2 = zip(*zipped)
>>> x == list(x2) and y == list(y2)
True

How would i apply the * operator in general?
Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: * operator in python tutorial

2010-01-20 Thread ben
I missed it because I skipped to the explanation of zip. I hit the
back arrow and went on and saw the link to "Unpacking Argument Lists"
which explained what I needed to know.

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


Re: Interesting Problem

2010-01-20 Thread M.-A. Lemburg
Victor Subervi wrote:
> Hi;
> I think I finally have an interesting problem for y'all. I need to import a
> script from a lower dir, forcing me to change dirs:
> 
> cwd = os.getcwd()
> os.chdir('%s/..' % cwd)
> sys.path.append(os.getcwd())
> from templateFrame import top, bottom
> os.chdir(cwd)

I suggest you read up on the built-in __import__() function:

http://docs.python.org/library/functions.html#__import__

There's no need to change sys.path just to import a module.

If you only need to execute some Python from a file, use execfile():

http://docs.python.org/library/functions.html#execfile

-- 
Marc-Andre Lemburg
eGenix.com

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


::: Try our new mxODBC.Connect Python Database Interface for free ! 


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


Re: What is a list compression in Python?

2010-01-20 Thread Luis M . González
On Jan 18, 1:07 pm, Kit  wrote:
> Hello Everyone, I am not sure if I have posted this question in a
> correct board. Can anyone please teach me:
>
> What is a list compression in Python?
>
> Would you mind give me some list compression examples?
>
> Thanks & really appreciate that.
> Kit

It's also worth noting that from Python 3.0 on, you have also dict and
set comprehensions.

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


Re: Using jython to call python procedures/methods

2010-01-20 Thread M.-A. Lemburg
KB wrote:
> Hi there,
> 
> I have an application that only publishes a Java API. I can use jython
> to access java classes, but jython currently (to the best of my
> knowledge) does not support numpy/scipy.
> 
> Ideally I would like to have jython call a "native" python routine
> where I have the numpy/scipy procedures already written.
> 
> Does anyone have any experience with this? Is it possible?
> 
> I had toyed with the idea of having jython/java write the data to a
> file/database and then manually kick off the python process, but
> ideally I would like this as automated as possible.

Have a look at this talk for some inspiration:

http://www.slideshare.net/onyame/mixing-python-and-java

Here's an example of using Pyro to connect Jython and CPython:

http://www.razorvine.net/python/PyroAndJython

As always with such solutions, there are a lot of weird problems
to be expected :-/

-- 
Marc-Andre Lemburg
eGenix.com

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


::: Try our new mxODBC.Connect Python Database Interface for free ! 


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


html code generation

2010-01-20 Thread George Trojan
I need an advice on table generation. The table is essentially a fifo, 
containing about 200 rows. The rows are inserted every few minutes or 
so. The simplest solution is to store row data per line and write 
directly html code:

line = "value1value2>... "
each run of the program would read the previous table into a list of 
lines, insert the first row and drop the last one, taking care of table 
header and trailer.

Is there a more classy solution?

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


Re: Best way to convert sequence of bytes to long integer

2010-01-20 Thread Steven D'Aprano
On Wed, 20 Jan 2010 19:27:34 +0100, Alf P. Steinbach wrote:

> * Mark Dickinson:
>> On Jan 20, 7:36 am, Steven D'Aprano > cybersource.com.au> wrote:
>>> I have a byte string (Python 2.x string), e.g.:
>>>
>>> s = "g%$f yg\n1\05"
>>> assert len(s) == 10
>>>
>>> I wish to convert it to a long integer, treating it as base-256.
>> 
>> By the way, are you willing to divulge what you're using this
>> functionality for?  When trying to hack out the API for int.to_bytes
>> and int.from_bytes on the mailing list and bug tracker, some of the
>> discussion about use-cases seemed a little too much like guesswork; I'd
>> be curious to find out about real-life use-cases.
> 
> One possibility is that Steven wants to apply bitlevel and/or arithmetic
> operations for e.g. some custom cryptographic thing. The string that he
> uses as example is not completely unlike a message digest.

Good guess!

I'm writing a module that handles, I won't call it encryption, 
obfuscation using classical cryptographic algorithms. One of the 
functions needs a deterministic but unpredictable integer generated from 
a human-generated password or passphrase, so I'm using:

hashlib.md5(key).digest()

to get a string of bytes, then converting it to an int.

int.from_bytes would be perfect for me, but in the meantime I'm using 
Paul Rubin's trick of int(s.encode("hex"), 16).

Thanks to all who responded.


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


Re: html code generation

2010-01-20 Thread Chris Colbert
use a deque with a 'junk' as each element

http://docs.python.org/library/collections.html

On Wed, Jan 20, 2010 at 4:03 PM, George Trojan wrote:

> I need an advice on table generation. The table is essentially a fifo,
> containing about 200 rows. The rows are inserted every few minutes or so.
> The simplest solution is to store row data per line and write directly html
> code:
> line = "value1value2>... "
> each run of the program would read the previous table into a list of lines,
> insert the first row and drop the last one, taking care of table header and
> trailer.
> Is there a more classy solution?
>
> George
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: html code generation

2010-01-20 Thread Chris Colbert
On Wed, Jan 20, 2010 at 4:52 PM, Chris Colbert  wrote:

> use a deque with a 'junk' as each element
>
> http://docs.python.org/library/collections.html
>
> On Wed, Jan 20, 2010 at 4:03 PM, George Trojan wrote:
>
>> I need an advice on table generation. The table is essentially a fifo,
>> containing about 200 rows. The rows are inserted every few minutes or so.
>> The simplest solution is to store row data per line and write directly html
>> code:
>> line = "value1value2>... "
>> each run of the program would read the previous table into a list of
>> lines, insert the first row and drop the last one, taking care of table
>> header and trailer.
>> Is there a more classy solution?
>>
>> George
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>
>
 In [1]: from collections import deque

In [2]: a = deque(['foo', 'bar'])

In [3]: ''.join(a)
Out[3]: 'foobar'

In [4]: a.popleft()
Out[4]: 'foo'

In [5]: a.append('baz')

In [6]: ''.join(a)
Out[6]: 'barbaz'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: html code generation

2010-01-20 Thread D'Arcy J.M. Cain
On Wed, 20 Jan 2010 21:03:10 +
George Trojan  wrote:
> I need an advice on table generation. The table is essentially a fifo, 
> containing about 200 rows. The rows are inserted every few minutes or 
> so. The simplest solution is to store row data per line and write 
> directly html code:
> line = "value1value2>... "
> each run of the program would read the previous table into a list of 
> lines, insert the first row and drop the last one, taking care of table 
> header and trailer.
> Is there a more classy solution?

Almost positively.  It's hard to say for sure though without knowing
more.  However, I have a few pointers for you.

First, think about a proper database.  You can store the raw data into
something like PostgreSQL and then you have your historical record.
Then you can extract and format the latest 200 records at run time.  You
can change that value any time you want and even look at historical
information such as the output exactly 24 hours ago.

Whether you generate the page on demand or pre-calculate at intervals
will depend on the ratio of updates to displays.  I would start with
generating on demand to start with and profile usage.

Look into server side HTML.  It can be a bit ugly generating your
entire web page in code.  Write the static part as a regular file and
include your small Python script to generate the table data.  The cool
thing with that is that you can give your webaster the task of making
pretty pages and simply deal with the variable generation.

HTH.

-- 
D'Arcy J.M. Cain  |  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: html code generation

2010-01-20 Thread Chris Colbert
ah ok, i misread your post. Store each 'junk' as an item
in the deque. the idea is the same though.

On Wed, Jan 20, 2010 at 4:55 PM, Chris Colbert  wrote:

>
>
> On Wed, Jan 20, 2010 at 4:52 PM, Chris Colbert wrote:
>
>> use a deque with a 'junk' as each element
>>
>> http://docs.python.org/library/collections.html
>>
>> On Wed, Jan 20, 2010 at 4:03 PM, George Trojan wrote:
>>
>>> I need an advice on table generation. The table is essentially a fifo,
>>> containing about 200 rows. The rows are inserted every few minutes or so.
>>> The simplest solution is to store row data per line and write directly html
>>> code:
>>> line = "value1value2>... "
>>> each run of the program would read the previous table into a list of
>>> lines, insert the first row and drop the last one, taking care of table
>>> header and trailer.
>>> Is there a more classy solution?
>>>
>>> George
>>> --
>>> http://mail.python.org/mailman/listinfo/python-list
>>
>>
>  In [1]: from collections import deque
>
> In [2]: a = deque(['foo', 'bar'])
>
> In [3]: ''.join(a)
> Out[3]: 'foobar'
>
> In [4]: a.popleft()
> Out[4]: 'foo'
>
> In [5]: a.append('baz')
>
> In [6]: ''.join(a)
> Out[6]: 'barbaz'
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Object Integer mapping

2010-01-20 Thread NighterNet
On Jan 20, 9:43 am, Robert Kern  wrote:
> On 2010-01-20 11:18 AM, Richard Thomas wrote:
>
>
>
> > On Jan 20, 4:43 pm, NighterNet  wrote:
> >> Need help on python version 3.1.x. I can't seem to know what going on
> >> a bit. The code that I check that the hash is different every time. Is
> >> there a way to make the hash the same? I using as to check the class
> >> is the same variables but if it different variable in the class that
> >> it add to the array and return the index.
>
> >> # Generic Object->Integer mapping
> >> # the object must be usable as a dictionary key
> >> class ObjMap:
> >>          def __init__(self):
> >>                  self.dict = {}
> >>                  self.next = 0
> >>          def get(self, obj):
> >>                  if obj in self.dict:
> >>                          return self.dict[obj]
> >>                  else:
> >>                          id = self.next
> >>                          self.next = self.next + 1
> >>                          self.dict[obj] = id
> >>                          return id
>
> >>          def items(self):
> >>                  getval = operator.itemgetter(0)
> >>                  getkey = operator.itemgetter(1)
> >>                  return map(getval, sorted(self.dict.items(), key=getkey))
>
> >> class Point:
> >>          def __init__(self):
> >>                  self.x = 0
> >>                  self.y = 0
> >>                  self.z = 0
>
> >> points = ObjMap()
>
> >> Testme = Point()
> >> Testme.x = 0
> >> print(points.get(Testme))
> >> Testme2 = Point()
> >> Testme2.y = 1
> >> print(points.get(Testme2))
> >> Testme3 = Point()
> >> Testme3.y = 1
> >> print(points.get(Testme3))
> >> Ttestme4 = Point()
> >> Ttestme4.y = 1
> >> print( points.get(Ttestme4))
>
> >> It keep adding new array from this but doesn't match hash. Need help
> >> on how to fix this class code.
>
> > You need to define how the Point class hashes. You can do this by
> > writing a __hash__ method like so:
>
> > class Point(object):
> >      ...
> >      def __hash__(self):
> >          return hash(self.x) ^ hash(self.y) ^ hash(self.z)
>
> > However you will also need to define how the Point class relates as
> > equal using the __eq__ or __cmp__ methods.
>
> > class Point(object):
> >      ...
> >      def __eq__(self, other):
> >          return self.x == other.x and self.y == other.y and self.z ==
> > other.z
>
> > This addition makes sure that if two points a equivalent then they
> > count as the same key in the dictionary.
>
> I recommend a simpler approach that duplicates less code and makes it easier 
> to
> maintain the __hash__/__eq__ invariants:
>
> class Point(object):
>      ...
>      def _key(self):
>          # The type(self).__name__ bit is optional, but usually handy.
>          return (type(self).__name__, self.x, self.y, self.z)
>
>      def __hash__(self):
>          return hash(self._key())
>
>      def __eq__(self, other):
>          if not hasattr(other, '_key'):
>              return False
>          return self._key() == other._key()
>
> It is also worth noting that once one allows hashing by value, one should 
> treat
> the objects as immutable, so one should not change the attributes as the OP 
> does
> in his example.
>
> --
> 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

Thanks you, it help fix my hash check against my classes.
-- 
http://mail.python.org/mailman/listinfo/python-list


installing psycopg2-2.0.13 with python3.1

2010-01-20 Thread Iain Barnett
Hi,

Would anyone know if it's possible to install psycopg2-2.0.13 with python3.1.1 
(or similar)? When I try I get the following

$ sudo python setup.py install  
 
Password:
  File "setup.py", line 233
except Warning, w:
  ^
SyntaxError: invalid syntax


I can install it with python2.6 with no problems, but obviously I'd prefer to 
use the latest version. My system is OSX10.6, and I'm new to Python.


Any help is much appreciated.

Iain

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


Re: Inheriting methods but over-riding docstrings

2010-01-20 Thread Gabriel Genellina
En Tue, 19 Jan 2010 08:44:09 -0300, Michele Simionato  
 escribió:

On Jan 16, 6:55 pm, Steven D'Aprano  wrote:


I have a series of subclasses that inherit methods from a base class,  
but

I'd like them to have their own individual docstrings.


from types import FunctionType, CodeType

def newfunc(func, docstring):
c = func.func_code
nc = CodeType(c.co_argcount, c.co_nlocals, c.co_stacksize,
  c.co_flags, c.co_code, c.co_consts, c.co_names,
  c.co_varnames, c.co_filename, func.__name__,
  c.co_firstlineno, c.co_lnotab, c.co_freevars,
  c.co_cellvars)
nf = FunctionType(nc, func.func_globals, func.__name__)
nf.__doc__ = docstring
return nf

def setdocstring(method, docstring):
cls = method.im_class
basefunc = getattr(super(cls, cls), method.__name__).im_func
setattr(cls, method.__name__, newfunc(basefunc, docstring))

class B(object):
def m(self):
"base"
return 'ok'

class C(B):
pass

setdocstring(C.m, 'C.m docstring')


This is basically the same technique as in  
 but there is  
a difference: you clone the function object *and* the code object it is  
based on. As I understand it, code objects are immutable and there is no  
need to clone them, but I may be wrong. Why did you feel the need to clone  
the code object too?


--
Gabriel Genellina

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


Re: html code generation

2010-01-20 Thread Jon Clements
On Jan 20, 10:03 pm, "D'Arcy J.M. Cain"  wrote:
> On Wed, 20 Jan 2010 21:03:10 +
>
> George Trojan  wrote:
> > I need an advice on table generation. The table is essentially a fifo,
> > containing about 200 rows. The rows are inserted every few minutes or
> > so. The simplest solution is to store row data per line and write
> > directly html code:
> > line = "value1value2>... "
> > each run of the program would read the previous table into a list of
> > lines, insert the first row and drop the last one, taking care of table
> > header and trailer.
> > Is there a more classy solution?
>
> Almost positively.  It's hard to say for sure though without knowing
> more.  However, I have a few pointers for you.
>
> First, think about a proper database.  You can store the raw data into
> something like PostgreSQL and then you have your historical record.
> Then you can extract and format the latest 200 records at run time.  You
> can change that value any time you want and even look at historical
> information such as the output exactly 24 hours ago.
>
> Whether you generate the page on demand or pre-calculate at intervals
> will depend on the ratio of updates to displays.  I would start with
> generating on demand to start with and profile usage.
>
> Look into server side HTML.  It can be a bit ugly generating your
> entire web page in code.  Write the static part as a regular file and
> include your small Python script to generate the table data.  The cool
> thing with that is that you can give your webaster the task of making
> pretty pages and simply deal with the variable generation.
>
> HTH.
>
> --
> D'Arcy J.M. Cain          |  Democracy is three 
> wolveshttp://www.druid.net/darcy/               |  and a sheep voting on
> +1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.

I'd second this and go for a DB solution; possibly even SQLite to
start with.

I would tend to go with a web framework (maybe OTT) or a templating
engine, so if you wish, someone can do the CSS/HTML and you just
provide something that says (gimme the last 200 hundred rows). Very
similar to what D'Arcy J.M. Cain has said but should hopefully
formalise the possible problem and be applicable across the project.

Further more you might want to look into AJAX, but ummm, that's a
completely new debate :)

Cheers,
Jon.

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


Re: multiprocessing problems

2010-01-20 Thread Nils Ruettershoff

Hi Doxa,

DoxaLogos wrote:
[...]

I found out my problems.  One thing I did was followed the test queue
example in the documentation, but the biggest problem turned out to be
a pool instantiated globally in my script was causing most of the
endless process spawn, even with the "if __name__ == "__main__":"
block.
  


Problems who solves them self, are the best problems ;)

One tip: currently your algorithm has some overhead. 'Cause you are 
starting 4 time an additional python interpreter, compute the files and, 
closing all new spawned interpreter and starting again 4 interpreter, 
which are processing the files.


For such kind of jobs I prefer to start processes once and feeding them 
with data via a queue. This reduces some overhead and increase runtime 
performance.



This could look like this:
(due some pseudo functions not directly executeable -> untested)

import multiprocessing
import Queue

class Worker(multiprocessing.Process):
   def __init__(self, feeder_q, queue_filled):
   multiprocessing.Process.__init__(self)
   self.feeder_q = feeder_q
   self.queue_filled = queue_filled
  
   def run(self):

   serve = True
   # start infinite loop
   while serve:
   try:
   # scan queue for work, will block process up to 5 
seconds. If until then no item is in queue a Queue.Empty will be raised

   text = self.feeder_q.get(True, timeout=5)
   if text:
   do_stuff(text)
   # very important! tell the queue that the fetched 
work has been finished

   # otherwise the feeder_q.join() would block infinite
   self.input_queue.task_done()
   except Queue.Empty:
   # as soon as queue is empty and all work has been enqueued
   # process can terminate itself
   if self.queue_filled.is_set() and feeder_q.empty():
   serve = False
   return


if __name__ == '__main__':
   number_of_processes = 4
   queue_filled = multiprocessing.Event()
   feeder_q = multiprocessing.JoinableQueue()
   process_list =[]
   # get file name which need to be processed
   all_files = get_all_files()
   # start processes
   for i in xrange(0,number_of_processes):
   process = Worker(feeder_q, queue_filled)
   process.start()
   process_list.append(thread)
   # start feeding
   for file in all_files:
   feeder_q.put(file)
   # inform processes that all work has been ordered
   queue_filled.set()
   # wait until queue is empty
   feeder_q.join()
   # wait until all processed have finished their jobs
   for process in process_list:
   process.join()



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


Re: installing psycopg2-2.0.13 with python3.1

2010-01-20 Thread Philip Semanchuk


On Jan 20, 2010, at 5:45 PM, Iain Barnett wrote:


Hi,

Would anyone know if it's possible to install psycopg2-2.0.13 with  
python3.1.1 (or similar)? When I try I get the following


$ sudo python setup.py install
Password:
 File "setup.py", line 233
   except Warning, w:
 ^
SyntaxError: invalid syntax


I can install it with python2.6 with no problems, but obviously I'd  
prefer to use the latest version. My system is OSX10.6, and I'm new  
to Python.


Hi Iain,
I've been out of the psycopg loop for a while now, but my guess is  
that the answer to your question is no. There are some Python 3- 
related checkins in the psycopg2 repository, but it looks like they're  
not ready for public use yet (unless you're adventurous).


Here's the source repository, which contains a commit commented "First  
round of changes for Python 3":

https://dndg.it/cgi-bin/gitweb.cgi?p=public/psycopg2.git

If you scroll to the bottom of that page you'll see a "head" created  
especially for Python 2 which is where 2.0.13 came from.


If you're willing to do some hacking on your own, Martin v. Löwis  
ported psycopg2 to Python 3 and provided a diff:

http://mail.python.org/pipermail/python-porting/2008-December/18.html

Last but not least, I should note that psycopg has its own mailing  
list, and I bet they know more about it there:

http://lists.initd.org/mailman/listinfo/psycopg

Hope this helps. Psycopg & Postgres both worked wonderfully well for me.

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


Re: multiprocessing problems

2010-01-20 Thread Nils Ruettershoff

Hi,

Adam Tauno Williams wrote:

[...]

Here's the guts of my latest incarnation.
def ProcessBatch(files):
p = []
for file in files:
p.append(Process(target=ProcessFile,args=file))
for x in p:
x.start()
for x in p:
x.join()
p = []
return
Now, the function calling ProcessBatch looks like this:
def ReplaceIt(files):
processFiles = []
for replacefile in files:
if(CheckSkipFile(replacefile)):
processFiles.append(replacefile)
if(len(processFiles) == 4):
ProcessBatch(processFiles)
processFiles = []
#check for left over files once main loop is done and process them
if(len(processFiles) > 0):
ProcessBatch(processFiles)



According to this you will create files is sets of four, but an unknown
number of sets of four.

  
This is not correct, 'cause of the x.join(). This will block the parent 
process until all processes have been terminated. So as soon as the 
current set of processes have finished their job, a new set will be spawned.


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


Re: installing psycopg2-2.0.13 with python3.1

2010-01-20 Thread Gabriel Genellina
En Wed, 20 Jan 2010 19:45:44 -0300, Iain Barnett   
escribió:


Would anyone know if it's possible to install psycopg2-2.0.13 with  
python3.1.1 (or similar)?I can install it with python2.6 with no  
problems, but obviously I'd prefer to use the latest version. My system  
is OSX10.6, and I'm new to Python.


If you're new to Python, perhaps it's better to stay with the 2.6 version.

Python 3.x introduced some incompatible changes in the language; not all  
third-party packages are available for Python 3.x yet. Regarding psycopg2,  
although some work has been done [1], there is no "official" release  
compatible with Python 3 yet.


If you insist on using Python 3.1, there is another interface to  
PostgreSQL called pg8000 that claims to be Python 3.x compatible (I've not  
actually tested it).


[1]  
http://mail.python.org/pipermail/python-porting/2008-December/04.html

[2] http://pybrary.net/pg8000/

--
Gabriel Genellina

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


Re: py2exe and pydocs. Downloads?

2010-01-20 Thread Gib Bogle

Gabriel Genellina wrote:


c:\temp>python -m pydoc sys
Help on built-in module sys:
[...same info...]


When I do this I get:

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


Sorted dictionary

2010-01-20 Thread Jan Kaliszewski

Hello,

Inspired by some my needs as well as some discussions in the net, I've  
implemented a sorted dictionary (i.e. a dict that returns keys, values and  
items always sorted by keys):


http://code.activestate.com/recipes/576998/

Maybe it'll appear to be useful for somebody... And I'm curious about your  
opinions.


Regards,
*j

--
Jan Kaliszewski (zuo) 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using jython to call python procedures/methods

2010-01-20 Thread bobicanprogram
On Jan 20, 10:32 am, KB  wrote:
> Hi there,
>
> I have an application that only publishes a Java API. I can use jython
> to access java classes, but jython currently (to the best of my
> knowledge) does not support numpy/scipy.
>
> Ideally I would like to have jython call a "native" python routine
> where I have the numpy/scipy procedures already written.
>
> Does anyone have any experience with this? Is it possible?
>
> I had toyed with the idea of having jython/java write the data to a
> file/database and then manually kick off the python process, but
> ideally I would like this as automated as possible.
>
> Thanks in advance.


You might want to take a look at the SIMPL toolkit (http://
www.icanprogram.com/simpl).   It sports both JAVA and Python hooks so
you should be able to pump the data from your JAVA module to a Python/
numpy/scipy module.

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


Re: * operator in python tutorial

2010-01-20 Thread Gringo
On 1/20/2010 12:38, ben wrote:
> Hello,
> 
> I am following through the python tutorial which gets to a line that
> uses the * operator with zip(). I searched and searched but could find
> no information on the operator or how to use it in general. The
> example from the tut is as follows:
 x = [1, 2, 3]
 y = [4, 5, 6]
 zipped = zip(x, y)
 zipped
> [(1, 4), (2, 5), (3, 6)]
 x2, y2 = zip(*zipped)
 x == list(x2) and y == list(y2)
> True
> 
> How would i apply the * operator in general?
> Thanks.

The * operator, when used in this context, unpacks the sequence and it's
as if you passed each item to the function as a different parameter.
For example, if you have a list x with 4 items, these two statements
would be the same:
f(x[0],x[1],x[2],x[3])
f(*x)

Hope this helps.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sorted dictionary

2010-01-20 Thread Steven D'Aprano
On Thu, 21 Jan 2010 02:02:02 +0100, Jan Kaliszewski wrote:

> Hello,
> 
> Inspired by some my needs as well as some discussions in the net, I've
> implemented a sorted dictionary (i.e. a dict that returns keys, values
> and items always sorted by keys):
> 
> http://code.activestate.com/recipes/576998/


What's the advantage of that over sorting the keys as needed?


E.g.

for key in sorted(dict):
print key


works.




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


Re: Py 3: How to switch application to Unicode strings?

2010-01-20 Thread Gnarlodious
Thanks for the help, but I am going to skip this problem because I
don't need Unicode characters in a script anyway.

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


Create object name from string value?

2010-01-20 Thread Gnarlodious
I want to declare several objects from names in a list:

objects=['object1', 'object2', 'object3', 'object4']
for objectName in objects:
objectName=classname()

That fails, and so do these:

exec(objectName)=classname()
eval(objectName)=classname()

So how to make an object whose name is the value in the variable?

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


Re: Create object name from string value?

2010-01-20 Thread Ben Finney
Gnarlodious  writes:

> I want to declare several objects from names in a list:
>
> objects=['object1', 'object2', 'object3', 'object4']
> for objectName in objects:
>   objectName=classname()

Since (I presume) you are a newcomer to Python, it's best to learn the
common style http://www.python.org/dev/peps/pep-0008/>. I will
assume the above is written as::

object_names = ['object1', 'object2', 'object3', 'object4']
for object_name in object_names:
object_name = Class()

> That fails

Well, it succeeds (runs successfully). But I get what you mean: it fails
to do what you expected.

> So how to make an object whose name is the value in the variable?

This is almost certainly better done with a dictionary. Like so::

object_names = ['object1', 'object2', 'object3', 'object4']
objects = {}
for object_name in object_names:
objects[object_name] = Class()

There are shortcuts that can be used to create the dictionary without
the separate list of names. But for now the above should make sense
immediately and help you get to the meat of the problem.

-- 
 \ “We now have access to so much information that we can find |
  `\  support for any prejudice or opinion.” —David Suzuki, 2008-06-27 |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Create object name from string value?

2010-01-20 Thread Gnarlodious
On Jan 20, 8:31 pm, Ben Finney  wrote:
> Since (I presume) you are a newcomer to Python, it's best to learn the
> common style http://www.python.org/dev/peps/pep-0008/>.
Thanks for that.

> This is almost certainly better done with a dictionary. Like so::
>
>     object_names = ['object1', 'object2', 'object3', 'object4']
>     objects = {}
>     for object_name in object_names:
>         objects[object_name] = Class()

And thanks for that too. I see it returns a list of objects, Python is
very cool.

-- Gnarlie

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


why the super class can access the subclass's attribute

2010-01-20 Thread yousay
I have sees aprogram like this ,i confusing why super class can access
the subclass's attribute
,this is the program,thanks in advance:
class MyThread(threading.Thread):
def join(self):
super(MyThread,self).join()
return self.result

class Worker(MyThread):
import random
import pdb
pdb.set_trace()
def run(self):
total = 0
for i in range(random.randrange(10,100)):
total +=i
self.result = total


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


Re: Create object name from string value?

2010-01-20 Thread Steven D'Aprano
On Wed, 20 Jan 2010 19:02:49 -0800, Gnarlodious wrote:

> I want to declare several objects from names in a list:
> 
> objects=['object1', 'object2', 'object3', 'object4'] for objectName in
> objects:
>   objectName=classname()


That's the wrong way to handle the problem. Named objects are only useful 
if you know the name of the object when writing the code. Otherwise, how 
do you know what name to use in the code?


> That fails, and so do these:
> 
> exec(objectName)=classname()
> eval(objectName)=classname()


The right way to solve this problem is with a dictionary:

for name in ["object1", "object2", "object3"]:
d = {name: classname()}
print d[name]



but for the record, the way to use exec is like this:

exec("object1 = classname()")

But beware, exec is not only much slower than the alternatives, but it 
risks putting a serious security flaw in your software. As a general 
rule, 9 times out of 10 if you think you want exec, you don't.


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


Re: Interesting Problem

2010-01-20 Thread alex23
Victor Subervi wrote:
> I think I finally have an interesting problem for y'all. I need to import a
> script from a lower dir, forcing me to change dirs:

I'm surprised that no one has yet mentioned packages.

Suppose you have the following folder layout and you stick an empty
__init__.py in each subfolder, like so:

app/
tools/
__init__.py
open.py
close.py
extensions/
__init__.py
inner.py
outer.py

Scripts in the 'app' folder can then import from the subfolders using
dotted notation:

  import tools
  from tools import open, close
  import tools.extensions
  from tools.extensions import inner, outer

However, with empty __init__ files, you can't use attribute lookup on
nested modules, so if you started with an 'import tools' any attempts
to reference the contents of the module in the following ways will
fail:

  tools.open
  tools.close
  tools.ext.inner
  tools.ext.outer

To provide this functionality, you need to import the items you want
available in the __init__ of the package they belong to. So by adding
the following:

  tools/__init__.py:  import open, close, ext
  tools/ext/__init__.py:  import inner, outer

You can then do a single 'import tools' and use standard attribute
lookup to access the contents of your subfolders.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Inheriting methods but over-riding docstrings

2010-01-20 Thread Michele Simionato
On Jan 21, 12:09 am, "Gabriel Genellina" 
wrote:
> This is basically the same technique as in  
>  but there is  
> a difference: you clone the function object *and* the code object it is  
> based on. As I understand it, code objects are immutable and there is no  
> need to clone them, but I may be wrong. Why did you feel the need to clone  
> the code object too?

No need. I just had the newfunc utility in my library (I think copied
from a recipe in the Python cookbook by Alex Martelli) so I used it.
In this case it is overkill, though. Also, I had no read your post
when I posted my solution, otherwise I would not have sent it ;)
Anyway, the setdocstring utility to extract the parent method was not
explicit in your post and may be of some use to somebody.

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


Re: py2exe and pydocs. Downloads?

2010-01-20 Thread Gabriel Genellina
En Wed, 20 Jan 2010 21:22:19 -0300, Gib Bogle  
 escribió:



Gabriel Genellina wrote:


c:\temp>python -m pydoc sys
Help on built-in module sys:
[...same info...]


When I do this I get:

No module named tempfile


You found a bug. Looks like it depends on the environment, or what  
packages are installed, or something like that, because it worked on my  
other PC but not here.

Please report it at http://bugs.python.org so it doesn't get forgotten.

--
Gabriel Genellina

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


counting lines of code

2010-01-20 Thread Michele Simionato
I need a small utility to count the lines of Python code in a
directory, traversing subdirectories and ignoring comments and
docstrings. I am sure there is already something doing that, what do
you suggest?

TIA,

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


Re: Sorted dictionary

2010-01-20 Thread Steven D'Aprano
On Wed, 20 Jan 2010 22:21:22 -0800, Dennis Lee Bieber wrote:

> On Thu, 21 Jan 2010 02:02:02 +0100, "Jan Kaliszewski"
>  declaimed the following in
> gmane.comp.python.general:
> 
>> Hello,
>> 
>> Inspired by some my needs as well as some discussions in the net, I've
>> implemented a sorted dictionary (i.e. a dict that returns keys, values
>> and items always sorted by keys):
>> 
>   How does this differ from all the other "ordered dictionary" 
> schemes out there (and maybe even included in 2.7? I'm still on 2.5)


Ordered dicts remember the insertion order of keys.

Sorted dicts return the keys in sorted order.




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


Re: counting lines of code

2010-01-20 Thread Mark Hammond

On 21/01/2010 5:51 PM, Michele Simionato wrote:

I need a small utility to count the lines of Python code in a
directory, traversing subdirectories and ignoring comments and
docstrings. I am sure there is already something doing that, what do
you suggest?


I suggest typing your subject line into google and hitting the "I feel 
lucky" button :)


HTH,

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


Re: counting lines of code

2010-01-20 Thread Ben Finney
Michele Simionato  writes:

> I need a small utility to count the lines of Python code in a
> directory, traversing subdirectories and ignoring comments and
> docstrings. I am sure there is already something doing that, what do
> you suggest?

Any of the static code checkers (‘pylint’, ‘pyflakes’, etc.) would
already be doing this.

You would at least be able to crib from them, maybe import the
functionality; ideally they can directly report what you want to see.

-- 
 \   “My business is to teach my aspirations to conform themselves |
  `\  to fact, not to try and make facts harmonise with my |
_o__)   aspirations.“ —Thomas Henry Huxley, 1860-09-23 |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: counting lines of code

2010-01-20 Thread Michele Simionato
I did not known about cloc, it does more that I need, but it looks
cool (it is perl and not Python, by who cares? ;)

Thanks,

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


Re: counting lines of code

2010-01-20 Thread Michele Simionato
On Jan 21, 8:12 am, Ben Finney  wrote:
> Michele Simionato  writes:
> > I need a small utility to count the lines of Python code in a
> > directory, traversing subdirectories and ignoring comments and
> > docstrings. I am sure there is already something doing that, what do
> > you suggest?
>
> Any of the static code checkers (‘pylint’, ‘pyflakes’, etc.) would
> already be doing this.

pylint does too many things, I want something fast that just counts
the lines and can be run on thousands of files at once.
cloc seems fine, I have just tried on 2,000 files and it gives me a
report in just a few seconds.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why the super class can access the subclass's attribute

2010-01-20 Thread Chris Rebert
On Wed, Jan 20, 2010 at 8:56 PM, yousay  wrote:
> I have sees aprogram like this ,i confusing why super class can access
> the subclass's attribute

To make this easy to understand, I'm going to ***drastically*** oversimplify.
With that disclaimer out of the way...

When you access an instance attribute, as in self.result, Python
actually returns self.__dict__["result"]; that is, it does a
dictionary lookup using the attribute name as a string as the key;
instance attribute assignment works analogously. Every object has a
special dictionary associated with it that is used to store its
instance attributes; this dictionary can itself be accessed through
the __dict__ attribute. (And if you're about to ask how the __dict__
attribute gets looked up, well, it's magic™!)

This "instance attribute access is just dictionary manipulation"
principle is universal, even in subclasses, superclasses, and
unrelated objects. Thus, in both Worker and its superclass MyThread,
accessing the `result` instance attribute manipulates the very same
dictionary (self.__dict__) and thus works exactly the same in both
cases.

So, essentially, `result` is accessible to MyThread because there's
nothing to prevent it from being accessible there.

Other related principles that may aid in your understanding (these are
not oversimplified):
- Attribute lookups all happen dynamically at runtime
- Python has no language-enforced notion of `protected` or `private`
attributes; everything's public
- Python is dynamically typed and thus does no compile-time typechecking

Cheers,
Chris
--
Simplifications include overlooking __slots__, metaclasses,
__getattribute__, and instance.class_attribute, among others.
http://blog.rebertia.com

> ,this is the program,thanks in advance:
> class MyThread(threading.Thread):
>    def join(self):
>        super(MyThread,self).join()
>        return self.result
>
> class Worker(MyThread):
>    import random
>    import pdb
>    pdb.set_trace()
>    def run(self):
>        total = 0
>        for i in range(random.randrange(10,100)):
>            total +=i
>        self.result = total
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sorted dictionary

2010-01-20 Thread Chris Rebert
On Wed, Jan 20, 2010 at 5:50 PM, Steven D'Aprano
 wrote:
> On Thu, 21 Jan 2010 02:02:02 +0100, Jan Kaliszewski wrote:
>
>> Hello,
>>
>> Inspired by some my needs as well as some discussions in the net, I've
>> implemented a sorted dictionary (i.e. a dict that returns keys, values
>> and items always sorted by keys):
>>
>> http://code.activestate.com/recipes/576998/
>
>
> What's the advantage of that over sorting the keys as needed?
>
>
> E.g.
>
> for key in sorted(dict):
>    print key
>
>
> works.

Well, it does spread out the cost of sorting the key list over each of
the N distinct key assignments, versus sorting the entire list all at
once, on-demand at the end of the process. And you avoid having to
traverse the M buckets in the dictionary to locate the N keys in the
first place. So it has different performance characteristics, which
could theoretically matter depending on the use case; e.g. it could
avoid a GUI application hanging for several seconds while it sorts a
large list of keys.

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


Symbols as parameters?

2010-01-20 Thread Martin Drautzburg
Hello all,

When passing parameters to a function, you sometimes need a paramter
which can only assume certain values, e.g.

def move (direction):
...
If direction can only be "up", "down", "left" or "right", you can solve
this by passing strings, but this is not quite to the point:

- you could pass invalid strings easily
- you need to quote thigs, which is a nuisance
- the parameter IS REALLY NOT A STRING, but a direction

Alternatively you could export such symbols, so when you "import *" you
have them available in the caller's namespace. But that forces you
to "import *" which pollutes your namespace.

What I am really looking for is a way

- to be able to call move(up)
- having the "up" symbol only in the context of the function call

So it should look something like this

... magic, magic ...
move(up)
... unmagic, unmagic ...
print up

This should complain that "up" is not defined during the "print" call,
but not when move() is called. And of course there should be as little
magic as possible.

Any way to achieve this?

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