Пример #1
0
 def save(self, path: str) -> None:
     """
     Saves this object instance as YAML to the specified path
     """
     self.validate()
     with open(path, "w") as file:
         related.to_yaml(self,
                         file,
                         suppress_empty_values=True,
                         suppress_map_key_values=True)
Пример #2
0
 def dump(self, path):
     """Dump the object to a yaml file
     """
     generated_yaml = related.to_yaml(
         self, suppress_empty_values=True,
         suppress_map_key_values=True)  # .strip()
     with open(path, "w") as f:
         f.write(generated_yaml)
Пример #3
0
def test_yaml_roundtrip_with_empty_values():
    original_yaml = open(YML_FILE).read().strip()
    yml_dict = from_yaml(original_yaml)
    compose = Compose(**yml_dict)
    generated_yaml = to_yaml(compose,
                             suppress_map_key_values=True,
                             suppress_empty_values=False).strip()
    assert "ports: []" in generated_yaml
Пример #4
0
    def _create_samples(self):
        self.project = Project.sample()
        print(self.project)

        self.document = Document.sample()
        print(self.document)
        print(' - as yaml - ')
        print(related.to_yaml(self.document))
Пример #5
0
def test_document_instantiation():
    # --Given--
    # --When--
    doc = Document(title='My Title')

    # --Then--
    assert doc.title == 'My Title'
    print(to_yaml(doc))
Пример #6
0
 def section(cls, title, obj, **kwargs):
     size = max(20, len(title))
     bar = "=" * size
     # if isinstance(obj, dict) and "html" in obj:
     #     content = str(obj["html"])
     # else:
     #     content = related.to_yaml(obj, **kwargs) if obj else "None"
     content = related.to_yaml(obj, **kwargs) if obj else "None"
     return "\n".join([bar, str.center(title, size), bar, "", content, ""])
Пример #7
0
def related_dump_yaml(obj, path, verbose=False):
    import related
    generated_yaml = related.to_yaml(obj,
                                     suppress_empty_values=False,
                                     suppress_map_key_values=True)  # .strip()
    if verbose:
        print(generated_yaml)

    with open(path, "w") as f:
        f.write(generated_yaml)
Пример #8
0
def test_address_yaml_roundtrip():
    address = Address(street="123 Main Street",
                      city="Springfield",
                      zipcode="12345")
    assert repr(address) == "Address(street='123 Main Street', " \
                            "city='Springfield', zipcode='12345', " \
                            "street_two=None)"

    yaml = to_yaml(address)
    new_address = from_yaml(yaml, Address)
    assert new_address == address
Пример #9
0
    def section(cls, title, obj, **kwargs):
        size = max(20, len(title))
        bar = "=" * size
        if isinstance(obj, str):
            content = obj  # pragma: no cover
        elif obj:
            content = related.to_yaml(obj, **kwargs)
        else:
            content = "None"

        return "\n".join([bar, str.center(title, size), bar, "", content, ""])
Пример #10
0
def test_person_with_education_to_yaml_and_back():
    person = Person(name="Brainy",
                    education=[
                        Education(school="School 2", degree=Degree.MASTERS),
                        Education(school="School 1", degree=Degree.BACHELORS),
                    ])

    yaml = to_yaml(person)
    new_person = from_yaml(yaml, Person)
    assert new_person.education == person.education
    assert new_person == person
Пример #11
0
def test_set_construction():
    scientists = [
        Person(first_name="Grace", last_name="Hopper"),
        Person(first_name="Katherine", last_name="Johnson"),
        Person(first_name="Katherine", last_name="Johnson")
    ]

    assert len(scientists) == 3

    role_models = RoleModels(scientists=scientists)
    assert len(role_models.scientists) == 2

    assert related.to_yaml(role_models).strip() in (SET_ORDER_1, SET_ORDER_2)
Пример #12
0
def test_compose_from_yml():
    original_yaml = open(YML_FILE).read().strip()
    yml_dict = from_yaml(original_yaml)
    compose = to_model(Compose, yml_dict)

    assert compose.version == '2'
    assert compose.services['web'].ports == ["5000:5000"]
    assert compose.services['redis'].image == "redis"

    generated_yaml = to_yaml(compose,
                             suppress_empty_values=True,
                             suppress_map_key_values=True).strip()

    assert original_yaml == generated_yaml
Пример #13
0
def get_question(meta, id, format):
    """
    Dumps a question in the specified format
    """

    question = meta.find_question(id)
    if question is None:
        raise click.ClickException("Question with specified ID was not found")

    text = ''
    if format == 'json':
        text = related.to_json(question)
    else:
        text = related.to_yaml(question)

    click.echo(text)
Пример #14
0
def test_compose_from_yml():
    original_yaml = open(YML_FILE).read().strip()
    yml_dict = from_yaml(original_yaml)
    compose = Compose(**yml_dict)

    # basic checks
    assert compose.version == '2'
    assert compose.services['redis'].image == "redis"

    # compare short form vs. long form
    expected = Port(target="5000", published="5000")
    assert expected == compose.services['web'].ports[0]

    # private attributes suppressed
    generated_yaml = to_yaml(compose,
                             suppress_private_attr=True,
                             suppress_empty_values=True,
                             suppress_map_key_values=True).strip()
    assert "_short_form" not in generated_yaml

    # can we make these equal??
    assert original_yaml == generated_yaml
Пример #15
0
def edit_defaults(meta, answers, correct_answers, points, grader, likelihood, is_multiple_choice):
    """
    Modifies problem set defaults for unspecified properties when creating new questions
    """
    qd = meta.question_defaults
    if answers:
        qd.num_answers = answers
    if correct_answers:
        qd.num_correct_answers = answers
    if points:
        qd.points = points
    if grader:
        qd.grader = grader
    if likelihood:
        qd.likelihood = likelihood
    if is_multiple_choice:
        qd.is_multiple_choice = is_multiple_choice

    save_meta(meta)

    click.echo("New defaults -")
    click.echo(related.to_yaml(qd))
Пример #16
0
 def save(self, path: str):
     with open(path, "w") as file:
         related.to_yaml(self, file)
Пример #17
0
import related


@related.immutable
class Person(object):
    first_name = related.StringField()
    second_name = related.StringField()


@related.immutable
class RoleModels(object):
    scientists = related.SetField(Person)


people = [Person("f", "l"), Person('n', 'z')]
print(related.to_yaml(RoleModels(scientists=people)))
""" docker compose sample
version: '2'
services:
  web:
    build: .
    ports:
    - 5000:5000
    volumes:
    - .:/code
  redis:
    image: redis
"""


@related.immutable
Пример #18
0
def test_roundtrip_yaml(company):
    new_company = from_yaml(to_yaml(company), Company)
    assert new_company == company
Пример #19
0
 def get_config_as_yaml(self):
     generated_yaml = related.to_yaml(self,
                                      suppress_empty_values=True,
                                      suppress_map_key_values=True)
     return generated_yaml
Пример #20
0
def test_yaml(company):
    company_yaml = to_yaml(to_dict(company))
    assert ("uuid: %s" % company.uuid) in company_yaml
    assert ("url: %s" % company.url.geturl()) in company_yaml