Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python2
import os
import tempfile
import sqlite3
class DBExportor(object):
def __init__(self, db=None):
if not db:
db = self.pull_db()
@oneyoung
oneyoung / max-seqs.py
Created April 18, 2016 13:28
Given an unsorted array of integers, find the length of longest increasing subsequence. For example,
Given [10, 9, 2, 5, 3, 7, 101, 18],
The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length.
array = [10, 9, 2, 5, 3, 7, 101, 18, 31, 25, 68, 45, 12]
# O(n^2)
def longest_seqs(seqs):
l = []
for i in range(len(seqs)):
l.append(max([l[j] for j in range(i) if l[j][-1] < seqs[i]] or [[]],
key=len) + [seqs[i]])
return len(max(l, key=len))
@oneyoung
oneyoung / service.sh
Last active March 1, 2016 15:47
/etc/init.d/ service
#!/bin/sh
### BEGIN INIT INFO
# Provides:
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
@oneyoung
oneyoung / app.py
Last active February 24, 2016 04:12
Falcon framework: static file handler for debug
class StaticFileRes(object):
def __init__(self, static_root='.'):
self.static_root = os.path.abspath(static_root)
def on_get(self, req, resp):
path = req.path
if path == '/': # handle default page
path = '/index.html'
full_path = self.static_root + path
if os.path.isfile(full_path):
@oneyoung
oneyoung / falcon_urlreverse.py
Last active January 18, 2021 17:16
A customize Falcon Router that support Django like url reverse
from falcon.routing import DefaultRouter
class ReverseRouter(DefaultRouter):
url_map = {}
# override add_route to add our map
def add_route(self, uri_template, method_map, resource, name=None):
if name:
self.url_map[name] = uri_template
@oneyoung
oneyoung / ardino_cos_curve.c
Last active August 28, 2015 02:18
cos curve for stepper monitor
void cos_curve(float a_max, float w_start, float w_max, int total, int pin) {
#define STATE_ACC 0
#define STATE_CONST 1
#define STATE_DEC 2
if (w_start > w_max) w_start = w_max;
float w_i = w_start;
float t_i = 0.000001;
int count = 0;
@oneyoung
oneyoung / s_curve.c
Last active August 29, 2015 14:26
步进电机S曲线生成
/*
amax: max of velocity accelerator
aa: accelerator of v accelerator
vm: max of velocity
s: total lenght of distance
pin: pluse pin of stepper controller
In order to improve performance, let's re-define the unit system:
time -- us
length -- step
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class DownloadFile {
public static void download(String fileName, String url) throws IOException {
@oneyoung
oneyoung / onlinemapsources.xml
Last active February 4, 2024 09:14
Google Maps (google.cn) for OruxMaps, copy to YOUR_SDCARD_PATH/oruxmaps/mapfiles/
<?xml version="1.0" encoding="utf-8"?>
<onlinemapsources>
<onlinemapsource uid="0">
<name>Google Maps</name>
<url><![CDATA[http://mt{$s}.google.cn/vt/lyrs=m@121&hl={$l}&x={$x}&y={$y}&z={$z}]]></url>
<minzoom>0</minzoom>
<maxzoom>19</maxzoom>
<projection>MERCATORESFERICA</projection>
<servers>0,1,2,3</servers>
<httpparam name=""></httpparam>
@oneyoung
oneyoung / Input.java
Last active January 3, 2023 09:42
Android Input control
/* stolen from input command
* but have some limitation:
* since it can use "android.permission.INJECT_EVENTS", (only system apps can do that)
* Inject event to other apps will raise a exception of no permission
*/
import android.hardware.input.InputManager;
import android.os.SystemClock;
import android.util.Log;
import android.view.InputDevice;