[Zope3-Users] Using 'if object' evaluates false even if object exists

2007-04-12 Thread Giovannetti, Mark
Hi list,

Hope everyone's having a splendid day/evening/whatever.

I've encountered a strange error.  I have two chunks
of nearly identical code below.  The only difference
is the first if statement.  One has:

if context:

the other has:

if context is not None

The first fails to be true even if context is not None.
This can be seen by the output from the logs showing the
context variable as:

777 Context
books.finance.fiscalyear.FiscalYearSQLContainer object at 0xad90aac
TitledAbsoluteURL.breadcrumbs, calling getBreadCrumbTitle
getBreadCrumbTitle, no context.
 
When I changed it to the second form (i.e. not None) the
context is OK and the code works.  Why does the object
'context' not evaluate to true?  This is a nasty gotcha that
I'd like to understand.

I thought that any object that exists (and doesn't equal
zero, '' or False) resolves to true.  Can anyone help?

I'm still pretty new to python and zope so be gentle.

I've made sure that the proper parameters are being passed
to the function and so on.

Thanks in advance!

Mark

P.S. I hope there wasn't any horrible wrapping...

 didn't work =
= source ==
def getBreadCrumbTitle(context=None, default=None):

Helper function to get a title for an URL breadcrumb list.

title = None
if context:
print , removeSecurityProxy(context)
title = getattr(context, 'title', None)

if title is None:
print Title not an attribute of context, setting to
default.
title = default
try:
annotations = IZopeDublinCore(context)
if annotations:
print Annotations, annotations
try:
title = annotations.title
print Annotation's Title, annotations.title
except KeyError:
print Annotation's Title KeyError
pass
else:
print Annotations adapted but empty.

except TypeError:
print Annotations not available-No Adaption to Dublin
Core
pass
else:
print getBreadCrumbTitle, Title not None-gotten from
getattr

else:
print getBreadCrumbTitle, no context.

if title is None:
raise TypeError(_insufficientTitleContext)
else:
print getBreadCrumbTitle, Title is not None, title

return title

== log ===
 {'__name__': u'FY', '_class': class
'books.finance.fiscalyear.FiscalYear', '_className':
u'books.finance.fiscalyear.FiscalYear', '__parent__':
zope.app.folder.folder.Folder object at 0xad747ac}
TitledSiteAbsoluteURL.breadcrumbs, calling getBreadCrumbTitle
 zope.app.folder.folder.Folder object at 0xad747ac
Title not an attribute of context, setting to default.
Annotations zope.dublincore.annotatableadapter.ZDCAnnotatableAdapter
object at 0xadd8cac
Annotation's Title huh
getBreadCrumbTitle, Title is not None huh
777 Context
books.finance.fiscalyear.FiscalYearSQLContainer object at 0xad90aac
TitledAbsoluteURL.breadcrumbs, calling getBreadCrumbTitle
getBreadCrumbTitle, no context.

==


 worked =
 source =
def getBreadCrumbTitle(context=None, default=None):

Helper function to get a title for an URL breadcrumb list.

title = None
if context is not None:
print , removeSecurityProxy(context)
title = getattr(context, 'title', None)

if title is None:
print Title not an attribute of context, setting to
default.
title = default
try:
annotations = IZopeDublinCore(context)
if annotations:
print Annotations, annotations
try:
title = annotations.title
print Annotation's Title, annotations.title
except KeyError:
print Annotation's Title KeyError
pass
else:
print Annotations adapted but empty.

except TypeError:
print Annotations not available-No Adaption to Dublin
Core
pass
else:
print getBreadCrumbTitle, Title not None-gotten from
getattr

else:
print 4, removeSecurityProxy(context)
print getBreadCrumbTitle, no context.

if title is None:
raise TypeError(_insufficientTitleContext)
else:
print getBreadCrumbTitle, Title is not None, title

return title

== log 
 {'__name__': u'FY', '_class': class
'books.finance.fiscalyear.FiscalYear', '_className':

Re: [Zope3-Users] Using 'if object' evaluates false even if object exists

2007-04-12 Thread Aleksander Kowalczyk

On 4/12/07, Giovannetti, Mark [EMAIL PROTECTED] wrote:


Hi list,

Hope everyone's having a splendid day/evening/whatever.

I've encountered a strange error.  I have two chunks
of nearly identical code below.  The only difference
is the first if statement.  One has:

if context:

the other has:

if context is not None

The first fails to be true even if context is not None.
This can be seen by the output from the logs showing the
context variable as:



Possibly a context is a container-like object.
For containers operation
if context is equal to if len(context)  0.  So even if context is not
None, it still may be False if the container does not contain any elements.
___
Zope3-users mailing list
[EMAIL PROTECTED]
http://mail.zope.org/mailman/listinfo/zope3-users


[Zope3-Users] Can interfaces be mutated cleanly?

2007-04-12 Thread Christian Theune
Hi,

I didn't find anything in the documentation or google on this:

I want to define an interface like:

class IMySchema(zope.interface.Interface):
   pass

Later, I want to use ore.alchemist which currently is able to take a
SQLAlchemy table description and turn it into a new interface/schema.

I want to extend this code so that I can pass it an existing interface
to modify it (update) with the schema definition.

I need this so that the interface is:

a) present in the code when reading it

b) available for ZCML configuration (it's a problem for us right now
because the database/SQLAlchemy stuff is setup up a little bit later
after executing a couple of ZCML actions)

While looking into it, we found that interfaces do not offer a public
way to modify the attributes they have, and modifying the internal data
structures would be both evil and cumbersome.

Any hints?

Christian

-- 
gocept gmbh  co. kg - forsterstraße 29 - 06112 halle/saale - germany
www.gocept.com - [EMAIL PROTECTED] - phone +49 345 122 9889 7 -
fax +49 345 122 9889 1 - zope and plone consulting and development


signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil
___
Zope3-users mailing list
[EMAIL PROTECTED]
http://mail.zope.org/mailman/listinfo/zope3-users