Giter Club home page Giter Club logo

Comments (5)

gatkin avatar gatkin commented on June 23, 2024

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?

from declxml.

EmilStenstrom avatar EmilStenstrom commented on June 23, 2024

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.

from declxml.

marnunez avatar marnunez commented on June 23, 2024

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

from declxml.

gatkin avatar gatkin commented on June 23, 2024

@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.

from declxml.

gatkin avatar gatkin commented on June 23, 2024

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

from declxml.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.