Commit b1fe6a20 by Dudás Ádám

common: better tools for activity code manipulation

parent ab55fbae
from collections import deque from collections import deque
from contextlib import contextmanager from contextlib import contextmanager
from hashlib import sha224 from hashlib import sha224
from itertools import chain, imap
from logging import getLogger from logging import getLogger
from time import time from time import time
...@@ -39,12 +40,52 @@ activity_context = contextmanager(activitycontextimpl) ...@@ -39,12 +40,52 @@ activity_context = contextmanager(activitycontextimpl)
activity_code_separator = '.' activity_code_separator = '.'
def has_prefix(activity_code, *prefixes):
"""Determine whether the activity code has the specified prefix.
E.g.: has_prefix('foo.bar.buz', 'foo.bar') == True
has_prefix('foo.bar.buz', 'foo', 'bar') == True
has_prefix('foo.bar.buz', 'foo.bar', 'buz') == True
has_prefix('foo.bar.buz', 'foo', 'bar', 'buz') == True
has_prefix('foo.bar.buz', 'foo', 'buz') == False
"""
equal = lambda a, b: a == b
act_code_parts = split_activity_code(activity_code)
prefixes = chain(*imap(split_activity_code, prefixes))
return all(imap(equal, act_code_parts, prefixes))
def has_suffix(activity_code, *suffixes):
"""Determine whether the activity code has the specified suffix.
E.g.: has_suffix('foo.bar.buz', 'bar.buz') == True
has_suffix('foo.bar.buz', 'bar', 'buz') == True
has_suffix('foo.bar.buz', 'foo.bar', 'buz') == True
has_suffix('foo.bar.buz', 'foo', 'bar', 'buz') == True
has_suffix('foo.bar.buz', 'foo', 'buz') == False
"""
equal = lambda a, b: a == b
act_code_parts = split_activity_code(activity_code)
suffixes = chain(*imap(split_activity_code, suffixes))
return all(imap(equal, reversed(act_code_parts), reversed(suffixes)))
def join_activity_code(*args): def join_activity_code(*args):
"""Join the specified parts into an activity code. """Join the specified parts into an activity code.
:returns: Activity code string.
""" """
return activity_code_separator.join(args) return activity_code_separator.join(args)
def split_activity_code(activity_code):
"""Split the specified activity code into its parts.
:returns: A list of activity code parts.
"""
return activity_code.split(activity_code_separator)
class ActivityModel(TimeStampedModel): class ActivityModel(TimeStampedModel):
activity_code = CharField(max_length=100, verbose_name=_('activity code')) activity_code = CharField(max_length=100, verbose_name=_('activity code'))
parent = ForeignKey('self', blank=True, null=True, related_name='children') parent = ForeignKey('self', blank=True, null=True, related_name='children')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment