LCONF.lconf_structure_classes

Overview

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-Default-Template-Structure-Usage

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 MAIN (ROOT) obj: uses the Root class

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 Key-Value-Mapping: uses the KVMap class

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'),
   ])),
])

LCONF Key :: Value-List: uses the KVList class

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)
])

LCONF Key-Value-List: uses the KVList class

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 List-Of-Tuples: uses the ListOT class

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 Repeated-Block-Identifier: uses the BlkI class

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 Block-Name - Block (dummy block): uses the BlkI class

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'),
      ])
   )),
])

LCONF Key :: Value Pairs items

Tuple format may vary depending on implementation:

  • (KEY, DEFAULT_VALUE)
  • (KEY, DEFAULT_VALUE, TRANSFORM_FUNCTION)
  • (KEY, DEFAULT_VALUE, TRANSFORM_FUNCTION OR None, Empty-KeyValuePair-ReplacementValue)

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.

  • (KEY, DEFAULT_VALUE, None, Empty-KeyValuePair-ReplacementValue)

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

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

# using no transform function but with `Empty-KeyValuePair-ReplacementValue`
if no transform function is used one MUST set it to None
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),
])

LCONF Default Comments

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'),
])

LCONF Default Comments

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'),
])

Classes

class LCONF.lconf_structure_classes.Blk(key_value_list)

(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

Parameters:key_value_list – (list) of tuples: FORMAT: (key, value)
Raises Err:
set_class__dict__item(key, value)

Sets the class __dict__: key to value: if key did not exist it is added

Parameters:
  • key – (str)
  • value – (any)
class LCONF.lconf_structure_classes.BlkI(min_required_blocks, max_allowed_blocks, dummy_blk_obj)

(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:
  • min_required_blocks

    (int) number of minimum required blocks when a LCONF text/source is parsed`

    • 0 or greater
    • to not define it: set it to -1
  • max_allowed_blocks

    (int) number of maximum required blocks when a LCONF text/source is parsed`

    • 1 or greater
    • to not define it: set it to -1
  • dummy_blk_obj – (Blk obj) the defaults for any Block
Raises Err:

set_class__dict__item(key, value)

Sets the class __dict__: key to value: if key did not exist it is added

Parameters:
  • key – (str)
  • value – (any)
class LCONF.lconf_structure_classes.KVList(use_oneline, list_of_items)

(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:
  • use_oneline

    (bool)

    • Set it to True: if the default list should be a oneline list Key :: Value-List
    • Set it to False: if the default list should be a multiline list Key-Value-List
  • list_of_items – (list)
Raises Err:

set_class__dict__item(key, value)

Sets the class __dict__: key to value: if key did not exist it is added

Parameters:
  • key – (str)
  • value – (any)
class LCONF.lconf_structure_classes.KVMap(key_value_list)

(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

Parameters:key_value_list

(list) of tuples: FORMAT: (key, value)

  • may not be empty
set_class__dict__item(key, value)

Sets the class __dict__: key to value: if key did not exist it is added

Parameters:
  • key – (str)
  • value – (any)
class LCONF.lconf_structure_classes.Root(key_value_list)

(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

Parameters:key_value_list – (list) of tuples: FORMAT: (key, value)
Raises Err:
static frompickle(in_pickle_dumps)

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:
set_class__dict__item(key, value)

Sets the class __dict__: key to value: if key did not exist it is added

Parameters:
  • key – (str)
  • value – (any)
class LCONF.lconf_structure_classes.ListOT(column_names, list_of_tuples, column_replace_missing=())

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:
  • column_names – (tuple) strings of column names: must be unique names
  • list_of_tuples – (list) items (tuples - rows) must have the same number of values as there are column_names
  • column_replace_missing

    (tuple)

    • if not empty: then for each column one replacement value must be defined
      these are values which will replace missing column items

    Note

    this will ALSO run through any transform functions: values must be type string

Raises Err:

..note:: after the initialization all of them are unchangeable: except some: extra_data

replace_column_names(new_column_names_tuple)

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:
set_class__dict__item(key, value)

Sets the class __dict__: key to value: if key did not exist it is added

Parameters:
  • key – (str)
  • value – (any)
this_column_values(column_name)

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: