Skip to content

Instantly share code, notes, and snippets.

@hxer
hxer / request_flask.py
Created March 2, 2016 04:45
Receiving and POSTing JSON with requests [Python and Flask]
import os
# Using Flask since Python doesn't have built-in session management
from flask import Flask, session, render_template
# Our target library
import requests
import json
app = Flask(__name__)
# Generate a secret random key for the session
@hxer
hxer / dict_dataclass.py
Last active November 1, 2022 08:02
复杂dict对象转换dataclass对象
import json
from typing import Dict
from dataclasses import (
asdict, dataclass, field, fields, is_dataclass
)
# 对于一些嵌套的数据类,需要深度遍历
class EnhancedJSONEncoder(json.JSONEncoder):
def default(self, o):
@hxer
hxer / log.py
Last active September 18, 2021 14:37
python logging 将日志同时输出文件和屏幕
# 日志打印屏幕或文件
def setup_logger_2(logger_name, level=logging.INFO, handler='all', log_file=''):
"""
:param logger_name:
:param log_file:
:param level:
:param handler: file or stream or all
:return:
"""
log_setup = logging.getLogger(logger_name)
#!/bin/sh
#
# Script to start CPU limit daemon
#
set -e
case "$1" in
start)
if [ $(ps -eo pid,args | gawk '$3=="/usr/bin/cpulimit_daemon.sh" {print $1}' | wc -l) -eq 0 ]; then
nohup /usr/bin/cpulimit_daemon.sh >/dev/null 2>&1 &
@hxer
hxer / batch.py
Created June 12, 2019 09:41
可迭代对象按固定大小chunk分割,不补齐
from itertools import islice, chain
#!/usr/bin/python3
def batch(iterable, size):
source_iter = iter(iterable)
while True:
batch_iter = islice(source_iter, size)
try:
first_element = next(batch_iter)
except StopIteration:
@hxer
hxer / local-debug-cluster.sh
Last active September 24, 2019 03:06
单机集群启动脚本可调试
#!/bin/bash
# Copyright 2014 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
@hxer
hxer / nginx.conf
Last active May 13, 2019 03:48
nginx反向代理
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.domain.com;
location / {
proxy_pass http://www.example.com;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
@hxer
hxer / mail_template.py
Created May 5, 2019 04:27
jinja2渲染HTML邮件
# -*- coding:utf-8 -*-
from jinja2 import Environment
from jinja2 import FileSystemLoader
if __name__ == "__main__"
data = {}
env = Environment(loader=FileSystemLoader('{0}/templates/'.format(os.path.dirname(__file__))))
template = env.get_template('risk_rule_daily.html')
html = template.render(**data)
@hxer
hxer / pom.xml
Created January 13, 2019 02:15
pom.xml in flink scala demo
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.eflink</groupId>
<artifactId>word-count</artifactId>
<version>1.0</version>
@hxer
hxer / logging.yaml
Last active July 26, 2018 06:09 — forked from JesseBuesking/logging.yaml
python2 MultiProcess Log
---
version: 1
disable_existing_loggers: False
formatters:
simple:
format: "%(name)-20s%(levelname)-8s%(message)s"
handlers:
console:
class: logging.StreamHandler
level: DEBUG