Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: abp/filters/rpy.py

Issue 29849585: Issue 6835 - Add R support to python-abp (Closed)
Patch Set: Fixed consistency issues in README.md Created Aug. 31, 2018, 10:17 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « README.md ('k') | tests/test_rpy.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # This file is part of Adblock Plus <https://adblockplus.org/>,
2 # Copyright (C) 2006-present eyeo GmbH
3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation.
7 #
8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15
16 """
17 Functions for integrating with rPython.
18
19 see: https://cran.r-project.org/web/packages/rPython/index.html
20 """
21
22 from __future__ import unicode_literals
23
24 from abp.filters import parse_line
25
26 __all__ = ['line2dict']
27
28
29 def tuple2dict(data):
30 """Convert a parsed filter from a namedtuple to a dict.
31
32 Parameters
33 ----------
34 data: namedtuple
35 The parsed filter.
36
37 Returns
38 -------
39 dict
40 The resulting dictionary
41
42 """
43 result = dict(data._asdict())
44 result['type'] = data.__class__.__name__
45
46 return result
47
48
49 def strings2utf8(data):
50 """Convert strings in a data structure to utf8 byte strings.
51
52 Parameters
53 ----------
54 data: dict
55 The data to convert. Can include nested dicts, lists and tuples.
56
57 Returns
58 -------
59 dict
60 With all strings encoded as unicode.
61
62 """
63 if isinstance(data, dict):
64 return {strings2utf8(k): strings2utf8(v) for k, v in data.items()}
65 if isinstance(data, list):
66 return [strings2utf8(v) for v in data]
67 if isinstance(data, tuple):
68 return tuple(strings2utf8(v) for v in data)
69 if isinstance(data, type('')): # Python 2/3 compatible way of
70 # saying "unicode string".
71 return data.encode('utf-8')
72 return data
73
74
75 def line2dict(text):
76 """Convert a filterlist line to a dictionary.
77
78 All strings in the output dictionary will be UTF8 byte strings. This is
79 necessary to prevent unicode encoding errors in rPython conversion layer.
80
81 Parameters
82 ----------
83 text: str
84 The filter text we want to parse
85
86 Returns
87 -------
88 dict
89 With the parsing results and all strings converted to utf8 byte
90 strings.
91
92 """
93 return strings2utf8(tuple2dict(parse_line(text)))
OLDNEW
« no previous file with comments | « README.md ('k') | tests/test_rpy.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld