Open In App

gentag Library in Python

Last Updated : 04 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The gentag library in python provides an efficient way for tagging Python objects. These arbitrary Python objects once assigned with a python tag can further be quarried based on the tags. In this article, the focus is on assignment, manipulations, and operations on tagging using python.

Installation:

Use the below command to install the gentag library.

pip install gentag

Defining and Assigning Tags

The tags object needs to assigned using Scope() class of gentag library. Post that each tag can be assigned using define() method, which accepts object name and list of tags to assign to it as the parameter.

Example:

Python3




from gentag import Scope
  
# defining tag object
tags = Scope()
  
# assigning tags
tags.define('gfg', ['cs', 'mock test', 'best'])
tags.define('manjeet', ['loves gfg', 'sde', 'coder', 'problem solver'])
  
print("The tagged instances : ")
print(tags.tags['gfg'])
print(tags.tags['manjeet'])


Output : 

Tagged objects

 Working with operations and ‘all’ tags:

Simple set operations such as &(and), or(|), -(difference) and symmetric difference(^) can be performed over tags using evaluate(). The intersection of all tags can be performed using ‘all’.

Example:

Python3




from gentag import Scope
  
# defining tag object
tags = Scope()
  
# assigning tags
tags.define('gfg', ['cs', 'mock test', 'best'])
tags.define('manjeet', ['loves gfg', 'cs', 'coder', 'problem solver'])
tags.define('cs', ['domain', 'problem solver', 'best'])
  
# evaluating tags
or_res = tags.evaluate('gfg | cs')
print("Getting or on gfg and cs : " + str(or_res))
  
# composite operations
comp_res = tags.evaluate('(gfg & cs) | manjeet ')
print("Getting composition on gfg and cs : " + str(comp_res))
  
# all tags intersection using 'all'
print("Intersection of all tags : " + str(tags.evaluate('all')))


Output : 

Operations on tags

Assigning tags as tags :

Each new tag can be initialized as the composition of a set of tag operations using define(). Just passing tags operations as 2nd parameter.

Example:

Python3




from gentag import Scope
  
# defining tag object
tags = Scope()
  
# assigning tags
tags.define('gfg', ['cs', 'mock test', 'best'])
tags.define('manjeet', ['loves gfg', 'cs', 'coder', 'problem solver'])
tags.define('cs', ['domain', 'problem solver', 'best'])
  
# assigning new tag as composition of others
tags.define('all_good', '(gfg & cs) | manjeet')
print("Getting newly assigned tag : " + str(tags.evaluate('all_good')))


Output : 

New assigned tag.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads