jacebrowning / yorm

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

Attributes with unkown types #95

Closed hoytnix closed 8 years ago

hoytnix commented 8 years ago

In Json there are two data-types that are unknown to the mapper:

dicts:

"John Doe": {
    "student_id": 123,
    "gpa": 3.0,
    "school": "GVSU",
    "year": 2009
}

lists:

test_scores = [0.80, 0.69, 0.93, 0.58]
jacebrowning commented 8 years ago

Could you include the error message and stack trace?

jacebrowning commented 8 years ago

Is this still a problem?

hoytnix commented 8 years ago

The following code resulted in the subsequent traceback for me when using the latest develop branch:

Likewise, I received this similar error using the latest PyPi build:

import yorm
from yorm.types import String, Integer, Float, Dictionary, List

@yorm.attr(name=String, number=Integer, gpa=Float, grades=List, foo=Dictionary)
@yorm.sync('my.json')
class Student:
    def __init__(self, name, number, grades, foo):
        self.name = name
        self.number = number
        self.gpa = 0.0
        self.grades = grades
        self.foo = foo

def main():
    s = Student(name="John", number=42, grades=[0.15, 0.39, 0.78], foo={"bar": "spam"})
    s.name = "John Doe"

if __name__ == '__main__':
    main()
#manjaro@alisa:/mnt/my_home/Dev/Contrib/jacebrowning/yorm:[0]$ python json_test.py
Traceback (most recent call last):
  File "json_test.py", line 21, in <module>
    main()
  File "json_test.py", line 16, in main
    s = Student(name="John", number=42, grades=[0.15, 0.39, 0.78], foo={"bar": "spam"})
  File "/mnt/my_home/Dev/Contrib/jacebrowning/yorm/yorm/utilities.py", line 97, in modified_init
    sync_object(self, path, attrs, **kwargs)
  File "/mnt/my_home/Dev/Contrib/jacebrowning/yorm/yorm/utilities.py", line 55, in sync_object
    mapper.store()
  File "/mnt/my_home/Dev/Contrib/jacebrowning/yorm/yorm/mapper.py", line 29, in wrapped
    return method(self, *args, **kwargs)
  File "/mnt/my_home/Dev/Contrib/jacebrowning/yorm/yorm/mapper.py", line 48, in wrapped
    result = method(self, *args, **kwargs)
  File "/mnt/my_home/Dev/Contrib/jacebrowning/yorm/yorm/mapper.py", line 239, in store
    data2 = converter.to_data(value)
  File "/mnt/my_home/Dev/Contrib/jacebrowning/yorm/yorm/types/containers.py", line 21, in to_data
    value2 = cls.create_default()
  File "/mnt/my_home/Dev/Contrib/jacebrowning/yorm/yorm/bases/converter.py", line 36, in create_default
    return cls.__new__(cls)
  File "/mnt/my_home/Dev/Contrib/jacebrowning/yorm/yorm/types/containers.py", line 16, in __new__
    raise NotImplementedError(msg)
NotImplementedError: Dictionary class must be subclassed to use
jacebrowning commented 8 years ago

Currently, that error is expected. You need to tell it the type of the items in grades.

Until https://github.com/jacebrowning/yorm/issues/58 is implemented, you need to extend the List class to make a FloatList (or Grades) class.

jacebrowning commented 8 years ago

58 is now closed. Try this again with:

@yorm.attr(name=String, number=Integer, gpa=Float, grades=List.of_type(Float)
                                                               ^^^^^^^