常用方法封装

python-生成CURL命令

import json

def getCurlCommand(path, params, method, headers):
    if method == 'GET':
        getParams = []
        for k in params:
            getParams.append(k + "=" + params[k])
        path += "?" + '&'.join(getParams)
    curlCommand = "curl --location --request "+method+" '"+path+"' \\\n"
    for k in headers:
        curlCommand += "--header '{}: {}' \\\n".format(k, headers[k])
    if params:
        curlCommand += "--data-raw '"+json.dumps(params)+"'"
    print(curlCommand)

headers = {
    'Bearer Token': 'XXX',
}
method = 'POST'
path = 'http://baidu.com'
params = {
    "staff": '1',
}

getCurlCommand(path, params, method, headers)

python-请求数据生成json和excel文件

import pandas as pd
import json
import requests

url = "https://test1.com"
# 请求头
headers = {
    "content-type": "application/json",
}
# 参数
payload = {"username": "", "password": ""}
# 转换成json
getJson = json.dumps(payload).encode("utf-8")
# 构造发送请求
response = requests.post(url=url, headers=headers, data=getJson)
resJson = json.loads(response.text)

url = "https://test2.com"
# 请求头
headers = {
    "content-type": "application/json",
    "Authorization": "Bearer " + resJson['payload']['token']
}
# 参数
payload = {
    "sql": "select * from table;",
    "data_base": "zh"
}
# 转换成json
getJson = json.dumps(payload).encode("utf-8")
# 构造发送请求
response = requests.post(url=url, headers=headers, data=getJson)
resJson = json.loads(response.text)

with open("/Users/zh/Downloads/response.json", "w") as f:
    json.dump(resJson['payload']['data'], f)

with open('/Users/zh/Downloads/response.json') as data:
    df = pd.DataFrame(json.load(data))
df.to_excel('/Users/zh/Downloads/data.xlsx')

python-excel转sql

from itertools import count
import xlrd

def open_excel(file):
    try:
        data = xlrd.open_workbook(file)
        return data
    except Exception as e:
        print(str(e))

def excel_by_name(file, by_name='Sheet1'):  # 修改自己路径
    data = open_excel(file)
    table = data.sheet_by_name(by_name)  # 获得表格
    nRows = table.nrows  # 拿到总共行数
    tableName = '`zh`.`test`'
    arr = table.row_values(0)
    del(arr[0])
    idIndex = arr.index("id")
    del(arr[idIndex])
    fields = '`, `'.join(arr)
    index = arr.index("deleted_at")
    sql = "INSERT INTO " + tableName + " (`"+fields+"`) VALUES "
    for i in range(1, nRows):  # 循环行数
        row_vals = table.row_values(i)
        del(row_vals[0])
        del(row_vals[idIndex])
        if len(row_vals[index]) == 0:
            row_vals[index] = 'null'
        fields = "', '".join(row_vals)
        sql += "('" + fields + "'), "
    sql = sql.replace("'null'", "null")
    print(sql.rstrip(", ") + ";")

excel_by_name('/Users/zh/Downloads/data.xlsx')

python-读取excel

import xlrd
import datetime

def open_excel(file):
    try:
        data = xlrd.open_workbook(file)
        return data
    except Exception as e:
        print(str(e))

def excel_by_name(file, by_name='Sheet1'):  # 修改自己路径
    data = open_excel(file)
    table = data.sheet_by_name(by_name)  # 获得表格
    nRows = table.nrows  # 拿到总共行数
    sql = "INSERT INTO `zh`.`test`(`is_delete`, `created_at`, `updated_at`) VALUES "
    for i in range(1, nRows):  # 循环行数
        row_vals = table.row_values(i)  # 行数据
        date = xlrd.xldate.xldate_as_datetime(
                row_vals[5], 0)
        sql += "({}, '{}', '{}'), ".format(int(row_vals[1], date, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    print(sql.rstrip(", ") + ";")

excel_by_name("/Users/zh/Downloads/aaa.xlsx")

php-kafka

public function consumer(Request $request)
{
    $config = new Conf();
    $config->set("group.id", "php_consumer");
    $config->set('metadata.broker.list', '127.0.0.1:9092,127.0.0.1:9093');
    $config->set('enable.auto.commit', 'false');
    $config->set('auto.offset.reset', 'earliest');
    $consumer = new \RdKafka\KafkaConsumer($config);
    $consumer->subscribe(['zh_test']);//topic
    while (true) {
        $msg = $consumer->consume(120*1000);
        if (null === $msg || $msg->err === RD_KAFKA_RESP_ERR__PARTITION_EOF) {
            // Constant check required by librdkafka 0.11.6. Newer librdkafka versions will return NULL instead.
            continue;
        } elseif ($msg->err) {
            Logger::audit("Kafka Consumer错误信息:" . $msg->errstr());
            break;
        } else {
            $value = $msg->payload;
            $json = json_decode($value, true);
            var_dump($json);
            $consumer->commit($msg); //手动提交偏移量
        }
    }
}
public function producer(Request $request)
{
    $config = new \RdKafka\Conf();
    $config->set("group.id", "php_producer");
    $config->set('metadata.broker.list', '127.0.0.1:9092,127.0.0.1:9093');
    $producer = new \RdKafka\Producer($config);
    $topic = $producer->newTopic('zh_test');
    $topic->produce(-1, 0, '2222');
    $result = $producer->flush(10000);
    var_dump($result);
}

php-图片打文字水印

/**
*图片打文字水印
*$source       原图
*$target       生成水印图
*$water_text   水印文字
*$water_w      文字水印宽度
*$water_h      文字水印高度
*$font_size    水印文字大小
*$angle        水印文字倾斜角度
*$alpha        水印文字透明度 0-127
*$over_flag true 覆盖 false 不覆盖创建一个新的图片
*/
function textwater($source, $target, $water_text='test', $water_w=250, $water_h=210, $font_size=14, $angle=-45, $alpha=80, $over_flag=false) {
	//检测图片是否存在
	if (!file_exists($source)) return false;
	$info = getimagesize($source);
	//通过编号获取图像类型
	$type = image_type_to_extension($info[2],false);
	//在内存中创建和图像类型一样的图像
	$fun = "imagecreatefrom".$type;
	//图片复制到内存
	$image = $fun($source);
	//设置字体颜色和透明度
	$color = imagecolorallocatealpha($image, 0, 0, 0, $alpha);
	$x_length = $info[0];
	$y_length = $info[1];
	$font = 'statics/fonts/SourceHanSansCN-Normal.ttf';
	$water_text = mb_convert_encoding($water_text, "html-entities", "utf-8");
	//铺满屏幕
	for ($x = 10; $x < $x_length; $x) {
		for ($y = 10; $y < $y_length; $y) {
			imagettftext($image, $font_size, $angle, $x, $y, $color, $font, $water_text);
			$y += $water_h;
		}
		$x += $water_w;
	}
	$fun = "image".$type;
	//保存图片
	$fun($image, $target);
	//销毁图片
	imagedestroy($image);
	return true;
}

mysql-全文索引匹配过半不显示-布尔全文搜索模式

WHERE MATCH(title) AGAINST ('mysql' IN BOOLEAN MODE); 

mysql-数据导出

mysql -uroot -proot -e "select userid, email from member order by userid" > /home/zh/test.xls
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值