Pass Variable On Import

2024/11/19 3:43:38

Let's say you have some time-consuming work to do when a module/class is first imported. This functionality is dependent on a passed in variable. It only needs to be done when the module/class is loaded. All instances of the class can then use the result.

For instance, I'm using rpy2:

import rpy2.robjects as robjectsPATH_TO_R_SOURCE = ## I need to pass this
robjects.r.source(PATH_TO_R_SOURCE, chdir = True) ## this takes timeclass SomeClass:def __init__(self, aCurve):self._curve = aCurvedef processCurve(self):robjects.r['someRFunc'](robjects.FloatVector(self._curve))

Am I stuck creating a module level function that I call to do the work?

import someClass
someClass.sourceRStuff(PATH_TO_R_SOURCE)
x = someClass.SomeClass([1,2,3,4])
etc...
Answer

Having a module init function isn't unheard of. Pygame does it for the sdl initialization functions. So yes, your best bet is probably

import someModule
someModule.init(NECESSARY_DATA)
x = someModule.someClass(range(1, 5))
https://en.xdnf.cn/q/26484.html

Related Q&A

Write a file to a directory that doesnt exist [duplicate]

This question already has answers here:How do I create a directory, and any missing parent directories?(27 answers)Closed 9 months ago.This post was edited and submitted for review 8 months ago and fa…

How to convert a string to an image?

I started to learn python a week ago and want to write a small program that converts a email to a image (.png) so that it can be shared on forums without risking to get lots of spam mails. It seems lik…

Numpy list of 1D Arrays to 2D Array

I have a large list files that contain 2D numpy arrays pickled through numpy.save. I am trying to read the first column of each file and create a new 2D array.I currently read each column using numpy.…

What is metrics in Keras?

It is not yet clear for me what metrics are (as given in the code below). What exactly are they evaluating? Why do we need to define them in the model? Why we can have multiple metrics in one model?…

ObjectNotExecutableError when executing any SQL query using AsyncEngine

Im using async_engine. When I try to execute anything: async with self.async_engine.connect() as con:query = "SELECT id, name FROM item LIMIT 50;"result = await con.execute(f"{query}&quo…

Store the cache to a file functools.lru_cache in Python = 3.2

Im using @functools.lru_cache in Python 3.3. I would like to save the cache to a file, in order to restore it when the program will be restarted. How could I do?Edit 1 Possible solution: We need to pi…

Moon / Lunar Phase Algorithm

Does anyone know an algorithm to either calculate the moon phase or age on a given date or find the dates for new/full moons in a given year?Googling tells me the answer is in some Astronomy book, but…

Flask-RESTful API: multiple and complex endpoints

In my Flask-RESTful API, imagine I have two objects, users and cities. It is a 1-to-many relationship. Now when I create my API and add resources to it, all I can seem to do is map very easy and genera…

Setting initial Django form field value in the __init__ method

Django 1.6I have a working block of code in a Django form class as shown below. The data set from which Im building the form field list can include an initial value for any of the fields, and Im having…

Should I use a main() method in a simple Python script?

I have a lot of simple scripts that calculate some stuff or so. They consist of just a single module.Should I write main methods for them and call them with the if __name__ construct, or just dump it a…