add textutils module.
This commit is contained in:
@@ -0,0 +1 @@
|
||||
__pycache__
|
||||
@@ -0,0 +1,29 @@
|
||||
import textutils
|
||||
import unittest
|
||||
|
||||
class TestTextUtils(unittest.TestCase):
|
||||
def test_positive_tokens(self):
|
||||
self.assertEqual(textutils.create_n_tokens("hello/world/how/are/you/doing", "/", 0), "")
|
||||
self.assertEqual(textutils.create_n_tokens("hello/world/how/are/you/doing", "/", 1), "hello")
|
||||
self.assertEqual(textutils.create_n_tokens("hello/world/how/are/you/doing", "/", 2), "hello/world")
|
||||
|
||||
|
||||
def test_negative_tokens(self):
|
||||
self.assertEqual(textutils.create_n_tokens("hello/world/how/are/you/doing", "/", -1), "doing")
|
||||
self.assertEqual(textutils.create_n_tokens("hello/world/how/are/you/doing", "/", -2), "you/doing")
|
||||
|
||||
def test_n_exceed_length(self):
|
||||
self.assertEqual(textutils.create_n_tokens("hello/world/how/are/you/doing", "/", 8), "hello/world/how/are/you/doing")
|
||||
self.assertEqual(textutils.create_n_tokens("hello/world/how/are/you/doing", "/", -6), "hello/world/how/are/you/doing")
|
||||
|
||||
def test_delim_at_the_begining(self):
|
||||
self.assertEqual(textutils.create_n_tokens("/hello/world/how/are/you/doing/", "/", 0), "")
|
||||
self.assertEqual(textutils.create_n_tokens("/hello/world/how/are/you/doing/", "/", 1), "")
|
||||
self.assertEqual(textutils.create_n_tokens("/hello/world/how/are/you/doing/", "/", 2), "/hello")
|
||||
|
||||
def test_delim_at_the_end(self):
|
||||
self.assertEqual(textutils.create_n_tokens("hello/world/how/are/you/doing/", "/", -1), "")
|
||||
self.assertEqual(textutils.create_n_tokens("hello/world/how/are/you/doing/", "/", -2), "doing/")
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,11 @@
|
||||
def create_n_tokens(str, delim, n):
|
||||
parts = str.split(delim)
|
||||
|
||||
if n > 0 :
|
||||
return delim.join(parts[0: n])
|
||||
else :
|
||||
l = len(parts)
|
||||
start = l + n
|
||||
if start < 0: start = 0
|
||||
|
||||
return delim.join(parts[start: l])
|
||||
Reference in New Issue
Block a user