Most of this code is based on the ReOBJ package. see LICENSE for more info
These Classes are used for the LCONF-Default-Template-Structure to implement the defaults and order.
LCONF does require a predefined implemented LCONF-Default-Template-Structure one can not >> just parse << a ‘LCONF text source/file’.
The “official python lconf library” uses for a LCONF-Default-Template-Structure exclusively the predefined classes in this module: LCONF/LconfStructureClasses.py
LCONF-Example
# Comment-Line: Root/Main LCONF-Section START TAG Line
___SECTION :: Example LCONF Root
key1 :: value1
key2 :: value2
key3 :: value3
key4 ::
# Comment-Line: Root/Main LCONF-Section END TAG Line
___END
Python-Example
Example LCONF-Default-Template-Structure: using class: Root
Every LCONF-Default-Template-Structure must have at least the Root class
example_lconf_template = Root([
('#1', '# Comment-Line: Root/Main key value pair'),
('key1', 'default_value1'),
('key2', 'default_value2', transform_function),
('#2', '# Comment: Root/Main key value pair with transform_function and `Empty-KeyValuePair-ReplacementValue`'),
('key3', 'default_value3', transform_function, 'empty_replacement_value3'),
('key4', 'default_value4', transform_function, 'empty_replacement_value4'),
])
LCONF-Example
# Key-Value-Mapping can not have a direct transform function applied
___SECTION :: Example LCONF `Key-Value-Mapping`
# Comment-Line: below is a Main `Key-Value-Mapping`
. key_value_mapping
key1 :: value1
key2 :: value2
key3 :: value3
key4 ::
___END
Python-Example
Example LCONF-Default-Template-Structure: using class: KVMap
example_lconf_template = Root([
# Comment-Line: below is a Main `Key-Value-Mapping`
('key_value_mapping', KVMap([
('#1', '# Comment-Line: Root/Main key value pair'),
('key1', 'default_value1'),
('key2', 'default_value2', transform_function),
('#2', '# Comment: Root/Main key value pair with transform_function and `Empty-KeyValuePair-ReplacementValue`'),
('key3', 'default_value3', transform_function, 'empty_replacement_value3'),
('key4', 'default_value4', transform_function, 'empty_replacement_value4'),
])),
])
Key :: Value-List and Key-Value-List both use the KVList class
The implementation of them are the same except that the parameter use_oneline is set for default emitting.
LCONF-Example
___SECTION :: Example LCONF `Key :: Value-List`
# Comment-Line: below is a Main `Key :: Value-List`
- list :: 1,2,3
___END
Python-Example
Example LCONF-Default-Template-Structure: using class: KVList
to emit as default Key :: Value-List set parameter use_oneline to True
# using no transform function
example_lconf_template = Root([
('#1', '# Comment-Line: below is a Main `Key :: Value-List`: use_oneline is True'),
('list', KVList(True, ['default_value1', 'default_value2']))
])
# using a transform function: applied to each list item
example_lconf_template = Root([
('#1', '# Comment-Line: below is a Main `Key :: Value-List`: with transform_function - use_oneline is True '),
('list', KVList(True, ['default_value1', 'default_value2']), transform_function)
])
Key-Value-List is very similar to Key :: Value-List and also uses the KVList class
LCONF-Example
to emit as default Key-Value-List set parameter use_oneline to False
___SECTION :: Example LCONF `Key-Value-List`
# Comment-Line: below is a Main `Key-Value-List`
- list
1
2
3
___END
Python-Example
Example LCONF-Default-Template-Structure: using class: KVList
# using no transform function
example_lconf_template = Root([
('#1', '# Comment-Line: below is a Main `Key :: Value-List`: use_oneline is False'),
('list', KVList(False, ['default_value1', 'default_value2']))
])
# using a transform function: applied to each list item
example_lconf_template = Root([
('#1', '# Comment-Line: below is a Main `Key :: Value-List`: with transform_function - use_oneline is False '),
('list', KVList(False, ['default_value1', 'default_value2']), transform_function)
])
LCONF-Example
___SECTION :: Example LCONF `List-Of-Tuples`
# Comment-Line: below is a Main `List-Of-Tuples` with 4 items: |Color Name|Red|Green|Blue|
- list_of_color_tuples |Color Name|Red|Green|Blue|
# Comment-Line: `List-Of-Tuples` item lines (rows)
forestgreen, 34, 139, 34
brick, 156, 102, 31
___END
Python-Example
Example LCONF-Default-Template-Structure: using class: ListOT
# using no transform function
example_lconf_template = Root([
# Comment-Line: below is a Main `List-Of-Tuples` with 4 columns: |Color Name|Red|Green|Blue|'
('list_of_color_tuples', ListOT(('Color Name', 'Red', 'Green', 'Blue'), [
('default_value_Color_Name', 'default_value_Red', 'default_value_Green', 'default_value_Blue'),
('default_value_Color_Name', 'default_value_Red', 'default_value_Green', 'default_value_Blue')
])),
])
# using multiple transform functions
example_lconf_template = Root([
# Comment-Line: below is a Main `List-Of-Tuples` with 4 columns: |Color Name|Red|Green|Blue|'
('list_of_color_tuples', ListOT(('Color Name', 'Red', 'Green', 'Blue'), [
('default_value_Color_Name', 'default_value_Red', 'default_value_Green', 'default_value_Blue'),
('default_value_Color_Name', 'default_value_Red', 'default_value_Green', 'default_value_Blue')
]), (None, transform_function, transform_function, transform_function)),
])
LCONF-Example
___SECTION :: Example LCONF `List-Of-Tuples`
# Comment-Line: below is a Main `List-Of-Tuples` with 3 items: |x|y|z|
- list_of_point_tuples |x|y|z|
# Comment-Line: `List-Of-Tuples` item lines (rows)
1,3,7
2,6,14
___END
Python-Example
Example LCONF-Default-Template-Structure: using class: ListOT
# using one transform function for each item in every list tuple (row)
example_lconf_template = Root([
# Comment-Line: below is a Main `List-Of-Tuples` with 3 items: |x|y|z|
('list_of_point_tuples', ListOT(('x', 'y', 'z'), [
('default_value_x', 'default_value_y', 'default_value_z'),
('default_value_x', 'default_value_y', 'default_value_z'),
]), transform_function),
])
LCONF-Example
List-Of-Tuples with missing items
___SECTION :: Example LCONF `List-Of-Tuples`
# Comment-Line: below is a Main `List-Of-Tuples` with 3 items: |x|y|z|
- list_of_point_tuples |x|y|z|
# Comment-Line: `List-Of-Tuples` item lines (rows) with missing items
,,
2, ,14
___END
Python-Example
Example LCONF-Default-Template-Structure: using class: ListOT
# using no column_replace_missing items
example_lconf_template = Root([
# Comment-Line: below is a Main `List-Of-Tuples` with 3 items: |x|y|z|
('list_of_point_tuples', ListOT(('x', 'y', 'z'), [
('default_value_x', 'default_value_y', 'default_value_z'),
('default_value_x', 'default_value_y', 'default_value_z'),
])),
])
# using column_replace_missing items
example_lconf_template = Root([
# Comment-Line: below is a Main `List-Of-Tuples` with 3 items: |x|y|z|
('list_of_point_tuples', ListOT(('x', 'y', 'z'), [
('default_value_x', 'default_value_y', 'default_value_z'),
('default_value_x', 'default_value_y', 'default_value_z'),
], column_replace_missing=('-1', '-1', '-1'))),
])
# using column_replace_missing items and one transform function for each item in every list tuple (row)
example_lconf_template = Root([
# Comment-Line: below is a Main `List-Of-Tuples` with 3 items: |x|y|z|
('list_of_point_tuples', ListOT(('x', 'y', 'z'), [
('default_value_x', 'default_value_y', 'default_value_z'),
('default_value_x', 'default_value_y', 'default_value_z'),
], column_replace_missing=('-1', '-1', '-1')), transform_function),
])
# using column_replace_missing items and multiple transform function for each item in every list tuple (row)
example_lconf_template = Root([
# Comment-Line: below is a Main `List-Of-Tuples` with 3 items: |x|y|z|
('list_of_point_tuples', ListOT(('x', 'y', 'z'), [
('default_value_x', 'default_value_y', 'default_value_z'),
('default_value_x', 'default_value_y', 'default_value_z'),
], column_replace_missing=('-1', '-1', '-1')), (transform_function, transform_function, transform_function)),
])
LCONF-Example
# Repeated-Block-Identifier can not have a direct transform function applied
___SECTION :: Example LCONF `Repeated-Block-Identifier`
# Comment-Line: below is a Main `Repeated-Block-Identifier`
* My_Repeated_Block
BlockName1
key1 :: value1
key2 :: value2
key3 ::
BlockName2
key1 :: value1
key2 :: value2
key3 :: value3
___END
Python-Example
Example LCONF-Default-Template-Structure: using class: BlkI
# Repeated-Block-Identifier min_required_blocks, max_allowed_blocks not defined (-1)
example_lconf_template = Root([
# Comment-Line: Root/Main `Repeated-Block-Identifier`: **min_required_blocks, max_allowed_blocks** not defined (-1)
('My_Repeated_Block', BlkI(-1, -1,
# Comment-Line: Dummy Block
Blk([
# Comment-Line: Block key value pair
('key1', 'default_value1`'),
# Comment-Line: Block key value pair with transform_function
('key2', 'default_value2', transform_function),
('#1', '# Comment: Block key value pair with transform_function and `Empty-KeyValuePair-ReplacementValue`'),
('key3', 'default_value3', transform_function, 'empty_replacement_value3'),
])
)),
# Repeated-Block-Identifier min_required_blocks, max_allowed_blocks set both to 2
example_lconf_template = Root([
# Comment-Line: Root/Main `Repeated-Block-Identifier`: **min_required_blocks, max_allowed_blocks** set both to 2
('My_Repeated_Block', BlkI(2, 2,
# Comment-Line: Dummy Block
Blk([
# Comment-Line: Block key value pair
('key1', 'default_value1`'),
# Comment-Line: Block key value pair with transform_function
('key2', 'default_value2', transform_function),
('#1', '# Comment: Block key value pair with transform_function and `Empty-KeyValuePair-ReplacementValue`'),
('key3', 'default_value3', transform_function, 'empty_replacement_value3'),
])
)),
])
LCONF-Example
# Block-Name - Block (dummy block) can not have a direct transform function applied
___SECTION :: Example LCONF `Block-Name` - `Block` (dummy block)
* My_Repeated_Block
# Comment-Line: below is a `Block-Name` - `Block` (dummy block)
BlockName1
key1 :: value1
key2 :: value2
key3 :: value3
# Comment-Line: below is another `Block-Name` - `Block` (dummy block)
BlockName2
key1 :: value1
key2 :: value2
key3 ::
___END
Python-Example
Example LCONF-Default-Template-Structure: using class: Blk
example_lconf_template = Root([
# Comment-Line: Root/Main `Repeated-Block-Identifier`: **min_required_blocks set to 2, max_allowed_blocks** set to 5
('My_Repeated_Block', BlkI(2, 5,
# Comment-Line: Dummy Block
Blk([
# Comment-Line: Block key value pair
('key1', 'default_value1`'),
# Comment-Line: Block key value pair with transform_function
('key2', 'default_value2', transform_function),
('#1', '# Comment: Block key value pair with transform_function and `Empty-KeyValuePair-ReplacementValue`'),
('key3', 'default_value3', transform_function, 'empty_replacement_value3'),
])
)),
])
Tuple format may vary depending on implementation:
Important
TRANSFORM FUNCTIONS
Must always be a proper transform-function except for Key :: Value Pairs which do not use any transformfunction but still an Empty-KeyValuePair-ReplacementValue. In such case the one uses None instead of any transform-function.
LCONF-Example
___SECTION :: Example LCONF `Key :: Value Pairs` items
# Comment-Line: Root/Main key value pair
key1 :: value1
___END
Python-Example
Example LCONF-Default-Template-Structure: using Key :: Value Pairs items
# using no transform function
example_lconf_template = Root([
# Comment-Line: Root/Main key value pair
('key1', 'default_value1'),
])
# using a transform function
example_lconf_template = Root([
# Comment-Line: Root/Main key value pair with transform_function
('key1', 'default_value1', transform_function),
])
# using a transform function and Empty-KeyValuePair-ReplacementValue
example_lconf_template = Root([
('#1', '# Comment-Line: Root/Main key value pair with transform_function and Empty-KeyValuePair-ReplacementValue'),
('key1', 'default_value1', transform_function, 'empty_replacement_value1'),
])
# using no transform function and Empty-KeyValuePair-ReplacementValue
example_lconf_template = Root([
('#1a', '# Comment: Root/Main key value pair with no transform_function and Empty-KeyValuePair-ReplacementValue'),
('#1b', '# Instead of the transform_function use None'),
('key1', 'default_value1', None, 'empty_replacement_value1'),
])
Empty Key :: Value Pairs are supported. One can optionally define an Empty-KeyValuePair-ReplacementValue in the LCONF-Default-Template-Structure.
LCONF-Example
___SECTION :: Example LCONF `Key :: Value Pairs` items
# Comment-Line: Root/Main key value pair
key1 :: value1
key2 :: Red
key3 ::
___END
Python-Example
Example LCONF-Default-Template-Structure: using Empty Key :: Value Pairs with Empty-KeyValuePair-ReplacementValue items
example_lconf_template = Root([
('#1', '# Comment: Root/Main key value pair - no transform function - with `Empty-KeyValuePair-ReplacementValue`'),
('key1', 'default_value1', None, 'Empty-KeyValuePair-ReplacementValue'),
('key2', 'Blue', None, 'NO-COLOR-DEFINED'),
('key3', 'Blue', None, 'NO-COLOR-DEFINED'),
])
LCONF-Example
___SECTION :: Example LCONF `Key :: Value Pairs` items
# Comment-Line: Root/Main key value pair
key1 :: value1
key2 ::
key3 ::
___END
# using a transform function and with `Empty-KeyValuePair-ReplacementValue`
example_lconf_template = Root([
('#1', '# Comment Root/Main key value pair - with transform function - with `Empty-KeyValuePair-ReplacementValue`'),
('key1', 'default_value1', transform_function, 'Empty-KeyValuePair-ReplacementValue'),
# Comment-Line: in the case below: if `key2` is in the `LCONF source` set to empty: the parsed lconf obj will have
# the `Empty-KeyValuePair-ReplacementValue` -1
('key2', 500, lconf_to_int, -1),
# Comment-Line: in the case below: if `key3` is by default empty: the parsed lconf obj will have
# the `Empty-KeyValuePair-ReplacementValue` -1 except the `LCONF source` to be parsed has set it to something
('key3', '', lconf_to_float, -999.4),
])
Default-Comments are implemented in the LCONF-Default-Template-Structure like Key :: Value Pairs items but are special:
Key and value must start with an one number sign #
- usually the keys are just incremented numbers to make them unique
example_lconf_template = Root([
('#1', '# Comment this is a `Default Comment Line` which can be emitted'),
('key1', 'default_value1'),
('#2', '# this is another `Default Comment Line` which can be emitted'),
('key2', 'default_value1'),
])
Key must start with an one number sign #
- usually the keys are just incremented numbers to make them unique
The Value must be an empty string.
example_lconf_template = Root([
# Comment below is an `Empty default Comment Line` which can be emitted
('#1', ''),
('key1', 'default_value1'),
# Comment below is another `Empty default Comment Line` which can be emitted
('#2', ''),
('key2', 'default_value1'),
])
(Blk)Block Class: LCONF-Default-Template-Structure Block-Name class
Has additional attributes:
key_order (list) the keys in order as initialized inclusive Default-Comment/Empty Lines
key_order_no_comments (list) the keys in order as initialized exclusive Default-Comment/Empty Lines
key_empty_replacementvalue (dict) all keys which have Empty-KeyValuePair-ReplacementValue
- if LCONF-Default-Template-Structure defines an Empty-KeyValuePair-ReplacementValue then the key is added with the Empty-KeyValuePair-ReplacementValue
Parameters: | key_value_list – (list) of tuples: FORMAT: (key, value) |
---|---|
Raises Err: |
Sets the class __dict__: key to value: if key did not exist it is added
Parameters: |
|
---|
(Blk)Block (I)dentifier Class: LCONF-Default-Template-Structure Repeated-Block-Identifier class
Has additional attributes:
- min_required_blocks (int)
- max_allowed_blocks (int)
Parameters: |
|
---|---|
Raises Err: |
Sets the class __dict__: key to value: if key did not exist it is added
Parameters: |
|
---|
(K)ey(V)alueList Class: LCONF-Default-Template-Structure Key :: Value-List and Key-Value-List class
Has additional attributes:
use_oneline (bool) will be initialized: default option to emit it as oneline or multiline list
- useful in case the list has many default values or very long values
Parameters: |
|
---|---|
Raises Err: |
Sets the class __dict__: key to value: if key did not exist it is added
Parameters: |
|
---|
(K)ey(V)alue(Map)ping Class: LCONF-Default-Template-Structure Key-Value-Mappings class
Has additional attributes:
key_order (list) the keys in order as initialized inclusive Default-Comment/Empty Lines
key_order_no_comments (list) the keys in order as initialized exclusive Default-Comment/Empty Lines
key_empty_replacementvalue (dict) all keys which have Empty-KeyValuePair-ReplacementValue
- if LCONF-Default-Template-Structure defines an Empty-KeyValuePair-ReplacementValue then the key is added with the Empty-KeyValuePair-ReplacementValue
Parameters: | key_value_list – (list) of tuples: FORMAT: (key, value)
|
---|
Sets the class __dict__: key to value: if key did not exist it is added
Parameters: |
|
---|
(M)ain/Root Class: LCONF-Default-Template-Structure Main/Root obj class
Has additional attributes:
key_order (list) the keys in order as initialized inclusive Default-Comment/Empty Lines
key_order_no_comments (list) the keys in order as initialized exclusive Default-Comment/Empty Lines
key_empty_replacementvalue (dict) all keys which have Empty-KeyValuePair-ReplacementValue
- if LCONF-Default-Template-Structure defines an Empty-KeyValuePair-ReplacementValue then the key is added with the Empty-KeyValuePair-ReplacementValue
Parameters: | key_value_list – (list) of tuples: FORMAT: (key, value) |
---|---|
Raises Err: |
Create a new Root from a pickled dumps.
Parameters: | in_pickle_dumps – (bytes) a pickled Root dumps |
---|---|
Returns: | (obj) a new Root object |
Raises Err: |
Sets the class __dict__: key to value: if key did not exist it is added
Parameters: |
|
---|
List(O)f(T)uples Class: LCONF-Default-Template-Structure List-Of-Tuples class
Tuple items (rows) should only be simple objects: no nested list, dictionary ect..
Note
to replace column_names use the replace_column_names() as it also updates related things
Has additional attributes:
- column_names (tuple): will be initialized: with column_names
- column_names_idx_lookup (dict): will be initialized: column_name, to tuple_idx mapping
- column_names_counted (int): will be initialized: with the number of column_names
- column_replace_missing (tuple)
Parameters: |
|
---|---|
Raises Err: |
..note:: after the initialization all of them are unchangeable: except some: extra_data
Replaces the column_names (tuple) with the new_column_names_tuple
Parameters: | new_column_names_tuple – (tuple) must have the same number of names as the current column_names |
---|---|
Raises Err: |
Sets the class __dict__: key to value: if key did not exist it is added
Parameters: |
|
---|
Returns the items of all rows for the column
Parameters: | column_name – (string) |
---|---|
Returns: | (list) all items of the column for all rows |
Raises Err: |