-
Notifications
You must be signed in to change notification settings - Fork 0
/
matlablib.py
148 lines (106 loc) · 4.48 KB
/
matlablib.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
140
141
142
143
144
145
146
147
148
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt
import time
import IPython
from IPython import embed
# learn where site packages are stored! ref: https://stackoverflow.com/questions/122327/how-do-i-find-the-location-of-my-python-site-packages-directory
# python -c "import site; print(site.getsitepackages())"
# ref: https://stackoverflow.com/questions/5849800/what-is-the-python-equivalent-of-matlabs-tic-and-toc-functions
def TicTocGenerator():
# Generator that returns time differences
ti = 0 # initial time
tf = time.time() # final time
while True:
ti = tf
tf = time.time()
yield tf - ti # returns the time difference
TicToc = TicTocGenerator() # create an instance of the TicTocGen generator
# This will be the main function through which we define both tic() and toc()
def toc(tempBool=True):
# Prints the time difference yielded by generator instance TicToc
tempTimeInterval = next(TicToc)
if tempBool:
print("Elapsed time: %f seconds.\n" % tempTimeInterval)
def tic():
# Records a time in TicToc, marks the beginning of a time interval
toc(False)
def closefigures(plt):
a = [plt.close(x) for x in range(1, 100)] # bit dumb..how can i cound total # of figures?
def quittin_early(): # TODO: add to matlablib
''' can drop to the python console without barfing a bunch of errors
preserves the namespace, and you can resume the script the way it
was running before
ref: https://stackoverflow.com/questions/27911570/can-you-specify-a-command-to-run-after-you-embed-into-ipython
nb; you could just import IPython, then call embed(), but this way I can setup any IPython magics here, which need to be
reloaded in this intstance. Variables persist, but the magics do not.
'''
import IPython
from pkg_resources import parse_version # installed with setuptools
if parse_version(IPython.release.version) >= parse_version('4.0.0'):
from traitlets.config import Config
else:
import IPython.config
from IPython.config import Config
c = Config()
c.InteractiveShellApp.exec_lines = [
"%load_ext autoreload",
"%autoreload 2",
"print('quittin early')"
]
IPython.start_ipython(config=c)
# known issue: In [10]: ERROR! Session/line number was not unique in database. History logging moved to new session 202
# just disregard - only the history that is messed up
def cc():
import matplotlib.pyplot
from IPython.testing.globalipapp import get_ipython # https://pmbaumgartner.github.io/blog/testing-ipython-magics/
ip = get_ipython()
matplotlib.pyplot.close("all")
# IPython.get_ipython().run_line_magic('reset', " -f in") # ref: https://ipython.readthedocs.io/en/stable/interactive/magics.html
# ip.run_line_magic('reset', " -f in") # short form works
ip.run_line_magic('reset', '-f ') # short form works
def add3nums(a, b, c):
return (a + b + c)
def quit_early():
raise Exception('Halt', 'drop to keyboard')
def keyboard():
## DROP TO KEYBOARD in the middle of a script. If you have iPYTHON SETUP can do this.
# Ref: https://stackoverflow.com/questions/2158097/drop-into-python-interpreter-while-executing-function
embed()
# IPython.embed(header='WELCOME, keyboard() (exit to go back)',local_ns=locals(), global_ns=locals())
# sys.tracebacklimit = 0
def kill_background():
# Kill python background tasks if gets hung up in pycharm
import os
os.system('tskill python')
def kill():
kill_background()
def SayHey():
print('hey')
class MyError(
Exception): # https://stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions-in-modern-python
def __init__(self):
# self.message = message
# self.animal = animal
pass
def __str__(self):
return self.message
def _render_traceback_(self):
print('ya got me')
def __repr__(self):
print('repr1')
# NO IDEA ABOUT THIS
# def showtraceback(self):
# traceback_lines = traceback.format_exception(*sys.exc_info())
# del traceback_lines[1]
# message = ''.join(traceback_lines)
# sys.stderr.write(message)
#
# import sys
# import traceback
# import IPython
# IPython.core.interactiveshell.InteractiveShell.showtraceback = showtraceback
def getPythonVarables():
import os
print(os.environ) # Print a dict of env vars and their values
# os.environ["PYTHONPATH"] # Query a specific env var