Last active
August 6, 2022 08:52
-
-
Save stla/7d4face56a7d298df0b593225f83a70b to your computer and use it in GitHub Desktop.
Python utils in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setwd("C:/SL/Python/RPythonUtils") | |
library(clipr) | |
list2list <- function(L, outfile = "list.txt") { | |
singletons_idxs <- which(lapply(L, length) == 1) | |
singletons <- L[singletons_idxs] | |
L <- replace(L, singletons_idxs, sprintf("c(%s)", singletons)) | |
write_clip(L, breaks = "],\n", sep = ", ") | |
cat("[", | |
sub("\\)", "]\n]", gsub("\\)]", "]", gsub("c\\(", "[", read_clip()))), | |
sep = "\n", file = outfile | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setwd("C:/SL/Python/RPythonUtils") | |
library(clipr) | |
library(magrittr) | |
matrix2list <- function(M, outfile = "haskell.txt") { | |
rownames(M) <- colnames(M) <- NULL | |
write_clip(M, sep = ", ") | |
clipboard <- c( | |
sprintf("%s],", head(read_clip(), -1)), | |
sprintf("%s]", tail(read_clip(), 1)) | |
) | |
paste0(c("[", paste0(" [", clipboard))) %>% | |
cat(sep = "\n", file = outfile) %>% | |
cat("]", sep = "", file = outfile, append = TRUE) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
locklines <- readLines("poetry.lock") | |
Lines <- grep("^\\[\\[package", locklines) | |
pkgs <- locklines[Lines + 1L] | |
pkgs <- vapply(strsplit(pkgs, "\""), `[`, character(1L), 2L) | |
versions <- locklines[Lines + 2L] | |
versions <- vapply(strsplit(versions, "\""), `[`, character(1L), 2L) | |
paste0( | |
"poetry run python -m pip install ", | |
gsub( | |
"-", | |
"_", | |
paste0(sprintf("%s==%s", pkgs, versions), collapse = " ") | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment