Re: [Tutor] 2 & 4 or more spaces per indentation level..

2009-01-16 Thread Alan Gauld

"Eric Dorsey"  wrote

Working in IDLE on Windows Vista, I have one program that I set to 
have 2
character spacing (due to the levels of if's and while's going on --  
later
my brother called this a bit of a code smell, ie. logic shouldn't go 
that
deep, it should be broken out into separate functions instead. 
Thoughts on
that are welcome to, do any of you feel logic should only go so many 
layers

deep?),


Deeply nested logic tends to be hard to debug and hard to terst 
thoroughly
so yes, this is considered a bad smell. Refactoring the inner parts of 
a loop
into a function is often a good way to deal with this but it may have 
a
performamnce impact if its a critical bit of code so its not a cure 
all.


Usually its possioble to reduce nesting by using a different logic 
flow

(De Morgans rules often help here) or by using higher level structures
like list comprehensions instead of loops. In fact functional 
programming
techniques in general can help minimise nesting of code - this is one 
of

the claimed advantages of functional languages, that they lead to less
deeply nested programs...


I copied some code from the 2 spacing program to another I'm writing
currently which has the default 4, and it got things kind of screwy 
with

spacing.


This can happen if you don't have the code in functions. Its easy to
copy a function but copying arbitrary lines out of a function is much
more risky.

but I was wondering, if this happened on a much bigger scale, or you 
were
say, pasting code in from some example where they used a different 
spacing
than you, is there a simple/good/smart way to get it all back to the 
4

spacing default?


There are many tools around that can do this kind of thing - indent,
tab nanny, untabify, format etc... Pick your own favourite.

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2 & 4 or more spaces per indentation level..

2009-01-16 Thread spir
Le Fri, 16 Jan 2009 09:11:06 +0530,
Senthil Kumaran  a écrit :


> 
> >they used a different spacing than you, is there a simple/good/smart
> >way to get it all back to the 4 spacing default? Or if for example I
> 
> Rule #0 is Never mix tabs and spaces.
> Rule #1 is use a standard spacing through out the code.
> 
> And when copying from a certain space-setting indentation to another,
> the modular script will still work fine. You can use some global
> find/replace mechanism to do suit your setting.
> For eg. I remember doing :%s/\ \ /\ \ \ \ /g
> 
The simplest method I guess is to first copy the code into a new 
file/doc/editor tab. Change there
to your personal indent width using whatever tool your editor provides, or by 
global
search/replace. Then only paste into your code.
denis

--
la vida e estranya
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] "Pointer" to a function? Storing a function as an object property? Passing arguments by value/by reference?

2009-01-16 Thread Vicent
I am posting this question to two Python forums: Tutor and Python(x,y).

In the case of Tutor [1], I think it's the right place to ask questions for
a newbie like me.

In the case of Python(x,y) Discussion Group [2], I am posting also because I
think I am addressing a specific group of Python users that probably
previously dealed with the same problems I do now. Anyway, let me know if
it's a good idea to keep on doing this in the future.

This question is about how to define classes or objects (or data structures)
I need to use, and how to do it in an efficient way.

I want to define an object or data structure called "Problem".

That "problem" has to contain, somehow, a property or element called
"function" which, in fact, I would like it to be a function, or a "pointer"
to a function.

For example, if  "prob"  is a "Problem" object, I would like to be able to
do something like this:


# call the function in prob, and store the result in "x" :

x = prob.function( arguments/variables required by the function )


Does it makes any sense? Which would it be the right (meaning efficient but
still object-oriented-programming-compliant) way to do it?

I mean, if I store "a whole function" within each "Problem" object (assuming
it can be done in Python), each Problem object would be consuming lot of
memory (wouldn't it?). Maybe it would be better just to store a kind of
"pointer" to the function within the "problem" object, so the object would
be "lighter". The function would be then defined outside the object, as
usual.

Can you give me some hint about this?

By the way, I have another related question. In C you can pass arguments to
a function by value or by reference. Is there any equivalence in Python to
that approach? How is the usual way to pass function arguments in Python?

I guess I'll discover many of this things by my own when "playing arround"
with Python, but some orientation will be welcomed.


[1] http://mail.python.org/mailman/listinfo/tutor

[2] http://groups.google.es/group/pythonxy


Thank you very much in advance.

-- 
Vicent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Pointer" to a function? Storing a function as an object property? Passing arguments by value/by reference?

2009-01-16 Thread Andre Engels
On Fri, Jan 16, 2009 at 1:51 PM, Vicent  wrote:
> I am posting this question to two Python forums: Tutor and Python(x,y).
>
> In the case of Tutor [1], I think it's the right place to ask questions for
> a newbie like me.
>
> In the case of Python(x,y) Discussion Group [2], I am posting also because I
> think I am addressing a specific group of Python users that probably
> previously dealed with the same problems I do now. Anyway, let me know if
> it's a good idea to keep on doing this in the future.
>
> This question is about how to define classes or objects (or data structures)
> I need to use, and how to do it in an efficient way.
>
> I want to define an object or data structure called "Problem".
>
> That "problem" has to contain, somehow, a property or element called
> "function" which, in fact, I would like it to be a function, or a "pointer"
> to a function.
>
> For example, if  "prob"  is a "Problem" object, I would like to be able to
> do something like this:
>
>
> # call the function in prob, and store the result in "x" :
>
> x = prob.function( arguments/variables required by the function )
>
>
> Does it makes any sense? Which would it be the right (meaning efficient but
> still object-oriented-programming-compliant) way to do it?
>
> I mean, if I store "a whole function" within each "Problem" object (assuming
> it can be done in Python), each Problem object would be consuming lot of
> memory (wouldn't it?). Maybe it would be better just to store a kind of
> "pointer" to the function within the "problem" object, so the object would
> be "lighter". The function would be then defined outside the object, as
> usual.
>
> Can you give me some hint about this?
In fact, this can be done in Python very easily, see the following
interactive session:

>>> class test(object):
   pass

>>> test1 = test()
>>> test2 = test()
>>> def double(x):
   return x+x

>>> def square(x):
   return x*x

>>> test1.f = double
>>> test2.f = square
>>> test1.f(5)
10
>>> test2.f(5)
25
>>>




-- 
André Engels, andreeng...@gmail.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Pointer" to a function? Storing a function as an object property? Passing arguments by value/by reference?

2009-01-16 Thread Kent Johnson
On Fri, Jan 16, 2009 at 7:51 AM, Vicent  wrote:
> I want to define an object or data structure called "Problem".
>
> That "problem" has to contain, somehow, a property or element called
> "function" which, in fact, I would like it to be a function, or a "pointer"
> to a function.
>
> For example, if  "prob"  is a "Problem" object, I would like to be able to
> do something like this:
>
>
> # call the function in prob, and store the result in "x" :
>
> x = prob.function( arguments/variables required by the function )

As André showed, this is trivially easy in Python. To expand on his
exampl...functions are "first-class objects" in Python. That means
that a function is an object that can be used in the same way as other
values, i.e. assigned to variables or attributes, passed as function
parameters, etc. The introduction of this essay has a bit more about
this:
http://personalpages.tds.net/~kent37/kk/1.html

> Does it makes any sense?

Sure.

> I mean, if I store "a whole function" within each "Problem" object (assuming
> it can be done in Python), each Problem object would be consuming lot of
> memory (wouldn't it?). Maybe it would be better just to store a kind of
> "pointer" to the function within the "problem" object, so the object would
> be "lighter". The function would be then defined outside the object, as
> usual.

All Python values are references, so you are essentially storing a
pointer to the function object within the problem. Python assignment
does not copy. This is a fundamental concept of Python that often
confuses newbies, it is worth taking some time to understand it
correctly. My explanation is here:
http://personalpages.tds.net/~kent37/kk/00012.html


> By the way, I have another related question. In C you can pass arguments to
> a function by value or by reference. Is there any equivalence in Python to
> that approach? How is the usual way to pass function arguments in Python?

This question is the source of endless heat and little light on
comp.lang.python, for (IMO) two reasons:
- the word 'reference' means two things in this context - 'pass by
reference' as a way of parameter passing and 'reference' as a name for
a pointer.
- the (IMO) correct answer is 'neither'. Python passes object
references by value. If you think of it as passing a pointer by value
you will pretty much have the right idea. See the second link above
for more.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Pointer" to a function? Storing a function as an object property? Passing arguments by value/by reference?

2009-01-16 Thread greg whittier
On Fri, Jan 16, 2009 at 7:51 AM, Vicent  wrote:
>
> That "problem" has to contain, somehow, a property or element called 
> "function" which, in fact, I would like it to be a function, or a "pointer" 
> to a function.

In python, the name of a function is just a pointer to it.  Try this

>>> def foo():
print "Hi!"


>>> class Problem:
def __init__(self,fun):
self.fun = fun


>>> p1 = Problem(foo)
>>> p2 = Problem(foo)
>>> foo

>>> p1.fun

>>> p2.fun

>>> p1.fun == p2.fun
True
>>>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Pointer" to a function? Storing a function as an object property? Passing arguments by value/by reference?

2009-01-16 Thread Vicent
On Fri, Jan 16, 2009 at 14:56, greg whittier  wrote:

>
> In python, the name of a function is just a pointer to it.  Try this
>
> >>> def foo():
>print "Hi!"
>
>
> >>> class Problem:
>def __init__(self,fun):
>self.fun = fun
>
>
> >>> p1 = Problem(foo)
> >>> p2 = Problem(foo)
> >>> foo
> 
> >>> p1.fun
> 
> >>> p2.fun
> 
> >>> p1.fun == p2.fun
> True
> >>>
>


Wow!!!   I thought that the question was simple but I was afraid that the
answer was going to be too complex.

But I see that it actually works like I thought it should!!! It's extremely
simple and intuitive, IMHO.

Thank you to all for your precise and clear answers. I've learned a lot, and
specially appreciate the links that Kent provided.

I hope I can contribute to this forum in the future.


-- 
Vicent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Simple program with menu to view videos

2009-01-16 Thread David

Hi,
I just received the dvd Python Fundamentals by Wesley J. Chun and it was 
packaged by the distributor for use on Window and Mac but I use Linux so 
I pulled the videos of of the dvd. There are 10 lessons with a total of 
58 individual videos. Most lessons are 6-8 videos long. So I did as Alan 
has suggested and came up with a plan of attack. What my basic program 
does is plays the first video and then the next in that lesson and when 
finished them all goes back to the menu. I am asking for some tips on 
how I can make this program with less code and using some classes, if 
that is the best way to proceed. I have a lot of code that is just 
copied from one function to the next. I am new to python and enjoy these 
little projects because I can see them in action. Here is my starting point;

http://dwabbott.com/code/index6.html
thanks
-david
--
Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com
pgp.mit.edu

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Translating FORTRAN (77?) to Python?

2009-01-16 Thread Wayne Watson
Title: Signature.html




I may have a need down the line to convert a large number of lines of
FORTRAN code to Python. Is there a good translator available to do
this? In the past, I've found some for translating Pascal to C, and
possibly others. 
-- 


   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)

 "The creation of the world did not occur at the 
  beginning of time; it occurs every day." -- M. Proust

Web Page: 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] @property?

2009-01-16 Thread spir
Hello,

would someone point me to some clear doc about properties: purpose, use, 
underlying model...
[Could not find myself, probably because the word "property itself has far too 
wide meaning.]

Thank you.
denis

--
la vida e estranya
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Translating FORTRAN (77?) to Python?

2009-01-16 Thread Chris Fuller
On Friday 16 January 2009 10:40, Wayne Watson wrote:
>  I may have a need down the line to convert a large number of lines of
> FORTRAN code to Python. Is there a good translator available to do this? In
> the past, I've found some for translating Pascal to C, and possibly others.

There is a Fotran to C converter, and you might have some luck interfacing 
that with SWIG or just following the tutorials in the Python docs.

http://www.netlib.org/f2c/
http://docs.python.org/extending/

Cheers
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Translating FORTRAN (77?) to Python?

2009-01-16 Thread greg whittier
There's an absolutely incredible project call f2py
http://cens.ioc.ee/projects/f2py2e/ that I've used before.  It doesn't
translate the code, but wraps it (which is actually better) and lets
you import your library as a module.  It even generates docstrings so
you can see how to call the functions.

On Fri, Jan 16, 2009 at 11:40 AM, Wayne Watson
 wrote:
> I may have a need down the line to convert a large number of lines of
> FORTRAN code to Python. Is there a good translator available to do this? In
> the past, I've found some for translating Pascal to C, and possibly others.
> --
>
>Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
>
>  (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)
>
>  "The creation of the world did not occur at the
>   beginning of time; it occurs every day." -- M. Proust
>
> Web Page: 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Creating simple windows in XP

2009-01-16 Thread W W
On Thu, Jan 15, 2009 at 5:45 PM, Alan Gauld wrote:

> You mean like doing
>
> import tkMessageBox
> tkMessageBox.showinfo("Window Text", "A short message")
>
> in Tkinter? :-)
>
> OR
>
> res = tkMessageBox.askokcancel("Which?", "Ready to stop?")
> print res
>
> At that level Tkinter is pretty easy too.


After trying that and getting the mildly annoying root window to pop up I
did a quick search and found how to hide that window.

from Tkinter import Tk
root = Tk()
root.withdraw()
tkMessageBox.showinfo("Window", "Root window is gone!")

If you need it back, root.deiconfiy() will show the window again.

HTH,
Wayne
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] @property?

2009-01-16 Thread Steve Willoughby
On Fri, Jan 16, 2009 at 06:32:19PM +0100, spir wrote:
> Hello,
> 
> would someone point me to some clear doc about properties: purpose, use, 
> underlying model...
> [Could not find myself, probably because the word "property itself has far 
> too wide meaning.]

I'm a little confused here because of the "@" in the subject.
Do you mean Python syntax using the "@" in front of a name like:

@protected
def spam(eggs):
...

Those are called "decorators".  Try searching for those, or we can help 
if that's what you're looking for. 

-- 
Steve Willoughby|  Using billion-dollar satellites
st...@alchemy.com   |  to hunt for Tupperware.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Simple program with menu to view videos

2009-01-16 Thread spir
Le Fri, 16 Jan 2009 11:35:13 -0500,
David  a écrit :

> Hi,
> I just received the dvd Python Fundamentals by Wesley J. Chun and it was 
> packaged by the distributor for use on Window and Mac but I use Linux so 
> I pulled the videos of of the dvd. There are 10 lessons with a total of 
> 58 individual videos. Most lessons are 6-8 videos long. So I did as Alan 
> has suggested and came up with a plan of attack. What my basic program 
> does is plays the first video and then the next in that lesson and when 
> finished them all goes back to the menu. I am asking for some tips on 
> how I can make this program with less code and using some classes, if 
> that is the best way to proceed. I have a lot of code that is just 
> copied from one function to the next. I am new to python and enjoy these 
> little projects because I can see them in action. Here is my starting point;
> http://dwabbott.com/code/index6.html
> thanks
> -david

Just had a look at your code. As you say: "a lot of code that is just copied 
from one
function to the next". The only reason for that is your functions have no 
argument (parameter):

def lesson3b():
player = "/usr/bin/mplayer"
fname = "/home/david/Desktop/core_video/data/python_0302.flv"
subprocess.call([player, fname])
lesson3c()

From this instance of a specific lesson player function, you can simply define 
a generic one:

def play_lesson(lesson_id):
# does not belong here:
player = "/usr/bin/mplayer"
fname = lesson_file_name(lesson_id) # how to get the file name?
subprocess.call([player, fname])
# does not belong here:
lesson3c()

Note that "player" is a constant, so that it does not belong to the function. 
At best, it may be
an optional argument which could later be overriden (by e.g. a command line 
argument -- if ever
you chage the player).
Also, the lesson3c() call is part of an overall scheduling logic that should be 
controled from an
external loop or menu -- this depends on how you intend to use the application.
Finally, note that the lesson file names are pretty regular: There are built 
out a single
prefix, a "series" number, then a lesson letter. Provided you do not change 
this logic, you can
build the filenames easily. The prefix can be written as an optional arg, like 
the player. Also, I
would distinguish "series" from "lesson" (this may be helpful at the schedular 
level). An
alternative may be a lesson file lookup in a dictionary of 
(lesson_id:lesson_file_name)
pairs, but there is no need for that here.

So that the core lesson playing function may look like that:

def play_lesson(series_id, lesson_id,
player = "/usr/bin/mplayer",
prefix="/home/david/Desktop/core_video/data/python_"):
# convert lesson lowercass letter id to a ordinal:
# (a,b,...z <--> 97,98,...122)
lesson_id = ord(lesson_id) - 96
# I use below a string formatting expression
# but the file name may be constructed "manually"
fname = "%s%02d%02d" %(prefix, series_id, lesson_id)
subprocess.call([player, fname])

Now, you need a master control function, from which the user can possibly 
choose which lesson to
watch, that will accordingly call the lesson player func. As said, this mainly 
depends on how you
expect it as a user.
Note that the logic you first coded is a kind of "masked" recursivity:

play1a
  play1b
play1c

playnx
  playny

This would be implemented in the above version by an additional explicit 
recursive call:

def play_lesson(series_id, lesson_id,
player = "/usr/bin/mplayer",
prefix="/home/david/Desktop/core_video/data/python_"):
...
subprocess.call([player, fname])
play_lesson(next_lesson(series_id, lesson_id))

(provided the next_lesson func exists)
This is very probably *not* the user model of an adaptative lesson player tool 
;-)

For a question of taste or programming style, I would personly rather create a 
"lesson" type
(class) that holds and handles all that stuff: identification, file_name, 
play(), next()... But it
would probably be neither clearer, nore simpler. You could do it afterwards as 
an introduction to
objects (the list will help you).

Denis

--
la vida e estranya
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Pointer" to a function? Storing a function as an object property? Passing arguments by value/by reference?

2009-01-16 Thread wesley chun
> All Python values are references, so you are essentially storing a
> pointer to the function object within the problem. Python assignment
> does not copy. This is a fundamental concept of Python that often
> confuses newbies, it is worth taking some time to understand it
> correctly. My explanation is here:
> http://personalpages.tds.net/~kent37/kk/00012.html


inside kent's post is an(other) article by fredrik lundh that may be
difficult to see during reading so i want to post it so you don't miss
it:
http://effbot.org/zone/call-by-object.htm

in my training courses, when people ask if Python is
"call-by-reference" or "call-by-value," i tell them it's neither...
*and* both, then proceed to show them what i mean. this topic is
definitely a place where beginners trip up, and it is also where you
begin your long road to maturity as a Python programmer. remember,
Python places an strong emphasis on objects, and in this particular
case, what happens depends on whether an object allows for
modification (mutability).

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] @property?

2009-01-16 Thread Kent Johnson
On Fri, Jan 16, 2009 at 12:32 PM, spir  wrote:
> Hello,
>
> would someone point me to some clear doc about properties: purpose, use, 
> underlying model...

Properties:
http://personalpages.tds.net/~kent37/kk/8.html

Decorators (the @ syntax):
http://personalpages.tds.net/~kent37/kk/1.html

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Translating FORTRAN (77?) to Python?

2009-01-16 Thread Wayne Watson
Title: Signature.html




That is interesting. I'll pursue it. Thanks. Of course, at the moment,
I have no F77 compiler, so I can't even execute or use the code. Is
there a freebie F77 compiler out there?

greg whittier wrote:

  There's an absolutely incredible project call f2py
http://cens.ioc.ee/projects/f2py2e/ that I've used before.  It doesn't
translate the code, but wraps it (which is actually better) and lets
you import your library as a module.  It even generates docstrings so
you can see how to call the functions.

On Fri, Jan 16, 2009 at 11:40 AM, Wayne Watson
 wrote:
  
  
I may have a need down the line to convert a large number of lines of
FORTRAN code to Python. Is there a good translator available to do this? In
the past, I've found some for translating Pascal to C, and possibly others.
--

   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)

 "The creation of the world did not occur at the
  beginning of time; it occurs every day." -- M. Proust

Web Page: 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



  
  ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

  


-- 


   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)

 "The creation of the world did not occur at the 
  beginning of time; it occurs every day." -- M. Proust

Web Page: 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Translating FORTRAN (77?) to Python?

2009-01-16 Thread Sander Sweers
On Fri, Jan 16, 2009 at 20:02, Wayne Watson
 wrote:
> That is interesting. I'll pursue it. Thanks. Of course, at the moment, I
> have no F77 compiler, so I can't even execute or use the code. Is there a
> freebie F77 compiler out there?

GCC supportd it http://gcc.gnu.org/.

Greets
Sander
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Translating FORTRAN (77?) to Python?

2009-01-16 Thread Smith, Jeff
There was an add-on to the GNU C compiler for FORTRAN77 at one time
(g77). I don't know if it is still available to how well it works
though.
 
Jeff
 



From: tutor-bounces+jsmith=medplus@python.org
[mailto:tutor-bounces+jsmith=medplus@python.org] On Behalf Of Wayne
Watson
Sent: Friday, January 16, 2009 2:03 PM
To: greg whittier
Cc: tutor@python.org
Subject: Re: [Tutor] Translating FORTRAN (77?) to Python?


That is interesting. I'll pursue it. Thanks. Of course, at the moment, I
have no F77 compiler, so I can't even execute or use the code. Is there
a freebie F77 compiler out there?

greg whittier wrote: 

There's an absolutely incredible project call f2py
http://cens.ioc.ee/projects/f2py2e/ that I've used before.  It
doesn't
translate the code, but wraps it (which is actually better) and
lets
you import your library as a module.  It even generates
docstrings so
you can see how to call the functions.

On Fri, Jan 16, 2009 at 11:40 AM, Wayne Watson

  wrote:
  

I may have a need down the line to convert a large
number of lines of
FORTRAN code to Python. Is there a good translator
available to do this? In
the past, I've found some for translating Pascal to C,
and possibly others.
--

   Wayne Watson (Watson Adventures, Prop.,
Nevada City, CA)

 (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std.
time)

 "The creation of the world did not occur at
the
  beginning of time; it occurs every day."
-- M. Proust

Web Page:


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

  


-- 

   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)

 "The creation of the world did not occur at the 
  beginning of time; it occurs every day." -- M. Proust

Web Page: 










Confidentiality Notice: The information contained in this electronic 
transmission is confidential and may be legally privileged. It is intended only 
for the addressee(s) named above. If you are not an intended recipient, be 
aware that any disclosure, copying, distribution or use of the information 
contained in this transmission is prohibited and may be unlawful. If you have 
received this transmission in error, please notify us by telephone (513) 
229-5500 or by email (postmas...@medplus.com). After replying, please erase it 
from your computer system.



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Simple program with menu to view videos

2009-01-16 Thread Kent Johnson
On Fri, Jan 16, 2009 at 11:35 AM, David  wrote:
>  I am asking for some tips on how I can make
> this program with less code and using some classes, if that is the best way
> to proceed. I have a lot of code that is just copied from one function to
> the next.

When you are copy/pasting code, think about how you can make a
function that abstracts the copied bits. For example you could have
this function:

def play(filename):
player = "/usr/bin/mplayer"
fname = "/home/david/Desktop/core_video/data/" + filename
subprocess.call([player, fname])

Then for example you would have
def lesson10f():
play("python_1006.flv")
menu()

So that is a start. Now look at what you are doing with the individual
sub-lessons. You really just want a way to play a series of lessons.
This can be done with a loop:

def lesson8():
  files = ['python_0800a.flv', 'python_0801.flv', , 'python_0806.flv']
  playall(files)

def playall(files):
  for name in files:
play(name)

Since the lesson names follow a regular pattern you might even be able
to generate the names automatically from the lesson number.

Finally, put a loop in your menu function rather than having the
lessons call back to the menu.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Translating FORTRAN (77?) to Python?

2009-01-16 Thread Wayne Watson
Title: Signature.html




Anything under Win?

Sander Sweers wrote:

  On Fri, Jan 16, 2009 at 20:02, Wayne Watson
 wrote:
  
  
That is interesting. I'll pursue it. Thanks. Of course, at the moment, I
have no F77 compiler, so I can't even execute or use the code. Is there a
freebie F77 compiler out there?

  
  
GCC supportd it http://gcc.gnu.org/.

Greets
Sander

  


-- 


   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)

 "The creation of the world did not occur at the 
  beginning of time; it occurs every day." -- M. Proust

Web Page: 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Translating FORTRAN (77?) to Python?

2009-01-16 Thread Sander Sweers
On Fri, Jan 16, 2009 at 21:06, Wayne Watson
 wrote:
> Anything under Win?

Yes, minigw http://www.mingw.org/.

Greets
Sander
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Translating FORTRAN (77?) to Python?

2009-01-16 Thread Wayne Watson
Title: Signature.html




Will that do me any good if I implement my application under Win Python?

Sander Sweers wrote:

  On Fri, Jan 16, 2009 at 21:06, Wayne Watson
 wrote:
  
  
Anything under Win?

  
  
Yes, minigw http://www.mingw.org/.

Greets
Sander

  


-- 


   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)

 "The creation of the world did not occur at the 
  beginning of time; it occurs every day." -- M. Proust

Web Page: 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Translating FORTRAN (77?) to Python?

2009-01-16 Thread Sander Sweers
On Fri, Jan 16, 2009 at 22:20, Wayne Watson
 wrote:
> Will that do me any good if I implement my application under Win Python?

Your question was for a fotran compiler to compile the source code.
The fotran program is your reference point to compare the results to.

Greets
Sander
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] curl and python

2009-01-16 Thread Jeremiah Jester
I'm trying to get curl to traverse a remote http directory get all page
names and do some processing on these file with python.

For starters, i need to get the directory listing in an array from the
domain. Any ideas on how to do this?

Thanks,
JJ




Disclaimer: The information contained in this transmission, including any 
attachments, may contain confidential information of Panasonic Avionics
Corporation.  This transmission is intended only for the use of the 
addressee(s) listed above.  Unauthorized review, dissemination or other use 
of the information contained in this transmission is strictly prohibited. 
If you have received this transmission in error or have reason to believe 
you are not authorized to receive it, please notify the sender by return 
email and promptly delete the transmission.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [python(x, y)] "Pointer" to a function? Storing a function as an object property? Passing arguments by value/by reference?

2009-01-16 Thread Alan Gauld


"Vicent"  wrote


That "problem" has to contain, somehow, a property or element called
"function" which, in fact, I would like it to be a function, or a 
"pointer"

to a function.


I won't repeat what others have said about passing function objects
into an object and storing them as attributes. However I will point 
out

an even more fundamental approach to your problem and pick up on
one of your comments...


# call the function in prob, and store the result in "x" :

x = prob.function( arguments/variables required by the function )


This is normal syntax for accessing the methods of an object.
Thus:

class Problem:
   def function(self, args):
 # your function code here
 return result

p = Problem()
x = p.function()

So if you know what function will be used for each problem
then you can define it as part of the class definition as a
normal method. Or you can pass a reference to a predefined
function into the class constructor:

class Problem:
def __init__(self, aFunc):
self.function = aFunc

def f(): return 42
def g(): return 66
p1 = Problem(f)
p2 = Problem(g)
x = p1.function()  # call f()
y = p2.function()  # call g()

This is IMHO slightly more elegant than assigning the function to
an attribute of the object after construction.

The down side of this style is that these functions will not
have any access to attributes inside your class via self, as methods
would.

Does it makes any sense? Which would it be the right (meaning 
efficient but

still object-oriented-programming-compliant) way to do it?


Its all potentially object oriented, depending upon what you want to 
do.


I mean, if I store "a whole function" within each "Problem" object 
(assuming
it can be done in Python), each Problem object would be consuming 
lot of

memory (wouldn't it?).


Methods are defined in classes (which are also objects in their own 
right
in Python) and objects hold references to the class. So each instance 
only
has a reference to the class which is used to access the predefined 
methods.
The functions that you pass as parameters as just direct references to 
those

function objects.


Maybe it would be better just to store a kind of
"pointer" to the function within the "problem" object,


Thats what happens.

By the way, I have another related question. In C you can pass 
arguments to
a function by value or by reference. Is there any equivalence in 
Python to
that approach? How is the usual way to pass function arguments in 
Python?


Basically by reference but as Wes said there are some wrinkles whoch 
mean

it can be less than intuitive for a beginner!

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Translating FORTRAN (77?) to Python?

2009-01-16 Thread Alan Gauld

"Smith, Jeff"  wrote


There was an add-on to the GNU C compiler for FORTRAN77 at one time
(g77). I don't know if it is still available to how well it works
though.


Caveat: Fortran is one of the few mainstream languages that I have 
never read or written so I have no personal experience. But...


At work we had a Fortran project which used the g77 GNU compiler 
and the FORTRAN team seemed to think it was OK. They were 
porting code from a VAX to a Sun Unix box.


I believe the GNU g77 translator is available for Windows too.

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] referring to subfolders

2009-01-16 Thread Che M

I have been using absolute paths in my programs, but
want to get out of the habit as I try to run them on
other computers.  I was surprised to find that this
type of reference didn't work:

path = '/subfolder/myfile.py'

But instead I did it this way and it works:

import os.path
self.currentdir = os.curdir
self.mysubfolder = os.path.join(self.currentdir, "subfolder")
path = self.mysubfolder + '/myfile.py'

Is this really the only way to do it,and so I have to import
os.path each time I have a case where I refer to a subdirectory?
It seems like something quick/built-in like the first way should 
work, or maybe I am misunderstanding something?

Thanks.

_
Windows Live™: Keep your life in sync.
http://windowslive.com/explore?ocid=TXT_TAGLM_WL_t1_allup_explore_012009
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] referring to subfolders

2009-01-16 Thread Steve Willoughby

Che M wrote:

I have been using absolute paths in my programs, but
want to get out of the habit as I try to run them on
other computers.  I was surprised to find that this
type of reference didn't work:

path = '/subfolder/myfile.py'


Pathnames are relative by default.  So:
   'myfile.py'
would be the file myfile.py in the current directory.
   'subfolder/myfile.py'
would be myfile.py inside subfolder, which is below the current directory.

It's the initial / character which makes it absolute, and start from the 
root directory.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] referring to subfolders

2009-01-16 Thread Che M




> Date: Fri, 16 Jan 2009 21:26:52 -0800
> From: st...@alchemy.com
> To: pine...@hotmail.com
> CC: tutor@python.org
> Subject: Re: [Tutor] referring to subfolders
>
> Che M wrote:
>> I have been using absolute paths in my programs, but
>> want to get out of the habit as I try to run them on
>> other computers. I was surprised to find that this
>> type of reference didn't work:
>>
>> path = '/subfolder/myfile.py'
>
> Pathnames are relative by default. So:
> 'myfile.py'
> would be the file myfile.py in the current directory.
> 'subfolder/myfile.py'
> would be myfile.py inside subfolder, which is below the current directory.
>
> It's the initial / character which makes it absolute, and start from the
> root directory.

Thanks--much simpler!


_
Windows Live™: Keep your life in sync. 
http://windowslive.com/howitworks?ocid=TXT_TAGLM_WL_t1_allup_howitworks_012009
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] curl and python

2009-01-16 Thread Kent Johnson
On Fri, Jan 16, 2009 at 5:56 PM, Jeremiah Jester
 wrote:
> I'm trying to get curl to traverse a remote http directory get all page
> names and do some processing on these file with python.
>
> For starters, i need to get the directory listing in an array from the
> domain. Any ideas on how to do this?

urllib2 and BeautifulSoup:
http://personalpages.tds.net/~kent37/kk/00010.html
http://personalpages.tds.net/~kent37/kk/9.html

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor