1

I have written some code, but it's all located in one file. It's sort of annoying to work with now because it's so long. So I'm thinking I should put all the function in a different file and just import them. I'm just not sure about the best way to go about doing this.

Current Code:

import glob, os
import csv as csv
import pandas as pd
import numpy as np
import sys

def create_list(var):      
    stuff
def store_table(var):
    stuff
def geo_info(var):
    stuff 
def factor_loads(var):
    stuff  
def add_loads(var):
    stuff 
def calc_bypass_stress(var):
    a lot of stuff
def calcloads(var): 
    tons of stuff and calls other functions above    

for i in list:
   for k in another_list:
       calcloads(var)

I'm thinking that I could split it up so the functions that do lookups and create tables are in tables.py; anything that calculates stuff is in calc_stuff.py and I run everything from thehead.py:

tables.py

def create_list(var):      
    stuff
def store_table(var):
    stuff
def geo_info(var):
    stuff
def factor_loads(var):
    stuff
def add_loads(var):
    stuff

calc_stuff.py

def calc_bypass_stress(var):
    a lot of stuff
def calcloads(var): 
    tons of stuff and calls other functions above    

thehead.py

import glob, os
import csv as csv
import pandas as pd
import numpy as np
import sys
from tables import *
from calc_stuff import *

for i in list:
   for k in another_list:
       calcloads(var)

Some questions:

1) Will this work?

2) Almost all of my functions use something from Pandas, Numpy, etc. Do I need to import those packages inside each of the .py files? Or just at thehead.py?

3) From thehead.py I'm calling a function (calcloads) that is located in calc_stuff.py which uses functions located in tables.py. Should I have kept all functions in one file as opposed to splitting functions up into different files?

0

2 Answers 2

2
  1. Yes.
  2. You need to import the packages in each of the .py files.
  3. It's not a problem to split the functions up into different files. If your files are becoming very long, it would definitely be a good idea.
Sign up to request clarification or add additional context in comments.

2 Comments

1) When you say to import packages in each .py file, do I also import the files I created into each file? 2) Would I put from calc_stuff import * in the calc_stuff.py file, though? 3) Do I put from calc_stuff import * in tables.py?
1) Yes. 2) No, a script shouldn't try to import itself. 3) Yes and no. You need an import statement there, but wildcard imports are strongly discouraged in Python, so I would suggest import calc_stuff instead.
2

I think @tjohnson 's answer is excellent. I would just add something to question 3. It sounds like you have written a lot of functions, and you are trying to make your code / files more manageable by putting the functions which are not under "active development" into a sort of library. In that case, yes it is not a problem to store your functions in a set of separate files. But, going by the "flat is better than nested" principle, it may improve ease of lookup to put them all in one very long .py file, and then put a table of contents in the comments of that file, or print a table of contents on the fly after importing it.

One quick and dirty way to get the table of contents is as follows. Suppose your very long file is called mymod1.py. Then you can write:

EDITED TO SIMPLIFY: (using an idea from @kojiro here)

from mymod1 import * 
import mymod1 # to get it in the namespace
def my_toc(module):
    exclude = ['date','pprint'] # list of callables imported via `from foo import bar`
    for item in dir(module):
        if (item not in exclude and
            callable(getattr(module, item, locals().get(item)))):
            print(item)
my_toc(mymod1)

Output:

myfunc_1
myfunc_2
myfunc_3 # etc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.