I'm trying to use matches() to determine if the database contains any one of a set of int values but am getting an exception thrown.
The following is some sample code that illustrates the issue (the exception information follows the code).
from tinydb import TinyDB, Query
db = TinyDB('testdb.json')
db.purge()
db.insert({'type': 'apple', 'ID': 1, 'ID2': '1', 'name': 'a'})
db.insert({'type': 'apple', 'ID': 2, 'ID2': '2', 'name': 'b'})
db.insert({'type': 'apple', 'ID': 3, 'ID2': '3', 'name': 'c'})
db.insert({'type': 'apple', 'ID': 4, 'ID2': '4', 'name': 'd'})
_attr = Query()
db.search(_attr['type'] == 'apple')
db.search((_attr['type'] == 'apple') & (_attr['name'].matches('[ab]')))
db.search((_attr['type'] == 'apple') & (_attr['ID2'].matches('[12]')))
db.search((_attr['type'] == 'apple') & (_attr['ID2'].matches('(1|2)')))
db.search((_attr['type'] == 'apple') & (_attr['ID'].matches('[12]')))
db.search((_attr['type'] == 'apple') & (_attr['ID'].matches('(1|2)')))
db.search((_attr['type'] == 'apple') & (str(_attr['ID']).matches('(1|2)')))
The exception is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\nicholasr\Documents\FME\Plugins\Python\tinydb\database.py", line 478, in search
docs = [doc for doc in self.all() if cond(doc)]
File "C:\Users\nicholasr\Documents\FME\Plugins\Python\tinydb\database.py", line 478, in <listcomp>
docs = [doc for doc in self.all() if cond(doc)]
File "C:\Users\nicholasr\Documents\FME\Plugins\Python\tinydb\queries.py", line 45, in __call__
return self.test(value)
File "C:\Users\nicholasr\Documents\FME\Plugins\Python\tinydb\queries.py", line 61, in <lambda>
return QueryImpl(lambda value: self(value) and other(value),
File "C:\Users\nicholasr\Documents\FME\Plugins\Python\tinydb\queries.py", line 45, in __call__
return self.test(value)
File "C:\Users\nicholasr\Documents\FME\Plugins\Python\tinydb\queries.py", line 136, in impl
return test(value)
File "C:\Users\nicholasr\Documents\FME\Plugins\Python\tinydb\queries.py", line 243, in <lambda>
return self._generate_test(lambda value: re.match(regex, value),
File "C:\Users\nicholasr\AppData\Local\Programs\Python\Python36-32\lib\re.py", line 172, in match
return _compile(pattern, flags).match(string)
TypeError: expected string or bytes-like object
thanks,
Nic