237 lines
8.6 KiB
Plaintext
237 lines
8.6 KiB
Plaintext
|
|
{
|
||
|
|
"cells": [
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": null,
|
||
|
|
"metadata": {},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"\n",
|
||
|
|
"\"\"\"\n",
|
||
|
|
"The module nested_list contains functions for parsing and manipulating the nested lists.\n",
|
||
|
|
"\"\"\"\n",
|
||
|
|
"from pyparsing import *\n",
|
||
|
|
"import ast\n",
|
||
|
|
"import pprint\n",
|
||
|
|
"import re\n",
|
||
|
|
"\n",
|
||
|
|
"\n",
|
||
|
|
"\"\"\"\n",
|
||
|
|
"The class dbl_quot returns a double quoted string while the default in python is single quotation mark for strings.\n",
|
||
|
|
"\"\"\"\n",
|
||
|
|
"class dbl_quot(str):\n",
|
||
|
|
" def __repr__(self):\n",
|
||
|
|
" return ''.join(('\"', super().__repr__()[1:-1], '\"'))\n",
|
||
|
|
" \n",
|
||
|
|
"\n",
|
||
|
|
"#old\n",
|
||
|
|
"def change(seq, what, make):\n",
|
||
|
|
" for i, item in enumerate(seq):\n",
|
||
|
|
" if item == what:\n",
|
||
|
|
" seq[i] = make\n",
|
||
|
|
" elif type(item) == list:\n",
|
||
|
|
" change(item, what, make)\n",
|
||
|
|
" return seq\n",
|
||
|
|
"\n",
|
||
|
|
"#new\n",
|
||
|
|
"def change_nl_item_val(seq, what, make):\n",
|
||
|
|
" \"\"\"\n",
|
||
|
|
" The function change_nl_item_val updates the value of a specific item in a nested list with \n",
|
||
|
|
" unknown depth and structure to another given value.\n",
|
||
|
|
"\n",
|
||
|
|
" :param seq: An iterable data structure like nested list.\n",
|
||
|
|
" :param what: The value that should be modified.\n",
|
||
|
|
" :param make: The vlue that should replace the original value.\n",
|
||
|
|
" :return: The modified iterable.\n",
|
||
|
|
" \"\"\"\n",
|
||
|
|
" for i, item in enumerate(seq):\n",
|
||
|
|
" \n",
|
||
|
|
" if type(item) != list and item == what:\n",
|
||
|
|
" seq[i] = make\n",
|
||
|
|
" elif type(item) == list:\n",
|
||
|
|
" change_nl_item_val(item, what, make)\n",
|
||
|
|
" return seq\n",
|
||
|
|
"\n",
|
||
|
|
"\n",
|
||
|
|
"\n",
|
||
|
|
"def change_nl_text_form(seq):\n",
|
||
|
|
" \"\"\"\n",
|
||
|
|
" The function change_nl_text_form() modifies the value of items of type text in a nested list\n",
|
||
|
|
" with form '****' to <text>****</text---> form.\n",
|
||
|
|
"\n",
|
||
|
|
" :param seq: An iterable data structure like nested list.\n",
|
||
|
|
" :return: The modified iterable.\n",
|
||
|
|
" \"\"\"\n",
|
||
|
|
" for i, item in enumerate(seq):\n",
|
||
|
|
" if seq[i] and seq[i] != '<text>' and item[0] == \"'\" and item[1] == \"(\":\n",
|
||
|
|
" seq[i] = \"<text>\" + item[1:-1] + \"</text--->\"\n",
|
||
|
|
" \n",
|
||
|
|
" elif seq[i] and seq[i] == '<text>':\n",
|
||
|
|
" item = [\"<text>\",seq[i+1],\"</text--->\"] \n",
|
||
|
|
" seq[i] = item\n",
|
||
|
|
" del seq[i+1], seq[i+1]\n",
|
||
|
|
" \n",
|
||
|
|
" elif seq[i] and type(item) == list:\n",
|
||
|
|
" change_nl_text_form(item)\n",
|
||
|
|
" \n",
|
||
|
|
" return seq\n",
|
||
|
|
"\n",
|
||
|
|
"\n",
|
||
|
|
"#old\n",
|
||
|
|
"def NL_items_type_Check(NL):\n",
|
||
|
|
" flattend_list = flatten_list(NL)\n",
|
||
|
|
" for item in flattend_list:\n",
|
||
|
|
" \n",
|
||
|
|
" if item.isdigit() or item in ('True', 'False'):\n",
|
||
|
|
" try:\n",
|
||
|
|
" if type(eval(item)) in(int, float, bool):\n",
|
||
|
|
" change(NL, item, ast.literal_eval(item))\n",
|
||
|
|
" except (ValueError, NameError):\n",
|
||
|
|
" pass\n",
|
||
|
|
" return(NL)\n",
|
||
|
|
"\n",
|
||
|
|
"\n",
|
||
|
|
"#new\n",
|
||
|
|
"def change_nl_item_type(seq):\n",
|
||
|
|
" \"\"\"\n",
|
||
|
|
" The function change_nl_item_type() modifies the type of string items in a nested list\n",
|
||
|
|
" to proper types in python according to their values.\n",
|
||
|
|
"\n",
|
||
|
|
" :param seq: An iterable data structure like nested list.\n",
|
||
|
|
" :return: The modified iterable.\n",
|
||
|
|
" \"\"\"\n",
|
||
|
|
" \n",
|
||
|
|
" for i, item in enumerate(seq):\n",
|
||
|
|
" try:\n",
|
||
|
|
" if type(item) != list and (item.isdigit() or item in ('True', 'False') or float(item)):\n",
|
||
|
|
" \n",
|
||
|
|
" if type(eval(item)) in(int, float, bool):\n",
|
||
|
|
" seq[i] = ast.literal_eval(item)\n",
|
||
|
|
" elif type(item) == list:\n",
|
||
|
|
" change_nl_item_type(item)\n",
|
||
|
|
" except (ValueError, NameError):\n",
|
||
|
|
" pass \n",
|
||
|
|
" return seq\n",
|
||
|
|
"\n",
|
||
|
|
"\n",
|
||
|
|
"\n",
|
||
|
|
"def item_type(item):\n",
|
||
|
|
" \"\"\"\n",
|
||
|
|
" The function item_type() detect the type of an item in a pythonic list and maps it to a secondo data type.\n",
|
||
|
|
"\n",
|
||
|
|
" :param item: An elemt in an iterable like a nested list.\n",
|
||
|
|
" :return: The relevant data type in Secondo nested lists as a string.\n",
|
||
|
|
" \"\"\"\n",
|
||
|
|
" \n",
|
||
|
|
" if isinstance(item, str) and item[0] == '\"':\n",
|
||
|
|
" return \"string\"\n",
|
||
|
|
" if isinstance(item, str) and item[0] != '\"' and item[0] != '<':\n",
|
||
|
|
" return \"symbol\"\n",
|
||
|
|
" if isinstance(item, int):\n",
|
||
|
|
" return \"integer\"\n",
|
||
|
|
" if isinstance(item, float):\n",
|
||
|
|
" return \"real\"\n",
|
||
|
|
" if isinstance(item, bool):\n",
|
||
|
|
" return \"boolean\"\n",
|
||
|
|
" if (isinstance(item, list) and item[0] == \"<text>\") or (isinstance(item, str) and item.startswith('<text>')):\n",
|
||
|
|
" return \"text\"\n",
|
||
|
|
" if isinstance(item, list):\n",
|
||
|
|
" return \"list\"\n",
|
||
|
|
" \n",
|
||
|
|
"\n",
|
||
|
|
"\n",
|
||
|
|
"def nl_parse(content, ftype): #ftype = True: file as argument, ftype = False: string as argument\n",
|
||
|
|
" \n",
|
||
|
|
" \"\"\"\n",
|
||
|
|
" The function nl_parse() parses a Secondo NestedList to a Python Nestedlist.\n",
|
||
|
|
"\n",
|
||
|
|
" :param content: The nested list to be parsed.\n",
|
||
|
|
" :param ftype: A boolean value which tells if the first parameter is a string containing the nested list(False) or a file name(True).\n",
|
||
|
|
" :return: The python nested list.\n",
|
||
|
|
" \"\"\"\n",
|
||
|
|
" #ident = Word(alphas, alphanums + \"-_.\")\n",
|
||
|
|
" #number = Word('-'+'+'+nums+'.')\n",
|
||
|
|
" #nestedParens = nestedExpr('(', ')', content=enclosed) \n",
|
||
|
|
" \n",
|
||
|
|
" enclosed = Forward()\n",
|
||
|
|
" nestedParens = nestedExpr('(', ')')\n",
|
||
|
|
" enclosed << (OneOrMore(nestedParens))\n",
|
||
|
|
" if ftype:\n",
|
||
|
|
" try:\n",
|
||
|
|
" with open(content, encoding=\"utf8\", errors='ignore') as content_file:\n",
|
||
|
|
" \n",
|
||
|
|
" content = content_file.read()\n",
|
||
|
|
" if not content.startswith('('):\n",
|
||
|
|
" content = '(' + content + ')'\n",
|
||
|
|
" NLresult = enclosed.parseString(content).asList()\n",
|
||
|
|
" \n",
|
||
|
|
" except (ParseException, ParseFatalException) as err:\n",
|
||
|
|
" NLresult = []\n",
|
||
|
|
" print(err)\n",
|
||
|
|
" print(\"Invalid syntax at line {}, column {}: '{}': {}.\".format(err.lineno,err.column,err.markInputline(),err.msg))\n",
|
||
|
|
"\n",
|
||
|
|
" else:\n",
|
||
|
|
" \n",
|
||
|
|
" try:\n",
|
||
|
|
" if not content.startswith('('):\n",
|
||
|
|
" content = '(' + content + ')'\n",
|
||
|
|
" NLresult = enclosed.parseString(content).asList()\n",
|
||
|
|
" \n",
|
||
|
|
" except (ParseException, ParseFatalException) as err:\n",
|
||
|
|
" NLresult = []\n",
|
||
|
|
" print(err)\n",
|
||
|
|
" print(\"Invalid syntax at line {}, column {}: '{}': {}.\".format(err.lineno,err.column,err.markInputline(),err.msg))\n",
|
||
|
|
" \n",
|
||
|
|
" NLresult = change_nl_text_form(NLresult[0])\n",
|
||
|
|
" NLresult = change_nl_item_type(NLresult)\n",
|
||
|
|
" return NLresult\n",
|
||
|
|
"\n",
|
||
|
|
" \n",
|
||
|
|
"def list_to_nl(NL):\n",
|
||
|
|
" \"\"\"\n",
|
||
|
|
" The function list_to_nl() converts a pythonic nested list to a textual nestedlist with nested parenthesis\n",
|
||
|
|
" that matches the nested list format in Secondo.\n",
|
||
|
|
"\n",
|
||
|
|
" :param NL: The nested list to be changed.\n",
|
||
|
|
" :return: The Secondo nested list.\n",
|
||
|
|
" \"\"\"\n",
|
||
|
|
" strresult = pprint.pformat(NL)\n",
|
||
|
|
" replacements = {\"[\": \"(\", \"]\": \")\", \",\": \"\", \"'\": '\"'}\n",
|
||
|
|
" strresult = \"\".join([replacements.get(c, c) for c in strresult])\n",
|
||
|
|
" return strresult\n",
|
||
|
|
"\n"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": null,
|
||
|
|
"metadata": {},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"#l = ['1',['2','0.1'],'1',['5','1',['>-(.hi)_</.','11.9876',['56','1',['True','1']]]],'8']\n"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"metadata": {
|
||
|
|
"kernelspec": {
|
||
|
|
"display_name": "Python 3",
|
||
|
|
"language": "python",
|
||
|
|
"name": "python3"
|
||
|
|
},
|
||
|
|
"language_info": {
|
||
|
|
"codemirror_mode": {
|
||
|
|
"name": "ipython",
|
||
|
|
"version": 3
|
||
|
|
},
|
||
|
|
"file_extension": ".py",
|
||
|
|
"mimetype": "text/x-python",
|
||
|
|
"name": "python",
|
||
|
|
"nbconvert_exporter": "python",
|
||
|
|
"pygments_lexer": "ipython3",
|
||
|
|
"version": "3.7.4"
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"nbformat": 4,
|
||
|
|
"nbformat_minor": 2
|
||
|
|
}
|