Style questions

2008-10-23 Thread David Di Biase
I have a few simple questions regarding python style standards. I have a
class contained in a module...I'm wondering if I should perform any imports
that are relevant to the class within the constructor of the class or at the
top of the module page.

Also if I'm creating a docstring for the class I should list all my public
methods, should I just list them or should I just summarise what they do?
ie:

"""Displays a graphical game of variant of Connect4.
   Supports two players on a 6x7 game board.

   Public methods:
   __init__()
   clear_screen()
   draw_header()
   draw_board()
   play()
   prompt_for_move()

"""

Last question, sometimes I have a simple function with no keyword arguments
and returns none. According to the styleguide we are to include return None
at the end of the function regardless, so should I also explicitly state
that the function returns this in the one line description? ie:

"""Clears the screen of all content and returns none.  """

I'm sure I'll have more simple ones. This is painstaking :-/

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


Schwartzian transform for tuple in list

2008-09-24 Thread David Di Biase
Hi,

I have a rather large list structure with tuples contained in them (it's
part of a specification I received) looks like so:
[(x1,y1,r1,d1),(x2,y2,r2,d2)...]

The list can range from about 800-1500 tuples in size and I'm currently
sorting it with this:

a_list.sort(lambda a, b: cmp(b[3], a[3]))

I'm actually sorting it by the last value in the tuple (d2). I have been
researching more efficient sorting algorithms and came across Schwartzian
transform via these links:

   -
   http://www.biais.org/blog/index.php/2007/01/28/23-python-sorting-efficiency
   -
   
http://dev.fyicenter.com/Interview-Questions/Python/Can_you_do_a_Schwartzian_Transform_in_Python_.html

I get what's happening (sorta...errr...lol)  but I'm not sure if it is more
efficient in my scenario, if it is then I have no idea how to implement it
properly :-/

Would be great if a true expert would offer a suggestion for me...

Thanks!

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

Re: Schwartzian transform for tuple in list

2008-09-24 Thread David Di Biase
When you say slightly, is it enough to make a difference? Why would  
getitems be faster even - not sure I can think why...


Sent from my iPhone

On 24-Sep-08, at 5:46 PM, Matt Nordhoff <[EMAIL PROTECTED]>  
wrote:



Chris Rebert wrote:
On Wed, Sep 24, 2008 at 2:02 PM, David Di Biase <[EMAIL PROTECTED] 
> wrote:

Hi,

I have a rather large list structure with tuples contained in them  
(it's

part of a specification I received) looks like so:
[(x1,y1,r1,d1),(x2,y2,r2,d2)...]

The list can range from about 800-1500 tuples in size and I'm  
currently

sorting it with this:

a_list.sort(lambda a, b: cmp(b[3], a[3]))


You'd probably be better off using the 'key' keyword argument to
.sort(), which is made for the Schwartzian Transform:

a_list.sort(key=lambda x: x[3])

This sorts the list items by the value produced by the key function
for each item. It's also (IIRC) faster than using a comparison
function like you're currently doing.

Regards,
Chris


Yes, using 'key' is faster than 'cmp'.

If you have Python 2.4 or newer, it would also be slightly faster to  
use

operator.itemgetter() instead of a lambda:


import operator
a_list.sort(key=operator.itemgetter(3))


I'm actually sorting it by the last value in the tuple (d2). I  
have been
researching more efficient sorting algorithms and came across  
Schwartzian

transform via these links:

http://www.biais.org/blog/index.php/2007/01/28/23-python-sorting-efficiency
http://dev.fyicenter.com/Interview-Questions/Python/Can_you_do_a_Schwartzian_Transform_in_Python_.html

I get what's happening (sorta...errr...lol)  but I'm not sure if  
it is more
efficient in my scenario, if it is then I have no idea how to  
implement it

properly :-/

Would be great if a true expert would offer a suggestion for me...

Thanks!

David

--

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


Fastest way to max() list

2008-09-25 Thread David Di Biase
I have a list with about 1000-1500 sub-lists which look like so:
list[-0.28817955213290786, 3.6693631467403929, 'H', 31.31225233035784]]

The first and second values are Angstrom units specifying the location of a
particle. What I'd like to do is determine the distance between the smallest
and largest value in the arrays first position 0. I tried reading the manual
for this but I don't see how it applies key or any of those other commands
to the function. I could easily write a sort to do this and capture the
first and last spots, but why do that when I can use max and min (if I can
actually do that...).

So you wonderful Python gods, lay some knowledge on me. please? lol...

while I'm at it, is there a way to modify an entire list without having to
produce a whole new one? For example now say I want to modify list[0] and
multiply it by some value. From what I understand previous version of Python
allowed lists to be multiplied like matrices...now apparently it just
replicates the list. :-/ shucks...

The first question would be useful to know, but the second question I do
quite a bit of and the "best practice" would be really great to know!

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

Re: Fastest way to max() list

2008-09-26 Thread David Di Biase
Hi Chris,

Yeah I hear you on point A. but this the specification I was given, so I
have to follow it unfortunately. I've also been restricted and not allowed
to use any other packages. I was using NumPy earlier (should have mentioned
that) but I was wondering if there was a simpler way. Is NumPy technically
even faster than just iterating and modifying the list directly?

Also if I'm creating an array then making it, why not just do a temporary
sort and capture the first and last values? Is this method you've provided
supposed to be faster?

Dave

On Fri, Sep 26, 2008 at 12:42 AM, Chris Rebert <[EMAIL PROTECTED]> wrote:

> On Thu, Sep 25, 2008 at 8:57 PM, David Di Biase <[EMAIL PROTECTED]>
> wrote:
> > I have a list with about 1000-1500 sub-lists which look like so:
> > list[-0.28817955213290786, 3.6693631467403929, 'H', 31.31225233035784]]
> >
> > The first and second values are Angstrom units specifying the location of
> a
> > particle. What I'd like to do is determine the distance between the
> smallest
> > and largest value in the arrays first position 0. I tried reading the
> manual
> > for this but I don't see how it applies key or any of those other
> commands
> > to the function. I could easily write a sort to do this and capture the
> > first and last spots, but why do that when I can use max and min (if I
> can
> > actually do that...).
>
> A. You should probably be using objects rather than arrays to
> represent your datapoints, so that they're more structured and it's
> more apparent what the values mean.
>
> B. Assuming by "distance" you meant "difference" and/or that the
> distance is only in 1 dimension:
>
> from operator import itemgetter
> firsts = map(itemgetter(0), main_list)
> distance = max(firsts) - min(firsts)
>
> >
> > So you wonderful Python gods, lay some knowledge on me. please? lol...
> >
> > while I'm at it, is there a way to modify an entire list without having
> to
> > produce a whole new one? For example now say I want to modify list[0] and
> > multiply it by some value. From what I understand previous version of
> Python
> > allowed lists to be multiplied like matrices...now apparently it just
> > replicates the list. :-/ shucks...
>
> You just have to apply the transform to each list element individually
> (also, you might consider using NumPy [http://numpy.scipy.org/] if
> you're doing a lot of matrix manipulation):
>
> for lst in main_list:
>lst[0] *= some_value
>
> Regards,
> Chris
>
> >
> > The first question would be useful to know, but the second question I do
> > quite a bit of and the "best practice" would be really great to know!
> >
> > Thanks in advanced!
> >
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
> --
> Follow the path of the Iguana...
> http://rebertia.com
>
--
http://mail.python.org/mailman/listinfo/python-list

Re: Fastest way to max() list

2008-09-26 Thread David Di Biase
Yeah,

Apologies, it's been a long day for me. It works, just have to check if the
nazis I'm doing this for will allow me to use object and NumPy. ack.

Thanks again,

Dave

On Fri, Sep 26, 2008 at 2:08 PM, Chris Rebert <[EMAIL PROTECTED]> wrote:

> On Fri, Sep 26, 2008 at 7:22 AM, David Di Biase <[EMAIL PROTECTED]>
> wrote:
> > Hi Chris,
> >
> > Yeah I hear you on point A. but this the specification I was given, so I
> > have to follow it unfortunately. I've also been restricted and not
> allowed
> > to use any other packages. I was using NumPy earlier (should have
> mentioned
> > that) but I was wondering if there was a simpler way. Is NumPy
> technically
> > even faster than just iterating and modifying the list directly?
> >
> > Also if I'm creating an array then making it,
>
> Uh, this part of your sentence doesn't quite make sense...
>
> > why not just do a temporary
> > sort and capture the first and last values? Is this method you've
> provided
> > supposed to be faster?
>
> Well, unless you're going to use the rest of the sorted values at some
> point in your program and since you have a large quantity of data,
> yes, my way ought to be faster. Sorting the list is O(N*log(N)) while
> running max and min over the list is only O(N).
>
> Regards,
> Chris
>
> >
> > Dave
> >
> > On Fri, Sep 26, 2008 at 12:42 AM, Chris Rebert <[EMAIL PROTECTED]> wrote:
> >>
> >> On Thu, Sep 25, 2008 at 8:57 PM, David Di Biase <[EMAIL PROTECTED]
> >
> >> wrote:
> >> > I have a list with about 1000-1500 sub-lists which look like so:
> >> > list[-0.28817955213290786, 3.6693631467403929, 'H',
> 31.31225233035784]]
> >> >
> >> > The first and second values are Angstrom units specifying the location
> >> > of a
> >> > particle. What I'd like to do is determine the distance between the
> >> > smallest
> >> > and largest value in the arrays first position 0. I tried reading the
> >> > manual
> >> > for this but I don't see how it applies key or any of those other
> >> > commands
> >> > to the function. I could easily write a sort to do this and capture
> the
> >> > first and last spots, but why do that when I can use max and min (if I
> >> > can
> >> > actually do that...).
> >>
> >> A. You should probably be using objects rather than arrays to
> >> represent your datapoints, so that they're more structured and it's
> >> more apparent what the values mean.
> >>
> >> B. Assuming by "distance" you meant "difference" and/or that the
> >> distance is only in 1 dimension:
> >>
> >> from operator import itemgetter
> >> firsts = map(itemgetter(0), main_list)
> >> distance = max(firsts) - min(firsts)
> >>
> >> >
> >> > So you wonderful Python gods, lay some knowledge on me. please? lol...
> >> >
> >> > while I'm at it, is there a way to modify an entire list without
> having
> >> > to
> >> > produce a whole new one? For example now say I want to modify list[0]
> >> > and
> >> > multiply it by some value. From what I understand previous version of
> >> > Python
> >> > allowed lists to be multiplied like matrices...now apparently it just
> >> > replicates the list. :-/ shucks...
> >>
> >> You just have to apply the transform to each list element individually
> >> (also, you might consider using NumPy [http://numpy.scipy.org/] if
> >> you're doing a lot of matrix manipulation):
> >>
> >> for lst in main_list:
> >>lst[0] *= some_value
> >>
> >> Regards,
> >> Chris
> >>
> >> >
> >> > The first question would be useful to know, but the second question I
> do
> >> > quite a bit of and the "best practice" would be really great to know!
> >> >
> >> > Thanks in advanced!
> >> >
> >> > --
> >> > http://mail.python.org/mailman/listinfo/python-list
> >> >
> >> --
> >> Follow the path of the Iguana...
> >> http://rebertia.com
> >
> >
>
>
>
> --
> Follow the path of the Iguana...
> http://rebertia.com
>
--
http://mail.python.org/mailman/listinfo/python-list

Fun with reverse sorts

2008-10-02 Thread David Di Biase
Hi there,

I'm sorting an expansive list descending according to a list of tuples.
Basically it has to sort the last value in the tuple (3) but if they are the
same then it should resort to using the second last value (2). Now according
to my very limited testing I've somewhat figured out that this SHOULD work:

list.sort(lambda a, b: (cmp(a[3], b[3]), cmp(a[2], b[2])) [a[3] == b[3]],
reverse = True)

Here's an example of the list: [(34,23,54,34), (34,23,230,34),
(34,23,523,334), (34,23,15,17), (34,23,54,17), (45,23,43,123),
(564,23,543,23), (23,54,600,23), (34,54,23,654), (43,54,32,34)]

My first question is in regards to style first. The style guide for Python
doesn't seem to state this (if it has I missed it) but should I be doing
reverse=True or reverse = True with the spaces. lol this is so miniscule but
it's good to know. Also, does this function look/feel right to all the pros
out there. Is there a better way of doing it?

I'm still wrapping my head around ways of accomplishing proper sorts!

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


Re: Fun with reverse sorts

2008-10-02 Thread David Di Biase
I did see that actually, I thought it was only applied to specifying default
parameters and wasn't sure if it ALSO applied to putting it into a function.
In a way however, I see what you're getting at - it's basically the same
thing you're just specifying a default value the same way...

Ok problem resolved.

Grazie tanto ;-)

David

On Fri, Oct 3, 2008 at 12:08 AM, Chris Rebert <[EMAIL PROTECTED]> wrote:

> On Thu, Oct 2, 2008 at 8:07 PM, David Di Biase <[EMAIL PROTECTED]>
> wrote:
> > Hi there,
> >
> > I'm sorting an expansive list descending according to a list of tuples.
> > Basically it has to sort the last value in the tuple (3) but if they are
> the
> > same then it should resort to using the second last value (2). Now
> according
> > to my very limited testing I've somewhat figured out that this SHOULD
> work:
> >
> > list.sort(lambda a, b: (cmp(a[3], b[3]), cmp(a[2], b[2])) [a[3] == b[3]],
> > reverse = True)
>
> Rather than defining a comparison function here (which is less
> efficient), you can use the 'key' argument, which specifies a function
> which is called for each item and returns a so-called key value that
> the corresponding element should be sorted according to. Also, so your
> slicing "[a[3] == b[3]]" isn't necessary because Python is smart and
> sorts tuples that way anyway. Finally, be careful not to use "list" as
> a variable name as this shadows the builtin 'list' type.
>
> So the improved code is:
>
> your_list.sort(key=lambda elem: (elem[3], elem[2]), reverse=True)
>
> >
> > Here's an example of the list: [(34,23,54,34), (34,23,230,34),
> > (34,23,523,334), (34,23,15,17), (34,23,54,17), (45,23,43,123),
> > (564,23,543,23), (23,54,600,23), (34,54,23,654), (43,54,32,34)]
> >
> > My first question is in regards to style first. The style guide for
> Python
> > doesn't seem to state this (if it has I missed it) but should I be doing
> > reverse=True or reverse = True with the spaces. lol this is so miniscule
> but
> > it's good to know. Also, does this function look/feel right to all the
> pros
> > out there. Is there a better way of doing it?
>
> Actually, this is mentioned in PEP 8. You might not be familiar with
> the term in use though ("keyword argument") which describes 'reverse'.
> Here's the relevant section:
>
> """
> - Don't use spaces around the '=' sign when used to indicate a
>  keyword argument or a default parameter value.
>
>  Yes:
>
>  def complex(real, imag=0.0):
>  return magic(r=real, i=imag)
>
>  No:
>
>  def complex(real, imag = 0.0):
>  return magic(r = real, i = imag)
> """
>
> So you want the former: reverse=True
>
> Cheers,
> Chris
> --
> Follow the path of the Iguana...
> http://rebertia.com
>
> >
> > I'm still wrapping my head around ways of accomplishing proper sorts!
> >
> > Dave
> >
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
> >
>
--
http://mail.python.org/mailman/listinfo/python-list