[Tutor] confusion about cloning (graphics)

2009-02-01 Thread David

Hello list,

I continue my reading of Harrington. In section 2.4. he is making a 
point about the need to clone, as otherwise the object associated to 
parameter corner (the point (20, 50)) takes the same value as corner2. 
That is: corner2 changes corner, which in turn changes the point (20, 
50). Here is the faulty code Harrington provides:




'''Program: makeRectBad.py
Attempt a function makeRect (incorrectly),
which takes a takes a corner point and dimensions to construct a Rectangle.
'''

from graphics import * # based on Zelle's graphics.py module

def makeRect(corner, width, height):  # Incorrect!
'''Return a new Rectangle given one corner Point and the dimensions.'''
corner2 = corner
corner2.move(width, height)
return Rectangle(corner, corner2)

def main():
winWidth = 300
winHeight = 300
win = GraphWin('Draw a Rectangle (NOT!)', winWidth, winHeight)
win.setCoords(0, 0, winWidth, winHeight)

rect = makeRect(Point(20, 50), 250, 200)
rect.draw(win)

# Wait for another click to exit
msg = Text(Point(winWidth/2, 20),'Click anywhere to quit.')
msg.draw(win)
win.getMouse()
win.close()

main()



This behaviour stuns me, because I was always following the belief that 
when variable2 refers to another variable1, the value of variable1 would 
NOT change, even as I operate on variable2 - just like my little 
experiment at the promt:


In [10]: x = 5

In [11]: y = x

In [12]: y + 2
Out[12]: 7

In [13]: x
Out[13]: 5

So here is my question: what have I failed to grasp? Are those different 
issues? If so, why?


I'd appreciate it if you could give me a short comment, this one bothers 
me ;-)


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


Re: [Tutor] Re : Is instance of what?

2009-02-01 Thread spir
Le Sat, 31 Jan 2009 15:00:02 -0500,
Kent Johnson  a écrit :

> On Sat, Jan 31, 2009 at 2:47 PM, spir  wrote:
> 
> >> > o.__class__ (or rather o.__class__.__name__) will work.
> >> Understood. Thank you.
> >> tj
> >
> > type(a) has been changed (since 2.2?) to return the same value as 
> > a.__class__
> 
> I think you mean type(o) (type of the instance) rather than type(a)
> (type of the class object).
> type(o) == o.__class__ is only true if o is an instance of a new-style
> class. Instances of oldstyle classes all have type 'instance'.
> 
> Kent
> 
You're right, Kent! I haven't paid enough attention to a/o. Thanks also for the 
precision about old style classes' instances.
By the way, don't your fingers find "isinstance" difficult to type? Mine always 
write typos ;-)

Denis

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


[Tutor] Precision with Decimal

2009-02-01 Thread gslindstrom
I am using the decimal module to work with money (US dollars and cents) and  
do not understand the precision. The documentation states:


"The decimal module incorporates a notion of significant places so that  
1.30 + 1.20 is 2.50. The trailing zero is kept to indicate significance.  
This is the customary presentation for monetary applications."


But I get:

from decimal import Decimal
a = Decimal('1.25')
a

Decimal('1.25')

b = Decimal('2.50')
b

Decimal('2.50')

a+b

Decimal('3.8')

I expect (and would like) a+b to be '3.75'. I've read through the  
getcontext() section but must be missing something. Can you help?


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


Re: [Tutor] confusion about cloning (graphics)

2009-02-01 Thread David

Dear list members,

thanks for all your replies, following your comments and links I will do 
some more research into this issue!


Kent Johnson wrote:

Harrington explains immediately after the program you cite; did you
see his explanation?

Yes, though the explanation didn't give me enough depth.

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


Re: [Tutor] string fomatting

2009-02-01 Thread Kent Johnson
On Sat, Jan 31, 2009 at 1:44 PM, Jay Jesus Amorin  wrote:
> Thanks bob.
>
> I want to search any characters in test after https://www.localhost.org/ and
> the search will end after it finds another /
>
> and when i print it will display testmodule.

If *all* you want is the first element of the path, you can get that
simply by splitting the string on / and retrieving the correct piece:

In [28]: test
Out[28]: 'https://www.localhost.org/testmodule/dev/trunk/admin/sql/mytest.sql'

In [29]: test.split('/')
Out[29]:
['https:',
 '',
 'www.localhost.org',
 'testmodule',
 'dev',
 'trunk',
 'admin',
 'sql',
 'mytest.sql']

In [30]: test.split('/')[3]
Out[30]: 'testmodule'

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


Re: [Tutor] Precision with Decimal

2009-02-01 Thread Kent Johnson
On Sat, Jan 31, 2009 at 10:05 PM,   wrote:
> I am using the decimal module to work with money (US dollars and cents) and
> do not understand the precision. The documentation states:
>
> "The decimal module incorporates a notion of significant places so that 1.30
> + 1.20 is 2.50. The trailing zero is kept to indicate significance. This is
> the customary presentation for monetary applications."
>
> But I get:
 from decimal import Decimal
 a = Decimal('1.25')
 a
> Decimal('1.25')
 b = Decimal('2.50')
 b
> Decimal('2.50')
 a+b
> Decimal('3.8')
>
> I expect (and would like) a+b to be '3.75'. I've read through the
> getcontext() section but must be missing something. Can you help?

I get a different result, are you sure you didn't set the precision
before you did the above?
In [31]: from decimal import *
In [32]: a=Decimal('1.25')
In [33]: b=Decimal('2.50')
In [34]: a+b
Out[34]: Decimal('3.75')

The precision is the number of significant digits, not the number of
decimal places:
In [37]: getcontext().prec=1

In [38]: a+b
Out[38]: Decimal('4')

In [39]: getcontext().prec=2

In [40]: a+b
Out[40]: Decimal('3.8')

In [41]: a*Decimal('1')
Out[41]: Decimal('1.4E+4')

The example is the docs is perhaps not the best one because 1.3 + 1.2
works correctly with normal floating point. 1.1 + 1.1 gives a
different result with Decimal vs floating point:
In [46]: 1.1 + 1.1
Out[46]: 2.2002

In [48]: Decimal('1.1') + Decimal('1.1')
Out[48]: Decimal('2.2')


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


Re: [Tutor] Precision with Decimal

2009-02-01 Thread Sander Sweers
On Sun, Feb 1, 2009 at 04:05,   wrote:
> "The decimal module incorporates a notion of significant places so that 1.30
> + 1.20 is 2.50. The trailing zero is kept to indicate significance. This is
> the customary presentation for monetary applications."
>
> But I get:
 from decimal import Decimal
 a = Decimal('1.25')
 a
> Decimal('1.25')
 b = Decimal('2.50')
 b
> Decimal('2.50')
 a+b
> Decimal('3.8')
>
> I expect (and would like) a+b to be '3.75'. I've read through the
> getcontext() section but must be missing something. Can you help?

You probably set the precision to 2 via getcontext.

example:

>>> import decimal
>>> a = decimal.Decimal('1.25')
>>> b = decimal.Decimal('2.50')
>>> a + b
Decimal("3.75")
>>> decimal.getcontext().prec = 2
>>> a + b
Decimal("3.8")

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


Re: [Tutor] confusion about cloning (graphics)

2009-02-01 Thread Kent Johnson
On Sun, Feb 1, 2009 at 3:34 AM, David  wrote:

> This behaviour stuns me, because I was always following the belief that when
> variable2 refers to another variable1, the value of variable1 would NOT
> change, even as I operate on variable2

> So here is my question: what have I failed to grasp? Are those different
> issues? If so, why?

My explanation is here:
http://personalpages.tds.net/~kent37/kk/00012.html

Harrington explains immediately after the program you cite; did you
see his explanation?

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


[Tutor] Python 3 tutorials

2009-02-01 Thread Kent Johnson
I added a page to the Python.org wiki to list tutorials that address Python 3:
http://wiki.python.org/moin/Python3.0Tutorials

Please add any I have missed. (Alan, that mean you - I couldn't find a
link to your work-in-progress update.)

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


Re: [Tutor] confusion about cloning (graphics)

2009-02-01 Thread spir
Le Sun, 01 Feb 2009 16:34:10 +0800,
David  a écrit :

> Hello list,
> 
> I continue my reading of Harrington. In section 2.4. he is making a 
> point about the need to clone, as otherwise the object associated to 
> parameter corner (the point (20, 50)) takes the same value as corner2. 
> That is: corner2 changes corner, which in turn changes the point (20, 
> 50). 

[...]

(Both point happen to have the same coordinates, actually to *be* the same 
point, that's it?)

> This behaviour stuns me, because I was always following the belief that 
> when variable2 refers to another variable1, the value of variable1 would 
> NOT change, even as I operate on variable2 - just like my little 
> experiment at the promt:
> 
> In [10]: x = 5
> 
> In [11]: y = x
> 
> In [12]: y + 2
> Out[12]: 7
> 
> In [13]: x
> Out[13]: 5
> 
> So here is my question: what have I failed to grasp? Are those different 
> issues? If so, why?
> 
> I'd appreciate it if you could give me a short comment, this one bothers 
> me ;-)
> 
> David

You are not the first one, you won't be the last one ;-)

Actually, python does not make any difference between values and objects: all 
are objects. (Python distinguishes mutable and immutable types, see below about 
that). More precisely, at the conceptual level, there are kinds of data (or 
data constructs), like a "position", that are values, even when compound. For 
instance, a position may be made of x and y simple values.
Let us say you create a custom type of objects such as:

class Point(object):
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __str__(self):
return "(x:%s y:%s)" %(self.x,self.y)

Conceptually, this is in fact a compound value that should rather be called 
"Position". So that when you "copy" a point/position to create a new one, you 
expect each one to have its own value -- what won't happen:

shift_x, shift_y = 20,30
p = Point(100,100)
q = p
# ...
q.x += shift_x ; q.y += shift_y
print p,q
==>
(x:120 y:130) (x:120 y:130)

This occurs because you make a confusion between what is for you a value (be it 
compound), or not, at the conceptual level ; and what it is for python: an 
object in both cases, that has an identity and a state.
When "q = p" is executed an alias name "q" is bound to the same unique Point 
object that was already bound to the name 'p'. That's all what happens. When 
this object's state is further changed, then logically printing p or q outputs 
the same new state. The point is shared by p and q.

The obvious remedy is to copy the point's state, meaning the values it holds, 
instead of copying the object itself:

q = Point()
q.x,q.y = p.x,p.y

Another solution is to use tuples, which are the container type that python 
provides to hold compound values. If you represent points/positions by (x,y) 
tuples, then the program wil behave the way you intend it, meaning that p and q 
will have different positions and actually *be* distinct objects for python:

shift_x, shift_y = 20,30
p = (100,100)
q = p
# ...
q = (q[0]+shift_x, q[1]+shift_y)
print p,q
==>
(100, 100) (120, 130)

The proper distinction here is that tuples are "immutable" objects. This means 
that one cannot change them directly (actually there is some more subtility) by 
writing e.g. q[0]=120. Concretely, for the programmer, it follows that tuples 
and all immutable objects (number,string,boolean) behave like we expect it from 
data that represent values in a program/model.

[There some literature on the topic. And even a design pattern called "value 
object" that adresses the issue.]

Denis

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


Re: [Tutor] IDLE vs PythonWin

2009-02-01 Thread Alan Gauld


"Wayne Watson"  wrote

Hi, sorry, but I have no idea what vim is, 


Yes, its just a text editor that runs in its own window.
Its a very very powerful text editor, one of perhaps 3 or 4 
that are universally used by professional programmers
on any operating system they may need to work with, 
and specifically designed for writing programs.


But you could substitute the name of any text editor.

I'd still like to know why pythonwin prints almost 
completely blank pages. 


Me, too, I checked and it works fine for me.
One option is to try a different print driver - it could be a bad 
interface between vim and the printer. But thats unlikely...



vim=vi(m), linux??


vim = vi improved.
vi was developed by Bill Joy(founder of Sun Microsystems) 
while at university in the late 1970s or very early 80s. It was
desuigned to make it easier to edit programs and take 
advantage of the brand new "glass teletypes" (ie video 
monitors) that weere becoming available. It ran on Unix 
and fast became the standard Unix editor.


Emacs was being built at around the same time on 
Digital computers as a set of macros on top of the Teco 
editor (The name stands for Editining Macros) and when 
these became popular it was turned into a fully fledged 
editor. But because it came from a bunch of Lisp programmers  
it was almost immediately made cross platform and became 
popular across a whole bunch of programmers. And because 
it had a Lisp macro lamnguage it was easy to extend. (A lot 
more than vi!) Both vi and emacs are extremely popular but 
work in almost diametrically opposite ways and so polarise 
opinions. This regularly leads to "Editor wars" between 
programmers on Usenet :-)


Personally I useed both on Unix and liked both. When I moved 
to Windows Emacs couldn't perform all its tricks, whereas 
vi just worked as usual so gradually I stopped using emacs.
Thebn I found vim wjhich added a whole bunch of the "missing" 
features of vi plus full GUI support(mice etc) and vim is now my 
standard editor for serious programming whether it be in Lisp, 
C, Python or HTML.



 I think you may have misunderstood Alan.


Yes, the description below is correct.
I have 3 separate windows open. I use Alt-Tab to cycle 
between them. In both the Python and OS windows I use up/down 
arrow to cycle between previous commands.


 Then he saves his code in vim and runs the script in the 
shell (in Ipython you'd use the magic function %run, 


In vim I could use the :! command to run it within vim, 
but I prefer to keep the errors etc plus the previous 
outputs all available in a separate window.


HTH,


--
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] Python 3 tutorials

2009-02-01 Thread Alan Gauld


"Kent Johnson"  wrote

Please add any I have missed. (Alan, that mean you - I couldn't find 
a

link to your work-in-progress update.)


I've literally just started last week so there's really nothing to 
note yet.

It usually takes jme a week or two per topic to do a major update.
So once  I get a few topics done then I'll publish the new link and
add it to my sig...

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



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


Re: [Tutor] PyCon2009 Tutorial: An Introduction to Object-OrientedProgramming

2009-02-01 Thread Alan Gauld


"wesley chun"  wrote


David Letscher and I will be leading a tutorial at this year's
PyCon titled "An Introduction to Object-Oriented Programming."


likewise, i'll be doing one on network programming, 


Such fun!
I just wish I could afford the trip to the US! :-)

Alan G.

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


Re: [Tutor] PyCon2009 Tutorial: An Introduction to Object-OrientedProgramming

2009-02-01 Thread wesley chun
> Such fun! I just wish I could afford the trip to the US! :-)

PyCons are such a great way to (re)connect with the community and so
many opportunities to learn from the masters (and mistresses) of
Python. the conferences are so incredible that once you go, you have
to go every year thereafter.

as far as costs go, alan and everyone else, even if you can't afford
it, there's help:
http://us.pycon.org/2009/registration/financial-aid/

hopefully this will help some of you out, especially this year!

cheers, and hope to meet some of you in a few months!
-- 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