Giter Club home page Giter Club logo

pathlad's Introduction

Pathlad

Your friendly Python Path Converter

Python 3.4 introduced Pathlib, a clean object-oriented cross-platform way of handling paths. If the examples in the docs do not convince you yet, please read these blog posts by Trey Hunner: Why you should be using Pathlib and No really, pathlib is great. Don't let your codebases be another one full with unnecessary path logic.

What it does

  1. Parses your Python code and automatically fix prehistoric file manipulations.

  2. Helps you kickstart with refactoring you legacy code paths. Don't expect it to do all the work for you (yet). The best part: even if it makes a mistake, you know already where to look.

  3. Profit.

Example

Before:

import os

os.path.isdir(os.path.join(__file__, "../test"))

After:

from pathlib import Path

(Path(__file__) / ".." / "test").is_dir()

You can find more examples in the examples directory.

Installation

Installation via pip:

pip install pathlad

Usage

Command line interface:

pathlad [dirname]

API Coverage

  • Pathlib's equivalences
  • os.makedirs
  • os.path.normpath
  • os.path.realpath
  • open
  • os.path.getsize
  • os.listdir
  • Nested calls
  • glob.glob
  • Path.write_*
  • from os import method

Advanced Usage options

Warning: pathlad is under active development, the API might change without notice.

pathlad is built on top of 2to3 (just like black). For now, the options are identical.

Usage: pathlad [options] file|dir ...

Options:
  -h, --help            show this help message and exit
  -j PROCESSES, --processes=PROCESSES
                        Run 2to3 concurrently
  -x NOFIX, --nofix=NOFIX
                        Prevent a transformation from being run
  -v, --verbose         More verbose logging
  --no-diffs            Don't show diffs of the refactoring
  -w, --write           Write back modified files
  -n, --nobackups       Don't write backups for modified files
  -o OUTPUT_DIR, --output-dir=OUTPUT_DIR
                        Put output files in this directory instead of
                        overwriting the input files.  Requires -n.
  -W, --write-unchanged-files
                        Also write files even if no changes were required
                        (useful with --output-dir); implies -w.
  --add-suffix=ADD_SUFFIX
                        Append this string to all output filenames. Requires
                        -n if non-empty.  ex: --add-suffix='3' will generate
                        .py3 files.

Tips

  • pathlad doesn't care about formatting. For the best results, run isort and black afterwards.

  • pathlib is generally slower than os.path, especially when creating lots of Path objects. In most cases however, such as dataset creation scripts and tests, this doesn't weigh up to the value of readable, maintainable code.

Contributing

However, being a programmer - I'm too lazy to spend 8 hours mindlessly performing a function, but not too lazy to spend 16 hours automating it.

~ Timothy Crosley

Pull-requests are welcome.

Helpful contributions include:

  • Extending API coverage for os, glob, shutils
  • More robust fixing w.r.t. spacing, optional arguments etc.
  • A method validation only (for example, a --check-only flag)
  • A pre-commit hook
  • Typing of some kind
  • Basically any feature you see in black or isort etc.
  • Remove unused os, glob imports (flake8 --select F401, autoflake --in-place --imports=os,glob,pathlib)

Resources

pathlad's People

Contributors

sbrugman avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

pathlad's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.