Tuesday, March 30, 2010

comp.lang.python - 25 new messages in 8 topics - digest

comp.lang.python
http://groups.google.com/group/comp.lang.python?hl=en

comp.lang.python@googlegroups.com

Today's topics:

* A question about making a sort-of-counter. - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/a3db486795b090f5?hl=en
* (a==b) ? 'Yes' : 'No' - 3 messages, 3 authors
http://groups.google.com/group/comp.lang.python/t/b985d5727945d2b4?hl=en
* Can't get odtwriter to work - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/969e1a79ce90f075?hl=en
* "Usability, the Soul of Python" - 6 messages, 5 authors
http://groups.google.com/group/comp.lang.python/t/e70d8f60e4ec72cc?hl=en
* psycopg2 / psycopg2.DataError: invalid input syntax for type timestamp with
time zone: - 4 messages, 3 authors
http://groups.google.com/group/comp.lang.python/t/7463ded0971425f8?hl=en
* Sometimes the python shell cannot recognize the presence of an attribute. -
3 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/85f282ceb4a5359d?hl=en
* sort array, apply rearrangement to second - 4 messages, 4 authors
http://groups.google.com/group/comp.lang.python/t/e282242a44ded215?hl=en
* Fetch files from mail in python - 2 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/ab1e159bcd418ac1?hl=en

==============================================================================
TOPIC: A question about making a sort-of-counter.
http://groups.google.com/group/comp.lang.python/t/a3db486795b090f5?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Mar 30 2010 3:12 pm
From: Justin Park


Thanks!
It works!

Justin.

Chris Rebert wrote:
> On Tue, Mar 30, 2010 at 2:31 PM, Justin Park <hp6@rice.edu> wrote:
>
>> Suppose I have a list.
>> a = list()
>> And suppose allowed digits as the element are 1,2,3,4,5.
>>
>> What can I do in order to iterate over all possible values for each element?
>> For instance, the sequence of the list I want to have would be
>> [1,1,1,1,1]
>> [1,1,1,1,2]
>> [1,1,1,1,3]
>>
>> ....
>>
>> [5,5,5,5,4]
>> [5,5,5,5,5]
>>
>> How can I make it happen?
>>
>
> allowed = range(1,6)
> length = 5
> for counter_tuple in product(allowed, repeat=length):
> counter_list = list(counter_tuple) # if you really need a list
> #do whatever with the counter value
>
> See the docs for itertools.product(); an example virtually identical
> to your situation is given:
> http://docs.python.org/library/itertools.html#itertools.product
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
>
>


== 2 of 2 ==
Date: Tues, Mar 30 2010 3:08 pm
From: Chris Rebert


On Tue, Mar 30, 2010 at 2:31 PM, Justin Park <hp6@rice.edu> wrote:
> Suppose I have a list.
> a = list()
> And suppose allowed digits as the element are 1,2,3,4,5.
>
> What can I do in order to iterate over all possible values for each element?
> For instance, the sequence of the list I want to have would be
> [1,1,1,1,1]
> [1,1,1,1,2]
> [1,1,1,1,3]
>
> ....
>
> [5,5,5,5,4]
> [5,5,5,5,5]
>
> How can I make it happen?

allowed = range(1,6)
length = 5
for counter_tuple in product(allowed, repeat=length):
counter_list = list(counter_tuple) # if you really need a list
#do whatever with the counter value

See the docs for itertools.product(); an example virtually identical
to your situation is given:
http://docs.python.org/library/itertools.html#itertools.product

Cheers,
Chris
--
http://blog.rebertia.com

==============================================================================
TOPIC: (a==b) ? 'Yes' : 'No'
http://groups.google.com/group/comp.lang.python/t/b985d5727945d2b4?hl=en
==============================================================================

== 1 of 3 ==
Date: Tues, Mar 30 2010 3:17 pm
From: Stephen Hansen


On 2010-03-30 13:16:00 -0700, Robert Fendt said:
>
> I find such a convoluted construct especially ugly in a language
> which I previously regarded as having a rather striking beauty
> of syntactical simplicity. The construct is superfluous,
> illogical, unelegant, and thus very un-pythonesque, IMHO. But of
> course that's just my $0.02.

In Python before list-comprehensions, I might have agreed with you.
Initially, I was quite resistant to list comprehensions as well. They
seemed ugly, and backwards, and why not just write a for loop?

Then I got used to them, and I find they are very elegant when used
properly. Sometimes you can do something ugly with them, but its
actually quite possible to write positively hideous Python even without
any of these new fancy shmancy features.

But, in the post-comprehension world, where one can do:

my_odds = [x for x in range(100) if x % 2 == 1]

Things have changed. I've now grown used to reading expressions like
that which seem a bit backwards, with the value being returned by an
expression is the left-most element. Its not an exact correlation
because they're answering different problems.

But having gotten used to list comprehensions, and actually quite
appreciating their elegance now, I find this reads very well:

is_odd = "odd" if x % 2 == 1 else "even"

In fact, it reads better then any of the other conditional expression
syntaxes people proposed back in the day, and a LOT better then what
was done before:

is_odd = x % 2 == 1 and "odd" or "even"

Even if the above falls a bit more in line with what other languages
usually do order-wise, this isn't other languages.

Now, none of this addresses your original argument of why not just use
a regular if statement.

I dunno, I often used "and/or" for simple expressions or defaults and
found it very convienent and made code more readable then the line and
whitespace inducing true if-statement. And so I'm glad to have
something even more readable and without the bug-prone and/or error.

Why not just use a for loop anytime you use a list comprehension? :)
Same question really applies.

--
--S

... p.s: change the ".invalid" to ".com" in email address to reply privately.

== 2 of 3 ==
Date: Tues, Mar 30 2010 4:18 pm
From: Lawrence D'Oliveiro


In message <7316f3d2-bcc9-4a1a-8598-
cdd5d41fd74b@k17g2000yqb.googlegroups.com>, Joaquin Abian wrote:

> (a==b) and 'YES' or 'NO'
>
> Yes, ugly

Why would you say that's ugly?

By the way, you don't need the parentheses.


== 3 of 3 ==
Date: Tues, Mar 30 2010 4:43 pm
From: Steve Holden


Lawrence D'Oliveiro wrote:
> In message <7316f3d2-bcc9-4a1a-8598-
> cdd5d41fd74b@k17g2000yqb.googlegroups.com>, Joaquin Abian wrote:
>
>> (a==b) and 'YES' or 'NO'
>>
>> Yes, ugly
>
> Why would you say that's ugly?
>
> By the way, you don't need the parentheses.

But at the same time, if you don't *absolutely know* you don't need the
parentheses then the parentheses are a very good idea, so I applaud the
example as Pythonic while agreeing that it's ugly. A conditional
expression seems more natural.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/


==============================================================================
TOPIC: Can't get odtwriter to work
http://groups.google.com/group/comp.lang.python/t/969e1a79ce90f075?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Mar 30 2010 3:20 pm
From: Grant Edwards


On 2010-03-30, Grant Edwards <invalid@invalid.invalid> wrote:

> I just installed odtwriter 1.3d on a Gentoo system running Python
> 2.6.4. I used the normal "python setup.py build" then "python
> setup.py install" method. But, odtwriter doesn't seem to work:
>
> rst2odt.py --help
>
> Traceback (most recent call last):
> File "/usr/bin/rst2odt.py", line 5, in <module>
> pkg_resources.run_script('odtwriter==1.3d', 'rst2odt.py')
> File "/usr/lib/python2.6/site-packages/pkg_resources.py", line 461, in run_script
> self.require(requires)[0].run_script(script_name, ns)
> File "/usr/lib/python2.6/site-packages/pkg_resources.py", line 1194, in run_script
> execfile(script_filename, namespace, namespace)
> File "/usr/lib/python2.6/site-packages/odtwriter-1.3d-py2.6.egg/EGG-INFO/scripts/rst2odt.py", line 21, in <module>
> from docutils.writers.odtwriter import Writer, Reader
> ImportError: No module named odtwriter
>
> I verified that I have docutils installed:
>
> Python 2.6.4 (r264:75706, Mar 1 2010, 10:33:43)
> [GCC 4.1.2 (Gentoo 4.1.2 p1.1)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import docutils
> >>> docutils
> <module 'docutils' from '/usr/lib/python2.6/site-packages/docutils/__init__.pyc'>
>
> What am I doing wrong?

I'm not sure what/if I did wrong when installing docutils/odtwriter,
but I managed to make it work by manually copying everything from

/usr/lib/python2.6/site-packages/odtwriter-1.3d-py2.6.egg/docutils/writers

to

/usr/lib/python2.6/site-packages/docutils/writers

I think that means that odtwriter's setup.py is old/broken, but I
don't know enough to fix it...

--
Grant Edwards grant.b.edwards Yow! I'm having a MID-WEEK
at CRISIS!
gmail.com

==============================================================================
TOPIC: "Usability, the Soul of Python"
http://groups.google.com/group/comp.lang.python/t/e70d8f60e4ec72cc?hl=en
==============================================================================

== 1 of 6 ==
Date: Tues, Mar 30 2010 3:32 pm
From: Robert Fendt


And thus spake "Alf P. Steinbach" <alfps@start.no>
Tue, 30 Mar 2010 13:40:22 +0200:

> <quote>
> From a usability standpoint, the braces go with the lines to print out the
> stanza rather than the for statement or the code after, so the following is best:
>
> for(i = 99; i > 0; ++i)
> {
> printf("%d slabs of spam in my mail!\n", i);
> printf("%d slabs of spam,\n", i);
> printf("Send one to abuse and Just Hit Delete,\n");
> printf("%d slabs of spam in my mail!\n\n", i + 1);
> }
> </quote>

I liked this one even more:

<quote>
One way of writing the same code in Python would be:

count = 99
while count > 0:
print u'%d slabs of spam in my mail!' % count
print u'%d slabs of spam,' % count
print u'Send one to abuse and Just Hit Delete,'
count += 1
print u'%d slabs of spam in my mail!' % count
print u''

The braces are gone, and with them the holy wars. Whatever brace
styles Python programmers may happen to use in languages with
braces, all the Python code looks the same, and while the major
brace styles illustrated above are a few of many ways the C code
could be laid out, there's only one real way to do it.
</quote>

Has the fact changed that Python does not care about (1) how
many characaters you use for indentation, (1a) you can use tabs
OR spaces, (2) indentation does not have to be consistent across
a module, (3) not even across a file, (4) even in nested blocks
and (5) you can even switch from spaces to tabs and back in the
same file? So much for 'all the Python code looks the same'.

In general I do not really see what qualifies the author for an
article on Python's usability. On the same site one can also
find a lot of things e.g. on intelligent design and creationism,
and the 'The Case For Uncreative Web Design' in which the author
advocates 'uncreative' (in the sense of non-distracting) web
design while at the same time showcasing quite the opposite:
suffice it to say I found most essays rather difficult to read
from a technical point of view, to say nothing about the content.

Regards,
Robert

== 2 of 6 ==
Date: Tues, Mar 30 2010 4:20 pm
From: Chris Rebert


On Tue, Mar 30, 2010 at 3:32 PM, Robert Fendt <no.spam@local.local> wrote:
> And thus spake "Alf P. Steinbach" <alfps@start.no>
> Tue, 30 Mar 2010 13:40:22 +0200:
>> <quote>
>>  From a usability standpoint, the braces go with the lines to print out the
>> stanza rather than the for statement or the code after, so the following is best:
>>
>> for(i = 99; i > 0; ++i)
>>      {
>>      printf("%d slabs of spam in my mail!\n", i);
>>      printf("%d slabs of spam,\n", i);
>>      printf("Send one to abuse and Just Hit Delete,\n");
>>      printf("%d slabs of spam in my mail!\n\n", i + 1);
>>      }
>> </quote>
>
> I liked this one even more:
>
> <quote>
> One way of writing the same code in Python would be:
>
> count = 99
> while count > 0:
>    print u'%d slabs of spam in my mail!' % count
>    print u'%d slabs of spam,' % count
>    print u'Send one to abuse and Just Hit Delete,'
>    count += 1
>    print u'%d slabs of spam in my mail!' % count
>    print u''
>
> The braces are gone, and with them the holy wars. Whatever brace
> styles Python programmers may happen to use in languages with
> braces, all the Python code looks the same, and while the major
> brace styles illustrated above are a few of many ways the C code
> could be laid out, there's only one real way to do it.
> </quote>
>
> Has the fact changed that Python does not care about (1) how
> many characaters you use for indentation, (1a) you can use tabs
> OR spaces, (2) indentation does not have to be consistent across
> a module, (3) not even across a file, (4) even in nested blocks
> and (5) you can even switch from spaces to tabs and back in the
> same file? So much for 'all the Python code looks the same'.

Since we're harping on block delimitation, I'll plug a post I did on
the subject a little while ago:
http://blog.rebertia.com/2010/01/24/of-braces-and-semicolons/

Hopefully it's more thorough than the OP's.

Cheers,
Chris


== 3 of 6 ==
Date: Tues, Mar 30 2010 4:23 pm
From: Lawrence D'Oliveiro


In message <20100331003241.47fa91f6@vulcan.local>, Robert Fendt wrote:

> The braces are gone, and with them the holy wars.

Let me start a new one. I would still put in some kind of explicit indicator
of the end of the grouping construct:

count = 99
while count > 0:
print u'%d slabs of spam in my mail!' % count
print u'%d slabs of spam,' % count
print u'Send one to abuse and Just Hit Delete,'
count += 1
print u'%d slabs of spam in my mail!' % count
print u''

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate