migrate snippets from gitlab
This commit is contained in:
17
.project
Normal file
17
.project
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>snippets</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.python.pydev.PyDevBuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.python.pydev.pythonNature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
||||||
44
eclipse-main-template.py
Normal file
44
eclipse-main-template.py
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#!python3
|
||||||
|
# Created 5 Jul 2019 Andreas Balogh
|
||||||
|
# @author: baloan
|
||||||
|
|
||||||
|
""" Module: Main Template """
|
||||||
|
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
LOG = logging.getLogger()
|
||||||
|
|
||||||
|
|
||||||
|
def cli(argv=None):
|
||||||
|
# command line interface
|
||||||
|
if argv is None:
|
||||||
|
argv = sys.argv
|
||||||
|
LOG.info("%s %s", os.path.basename(argv[0]), " ".join(argv[1:]))
|
||||||
|
parser = ArgumentParser(description="Module Template")
|
||||||
|
parser.add_argument("action", help="mock action verb")
|
||||||
|
parser.add_argument("--database", help="database connection")
|
||||||
|
parser.add_argument("--config_file", help="json filepath, not used by mock")
|
||||||
|
parser.add_argument("--env", default=argparse.SUPPRESS, help="dump environment")
|
||||||
|
args = parser.parse_args(argv[1:])
|
||||||
|
argd = vars(args)
|
||||||
|
# arguments
|
||||||
|
for k, v in argd.items():
|
||||||
|
print(k, v)
|
||||||
|
# enviroment
|
||||||
|
if "env" in argd:
|
||||||
|
for k, v in os.environ.items():
|
||||||
|
print(k, v)
|
||||||
|
# feature
|
||||||
|
LOG.info("done")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
logging.Formatter.default_time_format = '%H:%M:%S'
|
||||||
|
logging.Formatter.default_msec_format = '%s.%03d'
|
||||||
|
logging.basicConfig(level=logging.DEBUG,
|
||||||
|
format='%(asctime)s [%(process)i] %(levelname).4s %(module)s.%(funcName)s: %(message)s')
|
||||||
|
sys.exit(cli())
|
||||||
40
pickle_cache.py
Normal file
40
pickle_cache.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
def pickle_cache(mode="auto"):
|
||||||
|
|
||||||
|
def pc_decorator(func):
|
||||||
|
|
||||||
|
def pc_wrapper(*args, **kwargs):
|
||||||
|
cache_fp = Path(PROXY_DIR, f"{func.__name__}.pickle")
|
||||||
|
today = dt.date.today()
|
||||||
|
if cache_fp.exists():
|
||||||
|
statinfo = cache_fp.stat()
|
||||||
|
# (st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime, st_ctime) = statinfo
|
||||||
|
(_, _, _, _, _, _, _, _, st_mtime, _) = statinfo
|
||||||
|
mdate = dt.datetime.fromtimestamp(st_mtime).date()
|
||||||
|
else:
|
||||||
|
mdate = None
|
||||||
|
if mode == "auto":
|
||||||
|
if cache_fp.exists() and mdate == today:
|
||||||
|
action = "cached"
|
||||||
|
else:
|
||||||
|
action = "query"
|
||||||
|
else:
|
||||||
|
action = mode
|
||||||
|
if action == "cached" :
|
||||||
|
LOG.info(f"{func.__name__}: loading cached data...")
|
||||||
|
with cache_fp.open("rb") as fh:
|
||||||
|
rc = pickle.load(fh)
|
||||||
|
elif action == "query":
|
||||||
|
LOG.info(f"{func.__name__}: database access...")
|
||||||
|
rc = func(*args, **kwargs)
|
||||||
|
if cache_fp.exists() and mdate != today and Path(PROXY_DIR, "Archive").exists():
|
||||||
|
archive_fp = Path(PROXY_DIR, "Archive", f"{func.__name__}-{mdate:%Y%m%d}.pickle")
|
||||||
|
shutil.copy(cache_fp, archive_fp)
|
||||||
|
with cache_fp.open("wb") as fh:
|
||||||
|
pickle.dump(rc, fh)
|
||||||
|
else:
|
||||||
|
LOG.error(f"{func.__name__}: mode {action} unknown")
|
||||||
|
return rc
|
||||||
|
|
||||||
|
return pc_wrapper
|
||||||
|
|
||||||
|
return pc_decorator
|
||||||
7
python_default_logger.py
Normal file
7
python_default_logger.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
LOG=logging.getLogger()
|
||||||
|
|
||||||
|
logging.Formatter.default_time_format='%H:%M:%S'
|
||||||
|
logging.Formatter.default_msec_format='%s.%03d'
|
||||||
|
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(process)i] %(levelname).4s %(module)s.%(funcName)s: %(message)s'
|
||||||
24
tk_task_wrapper.py
Normal file
24
tk_task_wrapper.py
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
def long_running_task(status):
|
||||||
|
" status decorator "
|
||||||
|
|
||||||
|
def lrt_decorator(func):
|
||||||
|
|
||||||
|
def lrt_wrapper(*args, **kwargs):
|
||||||
|
self = args[0]
|
||||||
|
self.lb12["text"] = status
|
||||||
|
self.bu10.state(['disabled'])
|
||||||
|
self.bu11.state(['disabled'])
|
||||||
|
self.update_idletasks()
|
||||||
|
try:
|
||||||
|
rc = func(*args, **kwargs)
|
||||||
|
finally:
|
||||||
|
# make sure GUI unlocks even after exception
|
||||||
|
self.lb12["text"] = "Ready."
|
||||||
|
self.bu10.state(['!disabled'])
|
||||||
|
self.bu11.state(['!disabled'])
|
||||||
|
self.update_idletasks()
|
||||||
|
return rc
|
||||||
|
|
||||||
|
return lrt_wrapper
|
||||||
|
|
||||||
|
return lrt_decorator
|
||||||
Reference in New Issue
Block a user