I have problems with the following code, it says "NameError: global name 'Teater' is not defined" I can not solve it by myself...
teaterLista = []
lista = []
class Teater:
def __init__(self, teaterNamn, plats, pensionar,vuxen,barn):
self.teaterNamn = teaterNamn
self.plats = plats
self.pensionar = pensionar
self.vuxen = vuxen
self.barn = barn
def readData():
#x = Teater(x,teaterNamn, plats,pensionar,vuxen,barn)
dataFile = open('c:/Teater.txt','r')
for line in dataFile:
if(line != '\n'):
temp = line.split('=',1)[1]
lista.append(temp.strip()) #strip tar bort radavslut
x = Teater(x,lista[0],lista[1],lista[2],lista[3],lista[4])
#teaterLista[0] = x
#print(teaterLista[0])
readData()
You call readData() during class definition. In Python a class body is executed during its definition in the contex of the class definition just as normal code would be. As the class is not completely defined at that moment, you cannot create a new instance, yet, thus get the error.
Dedent the whole definition for readData and the following line so all this is executed after the definition of the class has completed. This makes readLine a module-level function, not a class method. This is typical for a factory function.
teaterLista = []
lista = []
class Teater:
def __init__(self, teaterNamn, plats, pensionar,vuxen,barn):
self.teaterNamn = teaterNamn
self.plats = plats
self.pensionar = pensionar
self.vuxen = vuxen
self.barn = barn
def readData():
#x = Teater(x,teaterNamn, plats,pensionar,vuxen,barn)
dataFile = open('c:/Teater.txt','r')
for line in dataFile:
if(line != '\n'):
temp = line.split('=',1)[1]
lista.append(temp.strip()) #strip tar bort radavslut
x = Teater(lista[0],lista[1],lista[2],lista[3],lista[4])
#teaterLista[0] = x
#print(teaterLista[0])
readData()
Note: x = Teater(x, ... will not work, as x is not defined for the first invocation. If you think about specifying this for the self argument: no need; this is done implicitly. You should read how classes work in the documentation/tutorial.
You have to be careful in Python to correctly indent your code, as that defines the block scope.
Related
I've made a class as follow:
class Plugins:
def __init__(self):
pass
def voter_rep(self, loc, start_header, end_header):
self.loc = loc
ocr_xml = AbbyyXML(loc)
xml_doc = XMLDoc(ocr_xml, CONSTANTS)
xml_doc.split_words("", False)
self.start_header = start_header
self.end_header = end_header
header_pages = xml_doc.se_page(start_header, end_header)
## and stuff
voter_dict = {'Voter':[], 'Record_Key':[], 'Comments':[]}
## and stuff
return voter_dict, rep_dict
if I run the method function on its own and outside of the class it works totally fine, namely if I write the function as:
def voter_rep(loc, start_header, end_header):
ocr_xml = AbbyyXML(loc)
xml_doc = XMLDoc(ocr_xml, CONSTANTS)
xml_doc.split_words("", False)
header_pages = xml_doc.se_page(start_header, end_header)
## and stuff
voter_dict = {'Voter':[], 'Record_Key':[], 'Comments':[]}
## and stuff
return voter_dict, rep_dict
in the function alone I get rid of self and will just have voter_rep(loc, start_header, end_header) but when I want to call it from the class I do plugins.voter_rep(loc, start_header, end_header) which does not work, and it returns:
NameError: name 'plugins' is not defined
I wonder why is it that my function works on its own but not callable from the class?
You can do
plugins = Plugins()
loc = #some val
start_header = #some val
end_header = #some val
plugins.voter_rep(loc, start_header, end_header)
As the error message shows, you are using small 'p' instead of capital. Also since it is not a static function, so it is not good to call it via class name.
Plugins.voter_rep(loc, start_header, end_header)
Take note of the capital letter.
I am writing a class where I would like to pass function as a class attribute and later use it, like that:
class Nevronska_mreza:
def __init__(self, st_vhodni, st_skriti, st_izhod, prenosna_funkcija=pf.sigmoid):
self.mreza = []
self.st_vhodni = st_vhodni
self.st_skriti = st_skriti
self.st_izhodni = st_izhod
self.prenosna_funckija = prenosna_funkcija
self.mreza.append([{'utezi': [random() for i in range(st_vhodni + 1)]} for j in range(st_skriti)])
self.mreza.append([{'utezi': [random() for i in range(st_skriti + 1)]} for j in range(st_izhod)])
def razsirjanje_naprej(self, vhod):
for sloj in self.mreza:
nov_vhod = []
for nevron in sloj:
nevron['izhod'] = self.prenosna_funkcija(self.aktivacijska_funkcija(nevron['utezi'], vhod))
nov_vhod.append(nevron['izhod'])
vhod = nov_vhod
return vhod
but it seems like this isn't the right way, I get the following error:
AttributeError: 'Nevronska_mreza' object has no attribute 'prenosna_funkcija'
Is it possible to do something like that?
Yes you can pass a function around as an argument however you have made a couple of mistakes.
Firstly you have used the word function, although not a reserved word it should be avoided as a name of an entity such as a variable.
Secordly you have used an optional parameter before mandatory parameters which will cause an error such as:
File "test.py", line 5
def __init__(self, function=fun1, data1, data2):
^
SyntaxError: non-default argument follows default argument
Thirdly when calling the method you have not specified the scope, the function name is in the self scope of the object.
Taking all of these into account the following is working code
def fun1(x):
return x+1
class A:
def __init__(self, data1, data2, fn=fun1):
self.fn = fn
self.data1 = data1
self.data2 = data2
def some_method(self):
y = self.fn(self.data1)
print(y)
b = A(1, 2, fun1)
b.some_method()
After posting your full code I can see that you currently have self.prenosna_funckija instead of prenosna_funkcija in the following line:
self.prenosna_funckija = prenosna_funkcija
This would explain the attribute error as when you are calling self.prenosna_funkcija it genuinely does not exist.
You're close:
def fun1(x):
return x+1
class A:
def __init__(self, function=fun1, data1=None, data2=None):
self.function = function
self.data1 = data1
self.data2 = data2
def some_method(self):
y = self.function(self.data1)
return y
a = A(data1 = 41)
result = a.some_method()
print(result)
prints
42
I am trying to use the "setx" function of a Property in a Class to do some processing of date information that I get from excel. I have a few of my own functions that do the data processing which I tested outside the class, and they worked just fine. But when I move them into the class they suddenly become invisible unless I use the self. instance first. When I use the self.My_xldate_as_tuple() method I get an error:
My_xldate_as_tuple() takes 1 positional argument but 2 were given
Even though the code is EXACTLY what i used outside the class before and it worked.
Before moving into the Property Set block, I was doing the processing of date data outside of the class and setting the variables from outside of the class. That gets clunky when I have about 15 different operations that are all based on when the NumDates Property change. I'm showing shortened versions of both the working set of code and the non-working set of code. What is going on with the self. call that changes how the function takes inputs?
Broken Code:
class XLDataClass(object):
_NumDates = []
TupDates = []
def getNumDates(self): return self._NumDates
def setNumDates(self, value):
self._NumDates = value
self.TupDates = list(map(self.My_xldate_as_tuple,value)) #Error here
#This version doesn't work either, since it can't find My_xldate_as_tuple anymore
self.TupDates = list(map(My_xldate_as_tuple,value))
def delNumDates(self):del self._NumDates
NumDates = property(getNumDates,setNumDates,delNumDates,"Ordinal Dates")
#exact copy of the My_xldate_as_tuple function that works outside the class
def My_xldate_as_tuple(Date):
return xlrd.xldate_as_tuple(Date,1)
#Other code and functions here
#end XlDataClass
def GetExcelData(filename,rowNum,titleCol):
csv = np.genfromtxt(filename, delimiter= ",")
NumDates = deque(csv[rowNum,:])
if titleCol == True:
NumDates.popleft()
return NumDates
#Setup
filedir = "C:/Users/blahblahblah"
filename = filedir + "/SamplePandL.csv"
xlData = XLDataClass()
#Put csv data into xlData object
xlData.NumDates= GetExcelData(filename,0,1)
Working Code:
class XLDataClass(object):
NumDates = []
TupDates = []
#Other code and functions here
#end XlDataClass
#exact copy of the same function outside of the class, which works here
def My_xldate_as_tuple(Date):
return xlrd.xldate_as_tuple(Date,1)
def GetExcelData(filename,rowNum,titleCol):
csv = np.genfromtxt(filename, delimiter= ",")
NumDates = deque(csv[rowNum,:])
if titleCol == True:
NumDates.popleft()
return NumDates
#Setup
filedir = "C:/Users/blahblahblah"
filename = filedir + "/SamplePandL.csv"
xlData = XLDataClass()
#Put csv data into xlData object
xlData.NumDates = GetExcelData(filename,0,1)
#same call to the function that was inside the Setx Property of the class, but it works here.
xlData.TupDates = list(map(self.My_xldate_as_tuple,value))
Instance methods in Python require an explicit self in the argument list. Inside the class, you need to write your method definition like:
def My_xldate_as_tuple(self, Date):
I have written an algorithm in Python and now I am trying to make it a bit more object oriented. I have a good understanding (I think) of objects and classes and I have spend some time reading online the syntax for classes in Python. However, I guess my question is quite basic and it would be great to get some help.
I have created a Class XML which contains 3 definitions. I also have used __init__ to initialize the object.
class XML():
def __init__(self,f):
self.f = f
def xmlToString(self):
data = self.f.read()
self.f.close()
...
return station_arr
def exportArray(self):
f= open('stations/'+self.STATION+'.txt')
lines= f.readlines()
...
return phenomena,parameters
def calcAvg(self):
split_phenom = self.phenomena.split(';')
list_of_lists = []
for e in self.parameters:
...
return phenomena,parameters
Then, in the main.py I instantiate the objects and call the methods I want like this:
stations_names ['one', 'two'...]
for station in stations_names:
f = open('respond.txt','r')
xmlStr = ClassXML.XML(f)
stations_arr = xmlStr.xmlToString()
xmlRead = ClassXML.XML(stations_arr)
phenomena,parameters = xmlRead.exportArray()
xmlRetr = ClassXML.XML(phenomena,parameters)
avg_dict,dict_values = xmlRetr.calcAvg()
The error I get is this:
f= open('stations/'+self.station+'.txt')
AttributeError: XML instance has no attribute 'station'
So I understand what is the problem. Some how I have to pass into the class the variable "station". But when I try to included it in the init function I get different errors:
xmlStr = ClassXML.XML(f)
TypeError: __init__() takes exactly 3 arguments (2 given)
Then I thought maybe I have to have multiple init functions but as far as I know this is not possible in Python.
To be honest I don't really know how to handle the problem. Any tip would be useful.
Thanks
D
P.s. I am not sure if the title explains correctly my question, but I can not find any correct words to put it!
IMPLEMENTED FINAL ANSWER
class XML():
def __init__(self,f,station):
self.f = f
self.station =station
def xmlToString(self):
data = self.f.read()
self.f.close()
...
return station_arr
def exportArray(self):
f= open('stations/'+self.STATION+'.txt')
lines= f.readlines()
...
return phenomena,parameters
def calcAvg(self,phenomena,parameters):
split_phenom = self.phenomena.split(';')
list_of_lists = []
for e in self.parameters:
...
return avg_dict,dict_values
** Main **:
for station in stations_names:
f = open('respond.txt','r')
## Instantiate class: ClassXmlString
xmlStr = ClassXML.XML(f,station)
stations_arr = xmlStr.xmlToString()
if stations_arr !='':
phenomena,parameters = xmlStr.exportArray()
avg_dict,dict_values = xmlStr.calcAvg(phenomena,parameters)
class XML():
def __init__(self,f,station):
self.f = f
self.station=station
def xmlToString(self):
data = self.f.read()
self.f.close()
...
self.station_arr = station_arr
def exportArray(self):
#here you need to use self.station_arr
f= open('stations/'+self.station+'.txt')
lines= f.readlines()
...
self.phenomena=phenomena
self.parameters=parameters
def calcAvg(self,):
#here you need to use self.phenomena and self.parameters
split_phenom = self.phenomena.split(';')
list_of_lists = []
for e in self.parameters:
...
self.avg_dict = avg_dict
self.dict_values = dict_values
def makeOutput(self):
#call all your functions
self.xmlToString()
self.exportArray()
self.scalcAvg()
return self.avg_dict , self.dict_values
#now in your main you need to instanciate your class once! not each time you need to call a method:
stations_names ['one', 'two'...]
for station in stations_names:
f = open('respond.txt','r')
xmlStr = ClassXML.XML(f,station)
avg_dict,dict_values = xmlStr.makeOutput()
Haven't tried it, but should work.
I think you could change the way the solution is organized, to make things a bit easier.
Based on the code you posted, I'm assuming that:
xmlToString takes the file f and station as parameter
exportArray takes stations_arr as parameter
calcAvg takes (phenomena, parameters) as parameter
I'll also assume you are ultimately interested in the (avg_dict, dict_values). That said, a slightly refactored version of this solution be something like this:
Main code:
stations_names ['one', 'two'...]
for station in stations_names:
my_xml_object = ClassXML.XML('respond.txt', station)
avg_dict, dict_values = my_xml_object.calcAvg()
Class:
class XML():
def __init__(self, f_name, station):
# 1 - define self.data
with open(f_name, 'r') as f:
self.data = f.read()
# 2 - define self.station_arr
self.station_arr = self.xmlToString(station)
# 3 - Finally define (phenomena, parameters), which
# will be used by calcAvg()
self.phenomena, self.parameters = self.exportArray(station_arr)
def xmlToString(self, station):
data = self.data
...
return station_arr
def exportArray(self, station_arr):
# you must define self.STATION somewhere
f = open('stations/' + self.STATION + '.txt')
lines = f.readlines()
...
return phenomena, parameters
def calcAvg(self):
split_phenom = self.phenomena.split(';')
list_of_lists = []
for e in self.parameters:
...
return phenomena, parameters
I havent't test it, but the most important is that you get the idea.
I am having some problem accessing class instances. I am calling the class from a procedure, name of instance is defined in some variable. I want the instance name to be created of that value and then want to access it, but when i access it is giving error. Can some one please help to resolve this issue.
class myclass:
def __init__(self,object):
self.name = object
def mydef():
global a1
b = "a1"
b = myclass(b)
mydef()
print a1.name
Second Problem:
In my actual script, I have to create a large number of such instances from this function (around 100). So defining their name as global would be painful, is there a way i could access those instances outside function without having to declare them as global.
Modification:
class myclass:
def __init__(self,object,typename):
self.name = object
self.typeid = typename
def mydef():
file_han = open(file,"r")
while True:
line = file_han.readline()
if not line:
break
start = line.find('"')
end = line.find('"',start+1)
string_f = line[start+1:end]
myclass(string_f,'a11')
mydef(file)
print def.name
print def.typeid
File Contents are :
a11 "def"
a11 "ghi"
a11 "eff"
Here's how I'd do it. I don't know why you're messing around with globals, if you'd care to explain, I'll update my answer.
class Myclass(object):
def __init__(self, name):
self.name = name
def mydef():
return Myclass("a1")
a1 = mydef()
print a1.name
Gather your instances in a list:
instances = []
for x in range(1000):
instances.append(Myclass("Instance {0}".format(x)))
print instance[42].name
Note the changes:
Class names should be capitalized
Use object as the base class of your classes (since python 2.2, but no longer necessary in 3.x)
Don't shadow the built-in object with your parameter name
Just use the string "a1" directly as a parameter instead of assigning it to a variable
Return something from the function instead of passing the result by global variable
RE: Comment
You haven't said anything about the format of these files, so I'll just give an example where the file to be read contains one class name per line, and nothing else:
def mydef(filename):
ret = []
with open(filename) as f:
for line in f:
# Call `strip` on line to remove newline and surrounding whitespace
ret.append(Myclass(line.strip()))
return ret
So if you have several files and wish to add all your instances from all your files to a large list, do it like this:
instances = []
for filename in ["myfile1", "myfile2", "myfile3"]:
instances.extend(mydef(filename))
RE: OP Edit
def mydef(filename):
ret = []
with open(filename, "r") as file_han:
for line in file_han:
string_f = line.split('"')[1]
ret.append(Myclass(string_f))
return ret
i = mydef("name_of_file")
RE: Comment
Oh, you want to access them by name. Then return a dict instead:
def mydef(filename):
ret = {}
with open(filename, "r") as file_han:
for line in file_han:
string_f = line.split('"')[1]
ret[string_f] = Myclass(string_f)
return ret
i = mydef("name_of_file")
print i["ghi"].name # should print "ghi"
RE: Comment
If I understand you correctly, you want to have it both ways -- index by both line number and name. Well then why don't you return both a list and a dictionary?
def mydef(filename):
d = {}
L = []
with open(filename, "r") as file_han:
for line in file_han:
string_f = line.split('"')[1]
instance = Myclass(string_f)
d[string_f] = instance
L.append(instance)
return L, d
L, d = mydef("name_of_file")
print d["ghi"].name
print L[3]
print L.index(d["ghi"])
You could use class as repository for your instances, for example
class Named(object):
def __init__(self,name):
self.name = name
def __new__(cls,name):
instance = super(type,cls).__new__(cls,name)
setattr(cls,name,instance)
return instance
def __repr__(self):
return 'Named[%s]'%self.name
Named('hello')
Named('x123')
Named('this is not valid attribute name, but also working')
print(Named.hello,Named.x123,getattr(Named,'this is not valid attribute name, but also working'))