Skip to content

Commit

Permalink
v_0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
walterverwer committed May 4, 2021
1 parent fa7b17c commit cc6618d
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 0 deletions.
8 changes: 8 additions & 0 deletions build/lib/walpy/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""
@author: Walter Verwer
@git: https://github.com/walterverwer
"""
from .r_importer import r_importer
from .suppress import suppress
from .write_run import write_run
53 changes: 53 additions & 0 deletions build/lib/walpy/r_importer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import rpy2.robjects.packages as rpackages
from rpy2.robjects.vectors import StrVector
from walpy import suppress

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
"""
# 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)
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
19 changes: 19 additions & 0 deletions build/lib/walpy/suppress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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()
32 changes: 32 additions & 0 deletions build/lib/walpy/write_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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
Binary file removed dist/walpy-0.1.tar.gz
Binary file not shown.
Binary file added dist/walpy-0.2-py3-none-any.whl
Binary file not shown.
Binary file added dist/walpy-0.2.tar.gz
Binary file not shown.
23 changes: 23 additions & 0 deletions walpy.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Metadata-Version: 2.1
Name: walpy
Version: 0.2
Summary: General functions
Home-page: https://github.com/walterverwer/walpy
Author: Walter Verwer
Author-email: walterverwer@gmail.com
License: MIT
Download-URL: https://github.com/walterverwer/walpy/archive/refs/tags/v_0.1.tar.gz
Description: # Readme walpy
Walpy is a general purpose package containing '30-second-functions'.

Keywords: SOME,MEANINGFULL,KEYWORDS
Platform: UNKNOWN
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Build Tools
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Description-Content-Type: text/markdown
13 changes: 13 additions & 0 deletions walpy.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
LICENCE.txt
README.md
setup.cfg
setup.py
walpy/__init__.py
walpy/r_importer.py
walpy/suppress.py
walpy/write_run.py
walpy.egg-info/PKG-INFO
walpy.egg-info/SOURCES.txt
walpy.egg-info/dependency_links.txt
walpy.egg-info/requires.txt
walpy.egg-info/top_level.txt
1 change: 1 addition & 0 deletions walpy.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 2 additions & 0 deletions walpy.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rpy2
IPython
1 change: 1 addition & 0 deletions walpy.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
walpy

0 comments on commit cc6618d

Please sign in to comment.