Created
April 23, 2019 20:21
-
-
Save malex984/306d904d20c69f799034bc849db405a4 to your computer and use it in GitHub Desktop.
Sample module / application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: put both files into the same directory