Best Python code snippet using slash
test_filtering.py
Source:test_filtering.py  
...50cumprod_expected = data.cumprod().ffill()51cumprod_filtered_expected = data[filter].cumprod().reindex(index).ffill()52cumprod_df_expected = df.cumprod().ffill()53cumprod_df_filtered_expected = df[filter].cumprod().reindex(index).ffill()54def _normalize(x):55    """try and get a vector/dataframe in a format unittest can compare them"""56    if isinstance(x, float):57        if np.isnan(x):58            return -np.inf59        return x60    if isinstance(x, (pa.DataFrame, pa.Series)):61        x = x.values62    if x.ndim == 1:63        return tuple([_normalize(y) for y in x])64    r = x.copy()65    r[np.isnan(r)] = -np.inf66    return tuple(r.tolist())67class NodeFilterTest(unittest.TestCase):68    def setUp(self):69        self.ctx = MDFContext(index[0])70    def test_queue(self):71        actual = self._run(datanode.queuenode(as_list=True))72        self.assertEqual(actual.tolist(), queue_expected)73        actual = self._run(datanode.queuenode(filter=filternode, as_list=True))74        self.assertEqual(actual.tolist(), queue_filtered_expected)75    def test_delay(self):76        actual = self._run(datanode.delaynode(periods=1, initial_value=np.nan))77        self.assertEqual(_normalize(actual), _normalize(delay_expected))78        actual = self._run(datanode.delaynode(periods=1, initial_value=np.nan, filter=filternode))79        self.assertEqual(_normalize(actual), _normalize(delay_filtered_expected))80    def test_delay_df(self):81        actual = self._run(dfnode.delaynode(periods=1, initial_value=np.nan))82        self.assertEqual(_normalize(actual), _normalize(delay_df_expected))83        actual = self._run(dfnode.delaynode(periods=1, initial_value=np.nan, filter=filternode))84        self.assertEqual(_normalize(actual), _normalize(delay_df_filtered_expected))85    def test_nansum(self):86        actual = self._run(datanode.nansumnode())87        self.assertEqual(_normalize(actual), _normalize(nansum_expected))88        actual = self._run(datanode.nansumnode(filter=filternode))89        self.assertEqual(_normalize(actual), _normalize(nansum_filtered_expected))90    def test_nansum_df(self):91        actual = self._run(dfnode.nansumnode())92        self.assertEqual(_normalize(actual), _normalize(nansum_df_expected))93        actual = self._run(dfnode.nansumnode(filter=filternode))94        self.assertEqual(_normalize(actual), _normalize(nansum_df_filtered_expected))95    def test_cumprod(self):96        actual = self._run(datanode.cumprodnode())97        self.assertEqual(_normalize(actual), _normalize(cumprod_expected))98        actual = self._run(datanode.cumprodnode(filter=filternode))99        self.assertEqual(_normalize(actual), _normalize(cumprod_filtered_expected))100    def test_cumprod_df(self):101        actual = self._run(dfnode.cumprodnode())102        self.assertEqual(_normalize(actual), _normalize(cumprod_df_expected))103        actual = self._run(dfnode.cumprodnode(filter=filternode))104        self.assertEqual(_normalize(actual), _normalize(cumprod_df_filtered_expected))105    def _run(self, node):106        result = []107        for t in index:108            self.ctx.set_date(t)109            result.append(self.ctx[node])110        if isinstance(result[0], pa.Series):111            return pa.DataFrame(result, index=index)...normalize.py
Source:normalize.py  
1# -*- coding: utf-8 -*-2import six3from geodata.text import _normalize4from geodata.text.token_types import token_types5from geodata.encoding import safe_decode6# String options7NORMALIZE_STRING_LATIN_ASCII = _normalize.NORMALIZE_STRING_LATIN_ASCII8NORMALIZE_STRING_TRANSLITERATE = _normalize.NORMALIZE_STRING_TRANSLITERATE9NORMALIZE_STRING_STRIP_ACCENTS = _normalize.NORMALIZE_STRING_STRIP_ACCENTS10NORMALIZE_STRING_DECOMPOSE = _normalize.NORMALIZE_STRING_DECOMPOSE11NORMALIZE_STRING_LOWERCASE = _normalize.NORMALIZE_STRING_LOWERCASE12NORMALIZE_STRING_TRIM = _normalize.NORMALIZE_STRING_TRIM13NORMALIZE_STRING_REPLACE_HYPHENS = _normalize.NORMALIZE_STRING_REPLACE_HYPHENS14NORMALIZE_STRING_SIMPLE_LATIN_ASCII = _normalize.NORMALIZE_STRING_SIMPLE_LATIN_ASCII15DEFAULT_STRING_OPTIONS = _normalize.NORMALIZE_DEFAULT_STRING_OPTIONS16# Token options17NORMALIZE_TOKEN_REPLACE_HYPHENS = _normalize.NORMALIZE_TOKEN_REPLACE_HYPHENS18NORMALIZE_TOKEN_DELETE_HYPHENS = _normalize.NORMALIZE_TOKEN_DELETE_HYPHENS19NORMALIZE_TOKEN_DELETE_FINAL_PERIOD = _normalize.NORMALIZE_TOKEN_DELETE_FINAL_PERIOD20NORMALIZE_TOKEN_DELETE_ACRONYM_PERIODS = _normalize.NORMALIZE_TOKEN_DELETE_ACRONYM_PERIODS21NORMALIZE_TOKEN_DROP_ENGLISH_POSSESSIVES = _normalize.NORMALIZE_TOKEN_DROP_ENGLISH_POSSESSIVES22NORMALIZE_TOKEN_DELETE_OTHER_APOSTROPHE = _normalize.NORMALIZE_TOKEN_DELETE_OTHER_APOSTROPHE23NORMALIZE_TOKEN_SPLIT_ALPHA_FROM_NUMERIC = _normalize.NORMALIZE_TOKEN_SPLIT_ALPHA_FROM_NUMERIC24NORMALIZE_TOKEN_REPLACE_DIGITS = _normalize.NORMALIZE_TOKEN_REPLACE_DIGITS25DEFAULT_TOKEN_OPTIONS = _normalize.NORMALIZE_DEFAULT_TOKEN_OPTIONS26TOKEN_OPTIONS_DROP_PERIODS = _normalize.NORMALIZE_TOKEN_OPTIONS_DROP_PERIODS27DEFAULT_TOKEN_OPTIONS_NUMERIC = _normalize.NORMALIZE_DEFAULT_TOKEN_OPTIONS_NUMERIC28def remove_parens(tokens):29    new_tokens = []30    open_parens = 031    for t, c in tokens:32        if c == token_types.PUNCT_OPEN:33            open_parens += 134        elif c == token_types.PUNCT_CLOSE:35            if open_parens > 0:36                open_parens -= 137        elif open_parens <= 0:38            new_tokens.append((t, c))39    return new_tokens40def normalize_string(s, string_options=DEFAULT_STRING_OPTIONS):41    s = safe_decode(s)42    return _normalize.normalize_string(s, string_options)43def normalized_tokens(s, string_options=DEFAULT_STRING_OPTIONS,44                      token_options=DEFAULT_TOKEN_OPTIONS,45                      strip_parentheticals=True, whitespace=False):46    '''47    Normalizes a string, tokenizes, and normalizes each token48    with string and token-level options.49    This version only uses libpostal's deterministic normalizations50    i.e. methods with a single output. The string tree version will51    return multiple normalized strings, each with tokens.52    Usage:53        normalized_tokens(u'St.-Barthélemy')54    '''55    s = safe_decode(s)56    normalized_tokens = _normalize.normalized_tokens(s, string_options, token_options, whitespace)57    if strip_parentheticals:58        normalized_tokens = remove_parens(normalized_tokens)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
