Anonymous avatar Anonymous committed 731342e

success all doctest

Comments (0)

Files changed (2)

src/socrates/core.py

     def writeSubject(self, subject_id, segments, **pairs):
         """
 >>> storage = Storage("sqlite://", create=True)
->>> pid = storage.newPredicate('kssoid', 'string')
->>> sid = storage.writeSubject(500, {'kssoid':'liuxin2'})
+>>> pid = storage.newPredicate('ssoid', 'string')
+>>> sid = storage.writeSubject(500, {'ssoid':'liuxin2'})
 >>> print sid
 500
->>> sid = storage.writeSubject(None, {'kssoid':'nobody'})
+>>> sid = storage.writeSubject(None, {'ssoid':'nobody'})
 >>> print sid
 11
         """
 ssoid
 >>> subject = storage.getSubjectById(segment.subject_id)
 >>> print subject.pnames
-set([u'ssoid'])
+[u'ssoid']
         """
         segments = [segment for segment in self.getSegmentsBySubjectId(subject_id)]
+        subject = self.registed_storage['subject']
         subject_is = [segment 
                       for segment in segments 
                       if segment.predicate_id==meta.IS and segment.obj in (meta.PREDICATE, meta.TYPE)]
 >>> newseg = storage.writeSegment(None, 'ssoid', 'Milly Lee')
 >>> subject = storage.getSubjectById(segment.subject_id)
 >>> print subject.pnames
-set([u'ssoid'])
+[u'ssoid']
 >>> storage.deleteSubjectById(segment.subject_id)
 >>> segments = [segment for segment in storage.filterSegmentsByPair('ssoid', 'March Liu')]
 >>> segments
             filter(select([SegmentString.obj], 
                           and_(SegmentString.subject_id==SegmentSubject.subject_id,
                                SegmentString.predicate_id==meta.NAME)).as_scalar()==name).scalar()
-        #session.close()
         return re
 
+    def get_predicate_name_by_id(self, pid):
+        """
+>>> storage = Storage("sqlite://", create=True)
+>>> name = storage.get_predicate_name_by_id(meta.NAME)
+>>> print name
+name
+>>> print name == 'name'
+True
+        """
+        SegmentString = self.registed_type['string']
+        re = self.Session.query(SegmentString.obj,).\
+            filter(SegmentString.predicate_id==meta.NAME).\
+            filter(SegmentString.subject_id==pid).scalar()
+
+        return re
+
+    def get_object_by_id_pair(self, sid, pid):
+        sgstr = self.registed_type['string']
+        sgsbj = self.registed_type['subject']
+        storage = self.Storage()
+        session = storage.Session()
+        objType = session.query(sgsbj.c.obj).filter(sgsbj.c.subjecgt_id==pid).filter(sgsbj.c.predicate_id==meta.OBJTYPE).scalar()
+        stname = session.query(sgstr.c.obj).filter(sgstr.c.subjecgt_id==pid).filter(sgstr.c.predicate_id==meta.NAME).scalar()
+        table = storage.registed_storage['stname']
+        return session.query(table.c.obj).filter(table.c.subject_id==sid).filter(table.c.predicate_id==pid).scalar()
+
+    def get_segment_by_id_pair(self, sid, pid):
+        sgstr = self.registed_type['string']
+        sgsbj = self.registed_type['subject']
+        session = self.Session()
+        objType = session.query(sgsbj.obj).filter(sgsbj.subject_id==pid).filter(sgsbj.predicate_id==meta.OBJTYPE).scalar()
+        stname = session.query(sgstr.obj).filter(sgstr.subject_id==objType).filter(sgstr.predicate_id==meta.NAME).scalar()
+        table = self.registed_storage[stname]
+        return session.query(table).filter(table.c.subject_id==sid).filter(table.c.predicate_id==pid).first()
+
+
     def __make_type_helper(self, typename, table):
         def setSubject(_self, subject):
             _self.obj = subject.subject_id

src/socrates/types.py

 from socrates.parser.prdparser.parse import ExprParser as PrdExprParser
 
 class Subject(object):
-    def __init__(self, storage, segments):
-        self.__storage = storage
-        self.segments = segments
+    def __init__(self, engine, segments):
+        self.engine = engine
+        self.segments = dict((engine.get_predicate_name_by_id(segment.predicate_id), segment)
+                             for segment in segments)
         self.subject_id=segments[0].subject_id
         self.prdexprpaser = PrdExprParser()
     def write(self, prdexpr, obj):
-        segment = self.__storage.writeSegment(self.subject_id, prdexpr, obj)
+        segment = self.engine.writeSegment(self.subject_id, prdexpr, obj)
         self.segments.append(segment)
     def Session(self, ):
-        return self.__storage.Session
+        return self.engine.Session
     def commit(self):
         return self.Session.commit()
     def rollback(self):
         return self.Session.rollback()
 
     def object(self, pname):
-        re = self.objects(pname)
-        if len(re)==0:
-            return None
-        elif len(re)==1:
-            return re[0]
+        if pname in self.segments:
+            return self.segments[pname].object
         else:
-            return re
+            pid = self.engine.get_predicate_id_by_name(pname)
+            segment = self.engine.get_segment_by_id_pair(self.subject_id, pid)
+            self.segments[pname] = segment
+            return segment.obj
+
     def __eq__(self, obj):
         return self.subject_id==obj.sbuject_id
     def predicates(self):
         return tuple(set(segment.predicate for segment in self.segments))
-    def objects(self, pname):
-        pid = self.__storage.get_predicate_id_by_name(pname)
-        return [segment.object for segment in self.segments if segment.predicate_id==pid]
 
-    def count(self, pname):
-        pid = self.__storage.get_predicate_id_by_name(pname)
-        return len([segment.object for segment in self.segments if segment.predicate_id==pid])
-        
-
-    pnames = property(lambda self:set(segment.predicate.name for segment in self.segments))
+    pnames = property(lambda self:self.segments.keys())
 
 class Predicate(Subject):
     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))
+                                          if semgent.predicate_id==meta.OBJTYPE))
     name = property(lambda self:self.object('name'))
 
 class Type(Subject):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.