Last active
July 21, 2017 16:39
-
-
Save kain88-de/9728e5b76082d9c3336ce840cb3195b5 to your computer and use it in GitHub Desktop.
datreant tag selection parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import datreant.core as dtr\n", | |
"import os" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"for treetype in ('birch', 'oak'):\n", | |
" for branch in ('first', 'random'):\n", | |
" for sel in ('all', 'wood', 'core'):\n", | |
" folder = os.path.join('forest', '{}_{}_{}'.format(treetype, branch, sel))\n", | |
" tree = dtr.Treant(folder)\n", | |
" tree.tags = [treetype, branch, sel]\n", | |
" folder = os.path.join('forest', '{}_{}'.format(treetype, branch))\n", | |
" tree = dtr.Treant(folder)\n", | |
" tree.tags = [treetype, branch]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"bundle = dtr.discover('forest/')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"birch_first\t birch_random\t oak_first\t oak_random\r\n", | |
"birch_first_all birch_random_all oak_first_all oak_random_all\r\n", | |
"birch_first_core birch_random_core oak_first_core oak_random_core\r\n", | |
"birch_first_wood birch_random_wood oak_first_wood oak_random_wood\r\n" | |
] | |
} | |
], | |
"source": [ | |
"!ls forest" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"/home/max/Academia/Projects/datreant-pyparsing/forest/oak_first\n", | |
"/home/max/Academia/Projects/datreant-pyparsing/forest/oak_random\n", | |
"/home/max/Academia/Projects/datreant-pyparsing/forest/oak_random_core\n", | |
"/home/max/Academia/Projects/datreant-pyparsing/forest/oak_first_core\n" | |
] | |
} | |
], | |
"source": [ | |
"treants = bundle[bundle.tags[['oak', {'all'}, {'wood'}]]]\n", | |
"\n", | |
"for t in treants:\n", | |
" print(t)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from pyparsing import (CaselessLiteral, Word, alphas, quotedString,\n", | |
" removeQuotes, operatorPrecedence, opAssoc, stringEnd)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"class UnaryOperation(object):\n", | |
" def __init__(self, t):\n", | |
" self.op, self.a = t[0]\n", | |
"\n", | |
"\n", | |
"class BinaryOperation(object):\n", | |
" def __init__(self, t):\n", | |
" self.op = t[0][1]\n", | |
" self.operands = t[0][0::2]\n", | |
"\n", | |
"\n", | |
"class SearchAnd(BinaryOperation):\n", | |
" def generateTagExpression(self):\n", | |
" return list(oper.generateTagExpression() for oper in self.operands)\n", | |
"\n", | |
" def __repr__(self):\n", | |
" return \"AND:(%s)\" % (\",\".join(str(oper) for oper in self.operands))\n", | |
"\n", | |
"\n", | |
"class SearchOr(BinaryOperation):\n", | |
" def generateTagExpression(self):\n", | |
" return tuple(oper.generateTagExpression() for oper in self.operands)\n", | |
"\n", | |
"\n", | |
" def __repr__(self):\n", | |
" return \"OR:(%s)\" % (\",\".join(str(oper) for oper in self.operands))\n", | |
"\n", | |
"\n", | |
"class SearchNot(UnaryOperation):\n", | |
" def generateTagExpression(self):\n", | |
" exps = self.a.generateTagExpression()\n", | |
" if isinstance(exps, list):\n", | |
" return [{e} for e in exps]\n", | |
" elif isinstance(exps, tuple):\n", | |
" return tuple({e} for e in exps)\n", | |
" else:\n", | |
" return {self.a.generateTagExpression()}\n", | |
"\n", | |
" def __repr__(self):\n", | |
" return \"NOT:(%s)\" % str(self.a)\n", | |
"\n", | |
"\n", | |
"class SearchTerm(object):\n", | |
" def __init__(self, tokens):\n", | |
" self.term = tokens[0]\n", | |
"\n", | |
" def generateTagExpression(self):\n", | |
" return \"{}\".format(self.term)\n", | |
"\n", | |
" def __repr__(self):\n", | |
" return self.term\n", | |
"\n", | |
"\n", | |
"# define the grammar\n", | |
"and_ = CaselessLiteral(\"and\")\n", | |
"or_ = CaselessLiteral(\"or\")\n", | |
"not_ = CaselessLiteral(\"not\")\n", | |
"searchTerm = Word(alphas) | quotedString.setParseAction(removeQuotes)\n", | |
"searchTerm.setParseAction(SearchTerm)\n", | |
"searchExpr = operatorPrecedence(searchTerm, [\n", | |
" (not_, 1, opAssoc.RIGHT, SearchNot),\n", | |
" (and_, 2, opAssoc.LEFT, SearchAnd),\n", | |
" (or_, 2, opAssoc.LEFT, SearchOr),\n", | |
"])\n", | |
"Parser = searchExpr + stringEnd" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"AND:(oak,NOT:(AND:(all,wood)))\n", | |
"['oak', [{'all'}, {'wood'}]]\n", | |
"/home/max/Academia/Projects/datreant-pyparsing/forest/oak_first\n", | |
"/home/max/Academia/Projects/datreant-pyparsing/forest/oak_random\n", | |
"/home/max/Academia/Projects/datreant-pyparsing/forest/oak_random_core\n", | |
"/home/max/Academia/Projects/datreant-pyparsing/forest/oak_first_core\n" | |
] | |
} | |
], | |
"source": [ | |
"sle_str = 'oak and not all and not wood'\n", | |
"sel_str = 'not (all and wood) and oak'\n", | |
"sel_str = 'oak and not (all and wood)'\n", | |
"\n", | |
"\n", | |
"sel_ast = Parser.parseString(sel_str)[0]\n", | |
"print(sel_ast)\n", | |
"sel = sel_ast.generateTagExpression()\n", | |
"print(sel)\n", | |
"\n", | |
"#treants = bundle[bundle.tags[['oak', {'all'}, {'wood'}]]]\n", | |
"treants = bundle[bundle.tags[sel]]\n", | |
"\n", | |
"for t in treants:\n", | |
" print(t)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"AND:(oak,OR:(all,wood))\n", | |
"['oak', ('all', 'wood')]\n", | |
"/home/max/Academia/Projects/datreant-pyparsing/forest/oak_random_all\n", | |
"/home/max/Academia/Projects/datreant-pyparsing/forest/oak_first_wood\n", | |
"/home/max/Academia/Projects/datreant-pyparsing/forest/oak_random_wood\n", | |
"/home/max/Academia/Projects/datreant-pyparsing/forest/oak_first_all\n" | |
] | |
} | |
], | |
"source": [ | |
"sle_str = 'oak and not all and not wood'\n", | |
"sel_str = 'not (all and wood) and oak'\n", | |
"#sel_str = 'oak and not (all and wood)'\n", | |
"sel_str = 'oak and (all or wood)'\n", | |
"\n", | |
"\n", | |
"sel_ast = Parser.parseString(sel_str)[0]\n", | |
"print(sel_ast)\n", | |
"sel = sel_ast.generateTagExpression()\n", | |
"print(sel)\n", | |
"\n", | |
"#treants = bundle[bundle.tags[['oak', {'all'}, {'wood'}]]]\n", | |
"treants = bundle[bundle.tags[sel]]\n", | |
"\n", | |
"for t in treants:\n", | |
" print(t)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python [default]", | |
"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.6.1" | |
}, | |
"toc": { | |
"colors": { | |
"hover_highlight": "#DAA520", | |
"navigate_num": "#000000", | |
"navigate_text": "#333333", | |
"running_highlight": "#FF0000", | |
"selected_highlight": "#FFD700", | |
"sidebar_border": "#EEEEEE", | |
"wrapper_background": "#FFFFFF" | |
}, | |
"moveMenuLeft": true, | |
"nav_menu": { | |
"height": "12px", | |
"width": "252px" | |
}, | |
"navigate_menu": true, | |
"number_sections": true, | |
"sideBar": true, | |
"threshold": 4, | |
"toc_cell": false, | |
"toc_section_display": "block", | |
"toc_window_display": false, | |
"widenNotebook": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment