Programming languages

How to initialize an empty KiCAD project on the command line

TL;DR:

Note: We recommend to use our new script to initialize a project with project-specific footprint and symbol libraries, see How to initialize your KiCAD project on the command line. The script on this page initializes an empty project without any libraries.

Inside the directory where you want to create the project, run

wget -qO- https://techoverflow.net/scripts/kicad-initialize.sh | bash /dev/stdin MyProject

You should replace MyProject (at the end of the command) with your project name.

Note: This will initialize an empty KiCAD project without any libraries. This is equivalent to creating a new project in KiCAD itself (using the GUI).

Continue reading →

Posted by Uli Köhler in Electronics, KiCAD, Shell

bash template: Script with one argument

Use this template to create a bash / shell script with one argument that prints a usage message if used with the wrong number of arguments:

#!/bin/bash
if [ $# -ne 1 ]
then
    echo "Usage: $0 <filename>"
    exit 1
fi
# Note: The argument is $1
# TODO: Place your code here

 

Posted by Uli Köhler in Shell

How to fix Puppetteer ‘Running as root without –no-sandbox is not supported’

Problem:

When you try to run your puppetteer application, e.g. under docker, you see this error message:

Solution:

Note: Unless you are running in a Docker or similar container, first consider running the application as non-root-user!

You have to pass the --no-sandbox option to puppeteer.launch():

const browser = await puppeteer.launch({
    headless: true,
    args: ['--no-sandbox']
});

We recommend to use this slightly more complex solution to pass it only if the process is being run as root:

/**
 * Return true if the current process is run by the root user
 * https://techoverflow.net/2019/11/07/how-to-check-if-nodejs-is-run-by-root/
 */
function isCurrentUserRoot() {
   return process.getuid() == 0; // UID 0 is always root
}

const browser = await puppeteer.launch({
    headless: true,
    args: isCurrentUserRoot() ? ['--no-sandbox'] : undefined
});

This ensures Chromium is run in the most secure mode possible with the current user.

Posted by Uli Köhler in Javascript, NodeJS, Puppeteer

How to check if NodeJS is run by root

Use this function to determine if the current NodeJS process is being run by the root user:

function isCurrentUserRoot() {
    return process.getuid() == 0; // UID 0 is always root
}

Usage example:

if(isCurrentUserRoot()) {
    // TODO Your code for root user goes here
} else {
    // TODO Your code for NON-root user goes here!
}

This works since the UID of the root user is always 0 on Linux/Unix systems.

Note: You don’t need to

const process = require("process");

since the process object is automatically imported in any NodeJS environment

Posted by Uli Köhler in Javascript, NodeJS

How to visualize I2C first byte structure in TikZ

In our previous post on How many bits does an I2C address have? we showed this diagram:

This diagram was made using TikZ using the following source code:

% I2C first byte diagram
% Author: Uli Koehler (https://techoverflow.net)
\documentclass[tikz, border=1mm]{standalone}
\usetikzlibrary{positioning, decorations.pathreplacing, arrows.meta}

% Source: https://tex.stackexchange.com/a/24133/45450
\makeatletter
\newcommand*{\textoverline}[1]{$\overline{\hbox{#1}}\m@th$}
\makeatother

\begin{document}
\begin{tikzpicture}
\node (A6) [draw, minimum height=7mm, minimum width=10mm] {$A_6$};
\node (A5) [draw, minimum height=7mm, minimum width=10mm, right=0cm of A6] {$A_5$};
\node (A4) [draw, minimum height=7mm, minimum width=10mm, right=0cm of A5] {$A_4$};
\node (A3) [draw, minimum height=7mm, minimum width=10mm, right=0cm of A4] {$A_3$};
\node (A2) [draw, minimum height=7mm, minimum width=10mm, right=0cm of A3] {$A_2$};
\node (A1) [draw, minimum height=7mm, minimum width=10mm, right=0cm of A2] {$A_1$};
\node (A0) [draw, minimum height=7mm, minimum width=10mm, right=0cm of A1] {$A_0$};
\node (RW) [draw, minimum height=7mm, minimum width=10mm, right=0cm of A0] {R/\textoverline{W}};

\coordinate[above left=0mm of A6] (AddrLeft);
\coordinate[above right=0mm of A0] (AddrRight);

\coordinate[below left=0mm of RW] (RWLeft);
\coordinate[below right=0mm of RW] (RWRight);

\draw[decorate,decoration={brace,amplitude=10pt}, minimum height=9mm]
    (AddrLeft) -- (AddrRight)
    node[anchor=south,midway,above=8pt] {I2C address};

\draw[decorate,decoration={brace,amplitude=8pt}, minimum height=6mm]
    (RWRight) -- (RWLeft)
    node[anchor=north,midway,below=8pt] {Read/\textoverline{Write} bit};

\end{tikzpicture}
\end{document}

and compiled to SVG using this Makefile – for details, see How to export TikZ graphics as SVG:

%.pdf: %.tex
    pdflatex $<

%.svg: %.pdf
    pdf2svg $< $@

all: I2CFirstByte.svg

 

Posted by Uli Köhler in LaTeX

How to export TikZ graphics as SVG

If you have a TikZ graphic, you can use the LaTeX standalone package to make the page fit the content:

% Minimal TikZ standalone example
\documentclass[tikz, border=1mm]{standalone}

\begin{document}
\begin{tikzpicture}
\draw (0,0) node [] {My text};
\end{tikzpicture}
\end{document}

Assuming you have saved that file as MyDiagram.tex, you can convert it to a PDF and subsequently convert that PDF to a SVG using

pdflatex MyDiagram.tex
pdf2svg MyDiagram.pdf MyDiagram.svg

which will generate this SVG:

Note that the 1mm border around the content is intentional and recommended for most usecases. The background is transparent by default (but has been set to white in HTML on this blogpost to illustrate the extent of the SVG).

You can also use this Makefile template:

%.pdf: %.tex
    pdflatex $<

%.svg: %.pdf
    pdf2svg $< $@

all: MyDiagram.svg

which allows you to automatically run the commands for one or multiple TeX files.

Posted by Uli Köhler in LaTeX

Minimal TikZ standalone example

This example contains a simple TikZ graphic using the standalone package, i.e. it will be exported to a PDF/DVI where the page just fits the content:

\documentclass[tikz, border=1mm]{standalone}

\begin{document}
\begin{tikzpicture}
\draw (0,0) node [draw=black] {My text};
\end{tikzpicture}
\end{document}

The result looks like this:

Assuming the file is named MyDiagram.tex, you can compile it to PDF using

pdflatex MyDiagram.tex

Note that our example contains a 1mm border by default since this seems to be more suitable for the average usecase than having no border at all. In order to change that, you just need to modify the first line, e.g.

\documentclass[tikz]{standalone} % No border

or

\documentclass[tikz]{standalone, border=5mm} % 5mm border
Posted by Uli Köhler in LaTeX

How to add box / border around node in TikZ

In TikZ, if you have a node like

\draw (0,0) node [] {My text};

you can add a border around it by using the draw=... attribute for the node:

\draw (0,0) node [draw] {My text};

You can also tell TikZ to draw it in blue:

\draw (0,0) node [draw=blue] {My text};

or tell it to draw it dashed:

\draw (0,0) node [draw, dashed] {My text};

You can also make the border thin or thick:

\draw (0,0) node [draw, very thin] {Very thin border};
\draw (0,-1) node [draw, thin] {Thin border};
\draw (0,-2) node [draw, semithick] {Semithick border};
\draw (0,-3) node [draw, thick] {Thick border};
\draw (0,-4) node [draw, very thick] {Very thick border};
\draw (0,-5) node [draw, ultra thick] {Ultra thick border};

Posted by Uli Köhler in LaTeX

How to get milliseconds from pandas.Timedelta object

If you have a pandas.Timedelta object, you can use Timedelta.total_seconds() to get the seconds as a floating-point number with millisecond resolution and then multiply with one billion (1e3, the number of milliseconds in one second) to obtain the number of milliseconds in the Timedelta:

timedelta.total_seconds() * 1e3

In case you want an integer, use

int(round(timedelta.total_seconds() * 1e3))

Note that using round() is required here to avoid errors due to floating point precision.

or use this function definition:

def milliseconds_from_timedelta(timedelta):
    """Compute the milliseconds in a timedelta as floating-point number"""
    return timedelta.total_seconds() * 1e3

def milliseconds_from_timedelta_integer(timedelta):
    """Compute the milliseconds in a timedelta as integer number"""
    return int(round(timedelta.total_seconds() * 1e3))

# Usage example:
ms = milliseconds_from_timedelta(timedelta)
print(ms) # Prints 2000.752
ms = milliseconds_from_timedelta_integer(timedelta)
print(ms) # Prints 2001

 

Posted by Uli Köhler in pandas, Python

How to get microseconds from pandas.Timedelta object

If you have a pandas.Timedelta object, you can use Timedelta.total_seconds() to get the seconds as a floating-point number with microsecond resolution and then multiply with one million (1e6, the number of microseconds in one second) to obtain the number of microseconds in the Timedelta:

timedelta.total_seconds() * 1e6

In case you want an integer, use

int(round(timedelta.total_seconds() * 1e6))

Note that using round() is required here to avoid errors due to floating point precision.

or use this function definition:

def microseconds_from_timedelta(timedelta):
    """Compute the microseconds in a timedelta as floating-point number"""
    return timedelta.total_seconds() * 1e6

def microseconds_from_timedelta_integer(timedelta):
    """Compute the microseconds in a timedelta as integer number"""
    return int(round(timedelta.total_seconds() * 1e6))

# Usage example:
us = microseconds_from_timedelta(timedelta)
print(us) # Prints 2000751.9999999998

us = microseconds_from_timedelta_integer(timedelta)
print(us) # Prints 2000752
Posted by Uli Köhler in pandas, Python

How to get nanoseconds from pandas.Timedelta object

If you have a pandas.Timedelta object, you can use Timedelta.total_seconds() to get the seconds as a floating-point number with nanosecond resolution and then multiply with one billion (1e9, the number of nanoseconds in one second) to obtain the number of nanoseconds in the Timedelta:

timedelta.total_seconds() * 1e9

In case you want an integer, use

int(round(timedelta.total_seconds() * 1e9))

Note that using round() is required here to avoid errors due to floating point precision.

or use this function definition:

def nanoseconds_from_timedelta(timedelta):
    """Compute the nanoseconds in a timedelta as floating-point number"""
    return timedelta.total_seconds() * 1e9

def nanoseconds_from_timedelta_integer(timedelta):
    """Compute the nanoseconds in a timedelta as integer number"""
    return int(round(timedelta.total_seconds() * 1e9))

# Usage example:
ns = nanoseconds_from_timedelta(timedelta)
print(ns) # Prints 2000751999.9999998

ns = nanoseconds_from_timedelta_integer(timedelta)
print(ns) # Prints 2000752000

 

Posted by Uli Köhler in pandas, Python

How to create pandas.Timedelta object from two timestamps

If you have two pandas.Timestamp objects, you can simply substract them using the minus operator (-) in order to obtain a pandas.Timedelta object:

import pandas as pd
import time

# Create two timestamps
ts1 = pd.Timestamp.now()
time.sleep(2)
ts2 = pd.Timestamp.now()

# The difference of these timestamps is a pandas.Timedelta object.
timedelta = ts2 - ts1
print(timedelta) # Prints '0 days 00:00:02.000752'

 

Posted by Uli Köhler in pandas, Python

How to get current timestamp in Pandas

Use

import pandas as pd

current_timestamp = pd.Timestamp.now()

pd.Timestamp.now() will return the timestamp as pandas.Timestamp object.

Example:

>>> import pandas as pd
>>> pd.Timestamp.now()
Timestamp('2019-10-27 16:11:43.998993')
Posted by Uli Köhler in pandas, Python

How to fix ModuleNotFoundError: No module named ‘dateutil’

Problem:

You want to use the dateutil library in Python, but when you try to import it, you see an exception like this:

Traceback (most recent call last):
  File "my.py", line 11, in <module>
    import dateutil as dl
ModuleNotFoundError: No module named 'dateutil'

When you try installing it using e.g. pip install dateutil you see this error message:

Collecting dateutil
  Could not find a version that satisfies the requirement dateutil (from versions: )
No matching distribution found for dateutil

Solution:

The package is named python-dateutil. Install using

sudo pip install python-dateutil

or (for Python3)

sudo pip3 install python-dateutil

 

Posted by Uli Köhler in Python

How to check NODE_ENV environment variable in Node.JS

You can use process.env.NODE_ENV to check the value of the NODE_ENV environment variable in Node.JS:

if(process.env.NODE_ENV == "development") {
    // TODO your code goes here
}

or

if(process.env.NODE_ENV == "production") {
    // TODO your code goes here
}

Note that by default NODE_ENV is undefined so remember to handle that case appropriately.

Posted by Uli Köhler in Javascript, NodeJS

Minimal Koa.JS example with Router & Body parser

This is the minimal Koa.JS application which I use as a template for new NodeJS webserver applications.

#!/usr/bin/env node
const router = require('koa-router')();
const koaBody = require('koa-body');
const Koa = require('koa');
const app = new Koa();

app.use(koaBody());

router.get('/', async ctx => {
    ctx.body = "Hello world";
});

app.use(router.routes());

if (!module.parent) app.listen(3000);

Install the requirements using

npm i --save koa koa-router koa-body

and run using

node index.js

assuming you have saved our code from above in index.js.

Now (with the node index.js command still running) go to http://localhost:3000 . You should see Hello world there. Now it’s your turn to continue on your endeavour to develop the world’s greatest webservers 🙂

Posted by Uli Köhler in Javascript, NodeJS

What does ‘if (!module.parent)’ mean in NodeJS?

In NodeJS applications you often see code like

if (!module.parent) {
    app.listen(3000);
}

This means: Run app.listen(3000) only if you are running the file

Suppose this code is in index.js. In this case, the code will only be executed if you run index.js directly (i.e. using node index.js) and not if index.js is required from another file (by require('./index.js');).

If index.js is required from another Javascript module (i.e. file), module.parent will be set to that module.

Posted by Uli Köhler in Javascript, NodeJS

How to get schema of SQLite3 table in Python

Also see How to show table schema for SQLite3 table on the command line

Use this function to find the table schema of a SQLite3 table in Python:

def sqlite_table_schema(conn, name):
    """Return a string representing the table's CREATE"""
    cursor = conn.execute("SELECT sql FROM sqlite_master WHERE name=?;", [name])
    sql = cursor.fetchone()[0]
    cursor.close()
    return sql

Usage example:

print(sqlite_table_schema(conn, 'commands'))

Full example:

#!/usr/bin/env python3
import sqlite3
conn = sqlite3.connect('/usr/share/command-not-found/commands.db')

def sqlite_table_schema(conn, name):
    cursor = conn.execute("SELECT sql FROM sqlite_master WHERE name=?;", [name])
    sql = cursor.fetchone()[0]
    cursor.close()
    return sql

print(sqlite_table_schema(conn, 'commands'))

which prints

CREATE TABLE "commands" 
           (
            [cmdID] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
            [pkgID] INTEGER NOT NULL,
            [command] TEXT,
            FOREIGN KEY ([pkgID]) REFERENCES "pkgs" ([pkgID])
           )

 

 

Posted by Uli Köhler in Python, SQLite

How to fix SQLite3 Python ‘Incorrect number of bindings supplied. The current statement uses 1, … supplied’

Problem:

You are trying to run a simple SQL query with placeholders on a SQLite3 database, e.g.:

name = "mytable"
conn.execute("SELECT sql FROM sqlite_master WHERE name=?;", name)

But you get an exception like this:

---------------------------------------------------------------------------
ProgrammingError                          Traceback (most recent call last)
<ipython-input-55-e385cf40fd72> in <module>
      1 name = "mytable"
----> 2 conn.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name=?;", name)

ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 7 supplied.

Solution:

You need to use a list as the second argument to conn.execute(...)!

Since you only gave the function a string, the string is being interpreted as list of characters.

In our example from above, you just need to wrap name in square brackets to read [name]:

name = "mytable"
conn.execute("SELECT sql FROM sqlite_master WHERE name=?;", [name])
Posted by Uli Köhler in Python, SQLite

How to list tables in SQLite3 database in Python

Also see How to list SQLite3 database tables on command line

You can use this snippet to list all the SQL tables in your SQLite 3.x database in Python:

def tables_in_sqlite_db(conn):
    cursor = conn.execute("SELECT name FROM sqlite_master WHERE type='table';")
    tables = [
        v[0] for v in cursor.fetchall()
        if v[0] != "sqlite_sequence"
    ]
    cursor.close()
    return tables

Usage example:

#!/usr/bin/env python3
import sqlite3
# Open database
conn = sqlite3.connect('/usr/share/command-not-found/commands.db')
# List tables
tables = tables_in_sqlite_db(conn)

# Your code goes here!
# Example:
print(tables) # prints ['commands', 'packages']

 

Posted by Uli Köhler in Databases, Python, SQLite