Arie Bregman

Linux And Stuff

Category: Python (page 2 of 2)

Python: How to use signal handlers

What is a signal?

Software interrupts sent to the program to notify on significant event or request the program  to run special sequence .

For example, the following command uses SIGHUP signal to request the program to reload it’s configuration

Continue reading

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 daemonas. 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: 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: 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:

Now let’s try the same on tuple:

Continue reading

Python: Check whether a string is Palindrome or not

A palindrome is a sequence of characters which reads the same backward or forward.

For example dad, mom, kayak, a santa at nasa, anna. Not palindromes: mario, luigi, ice cream.

There is more than one way in python to check whether given string is palindrome or not. Let’s have a look.

Method 1

defiantly one of the shortest ways to do it.

[::-1] taking care of inverting the string and all is left is simply compare between the original to inverted string.

Method 2

Slower than the first method:

Method 3

My least favorite but quite common (especially in other languages)

Q&A

Q: How can I handle case-sensitive palindrome?

A: you can use upper() when comparing the reversed and the original strings.

Newer posts

© 2017 Arie Bregman

Theme by Anders NorenUp ↑