Skip to main content

Powerful Ligic Tree operations.

Project description

Install

pip install logictree

Basic Use Case

  • import
>>> from logictree import LogicTree, parse_logic_str
  • create
    include build a new Logic Tree object from a logic-string or logic-dict
>>> logic_dic = parse_logic_str('{0} and {1} and not {2} and not not {3}')
# logic_dic:
{'child_nodes': ['0',
                 '1',
                 {'child_nodes': ['2'], 'inverse': True, 'logic': None},
                 '3'],
'inverse': False,
'logic': 'and'}                       
>>> logic_tree = LogicTree.new_from_dict(logic_dic)
# logic_tree:
LogicTree(child_nodes=['0',
                       '1',
                       LogicTree(child_nodes=['2'], logic='and', inverse=True),
                       '3'],
          logic='and',
          inverse=False)
>>> logic_tree.to_str(line_feed=False)
'{0} and {1} and not {2} and {3}'
>>> logic_tree.to_str(line_feed=True)
'{0}\n'
'and {1}\n'
'and not {2}\n'
'and {3}'

>>> logic_tree = LogicTree.new_from_str('(({1} and {2} or {3}) and {4} or {5}) and {6} or {7} or {8} and ({9} or {10} and ({11} or {12} and {13}))')
>>> logic_tree.to_str(line_feed=False)
'(({1} and {2} or {3}) and {4} or {5}) and {6} or {7} or {8} and ({9} or {10} and ({11} or {12} and {13}))'
>>> logic_tree.to_str(line_feed=True)
'    (\n'
'            (\n'
'                    {1}\n'
'                    and {2}\n'
'                or {3}\n'
'            )\n'
'            and {4}\n'
'        or {5}\n'
'    )\n'
'    and {6}\n'
'or {7}\n'
'or \n'
'    {8}\n'
'    and (\n'
'        {9}\n'
'        or \n'
'            {10}\n'
'            and (\n'
'                {11}\n'
'                or \n'
'                    {12}\n'
'                    and {13}\n'
'            )\n'
'    )'
  • simplify
    include such as remove empty node, apply de-morgen, single node up
>>> logic_tree = LogicTree.new_from_str('not ({1} and ({2} or not {3}))')
>>> logic_tree.to_str_as_multi_lines()
'not (\n'
'{1}\n'
'and (\n'
'     {2}\n'
'     or not {3}\n'
'     )\n'
')'
>>> logic_tree.de_morgan(recursive=True).to_str_as_multi_lines()
'not {1}\n'
'or \n'
'    not {2}\n'
'    and {3}'  

>>> logic_tree = LogicTree(
        child_nodes=[
            # ((1))
            LogicTree(child_nodes=[LogicTree(child_nodes=['a11', ],
                                             logic='and',
                                             inverse=False)
                                   ],
                      logic='and',
                      inverse=False),
            # not ((1))
            LogicTree(child_nodes=[LogicTree(child_nodes=['aa11'],
                                             logic='and',
                                             inverse=False)
                                   ],
                      logic='and',
                      inverse=True),
            # ((not 1))
            LogicTree(child_nodes=[LogicTree(child_nodes=['b11', ],
                                             logic='and',
                                             inverse=True)
                                   ],
                      logic='and',
                      inverse=False),
            # not ((not 1))
            LogicTree(child_nodes=[LogicTree(child_nodes=['bb11'],
                                             logic='and',
                                             inverse=True)
                                   ],
                      logic='and',
                      inverse=True),
        ],
        logic='or',
        inverse=False)
>>> logic_tree.to_str_as_multi_lines()
'    {a11}\n'
'or \n'
'    not (\n'
'    {aa11}\n'
'    )\n'
'or \n'
'    not {b11}\n'
'or \n'
'    not (\n'
'    not {bb11}\n'
'    )'
>>> logic_tree.copy(recursive=True).simplify().to_str_as_multi_lines()
'{a11}\n'
'or not {aa11}\n'
'or not {b11}\n'
'or {bb11}' 
  • modify
    include such remove leaf nodes, merge with other
>>> s2 = '(({1} and {2} or {3}) and {1} or {2}) and {3} or {1} or {2} and ({3} or {1} and ({2} or {3} and {0}))'
>>> LogicTree.new_from_str(s2).collect_leaf_values()
{'0': 1, '1': 4, '2': 4, '3': 4}

>>> logic_tree = LogicTree.new_from_str('{1} and {2} and ({1} or {3})')
>>> logic_tree.remove_leaf_values({'1', '2'}).to_str_as_multi_lines()  
'{3}'

>>> logic_tree = LogicTree.new_from_str('{1} and {2}')                                     
>>> logic_tree.copy().merge_leaf_values('3', inverse=True, logic='or').to_str_as_one_line()
'{1} and {2} or not {3}'

>>> LogicTree.new_from_str('not ({1} or {2})').merge_leaf_values('3', '4', logic='or').to_str_as_one_line()
'not ({1} or {2} or not {3} or not {4})'

>>>  LogicTree.new_from_str('{1} or {2}').merge_other(LogicTree.new_from_str('{3} and {4}')).to_str_as_one_line()
'{3} and {4} and ({1} or {2})'

FAQ

  • follow-up plan?
    • depend

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

logictree-1.1.3.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

logictree-1.1.3-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file logictree-1.1.3.tar.gz.

File metadata

  • Download URL: logictree-1.1.3.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for logictree-1.1.3.tar.gz
Algorithm Hash digest
SHA256 20d1f25c357f6c15befe8c69ddd2ee62e43bc415b560cf6371da75597db48c14
MD5 0fdfd3b396275b30cd596e248119f11e
BLAKE2b-256 61e58226bf622d6b5f6541e8b988403f8f05f8f720d0ed07b11b06ef4746f67e

See more details on using hashes here.

File details

Details for the file logictree-1.1.3-py3-none-any.whl.

File metadata

  • Download URL: logictree-1.1.3-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for logictree-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ec4d6e77bf9f62a73ec78a61f534def39c031fa728f4ae5bf8c10c81885d8b91
MD5 f7e8f7e399ec6915997f294656a7d218
BLAKE2b-256 ebe6d8d5c77d66f0b31fc786e37cf7b3039f2523e5880ac49f8f8580735b503f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page