zip如果迭代的长度不相等,我正在寻找一个很好的方法来引发异常的几个迭代.
在迭代项是列表或具有len方法的情况下,此解决方案简洁明了:
def zip_equal(it1, it2):
if len(it1) != len(it2):
raise ValueError("Lengths of iterables are different")
return zip(it1, it2)
Run Code Online (Sandbox Code Playgroud)
但是,如果it1和it2是生成器,则前一个函数将失败,因为未定义长度TypeError: object of type 'generator' has no len().
我想这个itertools模块提供了一种简单的方法来实现它,但到目前为止我还没有找到它.我想出了这个自制的解决方案:
def zip_equal(it1, it2):
exhausted = False
while True:
try:
el1 = next(it1)
if exhausted: # in a previous iteration it2 was exhausted but it1 still has elements
raise ValueError("it1 and it2 have different lengths")
except StopIteration:
exhausted = True
# it2 must …Run Code Online (Sandbox Code Playgroud) 假设我有一个包含返回S3对象的函数的包:
new_myclass <- function() {
return(structure(list(a=1, b=2), class = "myclass"))
}
Run Code Online (Sandbox Code Playgroud)
我还有两个函数,它们接受一个myclass对象并分别返回一个HTML表示和一个对象的LaTeX表示.
myclass2html <- function(obj) { return("<p>MyClass object</p>")}
myclass2latex <- function(obj) { return("\begin{em}MyClass\end{em} object $x$")}
Run Code Online (Sandbox Code Playgroud)
我应该定义哪些功能/方法来提供一致,透明knitr和rmarkdown支持?我想支持.Rmd文件和.R文件,标题如下:
#'---
#' title: My document
#' output: pdf_output
#'---
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的方法通过该knit_print方法:
knit_print.myclass <- function(x, ...) {
rmarkdown_fmt <- rmarkdown::metadata$output
knitr_fmt <- knitr::opts_knit$get("out.format")
# should I use these heuristics with both variables?
if (rmarkdownfmt == "pdf_document") {
return(knitr::asis_output(myclass2latex(x)))
}
if (knitr_fmt %in% c("html", "markdown")) …Run Code Online (Sandbox Code Playgroud) 我在几个 C 项目(projectA和projectB)中使用介子,其中projectB链接到来自projectA. 在 中,我使用介子projectA/meson.build编写了一个文件,该文件已按预期安装。pkg-config projectA.pcjoin_paths(get_option('prefix'), get_option('libdir'), 'pkgconfig')
在projectB/meson.build我用来dependency('projectA')查找projectA.pc文件。
当我使用自定义安装前缀来构建时projectA,projectB构建时meson无法找到。有没有办法指定from ?projectA.pcprojectBPKG_CONFIG_PATHprojectB/meson.build
这个问题通过这个最小的例子重现:
项目A/介子.build:
project('projectA', 'c', version: '1')
pkg = import('pkgconfig')
pkg.generate(name : 'projectA',
description: 'ProjectA',
version: meson.project_version())
Run Code Online (Sandbox Code Playgroud)
项目B/介子.build:
project('projectB', 'c', version: '1')
dep = dependency('projectA')
Run Code Online (Sandbox Code Playgroud)
命令:
meson buildA projectA --prefix=$PWD/install
(cd buildA && ninja install)
#[0/1] Installing files.
#Installing /tmp/test/buildA/meson-private/projectA.pc to …Run Code Online (Sandbox Code Playgroud) 我试图dplyr::filter在一个函数中包装,当有多个filter条件时,它们作为向量或列表传递.看到这个最小的例子:
filter_wrap <- function(x, filter_args) {
filter_args_enquos <- rlang::enquos(filter_args)
dplyr::filter(x, !!!filter_args_enquos)
}
Run Code Online (Sandbox Code Playgroud)
当有一个条件我可以使它工作:
data(iris)
message("Single condition works:")
expected <- dplyr::filter(iris, Sepal.Length > 5)
obtained <- filter_wrap(iris, filter_args = Sepal.Length > 5)
stopifnot(identical(expected, obtained))
Run Code Online (Sandbox Code Playgroud)
当我尝试传递多个条件时,我遇到了问题.我期待调用中的!!!运算符dplyr::filter会拼接我的参数但是给出错误消息我想我理解它错了.
message("Multiple conditions fail:")
expected <- dplyr::filter(iris, Sepal.Length > 5, Petal.Length > 5)
obtained <- filter_wrap(iris, c(Sepal.Length > 5, Petal.Length > 5))
# Error in filter_impl(.data, quo) : Result must have length 150, not 300
# Called from: filter_impl(.data, …Run Code Online (Sandbox Code Playgroud)