pyvisa error: 'list' object has no attribute 'endswith' - python

I'm trying to send a command to an instrument using pyvisa - but I'm getting the following error when I run the python script:
cmd.endswith = 0 AttributeError: 'list' object has no attribute
'endswith'
This following is the code that is receiving the above error:
import time
import visa
rm=visa.ResourceManager()
vi=rm.open_resource('ASRL1::INSTR')
cmd = [0xAA,0,0x20,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xcb]
cmd.endswith = 0
vi.write(cmd)
vi.read()
Any suggestions on how to get rid of the error effectively?

The endswith function only works for strings. What I think you meant to do was to iterate through your list and check if it endswith 0. This is why you get an error saying that lists have no attribute endswith, because they do not. Only strings do.
Also, endswith is used by: listname.endswith(ending) which returns either True or False.
Hope it helps.

Related

Why is format(str(var)) giving me an Attribute Error for my os.system

I'm trying to make a Python script that connects me to a VPN Server 'number' (with this number as a variable)
I wrote:
import os
VPNServer = 0
VPNServer += 1
os.system("networksetup -connectpppoeservice 'VPNServer {servernumber}'").format(servernumber = str(VPNServer))
print("→ Successfully connected to", "VPNServer", VPNServer)
But everytime I try to run it, the console gives me an AttributeError
AttributeError: 'int' object has no attribute 'format'
I don't understand it because I took the string version of the variable
If someone could help, it would be super cool
I'm on macOS and I'm using Python 3.8.1
In the snippet you provided, you write
os.system('<some string>').format(args)
You are making the format call on the return value of os.system, which happens to be an integer. This is identical to writing e.g.
5.format(args)
Since int objects have no attribute format, you get the AttributeError you described.
What you want to write is
os.system('<some string>'.format(args))
In this specific case, your snippet should resemble
os.system(
"networksetup -connectpppoeservice 'VPNServer {servernumber}'"
.format(servernumber=VPNServer)
)
Note that the str(VPNServer) call is superfluous, since format will autmatically call the __str__ method of the object provided.

Gensim Attribute Error when trying to use pre_scan on a doc2vec object

I am following the tutorial here:
https://github.com/RaRe-Technologies/gensim/blob/develop/docs/notebooks/doc2vec-wikipedia.ipynb
But when I get to this part:
pre = Doc2Vec(min_count=0)
pre.scan_vocab(documents)
I get the following error on scan_vocab:
AttributeError: 'Doc2Vec' object has no attribute 'scan_vocab'
Does anyone know how to fix this? Thanks.
That's a known problem after a 2018 refactoring of the Doc2Vec code:
https://github.com/RaRe-Technologies/gensim/issues/2085
You can just skip that cell to proceed with the rest of that demo notebook. (If you really needed to adjust the min_count using the info from a full-scan, you might be able to call some internal classes/methods mentioned in the above issue.)

pyvmomi:error when calling RelocateVM

Hi I am using pyvmomi API, to perform vmotions against a cluster when DRS is set to manual mode. I am going through a vcenter and querying a cluster and getting recommendation and using that to perform the Vmotions. The code is something like this.
content=getVCContent(thisHost, {'user':username,'pwd':decoded_password},logger)
allClusterObj = content.viewManager.CreateContainerView(content.rootFolder, [pyVmomi.vim.ClusterComputeResource], True)
allCluster = allClusterObj.view
for thisDrsRecommendation in thisCluster.drsRecommendation:
print thisDrsRecommendation.reason
for thisMigration in thisDrsRecommendation.migrationList:
print ' vm:', thisMigration.vm.name
while True:
relocate_vm_to_host(thisMigration.vm.name,thisMigration.destination.name, allClusterObj.view)
#FUNCTION definition
def relocate_vm_to_host(vm, host , allCluster):
for thisCluster in allCluster:
for thisHost in thisCluster.host:
if thisHost.name == host:
for thisVm in thisHost.vm:
print 'Relocating vm:%s to host:%s on cluster:%s' %(thisVm.name,thisHost.name,thisCluster.name)
task = thisVm.RelocateVM(priority='defaultpriority')
I am getting an error saying the attribute doesn't exist.
AttributeError: 'vim.VirtualMachine' object has no attribute 'RelocateVM'
But the pyvmomi documentaion here https://github.com/vmware/pyvmomi/blob/master/docs/vim/VirtualMachine.rst
has a detailed explanation for the method
RelocateVM(spec, priority):
Anyone know what's the reason the method is missing? I also tried checking the available methods of the object ,that has RelocateVM_Task ,instead of RelocateVM(for which I couldn't find documentation) When I used that I get this error
TypeError: For "spec" expected type vim.vm.RelocateSpec, but got str
I checked the documentation for vim.vm.RelocateSpec, I am calling it in a function , but still throws an error.
def relocate_vm(VmToRelocate,destination_host,content):
allvmObj = content.viewManager.CreateContainerView(content.rootFolder, [pyVmomi.vim.VirtualMachine], True)
allvms = allvmObj.view
for vm in allvms:
if vm.name == VmToRelocate:
print 'vm:%s to relocate %s' %(vm.name , VmToRelocate)
task = vm.RelocateVM_Task(spec = destination_host)
Any help is appreciated.
Thanks
Looks like a mistake in the documentation. The method is called Relocate (and not RelocateVM).
Note, BTW, that in your first sample you're not passing the destination host to the call to Relocate so something is definitely missing there.
You can see some samples at https://gist.github.com/rgerganov/12fdd2ded8d80f36230f or at https://github.com/sijis/pyvmomi-examples/blob/master/migrate-vm.py.
Lastly, one way to realize you're using the wrong name is by calling Python's dir method on a VirtualMachine object. This will list all properties of the object so you can see which methods it has:
>>> vm = vim.VirtualMachine('vm-1234', None)
>>> dir(vm)
['AcquireMksTicket', [...] 'Relocate', 'RelocateVM_Task', [...] ]
(abbreviated output)

AttributeError: 'list' object has no attribute 'updateItem'

I am working on map automation using arcpy.
I need to add a legend on the map layout based on the layers added to the mxd.I am using the code below (as given on the tutorial):
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyrFile = arcpy.mapping.Layer(r"C:\Project\Data\Rivers.lyr")
arcpy.mapping.AddLayer(df, lyrFile, "TOP")
styleItem = arcpy.mapping.ListStyleItems("USER_STYLE", "Legend Items", "NewDefaultLegendStyle")[0]
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
legend.updateItem(lyrFile, styleItem)
But everytime I run this code i get the following error:
Runtime error
Traceback (most recent call last):
File "<string>", line 1, in <module>
AttributeError: 'list' object has no attribute 'updateItem'
What could cause this error to appear?
What could cause this error to appear?
Well, I am not familiar with arcpy, but it seems the 0th element of whatever ListLayoutElements() returns is a list which indeed has no updateItem() method.
You might want to .append() to the list, or you might want to have a different type of object.
Your code is the same as ArcGIS Help example,
http://resources.arcgis.com/zh-cn/help/main/10.2/index.html#//00s30000006z000000
I tested the example code and it ran correctly.
By the way, I am wondering if you had pasted your own code. Otherwise you probably encounter problem in line 2,4,6 rather than the last line.
As the user2357112 suggested, you'd better try it again with clean code. Or you can confirm the type of the variable "legend" just by print type(legend)before the line
legend.updateItem(lyrFile, styleItem)

Python Blender math.trunc()

iam trying to use the math.trunc in Blender 2.49b Python
but iam getting this error
AttributeError: 'module' object has no attribute 'trunc'
i also imported math
its on line
uv[i][0] = trunc(uv[i][0] * 100000) / 100000
i also tryied it via the int, like
uv[i][0] = int(uv[i][0] * 100000) / 100000
which gives me an error
TypeError: 'float' object is
unsubscriptable
so how should i trunc the value:(
thank you
The second error seems to imply that uv in your code is a float object and you are trying to subscript it uv[i]. Try to math.trunc(uv) and see. Also you can check if trunc is available by doing hasattr(math,'trunc')
It might depend what verson of Python is used by Blender (I imagine that would be Python 2.5).
Try this in Blender:
import math
help(math)
This will crash Blender, but you will be able to see the math to the library under FILE and you should be able to scroll down to see if the trunc function is available in the version of Python used by Blender. It might be not present, which would explain the error.

Categories

Resources