Пример #1
0
def main():
    # Argument processing
    scriptDir = util.getScriptPath()
    parser = argparse.ArgumentParser(description='Compare annotations')
    parser.add_argument('-q',
                        '--quoteIds',
                        dest='quoteIdsFile',
                        help="List of original quoteIds")
    parser.add_argument('-c',
                        '--characters',
                        dest='characterDir',
                        help='directory for character files',
                        action='store',
                        default=os.path.join(scriptDir, '../data/characters'))
    parser.add_argument('file1',
                        help='file with new annotations',
                        action='store')
    parser.add_argument('file2',
                        help='file with old annotations',
                        action='store')
    args = parser.parse_args()

    quoteIds = []
    if args.quoteIdsFile != None:
        quoteIds = util.readlines(args.quoteIdsFile)
    compare(args.file1, args.file2, quoteIds)
Пример #2
0
def checkpath(path, structure):
    expectedFiles = structure.get('files')
    expectedAllFile = structure.get('allFile')
    print('checking', path)
    if expectedAllFile != None or expectedFiles != None:
        files = set(os.listdir(path))
        if structure.get('includesIndex'):
            files.remove(config.indexFileName)
            indexedFiles = util.readlines(
                os.path.join(path, config.indexFileName))
            for line in indexedFiles:
                assert line[
                    0] != '\n', 'Az .index fájl az utolsó sor kivételével nem tartalmazhat üres sorokat!'
            assert indexedFiles[-1][
                -1] == '\n', 'Az .index fájl utolsó sora egy üres sor kell hogy legyen! (most ez "' + indexedFiles[
                    -1] + '")'
            indexedFiles = set(
                [file.rstrip('\n') for file in indexedFiles if file != '\n'])
            assert files == indexedFiles, 'Az .index fájl tartalma invalid!'
        else:
            assert config.indexFileName not in files, 'Ennek a könyvtárnak nem lehet .index fájlja!'
        if expectedAllFile != None:
            for file in files:
                checkpath(os.path.join(path, file), expectedAllFile)
        elif expectedFiles != None:
            expectedFileSet = set(expectedFiles.keys())
            assert expectedFileSet == files, 'Ennek a könytárnak pontosan ezen fájlokat kell tartalmaznia: ' + str(
                expectedFileSet)
            for file in files:
                checkpath(os.path.join(path, file), expectedFiles[file])
    else:
        _, ext = os.path.splitext(path)
        assertByExt = assertsByExt.get(ext)
        if assertByExt != None:
            assertByExt(util.read(path), structure)
Пример #3
0
def main():
    # Argument processing
    parser = argparse.ArgumentParser(description='Convert CQSC XML')
    parser.add_argument('-s', '--split', dest='splitChapters', help='split by chapter', action='store_true')
    parser.add_argument('-p', dest='includeSectionTags', help='paragraphs and headings', action='store_true')
    parser.add_argument('-n', dest='extractNestedQuotes', help='extract nested quotes', action='store_true')
    parser.add_argument('indir', help='directory to use', action='store')
    args = parser.parse_args()

    files = util.readlines(args.indir + '/files.txt')
    for file in files:
        filename = args.indir + "/" + file
        (base,ext) = os.path.splitext(filename)
        match = re.match(r"(.*)_(\d+)", base)
        if match:
            base = match.group(1)
        charactersFile = base + ".characters.json"
        if not os.path.isfile(charactersFile):
            charactersFile = None
        print "convert " + filename + " with characters " + str(charactersFile)
        if args.splitChapters:
            base2 = os.path.basename(base)
            outbase = args.indir + "/converted_split/" + base2
            outfile = outbase + "/" + file
        else:
            outbase = args.indir + "/converted/"
            outfile = outbase + file
        if not os.path.exists(outbase):
            os.makedirs(outbase)
        try:
            cqsc.convertMentionLevels(filename, outfile, charactersFile, args.splitChapters, args.includeSectionTags, args.extractNestedQuotes)
        except:
            log.error("Unexpected error processing " + filename + ": ", exc_info=True)
Пример #4
0
def main():
    # Argument processing
    scriptDir = util.getScriptPath()
    parser = argparse.ArgumentParser(description='Compare annotations')
    parser.add_argument('-q', '--quoteIds', dest='quoteIdsFile', help="List of original quoteIds"); 
    parser.add_argument('-c', '--characters', dest='characterDir', help='directory for character files', 
        action='store', default=os.path.join(scriptDir, '../data/characters'))
    parser.add_argument('file1', help='file with new annotations', action='store')
    parser.add_argument('file2', help='file with old annotations', action='store')
    args = parser.parse_args()

    quoteIds = []
    if args.quoteIdsFile != None:
        quoteIds = util.readlines(args.quoteIdsFile)
    compare(args.file1, args.file2, quoteIds)
def translate(source_path: str, output_path: str) -> None:
    input_lines = [
        s for s in readlines(source_path) if parse_local_include(s) is None
    ]
    parser = SnippetParser(source_path)
    parser.parse()

    vocab = set(
        itertools.chain.from_iterable(
            WORD_RE.findall(line) for line in input_lines))
    for token in vocab:
        parser.enable_snippet(token)

    with open(output_path, 'w') as output_file:
        output_file.write(parser.get_enabled_snippets())
        output_file.write('\n'.join(input_lines))
Пример #6
0
def main():
    # Argument processing
    parser = argparse.ArgumentParser(description='Convert CQSC XML')
    parser.add_argument('-s',
                        '--split',
                        dest='splitChapters',
                        help='split by chapter',
                        action='store_true')
    parser.add_argument('-p',
                        dest='includeSectionTags',
                        help='paragraphs and headings',
                        action='store_true')
    parser.add_argument('-n',
                        dest='extractNestedQuotes',
                        help='extract nested quotes',
                        action='store_true')
    parser.add_argument('indir', help='directory to use', action='store')
    args = parser.parse_args()

    files = util.readlines(args.indir + '/files.txt')
    for file in files:
        filename = args.indir + "/" + file
        (base, ext) = os.path.splitext(filename)
        match = re.match(r"(.*)_(\d+)", base)
        if match:
            base = match.group(1)
        charactersFile = base + ".characters.json"
        if not os.path.isfile(charactersFile):
            charactersFile = None
        print "convert " + filename + " with characters " + str(charactersFile)
        if args.splitChapters:
            base2 = os.path.basename(base)
            outbase = args.indir + "/converted_split/" + base2
            outfile = outbase + "/" + file
        else:
            outbase = args.indir + "/converted/"
            outfile = outbase + file
        if not os.path.exists(outbase):
            os.makedirs(outbase)
        try:
            cqsc.convertMentionLevels(filename, outfile, charactersFile,
                                      args.splitChapters,
                                      args.includeSectionTags,
                                      args.extractNestedQuotes)
        except:
            log.error("Unexpected error processing " + filename + ": ",
                      exc_info=True)
    def parse_snippets(self, path: str) -> Type[HeaderFile]:
        result = HeaderFile(path)
        snippet = None
        group = []
        for line_num, line in enumerate(readlines(path), 1):
            if line.startswith(BEGIN_TAG):
                dprint(f'Process {line}')
                key = line[len(BEGIN_TAG):].strip().strip('`')
                if snippet is not None:
                    raise Exception(f'Unexpected open section '
                                    f'({key}) {path}:{line_num} {line}')
                snippet = Snippet(key,
                                  enabled=key.startswith(ALWAYS_INCLUDE_TAG))
            elif line.startswith(END_TAG):
                dprint(f'Process {line}')
                key = line[len(END_TAG):].strip().strip('`')
                if snippet is None or snippet.tag != key:
                    raise Exception(f'Unexpected close section '
                                    f'({key}) {path}:{line_num} {line}')
                if key in self.snippets:
                    raise Exception(f'Duplicated key found '
                                    f'({key}) {path}:{line_num}')
                snippet.body = '\n'.join(group)
                self.snippets[snippet.tag] = snippet
                result.add_snippet(snippet)
                snippet = None
                group = []
            elif line.startswith(REQUIRED_TAG) and snippet is not None:
                for dependency in filter(
                        None,
                        map(str.strip, line[len(REQUIRED_TAG):].split(','))):
                    dprint(f'{snippet.tag} depends on `{dependency}`')
                    if dependency not in self.snippets:
                        raise Exception(f'Unknown dependency {dependency} '
                                        f'for snippet {snippet.tag}'
                                        f'({key}) {path}:{line_num}')
                    snippet.add_dependency(self.snippets[dependency])
            elif snippet is not None:
                group.append(line)

        if snippet is not None:
            raise Exception(f'Missed last closing tag {path} ({snippet.tag})')

        return result
Пример #8
0
            break
        idx, rule = line.split(": ")
        rules[idx] = rule
    return rules, lines


def build_regex(idx, rules, depth=0):
    if depth > 14:  # limit found by stepping up until output stabilizes
        return ""
    rule = rules[idx]
    if rule.startswith('"'):
        return rule.strip('"')
    if "|" in rule:
        rule1, rule2 = rule.split(" | ")
        regex1 = "".join(
            build_regex(r, rules, depth + 1) for r in rule1.split(" "))
        regex2 = "".join(
            build_regex(r, rules, depth + 1) for r in rule2.split(" "))
        return f"({regex1}|{regex2})"
    return "".join(build_regex(r, rules, depth + 1) for r in rule.split(" "))


def count(rules, lines):
    regex = re.compile(build_regex("0", rules))
    return sum(1 for line in lines if regex.fullmatch(line))


if __name__ == "__main__":
    rules, lines = parse_rules(util.readlines())
    print(count(rules, lines))
Пример #9
0
def parse_groups(file_path):
    lines = util.readlines(file_path)
    lines_group = util.chunk(lambda x: x == "", lines)
    return map(lambda x: list(map(list, x)), lines_group)
Пример #10
0
    rule = get_rule(rules, color)
    if rule is None:
        return 0
    count = 1  # Current bag
    containing_bags = rule[1]
    for cont_bag_color, cont_bag_count in containing_bags:
        count += cont_bag_count * packing_count(rules, cont_bag_color)
    return count


def packing_count_inside(rules, color):
    # Ignore the bag that wraps the other bags
    return packing_count(rules, color) - 1


example_input = list(util.readlines('data/07_example.txt', parse_rule))
example_1 = packing_options(example_input, 'shiny gold')
print(f'Puzzle 7: example 1 - {len(example_1)}')

puzzle_input = list(util.readlines('data/07.txt', parse_rule))
part_1 = packing_options(puzzle_input, 'shiny gold')
print(f'Puzzle 7: part 1 - {len(part_1)}')

example_2a = packing_count_inside(example_input, 'shiny gold')
print(f'Puzzle 7: example 2a - {example_2a}')

example_input_2 = list(util.readlines('data/07_example_2.txt', parse_rule))
example_2b = packing_count_inside(example_input_2, 'shiny gold')
print(f'Puzzle 7: example 2b - {example_2b}')

part_2 = packing_count_inside(puzzle_input, 'shiny gold')
Пример #11
0
import day07a
import util


def count_children(bags, parent):
    return sum(count + count * count_children(bags, child)
               for child, count in ((child, parents[parent])
                                    for child, parents in bags.items()
                                    if parent in parents))


if __name__ == "__main__":
    print(count_children(day07a.parse_bags(util.readlines()), "shiny gold"))
Пример #12
0
    def __init__(self, *args, **kwargs):
        QtGui.QDialog.__init__(self, *args, **kwargs)
        
        dialog = self
                
        self.title = u"[auto] Report from " + BUGREPORT_USER
        self.hash = hashlib.md5(self.title)
        
        dialog.setWindowTitle(self.title)
        dialog.setLayout(QtGui.QVBoxLayout())
        label = QtGui.QLabel()
        label.setText("<b>Send us logs to help us find bugs!</b><br/><br/>Thanks for opting to report a bug.<br/><br/>")
        label.setWordWrap(True)
        dialog.layout().addWidget(label)

#        label = QtGui.QLabel()
#        label.setText("<b>This is what happened</b> (explain what happened, and how)")
#        label.setWordWrap(False)
#        dialog.layout().addWidget(label)

#        self.report = QtGui.QTextEdit()
#        report = self.report
#        report.append("type here")
#        dialog.layout().addWidget(report)
        
        label = QtGui.QLabel()
        label.setText("<b>These are the partial logs</b>")
        label.setWordWrap(False)
        dialog.layout().addWidget(label)
        self.box = QtGui.QTextEdit()
        box = self.box
        box.setReadOnly(True)
        try:
            box.setFont(QtGui.QFont("Lucida Console", 8))
            box.append(u"\n**FAF Username:** " + BUGREPORT_USER)
            box.append(u"\n**FAF Version:** " + VERSION_STRING)
            box.append(u"\n**FAF Directory:** " + APPDATA_DIR)
            box.append(u"\n**FA Path:** " + str(fa.gamepath))
            box.append(u"\n**Home Directory:** " + PERSONAL_DIR)
            box.append(u"")
            box.append(u"\n**FA Forever Log (last 128 lines):**")
            box.append(u"{{{")
            try:
                box.append("\n".join(readlines(LOG_FILE_FAF, False)[-128:]))
            except:
                box.append(unicode(LOG_FILE_FAF))
                box.append(u"empty or not readable")
                
            box.append(u"}}}")
            box.append(u"")
            box.append(u"\n**Forged Alliance Log (full):**")
            box.append(u"{{{")
            try:
                box.append(readfile(LOG_FILE_GAME, False))
            except:
                box.append(unicode(LOG_FILE_GAME))
                box.append(u"empty or not readable")
            box.append(u"}}}")
            box.append(u"")
        except:
            box.append(u"\n**(Exception raised while writing debug vars)**")
            pass

        dialog.layout().addWidget(box)
        self.sendButton = QtGui.QPushButton("\nReport this error\n")
        self.sendButton.pressed.connect(self.postReport) 
        dialog.layout().addWidget(self.sendButton)
        
        label = QtGui.QLabel()
        label.setText("<b>Do you want to continue, or get help in the forums?</b><br/><i>(thanks for helping us make FAF better)</i>")
        label.setWordWrap(False)
        dialog.layout().addWidget(label)
        
        self.buttons = QtGui.QDialogButtonBox()
        buttons = self.buttons        
        buttons.addButton("Close", QtGui.QDialogButtonBox.AcceptRole)
        buttons.addButton("Help", QtGui.QDialogButtonBox.HelpRole)
        buttons.accepted.connect(dialog.accept)
        buttons.rejected.connect(dialog.reject)
        buttons.helpRequested.connect(dialog.techSupport)
        dialog.layout().addWidget(buttons)        
    
        self.setModal(False)
Пример #13
0
    def __init__(self, exc_info, *args, **kwargs):
        QtGui.QDialog.__init__(self, *args, **kwargs)

        excType, excValue, tracebackobj = exc_info

        dialog = self

        dialog.setLayout(QtGui.QVBoxLayout())
        label = QtGui.QLabel()
        label.setText(
            "An Error has occurred in FAF.<br><br>You can report it by clicking the ticket button. <b>Please check if that error is new first !</b>"
        )
        label.setWordWrap(True)
        dialog.layout().addWidget(label)

        label = QtGui.QLabel()
        label.setText(
            "<b>This is what happened (but please add your own explanation !)</b>"
        )
        label.setWordWrap(False)
        dialog.layout().addWidget(label)

        self.trace = u"".join(
            traceback.format_exception(excType, excValue, tracebackobj, 10))
        self.hash = hashlib.md5(self.trace).hexdigest()

        self.title = u"[auto] Crash from " + CRASHREPORT_USER + u": " + str(
            excValue)

        dialog.setWindowTitle(self.title)

        self.box = QtGui.QTextEdit()
        box = self.box
        try:
            box.setFont(QtGui.QFont("Lucida Console", 8))
            box.append(u"\n**FAF Username:** " + CRASHREPORT_USER)
            box.append(u"\n**FAF Version:** " + VERSION_STRING)
            box.append(u"\n**FAF Directory:** " + APPDATA_DIR)
            box.append(u"\n**FA Path:** " + str(fa.gamepath))
            box.append(u"\n**Home Directory:** " + PERSONAL_DIR)
        except:
            box.append(u"\n**(Exception raised while writing debug vars)**")
            pass

            box.append(u"")
            box.append(u"\n**FA Forever Log (last 128 lines):**")
            box.append(u"{{{")
            try:
                box.append("\n".join(readlines(LOG_FILE_FAF, False)[-128:]))
            except:
                box.append(unicode(LOG_FILE_FAF))
                box.append(u"empty or not readable")

        box.append(u"\n**Stacktrace:**")
        box.append(u"{{{")
        box.append(self.trace)
        box.append(u"}}}")
        box.append(u"")

        dialog.layout().addWidget(box)
        self.sendButton = QtGui.QPushButton("\nOpen ticket system.\n")
        self.sendButton.pressed.connect(self.postReport)
        dialog.layout().addWidget(self.sendButton)

        label = QtGui.QLabel()
        label.setText(
            "<b></b><br/><i>(please note that the error may be fatal and continue won't work in that case)</i>"
        )
        label.setWordWrap(False)
        dialog.layout().addWidget(label)

        self.buttons = QtGui.QDialogButtonBox()
        buttons = self.buttons
        buttons.addButton("Continue", QtGui.QDialogButtonBox.AcceptRole)
        buttons.addButton("Close FAF", QtGui.QDialogButtonBox.RejectRole)
        buttons.addButton("Help", QtGui.QDialogButtonBox.HelpRole)
        buttons.accepted.connect(dialog.accept)
        buttons.rejected.connect(dialog.reject)
        buttons.helpRequested.connect(dialog.techSupport)
        dialog.layout().addWidget(buttons)
Пример #14
0

def parse_pwd_policy(pwd_policy_str):
    parsed = re.search("(\d+)-(\d+) ([a-z]): ([a-z]+)", pwd_policy_str)
    pwd_char_range = range(int(parsed[1]), int(parsed[2]) + 1)
    pwd_char = parsed[3]
    pwd = parsed[4]
    return (pwd_char_range, pwd_char, pwd)


def is_valid_pwd(pwd_policy):
    pwd_char_range, pwd_char, pwd = pwd_policy
    return pwd.count(pwd_char) in pwd_char_range


example_input = list(util.readlines('data/02_example.txt', parse_pwd_policy))

example_1 = sum(1 for x in example_input if is_valid_pwd(x))
print(f'Puzzle 2: example 1 = {example_1}')

puzzle_input = list(util.readlines('data/02.txt', parse_pwd_policy))

part_1 = sum(1 for x in example_input if is_valid_pwd(x))
print(f'Puzzle 2: part 1 = {part_1}')


def is_valid_pwd_fixed(pwd_policy):
    pwd_char_range, pwd_char, pwd = pwd_policy
    pwd_char_pos_1 = pwd_char_range.start - 1  # 0-based index
    pwd_char_pos_2 = pwd_char_range.stop - 2  # 0-based index + stop exclusive
    match_1 = pwd_char_pos_1 < len(pwd) and pwd[pwd_char_pos_1] is pwd_char
Пример #15
0
    def reset(self):
        self.pos = 0
        self.accumulator = 0

    def run(self):
        visited = []
        while self.pos < len(self.program):
            if self.pos in visited:
                break
            visited.append(self.pos)
            op, arg = self.program[self.pos]
            getattr(self, f"eval_{op}")(arg)
        return visited

    def eval_nop(self, arg):
        self.pos += 1

    def eval_jmp(self, arg):
        self.pos += arg

    def eval_acc(self, arg):
        self.accumulator += arg
        self.pos += 1


if __name__ == "__main__":
    hhgc = GameConsole(util.readlines())
    hhgc.run()
    print(hhgc.accumulator)
Пример #16
0
import collections
import util


def parse_bags(lines):
    bags = collections.defaultdict(dict)
    for line in lines:
        if line.endswith("no other bags."):
            continue
        parent, children = line.split(" bags contain ")
        for childpart in children.split(", "):
            count, *child, _ = childpart.split()
            bags[" ".join(child)][parent] = int(count)
    return bags


def find_parents(bags, child):
    parents = set(bags[child])
    for parent in bags[child]:
        parents.update(find_parents(bags, parent))
    return parents


if __name__ == "__main__":
    print(len(find_parents(parse_bags(util.readlines()), "shiny gold")))
Пример #17
0
        trees = 0
        while self.position_on_map():
            is_tree = self.move(down,
                                right)  # Switch order to match our system.
            if is_tree:
                trees += 1
        return trees

    def traverse_multiple(self, moves):
        trees_prod = 1
        for (right, down) in moves:
            trees_prod *= self.traverse(right, down)
        return trees_prod


example_input = list(util.readlines('data/03_example.txt'))

example_1 = TreeMap(example_input).traverse(right=3, down=1)
print(f'Puzzle 3: example 1 = {example_1}')

puzzle_input = list(util.readlines('data/03.txt'))
puzzle_1 = TreeMap(puzzle_input).traverse(right=3, down=1)
print(f'Puzzle 3: part 1 = {puzzle_1}')

example_2 = TreeMap(example_input).traverse_multiple([(1, 1), (3, 1), (5, 1),
                                                      (7, 1), (1, 2)])
print(f'Puzzle 3: example 2 = {example_2}')

puzzle_2 = TreeMap(puzzle_input).traverse_multiple([(1, 1), (3, 1), (5, 1),
                                                    (7, 1), (1, 2)])
print(f'Puzzle 3: part 2 = {puzzle_2}')
#!/usr/env python

from util import readlines


def write_bow(v, path):
  f = open(path, "w")
  for i in v:
    f.write(i[0] + "\n")
  f.close()


lines = readlines("data/vocabulary.csv")[1:]

splitted = list(map(lambda l : l.split(";"), lines))

ge_std = list(map(lambda s : (s[0], float(s[2])), splitted))
co_std = list(map(lambda s : (s[0], float(s[3])), splitted))

ge_std.sort(key = lambda x : x[1], reverse = True)
co_std.sort(key = lambda x : x[1], reverse = True)


write_bow(ge_std[0:1000], "data/bow_gender")
write_bow(co_std[0:1000], "data/bow_country")

Пример #19
0
import day03a
import util


def treecount_prod(grid, moves):
    return util.prod(day03a.treecount(grid, move_x, move_y) for move_x, move_y in moves)


if __name__ == "__main__":
    print(
        treecount_prod(
            list(util.readlines()),
            [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)],
        )
    )
Пример #20
0
from textmind.wenxin import TextMind


re_exp = re.compile(r'\[(\S+?)\]')

set_We = {'我们', '大家', '咱们', '咱们', '俺们'}    # 第一人称复数 http://baike.baidu.com/view/1569657.htm
set_I  = {'我', '俺'}                             # 第一人称单数

textLen = []

# Get absolute path of base dir
try: ind = __file__.rindex('/' if '/' in __file__ else '\\')
except: ind = len(__file__)
cur_dir = __file__[:ind+1]

neg_exp = util.readlines(cur_dir + r"./weibo/Exps-Neg.lst")
pos_exp = util.readlines(cur_dir + r"./weibo/Exps-Pos.lst")


def iter_text(_statuses,_filter):
    for s in _statuses:
        if _filter is not None:
            if 'created_at' not in s:
                continue
            else:
                created_at = s.get('created_at')
                created = parser.parse(created_at, fuzzy=True) if isinstance(created_at,basestring) else utc.localize(created_at)
                if created > _filter:
                    continue

        text = s.get('text').encode('UTF-8')
Пример #21
0
            statuses = dSource.get_statuses(uid)
            v1 = extract_statuses_behave(statuses)
            v2 = extract_statuses_text(statuses)
            v += u+v1+v2
        return v
    except RuntimeError:
        return None


if __name__ == '__main__':
    from multiprocessing import Pool, freeze_support
    pool = Pool()
    freeze_support()

    output_file = folder + '/BasicFeatures.csv'
    uid_file = folder + "/UserList.txt"
    uids = util.readlines(uid_file)#[91:110]

    # results = [extract(uid) for uid in uids]
    results = pool.map(extract, uids)
    pool.close()

    with codecs.open(output_file, 'w+', encoding='utf-8') as fp:
        fp.write(u'\uFEFF')
        for result in results:
            if result is None:
                continue
            line = ','.join(result)
            fp.write(line + '\n')
Пример #22
0
def float_mask(mask):
    if "X" in mask:
        prefix, mask = mask.split("X", 1)
        for suffix in float_mask(mask):
            yield prefix + "0" + suffix
            yield prefix + "1" + suffix
    else:
        yield mask


def parse_mask(line):
    return day14a.parse_mask(line).replace("0", "Z")


def day14b(lines):
    mem = {}
    masks = None
    for line in lines:
        if line.startswith("mask"):
            masks = list(float_mask(parse_mask(line)))
        elif line.startswith("mem"):
            addr, value = day14a.parse_mem(line)
            for mask in masks:
                mem[day14a.apply_mask(mask, addr)] = value
    return sum(mem.values())


if __name__ == "__main__":
    print(day14b(util.readlines()))
Пример #23
0
import day13a
import util


def busjam(lines):
    _, buses = day13a.parse(lines)
    time = 0
    step = 1
    for delay, bus in enumerate(buses):
        if bus == "x":
            continue
        while (time + delay) % bus:
            time += step
        step *= bus
    return time


if __name__ == "__main__":
    print(busjam(util.readlines()))
Пример #24
0
import codecs
import util
from extractor.source_mysql import DataSourceMySQL as DataSource
from extractor.extractor import *

dSource = DataSource()

def extract(uid):
    profile  = dSource.get_profile(uid)
    if profile is None:
        return []
    statuses = dSource.get_statuses(uid)

    u = extract_profile(profile)
    v1 = extract_statuses_behave(statuses)
    v2 = extract_statuses_text(statuses)
    return u+v1+v2


if __name__ == '__main__':
    output_file = 'G:/EXP-Data/BasicFeatures.csv'
    uid_file = r"E:\Study\Publishing\TimeSequencePaper\data\UserList_hasSeq.txt"
    uid_list = util.readlines(uid_file)

    with codecs.open(output_file, 'w', encoding='utf-8') as fp:
        fp.write(u'\uFEFF')
        for uid in uid_list:
            v = extract(uid)
            line = ','.join([uid]+v)
            print(uid)
            fp.write(line + '\n')
Пример #25
0
import day19a
import util


if __name__ == "__main__":
    rules, lines = day19a.parse_rules(util.readlines())
    rules["8"] = "42 | 42 8"
    rules["11"] = "42 31 | 42 11 31"
    print(day19a.count(rules, lines))
Пример #26
0
                                    for rule in rulepart.split(" or "))]
    return rules


def parse(lines):
    rules = parse_rules(lines)
    next(lines)  # "your ticket:"
    my_ticket = [int(n) for n in next(lines).split(",")]
    next(lines)  # ""
    next(lines)  # "nearby tickets:"
    nearby_tickets = ([int(n) for n in line.split(",")] for line in lines)
    return rules, my_ticket, nearby_tickets


def validate(value, rules):
    return {
        name
        for name, ranges in rules.items()
        if any(a <= value <= b for a, b in ranges)
    }


def day16a(lines):
    rules, my_ticket, nearby_tickets = parse(lines)
    return sum(value for ticket in nearby_tickets for value in ticket
               if not validate(value, rules))


if __name__ == "__main__":
    print(day16a(util.readlines()))
Пример #27
0
import day08a
import util


class BacktrackingGameConsole(day08a.GameConsole):
    def run(self):
        visited = super().run()
        while self.pos < len(self.program):
            last_pos = visited.pop()
            last_op = self.program[last_pos][0]
            if last_op not in ("nop", "jmp"):
                continue
            try_op = "nop" if last_op == "jmp" else "jmp"
            self.program[last_pos][0] = try_op
            self.reset()
            super().run()
            self.program[last_pos][0] = last_op


if __name__ == "__main__":
    hhgc = BacktrackingGameConsole(util.readlines())
    hhgc.run()
    print(hhgc.accumulator)
Пример #28
0
    return False


def find_sum_component_mismatch(numbers, preamble_length):
    # Performance + reuse hack:
    # The window contains the preamble and the to check number.
    # To be 100% correct, we should split those apart,
    # but it makes no difference in our implementation so we can
    # save us the effort.
    for window in sliding_window(numbers, preamble_length + 1):
        if has_sum_components(window, window[-1]) is False:
            return window[-1]
    raise Exception('Not found')


example_input = list(util.readlines('data/09_example.txt', int))
example_1 = find_sum_component_mismatch(example_input, 5)
print(f'Puzzel 09: example 1 - {example_1}')

puzzle_input = list(util.readlines('data/09.txt', int))
part_1 = find_sum_component_mismatch(puzzle_input, 25)
print(f'Puzzel 09: part 1 - {part_1}')


def find_encryption_weakness(numbers, invalid_number):
    for window_size in range(2, len(numbers)):
        for window in sliding_window(numbers, window_size):
            if sum(window) == invalid_number:
                return min(window) + max(window)
    return Exception('Not found')
Пример #29
0
def rotate(orientation, direction, degrees):
    orientations = ['east', 'south', 'west', 'north']
    rotations = int(degrees / 90)
    if direction == 'L':
        rotations *= -1
    cur_direction_index = orientations.index(orientation)
    new_direction_index = (cur_direction_index + rotations) % 4
    return orientations[new_direction_index]


def manhattan_dist(position):
    east, north, orientation = position
    return abs(east) + abs(north)


example_input = list(util.readlines('data/12_example.txt', parse_instruction))
print(f'Puzzle 12: ex. 1 - {manhattan_dist(move(example_input))}')

puzzle_input = list(util.readlines('data/12.txt', parse_instruction))
print(f'Puzzle 12: part 1 - {manhattan_dist(move(puzzle_input))}')


def move_waypoint(instructions):
    east, north, orientation = 0, 0, 'east'  # Position absolute to the grid.
    east_w, north_w = 10, 1  # Position relative to the position of the ship.
    for (direction, distance) in instructions:
        if direction == 'F':
            east += east_w * distance
            north += north_w * distance
        elif direction == 'N':
            north_w += distance
Пример #30
0
        u_data[uid] = times

    with codecs.open('%ssummary.csv' % base_dir, 'w', encoding='utf-8') as fp:
        fp.write(',')
        for t in time_list:
            fp.write('%s,' % t)
        fp.write('\n')

        for uid,times in u_data.iteritems():
            fp.write('%s,' % uid)
            for t in time_list:
                cell = 1 if t in times else 0
                fp.write('%s,' % cell)
            fp.write('\n')


if __name__ == '__main__':
    # from multiprocessing import Pool, freeze_support
    # pool = Pool()
    # freeze_support()

    uid_list = util.readlines(base_dir + "UserList.txt")

    # [process_user(uid) for uid in uid_list[400:500]]
    # pool.map(process_user, uid_list)
    # pool.close()

    time_list = ['2011%02d' % i for i in range(1, 53)] + ['2012%02d' % i for i in range(1, 42)]
    process_features(uid_list, time_list)
Пример #31
0
                iter = 0
                for head,fw in fw_pool.iteritems():
                    fw.write( '%s,' % features[iter] )
                    iter += 1

            for fw in fw_pool.values():
                fw.write('\n')

        u_data[uid] = times

    with codecs.open('%ssummary.csv' % (base_dir), 'w', encoding='utf-8') as fp:
        fp.write(',')
        for t in time_list:
            fp.write('%s,' % t)
        fp.write('\n')

        for uid,times in u_data.iteritems():
            fp.write('%s,' % uid)
            for t in time_list:
                cell = 1 if t in times else 0
                fp.write('%s,' % cell)
            fp.write('\n')


if __name__ == '__main__':
    uid_list = util.readlines(r"E:\Study\Publishing\TimeSequencePaper\data\UserList.txt")
    time_list = ['2011%02d' % i for i in range(15,53)] + ['2012%02d' % i for i in range(1,42)]

    #process_users(uid_list)
    process_features(uid_list,time_list)
Пример #32
0
            statuses = dSource.get_statuses(uid)
            v1 = extract_statuses_behave(statuses)
            v2 = extract_statuses_text(statuses)
            v += u + v1 + v2
        return v
    except RuntimeError:
        return None


if __name__ == '__main__':
    from multiprocessing import Pool, freeze_support
    pool = Pool()
    freeze_support()

    output_file = folder + '/BasicFeatures.csv'
    uid_file = folder + "/UserList.txt"
    uids = util.readlines(uid_file)  #[91:110]

    # results = [extract(uid) for uid in uids]
    results = pool.map(extract, uids)
    pool.close()

    with codecs.open(output_file, 'w+', encoding='utf-8') as fp:
        fp.write(u'\uFEFF')
        for result in results:
            if result is None:
                continue
            line = ','.join(result)
            fp.write(line + '\n')
Пример #33
0
def parse_passports(file_path):
    lines = util.readlines(file_path)
    lines_passport = util.chunk(lambda x: x == "", lines)
    return map(parse_passport, lines_passport)
Пример #34
0
    return sum(grid.get((x + dx, y + dy)) == "#" for dx, dy in directions)


def change_seats(grid, copy, count_adjacent, occupied_threshold):
    changes = 0
    for crd, state in grid.items():
        adjacent = count_adjacent(grid, *crd)
        if state == "L" and adjacent == 0:
            copy[crd] = "#"
            changes += 1
        elif state == "#" and adjacent >= occupied_threshold:
            copy[crd] = "L"
            changes += 1
        else:
            copy[crd] = state
    return changes


def settle_seats(lines, count_adjacent=count_adjacent, occupied_threshold=4):
    grid = {(x, y): state
            for y, line in enumerate(lines) for x, state in enumerate(line)
            if state == "L"}
    copy = {}
    while change_seats(grid, copy, count_adjacent, occupied_threshold):
        grid, copy = copy, grid
    return sum(state == "#" for state in grid.values())


if __name__ == "__main__":
    print(settle_seats(util.readlines()))
Пример #35
0
        rows = split_range(rows, instruction)
    cols = (0, 7)
    col_instructions = instructions[7:]
    for instruction in col_instructions:
        cols = split_range(cols, instruction)
    seat_row = rows[0]
    seat_col = cols[0]
    seat_id = seat_row * 8 + seat_col
    return {'row': seat_row, 'col': seat_col, 'seat ID': seat_id}


print(decode_seat('BFFFBBFRRR'))
print(decode_seat('FFFBBBFRRR'))
print(decode_seat('BBFFBBFRLL'))

puzzle_input = list(util.readlines('data/05.txt'))

part_1 = max(map(lambda x: decode_seat(x)['seat ID'], puzzle_input))
print(f'Puzzle 5: part 1 = {part_1}')


def find_my_seat(instructions):
    seats = map(decode_seat, instructions)
    taken = set(map(lambda x: (x['row'], x['col']), seats))
    # Mine is the first free seat with both neighbours taken.
    for row in range(0, 128):
        for col in range(0, 8):
            seat = (row, col)
            seat_p = (row, col - 1)
            seat_n = (row, col + 1)
            if seat not in taken and seat_p in taken and seat_n in taken:
Пример #36
0
    def __init__(self, exc_info, *args, **kwargs):
        QtGui.QDialog.__init__(self, *args, **kwargs)
        
        excType, excValue, tracebackobj = exc_info
        
        dialog = self
        
        
        dialog.setLayout(QtGui.QVBoxLayout())
        label = QtGui.QLabel()
        label.setText("An Error has occurred in FAF.<br><br>You can report it by clicking the ticket button. <b>Please check if that error is new first !</b>")
        label.setWordWrap(True)
        dialog.layout().addWidget(label)

        label = QtGui.QLabel()
        label.setText("<b>This is what happened (but please add your own explanation !)</b>")
        label.setWordWrap(False)
        dialog.layout().addWidget(label)

        self.trace = u"".join(traceback.format_exception(excType, excValue, tracebackobj, 10))
        self.hash = hashlib.md5(self.trace).hexdigest()

        self.title = u"[auto] Crash from " + CRASHREPORT_USER + u": " + str(excValue)
        
        dialog.setWindowTitle(self.title)
        
        self.box = QtGui.QTextEdit()
        box = self.box
        try:
            box.setFont(QtGui.QFont("Lucida Console", 8))
            box.append(u"\n**FAF Username:** " + CRASHREPORT_USER)
            box.append(u"\n**FAF Version:** " + VERSION_STRING)
            box.append(u"\n**FAF Directory:** " + APPDATA_DIR)
            box.append(u"\n**FA Path:** " + str(fa.gamepath))
            box.append(u"\n**Home Directory:** " + PERSONAL_DIR)            
        except:
            box.append(u"\n**(Exception raised while writing debug vars)**")
            pass
        
            box.append(u"")
            box.append(u"\n**FA Forever Log (last 128 lines):**")
            box.append(u"{{{")
            try:
                box.append("\n".join(readlines(LOG_FILE_FAF, False)[-128:]))
            except:
                box.append(unicode(LOG_FILE_FAF))
                box.append(u"empty or not readable")
        
        box.append(u"\n**Stacktrace:**")
        box.append(u"{{{")
        box.append(self.trace)
        box.append(u"}}}")
        box.append(u"")

        dialog.layout().addWidget(box)
        self.sendButton = QtGui.QPushButton("\nOpen ticket system.\n")
        self.sendButton.pressed.connect(self.postReport) 
        dialog.layout().addWidget(self.sendButton)
        
        label = QtGui.QLabel()
        label.setText("<b></b><br/><i>(please note that the error may be fatal and continue won't work in that case)</i>")
        label.setWordWrap(False)
        dialog.layout().addWidget(label)
        
        self.buttons = QtGui.QDialogButtonBox()
        buttons = self.buttons        
        buttons.addButton("Continue", QtGui.QDialogButtonBox.AcceptRole)
        buttons.addButton("Close FAF", QtGui.QDialogButtonBox.RejectRole)
        buttons.addButton("Help", QtGui.QDialogButtonBox.HelpRole)
        buttons.accepted.connect(dialog.accept)
        buttons.rejected.connect(dialog.reject)
        buttons.helpRequested.connect(dialog.techSupport)
        dialog.layout().addWidget(buttons)        
Пример #37
0
    def __init__(self, *args, **kwargs):
        QtGui.QDialog.__init__(self, *args, **kwargs)

        dialog = self

        self.title = u"[auto] Report from " + BUGREPORT_USER
        self.hash = hashlib.md5(self.title)

        dialog.setWindowTitle(self.title)
        dialog.setLayout(QtGui.QVBoxLayout())
        label = QtGui.QLabel()
        label.setText(
            "<b>Send us logs to help us find bugs!</b><br/><br/>Thanks for opting to report a bug.<br/><br/>"
        )
        label.setWordWrap(True)
        dialog.layout().addWidget(label)

        #        label = QtGui.QLabel()
        #        label.setText("<b>This is what happened</b> (explain what happened, and how)")
        #        label.setWordWrap(False)
        #        dialog.layout().addWidget(label)

        #        self.report = QtGui.QTextEdit()
        #        report = self.report
        #        report.append("type here")
        #        dialog.layout().addWidget(report)

        label = QtGui.QLabel()
        label.setText("<b>These are the partial logs</b>")
        label.setWordWrap(False)
        dialog.layout().addWidget(label)
        self.box = QtGui.QTextEdit()
        box = self.box
        box.setReadOnly(True)
        try:
            box.setFont(QtGui.QFont("Lucida Console", 8))
            box.append(u"\n**FAF Username:** " + BUGREPORT_USER)
            box.append(u"\n**FAF Version:** " + VERSION_STRING)
            box.append(u"\n**FAF Directory:** " + APPDATA_DIR)
            box.append(u"\n**FA Path:** " + str(fa.gamepath))
            box.append(u"\n**Home Directory:** " + PERSONAL_DIR)
            box.append(u"")
            box.append(u"\n**FA Forever Log (last 128 lines):**")
            box.append(u"{{{")
            try:
                box.append("\n".join(readlines(LOG_FILE_FAF, False)[-128:]))
            except:
                box.append(unicode(LOG_FILE_FAF))
                box.append(u"empty or not readable")

            box.append(u"}}}")
            box.append(u"")
            box.append(u"\n**Forged Alliance Log (full):**")
            box.append(u"{{{")
            try:
                box.append(readfile(LOG_FILE_GAME, False))
            except:
                box.append(unicode(LOG_FILE_GAME))
                box.append(u"empty or not readable")
            box.append(u"}}}")
            box.append(u"")
        except:
            box.append(u"\n**(Exception raised while writing debug vars)**")
            pass

        dialog.layout().addWidget(box)
        self.sendButton = QtGui.QPushButton("\nReport this error\n")
        self.sendButton.pressed.connect(self.postReport)
        dialog.layout().addWidget(self.sendButton)

        label = QtGui.QLabel()
        label.setText(
            "<b>Do you want to continue, or get help in the forums?</b><br/><i>(thanks for helping us make FAF better)</i>"
        )
        label.setWordWrap(False)
        dialog.layout().addWidget(label)

        self.buttons = QtGui.QDialogButtonBox()
        buttons = self.buttons
        buttons.addButton("Close", QtGui.QDialogButtonBox.AcceptRole)
        buttons.addButton("Help", QtGui.QDialogButtonBox.HelpRole)
        buttons.accepted.connect(dialog.accept)
        buttons.rejected.connect(dialog.reject)
        buttons.helpRequested.connect(dialog.techSupport)
        dialog.layout().addWidget(buttons)

        self.setModal(False)
Пример #38
0
re_exp = re.compile(r'\[(\S+?)\]')

set_We = {'我们', '大家', '咱们', '咱们',
          '俺们'}  # 第一人称复数 http://baike.baidu.com/view/1569657.htm
set_I = {'我', '俺'}  # 第一人称单数

textLen = []

# Get absolute path of base dir
try:
    ind = __file__.rindex('/' if '/' in __file__ else '\\')
except:
    ind = len(__file__)
cur_dir = __file__[:ind + 1]

neg_exp = util.readlines(cur_dir + r"./weibo/Exps-Neg.lst")
pos_exp = util.readlines(cur_dir + r"./weibo/Exps-Pos.lst")


def iter_text(_statuses, _filter):
    for s in _statuses:
        if _filter is not None:
            if 'created_at' not in s:
                continue
            else:
                created_at = s.get('created_at')
                created = parser.parse(created_at, fuzzy=True) if isinstance(
                    created_at, basestring) else utc.localize(created_at)
                if created > _filter:
                    continue
Пример #39
0

def evaluate(tokens, operators, term_parser, root_parser):
    result = term_parser(tokens, root_parser)
    while tokens and tokens[0] in operators:
        t = tokens.pop(0)
        result = operators[t](result, term_parser(tokens, root_parser))
    return result


def leaf_parser(tokens, root_parser):
    t = tokens.pop(0)
    if t == "(":
        result = root_parser(tokens)
        tokens.pop(0)  # ")"
    else:
        result = int(t)
    return result


def root_parser(tokens):
    return evaluate(tokens, operators, leaf_parser, root_parser)


def tokenize(line):
    return [t for t in line if t != " "]


if __name__ == "__main__":
    print(sum(root_parser(tokenize(line)) for line in util.readlines()))
Пример #40
0
import util


def parse(line):
    rule, char, password = line.split()
    atleast, atmost = rule.split("-")
    return int(atleast), int(atmost), char[0], password


def validate(line):
    atleast, atmost, char, password = parse(line)
    return atleast <= password.count(char) <= atmost


if __name__ == "__main__":
    print(util.count(validate, util.readlines()))
Пример #41
0
    statuses = [s if isinstance(s, dict) else s.__dict__ for s in statuses]
    if len(statuses) == 0:
        return
    else:
        for status in statuses:
            for attr in ['seg', 'seg_c', 'ltp', 't_cl']:
                status.pop(attr, None)
    with open(path+uid+'.json', 'w') as fp:
        json.dump(statuses, fp, encoding='utf-8', indent=1, ensure_ascii=False, default=util.time2str)


def dump_user_data(uid):
    print(uid)
    try:
        dump_profile(uid)
        dump_status(uid)
    except IOError as e:
        print e
        pass


if __name__ == '__main__':
    from multiprocessing import Pool, freeze_support

    uids = util.readlines(folder + "UserList.txt")[5000]
    # [dump_user_data(uid) for uid in uids]

    freeze_support()
    pool = Pool()
    pool.map(dump_user_data, uids)
    pool.close()