Giter Site home page Giter Site logo

jtprince / enumerable Goto Github PK

View Code? Open in Web Editor NEW

This project forked from brenttaylor/enumerable

0.0 2.0 0.0 12 KB

Enumerable is a python library that re-implements a few core features of python's functional toolbox to be more flexible and support a more transactional way of solving problems.

License: MIT License

Python 100.00%

enumerable's Introduction

Enumerable

Enumerable is a python library that re-implements a few core features of python's functional toolbox to be more flexible and support a more transactional way of solving problems. By design, these tools support method chaining and lazy evaluation.

Installation

Installation is simple via pip.

pip install enumerable

Examples of use

A common thing we do in python, especially in the IT world is for data processing. Let's say you want to load a log file and filter for specific events and strip off the line endings for further processing:

import re
from enumerable import map, filter, reduce
from operator import methodcaller


# Assume a log format along the lines of:
# CRITICAL - [5:17:2015] - <error details>
# WARNING - [2:2:2016] - <warning details>
# SUCCESS - [8:24:2017] - <success details>
with open("log.txt", 'r') as log:
    def date(log_line, timestamp):
        return timestamp in log_line
    
    def critical_error(log_line):
        return re.search("^CRITICAL", log_line)

    # Get today's critical errors and strip
    # strip the line endings
    results = filter(log, date, "[3:29:2017]") \
        .filter(critical_error) \
        .map(methodcaller('strip'))

for line in results:
    <do something>

Map, Fold, and Reduce are currently provided and they are all lazy evaluated and support method chaining.

>>> import operator
>>> from enumerable import map, filter, reduce
>>>
>>> my_list = [1, 2, 3, 4, 5]
>>> # Notice that any additional arguments passed to map/filter/reduce
... # are then passed to the provided function.
... map(my_list, operator.pow, 2).to(tuple)
(1, 4, 9, 16, 25)
>>>

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.