Skip to content

Instantly share code, notes, and snippets.

@stupidbodo
stupidbodo / type_conversion.go
Last active July 14, 2020 05:05
Golang Type Conversion
// Playbook: http://play.golang.org/p/kp5uL_mMGk
// Note: Type assertion like "someVariable.(string)" is useful when you are dealing with interface{} (empty interface)
package main
import (
"fmt"
"strconv"
)
@stupidbodo
stupidbodo / datetime_in_python.py
Last active August 30, 2022 00:08
Handling Datetime in Python
# Default timezone is UTC and precisions is up to microseconds
#======================================
# Workflow
#======================================
# 1) Convert string to it's native datetime type.
# 2) Do all manipulation when the value is in datetime type
# 3) Convert it back to the type you need(if needed).
# This means that if you want to convert string "1970-01-01" to
@stupidbodo
stupidbodo / xor.py
Created July 10, 2014 05:44
Logical Exclusive Or VS Bitwise Exclusive Or - Python
# Exclusive or is a logical operation that outputs true whenever both inputs differ
# Truth Table
A = 0, B = 0, Output = 0
A = 1, B = 0, Output = 1
A = 0, B = 1, Output = 1
A = 1, B = 1, Output = 0
# Example of logical xor
# Logical xor evaluates expression logically(like how humans would read it)
@stupidbodo
stupidbodo / format_html_strings.py
Last active August 29, 2015 14:03
Formatting HTML Strings in Python
# 2 options in formatting html strings in python
# 1) You don't care about excess whitespace - Use triple quotes
# 2) You want to get rid of excess whitespace - Use standard escape quotes method
##############################
# Triple quotes method
##############################
url = "http://example.com"
width = "100"
@stupidbodo
stupidbodo / Python_Combine_for_and_if_statement.py
Last active August 29, 2015 14:03
Python Combine "for" and "if" statement - Useful for loopping through JSON and find data using keys
users = [{
"user_id": 1,
"hobby": "Play Football"
}, {
"user_id": 1,
"hobby": "Play Basketball"
}]
# Given data above, if you want loop through the JSON and check if a specific user exist,
# you can do it with the code below.
  • Update HISTORY.rst
  • Update version number in my_project/__init__.py
  • Update version number in setup.py
  • Run the tests:
python setup.py test
tox
  • Commit the changes:
# Redirect STDOUT/STDERR into syslog
exec > >(logger -p user.info) 2> >(logger -p user.warn)
SSH agent forwarding is great. It allows you to ssh from one server to
another all the while using the ssh-agent running on your local
workstation. The benefit is you don't need to generate ssh key pairs
on the servers you are connecting to in order to hop around.
When you ssh to a remote machine the remote machine talks to your
local ssh-agent through the socket referenced by the SSH_AUTH_SOCK
environment variable.
So you the remote server you can do something like:
@stupidbodo
stupidbodo / non_blocking_gen_server_erlang.erl
Created October 25, 2013 15:52
Non-blocking gen_server for erlang
% non-blocking gen_server erlang
% By default handle_call is blocks the gen_server.
% Using a dispatcher-worker style, we can make
% handle_call non blocking
% The call below blocks
handle_call({get, Param}, From, State = #state{sock = Sock}) ->
Reply = do_time_consuming_remote_operation(Param, Sock),
{reply, Reply, State};
###############################################################################
# Copyright 2012 Jakub Jirutka. All rights reserved.
#
# "THE KOFOLA-WARE LICENSE" (Revision 1):
# Jakub Jirutka originally wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a Kofola in return. <jakub@jirutka.cz>
#
###############################################################################