Class decorator to capture the creation and deletion of objects

2014-02-24 Thread Sangeeth Saravanaraj
This question was initially asked in tu...@python.org; Now I am widening
the audience to gain attention.

I want to create a decorator which should do the following things:
 => When an object of the decorated class is created, the objects name (say
the value of the incoming "id" argument) should be stored as a record in a
table in a database.
 => When an object of the decorated class is deleted, the record with this
deleted objects name (i.e. object.id) should be removed from the table.

Now, for example - consider the following snippet:

@saveme
class A(object):
def __init__(self, id):
self.id = id

@saveme
class B(object):
def __init__(self, id):
self.id = id

"saveme" should do what I have explained earlier.

a1 = A("A1")
a2 = A("A2")
a3 = A("A3")
b1 = B("B1")
b2 = B("B2")

At this point if I query and print all the records in a table, I should get
the following output:
["A1", "A2", "A3", "B1", "B2"]

del a1
del a2
del a3
del b1
del b2

At this point, all entries in the table should be deleted; query should
return an empty list!

And, I want to highlight that the classes that are being decorated with
"saveme" can de derived classes too [which initialises its base classes
using super() method]!

Now the following is what I have tried:

class saveme(object):
def __init__(self, klass):
print "saveme::__init__()"
self._klass = klass

def __call__(self, *args, **kwargs):
print "saveme::__call__()"
obj = self._klass(*args, **kwargs)
# creation of DB record will happen here!
# i.e. something like add_to_db(kwargs.get("id"))
return obj

def __del__(self):
# deletion of DB record will happen here!
# i.e. something like remove_from_db(id)
# TODO: how to retrieve the "id" here?!
print "saveme::__del__()"


class Parent1(object):
def __init__(self):
print "Parent1:: __init__()"
super(Parent1, self).__init__()


class Parent2(object):
def __init__(self):
print "Parent2:: __init__()"
super(Parent2, self).__init__()


@saveme
class A(Parent1, Parent2):
def __init__(self, id):
print "A::__init__()"
self.id = id
#super(A, self).__init__()


#@saveme
#class B(object):
#def __init__(self, id):
#print "B::__init__()"
#self.id = id


def main():
a1 = A(id="A1")
#b1 = B(id="B1")

if __name__ == "__main__":
main()


When executed the above, I ran in to the following:

saveme::__init__()
saveme::__call__()
A::__init__()
Traceback (most recent call last):
  File "1.py", line 54, in 
main()
  File "1.py", line 50, in main
a1 = A(id="A1")
  File "1.py", line 10, in __call__
obj = self._klass(*args, **kwargs)
  File "1.py", line 39, in __init__
super(A, self).__init__()
TypeError: must be type, not saveme
saveme::__del__()


When I commented "super(A, self).__init__()" in the class A :: __init__()
method, it returned an object of type A and I was able to see the prints in
the __call__ and __del__ methods but the __init__() methods of the base
classes (Parent1 & Parent2) were not called!

>From the error message, what I could understand is - the object returned by
saveme::__call__() is not of type A but of type saveme. But when I put a
print in the saveme::__call__() I could see it returns an object of type A
and not saveme.

Now the question is - with this approach to capture the initiation and
deletion events of an object, how do I initialise the base classes using
super()?

Or, is there any other better way to capture the __call__ and __del__
 events for an object of a certain class - if so, how?!

Thank you,

Sangeeth


PS:
http://stackoverflow.com/questions/21826854/typeerror-when-using-super-method-with-class-decorator-for-a-derived-class
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.5, bytes, and %-interpolation (aka PEP 461)

2014-02-24 Thread Steven D'Aprano
On Mon, 24 Feb 2014 16:10:53 -0800, Ethan Furman wrote:

> On 02/24/2014 03:55 PM, Steven D'Aprano wrote:

>> Will b'%s' take any arbitrary object, as in:
>>
>> b'Key: %s' % [1, 2, 3, 4]
>> => b'Key: [1, 2, 3, 4]'
> 
> No.

Very glad to hear it.


[...]
>>> Can anybody think of a use-case for this particular feature?
>>
>> Not me.
> 
> I find that humorous, as %a would work with your list example above.  :)

I know. But why would I want to do it? "It won't fail" is not a use-case. 
I can subclass int and give it a __getitem__ method that raise SystemExit, 
but that's not a use-case for doing so :-)

I cannot think of any reason to want to ASCII-ise the repr of arbitrary 
objects, and on the rare occasion that I did, I could say 

repr(obj).encode('ascii', 'backslashescape')


I don't object to this feature, but nor do I want it.


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


Re: Problem with the console on the new python.org site

2014-02-24 Thread Pierre Quentel
Le lundi 24 février 2014 14:19:12 UTC+1, Jean-Michel Pichavant a écrit :
> - Original Message -
> > On Sun, 23 Feb 2014 10:20:15 -0800, Pierre Quentel wrote:
> > 
> > > The new home page of python.org is very nice, congratulations !
> > 
> > The best I can say about it is that I'm extremely underwhelmed by the
> > design, which is far more "busy" and colourful than the old design
> > (this
> > is not a complement), and not happy that it doesn't work correctly
> > without Javascript.
> 
> Steven, you must have been the grumpy smurf in another life ;)
>  
> > > But there is a problem with the online console provided by
> > > PythonAnywhere : with my azerty keyboard, I can't enter characters
> > > such
> > > as ) or ] - very annoying !
> 
> I have an azerty keyboard and it works just fine, your problem must be 
> something else.

It seems to depend on the browser. I had the problem with Firefox 27.0.1 on 
Windows XP ; the link "Feedback" didn't work with this browser, this is why I 
posted here

With Chrome 33.0.1750.117 m and Opera 12.12 the characters are recognised

I reported the issue on the feedback form, and discovered it was already known 
by the PythonAnywhere team (https://www.pythonanywhere.com/forums/topic/213/). 
Hope it will be fixed quickly, it doesn't give a good impression for newcomers 
to Python whose first contact with the language is this console
> 
> 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.

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


Re: [Baypiggies] Class decorator to capture the creation and deletion of objects

2014-02-24 Thread Chris Angelico
On Tue, Feb 25, 2014 at 4:26 PM, alex23  wrote:
> On 25/02/2014 12:34 PM, Chris Angelico wrote:
>>
>> On Tue, Feb 25, 2014 at 1:24 PM, Alex Martelli  wrote:
>>>
>>> At this point, all entries in the table should be deleted; query should
>>> return an empty list!
>>
>>
>> You can't actually depend on del resulting in __del__ being called.
>
>
> Mind those attributions, Chris, it was actually Sangeeth Saravanaraj who
> wrote what you quoted. I'm pretty sure Alex knows all about __del__ :)

Whoops, yes. I deleted a header by mistake. Sorry Alex! And thanks
Alex for pulling me up on it. (We have a few of those situations.
Fortunately we have Steven and Stephen, otherwise we'd have a lot more
of it.)

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


Re: Help father help son / Install Version 2.6 on Mac OSX 10.9

2014-02-24 Thread Ned Deily
In article 
,
 Chris Angelico  wrote:

> On Tue, Feb 25, 2014 at 4:28 PM,   wrote:
> > Trying to install Python 2.6 because PyGames says it will only install if 
> > Python 2.6 is present.
> 
> Are you sure you need 2.6 and not 2.7? This suggests 2.7 works:
> 
> http://pygame.org/wiki/MacCompile

And, in any case, Apple ships OS X 10.9 with copies of both Python 2.7 
and 2.6:

/usr/bin/python2.7
/usr/bin/python2.6

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

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


Re: Help father help son / Install Version 2.6 on Mac OSX 10.9

2014-02-24 Thread Chris Angelico
On Tue, Feb 25, 2014 at 4:28 PM,   wrote:
> Trying to install Python 2.6 because PyGames says it will only install if 
> Python 2.6 is present.

Are you sure you need 2.6 and not 2.7? This suggests 2.7 works:

http://pygame.org/wiki/MacCompile

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


Help father help son / Install Version 2.6 on Mac OSX 10.9

2014-02-24 Thread quequegg
I just want to help him get the environment set up.
Running Mac Maverick, OSX 10.9
Trying to install Python 2.6 because PyGames says it will only install if 
Python 2.6 is present.
When I run make framework install
I am treated to: gcc -c -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g 
-fwrapv -O3 -Wall -Wstrict-prototypes  -I. -IInclude -I./Include   
-DPy_BUILD_CORE -DSVNVERSION=\"`LC_ALL=C svnversion .`\" -o 
Modules/getbuildinfo.o ./Modules/getbuildinfo.c
clang: error: no such file or directory: 'directory"'
make: *** [Modules/getbuildinfo.o] Error 1

Hint?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Baypiggies] Class decorator to capture the creation and deletion of objects

2014-02-24 Thread alex23

On 25/02/2014 12:34 PM, Chris Angelico wrote:

On Tue, Feb 25, 2014 at 1:24 PM, Alex Martelli  wrote:

At this point, all entries in the table should be deleted; query should
return an empty list!


You can't actually depend on del resulting in __del__ being called.


Mind those attributions, Chris, it was actually Sangeeth Saravanaraj who 
wrote what you quoted. I'm pretty sure Alex knows all about __del__ :)

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


Re: Coding a simple state machine in python

2014-02-24 Thread alex23

On 25/02/2014 1:27 PM, Tim Daneliuk wrote:

On 02/24/2014 08:55 PM, William Ray Wing wrote:

On Feb 24, 2014, at 8:30 PM, Ronaldo  wrote:

How do I write a state machine in python?

>>

Stackoverflow has a couple of compact examples here:


Now you're making it TOO easy Bill ;)


No, the _easy_ solution is:

https://pypi.python.org/pypi?%3Aaction=search&term=state++machine&submit=search
--
https://mail.python.org/mailman/listinfo/python-list


Re: [Baypiggies] Class decorator to capture the creation and deletion of objects

2014-02-24 Thread Jacob Alheid
—
Sent from Mailbox for iPad

On Mon, Feb 24, 2014 at 8:49 PM, David Lawrence  wrote:

> If as according to the docs, there is no guarantee of __del__ being called,
> anything that relies on that seems unsafe (depending on how robust one
> needs the solutions to be).  Solutions I've seen elsewhere suggest creating
> something like a *release()* method on your objects and calling it
> explicitly before calling *del my_obj *(if you even call del, rather than
> just relying on GC).
> Alternatively, if it suits the scenario, use a context manager.
> All suggestions lifted from (a SO that informed me in the past):
> http://stackoverflow.com/questions/6104535/i-dont-understand-this-python-del-behaviour
> On Mon, Feb 24, 2014 at 6:24 PM, Alex Martelli  wrote:
>> Off the cuff, I'd make saveme into a function, not a class;
>> the saveme function would alter the class passed in as its only argument
>> (saving __init__ and/or __del__ methods it may have and replacing them with
>> other -- nested -- functions that call them and do the rest of the job) and
>> return the same class object it received.
>>
>> No time to actually write the code but this seems a much sounder
>> architecture.
>>
>>
>> Alex
>>
>>
>>
>> On Mon, Feb 24, 2014 at 4:49 PM, Sangeeth Saravanaraj <
>> sangeeth.saravana...@gmail.com> wrote:
>>
>>> This question was initially asked in tu...@python.org; Now I am widening
>>> the audience to gain attention.
>>>
>>> I want to create a decorator which should do the following things:
>>>  => When an object of the decorated class is created, the objects name
>>> (say the value of the incoming "id" argument) should be stored as a record
>>> in a table in a database.
>>>  => When an object of the decorated class is deleted, the record with
>>> this deleted objects name (i.e. object.id) should be removed from the
>>> table.
>>>
>>> Now, for example - consider the following snippet:
>>>
>>> @saveme
>>> class A(object):
>>> def __init__(self, id):
>>> self.id = id
>>>
>>> @saveme
>>> class B(object):
>>> def __init__(self, id):
>>> self.id = id
>>>
>>> "saveme" should do what I have explained earlier.
>>>
>>> a1 = A("A1")
>>> a2 = A("A2")
>>> a3 = A("A3")
>>> b1 = B("B1")
>>> b2 = B("B2")
>>>
>>> At this point if I query and print all the records in a table, I should
>>> get the following output:
>>> ["A1", "A2", "A3", "B1", "B2"]
>>>
>>> del a1
>>> del a2
>>> del a3
>>> del b1
>>> del b2
>>>
>>> At this point, all entries in the table should be deleted; query should
>>> return an empty list!
>>>
>>> And, I want to highlight that the classes that are being decorated with
>>> "saveme" can de derived classes too [which initialises its base classes
>>> using super() method]!
>>>
>>> Now the following is what I have tried:
>>>
>>> class saveme(object):
>>> def __init__(self, klass):
>>> print "saveme::__init__()"
>>> self._klass = klass
>>>
>>> def __call__(self, *args, **kwargs):
>>> print "saveme::__call__()"
>>> obj = self._klass(*args, **kwargs)
>>> # creation of DB record will happen here!
>>> # i.e. something like add_to_db(kwargs.get("id"))
>>> return obj
>>>
>>> def __del__(self):
>>> # deletion of DB record will happen here!
>>> # i.e. something like remove_from_db(id)
>>> # TODO: how to retrieve the "id" here?!
>>> print "saveme::__del__()"
>>>
>>>
>>> class Parent1(object):
>>> def __init__(self):
>>> print "Parent1:: __init__()"
>>> super(Parent1, self).__init__()
>>>
>>>
>>> class Parent2(object):
>>> def __init__(self):
>>> print "Parent2:: __init__()"
>>> super(Parent2, self).__init__()
>>>
>>>
>>> @saveme
>>> class A(Parent1, Parent2):
>>> def __init__(self, id):
>>> print "A::__init__()"
>>> self.id = id
>>> #super(A, self).__init__()
>>>
>>>
>>> #@saveme
>>> #class B(object):
>>> #def __init__(self, id):
>>> #print "B::__init__()"
>>> #self.id = id
>>>
>>>
>>> def main():
>>> a1 = A(id="A1")
>>> #b1 = B(id="B1")
>>>
>>> if __name__ == "__main__":
>>> main()
>>>
>>>
>>> When executed the above, I ran in to the following:
>>>
>>> saveme::__init__()
>>> saveme::__call__()
>>> A::__init__()
>>> Traceback (most recent call last):
>>>   File "1.py", line 54, in 
>>> main()
>>>   File "1.py", line 50, in main
>>> a1 = A(id="A1")
>>>   File "1.py", line 10, in __call__
>>> obj = self._klass(*args, **kwargs)
>>>   File "1.py", line 39, in __init__
>>> super(A, self).__init__()
>>> TypeError: must be type, not saveme
>>> saveme::__del__()
>>>
>>>
>>> When I commented "super(A, self).__init__()" in the class A :: __init__()
>>> method, it returned an object of type A and I was able to see the prints in
>>> the __call__ and __del__ methods but the __init__() methods of the base
>>> classes (Parent1 & Parent2) were not called!
>>>
>>> From the error message, what I could under

Docs related python powerpoint automation using pywin32

2014-02-24 Thread Jaydeep Patil
Hi,

Required help regarding python powerpoint automation using pywin32.

provide some reference or guides?


Regards
Jaydeep
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Baypiggies] Class decorator to capture the creation and deletion of objects

2014-02-24 Thread David Lawrence
If as according to the docs, there is no guarantee of __del__ being called,
anything that relies on that seems unsafe (depending on how robust one
needs the solutions to be).  Solutions I've seen elsewhere suggest creating
something like a *release()* method on your objects and calling it
explicitly before calling *del my_obj *(if you even call del, rather than
just relying on GC).

Alternatively, if it suits the scenario, use a context manager.

All suggestions lifted from (a SO that informed me in the past):
http://stackoverflow.com/questions/6104535/i-dont-understand-this-python-del-behaviour


On Mon, Feb 24, 2014 at 6:24 PM, Alex Martelli  wrote:

> Off the cuff, I'd make saveme into a function, not a class;
> the saveme function would alter the class passed in as its only argument
> (saving __init__ and/or __del__ methods it may have and replacing them with
> other -- nested -- functions that call them and do the rest of the job) and
> return the same class object it received.
>
> No time to actually write the code but this seems a much sounder
> architecture.
>
>
> Alex
>
>
>
> On Mon, Feb 24, 2014 at 4:49 PM, Sangeeth Saravanaraj <
> sangeeth.saravana...@gmail.com> wrote:
>
>> This question was initially asked in tu...@python.org; Now I am widening
>> the audience to gain attention.
>>
>> I want to create a decorator which should do the following things:
>>  => When an object of the decorated class is created, the objects name
>> (say the value of the incoming "id" argument) should be stored as a record
>> in a table in a database.
>>  => When an object of the decorated class is deleted, the record with
>> this deleted objects name (i.e. object.id) should be removed from the
>> table.
>>
>> Now, for example - consider the following snippet:
>>
>> @saveme
>> class A(object):
>> def __init__(self, id):
>> self.id = id
>>
>> @saveme
>> class B(object):
>> def __init__(self, id):
>> self.id = id
>>
>> "saveme" should do what I have explained earlier.
>>
>> a1 = A("A1")
>> a2 = A("A2")
>> a3 = A("A3")
>> b1 = B("B1")
>> b2 = B("B2")
>>
>> At this point if I query and print all the records in a table, I should
>> get the following output:
>> ["A1", "A2", "A3", "B1", "B2"]
>>
>> del a1
>> del a2
>> del a3
>> del b1
>> del b2
>>
>> At this point, all entries in the table should be deleted; query should
>> return an empty list!
>>
>> And, I want to highlight that the classes that are being decorated with
>> "saveme" can de derived classes too [which initialises its base classes
>> using super() method]!
>>
>> Now the following is what I have tried:
>>
>> class saveme(object):
>> def __init__(self, klass):
>> print "saveme::__init__()"
>> self._klass = klass
>>
>> def __call__(self, *args, **kwargs):
>> print "saveme::__call__()"
>> obj = self._klass(*args, **kwargs)
>> # creation of DB record will happen here!
>> # i.e. something like add_to_db(kwargs.get("id"))
>> return obj
>>
>> def __del__(self):
>> # deletion of DB record will happen here!
>> # i.e. something like remove_from_db(id)
>> # TODO: how to retrieve the "id" here?!
>> print "saveme::__del__()"
>>
>>
>> class Parent1(object):
>> def __init__(self):
>> print "Parent1:: __init__()"
>> super(Parent1, self).__init__()
>>
>>
>> class Parent2(object):
>> def __init__(self):
>> print "Parent2:: __init__()"
>> super(Parent2, self).__init__()
>>
>>
>> @saveme
>> class A(Parent1, Parent2):
>> def __init__(self, id):
>> print "A::__init__()"
>> self.id = id
>> #super(A, self).__init__()
>>
>>
>> #@saveme
>> #class B(object):
>> #def __init__(self, id):
>> #print "B::__init__()"
>> #self.id = id
>>
>>
>> def main():
>> a1 = A(id="A1")
>> #b1 = B(id="B1")
>>
>> if __name__ == "__main__":
>> main()
>>
>>
>> When executed the above, I ran in to the following:
>>
>> saveme::__init__()
>> saveme::__call__()
>> A::__init__()
>> Traceback (most recent call last):
>>   File "1.py", line 54, in 
>> main()
>>   File "1.py", line 50, in main
>> a1 = A(id="A1")
>>   File "1.py", line 10, in __call__
>> obj = self._klass(*args, **kwargs)
>>   File "1.py", line 39, in __init__
>> super(A, self).__init__()
>> TypeError: must be type, not saveme
>> saveme::__del__()
>>
>>
>> When I commented "super(A, self).__init__()" in the class A :: __init__()
>> method, it returned an object of type A and I was able to see the prints in
>> the __call__ and __del__ methods but the __init__() methods of the base
>> classes (Parent1 & Parent2) were not called!
>>
>> From the error message, what I could understand is - the object returned
>> by saveme::__call__() is not of type A but of type saveme. But when I put a
>> print in the saveme::__call__() I could see it returns an object of type A
>> and not saveme.
>>
>> Now the question is - with this ap

Re: Python powerpoint automation using pywin32

2014-02-24 Thread Jaydeep Patil
On Monday, 24 February 2014 17:27:23 UTC+5:30, sffj...@gmail.com  wrote:
> On Monday, 24 February 2014 11:35:08 UTC, Jaydeep Patil  wrote:
> 
> > I need to create a new powerpoint presentation. I need to add images, paste 
> > some graphs, add texts, tables into powerpoint.
> 
> > 
> 
> > Is any link or document available which help me to do this work more 
> > effectivey & faster.
> 
> 
> 
> Always remember, PyPi is your friend.
> 
> 
> 
> I've not used it but the following is available which works with Microsoft's 
> XML based document types. It is not automation per se (and doesn't use 
> pywin32) but a library for pptx document manipulation.
> 
> 
> 
> https://pypi.python.org/pypi/python-pptx/
> 
> 
> 
> Docs are here
> 
> 
> 
> https://python-pptx.readthedocs.org/en/latest/
> 
> 
> 
> --Simon


Hi Simon,

I need to use COM interface for PowerPoint generation.

So let me know any related docs?


Regards
Jaydeep
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Coding a simple state machine in python

2014-02-24 Thread Tim Daneliuk

On 02/24/2014 08:55 PM, William Ray Wing wrote:


On Feb 24, 2014, at 8:30 PM, Ronaldo  wrote:


How do I write a state machine in python? I have identified the states and the 
conditions. Is it possible to do simple a if-then-else sort of an algorithm? 
Below is some pseudo code:

if state == "ABC":
   do_something()
   change state to DEF

if state == "DEF"
   perform_the_next_function()
...

I have a class to which certain values are passed from a GUI and the functions 
above have to make use of those variables. How do I go about doing this? I have 
the following algorithm:

class TestClass():
def __init__(self, var1, var2): #var1 and var2 are received from a GUI
   self.var1 = var1
...
if state == "ABC"
   doSomething(var1, var2)
..

Could someone point me in the right direction? Thank you!

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


And, to extend Tim's suggestion of a dictionary just a bit, note that since 
Python functions are happy to pass function names as arguments, you can use a 
dictionary to make a really nice compact dispatch table.  That is, function A 
does its thing, gets to a new state, and returns as one of its return arguments 
the key into the dictionary that points to the next function_name to be called 
based on that new state.

Stackoverflow has a couple of compact examples here:

http://stackoverflow.com/questions/715457/how-do-you-implement-a-dispatch-table-in-your-language-of-choice

Bill




Now you're making it TOO easy Bill ;)

--

Tim Daneliuk tun...@tundraware.com
PGP Key: http://www.tundraware.com/PGP/

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


Re: Coding a simple state machine in python

2014-02-24 Thread William Ray Wing

On Feb 24, 2014, at 8:30 PM, Ronaldo  wrote:

> How do I write a state machine in python? I have identified the states and 
> the conditions. Is it possible to do simple a if-then-else sort of an 
> algorithm? Below is some pseudo code:
> 
> if state == "ABC":
>   do_something()
>   change state to DEF
> 
> if state == "DEF"
>   perform_the_next_function()
> ...
> 
> I have a class to which certain values are passed from a GUI and the 
> functions above have to make use of those variables. How do I go about doing 
> this? I have the following algorithm:
> 
> class TestClass():
>def __init__(self, var1, var2): #var1 and var2 are received from a GUI
>   self.var1 = var1
> ...
>if state == "ABC"
>   doSomething(var1, var2)
> ..
> 
> Could someone point me in the right direction? Thank you!
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

And, to extend Tim's suggestion of a dictionary just a bit, note that since 
Python functions are happy to pass function names as arguments, you can use a 
dictionary to make a really nice compact dispatch table.  That is, function A 
does its thing, gets to a new state, and returns as one of its return arguments 
the key into the dictionary that points to the next function_name to be called 
based on that new state.

Stackoverflow has a couple of compact examples here:  

http://stackoverflow.com/questions/715457/how-do-you-implement-a-dispatch-table-in-your-language-of-choice

Bill
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Baypiggies] Class decorator to capture the creation and deletion of objects

2014-02-24 Thread Chris Angelico
On Tue, Feb 25, 2014 at 1:24 PM, Alex Martelli  wrote:
> del a1
> del a2
> del a3
> del b1
> del b2
>
> At this point, all entries in the table should be deleted; query should
> return an empty list!
>

You can't actually depend on del resulting in __del__ being called.
The two are tangentially related, but Python doesn't guarantee that
unbinding a name will result in the destruction of the object.

I recommend that, instead, you have some kind of explicit removal system.

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


Re: Coding a simple state machine in python

2014-02-24 Thread Tim Daneliuk

On 02/24/2014 07:30 PM, Ronaldo wrote:

How do I write a state machine in python? I have identified the states and the 
conditions. Is it possible to do simple a if-then-else sort of an algorithm? 
Below is some pseudo code:

if state == "ABC":
do_something()
change state to DEF

if state == "DEF"
perform_the_next_function()
...

I have a class to which certain values are passed from a GUI and the functions 
above have to make use of those variables. How do I go about doing this? I have 
the following algorithm:

class TestClass():
 def __init__(self, var1, var2): #var1 and var2 are received from a GUI
self.var1 = var1
...
 if state == "ABC"
doSomething(var1, var2)
..

Could someone point me in the right direction? Thank you!




There are probably lots of ways to do it, but I'd use a dictionary and
a variable to hold the current state:

CURRENT_STATE = "Start"
DFA_STATE_MACHINE = {"Start" : start_fn, "State1" : state1_fn, "State2" : 
state2_fn }

#
# Functions for each state go here.  They end by setting CURRENT_STATE to some 
value
#

def start_fn():
.
.
.


def state1_fn():

.
.
.


# And so on


# Now run the state machine

while ( CURRENT_STATE != "Done"):

# Execute the function for the current state

DFA_STATE_MACHINE[CURRENT_STATE]()


Like I said, there are other - more compact ways - to do this, but this
is the general idea.   Now - go do your own homework :)



--

Tim Daneliuk tun...@tundraware.com
PGP Key: http://www.tundraware.com/PGP/

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


Coding a simple state machine in python

2014-02-24 Thread Ronaldo
How do I write a state machine in python? I have identified the states and the 
conditions. Is it possible to do simple a if-then-else sort of an algorithm? 
Below is some pseudo code:

if state == "ABC":
   do_something()
   change state to DEF

if state == "DEF"
   perform_the_next_function()
...

I have a class to which certain values are passed from a GUI and the functions 
above have to make use of those variables. How do I go about doing this? I have 
the following algorithm:

class TestClass():
def __init__(self, var1, var2): #var1 and var2 are received from a GUI
   self.var1 = var1
...
if state == "ABC"
   doSomething(var1, var2)
..

Could someone point me in the right direction? Thank you!
   
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.5, bytes, and %-interpolation (aka PEP 461)

2014-02-24 Thread Ethan Furman

On 02/24/2014 03:55 PM, Steven D'Aprano wrote:

On Mon, 24 Feb 2014 11:54:54 -0800, Ethan Furman wrote:


Greetings!

A PEP is under discussion to add %-interpolation back to the bytes type
in Python 3.5.

Assuming the PEP is accepted, what *will* be added back is:

Numerics:

b'%d' % 10  --> b'10'
b'%02x' % 10  --> b'0a'

Single byte:

b'%c' % 80  --> b'P'


Will %c also accept a length-1 bytes object?

b'%c' % b'x'
=> b'x'


Yes.



and generic:

b'%s' % some_binary_blob --> b'tHE*&92h4' (or whatever)


Will b'%s' take any arbitrary object, as in:

b'Key: %s' % [1, 2, 3, 4]
=> b'Key: [1, 2, 3, 4]'


No.


or only something which is already bytes (i.e. a bytes or bytearray
object)?


It must already be bytes, or have __bytes__ method (that returns bytes, 
obviously ;) .



What is under debate is whether we should also add %a:

b'%a' % some_obj  --> b'some_obj_repr'

What %a would do:

get the repr of some_obj

convert it to ascii using backslashreplace (to handle any code points
over 127)

encode to bytes using 'ascii'

Can anybody think of a use-case for this particular feature?


Not me.


I find that humorous, as %a would work with your list example above.  :)

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


Re: Python 3.5, bytes, and %-interpolation (aka PEP 461)

2014-02-24 Thread Steven D'Aprano
On Tue, 25 Feb 2014 00:18:53 +0200, Marko Rauhamaa wrote:

> random...@fastmail.us:
> 
>> On Mon, Feb 24, 2014, at 15:46, Marko Rauhamaa wrote:
>>> That is:
>>> 
>>>  1. ineffient (encode/decode shuffle)
>>> 
>>>  2. unnatural (strings usually have no place in protocols)
>>
>> That's not at all clear. Why _aren't_ these protocols considered text
>> protocols? Why can't you add a string directly to headers?

You cannot mix text strings and byte strings in Python 3. Python 2 allows 
you to do so, and it leads to hard-to-diagnose bugs and confusing 
behaviour. This is why Python 3 insists on a strict separation between 
the two.

But of course you can add *byte* strings directly to byte headers. Just 
prefix your strings with a b, as in b'Header' instead of 'Header', and it 
will work fine.

However, you don't really want to be adding large numbers of byte strings 
together, due to efficiency. Better to use % interpolation to insert them 
all at once. Hence the push to add % to bytes in Python 3.


Marko replied:
> Text expresses a written human language. In prosaic terms, a Python
> string is a sequence of ISO 10646 characters, whose codepoints are not
> octets.

Almost correct, but not quite. Python strings are Unicode, not ISO-10646. 
The two are not the same.

http://www.unicode.org/faq/unicode_iso.html


> Most network protocols are defined in terms of octets, although many of
> them can carry textual, audio or video payloads (among others). So when
> RFC 3507 (ICAP) shows an example starting:
> 
>RESPMOD icap://icap.example.org/satisf ICAP/1.0 Host:
>icap.example.org
>Encapsulated: req-hdr=0, res-hdr=137, res-body=296
> 
> it consists of 8-bit octets and not some human language.

Not really relevant. In practical terms, whether they are implemented as 
octets or not, the sequence "Host" *is* human language, specifically it 
is the English word Host that just happens to be encoded in ASCII. 
Likewise the sequence "Encapsulated" *is* the English word Encapsulated 
encoded in ASCII.


> In practical terms, you get the bytes off the socket as, well, bytes. It
> makes little sense to "decode" those bytes into a string for
> manipulation. Manipulating bytes directly is both more efficient and
> more natural from the point of view of the standard.

But not necessarily more natural from the point of the programmer, which 
is what matters.

I agree that if you don't need to interpret the data as Unicode text, 
then there's no real benefit to decoding to text. (In fact, if your data 
can contain arbitrary bytes, you may not be able to decode to text, since 
not all byte sequences are legal UTF-8.)


> Many internet protocols happen to look like text. It makes it nicer for
> human network programmers to work with them. However, they are primarily
> meant for computers, and the message formats are really a form of binary
> code.

The reason that, say, the subject header line in emails starts with the 
word "Subject" rather than some arbitrary binary code is because it is 
intended to be human-readable. Not just human-readable, but *semantically 
meaningful*. That's why the subject line is labelled "Subject" rather 
than "Field 23" or "SJT".

Fortunately, such headers are usually (always?) ASCII, and byte strings 
in Python privilege ASCII-encoded text. When you write b'Subject', you 
get the same sequence of bytes as 'Subject'.encode('ascii').


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


Re: Python 3.5, bytes, and %-interpolation (aka PEP 461)

2014-02-24 Thread Steven D'Aprano
On Mon, 24 Feb 2014 11:54:54 -0800, Ethan Furman wrote:

> Greetings!
> 
> A PEP is under discussion to add %-interpolation back to the bytes type
> in Python 3.5.
> 
> Assuming the PEP is accepted, what *will* be added back is:
> 
> Numerics:
> 
>b'%d' % 10  --> b'10'
>b'%02x' % 10  --> b'0a'
> 
> Single byte:
> 
>b'%c' % 80  --> b'P'

Will %c also accept a length-1 bytes object?

b'%c' % b'x'
=> b'x'



> and generic:
> 
>b'%s' % some_binary_blob --> b'tHE*&92h4' (or whatever)

Will b'%s' take any arbitrary object, as in:

b'Key: %s' % [1, 2, 3, 4]
=> b'Key: [1, 2, 3, 4]'


or only something which is already bytes (i.e. a bytes or bytearray 
object)?



> What is under debate is whether we should also add %a:
> 
>b'%a' % some_obj  --> b'some_obj_repr'
> 
> What %a would do:
> 
>get the repr of some_obj
> 
>convert it to ascii using backslashreplace (to handle any code points
>over 127)
> 
>encode to bytes using 'ascii'
> 
> Can anybody think of a use-case for this particular feature?


Not me.




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


Re: Python 3.5, bytes, and %-interpolation (aka PEP 461)

2014-02-24 Thread Ethan Furman

On 02/24/2014 01:04 PM, random...@fastmail.us wrote:

On Mon, Feb 24, 2014, at 15:46, Marko Rauhamaa wrote:

That is:

  1. ineffient (encode/decode shuffle)

  2. unnatural (strings usually have no place in protocols)


That's not at all clear. Why _aren't_ these protocols considered text
protocols? Why can't you add a string directly to headers?


Because text is a high-order abstraction.  You don't store text in files, you don't transmit text over the wire or 
through the air -- those actions are done with a lower abstraction, that of bytes.


You're framework may allow you to add a string, but under the covers it's converting to bytes -- at which point is up to 
the framework.


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


Re: Python 3.5, bytes, and %-interpolation (aka PEP 461)

2014-02-24 Thread Marko Rauhamaa
random...@fastmail.us:

> On Mon, Feb 24, 2014, at 15:46, Marko Rauhamaa wrote:
>> That is:
>> 
>>  1. ineffient (encode/decode shuffle)
>> 
>>  2. unnatural (strings usually have no place in protocols)
>
> That's not at all clear. Why _aren't_ these protocols considered text
> protocols? Why can't you add a string directly to headers?

Text expresses a written human language. In prosaic terms, a Python
string is a sequence of ISO 10646 characters, whose codepoints are not
octets.

Most network protocols are defined in terms of octets, although many of
them can carry textual, audio or video payloads (among others). So when
RFC 3507 (ICAP) shows an example starting:

   RESPMOD icap://icap.example.org/satisf ICAP/1.0
   Host: icap.example.org
   Encapsulated: req-hdr=0, res-hdr=137, res-body=296

it consists of 8-bit octets and not some human language.

In practical terms, you get the bytes off the socket as, well, bytes. It
makes little sense to "decode" those bytes into a string for
manipulation. Manipulating bytes directly is both more efficient and
more natural from the point of view of the standard.

Many internet protocols happen to look like text. It makes it nicer for
human network programmers to work with them. However, they are primarily
meant for computers, and the message formats are really a form of binary
code.


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.5, bytes, and %-interpolation (aka PEP 461)

2014-02-24 Thread random832
On Mon, Feb 24, 2014, at 15:46, Marko Rauhamaa wrote:
> That is:
> 
>  1. ineffient (encode/decode shuffle)
> 
>  2. unnatural (strings usually have no place in protocols)

That's not at all clear. Why _aren't_ these protocols considered text
protocols? Why can't you add a string directly to headers?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 3.5, bytes, and %-interpolation (aka PEP 461)

2014-02-24 Thread Marko Rauhamaa
Ethan Furman :

> Can anybody think of a use-case for this particular feature?

Internet protocol entities constantly have to format (and parse)
ASCII-esque octet strings:

headers.append(b'Content-length: %d\r\n' % len(blob))

headers.append(b'Content-length: {}\r\n'.format(len(blob)))

Now you must do:

headers.append(('Content-length: %d\r\n' % len(blob)).encode())

headers.append('Content-length: {}\r\n'.format(len(blob)).encode())

That is:

 1. ineffient (encode/decode shuffle)

 2. unnatural (strings usually have no place in protocols)

 3. confusing (what is stored as bytes, what is stored as strings?)

 4. error-prone (UTF-8 decoding exceptions etc)


To be sure, %s will definitely be needed as well:

   uri = b'http://%s/robots.txt' % host


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can global variable be passed into Python function?

2014-02-24 Thread Mark Lawrence

On 24/02/2014 18:05, j.e.ha...@gmail.com wrote:

On Sunday, February 23, 2014 5:01:25 AM UTC-6, Marko Rauhamaa wrote:

Chris Angelico :


That's the exact line of thinking that leads to problems. You are not



placing a number at the address "xyz", you are pointing the name "xyz"



to the number 3. That number still exists elsewhere.




And?



In C, I can say:



Number *o = malloc(sizeof *o);

o->value = 3;



Your statement is valid: the number 3 resides elsewhere than the

variable o.


typedef struct {
   int value;
} Number;

   Number *o;
   o = malloc(sizeof(*o));
   o->value=3;
   printf("o<%p>, o->value<%p>\n", o, &o->value);

o<0x9fe5008>, o->value<0x9fe5008>

Is the compiler borked?



I can't be bothered to check.  OTOH google groups is so please read and 
action this https://wiki.python.org/moin/GoogleGroupsPython, thanks.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


Re: Python Distributors

2014-02-24 Thread Mark Lawrence

On 24/02/2014 19:40, ed...@cornell.edu wrote:

I am researching the best Python Distributors based on mainly on convenience 
and performance and I am wondering if anyone has any opinions on which are the 
best to use. Thanks!



Given in no particular order I'd have said Peter Otten, Steven D'Aprano, 
Chris Angelico and MRAB.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


Re: Python Distributors

2014-02-24 Thread Nafiul Islam
On Tuesday, February 25, 2014 1:40:35 AM UTC+6, ed...@cornell.edu wrote:
> I am researching the best Python Distributors based on mainly on convenience 
> and performance and I am wondering if anyone has any opinions on which are 
> the best to use. Thanks!

I don't know if you're looking for distributors, as in companies that ship 
commercial versions of Python or different python distributions, so I'll go 
ahead and give you some advice on both.

Regarding commercial distributors, you I feel that both enthought and 
ActiveState provide good packages.

However, if you're looking towards a python distribution geared towards 
performance, why not give pypy a shot? It supports most major frameworks out 
there, and I can say from experience that the performance gains are quite 
handsome.
-- 
https://mail.python.org/mailman/listinfo/python-list


Python 3.5, bytes, and %-interpolation (aka PEP 461)

2014-02-24 Thread Ethan Furman

Greetings!

A PEP is under discussion to add %-interpolation back to the bytes type in 
Python 3.5.

Assuming the PEP is accepted, what *will* be added back is:

Numerics:

  b'%d' % 10  --> b'10'
  b'%02x' % 10  --> b'0a'

Single byte:

  b'%c' % 80  --> b'P'

and generic:

  b'%s' % some_binary_blob --> b'tHE*&92h4' (or whatever)

What is under debate is whether we should also add %a:

  b'%a' % some_obj  --> b'some_obj_repr'

What %a would do:

  get the repr of some_obj

  convert it to ascii using backslashreplace (to handle any code points over 
127)

  encode to bytes using 'ascii'

Can anybody think of a use-case for this particular feature?

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


Python Distributors

2014-02-24 Thread ed352
I am researching the best Python Distributors based on mainly on convenience 
and performance and I am wondering if anyone has any opinions on which are the 
best to use. Thanks!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python

2014-02-24 Thread CM
On Monday, February 24, 2014 3:31:11 AM UTC-5, Karthik Reddy wrote:
> I worked as a weblogic administrator and now i am changing to development and 
> i am very much interested in python . please suggest me what are the 
> things i need to learn more  rather than python to get an I.T job. I came to 
> know about  Django but i am in a confusion please help me .

I recommend you look at job advertisements in areas you'd like to work (both
areas of the world and areas within IT) and see what they seem to want.

Also, consider more informative subject lines to future posts.  :D
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Install python 2 and 3 in the "wrong" order

2014-02-24 Thread Tim Golden

On 15/02/2014 11:25, Nagy László Zsolt wrote:



 From a cmd window:

ftype python.file="C:\Windows\py.exe" "%1" %*

ftype python.noconfile="C:\Windows\pyw.exe" "%1" %*

There is a serious problem with this! Example code test.py:

#!/usr/bin/env python3
import sys
print(sys.argv)

Test run:

C:\Temp>test.py 1 2 3
['C:\\Temp\\test.py']

This is too bad! Can somebody tell me what would be the good setup for
py.exe? Do I have to reinstall Python3 with all libs? :-s


Nagy, are you still seeing this behaviour? Because my system, with the 
exact ftype confiiguration you're describing, behaves as you would expect.


c:\Users\tim>assoc .py
.py=Python.File

c:\Users\tim>ftype python.file
python.file="C:\Windows\py.exe" "%1" %*

c:\Users\tim>t.py 1 2 3
['C:\\Users\\tim\\t.py', '1', '2', '3']

I have 2.7 & 3.3 installed (and about half a dozen other versions, too).

TJG
--
https://mail.python.org/mailman/listinfo/python-list


Re: Mac vs. Linux for Python Development

2014-02-24 Thread Chris Angelico
On Tue, Feb 25, 2014 at 5:25 AM, Jean-Michel Pichavant
 wrote:
> If you go for Linux, know that ubuntu would not be the first choice, ubuntu 
> prefers user experience over stability. Debian for instance is a distribution 
> largely used in the industry.
>

What you'll generally find, actually, is that there's very little
effective difference between one distro and another - there's a lot of
usability difference between, say, Xfce and GNOME3 and Mate and Unity
and so on, but you can get each of those on any Linux that supports
them (personally, I like Xfce, which I use with Debian; you can get
Xubuntu which comes with it). There's also a huge difference between
Python 2.4 and Python 2.7, but beyond the fact that Red Hat 5 happens
to ship with 2.4 and Debian Wheezy happens to ship with 2.7, there's
no connection between that and your distro. Especially among the
families of related distros (Debian and Red Hat being the patriarchs
of huge family trees, and Ubuntu being head of a major sub-tree under
Debian), you'll usually find there's not a huge amount of fundamental
difference.

So pick any distro that strikes your fancy! Try it out! If it doesn't
work out, pick a different one. Start with one that your friends use
(if you have any), that way you can get immediate help.

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


Re: python

2014-02-24 Thread Emile van Sebille

On 2/24/2014 12:31 AM, Karthik Reddy wrote:

I worked as a weblogic administrator and now i am changing to development and i 
am very much interested in python . please suggest me what are the 
things i need to learn more  rather than python to get an I.T job. I came to 
know about  Django but i am in a confusion please help me .



A good place to start: https://wiki.python.org/moin/BeginnersGuide

Emile



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


Re: Mac vs. Linux for Python Development

2014-02-24 Thread Jean-Michel Pichavant
- Original Message -
> Hello,
> 
> I'm sure this is a common question but I can't seem to find a
> previous thread that addresses it.   If one one exists, please point
> me to it.
> 
> I've been developing with python recreationally for a while on Ubuntu
> but will soon be transitioning to full-time python development.  I
> have the option of using a Mac or Ubuntu environment and I'd like to
> hear any thoughts on the pros and cons of each. Specifically, how's
> the support for numpy and scipy?  How are the IDEs?
> 
> Since I generally like working with a Mac, I'd like to hear if there
> are any significant downsides to python dev on OsX.
> 
> Thanks
> 

I'd rather go for linux, I have the feeling that the dev community is larger, 
though I have no numbers to provide, so I may be wrong. One would successfully 
argue that most python dev are cross-platform anyway.

IDEs in Linux are great, and you'll get the best of them for free. However vim 
/ emacs are available for free on OsX as well...

If you go for Linux, know that ubuntu would not be the first choice, ubuntu 
prefers user experience over stability. Debian for instance is a distribution 
largely used in the industry.

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.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can global variable be passed into Python function?

2014-02-24 Thread Chris Angelico
On Tue, Feb 25, 2014 at 5:20 AM,   wrote:
> On Mon, Feb 24, 2014, at 13:05, j.e.ha...@gmail.com wrote:
>> typedef struct {
>>   int value;
>> } Number;
>>
>>   Number *o;
>>   o = malloc(sizeof(*o));
>>   o->value=3;
>>   printf("o<%p>, o->value<%p>\n", o, &o->value);
>>
>> o<0x9fe5008>, o->value<0x9fe5008>
>>
>> Is the compiler borked?
>
> That's cheating. Try printf("o<%p>", &o);

It's not cheating, but it's showing something different. Comparing o
and &o->value shows that the structure itself and its first member are
at the same location in memory. Comparing o and &o shows that the
structure and the pointer to the structure are at different locations
in memory.

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


Re: Can global variable be passed into Python function?

2014-02-24 Thread Chris Angelico
On Tue, Feb 25, 2014 at 5:05 AM,   wrote:
> typedef struct {
>   int value;
> } Number;
>
>   Number *o;
>   o = malloc(sizeof(*o));
>   o->value=3;
>   printf("o<%p>, o->value<%p>\n", o, &o->value);
>
> o<0x9fe5008>, o->value<0x9fe5008>
>
> Is the compiler borked?

No, because a structure in C is simply a laying-out of its members. A
pointer to the structure is exactly the same as a pointer to its first
member, same as a pointer to an array is really just a pointer to its
first element (with other elements laid out sequentially, possibly
with padding for alignment). In Python, a complex object is a thing in
its own right; it has an identity and a set of members, each of which
has its own identity. In C, the structure is an invisible piece of
nothingness that surrounds an aggregation of other objects. For
instance:

typedef struct {
int val1;
int val2;
} NumberPair;

NumberPair five_pairs[5];
int *ten_ints = (int *)five_pairs;

This is considered naughty (pointer aliasing), and can make a mess of
optimization, but as far as I know, it's never going to be a problem
(I don't think it's possible for there to be any padding between two
ints; it's possible the struct will demand coarser alignment than the
ints would, which is why I've written them in that order). You can
access ten integers from the aliased pointer, and five pairs of
integers through the original structure array, and they're all exactly
the same. If you print out those two pointers, they will have the same
value in them, and &five_pairs[3]->val2 will be the same as
&ten_ints[7] (and the same as ten_ints+7).

For in C, neither the structure nor the non-structure is anything. The
only thing that counts is code expressing itself through love.
(Galatians 5:6, K&R translation. Or something like that.)

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


Re: [OT] Can global variable be passed into Python function?

2014-02-24 Thread random832
On Mon, Feb 24, 2014, at 13:19, Michael Torrie wrote:
> Why would you think that? The address of the start of your malloc'ed
> structure is the same as the address of the first element.  Surely this
> is logical?  And of course all this is quite off topic.

That's not helpful - the problem, in context, is that he doesn't
understand that the fact that the structure exists at that address is
not the same thing as the variable "o" existing at that address.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can global variable be passed into Python function?

2014-02-24 Thread random832
On Mon, Feb 24, 2014, at 13:05, j.e.ha...@gmail.com wrote:
> typedef struct {
>   int value;
> } Number;
> 
>   Number *o;
>   o = malloc(sizeof(*o));
>   o->value=3;
>   printf("o<%p>, o->value<%p>\n", o, &o->value);
> 
> o<0x9fe5008>, o->value<0x9fe5008>
> 
> Is the compiler borked?

That's cheating. Try printf("o<%p>", &o);
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [OT] Can global variable be passed into Python function?

2014-02-24 Thread Michael Torrie
On 02/24/2014 11:05 AM, j.e.ha...@gmail.com wrote:
> typedef struct {
>   int value;
> } Number;
> 
>   Number *o;
>   o = malloc(sizeof(*o));
>   o->value=3;
>   printf("o<%p>, o->value<%p>\n", o, &o->value);
> 
> o<0x9fe5008>, o->value<0x9fe5008>
> 
> Is the compiler borked?

Why would you think that? The address of the start of your malloc'ed
structure is the same as the address of the first element.  Surely this
is logical?  And of course all this is quite off topic.


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


Re: Can global variable be passed into Python function?

2014-02-24 Thread j . e . haque
On Sunday, February 23, 2014 5:01:25 AM UTC-6, Marko Rauhamaa wrote:
> Chris Angelico :
> 
> > That's the exact line of thinking that leads to problems. You are not
> 
> > placing a number at the address "xyz", you are pointing the name "xyz"
> 
> > to the number 3. That number still exists elsewhere.
> 
> 
> 
> And?
> 
> 
> 
> In C, I can say:
> 
> 
> 
>Number *o = malloc(sizeof *o);
> 
>o->value = 3;
> 
> 
> 
> Your statement is valid: the number 3 resides elsewhere than the
> 
> variable o.

typedef struct {
  int value;
} Number;

  Number *o;
  o = malloc(sizeof(*o));
  o->value=3;
  printf("o<%p>, o->value<%p>\n", o, &o->value);

o<0x9fe5008>, o->value<0x9fe5008>

Is the compiler borked?

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


Re: Mac vs. Linux for Python Development

2014-02-24 Thread Michael Torrie
On 02/24/2014 10:34 AM, Michael Torrie wrote:
> I know a lot of Mac developers that love the Sublime text editor.  And
> if you combine it with https://github.com/lunixbochs/actualvim, it's
> even better.

Sublime is actually on all platforms, and lots of people like it.

http://www.sublimetext.com/

Personally I just use vim on any platform.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Mac vs. Linux for Python Development

2014-02-24 Thread Michael Torrie
On 02/23/2014 01:43 AM, twiz wrote:
> I've been developing with python recreationally for a while on Ubuntu
> but will soon be transitioning to full-time python development.  I
> have the option of using a Mac or Ubuntu environment and I'd like to
> hear any thoughts on the pros and cons of each. Specifically, how's
> the support for numpy and scipy?  How are the IDEs?

I know a lot of Mac developers that love the Sublime text editor.  And
if you combine it with https://github.com/lunixbochs/actualvim, it's
even better.

Personally OS X's focus policy drives me absolutely bonkers as a
developer.  And I can't function without alt-middle clicking to size
windows and alt-click to move windows.

> Since I generally like working with a Mac, I'd like to hear if there
> are any significant downsides to python dev on OsX.

You can always run a virtual machine on OS X and have the best of both
worlds.



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


Re: Mac vs. Linux for Python Development

2014-02-24 Thread William Ray Wing
On Feb 23, 2014, at 3:43 AM, twiz  wrote:

> Hello,
> 
> I'm sure this is a common question but I can't seem to find a previous thread 
> that addresses it.   If one one exists, please point me to it.
> 
> I've been developing with python recreationally for a while on Ubuntu but 
> will soon be transitioning to full-time python development.  I have the 
> option of using a Mac or Ubuntu environment and I'd like to hear any thoughts 
> on the pros and cons of each. Specifically, how's the support for numpy and 
> scipy?  How are the IDEs?
> 
> Since I generally like working with a Mac, I'd like to hear if there are any 
> significant downsides to python dev on OsX.  
> 
> Thanks
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

In addition to the other excellent answers you've received, I'd point you to

http://stackoverflow.com/questions/81584/what-ide-to-use-for-python

where there is a fairly extensive comparison chart of IDEs, features, and 
supported OSes.

And, by the way, I'm a very happy camper using BBEdit and WingIDE (the name 
collision is purely coincidental).

Thanks,
Bill
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can global variable be passed into Python function?

2014-02-24 Thread Steven D'Aprano
On Mon, 24 Feb 2014 21:07:42 +1300, Gregory Ewing wrote:

> Steven D'Aprano wrote:
>> Yes, Pascal has pointers as a first-class data type. Syntax is similar
>> to C, ^x is a pointer to x, p^ dereferences the pointer p.
> 
> Actually, the prefix ^ is only used for declaring pointer *types*.
> Standard Pascal has no way of getting a pointer to an arbitrary
> variable; the only way to get a pointer is new(p) (which is the
> equivalent of p = malloc(sizeof(*p))).



Standard Pascal? Who uses standard Pascal? I'm talking about MacPascal :-)

Actually, it's possible that I've forgotten the details, it's been over 
twenty years since I last dereferences a Pascal pointer in anger, so you 
might be right... a quick glance at some of my text books suggest that 
you probably are. Thanks for the correction!


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


Re: Problem with the console on the new python.org site

2014-02-24 Thread Jean-Michel Pichavant
- Original Message -
> On Sun, 23 Feb 2014 10:20:15 -0800, Pierre Quentel wrote:
> 
> > The new home page of python.org is very nice, congratulations !
> 
> The best I can say about it is that I'm extremely underwhelmed by the
> design, which is far more "busy" and colourful than the old design
> (this
> is not a complement), and not happy that it doesn't work correctly
> without Javascript.

Steven, you must have been the grumpy smurf in another life ;)
 
> > But there is a problem with the online console provided by
> > PythonAnywhere : with my azerty keyboard, I can't enter characters
> > such
> > as ) or ] - very annoying !

I have an azerty keyboard and it works just fine, your problem must be 
something else.

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.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Functions help

2014-02-24 Thread Jean-Michel Pichavant
- Original Message - 

> On Feb 23, 2014, at 1:44 AM, Steven D'Aprano <
> steve+comp.lang.pyt...@pearwood.info > wrote:
> > Sorry, I don't really understand your question. Could you show an
> > example
> 
> > of what you are doing?
> 

> > Do you mean "add 5" or "*5"? "Add *5 doesn't really mean anything
> > to
> > me.
> 
> Sorry I forgot to add the code that I had to give an example of what
> I was talking about. I’ll put it below, sorry that it’s so long. A

Here's an example of how your star() function could be rewritten.
Note that it does not exactly do what it does in your example regarding the 
start and end position of the turtle.
So use it for inspiration only ;)


from turtle import *
from math import sin, sqrt, radians

def star(width):
R = (width)/(2*sin(radians(72)))
A = (2*width)/(3+sqrt(5))
# record the initial position
position = getturtle().pos()
heading =  getturtle().heading()
penup()
left(36) # initial angle
forward(R)
pendown()
for arms in range(5):
left(144)
forward(A)
right(72)
forward(A)
penup()
# go back to the initial position
getturtle().setpos(position)
getturtle().setheading(heading)
   
showturtle()
clear()
star(50)


-- 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.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Functions help

2014-02-24 Thread Mark Lawrence

On 24/02/2014 04:01, ru...@yahoo.com wrote:

On 02/23/2014 08:21 PM, Mark Lawrence wrote:

On 24/02/2014 02:55, Benjamin Kaplan wrote:

On Sun, Feb 23, 2014 at 5:39 PM, alex23  wrote:

On 24/02/2014 11:09 AM, Mark Lawrence wrote:

On 24/02/2014 00:55, alex23 wrote:

   for _ in range(5):
   func()

the obvious indentation error above


Stupid cut&paste :(


Your message came through fine for me (viewing as mailing list in
gmail). Mark's client must be dropping spaces.


I'm reading gmane.comp.python.general using Thunderbird 24.3.0 on Windows 7.


The original message was properly indented on Google Groups.
Perhaps you should switch to GG or some non-broken client that
doesn't mangle whitespace.



MRAB has confirmed that as always Thunderbird is working perfectly, 
thank you.  But you can stick with your bug ridden, badly flawed tool as 
long as you like, just expect me to keep complaining until google fixes 
it, or people stop using it, or people follow the instructions that were 
put up on a Python web site to prevent the bugs showing up.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


Re: The sum of numbers in a line from a file

2014-02-24 Thread sffjunkie
On Monday, 24 February 2014 11:48:23 UTC, sffj...@gmail.com  wrote:

> split_points = [2, 4, 5]

Change this to `split_points = [3, 5]` for your requirements

--Simon Kennedy
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python powerpoint automation using pywin32

2014-02-24 Thread sffjunkie
On Monday, 24 February 2014 11:35:08 UTC, Jaydeep Patil  wrote:
> I need to create a new powerpoint presentation. I need to add images, paste 
> some graphs, add texts, tables into powerpoint.
> 
> Is any link or document available which help me to do this work more 
> effectivey & faster.

Always remember, PyPi is your friend.

I've not used it but the following is available which works with Microsoft's 
XML based document types. It is not automation per se (and doesn't use pywin32) 
but a library for pptx document manipulation.

https://pypi.python.org/pypi/python-pptx/

Docs are here

https://python-pptx.readthedocs.org/en/latest/

--Simon
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The sum of numbers in a line from a file

2014-02-24 Thread sffjunkie
On Thursday, 20 February 2014 16:22:00 UTC, kxjakkk  wrote:
> Let's say I have a sample file like this:
> Name1   2 34 5  6 78
> 
> name1099-66-7871   A-FY10067815998
> name2999-88-7766   A-FN99   100969190
> name3000-00-0110AUD5100281976
> name4398-72-P/FY7684496978
> name5909-37-3689A-FY97941006179
> 
> For name1, I want to add together columns 4, 5, 6, and get an average from 
> that, then do the same for the last two columns. I want to do this for every 
> name. 

The following solution works for Python3 (due to the unpacking using the * 
syntax)



from collections import defaultdict, namedtuple

info = namedtuple('info', 'sum avg')

interesting_data = (x.strip(' \n') for idx, x in 
enumerate(open('file').readlines()) if idx > 1 and len(x.strip(' \n')) > 0)

split_points = [2, 4, 5]

results = defaultdict(list)
for line in interesting_data:
name, _, _, _, *rest = line.split()

last_point = 0
for point in split_points:
s = sum(map(int, rest[last_point:point]))
i = info(s, s / (point - last_point))

results[name].append(i)
last_point = point

print(results)
print(results['name3'][0].avg)

--Simon Kennedy
-- 
https://mail.python.org/mailman/listinfo/python-list


Python powerpoint automation using pywin32

2014-02-24 Thread Jaydeep Patil
I need to create a new powerpoint presentation. I need to add images, paste 
some graphs, add texts, tables into powerpoint.
Is any link or document available which help me to do this work more effectivey 
& faster.



Regards
Jay
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Functions help

2014-02-24 Thread sffjunkie
On Sunday, 23 February 2014 05:43:17 UTC, Scott W Dunning  wrote:
 
> I had a question regarding functions.  Is there a way to call a function 
> multiple times without recalling it over and over.  Meaning is there a way I 
> can call a function and then add *5 or something like that?

The following answers your question but is probably not a method you want to 
employ. 

from functools import partial 

def a(ch): 
  print(ch) 

def b(x): 
  print(x + 4) 

[x() for x in [partial(a, 'd'), partial(b, 10)]*5] 

Produces 

d 
14 
d 
14 
d 
14 
d 
14 
d 
14 

--Simon 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Functions help

2014-02-24 Thread sffjunkie
On Sunday, 23 February 2014 05:43:17 UTC, Scott W Dunning  wrote:
> I had a question regarding functions.  Is there a way to call a function 
> multiple times without recalling it over and over.  Meaning is there a way I 
> can call a function and then add *5 or something like that?
> 

The following answers your question but is probably not a method you want to 
employ.

from functools import partial

def a(ch):
  print(ch)

def b(x):
  print(x + 10)

[x() for x in [partial(a, 'd'), partial(b, 10)]*5]

Produces

d
14
d
14
d
14
d
14
d
14

--Simon
-- 
https://mail.python.org/mailman/listinfo/python-list


insert html into ElementTree without parsing it

2014-02-24 Thread graeme . pietersz
I am building HTML pages using ElementTree.

I need to insert chunks of untrusted HTML into the page. I do not need or want 
to parse this, just insert it at a particular point as is.

The best solutions I can think of are rather ugly ones: manipulating the string 
created by tostring.

Is there a nicer way of doing this? Is it possible, for example, to customise 
how an element is converted to a string representation? I am open to using 
something else (e.g. lxml) if necessary.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can global variable be passed into Python function?

2014-02-24 Thread wxjmfauth
Le lundi 24 février 2014 01:37:42 UTC+1, Steven D'Aprano a écrit :
> 
> 
> 
> Performance can matter :-)
> 
> 
> 


>>> timeit.timeit("'abc' * 1000 + 'z'")
0.991999136702321
>>> timeit.timeit("'abc' * 1000 + '\N{EURO SIGN}'")
2.5462559386176444
>>> 

Two points to notice

- Even with utf-8, the worse performance case, such
a difference reaches [0.x - a few] percent.

- Indexing? Very well preserved for the "abc part"

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


python

2014-02-24 Thread Karthik Reddy
I worked as a weblogic administrator and now i am changing to development and i 
am very much interested in python . please suggest me what are the 
things i need to learn more  rather than python to get an I.T job. I came to 
know about  Django but i am in a confusion please help me .
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can global variable be passed into Python function?

2014-02-24 Thread Gregory Ewing

Steven D'Aprano wrote:
Yes, Pascal has pointers as a first-class data type. Syntax is similar to 
C, ^x is a pointer to x, p^ dereferences the pointer p.


Actually, the prefix ^ is only used for declaring
pointer *types*. Standard Pascal has no way of getting
a pointer to an arbitrary variable; the only way to
get a pointer is new(p) (which is the equivalent of
p = malloc(sizeof(*p))).

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list