Re: [lxml] (no subject)

schwehr@ccom.unh.edu, 21.07.2011 21:18:
There are two main options. 1) You can subtype the ElementMaker that is used in pykml.factory and override the ".coord()" method explicitly. 2) You can register a PyType in lxml.objectify that handles types, lists, etc., either specifically for the "coord" tag or always. That's the more general and proper way, and it also allows you to get a tuple back when you read the value of such an XML tag. http://lxml.de/objectify.html#how-data-types-are-matched http://lxml.de/objectify.html#defining-additional-data-classes You may want to integrate this feature into pykml itself, BTW, the authors may be happy about a patch. Have you talked to them? Stefan

On 7/21/11 9:43 PM, Stefan Behnel wrote:
Hi Stefan, Thanks for the help. I think I just haven't quite wrapped my head around it yet. I've been trying to figure out option 1 and not getting anything to work. Here is my closest guess: class GX_ElementMakerCustom(objectify.ElementMaker): def coord(self,values): # FIX: make this handle variable number of args. e.g. y=None, z=None print 'in coord!' if isinstance(values,tuple): values = ' '.join(values()) return values GX_ElementMaker = GX_ElementMaker( annotate=False, namespace=nsmap['gx'], nsmap={'gx': nsmap['gx']}, ) Nothing changes when I create node like c = GX.coord( (1,2) ) # I never see Option 2: I was working down the path of adding a TupleType that would then detect it's parent node and set itself appropriately based on that to be space or comma separated. I've got the type registering fine. There are at least two different formats that the tuple must become. As stringify doesn't have enough information about the tree, I was messing with inspect.currentframe().f_back to try to identify the parent from inside of stringify_tuple. That seems very awkward and brittle. I would really like it to work both directions with tuples. Am I missing something that would let me know what node type the tuple was serializing/deserializing under? As the author just posted, he and I have been talking already :) Any help getting through one type would be a huge help. Once I get it, I will try skim through the KML spec and see if this kind of thing happens elsewhere. Thanks!! -kurt

Something along those lines might get you going: 1) (Note: I've never used customized ElementMakers, so no idea if there's a better way to do it)
2)
As you can see, I haven't yet bothered to address the problem that I convert the tuple values to strings, which of course should be done for the .pyval property . I'd say there's also an additional 2b) solution if you rather want to base the tuple-stringification on the tag 'coord' than on the type of the python type of the attribute value. Might read up on using custom element classes in general: http://lxml.de/element_classes.html Holger -- Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de

On 7/21/11 9:43 PM, Stefan Behnel wrote:
Hi Stefan, Thanks for the help. I think I just haven't quite wrapped my head around it yet. I've been trying to figure out option 1 and not getting anything to work. Here is my closest guess: class GX_ElementMakerCustom(objectify.ElementMaker): def coord(self,values): # FIX: make this handle variable number of args. e.g. y=None, z=None print 'in coord!' if isinstance(values,tuple): values = ' '.join(values()) return values GX_ElementMaker = GX_ElementMaker( annotate=False, namespace=nsmap['gx'], nsmap={'gx': nsmap['gx']}, ) Nothing changes when I create node like c = GX.coord( (1,2) ) # I never see Option 2: I was working down the path of adding a TupleType that would then detect it's parent node and set itself appropriately based on that to be space or comma separated. I've got the type registering fine. There are at least two different formats that the tuple must become. As stringify doesn't have enough information about the tree, I was messing with inspect.currentframe().f_back to try to identify the parent from inside of stringify_tuple. That seems very awkward and brittle. I would really like it to work both directions with tuples. Am I missing something that would let me know what node type the tuple was serializing/deserializing under? As the author just posted, he and I have been talking already :) Any help getting through one type would be a huge help. Once I get it, I will try skim through the KML spec and see if this kind of thing happens elsewhere. Thanks!! -kurt

Something along those lines might get you going: 1) (Note: I've never used customized ElementMakers, so no idea if there's a better way to do it)
2)
As you can see, I haven't yet bothered to address the problem that I convert the tuple values to strings, which of course should be done for the .pyval property . I'd say there's also an additional 2b) solution if you rather want to base the tuple-stringification on the tag 'coord' than on the type of the python type of the attribute value. Might read up on using custom element classes in general: http://lxml.de/element_classes.html Holger -- Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de
participants (4)
-
jholg@gmx.de
-
Kurt Schwehr
-
Stefan Behnel
-
Tyler Erickson