24 December 2017

Process Management in Python - os.wait()

This is a continuation of series of posts on Process management in Python. Read this and this before continuing further. In this post, we are going to see
  1. How the parent process can get the status of its child after the child has exited using "os.wait"
  2. What happens to the child processes after they exit but the parent is still alive and did not get the status (defunct - meaning that userspace is cleared but a small about of information in kernel space is retained so as to give the information when the parent requests the status which may potentially lead to inability to create new processes)
Getting the status of the child processes is simple. Just call os.wait() to get status of any child processes or waitpid to get status of a specific process. Note, this is blocking call meaning that the parent (thread) waits here until the child is exited/terminated.

For more information on wait/waitpid, refer - https://en.wikipedia.org/wiki/Wait_(system_call)

Refer the Code in GIT Hub

Here is the code in Python (being Python, it is self-explanatory :-))


23 December 2017

Process Management in Python - os.exec

This is continuation of the previous post - Process Management in Python - os.fork()

In Linux, running a command from the shell is a two-step process. The shell first creates a process using fork (posted earlier) and overlays a new program using "exec". For example, when you want to run the command "ls" from the shell - the shell forks a copy of its own and immediately loads the binary of "ls" and starts the execution again.


Beware, you cannot find a function exec() but rather it is a family of functions with names such as - execl, execle, execlp, execv. The functions are almost same and they differ only in parameters that are being passed. I have used one such functions in the following code. When you run this code, you should see a directory listing - the current process upon the successful calling of exec, loads "ls" and executes it. Once "ls" is executed, it exits (like it normally used to do)

As an experiment, you can do fork to create a child process and in the child process execution flow, you can overlay a new program.

Refer the Code in GIT Hub

Try running this script from Linux shell and see the output. Do not forget to share, like, comment if you like this post :-)


22 December 2017

Process Management in Python - os.fork()

Inspired by https://pymotw.com/3/signal/index.html, I thought of exploring os package in Python. The next few posts would be on process management and how to do it in Python. I would be showing you the code that is tiny. The goal is to provide basic code and I leave the improvision to you.

There are four important system calls - fork, exec, wait and exit that are related to process management. Interestingly Python provides equivalent APIs as a part of os package so that you can do a lot of system programming (and use it in a cool way). In this post, let us see some code that uses "os.fork" to create a new process.

os.fork() - creates a new process (often called as parent-child processes). The following code has enough comments and some explanation embedded as comments. Suggest you read the code and run it in Linux box.

Refer the Code in GIT Hub

If you want to know more about fork, refer - https://en.wikipedia.org/wiki/Fork_(system_call)

Share, comment and contribute if you like this post :-)


21 December 2017

Alarm and Signal in Python

Continuation to my previous post in Signals in Python. Again the credits go to Python Module of the week. The following code shows how easier it is to work with Python. This simple script registers for an alarm using SIGALRM which is configured to fire after "n" seconds. After "n" seconds, the handler function gets called.

In this code, I set alarm once as a part of main control flow and register again in the alarm handler. This program does not exit unless one kills the process or stops the execution of the program. (Python code seems to be more beautiful)



20 December 2017

Signal Handling in Python

If you are system programmer, you would have heard about signals and probably would have done lots of coding using signal. It might also be a second nature to you. But when you started to get your hands dirty with signals especially in C, it might have been quite involved. Now moving to signal handling in Python

Recently (last week), there was a post from Python Module of the week on signals and I thought I would try it. Here is the code that does some signal handling. You might want to check the full post (BTW, it is very interesting one). I feel that it easy to write code (and beautiful too) and I was able to do C-equivalent code in less than 30 minutes (which means that Python makes us productive). Here is the script that I wrote. You can find in GIT Hub and feel free to play around.



17 December 2017

"Python Crash Course by Eric Matthes" - Final Post and Review

If you are new to Python and want to quickly ramp-up to become no non-sense developer in Python, you might want to read this post, these posts that are related to this book and probably all of these posts to become Python Super Star.

This is going to be the final post on this book

Review Comments: I accidentally stumbled on this book when I was looking for the books on Python. As the name suggests, it met the purpose of offering the crash course in Python. I could see the chapters are shorter as that one can complete a chapter (or two) in a single sitting. and logically arranged. The code snippets (examples) and "try it yourself" offered a great insight into the programming language. If you are in a hurry to get yourself exposed to Python, grab a copy of this book and you should be making a lot of good progress in booting up in Python. I should also alarm you that this book does not cover the language and its features in great detail. If you are looking for a deeper coverage of each feature, there are other books.

I have solved all the exercises in GitHub. I do not intend to solve the projects now.

Ratings: I would rate this book a 4.5 Stars.

Recommendation: Apt for beginners of Python (If someone is new to programming, they might feel left behind in later chapters as the book covers quite a lot of ground in every chapter or might need to spend more time referring the materials online). If you are new to programming, maybe a MOOC course on Python would be an excellent start.

You can find all posts related to this book - here

Next Goal:
It is time to learn Python little deeper using "Learning Python" by Mark Lurtz

Want to challenge yourself?
Do you want to take part in the challenge of completing "Learning Python" in two months? Add comments below.

13 December 2017

"Python Crash Course by Eric Matthes" - Code Journal 2

If you are new to Python and want to quickly ramp-up to become no non-sense developer in Python, you might want to read this post, these posts that are related to this book and probably all of these posts to become Python Super Star.

Today's update:
Today (13-Dec-17, coded from 6am to 7.45am), I committed the coding solutions for Chapter 8. While solving the exercises, I realized that how easy and lovely is Python. When people say that Python is highly expressive, I did not relate to it. Now, I can :-). The amount of features that you can build with the amount of code is no way related. You will end up writing lesser code (less code means less time to develop means high productivity. Less code means fewer bugs which leads to high quality). If you want to experience it, try reading Chapter 8 and try solving examples. Lists, dictionaries along with various ways of using functions, passing arguments, passing parameters dynamically, passing a variable number of arguments are cool features of Python. Functions in Python is very powerful and yet flexible (I urge you to experience it by yourself)

Do not underestimate Python and this book (because it is a crash course). Solving some of the exercises can be very involved and serves the purpose. I would give 5/5 for the exercises in this chapter. Great selection of exercises. Kudos to the author

Solutions to Chapter 8 can be found in GIT Hub Repository. View it and Share it

Want to contribute/collaborate?
If you want to contribute, you can clone the repository, create a branch and give me a pull request. If you want to know how to collaborate, email/call me. Together, we can make things better.

If you wish to join me in the journey of a lifelong student, add yourself to my blog list and I will not disappoint you :-). 

12 December 2017

"Python Crash Course by Eric Matthes" - Code Journal 1

If you are new to Python and want to quickly ramp-up to become no non-sense developer in Python, you might want to read this post, these posts that are related to this book and probably all of these posts to become Python Super Star.


I am archiving all the code that I have written so far in GIT. Just in case if you want to see it live, check this out - https://github.com/grabyourfreedom/pythoncrashcourse. I have committed all the code that I have written till chapter 5. I will keep working on it - fix bugs, add comments, refactor and add solutions to more exercises. For next few days, I will work on the code until chapter 11 and start reading the other chapters from Saturday (for more posts and code). The code journal posts are likely to be short as I am planning to post only updates here and commit the code to GIT hub.

It is very exciting to ramp in a language - reading, coding, and sharing.

Want to contribute/collaborate?
If you want to contribute, you can clone the repository, create a branch and give me a pull request.

If you wish to join me in the journey of a lifelong student, add yourself to my blog list and I will not disappoint you :-). 

11 December 2017

"Python Crash Course by Eric Matthes" - Reading Journal 4

If you are new to Python and want to quickly ramp-up to become no non-sense developer in Python, you might want to read this post, these posts that are related to this book and probably all of these posts to become Python Super Star.

This post gives an overview of what I learned in Chapters 10 and 11.

Chapter 10: Files and Exceptions
It looks to me that the last few chapters are really informative. This chapter didn't disappoint me. The first part talks about file operations - reading a file, writing to a file, appending to a file with few variations. The second part talks about handling/managing errors using exceptions (try, except and else). As a bonus, the chapter covers storing data using JSON module and at the end talks about code refactoring. Overall it is written well and does the job.

Chapter 11: Testing Your Code 
This chapter seems to be very easy and yet at the same time very informative. It is great to see a book that boots up someone in a language (programming) talks about "testing the code". I feel that it makes a lot of difference in developer's life (others too) when we test our own code using automated unit tests. This chapter talks about testing your functions and classes using Python's unittest module with cool examples. And the side effect, it kindles the interest to explore further on creating automated tests too. Great job by the author

The next set of posts from me would be code journal. I will try to write a couple of posts sharing the coding exercises. With this, I concluded the basics of Python and I am excited to start the projects :-)

If you wish to join me in the journey of a lifelong student, add yourself to my blog list and I will not disappoint you :-). 

10 December 2017

"Python Crash Course by Eric Matthes" - Reading Journal 3

If you are new to Python and want to quickly ramp-up to become no non-sense developer in Python, you might want to read this post, these posts that are related to this book and probably all of these posts to become Python Super Star.

This post gives an overview of Chapters 7, 8 and 9

Chapter 7: User Inputs and while Loops
This chapter covers how to get input from the user, how to convert it to numerical values if you need it. It covers while loop, how to exit from the loop using flag, break, continue and how to avoid infinite loops. It gives a couple of examples of when and how to use while loops with lists and dictionaries. This chapter is extremely light for a developer and might be interesting to someone who is new to programming. You should complete this in less than 30 minutes including the exercises.

Chapter 8: Functions
The chapter covers quite a lot of ground with functions, modules and a bit on PEP8 styling guidelines with respect to functions. You will see how to define a function, without and with parameters, returning value, keyword argument, optional values, default values for parameters, passing a variable number of arguments, passing a variable number of arguments with keywords, importing a module, importing a function from a module, importing all the functions from a modules and aliases for module/functions with sufficient examples. The chapter closes with the styling guidelines from PEP8. An awesome chapter and I really liked this chapter. This chapter might take a little longer including exercises and reflection. It is best to pause here for some time to reflect to ensure you got everything. After completing this chapter, you will really feel that Python is powerful. Kudos to the author for giving us a wonderful chapter.

Chapter 9: Classes
Like the previous chapter, this chapter too covers very interesting paradigm of Object Orientation using classes and objects. If someone is new to programming, I recommend for a slow reading solving all the exercises and trying out all the examples. It is worth the effort. Ensure that you understand the essence of the chapter (classes, objects in OO paradigm) before moving further. If you already know another OO language like C++ or Java, it should be a cakewalk for you. This chapter talks about creating classes, __init__ method, self, creating objects, accessing attributes, calling methods of the objects, subclassing/inheritance, overriding, importing classes and styling of classes. Another wonderful chapter with real-world examples.

If you wish to join me in the journey of a lifelong student, add yourself to my blog list and I will not disappoint you :-). Together, let us transform ourselves.

09 December 2017

"Python Crash Course by Eric Matthes" - Reading Journal 2

Like me, if you are new to Python and want to quickly ramp-up to become no non-sense developer in Python, you might want to read this post, these posts that are related to this book and probably all of these posts to become Python Super Star.

This post gives an overview of what I learned in Chapters 4, 5 and 6

Chapter 4 - Working with Lists
In this chapter, you will work with Lists in a much better way and you will see how much easier is working with Lists. You will learn the operations such as iterating the list using for loop, working with numerical lists using range, slicing a list, copying the lists, difference between copying and assigning lists. You also get to know various errors (runtime and logical error) that are likely to happen when you do not indent properly. At the end, you have a very nice section on how to Style the code. Refer PEP8 from here. This chapter is engaging and you can complete this chapter in 30 to 45 minutes including solving exercises.

Chapter 5 - if Statements
This is another easy chapter that covers conditions/tests, condition evaluation to boolean, test equality, test inequality, comparisons, multiple conditions using "and" and "or", check for existence of a value in a list and non-existence of a value in list, test to check whether the list is empty, simple if, if..else and if...elif...else. Like the other chapters, this chapter is also engaging and one can complete this within an hour

Chapter 6 - Dictionaries
This chapter covers the basics of another important feature of Python - Dictionary. This chapter teaches what is dictionary (you will understand the difference between list and dictionary when you work through examples),  adding new key-value pairs, modifying key-value pairs, removing a key-value pair, various ways of looping (items, keys, values) and various types of nesting (dictionary inside dictionary, list inside dictionary and dictionary inside list).

If you wish to join me in the journey of a lifelong student, add yourself to my blog list and I will not disappoint you :-). Together, let us transform ourselves.