01 January 2018

"Learning Python by Mark Lutz" - Reading Journal 3 [Chapter 4 and Chapter 5]

Learning Python by Mark Lutz Fifth Edition
Learning Python by Mark Lutz
Like me, if you are new to Python and want to ramp-up to become no non-sense developer in Python, you might want to read these posts (and if you are prepared to take up the tough path of mastering the language). The post like this is my experience - the journey that is little longer but interesting, thought-provoking and rewarding. 

For next few months, I would be posting my experiences with Python more specifically the learning I am going through from the book - "Learning Python by Mark Lutz". You can find all of my reading journal of this book

The rest of this post covers what I learned in Chapter 4 and Chapter 5.

Chapter 4: Introducing Python Object Types
As the chapter title says, this chapter introduces core data types that are built-in to Python - Numeric, String, Lists, Dictionaries. It also gives an overview of Files, Set, and user-defined classes. It talks about sequences, mappings, and sets. How to iterate over collections and access mappings/dictionaries. What are type-specific functions available. On files, a simple read/write with encoding/decoding is discussed at a higher level. At the end of this chapter, you will realize that everything is object in Python (and dynamic/strick typing system). It was very interesting to read. On the downside, I am still curious as to when I will write some code. I do not see coding exercises/puzzles at the end of the chapter yet.

Chapter 5: Numeric Types
What we think as just integers and floats are mind-boggling when we try to go little deeper. This chapter is no exception especially when you have two versions of Python (2.x and 3.x) behave little differently. I am pretty sure that numeric types and differences in two major versions of Python are going to be little tough to remember and one has to keep referring the material until it becomes second nature. Let us come to the learning.

This chapter covers numeric types - integers, floats, decimal, fraction, complex numbers, operators, building expressions, precedence, bitwise operators, comprehension, sets, booleans, evaluation of tests, chained tests, representing numbers in other bases (hex, octal and binary) and finally some introduction on number crunching with popular modules. I strongly suggest practicing while you read to get the feel of what the author says. The chapter was longer with so many details but never boring though. Truly great stuff.

All posts related to "Learning Python by Mark Lutz"

Friendly Note: I am posting the images and links from the publisher's website. I am not an affiliate and I do not get paid for writing this. If my journal is useful for someone to learn Python, I am more than happy :-)

31 December 2017

"Learning Python by Mark Lutz" - Reading Journal 2 [Chapter 2 and Chapter 3]

Learning Python by Mark Lutz Fifth Edition
Learning Python by Mark Lutz
Like me, if you are new to Python and want to ramp-up to become no non-sense developer in Python, you might want to read these posts (and if you are prepared to take up the tough path of mastering the language). The post like this is my experience - the journey that is little longer but interesting, thought-provoking and rewarding. 

For next few months, I would be posting my experiences with Python more specifically the learning I am going through from the book - "Learning Python by Mark Lutz". You can find all of my reading journal of this book and accompanying code in GitHub

The rest of this post covers what I learned in Chapter 2 and Chapter 3.

Chapter 2: How Python Runs Programs
This chapter introduces you a needed theoretical overview of Python runtime (interpreter) and Python bytecode. The execution of the program is explained in two viewpoints - programmer's view and Python's view. The programmers' view is what code to be written following the language constructs generating source code and Python's view is how to execute the program converting source code to actual functionality (the output of programmer view is input to Python's view). The chapter offers execution model variations (different runtime models - CPython, Jython, Stackless, PyPy) and optimization tools (Psyco JIT, Shed Skin, Cthyon). This chapter is wonderfully written and there will be some learning to everyone. I thoroughly enjoyed the content :-). Kudos to the author for covering a variety of insights.

Chapter 3: How You Run Programs
I need to start with a warning. This chapter has so much information and ways to run your code like "from the command line", "interactive", "IDLE" and "IDE". I personally suggest that we should try all the methods at least once and choose one of the methods. I find IDE convenient. Yes, I do use PyCharm community edition. On the good side, the chapter introduces modules, importing, namespaces, module reloads which is quite interesting. Good round up of exercises at the end. I do not want to bog you down with a lot of details. One little advice I have for you is to take breaks while reading this chapter and be prepared for a long haul. Without the break, I feel this chapter is little tough to complete and might appear daunting (and might be demotivating to complete). The good thing about such details are completeness

BTW, with this, we complete the first part of the book and we have eight more parts.

Looking forward to the Part 2

All posts related to "Learning Python by Mark Lutz"
Source Code@GitHub (exercises of this book)

Friendly Note: I am posting the images and links from the publisher's website. I am not an affiliate and I do not get paid for writing this. If my journal is useful for someone to learn Python, I am more than happy :-)

30 December 2017

"Learning Python by Mark Lutz" - Reading Journal 1 [Intro and Chapter 1]


Like me, if you are new to Python and want to ramp-up to become no non-sense developer in Python, you might want to read these posts. The post like this is my experience - the journey that is little longer but interesting, thought-provoking and rewarding.

For next several months, I would be posting my experiences with Python more specifically the learning I am going to have from the book - "Learning Python by Mark Lutz". My first impression after reading the table of contents is very promising. It looks to me that it is one of the books that provide complete coverage of Python and exhaustive too. I am eager and yet at the same time little afraid because I never learned any programming language reading a book completely. Since it is massive in size containing 1500+ pages, I feel it is going to test my consistency. And that is one of the reasons why I want to write my reading journal.

The rest of the post contains what I learned from the chapter 1 (Initially I thought I would write journal of first three chapters. But I could not do it due to merits and the amount of content in this chapter)

Chapter 1: A Python Q & A Session
The book starts with a bang providing enough material and motivation as to why someone should invest time learning Python and reading/working-out this book. This chapter provides an introduction to Python in the form of question and answer. These are kind of questions that were eating my mind and wanting an answer because the websites or courses talk only on the language but this book seems to address the gap as to "why". I also feel that the author has set up an important tone - question everything when you learn. You will find answers to the following questions
  1. Why people use Python?
  2. Is Python scripting language?
  3. Is there any downside of Python?
  4. Who uses Python?
  5. What can I do with Python
  6. How is Python developed and supported?
  7. What are its technical strengths?
  8. How Python stacks up to other languages?
This chapter gives deeper insights with solid reasonings and you will understand what you will get by learning Python and what is the amount of knowledge that you will get by reading this book - "Learning Python by Mark Lutz"

Needless to say, I am all excited to continue to learn Python :-)

29 December 2017

Simple ProcMon - Putting all things together

I have written few posts on process management in Python - fork, exec, wait, and exit. These are exactly same Python APIs around system APIs provided by Linux/Unix/POSIX. In this post, I am going to talk about putting things together and create a simple process monitor.

Read how to fork, exec, wait and exit before proceeding with this post.

Functionality of Simple Process Monitor
  1. Have the list of processes that need to be monitored in the INI file. Let us call this configuration
  2. Load the configuration file
  3. Start the process
  4. Exec the new program
  5. Loop #3 and #4 until all the processes are loaded
  6. Monitor the process (wait). A simple call to "wait" would listen for a child to exit.
  7. When a child dies, handle it either by restarting the process again or do nothing (depending on how it is configured).
  8. Go back to #6 and continue the monitoring
It is going to tough to post all the code here. Refer my GITHub for the code. Feel free to extend or write tests

Experience: It is another personal experience on higher productivity claim of Python. I wrote the code in a couple of hours and spent around 2 hours on refactoring. Given my (lack of) experience in Python, Python seems to fulfill its promise.

28 December 2017

Blogger, Feedburner Email Subscription Popup Widget


After a very long time, I got a chance to write some Javascript, jQuery, and some HTML stuff. I thought of updating my blog with a new subscription pop-up. The goal is to show a pop-up enabling the user to subscribe to my blog through email. While I have them already in a couple of places, I felt a pop-up at the center of the screen would help - to get some attention and action. Let us see how it goes.

I am sharing the code here. Feel free to use it if you are searching for something like this (but give the credits by not removing Widget by Lakshmi N).

Now coming to the widget. The following are the functionalities
  1. Display a pop-up with an option to subscribe or close the widget
  2. Integrate the pop-up with my feed provider (which is Google's Feedburner) so that I do not need to manage the subscriptions. It comes with a lot of security and  I can move to a different provider if there is a need.
  3. Make it less annoying. Use cookies and do not display the pop-up for another N days (by setting the expiry for cookie).
While I foresee some changes on the look and feel - but the core features are almost there. I have also committed the changes to GitHub.  There are few customizations that you might need depending on your requirements and taste. Here is how I have implemented
  1. HTML/CSS: If you turn on developer tools in your browser, you should see that the entire pop-up is written using HTML, CSS, and Javascript. I have used absolute positioning of the widget so that it shows up in the middle of the browser window. 
  2. Images: There are two images that I have used - one for the background for the pop-up and the close button. You can customize these too for your requirements in case if you need better images
  3. Display Timeout: Configure timeout duration after which you want the pop-up to be displayed. The display of the pop-up will be delayed for such duration after document.ready(). This should be lesser duration.
  4. Cookie Expiry Timeout: This is the duration after which the popup will again appear. This can be any precision (it can be from milliseconds to year). In order for this logic to kick-in, the user should have clicked "close". Ideally, this should be longer duration.
  5. JQuery: I have used JQuery, so ensure that you load jQuery in section and add/update document.ready(). I have used one already on my blog from Google CDN.
  6. Other Platforms: I have tested this on my blog (blogger) and not tested on other blogging platforms. But I believe it should work. Give it a try :-)
  7. Questions: If you have any questions or the stuff is not working, use the comments section to reach out to me. I try my best to support.
Here is my gist (If you remove the credits, it is mere copying and if you give credits it will be termed as normal usage. Be happy that I do not ask for money or bitcoins :-)).


27 December 2017

Want to GIT?

The entire programming world is moving towards using GIT as it is becoming a de-facto. If you have few hours during the holidays and want to learn how to work with GIT (or how GIT works), do not miss this course from Udacity. The course instructor has done a good job of putting together a nice course with a lot of materials and exercises (you can complete the course in two days budgeting some breaks for reflection)

Udacity Course Link - Version Control with Git

Points to be noted:
Note 1: You will learn only git (not GIT Hub or something that uses git to host your code). Nevertheless, this should be the first step before embarking further. GIT Hub (or something similar) should be logical next step
Note 2: Highly recommend for students who are still in college (writing code is good thing, writing tests for the code is better thing, managing your code is the best thing)
Note 3: Very highly recommend for professionals who have not explored GIT yet (learn now or will be made to learn it in 2018).
Note 4: Many text editors (like Atom) and IDEs (like PyCharm) support GIT/GIT Hub (super easy)
Note 5 (good that you have read thus far): Refer https://git-scm.com and learn why you should be learning GIT if you are a developer

26 December 2017

Paradox of Choices

Less is more and more is less

Why?

I often undergo this once in a year when I try to change my phone. For past few years, I have been using smartphones (Android) but buying them is a strenuous exercise. It heavily taxes me. In fact, i take approximately about a month to decide whether to buy a phone and another month thinking what phone/brand to buy. Given the rate at which that I change the phone for past few years, it makes sense to shortlist two or three phones and pick the right one. It is especially more tedious when you want to buy things online (because the website throws options at you that you can compare)

Aren't choices good? The answer is "yes" and "no". The choices are good as long as we are able to decide and they are bad when we hit the decision paralysis (making choices).

Certainly, the choices beyond certain point overwhelm us. Our brain thinks that it is analyzing the choices but fails to understand that the choices deceive us. The brain gets tired and as a result, we end up making not so good choices (bad) or do not take decisions at all (worse).

If you are from India and had taken your wife to get a saree, you would have experienced this. The female is known to make wise decisions when it comes to life but often indecisive to select a saree (so the number of choices is the culprit)

So, it is true if someone says "less is more" (when the choice is less, you act on those choices very quickly).

Tidbits - https://en.wikipedia.org/wiki/The_Paradox_of_Choice

25 December 2017

Process Management in Python - os._exit()

This is a continuation of series of posts on Process management in Python. Read thisthis and this before continuing further. In this post, we are going to see how os._exit works and how it is related to "wait".

os._exit when invoked, the process that is calling it terminates immediately without doing any clean-ups (however system.exit() can be handled by the Python program, a much safer/clean way). The parent process receives the exit status when the parent process calls "wait" or "waitpid. os._exit calls _exit of OS

The following code demonstrates it. Beware, os._exit exits the process abruptly.


For more information on exit and _exit refer - https://en.wikipedia.org/wiki/Exit_(system_call)

Refer the Code in GIT Hub

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


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 :-)