Skip to content

Commit 46a58af

Browse files
committed
Do not group 2 numbers
1 parent 4b558f8 commit 46a58af

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

bin/lsdeflate

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ if __name__ == "__main__":
99
for ext in grouped_names:
1010
all_out = all_out + list(map(lambda s: s + ext,
1111
group_basenames(grouped_names[ext])))
12-
all_out.sort()
1312
print("\n".join(all_out))

lsdeflate/__init__.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def group_names_by_ext(lines):
3434
Find the longest sequence
3535
Subtract this from the list
3636
recurse
37+
38+
This returns a list of ranges in no particular order
3739
"""
3840
def list_to_ranges(lst):
3941
if not isinstance(lst, np.ndarray): lst = np.asarray(lst)
@@ -49,8 +51,13 @@ def list_to_ranges(lst):
4951
rest = np.zeros(lst.shape[0]-1-iend+istart,dtype=np.int)
5052
rest[:istart] = lst[:istart]
5153
rest[istart:] = lst[iend+1:]
52-
return sorted([(lst[istart],lst[iend],diffs[istart])] + list_to_ranges(rest),
53-
key=lambda x:x[0])
54+
# If it is only a small range consider giving just the numbers
55+
if ((lst[iend] - lst[istart])/diffs[istart] == 1):
56+
result = [(lst[istart],lst[istart],1),
57+
(lst[iend],lst[iend],1)]
58+
else:
59+
result = [(lst[istart],lst[iend],diffs[istart])]
60+
return result + list_to_ranges(rest)
5461

5562

5663
"""
@@ -93,8 +100,9 @@ def group_basenames(basenames):
93100
for num_s in tmp_s:
94101
output.append(key[0] + num_s + key[1])
95102
else:
96-
ranges = list_to_ranges(tmp)
97-
for imin,imax,step in ranges:
103+
for amin,amax,step in sorted(list_to_ranges(tmp), key=lambda x: x[0]):
104+
imin = np.nonzero(tmp == amin)[0][0]
105+
imax = np.nonzero(tmp == amax)[0][0]
98106
smin = grouped_numbers_str[key][imin]
99107
smax = grouped_numbers_str[key][imax]
100108
# Remove leading zeros

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
setup(
33
name = 'lsdeflate',
44
packages = ['lsdeflate'], # this must be the same as the name above
5-
version = '0.2',
5+
version = '0.3',
66
description = 'Compress ls output by gathering numeric ranges into brace expansions',
77
author = 'Daan van Vugt',
88
author_email = '[email protected]',
99
url = 'https://github.com/exteris/mypackage', # use the URL to the github repo
10-
download_url = 'https://github.com/exteris/lsdeflate/archive/0.2.tar.gz', # I'll explain this in a second
10+
download_url = 'https://github.com/exteris/lsdeflate/archive/0.3.tar.gz', # I'll explain this in a second
1111
keywords = ['unix', 'ls', 'directory', 'clean'], # arbitrary keywords
1212
classifiers = [],
1313
scripts=['bin/lsdeflate'],

tests/test_rangefinder.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ class RangeCases(unittest.TestCase):
88
# for python range function we need to go until step+1
99
cases = ([],# empty
1010
[(1,1,1)],# only [1]
11-
[(1,2,1)],# [1 2]
12-
[(1,3,2)],
11+
[(1,1,1),(2,2,1)],# [1 2]
12+
[(1,1,1),(3,3,1)],# [1 3]
1313
[(2,4,1)],
1414
[(1,1,1),(2,6,2)],
15-
[(1,2,1),(4,5,1),(8,9,1)],
1615
[(1,8,1),(10,12,1),(14,16,1)],
1716
)
1817

@@ -22,7 +21,7 @@ def test_list_to_ranges(self):
2221
lst = sorted(sum(
2322
map(lambda c: list(range(c[0],c[1]+1,c[2])), case), []))
2423
# sum concatenates lists (because + operator does that)
25-
result = lsdeflate.list_to_ranges(lst)
24+
result = sorted(lsdeflate.list_to_ranges(lst), key=lambda x: x[0])
2625
self.assertEqual(case, result, "list="+repr(lst))
2726

2827
if __name__ == '__main__':

0 commit comments

Comments
 (0)