Namespaces
The lone zen, namespaces are just import statements written in ways that don’t cause conflicts. In this example, there are two libraries, lib1 and lib2, both containing methods named example. What would be the solution that allows both of the methods to be imported into one Python file? You can just change one or both of their names to unique namespaces:
- Code without namespaces:
from lib1 import example
from lib2 import example
This is bound to cause conflicts “””
- Code with namespaces:
from lib1 import example as ex1
from lib2 import example as ex2
#This won’t cause conflicts
A honking great idea indeed.
Through these principles, you can observe how Python has evolved into the language that it is and how it has distinguished itself from all the other programming languages. These changes have also helped make Python a language that aligns itself with DevOps principles. So, let’s now observe the marriage between the principles behind Python and DevOps and how they are mutually beneficial to each other.
What Python offers DevOps
In the previous section, we focused on the principles of Python. Now, we are going to look into what following those principles offers DevOps as a practice and DevOps engineers in general. The principles behind DevOps and Python are more similar than they are different. They both share an emphasis on flexibility, automation, and conciseness. This makes Python and DevOps a perfect pairing in the field of DevOps. Even for DevOps professionals who may not have the sharpest coding skills, Python is easy to pick up, easy to use, and can be integrated with practically every tool and platform because almost all these platforms have native support and libraries in Python.
I previously stated that the reason that Python is so pervasive in DevOps is that it handles data that resides between curly brackets ({}) better than almost any other language. The offerings of Python for DevOps are numerous and will be covered in further detail in future chapters. Right now, we will go over some of these offerings in brief.
Operating systems
Python has native libraries that interact with the OS of any server that it is currently working on. These libraries allow for programmatic access to various OS processes. This is especially useful when you work with virtual machines on the cloud (such as with Amazon EC2). You can do things such as the following:
- Set environment variables in the OS
- Get information about files or directories
- Manipulate, create, or delete files and directories
- Kill or spawn processes and threads
- Create temporary files and file locations
- Run Bash scripts
OSs are nice and all, but they can be difficult to maintain in a desired state with ideal resource usage. For this challenge, we have a common solution in containerization.