Rectangle 27 1

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:

  1. ANNOTATION_TYPE
  2. CONSTRUCTOR
  3. FIELD
  4. LOCAL_VARIABLE
  5. METHOD
  6. PARAMETER
  7. TYPE (any type of element)

For example:

@Target(ElementType.CONSTRUCTOR)

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:

Exceptions

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)

Time-out

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.

https://github.com/nvbn/thefuck - Corrects previous console commands

https://github.com/KeyboardFire/mkcast - A tool for creating GIF screencasts of a terminal, with key presses overlaid.

http://python-eve.org/ - Rest Api framework for python for humans.

https://github.com/dropbox/pyston - An open-source Python implementation using JIT techniques.

http://www.joke2k.net/faker/ - Faker Faker is a Python package that generates fake data for you.

https://github.com/clips/pattern - Web mining module for Python, with tools for scraping

https://github.com/lra/mackup - Keep your application settings in sync (OS X/Linux)

https://github.com/scrapy/scrapy - Scrapy, a fast high-level web crawling & scraping framework for Python.

https://github.com/faif/python-patterns - A collection of design patterns/idioms in Python

https://github.com/locustio/locusthttp://locust.io/— Scalable user load testing tool written in Python

Tornado Async tools

http://www.gitlogs.com/most_popular?topic=tornado

https://github.com/trendrr/whirlwind - MVC Framework

https://github.com/retresco/Spyder - WebCrawler

https://github.com/bdarnell/tornado-production-skeleton

https://github.com/paradoxxxzero/butterfly - A terminal using tornado and web sockets.

https://github.com/renxing/quora-python

MongoDriver

https://github.com/mongodb/motor

https://gist.github.com/bootandy/1676440

https://github.com/bitly/asyncmongo

http://motor.readthedocs.org/en/stable/tutorial-tornado.html

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 speedtest.net configuration...
Retrieving speedtest.net 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

Then:

$ 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