Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5953585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:50:50+00:00 2026-05-22T17:50:50+00:00

I am working on some code to organize our event definition/category mapping. Our goal

  • 0

I am working on some code to organize our event definition/category mapping. Our goal is to be able to define “event names” as well as associate categories, its pretty much a static process. What we want is people using constants and not a bunch of strings everywhere. We also need somewhere to validate events/categories, as well as do some lookup things like getting events by categories and vice versa.

class EventMapping(object)
    def __init__(self, name, categories):
        self.name =  name
        self.categories=  categories
        EventRegistry.register(self)

    def __eq__(self, obj):
        if(isinstance(obj, str)):
            return obj==self.name
        if(isinstance(obj, EventMapping)):
            return obj.name ==self.name 
               and obj.categories==self.categories
        return False

    def __repr__(self):
        return self.name   

class Event():
    class MyCompontent():
        ComponentEvent = EventMapping("ComponentEvent", None)
        ComponentEventWCategory = EventMapping("ComponentEvent"
             , [Category.MyComponent.Useful])
    class Data():
         Created =EventMapping("Created", [Category.Data.All, Category.Data.Count]
         Updated= EventMapping("Updated", [Category.Data.All]
         Deleted= EventMapping("Deleted", [Category.Data.All, Category.Data.Count]

class Category():
    class MyComponent():
        Useful = "Useful"
    class Data():
        All= "All" 
        Count= "Count" 

class EventRegistry():
    @staticmethod
    def register( eventMapping):
        eventName= eventMapping.name
        #store locally...

    @staticmethod
    def events(category):
        #retreive events by category

    @staticmethod
    def category(event_name):
        #retreive categories by event name

There are a couple of things I don’t like:

  1. In the EventRegistry class I store storing them twice, once in an Event Dictionary key being the name, and value a list of categories. The second is the opposite Category dictionary keyed by category name and a list of events. Is there a better way to achieve this? I basically need an easy way to look up by name or category.

  2. Should I just create two registries? One for Categories one for Events?

  3. When it comes to using the Events we do it like:

    self.subscribe(Event.MyComponent.Useful)
    self.subscribe(Category.MyComponent.Useful)

we then use the registry to validate the event exists, as well as deliver the message. Is there an easier way to achieve the package like namespaces?

Just looking for some feedback on better ways to do it, or a pattern to follow. I have searched around and can’t find anything. Even if you know of some module i can look at and get a better feel for a better way to do it, ill be glad to look into that as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:50:50+00:00Added an answer on May 22, 2026 at 5:50 pm

    Why don’t you go for something more pythonic?

    from collections import defaultdict
    
    
    _linked = defaultdict(set)
    
    
    class Linkable(object):
    
        def link(self, *others):
            _linked[self].update(others)
            for each in others:
                _linked[each].add(self)
    
        def unlink(self, *others):
            _linked[self].difference_update(others)
            for each in others:
                _linked[each].discard(self)
    
        def links(self):
            return set(_linked[self])
    
    
    class NameHierarchy(object):
    
        def __init__(self, name, parent=None):
            self.name = name
            self.parent = parent or self
            self.path = parent.path + [self] if parent else [self]
    
        def __str__(self):
            return '.'.join(each.name for each in self.path)
    
        def add(self, name):
            if name not in self.__dict__:
                setattr(self, name, self.__class__(name, self))
    
    
    class LinkedNameHierarchy(Linkable, NameHierarchy):
        pass
    
    
    Category = LinkedNameHierarchy('Category')
    
    Category.add('MyComponent')
    Category.MyComponent.add('Useful')
    
    Category.add('Data')
    Category.Data.add('All')
    Category.Data.add('Count')
    
    Event = LinkedNameHierarchy('Event')
    
    Event.add('MyComponent')
    Event.MyComponent.add('ComponentEvent')
    Event.MyComponent.add('ComponentEventWCategory')
    
    Event.MyComponent.ComponentEventWCategory.link(Category.MyComponent.Useful)
    
    Event.add('Data')
    Event.Data.add('Created')
    Event.Data.add('Updated')
    Event.Data.add('Deleted')
    
    Event.Data.Created.link(Category.Data.All, Category.Data.Count)
    Event.Data.Updated.link(Category.Data.All)
    Event.Data.Deleted.link(Category.Data.All, Category.Data.Count)
    
    
    def show_links():
        print
        print '----8<--------8<--------8<--------8<--------8<--------8<----'
        for k, v in sorted(_linked.iteritems(), key=lambda x:str(x[0])):
            print
            print k
            for each in sorted(v, key=str):
                print '        ', each
    
    show_links()
    
    Event.Data.Created.unlink(Category.Data.Count)
    
    show_links()
    

    There are circular references, which normally would cause memory leakage. In this case it’s ok though, because those objects presumably live as long as the proram runs anyhow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on some code to colorize an image in Java. Basically what I'd
I'm working on some code that uses the System.Diagnostics.Trace class and I'm wondering how
I'm working on some code for a loosely coupled cluster. To achieve optimal performance
I'm working with some code that is confusing me and I'm wondering if I'm
I was working on some code recently and came across a method that had
I am working on some code coverage for my applications. Now, I know that
I'm working with some code (not mine I hasten to add, I don't trust
I am working on some code written by a co-worker who no longer works
I'm working with some code that widely uses the idiom of returning a pointer
I have started working on some code left behind by previous developers, and I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.