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

Closed
msiemens opened this Issue Aug 12, 2017 · 5 comments

Comments

2 participants
@msiemens
Owner

msiemens commented Aug 12, 2017

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.

@Phuket2

This comment has been minimized.

Show comment
Hide comment
@Phuket2

Phuket2 Aug 13, 2017

@msiemens, wouldn't you just add a readonly attr to store/read the table name?

Phuket2 commented Aug 13, 2017

@msiemens, wouldn't you just add a readonly attr to store/read the table name?

@Phuket2

This comment has been minimized.

Show comment
Hide comment
@Phuket2

Phuket2 Aug 13, 2017

@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...
In the def table method, I updated the options kwargs with the name of the table, which is present. Then in the Table class init, i added a new kwarg param as well as a attr and a property to store and access the attr. In my simple cases, it appears to work.

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...
	
	```
	
		

Phuket2 commented Aug 13, 2017

@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...
In the def table method, I updated the options kwargs with the name of the table, which is present. Then in the Table class init, i added a new kwarg param as well as a attr and a property to store and access the attr. In my simple cases, it appears to work.

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...
	
	```
	
		

@msiemens msiemens closed this in 58e2ab7 Aug 30, 2017

@msiemens

This comment has been minimized.

Show comment
Hide comment
@msiemens

msiemens Aug 30, 2017

Owner

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 :)

Owner

msiemens commented Aug 30, 2017

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 :)

@msiemens

This comment has been minimized.

Show comment
Hide comment
@msiemens

msiemens Aug 30, 2017

Owner

Now released in v3.5.0 🎉

Owner

msiemens commented Aug 30, 2017

Now released in v3.5.0 🎉

@Phuket2

This comment has been minimized.

Show comment
Hide comment
@Phuket2

Phuket2 Sep 3, 2017

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.
Thanks Again

Phuket2 commented Sep 3, 2017

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.
Thanks Again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment