{"id":9417,"library":"yorm","title":"YORM (YAML Object-Relational Mapper)","description":"YORM (YAML Object-Relational Mapper) is a Python library that provides automatic object-YAML mapping, allowing Python objects to be seamlessly persisted to and loaded from YAML files. It enables defining models with attributes that are automatically synchronized with a specified YAML file, supporting features like autosaving and schema validation. The current version is 1.6.2, and it has an active but irregular release cadence.","status":"active","version":"1.6.2","language":"en","source_language":"en","source_url":"https://github.com/jacebrowning/yorm","tags":["YAML","ORM","persistence","serialization","data-storage"],"install":[{"cmd":"pip install yorm","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"Base class for YORM models.","symbol":"Model","correct":"import yorm\nclass MyModel(yorm.Model):"},{"note":"Decorator for classes and attributes. Use `@yorm.attr(autosave=True)` for class-level autosaving or `my_attr = yorm.attr(type=str)` for attribute definition.","symbol":"attr","correct":"import yorm\n@yorm.attr(autosave=True)\nclass MyModel(yorm.Model):"},{"note":"As of v1.6, `yorm.sync()` requires a path argument. Implicit paths are deprecated.","wrong":"yorm.sync(MyModel)","symbol":"sync","correct":"import yorm\nyorm.sync(MyModel, 'path/to/data.yaml')"}],"quickstart":{"code":"import yorm\nimport os\nimport tempfile\nimport shutil\n\n# Create a temporary directory for the YAML file\ntemp_dir = tempfile.mkdtemp()\n\ntry:\n    # Define a model with autosave enabled at the class level\n    @yorm.attr(autosave=True)\n    class Person(yorm.Model):\n        name = yorm.attr(type=str)\n        email = yorm.attr(type=str, default='')\n\n    # Synchronize the model with a YAML file in the temporary directory\n    file_path = os.path.join(temp_dir, 'person.yaml')\n    yorm.sync(Person, file_path)\n    print(f\"Synchronized Person model with: {file_path}\")\n\n    # Create a new person object. Due to autosave=True, it's saved automatically.\n    print(\"\\nCreating Bob...\")\n    bob = Person(name=\"Bob\", email=\"bob@example.com\")\n    print(f\"Bob created: {bob.name} ({bob.email})\")\n\n    # Update an attribute. The change is saved automatically.\n    print(\"Updating Bob's email...\")\n    bob.email = \"robert@example.com\"\n    print(f\"Bob's email updated to: {bob.email}\")\n\n    # Fetch all persons (should be just Bob)\n    print(\"\\nFetching all persons...\")\n    persons = Person.all()\n    for p in persons:\n        print(f\"Found: {p.name} ({p.email})\")\n\n    # Verify the update by reloading and checking\n    if persons and persons[0].email == \"robert@example.com\":\n        print(\"Update verified: Bob's email is now robert@example.com\")\n    else:\n        print(\"Error: Bob's email not updated as expected.\")\n\n    # Delete all persons. This also gets persisted.\n    print(\"\\nDeleting all persons...\")\n    Person.all().clear()\n    print(f\"Persons remaining after clear: {len(Person.all())}\")\n\nfinally:\n    # Clean up the temporary directory\n    shutil.rmtree(temp_dir)\n    print(f\"\\nCleaned up temporary directory: {temp_dir}\")","lang":"python","description":"This quickstart demonstrates how to define a YORM model, synchronize it with a temporary YAML file, create and update objects, and verify persistence. It uses `autosave=True` for automatic saving of changes."},"warnings":[{"fix":"Always provide a `path` argument to `yorm.sync()`, e.g., `yorm.sync(Person, 'data/person.yaml')`.","message":"Calling `yorm.sync()` without a `path` argument is deprecated and will raise a `DeprecationWarning`.","severity":"deprecated","affected_versions":">=1.6"},{"fix":"Ensure `autosave=True` in the `@yorm.attr` decorator for the class, or explicitly call `your_object.save()` after making changes to persist them.","message":"If `autosave=False` is set (or omitted) on a model, changes to objects will not be automatically persisted to the YAML file. Data loss can occur if `model.save()` is not explicitly called.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Avoid manual editing of YORM-managed YAML files. If manual edits are necessary, ensure strict YAML syntax and schema adherence. In case of errors, inspect the YAML for malformed entries or incorrect data types.","message":"Manually editing the generated YAML file in an incorrect format can lead to `ruamel.yaml` parsing errors or `yorm.exceptions.InvalidObjectError` when the data is loaded.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure all attributes you intend to use with the model are defined using `yorm.attr`, for example: `some_field = yorm.attr(type=str)`.","cause":"An attribute was accessed or assigned on a YORM model object that was not explicitly defined using `yorm.attr()` in the class definition.","error":"AttributeError: 'MyModel' object has no attribute 'some_field'"},{"fix":"Ensure that the parent directories for the YAML file path provided to `yorm.sync()` exist. YORM will create the file itself, but not intermediate directories. You can create them with `os.makedirs(os.path.dirname(file_path), exist_ok=True)`.","cause":"YORM attempted to load or create a YAML file at a path where the parent directories do not exist.","error":"FileNotFoundError: [Errno 2] No such file or directory: '/path/to/nonexistent/file.yaml'"},{"fix":"Ensure that data in the YAML file conforms to the expected types defined in your YORM model, or that values assigned to attributes during runtime match their specified types.","cause":"Data loaded from the YAML file or assigned to an attribute does not match the `type` specified in its `yorm.attr()` definition.","error":"yorm.exceptions.InvalidObjectError: Expected value of type <class 'str'>, got 123"}]}