小编Ste*_*fke的帖子

按值对HashMap数据进行排序

我想按Rust中的值对HashMap数据进行排序(例如,在字符串中计算字符频率时).

我正在尝试做的Python相当于:

count = {}
for c in text:
    count[c] = count.get('c', 0) + 1

sorted_data = sorted(count.items(), key=lambda item: -item[1])

print('Most frequent character in text:', sorted_data[0][0])
Run Code Online (Sandbox Code Playgroud)

我对应的Rust代码如下所示:

// Count the frequency of each letter
let mut count: HashMap<char, u32> = HashMap::new();
for c in text.to_lowercase().chars() {
    *count.entry(c).or_insert(0) += 1;
}

// Get a sorted (by field 0 ("count") in reversed order) list of the
// most frequently used characters:
let mut count_vec: Vec<(&char, &u32)> = count.iter().collect();
count_vec.sort_by(|a, b| b.1.cmp(a.1)); …
Run Code Online (Sandbox Code Playgroud)

rust

29
推荐指数
3
解决办法
7406
查看次数

如何在 FastAPI 中为 UploadFile 创建 OpenAPI 架构?

FastAPI 自动在 OpenAPI 规范中为UploadFile参数生成架构。

例如,这段代码:

from fastapi import FastAPI, File, UploadFile

app = FastAPI()


@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(..., description="The file")):
    return {"filename": file.filename}
Run Code Online (Sandbox Code Playgroud)

components:schemas在 OpenAPI 规范下生成此架构:

{
    "Body_create_upload_file_uploadfile__post": {
        "title": "Body_create_upload_file_uploadfile__post",
        "required":["file"],
        "type":"object",
        "properties":{
            "file": {"title": "File", "type": "string", "description": "The file","format":"binary"}
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如何明确指定 UploadFiles(或至少其名称)的架构?

我已经阅读了 FastAPIs 文档并搜索了问题跟踪器,但一无所获。

python openapi fastapi

9
推荐指数
2
解决办法
1254
查看次数

Python 类型:使用类变量的值作为(mixin)方法的返回类型

概括

如何使用类变量的值(这是一个类对象)作为 Python 类型 / mypy 的(mixin)方法的返回类型?

这是一个最小的示例,您将在下面找到真正的、更复杂的用例:

from typing import Generic, Type, TypeVar


T = TypeVar('T')


class Base(Generic[T]):
    return_type: Type[T]
    value: T  # This attribute is only needed for this minimal example


class Mixin:
    def get(self: Base[T]) -> T:  # mypy: The erased type of self "Base" is not a supertype of its class "Mixin"
        return self.return_type(self.value)  # mypy: Too many arguments for "object"


class Concrete(Mixin, Base[int]):
    return_type = int

    def __init__(self):
        self.value = 3


c = Concrete()
x: …
Run Code Online (Sandbox Code Playgroud)

python type-hinting mypy

5
推荐指数
1
解决办法
4290
查看次数

(Py)Qt5:如何以编程方式设置当前项目时更新选择

用例:我有一个QListWidget.当用户选择任何行时,我想将当前项目和选择重置为第3行:

from PyQt5.QtWidgets import QApplication, QListWidget

app = QApplication([])
l = QListWidget()
l.setSelectionMode(QListWidget.SingleSelection)
l.addItems(list('abcde'))

def slot(current, previous):
    # sm = l.selectionModel()
    l.blockSignals(True)
    l.setCurrentRow(3)
    l.blockSignals(False)
    # sm.select(l.currentIndex(), sm.Select)  # Does not work
    # sm.setCurrentIndex(l.currentIndex(), sm.Select)  # Does not work

l.currentItemChanged.connect(slot)
l.show()
app.exec()
Run Code Online (Sandbox Code Playgroud)

上面的示例将第三行设置为当前行,但将所选行保持为用户单击的行.我已经尝试过的各种组合QItemModel.select()QItemModel.setCurrentIndex()和类似的东西,但没有奏效.我在谷歌或Qt论坛上也没有找到答案.

python pyqt qlistwidget pyqt5

2
推荐指数
1
解决办法
77
查看次数

Simpy - 何时使用 yield 以及何时调用该函数

我正在尝试使用 Simpy 来模拟在城市网格中移动汽车的一些行为。但是,我在概念上围绕何时使用类似的东西时遇到了一些麻烦

yield self.env.timeout(delay) 或者yield env.process(self.someMethod()) 与只是调用方法self.someMethod()

在非常理论的层面上,我理解yield语句和生成器如何应用于可迭代对象,但不太确定它与Simpy.

Simpy教程还相当密集。

例如:

class Car(object):
    def __init__(self, env, somestuff):
        self.env = env
        self.somestuff = somestuff

        self.action = env.process(self.startEngine())  # why is this needed?  why not just call startEngine()?

    def startEngine(self):
        #start engine here
        yield self.env.timeout(5) # wait 5 seconds before starting engine
        # why is this needed?  Why not just use sleep? 



env = simpy.Environment()
somestuff = "blah"
car = Car(env, somestuff)
env.run()
Run Code Online (Sandbox Code Playgroud)

python simpy

1
推荐指数
1
解决办法
1861
查看次数

标签 统计

python ×4

fastapi ×1

mypy ×1

openapi ×1

pyqt ×1

pyqt5 ×1

qlistwidget ×1

rust ×1

simpy ×1

type-hinting ×1