Skip to content

Instantly share code, notes, and snippets.

Mahmoud Abduljawad mahmoudajawad

Block or report user

Report or block mahmoudajawad

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@mahmoudajawad
mahmoudajawad / class_singleton.py
Last active Apr 1, 2018
Python (3.x) singleton metaclass. It allows manipulation of child classes, as well as converting all class funcs to classmethod's.
View class_singleton.py
class ClassSingleton(type):
def __new__(cls, cls_name, bases, attrs):
for name, attr in attrs.items():
if callable(attr):
attrs[name] = classmethod(attr)
return type.__new__(cls, cls_name, bases, attrs)
def __init__(cls, cls_name, bases, attrs):
cls.singleton()
@mahmoudajawad
mahmoudajawad / parse_multipart.py
Last active May 23, 2018
Alternative parse_multipart function (to replace python cgi default implementation) that can read additional attributes.
View parse_multipart.py
def parse_multipart(rfile, boundary):
boundary = b'--' + boundary
rfile = re.compile(boundary+b'(?:\r\n|\n)').split(rfile.replace(b'\r\n'+boundary+b'--', b'').replace(b'\n'+boundary+b'--', b''))
pattern = b'Content-Disposition: form-data; name="?([\$a-zA-Z0-9\.\-_\\\:]+)"?(?:; filename="?([a-zA-Z0-9\.\-_]+)"?(?:\r\n|\n)Content-Type: ([a-zA-Z0-9\.\-_]+\/[a-zA-Z0-9\.\-_]+)(?:\r\n|\n)|(?:\r\n|\n))(?:\r\n|\n)(.+)'
multipart = {}
for part in rfile:
try:
multipart_key = re.match(pattern, part, re.DOTALL).group(1)
multipart[multipart_key] = [group for group in re.match(pattern, part, re.DOTALL).groups()]
if multipart[multipart_key][3][-2] == 13 and multipart[multipart_key][3][-1] == 10:
You can’t perform that action at this time.