How to see stashed changes using git stash

The command git stash is used to stash the changes in a dirty working directory away.

You can list all stashed change using the command  git stash list,

Every time you stash your working directory, git will save the state of working directory into somethine which mantins history of stash tree. Every time you stash your changes it will be save as a kind of commit in stash tree. All stashed changes are stacked in the order with serial or reference. stash@{0} is the first or top most or recent stash. Every time if you want to refer a particular stash you have to use the it’s reference id something like stash@{3}, stash@{5}… etc

Think of each stash as a separate  commit. These commits are stored and stacked differently and not overlapped with conventional git commit history 

A stash is represented as a commit whose tree records the state of the working directory, and its first parent is the commit at HEAD when the stash was created.

The following command can be used to extract diff of stashed change againest any other stash or commit or branch or HEAD.

  • git stash show
  • git show
  • git diff
  • git difftool

Let’s see, how we can use each of the above mentioned commands.

Command git stash show

The simple command git stash show  gives very brief summary of changes of file, but will not show the diff of changes against current HEAD. Something like,

Something like below

Some times this is not so useful. You may want to see the difference against  current HEAD or any specific commit or current directory.

If you use git stash show along with option -p, It will show all changes.

 

Check diff against selected stash.

Command git show

The command git-show  is used to see  various types of objects.

The command git-show is not only used to visualize  stash changes, but also used to see one or more objects like blobs, trees, tags and commits. For more information check git-show

Synopsis: 


To see top most stash difference against  HEAD:


To get diff of of selected stash against HEAD:


See selected complete whole file as if stash is applied  from selected stash:

Where,

stash@{0} is the reference of stash. I could be any one of stash@{0}, stash@{1}, stash@{2}… etc.

<file_name>  is the name of the file relative to project/git repository

Command git diff 

The command git-diff  is also one of common command which is used to show changes between commits, commit and working tree, etc.

By default, git diff will show the diff of selected stash against(modified files) current state of repository unless other stash  reference or commit is specified.


To get difference between top most stash stash@{0} and master branch:


Only display the names of file not diff of changes:


See the diff between selected stashes for a selected file:

 

Command git difftool

The command git-difftool can also be used to find diff between selected stash and selected commit or branch or stash

See the difference between latest two stashes:

 

 

Summary: 

Commands which are  useful to extract the diff from selected stash git stash show, git show,  git diff, git difftool . 

See difference using command git stash show,

See the changes in the stash using command git show,

See the difference between latest stash and selected commit using command git diff,

 

References:

[1] https://git-scm.com/docs/git-show
[2] https://git-scm.com/docs/git-stash

 

How to delete files older than specified number of days using find command

The Linux command utility find will allow us to perform arbitrary commands on files which are filtered by the command.  Using this opportunity we can delete the files which are older than specified days by passing either command or action to find command.

Find command syntax would look like,

How to delete files older than specified days

To delete files which are older than specified number of days. We have to filter those files using criteria with action which will delete those files.

We can filter the files which are older than specified number of days using test -mtime.  Filtered files can b deleted using either of the following actions  -exec or -delete.

The command we need would look like,

find <path-to-files> -mtime +n -exec rm {} \;

or

find <path-to-files> -mtime +n -delete

Where,
n specifies number of days old that file should be to get not filtered(to get included in output)

Let’s consider an example to  delete files which are older than 7 days.

Example: Delete files older than 7 days

or, as mentioned we can also use -delete action.

As you can in the above command. We also mention other filters(tests) like -name along with -mtime to control what files should be deleted.

Break Down Of Command

First argument:  this could be either absolute path or relative path or wildcard specifies that where to search for files

Second argument: this is the criteria to filter files based on name, path, pattern and how many number of days older etc. More tests can be added to reduce final outcome of files.  Here we used -mtime +7 to filter all files which are older than 7 days.

Third argument: this argument will be the action. Which specifies what action should be preformed on found files. By default it is -print, means it will just print the result. As per our requirement we are using action -delete or -exec to delete files

Action -exec this is generic action, which can be used to perform any shell command on each file which is being located. Here use are using rm {} \;  Where {} represents the current file, it will expand to the name/path of found file.
Note: There is space between {} and \. If you omit this space it will throw and error.

🙂

How to write port-forwarding program using Twisted

Recently I was faced with an issue where a long running process is listening on loop back IP (127.0.0.1) on port 8080 on one of our servers and client programs on other machines are trying to access it on server’s local IP 10.91.20.66.  We ended up at this situation when we have updated server configuration and restarted the server program and forgot to change IP binding info in config file from loop back to local IP. Server got busy with it’s work, with lots of customer’s connections already, by the time we have discovered that some services of  server are not accessible to client programs on other machines. So, the dummy’s guide to fixing it by changing config and restarting the server program is not an option as we can’t risk to disconnect existing customers. So, hot patching is the only option until we can restart the program at next scheduled down time.

I could have fixed this in couple of ways either by adding few lines to iptables configuration or by writing simple socket program in python. The task is to forward data coming in on local IP port 8080 to loop back IP (127.0.0.1) port 8080 and send replies back to source address. Forwarding one socket data to other socket is pretty trivial using Python’s socket library and Twisted made it even more trivial, so I went with the following solution using Twisted.

That’s it. Now, all I needed to do is to run this program by the following command

This simple program is made possible by the heavy lifting done by twisted library. Interested folks can look under hood at twisted’s portforward.py module.

Introduction to Erlang part 1

Erlang is a functional programming language. If you have ever worked with imperative languages, statements such as i++ may be normal to you; in functional programming they are not allowed. In fact, changing the value of any variable is strictly forbidden.

We will start with installing Erlang.

Installation:

In Erlang, you can test most of your stuff in an emulator; it will run your scripts
and  it will also let you edit stuff live. To start,  open a terminal and then type $ erl. If you’ve set up everything fine, you should see text like this:

Shell

Shell commands:

If you type in i then c, Erlang should stop the currently running code and bring you back to a responsive shell. J will give you a list of processes running (a star after a number indicates this is the job you are currently running), which you can then interrupt with i followed by the number. If you use k , you will kill the shell as it is instead of just interrupting it. Press s to start a new one.

Now we will go through some basics

In the Erlang shell, expressions have to be terminated with a period followed by whitespace(line break, a space etc.), otherwise they won’t be executed. You can separate expressions with commas, but only the result of the last one will be shown (the others are still executed). This is certainly unusual syntax for most people and it comes from the days Erlang was implemented directly in Prolog, a logic programming language.

Here is example,

 

Erlang doesn’t care if you enter floating point numbers or integers: both types are supported when dealing with arithmetic.Integers and floating values are pretty much the only types of data Erlang’s mathematical operators will handle transparently for you.

Note that we can use several operators in a single expression, and mathematical operations obey the normal precedence rules.

If you want to express integers in other bases than base 10, just enter the number as Base#Value (given Base is in the range 2..36):


 

Invariable Variables

Doing arithmetic is alright, but you won’t go far without being able to store results somewhere. For that, we’ll use variables. If you have read the intro to this book, you’ll know that variables can’t be variable in functional programming. The basic behavior of variables can be demonstrated with these 7 expressions (note that variables begin with an uppercase letter):

The first thing these commands tell us is that you can assign a value to a variable exactly once; then you can ‘pretend’ to assign a value to a variable if it’s the same value it already has. If it’s different, Erlang will complain. It’s a correct observation, but the explanation is a bit more complex and depends on the = operator. The = operator (not the variables) has the role of comparing values and complaining if they’re different. If they’re the same, it returns the value:

IMPORTANT POINTS TO NOTE:

Data Types:

1) Terms: A piece of data of any data type is called a term.

2) Number: There are two types of numeric literals, integers and floats. Besides the conventional notation, there are two Erlang-specific notations:

$char ASCII value or unicode code-point of the character char.

base#value Integer with the base base, that must be an integer in the range 2..36. In Erlang 5.2/OTP R9B and earlier versions, the allowed range is 2..16.

Examples:

3) Atom

Examples:

4) Bit Strings and Binaries

A bit string is used to store an area of untyped memory. Bit strings are expressed using the bit syntax. Bit strings that consist of a number of bits that are evenly divisible by eight, are called binaries.

Examples:

5) Reference

A reference is a term that is unique in an Erlang runtime system, created by calling make_ref/0.

6) Fun

A fun is a functional object. Funs make it possible to create an anonymous function and pass the function itself — not its name — as argument to other functions.

Example:

7) Port Identifier

A port identifier identifies an Erlang port. open_port/2, which is used to create ports, returns a value of this data type.

8) Pid

A process identifier, pid, identifies a process. The following BIFs, which are used to create processes, return values of this data type:

spawn/1,2,3,4 spawn_link/1,2,3,4 spawn_opt/4

Example:

9) Tuple

A tuple is a compound data type with a fixed number of terms: Each term Term in the tuple is called an element. The number of elements is said to be the size of the tuple. There exists a number of BIFs to manipulate tuples.

Examples:

10) Map

A map is a compound data type with a variable number of key-value associations: ex: {Key1=>Value1,…,KeyN=>ValueN}

Each key-value association in the map is called an association pair. The key and value parts of the pair are called elements. The number of association pairs is said to be the size of the map.

Examples:

11) List

A list is a compound data type with a variable number of terms. [Term1,…,TermN] Each term Term in the list is called an element. The number of elements is said to be the length of the list.

Formally, a list is either the empty list [] or consists of a head (first element) and a tail (remainder of the list). The tail is also a list. The latter can be expressed as [H|T]. The notation [Term1,…,TermN] above is equivalent with the list [Term1|[…|[TermN|[]]]].

A list where the tail is a list is sometimes called a proper list. It is allowed to have a list where the tail is not a list, for example, [a|b]. However, this type of list is of little practical use.

Examples:

13) Record A record is a data structure for storing a fixed number of elements. It has named fields and is similar to a struct in C. However, a record is not a true data type. Instead, record expressions are translated to tuple expressions during compilation. Therefore, record expressions are not understood by the shell unless special actions are taken.

14) Boolean

There is no Boolean data type in Erlang. Instead the atoms true and false are used to denote Boolean values.

Examples:

15) Type converstions


 

 

Sending emails asynchronously using Twisted – Part 2

In Part 1 of article, we saw how to send blocking emails using ‘smtplib’ module & non-blocking emails using Twisted framework. In this part, we will see how to send asynchronous emails to multiple recipients using Twisted

  • Sending multiple emails

    Refer following script.This script sends emails to given recipients asynchronously. Here we have used twisted.internet.defer.DeferredList API. This API is very useful in some scenarios. Suppose you have to finish multiple task asynchronously and then you have to finish one final task. For examples, your program is connected to 4 different clients & and before shutting it down, you have to make sure that all connections are closed properly. In such cases, DeferredList API is used. Create deferrands of each task & make their list. Pass this list to ‘DeferredList‘ API which will return you another deferrand. This final deferrand will be fired when all deferrands in list will be fired.


     
  • Sending multiple emails using coiterator

    Though above script runs fine, there is one problem. Here, recipients number is very small. But suppose you have to send emails to millions recipients then will this code work ?. Refer function ‘send_multiple_emails’.


    Here we have used ‘for’ loop which is blocking. So until this ‘for’ loop is iterated, program will not move to next line of code. For 3 recipients iteration will not take much time however for millions of recipients, it will not work.
    So lets modify our code to work like generators.

    Here, we have used twisted.internet.task.coiterate API. This API iterates over iterator by dividing reactor runtime between all iterators. Thus we can send millions of emails asynchronously.

Sending emails asynchronously using Twisted – Part 1

  • Using ‘smtplib‘ module

It is very easy to send emails using ‘smtplib‘ module of python. Check following recipe.

But ‘smtplib’ module sends emails synchronously. So code execution is blocked until email is sent. To overcome this, lets try to send email asynchornously.

  • Using Twisted

For this tutorial we are going to use Twisted framework. Twisted is event-driven networking engine. It uses reactor-pattern. Twisted uses deferred objects to address waiting IOs. Deferred is more like subset of promises. Check following recipe to send asynchronously MIME message using Twisted.

 

 

Building hello world using Python Tkinter

Continuing our series of desktop GUIs, in this post we are going to go over the topic of building a small hello world program using Python Tkinter.

What is Tkinter ?

Tkinter is python wrapper/binding to Tk library. Tk was developed as GUI library for Tcl language by John Ousterhout. For many other high level language authors coming to programming scene in 1990s this seemed like easy tool to capitalize on in bringing GUI library to their language. That’s why you can find many different language bindings to Tk. Tk brought easy GUI building to programming masses.

Often times many people write off Tk or Tkinter as ugly old GUI library. Their claims are not completely unfounded. But, Tk in recent times made strides to bring modern look by implementing Tile, themeing engine. Tile is also called as ttk. With ttk in place look and feel issues of Tk are addressed.

ttk xp blue theme demo
Tile xp blue theme demo

But, Tkinter still lags behind in terms available collection of default widgets compared to other libraries like PySide or wxPython . However, Tkinter is still a good candidate if you quickly want to dish out a GUI without having programming styles imposed on you. (You will see these style restrictions in our next posts about wxPython and PySide)

Hello world

Now, we will go over building a simple hello world program using Tkinter. In this program we will create a simple window with “Hello world” text in it. And a button that say “Kill Me”. Clicking that button will cause the python program to exit.

tkinter hello world

That import * line saves your some typing.

This line instantiates Tk and creates main window of the program. Next we create a Label (a widget which displays text ) and call it’s pack method. pack is one three available layout methods provided by Tkinter (rest of the two being grid and place). The widget will not show up until you call pack.  Next, we create a Button widget and pack it, similar to that of Label. The interesting point to note while creating button widget is, we provide command argument to it with a reference of sys.exit function. A newbie mistake that is often made is, passing function with () next to it. That will cause the target function to be invoked immediately. You are only supposed to pass the reference of function and Tkinter will call it when button is pressed. So, when this “Kill Me” button is pressed sys.exit will be called and program exits. One, last important line to notice is root.mainloop() . This line sets the event monitoring loop in motion and Tkinter take control of the program from here.

 

Common constructs of GUI programs

In this post we will go over some of the basic and common constructs that you come across while building GUI programs almost in any language or library. The following are some of the common items that I can think of

  1. Loop
  2. Widgets
  3. Events
  4. Layout

Loop

Loop or Mainloop as it is called in some GUI libraries is one of the most common element of all GUI programs. In a GUI program this is usually called at the at end of constructing the all the items (widgets) in program. When you call the mainloop, it takes over the control and keeps running until you terminate the GUI.

The main purpose of loop is to poll for user input (mouse and keyboard actions) and fire events which your program can handle in asynchronous way. Following diagram explains loop in simple terms.

GUI main loop
GUI main loop

As you can see from the above diagram the GUI loop keeps polling for user input. If there is any event via mouse or keyboard or other input devices (joysticks, game pads ) when input is available loop gives control to callback functions that are bound to input events.  Loop keeps running until the main window (also called parent window) exits. Once the main window is destroyed the loop exits and gives control back to your code. In most cases end of the mainloop would be end of many GUI programs.

Widgets

GUIs are collection of widgets. Example of widgets are button, scroll bar, check box etc. Widget is an element of user interaction. Their main purpose is to display or collect information from user. A label displays text to user. A text input widget collects text from user. One way to decide the richness of GUI tool kits is to look at how many widgets they offer. The more widgets a library offers you the better it is for you. So, that you don’t have to write extra code to implement custom widgets.  Widgets usually are sub classes of a Widget parent class. So, most of the widgets inherit properties of widget class in respective GUI libraries. In some GUI libraries main window also has properties of a widget. gui window

Events

When ever user presses a button or enters text using keyboard those actions generate events in GUI libraries. GUI libraries provide a way to hook into these events so that your program can respond to user actions. The event hooking/binding style differs from library to library. But, the aim is same across all libs, that is to provide programmer a way to respond to user input.

Layout

Layout facilities provided by GUI libs helps to make programmer’s life easier when placing widgets in main window. Layouts are simple rules you use to convey to GUI library on how you would like your widgets to be laid out on screen. Instead of manually placing widgets on screen by specifying distance units and placement pixel positions, once you construct your widgets you hand them over to certain layout scheme to let GUI library handle the placement. The benefits are, layouts will take care of expansions, size proportions and other nitty-gritty detail of placing your widgets in orderly way.

The layout features provided by libraries vary greatly. You often have to pick the one that suits your needs. These layouts can be mixed in order to achieve the placement and look you want.

 

Desktop GUI libraries in Python

If you want to build desktop GUIs, Python is right candidate to pick. It offers you plethora of GUI libraries compared to any other language. So, there are high number of chances that, some library in there fits your bill which is readily available, in whole of lot of GUI libs python has to offer. Following is a list of some of the popular GUI libs

  1. Tkinter 
  2. wxPython
  3. PyQt / PySide
  4. PyGTK

(Complete list of available libraries can be found on python wiki page.)

All of the above mentioned ones are cross-platform capable libraries. So, that you don’t have to rewrite any part of your GUI and gladly run it across all the main contenders Windows, Linux and Mac . There are other platform specific ones like win32gui , WinForms using IronPython. But, it helps to stick with cross-platform capable libs to avoid extra work when building your app for other platforms.

Over the next few posts I’ll present you with examples of simple hello world programs using above mentioned list of libraries.

Usually the best way to build your GUI application is to write compute intensive part (if there is any ) of your business logic in low level languages like C/C++ and then write GUI code using the above mentioned Python libs. This saves you lot of time and energy. As, compared to low level languages, in Python you get the benefit of automatic memory management and you only have to write less number of lines of code.

Desktop GUIs are on decline with the advent of web. More and more programs are being coded as services using web technologies ( HTML, CSS, JavaScript ). It has gotten so far, now UI/GUI by default means web UI.  Each passing day web technologies are filling up  the gap in terms of number of features (Dynamic content, Media capabilities ) . But, desktop GUIs still have their place in certain areas and there are numerous programs already written using desktop GUI technologies that needs to be managed.

How to blink browser tab

Have you ever wished to blink browser tab on certain activity to get user’s attention?

Here is a scenario: Suppose there is a chat application installed on your website. Now what if the user of your website has multiple tabs opened in his browser and there is an incoming message in your website? How will you make your user realize that there is an unread message? Of course you can play some music, but it has some limitations. For instance, user may be listening to some songs or speakers could be muted. If none of them is applicable then also he has to go through each and every single tab in his browser and find out the one which is playing that music.

Visual blinking is the best way to get user’s attention and the easiest way to achieve that is to keep blinking the title of your website until user returns.

Here’s a JavaScript function to do so:

Just execute the function like below and browser tab will start blinking: