Skip to main content

Basic infrastructure for writing scripts

Project description

Python is an excellent language that makes writing scripts very straightforward. Over the course of writing many scripts, we realized that we were doing some things over and over like creating a logger and accepting command line arguments. Base script is a very simple abstraction that takes care of setting up logging and other basics so you can focus on your application specific logic.

Here are some facilities that Base Script offers:

  • Logging

  • Accepting command-line arguments using argparse


pip install basescript


Here is a simple example to get started

Hello World

from basescript import BaseScript

class HelloWorld(BaseScript):
    def run(self):
        print "Hello world"

if __name__ == '__main__':

NOTE: all examples showcased here are available under the
``examples`` directory

Run the above by doing:


Run script with log level set to DEBUG

python --log-level DEBUG

Run script with custom log file

python --log-level DEBUG --log mylog

Command line args, Using the logger

The following is a more involved example

from basescript import BaseScript

class Adder(BaseScript):
    # The following specifies the script description so that it be used
    # as a part of the usage doc when --help option is used during running.
    DESC = 'Adds numbers'

    def init(self):
        We can put whatever script initialization we need for our script
        over here. This is preferred to overriding __init__
        self.a = 10
        self.b = 20

    def define_args(self, parser):
        parser.add_argument('c', type=int, help='Number to add')

    def run(self):"Starting run of script ...")

        print self.a + self.b + self.args.c"Script is done")

if __name__ == '__main__':
Run the script as follows and observe the usage information shown. Note how the
description appears along with the c argument.
python --help

usage: [-h] [--name NAME] [--log LOG]
                [--log-level LOG_LEVEL] [--quiet]

Adds numbers

positional arguments:
  c                     Number to add

optional arguments:
  -h, --help            show this help message and exit
  --name NAME           Name to identify this instance
  --log LOG             Name of log file
  --log-level LOG_LEVEL
                        Logging level as picked from the logging module

Run the script now to see the intended output

python 30

Run the same with info and higher level logs enabled

python --log-level INFO 30
2016-04-10 13:48:27,356 INFO Starting run of script ...
2016-04-10 13:48:27,356 INFO Script is done
--log-level accepts all the values shown at
log is a log object created using python’s standard logging module. You can

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

basescript-0.1.1.tar.gz (3.5 kB view hashes)

Uploaded source

Built Distribution

basescript-0.1.1-py2-none-any.whl (6.2 kB view hashes)

Uploaded py2

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page