Skip to content

Instantly share code, notes, and snippets.

View deeplook's full-sized avatar

deeplook

View GitHub Profile
@deeplook
deeplook / compact_structured_json.py
Last active March 22, 2016 21:50
Create a structured, but more compact JSON representation of a native data structure.
from __future__ import print_function
"""
Create a structured, but more compact JSON representation of a native data structure.
This is inspired by the way native Python data structures are displayed in Jupyter,
e.g.:
In [1]: d = {"count": 7, "limit": 7, "results": [{"path": "wifi", "meaning": "rssi", "points": [{"timestamp": 1458211728242, "value": -67}, {"timestamp": 1458211736170, "value": -74}, {"timestamp": 1458211740399, "value": -62}, {"timestamp": 1458211746265, "value": -64}], "deviceId": "1e31f865-93b9-482a-a7be-858b56c396ae"}, {"path": "/", "meaning": "batteryLevel", "points": [{"timestamp": 1458211728413, "value": 90}, {"timestamp": 1458211736216, "value": 90}, {"timestamp": 1458211740443, "value": 90}], "deviceId": "1e31f865-93b9-482a-a7be-858b56c396ae"}], "offset": 0}
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
A tool for signaling processes found also by their command-line invocation.
Sometimes you want to terminate a process based on some specific substring
in the command that was entered in a terminal to start it, e.g. here you
might want to terminate only a process like this that is running a Python
SimpleHTTPServer started with::
@deeplook
deeplook / pixel_planet.ipynb
Last active June 13, 2016 09:01
Try to find out easily (read: interactively) using Jupyter widgets how many pixels we need to store in order to cover larger parts of the planet.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@deeplook
deeplook / test-basename-widget.ipynb
Created June 28, 2016 10:31
This is an experiment in creating a Jupyter notebook showing a world map with different parameters (including map projection) by combining a Matplotlib Basemap and IPython widgets.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@deeplook
deeplook / periodic_timer.py
Created July 20, 2016 12:41
A class wrapping a callable to be called periodically inside a thread.
import datetime
import threading
class PeriodicTimer(object):
"""
A class wrapping a callable to be called periodically inside a thread.
The wrapped function is called the first time immediately after creating
an instance of this class.
@deeplook
deeplook / timestamp.py
Last active October 26, 2021 07:00 — forked from ju-popov/timestamp.py
Python DateTime / Timestamp Conversion
######################################################################
# CURRENT AWARE LOCAL DATETIME
######################################################################
from datetime import datetime
from tzlocal import get_localzone # pip install tzlocal
local_tz = get_localzone()
local_dt = datetime.now(local_tz)
@deeplook
deeplook / slowcat.py
Created July 28, 2017 11:56
Implement a "slow-motion" Unix 'cat' command.
#!/usr/bin/env python
"""
Implement a "slow-motion" Unix 'cat' command.
Intended to be used with tools like 'asciinema', see asciinema.org.
None of the options of Unix 'cat' are implemented here.
Examples:
@deeplook
deeplook / mapit.py
Last active August 8, 2017 11:31
A little tool to render GeoJSON files as layers in an HTML output map file.
#!/usr/bin/env python
"""
A little tool to render GeoJSON files as layers in an HTML output map file.
The output map shows the region covered by the combined bounding box of all
GeoJSON input files. This was tested only with Python 3.5, but should also
work on Python >= 2.7.
The pygeoj package does calculate the bounding box correctly only for version
@deeplook
deeplook / benchmark_fib.sh
Last active May 28, 2019 19:43
Benchmark using Fibonacci numbers with Python, Cython and PyPy.
#!/usr/bin/env bash
echo "Benchmark for Fibonacci numbers with Python3, Cython and PyPy"
echo '
def fib(n):
"Return the n-th Fibonacci number."
i = 0
a, b = 0, 1
if n < 2:
@deeplook
deeplook / scalavarning.py
Created September 28, 2017 09:22
Spot single tokens (like the 'var' keyword) in Scala source code files.
#!/usr/bin/env python
"""
List keyword tokens in Scala source code.
Originally intended for spotting lines containing "var" declarations
in Scala source files (hence the name "scala varning") which are
somewhat considered "harmful" in Scala. The same can be useful for
spotting explicit "return" statements which might be superfluous,
and, of course, other keywords.