Wednesday, November 21, 2012

Push new code to Github

Create new repository (push fresh code to github)

1. Execute below steps (commands) in your local first - (g
o to the folder which you want to push to github)

git init
git add .
git commit -m "Initial commit"


2. Create new repo on github (using add new repository)
e.g. MyProject - https://github.com/jpa/myproject.git

3. Again go back to your local and use below commands to push the code

git remote add [name to use for remote] [private URI] # associate local repo to the remote
git push [name of remote] master # push your repository to the remote


Sample steps for it -

git remote add origin https://github.com/jpa/myproject.git

git fetch origin #It will pull the ReadMe file from git repository which you created on it.
git merge origin/master #Merge the changes with the local files.
git push origin #push the code.


All Set :)

Thursday, November 8, 2012

Browser Test

Test your web application on different platforms to see how it comes up on different OS and different browsers -



- http://www.browserstack.com/

- https://browserling.com/

- http://browsershots.org/

- http://crossbrowsertesting.com/

- https://browserlab.adobe.com/en-us/index.html

- IETester

browserstack and browsershots are really good.

Feel free to add to the list.

Wednesday, November 7, 2012

python url parse

Python got awesome module called urlparse. When you want different values from url, you might think of doing substring. Which is very risky and bad practice.

Here is the simple and easy way to do it in python -


>>> str = "http://demo.myapp.com/api/v1/items/26/"
>>> from urlparse import urlparse
>>> o = urlparse(str)
>>> o
ParseResult(scheme='http', netloc='demo.myapp.com', path='/api/v1/items/26/', params='', query='', fragment='')

>>> o.path
'/api/v1/items/26/'


That was easy!

You can do much more using this module, please checkout the more details on this Reference link.

Sunday, November 4, 2012

git cache issue (.gitignore)


Run into strange issue today, i had added few files/path of the files in .gitignore, though while doing git status it were keep coming up. I explicitly specified the file names in .gitignore, however it ignored my additions to .gitignore :(

After spending some time, went to stackoverflow/google and figured the solution.

If you running into similar issue,  following is the solution:

Remove the cache -
git rm -r --cached .

This removes everything from the index, then just run:
git add .


Commit it:
git commit -m ".gitignore cache fix"


It should be fine now, check -
git status

Null Check in different language


Python - Null Check

15 or "default"       # returns 15
0 or "default"         # returns "default"
None or "default"    # returns "default"
False or "default"    # returns "default"

Django Template - None Check


{{obj.item_value|default_if_none:"smile"}}

C# - Null Check

var data = val ?? "default value";

Java - Null check

Foo f = new Foo();
DummyObj obj = new Obj().getSelection();
String str = obj != null ? f.format(obj) : "";

Javscript - Null/Undefined check

if (! param) param = "abc";
//other way to check this
if (param == null) is same as if(!param)


jQuery - Null/existence check

if ( $('#myDivObj').length ) {}

Feel free to add comments and other languages Null check mechanism (inline or explicit) 

Friday, October 5, 2012

Know your environment: checkout versions

Often we run into situation to check what version we are running for particular framework, language, module.

Here, I have tried to list down the one I come across during my work -

Go to Python shell and follow below instruction for individual.

Django


>>> import django
>>> print django.VERSION
(1, 3, 1, 'final', 0)

Python


>>> import sys
>>> print sys.version
2.7.1 (r271:86832, Jul 31 2011, 19:30:53) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)]

NLTK


>>> import nltk
>>> nltk.__version__
'2.0.1rc4'

dateutil

>>> import dateutil
>>> dateutil.__version__
'1.5'

- Important thing to note here is python-dateutil 2.0 is not compatible with python 2.7 it only works with python 3.0. For 2.7 please try 1.5

-If you have 2.0, first uninstall and then install specific one


$ pip uninstall python-dateutil


$ pip install python-dateutil==1.5

Thursday, October 4, 2012

Sympy - Superb python library

Recently we came across requirement where we need to evaluate/compare/validate the algebra equation.

Different ways to do it -

- Use eval() function of python
>>> x = 2

>>> eval("x+2")
4


- Use Regex - Not a practical approach

- Use Sympy - works for simple cases which we are interested in, though probably we are just using the <10% of it.
>>> from sympy import *
>>> x = Symbol('x')
>>> y = Symbol('y')

>>> simplify(2*x+y)
2*x + y
>>> simplify(y+2*x)
2*x + y

p.s. both 2x+y and y+2x is same thing.


>>> simplify(2*(x+7)) == simplify(2*x + 14)
True
>>> simplify(2*(x+7)) == simplify(2*x + 13)
False
>>> simplify(2*(y+7)) == simplify(2*x + 14)
False
>>> simplify(2*(y+7)) == simplify(2*y + 14)
True


Here is the interesting one though -
>>> simplify((1/2) * 7*x) == simplify(7*x / 2)
False
>>> simplify(0.5 * 7*x) == simplify(7*x / 2)
True
>>> simplify((1.0/2.0) * 7*x) == simplify(7*x / 2)
True

Because in python if you specify 1/2, the answer is 0 as it consider it as positive integer, to get the decimal points you need to use 1.0/2.0

It can do expand the equation for you too -
>>> expand((x+y)**6)

x**6 + 6*x**5*y + 15*x**4*y**2 + 20*x**3*y**3 + 15*x**2*y**4 + 6*x*y**5 + y**6


For more information you can checkout here.