python-databases 0.1.1__tar.gz → 0.1.3__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.
Files changed (22) hide show
  1. python_databases-0.1.3/PKG-INFO +75 -0
  2. python_databases-0.1.3/README.md +45 -0
  3. python_databases-0.1.3/python_databases/postgresql_infrastructure/__init__.py +7 -0
  4. python_databases-0.1.3/python_databases/postgresql_infrastructure/postgresql.py +83 -0
  5. python_databases-0.1.3/python_databases.egg-info/PKG-INFO +75 -0
  6. {python_databases-0.1.1 → python_databases-0.1.3}/python_databases.egg-info/SOURCES.txt +4 -1
  7. {python_databases-0.1.1 → python_databases-0.1.3}/setup.py +1 -1
  8. python_databases-0.1.3/tests/test_postgres.py +46 -0
  9. python_databases-0.1.1/PKG-INFO +0 -31
  10. python_databases-0.1.1/README.md +0 -1
  11. python_databases-0.1.1/python_databases.egg-info/PKG-INFO +0 -31
  12. {python_databases-0.1.1 → python_databases-0.1.3}/LICENSE +0 -0
  13. {python_databases-0.1.1 → python_databases-0.1.3}/MANIFEST.in +0 -0
  14. {python_databases-0.1.1 → python_databases-0.1.3}/pyproject.toml +0 -0
  15. {python_databases-0.1.1 → python_databases-0.1.3}/python_databases/__init__.py +0 -0
  16. {python_databases-0.1.1 → python_databases-0.1.3}/python_databases/elasticsearch_infrastructure/__init__.py +0 -0
  17. {python_databases-0.1.1 → python_databases-0.1.3}/python_databases/elasticsearch_infrastructure/elasticsearch.py +0 -0
  18. {python_databases-0.1.1 → python_databases-0.1.3}/python_databases.egg-info/dependency_links.txt +0 -0
  19. {python_databases-0.1.1 → python_databases-0.1.3}/python_databases.egg-info/requires.txt +0 -0
  20. {python_databases-0.1.1 → python_databases-0.1.3}/python_databases.egg-info/top_level.txt +0 -0
  21. {python_databases-0.1.1 → python_databases-0.1.3}/requirements.txt +0 -0
  22. {python_databases-0.1.1 → python_databases-0.1.3}/setup.cfg +0 -0
@@ -0,0 +1,75 @@
1
+ Metadata-Version: 2.4
2
+ Name: python-databases
3
+ Version: 0.1.3
4
+ Summary: A Python library for managing and interacting with multiple databases seamlessly.
5
+ Home-page: https://github.com/aviz92/python-databases
6
+ Author: Avi Zaguri
7
+ Author-email:
8
+ Project-URL: Repository, https://github.com/aviz92/python-databases
9
+ Requires-Python: >=3.11
10
+ Description-Content-Type: text/markdown
11
+ License-File: LICENSE
12
+ Requires-Dist: setuptools
13
+ Requires-Dist: wheel
14
+ Requires-Dist: dotenv
15
+ Requires-Dist: custom-python-logger
16
+ Requires-Dist: retrying
17
+ Requires-Dist: elasticsearch<9
18
+ Requires-Dist: pandas
19
+ Requires-Dist: psycopg2-binary
20
+ Requires-Dist: pytest
21
+ Dynamic: author
22
+ Dynamic: description
23
+ Dynamic: description-content-type
24
+ Dynamic: home-page
25
+ Dynamic: license-file
26
+ Dynamic: project-url
27
+ Dynamic: requires-dist
28
+ Dynamic: requires-python
29
+ Dynamic: summary
30
+
31
+ # Python Databases
32
+ A flexible and extensible Python library that provides a unified interface for interacting with multiple types of databases.
33
+
34
+ ---
35
+
36
+ ## Features
37
+ - ✅ Unified interface for multiple database connectors
38
+ - ✅ Support for:
39
+ - Elasticsearch
40
+ - On-Premises deployments
41
+ - Cloud deployments using `cloud_id`
42
+ - PostgreSQL
43
+ - Basic support with plans for SQLAlchemy integration
44
+
45
+ ---
46
+
47
+ ## Installation
48
+ ```bash
49
+ pip install python-databases
50
+ ```
51
+
52
+ ---
53
+
54
+ ## 📌 Roadmap
55
+
56
+ * [x] Elasticsearch support
57
+ - [x] On-Premises deployments
58
+ - [x] Cloud deployments using `cloud_id`
59
+ * [x] PostgreSQL
60
+ - [ ] SQLAlchemy support
61
+
62
+ ---
63
+
64
+ ## 🤝 Contributing
65
+ If you have a helpful tool, pattern, or improvement to suggest:
66
+ Fork the repo <br>
67
+ Create a new branch <br>
68
+ Submit a pull request <br>
69
+ I welcome additions that promote clean, productive, and maintainable development. <br>
70
+
71
+ ---
72
+
73
+ ## 🙏 Thanks
74
+ Thanks for exploring this repository! <br>
75
+ Happy coding! <br>
@@ -0,0 +1,45 @@
1
+ # Python Databases
2
+ A flexible and extensible Python library that provides a unified interface for interacting with multiple types of databases.
3
+
4
+ ---
5
+
6
+ ## Features
7
+ - ✅ Unified interface for multiple database connectors
8
+ - ✅ Support for:
9
+ - Elasticsearch
10
+ - On-Premises deployments
11
+ - Cloud deployments using `cloud_id`
12
+ - PostgreSQL
13
+ - Basic support with plans for SQLAlchemy integration
14
+
15
+ ---
16
+
17
+ ## Installation
18
+ ```bash
19
+ pip install python-databases
20
+ ```
21
+
22
+ ---
23
+
24
+ ## 📌 Roadmap
25
+
26
+ * [x] Elasticsearch support
27
+ - [x] On-Premises deployments
28
+ - [x] Cloud deployments using `cloud_id`
29
+ * [x] PostgreSQL
30
+ - [ ] SQLAlchemy support
31
+
32
+ ---
33
+
34
+ ## 🤝 Contributing
35
+ If you have a helpful tool, pattern, or improvement to suggest:
36
+ Fork the repo <br>
37
+ Create a new branch <br>
38
+ Submit a pull request <br>
39
+ I welcome additions that promote clean, productive, and maintainable development. <br>
40
+
41
+ ---
42
+
43
+ ## 🙏 Thanks
44
+ Thanks for exploring this repository! <br>
45
+ Happy coding! <br>
@@ -0,0 +1,7 @@
1
+ from dotenv import load_dotenv
2
+
3
+ from python_databases.postgresql_infrastructure.postgresql import PostgreSQL
4
+
5
+ load_dotenv()
6
+
7
+ __all__ = ['PostgreSQL']
@@ -0,0 +1,83 @@
1
+ import logging
2
+ import os
3
+ from typing import Optional, Union, Any
4
+ import psycopg2
5
+ from psycopg2 import extras
6
+ from retrying import retry
7
+
8
+
9
+ class PostgreSQL:
10
+ def __init__(
11
+ self,
12
+ name: Optional[str] = None,
13
+ host: Optional[str] = None,
14
+ port: Optional[str] = None,
15
+ username: Optional[str] = None,
16
+ password: Optional[str] = None
17
+ ):
18
+ self.logger = logging.getLogger(self.__class__.__name__)
19
+
20
+ self.name = name or os.getenv("POSTGRESQL_NAME")
21
+ self.host = host or os.getenv("POSTGRESQL_HOST")
22
+ self.port = port or os.getenv("POSTGRESQL_PORT")
23
+ self.username = username or os.getenv("POSTGRESQL_USERNAME")
24
+ self.password = password or os.getenv("POSTGRESQL_PASSWORD")
25
+
26
+ self._client = None
27
+ self._cursor = None
28
+
29
+ self.create_connection()
30
+
31
+ @property
32
+ def client(self):
33
+ if not self._client or self._client.closed:
34
+ self.logger.warning("Re-establishing PostgreSQL connection")
35
+ self.create_connection()
36
+ return self._client
37
+
38
+ @property
39
+ def cursor(self):
40
+ if not self._cursor or self._cursor.closed:
41
+ self.logger.warning("Re-creating PostgreSQL cursor")
42
+ self.create_connection()
43
+ return self._cursor
44
+
45
+ # @retry(stop_max_attempt_number=3, wait_fixed=180000)
46
+ def create_connection(self):
47
+ self.logger.info(f"Create connection to {self.host}:{self.port}")
48
+
49
+ self._client = psycopg2.connect(
50
+ database=self.name,
51
+ host=self.host,
52
+ port=self.port,
53
+ user=self.username,
54
+ password=self.password,
55
+ )
56
+ self._cursor = self._client.cursor(cursor_factory=extras.DictCursor)
57
+
58
+ def execute_query(self, query: str, params: Optional[Union[tuple, dict[str, Any]]] = None) -> None:
59
+ self.logger.debug(f"Fetch rows from DB by query {query} with params {params}")
60
+ try:
61
+ self.cursor.execute(query, params)
62
+ except psycopg2.ProgrammingError as e:
63
+ raise psycopg2.ProgrammingError(f"Error executing query: {query}") from e
64
+ self.logger.debug(f"Finished to fetch rows by query")
65
+
66
+ def fetch_all(self, query: str, params: Optional[Union[tuple, dict[str, Any]]] = None) -> list[dict[str, Any]]:
67
+ self.execute_query(query, params)
68
+ try:
69
+ results = self.cursor.fetchall()
70
+ except psycopg2.ProgrammingError as e:
71
+ raise psycopg2.ProgrammingError(f"Error fetching rows: {query}") from e
72
+ self.logger.debug(f"Fetched {len(results)} rows")
73
+ return results
74
+
75
+ def close(self):
76
+ self.cursor.close()
77
+ self.client.close()
78
+
79
+ def __enter__(self):
80
+ return self
81
+
82
+ def __exit__(self, exc_type, exc_val, exc_tb):
83
+ self.close()
@@ -0,0 +1,75 @@
1
+ Metadata-Version: 2.4
2
+ Name: python-databases
3
+ Version: 0.1.3
4
+ Summary: A Python library for managing and interacting with multiple databases seamlessly.
5
+ Home-page: https://github.com/aviz92/python-databases
6
+ Author: Avi Zaguri
7
+ Author-email:
8
+ Project-URL: Repository, https://github.com/aviz92/python-databases
9
+ Requires-Python: >=3.11
10
+ Description-Content-Type: text/markdown
11
+ License-File: LICENSE
12
+ Requires-Dist: setuptools
13
+ Requires-Dist: wheel
14
+ Requires-Dist: dotenv
15
+ Requires-Dist: custom-python-logger
16
+ Requires-Dist: retrying
17
+ Requires-Dist: elasticsearch<9
18
+ Requires-Dist: pandas
19
+ Requires-Dist: psycopg2-binary
20
+ Requires-Dist: pytest
21
+ Dynamic: author
22
+ Dynamic: description
23
+ Dynamic: description-content-type
24
+ Dynamic: home-page
25
+ Dynamic: license-file
26
+ Dynamic: project-url
27
+ Dynamic: requires-dist
28
+ Dynamic: requires-python
29
+ Dynamic: summary
30
+
31
+ # Python Databases
32
+ A flexible and extensible Python library that provides a unified interface for interacting with multiple types of databases.
33
+
34
+ ---
35
+
36
+ ## Features
37
+ - ✅ Unified interface for multiple database connectors
38
+ - ✅ Support for:
39
+ - Elasticsearch
40
+ - On-Premises deployments
41
+ - Cloud deployments using `cloud_id`
42
+ - PostgreSQL
43
+ - Basic support with plans for SQLAlchemy integration
44
+
45
+ ---
46
+
47
+ ## Installation
48
+ ```bash
49
+ pip install python-databases
50
+ ```
51
+
52
+ ---
53
+
54
+ ## 📌 Roadmap
55
+
56
+ * [x] Elasticsearch support
57
+ - [x] On-Premises deployments
58
+ - [x] Cloud deployments using `cloud_id`
59
+ * [x] PostgreSQL
60
+ - [ ] SQLAlchemy support
61
+
62
+ ---
63
+
64
+ ## 🤝 Contributing
65
+ If you have a helpful tool, pattern, or improvement to suggest:
66
+ Fork the repo <br>
67
+ Create a new branch <br>
68
+ Submit a pull request <br>
69
+ I welcome additions that promote clean, productive, and maintainable development. <br>
70
+
71
+ ---
72
+
73
+ ## 🙏 Thanks
74
+ Thanks for exploring this repository! <br>
75
+ Happy coding! <br>
@@ -11,4 +11,7 @@ python_databases.egg-info/dependency_links.txt
11
11
  python_databases.egg-info/requires.txt
12
12
  python_databases.egg-info/top_level.txt
13
13
  python_databases/elasticsearch_infrastructure/__init__.py
14
- python_databases/elasticsearch_infrastructure/elasticsearch.py
14
+ python_databases/elasticsearch_infrastructure/elasticsearch.py
15
+ python_databases/postgresql_infrastructure/__init__.py
16
+ python_databases/postgresql_infrastructure/postgresql.py
17
+ tests/test_postgres.py
@@ -1,6 +1,6 @@
1
1
  from setuptools import setup, find_packages
2
2
 
3
- package_version = '0.1.1'
3
+ package_version = '0.1.3'
4
4
 
5
5
  package_name = 'python-databases'
6
6
  package_description = 'A Python library for managing and interacting with multiple databases seamlessly.'
@@ -0,0 +1,46 @@
1
+ # test_postgresql.py
2
+
3
+ import pytest
4
+ from unittest.mock import patch, MagicMock
5
+ from python_databases.postgresql_infrastructure import PostgreSQL
6
+
7
+
8
+ @pytest.fixture
9
+ def mock_connection():
10
+ with patch("psycopg2.connect") as mock_connect:
11
+ mock_conn = MagicMock()
12
+ mock_cursor = MagicMock()
13
+ mock_conn.cursor.return_value = mock_cursor
14
+ mock_connect.return_value = mock_conn
15
+ yield mock_conn, mock_cursor
16
+
17
+
18
+ def test_connection_created(mock_connection):
19
+ conn, _ = mock_connection
20
+ db = PostgreSQL(name="test_db", host="localhost", port="5432", username="user", password="pass")
21
+ assert db.client == conn
22
+ assert conn.cursor.call_count == 2
23
+
24
+
25
+ def test_execute_query_success(mock_connection):
26
+ _, cursor = mock_connection
27
+ db = PostgreSQL(name="test_db", host="localhost", port="5432", username="user", password="pass")
28
+ db.execute_query("SELECT 1", None)
29
+ cursor.execute.assert_called_with("SELECT 1", None)
30
+
31
+
32
+ def test_fetch_all_success(mock_connection):
33
+ _, cursor = mock_connection
34
+ cursor.fetchall.return_value = [{"id": 1}]
35
+ db = PostgreSQL(name="test_db", host="localhost", port="5432", username="user", password="pass")
36
+ results = db.fetch_all("SELECT id FROM test")
37
+ cursor.execute.assert_called_with("SELECT id FROM test", None)
38
+ assert results == [{"id": 1}]
39
+
40
+
41
+ def test_context_manager_closes_connection(mock_connection):
42
+ conn, cursor = mock_connection
43
+ with PostgreSQL(name="test_db", host="localhost", port="5432", username="user", password="pass") as db:
44
+ pass
45
+ cursor.close.assert_called_once()
46
+ conn.close.assert_called_once()
@@ -1,31 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: python-databases
3
- Version: 0.1.1
4
- Summary: A Python library for managing and interacting with multiple databases seamlessly.
5
- Home-page: https://github.com/aviz92/python-databases
6
- Author: Avi Zaguri
7
- Author-email:
8
- Project-URL: Repository, https://github.com/aviz92/python-databases
9
- Requires-Python: >=3.11
10
- Description-Content-Type: text/markdown
11
- License-File: LICENSE
12
- Requires-Dist: setuptools
13
- Requires-Dist: wheel
14
- Requires-Dist: dotenv
15
- Requires-Dist: custom-python-logger
16
- Requires-Dist: retrying
17
- Requires-Dist: elasticsearch<9
18
- Requires-Dist: pandas
19
- Requires-Dist: psycopg2-binary
20
- Requires-Dist: pytest
21
- Dynamic: author
22
- Dynamic: description
23
- Dynamic: description-content-type
24
- Dynamic: home-page
25
- Dynamic: license-file
26
- Dynamic: project-url
27
- Dynamic: requires-dist
28
- Dynamic: requires-python
29
- Dynamic: summary
30
-
31
- # Python Databases
@@ -1 +0,0 @@
1
- # Python Databases
@@ -1,31 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: python-databases
3
- Version: 0.1.1
4
- Summary: A Python library for managing and interacting with multiple databases seamlessly.
5
- Home-page: https://github.com/aviz92/python-databases
6
- Author: Avi Zaguri
7
- Author-email:
8
- Project-URL: Repository, https://github.com/aviz92/python-databases
9
- Requires-Python: >=3.11
10
- Description-Content-Type: text/markdown
11
- License-File: LICENSE
12
- Requires-Dist: setuptools
13
- Requires-Dist: wheel
14
- Requires-Dist: dotenv
15
- Requires-Dist: custom-python-logger
16
- Requires-Dist: retrying
17
- Requires-Dist: elasticsearch<9
18
- Requires-Dist: pandas
19
- Requires-Dist: psycopg2-binary
20
- Requires-Dist: pytest
21
- Dynamic: author
22
- Dynamic: description
23
- Dynamic: description-content-type
24
- Dynamic: home-page
25
- Dynamic: license-file
26
- Dynamic: project-url
27
- Dynamic: requires-dist
28
- Dynamic: requires-python
29
- Dynamic: summary
30
-
31
- # Python Databases