195 lines
7.1 KiB
Plaintext
195 lines
7.1 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"########## Nested List manipulation functions ############\n",
|
|
"\n",
|
|
"\n",
|
|
"#returns a double quoted string for types string and text but symbols remain with single quotation mark\n",
|
|
"class dbl_quot(str):\n",
|
|
" def __repr__(self):\n",
|
|
" return ''.join(('\"', super().__repr__()[1:-1], '\"'))\n",
|
|
"\n",
|
|
"#updates a specific item in a nested list with unknown depth and structure to another given value\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",
|
|
"\n",
|
|
"#modifying the text items of form '****' in a nested list to <text>****</text---> form\n",
|
|
"def change_textform(seq):\n",
|
|
" for i, item in enumerate(seq):\n",
|
|
" #if type(item) != list and seq[i] and seq[i] != '<text>' and item[0] == \"'\" and item[1] != \"(\":\n",
|
|
" # seq[i] = \"<text>\" + item[1:]\n",
|
|
" # j = i+1 \n",
|
|
" # while True: \n",
|
|
" \n",
|
|
" # seq[i] += seq[j]\n",
|
|
" # del seq[j] \n",
|
|
" # \n",
|
|
" # if seq[j] == \"'\":\n",
|
|
" # seq[i] += \"</text--->\"\n",
|
|
" # del seq[j]\n",
|
|
" # break\n",
|
|
" #elif:\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_textform(item)\n",
|
|
" \n",
|
|
" return seq\n",
|
|
"\n",
|
|
"\n",
|
|
"#converts a nested list with unknown depth and structure to a flat list\n",
|
|
"def flatten_list(lisst):\n",
|
|
" lst = []\n",
|
|
" for i in lisst:\n",
|
|
" if isinstance (i, list):\n",
|
|
" lst.extend (flatten_list (i))\n",
|
|
" else:\n",
|
|
" lst.append (i)\n",
|
|
" return lst \n",
|
|
"\n",
|
|
"\n",
|
|
"#returns index of first appearance of item in a nested list with unknown depth and structure\n",
|
|
"def nested_index(lst, target):\n",
|
|
" for index, item in enumerate(lst):\n",
|
|
" if item == target:\n",
|
|
" return [index]\n",
|
|
" if isinstance(item, (list, tuple)):\n",
|
|
" path = nested_index(item, target)\n",
|
|
" if path:\n",
|
|
" return [index] + path\n",
|
|
" return []\n",
|
|
"\n",
|
|
"\n",
|
|
"#indices=[]\n",
|
|
"#for item in flattend_list:\n",
|
|
"# indices.append(nested_index(nestedlist,item))\n",
|
|
"#print(indices)\n",
|
|
"\n",
|
|
"#recognizing the items with types boolean and numeric types in a nested list and convert them to equivalent types in python\n",
|
|
"def NL_itemsTypeCheck(NL):\n",
|
|
" flattend_list = flatten_list(NL)\n",
|
|
" #print (flattend_list)\n",
|
|
" for item in flattend_list:\n",
|
|
" \n",
|
|
" #if not item.startswith('\"') and not item.startswith('<text>') and not item.startswith('</text--->') and item != ' ' and item.lower() != 'from' and item[-1] != ':' and item[-1] != '.' and item.lower() != \"is\":\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",
|
|
" #print (NL)\n",
|
|
" return(NL)\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"#Parsing Secondo_NestedList to a Python_Nestedlist\n",
|
|
"\n",
|
|
"\n",
|
|
"from pyparsing import *\n",
|
|
"import pprint\n",
|
|
"import re\n",
|
|
"\n",
|
|
"def NLparse(content, ftype): #ftype = True: file as argument, ftype = False: string as argument\n",
|
|
" #ident = Word(alphas, alphanums + \"-_.\")\n",
|
|
" #number = Word('-'+'+'+nums+'.')\n",
|
|
" #nestedParens = nestedExpr('(', ')', content=enclosed) \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",
|
|
" print(NLresult[0]) \n",
|
|
" NLresult = change_textform(NLresult[0])\n",
|
|
" NLresult = NL_itemsTypeCheck(NLresult)\n",
|
|
" print('result of NLParser: ')\n",
|
|
" print(NLresult)\n",
|
|
" return NLresult\n",
|
|
"\n",
|
|
" \n",
|
|
" \n",
|
|
"#converts the python nested list to a string with nested parenthesis\n",
|
|
"def NLtoString(NL):\n",
|
|
" strresult = pprint.pformat(NL)\n",
|
|
" replacements = {\"[\": \"(\", \"]\": \")\", \"'\": \"\"}\n",
|
|
" strresult = \"\".join([replacements.get(c, c) for c in strresult])\n",
|
|
" #print(strresult)\n",
|
|
" return strresult\n",
|
|
"\n",
|
|
"\n",
|
|
"#NL=NLparse(\"opt.txt\", True)\n",
|
|
"#NL=NLparse(\"(restore database berlintest from berlintest)\", False)\n",
|
|
"#print(NL)\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\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
|
|
}
|