attrs/README.rst

115 lines
4.3 KiB
ReStructuredText
Raw Normal View History

2017-04-27 14:44:58 +00:00
.. image:: http://www.attrs.org/en/latest/_static/attrs_logo.png
:alt: attrs Logo
==================================
attrs: Classes Without Boilerplate
==================================
2015-01-27 16:53:17 +00:00
2015-12-19 12:04:01 +00:00
.. image:: https://readthedocs.org/projects/attrs/badge/?version=stable
2017-04-27 14:44:58 +00:00
:target: http://www.attrs.org/en/stable/?badge=stable
2015-12-19 12:04:01 +00:00
:alt: Documentation Status
2017-02-20 06:43:48 +00:00
.. image:: https://travis-ci.org/python-attrs/attrs.svg?branch=master
:target: https://travis-ci.org/python-attrs/attrs
:alt: CI Status
2015-01-27 16:53:17 +00:00
2017-02-20 06:43:48 +00:00
.. image:: https://codecov.io/github/python-attrs/attrs/branch/master/graph/badge.svg
:target: https://codecov.io/github/python-attrs/attrs
2017-02-19 08:51:43 +00:00
:alt: Test Coverage
2015-01-27 21:41:24 +00:00
.. teaser-begin
2015-01-27 16:53:17 +00:00
2017-03-24 09:28:28 +00:00
``attrs`` is the Python package that will bring back the **joy** of **writing classes** by relieving you from the drudgery of implementing object protocols (aka `dunder <https://nedbatchelder.com/blog/200605/dunder.html>`_ methods).
2016-08-15 15:24:09 +00:00
Its main goal is to help you to write **concise** and **correct** software without slowing down your code.
2016-08-15 15:24:09 +00:00
.. -spiel-end-
For that, it gives you a class decorator and a way to declaratively define the attributes on that class:
.. -code-begin-
2015-01-27 16:53:17 +00:00
.. code-block:: pycon
>>> import attr
>>> @attr.s
2017-05-22 22:32:17 +00:00
... class SomeClass(object):
... a_number = attr.ib(default=42)
... list_of_numbers = attr.ib(default=attr.Factory(list))
...
2017-05-22 22:32:17 +00:00
... def hard_math(self, another_number):
... return self.a_number + sum(self.list_of_numbers) * another_number
>>> sc = SomeClass(1, [1, 2, 3])
>>> sc
SomeClass(a_number=1, list_of_numbers=[1, 2, 3])
>>> sc.hard_math(3)
19
>>> sc == SomeClass(1, [1, 2, 3])
2015-01-27 16:53:17 +00:00
True
2017-05-22 22:32:17 +00:00
>>> sc != SomeClass(2, [3, 2, 1])
2015-01-27 16:53:17 +00:00
True
2017-05-22 22:32:17 +00:00
>>> attr.asdict(sc)
{'a_number': 1, 'list_of_numbers': [1, 2, 3]}
>>> SomeClass()
SomeClass(a_number=42, list_of_numbers=[])
2017-02-04 10:46:10 +00:00
>>> C = attr.make_class("C", ["a", "b"])
>>> C("foo", "bar")
C(a='foo', b='bar')
2015-01-27 16:53:17 +00:00
2016-08-15 11:59:04 +00:00
After *declaring* your attributes ``attrs`` gives you:
2015-01-27 16:53:17 +00:00
2016-08-15 11:59:04 +00:00
- a concise and explicit overview of the class's attributes,
2015-01-27 16:53:17 +00:00
- a nice human-readable ``__repr__``,
- a complete set of comparison methods,
2015-01-28 15:28:47 +00:00
- an initializer,
- and much more,
2015-01-27 16:53:17 +00:00
2016-08-15 11:59:04 +00:00
*without* writing dull boilerplate code again and again and *without* runtime performance penalties.
2015-01-27 16:53:17 +00:00
This gives you the power to use actual classes with actual types in your code instead of confusing ``tuple``\ s or confusingly behaving ``namedtuple``\ s.
2016-08-15 11:59:04 +00:00
Which in turn encourages you to write *small classes* that do `one thing well <https://www.destroyallsoftware.com/talks/boundaries>`_.
Never again violate the `single responsibility principle <https://en.wikipedia.org/wiki/Single_responsibility_principle>`_ just because implementing ``__init__`` et al is a painful drag.
2015-01-27 16:53:17 +00:00
2016-08-15 11:59:04 +00:00
2016-08-15 15:24:09 +00:00
.. -testimonials-
2016-08-15 11:59:04 +00:00
Testimonials
============
2017-05-23 23:29:32 +00:00
*Im looking forward to is being able to program in Python-with-attrs everywhere.
It exerts a subtle, but positive, design influence in all the codebases Ive see it used in.*
2016-08-15 11:59:04 +00:00
2017-05-23 23:29:32 +00:00
-- **Glyph Lefkowitz**, creator of `Twisted <https://twistedmatrix.com/>`_, `Automat <https://pypi.python.org/pypi/Automat>`_, and other open source software, in `The One Python Library Everyone Needs <https://glyph.twistedmatrix.com/2016/08/attrs.html>`_
2016-08-15 11:59:04 +00:00
2017-05-23 23:29:32 +00:00
*I'm increasingly digging your attr.ocity. Good job!*
2016-08-15 11:59:04 +00:00
2017-05-23 23:29:32 +00:00
-- **Łukasz Langa**, prolific CPython core developer and Production Engineer at Facebook
2016-08-15 11:59:04 +00:00
2017-03-25 06:24:19 +00:00
2017-05-23 23:29:32 +00:00
*Writing a fully-functional class using ``attrs`` takes me less time than writing this testimonial.*
2017-03-25 06:24:19 +00:00
2017-05-23 23:29:32 +00:00
-- **Amber Hawkie Brown**, Twisted Release Manager and Computer Owl
2017-03-25 06:24:19 +00:00
2017-05-23 23:29:32 +00:00
*attrs—classes for humans. I like it.*
2017-05-23 00:56:24 +00:00
2017-05-23 23:29:32 +00:00
-- **Kenneth Reitz**, author of `requests <http://www.python-requests.org/>`_, Python Overlord at Heroku, `on paper no less <https://twitter.com/hynek/status/866817877650751488>`_
2017-05-23 00:56:24 +00:00
2016-08-15 11:59:04 +00:00
.. -end-
.. -project-information-
2016-08-15 11:59:04 +00:00
Project Information
===================
2015-01-27 16:53:17 +00:00
2017-02-20 06:43:48 +00:00
``attrs`` is released under the `MIT <https://choosealicense.com/licenses/mit/>`_ license,
2017-04-27 14:44:58 +00:00
its documentation lives at `Read the Docs <http://www.attrs.org/>`_,
2017-02-20 06:43:48 +00:00
the code on `GitHub <https://github.com/python-attrs/attrs>`_,
2016-08-18 10:21:44 +00:00
and the latest release on `PyPI <https://pypi.org/project/attrs/>`_.
Its rigorously tested on Python 2.7, 3.4+, and PyPy.
2017-04-27 14:44:58 +00:00
If you'd like to contribute you're most welcome and we've written `a little guide <http://www.attrs.org/en/latest/contributing.html>`_ to get you started!