A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.
Jump to
Showing posts with label basics. Show all posts
Showing posts with label basics. Show all posts
Node CRUD basics
Javascript philosophy and execution flow is quite different to Perl:
- With Perl it's strongly discouraged to wrap an operation in a `try { ... }` block without a `catch { ... }` block following it, because any errors would be hidden and the operation might silently fail.
- With Javascript and Promises, many operations are automatically wrapped in a `try { ... }` block, and are not even executed if they don't have a `.then()` or a `.catch()` added, so they silently fail!
Knex basics
Labels:
basics,
crud,
javascript,
knex,
node
MySQL explain plan cheat sheet
Explanation of explain output:
- select_type: SIMPLE/SUBQUERY/UNION/DERIVED
- partitions: NULL ??
- type: (from worst to the best): ALL, index, range, ref, eq_ref, const, system
- possible_keys: food,bar,baz,id (good)
- key: foo(good)
- key_len: 334 ??
- ref: const,const,const ??
- rows: 1 (lower is better. must be less than total rows)
- filtered: 100.00 (lower is better, but only used if there's a join)
- Extra: NULL
- using where
- using temporary
- using filesort
- using index
MySQL basics - creating a user, etc.
Creating a user:
CREATE USER 'user'@'hostname';
ALTER USER 'user'@'hostname' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON dbName.* To 'user'@'localhost' IDENTIFIED BY 'password';
(source)
Docker basics
# Download an image and spin up a container, run it, connect to it
docker run -d -p 80:80 --name pintail-whoami pintailai/pintail-whoami:0.0.1 # download + run
docker run --rm -v /path/on/machine:/app/out image-name:stable params to app
docker ps # show running containers
docker ps | cut -c-$(tput cols) # show running containers without wrapping to the next line
docker ps -q # show just the IDs of the running containers
docker ps -q | head -1 # show ID of most recently started container (how to sort)
# Run a shell on a container
docker run -i -t [Container ID] /bin/bash
# Connect to a shell on an already-runnning docker container.
# (Shell: Use /bin/bash for ubuntu or /bin/ash for alpine)
docker exec -it [Container ID] [shell]
docker exec -it `docker ps -q | head -1` /bin/bash # run shell on the most recently started container
# Copy a file off
docker ps | cut -c-$(tput cols) # show running containers without wrapping to the next line
docker ps -q # show just the IDs of the running containers
docker ps -q | head -1 # show ID of most recently started container (how to sort)
# Run a shell on a container
docker run -i -t [Container ID] /bin/bash
# Connect to a shell on an already-runnning docker container.
# (Shell: Use /bin/bash for ubuntu or /bin/ash for alpine)
docker exec -it [Container ID] [shell]
docker exec -it `docker ps -q | head -1` /bin/bash # run shell on the most recently started container
# Copy a file off
docker cp <container>:<src-path> <local-dest-path>
# Delete all stopped containers and images
docker system prune -a
# List all images
docker image ls
nginx basics
How to serve up a directory on the web
server {
listen 80 default_server;
root /var/www/html/foo;
index index.html
location / {
autoindex on;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}
}
- sudo apt-get install nginx
- vi /etc/nginx/nginx.conf # observe how the html { } section has: include /etc/nginx/sites-enabled/*;
- vi /etc/nginx/sites-available/foo # build the server { } section
server {
listen 80 default_server;
root /var/www/html/foo;
index index.html
location / {
autoindex on;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}
}
- # build .htpasswd file
- ln -s /etc/nginx/sites-available/foo /etc/nginx/sites-enabled/foo
- service nginx restart
Python basics
Most basic: Use the latest Python 3 (or higher)
Use multiple lines in a one-liner with \n:
echo -e "import sys\nsys.exit('Error')" | python3
Use multiple lines in a one-liner with \n:
echo -e "import sys\nsys.exit('Error')" | python3
CSS/Layout basics
CSS/styling ideas for developers with little front-end design experience:
- use Bootstrap
- get someone with experience to do the graphical design, then implement it using CSS
- turn a list into a grid of links with JQuery
- Or something more low-tech
- If you want to build something from scratch, the basic principles as I have come to understand them are:
- (1) decide on an element type, e.g.
- <span> for in-line elements (in a horizontal line),
- <div> for a vertical line of elements,
- <ul> for a list, etc.
- (2) fiddle with the CSS attributes like: float, clear, overflow, border, padding to make it look like you want.
- How to use "float"
- (3) use javascript if anything needs to appear/disappear based on user interaction.
- But I usually start with a framework layout like Bootstrap, add pre-made Javascript libraries for widgets and only do the above steps for any little tweaks needed.
- The current state of the art is the "CSS Grid" layout (for 2D grids).
- One opinion is that CSS Grid beats Bootstrap for layout
- Flexbox is the more established and better supported method of layout (for 1D lists)
- Enlightenment and inspiration: CSS Zen Garden
- examples of the same content transformed with CSS to appear totally different
- Mozilla Developer Network (MDN) has a good CSS reference
- CSS tricks has useful guides
- Quirks Mode tells you which browsers support which features.
Mojolicious basics
How to do common stuff.
Run an app:
MOJO_USERAGENT_DEBUG=1 perl -I lib ~/path/to/morbo --verbose --watch lib --watch local bin/app.pl
View existing routes:
perl -I lib bin/app.pl routes
Find the code that defines the routes:
grep -r '$r->get' .
Install database:
perl -I lib bin/app.pl dbic_migration --action=install
Write a script that uses the app config, etc:
See Mojolicious::Command
Debug Test::Mojo:
print $t->tx->res->body; # See also guide to debugging
Catch unexpected exceptions:
Dump out 2nd-level routes:
my @routes = sort map { $_->to_string } map { @{ $_->children } }
grep { $_->name eq 'distro' || $_->name eq 'candidates' }
@{ $t->app->routes->children };
Run an app:
MOJO_USERAGENT_DEBUG=1 perl -I lib ~/path/to/morbo --verbose --watch lib --watch local bin/app.pl
View existing routes:
perl -I lib bin/app.pl routes
Find the code that defines the routes:
grep -r '$r->get' .
grep -r '$r->post' .
perl -I lib bin/app.pl dbic_migration --action=install
Write a script that uses the app config, etc:
See Mojolicious::Command
Debug Test::Mojo:
print $t->tx->res->body; # See also guide to debugging
Catch unexpected exceptions:
Dump out 2nd-level routes:
my @routes = sort map { $_->to_string } map { @{ $_->children } }
grep { $_->name eq 'distro' || $_->name eq 'candidates' }
@{ $t->app->routes->children };
Oracle commands for MySQL/PostgreSQL users
Oracle tips for MySQL users:
- Simple range: SELECT * FROM (SELECT * FROM foo) WHERE ROWNUM <= 100; -- equivalent of MySQL's LIMIT clause, only for start of table
- Wrong range: SELECT * FROM (SELECT rownum r, f.bar FROM schema.foo f) WHERE r > 100 AND r <= 200; -- Selects a range, but order will be inconsistent, even if you add ORDER BY to inner select
- Right range:
SELECT * FROM ( SELECT q.*, ROWNUM r FROM ( SELECT * FROM schema.foo ORDER BY id ) q ) WHERE r >= 100 AND r < 200(source) - SELECT last_name FROM employees WHERE last_name LIKE '%d_g\_cat%' ESCAPE '\';
- matches dog_cat, foodig_catbar, etc.
- _ = any single character
- % = any characters
- \_ = literal _ (underscore)
- ESCAPE '\'; -- set the escape character to \ (backslash)
- SELECT ...... WHERE REGEXP_LIKE (instance_name, '^Ste(v|ph)en$');
- ALTER USER foo IDENTIFIED BY "newpassword456!" REPLACE "oldpassword123!"; -- change password (REPLACE is new in 9.2)
svn basics, for git users
How to do stuff in subversion, when you're used to git:
# create a new branch
svn copy -m "Branching trunk" https://www.example.com/svn/repos/project/trunk https://www.example.com/svn/repos/project/branches/new_branch_name
# check out new branch
svn checkout https://www.example.com/svn/repos/project/branches/new_branch_name
# check status/diffs of working directory
svn status
svn diff
# show a specific commit
svn diff -c63197
svn diff -c63197 | vim -R -
svn diff -r63196:63197
svn diff -c63197 db/src/securesite/
# commit files
svn commit -m'Commit message' filename.ext
# see your changes after committing
svn update
# find where a branch was cut from trunk (svn equivalent of git mergebase)
# The last commit will be the first one in this branch after it was cut from trunk:
svn log -v --stop-on-copy
# cherry pick
svn merge -cXXXX trunk branch
# revert a commit
svn merge -c -REV .
# create a new branch
svn copy -m "Branching trunk" https://www.example.com/svn/repos/project/trunk https://www.example.com/svn/repos/project/branches/new_branch_name
# check out new branch
svn checkout https://www.example.com/svn/repos/project/branches/new_branch_name
# check status/diffs of working directory
svn status
svn diff
# show a specific commit
svn diff -c63197
svn diff -c63197 | vim -R -
svn diff -r63196:63197
svn diff -c63197 db/src/securesite/
# commit files
svn commit -m'Commit message' filename.ext
# see your changes after committing
svn update
# find where a branch was cut from trunk (svn equivalent of git mergebase)
# The last commit will be the first one in this branch after it was cut from trunk:
svn log -v --stop-on-copy
# cherry pick
svn merge -cXXXX trunk branch
# revert a commit
svn merge -c -REV .
Catalyst controller basics
Create a controller module:
myapp_create.pl Controller Foo
created "......../lib/Controller/Foo.pm"
created "......../t/controller_Foo.t"
Inside Foo.pm:
sub index :Path :Args(0) {
my ( $self, $c ) = @_;
$c->response->body('Matched XXXX::Controller::Foo in Foo.');
}
sub root :Chained('/') :PathPrefix :CaptureArgs(0) {
# common stuff for this controller
}
sub bar :Chained('root') :PathPart('bar') :Args(0) {
my ( $self, $c ) = @_;
# something
}
myapp_create.pl Controller Foo
created "......../lib/Controller/Foo.pm"
created "......../t/controller_Foo.t"
sub index :Path :Args(0) {
my ( $self, $c ) = @_;
$c->response->body('Matched XXXX::Controller::Foo in Foo.');
}
sub root :Chained('/') :PathPrefix :CaptureArgs(0) {
# common stuff for this controller
}
sub bar :Chained('root') :PathPart('bar') :Args(0) {
my ( $self, $c ) = @_;
# something
}
Labels:
basics,
catalyst,
controller,
framework,
perl
Developing on Android basics
Initial steps to take:
- Turn on USB Debug mode
- Old devices: Settings | Applications | Development | USB debugging (will disable SD card access)
- New devices: Settings | About phone | Build number | Tap 7 times (yes, really). Dev settings will now appear on the menu.
- Troubleshooting connection:
- Verify USB cable works
- Verify USB port works
- Re-plug in the cable about 20 times (yes, really)
- Windows: Run "Android SDK manager" (sdk\tools\android.bat) and install 'Google USB driver' under Extras
- adb devices # error "???????????? no permissions usb:3-3
- adb kill-server
- sudo adb start-server # server must be started as root
- adb devices
- ADB Shell commands:
- adb shell pm list packages
- more commands
vim tabs and buffers basics
in vim type ":grep -Ri search_string *"
...it executes... press enter to get back to vim
then type ":copen" to open a window with the search results in
and you can browse the matches really easily
:help grep
then if you press enter to open one of the files found,
to get back to your original file type :buffers or :ls
:tabe #[number against file] will open the original file in a new tab
:sp #[number against file] will open the original file in a new window
:buffer [number against original file] will open the original file again
and you could make the F keys load the buffers
I prefer tabs over buffers because they're more visible
in bash vim -p file1 file2 will open 2 files in two different tabs
gt and gT will cycle through the files (I have that mapped to alt+left and alt+right)
:tabe
Thanks Phill
Ruby basics
- Install Ruby
- Setup gems
- gem install nokogiri
- gem install builder
- Database: SQLite (docs)
- Database driver:
- ! gem install dbi (docs don't work, but these docs do)
- ! gem install dbd-sqlite3
? gem install sqlite-ruby (docs)
- Write script offline
- Host on Heroku
Save URL to a file
require 'open-uri'
open('image.png', 'wb') do |file|
file << open('http://example.com/image.png').read
end
is this the same as:
require 'open-uri'
file = open('image.png', 'wb')
file << open('http://example.com/image.png').read
Fetch URL through a proxy
print Net::HTTP::Proxy(proxy_addr, proxy_port, proxy_user, proxy_pass).get URI.parse('http://www.compufer.com/')
Hello world
puts "Hello, what's your name?"
STDOUT.flush
name = gets.chomp
puts 'Hello, ' + name + '.'
Blocks
def try
if block_given?
yield
else
puts "no block"
end
end
try # => "no block"
try { puts "hello" } # => "hello"
try do puts "hello" end # => "hello"
- Whichever variables(s) you pass into the block, the method will fill them with something, depending on what its function is. e.g. "open" puts a file handle in there.
- List of file modes
- Hash, Hashes
- ---------------
- Builder tut1, tut2, tut3
- Read docs
- open-uri docs
- open returns an IO like object
- File
- Ruby blocks (closures)
Labels:
basics,
gem,
programming,
ruby
DBIx::Class basics
# SELECT COUNT(*) FROM product WHERE id = 104316my $rs = $schema->resultset( 'Public::Product' )->search( { id => 104316 });print "test: ".$rs->all;
Git basics
Have git remember merge conflict resolutions so you never have to do them more than once:
git config --global rerere.enabled 1
Prevent "git push" with no parameters from pushing anything other than your current branch:
git config --global push.default current
Show a graphical summary of the merge tree (looks a bit like gitk).
This gives the answer to "Have I pushed or not"? :
Find the point at which you forked (branched):
Show all changes made in this branch (if you branched from master):
Ensure only one commit per file; pipe the above through:
Edit all commit messages since a particular commit:
Undo the last commit made locally (e.g. in order to re-do it differently, perhaps in the middle of a rebase):
To reverse a pushed commit two commits back,
first reverse the second most recent commit, and then commit it back:
git revert -n HEAD~1
(Do not try to rebase what has been pushed, it will not work. Rebase only changes your local repo)
To revert a merge commit:
git revert SHA -m 1
(Where SHA is the commit ID)
Reset a branch to be same as the repo:
git reset --hard origin/branch_name
Delete a branch:
git branch -D branch_name # delete it locally
git push origin :branch_name # delete it on remote
Use a branch in a different repo for the same project:
git remote add repo_shortname user@server:/repo_name
Use different branch names in each repo.
Push to your repo_shortname instead of origin.
Create a new, tracked branch:
Tracked means that 'git status' will show you how many commits behind the parent branch you are, and if the parent branch has diverged (which means you might want to rebase onto it)
git checkout master # or wherever you want to branch from
Easily make an alteration to a previous commit:
git commit -m'fixup! same title as another commit' filename
git rebase -i --autosquash [commit id]
(the commit will be put in the correct place and marked as 'fixup' automatically)
A git workflow:
(dca-73 is the name of a branch)
How to merge conflicts without editing the file:
git checkout --ours path/to/filename
Automatically run checks on your code before committing with git hooks:
e.g. Just edit the file .git/hooks/pre-commit in your working directory, and make it executable.
It will not be committed along with your code because it's in the special .git directory.
git config --global rerere.enabled 1
Prevent "git push" with no parameters from pushing anything other than your current branch:
git config --global push.default current
Show a graphical summary of the merge tree (looks a bit like gitk).
This gives the answer to "Have I pushed or not"? :
git log --decorate --color --graph --oneline
Find the point at which you forked (branched):
git merge-base [branch] [trunk or previous branch]
Show all changes made in this branch (if you branched from master):
git log --oneline --name-only --reverse master..HEAD
Ensure only one commit per file; pipe the above through:
| grep -v '^....... ' |sort |uniq -c |sort -nr
Edit all commit messages since a particular commit:
git rebase -i $(git merge-base [branch or HEAD] [trunk or previous branch])
Undo the last commit made locally (e.g. in order to re-do it differently, perhaps in the middle of a rebase):
git reset HEAD^
To reverse a pushed commit two commits back,
first reverse the second most recent commit, and then commit it back:
git revert -n HEAD~1
git push origin branch(Do not try to rebase what has been pushed, it will not work. Rebase only changes your local repo)
To revert a merge commit:
git revert SHA -m 1
(Where SHA is the commit ID)
Reset a branch to be same as the repo:
git reset --hard origin/branch_name
Delete a branch:
git branch -D branch_name # delete it locally
git push origin :branch_name # delete it on remote
Use a branch in a different repo for the same project:
git remote add repo_shortname user@server:/repo_name
Use different branch names in each repo.
Push to your repo_shortname instead of origin.
Create a new, tracked branch:
Tracked means that 'git status' will show you how many commits behind the parent branch you are, and if the parent branch has diverged (which means you might want to rebase onto it)
git checkout master # or wherever you want to branch from
git checkout -t -b my_new_branch
Easily make an alteration to a previous commit:
git commit -m'fixup! same title as another commit' filename
git rebase -i --autosquash [commit id]
(the commit will be put in the correct place and marked as 'fixup' automatically)
(dca-73 is the name of a branch)
git fetch
git checkout dca-73
git pull origin dca-73
# run tests
# make changes
# run tests
git fetch
git push origin dca-73
git checkout master
git pull origin master
git merge --no-ff dca-73
# run tests
# check it looks right
git log --decorate --graph --oneline
git push origin master
How to merge conflicts without editing the file:
git checkout --ours path/to/filename
git checkout --theirs path/to/filename
e.g. Just edit the file .git/hooks/pre-commit in your working directory, and make it executable.
It will not be committed along with your code because it's in the special .git directory.
PostgreSQL basics (for MySQL users)
Installing
- sudo apt update
- sudo apt install postgresql postgresql-contrib
- sudo service postgresql start
- sudo passwd postgres # then close and re-open the terminal(?)
- sudo -u postgres psql
- create user foo;
- alter user foo with superuser;
- alter user foo with password 'new_password';
- psql -Ufoo -d postgres
Reconfigure authentication if necessary to either require or disable passwords.
(source)
Using
connect: psql -U [username, e.g. postgres] -d [database]
- \c dbname = connect to dbname
- \l = list databases:
- \dt = describe tables, views and sequences
- \dt+ = describe tables with comments and sizes
- \dT = describe Types
- \di = describe indexes
- \q = quit
- \x = toggle equivalent of adding MySQL's \G at the end of queries to display columns as rows
- \connect database = change to a different database
- CREATE DATABASE yourdbname;
- CREATE USER youruser WITH ENCRYPTED PASSWORD 'yourpass';
- GRANT ALL PRIVILEGES ON DATABASE yourdbname TO youruser;
- Use auto_increment in a column definition:
- create sequence foo__id__seq increment by 1 no maxvalue no minvalue start with 1 cache 1;
- create table foo ( id integer primary key default nextval('foo__id__seq') );
- Reset an auto_increment counter (sequence):
- SELECT setval('sequence_name', 1, false); -- this works
- ALTER SEQUENCE sequence_name RESTART WITH 1; -- this also works
- See what's in an ENUM
- SELECT enumlabel FROM pg_enum WHERE enumtypid = 'myenum'::regtype ORDER BY id;
- Closest equivalent of MySQL's "show create table"
- pg_dump -U postgres --schema-only [database] >> dump.sql
- See current activity
- SELECT * FROM pg_stat_activity (equivalent to MySQL's "show processlist")
- Drop all current connections
- SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname='foo'; # foo = database name
- pg_dump -Upostgres -hHOSTNAME DBNAME -fOUTPUTFILE.sql --no-password
- psql -Upostgres -hHOSTNAME -dDBNAME -fINPUTFILE.sql
- psql -Upostgres -hHOSTNAME -dDBNAME -c "Some SQL command"
Labels:
basics,
databases,
postgresql,
software,
sql
Git setup
git config --global user.name "Your Name Comes Here"
git config --global user.email you@yourdomain.example.com
git config --global color.diff auto
git config --global color.status auto
git config --global color.branch auto
git config --global core.editor vim
git config --global user.email you@yourdomain.example.com
git config --global color.diff auto
git config --global color.status auto
git config --global color.branch auto
git config --global core.editor vim
Labels:
basics,
commands,
config,
git,
versioning
Vim navigation
1. Vim Line Navigation
Following are the four navigation that can be done line by line.
- k – navigate upwards
- j – navigate downwards
- l – navigate right side
- h – navigate left side
By using the repeat factor in VIM we can do this operation for N times. For example, when you want to
go down by 10 lines, then type “10j”.
Within a line if you want to navigate to different position, you have 4 other options.
- 0 – go to the starting of the current line.
- ^ – go to the first non blank character of the line.
- $ – go to the end of the current line.
- g_ – go to the last non blank character of the line.
2. Vim Screen Navigation
Following are the three navigation which can be done in relation to text shown in the screen.
- H – Go to the first line of current screen.
- M – Go to the middle line of current screen.
- L – Go to the last line of current screen.
- ctrl+f – Jump forward one full screen.
- ctrl+b – Jump backwards one full screen
- ctrl+d – Jump forward (down) a half screen
- ctrl+u – Jump back (up) one half screen
3. Vim Special Navigation
You may want to do some special navigation inside a file, which are:
- N% – Go to the Nth percentage line of the file.
- NG – Go to the Nth line of the file.
- G – Go to the end of the file.
- `” – Go to the position where you were in NORMAL MODE while last closing the file.
- `^ – Go to the position where you were in INSERT MODE while last closing the file.
- gg – Go to the beginning of the file.
4. Vim Word Navigation
You may want to do several navigation in relation to the words, such as:
- e – go to the end of the current word.
- E – go to the end of the current WORD.
- b – go to the previous (before) word.
- B – go to the previous (before) WORD.
- w – go to the next word.
- W – go to the next WORD.
5. Vim Paragraph Navigation
- { – Go to the beginning of the current paragraph. By pressing { again and again move to the previous paragraph beginnings.
- } – Go to the end of the current paragraph. By pressing } again and again move to the next paragraph end, and again.
6. Vim Search Navigation
- /i – Search for a pattern which will you take you to the next occurrence of it.
- ?i – Search for a pattern which will you take you to the previous occurrence of it.
- * - Go to the next occurrence of the current word under the cursor.
- # - Go to the previous occurrence of the current word under the cursor.
7. Vim Code Navigation
% – Go to the matching braces, or parenthesis inside code.
8. Vim Navigation from Command Line
Vim +N filename: Go to the Nth line of the file after opening it.
vim +10 /etc/passwd
An easy way to browse the file system is the command:
:Sex
Really!
Labels:
basics,
shortcut keys,
text editor,
vi
Subscribe to:
Posts (Atom)