Skip to content

Commit 6f02f2f

Browse files
committed
All slug separators are underscores by default for consistency. wireservice#615
1 parent 7286516 commit 6f02f2f

3 files changed

Lines changed: 20 additions & 14 deletions

File tree

agate/utils.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def issequence(obj):
253253
return isinstance(obj, Sequence) and not isinstance(obj, six.string_types)
254254

255255

256-
def deduplicate(values, column_names=False):
256+
def deduplicate(values, column_names=False, separator='_'):
257257
"""
258258
Append a unique identifer to duplicate strings in a given sequence of
259259
strings. Identifers are an underscore followed by the occurance number of
@@ -284,7 +284,7 @@ def deduplicate(values, column_names=False):
284284
duplicates = 0
285285

286286
while final_value in final_values:
287-
final_value = new_value + '_' + str(duplicates + 2)
287+
final_value = new_value + separator + str(duplicates + 2)
288288
duplicates += 1
289289

290290
if column_names and duplicates > 0:
@@ -301,11 +301,17 @@ def slugify(values, ensure_unique=False, **kwargs):
301301
If ``ensure_unique`` is True, any duplicate strings will be appended with
302302
a unique identifier.
303303
304+
agate uses an underscore as a default separator but this can be changed with
305+
kwargs.
306+
304307
Any kwargs will be passed to the slugify method in python-slugify. See:
305308
https://github.com/un33k/python-slugify
306309
"""
310+
slug_args = {'separator': '_'}
311+
slug_args.update(kwargs)
312+
307313
if ensure_unique:
308-
new_values = tuple(pslugify(value, **kwargs) for value in values)
309-
return deduplicate(new_values)
314+
new_values = tuple(pslugify(value, **slug_args) for value in values)
315+
return deduplicate(new_values, separator=slug_args['separator'])
310316
else:
311-
return tuple(pslugify(value, **kwargs) for value in values)
317+
return tuple(pslugify(value, **slug_args) for value in values)

tests/test_computations.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def test_slug(self):
370370
('Ab*c #e', 2),
371371
('He11O W0rld', 3)
372372
)
373-
expected = ['hello-world', 'ab-c-e', 'he11o-w0rld']
373+
expected = ['hello_world', 'ab_c_e', 'he11o_w0rld']
374374

375375
table = Table(rows, ['one', 'two'], [self.text_type, self.number_type]).compute([
376376
('slugs', Slug('one'))
@@ -384,7 +384,7 @@ def test_slug_column_name_sequence(self):
384384
('Ab*c #e', 2, 'He11O W0rld'),
385385
('He11O W0rld', 3, 'hello world')
386386
)
387-
expected = ['hello-world-ab-c-e', 'ab-c-e-he11o-w0rld', 'he11o-w0rld-hello-world']
387+
expected = ['hello_world_ab_c_e', 'ab_c_e_he11o_w0rld', 'he11o_w0rld_hello_world']
388388

389389
table1 = Table(rows, ['one', 'two', 'three'], [self.text_type, self.number_type, self.text_type])
390390
table2 = table1.compute([
@@ -400,7 +400,7 @@ def test_slug_ensure_unique(self):
400400
('He11O W0rld', 3),
401401
('HellO WOrld ', 3)
402402
)
403-
expected = ['hello-world', 'ab-c-e', 'he11o-w0rld', 'hello-world_2']
403+
expected = ['hello_world', 'ab_c_e', 'he11o_w0rld', 'hello_world_2']
404404

405405
table = Table(rows, ['one', 'two'], [self.text_type, self.number_type]).compute([
406406
('slugs', Slug('one', ensure_unique=True))

tests/test_table/test_rename.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test_rename_slugify_columns(self):
7878
table3 = table.rename(strings, slug_columns=True, separator='.')
7979

8080
self.assertColumnNames(table, ['one', 'two', 'three'])
81-
self.assertColumnNames(table2, ['test-koz', 'test-2', 'test-2_2'])
81+
self.assertColumnNames(table2, ['test_koz', 'test_2', 'test_2_2'])
8282
self.assertColumnNames(table3, ['test.koz', 'test.2', 'test.2_2'])
8383

8484
def test_rename_slugify_rows(self):
@@ -89,8 +89,8 @@ def test_rename_slugify_rows(self):
8989
table3 = table.rename(row_names=strings, slug_rows=True, separator='.')
9090

9191
self.assertIs(table.row_names, None)
92-
self.assertRowNames(table2, ['test-koz', 'test-2', 'test-2_2'])
93-
self.assertRowNames(table3, ['test.koz', 'test.2', 'test.2_2'])
92+
self.assertRowNames(table2, ['test_koz', 'test_2', 'test_2_2'])
93+
self.assertRowNames(table3, ['test.koz', 'test.2', 'test.2.2'])
9494

9595
def test_rename_slugify_columns_in_place(self):
9696
column_names = [u'Test kož', 'test 2', 'test 2']
@@ -106,7 +106,7 @@ def test_rename_slugify_columns_in_place(self):
106106
table3 = table.rename(slug_columns=True, separator='.')
107107

108108
self.assertColumnNames(table, [u'Test kož', 'test 2', 'test 2_2'])
109-
self.assertColumnNames(table2, ['test-koz', 'test-2', 'test-2-2'])
109+
self.assertColumnNames(table2, ['test_koz', 'test_2', 'test_2_2'])
110110
self.assertColumnNames(table3, ['test.koz', 'test.2', 'test.2.2'])
111111

112112
def test_rename_slugify_rows_in_place(self):
@@ -117,5 +117,5 @@ def test_rename_slugify_rows_in_place(self):
117117
table3 = table.rename(slug_rows=True, separator='.')
118118

119119
self.assertRowNames(table, ['Test kož', 'test 2', 'test 2'])
120-
self.assertRowNames(table2, ['test-koz', 'test-2', 'test-2_2'])
121-
self.assertRowNames(table3, ['test.koz', 'test.2', 'test.2_2'])
120+
self.assertRowNames(table2, ['test_koz', 'test_2', 'test_2_2'])
121+
self.assertRowNames(table3, ['test.koz', 'test.2', 'test.2.2'])

0 commit comments

Comments
 (0)