I was using parse function to modify a xml and it worked but I tried to use .fromstring and it showed an error
AttributeError: 'Element' object has no attribute 'getroot'
here is the part of code.
AttributeError: 'Element' object has no attribute 'getroot'
This is because the fromstring method returns the root object already. SO whatever your var that stores the information form the "fromstring()" method is, that var is the root.
Related
Upon implementing torchaudio.info on a filepath, I am getting a different return than specified in the documentation.
The code here returns <torchaudio.backend.common.AudioMetaData object at 0x000001908CFB3B20>
tempFile = os.path.join(audio_dir,'1','121007.wav')
metadata=torchaudio.info(tempFile)
print(metadata)
while I need a return of the following form as the documentation specifies
AudioMetaData(sample_rate=44100, num_frames=109368, num_channels=2, bits_per_sample=16, encoding=PCM_S)
Probably the version issue. v0.8 did not have __str__ method. It was added in v0.9.
https://github.com/pytorch/audio/pull/1339
I have a java class registered in PySpark, and Im trying to pass a Broadcast variable from PySpark to a method in this class. Like so:
from py4j.java_gateway import java_import
java_import(spark.sparkContext._jvm, "net.a.b.c.MyClass")
myPythonGateway = spark.sparkContext._jvm.MyClass()
with open("tests/fixtures/file.txt", "rb") as binary_file:
data = spark.sparkContext.broadcast(binary_file.read())
myPythonGateway.setData(data)
But this is throwing:
AttributeError: 'Broadcast' object has no attribute '_get_object_id'
However, if I pass the byte[] directly, without wrapping it in broadcast(), it works fine. But I need this variable to be broadcast, as it will be used repeatedly.
According to the py4j docs, the above error will be thrown if you try to pass a Python collection to a method that expects a Java collection. The docs give the following solution:
You can explicitly convert Python collections using one of the following converter located in the py4j.java_collections module: SetConverter, MapConverter, ListConverter.
An example is provided there also.
Presumably, this error is occurring when py4j tries to convert the value attribute of the Broadcast object, so converting this may fix the problem e.g.
converted_data = ListConverter().convert(binary_file.read(),spark.sparkContext._jvm._gateway_client)
broadcast_data = spark.sparkContext.broadcast(converted_data)
myPythonGateway.setData(broadcast_data)
I am trying to create a P4.MergeData object to use with a resolver. I get this error (in title) when I do:
import P4
md = P4.MergeData()
How do I create this object? Or if I don't need to, how do I merge two files in general?
Run the p4 resolve command, passing a Resolver object to p4.run_resolve(). Your P4Resolver's resolve() method will be called with a MergeData object that describes the resolve operation.
https://www.perforce.com/manuals/p4python/Content/P4Python/python.p4_resolver.html#Class_P4.Resolver
I'm trying to write a unit test mock opening a file and passing it into a function that uses it to dump a JSON object into. How do I create a fake IO object that mimics the behavior of an open file handle but uses similar attributes, specifically .name?
I've read through tons of answers on here and all of them work around the problem in various ways. I've tried mock patching builtins.open, I've tried mock patching the open being called inside my module, but the main error I keep running into is that when I try to access the fake IO object's .name attribute, I get an AttributeError:
AttributeError: 'CallbackStream' object has no attribute 'name'
So here's a simple function that writes a dictionary to disk in JSON format and takes an open file handle:
def generate(data, json_file):
# data is a dict
logging.info(f"Writing out spec file to {json_file.name}")
json.dump(data, json_file)
Here's what I've tried to unit test:
#patch("builtins.open", new_callable=mock_open())
def test_generate_json_returns_zero(self, mock_open):
mocked_file = mock_open()
mocked_file.name = "FakeFileName"
data = {'stuff': 'stuff2'}
generate(data, json_file=mocked_file)
However, that produces the AttributeError above, where I can't use json_file.name because it doesn't exist as an attribute. I thought setting it explicitly would work, but it didn't.
I can bypass the issue by using a temporary file, via `tempfile.TemporaryFile:
def test_generate_json_returns_zero(self, mock_open):
data = {'stuff': 'stuff2'}
t = TemporaryFile("w")
generate(data, json_file=t)
But that doesn't solve the actual problem, which is that I can't figure out how to mock the file handle so that I don't actually need to create a real file on disk.
I can't get past the .name not being a valid attribute. How do I mock the file object such that I could actually use the .name attribute of an IO object and still fake a json.dump() to it?
The new_callable parameter is meant to be an alternative class of Mock, so when you call:
#patch("builtins.open", new_callable=mock_open())
It patches builtins.open by replacing it with what mock_open() returns, rather than a MagicMock object, which is what you actually need, so change the line to simply:
#patch("builtins.open")
and it should work.
Your test never actually calls open, so there's no need to patch it. Just create a Mock instance with the attribute you need.
def test_generate_json_returns_zero(self):
mocked_file = Mock()
mocked_file.name = "FakeFileName"
data = {'stuff': 'stuff2'}
generate(data, json_file=mocked_file)
I am using BeautifulSoup in python3.6. I am not getting an error while using a string as a parameter in the find method.
bs.find('div',attrs={'class' : 'ptag'}).text
but i am getting error while using variable instead of string directly.
bs.find('div',{'class' : ptagclass})
Error:
data=(bs.find('div',{'class' : ptagclass}).text)
AttributeError: 'NoneType' object has no attribute 'text'
The problem relies upon in the way you are trying to find the parameter, try replacing
bs.find('div',{'class' : ptagclass})
with
bs.find('div',{'class' : "ptagclass"}) this should solve the issue.
Hope this helps