Skip to content

Commit

Permalink
Implement array field #63
Browse files Browse the repository at this point in the history
  • Loading branch information
hakancelikdev committed Sep 29, 2023
1 parent 1a75a1e commit 2829504
Show file tree
Hide file tree
Showing 26 changed files with 350 additions and 70 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.5.0
- uses: actions/setup-python@v2.3.3
- uses: actions/checkout@v4.1.0
- uses: actions/setup-python@v4.7.0
with:
python-version: "3.10"
architecture: "x64"
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ jobs:
run:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.5.0
- uses: actions/setup-python@v2.3.3
- uses: actions/checkout@v4.1.0
- uses: actions/setup-python@v4.7.0
with:
python-version: "3.10"
python-version: "3.8"
architecture: "x64"
- name: Install dependencies for pre-commit
run: |
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.5.0
- uses: actions/setup-python@v2.3.3
- uses: actions/checkout@v4.1.0
- uses: actions/setup-python@v4.7.0
with:
python-version: "3.10"
architecture: "x64"
Expand All @@ -28,8 +28,8 @@ jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.5.0
- uses: actions/setup-python@v2.3.3
- uses: actions/checkout@v4.1.0
- uses: actions/setup-python@v4.7.0
with:
python-version: "3.10"
architecture: "x64"
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ jobs:
os: [ubuntu-latest, macos-latest]
python-version: ["3.8", "3.9", "3.10"]
steps:
- uses: actions/checkout@v2.5.0
- uses: actions/checkout@v4.1.0

- name: Set up Python${{ matrix.python-version }}
uses: actions/setup-python@v2.3.3
uses: actions/setup-python@v4.7.0
with:
python-version: ${{ matrix.python-version }}
architecture: x64
Expand All @@ -26,10 +26,10 @@ jobs:
coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.5.0
- uses: actions/checkout@v4.1.0

- name: Set up Python3.10
uses: actions/setup-python@v2.3.3
uses: actions/setup-python@v4.7.0
with:
python-version: "3.10"
architecture: x64
Expand Down
12 changes: 7 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# Install the git hook scripts, run `$ pre-commit install` to set up the git hook scripts
# Run against all the files, `$ pre-commit run --all-files`
# Updating hooks automatically: `$ pre-commit autoupdate`

default_language_version:
python: python3.8
fail_fast: true
repos:
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort

- repo: https://github.com/hakancelikdev/unimport
rev: 0.16.0
rev: 1.0.0
hooks:
- id: unimport
args:
Expand All @@ -26,21 +28,21 @@ repos:
- --refactor

- repo: https://github.com/myint/docformatter
rev: v1.6.5
rev: v1.7.5
hooks:
- id: docformatter
args: [--in-place]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.3.0
rev: v1.5.1
hooks:
- id: mypy
args: [--no-strict-optional, --ignore-missing-imports, --show-error-codes]
additional_dependencies: [types-toml==0.1.3]
exclude: "tests/"

- repo: https://github.com/pycqa/flake8
rev: 6.0.0
rev: 6.1.0
hooks:
- id: flake8
args:
Expand Down
19 changes: 17 additions & 2 deletions docs/tutorial/field-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,25 @@
`str`
> Field value can be a string.
`array`
> Field value can be an array of int, float and str.
This field is an alias of [`array.array`](https://docs.python.org/3/library/array.html) from the standard library.
It understands the type of the array from the type of the field, and it can be used to store arrays of int, float and str.

To use this field, firstly you need to import array from pydbm. Then you must specify the type of the array in square brackets after the type of the field.
Finally, you can use the field as a normal field.

For example, if you want to store an array of int, you can use `pydbm.array[int]` as the type of the field.
But when you want to define the type of the field, you must use `pydbm.array(...)` instead of `array[int]`.

```python
import datetime

from pydbm import DbmModel
import pydbm


class User(DbmModel):
class User(pydbm.DbmModel):
bool_field: bool
bytes_field: bytes
date_field: datetime.date
Expand All @@ -39,4 +51,7 @@ class User(DbmModel):
int_field: int
none_field: None
str_field: str
int_array: pydbm.array[int]
float_array: pydbm.array[float]
str_array: pydbm.array[str]
```
2 changes: 2 additions & 0 deletions docs/tutorial/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ If you pass unique together fields of the model, it will be faster.

```python
is_exists: bool = UserModel.objects.exists(username="hakancelik")
```


## Model properties

Expand Down
8 changes: 7 additions & 1 deletion docs/tutorial/validators.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ class UserModel(DbmModel):
- validate_max_value
- validate_min_value

These can be imported from pydbm.models.validators, or just import from pydbm
## Array Validators
- validate_array_int
- validate_array_float
- validate_array_str

```python
from pydbm import (
Expand All @@ -42,5 +45,8 @@ from pydbm import (
validate_str,
validate_max_value,
validate_min_value,
validate_array_int,
validate_array_float,
validate_array_str,
)
```
9 changes: 8 additions & 1 deletion src/pydbm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
DbmModel,
Field,
Undefined,
validate_array_float,
validate_array_int,
validate_array_str,
validate_bool,
validate_bytes,
validate_date,
Expand All @@ -14,7 +17,7 @@
validate_none,
validate_str,
)
from pydbm.typing_extra import NormalizationT, ValidatorT
from pydbm.typing_extra import NormalizationT, ValidatorT, array

__all__ = (
"PydbmBaseException",
Expand All @@ -33,6 +36,10 @@
"validate_min_value",
"validate_none",
"validate_str",
"validate_array_str",
"validate_array_int",
"validate_array_float",
"NormalizationT",
"ValidatorT",
"array",
)
4 changes: 2 additions & 2 deletions src/pydbm/database/data_types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


class BaseDataType(abc.ABC):
data_types: dict[SupportedClassT, typing.Type[BaseDataType]] = {}
data_types: typing.ClassVar[dict[SupportedClassT, typing.Type[BaseDataType]]] = {}

@staticmethod
@abc.abstractmethod
Expand All @@ -33,4 +33,4 @@ def get_data_type(cls, item: SupportedClassT) -> typing.Type[BaseDataType]:
try:
return cls.data_types[item]
except KeyError:
raise TypeError(f"Type {item} is not supported yet!")
raise TypeError(f"Type {item!r} is not supported yet!")
29 changes: 28 additions & 1 deletion src/pydbm/database/data_types/types.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from __future__ import annotations

import ast
import datetime

from pydbm.database.data_types.base import BaseDataType
from pydbm.typing_extra import array

__all__ = (
"ArrayFloatDataType",
"ArrayIntDataType",
"ArrayTextDataType",
"BoolDataType",
"BytesDataType",
"DateDataType",
Expand Down Expand Up @@ -98,4 +103,26 @@ def get(cls, value: str) -> str:

@staticmethod
def set(value: str) -> str:
return str(value)
return value


class _ArrayDataType(BaseDataType, data_type=array):
@classmethod
def get(cls, value: str) -> array:
return array(*ast.literal_eval(value))

@staticmethod
def set(value: array) -> str:
return str(value.tolist())


class ArrayIntDataType(_ArrayDataType, data_type=array[int]):
pass


class ArrayFloatDataType(_ArrayDataType, data_type=array[float]):
pass


class ArrayTextDataType(_ArrayDataType, data_type=array[str]):
pass
4 changes: 4 additions & 0 deletions src/pydbm/database/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pydbm.database.data_types import BaseDataType
from pydbm.inspect_extra import get_obj_annotations
from pydbm.models.fields import AutoField
from pydbm.typing_extra import array

if typing.TYPE_CHECKING:
from pydbm import DbmModel
Expand All @@ -36,6 +37,9 @@
int: "int",
None: "null",
str: "str",
array[int]: "array.int",
array[float]: "array.float",
array[str]: "array.str",
}
DATABASE_EXTENSION: str = "pydbm"
DATABASE_PATH: Path = Path("pydbm") # TODO: take from env
Expand Down
6 changes: 6 additions & 0 deletions src/pydbm/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from pydbm.models.base import DbmModel
from pydbm.models.fields import Field, Undefined
from pydbm.models.validators import (
validate_array_float,
validate_array_int,
validate_array_str,
validate_bool,
validate_bytes,
validate_date,
Expand All @@ -27,4 +30,7 @@
"validate_min_value",
"validate_none",
"validate_str",
"validate_array_float",
"validate_array_int",
"validate_array_str",
)
Loading

0 comments on commit 2829504

Please sign in to comment.