Skip to main content

CodeAlchemy is a Python package that provides utility and decorators for development.

Project description

CodeAlchemy

PyPI version License Python versions

codealchemy is a Python package that provides decorators and utilities for Development CodeAlchemy is a versatile Python package designed to simplify development workflows by providing powerful decorators for logging, performance monitoring, and utilities. Whether you're looking to track function execution times, group logs for better readability, or Utility methods to interact with other services.

Key Features:

  • Decorators: Enhance your Python functions with performance tracking and logging capabilities.
  • Kafka Utilities: Easily produce and consume messages with Kafka, with advanced features like round-robin message distribution and offset management.

Installation

You can install codealchemy using pip:

pip install codealchemy

Usage

Here is how you can use the codealchemy package in your Python code:

Decorators

  • code_execution_time: This decorator logs the execution time of the decorated function.
  • log_group(group_name): This decorator logs entry and exit points of the decorated function, grouping logs under a specified group name.
  1. code execution time
from codealchemy import code_execution_time

@code_execution_time
def example_function():
    import time
    time.sleep(2)
    print("Function executed")

example_function()
  1. Log groups
from codealchemy import log_group
import logging

# Ensure the logger level is set to INFO
logging.getLogger(__name__).setLevel(logging.INFO)

@log_group("MainGroup")
def main_function():
    print("Inside main function")
    print("*"*20)
    logging.info("Inside main function")
    inner_function()

@log_group("InnerGroup")
def inner_function():
    print("Inside inner function")
    print("*"*20)
    logging.info("Inside inner function")
    innermost_function()

@log_group("InnermostGroup")
def innermost_function():
    print("Inside innermost function")
    print("*"*20)
    logging.info("Inside innermost function")
    print("Innermost function executed")

main_function()

image

Utility

  • kafkaUtils: A utility module that simplifies Kafka-related operations for Python developers.
  1. Kafka Producer: The Kafka producer sends messages to Kafka topics. Here's how you can use it:

    from codealchemy import kafkaUtils
    
    config_file = "config.json"
    topic = "topic_name"
    
    producer = kafkaUtils.KafkaProducer(config_file, topic)
    
    
    print("\nProducing Messages to default partition:")
    # Send messages to default partitions
    for i in range(10):
        # Construct the message payload
        message = {"key": i, "value": f"Test message {i}"}
        producer.send_message(message)
    # Flush to ensure all messages are sent
    producer.flush()
    print("\nLatest offsets:")
    producer.display_partition_offsets()
    
    
    print("\nProducing Messages to Particular partition:")
    # Send messages to specific partitions
    for i in range(10):
        # Construct the message payload
        message = {"key": i, "value": f"Test message {i}"}
        producer.send_message(message, partition=1)
    producer.flush()
    print("\nLatest offsets:")
    producer.display_partition_offsets()
    
    
    print("\nProducing Messages in Round Robin:")
    # Send messages in a round-robin fashion across partitions
    for i in range(10):
        message = {"key": i, "value": f"Test message {i}"}
        producer.send_message_round_robin(message)
    producer.flush()
    print("\nLatest offsets:")
    producer.display_partition_offsets()
    
  2. Kafka Consumer: The Kafka consumer consumes messages from Kafka topics. Here's how you can use it:

    from codealchemy import kafkaUtils
    
    config_file = "config.json"
    topic = "topic_name"
    group_id = "group_id"
    
    auto_offset = "earliest"
     # earliest -->Start from the beginning if no previous offset exists
     # latest -->Start from the end if no previous offset exists
    
    consumer = kafkaUtils.KafkaConsumer(config_file, topic, group_id, auto_offset)
    consumer.consume_messages()
    

    If you need to consume from particular offset

    consumer = kafkaUtils.KafkaConsumer(config_file, topic, group_id, auto_offset, offset=10)
    consumer.consume_from_offset(offset=2350, partition=1)
    # consumer.consume_from_offset(offset=2350) # For all partition
    

    If you need to consume from particular timestamp

    consumer = kafkaUtils.KafkaConsumer(config_file, topic, group_id, auto_offset, offset=10)
    consumer.consume_from_timestamp(timestamp="2024-11-13T22:39:00.999Z", partition=1)
    # consumer.consume_from_timestamp(timestamp="2024-11-13T22:39:00.999Z") # For all partition
    

    Sample config.json

    {
      "bootstrap.servers": "kafkabroker:9093",
      "security.protocol": "SSL",
      "ssl.key.password": "Password@1",
      "ssl.certificate.location": "/etc/secrets/kafka/truststore.pem",
      "ssl.key.location": "/etc/secrets/kafka/keystore.pem",
      "ssl.ca.location": "/etc/secrets/kafka/caroot.pem"
    }
    

License

This project is licensed under the MIT License. See the LICENSE file for more details.

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

codealchemy-1.2.3.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

codealchemy-1.2.3-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file codealchemy-1.2.3.tar.gz.

File metadata

  • Download URL: codealchemy-1.2.3.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.15

File hashes

Hashes for codealchemy-1.2.3.tar.gz
Algorithm Hash digest
SHA256 ebbe5788c1b2f5db0e90427d2f95ac20c0aa2aa7fc798544991a49928b4d1871
MD5 49dc2a4a2d0766ffe590de4864c2440e
BLAKE2b-256 6b1e07460fdc889541c6f48a54b719378b2ec8e46fe3f649550222f71240072c

See more details on using hashes here.

File details

Details for the file codealchemy-1.2.3-py3-none-any.whl.

File metadata

  • Download URL: codealchemy-1.2.3-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.15

File hashes

Hashes for codealchemy-1.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a7bb8412082e71f2d3f5209fb32ff1caad05c5d4708515a86f80af5078ef8d46
MD5 e6f39b9e78caf19760b22c558923da26
BLAKE2b-256 657ace342dddc9f39ae6c4fd057883d6d19ab5c38ad4e4de609ac28bb369a657

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