ziggy.GraphReduce.Degree_mapper

Package ziggy :: Package GraphReduce :: Module Degree_mapper |
[hide private]
[frames] | no frames]

# Source Code for Module ziggy.GraphReduce.Degree_mapper

     1  #! /usr/bin/env python
     2  '''
     3  Created on Aug 25, 2010
     4
     5  @author: dwmclary
     6  '''
     7
     8  import sys
     9
    10 -class DegreeNode(object):
    11 -    def __init__(self, nID=None, in_degree=None, adj=None, out_degree=None):
    12          if nID != None:
    13              self.nID = nID
    14              self.in_degree = in_degree
    15              self.adj = adj
    16              self.out_degree = out_degree
    17
    18 -    def write(self):
    19          s = str(self.nID)+  " "+ " ".join(map(str,self.adj)) +\
    20          " in: "+" ".join(map(str, self.in_degree)) + " out: " + " ".join(map(str,self.out_degree))
    21          return s
    22
    23 -    def __str__(self):
    24          return self.write()
    25
    26 -def read_adjacency(file, comment="#", sep=None):
    27      for line in file:
    28          line = line.rstrip()
    29          if line[0] != comment:
    30              entry = line.split(sep)
    31              nid = entry[0]
    32              if "in:" in entry:
    33                  d_index = entry.index("in:")
    34                  p_index = entry.index("out:")
    35                  in_degree = entry[d_index+1:p_index]
    36                  out_degree = entry[p_index+1:]
    37                  adj = entry[1:d_index]
    38              else:
    39                  adj = entry[1:]
    40                  out_degree = adj
    41                  in_degree = []
    42              node = DegreeNode(nid,in_degree,adj, out_degree)
    43              yield node
    44
    45 -def main():
    46      data = read_adjacency(sys.stdin)
    47      for node in data:
    48
    49          node.out_degree = node.adj
    50          print node
    51          for m in node.adj:
    52              print str(m) + " indegree: " + node.nID
    53
    54  if __name__ == "__main__":
    55      try:
    56          import psyco
    57          psyco.full()
    58      except:
    59          pass
    60      main()
    61