<jupyter_start><jupyter_text>Getting startedWelcome to the walpy notebook. In this notebook I have writen general (short) functions for every day use, which are written to a separate python script. For this reason every function can be imported from the repo separately. There is also a general file containing all functions. The primary usage would be in data science work, however the use cases could be more comprehensive.<jupyter_code># Full module dependencies:
import os
import rpy2.robjects.packages as rpackages
import sys
from IPython import get_ipython
from IPython.core.magic import register_cell_magic
from rpy2.robjects.vectors import StrVector<jupyter_output><empty_output><jupyter_text>Write and run (cell magic)This function is a modification of the %%writefiles cell magic command. The original function only writes the cell to a (new) python script, while this function allows one to write and run the cell. This is useful when working in a notebook for developing functions, like this notebook! The write run function can also write files into a specific location. This is done by simply specifying path/file.py.<jupyter_code>#%%write_and_run write_and_run.py     
from IPython.core.magic import register_cell_magic
from IPython import get_ipython
@register_cell_magic
def write_run(line, cell):
  """
  Write and run the cell. Use as follows: 
  
  %%write_and_run script.py to overwrite existing scripts.
  
  %%write_and_run -a script.py to append to the existing script.
  
  For both cases, if the script does not exist, then a new one is 
  created automatically.

  Args:
      line: ignore. See above.

      cell: ignore. See above.

  Returns:
      None
  """
  argz = line.split()
  file = argz[-1]
  mode = 'w'
  if len(argz) == 2 and argz[0] == '-a':
      mode = 'a'
  with open(file, mode) as f:
      f.write(cell)
  get_ipython().run_cell(cell)

  return<jupyter_output><empty_output><jupyter_text>Suppress outputEver wanted to suppress the output of a function, but the function does not allow this? Well this function solves this problem! Just use the suppress function.<jupyter_code>%%write_run walpy/suppress.py
import sys
import os
class suppress(object):

 def __init__(self,stdout = None, stderr = None):
  self.devnull = open(os.devnull,'w')
  self._stdout = stdout or self.devnull or sys.stdout
  self._stderr = stderr or self.devnull or sys.stderr

 def __enter__(self):
  self.old_stdout, self.old_stderr = sys.stdout, sys.stderr
  self.old_stdout.flush(); self.old_stderr.flush()
  sys.stdout, sys.stderr = self._stdout, self._stderr

 def __exit__(self, exc_type, exc_value, traceback):
  self._stdout.flush(); self._stderr.flush()
  sys.stdout = self.old_stdout
  sys.stderr = self.old_stderr
  self.devnull.close()<jupyter_output><empty_output><jupyter_text>Suppress exampleSuppose you want the suppress the following line of code:<jupyter_code>print('I want to suppress this!')<jupyter_output>I want to suppress this!<jupyter_text>Then one can simply run the following:<jupyter_code>with suppress():
  print('I want to suppress this!')<jupyter_output><empty_output><jupyter_text>R to Python installer and importerWhen importing R packages while working in a Python environment can lead to some unnecessary code. In order to provide a as clean and close to importing Python packages, I have written this function that allows one to import a list of R packages. The package is flexible enough to also allow for manual imports. If one wants to manually import an R package, simply specify the package(s) to be installed manually and map the example below the following code cell to the individual's use case.<jupyter_code>%%write_run walpy/r_importer.py
import walpy
import rpy2.robjects.packages as rpackages
from rpy2.robjects.vectors import StrVector

def r_importer(modules, install_only=[], log=False):
  """
  Import and install R packages. If the desired packages are not installed it will
  automatically install them. Note that this function will act as a one time
  delay in running time, if modules need to be installed. Import R packages
  manually as e.g. <stargazer =  rpy2.robjects.packages.importr('stargazer')>.
  So, the same name used for installing, should be used to import the functions.
  Important to note, this function imports the following modules from rpy2:
  "rpy2.robjects.packages" and "rpy2.robjects.vectors".
 
  Args:
      modules: list of the desired packages. The packages to be included should
      be as a string. E.g. modules = ['stargazer', 'tidyverse'].
 
      install_only: default=None. list or string of packages to be installed 
      only. Note, combinations are possible.
 
      log: default=False. Prints a log message if true, of the packages that are
      (succesfully) installed.
 
  Returns:
      None
  """
  if not isinstance(modules, list):
      modules = [modules]
  
  if not isinstance(install_only, list):
      install_only = [install_only]
      
  # import R's utility package:
  utils = rpackages.importr('utils')
 
  # R package names:
  packnames = tuple(modules)
 
  # Selectively install what needs to be install. Use CRAN cloud server:
  names_to_install = [x for x in packnames if not rpackages.isinstalled(x)]
  if len(names_to_install) > 0:
    print('Installing:', names_to_install)
    with walpy.suppress():
        utils.install_packages(StrVector(names_to_install),
                            repos='https://cloud.r-project.org/')
    print('Successfully installed:', names_to_install)
  
  # Make modules non-overlapping with install_only:
  modules = set(modules) - set(install_only)
 
  # Import modules to be automatically imported
  for module in modules:
    rpackages.importr(module)
  
  # Print log message if true:
  if log == True:
    print('Successfully imported:', [i for i in modules])
 
  return<jupyter_output><empty_output><jupyter_text>R importer example In order to manually import *sandwich* and *stargazer*, and automatically import *dplyr* and *tidyverse* run the following code.  **Important**: in order to prevent cluttering in this document I have already installed the packages! Normally if log=True and one has not installed the packages, you will see the corresponding installation output of the rpy2 package.<jupyter_code># First import the following rpy2 module for manual importing:
import rpy2.robjects.packages
 
# Modules to be installed:
modules = ['dplyr', 'tidyverse'] # This is correct!
modules = ['dplyr', 'tidyverse','sandwich', 'stargazer'] # This is also correct!
install_only = ['sandwich', 'stargazer'] # What to install only

# Run the R importer function
r_importer(modules, install_only)

# Assign packages of the manual installs to the Python environment:
sandwich = rpy2.robjects.packages.importr('sandwich') 
stargazer =  rpy2.robjects.packages.importr('stargazer')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07848837209302326, 'fraction_numerical': 0.0024709302325581395, 'mean_word_length': 4.396862745098039, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 30, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '664366', 'license': 'MIT', 'n_tokens_mistral': 2043, 'n_tokens_neox': 1974, 'n_words': 784, 'path': 'walpy.ipynb', 'repo_name': 'walterverwer/Utility-Functions', 'size': '6880'}