📜  shotgun filter any - Python (1)

📅  最后修改于: 2023-12-03 15:05:12.514000             🧑  作者: Mango

Shotgun Filter Any - Python

Shotgun Filter Any is a Python module that provides a simple way to filter lists of dictionaries based on a set of conditions. It allows you to apply multiple filter conditions and returns only the elements that meet the criteria. This module is particularly useful when working with Shotgun, a popular production management tool for the entertainment industry.

Installation

To install Shotgun Filter Any, simply run the following command in your terminal:

pip install shotgun_filter_any
Basic Usage

First, import the filter_any function from the shotgun_filter_any module:

from shotgun_filter_any import filter_any

Next, create a list of dictionaries to filter. Here is an example:

tasks = [
    {"Task": "Task 1", "Status": "In progress", "Assignee": "John"},
    {"Task": "Task 2", "Status": "Not started", "Assignee": "Mary"},
    {"Task": "Task 3", "Status": "Completed", "Assignee": "John"},
    {"Task": "Task 4", "Status": "In progress", "Assignee": "Jack"},
    {"Task": "Task 5", "Status": "Not started", "Assignee": "John"},
    {"Task": "Task 6", "Status": "Completed", "Assignee": "Mary"},
]

Now, define your filter conditions as a list of dictionaries. The keys of these dictionaries represent the fields of each dictionary in the list that you want to filter. The values can be any of the following:

  • A single value
  • A list of values (interpreted as an "OR" condition)
  • A dictionary with one key and value, where the key is a comparison operator ("==", "!=", ">", "<", ">=", "<=") and the value is the comparison value.

Here's an example:

filters = [
    {"Status": "Completed"},
    {"Assignee": ["John", "Mary"]},
    {"Status": {"==": "In progress"}},
]

Finally, apply the filters using the filter_any function:

filtered_tasks = filter_any(tasks, filters)

The resulting filtered_tasks list will contain all tasks that meet any of the filter conditions:

[
    {"Task": "Task 1", "Status": "In progress", "Assignee": "John"},
    {"Task": "Task 3", "Status": "Completed", "Assignee": "John"},
    {"Task": "Task 4", "Status": "In progress", "Assignee": "Jack"},
    {"Task": "Task 6", "Status": "Completed", "Assignee": "Mary"},
]
Additional Features

Shotgun Filter Any also supports the following additional features:

  • Nested fields (e.g. "Project.Name": "Project 1")
  • Case-insensitive string matching (e.g. "Status": {"==i": "in progress"})
  • Regular expression matching (e.g. "Task": {"=~": "^Task [13]$"}")

Refer to the module's documentation for more details on these features.

Conclusion

Shotgun Filter Any provides a simple and flexible way to filter lists of dictionaries based on any set of conditions. Its functionality is particularly useful when dealing with Shotgun data in the entertainment industry.