Skip to content

Commit

Permalink
pep-ified code
Browse files Browse the repository at this point in the history
  • Loading branch information
LanceMaverick committed Jan 27, 2017
1 parent 00c4473 commit bb26bf1
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 100 deletions.
116 changes: 59 additions & 57 deletions beards/teleplot/TelePlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,73 @@
import mpmath as mt
import re
import numpy as np

trig_dict = {'sin' : mt.sin, 'cos' : mt.cos, 'tan' : mt.tan,
'asin' : mt.asin, 'acos' : mt.acos, 'atan' : mt.tan,
'cosec' : mt.csc, 'sec' : mt.sec, 'cot' : mt.cot,
'cospi' : mt.cospi, 'sinpi' : mt.sinpi, 'sinc' : mt.sinc}
hyp_dict = {'sinh' : mt.sinh, 'cosh' : mt.cosh, 'tanh' : mt.tan,
'asinh' : mt.asinh, 'acosh' : mt.acosh, 'atanh' : mt.tanh,
'cosech' : mt.csch, 'sech' : mt.sech, 'coth' : mt.coth}

logInd_dict = {'log' : mt.log, 'exp' : mt.exp, 'log10' : mt.log10}
trig_dict = {'sin': mt.sin, 'cos': mt.cos, 'tan': mt.tan,
'asin': mt.asin, 'acos': mt.acos, 'atan': mt.tan,
'cosec': mt.csc, 'sec': mt.sec, 'cot': mt.cot,
'cospi': mt.cospi, 'sinpi': mt.sinpi, 'sinc': mt.sinc}
hyp_dict = {'sinh': mt.sinh, 'cosh': mt.cosh, 'tanh': mt.tan,
'asinh': mt.asinh, 'acosh': mt.acosh, 'atanh': mt.tanh,
'cosech': mt.csch, 'sech': mt.sech, 'coth': mt.coth}

others_dict = {'sqrt' : mt.sqrt, 'cbrt' : mt.cbrt, 'root' : mt.root,
'power' : mt.power, 'expm1' : mt.expm1,
'fac' : mt.factorial, 'fac2' : mt.fac2, 'gamma' : mt.gamma,
'rgamma' : mt.gamma, 'loggamma' : mt.loggamma,
'superfac' : mt.superfac, 'hyperfac' : mt.hyperfac,
'barnesg' : mt.barnesg, 'psi' : mt.psi,
'harmonic' : mt.harmonic}
logInd_dict = {'log': mt.log, 'exp': mt.exp, 'log10': mt.log10}

others_dict = {'sqrt': mt.sqrt, 'cbrt': mt.cbrt, 'root': mt.root,
'power': mt.power, 'expm1': mt.expm1,
'fac': mt.factorial, 'fac2': mt.fac2, 'gamma': mt.gamma,
'rgamma': mt.gamma, 'loggamma': mt.loggamma,
'superfac': mt.superfac, 'hyperfac': mt.hyperfac,
'barnesg': mt.barnesg, 'psi': mt.psi,
'harmonic': mt.harmonic}

eqn_parser = {}
eqn_parser.update(trig_dict)
eqn_parser.update(hyp_dict)
eqn_parser.update(logInd_dict)
eqn_parser.update(others_dict)
eqn_parser.update(others_dict)


def checkandParse(string):
opts2perform = {}
for key in eqn_parser:
regex_str = '({})\(([xy\+\-\/\*\.\d]+)\)'.format(key)
res = re.findall(regex_str, string)
if len(res) > 0:
opts2perform[key] = res[0]
return opts2perform
opts2perform = {}
for key in eqn_parser:
regex_str = '({})\(([xy\+\-\/\*\.\d]+)\)'.format(key)
res = re.findall(regex_str, string)
if len(res) > 0:
opts2perform[key] = res[0]
return opts2perform


class TelePlot:
def __init__(self, plot_type='scatter', style='default'):
self.plotType = plot_type
self.plotStyle = style
self.xlabel = 'x'
self.ylabel = 'y'
self.x = []
self.y = []
def parseOpts(self, in_array):
range_ = [-10, 10]
for opts_tuple in in_array:
if 'xaxis' in opts_tuple[0]:
print("Got Axis title for x of " + opts_tuple[1])
self.xlabel = opts_tuple[1]
if 'yaxis' in opts_tuple[0]:
self.ylabel = opts_tuple[1]
if 'range' in opts_tuple[0]:
numbers = opts_tuple[1].split(',')
range_ = numbers
n = 100
if len(range_) > 2:
n = (float(range_[1])-float(range_[0]))/int(range_[2])
self.x = np.linspace(float(range_[0]), float(range_[1]), n)
def savePlot(self):
plt.xlabel(self.xlabel)
plt.ylabel(self.ylabel)
print(self.y)
plt.plot(self.x,self.y)
filename = './{}.png'.format(str(uuid4())[:6])
plt.savefig(filename)
plt.clf()
return filename
def __init__(self, plot_type='scatter', style='default'):
self.plotType = plot_type
self.plotStyle = style
self.xlabel = 'x'
self.ylabel = 'y'
self.x = []
self.y = []

def parseOpts(self, in_array):
n = 100
range_ = [-10, 10]
for opts_tuple in in_array:
if 'xaxis' in opts_tuple[0]:
print("Got Axis title for x of " + opts_tuple[1])
self.xlabel = opts_tuple[1]
if 'yaxis' in opts_tuple[0]:
self.ylabel = opts_tuple[1]
if 'range' in opts_tuple[0]:
numbers = opts_tuple[1].split(',')
range_ = numbers
if len(range_) > 2:
n = (float(range_[1]) - float(range_[0])) / int(range_[2])
self.x = np.linspace(float(range_[0]), float(range_[1]), n)

def savePlot(self):
plt.xlabel(self.xlabel)
plt.ylabel(self.ylabel)
print(self.y)
plt.plot(self.x, self.y)
filename = './{}.png'.format(str(uuid4())[:6])
plt.savefig(filename)
plt.clf()
return filename
87 changes: 44 additions & 43 deletions beards/teleplot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from numpy import linspace
from math import *


def format_msg(msg):
text = msg['text']
return ' '.join(get_args(text)).title()
Expand All @@ -29,48 +30,48 @@ class TelePlotSB(BeardChatHandler):

@onerror
async def makePlot(self, msg):
in_string = msg['text'].replace('/teleplot ', '')
arrays = re.findall(r'(\[[\-\d\,\.\s]+\])+', in_string)
eq_parser = TelePlot.eqn_parser
options = re.findall(r'\-(\w+)\s\"([\w\d\/\s\.\-\'\)\(\,]+)', in_string)

print("options: ")
print(options)
plotter = TelePlot.TelePlot()
plotter.parseOpts(options)
in_string = msg['text'].replace('/teleplot ', '')
arrays = re.findall(r'(\[[\-\d\,\.\s]+\])+', in_string)
eq_parser = TelePlot.eqn_parser
options = re.findall(r'\-(\w+)\s\"([\w\d\/\s\.\-\'\)\(\,]+)', in_string)

if len(arrays) < 1:
opts2perform = TelePlot.checkandParse(in_string)
if len(opts2perform) == 0:
eqn = re.findall(r'(\([\w\(\)\d\s\-\+\*\/]+\))', in_string)
eqn = eqn[0]
for i in plotter.x:
equation = eqn.replace('x','{}'.format(i)).replace('(','').replace(')','')
print(equation)
j = sp.simplify(equation)
plotter.y.append(j)
else:
strings_for_y = []
for i in plotter.x:
res = re.findall(r'\(([\w\*\d\+\/\-\.\,]+)\)', in_string)[0]
final_string = res[0]
for key in opts2perform:
inside = opts2perform[key][1].replace('x','{}'.format(i))
inside = sp.simplify(inside)
operation = eq_parser[key](inside)
final_string = final_string.replace('x'.format(opts2perform[key][0], opts2perform[key][1]), '{}'.format(operation))
print(final_string)
strings_for_y.append(final_string)
for y in strings_for_y:
j = sp.simplify(y)
plotter.y.append(j)
print("options: ")
print(options)
plotter = TelePlot.TelePlot()
plotter.parseOpts(options)

else:
print("Arrays: ")
assert len(arrays) == 2, "Error: Insufficient Number of Arrays Given."
X = re.findall(r'([\d\.\-]+)', arrays[0])
Y = re.findall(r'([\d\.\-]+)', arrays[1])
print(X,Y)
file_name = plotter.savePlot()
await self.sender.sendPhoto(('temp.png', open('{}'.format(file_name), 'rb')))
os.remove('{}'.format(file_name))
if len(arrays) < 1:
opts2perform = TelePlot.checkandParse(in_string)
if len(opts2perform) == 0:
eqn = re.findall(r'(\([\w\(\)\d\s\-\+\*\/]+\))', in_string)
eqn = eqn[0]
for i in plotter.x:
equation = eqn.replace('x', '{}'.format(i)).replace('(', '').replace(')', '')
print(equation)
j = sp.simplify(equation)
plotter.y.append(j)
else:
strings_for_y = []
for i in plotter.x:
res = re.findall(r'\(([\w\*\d\+\/\-\.\,]+)\)', in_string)[0]
final_string = res[0]
for key in opts2perform:
inside = opts2perform[key][1].replace('x', '{}'.format(i))
inside = sp.simplify(inside)
operation = eq_parser[key](inside)
final_string = final_string.replace('x'.format(opts2perform[key][0], opts2perform[key][1]), '{}'.format(operation))
print(final_string)
strings_for_y.append(final_string)
for y in strings_for_y:
j = sp.simplify(y)
plotter.y.append(j)

else:
print("Arrays: ")
assert len(arrays) == 2, "Error: Insufficient Number of Arrays Given."
X = re.findall(r'([\d\.\-]+)', arrays[0])
Y = re.findall(r'([\d\.\-]+)', arrays[1])
print(X, Y)
file_name = plotter.savePlot()
await self.sender.sendPhoto(('temp.png', open('{}'.format(file_name), 'rb')))
os.remove('{}'.format(file_name))

0 comments on commit bb26bf1

Please sign in to comment.