Skip to content

Instantly share code, notes, and snippets.

View ishahid's full-sized avatar

Imran Shahid ishahid

View GitHub Profile
@ishahid
ishahid / Hash.cs
Last active December 13, 2015 17:38 — forked from inogo/Hashes.cs
MD5 and SHA1 hash calculation tool.
using System;
using System.Text;
using System.Security.Cryptography;
static class Hash
{
public static string SHA1(string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
var sha1 = new SHA1CryptoServiceProvider();
@ishahid
ishahid / timestampedmodel.py
Last active December 24, 2015 06:49
An abstract base class model for Django that provides self-updating 'created' and 'modified' fields.
from django.db import models
class TimeStampedModel(models.Model):
"""
An abstract base class model that provides self-updating 'created'
and 'modified' fields.
"""
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
@ishahid
ishahid / reserved_words.py
Last active February 27, 2017 23:47
Reserved words to exclude when creating usernames (or any other resource).
# A list of possible reserved words
reserved_words = [
# Companies
'amazon', 'apple', 'atlassian', 'canonical', 'facebook', 'github',
'google', 'htc', 'huawei', 'ibm', 'microsoft', 'mozilla', 'naughtydog',
'nintendo', 'nokia', 'rim', 'samsung', 'sony', 'toshiba', 'twitter',
'ubisoft', 'wikipedia', 'xref', 'yahoo',
# Operating systems
@ishahid
ishahid / date_diff.php
Created January 15, 2014 02:16
Find difference between two dates in days. PHP 5.2.x.
<?php
/**
* Find difference between two dates in days. PHP 5.2.x.
*/
$d1 = date('Y-m-d', strtotime('2013-06-13 12:27:43'));
$d2 = date("Y-m-d");
echo diff($d1, $d2);
function diff($date1, $date2) {
@ishahid
ishahid / http_headers.php
Created January 15, 2014 02:19
Display HTTP headers in PHP.
<?php
$http_client_ip = '';
$http_x_forwarded_for = '';
$remote_addr = $_SERVER['REMOTE_ADDR'];
$remote_host = '';
$remote_port = $_SERVER['REMOTE_PORT'];
$remote_user = '';
$redirect_remote_user = '';
$http_user_agent = $_SERVER['HTTP_USER_AGENT'];
@ishahid
ishahid / hash.py
Created January 15, 2014 02:28
Hash calculation tool.
#!/usr/bin/env python
import os, sys
import hashlib
def usage(full=False):
msg = ''
if full:
msg += 'Computes hash of a string.' + os.linesep
@ishahid
ishahid / soap.php
Created January 15, 2014 02:31
SOAP service call in PHP.
<?php
try {
$options = array(
'soap_version'=>SOAP_1_2,
'exceptions'=>true,
'trace'=>1,
'cache_wsdl'=>WSDL_CACHE_NONE
);
$client = new SoapClient('http://soap.service-provider.com/endpoint.asmx?WSDL', $options);
}
@ishahid
ishahid / recursive_sum.py
Created January 15, 2014 02:39
Recursive sum of all elements of a given list.
def sum(args):
""" Returns sum of all elements of a given list, recursively """
if len(args) == 0:
return 0
return args[0] + sum(args[1:])
if __name__ == "__main__":
@ishahid
ishahid / num2str.py
Created January 15, 2014 02:41
Convert numbers to their string representation.
import re
units = {
0: 'Zero', 1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine'
}
tens = {
10: 'Ten', 11: 'Eleven', 12: 'Twelve', 13: 'Thirteen', 14: 'Fourteen', 15: 'Fifteen', 16: 'Sixteen',
17: 'Seventeen', 18: 'Eighteen', 19: 'Nineteen', 20: 'Twenty', 30: 'Thirty', 40: 'Forty', 50: 'Fifty',
60: 'Sixty', 70: 'Seventy', 80: 'Eighty', 90: 'Ninety'
}
@ishahid
ishahid / ugly_number.py
Created January 15, 2014 02:44
O(N) solution to find Nth ugly number.
def ugly_number(n):
""" Returns Nth ugly number """
assert n > 0, 'The argument must be greater than zero.'
list = [0]*n
i2, i3, i5 = 0, 0, 0
w2, w3, w5 = 2, 3, 5
w, list[0] = 1, 1