Skip to main content

Not all nor any. Just the one.

Project description

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.


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


pip install one


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)):

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

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!

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for one, version 0.2.1
Filename, size File type Python version Upload date Hashes
Filename, size one-0.2.1-py2.py3-none-any.whl (4.2 kB) File type Wheel Python version 2.7 Upload date Hashes View
Filename, size one-0.2.1.tar.gz (5.0 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page