Exemplo n.º 1
0
    def test_instance(self):
        Point = recordclass('Point', 'x y')
        p = Point(11, 22)
        self.assertEqual(p, Point(x=11, y=22))
        self.assertEqual(p, Point(11, y=22))
        self.assertEqual(p, Point(y=22, x=11))
        self.assertEqual(p, Point(*(11, 22)))
        self.assertEqual(p, Point(**dict(x=11, y=22)))
        self.assertRaises(TypeError, eval, 'Point(XXX=1, y=2)', locals())   # wrong keyword argument
        self.assertRaises(TypeError, eval, 'Point(x=1)', locals())          # missing keyword argument
        self.assertEqual(repr(p), 'Point(x=11, y=22)')
        #self.assertNotIn('__weakref__', dir(p))
        #print(p)
        self.assertEqual(p, Point._make([11, 22]))                          # test _make classmethod
        self.assertEqual(p.__attrs__, ('x', 'y'))                             # test __attrs__ attribute
        self.assertEqual(p._replace(x=1), Point(1, 22))                          # test _replace method
        self.assertEqual(p._asdict(), dict(x=1, y=22))                     # test _asdict method
        self.assertEqual(vars(p), p._asdict())                              # verify that vars() works

        p.x = 1
        self.assertEqual(p.x, 1)

        # verify that field string can have commas
        Point = recordclass('Point', 'x, y')
        p = Point(x=11, y=22)
        self.assertEqual(repr(p), 'Point(x=11, y=22)')

        # verify that fieldspec can be a non-string sequence
        Point = recordclass('Point', ('x', 'y'))
        p = Point(x=11, y=22)
        self.assertEqual(repr(p), 'Point(x=11, y=22)')
Exemplo n.º 2
0
 def test_hash(self):
     A = recordclass('A', 'x y', readonly=True)
     a = A(1, 2)
     hash_a = hash(a)
     #self.assertEqual(hash(a), hash(tuple(a)))
     B = recordclass('B', 'x y', hashable=True)
     b = B(1, 2)
     hash_b = hash(b)
     #self.assertEqual(hash_b, hash(tuple(b)))
     b.x = -1
     self.assertNotEqual(hash(b), hash_b)
Exemplo n.º 3
0
 def test_repr(self):
     with support.captured_stdout() as template:
         A = recordclass('A', 'x')
     self.assertEqual(repr(A(1)), 'A(x=1)')
     # repr should show the name of the subclass
     class B(A):
         pass
     self.assertEqual(repr(B(1)), 'B(x=1)')
Exemplo n.º 4
0
    def test_factory(self):
        Point = recordclass('Point', 'x y')
        self.assertEqual(Point.__name__, 'Point')
        self.assertEqual(Point.__doc__, 'Point(x, y)')
#         self.assertEqual(Point.__slots__, ())
        self.assertEqual(Point.__module__, __name__)
        self.assertEqual(Point.__attrs__, ('x', 'y'))

        self.assertRaises(ValueError, recordclass, 'abc%', 'efg ghi')       # type has non-alpha char
        self.assertRaises(ValueError, recordclass, 'class', 'efg ghi')      # type has keyword
        self.assertRaises(ValueError, recordclass, '9abc', 'efg ghi')       # type starts with digit

        self.assertRaises(ValueError, recordclass, 'abc', 'efg g%hi')       # field with non-alpha char
        self.assertRaises(ValueError, recordclass, 'abc', 'abc class')      # field has keyword
        self.assertRaises(ValueError, recordclass, 'abc', '8efg 9ghi')      # field starts with digit
        self.assertRaises(ValueError, recordclass, 'abc', '_efg ghi')       # field with leading underscore
        self.assertRaises(ValueError, recordclass, 'abc', 'efg efg ghi')    # duplicate field

        recordclass('Point0', 'x1 y2')   # Verify that numbers are allowed in names
        recordclass('_', 'a b c')        # Test leading underscores in a typename

        nt = recordclass('nt', 'the quick brown fox')                       # check unicode input
        self.assertNotIn("u'", repr(nt.__attrs__))
        nt = recordclass('nt', ('the', 'quick'))                           # check unicode input
        self.assertNotIn("u'", repr(nt.__attrs__))

        self.assertRaises(TypeError, Point._make, [11])                     # catch too few args
        self.assertRaises(TypeError, Point._make, [11, 22, 33])             # catch too many args
Exemplo n.º 5
0
 def test_name_fixer(self):
     for spec, renamed in [
         [('efg', 'g%hi'),  ('efg', '_1')],                              # field with non-alpha char
         [('abc', 'class'), ('abc', '_1')],                              # field has keyword
         [('8efg', '9ghi'), ('_0', '_1')],                               # field starts with digit
         [('abc', '_efg'), ('abc', '_1')],                               # field with leading underscore
         [('abc', 'efg', 'efg', 'ghi'), ('abc', 'efg', '_2', 'ghi')],    # duplicate field
         [('abc', '', 'x'), ('abc', '_1', 'x')],                         # fieldname is a space
     ]:
         self.assertEqual(recordclass('NT', spec, rename=True).__attrs__, renamed)
Exemplo n.º 6
0
def _make_recordclass(name, types, readonly=False, hashable=False):
    msg = "RecordClass('Name', [(f0, t0), (f1, t1), ...]); each t must be a type"
    types = [(n, _type_check(t, msg)) for n, t in types]
    rec_cls = recordclass(name, [n for n, t in types], readonly=readonly, hashable=hashable)
    rec_cls.__annotations__ = dict(types)
    try:
        rec_cls.__module__ = _sys._getframe(2).f_globals.get('__name__', '__main__')
    except (AttributeError, ValueError):
        pass
    return rec_cls
Exemplo n.º 7
0
def recordclassdef(name, fields, default=None):
    r = recordclass(name, fields)

    def newNew(_cls):
        ff = [default for f in fields]
        #print(ff)
        return _cls.__old_new(_cls, *ff)

    r.__old_new = r.__new__
    r.__new__ = newNew

    return r
Exemplo n.º 8
0
    def test_odd_sizes(self):
        Zero = recordclass('Zero', '')
        self.assertEqual(Zero(), Zero())
        self.assertEqual(Zero._make([]), Zero())
        self.assertEqual(repr(Zero()), 'Zero()')
        self.assertEqual(Zero()._asdict(), {})
        self.assertEqual(Zero().__attrs__, ())

        Dot = recordclass('Dot', 'd')
        self.assertEqual(Dot(1), Dot(1,))
        self.assertEqual(Dot._make([1]), Dot(1,))
        self.assertEqual(Dot(1).d, 1)
        self.assertEqual(repr(Dot(1)), 'Dot(d=1)')
        self.assertEqual(Dot(1)._asdict(), {'d':1})
        self.assertEqual(Dot(1)._replace(d=999), Dot(999,))
        self.assertEqual(Dot(1).__attrs__, ('d',))

        # n = 5000
        n = 254 # SyntaxError: more than 255 arguments:
        import string, random
        names = list(set(''.join([random.choice(string.ascii_letters)
                                  for j in range(10)]) for i in range(n)))
        n = len(names)
        Big = recordclass('Big', names)
        b = Big(*range(n))
        self.assertEqual(b, Big(*tuple(range(n))))
        self.assertEqual(Big._make(range(n)), Big(*tuple(range(n))))
        for pos, name in enumerate(names):
            self.assertEqual(getattr(b, name), pos)
        repr(b)                                 # make sure repr() doesn't blow-up
        d = b._asdict()
        d_expected = dict(zip(names, range(n)))
        self.assertEqual(d, d_expected)
        b2 = b._replace(**dict([(names[1], 999),(names[-5], 42)]))
        b2_expected = list(range(n))
        b2_expected[1] = 999
        b2_expected[-5] = 42
        self.assertEqual(b2, Big(*tuple(b2_expected)))
        self.assertEqual(b.__attrs__, tuple(names))
Exemplo n.º 9
0
    def test_name_conflicts(self):
        # Some names like "self", "cls", "tuple", "itemgetter", and "property"
        # failed when used as field names.  Test to make sure these now work.
        T = recordclass('T', 'itemgetter property self cls tuple')
        t = T(1, 2, 3, 4, 5)
        self.assertEqual(t, T(1,2,3,4,5))
        newt = t._replace(itemgetter=10, property=20, self=30, cls=40, tuple=50)
        self.assertEqual(newt, T(10,20,30,40,50))

        # Broader test of all interesting names in a template
        with support.captured_stdout() as template:
            T = recordclass('T', 'x')
        words = set(re.findall('[A-Za-z]+', template.getvalue()))
        words -= set(keyword.kwlist)
        words = list(words)
        if 'None' in words:
            words.remove('None')
        T = recordclass('T', words)
        # test __new__
        values = tuple(range(len(words)))
        t = T(*values)
        self.assertEqual(t, T(*values))
        t = T(**dict(zip(T.__attrs__, values)))
        self.assertEqual(t, T(*values))
        # test _make
        t = T._make(values)
        self.assertEqual(t, T(*values))
        # exercise __repr__
        repr(t)
        # test _asdict
        self.assertEqual(t._asdict(), dict(zip(T.__attrs__, values)))
        # test _replace
        t = T._make(values)
        newvalues = tuple(v*10 for v in values)
        newt = t._replace(**dict(zip(T.__attrs__, newvalues)))
        self.assertEqual(newt, T(*newvalues))
        # test __attrs__
        self.assertEqual(T.__attrs__, tuple(words))
Exemplo n.º 10
0
    def test_defaults(self):
        Point = recordclass('Point', 'x y', defaults=(10, 20))              # 2 defaults
        self.assertEqual(Point(1, 2), Point(1, 2))
        self.assertEqual(Point(1), Point(1, 20))
        self.assertEqual(Point(), Point(10, 20))

        Point = recordclass('Point', 'x y', defaults=(20,))                 # 1 default
        self.assertEqual(Point(1, 2), Point(1, 2))
        self.assertEqual(Point(1), Point(1, 20))

        Point = recordclass('Point', 'x y', defaults=())                     # 0 defaults
        self.assertEqual(Point(1, 2), Point(1, 2))
        with self.assertRaises(TypeError):
            Point(1)

        with self.assertRaises(TypeError):                                  # catch too few args
            Point()
        with self.assertRaises(TypeError):                                  # catch too many args
            Point(1, 2, 3)
        with self.assertRaises(TypeError):                                  # too many defaults
            Point = recordclass('Point', 'x y', defaults=(10, 20, 30))
        with self.assertRaises(TypeError):                                  # non-iterable defaults
            Point = recordclass('Point', 'x y', defaults=10)
        with self.assertRaises(TypeError):                                  # another non-iterable default
            Point = recordclass('Point', 'x y', defaults=False)

        Point = recordclass('Point', 'x y', defaults=None)                   # default is None
        self.assertIsNone(Point.__new__.__defaults__, None)
        self.assertEqual(Point(10, 20), Point(10, 20))
        with self.assertRaises(TypeError):                                  # catch too few args
            Point(10)

        Point = recordclass('Point', 'x y', defaults=[10, 20])               # allow non-tuple iterable
        self.assertEqual(Point.__new__.__defaults__, (10, 20))
        self.assertEqual(Point(1, 2), Point(1, 2))
        self.assertEqual(Point(1), Point(1, 20))
        self.assertEqual(Point(), Point(10, 20))

        Point = recordclass('Point', 'x y', defaults=iter([10, 20]))         # allow plain iterator
        self.assertEqual(Point.__new__.__defaults__, (10, 20))
        self.assertEqual(Point(1, 2), Point(1, 2))
        self.assertEqual(Point(1), Point(1, 20))
        self.assertEqual(Point(), Point(10, 20))
Exemplo n.º 11
0
    def test_tupleness(self):
        Point = recordclass('Point', 'x y')
        p = Point(11, 22)

        self.assertEqual(tuple(p), (11, 22))                                # coercable to a real tuple
        self.assertEqual(list(p), [11, 22])                                 # coercable to a list
        self.assertEqual(max(p), 22)                                        # iterable
        self.assertEqual(max(*p), 22)                                       # star-able
        x, y = p
        self.assertEqual(tuple(p), (x, y))                                         # unpacks like a tuple
        self.assertEqual((p[0], p[1]), (11, 22))                            # indexable like a tuple
        self.assertRaises(IndexError, p.__getitem__, 3)

        self.assertEqual(p.x, x)
        self.assertEqual(p.y, y)
        self.assertRaises(AttributeError, eval, 'p.z', locals())
Exemplo n.º 12
0
Arquivo: base.py Projeto: frnsys/brood
    def __new__(cls, name, parents, dct):
        state_vars = dct.get('state_vars', [])
        behaviors = dct.get('behaviors', [])

        dct['State'] = recordclass('{}State'.format(name), state_vars)

        # extract vars required by behaviors
        # can handle partials as well
        all_required_vars = set(sum((
            b.required_vars if not isinstance(b, partial)
            else b.func.required_vars
            for b in behaviors
        ), []))

        missing = [var for var in all_required_vars
                   if var not in state_vars]
        if missing:
            raise MissingVarError('required vars {} are missing in agent `state_vars`'.format(missing))
        return type.__new__(cls, name, parents, dct)
Exemplo n.º 13
0
#!/usr/bin/python3
import struct

from recordclass import recordclass
from pyxenoverse import BaseRecord, merge_dict

BACEntry = recordclass(
    'BACEntry',
    ['flags', 'num_sub_entries', 'unk_06', 'sub_entry_offset', 'unk_0C'])
BAC_ENTRY_SIZE = 16
BAC_ENTRY_BYTE_ORDER = 'IHHII'


class Entry(BaseRecord):
    def __init__(self, bac, index):
        super().__init__()
        self.bac = bac
        self.index = index
        self.sub_entries = []
        self.data = BACEntry(*([0] * len(BACEntry.__fields__)))
        self.flags = 0x80000000

    def read(self, f, endian):
        self.data = BACEntry(
            *struct.unpack(endian +
                           BAC_ENTRY_BYTE_ORDER, f.read(BAC_ENTRY_SIZE)))
        # self.flags = self.flags & 0xF if self.num_sub_entries > 0 else (self.flags & 0x0F) | 0x80000000

    def write(self, f, sub_entry_offset, item_offset, endian):
        self.sub_entries.sort(key=lambda n: n.type)
        self.num_sub_entries = len(self.sub_entries)
Exemplo n.º 14
0
"""Unit tests for recordclass.py."""
import unittest, doctest, operator
from recordclass import recordclass
from collections import OrderedDict
import pickle, copy
import keyword
import re
import sys

try:
    from test import support
except:
    from test import test_support as support


TestNT = recordclass('TestNT', 'x y z')    # type used for pickle tests

class RecordClassTest(unittest.TestCase):

    def test_factory(self):
        Point = recordclass('Point', 'x y')
        self.assertEqual(Point.__name__, 'Point')
        self.assertEqual(Point.__doc__, 'Point(x, y)')
#         self.assertEqual(Point.__slots__, ())
        self.assertEqual(Point.__module__, __name__)
        self.assertEqual(Point.__attrs__, ('x', 'y'))

        self.assertRaises(ValueError, recordclass, 'abc%', 'efg ghi')       # type has non-alpha char
        self.assertRaises(ValueError, recordclass, 'class', 'efg ghi')      # type has keyword
        self.assertRaises(ValueError, recordclass, '9abc', 'efg ghi')       # type starts with digit
Exemplo n.º 15
0
    # convert to lower case
    stripped = [w.lower() for w in stripped]

    # remove stop words
    stop_words = set(stopwords.words('english'))
    stop_words.add("")
    words = [w for w in stripped if not w in stop_words]

    # lemmatize
    lemma_function = WordNetLemmatizer()
    mod_tokens = [lemma_function.lemmatize(tok) for tok in words]

    return mod_tokens


posting = recordclass('posting', 'term_freq doc_id')
posting_list = recordclass('posting_list', 'doc_freq postings')


def add_tokens(iindex, tokens, doc_id):
    tf = {}
    for tok in tokens:
        if tok not in tf:
            tf[tok] = 0
        tf[tok] += 1

    for (k, term_freq) in tf.items():
        if k not in iindex:
            iindex[k] = posting_list(0, [])
        iindex[k].postings.append(posting(term_freq, doc_id))
        iindex[k].doc_freq += 1
Exemplo n.º 16
0
        right_on=('name', 'email'),
        how='left')['actor_id']
    committer = pd.merge(
        cf, af, left_on=actor_columns[2:],
        right_on=('name', 'email'),
        how='left')['actor_id']
    commit_frame.insert(3, 'author', author)
    commit_frame.insert(4, 'committer', committer)
    if drop_name_email:
        commit_frame.drop(actor_columns, axis=1, inplace=True)
    return commit_frame


PARSED_EMAIL_FIELDS = ['email', 'valid', 'name', 'domain', 'parsed_name']

ParsedEmail = recordclass('ParsedEmail', PARSED_EMAIL_FIELDS)

PARSED_NAME_FIELDS = ['name', 'name_type']

ParsedName = recordclass('ParsedName', PARSED_NAME_FIELDS)


def proper(name: ParsedName):
    return name.name_type == 'proper' or name.name_type == 'personal'


class Actor:
    name: str
    email: str
    actor_id: str
    parsed_email: ParsedEmail
Exemplo n.º 17
0
import pygame
import pygame.gfxdraw
from recordclass import recordclass
from colors import *
from shapes import *
from fonts import *

XYPoint = recordclass("XYPoint", "x y")

class Screen:
    def __init__(self, screen, scoreTracker):
        self.__screen = screen
        self.__scoreTracker = scoreTracker
        self.__headerHeight = 160
        self.__margin = 10

        self.__lastTotalArea = 0, 0, 0, 0
        self.__lastPlayerCountAreas = [(0, 0, 0, 0), (0, 0, 0, 0)]
        self.__lastPlayerScoreAreas = [(0, 0, 0, 0), (0, 0, 0, 0)]
        self.__lastPlayerTotalScoreAreas = [(0, 0, 0, 0), (0, 0, 0, 0)]

        self._register_components()
        self._prerender()

    def _register_components(self):
        self.__logo = pygame.image.load("images/logo.png").convert_alpha()
        self.__headerLbl = HEADER_FONT.render("ENTS Plinko", 1, PRIMARY_HEADER_TEXT_COLOR)

    def _prerender(self):
        self.__screen.fill(BACKGROUND_COLOR)
        sr = self.__screen.get_rect()
Exemplo n.º 18
0
class IncomeStatement:
    Revenue = recordclass('Revenue', 'salaries bonuses capital_gains')
    EbitExpenses = recordclass(
        'EbitExpenses',
        'o_and_a deductions education a_and_m discretionary debts')
    ItExpenses = recordclass('ItExpenses', 'interest taxes')
    Expenses = recordclass('Expenses', 'ebit it')

    def __init__(self, beginning, ending):
        self.beginning = beginning
        self.ending = ending

        self.revenue = self.Revenue(Decimal(0), Decimal(0), Decimal(0))
        self.expenses = self.Expenses(
            self.EbitExpenses(Decimal(0), Decimal(0), Decimal(0), Decimal(0),
                              Decimal(0), Decimal(0)),
            self.ItExpenses(Decimal(0), Decimal(0)),
        )
        self.cc_transactions = {}

    def add_paystub(self, paystub):
        pay_period = paystub.pay_period
        paystub = paystub.current
        if not is_date_between(
                date=pay_period.end, start=self.beginning, end=self.ending):
            return
        self.revenue.salaries += paystub.earnings.wages
        self.revenue.bonuses += paystub.earnings.bonuses
        self.expenses.ebit.deductions -= paystub.deductions.total
        self.expenses.it.taxes -= paystub.taxes.total

    def add_timed_liability(self, liability):
        self.expenses.ebit.o_and_a -= liability.amount_from(
            self.beginning, self.ending)

    def add_cc_statement(self, statement):
        for transaction in statement.transactions:
            if is_date_between(
                    transaction.date, self.beginning, self.ending
            ) and transaction.id not in self.cc_transactions:
                self.cc_transactions[transaction.id] = transaction
                if transaction.in_category(Automotive):
                    self.expenses.ebit.o_and_a += transaction.amount
                elif transaction.in_category(Education):
                    self.expenses.ebit.education += SemesterAccrualBasis(
                        transaction.amount,
                        transaction.date).accrued(self.beginning, self.ending)
                elif transaction.in_category(Entertainment):
                    self.expenses.ebit.discretionary += transaction.amount
                elif transaction.in_category(Food):
                    self.expenses.ebit.o_and_a += transaction.amount
                elif transaction.in_category(Luxuries):
                    self.expenses.ebit.discretionary += transaction.amount
                elif transaction.in_category(Medical):
                    self.expenses.ebit.o_and_a += transaction.amount
                elif transaction.in_category(Utilities):
                    self.expenses.ebit.o_and_a += transaction.amount
                else:
                    self.expenses.ebit.discretionary += transaction.amount

    @property
    def revenue_total(self):
        return sum([source for source in self.revenue])

    @property
    def ebit_expenses(self):
        return sum([source for source in self.expenses.ebit])

    @property
    def it_expenses(self):
        return sum([source for source in self.expenses.it])

    @property
    def expenses_total(self):
        return sum([self.ebit_expenses, self.it_expenses])

    @property
    def operating_income(self):
        return self.revenue_total + self.ebit_expenses  # expenses are negative, so really revenue - expenses

    @property
    def net_income(self):
        return self.revenue_total + self.expenses_total  # expenses are negative, so really revenue - expenses

    def to_table(self):
        tables = [
            [
                ['Income Statement', 'for period'],
                ['Beginning', self.beginning],
                ['Ending', self.ending],
            ],
            [['Revenue']],
            [
                ['Salaries', self.revenue.salaries],
                ['Bonuses', self.revenue.bonuses],
                ['Realized Capital Gains', self.revenue.capital_gains],
                ['Total', self.revenue_total],
            ],
            [['Expenses']],
            [
                ['Operating and Administrative', self.expenses.ebit.o_and_a],
                ['Deductions', self.expenses.ebit.deductions],
                ['Education', self.expenses.ebit.education],
                ['Acquisition and Maintenence', self.expenses.ebit.a_and_m],
                ['Discretionary', self.expenses.ebit.discretionary],
                ['Creditor Obligations', self.expenses.ebit.debts],
                ['Total', self.ebit_expenses],
            ],
            [['Operating Income (EBIT)', self.operating_income]],
            [['Interest Expense', self.expenses.it.interest],
             ['Income Tax Expense', self.expenses.it.taxes],
             ['Total', self.it_expenses]],
            [['Net Income', self.net_income]],
        ]

        formatted_tables = ''
        for table in tables:
            formatted_tables += tabulate(table,
                                         tablefmt='fancy_grid',
                                         floatfmt='.2f',
                                         headers='firstrow') + '\n\n'
        return formatted_tables
Exemplo n.º 19
0
from codemend import BackupHandler, relative_path

from codemend.models.baseline2 import Baseline, Context, RandomBaseline, SuggestItem
from codemend.models.element import ElementNormalizer
from codemend.models.element_extract import extract_varmap_elems
from codemend.models.word2vec_baseline2 import Word2vecBaseline
from codemend.models.bimodal_baseline2 import BiModalBaseline
from codemend.models.bimodal2 import BiModal  # to make pickler happy

ResultLogEntry = recordclass('ResultLogEntry',
                              ['code_sample_idx',
                               'query',
                               'baseline',
                               'candidate',
                               'rank',
                               'is_gt',  # 1 or 0
                               'score',
                               'answer_rank',
                               'answer',
                              ])

class EvalBrain:
  def __init__(self):
    self.expected_set_cache = {}

    self.queries = []
    # load the ground-truth file
    with open('eval-gt-2.csv', 'rb') as csv_file:
      reader = csv.reader(csv_file)
      # columns:
Exemplo n.º 20
0
import argparse
import time

from recordclass import recordclass

import pdb

import numpy

import obj_reader
import texture
import seam_erasure
from util import to_uint8
from lib import weight_data

InputTextureFile = recordclass("InputTextureFile",
                               ["name", "depth", "isFloat", "isDataFile"])


def create_parser():
    """ Creates an ArgumentParser for this command line tool. """
    parser = argparse.ArgumentParser(
        description="Erase texture seams to " +
        "prevent visible seams or tearing in various texture maps (color, " +
        "normal, displacement, ambient occlusion, etc.)",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        usage="%(prog)s path/to/input_model path/to/input_texture [-h] " +
        "[-o path/to/output_texture] [-g] [--sv {none,texture,lerp}] [-d]")
    parser.add_argument("in_mesh",
                        metavar="path/to/input_model",
                        help="Path to input mesh file.")
    parser.add_argument(
from collections import defaultdict
from recordclass import recordclass
from src.common import storage, Ptw, Bar, extract_sublist
from src.common.math import percentage, percentage_str

# stores number of cases 
# key was/was not found in storage when command was executed
CmdKeyCases = recordclass('CmdFound', 'key_existed, key_null')

class CommandPercentage:
    """Computes number of command occurrences 
    and their percentage from total lines."""
    def __init__(self):
        # {cmd: CmdKeyCases}
        self.cases = defaultdict(lambda: CmdKeyCases(0, 0))
        self.hashloads = 0
        self.wtab = None
    def comp_ALRS(self, line):
        if storage[line.key]:
            self.cases[line.cmd].key_existed += 1
        else:
            self.cases[line.cmd].key_null += 1
    def comp_H(self, line):
        self.hashloads += 1
    def _create_wtab(self):
        if self.wtab is not None:
            return
        total = self.hashloads
        for key in self.cases:
            total += self.cases[key].key_existed + self.cases[key].key_null
        # sortby ocs, max first, center first column, rest align to right
Exemplo n.º 22
0
import torch as t
from recordclass import recordclass

tensor_cache = {}

Vector = recordclass('Vector', 'x y')


def manhattan(a, b):
    return abs(a.x - b.x) + abs(a.y - b.y)


def to_one_hot(index, n_classes):
    return tensor_cache['{}_{}'.format(index, n_classes)]


for n_classes in range(1, 20):
    for i in range(n_classes):
        onehot = t.zeros(n_classes)
        onehot.scatter_(0, t.tensor(i), 1)
        tensor_cache['{}_{}'.format(i, n_classes)] = onehot
Exemplo n.º 23
0
from recordclass import recordclass

from pyxenoverse.bac.types import BaseType

BACTransparencyEffect = recordclass('BACTransparencyEffect', [
    'start_time', 'duration', 'u_04', 'character_type', 'transparency_flags',
    'transparency_flags2', 'dilution', 'u_0e', 'u_10', 'red', 'green', 'blue',
    'f_20', 'f_24', 'f_28', 'f_2c', 'f_30', 'f_34', 'f_38', 'f_3c'
])


# Type 23
class TransparencyEffect(BaseType):
    type = 23
    bac_record = BACTransparencyEffect
    byte_order = 'HHHHHHHHIfffffffffff'
    size = 64

    def __init__(self, index):
        super().__init__(index)
        self.color = [0, 0, 0]

    def read(self, f, endian, _):
        super().read(f, endian, _)
        self.red = int(self.red * 255.0)
        self.green = int(self.green * 255.0)
        self.blue = int(self.blue * 255.0)
        self.color = [self.red, self.green, self.blue]

    def write(self, f, endian):
        self.red, self.green, self.blue = self.color
Exemplo n.º 24
0
""" orderbook as module contains the configuration parameters and the class Orderbook
that expose the functional of adding,updating and cancelling and order and retrieving
the best of ask and bid orders"""

from collections import defaultdict
from recordclass import recordclass

DEC = 5
ASK = "S"
BID = "B"
CANCEL = "c"
UPDATE = "u"
ADD = "a"

Order = recordclass(
    'Order', ['order_id', 'timestamp', 'ticker', 'side', 'price', 'size'])


class OrderBook:
    """ OrderBook its a data structure that keep track of the orders inserted on multiple
    tickers and on both bid and ask side of the book"""
    def __init__(self):
        self.orders_dict = {}
        self.max_bid = defaultdict(int)
        self.min_ask = defaultdict(int)

    def _cancel(self, order_id: str):
        """ cancel keeps memory of side,price,and ticker, delete the order and
        search for a new min or max in the only case the order was on its side frontier"""
        order_tmp = self.orders_dict[order_id]
        del self.orders_dict[order_id]
Exemplo n.º 25
0
# Liar's Dice game bot
# Matthew Kroesche

import discord
import recordclass
import random
import asyncio

from .game import Game

Player = recordclass.recordclass('Player', 'user dice bid passed')
# user: the discord.User controlling this player.
# dice: the list of die rolls of this player
# bid: either None or a 2-tuple (number, value)
# passed: boolean indicating whether the player has passed this round

PASS_NONE = 0  # Passing not allowed
PASS_TWO = 1  # Passing allowed if there are exactly two dice showing different values
PASS_UNIQUE = 2  # Passing allowed if no two dice show the same value
PASS_SKIP = 4  # Passing only allowed if the player before you didn't also pass

SPOT_NONE = 0  # Spot not allowed
SPOT_NORMAL = 1  # Spot allowed
SPOT_REWARD = 2  # A correct spot call gives you a die back (or takes one away in backwards games)
SPOT_PENALIZE = 4  # A correct spot call causes everyone else to lose a die (or gain one in backward games)

# Default settings for pass and spot
PASS_DEFAULT = PASS_UNIQUE
SPOT_DEFAULT = SPOT_PENALIZE

RESULT_DELAY = 10  # Number of seconds before dice messages are deleted
Exemplo n.º 26
0
import random
import subprocess
import signal
import os
import math
import random
from time import sleep
from tf.transformations import euler_from_quaternion
#from collections import namedtuple
from recordclass import recordclass

position = None
lidar = None
collision = False

debug_info = recordclass('extra_info', "dist collisions goal".split())


def update_position(data):
    global position
    position = (data.pose.pose.position.x, data.pose.pose.position.y)


def update_lidar(data):
    global lidar
    lidar = data.ranges


def set_collision(data):
    global collision
    #print("Collided")

class WaterDetectionStatus(Enum):
    UNKNOWN_ERROR = -1
    MEASUREMENT_VALID = 1
    INVALID_DATA = 2
    TOO_CLOUDY = 3
    SH_REQUEST_ERROR = 4
    SH_NO_DATA = 5
    SH_NO_CLOUD_DATA = 6
    INVALID_POLYGON = 7


Measurement = recordclass('SurfaceWaterLevelMeasurement', [
    'BLUEDOT_WB_ID', 'BLUEDOT_MEAS_DATE', 'SAT_IMAGE_DATE', 'SENSOR_TYPE',
    'MEAS_STATUS', 'MEAS_ALG_VER', 'CLOUD_COVERAGE', 'SURF_WATER_LEVEL',
    'CC_ORIG', 'CC_CLEAN', 'ALG_STATUS', 'GEOMETRY', 'S3_IMAGE_URL'
])


def get_new_measurement_entry(dam_id, date, sensor, version):
    return Measurement(BLUEDOT_WB_ID=dam_id,
                       BLUEDOT_MEAS_DATE=datetime.today().strftime('%Y-%m-%d'),
                       SAT_IMAGE_DATE=date.strftime('%Y-%m-%d'),
                       SENSOR_TYPE=sensor.value,
                       MEAS_STATUS=WaterDetectionStatus.UNKNOWN_ERROR.value,
                       MEAS_ALG_VER=version,
                       CLOUD_COVERAGE=1.0,
                       SURF_WATER_LEVEL=0.0,
                       CC_ORIG=0,
                       CC_CLEAN=0,
Exemplo n.º 28
0
* Author(s): Melissa LeBlanc-Williams

"""

from recordclass import recordclass
from PIL import Image
from displayio.bitmap import Bitmap
from displayio.colorconverter import ColorConverter
from displayio.ondiskbitmap import OnDiskBitmap
from displayio.shape import Shape
from displayio.palette import Palette

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"

Rectangle = recordclass("Rectangle", "x1 y1 x2 y2")
Transform = recordclass("Transform",
                        "x y dx dy scale transpose_xy mirror_x mirror_y")


# pylint: disable=too-many-instance-attributes
class TileGrid:
    """Position a grid of tiles sourced from a bitmap and pixel_shader combination. Multiple
    grids can share bitmaps and pixel shaders.

    A single tile grid is also known as a Sprite.
    """
    def __init__(self,
                 bitmap,
                 *,
                 pixel_shader,
Exemplo n.º 29
0
replays = []

for name in files:
    replay = osr.read_file(name, flip)
    replays.append(replay)

replays.sort()

n = len(replays)
for r in replays:
    print('%2d. %15s - %d' % (n, r.player, r.score))
    n -= 1
print('read %d replays' % len(replays))

State = recordclass('State', 'replay color x y z trail')
states = []

for replay in replays:
    # we don't need anything else on the replay object so remove the references
    replay = replay.replay
    color = WHITE if len(replays) == 1 else pick_color()
    states.append(State(replay, color, 0, 0, 0, deque()))

del replays

HEIGHT = 768
WIDTH = 1366
KEYSIZE = min((WIDTH-1024)/5, HEIGHT / len(states))

def scale(x, y):
Exemplo n.º 30
0
class Interface(Frame):
    def __init__(self, title="Window", width=300, height=200):
        # init gui #
        # create main window
        self._master = Tk()
        # set its size
        self._master.geometry(str(width) + "x" + str(height))
        # set its title
        self._master.title(title)
        # init
        Frame.__init__(self, self._master)


# Selected_Point
SelectedPoint = recordclass('SelectedPoint', 'index point distance_from_click')
# Primitive
Primitive = recordclass('Primitive', 'num_primitive num_frame')


class Window(Interface):

    # window size
    _width = 1920
    _width_button = 8
    _height = 1080
    _height_button = 2

    def __init__(self, datasets=None):
        # check
        if not isinstance(datasets, dict):
Exemplo n.º 31
0
    with CsvWriter(obj.directory, DocumentSimilarity) as out:
        distances = squareform(pdist(theta.T, 'cosine'))
        out << (dict(a_id=i,
                     b_id=sim_i,
                     similarity=1 - row[sim_i])
                for i, row in enumerate(distances)
                for sim_i in row.argsort()[:31]  # first 30 similar docs
                if sim_i != i)

    with CsvWriter(obj.directory, TopicSimilarity) as out:
        distances = squareform(pdist(phi.T, 'cosine'))
        out << (dict(a_id=topic_id(1, i),
                     b_id=topic_id(1, sim_i),
                     similarity=1 - row[sim_i])
                for i, row in enumerate(distances)
                for sim_i in row.argsort()[:]
                if sim_i != i)

    with CsvWriter(obj.directory, TermSimilarity) as out:
        distances = squareform(pdist(phi, 'cosine'))
        out << (dict(a_modality_id=1, a_id=i,
                     b_modality_id=1, b_id=sim_i,
                     similarity=1 - row[sim_i])
                for i, row in enumerate(distances)
                for sim_i in row.argsort()[:21]  # first 20 similar terms
                if sim_i != i)


if __name__ == '__main__':
    cli(obj=recordclass('Obj', 'directory')(directory=None))
from collections import defaultdict
import matplotlib.pyplot as plt
from recordclass import recordclass
from src.common import COMMANDS

# total_ocs - stores current total occurrences of command
# samples - list of samples of total_ocs taken at times of sampling
#   these are used as y-values on a graph
CmdSample = recordclass('CmdSample', 'total_ocs, samples')


class CommandTimeline:
    """Computes changes in number of occurrences of commands over time."""
    def __init__(self, lines_sample=500):
        """Args:
            lines_sample (int) opt: Splits graph into sections
                each containing this number of lines.
        """
        self.lines_sample = max([lines_sample, 1])
        self.next_sample = 1
        # {cmd: list of occurrences at the time}
        self.y_cmd = defaultdict(lambda: CmdSample(0, []))
        self.x_time = []

    def _create_sample(self, line_num):
        for cmd in COMMANDS:
            csample = self.y_cmd[cmd]
            csample.samples.append(csample.total_ocs)
        self.x_time.append(line_num)

    def comp_all_timeline(self, line):
#!/usr/bin/env python

import random
import time
import psutil
import os

from recordclass import recordclass

def memory_usage_psutil():
    # return the memory usage in percentage like top
    process = psutil.Process(os.getpid())
    mem = process.memory_percent()
    return mem

X = recordclass('x', ['a','b','c'])

while True:
  a = X(str(random.randint(1000000,10000000)), random.randint(1,1000),
            'a' * random.randint(1,1024))
  del a
  time.sleep(.01)
  print(memory_usage_psutil())
  
Exemplo n.º 34
0
"""
import sys
import re
from typing import Dict, Any
from recordclass import recordclass

_items = ['institution',
          'title',
          'award',
          'hegis',
          'first_registration_date',
          'last_registration_action',
          'tap', 'apts', 'vvta',
          'certificate_license',
          'accreditation']
_variant_info = recordclass('Variant_Info', _items, mapping=True)


class Program(object):
  """ For each program registered with NYS Department of Education, collect information about the
      program scraped from the DoE website.

      Some programs appear more than once, so a class list of programs instances prevents duplicate
      entries.

      A single program can have multiple variants, which differ in title, institution, award, and/or
      hegis. Emprically, no two variants share the same {award, hegis, and institution} combination,
      so that tuple is used as the key for a dictionary of per-variant values. All variants of a
      program share a single program code and unit code.

      Variant details are maintained as a recordclass so the values can be updated as new records
Exemplo n.º 35
0
import numpy
from sklearn.cluster import KMeans
from recordclass import recordclass
import pickle

Log = recordclass('Log', ['coin', 'value_ask', 'value_bid'])

# classes

class SuperQueue:
	def __init__(self, dim):
		self.items = []
		self.dim = dim
	
	def put(self, el):
		if len(self.items) == self.dim:
			self.items = self.items[1 :]
		self.items.append(el)
		
	def clear(self):
		del self.items[:]
		self.dim = 0
		
	def changeSize(self, dim):
		self.dim = dim
		
	def show(self):
		print(self.items)
		
# functions
Exemplo n.º 36
0
import json
import pickle
import string
import nltk
import re
import math
from collections import defaultdict
from bs4 import BeautifulSoup
from bs4.element import Comment
from recordclass import recordclass

# creates the Posting class
# identical to namedtuple, except it is mutable. This is useful for the rescoring of the
# tf-idf scores of old postings
Posting = recordclass("Posting", ["docId", "tf", "tfidf"])


class indexCreator:
    def __init__(self, webFilesPath, indexPath):
        self.webFilesPath = webFilesPath
        self.indexPath = indexPath
        self.invertedIndex = defaultdict(list)

    def createIndex(self):
        self._traverseDirectory()
        self._calculateScores()
        self._serializeIndex()

    def _openJsonFile(self):
        """
Exemplo n.º 37
0
    ArticleSubjectTerms         -> subjectTerms          : 10
    JournalTitle                -> pubtitle              : 5
    JournalDate                 -> pubdate               : 4
    # JournalCountry              -> publisher             : 20
    JournalIssue                -> issue                 : 7
    JournalVolume               -> volume                : 8
    JournalYear                 -> year                  : 6
"""
ColumnMap = {
    "ArticleTitle":                 0,
    "ArticleAuthors":               2,
    # "ArticleCorrespondenceAuthor":  7,
    "ArticleAbstract":              1,
    "ArticleSubjectTerms":         10,
    "JournalTitle":                5,
    "JournalDate":                 4,
    # "JournalCountry":              20,
    "JournalIssue":                7,
    "JournalVolume":               8,
    "JournalYear":                 6
}


"""
    The JournalEntry named tuple holds a single publication record.
"""
JournalEntry = recordclass("JournalEntry", "ArticleTitle ArticleAuthors \
                                           ArticleAbstract ArticleSubjectTerms JournalTitle \
                                           JournalDate JournalIssue JournalVolume \
                                           JournalYear")
Exemplo n.º 38
0
class OmringaGameState(GameState):
    class State(Enum):
        BET = 0
        NATURE = 1
        PLACE = 2

    Change = recordclass('Change', 'player state bets passes chosen_player pass_ position index')

    def __init__(self):
        super().__init__(2, (7, 7), 3, 0)
        self.min_bet = 0
        self.max_bet = 9
        self.group_penalty = -5
        self.state = OmringaGameState.State.BET
        self.bets = (None, None)
        self.passes = 0
        self.chosen_player = None
        self.board_ = np.zeros(self.board_shape, np.float32)
        self.empty_positions = [
            (y, x)
            for y in range(self.board_shape[0])
            for x in range(self.board_shape[1])]
        self.changes = []

    def move_count(self):
        if self.state == OmringaGameState.State.BET:
            return self.max_bet - self.min_bet
        elif self.state == OmringaGameState.State.NATURE:
            return 2
        else:
            if self.passes == 2:
                return 0
            else:
                return len(self.empty_positions) + 1

    def apply_move(self, move):
        change = OmringaGameState.Change(
            player=self.player,
            state=self.state,
            bets=self.bets,
            passes=self.passes,
            chosen_player=self.chosen_player,
            pass_=None,
            position=None,
            index=None)
        self.changes.append(change)
        if self.state == OmringaGameState.State.BET:
            self.bets = (
                move if self.player == 0 else self.bets[0],
                move if self.player == 1 else self.bets[1])
            if self.bets[self.player ^ 1] is None:
                self.state = OmringaGameState.State.BET
                self.player ^= 1
            else:
                if self.bets[0] == self.bets[1]:
                    self.state = OmringaGameState.State.NATURE
                    self.player = -1
                else:
                    self.state = OmringaGameState.State.PLACE
                    self.player = 1 if self.bets[0] < self.bets[1] else 0
        elif self.state == OmringaGameState.State.NATURE:
            self.state = OmringaGameState.State.PLACE
            self.player = move
            self.chosen_player = move
        else:
            if move == len(self.empty_positions):
                change.pass_ = True
                self.state = OmringaGameState.State.NATURE
                self.player ^= 1
                self.passes += 1
            else:
                change.pass_ = False
                change.position = self.empty_positions[move]
                change.index = move

                self.board_[change.position[0], change.position[1]] = self.player + 1

                self.empty_positions[move], self.empty_positions[-1] = \
                    self.empty_positions[-1], self.empty_positions[move]
                self.empty_positions.pop()

    def undo_move(self):
        change = self.changes.pop()
        self.player = change.player
        self.state = change.state
        self.bets = change.bets
        self.passes = change.passes
        self.chosen_player = change.chosen_player

        if self.state == OmringaGameState.State.PLACE:
            if not change.pass_:
                self.empty_positions.append(change.position)
                self.empty_positions[change.index], self.empty_positions[-1] = \
                    self.empty_positions[-1], self.empty_positions[change.index]

                self.board_[change.position[0], change.position[1]] = 0

    def board(self):
        return np.copy(self.board_)

    def info(self):
        return np.array([
            self.bets[0] if self.bets[0] is not None else -1,
            self.bets[1] if self.bets[1] is not None else -1,
            self.chosen_player if self.chosen_player is not None else -1], np.float32)

    def group_count(self, id):
        result = 0

        dys = [-1, 0, 0, 1]
        dxs = [0, -1, 1, 0]
        visited = np.zeros(self.board_shape, np.bool)

        for y in range(self.board_shape[0]):
            for x in range(self.board_shape[1]):
                if not visited[y][x] and self.board_[y][x] == id:
                    visited[y][x] = True
                    result += 1

                    stack = [(y, x)]
                    while stack:
                        p = stack.pop()

                        for dx, dy in zip(dys, dxs):
                            dp = (p[0] + dy, p[1] + dx)
                            if 0 <= dp[0] < self.board_shape[0] and \
                                    0 <= dp[1] < self.board_shape[1] and \
                                    not visited[dp] and self.board_[dp] == id:
                                visited[dp] = True
                                stack.append(dp)

        return result

    def payoff(self):
        result = np.array([
            self.group_count(1) * self.group_penalty + (self.board_ == 1).sum(),
            self.group_count(2) * self.group_penalty + (self.board_ == 2).sum()], np.float32)

        if self.bets[0] < self.bets[1]:
            result[0] += self.bets[0] + 0.5
        elif self.bets[0] > self.bets[1]:
            result[1] += self.bets[1] + 0.5
        else:
            result[self.chosen_player ^ 1] += self.bets[self.chosen_player ^ 1] + 0.5

        return result
Exemplo n.º 39
0
def summarize_poly_aaa_variants(variants):

    columns = [
        'snp_id', 'gene', 'poly_aaa_increase', 'poly_aaa_decrease',
        'poly_aaa_change', 'chr', 'start', 'end', 'ref', 'alt', 'transcript',
        'cds_start', 'cds_end'
    ]

    Record = recordclass('RecordPolyA', columns)

    aaa_records = []
    aaa_variants = set()
    up_variants = {}
    down_variants = {}
    all_variants_ids = []
    variants_sources = Counter()
    transcripts = set()
    new_poly_a = 0
    in_poly_a = 0

    for variant in all_poly_a_variants(variants, preserve_sources=True):

        all_variants_ids.extend(variant.snp_id.split(','))

        new = False
        in_a = False

        for transcript in variant.affected_transcripts:

            if not transcript.poly_aaa:
                continue

            for alt, aaa_data in transcript.poly_aaa.items():

                record = Record(
                    variant.snp_id,
                    None,  #variant.ensembl_gene_stable_id    # TODO
                    aaa_data.increased,
                    aaa_data.decreased,
                    aaa_data.change,
                    variant.chr_name,
                    variant.chr_start,
                    variant.chr_end,
                    variant.ref,
                    alt,
                    transcript.ensembl_id,
                    transcript.cds_start,
                    transcript.cds_end)

                if not aaa_data.has and aaa_data.will_have:
                    new = True
                if aaa_data.has:
                    in_a = True

                if aaa_data.increased:
                    up_variants[variant] = True
                if aaa_data.decreased:
                    down_variants[variant] = True
                transcripts.add(transcript.ensembl_id)

                aaa_records.append(record)

            aaa_variants.add(variant)

        if new:
            new_poly_a += 1
        if in_a:
            in_poly_a += 1

        for source in set(variant.source.split(',')):
            variants_sources[source] += 1

    report('poly aaa increase and decrease by variants', aaa_records, columns)
    report('poly aaa sources', variants_sources.items(), ['source', 'count'])

    report('all ids', all_variants_ids)

    print('Variants creating new poly(A) tracks: %s' % new_poly_a)
    print('Variants in existing poly(A) tracks: %s' % in_poly_a)
    print('Affected transcripts: %s' % len(transcripts))
    print('Down variants: %s' % len(down_variants))
    print('Up variants: %s' % len(up_variants))
    print('Unique variants: %s' % len(aaa_variants))
    print('Variants identifiers: %s' %
          sum(v.snp_id.count(',') + 1 for v in aaa_variants))
    print(variants_sources)
Exemplo n.º 40
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import re
from recordclass import recordclass
from math import sqrt
import itertools as it

Part = recordclass('Attrs', 'p v a')
V = recordclass('Vector', 'x y z')
Eq = recordclass('Equation', 'a b c')
parser = re.compile(r'p=<(-?[0-9]+),(-?[0-9]+),(-?[0-9]+)>, '
                    r'v=<(-?[0-9]+),(-?[0-9]+),(-?[0-9]+)>, '
                    r'a=<(-?[0-9]+),(-?[0-9]+),(-?[0-9]+)')


def equation(p1, p2, d):
    a = .5 * (p1.a[d] - p2.a[d])
    return Eq(a, p1.v[d] - p2.v[d] + a, p1.p[d] - p2.p[d])


def md(v):
    return sum([abs(v[d]) for d in range(3)])


def collide(p1, p2):
    r = []
    for d in range(3):
        if p1.a[d] == p2.a[d]:
            if p1.v[d] == p2.v[d]:
                if p1.p[d] != p2.p[d]:
Exemplo n.º 41
0
import skimage.morphology
from scipy import ndimage


EPSILON = 0.00000000001

''' These data structures are used in the search function
'''

Circle = collections.namedtuple("Circle", ["radius", "center", "angle", "deflection"])
Path  = collections.namedtuple("Path", ["states"])
# wraps a state with additional search information
SearchNode = collections.namedtuple("SearchNode", ["state", "cost", "heuristic", "parent"])
SearchNodeTree = collections.namedtuple("SearchNodeTree", ["state", "cost", "heuristic", "parent", "tree_node"])
# used for recreating the search tree
TreeNode = recordclass.recordclass("TreeNode", ["state", "children"])

class Map(object):
	""" Convenience wrapper for an occupancy grid map object.
	    Provides methods to:
	        - check distances to nearest objects
	        - check if points are permissible
	        - mark map regions as explored
	        - dilate the map
	"""
	def __init__(self, map_msg):
		self.memoize_disks = True
		self.map_msg = map_msg
		self.map_info = map_msg.info
		#  # 0: permissible, -1: unmapped, 100: blocked
		self.raw_matrix = np.array(map_msg.data).reshape((map_msg.info.height, map_msg.info.width))
Exemplo n.º 42
0
    "flip_lr input_height input_width")

ReacherConfig = collections.namedtuple(
    "ReacherConfig", "data_dir num_frames im_height im_width batch_size "
    "policy channels num_actions max_degree action_coding "
    "input_seq_len pred_seq_len n_examples num_joints angles")

BAIRConfig = collections.namedtuple(
    "BAIRConfig",
    "data_dir num_frames input_res im_height im_width channels batch_size "
    "input_seq_len pred_seq_len num_actions n_examples max_seq_length angles rescale_size"
)

TOPConfig = recordclass(
    "TOPConfig",
    "data_dir num_frames input_res im_height im_width channels batch_size "
    "input_seq_len pred_seq_len num_actions n_examples max_seq_length angles "
    "flip_lr input_height input_width rescale_size")

GridworldConfig = collections.namedtuple(
    "GridworldConfig",
    "dataset_type data_file num_frames batch_size image_size "
    "input_seq_len pred_seq_len channels max_action num_actions agent_size angles"
)

# A generic config for datasets of (unchunked) videos
VideoDatasetConfig = collections.namedtuple(
    "VideoDatasetConfig", "data_dir num_frames "
    "im_channels im_height im_width"
    "input_seq_len pred_seq_len n_examples")
Exemplo n.º 43
0
 def test_annotations(self):
     C = recordclass('C', [('x',int),('y',int)])
     self.assertEqual(C.__new__.__annotations__, {'x':int, 'y':int})
     D = recordclass('D', [('x',int),'y'])
     self.assertEqual(D.__new__.__annotations__, {'x':int})
Exemplo n.º 44
0
def get_peak_coeffs(gain, centre, sr, Q):
    A = db2a(gain / 2)
    w0 = 2 * pi * centre / sr
    cw0 = cos(w0)
    sw0 = sin(w0)
    alpha = sw0 / 2 * Q

    a0 = 1 + alpha / A

    b = [(1 + (alpha * A)) / a0, (-2 * cw0) / a0, (1 - alpha * A) / a0]
    a = [1, (-2 * cw0) / a0, (1 - alpha / A) / a0]

    return b, a

BiquadMemory = recordclass('BiquadMemory', ['z1', 'z2'])
BiquadCoefficients = recordclass(
    'BiquadCoefficients', [
        'b0', 'b1', 'b2', 'a1', 'a2'])


def biquad_step(i, bm, bc):
    out = i * bc.b0 + bm.z1
    bm.z1 = i * bc.b1 - bc.a1 * out + bm.z2
    bm.z2 = i * bc.b2 - bc.a2 * out
    return out


def biquad_cascade(i, bm, bc):
    for m, c in zip(bm, bc):
        i = biquad_step(i, m, c)
Exemplo n.º 45
0
from recordclass import recordclass
from itertools import combinations
import numpy as np

def change_velocity(a1, a2, va1, va2):
    if a1 > a2:
        return (va1 - 1, va2 + 1)
    elif a1 < a2:
        return (va1 + 1, va2 - 1)
    return va1, va2

Moon = recordclass('Moon', 'n x y z vx vy vz')

f = open("input", "r")

positions = [x.strip('\n').strip('<').strip('>').split(',') for x in f.readlines()]

moons = []
for n, p in enumerate (positions):
    x = int(p[0][2:])
    y = int(p[1][3:])
    z = int(p[2][3:])
    moons.append(Moon(n, x, y, z, 0, 0, 0))

for i in range (0, 1000):
    pairs = combinations(moons, 2)
    for p in pairs:
        m1, m2 = p
        m1.vx, m2.vx = change_velocity(m1.x, m2.x, m1.vx, m2.vx)
        m1.vy, m2.vy = change_velocity(m1.y, m2.y, m1.vy, m2.vy)
        m1.vz, m2.vz = change_velocity(m1.z, m2.z, m1.vz, m2.vz)
Exemplo n.º 46
0
from recordclass import recordclass

SandboxConfig = recordclass(
    "SandboxConfig",
    ['folder_mappers', 'networking', 'logon_script', 'virtual_gpu'])
Exemplo n.º 47
0
 def test_factory_doc_attr(self):
     Point = recordclass('Point', 'x y')
     self.assertEqual(Point.__doc__, 'Point(x, y)')
Exemplo n.º 48
0
RawDataExampleDType = [
    ('id', object),
    ('dependency_path', object),
    ('arg1', object),
    ('arg2', object),
    ('entity_types', object),
    ('trigger', object),
    ('sid', object),
    ('sentence', object),
    ('pos', object),
    ('relation', object),
]

DataExample = recordclass(
    "DataExample",
    ["id", "arg1", "arg2", "feats", "relation", "neg", "trigger"])
DataExampleDType = [('id', int), ('arg1', int, (1, )), ('arg2', int, (1, )),
                    ('feats', list), ('relation', object), ('neg', list),
                    ('trigger', str)]

DataSet = recordclass("DataSet",
                      ["train", "valid", "test"])  # list[DataExample]

AllData = recordclass("AllData", [
    'feature_extrs',
    'dataset',
    'feature_lexicon',
    'entity_lexicon',
    'relation_lexicon',
])
from recordclass import recordclass

from pyxenoverse.bac.types import BaseType

BACEffectPropertyControl = recordclass('BACEffectPropertyControl', [
    'start_time',
    'duration',
    'u_04',
    'character_type',
    'skill_id',
    'skill_type',
    'effect_id',
    'effect_duration',
    'flags',
    'u_12',
    'u_14',
    'u_16',
])


# Type 27
class EffectPropertyControl(BaseType):
    type = 27
    bac_record = BACEffectPropertyControl
    byte_order = 'HHHHHHHHHHHH'
    size = 24

    def __init__(self, index):
        super().__init__(index)
Exemplo n.º 50
0
# |   |
# |   --- <name>
# |       |
# |       --- hunk (unit name, content)
# --- bss
#     |
#     --- <name>
#         |
#         --- hunk (unit name)
# symbols
# |
# --- <name>
#     |
#     --- symbol (unit name, hunk type, hunk name, offset)

Hunk      = recordclass('Hunk', ('uname', 'content', 'refs', 'relocs'))
Symbol    = recordclass('Symbol', ('uname', 'htype', 'hname', 'offset'))
Reloc     = recordclass('Reloc', ('uname', 'htype', 'hname', 'hnum', 'offset'))
Reference = recordclass('Reference', ('sname', 'type', 'offset'))
DataBase  = recordclass('DataBase', ('hunks', 'symbols', 'map'))

db = DataBase(hunks = {'code': {}, 'data': {}, 'bss': {}}, symbols = {}, map = {})



# block types from from dos/doshunks.h
class BlockType(IntEnum):
    HUNK_UNIT	  = 999
    HUNK_NAME	  = 1000
    HUNK_CODE	  = 1001
    HUNK_DATA	  = 1002
Exemplo n.º 51
0
"""
A template for the baseline methods that participate in the evaluation of
eval2.py.

"""

from abc import ABCMeta, abstractmethod
import random
from recordclass import recordclass
import re

SuggestItem = recordclass('SuggestItem', ['elem', 'score'])

class Context:
  """
  Vehicle of all the necessary information to be included in a query for the
  model to make a prediction.

  This abstract representation is composed only of functions called, parameter
  used, return relations, etc.

  There are a corresponding preprocessor and postprocessor (e.g., matching
  model's suggested highlight with user's cursor position, etc.)

  This class itself should not contain any thing about the messy world.

  """
  def __init__(self, used_elem_ids, used_elem_objs=None, var_map=None):
    """
    SIMPLIFIED elements (string IDs) only.
from __future__ import print_function, division
import os, sys
from recordclass import recordclass

# 'extra' is for extra lines
OBJ = recordclass( 'OBJ', ['v','vt','vn','f','extra','filename'] )
FaceVertex = recordclass( 'FaceVertex', ['v','vt','vn'] )

def load_obj( filename ):
	print( "Loading:", filename )
	
	with open( filename ) as lines:

		v = []
		vt = []
		vn = []
		f = []

		extra_lines = []

		for line in lines:
			sline = line.strip().split()
			if len( sline ) == 0: continue
			if sline[0] == 'v':
				v.append( tuple( map( float, sline[1:] ) ) )
			elif sline[0] == 'vt':
				vt.append( tuple( map( float, sline[1:] ) ) )
			elif sline[0] == 'vn':
				vn.append( tuple( map( float, sline[1:] ) ) )
			elif sline[0] == 'f':
				## Pad bundle with two extra '//' and then take the first three values in between.
Exemplo n.º 53
0
  def __init__(self):
    self.expected_set_cache = {}

    self.queries = []
    # load the ground-truth file
    with open('eval-gt-2.csv', 'rb') as csv_file:
      reader = csv.reader(csv_file)
      # columns:
      # case_study_no,answer_func,answer_arg,query
      Query = recordclass('Query', next(reader))
      for query in imap(Query._make, reader):
        assert query.case_study_no.startswith('example-')
        query.case_study_no = int(query.case_study_no.replace('example-', ''))
        query.answer = query.answer.strip()
        query.query = query.query.strip()
        query.query_source = query.query_source.strip()
        self.queries.append(query)

    print 'Loading the code samples...'
    self.code_samples = []
    fnames = [relative_path('demo/code-samples/before_afters/before%d.py'%x)
              for x in [1,2,3,4,5]]
    for f in fnames:
      with open(f) as reader:
        code = reader.read().strip()
        self.code_samples.append(code)

    print 'Initializing context builder...'
    self.cb = ContextBuilder()

    print 'Initializing element normalizer...'
    self.enormer = ElementNormalizer()

    print 'Instantiating baselines...'
    self.baselines = []
    self.baselines.append(RandomBaseline(self.cb.getAllElements()))

    w2vb1 = Word2vecBaseline(
        relative_path('models/output/vectors-so-text-python-lemma.bin'),
        self.cb.getAllElementCounts(), 1, 'w2v')

    w2vb2 = Word2vecBaseline(w2vb1.model,
        self.cb.getAllElementCounts(), 1, 'w2v-heuristic', heuristic=True)

    w2vb3 = Word2vecBaseline(w2vb1.model,
        self.cb.getAllElementCounts(), 1, 'w2v-cooccur', use_coke=True)

    w2vb4 = Word2vecBaseline(w2vb1.model,
        self.cb.getAllElementCounts(), 1, 'w2v-hc', heuristic=True, use_coke=True)

    self.baselines += [w2vb1, w2vb2, w2vb3, w2vb4]

    # bimodal = BiModalBaseline('bimodal-concat-10epoch',
    #     relative_path('models/output/bi2-test-ggg.model'),
    #     relative_path('models/output/vectors-flat-mpl-0205.bin'))
    # bimodal_ids = list('denopq')
    bimodal_ids = list('d')
    for id_  in bimodal_ids:
      bimodal = BiModalBaseline('bimodal-'+id_,
          relative_path('models/output/bi2-0410-%s.model'%id_),
          w2vb1.model)
      self.baselines.append(bimodal)

    print 'Starts evaluating...'
    metric_names = ['MRR', 'P@1', 'P@5', 'P@10']
    results = np.zeros((len(self.baselines), len(metric_names)), dtype=float)
    result_log = []  # for diagnosis

    count_query = 0
    for idx, code in enumerate(self.code_samples):
      # triple-for-loop structure: {code-sample -> gt -> baseline}.
      print 'Processing code sample %d'%(idx + 1)

      current_queries = filter(lambda x: int(x.case_study_no) == int(idx + 1), self.queries)
      assert current_queries

      context = self.cb.getContext(code)

      for query in current_queries:  # "query" = "ground truth"
        count_query += 1
        assert query.answer

        for b_idx, b in enumerate(self.baselines):
          suggested_items = b.suggest(query.query, context)
          answer_rank = self.getRankOfExpectedItem(
              suggested_items, code, query.answer)

          mrr_idx = metric_names.index('MRR')
          p1_idx = metric_names.index('P@1')
          p5_idx = metric_names.index('P@5')
          p10_idx = metric_names.index('P@10')
          if answer_rank > 0:
            results[b_idx, mrr_idx] += 1. / answer_rank
            if answer_rank == 1:
              results[b_idx, p1_idx] += 1
            if answer_rank <= 5:
              results[b_idx, p5_idx] += 1
            if answer_rank <= 10:
              results[b_idx, p10_idx] += 1


          self.updateResultLog(result_log, idx + 1, query.query, b,
                               suggested_items, code, query.answer,
                               answer_rank)

    assert count_query > 0
    for metric_idx, metric in enumerate(metric_names):
      if metric == 'MRR' or metric.startswith('P@'):
        results[:, metric_idx] /= count_query

    # output
    print 'Writing outputs...'
    with open(relative_path('models/output/eval-result-0413.csv'), 'wb') as csv_file:
      writer = csv.writer(csv_file)
      writer.writerow(['Baseline'] + metric_names)
      for b_idx, b in enumerate(self.baselines):
        writer.writerow([b.__repr__()] + results[b_idx].tolist())

    with open(relative_path('models/output/eval-log-0413.csv'), 'wb') as csv_file:
      writer = csv.writer(csv_file)
      writer.writerow(ResultLogEntry._fields)
      for row in result_log:
        writer.writerow(row)

    # close resources
    print 'Closing resources'
    # whoosh_baseline.close()

    print 'Done'
from __future__ import print_function, division
import os, sys
from recordclass import recordclass

# 'extra' is for extra lines
OBJ = recordclass("OBJ", ["v", "vt", "vn", "f", "extra", "filename"])
FaceVertex = recordclass("FaceVertex", ["v", "vt", "vn"])


def load_obj(filename):
    print("Loading:", filename)

    with open(filename) as lines:

        v = []
        vt = []
        vn = []
        f = []

        extra_lines = []

        for line in lines:
            sline = line.strip().split()
            if len(sline) == 0:
                continue
            if sline[0] == "v":
                v.append(tuple(map(float, sline[1:])))
            elif sline[0] == "vt":
                vt.append(tuple(map(float, sline[1:])))
            elif sline[0] == "vn":
                vn.append(tuple(map(float, sline[1:])))
Exemplo n.º 55
0
MapStruct = collections.namedtuple('MapStruct', 'cells height width depth')
def load_map(map_path):
    map_data = open(map_path, 'rb').read()
    eb = (len(map_data) - 3) % 4
    if eb > 0:
        #print("{}: {} extra bytes".format(map_path, eb))
        # many maps seem to have an extra byte tacked on.
        # must be a bug in some editor
        pass
    return MapStruct(
        [MapRec(*rec) for rec in struct.iter_unpack('4B', map_data[3:-eb])],
        *struct.unpack('3B', map_data[:3]))

MCDRec = recordclass('MCDRec', '''origin Frame LOFT ScanG UFO_Door
        Stop_LOS No_Floor Big_Wall Gravlift Door Block_Fire Block_Smoke
        u39 TU_Walk TU_Slide TU_Fly Armor HE_Block Die_MCD Flammable Alt_MCD
        u48 T_Level P_Level u51 Light_Block Footstep Tile_Type HE_Type
        HE_Strength Smoke_Blockage Fuel Light_Source Target_Type Xcom_Base u62''')

MCDStruct = struct.Struct("<8s12sH8x12B6Bb13B")
MCDPatchMap = {
    'bigWall': 'Big_Wall',
    'TUWalk': 'TU_Walk',
    'TUSlide': 'TU_Slide',
    'TUFly': 'TU_Fly',
    'deathTile': 'Die_MCD',
    'terrainHeight': 'T_Level',
    'specialType': 'Target_Type',
    'explosive': 'HE_Strength',
    'armor': 'Armor',
    'flammability': 'Flammable',
Exemplo n.º 56
0
elem_counts = bh.load('elem_pyplot_counts_0404')
enormer = ElementNormalizer()

rtype_lookup = {}  # [elem_id] = rtype (simplified)
with open(relative_path(
    'docstring_parse/annotation/rtype_map.csv'), 'rb') as csvfile:
  reader = csv.reader(csvfile)
  for elem_id, rtype in reader:
    rtype_lookup[elem_id] = enormer.unsimplify(rtype)

adoc_lookup = {}  # [elem_id] = utter (additional doc)
with open(relative_path(
    'docstring_parse/annotation/additional_doc.csv'), 'rb') as csvfile:
  reader = csv.reader(csvfile)
  ADoc = recordclass('ADoc', next(reader))
  assert ADoc._fields == ('elem_id', 'doc')
  for adoc in imap(ADoc._make, reader):
    adoc_lookup[adoc.elem_id] = adoc.doc

aref_lookup = defaultdict(set)  # [elem_id] = [ARef] (additional reference)
with open(relative_path(
    'docstring_parse/annotation/additional_ref.csv'), 'rb') as csvfile:
  reader = csv.reader(csvfile)
  ARef = namedtuple('ARef', next(reader))
  assert ARef._fields == ('elem_id', 'ref', 'type')
  for aref in imap(ARef._make, reader):
    aref_lookup[aref.elem_id].add(aref)

# Use aref to augment fau and create faku
faku = {}  # [f@a,k] = utter
Exemplo n.º 57
0
import os
from pprint import pprint
from recordclass import recordclass
from gpt3.bleu.util import read_file, json_create, json_append_dict, query

File = recordclass("File", "file language")
Test = recordclass("Test", "src target")


def openai_prompt(test, src_examples, tar_examples, src_line):
    if len(src_examples) == 0:
        return f"Q: What is the {test.target.language} translation of {src_line}\nA:"
    else:
        few_shots = "\n\n".join([
            f"{src}\t=\t{tar}" for src, tar in zip(src_examples, tar_examples)
        ])
        return f"Translate {test.src.language} to {test.target.language}:\n{few_shots}\n\n{src_line}\t="


def openai_quote_prompt(test, src_examples, tar_examples, src_line):
    if len(src_examples) == 0:
        return f"Q: What is the {test.target.language} translation of \"{src_line}\"\nA: \""
    else:
        p = lambda src, tar: f"\"{src}\" = \"{tar}\""
        few_shots = "\n\n".join(
            [p(src, tar) for src, tar in zip(src_examples, tar_examples)])
        return (f"Translate {test.src.language} to {test.target.language}:"
                f"\n{few_shots}"
                f"\n\n\"{src_line}\" = \"")
    # quote_symb = '"' if '"' not in src_line else "'"
    # if quote_symb in src_line:
Exemplo n.º 58
0
import struct
from recordclass import recordclass

from pyxenoverse import BaseRecord

EANKeyframe = recordclass('EANKeyframe', ['x', 'y', 'z', 'w'])


class Keyframe(BaseRecord):
    def __init__(self, frame=0, w=0, x=0, y=0, z=0):
        self.frame = frame
        super().__init__()
        self.data = EANKeyframe(x, y, z, w)

    def __lt__(self, other):
        return self.frame < other.frame

    def paste(self, other):
        self.data = EANKeyframe(*other.data)

    def read(self, f, keyframe_size, endian):
        if keyframe_size == 1:
            self.data = EANKeyframe(*struct.unpack(endian + 'eeee', f.read(8)))
        else:
            self.data = EANKeyframe(*struct.unpack(endian +
                                                   'ffff', f.read(16)))
        # print(" (keyframe_size:{}) X : {}, Y : {}, Z : {}, W : {}".format(
        #     keyframe_size, self.x, self.y, self.z, self.w))

    def write(self, f, keyframe_size, endian):
        if keyframe_size == 1:
Exemplo n.º 59
0
from recordclass import recordclass

"""
- utter: for machine to read; i.e., for model to consume
- utter_expand: for human to interpret.

# Note: these doc records can be additionally indexed by parent_id, and thus
# children can be easily found.

"""
ElemDoc = recordclass('ElemDoc', ('elem_id', 'name',
                         'full_name', 'type',
                         'parent_id', 'rtype', 'count',
                         'utter', 'utter_expand'))
Exemplo n.º 60
0
# Snarkback game bot
# Matthew Kroesche

import discord
import recordclass
import random
import os
import datetime
import asyncio
import urllib.request

from .game import Game



Player = recordclass.recordclass('Player', 'user score round_score prompts snarks votes num_votes')
# user: the discord.User controlling this player
# score: the score of this player at the end of the last round
# round_score: the number of points accumulated by this player in the current round
# prompts: the list of string prompts given to this player
# snarks: the list of string responses by this player to the prompts
# votes: the list of integer indices representing votes cast by this player
# num_votes: the number of votes this player needs to cast



MAX_SNARK_SIZE = 50 # The maximum length of a reply to a prompt

# Timer settings
PROMPT_TIMER = 90 # The timer for a prompt
FINAL_PROMPT_TIMER = 45 # The timer for a prompt in round three