Skip to content

Instantly share code, notes, and snippets.

@BenZstory
BenZstory / phash_with_naive_bayes_helper.py
Created November 10, 2020 14:21
[phash_with_naive_bayes] #python
import os
import numpy as np
import sys
import glob
import cv2
import time
import json
import threading
import subprocess
from queue import Queue
@BenZstory
BenZstory / str2bool_argparse.py
Created June 5, 2020 03:11
[str2bool for python argparse] How to let python argparse support true/false flag in str #python
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
@BenZstory
BenZstory / call_cmd.py
Last active June 5, 2020 03:07
[Cmd calling helper in python, with log steaming] helps to easily do subprocess calling and log output to a file or logger #python #shell
def simpler_log(line):
global g_log_path
if g_log_path != "":
with open(g_log_path, 'a', encoding='utf-8') as fout:
fout.write(line + "\n")
else:
g_logger.info("log_path is empty, print guess won't work.")
print(line)
@BenZstory
BenZstory / yyets_links_collector.py
Created April 12, 2020 04:35
[yyets link collector] helps you collect a batch of yyets:// style links for rrshare #python #spider
git config --global https.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080
git config --global --unset http.proxy
git config --global --unset https.proxy
npm config delete proxy
@BenZstory
BenZstory / get_proxy_from_pool.py
Created January 17, 2020 06:25
[use_a_proxy_pool] demo codes to maintain a tested proxies pool #python
import requests
from requests.exceptions import ProxyError, ConnectTimeout, SSLError, ReadTimeout, ConnectionError
TEST_URL = "https://www.sogou.com"
TEST_TIMEOUT = 30
PROXY_UPDATE_CNT_PER_TIME = 5
PROXY_UPDATE_URL = "https://api-ip.abuyun.com/obtain?license=LC392580C7B8805P&sign=6e696401251f916a510c876df69ff837&cnt=" + str(PROXY_UPDATE_CNT_PER_TIME)
def test_proxy(proxy, logger):
@BenZstory
BenZstory / combine_and_permutation.py
Created January 17, 2020 06:15
[combine and permutation methods] recursive methods to calculate combine and permutation #python
# a recursive method that return all combine result of a list
def combine(chosen, candidates, to_choose, result):
if to_choose == 0:
result.add(tuple(chosen))
return
else:
for i in range(0, len(candidates)-to_choose+1):
chosen.append(candidates[i])
combine(chosen, candidates[i+1:len(candidates)], to_choose-1, result)
chosen.remove(candidates[i])
@BenZstory
BenZstory / get_logger.py
Last active October 21, 2019 07:23
[get_logger] logger builder for both jupyter/tensorflow streamHandler and fileHandler #python #jupyter
def get_logger(from_logger='tensorflow', log_file=None, sh_level=logging.DEBUG, fh_level=logging.DEBUG):
"""Create a configured instance of logger."""
fmt = '[%(asctime)s] %(levelname)s : %(message)s'
# date_fmt = '%Y-%m-%d %H:%M:%S,uuu'
formatter = logging.Formatter(fmt)
# get default logger and set lowest logging level
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
@BenZstory
BenZstory / load_model.python
Created August 23, 2019 09:53
[load_model] load a ckpt or frozen_pb #tf #dl
def load_model(model, input_map=None):
"""Loads a tensorflow model and restore the variables to the default session."""
# Check if the model is a model directory (containing a metagraph and a checkpoint file)
# or if it is a protobuf file with a frozen graph
model_exp = os.path.expanduser(model)
if (os.path.isfile(model_exp)):
print('Model filename: %s' % model_exp)
with tf.gfile.FastGFile(model_exp, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
@BenZstory
BenZstory / parallel_tfrecords.py
Last active August 22, 2019 07:49
[parallel tfrecords] create tfrecords with multi threads #ml #tf
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import re
import sys
import time
import json
import shutil