FastLab
An extension library for FastAPI framework
Features
Installation
use pip to install the package:
pip install fastlab
Getting started
Logging
Easy to log string to console, see more: https://docs.python.org/3/library/logging.html
from fastlab import logs
logs.warning('warn') # 2021-12-18 14:23:31.000 WARNING 88493 --- [ MainThread] test_logs : warn
logs.info('info') # 2021-12-18 14:23:31.000 INFO 88493 --- [ MainThread] test_logs : info
logs.error('error') # 2021-12-18 14:23:31.000 ERROR 88493 --- [ MainThread] test_logs : error
Models
Common Models
🔰 Response
from fastapi import FastAPI
from pydantic import BaseModel
from fastlab.models import Response
class Item(BaseModel):
name: str
version: str
app = FastAPI()
@app.get("/item", response_model=Response[Item])
async def item():
return Response(data=Item(name='fastlab', version='0.1.0'))
Get http://localhost:8080/item response:
{
"code": 0,
"message": "",
"data": {
"name": "fastlab",
"version": "0.1.0"
}
}
🔰 PageData
from fastapi import FastAPI
from pydantic import BaseModel
from fastlab.models import Response, PageData
class Item(BaseModel):
name: str
version: str
app = FastAPI()
@app.get("/items", response_model=Response[PageData[Item]])
async def items(skip: int = 0, limit: int = 10):
total = 100
data = [Item(name=f'fastlab-{i}', version=f'0.1.{i}') for i in range(skip, skip + limit)]
return Response(data=PageData(skip=skip, limit=limit, total=total, has_more=total > skip + limit, data=data))
Utils
🔰 TimeUtils
from fastlab.utils import TimeUtils
# Print now timestamp: 1639732030521
print(TimeUtils.timestamp())
Routers
🔰 HealthRouter
API for health check, endpoint /health.
from fastapi import FastAPI
from fastlab.routers import HealthRouter
app = FastAPI()
app.include_router(HealthRouter)
Decorators
🔰 LogExecTime
Log wrapper for measuring sync & async function execution times.
import asyncio
import time
from fastlab.decorators import LogExecTime
@LogExecTime
def log_sleep():
time.sleep(1.5)
@LogExecTimeAsync
async def log_sleep_async():
await asyncio.sleep(1.5)
🔰 WithEnvConfig
Replace the configuration with system environment variables. Follows:
- Change the setting name to uppercase
- Prefix it with
prefixsetting - Escape any underscores (
_) by duplicating them - Convert all periods (.) to underscores (
_)
from fastlab.decorators import WithEnvConfig
@WithEnvConfig(prefix='FL_')
def load_config():
return {
'name': 'fastlab',
'version': '0.2.1',
'extra': {
'memory_lock': False
}
}
conf = load_config()
For example, FL_EXTRA_MEMORY__LOCK=true transform conf['extra']['memory_lock'] as True
Testing
Install this package locally
python setup.py develop
Run test case
python tests/test_logs.py
Metadata
Release files for fastlab 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fastlab-0.3.0.tar.gz | 8.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastlab-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.2 kB
Release files / fastlab-0.3.0.tar.gz
| Download URL | fastlab-0.3.0.tar.gz |
|---|---|
| Size | 8.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8aa44fc417436e27770391e8f73b918b1c536a84346350c57dc224619058eee8
|
|
BLAKE2b-256 checksum How to use checksums |
6d08fb2fff990e6c193016a8b9f16890da47c095511be264aeb2ea9af3aa6b30
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.18
|
Release files / fastlab-0.3.0-py3-none-any.whl
| Download URL | fastlab-0.3.0-py3-none-any.whl |
|---|---|
| Size | 7.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e53d6691de61d4ced652d678dfc2d225f54d0f8e1a8c2b8bc7bb346f890a9970
|
|
BLAKE2b-256 checksum How to use checksums |
97e04f8549d9f1b5cc76ecb4fab7982ba3584b1ce0a35f2601ac19d38d5592e9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.18
|