[Tutor] class overriding question

2004-12-18 Thread Brian van den Broek
Hi all,
instead of sleeping, I've been up all night finally attacking my
apprehension about classes. I think I'm mostly getting the hang of it --
I managed to convert a 300 line procedural script into (what I think is) 
a fully object-oriented approach. :-)

I made a lot of use of Mark Pilgrim's Dive Into Python
, but Pilgrim said something that I'd like
to check my understanding of. In section 5.5 
 he 
writes:

Guido, the original author of Python, explains method overriding this
way: "Derived classes may override methods of their base classes.
Because methods have no special privileges when calling other methods
of the same object, a method of a base class that calls another
method defined in the same base class, may in fact end up calling a
method of a derived class that overrides it. (For C++ programmers:
all methods in Python are effectively virtual.)" If that doesn't make
sense to you (it confuses the hell out of me), feel free to ignore
it. I just thought I'd pass it along.
I think I get this, but my inexperience with classes and Pilgrim's 
rhetorical flourish make me doubt myself. I think the following three 
ways of saying it all say the same thing (to a greater or lesser degree 
of precision) as the quote from Guido above. Do I have the idea?

Say class spam defines a method ham, and a method eggs, where ham calls 
eggs.  Further say class foo is derived from spam and overrides its eggs 
method, but not its ham method. Then calling the ham method of an 
instance bar of foo (and thus calling the ham method of spam, as foo is 
a spam), will call the eggs method of foo, despite the fact that ham is 
a method of spam, and within spam points originally to spam's version of 
the eggs method.

Alternatively, when calling bar.ham(), Python essentially says "OK, 
bar's a foo. Does foo have a ham method? No, but it is derived from 
spam. Does spam have a ham method? Yes, and it calls an eggs method. 
Since bar's a foo, I should first look to see if foo has an eggs method. 
(Nevermind that it was code in spam that started me off looking for 
eggs.) Golly good, it does. So I will use that and not even bother to 
look at spam's version of eggs."

One other way: if we represent class inheritance as a tree, an a given 
instance is of a class at level n in the tree, any search for a method 
begins with the instance class and works up the tree to the ultimate 
base class at level 1, no matter how high up the tree the search for the 
method was initiated. And, in that search up the tree, the first 
correctly named method to be found will be used. (I'm using the 
mathematician's notion of trees with roots at the top.)

Anyway, I hope I've both made sense and have got the idea right.
Best to all,
Brian vdB
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] class overriding question

2004-12-18 Thread Kent Johnson
Yup, that's right!
Attribute access (the dot operator '.') is an operation that happens at runtime, and each attribute 
access stands alone. Every attribute access goes through the same search path, starting with self, 
then the class (type) of self, finally the base classes. So, in your example, self.foo is found in 
the class of bar, while self.ham is found in the base class of the class of bar.

Kent
Brian van den Broek wrote:
Hi all,
instead of sleeping, I've been up all night finally attacking my
apprehension about classes. I think I'm mostly getting the hang of it --
I managed to convert a 300 line procedural script into (what I think is) 
a fully object-oriented approach. :-)

I made a lot of use of Mark Pilgrim's Dive Into Python
, but Pilgrim said something that I'd like
to check my understanding of. In section 5.5 
 he 
writes:

Guido, the original author of Python, explains method overriding this
way: "Derived classes may override methods of their base classes.
Because methods have no special privileges when calling other methods
of the same object, a method of a base class that calls another
method defined in the same base class, may in fact end up calling a
method of a derived class that overrides it. (For C++ programmers:
all methods in Python are effectively virtual.)" If that doesn't make
sense to you (it confuses the hell out of me), feel free to ignore
it. I just thought I'd pass it along.

I think I get this, but my inexperience with classes and Pilgrim's 
rhetorical flourish make me doubt myself. I think the following three 
ways of saying it all say the same thing (to a greater or lesser degree 
of precision) as the quote from Guido above. Do I have the idea?

Say class spam defines a method ham, and a method eggs, where ham calls 
eggs.  Further say class foo is derived from spam and overrides its eggs 
method, but not its ham method. Then calling the ham method of an 
instance bar of foo (and thus calling the ham method of spam, as foo is 
a spam), will call the eggs method of foo, despite the fact that ham is 
a method of spam, and within spam points originally to spam's version of 
the eggs method.

Alternatively, when calling bar.ham(), Python essentially says "OK, 
bar's a foo. Does foo have a ham method? No, but it is derived from 
spam. Does spam have a ham method? Yes, and it calls an eggs method. 
Since bar's a foo, I should first look to see if foo has an eggs method. 
(Nevermind that it was code in spam that started me off looking for 
eggs.) Golly good, it does. So I will use that and not even bother to 
look at spam's version of eggs."

One other way: if we represent class inheritance as a tree, an a given 
instance is of a class at level n in the tree, any search for a method 
begins with the instance class and works up the tree to the ultimate 
base class at level 1, no matter how high up the tree the search for the 
method was initiated. And, in that search up the tree, the first 
correctly named method to be found will be used. (I'm using the 
mathematician's notion of trees with roots at the top.)

Anyway, I hope I've both made sense and have got the idea right.
Best to all,
Brian vdB
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter questions

2004-12-18 Thread Mark Kels
 > I find the easiest way is to create an PhotoImage object attach
> the graphic file(jpg,bmp,gif) to that and assign the PhotoImage
> object to the Button.image property. You can "animate" the image
> by simply reassigning the file to the underlying PhotoImage onject.
Thanks, but a practical explanation will be more helpful.

> You mean print as in to a printer?
> Personally I tend to generate an HTML file and use the
> native OS Tools to print that. You can get fancy and
> use the native OS priniting libraries but since its
> very OS specific I find HTML is easier! OUtside Tkinter
> you may well find the GUI libraries have done the cross
> platform stuff for you, but not in Tkinter.
Again, a practical explanation will be more helpful...

> Define a full window? You mean full screen?
> Thats usually better done as a parameter that the user can
> set unless there is a very good reason not to. (Personally
> I refuse to use any program that insists on opening full screen!)
Full window or full screen is when the window of the program is all
over the screen except the start bar (or whatever the blue line in the
bottom of a windows xp called).
And why you refuse to use any program that insists on opening full screen ?
If it does then there must be a good reason for that... 

> If OTOH you mean that you don't want the DOS box in the background
> thats easy, just rename the .py file to .pyw. But I suspect,
> since you talk about cross platform you mean full screen.
I did wanted a full screen, but this is very helpful too :) .

Thanks allot .

-- 
1. The day Microsoft makes something that doesn't suck is probably the
day they start making vacuum cleaners.
2. Unix is user friendly - it's just picky about it's friends.
3. Documentation is like sex: when it is good, it is very, very good.
And when it is bad, it is better than nothing. - Dick Brandon
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] class overriding question

2004-12-18 Thread Brian van den Broek
Kent Johnson said unto the world upon 2004-12-18 08:53:
Yup, that's right!
Attribute access (the dot operator '.') is an operation that happens at 
runtime, and each attribute access stands alone. Every attribute access 
goes through the same search path, starting with self, then the class 
(type) of self, finally the base classes. So, in your example, self.foo 
is found in the class of bar, while self.ham is found in the base class 
of the class of bar.

Kent
Brian van den Broek wrote:

reconstructed it to make sure I had the intent of Guido's point>

Thanks Kent! I appreciate the external check. (The way Guido made the 
point was a bit gnostic, but not perhaps so much as Pilgrim's comment 
made it out.)

Best to all,
Brian vdB
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Difference between for i in range(len(object)) andfor iin object

2004-12-18 Thread Jacob S.
> Thing is, for people like me, you generally either don't know that a
> question is a dumb one until someone tells you the answer, (the 'of
> course'  moment), or until 5 minutes after you emailed
> your query, you find the answer you were looking for...

Amen! My life's story!

Also, to Kumar.

This is what you want.

test_cor = []
for line in cor:
test_cor.append(line.split('\t',1)[1])  ## This only works if there are
only 3 columns

Or, even simpler:

test_cor = [line.split('\t',1)[1] for line in cor]

HTH,
Jacob

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python 2.4 IDLE Windows 2000

2004-12-18 Thread Jacob S.
Gee,
I think I'm going to burst out in tears.
Mike Hansen gave the solution to the very problem I'm having, yet, when I
used the console version and deleted the custom color theme, it stopped
giving me error messages, but it still won't start up without the console.

I'm am still stunted of my python 2.4 experience.
Help, please!!!

Desperate,
Jacob

> Mike Hansen <[EMAIL PROTECTED]> wrote of an idle IDLE:
>
> > That rooted out the problem. A while ago, I changed the colors to kind
> > of match my VIM color theme(ps_color). When I did
> > idlelib.PyShell.main(), IDLE came up with my custom color theme.
> > However, there was a bunch of warnings about my theme. From IDLE, I
> > deleted the theme. Now IDLE will launch normally. I'll set up the color
> >
> > theme later. Maybe older color themes aren't compatible with the newer
> > IDLE? The color theme must have been laying around. I didn't brute
> > force
> > it in or anything like that.
> >
>
> > >IDLE is a
> > >part of the Standard Library, so it's actually possible to try turning
> > on
> > >individual pieces of it, one after the other.  Maybe that will help us
> > >debug what's going on.
> > >
> > >Start up your console version of Python, and try:
> > >
> > >
> > import idlelib.PyShell
> > idlelib.PyShell.main()
> > 
>
>
> Just a +1 to Mike's problem (and the solution).  For sake of Googlers
searching on error output (which I've done before), here are the errors I
was getting on my machine when trying to launch IDLE.  Note, when I tried to
open a .py file with IDLE the file would not open at all (and there were no
error messages) - interesting that the whole process failed - is the missing
error handling on the win or python side (no reason for these to have been
fatal errors...)
>
> Ah, now I can enjoy 2.4.  Thanks!
>
> my error output (win32):
>
>
> >>> import idlelib.PyShell
> >>> idlelib.PyShell.main()
>
>  Warning: configHandler.py - IdleConf.GetThemeDict -
>  problem retrieving theme element 'builtin-background'
>  from theme 'sagecomments'.
>  returning default value: '#ff'
>
>  Warning: configHandler.py - IdleConf.GetThemeDict -
>  problem retrieving theme element 'builtin-foreground'
>  from theme 'sagecomments'.
>  returning default value: '#00'
>
>  Warning: configHandler.py - IdleConf.GetThemeDict -
>  problem retrieving theme element 'builtin-background'
>  from theme 'sagecomments'.
>  returning default value: '#ff'
>
>  Warning: configHandler.py - IdleConf.GetThemeDict -
>  problem retrieving theme element 'builtin-foreground'
>  from theme 'sagecomments'.
>  returning default value: '#00'
>
>  Warning: configHandler.py - IdleConf.GetThemeDict -
>  problem retrieving theme element 'builtin-background'
>  from theme 'sagecomments'.
>  returning default value: '#ff'
>
>  Warning: configHandler.py - IdleConf.GetThemeDict -
>  problem retrieving theme element 'builtin-foreground'
>  from theme 'sagecomments'.
>  returning default value: '#00'
>
>  Warning: configHandler.py - IdleConf.GetThemeDict -
>  problem retrieving theme element 'builtin-background'
>  from theme 'sagecomments'.
>  returning default value: '#ff'
>
>  Warning: configHandler.py - IdleConf.GetThemeDict -
>  problem retrieving theme element 'builtin-foreground'
>  from theme 'sagecomments'.
>  returning default value: '#00'
>
>  Warning: configHandler.py - IdleConf.GetThemeDict -
>  problem retrieving theme element 'builtin-background'
>  from theme 'sagecomments'.
>  returning default value: '#ff'
>
>  Warning: configHandler.py - IdleConf.GetThemeDict -
>  problem retrieving theme element 'builtin-foreground'
>  from theme 'sagecomments'.
>  returning default value: '#00'
>
>  Warning: configHandler.py - IdleConf.GetThemeDict -
>  problem retrieving theme element 'builtin-background'
>  from theme 'sagecomments'.
>  returning default value: '#ff'
>
>  Warning: configHandler.py - IdleConf.GetThemeDict -
>  problem retrieving theme element 'builtin-foreground'
>  from theme 'sagecomments'.
>  returning default value: '#00'
>
>  Warning: configHandler.py - IdleConf.GetThemeDict -
>  problem retrieving theme element 'builtin-background'
>  from theme 'sagecomments'.
>  returning default value: '#ff'
>
>  Warning: configHandler.py - IdleConf.GetThemeDict -
>  problem retrieving theme element 'builtin-foreground'
>  from theme 'sagecomments'.
>  returning default value: '#00'
>
>  Warning: configHandler.py - IdleConf.GetThemeDict -
>  problem retrieving theme element 'builtin-background'
>  from theme 'sagecomments'.
>  returning default value: '#ff'
>
>  Warning: configHandler.py - IdleConf.GetThemeDict -
>  problem retrieving theme element 'builtin-foreground'
>  from theme 'sagecomments'.
>  returning default value: '#00'
>
>  Warning: configHandler.py - IdleConf.GetThemeDict -
>  problem retrieving theme element 'builtin-background'
>  from theme 'sagecomments'.
>  returning default value: '#ff'
>

Re: [Tutor] "TypeError: 'int' object is not callable"??

2004-12-18 Thread Jacob S.
Thanks for the explanation!
Jacob Schmidt


> Jacob S. wrote:
> > Thank you!
> >
> > Wait, though.
> >
> > How do I do this?
> >
> > def differentnoofvars(*args,**kwargs):  ## By the way, is it **kwargs or
> > **kwds?
>
> Call it what you like, it's an ordinary function parameter. kwds is
commonly used but you can use
> kwargs.
> > print kwargs
> > another(kwargs)
>
> Should be another(**kwargs). If you call another(kwargs) then kwargs will
be an ordinary parameter
> of another and another would be defined as
> def another(kwargs):
>...
>
> >
> > def another(**kwargs):
> > for x,y in kwagrs.items():
> > print "%s = %s" % (x,y)
> >
> > a = ['a=2','f=3','t=[1,2,3]']  ## A list of kwargs that I want to send
>
> Should be a dict, the **kwds parameter is a dict mapping keywords to
values
> a = {'a':2, 'f':3, 't':[1,2,3]}
>
> There really are two different and complementary things going on here, at
the point of call and at
> the point of function definition.
>
> At the point of call, you can pass a dictionary instead of using explicit,
named parameters. For
> example, given a function test() defined like this:
>   >>> def test(a, b):
>   ...  print a, b
>
> you can call it with ordinary named arguments:
>   >>> test(a='foo', b='bar')
> foo bar
>
> Or you can pass it a dictionary with the named arguments, using extended
calling syntax:
>   >>> d= {'a':'foo', 'b':'bar'}
>   >>> test(**d)
> foo bar
>
>
> Inside the function, if you have a **kwds parameter, it will receive a
dict containing any keyword
> arguments not explicitly declared. This allows you to pass keyword
parameters that you don't
> anticipate when the function is defined. For example,
>
>   >>> def test2(a, **kwds):
>   ...   print a
>   ...   for k,v in kwds.items():
>   ... print k,v
>
>   >>> test2(1) # No keywords
> 1
>   >>> test2(a=1) # a is a declared parameter so kwds is empty
> 1
>   >>> test2(1, b=2, c=3) # b and c are passed in kwds
> 1
> c 3
> b 2
>
> Kent
>
> > individually to differentnoofvars
> > differentnoofvars(a)
> >
> >
> >
> >>>Hey, could you give an example?
> >>>Thanks,
> >>>Jacob
> >>>
> >>>
> apply() is deprecated; it has been replaced by 'extended
> >>>
> >>>call syntax'.
> >>>Instead of
> >>>
>    apply(fn, args, kwds)
> you can now write
>    fn(*args, **kwds)
> 
> Kent
> >>
> >>Here is a quick example I came up with:
> >>
> >>
> >def spam(*args, **kwargs):
> >>
> >>... print "Here are the args you supplied:"
> >>... for item in args:
> >>... print item
> >>... print
> >>... print "Here are the kwargs you supplied:"
> >>... for key,value in kwargs.items():
> >>... print key, '=', value
> >>...
> >>
> >spam(1,'a','eggs',s=0, p=1, a=2, m=3)
> >>
> >>Here are the args you supplied:
> >>1
> >>a
> >>eggs
> >>
> >>Here are the kwargs you supplied:
> >>a = 2
> >>p = 1
> >>s = 0
> >>m = 3
> >>
> >>In the case of the spam() function, 1, 'a', and 'eggs' are all put into
> >>the sequence args (not sure if it is a list or tuple).  The key/value
> >>pairs are bundled into the dictionary kwargs.  The arguments have to be
> >>given in the right order though:
> >>
> >>
> >spam(t=1, b=1, 'this', 'will', 'fail')
> >>
> >>Traceback (SyntaxError: non-keyword arg after keyword arg
> >>
> >>HTH!
> >>
> >>Christian
> >>http://www.dowski.com
> >>
> >>
> >>
> >>
> >
> >
> > ___
> > Tutor maillist  -  [EMAIL PROTECTED]
> > http://mail.python.org/mailman/listinfo/tutor
> >
> ___
> Tutor maillist  -  [EMAIL PROTECTED]
> http://mail.python.org/mailman/listinfo/tutor
>

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Mainframe experience

2004-12-18 Thread David Holland

Sadly in my IT company (which I better not name),
mainframe experience seems to be a route to redundancy
!  Fortunately I work with Oracle.
=== message truncated === 





___ 
ALL-NEW Yahoo! Messenger - all new features - even more fun! 
http://uk.messenger.yahoo.com
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Re: A simpler mousetrap

2004-12-18 Thread Jacob S.
Also, just as interesting, yet probably less reliable:

a = list(a)
a[-3:] = 'bak'
a = "".join(a)

OR

a = a.rstrip('pct')
a = a+'bak'

OR

a = a.rstrip('pct')+'bak'  ## Which is essentially the same thing

OR

a = a[:-3]+'bak'

ETC.

HTH,
Jacob

> x=os.path.splitext(a)[0]+'.bak'
>
> Ah, jolly good, looks a bit simpler. Thanks!
>
> Regards,
>
> Liam Clarke
>
> On Thu, 16 Dec 2004 09:44:03 +0100, Wolfram Kraus
> <[EMAIL PROTECTED]> wrote:
> > Liam Clarke wrote:
> > > Hi all,
> > >
> > > I'm writing some code, and I want to take a given path + filename, and
> > > change the file extension to *.bak.
> > >
> > > In doing so, (entirely internal this function), I am assuming -
> > >
> > > That the file will always have an extension
> > > Thathe extension will vary
> > > But, it will follow the general DOS format of name.ext
> > >
> > > So, I came up with this -
> > >
> > > a="./tc/arc/gab.pct"
> > >
> > > x=a.replace(a[a.rfind('.'):len(a)],'.bak')
> > >
> > > x="./tc/arc/gab.bak"
> > >
> > > So, it works, but it feels a bit, well, hacky to me. Not nearly hacky
> > > as using an regex though ; )
> > >
> > > I thought about
> > >
> > > a="./tc/arc/gab.pct"
> > >
> > > aList=a.split('.')
> > > aList[-1]='bak'
> > > a=".".join(aList)
> > >
> > > but I'm wondering if there's a simpler way, as there usually seems to
> > > be, and it's always so obvious once I'm shown it, like 6 down - Six on
> > > vehicle live in the manse (VI + car). Obvious once you know.
> > >
> > > Regards,
> > >
> > > Liam Clarke
> >
> > Hey Liam!
> >
> > The os.path module is your friend, especially split and splitext:
> > http://docs.python.org/lib/module-os.path.html
> >
> > HTH,
> > Wolfram
> >
> > ___
> > Tutor maillist  -  [EMAIL PROTECTED]
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
>
> -- 
> 'There is only one basic human right, and that is to do as you damn well
please.
> And with it comes the only basic human duty, to take the consequences.
> ___
> Tutor maillist  -  [EMAIL PROTECTED]
> http://mail.python.org/mailman/listinfo/tutor
>
>

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Vpython

2004-12-18 Thread Jacob S.
> > I was wondering were can I find some Vpython graphics program codes that
are readily available.

I have 2 that I am proud of.

A simple function grapher
Usage:
>0.125*x**2

Graphs y = 1/8*x**2

>y = 1/x

Graphs y = 1/x

>clear

Clears display window

>r = t**2

Graphs polar function r = t**2

>remove lines

Removes all axes, including polar axes, should a polar function be graphed
after

>return lines

Returns all lines, so that they appear when appropriate.

>x = sin(x**2)+log(x**2)

Graphs the function...


--

from __future__ import division
from visual import *
import os
from math import *
ja = 0

def start():
for objects in scene.objects:
objects.visible = 0
scene.title = "Function Grapher by Jacob, Inc."
tem = raw_input('Are you on a desktop, or a notebook? ')
if tem == 'desktop':
scene.x = 365
if tem == 'notebook':
scene.x = 574
scene.visible=1
scene.exit=0
scene.userspin = 0
scene.range=(10,10,1)
scene.background=(1,1,1)
global xaxis
global yaxis
xaxis = curve(color=color.black)
xaxis.append(pos=(100,0,0))
xaxis.append(pos=(-100,0,0))
yaxis = curve(color=color.black)
yaxis.append(pos=(0,100,0))
yaxis.append(pos=(0,-100,0))
global radiusaxis
global radiusaxis2
radiusaxis = curve(pos=[(-100,-100),(100,100)],color=color.black)
radiusaxis2 = curve(pos=[(-100,100),(100,-100)],color=color.black)
radiusaxis.visible = 0
radiusaxis2.visible = 0

start()
y = 3
m = 0
t = 0
d = 1
print """\
List of Commands:
clear
quit
remove lines
return lines

Function Syntax:
var = funct a,b
where var = what
and funct = f(what)
and a,b = range
"""

print 'Please type in functions in below. '
while y != "":
lists=[]
y = raw_input('>')
if y == 'clear':
scene.visible=0
start()
print "-"*36
continue
elif y == 'quit':
scene.visible = 0
del scene
break
elif y == 'remove lines':
a = [radiusaxis,radiusaxis2,xaxis,yaxis]
for x in a:
x.visible = 0
d = 0
continue
elif y == 'return lines':
a = [radiusaxis,radiusaxis2,xaxis,yaxis]
for x in a:
x.visible = 1
d = 1
continue
if y.count('=') == 1:
y = y.split(' = ')
type = y[0].lower()
y = y[1]
y = y.replace("y","x")
if type == 'r':
y = y.replace('x','t')
if d == 1:
radiusaxis.visible = 1
radiusaxis2.visible = 1
else:
type = 'y'
y = y.split(" ")
if len(y) > 1:
pass
else:
if type == 'r':
y.append('0,5*pi')
else:
y.append('-10,10')
range = y[1]
y = y[0]
range = range.split(",")
min = float(eval(range[0]))
max = float(eval(range[1]))
lists.append(curve(color=(1,0,1)))
x = min
if type == 'y' or type == 'x':
radiusaxis.visible = 0
radiusaxis2.visible = 0
while x >= min and x <= max:
x = x+0.005
try:
if eval(y) <= 15 and eval(y) >= -15:
if type == 'y':
lists[-1].append(pos=(x,eval(y),0))
elif type == 'x':
lists[-1].append(pos=(eval(y),x,0))
else:
lists.append(curve(color=(1,0,1)))
except:
pass
elif type == 'r':
m = 'eval(y)*cos(t)'
n = 'eval(y)*sin(t)'
t = min
while t >= min and t <= max:
try:
lists[-1].append(pos=(eval(m),eval(n),0))
except:
lists.append(curve(color=(1,0,1)))
t = t+0.005


___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Nifty

2004-12-18 Thread Jacob S.
I probably wouldn't be any help on projects, but I would probably learn
stuff from it.
I'm okay with it.
Jacob Schmidt

> >> I just got in contact with Nick Parlante of the Nifty
> >> Assignments project; he's been collecting material on fun
> >> projects:
> >>
> >> http://nifty.stanford.edu/
>
> > What do others think?
> Private channel or not, I'm in. (at least until classes spike up again in
> early February) Sounds like a good way to burn through a winter.
>
> Heck I don't even know if I CAN convert C++ to Python but I've never let a
> problem like language stop me from playing Chinese chess on boats in china
> ... So even if I lack the ability, I'm for the idea of having running
> 'projects' for the tutor list to do. That way it's driven a bit less by
> homework problems.
>
> --
>
> Programmer's mantra; Observe, Brainstorm, Prototype, Repeat
>
> David Broadwell
>
> ___
> Tutor maillist  -  [EMAIL PROTECTED]
> http://mail.python.org/mailman/listinfo/tutor
>
>

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] least squares

2004-12-18 Thread Jacob S.
Hey, now.

I know that some of us are newbies to python, but that doesn't mean that
we're all newbies to math concepts. (Even advanced ones.) Having said that,
I will shut my fat mouth now because it will be my luck that the math is
beyond what I can handle. I wouldn't mind seeing the code. Even if it is
long, I wouldn't mind seeing it. A private message is acceptable for
something too lengthy for the tutor list.

Jacob


> Hi Matt,
>
>
> Ok.  I have to admit that my mathematical maturity is actually a little
> low, but I'll try to follow along.  But this really doesn't sounds like a
> problem specific to Python though, but more of a math problem.
>
> So you may actually want to talk with folks with a real math background; I
> would not be mathematically mature enough to know if your code is correct.
> I'd strongly recommend talking to folks on the 'sci.math' or
> 'sci.math.num-analysis' newsgroups.
>
>
> I haven't seen your code, so I'm still in the dark about how it works or
> what its capabilities are.  I'll make a few assumptions that might not be
> right, but I have to start somewhere.  *grin*
>
> Does your code provide a way at getting at all possible solutions, or only
> one particular solution?  If so, then you can just filter out for
> solutions that satisfy the property you want.
>
> For example, we can produce a function that generates all squares in the
> world:
>
> ###
> >>> import itertools
> >>> def allSquares():
> ... for i in itertools.count():
> ... yield i*i
> ...
> >>> squares = allSquares()
> >>> squares.next()
> 0
> >>> squares.next()
> 1
> >>> squares.next()
> 4
> >>> squares.next()
> 9
> >>> squares.next()
> 16
> >>> squares.next()
> 25
> ###
>
>
> I can keep calling 'next()' on my 'squares' iteration, and keep getting
> squares.  Even though this can produce an infinite sequence of answers, we
> can still apply a filter on this sequence, and pick out the ones that are
> "palindromic" in terms of their digits:
>
> ###
> >>> def palindromic(n):
> ..."Returns true if n is 'palindromic'."
> ...return str(n) == str(n)[::-1]
> ...
> >>> filteredSquares = itertools.ifilter(palindromic, allSquares())
> >>> filteredSquares.next()
> 0
> >>> filteredSquares.next()
> 1
> >>> filteredSquares.next()
> 4
> >>> filteredSquares.next()
> 9
> >>> filteredSquares.next()
> 121
> >>> filteredSquares.next()
> 484
> >>> filteredSquares.next()
> 676
> ###
>
>
> This sequence-filtering approach takes advantage of Python's ability to
> work on a iteration of answers.  If your program can be written to produce
> an infinite stream of answers, and if a solution set with all positive
> coefficients is inevitable in that stream, then you can take this
> filtering approach, and just capture the first solution that matches your
> constraints.
>
>
>
> Similarly, if your program only produces a single solution, does it do so
> through an "iterative" algorithm?  By iterative, I mean: does it start off
> with an initial guess and apply a process to improve that guess until the
> solution is satisfactory?
>
> For others on the Tutor list, here is an "iterative" way to produce the
> square root of a number:
>
> ###
> def mysqrt(x):
> guess = 1.0  ## initial guess
> while not goodEnough(guess, x):
> guess = improve(guess, x)
> return guess
>
> def improve(guess, x):
> """Improves the guess of the square root of x."""
> return average(guess, x / guess)
>
> def average(x, y):
> return (x + y) / 2.0
>
> def goodEnough(guess, x):
> """Returns true if guess is close enough to the square root of x."""
> return abs(guess**2 - x) < 0.1
> ###
>
> (adapted/ripped off from material in SICP:
> http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-10.html#%_sec_1.1.7)
>
>
>
> If your program tries to solve the problem through an iterative process,
> then, again, does it inevitably produce a solution where all the constant
> coefficients 'Cn' are positive?  If so, maybe you can just continue to
> produce better and better solutions until its satisfactory, until all the
> coefficients are positive.
>
>
> Otherwise, without looking at your code, I'm stuck.  *grin* And even if I
> do see your code, I might be stuck still.  If your code is short, feel
> free to post it up, and we'll see how far we can get.  But you really may
> want to talk with someone who has a stronger math background.
>
> Good luck to you!
>
> ___
> Tutor maillist  -  [EMAIL PROTECTED]
> http://mail.python.org/mailman/listinfo/tutor
>

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Nifty

2004-12-18 Thread Brian van den Broek
Jacob S. said unto the world upon 2004-12-18 21:06:
I probably wouldn't be any help on projects, but I would probably learn
stuff from it.
I'm okay with it.
Jacob Schmidt

I just got in contact with Nick Parlante of the Nifty
Assignments project; he's been collecting material on fun
projects:
http://nifty.stanford.edu/

What do others think?
Private channel or not, I'm in. (at least until classes spike up again in
early February) Sounds like a good way to burn through a winter.

David Broadwell
Hi Jacob,
I've privately sent you what correspondence there has been amongst those 
who expressed interest.

If/once we pick a common project to undertake, I will post an 
announcement here. (We will also likely put up a wiki page somewhere.)

Best to all,
Brian vdB
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] listing all combinations of elements of a list

2004-12-18 Thread Jacob S.
Am I wrong, or is that what the module sets is for? I don't remember if
python 2.3 has it, but python2.4 has it available.

>>> import sets
>>> s = sets.Set
>>> a = s([1,2,3])
>>> a
Set([1, 2, 3])
>>> a.update([1,1,2,2,3,3,4,4,5,5])
>>> a
Set([1, 2, 3, 4, 5])
>>>

HTH,
Jacob Schmidt

> > def combination(items)
> > list = []
> > for i in range(0,len(items)):
> >for j in range(0,len(items)):
>
>  for i in items:
>  for j in items:
>
> Is both shorter and faster - no len() function calls.
>
> Or if you want to use indices:
>
> size = len(items)  # only calculate once, it won't change!
> lst = []# don't use builtin function names for
> variables
> for i in range(0,size)
>  for j in range(i+1,size)
>lst.append()
> return lst
>
> That saves a lot of len() calls and a comparison.
> Also it avoids iterating over the whole list each time.
>
>
> > My problems with this code being that a) code I write is usually
> pretty
> > inefficient, b) it doesn't extend to subsets of size > 2, and c) it
> uses
> > nested loops, which I have gathered from some previous discussions
> on
> > this list to be less than ideal.
>
> I've tidied up a little but I think nested loops are a necessary evil
> in
> this case - although somebody is sure to prove me wrong! :-)
>
> HTH,
>
> Alan G.
>
> ___
> Tutor maillist  -  [EMAIL PROTECTED]
> http://mail.python.org/mailman/listinfo/tutor
>
>

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Nifty

2004-12-18 Thread Chad Crabtree
I think it's a great idea, I would like to participate also.

Brian van den Broek wrote:

> Jacob S. said unto the world upon 2004-12-18 21:06:
>
>> I probably wouldn't be any help on projects, but I would probably
learn
>> stuff from it.
>> I'm okay with it.
>> Jacob Schmidt
>>
>>
> I just got in contact with Nick Parlante of the Nifty
> Assignments project; he's been collecting material on fun
> projects:
>
> http://nifty.stanford.edu/

>>>
 What do others think?
>>>
>>>
>>> Private channel or not, I'm in. (at least until classes spike up 
>>> again in
>>> early February) Sounds like a good way to burn through a winter.
>>
> 
>
>>>
>>> David Broadwell
>>
>
> Hi Jacob,
>
> I've privately sent you what correspondence there has been amongst 
> those who expressed interest.
>
> If/once we pick a common project to undertake, I will post an 
> announcement here. (We will also likely put up a wiki page
somewhere.)
>
> Best to all,
>
> Brian vdB
>




__ 
Do you Yahoo!? 
Send a seasonal email greeting and help others. Do good. 
http://celebrity.mail.yahoo.com
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor