Re: Eric - No module named MainWindow

2013-02-18 Thread Vincent Vande Vyvre
Le 18/02/13 00:04, Phil a écrit :
 ...

 from PyQt4 import QtCore, QtGui
 from ui.MainWindow import MainWindow

As I've sayed in my last post, this is not ui.MainWindow but
ui.mainwindow
-- 
Vincent V.V.
Oqapy https://launchpad.net/oqapy . Qarte
https://launchpad.net/qarte . PaQager https://launchpad.net/paqager
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Struggling with program

2013-02-18 Thread Jean-Michel Pichavant
- Original Message -
 I'm trying to do this assignment and it not working, I don't
 understand why...
 
 This is what I have to do:
 
 Write the definition of a class  Player containing:
 An instance variable  name of type  String , initialized to the empty
 String.
 An instance variable  score of type  int , initialized to zero.
 A method called  set_name that has one parameter, whose value it
 assigns to the instance variable  name .
 A method called  set_score that has one parameter, whose value it
 assigns to the instance variable  score .
 A method called  get_name that has no parameters and that returns the
 value of the instance variable  name .
 A method called  get_score that has no parameters and that returns
 the value of the instance variable  score .
  No constructor need be defined.
 Here is my code:
 
 class Player:
 
   
   name = ''
   
   def __init__(self,score = 0)
   
   def set_name (self):
   self.name
 
   def set_score (self):
   self.score
 
   def get_name
   return name
   
   def  get_score
   return score
 
 can someone please help me?

class Player:

  # here are class attributes
  foo = 'I am a class attribute'

  # now the methods
  def __init__(self):
# instance attributes
self.name = ''
self.score = 0

  def set_name(self, name):
self.name = name

  def get_name(self):
return self.name
  

I hope you'll be able to figure how to write the score related methods.

Cheers,

JM


-- IMPORTANT NOTICE: 

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


Re: Struggling with program

2013-02-18 Thread Jean-Michel Pichavant
 So your assignment is a bit misleading when it says a constructor is
 not
 required, because if you want to initialize some instance attributes,
 it
 has to be done in a method, usually the constructor, which in Python
 is
 __init__.

By constructor, he may actually refer to the __new__ method.

JM


-- IMPORTANT NOTICE: 

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


Re: Eric - No module named MainWindow

2013-02-18 Thread Phil

On 18/02/13 18:07, Vincent Vande Vyvre wrote:

Le 18/02/13 00:04, Phil a écrit :

...

from PyQt4 import QtCore, QtGui
from ui.MainWindow import MainWindow


As I've sayed in my last post, this is not ui.MainWindow but
ui.mainwindow



Thanks again Vincent,

Your answer is correct but that wasn't the only problem. Anyway, as I 
said in my previous message, it works now and I'm pleased that I have 
advanced a little with pyqt4.


--
Regards,
Phil
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python-list Digest, Vol 113, Issue 111

2013-02-18 Thread Jean-Michel Pichavant

- Original Message - 
 Hi, I don't know if I should ask this on here, or in the tutor
 section, but I heard that http://www.lighttable.com was an
 innovative IDE, so I was wondering if it works for python since I'm
 learning python over time. Thanks


Hi, 

Please do not use html format when asking this list. And cut any text that is 
not related to your question (you included a thread that had nothing to do with 
your question).
And last request, please do not top post.

Anyway, welcome to the list. The IDE you mentioned seems to aim web development 
and state to support Flask, a python web framework.
I'm not sure it's suitable for general purpose though.
I would also advise a python beginner *not* to use an alpha IDE.

Regarding which is the best IDE for python, the answer is simple, the best IDe 
is ... no just kidding :p you'll find a tons of flame wars in this list about 
the best IDE, search the archive for it.

Here's one, I'm not using it but it's quite effective.
http://eric-ide.python-projects.org/eric-screenshots.html

cheers

JM


-- IMPORTANT NOTICE: 

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


python mail box

2013-02-18 Thread cyberkishor
I want to display mail to django apps from my google accout.
and when the fetch all unread message,i want to replay them from my apps so i 
need replay option also.
by point:
1. First Fetch all unread mail from google account.
2. If replay from apps it's replay those email. 
3. i want to same this email with my db also
-- 
http://mail.python.org/mailman/listinfo/python-list


Differences creating tuples and collections.namedtuples

2013-02-18 Thread John Reid
Hi,

I was hoping namedtuples could be used as replacements for tuples in all 
instances. There seem to be some differences between how tuples and namedtuples 
are created. For example with a tuple I can do:

a=tuple([1,2,3])

with namedtuples I get a TypeError:

from collections import namedtuple
B=namedtuple('B', 'x y z')
b=B([1,2,3])

TypeError: __new__() takes exactly 4 arguments (2 given)
 ipython-input-23-d1da2ef851fb(3)module()
  1 from collections import namedtuple
  2 B=namedtuple('B', 'x y z')
 3 b=B([1,2,3])

I'm seeing this problem because of the following code in IPython:

def canSequence(obj):
if isinstance(obj, (list, tuple)):
t = type(obj)
return t([can(i) for i in obj])
else:
return obj

where obj is a namedtuple and t([can(i) for i in obj]) fails with the 
TypeError. See http://article.gmane.org/gmane.comp.python.ipython.user/10270 
for more info.

Is this a problem with namedtuples, ipython or just a feature?

Thanks,
John.

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


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Oscar Benjamin
On 18 February 2013 11:47, John Reid johnbaronr...@gmail.com wrote:
 Hi,

 I was hoping namedtuples could be used as replacements for tuples in all 
 instances.

namedtuples are not really intended to serves as tuples anywhere. They
are intended to provide lightweight, immutable, hashable objects with
*named* (rather than numbered) values.

 There seem to be some differences between how tuples and namedtuples are 
 created. For example with a tuple I can do:

 a=tuple([1,2,3])

 with namedtuples I get a TypeError:

 from collections import namedtuple
 B=namedtuple('B', 'x y z')
 b=B([1,2,3])

For a namedtuple you need to unpack the arguments

b = B(*[1, 2, 3])

or

b = B(1, 2, 3)


 TypeError: __new__() takes exactly 4 arguments (2 given)
 ipython-input-23-d1da2ef851fb(3)module()
   1 from collections import namedtuple
   2 B=namedtuple('B', 'x y z')
  3 b=B([1,2,3])

 I'm seeing this problem because of the following code in IPython:

 def canSequence(obj):
 if isinstance(obj, (list, tuple)):
 t = type(obj)
 return t([can(i) for i in obj])
 else:
 return obj

What is the point of the code above? If obj is a list or a tuple you
create a new list or tuple with the same data and then return it
otherwise you just return the object. What about:

def canSequence(obj):
return obj

Or is it that you want to copy the object (but only when it is a tuple
or list). Then how about

def canSequence(obj):
if isinstance(obj, (list, tuple)):
return obj[:]
else:
return obj

Note that this turns namedtuples into tuples. It might be better to
catch TypeError rather than special casing the types:

def canSequence(obj):
try:
return obj[:]
except TypeError:
return obj

Or perhaps it would be better to use the copy module (assuming that
the purpose is to copy the object).


 where obj is a namedtuple and t([can(i) for i in obj]) fails with the 
 TypeError. See http://article.gmane.org/gmane.comp.python.ipython.user/10270 
 for more info.

 Is this a problem with namedtuples, ipython or just a feature?

I think that it is a problem with the canSequence function.


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


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Oscar Benjamin
On 18 February 2013 12:03, Oscar Benjamin oscar.j.benja...@gmail.com wrote:
 On 18 February 2013 11:47, John Reid johnbaronr...@gmail.com wrote:
 I'm seeing this problem because of the following code in IPython:

 def canSequence(obj):
 if isinstance(obj, (list, tuple)):
 t = type(obj)
 return t([can(i) for i in obj])
 else:
 return obj

 What is the point of the code above? If obj is a list or a tuple you
 create a new list or tuple with the same data and then return it
 otherwise you just return the object. What about:

Sorry, I didn't read this properly. I see that you want apply can() to
all the elements. What is the reason for wanting to preserve the type
of the sequence?


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


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Dave Angel

On 02/18/2013 06:47 AM, John Reid wrote:

Hi,

I was hoping namedtuples could be used as replacements for tuples in all 
instances. There seem to be some differences between how tuples and namedtuples 
are created. For example with a tuple I can do:

a=tuple([1,2,3])

with namedtuples I get a TypeError:

from collections import namedtuple
B=namedtuple('B', 'x y z')
b=B([1,2,3])


You are passing a single list to the constructor, but you specified that 
the namedtuple was to have 3 items.  So you need two more.




TypeError: __new__() takes exactly 4 arguments (2 given)

ipython-input-23-d1da2ef851fb(3)module()

   1 from collections import namedtuple
   2 B=namedtuple('B', 'x y z')
 3 b=B([1,2,3])

I'm seeing this problem because of the following code in IPython:

def canSequence(obj):
 if isinstance(obj, (list, tuple)):
 t = type(obj)
 return t([can(i) for i in obj])
 else:
 return obj

where obj is a namedtuple and t([can(i) for i in obj]) fails with the 
TypeError. See http://article.gmane.org/gmane.comp.python.ipython.user/10270 
for more info.

Is this a problem with namedtuples, ipython or just a feature?

Thanks,
John.



If you want one item (list or tuple) to act like 3 separate arguments, 
you could use the * operator:


b = B( *[1,2,3] )

or in your canSequence function, if you want a namedTuple
  return t(*[can(i) for i in obj])



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


Re: Python trademark - A request for civility

2013-02-18 Thread Jean-Michel Pichavant
- Original Message -
 Folks,
 
 It seems that people have been sending threats and abuse to the
 company
 claiming a trademark on the name Python. And somebody, somewhere,
 may
 have launched a DDOS attack on their website.
 
 The Python Software Foundation has asked the community for restraint
 and
 civility during this dispute. Abuse and threats just bring the Python
 community into disrepute.
 
 http://pyfound.blogspot.com/2013/02/asking-for-civility-during-our.html
 
 
 
 --
 Steven

They've been launching at us rantingrick attacks for a long time, it's time for 
some payback ! 

JM


-- IMPORTANT NOTICE: 

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


Re: Recording live video stream from IP camera issue

2013-02-18 Thread Sam Berry
Thanks for the advice! I looked into it, seems using windows was an issue, 
switched to ubuntu and it worked! 

Just wondering if you have used opencv for recording purposes?

Or if you know of any python documentation for opencv2?

Information is hard to come by!

Thanks again, Sam
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simulation of human body in movement

2013-02-18 Thread Juhani Karlsson
Does it need to be simulation? That sound quite complicated.
Can`t you just get some motion capture animation data?

- J

On 18 February 2013 06:51, Nick Mellor thebalance...@gmail.com wrote:

 Hi all,

 I'm looking for a fairly undetailed simulation of the human body walking
 and standing. Has anyone had a go at this in cgkit or similar?

 Thanks,

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




-- 
-- 
Juhani Karlsson
3D Artist/TD

Talvi Digital Oy
Pursimiehenkatu 29-31 b 2krs.
00150 Helsinki
+358 443443088
juhani.karls...@talvi.fi
www.vimeo.com/talvi
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread John Reid

On 18/02/13 12:05, Oscar Benjamin wrote:
 On 18 February 2013 12:03, Oscar Benjamin oscar.j.benja...@gmail.com wrote:
 On 18 February 2013 11:47, John Reid johnbaronr...@gmail.com wrote:
 I'm seeing this problem because of the following code in IPython:

 def canSequence(obj):
 if isinstance(obj, (list, tuple)):
 t = type(obj)
 return t([can(i) for i in obj])
 else:
 return obj
 What is the point of the code above? If obj is a list or a tuple you
 create a new list or tuple with the same data and then return it
 otherwise you just return the object. What about:
 Sorry, I didn't read this properly. I see that you want apply can() to
 all the elements. What is the reason for wanting to preserve the type
 of the sequence?


Well like I said it is not me that wants to do this. It is part of the
code in IPython for sending messages between clients and engines.

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


Re: Python trademark - A request for civility

2013-02-18 Thread rusi
On Feb 18, 5:19 pm, Jean-Michel Pichavant jeanmic...@sequans.com
wrote:
 - Original Message -
  Folks,

  It seems that people have been sending threats and abuse to the
  company
  claiming a trademark on the name Python. And somebody, somewhere,
  may
  have launched a DDOS attack on their website.

  The Python Software Foundation has asked the community for restraint
  and
  civility during this dispute. Abuse and threats just bring the Python
  community into disrepute.

 http://pyfound.blogspot.com/2013/02/asking-for-civility-during-our.html

  --
  Steven

 They've been launching at us rantingrick attacks for a long time, it's time 
 for some payback !

 JM

In an over-legalized atmosphere its best to be very careful with
everything we say -- jokes included.
Anything you say could be used against you etc etc
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread John Reid
On 18/02/13 12:03, Oscar Benjamin wrote:
 On 18 February 2013 11:47, John Reid johnbaronr...@gmail.com wrote:
 Hi,

 I was hoping namedtuples could be used as replacements for tuples in all 
 instances.
 namedtuples are not really intended to serves as tuples anywhere. They
 are intended to provide lightweight, immutable, hashable objects with
 *named* (rather than numbered) values.
If they are not supposed to be tuples then calling them namedtuples and
inheriting from tuple seems a little odd.

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


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread John Reid
On 18/02/13 12:11, Dave Angel wrote:
 On 02/18/2013 06:47 AM, John Reid wrote:
 Hi,

 I was hoping namedtuples could be used as replacements for tuples in
 all instances. There seem to be some differences between how tuples
 and namedtuples are created. For example with a tuple I can do:

 a=tuple([1,2,3])

 with namedtuples I get a TypeError:

 from collections import namedtuple
 B=namedtuple('B', 'x y z')
 b=B([1,2,3])
 
 You are passing a single list to the constructor, but you specified that
 the namedtuple was to have 3 items.  So you need two more.

I'm aware how to construct the namedtuple and the tuple. My point was
that they use different syntaxes for the same operation and this seems
to break ipython. I was wondering if this is a necessary design feature
or perhaps just an oversight on the part of the namedtuple author or
ipython developers.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Oscar Benjamin
On 18 February 2013 14:09, John Reid j.r...@mail.cryst.bbk.ac.uk wrote:
 On 18/02/13 12:11, Dave Angel wrote:
 On 02/18/2013 06:47 AM, John Reid wrote:
 Hi,

 I was hoping namedtuples could be used as replacements for tuples in
 all instances. There seem to be some differences between how tuples
 and namedtuples are created. For example with a tuple I can do:

 a=tuple([1,2,3])

 with namedtuples I get a TypeError:

 from collections import namedtuple
 B=namedtuple('B', 'x y z')
 b=B([1,2,3])

 You are passing a single list to the constructor, but you specified that
 the namedtuple was to have 3 items.  So you need two more.

 I'm aware how to construct the namedtuple and the tuple. My point was
 that they use different syntaxes for the same operation and this seems
 to break ipython. I was wondering if this is a necessary design feature
 or perhaps just an oversight on the part of the namedtuple author or
 ipython developers.

I would say that depending on isinstance(obj, tuple) was the error. I
can't offer a suggestion as you haven't clarified what the purpose of
this code in canSequence() is or what constraints it is expected to
satisfy.


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


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread John Reid

On 18/02/13 14:12, Oscar Benjamin wrote:
 On 18 February 2013 13:51, John Reid johnbaronr...@gmail.com wrote:
 On 18/02/13 12:03, Oscar Benjamin wrote:
 On 18 February 2013 11:47, John Reid johnbaronr...@gmail.com wrote:
 Hi,

 I was hoping namedtuples could be used as replacements for tuples in all 
 instances.
 namedtuples are not really intended to serves as tuples anywhere. They
 are intended to provide lightweight, immutable, hashable objects with
 *named* (rather than numbered) values.
 If they are not supposed to be tuples then calling them namedtuples and
 inheriting from tuple seems a little odd.
 You can use namedtuple instances in places that expect tuples.
 Inheriting from tuples enables them to be all the things I said:
 lightweight, immutable and hashable. The type object itself has a
 different interface for the constructor, though.


Then I can't use them in every place I use tuples. For example IPython
relies upon the type's interface for the constructor as part of a
serialization mechanism.

I wonder why they have a different interface. It seems to restrict their
usability. No doubt there were other factors involved in the design of
the interface.

John.

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


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Oscar Benjamin
On 18 February 2013 13:51, John Reid johnbaronr...@gmail.com wrote:
 On 18/02/13 12:03, Oscar Benjamin wrote:
 On 18 February 2013 11:47, John Reid johnbaronr...@gmail.com wrote:
 Hi,

 I was hoping namedtuples could be used as replacements for tuples in all 
 instances.
 namedtuples are not really intended to serves as tuples anywhere. They
 are intended to provide lightweight, immutable, hashable objects with
 *named* (rather than numbered) values.

 If they are not supposed to be tuples then calling them namedtuples and
 inheriting from tuple seems a little odd.

You can use namedtuple instances in places that expect tuples.
Inheriting from tuples enables them to be all the things I said:
lightweight, immutable and hashable. The type object itself has a
different interface for the constructor, though.


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


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread John Reid


On 18/02/13 14:15, Oscar Benjamin wrote:
 On 18 February 2013 14:09, John Reid j.r...@mail.cryst.bbk.ac.uk wrote:
 On 18/02/13 12:11, Dave Angel wrote:
 On 02/18/2013 06:47 AM, John Reid wrote:
 Hi,

 I was hoping namedtuples could be used as replacements for tuples in
 all instances. There seem to be some differences between how tuples
 and namedtuples are created. For example with a tuple I can do:

 a=tuple([1,2,3])

 with namedtuples I get a TypeError:

 from collections import namedtuple
 B=namedtuple('B', 'x y z')
 b=B([1,2,3])

 You are passing a single list to the constructor, but you specified that
 the namedtuple was to have 3 items.  So you need two more.

 I'm aware how to construct the namedtuple and the tuple. My point was
 that they use different syntaxes for the same operation and this seems
 to break ipython. I was wondering if this is a necessary design feature
 or perhaps just an oversight on the part of the namedtuple author or
 ipython developers.
 
 I would say that depending on isinstance(obj, tuple) was the error. I
 can't offer a suggestion as you haven't clarified what the purpose of
 this code in canSequence() is or what constraints it is expected to
 satisfy.
 

Like I said it is not my code. I'm hoping the IPython developers can
help me out here. That said it would be nice to know the rationale for
namedtuple.__new__ to have a different signature to tuple.__new__. I'm
guessing namedtuple._make has a similar interface to tuple.__new__. Does
anyone know what the rationale was for this design?

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


Re: python mail box

2013-02-18 Thread Jean-Michel Pichavant
- Original Message -
 I want to display mail to django apps from my google accout.
 and when the fetch all unread message,i want to replay them from my
 apps so i need replay option also.
 by point:
 1. First Fetch all unread mail from google account.
 2. If replay from apps it's replay those email.
 3. i want to same this email with my db also
 --
 http://mail.python.org/mailman/listinfo/python-list
 

Your question being ?

JM


-- IMPORTANT NOTICE: 

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


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Oscar Benjamin
On 18 February 2013 14:23, John Reid j.r...@mail.cryst.bbk.ac.uk wrote:
[snip]
 That said it would be nice to know the rationale for
 namedtuple.__new__ to have a different signature to tuple.__new__. I'm
 guessing namedtuple._make has a similar interface to tuple.__new__. Does
 anyone know what the rationale was for this design?

Because namedtuples use names for the arguments in the constructor:

 from collections import namedtuple
 Point = namedtuple('Point', 'x y')
 p1 = Point(x=2, y=3)
 p1
Point(x=2, y=3)
 p1.x
2


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


Multiple Plotting in Matplotlib

2013-02-18 Thread subhabangalore
Dear Group,

I am trying to view multiple plotting files in matplotlib. My numbers range 
from 5 to few hundred. I was trying to use plt.subplot(), and plt.figure(n).
But they did not work.
plt.subplot() did not work at all.
plt.figure(n) works till n=4. After that I am trying to get error messages.

If any one of the learned members can kindly help.

Thanking in Advance,
Regards,
Subhabrata. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread John Reid
On 18/02/13 14:53, Oscar Benjamin wrote:
 On 18 February 2013 14:23, John Reid j.r...@mail.cryst.bbk.ac.uk wrote:
 [snip]
 That said it would be nice to know the rationale for
 namedtuple.__new__ to have a different signature to tuple.__new__. I'm
 guessing namedtuple._make has a similar interface to tuple.__new__. Does
 anyone know what the rationale was for this design?
 
 Because namedtuples use names for the arguments in the constructor:
 
 from collections import namedtuple
 Point = namedtuple('Point', 'x y')
 p1 = Point(x=2, y=3)
 p1
 Point(x=2, y=3)
 p1.x
 2
 
That's a good point. I haven't used __new__ much before but wouldn't
something like this __new__() cater for both uses? (Example taken from
namedtuple docs
http://docs.python.org/2/library/collections.html#collections.namedtuple).

 Point = namedtuple('Point', ['x', 'y'], verbose=True)
class Point(tuple):
'Point(x, y)'

__slots__ = ()

_fields = ('x', 'y')

def __new__(_cls, *args, **kwds):
'Create a new instance of Point(x, y)'
if args:
return _tuple.__new__(_cls, args)
else:
return _tuple.__new__(_cls, (kwds[f] for f in _fields))

...


Perhaps I could subclass namedtuple so that my namedtuples have the
correct signature for __new__.
-- 
http://mail.python.org/mailman/listinfo/python-list


improving performance of writing into a pipe

2013-02-18 Thread mikprog
Hi guys,

on an embedded linux system (BeagleBoard) I am writing data coming from 
bluetooth dongle into a pipe.
The function is the following one:


def write_to_pipe(line):

# next line ensures that bytes like '0x09' are not translated into '\t' for 
#example, and they are sent as such
hexbytes = \\x + \\x.join([hex(ord(c))[2:].zfill(2) for c in line])
wrap = [echo -en ', '  /tmp/mypipe]
msg = hexbytes.join(wrap) 
print DBG: sending: , msg
 
try:
os.popen( msg )
except:
print Error: write_to_pipe has failed!


Now I typically receive 4 bytes from the bluetooth dongle and that is fine.
However when I receive many more than that it seems that the writing into the 
pipe is too slow.

Is there any clever/obvious way to improve the code above?
(I am quite sure there is to be honest).

Thanks for any suggestion!
Mik
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: improving performance of writing into a pipe

2013-02-18 Thread Oscar Benjamin
On 18 February 2013 15:12,  mikp...@gmail.com wrote:
 Hi guys,

 on an embedded linux system (BeagleBoard) I am writing data coming from 
 bluetooth dongle into a pipe.
 The function is the following one:


 def write_to_pipe(line):

 # next line ensures that bytes like '0x09' are not translated into '\t' 
 for
 #example, and they are sent as such
 hexbytes = \\x + \\x.join([hex(ord(c))[2:].zfill(2) for c in line])
 wrap = [echo -en ', '  /tmp/mypipe]
 msg = hexbytes.join(wrap)
 print DBG: sending: , msg

 try:
 os.popen( msg )
 except:
 print Error: write_to_pipe has failed!


 Now I typically receive 4 bytes from the bluetooth dongle and that is fine.
 However when I receive many more than that it seems that the writing into the 
 pipe is too slow.

 Is there any clever/obvious way to improve the code above?
 (I am quite sure there is to be honest).

Can you not open the pipe file directly in Python code? e.g.

fout = open('/tmp/mypipe', 'w')
fout.write(data)

I guess that this would be more efficient than using os.popen to run echo.


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


Re: news.gmane.org (was Re: Awsome Python - chained exceptions

2013-02-18 Thread Kwpolska
On Mon, Feb 18, 2013 at 7:30 AM, Rick Johnson
rantingrickjohn...@gmail.com wrote:
 Terry Reedy tjreedy at udel.edu writes:
 For at least the 10th time, there is little to no excuse for reading and
 writing python-list thru google-groups. The news.gmane.org mirror has
 multiple interfaces:

 [Sent from gmane.comp.python.general]

 Yes you have mentioned this before and for some reason i failed to follow your
 advice. I must have fallen into the trap of familiarity. In any event, if this
 message works i shall use gmane from now on. Thanks Terry!


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

Or something even better: use the Mailman mailing list,
http://mail.python.org/mailman/listinfo/python-list (mirrored to
Usenet).
--
Kwpolska http://kwpolska.tk | GPG KEY: 5EAAEA16
stop html mail| always bottom-post
http://asciiribbon.org| http://caliburn.nl/topposting.html
-- 
http://mail.python.org/mailman/listinfo/python-list


memory management

2013-02-18 Thread Sudheer Joseph
HI,
I have been trying to compute cross correlation between a time series 
at a location f(1) and the timeseries of spatial data f(XYT) and saving the 
resulting correlation coefficients and lags in a 3 dimensional array which is 
of fairly big size. Though the code I made for this purpose works up to few 
iterations then it hangs due to apparent memory crunch. Can anybody suggest a 
better way to handle this situation so that the computation and data storing 
can be done with out hangups. Finally I intend to save the data as netcdf file 
which is not implemented as of now. Below is the piece of code I wrote for this 
purpose.

from mpl_toolkits.basemap import Basemap as bm, shiftgrid, cm
import numpy as np
import matplotlib.pyplot as plt
from netCDF4 import Dataset
from math import pow, sqrt
import sys
from scipy.stats import t
indep=120
nlags=365
ncin = Dataset('qu_ru.nc', 'r')
lons = ncin.variables['LON421_600'][:]
lats = ncin.variables['LAT81_220'][:]
dep = ncin.variables['DEPTH1_29'][:]
adep=(dep==indep).nonzero()
didx=int(adep[0])
qu = ncin.variables['qu'][:,:,:]
#qv = ncin.variables['QV'][0,:,:]
ru = ncin.variables['ru'][:,didx,0,0]
ncin.close()
fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8])
# use major and minor sphere radii from WGS84 ellipsoid.
m = bm(projection='cyl', llcrnrlon=30, llcrnrlat=-40,urcrnrlon=120, 
urcrnrlat=30)
# transform to nx x ny regularly spaced 5km native projection grid
nx = int((m.xmax-m.xmin))+1; ny = int((m.ymax-m.ymin)+1)
q=ru[1:2190]
qmean=np.mean(q)
qstd=np.std(q)
qnorm=(q-qmean)/qstd
lags3d=np.arange(731*140*180).reshape(731,140,180)
r3d=np.arange(731*140*180).reshape(731,140,180)
for i in np.arange(len(lons)):
   for j in np.arange(len(lats)):
  print i,j
  p=qu[1:2190,j,i].squeeze()
  p.shape
  pmean=np.mean(p)
  pstd=np.std(p)
  pnorm=(p-pmean)/pstd
  n=len(p)
#  fg=plt.figure()
  c=plt.xcorr(p,q,usevlines=True,maxlags=nlags,normed=True,lw=2)
  acp=plt.acorr(p,usevlines=True,maxlags=nlags,normed=True,lw=2)
  acq=plt.acorr(q,usevlines=True,maxlags=nlags,normed=True,lw=2)
  acp[1][nlags]=0
  acq[1][nlags]=0
  lags=c[0]
  r=c[1]
  lags3d[:,j,i]=lags
  r3d[:,j,i]=r 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Multiple Plotting in Matplotlib

2013-02-18 Thread Nelle Varoquaux
 Dear Group,

 I am trying to view multiple plotting files in matplotlib. My numbers range 
 from 5 to few hundred. I was trying to use plt.subplot(), and plt.figure(n).
 But they did not work.
 plt.subplot() did not work at all.
 plt.figure(n) works till n=4. After that I am trying to get error messages.

Can you specify what did not work at all means and paste the error
messages and the code you are using ?

Thanks,
N


 If any one of the learned members can kindly help.

 Thanking in Advance,
 Regards,
 Subhabrata.
 --
 http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: improving performance of writing into a pipe

2013-02-18 Thread mikprog
On Monday, February 18, 2013 3:21:53 PM UTC, Oscar Benjamin wrote:
[..]
 
 Can you not open the pipe file directly in Python code? e.g.
 
 
 
 fout = open('/tmp/mypipe', 'w')
 
 fout.write(data)
 
 
 
 I guess that this would be more efficient than using os.popen to run echo.
 
 

that's an idea,
thanks Oscar.
However I get an exception while trying to open the queue:
fout = open('/tmp/mypipe', 'w')

I have tried it in a command line and the call doesn't return until in another 
terminal I open the same queue for reading (???)

I have created the queue with:
mkfifo /tmp/mypipe

any clue?
mik

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


Re: Multiple Plotting in Matplotlib

2013-02-18 Thread subhabangalore
On Monday, February 18, 2013 9:18:34 PM UTC+5:30, Nelle Varoquaux wrote:
  Dear Group,
 
 
 
  I am trying to view multiple plotting files in matplotlib. My numbers range 
  from 5 to few hundred. I was trying to use plt.subplot(), and plt.figure(n).
 
  But they did not work.
 
  plt.subplot() did not work at all.
 
  plt.figure(n) works till n=4. After that I am trying to get error messages.
 
 
 
 Can you specify what did not work at all means and paste the error
 
 messages and the code you are using ?
 
 
 
 Thanks,
 
 N
 
 
 
 
 
  If any one of the learned members can kindly help.
 
 
 
  Thanking in Advance,
 
  Regards,
 
  Subhabrata.
 
  --
 
  http://mail.python.org/mailman/listinfo/python-list

Thanks Nelle. It seems there was a minor coding mistake I was doing.

Regards,
Subhabrata.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: improving performance of writing into a pipe

2013-02-18 Thread Thomas Rachel

Am 18.02.2013 17:31 schrieb mikp...@gmail.com:


However I get an exception while trying to open the queue:
fout = open('/tmp/mypipe', 'w')


I don't see an exception in your answer. Where did you put it for us?



I have tried it in a command line and the call doesn't return until in another 
terminal I open the same queue for reading (???)


That's normal. An open call to a pipe blocks until someone reads from it.

But it's the same with your popen() version.


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


Re: improving performance of writing into a pipe

2013-02-18 Thread mikprog
[..]
 
 I don't see an exception in your answer. Where did you put it for us?
 

well I just did print a message:

PIPEPATH = [/tmp/mypipe]

[..]
try:
self.process = os.popen( self.PIPEPATH, 'w')
except:
print Error while trying opening the pipe!
print check: , self.PIPEPATH
exit()

I see the error messages.

It's quite frustrating as I think I am doing something really stupid in here.

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


Re: news.gmane.org (was Re: Awsome Python - chained exceptions

2013-02-18 Thread rurpy
On 02/17/2013 11:10 PM, Terry Reedy wrote:
 On 2/18/2013 12:51 AM, Rick Johnson wrote:
   if you (or anyone else) would be kind enough to recommend an
   alternative to this gawd awful software [google groups],
 ?  i'm all ears. My expectations at minimum are:
 
 For at least the 10th time, there is little to no excuse for reading and 
 writing python-list thru google-groups. The news.gmane.org mirror has 
 multiple interfaces:

And for at least the 11th time, you are wrong.  There are reasons
(not applicable to everyone but applicable to many) for using
Google Groups, among others it is more accessible and easier to 
use for many than a news reader with Gmane.

That you don't like aspects of the posts produced by GG is fine
(I don't either) but it does not justify posting BS claims -- if 
you want people to use Gmane because *you* don't like reading GG
posts then say so but don't claim that doing so is just as easy 
as GG -- it is not.

There are ways of mitigating some of the worst characteristics of
GG posts, see 

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



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


Python Warts: The where, when, how, and why of a PyWart.

2013-02-18 Thread Rick Johnson
  
  NOTE:
  
  This thread is actually an extension (of sorts) to a thread
  started by Anatoly tecktonik back in December 2012; posted on
  the python-ideas mailing list; titled: Documenting Python
  warts on Stack Overflow.

  I don't feel that python-ideas was the proper location to
  discuss this issue (and neither did the BDFL) so i am moving it
  over here for extended discussion. This message started out as
  a private email to anatoly, however, i decided the entire
  community needs to discuss this topic so i am posting it
  verbatim.
  

Anatoly, I've seen your posts on python-ideas regarding this issue and wanted to
inquire as to your progress so far. Also, you should not be surprised that many
in the Python community do not want a searchable list of warts available; which
is evident by their knee-jerk diversionary tactics of fighting over the
semantics of:

  What is a wart? - blah blah blah
  What is not a wart? - blah blah blah

What they fail to understand is that such a list will actually /improve/ the
language. 

But let's not fool ourselves, of /course/ the word wart is subjective! Think
of a mother who gives birth to the worlds wartiest baby; she will immediately
ignore the physical warts and only see the beauty within. That's a necessary
coping mechanism hard-wired into the brain of all mothers, it is vital that a
mother raise her offspring regardless of the babies physical appearance.

But for software developers it's nothing more than hiding your head in the sand.
Sure, we cannot allow ourselves to wallow in minutiae, however, we also MUST NOT
allow ourselves to ignore the brittle foundation that supports this mammoth
structure we call Python; lest it come crashing down around us!

Like you, i understand that we cannot improve a language until we understand the
flaws of the language. Documenting Python Warts are a step in the correct
direction. But before we can start documenting warts we need to find a proper
location for them, because if we don't find a highly visible location for them,
then we will most likely just waste our time.

For some time i have documenting the warts on python-list using the pre-fix:
PyWart: {Description Here}. And, to be fair, i must admit that my execution of
the warts are not as professionally structured as a PEP. However, I'm not
convinced that they /need/ to be as structured as a PEP! To much time worrying
about PyWart structure distracts from the wart we are intending to cut off! We
are not writing feature request -- which justify such structured and detailed
explanations, no, we are merely exposing the weaknesses that inject
multiplicity, inconsistency, and asininity into our minds WHILST we write code,
therefore, /distracting/ us from the problem at hand!

The point of PyWarts is:

  To document the warts of the language whilst
  simultaneously allowing community members to post visible
  replies, rejections, or support.

The only place better than posting on python-list would be to create *another*
list called python-warts -- however, i am not a fan of multiplicity, and i feel
/another/ list would just be too confusing. That is why i employ the PyWarts:
title prefix.


 Reasons for posting warts on python-list:


1. python-list will most likely exist as long as python does, or longer. No need
to worry about external sites becoming extinct (f.e. Stack Overflow).

2. python-list is visible to all members of the community: from the BDFL down to
the newest of noob (even though we are unlikely to get any input from GvR
outside of his comfort zone over at python-ideas and py-dev!).

3. A public mailing list allows /anyone/ to post support or rejections of the
wart, thereby validating or invalidating the wart.

4. By engaging in public conversation on these issues, we are maintaining a real
sense of community between all members; regardless of any pecking orders.

5. But most importantly: Anyone can boast about Python's TIOBE score, or
whatever positive attribute of the language they like, but it takes real guts
(and insight) to admit to the warts. 

You cannot improve any system by failing to recognize the negative traits
inherent in that system!

PS: The CAPTCHA is rectal; coincidence?


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


Re: improving performance of writing into a pipe

2013-02-18 Thread Michael Torrie
On 02/18/2013 10:00 AM, mikp...@gmail.com wrote:
 [..]

 I don't see an exception in your answer. Where did you put it for us?

 
 well I just did print a message:
 
 PIPEPATH = [/tmp/mypipe]
 
 [..]
 try:
 self.process = os.popen( self.PIPEPATH, 'w')
 except:
 print Error while trying opening the pipe!
 print check: , self.PIPEPATH
 exit()
 
 I see the error messages.

Unfortunately your attempt to catch this exception is hiding the true
cause.  You need to give us the actual exception.  Otherwise it could be
anything from self.PIPEPATH not existing to who knows what.

Almost never do you want to catch all exceptions like you're doing.  You
should only catch the specific exceptions you know how to deal with in
your code.

For testing purposes, if your code really is as you put it, then
catching exceptions is kind of silly since you're just re-raising the
exception (sort of) but without any contextual information that would
make the error meaningful.

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


Re: news.gmane.org (was Re: Awsome Python - chained exceptions

2013-02-18 Thread Rick Johnson
 rurpy at yahoo.com writes:
 On 02/17/2013 11:10 PM, Terry Reedy wrote:
  For at least the 10th time [...]
 
 And for at least the 11th time, you are wrong.  There are reasons
 (not applicable to everyone but applicable to many) for using
 Google Groups, among others it is more accessible and easier to 
 use for many than a news reader with Gmane.

I will admit that GG's is easier in this respect. 

However, you /can/ read gmane /without/ a newsreader using the frames and
threads  or flat (blog-like) web-interface. Although, there are a few issues
that are annoying me:

1. When viewing in the flat interface style, the text of the messages is so
small i need to squint whilst reading. Of course i can zoom my web browser,
however, then i get a horizontal scroll bar and some of the post text is
unreachable without scrolling (I really hate horizontal scroll bars!). Not to
mention that i will need to adjust the zoom level back to normal when leaving
the site. 

2. When positing a new message i must enter my email address and username each
time. The forms are auto-filled for replys but not for new messages. Go figure!

3. There is no method to sort the topics by either: last reply first or date
of thread composition. This is probably more suited to a personal newsreader
though.

4. (In the blog style interface) the menu of threads uses a font with
insufficient vertical spacing and everything becomes so jammed together that it
is completely unreadable. I will try to change my browsers' font and see if that
solves the issue; although i am quite fond of my current settings!

 There are ways of mitigating some of the worst characteristics of
 GG posts, see 
 
   http://wiki.python.org/moin/GoogleGroupsPython

Thanks for this link!



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


Python 3.3 vs. MSDOS Basic

2013-02-18 Thread John Immarino
I coded a Python solution for Problem #14 on the Project Euler website. I was 
very surprised to find that it took 107 sec. to run even though it's a pretty 
simple program.  I also coded an equivalent solution for the problem in the old 
MSDOS basic. (That's the 16 bit app of 1980s vintage.)  It ran in 56 sec. Is 
there a flaw in my coding, or is Python really this slow in this particular 
application. MSDOS Basic usually runs at a snails pace compared to Python.

Below is the problem and the code:



The following iterative sequence is defined for the set of positive integers:

n → n/2 (n is even)
n → 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence:
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1

It can be seen that this sequence (starting at 13 and finishing at 1) contains 
10 terms. Although it has not been proved yet (Collatz Problem), it is thought 
that all starting numbers finish at 1.

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one million.


max=0
m=0
while m=100:
m+=1
count=0
n=m
while n!=1:
count+=1
if n%2==0:
n=n//2
else:
n=3*n+1
if countmax:
 max=count
 num=m
print(num,max)



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


Re: improving performance of writing into a pipe

2013-02-18 Thread Serhiy Storchaka

On 18.02.13 17:12, mikp...@gmail.com wrote:

on an embedded linux system (BeagleBoard) I am writing data coming from 
bluetooth dongle into a pipe.
The function is the following one:


def write_to_pipe(line):

 # next line ensures that bytes like '0x09' are not translated into '\t' for
 #example, and they are sent as such
 hexbytes = \\x + \\x.join([hex(ord(c))[2:].zfill(2) for c in line])
 wrap = [echo -en ', '  /tmp/mypipe]
 msg = hexbytes.join(wrap)
 print DBG: sending: , msg

 try:
 os.popen( msg )
 except:
 print Error: write_to_pipe has failed!


Now I typically receive 4 bytes from the bluetooth dongle and that is fine.
However when I receive many more than that it seems that the writing into the 
pipe is too slow.

Is there any clever/obvious way to improve the code above?
(I am quite sure there is to be honest).


def write_to_pipe(line):
hexbytes = ''.join('\\x%02x' % ord(c) for c in line)
with open('/tmp/mypipe', 'w') as f:
f.write(hexbytes)


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


request for help

2013-02-18 Thread leonardo selmi
pls i need help:

i have copied the following from a book and tried to make it work:

import math

def area(radius):
return math.pi * radius**2

def circumference(radius):
return 2 * math.pi * radius

i saved the above program from python shell into a file as circle.py . when i 
type import circle i get  error..


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


Instances as dictionary key, __hash__ and __eq__

2013-02-18 Thread Jean-Michel Pichavant
Greetings,

I opened something like a month ago a thread about hash functions and how I 
could write classes which instances can be safely used as dictionary keys.
I though I had it but when I read back my code, I think I wrote yet another bug.

Consider the following simple (buggy) class, python 2.5

class FooSet(object):
Define an algorithm set, containing pdcch/pdsch (or none).
def __init__(self, pdcch, pdsch):
self.pdcch = bool(pdcch)
self.pdsch = bool(pdsch)
# __hash__ and __eq__ allow to use the object as a dictionary key
def __hash__(self):
return hash((self.pdcch, self.pdsch))
def __eq__(self, other):
return hash(self) == hash(other)

Can you confirm that using the hash function for testing equality is a very bad 
idea ?

One obvious solution would be:

def __eq__(self, other):
return self.pdsch = other.pdsch and self.pdcch == other.pdcch

But I was looking for a standard solution, that I could use for basically all 
my container classes

So I came up with these ones:

def __hash__(self):
return hash(tuple(vars(self).values()))   
def __eq__(self, other):
return vars(self) == vars(other)

But I'm not sure about vars(self).values(), I don't really care about the order 
of the values, but I need to be sure that for 2 equal dictionaries, they will 
both return their values in the same order.
And that's the point, I'm not sure at all.

Additionally,  If I'm making things much more complicated than they need to be, 
let me know.

Cheers,

JM


-- IMPORTANT NOTICE: 

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


Re: request for help

2013-02-18 Thread Matt Jones
Is this exactly how it shows in your shell?  If so, it seems you need to
indent your methods.

#
import math

def area(radius):
return math.pi * radius**2

def circumference(radius):
return 2 * math.pi * radius
#

*Matt Jones*


On Mon, Feb 18, 2013 at 1:42 PM, leonardo selmi l.se...@icloud.com wrote:

 pls i need help:

 i have copied the following from a book and tried to make it work:

 import math

 def area(radius):
 return math.pi * radius**2

 def circumference(radius):
 return 2 * math.pi * radius

 i saved the above program from python shell into a file as circle.py .
 when i type import circle i get  error..


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

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


Re: request for help

2013-02-18 Thread Rick Johnson
leonardo selmi l.selmi at icloud.com writes:
 [...]
 i saved the above program from python shell into a file as
 circle.py . when i type import circle i get  error..

Urm... would you be so kind as to copy and paste the error message verbatim? You
have obvious syntax errors in this code due to improper indentation, but take a
word of advice from the Python Zen:

 In the face of ambiguity, refuse the temptation to guess

So let's have a look-see at that error message, shall we?

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


Re: request for help

2013-02-18 Thread Gary Herron

On 02/18/2013 11:42 AM, leonardo selmi wrote:

pls i need help:

i have copied the following from a book and tried to make it work:

import math

def area(radius):
return math.pi * radius**2

def circumference(radius):
return 2 * math.pi * radius

i saved the above program from python shell into a file as circle.py . when i type 
import circle i get  error..


kind regards


First, you shouldn't ask us to help you fix an error without telling us 
what the error is!   You should also tell us what version of Python and 
what system (Windows? Linux? ...) , and anything more that can help us 
understand what you did and what failed.



So this is just guesswork:
Spaces are important in Python.  The body of a function **must** be 
indented.  If you do have the indents in your code, and they were just 
lost in the process of cutting and pasting and emailing, the we really 
do need more information.


def area(radius):
return math.pi * radius**2

def circumference(radius):
return 2 * math.pi * radius

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


Re: request for help

2013-02-18 Thread Stefan Holdermans
Leonardi,

 i saved the above program from python shell into a file as circle.py . when 
 i type import circle i get  error..


Next time, please mention what kind of error you're getting.

Was it an indentation error? Because, as you pasted it, your code would lead to 
one.

If I fix the indentation, as in

  import math

  def area(radius):
return math.pi * radius**2

  def circumference(radius):
return 2 * math.pi * radius

it works fine for me.

HTH,

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


Re: request for help

2013-02-18 Thread leonardo

thanks guys and sorry for my incomplete datas, here is the error message:

Traceback (most recent call last):
 File pyshell#0, line 1, in module
   import circle
 File circle.py, line 1
   Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43)
^
SyntaxError: invalid syntax


thanks for any help!






Il giorno 18/feb/2013, alle ore 20:59, Stefan Holdermans 
ste...@vectorfabrics.com ha scritto:

 Leonardi,
 
 i saved the above program from python shell into a file as circle.py . 
 when i type import circle i get  error..
 
 
 Next time, please mention what kind of error you're getting.
 
 Was it an indentation error? Because, as you pasted it, your code would lead 
 to one.
 
 If I fix the indentation, as in
 
 import math
 
 def area(radius):
   return math.pi * radius**2
 
 def circumference(radius):
   return 2 * math.pi * radius
 
 it works fine for me.
 
 HTH,
 
 Stefan
 -- 
 http://mail.python.org/mailman/listinfo/python-list

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


Re: request for help

2013-02-18 Thread Gary Herron

On 02/18/2013 12:14 PM, leonardo wrote:

thanks guys and sorry for my incomplete datas, here is the error message:

Traceback (most recent call last):
  File pyshell#0, line 1, in module
import circle
  File circle.py, line 1
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43)
 ^
SyntaxError: invalid syntax


If I read this error message correctly,  I don't think the contents of 
circle.py are at all what you claim.  It looks like the first line of 
circle.py contains the text Python 2.7.3 ... which is certainly not 
what you intended or claimed. Please examine the contents of circle.py 
very carefully.






thanks for any help!






Il giorno 18/feb/2013, alle ore 20:59, Stefan Holdermans 
ste...@vectorfabrics.com ha scritto:


Leonardi,


i saved the above program from python shell into a file as circle.py . when i type 
import circle i get  error..


Next time, please mention what kind of error you're getting.

Was it an indentation error? Because, as you pasted it, your code would lead to 
one.

If I fix the indentation, as in

import math

def area(radius):
   return math.pi * radius**2

def circumference(radius):
   return 2 * math.pi * radius

it works fine for me.

HTH,

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


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


Re: request for help

2013-02-18 Thread Rick Johnson
leonardo tampucciolina at libero.it writes:
 here is the error message:
 [...]

Okay, now we are on the road to solving this problem.

But first we need to take a slight detour and learn about python packaging,
because no matter what the current error is, naming a module circle and then
throwing it naked out into the Python module wilderness is complete folly; i
can assure you!


 What is a Python package?


A python package is simply another layer of namespace that protects our symbols
from clashing; in this case: module identifiers.

Even noobs understand that function bodies and class bodies (bka: object
definitions) protect code from outside influences, and that modules protect the
symbols contained in one module from the symbols contained in /other/ modules,
however, we still must protect module identifiers somehow. How do we do this?
Packages to the rescue!

Your circle.py module needs to be under the protective care of a specialized
package named geom2d , which itself should be under the care of a specialized
package named math, which itself should be under the global blanket of a
personal library package named insertUniqueNameHere! This is how we protect 
module symbols whilst simultaneously employing a logical structure in our code.

 Impatient Ivan exclaimed: So how the heck do i create the package Rick?


Steps to create the entire package hierarchy:


Note: All package and modules names should be lowercase!

1a. Create a folder named mylib on the Python search path.
1b. Convert the mylib folder into a package by adding a file named:
__init__.py This will be your personal package for containing your personal
modules.

2a. Inside the mylib folder create another folder called math
2b. Convert the mylib\math folder to a package by adding a file named
__init__.py.

3a. Inside the mylib\math folder create another folder named geom2d
3b. Convert the mylib\math\geom2d folder to a package by adding a file named
__init__.py.

4. Inside the mylib\math\geom2d folder create a new file named circlelib.py.
This is where you will place the code for computing circle data. Later you will
probably write something more useful, but for now this module is the best you 
have.

Now, you'll need to import the circlelib for use and this is how you do it:

## START CODE ##
from mylib.math.geom2d.circlelib import area, circumference
area(blah)
circumference(blah)
## END CODE ##

From now on, if you create any more modules that deal with maths (or a subset 
of
math: geom) you have a place to store them intelligently. There is quite a bit
more to Python packages but what i describe above is the most fundamental 
aspect.


 Back to your exception


Did correcting the indentation fix the problem? If not, what is the next error
you get?

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


Re: Instances as dictionary key, __hash__ and __eq__

2013-02-18 Thread Tim Delaney
On 19 February 2013 06:51, Jean-Michel Pichavant jeanmic...@sequans.comwrote:

 Greetings,

 I opened something like a month ago a thread about hash functions and how
 I could write classes which instances can be safely used as dictionary keys.
 I though I had it but when I read back my code, I think I wrote yet
 another bug.

 Consider the following simple (buggy) class, python 2.5

 class FooSet(object):
 Define an algorithm set, containing pdcch/pdsch (or none).
 def __init__(self, pdcch, pdsch):
 self.pdcch = bool(pdcch)
 self.pdsch = bool(pdsch)
 # __hash__ and __eq__ allow to use the object as a dictionary key
 def __hash__(self):
 return hash((self.pdcch, self.pdsch))
 def __eq__(self, other):
 return hash(self) == hash(other)

 Can you confirm that using the hash function for testing equality is a
 very bad idea ?


Yes - it is a *very* bad idea. A hash by definition can produce collisions,
since you are taking much larger amount of data and are trying to represent
it in a smaller amount of space. It's effectively lossy compression - you
can never reliably get the original back.


 One obvious solution would be:

 def __eq__(self, other):
 return self.pdsch = other.pdsch and self.pdcch == other.pdcch


This is a correct and the simplest way to do it.


 But I was looking for a standard solution, that I could use for
 basically all my container classes

 So I came up with these ones:

 def __hash__(self):
 return hash(tuple(vars(self).values()))
 def __eq__(self, other):
 return vars(self) == vars(other)

 But I'm not sure about vars(self).values(), I don't really care about the
 order of the values, but I need to be sure that for 2 equal dictionaries,
 they will both return their values in the same order.
 And that's the point, I'm not sure at all.


You cannot rely on this. Dictionaries are unordered, and the order that
items are added affects the order that the elements will be iterated over.
You could sort the vars by name (thus giving the stable order you need) but
there's another flaw - vars() contains more than just the attributes you
set.

 class A():
... pass
...
 vars(A)
mappingproxy({'__qualname__': 'A', '__dict__': attribute '__dict__' of 'A'
objects, '__module__':
'__main__', '__weakref__': attribute '__weakref__' of 'A' objects,
'__doc__': None})

So by using vars you are preventing instances of subclasses of your class
from comparing equal to each other (or to instances of the base class).

Additionally,  If I'm making things much more complicated than they need to
 be, let me know.


You are. There are ways to achieve what you want, but it requires a lot
more setup and discipline. The simplest way is probably to have a
_equal_fields() method that subclasses override, returning a tuple of the
attributes that should be hashed. Then in __hash__() and __eq__ you iterate
over the returned tuple, get the value for each attribute and either hash
or compare.

Of course, you have to take into account in __eq__ that the other instance
may not have the same attributes (e.g. self is a subclass that uses extra
attributes in its __hash__ and __eq__).

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


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Terry Reedy

On 2/18/2013 6:47 AM, John Reid wrote:


I was hoping namedtuples could be used as replacements for tuples

  in all instances.

This is a mistake in the following two senses. First, tuple is a class 
with instances while namedtuple is a class factory that produces 
classes. (One could think of namedtuple as a metaclass, but it was not 
implemented that way.) Second, a tuple instance can have any length and 
different instances can have different lengths. On the other hand, all 
instances of a particular namedtuple class have a fixed length. This 
affects their initialization. So does the fact that Oscar mentioned, 
that fields can be initialized by name.


 There seem to be some differences between how tuples and namedtuples
 are created. For example with a tuple I can do:


a=tuple([1,2,3])


But no sensible person would ever do that, since it creates an 
unnecessary list and is equivalent to


a = 1,2,3

The actual api is tuple(iterable). I presume you know that, but it gets 
to the question you ask about 'why the difference?'. The only reason to 
use an explicit tuple() call is when you already have an iterable, 
possibly of unknown length, rather than the individual field objects. In 
the latter case, one should use a display.



with namedtuples I get a TypeError:

from collections import namedtuple
B=namedtuple('B', 'x y z')
b=B([1,2,3])


There is no namedtuple B display, so one *must* use an explicit call 
with the proper number of args. The simplest possibility is B(val0, 
val1, val2). Initializaing a namedtuple from an iterable is unusual, and 
hence gets the longer syntax. In other words, the typical use case for a 
namedtuple class is to replace statements that have tuple display.


return a, b, c
to
return B(a, b, c)

or
x = a, b, c
to
x = B(a, b, c)

It is much less common to change tuple(iterable) to B(iterable).


def canSequence(obj):
 if isinstance(obj, (list, tuple)):
 t = type(obj)
 return t([can(i) for i in obj])
 else:
 return obj


The first return could also be written t(map(can, obj)) or, in Python 3,
t(can(i) for i in obj).


where obj is a namedtuple and t([can(i) for i in obj]) fails with the 
TypeError. See http://article.gmane.org/gmane.comp.python.ipython.user/10270 
for more info.

Is this a problem with namedtuples, ipython or just a feature?


With canSequence. If isinstance was available and the above were written 
before list and tuple could be subclassed, canSequence was sensible when 
written. But as Oscar said, it is now a mistake for canSequence to 
assume that all subclasses of list and tuple have the same 
initialization api.


In fact, one reason to subclass a class is to change the initialization 
api. For instance, before python 3, range was a function that returned a 
list. If lists had always been able to be subclasses, it might instead 
have been written as a list subclass that attached the start, stop, and 
step values, like so:


# python 3
class rangelist(list):
 def __init__(self, *args):
  r = range(*args)
  self.extend(r)
  self.start = r.start
  self.stop = r.stop
  self.step = r.step

r10 = rangelist(10)
print(r10, r10.start, r10.stop, r10.step)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 0 10 1

However, define can() and canSequence(r10) will raise a TypeError, just 
as with a namedtuple B instance.


TypeError: 'list' object cannot be interpreted as an integer

So, while your question about the namedtuple api is a legitimate one, 
your problem with canSequence is not really about namedtuples, but about 
canSequence making a bad assumption.


--
Terry Jan Reedy

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


Re: Python 3.3 vs. MSDOS Basic

2013-02-18 Thread Ian Kelly
On Mon, Feb 18, 2013 at 12:13 PM, John Immarino joh...@gmail.com wrote:
 I coded a Python solution for Problem #14 on the Project Euler website. I was 
 very surprised to find that it took 107 sec. to run even though it's a pretty 
 simple program.  I also coded an equivalent solution for the problem in the 
 old MSDOS basic. (That's the 16 bit app of 1980s vintage.)  It ran in 56 sec. 
 Is there a flaw in my coding, or is Python really this slow in this 
 particular application. MSDOS Basic usually runs at a snails pace compared to 
 Python.

Well, I don't see anything that looks especially slow in that code,
but the algorithm that you're using is not very efficient.  I rewrote
it using dynamic programming (details left as an exercise), which got
the runtime down to about 4 seconds.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3.3 vs. MSDOS Basic

2013-02-18 Thread Chris Angelico
On Tue, Feb 19, 2013 at 6:13 AM, John Immarino joh...@gmail.com wrote:
 I coded a Python solution for Problem #14 on the Project Euler website. I was 
 very surprised to find that it took 107 sec. to run even though it's a pretty 
 simple program.  I also coded an equivalent solution for the problem in the 
 old MSDOS basic. (That's the 16 bit app of 1980s vintage.)  It ran in 56 sec. 
 Is there a flaw in my coding, or is Python really this slow in this 
 particular application. MSDOS Basic usually runs at a snails pace compared to 
 Python.

BASIC does a lot less. If you wrote an 8086 assembly language
interpreter in Python, it'd run fairly slowly too :) Python isn't
really the world's best language for number crunching inside a machine
word; though if this were a major project, I would recommend looking
into Cython, as it lets you translate a few critical portions of your
code to C while leaving the rest in Python.

In order to get some useful stats, I added a little timing code to
your original; on my Windows XP laptop, running Python 3.3, your
version took 212.64 seconds to get to a result (namely, 837799 with a
count of 524).

Here's how I'd code it:

import time
start=time.time()
max=0
for m in range(1,101):
n=m
count=0
while n1:
if n%2: n=3*n+1
else: n//=2
count+=1
if countmax: max,num=count,m
if not m16383: print(-,m,count)
print(num,max)
print(time.time()-start)

(You'll see the same timing information that I added to yours. It adds
immeasurably to the run-time, and gives some early idea of how it's
going.)

Running under Python 2.6, both your version and mine take about 90
seconds to run. But under Python 3.3, where (among other things)
range() yields values lazily, my version is significantly faster than
yours. BUT! Both versions, under 3.3, are significantly *slower* than
under 2.6. My first thought is that it's because Py2 has different
types for 'int' and 'long', and Py3 doesn't (effectively, everything's
a long), so I added an L suffix to every number and ran each of them
under 2.6 again. Seems that was the bulk of the difference, though not
all.

Pythonistas, does this count as a regression, or is Python
sufficiently not a number crunching language that we don't care?

(range = my code, as above; while = original version with a C-style
loop counter)
range py3: 171.07846403121948
while py3: 212.64104509353638
range py2: 87.859000206
while py2: 86.4059998989
range py2 longs: 190.530999899
while py2 longs: 176.12528

For comparison purposes, I also coded up the equivalent in Pike.
Pike's a very similar language to Python, but with a C-like syntax,
and certain optimizations - including, significantly to this exercise,
an integer type that sits within a machine word if it can (though
it'll happily go arbitrary precision when it's needed to). It pretends
to the programmer that it's a Py3-style everything's an int, but
underneath, functions more like Py2 with separate short and long
types. The result: 22.649 seconds to reach the same conclusion.

How long did your BASIC version take, and how long did the Python
version on the same hardware?

This sort of pure number crunching isn't really where a modern high
level language shines. You'll come to *really* appreciate Python as
soon as you start working with huge arrays, dictionaries, etc. This is
a job for C, really.

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


Re: Python 3.3 vs. MSDOS Basic

2013-02-18 Thread Chris Angelico
On Tue, Feb 19, 2013 at 8:56 AM, Chris Angelico ros...@gmail.com wrote:
 On Tue, Feb 19, 2013 at 8:55 AM, Chris Angelico ros...@gmail.com wrote:
 How long did your BASIC version take, and how long did the Python
 version on the same hardware?

 Oops, my bad, you already posted the figures :) And I forgot to ask:
 Which Python version didyou use?

 ChrisA

Doh. I'm having a great day of not reading properly, today. (I blame
checking mail on the bus, it took me over an hour to read this one
message and I'd forgotten the subject line by the time I got to the
end.) Python 3.3, right there in the header. Disregard me!

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


Re: Python 3.3 vs. MSDOS Basic

2013-02-18 Thread Chris Angelico
On Tue, Feb 19, 2013 at 8:55 AM, Chris Angelico ros...@gmail.com wrote:
 How long did your BASIC version take, and how long did the Python
 version on the same hardware?

Oops, my bad, you already posted the figures :) And I forgot to ask:
Which Python version didyou use?

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


Re: Python 3.3 vs. MSDOS Basic

2013-02-18 Thread Chris Angelico
On Tue, Feb 19, 2013 at 8:54 AM, Ian Kelly ian.g.ke...@gmail.com wrote:
 Well, I don't see anything that looks especially slow in that code,
 but the algorithm that you're using is not very efficient.  I rewrote
 it using dynamic programming (details left as an exercise), which got
 the runtime down to about 4 seconds.

Did it involve a dictionary, mapping a value to its count, so that any
time you hit a value you've seen, you can short-cut it? That was my
first optimization consideration, though I didn't implement it in any
version, so as to keep the timings comparable.

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


Re: Python 3.3 vs. MSDOS Basic

2013-02-18 Thread Ian Kelly
On Mon, Feb 18, 2013 at 3:01 PM, Chris Angelico ros...@gmail.com wrote:
 On Tue, Feb 19, 2013 at 8:54 AM, Ian Kelly ian.g.ke...@gmail.com wrote:
 Well, I don't see anything that looks especially slow in that code,
 but the algorithm that you're using is not very efficient.  I rewrote
 it using dynamic programming (details left as an exercise), which got
 the runtime down to about 4 seconds.

 Did it involve a dictionary, mapping a value to its count, so that any
 time you hit a value you've seen, you can short-cut it? That was my
 first optimization consideration, though I didn't implement it in any
 version, so as to keep the timings comparable.

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


Re: Python Warts: The where, when, how, and why of a PyWart.

2013-02-18 Thread Terry Reedy

On 2/18/2013 1:04 PM, Rick Johnson wrote:


   This thread is actually an extension (of sorts) to a thread
   started by Anatoly tecktonik back in December 2012; posted on
   the python-ideas mailing list; titled: Documenting Python
   warts on Stack Overflow.


This was a threat to abuse StackOverflow with off-topic posts if 'we' 
did not pay him more attention.



   I don't feel that python-ideas was the proper location to
   discuss this issue (and neither did the BDFL)


Correct. I said so first, though Guido saying so later had more effect ;=(.
http://mail.python.org/pipermail/python-ideas/2012-December/018414.html
I also said that the post was obnoxious and best ignored. You dirty 
yourself by associating yourself with that post.


I will just briefly repeat two points:
1. we already have a searchable list of Python problems -- the tracker; 
2. everything that Anatoly complained about has already been discussed, 
sometimes ad nauseum.


His real complaint is that he tends not accept and respect that other 
reasonable and intelligent people disagree with him and that we develop 
Python for all users, not just him.



Anatoly, I've seen your posts on python-ideas regarding this issue

 and wanted to inquire as to your progress so far.

Anatoly has made progress in restraining his most obnoxious behaviors, 
such as repeatedly re-opening closed tracker issues, and otherwise 
trying to divert developer attention from issues we can and will fix* to 
his pet dead issues. You would do him a disservice if you encouraged him 
to regress in that respect.


However, moving his diatribes to python-list from the tracker, pydev, 
and python-ideas *would* be progress, and I hope you succeed in 
encouraging him to do that. I have so far failed.


*The mostly volunteer developers have closed an average of perhaps 5 
issues a day for the last 2 years that I have been watching. Quite 
amazing. A few are rejections of proposed enhancements or behavior 
changes, but most are fixes and improvements.


 Also, you should not be surprised that many

in the Python community do not want a searchable list of warts available;


Such a lie. Issues on the tracker are *not* removed. Neither are posts 
on the mailing lists.


Rick, it is one thing to say you don't like some feature of Python -- I 
am sure you don't -- and wish it to be changed -- I am sure you do. It 
is really quite another to lie about people who volunteer their efforts 
to improve Python. The more you do this, the most you encourage 
developers to ignore python-list.



But let's not fool ourselves, of /course/ the word wart is subjective!


As someone who has suffered from aggressive, objectively real warts for 
at least 2 decades and probably will for the rest of my life, I do not 
find the bad metaphor very amusing. I wish people would stop using it.


--
Terry Jan Reedy

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


Re: news.gmane.org (was Re: Awsome Python - chained exceptions

2013-02-18 Thread Terry Reedy

On 2/18/2013 1:32 PM, Rick Johnson wrote:


2. When positing a new message i must enter my email address and username each
time. The forms are auto-filled for replys but not for new messages. Go figure!


Using the newsreader interface, I get 1 email message per list to verify 
the email address. After that, it is as if I were subscribed. (Some 
mirrored email lists require a subscription at their site, but most 
python.org lists seems not to.) And, of course, Thunderbird fills in 
data for both new messages and replies. I do not know why it would be 
different through the web interface.



3. There is no method to sort the topics by either: last reply first or date
of thread composition. This is probably more suited to a personal newsreader
though.


The monthly archives, which include the current month,
can be accessed by thread, subject, author, or date.
http://mail.python.org/pipermail/python-list/

--
Terry Jan Reedy

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


Re: Instances as dictionary key, __hash__ and __eq__

2013-02-18 Thread Terry Reedy

On 2/18/2013 2:51 PM, Jean-Michel Pichavant wrote:

Greetings,

I opened something like a month ago a thread about hash functions and how I 
could write classes which instances can be safely used as dictionary keys.
I though I had it but when I read back my code, I think I wrote yet another bug.

Consider the following simple (buggy) class, python 2.5

class FooSet(object):
 Define an algorithm set, containing pdcch/pdsch (or none).
 def __init__(self, pdcch, pdsch):
 self.pdcch = bool(pdcch)
 self.pdsch = bool(pdsch)
 # __hash__ and __eq__ allow to use the object as a dictionary key
 def __hash__(self):
 return hash((self.pdcch, self.pdsch))
 def __eq__(self, other):
 return hash(self) == hash(other)

Can you confirm that using the hash function for testing equality is a very bad 
idea ?

One obvious solution would be:

def __eq__(self, other):
 return self.pdsch = other.pdsch and self.pdcch == other.pdcch

But I was looking for a standard solution, that I could use for basically all 
my container classes

So I came up with these ones:

def __hash__(self):
 return hash(tuple(vars(self).values()))
def __eq__(self, other):
 return vars(self) == vars(other)

But I'm not sure about vars(self).values(), I don't really care about the order 
of the values, but I need to be sure that for 2 equal dictionaries, they will 
both return their values in the same order.


No, you cannot depend on that in general even though it may work in 
specific cases.



And that's the point, I'm not sure at all.

Additionally,  If I'm making things much more complicated than they need to be, 
let me know.

Cheers,

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.




--
Terry Jan Reedy

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


Re: Python trademark - A request for civility

2013-02-18 Thread Gregory Ewing

Jean-Michel Pichavant wrote:


They've been launching at us rantingrick attacks for a long time,


Would such an attack be called a rantingrickroll?

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


Re: request for help

2013-02-18 Thread Terry Reedy

On 2/18/2013 2:42 PM, leonardo selmi wrote:


i saved the above program from python shell into a file as circle.py .


Which copied too much into the file.
Edit circle.py until it is a proper python program.

My initial guess was that you copied the  prompts, but you later 
message shows that it was the welcome header. Header and prompts all 
need to go. Idle makes it easier to cut and paste a single statement 
from its shell to an editor window and then save. Or start your code in 
an editor, such as IDLE's.


--
Terry Jan Reedy

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


Re: Python Warts: The where, when, how, and why of a PyWart.

2013-02-18 Thread Rick Johnson
On Monday, February 18, 2013 4:31:01 PM UTC-6, Terry Reedy wrote:
 This was a threat to abuse StackOverflow with off-topic posts if 'we' 
 did not pay him more attention.

If that is in-fact true then i am going to be as upset with Anatoly as you seem 
to be. Pointing out problems on appropriate list is fine, but making spam 
threats is totally wrong.

 [...]
 I will just briefly repeat two points:
 1. we already have a searchable list of Python problems -- the tracker; 

But i don't believe a Bug Tracker and a Language Wart listing are the same. 
My belief is that a Tracker is for posting patches for bugs; meaning either 
the kind that throw errors:

[Warning: Naively hypothetical examples ahead!]

py 1+2 
Exception: Seg Fault!

...or the kind that produce illogical results:

py 1+2
12

I also believe a Bug Tracker is a serious place where stict etiquette and 
posting rules should be maintained:

 * only post if you have a working solution!
 * express the problem as succinctly as possible in text.
 * provide example code that exposes the error in a succinctly manner.
 * check any emotional baggage at the door.
 * only present real bugs and not illusions or preconceived notions of how a 
certain chunk of code, or syntax, or whatever, should behave.
 
As you can see, a bug tracker is in contrast to a PyWart listing that i 
propose. You can think of the PyWart listing as the first stop to solving all 
problems in the language. Only the true bugs, for which a patch has been 
written, should continue on to the Bug Tracker.  

In the PyWart list we want to get feedback not only for true bugs, but for 
un-intuitiable API semantics, inconsistencies, multiplicities, and asininities. 

We also want people to express themselves without applying any filters. If a 
certain feature bugs them, then rant about it a little bit, let people know 
how you feel about the wart, and THEN try to offer a solution if you have the 
capacity to do so. If not, hope that someone more talented will be influenced 
enough by your arguments to submit a patch on the tracker.

So, you could conclude that a PyWart listing is an informal manner by which 
/all/ levels of the community can participate in the evolution of the language. 
Anyone who has a mouth and a problem will now have an outlet. 

Sure, some people will consider ridiculous things to be warts that need 
removing, and they might rant and rave and nobody will pay them any attention, 
but hey, at least with my proposed PyWart listing they have a safe outlet for 
these emotions. And, maybe a fix will be the result, who knows.

All groups need an area where you can go and misbehave without being ostracized 
from the community. Some groups call this area the bar or what ever. With a 
PyWart listing we can maintain a high level of professionalism at py-ideas and 
at the bug tracker whilst simultaneously providing a synergy of evolutionary 
ideas to be expressed without censorship.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: memory management

2013-02-18 Thread Dave Angel

On 02/18/2013 10:29 AM, Sudheer Joseph wrote:

HI,
 I have been trying to compute cross correlation between a time series 
at a location f(1) and the timeseries of spatial data f(XYT) and saving the 
resulting correlation coefficients and lags in a 3 dimensional array which is 
of fairly big size. Though the code I made for this purpose works up to few 
iterations then it hangs due to apparent memory crunch. Can anybody suggest a 
better way to handle this situation so that the computation and data storing 
can be done with out hangups. Finally I intend to save the data as netcdf file 
which is not implemented as of now. Below is the piece of code I wrote for this 
purpose.



Python version and OS please.  And is the Python 32bit or 64bit?  How 
much RAM does the computer have, and how big are the swapfiles ?


Fairly big is fairly vague.  To some people, a list with 100k members 
is huge, but not to a modern computer.


How have you checked whether it's running out of memory?  Have you run 
'top' on it?  Or is that just a guess?


I haven't used numpy, scipy, nor matplotlib, and it's been a long time 
since I did correlations.  But are you sure you're not just implementing 
an O(n**3) algorithm or something, and it's just extremely slow?




from mpl_toolkits.basemap import Basemap as bm, shiftgrid, cm
import numpy as np
import matplotlib.pyplot as plt
from netCDF4 import Dataset
from math import pow, sqrt
import sys
from scipy.stats import t


 snip

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


Re: Python 3.3 vs. MSDOS Basic

2013-02-18 Thread Alexander Blinne
Am 18.02.2013 20:13, schrieb John Immarino:
 I coded a Python solution for Problem #14 on the Project Euler website. I was 
 very surprised to find that it took 107 sec. to run even though it's a pretty 
 simple program.  I also coded an equivalent solution for the problem in the 
 old MSDOS basic. (That's the 16 bit app of 1980s vintage.)  It ran in 56 sec. 
 Is there a flaw in my coding, or is Python really this slow in this 
 particular application. MSDOS Basic usually runs at a snails pace compared to 
 Python.

 max=0
 m=0
 while m=100:
 m+=1
 count=0
 n=m
 while n!=1:
 count+=1
 if n%2==0:
 n=n//2
 else:
 n=3*n+1
 if countmax:
  max=count
  num=m
 print(num,max)

I cannot compare my timings with basic but python 2.7.3 and python 3.2.3
are both equally slow hier (~50 sec).
pypy is a lot faster (only some old version 1.7.0, current versions
should be faster still) with about 5 sec.

The following C-Program:

#include stdio.h

int main(void) {

  int max = 0;
  int m = 0;
  long int n;
  int count;
  int num;

  while(m=100) {
m++;
n = m;
count = 0;

while(n != 1) {
  count++;
  if(n % 2 == 0) {
n = n / 2;
  }
  else {
n = n*3 + 1;
  }
}

if(count  max) {
  max = count;
  num = m;
}
  }

  printf(%d, %d\n, num, max);
}

Does the job in just under 1 sec.

Greetings
Alexander






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


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Steven D'Aprano
Terry Reedy wrote:

 On 2/18/2013 6:47 AM, John Reid wrote:
 
 I was hoping namedtuples could be used as replacements for tuples
in all instances.
 
 This is a mistake in the following two senses. First, tuple is a class
 with instances while namedtuple is a class factory that produces
 classes. (One could think of namedtuple as a metaclass, but it was not
 implemented that way.) 


I think you have misunderstood. I don't believe that John wants to use the
namedtuple factory instead of tuple. He wants to use a namedtuple type
instead of tuple.

That is, given:

Point3D = namedtuple('Point3D', 'x y z')

he wants to use a Point3D instead of a tuple. Since:

issubclass(Point3D, tuple) 

holds true, the Liskov Substitution Principle (LSP) tells us that anything
that is true for a tuple should also be true for a Point3D. That is, given
that instance x might be either a builtin tuple or a Point3D, all of the
following hold:

- isinstance(x, tuple) returns True
- len(x) returns the length of x
- hash(x) returns the hash of x
- x[i] returns item i of x, or raises IndexError
- del x[i] raises TypeError
- x + a_tuple returns a new tuple
- x.count(y) returns the number of items equal to y

etc. Basically, any code expecting a tuple should continue to work if you
pass it a Point3D instead (or any other namedtuple).

There is one conspicuous exception to this: the constructor:

type(x)(args)

behaves differently depending on whether x is a builtin tuple, or a Point3D.

The LSP is about *interfaces* and the contracts we make about those
interfaces, rather than directly about inheritance. Inheritance is just a
mechanism for allowing types to automatically get the same interface as
another type. Another way to put this, LSP is about duck-typing. In this
case, if we have two instances:

x = (1, 2, 3)
y = Point3D(4, 5, 6)


then x and y:

- quack like tuples
- swim like tuples
- fly like tuples
- walk like tuples
- eat the same things as tuples
- taste very nice cooked with orange sauce like tuples

etc., but y does not lay eggs like x. The x constructor requires a single
argument, the y constructor requires multiple arguments.

You can read more about LSP here:

http://en.wikipedia.org/wiki/Liskov_substitution_principle

although I don't think this is the most readable Wikipedia article, and the
discussion of mutability is a red-herring. Or you can try this:

http://c2.com/cgi/wiki?LiskovSubstitutionPrinciple

although even by c2 wiki standards, it's a bit of a mess. These might help
more:

http://blog.thecodewhisperer.com/2013/01/08/liskov-substitution-principle-demystified/

http://lassala.net/2010/11/04/a-good-example-of-liskov-substitution-principle/


 Second, a tuple instance can have any length and 
 different instances can have different lengths. On the other hand, all
 instances of a particular namedtuple class have a fixed length.

This is a subtle point. If your contract is, I must be able to construct an
instance with a variable number of items, then namedtuples are not
substitutable for builtin tuples. But I think this is an *acceptable*
violation of LSP, since we're deliberately restricting a namedtuple to a
fixed length. But within the constraints of that fixed length, we should be
able to substitute a namedtuple for any tuple of that same length.


 This 
 affects their initialization. So does the fact that Oscar mentioned,
 that fields can be initialized by name.

Constructing namedtuples by name is not a violation, since it *adds*
behaviour, it doesn't take it away. If you expect a tuple, you cannot
construct it with:

t = tuple(spam=a, ham=b, eggs=c)

since that doesn't work. You have to construct it from an iterable, or more
likely a literal:

t = (a, b, c)

Literals are special, since they are a property of the *interpreter*, not
the tuple type. To put it another way, the interpreter understands (a,b,c)
as syntax for constructing a tuple, the tuple type does not. So we cannot
expect to use (a,b,c) syntax to construct a MyTuple instance, or a Point3D
instance instead.

If we hope to substitute a subclass, we have to use the tuple constructor
directly:

type_to_use = tuple
t = type_to_use([a, b, c])

Duck-typing, and the LSP, tells us that we should be able to substitute a
Point3D for this:

type_to_use = namedtuple('Point3D', 'x y z')
t = type_to_use([a, b, c])

but we can't. And that is an important violation of LSP.

There could be three fixes to this, none of them practical:

1) tuple could accept multiple arguments, tuple(a, b, c) = (a, b, c) but
that conflicts with the use tuple(iterable). If Python had * argument
unpacking way back in early days, it might have been better to give tuples
the signature tuple(*args), but it didn't and so it doesn't and we can't
change that now.

2) namedtuples could accept a single iterable argument like tuple does, but
that conflicts with the desired signature pt = Point3D(1, 2, 3).

3) namedtuples should not claim to be tuples, which is probably the

Re: Python 3.3 vs. MSDOS Basic

2013-02-18 Thread Terry Reedy

On 2/18/2013 2:13 PM, John Immarino wrote:

I coded a Python solution for Problem #14 on the Project Euler
website. I was very surprised to find that it took 107 sec. to run
even though it's a pretty simple program.  I also coded an equivalent
solution for the problem in the old MSDOS basic. (That's the 16 bit
app of 1980s vintage.)  It ran in 56 sec. Is there a flaw in my
coding, or is Python really this slow in this particular application.
MSDOS Basic usually runs at a snails pace compared to Python.


I find this surprising too. I am also surprised that it even works, 
given that the highest intermediate value is about 57 billion and I do 
not remember that Basic had infinite precision ints.



The following iterative sequence is defined for the set of positive
integers:

n → n/2 (n is even) n → 3n + 1 (n is odd)


Note that if n is odd, 3n + 1 is even (and not 1!), so one may take two 
steps with (3n + 1)/2.



Using the rule above and starting with 13, we generate the following
sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1

It can be seen that this sequence (starting at 13 and finishing at 1)
contains 10 terms. Although it has not been proved yet (Collatz
Problem), it is thought that all starting numbers finish at 1.


https://en.wikipedia.org/wiki/Collatz_conjecture


Which starting number, under one million, produces the longest
chain?


I suppose 'print(837799)' would not count as a proper solution.


NOTE: Once the chain starts the terms are allowed to go above one
million.


Here is my slightly revised code with timings on a good, 20 month old 
win 7 machine.


from time import time
start = time()

num, max = 0, 0
for m in range(1, 101):
n = m
count=0
while n !=1:
if n  1:  #n % 2:
n = (3*n + 1) // 2
count += 2
else:
n = n//2
count += 1
if count  max:
 num = m
 max = count

print(num, max , time()-start)

# original: 837799, 524 steps, 53.9 secs
# for ... range: 52.3
# reverse inner if 49.0
# double step 39.1
# n  1 instead of n % 2 for test: 36.0, 36.0,  35.9
# n1  instead of n//2: 34.7, 36.1, 36.2;
# this may be internally optimized, so skip

I do not see any fluff left to remove, unless one takes the major step 
of saving already calculated values in an array.


Since the highest intermediate value of n is 56991483520 (445245965
*2**7, from adding if n  maxn: maxn = n to the odd branch, before 
dividing by 2), the array would have to be limited to a much lower 
value, say a few million.


--
Terry Jan Reedy


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


Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Jon Reyes
So I have a dictionary and the key is a number. The values are either a single 
tuple or a tuple of tuples. Is there a better way to go about accessing the 
values of the dictionary? All the tuples contain four elements.

So say:
col = {1: (0,1,2,3): 2: ((0,1,2,3),(2,3,4,5))}

Then to access the values of the tuple I'd do this:

for key,value in col.iteritems():
if isinstance(value[0], tuple):
#iterate through the tuples of a tuple
else:
#iterate through the tuple

At first I was thinking that I could just put the same keys with just single 
tuples on a dictionary but only one tuple exists when I iterate through the 
dictionary. I'm sorry, I'm really new at Python and I just grab anything I can 
when I need it from Google and the Python docs.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Mitya Sirenef

On 02/18/2013 07:52 PM, Jon Reyes wrote:
So I have a dictionary and the  key is a number. The values are either a single tuple or a tuple of 
tuples. Is there a better way to go about accessing the values of the 
dictionary? All the tuples contain four elements.


 So say:
 col = {1: (0,1,2,3): 2: ((0,1,2,3),(2,3,4,5))}

 Then to access the values of the tuple I'd do this:

 for key,value in col.iteritems():
 if isinstance(value[0], tuple):
 #iterate through the tuples of a tuple
 else:
 #iterate through the tuple

 At first I was thinking that I could just put the same keys with just 
single tuples on a dictionary but only one tuple exists when I iterate 
through the dictionary. I'm sorry, I'm really new at Python and I just 
grab anything I can when I need it from Google and the Python docs.


It would be easier to process if, when adding a single tuple
to the dict, you could wrap it inside a tuple: (mytup,)

If your data set is not very large and you don't mind the
slight performance hit, you can simplify processing step:

for k,v in col.iteritems():
if not isinstance(v[0], tuple):
v = (v,)
for tup in v: ...


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

Although the most acute judges of the witches and even the witches
themselves, were convinced of the guilt of witchery, the guilt nevertheless
was non-existent. It is thus with all guilt.  Friedrich Nietzsche

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


Re: memory management

2013-02-18 Thread Sudheer Joseph
 Python version and OS please.  And is the Python 32bit or 64bit?  How 
 
 much RAM does the computer have, and how big are the swapfiles ?
 
Python 2.7.3
ubuntu 12.04 64 bit
4GB RAM
 
 Fairly big is fairly vague.  To some people, a list with 100k members 
 
 is huge, but not to a modern computer.
I have a data loaded to memory from netcdf file which is 2091*140*180 grid 
points (2091 time, 140 latitude 180 longitude) apart from this I define a 2 3d 
arrays r3d and lags3d to store the output for writing out to netcdf file after 
completion. 
 
 
 How have you checked whether it's running out of memory?  Have you run 
 
 'top' on it?  Or is that just a guess?

I have not done this but the speed (assessed from the listing of grid i and j) 
get stopped after j=6 ie after running 6 longitude grids)

Will check the top as you suggested

Here is the result of top it used about 3gB memory

  PID USER  PR  NI  VIRT  RES  SHR S %CPU %MEMTIME+  COMMAND
 3069 sjo   20   0 3636m 3.0g 2504 D3 78.7   3:07.44 python  
 
 I haven't used numpy, scipy, nor matplotlib, and it's been a long time 
 
 since I did correlations.  But are you sure you're not just implementing 
 
 an O(n**3) algorithm or something, and it's just extremely slow?
 
Correlation do not involve such computation normally, I am not sure if 
internally python does some thing like that.
with best regards,
Sudheer
 
 
 
  from mpl_toolkits.basemap import Basemap as bm, shiftgrid, cm
 
  import numpy as np
 
  import matplotlib.pyplot as plt
 
  from netCDF4 import Dataset
 
  from math import pow, sqrt
 
  import sys
 
  from scipy.stats import t
 
 
 
   snip
 
 
 
 -- 
 
 DaveA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Jon Reyes
Wow, why didn't I think of that. Thanks! I'll try it now. By the way I think I 
don't need to wrap the single tuples in runtime because I'm declaring that 
dictionary anyway beforehand and I could just do it right there. I won't be 
adding elements to the tuple.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Roy Smith
Terry Reedy tjre...@udel.edu wrote:

 Initializaing a namedtuple from an iterable is unusual, and 
 hence gets the longer syntax. I

I quick look through our codebase agrees with that.  I found 27 
namedtuple classes.  21 were initialized with MyTuple(x, y, z) syntax.  
Three used MyTuple(*data).

Most interesting were the three that used MyTuple(**data).  In all three 
cases, data was a dictionary returned by re.match.groupdict().  The 
definition of the namedtuple was even built by introspecting the regex 
to find all the named groups!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Jon Reyes
Sorry if I didn't check the code before I posted it, I just mocked it up in 
Google's editor. That's what Mitya suggested too, yep, I guess I just need to 
make it uniform to get rid of the extra checking. Thanks man!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Roy Smith
In article c8abdc96-a47c-462a-9d6e-fcbaad110...@googlegroups.com,
 Jon Reyes everystepsa...@gmail.com wrote:

 So I have a dictionary and the key is a number.
 [...]
 col = {1: (0,1,2,3): 2: ((0,1,2,3),(2,3,4,5))}

The keys here are strings, not numbers.  But that's a detail.  Somewhat 
more importantly, that's a syntax error (one of the colons should be a 
comma).

 The values are either a 
 single tuple or a tuple of tuples. Is there a better way to go about 
 accessing the values of the dictionary? All the tuples contain four elements.

I would make all the values the same shape, i.e. all lists of tuples:

col = {1: [(0,1,2,3)]: 2: [(0,1,2,3),(2,3,4,5)]}

Then you're always doing the same thing with values when you process 
them.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Mark Lawrence

On 19/02/2013 00:52, Jon Reyes wrote:

So I have a dictionary and the key is a number. The values are either a single 
tuple or a tuple of tuples. Is there a better way to go about accessing the 
values of the dictionary? All the tuples contain four elements.

So say:
col = {1: (0,1,2,3): 2: ((0,1,2,3),(2,3,4,5))}

Then to access the values of the tuple I'd do this:

for key,value in col.iteritems():
 if isinstance(value[0], tuple):
 #iterate through the tuples of a tuple
 else:
 #iterate through the tuple

At first I was thinking that I could just put the same keys with just single 
tuples on a dictionary but only one tuple exists when I iterate through the 
dictionary. I'm sorry, I'm really new at Python and I just grab anything I can 
when I need it from Google and the Python docs.



How about this using Python 3.

col = {1: ((0,1,2,3),), 2: ((0,1,2,3),(2,3,4,5))}
for key,tuples in col.items():
for t in tuples:
print(key,t)

A slight aside, your keys look more like strings to me than numbers :)

--
Cheers.

Mark Lawrence

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


Re: Python 3.3 vs. MSDOS Basic

2013-02-18 Thread John Immarino
On Monday, February 18, 2013 2:58:57 PM UTC-7, Chris Angelico wrote:
 On Tue, Feb 19, 2013 at 8:56 AM, Chris Angelico ros...@gmail.com wrote:
 
  On Tue, Feb 19, 2013 at 8:55 AM, Chris Angelico ros...@gmail.com wrote:
 
  How long did your BASIC version take, and how long did the Python
 
  version on the same hardware?
 
 
 
  Oops, my bad, you already posted the figures :) And I forgot to ask:
 
  Which Python version didyou use?
 
 
 
  ChrisA
 
 
 
 Doh. I'm having a great day of not reading properly, today. (I blame
 
 checking mail on the bus, it took me over an hour to read this one
 
 message and I'd forgotten the subject line by the time I got to the
 
 end.) Python 3.3, right there in the header. Disregard me!
 
 
 
 ChrisA

Thanks,Chris. I'm a newbie to Python and didn't realize that it's not as good 
at number crunching as some of the others. It does seem to do better than Basic 
with numbers in lists as opposed to arrays in Basic.









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


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Jon Reyes
Hi Mark. Well, doesn't iteritems() work the same? or am I missing something? By 
the way I'm sure I read the dictionaries part of Python but I'm unsure if it 
would take int's as a key for dictionaries. I've been weaned on Java where the 
keys of hashmaps are always Strings. 

PS: Just checked, wow I could use ints as keys. Awesome!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Oscar Benjamin
On 19 February 2013 00:18, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
 Terry Reedy wrote:
 On 2/18/2013 6:47 AM, John Reid wrote:
[snip]
 Is this a problem with namedtuples, ipython or just a feature?

 With canSequence. If isinstance was available and the above were written
 before list and tuple could be subclassed, canSequence was sensible when
 written. But as Oscar said, it is now a mistake for canSequence to
 assume that all subclasses of list and tuple have the same
 initialization api.

 No, it is not a mistake. It is a problem with namedtuples that they violate
 the expectation that they should have the same constructor signature as
 other tuples. After all, namedtuples *are* tuples, they should be
 constructed the same way. But they aren't, so that violates a reasonable
 expectation.

It is a mistake. A namedtuple class instance provides all of the
methods/operators provided by a tuple. This should be sufficient to
fill the tuplishness contract. Requiring that obj satisfy a contract
is one thing. When you get to the point of requiring that type(obj)
must do so as well you have gone beyond duck-typing and the normal
bounds of poly-morphism.

It's still unclear what the purpose of canSequence is, but I doubt
that there isn't a better way that it (and its related functions)
could be implemented that would not have this kind of problem.


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


Re: Python 3.3 vs. MSDOS Basic

2013-02-18 Thread John Immarino
 
  max=0
 
 
 
   max is a bad name -- it masks the built-in max() function
 
 
 
  m=0
 
  while m=100:
 
  m+=1
 
 
 
   Since m is only modified here and has a value of 1 for the first
 
 pass through, you can replace those three lines with
 
 
 
 for m in xrange(1, 101): #python 2.x, just use range() for 3.x
 
 
 
  count=0
 
  n=m
 
 
 
  while n!=1:
 
  count+=1
 
  if n%2==0:
 
  n=n//2
 
  else:
 
  n=3*n+1
 
 
 
   Avoid the comparison to 0 by reversing the then/else actions... Any
 
 0 result is false.
 
 
 
 -=-=-=-=-
 
 import time
 
 
 
 mx = 0
 
 
 
 start = time.time()
 
 for m in xrange(1, 101):
 
 count = 0
 
 n = m
 
 while n  1:
 
 count += 1
 
 if n % 2:   # 0 means false
 
 n = 3 * n + 1
 
 else:
 
 n = n // 2
 
 
 
 if count  mx:
 
 mx, num = count, m
 
 
 
 end = time.time()
 
 
 
 print num, mx
 
 print end-start
 
 -=-=-=-=-
 
 Microsoft Windows XP [Version 5.1.2600]
 
 (C) Copyright 1985-2001 Microsoft Corp.
 
 
 
 E:\UserData\Wulfraed\My Documentscd Python Progs
 
 
 
 E:\UserData\Wulfraed\My Documents\Python ProgsScript1.py
 
 837799 524
 
 83.203687
 
 
 
 E:\UserData\Wulfraed\My Documents\Python Progs
 
 
 
 
 
 
 
 
 
 
 
 
 
 -- 
 
   Wulfraed Dennis Lee Bieber AF6VN
 
 wlfr...@ix.netcom.comHTTP://wlfraed.home.netcom.com/

Thanks, your suggestions are well taken.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Mitya Sirenef

On 02/18/2013 08:38 PM, Jon Reyes wrote:
Hi Mark. Well, doesn't  iteritems() work the same? or am I missing something? By the way I'm 
sure I read the dictionaries part of Python but I'm unsure if it would 
take int's as a key for dictionaries. I've been weaned on Java where the 
keys of hashmaps are always Strings.


 PS: Just checked, wow I could use ints as keys. Awesome!

In fact, any hashable object can be a key in a dict, so you can define
your own custom objects and use them as keys -- this can be
extremely useful sometimes!

 -m


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

Oaths are the fossils of piety.  George Santayana

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


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread alex23
On Feb 18, 9:47 pm, John Reid johnbaronr...@gmail.com wrote:
 See http://article.gmane.org/gmane.comp.python.ipython.user/10270 for more 
 info.

One quick workaround would be to use a tuple where required and then
coerce it back to Result when needed as such:

def sleep(secs):
import os, time, parallel_helper
start = time.time()
time.sleep(secs)
return tuple(parallel_helper.Result(os.getpid(), time.time() -
start))

rc = parallel.Client()
v = rc.load_balanced_view()
async_result = v.map_async(sleep, range(3, 0, -1), ordered=False)
for ar in async_result:
print parallel_helper.Result(*ar)

You can of course skip the creation of Result in sleep and only turn
it into one in the display loop, but it all depends on additional
requirements (and adds some clarity to what is happening, I think).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Dave Angel

On 02/18/2013 08:38 PM, Jon Reyes wrote:

Hi Mark. Well, doesn't iteritems() work the same? or am I missing something? By 
the way I'm sure I read the dictionaries part of Python but I'm unsure if it 
would take int's as a key for dictionaries. I've been weaned on Java where the 
keys of hashmaps are always Strings.

PS: Just checked, wow I could use ints as keys. Awesome!



The keys to a dictionary may be any immutable type.  That includes str, 
int, and tuple, but it also can include any other class that meets a 
couple of simple criteria.  In simplified language, the only requirement 
is that the key object cannot change its value or hash, so that if two 
key objects are equal, they stay equal, and if they differ, they stay 
different.


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


Re: Awsome Python - chained exceptions

2013-02-18 Thread alex23
On Feb 18, 3:51 pm, Rick Johnson rantingrickjohn...@gmail.com wrote:
 I apologize for this doubling of my messages and i can assure you i
 don't do this intentionally. Proper netiquette is very important to me.
 These double posts are another unfortunate side-effect of using the
 buggy Google Groups web-face to read/write Usenet. I've sent feedback
 to the Google Groups long ago and have yet to see any changes or even
 get any replys.

Weird, I'm using GG too and not seeing any doubling of my messages. I
have reverted to using the old interface, though, so it might be a
side-effect of the new version they're hyping, which does seem to have
been designed by Satan himself (the way they've separated thread view
from article view is a huge WTF). I've sent a heap of feedback to them
as well with no response. Google don't really seem to want to hype
Usenet as anything other than a target for blogspot spam, it appears.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Jon Reyes
Thanks Dave and Mitya for enlightening me about dictionaries. I'm still 
confused about this though:

 so that if two 
key objects are equal, they stay equal, and if they differ, they stay 
different. 

What does this mean? I won't be comparing key objects with one another. Also, 
when I had two keys with the same value the value of the other key disappeared 
so I assume in runtime if there are multiple keys of the same value only the 
last one will appear.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Mark Lawrence

On 19/02/2013 01:38, Jon Reyes wrote:

Hi Mark. Well, doesn't iteritems() work the same?



It's iteritems for Python 2, items for Python 3.

--
Cheers.

Mark Lawrence

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


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Jon Reyes
Oh, I see, thanks! I was thinking I'll study 2.7 and once I'm comfortable with 
Python as a language I'll move to 3. Heck, I don't even know how to create a 
simple main method.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Mitya Sirenef

On 02/18/2013 09:17 PM, Jon Reyes wrote:

Thanks Dave and Mitya for  enlightening me about dictionaries. I'm still 
confused about this though:


  so that if two
 key objects are equal, they stay equal, and if they differ, they stay
 different. 

 What does this mean? I won't be comparing key objects with one 
another. Also, when I had two keys with the same value the value of the 
other key disappeared so I assume in runtime if there are multiple keys 
of the same value only the last one will appear.


You won't be, but dict will.

Dict is by definition a mapping where a value is assigned to a unique
key. If you have two keys and two values, and then change one key to
be equal to the second key, that's not kosher, because which value it's
supposed to return when you try to get it by that key?

So in effect, key's hash value should not change. If key is immutable,
you can be certain that it's hash value will not change. If it's
mutable, you have to make sure not to change the key in a way that'd
make its hash value different than it was.

 -m

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

Graphic design is the paradise of individuality, eccentricity, heresy,
abnormality, hobbies and humors.  George Santayana

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


Re: Python 3.3 vs. MSDOS Basic

2013-02-18 Thread Chris Angelico
On Tue, Feb 19, 2013 at 12:39 PM, John Immarino joh...@gmail.com wrote:
 On Monday, February 18, 2013 2:58:57 PM UTC-7, Chris Angelico wrote:
 On Tue, Feb 19, 2013 at 8:56 AM, Chris Angelico ros...@gmail.com wrote:

  On Tue, Feb 19, 2013 at 8:55 AM, Chris Angelico ros...@gmail.com wrote:

  How long did your BASIC version take, and how long did the Python

  version on the same hardware?

 

  Oops, my bad, you already posted the figures :) And I forgot to ask:

  Which Python version didyou use?

 

  ChrisA



 Doh. I'm having a great day of not reading properly, today. (I blame

 checking mail on the bus, it took me over an hour to read this one

 message and I'd forgotten the subject line by the time I got to the

 end.) Python 3.3, right there in the header. Disregard me!



 ChrisA

 Thanks,Chris. I'm a newbie to Python and didn't realize that it's not as good 
 at number crunching as some of the others. It does seem to do better than 
 Basic with numbers in lists as opposed to arrays in Basic.

Yes, Python is excellent at data handling. I'll cheerfully use Python
to manipulate huge lists or arrays, and its performance at that is
usually well within the good enough range (for instance, anything
that manipulates the file system will be waiting on my disks, not on
Python). It's an excellent tool in the toolkit, just not the one
solution to everything. (Nothing's that!)

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


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Steven D'Aprano
Oscar Benjamin wrote:

 On 19 February 2013 00:18, Steven D'Aprano
 steve+comp.lang.pyt...@pearwood.info wrote:
 Terry Reedy wrote:
 On 2/18/2013 6:47 AM, John Reid wrote:
 [snip]
 Is this a problem with namedtuples, ipython or just a feature?

 With canSequence. If isinstance was available and the above were written
 before list and tuple could be subclassed, canSequence was sensible when
 written. But as Oscar said, it is now a mistake for canSequence to
 assume that all subclasses of list and tuple have the same
 initialization api.

 No, it is not a mistake. It is a problem with namedtuples that they
 violate the expectation that they should have the same constructor
 signature as other tuples. After all, namedtuples *are* tuples, they
 should be constructed the same way. But they aren't, so that violates a
 reasonable expectation.
 
 It is a mistake. A namedtuple class instance provides all of the
 methods/operators provided by a tuple. This should be sufficient to
 fill the tuplishness contract.

Should be, but *doesn't*. 

If your code expects a tuple, then it should work with a tuple. Namedtuples
are tuples, but they don't work where builtin tuples work, because their
__new__ method has a different signature.

I can understand arguing that this is acceptable breakage for various
reasons -- practicality beats purity. I can't understand arguing that the
principle is wrong.


 Requiring that obj satisfy a contract 
 is one thing. When you get to the point of requiring that type(obj)
 must do so as well you have gone beyond duck-typing and the normal
 bounds of poly-morphism.

Constructor contracts are no less important than other contracts. I'm going
to give what I hope is an example that is *so obvious* that nobody will
disagree.

Consider the dict constructor dict.fromkeys:

py mydict = {'a':1}
py mydict.fromkeys(['ham', 'spam', 'eggs'])
{'eggs': None, 'ham': None, 'spam': None}


Now I subclass dict:

py class MyDict(dict):
... @classmethod
... def fromkeys(cls, func):
... # Expects a callback function that gets called with no arguments
... # and returns two items, a list of keys and a default value.
... return super(MyDict, cls).fromkeys(*func())
...

Why would I change the syntax like that? Because reasons. Good or bad,
what's done is done and there is my subclass. Here is an instance:

py mydict = MyDict({'a': 1})
py isinstance(mydict, dict)
True


Great! So I pass mydict to a function that expects a dict. This ought to
work, because mydict *is* a dict. It duck-types as a dict, isinstance
agrees it is a dict. What could possibly go wrong?

What goes wrong is that some day I pass it to a function that calls
mydict.fromkeys in the usual fashion, and it blows up.

py mydict.fromkeys(['spam', 'ham', 'eggs'])
Traceback (most recent call last):
  File stdin, line 1, in module
  File stdin, line 4, in fromkeys
TypeError: 'list' object is not callable

How is this possible? Is mydict not a dict? It should be usable anywhere I
can use a dict. How is this possibly acceptable behaviour for something
which claims to be a dict?

This is a violation of the Liskov Substitution Principle, and a violation of
normal expectations that if mydict quacks like a dict, it should lay eggs
like a duck.

That namedtuple's constructor is __new__ rather than fromkeys is an
irrelevant distraction. The principle still applies. It is perfectly
reasonable to expect that if instance t is a tuple, then *any* method on t
should have the same signature, regardless of whether that method is
called index, __getitem__, or __new__.

If this fundamental principle is violated, there should be a very good
reason, and not just because constructor contracts aren't important.


 It's still unclear what the purpose of canSequence is, but I doubt
 that there isn't a better way that it (and its related functions)
 could be implemented that would not have this kind of problem.

Incorrect. The problem is with *namedtuples*, not canSequence, because
namedtuples promise to implement a strict superset of the behaviour of
builtin tuples, while in fact they actually *take behaviour away*. Tuples
promise to allow calls to the constructor like this:

any_tuple.__new__(type(any_typle), iterable))

but that fails if any_tuple is a namedtuple.

I am not arguing for or against the idea that this is an *acceptable*
breakage, give other requirements. But from the point of view of interface
contracts, it is a breakage, and as the Original Poster discovered, it can
and will break code.



-- 
Steven

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


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Dave Angel

On 02/18/2013 09:54 PM, Mitya Sirenef wrote:

On 02/18/2013 09:17 PM, Jon Reyes wrote:

Thanks Dave and Mitya for  enlightening me about dictionaries. I'm
still confused about this though:

 
   so that if two
  key objects are equal, they stay equal, and if they differ, they stay
  different. 
 
  What does this mean? I won't be comparing key objects with one
another. Also, when I had two keys with the same value the value of the
other key disappeared so I assume in runtime if there are multiple keys
of the same value only the last one will appear.

You won't be, but dict will.

Dict is by definition a mapping where a value is assigned to a unique
key. If you have two keys and two values, and then change one key to
be equal to the second key, that's not kosher, because which value it's
supposed to return when you try to get it by that key?

So in effect, key's hash value should not change. If key is immutable,
you can be certain that it's hash value will not change. If it's
mutable, you have to make sure not to change the key in a way that'd
make its hash value different than it was.

  -m



It's a little stronger than that, since equal hashes cannot assure equal 
data.  The equality of each object pair in a dict must not change over 
time, not just the hashes of the individual objects.


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


Re: Python Warts: The where, when, how, and why of a PyWart.

2013-02-18 Thread Terry Reedy

On 2/18/2013 6:55 PM, Rick Johnson wrote:

Pointing out problems on appropriate list is fine,


I agree. Python-list is the most free among python.org lists.


But i don't believe a Bug Tracker and a Language Wart listing are the same.


I agree. Anyone is free to make their own site or blog, call it what 
they want, and run it however they want.


 My belief is that a Tracker is for posting patches for bugs;
 meaning either the kind that throw errors:


[Warning: Naively hypothetical examples ahead!]
 py 1+2
 Exception: Seg Fault!


We take these seriously if they involve core python. There is even, 
somewhere, a separate list of 'crashers' that we think should be fixed. 
The one left are the ones we do not know how to fix. Some reports are 
closed as won't fix when the crash is a result of freedom we will not 
take away. Two examples are ctypes and the possibility of hacking the 
bytecode and other fields in CPython code objects.



...or the kind that produce illogical results:
 py 1+2
 12


Would that all bug issue were so clear. The hard one involve behavior 
that is not clearly defined in the docs.


The tracker is also for improvements, though I think idea are best 
discussed off the tracker first to discover if there is really any 
support and possibly to improve the idea. There are currently 1170 open 
'enhancement' requests on the tracker. Most are likely forgotten about 
and will never go anywhere without off-tracker discussion.


The tracker is definitely not the place for 'How do I use Python?' 
questions, or How do I fix my code? questions. They belong here.



I also believe a Bug Tracker is a serious place where stict etiquette

 and posting rules should be maintained:

Absolutely. Thank you for saying that.


  * only post if you have a working solution!


That is ideal, but some people find problems they do not know how to 
fix. What is annoying however, is people how say I could contribute 
code, but I am too busy, so I want one of you volunteer to do the work I 
will not do.



  * express the problem as succinctly as possible in text.
  * provide example code that exposes the error in a succinctly manner.


These two apply to posts here too. I am still amazed at 'My code doesn't 
work. How do I fix it? posts.



  * check any emotional baggage at the door.
  * only present real bugs and not illusions or preconceived notions   of 
how a certain chunk of code, or syntax, or whatever, should behave.


I would add that when people do post the last kind of issue, they should 
respect the developers enough to accept an issue being closed and not 
reopen it to drag on the discussion. We have too limited time for too 
many issues and are not quite keeping up as it is.


I have little or no argument with the other points you made.

--
Terry Jan Reedy

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


Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Mitya Sirenef

On 02/18/2013 10:14 PM, Dave Angel wrote:

On 02/18/2013 09:54 PM, Mitya  Sirenef wrote:

 On 02/18/2013 09:17 PM, Jon Reyes wrote:
 Thanks Dave and Mitya for enlightening me about dictionaries. I'm
 still confused about this though:
 
   so that if two
  key objects are equal, they stay equal, and if they differ, they stay
  different. 
 
  What does this mean? I won't be comparing key objects with one
 another. Also, when I had two keys with the same value the value of the
 other key disappeared so I assume in runtime if there are multiple keys
 of the same value only the last one will appear.

 You won't be, but dict will.

 Dict is by definition a mapping where a value is assigned to a unique
 key. If you have two keys and two values, and then change one key to
 be equal to the second key, that's not kosher, because which value it's
 supposed to return when you try to get it by that key?

 So in effect, key's hash value should not change. If key is immutable,
 you can be certain that it's hash value will not change. If it's
 mutable, you have to make sure not to change the key in a way that'd
 make its hash value different than it was.

 -m


 It's a little stronger than that, since equal hashes cannot assure
 equal data. The equality of each object pair in a dict must not
 change over time, not just the hashes of the individual objects.


Ah, yes - that's true; if hashes were unequal and then the key is
changed to be equal to the first key, both mydict[key1] and mydict[key2]
will give you value1, but iterating over dict items will print key1,
value1; key2, value2. And that's not a good thing.  -m


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

True friends stab you in the front.
Oscar Wilde

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


Re: Python 3.3 vs. MSDOS Basic

2013-02-18 Thread Nick Mellor
Hi John,

Thanks for the problem. I've been writing Python for about 4 years now and am 
beginning to feel like I'm writing much better Python code.

Python does fine on this problem if you play to its strengths. The following 
uses dictionary lookups to store previously computed sequence lengths, thus 
saving a lot of work. The problem is very sparse, i.e. there are huge gaps 
between numbers that are actually used in the solution, making dictionaries a 
better fit than lists.

This code crosses the line in under 3s on a 64-bit laptop. MS-DOS BASIC anyone? 
:-)

I tried precomputing powers of 2 and multiples of 2, but to my surprise it made 
very little difference to timings. Even though precomputing n//2 is fast, I 
think again this is because the problem is sparse and the time the computer 
saves is not offset by the cost of precomputing many multiples of 2 that are 
never needed.

Best wishes,

Nick

And the winner is 837799 with sequence length 524
Time (s):  2.924168109893799
Sequence is:
[837799, 2513398, 1256699, 3770098, 1885049, 5655148, 2827574, 1413787, 
4241362, 2120681, 6362044, 3181022, 1590511, 4771534, 2385767, 7157302, 
3578651, 10735954, 5367977, 16103932, 8051966, 4025983, 12077950, 6038975, 
18116926, 9058463, 27175390, 13587695, 40763086, 20381543, 61144630, 30572315, 
91716946, 45858473, 137575420, 68787710, 34393855, 103181566, 51590783, 
154772350, 77386175, 232158526, 116079263, 348237790, 174118895, 522356686, 
261178343, 783535030, 391767515, 1175302546, 587651273, 1762953820, 881476910, 
440738455, 1322215366, 661107683, 1983323050, 991661525, 2974984576, 
1487492288, 743746144, 371873072, 185936536, 92968268, 46484134, 23242067, 
69726202, 34863101, 104589304, 52294652, 26147326, 13073663, 39220990, 
19610495, 58831486, 29415743, 88247230, 44123615, 132370846, 66185423, 
198556270, 99278135, 297834406, 148917203, 446751610, 223375805, 670127416, 
335063708, 167531854, 83765927, 251297782, 125648891, 376946674, 188473337, 
565420012, 282710006, 141355003, 42
 4065010, 212032505, 636097516, 318048758, 159024379, 477073138, 238536569, 
715609708, 357804854, 178902427, 536707282, 268353641, 805060924, 402530462, 
201265231, 603795694, 301897847, 905693542, 452846771, 1358540314, 679270157, 
2037810472, 1018905236, 509452618, 254726309, 764178928, 382089464, 191044732, 
95522366, 47761183, 143283550, 71641775, 214925326, 107462663, 322387990, 
161193995, 483581986, 241790993, 725372980, 362686490, 181343245, 544029736, 
272014868, 136007434, 68003717, 204011152, 102005576, 51002788, 25501394, 
12750697, 38252092, 19126046, 9563023, 28689070, 14344535, 43033606, 21516803, 
64550410, 32275205, 96825616, 48412808, 24206404, 12103202, 6051601, 18154804, 
9077402, 4538701, 13616104, 6808052, 3404026, 1702013, 5106040, 2553020, 
1276510, 638255, 1914766, 957383, 2872150, 1436075, 4308226, 2154113, 6462340, 
3231170, 1615585, 4846756, 2423378, 1211689, 3635068, 1817534, 908767, 2726302, 
1363151, 4089454, 2044727, 6134182, 3067091, 9201274, 4600637, 13801912, 
 6900956, 3450478, 1725239, 5175718, 2587859, 7763578, 3881789, 11645368, 
5822684, 2911342, 1455671, 4367014, 2183507, 6550522, 3275261, 9825784, 
4912892, 2456446, 1228223, 3684670, 1842335, 5527006, 2763503, 8290510, 
4145255, 12435766, 6217883, 18653650, 9326825, 27980476, 13990238, 6995119, 
20985358, 10492679, 31478038, 15739019, 47217058, 23608529, 70825588, 35412794, 
17706397, 53119192, 26559596, 13279798, 6639899, 19919698, 9959849, 29879548, 
14939774, 7469887, 22409662, 11204831, 33614494, 16807247, 50421742, 25210871, 
75632614, 37816307, 113448922, 56724461, 170173384, 85086692, 42543346, 
21271673, 63815020, 31907510, 15953755, 47861266, 23930633, 71791900, 35895950, 
17947975, 53843926, 26921963, 80765890, 40382945, 121148836, 60574418, 
30287209, 90861628, 45430814, 22715407, 68146222, 34073111, 102219334, 
51109667, 153329002, 76664501, 229993504, 114996752, 57498376, 28749188, 
14374594, 7187297, 21561892, 10780946, 5390473, 16171420, 8085710, 4042855, 
12128566, 6064283, 18192
 850, 9096425, 27289276, 13644638, 6822319, 20466958, 10233479, 30700438, 
15350219, 46050658, 23025329, 69075988, 34537994, 17268997, 51806992, 25903496, 
12951748, 6475874, 3237937, 9713812, 4856906, 2428453, 7285360, 3642680, 
1821340, 910670, 455335, 1366006, 683003, 2049010, 1024505, 3073516, 1536758, 
768379, 2305138, 1152569, 3457708, 1728854, 864427, 2593282, 1296641, 3889924, 
1944962, 972481, 2917444, 1458722, 729361, 2188084, 1094042, 547021, 1641064, 
820532, 410266, 205133, 615400, 307700, 153850, 76925, 230776, 115388, 57694, 
28847, 86542, 43271, 129814, 64907, 194722, 97361, 292084, 146042, 73021, 
219064, 109532, 54766, 27383, 82150, 41075, 123226, 61613, 184840, 92420, 
46210, 23105, 69316, 34658, 17329, 51988, 25994, 12997, 38992, 19496, 9748, 
4874, 2437, 7312, 3656, 1828, 914, 457, 1372, 686, 343, 1030, 515, 1546, 773, 
2320, 1160, 580, 290, 145, 436, 218, 109, 328, 164, 82, 41, 124, 62, 31, 94, 
47, 142, 71, 214, 107, 

Re: First attempt at a Python prog (Chess)

2013-02-18 Thread Tim Roberts
Chris Hinsley chris.hins...@gmail.com wrote:

Is a Python list as fast as a bytearray ?

Python does not actually have a native array type.  Everything in your
program that looked like an array was actually a list.
-- 
Tim Roberts, t...@probo.com
Providenza  Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3.3 vs. MSDOS Basic

2013-02-18 Thread Terry Reedy

On 2/18/2013 4:55 PM, Chris Angelico wrote:


Running under Python 2.6, both your version and mine take about 90
seconds to run. But under Python 3.3, where (among other things)
range() yields values lazily, my version is significantly faster than
yours. BUT! Both versions, under 3.3, are significantly *slower* than
under 2.6. My first thought is that it's because Py2 has different
types for 'int' and 'long', and Py3 doesn't (effectively, everything's
a long), so I added an L suffix to every number and ran each of them
under 2.6 again. Seems that was the bulk of the difference, though not
all.

Pythonistas, does this count as a regression, or is Python
sufficiently not a number crunching language that we don't care?


Both. This brute-force algorithm is almost pure number crunching. This 
is the sort of thing pypy and cython are good at speeding up. (I leave 
out numpy only because it is not an array-oriented problem.)


I put a counter in the inner loop of my improved version the does 
(3*n+1)//2 in one step and got 87 826 478 in 40 seconds (without the 
counter). That is 2 million loops per second and each loop does a 
compare, one or two integer ops, and creates and releases one or two ints.


If I were doing a lot of int crunching like this with CPython and were 
building my own interpreter, I would greatly expand the range of 
pre-allocated 'small' ints to avoid some of the repeated allocation and 
de-allocation. On a multi-gibibyte machine, allocating up to 100 
instead of 256 would be feasible.


As Ian noted, an intelligent algorithm in CPython can match pypy and is 
in the ballpark of C, but is much easier to write in Python than C. It 
is possible that Ian's code could be improved further. A pre-allocated 
arrray + dict might be faster. Whenever an odd value is filled in, 
powers of 2 times that value can also be.


--
Terry Jan Reedy

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


PIL ImageChops.difference not working correctly(?)

2013-02-18 Thread Jon Reyes
I have two images generated from ImageMagick that I need to compare through PIL 
but when I compare them it says that the two images aren't identical. I tried 
viewing the supposed difference but all I see is a black image which means 
the two image are identical. ImageChops even returns the region where the 
supposed non - identical marks are, but there's nothing there. Any thoughts?

I just do this to compare if the two images are identical:
diff = ImageChops.difference(img1, img2)
if diff.getbbox() == None:
#then the images must be the same
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PIL ImageChops.difference not working correctly(?)

2013-02-18 Thread Jon Reyes
Wow, what the heck, I just learned that using beyond compare if I compare the 
two images then I'll see a different section under tolerance mode. Anyone an 
expert on images? The two images have whites on that same image but they're 
different according to BeyondCompare. What's more, if I do a binary comparison 
the two images are the same. Guess I'll just find a way to compare images 
through binary in Python.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: First attempt at a Python prog (Chess)

2013-02-18 Thread Steven D'Aprano
On Mon, 18 Feb 2013 20:15:28 -0800, Tim Roberts wrote:

 Chris Hinsley chris.hins...@gmail.com wrote:

Is a Python list as fast as a bytearray ?
 
 Python does not actually have a native array type.  Everything in your
 program that looked like an array was actually a list.

Actually it does, but you have to import it first, it is not a built-in 
data type.


py import array
py arr = array.array('f')  # array of floats (C singles)
py arr.append(0.1)
py arr.append(0.2)
py print(arr)
array('f', [0.1000149011612, 0.2000298023224])
py arr.append(foo)
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: a float is required


See the documentation for array to see the available type-codes.

http://docs.python.org/2/library/array.html
http://docs.python.org/3/library/array.html

As part of the standard library, Jython and IronPython are required to 
implement arrays as well (although they don't use C arrays, so the 
implementation may be different).

http://www.jython.org/docs/library/array.html
http://ironpython-test.readthedocs.org/en/latest/library/array.html

IronPython also gives you access to .Net arrays, although of course that 
is not standard Python:

http://www.ironpython.info/index.php/Typed_Arrays_in_IronPython



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


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread raymond . hettinger
On Monday, February 18, 2013 6:09:16 AM UTC-8, John Reid wrote:
 I'm aware how to construct the namedtuple and the tuple. My point was
 that they use different syntaxes for the same operation and this seems
 to break ipython. I was wondering if this is a necessary design feature
 or perhaps just an oversight on the part of the namedtuple author or
 ipython developers.

It was not an oversight on the part of the namedtuple author.
It was a deliberate design decision to improve usability for
named tuple's primary use case.

Consider a namedtuple such as:

 Person = namedtuple('Person', ['name', 'rank', 'serial_number'])

With the current signature, instances can be created like this:

p = Person('Guido', 'BDFL', 1)

If instead, the __new__ signature matched that of regular tuples,
you would need to write:

   p = Person(('Guido', 'BDFL', 1))

The double parens are awkward.  You would lose tooltips that prompt you for 
Person(name, rank, serial_number).  You would lose the ability to use keyword 
arguments such as Person(rank='BDFL', serial_number=1, name='Guido') or 
Person(**d).  The repr for the namedtuple would lose its eval(repr(t)) 
roundtrip property.  

If your starting point is an existing iterable such as s=['Guido', 'BDFL', 1], 
you have a couple of choices:   p=Person(*s) or p=Person._make(s).  The latter 
form was put it to help avoid unpacking and repacking the arguments.   


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


Re: Differences creating tuples and collections.namedtuples

2013-02-18 Thread Gregory Ewing

Steven D'Aprano wrote:


Terry Reedy wrote:



In fact, one reason to subclass a class is to change the initialization
api.



That might be a reason that people give, but it's a bad reason from the
perspective of interface contracts, duck-typing and the LSP.


Only if you're going to pass the class off to something as
a factory function.

Note that having a different constructor signature is *not*
an LSP violation for *instances* of a class. The constructor
is not part of the interface for instances, only for the
class itself.

In practice, it's very common for a class to have a different
constructor signature from its base class, and this rarely
causes any problem.

IPython is simply making a dodgy assumption. It gets away with
it only because it's very rare to encounter subclasses of
list or tuple at all.

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


  1   2   >