/images/avatar.jpeg

Python数据结构与算法

线性表

python的list是可变线性表。

  1. len()是 O(1)操作

  2. 元素访问和赋值,尾端加入和尾端删除(包括尾端切片删除)都是O(1)操作。

  3. 一般位置的元素加入,切片替换,切片删除,表拼接(extend)都是O(n)操作。

Mysql 常用命令

安装

# 启动mysql 
sudo systemctl start mysql
# 查看mysql 状态
sudo systemctl status mysql

# 查看mysql 配置 
vim /etc/mysql/mariadb.conf.d/50-server.cnf

# 查看系统中的mysql进程
ps -aux | grep "mysql"
ps -ef | grep mysql

# 杀掉mysql进程 提示 bash: kill: (4385) - Operation not permitted, 因为没有权限,要使用sudo
sudo kill -15 
  • 问题:访问数据库报错 ERROR 1524 (HY000): Plugin ‘unix_socket’ is not loaded

Redis实战读书笔记

配置开机启动

转载: redis设置开机自启

sudo vim /etc/systemd/system/redis.service
sudo chmod 777 redis.service

[Unit]
Description=redis-server
After=network.target
 
[Service]
Type=forking
ExecStart=/opt/redis-6.0.5/src/redis-server /opt/redis-6.0.5/redis.conf
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

1. 先关闭redis-server
systemctl stop redis.service
2. 开启redis-server
systemctl start redis.service
3. 开启成功,将服务加入开机自启
systemctl enable redis.service
4. 重启 
shutdown -r now
5. 查看服务运行状态
systemctl status redis.service

4、配置描述:

  Description:描述服务
  After:描述服务类别
  [Service]服务运行参数的设置
  Type=forking是后台运行的形式
  ExecStart为服务的具体运行命令
  ExecReload为重启命令
  ExecStop为停止命令
  PrivateTmp=True表示给服务分配独立的临时空间
  注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
  [Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
  


# 查看redis 是否启动成功
netstat -lntp | grep 6379

命令速查

  1. 查看数据库数量 CONFIG GET databases

P1001 A+B Problem

题目描述

输入两个整数 a,b输出它们的和。

输入格式

两个整数以空格分开。

输出格式

一个整数。

输入输出样例

输入 #1

20 30

输出 #1

50

# 80ms /  3.16MB /  45B Python 3
print(sum([int(x) for x in input().split()]))
/*
22ms /  6.92MB /  128B Go
*/
package main

import (
	"fmt"
)

var (
	a int64
    b int64
)

func main() {
	fmt.Scanf("%d%d", &a, &b)
	fmt.Printf("%d", a+b)
}
/*
 514ms /  11.43MB /  232B Java 8
*/
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        int a = cin.nextInt();
        int b = cin.nextInt();
        System.out.println(a+b);
    }
}

Vim配置

介绍

安装Vundle插件管理器

git clone --depth=1 https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
git clone --depth=1 https://github.com/preservim/nerdtree.git ~/.vim/bundle/nerdtree
git clone --depth=1 https://github.com/Xuyuanp/nerdtree-git-plugin.git ~/.vim/bundle/nerdtree-git-plugin

更新配置文件

vim ~/.vimrc

set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'scrooloose/nerdtree'
Plugin 'Xuyuanp/nerdtree-git-plugin'
call vundle#end()

" 显示行号
set nu

" tabs键四空格
set ts=4

" 代码高亮
syntax enable

" 启用鼠标
" set mouse=a
" set selection=exclusive
" set selectmode=mouse,key

"显示状态栏
set laststatus=2

" 代码换行自动缩进
set autoindent
filetype plugin on
" 按 F5 执行当前 Python 代码"
map <F5> :call PRUN()<CR>
func! PRUN()
    exec "w"
    if &filetype == 'python'
        exec "!python %"
    endif
endfunc

" NerdTree才插件的配置信息
""将F2设置为开关NERDTree的快捷键
map <f3> :NERDTreeToggle<cr>
""修改树的显示图标
let g:NERDTreeDirArrowExpandable = '+'
let g:NERDTreeDirArrowCollapsible = '-'
""窗口位置
let g:NERDTreeWinPos='left'
""窗口尺寸
let g:NERDTreeSize=30
""窗口是否显示行号
let g:NERDTreeShowLineNumbers=1
""不显示隐藏文件
let g:NERDTreeHidden=0

安装插件 :PluginInstall

为debian10添加软件源

Debian 10添加163软件源:

vi /etc/apt/source.list
deb http://mirrors.163.com/debian/ buster main contrib non-free
deb http://mirrors.163.com/debian/ buster-updates main contrib non-free
deb http://mirrors.163.com/debian-security/ buster/updates main contrib non-free
:wq

其它debian发行版本同理,更改代号即可,历史代号可到官网查看 debian.org

Python时间处理

datetime 库

from datetime import datetime, timedelta
# 字符串 转 datetime类型
another_day_str = '2020-07-18 21:00:00'
another_day_datetime = datetime.strptime(another_day_str, '%Y-%m-%d %H:%M:%S')

# 时间加减运算, 增加1小时30分30秒
after_datetime = another_day_datetime + timedelta(hours=1, minutes=30, seconds=30)

# datetime类型 转 字符串 2020-07-18 22:30:30
after_str = after_datetime.strftime('%Y-%m-%d %H:%M:%S')

# 获取当前时间的字符串 格式化输出 20200719223030
now_datetime = datetime.now()
now_str = now_datetime.strftime('%Y%m%d%H%M%S')

# 转换为时间戳
timestamp = time.mktime(now_datetime.timetuple())

# 时间戳转换为datetime
datetime.datetime.fromtimestamp(timestamp)

# 获取间隔天数
day = (now_datetime - another_day_datetime).days

# 获取间隔秒数(不包含天数差)
second = (now_datetime - another_day_datetime).seconds

time库

import time
# 返回当前时间的时间戳(1970纪元后经过的浮点秒数)
timestamp = time.time()

# 时间戳转换为 struct_time类型(结构体时间)
struct_time = time.localtime(timestamp)

# 字符串转结构体时间 struct_time
now_str = '2020-07-19 21:00:00'
struct_time = time.strptime(now_str, "%Y-%m-%d %H:%M:%S")
# time.struct_time(tm_year=2020, tm_mon=7, tm_mday=19, tm_hour=21, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=201, tm_isdst=-1) <class 'time.struct_time'>

# struct_time 转 格式化字符串 20200719210000
str_time =  time.strftime('%Y%m%d%H%M%S', struct_time)

Commonly used format codes:

Python使用os模块

import os
old_path = 'data/20200222'
new_path = 'new_path'

old_file = 'oldfile'
new_file = 'newfile'

# 判断使用的系统平台windows 返回 nt, Linux 返回posix
os_name = os.name
print(os_name)
if os_name == 'nt':
    print('Windows操作系统!')
if os_name == 'posix':
    print('Linux操作系统!')

# 获取当前用户主目录路径
print(os.environ['HOME'])
print(os.path.expandvars('$HOME'))
print(os.path.expanduser('~'))

# 获取当前工作目录
print(os.getcwd())

# 创建目录
os.mkdir(new_path)

# 递归创建目录
os.makedirs(old_path)

# 获得文件的大小,如果为目录,返回0
os.path.getsize(new_path)


print(os.listdir(new_path))

# 删除文件

if os.path.exists(new_file):
    os.remove(new_file)
else:
    print("文件不存在, 删除失败")

# 改文件名

if os.path.exists(old_file):
    # 查看文件/目录是否存在
    if os.path.exists(new_file):
        print("新文件(名)已存在")
    else:
        os.rename(old_file, new_file)
else:
    print("旧文件(名)不存在")
# 文件目录遍历
for dirpath, dirnames, filenames in os.walk(new_path):
    print(dirpath)       # 当其目录绝对路径, 类似linux命令pwd的输出,str类型
    print(dirnames)      # 当前目录下的子目录,list类型
    print(filenames)     # 当前目录下的所有文件,list类型
    

# 删除目录
os.rmdir(new_path)
os.rmdir(old_path)

git常见问题处理

git status 中文显示异常

中文名等特殊符号被转义,不易读。 使用下面命令可正常显示中文。 git config --global core.quotepath false

查看项目中某个人的代码数


git log --author="Spaceack" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' 

added lines: 1280, removed lines: 198, total lines: 1082

合并多次commit

例如要合并 最新的三次提交,将后两个提交合并到第一个:

SSE协议

介绍

服务器推送事件: Server-Sent Events, SSE

特点

  1. 仅从服务器向客户端实现单向实时通信。
  2. 实现简单,基于HTTP协议。
  3. 浏览器端有断线重连功能。
  4. 支持用户自定义消息类型。
  5. 用来传送文本, 二进制需要编码。

flask-sse 实现

flask-sse文档