Skip to main content

check if a given dictionary is subset of provided predicate

Project description

is_subset

GPLv2 License PyPI Actions Status

is_subset is a Python utility for checking if a given dictionary (dict2) is a subset of another dictionary (dict1). It supports advanced matching features such as nested dictionaries, regular expressions, and conditional operators.

Installation

Install my-project with pip

pip install is_subset

Features

  • Check if one dictionary (dict2) is a subset of another dictionary (dict1).
  • Supports:
    • Exact matches
    • Nested dictionaries
    • Conditional checks like __is, __not, __in, __not_in, __matches, and __not_matches.
  • Works seamlessly with Python primitives like strings, numbers, booleans, and collections.

Usage

You can check if a dictionary is subset of another using

Basic Examples

from is_subset import is_subset

# Simple matching
dict1 = {"age": 30, "name": "John"}

dict2 = {"age": 30}
print(is_subset(dict1, dict2))  # Output: True

dict2 = {"age": 25}
print(is_subset(dict1, dict2))  # Output: False

Advanced Examples

Nested Dictionary Matching

dict1 = {
    "user": {
        "name": "John",
        "details": {
            "age": 30,
            "city": "Metropolis"
        }
    }
}

dict2 = {"user": {"details": {"city": "Metropolis"}}}
print(is_subset(dict1, dict2))  # Output: True

Conditional Matching (__is, __not, __in, __not_in)

# Conditional matching for exact values
dict2 = {"user": {"details": {"city": {"__is": "Metropolis"}}}}
print(is_subset(dict1, dict2))  # Output: True

# Conditional matching for exclusion
dict2 = {"user": {"details": {"city": {"__not": "Gotham"}}}}
print(is_subset(dict1, dict2))  # Output: True

# Conditional matching for inclusion in a list
dict2 = {"user": {"details": {"age": {"__in": [25, 30]}}}}
print(is_subset(dict1, dict2))  # Output: True

# Conditional matching for exclusion in list

dict2 = {"user": {"details": {"age": {"__not_in": [20, 23]}}}}
print(is_subset(dict1, dict2))  # Output: True

Regular Expression Matching (__matches, __not_matches)

dict1 = {"email": "john.doe@example.com"}

# Regex matching
dict2 = {"email": {"__matches": r".*@example\.com"}}
print(is_subset(dict1, dict2))  # Output: True

# Negative regex matching
dict2 = {"email": {"__not_matches": r".*@gmail\.com"}}
print(is_subset(dict1, dict2))  # Output: True

API Reference

is_subset(dict1: dict, dict2: dict) -> bool

Parameters:

  • dict1 (dict): The main dictionary to check against.
  • dict2 (dict): A dictionary specifying the conditions to check.

Returns:

  • bool: True if all conditions in dict2 match the dict1, otherwise False.

Supported Condition Operators:

  • __is: Matches if the value is equal.
  • __not: Matches if the value is not equal.
  • __in: Matches if the value is in a list or collection.
  • __not_in: Matches if the value is not in a list or collection.
  • __matches: Matches if the value satisfies the given regex.
  • __not_matches: Matches if the value does not satisfy the given regex.

Contributing

Contributions are always welcome! In case you have any request or encounter a bug, please create an issue on the repo.

Authors

Project details


Download files

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

Source Distribution

is_subset-0.1.1.tar.gz (42.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

is_subset-0.1.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file is_subset-0.1.1.tar.gz.

File metadata

  • Download URL: is_subset-0.1.1.tar.gz
  • Upload date:
  • Size: 42.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.4

File hashes

Hashes for is_subset-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2d26bd2130c1b7593d1049b27a11301aa9bae5b41a03c84773100b96b73cff18
MD5 6a9f16054214a71cbd6af6c198d310ab
BLAKE2b-256 a5e6148e0c885a4fd10b4e4a5cf2fd18e3c8b3a9a2e8cd414d68a43399fbda5e

See more details on using hashes here.

File details

Details for the file is_subset-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: is_subset-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.4

File hashes

Hashes for is_subset-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bc6a49d8e76c824a9cb5e559f97d7eae90457d12c6ae70e7fe6e2dbb6d5e1687
MD5 765c451479aeaf0b9ae119beffe49876
BLAKE2b-256 fe0b3d4793b411bd1c3860f571dc866532409819158cc9fb95b0634e0850dfdf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page