本文整理汇总了Python中pathlib.Path方法的典型用法代码示例。如果您正苦于以下问题:Python pathlib.Path方法的具体用法?Python pathlib.Path怎么用?Python pathlib.Path使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pathlib
的用法示例。
在下文中一共展示了pathlib.Path方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_report
# 需要导入模块: import pathlib [as 别名]
# 或者: from pathlib import Path [as 别名]
def write_report(report: str, location: Path) -> None:
"""Write the report to a file.
If the location does not exist with folders they are created.
Args:
report: the string report to write
location: path location to the file
Returns:
None, writes output to location
"""
if not location.parent.exists():
LOGGER.info("Creating directory tree for: %s", location.parent.resolve())
location.parent.mkdir(parents=True, exist_ok=True)
with open(location, "w", encoding="utf-8") as output_loc:
LOGGER.info("Writing output report to: %s", location.resolve())
output_loc.write(report)
示例2: get_src_location
# 需要导入模块: import pathlib [as 别名]
# 或者: from pathlib import Path [as 别名]
def get_src_location(src_loc: Optional[Path] = None) -> Path:
"""Find packages is used if the ``src_loc`` is not set
Args:
src_loc: current source location, defaults to None
Returns:
Path to the source location
Raises:
FileNoeFoundError: if the source location doesn't exist.
"""
if not src_loc:
find_pkgs = find_packages()
if find_pkgs:
src_loc = Path(find_pkgs[0])
return src_loc
else:
if src_loc.exists():
return src_loc
raise FileNotFoundError(
"No source directory specified or automatically detected. "
"Use --src or --help to see options."
)
示例3: __setitem__
# 需要导入模块: import pathlib [as 别名]
# 或者: from pathlib import Path [as 别名]
def __setitem__(self, key: Path, value: Genome) -> None:
"""Setter for GenomeGroup, enforces Path keys and Genome values.
Args:
key: key for the mapping, must be a path
value: the genome
Returns:
None
"""
if not isinstance(key, Path):
raise TypeError("Only Path keys are supported.")
if not isinstance(value, Genome):
raise TypeError("Only Genome values are supported.")
self._store[key] = value
示例4: add_file
# 需要导入模块: import pathlib [as 别名]
# 或者: from pathlib import Path [as 别名]
def add_file(
self,
source_file: Union[str, Path],
coverage_file: Optional[Union[str, Path]] = Path(".coverage"),
) -> None:
"""Add a ``.py`` source file to the group as a new Genome.
The Genome is created automatically.
Args:
source_file: the source file to add with Genome creation
coverage_file: an optional coverage file to set on the Genome, defaults to ".coverage".
Returns:
None
"""
self.add_genome(Genome(source_file=source_file, coverage_file=coverage_file))
示例5: test_cli_summary_report_invariant
# 需要导入模块: import pathlib [as 别名]
# 或者: from pathlib import Path [as 别名]
def test_cli_summary_report_invariant(mock_args, mock_TrialTimes, lm, li):
"""Property:
1. cli_summary report returns a valid string without errors given any set of integers for
locs_mutated and locs_identified.
"""
results = cli.cli_summary_report(
src_loc=Path("file.py"),
args=mock_args,
locs_mutated=lm,
locs_identified=li,
runtimes=mock_TrialTimes,
)
assert isinstance(results, str)
assert len(results) > 1
示例6: mock_source_and_targets
# 需要导入模块: import pathlib [as 别名]
# 或者: from pathlib import Path [as 别名]
def mock_source_and_targets():
"""Mock source file with uncovered/covered targets to use with mock_coverage_file.
Covered lines include: 1, 2, 4
"""
# see mock_coverage_file fixture
source_file = Path("file3.py")
targets = {
LocIndex(ast_class="AugAssign", lineno=1, col_offset=1, op_type="o"),
LocIndex(ast_class="AugAssign", lineno=2, col_offset=1, op_type="o"),
LocIndex(ast_class="AugAssign", lineno=3, col_offset=1, op_type="o"),
LocIndex(ast_class="BinOp", lineno=4, col_offset=1, op_type="o"),
LocIndex(ast_class="BinOp", lineno=5, col_offset=1, op_type="o"),
}
return SourceAndTargets(source_file, targets)
####################################################################################################
# TRANSFORMERS: AUGASSIGN FIXTURES
####################################################################################################
示例7: to_predictor
# 需要导入模块: import pathlib [as 别名]
# 或者: from pathlib import Path [as 别名]
def to_predictor(estimator, directory=DEFAULT_EXPORT_DIRECTORY):
""" Exports given estimator as predictor into the given directory
and returns associated tf.predictor instance.
:param estimator: Estimator to export.
:param directory: (Optional) path to write exported model into.
"""
input_provider = InputProviderFactory.get(estimator.params)
def receiver():
features = input_provider.get_input_dict_placeholders()
return tf.estimator.export.ServingInputReceiver(features, features)
estimator.export_saved_model(directory, receiver)
versions = [
model for model in Path(directory).iterdir()
if model.is_dir() and 'temp' not in str(model)]
latest = str(sorted(versions)[-1])
return predictor.from_saved_model(latest)
示例8: _api_rst_fullnames_per_section
# 需要导入模块: import pathlib [as 别名]
# 或者: from pathlib import Path [as 别名]
def _api_rst_fullnames_per_section() -> List[List[str]]:
result: List[List[str]] = []
section: List[str] = []
seen: Set[str] = set()
with open(Path(__file__).parent / 'api.rst', mode='r') as f:
for line in f.readlines():
if line.strip() == '.. autosummary::':
if section:
result.append(section)
section = []
elif line.startswith(' openfermioncirq.'):
fullname = line.strip()
if fullname in seen:
# coverage: ignore
raise ValueError(f'{fullname} appears twice in api.rst')
section.append(fullname)
seen.add(fullname)
if section:
result.append(section)
return result
示例9: __init__
# 需要导入模块: import pathlib [as 别名]
# 或者: from pathlib import Path [as 别名]
def __init__(self, path, img_size=416, half=False):
path = str(Path(path)) # os-agnostic
files = []
if os.path.isdir(path):
files = sorted(glob.glob(os.path.join(path, '*.*')))
elif os.path.isfile(path):
files = [path]
images = [x for x in files if os.path.splitext(x)[-1].lower() in img_formats]
videos = [x for x in files if os.path.splitext(x)[-1].lower() in vid_formats]
nI, nV = len(images), len(videos)
self.img_size = img_size
self.files = images + videos
self.nF = nI + nV # number of files
self.video_flag = [False] * nI + [True] * nV
self.mode = 'images'
self.half = half # half precision fp16 images
if any(videos):
self.new_video(videos[0]) # new video
else:
self.cap = None
assert self.nF > 0, 'No images or videos found in ' + path
示例10: convert_images2bmp
# 需要导入模块: import pathlib [as 别名]
# 或者: from pathlib import Path [as 别名]
def convert_images2bmp():
# cv2.imread() jpg at 230 img/s, *.bmp at 400 img/s
for path in ['../coco/images/val2014/', '../coco/images/train2014/']:
folder = os.sep + Path(path).name
output = path.replace(folder, folder + 'bmp')
if os.path.exists(output):
shutil.rmtree(output) # delete output folder
os.makedirs(output) # make new output folder
for f in tqdm(glob.glob('%s*.jpg' % path)):
save_name = f.replace('.jpg', '.bmp').replace(folder, folder + 'bmp')
cv2.imwrite(save_name, cv2.imread(f))
for label_path in ['../coco/trainvalno5k.txt', '../coco/5k.txt']:
with open(label_path, 'r') as file:
lines = file.read()
lines = lines.replace('2014/', '2014bmp/').replace('.jpg', '.bmp').replace(
'/Users/glennjocher/PycharmProjects/', '../')
with open(label_path.replace('5k', '5k_bmp'), 'w') as file:
file.write(lines)
示例11: coco_single_class_labels
# 需要导入模块: import pathlib [as 别名]
# 或者: from pathlib import Path [as 别名]
def coco_single_class_labels(path='../coco/labels/train2014/', label_class=43):
# Makes single-class coco datasets. from utils.utils import *; coco_single_class_labels()
if os.path.exists('new/'):
shutil.rmtree('new/') # delete output folder
os.makedirs('new/') # make new output folder
os.makedirs('new/labels/')
os.makedirs('new/images/')
for file in tqdm(sorted(glob.glob('%s/*.*' % path))):
with open(file, 'r') as f:
labels = np.array([x.split() for x in f.read().splitlines()], dtype=np.float32)
i = labels[:, 0] == label_class
if any(i):
img_file = file.replace('labels', 'images').replace('txt', 'jpg')
labels[:, 0] = 0 # reset class to 0
with open('new/images.txt', 'a') as f: # add image to dataset list
f.write(img_file + '\n')
with open('new/labels/' + Path(file).name, 'a') as f: # write label
for l in labels[i]:
f.write('%g %.6f %.6f %.6f %.6f\n' % tuple(l))
shutil.copyfile(src=img_file, dst='new/images/' + Path(file).name.replace('txt', 'jpg')) # copy images
示例12: plot_images
# 需要导入模块: import pathlib [as 别名]
# 或者: from pathlib import Path [as 别名]
def plot_images(imgs, targets, paths=None, fname='images.jpg'):
# Plots training images overlaid with targets
imgs = imgs.cpu().numpy()
targets = targets.cpu().numpy()
# targets = targets[targets[:, 1] == 21] # plot only one class
fig = plt.figure(figsize=(10, 10))
bs, _, h, w = imgs.shape # batch size, _, height, width
bs = min(bs, 16) # limit plot to 16 images
ns = np.ceil(bs ** 0.5) # number of subplots
for i in range(bs):
boxes = xywh2xyxy(targets[targets[:, 0] == i, 2:6]).T
boxes[[0, 2]] *= w
boxes[[1, 3]] *= h
plt.subplot(ns, ns, i + 1).imshow(imgs[i].transpose(1, 2, 0))
plt.plot(boxes[[0, 2, 2, 0, 0]], boxes[[1, 1, 3, 3, 1]], '.-')
plt.axis('off')
if paths is not None:
s = Path(paths[i]).name
plt.title(s[:min(len(s), 40)], fontdict={'size': 8}) # limit to 40 characters
fig.tight_layout()
fig.savefig(fname, dpi=200)
plt.close()
示例13: init
# 需要导入模块: import pathlib [as 别名]
# 或者: from pathlib import Path [as 别名]
def init(args):
# init logger
log_format = '%(asctime)-10s: %(message)s'
if args.log_file is not None and args.log_file != "":
Path(args.log_file).parent.mkdir(parents=True, exist_ok=True)
logging.basicConfig(level=logging.INFO, filename=args.log_file, filemode='w', format=log_format)
logging.warning(f'This will get logged to file: {args.log_file}')
else:
logging.basicConfig(level=logging.INFO, format=log_format)
# create output dir
if args.output_dir.is_dir() and list(args.output_dir.iterdir()):
logging.warning(f"Output directory ({args.output_dir}) already exists and is not empty!")
assert 'bert' in args.output_dir.name, \
'''Output dir name has to contain `bert` or `roberta` for AutoModel.from_pretrained to correctly infer the model type'''
args.output_dir.mkdir(parents=True, exist_ok=True)
# set random seeds
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
示例14: __init__
# 需要导入模块: import pathlib [as 别名]
# 或者: from pathlib import Path [as 别名]
def __init__(self, reduce_memory=False):
if reduce_memory:
self.temp_dir = TemporaryDirectory()
self.working_dir = Path(self.temp_dir.name)
self.document_shelf_filepath = self.working_dir / 'shelf.db'
self.document_shelf = shelve.open(str(self.document_shelf_filepath),
flag='n', protocol=-1)
self.documents = None
else:
self.documents = []
self.document_shelf = None
self.document_shelf_filepath = None
self.temp_dir = None
self.doc_lengths = []
self.doc_cumsum = None
self.cumsum_max = None
self.reduce_memory = reduce_memory
示例15: get_kitti_info_path
# 需要导入模块: import pathlib [as 别名]
# 或者: from pathlib import Path [as 别名]
def get_kitti_info_path(idx,
prefix,
info_type='image_2',
file_tail='.png',
training=True,
relative_path=True):
img_idx_str = get_image_index_str(idx)
img_idx_str += file_tail
prefix = pathlib.Path(prefix)
if training:
file_path = pathlib.Path('training') / info_type / img_idx_str
else:
file_path = pathlib.Path('testing') / info_type / img_idx_str
if not (prefix / file_path).exists():
raise ValueError("file not exist: {}".format(file_path))
if relative_path:
return str(file_path)
else:
return str(prefix / file_path)