Python

Python入门

1、编码规范

https://www.runoob.com/w3cnote/google-python-styleguide.html

2、入门

https://www.runoob.com/python3/python3-tutorial.html
https://www.runoob.com/python/python-tutorial.html

3、Python

https://www.python.org/
https://docs.python.org/3.9/library/

The Python Standard Library

https://docs.python.org/3.9/library/index.html

安装

解压xz

XZ compressed source tarball

xz -d Python-3.10.2.tar.xz 
tar xvf Python-3.10.2.tar
cd Python-3.10.2/

Linux安装

https://www.python.org/downloads

# yum install gcc libffi-devel zlib* openssl-devel 
# tar -zxvf Python-x.x.x.tgz 
# cd Python-x.x.x
# make clean && make distclean 
# ./configure 
# make && make install

依赖

python需要使用zlib和openssl,所以确保已经安装了以下两个库

yum install -y zlib-devel openssl-devel

python3.10.1异常,需要升级到3.10.2

<frozen importlib._bootstrap>:914: ImportWarning: _SixMetaPathImporter.find_spec() not found; falling back to find_module()
<frozen importlib._bootstrap>:671: ImportWarning: _SixMetaPathImporter.exec_module() not found; falling back to load_module()

下载3.10.2的源代码,重新编译安装

安装模块

查看pip版本
python3 -m pip --version

查看已经安装了哪些模块

python3 -m pip list
Package      Version
------------ -------
et-xmlfile   1.1.0
kafka-python 2.0.2
openpyxl     3.0.9
pip          22.0.3

pip3 install kafka-python

更新模块

python3 -m pip install --upgrade pip setuptools wheel

查看模块消息

pip show xlswriter

编码细节

输出时分秒+毫秒 / 可选参数

def info(msg, ex=None):
    if ex:
        print('[{}]-{}'.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'), msg), ex)
    else:
        print('[{}]-{}'.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'), msg))

耗时:

start = time.time()
recv_data(xxx)
end = time.time()
seconds = round((end - start) * 1000)
print('recv_data used:{} ms'.format(seconds))

traceback

https://docs.python.org/3/library/traceback.html

临时文件

https://docs.python.org/3.9/library/tempfile.html
https://docs.python.org/3.9/library/tempfile.html#tempfile-examples

tempfile.NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True, *, errors=None)

Python三元运算

result = [on_true] if [expression] else [on_false]

Python引用自己写的模块举例

例1

import os
import sys
import time
sys.path.append(os.getcwd())

from base.wy_dir import list_dir

例2

import os
import sys
import time
import random
from math import ceil

from openpyxl.chart.label import DataLabelList
from openpyxl.styles import Alignment, Font
from openpyxl import Workbook
from openpyxl.chart import (
    ScatterChart,
    Reference,
    Series, BarChart,
)
from openpyxl.chart.marker import (
    Marker
)

sys.path.append(os.getcwd())

from base.dirs import get_filename
from base.base import log_time

多线程/多进程

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED, ProcessPoolExecutor

def aaa():
    start = time.time()

    files = list_dir(input_dir, '.log', True)  
    for f in files:
        print(f)
        
    # 使用线程池或者进程池
    # thread_pool = ThreadPoolExecutor(max_workers=4, thread_name_prefix="pir_")
    thread_pool = ProcessPoolExecutor(max_workers=4)
    # for input_path in files:
    #     thread_pool.submit(process_data, input_path)

    all_task = [thread_pool.submit(process_file,input_path,para1,para2) for input_path in files]
    wait(all_task, return_when=ALL_COMPLETED)

    # 反馈参数
    end = time.time()
    print('处理完成,用时:{} ms'.format(round((end - start) * 1000)))

线程

如果执行的代码可能会阻塞后面代码执行,考虑使用线程处理

import threading

thread_open_file = OpenFileThread(in_file_name)
thread_open_file.start()

class OpenFileThread(threading.Thread):
    def __init__(self, file_name):
        super().__init__()
        self.file_name = file_name

    def run(self):
        # print(self.file_name)
        os.system(self.file_name)

django

https://www.djangoproject.com/start/

Getting started with Django

Depending how new you are to Django, you can try a tutorial, or just dive into the documentation.

Want to learn more about Django? Read the overview to see whether Django is right for your project.

DJANGO OVERVIEW

Install Django

Before you can use Django, you’ll need to install it. Our complete installation guide covers all the possibilities; this guide will get you to a simple, minimal installation that’ll work while you walk through the introduction.

DJANGO INSTALLATION GUIDE

Write your first Django app

Installed Django already? Good. Now try this tutorial, which walks you through creating a basic poll application. It’s got two parts:

A public site that lets people view polls and vote in them.
An administrative interface that lets you add, change and delete polls.

TAKE THE TUTORIAL

Sharpen your skills

The official Django documentation covers everything you need to know about Django (and then some).

READ THE DOCS

https://docs.djangoproject.com/en/4.0/topics/install/#installing-distribution-package

Installing the development version

git clone https://github.com/django/django.git
python3 -m pip install -e django/

Python MySQL

https://cdn.mysql.com//Downloads/Connector-Python/mysql-connector-python-8.0.28-windows-x86-32bit.msi

https://dev.mysql.com/downloads/connector/python/

https://pypi.org/project/mysqlclient/

pip install mysqlclient

Python中的*args和**kwargs

*args,以list的形式接收参数值

**kwargs以dict的key-value形式接收参数名和参数数值

参数名为*用于划分位置变量和关键字变量

def func(va, vb, *, vc, vd):
    pass

func(1, 2, vc=3, vd=4)

字符串格式化

format()
它会用传入的参数依次替换字符串内的占位符{0}、{1}……,不过这种方式写起来比%要麻烦得多:

>>> 'Hello, {0}, 成绩提升了 {1:.1f}%'.format('小明', 17.125)
'Hello, 小明, 成绩提升了 17.1%'
f-string

还有一种格式化字符串的方法是使用以f开头的字符串,称之为f-string,它和普通字符串不同之处在于,字符串如果包含{xxx},就会以对应的变量xxx替换:

>>> r = 2.5
>>> s = 3.14 * r ** 2
>>> print(f'The area of a circle with radius {r} is {s:.2f}')
The area of a circle with radius 2.5 is 19.62

定义默认参数要牢记一点:默认参数必须指向不变对象!

当函数参数默认值是一个可变的变量时(类似指针或者传址)就会收到下面的提示信息

Default argument value is mutable

函数的参数可能只被初始化一遍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值