diff options
author | jaseg <code@jaseg.net> | 2017-12-25 13:16:58 +0100 |
---|---|---|
committer | jaseg <code@jaseg.net> | 2017-12-25 13:16:58 +0100 |
commit | 62add955f99e96a89bc27002675eb99f5182185d (patch) | |
tree | 65d50a25267e8670e9937d8f4a86ec0339940c2a | |
parent | 092707259b407c1b30b45b415391287cecbf3c34 (diff) | |
download | python-mpv-62add955f99e96a89bc27002675eb99f5182185d.tar.gz python-mpv-62add955f99e96a89bc27002675eb99f5182185d.tar.bz2 python-mpv-62add955f99e96a89bc27002675eb99f5182185d.zip |
Refactor node_cast_value to use large if/elif instead of dict lookup
-rw-r--r-- | mpv.py | 37 |
1 files changed, 25 insertions, 12 deletions
@@ -196,18 +196,31 @@ class MpvNode(Structure): @staticmethod def node_cast_value(v, fmt=MpvFormat.NODE, decoder=identity_decoder): - return { - MpvFormat.NONE: lambda v: None, - MpvFormat.STRING: lambda v: decoder(v.string), - MpvFormat.OSD_STRING: lambda v: v.string.decode('utf-8'), - MpvFormat.FLAG: lambda v: bool(v.flag), - MpvFormat.INT64: lambda v: v.int64, - MpvFormat.DOUBLE: lambda v: v.double, - MpvFormat.NODE: lambda v: v.node.contents.node_value(decoder) if v.node else None, - MpvFormat.NODE_ARRAY: lambda v: v.list.contents.array_value(decoder) if v.list else None, - MpvFormat.NODE_MAP: lambda v: v.map.contents.dict_value(decoder) if v.map else None, - MpvFormat.BYTE_ARRAY: lambda v: v.byte_array.contents.bytes_value() if v.byte_array else None, - }[fmt](v) + if fmt == MpvFormat.NONE: + return None + elif fmt == MpvFormat.STRING: + return decoder(v.string) + elif fmt == MpvFormat.OSD_STRING: + return v.string.decode('utf-8') + elif fmt == MpvFormat.FLAG: + return bool(v.flag) + elif fmt == MpvFormat.INT64: + return v.int64 + elif fmt == MpvFormat.DOUBLE: + return v.double + else: + if not v.node: # Check for null pointer + return None + if fmt == MpvFormat.NODE: + return v.node.contents.node_value(decoder) + elif fmt == MpvFormat.NODE_ARRAY: + return v.list.contents.array_value(decoder) + elif fmt == MpvFormat.NODE_MAP: + return v.map.contents.dict_value(decoder) + elif fmt == MpvFormat.BYTE_ARRAY: + return v.byte_array.contents.bytes_value() + else: + raise TypeError('Unknown MPV node format {}. Please submit a bug report.'.format(fmt)) class MpvNodeUnion(Union): _fields_ = [('string', c_char_p), |