Coder Social home page Coder Social logo

uhasker / fluentfs Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 1.13 MB

Higher-order functions for the filesystem

Home Page: https://uhasker.github.io/fluentfs

License: MIT License

Python 100.00%
files filesystem functional-programming python higher-order-functions fluent-api

fluentfs's Introduction

fluentfs

The fluentfs library provides a functional and fluent interface for filesystem interactions.

This library is usable and tested, but the API is currently not stable, a lot of features are still missing etc.

In summary - try it out, but (obviously) don't use it for anything in production.

Requirements

You need Python >= 3.9 for this library.

Installation

You can install fluentfs using the pip package manager:

pip install fluentfs

Code examples

Let's say you need to get the total number of lines of all txt files in the current directory (including its subdirectories). The fluentfs library allows you to express this functionally as following:

  1. Filter the files by the "txt" extension
  2. Map the files to their line counts
  3. Reduce the line counts to their sum

This is how it looks in code:

import fluentfs as fs

total_txt_line_count = (
    fs.Dir(".")                         # get the current directory
        .files                          # get an iterator for the (regular) files in the current directory
        .filter_extension("txt")        # filter the files by the txt extension
        .text_file_iterator()           # all the files should now be text files -> obtain a text file iterator to enable text file functions
        .map_line_count()               # map every file to its line count
        .sum()                          # sum the line count
)
print(total_txt_line_count)

The library is very flexible, allowing you to write both short and long forms for most properties:

import fluentfs as fs

# Short form (for when you quickly need to prototype something)
fs.Dir(".").files.filter_ext("txt").t().map_lc().sum()

# Long form (for when you need to use the library in a codebase)
(
    fs.Dir(".")
        .files
        .filter_extension("txt")
        .text_file_iterator()
        .map_line_count()
        .sum()
)

The fluentfs library is very general. If you need to perform an operation that is currently not present, simply call the respective higher-order functions with your own callables. For example here is how we could perform our task by explicitly calling the respective higher-order functions:

import fluentfs as fs

(
    fs.Dir(".")
    .files
    .filter(lambda f: f.extension == "txt")
    .text_file_iterator()
    .map_self(lambda f: f.line_count)
    .reduce(lambda x, y: x + y, 0)
)

Documentation

You can have a look at the basics if this is the first time you are using this library.

You can have a look at the guide if you wish to go more in-depth.

You can have a look at the recipes if you have a specific task you want to accomplish and want to look at some fluent chains that accomplish this or a similar task.

You can also have a look at the API documentation.

fluentfs's People

Contributors

uhasker avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

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.