20 Oct 2019

Dev Glossary

  • idempotent [idem’potent]: gives the same result when applied multiple times
    • 1 * 1 * 1 = 1
    • add operation on a set (sets store unique values)
set.add('1');
set.add('1');

API’s GET, PUT and DELETE should be idempotent

  • ephemeral: temporary

    • RAM
    • a mutable object
    • ephemeral servers like Lambda on AWS
    • IP addresses can be ephemeral
  • predicate: a function that returns a boolean

function isPhoneNumber(str) {
    ...
    return true;
    ...
    return false;
}
  • memoization: caching the return value of a function

  • serialization: converting an object to a more generic format (eg. JSON) so that it can be used somewhere else, eg. JavaScript object → JSON → Python object

25 Jun 2019

Observables vs Promises

Observable

  • Imagine an observable as a continuous stream of data.
  • You can subscribe to this stream and receive each item as it comes, and you can also perform various operations on these items.
  • The stream keeps flowing until you unsubscribe, and it can emit multiple items over time.
  • Observables are a technique used in reactive programming, a paradigm involving async data streams.
class Observable {
    constructor(functionThatTakesObserver) {
        this._functionThatTakesObserver = functionThatTakesObserver;
    }

    subscribe(observer) {
        return this._functionThatTakesObserver(observer)
    }
}

let myObservable = new Observable(observer => {
    setTimeout(() => {
        observer.next("Got data")
        observer.complete()
    }, 1000)
})

let myObserver = {
    next(data) { 
        console.log(data);
    },
    error(e) {
        console.log(e);
    },
    complete() {
        console.log("Request complete")
    }
}

myObservable.subscribe(myObserver)

// Output:
// (1 second) Got data
// (1 second) Request complete

Observables vs Promises

Everything you can do with a Promise you can do an Observable. Everything you can do with an observable you can’t necessarily do with a Promise. An observable can call next() multiple times, whereas a promise either resolves or rejects, it can’t emit multiple values.

Promise

  • Think of a promise as a one-time event that will happen in the future, like waiting for a package delivery.
  • You create a promise for a specific task, and it will either be fulfilled (success) or rejected (failure) when the task is completed.
  • Once the promise is fulfilled or rejected, it's done and can't change its outcome.
  • You can attach callbacks to handle the fulfillment or rejection of the promise.

12 Jun 2019

JavaScript Arrow Function

Arrow functions allow shorter syntax, for example:

const foo = () {
    return "bar";
}

vs

const foo = () => {
    return "bar";
}

or even

const foo = () => "bar";

If a function has only one parameter you can skip the parentheses:

const timesTwo = val => 2 * val;

Handling this

→ In regular functions this keyword represents the object that called the function.

→ In arrow functions this keyword represents the object that defined the arrow function.

Regular function:

javascriptCopy code
const obj = {
  name: 'John',
  regularMethod: function() {
    console.log(this.name);
  }
};
obj.regularMethod(); // Output: John

Arrow function:

javascriptCopy code
const obj = {
  name: 'John',
  arrowMethod: () => {
    console.log(this.name);
  }
};
obj.arrowMethod(); // Output: undefined (the arrow function inherits the `

17 May 2019

Logical OR Assignment

The syntax is as follows:

variable ||= value

This is equivalent to writing:

variable = variable || value

Works in JavaScript and Ruby.

26 Feb 2019

Python filter() Function

  • Selects elements from a list, tuple or another iterable based on the output of a function.
  • The function in filter() is applied to each element and if it returns True, the element is selected.
  • Returns an iterator.

Syntax

filter(function, iterable)

Examples

Basic Usage

def check_even(number):
    if number % 2 == 0:
        return True
    return False

numbers = [1, 2, 3, 4]

even_numbers_iterator = filter(check_even, numbers)

even_numbers = list(even_numbers_iterator)

print(even_numbers)

# Output: [2, 4]

Using Lambda

numbers = [1, 2, 3, 4]

even_numbers_iterator = filter(lambda: x: (x%2 == 0), numbers)

Using None

When None is used, all elements that are truthy values (gives True if converted to boolean) are extracted.

some_list = [1, 'a', 0, False, True. '0']

filtered_iterator = filter(None, numbers)

filtered_list = list(filtered_iterator)

print(filtered_list)

# Output: [1, 'a', True, '0']

12 Feb 2019

SSH

Secure Shell or Secure Socket shell is an application layer protocol.

→ Supports both password and key-based authentication

→ Encrypts data communication between computers

→ By default the server listens on HTTP port 22

SSH Commands

  • ssh-keygen: new auth key pair for SSH
  • ssh-copy-id: copies, installs and configures an SSH key on a server
  • scp: an SSH-secured version of the RCP protocol, lets the user copy files from one machine to another
  • sftp: an SSH-secured version of the FTP protocol, used to share files on the internet

Creating a Key Pair

ssh-keygen -t rsa

The -t flag means the type of key we want to generate.

09 Jan 2019

Add SSH key to GitLab

  1. In terminal ssh-keygen -t ed25519
  2. Go to directory where it was created and copy the contents of the .pub file into gitlab ssh keys

27 Dec 2018

Zero-Day Attacks

"Zero-day" - the developer has only just learned about the flaw and has zero days to fix it.

Zero-day attack: when hackers exploit the flaw before the developers have a chance to address it.

Zero-day vulnerability: a software flaw that discovered by attackers before the vendor/developer has become aware of it.

Zero-day exploit: the method/technique hackers use to attack a system.

28 Nov 2018

Checksum

A sequence of numbers and letters that serves to ensure a downloaded file doesn’t have errors.

If you know the checksum of an original file, you can use a checksum utility to confirm your copy is identical.

To produce a checksum you run a program that puts the file through an algorithm (MD5, SHA-1, SHA-256, SHA-512 etc.), and a cryptographic hash function will produce a string of a fixed length.

This works cause small changes in the file will produce very different looking checksums.

Because of collisions, you shouldn’t rely on MD5 or SHA-1 to check that a file is authentic, just to check corruption.

There haven’t been any repots of an SHA-256 collision yet.

20 Oct 2018

POSIX

POSIX - Portable Operating System Interface

→ A family of standards specified by IEEE for maintaining compatibility among operating systems

→ Any software that conforms to POSIX standards should be compatible with other operating systems that adhere to POSIX

There are currently more than 20 standards under the POSIX umbrella.

POSIX standards define:

  1. System Calls: tasks like file I/O, process management, memory management, and inter-process communication.
  2. Utilities: command-line utilities and tools, like ls, cp, and grep, along with their command-line options and behavior.
  3. File System Hierarchy: POSIX defines a standard file system hierarchy, including directories like /bin, /usr, and /lib.
  4. Environment Variables: POSIX specifies standard environment variables, which help programs locate essential system resources and configuration data.
  5. Shell Command Language: POSIX defines a standardized shell command language and scripting syntax, which is the basis for Unix shells like bash.

03 Oct 2018

Database Schema

Database schema defines how data is organized within a relational database (logical constraints such as, table names, fields, data types, and the relationships between these)

A database schema = “blueprint” of a database which describes how the data may relate to other tables or other data models. Schema does not actually contain data.

This process of database schema design is also known as data modelling.

29 Jun 2018

How to Write a Git Commit Message

Some words to start with

  • fix
  • rework
  • add
  • update
  • polish

Convention

Teams should first agree on a commit message convention:

  • Style: markup syntax, wrap margins, grammar, capitalization, punctuation
  • Content: What should it contain or not contain?
  • Metadata: Issue IDs, PR numbres etc.

The seven rules of a great Git commit message

  1. Separate subject from body with a blank line
  2. Limit the subject line to 50 characters

    → If you’re having a hard time summarizing you might be committing too many changes at once.

  3. Capitalize the subject line

  4. Do not end the subject line with a period

  5. Use the imperative mood in the subject line

  6. Wrap the body at 72 characters

  7. Use the body to explain what and why vs. how

    → Focus on making clear the reasons why you made the change - the way it things worked before the change (and what was wrong with that), the way they work now and why you decided to solve it the way you did.

The what and where

For example

Fix typo (what) in introduction to user guide (where).

Useful tools

git shortlog groups commits by user

01 Jun 2018

Atomic Commits

Do

  • Commit atomic changes ← ones that revolve around one task or fix.
  • Only commit when task is complete.

Don’t

  • Commit “whenever you feel like it”.
  • Commit “at the end of the work day”.

Benefits

  • Easy to roll back without affecting other changes.
  • Easy to make features into other branches.

10 May 2018

Python Generators

Generator functions are a special kind of function that return a lazy generator. Unlike lists, lazy generators do not store their contents in memory. We use them to work with data streams or large files (eg. CSV files).

Example 1: Large Files

In the following code open(file_name, "r") returns a generator, but file.read() loads the whole file into memory.

open(file_name, "r"):
result = file.read().split("\n")

Instead, we can yield each row instead of returning it:

for row in open(file_name, "r"):
        yield row

Example 2: Infinite Sequences

def infinite_sequence():
        num = 0
        while True:
                yield num
                num += 1

We can use it in a for-loop:

for i in infinite_sequence():
        print(i, end=" ")

# Output: 0 1 2 3 4 5 6 7 8 ...

Or we can also call next():

next(gen)

# Output: 0

next(gen)

# Ouptut: 1

Generator Expression (Generator Comprehension)

Similar to list comprehension but with generators.

csv_gen = (row for row in open(file_name))