Package: pydifact
pydifactA Python library to parse and serialize UN/EDIFACT interchanges.PreambleThis is a port ofmetroplex-systems/edifactto Python. Thanks here at the start toCraig Duncanfor 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.InstallAs usual, use a virtualenv, and install via pip or pipenv:pipinstallpydifactHowever, 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:gitclonehttps://github.com/nerdocs/pydifact.gitcdpydifact
pipinstall-e.UsageTo read a full Interchange from a file or string, take theInterchangeclass and
iter over the messages and segments:frompydifact.segmentcollectionimportInterchangeinterchange=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'")formessageininterchange.get_messages():forsegmentinmessage.segments:print("Segment tag:{}, content:{}".format(segment.tag,segment.elements))You may also want to iterate directly on segments :frompydifact.segmentcollectionimportInterchangeinterchange=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'")forsegmentininterchange.segments:print("Segment tag:{}, content:{}".format(segment.tag,segment.elements))Or you can create an EDI interchange on the fly:frompydifact.segmentcollectionimportInterchangefrompydifact.segmentsimportSegmentinterchange=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 :frompydifact.segmentcollectionimportRawSegmentCollectioncollection=RawSegmentCollection.from_str("UNH+1+ORDERS:D:96A:UN:EAN008'")forsegmentincollection.segments:print("Segment tag:{}, content:{}".format(segment.tag,segment.elements))LimitationsNo support of optional functional groups (UNG→UNE),AlternativesIn 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.DevelopmentSetupTo develop pydifact, clone the repository and install the dev requirements:pip install -e .[dev]This installs all the python packages needed for development and testing.Code formattingFormat all python files usingblackbefore 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.Testingpydifact usespytestfor testing.
Just execpytestwithin the project folder to execute the unit tests. Make sure you installed the library properly before usingpip install -e .[dev].There is one test to check the performance of parsing huge files, namedtest_huge_message- you can skip that test by callingpytest--ignoretests/test_huge_message.pyThis is recommended for fast testing.LicenseThis library is licensed under theMITlicense, see theLICENSE file.