So, these kinds of issues were the subject of the coercion project at SD7. Robert Bradshaw, Mike Hansen, Bobby Moretti and I are currently in the process of changing a lot of the infrastructure behind parents, elements, etc. If you want to help (or want to see our current progress) see the sprint page at http://wiki.sagemath.org/days7/coercion or contact one of us via e-mail.
Given that, I'll answer your questions, both as Sage exists now and as we see it existing in a few months. I'm wondering if there is some documentation or a howto somewhere that > describes the process of adding new structures to Sage. We're working on this. See the wiki page I referenced above for the current progress. > For example, > we want to add poset functionality to Sage---Peter Jipsen and I began > working on this at Sage Days 7. So we created a Poset class that > inherits from ParentWithBase (because inheriting from Parent isn't > support if I want to have elements---something that should be fixed, I > guess), Yup. This will be one of the changes that we're making. ParentWithBase and ParentWithGens are both disappearing, and both generator and base functionality is getting pushed up to CategoryObject (a superclass of parent) so that you can have objects with generators and bases but without elements. For now you should probably inherit from ParentWithBase and have base = self. > and a PosetElement class that inherits from the Element class. Yes, you should still be inheriting from Element. If this is not correct, then someone should tell me now, and the > better way to do this. > > Also, I have some technical questions. > > 1. Can someone describe to me how ParentWithBase works? Does each > instance of Element store the parent poset? I imagine this is not the > case as it would mean several copies of the Parent will be stored in > memory. So it's some kind of identifier for the poset? This is the case, because elements need to know their parent. Since Python objects are stored as pointers, this isn't a huge memory overhead. But you can just treat it as storing the parent, and get access to it by calling self.parent(). > 2. My PosetElement class accepts two arguments: the Parent poset P and > a label for the poset element. The problem I am having here is that I > can create an instance of PosetElement with Parent poset P, but this > element won't belong to P (and it shouldn't). But then the parent > function returns P on this new element. Here is a psuedo-example: > > sage: P = FinitePoset([0,1,2]) > sage: y = PosetElement(P,3) > sage: parent(y) == P > True > sage: y in P > False Your __init__ method on the element should check to see if the input it gets generates a valid element of the parent. If it doesn't, raise a ValueError. One generally doesn't expose the element class directly to the user. Instead, the better behavior is to use the __call__ method on Parent and the write P(3). I'm probably messing something up with my class definitions. (I'm new > to OOProgramming and Sage.) I'm including a very basic implementation > of the classes below. Any suggestions? > > Take care, > Franco > > -- > > from sage.structure.element import Element > > class FinitePoset(ParentWithBase): > def __init__(self,data=None): > Parent.__init__(self) > self.elements=[] > if not data is None: > for x in data: > self.elements.append(PosetElement(self,x)) You can write "data is not None" if you want: Python has an "is not" operator. def __contains__(self,x): > return x in self.elements > > def list(self): > return self.elements > > class PosetElement(Element): > def __init__(self,poset,label): > Element.__init__(self,poset) This __init__ method should check that the label actually belongs to the parent. You can have a parameter check, by default True, which is able to disable this checking. Let me know if you have more questions. And for those of you out there interested in coercion, I'll have my bundle posted as soon as I get a chance to tie off some loose ends. David --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---