Difference between revisions of "Python:Template"
Jump to navigation
Jump to search
m |
|||
(14 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | [[Category:Python]] | ||
Template for Python programs. Creating logfiles, standard modules to load, opening files, user configuration. | Template for Python programs. Creating logfiles, standard modules to load, opening files, user configuration. | ||
Line 7: | Line 8: | ||
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
+ | #!/usr/bin/env python | ||
def usage(): | def usage(): | ||
print ("Usage: "+__file__) | print ("Usage: "+__file__) | ||
print("Template for python programs") | print("Template for python programs") | ||
+ | version = 'R1A, 20181010, Initial release' | ||
+ | print("Version: "+version) | ||
sys.exit(1) | sys.exit(1) | ||
Line 16: | Line 20: | ||
# USER CONFIGURABLE ITEMS | # USER CONFIGURABLE ITEMS | ||
+ | warning = 'If you need to modify these variables anywhere else you have to declare it global there' | ||
filespec = '*.*' | filespec = '*.*' | ||
− | + | logfile2name = '<apath>_timestamp.txt' | |
# END USER CONFIGURABLE ITEMS, DO NOT CHANGE ANYTHING BELOW THIS LINE | # END USER CONFIGURABLE ITEMS, DO NOT CHANGE ANYTHING BELOW THIS LINE | ||
− | import os | + | import os |
+ | import sys | ||
+ | import re | ||
+ | import glob | ||
+ | import logging | ||
+ | from collections import defaultdict | ||
from datetime import datetime | from datetime import datetime | ||
− | timestamp = datetime.now().strftime("% | + | timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") |
+ | |||
+ | logfile2name = logfile2name.replace('timestamp',timestamp) | ||
def main(): | def main(): | ||
− | + | logfilename = openlog() | |
− | + | openlog(<apath>) | |
− | + | logging.info("This is in the logfile with generated name") | |
− | |||
− | |||
openfile(filespec) | openfile(filespec) | ||
return | return | ||
+ | |||
def openfile(filespec): | def openfile(filespec): | ||
Line 41: | Line 52: | ||
with open (filename,"r") as file: | with open (filename,"r") as file: | ||
for line in file: | for line in file: | ||
+ | #remove newline characters | ||
+ | line.rstrip('\r\n') | ||
<codeblock> | <codeblock> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
return | return | ||
Line 59: | Line 61: | ||
if logfilename == '': | if logfilename == '': | ||
logfilename = re.sub('/bin/','/log/',__file__) | logfilename = re.sub('/bin/','/log/',__file__) | ||
− | logfilename += '_'+ | + | logfilename += '_'+timestamp+'.log' |
− | + | logging.basicConfig(filename=logfilename,level=logging.DEBUG,format='%(asctime)s %(levelname)7s %(message)s') | |
− | + | logging.info(logfilename+" open.") | |
− | + | return(logfilename) | |
− | + | ||
− | return( | ||
main() | main() | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Logging suggestion to test: | ||
+ | <syntaxhighlight lang=python> | ||
+ | def logfunc(logFile): | ||
+ | logging.basicConfig(format = '%(asctime)s %(filename)25s %(levelname)6s : %(message)s' , level=logging.INFO) | ||
+ | log = logging.getLogger() | ||
+ | fh = logging.FileHandler(logFile) | ||
+ | formatter = logging.Formatter('%(asctime)s %(filename)25s %(levelname)6s : %(message)s') | ||
+ | fh.setFormatter(formatter) | ||
+ | log.addHandler(fh) | ||
+ | return log | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 21:50, 27 June 2020
Template for Python programs. Creating logfiles, standard modules to load, opening files, user configuration.
Code for mailing is on this wiki too.
ToDO:
- Parsing commandline options
#!/usr/bin/env python
def usage():
print ("Usage: "+__file__)
print("Template for python programs")
version = 'R1A, 20181010, Initial release'
print("Version: "+version)
sys.exit(1)
# USER CONFIGURABLE ITEMS
warning = 'If you need to modify these variables anywhere else you have to declare it global there'
filespec = '*.*'
logfile2name = '<apath>_timestamp.txt'
# END USER CONFIGURABLE ITEMS, DO NOT CHANGE ANYTHING BELOW THIS LINE
import os
import sys
import re
import glob
import logging
from collections import defaultdict
from datetime import datetime
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
logfile2name = logfile2name.replace('timestamp',timestamp)
def main():
logfilename = openlog()
openlog(<apath>)
logging.info("This is in the logfile with generated name")
openfile(filespec)
return
def openfile(filespec):
filelist = glob.glob(filespec)
for filename in filelist:
if os.path.isfile(filename):
with open (filename,"r") as file:
for line in file:
#remove newline characters
line.rstrip('\r\n')
<codeblock>
return
def openlog (logfilename = ''):
if logfilename == '':
logfilename = re.sub('/bin/','/log/',__file__)
logfilename += '_'+timestamp+'.log'
logging.basicConfig(filename=logfilename,level=logging.DEBUG,format='%(asctime)s %(levelname)7s %(message)s')
logging.info(logfilename+" open.")
return(logfilename)
main()
Logging suggestion to test:
def logfunc(logFile):
logging.basicConfig(format = '%(asctime)s %(filename)25s %(levelname)6s : %(message)s' , level=logging.INFO)
log = logging.getLogger()
fh = logging.FileHandler(logFile)
formatter = logging.Formatter('%(asctime)s %(filename)25s %(levelname)6s : %(message)s')
fh.setFormatter(formatter)
log.addHandler(fh)
return log