All Projects → jlevy → strif

jlevy / strif

Licence: Apache-2.0 license
Tiny, useful Python lib for strings and files

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to strif

yogurl
Serve files and code over HTTP in one command. The CLI for http://yogurl.io
Stars: ✭ 45 (+18.42%)
Mutual labels:  files
FileOrganizer
Automatically organizes files in your computer
Stars: ✭ 34 (-10.53%)
Mutual labels:  files
ripgrep-zh
中文翻译:<BurntSushi/ripgrep> 一个面向行的搜索工具 ❤️ 校对 ✅
Stars: ✭ 82 (+115.79%)
Mutual labels:  files
nucked-truth-of-files
HollyJS Moscow
Stars: ✭ 14 (-63.16%)
Mutual labels:  files
vtex-cms-sauce
VTEX package for handling CMS requests.
Stars: ✭ 25 (-34.21%)
Mutual labels:  files
vsc-material-theme-icons
Material Theme Icons, the most epic icons theme for Visual Studio Code and Material Theme.
Stars: ✭ 90 (+136.84%)
Mutual labels:  files
go-storage
A vendor-neutral storage library for Golang: Write once, run on every storage service.
Stars: ✭ 387 (+918.42%)
Mutual labels:  files
txr
Send files super easily to other computers/servers using WebSockets.
Stars: ✭ 20 (-47.37%)
Mutual labels:  files
file2html
JS convertor of files to HTML and CSS code
Stars: ✭ 29 (-23.68%)
Mutual labels:  files
washer
A whoosh-based CLI indexer and searcher for your files.
Stars: ✭ 16 (-57.89%)
Mutual labels:  files
files-io
Read many files with node
Stars: ✭ 19 (-50%)
Mutual labels:  files
glob-fs
file globbing for node.js. speedy and powerful alternative to node-glob. This library is experimental and does not work on windows!
Stars: ✭ 54 (+42.11%)
Mutual labels:  files
odcrawler-frontend
A frontend for ODCrawler, an Open Directory search engine.
Stars: ✭ 20 (-47.37%)
Mutual labels:  files
dirdf
R package: dirdf - Extracts Metadata from Directory and File Names
Stars: ✭ 57 (+50%)
Mutual labels:  files
speedcopy
Patched python shutil.copyfile to allow faster speeds on samba shares.
Stars: ✭ 13 (-65.79%)
Mutual labels:  files
lumen-file-manager
File manager module for the Lumen PHP framework.
Stars: ✭ 40 (+5.26%)
Mutual labels:  files
tg-file-decoder
Decode Telegram bot API file IDs
Stars: ✭ 30 (-21.05%)
Mutual labels:  files
copy
Copy files using glob patterns. Sync, async, promise or streams. (node.js utility)
Stars: ✭ 84 (+121.05%)
Mutual labels:  files
Hyde
Call of Duty XAsset compiler that transforms raw assets into digestible data.
Stars: ✭ 15 (-60.53%)
Mutual labels:  files
upload
How to Upload a File to a Server in PHP
Stars: ✭ 83 (+118.42%)
Mutual labels:  files

strif

Strif is a tiny (only a few hundred loc) library of string- and file-related utilities for Python 2.7 and 3.6.

The goal is to complement the standard libs, not replace or wrap them. It’s basically an assembly of some functions and tricks that have repeatedly shown value in various projects.

Why bother, if it’s so short? Because it saves time, fills in gaps, avoids clumsy repetition, and has zero dependencies.

Is it mature? I’ve used these tools individually in production situations, but it’s not a comprehensively tested library.

Highlights

Atomic file operations plus extras

It’s generally good practice when creating files to write to a file with a temporary name, and move it to a final location once the file is complete. This way, you never leave partial, incorrect versions of files in a directory due to interruptions or failures. For example, these can be used in place of shutil.copyfile or shutil.copytree:

copyfile_atomic(source_path, dest_path, make_parents=True, backup_suffix=None)
copytree_atomic(source_path, dest_path, make_parents=True, backup_suffix=None, symlinks=False)

You also have convenience options for creating parent directories of the target, if they don’t exist. And you can keep a backup of the target, rather than clobber it, if you prefer. Used judiciously, these options can save you some boilerplate coding.

Syntax sugar for atomic file creation

Similarly, you have syntax sugar for creating files or directories atomically using with:

with atomic_output_file("some-dir/my-final-output.txt",
                        make_parents=True, backup_suffix=".old.{timestamp}") as temp_target:
  with open(temp_target, "w") as f:
    f.write("some contents")

Now if there is some issue during write, the output will instead be at a temporary location in the same directory. (It will have a name like some-dir/my-final-output.txt.partial.XXXXX.) This ensures integrity of the file appearing in the final location.

Optionally, as a bonus, if you would have clobbered a previous output, it keeps a backup with a (fixed or uniquely timestamped) suffix.

Syntax sugar for temporary files

Syntax sugar for auto-deleting temporary files or directories using with:

with temp_output_file("my-scratch.") as (fd, path):
  # Do a bunch of stuff with the opened file descriptor or path, knowing
  # it will be removed assuming successful termination.


with temp_output_dir("work-dir.", dir="/var/tmp") as work_dir:
  # Create some files in the now-existing path work_dir, and it will be
  # deleted afterwords.

Note these don’t delete files in case of error, which is usually what you want. Add always_clean=True if you want the temporary file or directory to be removed no matter what.

Miscellany

  • Secure random base 36 identifiers: Digression: Use base 36 in general for random id strings. It is briefer than hex, avoids ugly non-alphanumeric characters like base 64, and is case insensitive, which is generally wise (e.g. due to MacOS case-insensitive filesystems).
  • Secure random timestamped identifiers. These are similar, but start with an ISO timestamp plus a base 36 identifier, so that they sort chronologically but are still unique.
  • Abbreviations for strings and lists: Abbreviate strings and lists in a pretty way, truncating items and adding ellipses.

Installation

pip install strif

Or add as a dependency in your setup.py.

Further Information

pydoc strif

Contributing

Please file issues or PRs!

License

Apache 2.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].