Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > A question about readability

Reply
Thread Tools

A question about readability

 
 
Marco
Guest
Posts: n/a
 
      12-07-2012
Hi all, do you think this code:

$ more myscript.py
for line in open('data.txt'):
result = sum(int(data) for data in line.split(';'))
print(result)

that sums the elements of the lines of this file:

$ more data.txt
30;44;99;88
11;17;16;50
33;91;77;15
$ python3.3 myscript.py
261
94
216

is explicit enough? Do you prefer a clearer solution?
Thanks in advance, Marco
--
Marco
 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      12-07-2012
In article <k9srup$kc7$(E-Mail Removed)>,
Marco <(E-Mail Removed)> wrote:

> Hi all, do you think this code:
>
> $ more myscript.py
> for line in open('data.txt'):
> result = sum(int(data) for data in line.split(';'))
> print(result)


That sum() line is a bit of a mouthful. I would refactor it into
something like this:

> for line in open('data.txt'):
> fields = line.split(';')
> print sum(int(i) for i in fields)


It's the same number of lines, but I think it's easier to read.

BTW, are you using Python 2 or 3? It helps when asking these kinds of
questions to let people know. I assumed 2 in my answer above.
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-07-2012
On Fri, 07 Dec 2012 14:46:03 +0100, Marco wrote:

> Hi all, do you think this code:
>
> $ more myscript.py
> for line in open('data.txt'):
> result = sum(int(data) for data in line.split(';'))
> print(result)

[...]
> is explicit enough? Do you prefer a clearer solution? Thanks in advance,


It's perfectly fine for such a simple script. It is readable and explicit.


--
Steven
 
Reply With Quote
 
rusi
Guest
Posts: n/a
 
      12-07-2012
On Dec 7, 6:46*pm, Marco <(E-Mail Removed)> wrote:
> Hi all, do you think this code:
>
> $ more myscript.py
> for line in open('data.txt'):
> * * *result = sum(int(data) for data in line.split(';'))
> * * *print(result)
>
> that sums the elements of the lines of this file:
>
> $ more data.txt
> 30;44;99;88
> 11;17;16;50
> 33;91;77;15
> $ python3.3 myscript.py
> 261
> 94
> 216
>
> is explicit enough? Do you prefer a clearer solution?
> Thanks in advance, Marco
> --
> Marco


Interpreting your question as a general question of stylistics, my
experience is that a 3 line script often becomes a 10 line or a 50
line script at which point the direct printing will have to be
modified to create an internal data structure.

So on the whole I find it expedient to start with that assumption and
write it as:

def linesums(file):
return [sum(int(i) for i in l.split(';')) for l in open(file, 'r')]

Whether this one-liner is readable or not and how to make it more so
etc is probably bikeshedding.

More important questions are for example:
- Should you protect the open with a try
- When to list and when to generate(or)

All these are nice features of python but can lead to over-engineered
code
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: A question about readability rusi Python 0 12-10-2012 10:26 AM
Another style/readability question... jackassplus@gmail.com C Programming 3 03-17-2010 10:04 AM
Indentation for code readability DE Python 16 03-31-2007 02:05 PM
'academic' problem ( speed/memory efficiency vs. human readability and easy design ) burningsunorama@gmail.com C++ 9 07-28-2006 05:22 PM
Is this just for readability to the developer? python David Stockwell Python 3 06-02-2004 01:54 PM



Advertisments