English 中文(简体)
Function doesn t return anything and doesn t run when imported
原标题:

This is killing me, I ve spent handful of hours trying to debug this on my own with no luck. Basically, I have a function1.py that uses Pandas to pulls in some csv files, join them according to my specifications, and is supposed to 1) print the resulting DF 2) write the DF to excel. The function1.py is its own module and is coded like this:

def getMerged(csv_files, output_name):

   import pandas as pd


   list_of_expiry_dfs = []

   for csvfile in csv_files:
       names = [ date ,  open ,  high ,  low ,  close ,  vol ]             # this can also go outside the fn
       df = pd.read_csv(csvfile, sep= ; , names=names)
       df = df.set_index( date )
       list_of_expiry_dfs.append(df)

   x = 1

   composite = list_of_expiry_dfs[0]
   for each in list_of_expiry_dfs:
       composite = composite.join(each, lsuffix=x-1, rsuffix=x, how= outer )
       x = x + 1


   yaxis =[]
   for expiry in csv_files:
       open =  open (  + expiry[:6] + ")"
       high =  high (  + expiry[:6] + ")"
       low =  low (  + expiry[:6] + ")"
       close =  close (  + expiry[:6] + ")"
       vol =  vol (  + expiry[:6] + ")"
       yaxis.append(open)
       yaxis.append(high)
       yaxis.append(low)
       yaxis.append(close)
       yaxis.append(vol)
   composite.columns = yaxis

   print(composite)
   composite.to_excel(output_name, header=True)

At the bottom of the function.py, I can optionally choose to run this function, by provided some parameters as follows:

csv_files = [
 BZ1219.csv ,
 BZ0120.csv ,
 BZ0220.csv ,
 BZ0320.csv ,
 BZ0420.csv 
]

getMerged(csv_files, output_name= test.xlsx )

And it works just fine when run as the main executable. But that s not what I want to do. I ve identified a slew of issues I need help with.

First, I m totally unable to return "composite" outside of the function. I ve all types of solutions including return statements, global variables, setting the function equal to a variable, and also setting attributes. No matter what I do, I simply cannot assess composite outside of the function.

What I really want to do is to be able to import function1.py from another file and run it as I please. Aka function1.py containing code block 1 only (no code block 2). That other.py looks like this:

csv_files = [ BZ1219.csv , BZ0120.csv , BZ0220.csv , BZ0320.csv , BZ0420.csv ]
output_name = "test.xlsx"


from function1 import getMerged


getMerged(csv_files=csv_files, output_name=output_name)

This doesn t return an error, but also doesn t do anything. The closest I ve gotten is by adding the following code to code block 1. It does.. well, something. But it kicks the function into an infinite loop which eventually breaks down.

def getMerged(csv_files, output_name):
   ** insert the rest of first code block **
   return composite
   output = getMerged(csv_files=csvfiles, output_name=output_name)

After making a number of changes to other.py, I was able at one point to FINALLY get some output running in other.py with the above script, but again, it ran as an infinite loop until it broken down. I ve changed the code so many times I dont even remember what that code change to other.py was

Please help!

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签