Skip to main content

A library for filtering things based on tags

Project description

alpacloud.eztag

eztag helps you easily filter things by tags.

Usage

Filtering in code

You can directly invoke the filters as functions. For example:

from alpacloud.eztag.tag import TagSet

@dataclass
class Snippet:
    name: str
    content: str
    tags: TagSet

you can filter them with convenient syntax:

filter(lambda s: s.tags.has("python"), snippets)

There are several filter functions available:

  • has : check if a TagSet has a tag
  • match : check if a TagSet has a key with a value
  • rematch : check if a TagSet has a key with a value that matches a regex
  • contains : check if a TagSet has a key whose value contains a substring

Filtering external data

If your objects don't have tags, you can associate tag data with them using Selector:

from alpacloud.eztag.logic import TagMatch
from alpacloud.eztag.selector import Selector
from alpacloud.eztag.tag import TagSet

tasks = Selector([
    (TagSet.from_dict({"env":"prd", "dangerous": "true"}), task0),
    (TagSet.from_dict({"env":"stg", "dangerous": "false"}), task1),
])

dangerous_tasks = tasks.select(TagMatch("dangerous", "true"))

Filtering from the CLI

eztag allows you to input filters in a simple language from the CLI. You can build this filtering into your own tools.

import click

from alpacloud.eztag.parser import Parser, transformer
from alpacloud.eztag.selector import Selector

tagged_tasks = Selector(...)

@click.command()
@click.argument("filter")
def cli(filter):
    expr = transformer.transform(Parser(filter).parse())
    selected_tasks = tagged_tasks.select(expr)

    for task in selected_tasks:
        task.run()

Then you can invoke it like this:

task-run --filter 'and(match(env, prd), re(name, /cert.*/)'

CLI filter usage

Filter syntax is:

regex_literal := "/" regex "/"
string_literal := any characters except "(),/" and spaces
expr := identifier(expr [, expr])* | regex_literal | string_literal
identifier := "and" | "or" | "not" | "has" | "match" | "re" | "contains"

the operations are:

  • and : logical AND
  • or : logical OR
  • not : logical NOT
  • has : check if a tag has a key
  • match : check if a tag has a key with a value
  • re : check if a tag has a key with a value that matches a regex
  • contains : check if a tag has a key whose value contains a substring

Advanced usage

Adding custom filters or operators

You can add your own filters or operators to streamline your usecase. For example, if you shard your tasks, you can add a filter to run only on a specific shard and then filter with AND(MATCH(env, prd), SHARD(5)) for example.

  1. Implement your filter as a subclass of alpacloud.eztag.logic.Expr

    from dataclasses import dataclass
    from alpacloud.eztag.logic import Expr
    
    @dataclass(frozen=True)
    class Shard(Expr):
        """Run tasks with this shard identifier"""
        shard: int
    
        def check(self, tags) -> bool:
            return tags.contains("shard", str(self.shard))
    
  2. Register your filter in the transformer dictionary:

    from alpacloud.eztag.transformer import transformer, TokenTransformer, TokenTransformation
    
    my_transformer = transformer.extended({
        "SHARD": TokenTransformation("SHARD", Shard, args=["shard"]),
    })
    

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

alpacloud_eztag-0.1.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

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

alpacloud_eztag-0.1.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file alpacloud_eztag-0.1.0.tar.gz.

File metadata

  • Download URL: alpacloud_eztag-0.1.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for alpacloud_eztag-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e5ce37cc06501fddb8f186b2610d95b3ad62c3abddd08b28726561e8a0da95d9
MD5 df82c522ad7946d156f8c4521907d734
BLAKE2b-256 7d1b77edf2c162bfd0bf5abb9527f0da9300d087f4a95490caebf516d716256d

See more details on using hashes here.

File details

Details for the file alpacloud_eztag-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for alpacloud_eztag-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 82398200e806677e7418fc61dfea3a72ba65e6a79dc12086b00b0feb82d737ca
MD5 be6174cf8910375b058cf9d4002fc81f
BLAKE2b-256 db7ae0efb9512ff985f90dd292ad01cc70ad208cf9012893a7a1ae102657b8e8

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