Skip to content

Instantly share code, notes, and snippets.

View mohd-akram's full-sized avatar
💼
Looking for work

Mohamed Akram mohd-akram

💼
Looking for work
View GitHub Profile
@mohd-akram
mohd-akram / query_sort.py
Created January 3, 2013 17:28
An intuitive method to sort results in a few lines (Python)
"""This module sorts a list of results based on a query.
If there is an intersection between words in the query and a result, it
is placed near the beginning of the list (based on length of intersection).
If there is no intersection, the position of the query (substring) in the
result (string) is used for sorting. These results are placed toward the end
of the list.
"""
@mohd-akram
mohd-akram / schotter.c
Created September 13, 2018 15:45
Standalone version of LOLWUT from Redis 5.
/*
* Copyright (c) 2018, Salvatore Sanfilippo <antirez at gmail dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
@mohd-akram
mohd-akram / escape-strings.sh
Last active March 30, 2020 13:58
Escape POSIX (grep, sed, ex) regular expression patterns and replacements
#!/bin/sh
# Functions to escape strings for use in utilities like grep, sed and ex
# Examples
# grep "$(bre "$foo")" file
# sed "s/$(ptrn "$foo")/$(repl "$bar")/g" file
# echo "s/$(exptrn "$foo")/$(exrepl "$bar")/g | %p | q" | ex file
bre() {
@mohd-akram
mohd-akram / shallow-clone
Last active September 23, 2020 12:54
A script to shallow clone a repository up to the most recent version tag, or a specified tag/ref.
#!/bin/sh
# usage: shallow-clone <url> [ref]
url=$1
ref=$2
repo=$(basename "$url" .git)
head=$(git ls-remote "$url" HEAD | tail -n1 | cut -f1)
if [ ! "$head" ]; then exit 1; fi
if [ "$ref" ]; then
id=$(git ls-remote "$url" "$ref" "$ref^{}" | tail -n1 | cut -f1)
else
@mohd-akram
mohd-akram / ere2bre
Last active September 26, 2020 18:09
A script to convert an ERE (extended regular expression) to one or more BREs
#!/usr/bin/env awk -f
# usage: ere2bre <ere>
function bracket( \
pattern, i, \
c, c2, term, len, end \
) {
++i # skip opening bracket
if (substr(pattern, i, 1) == "]") ++i # skip right bracket
@mohd-akram
mohd-akram / generic
Last active October 31, 2020 10:41
Postfix configuration for sending email via mail/mailx using an email service (eg. Outlook, Gmail)
user name@example.com
@mohd-akram
mohd-akram / build-nginx-module.sh
Last active April 4, 2021 08:46
A script to build a dynamic Nginx module with the correct configure options.
#!/bin/sh
# A script to build a dynamic Nginx module with the correct configure options.
set -eu
set -o pipefail 2>/dev/null || :
dir="$1"
if [ ! "$dir" ]; then
@mohd-akram
mohd-akram / freebsd-vmware.sh
Created January 29, 2020 13:07
A script to install VMware tools, xfce and lightdm on a freshly installed FreeBSD system.
#!/bin/sh
username=$1
if [ ! "$username" ]; then
echo usage: $0 username >&2
exit 1
fi
if [ "`id -u`" != 0 ]; then
@mohd-akram
mohd-akram / minesweeper.py
Last active May 4, 2022 03:49
A command line version of Minesweeper in Python
"""A command line version of Minesweeper"""
import random
import re
import time
from string import ascii_lowercase
def setupgrid(gridsize, start, numberofmines):
emptygrid = [['0' for i in range(gridsize)] for i in range(gridsize)]
@mohd-akram
mohd-akram / clping
Last active May 10, 2022 10:18
A utility to ping different regions of cloud providers
#!/bin/sh
set -eu
set -o pipefail 2>/dev/null || :
aws_regions="\
af-south-1 ap-east-1 ap-northeast-1 ap-northeast-2 ap-northeast-3
ap-south-1 ap-southeast-1 ap-southeast-2 ca-central-1 eu-central-1 eu-north-1
eu-south-1 eu-west-1 eu-west-2 eu-west-3 me-south-1 sa-east-1 us-east-1
us-east-2 us-west-1 us-west-2