1- # ## Return all tags
2- # client.tags.find() -> list[Tag]
3-
4- # ## Return all tags with name and parent
5- # client.tags.find(name="tag_name", parent="parent_tag_guid" | parent_tag | None)
6-
7- # # Create Tag
8- # mytag = client.tags.create(
9- # name="tag_name",
10- # parent="parent_tag_guid" | parent_tag | None
11- # ) -> Tag
12-
13- # # Delete Tag
14-
15- # mytag = client.tags.get("tag_guid")
16- # mytag.destroy() -> None
17-
18-
19- # # Find content using tags
20- # mycontentitems = mytag.content.find() -> list[ContentItem]
21-
22- # # Get content item's tags
23- # mycontentitem: ContentItem = mycontentitems[0]
24- # mycontentitem.tags.find() -> list[Tag]
251from __future__ import annotations
262
273from typing import TYPE_CHECKING , Optional
@@ -200,81 +176,13 @@ def find(self) -> list[Tag]:
200176 return child_tags
201177
202178
203- # def _unique_content_items_for_tags(tags: list[Tag]) -> list[ContentItem]:
204- # tag_content_items: list[ContentItem] = []
205- # content_item_seen = set[str]()
206- # for descendant_tag in tags:
207- # for content_item in descendant_tag.content_items.find():
208- # if content_item["guid"] not in content_item_seen:
209- # tag_content_items.append(content_item)
210- # content_item_seen.add(content_item["guid"])
211- # return tag_content_items
212-
213-
214- # class ChildrenTagsContentItems(ContextManager):
215- # def __init__(self, ctx: Context, path: str, /, *, parent_tag: Tag) -> None:
216- # super().__init__()
217- # self._ctx = ctx
218- # self._path = path
219- # self._parent_tag = parent_tag
220-
221- # def find(self) -> list[ContentItem]:
222- # """
223- # Find all unique content items that are tagged with a tag or any of the tag's descendants.
224-
225- # Returns
226- # -------
227- # list[ContentItem]
228- # List of content items that contain a tag or any of its descendant tags.
229- # """
230- # descendant_tags = ChildrenTags(
231- # self._ctx,
232- # self._path,
233- # parent_tag=self._parent_tag,
234- # ).find()
235- # return _unique_content_items_for_tags([self._parent_tag, *descendant_tags])
236-
237-
238- # class DescendantTagsContentItems(ContextManager):
239- # def __init__(self, ctx: Context, path: str, /, *, parent_tag: Tag) -> None:
240- # super().__init__()
241- # self._ctx = ctx
242- # self._path = path
243- # self._parent_tag = parent_tag
244-
245- # def find(self) -> list[ContentItem]:
246- # """
247- # Find all unique content items that are tagged with a tag or any of the tag's descendants.
248-
249- # Returns
250- # -------
251- # list[ContentItem]
252- # List of content items that contain a tag or any of its descendant tags.
253- # """
254- # descendant_tags = DescendantTags(self._ctx, parent_tag=self._parent_tag).find()
255- # return _unique_content_items_for_tags([self._parent_tag, *descendant_tags])
256-
257-
258179class DescendantTags (ContextManager ):
259180 def __init__ (self , ctx : Context , / , * , parent_tag : Tag ) -> None :
260181 super ().__init__ ()
261182 self ._ctx = ctx
262183 self ._path = "v1/tags"
263184 self ._parent_tag = parent_tag
264185
265- # @property
266- # def content_items(self) -> DescendantTagsContentItems:
267- # """
268- # Find all unique content items that are tagged with a tag or any of the tag's descendants.
269-
270- # Returns
271- # -------
272- # DescendantTagsContentItems
273- # Helper class that can `.find()` all content items that contain a tag or any of its
274- # descendant tags.
275- # """
276- # return DescendantTagsContentItems(self._ctx, self._path, parent_tag=self._parent_tag)
277-
278186 def find (self ) -> list [Tag ]:
279187 """
280188 Find all child tags that descend from a single tag.
@@ -295,19 +203,14 @@ def find(self) -> list[Tag]:
295203 results = response .json ()
296204 all_tags = []
297205 for result in results :
298- tag = Tag (self ._ctx , f"{ self ._path } /{ result ['id' ]} " , ** result )
206+ tag = Tag (
207+ self ._ctx ,
208+ # TODO-barret-future: Replace with `self._ctx.client.tags._path`?
209+ f"{ self ._path } /{ result ['id' ]} " ,
210+ ** result ,
211+ )
299212 all_tags .append (tag )
300213
301- # all_tags = [
302- # Tag(
303- # self._ctx,
304- # # TODO-barret-future: Replace with `self._ctx.client.tags._path`?
305- # f"{self._path}/{results['id']}",
306- # **result,
307- # )
308- # for result in results
309- # ]
310-
311214 # O(n^2) algorithm to find all child tags
312215 # This could be optimized by using a dictionary to store the tags by their parent_id and
313216 # then recursively traverse the dictionary to find all child tags. O(2 * n) = O(n) but the
@@ -330,38 +233,6 @@ def find(self) -> list[Tag]:
330233
331234 return child_tags
332235
333- # O(n) algorithm to find all child tags
334- child_tags = []
335- parent_ids = {self ._root_id }
336-
337- # Construct a d
338- # O(n)
339- tag_by_parent_id : dict [str , list [Tag ]] = {}
340- for tag in all_tags :
341- parent_id : str | None = tag .get ("parent_id" , None )
342- if parent_id is None :
343- continue
344- parent_id = str (parent_id )
345- if parent_id not in tag_by_parent_id :
346- tag_by_parent_id [parent_id ] = []
347- tag_by_parent_id [parent_id ].append (tag )
348-
349- # O(n) compute space
350- ret : list [Tag ] = []
351- parent_ids_seen = set [str ]()
352- while len (parent_ids ) > 0 :
353- parent_id = parent_ids .pop ()
354- if parent_id in parent_ids_seen :
355- continue
356- parent_ids_seen .add (parent_id )
357- if parent_id in tag_by_parent_id :
358- tags = tag_by_parent_id [parent_id ]
359- ret .extend (tags )
360- for tag in tags :
361- parent_ids .add (tag ["id" ])
362-
363- return ret
364-
365236
366237class Tags (ContextManager ):
367238 """Content item tags resource."""
0 commit comments