-
Notifications
You must be signed in to change notification settings - Fork 5
Add support for new Map type introduced in Erlang R17 #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ | |
|
|
||
| from twotp.term import Integer, List, Tuple, Float, Atom, Reference | ||
| from twotp.term import Port, Pid, Binary, Fun, NewFun, Export, BitBinary | ||
| from twotp.term import ConstantHolder, Dict, Set | ||
| from twotp.term import ConstantHolder, Dict, Set, Map | ||
|
|
||
|
|
||
|
|
||
|
|
@@ -117,6 +117,16 @@ def parse_list(self, data): | |
| return List(elements), data[1:] | ||
|
|
||
|
|
||
| def parse_map(self, data): | ||
| """ | ||
| Parse a map. | ||
| """ | ||
| arity = self.parseInt(data[:4]) | ||
| elements, data = self._parse_seq(arity*2, data[4:]) # K1,V1,K2,V2, ... | ||
|
|
||
| return Map(dict([(elements[i], elements[i+1]) for i in range(0, len(elements), 2)])), data | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can remove the brackets, maybe use xrange too. |
||
|
|
||
|
|
||
| def _parse_seq(self, arity, data): | ||
| """ | ||
| Helper methods to deal with sequence of encoded terms. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -205,6 +205,35 @@ def Set(val): | |
| return set(val) | ||
|
|
||
|
|
||
| class Map(Term): | ||
| def __init__(self, val): | ||
| self._val = val | ||
|
|
||
|
|
||
| def __len__(self): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be worth maybe inheriting collections.Mapping and to provide the necessary methods. |
||
| return len(self._val) | ||
|
|
||
|
|
||
| def __eq__(self, other): | ||
| """ | ||
| Check for equality of atom text. | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docstring doesn't match. |
||
| """ | ||
| if not isinstance(other, Map): | ||
| return False | ||
| return self._val == other._val | ||
|
|
||
|
|
||
| def __repr__(self): | ||
| """ | ||
| Simple representation with the text. | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
| """ | ||
| s = "<%s at %s, values %s>" % (self.__class__.__name__, | ||
| hex(id(self)), self._val) | ||
| return s | ||
|
|
||
| def iteritems(self): | ||
| return self._val.iteritems() | ||
|
|
||
|
|
||
| class Pid(Term): | ||
| """ | ||
|
|
@@ -548,6 +577,7 @@ class ConstantHolder(object): | |
| MAGIC_BIT_BINARY = 77 | ||
| MAGIC_COMPRESSED = 80 | ||
| MAGIC_NEW_FLOAT = 70 | ||
| MAGIC_MAP = 116 | ||
|
|
||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove the parenthesis here.