Last active
April 3, 2022 09:41
-
-
Save 270ajay/ae735d2274274d5945d952a8e5467cfc to your computer and use it in GitHub Desktop.
logging, json, excel functions, py to exe
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
# Example of license header: | |
# Unauthorized copying of this file, via any medium is strictly prohibited | |
# Proprietary and confidential | |
"""This file contains utility classes and functions.""" | |
import json | |
import logging | |
import openpyxl | |
def logger(should_write_to_console, | |
file_name, | |
file_mode="w", | |
file_log_level="d", | |
console_log_level="i"): | |
"""Logs to console and file.""" | |
level_dict = { | |
"d": logging.DEBUG, | |
"e": logging.ERROR, | |
"i": logging.INFO, | |
"w": logging.WARNING, | |
"c": logging.CRITICAL | |
} | |
# Fix for not writing log to file | |
# https://stackoverflow.com/a/56366848 | |
for handler in logging.root.handlers[:]: | |
logging.root.removeHandler(handler) | |
logging.basicConfig( | |
level=level_dict[file_log_level], | |
format='%(levelname)s: %(asctime)s %(process)d %(message)s', | |
#datefmt='%d/%m/%Y %I:%M:%S %p', | |
filename=file_name, | |
filemode=file_mode) | |
if should_write_to_console: | |
console = logging.StreamHandler() | |
console.setLevel(level_dict[console_log_level]) | |
console.setFormatter( | |
logging.Formatter( | |
'%(levelname)s: %(asctime)s %(process)d %(message)s')) | |
logging.getLogger().addHandler(console) | |
def remove_duplicate_dict_from_list(dict_list, field_name): | |
"""Returns list of dicts after removing duplicate dicts. | |
Uses field_name to identify duplicates. | |
""" | |
if not dict_list: | |
return dict_list | |
fields_seen = set() | |
dict_list_without_duplicates = [] | |
for info in dict_list: | |
field = info[field_name] | |
if field in fields_seen: | |
logging.warning("Duplicate %s : %s", field_name, field) | |
continue | |
dict_list_without_duplicates.append(info) | |
fields_seen.add(field) | |
return dict_list_without_duplicates | |
def get_json_data_from_json_file(file_name): | |
"""Returns json dictionary from json file.""" | |
with open(file_name) as f: | |
json_data = json.load(f) | |
return json_data | |
def get_json_data_from_json_obj(json_object): | |
"""Returns json dictionary from json object.""" | |
if isinstance(json_object, (bytes, str, bytearray)): | |
json_data = json.loads(json_object) | |
return json_data | |
else: | |
json_data = json.load(json_object) | |
json_object.close() | |
json_object.release_conn() | |
return json_data | |
def write_json_to_file(json_data, file_name): | |
"""Writes json object to json file.""" | |
with open(file_name, 'w') as f: | |
json.dump(json_data, f, indent=2) | |
def get_workbook_from_excel(file_location): | |
"""Creates openpyxl workbook using excel file.""" | |
workbook = openpyxl.load_workbook(file_location, | |
read_only=True, | |
data_only=True) | |
return workbook | |
def get_rows(workbook, sheet_name, num_rows_to_skip): | |
"""Returns rows.""" | |
sheet = workbook[sheet_name] | |
rows = sheet.rows | |
for i in range(0, num_rows_to_skip): | |
next(rows) | |
return rows | |
def create_workbook(): | |
"""Creates and returns workbook in which data can be written.""" | |
return openpyxl.Workbook(write_only=True) | |
def create_worksheet(workbook, title): | |
"""Returns worksheet after creating it.""" | |
return workbook.create_sheet(title=title) | |
def save_workbook(workbook, file_name): | |
"""Saves excel workbook to disk.""" | |
workbook.save(file_name) | |
# To make .exe | |
# | |
# 1. `pip install pyinstaller` if you do not have pyinstaller installed. | |
# | |
# 2. Open cmd in the main directory (containing converter, input, output folders) and enter the command: | |
# `pyinstaller --onefile python_file.py --hiddenimport openpyxl --hiddenimport json --hiddenimport isodate --exclude numpy --exclude pandas` | |
# We exclude pandas and numpy because it makes the size of .exe very large. | |
# If you do not have pandas and numpy installed, you may remove them from the command. | |
# | |
# 3. It creates dist and build folder. | |
# | |
# 4. Delete build folder. | |
# | |
# 5. Move run.exe from dist folder to the main directory. | |
# | |
# 6. Delete dist folder. | |
# | |
# 7. Open run.exe. It only needs input and output folder. Everything else can be deleted. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment