Skip to content

Instantly share code, notes, and snippets.

@tchen
tchen / main.py
Created May 31, 2017 04:59
Tornado JSON request body
# An example tornado request handler that handles both JSON POST request
# bodies and x-www-form-urlencoded POST bodies.
#
# The benefit of JSON request bodies are more complicated and potentially
# nested dict and list data types.
#
# One drawback to JSON request bodies is that arguments can come in
# different types, so handlers will need to perform additional checks.
# With x-www-form-urlencoded fields, all argument values are strings, if
# they exist.
@joshuadavidnelson
joshuadavidnelson / example-output.html
Last active January 22, 2024 12:42
Using WordPress responsive images for css background-image property, in-line styling
<style>
.header {
background-image: url(http://local.dev/wp-content/uploads/2016/04/image-300x151.png)
}
@media only screen and (min-width: 300px) {.header {
background-image: url(http://local.dev/wp-content/uploads/2016/04/image-768x386.png)
}}
@media only screen and (min-width: 768px) {.header {
background-image: url(http://local.dev/wp-content/uploads/2016/04/image-1024x515.png)
}}
@you-think-you-are-special
you-think-you-are-special / Singleton.js
Created February 18, 2015 10:52
ES6 Singleton example. Use: import Singleton from 'Singleton'; let instance = Singleton.instance;
'use strict';
/**
* Created by Alexander Litvinov
* Email: alexander@codeordie.ru
* May be freely distributed under the MIT license
*/
let singleton = Symbol();
let singletonEnforcer = Symbol();
@you-think-you-are-special
you-think-you-are-special / System.import.js
Created February 17, 2015 14:43
Import several modules in ES6 style
Promise.all(
['module1', 'module2', 'module3']
.map(x => System.import(x)))
.then(([module1, module2, module3]) => {
// Use module1, module2, module3
});
@gawen
gawen / replace.c
Created February 6, 2015 14:17
Rename/Replace function behind "os.replace" in Python 3
static PyObject *
internal_rename(PyObject *args, PyObject *kwargs, int is_replace)
{
char *function_name = is_replace ? "replace" : "rename";
path_t src;
path_t dst;
int src_dir_fd = DEFAULT_DIR_FD;
int dst_dir_fd = DEFAULT_DIR_FD;
int dir_fd_specified;
PyObject *return_value = NULL;
@abegong
abegong / gist:7608465
Last active July 25, 2018 00:44
Example of regex-based tornado handler. (Doesn't actually work, but you get the idea.) NB: the database here is mongo, not SQL
class OpenTemplateHandler(tornado.web.RequestHandler):
def get_current_user(self):
return json.loads(self.get_secure_cookie("myapp-user"))
def get(self, path):
self.render(path+'.html')
class ApiHandler(BaseHandler):
@tornado.gen.coroutine
def get(self, path):