Exemple #1
0
 def test_standard_begin_end_tags(self):
     source = string_to_list("<% echo 'hi'; %>")
     expected = string_to_list("""
         <?php
         echo 'hi';
         ?>""")
     self._test(source, expected)
Exemple #2
0
 def test_break_apart_multiple(self):
     #TODO: This isn't working! Debug this test!
     source = string_to_list(
         '<?php switch ($i) { '
         'case 0: echo "i equals 0"; break; '
         'case 1: echo "i equals 1"; break; '
         'case 2: echo "i equals 2"; break; '
         '} '
         'if (true) { echo "True"; } else { echo "False"; }'
         '?>')
     expected = string_to_list("""
         <?php
         switch ($i) {
             case 0:
                 echo "i equals 0";
                 break;
             case 1:
                 echo "i equals 1";
                 break;
             case 2:
                 echo "i equals 2";
                 break;
         }
         if (true) {
             echo "True";
         }
         else {
             echo "False";
         }
         ?>""")
     self._test(source, expected)
Exemple #3
0
 def test_semicolon_ending(self):
     source = string_to_list("""
         <?php
         echo "This " . "is " .
              "a " . "test.";
         ?>""")
     expected = string_to_list("""
         <?php
         echo "This " . "is " . "a " . "test.";
         ?>""")
     self._test(source, expected)
Exemple #4
0
 def test_comment_line(self):
     source = string_to_list("""
         <?php
         /* this is a comment line */
         echo 'this is code';
         /* this is another comment line */
         >?""")
     expected = string_to_list("""
         <?php
         # this is a comment line
         echo 'this is code';
         # this is another comment line
         >?""")
     self._test(source, expected)
Exemple #5
0
def match(t_file, dm_file):
    """
    Open files, convert the text to list and clean it. Check for identical content.
    :param t_file: Full path to file 1, as string
    :param dm_file: Full path to file 2, as string
    :return: Three sets = matching content, not matched from file 1, not matched from file 2.
    """
    legacy = utils.open_file(t_file)
    ontology = utils.open_file(dm_file)
    legacy_occupations = utils.string_to_list(legacy)
    ontology_occupations = utils.string_to_list(ontology)
    cleaned_legacy = utils.clean_text(legacy_occupations)
    cleaned_ontology = utils.clean_text(ontology_occupations)
    return match_strings(cleaned_ontology, cleaned_legacy)
Exemple #6
0
 def test_open_brace_ending(self):
     source = string_to_list("""
         <?php
         if (true &&
             false) {
             echo "Impossible!";
         }
         ?>""")
     expected = string_to_list("""
         <?php
         if (true && false) {
             echo "Impossible!";
         }
         ?>""")
     self._test(source, expected)
Exemple #7
0
 def test_comment_block(self):
     source = string_to_list("""
         <?php
         /* this is a comment block
          * with a second line
          */
         echo 'this is code';
         >?""")
     expected = string_to_list("""
         <?php
         # this is a comment block
         # with a second line
         echo 'this is code';
         >?""")
     self._test(source, expected)
def get_taxonomy():
    a_file = open_file("resources/occupations_from_legacy_taxonomy.txt")
    strings = string_to_list(a_file)
    cleaned_strings = clean_text(strings)
    split_strings = map(split_slash, cleaned_strings)
    flatten_list = flatten(split_strings)
    return flatten_list
Exemple #9
0
 def test_indented(self):
     source = """
        one
        two
        three"""
     expected = ["one", "two", "three", ]
     got = string_to_list(source)
     self.assertEqual(got, expected)
Exemple #10
0
 def test_no_here_docs(self):
     php_lines = string_to_list("""
         <?php
         echo "Hello world"
         ?>""")
     updated_php, updated_python = strings.grab_heredocs(php_lines, [])
     self.assertEqual(updated_php, php_lines, "PHP should not have changed.")
     self.assertEqual(len(updated_python), 0, "Should be no python.")
Exemple #11
0
 def __init__(self, tiles, zone_name):
     char_name, char_kind = utils.get_char_info()
     self.player_name = char_name
     self.player_kind = char_kind
     self.zone_name = zone_name
     # -------------------------------------
     char_dir = "{}_{}".format(char_name, char_kind)
     filename = "{}.txt".format(char_kind)
     filepath = os.path.join("data", "player_files", char_dir, filename)
     if p: print("Player.__init__: reading from filepath:", filepath)
     with open(filepath, "r") as f:
         mylines = f.readlines()
         mylines = [i.strip() for i in mylines if len(i.strip()) > 0]
     mydict = {}
     for elem in mylines:
         mydict = utils.key_value_pair(elem, mydict)
     # -------------------------------------
     self.max_hp = int(mydict["max_hp"])
     self.max_mp = int(mydict["max_hp"])
     self.level = int(mydict["max_hp"])
     self.experience_points = int(mydict["experience_points"])
     # -------------------------------------
     self.gold = int(mydict["gold"])
     self.weapon = mydict["weapon"]  # We'll flesh this out down the road
     self.status_effects = None  # We'll flesh this out down the road
     self.explored_locations = []  # We'll flesh this out down the road
     self.has_killed = bool(
         mydict["has_killed"])  # We'll flesh this out down the road
     # -------------------------------------
     self.tiles = tiles
     self.current_tile = self.tiles.get_tile(mydict["current_tile_index"])
     # -------------------------------------
     self.items = Items()
     item_list = utils.string_to_list(mydict["items"])
     self.items.load_items(item_list)
     # -------------------------------------
     self.quests_current = Quests()
     quests_list = utils.string_to_list(mydict["quests_current"])
     self.quests_current.load_quests(quests_list)
     # ----
     self.quests_completed = Quests()
     quests_list = utils.string_to_list(mydict["quests_completed"])
     self.quests_completed.load_quests(quests_list)
Exemple #12
0
 def test_reformat_indentation(self):
     source = string_to_list("""
         <?php
         if (true) {
          if (false) {
           if (true) {
             echo "Nevermind.";
         }}}
         ?>""")
     expected = string_to_list("""
         <?php
         if (true) {
             if (false) {
                 if (true) {
                     echo "Nevermind.";
                 }
             }
         }
         ?>""")
     self._test(source, expected)
Exemple #13
0
 def do_update(self):
     for name in self.dirty:
         log.debug("Syncing data to %s settings", name)
         wgt = self.ui.get_widget(name)
         if name == "power-off-intervals":
             try:
                 list = utils.string_to_list(wgt.get_text())
             except Exception, e:
                 log.debug("invalid value of list %s", e)
                 # wgt.override_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(1.0, 0.0, 0.0, 1.0) )
             else:
                 # wgt.override_color(Gtk.StateFlags.NORMAL, None)
                 self.settings.set_formatted("poweroff-intervals", list, "ai")
Exemple #14
0
 def test_one_here_docs(self):
     #NOTE: This is pretty nasty to code. I wonder if we can make it easier
     # by allowing behaviors to accept multi-line strings, rather than lists
     # of lines? Or by stripping the \n during file input? Maybe...
     php_lines = string_to_list("""
         <?php
         echo "Hello world";
         x = <<<EOF
         Whatever, dude.
         EOF;
         ?>""")
     expected_php = string_to_list("""
         <?php
         echo "Hello world";
         x = HEREDOC_1
         ?>""")
     expected_python = string_to_list('''
         HEREDOC_1 = """
         Whatever, dude.
         """''')
     updated_php, updated_python = strings.grab_heredocs(php_lines, [])
     self.assertEqual(updated_php, expected_php)
     self.assertEqual(updated_python, expected_python)
Exemple #15
0
 def test_switch_statement(self):
     source = string_to_list("""
         <?php
         switch ($i) {
             case 0: echo "i equals 0"; break;
             case 1: echo "i equals 1"; break;
             case 2: echo "i equals 2"; break;
         }
         ?>""")
     expected = string_to_list("""
         <?php
         switch ($i) {
             case 0:
                 echo "i equals 0";
                 break;
             case 1:
                 echo "i equals 1";
                 break;
             case 2:
                 echo "i equals 2";
                 break;
         }
         ?>""")
     self._test(source, expected)
Exemple #16
0
 def __init__(self, mydict):
     if p: print("Npc.__init__:", mydict)
     self.name = mydict["name"].lower().strip()
     self.position = mydict["position"].lower().strip()
     self.kind = mydict["kind"].lower().strip()
     self.species = mydict["species"].lower().strip()
     # ----
     self.initial_greeting = mydict["initial_greeting"].strip()
     self.greeting = mydict["greeting"].strip()
     self.farewell = mydict["farewell"].strip()
     # ----
     self.items = Items()
     if mydict["items"] == "none":
         self.items = None
     else:
         mylist = utils.string_to_list(mydict["items"])
         self.items.load_items(mylist)
     # ----
     self.quests = Quests()
     if mydict["quests"] == "none":
         self.quests = None
     else:
         mylist = utils.string_to_list(mydict["quests"])
         self.quests.load_quests(mylist)
 def get(self):
     board = utils.string_to_list(self.request.get('board'))
     next_move = -1
     # check if they sent you a full board, this will happen if the player went first.
     if tictactoe.is_full_board(board):
         # if they did, just check the game state.
         game_state, message = tictactoe.get_game_state(board)
     else:
         try:
             next_move = tictactoe.get_move(board, config.COMPUTER)
             new_board = tictactoe.make_move(board, next_move, config.COMPUTER)
             game_state, message = tictactoe.get_game_state(new_board)
         except Exception as e:
             game_state, message = config.ERROR, e.message
     result = {'move': next_move, 'game_state': game_state, 'message': message}
     self.render_json(result)
Exemple #18
0
 def test_strip_newlines(self):
     source = [
         "<?php\n",
         "/* This\n",
         "   is\n",
         "   a\n",
         "   test. */\n",
         "?>"
     ]
     expected = string_to_list("""
         <?php
         /* This
            is
            a
            test. */
         ?>""")
     got, ignore = pretty.strip_php_newlines(source, [])
     self.assertEqual(got, expected)
	def _check_element(self, s):
		ll = []
		t = re.match(self._TRANSISTOR_PATTERN, s)
		r = re.match(self._RESISTOR_PATTERN, s)
		c = re.match(self._CAPACITOR_PATTERN, s)
		d = re.match(self._DIODE_PATTERN, s)
		n = re.match(self._NEW_LINE_PATTERN, s)
		cc = re.match(self._CELL_START_PATTERN, s)
		ce = re.match(self._CELL_END_PATTERN, s)
		if t == 0 and r == 0 and c == 0 and d == 0 and n == 0 and cc == 0:
			return "Unrecognizable element"
		else:
			if t:
				l = utils.string_to_list(s)
				ll = self._make_tokens(l, self._TRANSISTOR)
				return ll
			if r:
				l = utils.string_to_list(s)
				ll = self._make_tokens(l, self._RESISTOR)
				return ll
			if c:
				l = utils.string_to_list(s)
				ll = self._make_tokens(l, self._CAPACITOR)
				return ll
			if d:
				l = utils.string_to_list(s)
				ll = self._make_tokens(l, self._DIODE)
				return ll
			if n:
				l = utils.string_to_list(s)
				ll = self._make_tokens(l)
				return ll
			if cc:
				l = utils.string_to_list(s)
				ll = self._make_tokens(l)
				return ll
			if ce:
				l = utils.string_to_list(s)
				ll = self._make_tokens(l)
				return ll
Exemple #20
0
    def detect_roles(unclear_role):
        unclear_role = normalize(unclear_role)

        for role in ALL_ROLES:
            examples = role['examples']
            for example in examples:
                if unclear_role == example:
                    return set([role["name"]])

        terms = string_to_list(unclear_role)
        roles = set([])

        for term in terms:
            if term in TERMS_ROLES:
                roles = roles | set([TERMS_ROLES[term]])

        if roles:
            return roles

        for term in terms:
            threshold = get_threshold(len(term))
            best_fit_term = ""

            for example, role in TERMS_ROLES.items():
                dis = distance(example, term)

                if dis < threshold or dis == threshold:
                    threshold = dis
                    best_fit_term = example

            if best_fit_term:
                roles = roles | set([TERMS_ROLES[best_fit_term]])

        if roles:
            return roles

        return set(["UNCERTAIN"])
    testset = torchvision.datasets.CIFAR10(root='data',
                                           train=False,
                                           download=True,
                                           transform=transform_test)
    testloader = torch.utils.data.DataLoader(testset,
                                             batch_size=100,
                                             shuffle=False,
                                             num_workers=2)

    classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse',
               'ship', 'truck')

    #Model
    net = DummyModel()

    device_ids = string_to_list(args.device_ids)
    net, device = multi_gpu_model_device(device_ids, net)

    if args.resume:
        # Load checkpoint.
        print('==> Resuming from checkpoint..')
        assert os.path.isdir(
            'checkpoint'), 'Error: no checkpoint directory found!'
        checkpoint = torch.load('./checkpoint/ckpt.pth')
        net.load_state_dict(checkpoint['net'])
        best_acc = checkpoint['acc']
        start_epoch = checkpoint['epoch']

    criterion = nn.CrossEntropyLoss()
    optimizer = optim.SGD(net.parameters(),
                          lr=args.lr,
Exemple #22
0
    def populate_references(self):
        session = self._db_client.get_session()

        authority_id_to_related = {
        }  # From ID to tuple of citations, related authorities, and related inquests.
        inquest_id_to_related = {
        }  # From ID to tuple of related authorities and related inquests.

        logger.info('Populating authorities.')

        for row in self._read_workbook('Authorities'):
            (r_authority_id, r_name, r_overview, r_synopsis, r_quotes, r_notes,
             r_primary, r_primary_text, r_judicial_review, r_keywords, r_tags,
             r_citations, r_see_authorities, r_see_inquests) = row

            session.add(
                models.Authority(authorityId=r_authority_id,
                                 isPrimary=r_primary,
                                 primaryField=utils.format_string(
                                     r_primary_text,
                                     utils.NullBehaviour.NULL_TO_NULL),
                                 isJudicialReview=r_judicial_review,
                                 name=utils.format_string(r_name),
                                 overview=utils.format_string(
                                     r_overview,
                                     utils.NullBehaviour.NULL_TO_STRING),
                                 synopsis=utils.format_string(
                                     r_synopsis,
                                     utils.NullBehaviour.NULL_TO_STRING),
                                 quotes=utils.format_string(
                                     r_quotes,
                                     utils.NullBehaviour.NULL_TO_NULL),
                                 notes=utils.format_string(
                                     r_notes,
                                     utils.NullBehaviour.NULL_TO_NULL),
                                 tags=utils.format_as_list(
                                     r_tags,
                                     utils.NullBehaviour.NULL_TO_NULL)))
            session.flush()

            # TODO: behaviour should be NOT NULL.
            for keyword_id in utils.string_to_list(
                    r_keywords, utils.NullBehaviour.NULL_TO_NULL):
                session.add(
                    models.AuthorityKeywords(
                        authorityId=r_authority_id,
                        authorityKeywordId=keyword_id,
                    ))

            authority_id_to_related[r_authority_id] = (r_citations,
                                                       r_see_authorities,
                                                       r_see_inquests)
            self._reference_to_name[(
                self._REFERENCE_TYPE_AUTHORITY,
                int(r_authority_id))] = utils.format_string(r_name)

        logger.info('Populating inquests.')

        for row in self._read_workbook('Inquests'):
            (r_inquest_id, r_name, r_overview, r_synopsis, r_notes,
             r_presiding_officer, r_location, r_start, r_end, r_sitting_days,
             r_exhibits, r_recommendations, r_jurisdiction_id, r_primary,
             r_primary_text, r_keywords, r_tags, r_see_authorities,
             r_see_inquests) = row

            session.add(
                models.Inquest(inquestId=r_inquest_id,
                               jurisdictionId=r_jurisdiction_id,
                               location=utils.format_string(
                                   r_location,
                                   utils.NullBehaviour.NULL_TO_NULL),
                               isPrimary=r_primary,
                               name=utils.format_string(r_name),
                               overview=utils.format_string(
                                   r_overview,
                                   utils.NullBehaviour.NULL_TO_NULL),
                               synopsis=utils.format_string(
                                   r_synopsis,
                                   utils.NullBehaviour.NULL_TO_STRING),
                               notes=utils.format_string(
                                   r_notes, utils.NullBehaviour.NULL_TO_NULL),
                               presidingOfficer=utils.format_string(
                                   r_presiding_officer,
                                   utils.NullBehaviour.NULL_TO_STRING),
                               start=utils.string_to_date(r_start),
                               end=utils.string_to_date(r_end),
                               sittingDays=utils.string_to_int(
                                   r_sitting_days,
                                   utils.NullBehaviour.NULL_TO_NULL),
                               exhibits=utils.string_to_int(
                                   r_exhibits,
                                   utils.NullBehaviour.NULL_TO_NULL),
                               recommendations=utils.string_to_int(
                                   r_recommendations,
                                   utils.NullBehaviour.NULL_TO_NULL),
                               tags=utils.format_as_list(
                                   r_tags, utils.NullBehaviour.NULL_TO_NULL)))
            session.flush()

            inquest_id_to_related[r_inquest_id] = (r_see_authorities,
                                                   r_see_inquests)
            self._reference_to_name[(
                self._REFERENCE_TYPE_INQUEST,
                int(r_inquest_id))] = utils.format_string(r_name)

            # TODO: use behaviour NOT_NULL.
            for keyword_id in utils.string_to_list(
                    r_keywords, utils.NullBehaviour.NULL_TO_NULL):
                # TODO: remove this if statement.
                if keyword_id != 'INQUEST_FEDERAL_PROVINCIAL':
                    session.add(
                        models.InquestKeywords(
                            inquestId=r_inquest_id,
                            inquestKeywordId=keyword_id,
                        ))

        logger.info('Populating authority relationships.')

        for (r_authority_id,
             (r_citations, r_see_authorities,
              r_see_inquests)) in authority_id_to_related.items():
            for citation in utils.string_to_list(
                    r_citations, utils.NullBehaviour.NULL_TO_NULL):
                session.add(
                    models.AuthorityCitations(
                        authorityId=r_authority_id,
                        citedAuthorityId=citation,
                    ))
            for related_authority in utils.string_to_list(
                    r_see_authorities, utils.NullBehaviour.NULL_TO_NULL):
                session.add(
                    models.AuthorityRelated(
                        authorityId=r_authority_id,
                        relatedAuthorityId=related_authority,
                    ))
            for related_inquest in utils.string_to_list(
                    r_see_inquests, utils.NullBehaviour.NULL_TO_NULL):
                session.add(
                    models.AuthorityInquests(
                        authorityId=r_authority_id,
                        inquestId=related_inquest,
                    ))

        logger.info('Populating inquest relationships.')

        # TODO: add tables for inquest relationships.

        session.commit()
def get_ontology():
    ontology = open_file("resources/ontology_all_occupations.txt")
    ontology_occupations = string_to_list(ontology)
    cleaned_legacy = clean_text(ontology_occupations)
    return cleaned_legacy