Exemple #1
0
def main():
    args_parser = argparse.ArgumentParser(
        description="Updates 'Chrome Browser Network Traffic Annotations' doc."
    )
    args_parser.add_argument("--config-file", help="Configurations file.")
    args_parser.add_argument(
        "--annotations-file",
        help="TSV annotations file exported from auditor.")
    args_parser.add_argument("--verbose",
                             action="store_true",
                             help="Reports all updates. "
                             " Also creates a scripts/template.json file "
                             " outlining the document's current structure.")
    args_parser.add_argument("--config-help",
                             action="store_true",
                             help="Shows the configurations help.")
    args = args_parser.parse_args()

    if args.config_help:
        print_config_help()
        return 0

    # Load and parse config file.
    with open(os.path.join(SRC_DIR, args.config_file)) as config_file:
        config = json.load(config_file)

    tsv_contents = load_tsv_file(os.path.join(SRC_DIR, args.annotations_file),
                                 False)
    if not tsv_contents:
        print("Could not read annotations file.")
        return -1

    xml_parser = XMLParser(
        os.path.join(SRC_DIR, "tools/traffic_annotation/summary/grouping.xml"),
        map_annotations(tsv_contents))
    placeholders = xml_parser.build_placeholders()
    print("#" * 40)
    print("There are:", len(placeholders), "placeholders")
    if args.verbose:
        print(placeholders)
    print("#" * 40)

    network_traffic_doc = NetworkTrafficAnnotationsDoc(
        doc_id=config["doc_id"],
        doc_name=config["doc_name"],
        credentials_file_path=config["credentials_file_path"],
        client_token_file_path=config["client_token_file_path"],
        verbose=args.verbose)

    if not network_traffic_doc.update_doc(placeholders):
        return -1

    return 0
Exemple #2
0
def runtests(filename):
    f = open(filename)
    tests = f.read().split("#data\n")
    errorAmount = index = 0
    errorLog = []
    for index, test in enumerate(tests):
        if test == "": continue
        test = "#data\n" + test
        input, expected, errors = parseTestcase(test)
        parser = XMLParser()
        result = parser.parse(input).printTree()
        if result != "#document\n" + expected:
            errorAmount += 1
            errorLog.append("For:\n" + input + "\nExpected:\n" + expected + "\nGot:\n" + result + "\n\n")
    if errorAmount == 0:
        print "All Good!"
    else:
        print "\n" + "".join(errorLog)
Exemple #3
0
def runtests(filename):
    f = open(filename)
    tests = f.read().split("#data\n")
    errorAmount = index = 0
    errorLog = []
    for index, test in enumerate(tests):
        if test == "": continue
        test = "#data\n" + test
        input, expected, errors = parseTestcase(test)
        parser = XMLParser()
        result = parser.parse(input).printTree()
        if result != "#document\n" + expected:
            errorAmount += 1
            errorLog.append("For:\n" + input + "\nExpected:\n" + expected +
                            "\nGot:\n" + result + "\n\n")
    if errorAmount == 0:
        print "All Good!"
    else:
        print "\n" + "".join(errorLog)
Exemple #4
0
def xml_to_text(stream):
    if type(stream) is str:
        stream = XMLParser(stream)

    encoding = 'utf-8'
    text = []
    for event, value, line in stream:
        # TODO Extract some attribute values
        if event == TEXT:
            text.append(value)
        elif event == XML_DECL:
            encoding = value[1]
    return unicode(' '.join(text), encoding)
	def __init__(self,):
		'''
		filename - the file that contains the words
		scores - dictionary containing all the letters and their score '''
		self.filename = 'sowpods.txt'
		self.api_key = '9832fd27-0fe4-4bc6-961a-e2e74b2d9bb9'
		self.parser = XMLParser()
		self.scores = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
						"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
						"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
						"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
						"x": 8, "z": 10}
		self.load_file()
Exemple #6
0
 def __init__(self, xml, polly_xml=None, symsubs=None, namesubs=None):
     self.functions = XMLParser(xml, symsubs, namesubs).functions
     if polly_xml:
         self.scops = PollyXMLParser(polly_xml).scops
     else:
         self.scops = []
Exemple #7
0
# -*- coding: utf-8 -*-
from parser import XMLParser
from html_generator import HTMLGenerator

import sys

if __name__ == "__main__":

    if len(sys.argv) != 3:
        print "python " + __file__ + " <xml_file> <n_day>"
        print "e.g.:"
        print "> python " + __file__ + " input/schedule.xml 0"
        print "for printing the schedule of the first day."
        sys.exit(1)

    parser = XMLParser(sys.argv[1])
    conference = parser.parse()
    dumper = HTMLGenerator(conference)
    dumper.dump(int(sys.argv[2]))
Exemple #8
0
from parser import XMLParser

parser = XMLParser()

for x in range(1, 7):
    print(f"Test case {x}: ", end="")
    doc = str(open(f'sample{x}/ccd.xml').read()).strip()
    query = str(open(f'sample{x}/query.txt').read()).strip().replace('\n', ' ')
    output = str(open(f'sample{x}/output.txt').read()).strip()

    document = parser.parse(doc).find('ClinicalDocument')

    if document.execute_query(query) == output:
        print('Correct. Found', output)
    else:
        print("Incorrect. Did not find", output)
Exemple #9
0
class TestAssignment(unittest.TestCase):

	def setUp(self):
		""" Per Test setup."""
		restrictions = {"DateListed": "2016", "Description": "and"}
		required_fields = ["MlsId", "MlsName", "DateListed", "StreetAddress", "Price",
					  	   "Bedrooms", "Bathrooms", "Appliances", "Rooms", "Description"]
		self._initialize_xml_parser(restrictions, required_fields)

	def tearDown(self):
		""" Per Test tearDown"""
		if os.path.isfile("results.csv"):
			os.remove("results.csv")

	def _initialize_xml_parser(self, restrictions, required_fields):
		self._xml_parser = XMLParser(MagicMock(), MagicMock(),
									 restrictions, required_fields)

	def _validate_format_sub_nodes_output(self, results, field, expected):
		listing = MagicMock()
		listing.findall.return_value = results
		actual = self._xml_parser._format_sub_nodes(listing, field)
		self.assertEquals(expected, actual)

	def _get_mocked_results(self, values):
		results = []
		for value in values:
			result = MagicMock()
			result.text = value
			results.append(result)
		return results

	def test_format_sub_nodes_no_results(self):
		results = []
		expected = ""
		self._validate_format_sub_nodes_output(results, "field", expected)

	def test_format_sub_nodes_one_result(self):
		values = ["text"]
		results = self._get_mocked_results(values)
		expected = "text"
		self._validate_format_sub_nodes_output(results, "field", expected)

	def test_format_sub_nodes_multiple_results(self):
		values = ["dog", "cat", "horse", "cow"]
		results = self._get_mocked_results(values)
		expected = "dog,cat,horse,cow"
		self._validate_format_sub_nodes_output(results, "animals", expected)

	def _validate_get_bathroom_count_output(self, results, expected):
		listing = MagicMock()
		listing.find.return_value = results
		actual = self._xml_parser._get_bathroom_count(listing)
		self.assertEquals(expected, actual)

	def test_get_bathroom_count_no_values(self):
		results = []
		expected = '0'
		self._validate_get_bathroom_count_output(results, expected)

	def test_get_bathroom_count_throws_exception(self):
		results = Exception
		expected = '0'
		self._validate_get_bathroom_count_output(results, expected)

	def test_get_bathroom_count_with_values(self):
		results = MagicMock()
		results.text.return_value = '1'
		expected = '4'
		self._validate_get_bathroom_count_output(results, expected)

	def _validate_get_required_fields(self, find_results, findall_results, expected):
		listing = MagicMock()
		listing.find.return_value = find_results
		listing.findall.return_value = findall_results
		actual = self._xml_parser._get_required_fields(listing)
		self.assertEquals(expected, actual)

	def test_get_required_fields_no_fields(self):
		self._initialize_xml_parser(dict(), list())
		expected = dict()
		self._validate_get_required_fields("", list(), expected)

	def test_get_parsed_data_no_restrictions(self):
		required_fields = ["MslID", "DateListed"]
		listings = [MagicMock()] * random.randrange(1, 100)
		self._initialize_xml_parser(dict(), required_fields)
		actual = self._xml_parser._get_parsed_data(listings)
		self.assertEquals(len(listings), len(actual))

	def _validate_turn_dict_to_csv(self, parsed_data, file_exists, expected):
		self.assertEquals(os.path.isfile("results.csv"), False)
		actual = self._xml_parser._turn_dict_to_csv(parsed_data)
		self.assertEquals(os.path.isfile("results.csv"), file_exists)
		self.assertEquals(actual, expected)

	def test_turn_dict_to_csv_empty_dict(self):
		parsed_data = dict()
		file_exists = False
		expected = "Unable to create CSV file."
		self._validate_turn_dict_to_csv(parsed_data, file_exists, expected)

	def test_turn_dict_to_csv_populated_dict(self):
		parsed_data = MagicMock()
		file_exists = True
		expected = "Successfully created CSV file."
		self._validate_turn_dict_to_csv(parsed_data, file_exists, expected)
Exemple #10
0
	def _initialize_xml_parser(self, restrictions, required_fields):
		self._xml_parser = XMLParser(MagicMock(), MagicMock(),
									 restrictions, required_fields)
Exemple #11
0
def test(string):
    x = XMLParser()
    tree = x.parse(string)
    print tree.printTree()
class ScrabbleSolver(object):

	def __init__(self,):
		'''
		filename - the file that contains the words
		scores - dictionary containing all the letters and their score '''
		self.filename = 'sowpods.txt'
		self.api_key = '9832fd27-0fe4-4bc6-961a-e2e74b2d9bb9'
		self.parser = XMLParser()
		self.scores = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
						"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
						"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
						"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
						"x": 8, "z": 10}
		self.load_file()

	def load_file(self):
		''' Open the file and load all the words from the file into a list '''
		#open the word file
		word_file = open(self.filename,'r')
		#create an empty list to sort the lowered words
		self.words = []
		#traverse the file line by line and append the lowered word without the newline character([:-2]) to the list
		for word in word_file:
			self.words.append(word[:-2].lower())
		#then we close the file
		word_file.close()
    	

	def check_rack(self,rack):
		'''
			checks all the words from the list and compares them to the substrings in the rack
		'''
		#we lower the letters in the rack so AAA is equal to aaa
		rack = rack.lower()
		if rack == '' or any(map(lambda x: x.isdigit(), rack)):
			raise ex.IncorrectRack(rack)
		#create an empty list to hold the letters that we have already used
		used_letters = []
		#create a dictionary to hold the key=words ,value = score
		self.scored_words = {}
		#traverse the words list
		for word in self.words:
			#we create a sentinel value that at the end of every word will check if the whole word is in the rack
			increment = 0
			for letter in word:
				#we check to see if the letter is in the rack and if the letter is not in used letter
				#if it is in the used we check the count of the letter in the rack and in the word
				if letter in rack and (letter not in used_letters or rack.count(letter)>=word.count(letter)):
					increment += 1
					used_letters.append(letter)
			#after we have traversed the word, 
			# we check if the increment is equal to the len of the word , again to see if the whole word is in the rack
			if increment == len(word):
				#score the word
				score = self._get_score(word)
				#add the word and the score to the scored words dict
				self.scored_words[word]=score
			#we delete everything in the used letters so we can start over
			used_letters = []
		return self._bucketize(self.scored_words)

	def _bucketize(self,info_dict):
		'''
			makes the info_dict into a dictionary with buckets containing 10 results each
		'''
		#we create the bucketized dict , key = bucket number, value = dict(key=word,value=score)
		to_return = OrderedDict()
		#we get the keys
		keys = info_dict.keys()
		#we check the number of buckets we should create
		buckets = len(info_dict) / 10
		#we loop over the number of buckets
		for index in xrange(buckets):
			#if buckets is  >= 1 then we are sure that there are atleast 10 items
			for x in xrange(10):
				try:
					to_return[index][keys[x]] = info_dict[keys[x]]
				except KeyError:
					to_return[index] = OrderedDict()
					to_return[index][keys[x]] = info_dict[keys[x]]
			#on every loop we make keys 10 items shorter
			keys = keys[10:]

		#if there are still items in list, meaning that they are less than 10
		if keys:
			#we set last key to 1 if the whole result is less than 10
			if buckets == 0:
				last_key = 1
			#or to the last bucket + 1
			else:
				last_key = max(to_return.keys())+1
			to_return[last_key] = OrderedDict()
			for key in keys:
				to_return[last_key][key] = info_dict[key]
		return to_return

	def sort(self,sort_by):
		''' sorts the results by word or by score '''

		if sort_by == 'word':
			#we create a dictionary to hold key=word length, value = list of words with that len
			sorted_len_dict = OrderedDict()
			#we get the words
			keys_to_sort = self.scored_words.keys()
			#creating a set to held the different lens
			key_lens = set((len(x) for x in keys_to_sort))
			current_len = min(key_lens)
			max_len = max(key_lens)
			while current_len <= max_len:
				for key in keys_to_sort:
					if len(key) == current_len:
						try:
							sorted_len_dict[current_len].append(key)
						except KeyError:
							sorted_len_dict[current_len] = [key]
				current_len += 1

			#create an ordered dict to hold the actual sorted words and their scores
			sorted_dict = OrderedDict()
			for key,value in sorted_len_dict.items():
				for val in sorted(value):
					sorted_dict[val] = self.scored_words[val]
			return self._bucketize(sorted_dict)

		elif sort_by == 'score':
			#dict to be returned
			to_return = OrderedDict()
			#instanciate a list
			keys = self.scored_words.values()
			keys.sort()
			while keys:
				for key in self.scored_words:
					if not keys:
						break
					if self.scored_words[key] == keys[0]:
						to_return[key] = self.scored_words[key]
						keys.pop(0)
			return self._bucketize(to_return)

	def _get_score(self,word):
		score = 0
		for letter in word:
			score += self.scores[letter]
		return score

	def get_def(self,word):
		endpoint = 'http://www.dictionaryapi.com/api/v1/references/collegiate/xml/{}?key={}'.format(word,self.api_key)
		result = requests.get(endpoint)
		result = result.text.encode('utf-8')
		try:
			result = self.parser.parse(result)
		except ex.NoFreeLunch as e:
			e.word = word
			raise e
		return result
Exemple #13
0
def test(string):
    x = XMLParser()
    tree = x.parse(string)
    print tree.printTree()