Skip to content

Commit 41bdd9f

Browse files
Code cleanup, add new setup script and tests
1 parent 26e9cd6 commit 41bdd9f

File tree

3 files changed

+81
-33
lines changed

3 files changed

+81
-33
lines changed

usefulib/_usefulibs.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
Add your useful method here if you are contributing. Remember to add unit tests in tests.py :)
88
"""
99

10-
if __name__ == "__main__":
11-
import setup_script
12-
else:
13-
from . import setup_script
10+
""" Setup - add any setup scripts here remembering to put the function(s) they are for. """
1411

15-
setup_script.SETUP() # If you need any setup scripts, write them in setup_script.py.
12+
import random # generate_random_string()
13+
import string # generate_random_string()
14+
import uuid # generateUUID()
15+
16+
""""""
1617

1718
def reverse_string(string):
1819
""" @hamdivazim - Reverses a string. """
@@ -37,7 +38,7 @@ def loop_dict(dictionary):
3738
i = 0
3839

3940
for key in dictionary.keys():
40-
result += (key, dictionary[key], i)
41+
result.append((key, dictionary[key], i))
4142

4243
i += 1
4344

@@ -46,24 +47,23 @@ def loop_dict(dictionary):
4647
def find_nth_root(num, n):
4748
""" @hamdivazim - Returns the nth root of a number you provide. """
4849

50+
if num < 0:
51+
raise ValueError("Cannot get root of a negative number.")
52+
4953
return num ** (1 / n)
5054

51-
def filter_by_string(lst, string):
55+
def filter_by_string(lst, stri):
5256
"""
5357
@hamdivazim - Filters a list based on whether elements contain a specific string.
5458
Probably best used when filtering by a search query.
5559
"""
5660

57-
result = []
61+
try:
62+
result = [x for x in lst if stri in x]
5863

59-
for element in lst:
60-
try:
61-
if string in element:
62-
result += element
63-
except TypeError:
64-
raise TypeError("A non-string value was found while using the usefulib.filter_by_string method. Maybe try using filter_by_condition?")
65-
66-
return result
64+
return result
65+
except TypeError:
66+
raise TypeError("A non-string value was found while filtering. Maybe try using filter_by_condition()?")
6767

6868
def filter_by_condition(lst, condition: str):
6969
"""
@@ -84,11 +84,11 @@ def filter_by_condition(lst, condition: str):
8484
result = []
8585

8686
for i in lst:
87-
exec(f"if({condition}):result+=i")
87+
exec(f"if({condition}):result.append(i)")
8888

8989
return result
9090

91-
def generate_random_string(word_length):
91+
def generate_random_string(word_length=18):
9292
components = [string.ascii_letters, string.digits, "!@#$%&"]
9393

9494
chars = []

usefulib/setup_script.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

usefulib/tests.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,66 @@
66
77
88
This is where you should write unit tests for your useful method. If you can't do so for any reason, mention so in your PR so I can help.
9-
"""
9+
"""
10+
11+
import unittest
12+
from _usefulibs import *
13+
14+
class TestUsefulibs(unittest.TestCase):
15+
16+
def setUp(self):
17+
# For now, there are no setup scripts for tests. If you need to add one, remove this comment and add it.
18+
pass
19+
20+
def test_reverse_string(self):
21+
""" @hamdivazim """
22+
23+
self.assertEqual(reverse_string("abc"), "cba")
24+
self.assertEqual(reverse_string("123abcdef456"), "654fedcba321")
25+
26+
def test_loop_dict(self):
27+
""" @hamdivazim """
28+
test_dict = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}
29+
30+
self.assertEqual(loop_dict(test_dict), [("a", 1, 0),("b", 2, 1),("c", 3, 2),("d", 4, 3),("e", 5, 4),])
31+
32+
index = 0
33+
for key, val, i in loop_dict(test_dict):
34+
if index == 1:
35+
self.assertEqual(key, "b")
36+
self.assertEqual(val, 2)
37+
self.assertEqual(i, 1)
38+
39+
index += 1
40+
41+
def test_find_nth_root(self):
42+
""" @hamdivazim """
43+
self.assertEqual(find_nth_root(8, 3), 2)
44+
self.assertEqual(find_nth_root(729, 6), 3)
45+
self.assertEqual(find_nth_root(4, -2), 0.5)
46+
self.assertRaises(ValueError, find_nth_root, -4, -2)
47+
48+
def test_filter_by_string(self):
49+
""" @hamdivazim """
50+
lst = ["I really do love apples!", "Bananas are disgusting!", "An apple a day keeps the doctor away!", "You want a pear?"]
51+
lst2 = [1, "I really do love apples!", "Bananas are disgusting!", "An apple a day keeps the doctor away!", "You want a pear?"]
52+
53+
self.assertEqual(filter_by_string(lst, "apple")[1], "An apple a day keeps the doctor away!")
54+
self.assertRaises(TypeError, filter_by_string, lst2, "apple")
55+
56+
def test_filter_by_condition(self):
57+
""" @hamdivazim """
58+
self.assertEqual(filter_by_condition([0,1,2,3,4,5,6,7,8,9,10], "i % 2 == 0"), [0, 2, 4, 6, 8, 10])
59+
self.assertEqual(filter_by_condition(["123abc", "456def", "123456"], '"123" in i'), ["123abc", "123456"])
60+
61+
def test_generate_random_string(self):
62+
""" @hamdivazim """
63+
self.assertNotEqual(generate_random_string(18), generate_random_string(18))
64+
65+
def test_generateUUID(self):
66+
""" @hamdivazim """
67+
self.assertNotEqual(generateUUID(), generateUUID())
68+
69+
70+
if __name__ == "__main__":
71+
unittest.main()

0 commit comments

Comments
 (0)