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'])
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'])
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
def test_not_cache(self): rex("s/cache/test1/", cache=False) self.assertNotIn("s/cache/test1/", rex_module.REX_CACHE)
def test_empty_unicode(): m = "This is dog!" == rex('/[0-9]+!/') assert m.__unicode__() == u''
def test_m_true(self): self.assertTrue("Aa 9-9 88 xx" == rex("/([0-9-]+) (?P<t>[0-9-]+)/"))
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/"))
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',
def test_m_action(self): r = rex("m/test/") self.assertEqual(r.action, "m") self.assertEqual(r.pattern, "test") self.assertEqual(r.flags, 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)
def test_m_true_orthodox(): assert rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9 88 xx")
def test_m_true(): assert ("Aa 9-9 88 xx" == rex('/([0-9-]+) (?P<t>[0-9-]+)/'))
def test_s_action_flags(): r = rex('/test/im') assert r.action == 'm' assert r.pattern == 'test' assert r.flags == re.I | re.M
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
def test_m_action_ex(): r = rex('m!test!') assert r.action == 'm' assert r.pattern == 'test' assert r.flags == 0
def test_m_false(): assert not ("Aa 9-9 xx" == rex('/([0-9-]+) (?P<t>[0-9-]+)/'))
def test_m_false_orthodox(): assert not rex('/([0-9-]+) (?P<t>[0-9-]+)/', "Aa 9-9 xx")
def test_s_i_orthodox(): assert rex('s/CAT/dog/i', "This is a cat") == 'This is a dog'
def test_str(self): m = "This is dog!" == rex("/[a-z]+!/") self.assertEqual(str(m), "dog!")
def test_s_multi_orthodox(): assert rex('s/cat/dog/', "This is a cat cat cat cat") == 'This is a dog dog dog dog'
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)
def test_cache(): rex('s/cache/test/') assert 's/cache/test/' in rex_module.REX_CACHE
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"])
def test_cache_2(): a = rex('s/cache/test/') b = rex('s/cache/test/') assert a is b
def test_cache_2(self): a = rex("s/cache/test/") b = rex("s/cache/test/") self.assertEqual(a is b, True)
def test_no_cache_2(): a = rex('s/cache/test/', cache=False) b = rex('s/cache/test/', cache=False) assert not (a is b)
def test_not_cache(): rex('s/cache/test1/', cache=False) assert not 's/cache/test1/' in rex_module.REX_CACHE
def test_m_action(): r = rex('m/test/') assert r.action == 'm' assert r.pattern == 'test' assert r.flags == 0
def test_clear_cache(): rex('s/cache/test/') rex_clear_cache() assert not 's/cache/test/' in rex_module.REX_CACHE
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
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
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
def test_m_true_call(): r = rex('/([0-9-]+) (?P<t>[0-9-]+)/') assert r("Aa 9-9 88 xx")
def test_clear_cache(self): rex("s/cache/test/") rex_clear_cache() self.assertNotIn("s/cache/test/", rex_module.REX_CACHE)
def test_m_false_call(): r = rex('/([0-9-]+) (?P<t>[0-9-]+)/') assert not r("Aa 9-9 xx")
def test_unicode(self): m = "This is dog!" == rex("/[a-z]+!/") self.assertEqual(unicode(m), u"dog!")
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')) == [])
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)
def test_s(): s = ("This is a cat" == rex('s/cat/dog/')) assert s == 'This is a dog'
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)
def test_s_i(): s = "This is a cat" == rex('s/CAT/dog/i') assert s == 'This is a dog'
def test_m_false(self): self.assertFalse("Aa 9-9 xx" == rex("/([0-9-]+) (?P<t>[0-9-]+)/"))
def test_no_action(): r = rex('/test/') assert r.action == 'm' assert r.pattern == 'test' assert r.flags == 0
def test_s_i(self): self.assertEqual("This is a dog", "This is a cat" == rex("s/CAT/dog/i"))
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'
def test_cache(self): rex("s/cache/test/") self.assertIn("s/cache/test/", rex_module.REX_CACHE)
def test_str(): m = "This is dog!" == rex('/[a-z]+!/') assert str(m) == 'dog!'
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)
def test_empty_str(): m = "This is dog!" == rex('/[0-9]+!/') assert str(m) == ''
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
def test_unicode(): m = "This is dog!" == rex('/[a-z]+!/') assert m.__unicode__() == u'dog!'