Skip to main content

A Python EDI file parser.

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/order.edi")
interchange = Interchange.from_str(
  "UNA:+,? 'UNB+UNOC:1+1234+3333+200102:2212+42'UNH+42z42+PAORES:93:1:IA'UNT+2+42z42'UNZ+2+42'"
)
for message in interchange.get_messages():
    for segment in message.segments:
        print('Segment tag: {}, content: {}'.format(
            segment.tag, segment.elements))

You may also want to iterate directly on segments :

from pydifact.segmentcollection import Interchange
interchange = Interchange.from_file("./tests/data/order.edi")
interchange = Interchange.from_str("UNA:+,? 'UNH+1+ORDERS:D:96A:UN:EAN008'")

for segment in interchange.segments:
    print('Segment tag: {}, content: {}'.format(
        segment.tag, segment.elements))

Or you can create an EDI interchange on the fly:

from pydifact.segmentcollection import Interchange
from pydifact.segments import Segment
interchange = Interchange()
interchange.add_segment(Segment('QTY', ['12', '3']))
print(interchange.serialize())

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('Segment tag: {}, content: {}'.format(
        segment.tag, segment.elements))

Limitations

  • No support of optional functional groups (UNGUNE),

Alternatives

In python ecosystem:

  • python-edifact - simpler, IMHO less cleaner 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

To develop pydifact, install the dev requirements with pipenv install --dev. This installs all the python packages needed for development and testing.

Format all python files using black.

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. Just exec pytest within the project folder to execute the unit tests.

There is one test to check the performance of parsing huge files, named test_huge_message - you can skip that test by calling

pytest --ignore tests/test_huge_message.py

This is recommended for fast testing.

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.1.4.tar.gz (23.9 kB view details)

Uploaded Source

Built Distribution

pydifact-0.1.4-py3-none-any.whl (38.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydifact-0.1.4.tar.gz
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.7

File hashes

Hashes for pydifact-0.1.4.tar.gz
Algorithm Hash digest
SHA256 60cf9584f2c60465c5c1e57e1be6d652b8b0897d858892636367ada66aba3b31
MD5 a6be0aeee2ffa510bbfe0fa9b7aab251
BLAKE2b-256 149d6ab71071b1f3998bdddb19a838dd10f1189b01b7e2ea97ccd82a573c4ee6

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: pydifact-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 38.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.7

File hashes

Hashes for pydifact-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e39e192e62ecd3079ea53c9d33cc4734f883e21e36cc4fb21f82d965f00d71ac
MD5 bfc5d7f71da85182a3397ef69141e6d0
BLAKE2b-256 ece4970cdb34620a42fbace1629546052e3a631cfd258fc7af0b2cb645621703

See more details on using hashes here.

Provenance

Supported by

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