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_DMatrix_save_to_path(self):
"""Saving to a binary file using pathlib from a DMatrix."""
data = np.random.randn(100, 2)
target = np.array([0, 1] * 50)
features = ['Feature1', 'Feature2']
dm = xgb.DMatrix(data, label=target, feature_names=features)
# save, assert exists, remove file
binary_path = Path("dtrain.bin")
dm.save_binary(binary_path)
assert binary_path.exists()
Path.unlink(binary_path)
assert len(dump) == 1, 'Exepcted only 1 tree to be dumped.'
assert len(dump[0].splitlines()) == 3, 'Expected 1 root and 2 leaves - 3 lines.'
# load the model again using Path
bst2 = xgb.Booster(model_file=save_path)
dump2 = bst2.get_dump()
dump_assertions(dump2)
# load again using load_model
bst3 = xgb.Booster()
bst3.load_model(save_path)
dump3= bst3.get_dump()
dump_assertions(dump3)
# remove file
Path.unlink(save_path)
def test_save_image():
""" Tests save_image to create image"""
fake_path = "fake_images/Fakeimage.jpg"
fake_image = b"FakeFakeFake"
bot.save_image(path=fake_path, image=fake_image)
assert Path.exists(Path(fake_path)), "Fake image wasn't created"
# Removing test path & image
Path.unlink(Path(fake_path))
Path.rmdir(Path(fake_path).parent)
def test_save_db(self):
"""
Saves the current database to a file.
"""
self.myapp.import_file_dirs('test-data/COD')
testfile = Path('./tst.sql')
with suppress(Exception):
Path.unlink(testfile)
self.myapp.save_database(testfile.absolute())
self.assertEqual(True, testfile.is_file())
self.assertEqual(True, testfile.exists())
Path.unlink(testfile)
self.assertEqual(False, testfile.exists())
self.assertEqual('Database saved.', self.myapp.statusBar().currentMessage())
copy_json = json.loads(copy_notebook_path.read_text())
assert all(
[
copy_cell["source"] == source_cell["source"]
for copy_cell, source_cell in zip(copy_json["cells"], source_json["cells"])
]
)
# Have to run blackbook main to show spaces_copy.ipynb is unaffected
blackbook.__main__.main(source_notebook_path)
output_json = json.loads(source_notebook_path.read_text())
check_copy_json = json.loads(copy_notebook_path.read_text())
# Revert to pre-format state: unformat source notebook and delete copy
shutil.copy(str(copy_notebook_path), str(source_notebook_path))
pathlib.Path.unlink(copy_notebook_path)
assert not all(
[
formatted_cell["source"] == unformatted_cell["source"]
for formatted_cell, unformatted_cell in zip(
output_json["cells"], check_copy_json["cells"]
)
for block in blocks:
write(bytes(block))
for gp, rec_id, original_address in zip(
self.groups, gp_rec_ids, original_data_block_addrs
):
gp.data_group.record_id_len = rec_id
gp.data_group.data_block_addr = original_address
seek(0)
write(bytes(self.identification))
write(bytes(self.header))
if dst == self.name:
self.close()
Path.unlink(self.name)
Path.rename(destination, self.name)
self.groups.clear()
self.header = None
self.identification = None
self.channels_db.clear()
self.masters_db.clear()
self._tempfile = TemporaryFile()
self._file = open(self.name, "rb")
self._read()
if self._callback:
self._callback(100, 100)
return dst
try:
self._log.info('Sending %s to %s', file_path,
self._bot.first_name)
now = datetime.datetime.now()
caption = 'Directory Watcher Alert at {0}'.format(
now.strftime('%Y-%m-%d %H:%M:%S'))
with open(file_path, 'rb') as fd:
self._bot.reply_cam_photo(photo=fd, caption=caption,
from_watchdog=True)
except Exception as err:
self._log.error('Can\'t open %s for sending: %s',
file_path, err)
raise DirectoryWatcherError(err)
try:
self._log.info('%s sent, deleting', file_path)
Path.unlink(file_path)
except Exception as err:
self._log.error('Can\'t delete %s: %s', file_path, err)
raise DirectoryWatcherError(err)
def profile_save():
data_fields = ['username', 'about', 'allow_community']
user = User.objects.get(id=current_user.id)
for field in data_fields:
value = request.form.get(field)
setattr(user, field, value)
if not user.socials:
user.socials = {}
for field, value in request.form.items():
if field.startswith('socials.'):
user.socials[field[len('socials.'):]] = value
if request.form.get('delete'):
Path.unlink(Path(f"{app.config.get('AVATARS_PATH')}/{user.id}"))
user.image_path = None
if request.files.get('avatar'):
path = f"{app.config.get('AVATARS_PATH')}/{user.id}"
save_image(request.files['avatar'], path, (200, 200))
user.image_path = path
user.save()
return redirect(url_for_user(user))