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

  • SEARCH
  • Home
  • 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 3977752
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:57:40+00:00 2026-05-20T04:57:40+00:00

I have a module where all classes that implement my policies are defined. class

  • 0

I have a module where all classes that implement my “policies” are defined.

class Policy_Something(Policy_Base):
    slug='policy-something'
    ...

class Policy_Something_Else(Policy_Base):
    slug='policy-something-else'
    ...

I need to create a mapping from slug to class. Something like:

slug_to_class = {
    'policy-something': Policy_Something,
    'policy-something-else': Policy_Something_Else
}

I was thinking instead of automatically creating slug_to_class by inspecting the module and looking for classes that inherit from Policy_Base (similar to how unittest finds tests, I assume).

Any reason I shouldn’t do that? If not, how exactly would I do that?

  • 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-20T04:57:40+00:00Added an answer on May 20, 2026 at 4:57 am

    Since your “policy” classes inherit from Policy_Base, why not import all relevant modules and then do something like this?:

    import re
    
    def slugify(s):
        return re.sub(r'\W+', '-', s.lower().replace('_', '-'))
    
    def get_slug_to_class_items(policy_class):
        yield (slugify(policy_class.__name__), policy_class)
        for subclass in policy_class.__subclasses__():
            for slug, subclass in get_slug_to_class_items(subclass):
                yield (slug, subclass)
    
    slug_to_class = dict(get_slug_to_class_items(Policy_Base))
    # print repr(slug_to_class)
    

    The get_slug_to_class_items function finds classes that inherit from Policy_Base (recursively iterating on the class hierarchy) and returns a generator of 2-tuples (slug, class) to be set as the items of the expected dict.

    Note it’s important that all modules with “policy” classes are imported before calling get_slug_to_class_items.

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

Sidebar

Related Questions

I have a python module that defines a number of classes: class A(object): def
I'm designing/building a system of classes that all derive from a single base class.
First of all I am stumped. I have a search-module for a CMS that
I have a C++ module that needs to get information from other classes, without
Current I have a seperate maven module for my database access, all my DAO
I have module application. When I run it, the main window of that app
I have module that implements custom content type via NodeAPI hooks ( hook_insert ,
If you have: module A class B end end You can find B and
I have a module that sends an email to a specified email address but
Hi I have an li element that will have something along the lines of

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.