[Zope] A quick zope/python question

2006-05-09 Thread Jonathan

I have a short python script:

gc = context.restrictedTraverse('coup/temp_folder/GarbageCollector', None)
print 'gc=',str(gc)
if gc:
 print 'GarbageCollector exists'
else:
 print 'No GarbageCollector'

which, when run produces:

gc= TransientObjectContainer at GarbageCollector
No GarbageCollector

Which is really strange because 'gc' was just reported to be a TOC!

When the script is changed to:

gc = context.restrictedTraverse('coup/temp_folder/GarbageCollector', None)
print 'gc=',str(gc)
if gc is not None:
 print 'GarbageCollector exists'
else:
 print 'No GarbageCollector'

I get the expected response:

gc= TransientObjectContainer at GarbageCollector
GarbageCollector exists


I think I am misunderstanding something basic about python/zope here. Why 
does a TOC object evaluate to false?


Thanks,

Jonathan

___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] A quick zope/python question

2006-05-09 Thread Lennart Regebro

On 5/9/06, Jonathan [EMAIL PROTECTED] wrote:

I think I am misunderstanding something basic about python/zope here. Why
does a TOC object evaluate to false?


Well, why not? If you are checking for none-ness, use is None.
Evaluating the object is wrong.

If the TOC object evaluates to something empty as a side-effect of
something, or because it explicitly is supposed to return something
useful when evaluated, who in this case happens to be empty/false, is
besides the point. It should be easy to figure out if you read the
source, though. :-)

--
Lennart Regebro, Nuxeo http://www.nuxeo.com/
CPS Content Management http://www.cps-project.org/
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists -
http://mail.zope.org/mailman/listinfo/zope-announce
http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] A quick zope/python question

2006-05-09 Thread Jens Vagelpohl

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1


On 9 May 2006, at 16:18, Jonathan wrote:

gc= TransientObjectContainer at GarbageCollector
GarbageCollector exists


I think I am misunderstanding something basic about python/zope  
here. Why does a TOC object evaluate to false?


I think the basic misunderstanding is about what happens if you say...

if FOO: ...

Python will try to evaluate using steps such as looking for methods  
like __nonzero__, __len__, etc, and you're not aware what these may  
evaluate to given your object. A Folder, for example, evaluates to  
False when it is empty.


You should use a different test to determine if that item exists.

jens

-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.1 (Darwin)

iD8DBQFEYLRlRAx5nvEhZLIRAmSWAKCxP+IjWYHEC/J1E8zDvq0R2OUB1wCePA74
Fm8Oc74d3dn915gDefbR1Yo=
=FOpK
-END PGP SIGNATURE-
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] A quick zope/python question

2006-05-09 Thread Andreas Jung



--On 9. Mai 2006 11:18:25 -0400 Jonathan [EMAIL PROTECTED] wrote:


I have a short python script:

gc = context.restrictedTraverse('coup/temp_folder/GarbageCollector', None)
print 'gc=',str(gc)
if gc:
  print 'GarbageCollector exists'
else:
  print 'No GarbageCollector'

which, when run produces:


Check the implementation. if obj calls obj.__nonzero__() or 
obj.__len__().. depending on the implemenation this will explain the result





When the script is changed to:

gc = context.restrictedTraverse('coup/temp_folder/GarbageCollector', None)
print 'gc=',str(gc)
if gc is not None:
  print 'GarbageCollector exists'
else:
  print 'No GarbageCollector'


This is the right way to do it.

-aj

--
ZOPYX Ltd.  Co. KG - Charlottenstr. 37/1 - 72070 Tübingen - Germany
Web: www.zopyx.com - Email: [EMAIL PROTECTED] - Phone +49 - 7071 - 793376
E-Publishing, Python, Zope  Plone development, Consulting


pgppDSSxP4hG6.pgp
Description: PGP signature
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] A quick zope/python question

2006-05-09 Thread Jonathan

Thanks to everyone for setting me straight!

I have been (erroneously!) assuming that any defined object would evaluate 
to 'true'.


Thanks again.

Jonathan

- Original Message - 
From: Jens Vagelpohl [EMAIL PROTECTED]

To: [Zope] Mailing List zope@zope.org
Sent: Tuesday, May 09, 2006 11:25 AM
Subject: Re: [Zope] A quick zope/python question



-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1


On 9 May 2006, at 16:18, Jonathan wrote:

gc= TransientObjectContainer at GarbageCollector
GarbageCollector exists


I think I am misunderstanding something basic about python/zope  here. 
Why does a TOC object evaluate to false?


I think the basic misunderstanding is about what happens if you say...

if FOO: ...

Python will try to evaluate using steps such as looking for methods  like 
__nonzero__, __len__, etc, and you're not aware what these may  evaluate 
to given your object. A Folder, for example, evaluates to  False when it 
is empty.


You should use a different test to determine if that item exists.

jens

-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.1 (Darwin)

iD8DBQFEYLRlRAx5nvEhZLIRAmSWAKCxP+IjWYHEC/J1E8zDvq0R2OUB1wCePA74
Fm8Oc74d3dn915gDefbR1Yo=
=FOpK
-END PGP SIGNATURE-
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - http://mail.zope.org/mailman/listinfo/zope-announce
http://mail.zope.org/mailman/listinfo/zope-dev )



___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


[Zope] python question

2005-07-01 Thread Jürgen Herrmann
hi all!

i think there should be no functional difference in the two code snippets
below, but is there any difference in performance?
(i know, the except AttributeError could possibly mask an AttributeError
in the called function...)

1.:
hook = getattr(o, '_before_transaction_commit', None)
if hook: hook()

2.:
try: o._before_transaction_commit()
except AttributeError: pass

regards, juergen herrmann
___

 XLhost.de - eXperts in Linux hosting 

Juergen Herrmann
Weiherweg 10, 93051 Regensburg, Germany
Fon:  +49 (0)700 XLHOSTDE [0700 95467833]
Fax:  +49 (0)721 151 463027

ICQ:  27139974  -  IRC: [EMAIL PROTECTED]
WEB:  http://www.XLhost.de
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] python question

2005-07-01 Thread Andreas Jung



--On 1. Juli 2005 17:10:58 +0200 Jürgen Herrmann 
[EMAIL PROTECTED] wrote:



hi all!

i think there should be no functional difference in the two code snippets
below, but is there any difference in performance?


You could benchmark it :-)

-aj

pgphfg7D56dFp.pgp
Description: PGP signature
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] python question

2005-07-01 Thread Fred Drake
On 7/1/05, Jürgen Herrmann [EMAIL PROTECTED] wrote:
 i think there should be no functional difference in the two code snippets
 below, but is there any difference in performance?

Don't know, and don't care; these are (at the Python level)
functionally different.

 (i know, the except AttributeError could possibly mask an AttributeError
 in the called function...)

Exactly.  Shouldn't be a real issue in a released version, but better
safe than sorry.

 1.:
 hook = getattr(o, '_before_transaction_commit', None)
 if hook: hook()

Better yet:

 hook = getattr(o, '_before_transaction_commit', None)
 if hook is not None: hook()


  -Fred

-- 
Fred L. Drake, Jr.fdrake at gmail.com
Zope Corporation
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists -
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


[Zope] python question

2005-05-31 Thread Kate Legere
I may be asking this in the wrong place but I have a tal page where I want
to use python to display only the most recent addition to a folder of files.


I can sort the folder and return all the files easily with:
 return sequence.sort(files, (('bobobase_modification_time', 'cmp',
'desc'),))

How do I return just the most recent one? (I tried sorting and then using
return max(files) but that doesn't give me all the properties and I need
them for display) 

Kate

___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


RE: [Zope] python question

2005-05-31 Thread Jim Abramson
sort() on a sequence does not return anything.  It operates on the
[mutable] sequence in place.

You probably want to push this bit into a python script where you have
more elbow room, and do this:

files.sort(etc,etc)
return files[0]

hth,
Jim
 

 -Original Message-
 From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On 
 Behalf Of Kate Legere
 Sent: Tuesday, May 31, 2005 3:35 PM
 To: Zope
 Subject: [Zope] python question
 
 I may be asking this in the wrong place but I have a tal page 
 where I want to use python to display only the most recent 
 addition to a folder of files.
 
 
 I can sort the folder and return all the files easily with:
  return sequence.sort(files, (('bobobase_modification_time', 'cmp',
 'desc'),))
 
 How do I return just the most recent one? (I tried sorting 
 and then using return max(files) but that doesn't give me all 
 the properties and I need them for display) 
 
 Kate
 
 ___
 Zope maillist  -  Zope@zope.org
 http://mail.zope.org/mailman/listinfo/zope
 **   No cross posts or HTML encoding!  **
 (Related lists -
  http://mail.zope.org/mailman/listinfo/zope-announce
  http://mail.zope.org/mailman/listinfo/zope-dev )
 
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists -
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] python question

2005-05-31 Thread J Cameron Cooper

Kate Legere wrote:

I may be asking this in the wrong place but I have a tal page where I want
to use python to display only the most recent addition to a folder of files.


I can sort the folder and return all the files easily with:
 return sequence.sort(files, (('bobobase_modification_time', 'cmp',
'desc'),))

How do I return just the most recent one? (I tried sorting and then using
return max(files) but that doesn't give me all the properties and I need
them for display) 


If it's sorted, you can use a Python slice::

 return sequence.sort(files, (('bobobase_modification_time',
  'cmp', 'desc'),))[0]

The trick is the last three characters. See 
http://docs.python.org/tut/node5.html


--jcc

--
Building Websites with Plone
http://plonebook.packtpub.com/
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


RE: [Zope] python question

2005-05-31 Thread Kate Legere
Thanks - turns out the real error was in the file dealing with it - all
fixed now.

Thanks for the help.

Kate

-Original Message-
From: Jim Abramson [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, May 31, 2005 2:38 PM
To: Kate Legere; Zope
Subject: RE: [Zope] python question

sort() on a sequence does not return anything.  It operates on the
[mutable] sequence in place.

You probably want to push this bit into a python script where you have
more elbow room, and do this:

files.sort(etc,etc)
return files[0]

hth,
Jim
 

 -Original Message-
 From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On 
 Behalf Of Kate Legere
 Sent: Tuesday, May 31, 2005 3:35 PM
 To: Zope
 Subject: [Zope] python question
 
 I may be asking this in the wrong place but I have a tal page 
 where I want to use python to display only the most recent 
 addition to a folder of files.
 
 
 I can sort the folder and return all the files easily with:
  return sequence.sort(files, (('bobobase_modification_time', 'cmp',
 'desc'),))
 
 How do I return just the most recent one? (I tried sorting 
 and then using return max(files) but that doesn't give me all 
 the properties and I need them for display) 
 
 Kate
 
 ___
 Zope maillist  -  Zope@zope.org
 http://mail.zope.org/mailman/listinfo/zope
 **   No cross posts or HTML encoding!  **
 (Related lists -
  http://mail.zope.org/mailman/listinfo/zope-announce
  http://mail.zope.org/mailman/listinfo/zope-dev )
 

___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] python question

2005-05-31 Thread Tino Wildenhain
Am Dienstag, den 31.05.2005, 15:34 -0400 schrieb Kate Legere:
 I may be asking this in the wrong place but I have a tal page where I want
 to use python to display only the most recent addition to a folder of files.
 
 
 I can sort the folder and return all the files easily with:
  return sequence.sort(files, (('bobobase_modification_time', 'cmp',
 'desc'),))
 
 How do I return just the most recent one? (I tried sorting and then using
 return max(files) but that doesn't give me all the properties and I need
 them for display) 

Use:

return sequence.sort(files, (('bobobase_modification_time',
'cmp','desc'),))[:3]


[:3] is a slice operator which returns

item0...item2

since you sorted descenting by modification time, the
first 3 objects should be the most recent 3.

Regards
Tino

___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


RE: [Zope] python question

2005-05-31 Thread Tino Wildenhain
Am Dienstag, den 31.05.2005, 15:38 -0400 schrieb Jim Abramson:
 sort() on a sequence does not return anything.  It operates on the
 [mutable] sequence in place.

sequence is a module in Zope. So she indeed was right.

___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


RE: [Zope] python question

2005-05-31 Thread Jim Abramson
blast!

i'll keep my mouth shut til I get the library memorized.

Jim 

 -Original Message-
 From: Tino Wildenhain [mailto:[EMAIL PROTECTED] 
 Sent: Tuesday, May 31, 2005 4:06 PM
 To: Jim Abramson
 Cc: Kate Legere; Zope
 Subject: RE: [Zope] python question
 
 Am Dienstag, den 31.05.2005, 15:38 -0400 schrieb Jim Abramson:
  sort() on a sequence does not return anything.  It operates on the 
  [mutable] sequence in place.
 
 sequence is a module in Zope. So she indeed was right.
 
 
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists -
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


[Zope-dev] zope/python question

2002-04-24 Thread Dirk Datzert

Hi,

I saw in some products a __of__ operator and looked at the python.org
for a documentation.

I didn't found one.

Can anybody tell me what the __of__ does ?
Or tell me a URL for the documentation of the __of__ operator ?

Thanks,
Dirk



___
Zope-Dev maillist  -  [EMAIL PROTECTED]
http://lists.zope.org/mailman/listinfo/zope-dev
**  No cross posts or HTML encoding!  **
(Related lists - 
 http://lists.zope.org/mailman/listinfo/zope-announce
 http://lists.zope.org/mailman/listinfo/zope )



Re: [Zope-dev] zope/python question

2002-04-24 Thread Steve Alexander

Dirk Datzert wrote:
 Hi,
 
 I saw in some products a __of__ operator and looked at the python.org
 for a documentation.
 
 I didn't found one.
 
 Can anybody tell me what the __of__ does ?

It creates an acquisition wrapper for the object the method belongs to, 
in the context of the object you pass as the argument.

It is part of the Extension Class package.


 Or tell me a URL for the documentation of the __of__ operator ?

http://cvs.zope.org/~checkout~/Zope/lib/Components/ExtensionClass/doc/Acquisition.stx?rev=HEAD

http://cvs.zope.org/~checkout~/Zope/lib/Components/ExtensionClass/doc/ExtensionClass.stx?rev=HEAD


--
Steve Alexander



___
Zope-Dev maillist  -  [EMAIL PROTECTED]
http://lists.zope.org/mailman/listinfo/zope-dev
**  No cross posts or HTML encoding!  **
(Related lists - 
 http://lists.zope.org/mailman/listinfo/zope-announce
 http://lists.zope.org/mailman/listinfo/zope )