Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python3 support #14

Merged
merged 5 commits into from
Feb 2, 2021
Merged

Python3 support #14

merged 5 commits into from
Feb 2, 2021

Conversation

uniphil
Copy link
Contributor

@uniphil uniphil commented Jan 30, 2021

Hello! I took a first stab at porting this library to python3 (#13). I've gotten it to a state where the tests pass without warnings (and still pass in python 2.7), but I haven't validated it on an existing codebase, since I was checking this out for a new project :)

Since py3 forces us to be more explicit about unicode/bytes differences, this
check is a bit more robust than exact equality
Return from generator instead of raising StopIteration

Check list_tables without checking order
Python3 gives resource warnings when refs are dropped for files that haven't
been closed. This commit adds explicit management via a `.close()` method on
classes that manage raw files, and calls `.close()` to eliminate all resource
warnings from tests.

Context managers are pretty nice for handling this, but I didn't want to change
the code too much. But! Python has thought of everything, and there's a
`closing` utility from `contextlib` that enables this pattern for objects with
methods called `close`:

```python
from contextlib import closing
from cog.database import Cog
from cog import config

with closing(Cog(config=config)) as db:
    db.create_namespace('suuup')
    db.create_or_load_table('suuup', 'my_friends')
    db.put(('a', '1'))
    assert db.get('a') == (b'0', ('a', '1'))

```
@arun1729
Copy link
Owner

arun1729 commented Feb 1, 2021

thank you!! Appreciate this very much! @uniphil

@arun1729 arun1729 merged commit 2ab5f41 into arun1729:master Feb 2, 2021
@uniphil uniphil deleted the python3 branch February 2, 2021 03:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants