Skip to main content

one, the missing Python function

https://pypip.in/d/one/badge.png https://travis-ci.org/mgaitan/one.svg

Not all nor any: just the one.

one is a simple function to check if there is a unique value that evaluates to True in an iterable, and return it. Optionally, it receives a callable as the test function.

Examples:

>>> from one import one
>>> one((True, False, False))
True
>>> one((True, False, True))
False
>>> one((0, 0, 'a'))
'a'
>>> one((0, False, None))
False
>>> one((True, True))
False
>>> one(('', 1))
1
>>> one((10, 20, 30, 42), lambda i: i > 40)
42

Install

pip install one

Usage

from one import one

Patterns and use cases

one could replace the following patterns:

true_values = [i for i in iterable if cmp(i)]
if len(true_values) == 1:
    return true_values[0]
return False

# using one
return one(iterable, cmp)

Another pattern, is in a complex condition

if ((a and not b and not c) or
     (b and not a and not c) or
      (c and not a and not b)):
    do_something()

# using one
if one((a, b, c)):
    do_something()

A very frequent case is when you have values that must exclude each others.

class ShopStore(models.Model):
    address = models.CharField(max_length=200, null=True, blank=True)
    is_online = models.BooleanField(default=False)

    def clean(self):
        if not one((self.address, self.is_online)):
            raise models.ValidationError(u'A shop must be online or physical, but not both')

Send me your examples!

Metadata

Release files for one 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for one 0.2.1
File Size Uploaded
one-0.2.1.tar.gz 5.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for one 0.2.1
File Interpreter ABI Platform
one-0.2.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 9.2 kB

Release files / one-0.2.1.tar.gz

Download URL one-0.2.1.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4c254c340f838d7f50c99f8fe0e741f6898e4c9f8fed1ae03186e5294374266e
BLAKE2b-256 checksum
How to use checksums
fc1d165a5a030f299aa281801646a8867bc8745eec0441c78c530e78b7ae16c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / one-0.2.1-py2.py3-none-any.whl

Download URL one-0.2.1-py2.py3-none-any.whl
Size 4.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
a82982bc3da1ccf75e81ede365ac9c27630e8d6c21ac1d6055bf7cbe7af8eae4
BLAKE2b-256 checksum
How to use checksums
bd8ae4d72608ff3d97e99261aee7d099a80f1b40243217fe86bf5f1110ea41d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page