Beispiel #1
0
 def test_notes_at_div(self):
     p = pattern.Pattern(1, 1, 5)
     self.assertSetEqual(p.notes_at_div(0), set())
     p.add_note(0, 0)
     p.add_note(2, 0)
     p.add_note(4, 0)
     self.assertSetEqual(p.notes_at_div(0), {0, 2, 4})
     self.assertIsNone(p.notes_at_div(1))
Beispiel #2
0
 def test_add_note(self):
     p = pattern.Pattern(4, 4, 2)
     self.assertTrue(p.add_note(0, 0))
     self.assertTrue(p.add_note(1, 0))
     self.assertFalse(p.add_note(0, 0), "Duplicate note")
     self.assertFalse(p.add_note(3, 0), "Note out of range")
     self.assertFalse(p.add_note(0, 99), "Div out of range")
     self.assertFalse(p.add_note(-1, 0), "Negative note")
     self.assertFalse(p.add_note(0, -1), "Negative div")
Beispiel #3
0
 def test_remove_note(self):
     p = pattern.Pattern(1, 4, 2)
     self.assertTrue(p.add_note(0, 0))
     self.assertFalse(p.remove_note(2, 0), "Note out of range")
     self.assertFalse(p.remove_note(0, 20), "Div out of range")
     self.assertFalse(p.remove_note(-1, 0), "Negative note")
     self.assertFalse(p.remove_note(0, -1), "Negative div")
     self.assertTrue(p.remove_note(0, 0))
     self.assertFalse(p.remove_note(0, 0), "Note already removed")
Beispiel #4
0
    def __init__(self):

        self.accepted_patterns = [
            pattern.Pattern([
                constant.LAST_NAME_PATTERN, constant.FIRST_NAME_PATTERN,
                constant.PHONE_NUMBER_PATTERN, constant.COLOR_PATTERN,
                constant.ZIP_CODE_PATTERN
            ]),
            pattern.Pattern([
                constant.FIRST_NAME_PATTERN + " " + constant.LAST_NAME_PATTERN,
                constant.COLOR_PATTERN, constant.ZIP_CODE_PATTERN,
                constant.PHONE_NUMBER_PATTERN
            ]),
            pattern.Pattern([
                constant.FIRST_NAME_PATTERN, constant.LAST_NAME_PATTERN,
                constant.ZIP_CODE_PATTERN, constant.PHONE_NUMBER_PATTERN,
                constant.COLOR_PATTERN
            ])
        ]
Beispiel #5
0
    def test_empty_init(self):
        "Test the default Pattern creation"

        # 1. Create default Pattern object
        myobj = pattern.Pattern()

        # 2. Make sure it has the default values
        self.assertEqual(myobj.part2, False)
        self.assertEqual(myobj.text, None)
        self.assertEqual(len(myobj.signals), 0)
        self.assertEqual(len(myobj.output), 0)
    def __init__(self, text=None, part2=False):

        # 1. Set the initial values
        self.part2 = part2
        self.text = text
        self.patterns = []

        # 2. Process text (if any)
        if text is not None and len(text) > 0:
            for line in text:
                self.patterns.append(pattern.Pattern(text=line, part2=part2))
Beispiel #7
0
    def test_text_init(self):
        "Test the Pattern object creation from text"

        # 1. Create Pattern object from text
        myobj = pattern.Pattern(text=EXAMPLE_ONE)

        # 2. Make sure it has the expected values
        self.assertEqual(myobj.part2, False)
        self.assertEqual(len(myobj.text), 86)
        self.assertEqual(len(myobj.signals), 10)
        self.assertEqual(len(myobj.output), 4)

        # 3. Test methods
        self.assertEqual(myobj.one_four_seven_eight(), 2)
Beispiel #8
0
    def test_text_init_two(self):
        "Test the Pattern object creation from text for part two"

        # 1. Create Pattern object from text
        myobj = pattern.Pattern(text=EXAMPLE_TWO)

        # 2. Make sure it has the expected values
        self.assertEqual(myobj.part2, False)
        self.assertEqual(len(myobj.text), 84)
        self.assertEqual(len(myobj.signals), 10)
        self.assertEqual(len(myobj.output), 4)

        # 3. Test methods
        self.assertEqual(myobj.value(), 5353)
Beispiel #9
0
 def test_str_conversion(self):
     p = pattern.Pattern(4, 4, 2)
     self.assertEqual(str(p), "000| - - - - - - - - - - - - - - - - ",
                      "Empty Pattern")
     self.assertTrue(p.add_note(0, 0))
     self.assertTrue(p.add_note(0, 4))
     self.assertTrue(p.add_note(0, 8))
     self.assertTrue(p.add_note(0, 12))
     self.assertEqual(str(p), "000| # - - - # - - - # - - - # - - - ",
                      "Empty Pattern")
     p = pattern.Pattern(6, 1, 4)
     self.assertEqual(str(p), "000| - - - - - - ", "Empty Pattern")
     self.assertTrue(p.add_note(0, 0))
     self.assertTrue(p.add_note(1, 0))
     self.assertTrue(p.add_note(2, 0))
     self.assertTrue(p.add_note(3, 0))
     self.assertEqual(
         str(p),
         "000| # - - - - - \n001| # - - - - - \n002| # - - - - - \n003| # - - - - - ",
         "MultiNote test")
     self.assertEqual(
         repr(p),
         "000| # - - - - - \n001| # - - - - - \n002| # - - - - - \n003| # - - - - - ",
         "MultiNote test")
Beispiel #10
0
    def __rotate_pattern(self, pattern):
        # get the circles into a list firstly
        circles = self.__identify_circles(pattern)

        # calculate the shortest distance between two circles
        min_dist = None
        line = None
        if circles and len(circles) is 4:
            for c1 in circles:
                for c2 in circles:
                    if c1 is c2: continue
                    if not min_dist or c1.distance(c2) < min_dist:
                        min_dist = c1.distance(c2)
                        line = (c1, c2)

        if line and len(circles) is 4:
            c1, c2 = line  # c1 and c2 are the min distance line
            c3, c4 = filter(lambda c: c is not c1 and c is not c2, circles)

            if self.draw:
                cv2.line(self.image, (c1.x, c1.y), (c2.x, c2.y), (0, 255, 0),
                         2)

            # find the angle the minimum line makes with the x-axis
            angle = math_util.line_angle((c1.x, c1.y), (c2.x, c2.y))

            # get the center of the pattern
            pattern_x = sum(map(lambda c: c.x, circles)) / len(circles)
            pattern_y = sum(map(lambda c: c.y, circles)) / len(circles)

            # rotate all circle co-ordinates by that angle
            for c in circles:
                c.x, c.y = math_util.rotate_point((c.x, c.y), -angle)

            # check if it is flipped by simply checking the y values
            if c3.y > c1.y:
                angle = (angle + math.pi) % (2 * math.pi)
                for c in circles:
                    c.x, c.y = -c.x, -c.y

            # return a pattern object
            return p.Pattern(pattern_x, pattern_y, -1,
                             int(math.degrees(angle)),
                             self.__orient_circles(circles))
        else:
            return None
Beispiel #11
0
 def __init__(self, file=None, otherFiles=[]):
     self.root = Node()
     self.reset()
     files = []
     if file != None:
         files.append(file)
     for f in otherFiles:
         files.append(f)
     for f in files:
         for line in open(f):
             cols = line.strip().split("\t")
             if len(cols) < 2:
                 sys.stderr.write(line)
                 continue
             for c in cols[1:]:
                 p = pattern.Pattern(objectConcept=c)
                 p.readPattern(cols[0], False)
                 self.root.addPattern(p)
Beispiel #12
0
    def __init__(self, source, **kwargs):
        super(Piece, self).__init__(**kwargs)
        self.auto_bring_to_front = False
        self.source = source  # also look at StringProperty
        self.fabric = CoreImage.load(filename=self.source, keep_data=True)
        self.image = Image(texture=self.fabric.texture)
        self.scale = self.image.texture_size[0] / self.size[0]
        self.do_scale = False
        self.add_widget(self.image)
        self.pattern = pattern.Pattern()
        self.add_widget(self.pattern)

        self.instructions = InstructionGroup()
        self.canvas.add(self.instructions)

        self.stitch_coords = []
        print("Self size:", self.size, "texture size:",
              self.image.texture_size, "Scale:", self.scale)
        print("Pattern size:", self.fabric.size)
Beispiel #13
0
    def visit_AnnAssign(self, node):
        if not isinstance(node.target, ast.Name):
            return node

        if isinstance(node.annotation, ast.Tuple):
            p = [pattern.parse(x) for x in node.annotation.elts]
        else:
            p = pattern.parse(node.annotation)

        if node.value == None:
            raise SyntaxError("No function body")

        func_name = node.target.id
        func_body = node.value
        if func_name not in self.defines:
            self.defines[func_name] = pattern.Pattern(func_name, self)
        self.defines[func_name].add(p, func_body)

        return ast.fix_missing_locations(
            ast.copy_location(ast.Expr(ast.Num(0)), node))
Beispiel #14
0
    def __init__(self, system, name, func):
        self.system = system
        self.name = name
        self.base_utility = 0
        a, va, hk, d = inspect.getargspec(func)
        self.keys = a
        patterns = {}
        for i, name in enumerate(a[:]):
            if name == 'utility':
                self.base_utility = d[i]
                del a[i]
            else:
                patterns[name] = d[i]
        self.pattern_specs = patterns
        self.pattern = pattern.Pattern(patterns)
        self.bound = None

        self.original_func = func
        code = inspect.getsource(func)
        m = re.match(r'[^(]+\([^(]*\):', code)
        self.code = code[m.end():]
        code = 'if True:' + code[m.end():]
        self.func = compile(code, '<production-%s>' % self.name, 'exec')
Beispiel #15
0
def make_chart(image, style):
    my_pattern = pattern.Pattern(image, 'dmcfloss')
    my_pattern.build_pattern()
    my_pattern.render_chart(style)
    return
Beispiel #16
0
def main():
    '''
    # Main Function
    execute hook for setuptools install
    defined in setup.py
    '''

    # Parse commandline arguments and create helppage
    parser = argparse.ArgumentParser(
            description='Simple test-suite for json/yaml files, optimized for CI pipelines.')

    parser.add_argument('pattern', type=str, nargs='+',
                        help="Files containing patterns to apply to the JSON-Document")

    group = parser.add_mutually_exclusive_group()

    group.add_argument('--stdin', action='store_true',
                       help='read document to check from `stdin`')
    group.add_argument('-f', '--file', type=str, metavar='STR',
                       help='read document to check from file.')
    group.add_argument('-u', '--url', type=str, metavar='STR',
                       help='read document to check from URL')

    parser.add_argument('--encoding', type=str, metavar='STR', default='UTF-8',
                        help='set file encoding - default is UTF-8')

    parser.add_argument('--prefix', type=str, metavar='STR', default='_',
                        help='meta and function prefix used by jMatch, default is "_"')

    parser.add_argument('--format', choices=['JSON', 'YAML'], default='json',
                        help='format of the document to check')

    parser.add_argument('-t', '--trace', action='store_true',
                        help='show routes to matching elements in the document')

    parser.add_argument('-p', '--path', action='store_true',
                        help='print the source path of the used pattern')

    parser.add_argument('-s', '--stats', action='store_true',
                        help='show statistics for all the performed checks')

    args = parser.parse_args()


    # Get data to check as string
    data: str = ''

    if args.stdin is True:
        data = str(sys.stdin.read())
    elif args.file is not None:
        path: str = os.path.realpath(args.file)
        if not os.path.exists(path):
            print('The file "{0}" does not exist.'.format(args.file))
        data = open(path, 'r', encoding=args.encoding).read()
    elif args.url is not None:
        request = requests.get(args.url)
        if request.status_code != 200:
            message = 'The url: "{0}" is not accessible. The request returned status code {1}'
            print(message.format(args.url, request.status_code))
            exit(1)
        data = request.text
    else:
        print('You need to specify a --target file, a --url or --stdin to read data.')
        exit(1)

    # Output Headline
    if args.file is not None:
        termcolor.cprint('\nAnalysis of {0}\n'.format(os.path.realpath(args.file)), attrs=['bold'])

    # Get pattern decoder
    decoder: typing.Union[typing.Callable, None] = None
    decode_format = str(args.format).lower()
    if decode_format in ['json']:
        decoder = json.loads
    elif decode_format in ['yml', 'yaml']:
        decoder = yaml.safe_load
    else:
        print('The specified format "{0}" is not supported.'.format(args.format))
        exit(1)

    # Create List of all patterns to check and their metadata
    checks: list = []
    required_names = ['message', 'type', 'pattern']
    for pattern_file in args.pattern:
        path = os.path.realpath(pattern_file)
        if not os.path.exists(path):
            print('The pattern file "{0}" does not exist.'.format(pattern_file))
            exit(1)
        try:
            decoded_check = decoder(open(path, 'r', encoding=args.encoding).read())
        except json.decoder.JSONDecodeError as e:
            print('The patternfile "{0}" is no valid JSON file.')

        required_keys = map(lambda s: '{0}{1}'.format(args.prefix, s.lower()), required_names)
        meta_wrong_error = 'Your Metadata is missing one of the folowing keys: {0} '

        def meta_ok(check: dict):
            return all(list(map(lambda x: x in check.keys(), required_keys)))

        if isinstance(decoded_check, list):
            for check in decoded_check:
                if not meta_ok(check):
                    exit(1)
                check['path'] = path
                checks.append(check)
        elif isinstance(decoded_check, dict):
            if not meta_ok(decoded_check):
                print(meta_wrong_error.format(required_keys))
                exit(1)
            checks.append(dict(**decoded_check, **{'path': pattern_file}))

    # Check all patterns and collect data
    exit_code: int = 0
    for idx, check in enumerate(checks):
        current_pattern = pattern.Pattern(check['{0}pattern'.format(args.prefix)], args.prefix)
        checks[idx]['matches'], checks[idx]['info'] = current_pattern.matches(decoder(data))
        if checks[idx]['matches'] and check['{0}type'.format(args.prefix)].lower() in ['error']:
            exit_code = 1

    # Display response
    for check in checks:
        if check['matches']:
            color = 'red' if check['{0}type'.format(args.prefix)] in 'error' else 'green'
            message = termcolor.colored(check['{0}message'.format(args.prefix)], color, attrs=['bold'])
            print(' ' * 2 + message + '\n')

            if args.path:
                message = ' ' * 4 + 'Pattern-file: \n      => "{0}"\n'
                print(termcolor.colored(message.format(check['path'])))

            if args.trace:
                print(termcolor.colored(' ' * 4 + 'Routes:'))
                for trace in check['info']['traces']:
                    print(' ' * 6 + '=> ' + (trace if trace != '' else "[]"))
                print()

    if args.stats:

        error_filter = lambda x: x['matches'] and x['{0}type'.format(args.prefix)] == 'error'
        summary = '{0} checks performed, {1} patterns matched, {2} of them are errors\n'.format(
            len(checks),
            len(list(filter(lambda x: x['matches'], checks))),
            len(list(filter(error_filter, checks)))
        )
        print('  {0} {1}'.format(termcolor.colored('Summary:', attrs=['bold']), summary))
    else:
        print()

    exit(exit_code)
Beispiel #17
0
        result = []

        if self.__pattern:
            patterncode = self.__pattern.getPatternCode()
            tmp = u"pattern='%s'" % patterncode
            '''pos = self.__pattern.getPos3in()
			patterncode = patterncode[0:pos] + u'0' + patterncode[pos+1:]
			tmp += u" OR pattern='%s')" % patterncode'''
            result.append(tmp)
            '''start = self.__pattern.getStart()
			beginlen = len(start)
			
			if beginlen > 0 :
				tmp = u"substr(%s,1,%s)='%s'" % (WORD, str(beginlen), start)
				result.append(tmp)
			'''
        if len(self.__end) > 0:
            endlen = len(self.__end)
            tmp = u"substr(%s,length(%s) - %s,%s)='%s'" % (
                WORD, WORD, str(endlen - 1), str(endlen), self.__end)
            result.append(tmp)
        return result


if __name__ == '__main__':
    import arudquery
    q = ArQuery()
    q.setPattern(pattern.Pattern(u'مٌسْتَفْعَل'))
    t = q.getResult()
    print t
Beispiel #18
0
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
from db.litebase import liteBase
import pattern
import arudquery
#~ import wazn

if __name__ == '__main__':

    dbpath = os.path.realpath('test/arabicdict.sqlite')
    print dbpath
    srcdb = liteBase(dbpath)
    tab = srcdb.getTable('nouns')

    queryengine = arudquery.ArQuery()
    arpattern = pattern.Pattern(u'مَفْعَل')

    queryengine.setPattern(arpattern)  #wazn.V3I4R3B
    queryengine.setEnd(u'ب')

    conditions = queryengine.getResult()
    print conditions

    rows = tab.getData(conditions)
    for row in rows:
        word = tab.getColumnIndex('word')
        patt = tab.getColumnIndex('pattern')
        print row[word] + "   " + row[patt]