Skip to content

Commit

Permalink
Merge pull request #1 from hardcoretech/refactor/intermediate-obj
Browse files Browse the repository at this point in the history
refactor: render to intermediate objects before stringify
  • Loading branch information
CJHwong authored May 6, 2022
2 parents 98e0a52 + c423639 commit d669d96
Show file tree
Hide file tree
Showing 17 changed files with 839 additions and 440 deletions.
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,77 @@
# pyx12-lib

---

## Quick Example

### Rendering
* Define the grammar for the segment.
```python
from pyx12lib.core.grammar import Element, BaseSegment, segment, element


class StSegment(BaseSegment):
segment_id = 'ST'
usage = segment.USAGE_MANDATORY
max_use = 1
elements = (
Element(
reference_designator='ST01',
name='Transaction Set Identifier Code',
usage=element.USAGE_MANDATORY,
element_type=element.ELEMENT_TYPE_ID,
minimum=3,
maximum=3,
),
Element(
reference_designator='ST02',
name='Transaction Set Control Number',
usage=element.USAGE_MANDATORY,
element_type=element.ELEMENT_TYPE_STRING,
minimum=4,
maximum=9,
),
)
```
* Render the segment from its grammar.
```python
from pyx12lib.common.interchange.grammar import StSegment
from pyx12lib.core.renderer import SegmentRenderer


class StRenderer(SegmentRenderer):
grammar = StSegment

element_value_getters = {
'ST01': lambda ele, data, stat: '997',
'ST02': lambda ele, data, stat: '{:04d}'.format(data.transaction_set_no),
}
```
* Advanced definition for element value getters
```python
from pyx12lib.common.interchange.grammar import StSegment
from pyx12lib.core.renderer import SegmentRenderer


class StRenderer(SegmentRenderer):
grammar = StSegment

@property
def element_value_getters(self):
return {
'ST01': lambda ele, data, stat: '997',
'ST02': self.st02,
}

@staticmethod
def st02(ele, data, stat):
assert ele is StSegment.elements[1] # the grammar for the element.
assert stat['ST01'] is '997' # stat consists of rendered data so far.
return '{:04d}'.format(data.transaction_set_no) # return value should always be strings
```

---
## Test
```bash
python -m unittest discover
```
5 changes: 0 additions & 5 deletions pyx12lib/common/interchange/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +0,0 @@
from __future__ import absolute_import

from .functional_group import GeRenderer, GsRenderer
from .interchange import IeaRenderer, IsaRenderer
from .transaction_set import SeRenderer, StRenderer
54 changes: 0 additions & 54 deletions pyx12lib/common/interchange/functional_group.py

This file was deleted.

94 changes: 45 additions & 49 deletions pyx12lib/common/interchange/grammar/functional_group.py
Original file line number Diff line number Diff line change
@@ -1,99 +1,95 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from pyx12lib.core.grammar import Element
from pyx12lib.core.grammar.segment import USAGE_MANDATORY, BaseSegment
from pyx12lib.core.grammar import BaseSegment, Element, element, segment


class GsSegment(BaseSegment):
segment_id = 'GS'
usage = 'M'
segment_id = "GS"
usage = segment.USAGE_MANDATORY
max_use = 1
elements = (
Element(
reference_designator='GS01',
name='Functional Identifier Code',
usage=USAGE_MANDATORY,
element_type='ID',
reference_designator="GS01",
name="Functional Identifier Code",
usage=element.USAGE_MANDATORY,
element_type=element.ELEMENT_TYPE_ID,
minimum=2,
maximum=2,
),
Element(
reference_designator='GS02',
name='Application Sender\'s Code',
usage=USAGE_MANDATORY,
element_type='AN',
reference_designator="GS02",
name="Application Sender's Code",
usage=element.USAGE_MANDATORY,
element_type=element.ELEMENT_TYPE_STRING,
minimum=2,
maximum=15,
),
Element(
reference_designator='GS03',
name='Application Receiver\'s Code',
usage=USAGE_MANDATORY,
element_type='AN',
reference_designator="GS03",
name="Application Receiver's Code",
usage=element.USAGE_MANDATORY,
element_type=element.ELEMENT_TYPE_STRING,
minimum=2,
maximum=15,
),
Element(
reference_designator='GS04',
name='Date',
usage=USAGE_MANDATORY,
element_type='DT',
reference_designator="GS04",
name="Date",
usage=element.USAGE_MANDATORY,
element_type=element.ELEMENT_TYPE_DATE,
minimum=8,
maximum=8,
),
Element(
reference_designator='GS05',
name='Time',
usage=USAGE_MANDATORY,
element_type='TM',
reference_designator="GS05",
name="Time",
usage=element.USAGE_MANDATORY,
element_type=element.ELEMENT_TYPE_TIME,
minimum=4,
maximum=8,
),
Element(
reference_designator='GS06',
name='Group Control Number',
usage=USAGE_MANDATORY,
element_type='N0',
reference_designator="GS06",
name="Group Control Number",
usage=element.USAGE_MANDATORY,
element_type=element.get_numeric_type(0),
minimum=1,
maximum=9,
),
Element(
reference_designator='GS07',
name='Responsible Agency Code',
usage=USAGE_MANDATORY,
element_type='ID',
reference_designator="GS07",
name="Responsible Agency Code",
usage=element.USAGE_MANDATORY,
element_type=element.ELEMENT_TYPE_ID,
minimum=1,
maximum=2,
),
Element(
reference_designator='GS08',
name='Version / Release / Industry Identifier Code',
usage=USAGE_MANDATORY,
element_type='AN',
reference_designator="GS08",
name="Version / Release / Industry Identifier Code",
usage=element.USAGE_MANDATORY,
element_type=element.ELEMENT_TYPE_STRING,
minimum=1,
maximum=12,
),
)


class GeSegment(BaseSegment):
segment_id = 'GE'
class GeSegment(segment.BaseSegment):
segment_id = "GE"
max_use = 1
elements = (
Element(
reference_designator='GE01',
name='VNumber of Transaction Sets Included',
usage=USAGE_MANDATORY,
element_type='N0',
reference_designator="GE01",
name="Number of Transaction Sets Included",
usage=element.USAGE_MANDATORY,
element_type=element.get_numeric_type(0),
minimum=1,
maximum=6,
),
Element(
reference_designator='GE02',
name='Group Control Number',
usage=USAGE_MANDATORY,
element_type='N0',
reference_designator="GE02",
name="Group Control Number",
usage=element.USAGE_MANDATORY,
element_type=element.get_numeric_type(0),
minimum=1,
maximum=9,
),
Expand Down
Loading

0 comments on commit d669d96

Please sign in to comment.