Re: [Tutor] How to run multiline shell command within python

2013-01-10 Thread Hugo Arts
On Thu, Jan 10, 2013 at 7:01 AM, Karim  wrote:

>
>
> Hello all,
>
> I want to run multiline shell command within python without using a
> command file but directly execute several lines of shell.
> I already use *subprocess.checkoutput("csh -f my_file.csh".split())* but I
> want to know if it is posssible to avoid making file and execute
> shell lines of code directly.
>
>
Yes, this is very possible. Specify shell=True as an argument and you can
do anything you can do in a shell:

 >>> commands = """echo hello
... echo hello | wc -l
... ps aux | grep python"""
>>> b = subprocess.check_output(commands, shell=True)
>>> print(b.decode('ascii'))
hello
1
hugo  1255  1.0  0.6 777316 49924 ?Sl   09:14   0:08
/usr/bin/python2 /usr/bi
hugo  6529  0.0  0.0  42408  7196 pts/0S+   09:23   0:00 python
hugo  6559  0.0  0.0  10656  1128 pts/0S+   09:28   0:00 grep python

>>>

watch out though, accepting user input into the commands variable will lead
to shell injection, which can be a dangerous security vulnerability.

HTH,
Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Flow charts

2013-01-10 Thread Alan Gauld

On 10/01/13 02:26, Ed Owens wrote:

First, please start a new thread for a new topic, don't reply to an 
existing one. Threaded mail/news readers are not fooled by a simple 
change of subject so your post gets buried deep in an old thread.

Just create a brand new mail to the list address.
Anyway...


I'm working my way through Chun's book "Core Python Applications
Programming" and can't get one of the examples to actually work.  In
trying to analyze the problem (good learning approach) I had troubles
understanding the interactions between the two classes of objects.  As
an old FORTRAN programmer, I picked up my pencil to flowchart the code,
then realized I didn't know how to flowchart an OOP.


Us a message sequence chart(MSC). A vertical line represents an object. 
Draw arrows between the lines to represent the messages passing between 
them. Messages correspond to function calls in procedural programming.


MSCs are a very old design tool from the communications world but 
adopted by OOP and included in UML (see below)



Google led me to UML (Unified Modeling Language) and OMG (apparently Oh
My God!!!).  Looks more complicated than the code I'm trying to understand.


OMG (Object Management Group) is a body overseeeing many things OO 
based. Design tools, integration technologies, languages  and 
specifications etc.


UML is a design language that, if used to its limit, can be used to 
generate working code. As a result it has a lot of stuff in there to 
represent fine grained detail. So yes, it is as complex as code because 
in a sense that's what it is. Graphical code.


In addition UML is designed to completely represent a large software 
system so it includes notations for designing networks and data centres 
as well as application code.


The good news is that most people don't use UML like that and only use 
about 20% of UML or less. The main diagrams you are likely to use as a 
Python programmer are class diagram, MSC, activity diagram(flow charts!)
and state charts. Focus on those areas and don't obsess on the details 
and UML becomes useful to ordinary mortals. I use UML in the v3 version 
of my tutor in the OOP topic, the examples there hopefully show how UML 
can be used even in simple designs.



It there a technique that people use to figure out how do design OOP
models, objects, and the information flow?


There are lots of them, that's why UML was invented to bring the best 
bits together into a single Unified Modelling Language.


But OOP is more about a different way of thinking about the problem.
You have to start thinking about objects and their responsibilities.
Objects own data and provide services. Other objects use those services 
to provide their services and so on. So we tend to get a bottom up style 
of design. Build (or select from the library) the lowest level
objects then build the next layer up that consume those. Don;t be scared 
to split objects into two(or more) if they start to get unweildy or to 
combine two (or more) if they don;t seem to have a purpose or are 
duplicating services.


CRC cards - not part of UML - are a good place to start when building 
OOP systems. A very easy technique that does not require any fancy tools.


HTH,

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Tobias Marquardt

Hello,

I have a class with some class methods that are also stored in a list.
Now I have a problem calling these methods.
Essentially the relevant code looks like this:

class MyClass(object):

@classmethod
def foo(cls):
cls.method_list[0]()

@classmethod
def bar(cls):
print("Hello World")

method_list = [bar]


So foo() takes bar() from the list and tries calls it, which results in
an error:

File "aco/antsystem/test.py", line 11, in foo
cls.method_list[0]()
TypeError: 'classmethod' object is not callable

I guess the reason is that bar() needs to be called on the class like:
cls.bar(), but how to I achieve this here?
Any suggestions?

Tobias




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Peter Otten
Tobias Marquardt wrote:

> Hello,
> 
> I have a class with some class methods that are also stored in a list.
> Now I have a problem calling these methods.
> Essentially the relevant code looks like this:
> 
> class MyClass(object):
> 
>  @classmethod
>  def foo(cls):
>  cls.method_list[0]()
> 
>  @classmethod
>  def bar(cls):
>  print("Hello World")
> 
>  method_list = [bar]

At this point MyClass is not yet defined, so bar has no information about 
it.
 
 
> So foo() takes bar() from the list and tries calls it, which results in
> an error:
> 
> File "aco/antsystem/test.py", line 11, in foo
>  cls.method_list[0]()
> TypeError: 'classmethod' object is not callable
> 
> I guess the reason is that bar() needs to be called on the class like:
> cls.bar(), but how to I achieve this here?
> Any suggestions?

Build the list outside the class:

MyClass.method_list = [MyClass.bar]

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Hugo Arts
On Thu, Jan 10, 2013 at 11:13 AM, Tobias Marquardt  wrote:

> Hello,
>
> I have a class with some class methods that are also stored in a list.
> Now I have a problem calling these methods.
> Essentially the relevant code looks like this:
>
> class MyClass(object):
>
> @classmethod
> def foo(cls):
> cls.method_list[0]()
>
> @classmethod
> def bar(cls):
> print("Hello World")
>
> method_list = [bar]
>
>
> So foo() takes bar() from the list and tries calls it, which results in
> an error:
>
> File "aco/antsystem/test.py", line 11, in foo
> cls.method_list[0]()
> TypeError: 'classmethod' object is not callable
>
> I guess the reason is that bar() needs to be called on the class like:
> cls.bar(), but how to I achieve this here?
> Any suggestions?
>
>
Why do you need the method list at all? if you simply get rid of that and
call cls.bar() you would be rid of the problem entirely. What piece of your
code requires this list? Perhaps we can redesign that to be rid of it.

Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Tobias M.

Peter Otten wrote:
Build the list outside the class: MyClass.method_list = [MyClass.bar] 
Thanks, that is a solution. But I don't really like to put the list 
outside the class as it is strongly related to the class and not used 
outside.


Hugo Arts wrote:
What piece of your code requires this list? Perhaps we can redesign 
that to be rid of it.
Actually in my code it's not a list but a dictionary. The class is part 
of a network server and handles incomming packets. It reads the header 
of the packet and determines the packet type. The dictionary has the 
packet type as key and the method to handle the packet of the 
corresponding type as value. So the dictionary is used to look up the 
correct method to process the packet.


With this I try to avoid a lot of "if...elif..." statements and make it 
easy to expand the network protocol by just adding a new handle method 
and put it in the dictionary.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Steven D'Aprano

On 10/01/13 21:13, Tobias Marquardt wrote:

Hello,

I have a class with some class methods that are also stored in a list.
Now I have a problem calling these methods.


Peter Otten has already given one solution. Read on for two more.


Essentially the relevant code looks like this:

class MyClass(object):

@classmethod
def foo(cls):
 cls.method_list[0]()

@classmethod
def bar(cls):
print("Hello World")

method_list = [bar]


So foo() takes bar() from the list and tries calls it, which results in
an error:

File "aco/antsystem/test.py", line 11, in foo
cls.method_list[0]()
TypeError: 'classmethod' object is not callable

I guess the reason is that bar() needs to be called on the class like:
cls.bar(), but how to I achieve this here?


To answer your direct question, all you need to do is manually imitate what
Python does when you call a descriptor (see below):


py> MyClass.method_list[0].__get__(MyClass, None)()
Hello World


So, inside the foo() method, do this:

@classmethod
def foo(cls):
cls.method_list[0].__get__(cls, None)()


and it should work.

"What the hell is a descriptor?" I hear you ask. Don't worry about them,
they are an advanced part of Python, used to implement methods (regular,
class and static) and properties. If you care, you can read this:

http://docs.python.org/2/howto/descriptor.html

but you may find it heavy going. (I know I did the first five times I
read it.)

Here's another solution: use a regular function object instead of a
method. This relies on an unusual, but deliberate, quirk of the way Python
implements methods: they are actually regular functions inside the class
body when the class is created, and don't get turned into methods until you
do a lookup like "self.spam" or "cls.ham".


class MyClass(object):
@classmethod
def foo(cls):
# Note that you manually provide the class argument.
cls.method_list[0](cls)

def bar(cls):
print("Hello World")

# At this time, bar is still a regular function object, not a
# method of any sort.
method_list = [bar]
del bar  # just to avoid any confusion


By *not* turning bar into a class method, it remains an ordinary
function object. You then store the function object inside the list,
and when you access it via method_list[0] Python gives you the regular
function, not a method. Since it is a regular function, it is callable,
but you have to manually provide the cls argument.



--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Peter Otten
Tobias M. wrote:

> Peter Otten wrote:
>> Build the list outside the class: MyClass.method_list = [MyClass.bar]
> Thanks, that is a solution. But I don't really like to put the list
> outside the class as it is strongly related to the class and not used
> outside.

> Actually in my code it's not a list but a dictionary. The class is part 

Well, I usually prefer to keep things simple, but you can do the binding 
manually:

>>> class A(object):
... @classmethod
... def foo(cls):
... print "Hello from A.foo()"
... lookup = {"foo": foo}
... @classmethod
... def get_handler(cls, packet_type):
... return cls.lookup[packet_type].__get__(None, cls)
... 
>>> A.get_handler("foo")()
Hello from A.foo()

If you adopt this approach you might omit the lookup dictionary and use 
getattr():

>>> class B(object):
... @classmethod
... def handle_foo(cls): print "Hello from B.handle_foo()"
... @classmethod
... def get_handler(cls, packet_type):
... return getattr(cls, "handle_" + packet_type)
... 
>>> B.get_handler("foo")()
Hello from B.handle_foo()

(I've added the "handle_" prefix so an attacker can't just invent package 
types to have arbitrary methods invoked.)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Tobias M.

Steven D'Aprano wrote:
To answer your direct question, all you need to do is manually imitate 
what

Python does when you call a descriptor (see below):


py> MyClass.method_list[0].__get__(MyClass, None)()
Hello World


So, inside the foo() method, do this:

@classmethod
def foo(cls):
cls.method_list[0].__get__(cls, None)()


and it should work.

"What the hell is a descriptor?" I hear you ask. Don't worry about them,
they are an advanced part of Python, used to implement methods (regular,
class and static) and properties. If you care, you can read this:

http://docs.python.org/2/howto/descriptor.html

but you may find it heavy going. (I know I did the first five times I
read it.)

Here's another solution: use a regular function object instead of a
method. This relies on an unusual, but deliberate, quirk of the way 
Python

implements methods: they are actually regular functions inside the class
body when the class is created, and don't get turned into methods 
until you

do a lookup like "self.spam" or "cls.ham".


class MyClass(object):
@classmethod
def foo(cls):
# Note that you manually provide the class argument.
cls.method_list[0](cls)

def bar(cls):
print("Hello World")

# At this time, bar is still a regular function object, not a
# method of any sort.
method_list = [bar]
del bar  # just to avoid any confusion


By *not* turning bar into a class method, it remains an ordinary
function object. You then store the function object inside the list,
and when you access it via method_list[0] Python gives you the regular
function, not a method. Since it is a regular function, it is callable,
but you have to manually provide the cls argument.



Thank you for this great answer!
I like the first approach (using __get__()). I tried it out but got 
problem when bar operates on a class variable of MyClass:


class MyClass(object):

x = "Hello World"

@classmethod
def foo(cls):
cls.method_list[0].__get__(cls, None)()

@classmethod
def bar(cls):
print(cls.x)

method_list = [bar]


I get:
File "test.py", line 17, in bar
print(cls.x)
AttributeError: type object 'type' has no attribute 'x'

When using Peter Otten's solution bar is able to access cls.x.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread eryksun
On Thu, Jan 10, 2013 at 7:11 AM, Steven D'Aprano  wrote:
>
> So, inside the foo() method, do this:
>
> @classmethod
> def foo(cls):
> cls.method_list[0].__get__(cls, None)()

That should be

cls.method_list[0].__get__(None, cls)()

The way you have it binds the method to type(MyClass), which in this
case is "type" itself. See cm_descr_get in funcobject.c, CPython 2.7
(line 645):

http://hg.python.org/cpython/file/70274d53c1dd/Objects/funcobject.c#l635

Also see the slot wrapper wrap_descr_get for the mapping from None to
NULL (line 4671):

http://hg.python.org/cpython/file/70274d53c1dd/Objects/typeobject.c#l4660
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Tobias M.

Am 10.01.2013 13:57, schrieb eryksun:

That should be

 cls.method_list[0].__get__(None, cls)()

The way you have it binds the method to type(MyClass), which in this
case is "type" itself. See cm_descr_get in funcobject.c, CPython 2.7
(line 645):

http://hg.python.org/cpython/file/70274d53c1dd/Objects/funcobject.c#l635

Also see the slot wrapper wrap_descr_get for the mapping from None to
NULL (line 4671):

http://hg.python.org/cpython/file/70274d53c1dd/Objects/typeobject.c#l4660

This works. Thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread eryksun
On Thu, Jan 10, 2013 at 7:48 AM, Peter Otten <__pete...@web.de> wrote:
>
> If you adopt this approach you might omit the lookup dictionary and use
> getattr():
>
 class B(object):
> ... @classmethod
> ... def handle_foo(cls): print "Hello from B.handle_foo()"
> ... @classmethod
> ... def get_handler(cls, packet_type):
> ... return getattr(cls, "handle_" + packet_type)
> ...
 B.get_handler("foo")()
> Hello from B.handle_foo()
>
> (I've added the "handle_" prefix so an attacker can't just invent package
> types to have arbitrary methods invoked.)

This approach also makes it simpler to override handlers in a
subclass. You don't have to worry about separately updating a copy of
a list or dict.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Tobias M.

Am 10.01.2013 13:48, schrieb Peter Otten:

If you adopt this approach you might omit the lookup dictionary and use
getattr():

... @classmethod
... def handle_foo(cls): print "Hello from B.handle_foo()"
... @classmethod
... def get_handler(cls, packet_type):
... return getattr(cls, "handle_" + packet_type)
...

Well I think this is an elegant solution.

But due to my protocol a packet type is uniquely defined by a 
combination of two header fields (that contain integers). Building a 
verbose method name from these would complicate it again.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Dave Angel
On 01/10/2013 06:36 AM, Tobias M. wrote:
> Peter Otten wrote:
>> Build the list outside the class: MyClass.method_list = [MyClass.bar] 
> Thanks, that is a solution. But I don't really like to put the list
> outside the class as it is strongly related to the class and not used
> outside.

But as it's the simplest solution, and one with no runtime overhead, you
really should consider it.  Having a line or two following the class
definition is not uncommon in Python, and comments can make sure the
reader of the code understands it's part of the class initialization.

If that really offends you, put the code in a _setup() method of the
same class, and run MyClass._setup() immediately after defining the
class.  _setup() can be a classmethod as well, and you could have it
protect itself against running more than once, perhaps by refusing to
run if the list/dict already contains entries.



-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Tobias M.

Tobias M. wrote:

Well I think this is an elegant solution.

But due to my protocol a packet type is uniquely defined by a 
combination of two header fields (that contain integers). Building a 
verbose method name from these would complicate it again.

EDIT: I actually meant "meaningful method name".





___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Tobias M.

Dave Angel wrote:

But as it's the simplest solution, and one with no runtime overhead, you
really should consider it.  Having a line or two following the class
definition is not uncommon in Python, and comments can make sure the
reader of the code understands it's part of the class initialization.

If that really offends you, put the code in a _setup() method of the
same class, and run MyClass._setup() immediately after defining the
class.  _setup() can be a classmethod as well, and you could have it
protect itself against running more than once, perhaps by refusing to
run if the list/dict already contains entries.

Interesting objection! However I'll probably implement the solution 
using __get__(None, cls) to get the method from the dictionary. It 
doesn't really add complexity and the additional overhead is 
insignificant in my use case. There will be only one client connected at 
a time and not much communication. The time the server will need to 
process the packet data will be disproportionally larger than the time 
to read the header and choose the handler method.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] garbage collection/class question

2013-01-10 Thread richard kappler
Class is still something I struggle with. I think I'm finally starting to
get my head wrapped around it, but the discussion in a different thread has
sparked a question. First, please check my understanding:
A class creates objects, it's like a template that allows me to create as
many copies as I want of the object but allows me to have slightly
different versions of the object by having different values for the
variables within the object, which I can set with arguments?
By using  __init__ (self) I instantiate a new copy of the object?

Whether the above is correct or not (and do please correct me/ tutor me),
my question is, if I create objects in a while True loop, do the objects
get garbage collected, ie. disappear when the loop returns to the beginning
and creates new versions of the objects?

Psuedo code example:

(presuming I have a class that creates a lemon, a lime and has a function
that operates on them called juice)

while True:
lemon = yellow() # create a yellow object called lemon
lime = green() # create a green object called lime
drink = juice(lemon, lime) # operate on objects lemon and lime in a
function called juice resident in the class

So, using the above example, when the loop reaches the end and returns to
the beginning, new lemons and limes are created, yes? What happens to the
old ones? Or have I still got this completed boggled?

regards, RIchard

-- 

quando omni flunkus moritati
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Peter Otten
Tobias M. wrote:

> Am 10.01.2013 13:48, schrieb Peter Otten:
>> If you adopt this approach you might omit the lookup dictionary and use
>> getattr():
>>
>> ... @classmethod
>> ... def handle_foo(cls): print "Hello from B.handle_foo()"
>> ... @classmethod
>> ... def get_handler(cls, packet_type):
>> ... return getattr(cls, "handle_" + packet_type)
>> ...
> Well I think this is an elegant solution.
> 
> But due to my protocol a packet type is uniquely defined by a
> combination of two header fields (that contain integers). Building a
> verbose method name from these would complicate it again.

Technically, not much:

>>> class C(object):
... @classmethod
... def handle_1_42(self): print "Hi"
... @classmethod
... def get_handler(cls, packet_type):
... return getattr(cls, "handle_%d_%d" % packet_type)
... 
>>> C.get_handler((1, 42))()
Hi

Of course handle_1_42() is not exactly the method name one would hope for. 
You could, again, strive for simplicity and add a lookup table that maps 
protocol tuples to function /names/ , but as simplicity doesn't seem to be 
your cup of tea:

class HandlersType(type):
def __init__(cls, name, bases, classdict):
cls.lookup_protocol = lookup = {}
for k, v in classdict.items():
if isinstance(v, protocol_method):
lookup[v.protocol] = getattr(cls, k)

class protocol_method(classmethod):
pass

def protocol(x, y):
def attach_protocol(f):
f = protocol_method(f)
f.protocol = x, y
return f
return attach_protocol

class D:
__metaclass__ = HandlersType

@protocol(42, 17)
def foo(cls):
print "Hi"

D.lookup_protocol[42, 17]()

;)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to run multiline shell command within python

2013-01-10 Thread Karim

On 10/01/2013 09:31, Hugo Arts wrote:
On Thu, Jan 10, 2013 at 7:01 AM, Karim > wrote:




Hello all,

I want to run multiline shell command within python without using
a command file but directly execute several lines of shell.
I already use *subprocess.checkoutput("csh -f
my_file.csh".split())* but I want to know if it is posssible to
avoid making file and execute
shell lines of code directly.


Yes, this is very possible. Specify shell=True as an argument and you 
can do anything you can do in a shell:


 >>> commands = """echo hello
... echo hello | wc -l
... ps aux | grep python"""
>>> b = subprocess.check_output(commands, shell=True)
>>> print(b.decode('ascii'))
hello
1
hugo  1255  1.0  0.6 777316 49924 ?Sl 09:14   0:08 
/usr/bin/python2 /usr/bi

hugo  6529  0.0  0.0  42408  7196 pts/0S+ 09:23   0:00 python
hugo  6559  0.0  0.0  10656  1128 pts/0S+ 09:28   0:00 grep python

>>>

watch out though, accepting user input into the commands variable will 
lead to shell injection, which can be a dangerous security vulnerability.


HTH,
Hugo


Many thanks Hugo. It makes my day!
In my case there are no possibilities for shell injection. It is 
internal to a class.


Regards
Karim

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] garbage collection/class question

2013-01-10 Thread Hugo Arts
On Thu, Jan 10, 2013 at 3:06 PM, richard kappler wrote:

> Class is still something I struggle with. I think I'm finally starting to
> get my head wrapped around it, but the discussion in a different thread has
> sparked a question. First, please check my understanding:
> A class creates objects, it's like a template that allows me to create as
> many copies as I want of the object but allows me to have slightly
> different versions of the object by having different values for the
> variables within the object, which I can set with arguments?
> By using  __init__ (self) I instantiate a new copy of the object?
>
>
This is essentially correct. There are some intricacies in python with
__init__, which doesn't create a new object but merely initiates it. Python
also has __new__, which actually creates a new instance. But this is mostly
just details, in 99% of cases we can simply write an __init__ method for
initialization and not worry about __new__


> Whether the above is correct or not (and do please correct me/ tutor me),
> my question is, if I create objects in a while True loop, do the objects
> get garbage collected, ie. disappear when the loop returns to the beginning
> and creates new versions of the objects?
>
> Psuedo code example:
>
> (presuming I have a class that creates a lemon, a lime and has a function
> that operates on them called juice)
>
> while True:
> lemon = yellow() # create a yellow object called lemon
> lime = green() # create a green object called lime
> drink = juice(lemon, lime) # operate on objects lemon and lime in a
> function called juice resident in the class
>
> So, using the above example, when the loop reaches the end and returns to
> the beginning, new lemons and limes are created, yes? What happens to the
> old ones? Or have I still got this completed boggled?
>
>
In a general sense, objects are garbage collected when there are no more
references to these objects, i.e. when you become unable to use them. On
each pass of the loop, you make the names lemon, lime and drink point to
new instances. The instances they were pointing to previously thus become
inaccessible. If there are no other references to these objects in the
program, they will be garbage collected (whether this happens immediately
or after a while differs depending on python implementation).

Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] garbage collection/class question

2013-01-10 Thread Dave Angel
On 01/10/2013 09:06 AM, richard kappler wrote:

Since you don't specify Python version or implementation, I'll use
CPython version 2.7 for the details below.  Jython (for java
environments) and other implementations are likely to differ in their
garbage collection details.  But the effect will be the same.  I'm also
assuming new-style classes, which are all that's available in Python 3,
but in 2.7, you get one by making sure your class is derived from object.

> Class is still something I struggle with. I think I'm finally starting to
> get my head wrapped around it, but the discussion in a different thread has
> sparked a question. First, please check my understanding:
> A class creates objects, it's like a template that allows me to create as
> many copies as I want of the object but allows me to have slightly
> different versions of the object by having different values for the
> variables within the object, which I can set with arguments?

Python is already full of class objects.  The only special thing about
'class' is it lets you define your own types of objects.  There are
dozens of built-in and library types, and you're probably already
familiar with many of them.  For example, every integer is an instance
of the int class.  Python has a special syntax that lets you create them
invisibly, but they are there, nonetheless.  I don't think it's useful
to think of them as copies of a master int, but rather as independent
int objects, each with some content that may or may not be unique.

> By using  __init__ (self) I instantiate a new copy of the object?

The new INSTANCE is made when somebody invokes  MyClass(arg1).  During
that creation, two methods of MyClass are called.  The __new__() method
actually creates the empty class object, and associates methods and such
to it.  And then the __init__() method gets control and can add other
attributes to it.  Most programs let the default __new__() method do its
thing.


> Whether the above is correct or not (and do please correct me/ tutor me),
> my question is, if I create objects in a while True loop, do the objects
> get garbage collected, ie. disappear when the loop returns to the beginning
> and creates new versions of the objects?
>
> Psuedo code example:
>
> (presuming I have a class that creates a lemon, a lime and has a function
> that operates on them called juice)

But your code below does not fit the sentence above.

> while True:
> lemon = yellow() # create a yellow object called lemon

Actually, the object has no name.  The name lemon is bound to the object
in that line, but it's really a one-way binding.  However, CPython keeps
a count of such bindings for each object, called a reference count.

> lime = green() # create a green object called lime
> drink = juice(lemon, lime) # operate on objects lemon and lime in a
> function called juice resident in the class

Since you don't give any context, I don't know where() juice is defined,
or where this loop is defined, nor what you mean by 'the class'.  But if
you mean what you say, then you're missing an object parameter to the
juice method.

> So, using the above example, when the loop reaches the end and returns to
> the beginning, new lemons and limes are created, yes?

Not exactly.  New instances of class yellow and class green are created,
and the names lemon and lime are unbound from the old objects, and bound
to those new objects.  If this loop is all the code, then the old
objects now have zero reference counts and will be immediately freed. 
Technically this is different than garbage collection, but the effect is
the same.

>  What happens to the
> old ones? Or have I still got this completed boggled?
>
> regards, RIchard
>
>

Objects that have no bindings are inaccessible, and eventually go away
(get freed).



-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Tobias M.

Am 10.01.2013 15:15, schrieb Peter Otten:

Of course handle_1_42() is not exactly the method name one would hope for.
You could, again, strive for simplicity and add a lookup table that maps
protocol tuples to function /names/ , but as simplicity doesn't seem to be
your cup of tea:

class HandlersType(type):
 def __init__(cls, name, bases, classdict):
 cls.lookup_protocol = lookup = {}
 for k, v in classdict.items():
 if isinstance(v, protocol_method):
 lookup[v.protocol] = getattr(cls, k)

class protocol_method(classmethod):
 pass

def protocol(x, y):
 def attach_protocol(f):
 f = protocol_method(f)
 f.protocol = x, y
 return f
 return attach_protocol

class D:
 __metaclass__ = HandlersType

 @protocol(42, 17)
 def foo(cls):
 print "Hi"

D.lookup_protocol[42, 17]()

;)


In my eyes this is anything but simple.
Correct me if I'm wrong:
I already use a lookup table in my code, called "method_list" in my 
first post. The advantage of your above code is that I don't need to 
manually take care of the lookup table and extension by subclassing is 
easier.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] running multiple versions of python

2013-01-10 Thread Fowler, Trent
The archives aren't searchable or I would have started there.  If there is a 
thread dealing with this, please send me there.

I am running Windows 7 and I've installed two versions of python, 3.3 and 2.7. 
Python 3.3 was the first version I installed and I was able to run scripts from 
the desktop (not the command line). I installed python 2.7 so that I could get 
numpy, scipy, and matplotlib down the road, but I found that all the scripts on 
my desktop defaulted to python 2.7. Since I coded in 3.3 this caused some 
issues.

I was able to fix this by right clicking the script icon, browsing programs, 
navigating to the python 3.3 file in my C: drive, and selecting the idle inside 
that directory. But then I found I wasn't able to run those scripts with python 
2.7 using the exact same procedure.

Unfortunately I don't know command-line programming very much at all, and it 
seemed like most of what I've seen online is geared towards that as a solution. 
Ideally I'd like to specify which python I want to run a script from the 
desktop, or possibly while I'm editing the script.

If it's relevant I'm running the eric python IDE, version 5.

I'd be happy to do the work of reading something fairly technical and long 
(like a blog post or PDF), but it won't help me much if it isn't aimed at 
beginners.

Thanks in advance, 
-Trent  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] running multiple versions of python

2013-01-10 Thread eryksun
On Thu, Jan 10, 2013 at 11:34 AM, Fowler, Trent  wrote:
>
> Python 3.3 was the first version I installed and I was able to run scripts
> from the desktop (not the command line). I installed python 2.7 so that
> I could get numpy, scipy, and matplotlib down the road, but I found that
> all the scripts on my desktop defaulted to python 2.7.

numpy, scipy, and matplotlib are available for Python 3.

3.3 includes launchers (py.exe and pyw.exe) that process UNIX-style
shebangs in order to run a script with a given version of Python,
provided it's available. These launchers should be installed in the
Windows directory (e.g. C:\Windows). Try reinstalling 3.3. Then open a
console and verify the following settings:

C:\>assoc .py
.py=Python.File

C:\>ftype Python.File
Python.File=C:\Windows\py.ex "%1" %*

C:\>assoc .pyw
.pyw=Python.NoConFile

C:\>ftype Python.NoConFile
Python.NoConFile="C:\Windows\pyw.exe" "%1" %*


> Ideally I'd like to specify which python I want to run a script from the 
> desktop,
> or possibly while I'm editing the script.

Refer to PEP 397 and the docs for the launcher:

https://bitbucket.org/vinay.sajip/pylauncher/src/tip/Doc/launcher.rst

For example, it accepts common UNIX shebangs such as

#! /usr/bin/env python2

I think the launcher defaults to the latest 2.x if it's installed. If
you want a different default such as 3.3, edit py.ini in your
profile's AppData\Local folder. The following should open the file (or
create it) with notepad:

notepad %LOCALAPPDATA%\py.ini

Refer to the "Customization via INI files" section of the docs.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] garbage collection/class question

2013-01-10 Thread Mitya Sirenef

On 01/10/2013 09:06 AM, richard kappler wrote:

Class is still something I  struggle with. I think I'm finally starting

> to get my head wrapped around it, but the discussion in a different
> thread has sparked a question. First, please check my understanding:

> A class creates objects, it's like a template that allows me to create
> as many copies as I want of the object but allows me to have slightly
> different versions of the object by having different values for the
> variables within the object, which I can set with arguments?

There are good answers already, I just want to address this question;
you are correct but classes allow you to do other things, too. You may
want to use a class even if you don't need multiple instances. Classes
allow you to group related functionality and data together:

class Tree(object):
height = 0

def grow(self):
self.height += 1

You may have a dozen of related functions and you can logically group
them together by making them methods of a class, making it easier to
think about and work on the logic of your program.

Classes also create a namespace for each instance:

x = 1

class A(object):
x = 2

a = A()

a.x = 3

Here, a.x is completely independent of the global namespace's 'x'. In
instance methods, it can be called with 'self.x' .

There are other things classes provide, but these are the most important
in small / medium size programs. You can read up on OOP here:

http://en.wikipedia.org/wiki/Object-oriented_programming


HTH, - m



--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] running multiple versions of python

2013-01-10 Thread Alan Gauld

On 10/01/13 16:34, Fowler, Trent wrote:

The archives aren't searchable


The archives on ActiveState and gmane are. :-)

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] running multiple versions of python

2013-01-10 Thread Alan Gauld

On 10/01/13 16:34, Fowler, Trent wrote:


Ideally I'd like to specify which python I want to run a script from the desktop


Eryksun has mentioned the launchers but you can do this with standard 
Windows functionality.


You just need to edit the file associations to create new extensions 
.py2 and .py3, associate them with the right python executable and name 
the files appropriately.


Alternatively create new context menu entries for .py files to "Run in 
Python2" and "Run in Python 3" (and edit ones too if you want - "Edit in 
IDLE2", "Edit in IDLE3")


From memory you access the options to do that from the Windows Explorer
menus. Try searching the windows help for details.

No need to mess with command prompts... although as a programmer you 
should probably learn that skill anyway. Quote: "GUIs make easy things 
trivial and hard things impossible" - anon.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to run multiline shell command within python

2013-01-10 Thread Karim

On 10/01/2013 16:21, Matty Sarro wrote:
Have you looked a the pexpect class? It works like gangbusters, 
especially if you're trying to run something with an interactive shell.


http://www.noah.org/wiki/pexpect


On Thu, Jan 10, 2013 at 9:25 AM, Karim > wrote:


On 10/01/2013 09:31, Hugo Arts wrote:

On Thu, Jan 10, 2013 at 7:01 AM, Karim mailto:kliat...@gmail.com>> wrote:



Hello all,

I want to run multiline shell command within python without
using a command file but directly execute several lines of shell.
I already use *subprocess.checkoutput("csh -f
my_file.csh".split())* but I want to know if it is posssible
to avoid making file and execute
shell lines of code directly.


Yes, this is very possible. Specify shell=True as an argument and
you can do anything you can do in a shell:

 >>> commands = """echo hello
... echo hello | wc -l
... ps aux | grep python"""
>>> b = subprocess.check_output(commands, shell=True)
>>> print(b.decode('ascii'))
hello
1
hugo  1255  1.0  0.6 777316 49924 ?  Sl   09:14   0:08
/usr/bin/python2 /usr/bi
hugo  6529  0.0  0.0  42408  7196 pts/0S+   09:23   0:00
python
hugo  6559  0.0  0.0  10656  1128 pts/0S+   09:28   0:00
grep python

>>>

watch out though, accepting user input into the commands variable
will lead to shell injection, which can be a dangerous security
vulnerability.

HTH,
Hugo


Many thanks Hugo. It makes my day!
In my case there are no possibilities for shell injection. It is
internal to a class.

Regards
Karim


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




Thanks Matty!

I will have a look specially for interactive session.

Regards
Karim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with calling class methods stored in a list

2013-01-10 Thread Steven D'Aprano

On 10/01/13 23:57, eryksun wrote:

On Thu, Jan 10, 2013 at 7:11 AM, Steven D'Aprano  wrote:


So, inside the foo() method, do this:

 @classmethod
 def foo(cls):
 cls.method_list[0].__get__(cls, None)()


That should be

 cls.method_list[0].__get__(None, cls)()


Oops, yes, thanks for the correction.


--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor