Created
July 17, 2019 16:33
-
-
Save Zwork101/a8a94fa5588de07ddbc8d81043b8003e to your computer and use it in GitHub Desktop.
Another peels example
This file contains hidden or 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
class EducationFlags(FlagConverter): | |
ELEMENTARY_COMPLETED = 1 | |
MIDDLE_SCHOOL_COMPLETED = 2 | |
HIGHSCHOOL_COMPLETED = 4 | |
COLLEGE_COMPLETED = 8 | |
BACHELOR = 16 | |
MASTER = 32 | |
DOCTOR = 64 | |
person = Peel( | |
Segment("name_len", 1, "I"), | |
Segment("name", "name_len", "U"), | |
Segment("age", 1, "I"), | |
Segment("education", 1, EducationFlags) | |
) | |
no_pet = Segment("no_pet", 0, "-") | |
cat_peel = Peel( | |
Segment("name_len", 1, "I"), | |
Segment("name", "name_len", "U"), | |
Segment("age", 1, "I") | |
) | |
cat = Segment("cat", 1, cat_peel) | |
dog_peel = Peel( | |
Segment("name_len", 1, "I"), | |
Segment("name", "name_len", "U"), | |
Segment("age", 1, "I"), | |
Segment("barks", 1, "B") | |
) | |
dog = Segment("dog", 1, dog_peel) | |
household = Peel( | |
Segment("address_len", 1, "I"), | |
Segment("address", "address_len", "U"), | |
Segment("family_size", 1, "I"), | |
Segment("family", "family_size", person), | |
Segment("pet_id", 1, "I"), | |
SwitchSegment("pet_id", {0: no_pet, 1: cat, 2: dog}) | |
) | |
data = household.generate( | |
address_len=13, | |
address="123 Hill Road", | |
family_size=2, | |
family=[ | |
{"name_len": 3, "name": "Bob", "age": 29, "education": EducationFlags(7)}, | |
{"name_len": 4, "name": "Mary", "age": 30, "education": EducationFlags(47)} | |
], | |
pet_id=2, | |
dog=[{"name_len": 5, "name": "Doggy", "age": 3, "barks": False}] | |
) | |
print(data) | |
stream = BytesIO(data) | |
stream.read(0) | |
print( | |
household.parse_stream(stream) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment