Re: [Tutor] Feedback on coding style

2010-12-09 Thread Albert-Jan Roskam
Hi,

Re: coding style, I can *really* recommend the book 'Code Complete' 
(http://cc2e.com/). It doesn't focus on Python specifically, but it's a 
wonderful book. You can find a pdf checklist of the book if you Google a bit.
 Cheers!!
Albert-Jan 


~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a fresh water system, and public health, what have 
the 
Romans ever done for us?
~~ 





From: Dave Angel da...@ieee.org
To: howit...@archlinux.us
Cc: tutor@python.org
Sent: Wed, December 8, 2010 11:01:05 PM
Subject: Re: [Tutor] Feedback on coding style

On 01/-10/-28163 02:59 PM, howit...@archlinux.us wrote:
 Hi.
 
 For the past week, I've been following an online Python guide named:
 'Learn Python the Hard Way'. I'm very happy with it as it gives a lot of
 freedom to explore.
 
 However, due to this I have no idea if I'm thinking the right way. That's
 why I've attached a script of mine I've been working on all day.
 
 It works a 100%, but I'm afraid I've made very bad choices concerning
 design and coding style. (If it could've been much simpler, if there are
 glaring mistakes, poor methods, ..)
 
 Could anyone be so friendly as to offer a bit of feedback, to a newbie?
 
 PS: The script very simple accepts 2 arguments from the commandline.
     First arg being the number to which should be counted,
     second arg being the interval.
 
 Thank you,
 Adrian

I agree with Hugo, probably on all his points.  But to help you fix it, I think 
I can help you a bit as well.

First is I think you're misunderstanding the purpose of a function.  A function 
should be a self-contained piece of code that gets called, and that returns 
when 
done.  You're using them mostly as a way to implement what BASIC used to have 
as 
a GOTO.  For example, when a loop is needed, you do it by calling another 
function which calls the first one.  That is called recursion if it's done on 
purpose, and a bug if it happens by accident.

Second is that you're misusing global variables.  A function needs to be called 
with those values it needs to do its work, and it needs to return the results 
of 
that work.  Very seldom should those things be globals.

Start with the first function.  You declare it with maxn and incr as 
parameters, 
and you call it correctly.  But it also uses the globals, rather than using the 
ones passed in.

Then the function ask_change().  You should be passing it the two arguments, 
and 
getting back modified arguments as return values.  And the function shouldn't 
call the_loop() itself, it should just get the new values.

jibjab_result() is trying to be a loop, by effectively calling itself, through 
choice_result().  If you need a loop, just write one.  And that loop will 
probably be in ask_change(), without needing any other functions inside.

It's good to decompose a problem into functions, but you're not using functions 
in the way they're designed.  For small problems, this can work, but as the 
problems get more complex, you'll be forced to change your habits.

DaveA



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Feedback on coding style

2010-12-09 Thread Alan Gauld

Albert-Jan Roskam fo...@yahoo.com wrote


Re: coding style, I can *really* recommend the book 'Code Complete'
(http://cc2e.com/). It doesn't focus on Python specifically, but 
it's a

wonderful book.


I'll second that. I haven't read the 2nd Ed but the first edition was 
one
of the few (5 or 6?) books I've read about computing that actually 
changed

the way I write code.

It's not an absolute beginners book, you need a bit of experience in 
writing
reasonable length programs - more than 100 lines say, to really 
appreciate

why some things are recommended, but for the right audience it is,
as Albert says, a wonderful book..

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Feedback on coding style

2010-12-09 Thread howit...@archlinux.us
Thanks a lot for the very friendly and constructive comments.

I can't wait to start overhauling the script with the
comments received here.

If I could repay the friendliness to anyone, let me know.
Adrian
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Feedback on coding style

2010-12-08 Thread howitzer
Hi.

For the past week, I've been following an online Python guide named:
'Learn Python the Hard Way'. I'm very happy with it as it gives a lot of
freedom to explore. 

However, due to this I have no idea if I'm thinking the right way. That's 
why I've attached a script of mine I've been working on all day.

It works a 100%, but I'm afraid I've made very bad choices concerning
design and coding style. (If it could've been much simpler, if there are
glaring mistakes, poor methods, ..)

Could anyone be so friendly as to offer a bit of feedback, to a newbie?

PS: The script very simple accepts 2 arguments from the commandline.
First arg being the number to which should be counted,
second arg being the interval.

Thank you,
Adrian
#!/usr/bin/env python
# Made with python2.7

from sys import argv
from sys import exit

script, maxnumber, increment = argv

maxnumber = int(maxnumber)
increment = int(increment)

jibjab = 0

numbers = []

askchange = 1 

# The script goes as following:
# When you start it and give two arguments, check_OP_size
# checks wether you won't get too much output.

# If you PASS the test, you're immediately sent to the main
# function (the_loop, which is performing the actualy goal
# of the script). If you FAIL you're sent to ask_change.

# ask_change() notifies you that you're going to get
# a lot of output and optionally lets you change your arguments.

# You're then sent to choice_result which check if your response
# is a valid response (e.g. Y/N). If it's not, you're sent to
# jibjab_result that gives you 3 chances to type in Y/N. 

# If you fail typing Y/N three times, the app quits.
# If you make it however, you're sent to: change_vars.

# Of course, change_vars also immediately sends you to check_OP_size
# to check for OP size.

# ANY COMMENTS WELCOME AT howit...@archlinux.us

def check_OP_size(maxn, incr):
global choice, maxnumber, increment
if maxn / incr  10:
ask_change()
if maxn / incr = 10:
the_loop(maxnumber, increment)

def ask_change():
global choice, maxnumber, increment
print You will recieve a lot of output.\nDo you wish to change your data?
choice = raw_input(Y/N: )
choice_result()

def jibjab_result():
global choice, jibjab
if jibjab  2:
jibjab += 1
choice = raw_input(Y/N: )
choice_result()
elif jibjab = 2:
print Idiot.
exit()

def choice_result():
global choice, maxnumber, increment
if choice == Y:
change_vars()
elif choice == N:
the_loop(maxnumber, increment)
else:
jibjab_result()

def change_vars():
global maxnumber, increment
maxnumber = raw_input(Give a new max number: )
increment = raw_input(Give a new increment: )
maxnumber = int(maxnumber)
increment = int(increment)
check_OP_size(maxnumber, increment)
#the_loop(maxnumber, increment)

def the_loop(maxn, incr):
Advances a number with the supplied increment untill it has reached the passed maximum number.
maxn = int(maxn)
incr = int(incr)
i = 0
#for i in range( 0, maxn, incr):
while i  maxn:
print At the top i is %d % i
numbers.append(i)

i = i + incr
print Numbers now: , numbers
print At the bottom i is %d % i

check_OP_size(maxnumber, increment)

print The numbers: 
for num in numbers:
print num,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Feedback on coding style

2010-12-08 Thread Hugo Arts
On Wed, Dec 8, 2010 at 8:11 PM,  howit...@archlinux.us wrote:
 Hi.

 For the past week, I've been following an online Python guide named:
 'Learn Python the Hard Way'. I'm very happy with it as it gives a lot of
 freedom to explore.

 However, due to this I have no idea if I'm thinking the right way. That's
 why I've attached a script of mine I've been working on all day.

 It works a 100%, but I'm afraid I've made very bad choices concerning
 design and coding style. (If it could've been much simpler, if there are
 glaring mistakes, poor methods, ..)

 Could anyone be so friendly as to offer a bit of feedback, to a newbie?


I like that you've divided the program up into functions, this is A
Good Thing(TM). It made it much easier for me to understand what is
going on, so keep doing that.

little nitpick (maybe not *so* little), your functions are mutually
recursive. This is a difficult word that basically means that they are
all calling each other in a cycle that is potentially endless. In this
case, the cycle goes like this: check_OP_size - ask_change -
choice_result - change_vars - check_OP_size. You see that if I keep
inputting the right things, I can go through this cycle endlessly. In
this case, it's not a big problem, because no one is likely to do that
for long enough to cause problems, but if another part of your program
is doing the inputting, it can go wrong quickly. Why? Because
functions can't call each other indefinitely. Try running this
example:

def f(): f()
f()

This is a function that keeps calling itself, the simplest possible
cycle. It gives you an error maximum recursion depth exceeded. There
are reasons for this, and you should learn about recursion and the
call stack at some point, but that is another long essay. For now it
is enough to know that recursion is something to be careful with, as
there is a limit to how many functions can call each other.

Second, you're using globals too much. Functions should rely as much
as possible on their arguments, and nothing else. I can count the
number of times I needed the global statement on one hand, and I've
programmed a lot of python. Globals make your program difficult to
follow, because you can't clearly see where the data is coming from
and going to. Rather than having your functions communicate through
global variables, you should have them communicate through function
arguments and return values. See if you can write a version without
the global statements.

There are a few other little things. The two if statements in
check_OP_size can be rewritten into an if/else statement, since the
two checks are complementary (one of them will always be true). The
while loop in the_loop is a little overcomplicated (you had the right
idea with the commented out range call).

Lastly, the program could've been written a lot more succinctly, but
this will come with time and practice. Writing code is hard, and
getting a feel for how to divide up your functions and how to express
yourself in python most naturally will take time and practice. I'd say
you're on the right track, and if you get rid of all the globals
you'll be well on your way to becoming an excelling python programmer.
If you need any more help, let us know.

Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Feedback on coding style

2010-12-08 Thread Dave Angel

On 01/-10/-28163 02:59 PM, howit...@archlinux.us wrote:

Hi.

For the past week, I've been following an online Python guide named:
'Learn Python the Hard Way'. I'm very happy with it as it gives a lot of
freedom to explore.

However, due to this I have no idea if I'm thinking the right way. That's
why I've attached a script of mine I've been working on all day.

It works a 100%, but I'm afraid I've made very bad choices concerning
design and coding style. (If it could've been much simpler, if there are
glaring mistakes, poor methods, ..)

Could anyone be so friendly as to offer a bit of feedback, to a newbie?

PS: The script very simple accepts 2 arguments from the commandline.
First arg being the number to which should be counted,
second arg being the interval.

Thank you,
Adrian


I agree with Hugo, probably on all his points.  But to help you fix it, 
I think I can help you a bit as well.


First is I think you're misunderstanding the purpose of a function.  A 
function should be a self-contained piece of code that gets called, and 
that returns when done.  You're using them mostly as a way to implement 
what BASIC used to have as a GOTO.  For example, when a loop is needed, 
you do it by calling another function which calls the first one.  That 
is called recursion if it's done on purpose, and a bug if it happens by 
accident.


Second is that you're misusing global variables.  A function needs to be 
called with those values it needs to do its work, and it needs to return 
the results of that work.  Very seldom should those things be globals.


Start with the first function.  You declare it with maxn and incr as 
parameters, and you call it correctly.  But it also uses the globals, 
rather than using the ones passed in.


Then the function ask_change().  You should be passing it the two 
arguments, and getting back modified arguments as return values.  And 
the function shouldn't call the_loop() itself, it should just get the 
new values.


jibjab_result() is trying to be a loop, by effectively calling itself, 
through choice_result().  If you need a loop, just write one.  And that 
loop will probably be in ask_change(), without needing any other 
functions inside.


It's good to decompose a problem into functions, but you're not using 
functions in the way they're designed.  For small problems, this can 
work, but as the problems get more complex, you'll be forced to change 
your habits.


DaveA



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor