---
url: /python.md
---
```md
python

生成器是一个函数，它返回一个对象（迭代器），我们可以对其进行迭代（一次一个值）

如何在 Python 中创建生成器？
在 Python 中创建生成器非常简单。 就像使用 yield 语句而不是 return 语句定义普通函数一样容易。

如果一个函数包含至少一个 yield 语句(它可能包含其他 yield 或 return 语句)，那么它就成为一个生成器函数。yield 和 return 都将从函数返回一些值。

不同之处在于，当 return 语句完全终止一个函数时，yield 语句会暂停该函数保存其所有状态，然后在后续调用时继续执行

生成器函数与常规函数之间的区别
这是生成器函数与常规函数的不同之处。

生成器函数包含一个或多个 yield 语句。

调用时，它返回一个对象（迭代器），但不会立即开始执行。

像**iter**()和**next**()这样的方法会自动实现。因此，我们可以使用 next()来遍历项目。

一旦函数产生了结果，函数就会暂停，控制就会转移给调用者。

局部变量及其状态在连续调用之间被记住。

最后，当函数终止时，在进一步调用时会自动引发 StopIteration。

这是一个示例，用于说明上述所有要点。我们有一个 my_gen()由几个 yield 语句命名的生成器函数

在 Python 中，使用 yield 的函数被称为生成器函数（generator function）。 用于从一个函数中返回一个生成器（generator）。

生成器是一种特殊类型的迭代器，它允许你延迟计算结果，这在处理大数据或者创建复杂数据结构时特别有用，因为你不需要一次性将所有的数据都存储在内存中
生成器有两种方法：next()和 send()，都可以调用生成器

使用 yield 把函数变成一个生成器函数，每次只产生一个元素，就能节省很多开销了

scrapy shell https://maoyan.com/board/4?offset=0

https://bot.sannysoft.com/

抓取微博和今日头条所有可获取的有关“智慧养老的数据” 1.利用 python 进行数据预处理，滤除重复无用信息（最好可以看到具体数字），刻画人群画像 2.进行中文分词并提取高频词制作词云 3.进行情感分析，判断所包含的正负面情感词个数，分析大众对于智慧养老的看法 4.基于 LDA 模型挖掘数据集中的潜在主题。

tushare a022dd80c02b0ea4ce9c232088a9ae61fd08250b81b19561b4d37dc5
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
修改 python310.\_pth，去掉 import site 前的注释，这样 "\lib\site-packages"才会加入 sys.path 中
Lib\site-packages

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn
python get-pip.py

pip freeze > requirements.txt

pip install virtualenv
pip install --no-cache-dir virtualenvwrapper

pip install --no-cache-dir -U -r .\requirements.txt

git clone https://github.com/secdev/scapy
pip cache purge
pip config set global.cache-dir false
```

### pyinstaller

```python
pyinstaller --add-data="D:\Program Files (x86)\python3\Lib\site-packages\pyecharts;pyecharts"  -F .\main.py\ --clean
pyinstaller --add-data="D:\Program Files (x86)\python3\Lib\site-packages\fake_useragent;fake_useragent" --add-data="xxx;."  -F .\main.py\ --clean -i "xxxx" -n="xxx"

powershell
$env:PLAYWRIGHT_BROWSERS_PATH="0"
$env:PLAYWRIGHT_SKIP_BROWSER_GC="1"
 playwright install chromium
```

### pip.conf

```

[global]
index-url=http://pypi.douban.com/simple
extra-index-url=http://mirrors.aliyun.com/pypi/simple
                https://pypi.tuna.tsinghua.edu.cn/simple/
                http://pypi.mirrors.ustc.edu.cn/simple/
[install]
trusted-host=
             pypi.douban.com
             mirrors.aliyun.com
             pypi.tuna.tsinghua.edu.cn
             pypi.mirrors.ustc.edu.cn
disable-pip-version-check = true
timeout = 60

pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip config set install.trusted-host mirrors.aliyun.com

```

### http server

python -m http.server 8080

## conda

conda config --add channels http://mirrors.aliyun.com/anaconda/pkgs/main

conda config --add channels http://mirrors.aliyun.com/anaconda/pkgs/r

conda config --add channels http://mirrors.aliyun.com/anaconda/pkgs/msys2

conda config --set show_channel_urls yes

## 可以做什么

json.dumps将python对象格式化成json字符（将dict转化成str）
json.loads将json字符串解码成python对象（将str转化成dict）

## case

```python
                match xxx:
                    case "xxx":
                        xxx

```

## 升级所有依赖

```

pip-review

```

## f-string

```

print(f"{name:_>20}: {value}")
print(f"{name:#<20}: {value}")
print(f"{name:|^20}: {value}")
# 结果
_________________var: 100
var#################: 100
||||||||var|||||||||: 100

```

## web

flask

## 实用

```

d1 = {'a': 1, 'b': 2}
d2 = {'b': 3, 'c': 4}
merged = {**d1, **d2}  # 结果: {'a': 1, 'b': 3, 'c': 4}


set1 = {1, 2, 3}
set2 = {2, 3, 4}
unique_elements = set1.symmetric_difference(set2)  # 结果: {1, 4}


 from functools import partial
def power(base, exponent):
 return base ** exponent
square = partial(power, exponent=2)
print(square(5))  # 结果: 25

python -m http.server 80

python  -m pyftpdlib -p 2121 -w -u admin -P admin -d .


```

## pandas

```
pandas筛选不包含字符串, 不在列表内
不包含字符串
df[(df[‘订单号’].str.contains(‘S0’) == False)]
不在列表内
df[(~ df[‘订单号’].isin([‘S0’, ‘S1’]) )]

```

## 进化

```

Level 1:

    def xxx():

         name="xxx"

Level 2:

  import argparse
  python xxx.py -n

Level 3:

   from prompt_toolkit.shortcuts import radiolist_dialog ,yes_no_dialog

level 4:

    pyinstaller

    ./xxx.exe


```

## 海象运算符

```
海象运算符的符号是:=，允许在表达式内部进行赋值操作

variable := expression

```

## Parquet 文件格式

```

Parquet 是 Apache 开源项目之一，具有以下特点：

列式存储：相比于行式存储（如 CSV），Parquet 在列式查询、压缩和数据扫描方面有更好的性能。

高效压缩：Parquet 支持多种压缩算法，如 Snappy、Gzip 和 Zstd，提高存储效率。

Schema 自描述：Parquet 文件包含 schema 元数据，支持复杂数据类型（如嵌套和列表）
。
支持分区：在大规模数据处理中，Parquet 支持按照不同列进行分区存储，提高查询效率


```
