Skip to content

Instantly share code, notes, and snippets.

View mrsarm's full-sized avatar
🏠
Working from home

Mariano Ruiz mrsarm

🏠
Working from home
View GitHub Profile
@mrsarm
mrsarm / pg_stat_activity.sql
Created January 29, 2020 20:26
pg_stat_activity.sql: check PostgreSQL active connections (and idle)
-- Find all active connections (and idle)
SELECT
pid
,state
,datname
,usename
,application_name
,client_hostname
,client_port
,backend_start
@mrsarm
mrsarm / find-samples.sh
Created August 21, 2019 17:08
find-samples.sh: examples of how to use the `find` command, and combine it with others commands
# Find in the current folder (recursively) all the files with .xml extension.
find . -name '*.xml'
# Find all the __pycache__ files (or folders) and execute
# for each result the command `rm -r` with the filename as a first argument
find . -name "__pycache__" -print0 | xargs -0 rm -r
# Find into the /tmp folder files that the path matchs a regex expression
find /tmp -regextype posix-egrep -regex ".*\.(le|c)ss$"
@mrsarm
mrsarm / time.py
Last active October 5, 2022 15:13
Python date and time handling examples
# Get valid patterns from http://strftimer.com/
>>> from datetime import datetime, date
>>> datetime.strptime("January 23, 2019", "%B %d, %Y")
datetime.datetime(2019, 1, 23, 0, 0)
>>> datetime.strptime("January 23, 2018", "%B %d, %Y").strftime("%Y-%m-%d")
'2018-01-23'
>>> datetime.strptime("2021-01-01T18:33:00Z", '%Y-%m-%dT%H:%M:%SZ') # The ISO format supported by Python does not parse dates ended with 'Z'
@mrsarm
mrsarm / video2gif.sh
Created March 22, 2019 17:13
video2gif.sh: convert any input video file (avi, mp4...) to a gif file with a reasonable file size
#!/usr/bin/env sh
#
# video2gif.sh
#
# See https://askubuntu.com/questions/648603/how-to-create-an-animated-gif-from-mp4-video-via-command-line
#
# Useful Params:
#
# -r Num of frames x sec
# -vf scale=512:-1 Scale up to 512, remove to preserve original resolution
@mrsarm
mrsarm / rmduplicates.py
Last active February 25, 2019 14:10
rmduplicates.py: filter repeated JSON objects from a given JSON line file
#!/usr/bin/python3
"""
Filter repeated JSON objects from a given JSON line file.
""" # noqa
import argparse
import json
import sys
parser = argparse.ArgumentParser(
@mrsarm
mrsarm / msdos2utf8.py
Last active August 22, 2019 19:54
Use: ./msdos2utf8.py OLD_DOS_FILE NEW_ENCODED_FILE
#!/usr/bin/python3
#
# Usage: ./msdos2utf8.py OLD_DOS_FILE NEW_ENCODED_FILE
#
# Creates a new file UTF-8 encoded, taking the input file as a file with old ASCII encoded used in MS-DOS systems.
#
# Try to replace 'cp437' with 'iso8859' if the new file isn't encoded properly
import sys
@mrsarm
mrsarm / random.py
Last active January 23, 2019 19:28
Python random generation examples
from random import random, choice, choices, randint, seed
import string
seed()
random() # Floating point number in the range [0.0, 1.0)
0.24756287159145907
choice(['A','B','Z'])
'B'
@mrsarm
mrsarm / json-flattening.sh
Created April 13, 2018 13:26
Get all the columns names a flat structure (CSV) should have to store the same info than a given JSON file
cat file.json | jq '([leaf_paths as $path | {"key": $path | map(tostring) | join("_"),"value": getpath($path)}] | from_entries) | keys' > file_keys.txt
@mrsarm
mrsarm / log.py
Created March 26, 2018 17:57
Log errors in Python
# See https://docs.python.org/3/library/logging.html#logging.debug ::
#
# There are three keyword arguments in kwargs which are inspected:
# exc_info which, if it does not evaluate as false, causes exception information to be
# added to the logging message. If an exception tuple (in the format returned by sys.exc_info())
# is provided, it is used; otherwise, sys.exc_info() is called to get the exception information.
#
# Changed in version 3.5: The exc_info parameter can now accept exception instances.
import logging
find . -name "__pycache__" -print0 | xargs -0 rm -r