Example of XML Logs

There is a frequent need to track an activity in time. There are many ways to do this, but in general a value and time is kept in a table. pyMeausure has an XML class to help with data storage and manipulation.

In [1]:
# import pyMez
from pyMez import *
Importing pyMez, this should take roughly 30 seconds
The module smithplot was not found,please put it on the python path
The module smithplot was not found,please put it on the python path

To create a new log just call the class.

In [2]:
new_log=XMLLog()
In [3]:
# we add an entry with add_entry method
new_log.add_entry("I started the log today!")

This log class can be printed, converted to html and saved.

In [4]:
print(new_log)
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="../XSL/DEFAULT_LOG_STYLE.xsl"?>
<Log>
	<Entry Date="2017-04-29T01:42:23.275000" Index="1">I started the log today!</Entry>
</Log>

In [5]:
style_sheet=os.path.join(TESTS_DIRECTORY,'../XSL/DEFAULT_LOG_STYLE.xsl') #this locates the style sheet
html=new_log.to_HTML(style_sheet)
print(html)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<h2> Description: </h2>
<h3></h3>
<br><hr>
<b><i> Entry: </i></b>1<br><b><i> Date: </i></b>2017-04-29T01:42:23.275000<br>I started the log today!<br><br><hr>
</body></html>

In [6]:
# now the file was autonamed when it was generated
print(new_log.path)
XML_Log_20170428_003.xml
In [7]:
# to save it using this name 
new_log.save()
# to save as a different name use new_log.save("NewName.xml")
In [8]:
# to reopen it just intialize the class with its location 
path=new_log.path
reopened_log=XMLLog(path)
print(reopened_log)
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="../XSL/DEFAULT_LOG_STYLE.xsl"?>
<Log>
	
	
	<Entry Date="2017-04-29T01:42:23.275000" Index="1">I started the log today!</Entry>
	

</Log>

In [9]:
# to get a python list of dictionaries 
new_log.get_table()
Out[9]:
[{'Date': '2017-04-29T01:42:23.275000',
  'Entry': u'I started the log today!',
  'Index': '1'}]
In [10]:
# we can also add logs 
next_log=XMLLog()
next_log.add_entry("This is another log")
new_log+next_log
print(new_log)
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="../XSL/DEFAULT_LOG_STYLE.xsl"?>
<Log>
	<Entry Date="2017-04-29T01:42:23.275000" Index="1">I started the log today!</Entry>
	<Entry Date="2017-04-29T01:42:23.401000" Index="2">This is another log</Entry>
</Log>

In [11]:
# we can describe the log 
new_log.add_description("This log is a test that displays the class")
In [12]:
# to display a formated version in the notebook
from IPython.display import HTML
HTML(new_log.to_HTML(style_sheet))
Out[12]:

Description:

This log is a test that displays the class



Entry: 1
Date: 2017-04-29T01:42:23.275000
I started the log today!

Entry: 2
Date: 2017-04-29T01:42:23.401000
This is another log