generated from allenai/python-package-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add pep 249 support * add async pep 249 support * add pep 249 async support * add __init__.py files * update min python to 3.9 in actions * fix imports * remove more | from type hints * run isort * fix linting * update doc strings * add has_results property * format class variables * fix tests for nextset
- Loading branch information
Showing
33 changed files
with
1,627 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,4 @@ dependencies: | |
- websocket-client>=1.2.1 | ||
- pyee | ||
- websockets | ||
- pep249abc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Any, Dict, Union | ||
|
||
from .core import ( | ||
Connection, | ||
Cursor, | ||
DatabaseError, | ||
DataError, | ||
Error, | ||
IntegrityError, | ||
InterfaceError, | ||
InternalError, | ||
NotSupportedError, | ||
OperationalError, | ||
ProgrammingError, | ||
) | ||
from .data_types import DaemonServer | ||
|
||
__all__ = [ | ||
"apilevel", | ||
"threadsafety", | ||
"paramstyle", | ||
"DatabaseError", | ||
"DataError", | ||
"Error", | ||
"InterfaceError", | ||
"IntegrityError", | ||
"InternalError", | ||
"NotSupportedError", | ||
"OperationalError", | ||
"ProgrammingError", | ||
"CONNECTION_CLOSED", | ||
"convert_runtime_errors", | ||
"connect", | ||
"Connection", | ||
"Cursor", | ||
] | ||
|
||
# pylint: disable=invalid-name | ||
apilevel = "2.0" | ||
threadsafety = 1 | ||
paramstyle = "qmark" | ||
|
||
|
||
def connect(connection_details: Union[DaemonServer, dict], opts: Dict[str, Any] = {}) -> Connection: | ||
"""Connect to a Mapepire Server, returning a connection.""" | ||
return Connection(connection_details, opts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Any, Dict, Union | ||
|
||
from ..asyncio.connection import AsyncConnection | ||
from ..core.exceptions import ( | ||
DatabaseError, | ||
DataError, | ||
Error, | ||
IntegrityError, | ||
InterfaceError, | ||
InternalError, | ||
NotSupportedError, | ||
OperationalError, | ||
ProgrammingError, | ||
) | ||
from ..data_types import DaemonServer | ||
from .cursor import AsyncCursor | ||
|
||
__all__ = [ | ||
"apilevel", | ||
"threadsafety", | ||
"paramstyle", | ||
"connect", | ||
"AsyncConnection", | ||
"AsyncCursor", | ||
"Error", | ||
"InterfaceError", | ||
"DatabaseError", | ||
"DataError", | ||
"IntegrityError", | ||
"InternalError", | ||
"NotSupportedError", | ||
"OperationalError", | ||
"ProgrammingError", | ||
] | ||
|
||
# pylint: disable=invalid-name | ||
apilevel = "2.0" | ||
threadsafety = 1 | ||
paramstyle = "qmark" | ||
|
||
|
||
def connect( | ||
connection_details: Union[DaemonServer, dict], opts: Dict[str, Any] = {} | ||
) -> AsyncConnection: | ||
"""Connect to a Mapepire Server, returning a connection.""" | ||
return AsyncConnection(connection_details, opts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from typing import Optional, Sequence, Union | ||
|
||
from pep249 import aiopep249 | ||
from pep249.aiopep249 import ProcArgs, ProcName, QueryParameters, SQLQuery | ||
|
||
from ..core.connection import Connection | ||
from ..data_types import DaemonServer | ||
from .cursor import AsyncCursor | ||
from .utils import to_thread | ||
|
||
|
||
class AsyncConnection(aiopep249.AsyncCursorExecuteMixin, aiopep249.AsyncConnection): | ||
""" | ||
A DB API 2.0 compliant async connection for Mapepire, as outlined in | ||
PEP 249. | ||
Can be constructed by passing a connection details object as a dict, | ||
or a `DaemonServer` object: | ||
``` | ||
import asyncio | ||
from mapepire_python.asyncio import connect | ||
from mapepire_python.data_types import DaemonServer | ||
creds = DaemonServer( | ||
host=SERVER, | ||
port=PORT, | ||
user=USER, | ||
password=PASS, | ||
ignoreUnauthorized=True | ||
) | ||
>>> async def main(): | ||
... async with connect(creds) as conn: | ||
... async with await conn.execute("select * from sample.employee") as cur: | ||
... print(await cur.fetchone()) | ||
>>> if __name__ == '__main__': | ||
... asyncio.run(main()) | ||
``` | ||
""" | ||
|
||
def __init__(self, database: Union[DaemonServer, dict], opts={}) -> None: | ||
super().__init__() | ||
self._connection = Connection(database, opts=opts) | ||
|
||
async def cursor(self) -> AsyncCursor: | ||
return AsyncCursor(self, self._connection.cursor()) | ||
|
||
async def close(self) -> None: | ||
await to_thread(self._connection.close) | ||
|
||
async def execute( | ||
self, operation: SQLQuery, parameters: Optional[QueryParameters] = None | ||
) -> AsyncCursor: | ||
cursor = await self.cursor() | ||
return await cursor.execute(operation, parameters) | ||
|
||
async def executemany( | ||
self, operation: SQLQuery, seq_of_parameters: Sequence[QueryParameters] | ||
) -> AsyncCursor: | ||
cursor = await self.cursor() | ||
return await cursor.executemany(operation, seq_of_parameters) | ||
|
||
async def callproc( | ||
self, procname: ProcName, parameters: Optional[ProcArgs] = None | ||
) -> Optional[ProcArgs]: | ||
cursor = await self.cursor() | ||
return await cursor.callproc(procname, parameters) | ||
|
||
async def executescript(self, script: SQLQuery) -> AsyncCursor: | ||
"""A lazy implementation of SQLite's `executescript`.""" | ||
return await self.execute(script) | ||
|
||
async def commit(self) -> None: | ||
to_thread(self._connection.commit) | ||
|
||
async def rollback(self) -> None: | ||
to_thread(self._connection.rollback) |
Oops, something went wrong.