Skip to content

Commit

Permalink
doc: add sample code to README
Browse files Browse the repository at this point in the history
  • Loading branch information
CJHwong committed May 6, 2022
1 parent a362908 commit c423639
Showing 1 changed file with 76 additions and 0 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
```

0 comments on commit c423639

Please sign in to comment.