Skip to content

Commit 6f09f96

Browse files
author
Sylvain MARIE
committed
Updated usage document
1 parent 0ada168 commit 6f09f96

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

docs/usage.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Usage details
22

3-
## @autoargs
3+
## 1. Fields
4+
5+
If you use another library to define your class fields (`pyfields` is the only one supported as of 2.1.0), you can skip this section and jump directly to [section 2.](#2-facets). Otherwise, these two decorators can be useful to do something equivalent.
6+
7+
### @autoargs
48

59
Automatically affects the contents of a function to self. Initial code and test examples from [this answer from utnubu](http://stackoverflow.com/questions/3652851/what-is-the-best-way-to-do-automatic-attribute-assignment-in-python-and-is-it-a#answer-3653049).
610

@@ -107,7 +111,7 @@ print(a.verbose) # raises AttributeError
107111
Finally note that `@autoargs` is automatically applied when you decorate the whole class with `@autoclass`, see below.
108112

109113

110-
## @autoprops
114+
### @autoprops
111115

112116
Automatically generates all properties getters and setters from the class constructor.
113117

@@ -260,13 +264,20 @@ t.b = ['r',''] # raises ContractNotRespected
260264
Finally note that `@autoprops` is automatically applied when you decorate the whole class with `@autoclass`, see below.
261265

262266

263-
## @autodict
267+
## 2. Facets
268+
269+
These facets all support two ways of defining the fields:
270+
271+
- from `pyfields`
272+
- or from the constructor signature (just like `@autoprops` does)
273+
274+
### @autodict
264275

265276
Automatically generates a read-only dictionary view on top of the object. It does several things:
266277

267278
* it adds `collections.Mapping` to the list of parent classes (i.e. to the class' `__bases__`)
268279
* it generates `__len__`, `__iter__` and `__getitem__` in order for the appropriate fields to be exposed in the dict view. Parameters allow to customize the list of fields that will be visible. Note that any methods with the same name will be overridden.
269-
* if `only_constructor_args` is `True` (default), it generates a static `from_dict` method in the class corresponding to a call to the constructor with the unfolded dict. Note that this method may be overridden by the user.
280+
* if `only_known_fields` is `True` (default), it generates a static `from_dict` method in the class corresponding to a call to the constructor with the unfolded dict. Note that this method may be overridden by the user.
270281
* if `__eq__` is not implemented on this class, it generates a version that handles the case `self == other` where other is of the same type. In that case the dictionary equality is used. Other equality tests remain unchanged.
271282
* if `__str__` and `__repr__` are not implemented on this class, it generates them too.
272283

@@ -330,7 +341,7 @@ assert o == {'a': 1, 'b': 'r'}
330341
* You can decide to open to all object fields, including or excluding (default) the fields that are not arguments of the constructor, and including or excluding (default) the class-private ones. Note that class-private attributes will be visible with their usual scrambled name:
331342

332343
```python
333-
@autodict(only_constructor_args=False, only_public_fields=False)
344+
@autodict(only_known_fields=False, only_public_fields=False)
334345
class D(object):
335346
@autoargs
336347
def __init__(self, a: str, b: List[str]):
@@ -362,7 +373,7 @@ class Bar(object):
362373
Finally note that `@autodict` is automatically applied when you decorate the whole class with `@autoclass`, see below.
363374

364375

365-
## @autohash
376+
### @autohash
366377

367378
A decorator to makes objects of the class implement __hash__, so that they can be used correctly for example in sets. Parameters allow to customize the list of attributes that are taken into account in the hash.
368379

@@ -398,7 +409,7 @@ assert hash(o) != hash(p)
398409
```python
399410
from random import random
400411

401-
@autohash(only_constructor_args=True, only_public_fields=True)
412+
@autohash(only_known_fields=True, only_public_fields=True)
402413
class D(object):
403414
@autoargs
404415
def __init__(self, a: str, _b: str):
@@ -428,8 +439,15 @@ class Bar(object):
428439

429440
Finally note that `@autohash` is automatically applied when you decorate the whole class with `@autoclass`, see below.
430441

442+
### @autorepr
443+
444+
This decorator is useful if you wish to add a string representation to your class but you do not wish to use the entire `@autodict`. It just creates the `__str__` and `__repr__` methods.
445+
446+
### @autoeq
447+
448+
This decorator is useful if you wish to add an equality method to your class but you do not wish to use the entire `@autodict`. It just creates the `__eq__` method.
431449

432-
## @autoslots
450+
### @autoslots
433451

434452
Automatically create slots for each attribute. Parameters allow to customize the list of attributes that are taken into account.
435453

@@ -485,7 +503,7 @@ class Bar(object):
485503
Finally note that `@autoslots` is not automatically applied when you decorate the whole class with `@autoclass`, you have to use `@autoclass(autoslots=True)` see below.
486504

487505

488-
## @autoclass
506+
### @autoclass
489507

490508
Applies all or part of the above decorators at once. Useful if you want to make the most from this library.
491509

@@ -537,16 +555,18 @@ class PartsOfTheAbove:
537555

538556
# instance creation
539557
o = PartsOfTheAbove(a=2, b=True)
558+
print(o) # works: autorepr is automatically enabled when autodict=False
540559

541560
assert o == {'a': 2, 'b': True, 'c': None} # AssertionError
561+
assert o == PartsOfTheAbove(a=2, b=True) # works: autoeq is automatically enabled
542562
assert PartsOfTheAbove.from_dict(o) == o # AttributeError: 'PartsOfTheAbove' has no attribute 'from_dict'
543563
assert dict(**o) == o # TypeError: argument after ** must be a mapping
544564
```
545565

546566

547567
## Alternative to decorators: manual function wrappers
548568

549-
Equivalent manual wrapper methods are provided for all decorators in this library: `autoargs_decorate(init_func, include, exclude)`, `autoprops_decorate(cls, include, exclude)`, `autoprops_override_decorate(func, attribute, is_getter)`, `autodict_decorate(cls, include, exclude, only_constructor_args, only_public_fields)`, `autoclass_decorate(cls, include, exclude, autoargs, autoprops, autodict)`, `autoslots_decorate(cls, include, exclude, use_public_names, add_weakref_slot)`
569+
Equivalent manual wrapper methods are provided for all decorators in this library: `autoargs_decorate(init_func, include, exclude)`, `autoprops_decorate(cls, include, exclude)`, `autoprops_override_decorate(func, attribute, is_getter)`, `autodict_decorate(cls, include, exclude, only_known_fields, only_public_fields)`, `autoclass_decorate(cls, include, exclude, autoargs, autoprops, autodict)`, `autoslots_decorate(cls, include, exclude, use_public_names, add_weakref_slot)`
550570

551571
Therefore you can do:
552572

0 commit comments

Comments
 (0)