Skip to content

Instantly share code, notes, and snippets.

View stoensin's full-sized avatar
🎯
Focusing

Joe Stone stoensin

🎯
Focusing
  • shenzhen university
  • shenzhen
View GitHub Profile
'''
1 当指定异常被引发时,使用on_exception装饰器重试。这里有一个例子,当出现任何requests异常时,使用指数退避(backoff.expo即退避时间指数增长):
2 当目标函数返回值符合某个特定条件时,on_predicate装饰器会安排重试。当为外部生成内容轮询资源时可能有用。
3 两个backoff装饰器都可以选择使用关键字参数on_success、on_backoff和on_giveup接受事件处理程序函数。这在报告统计或执行其他自定义日志方面可能有用。
'''
@stoensin
stoensin / Abstract Factory
Last active November 28, 2019 03:38
意图:定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行 主要解决:主要解决接口选择的问题 何时使用:我们明确地计划不同条件下创建不同实例时 如何解决:让其子类实现工厂接口,返回的也是一个抽象的产品 关键代码:创建过程在其子类执行 使用场景: 1、日志记录器:记录可能记录到本地硬盘、系统事件、远程服务器等,用户可以选择记录日志到什么地方。 2、数据库访问,当用户不知道最后系统采用哪一类数据库,以及数据库可能有变化时。 3、设计一个连接服务器的框架,需要三个协议,"POP3"、"IMAP"、"HTTP",可以把这三个作为产品类,共同实现一个接口。
#!/usr/bin/python
#coding:utf8
'''
Abstract Factory
抽象工厂模式也就是不仅生产鼠标,同时生产键盘。
也就是 PC 厂商是个父类,有生产鼠标,生产键盘两个接口。
戴尔工厂,惠普工厂继承它,可以分别生产戴尔鼠标+戴尔键盘,和惠普鼠标+惠普键盘。
创建工厂时,由戴尔工厂创建。
后续工厂.生产鼠标()则生产戴尔鼠标,工厂.生产键盘()则生产戴尔键盘。
'''
@stoensin
stoensin / &map
Last active November 14, 2022 12:15
python因为其全局解释器锁GIL而无法通过线程实现真正的平行计算。 IO密集型:读取文件,读取网络套接字频繁。 计算密集型:大量消耗CPU的数学与逻辑运算,也就是我们这里说的平行计算。 而concurrent.futures模块,可以利用multiprocessing实现真正的平行计算。 核心原理是:concurrent.futures会以子进程的形式,平行的运行多个python解释器,从而令python程序可以利用多核CPU来提升执行速度。由于子进程与主解释器相分离,所以他们的全局解释器锁也是相互独立的。每个子进程都能够完整的使用一个CPU内核。
# 求最大公约数
def gcd(pair):
a, b = pair
low = min(a, b)
for i in range(low, 0, -1):
if a % i == 0 and b % i == 0:
return i
numbers = [
(1963309, 2265973), (1879675, 2493670), (2030677, 3814172),
@jasny
jasny / sha256-hmac.md
Last active December 12, 2023 12:32
Hashing examples in different languages

Example inputs:

Variable Value
key the shared secret key here
message the message to hash here

Reference outputs for example inputs above:

| Type | Hash |

@stoensin
stoensin / all
Last active May 5, 2019 15:23
冒泡、选择、插入、希尔、归并、计数、快排
class ALG(object):
def __init__(self,list):
self.list=list
#冒泡
def bubble_sort(nums):
n =len(nums)
for i in range(n - 1):
for j in range(n-1 - i):
if nums[j+1] < nums[j]:
nums[j], nums[j + 1] = nums[j + 1], nums[j]
@stoensin
stoensin / ops
Created February 28, 2019 15:07
import tensorflow as tf
import numpy as np
##################################################################################
# Initialization
##################################################################################
# Xavier : tf.contrib.layers.xavier_initializer()
# He : tf.contrib.layers.variance_scaling_initializer()
# Normal : tf.random_normal_initializer(mean=0.0, stddev=0.02)
@tomericco
tomericco / cosine_similarity.js
Created January 26, 2019 10:58
Cosine similarity implementation in JS
const str1 = 'This is an example to test cosine similarity between two strings';
const str2 = 'This example is testing cosine similatiry for given two strings';
//
// Preprocess strings and combine words to a unique collection
//
const str1Words = str1.trim().split(' ').map(omitPunctuations).map(toLowercase);
const str2Words = str2.trim().split(' ').map(omitPunctuations).map(toLowercase);
const allWordsUnique = Array.from(new Set(str1Words.concat(str2Words)));
@v0y4g3r
v0y4g3r / notify.sh
Last active February 19, 2019 11:10
bark-bash
#! /bin/bash
in=$1
if [ -z "$in" ]
then
in=$(cat -)
fi
if [ -z "$in" ]
@dslwind
dslwind / 邪门歪道之用OpenCV精准切割视频
Last active April 9, 2024 09:24
使用OpenCV、Python以及FFmpeg逐帧处理视频
# 邪魔歪道之使用OpenCV精准切割视频
## ffmpeg 切割视频
使用ffmpeg直接切割视频的命令
```
ffmpeg -i test.mp4 -ss 00:00:00 -t 00:00:30 -c:v copy -c:a copy output.mp4
```
或者
```
@mieitza
mieitza / mongo_to_csv.py
Created January 3, 2018 15:09 — forked from wixb50/mongo_to_csv.py
python mongo to csv use pandas.
# @Author: xiewenqian <int>
# @Date: 2016-11-28T20:35:09+08:00
# @Email: wixb50@gmail.com
# @Last modified by: int
# @Last modified time: 2016-12-01T19:32:48+08:00
import pandas as pd
from pymongo import MongoClient