Martin Walsh | 1 Feb 04:36 2009

Re: Want to write a Perl based IP to Domain Name converter.

wesley chun wrote:
>>>>> The script in the following can do the batch conversion from domain
>>>>> name to IP:
>>>>  This is a Python list, not Perl!
>>> OMG!  It's my mistake, sorry for this.
>> Lol..thats okay. Now that you are here and have seen what fun we have
>> writing Python code - why not join the party?
>>
>> Don't be shy to join us :p - i bet you won't be sorry.
> 
> 
> well, he had to join Tutor to post, unless a mod allowed his msg, so
> there should be *some* interest in doing it in Python. after all, the
> equivalent code is already slightly shorter and easier to read than
> the Perl version...
> 
> import socket
> import sys
> 
> for hp in sys.argv[1:]:
>     h, p = hp.strip().split(':')
>     print '%s -> %s:%s' % (hp, socket.gethostbyname(h), p)

Here's my attempt, a few lines longer. The 'split /:/ or next' part
confuses me a bit, though I suspect it's a bug ... 'cause I guess, as
long as the line has value even if it doesn't match the host:port
pattern, it won't be an exception, or undef, or false, or whatever you
call it in perl. :D

import socket
(Continue reading)

David | 1 Feb 09:34 2009
Picon
Picon

confusion about cloning (graphics)

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:

<code>

'''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)
(Continue reading)

spir | 1 Feb 10:51 2009
Picon

Re: Re : Is instance of what?

Le Sat, 31 Jan 2009 15:00:02 -0500,
Kent Johnson <kent37 <at> tds.net> a écrit :

> On Sat, Jan 31, 2009 at 2:47 PM, spir <denis.spir <at> free.fr> 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 <at> python.org
http://mail.python.org/mailman/listinfo/tutor
(Continue reading)

spir | 1 Feb 12:45 2009
Picon

Re: confusion about cloning (graphics)

Le Sun, 01 Feb 2009 16:34:10 +0800,
David <ldl08 <at> gmx.net> 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
> 
(Continue reading)

gslindstrom | 1 Feb 04:05 2009
Picon

Precision with Decimal

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 <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Kent Johnson | 1 Feb 13:35 2009
Picon

Re: string fomatting

On Sat, Jan 31, 2009 at 1:44 PM, Jay Jesus Amorin <jay.amorin <at> gmail.com> 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 <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Kent Johnson | 1 Feb 13:46 2009
Picon

Re: confusion about cloning (graphics)

On Sun, Feb 1, 2009 at 3:34 AM, David <ldl08 <at> gmx.net> 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 <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Sander Sweers | 1 Feb 14:04 2009
Picon

Re: Precision with Decimal

On Sun, Feb 1, 2009 at 04:05,  <gslindstrom <at> gmail.com> 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 <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Kent Johnson | 1 Feb 14:16 2009
Picon

Re: Precision with Decimal

On Sat, Jan 31, 2009 at 10:05 PM,  <gslindstrom <at> gmail.com> 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('11111')
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.2000000000000002

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

Kent
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Kent Johnson | 1 Feb 14:29 2009
Picon

Python 3 tutorials

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 <at> python.org
http://mail.python.org/mailman/listinfo/tutor


Gmane