一摞Python风格的纸牌 后端开发

import collections
from random import choice

# namedtuple 是一个可命名的元祖,它可以让你使用属性式的标签来访问元祖中的元素,而不是通过索引
Card = collections.namedtuple('Card', ['rank', 'suit'])

class FrenchDesk:
    ranks = [str(n) for n in range(2, 11)] + list('JQKA')
    suite = '黑桃 方块 梅花 红星'.split()

    def __init__(self):
        self._cards = [Card(rank, suit) for suit in self.suite
                       for rank in self.ranks]

    def __len__(self):
        return len(self._cards)

    def __getitem__(self, position):
        return self._cards[position]

# 手工设置一张牌
beer_card = Card('7', '方块')
print("手工设置一张牌:")
print(beer_card.rank, beer_card.suit)

deck = FrenchDesk()
print(f"总共有 {len(deck)} 张牌。")

print(deck[0])
print(deck[-1])

# 随机选一张牌
# 函数random.choice 可以从序列中随机获取一项的函数
print("随机获取的牌:")
print(choice(deck))
print(choice(deck))
print(choice(deck))

Fluent Python 1-1

标签: python

明算软件 发布于  2024-4-28 21:58 

FastAPI请求体模型内的属性验证 后端开发

使用pydantic.Field


class UserModel(BaseModel):
    username: str = Field(..., min_length=3)
    description: Optional[str] = Field(None, max_lenght=128)
    gender: Gender

明算软件 发布于  2024-1-24 16:59 

FastAPI路径参数的验证 后端开发

路径参数都是必须项


@app.get('/users/{user_id}')
async def get_user(user_id: int = Path(...,
                                       title="The user id"
                                       ge=1,
                                       le=10000)):
    return {'user': f'This isi the user for {user_id}'}

明算软件 发布于  2024-1-22 14:46 

FastAPI函数的参数识别规则 后端开发

  1. 如果在路径参数中定义了,那么匹配为路径参数;
  2. 如果参数的类型为int,str之类的基本类型,则为查询参数;
  3. 如果是pydantic的模型类型,则为请求体。

明算软件 发布于  2024-1-21 18:14 

FastAPI为请求体中的数据定义模型 后端开发


from pydantic import BaseModel

class UserModel(BaseModel):
    username: str
    description: Optional[str] = None

明算软件 发布于  2024-1-21 16:20 

Python 类型提示简介 后端开发

Python 3.6+ 版本加入了对"类型提示"的支持。
这些"类型提示"是一种新的语法(在 Python 3.6 版本加入)用来声明一个变量的类型。
通过声明变量的类型,编辑器和一些工具能给你提供更好的支持。
这只是一个关于 Python 类型提示的快速入门 / 复习。它仅涵盖与 FastAPI 一起使用所需的最少部分...实际上只有很少一点。
整个 FastAPI 都基于这些类型提示构建,它们带来了许多优点和好处。
但即使你不会用到 FastAPI,了解一下类型提示也会让你从中受益。

def get_full_name(first_name: str, last_name: str):
    full_name = first_name.title() + " " + last_name.title()
    return full_name

print(get_full_name('john', 'dog'))

支持简单类型:

  • int
  • float
  • bool
  • bytes

嵌套类型

from typing import List

def process_items(items: List[str]):
    for item in items:
        print(item)

这表示:变量 items 是一个 list ,并且这个列表里的每一个元素都是 str 。

元组和集合

from typing import Set, Tuple

def process_items(items_t: Tuple[int, int, str], items_s: Set[bytes]):
    return items_t, tiems_s

字典
定义 dict 时,需要传入两个子类型,用逗号进行分隔。

标签: python

明算软件 发布于  2024-1-4 15:49 

Python字典的定义 后端开发

定义一个新字典:
1、使用花括号定义:

{'one':1, 'two':2}

2、使用类型构造器:

dict(one=1, two=2)

3、使用字典推导式:

{x:x**2 for x in range(10)}
标签: python

明算软件 发布于  2023-3-23 21:25 

树莓派配置国内源 后端开发

在配置树莓派的时候,使用官方源有时下载速度特别慢,因此需要换成国内源。我尝试换过中科大源和清华园。总体感觉比较玄学,官方源,科大源,清华源在下载的时候都或多或少的存在一个问题,在此记录一下树莓派的配置国内源过程,下次稍微少点麻烦。

1.需要用到的地址

科大镜像网站:http://mirrors.ustc.edu.cn/help/raspbian.html

清华镜像网站:https://mirrors.tuna.tsinghua.edu.cn/help/raspbian/

2.配置国内源:

sudo nano /etc/apt/sources.list              #打开文件

将文件内容用以下内容替换,换上科大源

deb https://mirrors.ustc.edu.cn/debian/ buster main contrib non-free
# deb-src http://mirrors.ustc.edu.cn/debian buster main contrib non-free
deb https://mirrors.ustc.edu.cn/debian/ buster-updates main contrib non-free
# deb-src http://mirrors.ustc.edu.cn/debian buster-updates main contrib non-free
deb https://mirrors.ustc.edu.cn/debian-security buster/updates main contrib non-free
# deb-src http://mirrors.ustc.edu.cn/debian-security/ buster/updates main non-free contrib

#将文件内容用以下内容替换,换上清华源(针对aarch64用户)
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free

更新

 sudo apt-get update
 sudo apt-get upgrade                        #更新

明算软件 发布于  2022-12-6 13:07 

修复树莓派鼠标移动缓慢迟滞问题 后端开发

在使用树莓派过程中插入usb鼠标会发现有很多品牌的鼠标都会出现移动缓慢迟滞现象,就连我也不例外,碰到好多次刷好系统都需要进行小小的修改才可以正常使用。

问题描述:
鼠标移动缓慢,拖动有迟滞现象。

解决方法:

sudo nano /boot/cmdline.txt

在最后插入一个空格并添加内容:
us

usbhid.mousepoll=0

重启系统。


明算软件 发布于  2022-12-6 12:59 

ubuntu全版本通用换源教程 后端开发

我使用树莓派安装Ubuntu系统,用国内源更新

每个版本的源都是不同的,

如果源的版本不同就会出现很多的错误,比如gcc缺少依赖项等

首先打开终端输入

lsb_release -a

查看版本的代号和版本

release就是该ubuntu的版本号

codename就是该版本的代号

然后打开源文件:

sudo vim /etc/apt/sources.list
或者
sudo nano /etc/apt/sources.list

如果显示 vim:没有这个命令 就将vim换成vi或下载vim

sudo apt install vim

按i进入编辑模式

将原来的内容全部删除

将源地址加入进去:

deb http://mirrors.aliyun.com/ubuntu/ codename main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ codename main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ codename-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ codename-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ codename-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ codename-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ codename-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ codename-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ codename-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ codename-backports main restricted universemultiverse

codename就是课该版本的代号,比如说我的是22.10版本的(kinetic),我的源就是

deb https://mirrors.ustc.edu.cn/ubuntu/ kinetic main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ kinetic main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ kinetic-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ kinetic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ kinetic-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ kinetic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ kinetic-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ kinetic-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ kinetic-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ kinetic-proposed main restricted universe multiverse

ubuntu中复制粘贴的方法:

shift + insert ,ctrl + insert
直接鼠标左键选中要复制的命令,然后在需要粘贴的地方按一下鼠标滚轮即可

之后按esc退出编辑模式

输入:wq

保存并退出

最后更新缓存和升级

sudo apt-get update
sudo apt-get upgrade # 此步骤并非必要,但可以加快以后的下载速度

重启

就完成了


明算软件 发布于  2022-12-6 11:09