Re: Working with XML/XSD

2013-08-07 Thread dieter
David Barroso dbarro...@dravetech.com writes:

 I was wondering if someone could point me in the right direction. I would
 like to develop some scripts to manage Cisco routers and switches using
 XML. However, I am not sure where to start. Does someone have some
 experience working with XML, Schemas and things like that? Which libraries
 do you use? Do you know of any good tutorial?

I made good experience with PyXB.

This package takes an XML-schema definition and generates Python classes
from it. XML documents (conforming to the schema) can be parsed into
instances of those classes and instances of those classes can
be serialized as XML documents.

It hides well (many of) the complexities of XML-schema (if the
schemas are given).

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


Re: Reg secure python environment with web terminal emulator

2013-08-07 Thread dieter
Lakshmipathi.G lakshmipath...@gmail.com writes:

 We have a server running a web-based terminal emulator (based on shellinabox
 for screen-casting  check www.webminal.org) that allows users to learn
 simple bash commands. This Linux environment secured by things like quota,
 selinux,ulimit  etc

 Now some users are requesting python access. How to ensure python is executed
 in a restricted environment. I came across
 http://docs.python.org/2/library/restricted.html
 but it seems like disabled in 2.3. Any thoughts on how we can safely
 provide python access
 to users.

When you are satisfied with the protection you have achieved
for bash commands, those same protection might be sufficient
for Python as well. I assume that you used operating system
facilities to restrict what the (system) user can do on the
operating system level: the same restriction would apply to the
(same) user executing Python code.

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


Re: new to While statements

2013-08-07 Thread krismesenbrink
wow everyone thanks for the feed back! i'll have to rewrite this with 
everything you guys taught me. this makes ALOT more sense. :D
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Pool map with a method of a class and a list

2013-08-07 Thread Joshua Landau
On 6 August 2013 20:42, Luca Cerone luca.cer...@gmail.com wrote:
 Hi Chris, thanks

 Do you ever instantiate any A() objects? You're attempting to call an

 unbound method without passing it a 'self'.

 I have tried a lot of variations, instantiating the object, creating lambda 
 functions that use the unbound version of fun (A.fun.__func__) etc etc..
 I have played around it quite a bit before posting.

 As far as I have understood the problem is due to the fact that Pool pickle 
 the function and copy it in the various pools..
 But since the methods cannot be pickled this fails..

 The same example I posted won't run in Python 3.2 neither (I am mostly 
 interested in a solution for Python 2.7, sorry I forgot to mention that).

 Thanks in any case for the help, hopefully there will be some other advice in 
 the ML :)


I think you might not understand what Chris said.

Currently this does *not* work with Python 2.7 as you suggested it would.

 op = map(A.fun,l)
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: unbound method fun() must be called with A instance as
first argument (got int instance instead)

This, however, does:

 op = map(A(3).fun,l)
 op
[1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683]


Chris might have also been confused because once you fix that it works
in Python 3.

You will find that
http://stackoverflow.com/questions/1816958/cant-pickle-type-instancemethod-when-using-pythons-multiprocessing-pool-ma
explains the problem in more detail than I understand. I suggest
reading it and relaying further questions back to us. Or use Python 3
;).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Enum vs OrderedEnum

2013-08-07 Thread Ian Kelly
On Tue, Aug 6, 2013 at 7:55 PM, Ben Finney ben+pyt...@benfinney.id.au wrote:
 Ian Kelly ian.g.ke...@gmail.com writes:
 Terrain that is “radiated” would be terrain that has some kind of spokes
 spreading out from its centre. I think you mean “irradiated”.

 Hope the game goes well :-)

It's actually a reimplementation of a game from 1993, so I'm somewhat
stuck with the terminology.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pexpect, loading an entry field

2013-08-07 Thread Lakshmipathi.G
Hi -
I'm using Python 2.7.3 (Fedora 17) . I tried a simple example with
pexpect to copy a file to remote system. It works

$ cat pex.py
import pexpect
s = pexpect.spawn ('scp pex.py root@10.30.77.244:/tmp')
s.expect ('Password:')
s.sendline ('a')
s.expect(pexpect.EOF,timeout=20)

Execute above program (change remote ip and password) - If it works
then its not an issue with pexpect or python
environment.


-- 

Cheers,
Lakshmipathi.G
FOSS Programmer.
www.giis.co.in



On Tue, Aug 6, 2013 at 11:33 PM, inq1ltd inq1...@inqvista.com wrote:


 pexpect looks simple to use. Please check this example


 http://www.pythonforbeginners.com/systems-programming/how-to-use-the-pexpect

 -module-in-python/

  python help;

 

  I am using pexpect to open my program.

  Can someone tell me how to get data to appear in

  an entry field.

  After pexpect opens the my program I have tried to use

  send, sendline, and write functions to try to put data into

  the program's entry field.

  However, the data is going to the terminal window,

  the window that is used to initiate the call to pexpect

  but not to the entry field in the open program.

  I would appreciate suggestions.

 

  jol

 



 Thanks for the response.



 I have been there but that site gives me

 the same information that I get from

 noah.org. I have the pexpect docs and they presume

 that this problem doesn't exist.



 None of the information in the NOAH site or the FAQ's

 address this question.



 I'm using suse linux running python 2.7 and pexpect.



 The data, when using send() method is going to the terminal window,

 This is the same window that is used to initiate the call to pexpect.



 Pexpect opens the program but the send method sends

 data to the terminal window, not the entry field located

 in the child spawned by pexpect.



 interact works, as does close(), TIMEOUT, EOF, before, after,

 expect_exact and methods. But the entry field does not

 load.



 So my question is;



 Why??









 

 

 

  --

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


Re: Working with XML/XSD

2013-08-07 Thread David Barroso
I will start looking to the links and libraries you pointed out. As the
schema definitions are provided by the vendor I think I will go for PyXB.

Thanks!


On Wed, Aug 7, 2013 at 8:00 AM, dieter die...@handshake.de wrote:

 David Barroso dbarro...@dravetech.com writes:

  I was wondering if someone could point me in the right direction. I would
  like to develop some scripts to manage Cisco routers and switches using
  XML. However, I am not sure where to start. Does someone have some
  experience working with XML, Schemas and things like that? Which
 libraries
  do you use? Do you know of any good tutorial?

 I made good experience with PyXB.

 This package takes an XML-schema definition and generates Python classes
 from it. XML documents (conforming to the schema) can be parsed into
 instances of those classes and instances of those classes can
 be serialized as XML documents.

 It hides well (many of) the complexities of XML-schema (if the
 schemas are given).

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

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


Re: Enum vs OrderedEnum

2013-08-07 Thread Ian Kelly
On Tue, Aug 6, 2013 at 6:33 PM, Ethan Furman et...@stoneleaf.us wrote:
 class Environment(AutoNumber):

 gaia = 2.0
 fertile = 1.5
 terran = 1.0
 jungle = 1.0
 ocean = 1.0
 arid = 1.0
 steppe = 1.0
 desert = 1.0
 minimal = 1.0
 barren = 0.5
 tundra = 0.5
 dead = 0.5
 inferno = 0.5
 toxic = 0.5
 radiated = 0.5

 def __init__(self, growth_factor):
 self._growth_factor = growth_factor

 @property
 def growth_factor(self):
 return self._growth_factor

 This works because each Enum member gets its own integer value (1 - 15) in
 __new__, plus a growth factor that is stored by __init__.  Whether you think
 this is better I have no idea.  ;)

Black magic, I like it.  I think I'd write it like this, however:

class Environment(AutoNumber):

gaia = 2.0
fertile = 1.5
terran = jungle = ocean = arid = steppe = desert = minimal = 1.0
barren = tundra = dead = inferno = toxic = radiated = 0.5

def __init__(self, growth_factor):
self.growth_factor = growth_factor
-- 
http://mail.python.org/mailman/listinfo/python-list


beginner question (True False help)

2013-08-07 Thread eschneider92
I'm trying to create an option for the program to repeat if the user types 'y' 
or 'yes', using true and false values, or otherwise end the program. If anyone 
could explain to me how to get this code working, I'd appreciate it.

letters='abcdefghijklmn'
batman=True
def thingy():
print('type letter from a to n')
typedletter=input()
if typedletter in letters:
print('yes')
else:
print('no')
def repeat():
print('go again?')
goagain=input()
if goagain in ('y', 'yes'):
print('ok')
else:
print('goodbye')
batman=False
while batman==True:
thingy()
repeat()
print('this is the end')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Pool map with a method of a class and a list

2013-08-07 Thread Luca Cerone
Hi Joshua thanks!

 I think you might not understand what Chris said.
 Currently this does *not* work with Python 2.7 as you suggested it would.
  op = map(A.fun,l)

Yeah actually that wouldn't work even in Python 3, since value attribute used 
by fun has not been set.
It was my mistake in the example, but it is not the source of the problem..

 This, however, does:
  op = map(A(3).fun,l)
 
  op
 
 [1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683]
 
 

This works fine (and I knew that).. but is not what I want...

You are using the map() function that comes with Python. I want
to use the map() method of the Pool class (available in the multiprocessing 
module).

And there are differences between map() and Pool.map() apparently, so that if 
something works fine with map() it may not work with Pool.map() (as in my case).

To correct my example:

from multiprocessing import Pool

class A(object):
def __init__(self,x):
self.value = x
def fun(self,x):
return self.value**x

l = range(100)
p = Pool(4)
op = p.map(A(3).fun, l)

doesn't work neither in Python 2.7, nor 3.2 (by the way I can't use Python 3 
for my application).

 You will find that 
 http://stackoverflow.com/questions/1816958/cant-pickle-type-instancemethod- 
  when-using-pythons-multiprocessing-pool-ma 
 explains the problem in more detail than I understand. I suggest 
 reading it and relaying further questions back to us. Or use Python 3 

:) Thanks, but of course I googled and found this link before posting. I don't 
understand much of the details as well, that's why I posted here.

Anyway, thanks for the attempt :)

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


Re: beginner question (True False help)

2013-08-07 Thread Joshua Landau
On 7 August 2013 09:17,  eschneide...@comcast.net wrote:
 I'm trying to create an option for the program to repeat if the user types 
 'y' or 'yes', using true and false values, or otherwise end the program. If 
 anyone could explain to me how to get this code working, I'd appreciate it.

Always tell people what in particular you don't understand (*ducks*)
because it wasn't obvious what part of the problem you were unable to
fulfil.

 letters='abcdefghijklmn'
 batman=True
 def thingy():
 print('type letter from a to n')
 typedletter=input()
 if typedletter in letters:
 print('yes')
 else:
 print('no')
 def repeat():
 print('go again?')
 goagain=input()
 if goagain in ('y', 'yes'):
 print('ok')
 else:
 print('goodbye')
 batman=False

This doesn't do what you want it to.

x = old thing

def change_x():
x = new thing

change_x()

print(x) # Not changed!

The solution is to put global x at the start of the function.

 while batman==True:
 thingy()
 repeat()
 print('this is the end')


Note that this isn't actually a good way to do it. Imagine you had
several hundred function -- would you really want to have an
equivalent number of names floating around that you have to look
after?

The solution is to make the functions simply return values:

x = old thing

def return_thing():
x = new thing
return new thing # You can do this in one line

x = return_thing() # Get the value from the function and change x with it


Does this make sense?
-- 
http://mail.python.org/mailman/listinfo/python-list


Resuming the HTTP Download of a File and HTTP compression

2013-08-07 Thread iMath
the file I want to download is 100 bytes uncompressed, the downloads was 
interrupted when 5000 bytes compressed data was transmitted .If I want to 
Resuming the HTTP Download ,I wonder what value of the HTTP Range header should 
be ,“bytes=5000-“ or “bytes= os.path.getsize(downloadedPart)-”? why ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Resuming the HTTP Download of a File and HTTP compression

2013-08-07 Thread iMath
the file I want to download is 100 bytes uncompressed, the downloads was 
interrupted when 5000 bytes compressed data was transmitted .If I want to 
Resuming the HTTP Download ,I wonder what value of the HTTP Range header should 
be ,“bytes=5000-“ or “bytes= os.path.getsize(downloadedPart)-”? why ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reg secure python environment with web terminal emulator

2013-08-07 Thread Lakshmipathi.G
Hi -

Thanks for the response. Yes, we used OS features to
restrict the system user accounts.

We don't allow gcc - this helped us to avoid  kernel exploits via C code like :
https://www.centos.org/modules/newbb/viewtopic.php?viewmode=flattopic_id=42827forum=59
https://bugzilla.redhat.com/show_bug.cgi?id=962792

We are concerned whether user may try C exploits via Python code and break the
system. What's the minimal python set-up you would suggest? I'm
thinking something like:

1- Uninstall python-devel packages
2- Remove easy_install or pip (any such install utilities)
3- Keep only very basic modules under /usr/lib/python/site-packages
and delete the others.

Thanks.


-- 

Cheers,
Lakshmipathi.G
FOSS Programmer.
www.giis.co.in






On Wed, Aug 7, 2013 at 11:35 AM, dieter die...@handshake.de wrote:
 Lakshmipathi.G lakshmipath...@gmail.com writes:

 We have a server running a web-based terminal emulator (based on shellinabox
 for screen-casting  check www.webminal.org) that allows users to learn
 simple bash commands. This Linux environment secured by things like quota,
 selinux,ulimit  etc

 Now some users are requesting python access. How to ensure python is executed
 in a restricted environment. I came across
 http://docs.python.org/2/library/restricted.html
 but it seems like disabled in 2.3. Any thoughts on how we can safely
 provide python access
 to users.

 When you are satisfied with the protection you have achieved
 for bash commands, those same protection might be sufficient
 for Python as well. I assume that you used operating system
 facilities to restrict what the (system) user can do on the
 operating system level: the same restriction would apply to the
 (same) user executing Python code.

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


Re: Bug? ( () == [] ) != ( ().__eq__([]) )

2013-08-07 Thread Shiyao Ma
Sorry. I don't quite get it. As you said, it first tries,
leftOperand.__eq__(rightOperand) then if it returns NotImplemented, it goes
to invoke rightOperand.__eq__(leftOperand). But for any reason, [] == ()
returns false, why?


On Mon, Aug 5, 2013 at 7:06 AM, Chris Angelico ros...@gmail.com wrote:

 On Sun, Aug 4, 2013 at 11:35 PM, Markus Rother pyt...@markusrother.de
 wrote:
  Hello,
 
  The following behaviour seen in 3.2 seems very strange to me:
 
  As expected:
  () == []
  False
 
  However:
  ().__eq__([])
  NotImplemented
  [].__eq__(())
  NotImplemented

 You don't normally want to be calling dunder methods directly. The
 reasoning behind this behaviour goes back to a few things, including a
 way to handle 1 == Foo() where Foo is a custom type that implements
 __eq__; obviously the integer 1 won't know whether it's equal to a Foo
 instance or not, so it has to defer to the second operand to get a
 result. This deferral is done by returning NotImplemented, which is an
 object, and so is true by default. I don't see any particular reason
 for it to be false, as you shouldn't normally be using it; it's more
 like a null state, it means I don't know if we're equal or not. If
 neither side knows whether they're equal, then they're presumed to be
 unequal, but you can't determine that from a single call to __eq__.

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




-- 
http://about.me/introom
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bug? ( () == [] ) != ( ().__eq__([]) )

2013-08-07 Thread Chris Angelico
On Wed, Aug 7, 2013 at 10:24 AM, Shiyao Ma i...@introo.me wrote:
 Sorry. I don't quite get it. As you said, it first tries,
 leftOperand.__eq__(rightOperand) then if it returns NotImplemented, it goes
 to invoke rightOperand.__eq__(leftOperand). But for any reason, [] == ()
 returns false, why?

If neither of them has implemented a check, then it's assumed they're
not equal. It wouldn't be helpful for the == operator to return
anything other than True or False (except maybe NaN), so it returns
False.

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


Re: Using Pool map with a method of a class and a list

2013-08-07 Thread Joshua Landau
On 7 August 2013 09:33, Luca Cerone luca.cer...@gmail.com wrote:
 To correct my example:

 from multiprocessing import Pool

 class A(object):
 def __init__(self,x):
 self.value = x
 def fun(self,x):
 return self.value**x

 l = range(100)
 p = Pool(4)
 op = p.map(A(3).fun, l)

 doesn't work neither in Python 2.7, nor 3.2 (by the way I can't use Python 3 
 for my application).

Are you using Windows? Over here on 3.3 on Linux it does. Not on 2.7 though.

 You will find that
 http://stackoverflow.com/questions/1816958/cant-pickle-type-instancemethod- 
  when-using-pythons-multiprocessing-pool-ma
 explains the problem in more detail than I understand. I suggest
 reading it and relaying further questions back to us. Or use Python 3

 :) Thanks, but of course I googled and found this link before posting. I 
 don't understand much of the details as well, that's why I posted here.

 Anyway, thanks for the attempt :)

Reading there, the simplest method seems to be, in effect:

from multiprocessing import Pool
from functools import partial

class A(object):
def __init__(self,x):
self.value = x
def fun(self,x):
return self.value**x

def _getattr_proxy_partialable(instance, name, arg):
return getattr(instance, name)(arg)

def getattr_proxy(instance, name):

A version of getattr that returns a proxy function that can
be pickled. Only function calls will work on the proxy.

return partial(_getattr_proxy_partialable, instance, name)

l = range(100)
p = Pool(4)
op = p.map(getattr_proxy(A(3), fun), l)
print(op)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Pool map with a method of a class and a list

2013-08-07 Thread Luca Cerone
  doesn't work neither in Python 2.7, nor 3.2 (by the way I can't use Python 
  3 for my application).
  
 Are you using Windows? Over here on 3.3 on Linux it does. Not on 2.7 though.

No I am using Ubuntu (12.04, 64 bit).. maybe things changed from 3.2 to 3.3?
 
 from multiprocessing import Pool
 
 from functools import partial
 
 
 
 class A(object):
 
 def __init__(self,x):
 
 self.value = x
 
 def fun(self,x):
 
 return self.value**x
 
 
 
 def _getattr_proxy_partialable(instance, name, arg):
 
 return getattr(instance, name)(arg)
 
 
 
 def getattr_proxy(instance, name):
 
 
 
 A version of getattr that returns a proxy function that can
 
 be pickled. Only function calls will work on the proxy.
 
 
 
 return partial(_getattr_proxy_partialable, instance, name)
 
 
 
 l = range(100)
 
 p = Pool(4)
 
 op = p.map(getattr_proxy(A(3), fun), l)
 
 print(op)

I can't try it now, I'll let you know later if it works!
(Though just by reading I can't really understand what the code does).

Thanks for the help,
Luca
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lxml tostring quoting too much

2013-08-07 Thread andrea crotti
2013/8/6 Chris Down ch...@chrisdown.name:
 On 2013-08-06 18:38, andrea crotti wrote:
 I would really like to do the following:

 from lxml import etree as ET
 from lxml.builder import E

 url = http://something?x=10y=20;
 l = E.link(url)
 ET.tostring(l) - linkhttp://something?x=10y=20/link

 However the lxml tostring always quotes the , I can't find a way to
 tell it to avoid quoting it.

 You're probably aware, but without the escaping, it is no longer well formed
 XML. Why do you want to do that? Is there a larger underlying problem that
 should be solved instead?

 Either way, you can use unescape from the xml.sax.saxutils module[0].

 Chris

 0: http://docs.python.org/2/library/xml.sax.utils.html

Yes I know it's not correct, I thought that I still had to send that
anyway but luckily the problem was somewhere else, so encoding was
actually necessary and I don't need to do something strange..
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can someone suggest better resources for learning sqlite3? I wanted to use the Python library but I don't know sql.

2013-08-07 Thread Skip Montanaro
 Can someone suggest me better resources for learning sql/sqlite3?

The concepts behind the Structured Query Language haven't changed much
since Edgar Codd first developed them in the 1970s.  (He received the
Turing Award in 1981 for this work.)

Building and querying databases is very easy to do very badly,
especially if you are new to its concepts.  This is a case where a
textbook might be helpful. Since these ideas have been around for a
long while, there are plenty of good books on the subject. I have one
on my desk at work whose name I can't remember off the top of my head.
 I still refer to it from time-to-time.  If you'd like a reference,
let me know and I'll check on it at work.

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


Re: Using Pool map with a method of a class and a list

2013-08-07 Thread Joshua Landau
On 7 August 2013 11:10, Luca Cerone luca.cer...@gmail.com wrote:
 I can't try it now, I'll let you know later if it works!
 (Though just by reading I can't really understand what the code does).

Well,

 from multiprocessing import Pool
 from functools import partial

 class A(object):
 def __init__(self,x):
 self.value = x
 def fun(self,x):
 return self.value**x

This is all the same, as with

 l = range(100)
 p = Pool(4)

You then wanted to do:

 op = p.map(A(3).fun, l)

but bound methods can't be pickled, it seems.

However, A(3) *can* be pickled. So what we want is a function:

def proxy(arg):
A(3).fun(arg)

so we can write:

 op = p.map(proxy, l)

To generalise you might be tempted to write:

def generic_proxy(instance, name):
def proxy(arg):
# Equiv. of instance.name(arg)
getattr(instance, name)(arg)

but the inner function won't work as functions-in-functions can't be
pickled either.

So we use:

 def _getattr_proxy_partialable(instance, name, arg):
 return getattr(instance, name)(arg)

Which takes all instance, name and arg. Of course we only want our
function to take arg, so we partial it:

 def getattr_proxy(instance, name):
 
 A version of getattr that returns a proxy function that can
 be pickled. Only function calls will work on the proxy.
 
 return partial(_getattr_proxy_partialable, instance, name)

partial objects are picklable, btw.

 op = p.map(getattr_proxy(A(3), fun), l)
 print(op)

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


Re: Enum vs OrderedEnum

2013-08-07 Thread Neil Cerutti
On 2013-08-07, Ian Kelly ian.g.ke...@gmail.com wrote:
 On Tue, Aug 6, 2013 at 7:55 PM, Ben Finney ben+pyt...@benfinney.id.au wrote:
 Ian Kelly ian.g.ke...@gmail.com writes:
 Terrain that is ?radiated? would be terrain that has some kind of spokes
 spreading out from its centre. I think you mean ?irradiated?.

 Hope the game goes well :-)

 It's actually a reimplementation of a game from 1993, so I'm
 somewhat stuck with the terminology.

I haven't played MOO1 for at least a month. :)

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


Mock pathc question

2013-08-07 Thread balderman
Hi
I would like to mock patch the attribute 'calc' in the 'Client' class (See code 
below).
I have 2 unit tests:
1) test1 -  that patch an existing instance of 'Client' - it works fine.
1) test2 -  that tries to patch the 'Client' class. My expectation is that 
after the patching, every instance of 'Client' will be created with 
'MockClient'. However this is not the case..

Can you please advice?

Thanks 

Avishay


code below:
-

import mock
import sys
import unittest

SEVEN = 7

class Calc:
def __init__(self):
print self.__class__
def add(self,a,b):
return a + b

class MockCalc:
def __init__(self):
print self.__class__
def add(self,a,b):
return SEVEN

class Client:
def __init__(self):
self.calc = Calc()
def add(self,a,b):
return self.calc.add(a,b)

class TestIt(unittest.TestCase):
def setUp(self):
pass

def test2(self):
'''Mocking the Calc and replace it with MockCalc.'''  
print  \ntest2 
my_mock = mock.patch('mock_play.Calc',create=True, new=MockCalc)
my_mock.start()
# client should be created with 'MockCalc'
client = Client()
# result should be 7
print str(client.add(1,34))
my_mock.stop()
# result should be 35 again
print str(client.add(1,34))

def test1(self):
'''Mocking the client instance.'''  
print  test1 
client = Client()
my_mock = mock.patch.object(client, 'calc', new_callable=MockCalc)
# result should be 35
print str(client.add(1,34))
# now i want to switch to the MockCalc
my_mock.start()
# result should be 7
print str(client.add(1,34))
my_mock.stop()
# result should be 35 again
print str(client.add(1,34))

if __name__ == __main__:
unittest.main()


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


Re: Using Pool map with a method of a class and a list

2013-08-07 Thread Peter Otten
Joshua Landau wrote:

 On 7 August 2013 11:10, Luca Cerone luca.cer...@gmail.com wrote:
 I can't try it now, I'll let you know later if it works!
 (Though just by reading I can't really understand what the code does).
 
 Well,
 
 from multiprocessing import Pool
 from functools import partial

 class A(object):
 def __init__(self,x):
 self.value = x
 def fun(self,x):
 return self.value**x
 
 This is all the same, as with
 
 l = range(100)
 p = Pool(4)
 
 You then wanted to do:
 
 op = p.map(A(3).fun, l)
 
 but bound methods can't be pickled, it seems.
 
 However, A(3) *can* be pickled. So what we want is a function:
 
 def proxy(arg):
 A(3).fun(arg)
 
 so we can write:
 
 op = p.map(proxy, l)
 
 To generalise you might be tempted to write:
 
 def generic_proxy(instance, name):
 def proxy(arg):
 # Equiv. of instance.name(arg)
 getattr(instance, name)(arg)
 
 but the inner function won't work as functions-in-functions can't be
 pickled either.
 
 So we use:
 
 def _getattr_proxy_partialable(instance, name, arg):
 return getattr(instance, name)(arg)
 
 Which takes all instance, name and arg. Of course we only want our
 function to take arg, so we partial it:
 
 def getattr_proxy(instance, name):
 
 A version of getattr that returns a proxy function that can
 be pickled. Only function calls will work on the proxy.
 
 return partial(_getattr_proxy_partialable, instance, name)
 
 partial objects are picklable, btw.
 
 op = p.map(getattr_proxy(A(3), fun), l)
 print(op)
 
 :)


There is also the copy_reg module. Adapting

http://mail.python.org/pipermail/python-list/2008-July/469164.html

you get:

import copy_reg
import multiprocessing
import new

def make_instancemethod(inst, methodname):
return getattr(inst, methodname)

def pickle_instancemethod(method):
return make_instancemethod, (method.im_self, method.im_func.__name__)

copy_reg.pickle(
new.instancemethod, pickle_instancemethod, make_instancemethod)

class A(object):
   def __init__(self, a):
   self.a = a
   def fun(self, b):
   return self.a**b

if __name__ == __main__:
items = range(10)
pool = multiprocessing.Pool(4)
print pool.map(A(3).fun, items)


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


Re: Using Pool map with a method of a class and a list

2013-08-07 Thread Joshua Landau
On 7 August 2013 15:46, Peter Otten __pete...@web.de wrote:
 import copy_reg
 import multiprocessing
 import new

new is deprecated from 2.6+; use types.MethodType instead of
new.instancemethod.

 def make_instancemethod(inst, methodname):
 return getattr(inst, methodname)

This is just getattr -- you can replace the two uses of
make_instancemethod with getattr and delete this ;).

 def pickle_instancemethod(method):
 return make_instancemethod, (method.im_self, method.im_func.__name__)

 copy_reg.pickle(
 new.instancemethod, pickle_instancemethod, make_instancemethod)

 class A(object):
def __init__(self, a):
self.a = a
def fun(self, b):
return self.a**b

 if __name__ == __main__:
 items = range(10)
 pool = multiprocessing.Pool(4)
 print pool.map(A(3).fun, items)

Well that was easy. The Stackoverflow link made that look *hard*. -1
to my hack, +1 to this.

You can do this in one statement:

copy_reg.pickle(
types.MethodType,
lambda method: (getattr, (method.im_self, method.im_func.__name__)),
getattr
)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Pool map with a method of a class and a list

2013-08-07 Thread Peter Otten
Joshua Landau wrote:

 On 7 August 2013 15:46, Peter Otten __pete...@web.de wrote:

 def make_instancemethod(inst, methodname):
 return getattr(inst, methodname)
 
 This is just getattr -- you can replace the two uses of
 make_instancemethod with getattr and delete this ;).

D'oh ;)

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


Re: HTTP post with urllib2

2013-08-07 Thread cerr
On Tuesday, August 6, 2013 5:14:48 PM UTC-7, MRAB wrote:
 On 06/08/2013 23:52, cerr wrote:
 
  Hi,
 
 
 
  Why does this code:
 
 
 
  #!/usr/bin/python
 
 
 
 
 
  import urllib2
 
  from binascii import hexlify, unhexlify
 
 
 
  host = localhost
 
  uri=/test.php
 
  data =\x48\x65\x6C\x6C\x6F\x57\x6F\x72\x6C\x64 #Hello World
 
  url=http://{0}{1}?f=test.format(host, uri)
 
  req = urllib2.Request(url, data,{'Content-Type': 
  'application/octet-stream'})
 
  req.get_method = lambda: 'PUT'
 
  response = urllib2.urlopen(req, 120)
 
  retval = response.read()
 
  print RETVAL +retval
 
 
 
 
 
 
 
  return me this:
 
 
 
  ./post.py
 
  Traceback (most recent call last):
 
 File ./post.py, line 13, in module
 
   response = urllib2.urlopen(req, 120)
 
 File /usr/lib/python2.7/urllib2.py, line 126, in urlopen
 
   return _opener.open(url, data, timeout)
 
 File /usr/lib/python2.7/urllib2.py, line 398, in open
 
   req = meth(req)
 
 File /usr/lib/python2.7/urllib2.py, line 1116, in do_request_
 
   'Content-length', '%d' % len(data))
 
 
 
 
 
  I don't get it, what's going on here?
 
 
 
 The docs say urllib2.urlopen(url[, data][, timeout]).
 
 
 
 You're calling it as urllib2.urlopen(req, 120).
 
 
 
 In other words, 'url' is req and 'data' is 120.
 
 
 
 It should be urllib2.urlopen(req, None, 120).

Yes, great! That did it! :)
Coming into the office in the morning, sitting down, changing this and get it 
working! Good way to start my day! :)

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


Re: make elements of a list twice or more.

2013-08-07 Thread liuerfire Wang
I got it!
It can do like [i for i in x for y in range(2)]


On Wed, Aug 7, 2013 at 4:50 PM, liuerfire Wang liuerf...@gmail.com wrote:

 Sorry for the title which didn't make clear.

 Here is a list x = [b, a, c] (a, b, c are elements of x. Each of them are
 different type).  Now I wanna generate a new list as [b, b, a, a, c, c].

 I know we can do like that:

 tmp = []
 for i in x:
 tmp.append(i)
 tmp.append(i)

 However, I wander is there a more beautiful way to do it, like [i for i in
 x]?

 Thanks.


 --
 Best regards.
 /**
 google+: +liuerfire http://gplus.to/onepiece twitter: 
 @liuerfirehttps://twitter.com/#!/liuerfire
 蛋疼不蛋疼的都可以试着点一下~^_^~ http://db.tt/YGEdRM0
 ***/




-- 
Best regards.
/**
google+: +liuerfire http://gplus.to/onepiece twitter:
@liuerfirehttps://twitter.com/#!/liuerfire
蛋疼不蛋疼的都可以试着点一下~^_^~ http://db.tt/YGEdRM0
***/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating a running tally/ definitely new to this

2013-08-07 Thread Joshua Landau
On 6 August 2013 16:24,  gratedme...@gmail.com wrote:
 On Monday, August 5, 2013 10:15:30 PM UTC-4, Dave Angel wrote:
 gratedme...@gmail.com wrote:

  I currently working on a game, where I need to maintain a running tally of 
  money, as the player makes purchases as they navigate thru game.   I not 
  exactly sure how to do this in python.  I know it is a fairly basic step, 
  nonetheless.  Any assistance would be greatly appreciated.

 (just to save you the pain later:

http://wiki.python.org/moin/GoogleGroupsPython

 )

Look! A link! Read it!


 This a project I am am working on.  I am using Learn Python the Hard Way. 
 To best explain. I'm working on a game with a similar format to John Dell's 
 Dopewars, but on Python. SO I've created the several destinations to travel, 
 but now maintaining the running tally (money) has been my issue. I'm going 
 to take your advice and play with code you posted.  Please contact me with 
 any more suggestions.

You're doing something wrong. No-one on this list knows what it is.
Hence no-one can help you until you give us some way of finding out.
-- 
http://mail.python.org/mailman/listinfo/python-list


make elements of a list twice or more.

2013-08-07 Thread liuerfire Wang
Sorry for the title which didn't make clear.

Here is a list x = [b, a, c] (a, b, c are elements of x. Each of them are
different type).  Now I wanna generate a new list as [b, b, a, a, c, c].

I know we can do like that:

tmp = []
for i in x:
tmp.append(i)
tmp.append(i)

However, I wander is there a more beautiful way to do it, like [i for i in
x]?

Thanks.


-- 
Best regards.
/**
google+: +liuerfire http://gplus.to/onepiece twitter:
@liuerfirehttps://twitter.com/#!/liuerfire
蛋疼不蛋疼的都可以试着点一下~^_^~ http://db.tt/YGEdRM0
***/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: make elements of a list twice or more.

2013-08-07 Thread Peter Otten
liuerfire Wang wrote:

 Here is a list x = [b, a, c] (a, b, c are elements of x. Each of them are
 different type).  Now I wanna generate a new list as [b, b, a, a, c, c].
 
 I know we can do like that:
 
 tmp = []
 for i in x:
 tmp.append(i)
 tmp.append(i)
 
 However, I wander is there a more beautiful way to do it, like [i for i in
 x]?

Using itertools:

 items
[b, a, c]
 from itertools import chain, tee, repeat

 list(chain.from_iterable(zip(*tee(items
[b, b, a, a, c, c]

Also using itertools:

 list(chain.from_iterable(repeat(item, 2) for item in items))
[b, b, a, a, c, c]

For lists only, should be fast:

 result = 2*len(items)*[None]
 result[::2] = result[1::2] = items
 result
[b, b, a, a, c, c]

But I would call none of these beautiful...

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


Re: Can someone suggest better resources for learning sqlite3? I wanted to use the Python library but I don't know sql.

2013-08-07 Thread Skip Montanaro
 I have one on my desk at work whose name I can't remember off the
 top of my head.  I still refer to it from time-to-time.  If you'd
 like a reference, let me know and I'll check on it at work.

While I think of it:

The Practical SQL Handbook; Using Structured Query Language, by
Bowman, Emerson, and Darnovsky

Mine's the third edition.  It even has a Sybase SQL Runtime CDROM
inside the back cover.  How quaint. :-)  There is a fourth edition
available.

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


Re: Bug? ( () == [] ) != ( ().__eq__([]) )

2013-08-07 Thread Ethan Furman

On 08/07/2013 02:24 AM, Shiyao Ma wrote:


Sorry. I don't quite get it. As you said, it first tries, 
leftOperand.__eq__(rightOperand) then if it returns
NotImplemented, it goes to invoke rightOperand.__eq__(leftOperand). But for any 
reason, [] == () returns false, why?


A list that is empty is not equal to a tuple that is empty, much like a car that is empty is not equal to a boat that is 
empty.


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


Re: Mock pathc question

2013-08-07 Thread Jean-Michel Pichavant
- Mail original -
 Hi
 I would like to mock patch the attribute 'calc' in the 'Client' class
 (See code below).
 I have 2 unit tests:
 1) test1 -  that patch an existing instance of 'Client' - it works
 fine.
 1) test2 -  that tries to patch the 'Client' class. My expectation is
 that after the patching, every instance of 'Client' will be created
 with 'MockClient'. However this is not the case..
 
 Can you please advice?
 
 Thanks
 
 Avishay


One way to do this is to decorate the test2 method, 
http://www.voidspace.org.uk/python/mock/patch.html.
This way you get rid of all the start/stop boiler-plate, the scope of your 
patch is the scope of the method.

*code not tested*

class TestIt(unittest.TestCase):
def setUp(self):
pass

@mock.patch(Calc, MockCalc)
def test2(self):
client = Client()
# result should be 7
print str(client.add(1,34))

def test3(self):
client = Client()
# result should be 35 again
print str(client.add(1,34)) 

By the way, what is 'mock_play' in your original post, could be the reason why 
you things did go wrong.

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: new to While statements

2013-08-07 Thread Vito De Tullio
Dan Sommers wrote:

 while asking for reponse:
 
 while adventuring:
 
 that's a funny way to say `while True:`...
 
 Funny, perhaps, the first time you see it, but way more informative than
 the other way to the next one who comes along and reads it.

While I understand that it's syntactically and semantically correct, my nose 
still doesn't like it.

It's not that's just not common... I just think it's a mishmash... it's not 
a rule thing, more a feeling wrong one.

Maybe it's better if I try to explain in another way...

My first instinct about it it's to think why the author choose this way to 
talk to the reader.

This message it's clearly meant to be read by another programmer, not by the 
user. But in my mind it should be a comment. In my mind code explain what 
is happening, comment say why. (Sometimes talking to my colleagues we say 
comment is a way to ask to forgiveness for a particular obscure code, as in 
sorry, I needed to flip-this and flop-that for such-and-such reason) 

Following this reasoning, I will found more readable something like

# asking for response
while True:
...

that

while 'asking for response':
...

because in the latter case the why and the how are mixed. It's like 
you're talking with the interpreter, but the message is for the programmer..

I hope I explained myself...




-- 
By ZeD

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


Is a Metaclass the appropriate way to solve this problem?

2013-08-07 Thread Matthew Lefavor
All:

Like most people, I find the whole metaclass topic pretty obscure, and I
have avoided trying to use one for a while. I am also aware of Tim Peter's
famous advice that if you have to ask whether you need a metaclass, then
you almost certainly don't. But in this case I know I am solving a problem
similar to problems that other high-profile Python modules (e.g., Django's
Model class) have solved using metaclasses.

My company is using a database whose interface is defined in a series of
JSON objects. I am writing some interface code to the database and I am
trying to hide the detail of the JSON implementation from the user.

I want the user to be able to define a class representing a database entry
for any arbitrary table, whose attributes represent database entities, like
fields or tags, with syntax something like the following:

class DataSet:
data_set_id = DatabaseKeyField(int)
description = DatabaseField(str)
creation_date = DatabaseField(datetime.date)
creation_timestamp = DatabaseField(datetime.datetime)

def __init__(self, ds_id, description, timestamp):
self.data_set_id = ds_id
self.description = description
self.creation_timestamp = timestamp
self.creation_date = timestamp.date

I know that to create the DatabaseField objects I should be using a
descriptor. But I also want the DataSet to automatically gain methods that
will convert it into the expected JSON syntax (e.g., a __specifier__ method
that will create a JSON object with only key fields, and an __object__
method that will create a JSON object with all the fields and other bells
and whistles.)

My ultimate question then: How do I go about adding those kinds of methods
(which iterate through all the attributes which are Database* descriptors)?
I know that I could eschew metaclasses altogether and use a common
super-class, but this doesn't feel like the right situation for inheritance
to me. Is a metaclass going to be the cleanest and easiest-to-understand
way to solve this problem?

Thank you, all!

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


Re: [tkinter] trouble running imported modules in main program

2013-08-07 Thread Cousin Stanley
Terry Reedy wrote:

 Code comments : 
 
   double and triple spacing code 
   make it painful to read, 

Not for everyone  :-)

I prefer  mostly  double-spaced code
in any language  
 
   especially in a 10 line box.

Agree, but the 10 line box 
would not be used for routine
code editing and viewing 
and could be made larger 
to accomodate viewing code
posted online 


-- 
Stanley C. Kitching
Human Being
Phoenix, Arizona

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


Re: Is a Metaclass the appropriate way to solve this problem?

2013-08-07 Thread Ian Kelly
On Wed, Aug 7, 2013 at 1:38 PM, Matthew Lefavor mclefa...@gmail.com wrote:
 I know that to create the DatabaseField objects I should be using a
 descriptor. But I also want the DataSet to automatically gain methods that
 will convert it into the expected JSON syntax (e.g., a __specifier__ method
 that will create a JSON object with only key fields, and an __object__
 method that will create a JSON object with all the fields and other bells
 and whistles.)

 My ultimate question then: How do I go about adding those kinds of methods
 (which iterate through all the attributes which are Database* descriptors)?
 I know that I could eschew metaclasses altogether and use a common
 super-class, but this doesn't feel like the right situation for inheritance
 to me. Is a metaclass going to be the cleanest and easiest-to-understand way
 to solve this problem?

You could use a class decorator.  It's not as flexible as a metaclass,
but from the sound of it you don't need that kind of flexibility; you
just need to modify the class a bit after it's already been created.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: beginner question (True False help)

2013-08-07 Thread eschneider92
What I wanted to happen is when the user typed something other than 'y' or 
'yes' after being asked 'go again?', the batman==False line would cause the 
program to stop asking anything and say 'this is the end'. Instead, what is 
happening is that the program just keeps going. I figured that after defining 
the function (thingy(), repeat()), that the while statement would repeat until 
the 'go again' user input was something other than 'y' or 'yes', and the 
batman==False part of the repeat() function would cause the 'while 
batman==True' part to become False and end. You probably answered my question 
and I'm too dumb to see it, but that's a slight elaboration on my problem.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Pool map with a method of a class and a list

2013-08-07 Thread Luca Cerone
Thanks for the post.
I actually don't know exactly what can and can't be pickles..
not what partialing a function means..
Maybe can you link me to some resources?
 
I still can't understand all the details in your code :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: make elements of a list twice or more.

2013-08-07 Thread Joshua Landau
On 7 August 2013 17:59, Peter Otten __pete...@web.de wrote:
 liuerfire Wang wrote:

 Here is a list x = [b, a, c] (a, b, c are elements of x. Each of them are
 different type).  Now I wanna generate a new list as [b, b, a, a, c, c].

 I know we can do like that:

 tmp = []
 for i in x:
 tmp.append(i)
 tmp.append(i)

 However, I wander is there a more beautiful way to do it, like [i for i in
 x]?

 Using itertools:

 items
 [b, a, c]
 from itertools import chain, tee, repeat

 list(chain.from_iterable(zip(*tee(items
 [b, b, a, a, c, c]

 Also using itertools:

 list(chain.from_iterable(repeat(item, 2) for item in items))
 [b, b, a, a, c, c]

list(chain.from_iterable([item, item] for item in items))
?


I'm actually posting to point out
http://www.python.org/dev/peps/pep-0448/ would let you write:

[*(item, item) for item in items]

which I think is totz rad and beats out OP's

[item for item in items for _ in range(2)]

in readability, succinctness and obviousness.


PS: For jokes, you can also painfully do:

list((yield item) or item for item in items)


 For lists only, should be fast:

 result = 2*len(items)*[None]
 result[::2] = result[1::2] = items
 result
 [b, b, a, a, c, c]

 But I would call none of these beautiful...

Au contraire, that is marvelous (I'd still avoid it, though).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Pool map with a method of a class and a list

2013-08-07 Thread Joshua Landau
On 7 August 2013 23:26, Luca Cerone luca.cer...@gmail.com wrote:
 Thanks for the post.
 I actually don't know exactly what can and can't be pickles..

I just try it and see what works ;).

The general idea is that if it is module-level it can be pickled and
if it is defined inside of something else it cannot. It depends
though.

 not what partialing a function means..

partial takes a function and returns it with arguments filled in:

from functools import partial

def add(a, b):
return a + b

add5 = partial(add, 5)

print(add5(10)) # Returns 15 == 5 + 10

 Maybe can you link me to some resources?

http://docs.python.org/2/library/functools.html#functools.partial


 I still can't understand all the details in your code :)

Never mind that, though, as Peter Otten's code (with my very minor
suggested modifications) if by far the cleanest method of the two and
is arguably more correct too.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Pool map with a method of a class and a list

2013-08-07 Thread Luca Cerone
Thanks for the help Peter!

 
 
 
  def make_instancemethod(inst, methodname):
 
  return getattr(inst, methodname)
 
  
 
  This is just getattr -- you can replace the two uses of
 
  make_instancemethod with getattr and delete this ;).
 
 
 
 D'oh ;)

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


Re: beginner question (True False help)

2013-08-07 Thread Dave Angel
eschneide...@comcast.net wrote:

 What I wanted to happen is when the user typed something other than 'y' or 
 'yes' after being asked 'go again?', the batman==False line would cause the 
 program to stop asking anything and say 'this is the end'. Instead, what is 
 happening is that the program just keeps going. I figured that after defining 
 the function (thingy(), repeat()), that the while statement would repeat 
 until the 'go again' user input was something other than 'y' or 'yes', and 
 the batman==False part of the repeat() function would cause the 'while 
 batman==True' part to become False and end. You probably answered my question 
 and I'm too dumb to see it, but that's a slight elaboration on my problem.

When you assign a variable inside a function, it has no effect on a
global variable with similar name.  In order to make it change the
global, you'd have needed the global declaration.

Try this:

var = 42
def  myfunc():
 var = 90


print before:, var
myfunc()
print after:, var

Now, change the function, by adding a declaration:

def myfunc():
global var
var = 90

and the result will change.


-- 
DaveA


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


Re: new to While statements

2013-08-07 Thread Dave Angel
Vito De Tullio wrote:

 Dan Sommers wrote:

 while asking for reponse:
 
 while adventuring:
 
 that's a funny way to say `while True:`...
 
 Funny, perhaps, the first time you see it, but way more informative than
 the other way to the next one who comes along and reads it.

 While I understand that it's syntactically and semantically correct, my nose 
 still doesn't like it.


Neither does mine.  There's no need for a trick here.   while True
reads better, and a comment (on the same line, preferably) can explain
what the loop is for.


-- 
DaveA

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


Re: make elements of a list twice or more.

2013-08-07 Thread liuerfire Wang
On Thu, Aug 8, 2013 at 6:18 AM, Joshua Landau jos...@landau.ws wrote:


 I'm actually posting to point out
 http://www.python.org/dev/peps/pep-0448/ would let you write:

 [*(item, item) for item in items]


It seems like that it can be only used in python 3.4? I just use python 2.7
because of work needs.



  For lists only, should be fast:
 
  result = 2*len(items)*[None]
  result[::2] = result[1::2] = items
  result
  [b, b, a, a, c, c]


Yeah, this is amazing and very fast.

I just make a test:

import timeit

from itertools import chain, tee, repeat

x = [1, 2, 3, 4, 5, 6, 7, 8]

def test1():
[i for i in x for y in range(2)]

def test2():

tmp = []
for i in x:
tmp.append(i)
tmp.append(i)

def test3():
list(chain.from_iterable(zip(*tee(x

def test4():
result = 2 * len(x) * [None]
result[::2] = result[1::2] = x

if __name__ == '__main__':
t1 = timeit.Timer(test1(), from __main__ import test1)
t2 = timeit.Timer(test2(), from __main__ import test2)
t3 = timeit.Timer(test3(), from __main__ import test3)
t4 = timeit.Timer(test4(), from __main__ import test4)
print t1.timeit(100)
print t2.timeit(100)
print t3.timeit(100)
print t4.timeit(100)

And the result is:

4.56177520752
2.85114097595
7.61084198952
1.29519414902



On Thu, Aug 8, 2013 at 6:18 AM, Joshua Landau jos...@landau.ws wrote:

 On 7 August 2013 17:59, Peter Otten __pete...@web.de wrote:
  liuerfire Wang wrote:
 
  Here is a list x = [b, a, c] (a, b, c are elements of x. Each of them
 are
  different type).  Now I wanna generate a new list as [b, b, a, a, c, c].
 
  I know we can do like that:
 
  tmp = []
  for i in x:
  tmp.append(i)
  tmp.append(i)
 
  However, I wander is there a more beautiful way to do it, like [i for i
 in
  x]?
 
  Using itertools:
 
  items
  [b, a, c]
  from itertools import chain, tee, repeat
 
  list(chain.from_iterable(zip(*tee(items
  [b, b, a, a, c, c]
 
  Also using itertools:
 
  list(chain.from_iterable(repeat(item, 2) for item in items))
  [b, b, a, a, c, c]

 list(chain.from_iterable([item, item] for item in items))
 ?


 I'm actually posting to point out
 http://www.python.org/dev/peps/pep-0448/ would let you write:

 [*(item, item) for item in items]

 which I think is totz rad and beats out OP's

 [item for item in items for _ in range(2)]

 in readability, succinctness and obviousness.


 PS: For jokes, you can also painfully do:

 list((yield item) or item for item in items)


  For lists only, should be fast:
 
  result = 2*len(items)*[None]
  result[::2] = result[1::2] = items
  result
  [b, b, a, a, c, c]
 
  But I would call none of these beautiful...

 Au contraire, that is marvelous (I'd still avoid it, though).
 --
 http://mail.python.org/mailman/listinfo/python-list




-- 
Best regards.
/**
google+: +liuerfire http://gplus.to/onepiece twitter:
@liuerfirehttps://twitter.com/#!/liuerfire
蛋疼不蛋疼的都可以试着点一下~^_^~ http://db.tt/YGEdRM0
***/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: beginner question (True False help)

2013-08-07 Thread Larry Hudson

On 08/07/2013 01:17 AM, eschneide...@comcast.net wrote:

I'm trying to create an option for the program to repeat if the user types 'y' 
or 'yes', using true and false values, or otherwise end the program. If anyone 
could explain to me how to get this code working, I'd appreciate it.

letters='abcdefghijklmn'
batman=True
def thingy():
 print('type letter from a to n')
 typedletter=input()
 if typedletter in letters:
 print('yes')
 else:
 print('no')
def repeat():
 print('go again?')
 goagain=input()
 if goagain in ('y', 'yes'):
 print('ok')
 else:
 print('goodbye')
 batman=False
while batman==True:
 thingy()
 repeat()
 print('this is the end')

You've already received answers to this, primarily pointing out that batman needs to be declared 
as global in your repeat() function.  Global variables can be read from inside a function 
without declaring them as such, but if you need to change them, they MUST be declared as 
globals, otherwise it will merely create an independant local variable with the same name.


A second possibility is to do away with batman in the repeat() function, and instead return True 
in the 'yes' clause and False in the else clause.  Then in your while loop, change the repeat() 
line to:

batman = repeat()

A third version (which I would prefer) is to do away with batman altogether (maybe the Penguin 
got 'im??)   ;-)   Use the True/False version of repeat() and change the while loop to:


while True:
thingy()
if not repeat():
break

And finally unindent your final print() line.  The way you have it will print 'The end' every 
time in your loop.  Not what you want, I'm sure.


 -=- Larry -=-

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


Re: outputting time in microseconds or milliseconds

2013-08-07 Thread matt . doolittle33

 
 Taking a step back, you're probably better off using datetimes.  You'll 
 
 get all this conversion nonsense for free:
 
i did: 

   from time import strftime, time
   from datetime import datetime

   now = datetime.now()

   self.logfile.write('%s\t'%(strftime(%Y-%m-%d,)))
   self.logfile.write('%s\t'%(now.strftime(%H:%M:%S.%f,)))

this gives me:

2013-08-04  14:01:27.906126
2013-08-04  14:01:28.052273
2013-08-04  14:01:28.058967
2013-08-04  14:01:28.243959
2013-08-04  14:01:28.251107
2013-08-04  14:01:28.251268
2013-08-04  14:01:28.251373
2013-08-04  14:01:28.251475
2013-08-04  14:01:28.424568
2013-08-04  14:01:28.612548
2013-08-04  14:01:28.616569
2013-08-04  14:01:28.616727
2013-08-04  14:01:28.792487
2013-08-04  14:01:28.796226

thats what i need. Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: new to While statements

2013-08-07 Thread Larry Hudson

On 08/06/2013 08:38 PM, krismesenbr...@gmail.com wrote:

import random



def room ():

 hp = 10
 while hp != 0:

 random_Number = random.randint(1, 2)

 #asking if you want to roll/play
 des = input(Would you like to roll the die?)


snip

One very trivial comment...  Add one or two spaces to the end of your prompt string, (I like to 
use two).


No biggie, but it just looks nicer if the answer doesn't butt up directly against the end of the 
prompt.


 -=- Larry -=-

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


Re: Crawl Quora

2013-08-07 Thread David Hutto
Never tried this, but if it's not data you're after, but a search term type
of app, then ip address crawl, and if keyword/metadata, then crawl, and
parse, just as it seems you are doing, for keywords, and url's associated
with them, then eliminate url's without that specified keyword parameter
into your function.

Then, of course, just as stated above, some sites won't let you have access
in other ways, which you should be able to circumvent some way.



On Sat, Aug 3, 2013 at 5:09 PM, Dave Angel da...@davea.name wrote:

 Umesh Sharma wrote:

  Hello,
 
  I am writing a crawler in python, which crawl quora. I can't read the
 content of quora without login. But google/bing crawls quora. One thing i
 can do is use browser automation and login in my account and the go links
 by link and crawl content, but this method is slow. So can any one tell me
 how should i start in writing this crawler.
 
 
 I had never heard of quora.  And I had to hunt a bit to find a link to
 this website.  When you post a question here which refers to a
 non-Python site, you really should include a link to it.

 You start with reading the page:  http://www.quora.com/about/tos

 which you agreed to when you created your account with them.  At one
 place it seems pretty clear that unless you make specific arrangements
 with Quora, you're limited to using their API.

 I suspect that they bend over backwards to get Google and the other big
 names to index their stuff.  But that doesn't make it legal for you to
 do the same.

 In particular, the section labeled Rules makes constraints on
 automated crawling.  And so do other parts of the TOS.  Crawling is
 permissible, but not scraping.  What's that mean?  I dunno.  Perhaps
 scraping is what you're describing above as method is slow.

 I'm going to be looking to see what API's they offer, if any.  I'm
 creating an account now.

 --
 DaveA

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




-- 
Best Regards,
David Hutto
*CEO:* *http://www.hitwebdevelopment.com*
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [tkinter] trouble running imported modules in main program

2013-08-07 Thread snakeinmyboot
if __name__ == '__main__':
 root = tkinter.Tk()
 app = MainClass(root)  # 'MainClass' depends on the module.
 root.mainloop
 root.destroy 

for REAL you guys...wtf does this even mean lol. what is a boilerplate test 
code?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reg secure python environment with web terminal emulator

2013-08-07 Thread Lakshmipathi.G
 If you permit file I/O and anything that can spawn a process, it is
 possible to create a raw binary executable and trigger its execution.
 --

Yes,we permit file i/o with quota limits and spawning a process is
allowed upto a limit.
If I'm not wrong, we will be safe if user invokes  subprocess  or
os.system('sudo') calls
due to system constraints.

Could you please share more info about creating raw binary executable
and its potential
problem.

Thanks for your response.


-- 

Cheers,
Lakshmipathi.G
FOSS Programmer.
www.giis.co.in
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Mock pathc question

2013-08-07 Thread Avishay Balderman
Hi
1) I prefer to use start/stop and not the decorator .
2) mock_play is the name of the module where the code belongs

Thanks

Avishay

Sent from my iPhone

On 7 באוג 2013, at 21:01, Jean-Michel Pichavant jeanmic...@sequans.com
wrote:

- Mail original -

Hi

I would like to mock patch the attribute 'calc' in the 'Client' class

(See code below).

I have 2 unit tests:

1) test1 -  that patch an existing instance of 'Client' - it works

fine.

1) test2 -  that tries to patch the 'Client' class. My expectation is

that after the patching, every instance of 'Client' will be created

with 'MockClient'. However this is not the case..


Can you please advice?


Thanks


Avishay



One way to do this is to decorate the test2 method,
http://www.voidspace.org.uk/python/mock/patch.html.
This way you get rid of all the start/stop boiler-plate, the scope of your
patch is the scope of the method.

*code not tested*

class TestIt(unittest.TestCase):
   def setUp(self):
   pass

   @mock.patch(Calc, MockCalc)
   def test2(self):
   client = Client()
   # result should be 7
   print str(client.add(1,34))

   def test3(self):
   client = Client()
   # result should be 35 again
   print str(client.add(1,34))

By the way, what is 'mock_play' in your original post, could be the reason
why you things did go wrong.

JM


-- IMPORTANT NOTICE:

The contents of this email and any attachments are confidential and may
also be privileged. If you are not the intended recipient, please notify
the sender immediately and do not disclose the contents to any other
person, use it for any purpose, or store or copy the information in any
medium. Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [tkinter] trouble running imported modules in main program

2013-08-07 Thread David
On 8 August 2013 14:06, snakeinmyboot mikelha...@gmail.com wrote:

 for REAL you guys...wtf does this even mean lol. what is a boilerplate test 
 code?

Did you try at all to find the answer to this yourself?

I ask because it took me only a few seconds to go to wikipedia and
search for boilerplate find this for you:
http://en.wikipedia.org/wiki/Boilerplate_code

Tip: To successfully use forums like this one (where a lot of very
smart people read), the more care/effort/thought you demonstrate that
you tried to solve your issue before asking, the more likely you are
to receive a response.
-- 
http://mail.python.org/mailman/listinfo/python-list


[issue1596321] KeyError at exit after 'import threading' in other thread

2013-08-07 Thread Thomas Guettler

Thomas Guettler added the comment:

Only few people seem to use daemon threads. We do and see this problem often 
with Python 2.7.

How difficult is it to get this fixed for 2.7?

Is there a way to work around this problem?

--
nosy: +guettli

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1596321
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18674] Store weak references in modules_by_index

2013-08-07 Thread Antoine Pitrou

Antoine Pitrou added the comment:

 It seems to me that the more appropriate change here would be to
 redefine PyState_FindModule as return a *new* ref rather than a
 borrowed ref and have it do the Py_INCREF before returning.
 
 Code using it would then need to add an appropriate Py_DECREF. A
 reference leak is generally a less dangerous bug than an early free.

I hadn't thought about that. Code must add Py_DECREF only on 3.4+, then.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18674
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue15787] PEP 3121 Refactoring

2013-08-07 Thread Alexander Belopolsky

Changes by Alexander Belopolsky alexander.belopol...@gmail.com:


--
dependencies: +PEP 3121 Refactoring applied to _csv module

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15787
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue15787] PEP 3121 Refactoring

2013-08-07 Thread Alexander Belopolsky

Changes by Alexander Belopolsky alexander.belopol...@gmail.com:


--
dependencies: +PEP 3121, 384 refactoring applied to curses_panel module

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15787
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue15787] PEP 3121, 384 Refactoring

2013-08-07 Thread Alexander Belopolsky

Changes by Alexander Belopolsky alexander.belopol...@gmail.com:


--
dependencies: +PEP 384 Refactoring applied to _csv module
title: PEP 3121 Refactoring - PEP 3121, 384 Refactoring

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15787
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue15787] PEP 3121, 384 Refactoring

2013-08-07 Thread Alexander Belopolsky

Changes by Alexander Belopolsky alexander.belopol...@gmail.com:


--
dependencies: +PEP 384 inconsistent with implementation

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15787
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1856] shutdown (exit) can hang or segfault with daemon threads running

2013-08-07 Thread Thomas Guettler

Thomas Guettler added the comment:

There are some examples to work around this for Python2: 
http://stackoverflow.com/questions/18098475/detect-interpreter-shut-down-in-daemon-thread

--
nosy: +guettli

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1856
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue15787] PEP 3121, 384 Refactoring

2013-08-07 Thread Alexander Belopolsky

Changes by Alexander Belopolsky alexander.belopol...@gmail.com:


--
nosy: +pitrou

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15787
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue15787] PEP 3121, 384 Refactoring

2013-08-07 Thread Alexander Belopolsky

Alexander Belopolsky added the comment:


Regarding the suggestion of separating PEP3121 and PEP384. It might be true 
that datetime and other modules do not benefit directly from PEP 384, however 
it is still a fact that the stdlib modules should be seen as a set of reference 
modules, that are all implemented in a way that complies with the 
implementation fo the xxmodules.
I have talked with Martin von Löwis about this, and as far as I understood him 
correctly he also sees the PEP384 refactoring applied to the whole stdlib as a 
necessary signal to other developers to refactor their modules accordingly.
 (Robin Schreiber, #15390, msg177274)

MvL have recently confirmed this on python-dev: Choice of supporting PEP 384 
was deliberate. It will change all types into heap types, which is useful for 
multiple-interpreter support and GC.

Accordingly, I've changed the title of this issue and added a few PEP 384 only 
dependencies.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15787
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18675] Daemon Threads can seg fault

2013-08-07 Thread Thomas Guettler

New submission from Thomas Guettler:

This is a documentation bug: Since #1856  is not solved for Python2, it needs 
to be documented.

Daemon Threads on Python2 can seg fault.

Work arounds: 
http://stackoverflow.com/questions/18098475/detect-interpreter-shut-down-in-daemon-thread

--
assignee: docs@python
components: Documentation
messages: 194601
nosy: docs@python, guettli
priority: normal
severity: normal
status: open
title: Daemon Threads can seg fault
versions: Python 2.6, Python 2.7

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18675
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue15787] PEP 3121, 384 Refactoring

2013-08-07 Thread Alexander Belopolsky

Alexander Belopolsky added the comment:

With respect to PEP 384 refactoring, I would like to see 
Tools/scripts/abitype.py used for most of the conversions.  The PEP itself can 
probably be amended to advertise this tool more prominently.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15787
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13153] IDLE crashes when pasting non-BMP unicode char on Py3

2013-08-07 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

u'\U000104a2' == u'\ud801\udca2' on narrow build.

u'\ud801'.encode('utf-8', 'surrogatepass') == b'\xed\xa0\x81'
u'\udca2'.encode('utf-8', 'surrogatepass') == b'\xed\xb2\xa2'

Hope it will help.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13153
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18671] enhance formatting in logging package

2013-08-07 Thread M. Dietrich

M. Dietrich added the comment:

for a logging library the important thing would be to not loose the information 
that was meant to log. as i said i do alot of long-running huge-data-processing 
scripts in py using the library. if the logging breaks but doesnt log what was 
intended to log i judge this a major problem. and furthermore: it is so simple 
to add both information: the data that was to be logged plus the state that an 
error occured while formatting and where the log-call was made.

--
status: pending - open

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18671
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18668] Properly document setting m_size in PyModuleDef

2013-08-07 Thread Roundup Robot

Roundup Robot added the comment:

New changeset 698fd628b001 by Eli Bendersky in branch '3.3':
Issue #18668: Properly document setting m_size in PyModuleDef
http://hg.python.org/cpython/rev/698fd628b001

New changeset 9877c25d9556 by Eli Bendersky in branch 'default':
Closing #18668: Properly document setting m_size in PyModuleDef
http://hg.python.org/cpython/rev/9877c25d9556

--
nosy: +python-dev
resolution:  - fixed
stage: needs patch - committed/rejected
status: open - closed

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18668
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18668] Properly document setting m_size in PyModuleDef

2013-08-07 Thread Eli Bendersky

Eli Bendersky added the comment:

Thanks for the review!

--
resolution: fixed - 
stage: committed/rejected - needs patch
status: closed - open

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18668
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18668] Properly document setting m_size in PyModuleDef

2013-08-07 Thread Eli Bendersky

Changes by Eli Bendersky eli...@gmail.com:


--
resolution:  - fixed
stage: needs patch - committed/rejected
status: open - closed

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18668
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue15651] PEP 3121, 384 refactoring applied to elementtree module

2013-08-07 Thread Eli Bendersky

Eli Bendersky added the comment:

Antoine, some questions about the patch:

First, I think it omits expat_capi from the state. Is that intentional?

Second, I'm not sure if this approach is fully aligned with PEP 3121. A global, 
shared state is still used. Instead of actually having a different module state 
per subinterpreter, this patch will have shared state. Another problem seems to 
be using PyModule_FindModule without using PyModule_AddModule first. 

These problems could be shared to all of Robin's original patches. Of course, 
there's also the possibility that I don't fully understand PEP 3121 yet :)

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15651
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue15651] PEP 3121, 384 refactoring applied to elementtree module

2013-08-07 Thread Antoine Pitrou

Antoine Pitrou added the comment:

 First, I think it omits expat_capi from the state. Is that
 intentional?

What would it do in the state? There's nothing to release.

 Second, I'm not sure if this approach is fully aligned with PEP 3121.
 A global, shared state is still used. Instead of actually having a
 different module state per subinterpreter, this patch will have
 shared state.

I don't understand what you are talking about. Perhaps you haven't looked
what PyState_FindModule() does?

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15651
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18668] Properly document setting m_size in PyModuleDef

2013-08-07 Thread Nick Coghlan

Nick Coghlan added the comment:

I thought setting m_size to zero was for No per module state, but
reinitialization is fine? Does that not work? (I haven't actually tried it)

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18668
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue15651] PEP 3121, 384 refactoring applied to elementtree module

2013-08-07 Thread Eli Bendersky

Eli Bendersky added the comment:

On Wed, Aug 7, 2013 at 6:28 AM, Antoine Pitrou rep...@bugs.python.orgwrote:


 Antoine Pitrou added the comment:

  First, I think it omits expat_capi from the state. Is that
  intentional?

 What would it do in the state? There's nothing to release.


That's true, but I thought one of the goals of PEP 3121 is to separate
states between sub-interpreters. So that one can't corrupt another. I'm not
sure how much it matters in practice in this case of the pyexpat capsule;
need to look into it more.

  Second, I'm not sure if this approach is fully aligned with PEP 3121.
  A global, shared state is still used. Instead of actually having a
  different module state per subinterpreter, this patch will have
  shared state.

 I don't understand what you are talking about. Perhaps you haven't looked
 what PyState_FindModule() does?


I did not look at the implementation yet. But the documentation says:

Returns the module object that was created from *def* for the current
interpreter. This method requires that the module object has been attached
to the interpreter state with
PyState_AddModule()http://docs.python.org/dev/c-api/module.html?highlight=pymoduledef_base#PyState_AddModulebeforehand.
In case the corresponding module object is not found or has not
been attached to the interpreter state yet, it returns NULL.

I don't see a call to PyState_AddModule. What am I missing?

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15651
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18676] Queue: zero should not be accepted as timeout value

2013-08-07 Thread Zhongyue Luo

New submission from Zhongyue Luo:

The docstring of methods put() and get() in Queue.py states

get(): If 'timeout' is a positive number, it blocks at most 'timeout' seconds 
and raises the Full exception if no free slot was available within that time.

put(): If 'timeout' is a positive number, it blocks at most 'timeout' seconds 
and raises the Empty exception if no item was available within that time.

Additionally the ValueError both methods raise is

 raise ValueError('timeout' must be a positive number)

However the logic checks if 'timeout' is non-negative.

 elif timeout  0:
 raise ValueError('timeout' must be a positive number)

The logic should change as

 elif timeout = 0:
 raise ValueError('timeout' must be a positive number)

--
components: Library (Lib)
messages: 194611
nosy: zyluo
priority: normal
severity: normal
status: open
title: Queue: zero should not be accepted as timeout value
type: behavior
versions: Python 2.7

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18676
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue15651] PEP 3121, 384 refactoring applied to elementtree module

2013-08-07 Thread Antoine Pitrou

Antoine Pitrou added the comment:

 That's true, but I thought one of the goals of PEP 3121 is to
 separate
 states between sub-interpreters. So that one can't corrupt another.
 I'm not
 sure how much it matters in practice in this case of the pyexpat
 capsule;
 need to look into it more.

pyexpat's capi object is a static struct inside pyexpat.c, so that
wouldn't change anything.
Separating states between sub-interpreters only matters when said state
is mutable, which it isn't here.

 I don't see a call to PyState_AddModule. What am I missing?

It is called implicitly when an extension module is imported.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15651
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18676] Queue: document that zero is accepted as timeout value

2013-08-07 Thread R. David Murray

R. David Murray added the comment:

This is more of a documentation issue than a code issue.  To be mathematically 
precise, the text and error message should read a non-negative value.  
Alternatively the text and error could be changed to report that timeout may 
not be negative, which would probably be clearer.  (Note that timeout is a 
float value, and thus the proposed code change would only break code, it would 
not change the functionality in any significant way.)

--
assignee:  - docs@python
components: +Documentation
nosy: +docs@python, r.david.murray
title: Queue: zero should not be accepted as timeout value - Queue: document 
that zero is accepted as timeout value
versions: +Python 3.3, Python 3.4

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18676
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18671] enhance formatting in logging package

2013-08-07 Thread Vinay Sajip

Vinay Sajip added the comment:

From the line number you mentioned, it looks like you're talking about Python 
2.7. However, Python 2.7 is closed to new features: generally speaking, only 
bug fixes are supposed to be committed to this branch.

I can consider expanding the Logged from ... error message for 3.4.

--
resolution: invalid - 
status: open - pending
versions: +Python 3.4

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18671
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-07 Thread Kristján Valur Jónsson

New submission from Kristján Valur Jónsson:

A proposed patch adds two features to context managers:

1)It has always irked me that it was impossible to assemble nested context 
managers in the python language. See issue #5251.
The main problem, that exceptions in __enter__ cannot be properly handled, is 
fixed by introducing a new core exception, ContextManagerExit.  When raised by 
__enter__(), the body that the context manager protects is skipped.  This 
exception is in the spirit of other semi-internal exceptions such as 
GeneratorExit and StopIteration.  Using this exception, contextlib.nested can 
properly handle the case where the body isn't run because of an internal 
__enter__ exception which is handled by an outer __exit__.

2) The mechanism used in implementing ContextManagerExit above is easily 
extended to allowing a special context manager: None.  This is useful for 
having _optional_ context managers.  E.g. code like this:
with performance_timer():
do_work()

def performance_timer():
if profiling:
return accumulator
return None

None becomes the trivial context manager and its __enter__ and __exit__ calls 
are skipped, along with their overhead.

This patch implements both features.
In addition, it:
1) reintroduces contextlib.nested, which is based on nested_delayed
2) introduces contextlib.nested_delayed, which solves the other problem with 
previous versions of nested, that an inner context manager expression shouldn't 
be evaluated early.  contextlib.nested evaluates callables returning context 
managers, rather than managers directly.
3) Allows contextlib.contextmanager decorated functions to not yield, which 
amounts to skipping the protected body (implicitly raising ContextManagerExit)
4) unittests for the whole thing.

I'll introduce this stuff on python-ideas as well.

--
components: Interpreter Core
files: contextmanagerexit.patch
keywords: patch
messages: 194615
nosy: kristjan.jonsson
priority: normal
severity: normal
status: open
title: Enhanced context managers with ContextManagerExit and None
type: enhancement
versions: Python 3.4
Added file: http://bugs.python.org/file31182/contextmanagerexit.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18677
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11619] On Windows, don't encode filenames in the import machinery

2013-08-07 Thread Steven Velez

Steven Velez added the comment:

This may be a small use case, but a use case none-the less.  In my situation, I 
am distributing a frozen python package and it runs under the users home 
directory.   If the user's name has international characters, this will fail.

I expect we will have similar problems when dealing with our application which 
embeds python and is also running from within the user directory...

--
nosy: +Steven.Velez

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue11619
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-07 Thread R. David Murray

R. David Murray added the comment:

Your use cases are either already addressed by contextlib.ExitStack, or should 
be addressed in the context of its existence.  It is the replacement for 
contextlib.nested.

--
nosy: +ncoghlan, r.david.murray

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18677
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18678] Wrong struct members name for spwd module

2013-08-07 Thread Vajrasky Kok

New submission from Vajrasky Kok:

Both python2 and python3 have this behaviour.

 import os; os.getuid()
0
 'I am root'
'I am root'
 import spwd
 spwd.getspnam('bin')
spwd.struct_spwd(sp_nam='bin', sp_pwd='*', sp_lstchg=15558, sp_min=0, 
sp_max=9, sp_warn=7, sp_inact=-1, sp_expire=-1, sp_flag=-1)
 spwd.getspnam.__doc__
'getspnam(name) - (sp_namp, sp_pwdp, sp_lstchg, sp_min, sp_max,\n  
  sp_warn, sp_inact, sp_expire, sp_flag)\nReturn the shadow password 
database entry for the given user name.\nSee spwd.__doc__ for more on shadow 
password database entries.'

The documentation tells the function getspnam will give struct which has member 
sp_namp and sp_pwdp. But as you can see, the function getspnam gives me a tuple 
with has member sp_nam (without p) and sp_pwd (without p).

If you man spwd, you can see the documentation is correct:
Structure
   The shadow password structure is defined in shadow.h as follows:

   struct spwd {
   char *sp_namp; /* Login name */
   char *sp_pwdp; /* Encrypted password */
   long  sp_lstchg;   /* Date of last change (measured
 in days since 1970-01-01 00:00:00 + 
(UTC)) */
   long  sp_min;  /* Min # of days between changes */
   long  sp_max;  /* Max # of days between changes */
   long  sp_warn; /* # of days before password expires
 to warn user to change it */
   long  sp_inact;/* # of days after password expires
 until account is disabled */
   long  sp_expire;   /* Date when account expires (measured
 in days since 1970-01-01 00:00:00 + 
(UTC)) */
   unsigned long sp_flag;  /* Reserved */
   };

For curious souls who do not have unix box:
http://linux.die.net/man/3/getspnam

I have contemplated about whether this behaviour is intended as it is, but I 
guess this is just a bug. Typo.

Attached the patch to fix this inconsistency. I also fixed some documentation 
about sp_inact and sp_expire.

I only marked this as Python 3.4 fix because I am not sure whether we should 
backport it to previous python versions. Some programs that expect sp_nam and 
sp_pwd names could break.

--
components: Extension Modules
files: spwd_struct_members_name_fix.patch
keywords: patch
messages: 194618
nosy: vajrasky
priority: normal
severity: normal
status: open
title: Wrong struct members name for spwd module
type: behavior
versions: Python 3.4
Added file: http://bugs.python.org/file31183/spwd_struct_members_name_fix.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18678
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18678] Wrong struct members name for spwd module

2013-08-07 Thread R. David Murray

R. David Murray added the comment:

Ideally, for backward compatibility reasons we really ought to support access 
by the old (incorrect) name even in 3.4 (with a deprecation warning, even more 
ideally).  I'm not sure if that's practical?

--
nosy: +r.david.murray

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18678
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-07 Thread Kristján Valur Jónsson

Kristján Valur Jónsson added the comment:

IMHO, exitstack is not a very nice construct.  It's implementation is far 
longer than contextlib.nested.

And the chief problem still remains, which has not been addressed until this 
patch (as far as I know):
In Python, it is impossible to combine existing context managers into a nested 
one.  ExitStack may address a use case of nested context managers, but it 
doesn't address the basic problem.

ContextManagerExit comes with its own nice little features, too.  Now you can 
write:

@contextlib.contextmanager:
def if_ctxt(condition):
if condition:
yield

#hey look! an if statement as a with statement!
with if_ctxt(condition):
do_work

This can easily be extended, where a context manager can both manage context, 
_and_ provide optional execution of its block.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18677
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-07 Thread R. David Murray

R. David Murray added the comment:

Raising it on python-ideas sounds like a good idea, then.

I must admit that I don't understand what you mean by combining existing 
context managers into a nested one that isn't addressed by ExitStack.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18677
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue11619] On Windows, don't encode filenames in the import machinery

2013-08-07 Thread Eric Snow

Changes by Eric Snow ericsnowcurren...@gmail.com:


--
nosy: +eric.snow

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue11619
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18677] Enhanced context managers with ContextManagerExit and None

2013-08-07 Thread Kristján Valur Jónsson

Kristján Valur Jónsson added the comment:

Simply put, there is no way in the language to nest two context managers, even 
though we have full access to their implementation model, i.e. can call 
__enter__ and __exit__ manually.  This reflects badly (pun intended) on 
Python's reflection and introspection capabilities.

If context managers are to be first class entities in the language, then you 
ought to be able to write absract code using them, and
assemble complex ones out of simple ones.  Hypothetical code here:

def nest(a, b):
# currently not possible
return c

def run_with_context(ctxt, callable):
# abstract executor
with ctxt:
return callable()

run_with_context(nested(a,b), callable)

ExitStack address one use case that contextlib.nested was supposed to solve, 
namely the cleanup of a dynamic sequence of context managers.  But it does this 
no by creating a new manager, but by providing a programming pattern to follow. 
 In that sensse, the multiple context manager syntax (with (a, b, c): ) is also 
a hack because it provides language magic to perform what you ought to be able 
to do dynamically...

Does this makes sense?

Anyway, by providing the ContextManagerExit exception, then sufficient 
flexibility is added to the context manager mechanism that at least the use 
case of nested() becomes possible.

Context managers are really interesting things.  I was inspired by Raymond 
Hettinger's talk last pycon to explore their capabilities and this is one of 
the things I came up with :)

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18677
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13153] IDLE crashes when pasting non-BMP unicode char on Py3

2013-08-07 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

Seems as Tk stores pasted \U000104a2 as surrogate pair \ud801\udca2. Then 
it encoded in UTF-8 as \xed\xa0\x81\xed\xb2\xa2 end passed to Python. Python 
converts char* to Unicode object with PyUnicode_FromString() which forbids 
invalid UTF-8 including encoded surrogates.

Please test proposed patch on Windows.

--
Added file: http://bugs.python.org/file31184/tkinter_string_conv.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13153
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18674] Store weak references in modules_by_index

2013-08-07 Thread Antoine Pitrou

Antoine Pitrou added the comment:

(and of course, with module states not being PyObjects, we have the same 
lifetimes issues as with Py_buffers not being PyObjects)

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18674
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13153] IDLE crashes when pasting non-BMP unicode char on Py3

2013-08-07 Thread Serhiy Storchaka

Changes by Serhiy Storchaka storch...@gmail.com:


Removed file: http://bugs.python.org/file31184/tkinter_string_conv.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13153
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue13153] IDLE crashes when pasting non-BMP unicode char on Py3

2013-08-07 Thread Serhiy Storchaka

Changes by Serhiy Storchaka storch...@gmail.com:


Added file: http://bugs.python.org/file31185/tkinter_string_conv.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue13153
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18679] include a codec to handle escaping only control characters but not any others

2013-08-07 Thread Derek Wilson

New submission from Derek Wilson:

Escaping strings for serialization or display is a common problem. Currently, 
in python3, in order to escape a sting, you need to do this:

'my\tstring'.encode('unicode_escape').decode('ascii')

This would give you a string that was represented like this:

'my\\tstring'

But this does not present a suitable representation when the string contains 
unicode characters. Consider this example:

s = 'Α\tΩ'

There is no method to write this string this with only the control character 
escaped.

Even python itself recognizes this as a problem and implemented a solution 
for it.

 s = 'Α\tΩ'
 print(s)
Α   Ω
 print(repr(s))
'Α\tΩ'
 print(s.encode('unicode_escape').decode('ascii'))
\u0391\t\u03a9

What I want is public exposure of the functionality to represent control 
characters with their common \ escape sequences (or \x## for control characters 
where necessary - for instance unit and record separators).

I have numerous use cases for this and python's own str.__repr__ implementation 
shows that the functionality is valuable. I would bet that the majority of 
cases where people use unicode_escape something like a control_escape is more 
along the lines of what is desired.

And while we're at it, it would be great if this were a unicode-unicode codec 
like the rot_13 codec. My desired soluiton would look like this:

 import codecs
 s = 'Α\tΩ'
 e = codecs.encode(s, 'control_escape'))
 print(e)
Α\tΩ
 print(codecs.decode(e, 'control_escape'))
Α   Ω

If this is something that could be included in python 3.4, that would be 
awesome. I am willing to work on this if so.

--
components: Library (Lib)
messages: 194625
nosy: underrun
priority: normal
severity: normal
status: open
title: include a codec to handle escaping only control characters but not any 
others
type: enhancement
versions: Python 3.4

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18679
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18676] Queue: document that zero is accepted as timeout value

2013-08-07 Thread Zhongyue Luo

Zhongyue Luo added the comment:

David,

How about like below?

  elif timeout  sys.float_info.epsilon:
 raise ValueError('timeout' must be a positive number)

The docstring has been there for quite a while and IMHO it just doesn't make 
sense passing 0.0 as a timeout value.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18676
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue8713] multiprocessing needs option to eschew fork() under Linux

2013-08-07 Thread Richard Oudkerk

Changes by Richard Oudkerk shibt...@gmail.com:


Added file: http://bugs.python.org/file31186/b3620777f54c.diff

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue8713
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue8713] multiprocessing needs option to eschew fork() under Linux

2013-08-07 Thread Richard Oudkerk

Richard Oudkerk added the comment:

I have done quite a bit of refactoring and added some extra tests.

When I try using the forkserver start method on the OSX Tiger buildbot (the 
only OSX one available) I get errors.  I have disabled the tests for OSX, but 
it seemed to be working before.  Maybe that was with a different buildbot.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue8713
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18680] JSONDecoder should document that it raises a ValueError for malformed data

2013-08-07 Thread Corey Farwell

New submission from Corey Farwell:

Before someone comes in and tries to correct me, I know Python documentation is 
different than Javadocs. It is common to test if the JSON is malformed using a 
try...catch. What if I want to catch something more specific than Exception? 
The only way a user would know what to catch is to `python -c import json; 
json.loads('FAIL')`. Many other Python modules document which exception is 
raised on invalid input/parameters.

--
assignee: docs@python
components: Documentation
messages: 194628
nosy: corey, docs@python
priority: normal
severity: normal
status: open
title: JSONDecoder should document that it raises a ValueError for malformed 
data
type: enhancement
versions: Python 2.6, Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 
3.4, Python 3.5

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18680
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18676] Queue: document that zero is accepted as timeout value

2013-08-07 Thread Richard Oudkerk

Richard Oudkerk added the comment:

 IMHO it just doesn't make sense passing 0.0 as a timeout value.

I have written lots of code that looks like

timeout = max(deadline - time.time(), 0)
some_function(..., timeout=timeout)

This makes perfect sense.  Working code should not be broken -- it is the 
docsting that should be changed.

I can't think of *any* function taking a timeout which rejects a zero timeout.  
See select(), poll(), Condition.wait(), Lock.acquire(), Thread.join().  In each 
case a zero timeout causes a non-blocking call.

Also, note that the implementation does not contradict the docstring or 
documentation: they say nothing about what happens it timeout is zero (or 
negative).

--
nosy: +sbt

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18676
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18680] JSONDecoder should document that it raises a ValueError for malformed data

2013-08-07 Thread Corey Farwell

Corey Farwell added the comment:

Ideally, this would also be decoumented in json.loads/json.load

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18680
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18679] include a codec to handle escaping only control characters but not any others

2013-08-07 Thread R. David Murray

R. David Murray added the comment:

In what way does repr(x)[1:-1] not serve your use case?

--
nosy: +r.david.murray

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18679
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18676] Queue: document that zero is accepted as timeout value

2013-08-07 Thread R. David Murray

R. David Murray added the comment:

Exactly.  0 means Don't wait, just raise an error immediately if the queue is 
empty/full.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue18676
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



  1   2   >