Permalink
Browse files

codedep - make a package

Ignore-this: 1a91b1ebc92d7571896cb521a24b23a3

darcs-hash:20130716230031-3a4db-a87572ca8e3604590c069524501464f98526278a
  • Loading branch information...
1 parent bccd56e commit c74b3daba52257d625cd5244cbc67a264fbac485 @MattShannon committed Jul 16, 2013
View
@@ -0,0 +1,12 @@
+"""Implements code-level dependency tracking."""
+
+# Copyright 2011, 2012, 2013 Matt Shannon
+
+# This file is part of armspeech.
+# See `License` for details of license and warranty.
+
+
+from __future__ import division
+
+from decorators import codeHash, codeDeps, ForwardRef, codedepEvalThunk
+from compute import getHash
File renamed without changes.
View
@@ -0,0 +1,49 @@
+"""Computes hash values for functions and objects."""
+
+# Copyright 2011, 2012, 2013 Matt Shannon
+
+# This file is part of armspeech.
+# See `License` for details of license and warranty.
+
+
+from __future__ import division
+
+from codedep.hash import hashString
+from codedep.decorators import codeDeps, ForwardRef
+
+@codeDeps()
+def _resolveAnyForwardRefs(deps):
+ return [ dep.thunk() if isinstance(dep, ForwardRef) else dep for dep in deps ]
+
+@codeDeps(_resolveAnyForwardRefs)
+def getDeps(fnOrClassOrObj):
+ if '_codedepCodeDeps' not in fnOrClassOrObj.__dict__:
+ raise RuntimeError('codedep values not found for %s' % fnOrClassOrObj)
+ fnOrClassOrObj._codedepCodeDeps = _resolveAnyForwardRefs(fnOrClassOrObj.__dict__['_codedepCodeDeps'])
+ return fnOrClassOrObj.__dict__['_codedepCodeDeps']
+
+@codeDeps(getDeps)
+def getAllDeps(fnOrClassOrObj):
+ ret = []
+ agenda = [fnOrClassOrObj]
+ seen = set()
+ while agenda:
+ curr = agenda.pop()
+ ident = id(curr)
+ if not ident in seen:
+ seen.add(ident)
+ ret.append(curr)
+ agenda.extend(reversed(getDeps(curr)))
+ return ret
+
+# (FIXME : does this guarantee the hash will change if any changes are made to a
+# set of functions? Can come up with a counter-example?)
+@codeDeps(getAllDeps)
+def computeHash(fnOrClassOrObj):
+ return hashString(str([ dep.__dict__['_codedepCodeHash'] for dep in getAllDeps(fnOrClassOrObj) ]))
+
+@codeDeps(computeHash)
+def getHash(fnOrClassOrObj):
+ if '_codedepHash' not in fnOrClassOrObj.__dict__:
+ fnOrClassOrObj._codedepHash = computeHash(fnOrClassOrObj)
+ return fnOrClassOrObj.__dict__['_codedepHash']
@@ -1,4 +1,4 @@
-"""Implements code-level dependency tracking."""
+"""Decorators for adding dependency information to functions and objects."""
# Copyright 2011, 2012, 2013 Matt Shannon
@@ -8,15 +8,9 @@
from __future__ import division
-import hashlib
-import inspect
+from codedep.hash import hashString
-def hashString(strr):
- """Computes git-style hash of a string."""
- m = hashlib.sha1()
- m.update('blob '+str(len(strr))+'\0')
- m.update(strr)
- return m.hexdigest()
+import inspect
def _updateInfo(fnOrClassOrObj):
"""Sets any unset codedep values to default values.
@@ -120,40 +114,3 @@ def getTwo():
fnOrClassOrObj._codedepCodeDeps = (fnOrClassOrObjThunk,)
_updateInfo(fnOrClassOrObj)
return fnOrClassOrObj
-
-@codeDeps()
-def _resolveAnyForwardRefs(deps):
- return [ dep.thunk() if isinstance(dep, ForwardRef) else dep for dep in deps ]
-
-@codeDeps(_resolveAnyForwardRefs)
-def getDeps(fnOrClassOrObj):
- if '_codedepCodeDeps' not in fnOrClassOrObj.__dict__:
- raise RuntimeError('codedep values not found for %s' % fnOrClassOrObj)
- fnOrClassOrObj._codedepCodeDeps = _resolveAnyForwardRefs(fnOrClassOrObj.__dict__['_codedepCodeDeps'])
- return fnOrClassOrObj.__dict__['_codedepCodeDeps']
-
-@codeDeps(getDeps)
-def getAllDeps(fnOrClassOrObj):
- ret = []
- agenda = [fnOrClassOrObj]
- seen = set()
- while agenda:
- curr = agenda.pop()
- ident = id(curr)
- if not ident in seen:
- seen.add(ident)
- ret.append(curr)
- agenda.extend(reversed(getDeps(curr)))
- return ret
-
-# (FIXME : does this guarantee the hash will change if any changes are made to a
-# set of functions? Can come up with a counter-example?)
-@codeDeps(getAllDeps)
-def computeHash(fnOrClassOrObj):
- return hashString(str([ dep.__dict__['_codedepCodeHash'] for dep in getAllDeps(fnOrClassOrObj) ]))
-
-@codeDeps(computeHash)
-def getHash(fnOrClassOrObj):
- if '_codedepHash' not in fnOrClassOrObj.__dict__:
- fnOrClassOrObj._codedepHash = computeHash(fnOrClassOrObj)
- return fnOrClassOrObj.__dict__['_codedepHash']
View
@@ -0,0 +1,18 @@
+"""Specifies hash functions."""
+
+# Copyright 2011, 2012, 2013 Matt Shannon
+
+# This file is part of armspeech.
+# See `License` for details of license and warranty.
+
+
+from __future__ import division
+
+import hashlib
+
+def hashString(strr):
+ """Computes git-style hash of a string."""
+ m = hashlib.sha1()
+ m.update('blob '+str(len(strr))+'\0')
+ m.update(strr)
+ return m.hexdigest()
@@ -8,17 +8,17 @@
from __future__ import division
-import codedep
+from codedep.hash import hashString
import unittest
class TestCodedep(unittest.TestCase):
def test_hashString_characterization(self):
"""Simple characterization test for hashString so we will know if it ever changes."""
- assert codedep.hashString('') == 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
- assert codedep.hashString('"') == '9d68933c44f13985b9eb19159da6eb3ff0e574bf'
- assert codedep.hashString('abc') == 'f2ba8f84ab5c1bce84a7b441cb1959cfc7093b7f'
- assert codedep.hashString('aRvo;ui') == 'af7aa2bc77f03b961a67b633f93674331eab36c8'
+ assert hashString('') == 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
+ assert hashString('"') == '9d68933c44f13985b9eb19159da6eb3ff0e574bf'
+ assert hashString('abc') == 'f2ba8f84ab5c1bce84a7b441cb1959cfc7093b7f'
+ assert hashString('aRvo;ui') == 'af7aa2bc77f03b961a67b633f93674331eab36c8'
def suite():
return unittest.TestLoader().loadTestsFromTestCase(TestCodedep)
View
@@ -1,4 +1,4 @@
#!/bin/bash
-PYTHONPATH=. /usr/bin/python -u test_codedep.py "$@"
+PYTHONPATH=. /usr/bin/python -u codedep/test_codedep.py "$@"
PYTHONPATH=. /usr/bin/python -u bisque/test_bisque.py "$@"
PYTHONPATH=. /usr/bin/python -u armspeech/test_armspeech.py "$@"

0 comments on commit c74b3da

Please sign in to comment.