im new to python and i know there has been a lot of discussion on this but I still have a question. I am trying to access a variable from class to a same class function, its throughing a error as
"AttributeError: 'int' object has no attribute 'isOpen'"
Code is:
class serialCommunication():
ser = 0 #class global variable
def ser_port():
.....
def ser_Burdrate():
BurdRate = ( "4800",
"9600",
"19200",
"38400",
"57600",
"115200"
)
BR = int(input("Enter the Burd Rate\n0\t--\t4800\n1\t--\t9600\n2\t--\t19200\n3\t--\t38400\n4\t--\t57600\n5\t--\t115200\n\n"))
return Portno , int(BurdRate[BR]), timeout
def ser_open():
port = serialCommunication.ser_Burdrate()
serialCommunication.ser = serial.Serial(port[0], port[1], timeout=port[2])
port = serialCommunication.ser.getPort()
print (serialCommunication.ser , '\r\n')
.....
def ser_Write():
if (serialCommunication.ser.isOpen()):
print ('open: ', serialCommunication.ser.getPort())
elif (serialCommunication.ser.closed()):
serialCommunication.ser_open()
please advice on the same
thanks in advance
thanks for advice i changed
ser = serial.Serial()
and it's throughing a error as
"TypeError: 'bool' object is not callable"
in if statement the bool object can be executed right..?
You're trying to call a method .isOpen() on a int type (ser = 0 declared in the class serialCommunication) as the error suggests the object does not have this method.
I can't see the rest of the class but are you sure you shouldn't be trying to reference a method of the class directly? Also without an instance the method you call would need to be a class method prefixed with #classmethod decorator.
Related
i am new to python and trying to create some code for my understanding. i am getting error for getting tick_data in another object.
following is my code where i defined dataframe
class MarketDataLoader():
def __init__(self,symbols,cashPerTrade,broker_config: 'BrokerConfig'):
self._tick_data = None
#few other init var
#property
def tick_data(self) -> 'pandas.DataFrame':
return self._tick_data
def process_tick_data(self, data, dataframe):
# some processing for tick data and assign value to tick data
def process_and_resample_tick_data(self):
self._api.subscribe_market_data(self._symbols, (self.process_tick_data,))
when process_and_resample_tick_data method got called it starts streaming to process_tick_data method
then in trade executor class
class TradeExecutor():
def __init__(self,market_data: 'MarketDataLoader',trade_config:'TradeConfig',broker_config:'BrokerConfig'):
print("Init Trade execute")
self._market_data = market_data
self._trade_config = trade_config
self._broker_config =broker_config
def start_trade(self):
logger.info("starting trade from Trade Executor for ",self._trade_config.symbols,
" cash for trade ",self._trade_config.cash_per_trade)
while True: # repeat until we get all historical bars
time.sleep(10)
print("trade time ",self._market_data)
#error on following line of code
tick_frame = self._market_data.tick_data()
i am getting error on tick_frame = self._market_data.tick_data() i am not sure how to resolve following error
tick_frame = self._market_data.tick_data()
TypeError: 'DataFrame' object is not callable
python-BaseException
You're trying to call a method declared as a property.
When we use the #property decorator we treat the method as an attribute. For example, suppose we have the following class:
class JBug(object):
def __init__(self):
name = 'Junie'
#property
def hello(self):
return "Hello, my name is %s" % name
To get the desired behavior we do not call hello, but treat it as an attribute:
>>> J = JBug()
>>> print(J.hello)
Hello, my name is Junie
I'm trying to develop this code, for exercising Python with Object-oriented programming:
class Pessoa:
def __init__(self, nome, sobrenome):
self._nome = nome
self._sobrenome = sobrenome
#getters
#property
def nome(self):
return self._nome
#property
def sobrenome(self):
return self._sobrenome
#nome.setter
def setNome(self, nome):
self._nome = nome
#sobrenome.setter
def sobrenome(self, sobrenome):
self._sobrenome = sobrenome
def getNomeCompleto(self):
return self._nome + " " + self._sobrenome
pessoa1 = Pessoa(None, None)
pessoa1.nome("Mário")
pessoa1.sobrenome("Lopes")
print(pessoa1.getNomeCompleto())
When I run the code, I always get this error, related to the line 'pessoa1.nome("Mário")':
TypeError: 'NoneType' object is not callable
And I have no idea how to solve it. What I'm trying to call is the setter method for nome.
At the moment, your code tries to call the nome property on your class, which is initialized to None, raising the error you see.
Your setter for nome is currently incorrectly named - its name should match that of the property (getter):
#nome.setter
def nome(self, nome):
self._nome = nome
After making that correction, the property value should instead be set using:
pessoa1.nome = "Mário"
This will then proceed via the setter method you defined.
In full:
pessoa1.nome = "Mário"
pessoa1.sobrenome = "Lopes"
print(pessoa1.getNomeCompleto())
Output:
Mário Lopes
pessoa1.nome("Mário")
You are trying to pass an argument to an object field. Call your setter instead, i.e.
pessoa1.setNome("Mário")
I'm using property method in class to get a subset of user's input:
class deck:
def __init__(self, card_list):
self.cards = card_list
self.cards_slice = []
def get_deal_card(self):
return self.cards_slice
def set_deal_card(self, count):
if count > len(self.cards):
raise Exception("Process failed. Out of index.")
else:
self.cards_slice = self.cards.pop(count)
deal_card = property(get_deal_card, set_deal_card)
d1 = deck([['A',1],['B',1]])
d1.deal_card(0)
It returns type error "List object is not callable". I know this error occurs when I previously defined a list with a name that I used somewhere else. However I still can't see what I do wrong in this program.
Thanks!
table = QTableView()
model = QStandardItemModel()
table.setModel(model)
r = table.model().rowCount()
c = table.model().columnCount()
tLeft = table.model().index(0, 0)
bRight = table.model().index(r, c)
table.dataChanged(tLeft, bRight).connect(SomeFunction)
AttributeError: 'NoneType' object has no attribute 'connect'
Goal - to call SomeFunction when one of the items was changed directly by user in QTableView().
What do I do wrong? I see that NoneType object cannot has attribute connect, but why it is NoneType.
Please comment. I am a beginner. Qt5.
You should do:
table.model().dataChanged.connect(someFunction)
# or model.dataChanged.connect(someFunction)
No need to precise the arguments. The slot should look like this:
def someFunction(tLeft,bRight):
#do stuff
I'm trying to receive certain contents of a list called eeprom, and save them under a list called bytes.
class Parameter (object):
def __init__ (self, eeprom, *address):
self.eeprom = eeprom
self.bytes = list()
for a in address:
a = int(a, 16)
byte = eeprom[a] # here lies the problem
self.bytes.append(byte)
sthg = Parameter(eeprom, "0x00B9", "0x00BA")
Whenever I run the script, I get the following error:
TypeError: 'int' object has no attribute '__getitem__'
Does anyone has any idea of why this happens? If I write this on the interpreter it works, it is only when I run it as a module that I get this error.
When you are instantiating Parameter you are most likely passing an int in for the eeprom arguments instead of a list. You are probably doing the equivalent of
sthg = Parameter(1, "0x00B9", "0x00BA")
when you should be doing
sthg = Parameter([1,2,3,4], "0x00B9", "0x00BA")
When you are instantiating your class you should pass eeprom as a list argument but you are passing it as a int argument.
int object do not have any __getitem__() function that is why you can not access it using [] (indexing), so you should be having any iterable as a eeprom argument..
class Parameter (object):
def __init__ (self, eeprom, *address):
self.eeprom = eeprom
self.bytes = list()
for a in address:
a = int(a,16)
byte = eeprom[a] # here lies the problem
self.bytes.append(byte)
eeprom = [1,2,3,4,5,6,7,8,9,10,11]
sthg = Parameter(eeprom, "0x0009")
eeprom should be List object not integer an integer
Example:
eeprom = [1,2,3,4,5,6,7,8,9,10,11]