def

python中单下划线和双下划线的区别

对着背影说爱祢 提交于 2021-02-06 08:02:43
1.单下划线 在python中单下划线代表私有,但也仅仅是名义上的私有,只是一种规范,告诉人们不要在外部使用它。但实际上python没有真正意义上的私有,我们一样可以在外部去调用私有方法或属性。 class BaseForm(StrAndUnicode): def _get_errors(self): ''' Returns an ErrorDict for the data provided for the form :return: ''' if self._errors is None: self.full_clean() return self._errors errors = property(_get_errors) 该代码片段来自Django源码(django/forms/forms.py)。这段代码的设计就是errors属性是对外API的一部分,如果你想获取错误详情,应该访问errors属性,而不是(也不应该)访问_get_errors方法。实际上我们仍然可以使用对象在外部调用它。 baseform = BaseForm() baseform._get_errors() 2.双下划线 双下划线使用来避免父类方法被子类方法覆盖的。双下划线方法的本质是在方法前加了_类名,我们可以使用对象._类名__方法名(),来在外部调用它。 class A(object): def

大型fastapi项目实战 高并发请求神器之aiohttp(上) [建议收藏]

拟墨画扇 提交于 2021-02-06 05:31:20
点击“ python编程军火库 ”,“置顶”公众号 重磅干货,第一时间送达 大型fastapi项目实战 高并发请求神器之aiohttp(上) [建议收藏] aiohttp介绍及安装 1.背景介绍 2.aiohttp 是什么 3.aiohttp 核心功能 4.aiohttp 库安装 aoihttp 业务核心功能 1. 发起 get 请求 2. 发起 post 请求 3. 向 url 中传递参数 4. 向目标服务器上传文件 5. 设置请求超时 aoihttp 爬虫核心功能 1. 自定义cookie 2. 在多个请求之间共享cookie 3. 自定义请求头 4. SSL验证警告问题 5. 代理问题 aoihttp 连接池 1.使用连接器 2.限制连接池的容量 小结: 大型fastapi项目实战 高并发请求神器之aiohttp(上) [建议收藏] aiohttp介绍及安装 1.背景介绍 在 Python 众多的 HTTP 客户端中,最有名的莫过于 requests、aiohttp 和 httpx。在不借助其他第三方库的情况下,requests 只能发送同步请求;aiohttp 只能发送异步请求;httpx 既能发送同步请求,又能发送异步请求。在并发量大的情况下,如何高效的处理数据,异步是我们的优选,今天我们主要详解的是在生产环境广泛使用的 aiohttp。 2.aiohttp 是什么

大型fastapi项目实战 高并发请求神器之aiohttp(下)

点点圈 提交于 2021-02-05 12:14:49
点击“ python编程军火库 ”,“置顶”公众号 重磅干货,第一时间送达 大型fastapi项目实战 高并发请求神器之aiohttp(下) 1. 上节代码简单解释 2. aiohttp 性能测试 3. 解决 aiohttp 不支持 HTTPS 代理 总结 大型fastapi项目实战 高并发请求神器之aiohttp(下) 1. 上节代码简单解释 基于上节给出的代码实例,直接使用代码就可以工作,本节我们注解一下核心代码 # -*- encoding: utf-8 -*- import asyncio import aiohttp async def main (): async with aiohttp.ClientSession() as session: async with session.get( 'http://www.baidu.com' ) as resp: print (resp.status) res = await resp.text() print (res[: 100 ]) if __name__ == '__main__' : # 注意: # python3.7+ 支持写法 # asyncio.run(main()) # python3.6及以下版本写法 event_loop = asyncio.get_event_loop() result = event

机器学习--matplotlib绘制各种图表

五迷三道 提交于 2021-02-03 13:37:21
机器学习三剑客:numpy、pandas、matplotlib NumPy系统是Python的一种开源的数值计算扩展。这种工具可用来存储和处理大型矩阵。 pandas 是基于numpy的一种工具,该工具是为了解决数据分析任务而创建的。 Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。 柱状图bar from matplotlib import pyplot as plt import matplotlib # 显示图表,仅限于jupyter使用 % matplotlib inline # 指定默认字体 matplotlib.rcParams[ ' font.sans-serif ' ] = [ ' SimHei ' ] # 第一个参数:索引 # 第二个参数:高度 参数必须对应否则报错 plt.bar(range(5),[100,200,300,400,500],color= ' red ' ) plt.xticks(range( 5),[ ' A ' , ' B ' , ' C ' , ' D ' , ' E ' ]) plt.xlabel( ' 姓名 ' ) plt.ylabel( ' 得分 ' ) plt.title( ' 学生得分 ' ) # 或显示图标Plt.show() 饼图pie labels =

python3_unittest单元测试框架

瘦欲@ 提交于 2021-02-03 09:06:38
看见英文懵逼,强迫学习英语 The Unittest suppots test automation,sharing of setup and shutdown code of tests, aggregation of tests into collections, and independence of the tests from the reporting framework (支持测试自动化,为测试共享设置和关闭代码,将测试集合到集合中,以及从报告框架中独立测试。) To achieve this,unittest supports some important concepts in an object_oriented way: (为了实现这一点, unittest 以面向对象的方式支持一些重要的概念:) 整理结构:unittest库提供了testcase,test suites,test fixtures, test runner: test fixture:   A test fixture represents the preparation needed to perform one or more tests, and any associate cleanup actions. This may involve, for example, creating

[LeetCode] 670. Maximum Swap 最大置换

ぃ、小莉子 提交于 2021-02-03 09:04:24
Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get. Example 1: Input: 2736 Output: 7236 Explanation: Swap the number 2 and the number 7. Example 2: Input: 9973 Output: 9973 Explanation: No swap. Note: The given number is in the range [0, 108] 解法:从后向前扫,遇到比max_value 大的就记录这个最大数的值和位置,继续向前扫,遇到小于这个max_value时,就记录这个交换位置, 因为越往左扫数位越高,交换后整个数字值越大。 Java: class Solution { public int maximumSwap(int num) { char[] digits = Integer.toString(num).toCharArray(); int[] buckets = new int[10]; for (int i = 0; i <

Spark(十四)SparkStreaming的官方文档

牧云@^-^@ 提交于 2021-02-03 05:50:25
一、SparkCore、SparkSQL和SparkStreaming的类似之处 二、SparkStreaming的运行流程 2.1 图解说明 2.2 文字解说 1、我们在集群中的其中一台机器上提交我们的Application Jar,然后就会产生一个Application,开启一个Driver,然后初始化SparkStreaming的程序入口StreamingContext; 2、Master会为这个Application的运行分配资源,在集群中的一台或者多台Worker上面开启Excuter,executer会向Driver注册; 3、Driver服务器会发送多个receiver给开启的excuter,(receiver是一个接收器,是用来接收消息的,在excuter里面运行的时候,其实就相当于一个task任务) 4、receiver接收到数据后,每隔200ms就生成一个block块,就是一个rdd的分区,然后这些block块就存储在executer里面,block块的存储级别是Memory_And_Disk_2; 5、receiver产生了这些block块后会把这些block块的信息发送给StreamingContext; 6、StreamingContext接收到这些数据后,会根据一定的规则将这些产生的block块定义成一个rdd; 三

python函数知识点(详解匿名函数)

蓝咒 提交于 2021-02-03 04:54:33
Python函数是组织好的、单一的、具有独立功能模块的代码块。 函数能提高应用的模块性,和代码的重复利用率。Python提供了许多内建函数,比如print()。但你也可以自己创建函数,这被叫做用户自定义函数。 定义一个函数 你可以定义一个由自己想要功能的函数,以下是简单的规则: 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 () 。 任何传入参数和自变量必须放在圆括号中间。圆括号之间可以用于定义参数。 函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。(使用'''xxx''') 函数内容以冒号起始,并且缩进。 return [表达式] 结束函数,选择性地返回一个值给调用方。不带表达式的return相当于返回 None。 实例(参数传递): def printme ( str ): ''' 打印传入的字符串到标准显示设备上''' return str printme("aaa")#输出结果为"aaa"如果没有填写参数会报错: 可变类型与不可变类型: 在 python 中,strings, tuples, 和 numbers 是不可更改的对象,而 list,dict 等则是可以修改的对象。 不可变类型: 变量赋值 a=5 后再赋值 a=10 ,这里实际是新生成一个 int 值对象 10,再让 a 指向它,而 5 被丢弃,不是改变a的值,相当于新生成了a。

【leetcode】Basic Calculator III

妖精的绣舞 提交于 2021-02-03 01:02:06
题目如下: Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . The expression string contains only non-negative integers, +, -, *, / operators , open ( and closing parentheses ) and empty spaces . The integer division should truncate toward zero. You may assume that the given expression is always valid. All intermediate results will be in the range of [-2147483648, 2147483647]. Some examples: "1 + 1" = 2 " 6-4 / 2 " = 4 "2*(5+5*2)/3+(6/2+8)" = 21

TensorFlow(九):卷积神经网络

三世轮回 提交于 2021-02-02 16:31:50
一:传统神经网络存在的问题 权值太多,计算量太大 权值太多,需要大量样本进行训练 二:卷积神经网络(CNN) CNN通过感受野和权值共享减少了神经网络需要训练的参数个数。 三:池化 四:卷积操作 五:CNN结构 六:基于卷积神经网络的手写数字识别 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # 载入数据集 mnist=input_data.read_data_sets( ' MNIST_data ' ,one_hot= True) # 每个批次的大小 batch_size=100 # 计算一共有多少个批次 n_batch=mnist.train.num_examples// batch_size # 初始化权值 def weight_variable(shape): initial =tf.truncated_normal(shape,stddev=0.1) # 生成一个截断的正态分布 return tf.Variable(initial) # 初始化偏置 def bias_variable(shape): initial =tf.constant(0.1,shape= shape) return tf.Variable(initial) # 卷积层 def