-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from hardcoretech/refactor/intermediate-obj
refactor: render to intermediate objects before stringify
- Loading branch information
Showing
17 changed files
with
839 additions
and
440 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.