Skip to main content

Some useful functions, classes to help work. Create by Van of Techlab CA team.

Project description

CA VNTL Helper

This is a simple helper for the CA Techlab project. It is designed to help you track errors and do some actions when errors occur. It will help you to track, log, and alert when an error occurs in your code. It can show the parameters you passed, so you can easily simulate the error.

Installation

Install the package using pip:

pip install ca-vntl-helper

Usage

The decorator function

Import the decorator function from the package and use it to decorate your function. The message will be through when an error occurs.

from ca_vntl_helper import error_tracking_decorator

@error_tracking_decorator
def example_function():
    # Your code here
    pass

Example

Some nested functions and an error will be raised in the innermost function. The error message will be printed out.

You just place the decorator on the top of the function you want to track.

from ca_vntl_helper import error_tracking_decorator

def divide(a, b):
    return a / b

def second_inner_function(second_inner_a, second_inner_b):
    return divide(second_inner_a, second_inner_b)
    
def first_inner_function(first_inner_a, first_inner_b):
    return second_inner_function(first_inner_a, first_inner_b)

@error_tracking_decorator # Just place the decorator here
def outer_function(outer_a, outer_b):
    return first_inner_function(outer_a, outer_b)

if __name__ == "__main__":
    # The process will get an error when dividing by 0
    outer_function(1, 0)
ERROR:root:Error in function outer_function 
===================================================
Filename: /Users/abc/Documents/projects/packaging_helper/src/test.py,
Function name: outer_function, params: {'outer_a': 1, 'outer_b': 0}
	-----
	Line: 20,     return first_inner_function(outer_a, outer_b)
 	-----
===================================================
Filename: /Users/abc/Documents/projects/packaging_helper/src/test.py,
Function name: first_inner_function, params: {'first_inner_a': 1, 'first_inner_b': 0}
	-----
	Line: 15,     return second_inner_function(first_inner_a, first_inner_b)
 	-----
===================================================
Filename: /Users/abc/Documents/projects/packaging_helper/src/test.py,
Function name: second_inner_function, params: {'second_inner_a': 1, 'second_inner_b': 0}
	-----
	Line: 11,     return divide(second_inner_a, second_inner_b)
 	-----
===================================================
Filename: /Users/abc/Documents/projects/packaging_helper/src/test.py,
Function name: divide, params: {'a': 1, 'b': 0}
	-----
	Line: 7,     return a / b
 	-->ROOT CAUSE: ZeroDivisionError: division by zero 
	-----

As you can see, the error message is printed out with the function name, parameters, the filename, and the line number. The root cause of the error is also shown.

The class with callback functions

In case you want to do some actions like alert to Slack or save error logs to S3, you can use the class with callback functions.

from ca_vntl_helper import ErrorTrackerWithCallBacks

# Define your callback functions, notice that those functions must have a parameter to receive the message
def send_message_to_slack(message):
    # Your code here
    print("Message sent to slack:")
    print(message)
    
def save_message_to_logfile_on_s3(message):
    # Your code here
    print("Message saved to logfile on S3:")
    print(message)

error_tracker = ErrorTrackerWithCallBacks(callback_functions=[send_message_to_slack, save_message_to_logfile_on_s3])
error_tracking_decorator_with_callbacks = error_tracker.error_tracking_decorator

def divide(a, b):
    return a / b

def second_inner_function(second_inner_a, second_inner_b):
    return divide(second_inner_a, second_inner_b)

def first_inner_function(first_inner_a, first_inner_b):
    return second_inner_function(first_inner_a, first_inner_b)

@error_tracking_decorator_with_callbacks  # Just place the decorator here
def outer_function(outer_a, outer_b):
    return first_inner_function(outer_a, outer_b)

if __name__ == "__main__":
    # The process will get an error when dividing by 0
    outer_function(1, 0)
Message sent to slack:
Error in function outer_function 
===================================================
Filename: /Users/abc/Documents/projects/packaging_helper/src/test.py,
Function name: outer_function, params: {'outer_a': 1, 'outer_b': 0}
	-----
	Line: 28,     return first_inner_function(outer_a, outer_b)
 	-----
===================================================
Filename: /Users/abc/Documents/projects/packaging_helper/src/test.py,
Function name: first_inner_function, params: {'first_inner_a': 1, 'first_inner_b': 0}
	-----
	Line: 23,     return second_inner_function(first_inner_a, first_inner_b)
 	-----
===================================================
Filename: /Users/abc/Documents/projects/packaging_helper/src/test.py,
Function name: second_inner_function, params: {'second_inner_a': 1, 'second_inner_b': 0}
	-----
	Line: 19,     return divide(second_inner_a, second_inner_b)
 	-----
===================================================
Filename: /Users/abc/Documents/projects/packaging_helper/src/test.py,
Function name: divide, params: {'a': 1, 'b': 0}
	-----
	Line: 15,     return a / b
 	-->ROOT CAUSE: ZeroDivisionError: division by zero 
	-----

Message saved to logfile on S3:
Error in function outer_function 
===================================================
Filename: /Users/abc/Documents/projects/packaging_helper/src/test.py,
Function name: outer_function, params: {'outer_a': 1, 'outer_b': 0}
	-----
	Line: 28,     return first_inner_function(outer_a, outer_b)
 	-----
===================================================
Filename: /Users/abc/Documents/projects/packaging_helper/src/test.py,
Function name: first_inner_function, params: {'first_inner_a': 1, 'first_inner_b': 0}
	-----
	Line: 23,     return second_inner_function(first_inner_a, first_inner_b)
 	-----
===================================================
Filename: /Users/abc/Documents/projects/packaging_helper/src/test.py,
Function name: second_inner_function, params: {'second_inner_a': 1, 'second_inner_b': 0}
	-----
	Line: 19,     return divide(second_inner_a, second_inner_b)
 	-----
===================================================
Filename: /Users/abc/Documents/projects/packaging_helper/src/test.py,
Function name: divide, params: {'a': 1, 'b': 0}
	-----
	Line: 15,     return a / b
 	-->ROOT CAUSE: ZeroDivisionError: division by zero 
	-----

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ca_vntl_helper-1.0.4.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ca_vntl_helper-1.0.4-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file ca_vntl_helper-1.0.4.tar.gz.

File metadata

  • Download URL: ca_vntl_helper-1.0.4.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.4

File hashes

Hashes for ca_vntl_helper-1.0.4.tar.gz
Algorithm Hash digest
SHA256 e0f49245a35b89503e8ee63e427ae630d59c7e744658ebcb65b85b0ed5aad92f
MD5 7ac29e8594965dcd41973405d35155c3
BLAKE2b-256 09a3d36499cc1e3ed1c7eaa2cc34ce2d9d94b6fcbe31e7f6fe8b79cf0cf14266

See more details on using hashes here.

File details

Details for the file ca_vntl_helper-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: ca_vntl_helper-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.4

File hashes

Hashes for ca_vntl_helper-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2b60484ada8fa53764eb3b000727812c1721ac0cf1fd217cb433aae0ba0b4ef0
MD5 3d3db1b72012e678d00565c50cf3cd96
BLAKE2b-256 bb00ef602cdafef043d2a67bc804ee6cffcbc512058f26a03422309c77c0ba87

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page