Skip to content

Commit f4a7f1f

Browse files
Add new usefulib
1 parent 41bdd9f commit f4a7f1f

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,6 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
# usefulib temp data
132+
temp_data/

usefulib/_usefulibs.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import random # generate_random_string()
1313
import string # generate_random_string()
1414
import uuid # generateUUID()
15+
import os # external_verbose_output()
1516

1617
""""""
1718

@@ -89,6 +90,8 @@ def filter_by_condition(lst, condition: str):
8990
return result
9091

9192
def generate_random_string(word_length=18):
93+
""" @hamdivazim - generates a random string using ASCII chars, digits and special chars. """
94+
9295
components = [string.ascii_letters, string.digits, "!@#$%&"]
9396

9497
chars = []
@@ -106,6 +109,8 @@ def generate_random_string(word_length=18):
106109
return "".join(result)
107110

108111
def generateUUID(version=4):
112+
""" @hamdivazim - generates a UUID using version 4 by default but you can choose. """
113+
109114
if version < 1 or version > 5:
110115
raise ValueError(f"{version} is not a valid UUID version number (valid values are 1-5).")
111116
else:
@@ -118,4 +123,14 @@ def generateUUID(version=4):
118123
elif version == 4:
119124
return uuid.uuid4()
120125
elif version == 5:
121-
return uuid.uuid5()
126+
return uuid.uuid5()
127+
128+
def external_verbose_output(data, path="data.log"):
129+
""" @hamdivazim - if you are printing a lot of data, you can use this method to write the output to log file. """
130+
131+
if not os.path.exists(path):
132+
open(path, "x").close()
133+
134+
with open(path, "w") as f:
135+
f.write("# Logged by usefulibs.external_verbose_output()\n\n")
136+
f.write(data)

usefulib/tests.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def test_find_nth_root(self):
4747

4848
def test_filter_by_string(self):
4949
""" @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?"]
50+
lst = ["I really do love apples!", "Bananas are nice!", "An apple a day keeps the doctor away!", "You want a pear?"]
51+
lst2 = [1, "I really do love apples!", "Bananas are nice!", "An apple a day keeps the doctor away!", "You want a pear?"]
5252

5353
self.assertEqual(filter_by_string(lst, "apple")[1], "An apple a day keeps the doctor away!")
5454
self.assertRaises(TypeError, filter_by_string, lst2, "apple")
@@ -65,6 +65,14 @@ def test_generate_random_string(self):
6565
def test_generateUUID(self):
6666
""" @hamdivazim """
6767
self.assertNotEqual(generateUUID(), generateUUID())
68+
self.assertRaises(ValueError, generateUUID, 6)
69+
70+
def test_external_verbose_output(self):
71+
""" @hamdivazim """
72+
external_verbose_output("Test Data\n1 2 3\na b c", R"usefulib\temp_data\ext_verbose_test.log")
73+
74+
with open(R"usefulib\temp_data\ext_verbose_test.log", "r") as f:
75+
self.assertEqual(f.read(), "# Logged by usefulibs.external_verbose_output()\n\nTest Data\n1 2 3\na b c")
6876

6977

7078
if __name__ == "__main__":

0 commit comments

Comments
 (0)