-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from jacebrowning/document-formats
Document format options
- Loading branch information
Showing
9 changed files
with
258 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# Synchronization | ||
|
||
The simplest way to turn a dataclass into a datafile is to replace the `@dataclass` decorator with `@datafile`: | ||
|
||
```python | ||
# BEFORE | ||
|
||
from dataclasses import dataclass | ||
|
||
@dataclass | ||
class Item: | ||
name: str | ||
count: int | ||
available: bool | ||
``` | ||
|
||
```python | ||
# AFTER | ||
|
||
from datafiles import datafile | ||
|
||
@datafile('items/{self.name}.yml') | ||
class Item: | ||
name: str | ||
count: int | ||
available: bool | ||
``` | ||
|
||
But you can also decorate an existing dataclass: | ||
|
||
```python | ||
# BEFORE | ||
|
||
from dataclasses import dataclass | ||
|
||
@dataclass | ||
class Item: | ||
name: str | ||
count: int | ||
available: bool | ||
``` | ||
|
||
```python | ||
# AFTER | ||
|
||
from dataclasses import dataclass | ||
|
||
from datafiles import datafile | ||
|
||
@datafile('items/{self.name}.yml') | ||
@dataclass | ||
class Item: | ||
name: str | ||
count: int | ||
available: bool | ||
``` | ||
|
||
# Options | ||
|
||
The following options can be passed to `@datafile()` decorator: | ||
|
||
| Name | Type | Description | Default | ||
| --- | --- | --- | --- | | ||
| `attrs` | `dict` | Attributes to synchronize mapped to `datafile.converters` classes for serialization. | _Inferred from type annotations._ | | ||
| `manual` | `bool` | Synchronize object and file changes manually. | `False` | | ||
| `defaults` | `bool` | Include default values in files. | `False` | | ||
|
||
For example: | ||
|
||
```python | ||
from datafiles import datafile | ||
|
||
@datafile('items/{self.name}.yml', manual=True, defaults=True) | ||
class Item: | ||
name: str | ||
count: int | ||
available: bool | ||
``` | ||
|
||
# Meta class | ||
|
||
Alternatively, any of the above options can be configured through code by setting `datafile_<option>` in a `Meta` class: | ||
|
||
```python | ||
from datafiles import datafile, converters | ||
|
||
@datafile('items/{self.name}.yml') | ||
class Item: | ||
name: str | ||
count: int | ||
available: bool | ||
|
||
class Meta: | ||
datafile_attrs = {'count': converters.Integer} | ||
datafile_manual = True | ||
datafile_defaults = True | ||
|
||
``` | ||
|
||
# Base class | ||
|
||
Finally, a datafile can explicitly extend `datafiles.Model` to set all options in the `Meta` class: | ||
|
||
```python | ||
from dataclasses import dataclass | ||
|
||
from datafiles import Model, converters | ||
|
||
@dataclass | ||
class Item(Model): | ||
name: str | ||
count: int | ||
available: bool | ||
|
||
class Meta: | ||
datafile_pattern = 'items/{self.name}.yml' | ||
datafile_attrs = {'count': converters.Integer} | ||
datafile_manual = True | ||
datafile_defaults = True | ||
|
||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# YAML | ||
|
||
By default, datafiles uses the [YAML language](https://yaml.org/) for serialization. Any of the following file extensions will use this format: | ||
|
||
- `.yml` | ||
- `.yaml` | ||
- (no extension) | ||
|
||
Sample output: | ||
|
||
```yaml | ||
my_dict: | ||
value: 0 | ||
my_list: | ||
- value: 1 | ||
- value: 2 | ||
my_bool: true | ||
my_float: 1.23 | ||
my_int: 42 | ||
my_str: Hello, world! | ||
``` | ||
|
||
Where possible, comments and whitespace are preserved in files as shown in this [Jupyter Notebook](https://github.com/jacebrowning/datafiles/blob/develop/notebooks/roundtrip_comments.ipynb). | ||
|
||
# JSON | ||
|
||
The [JSON language](https://www.json.org/) is also supported. Any of the following file extensions will use this format: | ||
|
||
- `.json` | ||
|
||
Sample output: | ||
|
||
```json | ||
{ | ||
"my_dict": { | ||
"value": 0 | ||
}, | ||
"my_list": [ | ||
{ | ||
"value": 1 | ||
}, | ||
{ | ||
"value": 2 | ||
} | ||
], | ||
"my_bool": true, | ||
"my_float": 1.23, | ||
"my_int": 42, | ||
"my_str": "Hello, world!" | ||
} | ||
``` | ||
|
||
Additional examples can be found in this [Jupyter Notebook](https://github.com/jacebrowning/datafiles/blob/develop/notebooks/format_options.ipynb). | ||
|
||
# TOML | ||
|
||
The [TOML language](https://github.com/toml-lang/toml) is also supported. Any of the following file extensions will use this format: | ||
|
||
- `.toml` | ||
|
||
Sample output: | ||
|
||
```toml | ||
my_bool = true | ||
my_float = 1.23 | ||
my_int = 42 | ||
my_str = "Hello, world!" | ||
|
||
[[my_list]] | ||
value = 1 | ||
|
||
[[my_list]] | ||
value = 2 | ||
|
||
[my_dict] | ||
value = 0 | ||
``` | ||
|
||
Additional examples can be found in this [Jupyter Notebook](https://github.com/jacebrowning/datafiles/blob/develop/notebooks/format_options.ipynb). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters