[Tutor] Re: Re: Self referencing within a dictionary

2005-04-03 Thread Andrei
John Fouhy wrote on Sun, 03 Apr 2005 09:53:56 +1200:

> Andrei wrote:
>> Liam Clarke wrote on Sat, 2 Apr 2005 22:12:49 +1200:
>>>I know that as above doesn't work, but was just wondering if it's
>>>possible, and if it's a Bad Thing?
>> Max has already shown it's possible. Whether it's a Bad Thing... I don't
>> see why it would be. But I also can't imagine right now any realistic cases
>> where it would be useful to have a dictionary contain itself as a value.
> 
> It wouldn't contain itself as a value.

My mistake, I read the messages hastily and missed the ['a']. It was a more
interesting question that way ;).

-- 
Yours,

Andrei

=
Real contact info (decode with rot13):
[EMAIL PROTECTED] Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.

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


Re: [Tutor] What is the best book to start? - Many Thanks!

2005-04-03 Thread John Carmona
Dear Hoffmann, I am also a Newbie and I am currently going through "A Byte 
of Python" tutorial from Swaroop C H.

http://www.byteofpython.info/download?PHPSESSID=c0d52343d90f69f25942f49df9ae7944
If you are completely new to programming like me, you will find that this 
tutorial is excellent.
Good luck
JC

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


Re: [Tutor] Self referencing within a dictionary

2005-04-03 Thread Alan Gauld
> foo = {'a':1, 'b':2, 'c':foo['a']}

TRy

foo = {'a':1, 'b':2}
foo['c'] = foo['a']

The only reasobn it ddidn't work before was that
foo didn't exist at the time you tried to add the reference.

foo['c'] is just a reference to whatever foo['a'] references.

foo['d'] = foo

is a tad more interesting however.

especially if you do

foo['d']['d']['b'] = 42

As a mental excercise...
Now what does foo look like? :-)


> I know that as above doesn't work, but was just wondering if it's
> possible, and if it's a
> Bad Thing?

It's possible and may be a good thing depending on what you are trying
to do. It certainly shouldn't be a regular thing!

Alan G.

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


[Tutor] Script (Python) for Zope

2005-04-03 Thread Mike Jaeger
Hello,

I am looking for a Python-script for Zope which counts the objects (files) in 
the current folder and all its subfolders, but I don't know how to implement 
this script. Can somebody help me, please?
Or ist there a newsgroup/mailing list which can help me to find a solution for 
this problem?
Thanks.

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


[Tutor] [HELP]how to test properties of a file

2005-04-03 Thread Shidai Liu
Dear all,

Here is a simple question. But I can't find a simple answer.
How to test if a file is readable, executalbe or writable, especially, writable?

-- 
With best wishes!
Shidai
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [HELP]how to test properties of a file

2005-04-03 Thread Pierre Barbier de Reuille
Tthe simplest, IMHO, is :
try:
  f = file(filename, "w")
  [...]
except IOError:
  print "The file is not writable"
Of course, not that this method empty the file if it is writable ! The 
best is to just put your IO code in such a try block ... That way, 
you're sure the file has the right mode.

If you don't want to open the file to detect its mode, then you need to 
use the os and stat modules together ...

Pierre
Shidai Liu a écrit :
Dear all,
Here is a simple question. But I can't find a simple answer.
How to test if a file is readable, executalbe or writable, especially, writable?
--
Pierre Barbier de Reuille
INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France
tel   : (33) 4 67 61 65 77fax   : (33) 4 67 61 56 68
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [HELP]how to test properties of a file

2005-04-03 Thread Shidai Liu
On Apr 3, 2005 6:42 PM, Pierre Barbier de Reuille
<[EMAIL PROTECTED]> wrote:
> Tthe simplest, IMHO, is :
> 
> try:
>   f = file(filename, "w")
>   [...]
> except IOError:
>   print "The file is not writable"
> 
> Of course, not that this method empty the file if it is writable ! The
> best is to just put your IO code in such a try block ... That way,
> you're sure the file has the right mode.
> 
> If you don't want to open the file to detect its mode, then you need to
> use the os and stat modules together ...
> 
> Pierre
> 

I thought of this first. But I wasn't sure if it's the simplest.
Thanks for your help!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Re: If elif not working in comparison

2005-04-03 Thread gerardo arnaez
On Mar 29, 2005 3:06 AM, C Smith <[EMAIL PROTECTED]> wrote:

> If you have Python 2.4 you might want to check out the decimal type
> that is now part of the language.  There is a description at
> 
> http://www.python.org/doc/2.4/whatsnew/node9.html
> 
>  >>> import decimal
>  >>> a = decimal.Decimal('35.72')
>  >>> b = decimal.Decimal('1.73')
>  >>> a+b
> Decimal("37.45")
>  >>> a-b
> Decimal("33.99")
>  >>> a*b
> Decimal("61.7956")
>  >>> a/b
> Decimal("20.64739884393063583815028902")
> 

Would I used an if else: construction to determine where the INR value
lay and decide what precentage to increase it by?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Operator Overloading

2005-04-03 Thread Kevin Reeder
Following an example from a book, I'm getting an unexpected outcome.
The point of exercise is to extend operator overloading methods from
a superclass and track the method calls. Here's the code,

class MyList:
def __init__(self, start):
self.wrapped = [ ]
for x in start: self.wrapped.append(x)

def __add__(self, other):
return MyList(self.wrapped + other)

def __len__(self):
return len(self.wrapped)

=

from module import MyList

class MyListSub(MyList):

calls = 0

def __init__(self, start):
self.adds = 0
MyList.__init__(self, start)

def __add__(self, other):
MyListSub.calls = MyListSub.calls + 1
self.adds = self.adds + 1
return MyList.__add__(self, other)

def __len__(self):
MyListSub.calls = MyListSub.calls + 1
self.adds = self.adds + 1
return MyList.__len__(self)

def stats(self):
return self.calls, self.adds

This is not my code but is taken from the book I'm working with. My
problem is that whenever I call to the __add__ method the counters
increase by 2 while calls the __len__ method increase the counters
by 1 as expected. What I've concluded is that the method which
overloads and arithmetic operations executes the counter statements
twice while the method which overloads a sequencing operation
executes the counter statements only once. Otherwise, I can see no
difference between the two methods.

Here's an example,

>>> A = MyListSub([1, 2, 3])
>>> A.stats()
(0, 0)
>>> len(A)
3
>>> A.stats()
(1, 1)
>>> A + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
>>> A.stats()
(3, 3)

I'm stumped and and would appreciate any help.

Kevin


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