Skip to content

Commit

Permalink
Initial save and restore functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbydurrett committed Sep 18, 2017
1 parent f6773a7 commit 27f16aa
Show file tree
Hide file tree
Showing 3 changed files with 273 additions and 1 deletion.
99 changes: 98 additions & 1 deletion myplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,93 @@
ylabel4 = "NOT DEFINED"
yticksuffix = None
numticks = 25

# flag set to true when doing graph from saved data

restoring_data = False

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import util
import myplot

def save_data(plot_name):
"""
Saves the data for a graph so that it can be redrawn later.
"""

# Exit if you are displaying an image from saved data
# No need to save the saved data again.

if restoring_data:
return

save_file=util.open_save_file()

# These are the global variables in myplot that are used
# to draw a graph. Saving all of the information that the
# graph functions would use.

util.save_string(save_file,plot_name)

util.save_date_times(save_file,xdatetimes)

util.save_string_list(save_file,xlabels)

util.save_list_list_nums(save_file,ylists)

util.save_string_list(save_file,ylistlabels)

util.save_string(save_file,title)
util.save_string(save_file,filename)
util.save_string(save_file,ylabel1)
util.save_string(save_file,ylabel2)
util.save_string(save_file,ylabel3)
util.save_string(save_file,ylabel4)
util.save_string(save_file,yticksuffix)

util.close_file(save_file)

def restore_data(file_name):
"""
Restores the data for a graph so that it can be redrawn.
Returns name of plot like line.
"""

# Set flag so that graph routines know that they are graphing restored data

myplot.restoring_data = True

restore_file=util.open_restore_file(file_name)

# Restores the values of the myplot global variables just as they would be
# if pulled from the database for a graph.

plot_name = util.restore_string(restore_file)

myplot.xdatetimes = util.restore_date_times(restore_file)

myplot.xlabels = util.restore_string_list(restore_file)

myplot.ylists = util.restore_list_list_nums(restore_file)

myplot.ylistlabels = util.restore_string_list(restore_file)

myplot.title = util.restore_string(restore_file)
myplot.filename = util.restore_string(restore_file)
myplot.ylabel1 = util.restore_string(restore_file)
myplot.ylabel2 = util.restore_string(restore_file)
myplot.ylabel3 = util.restore_string(restore_file)
myplot.ylabel4 = util.restore_string(restore_file)
myplot.yticksuffix = util.restore_string(restore_file)

util.close_file(restore_file)

return plot_name

def my_colors(colornum):
"""
Expand Down Expand Up @@ -204,6 +286,10 @@ def stacked_bar():
if len(xlabels) == 0:
print("No results to graph")
return

# Save data to redraw plot later

save_data('stacked_bar')

# set the screen title, size, density

Expand Down Expand Up @@ -283,6 +369,10 @@ def line():
a variable number of plots.
"""

# Save data to redraw plot later

save_data('line')

# set the screen title, size, density

Expand Down Expand Up @@ -331,6 +421,10 @@ def line_2subplots():
two subplots.
"""

# Save data to redraw plot later

save_data('line_2subplots')

# set the screen title, size, density

Expand Down Expand Up @@ -383,12 +477,15 @@ def line_2subplots():

return


def line_4subplots():
"""
Four subplots
"""

# Save data to redraw plot later

save_data('line_4subplots')

# set the screen title, size, density

Expand Down
51 changes: 51 additions & 0 deletions show_saved.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
PythonDBAGraphs: Graphs to help with Oracle Database Tuning
Copyright (C) 2016 Robert Taft Durrett (Bobby Durrett)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Contact:
[email protected]
show_saved.py
Restores saved data and displays graph.
"""

import myplot
import util

# Get full path to the .txt file that was saved when the graph was produced

file_name = util.input_no_default('Enter name of data file to be restored: ')

# Restore data into all of the myplot module global variables that are used
# by the different graphs

plot_name = myplot.restore_data(file_name)

# Call the graphing routine that was used for this plot

if plot_name == 'stacked_bar':
myplot.stacked_bar()
elif plot_name == 'line':
myplot.line()
elif plot_name == 'line_2subplots':
myplot.line_2subplots()
elif plot_name == 'line_4subplots':
myplot.line_4subplots()
else:
print('Invalid plot_name = '+plot_name)
124 changes: 124 additions & 0 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import db
import argparse
import locale
import datetime

"""
Expand Down Expand Up @@ -203,3 +204,126 @@ def exit_no_results(results):
if results == None:
print("No results returned")
sys.exit()

# data saving routines

def open_save_file():
"""
Opens a file in the same directory where we save images.
Returns the file object.
"""
file_name = output_dir+myplot.title+'.txt'
print("Saving data in "+file_name)
save_file = open(file_name, 'w')
return save_file

def save_string(save_file,mystring):
"""
Writes a string to the output file.
Converts None to 'None' and adds newline.
The rest of the save routines call this routine to write
strings on each line.
"""
if mystring == None:
mystring = 'None'
save_file.write(mystring+'\n')

def save_date_times(save_file,my_date_times):
"""
Saves a list of datatime objects. Uses repr to get a string
representation.
Saves number of list items first.
"""
save_string(save_file,str(len(my_date_times)))
for dt in my_date_times:
save_string(save_file,repr(dt))

def save_list_list_nums(save_file,my_list_list_nums):
"""
Saves a list of lists of floats.
Saves the number of lists and then for each list saves
the number of list members and the members themselves.
"""
save_string(save_file,str(len(my_list_list_nums)))
for l in my_list_list_nums:
save_string(save_file,str(len(l)))
for n in l:
save_string(save_file,str(n))

def save_string_list(save_file,my_string_list):
"""
Saves a list of strings starting with the number
of list members.
"""
save_string(save_file,str(len(my_string_list)))
for s in my_string_list:
save_string(save_file,s)

def close_file(save_file):
save_file.close()

# data restoration routines

def open_restore_file(file_name):
"""
Opens a file with saved graph data for reading.
"""
restore_file = open(file_name, 'r')
return restore_file

def restore_string(restore_file):
"""
Reads one string taking off newlines if they exist and
converting 'None' to None.
"""
mystring = restore_file.readline()
if mystring[len(mystring)-1:]=='\n':
mystring = mystring[0:-1]
if mystring == 'None':
mystring = None

return mystring

def restore_date_times(restore_file):
"""
Returns a list of data time objects.
Since it uses the dangerous eval function it
checks that the lines start with datetime.datetime as
they would if they were saved by the save routines.
"""
num_entries = int(restore_string(restore_file))
dt_list = []
for entry in range(num_entries):
dt_string = restore_string(restore_file)
if dt_string[0:17] != 'datetime.datetime':
print("Possible data corruption")
sys.exit()
else:
dt_list.append(eval(dt_string))
return dt_list

def restore_list_list_nums(restore_file):
"""
Restores a list of lists of floats
"""
num_lists = int(restore_string(restore_file))
list_list=[]
for l in range(num_lists):
new_list=[]
list_size=int(restore_string(restore_file))
for n in range(list_size):
new_list.append(float(restore_string(restore_file)))
list_list.append(new_list)
return list_list

def restore_string_list(restore_file):
"""
Restores a list of strings.
"""
list_size = int(restore_string(restore_file))
new_list=[]
for n in range(list_size):
new_list.append(restore_string(restore_file))
return new_list

0 comments on commit 27f16aa

Please sign in to comment.