1. March Liu
  2. Socrates

Commits

March Liu  committed bf9a729

something error, storage and find the bug

  • Participants
  • Parent commits a21f752
  • Branches default
  • Tags find code error

Comments (0)

Files changed (2)

File src/socrates/core.py

View file
  • Ignore whitespace
 
     def writeSubject(self, subject_id, segments, **pairs):
         """
-        此版本不保证操作的原子性,亦未做性能调优。
 >>> storage = Storage("sqlite://", create=True)
 >>> pid = storage.newPredicate('kssoid', 'string')
 >>> sid = storage.writeSubject(500, {'kssoid':'liuxin2'})
 
     def writeSegment(self, subject_id, predicateExpr, obj, autocommit=True):
         """
-存在复合类型的谓词时,如果没有有指定宾语类型,应该抛出错误,当前
-只是简单的给出第一个匹配顶,这是一种不严谨的方式,以后会改掉。
 >>> from sqlalchemy import DateTime
 >>> storage = Storage("sqlite://", create=True)
 >>> retype = storage.registeType("datetime", colType=DateTime, create=True)
             sid = subject_id
         else:
             sid = self.sequence()
-        pid = self.get_preidcate_id_by_name(predicate)
-        SegmentType = self.registed_type[tnames[0]]
+        pid = self.get_preidcate_id_by_name(pname)
+        SegmentType = self.registed_type[tnames[0][0]]
         segment = SegmentType(sid, pid, obj)
         self.Session.add(segment)
         if autocommit:
 >>> pid = storage.newPredicate('ssoid', 'string')
 >>> ssoid = storage.getSubjectById(pid)
 >>> segment = storage.writeSegment(500, 'ssoid', 'March Liu')
->>> segments = storage.filterSegmentsByPair('kssoid', 'March Liu')
+>>> segments = [segment for segment in storage.filterSegmentsByPair('ssoid', 'March Liu')]
 >>> print segments[0].subject_id
 500
 >>> print segments[0].predicate.name
-u'ssoid'
+ssoid
 >>> print segments[0].obj
-u'March Liu'
+March Liu
 >>> print len(segments)
 1
         """
-        for k, v in pairs:
-            pname, tnames = self.parsePredicateExpression(k)
-            pid = self.get_preidcate_id_by_name(pname)
-            for tname in tnames:
-                SegmentType = self.registed_type[tnames[0]]
-                for row in self.Session.Query(SegmentType).filter(SegmentType.predicate_id==pid).filter(SegmentType.obj==v):
-                    yield row
+        pname, tnames = self.parsePredicateExpression(predicate_segment)
+        pid = self.get_preidcate_id_by_name(pname)
+        for tname in tnames:
+            SegmentType = self.registed_type[tnames[0][0]]
+            for row in self.Session.query(SegmentType).filter(SegmentType.predicate_id==pid).filter(SegmentType.obj==obj):
+                yield row
         self.Session.commit()
 
     def parsePredicateExpression(self, expr):
         if tname:
             tnames = (tname,)
         else:
-            objType = self.Session.query(subjects.c.obj).\
-                filter(subjects.c.subject_id==pid).\
-                filter(subjects.c.predicate_id==meta.OBJTYPE).scalar() 
-            tnames = self.Session.query(strings.c.obj).\
-                filter(strings.c.subject_id==objType).\
-                filter(strings.c.predicate_id==meta.NAME).all()
+            subjects = self.registed_type['subject']
+            strings = self.registed_type['string']
+            pid = self.get_preidcate_id_by_name(pname)
+            objType = self.Session.query(subjects.obj).\
+                filter(subjects.subject_id==pid).\
+                filter(subjects.predicate_id==meta.OBJTYPE).scalar() 
+            tnames = self.Session.query(strings.obj).\
+                filter(strings.subject_id==objType).\
+                filter(strings.predicate_id==meta.NAME).all()
         return pname, tnames
 
     def get_type_id_by_name(self, name):
         """
 >>> storage = Storage("sqlite://", create=True)
 >>> storage.get_preidcate_id_by_name("no true")
->>> print storage.get_preidcate_id_by_name("string")==meta.STRING
+>>> print storage.get_preidcate_id_by_name("name")==meta.NAME
 True
         """
-        if objtype_name == 'subject':
+        if name == 'subject':
             re = meta.SUBJECT
         else:
             SegmentString = self.registed_type['string']

File src/socrates/types.py

View file
  • Ignore whitespace
     pnames = property(lambda self:set(segment.predicate.name for segment in self.segments))
 
 class Predicate(Subject):
-    objType = property(lambda self:self.predicate('objType'))
+    objType = property(lambda self:self.object('objType'))
     objTypes = property(lambda self:tuple(segment.object 
                                           for segment in self.segments 
                                           if semgents.predicate_id==meta.OBJTYPE))
-    name = property(lambda self:self.predicate('name'))
+    name = property(lambda self:self.object('name'))
 
 class Type(Subject):
-    name = property(lambda self:self.predicate('name'))
-    storage = property(lambda self:self.predicate('storage'))
+    name = property(lambda self:self.object('name'))
+    storage = property(lambda self:self.object('storage'))
 
 class Segment(object):
     def __init__(self, subject_id, predicate_id, obj):