Linux, OpenSource, Programming And Hacks

Mar 3, 2018

Some Useful Tmux Shortcuts and Cheatsheet

Mar 3, 2018
Tmux is a terminal multiplexer. it's can be used to create several window panes. Create sessions and much more.
Tmux is particularly very useful on remote terminals, I like to use Vim as a text-editor with Tmux.

Open Tmux


Mar 2, 2018

Minify JavaScript / CSS with Django Compressor

Mar 2, 2018
Django Compressor minifies JavaScript and CSS files. It can combine several inline or linked files into cacheable static files. it also supports uglify wih yuglify compressor

Install Django Compressor

pip install django_compressor

In your

    # Add compressor to the installed apps


    # other finders..


#add filters


{% compress css %}
<link rel="stylesheet" href="/static/css/example.css" type="text/css">
<style>p { font-size:5px; color:green;}</style>
<link rel="stylesheet" href="/static/css/example2.css" type="text/css" charset="utf-8">
{% endcompress %}

{% compress js %}
<script src="/static/js/example.js"></script>
<script> console.log("some msg"); function sample() { console.log("some function definition") }</script>
{% endcompress %}

<link rel="stylesheet" href="/static/CACHE/css/07786f58by27.css" type="text/css" />
<script type="text/javascript" src="/static/CACHE/js/70850ie6975h.js"></script>

Feb 23, 2018

Automatically create OneToOneField in Django

Feb 23, 2018
The Django-annoying package contains various useful functions and AutoOneToOneField is one of them.

Using AutoOneToOneField a related object is automatically created when the parent object is created.

In this example, whenever a new user account is created, a corresponding Profile is also created

Important: The child object is not created unless it is accessed.

1. Install

pip install django-annoying

2. In your models

from django.contrib.auth.models import User
from annoying.fields import AutoOneToOneField

class Profile(models.Model):
    user = AutoOneToOneField(User, primary_key=True)
    name = models.CharField(max_length=255, blank=True)
    age = models.IntegerField(blank=True, null=True)

3. Accessing the Child Object

As I mentioned that the child object won't be created usless it is accessed.

so, in you view you might wanna do something like this.

def saveprofile(request):
    u = User.objects.get(username=request.user)
    print(u.player) #accessing child object

That's it!

Feb 22, 2018

3 Common ways to Reverse a String in Python

Feb 22, 2018
One of the very common problems in any programming language is how to reverse a String.

Let's see a few ways to do this in python.

1. Using String Slicing

well first of all the most easy way.. Here we are using the power of String Slicing. Just a single line command!

-1 at the last index means reading from end. and the first two empty index means that we are considering the whole string. 

mystring = "Hello World"
rev = mystring[::-1]

Jan 14, 2018

How to Install SQLite3 in Ubuntu / Linux Mint

Jan 14, 2018
SQlite is an Embedded Relational Database Management System. SQlite does not require a server to run unlike mysql or other popular dbms.

To install Sqlite3 in Ubuntu / Linux Mint issue the following commands

This will work for Ubuntu 14.04 and above

sudo apt-get update
sudo apt-get install sqlite3 libsqlite3-dev

Jan 12, 2018

Install TypeScript Syntax highlighting in Sublime Text Editor

Jan 12, 2018
We will need the TypeScript Package to get Syntax highlighting to work. we will install the package using Package Control.

So, first go ahead and install Package Control

Now, go to preferences, where you can now find Package Control option, click on it .

Jan 11, 2018

Python Program to Check if a number is FIbonacci

Jan 11, 2018
In Fibonacci Sequence, every previous two numbers gives the next number.

the first 10 Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 35

In this python Program we will see how to test if a number given by user belongs to the Fibonacci sequence.

If the given number is n then any of 5n*n + 4 or 5n*n - 4 or both should be a perfect square to be a Fibonacci number.

Python Code:

import math

def CheckPerfect(x):
  i = int(math.sqrt(x))
  return (x == i*i)

def CheckFibo(n):
  if(CheckPerfect(5*n*n + 4) or CheckPerfect(5*n*n - 4)):
    print("Not Fibonacci")

CheckFibo(89) #this returns true, 89 is Fibonacci

you can suggest better algorithm for this in the comments!

Jan 10, 2018

Python Program to check if a number is Perfect Square

Jan 10, 2018
A number that can be expressed as the product of two equal integers is a Perfect Square number.

for example: 25 can be expressed as 5 x 5

In the given python3 code we take a user input and take out the square root and convert it into integer. if square root value multiplied by itself equals the user given number then its proved to be a Perfect number else not. 

import math

n = int(input())

i = int(math.sqrt(n)) #sqrt function returns float so typecasting to int

if(n == i*i):
  print("perfect Square: %d * %d = %d"%(i,i,n))
  print("Not Perfect Square")

Please comment below if you have a better algorithm than the given one.

Jan 8, 2018

How to Fix - Operational Error: cannot open database in Django Sqlite3

Jan 8, 2018
This may be a permission problem.

the database file must be writable to the user

you need to grant permission to db.sqlite3

here, myproject is the folder which contains db.sqlite3 file

chmod 664 /path/to/myproject/db.sqlite3
sudo chown :www-data /path/to/myproject/db.sqlite3 
sudo chown :www-data /path/to/myproject

also give necessary permissions to the folder that contains the sqlite3 database file. the database engine should be able to write a lockfile to the parent directory of the database file.

sudo chmod 775 /path/to/myproject

Dec 28, 2017

Get Free SSL certificate with Let's Encrypt on Ubuntu Apache Server

Dec 28, 2017
Let's Encrypt is an open certificate authority and provides free SSL Certificates. It's popular for it's simplicity to setup. It's been founded by Mozilla, Cisco and many more.

The setup process is very simple, I have explained it in 3 steps:
In the bottom section I have also mentioned the problems I faced and the solutions that worked.

Make Sure that your website is running on Apache server configured through VirtualHosts and specifies ServerName

Dec 25, 2017

How to link a custom domain name to AWS EC2 instance

Dec 25, 2017
if you have created your Amazon AWS EC2 instance, you can now access it on the web using your given public ip or public DNS (something like, you can find these in the description of the instance.

But you need to access your website just using you own domain like

Here are the steps to do so very easily:

Dec 24, 2017

How to setup a Static IP (Elastic IP) Address for AWS EC2

Dec 24, 2017
When you create an Amazon AWS EC2 instance, you are given a Public IP which you can use for remote login via SSH or Putty and of course to access on your browser. But the problem is that the Public IP keeps changing each time you restart your instance.

To get a fixed IP, the solution is to use an Elastic IP

Step1: You can find Elastic IPs option under the EC2 Dashboard (left sidebar). click on it.

Step 2: Click on 'Allocate new Address' button, you will get a new allocated IP

Step 3: Now, Click on the 'Actions' button and then click on 'associate Address' option.

you will get an option to select the EC2 instance that you want to get associated with the  newly allocated Elastic IP address.

That's it!

Note: Make sure you don't leave the Elastic IP unused, or you will be charged!

Dec 9, 2017

How to install Apache2 Server in AWS EC2 Linux

Dec 9, 2017
After you have created and launched your EC2 instance you must be wondering what to do next?
if you enter your instance ip in your web browser nothing will show because you need to install a server to see some action!

It's quite simple too!

I am showing all the steps on the Ubuntu EC2 instance but it will be same on any Linux instance.

Dec 5, 2017

Place a static file in root directory in Apache Server

Dec 5, 2017
Sometimes you are required to place files like sitemaps or other files in the root location.

Specially while using frameworks like Django, It can be bit tricky to do so within the framework.. So this method will flawlessly irrespective of which framework you are using.

So, If you are running Apache server, it's quite easy

Just open up your Apache Config file and add the following line at the bottom of that file.

Alias /example.txt /path/to/example.txt

Now restart your apache server.

After this you can access the file kept at /path/to/example.txt as /example.txt

Quite easy!

In ubuntu 16.04 you can find the config file here: /etc/apache2/apache2.conf

Sep 27, 2017

How to Install and use FFmpeg in Ubuntu 16.04

Sep 27, 2017
FFmpeg is a command line tool for video and audio conversion. It can also capture from a live audio or video source. It is cross-platform, available for Linux, Mac OS X, windows and more

Install ffmpeg in Ubuntu by entering these commands in the terminal (Ctrl +Alt + T):

sudo apt update
sudo apt install ffmpeg