Importing constants (function names) from external file

33 views Asked by At

This is the file structure:

root_dir/
├── analysis_tools
│   ├── analysis_tools.py
├── others
│   ├── constants.py
├── rabbit_wrappers
│   ├── rabbit_wrapper.py
└── utils

I am working on a workflow implemented using RabbitMq. This is the brief psuedo-code: 1. Task added to RabbitMQ queue 2. Autoscaler spins up Virtual Machine based on Queue 3. Virtual Machine turns on and consumes from the queue

There is one type of machine per queue. So if there is a message in queue1, I need to run func1. If queue2, i need to run func2.

func[n] consumes from queue[n]

from analysis_tools.analysis_tools import AnalysisTools

runner = AnalysisTools()

FUNCTION_MAP = {
    'func1' : runner.func1,
    'func2' : runner.func2
}
QUEUE_MAP = {
    'func1' : "queue1",
    'func2' : "queue2"
}

I have a function_map and a queue_map. I pass the function to run (func1/func2) in via command-line argument. Based on what function it's running, it automatically attaches and consumes from the relevant queue.

What I want to do save the FUNCTION_MAP and QUEUE_MAP inside others/constants.py. Then I want to be able to call thees functions in both analysis_tools/analysis_tools.py and rabbit_wrappers/rabbit_wrapper.py

The problem is when I keep my two MAPs in constants, it throws an error since it cannot find "runner", and I don't want to/probably shouldn't be instantiating an object of AnalysisTools in my constants.py file.

So basically, 2 questions.

Q1. How can I save my function_map externally, and call it?

I could call the runner from where I've called the function like so runner.FUNCTION_MAP["func1"]()

Q2. How can I import the constants generally?

This from others.constants import * does not work.

1

There are 1 answers

2
rgargente On

You are asking to create a circular dependancy. You want analysis_tools.py to depend on constants.py and constants.py on analysis_tools.py.

Having a file named constants is not generally a good idea. Things should live where they belong functionally, not according to type. You don't have a file call classes.py, functions.py, variables.py for the same reason you shouldn't have a file called constants.py.

FUNCTION_MAP is clearly related to AnalysisTools, just put it in the same file.

Re: Q2, you generally just import what you need in each module. If there are no circular dependencies you should have no problem doing that.