Wednesday, December 7, 2016

.vimrc

filetype off call pathogen#infect() call pathogen#helptags() filetype indent on filetype on filetype plugin on syntax on set expandtab set nobackup set nowritebackup set noswapfile set lines=40 set columns=80 set tabstop=4 set shiftwidth=4 set softtabstop=4 set autoindent set smarttab map j map k map l map h set backspace=indent,eol,start set ruler set ignorecase set scrolloff=5 " Add mapping for NERD Tree file browser. map n :NERDTreeToggle let NERDTreeWinSize=40 " Dont use fancy arrows for NERD Tree let g:NERDTreeDirArrows=0 set wildignore+=*.pyc,*.orig let g:ackprg="ack-grep -H --nocolor --nogroup --column" nmap a :Ack! let g:pyflakes_use_quickfix = 0

Saturday, January 17, 2015

fork and exec combination in python

fork() - will fork a new process with separate memory block, open file descriptors from parent process.
exec*() - it will replace current process with the program it is going to execute.


Above combination can be used to create several process in background.  exec*() in detail :
There are various confusing forms in python.

1. execl(<program_path>,<arg1>,<arg2>,..,<argN>)
     where,
     program_path : either input absolute path of program file or set PATH variable and specify only program name
     arg1,arg2,..,argN : These are arguments to program. Always remember to set arg1 to program name because in python sys.argv[0] is name of program. Actual program arguments are starting from arg2.

2. execv(<program_path>,args)
    where,
     program_path : either input absolute path of program file or set PATH variable and specify only program name
     args : Create a tuple of arguments where first element is program name because in python sys.argv[0] is name of program. Actual program arguments are starting from second element in tuple.

3. execlpe(<program_path>,arg1,arg2,..,..,env)
    - everything is similar to execl. Only difference is 'p' means python will look into PATH if program path is not specified. 'e' means we can pass dictionary which contains environment variable name and its value which will be set before executing program.

5. execvpe(<program_path>,args,env)
    - everything is similar to execv. Only difference is 'p' means python will look into PATH if program path is not specified. 'e' means we can pass dictionary which contains environment variable name and its value which will be set before executing program.

Try below program:

[manas_khandeshe@static-61 ~/Documents/sandbox]cat test.sh
#!/bin/bash

script_name=$0
arg1=$1
current=`pwd`
echo "Script name : $script_name, $arg1"
echo "XYZ: "$XYZ
echo "PATH: "$PATH
echo "current directory: $current"



#!/usr/bin/env python3
import os
import sys

def _execl():
    arg1 = "test"
    arg2 = "abc"
    os.environ["XYZ"] = 'MyXYZ'
    os.execlp("/Users/manas_khandeshe/Documents/test.sh",arg1,arg2)
    return
 

def _execlpe():
    arg1 = "test"
    arg2 = "abc"
    env = {
        "XYZ" : 'MyXYZ'
    }
    os.execlpe("/Users/manas_khandeshe/Documents/sandbox/test.sh",arg1,arg2,env)
    return
 

def _execv():
    args = ("test","abc")
    os.environ["XYZ"] = 'MyXYZ'
    os.execv("/Users/manas_khandeshe/Documents/sandbox/test.sh",args)
    return
 

def _execvpe():
    env = {
        "PATH":"/Users/manas_khandeshe/Documents/sandbox",
        "XYZ":"BlaBla"
    }
    args = ("test","abc")
    os.execvpe("test.sh",args,env)
    return



def main():
    _execl()
    #_execlpe()
    #_execv()
    #_execvpe()
    return

main()
exit(0)

Saturday, January 10, 2015

What does fork means in python




Technically, a forked process gets a copy of the original process’s global memory, including open file descriptors. Because of that, global objects like files start out with the same values in a child process, so all the processes here are tied to the same single stream. But it’s important to remember that global memory is copied, not shared; if a child process changes a global object, it changes only its own copy. (As we’ll see, this works differently in threads, the topic of the next section.)

Friday, December 26, 2014

Where to put libraries in python ?  Follow any of the following approach.

1. Current working directory

2. Set 'PYTHONLIB' environment variable at command line shell

3. In your script, do
    import sys
  sys.path.append(<directory location>)

4. Put in Python specific default lib locations. 
    e.g. /usr/lib/python3.3 

5. Create <anyname>.pth file with list of directory abs paths separated by new line. Where each path contains location of library files.
   import site
   site.addsitedir('/some/dir/you/want/on/the/path')
  

variable scope rule in python

variable scopes in python:
For a variable, python interpreter will access/search it starting from Local scope.
If it is not found in local scope, it will check in Enclosing defs as per following order.


L (local)
 -> E (enclosing def or lamba)
        -> G (global)
              -> B (built in)

import module vs ( from module import name1)

in python, statement like this one:

from module import name1, name2  # Copy these two names out (only)



is equivalent to this statement sequence:


import module                     # Fetch the module object
name1 = module.name1      # Copy names out by assignment
name2 = module.name2
del module


>> It means python interpreter will load entire module even if you want only few attributes of module.

Thursday, November 29, 2012

bulk rename

It's very simple command, but I never used before [I was going to write a script for this req. :)]

[mandy@mandy mpix]$ ls *20121031000012*
mp1_20121031000012.txt  mp2_20121031000012.txt  mp3_20121031000012.txt

[mandy@mandy mpix]$ rename "20121031000012" "20121031000013" *20121031000012.txt

[mandy@mandy mpix]$ ls *20121031000013*
mp1_20121031000013.txt  mp2_20121031000013.txt  mp3_20121031000013.txt

Sunday, November 25, 2012

Dealing with images??, get ImageMagick

I am using and playing a lot with the images in my current project. It involves heavy dealing with png's, jpg's and gif's like: merging, cutting, re-scaling and stuff.

There is a very cool library to deal with all these things : ImageMagick.

You can do pretty much anything with this.

You have extension available for PHP for imagemagick named as Imagick.( Perl also have an extension).

This could be a very good start if you are interested:
http://www.imagemagick.org/Usage/compose/

Saturday, November 24, 2012

MySQL Security Best Practices

Very useful link for MySQL Security:

http://www.greensql.com/articles/mysql-security-best-practices

Monday, October 29, 2012


How to write perl shell.

[root@manas ~]cat /tmp/perl_shell
#!/usr/bin/perl

my($output);
do {
    $output = eval;
    if($output =~ /^\s*$/) {
        print "\n$@";
    }
    else {
        print "\n$output";
    }
    print "\nperl_shell>";
} while(<>);


Now you can use perl shell like following :
perl_shell>print "Mandar Pande is a rich man ! "
Mandar Pande is a rich man !

perl_shell>print rand(100)
27.8423844211833

perl_shell>print sqrt(16);
4

simple tab settings in vi/vim


Pretty simple setting ! But I used to forget every time :). 
Python coding would be hell, if this setting is not defined in .vimrc.

set smartindent
set tabstop=4
set shiftwidth=4
set expandtab

Guys, Please continue to update .vimrc settings on this post. 

Wednesday, October 24, 2012

lftp file transfer in shell script


script uses conf file as an input:

[mandy@mandy mpix]$ cat lftp.sh 
#!/bin/sh
lftp -f lftp.conf

Input file uses open command on port 2121:

[mandy@mandy mpix]$ cat lftp.conf 
open -p 2121 server_name
mput file1
mput file2
mput file3
bye

Saturday, October 20, 2012

Remote copy using tar over ssh.

$tar -cvf - <source_content> | ssh <user@hostname> "tar -xvf - -C <where_to_copy>"

where,
-  : tells tar to compress and send output of tar to terminal ( at source )
- : tells tar to accept input from terminal and extract into directory specified by -C.

Observations :
It takes at least 40% of less time than actual scp/rcp takes !!

Wednesday, October 17, 2012

tar quick ref

List the files from tar:
------------------------

[mandy@mandy mpande]$ tar -tvf mp.tar
-rw-r----- mandy/mandy 12 2012-08-30 05:13:06 20120830000001.txt
-rw-r----- mandy/mandy 12 2012-08-30 05:13:15 20120830000002
-rw-r----- mandy/mandy 12 2012-08-30 05:13:25 20120830000003.txt
-rw-r----- mandy/mandy 12 2012-08-30 05:11:32 3
-rw-r----- mandy/mandy 12 2012-08-30 05:11:34 4
-rw-r----- mandy/mandy 12 2012-08-30 05:11:37 5

List only *2012* files from tar:
--------------------------------

[mandy@mandy mpande]$ tar -tvf mp.tar --wildcards --no-anchored '*2012*'
-rw-r----- mandy/mandy 12 2012-08-30 05:13:06 20120830000001.txt
-rw-r----- mandy/mandy 12 2012-08-30 05:13:15 20120830000002
-rw-r----- mandy/mandy 12 2012-08-30 05:13:25 20120830000003.txt

Extract files with *2012* from tar:
-----------------------------------

[mandy@mandy mpande]$ tar -xvf mp.tar --wildcards --no-anchored '*2012*'
20120830000001.txt
20120830000002
20120830000003.txt

List files excluding certain pattern:
-------------------------------------

[mandy@mandy mpande]$ tar -tvf mp.tar --exclude *2012*
-rw-r----- mandy/mandy 12 2012-08-30 05:11:32 3
-rw-r----- mandy/mandy 12 2012-08-30 05:11:34 4
-rw-r----- mandy/mandy 12 2012-08-30 05:11:37 5

Extract files excluding certain pattern:
----------------------------------------

[mandy@mandy mpande]$ tar -xvf mp.tar --exclude *2012*
3
4
5

Monday, October 8, 2012

Reset MySQL root Password

Hello,

Many times it happens that we do forget the root password for our mysql instance(whether local or on server if you have rights). It also happens, if we start using a system in which mysql was already there and we don't know whats the root login.

Following steps can help in resetting the root password
Note: You must have root privileges or sudo access to execute the commands :)

1) Stop the mysql daemon:
             like: /etc/init.d/mysql stop
2) Start to MySQL server w/o password:
             use skip-grant-tables option while restarting.
3) Connect to mysql server as the root user:
             mysql -u root
4) Setup new mysql root account password i.e. reset mysql password:
             update user set password=PASSWORD("new password") where User='root';
             (we need to enter password in encrypted form, so using password())
5) Exit and restart the MySQL server:
               like: /etc/init.d/mysql restart





Hope this is useful.

Tuesday, September 25, 2012

Perl smart match operator

Many times, it is very handy to use this operator for comparing arrays, hashes:
use 5.010;

my(@arr_a)=qw (123 abc def ghi);
my(@arr_b)=qw (123 abc def ghi);

print "same2same\n" if (@arr_a ~~ @arr_b);

Tuesday, September 18, 2012

How to setup yum in RHEL 6.0

[root@localhost ~]# cat /etc/yum.repos.d/iso1.repo <-- this name can be anything but in the format *.repo
[iso1.repo]
name=RHEL6
baseurl=ftp://<servername>/<path where repodata directory is available> < ---- [ it means 'repodata' dir should be available in this path ]
enabled=1
gpgcheck=0





==============================================================
Above procedure should be same for other OS.

Thursday, August 30, 2012

I haven't ever heard about open source Search Engine kinda thingy...... :P

Came around this:
The Apache Lucene project develops open-source search software
Apache Lucene
NOOP operator in Bash:

A standard NOOP Operator in bash is colon ":".

Try out:
: echo test or anything :)
[m@root]$ a=1
[m@root]$ b=2
[m@root]$ c=3
[m@root]$ echo $a; : echo $b ; echo $c
1
3


Thanks,
Kailash Chandak