Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_read(self):
c = Collection.open(self._fn_collection, mode='r')
assert len(list(c)) == 10
assert len(list(c)) == 10
assert len(c.find()) == 10
c.close()
def setUp(self, request):
self._tmp_dir = TemporaryDirectory(prefix='signac_collection_')
request.addfinalizer(self._tmp_dir.cleanup)
self._fn_json = os.path.join(self._tmp_dir.name, 'test.json')
self.c = Collection.open(filename=':memory:')
docs = [dict(_id=str(i)) for i in range(10)]
self.c.update(docs)
self.c.flush()
def test_write_flush_and_reopen(self):
docs = [dict(_id=str(i)) for i in range(10)]
self.c.update(docs)
self.c.flush()
assert os.path.getsize(self._fn_collection) > 0
with Collection.open(self._fn_collection) as c:
assert len(c) == len(docs)
for doc in self.c:
assert doc['_id'] in c
def test_read(self):
with Collection.open(self._fn_collection, mode='r') as c:
assert len(list(c)) == 3
with open(self._fn_collection, 'a') as file:
file.write("{'a': 0}\n") # ill-formed JSON (single quotes instead of double quotes)
with pytest.raises(JSONParseError):
with Collection.open(self._fn_collection, mode='r') as c:
pass
def test_file_size(self):
docs = [dict(_id=str(i)) for i in range(10)]
with open(self._fn_collection) as f:
assert len(list(f)) == 0
with Collection.open(self._fn_collection) as c:
c.update(docs)
with open(self._fn_collection) as f:
assert len(list(f)) == len(docs)
with Collection.open(self._fn_collection) as c:
assert len(c) == len(docs)
for doc in docs:
c.replace_one({'_id': doc['_id']}, doc)
with Collection.open(self._fn_collection) as c:
assert len(c) == len(docs)
with open(self._fn_collection) as f:
assert len(list(f)) == len(docs)
def read_benchmark(filename, filter=None, include_metadata=False):
with signac.Collection.open(filename) as c:
docs = list(c.find(filter))
df_data = pd.DataFrame(
{doc['_id']: dict(normalize(doc['data'], doc['meta']['N'])) for doc in docs}).T
if include_metadata:
df_meta = pd.DataFrame(
{doc['_id']: doc['meta'] for doc in docs}).T
return pd.concat([df_meta, df_data], axis=1)
else:
return df_data
def store_result(key, doc):
if args.output == '-':
if args.json:
print(json.dumps(doc, indent=2))
else:
pprint(doc)
else:
with signac.Collection.open(args.output) as c:
c.replace_one(key, doc, upsert=True)
def _log_operation(operation, error=None):
if self.strict_git:
if git.Repo(operation.job._project.root_directory()).is_dirty():
raise RuntimeError(
"Unable to reliably log operation, because the git repository in "
"the project root directory is dirty.\n\nMake sure to commit all "
"changes or ignore this warning by setting '{}(strict_git=False)'.".format(
type(self).__name__))
metadata = collect_metadata(operation)
# Add additional execution-related information to metadata.
metadata['stage'] = stage
metadata['error'] = None if error is None else str(error)
# Write metadata to collection inside job workspace.
with Collection.open(operation.job.fn(self._fn_logfile)) as logfile:
logfile.insert_one(metadata)