dulwich-users team mailing list archive
-
dulwich-users team
-
Mailing list archive
-
Message #00291
[PATCH 13/28] Move permutations from test_objects to misc.
From: Dave Borowitz <dborowitz@xxxxxxxxxx>
Change-Id: Idfda34da5603521c7c3b9b8adb3d8f031bb2d327
---
dulwich/misc.py | 34 ++++++++++++++++++++++++++++++++++
dulwich/tests/test_objects.py | 37 +++----------------------------------
2 files changed, 37 insertions(+), 34 deletions(-)
diff --git a/dulwich/misc.py b/dulwich/misc.py
index c7f00c0..55a3a5f 100644
--- a/dulwich/misc.py
+++ b/dulwich/misc.py
@@ -102,6 +102,40 @@ def unpack_from(fmt, buf, offset=0):
try:
+ from itertools import permutations
+except ImportError:
+ # Implementation of permutations from Python 2.6 documentation:
+ # http://docs.python.org/2.6/library/itertools.html#itertools.permutations
+ # Copyright (c) 2001-2010 Python Software Foundation; All Rights Reserved
+ # Modified syntax slightly to run under Python 2.4.
+ def permutations(iterable, r=None):
+ # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
+ # permutations(range(3)) --> 012 021 102 120 201 210
+ pool = tuple(iterable)
+ n = len(pool)
+ if r is None:
+ r = n
+ if r > n:
+ return
+ indices = range(n)
+ cycles = range(n, n-r, -1)
+ yield tuple(pool[i] for i in indices[:r])
+ while n:
+ for i in reversed(range(r)):
+ cycles[i] -= 1
+ if cycles[i] == 0:
+ indices[i:] = indices[i+1:] + indices[i:i+1]
+ cycles[i] = n - i
+ else:
+ j = cycles[i]
+ indices[i], indices[-j] = indices[-j], indices[i]
+ yield tuple(pool[i] for i in indices[:r])
+ break
+ else:
+ return
+
+
+try:
from collections import namedtuple
TreeEntryTuple = namedtuple('TreeEntryTuple', ['path', 'mode', 'sha'])
diff --git a/dulwich/tests/test_objects.py b/dulwich/tests/test_objects.py
index f86da62..30cba35 100644
--- a/dulwich/tests/test_objects.py
+++ b/dulwich/tests/test_objects.py
@@ -30,6 +30,9 @@ import stat
from dulwich.errors import (
ObjectFormatException,
)
+from dulwich.misc import (
+ permutations,
+ )
from dulwich.objects import (
Blob,
Tree,
@@ -64,40 +67,6 @@ tree_sha = '70c190eb48fa8bbb50ddc692a17b44cb781af7f6'
tag_sha = '71033db03a03c6a36721efcf1968dd8f8e0cf023'
-try:
- from itertools import permutations
-except ImportError:
- # Implementation of permutations from Python 2.6 documentation:
- # http://docs.python.org/2.6/library/itertools.html#itertools.permutations
- # Copyright (c) 2001-2010 Python Software Foundation; All Rights Reserved
- # Modified syntax slightly to run under Python 2.4.
- def permutations(iterable, r=None):
- # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
- # permutations(range(3)) --> 012 021 102 120 201 210
- pool = tuple(iterable)
- n = len(pool)
- if r is None:
- r = n
- if r > n:
- return
- indices = range(n)
- cycles = range(n, n-r, -1)
- yield tuple(pool[i] for i in indices[:r])
- while n:
- for i in reversed(range(r)):
- cycles[i] -= 1
- if cycles[i] == 0:
- indices[i:] = indices[i+1:] + indices[i:i+1]
- cycles[i] = n - i
- else:
- j = cycles[i]
- indices[i], indices[-j] = indices[-j], indices[i]
- yield tuple(pool[i] for i in indices[:r])
- break
- else:
- return
-
-
class TestHexToSha(TestCase):
def test_simple(self):
--
1.7.3.2.168.gd6b63
References