Python pathlib.Path() Examples

The following are 30 code examples of pathlib.Path(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pathlib , or try the search function .
Example #1
Source File: datasets.py    From pruning_yolov3 with GNU General Public License v3.0 8 votes vote down vote up
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) 
Example #2
Source File: pregenerate_training_data.py    From tpu_pretrain with Apache License 2.0 7 votes vote down vote up
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 
Example #3
Source File: estimator.py    From spleeter with MIT License 6 votes vote down vote up
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) 
Example #4
Source File: test_hachiko.py    From hachiko with MIT License 6 votes vote down vote up
def check_output_is_expected(directory, capsys):
    """Create, move, and delete a file."""
    # Create file
    original_filename = os.path.join(directory, 'file.txt')
    pathlib.Path(original_filename).touch()
    await asyncio.sleep(0.1)  # force release to stdout
    captured = capsys.readouterr()
    assert captured.out == 'File created!\n'
    # Move file
    new_filename = os.path.join(directory, 'new_filename.txt')
    os.rename(original_filename, new_filename)
    await asyncio.sleep(0.1)  # force release to stdout
    captured = capsys.readouterr()
    assert captured.out == 'File moved!\n'
    # Delete file
    os.remove(new_filename)
    await asyncio.sleep(0.1)  # force release to stdout
    captured = capsys.readouterr()
    assert captured.out == 'File deleted!\n' 
Example #5
Source File: api.py    From mutatest with MIT License 6 votes vote down vote up
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 
Example #6
Source File: cli.py    From mutatest with MIT License 6 votes vote down vote up
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."
    ) 
Example #7
Source File: filters.py    From mutatest with MIT License 6 votes vote down vote up
def filter(  # type: ignore
        self,
        loc_idxs: Set[LocIndex],
        source_file: Union[str, Path],
        invert: bool = False,
        resolve_source: bool = True,
    ) -> Set[LocIndex]:
        """Filter based on coverage measured file.

        This adds the source_file argument to the filter abstract method because the coverage
        file holds multiple measured-files, and the ``LocIndex`` object does not have a source
        file attribute. The choice is that the coverage file can be set and read once for the
        class instance, and any valid measured file can be used in the filter.

        Args:
            loc_idxs: location index set of targets
            source_file: source file that is measured by the coverage file
            invert: flag for inverted filter using NOT
            resolve_source: flag for using resolved source_file vs. direct str, default True.
                This exists mostly for testing purposes to access mocked entries in the
                fake coverage files.

        Returns:
            Filtered set of location index set
        """
        measured_file = str(Path(source_file).resolve()) if resolve_source else str(source_file)

        covered_lines = self.coverage_data.lines(measured_file) or list()

        if invert:
            return {loc for loc in loc_idxs if loc.lineno not in covered_lines}
        return {loc for loc in loc_idxs if loc.lineno in covered_lines} 
Example #8
Source File: api.py    From mutatest with MIT License 6 votes vote down vote up
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)) 
Example #9
Source File: librispeech.py    From End-to-end-ASR-Pytorch with MIT License 6 votes vote down vote up
def __init__(self, path, split, tokenizer, bucket_size, ascending=False):
        # Setup
        self.path = path
        self.bucket_size = bucket_size

        # List all wave files
        file_list = []
        for s in split:
            split_list = list(Path(join(path, s)).rglob("*.flac"))
            assert len(split_list) > 0, "No data found @ {}".format(join(path,s))
            file_list += split_list
        # Read text
        text = Parallel(n_jobs=READ_FILE_THREADS)(
            delayed(read_text)(str(f)) for f in file_list)
        #text = Parallel(n_jobs=-1)(delayed(tokenizer.encode)(txt) for txt in text)
        text = [tokenizer.encode(txt) for txt in text]

        # Sort dataset by text length
        #file_len = Parallel(n_jobs=READ_FILE_THREADS)(delayed(getsize)(f) for f in file_list)
        self.file_list, self.text = zip(*[(f_name, txt)
                                          for f_name, txt in sorted(zip(file_list, text), reverse=not ascending, key=lambda x:len(x[1]))]) 
Example #10
Source File: test_cli.py    From mutatest with MIT License 6 votes vote down vote up
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 
Example #11
Source File: kitti_common.py    From kitti-object-eval-python with MIT License 6 votes vote down vote up
def get_label_annos(label_folder, image_ids=None):
    if image_ids is None:
        filepaths = pathlib.Path(label_folder).glob('*.txt')
        prog = re.compile(r'^\d{6}.txt$')
        filepaths = filter(lambda f: prog.match(f.name), filepaths)
        image_ids = [int(p.stem) for p in filepaths]
        image_ids = sorted(image_ids)
    if not isinstance(image_ids, list):
        image_ids = list(range(image_ids))
    annos = []
    label_folder = pathlib.Path(label_folder)
    for idx in image_ids:
        image_idx = get_image_index_str(idx)
        label_filename = label_folder / (image_idx + '.txt')
        annos.append(get_label_anno(label_filename))
    return annos 
Example #12
Source File: kitti_common.py    From kitti-object-eval-python with MIT License 6 votes vote down vote up
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) 
Example #13
Source File: report.py    From mutatest with MIT License 6 votes vote down vote up
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) 
Example #14
Source File: docs_coverage_test.py    From OpenFermion-Cirq with Apache License 2.0 6 votes vote down vote up
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 
Example #15
Source File: toolkit.py    From PickTrue with MIT License 6 votes vote down vote up
def choose_file(self):
        path = filedialog.askdirectory(
            title="选择下载文件夹",
        )
        if not path:
            return
        path = Path(path)
        self.label_text.set(str(path))
        if self._config is not None:
            self._config.op_store_path(self._store_name, path) 
Example #16
Source File: datasets.py    From pruning_yolov3 with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #17
Source File: utils.py    From pruning_yolov3 with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #18
Source File: utils.py    From pruning_yolov3 with GNU General Public License v3.0 6 votes vote down vote up
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() 
Example #19
Source File: utils.py    From tpu_pretrain with Apache License 2.0 6 votes vote down vote up
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) 
Example #20
Source File: conftest.py    From mutatest with MIT License 6 votes vote down vote up
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
#################################################################################################### 
Example #21
Source File: toolkit.py    From PickTrue with MIT License 5 votes vote down vote up
def open_sys_explorer(path):
    ptf = platform.system().lower()
    path = Path(path)
    if "darwin" in ptf:
        return os.system('open %s' % path)
    elif 'windows' in ptf:
        return os.system('explorer.exe "%s"' % path)
    elif 'linux' in ptf:
        return os.system('xdg-open %s' % path)
    return info('平台不支持') 
Example #22
Source File: config.py    From PickTrue with MIT License 5 votes vote down vote up
def op_read_path(self, name):
        path = self.get(name, None)
        return Path(path) if path is not None else None 
Example #23
Source File: config.py    From PickTrue with MIT License 5 votes vote down vote up
def _save(self):
        path = Path(self._save_file)
        with open(path, "w") as f:
            json.dump(self, f) 
Example #24
Source File: config.py    From PickTrue with MIT License 5 votes vote down vote up
def from_config_file(cls):
        path = Path(cls._save_file)
        if not os.path.exists(path):
            return cls()
        with open(path, "rb") as f:
            return cls(**json.load(f)) 
Example #25
Source File: app.py    From quart with MIT License 5 votes vote down vote up
def select_jinja_autoescape(self, filename: str) -> bool:
        """Returns True if the filename indicates that it should be escaped."""
        if filename is None:
            return True
        return Path(filename).suffix in {".htm", ".html", ".xhtml", ".xml"} 
Example #26
Source File: filters.py    From mutatest with MIT License 5 votes vote down vote up
def __init__(self, coverage_file: Union[str, Path] = Path(".coverage")) -> None:
        """Initialize the filter.

        Args:
            coverage_file: an optional coverage file, a default ".coverage" is used.
        """
        self._coverage_file = Path(coverage_file)
        self._coverage_data: Optional[CoverageData] = None 
Example #27
Source File: loaders.py    From python-clean-architecture with MIT License 5 votes vote down vote up
def __call__(self, filepath: Path):
        return self.value(filepath) 
Example #28
Source File: os.py    From python-clean-architecture with MIT License 5 votes vote down vote up
def read_from_file(filepath: str, encoding: str = None, errors: str = None):
    """Simple util meant to easy the process of mocking file opening"""
    return pathlib.Path(filepath).read_text(encoding=encoding, errors=errors) 
Example #29
Source File: app.py    From quart with MIT License 5 votes vote down vote up
def name(self) -> str:
        """The name of this application.

        This is taken from the :attr:`import_name` and is used for
        debugging purposes.
        """
        if self.import_name == "__main__":
            path = Path(getattr(sys.modules["__main__"], "__file__", "__main__.py"))
            return path.stem
        return self.import_name 
Example #30
Source File: utils.py    From quart with MIT License 5 votes vote down vote up
def file_path_to_path(*paths: FilePath) -> Path:
    # Flask supports bytes paths
    safe_paths: List[Union[str, PathLike]] = []
    for path in paths:
        if isinstance(path, bytes):
            safe_paths.append(path.decode())
        else:
            safe_paths.append(path)
    return Path(*safe_paths)