Skip to main content

Pydifact is a library that aims to provide complete support for reading and writing EDIFACT files. These file format, despite being old, is still a standard in many business cases. In Austria e.g., it is used for the transfer of medical reports between medical doctors.

Project description

pydifact

A Python library to parse and serialize UN/EDIFACT interchanges.

Preamble

This is a port of metroplex-systems/edifact to Python. Thanks here at the start to Craig Duncan for this cool piece of software. Porting was like a breeze due to the high code quality there. All credits for the initial code here go to him, I just did the translation to Python(3), some "pythonifications" of the code and little improvements.

Why another EDIFACT library?

Because I did not find a decent UN/EDIFACT library for Python, so I decided to port one of the available good PHP libraries to Python. Here is the result.

ATM this is a Work In Progress, the API is not stable yet. Feel free to help.

Install

As usual, use a virtualenv, and install via pip or pipenv:

pip install pydifact

However, it is not stable yet, so the pypi version, including documentation and code examples, could differ from the latest git code. If in doubt, use the git version:

git clone https://github.com/nerdocs/pydifact.git
cd pydifact
pip install -e .

Usage

To read a full Interchange from a file or string, take the Interchange class and iter over the messages and segments:

from pydifact.segmentcollection import Interchange

interchange = Interchange.from_file("./tests/data/wikipedia.edi")
interchange = Interchange.from_str(
    "UNA:+,? '"
    "UNB+UNOC:1+1234+3333+200102:2212+42'"
    "UNH+42z42+PAORES:93:1:IA'"
    "MSG+1:45'"
    "IFT+3+XYZCOMPANY AVAILABILITY'"
    "ERC+A7V:1:AMD'"
    "UNT+5+42z42'UNZ+2+42'"
)
for message in interchange.get_messages():
    for segment in message.segments:
        print(f"Segment tag: {segment.tag}, content: {segment.elements}")

You may also want to iterate directly on segments :

from pydifact.segmentcollection import Interchange

interchange = Interchange.from_str(
    "UNA:+,? '"
    "UNB+UNOC:1+1234+3333+200102:2212+42'"
    "UNH+42z42+PAORES:93:1:IA'"
    "MSG+1:45'"
    "IFT+3+XYZCOMPANY AVAILABILITY'"
    "ERC+A7V:1:AMD'"
    "UNT+5+42z42'UNZ+2+42'"
)

for segment in interchange.segments:
    print(f"Segment tag: {segment.tags}, content: {segment.elements}")

Or you can create an EDI interchange on the fly:

from pydifact.segmentcollection import Interchange
from pydifact.segments import Segment

interchange = Interchange(syntax_identifier=("IBMA",1),
                          sender="MeMyselfAndIrene",
                          recipient="TheOtherOne",
                          control_reference="KLuzs7c6")
interchange.add_segment(Segment("QTY", ["12", "3"]))

print(interchange.serialize())

To include or override the Service String Advice segment (UNA), just specify it as a regular segment:

interchange.add_segment(Segment("UNA", ":+.? '"))

You may also want to parse a « raw » segment bunch which is not an interchange:

from pydifact.segmentcollection import RawSegmentCollection

collection = RawSegmentCollection.from_str("UNH+1+ORDERS:D:96A:UN:EAN008'")

for segment in collection.segments:
    print(f"Segment tag: {segment.tags}, content: {segment.elements}")

Limitations

  • No support of optional functional groups (UNGUNE),

Alternatives

In python ecosystem:

  • python-edifact - simpler, IMHO less clean code, less flexible. may be faster though (not tested). Seems unmaintained.
  • bots - huge, with webinterface (bots-monitor), webserver, bots-engine.
  • edicat - simple, only for separating lines/segments for CLI-piping.

Development

Setup

To develop pydifact, clone the repository and install the dev requirements:

make dev
# or
# pip install -e .[dev]

This installs all the python packages needed for development and testing.

Code formatting

Format all python files using black before committing.

Happy coding, PR are more than welcome to make this library better, or to add a feature that matches your needs. Nevertheless, don't forget adding tests for every aspect you add in code.

Testing

pydifact uses pytest for testing. There is a shortcut in the Makefile for your convenience:

make test

This is recommended for faster testing.

There are some additional tests to check the performance of parsing huge files - you can include that tests by calling

make test-extended

Credits

The official formats for UN/EDIFAT provided by UN Secretariat are raw text files located here and html (.htm) files located here, similarly for service codes as text and html files located at https://www.gefeg.com/jswg/.

There are many other helpful projects and people who made this possible:

For a contributors' list, see Contributors.

License

This library is licensed under the MIT license, see the LICENSE file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydifact-0.2.1.tar.gz (43.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydifact-0.2.1-py3-none-any.whl (42.5 kB view details)

Uploaded Python 3

File details

Details for the file pydifact-0.2.1.tar.gz.

File metadata

  • Download URL: pydifact-0.2.1.tar.gz
  • Upload date:
  • Size: 43.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"KDE neon","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pydifact-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7e17cfff9f0fa52235bf958fe17a133931b09d2c10add8797392b861743d301b
MD5 d6a3c7b3da6028036680b1c0985ad686
BLAKE2b-256 7733d1336a1c106ce34a5d8e4256055e9005be1bbcaebe4e69a71538ebe13987

See more details on using hashes here.

File details

Details for the file pydifact-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: pydifact-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 42.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"KDE neon","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pydifact-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c9005125afdc4da6f74274c8d4d023749ee9e468f99dc100170611af4ba7471b
MD5 875b9b2dd2279c9b9e94ca3e6274a27f
BLAKE2b-256 ffbc360f3f895b106eb152ff0fcbfdd50769222ccd496da9b2082ba93dfe3981

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page