小编rit*_*e46的帖子

Rust Pyo3 绑定:如何在通用 Rust 类型上重用 Python 方法

我有一个比这个更复杂的问题。但我认为这打破了它。我有一些通用结构,它具有三个构造函数来获取具体类型的结构。除了构造函数之外,它们都具有相同的通用方法。我想要的是这样的:

use pyo3::prelude::*;

#[pyclass]
struct AnyVec<T> {
    vec_: Vec<T>,
}

// General methods which
#[pymethods]
impl<T> AnyVec<T> {
    fn push(&mut self, v: T) {
        self.vec_.push(v)
    }

    fn pop(&mut self, v: T) -> T {
        self.vec_.pop()
    }
}

#[pymethods]
impl AnyVec<String> {
    #[new]
    fn new() -> Self {
        AnyVec { vec_: vec![] }
    }
}

#[pymethods]
impl AnyVec<f32> {
    #[new]
    fn new() -> Self {
        AnyVec { vec_: vec![] }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译这个时。pyo3警告我它不能使用泛型。error: #[pyclass] cannot have generic parameters …

python rust pyo3

6
推荐指数
0
解决办法
935
查看次数

货物条件依赖功能

我想包含依赖项的功能,这些功能取决于我的库中功能的激活。如果我使用 feature 进行编译"serde",我想ndarray使用"serde"support 进行安装。并且默认应该是默认ndarray安装。

我想要这样的东西Cargo.toml


[features]
include-serde = ["ndarray-with-serde"]

[dependencies]
ndarray = { version = "0.x" }
ndarray-with-serde = { version = "0.x", features=["serde"] }
Run Code Online (Sandbox Code Playgroud)

目前这可能吗?

rust rust-cargo

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

Polars 获取列值最大的分组行

所以考虑这个片段

import polars as pl

df = pl.DataFrame({'class': ['a', 'a', 'b', 'b'], 'name': ['Ron', 'Jon', 'Don', 'Von'], 'score': [0.2, 0.5, 0.3, 0.4]})
df.groupby('class').agg([pl.col('score').max()])
Run Code Online (Sandbox Code Playgroud)

这给了我:

class score
  b     0.4
  a     0.5
Run Code Online (Sandbox Code Playgroud)

但我想要与最高分数相对应的组的整行。我可以与原始数据框进行连接,例如

sdf = df.groupby('class').agg([pl.col('score').max()])
sdf.join(df, on=['class', 'score'])
Run Code Online (Sandbox Code Playgroud)

要得到

class  score  name
  a     0.5    Jon
  b     0.4    Von
Run Code Online (Sandbox Code Playgroud)

有没有办法避免连接并将名称列包含在 groupby 聚合中?

python python-polars

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

我们如何对极坐标中的时间序列进行重新采样

我想将表达式与 groupby 一起使用,每月进行下采样,因为下采样函数将被弃用。有没有一种简单的方法可以做到这一点,datetime.timedelta 仅适用于天数及更短的时间。

python python-polars

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

(+++)运算符在Text.ParserCombinators.ReadP(Haskell)中的作用是什么

我在ReadP包中遇到对称选择(+++)运算符时遇到问题.

https://www.haskell.org/cabal/release/cabal-1.22.8.0/doc/API/Cabal/Distribution-Compat-ReadP.html

由于我在互联网上找不到很多ReadP lib的例子,我只是在尝试一些东西.

我注意到有偏见的选择按预期工作.倾向于解析左边,如果失败则选择正确的解析器.

fmap Just (munch1 dianaFloat) <++ return Nothing

如果我插入对称选择运算符.算法没有完成.

fmap Just (munch1 dianaFloat) +++ return Nothing

如果我让左手失败,他们都会产生相同的结果:

fpail) <++ return Nothing

==

fpail) +++ return Nothing

所以我的问题是,(+++)运算符有什么用处,因为它们在解析成功时似乎都没有完成.

parsing haskell

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

标签 统计

python ×3

python-polars ×2

rust ×2

haskell ×1

parsing ×1

pyo3 ×1

rust-cargo ×1