Re: True lists in python?

2010-12-18 Thread Dmitry Groshev
On Dec 19, 9:48 am, Vito 'ZeD' De Tullio 
wrote:
> Dmitry Groshev wrote:
> > Is there any way to use a true lists (with O(c) insertion/deletion and
> > O(n) search) in python? For example, to make things like reversing
> > part of the list with a constant time.
>
> if you're interested just in "reverse" a collection maybe you can take a
> look at the deque[0] module.
>
> If you want "true lists" (um... "linked list"?) there are is this recipe[1]
> you might look.
>
> [0]http://docs.python.org/library/collections.html#collections.deque
> [1]http://code.activestate.com/recipes/577355-python-27-linked-list-vs-
> list/
>
> --
> By ZeD

-I can't find any information about reverse's complexity in python
docs, but it seems that deque is a linked list. Maybe this is the one
I need.
-Yes, I meant linked list - sorry for misunderstanding.
-Linked list on objects is too slow. It must be a C-extension for a
proper speed.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: True lists in python?

2010-12-18 Thread Vito 'ZeD' De Tullio
Dmitry Groshev wrote:

> Is there any way to use a true lists (with O(c) insertion/deletion and
> O(n) search) in python? For example, to make things like reversing
> part of the list with a constant time.

if you're interested just in "reverse" a collection maybe you can take a 
look at the deque[0] module.

If you want "true lists" (um... "linked list"?) there are is this recipe[1] 
you might look.

[0] http://docs.python.org/library/collections.html#collections.deque
[1] http://code.activestate.com/recipes/577355-python-27-linked-list-vs-
list/

-- 
By ZeD

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


Re: True lists in python?

2010-12-18 Thread Dmitry Groshev
On Dec 19, 9:18 am, Dmitry Groshev  wrote:
> Is there any way to use a true lists (with O(c) insertion/deletion and
> O(n) search) in python? For example, to make things like reversing
> part of the list with a constant time.

I forgot to mention that I mean *fast* lists. It's trivial to do
things like this with objects, but it will be slw.
-- 
http://mail.python.org/mailman/listinfo/python-list


True lists in python?

2010-12-18 Thread Dmitry Groshev
Is there any way to use a true lists (with O(c) insertion/deletion and
O(n) search) in python? For example, to make things like reversing
part of the list with a constant time.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: If/then style question

2010-12-18 Thread Carl Banks
On Dec 17, 12:23 am, Steven D'Aprano  wrote:
> On Thu, 16 Dec 2010 20:32:29 -0800, Carl Banks wrote:
> > Even without the cleanup issue, sometimes you want to edit a function to
> > affect all return values somehow.  If you have a single exit point you
> > just make the change there; if you have mulitple you have to hunt them
> > down and change all of them--if you remember to.  I just got bit by that
> > one.
>
> If your function has so many exit points that you can miss some of them
> while editing, your function is too big, does too much, or both.

Sanctimonious much?  In the real world, people "miss things" and "make
mistakes" and not necessarily because they are working on something
too complex to handle.  It happens.


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


Re: If/then style question

2010-12-18 Thread Steven D'Aprano
On Sat, 18 Dec 2010 12:29:31 +0100, Francesco wrote:

[...]
> I agree to your point, but I'm afraid you chose a wrong example (AFAIK,
> and that's not much). Sure, the second version of function(arg) is much
> more readable, but why do you think the first one would do "*lots*  of
> unnecessary work"?
> All the overhead in that function would be:
>if some_condition, three IF tests, and you know that's NOT a lot! 

Well, let's try it with a working (albeit contrived) example. This is 
just an example -- obviously I wouldn't write the function like this in 
real life, I'd use a while loop, but to illustrate the issue it will do.

def func1(n):
result = -1
done = False
n = (n+1)//2
if n%2 == 1:
result = n
done = True
if not done:
n = (n+1)//2
if n%2 == 1:
result = n
done = True
if not done:
n = (n+1)//2
if n%2 == 1:
result = n
done = True
if not done:
for i in range(100):
if not done:
n = (n+1)//2
if n%2 == 1:
result = n
done = True
return result


def func2(n):
n = (n+1)//2
if n%2 == 1:
return n
n = (n+1)//2
if n%2 == 1:
return n
n = (n+1)//2
if n%2 == 1:
return n
for i in range(100):
n = (n+1)//2
if n%2 == 1:
return n
return -1


Not only is the second far more readable that the first, but it's also 
significantly faster:

>>> from timeit import Timer
>>> t1 = Timer('for i in range(20): x = func1(i)', 
... 'from __main__ import func1')
>>> t2 = Timer('for i in range(20): x = func2(i)', 
... 'from __main__ import func2')
>>> min(t1.repeat(number=10, repeat=5))
7.3219029903411865
>>> min(t2.repeat(number=10, repeat=5))
4.530779838562012

The first function does approximately 60% more work than the first, all 
of it unnecessary overhead.



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


Re: Serialize my class as JSON causes "__init__() got an unexpected keyword argument 'indent'" ?

2010-12-18 Thread Terry Reedy

On 12/18/2010 6:32 AM, shearichard wrote:


Brilliant - thank you very much.

Now that you've explained it I can see why the documentation is
written the way it is ! Before I saw your example I thought the
documentation was a bit strange but I can see now what it was trying
to tell me !


If you have any idea who to improve that part of the doc so you would 
have understood it without Peter's nice explanation and example here, 
please open a doc issue on the tracker.


--
Terry Jan Reedy

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


Re: Added Python, WSGI to XAMPP

2010-12-18 Thread Gerry Reno
On 12/17/2010 02:36 PM, Daniel Fetchinson wrote:
>> How-To: Add VirtualEnv and Pylons (WSGI framework) to XAMPP
>> 

 Maybe, if there's no Zope.  Or we'll run away screaming...
>>>
>>> That is rather pathetically true...
>>>
>>> Ah well, each to their own...
>>>
>>> Chris
>>>
>> What I really don't like right off is that Pyramid is contorting the MVC
>> model just as Django did with their MTV model.  They are both making the
>> controller be the view and this confuses the hell out of people who come
>> from true MVC based projects.
>>
>> The VIEW is the bits that stream out of the webserver back to the users
>> browser.  The CONTROLLER is the code that gathers all the pieces from
>> the model and constructs the python code that is then fed to the engine
>> that then creates the view.  And just because the controller navigates
>> the logic to dynamically contruct/render a view, that does not make 'it'
>> the view.
>
> In turbogears that is exactly what happens.
>
> Cheers,
> Daniel
>
>
>


How-To: Add VirtualEnv and TurboGears2 (WSGI frmwk) to XAMPP
http://www.apachefriends.org/f/viewtopic.php?f=17&t=43207


-Gerry

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


Re: Newbie question about importing modules.

2010-12-18 Thread Tim Roberts
cronoklee  wrote:
>
>Hey thanks for the help fellas. The links were helpful and the pyExe
>program looks great. I might well end up using this.
>
>I'm still a little confused as to how the directory structure works. PIL
>(http://www.pythonware.com/products/pil/#pil117), for example comes packed
>in a folder called Imaging-1.1.7 which contains a bunch of other 
>directories, one of which is PIL.

That is a source distribution.  It is not intended to be used as is -- it
must be installed first.  The installation process will build whatever
shared libraries might be required, and then move the directories and
folders into the site-packages directory of your Python installation.

In this case, it will create a subdirectory called site-packages/PIL.  The
"site-packages" directory is already on your Python path, so when you say

from PIL import Image

the interpreter will scan through all of the directories in the path,
notice that one of them contains a folder called PIL that has the right
format for a module (meaning it has __init__.py), and will bring in
Image.py from that directory.

The PIL installer also creates a file called site-packages/PIL.pth which
contains the name "PIL".  Python looks for .pth files and adds those
directories to the module path at startup.  So, this would ALSO work:

import Image

>Sorry for the stupidity - I'm coming from PHP where you just include 
>path/to/script.php so this is a bit alien to me.

That certainly works for individual scripts, but even with PHP there are
customary central locations where complicated packages are installed.
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess.Popen() and a .msi installer

2010-12-18 Thread Nobody
On Fri, Dec 17, 2010 at 17:57, Sebastian Alonso wrote:

> Hey everyone, I'm working on a script which uses subprocess to launch a
> bunch of installers, but I'm getting problems with .msi installers
> although .exe ones work fine. The output I get is this:
>
 import subprocess
 p = subprocess.Popen('python.msi')

You need to add shell=True to "execute" anything which isn't a binary
executable (EXE).

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


Re: Serialize my class as JSON causes "__init__() got an unexpected keyword argument 'indent'" ?

2010-12-18 Thread shearichard
On Dec 18, 11:30 pm, Peter Otten <__pete...@web.de> wrote:
> shearichard wrote:
> > Hi - I've got a straightforward class I want to serialize as JSON
> > (actually I want to a serialize a list of them but I believe that's
> > irrelevant).
>
> > I've subclassed JSONEncoder and defined my own version of the
> > 'default' method ( based upon what I read at
> >http://docs.python.org/library/json.html) but when I then try to
> > serialize the class I get the (fairly weird) error message : "TypeError:
> > __init__() got an unexpected keyword argument 'indent'".
>
> > I suspect I'm doing something pretty fundamentally wrong but I don't
> > know what - can anyone tell me what's wrong (or alternatively tell me
> > how to log this as a bug ;-)
>
> > Here's my test case :
>
> > import json
> > class SuperPeople(object):
> >     pass
> > class People(SuperPeople, json.JSONEncoder):
> >     def __init__(self, name, age):
> >         self.__name = name
> >         self.__age = age
> >     def default(self, obj):
> >         if isinstance(obj, People):
> >             return [obj.__name, obj.__age]
> >         else:
> >             return json.JSONEncoder.default(self, obj)
>
> > def main():
> >     lstPeople = []
> >     lstPeople.append(People("Mary", 50))
> >     lstPeople.append(People("Joe", 40))
> >     lstPeople.append(People("Sue", 30))
>
> >     print json.dumps(lstPeople, cls=People)
>
> > if __name__ == "__main__":
> >     main()
>
> > ... and this is what the stacktrace looks like  
>
> > Traceback (most recent call last):
> >   File "testJSON.py", line 24, in 
> >     main()
> >   File "testJSON.py", line 20, in main
> >     json.dumps(lstPeople, cls=People)
> >   File "C:\bin\installed\Python2.6\lib\json\__init__.py", line 237, in
> > dumps
> >     **kw).encode(obj)
> > TypeError: __init__() got an unexpected keyword argument 'indent'
>
> > ... I'm running Python 2.6 on Win32.
>
> > All suggestions welcomed .
>
> You pass the encoder *class* to json.dumps(), so the function has to
> instantiate it. It does that with the arguments that an encoder class must
> accept. There's no way for it to expect that an encoder requires a name and
> an age.
>
> The solution is to separate the encoder and the class that shall be encoded.
> Here's one way:
>
> import json
>
> class People(object):
>     def __init__(self, name, age):
>         self.__name = name
>         self.__age = age
>     def get_json_state(self):
>         return [self.__name, self.__age]
>
> class PeopleEncoder(json.JSONEncoder):
>     def default(self, obj):
>         if isinstance(obj, People):
>             return obj.get_json_state()
>         else:
>             return json.JSONEncoder.default(self, obj)
>
> def main():
>     lstPeople = []
>     lstPeople.append(People("Mary", 50))
>     lstPeople.append(People("Joe", 40))
>     lstPeople.append(People("Sue", 30))
>
>     print json.dumps(lstPeople, cls=PeopleEncoder)
>
> if __name__ == "__main__":
>     main()

Brilliant - thank you very much.

Now that you've explained it I can see why the documentation is
written the way it is ! Before I saw your example I thought the
documentation was a bit strange but I can see now what it was trying
to tell me !

Your help is much appreciated.

Richard.

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


Re: If/then style question

2010-12-18 Thread Francesco

On 17/12/2010 0.51, Steven D'Aprano wrote:

Don't get me wrong... spaghetti code is*bad*. But there are other ways
of writing bad code too, and hanging around inside a function long after
you've finished is also bad:

def function(arg):
 done = False
 do_something()
 if some_condition:
 result = "finished"
 done = True
 if not done:
 do_something_else()
 if another_condition:
 result = "now we're finished"
 done = True
 if not done:
 do_yet_more_work()
 if third_condition:
 result = "finished this time for sure"
 done = True
 if not done:
 for i in range(100):
 if not done:
 do_something_small()
 if yet_another_condition:
 result = "finally done!"
 done = True
 return result

It's far more complicated than it need be, and does*lots*  of unnecessary
work. This can be written more simply and efficiently as:

def function(arg):
 do_something()
 if some_condition:
 return "finished"
 do_something_else()
 if another_condition:
 return "now we're finished"
 do_yet_more_work()
 if third_condition:
 return "finished this time for sure"
 for i in range(100):
 do_something_small()
 if yet_another_condition:
 return "finally done!"


I agree to your point, but I'm afraid you chose a wrong example (AFAIK, and 
that's not much).
Sure, the second version of function(arg) is much more readable, but why do you think the first one would do "*lots*  of unnecessary 
work"?

All the overhead in that function would be:
  if some_condition, three IF tests, and you know that's NOT a lot!
  if no conditions were met, (worst case) the first version would return an exception (unless result was globally defined) while 
the second would happily return None. Apart from this, the overhead in the first one would amount to one million IF tests, again not 
a lot these days. I don't think I would rewrite that function, if I found it written in the first way...

I don't mean that the fist example is better, just I'm sure you could imagine a 
more compelling proof of your concept.
Maybe there's something I don't know... in that case, please enlighten me!

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


Re: Serialize my class as JSON causes "__init__() got an unexpected keyword argument 'indent'" ?

2010-12-18 Thread Peter Otten
shearichard wrote:

> Hi - I've got a straightforward class I want to serialize as JSON
> (actually I want to a serialize a list of them but I believe that's
> irrelevant).
> 
> I've subclassed JSONEncoder and defined my own version of the
> 'default' method ( based upon what I read at
> http://docs.python.org/library/json.html ) but when I then try to
> serialize the class I get the (fairly weird) error message : "TypeError:
> __init__() got an unexpected keyword argument 'indent'".
> 
> I suspect I'm doing something pretty fundamentally wrong but I don't
> know what - can anyone tell me what's wrong (or alternatively tell me
> how to log this as a bug ;-)
> 
> Here's my test case :
> 
> import json
> class SuperPeople(object):
> pass
> class People(SuperPeople, json.JSONEncoder):
> def __init__(self, name, age):
> self.__name = name
> self.__age = age
> def default(self, obj):
> if isinstance(obj, People):
> return [obj.__name, obj.__age]
> else:
> return json.JSONEncoder.default(self, obj)
> 
> def main():
> lstPeople = []
> lstPeople.append(People("Mary", 50))
> lstPeople.append(People("Joe", 40))
> lstPeople.append(People("Sue", 30))
> 
> print json.dumps(lstPeople, cls=People)
> 
> 
> if __name__ == "__main__":
> main()
> 
> 
> ... and this is what the stacktrace looks like  
> 
> Traceback (most recent call last):
>   File "testJSON.py", line 24, in 
> main()
>   File "testJSON.py", line 20, in main
> json.dumps(lstPeople, cls=People)
>   File "C:\bin\installed\Python2.6\lib\json\__init__.py", line 237, in
> dumps
> **kw).encode(obj)
> TypeError: __init__() got an unexpected keyword argument 'indent'
> 
> 
> ... I'm running Python 2.6 on Win32.
> 
> All suggestions welcomed .

You pass the encoder *class* to json.dumps(), so the function has to 
instantiate it. It does that with the arguments that an encoder class must 
accept. There's no way for it to expect that an encoder requires a name and 
an age.

The solution is to separate the encoder and the class that shall be encoded. 
Here's one way:

import json

class People(object):
def __init__(self, name, age):
self.__name = name
self.__age = age
def get_json_state(self):
return [self.__name, self.__age]

class PeopleEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, People):
return obj.get_json_state()
else:
return json.JSONEncoder.default(self, obj)

def main():
lstPeople = []
lstPeople.append(People("Mary", 50))
lstPeople.append(People("Joe", 40))
lstPeople.append(People("Sue", 30))

print json.dumps(lstPeople, cls=PeopleEncoder)


if __name__ == "__main__":
main()


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


Serialize my class as JSON causes "__init__() got an unexpected keyword argument 'indent'" ?

2010-12-18 Thread shearichard
Hi - I've got a straightforward class I want to serialize as JSON
(actually I want to a serialize a list of them but I believe that's
irrelevant).

I've subclassed JSONEncoder and defined my own version of the
'default' method ( based upon what I read at 
http://docs.python.org/library/json.html
) but when I then try to serialize the class I get the (fairly weird)
error message : "TypeError: __init__() got an unexpected keyword
argument 'indent'".

I suspect I'm doing something pretty fundamentally wrong but I don't
know what - can anyone tell me what's wrong (or alternatively tell me
how to log this as a bug ;-)

Here's my test case :

import json
class SuperPeople(object):
pass
class People(SuperPeople, json.JSONEncoder):
def __init__(self, name, age):
self.__name = name
self.__age = age
def default(self, obj):
if isinstance(obj, People):
return [obj.__name, obj.__age]
else:
return json.JSONEncoder.default(self, obj)

def main():
lstPeople = []
lstPeople.append(People("Mary", 50))
lstPeople.append(People("Joe", 40))
lstPeople.append(People("Sue", 30))

print json.dumps(lstPeople, cls=People)


if __name__ == "__main__":
main()


... and this is what the stacktrace looks like  

Traceback (most recent call last):
  File "testJSON.py", line 24, in 
main()
  File "testJSON.py", line 20, in main
json.dumps(lstPeople, cls=People)
  File "C:\bin\installed\Python2.6\lib\json\__init__.py", line 237, in
dumps
**kw).encode(obj)
TypeError: __init__() got an unexpected keyword argument 'indent'


... I'm running Python 2.6 on Win32.

All suggestions welcomed .

Richard.




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