Coders / Decoders¶
Encode user-friendly values into internal formats used by the application, and decoder the values back into user-friendly values.
Codec¶
-
class
mhi.enerplot.common.codec.
Codec
¶ Codec: Coder / Decoder
Encode from user-friendly values into an internal value format, and decode from the internal format into (ideally) a user-friendly value.
-
encode
(value)¶ Encode a user-friendly value into an internal format
Parameters: value – the value to encode Returns: the encoded value
-
decode
(value)¶ Decode an internal format value into a more user-friendly format
Parameters: value – the value to decode Returns: the decoded value
-
Keyword Codec¶
-
class
mhi.enerplot.common.codec.
KeywordCodec
¶ Keyword Codec
Encode values for specific keys of a dictionary from user-friendly values into an internal value format, and decode values for those specific keys from the internal format into (ideally) a user-friendly value.
-
encodes
(keyword)¶ Predicate, indicating whether or not this keyword codec will encode and decode a particular keyword
Parameters: keyword (str) – keyword to test Returns: True
if this codec handles thekeyword
,False
otherwiseReturn type: bool
-
encode_all
(kwargs)¶ Encode all values in the given dictionary which are handled by this codec. Values for unrecognized keywords are unchanged.
Parameters: kwargs (dict) – a dictionary of keyword-value pairs Returns: A new dictionary containing encoded values, where supported. Return type: dict
-
decode_all
(kwargs)¶ Decode all values in the given dictionary which are handled by this codec. Values for unrecognized keywords are unchanged.
Parameters: kwargs (dict) – a dictionary of keyword-value pairs Returns: A new dictionary containing decoded values, where supported. Return type: dict
-
decode
(value)¶ Decode an internal format value into a more user-friendly format
Parameters: value – the value to decode Returns: the decoded value
-
encode
(value)¶ Encode a user-friendly value into an internal format
Parameters: value – the value to encode Returns: the encoded value
-
Simple Codec¶
-
class
mhi.enerplot.common.codec.
SimpleCodec
(code_dict=None, **codes)¶ Keyword Codec
Encode values for specific keys of a dictionary from user-friendly values into an internal value format, and decode values for those specific keys from the internal format into (ideally) a user-friendly value.
Parameters: - code_dict (dict) – A dictionary used to translate user-friendly values into internal values.
- **codes – additional keyword-value translation pairs.
Example
A codec which converts fruit names into integers:
>>> codec = SimpleCodec(apple=1, banana=2, pear=3) >>> codec.keywords('fruit') >>> codec.encode('apple') 1 >>> codec.decode(2) 'banana' >>> codec.encode_all({'animal': 'lion', 'fruit': 'pear'}) {'animal': 'lion', 'fruit': 3}
-
alternates
(code_dict, **codes)¶ Provide additional encodings aliases for the codec. These additional options must not duplicate any existing user-friendly keywords, and must not introduce any new values to the mapping.
For instance, a codec may defined the mapping ‘EMTPY’ => 0. An alternate mapping ‘BLANK’ => 0 may be provided, allowing either ‘EMPTY’ or ‘BLANK’ to be encoded as 0, but 0 will always be decoded as ‘EMPTY’.
Parameters: - code_dict (dict) – A dictionary of additional translation aliases.
- **codes – additional keyword-value translation alias pairs.
-
encode
(value)¶ Encode a user-friendly value into an internal format
Parameters: value – the value to encode Returns: the encoded value
-
decode
(value)¶ Decode an internal format value into a more user-friendly format
Parameters: value – the value to decode Returns: the decoded value
-
keywords
(*keywords)¶ Add keywords which will be recognized by this codec when
encode_all()
ordecode_all()
is called.Parameters: *keywords (str) – List of keywords to associate to this codec
-
decode_all
(kwargs)¶ Decode all values in the given dictionary which are handled by this codec. Values for unrecognized keywords are unchanged.
Parameters: kwargs (dict) – a dictionary of keyword-value pairs Returns: A new dictionary containing decoded values, where supported. Return type: dict
-
encode_all
(kwargs)¶ Encode all values in the given dictionary which are handled by this codec. Values for unrecognized keywords are unchanged.
Parameters: kwargs (dict) – a dictionary of keyword-value pairs Returns: A new dictionary containing encoded values, where supported. Return type: dict
-
encodes
(keyword)¶ Predicate, indicating whether or not this keyword codec will encode and decode a particular keyword
Parameters: keyword (str) – keyword to test Returns: True
if this codec handles thekeyword
,False
otherwiseReturn type: bool
Arrows¶
This module is used to encode and decode between a set
of compass directions
{"N", "E", "SW" }
and bit encoded values (0b001001001
).
Direction | Value |
---|---|
N | 1 |
S | 2 |
W | 4 |
E | 8 |
NW | 16 |
NE | 32 |
SW | 64 |
SE | 128 |
-
class
mhi.enerplot.common.arrow.
Arrow
¶ -
encodes
(keyword)¶ Predicate, indicating whether or not this keyword codec will encode and decode a particular keyword
Parameters: keyword (str) – keyword to test Returns: True
ifkeyword
is'arrows'
,False
otherwiseReturn type: bool
-
encode
(dirs)¶ Encode one or more directions into an bit-encoded integer:
>>> arrow.encode("N S") 3 >>> arrow.encode(["E", "W"]) 12
Parameters: dirs – the directions to encode Returns: a bit-encoded direction value Return type: int
-
decode
(dirs)¶ Decode a bit-encoded integer into a direction string:
>>> arrow.decode(15) 'N S W E'
Parameters: dirs (int) – the direction value to decode Returns: a space-separated list of compass directions Return type: str
-
decode_all
(kwargs)¶ Decode all values in the given dictionary which are handled by this codec. Values for unrecognized keywords are unchanged.
Parameters: kwargs (dict) – a dictionary of keyword-value pairs Returns: A new dictionary containing decoded values, where supported. Return type: dict
-
encode_all
(kwargs)¶ Encode all values in the given dictionary which are handled by this codec. Values for unrecognized keywords are unchanged.
Parameters: kwargs (dict) – a dictionary of keyword-value pairs Returns: A new dictionary containing encoded values, where supported. Return type: dict
-
Colours¶
This module is used to encode and decode between common colour names and RGB integer values. Standard Windows and CSS colour names are recognized.
-
class
mhi.enerplot.common.colour.
Colour
¶ -
encodes
(keyword)¶ Predicate, indicating whether or not this keyword codec will encode and decode a particular keyword
Parameters: keyword (str) – keyword to test Returns: True
ifkeyword
is'fg_color'
,'bg_color'
, or'true-color'
,False
otherwiseReturn type: bool
-
encode
(colour)¶ Encode a named colour into an RGB integer value:
>>> colour = Colour() >>> colour.encode("RED") 255 >>> colour.encode("#FA8800") 16418816
Parameters: colour – the colour to encoded Returns: the 888 RGB value Return type: int
-
decode
(colour)¶ Decode an RGB integer value into a named colour, if possible:
>>> colour = Colour() >>> colour.decode(0xFFFFFF) 'white' >>> colour.decode(0x9AFA00) 'mediumspringgreen' >>> colour.decode(16418816) '#FA8800'
Parameters: colour (int) – an 888 RGB colour value Returns: the name of the colour Return type: str
-
decode_all
(kwargs)¶ Decode all values in the given dictionary which are handled by this codec. Values for unrecognized keywords are unchanged.
Parameters: kwargs (dict) – a dictionary of keyword-value pairs Returns: A new dictionary containing decoded values, where supported. Return type: dict
-
encode_all
(kwargs)¶ Encode all values in the given dictionary which are handled by this codec. Values for unrecognized keywords are unchanged.
Parameters: kwargs (dict) – a dictionary of keyword-value pairs Returns: A new dictionary containing encoded values, where supported. Return type: dict
-