0%

一些自己总结的tricks

一些自己总结的tricks,主要是装环境和代码片段,存档自查用

Centos7安装MySQL5.7版本

1
2
3
4
5
6
7
8
9
10
11
12
13
wget -P /tmp https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
yum localinstall -y /tmp/mysql80-community-release-el7-3.noarch.rpm

yum repolist all | grep mysql
yum -y install yum-utils
yum-config-manager --disable mysql80-community
yum-config-manager --enable mysql57-community
yum repolist enabled | grep mysql

yum install -y mysql-community-server

service mysqld start
grep 'temporary password' /var/log/mysqld.log

Python3在内存中打包Zip

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# !user/bin/env python3
# -*-coding : utf-8 -*-
import zipfile
from io import BytesIO
import os


class InMemoryZIP(object):

def __init__(self):
# create the in-memory file-like object
self.in_memory_zip = BytesIO()

def append(self, filename_in_zip, file_contents):
""" Appends a file with name filename_in_zip \
and contents of file_contents to the in-memory zip.
"""
# create a handle to the in-memory zip in append mode
zf = zipfile.ZipFile(self.in_memory_zip, 'a', zipfile.ZIP_DEFLATED, False)

# write the file to the in-memory zip
zf.writestr(filename_in_zip, file_contents)

# mark the files as having been created on Windows
# so that Unix permissions are not inferred as 0000
for zfile in zf.filelist:
zfile.create_system = 0
return self

def appendfile(self, file_path, file_name=None):
""" Read a file with path file_path \
and append to in-memory zip with name file_name.
"""
if file_name is None:
file_name = os.path.split(file_path)[1]

f = open(file_path, 'rb')
file_contents = f.read()
self.append(file_name, file_contents)
f.close()
return self

def read(self):
""" Returns a string with the contents of the in-memory zip.
"""
self.in_memory_zip.seek(0)
return self.in_memory_zip.read()

def writetofile(self, filename):
"""
Write the in-memory zip to a file
"""
f = open(filename, 'wb')
f.write(self.read())
f.close()

if __name__ == '__main__':
imz = InMemoryZIP()
imz.appendfile('a.txt').append('test.txt', 'This is content in test.txt')
imz.writetofile('test.zip')