📜  pytohn epsilon - Python (1)

📅  最后修改于: 2023-12-03 15:34:32.870000             🧑  作者: Mango

Python Epsilon

Introduction

Python Epsilon is a Python package that adds useful utilities to the standard library. It's intended to be lightweight, efficient, and easy to use.

Features
Time utilities

Python Epsilon includes several time-related utilities, including convenient conversions between different time units and an easy-to-use timer class.

from epsilon import timeutils

# convert between time units
time_in_seconds = 120
time_in_minutes = timeutils.seconds_to_minutes(time_in_seconds)
time_in_hours = timeutils.seconds_to_hours(time_in_seconds)

# use the Timer class to time code execution
with timeutils.Timer() as t:
    # your code here
print(f"Code took {t.seconds} seconds to run.")
Exception handling

Python Epsilon provides a useful base class for creating custom exceptions, as well as a function for easily logging exceptions to file.

from epsilon import exceptions

class MyCustomException(exceptions.BaseException):
    def __init__(self, message):
        super().__init__(message)

try:
    # some code that might raise an exception
except Exception as e:
    exceptions.log_exception(e, "error.log")
    raise MyCustomException("Something went wrong.")
Logging utilities

Python Epsilon includes a simple, customizable logging function that logs messages to both file and console.

from epsilon import logutils

logutils.setup_logging()

logutils.log("This message is logged to both file and console.")
Other utilities

Python Epsilon also includes several other miscellaneous utility functions, including:

  • A function for generating random strings
  • A function for finding the nth occurrence of a substring in a string
  • A function for creating a directory if it doesn't exist
from epsilon import miscutils

# generate a random 10-character string
rand_str = miscutils.random_string(10)

# find the 3rd occurrence of 'hello' in a string
index = miscutils.find_nth("hello world hello", "hello", 3)

# create a directory if it doesn't exist
miscutils.create_dir_if_not_exists("my_dir")
Conclusion

Python Epsilon adds a variety of useful utilities to the Python standard library. Whether you need time-related utilities, custom exceptions, logging functionality, or other miscellaneous functions, Python Epsilon has you covered.