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

The Archive Base Latest Questions

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

I have a class in crawl.py that’s like this: from myapp.crawl import ScrapyCommand class

  • 0

I have a class in crawl.py that’s like this:

from myapp.crawl import ScrapyCommand

class Command(ScrapyCommand):

    name = 'someotherapp.crawl' #this should be the dynamic import

    def handle(self, *args, **options):
        super(Command, self).handle(self, *args, **options)
        ...
        ...< custom code >
        ...

and a file called list.py contains:

from myapp.list import ScrapyCommand

class Command(ScrapyCommand):

    name = 'someotherapp.list' #this should be the dynamic import

    def handle(self, *args, **options):
        super(Command, self).handle(self, *args, **options)
        ...
        ...< some other custom code >
        ...

This class that they both inherit called ScrapyCommand from two separate files – myapp/crawl.py and myapp/list.py. The ScrapyCommand that is inherited by crawl.py looks like this:

from someotherapp.crawl import OtherCommand

class ScrapyCommand(OtherCommand):     

    def __init__(self, *args, **kwargs):
        ...
        ...

    def handle(self, *args, **options):
        return

The ScrapyCommand that is inherited by list.py looks like this:

from someotherapp.list import OtherCommand

class ScrapyCommand(OtherCommand):

    def __init__(self, *args, **kwargs):
        ...
        ...

    def handle(self, *args, **options):
        return

I’ve edited this code for brevity. The logic in the ScrapyCommand is simple and both these files contain exactly the same code except the import statement. Look at the imports.

I’m looking to reduce the amount of duplication of code. Is there a way, I could make the base class dynamically import it’s own base class. The code in crawl.py and list.py is different but the code in their base-class is exactly the same. When list.py imports it’s base-class i.e. ScrapyCommand, that class should import it’s base-class i.e. OtherCommand from a file that I specify dynamically in the name parameter.

How can I do this? I haven’t been able to figure out an easier way of doing this and I’m only going down this rabbit hole because I have quite a few Commands and I could greatly reduce the amount of duplicated code. Thanks for reading this.

—

As for thew name attribute. I’m not entirely bent on using a class attribute. If you can suggest a better store the import statement I’d go with that but I would need to obviously have that import somewhere. Where should I put it? Thanks

  • 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:18:43+00:00Added an answer on June 13, 2026 at 8:18 am

    You could construct a class from a function. Does this solve your problem?

    def make_scrapy_command_class(base_class):
        class ScrapyCommand(base_class):
            # your methods here
            pass
    
        return ScrapyCommand
    
    ScrapyCommand = make_command_class(someotherapp.crawl.OtherCommand)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class that looks like this: class A { public: class variables_map vm
I have class like this below shown. which contains the shopping items where the
I have class B that has class A, this is simplified version of class
I have a python application like this / /crawl.py /crawl/__init__.py /crawl/john.py /tests/test_john.py What I
I have class that extend FragmentActivity in it I add fragment to layout as
I have class grades that I need to check if a specific grade is
I'm attempting to test that my service is calling Anemone.crawl correctly. I have the
In a web-crawler of mine, I have a class that keeps track of urls
I have class method that returns a list of employees that I can iterate
I have a function like this in JQuery and JS. I have a list

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.