Skip to content

Commit

Permalink
strutil: Add 'nym' function
Browse files Browse the repository at this point in the history
Helper function for dealing with names while avoiding pesky
things like quoting, spaces, and case sensitivity
  • Loading branch information
lhh committed Sep 6, 2023
1 parent 8cbe4b9 commit 32cf97f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tests/test_strutil_nym.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python

""" Tests for the nym function from strutil module """

from toolchest.strutil import nym


def test_identity():
assert nym('abc') == 'abc'


def test_spaces():
assert nym('abc def') == 'abc_def'


def test_tabs():
assert nym('abc\tdef') == 'abc_def'


def test_lowercase():
assert nym('aBcDeF') == 'abcdef'
21 changes: 21 additions & 0 deletions toolchest/strutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,24 @@ def regex_match(pattern, arg):
if re.match(pattern, arg) is not None:
return True
return False


def nym(arg):
'''
This is for allowing case and quote flexibility for strings when
searching dictionaries or other data sets based on user input (esp.
from the command line) where the likelihood of key collisions is
low. For example, if we want to search a dictionary, we'd check the
nym of the value provided with the nym of the key to see if they
match. This should not be used when likelihood of collisions is high.
(Origin: Greek word meaning "name")
Parameters:
arg (string): A string to create the nym for
Returns:
ret (string): A lower-case string with whitespace swapped to _
'''
ret = arg.lower().replace(' ', '_')
ret = ret.replace('\t', '_')
return ret

0 comments on commit 32cf97f

Please sign in to comment.