with tempfile.NamedTemporaryFile() as f:
= 'There is nothing\nbut\nprogress\nto eat'
s
save_txt(s, f.name)= open_txt(f.name)
s_
equals(s, s_)
#Test append
'\nnew', f.name, append=True)
save_txt(= open_txt(f.name)
s_ +'\nnew', s_) equals(s
Utils
Utility functions.
save_txt
save_txt (s, path, append=False)
open_txt
open_txt (path)
open_json
open_json (path)
save_json
save_json (d, path)
with tempfile.NamedTemporaryFile() as f:
= {'pita': 'bread', 42: ['elden', 'ring']}
d
save_json(d, f.name)= open_json(f.name)
d_ equals(d, d_)
save_pickle
save_pickle (o, path)
open_pickle
open_pickle (path)
with tempfile.NamedTemporaryFile() as f:
= ['There is nothing', 'but progress', 'to eat']
s
save_pickle(s, f.name)= open_pickle(f.name)
s_ equals(s, s_)
save_dill
save_dill (o, path)
open_dill
open_dill (path)
with tempfile.NamedTemporaryFile() as f:
= ['There is nothing', 'but progress', 'to eat']
s
save_dill(s, f.name)= open_dill(f.name)
s_ equals(s, s_)
mkdir
mkdir (path, exist_ok=False, parents=False, overwrite=False, tmp=False, **kwargs)
Creates and returns a directory defined by path
, optionally removing previous existing directory if overwrite
is True
with tempfile.TemporaryDirectory() as d:
= Path(os.path.join(d, 'new_dir'))
path = mkdir(path)
new_dir assert new_dir.exists()
test_eq(new_dir, path)
# test overwrite
with open(new_dir/'test.txt', 'w') as f: f.writelines('test')
len(list(walk(new_dir))), 1) # assert file is present
test_eq(= mkdir(new_dir, overwrite=True)
new_dir len(list(walk(new_dir))), 0) # assert file was deleted test_eq(
extract_files
extract_files (files, extract_to_dir, show_pbar=True)
open_image
open_image (path, gray=False, ignore_exif=True)
Open an image from disk path
as a PIL Image
= open_image(pv.test.SEG_IMAGE)
image image
open_mask
open_mask (path, gray=True, ignore_exif=True)
open_mask(pv.test.SEG_MASK)
save_image
save_image (image, path)
with tempfile.TemporaryDirectory() as d:
= Path(d)/'test.png'
path
save_image(image, path)= open_image(path)
image_ test_eq(np.array(image), np.array(image_))
get_files
get_files (path, extensions=None, recurse=True, folders=None, followlinks=True)
Get all the files in path
with optional extensions
, optionally with recurse
, only in folders
, if specified. COPIED FROM https://github.com/fastai/fastai/blob/master/nbs/05_data.transforms.ipynb
len(get_files(pv.test.ROOT_DIR/'segmentation', extensions='.png')), 2) test_eq(
ImageFile
ImageFile (x=None, *args, **kwargs)
*Path subclass for non-Windows systems.
On a POSIX system, instantiating a Path should return this object.*
= ImageFile(str(pv.test.SEG_IMAGE))
image_file assert isinstance(image_file.parent/'test', ImageFile)
str(image_file.parent/'test')
'/home/lgvaz/git/polvo/assets/segmentation/test'
str(pv.test.SEG_IMAGE)).open() ImageFile(
get_image_files
get_image_files (path, recurse=True, folders=None)
Get image files in path
recursively, only in folders
, if specified. COPIED FROM https://github.com/fastai/fastai/blob/master/nbs/05_data.transforms.ipynb
len(get_image_files(pv.test.ROOT_DIR/'segmentation')), 2) test_eq(
glob_match
glob_match (dirpath, matches:Sequence[str], recursive=True)
'*.png']) glob_match(pv.test.ROOT_DIR, [
[Path('/home/lgvaz/git/polvo/assets/polvo.png'),
Path('/home/lgvaz/git/polvo/assets/segmentation/mask1.png'),
Path('/home/lgvaz/git/polvo/assets/segmentation/image1.png')]
'*.png'], recursive=False) glob_match(pv.test.ROOT_DIR, [
[Path('/home/lgvaz/git/polvo/assets/polvo.png')]
RenderDict
RenderDict (json_data)
From https://www.reddit.com/r/IPython/comments/34t4m7/lpt_print_json_in_collapsible_format_in_ipython/
'h1': {'h2.1': [1, 2, 3, 4], 'h2.2': 'a'}}) RenderDict({