How to use the datafiles.datafile function in datafiles

To help you get started, we’ve selected a few datafiles examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jacebrowning / datafiles / tests / test_orm_usage.py View on Github external
def test_comments_in_matched_files(expect):
    @datafile("../tmp/templates/{self.key}/config.yml")
    class LegacyTemplate:
        key: str
        name: str
        link: str
        default: List[str]
        aliases: List[str]

    write(
        'tmp/templates/foo/config.yml',
        """
        link: # placeholder
        default:
          - # placeholder
          - # placeholder
        aliases:
          - # placeholder
github jacebrowning / datafiles / tests / test_custom_converters_future.py View on Github external
def test_extension(expect):
    @datafile("../tmp/sample.yml")
    class MyObject:
        value: MyDateTime

    x = MyObject(MyDateTime(2019, 1, 29))
    expect(x.datafile.text) == "value: '2019-01-29T00:00:00'\n"
github jacebrowning / datafiles / tests / samples.py View on Github external
datafile_attrs = {'included': String}


@datafile('../tmp/sample.yml', manual=True)
class SampleWithDefaults:
    without_default: str
    with_default: str = 'foo'


@dataclass
class _NestedSample1:
    name: str
    score: float


@datafile('../tmp/sample.yml', manual=True)
class SampleWithNesting:
    name: str
    score: float
    nested: _NestedSample1


@dataclass
class _NestedSample2:
    name: str = 'b'
    score: float = 3.4


@datafile('../tmp/sample.yml', manual=True)
class SampleWithNestingAndDefaults:
    name: str
    score: float = 1.2
github jacebrowning / datafiles / tests / test_setup.py View on Github external
def sample():
        @datafile('../tmp/{self.key}.yml')
        class Sample:
            key: int
            name: str
            score: float = 1 / 2

        return Sample(1, "a")
github jacebrowning / datafiles / tests / test_custom_converters.py View on Github external
def test_registration(expect):

    converters.register(datetime, DateTimeConverter)

    @datafile("../tmp/sample.yml")
    class Timestamp:
        dt: datetime

    ts = Timestamp(datetime(2019, 1, 29))
    expect(ts.datafile.text) == "dt: '2019-01-29T00:00:00'\n"

    ts = Timestamp(Missing)
    expect(ts.dt) == datetime(2019, 1, 29)

    ts.datafile.text = "dt: '2019-01-22T00:00:00'\n"
    expect(ts.dt.day) == 22
github jacebrowning / datafiles / tests / test_automatic_attributes.py View on Github external
# pylint: disable=unused-variable


import pytest

from datafiles import datafile
from datafiles.utils import logbreak


@datafile('../tmp/{self.name}.yml', auto_attr=True, auto_save=False)
class Sample:
    name: str


def describe_auto_attr():
    @pytest.mark.flaky
    def with_builtin(expect):
        sample = Sample('abc')

        sample.datafile.text = "count: 1"

        logbreak("Getting attribute")
        expect(sample.count) == 1

        logbreak("Setting attribute")
        sample.count = 4.2
github jacebrowning / datafiles / tests / test_instantiation.py View on Github external
def describe_nonexisting_file():
    @datafile('../tmp/sample.yml')
    class SampleAutomatic:
        pass

    SampleManual = SampleWithDefaults

    def it_is_created_automatically_by_default(expect):
        sample = SampleAutomatic()

        expect(sample.datafile.exists) == True

    def it_is_not_created_automatically_when_manual(expect):
        sample = SampleManual()

        expect(sample.datafile.exists) == False
github jacebrowning / datafiles / tests / test_extended_converters.py View on Github external
"""Tests for extended converters."""

# pylint: disable=unused-variable


import pytest

from datafiles import datafile
from datafiles.converters import Number, Text


@datafile('../tmp/sample.yml')
class Sample:
    number: Number = Number(0)
    text: Text = Text("")


def describe_number():
    @pytest.fixture
    def sample():
        return Sample()

    def with_float_to_integer(sample, expect, read, dedent):
        sample.number = 1.23

        expect(read('tmp/sample.yml')) == dedent(
            """
            number: 1.23
github jacebrowning / datafiles / tests / test_setup.py View on Github external
def sample():
        @datafile('/private/tmp/{self.key}.yml')
        class Sample:
            key: int
            name: str
            score: float = 1 / 2

        return Sample(5, "a")
github jacebrowning / datafiles / tests / test_patched_methods.py View on Github external
@datafile('../tmp/sample.yml')
class Sample:
    item: str = 'a'
    items: List[int] = field(default_factory=lambda: [1])
    data: Dict[str, int] = field(default_factory=lambda: {'a': 1})


@dataclass
class Nested:
    name: str = 'b'
    score: float = 3.4
    items: List[int] = field(default_factory=list)


@datafile('../tmp/sample.yml')
class SampleWithNesting:
    item: int
    nested: Nested = field(default_factory=Nested)


def describe_automatic_load():
    @pytest.mark.flaky
    def with_getattribute(expect):
        sample = Sample()

        write(
            'tmp/sample.yml',
            """
            item: b
            """,
        )