Commit 8d208bce authored by Yori 'AGy' Fournier's avatar Yori 'AGy' Fournier
Browse files

first commit for the module design:

the structure is ready but not yet organized

AxFullDesign
FigFullDesign
myDesignTool
parent f5824412
from myIOs import readStupidData, readStupidData2
from axTest1 import AxTest1
from axTest1 import AxDesign
from axTest1 import AxFullDesign
from figTest import FigTest2
from figTest import FigDesing
from figTest import FigFullDesing
from tests import myTest
......
......@@ -42,7 +42,7 @@
#
#
# IMPORT ---------------------------------------------------------------
from .. import SEVR, DBUG
from .. import SEVR, DBUG, INFO
from .. import MyAxes
......@@ -77,3 +77,46 @@ class AxTest1(MyAxes):
print(DBUG + 'I formatted the raw data!')
return(True)
class AxDesign(MyAxes):
def plotting(self):
f = open('./axplotting.input', 'r')
for line in f.readlines():
print(INFO + str(line))
exec(line)
f.close()
return(True)
def formatRawData(self, rawdata):
self.data = {'xdata' : [0, 1],
'ydata' : [0, 1]}
return(True)
class AxFullDesign(MyAxes):
def __init__(self, fig, emulatedAxes, ratio, frameRect, *args, **kwargs):
self.emulatedAxes = emulatedAxes
MyAxes.__init__(self, fig, ratio, frameRect, *args, **kwargs)
def plotting(self):
exec(''.join(self.fig.designSections[self.emulatedAxes]['plotting']))
return(True)
def formatRawData(self, rawdata):
exec(''.join(self.fig.designSections[self.emulatedAxes]['formatRawData']))
return(True)
# IMPORT --------------------------------------------------------------
YOU NEED TO DO THAT YOURSELF!
# CLASS FigTest========================================================
class FigTest(MyFig):
# DECLARE KEYWORDS ------------------------------------------------
def declareKeywords(self):
self.keywords = {'xrange': [0, 5],
'yrange': [0, 10]}
# ADD AXES --------------------------------------------------------
def addAxes(self):
ratio = 3./8.
frame = [0., 0., 1.0, 0.5]
frame2 = [0., 0.5, 1.0, 0.5]
self.add_axes(AxFullDesign(self, 'AxTest', ratio, frame))
self.add_axes(AxFullDesign(self, 'AxTest2', ratio, frame2))
# IMPORT --------------------------------------------------------------
YOU NEED TO DO THAT YOURSELF!
# CLASS AxTest=========================================================
class AxTest(MyAxes):
# FORMAT RAWDATA --------------------------------------------------
def formatRawData(self):
print("AxTest::formatRawData")
self.data = {'xdata': [0., 5.],
'ydata': [1., 6.]}
# PLOTTING --------------------------------------------------------
def plotting(self):
print("AxTest::plotting")
self.set_xlim(self.fig.keywords['xrange'])
self.set_ylim(self.fig.keywords['yrange'])
self.plot(self.data['xdata'], self.data['ydata'])
# IMPORT --------------------------------------------------------------
YOU NEED TO DO THAT YOURSELF!
# CLASS AxTest2========================================================
class AxTest2(MyAxes):
# FORMAT RAWDATA --------------------------------------------------
def formatRawData(self):
print("AxTest2::formatRawData")
self.data = {'xdata': [5., 0.],
'ydata': [1., 6.]}
# PLOTTING --------------------------------------------------------
def plotting(self):
print("AxTest::plotting")
self.set_xlim(self.fig.keywords['xrange'])
self.set_ylim(self.fig.keywords['yrange'])
self.plot(self.data['xdata'], self.data['ydata'])
# IMPORT --------------------------------------------------------------
YOU NEED TO DO THAT YOURSELF!
# CLASS FigTest========================================================
class FigTest(MyFig):
# DECLARE KEYWORDS ------------------------------------------------
def declareKeywords(self):
self.keywords = {'xrange': [0, 5],
'yrange': [0, 10]}
# ADD AXES --------------------------------------------------------
def addAxes(self):
ratio = 3./8.
frame = [0., 0., 1.0, 0.5]
frame2 = [0., 0.5, 1.0, 0.5]
self.add_axes(AxFullDesign(self, 'AxTest', ratio, frame))
self.add_axes(AxFullDesign(self, 'AxTest2', ratio, frame2))
# FIGTEST =============================================================
< FigTest::addAxes
ratio = 3./8.
frame = [0., 0., 1.0, 0.5]
frame2 = [0., 0.5, 1.0, 0.5]
self.add_axes(AxFullDesign(self, 'AxTest', ratio, frame))
self.add_axes(AxFullDesign(self, 'AxTest2', ratio, frame2))
/>
# ---------------------------------------------------------------------
< FigTest::declareKeywords
self.keywords = {'xrange': [0, 5],
'yrange': [0, 10]}
/>
# AXTEST ==============================================================
< AxTest::formatRawData
print("AxTest::formatRawData")
self.data = {'xdata': [0., 5.],
'ydata': [1., 6.]}
/>
# ---------------------------------------------------------------------
< AxTest::plotting
print("AxTest::plotting")
self.set_xlim(self.fig.keywords['xrange'])
self.set_ylim(self.fig.keywords['yrange'])
self.plot(self.data['xdata'], self.data['ydata'])
/>
# AXTEST2 ==============================================================
< AxTest2::formatRawData
print("AxTest2::formatRawData")
self.data = {'xdata': [5., 0.],
'ydata': [1., 6.]}
/>
# ---------------------------------------------------------------------
< AxTest2::plotting
print("AxTest::plotting")
self.set_xlim(self.fig.keywords['xrange'])
self.set_ylim(self.fig.keywords['yrange'])
self.plot(self.data['xdata'], self.data['ydata'])
/>
# =====================================================================
......@@ -69,11 +69,13 @@ from .. import os
from .. import DBUG, SEVR, INFO, SPCE, WARN
from .. import MyFig, MyFig_base
from .axTest1 import AxTest1
from .axTest1 import AxTest1, AxDesign, AxFullDesign
D_XRANGE = [-1, 1]
D_YRANGE = [-2, 2]
_D_PATH2DESIGN = './myplotlib/test/design/'
# Class MyFig Overwriting Matplotlib.figure.Figure
class FigTest2(MyFig):
......@@ -95,3 +97,155 @@ class FigTest2(MyFig):
frame = [0.0, 0.0, 1.0, 1.0] # part of the fig that is available
self.add_axes(AxTest1(self, ratio, frame))
class FigDesing(MyFig):
FIGSIZE = (8., 6.)
def addAxes(self):
ratio = 6. / 8. # height/width of the axes (in inch)
frame = [0.0, 0.0, 1.0, 1.0] # part of the fig that is available
self.add_axes(AxDesign(self, ratio, frame))
class FigFullDesing(MyFig):
FIGSIZE = (8., 6.)
def __init__(self, rawdata, designFile, *args, **kwargs):
self.designFile = designFile
self.readDesignFile()
MyFig.__init__(self, rawdata, *args, **kwargs)
def readDesignFile(self):
self.designSections = self.getSectionFromDesign()
# look for the emulated figure
figureFound = False
self.emulatedFigure = None
# for all sections
for className in self.designSections.keys():
# if one section has addAxes then set it to be the figure
if 'addAxes' in self.designSections[className].keys():
if not figureFound:
self.emulatedFigure = str(className)
figureFound = True
else:
print(WARN + "apparently you have two figures in that design file... only the first one will be set up")
if self.emulatedFigure is None:
print(WARN + "No figure found... will exit")
return(False)
return(True)
def getSectionFromDesign(self):
f = open(_D_PATH2DESIGN + str(self.designFile))
designSections = {}
startProcedure = False
className = ''
functionName = ''
procedure = []
for line in f.readlines():
# If recording a procedure
if startProcedure:
# and found the end of it
if line.startswith('/>'):
print(INFO + str(className) + ' ' + str(functionName))
# Save the procedure
if str(className) in designSections.keys():
designSections[str(className)].update({str(functionName): procedure})
else:
designSections.update({str(className): {}})
designSections[str(className)].update({str(functionName): procedure})
# reset
className = ''
functionName = ''
procedure = []
startProcedure = False
# and found bgining of a new one print WARNING
elif line.startswith('<'):
print(WARN + "I found a line starting with '<' before the end of the section, you may have forgot to close it.")
# record the procedure
else:
procedure.append(line)
# if not recording and found begining of a section
if (line.startswith('<')) and (startProcedure is False):
# check the format
if line.startswith('< '):
# get the ClassName and FunctionName
label = line.split(' ')
if len(label) < 2:
print(WARN + "apparently the format you use to declare this section is incrorrect. use: '< ClassName::FunctionName'")
className, functionName = label[1].split('::')
if('\n' in str(className)):
className = className[:-1]
if('\n' in str(functionName)):
functionName = functionName[:-1]
print(INFO + str(className) + ' ' + str(functionName))
procedure = []
# start recording
startProcedure = True
# if format wrong print WARNING
else:
print(WARN + "apparently you forgot a space after opening the section. please strictly use: '< ClassName::FunctionName'")
for className in designSections.keys():
print(INFO + str(className) + ": " + str(designSections[className].keys()))
f.close()
return(designSections)
def addAxes(self):
exec(''.join(self.designSections[self.emulatedFigure]['addAxes']))
def declareKeywords(self):
if 'declareKeywords' in self.designSections[self.emulatedFigure].keys():
exec(''.join(self.designSections[self.emulatedFigure]['declareKeywords']))
else:
pass
def update(self, **kwargs):
status = MyFig.update(self, **kwargs)
if not status: return(False)
status = self.readDesignFile()
return(status)
#_D_PATH2DESIGN = './myplotlib/test/design/'
#_D_PATH2_MYFIGURE = './myplotlib/test/design/'
#_D_PATH2_MYAXES = './myplotlib/test/design/
_D_PATH2DESIGN = './design/'
_D_PATH2_MYFIGURE = './design/'
_D_PATH2_MYAXES = './design/'
WARN = ' > warn-:'
INFO = ' > info-:'
def getSectionFromDesign(designFile):
f = open(_D_PATH2DESIGN + str(designFile))
designSections = {}
startProcedure = False
className = ''
functionName = ''
procedure = []
for line in f.readlines():
# If recording a procedure
if startProcedure:
# and found the end of it
if line.startswith('/>'):
print(INFO + str(className) + ' ' + str(functionName))
# Save the procedure
if str(className) in designSections.keys():
designSections[str(className)].update({str(functionName): procedure})
else:
designSections.update({str(className): {}})
designSections[str(className)].update({str(functionName): procedure})
# reset
className = ''
functionName = ''
procedure = []
startProcedure = False
# and found bgining of a new one print WARNING
elif line.startswith('<'):
print(WARN + "I found a line starting with '<' before the end of the section, you may have forgot to close it.")
# record the procedure
else:
procedure.append(line)
# if not recording and found begining of a section
if (line.startswith('<')) and (startProcedure is False):
# check the format
if line.startswith('< '):
# get the ClassName and FunctionName
label = line.split(' ')
if len(label) < 2:
print(WARN + "apparently the format you use to declare this section is incrorrect. use: '< ClassName::FunctionName'")
className, functionName = label[1].split('::')
if('\n' in str(className)):
className = className[:-1]
if('\n' in str(functionName)):
functionName = functionName[:-1]
print(INFO + str(className) + ' ' + str(functionName))
procedure = []
# start recording
startProcedure = True
# if format wrong print WARNING
else:
print(WARN + "apparently you forgot a space after opening the section. please strictly use: '< ClassName::FunctionName'")
for className in designSections.keys():
print(INFO + str(className) + ": " + str(designSections[className].keys()))
f.close()
return(designSections)
def savePlot(filename):
designSections = getSectionFromDesign(filename)
figureClassName = None
# IDENTIFY THE FIGURE
for className in designSections.keys():
if 'addAxes' in designSections[str(className)].keys():
figureClassName = className
# OBTAIN THE FUNCTIONS
if 'declareKeywords' in designSections[str(figureClassName)].keys():
declareKeywordsString = '\t'.join(designSections[str(figureClassName)]['declareKeywords'])
else:
declareKeywordsString = ''
if 'addAxes' in designSections[str(figureClassName)].keys():
addAxesString = '\t'.join(designSections[str(figureClassName)]['addAxes'])
else:
addAxesString = ''
if figureClassName is None or addAxesString == '' or declareKeywordsString == '':
print("ERROROROROR!!!!!")
print(figureClassName)
print(addAxesString)
print(declareKeywordsString)
# PUT THEM INTO THE STRUCTURE
template = _CLASS_TEMPLATE.format(className=figureClassName,
declareKeywords=declareKeywordsString,
addAxes=addAxesString)
# CREATE THE FIGURE FILE
f = open(_D_PATH2_MYFIGURE + str(figureClassName)[0].lower() + str(figureClassName)[1:] + '.py', 'w')
# WRITE THE FIGURE
f.writelines(template)
f.close()
del designSections[str(figureClassName)]
# IDENTIFY THE AXES
for axes in designSections.keys():
axesClassName = axes
if 'formatRawData' in designSections[axes].keys():
formatRawDataString = '\t'.join(designSections[axes]['formatRawData'])
if 'plotting' in designSections[axes].keys():
plottingString = '\t'.join(designSections[axes]['plotting'])
# OPEN THE FILE
f = open(_D_PATH2_MYAXES + str(axesClassName)[0].lower() + str(axesClassName)[1:] + '.py', 'w')
template = _AXES_TEMPLATE.format(axesName=axesClassName,
formatRawData=formatRawDataString,
plotting=plottingString)
f.writelines(template)
f.close()
_AXES_TEMPLATE = '''# IMPORT --------------------------------------------------------------
YOU NEED TO DO THAT YOURSELF!
# CLASS {axesName:=<63}
class {axesName}(MyAxes):
# FORMAT RAWDATA --------------------------------------------------
def formatRawData(self):
{formatRawData}
# PLOTTING --------------------------------------------------------
def plotting(self):
{plotting}
'''
_CLASS_TEMPLATE = '''# IMPORT --------------------------------------------------------------
YOU NEED TO DO THAT YOURSELF!
# CLASS {className:=<63}
class {className}(MyFig):
# DECLARE KEYWORDS ------------------------------------------------
def declareKeywords(self):
{declareKeywords}
# ADD AXES --------------------------------------------------------
def addAxes(self):
{addAxes}
'''
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment