Categories
Python Answers

How to explicitly free memory in Python?

Sometimes, we want to explicitly free memory in Python.

In this article, we’ll look at how to explicitly free memory in Python.

How to explicitly free memory in Python?

To explicitly free memory in Python, we can use the gc.collect method.

For instance, we write

import gc

del foo
del bar
gc.collect()

to use the del operator to remove references of foo and bar from memory.

And then we call gc.collect to free up the memory.

Conclusion

To explicitly free memory in Python, we can use the gc.collect method.

Categories
Python Answers

How to import modules from parent folder with Python?

Sometimes, we want to import modules from parent folder with Python.

In this article, we’ll look at how to import modules from parent folder with Python.

How to import modules from parent folder with Python?

To import modules from parent folder with Python, we register the module’s folder with sys.path.insrt.

Then we can import the module.

For instance, we write

import os
import sys
import inspect

currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0, parentdir) 

import mymodule

to call os.path.dirname with os.path.abspath(inspect.getfile(inspect.currentframe())) to get the path of the current file’s directory.

And then we get the parent’s path with os.path.dirname(currentdir).

Then we register the parent’s path with sys.path.insert(0, parentdir) .

Finally we import the module in parentdir with

import mymodule

Conclusion

To import modules from parent folder with Python, we register the module’s folder with sys.path.insrt.

Then we can import the module.

Categories
Python Answers

How to get file creation and modification date/times with Python?

Sometimes, we want to get file creation and modification date/times with Python.

In this article, we’ll look at how to get file creation and modification date/times with Python.

How to get file creation and modification date/times with Python?

To get file creation and modification date/times with Python, we can use the getmtime and getctime methods.

For instance, we write

import os.path, time

print("last modified: %s" % time.ctime(os.path.getmtime(file)))
print("created: %s" % time.ctime(os.path.getctime(file)))

to get the last modified datetime of file with os.path.getmtime.

Likewise, we use os.path.getctime to get the file‘s created datetime.

Conclusion

To get file creation and modification date/times with Python, we can use the getmtime and getctime methods.

Categories
Python Answers

How to parse XML with namespace in Python via ‘ElementTree’?

Sometimes, we want to parse XML with namespace in Python via ‘ElementTree’.

In this article, we’ll look at how to parse XML with namespace in Python via ‘ElementTree’.

How to parse XML with namespace in Python via ‘ElementTree’?

To parse XML with namespace in Python via ‘ElementTree’, we can use ther findall method.

For instance, we write

namespaces = {'owl': 'http://www.w3.org/2002/07/owl#'} 
root.findall('owl:Class', namespaces)

to call findall with 'owl:Class' and the namespaces dict with all the namespaces that we want to extract from the root XML object.

Conclusion

To parse XML with namespace in Python via ‘ElementTree’, we can use ther findall method.

Categories
Python Answers

How to have one color bar for all subplots with Python?

Sometimes, we want to have one color bar for all subplots with Python.

in this article, we’ll look at how to have one color bar for all subplots with Python.

How to have one color bar for all subplots with Python?

To have one color bar for all subplots with Python, we can use matplotlib’s subplots_adjust and colorbar methods.

For instance, we write

import numpy as np
import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=2, ncols=2)
for ax in axes.flat:
    im = ax.imshow(np.random.random((10,10)), vmin=0, vmax=1)

fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
fig.colorbar(im, cax=cbar_ax)

plt.show()

to call subplots_adjust on the fig subplot with the right argument to adjust the position of the subplots.

Then we add the color bar into its own axis with

fig.colorbar(im, cax=cbar_ax)

Conclusion

To have one color bar for all subplots with Python, we can use matplotlib’s subplots_adjust and colorbar methods.