Newbee Question - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Newbee Question (/thread-25514.html) |
Newbee Question - chas777 - Apr-01-2020 I am trying to make a class I can use to save a dataframe, two strings and two floats. I cant seem to get it to work. import numpy as np import os import matplotlib import matplotlib.pyplot as plt matplotlib.use('wxAgg') # matplotlib backend needed to work with wxPython GUI import time from datetime import datetime import pandas as pd selected_dir = r"C:\Users\cstreb\Desktop\Data Visualization" path = r"C:\Users\cstreb\Desktop\Data Visualization\Test1.xlsm" df = pd.read_excel (path, sheet_name='DefocusData', names = ["Description","Defocus","Tan","Sag"]) Title = os.path.basename(path) print (Title) Time = os.path.getmtime(path) local_time = time.ctime(Time) print("Last modification time(Local time):", local_time) FieldAngles = ((df.loc[df['Description'] == 'THF Field Angle, Roll Angle']).iloc[:,1]) DefocusPlanes = ((df.loc[df['Description'] == 'Number of THF Planes']).iloc[:,1]) DefocusFrequency = ((df.loc[df['Description'] == 'Defocus Frequency']).iloc[:,1]) print (DefocusFrequency) for x in DefocusPlanes.index: print (x) print (df.iloc[(x+3):(x+3+DefocusPlanes[x]), 1:4]) class geeks: def __init__(self): self.DFAngSagTang = pd.dataframe self.FileName = "" self.Date = "" self.FieldAngle = -1 self.LP = -1 def AddDefocusPlane(DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time): self.DFAngSagTang = DefocusPlane self.FileName = Title self.Date = local_time self.FieldAngle = FieldAngle self.LP = DefocusFrequency for x in DefocusPlanes.index: FieldAngle = (FieldAngles[x-1]) DefocusPlane = (df.iloc[(x+2):(x+2+DefocusPlanes[x]), 1:4]) print (DefocusPlane) SingleRunData.AddDefocusPlane(DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time) RunData.append(SingleRunData)I get the following error -=-=-=-=-=-=-=-=- Defocus Tan Sag 8 -50.8 9.2 10.1 9 -24.7 29.8 30.1 10 0.7 39.4 39.5 11 25.6 29.9 31.5 12 50.1 11.5 14.5 --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-197-163bba125617> in <module> 3 DefocusPlane = (df.iloc[(x+2):(x+2+DefocusPlanes[x]), 1:4]) 4 print (DefocusPlane) ----> 5 SingleRunData.AddDefocusPlane(DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time) 6 RunData.append(SingleRunData) <ipython-input-194-9edb5b7dd6ac> in AddDefocusPlane(DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time) 7 self.LP = -1 8 def AddDefocusPlane(DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time): ----> 9 self.DFAngSagTang = DefocusPlane 10 self.FileName = Title 11 self.Date = local_time NameError: name 'self' is not defined -=-=-= I get the following errors. Any help would be great , thank you. New Bee RE: Newbee Question - BrendanD - Apr-01-2020 You forgot self in your second class method. Should be: def AddDefocusPlane(self, DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time): RE: Newbee Question - chas777 - Apr-01-2020 class OneDataSet: def __init__(self, DFAngSagTang, FieldAngle, DefocusFrequency, Title, local_time): self.DFAngSagTang = DFAngSagTang self.FileName = Title self.Date = local_time self.FieldAngle = FieldAngle self.LP = DefocusFrequency def __call__(self, DFAngSagTang, FieldAngle, DefocusFrequency, Title, local_time): self.DFAngSagTang = DFAngSagTang self.FileName = Title self.Date = local_time self.FieldAngle = FieldAngle self.LP = DefocusFrequency SingleRunData = OneDataSet RunData = [] for x in DefocusPlanes.index: FieldAngle = (FieldAngles[x-1]) DefocusPlane = (df.iloc[(x+2):(x+2+DefocusPlanes[x]), 1:4]) SingleRunData(DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time) RunData.append(SingleRunData)Now I am trying to get data out ! I write len (RunData)and get 3 I try RunData[0]and get __main__.OneDataSet I try RunData[0].DFAngSagTangand it doesnt like me ! Last hertel thank you RE: Newbee Question - BrendanD - Apr-01-2020 One other mistake I missed. When you instantiate a class you must include parenthesis: SingleRunData = OneDataSet() RE: Newbee Question - chas777 - Apr-01-2020 Thanks for that but "SingleRunData = OneDataSet" seems to work But I am not able to get data back out ! I am trying though Yeahhhhhhh. Thank you for the help @BrendanD. You were correct after all. if I instantiate , I need all my parameters !!! RunData = [] for x in DefocusPlanes.index: FieldAngle = (FieldAngles[x-1]) DefocusPlane = (df.iloc[(x+2):(x+2+DefocusPlanes[x]), 1:4]) SingleRunData = OneDataSet(DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time) RunData.append(SingleRunData) for onedata in RunData: print (onedata.FieldAngle)So I moved it to one line ! SingleRunData = OneDataSet(DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time)Now I am happy !!! thank you |