{"id":18084854,"url":"https://github.com/jwodder/derange","last_synced_at":"2025-08-01T15:11:21.161Z","repository":{"id":62567737,"uuid":"106202746","full_name":"jwodder/derange","owner":"jwodder","description":"Compress lists of integers to range objects","archived":false,"fork":false,"pushed_at":"2025-07-28T16:08:40.000Z","size":82,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-07-28T18:14:01.593Z","etag":null,"topics":["available-on-pypi","compression","connected-component","connected-components","consecutive","distribution-sort","gaps-and-islands","interval","python","range","sequential","sorting","sparse-array","streak"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jwodder.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-08T19:15:00.000Z","updated_at":"2025-07-28T16:08:43.000Z","dependencies_parsed_at":"2023-01-31T01:55:12.968Z","dependency_job_id":"85e7ee8f-987a-4e41-8049-87d698d94a03","html_url":"https://github.com/jwodder/derange","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/jwodder/derange","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwodder%2Fderange","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwodder%2Fderange/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwodder%2Fderange/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwodder%2Fderange/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jwodder","download_url":"https://codeload.github.com/jwodder/derange/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwodder%2Fderange/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267563185,"owners_count":24108036,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-28T02:00:09.689Z","response_time":68,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["available-on-pypi","compression","connected-component","connected-components","consecutive","distribution-sort","gaps-and-islands","interval","python","range","sequential","sorting","sparse-array","streak"],"created_at":"2024-10-31T15:08:26.449Z","updated_at":"2025-08-01T15:11:21.143Z","avatar_url":"https://github.com/jwodder.png","language":"Python","readme":"|repostatus| |ci-status| |coverage| |pyversions| |license|\n\n.. |repostatus| image:: https://www.repostatus.org/badges/latest/active.svg\n    :target: https://www.repostatus.org/#active\n    :alt: Project Status: Active — The project has reached a stable, usable\n          state and is being actively developed.\n\n.. |ci-status| image:: https://github.com/jwodder/derange/actions/workflows/test.yml/badge.svg\n    :target: https://github.com/jwodder/derange/actions/workflows/test.yml\n    :alt: CI Status\n\n.. |coverage| image:: https://codecov.io/gh/jwodder/derange/branch/master/graph/badge.svg\n    :target: https://codecov.io/gh/jwodder/derange\n\n.. |pyversions| image:: https://img.shields.io/pypi/pyversions/derange.svg\n    :target: https://pypi.org/project/derange\n\n.. |license| image:: https://img.shields.io/github/license/jwodder/derange.svg\n    :target: https://opensource.org/licenses/MIT\n    :alt: MIT License\n\n`GitHub \u003chttps://github.com/jwodder/derange\u003e`_\n| `PyPI \u003chttps://pypi.org/project/derange\u003e`_\n| `Issues \u003chttps://github.com/jwodder/derange/issues\u003e`_\n| `Changelog \u003chttps://github.com/jwodder/derange/blob/master/CHANGELOG.md\u003e`_\n\nDo you have a list of integers?  Do you want to know what ranges of consecutive\nvalues the list covers?  Do you need to solve a `gaps and islands\n\u003chttps://stackoverflow.com/tags/gaps-and-islands/info\u003e`_ problem outside of\nSQL?  Maybe you have a list of dates and need to find the longest streak of\nconsecutive days on which something happened.  No?  Why not?  Well, either way,\nthe ``derange`` module is here for you, ready to solve all these problems and a\ncouple more.\n\n\nInstallation\n============\n``derange`` requires Python 3.8 or higher.  Just use `pip\n\u003chttps://pip.pypa.io\u003e`_ for Python 3 (You have pip, right?) to install it::\n\n    python3 -m pip install derange\n\n\nExamples\n========\nCondense commit years obtained from ``git log`` or the like into ``range``\nobjects:\n\n\u003e\u003e\u003e import derange\n\u003e\u003e\u003e derange.derange([2015, 2015, 2015, 2014, 2014, 2011, 2010, 2010, 2009, 2009])\n[range(2009, 2012), range(2014, 2016)]\n\nIf the input is already sorted, you can condense it slightly faster with\n``derange_sorted()``:\n\n\u003e\u003e\u003e derange.derange_sorted([2009, 2009, 2010, 2010, 2011, 2014, 2014, 2015, 2015, 2015])\n[range(2009, 2012), range(2014, 2016)]\n\nOrganize non-integer values into closed intervals (represented as pairs of\nendpoints) with ``deinterval()``:\n\n\u003e\u003e\u003e import datetime\n\u003e\u003e\u003e # deinterval() requires a callable for determining when two values are \"adjacent\":\n\u003e\u003e\u003e def within_24_hours(a,b):\n...     return abs(a-b) \u003c= datetime.timedelta(hours=24)\n...\n\u003e\u003e\u003e timestamps = [\n...     datetime.datetime(2017, 11, 2, 12, 0),\n...     datetime.datetime(2017, 11, 3, 11, 0),\n...     datetime.datetime(2017, 11, 4, 10, 0),\n...     datetime.datetime(2017, 11, 5,  9, 0),\n...     datetime.datetime(2017, 11, 6,  9, 0),\n...     datetime.datetime(2017, 11, 7, 10, 0),\n... ]\n\u003e\u003e\u003e derange.deinterval(within_24_hours, timestamps)\n[(datetime.datetime(2017, 11, 2, 12, 0), datetime.datetime(2017, 11, 6, 9, 0)), (datetime.datetime(2017, 11, 7, 10, 0), datetime.datetime(2017, 11, 7, 10, 0))]\n\n… which also has a ``deinterval_sorted()`` variant:\n\n\u003e\u003e\u003e derange.deinterval_sorted(within_24_hours, timestamps)\n[(datetime.datetime(2017, 11, 2, 12, 0), datetime.datetime(2017, 11, 6, 9, 0)), (datetime.datetime(2017, 11, 7, 10, 0), datetime.datetime(2017, 11, 7, 10, 0))]\n\u003e\u003e\u003e derange.deinterval_sorted(within_24_hours, reversed(timestamps))\nTraceback (most recent call last):\n    ...\nValueError: sequence not in ascending order\n\n\nAPI\n===\n\n.. code:: python\n\n    derange.derange(iterable: Iterable[int]) -\u003e List[range]\n\nConvert a sequence of integers to a minimal list of ``range`` objects that\ntogether contain all of the input elements.\n\nOutput is in strictly ascending order.  Input need not be in order (but see\nalso ``derange_sorted()``).  Duplicate input values are ignored.\n\n.. code:: python\n\n    derange.derange_sorted(iterable: Iterable[int]) -\u003e List[range]\n\nConvert a *non-decreasing* sequence of integers to a minimal list of ``range``\nobjects that together contain all of the input elements.  This is faster than\n``derange()`` but only accepts sorted input.\n\n.. code:: python\n\n    derange.deinterval(\n        adjacent: Callable[[T,T], bool],\n        iterable: Iterable[T],\n    ) -\u003e List[Tuple[T,T]]\n\nConvert a sequence of totally-ordered values to a minimal list of closed\nintervals (represented as pairs of endpoints) that together contain all of the\ninput elements.  This is a generalization of ``derange()`` for arbitrary types.\n\nTwo input values will be placed in the same interval iff they are directly\nadjacent or there exists a chain of adjacent input values connecting them,\nwhere adjacency is defined by the given ``adjacent`` callable.\n\n``adjacent`` will be called with two elements of ``iterable`` at a time to test\nwhether they should be placed in the same interval.  The binary relation\nimplied by ``adjacent`` must be reflexive and symmetric, and for all ``x \u003c y \u003c\nz``, if ``adjacent(x, z)`` is true, then both ``adjacent(x, y)`` and\n``adjacent(y, z)`` must also be true.\n\nOutput is in strictly ascending order.  Input need not be in order (but see\nalso ``deinterval_sorted()``).  Duplicate input values are ignored.\n\nNote that, unlike with ``range`` objects, intervals returned from\n``deinterval()`` contain their upper bounds.\n\n.. code:: python\n\n    derange.deinterval_sorted(\n        adjacent: Callable[[T,T], bool],\n        iterable: Iterable[T],\n    ) -\u003e List[Tuple[T,T]]\n\nConvert a *non-decreasing* sequence of totally-ordered values to a minimal list\nof closed intervals that together contain all of the input elements.  This is\nfaster than ``deinterval()`` but only accepts sorted input.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwodder%2Fderange","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjwodder%2Fderange","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwodder%2Fderange/lists"}