Categories
Python Answers

How to remove axis, legends and white spaces in Python Matplotlib plots?

Sometimes, we want to remove axis, legends and white spaces in Python Matplotlib plots.

In this article, we’ll look at how to remove axis, legends and white spaces in Python Matplotlib plots.

How to removing axis, legends and white spaces in Python Matplotlib plots?

To remove axis, legends and white spaces in Python Matplotlib plots, we can call axis and savefig with some options.

For instance, we write

from numpy import random
import matplotlib.pyplot as plt

data = random.random((5,5))
img = plt.imshow(data, interpolation='nearest')
img.set_cmap('hot')
plt.axis('off')
plt.savefig("test.png", bbox_inches='tight')

to call plt.axis with 'off' to remove the axes.

And we call savefig with bbox_inches to 'tight' to remove whitespaces.

Conclusion

To remove axis, legends and white spaces in Python Matplotlib plots, we can call axis and savefig with some options.

Categories
Python Answers

How to avoid “RuntimeError: dictionary changed size during iteration” error with Python?

Sometimes, we want to avoid "RuntimeError: dictionary changed size during iteration" error with Python.

In this article, we’ll look at how to avoid "RuntimeError: dictionary changed size during iteration" error with Python.

How to avoid "RuntimeError: dictionary changed size during iteration" error with Python?

To avoid "RuntimeError: dictionary changed size during iteration" error with Python, we’ve to make a copy of the dict before we can modify the original in a loop.

For instance, we write

for i in list(d):
    # ...

to call list with dict d to make a copy of d and get the keys from the copied dict.

Conclusion

To avoid "RuntimeError: dictionary changed size during iteration" error with Python, we’ve to make a copy of the dict before we can modify the original in a loop.

Categories
Python Answers

How to get MD5 hash of big files in Python?

Sometimes, we want to get MD5 hash of big files in Python.

In this article, we’ll look at how to get MD5 hash of big files in Python.

How to get MD5 hash of big files in Python?

To get MD5 hash of big files in Python, we can use the hashlib module.

For instance, we write

import hashlib
with open("your_filename.txt", "rb") as f:
    file_hash = hashlib.md5()
    while chunk := f.read(8192):
        file_hash.update(chunk)
print(file_hash.digest())
print(file_hash.hexdigest())

to read the your_filename.txt file with open as a binary file.

Then we call hashlib.md5 to create the file_hash object.

Next, we call f.read to read the file 8192 bytes as a time.

In the loop, we call file_hash.update to update the hash with the file chunk.

Then we can get the digest and hex digest of the file with digest and hexdigest, which are bytes and strings respectively.

Conclusion

To get MD5 hash of big files in Python, we can use the hashlib module.

Categories
Python Answers

How to pass functions with arguments to another function in Python?

Sometimes, we want to pass functions with arguments to another function in Python.

In this article, we’ll look at how to pass functions with arguments to another function in Python.

How to pass functions with arguments to another function in Python?

To pass functions with arguments to another function in Python, we can create functions that takes a function as an argument.

For instance, we write

def perform(fun, *args):
    fun(*args)

def action1(args):
    # ...

def action2(args):
    # ...

perform(action1)
perform(action2, p)
perform(action3, p, r)

to create the perform function that has the fun function parameter.

And the rest of the parameters we use to call fun with are in the args list.

We use the values in the args list as arguments with *.

Then we call perform as we do in the last 3 lines to call action1 and action2 with the arguments that comes after it.

Conclusion

To pass functions with arguments to another function in Python, we can create functions that takes a function as an argument.

Categories
Python Answers

How to do recursive folder read with Python?

Sometimes, we want to do recursive folder read with Python.

In this article, we’ll look at how to do recursive folder read with Python.

How to do recursive folder read with Python?

To do recursive folder read with Python, we can use the iglob method.

For instance, we write

import glob

for filename in glob.iglob(root_dir + '**/*.txt', recursive=True):
     print(filename)

to call glob.iglob with the pattern of the files we’re looking for and the recursive arguemnt set to True to traverse the files results.

We get an iterator from iglob and use a for loop to loop through the results.

And we print the filename returned in the loop.

Conclusion

To do recursive folder read with Python, we can use the iglob method.