FCM – send push notifications using Python

What is FCM ?

FCM – Firebase Cloud Messaging is a cross-platform  ( Android, iOS and Chrome ) messaging solution that lets you reliably deliver messages at no cost. FCM is best suited if you want to send push notification to your app which you built to run on Android and iOS. The advantage you get is you don’t have to separately deal with GCM (Google Cloud Messaging deprecated now) and Apple’s APNS. You hand over your notification message to FCM and FCM takes care of communicating with apple’s APNS and Android messaging servers to reliably deliver those messages.

fcm-2

Using FCM we can send message to single device or multiple devices.  There are two different types of messages, notification and data. Notification messages include JSON keys that are understood and interpreted by phone’s operating system. If you want to include customized app specific JSON keys use data message. You can combine both notification and data JSON objects in single message. You can also send messages with different priority.

Note : – You need to set priority  to high  if you want phone to wake up and show notification on screen

Sending message with Python

We can use PyFCM to send messages via FCM. PyFCM is good for synchronous ( blocking ) python. We will discuss non-blocking option in next paragraph.

Install PyFCM using following command

The following code will send a push notification to

So, the PyFCM API is the pretty straight forward to use.

Sending FCM push notification using Twisted

PyFCM discussed in above paragraph is good enough if you want to send messages in blocking fashion. If you have to send high number of concurrent messages then using Twisted is a good option.

Twisted Matrix
Twisted Matrix

Network operations performed using twisted library don’t block. Thus it’s a good choice when network concurrency is required by program. We can use txFCM library to send FCM messages using twisted

Install txFCM using following command

Following code send FCM message using txFCM

txFCM is built on top of PyFCM so all the API call that are available in PyFCM are also available in txFCM.

Python metaclasses explained

Python is having obscure wonderful mechanism of classes and it’s implementation.   In python every thing is an object. If you define a class, that class it self is an object in memory and it is an instance of other class. You may call it as a class object. If you instantiate it you will get brand new object called instance( instance of class).

Probably, meta classes  are  sort of confusing concept. Many people are afraid of this concept, but believe me it is very nice and simple if you understand it.

In simple words, meta classes are classes which are responsible to create  a class object( in memory).

As mentioned above, when you define a class. A class object will be created in memory but behind the senses it is an instance of other class by default type. So, classes are created by meta classes. You can specify your custom meta class which is responsible to create a class.

Mostly, meta classes are used when you write APIs or frameworks. Like django uses metaclasses in models.

Meta Classes
Meta Classes

I would like to explain this concept by taking real world example, to get you good understanding. Let’s take a look at following picture.

Python MetaClasses Illustrated
Python MetaClasses Illustrated

As show in picture, A factory can be treated a metaclass. Where, it produces vending machine, can be considered as a class. Where, the class (vending machine) produces instances, which are cans, bottles etc.

An example representing normal class definition and instance creation

Python is having builtin function called isinstance. Using this function we can determine if any object is an instance of specified object.

In the above example we haven’t specified the metaclass. So, python would use default one. i.e type.

In python 2, we specify metaclass using magic method __metaclass__.
__metaclass__ can be specified either as a class attribute or global variable. When you specify it globally in module level, all classes will become instances of this metaclass. If you specify this as a class attribute, only that class will become instance of specified metaclass. In python3 this __meatclass__ attribute is removed in favour of using metaclass argument that you have to specify in class definition.

Implementation of simple class with metaclass in place

Here in this example, you can see class Foo is the instance of metaclass MetaClassThus, Foo gets created as an instance of MetaClass. And, i is an instance of class Foo. As you can see, we confirmed this relation using isinstance function.

Python metaclasses - instance, class and metaclass
Python metaclasses – instance, class and metaclass

There is no restriction over how many times one class can be used as a metaclass for other classes.

In the above example,  the meta class MetaCalss is used in both Foo and Bar classes. Like wise you can use it in as many classes you want. If you want to apply it for all module level classes. You better use global __metaclass__ attribute. As you can see in the following example

OutPut:

This module level __metaclass__ magic variable doesn’t work on new style classes as show in above code.

 

Python Metaclasses Key Notes

  • Metaclasses are callables
  • Subclasses inherit the metaclass
  • Restriction over multiple metaclasses in multiple inheritance
MetaClasses are callable

The MetaClass always need not be a class. You can use any callable as a metaclass. A Simple example demonstrates using callable (function) as a metaclass

If you are using callable as a metaclass class. It should have same signature(arguments) as type. That is,

Following example is useless but explanatory. You can hack the class creation using metaclass. You can see below example, Foo became 3 instead of class object as we did return 3 from metaclass.

Subclasses inherit the metaclass

Like all other attributes and methods subclasses inherit metaclass.

OutPut:

#TODO

Restriction over multiple metaclasses in inheritance

Classes can have multiple base classes. Those maetaclasses may have different metaclass. If so, every thing should be linear in the inheritance. That is, multi-level inheritance works fine. But, python is having restriction over having multiple metaclasses in multiple inheritance  from different bases. Following example shows that, raised exception because of having multiple metaclasses from two bases.

Where metaclasses used ?

The main use case for a metaclass is creating an API, best example of this is Django ORM.

If you are aware of django. We usually define a model as follows

But,

It won’t return an IntegerField object. Instead it will return int.

This hack is actually possible by defining  __metaclass__ on models.Model and it uses some magic that will turn the model Person you defined with simple statements into a complex hook to a database field.

Django makes complex look simple by exposing a simple API and using metaclasses inside.

Metaclasses in python3

In python3 we specify metaclass by passing keyword argument metaclass to class definition. This is one of the difference between python2 and python3.

Py3K translation tool can be used to convert old syntax to new syntax.

Summary

In python classes are actually instances of other classes (soft of)  called metaclasses. You can specify metaclass using magic method __metaclass__ either in class definition or gobally module level. These metaclasses are useful when you want customize class creation.

 

Conclusion

Metaclasses is the obscure feature in python. Use it, when you really need them.

Metaclasses are deeper magic that 99% of users should never worry about. If you wonder whether you need them, you don’t (the people who actually need them know with certainty that they need them, and don’t need an explanation about why).

Python Guru Tim Peters

Best real life example where metaclasses are being used is Django ORM. As like Django if you are writing framework or API, you can leverage metaclasses and you really need them.

Please share. Let me know your comments and queries 😉 😉

References:

[1] https://www.python.org/dev/peps/pep-3115/
[2]http://stackoverflow.com/questions/2149846/customary-to-inherit-metaclasses-from-type
[3]http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python

How to check routes (routing table) in linux

The route is nothing but a path or way to the specific or range of destination IP addresses. Linux kernel maintains these routes called as kernel routing table and will route the traffic accordingly.

You can use any one of them to display the routing table

route
netstat
ip

Command route

The command route is used to modify and check the existing routes. To check the routing table using route command,

This will display the list of routes currently configured

Command: route -n 

This will give the output with out resolving the names

Command netstat

This command gives the statistics about the network. With this command you can do more than just printing routing table,

Print network connections,
routing tables,
interface statistics,
masquerade connections,  and
multicast memberships

To check the routing table,

Command: nestat -rn

Where

Option -r  is to display the routing table

Option -n is not to resolve the names. Print the IP addresses only

Command IP

To check routing table using command ip 

Command: ip route list

To find hops, route of the destination and how your machine is reaching the destination you use tracepath

Where you can replace howto.lintel.in with specific domain or IP address.

 

Writing shorthand statements in python

Python is having  shorthand statements and shorthand operators. These things will help you write more logic with less number of statements.

We will see those available shorthand statements.

lambda statement

Probably every body is aware of the lambda functions. The statement lambda is helpful to write single line functions with out naming a function. This will return the function reference where you can assign it to any arbitrary variable. It’s more like JavaScript anonymous functions.

Self called Lambda

You can write the lambda and you can make it call it self like self-invoking functions in javascript. Let’s see an example,

List Comprehension

List Comprehension is the great feature that python is having. Using this feature you can reduce the lot of code, you can reduces space complexity of the code. Simple for loops can be written using list comprehension.

Syntax:

L = [mapping-expression for element in source-list if filter-expression]

Where:

L       Variable, result gets assigned to

mapping-expression       Expression, which is executed on every loop if only filter-expression in if condition resolved as True

This list comprehension is equivalent to,

Example

Lets see list comprehension example. Get even number from the given range.

Usual code

List Comprehension

 

 

Dict Comprehension

Dict comprehension is available in python 2.7 and 3.x. This syntax will provide you the way to encapsulate several lines you use to create dictionaries into one line. It’s is similar to list comprehension but we use dict literals {} instead of []

Syntax:

{key:value for element in source-list if filter-expression }

Let’s how we use it by an example,

I have a list of fruits, I want to make it dictionary by changing their case

[‘APPLE’:, ‘MANGO’, ‘ORANGE’]

I want to convert all keys into lower case. This is we would do with out using comprehension

Using Simple list comprehension,

{i.upper(): 1 for i in l}

Set Comprehension

Set comprehension syntax is very much similar to dict comprehension with a small difference.

Let’s consider dict comprehension example. Using following statement you generate set

{i.upper() for i in l}

Where we haven’t specified value like we do in dict comprehension

Generator Expression

You might have already know about generators. Any function which contains yield statment is called generator. generator gives iterable where we can call next method to get the next item in the sequence.  Python got short notation for this generators like lambda. It is same as list comprehension but we enclose the expression with touple literals instead.

Generator Function

Generator Expression

Same generator function can written as follow,

😉

Shorthand If Else

Like C and javascript ternary operator (?:) you can write short hand if-else comparison. By taking readability into account we have following syntax in python

if-expression if (condition) else else-expression 

This is equivalent to,

if condiction:
    if-expression
else:
    else-expression

Tuple Unpacking

Python 3 even more powerful unpacking feature. Here it is,

Example:

a, rest = [1, 3, 4, 6]

In this case, a will get 1   and rest of the list will get assigned to variable rest. i.e  [3, 4, 6]

String Concatenation  with delimiter

If you want to concatenate list of strings with some random delimiter. You can do that by using string method join

 

How to create read only attributes and restrict setting attribute values on object in python

There are different way to prevent setting attributes and make attributes read only on object in python. We can use any one of the following way to  make attributes readonly

  1. Property Descriptor
  2. Using descriptor methods __get__ and __set__
  3. Using slots  (only restricts setting arbitary attributes)

Property Descriptor

Python ships with built in function called property. We can use this function to customize the way attributes be accessed and assigned.

First I will explain you about property before I get you idea about how it is useful to make attribute readonly.

Typical signature of the function property is

 property([fget[, fset[, fdel[, doc]]]]

As you can see here this function take four arguments, those are

fget is a function for getting an attribute value. fset is a function for setting an attribute value. fdel is a function for deleting an attribute value. And doc creates a docstring for the attribute.

All these function are for the sake of single attribute. That is fget function will be called when you access/get the attribute. fset function will be called when you are trying to set the attribute.

Simple example

Instantiate Foo and try to play the instance attribute x

I hope, you got what exactly the function property is and how we use it. In many cases we use this property to hide actual attributes and abstract them with another name.

You can use property as decorator also. Something like

Now let’s come to actual thing how we make attribute readonly.

It’s simple you just don’t define setter for the property attribute. Let’s see the following example

Here, as we didn’t define setter for the property attribute. So python won’t allow setting that specific attribute even you can’t delete if you don’t define fdel. Thus, attribute becomes read only. Still you can access b._money  and you can set that attribute there is no restriction over setting this internal attribute.

Descriptor methods __get__ and __set__

These magic methods define descriptor for the object attribute. To get complete understanding and usage about descriptor magic methods, please check other article .

Like fget and fset functions that property function takes, __get__ is used to define behavior when descriptor’s value is retrieved. __set__ method is used to define behavior  when descriptor value is getting set(assigned). Where __delete__ is used to define behavior when descriptor is getting deleted.

To restrict setting attribute and make it readonly. You have to use __set__ magic method of descriptor and raise exception in it.

Let’s see the simple example demonstrating descriptor object and readonly attributes using descriptors

Lets see the result and trying to set speed attribute

As you can see here, we can’t set the attribute speed on instance v of Vehicle. Because we are restricting it in descriptor method __set__ of class Speed

Python __slots__

The basic usage of __slots__ is to save space in objects. Instead of having a dynamic dict that allows adding attributes to objects at anytime, there is a static structure which does not allow additions after creation. This will also gain us some performance due to lack of dynamic  attribute assignment. That is, it saves the overhead of one dict for every object that uses slots.

Think of you are creating lot of (hundreds, thousands) instances from the same class, this could be useful as memory and performance optimization tool.

If you are using __slots__ means you are defining static attributes on class. This is how we save memory and gain performance as there is not dynamic attribute assignment. Thus you can’t set new attributes on object.

You see, in the above example we are not able to set attribute c as it not given in __sots__. Any way it’s about restricting  assignment to new attributes and you can combine either above two methods to make existing attributes readonly.

 

References:

[1] __get__ and __set__ data descriptors don’t work on instance attributes http://stackoverflow.com/questions/23309698/why-is-the-descriptor-not-getting-called-when-defined-as-instance-attribute

[2] http://stackoverflow.com/questions/472000/usage-of-slots

How to enable gzip compression in apache

What is gzip compression? Why do we need to enable it?

Gzip is the file format and method of compression and decompression of files using it’s algorithm to reduce the file size. It is used in web server, where web servers  send data to http client with gzip compression for faster data transfer and low bandwidth consumption.

Enabling gzip compression is the best practice and it is highly recommended, so pages are likely to load faster in browsers.

Enable gzip compression to improve website speed
Enable gzip compression to improve website speed

How to check gzip compression enabled or not

You can check if gzip compression is enabled or not on a particular website using following methods.

You can use the google PageSeep Insights tool not only to check gzip compression but also to analyze performance of a website.

You can also check if gzip compression is  enabled on server or not using command line tool curl on linux. Try out the following,

Replace the URL with what ever the website that you want to check for gzip compression.

Where,

-I   means, only make HEAD request to server to get headers

-H    specify the accepted encoding gzip using header ‘Accept-Encoding’

If you see header Content-Encoding with gzip in response headers, then compression is enabled on server and it’s working

It works in such a way that, client would specify it’s supported compression and encoding using header Accept-Encoding. Server will use compression if it is enabled on server else will send plain text back to client. Server will notify encoding format and compression to client through header Content-Encoding in response headers.

Here is the curl request on google.co.in to verify gzip compression

We can see that, the header Content-Encoding: gzip in response headers. Means gzip compression is enabled.

Enable gzip compression in apache

The gzip compression can be enabled  by directly changing httpd conf file. That is httpd.conf  or you can use .htaccess file to target only specific directory or path or site.

Add the following code to  /etc/httpd/conf/httpd.conf    if apache is installed somewhere add the that specific httpd.conf file

To enable gzip compression via .htaccess, put the following code into .htaccess file which  in the desired site directory.

Restart the apache. That’s it, apache will compress the response which is being sent to client. Enabling gzip compression absolutely would boost your site performance.

You can test if gzip compression is enabled or not using curl as mentioned in this article.

The gzip compression is recommended on all types of text files including following files and extensions

.html
.php
.css
.js
.txt
.json

But, gzip compression is not recommended on graphic files or .zip (in the sense files which are compressed already). Because by compressing these kind of files we hardly save few bytes. Thus increase in loading time because of added unaffected compression task.

Why GZIP compression ?

As explained, gzip compression saves the lot of bandwidth by reducing file size. It saves up to 50% to 80% of bandwidth. So, it reduces download and waiting time of browser for resources. If you enable gzip compression, it won’t affect unsupported browsers, where they can fallback to normal(no compression) data download.

 

Summary

GZIP compression is process of zipping or compressing files on server before they get delivered or transferred via network to browser. Browser will uncompress the data before it uses. As it saves 50% to 80% of bandwidth, if it enabled performance of  website will increase considerably.

Python: __new__ magic method explained

Python is Object oriented language, every thing is an object in python. Python is having special type of  methods called magic methods named with preceded and trailing double underscores.

When we talk about magic method __new__ we also need to talk about __init__

These methods will be called when you instantiate(The process of creating instance from class is called instantiation). That is when you create instance. The magic method __new__ will be called when instance is being created. Using this method you can customize the instance creation. This is only the method which will be called first then __init__ will be called to initialize instance when you are creating instance.

Method __new__ will take class reference as the first argument followed by arguments which are passed to constructor(Arguments passed to call of class to create instance). Method __new__ is responsible to create instance, so you can use this method to customize object creation. Typically method __new__ will return the created instance object reference. Method __init__ will be called once __new__ method completed execution.

You can create new instance of the class by invoking the superclass’s __new__ method using super. Something like super(currentclass, cls).__new__(cls, [,….])

Usual class declaration and instantiation

A class implementation with __new__ method overridden

OutPut:

Note:

You can create instance inside __new__  method either by using super function or by directly calling __new__ method over object  Where if parent class is object. That is,

instance = super(MyClass, cls).__new__(cls, *args, **kwargs)

or

instance = object.__new__(cls, *args, **kwargs)

Things to remember

If __new__ return instance of  it’s own class, then the __init__ method of newly created instance will be invoked with instance as first (like __init__(self, [, ….]) argument following by arguments passed to __new__ or call of class.  So, __init__ will called implicitly.

If __new__ method return something else other than instance of class,  then instances __init__ method will not be invoked. In this case you have to call __init__ method yourself.

Applications

Usually it’s uncommon to override __new__ method, but some times it is required if you are writing APIs or customizing class or instance creation or abstracting something using classes.

Singleton using __new__

You can implement the singleton design pattern using __new__ method. Where singleton class is a class that can only have one object. That is, instance of class.

Here is how you can restrict creating more than one instance by overriding __new__

It is not limited to singleton. You can also impose limit on total number created instances

 

Customize Instance Object

You can customize the instance created and make some operations over it before initializer __init__  being called.Also you can impose restriction on instance creation based on some constraints

 

Customize Returned Object

Usually when you instantiate class it will return the instance of that class.You can customize this behaviour and you can return some random object you want.

Following  one is simple example to demonstrate that returning random object other than class instance

Output:

Here you can see when we instantiate class it returns  3 instead of instance reference. Because we are returning 3 instead of created instance from __new__ method. We are calling __init__ explicitly.  As I mentioned above, we have to call __init__ explicitly if we are not returning instance object from __new__ method.

The __new__ method is also used in conjunction with meta classes to customize class creation

Conclusion

There are many possibilities on how you can use this feature.  Mostly it is not always required to override __new__ method unless you are doing something regarding instance creation.

Simplicity is better than complexity. Try to make life easier use this method only if it is necessary to use.

 

Monitor apache webserver realtime using Apachetop

Apachetop is a monitoring tool to monitor  the  performance of apache server and to watch request which are being served live. It’s very likely based on application mytop. It displays current number of reads, writes,  number of requests processed so far and current request being processed. This tool will use apache access_log to  Monitor apache webserver realtime using apachetop

Install apachetop

If you are using CentOS you can install this application by using yum as follows

$ yum install apachetop

On Debian based systems you can install using apt-get as follows,

$ apt-get install apachetop

If you wanna install it from source, download/clone it from github and then compile/install.

Clone Repository

$ git clone https://github.com/tessus/apachetop.git

How to apachetop

Run the application to monitor apache webserver, to do so, type apachetop command. You would see the screen something like below

If you open apachetop. You can see the help by hitting letter h.

You can filter request by URL or referrers or hosts. You can toggle filter by using f when apachetop is running

 

This application by default assumes that the path of access_log file as /var/log/httpd/access_log. If you have a custom installation of apache or running apache on SCL (Software Collections).You can specify the path with option -f. i.e,

$apachetop -f /opt/rh/httpd24/root/etc/httpd/logs/access_log

 

GIT: How to push code to repository on remote ssh server

You can push to  git repository which is on remote ssh server using git. You can configure remote repository as a git remote to be able access quickly using remote(short) name.

To be able to pull and push changes to repository on remote ssh server you have to specify it using any one of the following URL format.

For Instance, if repository is located at  /opt/project, URL would be

ssh://user@server/opt/project

You can push changes using following syntax

git push ssh://user@server/opt/project  master

or

git push server master

Where,

server is remote configured using git remote  command
master is the branch to push

or, it can also be specified in scp-like syntax

For Instance, if repository is located at /opt/project, URL would be

user@server:/opt/project

You can push changes using following syntax

git push user@server:/opt/project  master

or

git push server master

Where,

server is remote configured using git remote  command
master is the branch to push

When you push changes, you will be prompted for the password. You can configure ssh keys to bypass password authentication when you push changes.

How to fix GIT object file is empty error

 

You might have encountered this weird git object file is empty  error when your machine/laptop ran out of power while you are working with git, boom 💣

In this case, you can recover your repository simply by cloning new from remote repository if you don’t have much changes locally. Else, you can follow below mentioned procedure to recover your local repository.

If you try to commit your local/stated changes.

When repository is corrupted you would see error something like below when you try to commit changes

Take a backup of directory .git

$ cp  -a  .git .git-old

Run, git fsck to get corrupted files/objects. i.e, git fsck –full

Now, our job is remove all empty files reported by git fsck

Once you delete all empty files reported by git fsck. git fsck will start showing up missing blob.

If you are lucky enough your problem will be solved with the following step.

NOTE: dangling blobs shown by git fsck are not error

If you delete all empty files, then run git fsck. If it is reporting “missing blob” then look checkout git reflog. It should show “fatal: bad object HEAD”, else probably your problem will be solved with just git reset

$ git reset

If your problem is not solved and git reflog is showing up “fatal: bad object HEAD”. Run the following command to fix broken HEAD

Now, update the HEAD with parent of last commit

Now, run git fsck. If you see error something like invalid sha1 pointer in cache-tree . You can fix it by removing .git/index file and resetting repo using git reset

Check the status and then commit if you required and problem is solved.

 

References:

[1] http://stackoverflow.com/questions/11706215/how-to-fix-git-error-object-file-is-empty