Skip to content

Instantly share code, notes, and snippets.

@amirbehzad
amirbehzad / is_empty_array.php
Last active December 30, 2015 03:58
PHP: How to ensure a variable is an Array, and it is not empty
<?php
// returns True if the given $var is an Array, and it is not empty;
// otherwise, False. It does not gives an error if the given $var
// is not an Array, however, it returns False.
function isNonEmptyArray($var) {
return !empty(@reset($var));
}
@amirbehzad
amirbehzad / redis_mlpop.py
Last active September 28, 2020 16:34
How to LPOP multiple items from a Redis LIST in Python
# function to return multiple items (n) from a redis list (q) in an atomic manner
# as seen in: http://redis.io/commands/lpop#comment-431677622
def multi_pop(r, q, n):
p = r.pipeline()
p.multi()
p.lrange(q, 0, n - 1)
p.ltrim(q, n, -1)
return p.execute()
@amirbehzad
amirbehzad / phpunit_pdo_mock.php
Created January 21, 2016 04:18
How to mock PDO for test-cases written in PHPUnit
<?php
// ...
protected function getMockedPDO()
{
$query = $this->getMock('\PDOStatement');
$query->method('execute')->willReturn(true);
$db = $this->getMockBuilder('\PDO')
->disableOriginalConstructor()
@amirbehzad
amirbehzad / latency.markdown
Created February 2, 2016 10:30 — forked from hellerbarde/latency.markdown
Latency numbers every programmer should know

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

@amirbehzad
amirbehzad / C.sublime-build
Last active April 12, 2016 06:38
Build-system for Sublime-Text 2 to compile and run simple C programs
{
// Tools -> Build Systems -> New Build Systems
"cmd" : ["gcc -std=c99 $file_name -o ${file_base_name} && ./${file_base_name} -lm -Wall"],
"selector" : "source.c",
"shell": true,
"working_dir" : "$file_path"
}
@amirbehzad
amirbehzad / future-jobs.md
Created April 18, 2016 07:22
Future-proof Jobs

Future-proof Jobs

  • Engineer
  • Clergy
  • HR Manager
  • Media Producer
  • Game Warden
  • Labor Leader
  • Artist
  • Futurist
@amirbehzad
amirbehzad / Jekyll-OSX.md
Created April 18, 2016 15:42
How to Install Jekyll on OSX
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

echo "gem: --no-document" >> ~/.gemrc
curl -L https://get.rvm.io | bash -s stable --auto-dotfiles --autolibs=enable

rvm install ruby --latest
rvm requirements

brew update
@amirbehzad
amirbehzad / csfacts.md
Created May 29, 2016 17:08
Computer Science Fun Facts

Merge Sort

First, split the array in half. Then sort both sides.

Infinite Set

A set is said to be infinite if it is not finite

rules = { 2**x: ['{0:03b}'.format(x).translate(string.maketrans("10","x.")), (x > 0) and (x & 3 == x or x & 3 == 3) * 1] for x in range(8) }
apt-get install -y libpq-dev python-dev pkg-config libcgraph5 libfreetype6-dev libpng-dev g++ curl libxft-dev graphviz libgraphviz-dev
python2 -m pip install numpy scipy pandas matplotlib pytidylib simplejson requests openpyxl psycopg2 BeautifulSoup networkx ipywidgets pygraphviz
python2 -m pip install seaborn pydot dill nltk rarfile facebook-sdk requests_oauthlib pytidylib requests_futures xlrd sklearn us