Skip to content

Instantly share code, notes, and snippets.

View mahmoud's full-sized avatar
Back on the grid!

Mahmoud Hashemi mahmoud

Back on the grid!
View GitHub Profile
function nextID($id) {
$avail_chars = $this->config->item('link_id_chars'); //a string, no dupe chars plz.
if($id === '') {
return $avail_chars[0];
} else if($id[strlen($id)-1] === $avail_chars[strlen($avail_chars)-1]) {
return $this->nextID(substr($id, 0, -1)).$avail_chars[0];
} else {
return substr($id, 0, -1).$avail_chars[strrpos($avail_chars,$id[strlen($id)-1])+1];
}
}
Importer.py
/ftp/public
Traceback (most recent call last):
File "Importer.py", line 65, in <module>
importer.import_path(path)
File "Importer.py", line 56, in import_path
self.import_path(os.path.join(path,entry))
File "Importer.py", line 56, in import_path
self.import_path(os.path.join(path,entry))
File "Importer.py", line 56, in import_path
@mahmoud
mahmoud / mattslist.py
Created May 10, 2011 08:36
this is terrible, but it'll probably work
# actually not python, just pseudocode highlighted like python.
if( len(ints) < 2 ) # if the list is short, just exit
return
for (i=1; i < len(ints)-1; ++i): # for each of the list entries, from the second to the second to last
if ints[i] == ints[i-1] or ints[i] == ints[i+1]: # if the entry to the left or entry to the right is equal to the current one
ints[i] = ints[i] + 1 # add one to the current one
i = i - 1 # set the cursor back one, because continuing/looping again will cause it to move forward
continue # repeat the loop to make sure our +1 change was ok.
@mahmoud
mahmoud / gist:1044311
Created June 24, 2011 06:11
python dict destruction
import random
PyDict_MINSIZE = 8 # from dictobject.h
class DictDestroyer(object):
def __init__(self):
self.hash = 1
def __hash__(self):
self.hash = random.randint(0, 2**31)
return self.hash
@mahmoud
mahmoud / vbscript_list_min.vbs
Created July 18, 2011 06:44
VBScript List creation and Min findin'
(for fixed sized inputs, so not terribly useful. It's VBScript, so also not terribly useful. Also, haven't run this.)
Dim myArray(4)
myArray(0) = A
myArray(1) = B
myArray(2) = C
myArray(3) = D
myArray(4) = E
Function MinN( vals )
@mahmoud
mahmoud / gist:1169652
Created August 25, 2011 00:21
range() and xrange() limits
>>> import sys
>>> xrange(sys.maxint)
xrange(9223372036854775807)
>>> xrange(sys.maxint+1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long
>>> range(sys.maxint+1)
Traceback (most recent call last):
@mahmoud
mahmoud / pypy_exc.py
Created October 14, 2011 08:12
pypy Exception games
>>>> class MyException(Exception):
.... def pr(self):
.... print 'hi'
>>>> __builtins__.FutureWarning = MyException
>>>> try:
.... raise FutureWarning
.... except FutureWarning as fw:
.... fw.pr()
....
hi
@mahmoud
mahmoud / subsets.py
Created October 28, 2011 05:12
Some subset fun.
#!/usr/bin/env python
from itertools import combinations, chain
fun_list = ['a','b','c']
print "\nThe FB way"
def subsets(ins, out=None):
if out is None:
out = ['']
if len(ins) == 0:
@mahmoud
mahmoud / profiled_generate.txt
Created January 25, 2012 07:12
PythonDoesBlog profiler output
1245683 function calls (1214448 primitive calls) in 6.210 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
70411 0.314 0.000 1.316 0.000 re.py:228(_compile)
78966 0.222 0.000 0.222 0.000 {method 'match' of '_sre.SRE_Pattern' objects}
22563 0.203 0.000 0.375 0.000 lexer.py:64(match_reg)
10233 0.186 0.000 1.007 0.000 pygen.py:55(writeline)
19780 0.169 0.000 0.541 0.000 statemachine.py:694(make_transition)
@mahmoud
mahmoud / pubsubhubbub_ugh.txt
Created February 7, 2012 06:56
superfeedr derp stuff
# subscribe
curl http://pythondoeswhat.superfeedr.com/ --basic -d"hub.mode=subscribe" -d"hub.verify=sync" -d"hub.callback=http://makuro.org:7777/callback" -d"hub.topic=http://www.pythondoeswhat.com/feed/rss.xml" -D-
# ping
curl -X POST http://pythondoeswhat.superfeedr.com -d"hub.mode=publish" -d"hub.url=http://www.pythondoeswhat.com/feed/rss.xml" -D-
# unsubscribe
curl http://pythondoeswhat.superfeedr.com/ --basic -d"hub.mode=unsubscribe" -d"hub.verify=sync" -d"hub.callback=http://makuro.org:7777/callback" -d"hub.topic=http://www.pythondoeswhat.com/feed/rss.xml" -D-