Skip to main content

A utility package that marks objects and finds them with tags.

Project description

pytagspace

A utility package that marks objects and finds them with tags.

Description

This package is a simple tool to organize different objects by tags. Use TagSpace to tag the objects with tag_name = tag_value pairs.

Note

  • TagSpace Tag keep strong references to the tagged objects and therefore could use large memory if they are not removed explicitly. Use remove_objs to delete them.
  • The tagged objects and tag value must be Hashable.
  • The name of each tag must not start with '_'.
  • Each object can only have one tag value under each tag.

Installation

  • Install via pip
pip install pytagspace

Examples

  • Tag objects and find them
import pytagspace as pts

# tag objects by kwargs
pts.tag(1, 3, 5, 7, 9, odd=True, even=False)
pts.tag(2, 3, 5, 7, prime=True)
pts.tag(5, 9, info='like')
pts.tag(2, 4, info='dislike')

# find objects (return value is a set)
print(pts.find_objs(prime=True, info='like'))
# {5}
print(pts.find_objs(info='love'))
# set()  # Empty

# find objects with value filter
print(pts.find_objs(
  prime=True,
  info=pts.Filter(lambda x: x in ['like', 'dislike'])
))
# {2, 5}
print(pts.find_objs(
  prime=pts.Filter(lambda x: x in [1, 2, True])
))
# {2, 3, 5, 7}
print(pts.find_objs(
  prime=True,
  info=pts.Filter(lambda x: x.endswith('like'))
))
# {2, 5}

# find common tags by objects
print(pts.find_tags(5, 9))
# {'odd': True, 'even': False, 'info': 'like'}
print(pts.find_tags(2))
# {'prime': True, 'info': 'dislike'}
  • Tag functions and classes
from pytagspace import TagSpace

# create a TagSpace instance instead of the default one
space = TagSpace()

# use decorator tag
@space.tag_decorator(return_value=1, is_function=True)
def func1():
    return 1

@space.tag_decorator(return_value=1, is_function=False)
class C1:
    def __call__(self):
        return 1

# use normal tag
def func2():
    return 2
class C2:
    def __call__(self):
        return 2
space.tag(func2, return_value=2, is_function=True)
space.tag(C2, return_value=2, is_function=False)

# find objects
for f in space.find_objs(return_value=1):
    print(f())
# 1, <class C1>
for f in space.find_objs(is_function=False):
    print(f()())
# 1, 2
  • Tag removal
import pytagspace as pts
from datetime import timedelta
from dataclasses import dataclass


@dataclass
class Movie:
  name: str
  duration: timedelta
  year: int


movies = [
  Movie(
    name='Thunder Force',
    duration=timedelta(hours=1, minutes=45),
    year=2021
  ),
  Movie(
    name='Once Upon A Time...In Hollywood',
    duration=timedelta(hours=2, minutes=40),
    year=2019
  ),
  Movie(
    name='Run',
    duration=timedelta(hours=1, minutes=39),
    year=2020
  )
]

for movie in movies:
  pts.tag(movie, duration=movie.duration, year=movie.year)

print(pts.find_objs(
  duration=pts.Filter(lambda x: x < timedelta(hours=2))
))
# Movies under 2 hours: Thunder Force and Run

pts.remove_objs(movies[0])
print(pts.find_objs(
  duration=pts.Filter(lambda x: x < timedelta(hours=2))
))
# Movies under 2 hours: Run

pts.remove_tags('duration', year=2019)
print(pts.find_objs(
  duration=pts.Filter(lambda x: x < timedelta(hours=2))
))
# set()  # Empty set

print(pts.find_objs(year=2019))
# set() # Empty set

Future improvements

  • Store objects using weakref
  • Store objects that is not hashable(find_tags and remove_objs will be unavailable)

Update notes

1.0.1

  • Rename TagValueFilter to Filter for convenience (TagValueFilter is kept for backward compatibility)
  • Update examples in README.md

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

pytagspace-1.0.1.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

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

pytagspace-1.0.1-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file pytagspace-1.0.1.tar.gz.

File metadata

  • Download URL: pytagspace-1.0.1.tar.gz
  • Upload date:
  • Size: 18.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.8

File hashes

Hashes for pytagspace-1.0.1.tar.gz
Algorithm Hash digest
SHA256 9a98b17478872df77c61ecb7eaabd911136df46db532b7c54ce71b50dc39e1ee
MD5 a3e3f411c414346ec23bad4c8dccb474
BLAKE2b-256 51aa907de2a1d8fd07aa61cf7cd70eed1a178bced6d64e05254d1d0fdc959eb4

See more details on using hashes here.

File details

Details for the file pytagspace-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pytagspace-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.8

File hashes

Hashes for pytagspace-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 faa6ba07b9cd75996f141f5ad1c5639f7a134ab17f7bac3e6b69fc3300bf3888
MD5 126beca98332697cad19f659a4a582fd
BLAKE2b-256 85f3f357eebc61127039d18d783585127387e7b304b7231500e6721acd9435e8

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