schapman1974 / tinymongo

A simple wrapper to make a flat file drop in raplacement for mongodb out of TinyDB
MIT License
201 stars 32 forks source link

Error when loading database file #58

Open lnoest opened 4 years ago

lnoest commented 4 years ago

When using the example code in Python 3.8 i get this error :

Traceback (most recent call last):
  File "C:/Users/loicn/backend_db.py", line 7, in <module>
    db = connection.my_tiny_database
  File "C:\Users\loicn\AppData\Local\Programs\Python\Python38\lib\site-packages\tinymongo\tinymongo.py", line 73, in __getattr__
    return TinyMongoDatabase(name, self._foldername, self._storage)
  File "C:\Users\loicn\AppData\Local\Programs\Python\Python38\lib\site-packages\tinymongo\tinymongo.py", line 73, in __getattr__
    return TinyMongoDatabase(name, self._foldername, self._storage)
  File "C:\Users\loicn\AppData\Local\Programs\Python\Python38\lib\site-packages\tinymongo\tinymongo.py", line 73, in __getattr__
    return TinyMongoDatabase(name, self._foldername, self._storage)
  [Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded

Process finished with exit code 1
jefmud commented 4 years ago

What version of Python are you running? I am seeing this on Python 3.8.2, but not on Python 3.6.9.

jefmud commented 4 years ago

I figured it out.

TinyDB which TinyMongo wraps around has slightly different behavior in Python 3.8. So there will need to be some changes to TinyMongo to compensate for this.

There is a class "property" from tinydb that is not working in the newest versions of Python. It requires a fix of the class TinyMongoClient() property _storage.

If you want a temporary work-around you can override the class

import tinymongo as tm
import tinydb

class TinyMongoClient(tm.TinyMongoClient):
    @property
    def _storage(self):
        return tinydb.storages.JSONStorage

If you put that code at the top of your module, you can now use TinyMongoClient in the default JSON storage mode.

davidlatwe commented 4 years ago

Maybe you could try the master branch which seems to be TinyDB v4 adopted in this fork: https://github.com/cjboyle/tinymongo

jefmud commented 4 years ago

That fork works, thanks @davidlatwe . I had hacked the module to work. But my version "locked" TinyMongo to the JSONstorage and would not have supported other middlewares. So your version is definitely welcome.

I did not run the suite of tinymongo tests, but the tests in my project associated with its usage of tinymongo passed on all versions of Python 3.5+

    def _storage(self):
        """comments removed"""
        return TinyDB.default_storage_class

Are you guys going to release the fork as a 0.2.1 version?

lnoest commented 4 years ago

What version of Python are you running? I am seeing this on Python 3.8.2, but not on Python 3.6.9.

I was using 3.8.2 but with the workaround it works

marcos-diniz commented 4 years ago

I have the same problem using python3.7.5

jefmud commented 4 years ago

Thanks for the information.

On Sun, May 31, 2020 at 12:37 AM Marcos Diniz notifications@github.com wrote:

I have the same problem using python3.7.5

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/schapman1974/tinymongo/issues/58#issuecomment-636419963, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6KUBCKL7IE6GK5H6PV7X3RUHNIXANCNFSM4M75KOOQ .

-- Jeff Muday Instructional Technology Analyst Wake Forest University 1834 Wake Forest Rd. Winston Hall Rm. 27 Winston Salem, NC 27106 mudayja @ wfu . edu p 336.758.6171 f 336.758.6004

mypolopony commented 4 years ago

I actually got this on 3.7.6 The class TinyMongoClient(tm.TinyMongoClient) solution didn't seem to work for me, but I can't 100% ensure that I didn't mess something else up :)

jefmud commented 4 years ago

I have used this extensively-- so I have a high level of confidence it works and will continue to work even if Schapman upgrades the code.

Make sure you are importing tinymongo and tinydb. In this case we are "overriding" the TinyMongoClient class property import to ensure we have the correct return. It might look a little awkward, but it achieves the same effect as Conner Boyle's fork (Thank you David Lai for pointing this out)

######### code starts here import tinymongo as tm import tinydb class TinyMongoClient(tm.TinyMongoClient): @property def _storage(self): return tinydb.storages.JSONStorage ############ code ends here

ALTERNATELY... You could just work with the forked version from Conner Boyle's github repository.

$ pip uninstall tinymongo $ pip install git+https://github.com/cjboyle/tinymongo

The only "gotcha" of doing it this way is that you cannot uninstall CJBoyle's version with a "pip uninstall", you have to remove it the old fashioned way of going into the environment packages and deleting the directory.

On Wed, Jun 17, 2020 at 2:59 PM Selwyn-Lloyd McPherson < notifications@github.com> wrote:

I actually got this on 3.7.6 The class TinyMongoClient(tm.TinyMongoClient) solution didn't seem to work for me, but I can't 100% ensure that I didn't mess something else up :)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/schapman1974/tinymongo/issues/58#issuecomment-645561061, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6KUBCOTUYWTKDVDZFO3MLRXEHBDANCNFSM4M75KOOQ .

-- Jeff Muday Instructional Technology Analyst Wake Forest University 1834 Wake Forest Rd. Winston Hall Rm. 27 Winston Salem, NC 27106 mudayja @ wfu . edu p 336.758.6171 f 336.758.6004

frankcarey commented 4 years ago

Still works for me FYI

JRileyH commented 3 years ago

How would one support custom serialization such as the DateTimeSerializer provided with tinymongo using this work around? The suggested method of supporting datetime objects in tinymongo already overrides this property:

import tinydb
from tinydb_serialization import SerializationMiddleware
import tinymongo as tm

class TinyMongoClient(tm.TinyMongoClient):
    @property
    def _storage(self):
        serialization = SerializationMiddleware()
        serialization.register_serializer(tm.serializers.DateTimeSerializer(), 'TinyDate')
        return serialization