# FastLab
An extension library for FastAPI framework
[![Supported Versions](https://img.shields.io/pypi/pyversions/fastlab.svg)](https://pypi.org/project/fastlab)
[![PyPI version](https://badge.fury.io/py/fastlab.svg)](https://pypi.org/project/fastlab)
[![License](https://img.shields.io/github/license/tezignlab/fastlab)](https://github.com/tezignlab/fastlab/blob/main/LICENSE)
## Features
- [FastLab](#fastlab)
- [Features](#features)
- [Installation](#installation)
- [Getting started](#getting-started)
- [Logging](#logging)
- [Models](#models)
- [🔰 Response](#-response)
- [🔰 PageData](#-pagedata)
- [Utils](#utils)
- [🔰 TimeUtils](#-timeutils)
- [Routers](#routers)
- [🔰 HealthRouter](#-healthrouter)
- [Decorators](#decorators)
- [🔰 LogExecTime](#-logexectime)
- [🔰 WithEnvConfig](#-withenvconfig)
- [Testing](#testing)
## Installation
use `pip` to install the package:
```shell
pip install fastlab
```
## Getting started
### Logging
Easy to log string to console, see more: [https://docs.python.org/3/library/logging.html](https://docs.python.org/3/library/logging.html)
```python
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
```python
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:
```json
{
"code": 0,
"message": "",
"data": {
"name": "fastlab",
"version": "0.1.0"
}
}
```
#### 🔰 PageData
```python
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
```python
from fastlab.utils import TimeUtils
# Print now timestamp: 1639732030521
print(TimeUtils.timestamp())
```
### Routers
#### 🔰 HealthRouter
API for health check, endpoint `/health`.
```python
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.
```python
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:
1. Change the setting name to uppercase
2. Prefix it with `prefix` setting
3. Escape any underscores (`_`) by duplicating them
4. Convert all periods (.) to underscores (`_`)
```python
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
```
Raw data
{
"_id": null,
"home_page": "https://github.com/tezignlab/fastlab",
"name": "fastlab",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "fastapi logs utils models routers decorators",
"author": "Anoyi",
"author_email": "anoyi@qq.com",
"download_url": "https://files.pythonhosted.org/packages/6d/08/fb2fff990e6c193016a8b9f16890da47c095511be264aeb2ea9af3aa6b30/fastlab-0.3.0.tar.gz",
"platform": null,
"description": "# FastLab\n\nAn extension library for FastAPI framework\n\n[![Supported Versions](https://img.shields.io/pypi/pyversions/fastlab.svg)](https://pypi.org/project/fastlab)\n[![PyPI version](https://badge.fury.io/py/fastlab.svg)](https://pypi.org/project/fastlab)\n[![License](https://img.shields.io/github/license/tezignlab/fastlab)](https://github.com/tezignlab/fastlab/blob/main/LICENSE)\n\n\n## Features\n\n- [FastLab](#fastlab)\n - [Features](#features)\n - [Installation](#installation)\n - [Getting started](#getting-started)\n - [Logging](#logging)\n - [Models](#models)\n - [\ud83d\udd30 Response](#-response)\n - [\ud83d\udd30 PageData](#-pagedata)\n - [Utils](#utils)\n - [\ud83d\udd30 TimeUtils](#-timeutils)\n - [Routers](#routers)\n - [\ud83d\udd30 HealthRouter](#-healthrouter)\n - [Decorators](#decorators)\n - [\ud83d\udd30 LogExecTime](#-logexectime)\n - [\ud83d\udd30 WithEnvConfig](#-withenvconfig)\n - [Testing](#testing)\n\n\n## Installation\n\nuse `pip` to install the package:\n\n```shell\npip install fastlab\n```\n\n## Getting started\n\n### Logging\n\nEasy to log string to console, see more: [https://docs.python.org/3/library/logging.html](https://docs.python.org/3/library/logging.html)\n\n```python\nfrom fastlab import logs\n\nlogs.warning('warn') # 2021-12-18 14:23:31.000 WARNING 88493 --- [ MainThread] test_logs : warn\nlogs.info('info') # 2021-12-18 14:23:31.000 INFO 88493 --- [ MainThread] test_logs : info\nlogs.error('error') # 2021-12-18 14:23:31.000 ERROR 88493 --- [ MainThread] test_logs : error\n```\n\n### Models\n\nCommon Models\n\n#### \ud83d\udd30 Response\n\n```python\nfrom fastapi import FastAPI\nfrom pydantic import BaseModel\nfrom fastlab.models import Response\n\n\nclass Item(BaseModel):\n name: str\n version: str\n\n\napp = FastAPI()\n\n\n@app.get(\"/item\", response_model=Response[Item])\nasync def item():\n return Response(data=Item(name='fastlab', version='0.1.0'))\n```\n\nGet `http://localhost:8080/item` response: \n```json\n{\n \"code\": 0,\n \"message\": \"\",\n \"data\": {\n \"name\": \"fastlab\",\n \"version\": \"0.1.0\"\n }\n}\n```\n\n#### \ud83d\udd30 PageData\n\n```python\nfrom fastapi import FastAPI\nfrom pydantic import BaseModel\nfrom fastlab.models import Response, PageData\n\n\nclass Item(BaseModel):\n name: str\n version: str\n\n\napp = FastAPI()\n\n\n@app.get(\"/items\", response_model=Response[PageData[Item]])\nasync def items(skip: int = 0, limit: int = 10):\n total = 100\n data = [Item(name=f'fastlab-{i}', version=f'0.1.{i}') for i in range(skip, skip + limit)]\n return Response(data=PageData(skip=skip, limit=limit, total=total, has_more=total > skip + limit, data=data))\n```\n\n\n### Utils\n\n#### \ud83d\udd30 TimeUtils\n\n```python\nfrom fastlab.utils import TimeUtils\n\n# Print now timestamp: 1639732030521\nprint(TimeUtils.timestamp())\n```\n\n### Routers\n\n#### \ud83d\udd30 HealthRouter\n\nAPI for health check, endpoint `/health`.\n\n```python\nfrom fastapi import FastAPI\nfrom fastlab.routers import HealthRouter\n\napp = FastAPI()\napp.include_router(HealthRouter)\n```\n\n### Decorators\n\n#### \ud83d\udd30 LogExecTime\n\nLog wrapper for measuring sync & async function execution times.\n\n```python\nimport asyncio\nimport time\nfrom fastlab.decorators import LogExecTime\n\n@LogExecTime\ndef log_sleep():\n time.sleep(1.5)\n\n@LogExecTimeAsync\nasync def log_sleep_async():\n await asyncio.sleep(1.5)\n```\n\n\n#### \ud83d\udd30 WithEnvConfig\n\nReplace the configuration with system environment variables. Follows:\n\n1. Change the setting name to uppercase\n2. Prefix it with `prefix` setting\n3. Escape any underscores (`_`) by duplicating them\n4. Convert all periods (.) to underscores (`_`)\n\n```python\nfrom fastlab.decorators import WithEnvConfig\n\n@WithEnvConfig(prefix='FL_')\ndef load_config():\n return {\n 'name': 'fastlab', \n 'version': '0.2.1',\n 'extra': {\n 'memory_lock': False\n }\n }\n\nconf = load_config()\n```\n\nFor example, `FL_EXTRA_MEMORY__LOCK=true` transform `conf['extra']['memory_lock']` as `True`\n\n\n## Testing\n\nInstall this package locally\n\n```\npython setup.py develop\n```\n\nRun test case\n\n```\npython tests/test_logs.py\n```\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "An extension library for FastAPI framework",
"version": "0.3.0",
"project_urls": {
"Homepage": "https://github.com/tezignlab/fastlab"
},
"split_keywords": [
"fastapi",
"logs",
"utils",
"models",
"routers",
"decorators"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "97e04f8549d9f1b5cc76ecb4fab7982ba3584b1ce0a35f2601ac19d38d5592e9",
"md5": "b8cd91f96b99d4d1956d364f51bbaaa2",
"sha256": "e53d6691de61d4ced652d678dfc2d225f54d0f8e1a8c2b8bc7bb346f890a9970"
},
"downloads": -1,
"filename": "fastlab-0.3.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b8cd91f96b99d4d1956d364f51bbaaa2",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 7194,
"upload_time": "2024-03-19T10:26:54",
"upload_time_iso_8601": "2024-03-19T10:26:54.467053Z",
"url": "https://files.pythonhosted.org/packages/97/e0/4f8549d9f1b5cc76ecb4fab7982ba3584b1ce0a35f2601ac19d38d5592e9/fastlab-0.3.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6d08fb2fff990e6c193016a8b9f16890da47c095511be264aeb2ea9af3aa6b30",
"md5": "dc0302c1ec224511ce2d6e8ef2894a36",
"sha256": "8aa44fc417436e27770391e8f73b918b1c536a84346350c57dc224619058eee8"
},
"downloads": -1,
"filename": "fastlab-0.3.0.tar.gz",
"has_sig": false,
"md5_digest": "dc0302c1ec224511ce2d6e8ef2894a36",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 8048,
"upload_time": "2024-03-19T10:26:55",
"upload_time_iso_8601": "2024-03-19T10:26:55.501761Z",
"url": "https://files.pythonhosted.org/packages/6d/08/fb2fff990e6c193016a8b9f16890da47c095511be264aeb2ea9af3aa6b30/fastlab-0.3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-03-19 10:26:55",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "tezignlab",
"github_project": "fastlab",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "fastlab"
}