Code Monkey home page Code Monkey logo

forge's Introduction

forge logo

forge (python) signatures for fun and profit

pypi project MIT license Python 3.5+ master Travis CI Status master Coveralls Status Documentation Status

forge is an elegant Python package for revising function signatures at runtime. This libraries aim is to help you write better, more literate code with less boilerplate.

Installation

forge is a Python-only package hosted on PyPI for Python 3.5+.

The recommended installation method is pip-installing into a virtualenv:

$ pip install python-forge

Example

Consider a library like requests that provides a useful API for performing HTTP requests. Every HTTP method has it's own function which is a thin wrapper around requests.Session.request. The code is a little more than 150 lines, with about 90% of that being boilerplate. Using forge we can get that back down to about 10% it's current size, while increasing the literacy of the code.

import forge
import requests

request = forge.copy(requests.Session.request, exclude='self')(requests.request)

def with_method(method):
    revised = forge.modify(
        'method', default=method, bound=True,
        kind=forge.FParameter.POSITIONAL_ONLY,
    )(request)
    revised.__name__ = method.lower()
    return revised

post = with_method('POST')
get = with_method('GET')
put = with_method('PUT')
delete = with_method('DELETE')
options = with_method('OPTIONS')
head = with_method('HEAD')
patch = with_method('PATCH')

So what happened? The first thing we did was create an alternate request function to replace requests.request that provides the exact same functionality but makes its parameters explicit:

# requests.get() looks like this:
assert forge.repr_callable(requests.get) == 'get(url, params=None, **kwargs)'

# our get() calls the same code, but looks like this:
assert forge.repr_callable(get) == (
    'get(url, params=None, data=None, headers=None, cookies=None, '
        'files=None, auth=None, timeout=None, allow_redirects=True, '
        'proxies=None, hooks=None, stream=None, verify=None, cert=None, '
        'json=None'
    ')'
)

Next, we built a factory function with_method that creates new functions which make HTTP requests with the proper HTTP verb. Because the method parameter is bound, it won't show up it is removed from the resulting functions signature. Of course, the signature of these generated functions remains explicit, let's try it out:

response = get('http://google.com')
assert 'Feeling Lucky' in response.text

You can review the alternate code (the actual implementation) by visiting the code for requests.api.

Project information

forge is released under the MIT license, its documentation lives at Read the Docs, the code on GitHub, and the latest release on PyPI. It’s rigorously tested on Python 3.6+ and PyPy 3.5+.

forge is authored by Devin Fee. Other contributors are listed under https://github.com/dfee/forge/graphs/contributors.

forge's People

Contributors

dfee 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.