OpenStack: Heat Python Tutorial

In this tutorial, we’ll focus on how to interact with OpenStack Heat using Python.¬† Before deep diving into Heat Python examples, I suggest being familiar with Heat itself and more specifically:

  • Templates
  • Basic operations: create/delete/update stack

Still here? let’s go ūüôā

Continue reading “OpenStack: Heat Python Tutorial”

Python: Working with Jinja2 templates

In this post we’ll see how to¬†work with Jinja2 in your Python projects, to easily create and modify templates.

Jinja2 is widely used and you probably already noticed it if you used projects such as Flask and Ansible or even if you simply searched for a robust templating engine.I can describe its greatness for a couple of minutes if not hours, but the

I can describe its greatness for a couple of minutes if not hours, but the official doc does it much better, so I recommend to go over them to get an idea on what you can achieve by using it. This post is mainly for those who are looking for a quickstart guide.

Continue reading “Python: Working with Jinja2 templates”

Python: check if two strings are permutations of each other

Another question I cover in a presentation¬†I’m preparing¬†for¬†“Open University meets Open Source” meetups.

What is a Permutation?

Permutation is the action of rearranging objects, characters or symbols into different, unique sequences.

Each sequence is called ‘permutation’.

It’s common to see people mix permutation with combination, but those are two different things. Remember, combination doesn’t care about the order, while permutation does.

Continue reading “Python: check if two strings are permutations of each other”

Python: check if one string is a rotation of another string

Another question I cover in a presentation¬†I’m preparing¬†for¬†“Open University meets Open Source” meetups.

The question

You have two strings, str1 and str2 and you need to return True if the first string is a rotation of the second string. Otherwise, return False.

Examples

“hello”, ¬† ¬†“llohe” -> True. If we’ll rotate the second string twice, you’ll get ‘hello’.

Continue reading “Python: check if one string is a rotation of another string”

Python: Run daemon process

What is a daemon?

Except for nature spirit, a daemon is also a process running in the background,¬†meaning it’s a¬†non-interactive program. It’s detached from the terminal of an interactive user.

There is no easy way to identify which processes on the system are daemons. It’s common to think that processes with ppid (parent pid) of 1 are daemons, but you can easily create in your terminal interactive process with ppid of 1, meaning not all processes with ppid 1 are daemons.

Continue reading “Python: Run daemon process”

Python: Global Interpeter Lock

What is GIL?

GIL (Global interpreter Lock) is a mutex. Mutex is an mutual exclusion object used to¬†prevent from two threads or more, to access the same piece of code. ¬†This piece of code¬† also called ‘critical section’.

So GIL is Python’s way to ensure only one thread is running the code at any given time. It serializes the access to the interpreter and ensures that each thread gets exclusive access to the interpreter internals when running.

Continue reading “Python: Global Interpeter Lock”

Python: Lists vs. Tuples

For Python beginners, the differences between lists and tuples might not be as clear as to those who already got some experience using them.

python

Let’s review the key differences between the two. hopefully, it will help you choose the right type in your code and understand that although this post called ‘lists vs. tuples’ there is no real match here. each type has his own use case.

Mutability

Mutability talks about the capability of an object to change or alter. This is one of the noticeable difference between the two types.

First, let’s use list:

>>> my_list = [1,2,3]
>>> my_list[2] = 9
>>> print my_list
[1, 2, 9]

Now let’s try the same on tuple:

Continue reading “Python: Lists vs. Tuples”