dict = {"name": "zs", "age": 18, "city": "深圳", "tel": "1362626627"}
res = sorted(dict.items(), key=lambda x: x[0], reverse=False)
print(res)
new_dict = {}
for x in res:
new_dict[x[0]] = x[1]
print(new_dict)
2. 字典排序方式二foo = [{"name": "zs", "age": 19}, {"name": "ll", "age": 54},
{"name": "wa", "age": 17}, {"name": "df", "age": 23}]
res = sorted(foo, key=lambda x: x["name"], reverse=False)
print(res)
3.元组排序foo = [("zs", 19), ("ab", 2), ("t", 8)]
res = sorted(foo, key=lambda x: x[0], reverse=False)
print(res)
valid_columns = set(User.__table__.columns.keys()) # 获取model中定义的columns
这样就能获取到合法的schema字段,然后我们把数据源的数据过滤:valid_attrs = {k: v for k, v in i.items() if k in valid_columns}
如果其中有需要单独处理的字段,我们进行特殊处理:adjust_time(valid_attrs)
def adjust_time(attrs):
# mongo存储的是毫秒时间戳
attrs["created_at"] = datetime.datetime.fromtimestamp(attrs["created_at"] / 1000.0)
attrs["updated_at"] = datetime.datetime.fromtimestamp(attrs["updated_at"] / 1000.0)
if attrs.get("deleted_at"):
attrs["deleted_at"] = datetime.datetime.fromtimestamp(attrs["deleted_at"] / 1000.0)
之后我们就可以愉快的对SQLAlchemy对象进行更新或者插入了:already_exist = User.get_by_id(s, i["user_id"])
if already_exist:
logging.info("update item: %s", i)
User.update_by_user_id(s, already_exist.user_id, valid_attrs)
else:
logging.info("insert item: %s", i)
s.add(User(**valid_attrs))
其中,model中的 update_by_user_id定义如下:@classmethod
def update_by_user_id(cls, session, user_id, attr_map):
session.query(cls).filter(cls.user_id == user_id).update(attr_map)
搞定! def save_app_info(self):
try:
# update app_info
print(self.dicts)
data = db_session.query(App_Info).filter_by(app_id=self.app_id,mall_name=self.mall_name).first()
if data:
{setattr(data, k, v) for k,v in dicts.items()}
print(data)
else:
# insert app_info
db_session.execute(App_Info.__table__.insert(), self.dicts)
db_session.commit()
except:
db_session.rollback()
other.error("save app_data is error,here are details:{}".format(traceback.format_exc()))
finally:
db_session.close()
msg_count = db.session.query(sqlalchemy.func.count(SMS_Receive.id))\
.filter(and_(SMS_Receive.IsShow == True, SMS_Receive.PhoneNumber_id == number))\
.scalar()
sms_count_info = SMSCount(PhoneNumber_id=number, SMS_Count=msg_count)
db.session.add(sms_count_info)
db.session.commit()
之后是update代码:get_sms_count = SMSCount.query.filter_by(PhoneNumber_id=number).first()
get_sms_count.SMS_Count+=1
get_sms_count.PhoneNumber_id=number
db.session.commit()
可以看到在insert里面先加入到session里面才提交,而insert则是先查询出来之后更新才提交。admin = User.query.filter_by(username='admin').first()
admin.email = 'my_new_email@example.com'
db.session.commit()
user = User.query.get(5)
user.name = 'New Name'
db.session.commit()
yum install epel-release
CentOS Linux 8 - AppStream 23 B/s | 38 B 00:01
Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist
[baseos]
name=CentOS Linux $releasever - BaseOS
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=BaseOS&infra=$infra
#baseurl=http://mirror.centos.org/$contentdir/$releasever/BaseOS/$basearch/os/
baseurl=https://vault.centos.org/centos/$releasever/BaseOS/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[appstream]
name=CentOS Linux $releasever - AppStream
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=AppStream&infra=$infra
#baseurl=http://mirror.centos.org/$contentdir/$releasever/AppStream/$basearch/os/
baseurl=https://vault.centos.org/centos/$releasever/AppStream/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
$ sudo apt remove tracker tracker-extract tracker-miner-fs
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
gir1.2-clutter-gst-3.0 gir1.2-evince-3.0 libgsf-1-114 libgsf-1-common libmusicbrainz5-2
libtagc0
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
gnome-shell-extension-desktop-icons gnome-sushi insync-nautilus nautilus
nautilus-mediainfo nautilus-share tracker tracker-extract tracker-miner-fs ubuntu-desktop
ubuntu-desktop-minimal
0 upgraded, 0 newly installed, 11 to remove and 0 not upgraded.
After this operation, 5,517 kB disk space will be freed.
Do you want to continue? [Y/n]
因此,尝试在 Ubuntu 19.04 上删除 Tracker 会导致 Nautilus、桌面图标扩展和 ubuntu-desktop 元包被删除。 在 Fedora 另一方面,默认安装了更多的 Gnome 软件,删除 Tracker 也想删除 Gnome Boxes、Documents、Photos 和 Totem,以及其他 134 个包。tracker status
声称它的索引中有超过 100000 个文件,并且它目前正在索引文件。 但是如果您愿意,您可以尝试一下,看看它是否对您的系统有任何影响。systemctl --user mask tracker-store.service tracker-miner-fs.service tracker-miner-rss.service tracker-extract.service tracker-miner-apps.service tracker-writeback.service
对于追踪器 3:systemctl --user mask tracker-extract-3.service tracker-miner-fs-3.service tracker-miner-rss-3.service tracker-writeback-3.service tracker-xdg-portal-3.service tracker-miner-fs-control-3.service
在此之后,重置跟踪器:tracker reset --hard
对于追踪器 3:tracker3 reset -s -r
并重新启动。systemctl --user unmask tracker-store.service tracker-miner-fs.service tracker-miner-rss.service tracker-extract.service tracker-miner-apps.service tracker-writeback.service
对于追踪器 3:systemctl --user unmask tracker-extract-3.service tracker-miner-fs-3.service tracker-miner-rss-3.service tracker-writeback-3.service tracker-xdg-portal-3.service tracker-miner-fs-control-3.service
并在此之后重新启动您的系统.crontab [ -u user ] file
或crontab [ -u user ] { -l | -r | -e }
说明:* * * * *
- - - - -
| | | | |
| | | | +----- 星期中星期几 (0 - 6) (星期天 为0)
| | | +---------- 月份 (1 - 12)
| | +--------------- 一个月中的第几天 (1 - 31)
| +-------------------- 小时 (0 - 23)
+------------------------- 分钟 (0 - 59)
使用者也可以将所有的设定先存放在文件中,用 crontab file 的方式来设定执行时间。* * * * * /bin/ls
在 12 月内, 每天的早上 6 点到 12 点,每隔 3 个小时 0 分钟执行一次 /usr/bin/backup:0 6-12/3 * 12 * /usr/bin/backup
周一到周五每天下午 5:00 寄一封信给 alex@domain.name:0 17 * * 1-5 mail -s "hi" alex@domain.name < /tmp/maildata
每月每天的午夜 0 点 20 分, 2 点 20 分, 4 点 20 分....执行 echo "haha":20 0-23/2 * * * echo "haha"
下面再看看几个具体的例子:0 */2 * * * /sbin/service httpd restart 意思是每两个小时重启一次apache
50 7 * * * /sbin/service sshd start 意思是每天7:50开启ssh服务
50 22 * * * /sbin/service sshd stop 意思是每天22:50关闭ssh服务
0 0 1,15 * * fsck /home 每月1号和15号检查/home 磁盘
1 * * * * /home/bruce/backup 每小时的第一分执行 /home/bruce/backup这个文件
00 03 * * 1-5 find /home "*.xxx" -mtime +4 -exec rm {} \; 每周一至周五3点钟,在目录/home中,查找文件名为*.xxx的文件,并删除4天前的文件。
30 6 */10 * * ls 意思是每月的1、11、21、31日是的6:30执行一次ls命令
注意:当程序在你所指定的时间执行后,系统会发一封邮件给当前的用户,显示该程序执行的内容,若是你不希望收到这样的邮件,请在每一行空一格之后加上 > /dev/null 2>&1 即可,如:20 03 * * * . /etc/profile;/bin/sh /var/www/runoob/test.sh > /dev/null 2>&1
脚本无法执行问题#!/bin/sh
. /etc/profile
. ~/.bash_profile
3、在 /etc/crontab 中添加环境变量,在可执行命令之前添加命令 . /etc/profile;/bin/sh,使得环境变量生效,例如:20 03 * * * . /etc/profile;/bin/sh /var/www/runoob/test.sh
podman run -i -d -p 80:80 -v /data/:/data/ --privileged --name mytest centos:7.3.1611 /usr/sbin/init
-i: 允许你对容器进行交互
-d: 后台运行 不会进入容器
-p 端口号,左侧服务器指向:右侧容器内端口号,服务器端口不能重复,如果多个容器多个应用,需要nginx反响代理
-v 文件存储,左侧为映射到本地:右侧为容器内文件夹,防止镜像过大,迁移困难
--privileged: 和最后的参数/usr/sbin/init共同使用 才会有更高的权限控制
--name: 自定义容器启动的名称
centos:7.3.1611: 镜像名称 如果本地没有 会从网络上下载
也可以加入其它的参数 如-m 8G --cpus="4"
-m 8G :内存分配为8G
--cpus="4": 分配cpu核心总数为4
podman exec -i -t 7f5d968e2cf2 /bin/bash
-i: 允许你对容器进行交互
-t: 在新容器内指定一个伪终端或终端
243c32535da7:容器id可以用podman ps -a查看 也可以用名称代替
7、容器如果是挺值得,开启命令podman start mytest(容器名称)#默认centos源
[root@slave02 ~]# yum -y module install container-tools #容器工具基于模块
[root@slave02 ~]# yum -y install podman-docker #安装docker兼容包(可选)
版本[root@slave02 ~]# podman -v
podman version 3.3.0-dev
仓库[root@slave02 ~]# podman help|head -15
Manage pods, containers and images
Usage:
podman [options] [command]
Available Commands:
attach Attach to a running container
auto-update Auto update containers according to their auto-update policy
build Build an image using instructions from Containerfiles
commit Create new image based on the changed container #基于修改的容器创建新的容器
container Manage containers
cp Copy files/folders between a container and the local filesystem
create Create but do not start a container
diff Display the changes to the object's file system
events Show podman events
....
镜像加速器[root@slave02 ~]# cp /etc/containers/registries.conf /backup/registries.conf.back #备份一下
[root@slave02 ~]# vim /etc/containers/registries.conf
unqualified-search-registries = ["docker.io"] #非限定搜索登记处
[[registry]]
prefix = "docker.io"
location = "x" #x是阿里加速镜像地址
拉取镜像[root@slave02 ~]# podman pull nginx
6.运行一个web容器#准备html页面内容
[root@192 ~]# cat /opt/webhtml/index.html
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambition
#运行一个守护web容器进程,将/opt/webhtml目录内容映射到容器的/usr/share/nginx/html存放网页的位置
[root@192 ~]# podman run -d --name web -p 8888:80 -v /opt/webhtml:/usr/share/nginx/html nginx
3528e6d5148bcf980f0df5708a82419d3485a33d1d16d722db3e880cc103cd2c
[root@podman ~]# curl 192.168.136.129:8888
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambition
#容器的ip
[root@podman ~]# podman inspect web|grep IPAddress
"IPAddress": "10.88.0.6",
"IPAddress": "10.88.0.6",
#宿主机的ip
[root@podman ~]# ip r
192.168.136.0/24 dev ens33 proto kernel scope link src 192.168.136.129 metric 100
#由于进行了端口绑定,所以直接 curl 192.168.136.129:8888即可访问
进入后台web容器,查看服务状态[root@podman ~]# podman exec -it web bash
root@3528e6d5148b:/# service nginx status
[ ok ] nginx is running. #运行中
修改容器业务内容#修改宿主机/opt/webhtml/index.html即可
[root@podman ~]# cat /opt/webhtml/index.html
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambitionRHCAS
RHCE RHCA
#进行访问
[root@podman ~]# curl 192.168.136.129:8888
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambitionRHCAS
RHCE RHCA
#进入容器查看内容是否修改
[root@podman ~]# podman exec -it web bash
root@3528e6d5148b:/# cat /usr/share/nginx/html/index.html
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambitionRHCAS
RHCE RHCA
暂停与删除容器#暂停
[root@podman ~]# podman stop web
web
[root@podman ~]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3528e6d5148b docker.io/library/nginx:latest nginx -g daemon o... 25 minutes ago Exited (0) 16 seconds ago 0.0.0.0:8888->80/tcp web
#删除
[root@podman ~]# podman rm web
3528e6d5148bcf980f0df5708a82419d3485a33d1d16d722db3e880cc103cd2c
#或强制删除运行中的容器
[root@podman ~]# podman rm -f web
3528e6d5148bcf980f0df5708a82419d3485a33d1d16d722db3e880cc103cd2c
[root@podman ~]# podman run --name web -d -p 8080:80 -v /opt/webhtml:/usr/shar/nginx/html nginx
910db3ab6bd1ef18e5fd0afe1844912f0b89334b7b8ab758353a948a1b55282a
基于web容器,在优先级一般的/etc/systemd/system内[root@192 ~]# cd /etc/systemd/system/
[root@podman user]# podman generate systemd --
--container-prefix (Systemd unit name prefix for containers)
--files {生成.service文件,而不是打印到标准输出}
--format (Print the created units in specified format (json)) #以指定的格式打印单元文件
--name (Use container/pod names instead of IDs) #创建新容器,而不是使用现有的容器
--new (Create a new container instead of starting an existing one)#(跳过标头生成)
--no-header (Skip header generation)
--pod-prefix (Systemd unit name prefix for pods)
--restart-policy (Systemd restart-policy)
--separator (Systemd unit name separator between name/id and prefix)
--time (Stop timeout override)
[root@192 system]# podman generate systemd --name web --files --new
/etc/systemd/system/container-web.service
查看生成的单元文件[root@192 system]# cat container-web.service
# container-web.service
# autogenerated by Podman 3.3.0-dev #podman 3.3.0-dev自动生成
# Tue Aug 17 13:03:13 CST 2021 #8月17日星期二13:03:13 CST 2021
[Unit] #单元
Description=Podman container-web.service #描述
Documentation=man:podman-generate-systemd(1) #帮助以及生成的系统
Wants=network-online.target #网络
After=network-online.target
RequiresMountsFor=%t/containers #前面不重要直接跳过
[Service]
Environment=PODMAN_SYSTEMD_UNIT=%n
Restart=on-failure #故障时重新启动
TimeoutStopSec=70 #超时时间
ExecStart=/usr/bin/podman run --sdnotify=conmon --cgroups=no-conmon --rm --replace --name web -d -p 8080:80 -v /opt/webhtml:/usr/shar/nginx/html nginx #执行开始为/usr/bin/podman 运行刚才创建的容器
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target default.target
删除刚才的容器[root@podman ~]# podman rm web
910db3ab6bd1ef18e5fd0afe1844912f0b89334b7b8ab758353a948a1b55282a
[root@podman ~]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
设置开机自启[root@192 ~]# systemctl daemon-reload
[root@192 ~]# systemctl enable --now container-web.service
Created symlink /etc/systemd/system/multi-user.target.wants/container-web.service → /etc/systemd/system/container-web.service.
Created symlink /etc/systemd/system/default.target.wants/container-web.service → /etc/systemd/system/container-web.service.
[root@192 user]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b0c7709cb00e docker.io/library/nginx:latest nginx -g daemon o... 15 seconds ago Up 16 seconds ago 0.0.0.0:8080->80/tcp web
无根root模式设置容器和上面这种方式大同小异#需要运行loginctl enable-linger命令,使用户服务在服务器启动时自动启动即可
[containers@serverb ~]$ loginctl enable-linger