Skip to content

Instantly share code, notes, and snippets.

import metric
(500 * metric.byte) / (4 * metric.Mbps)
#0 Need floats else it rounds
(500 * metric.byte) / (4.0 * metric.Mbps)
#0.001 Notice 4.0 (float) vs 4 (int)
size = 500 * metric.byte
rate = 4 * metric.Mbps
#Bandwidth-Delay Product
#AKA "data in flight"
#https://d396qusza40orc.cloudfront.net/comnetworks/lect%2F2-1-physical-overview-ink.pdf
import metric
def bandwidthDelay(rate=0.00 * metric.Mbps, propDelay = (0.00 * metric.ms), unit=metric.byte):
return ((float(rate) * propDelay) / unit)
#Metric Units https://d396qusza40orc.cloudfront.net/comnetworks/lect%2F2-1-physical-overview-ink.pdf
Kbps = pow(10,3)
Mbps= pow(10,6)
Gbps = pow(10,9)
ms = pow(10,-3)
us = pow(10,-6)
ns = pow(10,-9)
from util import groupBy
_4b5bmap = {'11110':'0000',
'01001':'0001',
'11100':'1110',
'11101':'1111'}
#"are pre-determined in a dictionary and they are chosen to ensure that there will be at least two transitions per block of bits."
#TODO: Get the whole map from http://www.mathworks.com/matlabcentral/fileexchange/27566-4b5b-encoder-line-codificador-de-linea-4b5b/ ?
def translate4b5b(bits):
from util import groupBy
bits='010010001111'
def translateBits(bits):
translated = [int(_,2) for _ in groupBy(bits, 3)]
return translated
def groupBy(iterable, groupsize=3, strict=True):
if strict:
assert len(iterable)%groupsize == 0, 'Need evenly divisible'
return [iterable[_:_+groupsize] for _ in range(len(iterable))[::groupsize]]
import math
#maximum *symbol* rate is 2B
#V signal levels; ignoring noise, max bit rate is:
def nyquistLimit(bandwidth=0.00, numSignalLevels=0.00):
B=bandwidth
V=numSignalLevels
numSamplesPerSecond = 0.5#frequency
numSecondsPerSample = 1/numSamplesPerSecond #1/frequency
import math
def shannonCapacity(bandwidth, signal=0.0, noise=0.0, snr=0.0):
B = bandwidth
S = signal
N = noise
return B* math.log((1+(snr or S/N)),2)
print(shannonCapacity(20, snr=1000))
class BalanceTotal(SuperModel):
#...
def __getstate__(self):
return self.js()
def __setstate__(self, state):
void Main()
{
///Understanding Captures vs Groups http://stackoverflow.com/a/3320890/1175496
//In short, Groups represent things inside Grouping Parentheses ()
//Captures represent the items in the collection implied by repetition operators * and +
var matchesONE = Regex.Matches("{Q}{R}{S}", @"(\{[A-Z]\})");
var matchesOOM = Regex.Matches("{Q}{R}{S}", @"(\{[A-Z]\})+");
var matchesOPT = Regex.Matches("{Q}{R}{S}", @"(\{[A-Z]\})*");
Console.Out.WriteLine(matchesONE);