Comments (5)
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.
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.
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.
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.
Thank you for this reply @lamenezes
from simple-model.
Related Issues (20)
- Move docs to sphinx
- Handle multiple model clean HOT 1
- Change build_many to return a generator instead list HOT 4
- Clean doesn't work without override HOT 1
- Add support to properties HOT 1
- AttributeError when build child models with model_many_build and try to access parent methods HOT 2
- It's not possible to create subclasses of model without fields HOT 1
- Support fields with type typing.Optional
- Support fields with type typing.Union
- Improve docs HOT 2
- Use typing.cast when converting to type HOT 1
- Add RelatedField HOT 4
- Use re.compile to improve performance
- Error to install on Python 3.5.x HOT 1
- Fix model.as_dict when model has enum attribute HOT 1
- Rename model and field module to models and fields respectively HOT 2
- Allow model creation from list
- Add Table Of Contents (TOC) to README.rst
- Write docs for some features
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from simple-model.