我有一个比这个更复杂的问题。但我认为这打破了它。我有一些通用结构,它具有三个构造函数来获取具体类型的结构。除了构造函数之外,它们都具有相同的通用方法。我想要的是这样的:
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 …
我想包含依赖项的功能,这些功能取决于我的库中功能的激活。如果我使用 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)
目前这可能吗?
所以考虑这个片段
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 聚合中?
我在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
所以我的问题是,(+++)运算符有什么用处,因为它们在解析成功时似乎都没有完成.