Categories
Python Answers

How to create a simple XML file using Python?

Sometimes, we want to create a simple XML file using Python.

In this article, we’ll look at how to create a simple XML file using Python.

How to create a simple XML file using Python?

To create a simple XML file using Python, we can use the xml.etreer.cElementTree module.

For instance, we write

import xml.etree.cElementTree as ET

root = ET.Element("root")
doc = ET.SubElement(root, "doc")

ET.SubElement(doc, "field1", name="blah").text = "some value1"
ET.SubElement(doc, "field2", name="asdfasd").text = "some vlaue2"

tree = ET.ElementTree(root)
tree.write("filename.xml")

to create an element with the ET.Element class.

And we create a child element with the ET.SubElement class.

Then we set the text content of the elements by setting the text property to the values we want.

Then we create the DOM tree object with ET.ElementTree.

And we call write to write the file to the path we called write with.

Conclusion

To create a simple XML file using Python, we can use the xml.etreer.cElementTree module.

Categories
Python Answers

How to adjust padding with cutoff or overlapping labels with Python matplotlib?

Sometimes, we want to adjust padding with cutoff or overlapping labels with Python matplotlib.

In this article, we’ll look at how to adjust padding with cutoff or overlapping labels with Python matplotlib.

How to adjust padding with cutoff or overlapping labels with Python matplotlib?

To adjust padding with cutoff or overlapping labels with Python matplotlib, we call plt.savefig with the bbox_inches argument set to 'tight'.

For instance, we write

plt.savefig('myfile.png', bbox_inches="tight")

to call savefig to save the flots to myfile.png.

We set bbox_inches to 'tight' to reduce padding between the plots.

Conclusion

To adjust padding with cutoff or overlapping labels with Python matplotlib, we call plt.savefig with the bbox_inches argument set to 'tight'.

Categories
Python Answers

How to test that a Python function throws an exception?

Sometimes, we want to test that a Python function throws an exception.

In this article, we’ll look at how to test that a Python function throws an exception.

How to test that a Python function throws an exception?

To test that a Python function throws an exception, we can use the assertRaises method.

For instance, we write

import my_mod

#...

class MyTestCase(unittest.TestCase):
    def test1(self):
        self.assertRaises(SomeException, my_mod.my_func)

to create the MyTestCase test class.

In it, we define the test1 test method that calls assetRaises with the exception we want to check for and the function that throws the exception respectively.

Conclusion

To test that a Python function throws an exception, we can use the assertRaises method.

Categories
Python Answers

How to get the logical xor of two variables in Python?

Sometimes, we want to get the logical xor of two variables in Python.

In this article, we’ll look at how to get the logical xor of two variables in Python.

How to get the logical xor of two variables in Python?

To get the logical xor of two variables in Python, we can use the ^ operator.

For instance, we write

bool(a) ^ bool(b)

to return the xor result of 2 booleans with ^.

Conclusion

To get the logical xor of two variables in Python, we can use the ^ operator.

Categories
Python Answers

How to programmatically generate video or animated GIF in Python?

Sometimes, we want to programmatically generate video or animated GIF in Python.

In this article, we’ll look at how to programmatically generate video or animated GIF in Python.

How to programmatically generate video or animated GIF in Python?

To programmatically generate video or animated GIF in Python, we can use the imageio library.

To install it, we run

pip install imageio

Then we can use it by writing

import imageio
with imageio.get_writer('/path/to/movie.gif', mode='I') as writer:
    for filename in filenames:
        image = imageio.imread(filename)
        writer.append_data(image)

to create an animated GIF and save it to /path/to/movie.gif with imageio.getwriter.

In the with block, we loop through the filenames list to add the images at filename into the animated GIF with

image = imageio.imread(filename)
writer.append_data(image)

We read the image with imageio.imread and call append_data to append the image as a frame in the animated GIF.

Conclusion

To programmatically generate video or animated GIF in Python, we can use the imageio library.