Shantanu's Blog

Corporate Consultant

December 31, 2017


Machine learning basics

Machine learning is used to learn from given data and then predict values. For e.g. here is share price of a company for given years.

import numpy as np
xval = np.array([2001,2002,2003,2003,2004,2003,2006,2008,2009,2010]).reshape(-1,1)
yval = [1,2,3,4,5,6,7,7,9,10]

We need to create a model to store the data...

import sklearn.linear_model as skl
model = skl.LinearRegression()

The fit method of model will learn and help us predict values. In this case the price expected for the year 2012 is around 11.66,yval)

array([ 11.66141732])

We can also plot the data to understand how the values are moving acorss years...

import pylab as py


December 30, 2017


Install mysql with tokuDB engine within percona

This is required if you get an error while initiating tokudb engine:

echo never > /sys/kernel/mm/transparent_hugepage/enabled

And this is required if you get permissions error:

rm -rf /storage/custom3381

mkdir /storage/custom3381

chown 1001 /storage/custom3381

percona server has built-in environment variable for tokudb:

docker run -p 3381:3306 -v /my/custom3381:/etc/mysql/conf.d -v /storage/custom3381:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=india3381 -e INIT_TOKUDB=1 -d percona/percona-server:5.7

Labels: ,


Using xtra-backup for incremental backups

1) Download xtrabackup package
2) change directory
3) Full Backup
4) INcremental backup
5) Restore
6) Start mysql using backup
# Linux

# centOS and redhat
yum install
yum install percona-xtrabackup-24

cd percona-xtrabackup-2.4.9-Linux-x86_64

bin/xtrabackup --defaults-file=/my/custom3396/my.cnf -H -uroot -pindia3396 -P 3396 --datadir /storage/mysql/datadir3396 --backup --target-dir=/data3/backups/full/

The main advantage of using xtrabackup is that we can take incremental backup that will be much faster.

bin/xtrabackup --defaults-file=/my/custom3396/my.cnf -H -uroot -pindia3396 -P 3396 --datadir /storage/mysql/datadir3396 --backup --target-dir=/data3/backups/inc1 --incremental-basedir=/data3/backups/full/

The next day, we need to simply change the target directory path to "inc2" like this:

bin/xtrabackup --defaults-file=/my/custom3396/my.cnf -H -uroot -pindia3396 -P 3396 --datadir /storage/mysql/datadir3396 --backup --target-dir=/data3/backups/inc2 --incremental-basedir=/data3/backups/inc1

In case of disaster we need to apply logs and then prepare data:

1) First apply logs of target directory:
bin/xtrabackup --prepare  --apply-log-only --target-dir=/data3/backups/full/

2) Apply logs from incremental backup:
bin/xtrabackup --prepare --apply-log-only --target-dir=/data3/backups/full/ --incremental-dir=/data3/backups/inc1

3) apply log only option should not be used for the last incremental backup.
bin/xtrabackup --prepare  --target-dir=/data3/backups/full/  --incremental-dir=/data3/backups/inc2

4) Finally prepare target without apply log option for target directory:
bin/xtrabackup --prepare --target-dir=/data3/backups/full/

Now since the backup data directory is ready, we can create a new docker container pointing to the newly "prepared" data.

docker run -p 3391:3306 -e MYSQL_ROOT_PASSWORD=india3391 -v /my/custom3391:/etc/mysql/conf.d  -v /data3/backups/full:/var/lib/mysql -d shantanuo/mysql:5.7

You can check if the new data is working correctly.

mysql -h `hostname -i` -uroot -pindia3396 -P 3391


December 19, 2017


Using property in python class

Here is how a standard class look like. When I call monthly function, I get the default 35000 value. I can however set a new value by calling another function called monthly_updated.

class pay_check:
    def __init__(self):
        self._salary = 35000

    def monthly(self):
        return self._salary
    def monthly_updated(self, value):


This works, but it is possible to improve the usability of the class by adding property decorator. I make the monthly function as default getter that will be called when the user request the property method.

class pay_check:
    def __init__(self):
        self._salary = 35000

    def monthly(self):
        return self._salary

    def monthly(self, value):


Instead of myclass.monthly() I can now simply use myclass.monthly (without brackets)

Another advantage is that I can use the same method to set the new value as shown below:

Now the new value of salary is 50,000 as returned by this:

There are many advantages of using this style of programming. The code is readable, elegant and can be easily maintained. The user may slightly get confused with property concept since he has only seen functions as methods. But once he understand this, he can not live without it!

For e.g.
df.columns will return the column headings, but I can use the same function name to change the column names like this...
df.columns=['name', 'experience', 'remuneration', 'amount']

Or set a new value for the entire column:
df['dummy'] = '0'

And return the values of the given column using the same slice like this...

Understanding how "get", "set" and "del" properties are handled in a class is very important to manage the class instances.

Labels: ,

December 17, 2017


list all files from S3 bucket

# Here is the python code that will check if any of the files in a given S3 bucket is publicly accessible. Change your-bucket-name, region and access / secret key

import boto
from boto.s3.connection import OrdinaryCallingFormat
conn = boto.s3.connect_to_region('ap-south-1', aws_access_key_id='xxx', aws_secret_access_key='xxx',calling_format=OrdinaryCallingFormat())

mybucket = conn.get_bucket('your-bucket-name')
for key in mybucket.list():
      for grant in key.get_acl().acl.grants :
            if grant.permission == 'READ' :
                print ("PUBLIC: " +str(key))

Labels: ,


June 2001   July 2001   January 2003   May 2003   September 2003   October 2003   December 2003   January 2004   February 2004   March 2004   April 2004   May 2004   June 2004   July 2004   August 2004   September 2004   October 2004   November 2004   December 2004   January 2005   February 2005   March 2005   April 2005   May 2005   June 2005   July 2005   August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006   January 2007   February 2007   March 2007   April 2007   June 2007   July 2007   August 2007   September 2007   October 2007   November 2007   December 2007   January 2008   February 2008   March 2008   April 2008   July 2008   August 2008   September 2008   October 2008   November 2008   December 2008   January 2009   February 2009   March 2009   April 2009   May 2009   June 2009   July 2009   August 2009   September 2009   October 2009   November 2009   December 2009   January 2010   February 2010   March 2010   April 2010   May 2010   June 2010   July 2010   August 2010   September 2010   October 2010   November 2010   December 2010   January 2011   February 2011   March 2011   April 2011   May 2011   June 2011   July 2011   August 2011   September 2011   October 2011   November 2011   December 2011   January 2012   February 2012   March 2012   April 2012   May 2012   June 2012   July 2012   August 2012   October 2012   November 2012   December 2012   January 2013   February 2013   March 2013   April 2013   May 2013   June 2013   July 2013   September 2013   October 2013   January 2014   March 2014   April 2014   May 2014   July 2014   August 2014   September 2014   October 2014   November 2014   December 2014   January 2015   February 2015   March 2015   April 2015   May 2015   June 2015   July 2015   August 2015   September 2015   January 2016   February 2016   March 2016   April 2016   May 2016   June 2016   July 2016   August 2016   September 2016   October 2016   November 2016   December 2016   January 2017   February 2017   April 2017   May 2017   June 2017   July 2017   August 2017   September 2017   October 2017   November 2017   December 2017  

This page is powered by Blogger. Isn't yours?