-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySysUtils.py
More file actions
111 lines (85 loc) · 3.02 KB
/
MySysUtils.py
File metadata and controls
111 lines (85 loc) · 3.02 KB
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
import sys
import re
import pickle
import os
import numpy as np
import numpy.ma as ma
from IPython import get_ipython
from jupyter_client.blocking import BlockingKernelClient
def filedrive():
if sys.platform == 'linux':
filedrive = '/'
else:
filedrive = 'F:/'
return filedrive
def phd_folder():
"""Path to my PhD folder including .../PhD/"""
if sys.platform == 'linux':
phd_folder = '/home/hugke729/PhD/'
else:
phd_folder = 'C:/Users/Ken/Documents/PhD/'
return phd_folder
def replace(file, pattern, subst):
# Read contents from file as a single string
file_handle = open(file, 'r')
file_string = file_handle.read()
file_handle.close()
# Use RE package to allow for replacement
# (also allowing for (multiline) REGEX)
file_string = (re.sub(pattern, subst, file_string))
# Write contents to file.
# Using mode 'w' truncates the file.
file_handle = open(file, 'w')
file_handle.write(file_string)
file_handle.close()
def rm_var_except(except_list, ns=locals()):
"""remove all variables except those given in except_list"""
# use Ipython's who_ls to create a list containing only the variables that
# I actually interact with
mgc = get_ipython().magic
all_vars = mgc(
'who_ls ndarray list dict int str float bool tuple int32 int64')
for item in all_vars:
if item in except_list:
# Do nothing
pass
else:
exec('del(' + item + ')', globals(), ns)
def unpickle2(pickle_file):
"""unpickle a file created with python 2 into python 3"""
f = open(pickle_file, 'rb')
D = pickle.load(f, encoding='latin1')
f.close()
return D
def rmvar(var_list):
for i in var_list:
try:
del(i)
except:
pass
def preall(array_shape, copies=1, initial_value=0, mask=False):
"""simulataneously preallocate zero array(s) of identical shape
returns a tuple of n='copies' of initial_value arrays of shape array_shape
If copies is 1, only the array is returned (i.e. not as a tuple)
If mask is True, convert to masked_array"""
array_tuple = ()
for _ in range(copies):
empty_array = np.full(array_shape, float(initial_value))
if mask:
empty_array = ma.masked_invalid(empty_array)
# addition with tuples appends the last value
array_tuple += (empty_array, )
return array_tuple[0] if copies == 1 else array_tuple
def merge_dicts(x, y):
"""Given two dicts, merge them into a new dict as a shallow copy."""
z = x.copy()
z.update(y)
return z
def run_in_existing_kernel(cmd):
kernel_dir = '/run/user/1000/jupyter/'
connection_file = os.listdir(kernel_dir)[0]
connection_file = kernel_dir + connection_file
kc = BlockingKernelClient(connection_file=connection_file)
kc.load_connection_file()
kc.start_channels()
kc.execute(cmd)