AttributeError: 'str' object has no attribute 'as_plain_ordered_dict' - python

I am trying to mock "ConfigFactory.parse_file" for unit testing purpose and getting the following error.
AttributeError: 'str' object has no attribute 'as_plain_ordered_dict'
I tried mocking the attribute as_plain_ordered_dict to resolve the issue, but still getting the same error.
class Testt2StatsExporter(unittest.TestCase):
#patch('t2.client.OverlayClient')
#patch('pyhocon.ConfigFactory.parse_file')
#patch('oci.auth.signers.instance_principals_security_token_signer.InstancePrincipalsSecurityTokenSigner')
def test_create_telemetry_client(self, mock_config_factory, mock_security_token_signer, mock_client):
test_config = Mock()
test_config.json.return_value = {'metricsConfig': 'test_config'}
path_to_local_config = Mock()
type(path_to_local_config).as_plain_ordered_dict = PropertyMock(return_value=test_config)
mock_security_token_signer.return_value = "test_token"
mock_config_factory.return_value = test_config
mock_client.return_value = True
self.assertEqual(True, _create_telemetry_client(path_to_local_config))
and here is the main code base that I am testing.
path_to_local_config = '/etc/dlp/t2_config.conf'
def _create_telemetry_client(path_to_local_config: str):
t2config = ConfigFactory.parse_file(path_to_local_config).as_plain_ordered_dict()
common_config = t2config['metricsConfig']
t2_client_config = {'metricsConfig': common_config}
auth_provider = InstancePrincipalsSecurityTokenSigner()
return client.OverlayClient(t2_client_config, authentication_provider=auth_provider)
What can I try next?

Related

How to access the instance variable?

I have used a 'class' named 'CMS'. Also,i have defined a method def ConnectDB(),Where i have loaded the driver and created a cursor object to execute a query.Whenever i try to access the cursor object,it's giving me an attribute error.
obj.cursor.execute(sql,v)
AttributeError: CMS instance has no attribute 'cursor'
How do I resolve it?Refer the code below to understand the problem statement clearly.Thanks in advance.
class CMS:
def ConnectDB(self):
server = '192.121.063.012'
database = 'fghr_db'
username = 'abcdef'
password = 'zzzzzzz'
connStr = 'DRIVER=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql- 17.2.so.0.1;SERVER='+server+';DATABASE='+database+';'+ 'UID='+username+'; PWD='+ password+';Port=1270'
cnxn = pyodbc.connect(connStr)
cursor = cnxn.cursor()
def ReadCsv(self,filepath):
data = csv.reader(open(filepath))
print data
h=data.next()
result=[]
for x in data:
c=list(x)
result.append(c)
return result
obj=CMS()
obj.ConnectDB()
sql="INSERT INTO StudentCsv VALUES (?,?,?,?,?)"
z=obj.ReadCsv("Student.csv")
for v in z:
print v
obj.cursor.execute(sql,v)
obj.cnxn.commit()
ERROR getting like below:
obj.cursor.execute(sql,v) AttributeError: CMS instance has no
attribute 'cursor'

AttributeError: 'builtin_function_or_method' object has no attribute 'translate'

I am using Ubuntu as OS, pymysql, flask, sqlalchemy frameworks. Then I do GET request I have this error:
File "/srv/MSH/venv/local/lib/python2.7/site-packages/pymysql/converters.py", line 73, in _escape_unicode
return value.translate(_escape_table)
AttributeError: 'builtin_function_or_method' object has no attribute 'translate'
My routes.py code:
#app.route('/device/<dev_id>/<name>/<dev_type>/<state>', methods=['GET'])
def device(dev_id, state, dev_type, name):
# This function communicates with device
dev_state = 0
device = Devices.query.filter_by(dev_id=id).first()
# Check state
if state == "0":
state = False
if state == "1":
state = True
if device is None:
# if we don't know device write to database
dev = Devices(dev_id=dev_id, type=dev_type, prev_state=state, name=name)
db.session.add(dev)
db.session.commit()
return "hi"
else:
# Write data to database and return wanted state
device.prev_state = state
if device.state:
dev_state = 1
if not device.state:
dev_state = 0
db.session.commit()
return str(dev_state)
Wrong line here:
device = Devices.query.filter_by(dev_id=id).first()
You are query sql with something identical to internal python function. I think correct string is:
device = Devices.query.filter_by(dev_id=dev_id).first()

'numpy.ndarray' object has no attribute 'where'

I have problem at my code.
This is part of __init__:
file = np.loadtxt(file_path,delimiter = ",",skiprows=1)
header = np.loadtxt(file_path,delimiter = ",",dtype=str)
self.data = file
self.header = header[0]
This is part of a method:
def hello(self,col_name)
col = self.header.where[a == col_name]
return col
Then I wanted to check if I get the index of the header:
print (name.hello('hi'))
but got: "AttributeError: 'numpy.ndarray' object has no attribute 'where'"

pyqt4: AttributeError: 'QPlainTextEdit' object has no attribute 'text'

I'm aware that there are similar problems to mine but I tried those solutions and they don't work.
I have text field:
self.tMail = QtGui.QPlainTextEdit(self.centralwidget)
self.tMail.setGeometry(QtCore.QRect(50, 270, 451, 75))
self.tMail.setAccessibleName(_fromUtf8(""))
self.tMail.setInputMethodHints(QtCore.Qt.ImhNone)
self.tMail.setPlainText(_fromUtf8(""))
self.tMail.setOverwriteMode(False)
self.tMail.setObjectName(_fromUtf8("tMail"))
And i want to add them to the variable string by:
def handleButton(self):
timeString = self.tCzas.text()
mailString = self.tMail.text()
IDString = self.tID.text()
teamString = self.tTeam.text()
print(timeString)
print(mailString)
print(IDString)
print(teamString)`
also I tried:
mailString = self.tMail.plainText()
and I always get an error:
AttributeError: 'QPlainTextEdit' object has no attribute '...'
Why?
QPlainTextEdit doesn't have a text() function. try using the toPlainText() function:
def handleButton(self):
timeString = self.tCzas.toPlainText()
mailString = self.tMail.toPlainText()
IDString = self.tID.toPlainText()
teamString = self.tTeam.toPlainText()
print(timeString)
print(mailString)
print(IDString)
print(teamString)

AttributeError: 'Series' object has no attribute 'Id'

I am trying to create a malware classifier and I am experiencing the error
AttributeError: 'Series' object has no attribute 'Id. Not sure of the error.
Traceback (most recent call last):
File"C:/Users/Afiqmatters/PycharmProjects/MajorProject/feature_extraction.py", line 23, in <module>
rids = [mids.loc[i].Id for i in rchoice]
File "C:\Users\Afiqmatters\Miniconda\lib\site-packages\pandas\core\generic.py", line 2744, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'Id'
Here is the codes I have up to the error.
rs = Random()
rs.seed(1)
trainLabels = pd.read_csv('trainLabels.csv')
#print trainLabels
fids = []
opd = pd.DataFrame()
for label in range(1,10):
mids = trainLabels[trainLabels.Class == label]
mids = mids.reset_index(drop=True)
#print mids
rchoice = [rs.randint(0, len(mids) - 1) for i in range(10)]
print len
#print rchoice
rids = [mids.loc[i].Id for i in rchoice]
The error happens at rids = [mids.loc[i].Id for i in rchoice] and I am not sure of the error.
A sample of what is stored in the trainLabels.csv
Id Class
0A32eTdBKayjCWhZqDOQ 2
mids.loc[i] in this context is a "Series" object which is detailed here
This object type does not have an attribute Series.Id, so that's why you're seeing this error.
Did you mean to call the built-in function id( ) on the object?
rids = [id(mids.loc[i]) for i in rchoice]

Categories

Resources