You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
defreverse_string(string):
11
+
""" @hamdivazim - Reverses a string. """
12
+
13
+
returnstring[::-1]
14
+
15
+
defloop_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
+
forkeyindictionary.keys():
33
+
result+= (key, dictionary[key], i)
34
+
35
+
i+=1
36
+
37
+
returnresult
38
+
39
+
deffind_nth_root(num, n):
40
+
""" @hamdivazim - Returns the nth root of a number you provide. """
41
+
42
+
returnnum** (1/n)
43
+
44
+
deffilter_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
+
forelementinlst:
53
+
try:
54
+
ifstringinelement:
55
+
result+=element
56
+
exceptTypeError:
57
+
raiseTypeError("A non-string value was found while using the usefulib.filter_by_string method. Maybe try using filter_by_condition?")
58
+
59
+
returnresult
60
+
61
+
deffilter_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")
0 commit comments