Git Product home page Git Product logo

Comments (5)

lamenezes avatar lamenezes commented on August 17, 2024 1

after some thought I believe my previous proposal wasn't ideal. the class attribute value serves as default value (or function) to the field. for example the timestamp field default value could be datetime(2018, 1, 1) or the function datetime.now.

it doesn't feel right to run the "default function" over the passed value, so I think the best approach to your example is:

from datetime import datetime

from dateutil import parser
from simple_model import Model


class MyModel(Model):
    name: str = ''
    timestamp: datetime
    # default value is already None, no need to set it
    # I believe parser.parse always return a datetime object so it would be nice to define it

    def __post_init__(self, **kwargs):
        self.timestamp = parser.parse(self.timestamp)
        self.clean()  # I'm not the biggest fan of calling clean() on post init. but if you think it's really necessary here is the best place to do it (after the parsing)


data = {'name': 'Name', 'timestamp': '2018-02-05T10:00:01.000001Z'}
model = MyModel(**data)

dict(model)  # no exception is raised here

The case you provided feats the goal of the __post_init__ method. Its objective is to give the possibility of performing further operations on the model and its fields after simple model internal initializations.

IMO this solve the issue you opened, @fvlima. Thanks for your question and if you have any others doubts or suggestions feel free to comment here or open other issues.

from simple-model.

lamenezes avatar lamenezes commented on August 17, 2024

that is the expected behaviour. the cleaning methods are expected to be idempotent as the model maybe cleaned several times. maybe the clean method shouldn't be called implicitly as to avoid that kind of problem, but this avoids other issues such as converting to dict with "dirty" fields.

IMO the clean_timestamp is used in the example to initialize the field timestamp as a DateTime using the dateutil.parser.parse function. Therefore the example doesn't work as expected. I believe this "workaround" was necessary because the model is still lacking.

Upon designing simple model 1.0 I thought something like this may happen, so I considered the option to allow factories on attributes default values as shown on the example below:

from dateutil import parser
from simple_model import Model


class MyModel(Model):
    timestamp = parser.parse


model = MyModel(timestamp='2010-10-10')  # timestamp is still a str
model.clean()  # timestamp becomes the result of parser.parse(model.timestamp)

from simple-model.

fvlima avatar fvlima commented on August 17, 2024

In this example that you gave, what is the better type to associate with timestamp attr? Because if a type isn't declared, an assertion error will be raised.

from simple-model.

lamenezes avatar lamenezes commented on August 17, 2024

In this example that you gave, what is the better type to associate with timestamp attr? Because if a type isn't declared, a assertion error will be raised.

@fvlima that wasn't supposed to happen. I just fixed that behavior on 74e8694

now it's possible to define model with fields without defining types. this makes my previous example valid (except for the factory part that it's still under development)

from simple-model.

fvlima avatar fvlima commented on August 17, 2024

Thank you for this reply @lamenezes

from simple-model.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.