0

My main function loads a largish dataframe from a csv supplied by the user (along with several other data objects), and then instantiates an object that forms a bunch of equations as part of a mathematical programming problem. Many of the equations' components are returned by calls to about 5 helper functions that I define in a utils file (most importantly, outside of the class that stores the optimization problem). These helper functions make reference to the data loaded in main, but I want their calls to show up in the equations as parameterized only by a time index t (not by the dataframe), for readability. Is the best way to accomplish this to define those functions in utils.py with the data as explicit parameters, and then use functools.partial before generating the equations to make them implicit?

This seems like a verbose approach to me, but the other options seem worse: to define the helper functions inside of main, or to give up on the idea of a main function loading the data, which basically means giving up on having a main function. And possibly having confusing circular imports.

GrayOnGray
  • 315
  • 3
  • 12
  • You can not have circular imports in Python (yeah, you can import *inside* a function) such that importing is postponed, etc. but two files can not `import` each other in the head of the file. – Willem Van Onsem Nov 26 '17 at 21:40
  • @WillemVanOnsem That's not correct. Circular imports at the module level are possible, and even necessary sometimes. – wim Nov 26 '17 at 21:41

1 Answers1

0

I ended up going with my proposed answer:

define those functions in utils.py with the data as explicit parameters, and then use functools.partial before generating the equations to make them implicit

and with that codebase having been in production for over a year, it seems reasonable enough to me.

GrayOnGray
  • 315
  • 3
  • 12