Last active
April 12, 2021 05:38
-
-
Save Komet-Kazi/6368996ada7722b49981f06ffbf23af5 to your computer and use it in GitHub Desktop.
python snips
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
| import os | |
| import json | |
| import sys | |
| import logging | |
| from functools import wraps | |
| def create_logger(): | |
| # create a logger object | |
| logger = logging.getLogger("exc_logger") | |
| logger.setLevel(logging.INFO) | |
| # create a file to store all the | |
| # logged exceptions | |
| logfile = logging.FileHandler("exc_logger.log") | |
| fmt = "%(asctime)s - %(name)s - %(levelname)s - %(message)s" | |
| formatter = logging.Formatter(fmt) | |
| logfile.setFormatter(formatter) | |
| logger.addHandler(logfile) | |
| return logger | |
| logger = create_logger() | |
| # you will find a log file | |
| # created in a given path | |
| # print(logger) | |
| def traverse_parents(path, include_current=False): | |
| if not include_current: | |
| path = os.path.dirname(path) | |
| previous = None | |
| while previous != path: | |
| yield path | |
| previous = path | |
| path = os.path.dirname(path) | |
| def get_PyInfo(): | |
| """Returns info about the version, prefix, system version, 32/64-bit""" | |
| obj = {} | |
| obj["versionInfo"] = tuple(sys.version_info) | |
| obj["sysPrefix"] = sys.prefix | |
| obj["sysVersion"] = sys.version | |
| obj["is64Bit"] = sys.maxsize > 2 ** 32 | |
| return json.dumps(obj) | |
| def printEnvVariables(to_file=False): | |
| if to_file: | |
| json_file = '~/environLog.json' | |
| with open(json_file, "w") as outfile: | |
| json.dump(dict(os.environ), outfile) | |
| return 'Written' | |
| return print(json.dumps(dict(os.environ))) | |
| def exception(logger): | |
| # logger is the logging object | |
| # exception is the decorator objects | |
| # that logs every exception into log file | |
| def decorator(func): | |
| @wraps(func) | |
| def wrapper(*args, **kwargs): | |
| try: | |
| return func(*args, **kwargs) | |
| except: | |
| msg = (f'exception in {func.__name__}\n') | |
| logger.exception(msg) | |
| raise | |
| return wrapper | |
| return decorator | |
| @exception(logger) | |
| def divideStrByInt(): | |
| return 4 / 2 | |
| #!~/primary/bin/python | |
| from itertools import cycle | |
| from collections import deque | |
| from time import sleep | |
| from signal import pause | |
| def scroller(msg:str, chars=4): | |
| """ Infinite sliding window of size 'chars' over 'msg' | |
| Yields msg substring of length 'chars', | |
| Increments by char.""" | |
| d = deque(maxlen=chars) | |
| for letter in cycle(msg): | |
| d.append(letter) | |
| if len(d) == chars: | |
| yield ''.join(d) | |
| # Driver Code | |
| if __name__ == "__main__": | |
| print(divideStrByInt()) | |
| print(traverse_parents('/home/pi/pyScripts/printEnvVariables.py')) | |
| print(get_PyInfo()) | |
| print(printEnvVariables()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment