Comments (5)
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.
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.
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.
@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.
Support for processing hooks to allow this capability is now available in the latest release from PyPi
from declxml.
Related Issues (19)
- Exception messages are too vague HOT 3
- Support for reading data from attributes on childless objects? HOT 2
- declxml on Arch Linux HOT 3
- None values for namedtuples and objects not handled when serializing
- Access a node's parent attribute HOT 2
- Parser fails on non ASCII strings on python 2 HOT 3
- Array with nodes that have attributes AND values HOT 3
- Use attributes as keys for dictionaries HOT 19
- Cannot parse XML into frozen attrs classes
- Add type hints to public API
- Is there any array attributes HOT 2
- non-required named_tuple HOT 2
- Recursive Elements
- Optional aggregate processors - a pain point
- Create processor from typing annotations
- Order changed when seriallized to string
- Calling `xml.serialize_to_string()` without an indent leaves out `<?xml version="1.0" encoding="utf-8"?>`
- Parse failure on dictionary with attribute and value HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from declxml.