fificore 0.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fificore-0.0.2/LICENSE +21 -0
- fificore-0.0.2/PKG-INFO +47 -0
- fificore-0.0.2/README.md +3 -0
- fificore-0.0.2/pyproject.toml +32 -0
- fificore-0.0.2/setup.cfg +4 -0
- fificore-0.0.2/src/fifi/__init__.py +13 -0
- fificore-0.0.2/src/fifi/database/sqlalchemy_engine_base.py +59 -0
- fificore-0.0.2/src/fifi/decorator/db_async_session.py +20 -0
- fificore-0.0.2/src/fifi/decorator/singleton.py +15 -0
- fificore-0.0.2/src/fifi/decorator/time_log.py +20 -0
- fificore-0.0.2/src/fifi/models/decorated_base.py +12 -0
- fificore-0.0.2/src/fificore.egg-info/PKG-INFO +47 -0
- fificore-0.0.2/src/fificore.egg-info/SOURCES.txt +15 -0
- fificore-0.0.2/src/fificore.egg-info/dependency_links.txt +1 -0
- fificore-0.0.2/src/fificore.egg-info/requires.txt +8 -0
- fificore-0.0.2/src/fificore.egg-info/top_level.txt +1 -0
- fificore-0.0.2/tests/test_sqlalchemy_engine_base.py +85 -0
fificore-0.0.2/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 FiFi Sayad
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
fificore-0.0.2/PKG-INFO
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fificore
|
|
3
|
+
Version: 0.0.2
|
|
4
|
+
Summary: FiFi Core is full of tools
|
|
5
|
+
Author-email: FiFi <sayad.mehrdad@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 FiFi Sayad
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/fifisayad/FiFiCore.git
|
|
29
|
+
Keywords: core,db,utility
|
|
30
|
+
Classifier: Programming Language :: Python
|
|
31
|
+
Classifier: Programming Language :: Python :: 3
|
|
32
|
+
Classifier: Operating System :: OS Independent
|
|
33
|
+
Requires-Python: >=3.12.0
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
License-File: LICENSE
|
|
36
|
+
Requires-Dist: SQLAlchemy
|
|
37
|
+
Provides-Extra: dev
|
|
38
|
+
Requires-Dist: black; extra == "dev"
|
|
39
|
+
Requires-Dist: bumpver; extra == "dev"
|
|
40
|
+
Requires-Dist: isort; extra == "dev"
|
|
41
|
+
Requires-Dist: pip-tools; extra == "dev"
|
|
42
|
+
Requires-Dist: pytest; extra == "dev"
|
|
43
|
+
Dynamic: license-file
|
|
44
|
+
|
|
45
|
+
# FiFiCore
|
|
46
|
+
Repo consists of repeatative functionality across different projects which can use
|
|
47
|
+
this core library.
|
fificore-0.0.2/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools >= 77.0.3"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "fificore"
|
|
7
|
+
version = "0.0.2"
|
|
8
|
+
description = "FiFi Core is full of tools"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
authors = [
|
|
11
|
+
{ name = "FiFi", email = "sayad.mehrdad@gmail.com" },
|
|
12
|
+
]
|
|
13
|
+
license = { file = "LICENSE" }
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
]
|
|
19
|
+
keywords = ["core", "db", "utility"]
|
|
20
|
+
dependencies = [
|
|
21
|
+
"SQLAlchemy",
|
|
22
|
+
]
|
|
23
|
+
requires-python = ">=3.12.0"
|
|
24
|
+
|
|
25
|
+
[project.optional-dependencies]
|
|
26
|
+
dev = ["black", "bumpver", "isort", "pip-tools", "pytest"]
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Homepage = "https://github.com/fifisayad/FiFiCore.git"
|
|
30
|
+
|
|
31
|
+
[project.scripts]
|
|
32
|
+
# realpython = "reader.__main__:main"
|
fificore-0.0.2/setup.cfg
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
__all__ = [
|
|
2
|
+
"SQLAlchemyEngineBase",
|
|
3
|
+
"db_async_session",
|
|
4
|
+
"singleton",
|
|
5
|
+
"timeit_log",
|
|
6
|
+
"DecoratedBase",
|
|
7
|
+
]
|
|
8
|
+
|
|
9
|
+
from .database.sqlalchemy_engine_base import SQLAlchemyEngineBase
|
|
10
|
+
from .decorator.db_async_session import db_async_session
|
|
11
|
+
from .decorator.singleton import singleton
|
|
12
|
+
from .decorator.time_log import timeit_log
|
|
13
|
+
from .models.decorated_base import DecoratedBase
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
from abc import ABC
|
|
3
|
+
|
|
4
|
+
from sqlalchemy.ext.asyncio import AsyncEngine
|
|
5
|
+
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
|
|
6
|
+
|
|
7
|
+
from ..models.decorated_base import DecoratedBase
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SQLAlchemyEngineBase(ABC):
|
|
11
|
+
"""
|
|
12
|
+
This is a abstract class for providing enginge to database we consider it.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
engine: AsyncEngine
|
|
16
|
+
session_maker: async_sessionmaker
|
|
17
|
+
|
|
18
|
+
def __init__(
|
|
19
|
+
self,
|
|
20
|
+
user: str,
|
|
21
|
+
password: str,
|
|
22
|
+
host: str,
|
|
23
|
+
port: int,
|
|
24
|
+
db_name: str,
|
|
25
|
+
db_tech: str = "sqllite",
|
|
26
|
+
db_lib: str = "aiosqlite",
|
|
27
|
+
):
|
|
28
|
+
"""__init__.
|
|
29
|
+
|
|
30
|
+
Args:
|
|
31
|
+
user (str): user
|
|
32
|
+
password (str): password
|
|
33
|
+
host (str): host
|
|
34
|
+
port (int): port
|
|
35
|
+
db_name (str): db_name
|
|
36
|
+
db_tech (str): db_tech
|
|
37
|
+
db_lib (str): db_lib
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
self.engine = create_async_engine(
|
|
41
|
+
url="{}+{}://{}:{}@{}:{}/{}".format(
|
|
42
|
+
db_tech,
|
|
43
|
+
db_lib,
|
|
44
|
+
user,
|
|
45
|
+
password,
|
|
46
|
+
host,
|
|
47
|
+
port,
|
|
48
|
+
db_name,
|
|
49
|
+
),
|
|
50
|
+
echo=False,
|
|
51
|
+
pool_pre_ping=True,
|
|
52
|
+
)
|
|
53
|
+
self.session_maker = async_sessionmaker(self.engine, expire_on_commit=False)
|
|
54
|
+
|
|
55
|
+
asyncio.run(self.init_models())
|
|
56
|
+
|
|
57
|
+
async def init_models(self):
|
|
58
|
+
async with self.engine.begin() as conn:
|
|
59
|
+
await conn.run_sync(DecoratedBase.metadata.create_all)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import functools
|
|
2
|
+
from sqlalchemy.orm import sessionmaker
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def db_async_session(session_maker: sessionmaker):
|
|
6
|
+
def decorator(func):
|
|
7
|
+
@functools.wraps(func)
|
|
8
|
+
async def wrapper(*args, **kwargs):
|
|
9
|
+
async with session_maker() as session:
|
|
10
|
+
try:
|
|
11
|
+
result = await func(*args, session=session, **kwargs)
|
|
12
|
+
await session.commit()
|
|
13
|
+
return result
|
|
14
|
+
except Exception:
|
|
15
|
+
await session.rollback()
|
|
16
|
+
raise
|
|
17
|
+
|
|
18
|
+
return wrapper
|
|
19
|
+
|
|
20
|
+
return decorator
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import functools
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def singleton(cls):
|
|
5
|
+
"""Turns class into singleton"""
|
|
6
|
+
|
|
7
|
+
@functools.wraps(cls)
|
|
8
|
+
def wrapper(*args, **kwargs):
|
|
9
|
+
if wrapper.instance: # type: ignore
|
|
10
|
+
return wrapper.instance # type: ignore
|
|
11
|
+
wrapper.instance = cls(*args, **kwargs) # type: ignore
|
|
12
|
+
return wrapper.instance # type: ignore
|
|
13
|
+
|
|
14
|
+
wrapper.instance = None # type: ignore
|
|
15
|
+
return wrapper
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import time
|
|
2
|
+
import logging
|
|
3
|
+
from functools import wraps
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# Setup logger
|
|
7
|
+
logger = logging.getLogger(__name__)
|
|
8
|
+
logging.basicConfig(level=logging.INFO)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def timeit_log(func):
|
|
12
|
+
@wraps(func)
|
|
13
|
+
def wrapper(*args, **kwargs):
|
|
14
|
+
start = time.perf_counter()
|
|
15
|
+
result = func(*args, **kwargs)
|
|
16
|
+
duration = time.perf_counter() - start
|
|
17
|
+
logger.info(f"[{func.__name__}] executed in {duration:.4f} seconds")
|
|
18
|
+
return result
|
|
19
|
+
|
|
20
|
+
return wrapper
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from typing import Set
|
|
2
|
+
from sqlalchemy.ext.asyncio import AsyncAttrs
|
|
3
|
+
from sqlalchemy.orm import DeclarativeBase
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class DecoratedBase(AsyncAttrs, DeclarativeBase):
|
|
7
|
+
def to_dict(self, exclude: Set = set()):
|
|
8
|
+
return {
|
|
9
|
+
column.name: getattr(self, column.name)
|
|
10
|
+
for column in self.__table__.columns
|
|
11
|
+
if column not in exclude
|
|
12
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fificore
|
|
3
|
+
Version: 0.0.2
|
|
4
|
+
Summary: FiFi Core is full of tools
|
|
5
|
+
Author-email: FiFi <sayad.mehrdad@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 FiFi Sayad
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/fifisayad/FiFiCore.git
|
|
29
|
+
Keywords: core,db,utility
|
|
30
|
+
Classifier: Programming Language :: Python
|
|
31
|
+
Classifier: Programming Language :: Python :: 3
|
|
32
|
+
Classifier: Operating System :: OS Independent
|
|
33
|
+
Requires-Python: >=3.12.0
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
License-File: LICENSE
|
|
36
|
+
Requires-Dist: SQLAlchemy
|
|
37
|
+
Provides-Extra: dev
|
|
38
|
+
Requires-Dist: black; extra == "dev"
|
|
39
|
+
Requires-Dist: bumpver; extra == "dev"
|
|
40
|
+
Requires-Dist: isort; extra == "dev"
|
|
41
|
+
Requires-Dist: pip-tools; extra == "dev"
|
|
42
|
+
Requires-Dist: pytest; extra == "dev"
|
|
43
|
+
Dynamic: license-file
|
|
44
|
+
|
|
45
|
+
# FiFiCore
|
|
46
|
+
Repo consists of repeatative functionality across different projects which can use
|
|
47
|
+
this core library.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/fifi/__init__.py
|
|
5
|
+
src/fifi/database/sqlalchemy_engine_base.py
|
|
6
|
+
src/fifi/decorator/db_async_session.py
|
|
7
|
+
src/fifi/decorator/singleton.py
|
|
8
|
+
src/fifi/decorator/time_log.py
|
|
9
|
+
src/fifi/models/decorated_base.py
|
|
10
|
+
src/fificore.egg-info/PKG-INFO
|
|
11
|
+
src/fificore.egg-info/SOURCES.txt
|
|
12
|
+
src/fificore.egg-info/dependency_links.txt
|
|
13
|
+
src/fificore.egg-info/requires.txt
|
|
14
|
+
src/fificore.egg-info/top_level.txt
|
|
15
|
+
tests/test_sqlalchemy_engine_base.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
fifi
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sqlite3
|
|
3
|
+
from typing import Optional
|
|
4
|
+
import pytest
|
|
5
|
+
import logging
|
|
6
|
+
from sqlalchemy import Column, Integer, Select, String
|
|
7
|
+
from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncEngine
|
|
8
|
+
from sqlalchemy import text
|
|
9
|
+
from sqlalchemy.ext.asyncio.session import AsyncSession
|
|
10
|
+
|
|
11
|
+
from src.fifi import SQLAlchemyEngineBase
|
|
12
|
+
from src.fifi import DecoratedBase
|
|
13
|
+
from src.fifi import db_async_session
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class DummyModel(DecoratedBase):
|
|
17
|
+
__tablename__ = "dummy"
|
|
18
|
+
id = Column(Integer, primary_key=True)
|
|
19
|
+
name = Column(String)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@pytest.fixture
|
|
23
|
+
def sql_alchemy_engine_base_test():
|
|
24
|
+
sqlite3.connect("memory")
|
|
25
|
+
engine_base = SQLAlchemyEngineBase(
|
|
26
|
+
user="",
|
|
27
|
+
password="",
|
|
28
|
+
host="",
|
|
29
|
+
port=0,
|
|
30
|
+
db_name="memory",
|
|
31
|
+
db_tech="sqlite",
|
|
32
|
+
db_lib="aiosqlite",
|
|
33
|
+
)
|
|
34
|
+
yield engine_base
|
|
35
|
+
os.remove("./memory")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@pytest.mark.asyncio
|
|
39
|
+
class TestSQLAlchemyEngineBase:
|
|
40
|
+
|
|
41
|
+
async def test_engine_base_initializes_correctly(
|
|
42
|
+
self, sql_alchemy_engine_base_test
|
|
43
|
+
):
|
|
44
|
+
|
|
45
|
+
assert isinstance(sql_alchemy_engine_base_test.engine, AsyncEngine)
|
|
46
|
+
assert isinstance(
|
|
47
|
+
sql_alchemy_engine_base_test.session_maker, async_sessionmaker
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
async with sql_alchemy_engine_base_test.engine.begin() as conn:
|
|
51
|
+
|
|
52
|
+
result = await conn.execute(
|
|
53
|
+
text(
|
|
54
|
+
"SELECT name FROM sqlite_master WHERE type='table' AND name='dummy';"
|
|
55
|
+
)
|
|
56
|
+
)
|
|
57
|
+
table = result.scalar()
|
|
58
|
+
logging.info(f"tables = {table}")
|
|
59
|
+
assert table == "dummy"
|
|
60
|
+
|
|
61
|
+
async def test_dict_decorated_base(self, sql_alchemy_engine_base_test):
|
|
62
|
+
|
|
63
|
+
@db_async_session(sql_alchemy_engine_base_test.session_maker)
|
|
64
|
+
async def data_seeder(session: Optional[AsyncSession] = None):
|
|
65
|
+
if not session:
|
|
66
|
+
raise Exception()
|
|
67
|
+
fifi = DummyModel(name="FiFi")
|
|
68
|
+
mehrdad = DummyModel(name="Mehrdad")
|
|
69
|
+
session.add(fifi)
|
|
70
|
+
session.add(mehrdad)
|
|
71
|
+
|
|
72
|
+
@db_async_session(sql_alchemy_engine_base_test.session_maker)
|
|
73
|
+
async def data_reader(session: Optional[AsyncSession] = None) -> DummyModel:
|
|
74
|
+
if not session:
|
|
75
|
+
raise Exception()
|
|
76
|
+
stmt = Select(DummyModel).where(DummyModel.name == "FiFi")
|
|
77
|
+
result = await session.execute(stmt)
|
|
78
|
+
return result.scalar_one()
|
|
79
|
+
|
|
80
|
+
await data_seeder()
|
|
81
|
+
fifi = await data_reader()
|
|
82
|
+
logging.info(f"fifi object: {fifi.to_dict()}")
|
|
83
|
+
assert isinstance(fifi.to_dict(), dict)
|
|
84
|
+
assert isinstance(fifi, DummyModel)
|
|
85
|
+
assert str(fifi.name) == "FiFi"
|