{"id":8072,"library":"dawg2-python","title":"DAWG2 Python Library","description":"DAWG2-Python is a pure-Python library providing read-only access to Directed Acyclic Word Graphs (DAWGs) or Deterministic Acyclic Finite State Automata (DAFSAs). It specifically works with `.dawg` files created by the `dawgdic` C++ library or the original `DAWG` Python extension. The current version is 0.9.0, with infrequent releases typically for minor updates or dependency adjustments, focusing on API and binary compatibility with the original `DAWG` library where possible.","status":"active","version":"0.9.0","language":"en","source_language":"en","source_url":"https://github.com/pymorphy2-fork/DAWG-Python/","tags":["DAWG","DAFSA","data structure","dictionary","graph","compression","pure-python","read-only"],"install":[{"cmd":"pip install dawg2-python","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Requires Python 3.8 or newer, but less than 4.0.","package":"python","optional":false},{"reason":"While dawg2-python itself is pure-Python for reading, the .dawg files it consumes are typically created by the 'dawg' (C-extension) library or dawgdic C++ library.","package":"dawg","optional":true}],"imports":[{"symbol":"DAWG","correct":"from dawg_python import DAWG"},{"symbol":"BytesDAWG","correct":"from dawg_python import BytesDAWG"},{"symbol":"RecordDAWG","correct":"from dawg_python import RecordDAWG"}],"quickstart":{"code":"import os\nimport tempfile\nimport dawg # This is the *original* DAWG library, for creating the .dawg file\nimport dawg_python # This is dawg2-python, for reading the .dawg file\n\n# Create a dummy .dawg file using the original 'dawg' library\n# (dawg2-python is for reading, not creating/saving)\ndef create_dummy_dawg(filepath):\n    words = ['apple', 'apricot', 'banana', 'bandana', 'cat']\n    d = dawg.DAWG(words)\n    d.save(filepath)\n\n# Use a temporary file for demonstration\nwith tempfile.NamedTemporaryFile(suffix='.dawg', delete=False) as tmp_file:\n    dawg_filepath = tmp_file.name\n\ntry:\n    # 1. Create a DAWG file (requires the 'dawg' library installed)\n    print(f\"Creating a temporary DAWG file at {dawg_filepath}...\")\n    create_dummy_dawg(dawg_filepath)\n    print(\"DAWG file created.\")\n\n    # 2. Load and query the DAWG file using dawg2-python\n    print(\"Loading DAWG using dawg2-python...\")\n    reader = dawg_python.DAWG().load(dawg_filepath)\n    print(\"DAWG loaded successfully.\")\n\n    # Querying words\n    print(f\"'apple' in DAWG: {'apple' in reader}\")\n    print(f\"'banana' in DAWG: {'banana' in reader}\")\n    print(f\"'orange' in DAWG: {'orange' in reader}\")\n\n    # Getting prefixes\n    print(f\"Prefixes for 'bandana': {list(reader.prefixes('bandana'))}\")\n\n    # Iterating through all words\n    print(\"First 5 words:\")\n    for i, word in enumerate(reader.iterkeys()):\n        if i >= 5: break\n        print(f\"- {word}\")\n\nfinally:\n    # Clean up the temporary file\n    if os.path.exists(dawg_filepath):\n        os.remove(dawg_filepath)\n        print(f\"Cleaned up temporary file: {dawg_filepath}\")\n","lang":"python","description":"This quickstart demonstrates how to load and query a `.dawg` file using `dawg2-python`. Note that `dawg2-python` is a pure-Python *reader* and cannot create or save `.dawg` files. For creating the `.dawg` file in this example, the `dawg` (C-extension based) library is used. You would typically use existing `.dawg` files generated by `dawg` or `dawgdic`."},"warnings":[{"fix":"Use the original `dawg` library (which has C extensions) or `dawgdic` C++ tools to create and save `.dawg` files. Then, use `dawg2-python` for read-only access.","message":"DAWG2-Python is a pure-Python library for *reading* existing DAWG files. It does not support creating, modifying, or saving DAWG structures. Attempting to use methods like `save()` or passing data to the constructor will result in errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your Python environment is running Python 3.8 or a newer compatible version (less than 4.0).","message":"Starting from version 0.8.0, the minimal Python version required has been updated to 3.8.","severity":"breaking","affected_versions":">=0.8.0"},{"fix":"No direct action required for end-users installing via `pip`. Developers should be aware of the shift to `poetry` for project management.","message":"The `setup.py` build system was migrated to `poetry` in version 0.8.0. This primarily affects maintainers and those building from source, but it indicates a change in development tooling.","severity":"gotcha","affected_versions":">=0.8.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Use the original `dawg` library (e.g., `import dawg; d = dawg.DAWG(words); d.save('file.dawg')`) to create and save DAWG files. `dawg_python` is only for loading and querying.","cause":"Attempting to save a DAWG created or loaded with `dawg_python` (dawg2-python). This library is read-only.","error":"AttributeError: 'DAWG' object has no attribute 'save'"},{"fix":"Initialize the `DAWG` object without arguments (e.g., `d = dawg_python.DAWG()`) and then call `.load('filepath.dawg')` to load an existing DAWG file. Creating DAWGs from data requires the original `dawg` library.","cause":"Attempting to initialize `dawg_python.DAWG` with a list of words or other data. The constructor is intended for internal use or loading, not direct data input for building a DAWG.","error":"TypeError: DAWG() takes no arguments"},{"fix":"If you intend to *create* `.dawg` files, you must install the `dawg` library (note the missing `_python`): `pip install dawg`. If you only intend to *read* existing `.dawg` files, ensure you are importing `dawg_python` correctly and loading an already saved file.","cause":"This error likely occurs if you are following documentation that refers to the *original* `dawg` library (the C-extension version) for creating `.dawg` files, but you have only installed `dawg2-python`.","error":"ModuleNotFoundError: No module named 'dawg'"}]}