Giter Club home page Giter Club logo

automate-last-modified-dates's Introduction

Automate “Last modified” dates in your documentation

Automating repetitive documentation tasks will eliminate human error and keep your documentation structured.

The purpose of this article is to explain how you can automatically update XML data, in this case the last modified date. My hope is that you can leverage this to build out your own automation to increase productivity and accuracy in your own work.

Imagine that you’re working on a release history page. The release history has a section that contains the last modified of each API. The following XML snippet, named lastmodified.xml, represents structured XML for a release history.

<file name="Release history">
        <update name="API01">
                <type>Create</type>
                <returns>TXT</returns>
                <maxSize>1000</maxSize>
                <lastModified>2021-11-07</lastModified>
        </update>
        <update name="API02">
                <type>Update</type>
                <returns>TXT</returns>
                <maxSize>1000</maxSize>
                <lastModified>2021-10-29</lastModified>
        </update>
</file>

A developer has instructed you to update the documentation around the max size that your API can create and update. This request is easy enough. However, the developer is unsure when they’re going to make this change public. You stage your changes for the max size. Then, on the day the documenation should go live, your developer communicates to you that they’ve updated the API and your documenation should also update.

With a few lines of code, you can automatically update your “last modified field”.

Add the following python file in the same directory as your XML file:

import xml.etree.ElementTree as ET
from datetime import datetime


class TimestampUpdater:
    # Constructor
    def __init__(self, filepath):
        self.meta_file = filepath
        self.tree = ET.parse(self.meta_file)

    # To get xml data
    def getMetadataTree(self):
        return self.tree

    # to get xml data root
    def getMetadataRoot(self):
        return self.tree.getroot()

    # to compare current date to lastmodified date
    def updateLastModified(self):
        today = datetime.now()
        # add your XML paths here
        for testfile in self.getMetadataRoot().findall("update"):
            lastmodified = testfile.find("lastModified")
            previous_update = datetime.strptime(lastmodified.text, "%Y-%m-%d")
            if previous_update < today:
                lastmodified.text = today.strftime("%Y-%m-%d")
                self.getMetadataTree().write(self.meta_file)


# print lastmodified date to file
def print_file_content(filename):
    with open(filename, "r") as fh:
        for line in fh:
            print(line.rstrip())


# print diff to console
if __name__ == "__main__":
    # rename your file name here
    metafile = "lastmodified.xml"
    print("\n====Before updating:====")
    print_file_content(metafile)
    updater = TimestampUpdater(metafile)
    updater.updateLastModified()
    print("\n====After updating:====")
    print_file_content(metafile)

Save and run your python script to update your lastmodified.xml file.

The following is an example output:

<file name="Release history">
        <update name="API01">
                <type>Create</type>
                <returns>TXT</returns>
                <maxSize>1000</maxSize>
                <lastModified>2021-11-07</lastModified>
        </update>
        <update name="API02">
                <type>Update</type>
                <returns>TXT</returns>
                <maxSize>1000</maxSize>
                <lastModified>2021-10-29</lastModified>
        </update>
</file>

The XML attribute correctly updated to today’s date. You can now publish your XML without the fear that you’ve applied your date’s format incorrectly, forgot to close an XML tag, or that something has become out of date.

Let me know if how you would leverage automation in your own documentation. I’d love to hear any additional ideas and work flows you use.

Sources:

I used the following sources for inspiration for this post:

automate-last-modified-dates's People

Contributors

rachfop avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.