Skip to main content

AOP (Aspect-Oriented Programming) helper for Python

Project description

AOPHelper – Python을 위한 강력한 AOP 라이브러리

AOPHelper는 Python에서 **Aspect-Oriented Programming(AOP, 관점 지향 프로그래밍)**을 쉽게 적용할 수 있도록 도와주는 라이브러리입니다.
@Aspect 데코레이터를 사용하여 로깅, 성능 측정, 예외 처리 등을 간편하게 적용하세요!


🚀 설치 방법

pip install aophelper

🔥 빠른 시작

✅ 기본 AOP 적용 예제

from aophelper import Aspect, BaseAdvice

class LoggingAdvice(BaseAdvice):
    def before(self, func, *args, **kwargs):
        print(f"🚀 {func.__name__} 실행 시작!")

    def after(self, func, result, *args, **kwargs):
        print(f"✅ {func.__name__} 실행 완료! 결과: {result}")

aspect = Aspect()
aspect.set_advice(LoggingAdvice())

@aspect.apply
def add(x, y):
    return x + y

print(add(10, 20))

출력 결과

🚀 add 실행 시작! ✅ add 실행 완료! 결과: 30

📌 주요 기능

기능 설명

  • ✅ before() 함수 실행 전에 수행할 작업 추가
  • ⚡ around() 함수 실행 시간 측정 및 실행 방식 제어 가능
  • 🛑 on_exception() 예외 발생 시 자동 처리 및 로깅 가능
  • 📢 after() 함수 실행 후 결과를 후처리 가능
  • 🔄 비동기 지원 async def 함수에도 AOP 적용 가능

🛠 각 기능의 예제 코드

✅ before() – 함수 실행 전에 동작 추가

from aophelper import Aspect, BaseAdvice

class MyAdvice(BaseAdvice):
    def before(self, func, *args, **kwargs):
        print(f"{func.__name__} 실행 전! 매개변수: {args}, {kwargs}")

aspect = Aspect()
aspect.set_advice(MyAdvice())

@aspect.apply
def greet(name):
    print(f"안녕하세요, {name}님!")

greet("Alice")

출력 결과

greet 실행 전! 매개변수: ('Alice',), {}
안녕하세요, Alice님!

⚡ around() – 실행 시간 측정 및 동작 변경

import time

class TimerAdvice(BaseAdvice):
    def around(self, func, *args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        end = time.time()
        print(f"{func.__name__} 실행 시간: {end - start:.4f}초")
        return result

aspect.set_advice(TimerAdvice())

@aspect.apply
def slow_function():
    time.sleep(2)
    print("작업 완료!")

slow_function()

출력 결과

작업 완료!
slow_function 실행 시간: 2.0050초

🛑 on_exception() – 예외 발생 시 자동 처리

class ErrorHandlerAdvice(BaseAdvice):
    def on_exception(self, func, exception, *args, **kwargs):
        print(f"{func.__name__}에서 오류 발생: {exception}")

aspect.set_advice(ErrorHandlerAdvice())

@aspect.apply
def error_function():
    raise ValueError("잘못된 값!")

error_function()

출력 결과

error_function에서 오류 발생: 잘못된 값!

📢 after() – 실행 후 후처리

class PostProcessingAdvice(BaseAdvice):
    def after(self, func, result, *args, **kwargs):
        print(f"{func.__name__} 실행 완료! 결과: {result}")

aspect.set_advice(PostProcessingAdvice())

@aspect.apply
def multiply(x, y):
    return x * y

print(multiply(5, 10))

출력 결과

multiply 실행 완료! 결과: 50
50

🔄 비동기 함수(async def) 지원

AOPHelper는 async def 함수에도 적용할 수 있습니다.

import asyncio

class AsyncAdvice(BaseAdvice):
    async def before(self, func, *args, **kwargs):
        print(f"(비동기) {func.__name__} 실행 전!")

aspect.set_advice(AsyncAdvice())

@aspect.apply
async def async_task():
    await asyncio.sleep(1)
    print("비동기 작업 완료!")

asyncio.run(async_task())

출력 결과

(비동기) async_task 실행 전!
비동기 작업 완료!

이제 더 강력한 AOP 기능을 활용해 보세요! 🚀

기여 방법

버그 리포트나 기능 제안은 GitHub Issues에 남겨주세요

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

aophelper-0.1.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

aophelper-0.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file aophelper-0.1.0.tar.gz.

File metadata

  • Download URL: aophelper-0.1.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.10

File hashes

Hashes for aophelper-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9be9b8d65c98ca2fb22bd0f05d2a3d777bc168ea7924f5d3f49ef2cf51e8d3c3
MD5 baa98bc5dc6d655417c11d83f03ef241
BLAKE2b-256 c68c52bb6fef60fa9747dfba2c8c839f4ec5b7cdc0fc4078e037cb20853f1a7d

See more details on using hashes here.

File details

Details for the file aophelper-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: aophelper-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.10

File hashes

Hashes for aophelper-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a60b1cd8f36e70b4e4619b4d19d5ae67ff015d8c9ae5db87f2077f760ec9ea47
MD5 b8524855fb0e1f70819cff512e6d57dc
BLAKE2b-256 01fef495c3450d91ecaee007dd9373dbbf2c4ad28c4d2478aa1b6c938f7d7107

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