-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
139 lines (109 loc) · 3.13 KB
/
utils.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 19 23:10:20 2020
@author: Smit
"""
import sys
from collections import defaultdict
from pickle import dump, load
def drawProgressBar(current, total, string = '', barLen = 20):
'''
Draws a progress bar, like [====> ] 40%
Parameters
------------
current: int/float
Current progress
total: int/float
The total from which the current progress is made
string: str
Additional details to write along with progress
barLen: int
Length of progress bar
'''
percent = current/total
arrow = ">"
if percent == 1:
arrow = ""
# Carriage return, returns to the begining of line to owerwrite
sys.stdout.write("\r")
sys.stdout.write("Progress: [{:<{}}] {}/{}".format("=" * int(barLen * percent) + arrow,
barLen, current, total) + string)
sys.stdout.flush()
def get_max_keys(dictionary):
'''
Returns all keys with maximum value in a dictionary
Parameters
------------
dictionary: dict
Dictionary to get the max keys from
'''
max_value = max(dictionary.values())
keys = []
for key, val in dictionary.items():
if val == max_value:
keys.append(key)
return keys
def is_whitespace(char):
'''
Checks if the character is a whitespace
Parameters
--------------
char: str
A single character string to check
'''
# ord() returns unicode and 0x202F is the unicode for whitespace
if char == " " or char == "\t" or char == "\r" or char == "\n" or ord(char) == 0x202F:
return True
else:
return False
def is_punct(char):
'''
Checks if the character is a punctuation
Parameters
--------------
char: str
A single character string to check
'''
if char == "." or char == "," or char == "!" or char == "?" or char == '\\':
return True
else:
return False
def get_frequency(items):
'''
Returns a dictionary with item:frequency pair
Parameters
-------------
items: list, numpy array or an iterable
A list of items to calculate frequency
'''
freq = defaultdict(int)
for i in items:
freq[i] += 1
return freq
def save_pickle(file, variable):
'''
Saves variable as a pickle file
Parameters
-----------
file: str
File name/path in which the variable is to be stored
variable: object
The variable to be stored in a file
'''
if file.split('.')[-1] != "pkl":
file += ".pkl"
with open(file, 'wb') as f:
dump(variable, f)
print("Variable successfully saved in " + file)
def open_pickle(file):
'''
Returns the variable after reading it from a pickle file
Parameters
-----------
file: str
File name/path from which variable is to be loaded
'''
if file.split('.')[-1] != "pkl":
file += ".pkl"
with open(file, 'rb') as f:
return load(f)