Files
secondo/apis/python2/SecondoAPI/.ipynb_checkpoints/PRelation-checkpoint.ipynb

152 lines
5.0 KiB
Plaintext
Raw Permalink Normal View History

2026-01-23 17:03:45 +08:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import import_ipynb\n",
"from PGenType import *\n",
"#from TypeIndex import *\n",
"from PTuple import *\n",
"from Rel_Attrib import *\n",
"#from PGenObjects import *\n",
"\n",
"#import mmdb.data.indices.MemoryIndex.IndexType;\n",
"#import mmdb.error.convert.ConvertToObjectException;\n",
"#import mmdb.error.index.IndexingException;\n",
"#import mmdb.error.memory.MemoryException;\n",
"\n",
"\n",
"class PRelation():\n",
" #header consists of objects of type Rel_Attrib class\n",
" def __init__(self, header = None):\n",
" self.header = []\n",
" if header is not None:\n",
" self.header = header\n",
" self.tuples = []\n",
" self.indices = {}\n",
"\n",
" \n",
" #Function creates a single Tuple from header and a NestedList and adds it to list of Tuples\n",
" #header: a list of Rel_Attribs\n",
" #NestedList: containing the values of attributes in each Rel_Attrib of header\n",
" def createTupleFromList(self, NList):\n",
" tmp_NList = NList\n",
" new_tuple = PTuple()\n",
" for idx, Rel_attrib in enumerate(self.header):\n",
" att_type = Rel_Attrib.getTypeClass()\n",
" assert att_type is not None, \"Type is not supported!\"\n",
" cls = att_type.__class__.__name__\n",
" attribute = cls()\n",
" attribute.InFromList(tmp_NList)\n",
" new_tuple.add_Attribute(attribute);\n",
" tmp_NList = tmp_NList[1:]\n",
" #if defined is to be read from NList then: tmp_NList = tmp_NList[2:]\n",
" self.tuples.append(new_tuple)\n",
"\n",
"\n",
" def get_Tuples(self):\n",
" return self.tuples\n",
"\n",
" def set_Tuples(self, tuples):\n",
" self.tuples = tuples\n",
"\n",
" ##### Function must be modified after reviewing TypeIndex module\n",
" def create_Index(self, identifier, indexType):\n",
" index = TypeIndex.TypeIndex()\n",
" cls = index.index_Type.valueOf(indexType)\n",
" indexClass = globals()[cls]\n",
" assert not(indexClass is None), \"IndexType not found and instantiating the index class failed!\"\n",
" #index = TypeIndex.TypeIndex()\n",
" att = get_att_index_type(identifier)\n",
" index.create(att[0], self)\n",
" self.indices.update(identifier, index)\n",
"\n",
" def get_Indices(self):\n",
" return self.indices\n",
"\n",
" def get_Index(self, identifier):\n",
" return self.indices.get(identifier)\n",
"\n",
" def remove_Index(self, identifier):\n",
" del self.indices[identifier]\n",
"\n",
"\n",
" def getHeader(self):\n",
" return self.header\n",
"\n",
" #usage a, b = get_att_index_type(identifier)\n",
" def get_att_index_type(self, identifier):\n",
" for idx, att in enumerate(self.header):\n",
" if att.get_Identifier() == identifier:\n",
" return idx, att.getTypeName()\n",
"\n",
" def create_Instance(self, header):\n",
" rel = PRelation(header)\n",
" return rel\n",
"\n",
"\n",
" def get_rel_header(self):\n",
" return self.header\n",
"\n",
" def __eq__(self, obj):\n",
" if obj == self:\n",
" return True\n",
" if obj is None:\n",
" return False\n",
" if not isinstance(obj, PRelation):\n",
" return False\n",
" other = PRelation()\n",
" othet = obj\n",
" if not other.get_Header() == self.header:\n",
" return False\n",
" if not other.get_Tuples() == self.tuples:\n",
" return False\n",
" return True\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#b.__class__.__name__\n",
"#when the class is in the same module/dir\n",
"#def factory(classname):\n",
" # cls = globals()[classname]\n",
" # return cls()\n",
"\n",
"#when the class is in other module/dir\n",
"#def factory(classname):\n",
" # from myproject import mymodule\n",
" # cls = getattr(mymodule, classname)\n",
" # return cls()\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
}