Skip to content

Instantly share code, notes, and snippets.

View deehzee's full-sized avatar

Debajyoti Nandi deehzee

View GitHub Profile
@deehzee
deehzee / print256colours.sh
Created March 24, 2017 12:46 — forked from HaleTom/print256colours.sh
Print a 256-colour test pattern in the terminal
#!/bin/bash
# Tom Hale, 2016. MIT Licence.
# Print out 256 colours, with each number printed in its corresponding colour
# See http://askubuntu.com/questions/821157/print-a-256-color-test-pattern-in-the-terminal/821163#821163
set -eu # Fail on errors or undeclared variables
printable_colours=256
@deehzee
deehzee / colortest.py
Created March 24, 2017 12:42 — forked from justinabrahms/colortest.py
Small utility to test terminal support for 256-color output.
#!/usr/bin/env python
# Ported to Python from http://www.vim.org/scripts/script.php?script_id=1349
print "Color indexes should be drawn in bold text of the same color."
print
colored = [0] + [0x5f + 40 * n for n in range(0, 5)]
colored_palette = [
"%02x/%02x/%02x" % (r, g, b)
for r in colored
@deehzee
deehzee / colortest.py
Created March 24, 2017 12:42 — forked from justinabrahms/colortest.py
Small utility to test terminal support for 256-color output.
#!/usr/bin/env python
# Ported to Python from http://www.vim.org/scripts/script.php?script_id=1349
print "Color indexes should be drawn in bold text of the same color."
print
colored = [0] + [0x5f + 40 * n for n in range(0, 5)]
colored_palette = [
"%02x/%02x/%02x" % (r, g, b)
for r in colored
@deehzee
deehzee / handling_time.md
Last active March 9, 2017 10:34
How to convert date-time into timestamp and vice versa.

Conversion between timestamp and human date-time

Different ways to convert time between human readable format and timestamps.

import time

# How to convert from human datetime in local time to timestamp (s)
print(int(time.mktime(time.strptime('2017-03-09 15:39:30', '%Y-%m-%d %H:%M:%S'))))
@deehzee
deehzee / autoreload.md
Last active December 1, 2021 19:48
Auto reload of modules in jupyter notebook

Module autoreload

To auto-reload modules in jupyter notebook (so that changes in files *.py doesn't require manual reloading):

# for auto-reloading external modules
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2
@deehzee
deehzee / psycopg2_sshtunnel.py
Last active September 13, 2023 08:09
How to Connect To PostgreSQL Using SSHTunnelForwarder and Psycopg2
import psycopg2
from sshtunnel import SSHTunnelForwarder
# For interactive work (on ipython) it's easier to work with explicit objects
# instead of contexts.
# Create an SSH tunnel
tunnel = SSHTunnelForwarder(
('128.199.169.188', 22),
ssh_username='<username>',
@deehzee
deehzee / reset-sys-unicode.py
Last active February 8, 2017 13:13
Setting Default Encoding to Unicode in Python
# reset-sys-unicode.py
#
# http://stackoverflow.com/questions/21129020/how-to-fix-unicodedecodeerror-ascii-codec-cant-decode-byte
#
# to encode a text with utf8 in python2
import sys
reload(sys)
sys.setdefaultencoding('utf8')
@deehzee
deehzee / py-six.py
Last active February 8, 2017 13:13
Common Python 2-3 Compatibility Fixes
# Some common idioms to make Python work both in version 2.7 and 3.x
# For more examples, see:
# http://python-future.org/compatible_idioms.html
# Absolute import
from __future__ import absolute_import
# Floating point division
from __future__ import division
@deehzee
deehzee / md5sum.py
Last active February 8, 2017 13:15
Compute MD5SUM of a File in Python
# Compute md5sum of a file
# File: md5sum.py
# Debajyoti Nandi
# 2016-11-09
# Python 2
import os
import hashlib
@deehzee
deehzee / dir-walk.py
Last active February 8, 2017 13:16
How to walk in a directory in python (Find all files in a directory, or all files in the directory-tree)
# Examples of how to walk through a directory (root)
# File: dir-walk.py
# Debajyoti Nandi
# 2016-11-09
import os
root = "."