-
Notifications
You must be signed in to change notification settings - Fork 535
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
Add way to get a table's name #147
Comments
@msiemens, wouldn't you just add a readonly attr to store/read the table name? |
@msiemens, I made 3 changes in the database.py to store and access the table name. I didn't want to do a pull request etc... I am not good enough for that, i am still a beginner. Also, its probably not so Pythonic what i have done. Anyway, i just wanted to give it a go. But it does seem like this would not change the interface. Not sure about custom tables etc... def table(self, name=DEFAULT_TABLE, **options):
"""
Get access to a specific table.
Creates a new table, if it hasn't been created before, otherwise it
returns the cached :class:`~tinydb.Table` object.
:param name: The name of the table.
:type name: str
:param cache_size: How many query results to cache.
"""
if name in self._table_cache:
return self._table_cache[name]
options.update({'table_name':name}) # <--- added this
table = self.table_class(StorageProxy(self._storage, name), **options)
self._table_cache[name] = table
# table._read will create an empty table in the storage, if necessary
table._read()
return table
class Table(object):
"""
Represents a single TinyDB Table.
"""
def __init__(self, storage, cache_size=10, table_name=''): # added new kwarg
"""
Get access to a table.
:param storage: Access to the storage
:type storage: StorageProxyus
:param cache_size: Maximum size of query cache.
"""
self.__table_name = table_name # <-- private attr added
self._storage = storage
self._query_cache = LRUCache(capacity=cache_size)
data = self._read()
if data:
self._last_id = max(i for i in data)
else:
self._last_id = 0
# property added
@property
def name(self):
return self.__table_name
# class continues...
```
|
Hey, sorry for the long delay, I've been pretty busy lately. All in all, your changes look pretty good, I've implemented them in 58e2ab7, I'll release a version of TinyDB that includes this feature later today :) |
Now released in v3.5.0 🎉 |
Perfect, thanks for the update. I am just having some fun at the moment trying to use TDB as an old style macintosh resource manager. I started out doing this as in the old old days, I used the mac resource manager to hold string translations for the UI languages. As I am using Python on iOS i dont have direct access to plists etc... I am sure you are too young to remember the resource manager, but in my option it was fantastic. TDB seems like a perfect foundation to build a puesdo resource manager around. |
See https://forum.m-siemens.de/d/23-table-name-from-the-table-object
My main concern is how to add this in a backwards-compatible way, as I'm not totally sure what guarantees we specify in the docs about what arguments the table class should take.
The text was updated successfully, but these errors were encountered: