-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
create MongoDB with DMS (data management system) and dot-notation #11
Comments
For the record, attaching MongoDB dump with these collections # connect to Mongo and open the top-level database
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/')['DMS00']
from rich.pretty import pprint
import pymongo # maps Collection.attribute to another Collection in the same DB (lookup by "id")
structs={
'Beam.cs':'CrossSection',
'CrossSection.rve':'ConcreteRVE',
'ConcreteRVE.ct':'CTScan',
'ConcreteRVE.materials':'MaterialRecord',
'BeamState.beam':'Beam',
'BeamState.csState':'CrossSectionState',
'CrossSectionState.rveStates':'ConcreteRVEState',
'CrossSectionState.cs':'CrossSection',
'ConcreteRVEState.rve':'ConcreteRVE'
}
import typing
import dataclasses
@dataclasses.dataclass
class DotNode:
what: str
param: str
value: typing.Any
import ast
class MupifObjectTraversal(ast.NodeVisitor):
def __init__(self,db,expr):
self.tree=ast.parse(expr,filename='<mupif-expr>')
self.seq=[]
self.val=None
self.db=self.curr=db
def __call__(self):
self.generic_visit(self.tree)
return self.seq
def visit(self,node):
if isinstance(node,ast.Load): return # ignored
allowed=[ast.Expr,ast.Attribute,ast.Name,ast.Subscript,ast.Load,ast.Constant]
if node.__class__ not in allowed: raise SyntaxError(f'Node {node.__class__.__name__} not allowed (only {",".join([c.__name__ for c in allowed])} are)')
return super().visit(node)
def visit_Subscript(self,node):
'bracketed subscript'
self.generic_visit(node)
assert isinstance(self.curr,pymongo.collection.Collection)
self.val=self.curr.find_one({'id':node.slice.value})
if self.val is None: raise IndexError(f'Invalid id {node.slice.value} (not found in {self.curr})')
self.seq.append(DotNode(node.__class__.__name__.upper(),node.slice.value,self.val))
def visit_Attribute(self,node):
'dotted attribute'
self.generic_visit(node)
# is a pointer to another database
if (k:=f'{self.curr.name}.{node.attr}') in structs: self.curr=self.db[structs[k]]
# is a terminal value
else: self.curr=self.val[node.attr]
self.seq.append(DotNode(node.__class__.__name__.upper(),node.attr,self.curr))
def visit_Name(self,node):
'top named object'
self.generic_visit(node)
self.curr=self.curr[node.id]
self.seq.append(DotNode(node.__class__.__name__.upper(),node.id,self.curr))
pprint(MupifObjectTraversal(db=client,expr='BeamState[0].beam.cs.rve.ct.id')())
pprint(MupifObjectTraversal(db=client,expr='BeamState[0].csState[0].rveStates[1].rve.ct.id')())
pprint(MupifObjectTraversal(db=client,expr='BeamState[0].csState[0].rveStates[1].rve.materials[0].name')()) [ │ DotNode( │ │ what='NAME', │ │ param='BeamState', │ │ value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'BeamState') │ ), │ DotNode( │ │ what='SUBSCRIPT', │ │ param=0, │ │ value={ │ │ │ '_id': ObjectId('62e9145b9bb766a61709416b'), │ │ │ 'id': 0, │ │ │ 'npointz': 1, │ │ │ 'csState': [0, '1'], │ │ │ 'w': [] │ │ } │ ), │ DotNode( │ │ what='ATTRIBUTE', │ │ param='beam', │ │ value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'Beam') │ ), │ DotNode( │ │ what='ATTRIBUTE', │ │ param='cs', │ │ value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'CrossSection') │ ), │ DotNode( │ │ what='ATTRIBUTE', │ │ param='rve', │ │ value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'ConcreteRVE') │ ), │ DotNode( │ │ what='ATTRIBUTE', │ │ param='ct', │ │ value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'CTScan') │ ), │ DotNode(what='ATTRIBUTE', param='id', value=0) ] [ │ DotNode( │ │ what='NAME', │ │ param='BeamState', │ │ value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'BeamState') │ ), │ DotNode( │ │ what='SUBSCRIPT', │ │ param=0, │ │ value={ │ │ │ '_id': ObjectId('62e9145b9bb766a61709416b'), │ │ │ 'id': 0, │ │ │ 'npointz': 1, │ │ │ 'csState': [0, '1'], │ │ │ 'w': [] │ │ } │ ), │ DotNode( │ │ what='ATTRIBUTE', │ │ param='csState', │ │ value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'CrossSectionState') │ ), │ DotNode( │ │ what='SUBSCRIPT', │ │ param=0, │ │ value={ │ │ │ '_id': ObjectId('62e969d69bb766a6170941bd'), │ │ │ 'id': 0, │ │ │ 'cs': 0, │ │ │ 'rveStates': [0, 1], │ │ │ 'BendingMoment': [0, 0], │ │ │ 'kappa': [1, 1], │ │ │ 'eps_axial': [2, 2, 2] │ │ } │ ), │ DotNode( │ │ what='ATTRIBUTE', │ │ param='rveStates', │ │ value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'ConcreteRVEState') │ ), │ DotNode( │ │ what='SUBSCRIPT', │ │ param=1, │ │ value={ │ │ │ '_id': ObjectId('62e969c39bb766a6170941b2'), │ │ │ 'id': 1, │ │ │ 'rve': 1, │ │ │ 'sigmaHom': [1, 1, 1], │ │ │ 'epsHom': [1, 1, 1], │ │ │ 'Stiffness': [11, 12, 13, 14, 15, 16, 17, 18, 19], │ │ │ 'eps0Hom': [1, 1, 1] │ │ } │ ), │ DotNode( │ │ what='ATTRIBUTE', │ │ param='rve', │ │ value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'ConcreteRVE') │ ), │ DotNode( │ │ what='ATTRIBUTE', │ │ param='ct', │ │ value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'CTScan') │ ), │ DotNode(what='ATTRIBUTE', param='id', value=1) ] [ │ DotNode( │ │ what='NAME', │ │ param='BeamState', │ │ value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'BeamState') │ ), │ DotNode( │ │ what='SUBSCRIPT', │ │ param=0, │ │ value={ │ │ │ '_id': ObjectId('62e9145b9bb766a61709416b'), │ │ │ 'id': 0, │ │ │ 'npointz': 1, │ │ │ 'csState': [0, '1'], │ │ │ 'w': [] │ │ } │ ), │ DotNode( │ │ what='ATTRIBUTE', │ │ param='csState', │ │ value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'CrossSectionState') │ ), │ DotNode( │ │ what='SUBSCRIPT', │ │ param=0, │ │ value={ │ │ │ '_id': ObjectId('62e969d69bb766a6170941bd'), │ │ │ 'id': 0, │ │ │ 'cs': 0, │ │ │ 'rveStates': [0, 1], │ │ │ 'BendingMoment': [0, 0], │ │ │ 'kappa': [1, 1], │ │ │ 'eps_axial': [2, 2, 2] │ │ } │ ), │ DotNode( │ │ what='ATTRIBUTE', │ │ param='rveStates', │ │ value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'ConcreteRVEState') │ ), │ DotNode( │ │ what='SUBSCRIPT', │ │ param=1, │ │ value={ │ │ │ '_id': ObjectId('62e969c39bb766a6170941b2'), │ │ │ 'id': 1, │ │ │ 'rve': 1, │ │ │ 'sigmaHom': [1, 1, 1], │ │ │ 'epsHom': [1, 1, 1], │ │ │ 'Stiffness': [11, 12, 13, 14, 15, 16, 17, 18, 19], │ │ │ 'eps0Hom': [1, 1, 1] │ │ } │ ), │ DotNode( │ │ what='ATTRIBUTE', │ │ param='rve', │ │ value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'ConcreteRVE') │ ), │ DotNode( │ │ what='ATTRIBUTE', │ │ param='materials', │ │ value=Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'DMS00'), 'MaterialRecord') │ ), │ DotNode( │ │ what='SUBSCRIPT', │ │ param=0, │ │ value={ │ │ │ '_id': ObjectId('62e914fb9bb766a617094171'), │ │ │ 'id': 0, │ │ │ 'name': 'material0', │ │ │ 'type': None, │ │ │ 'props': {'manufacturer': 'Unemployed Philosophers Guild'} │ │ } │ ), │ DotNode(what='ATTRIBUTE', param='name', value='material0') ] |
The beanie module might be useful for defining the data structure: as pydantic classes, beanie interfacing with MongoDB. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: