Skip to content

Instantly share code, notes, and snippets.

View automactic's full-sized avatar

ChrisLi automactic

View GitHub Profile
import subprocess
import os
def change_install_name(lib_install_path: str):
def get_name_map(dir_path: str):
files = {}
for file in os.listdir(dir):
if file.endswith('.dylib') and not os.path.islink(dir_path+'/'+file):
files[file.split('.')[0]] = file
@automactic
automactic / send_data.py
Created July 28, 2017 18:36
python KSensor WebAPI request sample code
from datetime import datetime
import json
import urllib.request
def send_data(url: str, sid, pknnmb, timestamp: datetime, baseline, stddev, measures):
payload = {
'sid': str(sid),
'pknmb': str(pknnmb),
'timestamp': timestamp.strftime('%Y-%m-%d %H:%M:%S'),
@automactic
automactic / gist:3af58ee5ada0279518f802e31c601888
Last active July 28, 2017 18:35
Zimfarm useful commands
# stop all dispatcher containers
docker stop $(docker ps -q --filter "label=com.chrisshwli.zimfarm.name")
# run all dispatcher containers
docker-compose up --build -d
# run worker
docker build -t worker . && docker run -v /var/run/docker.sock:/var/run/docker.sock worker
docker run --link=zimfarm-worker-redis:redis openzim/mwoffliner mwoffliner --mwUrl=https://en.wikipedia.org/ --adminEmail=foo@bar.net
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Observable, Subscription } from 'rxjs/Rx';
import { Task } from '../../model/task';
@Component({
selector: 'tasks',
templateUrl: './list-tasks.component.html',
styleUrls: ['./list-tasks.component.css']
})
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Observable, Subscription } from 'rxjs/Rx';
import { Task } from '../../model/task';
@Component({
selector: 'tasks',
templateUrl: './list-tasks.component.html',
styleUrls: ['./list-tasks.component.css']
})
@automactic
automactic / proposal.md
Last active April 28, 2017 04:55
Zimfarm task proposal
  • Forget about the concept of templates
  • We create tasks directly from a json uploaded by admin, which is an array of task objects. The task object looks like below.
  • Every time admin upload a new json, all executing tasks continues, all already scheduled tasks get cancelled, system is populated with tasks from new json. (We can further discuss the strategy used here)
  • Database only keeps task execution history, including what is the command being executed, when task started / finished, which worker executed it, and if success or not
{
    "id": "12345-67890-qwerty",
    "command": "./build_zim.sh -args",
    "scheduling": {
 "repeat": true,
@automactic
automactic / question.md
Last active March 21, 2017 21:29
A question

I have a db connection, which need configuration. I want to use this connection in all flask apis. So normally I would do it like this:

app = Flask(__name__)
db = Database(config_obj)

@app.route('/')
def hello():
    # a lot of code
    username = db.get_data()
 return 'Hello, {}!'.format(username)
@automactic
automactic / gist:37c72ee3e3f85943a4d07915007d83cd
Last active March 21, 2017 13:28
XOR checksum of [0, n]

Input:

val = 0
for i in range(0, 100):
    val ^= i
    print("{}, {}".format(i, val))

Output:

@automactic
automactic / gist.md
Last active March 3, 2017 15:49
Celery Startup

start RabbitMQ rabbitmq-server

start Celery with detail task info celery -A api.celery worker --loglevel=info

Celery flowers celery -A api.celery flower

flask & mongo

@automactic
automactic / UICollectionView+FetchedResultControllerDelegate.swift
Last active January 17, 2017 16:06
Swift 3 UICollectionView FetchedResultControllerDelegate
class Controller: UIViewController, NSFetchedResultsControllerDelegate {
private var closures = [() -> Void]()
let managedObjectContext = AppDelegate.persistentContainer.viewContext
lazy var fetchedResultController: NSFetchedResultsController<Article> = {
}()
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .insert: