Re: Enum vs OrderedEnum

2013-08-07 Thread Ian Kelly
On Tue, Aug 6, 2013 at 7:55 PM, Ben Finney  wrote:
> Ian Kelly  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  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  wrote:

> David Barroso  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  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,   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= -”? 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= -”? 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=flat&topic_id=42827&forum=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  wrote:
> "Lakshmipathi.G"  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  wrote:

> On Sun, Aug 4, 2013 at 11:35 PM, Markus Rother 
> 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  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  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 :
> 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=10&y=20";
>> l = E.link(url)
>> ET.tostring(l) -> "http://something?x=10&y=20"
>>
>> 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  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  wrote:
> On Tue, Aug 6, 2013 at 7:55 PM, Ben Finney  wrote:
>> Ian Kelly  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  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



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 
> 
> >  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  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  twitter: 
> @liuerfire
> 蛋疼不蛋疼的都可以试着点一下~^_^~ 
> ***/
>
>


-- 
Best regards.
/**
google+: +liuerfire  twitter:
@liuerfire
蛋疼不蛋疼的都可以试着点一下~^_^~ 
***/
-- 
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,   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  twitter:
@liuerfire
蛋疼不蛋疼的都可以试着点一下~^_^~ 
***/
-- 
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  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  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  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  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  twitter:
@liuerfire
蛋疼不蛋疼的都可以试着点一下~^_^~ 
***/
-- 
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?")




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


Re: Reg secure python environment with web terminal emulator

2013-08-07 Thread dieter
"Lakshmipathi.G"  writes:
> Could you please share more info about creating raw binary executable
> and its potential
> problem.

In an earlier message, you reported to have banned "gcc" to
avoid "C" level exploits. A "raw binary executable" would allow
the same exploits. Think of a binary generated elsewhere (where
"gcc" is available) and put into your environment.

I am convinced that 100 % security is impossible - and correspondingly
would use a pragmatic approach: I would rely on OS level
constraints (user with very restricted rights, process running
in an isolated "box") - and ensure the OS is kept up to date
to reduce the risk of exploits of OS security weaknesses.

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


Re: beginner question (True False help)

2013-08-07 Thread wxjmfauth
Le mercredi 7 août 2013 10:17:21 UTC+2, eschne...@comcast.net a écrit :
> 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')

---

Your loop is not very well organized. It should be
at the same time the "loop" and the "condition tester".
Compare your code with this and note the missing and
unnecessary "batman":


>>> def z():
... letters = 'abc'
... c = input('letter: ')
... while c in letters:
... print('do stuff')
... c = input('letter: ')
... print('end, fin, Schluss')
... 
>>> z()
letter: a
do stuff
letter: b
do stuff
letter: b
do stuff
letter: c
do stuff
letter: n
end, fin, Schluss
>>> 
>>> z()
letter: q
end, fin, Schluss


Variant
It is common to use a infinite loop and to break it
in order to end the job.


>>> def z2():
... letters = 'abc'
... while True:
... c = input('letter: ')
... if c not in letters:
... print('end, fin, Schluss')
... break
... else:
... print('do stuff')
... 
>>> z2()
letter: a
do stuff
letter: b
do stuff
letter: a
do stuff
letter: q
end, fin, Schluss


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


Is it possible to make a unittest decorator to rename a method from "x" to "testx?"

2013-08-07 Thread adam . preble
We were coming into Python's unittest module from backgrounds in nunit, where 
they use a decorate to identify tests.  So I was hoping to avoid the convention 
of prepending "test" to the TestClass methods that are to be actually run.  I'm 
sure this comes up all the time, but I mean not to have to do:

class Test(unittest.TestCase):
def testBlablabla(self):
self.assertEqual(True, True)

But instead:
class Test(unittest.TestCase):
@test
def Blablabla(self):
self.assertEqual(True, True)

This is admittedly a petty thing.  I have just about given up trying to 
actually deploy a decorator, but I haven't necessarily given up on trying to do 
it for the sake of knowing if it's possible.

Superficially, you'd think changing a function's __name__ should do the trick, 
but it looks like test discovery happens without looking at the transformed 
function.  I tried a decorator like this:

def prepend_test(func):
print "running prepend_test"
func.__name__ = "test" + func.__name__

def decorator(*args, **kwargs):
return func(args, kwargs)

return decorator

When running unit tests, I'll see "running prepend_test" show up, but a dir on 
the class being tested doesn't show a renamed function.  I assume it only works 
with instances.  Are there any other tricks I could consider?
-- 
http://mail.python.org/mailman/listinfo/python-list