<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Python on Cylon&#39;s Collection</title>
    <link>https://www.161616.top/tags/python/</link>
    <description>Recent content in Python on Cylon&#39;s Collection</description>
    <generator>Hugo -- 0.125.7</generator>
    <language>zh</language>
    <lastBuildDate>Mon, 01 Jan 0001 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://www.161616.top/tags/python/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>python drf之viewset</title>
      <link>https://www.161616.top/python-django-restframework-view-set/</link>
      <pubDate>Wed, 06 Oct 2021 00:00:00 +0000</pubDate>
      <guid>https://www.161616.top/python-django-restframework-view-set/</guid>
      <description>What is Views drf提供了两个基类，五个视图扩展类，9个视图集
drf提供了一个Django中view的子类APIView ,主要变动大概为以下：
重新封装了Request 与 Response实例。 使用了独有的Request与Response对象，并且提供了专有的解析器 Parser 可以根据HTTP Content-Type 指明的请求数据进行解析。 增加了自有的鉴权/节流 在django中dispatch() 分发前，会对请求进行身份认证、权限检查、流量控制。 异常捕获 APIException。 APIView implement
python 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 @classmethod def as_view(cls, **initkwargs): .... # 调用父类的方法，Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx view = super(APIView, cls).as_view(**initkwargs) view.cls = cls # 并且生成一个新的request view.initkwargs = initkwargs # Note: session based authentication is explicitly CSRF validated, # all other authentication is CSRF exempt.</description>
    </item>
    <item>
      <title>python drf之Serializer</title>
      <link>https://www.161616.top/python-django-restframework-serializers/</link>
      <pubDate>Mon, 04 Oct 2021 00:00:00 +0000</pubDate>
      <guid>https://www.161616.top/python-django-restframework-serializers/</guid>
      <description>What is serializers？ serializers主要作用是将原生的Python数据类型（如 model querysets ）转换为web中通用的JSON，XML或其他内容类型。
DRF 提供了一个Serializer类，它为您提供了种强大的通用方法来控制响应的输出，以及一个ModelSerializer 类，它为创建处理 model instance 和 serializers 提供了一个序列化的快捷方式。
Reference drf serializers manual
How to Declaring Serializers? 序列化一个django model
python 1 2 3 4 5 6 7 class Comment: def __init__(self, email, content, created=None): self.email = email self.content = content self.created = created or datetime.now() comment = Comment(email=&amp;#39;leila@example.com&amp;#39;, content=&amp;#39;foo bar&amp;#39;) 声明Serializers，可以用来序列化与反序列化对象 Comment的属性及值。
python 1 2 3 4 5 6 from rest_framework import serializers class CommentSerializer(serializers.</description>
    </item>
    <item>
      <title>python django使用</title>
      <link>https://www.161616.top/python-django/</link>
      <pubDate>Sun, 03 Oct 2021 00:00:00 +0000</pubDate>
      <guid>https://www.161616.top/python-django/</guid>
      <description>路由匹配 django中默认匹配页 text 1 url(r&amp;#39;^$&amp;#39;, views.login), django中404匹配 text 1 url(r&amp;#39;^$&amp;#39;, views.login), # 需要放置最后，不过一般不推荐，都是通过异常捕获处理 named group 名称组 https://docs.djangoproject.com/en/1.11/topics/http/urls/#named-groups
text 1 url(r&amp;#39;^test[0-9]{4}&amp;#39;,views.login) 反向解析 别名不能出现冲突
text 1 2 from django.shortcuts import reverse reverse(xxx) 名称组反向解析 无名分组
python 1 2 3 4 5 6 # 路由部分 url(r&amp;#39;^index/(\d+)/&amp;#39;, views.home, name=&amp;#39;xxx&amp;#39;) # 前端 &amp;lt;a href=&amp;#34;{% url &amp;#39;id&amp;#39; obj.id %}&amp;#34; class=&amp;#34;btn btn-primary btn-xs&amp;#34;&amp;gt;remove&amp;lt;/a&amp;gt;s # 后端 print reverse(&amp;#39;id&amp;#39;, args=(id,)) 有名称分组
python 1 2 3 4 5 6 7 8 # 路由部分 url(r&amp;#34;^userdel/(?</description>
    </item>
    <item>
      <title>django ORM</title>
      <link>https://www.161616.top/django-orm/</link>
      <pubDate>Sat, 02 Oct 2021 00:00:00 +0000</pubDate>
      <guid>https://www.161616.top/django-orm/</guid>
      <description>https://www.cnblogs.com/Dominic-Ji/p/11516152.html
对象关系映射（Object Relational Mapping，简称ORM）模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术。
简单的说，ORM是通过使用描述对象和数据库之间映射的元数据，将程序中的对象自动持久化到关系数据库中。
ORM在业务逻辑层和数据库层之间充当了桥梁的作用。
django中仅测试ORM 导入model，然后直接使用对应对象进行ORM操作。
text 1 2 3 4 5 6 7 import os if __name__ == &amp;#34;__main__&amp;#34;: os.environ.setdefault(&amp;#34;DJANGO_SETTINGS_MODULE&amp;#34;, &amp;#34;app.settings&amp;#34;) import django django.setup() from xxx import models models.User.objects.all() 连接数据库 django配置数据库
python 1 2 3 4 5 6 7 8 9 10 DATABASES = { &amp;#39;default&amp;#39;: { &amp;#39;ENGINE&amp;#39;: &amp;#39;django.db.backends.mysql&amp;#39;, &amp;#39;USER&amp;#39;: &amp;#39;root&amp;#39;, &amp;#39;PASSWORD&amp;#39;: &amp;#39;111&amp;#39;, &amp;#39;HOST&amp;#39;:&amp;#39;127.0.0.1&amp;#39;, &amp;#39;NAME&amp;#39;: &amp;#39;book&amp;#39;, &amp;#39;CHARSET&amp;#39;: &amp;#39;utf8&amp;#39; } } 可选：pymysql 使用模块连接MySQL数据库:：在项目中__init__.py 文件中添加配置：
python 1 2 import pymysql pymysql.</description>
    </item>
    <item>
      <title>由PIPE size 引起的线上故障</title>
      <link>https://www.161616.top/pipe-size-problem/</link>
      <pubDate>Sat, 02 Oct 2021 00:00:00 +0000</pubDate>
      <guid>https://www.161616.top/pipe-size-problem/</guid>
      <description>sence：python中使用subprocess.Popen(cmd, stdout=sys.STDOUT, stderr=sys.STDERR, shell=True) ，stdout, stderr 为None.
在错误中执行是无法捕获 stderr的内容，后面将上面的改为 subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True),发现是可以拿到 stderr, 但是会遇到大量任务hanging，造成线上事故。
为此特意查询subprocess的一些参数的说明。
stdin stdout stderr 如果这些参数为 PIPE, 此时会为一个文件句柄，而传入其他（例如 sys.stdout 、None 等）的则为None
正如这里介绍的一样，subprocess 。
而使用 PIPE，却导致程序 hanging。一般来说不推荐使用 stdout=PIPE stderr=PIPE，这样会导致一个死锁，子进程会将输入的内容输入到 pipe，直到操作系统从buffer中读取出输入的内容。
查询手册可以看到确实是这个问题 Refernce
Warning This will deadlock when using stdout=PIPE and/or stderr=PIPE and the child process generates enough output to a pipe such that it blocks waiting for the OS pipe buffer to accept more data. Use communicate() to avoid that.</description>
    </item>
    <item>
      <title>python使用虚拟环境venv</title>
      <link>https://www.161616.top/python-venv/</link>
      <pubDate>Mon, 14 Jun 2021 00:00:00 +0000</pubDate>
      <guid>https://www.161616.top/python-venv/</guid>
      <description>venv模块支持使用自己的站点目录创建轻量级“虚拟环境”，可选择与系统站点目录隔离。每个虚拟环境都有自己的Python二进制文件（与用于创建此环境的二进制文件的版本相匹配），并且可以在其站点目录中拥有自己独立的已安装 Python 软件包集。
3.6 版后已移除: pyvenv 是 Python 3.3 和 3.4 中创建虚拟环境的推荐工具，不过 在 Python 3.6 中已弃用。
在 3.5 版更改: 现在推荐使用 venv 来创建虚拟环境。
创建venv虚拟环境 如果使用python2，则需要安装virtualenv模块
text 1 2 pip install virtualenv python -m virtualenv {name} python3内置了 venv 模块，可以直接使用
text 1 python3 -m venv {name} 进入虚拟环境
linux
text 1 venv\Scripts\activate windows
text 1 venv\Scripts\activate.bat 退出环境
text 1 2 venv\Scripts\deactivate.bat venv\Scripts\deactivate 使用venv环境安装软件报错 Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=&amp;lsquo;pypi.</description>
    </item>
    <item>
      <title>python中的signal</title>
      <link>https://www.161616.top/python-signal-handle/</link>
      <pubDate>Wed, 02 Jun 2021 00:00:00 +0000</pubDate>
      <guid>https://www.161616.top/python-signal-handle/</guid>
      <description>什么是信号 信号（signal）&amp;ndash; 进程间通讯的一种方式，也可作为一种软件中断的方法。一个进程一旦接收到信号就会打断原来的程序执行来按照信号进行处理。
简化术语，信号是一个事件，用于中断运行功能的执行。信号始终在主Python线程中执行。对于信号，这里不做详细介绍。
Python封装了操作系统的信号功能的库 singal 的库。singal 库可以使我们在python程序中中实现信号机制。
https://zh.wikipedia.org/wiki/Unix%E4%BF%A1%E5%8F%B7)
Python的信号处理 首先需要了解Python为什么要提供 signal Library。信号库使我们能够使用信号处理程序，以便当接收信号时都可以执行自定义任务。
Mission：当接收到信号时执行信号处理方法
可以通过使用 signal.singal() 函数来实现此功能
Python对信号的处理 通常情况下Python 信号处理程序总是会在主 Python 主解析器的主线程中执行，即使信号是在另一个线程中接收的。 这意味着信号不能被用作线程间通信的手段。 你可以改用 threading 模块中的同步原语。
Python信号处理流程，需要对信号处理程序（signal handling ）简要说明。signal handling 是一个任务或程序，当检测到特定信号时，处理函数需要两个参数，即信号id signal number （Linux 中 1-64），与堆栈帧 frame。通过相应信号启动对应 signal handling ，signal.signal() 将为信号分配 处理函数。
如：当运行一个脚本时，取消，此时是捕获到一个信号，可以通过捕获信号方式对程序进行异步的优雅处理。通过将信号处理程序注册到应用程序中：
py 1 2 3 4 5 6 7 8 9 10 11 import signal import time def handler(a, b): # 定义一个signal handling print(&amp;#34;Signal Number:&amp;#34;, a, &amp;#34; Frame: &amp;#34;, b) signal.</description>
    </item>
    <item>
      <title>macos python安装mysqlapi集合</title>
      <link>https://www.161616.top/mac-mysqlapi/</link>
      <pubDate>Thu, 29 Apr 2021 00:00:00 +0000</pubDate>
      <guid>https://www.161616.top/mac-mysqlapi/</guid>
      <description>记录一下，接了一个python2 django1.x的项目，很老了导致很多扩展无法安装
os version：macos catalina python version: 2.7.18
而django后端使用sqllite以外需要对应客户端引擎，而安装时编译依赖C客户端即实际mysql组件。
使用的数据库后端。 内建的数据库后端有：
&amp;lsquo;django.db.backends.postgresql&amp;rsquo; &amp;lsquo;django.db.backends.mysql&amp;rsquo; &amp;lsquo;django.db.backends.sqlite3&amp;rsquo; &amp;lsquo;django.db.backends.oracle&amp;rsquo;
并且修改配置实例
text 1 2 3 4 5 6 7 8 9 10 DATABASES = { &amp;#39;default&amp;#39;: { &amp;#39;ENGINE&amp;#39;: &amp;#39;django.db.backends.mysql&amp;#39;, &amp;#39;USER&amp;#39;: &amp;#39;mydatabaseuser&amp;#39;, &amp;#39;NAME&amp;#39;: &amp;#39;mydatabase&amp;#39;, &amp;#39;TEST&amp;#39;: { &amp;#39;NAME&amp;#39;: &amp;#39;mytestdatabase&amp;#39;, }, }, } brew unlink mysql
error: command &amp;lsquo;gcc&amp;rsquo; failed with exit status 1 text 1 2 3 4 5 6 creating build/temp.macosx-10.9-x86_64-2.7 gcc -fno-strict-aliasing -fno-common -dynamic -arch x86_64 -g -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Dversion_info=(1,2,5,&amp;#39;final&amp;#39;,1) -D__version__=1.</description>
    </item>
  </channel>
</rss>
