-
Notifications
You must be signed in to change notification settings - Fork 7
Open
Description
First let me say - great library! :) I've been evaluating it as well as attrs for a while now.
I'd like to suggest a possible feature extension: allow callable defaults, which will let you do things like this:
from datetime import datetime
import fields
class Record(fields.Tuple.Username[str].Created[datetime.now].Revision[long].Active[bool]):
passUsing types as defaults IMO makes the code more readable. Also assuming the callable takes no arguments, it allows simple initialization at run-time (like with datetime.now).
It's easy to do that with a simple sealer wrapper, like the one below:
from functools import wraps
from fields import factory, tuple_sealer
def with_callable_defaults(sealer):
"""
Wraps the given `sealer` to support `callable` for any specified default.
"""
@wraps(sealer)
def wrapped_sealer(fields, defaults):
for field, default in defaults.iteritems():
if callable(default):
try:
defaults[field] = default()
except TypeError:
pass
return sealer(fields, defaults)
return wrapped_sealer
Tuple = factory(with_callable_defaults(tuple_sealer))What do you think?
Metadata
Metadata
Assignees
Labels
No labels