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 8727877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:31:23+00:00 2026-06-13T08:31:23+00:00

I want to implement something a bit similar to django fixture system where in

  • 0

I want to implement something a bit similar to django fixture system where in fixture you set model property which indicates a model class of fixture. It looks something like this

my_app.models.my_model

My question is what is the standard way to process such a string in order to create the instance of the class pointed by this “path”. I think it should look something like:

  1. Split it to module name and class name parts
  2. Load module (if not loaded)
  3. Acquire class from module by its name
  4. Instantiate it

How exactly should I do it?

Edit: I came up with a dirty solution:

def _resolve_class(self, class_path):
    tokens = class_path.split('.')
    class_name = tokens[-1]
    module_name = '.'.join(tokens[:-1])
    exec "from %s import %s" % (module_name, class_name)
    class_obj = locals()[class_name]
    return class_obj

That does it’s job however is dirty because of usage of exec and possibility of manipulating execution by malicious preparation of fixtures. How should it be done properly?

  • 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-06-13T08:31:23+00:00Added an answer on June 13, 2026 at 8:31 am

    Note that the danger of using exec in a function is that it often allows an attacker to supply bogus values which will cause your function to “accidentally” execute whatever code the attacker wants. Here you’re directly writing a function that allows precisely that! Using exec doesn’t make it much worse. The only difference is that without exec they have to figure out how to get their code into a file on python’s import path.

    That doesn’t mean you shouldn’t do it. Just be aware of what you’re doing. Plugin frameworks inherently have this problem; the whole point of making a framework extensible at runtime is that you want whoever can configure the plugins to be able to execute whatever code they like inside your program. If your program will be used in an environment where the the end users are not the same people who are configuring the plugins, make sure you treat _resolve_class the same way you treat exec; don’t allow users to enter strings which you directly pass to _resolve_class!

    Now, that aside, you can avoid the use of exec quite easily. Python has a built-in function __import__ for getting at the underlying implementation of the import mechanism. You can use it to do dynamic imports (help(__import__) was enough for me to figure out how it works to write this answer; there is also the docs if you need a bit more detail). Using that, your function could look something like:

    def _resolve_class(self, class_path):
        modulepath, classname = class_path.rsplit('.', 1)
        module = __import__(modulepath, fromlist=[classname])
        return getattr(module, classname)
    

    (Note that I’ve also used rsplit with a maximum number of splits to avoid having to split the module path only to rejoin it again)

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

Sidebar

Related Questions

I want to implement something simple like /System/Workflows/Sample Workflow with the small addition of
I want to implement in my webpage something similar to google search page. The
I want to implement something similar to Wikimedia's revision history? What would be the
I want to implement something like Col3 = Col2 + Col1 in SQL. This
I want to implement something with jquery, simple javascript and java but I don't
I am learning OOP with python. I want to implement something like to display:
i want to implement a dynamic relation mechanism with python something like: a:=10 b:=30
for iOS 5.0 using ARC, here is something I want to implement and need
I want to do something like this: I tried to implement it by myself
For my website, I want to make something that works a bit like the

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.