Commits

Dhananjay Nene committed e631a2a

Added support for digest computation using md5 or sha256

Comments (0)

Files changed (1)

rnd/initial/pyedpyper.py

             target.send(vals)
 
 @coroutine
+def filter(predicate, next = None):
+    """
+    Filters the stream on the predicate. Only items that make the predicate evaluate
+    to true are passed on further. The rest are dropped. However note that this is 
+    just a special case of the router where only the next_if and not the next_else
+    are specified.
+    
+    TODO: Should this be retained ? Instinctive response is DROP. Only value is syn sugar
+    """
+    while True :
+        line = (yield)
+        if predicate(line) == True :
+            if next :
+                next.send(line)
+
+@coroutine
 def router(predicate, next_if = None, next_else = None):
     """
     routes to one of two branches based on the results of the predicate
                     val = mergefunc(keydict)
                     if next :
                         next.send(val)
-                    
+ 
+                   
+##############################################################################
+# Cryptographic operations
+##############################################################################
+@coroutine
+def digest(dtype='sha256', next = None):
+    from Crypto.Hash import MD5
+    from Crypto.Hash import SHA256
+    while True :
+        line = (yield)
+        m = None
+        if dtype == 'sha256' :
+            m = SHA256.new()
+        elif dtype == 'md5' :
+            m = MD5.new()
+        if m :
+            m.update(line)
+            val = m.hexdigest()
+        if next :
+            next.send(val)
+
 ##############################################################################
 # Sample Usage
 # NOTE: The usage constructs / use case is an entirely arbitrary sequence of
                 ['contacts','fixedwidth'],
                 lambda dct : ":".join([dct['contacts'].email,dct['fixedwidth'][2]]),
                 True,
-                tracer('PostJoin')
+                splitter([
+                    digest(
+                         next = tracer('Digest')
+                    ),
+                    tracer('PostJoin')
+                ])
             )            
 # Open file
 url_opener(