-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.py
102 lines (79 loc) · 3.56 KB
/
debug.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Module name: debug.py
# Purpose: debugging functions
#
# Notes:
#
# Copyright: 2019, release under GPL3. See LICENSE file for details
# Carl W Greenstreet <[email protected]>
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# import libraries
import datetime # manage date time formats
import inspect # to get variable names
import re # regular expressions to extract strings
import time # time to allow sleep pause for JS to catch up
# import third party libary specific imports
import bs4 # beautiful soup 4 library to parse website
import lxml # lxml to parse website
import requests # requests to get http
def debug_status(debug_flag=True):
"""Displays a debugging status message to console"""
if debug_flag: #when true
console_msg('Debugging is ON')
else:
console_msg('Debugging is OFF')
def retrieve_name(var):
""" Gets the name of var. Does it from the out-most frame inner-wards.
:param var: variable to get name from.
:return: string """
#https://stackoverflow.com/questions/18425225/getting-the-name-of-a-variable-as-a-string/18425523
for fi in reversed(inspect.stack()):
names = [var_name for var_name, var_val in fi.frame.f_locals.items() if var_val is var]
if len(names) > 0:
return names[0]
def console_msg(msg):
"""Display a message on console window"""
clear = "\n" * 2
print(clear)
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++\
\n\t" +msg +"\
\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++")
def val_type(debug_target, debug_flag):
"""Prints variable name, value and type to console window
when switch debug_flag = True"""
if debug_flag:
var_name = str(debug_target)
print("\n" + retrieve_name(debug_target) + " = ", debug_target)
print("\t" + retrieve_name(debug_target) + " type:", type(debug_target))
def list_count(debug_target, debug_flag):
"""Prints element count of list to console window
when switch debug_flag = True"""
if debug_flag:
var_name = str(debug_target)
print("\n" + retrieve_name(debug_target) + " count: ", len(debug_target))
def date_value(debug_target, debug_flag):
"""Prints date value to console when switch debug_flag = True"""
if debug_flag:
var_name = str(debug_target)
print('\nDate:', debug_target.date())
def time_value(debug_target, debug_flag):
"""Prints time value to console when switch debug_flag = True"""
if debug_flag:
var_name = str(debug_target)
print('Time:', debug_target.time())
def date_time_value(debug_target, debug_flag):
"""Prints date, time and date-time values to console when switch debug_flag = True"""
if debug_flag:
var_name = str(debug_target)
print('\nDate:', debug_target.date())
print('Time:', debug_target.time())
print('Date-time:', debug_target)
def print_df(debug_target, debug_flag):
"""prints dataframe and df stats when switch debug_flag = True"""
if debug_flag:
var_name = str(debug_target)
print("\n" + retrieve_name(debug_target) + " dataframe ", debug_target, sep='\n')
print("\t" + retrieve_name(debug_target) + " dataframe ", debug_target, sep='\n')
print("\t" + retrieve_name(debug_target) + " dataframe ", debug_target, sep='\n')