Tumgik
nomethoderror · 3 years
Text
How to modify nested list values in R
modify_in_maybe <- function(.x, .where, .f, ...) {  .where <- as.list(.where)  .f <- rlang::as_function(.f)  current_value <- purrr::pluck(.x, !!!.where)  replacement <- .f(current_value, ...)  if (is.null(current_value)) return(.x)  purrr::assign_in(.x, .where, replacement) } list(root = list(nesting = list(leaf = 2))) %>%  modify_in_maybe(c("root", "nesting", "leaf"), ~.x + .x) list(list(root = list(nesting = list(leaf = 2))),     list(root = list(nesting = list()))) %>%  map(modify_in_maybe, c("root", "nesting", "leaf"), ~.x + .x)
0 notes
nomethoderror · 3 years
Text
How to format timelines with formattable in R
library(formattable) library(htmltools) timeline_widget <- function(start, duration, label, error_message, is_outgoing_call) { mapply(function(start, duration, label, error_message, is_outgoing_call) { doRenderTags( div(class = "timeline-widget", style = style(display = "grid", `grid-template-columns` = "fit-content(100px) fit-content(1500px)", `grid-column-gap` = "5px"), list( span(class = "timeline-marker", style = style(`margin-left` = paste0(round(start * 3) + 10, "px"), `min-width` = '1px', width = paste0(round(duration * 3), "px"), `background-color` = csscolor("#FA614B66")), HTML(" ")), span(class = "timeline-label", style = style(`marging-left` = "5px", `font-weight` = ifelse(!is_outgoing_call, "bold", NA), color = ifelse(!is.na(error_message), "red", NA)), label)))) }, start, duration, label, error_message, is_outgoing_call, SIMPLIFY = FALSE) }
data %>% formattable(list( timeline = ~timeline_widget(timeline_start, timeline_duration, timeline_label, error, outgoing), ) )
0 notes
nomethoderror · 3 years
Text
How to flatten json in R with tidyjson
library(dplyr) library(tidyjson) read_json("file.json") %>% # change it  select(-document.id) %>%  enter_object(nestedElement1, nestedElement2, arrayElement) %>% # change it  gather_array("document.id") %>%  json_structure %>%  filter(type != "object" &           type != "array" &           type != "null") %>%  mutate(path = sapply(seq, function(i) paste(i, collapse = ".")),         value = ..JSON) %>%  select(matches("document.id|path|value")) %>%  as_tibble() %>%  pivot_wider(names_from = path, values_from = value) %>%  mutate(across(everything(), ~unlist(replace_na(., NA)))) %>%  identity -> result
0 notes
nomethoderror · 3 years
Text
How to pivot key values list longer and wider in R
pivot_key_values_longer <- function(data, prefix, names_from, values_from) {  array_path_regex <- paste0("^(", prefix, ")\\.([0-9]+)\\.(", names_from, "|", values_from, ")$")  index_prefixed_array_path_regex <- paste0("^([0-9]+)\\.(", prefix, "\\.(?:", names_from, "|", values_from, "))$")  data %>%    rename_with(~gsub(array_path_regex, "\\2.\\1.\\3", .x), matches(array_path_regex)) %>%    pivot_longer(matches(index_prefixed_array_path_regex),                 names_to = c(paste0(prefix, ".id"), ".value"),                 names_pattern = index_prefixed_array_path_regex) } pivot_key_values_wider <- function(data, prefix, names_from, values_from) {  data %>%    pivot_wider(      id_cols = !matches(paste0("^(", prefix, ")\\.(", names_from, "|", values_from, "|id)$")),      values_fn = unique,      names_from = paste0(prefix, ".", names_from),      values_from = paste0(prefix, ".", values_from),      names_prefix = paste0(prefix, ".")    ) } pivot_list_wider <- function(data, prefix, names_from, values_from) {  data %>%    pivot_key_values_longer(prefix, names_from, values_from) %>%    pivot_key_values_wider(prefix, names_from, values_from) }
#r
0 notes
nomethoderror · 3 years
Text
Introducing RSpector plugin for RubyMine
Enhanced RSpec Support for RubyMine
Find Usages on let variables
Go to Declaration or Usages on let variables
Refactor | Rename... of let variable names and usages
https://github.com/srizzo/rspector-rubymine-plugin
0 notes
nomethoderror · 3 years
Text
Introducing CodeBuddy plugin for IntelliJ
Enhanced text editing capabilities, inspired by TextMate. Some of the features
Dialog-less Find & Replace
Use Selection for Find
Use Selection for Replace
Find All
Replace
Replace All in Selection
Text Selection
Select Paragraph
Select Enclosing Typing Pairs
Text Manipulation
Twiddle
https://plugins.jetbrains.com/plugin/16252-codebuddy
0 notes
nomethoderror · 4 years
Text
How to tunnel AWS RDS to a local machine through a Kubernetes cluster with an ad hoc bastion host
Start the bastion host:
TUNNEL_HOST=[rds_endpoint] TUNNEL_PORT=[rds_port]; kubectl --restart=Never run -it --rm --port=$TUNNEL_PORT --image=alpine/socat:1.0.3 rds-tunnel TCP-LISTEN:$TUNNEL_PORT,fork TCP:$TUNNEL_HOST:$TUNNEL_PORT
Start port forward:
while true; do kubectl port-forward rds-tunnel [rds_port]:[rds_port]; done
0 notes
nomethoderror · 4 years
Text
How to capture/record XHR requests on Cypress
Cypress.Commands.add('recordXHR', (method = 'GET', url = /.*/) => { return cy.route({ url: url, method: method, onResponse: function (xhr) { const destFilename = Cypress._.capitalize(xhr.method.toLowerCase()) + xhr.url .slice(Cypress.config().baseUrl.length + 1) .split('/') .map((part) => Cypress._.capitalize(part)) .join('') + '.json'; const xhrOptions = { url: xhr.url, method: xhr.method, status: xhr.response.status, headers: xhr.response.headers, response: xhr.response.body }; cy.now('writeFile', 'cypress/xhr/' + destFilename, xhrOptions, { log: false }); } }); });
0 notes
nomethoderror · 5 years
Text
How to use environment variables to config gcloud SDK
Find available properties with:
gcloud config --help
Set environment variable with this format:
CLOUDSDK_[SECTION]_[PROPERTY]
Source: https://medium.com/@nieldw/using-environment-variables-to-set-gcloud-config-9eb2da7666e7
0 notes
nomethoderror · 5 years
Text
How to have project dependent Jupyter Notebook configuration  scripts
1 - Create a startup script that will check for a .jupyternotebookrc file:
# ~/.ipython/profile_default/startup/run_jupyternotebookrc.py import os import sys if 'ipykernel' in sys.modules: # hackish way to check whether it's running a notebook path = os.getcwd() while path != "/" and ".jupyternotebookrc" not in os.listdir(path): path = os.path.abspath(path + "/../") full_path = os.path.join(path, ".jupyternotebookrc") if os.path.exists(full_path): get_ipython().run_cell(open(full_path).read(), store_history=False)
2 - Create a configuration file in your project with the code you'd like to run:
# .jupyternotebookrc in any folder or parent folder of the notebook %load_ext jupytersourcemagic %load_ext jupyterserverwidget %load_ext jupyternotify %load_ext dotenv %dotenv .env -o %dotenv .env.development -o
0 notes
nomethoderror · 5 years
Text
Introducing Jupyter Source: A Jupyter Notebook %%magic to quickly edit and run source code
https://github.com/srizzo/jupyter-source-magic
4 notes · View notes
nomethoderror · 5 years
Text
Introducing Jupyter Server Widget: Jupyter Notebook %%magics and Widget to start and stop servers from a Cell
https://github.com/srizzo/jupyter-server-widget
1 note · View note
nomethoderror · 5 years
Text
Introducing Jupyter Live Magic: A Jupyter Notebook %%magic for periodic auto re-run and refresh of Cells
https://github.com/srizzo/jupyter-live-magic
0 notes
nomethoderror · 6 years
Text
How to edit jupyter notebook cells in an external editor
Adapted from https://stackoverflow.com/questions/28309430/edit-ipython-cell-in-an-external-editor
// ~/.jupyter/custom/custom.js IPython.keyboard_manager.command_shortcuts.add_shortcut('e', { handler : function (event) { var input = IPython.notebook.get_selected_cell().get_text(); var pre_cursor_text = [""] if (IPython.notebook.get_selected_cell().get_pre_cursor()) { pre_cursor_text = IPython.notebook.get_selected_cell().get_pre_cursor().split("\n") } var line = pre_cursor_text.length var col = pre_cursor_text[pre_cursor_text.length - 1].length + 1 var cmd = "f = open('.jupyter-tmp-cell-content.py', 'w');f.close()"; if (input != "") { cmd = '%%writefile .jupyter-tmp-cell-content.py\n' + input; } IPython.notebook.kernel.execute(cmd); cmd = "import os;os.system('mate -w .jupyter-tmp-cell-content.py -l " + line + ":" + col + "');f = open('.jupyter-tmp-cell-content.py', 'r');print(f.read())"; function handle_output(msg) { var ret = msg.content.text; IPython.notebook.get_selected_cell().set_text(ret); } var callback = {'output': handle_output}; IPython.notebook.kernel.execute(cmd, {iopub: callback}, {silent: false}); return false; }} );
0 notes
nomethoderror · 6 years
Text
How to find potentially unused ruby classes
Note: first "cd app" and make sure your tests/specs folder are not also scanned
ag --ruby --nofilename -o --match '(?<=class )[A-Z][a-zA-Z0-9_!$?]+' | sort | uniq | xargs -I '{}' ag --ruby --nofilename --case-sensitive -o --match '\b{}\b' | sort | uniq -u
0 notes
nomethoderror · 6 years
Text
How to find potentially unused ruby methods
Note: first "cd app" and make sure your tests/specs folder are not also scanned
ag --ruby --nofilename -o --match '(?<=def )[a-zA-Z0-9_!$?]+' | sort | uniq | sed s/?/\\\\\\\\?/g | xargs -I '{}' ag --ruby --nofilename --case-sensitive -o --match '\b{}(?![a-zA-Z0-9_!$?])' | sort | uniq -u
0 notes
nomethoderror · 7 years
Text
How to use relative paths in bash / shell scripts
From http://mywiki.wooledge.org/BashFAQ/028
cd "${BASH_SOURCE%/*}" || exit read somevar < etc/somefile
0 notes