added base scripts

This commit is contained in:
2020-12-02 13:34:39 +01:00
parent 5e6f764b8f
commit 715dbab163
11 changed files with 266 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
Metadata-Version: 1.0
Name: aoc
Version: 0.1
Summary: UNKNOWN
Home-page: UNKNOWN
Author: UNKNOWN
Author-email: UNKNOWN
License: UNKNOWN
Description: UNKNOWN
Platform: UNKNOWN

View File

@@ -0,0 +1,8 @@
setup.py
aoc/__init__.py
aoc/input.py
aoc/partselector.py
aoc.egg-info/PKG-INFO
aoc.egg-info/SOURCES.txt
aoc.egg-info/dependency_links.txt
aoc.egg-info/top_level.txt

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1 @@
aoc

2
base/aoc/__init__.py Normal file
View File

@@ -0,0 +1,2 @@
from .partselector import get_logger, part_one, part_two, draw
from .input import get_input, read_single_keypress

68
base/aoc/input.py Normal file
View File

@@ -0,0 +1,68 @@
import sys
import argparse
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--sample', action='store_true')
parser.add_argument('-f', '--file', type=str, nargs='?')
parser.add_argument('input', type=str, nargs='*')
return parser.parse_known_args()
def get_input(transform):
p = parse_args()[0]
if p.input:
return transform(p.input)
inp = []
if p.file:
f = open(p.file)
elif p.sample:
f = open('data/sample.txt')
else:
f = open('data/data.txt')
for line in f.readlines():
inp.append(transform(line.strip()))
f.close()
if len(inp) == 1:
return inp[0]
return inp
def read_single_keypress():
import termios, fcntl, sys, os
fd = sys.stdin.fileno()
# save old state
flags_save = fcntl.fcntl(fd, fcntl.F_GETFL)
attrs_save = termios.tcgetattr(fd)
# make raw - the way to do this comes from the termios(3) man page.
attrs = list(attrs_save) # copy the stored version to update
# iflag
attrs[0] &= ~(termios.IGNBRK | termios.BRKINT | termios.PARMRK
| termios.ISTRIP | termios.INLCR | termios. IGNCR
| termios.ICRNL | termios.IXON )
# oflag
attrs[1] &= ~termios.OPOST
# cflag
attrs[2] &= ~(termios.CSIZE | termios. PARENB)
attrs[2] |= termios.CS8
# lflag
attrs[3] &= ~(termios.ECHONL | termios.ECHO | termios.ICANON
| termios.ISIG | termios.IEXTEN)
termios.tcsetattr(fd, termios.TCSANOW, attrs)
# turn off non-blocking
fcntl.fcntl(fd, fcntl.F_SETFL, flags_save & ~os.O_NONBLOCK)
# read a single keystroke
ret = []
try:
ret.append(sys.stdin.read(1)) # returns a single character
fcntl.fcntl(fd, fcntl.F_SETFL, flags_save | os.O_NONBLOCK)
c = sys.stdin.read(1) # returns a single character
while len(c) > 0:
ret.append(c)
c = sys.stdin.read(1)
except KeyboardInterrupt:
ret.append('\x03')
finally:
# restore old state
termios.tcsetattr(fd, termios.TCSAFLUSH, attrs_save)
fcntl.fcntl(fd, fcntl.F_SETFL, flags_save)
return tuple(ret)

49
base/aoc/partselector.py Normal file
View File

@@ -0,0 +1,49 @@
import argparse
import logging
logging.basicConfig(level=logging.WARNING)
_logger = logging.getLogger(__name__)
args = None
def parse_args():
global args
if args is None:
parser = argparse.ArgumentParser()
parser.add_argument('-A', action='store_true')
parser.add_argument('-B', action='store_true')
parser.add_argument('-G', action='store_true')
parser.add_argument('-q', action='store_true')
parser.add_argument('-v', "--verbose", dest="verbose_count",
action="count", default=0,
help="increases log verbosity for each occurence.")
args = parser.parse_known_args()
return args
def get_logger(*args, **kwargs):
p = parse_args()[0]
log = logging.getLogger(*args, **kwargs)
log.setLevel(max(3 - p.verbose_count, 1) * 10)
return log
def part_one():
p = parse_args()[0]
if p.B:
return False
if not p.q:
print('==== PART I ====')
return True
def part_two():
p = parse_args()[0]
if p.A:
return False
if not p.q:
print('==== PART II ====')
return True
def draw():
p = parse_args()[0]
if p.G:
return True
return False

View File

@@ -0,0 +1,3 @@
from .partselector import get_logger, part_one, part_two, draw
from .input import get_input, read_single_keypress
from .debugger import Debugger

View File

@@ -0,0 +1,68 @@
import sys
import argparse
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--sample', action='store_true')
parser.add_argument('-f', '--file', type=str, nargs='?')
parser.add_argument('input', type=str, nargs='*')
return parser.parse_known_args()
def get_input(transform):
p = parse_args()[0]
if p.input:
return transform(p.input)
inp = []
if p.file:
f = open(p.file)
elif p.sample:
f = open('data/sample.txt')
else:
f = open('data/data.txt')
for line in f.readlines():
inp.append(transform(line.strip()))
f.close()
if len(inp) == 1:
return inp[0]
return inp
def read_single_keypress():
import termios, fcntl, sys, os
fd = sys.stdin.fileno()
# save old state
flags_save = fcntl.fcntl(fd, fcntl.F_GETFL)
attrs_save = termios.tcgetattr(fd)
# make raw - the way to do this comes from the termios(3) man page.
attrs = list(attrs_save) # copy the stored version to update
# iflag
attrs[0] &= ~(termios.IGNBRK | termios.BRKINT | termios.PARMRK
| termios.ISTRIP | termios.INLCR | termios. IGNCR
| termios.ICRNL | termios.IXON )
# oflag
attrs[1] &= ~termios.OPOST
# cflag
attrs[2] &= ~(termios.CSIZE | termios. PARENB)
attrs[2] |= termios.CS8
# lflag
attrs[3] &= ~(termios.ECHONL | termios.ECHO | termios.ICANON
| termios.ISIG | termios.IEXTEN)
termios.tcsetattr(fd, termios.TCSANOW, attrs)
# turn off non-blocking
fcntl.fcntl(fd, fcntl.F_SETFL, flags_save & ~os.O_NONBLOCK)
# read a single keystroke
ret = []
try:
ret.append(sys.stdin.read(1)) # returns a single character
fcntl.fcntl(fd, fcntl.F_SETFL, flags_save | os.O_NONBLOCK)
c = sys.stdin.read(1) # returns a single character
while len(c) > 0:
ret.append(c)
c = sys.stdin.read(1)
except KeyboardInterrupt:
ret.append('\x03')
finally:
# restore old state
termios.tcsetattr(fd, termios.TCSAFLUSH, attrs_save)
fcntl.fcntl(fd, fcntl.F_SETFL, flags_save)
return tuple(ret)

View File

@@ -0,0 +1,49 @@
import argparse
import logging
logging.basicConfig(level=logging.WARNING)
_logger = logging.getLogger(__name__)
args = None
def parse_args():
global args
if args is None:
parser = argparse.ArgumentParser()
parser.add_argument('-A', action='store_true')
parser.add_argument('-B', action='store_true')
parser.add_argument('-G', action='store_true')
parser.add_argument('-q', action='store_true')
parser.add_argument('-v', "--verbose", dest="verbose_count",
action="count", default=0,
help="increases log verbosity for each occurence.")
args = parser.parse_known_args()
return args
def get_logger(*args, **kwargs):
p = parse_args()[0]
log = logging.getLogger(*args, **kwargs)
log.setLevel(max(3 - p.verbose_count, 1) * 10)
return log
def part_one():
p = parse_args()[0]
if p.B:
return False
if not p.q:
print('==== PART I ====')
return True
def part_two():
p = parse_args()[0]
if p.A:
return False
if not p.q:
print('==== PART II ====')
return True
def draw():
p = parse_args()[0]
if p.G:
return True
return False

7
base/setup.py Normal file
View File

@@ -0,0 +1,7 @@
from setuptools import setup
setup(
name='aoc',
version='0.1',
packages=['aoc',],
)