<commit_before>#!/usr/bin/env python

from __future__ import print_function
from sys import exit, stderr, stdout
from os import getcwd
from re import findall, search, match
from subprocess import check_output, CalledProcessError
from argparse import ArgumentParser, RawTextHelpFormatter

try:
    from urllib.request import urlopen, HTTPError
except ImportError:
    from urllib2 import urlopen, HTTPError

from mirrors import RoundTrip, Data, statuses

parser = ArgumentParser(
    description=(
        "Find the fastest Ubuntu apt mirrors.\n"
        "Generate new sources.list file."
    ),
    formatter_class=RawTextHelpFormatter
)
parser.add_argument(
    '-t',
    '--top-number',
    nargs=1,
    type=int,
    help=(
        "specify number of mirrors to return\n"
        "default: 1\n"
    ),
    default=1,
    metavar='NUMBER'
)

status_args = [
    x[0].lower() + x[1:] for x in [
        y.replace(' ', '-') for y in statuses
    ]
]
status_args.reverse()

test_group = parser.add_mutually_exclusive_group(required=False)
test_group.add_argument(
    '-m',
    '--min-status',
    nargs=1,
    choices=status_args,
    help=(
        "return mirrors with minimum status\n"
        "choices:\n"
        "   %(up)s\n"
        "   %(day)s\n"
        "   %(two_day)s\n"
        "   %(week)s\n"
        "   %(unknown)s\n"
        "default: %(up)s\n" % {
            'up':status_args[0],
            'day':status_args[1],
            'two_day':status_args[2],
            'week':status_args[3],
            'unknown':status_args[4]
        }
    ),
    default=status_args[0],
    metavar='STATUS'
)
test_group.add_argument(
    '-p',
    '--ping-only',
    action='store_true',
    help=(
        "rank mirror(s) by latency only, disregard status(es)\n"
        "cannot be used in conjunction with -m/--min-status\n"
    ),
    default=False
)

output_group = parser.add_mutually_exclusive_group(required=False)
output_group.add_argument(
    '-c',
    '--choose',
    action='store_true',
    help=(
        "choose mirror from a list\n"
        "requires -t/--top-num NUMBER where NUMBER > 1\n"
    ),
    default=False
)
output_group.add_argument(
    '-l',
    '--list',
    dest='list_only',
    action='store_true',
    help=(
        "print list of mirrors only, don't generate file\n"
        "cannot be used in conjunction with -c/--choose\n"
    ),
    default=False
)

args = parser.parse_args()

# argparse returns list type for only choice arguments, not default
def indexZero(flag):
    if type(flag) is list:
        return flag[0]
    else:
        return flag

flag_number = indexZero(args.top_number)
flag_status = indexZero(args.min_status).replace('-', ' ')
if flag_status != 'unknown':
    flag_status = flag_status[0].upper() + flag_status[1:]

flag_list = args.list_only
flag_choose = args.choose
flag_ping = args.ping_only

def errorExit(err, status):
    print(err, file=stderr)
    exit(status)

if flag_choose and (not flag_number or flag_number < 2):
    parser.print_usage()
    errorExit(
        (
            "error: -c/--choose option requires -t/--top-number NUMBER "
            "where NUMBER is greater than 1."
        ),
        1
    )

def notUbuntu():
    errorExit("Not an Ubuntu OS", 1)

try:
    release = check_output("lsb_release -ics 2>/dev/null", shell=True)
except CalledProcessError:
    notUbuntu()
else:
    release = [s.strip() for s in release.decode('utf-8').split()]

hardware = check_output("uname -m", shell=True).strip().decode('utf-8')
if release[0] == 'Debian':
    errorExit("Debian is not currently supported", 1)
elif release[0] != 'Ubuntu':
    notUbuntu()

codename = release[1][0].upper() + release[1][1:]
ubuntu_url = "mirrors.ubuntu.com"
mirror_list = "http://%s/mirrors.txt" % ubuntu_url
try:
    archives = urlopen(mirror_list)
except IOError as err:
    errorExit(("Could not connect to '%s'.\n"
               "%s" % (mirror_list, err)), 1)

def progressUpdate(processed, total, status=None):
    if total > 1:
        stdout.write('\r')
        percent = int((float(processed) / total) * 100)
        if not status:
            message = "Testing %d mirror(s)" % total
        else:
            message = "Looking up status(es)"

        stdout.write("%s [%d/%d] %d%%" % (message, processed, total, percent))
        stdout.flush()

print("Got list from %s" % ubuntu_url)
archives = archives.read().decode('utf-8')
urls = findall(r'http://([\w\.\-]+)/', archives)
tested = 0
processed = 0
low_rtts = {}
num_urls = len(urls)
progressUpdate(0, num_urls)
for url in urls:
    ping = RoundTrip(url)
    lowest = ping.minRTT()
    if lowest:
        low_rtts.update({url:lowest})
        tested += 1

    processed += 1
    progressUpdate(processed, num_urls)

print()

if hardware == 'x86_64':
    hardware = 'amd64'
else:
    hardware = 'i386'

ranks = sorted(low_rtts, key=low_rtts.__getitem__)
info = []
if not flag_ping:
    progressUpdate(0, flag_number, status=True)
    for rank in ranks:
        launchpad_data = Data(
            rank,
            codename,
            hardware,
            flag_status
        ).getInfo()
        if launchpad_data:
            info.append(launchpad_data)

        info_size = len(info)
        progressUpdate(info_size, flag_number, status=True)
        if info_size == flag_number:
            break
else:
    for rank in ranks:
        info.append(rank)
        info_size = len(info)
        if info_size == flag_number:
            break

if (flag_number > 1) and not flag_ping:
    print()

if info_size == 0:
    errorExit(
        (
            "Unable to find alternative mirror status(es)\n"
            "Try using -p/--ping-only option or adjust -m/--min-status argument"
        ),
        1
    )

directory = '/etc/apt/'
apt_file = 'sources.list'
found = None
with open('%s' % directory + apt_file, 'r') as f:
    lines = f.readlines()
    def confirmMirror(url):
        deb = ('deb', 'deb-src')
        proto = ('http://', 'ftp://')
        if (url and (url[0] in deb) and
                (proto[0] in url[1] or
                 proto[1] in url[1])):
            return True
        else:
            return

    for line in lines:
        fields = line.split()
        if not found:
            if (confirmMirror(fields) and
                    (release[1] in fields[2]) and
                    (fields[3] == "main")):
                repo = [fields[1]]
                found = True
                continue
        else:
            if (confirmMirror(fields) and
                    (fields[2] == '%s-security' % (release[1]))):
                repo += [fields[1]]
                break
    else:
        errorExit("Error finding current repositories", 1)

repo_name = match(r'http://([\w\.\-]+)/', repo[0]).group(1)
current = None
current_key = None
for i, j in enumerate(info):
    if type(j) is tuple:
        mirror_url = j[0]
    else:
        mirror_url = j

    if mirror_url == repo_name:
        mirror_url += " (current)"
        current_key = i
        if i == 0:
            current = True
        else:
            current = False

    if not flag_ping:
        print((
            "%(rank)d. %(mirror)s\n%(tab)sLatency:  \t%(ms)d ms\n"
            "%(tab)sStatus:   \t%(status)s\n%(tab)sBandwidth:\t%(speed)s" % {
                'tab': '    ',
                'rank': i + 1,
                'mirror': mirror_url,
                'ms': low_rtts[j[0]],
                'status': j[1][0],
                'speed': j[1][1]
            }
        ))
    else:
        print("%d. %s: %d ms" % (i + 1, j, low_rtts[j]))

try:
    input = raw_input
except NameError:
    pass

def ask(query):
    global input
    answer = input(query)
    return answer

def currentMirror(require=True):
    global current
    global repo_name
    if current or not require:
        errorExit(
            (
                "%s is the currently used mirror.\n"
                "There is nothing to be done." % repo_name
            ),
            0
        )

def whichKey(flag, info, key):
    if not flag:
        return info[key][0]
    else:
        return info[key]

if flag_choose:
    query = "Choose a mirror (1 - %d)\n'q' to quit " % info_size
    key = ask(query)
    while True:
        match = search(r'[1-5]', key)
        if match and (len(key) == 1):
            key = int(key)
            break
        elif key == 'q':
            exit()
        else:
            query = "Invalid entry "
            key = ask(query)

    key = key - 1
    if current_key == key:
        currentMirror(require=False)

    mirror = whichKey(flag_ping, info, key)
else:
    mirror = whichKey(flag_ping, info, 0)

if flag_list:
    exit()
elif not flag_choose:
    currentMirror()

# Switch mirror from resolvable url back to full path
for m in archives.splitlines():
    if mirror in m:
        mirror = m
        break

lines = ''.join(lines)
for r in repo:
    lines = lines.replace(r, mirror)

def yesOrNo():
    y = 'yes'
    n = 'no'
    options = "Please enter '%s' or '%s': " % (y,n)
    while True:
        answer = ask(query)
        if answer == y:
            break
        elif answer == n:
            exit(0)
        else:
            query = options

wd = getcwd()
if wd == directory[0:-1]:
    query = (
        "'%(dir)s' is the current directory.\n"
        "Generating a new '%(apt)s' file will "
        "overwrite the current file.\n"
        "You should copy or backup '%(apt)s' before replacing it.\n"
        "Continue?\n[yes|no] " % {
            'dir': directory,
            'apt': apt_file
        }
    )
    yesOrNo()

write_file = wd + "/" + apt_file
try:
    with open(write_file, 'w') as f:
        f.write(lines)
except IOError as err:
    if err.strerror == 'Permission denied':
        errorExit(
            (
                "%s\nYou do not own %s\n"
                "Please run the script from a directory you own." % (
                    err,
                    wd
                )
            ),
            1
        )
    else:
        errorExit(err, 1)
else:
    print("New config file saved to %s" % write_file)

exit(0)
<commit_msg>split archives into urls instead of regex matching<commit_after>#!/usr/bin/env python

from __future__ import print_function
from sys import exit, stderr, stdout
from os import getcwd
from re import findall, search, match
from subprocess import check_output, CalledProcessError
from argparse import ArgumentParser, RawTextHelpFormatter

try:
    from urllib.request import urlopen, HTTPError
except ImportError:
    from urllib2 import urlopen, HTTPError

from mirrors import RoundTrip, Data, statuses

parser = ArgumentParser(
    description=(
        "Find the fastest Ubuntu apt mirrors.\n"
        "Generate new sources.list file."
    ),
    formatter_class=RawTextHelpFormatter
)
parser.add_argument(
    '-t',
    '--top-number',
    nargs=1,
    type=int,
    help=(
        "specify number of mirrors to return\n"
        "default: 1\n"
    ),
    default=1,
    metavar='NUMBER'
)

status_args = [
    x[0].lower() + x[1:] for x in [
        y.replace(' ', '-') for y in statuses
    ]
]
status_args.reverse()

test_group = parser.add_mutually_exclusive_group(required=False)
test_group.add_argument(
    '-m',
    '--min-status',
    nargs=1,
    choices=status_args,
    help=(
        "return mirrors with minimum status\n"
        "choices:\n"
        "   %(up)s\n"
        "   %(day)s\n"
        "   %(two_day)s\n"
        "   %(week)s\n"
        "   %(unknown)s\n"
        "default: %(up)s\n" % {
            'up':status_args[0],
            'day':status_args[1],
            'two_day':status_args[2],
            'week':status_args[3],
            'unknown':status_args[4]
        }
    ),
    default=status_args[0],
    metavar='STATUS'
)
test_group.add_argument(
    '-p',
    '--ping-only',
    action='store_true',
    help=(
        "rank mirror(s) by latency only, disregard status(es)\n"
        "cannot be used in conjunction with -m/--min-status\n"
    ),
    default=False
)

output_group = parser.add_mutually_exclusive_group(required=False)
output_group.add_argument(
    '-c',
    '--choose',
    action='store_true',
    help=(
        "choose mirror from a list\n"
        "requires -t/--top-num NUMBER where NUMBER > 1\n"
    ),
    default=False
)
output_group.add_argument(
    '-l',
    '--list',
    dest='list_only',
    action='store_true',
    help=(
        "print list of mirrors only, don't generate file\n"
        "cannot be used in conjunction with -c/--choose\n"
    ),
    default=False
)

args = parser.parse_args()

# argparse returns list type for only choice arguments, not default
def indexZero(flag):
    if type(flag) is list:
        return flag[0]
    else:
        return flag

flag_number = indexZero(args.top_number)
flag_status = indexZero(args.min_status).replace('-', ' ')
if flag_status != 'unknown':
    flag_status = flag_status[0].upper() + flag_status[1:]

flag_list = args.list_only
flag_choose = args.choose
flag_ping = args.ping_only

def errorExit(err, status):
    print(err, file=stderr)
    exit(status)

if flag_choose and (not flag_number or flag_number < 2):
    parser.print_usage()
    errorExit(
        (
            "error: -c/--choose option requires -t/--top-number NUMBER "
            "where NUMBER is greater than 1."
        ),
        1
    )

def notUbuntu():
    errorExit("Not an Ubuntu OS", 1)

try:
    release = check_output("lsb_release -ics 2>/dev/null", shell=True)
except CalledProcessError:
    notUbuntu()
else:
    release = [s.strip() for s in release.decode('utf-8').split()]

hardware = check_output("uname -m", shell=True).strip().decode('utf-8')
if release[0] == 'Debian':
    errorExit("Debian is not currently supported", 1)
elif release[0] != 'Ubuntu':
    notUbuntu()

codename = release[1][0].upper() + release[1][1:]
ubuntu_url = "mirrors.ubuntu.com"
mirror_list = "http://%s/mirrors.txt" % ubuntu_url
try:
    archives = urlopen(mirror_list)
except IOError as err:
    errorExit(("Could not connect to '%s'.\n"
               "%s" % (mirror_list, err)), 1)

def progressUpdate(processed, total, status=None):
    if total > 1:
        stdout.write('\r')
        percent = int((float(processed) / total) * 100)
        if not status:
            message = "Testing %d mirror(s)" % total
        else:
            message = "Looking up status(es)"

        stdout.write("%s [%d/%d] %d%%" % (message, processed, total, percent))
        stdout.flush()

print("Got list from %s" % ubuntu_url)
archives = archives.read().decode('utf-8')

urls = []
for archive in archives.splitlines():
    archive = archive.split('//', 1)[-1]
    archive = archive.split('/', 1)[0]
    urls.append(archive)

tested = 0
processed = 0
low_rtts = {}
num_urls = len(urls)
progressUpdate(0, num_urls)
for url in urls:
    ping = RoundTrip(url)
    lowest = ping.minRTT()
    if lowest:
        low_rtts.update({url:lowest})
        tested += 1

    processed += 1
    progressUpdate(processed, num_urls)

print()

if hardware == 'x86_64':
    hardware = 'amd64'
else:
    hardware = 'i386'

ranks = sorted(low_rtts, key=low_rtts.__getitem__)
info = []
if not flag_ping:
    progressUpdate(0, flag_number, status=True)
    for rank in ranks:
        launchpad_data = Data(
            rank,
            codename,
            hardware,
            flag_status
        ).getInfo()
        if launchpad_data:
            info.append(launchpad_data)

        info_size = len(info)
        progressUpdate(info_size, flag_number, status=True)
        if info_size == flag_number:
            break
else:
    for rank in ranks:
        info.append(rank)
        info_size = len(info)
        if info_size == flag_number:
            break

if (flag_number > 1) and not flag_ping:
    print()

if info_size == 0:
    errorExit(
        (
            "Unable to find alternative mirror status(es)\n"
            "Try using -p/--ping-only option or adjust -m/--min-status argument"
        ),
        1
    )

directory = '/etc/apt/'
apt_file = 'sources.list'
found = None
with open('%s' % directory + apt_file, 'r') as f:
    lines = f.readlines()
    def confirmMirror(url):
        deb = ('deb', 'deb-src')
        proto = ('http://', 'ftp://')
        if (url and (url[0] in deb) and
                (proto[0] in url[1] or
                 proto[1] in url[1])):
            return True
        else:
            return

    for line in lines:
        fields = line.split()
        if not found:
            if (confirmMirror(fields) and
                    (release[1] in fields[2]) and
                    (fields[3] == "main")):
                repo = [fields[1]]
                found = True
                continue
        else:
            if (confirmMirror(fields) and
                    (fields[2] == '%s-security' % (release[1]))):
                repo += [fields[1]]
                break
    else:
        errorExit("Error finding current repositories", 1)

repo_name = match(r'http://([\w\.\-]+)/', repo[0]).group(1)
current = None
current_key = None
for i, j in enumerate(info):
    if type(j) is tuple:
        mirror_url = j[0]
    else:
        mirror_url = j

    if mirror_url == repo_name:
        mirror_url += " (current)"
        current_key = i
        if i == 0:
            current = True
        else:
            current = False

    if not flag_ping:
        print((
            "%(rank)d. %(mirror)s\n%(tab)sLatency:  \t%(ms)d ms\n"
            "%(tab)sStatus:   \t%(status)s\n%(tab)sBandwidth:\t%(speed)s" % {
                'tab': '    ',
                'rank': i + 1,
                'mirror': mirror_url,
                'ms': low_rtts[j[0]],
                'status': j[1][0],
                'speed': j[1][1]
            }
        ))
    else:
        print("%d. %s: %d ms" % (i + 1, j, low_rtts[j]))

try:
    input = raw_input
except NameError:
    pass

def ask(query):
    global input
    answer = input(query)
    return answer

def currentMirror(require=True):
    global current
    global repo_name
    if current or not require:
        errorExit(
            (
                "%s is the currently used mirror.\n"
                "There is nothing to be done." % repo_name
            ),
            0
        )

def whichKey(flag, info, key):
    if not flag:
        return info[key][0]
    else:
        return info[key]

if flag_choose:
    query = "Choose a mirror (1 - %d)\n'q' to quit " % info_size
    key = ask(query)
    while True:
        match = search(r'[1-5]', key)
        if match and (len(key) == 1):
            key = int(key)
            break
        elif key == 'q':
            exit()
        else:
            query = "Invalid entry "
            key = ask(query)

    key = key - 1
    if current_key == key:
        currentMirror(require=False)

    mirror = whichKey(flag_ping, info, key)
else:
    mirror = whichKey(flag_ping, info, 0)

if flag_list:
    exit()
elif not flag_choose:
    currentMirror()

# Switch mirror from resolvable url back to full path
for m in archives.splitlines():
    if mirror in m:
        mirror = m
        break

lines = ''.join(lines)
for r in repo:
    lines = lines.replace(r, mirror)

def yesOrNo():
    y = 'yes'
    n = 'no'
    options = "Please enter '%s' or '%s': " % (y,n)
    while True:
        answer = ask(query)
        if answer == y:
            break
        elif answer == n:
            exit(0)
        else:
            query = options

wd = getcwd()
if wd == directory[0:-1]:
    query = (
        "'%(dir)s' is the current directory.\n"
        "Generating a new '%(apt)s' file will "
        "overwrite the current file.\n"
        "You should copy or backup '%(apt)s' before replacing it.\n"
        "Continue?\n[yes|no] " % {
            'dir': directory,
            'apt': apt_file
        }
    )
    yesOrNo()

write_file = wd + "/" + apt_file
try:
    with open(write_file, 'w') as f:
        f.write(lines)
except IOError as err:
    if err.strerror == 'Permission denied':
        errorExit(
            (
                "%s\nYou do not own %s\n"
                "Please run the script from a directory you own." % (
                    err,
                    wd
                )
            ),
            1
        )
    else:
        errorExit(err, 1)
else:
    print("New config file saved to %s" % write_file)

exit(0)
<|endoftext|>


<commit_before>import os

import pygame
from too_many_cooks import ingredient

from too_many_cooks.errors import CantGetItem, NoIngredientError
from too_many_cooks.globals import GlobalVars
from too_many_cooks.tile import Tile

class Appliance(object):
    def __init__(self, image_path, start_x, start_y):
        super().__init__()
        self.scale = GlobalVars.scale * 4

        self.image = pygame.image.load(image_path)
        self.image = pygame.transform.scale(self.image,
                                            (int(self.image.get_width() * self.scale),
                                             int(self.image.get_height() * self.scale)))

        self.current_tile = {
            'x': start_x,
            'y': start_y
        }
        self.contents = []

    def render(self, screen):
        x, y = Tile.tile_to_pixel(current_tile=self.current_tile)
        screen.blit(self.image, (x, y))

    def use(self, user):
        pass

    def recieve_ingredients(self, user, ingredients, keep_ingredients=False):
        recieved_ingredients = False
        for ingredient in ingredients:
            try:
                ingr = user.use_ingredient(ingredient)
                if keep_ingredients:
                    self.contents.append(ingr)
                recieved_ingredients = True
                print('Used {}'.format(ingredient))
            except NoIngredientError:
                pass

        if not recieved_ingredients:
            print("You're not holding any {}".format(' or '.join(ingredients)))
            GlobalVars.show_menu('No Ingredient')
            return False
        else:
            return True

class Storage(Appliance):
    def __init__(self, start_x, start_y, type, contents=None):
        image_path = (os.path.join('sprites', '{}.png'.format(type)))
        super().__init__(image_path, start_x, start_y)
        self.contents = contents

    def use(self, user):
        if user.hands_are_full():
            raise CantGetItem
        GlobalVars.show_menu('Show Ingredients', contents=self.contents)

class Grill(Appliance):
    def __init__(self, start_x, start_y):
        image_path = (os.path.join('sprites', 'grill.png'))
        super().__init__(image_path, start_x, start_y)

    def use(self, user):
        if self.recieve_ingredients(user, ['Beef Patty', 'Bacon']):
            GlobalVars.player.get_ingredient(ingredient.cooked_beef_patty)

class Fryer(Appliance):
    def __init__(self, start_x, start_y):
        image_path = (os.path.join('sprites', 'fryer.png'))
        super().__init__(image_path, start_x, start_y)

    def use(self, user):
        if self.recieve_ingredients(user, ['Potato']):
            GlobalVars.player.get_ingredient(ingredient.fries)

class ChoppingBlock(Appliance):
    def __init__(self, start_x, start_y):
        image_path = (os.path.join('sprites', 'choppingblock.png'))
        super().__init__(image_path, start_x, start_y)

    def use(self, user):
        ingr_1, ingr_2 = user.get_ingredients()

class Oven(Appliance):
    def __init__(self, start_x, start_y):
        image_path = (os.path.join('sprites', 'oven.png'))
        super().__init__(image_path, start_x, start_y)

    def use(self, user):
        ingr_1, ingr_2 = user.get_ingredients()

class CounterTop(Appliance):
    def __init__(self, start_x, start_y):
        image_path = (os.path.join('sprites', 'countertop.png'))
        super().__init__(image_path, start_x, start_y)

    def use(self, user):
        if self.recieve_ingredients(user, ['Cooked Patty', 'Burger Buns', 'Lettuce', 'Tomato']):

class Stove(Appliance):
    def __init__(self, start_x, start_y):
        image_path = (os.path.join('sprites', 'stove.png'))
        super().__init__(image_path, start_x, start_y)

    def use(self, user):
        ingr_1, ingr_2 = user.get_ingredients()
        self.recieve_ingredients(user, ['Tomato', 'Chicken', 'Noodles'])

<commit_msg>Burger recipe<commit_after>import os

import pygame
from too_many_cooks import ingredient

from too_many_cooks.errors import CantGetItem, NoIngredientError
from too_many_cooks.globals import GlobalVars
from too_many_cooks.recipe import BurgerRecipe
from too_many_cooks.tile import Tile

class Appliance(object):
    def __init__(self, image_path, start_x, start_y):
        super().__init__()
        self.scale = GlobalVars.scale * 4

        self.image = pygame.image.load(image_path)
        self.image = pygame.transform.scale(self.image,
                                            (int(self.image.get_width() * self.scale),
                                             int(self.image.get_height() * self.scale)))

        self.current_tile = {
            'x': start_x,
            'y': start_y
        }
        self.contents = []

    def render(self, screen):
        x, y = Tile.tile_to_pixel(current_tile=self.current_tile)
        screen.blit(self.image, (x, y))

    def use(self, user):
        pass

    def recieve_ingredients(self, user, ingredients, keep_ingredients=False):
        recieved_ingredients = False
        for ingredient in ingredients:
            try:
                ingr = user.use_ingredient(ingredient)
                if keep_ingredients:
                    self.contents.append(ingr)
                recieved_ingredients = True
                print('Used {}'.format(ingredient))
            except NoIngredientError:
                pass

        if not recieved_ingredients:
            print("You're not holding any {}".format(' or '.join(ingredients)))
            GlobalVars.show_menu('No Ingredient')
            return False
        else:
            return True

class Storage(Appliance):
    def __init__(self, start_x, start_y, type, contents=None):
        image_path = (os.path.join('sprites', '{}.png'.format(type)))
        super().__init__(image_path, start_x, start_y)
        self.contents = contents

    def use(self, user):
        if user.hands_are_full():
            raise CantGetItem
        GlobalVars.show_menu('Show Ingredients', contents=self.contents)

class Grill(Appliance):
    def __init__(self, start_x, start_y):
        image_path = (os.path.join('sprites', 'grill.png'))
        super().__init__(image_path, start_x, start_y)

    def use(self, user):
        if self.recieve_ingredients(user, ['Beef Patty', 'Bacon']):
            GlobalVars.player.get_ingredient(ingredient.cooked_beef_patty)

class Fryer(Appliance):
    def __init__(self, start_x, start_y):
        image_path = (os.path.join('sprites', 'fryer.png'))
        super().__init__(image_path, start_x, start_y)

    def use(self, user):
        if self.recieve_ingredients(user, ['Potato']):
            GlobalVars.player.get_ingredient(ingredient.fries)

class ChoppingBlock(Appliance):
    def __init__(self, start_x, start_y):
        image_path = (os.path.join('sprites', 'choppingblock.png'))
        super().__init__(image_path, start_x, start_y)

    def use(self, user):
        ingr_1, ingr_2 = user.get_ingredients()

class Oven(Appliance):
    def __init__(self, start_x, start_y):
        image_path = (os.path.join('sprites', 'oven.png'))
        super().__init__(image_path, start_x, start_y)

    def use(self, user):
        ingr_1, ingr_2 = user.get_ingredients()

class CounterTop(Appliance):
    def __init__(self, start_x, start_y):
        image_path = (os.path.join('sprites', 'countertop.png'))
        super().__init__(image_path, start_x, start_y)

    def use(self, user):
        if self.recieve_ingredients(user, ['Cooked Patty', 'Burger Buns', 'Lettuce', 'Tomato']):
            if BurgerRecipe.can_make(self.contents):
                print ("Made a burger")

class Stove(Appliance):
    def __init__(self, start_x, start_y):
        image_path = (os.path.join('sprites', 'stove.png'))
        super().__init__(image_path, start_x, start_y)

    def use(self, user):
        ingr_1, ingr_2 = user.get_ingredients()
        self.recieve_ingredients(user, ['Tomato', 'Chicken', 'Noodles'])

<|endoftext|>


<commit_before># Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import re

import android_commands
import json
import math

# Valid values of result type.
RESULT_TYPES = {'unimportant': 'RESULT ',
                'default': '*RESULT ',
                'informational': '',
                'unimportant-histogram': 'HISTOGRAM ',
                'histogram': '*HISTOGRAM '}

def _EscapePerfResult(s):
  """Escapes |s| for use in a perf result."""
  return re.sub('[\:|=/#&]', '_', s)

def GeomMeanAndStdDevFromHistogram(histogram_json):
  histogram = json.loads(histogram_json)
  # Handle empty histograms gracefully.
  if not 'buckets' in histogram:
    return 0.0, 0.0
  count = 0
  sum_of_logs = 0
  for bucket in histogram['buckets']:
    if 'high' in bucket:
      bucket['mean'] = (bucket['low'] + bucket['high']) / 2.0
    else:
      bucket['mean'] = bucket['low']
    if bucket['mean'] > 0:
      sum_of_logs += math.log(bucket['mean']) * bucket['count']
      count += bucket['count']

  if count == 0:
    return 0.0, 0.0

  sum_of_squares = 0
  geom_mean = math.exp(sum_of_logs / count)
  for bucket in histogram['buckets']:
    if bucket['mean'] > 0:
      sum_of_squares += (bucket['mean'] - geom_mean) ** 2 * bucket['count']
  return geom_mean, math.sqrt(sum_of_squares / count)

def _MeanAndStdDevFromList(values):
  avg = None
  sd = None
  if len(values) > 1:
    try:
      value = '[%s]' % ','.join([str(v) for v in values])
      avg = sum([float(v) for v in values]) / len(values)
      sqdiffs = [(float(v) - avg) ** 2 for v in values]
      variance = sum(sqdiffs) / (len(values) - 1)
      sd = math.sqrt(variance)
    except ValueError:
      value = ", ".join(values)
  else:
    value = values[0]
  return value, avg, sd

def PrintPerfResult(measurement, trace, values, units, result_type='default',
                    print_to_stdout=True):
  """Prints numerical data to stdout in the format required by perf tests.

  The string args may be empty but they must not contain any colons (:) or
  equals signs (=).

  Args:
    measurement: A description of the quantity being measured, e.g. "vm_peak".
    trace: A description of the particular data point, e.g. "reference".
    values: A list of numeric measured values.
    units: A description of the units of measure, e.g. "bytes".
    result_type: Accepts values of RESULT_TYPES.
    print_to_stdout: If True, prints the output in stdout instead of returning
        the output to caller.

    Returns:
      String of the formated perf result.
  """
  assert result_type in RESULT_TYPES, 'result type: %s is invalid' % result_type

  trace_name = _EscapePerfResult(trace)

  if result_type in ['unimportant', 'default', 'informational']:
    assert isinstance(values, list)
    assert len(values)
    assert '/' not in measurement
    value, avg, sd = _MeanAndStdDevFromList(values)
    output = '%s%s: %s%s%s %s' % (
        RESULT_TYPES[result_type],
        _EscapePerfResult(measurement),
        trace_name,
        # Do not show equal sign if the trace is empty. Usually it happens when
        # measurement is enough clear to describe the result.
        '= ' if trace_name else '',
        value,
        units)
  else:
    assert(result_type in ['histogram', 'unimportant-histogram'])
    assert isinstance(values, list)
    # The histograms can only be printed individually, there's no computation
    # across different histograms.
    assert len(values) == 1
    value = values[0]
    measurement += '.' + trace_name
    output = '%s%s: %s= %s' % (
        RESULT_TYPES[result_type],
        _EscapePerfResult(measurement),
        _EscapePerfResult(measurement),
        value)
    avg, sd = GeomMeanAndStdDevFromHistogram(value)

  if avg:
    output += '\nAvg %s: %f%s' % (measurement, avg, units)
  if sd:
    output += '\nSd  %s: %f%s' % (measurement, sd, units)
  if print_to_stdout:
    print output
  return output

class PerfTestSetup(object):
  """Provides methods for setting up a device for perf testing."""
  _DROP_CACHES = '/proc/sys/vm/drop_caches'
  _SCALING_GOVERNOR = '/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor'

  def __init__(self, adb):
    self._adb = adb
    num_cpus = self._adb.GetFileContents('/sys/devices/system/cpu/online',
                                         log_result=False)
    assert num_cpus, 'Unable to find /sys/devices/system/cpu/online'
    self._num_cpus = int(num_cpus[0].split('-')[-1])
    self._original_scaling_governor = None

  def DropRamCaches(self):
    """Drops the filesystem ram caches for performance testing."""
    if not self._adb.IsRootEnabled():
      self._adb.EnableAdbRoot()
    self._adb.RunShellCommand('sync')
    self._adb.RunShellCommand('echo 3 > ' + PerfTestSetup._DROP_CACHES)

  def SetUp(self):
    """Sets up performance tests."""
    if not self._original_scaling_governor:
      self._original_scaling_governor = self._adb.GetFileContents(
          PerfTestSetup._SCALING_GOVERNOR % 0,
          log_result=False)[0]
      self._SetScalingGovernorInternal('performance')
    self.DropRamCaches()

  def TearDown(self):
    """Tears down performance tests."""
    if self._original_scaling_governor:
      self._SetScalingGovernorInternal(self._original_scaling_governor)
    self._original_scaling_governor = None

  def _SetScalingGovernorInternal(self, value):
    for cpu in range(self._num_cpus):
      self._adb.RunShellCommand(
          ('echo %s > ' + PerfTestSetup._SCALING_GOVERNOR) % (value, cpu))
<commit_msg>Android: fix cpu governor setup for perf tests<commit_after># Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import re

import android_commands
import json
import math

# Valid values of result type.
RESULT_TYPES = {'unimportant': 'RESULT ',
                'default': '*RESULT ',
                'informational': '',
                'unimportant-histogram': 'HISTOGRAM ',
                'histogram': '*HISTOGRAM '}

def _EscapePerfResult(s):
  """Escapes |s| for use in a perf result."""
  return re.sub('[\:|=/#&]', '_', s)

def GeomMeanAndStdDevFromHistogram(histogram_json):
  histogram = json.loads(histogram_json)
  # Handle empty histograms gracefully.
  if not 'buckets' in histogram:
    return 0.0, 0.0
  count = 0
  sum_of_logs = 0
  for bucket in histogram['buckets']:
    if 'high' in bucket:
      bucket['mean'] = (bucket['low'] + bucket['high']) / 2.0
    else:
      bucket['mean'] = bucket['low']
    if bucket['mean'] > 0:
      sum_of_logs += math.log(bucket['mean']) * bucket['count']
      count += bucket['count']

  if count == 0:
    return 0.0, 0.0

  sum_of_squares = 0
  geom_mean = math.exp(sum_of_logs / count)
  for bucket in histogram['buckets']:
    if bucket['mean'] > 0:
      sum_of_squares += (bucket['mean'] - geom_mean) ** 2 * bucket['count']
  return geom_mean, math.sqrt(sum_of_squares / count)

def _MeanAndStdDevFromList(values):
  avg = None
  sd = None
  if len(values) > 1:
    try:
      value = '[%s]' % ','.join([str(v) for v in values])
      avg = sum([float(v) for v in values]) / len(values)
      sqdiffs = [(float(v) - avg) ** 2 for v in values]
      variance = sum(sqdiffs) / (len(values) - 1)
      sd = math.sqrt(variance)
    except ValueError:
      value = ", ".join(values)
  else:
    value = values[0]
  return value, avg, sd

def PrintPerfResult(measurement, trace, values, units, result_type='default',
                    print_to_stdout=True):
  """Prints numerical data to stdout in the format required by perf tests.

  The string args may be empty but they must not contain any colons (:) or
  equals signs (=).

  Args:
    measurement: A description of the quantity being measured, e.g. "vm_peak".
    trace: A description of the particular data point, e.g. "reference".
    values: A list of numeric measured values.
    units: A description of the units of measure, e.g. "bytes".
    result_type: Accepts values of RESULT_TYPES.
    print_to_stdout: If True, prints the output in stdout instead of returning
        the output to caller.

    Returns:
      String of the formated perf result.
  """
  assert result_type in RESULT_TYPES, 'result type: %s is invalid' % result_type

  trace_name = _EscapePerfResult(trace)

  if result_type in ['unimportant', 'default', 'informational']:
    assert isinstance(values, list)
    assert len(values)
    assert '/' not in measurement
    value, avg, sd = _MeanAndStdDevFromList(values)
    output = '%s%s: %s%s%s %s' % (
        RESULT_TYPES[result_type],
        _EscapePerfResult(measurement),
        trace_name,
        # Do not show equal sign if the trace is empty. Usually it happens when
        # measurement is enough clear to describe the result.
        '= ' if trace_name else '',
        value,
        units)
  else:
    assert(result_type in ['histogram', 'unimportant-histogram'])
    assert isinstance(values, list)
    # The histograms can only be printed individually, there's no computation
    # across different histograms.
    assert len(values) == 1
    value = values[0]
    measurement += '.' + trace_name
    output = '%s%s: %s= %s' % (
        RESULT_TYPES[result_type],
        _EscapePerfResult(measurement),
        _EscapePerfResult(measurement),
        value)
    avg, sd = GeomMeanAndStdDevFromHistogram(value)

  if avg:
    output += '\nAvg %s: %f%s' % (measurement, avg, units)
  if sd:
    output += '\nSd  %s: %f%s' % (measurement, sd, units)
  if print_to_stdout:
    print output
  return output

class PerfTestSetup(object):
  """Provides methods for setting up a device for perf testing."""
  _DROP_CACHES = '/proc/sys/vm/drop_caches'
  _SCALING_GOVERNOR_FMT = (
      '/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor')

  def __init__(self, adb):
    self._adb = adb
    kernel_max = self._adb.GetFileContents('/sys/devices/system/cpu/kernel_max',
                                         log_result=False)
    assert kernel_max, 'Unable to find /sys/devices/system/cpu/kernel_max'
    self._kernel_max = int(kernel_max[0])
    self._original_scaling_governor = None

  def DropRamCaches(self):
    """Drops the filesystem ram caches for performance testing."""
    if not self._adb.IsRootEnabled():
      self._adb.EnableAdbRoot()
    self._adb.RunShellCommand('sync')
    self._adb.RunShellCommand('echo 3 > ' + PerfTestSetup._DROP_CACHES)

  def SetUp(self):
    """Sets up performance tests."""
    if not self._original_scaling_governor:
      self._original_scaling_governor = self._adb.GetFileContents(
          PerfTestSetup._SCALING_GOVERNOR_FMT % 0,
          log_result=False)[0]
      self._SetScalingGovernorInternal('performance')
    self.DropRamCaches()

  def TearDown(self):
    """Tears down performance tests."""
    if self._original_scaling_governor:
      self._SetScalingGovernorInternal(self._original_scaling_governor)
    self._original_scaling_governor = None

  def _SetScalingGovernorInternal(self, value):
    for cpu in range(self._kernel_max + 1):
      scaling_governor_file = PerfTestSetup._SCALING_GOVERNOR_FMT % cpu
      if self._adb.Adb().DoesFileExist(scaling_governor_file):
        self._adb.RunShellCommand(
            ('echo %s > ' + scaling_governor_file) % value)
<|endoftext|>


<commit_before># Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from __future__ import absolute_import, division, print_function, unicode_literals

import platform
import re
import shlex
import sys

def is_windows():
    """Returns true if the system we are currently running on
    is a Windows system"""
    return sys.platform.startswith("win")

def get_linux_type():
    try:
        with open("/etc/os-release") as f:
            data = f.read()
    except EnvironmentError:
        return (None, None)

    os_vars = {}
    for line in data.splitlines():
        parts = line.split("=", 1)
        if len(parts) != 2:
            continue
        key = parts[0].strip()
        value_parts = shlex.split(parts[1].strip())
        if not value_parts:
            value = ""
        else:
            value = value_parts[0]
        os_vars[key] = value

    name = os_vars.get("NAME")
    if name:
        name = name.lower()
        name = re.sub("linux", "", name)
        name = name.strip().replace(" ", "_")

    version_id = os_vars.get("VERSION_ID")
    if version_id:
        version_id = version_id.lower()

    return "linux", name, version_id

class HostType(object):
    def __init__(self, ostype=None, distro=None, distrovers=None):
        if ostype is None:
            distro = None
            distrovers = None
            if sys.platform.startswith("linux"):
                ostype, distro, distrovers = get_linux_type()
            elif sys.platform.startswith("darwin"):
                ostype = "darwin"
            elif is_windows():
                ostype = "windows"
                distrovers = str(sys.getwindowsversion().major)
            else:
                ostype = sys.platform

        # The operating system type
        self.ostype = ostype
        # The distribution, if applicable
        self.distro = distro
        # The OS/distro version if known
        self.distrovers = distrovers
        machine = platform.machine().lower()
        if "arm" in machine or "aarch" in machine:
            self.isarm = True
        else:
            self.isarm = False

    def is_windows(self):
        return self.ostype == "windows"

    def is_arm(self):
        return self.isarm

    def is_darwin(self):
        return self.ostype == "darwin"

    def is_linux(self):
        return self.ostype == "linux"

    def as_tuple_string(self):
        return "%s-%s-%s" % (
            self.ostype,
            self.distro or "none",
            self.distrovers or "none",
        )

    def get_package_manager(self):
        if not self.is_linux():
            return None
        if self.distro in ("fedora", "centos", "centos stream"):
            return "rpm"
        if self.distro.startswith(("debian", "ubuntu")):
            return "deb"
        return None

    @staticmethod
    def from_tuple_string(s):
        ostype, distro, distrovers = s.split("-")
        return HostType(ostype=ostype, distro=distro, distrovers=distrovers)

    def __eq__(self, b):
        return (
            self.ostype == b.ostype
            and self.distro == b.distro
            and self.distrovers == b.distrovers
        )
<commit_msg>fix distro name<commit_after># Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from __future__ import absolute_import, division, print_function, unicode_literals

import platform
import re
import shlex
import sys

def is_windows():
    """Returns true if the system we are currently running on
    is a Windows system"""
    return sys.platform.startswith("win")

def get_linux_type():
    try:
        with open("/etc/os-release") as f:
            data = f.read()
    except EnvironmentError:
        return (None, None)

    os_vars = {}
    for line in data.splitlines():
        parts = line.split("=", 1)
        if len(parts) != 2:
            continue
        key = parts[0].strip()
        value_parts = shlex.split(parts[1].strip())
        if not value_parts:
            value = ""
        else:
            value = value_parts[0]
        os_vars[key] = value

    name = os_vars.get("NAME")
    if name:
        name = name.lower()
        name = re.sub("linux", "", name)
        name = name.strip().replace(" ", "_")

    version_id = os_vars.get("VERSION_ID")
    if version_id:
        version_id = version_id.lower()

    return "linux", name, version_id

class HostType(object):
    def __init__(self, ostype=None, distro=None, distrovers=None):
        if ostype is None:
            distro = None
            distrovers = None
            if sys.platform.startswith("linux"):
                ostype, distro, distrovers = get_linux_type()
            elif sys.platform.startswith("darwin"):
                ostype = "darwin"
            elif is_windows():
                ostype = "windows"
                distrovers = str(sys.getwindowsversion().major)
            else:
                ostype = sys.platform

        # The operating system type
        self.ostype = ostype
        # The distribution, if applicable
        self.distro = distro
        # The OS/distro version if known
        self.distrovers = distrovers
        machine = platform.machine().lower()
        if "arm" in machine or "aarch" in machine:
            self.isarm = True
        else:
            self.isarm = False

    def is_windows(self):
        return self.ostype == "windows"

    def is_arm(self):
        return self.isarm

    def is_darwin(self):
        return self.ostype == "darwin"

    def is_linux(self):
        return self.ostype == "linux"

    def as_tuple_string(self):
        return "%s-%s-%s" % (
            self.ostype,
            self.distro or "none",
            self.distrovers or "none",
        )

    def get_package_manager(self):
        if not self.is_linux():
            return None
        if self.distro in ("fedora", "centos", "centos_stream"):
            return "rpm"
        if self.distro.startswith(("debian", "ubuntu")):
            return "deb"
        return None

    @staticmethod
    def from_tuple_string(s):
        ostype, distro, distrovers = s.split("-")
        return HostType(ostype=ostype, distro=distro, distrovers=distrovers)

    def __eq__(self, b):
        return (
            self.ostype == b.ostype
            and self.distro == b.distro
            and self.distrovers == b.distrovers
        )
<|endoftext|>


<commit_before>#!/usr/bin/env python

# generate Python Manifest for the OpenEmbedded build system
# (C) 2002-2008 Michael 'Mickey' Lauer <mlauer@vanille-media.de>
# (C) 2007 Jeremy Laine
# licensed under MIT, see COPYING.MIT

import os
import sys
import time

VERSION = "2.5.2"
BASEREV = 0

__author__ = "Michael 'Mickey' Lauer <mlauer@vanille-media.de>"
__version__ = "20080306"

class MakefileMaker:

    def __init__( self, outfile ):
        """initialize"""
        self.packages = {}
        self.targetPrefix = "${libdir}/python%s/" % VERSION[:3]
        self.output = outfile
        self.out( "#" * 120 )
        self.out( "### AUTO-GENERATED by '%s' [(C) 2002-2008 Michael 'Mickey' Lauer <mlauer@vanille-media.de>] on %s" % ( sys.argv[0], time.asctime() ) )
        self.out( "###" )
        self.out( "### Visit THE Python for Embedded Systems Site => http://www.Vanille.de/projects/python.spy" )
        self.out( "###" )
        self.out( "### Warning: Manual edits will be lost!" )
        self.out( "###" )
        self.out( "#" * 120 )
    #
    # helper functions
    #

    def out( self, data ):
        """print a line to the output file"""
        print >> self.output, data

    def setPrefix( self, targetPrefix ):
        """set a file prefix for addPackage files"""
        self.targetPrefix = targetPrefix

    def doProlog( self ):
        self.out( """ """ )
        self.out( "" )

    def addPackage( self, revision, name, description, dependencies, filenames ):
        """add a package to the Makefile"""
        if type( filenames ) == type( "" ):
            filenames = filenames.split()
        fullFilenames = []
        for filename in filenames:
            if filename[0] != "$":
                fullFilenames.append( "%s%s" % ( self.targetPrefix, filename ) )
            else:
                fullFilenames.append( filename )
        self.packages[name] = revision, description, dependencies, fullFilenames

    def doBody( self ):
        """generate body of Makefile"""

        global VERSION

        #
        # generate provides line
        #

        provideLine = 'PROVIDES+="'
        for name in self.packages:
            provideLine += "%s " % name
        provideLine += '"'

        self.out( provideLine )
        self.out( "" )

        #
        # generate package line
        #

        packageLine = 'PACKAGES="'
        for name in self.packages:
            packageLine += "%s " % name
        packageLine += '"'

        self.out( packageLine )
        self.out( "" )

        #
        # generate package variables
        #

        for name, data in self.packages.iteritems():
            rev, desc, deps, files = data

            #
            # write out the description, revision and dependencies
            #
            self.out( 'DESCRIPTION_%s="%s"' % ( name, desc ) )
            self.out( 'PR_%s="ml%d"' % ( name, rev + BASEREV ) )
            self.out( 'RDEPENDS_%s="%s"' % ( name, deps ) )

            line = 'FILES_%s="' % name

            #
            # check which directories to make in the temporary directory
            #

            dirset = {} # if python had a set-datatype this would be sufficient. for now, we're using a dict instead.
            for target in files:
                dirset[os.path.dirname( target )] = True

            #
            # generate which files to copy for the target (-dfR because whole directories are also allowed)
            #

            for target in files:
                line += "%s " % target

            line += '"'
            self.out( line )
            self.out( "" )

    def doEpilog( self ):
        self.out( """""" )
        self.out( "" )

    def make( self ):
        self.doProlog()
        self.doBody()
        self.doEpilog()

if __name__ == "__main__":

    if len( sys.argv ) > 1:
        os.popen( "rm -f ./%s" % sys.argv[1] )
        outfile = file( sys.argv[1], "w" )
    else:
        outfile = sys.stdout

    m = MakefileMaker( outfile )

    # Add packages here. Only specify dlopen-style library dependencies here, no ldd-style dependencies!
    # Parameters: revision, name, description, dependencies, filenames
    #

    m.addPackage( 0, "python-core", "Python Interpreter and core modules (needed!)", "",
    "__future__.* copy.* copy_reg.* ConfigParser.* " +
    "getopt.* linecache.* new.* " +
    "os.* posixpath.* struct.* " +
    "warnings.* site.* stat.* " +
    "UserDict.* UserList.* UserString.* " +
    "lib-dynload/binascii.so lib-dynload/_struct.so lib-dynload/time.so " +
    "lib-dynload/xreadlines.so types.* ${bindir}/python*" )

    m.addPackage( 0, "python-core-dbg", "Python core module debug information", "python-core",
    "lib-dynload/.debug ${bindir}/.debug ${libdir}/.debug" )

    m.addPackage( 0, "python-devel", "Python Development Package", "python-core",
    "${includedir} config" ) # package

    m.addPackage( 0, "python-idle", "Python Integrated Development Environment", "python-core python-tkinter",
    "${bindir}/idle idlelib" ) # package

    m.addPackage( 0, "python-pydoc", "Python Interactive Help Support", "python-core python-lang python-stringold python-re",
    "${bindir}/pydoc pydoc.*" )

    m.addPackage( 0, "python-smtpd", "Python Simple Mail Transport Daemon", "python-core python-netserver python-email python-mime",
    "${bindir}/smtpd.*" )

    m.addPackage( 0, "python-audio", "Python Audio Handling", "python-core",
    "wave.* chunk.* sndhdr.* lib-dynload/ossaudiodev.so lib-dynload/audioop.so" )

    m.addPackage( 0, "python-bsddb", "Python Berkeley Database Bindings", "python-core",
    "bsddb" ) # package

    m.addPackage( 0, "python-codecs", "Python Codecs, Encodings & i18n Support", "python-core python-lang",
    "codecs.* encodings gettext.* locale.* lib-dynload/_locale.so lib-dynload/unicodedata.so stringprep.* xdrlib.*" )

    m.addPackage( 0, "python-compile", "Python Bytecode Compilation Support", "python-core",
    "py_compile.* compileall.*" )

    m.addPackage( 0, "python-compiler", "Python Compiler Support", "python-core",
    "compiler" ) # package

    m.addPackage( 1, "python-compression", "Python High Level Compression Support", "python-core python-zlib",
    "gzip.* zipfile.* tarfile.*" )

    m.addPackage( 1, "python-crypt", "Python Basic Cryptographic and Hashing Support", "python-core",
    "hashlib.* md5.* sha.* lib-dynload/crypt.so lib-dynload/_hashlib.so lib-dynload/_sha256.so lib-dynload/_sha512.so" )

    m.addPackage( 0, "python-textutils", "Python Option Parsing, Text Wrapping and Comma-Separated-Value Support", "python-core python-io python-re python-stringold",
    "lib-dynload/_csv.so csv.* optparse.* textwrap.*" )

    m.addPackage( 0, "python-curses", "Python Curses Support", "python-core",
    "curses lib-dynload/_curses.so lib-dynload/_curses_panel.so" ) # directory + low level module

    m.addPackage( 0, "python-ctypes", "Python C Types Support", "python-core",
    "ctypes lib-dynload/_ctypes.so" ) # directory + low level module

    m.addPackage( 0, "python-datetime", "Python Calendar and Time support", "python-core python-codecs",
    "_strptime.* calendar.* lib-dynload/datetime.so" )

    m.addPackage( 0, "python-db", "Python File-Based Database Support", "python-core",
    "anydbm.* dumbdbm.* whichdb.* " )

    m.addPackage( 0, "python-debugger", "Python Debugger", "python-core python-io python-lang python-re python-stringold python-shell",
    "bdb.* pdb.*" )

    m.addPackage( 0, "python-distutils", "Python Distribution Utilities", "python-core",
    "config distutils" ) # package

    m.addPackage( 0, "python-email", "Python Email Support", "python-core python-io python-re python-mime python-audio python-image",
    "email" ) # package

    m.addPackage( 0, "python-fcntl", "Python's fcntl Interface", "python-core",
    "lib-dynload/fcntl.so" )

    m.addPackage( 0, "python-hotshot", "Python Hotshot Profiler", "python-core",
    "hotshot lib-dynload/_hotshot.so" )

    m.addPackage( 0, "python-html", "Python HTML Processing", "python-core",
    "formatter.* htmlentitydefs.* htmllib.* markupbase.* sgmllib.* " )

    m.addPackage( 0, "python-gdbm", "Python GNU Database Support", "python-core",
    "lib-dynload/gdbm.so" )

    m.addPackage( 0, "python-image", "Python Graphical Image Handling", "python-core",
    "colorsys.* imghdr.* lib-dynload/imageop.so lib-dynload/rgbimg.so" )

    m.addPackage( 0, "python-io", "Python Low-Level I/O", "python-core python-math",
    "lib-dynload/_socket.so lib-dynload/_ssl.so lib-dynload/select.so lib-dynload/termios.so lib-dynload/cStringIO.so "
    "pipes.* socket.* tempfile.* StringIO.* " )

    m.addPackage( 0, "python-lang", "Python Low-Level Language Support", "python-core",
    "lib-dynload/array.so lib-dynload/parser.so lib-dynload/operator.so lib-dynload/_weakref.so " +
    "lib-dynload/itertools.so lib-dynload/collections.so lib-dynload/_bisect.so lib-dynload/_heapq.so " +
    "atexit.* bisect.* code.* codeop.* dis.* heapq.* inspect.* keyword.* opcode.* repr.* token.* tokenize.* " +
    "traceback.* linecache.* weakref.*" )

    m.addPackage( 0, "python-logging", "Python Logging Support", "python-core python-io python-lang python-stringold",
    "logging" ) # package

    m.addPackage( 0, "python-lib-old-and-deprecated", "Python Deprecated Libraries", "python-core",
    "lib-old" ) # package

    m.addPackage( 0, "python-tkinter", "Python Tcl/Tk Bindings", "python-core",
    "lib-dynload/_tkinter.so lib-tk" ) # package

    m.addPackage( 0, "python-math", "Python Math Support", "python-core",
    "lib-dynload/cmath.so lib-dynload/math.so lib-dynload/_random.so random.* sets.*" )

    m.addPackage( 0, "python-mime", "Python MIME Handling APIs", "python-core python-io",
    "mimetools.* uu.* quopri.* rfc822.*" )

    m.addPackage( 0, "python-mmap", "Python Memory-Mapped-File Support", "python-core python-io",
    "lib-dynload/mmap.so " )

    m.addPackage( 0, "python-unixadmin", "Python Unix Administration Support", "python-core",
    "lib-dynload/nis.so lib-dynload/grp.so lib-dynload/pwd.so getpass.*" )

    m.addPackage( 0, "python-netclient", "Python Internet Protocol Clients", "python-core python-crypt python-datetime python-io python-lang python-logging python-mime",
    "*Cookie*.* " +
    "base64.* cookielib.* ftplib.* gopherlib.* hmac.* httplib.* mimetypes.* nntplib.* poplib.* smtplib.* telnetlib.* urllib.* urllib2.* urlparse.*" )

    m.addPackage( 0, "python-netserver", "Python Internet Protocol Servers", "python-core python-netclient",
    "cgi.* BaseHTTPServer.* SimpleHTTPServer.* SocketServer.*" )

    m.addPackage( 0, "python-pickle", "Python Persistence Support", "python-core python-codecs python-io python-re",
    "pickle.* shelve.* lib-dynload/cPickle.so" )

    m.addPackage( 0, "python-pprint", "Python Pretty-Print Support", "python-core",
    "pprint.*" )

    m.addPackage( 0, "python-profile", "Python Basic Profiling Support", "python-core",
    "profile.* pstats.*" )

    m.addPackage( 0, "python-re", "Python Regular Expression APIs", "python-core",
    "re.* sre.* sre_compile.* sre_constants* sre_parse.*" ) # _sre is builtin

    m.addPackage( 0, "python-readline", "Python Readline Support", "python-core",
    "lib-dynload/readline.so rlcompleter.*" )

    m.addPackage( 0, "python-resource", "Python Resource Control Interface", "python-core",
    "lib-dynload/resource.so" )

    m.addPackage( 0, "python-shell", "Python Shell-Like Functionality", "python-core python-re",
    "cmd.* commands.* dircache.* fnmatch.* glob.* popen2.* shutil.*" )

    m.addPackage( 0, "python-robotparser", "Python robots.txt parser", "python-core python-netclient",
    "robotparser.*")

    m.addPackage( 0, "python-subprocess", "Python Subprocess Support", "python-core python-io python-re python-fcntl python-pickle",
    "subprocess.*" )

    m.addPackage( 2, "python-sqlite3", "Python Sqlite3 Database Support", "python-core python-datetime python-lang python-crypt python-io python-threading python-zlib",
    "lib-dynload/_sqlite3.so sqlite3/dbapi2.* sqlite3/__init__.*" )

    m.addPackage( 0, "python-sqlite3-tests", "Python Sqlite3 Database Support Tests", "python-core python-sqlite3",
    "sqlite3/test" )

    m.addPackage( 0, "python-stringold", "Python String APIs [deprecated]", "python-core python-re",
    "lib-dynload/strop.so string.*" )

    m.addPackage( 0, "python-syslog", "Python's Syslog Interface", "python-core",
    "lib-dynload/syslog.so" )

    m.addPackage( 0, "python-terminal", "Python Terminal Controlling Support", "python-core python-io",
    "pty.* tty.*" )

    m.addPackage( 0, "python-tests", "Python Tests", "python-core",
    "test" ) # package

    m.addPackage( 0, "python-threading", "Python Threading & Synchronization Support", "python-core python-lang",
    "_threading_local.* dummy_thread.* dummy_threading.* mutex.* threading.* Queue.*" )

    m.addPackage( 0, "python-unittest", "Python Unit Testing Framework", "python-core python-stringold python-lang",
    "unittest.*" )

    m.addPackage( 1, "python-xml", "Python basic XML support.", "python-core python-re",
    "lib-dynload/pyexpat.so xml xmllib.*" ) # package

    m.addPackage( 0, "python-xmlrpc", "Python XMLRPC Support", "python-core python-xml python-netserver python-lang",
    "xmlrpclib.* SimpleXMLRPCServer.*" )

    m.addPackage( 0, "python-zlib", "Python zlib Support.", "python-core",
    "lib-dynload/zlib.so" )

    m.addPackage( 0, "python-mailbox", "Python Mailbox Format Support", "python-core python-mime",
    "mailbox.*" )

    m.make()
<commit_msg>generate-manifest-2.5.py: pdb requires pprint<commit_after>#!/usr/bin/env python

# generate Python Manifest for the OpenEmbedded build system
# (C) 2002-2008 Michael 'Mickey' Lauer <mlauer@vanille-media.de>
# (C) 2007 Jeremy Laine
# licensed under MIT, see COPYING.MIT

import os
import sys
import time

VERSION = "2.5.2"
BASEREV = 0

__author__ = "Michael 'Mickey' Lauer <mlauer@vanille-media.de>"
__version__ = "20080306"

class MakefileMaker:

    def __init__( self, outfile ):
        """initialize"""
        self.packages = {}
        self.targetPrefix = "${libdir}/python%s/" % VERSION[:3]
        self.output = outfile
        self.out( "#" * 120 )
        self.out( "### AUTO-GENERATED by '%s' [(C) 2002-2008 Michael 'Mickey' Lauer <mlauer@vanille-media.de>] on %s" % ( sys.argv[0], time.asctime() ) )
        self.out( "###" )
        self.out( "### Visit THE Python for Embedded Systems Site => http://www.Vanille.de/projects/python.spy" )
        self.out( "###" )
        self.out( "### Warning: Manual edits will be lost!" )
        self.out( "###" )
        self.out( "#" * 120 )
    #
    # helper functions
    #

    def out( self, data ):
        """print a line to the output file"""
        print >> self.output, data

    def setPrefix( self, targetPrefix ):
        """set a file prefix for addPackage files"""
        self.targetPrefix = targetPrefix

    def doProlog( self ):
        self.out( """ """ )
        self.out( "" )

    def addPackage( self, revision, name, description, dependencies, filenames ):
        """add a package to the Makefile"""
        if type( filenames ) == type( "" ):
            filenames = filenames.split()
        fullFilenames = []
        for filename in filenames:
            if filename[0] != "$":
                fullFilenames.append( "%s%s" % ( self.targetPrefix, filename ) )
            else:
                fullFilenames.append( filename )
        self.packages[name] = revision, description, dependencies, fullFilenames

    def doBody( self ):
        """generate body of Makefile"""

        global VERSION

        #
        # generate provides line
        #

        provideLine = 'PROVIDES+="'
        for name in self.packages:
            provideLine += "%s " % name
        provideLine += '"'

        self.out( provideLine )
        self.out( "" )

        #
        # generate package line
        #

        packageLine = 'PACKAGES="'
        for name in self.packages:
            packageLine += "%s " % name
        packageLine += '"'

        self.out( packageLine )
        self.out( "" )

        #
        # generate package variables
        #

        for name, data in self.packages.iteritems():
            rev, desc, deps, files = data

            #
            # write out the description, revision and dependencies
            #
            self.out( 'DESCRIPTION_%s="%s"' % ( name, desc ) )
            self.out( 'PR_%s="ml%d"' % ( name, rev + BASEREV ) )
            self.out( 'RDEPENDS_%s="%s"' % ( name, deps ) )

            line = 'FILES_%s="' % name

            #
            # check which directories to make in the temporary directory
            #

            dirset = {} # if python had a set-datatype this would be sufficient. for now, we're using a dict instead.
            for target in files:
                dirset[os.path.dirname( target )] = True

            #
            # generate which files to copy for the target (-dfR because whole directories are also allowed)
            #

            for target in files:
                line += "%s " % target

            line += '"'
            self.out( line )
            self.out( "" )

    def doEpilog( self ):
        self.out( """""" )
        self.out( "" )

    def make( self ):
        self.doProlog()
        self.doBody()
        self.doEpilog()

if __name__ == "__main__":

    if len( sys.argv ) > 1:
        os.popen( "rm -f ./%s" % sys.argv[1] )
        outfile = file( sys.argv[1], "w" )
    else:
        outfile = sys.stdout

    m = MakefileMaker( outfile )

    # Add packages here. Only specify dlopen-style library dependencies here, no ldd-style dependencies!
    # Parameters: revision, name, description, dependencies, filenames
    #

    m.addPackage( 0, "python-core", "Python Interpreter and core modules (needed!)", "",
    "__future__.* copy.* copy_reg.* ConfigParser.* " +
    "getopt.* linecache.* new.* " +
    "os.* posixpath.* struct.* " +
    "warnings.* site.* stat.* " +
    "UserDict.* UserList.* UserString.* " +
    "lib-dynload/binascii.so lib-dynload/_struct.so lib-dynload/time.so " +
    "lib-dynload/xreadlines.so types.* ${bindir}/python*" )

    m.addPackage( 0, "python-core-dbg", "Python core module debug information", "python-core",
    "lib-dynload/.debug ${bindir}/.debug ${libdir}/.debug" )

    m.addPackage( 0, "python-devel", "Python Development Package", "python-core",
    "${includedir} config" ) # package

    m.addPackage( 0, "python-idle", "Python Integrated Development Environment", "python-core python-tkinter",
    "${bindir}/idle idlelib" ) # package

    m.addPackage( 0, "python-pydoc", "Python Interactive Help Support", "python-core python-lang python-stringold python-re",
    "${bindir}/pydoc pydoc.*" )

    m.addPackage( 0, "python-smtpd", "Python Simple Mail Transport Daemon", "python-core python-netserver python-email python-mime",
    "${bindir}/smtpd.*" )

    m.addPackage( 0, "python-audio", "Python Audio Handling", "python-core",
    "wave.* chunk.* sndhdr.* lib-dynload/ossaudiodev.so lib-dynload/audioop.so" )

    m.addPackage( 0, "python-bsddb", "Python Berkeley Database Bindings", "python-core",
    "bsddb" ) # package

    m.addPackage( 0, "python-codecs", "Python Codecs, Encodings & i18n Support", "python-core python-lang",
    "codecs.* encodings gettext.* locale.* lib-dynload/_locale.so lib-dynload/unicodedata.so stringprep.* xdrlib.*" )

    m.addPackage( 0, "python-compile", "Python Bytecode Compilation Support", "python-core",
    "py_compile.* compileall.*" )

    m.addPackage( 0, "python-compiler", "Python Compiler Support", "python-core",
    "compiler" ) # package

    m.addPackage( 1, "python-compression", "Python High Level Compression Support", "python-core python-zlib",
    "gzip.* zipfile.* tarfile.*" )

    m.addPackage( 1, "python-crypt", "Python Basic Cryptographic and Hashing Support", "python-core",
    "hashlib.* md5.* sha.* lib-dynload/crypt.so lib-dynload/_hashlib.so lib-dynload/_sha256.so lib-dynload/_sha512.so" )

    m.addPackage( 0, "python-textutils", "Python Option Parsing, Text Wrapping and Comma-Separated-Value Support", "python-core python-io python-re python-stringold",
    "lib-dynload/_csv.so csv.* optparse.* textwrap.*" )

    m.addPackage( 0, "python-curses", "Python Curses Support", "python-core",
    "curses lib-dynload/_curses.so lib-dynload/_curses_panel.so" ) # directory + low level module

    m.addPackage( 0, "python-ctypes", "Python C Types Support", "python-core",
    "ctypes lib-dynload/_ctypes.so" ) # directory + low level module

    m.addPackage( 0, "python-datetime", "Python Calendar and Time support", "python-core python-codecs",
    "_strptime.* calendar.* lib-dynload/datetime.so" )

    m.addPackage( 0, "python-db", "Python File-Based Database Support", "python-core",
    "anydbm.* dumbdbm.* whichdb.* " )

    m.addPackage( 1, "python-debugger", "Python Debugger", "python-core python-io python-lang python-re python-stringold python-shell python-pprint",
    "bdb.* pdb.*" )

    m.addPackage( 0, "python-distutils", "Python Distribution Utilities", "python-core",
    "config distutils" ) # package

    m.addPackage( 0, "python-email", "Python Email Support", "python-core python-io python-re python-mime python-audio python-image",
    "email" ) # package

    m.addPackage( 0, "python-fcntl", "Python's fcntl Interface", "python-core",
    "lib-dynload/fcntl.so" )

    m.addPackage( 0, "python-hotshot", "Python Hotshot Profiler", "python-core",
    "hotshot lib-dynload/_hotshot.so" )

    m.addPackage( 0, "python-html", "Python HTML Processing", "python-core",
    "formatter.* htmlentitydefs.* htmllib.* markupbase.* sgmllib.* " )

    m.addPackage( 0, "python-gdbm", "Python GNU Database Support", "python-core",
    "lib-dynload/gdbm.so" )

    m.addPackage( 0, "python-image", "Python Graphical Image Handling", "python-core",
    "colorsys.* imghdr.* lib-dynload/imageop.so lib-dynload/rgbimg.so" )

    m.addPackage( 0, "python-io", "Python Low-Level I/O", "python-core python-math",
    "lib-dynload/_socket.so lib-dynload/_ssl.so lib-dynload/select.so lib-dynload/termios.so lib-dynload/cStringIO.so "
    "pipes.* socket.* tempfile.* StringIO.* " )

    m.addPackage( 0, "python-lang", "Python Low-Level Language Support", "python-core",
    "lib-dynload/array.so lib-dynload/parser.so lib-dynload/operator.so lib-dynload/_weakref.so " +
    "lib-dynload/itertools.so lib-dynload/collections.so lib-dynload/_bisect.so lib-dynload/_heapq.so " +
    "atexit.* bisect.* code.* codeop.* dis.* heapq.* inspect.* keyword.* opcode.* repr.* token.* tokenize.* " +
    "traceback.* linecache.* weakref.*" )

    m.addPackage( 0, "python-logging", "Python Logging Support", "python-core python-io python-lang python-stringold",
    "logging" ) # package

    m.addPackage( 0, "python-lib-old-and-deprecated", "Python Deprecated Libraries", "python-core",
    "lib-old" ) # package

    m.addPackage( 0, "python-tkinter", "Python Tcl/Tk Bindings", "python-core",
    "lib-dynload/_tkinter.so lib-tk" ) # package

    m.addPackage( 0, "python-math", "Python Math Support", "python-core",
    "lib-dynload/cmath.so lib-dynload/math.so lib-dynload/_random.so random.* sets.*" )

    m.addPackage( 0, "python-mime", "Python MIME Handling APIs", "python-core python-io",
    "mimetools.* uu.* quopri.* rfc822.*" )

    m.addPackage( 0, "python-mmap", "Python Memory-Mapped-File Support", "python-core python-io",
    "lib-dynload/mmap.so " )

    m.addPackage( 0, "python-unixadmin", "Python Unix Administration Support", "python-core",
    "lib-dynload/nis.so lib-dynload/grp.so lib-dynload/pwd.so getpass.*" )

    m.addPackage( 0, "python-netclient", "Python Internet Protocol Clients", "python-core python-crypt python-datetime python-io python-lang python-logging python-mime",
    "*Cookie*.* " +
    "base64.* cookielib.* ftplib.* gopherlib.* hmac.* httplib.* mimetypes.* nntplib.* poplib.* smtplib.* telnetlib.* urllib.* urllib2.* urlparse.*" )

    m.addPackage( 0, "python-netserver", "Python Internet Protocol Servers", "python-core python-netclient",
    "cgi.* BaseHTTPServer.* SimpleHTTPServer.* SocketServer.*" )

    m.addPackage( 0, "python-pickle", "Python Persistence Support", "python-core python-codecs python-io python-re",
    "pickle.* shelve.* lib-dynload/cPickle.so" )

    m.addPackage( 0, "python-pprint", "Python Pretty-Print Support", "python-core",
    "pprint.*" )

    m.addPackage( 0, "python-profile", "Python Basic Profiling Support", "python-core",
    "profile.* pstats.*" )

    m.addPackage( 0, "python-re", "Python Regular Expression APIs", "python-core",
    "re.* sre.* sre_compile.* sre_constants* sre_parse.*" ) # _sre is builtin

    m.addPackage( 0, "python-readline", "Python Readline Support", "python-core",
    "lib-dynload/readline.so rlcompleter.*" )

    m.addPackage( 0, "python-resource", "Python Resource Control Interface", "python-core",
    "lib-dynload/resource.so" )

    m.addPackage( 0, "python-shell", "Python Shell-Like Functionality", "python-core python-re",
    "cmd.* commands.* dircache.* fnmatch.* glob.* popen2.* shutil.*" )

    m.addPackage( 0, "python-robotparser", "Python robots.txt parser", "python-core python-netclient",
    "robotparser.*")

    m.addPackage( 0, "python-subprocess", "Python Subprocess Support", "python-core python-io python-re python-fcntl python-pickle",
    "subprocess.*" )

    m.addPackage( 2, "python-sqlite3", "Python Sqlite3 Database Support", "python-core python-datetime python-lang python-crypt python-io python-threading python-zlib",
    "lib-dynload/_sqlite3.so sqlite3/dbapi2.* sqlite3/__init__.*" )

    m.addPackage( 0, "python-sqlite3-tests", "Python Sqlite3 Database Support Tests", "python-core python-sqlite3",
    "sqlite3/test" )

    m.addPackage( 0, "python-stringold", "Python String APIs [deprecated]", "python-core python-re",
    "lib-dynload/strop.so string.*" )

    m.addPackage( 0, "python-syslog", "Python's Syslog Interface", "python-core",
    "lib-dynload/syslog.so" )

    m.addPackage( 0, "python-terminal", "Python Terminal Controlling Support", "python-core python-io",
    "pty.* tty.*" )

    m.addPackage( 0, "python-tests", "Python Tests", "python-core",
    "test" ) # package

    m.addPackage( 0, "python-threading", "Python Threading & Synchronization Support", "python-core python-lang",
    "_threading_local.* dummy_thread.* dummy_threading.* mutex.* threading.* Queue.*" )

    m.addPackage( 0, "python-unittest", "Python Unit Testing Framework", "python-core python-stringold python-lang",
    "unittest.*" )

    m.addPackage( 1, "python-xml", "Python basic XML support.", "python-core python-re",
    "lib-dynload/pyexpat.so xml xmllib.*" ) # package

    m.addPackage( 0, "python-xmlrpc", "Python XMLRPC Support", "python-core python-xml python-netserver python-lang",
    "xmlrpclib.* SimpleXMLRPCServer.*" )

    m.addPackage( 0, "python-zlib", "Python zlib Support.", "python-core",
    "lib-dynload/zlib.so" )

    m.addPackage( 0, "python-mailbox", "Python Mailbox Format Support", "python-core python-mime",
    "mailbox.*" )

    m.make()
<|endoftext|>


<commit_before># coding=utf-8
# Copyright 2020 The TensorFlow Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""SCAN tasks with various different splits."""

import json
import os
import tensorflow.compat.v2 as tf
import tensorflow_datasets.public_api as tfds

_CITATION = """
@inproceedings{Lake2018GeneralizationWS,
  title={Generalization without Systematicity: On the Compositional Skills of
         Sequence-to-Sequence Recurrent Networks},
  author={Brenden M. Lake and Marco Baroni},
  booktitle={ICML},
  year={2018},
  url={https://arxiv.org/pdf/1711.00350.pdf},
}
@inproceedings{Keysers2020,
  title={Measuring Compositional Generalization: A Comprehensive Method on
         Realistic Data},
  author={Daniel Keysers and Nathanael Sch\"{a}rli and Nathan Scales and
          Hylke Buisman and Daniel Furrer and Sergii Kashubin and
          Nikola Momchev and Danila Sinopalnikov and Lukasz Stafiniak and
          Tibor Tihon and Dmitry Tsarkov and Xiao Wang and Marc van Zee and
          Olivier Bousquet},
  note={Additional citation for MCD splits},
  booktitle={ICLR},
  year={2020},
  url={https://arxiv.org/abs/1912.09713.pdf},
}
"""

_DESCRIPTION = """SCAN tasks with various splits.

SCAN is a set of simple language-driven navigation tasks for studying
compositional learning and zero-shot generalization.

Most splits are described at https://github.com/brendenlake/SCAN. For the MCD
splits please see https://arxiv.org/abs/1912.09713.pdf.

Basic usage:

```
data = tfds.load('scan/length')
```

More advanced example:

```
data = tfds.load(
    'scan',
    builder_kwargs=dict(
        config=tfds.text.ScanConfig(
            name='simple_p8', directory='simple_split/size_variations')))
```
"""

_DATA_URL = 'https://github.com/brendenlake/SCAN/archive/master.zip'
_MCD_SPLITS_URL = (
    'https://storage.googleapis.com/cfq_dataset/scan-splits.tar.gz')

class ScanConfig(tfds.core.BuilderConfig):
  """BuilderConfig for SCAN.

  Splits can be read in two formats:

  1) As a pair of train and test files where each file contains one example
     input and output per line.
  2) With a 'splitfile' which contains for each split the indices into the full
     (unsplit) dataset.
  """

  def __init__(self, *, name, directory=None, splitfile=None, **kwargs):
    """BuilderConfig for SCAN.

    Args:
      name: Unique name of the split.
      directory: Which subdirectory to read the data files from.
      splitfile: If set the samples are read from the original archive
        (tasks.txt) but the splits are created using this index file.
      **kwargs: keyword arguments forwarded to super.
    """
    # Version history:
    super(ScanConfig, self).__init__(
        name=name,
        version=tfds.core.Version('1.1.1'),
        description=_DESCRIPTION,
        **kwargs)
    self.splitfile = splitfile
    if 'mcd' in name:
      self.splitfile = name + '.json'
    if self.splitfile and directory is None:
      self.directory = ''
    elif directory is None:
      self.directory = name + '_split'
    else:
      self.directory = directory

_COMMANDS = 'commands'
_ACTIONS = 'actions'

class Scan(tfds.core.GeneratorBasedBuilder):
  """SCAN task / splits as proposed by Brenden M. Lake and Marco Baroni."""

  BUILDER_CONFIGS = [
      ScanConfig(name='simple'),
      ScanConfig(name='addprim_jump', directory='add_prim_split'),
      ScanConfig(name='addprim_turn_left', directory='add_prim_split'),
      ScanConfig(name='filler_num0', directory='filler_split'),
      ScanConfig(name='filler_num1', directory='filler_split'),
      ScanConfig(name='filler_num2', directory='filler_split'),
      ScanConfig(name='filler_num3', directory='filler_split'),
      ScanConfig(name='length'),
      ScanConfig(name='template_around_right', directory='template_split'),
      ScanConfig(name='template_jump_around_right', directory='template_split'),
      ScanConfig(name='template_opposite_right', directory='template_split'),
      ScanConfig(name='template_right', directory='template_split'),
      ScanConfig(name='mcd1'),
      ScanConfig(name='mcd2'),
      ScanConfig(name='mcd3'),
  ]

  def _info(self):
    return tfds.core.DatasetInfo(
        builder=self,
        description=_DESCRIPTION,
        features=tfds.features.FeaturesDict({
            _COMMANDS: tfds.features.Text(),
            _ACTIONS: tfds.features.Text(),
        }),
        supervised_keys=(_COMMANDS, _ACTIONS),
        homepage='https://github.com/brendenlake/SCAN',
        citation=_CITATION,
    )

  def _split_generators(self, dl_manager):
    """Returns SplitGenerators."""
    data_dir = dl_manager.download_and_extract(
        tfds.download.Resource(
            url=_DATA_URL,
            # Specify extract method manually as filename reported by github.com
            # misses the .zip extension so auto-detection doesn't work.
            extract_method=tfds.download.ExtractMethod.ZIP))
    data_dir = os.path.join(data_dir, 'SCAN-master',
                            self.builder_config.directory)
    split = self.builder_config.name
    splitfile = self.builder_config.splitfile
    if 'mcd' in split:
      split_dir = dl_manager.download_and_extract(_MCD_SPLITS_URL)
      split_dir = os.path.join(split_dir, 'scan-splits')
      splitfile = os.path.join(split_dir, splitfile)
    if splitfile:
      kwargs = {
          'datapath': os.path.join(data_dir, 'tasks.txt'),
          'splitpath': splitfile
      }
      train_kwargs = kwargs.copy()
      train_kwargs['splitname'] = 'train'
      test_kwargs = kwargs.copy()
      test_kwargs['splitname'] = 'test'
    else:
      train_kwargs = {
          'datapath': os.path.join(data_dir, 'tasks_train_' + split + '.txt')
      }
      test_kwargs = {
          'datapath': os.path.join(data_dir, 'tasks_test_' + split + '.txt')
      }
    return [
        tfds.core.SplitGenerator(
            name=tfds.Split.TRAIN, gen_kwargs=train_kwargs),
        tfds.core.SplitGenerator(name=tfds.Split.TEST, gen_kwargs=test_kwargs)
    ]

  def _read_examples(self, datapath):
    with tf.io.gfile.GFile(datapath) as infile:
      for i, line in enumerate(infile):
        if not line.startswith('IN: '):
          continue
        # Chop the prefix and split string between input and output
        commands, actions = line[len('IN: '):].strip().split(' OUT: ', 1)
        yield i, {_COMMANDS: commands, _ACTIONS: actions}

  def _generate_examples(self, datapath, splitpath=None, splitname=None):
    """Yields examples."""
    if splitpath:
      all_samples = list(self._read_examples(datapath))
      with tf.io.gfile.GFile(splitpath) as infile:
        split = json.load(infile)
      for idx in split[splitname + 'Idxs']:
        yield all_samples[idx]
    else:
      for example in self._read_examples(datapath):
        yield example
<commit_msg>CleanUp<commit_after># coding=utf-8
# Copyright 2020 The TensorFlow Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""SCAN tasks with various different splits."""

import json
import os
import tensorflow.compat.v2 as tf
import tensorflow_datasets.public_api as tfds

_CITATION = """
@inproceedings{Lake2018GeneralizationWS,
  title={Generalization without Systematicity: On the Compositional Skills of
         Sequence-to-Sequence Recurrent Networks},
  author={Brenden M. Lake and Marco Baroni},
  booktitle={ICML},
  year={2018},
  url={https://arxiv.org/pdf/1711.00350.pdf},
}
@inproceedings{Keysers2020,
  title={Measuring Compositional Generalization: A Comprehensive Method on
         Realistic Data},
  author={Daniel Keysers and Nathanael Sch\"{a}rli and Nathan Scales and
          Hylke Buisman and Daniel Furrer and Sergii Kashubin and
          Nikola Momchev and Danila Sinopalnikov and Lukasz Stafiniak and
          Tibor Tihon and Dmitry Tsarkov and Xiao Wang and Marc van Zee and
          Olivier Bousquet},
  note={Additional citation for MCD splits},
  booktitle={ICLR},
  year={2020},
  url={https://arxiv.org/abs/1912.09713.pdf},
}
"""

_DESCRIPTION = """SCAN tasks with various splits.

SCAN is a set of simple language-driven navigation tasks for studying
compositional learning and zero-shot generalization.

Most splits are described at https://github.com/brendenlake/SCAN. For the MCD
splits please see https://arxiv.org/abs/1912.09713.pdf.

Basic usage:

```
data = tfds.load('scan/length')
```

More advanced example:

```
data = tfds.load(
    'scan',
    builder_kwargs=dict(
        config=tfds.text.ScanConfig(
            name='simple_p8', directory='simple_split/size_variations')))
```
"""

_DATA_URL = 'https://github.com/brendenlake/SCAN/archive/master.zip'
_MCD_SPLITS_URL = (
    'https://storage.googleapis.com/cfq_dataset/scan-splits.tar.gz')

class ScanConfig(tfds.core.BuilderConfig):
  """BuilderConfig for SCAN.

  Splits can be read in two formats:

  1) As a pair of train and test files where each file contains one example
     input and output per line.
  2) With a 'splitfile' which contains for each split the indices into the full
     (unsplit) dataset.
  """

  def __init__(self, *, name, directory=None, splitfile=None, **kwargs):
    """BuilderConfig for SCAN.

    Args:
      name: Unique name of the split.
      directory: Which subdirectory to read the data files from.
      splitfile: If set the samples are read from the original archive
        (tasks.txt) but the splits are created using this index file.
      **kwargs: keyword arguments forwarded to super.
    """
    # Version history:
    super(ScanConfig, self).__init__(
        name=name,
        version=tfds.core.Version('1.1.1'),
        description=_DESCRIPTION,
        **kwargs)
    self.splitfile = splitfile
    if 'mcd' in name:
      self.splitfile = name + '.json'
    if self.splitfile and directory is None:
      self.directory = ''
    elif directory is None:
      self.directory = name + '_split'
    else:
      self.directory = directory

_COMMANDS = 'commands'
_ACTIONS = 'actions'

class Scan(tfds.core.GeneratorBasedBuilder):
  """SCAN task / splits as proposed by Brenden M. Lake and Marco Baroni."""

  BUILDER_CONFIGS = [
      ScanConfig(name='simple'),
      ScanConfig(name='addprim_jump', directory='add_prim_split'),
      ScanConfig(name='addprim_turn_left', directory='add_prim_split'),
      ScanConfig(name='filler_num0', directory='filler_split'),
      ScanConfig(name='filler_num1', directory='filler_split'),
      ScanConfig(name='filler_num2', directory='filler_split'),
      ScanConfig(name='filler_num3', directory='filler_split'),
      ScanConfig(name='length'),
      ScanConfig(name='template_around_right', directory='template_split'),
      ScanConfig(name='template_jump_around_right', directory='template_split'),
      ScanConfig(name='template_opposite_right', directory='template_split'),
      ScanConfig(name='template_right', directory='template_split'),
      ScanConfig(name='mcd1'),
      ScanConfig(name='mcd2'),
      ScanConfig(name='mcd3'),
  ]

  def _info(self):
    return tfds.core.DatasetInfo(
        builder=self,
        description=_DESCRIPTION,
        features=tfds.features.FeaturesDict({
            _COMMANDS: tfds.features.Text(),
            _ACTIONS: tfds.features.Text(),
        }),
        supervised_keys=(_COMMANDS, _ACTIONS),
        homepage='https://github.com/brendenlake/SCAN',
        citation=_CITATION,
    )

  def _split_generators(self, dl_manager):
    """Returns SplitGenerators."""
    data_dir = dl_manager.download_and_extract(_DATA_URL)
    data_dir = os.path.join(data_dir, 'SCAN-master',
                            self.builder_config.directory)
    split = self.builder_config.name
    splitfile = self.builder_config.splitfile
    if 'mcd' in split:
      split_dir = dl_manager.download_and_extract(_MCD_SPLITS_URL)
      split_dir = os.path.join(split_dir, 'scan-splits')
      splitfile = os.path.join(split_dir, splitfile)
    if splitfile:
      kwargs = {
          'datapath': os.path.join(data_dir, 'tasks.txt'),
          'splitpath': splitfile
      }
      train_kwargs = kwargs.copy()
      train_kwargs['splitname'] = 'train'
      test_kwargs = kwargs.copy()
      test_kwargs['splitname'] = 'test'
    else:
      train_kwargs = {
          'datapath': os.path.join(data_dir, 'tasks_train_' + split + '.txt')
      }
      test_kwargs = {
          'datapath': os.path.join(data_dir, 'tasks_test_' + split + '.txt')
      }
    return [
        tfds.core.SplitGenerator(
            name=tfds.Split.TRAIN, gen_kwargs=train_kwargs),
        tfds.core.SplitGenerator(name=tfds.Split.TEST, gen_kwargs=test_kwargs)
    ]

  def _read_examples(self, datapath):
    with tf.io.gfile.GFile(datapath) as infile:
      for i, line in enumerate(infile):
        if not line.startswith('IN: '):
          continue
        # Chop the prefix and split string between input and output
        commands, actions = line[len('IN: '):].strip().split(' OUT: ', 1)
        yield i, {_COMMANDS: commands, _ACTIONS: actions}

  def _generate_examples(self, datapath, splitpath=None, splitname=None):
    """Yields examples."""
    if splitpath:
      all_samples = list(self._read_examples(datapath))
      with tf.io.gfile.GFile(splitpath) as infile:
        split = json.load(infile)
      for idx in split[splitname + 'Idxs']:
        yield all_samples[idx]
    else:
      for example in self._read_examples(datapath):
        yield example
<|endoftext|>


<commit_before>from imagePro import videoFeed
from imagePro import imageDetection
from common import dateTime
import common.var as var
from messenger import notify
from threading import Thread

class remote(object):

    def __init__(self):
        self.debug = False
        self.cam = videoFeed()
        self.control = imageDetection()
        self.timer = dateTime()
        self.alert = False
        self.running = False

    def snapShot(self):
        self.cam.readCamera()
        self.cam.showFrame1()

    def setImages(self):
        image1 = self.cam.getGreyFrame1().copy()
        image2 = self.cam.getGreyFrame2().copy()
        self.control.setImage1(image1)
        self.control.setImage2(image2)

    def capture(self):
        #capture frame by fraame
        self.snapShot()
        #transfer data and calculate threshold images
        self.setImages()
        #search for motion within camera range
        self.detectMotion()
        #debug mode for program
        if debug == True:
            self.showExtra()
        #inputs from user
        self.cam.checkInput()

    def showExtra(self):
        self.control.showThresholdImage()
        self.control.showDifferenceImage()

    def closeExtra(self):
        self.cam.closeAll()

    def detectMotion(self):
        motionDetected = False
        self.control.thresholdCalc()
        motionDetected = self.control.detectMotion()
        time = self.getTime()

        #check if the time has passed the user specification
        self.alert = self.timer.isPassedTime()

        #replace with text and dropbox api later
        if motionDetected == True:
            if self.alert == True:
                #change to twilio send message
                print 'MOTION DETECTED at %s' % (time)
                self.message()

    def getTime(self):
        time = self.timer.getTime()
        return time

    def getFileDateTime(self):
        time = self.timer.getFileTime()
        return time

    def userInput(self):
        key = self.cam.getKey()
        inputChar = chr(key & 255)
        return inputChar

    def debugControl(self, debug):
        debug = not(debug)
        if debug == False:
            self.closeExtra()
        return debug

    def message(self):
        thread = Thread(target = notify.alert())
        thread.daemon = True
        thread.start()

    def start(self):
        self.running = True

    def stop(self):
        self.running = False

    def run(self):
        debug = False

        while(True):
            self.capture()
            options = self.userInput()
            #check if input matches expected inputs from computer
            #quit the program
            if options == var.QUIT:
                break
            #check if debug mode
            elif options == var.DEBUG:
                debug = self.debugControl(debug)
            #check if user messaged stop
            if self.running == False:
                break
        #releases camera from the program and stop program
        self.cam.close()

if __name__ == "__main__":
    security = remote()
    security.run()
<commit_msg>added a debug flag<commit_after>from imagePro import videoFeed
from imagePro import imageDetection
from common import dateTime
import common.var as var
from messenger import notify
from threading import Thread

class remote(object):

    def __init__(self):
        self.debug = False
        self.cam = videoFeed()
        self.control = imageDetection()
        self.timer = dateTime()
        self.alert = False
        self.running = True

    def snapShot(self):
        self.cam.readCamera()
        self.cam.showFrame1()

    def setImages(self):
        image1 = self.cam.getGreyFrame1().copy()
        image2 = self.cam.getGreyFrame2().copy()
        self.control.setImage1(image1)
        self.control.setImage2(image2)

    def capture(self):
        #capture frame by fraame
        self.snapShot()
        #transfer data and calculate threshold images
        self.setImages()
        #search for motion within camera range
        self.detectMotion()
        #debug mode for program
        if self.debug == True:
            self.showExtra()
        #inputs from user
        self.cam.checkInput()

    def showExtra(self):
        self.control.showThresholdImage()
        self.control.showDifferenceImage()

    def closeExtra(self):
        self.cam.closeAll()

    def detectMotion(self):
        motionDetected = False
        self.control.thresholdCalc()
        motionDetected = self.control.detectMotion()
        time = self.getTime()

        #check if the time has passed the user specification
        self.alert = self.timer.isPassedTime()

        #replace with text and dropbox api later
        if motionDetected == True:
            if self.alert == True:
                #change to twilio send message
                print 'MOTION DETECTED at %s' % (time)
                self.message()

    def getTime(self):
        time = self.timer.getTime()
        return time

    def getFileDateTime(self):
        time = self.timer.getFileTime()
        return time

    def userInput(self):
        key = self.cam.getKey()
        inputChar = chr(key & 255)
        return inputChar

    def debugControl(self, debug):
        debug = not(debug)
        if debug == False:
            self.closeExtra()
        return debug

    def message(self):
        thread = Thread(target = notify.alert())
        thread.daemon = True
        thread.start()

    def start(self):
        self.running = True

    def stop(self):
        self.running = False

    def run(self):

        while(True):
            self.capture()
            options = self.userInput()
            #check if input matches expected inputs from computer
            #quit the program
            if options == var.QUIT:
                break
            #check if debug mode
            elif options == var.DEBUG:
                self.debug = self.debugControl(self.debug)
            #check if user messaged stop
            if self.running == False:
                break
        #releases camera from the program and stop program
        self.cam.close()

if __name__ == "__main__":
    security = remote()
    security.run()
<|endoftext|>