Skip to content

Instantly share code, notes, and snippets.

View lihuanshuai's full-sized avatar
:octocat:
rusting

Li Huanshuai lihuanshuai

:octocat:
rusting
View GitHub Profile
@lihuanshuai
lihuanshuai / test_async.py
Created September 2, 2021 08:21
Code to test asyncio
import asyncio
import inspect
async def test_coro():
await asyncio.sleep(1)
print('coro')
return 1
@lihuanshuai
lihuanshuai / mil.py
Created July 19, 2021 09:18
Mil Simulation
from __future__ import print_function
def get_mil(d, v0, ds):
t = d * 1.0 / v0
mil = (5 * t * t - ds) * 1000 / d
return int(mil * 10 + 0.5) / 10.0
cols = [600, 700, 800, 900, 1000, 1100, 1200]
header = [''] + cols
print(*header, sep='\t')
from collections import OrderedDict
class LRUCache(object):
def __init__(self, size=5):
self.size = size
self.cache = OrderedDict()
def get(self, key):
if key in self.cache:
@lihuanshuai
lihuanshuai / oct_to_hex.py
Last active July 12, 2019 03:14
script for fixing python problem
import re
regex = re.compile(r'\\(\d+)'
def repl(m):
return r'\x' + hex(int(m.group(1), 8)).replace('0x', r'').zfill(2)
lines = vscode.editor.lines
lines = [line.decode() for line in lines]
lines = [regex.sub(repl, line) for line in lines]
@lihuanshuai
lihuanshuai / remove_dup_onetab_items.js
Last active January 24, 2019 03:07
remove duplicate onetab items
function get_close_nodes_for_duplicate_onetab_items() {
let closeNodes = [];
let cache = {};
let selector = '#contentAreaDiv > div > div:nth-child(2) > div > div:nth-child(2)';
let nodes = Array.from(document.querySelectorAll(selector));
for (let node of nodes) {
let linkNode = node.querySelector('a.clickable');
let closeNode = node.querySelector('img:nth-child(3)');
let url = linkNode.getAttribute('href');
if(cache[url]) {
@lihuanshuai
lihuanshuai / remove-padding.js
Created January 20, 2018 03:30
webtorrent toggle selection
document.querySelectorAll('.torrent-details tr').forEach(function (node) {
var name = node.querySelector('.col-name').textContent;
if (name.match(/^_____padding_file_/)) {
console.log(name);
node.querySelector('.deselect-file').click();
}
})
@lihuanshuai
lihuanshuai / remove-padding.js
Created January 20, 2018 03:30
webtorrent toggle selection
document.querySelectorAll('.torrent-details tr').forEach(function (node) {
var name = node.querySelector('.col-name').textContent;
if (name.match(/^_____padding_file_/)) {
console.log(name);
node.querySelector('.deselect-file').click();
}
})
from itertools import tee, islice as slice, chain, izip as zip
def previous_and_next(iterable, fill_value=None):
prevs, items, nexts = tee(iterable, 3)
prevs = chain([fill_value], prevs)
nexts = chain(slice(nexts, 1, None), [fill_value])
return zip(prevs, items, nexts)
import tables as tb
from numpy import array
from scipy import sparse
def store_sparse_mat(path, name, m, separator='__'):
if (m.__class__ not in [sparse.csr.csr_matrix, sparse.csc.csc_matrix]):
raise TypeError("This code only works for csr/csc matrices")
with tb.openFile(path, 'a') as f:
for par in ('data', 'indices', 'indptr', 'shape'):
@lihuanshuai
lihuanshuai / stat.md
Last active July 28, 2017 03:44
从 fex-team/ua-device 中补充 uap
We couldn’t find that file to show.