Skip to content

Commit f7cfc5d

Browse files
Add initial useful methods
1 parent bbcd4fd commit f7cfc5d

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

usefulib/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
usefulib v1.0
3+
Copyright Hamd Waseem (https://github.com/hamdivazim) under the GNU Public License 3.0.
4+
5+
https://github.com/hamdivazim/usefulib
6+
"""
7+
8+
__version__ = "1.0.0"
9+
10+
from _usefulibs import *

usefulib/_usefulibs.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""
2+
usefulib v1.0
3+
Copyright Hamd Waseem (https://github.com/hamdivazim) under the GNU Public License 3.0.
4+
5+
https://github.com/hamdivazim/usefulib
6+
7+
Add your useful method here if you are contributing. Remember to add unit tests in tests.py :)
8+
"""
9+
10+
def reverse_string(string):
11+
""" @hamdivazim - Reverses a string. """
12+
13+
return string[::-1]
14+
15+
def loop_dict(dictionary):
16+
"""
17+
@hamdivazim - Allows you to loop through a dictionary while having access to its value, key and current index.
18+
19+
Example:
20+
21+
```
22+
my_dict = {'a':1, 'b':2, 'c':3}
23+
24+
for key, val, i in loop_dict(my_dict):
25+
# Stuff here
26+
```
27+
"""
28+
29+
result = []
30+
i = 0
31+
32+
for key in dictionary.keys():
33+
result += (key, dictionary[key], i)
34+
35+
i += 1
36+
37+
return result
38+
39+
def find_nth_root(num, n):
40+
""" @hamdivazim - Returns the nth root of a number you provide. """
41+
42+
return num ** (1 / n)
43+
44+
def filter_by_string(lst, string):
45+
"""
46+
@hamdivazim - Filters a list based on whether elements contain a specific string.
47+
Probably best used when filtering by a search query.
48+
"""
49+
50+
result = []
51+
52+
for element in lst:
53+
try:
54+
if string in element:
55+
result += element
56+
except TypeError:
57+
raise TypeError("A non-string value was found while using the usefulib.filter_by_string method. Maybe try using filter_by_condition?")
58+
59+
return result
60+
61+
def filter_by_condition(lst, condition: str):
62+
"""
63+
@hamdivazim - Filters a list based on a specific condition.
64+
65+
How to use:
66+
condition property is a string. If you wanted to filter a list based on whether the element (suppose is going to be an integer) was even or not, you would:
67+
68+
```
69+
lst = [0,1,2,3,4,5,6,7,8,9,10]
70+
71+
new_lst = usefulib.filter_by_condition(lst, "i % 2 == 0")
72+
```
73+
74+
The variable `i` is the element.
75+
"""
76+
77+
result = []
78+
79+
for i in lst:
80+
exec(f"if({condition}):result+=i")
81+
82+
return result

usefulib/tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
usefulib v1.0
3+
Copyright Hamd Waseem (https://github.com/hamdivazim) under the GNU Public License 3.0.
4+
5+
https://github.com/hamdivazim/usefulib
6+
7+
8+
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+
"""

0 commit comments

Comments
 (0)