semvec/pypi-packages huggingface dataset:
exttr 2019.2.2
Integrating extra metadata into attr.ib()
License: MIT

exttr
=====

|PyPI| |Pythons| |Travis| |GitHub|

Integrating extra metadata into attr.ib()

.. |PyPI| image:: https://img.shields.io/pypi/v/exttr.svg
   :alt: PyPI version
   :target: https://pypi.org/project/exttr/

.. |Pythons| image:: https://img.shields.io/pypi/pyversions/exttr.svg
   :alt: supported Python versions
   :target: https://pypi.org/project/exttr/

.. |Travis| image:: https://travis-ci.org/altendky/exttr.svg?branch=master
   :alt: Travis build status
   :target: https://travis-ci.org/altendky/exttr

.. |GitHub| image:: https://img.shields.io/github/last-commit/altendky/exttr/master.svg
   :alt: source on GitHub
   :target: https://github.com/altendky/exttr


Example
-------

With a little luck a better example will be provided later but for now, here's something.

A dev in ``#python`` was interested in having click_ build them attrs_-defined configuration objects.
Here's a basic solution for that with the click options being defined on the attrs class attributes via a custom exttr keyword argument ``click=``.

.. _attrs: https://github.com/python-attrs/attrs
.. _click: https://github.com/pallets/click

.. code-block:: python

    import collections
    import sys

    import attr
    import click
    import exttr


    exttr.register_keywords(
        exttr.Keyword(name='click'),
    )


    @attr.s
    class Configuration:
        foo = exttr.ib(click=click.option('--red'))


    def main(configuration):
        print(configuration)

    def clicked_fields(cls):
        fields = collections.OrderedDict()

        for field in attr.fields(cls):
            decorator = exttr.get(cls, field.name, 'click')

            if decorator is None:
                continue

            fields[field.name] = decorator

        return fields


    def build_click(f, cls, command_or_group):
        fields = clicked_fields(cls)

        def cli(*args, **kwargs):
            configuration = cls(*args, **kwargs)

            return f(configuration)

        for name, decorator in reversed(fields.items()):
            before = getattr(cli, '__click_params__', [])

            cli = decorator(cli)

            after = getattr(cli, '__click_params__', [])
            new = after[len(before):]

            if len(new) == 1:
                new, = new
                new.name = name

        return command_or_group(cli)


    click_main = build_click(
        f=main,
        cls=Configuration,
        command_or_group=click.command(),
    )


    sys.argv[1:] = ['--red', 'burgundy']
    try:
        click_main()
    except SystemExit:
        pass

Output:

.. code-block:: python

    Configuration(foo='burgundy')
qubogen 0.1.1
QUBO matrix generator on major combinatorial optimization problems written in Python
License: MIT
Requires Python: >=3.7
Keywords: qubo,combinatorial optimization

# QUBOgen [![Build Status](https://travis-ci.org/tamuhey/qubogen.svg?branch=master)](https://travis-ci.org/tamuhey/qubogen)

- QUBO matrix generator on major combinatorial optimization problems written in Python

# Installation

```bash
$ git clone https://github.com/tamuhey/qubogen
$ cd qubogen
$ pip install -e .
```

# Usage

- See [Number Partitioning example in JupyterNotebook](./notebooks/number_partitioning.ipynb)

# Refs

- [A Tutorial on Formulating and Using QUBO Models](https://arxiv.org/abs/1811.11538)

MIT License

Copyright (c) 2019 tamuhey

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.