Permalink
Browse files

(codedep - more code cosmetic changes)

Ignore-this: b3eb7a5b8672336d860513ee4cb4e9a1

darcs-hash:20130717000442-3a4db-1c42f989fe5848063b36149e897e564624d4a979
  • Loading branch information...
1 parent 9657d7d commit 9d132211099c4f3c92db57b2f164dbedd362a7d1 @MattShannon committed Jul 17, 2013
Showing with 28 additions and 21 deletions.
  1. +2 −2 codedep/__init__.py
  2. +9 −5 codedep/compute.py
  3. +9 −7 codedep/decorators.py
  4. +4 −4 codedep/hash.py
  5. +4 −3 codedep/test_codedep.py
View
@@ -8,5 +8,5 @@
from __future__ import division
-from decorators import codeHash, codeDeps, ForwardRef, codedepEvalThunk
-from compute import getHash
+from codedep.decorators import codeHash, codeDeps, ForwardRef, codedepEvalThunk
+from codedep.compute import getHash
View
@@ -13,13 +13,16 @@
@codeDeps()
def _resolveAnyForwardRefs(deps):
- return [ dep.thunk() if isinstance(dep, ForwardRef) else dep for dep in 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'])
+ fnOrClassOrObj._codedepCodeDeps = _resolveAnyForwardRefs(
+ fnOrClassOrObj.__dict__['_codedepCodeDeps']
+ )
return fnOrClassOrObj.__dict__['_codedepCodeDeps']
@codeDeps(getDeps)
@@ -36,11 +39,12 @@ def getAllDeps(fnOrClassOrObj):
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?)
+# (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) ]))
+ return hashString(str([ dep.__dict__['_codedepCodeHash']
+ for dep in getAllDeps(fnOrClassOrObj) ]))
@codeDeps(computeHash)
def getHash(fnOrClassOrObj):
View
@@ -30,11 +30,13 @@ def _updateInfo(fnOrClassOrObj):
depending on the use case).
"""
if '_codedepCodeHash' not in fnOrClassOrObj.__dict__:
- fnOrClassOrObj._codedepCodeHash = hashString(inspect.getsource(fnOrClassOrObj))
+ fnOrClassOrObj._codedepCodeHash = hashString(
+ inspect.getsource(fnOrClassOrObj)
+ )
if '_codedepCodeDeps' not in fnOrClassOrObj.__dict__:
fnOrClassOrObj._codedepCodeDeps = []
-def codeHash(codeHash):
+def codeHash(hashValue):
"""Returns a decorator which can be used to manually set _codedepCodeHash.
For example:
@@ -46,7 +48,7 @@ def foo(x):
return a * x
"""
def decorate(fnOrClass):
- fnOrClass._codedepCodeHash = codeHash
+ fnOrClass._codedepCodeHash = hashValue
_updateInfo(fnOrClass)
return fnOrClass
return decorate
@@ -72,8 +74,8 @@ def decorate(fnOrClass):
class ForwardRef(object):
"""Stores an arbitrary callable for use with getDeps.
- This is useful when codeDeps would otherwise need to use a forward reference
- to something that hasn't been defined yet.
+ This is useful when codeDeps would otherwise need to use a forward
+ reference to something that hasn't been defined yet.
For example:
@codeDeps(ForwardRef(lambda: bar))
@@ -91,8 +93,8 @@ def codedepEvalThunk(fnOrClassOrObjThunk):
"""Evaluates a thunk, adding codedep values to the result.
Thunk should evaluate to a newly created function or class or instance of a
- class. It should be newly created since this function adds codedep values by
- attaching mutable state to the result of evaluating the thunk.
+ class. It should be newly created since this function adds codedep values
+ by attaching mutable state to the result of evaluating the thunk.
For example:
@codeDeps():
View
@@ -12,7 +12,7 @@
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()
+ hasher = hashlib.sha1()
+ hasher.update('blob '+str(len(strr))+'\0')
+ hasher.update(strr)
+ return hasher.hexdigest()
@@ -13,12 +13,13 @@
import unittest
class TestCodedep(unittest.TestCase):
- def test_hashString_characterization(self):
- """Simple characterization test for hashString so we will know if it ever changes."""
+ def test_hashString_characterize(self):
+ """Tests hashString function has not changed."""
assert hashString('') == 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
assert hashString('"') == '9d68933c44f13985b9eb19159da6eb3ff0e574bf'
assert hashString('abc') == 'f2ba8f84ab5c1bce84a7b441cb1959cfc7093b7f'
- assert hashString('aRvo;ui') == 'af7aa2bc77f03b961a67b633f93674331eab36c8'
+ assert (hashString('aRvo;ui') ==
+ 'af7aa2bc77f03b961a67b633f93674331eab36c8')
def suite():
return unittest.TestLoader().loadTestsFromTestCase(TestCodedep)

0 comments on commit 9d13221

Please sign in to comment.