Skip to content

Instantly share code, notes, and snippets.

@gurunars
gurunars / get_named_css_colors_for_android.py
Last active June 26, 2021 22:47
Fetches css color specs from https://www.w3schools.com/cssref/css_colors.asp and prints an Android color resource file content. Depends on BeautifulSoup.
"""
requests
beautifulsoup4
"""
import requests
from bs4 import BeautifulSoup
def to_rgb(hex):
@gurunars
gurunars / fibonacci.js
Created January 6, 2016 10:38
Fibonacci - non recursive solution
fibonacci : function(n) {
// f(0) = 0
// f(1) = 1
// f(n) = f(n-1) + f(n-2)
if (n === 0) {
return 0;
}
if (n === 1) {
return 1;
}
@gurunars
gurunars / merge_sort.py
Created February 16, 2016 14:19
Merge sort in Python
import random
my_randoms = random.sample(xrange(100), 13)
def merge(first_half, second_half):
if len(first_half) == 0:
return second_half
elif len(second_half) == 0:
return first_half
@gurunars
gurunars / wsgi.conf
Last active April 13, 2016 10:19
Apache Python wsgi app config
<VirtualHost *:80>
ServerName HOSTNAME.DOMAIN
WSGIDaemonProcess threads=5
WSGIScriptAlias / /PATH/TO/WSGI/DEFINITION/webapp.wsgi
<Directory /PATH/TO/WSGI/DEFINITION>
Order deny,allow
Require all granted
</Directory>
@gurunars
gurunars / main.py
Last active October 6, 2022 15:06
Regexp based validation for argparse arguments
import argparse
import re
class Validator(object):
def __init__(self, pattern):
self._pattern = re.compile(pattern)
def __call__(self, value):
@gurunars
gurunars / ask_confirmation.py
Created May 4, 2016 13:43
Ask for confirmation in Python
def confirm():
"""
Ask user to enter Y or N (case-insensitive).
:return: True if the answer is Y.
:rtype: bool
"""
answer = ""
while answer not in ["y", "n"]:
answer = raw_input("OK to push to continue [Y/N]? ").lower()
@gurunars
gurunars / find_latest_ami_name.py
Last active October 30, 2020 01:41
AWS lambda function to find the latest Amazon AMI
"""
Publish the function to S3:
cd $DIR_WITH_THIS_SCRIPT
zip find_latest_ami_name.zip find_latest_ami_name.py
aws s3 cp find_latest_ami_name.zip s3://$YOUR_S3_BUCKET/find_latest_ami_name.zip
"""
import json
@gurunars
gurunars / path_finder.py
Last active October 16, 2016 19:14
Djikstra algorithm implementation in Python
"""
Glossary used in the functions below:
Node can be virtually any hashable datatype.
:param start: starting node
:param end: ending node
:param weighted_graph: {"node1": {"node2": "weight", ...}, ...}
"""
@gurunars
gurunars / sample-program
Last active July 12, 2016 11:59
Sample Linux service config for init.d
#!/bin/sh
# Put to /usr/bin/sample-program
sleep 600 &
PID="$!"
# Trap is required to kill all child processes when the service is stopped
trap "kill $PID" exit INT TERM KILL TERM
@gurunars
gurunars / base_test.py
Last active May 26, 2017 10:52
Python unit test with multipatching
from abc import ABCMeta, abstractproperty
import unittest
import mock
class BaseTest(unittest.TestCase):
__metaclass__ = ABCMeta