Skip to content

Instantly share code, notes, and snippets.

View yxkelan's full-sized avatar

Yuan Xie yxkelan

  • San Francisco, CA
View GitHub Profile
@yxkelan
yxkelan / error_list_msg.py
Created November 2, 2012 06:06
Customizing the error list format Django
# In form.py
from django.forms.util import ErrorList
from django.utils.safestring import mark_safe
class ErrorListMsg(ErrorList):
def __unicode__(self):
return self.as_msg()
def as_msg(self):
if not self: return u''
@yxkelan
yxkelan / limit_file_size.py
Created November 2, 2012 06:15
Limit the uploaded file size Django
#In form.py
#The original source for this : http://djangosnippets.org/snippets/1303/
from django import forms
from django.template.defaultfilters import filesizeformat
from django.utils.translation import ugettext_lazy as _
# 2.5MB - 2621440
# 5MB - 5242880
# 10MB - 10485760
@yxkelan
yxkelan / check_time_format.js
Created November 3, 2012 06:05
Check time format and transfer 12-hour to 24-hour format
/* The original source: http://www.the-art-of-web.com/javascript/validate-date/ */
//obj is JQuery object
// return : true -- time invalid, false -- time valid
function checkTime(obj){
var errorFlag = false;
re = /^(\d{1,2}):(\d{2})(:00)?([apAP][mM])?$/;
@yxkelan
yxkelan / gist:5022789
Last active December 14, 2015 03:39
Using HTML5 <details> and <summary> to function as JS toggle.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<style>
summary::-webkit-details-marker{
color:green;
}
</style>
@yxkelan
yxkelan / gist:5022822
Created February 24, 2013 06:05
HTML5Shim --- allowing IE 6,7,8 to use HTML5 elements. See more on: http://code.google.com/p/html5shim/
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<!--[if lt IE 9]>
<script src="dist/html5shiv.js"></script>
<![endif]-->
<title>HTML5 Shim</title>
<style>
article{
@yxkelan
yxkelan / gist:5053016
Created February 27, 2013 23:55
Clear float example, creating a container, which includes 2 same column. See more about clear float: http://nicolasgallagher.com/micro-clearfix-hack/
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<style>
.container{
width:100%;
box-sizing:border-box;
@yxkelan
yxkelan / gist:5095821
Last active December 14, 2015 13:48
The assertion, from "Secrets of JavaScript Ninja". 1, Simple test assertion function. 2, Test groups. 3, Asynchronous testing.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<style>
li.pass{
color:green;
}
li.fail{
@yxkelan
yxkelan / gist:5114484
Last active December 14, 2015 16:19
JavaScript Scope !!! * scope are declared as function, not as {} block. * function declaration will be hoisted within scope but variables can't.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<style>
.pass{color:green;}
.fail{color:red;}
</style>
</head>
@yxkelan
yxkelan / gist:5157158
Created March 13, 2013 22:49
Style a select menu with CSS. The original one is on http://www.cssflow.com/snippets/dark-and-light-dropdown-lists
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en"> <!--<![endif]-->
</head>
@yxkelan
yxkelan / humansize.py
Last active December 15, 2015 18:48
humansize.py , from Dive into Python 3
SUFFIXES = {
1000: ['KB','MB','GB','TB','PB','EB','ZB','YB'],
1024: ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB']
}
def approximate_size(size, a_kilobyte_is_1024_bytes = True):
'''Convert a file size to human-readable form.
Keyword arguments:
size --- file size in bytes