Rectangle 27 2

Python Tuples

tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";

A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-separated values between parentheses also. For example

Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so on.

Note Python tuples
Rectangle 27 2

Shuffle an array using sort and random

Given the array:

var array = [1,2,3,4,5,6,7,8,9];

Sort randomly with:

array.sort(function(){     return Math.random() - 0.5 });

The - 0.5 means the returned value exists between the range -0.5 < x < 0.5, allowing the sort function to decide where to position the next item.

An example result:

array = [4,3,1,2,9,5,6,8,7]
Note JavaScript Array sorting
Rectangle 27 1

Restricting an annotation's application by using @Target

The type of element that an annotation can be applied to can be restricted using the @Target meta-annotation.

The @Target declaration takes the form of @Target(ElementType.<ELEMENT_TYPE>), where <ELEMENT_TYPE> is an element type from a predefined list. The available elements are:

  3. FIELD
  7. TYPE (any type of element)

For example:


The annotation that is annotated with this will only be able to be applied to constructors.

Note Java annotations restrictions
Rectangle 27 3

Java annotation @Test Parameters

The JUnit @Test annotation is used to indicate that a method should be called to run a test. @Test can contain two types of parameter:


When the test is expected to throw an exception, we can indicate this by adding expected = <exception>.class after @Test, where <exception> is the name of the expected exception.

//fail if IOException is not thrown
@Test(expected = IOException.class)


When a test needs to complete within a specific time period, we can add a parameter to @Test to fail if the test takes too long. The value in the parameter is measured in milliseconds.

//timeout after 500ms
@Test(timeout = 500)
Note Java annotations
Rectangle 27 2

Use tcpdump to listen to network interface traffic.

tcpdump is a useful utility to print out descriptions of contents of packets flowing through network interface card which match a given boolean expression.

sudo tcpdump -i wlan -v 'tcp port 80' 

The -i specifies the interface to listen on. The -v option specifies verbosity of packet information such as time to live, identification, total length and options in ip packet. The expression specifies a pcap-filter used to decide which packets to print. Here we listen for tcp traffic with either source or destination port set to 80.

See man pcap-filter for details on filters.

Note Linux network monitoring tcpdump
Rectangle 27 2

Preventing ssh from timing out

Due to latency or packet loss, ssh connections may timeout.

To prevent this, setup application-level keep-alives for ssh.

Add to the start of the ~/.ssh/config settings:

Host * ServerAlivesInterval 15

Now, the ssh client will send application-level keep-alives every 15 seconds.

If three of them fail consecutively, the client considers the connection failed and closes it in response. This count configurable using ServerAliveConutMax.

Opposed to the other option TCPKeepAlive, this is checked within the encrypted channel and is not spoofable.

Note ssh connection keep alive
Rectangle 27 2

Bandwidth monitoring tools

Here are some tools provided by the Ubuntu repos for network monitoring:

iftop - display bandwidth usage on an interface by host.

bmon -  portable bandwidth monitor and rate estimator, shows multiple interfaces at once.

slurm - network load monitor equipped with colored graphs.

tcptrack - reveals how much bandwidth is being used by what protocol (service/port), and destination the transmission is taking place to.

Each can be used for Specific monitoring or basic statistics.

Note Linux monitoring tools
Rectangle 27 2

Few tools to get start with python. - Corrects previous console commands - A tool for creating GIF screencasts of a terminal, with key presses overlaid. - Rest Api framework for python for humans. - An open-source Python implementation using JIT techniques. - Faker Faker is a Python package that generates fake data for you. - Web mining module for Python, with tools for scraping - Keep your application settings in sync (OS X/Linux) - Scrapy, a fast high-level web crawling & scraping framework for Python. - A collection of design patterns/idioms in Python— Scalable user load testing tool written in Python

Tornado Async tools - MVC Framework - WebCrawler - A terminal using tornado and web sockets.


Note Python tools
Rectangle 27 2

Getting the connection speed from the terminal

To get your internet speed from the terminal there is a tool called speedtest-cli and we can set it up as below:

$ pip install speedtest-cli

We should get something like this:

$ ./speedtest-cli
Retrieving configuration...
Retrieving server list...
Testing from Comcast Cable (x.x.x.x)...
Selecting best server based on ping...
Hosted by xxx [12.03 km]: 44.028 ms
Testing download speed....................
Download: 32.29 Mbit/s
Testing upload speed....................
Upload: 5.18 Mbit/s

Note Linux connection speed test
Rectangle 27 3

Setting up password-free authenticaton

To set up password-free authentication start by generating a pair of keys:

$ ssh-keygen


$ ssh-copy-id -i root@ip_address

This copies the public key into the authorized_hosts of the other machine. It will ask for the password of the host system.

Now, enter the remote system directly with:

$ ssh root@ip_address

Note Linux networking authentication