Groceries

常用的东西 每次都要 google 好麻烦 记个笔记

Commands

note

windows电源管理中没有休眠选项

1
2
3
powercfg -h off
powercfg hibernate size 40
Powercfg -h on

windows community solution

SQLAlchemy url to the database:

'mysql+pymysql://bird:qpzm2745@127.0.0.1/demo?charset=utf8'

python

django orm 使用 pymysql

project\project\__init__.py 文件中添加

1
2
3
import pymysql

pymysql.install_as_MySQLdb()

获取本机 ip 主机名 MAC地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import socket

def get_local_ip() -> str:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('1.1.1.1', 80))
ip = s.getsockname()[0]
finally:
s.close()
return ip

hostname = socket.gethostname()

def get_mac_address():
mac=uuid.UUID(int = uuid.getnode()).hex[-12:]
return ":".join([mac[e:e+2] for e in range(0,11,2)])

时间戳格式化

1
2
3
4
timeStamp = 1532585103
timetamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timeStamp))
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")

转换为时间戳

1
2
3
a = "2013-10-10 23:40:00"
timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
timeStamp = int(time.mktime(timeArray))

日期计算

1
2
3
4
delta = datetime.timedelta(days=3)
ctime = datetime.datetime.strptime(ctime,'%Y-%m-%d %H:%M:%S')
n_days = ctime + delta
print(n_days.strftime('%Y.%m.%d'))

list 的交集 并集 差集

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
def diff(listA,listB):
#求交集的两种方式
retA = [i for i in listA if i in listB]
retB = list(set(listA).intersection(set(listB)))

print "retA is: ",retA
print "retB is: ",retB

#求并集
retC = list(set(listA).union(set(listB)))
print "retC1 is: ",retC

#求差集,在B中但不在A中
retD = list(set(listB).difference(set(listA)))
print "retD is: ",retD

retE = [i for i in listB if i not in listA]
print "retE is: ",retE

def main():
listA = [1,2,3,4,5]
listB = [3,4,5,6,7]
diff(listA,listB)

if __name__ == '__main__':
main()

结果

1
2
3
4
5
retA is:  [3, 4, 5]
retB is: [3, 4, 5]
retC1 is: [1, 2, 3, 4, 5, 6, 7]
retD is: [6, 7]
retE is: [6, 7]