utils.py
changeset 6141 b8287e54b528
parent 6067 efca814587e2
child 6149 57e956441ca4
equal deleted inserted replaced
6140:65a619eb31c4 6141:b8287e54b528
    72         return True
    72         return True
    73     for argname in argnames:
    73     for argname in argnames:
    74         if argname not in argspec[0]:
    74         if argname not in argspec[0]:
    75             return False
    75             return False
    76     return True
    76     return True
       
    77 
       
    78 
       
    79 class wrap_on_write(object):
       
    80     def __init__(self, w, tag, closetag=None):
       
    81         self.written = False
       
    82         self.tag = unicode(tag)
       
    83         self.closetag = closetag
       
    84         self.w = w
       
    85 
       
    86     def __enter__(self):
       
    87         return self
       
    88 
       
    89     def __call__(self, data):
       
    90         if self.written is False:
       
    91             self.w(self.tag)
       
    92             self.written = True
       
    93         self.w(data)
       
    94 
       
    95     def __exit__(self, exctype, value, traceback):
       
    96         if self.written is True:
       
    97             if self.closetag:
       
    98                 self.w(unicode(self.closetag))
       
    99             else:
       
   100                 self.w(self.tag.replace('<', '</', 1))
       
   101 
    77 
   102 
    78 # use networkX instead ?
   103 # use networkX instead ?
    79 # http://networkx.lanl.gov/reference/algorithms.traversal.html#module-networkx.algorithms.traversal.astar
   104 # http://networkx.lanl.gov/reference/algorithms.traversal.html#module-networkx.algorithms.traversal.astar
    80 def transitive_closure_of(entity, relname, _seen=None):
   105 def transitive_closure_of(entity, relname, _seen=None):
    81     """return transitive closure *for the subgraph starting from the given
   106     """return transitive closure *for the subgraph starting from the given