jacebrowning / yorm

Automatic object-YAML mapping for Python.
https://yorm.readthedocs.io
MIT License
27 stars 6 forks source link

Mapping? #145

Closed AstraLuma closed 6 years ago

AstraLuma commented 7 years ago

Is there a variation on Dictionary that implements mappings?

That is to say, it doesn't have a fixed list of keys, but all keys are Integers and all values are Strings.

jacebrowning commented 7 years ago

Can you show me an example of the YAML you expect?

AstraLuma commented 7 years ago
my_mapping:
  50: spam
  60: eggs

This sort of data structure is not currently expressible in the YORM schema, where I don't know the number of rows in advance, but I do know the shape of the rows.

(Ok, I could have a list of yet-another-class, but that seems needless tedious for a simple key-value mapping.)

jacebrowning commented 7 years ago

Yeah, currently YORM expects something like:

my_mapping:
- key: 50
  value: spam
- key: 60
  value: spam

so that it can enforce a schema on each nested object. What does your class look like before adding the YORM decorators? Certainly you don't have attributes that are integers.

AstraLuma commented 7 years ago

This is a YORM-native schema.

The use case is that in a GitLab tool, I have (attached to a data object) a list of merge request IDs, and a note (comment) ID within that MR. The Pythonic way to do this would to have a dict mapping MR IDs to Note IDs.

jacebrowning commented 7 years ago

You could always try using yorm.types.Object as the attribute type which should handle arbitrary dictionary-like objects (but without schema validation).

Or try extending one of the existing types (see yorm.types.extended for examples).


I have found that explicitly defining keys for values (i.e. name: value) is better off in the long run and easier to reason about the data outside of YORM.

AstraLuma commented 7 years ago

Object doesn't invoke autosave, does it?

jacebrowning commented 7 years ago

The auto_save option only applies to the root object -- the one sync() is applied to.

jacebrowning commented 6 years ago

Did you have any luck with this approach?

AstraLuma commented 6 years ago

Yeah, it's working and I haven't touched the code in months.

jacebrowning commented 6 years ago

Glad to hear.