Ejemplo n.º 1
0
	def __parse(self):
		print "Parsing file..."
		parser = FileParser()
		self.lines = parser.parse(file)

		# contains distinct flows, opened with a SYN
		self.flows = {}

		for line in self.lines:
			line = line.strip()
			tokens = line.split(' ')
			tokens = self.__clean(tokens)
			if '.' not in tokens[1]:
				continue

			key = self.__get_key(tokens)

			if "SYN" in line:
				self.flows[key] = []
				continue

			if "Len=0" in line:
				continue

			time = float(tokens[1])
			length = 0
			for token in tokens:
				if "Len=" in token:
					length = int(token.split('=')[1])
					break

			self.flows[key].append((time, length))
Ejemplo n.º 2
0
def main():
    for filename in os.listdir('files'):
        filename = f'files/{filename}'
        if 'txt' in filename:
            p = FileParser(filename, '\n' * 3)
            p.start()
        else:
            raise ValueError('Unknown file type')
Ejemplo n.º 3
0
class EasySubConsole(object):
    def __init__(self):
        super(EasySubConsole, self).__init__()
        self._file_parser = FileParser()
        self._easysub = EasySub()

    def _usage(self):
        return u"""Usage:
	main.py path [path, ...]
	
	path:    File or directory's absolute path
		"""

    def _exit(self, code=0):
        sys.exit(code)

    def _validate_paths(self, paths):
        for path in paths:
            if not os.path.exists(path):
                return False
        return True

    def _get_files_from_paths(self, paths):
        files = list()
        for path in paths:
            if os.path.isfile(path) and self._file_parser.is_media_file(path):
                file = File(path=path)
                if not os.path.exists(file.sub_absolute_path):
                    files.append(file)
            else:
                for item in os.listdir(path):
                    item_path = os.path.join(path, item)
                    if os.path.isfile(
                            item_path) and self._file_parser.is_media_file(
                                item_path):
                        file = File(path=item_path)
                        if not os.path.exists(file.sub_absolute_path):
                            files.append(file)
        return files

    def run(self, args):
        if not args:
            print self._usage()
            self._exit(1)
        if not self._validate_paths(args):
            print u'Some of the input paths are not valid.'
            self._exit(2)
        files = self._get_files_from_paths(args)
        files_n = len(files)
        print unicode(files_n) + u' media files without subtitles found!'
        for file in files:
            if self._easysub.process_file(file):
                print os.path.basename(
                    file.sub_absolute_path) + u' is available.'
            else:
                print os.path.basename(
                    file.sub_absolute_path) + u' could not be downloaded.'
        self._exit(0)
Ejemplo n.º 4
0
def main():
    inputfile = FileParser()
    T = inputfile.read_int()
    for test in range(1, T + 1):
        S = inputfile.read_string()

        result = solve(S)

        print "Case #{}: {}".format(test, result)
Ejemplo n.º 5
0
def main():
    inputfile = FileParser()
    T = inputfile.read_int()
    for test in range(1, T + 1):
        C, J = inputfile.read_strings()

        result = solve(C, J)

        print "Case #{}: {} {}".format(test, result[0], result[1])
Ejemplo n.º 6
0
class EasySubConsole(object):
	def __init__(self):
		super(EasySubConsole, self).__init__()
		self._file_parser = FileParser()
		self._easysub = EasySub()

	def _usage(self):
		return u"""Usage:
	main.py path [path, ...]
	
	path:    File or directory's absolute path
		"""
		
	def _exit(self, code=0):
		sys.exit(code)

	def _validate_paths(self, paths):
		for path in paths:
			if not os.path.exists(path):
				return False
		return True

	def _get_files_from_paths(self, paths):
		files = list()
		for path in paths:
			if os.path.isfile(path) and self._file_parser.is_media_file(path):
				file = File(path=path)
				if not os.path.exists(file.sub_absolute_path):
					files.append(file)
			else:
				for item in os.listdir(path):
					item_path = os.path.join(path, item)
					if os.path.isfile(item_path) and self._file_parser.is_media_file(item_path):
						file = File(path=item_path)
						if not os.path.exists(file.sub_absolute_path):
							files.append(file)
		return files

	def run(self, args):
		if not args:
			print self._usage()
			self._exit(1)
		if not self._validate_paths(args):
			print u'Some of the input paths are not valid.'
			self._exit(2)
		files = self._get_files_from_paths(args)
		files_n = len(files)
		print unicode(files_n) + u' media files without subtitles found!'
		for file in files:
			if self._easysub.process_file(file):
				print os.path.basename(file.sub_absolute_path) + u' is available.'
			else:
				print os.path.basename(file.sub_absolute_path) + u' could not be downloaded.'
		self._exit(0)
Ejemplo n.º 7
0
def main():
    inputfile = FileParser()
    T = inputfile.read_int()
    for test in range(1, T + 1):
        N = inputfile.read_int()

        senators = inputfile.read_integers()

        result = solve(senators)

        print "Case #{}: {}".format(test, " ".join(result))
Ejemplo n.º 8
0
 def load_std(self):
     """
     Adds the core libraries to the environment
     """
     for i in env.standard_env:
         libs = env.include_lib(i)
         for lib in libs:
             if lib[0] == "py":
                 self.vm.env.update(lib[1])
             elif lib[0] == "pyl":
                 file_parse = FileParser(lib[1], self.vm)
                 file_parse.run()
Ejemplo n.º 9
0
def main():
    inputfile = FileParser()
    T = inputfile.read_int()
    for test in range(1, T + 1):
        J, P, S, K = inputfile.read_integers()

        solution = solve(J, P, S, K)

        assert len(solution) <= 1000
        assert len(solution) <= J * P * S
        assert len(solution) >= 1

        print "Case #{}: {}".format(test, len(solution))
        for j, p, s in solution:
            print "{} {} {}".format(j+1, p+1, s+1)
Ejemplo n.º 10
0
def main():
    inputfile = FileParser()
    T = inputfile.read_int()
    for test in range(1, T + 1):
        B, M = inputfile.read_integers()

        solution = solve(B, M)

        if solution is None:
            print "Case #{}: IMPOSSIBLE".format(test)
        else:
            print "Case #{}: POSSIBLE".format(test)
            for row in solution:
                print "".join(["1" if x else "0" for x in row])
                pass
Ejemplo n.º 11
0
    def __init__(self, app, spec, driver='gl'):
        self.app = app
        self.spec = spec
        self.widgets = []
        self.focus_order = None
        self.parser = FileParser(self.app)
        self.widgets = self.parser.parse(self.spec["widgets"])
        self.canvas = Canvas(self.spec["app"]["width"],
                             self.spec["app"]["height"])
        self.display = Display(self.canvas)
        self.display.set_driver(driver)

        if len(self.widgets) > 0:
            self.focused_widget = self.widgets[0]
        self.build_focus_order()
Ejemplo n.º 12
0
 def __init__(self):
     self.spaces_available = []
     self.people = FileParser().read_file()
     self.staff = self.people.get_staff()
     self.fellows = self.people.get_fellows()
     self.males = self.people.get_male_residential_fellows()
     self.females = self.people.get_female_residential_fellows()
     self.unallocated_staff = []
     self.unallocated_fellows_to_officespaces = []
     self.unallocated_fellows_to_livingspaces = []
Ejemplo n.º 13
0
    def __parse__(self):
        parser = FileParser()
        packets = False
        acks = False
        drops = False
        do_nothing = False
        for line in parser.parse(self.input):
            if len(line) == 0:
                continue

            if "skip" in line:
                do_nothing = True

            if line.startswith("\"packets"):
                packets = True
                do_nothing = False
                continue

            if line.startswith("\"acks"):
                acks = True
                drops = False
                do_nothing = False
                continue

            if line.startswith("\"drops"):
                drops = True
                do_nothing = False
                continue

            if do_nothing:
                continue

            if packets and not acks:
                if drops:
                    self.packet_drops.append(line)
                else:
                    self.packets.append(line)

            if acks:
                if drops:
                    self.ack_drops.append(line)
                else:
                    self.acks.append(line)
Ejemplo n.º 14
0
	def __parse__(self):
		parser = FileParser()
		packets = False
		acks = False
		drops = False
		do_nothing = False
		for line in parser.parse(self.input):
			if len(line) == 0:
				continue

			if "skip" in line:
				do_nothing = True
				
			if line.startswith("\"packets"):
				packets = True
				do_nothing = False
				continue
				
			if line.startswith("\"acks"):
				acks = True
				drops = False
				do_nothing = False
				continue

			if line.startswith("\"drops"):
				drops = True
				do_nothing = False
				continue

			if do_nothing:
				continue

			if packets and not acks:
				if drops:
					self.packet_drops.append(line)
				else:
					self.packets.append(line)
			
			if acks:
				if drops:
					self.ack_drops.append(line)
				else:
					self.acks.append(line)
Ejemplo n.º 15
0
class EasySub(object):
	def __init__(self):
		super(EasySub, self).__init__()
		self._file_parser = FileParser()
		self._subscene = Subscene()

	def _get_search_term(self, file):
		return u' '.join(self._file_parser.parse_filename(file.name))
		
	def process_file(self, file):
		search_term = self._get_search_term(file)
		try:
			results = self._subscene.search(search_term)
		except Exception, e:
			pass
		else:
Ejemplo n.º 16
0
class EasySub(object):
    def __init__(self):
        super(EasySub, self).__init__()
        self._file_parser = FileParser()
        self._subscene = Subscene()

    def _get_search_term(self, file):
        return u' '.join(self._file_parser.parse_filename(file.name))

    def process_file(self, file):
        search_term = self._get_search_term(file)
        try:
            results = self._subscene.search(search_term)
        except Exception, e:
            pass
        else:
Ejemplo n.º 17
0
 def __init__(self,
              trgt,
              path=None,
              matchdegree=10,
              change=1,
              remove=1,
              insert=1):
     if not path:
         path = os.getcwd()
     print(path)
     self.matchdegree = matchdegree
     self.trgt = trgt
     self.path = path
     self.fpob = FileParser()
     self.edob = EditDis(change, remove, insert)
     self.caob = Cache()
     self.res = Heap(max=False)
     self.walker()
Ejemplo n.º 18
0
class TartanDisplay(object):
    def __init__(self, app, spec, driver='gl'):
        self.app = app
        self.spec = spec
        self.widgets = []
        self.focus_order = None
        self.parser = FileParser(self.app)
        self.widgets = self.parser.parse(self.spec["widgets"])
        self.canvas = Canvas(self.spec["app"]["width"],
                             self.spec["app"]["height"])
        self.display = Display(self.canvas)
        self.display.set_driver(driver)

        if len(self.widgets) > 0:
            self.focused_widget = self.widgets[0]
        self.build_focus_order()

    def mark_dirty(self):
        self.dirty = True

    def mark_clean(self):
        self.dirty = False

    def build_focus_order(self):
        self.focus_order = self.widgets

    def refresh(self):
        self.canvas.clear()
        self.build_display()
        self.display.refresh()

    def blit(self, widget):
        self.canvas.blit(widget.anchor[0], widget.anchor[1], widget.canvas)

    def build_display(self):
        self.canvas.clear()
        for widget in self.widgets:
            widget.draw()
            self.blit(widget)
Ejemplo n.º 19
0
def main():
    """
    The main function called when the program is run. Will either start the interpreter or run a file if a filename is provided
    """
    if len(sys.argv) == 1:
        from interpreter import Interpreter

        # Main loop which prompts user for input and print the response of the input handed to the rep function
        interpreter = Interpreter()
        interpreter.load_std()
        interpreter.cmdloop()
    else:
        from fileparser import FileParser
        from virtualmachine import VirtualMachine

        parser = FileParser(sys.argv[1], VirtualMachine({}))
        parser.load_std()
        parser.run()
Ejemplo n.º 20
0
	def __init__(self):
		super(EasySub, self).__init__()
		self._file_parser = FileParser()
		self._subscene = Subscene()
Ejemplo n.º 21
0
# -*- coding: UTF-8 -*-

from __future__ import division
from fileparser import FileParser

def solve(n):
    if n == 0:
        return False

    digits = set("0123456789")

    i = 0
    while len(digits) > 0:
        i += 1
        digits -= set(str(n * i))
        #print "{} {} {} --> {}".format(i, n * i, set(str(n * i)), digits)

    return n * i

inputfile = FileParser()
T = inputfile.read_int()
for test in range(1, T+1):
    N = inputfile.read_int()

    result = solve(N)

    if result:
        print "Case #{}: {}".format(test, result)
    else:
        print "Case #{}: INSOMNIA".format(test)
Ejemplo n.º 22
0
 def __init__(self):
     super(EasySubConsole, self).__init__()
     self._file_parser = FileParser()
     self._easysub = EasySub()
Ejemplo n.º 23
0
from fileparser import FileParser

if __name__ == "__main__":
    FileParser('lorem.txt', 'ipsum')
    FileParser('lorem.txt', 'IPS', 'ipsum')
Ejemplo n.º 24
0
from statemachine import StateMachine
from fileparser import FileParser

input_file = 'SampleStateTransitions.csv'
output_file = 'output/StateMachine'
output_format = 'svg'

# Parse csv file
nodes, transitions = FileParser.parse(input_file)

# Create state machine and render it
state_machine = StateMachine(output_format)
state_machine.add(nodes, transitions)
state_machine.render(output_file)
Ejemplo n.º 25
0
 def __init__(self):
     super(EasySub, self).__init__()
     self._file_parser = FileParser()
     self._subscene = Subscene()
Ejemplo n.º 26
0
	def __parse(self):
		parser = FileParser()

		s = set()

		packet_numbers = {}
		sequence_numbers = {}

		start = False;
		
		for line in parser.parse(self.input):
			array = line.split()
			if len(array) == 6:
				# header
				continue

			if "Len=0" not in line:
				start = True

			if not start:
				continue

			if "FIN" in line:
				break;

			is_ack = ("ACK" in line) and ("Len=0" in line)
			is_data = ("Len=0" not in line) or ("SYN" in line) or ("FIN" in line)

			packet_number = -1
			time = float(array[1]) + 1 # add one to be like ns-2

			sequence_number = -1
			length = -1
			ack_number = -1

			for item in array:
				if is_data and "Seq=" in item:
					sequence_number = int(item.split("=")[1])
					packet_number = self.__get_packet_number(sequence_number)
				elif is_ack and "Ack=" in item:
					ack_number = int(item.split("=")[1])
				elif "Len=" in item:
					length = int(item.split("=")[1])

			if "SYN" in line or "FIN" in line:
				length = 1
				
					
			temp = ""
			

			if packet_number < 10:
				temp = "0"

			tsvl = array[-2]
			tser = array[-1]
			ts = tsvl + tser

			if is_data:
				data = str(time) + " " + "1." + temp + str(packet_number)
				self.packets.append(data)
				packet_key = sequence_number + length
#				print "Packet key:    ", packet_key
#				print "Packet number: ", packet_number
				if packet_key not in packet_numbers.keys():
					packet_numbers[sequence_number + length] = packet_number

				
					
			else:
				assert is_ack
				if ts in s:
#					print "Ack number: ", ack_number
#
#					print "Ack packet number: ", packet_numbers[ack_number]

					temp = ""
					if packet_numbers[ack_number] < 10:
						temp = "0"
					
					data = str(time) + " " + "1." + temp + str(packet_numbers[ack_number])
#					print data
					self.acks.append(data)


			


			if length > 1 and sequence_numbers.has_key(sequence_number) and sequence_numbers[sequence_number] != ts:
				# duplicate
				for item in self.packets:					
					num = item.split()[1]
					num = int(num.split(".")[1])
					if num == self.__get_packet_number(sequence_number):
						self.packets.remove(item)
						self.packet_drops.append(item)
						
						break
				

			s.add(ts)


			if length > 1 and sequence_numbers.has_key(sequence_number) and sequence_numbers[sequence_number] == ts:
				del sequence_numbers[sequence_number]
			else:
				sequence_numbers[sequence_number] = ts
Ejemplo n.º 27
0
 def test_create_instance(self):
     try:
         p = FileParser()
         p.go()
     except Exception as e:
         self.assertEqual(type(e), TypeError)
Ejemplo n.º 28
0
    def __parse(self):
        parser = FileParser()

        s = set()

        packet_numbers = {}
        sequence_numbers = {}

        start = False

        for line in parser.parse(self.input):
            array = line.split()
            if len(array) == 6:
                # header
                continue

            if "Len=0" not in line:
                start = True

            if not start:
                continue

            if "FIN" in line:
                break

            is_ack = ("ACK" in line) and ("Len=0" in line)
            is_data = ("Len=0" not in line) or ("SYN" in line) or ("FIN"
                                                                   in line)

            packet_number = -1
            time = float(array[1]) + 1  # add one to be like ns-2

            sequence_number = -1
            length = -1
            ack_number = -1

            for item in array:
                if is_data and "Seq=" in item:
                    sequence_number = int(item.split("=")[1])
                    packet_number = self.__get_packet_number(sequence_number)
                elif is_ack and "Ack=" in item:
                    ack_number = int(item.split("=")[1])
                elif "Len=" in item:
                    length = int(item.split("=")[1])

            if "SYN" in line or "FIN" in line:
                length = 1

            temp = ""

            if packet_number < 10:
                temp = "0"

            tsvl = array[-2]
            tser = array[-1]
            ts = tsvl + tser

            if is_data:
                data = str(time) + " " + "1." + temp + str(packet_number)
                self.packets.append(data)
                packet_key = sequence_number + length
                #				print "Packet key:    ", packet_key
                #				print "Packet number: ", packet_number
                if packet_key not in packet_numbers.keys():
                    packet_numbers[sequence_number + length] = packet_number

            else:
                assert is_ack
                if ts in s:
                    #					print "Ack number: ", ack_number
                    #
                    #					print "Ack packet number: ", packet_numbers[ack_number]

                    temp = ""
                    if packet_numbers[ack_number] < 10:
                        temp = "0"

                    data = str(time) + " " + "1." + temp + str(
                        packet_numbers[ack_number])
                    #					print data
                    self.acks.append(data)

            if length > 1 and sequence_numbers.has_key(
                    sequence_number
            ) and sequence_numbers[sequence_number] != ts:
                # duplicate
                for item in self.packets:
                    num = item.split()[1]
                    num = int(num.split(".")[1])
                    if num == self.__get_packet_number(sequence_number):
                        self.packets.remove(item)
                        self.packet_drops.append(item)

                        break

            s.add(ts)

            if length > 1 and sequence_numbers.has_key(
                    sequence_number
            ) and sequence_numbers[sequence_number] == ts:
                del sequence_numbers[sequence_number]
            else:
                sequence_numbers[sequence_number] = ts
Ejemplo n.º 29
0
	def __init__(self):
		super(EasySubConsole, self).__init__()
		self._file_parser = FileParser()
		self._easysub = EasySub()
Ejemplo n.º 30
0
 def test_validator(self):
     try:
         FileParser.validator({1: 2, 2: 3})
     except Exception as e:
         self.assertEqual(type(e), trafaret.dataerror.DataError)
Ejemplo n.º 31
0
class Manager(object):
    """
    The manager object has methods for allocating people to spaces.
    """
    def __init__(self):
        self.spaces_available = []
        self.people = FileParser().read_file()
        self.staff = self.people.get_staff()
        self.fellows = self.people.get_fellows()
        self.males = self.people.get_male_residential_fellows()
        self.females = self.people.get_female_residential_fellows()
        self.unallocated_staff = []
        self.unallocated_fellows_to_officespaces = []
        self.unallocated_fellows_to_livingspaces = []

    def space_placing(self):
        """Create OfficeSpace and LivingSpace instances"""
        spaces = open(os.path.join('data', 'spaces.txt'))
        for line in iter(spaces):
            line = line.split()
            space_type = line[1]
            name = line[0]
            occupant_type = line[2]
            if space_type == 'OFFICE':
                space = OfficeSpace(name, space_type, occupant_type)
                self.spaces_available.append(space)
            elif space_type == 'ROOM':
                space = LivingSpace(name, space_type, occupant_type)
                self.spaces_available.append(space)

    def generate_spaces(self):
        """Generate spaces that are not filled"""
        for space in self.spaces_available:
            if not space.is_filled():
                yield space

    def list_spaces(self):
        """List of spaces_available"""
        return self.spaces_available

    def allocation(self):
        """Sorting out of spaces occupants"""

        for space in self.generate_spaces():
            try:
                # allocate each staff and fellow to an office
                if space.space_type == 'OFFICE':
                    for person in self.staff:
                        space.add_person(person)

                    for person in self.fellows:
                        space.add_person(person)

                # allocate each male-fellow to a male living space
                if space.occupant_type == 'MALE':
                    for person in self.males:
                        space.add_person(person)

                # allocate each female-fellow to a female living space
                if space.occupant_type == 'FEMALE':
                    for person in self.females:
                        space.add_person(person)
            except:
                    continue

        """Return unallocated people after allocation"""
        for person in self.people:
            if person.role == 'STAFF':
                if not person.has_officespace:
                    self.unallocated_staff.append(person)
            elif person.role == 'FELLOW':
                if not person.has_officespace:
                    self.unallocated_fellows_to_officespaces.append(person)
                if not person.has_livingspace:
                    self.unallocated_fellows_to_livingspaces.append(person)