Homebrew on MAC OS (Yosemite) Update


After installed homebrew, when you run any “brew  command”, If you keep getting this error:

Homebrew requires Leopard or higher. For Tiger support, see:
https://github.com/mistydemeo/tigerbrew 

Do the following step to rectify this error.

Edit the brew.rb file to get this : (Which may be at /usr/local/Library)

Find following lines

if MACOS and MACOS_VERSION < 10.5
  abort <<-EOABORT.undent
    Homebrew requires Leopard or higher. For Tiger support, see:
    http://github.com/sceaga/homebrew/tree/tiger
  EOABORT
end    

Do following changes

if MACOS and MACOS_VERSION < 10.5 and MACOS_VERSION != 10.1
  abort <<-EOABORT.undent
    Homebrew requires Leopard or higher. For Tiger support, see:
    http://github.com/sceaga/homebrew/tree/tiger
  EOABORT
end

And yeah you done. Enjoy 🙂

How to implement system command using python


In this post I will show you that how you can exactly implemented system command using python.Here we use for example ls -l command. When I use this command in terminal it gives me following output

Harshs-MacBook-Pro:scripts harshkothari$ ls -l
total 32
-rw-r--r--  1 harshkothari  staff    13 Jul 23 02:33 demo.ini
-rw-r--r--@ 1 harshkothari  staff  1864 Jul 23 04:30 filesread.py
-rw-r--r--  1 harshkothari  staff  4121 Jul 23 04:30 langtofontmap.json

Now this command we can implement by following methods using python 🙂

  • os.system("command with args") passes the command and arguments to your system’s shell. This is nice because you can actually run multiple commands at once in this manner and set up pipes and input/output redirection.
    os.system("command < input_file | anothercommand > output_file")
    However, while this is convenient, you have to manually handle the escaping of shell characters such as spaces, etc. On the other hand, this also lets you run commands which are simply shell commands and not actually external programs. http://docs.python.org/lib/os-process.html

    >>> import os
    >>> os.system('ls -l')
    total 32
    -rw-r--r--  1 harshkothari  staff    13 Jul 23 02:33 demo.ini
    -rw-r--r--@ 1 harshkothari  staff  1864 Jul 23 04:30 filesread.py
    -rw-r--r--  1 harshkothari  staff  4121 Jul 23 04:30 langtofontmap.json
    0
  • stream = os.popen("command with args") will do the same thing as os.systemexcept that it gives you a file-like object that you can use to access standard input/output for that process. There are 3 other variants of popen that all handle the i/o slightly differently. If you pass everything as a string, then your command is passed to the shell; if you pass them as a list then you don’t need to worry about escaping anything. http://docs.python.org/lib/os-newstreams.html
    >>> import os
    >>> print os.popen('ls -l').read()
    total 32
    -rw-r--r--  1 harshkothari  staff    13 Jul 23 02:33 demo.ini
    -rw-r--r--@ 1 harshkothari  staff  1864 Jul 23 04:30 filesread.py
    -rw-r--r--  1 harshkothari  staff  4121 Jul 23 04:30 langtofontmap.json
  • The Popen class of the subprocess module. This is intended as a replacement for os.popenbut has the downside of being slightly more complicated by virtue of being so comprehensive. For example, you’d say
    print Popen("ls -l", stdout=PIPE, shell=True).stdout.read()
    instead of
    print os.popen("ls -l").read()
    but it is nice to have all of the options there in one unified class instead of 4 different popen functions. http://docs.python.org/lib/node528.html

    >>> from subprocess import Popen, PIPE
    >>> print Popen("ls -l", stdout=PIPE, shell=True).stdout.read()
    total 32
    -rw-r--r--  1 harshkothari  staff    13 Jul 23 02:33 demo.ini
    -rw-r--r--@ 1 harshkothari  staff  1864 Jul 23 04:30 filesread.py
    -rw-r--r--  1 harshkothari  staff  4121 Jul 23 04:30 langtofontmap.json
    
  • The call function from the subprocess module. This is basically just like the Popen class and takes all of the same arguments, but it simply wait until the command completes and gives you the return code. For example:
    call(["ls", "-l"], shell=True) http://docs.python.org/lib/node529.html

    >>> from subprocess import call
    >>> call(["ls", "-l"], shell=True)
    demo.ini                filesread.py            langtofontmap.json
    0
  • Using command module http://docs.python.org/2/library/commands.html
    >>> import commands
    >>> commands.getstatusoutput('ls -l')
    (0, 'total 32\n-rw-r--r--  1 harshkothari  staff    13 Jul 23 02:33 demo.ini\n-rw-r--r--@ 1 harshkothari  staff  1864 Jul 23 04:30 filesread.py\n-rw-r--r--  1 harshkothari  staff  4121 Jul 23 04:30 langtofontmap.json')
    >>> commands.getoutput('ls -l')
    'total 32\n-rw-r--r--  1 harshkothari  staff    13 Jul 23 02:33 demo.ini\n-rw-r--r--@ 1 harshkothari  staff  1864 Jul 23 04:30 filesread.py\n-rw-r--r--  1 harshkothari  staff  4121 Jul 23 04:30 langtofontmap.json'
    
  • The os module also has all of the fork/exec/spawn functions that you’d have in a C program

Hope this will help you 🙂

Pass Random variable to shell script rather then $1 or $2 using php


Here I am showing you how can you pass the random variable to shell script. ( i.e like $uname 🙂 )

You can receive data using $1 or $2 variable but you can’t directly receive data into $uname or any random variable.
Here is my bash file demo.sh

#!/bin/bash
echo $uname

Now I have to pass $uname variable so directly using this php function you cant do this

exec("sh demo.sh harshkothari",$output);

You have to send it via environment variable . This is simple code snippet how you can you pass this. php file name is shell.php

$uname = 'harshkothari';
putenv('uname='. $uname); //put environment variable uname 
exec("sh demo.sh",$output); //execute command
echo($output[0]. "\n"); //print return value

$output will return the last line of output and it will show like this

Harshs-MacBook-Pro:lcmapiuse harshkothari$ php shell.php 
harshkothari
Harshs-MacBook-Pro:lcmapiuse harshkothari$ 

Here is snapshot of terminal output

Output of php script, passing random variable to shell script

Output of php script, passing random variable to shell script

OpenCV on your MAC – Easy to install and Configure


OpenCV

OpenCV

OpenCV (Open Source Computer Vision Library) is a library of programming functions mainly aimed at real-time computer vision, developed by Intel, and now supported by Willow Garage and Itseez. It is free for use under the open source BSD license. The library is cross-platform. It focuses mainly on real-time image processing. If the library finds Intel’s Integrated Performance Primitives on the system, it will use these proprietary optimized routines to accelerate itself.

Here in this BlogPost I will explain you to install OpenCV(version 2.3, 2.4) on your MAC 🙂

There are two way to build openCV on your MAC.

1. You can use MacPorts

For this first you have to install MacPorts. The easiest way to install MacPorts on a Mac OS X system is by downloading the dmg for Mountain LionLionSnow Leopard orLeopard and running the system’s Installer by double-clicking on the pkg contained therein, following the on-screen instructions until completion. Installation package is here.

After installing MacPort you have to update it to latest version.

$ sudo port selfupdate

Now you can install OpenCV by running following command

$ sudo port install opencv

If you want to install python building

$ sudo port install opencv +python27

or with python 2.6

$ sudo port install opencv +python26

or with QT

$ sudo port install opencv +qt4

So by this way you can install OpenCV in your MAC OS.

2. You can use CMake

By using CMAKE you can also install OpenCV on  your mac. For that first you should have to install CMake, which thankfully comes with Mac binaries – so that was easy. You can download .dmg file here.

After installing CMake you have to download the OpenSource Package that you can download by clicking here.  Now you have OpenCV-2.4.3 ter.bz2 and unzip to folder. Now open terminal and go to that folder and write following commands in Terminal.

$ mkdir build

$ cd build

$ cmake -G "Unix Makefiles" ..

$ make -j8

$ sudo make install

Now You have done. Congrats you have installed OpenCV on your MAC 🙂

Now you can check by doing following thing.

GO to TerminalType Python

Now type

>>> import cv

Now if it run without any error then \m/ but if you encountered with following error

>>> import cv
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ImportError: No module named cv

Then close python by pressing ctrl + D key and now you have to run following command

 $ export PYTHONPATH=/usr/local/lib/python2.7/site-packages/

after running this command restart the Terminal and then open python and import cv this will work 🙂

If you have any doubt or problem then you can comment here. Thanks.

CoffeeScript – The Awesome way to write JavaScript


Do you struggle with Braces and semicolon in writing code of JavaScript. There is a solution of this problem and that is – CoffeeScript.  In this post I’ll explore  CoffeeScript – a minimalistic language that compiles to JavaScript.

CoffeeScript – The Awesome way to write JavaScript

CoffeeScript

CoffeeScript

CoffeeScript is a little language that compiles into JavaScript. Underneath all those awkward braces and semicolons, JavaScript has always had a gorgeous object model at its heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.

The golden rule of CoffeeScript is: It’s just JavaScript. The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. You can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa). The compiled output is readable and pretty-printed, passes through JavaScript Lint without warnings, will work in every JavaScript runtime, and tends to run as fast or faster than the equivalent handwritten JavaScript.

The language adds syntactic sugar inspired by Ruby,Python and Haskell. NO braces and semicolons. Since March 16, 2011, CoffeeScript has been on GitHub‘s list of most-watched projects, and as of 29 August 2012 is the eleventh most popular language on GitHub. CoffeeScript compiles predictably to JavaScript and programs can be written with less code, typically 1/3 fewer lines, with no effect on runtime performance

Syntax of CoffeeScript – Easy to use – Easy to Read

Here I am showing some example.

1. Assignment

str = "CoffeeScript"

2. Function

square = (x) -> x * x

3. Condition

string = "Passed" if  condition

4. Multiply numbers with 2

[1..10].map (i) -> i*2 

5. Object

math =
  root:   Math.sqrt
  square: square
  cube:   (x) -> x * square x

6. Loop

eat food for food in ['toast', 'cheese', 'wine']

CoffeScript vs JavaScript Code

CoffeScript vs JavaScript

Installation

The CoffeeScript compiler is itself written in CoffeeScript, using the Jison parser generator. The command-line version of coffee is available as a Node.js utility. The core compiler however, does not depend on Node, and can be run in any JavaScript environment, or in the browser.

To install, first make sure you have a working copy of the latest stable version of Node.js, and npm(the Node Package Manager). You can then install CoffeeScript with npm:

npm install -g coffee-script

(Leave off the -g if you don’t wish to install globally.)

If you’d prefer to install the latest master version of CoffeeScript, you can clone the CoffeeScriptsource repository from GitHub, or download the source directly. To install the lastest master CoffeeScript compiler with npm:

npm install -g http://github.com/jashkenas/coffee-script/tarball/master

Or, if you want to install to /usr/local, and don’t want to use npm to manage it, open the coffee-script directory and run:

sudo bin/cake install

For More Information and learning
1.http://coffeescript.org
2.http://jashkenas.github.com/coffee-script/documentation/docs/grammar.html