python

[python] pipeline design

Pipeline framework like assembly line in manufactory.

Image that:

  • A kind product consists of several steps to produce
  • Each step only has one worker
    • A worker can only handle one step
    • A worker can't work in parallel, must handle one by one.
    • Once the worker finished this step, he pass the product to next worker(step)
  • There are a great number of product for manufactorying.
    • In order to improve efficiency, each woker must be in full load.

Falcon: Django-like urlreverse router

The default Falcon router don't support urlreverse, but fortunately it allows us to use a costomized router.

Override the add_route, adding a aditional params name to specified url template name.

Note: Falcon version should >=0.4 to support this feature.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
        DefaultRouter.add_route(self, uri_template, method_map, resource)

    def reverse(self, _name, **kwargs):
        '''
        reverse url
        '''
        assert _name in self.url_map, "url name: %s not in url map" % _name
        url_tmpl = self.url_map.get(_name)
        return url_tmpl.format(**kwargs)

[python] os.system output out-of-order using shell pipe

t.py as below:

1
2
3
4
5
import os

print "### before ps"
os.system("ps")
print "### after ps"

logical is quite simple, if we directly run it: python t.py, output as below, nothing special:

1
2
3
4
5
6
7
### before ps
  PID TTY          TIME CMD
 1619 pts/9    00:00:00 python
 1620 pts/9    00:00:00 sh
 1621 pts/9    00:00:00 ps
18701 pts/9    00:00:00 bash
### after ps

导出Nook Reader笔记

Nook 内置的Reader对epub和文字版的pdf支持还是比较好的(中文就坑爹了, 要自己换个字体进去), 可以直接进行文字高亮操作, 并可以标注笔记.

此脚本可以直接将笔记导出成txt格式, 并保留页码信息, 方便反查, 适用于对于整理笔记时候, 不想再一个一个字打的懒人 XD

导出格式:

  • 文件名: 书名.txt
  • 导出的笔记按照页码排序
  • 每条记录格式: -页码- 高亮文本[标注的笔记(如果有的话)]

要求:

  • device: Nook已经root, 并且有adb
  • PC: adb, 并且可以识别Nook设备.