I have a folderish Python product called "Customer". When an instance of this product is created, I want it to contain a folder called "Vehicles" which is a normal OFS.Folder that will contain Vehicle objects owned by the customer. If I create a manage_afterAdd method in the Customer class, what does it need to call to create a folder called "Vehicles"? Or, if the manage_afterAdd is not a good way to accomplish this, what is better? Thanks in advance.
Chad Nantais writes:
I have a folderish Python product called "Customer". When an instance of this product is created, I want it to contain a folder called "Vehicles" which is a normal OFS.Folder that will contain Vehicle objects owned by the customer. If I create a manage_afterAdd method in the Customer class, what does it need to call to create a folder called "Vehicles"? Or, if the manage_afterAdd is not a good way to accomplish this, what is better? I would do it in the constructor for your product instances. It probably looks somehow like this:
def addMyProduct(self,id): myInstance= MyProduct() myInstance._setId(id) self._setObject(id,myInstance) # you add here myInstance= getattr(self,id) # to acquisition wrap it childFolder= Folder() childFolder._setId('some_nice_id') myInstance._setObject('some_nice_id',childFolder) # here continues your previous code. ... Dieter
On 3 Jul 2002, at 20:14, Dieter Maurer wrote:
It probably looks somehow like this:
def addMyProduct(self,id): myInstance= MyProduct() myInstance._setId(id) self._setObject(id,myInstance) # you add here myInstance= getattr(self,id) # to acquisition wrap it childFolder= Folder() childFolder._setId('some_nice_id') myInstance._setObject('some_nice_id',childFolder) # here continues your previous code.
What is the purpose of myInstance._setId(id)? Is it necessary? In SimpleItem, it doesn't anything but self.__name__=id. Most products don't use _setId, the Developers Guide doesn't even mention it, but just contains the IMHO somewhat cryptic sentence about OFS.SimpleItem: id or __name__ All Item instances must have an id string attribute which uniquely identifies the instance within it's container. As an alternative you may use __name__ instead of id. Now _setObject in ObjectManager already adds a string attribute named "id". So in fact _setId seems redundant and should be eleminated from the API. Ugly. -- Wolfgang Strobl
Wolfgang Strobl writes:
... What is the purpose of myInstance._setId(id)? "_setId" is very well named.
This well chosen name makes questions as the above superfluous ;-)
Is it necessary? Each Zope site object needs an id!
Depending on the object type, the id can be provided in a different way. But, it is a very good idea to use "_setId". This is very clear and explicit.
... Now _setObject in ObjectManager already adds a string attribute named "id". Are you sure?
When I just tried to verify your statement, I have been unable to see that the "set" object gets an "id" attribute...
So in fact _setId seems redundant and should be eleminated from the API. Don't remove the clear and well named methods from the API!
Dieter
On 5 Jul 2002, at 22:53, Dieter Maurer wrote:
Wolfgang Strobl writes:
... What is the purpose of myInstance._setId(id)? "_setId" is very well named.
This well chosen name makes questions as the above superfluous ;-)
I would have called it _set__name__, setName or something, but not setId.
Is it necessary? Each Zope site object needs an id!
But _setId doesn't set an id!
Depending on the object type, the id can be provided in a different way. But, it is a very good idea to use "_setId". This is very clear and explicit.
... Now _setObject in ObjectManager already adds a string attribute > named "id". Are you sure?
Well, I checked again. It creates a string property, but doesn't set a data attribute. Well, that's the problem with "using the source" when confronted with missing documentation and a somewhat baroque terminology: one sometimes fails. It's no replacement for specs and documentation.
When I just tried to verify your statement, I have been unable to see that the "set" object gets an "id" attribute...
But you noticed that _setId doesn't either?
So in fact _setId seems redundant and should be eleminated from the API. Don't remove the clear and well named methods from the API!
I'm still in search of a recipe how to tell clear and well named methods from confusingly and misleadingly named ones. Given that _setId doesn't do what it says, I'm not quite sure wether it is in the former or in the latter category. -- Wolfgang Strobl
Wolfgang Strobl <ws@mystrobl.de> wrote:
On 5 Jul 2002, at 22:53, Dieter Maurer wrote:
Wolfgang Strobl writes:
... What is the purpose of myInstance._setId(id)? "_setId" is very well named.
This well chosen name makes questions as the above superfluous ;-)
I would have called it _set__name__, setName or something, but not setId.
You're looking at the wrong one. Look in CopySupport.CopySource, which SimpleItem.SimpleItem inherits from (through Item): def _setId(self, id): # Called to set the new id of a copied object. self.id=id
Each Zope site object needs an id!
But _setId doesn't set an id!
Yes it does. Florent -- Florent Guillaume, Nuxeo (Paris, France) +33 1 40 33 79 87 http://nuxeo.com mailto:fg@nuxeo.com
Wolfgang Strobl writes:
On 5 Jul 2002, at 22:53, Dieter Maurer wrote:
Wolfgang Strobl writes:
... What is the purpose of myInstance._setId(id)? "_setId" is very well named.
This well chosen name makes questions as the above superfluous ;-)
I would have called it _set__name__, setName or something, but not setId.
Is it necessary? Each Zope site object needs an id!
But _setId doesn't set an id! It does....
You are confused by the fact that some object types use the "__name__" attribute to remember their id. They are allowed to do this as it is an implementation detail. Use "_setId" to set the id and "getId" to fetch it. Leave it to the object type how it stores the id internally. Dieter
participants (4)
-
Chad Nantais -
Dieter Maurer -
Florent Guillaume -
Wolfgang Strobl