{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Example of XML Logs\n",
    "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. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Importing pyMez, this should take roughly 30 seconds\n",
      "The module smithplot was not found,please put it on the python path\n",
      "The module smithplot was not found,please put it on the python path\n"
     ]
    }
   ],
   "source": [
    "# import pyMez\n",
    "from pyMez import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## To create a new log just call the class."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "new_log=XMLLog()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# we add an entry with add_entry method\n",
    "new_log.add_entry(\"I started the log today!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## This log class can be printed, converted to html and saved."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<?xml version=\"1.0\" ?>\n",
      "<?xml-stylesheet type=\"text/xsl\" href=\"../XSL/DEFAULT_LOG_STYLE.xsl\"?>\n",
      "<Log>\n",
      "\t<Entry Date=\"2017-04-29T01:42:23.275000\" Index=\"1\">I started the log today!</Entry>\n",
      "</Log>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(new_log)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n",
      "<html><body>\n",
      "<h2> Description: </h2>\n",
      "<h3></h3>\n",
      "<br><hr>\n",
      "<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>\n",
      "</body></html>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "style_sheet=os.path.join(TESTS_DIRECTORY,'../XSL/DEFAULT_LOG_STYLE.xsl') #this locates the style sheet\n",
    "html=new_log.to_HTML(style_sheet)\n",
    "print(html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "XML_Log_20170428_003.xml\n"
     ]
    }
   ],
   "source": [
    "# now the file was autonamed when it was generated\n",
    "print(new_log.path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# to save it using this name \n",
    "new_log.save()\n",
    "# to save as a different name use new_log.save(\"NewName.xml\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<?xml version=\"1.0\" ?>\n",
      "<?xml-stylesheet type=\"text/xsl\" href=\"../XSL/DEFAULT_LOG_STYLE.xsl\"?>\n",
      "<Log>\n",
      "\t\n",
      "\t\n",
      "\t<Entry Date=\"2017-04-29T01:42:23.275000\" Index=\"1\">I started the log today!</Entry>\n",
      "\t\n",
      "\n",
      "</Log>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# to reopen it just intialize the class with its location \n",
    "path=new_log.path\n",
    "reopened_log=XMLLog(path)\n",
    "print(reopened_log)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'Date': '2017-04-29T01:42:23.275000',\n",
       "  'Entry': u'I started the log today!',\n",
       "  'Index': '1'}]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# to get a python list of dictionaries \n",
    "new_log.get_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<?xml version=\"1.0\" ?>\n",
      "<?xml-stylesheet type=\"text/xsl\" href=\"../XSL/DEFAULT_LOG_STYLE.xsl\"?>\n",
      "<Log>\n",
      "\t<Entry Date=\"2017-04-29T01:42:23.275000\" Index=\"1\">I started the log today!</Entry>\n",
      "\t<Entry Date=\"2017-04-29T01:42:23.401000\" Index=\"2\">This is another log</Entry>\n",
      "</Log>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# we can also add logs \n",
    "next_log=XMLLog()\n",
    "next_log.add_entry(\"This is another log\")\n",
    "new_log+next_log\n",
    "print(new_log)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# we can describe the log \n",
    "new_log.add_description(\"This log is a test that displays the class\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n",
       "<html><body>\n",
       "<h2> Description: </h2>\n",
       "<h3>This log is a test that displays the class</h3>\n",
       "<br><hr>\n",
       "<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><hr>\n",
       "<b><i> Entry: </i></b>2<br><b><i> Date: </i></b>2017-04-29T01:42:23.401000<br>This is another log<br><br><hr>\n",
       "</body></html>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# to display a formated version in the notebook\n",
    "from IPython.display import HTML\n",
    "HTML(new_log.to_HTML(style_sheet))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
