#221 fromValue() helper method for simpleType enumerations

PyXB 1.2.4
closed
None
fixed
Component model
trivial
PyXB 1.2.3
enhancement
2013-11-29
2013-11-29
No

Over the past few months I've noticed our code is crawling with code like:

pyxb_obj = [x for x in pyxb_generated.simpleType.iteritems() if x.value()=='Some Value From Database'][0]

Where we want the PyXB generated object for a given constant value. JAXB has a really nice fromValue() method, which works like:

jaxbObject = simpleType.fromValue("Some Value From Database")

I've had a bit of a dig around PyXB, doing dir() on pyxb_generated.simpleType, etc. and can't see an equivalent. It would be really nice if PyXB had the fromValue() convenience / helper method. It would certainly clean up our code.

Discussion

  • Peter A. Bigot

    Peter A. Bigot - 2013-11-29
    • status changed from new to assigned

    The functionality seems reasonable.

    I'm a little unhappy with how these features intrude on the type namespace (for example, the existing approach silently breaks if the string "iteritems" is a member of the enumeration). So the interface needs to change, which is probably a PyXB 1.3 thing. I'm thinking something like something like:

       enum_inst = pyxb.EnumFromValue(simpleType, 'Some Value From Database')
    

    or

       enum_inst = simpleType._enumFromValue('Some Value From Database')
    

    depending on an interface design decision. The second one is likely to be the basis of the first, while the first would be the new public "approved" API.

    For now, what you might do is create a mix-in class that creates a dict on-demand from the value of iteritems and adds _enumFromValue to do the lookup, and use PyXB's extension mechanism to add that as a superclass to a custom subclass that supersedes the binding class for the cases where you do this frequently.

     
  • Peter A. Bigot

    Peter A. Bigot - 2013-11-29
    • status changed from assigned to closed
    • resolution set to fixed

    Fixed in next branch in commit below, but having done so I'm not sure what you mean "fromValue" to be: is it given the typed Python value that compares equal to the corresponding enumeration value, or the unicode expression of the enumeration value in an XML document?

    At any rate, you want http://pyxb.sourceforge.net/api/pyxb.binding.facets.CF_enumeration-class.html#elementForValue or its companion valueForUnicode. You can get those today with:

       enum_inst = simpleType._CF_enumeration.elementForValue('Some Value From Database').value()
       enum_inst = simpleType._CF_enumeration.valueForUnicode(u'Some Value From Database')
    

    All this enhancement does is hide the existence of _CF_enumeration:

       enum_inst = simpleType._elementForValue('Some Value From Database').value()
       enum_inst = simpleType._valueForUnicode(u'Some Value From Database')
    
    commit 35754b0d966fd902996c53ad6e3a3e6555f6e7ed
    Author: Peter A. Bigot <pab@pabigot.com>
    Date:   Fri Nov 29 07:05:57 2013 -0600
    
        trac/221: forward to _CF_enumeration helper methods in enumeration_mixin
    
        Do this without intruding any further on the namespace reserved for the
        user in the simpleType class that inherits the mixin.
    
    :100644 100644 07bd2f8... 309a181... M  pyxb/binding/basis.py
    :000000 100644 0000000... 095892c... A  tests/trac/test-trac-0221.py
    
     
  • Peter A. Bigot

    Peter A. Bigot - 2013-11-29

    Heh. Or you could just do

       enum_inst = simpleType('Some Value From Database')
       assert isinstance(enum_inst, simpleType)
    

    which just works and always has, as long as the value is valid for the enumeration (if it isn't, you get a pyxb.SimpleFacetValueError). This works for both the value as a Python type (if it isn't a string) or the Unicode expression of the value from a document.

    At any rate, if none of these work for you please follow up with more details.

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks