Skip to content

Instantly share code, notes, and snippets.

@mtayseer
mtayseer / ghome_azan.py
Created June 9, 2019 12:51
A cron job to cast islamic azan sound to a Google home device
import requests, googlehomepush, time, datetime, json, os, codecs, logging
custom_alarms = {'07:50': 'You have to leave now',
'07:30': 'You have 20 minutes'}
# We don't want to read prayer times from the web all the time, so we cache it
# for the day
current_dir = os.path.abspath(os.path.dirname(__file__))
today = datetime.datetime.today()
today_file = os.path.join(current_dir, today.strftime('%d-%m-%Y.prayer_times'))
@mtayseer
mtayseer / are_anagrams.py
Created March 25, 2015 00:38
Check if two statements are anagrams
# How to run?
# > python are_anagrams.py hello lolhe
# True
from collections import Counter
import sys
print(Counter(sys.argv[1]) == Counter(sys.argv[2]))
import math
def string_norm(s):
return math.sqrt(sum(ord(c) ** 2 for c in s if c != ' '))
def anagram_detection(s1,s2):
return string_norm(s1) == string_norm(s2)
s1 = input("Please enter first string: ").lower()
s2 = input("Please enter second string: ").lower()
#! /bin/python
# 7 minutes Ansible module to list groups in inventory (Python version) :D
# You can print output like "pretty-print" outside Ansible by using:
#./listgroups /path/to/ansible.cfg | python -m json.tool
import sys
import re
import json
#get hosts inventory from ansible.cfg file.
@mtayseer
mtayseer / host_template_loader.py
Last active February 16, 2017 13:19
Load templates (even included & extended templates) based on Mezzanine HOST_THEME
import os
import sys
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.template.base import TemplateDoesNotExist
from django.template.loaders.app_directories import Loader as BaseLoader
from django.utils._os import safe_join
from django.utils.importlib import import_module
from django.utils import six
from mezzanine.core.request import current_request
@mtayseer
mtayseer / print_pkg_deps.py
Last active August 29, 2015 14:01
Print a list of all installed Python packages & their dependencies
import pkg_resources, pprint
requires = {p.key: [r.key for r in p.requires()] for p in pkg_resources.working_set}
def graph(pkg):
if not requires[pkg]:
return {pkg: {}}
return {pkg: {k: v for p in requires[pkg] for k, v in graph(p).items() }}
key_mapping = [
('abc', '2'),
('def', '3'),
('ghi', '4'),
('jkl', '5'),
('mno', '6'),
('pqrs', '7'),
('tuv', '8'),
('wxyz', '9'),
(' ', '0'),
@mtayseer
mtayseer / euler_38.py
Created March 16, 2014 16:27
Solve project Euler problem #38 http://projecteuler.net/problem=38
# Analysis:
#
# Let's try to find the optimal range. We know that
# 1. 918273645 is a 1-9 pandigital & is a concatenated product, which means that this number is either the largest or
# below it, so we search from this number up
# 2. Then we find that the number we're trying to search for should start with 9, e.g. 9, 91, 945, etc
# 3. len(concat_product(x=90, n=3)) = 8, len(concat_product(x=90, n=4)) = 11. This range won't work.
# 4. len(concat_product(x=9000, n=2)) = 9. This is where we should start.
# 5. Taking #1 in consideration, we will start from 9182 till 9999
# 6. To get the pandigital number from a this range, we can multiply by n * 100000 + n * 2 => n * 100002
@mtayseer
mtayseer / PrintScreen.ahk
Created March 16, 2014 14:20
AHK script to show Microsoft snipping tool on PrintScreen. Windows only.
; Show Microsoft snipping tool on PrintScreen
#SingleInstance force
PRINTSCREEN::
Run snippingtool
return
@mtayseer
mtayseer / kill_portal.sh
Created March 16, 2014 09:47
One-liner which kills a Unix process given its name
# Kill a process, given part of its name
#
# How I'm doing it:-
#
# 1. Get a list of all processes
# 2. Grep for any process where "portal" is part of the
# command line used to launch it
# 3. I will get 2 processes: 1 of them is what I'm searching for &
# the other is just this grep command I'm running. I remove it by
# removing anything with "grep" in its command line