[Tutor] order loop from methos on class

2015-01-26 Thread jarod...@libero.it


Dear All,
What is the best way to loop on methos on a class?

class Rna():
def trim():
...
def align():
...

ena = Rna()

ena.trim()
ena.aling()



Thanks for any suggestion!!!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] order loop from methos on class

2015-01-26 Thread Steven D'Aprano
On Mon, Jan 26, 2015 at 11:48:52AM +0100, jarod...@libero.it wrote:
> 
> 
> Dear All,
> What is the best way to loop on methos on a class?

I do not understand the question. Can you explain in more detail?


> class Rna():
> def trim():
> ...
> def align():
> ...

Both methods need a "self" parameter:

def trim(self):
...
def align(self):
...


> ena = Rna()
> ena.trim()
> ena.aling()

ena.align()



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


Re: [Tutor] Tutor Digest, Vol 131, Issue 59 Order loop from methos on class (jarod...@libero.it)

2015-01-26 Thread Dino Bektešević
> Message: 1
> Date: Mon, 26 Jan 2015 11:48:52 +0100 (CET)
> From: "jarod...@libero.it" 
> To: tutor@python.org
> Subject: [Tutor] order loop from methos on class
> Message-ID:
> <2044122047.125321422269332108.JavaMail.defaultUser@defaultHost>
> Content-Type: text/plain; charset=UTF-8
>
>
>
> Dear All,
> What is the best way to loop on methos on a class?
>
> class Rna():
> def trim():
> ...
> def align():
> ...
>
> ena = Rna()
>
> ena.trim()
> ena.aling()
>
>
>
> Thanks for any suggestion!!!

Not quite sure where you're going with this, but to loop through the
methods of a class can be done and is dependent on the python
version...

For py3:

class Rna():
def trim():
print(1)
def align():
print(2)

ena = Rna()

for method in dir(Rna):
if method[:2] != "__":
ena.__getattribute__(method)()


the other way to do it is to:

for method in dir(Rna):
if method[:2] != "__":
getattr(ena, method)()

DO NOTICE HOW "WONKY" THIS IS. If you have a method that expects
input, or if you defined private methods (with __) this will either
skip them, or in worse case, if you cut out the if question, try to
call all the builtin methods (such as getattribute) and it will fail
because no proper input has been sent. This will only work for C style
"void" kind of methods.
Probably the best way to get them, though, do it is to:

import inspect

inspect.getmembers(Rna(), predicate=inspect.ismethod)

[('__init__', >), ('align', >), ('trim', >)]

This gets you tuples of (name, reference) kind of deal you can run
through. This does help to weed out some of the builtin methods, but
__init__ (or possibly __new__ ?) will still show up. If you need the
builtins as well you can ask for inspect.isbuiltin.

In python2 the first way is not going to work for sure, I don't think
there exist a __getattribute__, but the 2nd way (getattr) and the
inpect module will.

I do wonder why do you need this though. I've only once run into a
situation where I did something similar and that was _ONLY_ because I
was too lazy to write up proper classes and decompose the problem, for
what at the time seemed like a simple test run, so I ended up reading
a lot of properties from a file and did not want to name all of them
myself in the code. I ended up paying the price and wrote all the
necessary classes because it was hard to debug

Hopefully you'll think it through, don't be lazy it never pays off!
Best of luck,
Dino
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] R: Re: Tutor Digest, Vol 131, Issue 59 Order loop from methos on class (jarod...@libero.it)

2015-01-26 Thread jarod...@libero.it
Thanks so much!! Now I understood it is a stupid thing!
So, could you elpme to decompose the problem?
I initiazlizate the class with the file to use so all the function have all 
the parameters that I need.

At the moment I have in mind only to do this:
ena = Rna()
job1 = ena.trim()
job2 = ena.align()
It is no so elegant..



Any suggestion?

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


Re: [Tutor] Tutor Digest, Vol 131, Issue 59 Order loop from methos on class (jarod...@libero.it)

2015-01-26 Thread Dino Bektešević
2015-01-26 14:54 GMT+01:00 jarod...@libero.it :
> Thanks so much!! Now I understood it is a stupid thing!
> So, could you elpme to decompose the problem?
> I initiazlizate the class with the file to use so all the function have all
> the parameters that I need.
>
> At the moment I have in mind only to do this:
> ena = Rna()
> job1 = ena.trim()
> job2 = ena.align()
> It is no so elegant..
>
>
>
> Any suggestion?
>

Without seeing what exactly are you trying to do (I'm not a
chemist/biologist) I'm not sure. Any detail about what you're trying
to do would be helpful.
If you're bothered by having to call 2 methods except of 1 you could
always make another method that uses the two. It really depends on the
decision if you want to do the operations in place or not, that is
transform the original object through it's methods, or do you want to
return a new object of the same class.
In your example you write "job" as if you're dealing with threads. I
can't tell if you want job1 and job2 to be another different objects
(maybe of Rna class maybe not) or is it just that you don't understand
that you can also call a method on an object without having a variable
to hold a return value.

If you want to return another Rna object that is the same as the
original one, maybe something like this is what you need?

i.e.
import copy

class Rna():
def __init__(self):
self.trimmed = False
self.aligned = False

#NOT a part of the class
def trim(rna):
   temp = copy.copy(rna)
   temp.trimmed = True
   return temp

#in interpreter
 ena = Rna()
 ena.trimmed
False
 tena = trim(ena)
 ena.trimmed
False
 tena.trimmed
True

and both of ena and tena will be of class Rna. So you now have 2
different Rna objects.
You could also have it done in place, which saves memory and is more
natural and I'd recommend it:

class Rna():
   def __init__(self):
   self.trimmed=False
   self.aligned=False
   def trim(self):
self.trimmed=True
   def align(self):
   self.aligned=True
   def DoBoth(self):
self.trim()
self.align()
print("Done!")

#in the interpreter
 ena = Rna()
 ena.trimmed
False
 ena.aligned
False
 ena.DoBoth()
Done!
 ena.trimmed
True
 ena.aligned
True

This way you can do both without having to constantly write them
yourself... you issue 1 call which then calls various other methods
you have on the instance itself

Even that print isn't necessary, that's just there to show it
finished. You don't always have to have a variable to catch potential
output of a method. But maybe if you need/want to catch the output
maybe that output is an object of a different class(?) maybe it's a
tuple, or an array?

class Rna():
   def __init__(self):
   self.trimmed=False
   self.aligned=False
   def trim(self):
self.trimmed=True
   def align(self):
   self.aligned=True
   def DoBoth(self):
self.trim()
self.align()
print("Done! We return an list of solutions:")
return [1,2,3,4,5,6,7,8,9]

#in the interpreter
 ena = Rna()
 solutions = ena.DoBoth()
Done! We return an list of solutions:
 solutions
[1,2,3,4,5,6,7,8,9]

You could also add the solutions as an attribute to the object you
just did the calculation on:
def DoBoth(self):
self.trim()
self.align()
print("Done! We return an list of solutions:")
self.solutions =  [1,2,3,4,5,6,7,8,9]

#in the interpreter
 ena = Rna()
 ena.DoBoth()
Done! We return an list of solutions:
 ena.solutions
[1,2,3,4,5,6,7,8,9]

The better you describe WHAT you want and HOW you want it the better
people here will be able to help you


Best of luck,
Dino
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] R: Re: Tutor Digest, Vol 131, Issue 59 Order loop from methos on class (jarod...@libero.it)

2015-01-26 Thread Alan Gauld

On 26/01/15 13:54, jarod...@libero.it wrote:

I initiazlizate the class with the file to use so all the function have all
the parameters that I need.

At the moment I have in mind only to do this:
ena = Rna()
job1 = ena.trim()
job2 = ena.align()
It is no so elegant..


You originally posted a solution where you had a list of methods in an 
attribute called steps. That's a better solution than trying to

iterate over all the methods in the class.

for method in self.steps:
method()

But you don't really tell us what you are trying to do. So its hard to 
determine what an elegant solution is when we don't know the problem we 
are trying to solve.


It looks as if you are trying to create a workflow of some sort?
Does the flow always follow the same sequence - use a list of methods
Does the workflow vary depending on results - use a table where
 the return value of each method indicates which column to
 call next.
Is each method called only once or can it repeat steps?
How are errors to be handled?
Is timing critical?
Do you need to fully process one record at a time or can it be
batch oriented?

There are lots of questions to consider, all of which affect the
"best" solution.


hth
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


[Tutor] Inquiry regarding GSoC 2015

2015-01-26 Thread Arpit Agarwal
Hello,

I want to take part in GSoC 2015 under PSF. I am a beginner in python
and have started contributing and i am also registerd at github.
Please suggest as from which projects i should start for contributing
as i am beginner in python.

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


[Tutor] SQLAlchemy

2015-01-26 Thread Mike Nickey
Hey folks,

I'm working my way through Thinkful with python and I'm trying to get a
setup working. Part of this setup is using SQLAlchemy.
While I understand this is a python tutor list, would it be OK to ask
questions like 'how do I change my SQLAlchemy URL to include a user name'
here?

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


Re: [Tutor] SQLAlchemy

2015-01-26 Thread Martin A. Brown

Greetings Mike,

 : I'm working my way through Thinkful with python and I'm trying to 
 : get a setup working. Part of this setup is using SQLAlchemy. 
 : While I understand this is a python tutor list, would it be OK to 
 : ask questions like 'how do I change my SQLAlchemy URL to include 
 : a user name' here?

You could ask here about this (excellent) third-party library, but 
you will doubtless find a much richer pool of knowledge to draw from 
if you were to ask this question directly of the SQLAlchemy crowd 
[0].  There's a mailing list [1] or IRC [2].

I expect that you are calling sqlalchemy.engine.create_engine() at 
some point.  When you are doing that, you can/should pass your 
desired user credentials into this function.

  http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html

Good luck,

-Martin
 
 [0] http://www.sqlalchemy.org/support.html
 [1] https://groups.google.com/forum/#!forum/sqlalchemy
 [2] 'The IRC channel is on the Freenode network as #sqlalchemy.'

-- 
Martin A. Brown
http://linux-ip.net/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Inquiry regarding GSoC 2015

2015-01-26 Thread Danny Yoo
We (the Tutor list) are not a mentor for GSoC.  You'll want to talk to
the right organization for this, because I don't think any of us here
have any direct experience with that program.

GSoC has a Frequently Asked Questions that you should read:


http://www.google-melange.com/gsoc/document/show/gsoc_program/google/gsoc2015/help_page#whatis

There are mailing lists specific to GSoC.  See:
https://www.google-melange.com/gsoc/document/show/gsoc_program/google/gsoc2015/about_page
for details.


On Mon, Jan 26, 2015 at 7:56 AM, Arpit Agarwal  wrote:
> Hello,
>
> I want to take part in GSoC 2015 under PSF. I am a beginner in python
> and have started contributing and i am also registerd at github.
> Please suggest as from which projects i should start for contributing
> as i am beginner in python.
>
> thanks.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 131, Issue 59 Order loop from methos on class (jarod...@libero.it)

2015-01-26 Thread Sydney Shall

On 26/01/2015 14:41, Dino Bektešević wrote:

2015-01-26 14:54 GMT+01:00 jarod...@libero.it :

Thanks so much!! Now I understood it is a stupid thing!
So, could you elpme to decompose the problem?
I initiazlizate the class with the file to use so all the function have all
the parameters that I need.

At the moment I have in mind only to do this:
ena = Rna()
job1 = ena.trim()
job2 = ena.align()
It is no so elegant..



Any suggestion?



Without seeing what exactly are you trying to do (I'm not a
chemist/biologist) I'm not sure. Any detail about what you're trying
to do would be helpful.
If you're bothered by having to call 2 methods except of 1 you could
always make another method that uses the two. It really depends on the
decision if you want to do the operations in place or not, that is
transform the original object through it's methods, or do you want to
return a new object of the same class.
In your example you write "job" as if you're dealing with threads. I
can't tell if you want job1 and job2 to be another different objects
(maybe of Rna class maybe not) or is it just that you don't understand
that you can also call a method on an object without having a variable
to hold a return value.

If you want to return another Rna object that is the same as the
original one, maybe something like this is what you need?

i.e.
import copy

class Rna():
 def __init__(self):
 self.trimmed = False
 self.aligned = False

#NOT a part of the class
def trim(rna):
temp = copy.copy(rna)
temp.trimmed = True
return temp

#in interpreter

ena = Rna()
ena.trimmed

False

tena = trim(ena)
ena.trimmed

False

tena.trimmed

True

and both of ena and tena will be of class Rna. So you now have 2
different Rna objects.
You could also have it done in place, which saves memory and is more
natural and I'd recommend it:

class Rna():
def __init__(self):
self.trimmed=False
self.aligned=False
def trim(self):
 self.trimmed=True
def align(self):
self.aligned=True
def DoBoth(self):
 self.trim()
 self.align()
 print("Done!")

#in the interpreter

ena = Rna()
ena.trimmed

False

ena.aligned

False

ena.DoBoth()

Done!

ena.trimmed

True

ena.aligned

True

This way you can do both without having to constantly write them
yourself... you issue 1 call which then calls various other methods
you have on the instance itself

Even that print isn't necessary, that's just there to show it
finished. You don't always have to have a variable to catch potential
output of a method. But maybe if you need/want to catch the output
maybe that output is an object of a different class(?) maybe it's a
tuple, or an array?

class Rna():
def __init__(self):
self.trimmed=False
self.aligned=False
def trim(self):
 self.trimmed=True
def align(self):
self.aligned=True
def DoBoth(self):
 self.trim()
 self.align()
 print("Done! We return an list of solutions:")
 return [1,2,3,4,5,6,7,8,9]

#in the interpreter

ena = Rna()
solutions = ena.DoBoth()

Done! We return an list of solutions:

solutions

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

You could also add the solutions as an attribute to the object you
just did the calculation on:
def DoBoth(self):
 self.trim()
 self.align()
 print("Done! We return an list of solutions:")
 self.solutions =  [1,2,3,4,5,6,7,8,9]

#in the interpreter

ena = Rna()
ena.DoBoth()

Done! We return an list of solutions:

ena.solutions

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

The better you describe WHAT you want and HOW you want it the better
people here will be able to help you


Best of luck,
Dino
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



I do not know exactly what the OP wants to do, but it sounds like he is 
aligning RNA sequences.
At this stage I would suggest that he studies the possibilities of the 
BioPython package and the programs available at the NLM, USA or the 
Sanger Lab in Cambridge, England.

But I am only a beginner myself, so I might be way off track.
--
Sydney
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] R: Tutor Digest, Vol 131, Issue 60 order loop from methos on class

2015-01-26 Thread jarod...@libero.it
dear all,
thanks so much!! I  explain better  my problem :http://pastebin.com/iiPZMi2U

this is my workflow. Take my data from readset an create  a command list
ena = Rnaseq()
The order of the self.step are the order I wan to to execute my analisys. 
So this is the order:
In [160]: ena.show()
1- trimmomatic
2- merge_trimmomatic_stats
3- star
4- picard_sort_sam
5- rnaseqc
6- wiggle
7- cufflinks
8- cuffquant
9- gq_seq_utils_exploratory_analysis_rnaseq

First aim is to write a code to write all the pipeline .
up to now I use this:
job1 = ena.trimming()
ena.prepare_run(job1)
ena.prepare_run(job2)
..
the other aim is to selcet a range number to execute i.e. 2-9 or 1-4...

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


Re: [Tutor] R: Tutor Digest, Vol 131, Issue 60 order loop from methos on class

2015-01-26 Thread Danny Yoo
Hello,

> The order of the self.step are the order I wan to to execute my analisys.
> So this is the order:
> In [160]: ena.show()
> 1- trimmomatic
> 2- merge_trimmomatic_stats
> 3- star
> 4- picard_sort_sam
> 5- rnaseqc
> 6- wiggle
> 7- cufflinks
> 8- cuffquant
> 9- gq_seq_utils_exploratory_analysis_rnaseq
>
> First aim is to write a code to write all the pipeline .
> up to now I use this:
> job1 = ena.trimming()
> ena.prepare_run(job1)
> ena.prepare_run(job2)
> ..
> the other aim is to selcet a range number to execute i.e. 2-9 or 1-4...


In this case, yes, you probably want to explicitly represent the
sequence of steps as an explicit list of operators.

I think you may have wanted somehow to get this ordered sequence "for
free" by iterating over the class's methods, perhaps through a dir().
However, trying to get this ordering from the class via method
iteration is unreliable because, underneath the service, the iteration
is walking through a dictionary, and dictionaries do not give us a
deterministic ordering: the ordering is allowed to change on us
between program runs.

And in fact, in certain environments, dictionary iteration is
_guaranteed_ to give us a different ordering on every program run.
See: https://docs.python.org/2/using/cmdline.html#envvar-PYTHONHASHSEED

Ultimately, this means that if we want an ordered sequence, we can't
depend on dictionary iteration.  Lists are what we want.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Overlay Quiver Plot on Contours

2015-01-26 Thread Felisha Lawrence
Hello,
I have the following script:

import netCDF4
import numpy as np
import matplotlib.pyplot as plt
import pylab

ncfile = netCDF4.Dataset('30JUNE2012_0300UTC.cdf', 'r')
dbZ = ncfile.variables['MAXDBZF']

data = dbZ[0,0]
data.shape
x = np.array([0,10,11])
y = np.array([0,15,16])
X,Y = np.meshgrid(x,y)
u = 5*X
v = 5*Y

plt.figure()
plt.rcParams['figure.figsize'] = (12.0,8.0)
c = plt.contourf(data, 20, cmap='jet')
plt.hold(True)
q = plt.quiver(X,Y,u,v,angles='xy',scale=1000,color='r')
p = plt.quiverkey(q,1,16.5,50,"50 m/s",coordinates='data',color='r')

plt.colorbar()
plt.show()

and I am getting this error:

TypeError: You must first set_array for mappable

Can anyone provide guidance on how you overlay quiver plots on top of
contours?


Please and thanks


Felisha Lawrence


-- 
Felisha Lawrence
Howard University Program for Atmospheric Sciences(HUPAS), Graduate Student

NASA URC/BCCSO Graduate Fellow
NOAA NCAS Graduate Fellow
Graduate Student Association for Atmospheric Sciences(GSAAS), Treasurer
(240)-535-6665 (cell)
felisha.lawre...@gmail.com (email)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Overlay Quiver Plot on Contours

2015-01-26 Thread Alan Gauld

On 26/01/15 23:24, Felisha Lawrence wrote:

Hello,
I have the following script:

import netCDF4
import numpy as np
import matplotlib.pyplot as plt
import pylab

...

and I am getting this error:

TypeError: You must first set_array for mappable

Can anyone provide guidance on how you overlay quiver plots on top of
contours?


This list is for newcomers to Python and its standard
library. This looks like a fairly specific SciPy/Numpy,
and especially matplotlib type question.

You would probably be better off asking on the SciPy
forums/lists.

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Overlay Quiver Plot on Contours

2015-01-26 Thread Danny Yoo
>> and I am getting this error:
>>
>> TypeError: You must first set_array for mappable
>>
>> Can anyone provide guidance on how you overlay quiver plots on top of
>> contours?
>
>
> This list is for newcomers to Python and its standard
> library. This looks like a fairly specific SciPy/Numpy,
> and especially matplotlib type question.
>
> You would probably be better off asking on the SciPy
> forums/lists.


Agree with Alan.  You'll get better help from people who know the
library.  Very few of us here do things with SciPy.


Just to add: please add more context to your error message reports,
regardless of who you're asking help from.

To include the single line:

###
TypeError: You must first set_array for mappable
###

is somewhat helpful: it does have some information.


But unless you've suppressed error reporting, you should be getting
significantly more information from the error message.

I did a quick web search for your error message, and came up with:


http://stackoverflow.com/questions/6600579/colorbar-for-matplotlib-plot-surface-command

which might look reasonably close to what you're seeing.  And note
how, in that message, they include a full stack trace of the problem,
which looks like:

#
Traceback (most recent call last):
  File "", line 1, in 
  File 
"C:\Python26\lib\site-packages\spyderlib\widgets\externalshell\startup.py",
line 122, in runfile
execfile(filename, glbs)
 File "C:\Documents and Settings\mramacha\My
Documents\Python\Candela\test.py", line 22, in 
fig.colorbar(surf, shrink=0.5, aspect=5)
  File "C:\Python26\lib\site-packages\matplotlib\figure.py", line
1104, in colorbar
cb = cbar.Colorbar(cax, mappable, **kw)
  File "C:\Python26\lib\site-packages\matplotlib\colorbar.py", line
706, in __init__
mappable.autoscale_None() # Ensure mappable.norm.vmin, vmax
  File "C:\Python26\lib\site-packages\matplotlib\cm.py", line 261, in
autoscale_None
raise TypeError('You must first set_array for mappable')
TypeError: You must first set_array for mappable
#

This is the sort of thing that is a *joy* to see when debugging.  If I
don't have a good stack trace when trying to diagnose a problem, I am
not as happy, because I don't have my hand on any useful context to
work with.  It's like working in the dark.


In your case, I can't tell for sure if your scenario matches the
above, because the error message information content is low enough to
raise doubts.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] functions first?

2015-01-26 Thread Alex Kleider
Please correct me if I am wrong, but I've assumed that it is proper to 
define all functions before embarking on the main body of a program.
I find myself breaking this rule because I want to set the default 
values of some named function parameters based on a configuration file 
which I have to first read, hence the need to break the rule.

..so my question is "is this acceptable" or "is there a better way?"
thks
Alex
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] functions first?

2015-01-26 Thread Ben Finney
Alex Kleider  writes:

> Please correct me if I am wrong, but I've assumed that it is proper to
> define all functions before embarking on the main body of a program.

I would say rather that as much code as possible should be in small
well-defined functions, with the “main body” a tiny and trivial call to
a function.

That way, all the code is easily tested by unit testing tools.

> I find myself breaking this rule because I want to set the default
> values of some named function parameters based on a configuration file
> which I have to first read, hence the need to break the rule.

Module-level constants are fine, and they obviously need to be bound
before the definition of the function which uses them for parameter
defaults.

But if they're not constants – as implied by your statement you read
them from a configuration file – then they should not be in the function
definition, because reading the configuration file should itself be
encapsulated in a well-tested function.

-- 
 \  “It is the integrity of each individual human that is in final |
  `\examination. On personal integrity hangs humanity's fate.” |
_o__)   —Richard Buckminster Fuller, _Critical Path_, 1981 |
Ben Finney

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