The fluent Library
- Narrative Documentation
- Motivation: Why use
- Caveats and lessons learned
- Famous Last Words
- Motivation: Why use
- API Documentation
- Integration with other libraries
- Live Example Code to play with
- TODOs and future directions
- How to work on fluenpty
Project Homepage: https://github.com/dwt/fluent/
Build Server: https://circleci.com/gh/dwt/fluent
What problem does fluentpy solve
This library is a syntactic sugar library for Python. It allows you to write more things as expressions, which traditionally require statements in Python. The goal is to allow writing beautiful fluent code with the standard library or your classes, as defined at https://en.wikipedia.org/wiki/Fluent_interface.
Fluent is a powerful library, that allows you to use existing libraries through a fluent interface. This is especially useful since most of the Python standard library was written in a way that makes it hard to be used in this style.
fluentpy really usefull to write small Python shell filters, to do something that Python is good at, for example finding stuff with regexes:
$ python3 -m fluentpy "lib.sys.stdin.read().findall(r'(foo|bar)*').print()"
Or whatever other function from the standard lib or any library you would like to use. The Idea here is that while this is perfectly possible without fluent, it is just that little bit easier, to make it actually become fun and practical.
In this context you have basically three extra symbols
wrap is the factory for the object specific wrapper types. Every wrapped object has the fluent behaviour, i.e. every accessed property is also wrapped, while also gaining some type dependent special methods like regex methods on str like
.join(), etc. on list, etc.
lib is a wrapper that allows to use any symbol that is anywhere in the standard library (or accessible via an import) by attribute access. For Example:
import sys sys.stdin.read()
each you probably best think as a convenience lambda generator. It is meant to be a little bit more compact to write down operations you want to execute on every element in a collection.
print(map(lambda x: x * 2, range(1,10)))
wrap(range(1, 10)).map(each * 2).print()
each * 2 is the same as
lambda x: x * 2.
lambda each: each['foo'],
lambda each: each.bar.
lambda each: each.foo('bar') (Sorry about the
.call. there, but I haven’t found a way to get rid of it, pull requests welcome).
I suggest you use
.help() on the objects of this library to quickly get to know what they do.
Usage in short scripts or bigger projects
Just import fluent under the name you would like to use it. For short scripts I prefer
_ but for projects where gettext is used, I prefer
import fluent as _ _(range(10)).map(_.each * 3)
lib are available as symbols on
_, or you can import them directly from fluent
from fluent import wrap as _, lib, each _(range(10)).map(each * 3)
And most important of all: Have phun!