-
Notifications
You must be signed in to change notification settings - Fork 0
/
general.py
executable file
·52 lines (44 loc) · 1.38 KB
/
general.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
"""
These are simple functions that I like to use across different projects
These allow me to reuse my code and be more effective.
They aren't anything too special, but they are useful
"""
import pickle
def chunks(l,n=5):
'''
params:
l: taks in a list (or list like object)
n: takes an int: default = 5 this is how big the smaller
chunks will be
returns:
a list of lists with the smaller lists being size n
'''
return [l[i:i + n] for i in range(0, len(l), n)]
def pickle_write(file_name,data):
'''
takes a filename and data and writes it to pickle
'''
with open(file_name, 'wb') as fid:
pickle.dump(data, fid, pickle.HIGHEST_PROTOCOL)
def pickle_read(file_name):
'''
takes a filename and reads from pickle
'''
with open(file_name,'rb') as f:
data=pickle.load(f)
return data
def replace_text(text,
delimiter,
location,
new_text):
'''
delimit_change_join
takes in a string, a delimiter, location (to change the string), and
new text to replace at the desired location
this funciton splits,changes the desired text (at the proper location) to
the new_text variable.
it then returns a joined string
'''
temp = text.split(delimiter)
temp[location] = new_text
return delimiter.join(temp)