Skip to content

Commit

Permalink
add 'Data Classes' and 'FastAPI Project' recipes. (#272)
Browse files Browse the repository at this point in the history
* add dataclasses.
* add 'FastAPI Project' recipe.
  • Loading branch information
lucas-six authored Mar 3, 2024
1 parent 7499058 commit 34a82b9
Show file tree
Hide file tree
Showing 11 changed files with 871 additions and 629 deletions.
2 changes: 0 additions & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ name = "pypi"
pydantic = "*"
redis = {version = "*", extras = ["hiredis"]}
types-redis = "*"
motor = "*"
#uvloop = {version = "*", markers = "sys_platform != 'win32'"}
celery = {version = "*", extras = ["librabbitmq", "mongodb", "redis"]}
#requests = "*"
Expand All @@ -21,7 +20,6 @@ isort = "*"
mypy = "*"
pylint = "*"
pylint-pydantic = "*"
pylint-print = "*"
#pylint-django = "*"
#colorlog = "*"
#pytest = "*"
Expand Down
1,131 changes: 562 additions & 569 deletions Pipfile.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

- [Tuples with Named Fields - `namedtuple` (命名元组)](https://lucas-six.github.io/python-cookbook/cookbook/core/ext_type/namedtuple)
- [Ordered Dictionary - `OrderedDict`](https://lucas-six.github.io/python-cookbook/cookbook/core/ext_type/ordereddict)
- [Data Classes - `dataclasses`(数据类)](https://lucas-six.github.io/python-cookbook/cookbook/core/ext_type/dataclass)

### Date & Time (日期时间)

Expand Down Expand Up @@ -144,6 +145,7 @@
### Project

- [Project: `pyproject.toml`](https://lucas-six.github.io/python-cookbook/cookbook/build/project)
- [FastAPI Project](https://lucas-six.github.io/python-cookbook/cookbook/build/project_fastapi)
- `black`
- `isort`
- `mypy`
Expand Down
76 changes: 41 additions & 35 deletions cookbook/build/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,9 @@ pipenv --python 3.11
pipenv install --dev black isort mypy pylint
```

### Web Development: `FastAPI`

```bash
pipenv install pydantic
pipenv install --dev pylint-pydantic
```

## `pyproject.toml`

```toml
```ini
[project]
name = "<project_name>"
description = "<project description>"
Expand Down Expand Up @@ -45,9 +38,6 @@ classifiers = [
"Typing :: Typed",
]
dependencies = [
# Web Development: FastAPI
"pydantic",

"psycopg2 >= 2.8",
"redis >= 4.0",

Expand All @@ -62,10 +52,6 @@ test = [
"isort",
"mymy",
"pylint",

# Web Development: FastAPI
"pylint-pydantic",
"pylint-print",
]
doc = []

Expand Down Expand Up @@ -111,13 +97,9 @@ profile = "black"
skip_gitignore = true
extend_skip = [".gitignore", ".env", ".dockerignore"]
# skip_glob = []
extend_skip_glob = ["*/migrations/*"]

[tool.mypy]
python_version = "3.11"
plugins = [
"pydantic.mypy"
]
exclude = [
"test_main.py",
]
Expand All @@ -130,12 +112,6 @@ check_untyped_defs = true
no_implicit_reexport = true
disallow_untyped_defs = true

[tool.pydantic-mypy]
init_forbid_extra = true
init_typed = true
warn_required_dynamic_aliases = true
warn_untyped_fields = true

# mypy for MongoDB motor
[[tool.mypy.overrides]]
module = "motor.*"
Expand All @@ -148,12 +124,6 @@ jobs = 0
ignore = "CVS,.git,__pycache__,.mypy_cache,tests"
ignore-paths = "tests"
ignore-patterns = "test_.*.py"
ignored-classes = "Body"
extension-pkg-whitelist = "pydantic"
load-plugins = [
"pylint_pydantic",
"pylint_print",
]

[tool.pylint.'FORMAT']
max-line-length = 88
Expand Down Expand Up @@ -191,19 +161,55 @@ exclude = [
".git",
"**/__pycache__",
"**/.mypy_cache",
]
reportGeneralTypeIssues = "none"
reportUnboundVariable = "none"
stubPath = ""
pythonVersion = "3.11"
```

## Web Development: `Django`

```bash
pipenv install "django~=4.2"
```

```ini
dependencies = [
"django~=4.2",
]

[project.optional-dependencies]
test = [
"pylint-django",
]

[tool.pylint.main]
ignore-patterns = "test_.*.py,manage.py,settings.py"
load-plugins = [
"pylint.extensions.bad_builtin",
"pylint_django",
]

[tool.pylint.deprecated_builtins]
bad-functions = ["map", "filter", "print"]

[tool.isort]
extend_skip_glob = ["*/migrations/*"]

[tool.pyright]
exclude = [
"**/migrations",
]
ignore = [
"**/admin.py",
]
reportGeneralTypeIssues = "none"
stubPath = ""
pythonVersion = "3.11"
```

## More

- [`pipenv`](pkg/pipenv)
- [`pipenv` - Python Cookbook](pkg/pipenv)
- [FastAPI Project](project_fastapi)

## References

Expand Down
198 changes: 198 additions & 0 deletions cookbook/build/project_fastapi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
# FastAPI Project

First, set up [Base Project](project).

## Setup Environment

```bash
pipenv install pydantic
pipenv install --dev pylint-pydantic

pipenv install fastapi[all]
pipenv install uvicorn[standard]

pipenv install motor # MongoDB asyncio driver
```

## `pyproject.toml`

```ini
[project]
name = "<project_name>"
description = "<project description>"
authors = [
{name = "<Author Name>", email = "<author@email>"},
{name = "Lucas", email = "[email protected]"},
]
readme = "README.md"
requires-python = "~=3.11"
license = {file = "LICENSE"}
maintainers = [
{name = "<Maintainer Name>", email = "<maintainer@email>"},
]
keywords = ["xxx"]
classifiers = [
"Development Status :: 1 - Planning",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: Implementation :: CPython",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Utilities",
"Operating System :: OS Independent",
"License :: OSI Approved :: MIT License",
"License :: OSI Approved :: Apache Software License",
"Typing :: Typed",
]
dependencies = [
"pydantic",
"fastapi[all]",
"uvicorn[standard]",
"motor",
]
dynamic = ["version"]

[project.optional-dependencies]
test = [
"black",
"isort",
"mymy",
"pylint",
"pylint-pydantic",
]
doc = []

[project.urls]
Home = "<URL>"
Documentation = "<URL>"
Source = "<URL>"

[tool.black]
line-length = 88
target-version = ['py310', 'py311']
skip-string-normalization = true
include = '\.pyi?$'
extend-exclude = '''
tests/.*\.py$
migrations/.*\.py$
'''

[tool.isort]
src_paths = ["src", "app"]
atomic = true
profile = "black"
# skip = [
# '.bzr',
# '.direnv',
# '.eggs',
# '.git',
# '.hg',
# '.mypy_cache',
# '.nox',
# '.pants.d',
# '.svn',
# '.tox',
# '.venv',
# '__pypackages__',
# '_build',
# 'buck-out',
# 'build',
# 'dist',
# 'node_modules',
# 'venv'
# ]
skip_gitignore = true
extend_skip = [".gitignore", ".env", ".dockerignore"]
# skip_glob = []

[tool.mypy]
python_version = "3.11"
plugins = [
"pydantic.mypy"
]
exclude = [
"test_main.py",
]
follow_imports = "silent"
warn_redundant_casts = true
warn_unused_ignores = true
warn_unused_configs = true
disallow_any_generics = true
check_untyped_defs = true
no_implicit_reexport = true
disallow_untyped_defs = true

[tool.pydantic-mypy]
init_forbid_extra = true
init_typed = true
warn_required_dynamic_aliases = true
warn_untyped_fields = true

# mypy for MongoDB motor
[[tool.mypy.overrides]]
module = "motor.*"
ignore_missing_imports = true

[tool.pylint.main]
recursive = true
py-version = 3.11
jobs = 0
ignore = "CVS,.git,__pycache__,.mypy_cache,tests"
ignore-paths = "tests"
ignore-patterns = "test_.*.py"
ignored-classes = "Body"
extension-pkg-whitelist = "pydantic"
load-plugins = [
"pylint.extensions.bad_builtin",
"pylint_pydantic",
]

[tool.pylint.'FORMAT']
max-line-length = 88

[tool.pylint.'LOGGING']
logging-format-style = "new"

[tool.pylint.'MESSAGES CONTROL']
disable = [
"raw-checker-failed",
"bad-inline-option",
"locally-disabled",
"file-ignored",
"suppressed-message",
"useless-suppression",
"deprecated-pragma",
"use-symbolic-message-instead",
"logging-fstring-interpolation"
]
enable = [
"c-extension-no-member",
]

[tool.pylint.design]
max-args = 12
min-public-methods = 1
max-locals = 22

[tool.pylint.deprecated_builtins]
bad-functions = ["map", "filter", "print"]

[tool.pyright]
include = [
"src",
"app",
]
exclude = [
".git",
"**/__pycache__",
"**/.mypy_cache",
]
reportGeneralTypeIssues = "none"
reportUnboundVariable = "none"
stubPath = ""
pythonVersion = "3.11"
```

## References

- [Python Project - Python Cookbook](project)
- [`Pydantic`](https://pydantic-docs.helpmanual.io/): data validation
Loading

0 comments on commit 34a82b9

Please sign in to comment.