openergy / omemdb

A python in-memory relational database.
Mozilla Public License 2.0
0 stars 1 forks source link

get Record's metadata #10

Open Lefort-Antoine opened 1 year ago

Lefort-Antoine commented 1 year ago

Hy

How can I get metadata ?

In this example I found no way to get the metadata of the records loc2.

`from omemdb import Db, Record from omemdb.packages.omarsh import Schema, fields, validate, ValidationError

class Loc(Record):

    class Schema(Schema):
        pk = fields.String(required=True)
        insee = fields.String(
            missing=None,
            metadata=dict(level=1, name="Numero Insee"))
        p2 = fields.Float(
            missing=True,
            metadata=dict(level=1, name="Le parametre p2"),
            validate=validate.Range(0, 90))

class AppDb(Db):
    models = [
        Loc
    ]
db = AppDb()
db.loc.add(pk="1", insee="0112")
loc2 = db.loc.add(pk="2", insee="099")

`

Regards

ccruveiller commented 1 year ago

Hi @Lefort-Antoine ! Sorry for the delay, I'll look into your different issues.

Lefort-Antoine commented 1 year ago

Hy Could you have a look on the issues please @ccruveiller ?

Lefort-Antoine commented 1 year ago

Found a way. Still searching best one.

self.db.loc._dev_schema._declared_fields["insee"].metadata

I add for each Record ` @classmethod def get_metadata(cls): return {field : cls.Schema._declared_fields[field].metadata for field in cls.Schema._declared_fields}

`

Could be interesting for next version ?

ccruveiller commented 2 months ago

Hi metadata can be handled and here is an example to extract a records metadata:

db = AppFields()
# check correct value
country_mapping_d = {"france": True, "spain": False}
db.immutable_dict_field_record.add(ref="hello", country_map=country_mapping_d)
metadata_d = db.immutable_dict_field_record.one().get_metadata_dict()

Also metadata fields can directly be declared through record's field attributes, ex:

        loop_design_temperature_difference = fields.Float(
            missing=7,
            ### these attributes are metadata
            description="Design loop design temperature difference used for autosizing",
            topic="Sizing",
            eplus_usage="Sizing:Plant",
        )