Skip to content

Commit

Permalink
Renamed some classes to match PEP8 conventions. This will never affec…
Browse files Browse the repository at this point in the history
…t range types such as intrange that map to classes in the standard library that does not follow the PEP8 naming convention
  • Loading branch information
runfalk committed Mar 21, 2017
1 parent eff6e46 commit e7f9b16
Show file tree
Hide file tree
Showing 9 changed files with 271 additions and 141 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Spans
|test-status| |test-coverage| |documentation-status| |pypi-version| |py-versions| |license|

Spans is a pure Python implementation of PostgreSQL's
`range types <http://www.postgresql.org/docs/9.2/static/rangetypes.html>`_.
`range types <http://www.postgresql.org/docs/9.6/static/rangetypes.html>`_.
Range types are conveinent when working with intervals of any kind. Every time
you've found yourself working with date_start and date_end, an interval may have
been what you were actually looking for.
Expand Down
62 changes: 48 additions & 14 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,61 @@ API documentation
=================
This is the API reference for all public classes and functions in Spans.


Ranges
------

Range class
~~~~~~~~~~~
The ``range_`` class is the base for all ranges. Since this is an abstract class all examples uses the :class:`.intrange` sub class. The name ``range_`` with the trailing underscore is used to prevent overwriting the built in ``range`` (in compliance with :pep:`8`.)

.. autoclass:: spans.types.range_
.. autoclass:: spans.types.Range
:members:


Discrete range
~~~~~~~~~~~~~~
.. autoclass:: spans.types.discreterange
.. autoclass:: spans.types.DiscreteRange
:members:

Offsetable range
~~~~~~~~~~~~~~~~
.. autoclass:: spans.types.offsetablerange

Offsetable range mixin
~~~~~~~~~~~~~~~~~~~~~~
.. autoclass:: spans.types.OffsetableRangeMixin
:members: offset


Integer range
~~~~~~~~~~~~~
.. autoclass:: spans.types.intrange


Float range
~~~~~~~~~~~
.. autoclass:: spans.types.floatrange


String range
~~~~~~~~~~~~
.. autoclass:: spans.types.strrange


Date range
~~~~~~~~~~
.. autoclass:: spans.types.daterange
:members:
:special-members: __len__


Period range
~~~~~~~~~~~~
.. autoclass:: spans.types.PeriodRange
:members:


Datetime range
~~~~~~~~~~~~~~
.. autoclass:: spans.types.datetimerange


Timedelta range
~~~~~~~~~~~~~~~
.. autoclass:: spans.types.timedeltarange
Expand All @@ -60,40 +69,65 @@ Range sets

Range set
~~~~~~~~~
.. autoclass:: spans.settypes.rangeset
.. autoclass:: spans.settypes.RangeSet
:members:
:special-members: __iter__, __len__

Discrete range set
~~~~~~~~~~~~~~~~~~
.. autoclass:: spans.settypes.discreterangeset

Discrete range set mixin
~~~~~~~~~~~~~~~~~~~~~~~~
.. autoclass:: spans.settypes.DiscreteRangeSetMixin
:members: values

Offsetable range set
~~~~~~~~~~~~~~~~~~~~
.. autoclass:: spans.settypes.offsetablerangeset

Offsetable range set mixin
~~~~~~~~~~~~~~~~~~~~~~~~~~
.. autoclass:: spans.settypes.OffsetableRangeSetMixin
:members: offset


Integer range set
~~~~~~~~~~~~~~~~~
.. autoclass:: spans.settypes.intrangeset


Float range set
~~~~~~~~~~~~~~~
.. autoclass:: spans.settypes.floatrangeset


String range set
~~~~~~~~~~~~~~~~
.. autoclass:: spans.settypes.strrangeset


Date range set
~~~~~~~~~~~~~~
.. autoclass:: spans.settypes.daterangeset


Datetime range set
~~~~~~~~~~~~~~~~~~
.. autoclass:: spans.settypes.datetimerangeset


Timedelta range set
~~~~~~~~~~~~~~~~~~~
.. autoclass:: spans.settypes.timedeltarangeset


Meta range set
~~~~~~~~~~~~~~
.. autoclass:: spans.settypes.MetaRangeSet



Legacy names
------------
Historically some internal Spans classes had all lowercase names. This was changed in version 0.5.0. The reason some classes still have lowercase names is to match the Python built-ins they map to. ``date``'s range type is and will always be :class:`~spans.types.daterange`. However, it doesn't make much sense to maintain this convention for the more hidden classes in Spans.

.. automodule:: spans.types
:members: range_, discreterange, offsetablerange

.. automodule:: spans.settypes
:members: metarangeset, rangeset
11 changes: 11 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ Version are structured like the following: ``<major>.<minor>.<bugfix>``. The
first `0.1` release does not properly adhere to this. Unless explicitly stated,
changes are made by `Andreas Runfalk <https://github.com/runfalk>`_.


Version 0.5.0
-------------
Released on <unreleased>

This release is a preparation for a stable 1.0 release.

- Renamed classes to match :pep:`8#class-names` conventions. This does not apply
to classes that works on built-in that does not follow :pep:`8#class-names`.


Version 0.4.0
-------------
Released on 20th March, 2017
Expand Down
4 changes: 3 additions & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import os
import shlex

from datetime import date

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
Expand Down Expand Up @@ -50,7 +52,7 @@

# General information about the project.
project = u"Spans"
copyright = u"2015, Andreas Runfalk"
copyright = u"{.year}, Andreas Runfalk".format(date.today())
author = u"Andreas Runfalk"

# The version info for the project you're documenting, acts as replacement for
Expand Down
37 changes: 21 additions & 16 deletions doc/custom_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ Custom range types
==================
The built in range types may not suffice for your particular application. It is very easy to extend with your own classes. The only requirement is that the type supports rich comparison :pep:`207` and is immutable.


Standard range types
--------------------
A normal range can be implemented by extending :class:`spans.types.range_`.
A normal range can be implemented by extending :class:`spans.types.Range`.

.. code-block:: python
from spans.types import range_
from spans.types import Range
class floatrange(range_):
class floatrange(Range):
__slots__ = ()
type = float
Expand All @@ -22,26 +23,27 @@ A normal range can be implemented by extending :class:`spans.types.range_`.
.. note::
The ``__slots__ = ()`` is a performance optimization that is used for all ranges. It lowers the memory footprint for every instance. It is not mandatory but encourgaged.


Offsetable range types
----------------------
An offsetable range can be implemented using the mixin :class:`spans.types.offsetablerange`. The class still needs to extend :class:`spans.types.range_`.
An offsetable range can be implemented using the mixin :class:`spans.types.OffsetableRangeMixin`. The class still needs to extend :class:`spans.types.Range`.

.. code-block:: python
from spans.types import range_, offsetablerange
from spans.types import Range, OffsetableRangeMixin
class floatrange(range_, offsetablerange):
class floatrange(Range, OffsetableRangeMixin):
__slots__ = ()
type = float
If the offset type is not the same as the range type (such as ``date`` that is offsetable with ``timedelta``) the attribute ``offset_type`` can be used.

.. code-block:: python
from spans.types import discreterange, offsetablerange
from spans.types import DiscreteRange, OffsetableRangeMixin
from datetime import date, timedelta
class daterange(discreterange, offsetablerange):
class daterange(DiscreteRange, OffsetableRangeMixin):
__slots__ = ()
type = date
Expand All @@ -50,39 +52,42 @@ If the offset type is not the same as the range type (such as ``date`` that is o
span = daterange(date(2000, 1, 1), date(2000, 2, 1))
assert span.offset(timedelta(14)).upper == date(2000, 2, 15)
Discrete range types
--------------------
Discrete ranges (such as :class:`~spans.types.intrange` and :class:`~spans.types.daterange`) can be implemented by extending :class:`spans.types.discreterange`.
Discrete ranges (such as :class:`~spans.types.intrange` and :class:`~spans.types.daterange`) can be implemented by extending :class:`spans.types.DiscreteRange`.

.. code-block:: python
from spans.types import discreterange, offsetablerange
from spans.types import DiscreteRange, OffsetableRangeMixin
class intrange(discreterange, offsetablerange):
class intrange(DiscreteRange, OffsetableRangeMixin):
__slots__ = ()
type = intrange
step = 1
assert list(intrange(1, 5)) == [1, 2, 3, 4]
Note the `step` attribute. It must always be the smallest possible unit. Using `2` for intranges would not have expected behavior.
Note the ``step`` attribute. It must always be the smallest possible unit. Using ``2`` for intranges would not have expected behavior.


Range sets
----------
Range sets are conveinient to implement regardless of the mixins used. This is due to the metaclass
Range sets are conveinient to implement regardless of the mixins used. This is due to the metaclass :class:`spans.settypes.MetaRangeSet`. The metaclass automatically adds required mixins to the range set type.

.. code-block:: python
from spans.types import intrange
from spans.settypes import rangeset
from spans.settypes import RangeSet
class intrangeset(rangetset):
class intrangeset(RangeSet):
__slots__ = ()
type = intrange
assert intrangeset(
[intrange(1, 5), intrange(10, 15)]).span() == intrange(1, 15)
Custom mixins
-------------
It is possible to create custom mixins for range sets by adding mappings to :class:`spans.settypes.metarangeset`. The mapping has to be added before the range set class is created or it will not be used.
It is possible to create custom mixins for range sets by adding mappings to :class:`spans.settypes.MetaRangeSet`. The mapping has to be added before the range set class is created or it will not be used.
7 changes: 4 additions & 3 deletions doc/index.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Welcome to Spans' documentation!
================================
Spans |release| documentation
=============================
Spans is a pure Python implementation of PostgreSQL's
`range types <http://www.postgresql.org/docs/9.2/static/rangetypes.html>`_.
`range types <http://www.postgresql.org/docs/9.6/static/rangetypes.html>`_.
Range types are conveinent when working with intervals of any kind. Every time
you've found yourself working with date_start and date_end, an interval may have
been what you were actually looking for.
Expand Down Expand Up @@ -59,6 +59,7 @@ Introduction
.. toctree::
:maxdepth: 2
self
introduction
ranges
custom_types
Expand Down
2 changes: 1 addition & 1 deletion spans/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"""

__version__ = "0.4.1"
__version__ = "0.5.0"
__all__ = [
"intrange",
"floatrange",
Expand Down

0 comments on commit e7f9b16

Please sign in to comment.