json-criteria


Namejson-criteria JSON
Version 0.1.2 PyPI version JSON
download
home_page
SummaryPython library designed for evaluating data against serializable JSON criteria
upload_time2024-01-05 02:40:43
maintainer
docs_urlNone
authorMatthew Fay
requires_python>=3.8
license
keywords adaptable compare comparison conditions criteria data evaluation decision making decision-making decisions dependency-free dynamic adjustment eligibility evaluation flexibility flexible json logic maintainability maintainable nested nested conditions operators persistence rules serializable serialization serialized
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # json-criteria

`json-criteria` is a lightweight, dependency-free Python library designed for evaluating data against serializable JSON criteria. This library supports multiple operators and nested conditions, providing a flexible and powerful tool for condition-based decision-making.

## Why use `json-criteria`?

* **Serialization to JSON**
  * The criteria used by `json-criteria` can be easily serialized to JSON format. This enables the persistence of criteria, making it suitable for storage in databases or external configurations.
* **Dynamic Criteria Adjustment**
  * Since the criteria is stored as JSON, it allows for dynamic adjustments without the need for code deployment. Criteria can be modified or extended as needed, providing the ability to adapt application behavior without code changes.
* **Flexibility and Maintainability**
  * Storing conditions in a serializable format enhances application flexibility and maintainability. Criteria can be fine-tuned, added, or removed independently of the application code, resulting in a more adaptable and easier-to-maintain system.
* **Dependency-Free**
  * `json-criteria` is intentionally designed to be lightweight and dependency-free, ensuring ease of integration into diverse projects without introducing unnecessary dependencies.

In summary, `json-criteria` empowers developers with a versatile and lightweight solution for handling criteria, fostering a more dynamic, adaptable, and maintainable approach to decision-making in applications.

## Examples

### Simple Condition

Evaluate a single condition:

```python
from json_criteria import meets_criteria

# Define a record and criteria
record = {'name': 'Joe', 'age': 30}
criteria = {'key': 'age', 'op': 'equal_to', 'value': 30}

# Check if the record meets the criteria
# `result` will be True
result = meets_criteria(record, criteria)
```

### Nested Conditions

Handle more complex scenarios with nested conditions:

```python
from json_criteria import meets_criteria

# Define a record
record = {
    'name': 'Alice',
    'age': 28,
    'department': 'Engineering',
    'experience_years': 5,
    'is_manager': False
}

# criteria:
# (age >= 25 AND experience_years >= 3) OR (department is 'Engineering' AND is_manager is True)
criteria = {
    'OR': [
        {'AND': [
            {'key': 'age', 'op': 'greater_than_or_equal_to', 'value': 25},
            {'key': 'experience_years', 'op': 'greater_than_or_equal_to', 'value': 3}
        ]},
        {'AND': [
            {'key': 'department', 'op': 'equals', 'value': 'Engineering'},
            {'key': 'is_manager', 'op': 'equals', 'value': True}
        ]}
    ]
}

# Check if the record meets the criteria
# `result` will be True
result = meets_criteria(record, criteria)
```

These examples demonstrate the simplicity and power of `json_criteria` in handling both basic and intricate conditions for evaluating data. The library's support for nested conditions enables the representation of complex logical structures, making it a valuable tool for decision-making in various scenarios.

## Criteria

Criteria in `json-criteria` are represented as dictionaries, specifying the conditions that record(s) must satisfy. This allows for flexible and expressive criteria, supporting both simple conditions and intricate logical structures with AND, OR, and NOT operators.

A condition is composed of three fundamental components:

* `key`: The key to be evaluated.
* `op`: The operator indicating the comparison type.
* `value`: The expected value for the specified key.

Additional keys, such as `id` and `description`, can be included without impacting functionality. This feature provides the flexibility to add context or relevant information to the criteria.

### Supported Operators:

* `equal_to`
* `not_equal_to`
* `greater_than`
* `less_than`
* `greater_than_or_equal_to`
* `less_than_or_equal_to`
* `in`
* `not_in`
* `contains`
* `not_contains`
* `starts_with`
* `not_starts_with`
* `ends_with`
* `not_ends_with`
* `matches_regex`

These operators provide a comprehensive set of comparisons to handle a wide range of conditions in your criteria.

## API

Below you will find documentation for each library function, including examples.

### `all_meet_criteria`

Determine if all records in a given list meet the specified criteria. This function evaluates whether all records in the provided list satisfy the specified criteria, providing a straightforward way to ensure that the entire dataset meets the required conditions.

```python
from json_criteria import all_meet_criteria

# Define a list of user records
user_records = [
    {'name': 'Alice', 'age': 28, 'department': 'Engineering', 'is_manager': False},
    {'name': 'Bob', 'age': 35, 'department': 'Marketing', 'is_manager': True},
    {'name': 'Charlie', 'age': 45, 'department': 'Sales', 'is_manager': False}
]

# criteria: age >= 25 AND (is_manager is True OR department is 'Engineering')
criteria = {
    'AND': [
        {'key': 'age', 'op': 'greater_than_or_equal_to', 'value': 25},
        {'OR': [
            {'key': 'is_manager', 'op': 'equals', 'value': True},
            {'key': 'department', 'op': 'equals', 'value': 'Engineering'}
        ]}
    ]
}

# Check if all user records meet the criteria
# `result` will be True
result = all_meet_criteria(user_records, criteria)
```

### `any_meet_criteria`

This function determines whether any record in the provided list satisfies the specified criteria, offering a convenient way to identify if at least one record meets the required conditions.

```python
from json_criteria import any_meet_criteria

# Define a list of employee records and a criteria
employee_records = [
    {'name': 'Alice', 'age': 32, 'department': 'Sales'},
    {'name': 'Bob', 'age': 45, 'department': 'Marketing'},
    {'name': 'Charlie', 'age': 28, 'department': 'Engineering'},
    {'name': 'David', 'age': 37, 'department': 'Sales'},
]

# criteria: age greater than 35 OR (department is 'Engineering' AND age is less than 30)
criteria = {
    'OR': [
        {'key': 'age', 'op': 'greater_than', 'value': 35},
        {'AND': [
            {'key': 'department', 'op': 'equal_to', 'value': 'Engineering'},
            {'key': 'age', 'op': 'less_than', 'value': 30}
        ]}
    ]
}

# Check if any employee record meets the criteria
# `result` will be True
result = any_meet_criteria(employee_records, criteria)
```

### `apply_using_criteria`

Apply a specified function to each record that meets the given criteria. This function allows for targeted and conditional transformations on a list of records, enhancing flexibility in data manipulation.

```python
from json_criteria import apply_using_criteria

# Define a list of user records and a criteria
user_records = [
    {'name': 'Joe', 'age': 30, 'department': 'Sales'},
    {'name': 'John', 'age': 60, 'department': 'Marketing'},
    {'name': 'Alice', 'age': 25, 'department': 'Engineering'},
]

# criteria: Apply the function to records where age is less than 40 AND belong to the 'Engineering' department
criteria = {
    'AND': [
        {'key': 'age', 'op': 'less_than', 'value': 40},
        {'key': 'department', 'op': 'equal_to', 'value': 'Engineering'}
    ]
}

# Define a function to apply (e.g., mark users as active)
def mark_as_active(record):
    record['is_active'] = True
    return record

# Apply the specified function to records meeting the criteria
# `result` will be:
# [{'name': 'Joe', 'age': 30, 'department': 'Sales'},
#  {'name': 'John', 'age': 60, 'department': 'Marketing'},
#  {'name': 'Alice', 'age': 25, 'department': 'Engineering', 'is_active': True}]
result = apply_using_criteria(user_records, criteria, mark_as_active)
```

### `combine_criteria`

Combine multiple criteria using the AND or OR operator. This function allows for the composition of more complex logical conditions by nesting individual criteria within a larger structure.

```python
from json_criteria import combine_criteria

# Define two individual criteria
criteria1 = {
    'AND': [
        {'key': 'name', 'op': 'starts_with', 'value': 'S'},
        {'key': 'age', 'op': 'equal_to', 'value': 100}
    ]
}

criteria2 = {
    'OR': [
        {'key': 'name', 'op': 'starts_with', 'value': 'L'},
        {'key': 'age', 'op': 'equal_to', 'value': 20}
    ]
}

# Combine criteria using the AND operator
# `result` will be:
# {
#     'AND': [
#         { 'AND': [
#             { 'key': 'name', 'op': 'starts_with', 'value': 'S' }, 
#             { 'key': 'age', 'op': 'equal_to', 'value': 100 }
#             ] 
#         },
#         { 'OR': [
#             { 'key': 'name', 'op': 'starts_with', 'value': 'L' }, 
#             { 'key': 'age', 'op': 'equal_to', 'value': 20 }
#             ]
#         }
#     ]
# }
result = combine_criteria('AND', [criteria1, criteria2])
```

### `filter_using_criteria`

Filter a list of records based on the given criteria. This function provides a powerful mechanism for selectively extracting records that meet specific conditions, enhancing the flexibility of data filtering.

```python
from json_criteria import filter_using_criteria

# Define a list of employee records
employee_records = [
    {'name': 'Alice', 'age': 28, 'department': 'Engineering', 'is_manager': False},
    {'name': 'Bob', 'age': 35, 'department': 'Marketing', 'is_manager': True},
    {'name': 'Charlie', 'age': 45, 'department': 'Sales', 'is_manager': False},
    {'name': 'Susan', 'age': 30, 'department': 'Engineering', 'is_manager': True}
]

# criteria: (age >= 30 AND is_manager is True) OR (department is 'Engineering' AND name starts with 'S')
criteria = {
    'OR': [
        {'AND': [
            {'key': 'age', 'op': 'greater_than_or_equal_to', 'value': 30},
            {'key': 'is_manager', 'op': 'equals', 'value': True}
        ]},
        {'AND': [
            {'key': 'department', 'op': 'equals', 'value': 'Engineering'},
            {'key': 'name', 'op': 'starts_with', 'value': 'S'}
        ]}
    ]
}

# Filter employee records based on the criteria
# `result` will be: [
#    {'name': 'Bob', 'age': 35, 'department': 'Marketing', 'is_manager': True},
#    {'name': 'Susan', 'age': 30, 'department': 'Engineering', 'is_manager': True}
# ]
result = filter_using_criteria(employee_records, criteria)
```

### `find_using_criteria`

Find the first record that meets the given criteria. Returns `None` if none is found. This function provides a convenient way to locate the initial occurrence of a record satisfying specific conditions within a list.

```python
from json_criteria import find_using_criteria

# Define a list of employee records
employee_records = [
    {'name': 'Alice', 'age': 28, 'department': 'Engineering', 'is_manager': False},
    {'name': 'Bob', 'age': 35, 'department': 'Marketing', 'is_manager': True},
    {'name': 'Charlie', 'age': 45, 'department': 'Sales', 'is_manager': False},
    {'name': 'Susan', 'age': 30, 'department': 'Engineering', 'is_manager': True}
]

# criteria: Find the first employee who is a manager and older than 30
criteria = {'AND': [
    {'key': 'is_manager', 'op': 'equals', 'value': True},
    {'key': 'age', 'op': 'greater_than', 'value': 30}
]}

# Find the first employee record meeting the criteria
# `result` will be: {'name': 'Bob', 'age': 35, 'department': 'Marketing', 'is_manager': True}
result = find_using_criteria(employee_records, criteria)
```

### `get_all_criteria`

Given a record and a list of criteria, retrieve all criteria that the record meets. This function is useful for analyzing and identifying the specific criteria that a record satisfies within a given list.

```python
from json_criteria import get_all_criteria

# Define a user
user = {
    'name': 'Alice',
    'age': 28,
    'interests': ['Technology', 'Books'],
    'purchased_products': ['Laptop']
}

# criteria list:
criteria_list = [
    # User has an interest in 'Technology' AND (User is older than 25 OR User has purchased a 'Laptop')
    {'id': 10, 'AND': [
        {'key': 'interests', 'op': 'contains', 'value': 'Technology'},
        {'OR': [
            {'key': 'age', 'op': 'greater_than', 'value': 25},
            {'key': 'purchased_products', 'op': 'contains', 'value': 'Laptop'}
        ]}
    ]},
    # User has an interest in 'Books' AND User has not purchased a 'Tablet'
    {'id': 11, 'AND': [
        {'key': 'interests', 'op': 'contains', 'value': 'Books'},
        {'key': 'purchased_products', 'op': 'not_contains', 'value': 'Tablet'}
    ]}
]

# Get all criteria that the user meets
# `result` will be:
# [{'id': 10, 'AND': [
#     {'key': 'interests', 'op': 'contains', 'value': 'Technology'},
#     {'OR': [
#         {'key': 'age', 'op': 'greater_than', 'value': 25},
#         {'key': 'purchased_products', 'op': 'contains', 'value': 'Laptop'}
#     ]}
# ]}]
result = get_all_criteria(user, criteria_list)
```

### `meets_criteria`

Determine if a record meets the given criteria. This function provides a flexible and expressive way to evaluate whether a record satisfies specified conditions, supporting a variety of operators for comprehensive criteria checks.

```python
from json_criteria import meets_criteria

# Define a record for a user
user_record = {
    'user_type': 1,
    'email': 'test@email.com',
    'is_active': True,
    'age': 30,
    'department': 'Engineering'
}

# Criteria Description:
# - User type is 1
# - AND (Email ends with '@email.com' OR (Is active AND Age is less than 40))
# - AND (Department is 'Engineering' AND (Is active OR Age is greater than or equal to 30))
# - AND (User type is not 2 AND Email does not end with '@test.com')
criteria = {
    'AND': [
        {'key': 'user_type', 'op': 'equal_to', 'value': 1},
        {'OR': [
            {'key': 'email', 'op': 'ends_with', 'value': '@email.com'},
            {'AND': [
                {'key': 'is_active', 'op': 'equal_to', 'value': True},
                {'key': 'age', 'op': 'less_than', 'value': 40}
            ]}
        ]},
        {'AND': [
            {'key': 'department', 'op': 'equal_to', 'value': 'Engineering'},
            {'OR': [
                {'key': 'is_active', 'op': 'equal_to', 'value': True},
                {'key': 'age', 'op': 'greater_than_or_equal_to', 'value': 30}
            ]}
        ]},
        {'AND': [
            {'key': 'user_type', 'op': 'not_equal_to', 'value': 2},
            {'key': 'email', 'op': 'not_ends_with', 'value': '@test.com'}
        ]}
    ]
}

# Check if the user record meets the specified criteria
# `result` will be True
result = meets_criteria(user_record, criteria)
```

## Contributing Guide

I welcome all pull requests. Please make sure you add appropriate test cases for any features added.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "json-criteria",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "adaptable,compare,comparison,conditions,criteria,data evaluation,decision making,decision-making,decisions,dependency-free,dynamic adjustment,eligibility,evaluation,flexibility,flexible,json,logic,maintainability,maintainable,nested,nested conditions,operators,persistence,rules,serializable,serialization,serialized",
    "author": "Matthew Fay",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/c5/c6/74b0969019c7568e26e133b88f01ed8496f32b49104759db35373b0a1af0/json_criteria-0.1.2.tar.gz",
    "platform": null,
    "description": "# json-criteria\n\n`json-criteria` is a lightweight, dependency-free Python library designed for evaluating data against serializable JSON criteria. This library supports multiple operators and nested conditions, providing a flexible and powerful tool for condition-based decision-making.\n\n## Why use `json-criteria`?\n\n* **Serialization to JSON**\n  * The criteria used by `json-criteria` can be easily serialized to JSON format. This enables the persistence of criteria, making it suitable for storage in databases or external configurations.\n* **Dynamic Criteria Adjustment**\n  * Since the criteria is stored as JSON, it allows for dynamic adjustments without the need for code deployment. Criteria can be modified or extended as needed, providing the ability to adapt application behavior without code changes.\n* **Flexibility and Maintainability**\n  * Storing conditions in a serializable format enhances application flexibility and maintainability. Criteria can be fine-tuned, added, or removed independently of the application code, resulting in a more adaptable and easier-to-maintain system.\n* **Dependency-Free**\n  * `json-criteria` is intentionally designed to be lightweight and dependency-free, ensuring ease of integration into diverse projects without introducing unnecessary dependencies.\n\nIn summary, `json-criteria` empowers developers with a versatile and lightweight solution for handling criteria, fostering a more dynamic, adaptable, and maintainable approach to decision-making in applications.\n\n## Examples\n\n### Simple Condition\n\nEvaluate a single condition:\n\n```python\nfrom json_criteria import meets_criteria\n\n# Define a record and criteria\nrecord = {'name': 'Joe', 'age': 30}\ncriteria = {'key': 'age', 'op': 'equal_to', 'value': 30}\n\n# Check if the record meets the criteria\n# `result` will be True\nresult = meets_criteria(record, criteria)\n```\n\n### Nested Conditions\n\nHandle more complex scenarios with nested conditions:\n\n```python\nfrom json_criteria import meets_criteria\n\n# Define a record\nrecord = {\n    'name': 'Alice',\n    'age': 28,\n    'department': 'Engineering',\n    'experience_years': 5,\n    'is_manager': False\n}\n\n# criteria:\n# (age >= 25 AND experience_years >= 3) OR (department is 'Engineering' AND is_manager is True)\ncriteria = {\n    'OR': [\n        {'AND': [\n            {'key': 'age', 'op': 'greater_than_or_equal_to', 'value': 25},\n            {'key': 'experience_years', 'op': 'greater_than_or_equal_to', 'value': 3}\n        ]},\n        {'AND': [\n            {'key': 'department', 'op': 'equals', 'value': 'Engineering'},\n            {'key': 'is_manager', 'op': 'equals', 'value': True}\n        ]}\n    ]\n}\n\n# Check if the record meets the criteria\n# `result` will be True\nresult = meets_criteria(record, criteria)\n```\n\nThese examples demonstrate the simplicity and power of `json_criteria` in handling both basic and intricate conditions for evaluating data. The library's support for nested conditions enables the representation of complex logical structures, making it a valuable tool for decision-making in various scenarios.\n\n## Criteria\n\nCriteria in `json-criteria` are represented as dictionaries, specifying the conditions that record(s) must satisfy. This allows for flexible and expressive criteria, supporting both simple conditions and intricate logical structures with AND, OR, and NOT operators.\n\nA condition is composed of three fundamental components:\n\n* `key`: The key to be evaluated.\n* `op`: The operator indicating the comparison type.\n* `value`: The expected value for the specified key.\n\nAdditional keys, such as `id` and `description`, can be included without impacting functionality. This feature provides the flexibility to add context or relevant information to the criteria.\n\n### Supported Operators:\n\n* `equal_to`\n* `not_equal_to`\n* `greater_than`\n* `less_than`\n* `greater_than_or_equal_to`\n* `less_than_or_equal_to`\n* `in`\n* `not_in`\n* `contains`\n* `not_contains`\n* `starts_with`\n* `not_starts_with`\n* `ends_with`\n* `not_ends_with`\n* `matches_regex`\n\nThese operators provide a comprehensive set of comparisons to handle a wide range of conditions in your criteria.\n\n## API\n\nBelow you will find documentation for each library function, including examples.\n\n### `all_meet_criteria`\n\nDetermine if all records in a given list meet the specified criteria. This function evaluates whether all records in the provided list satisfy the specified criteria, providing a straightforward way to ensure that the entire dataset meets the required conditions.\n\n```python\nfrom json_criteria import all_meet_criteria\n\n# Define a list of user records\nuser_records = [\n    {'name': 'Alice', 'age': 28, 'department': 'Engineering', 'is_manager': False},\n    {'name': 'Bob', 'age': 35, 'department': 'Marketing', 'is_manager': True},\n    {'name': 'Charlie', 'age': 45, 'department': 'Sales', 'is_manager': False}\n]\n\n# criteria: age >= 25 AND (is_manager is True OR department is 'Engineering')\ncriteria = {\n    'AND': [\n        {'key': 'age', 'op': 'greater_than_or_equal_to', 'value': 25},\n        {'OR': [\n            {'key': 'is_manager', 'op': 'equals', 'value': True},\n            {'key': 'department', 'op': 'equals', 'value': 'Engineering'}\n        ]}\n    ]\n}\n\n# Check if all user records meet the criteria\n# `result` will be True\nresult = all_meet_criteria(user_records, criteria)\n```\n\n### `any_meet_criteria`\n\nThis function determines whether any record in the provided list satisfies the specified criteria, offering a convenient way to identify if at least one record meets the required conditions.\n\n```python\nfrom json_criteria import any_meet_criteria\n\n# Define a list of employee records and a criteria\nemployee_records = [\n    {'name': 'Alice', 'age': 32, 'department': 'Sales'},\n    {'name': 'Bob', 'age': 45, 'department': 'Marketing'},\n    {'name': 'Charlie', 'age': 28, 'department': 'Engineering'},\n    {'name': 'David', 'age': 37, 'department': 'Sales'},\n]\n\n# criteria: age greater than 35 OR (department is 'Engineering' AND age is less than 30)\ncriteria = {\n    'OR': [\n        {'key': 'age', 'op': 'greater_than', 'value': 35},\n        {'AND': [\n            {'key': 'department', 'op': 'equal_to', 'value': 'Engineering'},\n            {'key': 'age', 'op': 'less_than', 'value': 30}\n        ]}\n    ]\n}\n\n# Check if any employee record meets the criteria\n# `result` will be True\nresult = any_meet_criteria(employee_records, criteria)\n```\n\n### `apply_using_criteria`\n\nApply a specified function to each record that meets the given criteria. This function allows for targeted and conditional transformations on a list of records, enhancing flexibility in data manipulation.\n\n```python\nfrom json_criteria import apply_using_criteria\n\n# Define a list of user records and a criteria\nuser_records = [\n    {'name': 'Joe', 'age': 30, 'department': 'Sales'},\n    {'name': 'John', 'age': 60, 'department': 'Marketing'},\n    {'name': 'Alice', 'age': 25, 'department': 'Engineering'},\n]\n\n# criteria: Apply the function to records where age is less than 40 AND belong to the 'Engineering' department\ncriteria = {\n    'AND': [\n        {'key': 'age', 'op': 'less_than', 'value': 40},\n        {'key': 'department', 'op': 'equal_to', 'value': 'Engineering'}\n    ]\n}\n\n# Define a function to apply (e.g., mark users as active)\ndef mark_as_active(record):\n    record['is_active'] = True\n    return record\n\n# Apply the specified function to records meeting the criteria\n# `result` will be:\n# [{'name': 'Joe', 'age': 30, 'department': 'Sales'},\n#  {'name': 'John', 'age': 60, 'department': 'Marketing'},\n#  {'name': 'Alice', 'age': 25, 'department': 'Engineering', 'is_active': True}]\nresult = apply_using_criteria(user_records, criteria, mark_as_active)\n```\n\n### `combine_criteria`\n\nCombine multiple criteria using the AND or OR operator. This function allows for the composition of more complex logical conditions by nesting individual criteria within a larger structure.\n\n```python\nfrom json_criteria import combine_criteria\n\n# Define two individual criteria\ncriteria1 = {\n    'AND': [\n        {'key': 'name', 'op': 'starts_with', 'value': 'S'},\n        {'key': 'age', 'op': 'equal_to', 'value': 100}\n    ]\n}\n\ncriteria2 = {\n    'OR': [\n        {'key': 'name', 'op': 'starts_with', 'value': 'L'},\n        {'key': 'age', 'op': 'equal_to', 'value': 20}\n    ]\n}\n\n# Combine criteria using the AND operator\n# `result` will be:\n# {\n#     'AND': [\n#         { 'AND': [\n#             { 'key': 'name', 'op': 'starts_with', 'value': 'S' }, \n#             { 'key': 'age', 'op': 'equal_to', 'value': 100 }\n#             ] \n#         },\n#         { 'OR': [\n#             { 'key': 'name', 'op': 'starts_with', 'value': 'L' }, \n#             { 'key': 'age', 'op': 'equal_to', 'value': 20 }\n#             ]\n#         }\n#     ]\n# }\nresult = combine_criteria('AND', [criteria1, criteria2])\n```\n\n### `filter_using_criteria`\n\nFilter a list of records based on the given criteria. This function provides a powerful mechanism for selectively extracting records that meet specific conditions, enhancing the flexibility of data filtering.\n\n```python\nfrom json_criteria import filter_using_criteria\n\n# Define a list of employee records\nemployee_records = [\n    {'name': 'Alice', 'age': 28, 'department': 'Engineering', 'is_manager': False},\n    {'name': 'Bob', 'age': 35, 'department': 'Marketing', 'is_manager': True},\n    {'name': 'Charlie', 'age': 45, 'department': 'Sales', 'is_manager': False},\n    {'name': 'Susan', 'age': 30, 'department': 'Engineering', 'is_manager': True}\n]\n\n# criteria: (age >= 30 AND is_manager is True) OR (department is 'Engineering' AND name starts with 'S')\ncriteria = {\n    'OR': [\n        {'AND': [\n            {'key': 'age', 'op': 'greater_than_or_equal_to', 'value': 30},\n            {'key': 'is_manager', 'op': 'equals', 'value': True}\n        ]},\n        {'AND': [\n            {'key': 'department', 'op': 'equals', 'value': 'Engineering'},\n            {'key': 'name', 'op': 'starts_with', 'value': 'S'}\n        ]}\n    ]\n}\n\n# Filter employee records based on the criteria\n# `result` will be: [\n#    {'name': 'Bob', 'age': 35, 'department': 'Marketing', 'is_manager': True},\n#    {'name': 'Susan', 'age': 30, 'department': 'Engineering', 'is_manager': True}\n# ]\nresult = filter_using_criteria(employee_records, criteria)\n```\n\n### `find_using_criteria`\n\nFind the first record that meets the given criteria. Returns `None` if none is found. This function provides a convenient way to locate the initial occurrence of a record satisfying specific conditions within a list.\n\n```python\nfrom json_criteria import find_using_criteria\n\n# Define a list of employee records\nemployee_records = [\n    {'name': 'Alice', 'age': 28, 'department': 'Engineering', 'is_manager': False},\n    {'name': 'Bob', 'age': 35, 'department': 'Marketing', 'is_manager': True},\n    {'name': 'Charlie', 'age': 45, 'department': 'Sales', 'is_manager': False},\n    {'name': 'Susan', 'age': 30, 'department': 'Engineering', 'is_manager': True}\n]\n\n# criteria: Find the first employee who is a manager and older than 30\ncriteria = {'AND': [\n    {'key': 'is_manager', 'op': 'equals', 'value': True},\n    {'key': 'age', 'op': 'greater_than', 'value': 30}\n]}\n\n# Find the first employee record meeting the criteria\n# `result` will be: {'name': 'Bob', 'age': 35, 'department': 'Marketing', 'is_manager': True}\nresult = find_using_criteria(employee_records, criteria)\n```\n\n### `get_all_criteria`\n\nGiven a record and a list of criteria, retrieve all criteria that the record meets. This function is useful for analyzing and identifying the specific criteria that a record satisfies within a given list.\n\n```python\nfrom json_criteria import get_all_criteria\n\n# Define a user\nuser = {\n    'name': 'Alice',\n    'age': 28,\n    'interests': ['Technology', 'Books'],\n    'purchased_products': ['Laptop']\n}\n\n# criteria list:\ncriteria_list = [\n    # User has an interest in 'Technology' AND (User is older than 25 OR User has purchased a 'Laptop')\n    {'id': 10, 'AND': [\n        {'key': 'interests', 'op': 'contains', 'value': 'Technology'},\n        {'OR': [\n            {'key': 'age', 'op': 'greater_than', 'value': 25},\n            {'key': 'purchased_products', 'op': 'contains', 'value': 'Laptop'}\n        ]}\n    ]},\n    # User has an interest in 'Books' AND User has not purchased a 'Tablet'\n    {'id': 11, 'AND': [\n        {'key': 'interests', 'op': 'contains', 'value': 'Books'},\n        {'key': 'purchased_products', 'op': 'not_contains', 'value': 'Tablet'}\n    ]}\n]\n\n# Get all criteria that the user meets\n# `result` will be:\n# [{'id': 10, 'AND': [\n#     {'key': 'interests', 'op': 'contains', 'value': 'Technology'},\n#     {'OR': [\n#         {'key': 'age', 'op': 'greater_than', 'value': 25},\n#         {'key': 'purchased_products', 'op': 'contains', 'value': 'Laptop'}\n#     ]}\n# ]}]\nresult = get_all_criteria(user, criteria_list)\n```\n\n### `meets_criteria`\n\nDetermine if a record meets the given criteria. This function provides a flexible and expressive way to evaluate whether a record satisfies specified conditions, supporting a variety of operators for comprehensive criteria checks.\n\n```python\nfrom json_criteria import meets_criteria\n\n# Define a record for a user\nuser_record = {\n    'user_type': 1,\n    'email': 'test@email.com',\n    'is_active': True,\n    'age': 30,\n    'department': 'Engineering'\n}\n\n# Criteria Description:\n# - User type is 1\n# - AND (Email ends with '@email.com' OR (Is active AND Age is less than 40))\n# - AND (Department is 'Engineering' AND (Is active OR Age is greater than or equal to 30))\n# - AND (User type is not 2 AND Email does not end with '@test.com')\ncriteria = {\n    'AND': [\n        {'key': 'user_type', 'op': 'equal_to', 'value': 1},\n        {'OR': [\n            {'key': 'email', 'op': 'ends_with', 'value': '@email.com'},\n            {'AND': [\n                {'key': 'is_active', 'op': 'equal_to', 'value': True},\n                {'key': 'age', 'op': 'less_than', 'value': 40}\n            ]}\n        ]},\n        {'AND': [\n            {'key': 'department', 'op': 'equal_to', 'value': 'Engineering'},\n            {'OR': [\n                {'key': 'is_active', 'op': 'equal_to', 'value': True},\n                {'key': 'age', 'op': 'greater_than_or_equal_to', 'value': 30}\n            ]}\n        ]},\n        {'AND': [\n            {'key': 'user_type', 'op': 'not_equal_to', 'value': 2},\n            {'key': 'email', 'op': 'not_ends_with', 'value': '@test.com'}\n        ]}\n    ]\n}\n\n# Check if the user record meets the specified criteria\n# `result` will be True\nresult = meets_criteria(user_record, criteria)\n```\n\n## Contributing Guide\n\nI welcome all pull requests. Please make sure you add appropriate test cases for any features added.\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Python library designed for evaluating data against serializable JSON criteria",
    "version": "0.1.2",
    "project_urls": {
        "Homepage": "https://github.com/MatthewFay/json-criteria",
        "Issues": "https://github.com/MatthewFay/json-criteria/issues"
    },
    "split_keywords": [
        "adaptable",
        "compare",
        "comparison",
        "conditions",
        "criteria",
        "data evaluation",
        "decision making",
        "decision-making",
        "decisions",
        "dependency-free",
        "dynamic adjustment",
        "eligibility",
        "evaluation",
        "flexibility",
        "flexible",
        "json",
        "logic",
        "maintainability",
        "maintainable",
        "nested",
        "nested conditions",
        "operators",
        "persistence",
        "rules",
        "serializable",
        "serialization",
        "serialized"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "86549d74a7159b91270283e1b3b2d444fc3fa34ac03ccf16a71fed9bd1e12a2e",
                "md5": "af4ecf068644125c3fc0816ab67211b9",
                "sha256": "b4173490ab816de90ce47a929ecf918b4dba8de504a1c8a3983c0a620ca26321"
            },
            "downloads": -1,
            "filename": "json_criteria-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "af4ecf068644125c3fc0816ab67211b9",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 11579,
            "upload_time": "2024-01-05T02:40:41",
            "upload_time_iso_8601": "2024-01-05T02:40:41.436115Z",
            "url": "https://files.pythonhosted.org/packages/86/54/9d74a7159b91270283e1b3b2d444fc3fa34ac03ccf16a71fed9bd1e12a2e/json_criteria-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c5c674b0969019c7568e26e133b88f01ed8496f32b49104759db35373b0a1af0",
                "md5": "c37f998076f835c8345be16d7b0b3140",
                "sha256": "2177a9bf9a2da7794b1b884d86aacb30bf9fdd0012d54a4bf12a4dbdb04c099e"
            },
            "downloads": -1,
            "filename": "json_criteria-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "c37f998076f835c8345be16d7b0b3140",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 10992,
            "upload_time": "2024-01-05T02:40:43",
            "upload_time_iso_8601": "2024-01-05T02:40:43.105367Z",
            "url": "https://files.pythonhosted.org/packages/c5/c6/74b0969019c7568e26e133b88f01ed8496f32b49104759db35373b0a1af0/json_criteria-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-05 02:40:43",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "MatthewFay",
    "github_project": "json-criteria",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "json-criteria"
}
        
Elapsed time: 0.18077s