Calling super() in __init__ of a metaclass

2011-08-06 Thread Eli Bendersky
Consider this standard metaclass definition:

class MyMetaclass(type):
def __init__(cls, name, bases, dct):
super(MyMetaclass, cls).__init__(name, bases, dct)
# do meta-stuff

class Foo(object):
__metaclass__ = MyMetaclass

The call "super(MyMetaclass, cls)" should returns the parent of
MyMetaclass here. But the 'cls' passed into this __init__ is *not*
MyMetaclass, but rather the created class - i.e. Foo. So how does
"super" get to the parent of MyMetaclass using this information? The
documentation of "super" says:

If the second argument is a type, issubclass(type2, type) must be
true (this is useful for classmethods).

Yes, 'cls' is a type (it's "class Foo"), but no, it's not a subclass
of MyMetaclass, so this doesn't help.

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


can virtualenv run without the main installation?

2011-08-06 Thread smith jack
At first i have a python environment, after using virtualenv test
command, a new environment named test is created, in that directory
have some of the executable commands
such as python.exe, so can i program without the main installation of python?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I implement two decorators in Python both of which would eventually want to call the calling function

2011-08-06 Thread Rafael Durán Castañeda
You don't need doing something special, each decorator returns a new 
function that and only the result function formed by all decorators will 
be run. Consider this:


def clear_cache(func):
def decorator(*args, **kwargs):
print "cache cleared"
return func(*args, **kwargs)
return decorator


def login(func):
def decorator(*args, **kwargs):
print "login"
return func(*args, **kwargs)
return decorator

@login
@clear_cache
def post(msg= "POST", *args):
print msg
return args


result = post("POST", "something")
print result

The output will be:

login
cache cleared
POST
('something',)

On 06/08/11 07:49, Devraj wrote:

Hi all,

I am trying to simply my Web application handlers, by using Python
decorators.

Essentially I want to use decorators to abstract code that checks for
authenticated sessions and the other that checks to see if the cache
provider (Memcache in this instance) has a suitable response.

Consider this method definition with the decorators:

@auth.login_required
@cache.clear
def post(self, facility_type_id = None):

auth.login_required checks to see if the user is logged in, otherwise
returns an appropriate error message, or executes the original
function.

cache.clear would check to to see if the cache has a particular key
and drop that, before it executes the calling method.

Both auth.login_required and cache.clear would want to eventually
execute the calling method (post).

> From what I've read both, doing what I am doing now would execute the
calling method (post) twice.

My question, how do I chain decorators that end up executing the
calling method, but ensure that it's only called once.

Appreciate any pointers and thanks for your time.


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


Re: How do I implement two decorators in Python both of which would eventually want to call the calling function

2011-08-06 Thread Chris Rebert
On Fri, Aug 5, 2011 at 10:49 PM, Devraj  wrote:
> Hi all,
>
> I am trying to simply my Web application handlers, by using Python
> decorators.
>
> Essentially I want to use decorators to abstract code that checks for
> authenticated sessions and the other that checks to see if the cache
> provider (Memcache in this instance) has a suitable response.
>
> Consider this method definition with the decorators:
>
> @auth.login_required
> @cache.clear
> def post(self, facility_type_id = None):
>
> auth.login_required checks to see if the user is logged in, otherwise
> returns an appropriate error message, or executes the original
> function.
>
> cache.clear would check to to see if the cache has a particular key
> and drop that, before it executes the calling method.
>
> Both auth.login_required and cache.clear would want to eventually
> execute the calling method (post).
>
> >From what I've read both, doing what I am doing now would execute the
> calling method (post) twice.

That's incorrect unless the decorators you're using are weird.

> My question, how do I chain decorators that end up executing the
> calling method, but ensure that it's only called once.

That's how it works normally; decorators stack (and order is therefore
important). With normal wrapping decorators, only the first decorator
gets access to the original function and is able to call it.
Subsequent decorators only get access to the already-wrapped function.

Example:

def decorator_A(func):
def decorated(*args, **kwds):
print "In decorator A"
return func(*args, **kwds)
return decorated

def decorator_B(func):
def decorated(*args, **kwds):
print "In decorator B"
return func(*args, **kwds)
return decorated

@decorator_B
@decorator_A
def myfunc(arg):
print "hello", arg

>>> myfunc('bob')
In decorator B
In decorator A
hello bob


Notice that myfunc() only got executed once.

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


Re: can virtualenv run without the main installation?

2011-08-06 Thread smith jack
env create by virtualenv will refer to the main env, how did it find
the main env, is there any configuration files, if yes, where is it?

2011/8/6 smith jack :
> At first i have a python environment, after using virtualenv test
> command, a new environment named test is created, in that directory
> have some of the executable commands
> such as python.exe, so can i program without the main installation of python?
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I implement two decorators in Python both of which would eventually want to call the calling function

2011-08-06 Thread Peter Otten
Devraj wrote:

> Hi all,
> 
> I am trying to simply my Web application handlers, by using Python
> decorators.
> 
> Essentially I want to use decorators to abstract code that checks for
> authenticated sessions and the other that checks to see if the cache
> provider (Memcache in this instance) has a suitable response.
> 
> Consider this method definition with the decorators:
> 
> @auth.login_required
> @cache.clear
> def post(self, facility_type_id = None):
> 
> auth.login_required checks to see if the user is logged in, otherwise
> returns an appropriate error message, or executes the original
> function.
> 
> cache.clear would check to to see if the cache has a particular key
> and drop that, before it executes the calling method.
> 
> Both auth.login_required and cache.clear would want to eventually
> execute the calling method (post).
> 
> From what I've read both, doing what I am doing now would execute the
> calling method (post) twice.
> 
> My question, how do I chain decorators that end up executing the
> calling method, but ensure that it's only called once.

You typically don't need to do anything special for the above to work:

>>> def v(f):
... print "decorator v, wrapping", f.__name__, "into a"
... def a(*args, **kw):
... print "calling", f.__name__, "from a"
... return f(*args, **kw)
... return a
...
>>> def w(f):
... print "decorator w, wrapping", f.__name__, "into b"
... def b(*args, **kw):
... print "calling", f.__name__, "from b"
... return f(*args, **kw)
... return b
...
>>> @v
... @w
... def f(s):
... print s
...
decorator w, wrapping f into b
decorator v, wrapping b into a
>>> f("hello")
calling b from a
calling f from b
hello

The output shows that w wraps f into b, but v then doesn't get to see the 
original f, it wraps b into a. Put another way

@v
@w
def f(): ...

is the same as

def f(): ...

f = v(w(f))

and calling f() now is equivalent to calling a() which may or may not invoke 
b() which may or may not invoke the original f().

Translated into your example:

def post(self, facility_type_id = None): ...
post = auth.login_required(cache.clear(post))

The cache should only be cleared after a successful login, and the original 
post() will only be invoked after a successful login and with a cleared 
cache.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Calling super() in __init__ of a metaclass

2011-08-06 Thread Chris Rebert
On Sat, Aug 6, 2011 at 12:34 AM, Eli Bendersky  wrote:
> Consider this standard metaclass definition:
>
> class MyMetaclass(type):
>    def __init__(cls, name, bases, dct):
>        super(MyMetaclass, cls).__init__(name, bases, dct)
>        # do meta-stuff
>
> class Foo(object):
>    __metaclass__ = MyMetaclass
>
> The call "super(MyMetaclass, cls)" should returns the parent of
> MyMetaclass here. But the 'cls' passed into this __init__ is *not*
> MyMetaclass, but rather the created class - i.e. Foo.

...which is an instance of the first argument to super(), which is how
super is typically invoked. Remember: a class is an instance of its
metaclass.

Perhaps if you rename `cls` to `self` and then re-read your snippet,
you'll have a flash of sudden understanding. Calling the parameter
`cls` is merely convention.

> So how does
> "super" get to the parent of MyMetaclass using this information? The
> documentation of "super" says:
>
>    If the second argument is a type, issubclass(type2, type) must be
> true (this is useful for classmethods).
>
> Yes, 'cls' is a type (it's "class Foo"), but no, it's not a subclass
> of MyMetaclass, so this doesn't help.

The typical form of super(), mentioned earlier in the documentation,
is being used here:

super(type, obj) -> bound super object; requires isinstance(obj, type)

`type` here is the metaclass, `obj` here is the class. By definition,
a class is an *instance* of its metaclass, so the precondition is
satisfied.

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


Re: Calling super() in __init__ of a metaclass

2011-08-06 Thread Peter Otten
Eli Bendersky wrote:

> Consider this standard metaclass definition:
> 
> class MyMetaclass(type):
> def __init__(cls, name, bases, dct):
> super(MyMetaclass, cls).__init__(name, bases, dct)
> # do meta-stuff
> 
> class Foo(object):
> __metaclass__ = MyMetaclass
> 
> The call "super(MyMetaclass, cls)" should returns the parent of
> MyMetaclass here. But the 'cls' passed into this __init__ is *not*
> MyMetaclass, but rather the created class - i.e. Foo. So how does
> "super" get to the parent of MyMetaclass using this information? The
> documentation of "super" says:
> 
> If the second argument is a type, issubclass(type2, type) must be
> true (this is useful for classmethods).
> 
> Yes, 'cls' is a type (it's "class Foo"), but no, it's not a subclass
> of MyMetaclass, so this doesn't help.

Don't let yourself get confused by the name 'cls' for what is normally 
called 'self'. Foo is an instance of MyMetaclass, so the situation is 
exactly the same as in

class A(object):
def __init__(self, ...)
super(A, self).__init__(...)

I don't know how exactly super() is implemented, but to go from an instance 
to its class you can use type(instance) or instance.__class__.


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


Re: Calling super() in __init__ of a metaclass

2011-08-06 Thread Eli Bendersky
On Sat, Aug 6, 2011 at 11:04, Chris Rebert  wrote:
> On Sat, Aug 6, 2011 at 12:34 AM, Eli Bendersky  wrote:
>> Consider this standard metaclass definition:
>>
>> class MyMetaclass(type):
>>    def __init__(cls, name, bases, dct):
>>        super(MyMetaclass, cls).__init__(name, bases, dct)
>>        # do meta-stuff
>>
>> class Foo(object):
>>    __metaclass__ = MyMetaclass
>>
>> The call "super(MyMetaclass, cls)" should returns the parent of
>> MyMetaclass here. But the 'cls' passed into this __init__ is *not*
>> MyMetaclass, but rather the created class - i.e. Foo.
>
> ...which is an instance of the first argument to super(), which is how
> super is typically invoked. Remember: a class is an instance of its
> metaclass.
>
> Perhaps if you rename `cls` to `self` and then re-read your snippet,
> you'll have a flash of sudden understanding. Calling the parameter
> `cls` is merely convention.
>
>> So how does
>> "super" get to the parent of MyMetaclass using this information? The
>> documentation of "super" says:
>>
>>    If the second argument is a type, issubclass(type2, type) must be
>> true (this is useful for classmethods).
>>
>> Yes, 'cls' is a type (it's "class Foo"), but no, it's not a subclass
>> of MyMetaclass, so this doesn't help.
>
> The typical form of super(), mentioned earlier in the documentation,
> is being used here:
>
>    super(type, obj) -> bound super object; requires isinstance(obj, type)
>
> `type` here is the metaclass, `obj` here is the class. By definition,
> a class is an *instance* of its metaclass, so the precondition is
> satisfied.

Thanks, Chris. This clarifies things.

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


install editra

2011-08-06 Thread 守株待兔
sudo python /opt/Editra-0.6.58/setup.py install
Traceback (most recent call last):
  File "/opt/Editra-0.6.58/setup.py", line 639, in 
DoSourcePackage()
  File "/opt/Editra-0.6.58/setup.py", line 498, in DoSourcePackage
DATA = GenerateSrcPackageFiles()
  File "/opt/Editra-0.6.58/setup.py", line 190, in GenerateSrcPackageFiles
for loc_dir in os.listdir("locale"):
OSError: [Errno 2] No such file or directory: 'locale'

how to solve it??-- 
http://mail.python.org/mailman/listinfo/python-list


Re: install editra

2011-08-06 Thread Steven D'Aprano
守株待兔 wrote:

> sudo python /opt/Editra-0.6.58/setup.py install
> Traceback (most recent call last):
>   File "/opt/Editra-0.6.58/setup.py", line 639, in 
> DoSourcePackage()
>   File "/opt/Editra-0.6.58/setup.py", line 498, in DoSourcePackage
> DATA = GenerateSrcPackageFiles()
>   File "/opt/Editra-0.6.58/setup.py", line 190, in GenerateSrcPackageFiles
> for loc_dir in os.listdir("locale"):
> OSError: [Errno 2] No such file or directory: 'locale'
> 
> how to solve it??


Try:

cd /opt/Editra-0.6.58/
sudo python setup.py install


and if that doesn't work, try asking on an Editra mailing list.



-- 
Steven

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


Segmentation Fault on exit

2011-08-06 Thread Vipul Raheja
Hi,

I have wrapped a library from C++ to Python using SWIG. But when I
import it in Python, I am able to work fine with it, but it gives a
segmentation fault while exiting. Following is the log:

vipul@vipul-laptop:~/ossim-svn/src/pyossim/swig$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyossim
>>>
* Do some stuff *
>>> exit()
Segmentation fault
vipul@vipul-laptop:~/ossim-svn/src/pyossim/swig$

Kindly help.

Thanks and Regards,
Vipul Raheja
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Segmentation Fault on exit

2011-08-06 Thread Chris Angelico
On Sat, Aug 6, 2011 at 11:35 AM, Vipul Raheja  wrote:
> Hi,
>
> I have wrapped a library from C++ to Python using SWIG. But when I
> import it in Python, I am able to work fine with it, but it gives a
> segmentation fault while exiting. Following is the log:

The most likely cause of this is that you DECREF'd an object when you
shouldn't have, or failed to INCREF one when you should have. Check
over your object usage; if you can narrow down the "Do some stuff" bit
to the one function call that causes the crash, it'll help you figure
out where the error is.

Hope that helps!

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


Re: Segmentation Fault on exit

2011-08-06 Thread Vipul Raheja
Hi Chris,

Thanks for the reply.

However, the error occurs even if I don't do anything, that is, even if I
simply import the library and exit() after that.

I created a file a.py whose contents were the following:

import pyossim
exit()

Upon execution, the log was as follows:

vipul@vipul-laptop:~/ossim-svn/src/pyossim/swig$ python a.py
Segmentation fault

Following was the gdb log:

vipul@vipul-laptop:~/ossim-svn/src/pyossim/swig$ gdb `which python`
GNU gdb (GDB) 7.2-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /usr/bin/python...(no debugging symbols found)...done.

(gdb) run a.py
Starting program: /usr/bin/python a.py
[Thread debugging using libthread_db enabled]

Program received signal SIGSEGV, Segmentation fault.
0x0029 in ?? ()

(gdb) bt
#0  0x0029 in ?? ()
#1  0x0171de9f in OpenThreads::Mutex::~Mutex() () from
/usr/lib/libOpenThreads.so.13
#2  0x0035369e in ?? () from /lib/libc.so.6
#3  0x0035370f in exit () from /lib/libc.so.6
#4  0x080fc854 in ?? ()
#5  0x080fcdf5 in ?? ()
#6  0x080fdc9b in PyRun_SimpleFileExFlags ()
#7  0x0805b6d3 in Py_Main ()
#8  0x0805a8ab in main ()

(gdb)

Regards,
Vipul Raheja


On Sat, Aug 6, 2011 at 4:32 PM, Chris Angelico  wrote:

> On Sat, Aug 6, 2011 at 11:35 AM, Vipul Raheja 
> wrote:
> > Hi,
> >
> > I have wrapped a library from C++ to Python using SWIG. But when I
> > import it in Python, I am able to work fine with it, but it gives a
> > segmentation fault while exiting. Following is the log:
>
> The most likely cause of this is that you DECREF'd an object when you
> shouldn't have, or failed to INCREF one when you should have. Check
> over your object usage; if you can narrow down the "Do some stuff" bit
> to the one function call that causes the crash, it'll help you figure
> out where the error is.
>
> Hope that helps!
>
> Chris Angelico
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Segmentation Fault on exit

2011-08-06 Thread Chris Angelico
On Sat, Aug 6, 2011 at 12:16 PM, Vipul Raheja  wrote:
> Hi Chris,
> Thanks for the reply.
> However, the error occurs even if I don't do anything, that is, even if I
> simply import the library and exit() after that.
> I created a file a.py whose contents were the following:
> import pyossim
> exit()

In that case, it may be in your initialization/cleanup code. Check to
see if you return Py_None without increffing it, or keep a reference
to something you don't own and decref it on exit.

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


Hi

2011-08-06 Thread Naresh Agrawal
Looking for a Python Developer with





 
  
  ·
  Database (Oracle or SQL server ) – MUST
  have Capital Market Experience  - Fixed Income Market / Trading
  Systems 
   
  ·
  Java/.Net as the base development
  expertise
  
 










Please let me know if any one is interested to work 
Location is NY

Thanks

Nick
nka...@yahoo.com

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


How to make the program support communication behind NAT device?

2011-08-06 Thread smith jack
The subnet behind my router is 192.168.1.0/24, my pc ip is 192.168.1.9,
the server written with python is hosted on 192.168.1.3 on port 1033,
i can connect to this server from my pc

But cannot connect to this server when outside of this subnet? why?

I have made port translate on router, that is 10.10.9.2:1033 to 192.168.1.3:1033

what's wrong?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I implement two decorators in Python both of which would eventually want to call the calling function

2011-08-06 Thread Tim Chase

On 08/06/2011 02:49 AM, Chris Rebert wrote:

On Fri, Aug 5, 2011 at 10:49 PM, Devraj  wrote:

My question, how do I chain decorators that end up executing the
calling method, but ensure that it's only called once.


That's how it works normally; decorators stack (and order is therefore
important). With normal wrapping decorators, only the first decorator
gets access to the original function and is able to call it.


I'd clarify "first decorator" here as the one closest to the 
decorated function which is actually the *last* one in the list 
of decorators, but first-to-decorate.  In Chris's example below, 
decorator_A is the only one that calls myfunc().



Subsequent decorators only get access to the already-wrapped function.

Example:

def decorator_A(func):
 def decorated(*args, **kwds):
 print "In decorator A"
 return func(*args, **kwds)
 return decorated

def decorator_B(func):
 def decorated(*args, **kwds):
 print "In decorator B"
 return func(*args, **kwds)
 return decorated

@decorator_B
@decorator_A
def myfunc(arg):
 print "hello", arg


myfunc('bob')

In decorator B
In decorator A
hello bob


Notice that myfunc() only got executed once.


-tkc



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


Re: How to make the program support communication behind NAT device?

2011-08-06 Thread Chris Angelico
On Sat, Aug 6, 2011 at 11:20 AM, smith jack  wrote:
> The subnet behind my router is 192.168.1.0/24, my pc ip is 192.168.1.9,
> the server written with python is hosted on 192.168.1.3 on port 1033,
> i can connect to this server from my pc
>
> But cannot connect to this server when outside of this subnet? why?
>
> I have made port translate on router, that is 10.10.9.2:1033 to 
> 192.168.1.3:1033

This is a networking question, not a Python question. You'll probably
get better results on a forum focused on networking.

Your router's address, 10.10.9.2, is another RFC 1918 address, just
like 192.168.1.3. Are you attempting to connect from elsewhere on the
10.* network, or elsewhere on the internet? If the latter, you
probably have to deal with a second level of NAT.

The easiest way to test: Can you talk to (eg ping) the router,
10.10.9.2, from the computer that's unable to reach your server? If
not, there's your problem.

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


Re: Segmentation Fault on exit

2011-08-06 Thread Vipul Raheja
Hi,

I did look out for the problem's solution but have been struggling with it
ever since. I also asked around on IRC too but haven't quite progressed
towards the solution. Here is the link to the swig-generated .cxx file which
we need to look into for the answers. Could you please have a look at it and
help? It'll be really great.

Here's the link: www.geofemengineering.it/data/master_wrap.cxx

Thanks and Regards,
Vipul Raheja

On Sat, Aug 6, 2011 at 4:53 PM, Chris Angelico  wrote:

> On Sat, Aug 6, 2011 at 12:16 PM, Vipul Raheja 
> wrote:
> > Hi Chris,
> > Thanks for the reply.
> > However, the error occurs even if I don't do anything, that is, even if I
> > simply import the library and exit() after that.
> > I created a file a.py whose contents were the following:
> > import pyossim
> > exit()
>
> In that case, it may be in your initialization/cleanup code. Check to
> see if you return Py_None without increffing it, or keep a reference
> to something you don't own and decref it on exit.
>
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Segmentation Fault on exit

2011-08-06 Thread Dan Stromberg
I have little reason to doubt that it's related to referencing counting,
but:

http://stromberg.dnsalias.org/~dstromberg/checking-early.html

On Sat, Aug 6, 2011 at 3:35 AM, Vipul Raheja  wrote:

> Hi,
>
> I have wrapped a library from C++ to Python using SWIG. But when I
> import it in Python, I am able to work fine with it, but it gives a
> segmentation fault while exiting. Following is the log:
>
> vipul@vipul-laptop:~/ossim-svn/src/pyossim/swig$ python
> Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
> [GCC 4.4.5] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import pyossim
> >>>
> * Do some stuff *
> >>> exit()
> Segmentation fault
> vipul@vipul-laptop:~/ossim-svn/src/pyossim/swig$
>
> Kindly help.
>
> Thanks and Regards,
> Vipul Raheja
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Segmentation Fault on exit

2011-08-06 Thread Chris Angelico
On Sat, Aug 6, 2011 at 2:34 PM, Vipul Raheja  wrote:
> Here's the link: www.geofemengineering.it/data/master_wrap.cxx
> Thanks and Regards,
> Vipul Raheja

Ugh. Unfortunately that file is somewhat lengthy... I hate to say
"tl;dr" to people, but... is there any way to simplify that down?

Perhaps the best way to proceed would be to make a completely empty
SWIG-Python module, with absolutely none of the actual interface code,
just initializing itself and cleaning up. If that doesn't crash, add
code back in until it crashes on exit again.

Alternatively, you may be able to get good results by compiling a
debug build of Python, with reference tracking. I've never done that
myself, but others on this list will be able to advise.

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


how to separate a list into two lists?

2011-08-06 Thread smith jack
if a list L is composed with tuple consists of two elements, that is
L = [(a1, b1), (a2, b2) ... (an, bn)]

is there any simple way to divide this list into two separate lists , such that
L1 = [a1, a2... an]
L2=[b1,b2 ... bn]

i do not want to use loop, any methods to make this done?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to separate a list into two lists?

2011-08-06 Thread Chris Angelico
On Sat, Aug 6, 2011 at 6:07 PM, smith jack  wrote:
> if a list L is composed with tuple consists of two elements, that is
> L = [(a1, b1), (a2, b2) ... (an, bn)]
>
> is there any simple way to divide this list into two separate lists , such 
> that
> L1 = [a1, a2... an]
> L2=[b1,b2 ... bn]
>
> i do not want to use loop, any methods to make this done?

One easy way is to use list comprehensions. Technically that'll
involve a loop, but the loop is handled efficiently under the hood.

L1 = [x[0] for x in L]
L2 = [x[1] for x in L]

Another way would be to construct a dictionary from your list of
tuples and then use the keys() and values() of that dictionary to form
your lists (use collections.OrderedDict if the order matters). But I
think the list comps are the best way.

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


Re: how to separate a list into two lists?

2011-08-06 Thread Gary Herron

On 08/06/2011 10:07 AM, smith jack wrote:

if a list L is composed with tuple consists of two elements, that is
L = [(a1, b1), (a2, b2) ... (an, bn)]

is there any simple way to divide this list into two separate lists , such that
L1 = [a1, a2... an]
L2=[b1,b2 ... bn]

i do not want to use loop, any methods to make this done?


List comprehension:

   L1 = [item[0] for item in L]
   L2 = [item[1] for item in L]

which *is* still a loop.  (You are going to have to write *really* 
arcane code to have no loop.)


Gary Herron


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


Re: how to separate a list into two lists?

2011-08-06 Thread Emile van Sebille

On 8/6/2011 10:07 AM smith jack said...

if a list L is composed with tuple consists of two elements, that is
L = [(a1, b1), (a2, b2) ... (an, bn)]

is there any simple way to divide this list into two separate lists , such that
L1 = [a1, a2... an]
L2=[b1,b2 ... bn]



>>> L = [('a1', 'b1'), ('a2', 'b2'),('an', 'bn')]
>>> zip(*L)
[('a1', 'a2', 'an'), ('b1', 'b2', 'bn')]


Emile


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


Re: how to separate a list into two lists?

2011-08-06 Thread Kabie
No.

L1, L2 = zip(*L)

2011/8/7 Chris Angelico 

> On Sat, Aug 6, 2011 at 6:07 PM, smith jack  wrote:
> > if a list L is composed with tuple consists of two elements, that is
> > L = [(a1, b1), (a2, b2) ... (an, bn)]
> >
> > is there any simple way to divide this list into two separate lists ,
> such that
> > L1 = [a1, a2... an]
> > L2=[b1,b2 ... bn]
> >
> > i do not want to use loop, any methods to make this done?
>
> One easy way is to use list comprehensions. Technically that'll
> involve a loop, but the loop is handled efficiently under the hood.
>
> L1 = [x[0] for x in L]
> L2 = [x[1] for x in L]
>
> Another way would be to construct a dictionary from your list of
> tuples and then use the keys() and values() of that dictionary to form
> your lists (use collections.OrderedDict if the order matters). But I
> think the list comps are the best way.
>
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Fwd: how to separate a list into two lists?

2011-08-06 Thread Zero Piraeus
:

> if a list L is composed with tuple consists of two elements, that is
> L = [(a1, b1), (a2, b2) ... (an, bn)]
>
> is there any simple way to divide this list into two separate lists , such 
> that
> L1 = [a1, a2... an]
> L2=[b1,b2 ... bn]

How about this?

>>> L = [("a1", "b1"), ("a2", "b2"), ("an", "bn")]
>>> L1, L2 = zip(*L)
>>> L1
('a1', 'a2', 'an')
>>> L2
('b1', 'b2', 'bn')

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

 -[]z.
-- 
http://mail.python.org/mailman/listinfo/python-list


Problem installing 3.2.1 on a Linux server

2011-08-06 Thread John S James
My ISP (Bluehost) does not yet support Python version 3, but gave me shell
access (no sudo) so that I can install Python 3 myself on my server account
(running Apache). So I uploaded the standard Python.org installation package
(Python 3.2.1 for Linux) and followed instructions, starting with
./configure. These instructions had worked for me before, to install Python3
on a Linux netbook.

But on the server, ./configure starts fine, but later stops with an error.
Here is the section of the output where the error occurred:
*checking for build directories... done*
*configure: creating ./config.status*
*config.status: creating Makefile.pre*
*config.status: creating Modules/Setup.config*
*config.status: error: cannot find input file: `Misc/python.pc.in'*

Then the ./configure step stopped. Needless to say, the next step, Make, did
not work because there was no Makefile.

Others have received the same message, in different contexts. But I haven't
found a solution.

Two action items:

* For the Python community, it would help to fix this problem in the
standard Python distribution, if possible. People do want to run Python3 on
more servers. I could help by testing the fix to see if the Python3 install
now works.

* Meanwhile I'd appreciate any advice on a temporary fix (such as changing
the configure file, or where to find a package that I could install on the
server before installing Python3). All I need for now is the most basic
installation, to support CGI and Web forms.

John

-- 
John S. James
www.RepliCounts.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to separate a list into two lists?

2011-08-06 Thread bud
On Sun, 07 Aug 2011 01:07:00 +0800, smith jack wrote:

> if a list L is composed with tuple consists of two elements, that is L =
> [(a1, b1), (a2, b2) ... (an, bn)]
> 
> is there any simple way to divide this list into two separate lists ,
> such that L1 = [a1, a2... an]
> L2=[b1,b2 ... bn]
> 
> i do not want to use loop, any methods to make this done?


(x,y) = [ [z[i] for z in L] for i in range(len(L[0]))]

x
: ['a1', 'a2', 'an']

y
: ['b1', 'b2', 'bn']


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


Re: Complex sort on big files

2011-08-06 Thread sturlamolden
On Aug 1, 5:33 pm, aliman  wrote:

> I've read the recipe at [1] and understand that the way to sort a
> large file is to break it into chunks, sort each chunk and write
> sorted chunks to disk, then use heapq.merge to combine the chunks as
> you read them.

Or just memory map the file (mmap.mmap) and do an inline .sort() on
the bytearray (Python 3.2). With Python 2.7, use e.g. numpy.memmap
instead. If the file is large, use 64-bit Python. You don't have to
process the file in chunks as the operating system will take care of
those details.

Sturla

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


Re: how to separate a list into two lists?

2011-08-06 Thread bud
On Sat, 06 Aug 2011 10:24:10 -0700, Emile van Sebille wrote:

> On 8/6/2011 10:07 AM smith jack said...
>> if a list L is composed with tuple consists of two elements, that is L
>> = [(a1, b1), (a2, b2) ... (an, bn)]
>>
>> is there any simple way to divide this list into two separate lists ,
>> such that L1 = [a1, a2... an]
>> L2=[b1,b2 ... bn]
>>
>>
>  >>> L = [('a1', 'b1'), ('a2', 'b2'),('an', 'bn')] zip(*L)
> [('a1', 'a2', 'an'), ('b1', 'b2', 'bn')]
> 

Nice. :)  I forgot about zip, still learning Python myself. 

I'll have to check up on the *L - is that a reference?
I know in Perl, you can assign the lhs to a list,
below works because there are exactly 2 items on the rhs.
Does Python have a catchall, or an ignore the rest?
Example, if L was a tuple that had 3 or more items, 
the below lhs would fail.  Is this possible in Python?


(X,Y) = zip(*L)


X
: ('a1', 'a2', 'an')

Y
: ('b1', 'b2', 'bn')



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


Re: how to separate a list into two lists?

2011-08-06 Thread Chris Angelico
On Sat, Aug 6, 2011 at 7:21 PM, bud  wrote:
> Nice. :)  I forgot about zip, still learning Python myself.
>
> I'll have to check up on the *L - is that a reference?
> I

It expands the list into the arguments. It's the parallel to:

def func(*args):

which collapses the args into a list.

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


Re: can virtualenv run without the main installation?

2011-08-06 Thread Gelonida N
On 08/06/2011 09:38 AM, smith jack wrote:
> At first i have a python environment, after using virtualenv test
> command, a new environment named test is created, in that directory
> have some of the executable commands
> such as python.exe, so can i program without the main installation of python?

At least under linux virtualenv uses symbolic links.

So the answer is simple.

virtualenv need the pyton installation it was derived from.
only modules, that you will install under virtualenv will be installed
in the virtualenv directories.



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


Re: can virtualenv run without the main installation?

2011-08-06 Thread Gelonida N
On 08/06/2011 09:51 AM, smith jack wrote:
> env create by virtualenv will refer to the main env, how did it find
> the main env, is there any configuration files, if yes, where is it?
> 
> 2011/8/6 smith jack :
>> At first i have a python environment, after using virtualenv test
>> command, a new environment named test is created, in that directory
>> have some of the executable commands
>> such as python.exe, so can i program without the main installation of python?
>>


Under linux the first lien of virtualenv is

#!/usr/bin/python


Thus it will be run by python installed under /usr/bin/python


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


Re: can virtualenv run without the main installation?

2011-08-06 Thread Gelonida N
On 08/06/2011 09:51 AM, smith jack wrote:
> env create by virtualenv will refer to the main env, how did it find
> the main env, is there any configuration files, if yes, where is it?
> 
> 2011/8/6 smith jack :
>> At first i have a python environment, after using virtualenv test
>> command, a new environment named test is created, in that directory
>> have some of the executable commands
>> such as python.exe, so can i program without the main installation of python?
>>


Under linux the first lien of virtualenv is

#!/usr/bin/python


Thus it will be run by python installed under /usr/bin/python


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


Re: how to separate a list into two lists?

2011-08-06 Thread Gelonida N
On 08/06/2011 08:13 PM, bud wrote:
> On Sun, 07 Aug 2011 01:07:00 +0800, smith jack wrote:
> 
>> if a list L is composed with tuple consists of two elements, that is L =
>> [(a1, b1), (a2, b2) ... (an, bn)]
>>
>> is there any simple way to divide this list into two separate lists ,
>> such that L1 = [a1, a2... an]
>> L2=[b1,b2 ... bn]
>>
>> i do not want to use loop, any methods to make this done?
> 
> 
> (x,y) = [ [z[i] for z in L] for i in range(len(L[0]))]
> 
> x
> : ['a1', 'a2', 'an']
> 
> y
> : ['b1', 'b2', 'bn']
> 
> 
Asuming you are not an alias of Jack Smith and assuming you did not see
Jack's thread asking the same question:

x,y = unzip(*L)



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


Re: how to separate a list into two lists?

2011-08-06 Thread John Posner
On 2:59 PM, smith jack wrote:
> if a list L is composed with tuple consists of two elements, that is
> L = [(a1, b1), (a2, b2) ... (an, bn)]
>
> is there any simple way to divide this list into two separate lists , such 
> that
> L1 = [a1, a2... an]
> L2=[b1,b2 ... bn]
>
> i do not want to use loop, any methods to make this done?
>

The official Python documentation [1] notes that "zip()
 in conjunction with
the * operator can be used to unzip a list". Ex:

   >>> L = [ (1,'a'), (2,'b'), (3,'c'), (4,'d') ]
   >>> zip(*L)
   [(1, 2, 3, 4), ('a', 'b', 'c', 'd')]


HTH,
John

[1] http://docs.python.org/library/functions.html

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


Re: how to separate a list into two lists?

2011-08-06 Thread John Posner
On 2:59 PM, smith jack wrote:
> if a list L is composed with tuple consists of two elements, that is
> L = [(a1, b1), (a2, b2) ... (an, bn)]
>
> is there any simple way to divide this list into two separate lists , such 
> that
> L1 = [a1, a2... an]
> L2=[b1,b2 ... bn]
>
> i do not want to use loop, any methods to make this done?
>

The official Python documentation [1] notes that "zip()
 in conjunction with
the * operator can be used to unzip a list". Ex:

   >>> L = [ (1,'a'), (2,'b'), (3,'c'), (4,'d') ]
   >>> zip(*L)
   [(1, 2, 3, 4), ('a', 'b', 'c', 'd')]


HTH,
John

[1] http://docs.python.org/library/functions.html

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


help

2011-08-06 Thread aahan noor

Hi all: i am new to python. i am working with lm-sensors to monitor the cpu fan 
speed, temperature and voltages. i have  to use python code to get integer 
value of cpu temperature from lm sensors. please help me how i do that i shall 
be gratefullAahan   -- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to separate a list into two lists?

2011-08-06 Thread David Robinow
On Sat, Aug 6, 2011 at 1:23 PM, Kabie  wrote:
> No.
> L1, L2 = zip(*L)

Not quite. That makes L1 & L2 tuples.

L1, L2 = zip(*L)
L1 = list(L1)
L2 = list(L2)
???
-- 
http://mail.python.org/mailman/listinfo/python-list


DDE vs. COM for Python Windows apps

2011-08-06 Thread Kevin Walzer
I'm developing a Windows version of a Mac/Python app and I'd like to add 
a scripting interface to it. On the Mac I do this via Apple Events--this 
is an Apple technology that allows applications to drive other 
applications and query them for data using a message-passing interface. 
Each application defines a suite of commands and/or data objects that it 
will invoke, depending on which Apple Event is passed to it; these 
commands and data objects are most commonly documented in an AppleScript 
API dictionary, though there are other ways to send Apple Events as well.


On Windows, I've found two separate mechanisms that seem to offer what 
I'm looking for: Dynamic Data Exchange (DDE) and Component Object Model 
(COM). As I understand it, DDE allows data transfer and 
command-automation between applications via a lightweight 
message-passing interface. COM seems to offer similar functionality, 
with a much more complex learning curve.


The main complaint I've seen about DDE is that it is very old. The main 
complaint I've seen about COM is that it is very complex and very 
brittle. Based on my research, it seems that DDE might be sufficient for 
my needs--I want other applications to be able to send commands to my 
application and have data returned to them. But I'd like to have a 
better understanding of the merits of each.


Can someone summarize the advantages that COM might have over DDE, in a 
Pythonic context?


--Kevin

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problem installing 3.2.1 on a Linux server

2011-08-06 Thread Ned Deily
In article 
,
 John S James  wrote:
> My ISP (Bluehost) does not yet support Python version 3, but gave me shell
> access (no sudo) so that I can install Python 3 myself on my server account
> (running Apache). So I uploaded the standard Python.org installation package
> (Python 3.2.1 for Linux) and followed instructions, starting with
> ./configure. These instructions had worked for me before, to install Python3
> on a Linux netbook.
> 
> But on the server, ./configure starts fine, but later stops with an error.
> Here is the section of the output where the error occurred:
> *checking for build directories... done*
> *configure: creating ./config.status*
> *config.status: creating Makefile.pre*
> *config.status: creating Modules/Setup.config*
> *config.status: error: cannot find input file: `Misc/python.pc.in'*
> 
> Then the ./configure step stopped. Needless to say, the next step, Make, did
> not work because there was no Makefile.

Works for me on a current Debian system.  Are you downloading the source 
from python.org?  Did you verify that the file is created by tar? 

$ curl -O http://www.python.org/ftp/python/3.2.1/Python-3.2.1.tgz
$ tar xzf Python-3.2.1.tgz
$ cd ./Python-3.2.1/
$ ls -l Misc/python.pc.in
-rw-r- 1 nad nad 293 Jul  8 23:58 Misc/python.pc.in
$ ./configure
[...]
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Modules/Setup.config
config.status: creating Misc/python.pc
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup
creating Modules/Setup.local
creating Makefile

If you are still having problems, chances are you have an out-of-date or 
incomplete version of GNU autotools which contains the macros and tools 
that are used by the ./configure script.  On my system:

$ autoconf --version
autoconf (GNU Autoconf) 2.68

-- 
 Ned Deily,
 n...@acm.org

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


Re: help

2011-08-06 Thread Dan Stromberg
I'll be a lot easier for you to get help, if you take a shot at it yourself
first, then post a link to what you have here, along with any error messages
you may be getting.

On Sat, Aug 6, 2011 at 1:38 PM, aahan noor  wrote:

>
> Hi all:
>  i am new to python. i am working with lm-sensors to monitor the cpu fan 
> speed, temperature and voltages. i have  to use python code to get integer 
> value of cpu temperature from lm sensors. please help me how i do that
>  i shall be gratefull
>
> Aahan
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: DDE vs. COM for Python Windows apps

2011-08-06 Thread Chris Angelico
On Sat, Aug 6, 2011 at 10:33 PM, Kevin Walzer  wrote:
> The main complaint I've seen about DDE is that it is very old.

I can't speak about COM, as I have not used it to any great extent,
but DDE has a number of things going for it. Firstly though, "old"
does not need to be a criticism. How many network administrators use
ping(1) on a daily basis? Yet that utility was written in the early
1980s - in computing terms, that's pretty old.

DDE is implemented as window messages. This means it works with
anything that has a window; it might not be easy to send DDE requests
from a console application, and it would be quite tricky to receive
them in a console. It also means that the system can hang until the
request is dealt with. However, it is extremely simple and easy.

There are other options. The best thing to do would be to set down
your actual requirements, and then see what technology best fits them.
For instance:

* Do you need programs running on other computers to be able to
send/receive messages, or just on the local computer (ie, do you need
network support)?
* Do you need asynchronous requests/responses?
* Do you need to support multiple simultaneous requests?
* Conversely, must you explicitly forbid requests when another app is
using the API?
* Do you need to move significant amounts of data around, or could it
all be done with simple 32-bit integers?

If you need networking, far and away the best option is TCP sockets.
But your post suggests that something light-weight is what you're
after.

If you don't care about return values, or alternatively if you can
return an instant response, it might be easiest to simply use window
messages. You pass in a message number and two parameter integers, and
you get back a returned integer. That's enough for a lot of things,
but you have to be able to return very quickly.

You may wish to google for 'Windows IPC'; there's quite a few options
available. Deciding which one is best for your situation is partly art
and partly science... and partly faith in the future. I recommend
choosing the one that looks like the coolest and most fun to play
with; that way, even if the project fails, it's not been a complete
waste.

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


Re: how to separate a list into two lists?

2011-08-06 Thread Steven D'Aprano
Gelonida N wrote:

> Asuming you [Bud] are not an alias of Jack Smith and assuming you did 
> not see Jack's thread asking the same question:

That's a strange thing to say when Bud *answered* Jack's question.
 

> x,y = unzip(*L)

What's unzip? It doesn't exist in any version of Python between 1.5 and 3.3
that I have.



-- 
Steven

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


Re: how to separate a list into two lists?

2011-08-06 Thread Steven D'Aprano
bud wrote:

> I'll have to check up on the *L - is that a reference?

No, as Chris already answered, unary * is used for packing and unpacking
positional arguments to functions; unary ** is similarly used for
collecting keyword arguments.


> I know in Perl, you can assign the lhs to a list,
> below works because there are exactly 2 items on the rhs.
> Does Python have a catchall, or an ignore the rest?

Yes. In Python 3, you can do this:

>>> a, b, *t, c = (1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> t
[3, 4, 5, 6, 7, 8]

(but not in Python 2)


-- 
Steven

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


Re: how to separate a list into two lists?

2011-08-06 Thread Steven D'Aprano
Chris Angelico wrote:

> On Sat, Aug 6, 2011 at 6:07 PM, smith jack  wrote:
>> if a list L is composed with tuple consists of two elements, that is
>> L = [(a1, b1), (a2, b2) ... (an, bn)]
>>
>> is there any simple way to divide this list into two separate lists ,
>> such that L1 = [a1, a2... an]
>> L2=[b1,b2 ... bn]
>>
>> i do not want to use loop, any methods to make this done?
> 
> One easy way is to use list comprehensions. Technically that'll
> involve a loop, but the loop is handled efficiently under the hood.
> 
> L1 = [x[0] for x in L]
> L2 = [x[1] for x in L]

I hardly think that's "under the hood". It's right there: for x in L. How
much more explicitly a loop do you want?

To the original poster, Jack: if you don't loop over the list, how do you
expect to operate on each and every item?

Whether you write:

L1, L2 = [], []
for a,b in L:
L1.append(a)
L2.append(b)

or 

L1, L2 = zip(*L)

or 

L1 = [x[0] for x in L]
L2 = [x[1] for x in L]

you are still looping over the list. In the second case, the one-liner using
zip, you loop *twice*: once to unpack L into separate arguments, the second
time to zip them up. And the result you end up with are tuples, not lists,
so if you need lists, you have to loop *again*, over each tuple, converting
them to lists. Here's one way to do that while keeping it a one-liner:

L1, L2 = map(list, zip(*L))

If L is small, it really doesn't matter what you do, they will all be more
or less as efficient. Write whatever you feel looks best.

But if L is huge, then the difference between a four-liner that iterates
over the list once, and one-liner that iterates over it four times, may be
considerable.



-- 
Steven

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


Re: how to separate a list into two lists?

2011-08-06 Thread Tim Roberts
smith jack  wrote:
>
>if a list L is composed with tuple consists of two elements, that is
>L = [(a1, b1), (a2, b2) ... (an, bn)]
>
>is there any simple way to divide this list into two separate lists , such that
>L1 = [a1, a2... an]
>L2=[b1,b2 ... bn]
>
>i do not want to use loop, any methods to make this done?

There will always be a loop.  It might not be written with a "for"
statement, but there will always be a loop.

  L1 = [k[0] for k in L]
  L2 = [k[1] for k in L]

I did momentarily consider the following slimy solution:
  L1 = dict(L).keys()
  L2 = dict(L).values()
but that reorders the tuples.  They still correspond, but in a different
order.
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to separate a list into two lists?

2011-08-06 Thread Chris Angelico
On Sun, Aug 7, 2011 at 1:58 AM, Tim Roberts  wrote:
> I did momentarily consider the following slimy solution:
>  L1 = dict(L).keys()
>  L2 = dict(L).values()
> but that reorders the tuples.  They still correspond, but in a different
> order.
>

Which can be overcome with collections.OrderedDict. But what's dict(L)
going to do? It's going to loop over L, more than once in fact.

I guess the real question is: Why do you wish to avoid a loop?

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


help with DLL or path issue trying to use cx_Freeze

2011-08-06 Thread Bart Manchester
Im trying to use the most basic of cx_Freeze in a dos window and I receive the 
following error (see below).  I suspect I have some kind of path issue, but am 
at a loss as to how to proceed.  I am trying to execute this in the Scripts 
directory and have both the cxfreeze bat and also the .py file there.



C:\Python32\Scripts>cxfreeze general-tl1-work.py
Traceback (most recent call last):
  File "C:\Python32\Scripts\cxfreeze", line 5, in 
main()
  File "C:\Python32\lib\site-packages\cx_Freeze\main.py", line 187, in main
silent = options.silent)
  File "C:\Python32\lib\site-packages\cx_Freeze\freezer.py", line 84, in __init_
_
for n in self._GetDefaultBinPathExcludes() + binPathExcludes]
  File "C:\Python32\lib\site-packages\cx_Freeze\freezer.py", line 205, in _GetDe
faultBinPathExcludes
import cx_Freeze.util
ImportError: DLL load failed: %1 is not a valid Win32 application.

C:\Python32\Scripts>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to separate a list into two lists?

2011-08-06 Thread Steven D'Aprano
Chris Angelico wrote:

> On Sun, Aug 7, 2011 at 1:58 AM, Tim Roberts  wrote:
>> I did momentarily consider the following slimy solution:
>> L1 = dict(L).keys()
>> L2 = dict(L).values()
>> but that reorders the tuples.  They still correspond, but in a different
>> order.
>>
> 
> Which can be overcome with collections.OrderedDict. But what's dict(L)
> going to do? It's going to loop over L, more than once in fact.
> 
> I guess the real question is: Why do you wish to avoid a loop?

I think what the Original Poster actually meant was he wanted to avoid
*writing out an explicit loop*. That is, he wants a one-liner, so he
doesn't have to think about the details of iterating over the list.

When we write:

a = sum(a_sequence)

aren't we doing the same thing really?


-- 
Steven

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


how to dynamically generate __name__ for an object?

2011-08-06 Thread Eric Snow
Thought I knew how to provide a dynamic __name__ on instances of a
class.  My first try was to use a non-data descriptor:

# module base.py

class _NameProxy(object):
def __init__(self, oldname):
self.oldname = oldname
def __get__(self, obj, cls):
if obj is None:
return self.oldname
if "__name__" not in obj.__dict__:
return str(obj.__context__)
return obj.__name__

class _BaseMeta(type):
def __init__(cls, name, bases, namespace):
cls.__name__ = _NameProxy(name)

class Base(object):
__metaclass__ = _BaseMeta


$ python _base.py
Traceback (most recent call last):
  ...
  File "/usr/local/lib/python2.4/site-packages/base.py", line xx, in __init__
cls.__name__ = _NameProxy(name)
TypeError: Error when calling the metaclass bases
can only assign string to Base.__name__, not '_NameProxy'


Needless to say I was surprised.  After looking in typeobject.c, I
believe that __name__ must be a string where classes are concerned[1].
 So if I want all my instances to have a __name__ attribute, and for
it to be dynamically provided if it isn't set on the instance, what
are my options?  Or maybe I did something wrong and it should work as
I expected?

-eric


[1] http://hg.python.org/cpython/file/default/Objects/typeobject.c#l244
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to dynamically generate __name__ for an object?

2011-08-06 Thread Steven D'Aprano
Eric Snow wrote:

> Thought I knew how to provide a dynamic __name__ on instances of a
> class.  My first try was to use a non-data descriptor:

Perhaps you should explain what you are trying to do. If you want to give
instances their own name, why not just give them an instance
attribute "name"? Double-leading-and-trailing-underscore attributes are
reserved for Python, and have special semantics.


-- 
Steven

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


Re: how to separate a list into two lists?

2011-08-06 Thread Paul Rubin
smith jack  writes:
> if a list L is composed with tuple consists of two elements, that is
> L = [(a1, b1), (a2, b2) ... (an, bn)]
>
> is there any simple way to divide this list into two separate lists , such 
> that
> L1 = [a1, a2... an]
> L2=[b1,b2 ... bn]
>
> i do not want to use loop, any methods to make this done?

That is called unzipping and there is a sneaky idiom for it:

 L1,L2 = zip(*L)

Your homework assignment is figuring out how that works ;-).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to separate a list into two lists?

2011-08-06 Thread Vito 'ZeD' De Tullio
David Robinow wrote:

> On Sat, Aug 6, 2011 at 1:23 PM, Kabie  wrote:
>> No.
>> L1, L2 = zip(*L)
> 
> Not quite. That makes L1 & L2 tuples.
> 
> L1, L2 = zip(*L)
> L1 = list(L1)
> L2 = list(L2)
> ???

L1, L2 = map(list, zip(*L))

-- 
By ZeD

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