Monday, October 17, 2016

ipython notebook

Run it on a port of your choice:

ipython notebook --no-browser --ip=* --port=8889

Thursday, September 8, 2016

Git: How to check in a folder, but ignore its contents

Git does not let you to check in an empty folder, even if you are using it as a temp output location. How to work around it?

put this .gitignore into the folder, then git add .gitignore
*
*/
!.gitignore
The * line tells git to ignore all files in the folder, but !.gitignore tells git to still include the .gitignore file. This way, your local repository and any other clones of the repository all get both the empty folder and the .gitignore it needs.
May be obvious but also add */ to the git ignore to also ignore sub folders.

Tuesday, August 30, 2016

Using with to handle exception inside of your python unit tests

I am sure you have seem something like that in one of the tests

def test_me(self):
  with self.assertRaises(ZeroDivisionError):
    1 / 0

What does it do? It is simply means that test_me would pass if the logic inside of it would throw ZeroDivisionError exception.

But what all that 'with' stuff?

If you ever tried to open up a file to read data, etc you certainly remember doing all error checking, catching errors, closing files, etc... nightmare, hence context manager was introduced that would allow you to open the file by using 'with' and majority of checks and file closing was done for you.

Same in this case, you are using 'with' in this case to use assertRaises as a context manager that ensures that the error in this case is properly caught and cleaned up while it is being recorded. (Same idea as dealing with files...)

So without context manager, your code would have looked like following the old format

def test_me_in_old_way(self):
  import operator
  self.assertRaises(ZeroDivisionError, operator.devide, 1, 0)

First argument is error type that you expect to raise, second is the operator, and last two are the args.
Much more verbose

Thursday, August 25, 2016

Python Aha's

1. To check if you have certain package in your python instance and what its version is
$ python
>> import sqlalchemy
>> sqlalchemy.__version__

2. Path at terminal when executing this file
os.getcwd()

3. 

Tuesday, August 23, 2016

Ruby Aha's

Commenting out in ERB file

single line
<%-# commented line -%>
whole block 
<% if false %>
code to comment
<% end %>

To run Rails in PRODUCTION mode

RAILS_ENV=production rails server
If you see css or javascript files being lost make sure to execute

rake assets:precompile


and then start it via

RAILS_SERVE_STATIC_FILES=true rails server -b <ip.address> -e production

Move your dev database to use posgres instead of default sqllite

Install postgress gem - Update your Gemfile 
gem "pg"

Connect it via database.yml file where you to provide database information (host, user/pass, port, etc)

Migrate database scripts into it
rake db:migrate RAILS_ENV=development

Populate sample data via seed
rake db:seed

fsff

Wednesday, August 17, 2016

Linux Aha's

How to find processes running on a certain port... and kill them

Get the processes for port 5000

lsof -t -i:5000

or netstat -nlp | grep :5000

Now find where the process is running from...

 ls -l /proc/[pid]/exe

Kill processes running on port 5000


kill 'lsof -t -i:5000'

How to add trusted root certificated to the server

  1. Install the ca-certificates package:
    yum install ca-certificates
  2. Enable the dynamic CA configuration feature:
    update-ca-trust force-enable
  3. Add it as a new file to /etc/pki/ca-trust/source/anchors/:
    cp foo.crt /etc/pki/ca-trust/source/anchors/
  4. Use command:
    update-ca-trust extract
More here...

Wednesday, August 10, 2016

Git: ignore files

Let's say that you executed following lines and got whole bunch of random files that are ready to be checked in when you type git status

git init
git add .
git status

How do you remove them and keep them out of your git repo?
Create .gitignore file and add regex expressions into it to match files that you want to exclude

touch .gitignore
vim .gitignore

<
.gitignore
etc
>

What to do with the files that are already ready to be checked in? Remove them via following commad

git rm --cached FILENAME

Now check what you have going to be committed. Should be all clean by now

git status

Commit!

git commit -m "Initial commit"