Skip to content

Instantly share code, notes, and snippets.

View k4ml's full-sized avatar
🏠
Working from home

Kamal Mustafa k4ml

🏠
Working from home
View GitHub Profile
@k4ml
k4ml / gist:3606742
Created September 3, 2012 04:32
Python Inner function
# It's common in django to have function like:-
def send_sms(request):
form = SMSForm(request.POST)
if form.is_valid():
... some long operation
... some long operation
else:
... do something else
@k4ml
k4ml / _webfaction_setup.rst
Created September 17, 2012 02:58 — forked from ptone/_webfaction_setup.rst
setting up a stack on webfaction

Setting up Webfaction for modern Python deployment

last updated: 4/5/2011

note that this stuff is always a moving target, much of this has been cribbed and combined from various blog posts. Much of the information was out of date from those, and if it is more than a couple months after the last updated date above, consider some of this likely to now be out of date.

@k4ml
k4ml / gist:3805152
Created September 29, 2012 20:46
Using existing sqlite database in android with phonegap
// http://www.corporatezen.com/shipping_prepopulated_database_with_phonegap
package net.smach.halal;
import java.io.*;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import org.apache.cordova.*;
@k4ml
k4ml / gist:4066043
Created November 13, 2012 14:35
Python mock
class ForeignExchangeWebService(object):
def convert(self, value, currency):
# call web service
def bill_customer(order):
forex = ForeignExchangeWebService()
amount = forex.convert(order.amount, order.currency)
return amount
from mock import patch
@k4ml
k4ml / syspkg.py
Created November 15, 2012 18:55
Copy system packages into virtualenv
import os
import sys
import shutil
pkgname = sys.argv[1]
venv = sys.argv[2]
pyversion = '%s.%s' % (sys.version_info[0], sys.version_info[1])
try:
pkgobj = __import__(pkgname)
class MessageSender(object):
def __init__(self, user, recipient):
self.user = user
self.recipient = recipient
def send(self):
resp = API.send(recipient)
self.user.profile.points = F('points') - 1
self.user.profile.save()
self.user = User.objects.get(id=1)
@k4ml
k4ml / guard.py
Created December 31, 2012 02:16 — forked from anonymous/guard.py
email_obj = process_email(email_string)
if email_obj.has_data:
if email_obj.has_images or email_obj.has_unsupported_images:
process_images(email_obj.images)
else:
process_text(email_obj.text)
else:
raise InvalidEmail
@k4ml
k4ml / app.py
Last active April 29, 2021 15:27
Example of Bottle app with Django style request object as first parameter to request function handler and login_required decorator.
"""
Bottle (and also Flask) seem to favor global request object that when called within
a request function handler, will return request data that specific to the current request.
This is convenience since it free you from having to pass request object from function to
function as you would have in Django. Maybe I'm too used with Django approach but having request
object explicitly passed to your function allow me to correctly think how to structure my app.
It's not just one time when designing a solution, I come to dead end because I try to access request
data not from request context. It easy to fall into that because the request object available in the
global namespace.
@k4ml
k4ml / worker.py
Last active December 11, 2015 12:48
Ladang usage
import sys
import time
import ladang
def generate_csv(task_file):
print task_file
time.sleep(60) # generating file take a while
def main():
inotify = ladang.Ladang()
@k4ml
k4ml / test.php
Last active December 11, 2015 23:28
<?php if (isset($_POST['content'])): ?>
<b><?php echo $_POST['content']; exit;?></b>
<?php endif; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js">