Skip to content

Instantly share code, notes, and snippets.

@randy-ran
randy-ran / torndb-examples.py
Last active November 17, 2017 16:11 — forked from qpleple/torndb-examples.py
[torndb-examples] torndb-examples #tornado
# Torndb is a very thin wrapper around MySQLdb that makes it even easier to use MySQL.
# Because it is very light, one can just go through the one-file python source
# to learn how to use it.
# Installation: pip install torndb
# Official doc: http://torndb.readthedocs.org/en/latest/
# Source: https://github.com/bdarnell/torndb/blob/master/torndb.py
from torndb import Connection
// Example:
JavaScript.load("/javascripts/something.js");
// With callback (that’s the good thing):
JavaScript.load("http://www.someawesomedomain.com/api.js", function() {
API.use(); // or whatever api.js provides ...
});

Simple Starting Point

创建 Model

var Person = Backbone.Model.extend();
class UpdateTwitterHandler(tornado.web.RequestHandler, tornado.auth.TwitterMixin):
@tornado.web.asynchronous
@tornado.gen.engine
def post(self):
access_token = self.get_current_user_access_token()
result = yield tornado.gen.Task(self.twitter_request,
"/account/update_profile_background_image",
image=open('new_image.png', 'rb').read(),
access_token=access_token)
//一种用普通的字符串的方法,另外一种用正则,还是正则的扩展性高一点。。
//方法一:字符串方法
function ifRepeat(str) {
var ss = str;
for (var i = 0; i < ss.length; i++) {
var cha = ss.charAt(i);
var sub = ss.substring(0,i)+ss.substring(i+1);
if (sub.indexOf(cha)!=-1) {
console.log("有重复");
@randy-ran
randy-ran / handlers.py
Last active January 8, 2018 08:43 — forked from alejandrobernardis/handlers.py
[下载文件] 读取文件然后下载 #torando
class AdminFileHandler(BaseHandler):
@authenticated
def get(self, file_name):
_file_dir = os.path.abspath("")+"/my/path/downloads"
_file_path = "%s/%s" % (_file_dir, file_name)
if not file_name or not os.path.exists(_file_path):
raise HTTPError(404)
self.set_header('Content-Type', 'application/force-download')
self.set_header('Content-Disposition', 'attachment; filename=%s' % file_name)
from time import sleep
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.web import Application, asynchronous, RequestHandler
from multiprocessing.pool import ThreadPool
_workers = ThreadPool(10)
def run_background(func, callback, args=(), kwds={}):
def _callback(result):
@randy-ran
randy-ran / new_task.py
Last active August 29, 2015 14:25 — forked from swinton/new_task.py
#!/usr/bin/env python
import sys
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello', durable=True)
@randy-ran
randy-ran / 00.howto_install_phantomjs.md
Created December 31, 2015 01:44 — forked from julionc/00.howto_install_phantomjs.md
How to install PhantomJS on Debian/Ubuntu

How to install PhantomJS on Ubuntu

Version: 1.9.8

Platform: x86_64

First, install or update to the latest system software.

sudo apt-get update
sudo apt-get install build-essential chrpath libssl-dev libxft-dev
@randy-ran
randy-ran / compild.py
Created March 28, 2016 08:07 — forked from alex-1q84/compild.py
使用python的sched和Timer执行定时任务
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sched, time
from threading import Thread, Timer
import subprocess
s = sched.scheduler(time.time, time.sleep)
class Job(Thread):