Neither one nor Many

 
September 22 2017

Launch debug shell in some arbitrary piece of code

import code
code.interact(local=locals())

Or using IPython:

import IPython
IPython.embed()

regex

name params with ?P<X>,'

In [18]: output = "x:3372 y:1280 screen:0 window:29360137"
In [19]: reg = re.compile(r'x:(?P<X>\d*) y:(?P<Y>\d*)')
In [20]: reg.match(output).group('X')
Out[20]: '3372'

match non-greedy by adding ? suffix

Cheatsheets Comments (0)
 
August 14 2017

Place to keep commands I tend to google more than once for some reason.

Hibernation

Start cmd.exe as Administrator

powercfg.exe /hibernate off|on

Some file in C:\ hiberfil.sys (IIRC) will consume quite a bit of disk space which may be a reason for not using the hibernate feature.

Cheatsheets Comments (0)
 
December 19 2016

Assuming it's already flashed etc., (I forgot the steps because it's too long ago!)

What I do is connect the Edison via both micro USB's, one will be used as power, the other can be used for connecting to it.

In case the edison is booted you can open the terminal with screen:

sudo screen /dev/ttyUSB0 1152000 115200

Configure the WiFi via configure_edison --wifi

One caveat if you are also on the same network (i.e., your laptop is also on the same network) you may get a conflict via the USB wire. You can see with ifconfig two IP addresses one for the wlan0 and one for usb0, which is probably causing a conflict somehow. Solution is:

ifconfig usb0 down
Cheatsheets Comments (0)
 
August 26 2016

Some stuff I keep here as a cheetsheat for myself

mass replace recursively

NOTE: do not run in case you have .git directory, it can corrupt it.

Source: https://stackoverflow.com/a/1583282/1958831

mv .git /tmp/git_tmp
find . -type f -print0 | xargs -0 sed -i 's/subdomainA\.example\.com/subdomainB.example.com/g'
mv /tmp/git_tmp .git 

return value

$? return last exit code

buffer control with stdbuf

time make -j24 "$@"  2>&1 | stdbuf -i0 -o0 -e0 tee ./mm.log

sets buffer to zero, everything is piped immediately, bad for throughput, good for latency

< <(command)

gdb on program that works on stream, with mkfifo to make this a combined example (you can issue the bash command instead of the tail -f in this case)

mkfifo /tmp/foo
bash tail_all.sh > /tmp/foo
gdb <application>
run <program args> < <(tail -f /tmp/foo)

This syntax is also useful for stuff like:

while read -d ':' line; do
    echo "$line"
done < <(hbase classpath)

A pipe | creates a new subshell, this is also a nice way to avoid this (subshells cannot modify variables of their parent)!

EDIT: This is called process substitution, see http://tldp.org/LDP/abs/html/process-sub.html

matching

if [[ $foo =~ foo ]]; then
    ;
done

Note that you DO NOT use asterisks like *foo*!

processing stdin

A pattern I use quite a bit is the following.

function func
{
    while read line; do
        echo $line
    done < /dev/stdin
}
cat foo.txt | func

subshells / pids

Sometimes subshells are useful.

echo mypid = $!

( sleep 10; ) &
typeset pid=$!
sleep 1
kill $pid # kill if still running..

Note that you can use exit in a subshell without destroying the parent.

trap CTRL+C

function myfunc
{
    echo Do some cleanup here..
    exit 0
}
trap "myfunc" 2
print "Press CTRL+C to abort 10 seconds of waiting.."
sleep 10

namerefs

First learned about this in kornshell, but seems to work for bash as well.

function func {
    local -n test_ref=$1
    test_ref="Hello world"
}

func test # note omitted $-sign

echo $test

If you want kornshell compatibility use typeset -n instead of local -n.

The same example, but with an array, you have to first explicitly declare the array before passing it to func.

declare -A array
function func {
    local -n array_ref=$1
    array_ref=([one]=aaa [two]=bbb [three]=ccc)
}
func array

(If you want kornshell compatibility again, use typeset -A instead of declare -A (and typeset -n for local -n)).

arrays

Continuing the previous paragraph, some useful examples regarding arrays.

echo ${array[@]}  # aaa bbb ccc
echo ${!array[@]} # one two three
echo ${#array[*]} # 3

for key in ${!array[@]}; do
    local value=${array[$key]}
    echo $key = $value # one = aaa, ... 
done

Instead of initializing with the array=([foo]=bar [baz]=ban) syntax, assignment syntax is array[foo]=bar.

bash substrings

typeset hostname=$(hostname -s)
hostname=${hostname:0:10}  # first 10 chars..

NFS caching

Trick to use cachefilesd on NFS mounts. Useful if you work remote on a mounted NFS share. btrfs is not supported, so I create an ext4 mount instead like this:

dd if=/dev/zero of=/tmp/fscache.txt bs=1M count=4000
# be careful with choosing /dev/loopN
losetup /dev/loop10 /tmp/fscache.txt
mkfs.ext4 /dev/loop10
mount /dev/loop10 /var/cache/fscache

Very useful other commands can be found here: http://www.cyberciti.biz/faq/centos-redhat-install-configure-cachefilesd-for-nfs/ (i.e.: cat /proc/fs/nfsfs/*)

Benchmark I/O

start iperf -s server, then iperf -c 127.0.0.1.

Show Network I/O per process

atop can do this, which comes provided by your package manager probably, but chances are you need to compile it yourself for a newer version. The new version makes it possible to compile a kernel module, which (after reboot) will make network counters available in atop. Quite awesome!

simulate slow network

export interface=enx9cebe8349fdc
sudo tc qdisc add dev $interface root netem delay 10000ms
sudo tc qdisc del dev $interface root netem 

source


Prints files count per directory for current directory level:

du -a | cut -d/ -f2 | sort | uniq -c | sort -nr

UDP traffic through SSH tunnel

http://superuser.com/questions/53103/udp-traffic-through-ssh-tunnel https://securesocketfunneling.github.io/ssf/#download

Using iptables to redirect ip address

iptables -t nat -A OUTPUT -d 10.141.0.1 -j DNAT --to-destination 8.8.8.8

http://superuser.com/questions/681705/using-iptables-to-redirect-ip-address

sslh

http://www.rutschle.net/tech/sslh.shtml i.e., http://www.rutschle.net/sslh#using-proxytunnel-with-sslh

WHAT IS IT? sslh accepts connections on specified ports, and forwards them further based on tests performed on the first data packet sent by the remote client.

sed

trigen@zenbook:~> echo "Hello world (y)" | sed 's/(y)/:-)/'
Hello world :-)
trigen@zenbook:~> echo "Hello world (y)" | sed 's/.*\((y)\)/smiley: \1/'
smiley: (y)

trigen@zenbook:~> sed -i.bak 's/H/J/g' test.txt
trigen@zenbook:~> cat test.txt
Jello world
trigen@zenbook:~> cat test.txt.bak
Hello world

check if port is open

nc -z -v -w1 cppse.nl 80

openssl check certificates

openssl x509 -in <INPUT> -text

openssl s_client -showcerts -connect cppse.nl:443 </dev/null
# in case of SNI (combined domains in certificate)
openssl s_client -showcerts -servername cppse.nl -connect www.cppse.nl:443 </dev/null
Cheatsheets Comments (0)
 
July 16 2016

C++ streams

i.e.;

 std::ifstream fi;
 fi.open("all.csv");
 std::string line;
 while (getline(fi, line, '\n')) {
     std::cout << "line = " << line << std::endl;
 }

vector

Initialize N elements at once with: vector<int> vec_out(vec_in.size(), 0);.

Put last elem in front data[0] = data[data.size() - 1]; and erase last data.erase(data.begin() + (data.size() - 1), data.end());

algorithms

Sum accumulate(count.begin(), count.end(), 0).

Rotate 1 2 3 -> 2 3 1 with rotate(A.begin(), A.begin() + K, A.end());, the other way around with rbegin() and rend() respectively.

Find std::find(std::begin(c), std::end(c), 1001) != std::end(c);

Erase + remove idiom: c.erase(std::remove(std::begin(c), std::end(c), 1001), std::end(c));

join string boost so

#include <boost/algorithm/string/join.hpp>
#include <vector>
#include <iostream>

int main(int, char **)
{
    std::vector<std::string> list;
    list.push_back("Hello");
    list.push_back("World!");

    std::string joined = boost::algorithm::join(list, ", ");
    std::cout << joined << std::endl;
}

Templates

Forwarding

template <typename... Params>
void remove(Params &&... node) {
  static_assert(std::is_constructible<Type, Params...>::value, "parameters are invalid for constructing a Type");
  auto n = std::make_unique<value_type >(std::forward<Params>(node)...);
  remove(static_cast<const value_type &>(*n.get()));
}

stringstream

#include <sstream>
stringstream ss;
ss << "hello world" << 123 << endl;
ss.str(); // ...
ss.clear();
ss.str("");  // re-use.

binary search

std::sort (v.begin(), v.end());                       // 10 10 10 20 20 20 30 30
auto low = std::lower_bound (v.begin(), v.end(), 20); //          ^        ^
auto up  = std::upper_bound (v.begin(), v.end(), 20); //                   ^

priority_queue

// "if the number is less.. goes down the pile"
priority_queue<int, vector<int>, std::less<int>> pq; 
for (int i : {1, 6, 3}) 
    pq.emplace(i);
pq.top(); // 6
pq.pop();
pq.top(); // 3

Exposes it's container as protected member variable. In order to access, you have to extend priority_queue. Underlying data structure is a heap.

bitsets

bitset<8> bs; 
bs.set(0);
bs.set(2); // 00000101

set

Underlying container type is a binary tree. So most stuff is log n. Note that a binary tree is also sorted, so the first element *the_set.cbegin() is the min() element!

memory

unique_ptr<foo> p{nullptr};
p = make_unique<foo>();

Perfect forwarding variable template args

namespace std {
  template<typename T, typename ...Args>
  std::unique_ptr<T> make_unique(Args &&...args) {
    return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
  }
}

limits

std::numeric_limits<size_t>::max();

ostream_iterator

#include <iterator>
copy(tokens.begin(), tokens.end(), ostream_iterator<string>(cout, "\n"));

readable input parameters

void split(string in, vector<string> *tokens_ptr, string delim = " ") {
    vector<string> &tokens = *tokens_ptr;
    ...

split(input, &tokens, " ");

string

// find
string s("aa b c");
s.find(" "); // finds 2
s.find(" ", 3); // finds 4 (starting search from the "b")
// substr()
s.substr(0, 2); // start + length!
s.substr(pos, pos - previous); // typical

tokenizer with getline()

really nice implementation using getline() from this stackoverflow answer.

#include <string>
#include <sstream>
#include <vector>

using namespace std;

void split(const string &s, char delim, vector<string> &elems) {
    stringstream ss(s);
    string item;
    while (getline(ss, item, delim)) {
        elems.push_back(item);
    }
}


vector<string> split(const string &s, char delim) {
    vector<string> elems;
    split(s, delim, elems);
    return elems;
}

iterate to stdout with ostream_iterator<T> and copy()

#include <iterator>
#include <algorithm>

// output even a newline after each element
copy(c.begin(), c.end(), ostream_iterator<string>(cout, "\n"));

convert binary file into c code

xxd -i favicon.ico > favicon.ico.h

sorting concatinating

template <typename T>
std::string join_keys(std::vector<T> &idsVec) {
    std::stringstream ids;

    // remove dupes
    std::sort(idsVec.begin(), idsVec.end());
    idsVec.erase(std::unique(idsVec.begin(), idsVec.end() ), idsVec.end());

    // join ids with ","
    std::copy(idsVec.begin(), idsVec.end(), std::ostream_iterator<T>(ids, ","));
    std::string ids_str = ids.str();

    // remove the trailing ", "
    ids_str.erase(ids_str.empty() ? 0 : ids_str.length() - 1);
    return ids_str;
};

optional

c++17 will support it, in g++ version 7.0 it can be enabled with --std=c++1z

#include <optional>

...
std::optional<std::string> empty;
std::optional<std::string> filled{"hello"};

if (filled) {
    std::cout << "filled = " << *filled << std::endl;
}   
std::cout << "empty = " << empty.value_or("nothing") << std::endl;

c++14 flag --std=c++1y in g++ 4.9 supports std::experimental::optional

#include <experimental/optional>

...
std::experimental::optional<int> empty;

Note there is also make_optional.

file reading

using namespace std;
ifstream ifile("webroot/"s + folder + "/" + file, ios::binary);
string s( (istreambuf_iterator<char>(ifile)),
          (istreambuf_iterator<char>()) );
cout << "read = " << s.size() << " & " << s.length() << endl;

multiline string literal

const char * long_test = R"( ... )";
const char * long_test = R"XXX( ... )XXX";

std::experimental::optional and std::reference_wrapper

Passing optional reference:

#include <iostream>
#include <experimental/optional>
#include <functional>

namespace std {
  using std::experimental::optional;
}

void foo(std::optional<std::reference_wrapper<bool>> flag = {}) {
    if (flag) {
        flag->get() = true;
    }
}

int main() {
    bool flag = false;
    foo({flag});
    std::cout << "flag is now: " << std::boolalpha << flag << std::endl;
}

chrono stuff

/* sleeping */
std::this_thread::sleep_for(std::chrono::milliseconds(1000));

/* timing */
auto last_received = std::chrono::high_resolution_clock::now();
...
auto current_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> idle = current_time - last_received;
if (idle.count() > 500.) {

    /* literals */
    using namespace std::literals;

    std::this_thread::sleep_for(0.5s);

}
Cheatsheets Comments (0)

Author:
Ray Burgemeestre
february 23th, 1984

Topics:
C++, Linux, Webdev

Other interests:
Music, Art, Zen