Skip to content

Instantly share code, notes, and snippets.

@malex984
Created April 23, 2019 20:21
Show Gist options
  • Save malex984/306d904d20c69f799034bc849db405a4 to your computer and use it in GitHub Desktop.
Save malex984/306d904d20c69f799034bc849db405a4 to your computer and use it in GitHub Desktop.
Sample module / application
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# encoding: utf-8
# coding: utf-8
"""
Sample cli application
"""
from __future__ import absolute_import, print_function, unicode_literals
import sys, os
from sample_module import CONSTANT, DATA, Antwort # use sample module
question = "The Ultimate Question of Life, The Universe, and Everything"
print("My question is [{}].".format(question))
# use computation from sample module:
answer = Antwort(question) #!
print("Answer to my question is [{}].".format(answer))
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# encoding: utf-8
# coding: utf-8
"""
Sample module, which can also be executed via command line
"""
from __future__ import absolute_import, print_function, unicode_literals
import sys, os
#################################################################
# Some global constant
CONSTANT = '42'
# Global data
DATA = os.environ.get('DATA', CONSTANT)
#################################################################
# Some general function
def Antwort(q):
return(DATA)
#################################################################
# Note: this is the main trick to make this module separately usable:
if __name__ == "__main__": ## Execution via command line?
try:
# user interaction:
print('Input Your question: ?')
question = input()
answer = Antwort(question) # some computation
print("Answer to your question [{}] is [{}].".format(question, answer))
sys.exit(0)
except KeyboardInterrupt:
e = sys.exc_info()[1]
sys.stderr.write("\nUser interrupted process.\n")
sys.exit(0)
except SystemExit:
e = sys.exc_info()[1]
sys.exit(e.code)
except Exception:
e = sys.exc_info()[1]
sys.stderr.write("\nERROR: unhandled exception occurred: (%s).\n" % e)
sys.exit(-1)
finally:
# some global cleanup
print("So Long, and Thanks for All the Fish!")
@malex984
Copy link
Author

Note: put both files into the same directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment