Exemplo n.º 1
0
 def test_m_value_orthodox(self):
     self.assertEqual('88',
                      rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9 88 xx")[
                          't'])
     self.assertEqual('88',
                      rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9 88 xx")[2])
     self.assertEqual(None,
                      rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9 88 xx")[
                          'tttt'])
Exemplo n.º 2
0
 def test_m_value(self):
     self.assertEqual('88',
                      ("Aa 9-9 88 xx" == rex('/([0-9-]+) (?P<t>[0-9-]+)/'))[
                          't'])
     self.assertEqual('88',
                      ("Aa 9-9 88 xx" == rex('/([0-9-]+) (?P<t>[0-9-]+)/'))[
                          2])
     self.assertEqual(None,
                      ("Aa 9-9 88 xx" == rex('/([0-9-]+) (?P<t>[0-9-]+)/'))[
                          'tttt'])
Exemplo n.º 3
0
def arpa(fp, gram=None, header_start = None, header_end = None, section_start = None, section_end = None, file_end = None):
    section = None
    lm_info = {}
    max_gram = 0
    for l in fp:
        #print(l)
        if l.startswith("\\"):
            if l == "\\data\\\n":
                section = 0
                print("loading header", file=sys.stderr)
                if header_start and header_start() == False:
                    break
            elif l=="\\end\\\n":
                if file_end:
                    file_end(lm_info)
                break
            else:
                res = (l == rex("/^\\\\(\\d+)-grams/"))
                if res is not None:
                    section = int(res[1])
                    print("loading %d-grams" % section, file=sys.stderr)
                    if section_start and section_start(lm_info,section) == False:
                        break
            continue
        if l == "\n":
            if section == 0 and header_end and header_end(lm_info)== False:
                break
            elif section is not None and section > 0 and section_end and section_end(lm_info,section) == False:
                break
            section = None
            continue
        if section == 0:
            res = (l == rex("/^ngram (\d+)=(\d+)/"))
            lm_info[int(res[1])] = int(res[2])
            print("ngram %d=%d"%(int(res[1]), int(res[2])), file=sys.stderr)
            max_gram = max(max_gram, int(res[1]))
        else:
            larr = l.strip("\n").split("\t")
            bow = None
            if len(larr) == 3:
                bow = float(larr[-1])
            elif len(larr) < 2:
                continue
            if bow is None:
                bow = 0
            prob = float(larr[0])
            words = larr[1].split(" ")
            if gram and gram(lm_info, section, words, prob, bow) == False:
                break
Exemplo n.º 4
0
 def test_not_cache(self):
     rex("s/cache/test1/", cache=False)
     self.assertNotIn("s/cache/test1/", rex_module.REX_CACHE)
Exemplo n.º 5
0
def test_empty_unicode():
    m = "This is dog!" == rex('/[0-9]+!/')
    assert m.__unicode__() == u''
Exemplo n.º 6
0
 def test_m_true(self):
     self.assertTrue("Aa 9-9 88 xx" == rex("/([0-9-]+) (?P<t>[0-9-]+)/"))
Exemplo n.º 7
0
 def test_s_multi(self):
     self.assertEqual("This is a dog dog dog dog", "This is a cat cat cat cat" == rex("s/cat/dog/"))
Exemplo n.º 8
0
import importlib
import inspect
from textwrap import dedent, indent
from collections import namedtuple
from pathlib import Path

import jinja2
import sass
import click
import markdown
from rex import rex


CONTENT_MODULE_PATH = "tests.test_content"

OUTPUT_RE = rex(r"""s/^.*?assert .*? == ['"](.*)['"].*?# output$\n/\1/""")

Example = namedtuple("Example", ('name', 'title', 'details', 'setup', 'old', 'new', 'output'))


def compile_sass(source_path, target_path_pattern):
    # First generate the content from which we can generate the hashname
    output = sass.compile(
        filename=str(source_path),
        output_style='compressed')
    hash = hashlib.sha512(output.encode('utf-8')).hexdigest()[:8]
    target_path = str(target_path_pattern).format(hash)
    source_map_target_path = target_path + '.map'
    output = sass.compile(
        filename=str(source_path),
        output_style='compressed',
Exemplo n.º 9
0
 def test_m_action(self):
     r = rex("m/test/")
     self.assertEqual(r.action, "m")
     self.assertEqual(r.pattern, "test")
     self.assertEqual(r.flags, 0)
Exemplo n.º 10
0
def test_m_false_noncache():
    assert rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9 88 xx", cache=False)
    assert not rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa bb cc xx", cache=False)
Exemplo n.º 11
0
def test_m_true_orthodox():
    assert rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9 88 xx")
Exemplo n.º 12
0
def test_m_true():
    assert ("Aa 9-9 88 xx" == rex('/([0-9-]+) (?P<t>[0-9-]+)/'))
Exemplo n.º 13
0
def test_m_true_orthodox():
    assert rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9 88 xx")
Exemplo n.º 14
0
def test_s_action_flags():
    r = rex('/test/im')
    assert r.action == 'm'
    assert r.pattern == 'test'
    assert r.flags == re.I | re.M
Exemplo n.º 15
0
def test_s_action():
    r = rex('s/test/ohh/')
    assert r.action == 's'
    assert r.pattern == 'test'
    assert r.replacement == 'ohh'
    assert r.flags == 0
Exemplo n.º 16
0
def test_m_action_ex():
    r = rex('m!test!')
    assert r.action == 'm'
    assert r.pattern == 'test'
    assert r.flags == 0
Exemplo n.º 17
0
def test_m_action_ex():
    r = rex('m!test!')
    assert r.action == 'm'
    assert r.pattern == 'test'
    assert r.flags == 0
Exemplo n.º 18
0
def test_m_false():
    assert not ("Aa 9-9  xx" == rex('/([0-9-]+) (?P<t>[0-9-]+)/'))
Exemplo n.º 19
0
def test_s_action_flags():
    r = rex('/test/im')
    assert r.action == 'm'
    assert r.pattern == 'test'
    assert r.flags == re.I | re.M
Exemplo n.º 20
0
def test_m_false_orthodox():
    assert not rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9  xx")
Exemplo n.º 21
0
def test_m_false():
    assert not ("Aa 9-9  xx" == rex('/([0-9-]+) (?P<t>[0-9-]+)/'))
Exemplo n.º 22
0
def test_s_i_orthodox():
    assert rex('s/CAT/dog/i', "This is a cat") == 'This is a dog'
Exemplo n.º 23
0
 def test_str(self):
     m = "This is dog!" == rex("/[a-z]+!/")
     self.assertEqual(str(m), "dog!")
Exemplo n.º 24
0
def test_s_multi_orthodox():
    assert rex('s/cat/dog/',
               "This is a cat cat cat cat") == 'This is a dog dog dog dog'
Exemplo n.º 25
0
 def test_s_action(self):
     r = rex("s/test/ohh/")
     self.assertEqual(r.action, "s")
     self.assertEqual(r.pattern, "test")
     self.assertEqual(r.replacement, "ohh")
     self.assertEqual(r.flags, 0)
Exemplo n.º 26
0
def test_cache():
    rex('s/cache/test/')
    assert 's/cache/test/' in rex_module.REX_CACHE
Exemplo n.º 27
0
 def test_m_value(self):
     self.assertEqual("88", ("Aa 9-9 88 xx" == rex("/([0-9-]+) (?P<t>[0-9-]+)/"))["t"])
     self.assertEqual("88", ("Aa 9-9 88 xx" == rex("/([0-9-]+) (?P<t>[0-9-]+)/"))[2])
     self.assertEqual(None, ("Aa 9-9 88 xx" == rex("/([0-9-]+) (?P<t>[0-9-]+)/"))["tttt"])
Exemplo n.º 28
0
def test_cache_2():
    a = rex('s/cache/test/')
    b = rex('s/cache/test/')
    assert a is b
Exemplo n.º 29
0
 def test_cache_2(self):
     a = rex("s/cache/test/")
     b = rex("s/cache/test/")
     self.assertEqual(a is b, True)
Exemplo n.º 30
0
def test_no_cache_2():
    a = rex('s/cache/test/', cache=False)
    b = rex('s/cache/test/', cache=False)
    assert not (a is b)
Exemplo n.º 31
0
def test_empty_unicode():
    m = "This is dog!" == rex('/[0-9]+!/')
    assert m.__unicode__() == u''
Exemplo n.º 32
0
def test_not_cache():
    rex('s/cache/test1/', cache=False)
    assert not 's/cache/test1/' in rex_module.REX_CACHE
Exemplo n.º 33
0
def test_m_action():
    r = rex('m/test/')
    assert r.action == 'm'
    assert r.pattern == 'test'
    assert r.flags == 0
Exemplo n.º 34
0
def test_clear_cache():
    rex('s/cache/test/')
    rex_clear_cache()
    assert not 's/cache/test/' in rex_module.REX_CACHE
Exemplo n.º 35
0
def test_s_action():
    r = rex('s/test/ohh/')
    assert r.action == 's'
    assert r.pattern == 'test'
    assert r.replacement == 'ohh'
    assert r.flags == 0
Exemplo n.º 36
0
def test_rex_group():
    m = "This is cat! A kitten is a cat but not a dog." == rex(
        '/[a-z]+!.*(kitten\s\S{2}).*but.*(dog)\./')
    assert m == rex.group
Exemplo n.º 37
0
def test_m_true():
    assert ("Aa 9-9 88 xx" == rex('/([0-9-]+) (?P<t>[0-9-]+)/'))
Exemplo n.º 38
0
def test_m_value():
    assert ("Aa 9-9 88 xx" == rex('/([0-9-]+) (?P<t>[0-9-]+)/'))['t'] == '88'
    assert ("Aa 9-9 88 xx" == rex('/([0-9-]+) (?P<t>[0-9-]+)/'))[2] == '88'
    assert ("Aa 9-9 88 xx"
            == rex('/([0-9-]+) (?P<t>[0-9-]+)/'))['tttt'] is None
Exemplo n.º 39
0
def test_m_false_noncache():
    assert rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9 88 xx", cache=False)
    assert not rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa bb cc xx", cache=False)
Exemplo n.º 40
0
def test_m_value_orthodox():
    assert rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9 88 xx")['t'] == '88'
    assert rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9 88 xx")[2] == '88'
    assert rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9 88 xx")['tttt'] is None
Exemplo n.º 41
0
def test_m_false_orthodox():
    assert not rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9  xx")
Exemplo n.º 42
0
def test_m_true_call():
    r = rex('/([0-9-]+) (?P<t>[0-9-]+)/')
    assert r("Aa 9-9 88 xx")
Exemplo n.º 43
0
 def test_clear_cache(self):
     rex("s/cache/test/")
     rex_clear_cache()
     self.assertNotIn("s/cache/test/", rex_module.REX_CACHE)
Exemplo n.º 44
0
def test_m_false_call():
    r = rex('/([0-9-]+) (?P<t>[0-9-]+)/')
    assert not r("Aa 9-9  xx")
Exemplo n.º 45
0
 def test_unicode(self):
     m = "This is dog!" == rex("/[a-z]+!/")
     self.assertEqual(unicode(m), u"dog!")
Exemplo n.º 46
0
def test_m_g():
    assert (("Aa 9-9 88 xx" == rex('/(\d)/g')) == ['9', '9', '8', '8'])
    assert (("Aa 9-9 88 xx" == rex('/([aA])/g')) == ['A', 'a'])
    assert (("Aa 9-9 88 xx" == rex('/(ttt)/g')) == [])
Exemplo n.º 47
0
 def test_m_action_ex(self):
     r = rex("m!test!")
     self.assertEqual(r.action, "m")
     self.assertEqual(r.pattern, "test")
     self.assertEqual(r.flags, 0)
Exemplo n.º 48
0
def test_s():
    s = ("This is a cat" == rex('s/cat/dog/'))
    assert s == 'This is a dog'
Exemplo n.º 49
0
 def test_s_action_flags(self):
     r = rex("/test/im")
     self.assertEqual(r.action, "m")
     self.assertEqual(r.pattern, "test")
     self.assertEqual(r.flags, re.I | re.M)
Exemplo n.º 50
0
def test_s_i():
    s = "This is a cat" == rex('s/CAT/dog/i')
    assert s == 'This is a dog'
Exemplo n.º 51
0
 def test_m_false(self):
     self.assertFalse("Aa 9-9  xx" == rex("/([0-9-]+) (?P<t>[0-9-]+)/"))
Exemplo n.º 52
0
def test_no_action():
    r = rex('/test/')
    assert r.action == 'm'
    assert r.pattern == 'test'
    assert r.flags == 0
Exemplo n.º 53
0
 def test_s_i(self):
     self.assertEqual("This is a dog", "This is a cat" == rex("s/CAT/dog/i"))
Exemplo n.º 54
0
def test_s_multi():
    s = "This is a cat cat cat cat" == rex('s/cat/dog/')
    assert s == 'This is a dog dog dog dog'
Exemplo n.º 55
0
    def test_cache(self):
        rex("s/cache/test/")

        self.assertIn("s/cache/test/", rex_module.REX_CACHE)
Exemplo n.º 56
0
def test_str():
    m = "This is dog!" == rex('/[a-z]+!/')
    assert str(m) == 'dog!'
Exemplo n.º 57
0
 def test_no_cache_2(self):
     a = rex("s/cache/test/", cache=False)
     b = rex("s/cache/test/", cache=False)
     self.assertEqual(a is b, False)
Exemplo n.º 58
0
def test_empty_str():
    m = "This is dog!" == rex('/[0-9]+!/')
    assert str(m) == ''
Exemplo n.º 59
0
from collections import defaultdict

from rex import rex

ALIAS_RE = rex(
    '/^(?P<alias>[a-zA-Z0-9_.@-]+)(?:[\s]+)(?P<target>[a-zA-Z0-9_.@-]+),?.*?$/'
)


def parse_virtual_file(virtual_file, target_domain=''):
    result = defaultdict(set)
    for line in virtual_file:
        line: str = line.strip()
        if line.startswith('#'):
            continue
        match = ALIAS_RE(line)
        if match:
            target = match['target']
            if "@" not in target:
                target = f"{target}@{target_domain}"
            result[target].add(match['alias'])

    return result
Exemplo n.º 60
0
def test_unicode():
    m = "This is dog!" == rex('/[a-z]+!/')
    assert m.__unicode__() == u'dog!'