Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send xml data to constructor #12

Closed
EmilStenstrom opened this issue Jun 3, 2018 · 5 comments
Closed

Send xml data to constructor #12

EmilStenstrom opened this issue Jun 3, 2018 · 5 comments

Comments

@EmilStenstrom
Copy link

Hi! I really like declxml, and it almost does exactly what I was looking for. I understand if this is not something you want to support.

I would like to construct a custom class from an XML file, but do the transformations of the data in the constructor. As I understand the setup right now, attributes are "magically" set from declxml, instead of being sent to the constructor where I could transform them before saving them.

  1. Is there a way to do this with declxml?
  2. If not, is this something you would consider adding?
@gatkin
Copy link
Owner

gatkin commented Jun 3, 2018

That is a great suggestion. You are right, currently there is no way in declxml to have values sent to a class constructor; declxml calls setattr to set the attribute values on an object.

Hmm...I think this would be possible. When parsing XML data for objects, declxml reads all the data into a dict first and then creates a new instance of the object before setting all attribute values with setattr. I think receiving the XML data in an object's constructor would require the constructor to take all of its arguments as keyword arguments (since otherwise we don't know what order the parameters are to be passed in to the constructor). Then, declxml could call that constructor by unpacking the dictionary of all the parsed XML data.

What do you think of that solution?

@EmilStenstrom
Copy link
Author

That sounds like a great idea! Another would be to just take a "data" argument, and send in all the raw data to the constructor. Either way my code would be much nicer, when not having to create an object with the raw values first, and then send them to the constructor for refinement.

@marnunez
Copy link

marnunez commented Jun 4, 2018

Ohh this would be really nice, I've used construct's Adapters like this. In my case, I'd rather not send to the constructor just a single attribute. I'm facing XML tags like the following:

<track coords="X Y Z" data="S 255 33845 12623 4773 33791..." description="" label="cat7" nItems="-1" nPoints="584" nSegs="1" scaleFactor="10000.0"/>

where I need the coord attribute for making sense out of the data attribute, which is in the form of

data = "S start_time X1 Y1 Z1 X2 Y2 Z2... S start_time_2 X1 Y1 Z1"

but if coords="V", for example, it becomes

data = "S start_time V1 V2 V3 V4 V5 V6... S start_time_2 V1 V2 V3"

Embedding this into the parsing logic would free me for the trouble of case by case serialising and deserialising and permit me to work with the data as lists or numpy arrays

@gatkin
Copy link
Owner

gatkin commented Jun 12, 2018

@marnunez would the ValueMapping feature being added in #16 support your use case? With these new changes, you can define a to_xml and a from_xml function. The from_xml function could take a raw dictionary parsed from the XML data and provide it to your object's constructor with whatever arguments it needs, and the to_xml function could ask the object to convert itself back into a dictionary so that it can be serialized XML.

@gatkin gatkin changed the title Send xml data to contructor Send xml data to constructor Jun 16, 2018
@gatkin
Copy link
Owner

gatkin commented Jun 26, 2018

Support for processing hooks to allow this capability is now available in the latest release from PyPi

@gatkin gatkin closed this as completed Jun 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants