Skip to content

Commit 7b9b057

Browse files
committed
util: +pluralize
1 parent f41bc34 commit 7b9b057

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

‎util.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ def print2(*args, **kwargs):
300300
builtin_print(*args, **kwargs, end=end)
301301

302302
def array_to_range_text(a, sep=', ', dash='-'):
303+
'''
304+
Convert an array of integers to a range text (e.g., [1,2,3,5,6,8] -> '1-3, 5-6, 8')
305+
'''
303306
s = ''
304307
prev_seg = None
305308
for idx, seg in enumerate(a):
@@ -318,6 +321,19 @@ def array_to_range_text(a, sep=', ', dash='-'):
318321
prev_seg = seg
319322
return s
320323

324+
def pluralize(word, count, suffix='s', alt=None):
325+
'''
326+
Pluralize a word based on the count. If count is 1, return the word as is.
327+
If count is not 1, return the word with the suffix added, or the alt word if provided (e.g. for irregular plurals).
328+
'''
329+
if count == 1:
330+
return word
331+
else:
332+
if alt:
333+
return alt
334+
else:
335+
return word + suffix
336+
321337
def compare_obj(value_old, value, print_prefix='ROOT', mute=False):
322338
'''
323339
Compare two objects and print the differences.

0 commit comments

Comments
 (0)