罗翔老师讲的一点感悟

昨天是段历史, 明天是个谜团,
而今天是天赐的礼物, 像珍惜礼物那样珍惜今天.

对于不可控的事情,我们要保持乐观和自信;
对于可控的事情,我们要保持谨慎和节制。
对于不可控的事情,你的焦虑和恐惧于事无补,
你焦虑改变不了明天,反而会影响今天的心情;
你的恐惧也改变不了明天,因为人生,唯一恐惧的就是恐惧本身。
但是对于可控的事情,能不能好好的利用时间就是可控的,你要谨慎,要节制。
因为如果不谨慎不节制就会乐极生悲。

今天是个礼物,大家好好的把握今天。

Python的三元运算符几种替代方案

在Python语言中,是不支持三元计算符的,比如Java中用这种形式,来判断身份证的男女

1
String gender = Integer.parseInt(idcard.substring(16, 17)) % 2 != 0"男""女";

而在Python中没有这个运算符,有一个if else可以替代

1
gender = "男" if int(idcard[16:17]) % 2 != 0 else "女"

或者这样:

1
gender =  int(idcard[16:17]) % 2 != 0 and "男" or "女"

更或者这样:

1
2
# 利用int(True)=1和int(False)=0, 可以实现下面的选择
gender = ("女" , "男")[int(idcard[16:17]) % 2 != 0]

还可以这样

1
2
# 利用True和False当作字典的key, 可以实现下面的选择
gender = {True:"男",False:"女"}[int(idcard[16:17]) % 2 != 0]

如果判断的东西是否定和非否定关系,甚至还可以这样.

1
2
3
# 同样是利用利用int(True)=1和int(False)=0, 然后切片数组
age=18
adult_flag = "未成年"[age>18:]

windows 10 切换WIFI和网线的优先级

在办公室里需要同时连接网线和WIFI,网线连通公司内网,WIFI链接外网,手动切换比较麻烦,手动配置路由也比较麻烦。
查询了一下,发现可以通过设置网络的优先级来实现切换默认网络,找了找命令,可以用powershell来执行。

管理员模式运行powershell,获取网络列表, 过滤出IPv4且连接状态的网络.

1
2
3
4
5
6
7
PS C:\Users\skeetw> Get-NetIPInterface -AddressFamily IPv4 -ConnectionState Connected

ifIndex InterfaceAlias AddressFamily NlMtu(Bytes) InterfaceMetric Dhcp ConnectionState PolicyStore
------- -------------- ------------- ------------ --------------- ---- --------------- -----------
20 Ethernet IPv4 1500 45 Enabled Connected ActiveStore
21 Wi-Fi IPv4 1500 95 Enabled Connected ActiveStore
1 Loopback Pseudo-Interface 1 IPv4 4294967295 75 Disabled Connected ActiveStore

然后调整WIFI的优先级:

1
2
PS C:\Users\skeetw> Set-NetIPInterface -InterfaceIndex 21 -InterfaceMetric 95
PS C:\Users\skeetw> Set-NetIPInterface -InterfaceIndex 21 -InterfaceMetric 5

这个命令中-InterfaceIndex 指的是第一个命令结果网卡的索引号,-InterfaceMetric 用于指定接口跃点数。
接口跃点数数值越小,那么优先级越高,优先级高就会变成首选网络。

通过把WIFI网络的跃点数调成95或5, 这两条命令的交替执行就能达到wifi网络和有线网络交替成为默认网络,状态栏的网络图标的变换可能稍有延迟。

flask-sqlalchemy 同时连接多个数据库,动态获得不同的session

目前正在用Flask做一个小项目,持久层用的是flask-sqlalchemy, 这个和直接使用sqlalchemy还略有些不同。
项目中有个需求,要从另外一个mssqlserver数据库中读取数据,查到的很多资料都是需要再次在代码定义数据库的表结构,通过定义不同的bind,然后使用关键字”bind_key“把表映射的不同的数据库中。这样略有些麻烦,要对接的mssqlserver是另外一个C/S系统的数据库,主要是读取报表功能,已经找到了一个方法,可以通过在界面上操作,直接获取到所有对数据库的操作。

1
2
3
4
5
6
7
SELECT st.text as sql_statement,
qs.creation_time as plan_last_compiled,
qs.last_execution_time as plan_last_executed,
qs.execution_count as plan_executed_count
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.plan_handle) st
order by plan_last_compiled desc

执行这条sql就可以知道这个客户端对sqlserver最终执行了什么,所以简单的操作就是直接把sql语句拿到我们的系统里直接执行就可以了,所以就需要一个方法传入一个变量,就直接拿到特殊的session,而这个session又是ORM框架管理,查了很多资料,包括flask-sqlalchemy的官网后都没有找到直接答案,自己翻了翻源码,发现这样实现是可以的。

配置部分:

db_setting.py
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
from contextlib import contextmanager

from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy

from greenwater_app import app


app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db/database1.db'
app.config['SQLALCHEMY_BINDS'] = {
'database2': 'sqlite:///db/database2.db'
}
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SQLALCHEMY_ECHO'] = True

db = SQLAlchemy(app)

migrate = Migrate(app, db, compare_type=True)


@contextmanager
def session_scope(bind='default'):
"""Provide a transactional scope around a series of operations."""

if bind != 'default':
# 切换到指定的数据库,获取scope session
s = db.create_scoped_session(options={'bind': db.get_engine(db.get_app(), bind)})
else:
s = db.session

s.expire_on_commit = False
try:
yield s
s.commit()
except:
s.rollback()
raise
finally:
s.close()

正常的按照官网的逻辑是可以使用的。具体参考这里https://flask-sqlalchemy.palletsprojects.com/en/2.x/binds/

如果想直接拿到一个session去执行sql语句,可以这样:

1
2
3
4
5
6
7
8
sql = 'select * from user'
with session_scope(bind='database2') as s:
res = s.execute(sql).fetchall()
print([dict(r) for r in res])

with session_scope() as s:
res = s.execute(sql).fetchall()
print([dict(r) for r in res])

通过给session_scope传入不同的变量,就可以获得一个针对不同的数据库的session。

无花果竟然结果了

2021年3月22日在公司东面操场的空地上买了两只无花果的枝条种上了。
无花果刚种下的时候

每周去浇浇水,看看它,在最难熬的那段时间看着它每周都有成长,也算是一个不错的心里寄托。
无花果

2021年11月16日,发现虽然叶子全部都掉了,但是竟然结果了。
树叶全落的无花果
结了一个小无花果

应该好好总结过去的这半年,经历了一些,如果没有很好的收获,但是像这个无花果一样也有一个小小的结果。

Edge浏览器和搜狗输入法不兼容

Edge浏览器升级以后突然发现巨卡无比,尤其是新开标签页和搜索之前,浏览器整个陷入卡顿,无响应。
感觉不像是电脑整体卡住了,CPU的使用率也只是短暂的到达100%,最后实在找不到原因,也忍不了了,电脑也该重装了,就拿去IT那里给重装了。
结果重装完发现是好的,但是使用了半天发现问题又来了,排除了浏览器的所有插件,最后发现可能是搜狗输入法的问题,只要打开输入法,就用卡住。
但是Edge和搜狗都想用,尝试了半天修改搜狗的配置,最后发现是因为一个皮肤引起的,把搜狗的皮肤换成默认的,然后调整一下字体和颜色。
问题解决!

获取任意一个小程序的页面路径

学习了一下Vue和uni-app,发现做个小程序还是挺容易的,于是乎顺手搞了一个,现在已经发布了。
扫描这个码可以体验一下。
tancheng

做这个小程序的时候,遇到一个小问题,有一个按钮需要直接跳转到携程的买票页面,可是在携程的页面上无法直接找到当前页面的path, 搜索了好久也没有发现靠谱的答案。
最后发现,在小程序的文章编辑页面,可以实现这个功能。先公众号管理后台,先新建一个文章,然后在正文中插入小程序,输入小程序的ID,(这个ID可以在小程序的详情中找到)。
getpath1

然后下一步中,选择“获取更多页面路径”,输入自己的微信号。

getpath2

打开自己的手机,点击小程序右上角的三个点,弹出的页面中就多出来一个新的复制路径的按钮。

getpath3

这个设计还是挺巧妙的,点个赞!

在python字典组成的数组中,按照其中的字典的一个key的值进行排序

对接海康的系统,取出客流信息后,发现返回的结果是乱序的, 并没有按照时间先后顺序排列。

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
[
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 28,
"flowOutNum": 17,
"holdValue": 0,
"createTime": 1622070960029,
"updateTime": 1622073660035,
"statTime": "2021-05-27T07:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 25,
"flowOutNum": 44,
"holdValue": 0,
"createTime": 1622103360028,
"updateTime": 1622106060028,
"statTime": "2021-05-27T16:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 40,
"flowOutNum": 33,
"holdValue": 0,
"createTime": 1622078160028,
"updateTime": 1622080920032,
"statTime": "2021-05-27T09:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 33,
"flowOutNum": 41,
"holdValue": 0,
"createTime": 1622081760029,
"updateTime": 1622084460028,
"statTime": "2021-05-27T10:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 28,
"flowOutNum": 30,
"holdValue": 0,
"createTime": 1622096160028,
"updateTime": 1622098860029,
"statTime": "2021-05-27T14:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 32,
"flowOutNum": 24,
"holdValue": 0,
"createTime": 1622092560213,
"updateTime": 1622095260028,
"statTime": "2021-05-27T13:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 0,
"flowOutNum": 0,
"holdValue": 0,
"createTime": 1622045760027,
"updateTime": 1622048460027,
"statTime": "2021-05-27T00:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 0,
"flowOutNum": 0,
"holdValue": 0,
"createTime": 1622056560027,
"updateTime": 1622059260043,
"statTime": "2021-05-27T03:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 0,
"flowOutNum": 0,
"holdValue": 0,
"createTime": 1622049360031,
"updateTime": 1622052060029,
"statTime": "2021-05-27T01:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 15,
"flowOutNum": 21,
"holdValue": 0,
"createTime": 1622063760027,
"updateTime": 1622066460027,
"statTime": "2021-05-27T05:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 29,
"flowOutNum": 19,
"holdValue": 0,
"createTime": 1622067360029,
"updateTime": 1622070060027,
"statTime": "2021-05-27T06:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 0,
"flowOutNum": 0,
"holdValue": 0,
"createTime": 1622052960029,
"updateTime": 1622055660027,
"statTime": "2021-05-27T02:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 0,
"flowOutNum": 2,
"holdValue": 0,
"createTime": 1622060160028,
"updateTime": 1622062861776,
"statTime": "2021-05-27T04:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 30,
"flowOutNum": 28,
"holdValue": 0,
"createTime": 1622074560035,
"updateTime": 1622077260037,
"statTime": "2021-05-27T08:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 19,
"flowOutNum": 51,
"holdValue": 0,
"createTime": 1622085360028,
"updateTime": 1622088060029,
"statTime": "2021-05-27T11:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 27,
"flowOutNum": 23,
"holdValue": 0,
"createTime": 1622088960033,
"updateTime": 1622091660029,
"statTime": "2021-05-27T12:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 16,
"flowOutNum": 21,
"holdValue": 0,
"createTime": 1622099760030,
"updateTime": 1622102460029,
"statTime": "2021-05-27T15:00:00.000+08:00"
}
]

Python的 sorted 内置函数提供了一个lambda 参数可以对它排序。

1
2
data = hik_http_request(url, body).json()['data']['list']
return sorted(data, key=lambda e: e.__getitem__('createTime'))

排序后的结果:

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
[
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 0,
"flowOutNum": 0,
"holdValue": 0,
"createTime": 1622045760027,
"updateTime": 1622048460027,
"statTime": "2021-05-27T00:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 0,
"flowOutNum": 0,
"holdValue": 0,
"createTime": 1622049360031,
"updateTime": 1622052060029,
"statTime": "2021-05-27T01:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 0,
"flowOutNum": 0,
"holdValue": 0,
"createTime": 1622052960029,
"updateTime": 1622055660027,
"statTime": "2021-05-27T02:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 0,
"flowOutNum": 0,
"holdValue": 0,
"createTime": 1622056560027,
"updateTime": 1622059260043,
"statTime": "2021-05-27T03:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 0,
"flowOutNum": 2,
"holdValue": 0,
"createTime": 1622060160028,
"updateTime": 1622062861776,
"statTime": "2021-05-27T04:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 15,
"flowOutNum": 21,
"holdValue": 0,
"createTime": 1622063760027,
"updateTime": 1622066460027,
"statTime": "2021-05-27T05:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 29,
"flowOutNum": 19,
"holdValue": 0,
"createTime": 1622067360029,
"updateTime": 1622070060027,
"statTime": "2021-05-27T06:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 28,
"flowOutNum": 17,
"holdValue": 0,
"createTime": 1622070960029,
"updateTime": 1622073660035,
"statTime": "2021-05-27T07:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 30,
"flowOutNum": 28,
"holdValue": 0,
"createTime": 1622074560035,
"updateTime": 1622077260037,
"statTime": "2021-05-27T08:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 40,
"flowOutNum": 33,
"holdValue": 0,
"createTime": 1622078160028,
"updateTime": 1622080920032,
"statTime": "2021-05-27T09:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 33,
"flowOutNum": 41,
"holdValue": 0,
"createTime": 1622081760029,
"updateTime": 1622084460028,
"statTime": "2021-05-27T10:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 19,
"flowOutNum": 51,
"holdValue": 0,
"createTime": 1622085360028,
"updateTime": 1622088060029,
"statTime": "2021-05-27T11:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 27,
"flowOutNum": 23,
"holdValue": 0,
"createTime": 1622088960033,
"updateTime": 1622091660029,
"statTime": "2021-05-27T12:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 32,
"flowOutNum": 24,
"holdValue": 0,
"createTime": 1622092560213,
"updateTime": 1622095260028,
"statTime": "2021-05-27T13:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 28,
"flowOutNum": 30,
"holdValue": 0,
"createTime": 1622096160028,
"updateTime": 1622098860029,
"statTime": "2021-05-27T14:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 16,
"flowOutNum": 21,
"holdValue": 0,
"createTime": 1622099760030,
"updateTime": 1622102460029,
"statTime": "2021-05-27T15:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 25,
"flowOutNum": 44,
"holdValue": 0,
"createTime": 1622103360028,
"updateTime": 1622106060028,
"statTime": "2021-05-27T16:00:00.000+08:00"
},
{
"groupId": "b5900064-b435-4886-8061-2ba39148c7c6",
"groupName": "主入口",
"flowInNum": 7,
"flowOutNum": 8,
"holdValue": 0,
"createTime": 1622106960027,
"updateTime": 1622106960027,
"statTime": "2021-05-27T17:00:00.000+08:00"
}
]

Powered by Hexo and Hexo-theme-hiker

Copyright © 2013 - 2024 Tiaobug All Rights Reserved.

本站总访问量    次