Tuesday, November 18, 2014

Postgres command cheatsheet

http://blog.jasonmeridth.com/posts/postgresql-command-line-cheat-sheet/

change to postgres user and open psql prompt
$ sudo -u postgres psql postgres

Once the postgres database is running the prompt will look like this: postgres=#
(So don't type in postgres=#, only what follows in the following commands...)

list databases
postgres=# \l

list roles
postgres=# \du

create role
postgres=# CREATE ROLE demorole1 WITH LOGIN ENCRYPTED PASSWORD 'password1' CREATEDB;

create role with multiple privileges
postgres=# CREATE ROLE demorole1 WITH LOGIN ENCRYPTED PASSWORD
postgres=# 'password1' CREATEDB CREATEROLE REPLICATION SUPERUSER;

alter role
postgres=# ALTER ROLE demorole1 CREATEROLE CREATEDB REPLICATION SUPERUSER;

drop role
postgres=# DROP ROLE demorole1;

create database
postgres=# CREATE DATABASE demodb1 WITH OWNER demorole1 ENCODING 'UTF8';

grant privileges to new user
postgres=# GRANT ALL PRIVILEGES ON DATABASE demodb1 TO demorole1;

drop database
postgres=# DROP DATABASE demodb1;

connect to database
postgres=# \c <databasename>

list tables in connected database
postgres=# \dt

list columns on table
postgres=# \d <tablename>

backup database


$ pg_dump <databasename> > <outfile> 

Create a Base Web App -

Web App Meetup Homework 1


Create a New Rails App

  1. $ rails new <name_of_app>
  2. $ cd <name_of_app>
  3. $ bundle install


Install Devise




Install Postgres

http://www.postgresql.org/download/linux/ubuntu/

$ gem install pg

Add gem 'pg' to your Gemfile and run bundle install.

-------------------------------
In config/database.yml, use the following settings:
(yaml is white space delimited so make sure you space it exactly like this)

development:
  adapter: postgresql
  encoding: unicode
  database: <yourdatabasename>
  pool: 5
  password: <yourpassword_orleaveitblank>

test:
  adapter: postgresql
  encoding: unicode
  database: <yourdatabasename>
  pool: 5
  timeout: 5000

production:
  adapter: postgresql
  database: <yourdatabasename>
  pool: 5
  timeout: 5000
  user: <yourusername>
  password: <yourpassword>
----------------------------------
For Ubuntu Trusty64

1. Create the file /etc/apt/sources.list.d/pgdg.list
2. add a line for the repository
deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main

Import the repository signing key, and update the package lists
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \
sudo apt-key add -
sudo apt-get update

------------------------------------
http://stackoverflow.com/questions/20587779/heroku-wont-recognize-pg-gem-in-my-rails-app-gemfile

Once installed, make sure you initialize the pg datastore.
also, depending how your pg is installed, you might need to specify the username and host in your database.yml file
  username: someuser_on_pg
  host: localhost

Saturday, November 15, 2014

Ruby Interview Questions

http://thereq.com/q/best-ruby-software-interview-questions/easy

Array manipulation

Suppose you have the following array.

stuff = [:dog,:cat,:orange,:banana]

  • How can you slice this array to create a new array [:cat,:orange]
  • Add the element :apple on to the end of the array.
  • Now take :apple back off again
  • Add the element :fish to the start of the array.
  • Now remove the element :fish.

Friday, November 14, 2014

Understanding Algorithms - Resources

https://fiftyexamples.readthedocs.org/en/latest/algorithms.html

Calling something an algorithm means that the following properties are all true:
  • An algorithm is an unambiguous description that makes clear what has to be implemented. In a recipe, a step such as “Bake until done” is ambiguous because it doesn’t explain what “done” means. A more explicit description such as “Bake until the cheese begins to bubble” is better. In a computational algorithm, a step such as “Choose a large number” is vague: what is large? 1 million, 1 billion, or 100? Does the number have to be different each time, or can the same number be used on every run?
  • An algorithm expects a defined set of inputs. For example, it might require two numbers where both numbers are greater than zero. Or it might require a word, or a list of zero or more numbers.
  • An algorithm produces a defined set of outputs. It might output the larger of the two numbers, an all-uppercase version of a word, or a sorted version of the list of numbers.
  • An algorithm is guaranteed to terminate and produce a result, always stopping after a finite time. If an algorithm could potentially run forever, it wouldn’t be very useful because you might never get an answer.
  • Most algorithms are guaranteed to produce the correct result. It’s rarely useful if an algorithm returns the largest number 99% of the time, but 1% of the time the algorithm fails and returns the smallest number instead. [1]
  • If an algorithm imposes a requirement on its inputs (called aprecondition), that requirement must be met. For example, a precondition might be that an algorithm will only accept positive numbers as an input. If preconditions aren’t met, then the algorithm is allowed to fail by producing the wrong answer or never terminating.



http://core.ecu.edu/csci/wirthj/Algorithms/introAlgo-c.html
  An algorithm is a series of specific steps which solve a particular problem.
series
  • The steps must be done in a particular order and each of the steps must be used (unless the algorithm says otherwise).
specific
  • A step must NOT be replaced by a similar step.
steps
  • Like a cooking recipe, an algorithm tells you to do things. Each thing you do is called a step (aka instruction, aka command)
solve
  • An algorithm produces a final result (aka output) which is the solution to a problem.
particular problem
  • The algorithm for one problem will not usually solve a different problem.
  • The details defining the problem must be made available at the start of the algorithm. These details are called givens. (aka parameters).

  • Most algorithms have these basic parts:
  • Description of Problem
  • Set up
  • Parameters
  • Execution
  • Conclusion

http://www.cleveralgorithms.com/


Online Classes

Algorithms
http://theopenacademy.com/content/lecture-1-analysis-algorithms


Logic
https://www.coursera.org/course/intrologic

Monday, September 29, 2014

Mob Programming with Pat Maddox and Alistair Cockburn

My second Summer of Code is over so I am back to self study although I am not studying by myself right now. I am mobbing with Pat Maddox and Alistair Cockburn.

I spent the day with them we discussed a lot of things I had to Google:

Approval Testing
Approval tests take a snapshot of the results, and confirm that they have not changed.

http://approvaltests.sourceforge.net/ 
http://www.slideshare.net/lynnlangit/approval-tests-an-open-source-unit-testing-library-for-net-java-ruby-or-php-12532737

Golden Master Pattern
http://blog.adrianbolboaca.ro/2014/05/legacy-coderetreat-golden-master/

For all the given situations we need to think if the system tests generated with the Golden Master are enough, or we need to start adding other types of tests like unit tests, component tests, integration tests, security tests, etc.

The Golden Master technique is based on the following steps:
Find the way the system delivers its outputs
Check for clear outputs of the system: console, data layer, logger, file system, etc. 

Find a way to capture the output of the system without changing the production code
Think if that output can be “stolen” without changing the production code. For example you could “steal” the console output by redirecting the stream of the console to an in-memory stream. Another example would be injecting an implementation of a data layer interface that would write to another data source than the production code would. 

Find a pattern of the outputs
The output of the system could be text or a data structures tree or another type of stream. Starting from this output type you can decide if you could go on to the next step. 

Generate enough random inputs and persist the tuple input/output
In the case you can persist the outputs to a simple data repository, you can think about what are the inputs for the corresponding outputs. Here the magic consists in finding a good balance of random or pseudo-random inputs. We need inputs that would cover most of the system with tests, but in the same time the test would run in seconds. For example in the case of a text generating algorithm we need to decide if we want to feed the system with 1000 or one million entry data. Maybe 10 000 input data are enough. We will anyway check the tests coverage during the last stage.
We need to persist the pair input-output. The output would be the Golden Master, the reference data we will always check our SUT against. 

Write a system test to check the SUT against the previously persisted data
Now that we have a way to “steal” the outputs and we have a guess about how to generate enough input-output pairs, but not to many, we can call the system and check it against the outputs for a given input. We need to check if the test touches the SUT and if it passes. We also need to check that the test runs fast enough, in seconds. 

Commit the test
Always when a green test passes we need to commit the code to a local source control system. Why? So that we can easily revert to a stable testable system.
Important: Do not forget to commit also the golden masters (files, database, etc)! 

Check test behaviour and coverage

Hexagonal Architecture (http://alistair.cockburn.us/Hexagonal+architecture)
Allow an application to equally be driven by users, programs, automated test or batch scripts, and to be developed and tested in isolation from its eventual run-time devices and databases.



As events arrive from the outside world at a port, a technology-specific adapter converts it into a usable procedure call or message and passes it to the application. The application is blissfully ignorant of the nature of the input device. When the application has something to send out, it sends it out through a port to an adapter, which creates the appropriate signals needed by the receiving technology (human or automated). The application has a semantically sound interaction with the adapters on all sides of it, without actually knowing the nature of the things on the other side of the adapters.


This inspired: JavaScript non-framework called http://hexagonaljs.com/. It's so easy to extract GuiAdapter and StorageAdapter, leaving the middle hex boundary-unaware.

CQRS Pattern 
Command Query Responsibility Segregation built on Alistair's Hexagonal Architecture by Greg Young. http://cqrs.files.wordpress.com/2010/11/cqrs_documents.pdf 
Here is Alistair describing it: http://www.slashdotdash.net/2010/09/27/alistair-cockburns-lightning-talk-on-cqrs-recorded-at-the-mountain-west-ruby-conference-2010/

Monday, July 7, 2014

Command Line Tips

Excellent Resource:
https://github.com/0nn0/terminal-mac-cheatsheet/wiki/Terminal-Cheatsheet-for-Mac-(-basics-)

$ Ctrl+A -  to send the cursor to start of the command

$ Ctrl+E - to send the cursor to the end of the command.

$ Ctrl+C - used to quit a running program in terminal

$ Ctrl U - clears the line

$ Ctrl+L - clears the screen while retaining whatever was there on the current prompt

$ Ctrl+R - and write few characters from the start of command you’re looking for


$ echo <whatever> - reads back in the terminal whatever you write

$ cat <filename> - reads the file

$ touch <filename> - creates a new file

$ head -5 output - displays content from the beginning of a file

$ tail -5 output - displays the last five lines from the file ‘output’


$ history | grep "myarticle.txt"

$ rm -rf dirname - to force remove a directory and it's contents

$ ! - runs a previously run command


$ jobs - lists all the commands running in the background !

$ ps - displays a list of the processes you’ve launched

$ fg - bring a specific job back to the foreground

$ kill %<job-number> kills a process by it's job-number in the list or

$ kill<PID> kill a process by is process id (PID) 1234




sudo lsof -i - "list open files", which is used in many Unix-like systems to report a list of all open files and the processes that opened them. http://danielmiessler.com/study/lsof/

On a Mac you can 
$ Option + Click - to get to a specific spot in your commandline

For fun:

$ telnet towel.blinkenlights.nl 

http://www.tecmint.com/20-funny-commands-of-linux-or-linux-is-fun-in-terminal/


http://www.linuxjournal.com/content/time-saving-tricks-command-line

http://www.linuxuser.co.uk/tutorials/14-command-line-tips-tricks

http://mylinuxbook.com/5-really-cool-linux-command-line-tricks/

https://linuxacademy.com/blog/linux/ten-things-i-wish-i-knew-earlier-about-the-linux-command-line-2/

Blank Terminal Window
http://www.cnet.com/news/os-x-terminal-displays-a-blank-window-instead-of-a-command-prompt/

commandline windows pwd

http://www.lemoda.net/windows/windows2unix/windows2unix.html

arparp
assignlnCreate a file link
assignln -sOn Unix, a directory may not have multiple links, so instead a symbolic link must be created with ln -s.
assocfile
atat
batch
cron
attribchown
chmod
Sets ownership on files and directories
cdcdOn Windows, cd alone prints the current directory, but on Unix cd alone returns the user to his home directory.
cdpwdOn Windows, cd alone prints the current directory.
chkdskfsckChecks filesystem and repairs filesystem corruption on hard drives.
clsclearClear the terminal screen
copycp
date
time
dateDate on Unix prints the current date and time. Date and time on Windows print the date and time respectively, and prompt for a new date or time.
delrm
deltreerm -rRecursively deletes entire directory tree
dirls"dir" also works on some versions of Unix.
doskey /h
F7 key
historyThe Unix history is part of the Bash shell.
editvi
emacs
etc.
edit brings up a simple text editor in Windows. On Unix, the environment variable EDITORshould be set to the user's preferred editor.
exitexit
Control-D
On Unix, pressing the control key and D simultaneously logs the user out of the shell.
explorernautilus
etc.
The command explorer brings up the file browser on Windows.
fcdiff
findgrep
ftpftp
helpman"help" by itself prints all the commands
hostnamehostname
ipconfig /allifconfig -aThe /all option lets you get the MAC address of the Windows PC
memtopShows system status
mkdirmkdir
moremore
less
movemv
net sessionw
who
net statisticsuptime
nslookupnslookup
pingping
printlprSend a file to a printer.
reboot
shutdown -r
shutdown -r
regeditedit /etc/*The Unix equivalent of the Windows registry are the files under /etc and /usr/local/etc. These are edited with a text editor rather than with a special-purpose editing program.
rmdirrmdir
rmdir /srm -rWindows has a y/n prompt. To get the prompt with Unix, use rm -i. The i means "interactive".
setenvSet on Windows prints a list of all environment variables. For individual environment variables, set <variable> is the same as echo $<variable> on Unix.
set Pathecho $PATHPrint the value of the environment variable using set in Windows.
shutdownshutdownWithout an option, the Windows version produces a help message
shutdown -sshutdown -hAlso need -f option to Windows if logged in remotely
sortsort
start&On Unix, to start a job in the background, use command &. On Windows, the equivalent isstart command. See How to run a Windows command as a background job like Unix ?.
systeminfouname -a
tasklistps"tasklist" is not available on some versions of Windows. See also this article on getting a list of processes in Windows using Perl
title?In Unix, changing the title of the terminal window is possible but complicated. Search for "change title xterm".
tracerttraceroute
treefind
ls -R
On Windows, use tree | find "string"
typecat
veruname -a
xcopycp -RRecursively copy a directory tree