Skip to content

Instantly share code, notes, and snippets.

@fcojperez
Last active November 25, 2024 10:52
Show Gist options
  • Save fcojperez/63fe53819c958e0ce1832923f3c80ebb to your computer and use it in GitHub Desktop.
Save fcojperez/63fe53819c958e0ce1832923f3c80ebb to your computer and use it in GitHub Desktop.
A python framework simulator

FRAMEWORK DECORATOR

This gist is inspired by the video https://youtube.com/shorts/GysTi7uNTTI?si=Tb5WucfNBb5QoLAa

The goal of the video and this script, it's to emulate an framework similar to Flask or CDK

REQUIREMENTS

  • Virtual Python environment (recommended)

HOW TO RUN:

  • Download the script framework_decorator.py
  • Then inside of you virtual environment, you execute python framework_decorator.py
import datetime
class App:
"""
A simple web application class with a route decorator.
"""
def __init__(self):
pass
def route(self, path:str):
"""
A decorator to register routes to the app.
"""
def decorator(func):
print(f"Route registered: {path}")
return func
return decorator
app = App()
@app.route("/")
def default(time: datetime.datetime):
print(f'Default function')
print(f"It's {time.isoformat()}")
if __name__ == '__main__':
print(f'Running the script and calling the function decorated default()')
now = datetime.datetime.now()
default(time=now)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment