text
stringlengths
0
828
# Make sure we have indexes
coll = self.get_collection(coll_name)
for idx_name in cls.index_names():
coll.ensure_index(idx_name)"
1651,"def find_one(self, cls, id):
""""""Required functionality.""""""
one = self._find(cls, {""_id"": id})
if not one:
return None
return one[0]"
1652,"def find_by_index(self, cls, index_name, value):
""""""Required functionality.""""""
return self._find(cls, {index_name: str(value)})"
1653,"def save(self, obj):
""""""Required functionality.""""""
if not obj.id:
obj.id = uuid()
stored_data = {
'_id': obj.id,
'value': json.loads(obj.to_data())
}
index_vals = obj.indexes() or {}
for key in obj.__class__.index_names() or []:
val = index_vals.get(key, '')
stored_data[key] = str(val)
coll = self.get_collection(obj.__class__.get_table_name())
coll.update({""_id"": obj.id}, stored_data, upsert=True)"
1654,"def delete(self, obj):
""""""Required functionality.""""""
del_id = obj.get_id()
if not del_id:
return
coll = self.get_collection(obj.__class__.get_table_name())
coll.delete_one({""_id"": del_id})"
1655,"def new(cls, __name, __fields, **defaults):
'''
Creates a new class that can represent a record with the
specified *fields*. This is equal to a mutable namedtuple.
The returned class also supports keyword arguments in its
constructor.
:param __name: The name of the recordclass.
:param __fields: A string or list of field names.
:param defaults: Default values for fields. The defaults
may list field names that haven't been listed in *fields*.
'''
name = __name
fields = __fields
fieldset = set(fields)
if isinstance(fields, str):
if ',' in fields:
fields = fields.split(',')
else:
fields = fields.split()
else:
fields = list(fields)
for key in defaults.keys():
if key not in fields:
fields.append(key)
class _record(cls):
__slots__ = fields
__defaults__ = defaults
_record.__name__ = name
return _record"
1656,"def _check_1st_line(line, **kwargs):
""""""First line check.
Check that the first line has a known component name followed by a colon
and then a short description of the commit.
:param line: first line
:type line: str
:param components: list of known component names
:type line: list
:param max_first_line: maximum length of the first line
:type max_first_line: int
:return: errors as in (code, line number, *args)
:rtype: list
""""""
components = kwargs.get(""components"", ())
max_first_line = kwargs.get(""max_first_line"", 50)
errors = []
lineno = 1
if len(line) > max_first_line:
errors.append((""M190"", lineno, max_first_line, len(line)))
if line.endswith("".""):
errors.append((""M191"", lineno))