Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions rpy2/robjects/lib/dbplyr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@
with warnings.catch_warnings():
warnings.simplefilter("ignore")
dbplyr = importr('dbplyr', on_conflict="warn")
dbplyr = WeakPackage(dbplyr._env,
dbplyr.__rname__,
translation=dbplyr._translation,
exported_names=dbplyr._exported_names,
on_conflict="warn",
version=dbplyr.__version__,
symbol_r2python=dbplyr._symbol_r2python,
symbol_resolve=dbplyr._symbol_resolve)
dbplyr = WeakPackage(dbplyr)
TARGET_VERSION = '1.4.2'
if dbplyr.__version__ != TARGET_VERSION:
warnings.warn(
Expand Down
9 changes: 1 addition & 8 deletions rpy2/robjects/lib/dplyr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@
lazyeval = importr('lazyeval', on_conflict="warn")
rlang = importr('rlang', on_conflict='warn',
robject_translations={'.env': '__env'})
dplyr = WeakPackage(dplyr_._env,
dplyr_.__rname__,
translation=dplyr_._translation,
exported_names=dplyr_._exported_names,
on_conflict="warn",
version=dplyr_.__version__,
symbol_r2python=dplyr_._symbol_r2python,
symbol_resolve=dplyr_._symbol_resolve)
dplyr = WeakPackage(dplyr_)

TARGET_VERSION = '0.8.'
if not dplyr.__version__.startswith(TARGET_VERSION):
Expand Down
9 changes: 1 addition & 8 deletions rpy2/robjects/lib/ggplot2.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,7 @@
lazyeval = importr('lazyeval', on_conflict='warn')
base = importr('base', on_conflict='warn')
ggplot2 = importr('ggplot2', on_conflict='warn')
ggplot2 = WeakPackage(ggplot2._env,
ggplot2.__rname__,
translation=ggplot2._translation,
exported_names=ggplot2._exported_names,
on_conflict="warn",
version=ggplot2.__version__,
symbol_r2python=ggplot2._symbol_r2python,
symbol_resolve=ggplot2._symbol_resolve)
ggplot2 = WeakPackage(ggplot2)

TARGET_VERSION = '3.3.'
if not ggplot2.__version__.startswith(TARGET_VERSION):
Expand Down
9 changes: 1 addition & 8 deletions rpy2/robjects/lib/grdevices.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@

grdevices = importr('grDevices')

grdevices = WeakPackage(grdevices._env,
grdevices.__rname__,
translation=grdevices._translation,
exported_names=grdevices._exported_names,
on_conflict="warn",
version=grdevices.__version__,
symbol_r2python=grdevices._symbol_r2python,
symbol_resolve=grdevices._symbol_resolve)
grdevices = WeakPackage(grdevices)

# non-interactive file devices
png = grdevices.png
Expand Down
9 changes: 1 addition & 8 deletions rpy2/robjects/lib/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,7 @@

grid = importr('grid')

grid = WeakPackage(grid._env,
grid.__rname__,
translation=grid._translation,
exported_names=grid._exported_names,
on_conflict="warn",
version=grid.__version__,
symbol_r2python=grid._symbol_r2python,
symbol_resolve=grid._symbol_resolve)
grid = WeakPackage(grid)

grid_env = robjects.baseenv['as.environment']('package:grid')

Expand Down
9 changes: 1 addition & 8 deletions rpy2/robjects/lib/tidyr.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@
'This was designed againt tidyr versions starting with %s '
'but you have %s' % (TARGET_VERSION, tidyr.__version__))

tidyr = WeakPackage(tidyr._env,
tidyr.__rname__,
translation=tidyr._translation,
exported_names=tidyr._exported_names,
on_conflict="warn",
version=tidyr.__version__,
symbol_r2python=tidyr._symbol_r2python,
symbol_resolve=tidyr._symbol_resolve)
tidyr = WeakPackage(tidyr)


def _wrap(rfunc):
Expand Down
18 changes: 16 additions & 2 deletions rpy2/robjects/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ def __fill_rpy2r__(self, on_conflict='fail'):
self._env,
translation=self._translation,
symbol_r2python=self._symbol_r2python,
symbol_resolve=self._symbol_resolve
symbol_resolve=self._symbol_resolve,
exported_names=self._exported_names
)
msg_prefix = ('Conflict when converting R symbols'
' in the package "%s"'
Expand Down Expand Up @@ -358,12 +359,25 @@ class WeakPackage(Package):
a warning (and a None returned) whenever the desired symbol is
not found (rather than a traditional `AttributeError`).
"""
def __init__(self, package: Package):
self._env = package._env
self.__rname__ = package.__rname__
self._translation = package._translation
self._rpy2r = package._rpy2r
self._symbol_r2python = package._symbol_r2python
self._symbol_resolve = package._symbol_resolve
self._exported_names = package._exported_names
self.__version__ = package.__version__
self.__rdata__ = package.__rdata__
for k, v in package.__dict__.items():
self.__dict__[k] = v

def __getattr__(self, name):
res = self.__dict__.get(name)
if res is None:
warnings.warn(
"The symbol '%s' is not in this R namespace/package." % name
"The symbol '%s' is not in this R namespace/package: %s"
% (name, self.__rname__)
)
return res

Expand Down
14 changes: 10 additions & 4 deletions rpy2/robjects/packages_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ def get_packagepath(package):
# The functions are in this module in order to facilitate
# their access from other modules (without circular dependencies).
# It not necessarily the absolute best place to have the functions though.
def default_symbol_r2python(rname):
def default_symbol_r2python(rname, exported_names=None):
"""Replace each dot (.) with an underscore (_)."""
return rname.replace('.', '_')
res = rname.replace('.', '_')
return res \
if exported_names is None \
or rname in exported_names \
or res.startswith('_') \
else "__" + res


def default_symbol_resolve(symbol_mapping):
Expand Down Expand Up @@ -75,7 +80,8 @@ def default_symbol_resolve(symbol_mapping):
def _map_symbols(rnames,
translation=dict(),
symbol_r2python=default_symbol_r2python,
symbol_resolve=default_symbol_resolve):
symbol_resolve=default_symbol_resolve,
exported_names=None):
"""
:param names: an iterable of rnames
:param translation: a mapping for R name->python name
Expand All @@ -89,7 +95,7 @@ def _map_symbols(rnames,
if rname in translation:
rpyname = translation[rname]
else:
rpyname = symbol_r2python(rname)
rpyname = symbol_r2python(rname, exported_names=exported_names)
symbol_mapping[rpyname].append(rname)
conflicts, resolutions = symbol_resolve(symbol_mapping)

Expand Down
6 changes: 6 additions & 0 deletions rpy2/tests/robjects/test_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ def tests_package_repr(self):
pck = robjects.packages.Package(env, "dummy_package")
assert isinstance(repr(pck), str)

def tests_weak_package(self):
pcki = robjects.packages.importr('stats', on_conflict='warn')
pck = robjects.packages.WeakPackage(pcki)
assert pck._exported_names == pcki._exported_names
assert [i for i in pck._env] == [i for i in pcki._env]
assert len(set(pcki.__dict__.keys()).difference(set(pck.__dict__.keys()))) == 0

def test_signaturetranslatedanonymouspackage():
rcode = """
Expand Down
12 changes: 12 additions & 0 deletions rpy2/tests/robjects/test_packages_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ def test___map_symbols():
(symbol_mapping,
conflicts,
resolutions) = p_u._map_symbols(rnames, translations)

def test__map_symbols_hidden_function():
rnames = ('exported_func', 'hidden_func', '.onAttach', '__NAMESPACE__')
(symbol_mapping,
conflicts,
resolutions) = p_u._map_symbols(rnames,
exported_names={'exported_func'})
assert len(symbol_mapping.keys()) == len(rnames) \
and 'exported_func' in symbol_mapping \
and '__hidden_func' in symbol_mapping \
and '_onAttach' in symbol_mapping \
and '__NAMESPACE__' in symbol_mapping


def test__fix_map_symbols_invalidonconflict():
Expand Down