0

Here is my Function.........

from instrument import Instrument
import pandas as pd

  def getpairsData():
    global pairlist    <<============= Defined Global variable
    pairlist = Instrument.get_pairs_from_list() <<=============Call
    return pairlist   <<============= Available Global variable
 

"""It is being called by this loop....... #This loop will iterate over pairlist, and will pass one pair to get data, and save that data as file."""

 for pair in pairlist:      <<======= My Global variable above????

    getData()
    for i in range(0,11):
        df1= pd.read_csv("./data/"+ str(files[i])+".csv")
        print (df1.head())



import pandas as pd
import Utils

class Instrument():
def __init__(self,ob):
    self.name=ob['name']
    self.type=ob['type']
    self.displayName = ob['displayName']
    self.pipLocation = pow(10,ob['pipLocation']) # ex. - --> 0.0001
    self.marginRate = ob['marginRate']


@classmethod
def get_pairs_from_list(cls):  <<============= Receive Call 1
    i_list = cls.get_instruments_list()
    i_keys = [x.name for x in i_list]

    df = pd.DataFrame(i_keys)
    df = df.rename(columns={0: "Names"})

    df['Names'] = df['Names'].str.replace("_", '')
    print("You are here")

    pairlist = df["Names"].tolist()  # if it is being changed to a list with no keys

    return pairlist   <<============= Return Call 1

"""I am thinking the pairlist would return to the original function that is set as a Global variable and off to the races we go, but I am stuck on the print("You are here") not responding at all.

NameError: name 'pairlist' is not defined"""

5
  • It would also help to post the full traceback message. Is the error at for pair in pairlist: ? Is that for loop in a function? Have you called getpairsData() before you get to this loop? It looks like you haven't called getpairsdata to set the variable. Commented Aug 2, 2021 at 22:25
  • Thanks, I understand your point, but I do think the calls are in the script. I will attempt to place a pointer like <<=============== To Highlight the locations for the call. Thanks for any feedback. Commented Aug 2, 2021 at 22:44
  • Please reconsider the updated code for your feedback, Thanks again. Commented Aug 2, 2021 at 22:49
  • I am realizing that its not just a variable, but a Dataframe. Would that cause it not to work properly? Commented Aug 2, 2021 at 22:50
  • where is the function call for getpairsData() ? Commented Aug 2, 2021 at 22:54

2 Answers 2

2

As I don't have the full view of your implementation but I think your code should be something like below snippet:

from instrument import Instrument
import pandas as pd

def getpairsData():
  global pairlist
  pairlist = Instrument.get_pairs_from_list() # <<=============Call
  return pairlist   # <<============= Available Global variable
 
pairlist = getpairsData()
 
for pair in pairlist:      # <<======= My Global variable above????
  for i in range(0,11):
    df1= pd.read_csv("./data/"+ str(files[i])+".csv")
    print (df1.head())



import pandas as pd
import Utils

class Instrument():

  def __init__(self,ob):
      self.name=ob['name']
      self.type=ob['type']
      self.displayName = ob['displayName']
      self.pipLocation = pow(10,ob['pipLocation']) # ex. - --> 0.0001
      self.marginRate = ob['marginRate']


  @classmethod
  def get_pairs_from_list(cls):  # <<============= Receive Call 1
      i_list = cls.get_instruments_list()
      i_keys = [x.name for x in i_list]

      df = pd.DataFrame(i_keys)
      df = df.rename(columns={0: "Names"})

      df['Names'] = df['Names'].str.replace("_", '')
      print("You are here")

      pairlist = df["Names"].tolist()  # if it is being changed to a list with no keys

      return pairlist   # <<============= Return Call 1
Sign up to request clarification or add additional context in comments.

Comments

1
"""I have found that  @tdelaney explanation was correct.  I never called 
the variable because it was derived and then needed to be returned."""

@classmethod
def get_pairs_from_list(cls):  <<============= Receive Call 1
    i_list = cls.get_instruments_list()
    i_keys = [x.name for x in i_list]

    df = pd.DataFrame(i_keys)
    df = df.rename(columns={0: "Names"})

    df['Names'] = df['Names'].str.replace("_", '')
    print("You are here")

    pairlist = df["Names"].tolist()  # if it is being changed to a list 
                                    with no keys

    `return getpairsData(pairlist)`   <<=========== Return Call with 
    Variable

1 Comment

I have a pulse now, but something still isn't correct. I will take this off of the radar. Thanks for all of your help..

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.