惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
Security Latest
Security Latest
V
Vulnerabilities – Threatpost
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
L
LINUX DO - 最新话题
N
News and Events Feed by Topic
P
Proofpoint News Feed
G
GRAHAM CLULEY
NISL@THU
NISL@THU
S
Securelist
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Schneier on Security
Schneier on Security
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Threat Research - Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tenable Blog
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
Help Net Security
Help Net Security
C
CERT Recently Published Vulnerability Notes
T
The Exploit Database - CXSecurity.com
I
Intezer
阮一峰的网络日志
阮一峰的网络日志
AI
AI
PCI Perspectives
PCI Perspectives
Hacker News: Ask HN
Hacker News: Ask HN
aimingoo的专栏
aimingoo的专栏
宝玉的分享
宝玉的分享
博客园 - 【当耐特】
腾讯CDC
The Cloudflare Blog
Spread Privacy
Spread Privacy
Latest news
Latest news
有赞技术团队
有赞技术团队
D
Docker
Cyberwarzone
Cyberwarzone
L
LINUX DO - 热门话题
S
Secure Thoughts
Hugging Face - Blog
Hugging Face - Blog
K
Kaspersky official blog
M
MIT News - Artificial intelligence
N
News | PayPal Newsroom
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
SegmentFault 最新的问题
J
Java Code Geeks
S
Security Affairs
The Register - Security
The Register - Security
云风的 BLOG
云风的 BLOG
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

陈少文的网站

巨变与机遇的未来十年 Kubernetes 平台管理软件压力测试方案 使用镜像部署 Hexo 静态页面 终于等到你 - GitHub 镜像仓库服务(ghcr.io) 一起来学 Go --(6)Interface 一起来学 Go --(5)Goroutine 和 Channel 什么是函数式编程 如何在 Kubernetes 集群集成 Kata 柯里化与偏函数 使用 PyGithub 自动创建 Label 软件产品是团队能力的输出 Helm 2 、Helm 3 比较 IoT 变现 Kubernetes 中的 DNS 服务 国内的 Helm 镜像源 Harbor 使用自签证书支持 Https 访问 DevOps 工具链之 Prow 如何使用 kfctl 安装 Kubeflow VS Code 无法下载 Go 插件的工具包 工程师更应具有服务精神 你不知道的 Docker 使用技巧 使用 Docker 运行 Tensorflow 论中国 什么是左移 如何清空 Git 仓库全部历史记录 一禅小和尚 有风吹过厨房 时间的玫瑰 如何在 CentOS 安装 GPU 驱动 开发 Tips(19) 使用 Velero 备份 Kubernetes 集群 Kubernetes Cheat Sheet 开发 Tips(18) 如何构建一个 Java 工程 开发 Tips(17) KubeSpray 安装 Kubernetes 报错 ip in ansible_all_ipv4_addresses 基于 Kubernetes 和 Jenkins 搭建自动化测试系统 在 Kubernetes 上动态创建 Jenkins Slave 使用 Jenkins 进行服务拨测 开发 Tips(16) Kubernetes 签发 Ingress 证书及日常故障运维 Kubernetes 中 Deployment 的基本操作 Kubernetes 中的证书 如何使用 KubeBuilder 开发一个 Operator Kubernetes 1.6.0 安装问题汇总 镜像管理工具 -- Harbor 开发 Tips(15) Docker 如何拉取镜像 开发 Tips(14) 使用 Helm 安装 harbor 开发 Tips(13) 使用 S2I 构建云原生应用 在 Kubernetes 中使用 emptyDir、hostPath、localVolume 开发 Tips(12) 开发 Tips(11) 代码质量分析工具 SonarQube 使用 Kubeadm 安装 Kubernetes 集群 一起来学 Go --(4)常用函数 Kubernetes 中的 Ceph Kubernetes 之 Volumes Kubernetes 之 Labels、Selectors 开发 Tips(10) 开源正在重构商业模式 Kubernetes 之网络 Kubernetes 之 API 使用 Helm 和 Operator 快速部署 Prometheus Kubernetes 复杂有状态应用管理框架 -- Operator Kubernetes 的包管理器 -- Helm 一起来学 Go --(3)Go Modules 如何一步一步地优化博客方案 kubectl 实用指南 Kubernetes 中的基本概念 搭建远程 Kubernetes 开发环境 大公司和小公司的 ToB 思路 开发 Tips(9) Go 入门指南 一起来学 Go --(2)数据与逻辑结构 如何预防 Web 富文本中的 XSS 攻击 django-xss-cleaner 云工作时代 一起来学 Go --(1)背景与特点 SaaS 开发团队的不同阶段 你不知道的 Git 使用技巧 输出既服务 微服务设计 继续奔跑 开发 Tips(8) 从账户安全到二次验证 Django 性能之数据库查询优化 Django 性能之分库分表 敏捷开发之研发流程 打造一致性的团队 Pytest 进阶学习之 Mock PaaS 部署之 buildpack Go 开发配置 领域输出才是 PaaS 的核心竞争力 Pytest 入门学习 开发 Tips(6) 如何使用 Jenkins、Docker、GitLab 搭建 Django 自动化部署流程 开发 Tips(5)
开发 Tips(7)
微信公众号 · 2019-02-24 · via 陈少文的网站

Please enable Javascript to view the contents

主要记录最近遇到的一些开发问题,解决方法。

1. Python2 和 Python3 中的异常处理

Python2,Python3 都支持的两种方式:

  • 带参数
1
2
except ExceptionType as Argument:
    # 访问 Argument
  • 不带参数

仅 Python2 支持的方式:

1
2
except ExceptionType, Argument:
    # 访问 Argument

2. Django 中的 get_object_or_404 和 get_queryset

get_object_or_404 通过使用 get 获取对象,否则返回 404

1
2
3
4
5
6
7
8
9
from django.shortcuts import get_object_or_404
from django.forms.models import model_to_dict
from django.http import JsonResponse
from .models import Fruit


def filter404(request):
    obj = get_object_or_404(Fruit, title='aa')
    return JsonResponse(model_to_dict(obj))

使用 get_queryset 可以全局的定制查询行为,包括 admin

1
2
3
class FruitManager(models.Manager):
    def get_queryset(self):
        return super(FruitManager, self).get_queryset().filter(is_delete=False)

3. 树形结构存储

  • 物化路径

每个节点存储其完整路径编码。

1
2
3
4
5
Name        Path
William     1
Jones       1/1
Blake       1/2
Adams       1/2/1

优点是读取和写入都非常快。

  • 邻接表模型

邻接列表表示通过保持到某些相邻节点的链接来存储树。

1
2
3
Name        Parent     Next
William     null       Joseph
Jones       William    Blake

优点是,结构简单易懂,但是数据量很大时,基于递归的查询效率非常低。

  • 嵌套集

每个节点存储一些索引(通常是左右值)。

1
2
3
4
Name        left   right
William     1      10
Jones       2      3
Blake       4      7

优点是查询数据很快,但是更新时,需要修改的节点很大。

  • 区间嵌套集

将区间映射为二维空间,每一个节点根据规则对应一个分数。在涉及节点位置的层次查询时,不需要访问数据库。优点是,性能非常好,但是实现和理解起来有一点门槛。

django-treebeard 实现了物化路径,嵌套集和邻接列表。 django-mptt 混合了嵌套集和邻接列表,能高效地查询子节点,被破坏时可以重建树。

4. Pytest 找不到模块报错

报错信息:

1
2
3
4
5
6
=================================== ERRORS ====================================
_____________ ERROR collecting home_application/test/test_mptt.py _____________
ImportError while importing test module 'C:\pytest\home_application\test\test_mptt.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
ImportError: No module named home_application.test.test_mptt

问题原因:

test 目录下,存在 __init__.py 文件,导致 Pytest 将整个 test 当作一个模块来处理。实际上,我们需要的是 Pytest 进入目录,找到 test_ 开头的文件,运行测试。只需要删除 __init__.py 文件即可。

5. CentOS 7 中 /etc/rc.local 开机不自动运行

查看 /etc/rc.local 内容发现:

1
2
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

CentOS 7 建议创建 systemd 用于开机自启动。

如果需要使用旧的方式,则需要添加到 /etc/rc.d/rc.local。同时,执行 chmod +x /etc/rc.d/rc.local,赋予可执行权限。

6. Secure shell Extension 插件 NaCI 退出,状态 255

报错原因是本地保存的指纹信息与主机信息不符。

解决办法:

在 shell 窗口,按下 Ctrl+Shift+J 进入调试窗口,在 console 执行:

1
term_.command.removeAllKnownHosts()

清空 known_hosts 即可。

7. Python 续行的几种方式

1,利用反斜杠

1
2
3
4
5
6
7
8
9
a = 'sdfaf' \
    'test'

a = '1' + '2' + '3' + \
    '4' + '5'

if False and \
    True:
    pass

2, 利用括号

1
2
3
4
5
6
7
8
9
a = ('sdfaf'
    'test')

a = ('1' + '2' + '3' +
    '4' + '5')

if(False and
    True):
    pass

微信公众号