-
Notifications
You must be signed in to change notification settings - Fork 0
/
seq_tools.py
executable file
·63 lines (44 loc) · 1.77 KB
/
seq_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
import unittest
def is_sequence(arg):
return not hasattr(arg, 'strip') and hasattr(arg, '__getitem__') and hasattr(arg, '__iter__')
class TestIsSeq(unittest.TestCase):
def test_string(self):
self.assertFalse(is_sequence('test'))
def test_tuple_string(self):
self.assertTrue(is_sequence(('test', )))
def test_tuple_strings(self):
self.assertTrue(is_sequence(('test0', 'test1')))
def test_list_string(self):
self.assertTrue(is_sequence(['test', ]))
def test_list_strings(self):
self.assertTrue(is_sequence(['test0', 'test1']))
def to_sequence(arg):
return arg if is_sequence(arg) else (arg, )
class TestToSeq(unittest.TestCase):
def test_none(self):
self.assertEqual(to_sequence(None), (None, ))
def test_scalar(self):
self.assertEqual(to_sequence(12), (12, ))
def test_tuple(self):
self.assertEqual(to_sequence((1, 2, 3)), (1, 2, 3))
def test_list(self):
self.assertEqual(to_sequence([1, 2, 3]), [1, 2, 3])
def test_str(self):
self.assertEqual(to_sequence('abc'), ('abc', ))
def is_sequence_or_set(arg):
return isinstance(arg, set) or is_sequence(arg)
class TestIsSeqOrSet(unittest.TestCase):
def test_set(self):
self.assertTrue(is_sequence_or_set(set([1, 2, 3])))
def test_set_string(self):
self.assertTrue(is_sequence_or_set(set('test', )))
def test_set_strings(self):
self.assertTrue(is_sequence_or_set(set(('test0', 'test1'))))
def to_sequence_or_set(arg):
return arg if is_sequence_or_set(arg) else (arg, )
class TestToSeqOrSet(unittest.TestCase):
def test_set(self):
self.assertEqual(to_sequence_or_set(set([1, 2, 3])), {1, 2, 3})
if __name__ == '__main__':
unittest.main()