def __init__(self, root, config, subreddit): Loader.__init__(self, 'praw', root, config, subreddit) self.endpoint = { 'user_agent': Env.USER_AGENT(), 'client_id': Env.REDDIT_CLIENT_ID(), 'client_secret': Env.REDDIT_CLIENT_SECRET() } self.reddit = Reddit(**self.endpoint) # config parameters self.types = self.config['praw']['types'] self.periode = self.config['praw']['periode'] self.retrospect_time = self.config['praw']['retrospect_time'] # initial run variables self.last_run = {} for file_type in self.types: self.last_run[file_type] = 0 # saved run variables for file_type in self.types: meta = self.read_meta(file_type) if 'last_run' in meta: self.last_run[file_type] = meta['last_run']
def test_load_matrix_nums(self): matrix = Loader.transform_lines(TestTransformer, 'data/trans_data.txt') result = [ TestTransformer('A', 26, 1), TestTransformer('B', 21, 2), TestTransformer('C', 13, 3), TestTransformer('D', 44, 4) ] assert matrix == result
def __init__(self, root, config, subreddit): Loader.__init__(self, 'crawler', root, config, subreddit) self.endpoint = 'https://old.reddit.com/r/{}/new/' # config parameters self.types = self.config['crawler']['types'] self.periode = self.config['crawler']['periode'] # initial run variables self.last_run = {} for file_type in self.types: self.last_run[file_type] = self.config['crawler']['start_time'] # saved run variables for file_type in self.types: meta = self.read_meta(file_type) if 'last_run' in meta: self.last_run[file_type] = meta['last_run']
def test_load_matrix_nums_cplx(self): matrix = Loader.transform_lines_complex( TestTransformer, filename='data/trans_data_cplx.txt') result = [ TestTransformer('A', 26, 30), TestTransformer('B', 10, -1), TestTransformer('C', 12, 13), TestTransformer('D', 0, -1) ] assert matrix == result
def __init__(self, root, config, subreddit): Loader.__init__(self, 'pushshift', root, config, subreddit) self.endpoint = 'https://api.pushshift.io/reddit/{}/search?limit=100&sort=desc&subreddit={}&after={}&before={}' # config parameters self.types = self.config['pushshift']['types'] self.periode = self.config['pushshift']['periode'] # initial run variables self.last_run = {} self.end_run = {} for file_type in self.types: self.last_run[file_type] = int( datetime.now(timezone.utc).timestamp()) self.end_run[file_type] = self.config['pushshift']['start_time'] # saved run variables for file_type in self.types: meta = self.read_meta(file_type) if 'last_run' in meta: self.last_run[file_type] = meta['last_run'] if 'end_run' in meta: self.end_run[file_type] = meta['end_run']
def test_one_extension(): loader = Loader("tests/data", file_extensions=[".jpg"]) files = loader.load() assert len(files) == 0
def test_multiple_extensions_faulty_path(): with pytest.raises(NotADirectoryError): loader = Loader( "tests/data2", file_extensions=[".jpg", ".png", ".mp4", ".svg", ".txt"])
def test_one_extension_faulty_path(): with pytest.raises(NotADirectoryError): loader = Loader("tests/data2", file_extensions=[".jpg"])
def test_one_extension_without_dot_uppercase(): loader = Loader("tests/data", file_extensions=["JPG"]) files = loader.load() assert len(files) == 0
def test_one_extension_without_dot_whitespace(): loader = Loader("tests/data", file_extensions=[" jpg "]) files = loader.load() assert len(files) == 0
def __init__(self, line): self.num = int(line.split(' ')[1]) self.action = line.split(' ')[0] def apply_1(self, position): if self.action == 'up': return position[0] - self.num, position[1] if self.action == 'down': return position[0] + self.num, position[1] if self.action == 'forward': return position[0], position[1] + self.num def apply_2(self, position): if self.action == 'up': return position[0], position[1], position[2] - self.num if self.action == 'down': return position[0], position[1], position[2] + self.num if self.action == 'forward': return position[0] + self.num * position[2], position[1] + self.num, position[2] actions = Loader.transform_lines(Action) pos1 = (0, 0) pos2 = (0, 0, 0) for action in actions: pos1 = action.apply_1(pos1) pos2 = action.apply_2(pos2) print(pos1[0] * pos1[1]) print(pos2[0] * pos2[1])
def checksum_row(row): return max(row) - min(row) def checksum(matrix): return sum([checksum_row(row) for row in matrix]) def checksum_mod_row(row): for i in row: for j in row: if (i != j) & (i % j == 0): return i // j def checksum_mod(matrix): return sum([checksum_mod_row(row) for row in matrix]) mat = Loader.load_matrix("test_data.txt", delimiter='\\s+', numeric=True) print(checksum(mat)) mat = Loader.load_matrix("input.txt", delimiter='\\s+', numeric=True) print(checksum(mat)) mat = Loader.load_matrix("test_data_2.txt", delimiter='\\s+', numeric=True) print(checksum_mod(mat)) mat = Loader.load_matrix("input.txt", delimiter='\\s+', numeric=True) print(checksum_mod(mat))
from common.loader import Loader def is_valid(_pass_array): return len(_pass_array) == len(set(_pass_array)) def is_valid_v2(_pass_array): return len(_pass_array) == len( set(["".join(sorted(list(x))) for x in _pass_array])) print(is_valid('aa bb cc dd ee'.split(' '))) print(is_valid('aa bb cc dd aa'.split(' '))) print(is_valid('aa bb cc dd aaa'.split(' '))) mat = Loader.load_matrix('input.txt', delimiter='\\s+') print(sum([1 if is_valid(x) else 0 for x in mat])) print("PART TWO") print(is_valid_v2('abcde fghij'.split(' '))) print(is_valid_v2('abcde xyz ecdab'.split(' '))) print(is_valid_v2('a ab abc abd abf abj'.split(' '))) print(is_valid_v2('iiii oiii ooii oooi oooo'.split(' '))) print(is_valid_v2('oiii ioii iioi iiio'.split(' '))) print(sum([1 if is_valid_v2(x) else 0 for x in mat]))
from common.loader import Loader num = Loader.load_lines(numeric=True) inc = 0 windows_size = 3 for i in range(windows_size + 1, len(num) + 1): if sum(num[i - windows_size:i]) > sum(num[i - windows_size - 1:i - 1]): #print(str(num[i - windows_size:i]) + " > " + str(num[i - windows_size - 1:i - 1])) inc += 1 print(str(inc))
return '1' if counts.count('1') >= counts.count('0') else '0' def filter_by_bit(_i, _bins, _bit): # keep one left if len(_bins) == 1: return _bins ret = [] for bin in _bins: if bin[_i] == _bit: ret.append(bin) return ret bins = Loader.load_lines() digits = len(bins[0]) print( int(''.join([get_common(i, bins) for i in range(digits)]), 2) * int(''.join([get_common(i, bins, swap=True) for i in range(digits)]), 2)) ox = [b for b in bins] co2 = [b for b in bins] for i in range(digits): ox = filter_by_bit(i, ox, get_common(i, ox)) co2 = filter_by_bit(i, co2, get_common(i, co2, swap=True)) print(ox) print(co2) print(int(ox[0], 2) * int(co2[0], 2))
def test_multiple_extensions(): loader = Loader("tests/data", file_extensions=[".jpg", ".png", ".mp4", ".svg", ".txt"]) files = loader.load() assert len(files) == 0
import argparse from common.loader import Loader from common.image_processor import ImageProcessor if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-i", "--input", nargs="+", type=str, default=".", required=True, help="an input path to a directory") parser.add_argument("-o", "--output", type=str, default=".", required=True, help="an output path to a directory") parser.add_argument("-e", "--exts", nargs="+", type=str, required=True, help="a list of extensions to work on") args = parser.parse_args() input_path = args.input output_path = args.output file_extensions = args.exts # import all images found at path and below loader = Loader(path=input_path, file_extensions=file_extensions) files = loader.load() sorter = ImageProcessor(files) sorter.out(output_path)
from common.loader import Loader def run_offset_machine(_data, strange=False): position = 0 steps = 0 while position in range(0, len(_data)): offset = _data[position] if strange & (_data[position] >= 3): _data[position] -= 1 else: _data[position] += 1 position += offset steps += 1 return steps print(run_offset_machine([0, 3, 0, 1, -3])) print(run_offset_machine(Loader.load_lines(numeric=True))) print(run_offset_machine([0, 3, 0, 1, -3], True)) print(run_offset_machine(Loader.load_lines(numeric=True), True))