An extension library for FastAPI framework
Project description
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
🔰 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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastlab-0.2.1.tar.gz.
File metadata
- Download URL: fastlab-0.2.1.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ce7620d92b59a142be6c7657bbefa45b7da514bb9d0261c607714e850b078fc8
|
|
| MD5 |
a3517cf38f09462b97b023b49bff163e
|
|
| BLAKE2b-256 |
aa414d1dec4843cb7e97b0732be5116e9b75c58c02462efdf581522793ab72fa
|
File details
Details for the file fastlab-0.2.1-py3-none-any.whl.
File metadata
- Download URL: fastlab-0.2.1-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1842e3a9d6b976b284f042d87a6290b902e8281682e127359c76aa066830cfe0
|
|
| MD5 |
b788e5bc30a8137efe9581aefec52aae
|
|
| BLAKE2b-256 |
b037523c85d0314592ead8fed6065884143ab31313ce548f26cc89dd281d680c
|