def article_to_json(article: Article): static_fields = article.static_fields() - { "id", "project_id", "project", "properties" } static_fields = {fn: getattr(article, fn) for fn in static_fields} return dict(static_fields, properties=dict(article.get_properties().items()))
def from_field_name(cls, field_name: str, **kwargs): """Construct a category object corresponding to the field_name's type. For example, the field 'date' would map to a IntervalCategory, while author would map to TextCategory. @param kwargs: additional parameters passed to corresponding Category""" is_json_field = field_name not in Article.static_fields() field_type = get_property_primitive_type(field_name) if field_type in (int, str, float): return ArticleFieldCategory(is_json_field=is_json_field, field_name=field_name, **kwargs) elif field_type == datetime.datetime: return IntervalCategory(is_json_field=is_json_field, field_name=field_name, **kwargs) else: raise ValueError("Did not recognize primitive field type: {} (on {})".format(field_type, field_name))
def from_field_name(cls, field_name: str, **kwargs): """Construct a category object corresponding to the field_name's type. For example, the field 'date' would map to a IntervalCategory, while author would map to TextCategory. @param kwargs: additional parameters passed to corresponding Category""" is_json_field = field_name not in Article.static_fields() field_type = get_property_primitive_type(field_name) if field_type in (int, str, float): return ArticleFieldCategory(is_json_field=is_json_field, field_name=field_name, **kwargs) elif field_type == datetime.datetime: return IntervalCategory(is_json_field=is_json_field, field_name=field_name, **kwargs) else: raise ValueError( "Did not recognize primitive field type: {} (on {})".format( field_type, field_name))