Ejemplo n.º 1
0
class Member(namedtuple("Member", ["id", "hid", "system", "color", "avatar_url", "name", "birthday", "pronouns", "description", "prefix", "suffix", "created"])):
    id: int
    hid: str
    system: int
    color: str
    avatar_url: str
    name: str
    birthday: date
    pronouns: str
    description: str
    prefix: str
    suffix: str
    created: datetime

    def to_json(self):
        return {
            "id": self.hid,
            "name": self.name,
            "color": self.color,
            "avatar_url": self.avatar_url,
            "birthday": self.birthday.isoformat() if self.birthday else None,
            "pronouns": self.pronouns,
            "description": self.description,
            "prefix": self.prefix,
            "suffix": self.suffix
        }
Ejemplo n.º 2
0
def get_frappe_dataset(batch_size):
    home = str(Path.home())
    Args = namedtuple("Args", ["data"])
    args = Args(data=os.path.join(home, ".keras/datasets"))
    x_train, y_train, x_val, y_val, x_test, y_test = load_raw_data(args)
    x_train = tf.convert_to_tensor(x_train, dtype=tf.int64)
    x_test = tf.convert_to_tensor(x_test, dtype=tf.int64)
    y_train = tf.convert_to_tensor(y_train, dtype=tf.int64)
    y_test = tf.convert_to_tensor(y_test, dtype=tf.int64)

    db = tf.data.Dataset.from_tensor_slices((x_train, y_train))
    db = db.batch(batch_size).repeat(2)
    test_db = tf.data.Dataset.from_tensor_slices((x_test, y_test))
    test_db = test_db.batch(batch_size)
    return db, test_db
Ejemplo n.º 3
0
    def __init__(self, gpu=False):
        import torch
        import torchvision
        model_t = torchvision.models.detection.fasterrcnn_resnet50_fpn
        self.device = torch.device('cuda:0' if gpu else 'cpu')
        self.model = model_t(pretrained=True,
                             box_nms_thresh=0.3).to(self.device)
        self.model.eval()

    # noinspection PyArgumentList
    def predict_boxes(self, image_filename):
        import torch
        import matplotlib.pylab as plt
        input_tensor = torch.zeros(1, 3, 2710, 3384).to(self.device)
        img = plt.imread(image_filename).astype('float32') / 255
        input_tensor[0] = torch.Tensor(img.transpose((2, 0, 1)))
        output = self.model(input_tensor)[0]
        bboxes = output['boxes']
        labels = output['labels']
        scores = output['scores']
        bboxes[[0, 2]] /= img.shape[0]
        bboxes[[1, 3]] /= img.shape[1]
        return [
            Box(float(confidence), *map(float, box))
            for box, label, confidence in zip(bboxes, labels, scores)
            if label == 3
        ]


Box = namedtuple('Box', 'confidence xmin ymin xmax ymax')
Ejemplo n.º 4
0
from pruner.core.domain_registry import init_domain_core_pytorch
from pruner.domains.ioslicerbar.pytorch.propagator import simple_domain_propagate_o2io, simple_domain_propagate_o2i, \
    ConcatPropagateWrapper, simple_domain_propagate_o2o
from pruner.prunedirector.pytorch.dependency import PruneTracker, init_dep_node, add_dep_dsts, prune_module_domain, \
    regen_replace_modules_in_model
from pruner.util.pytorch.module_traverse import get_submodule, get_module_comp_rec

module_factory_fn, domain_factory_fn = init_domain_core_pytorch('ioslicerbar')

# utility for instrumentation
# out_module_names is a list of list, each inner most list is either one output from seq
# or all the outputs going into a shortcut

# end of instrumentation utility

in_out_record = namedtuple('in_out_record',
                           ['in_module_name', 'out_module_names', 'op'])


def create_entries_for_conv_group(module, tracker: PruneTracker,
                                  tracker_str_stack):
    entry_module = None
    exit_module = None

    # check
    entry_module_entity = None
    exit_module_entity = None

    prev_module = None
    prev_module_entity = None
    for submodule_name, submodule_entity in get_submodule(module).items():
        cur_submodule_type = type(submodule_entity)
Ejemplo n.º 5
0
import random
from collections.__init__ import namedtuple

from configuration import Configurable

Transition = namedtuple('Transition', ('current_state', 'current_future_pos', 'current_past_pos',\
                'action', 'reward',\
                    'next_state', 'next_future_pos', 'next_past_pos', \
                        'terminal', 'info'))


class ReplayMemory(Configurable):
    """
        Container that stores and samples transitions.
    """
    def __init__(self, config=None, transition_type=Transition):
        super(ReplayMemory, self).__init__(config)
        self.capacity = int(self.config['memory_capacity'])
        self.transition_type = transition_type
        self.memory = []
        self.position = 0

    @classmethod
    def default_config(cls):
        return dict(memory_capacity=10000, n_steps=1, gamma=0.99)

    def push(self, *args):
        """Saves a transition."""
        if len(self.memory) < self.capacity:
            self.memory.append(None)
            self.position = len(self.memory) - 1
Ejemplo n.º 6
0
class ImageVideoWriter:
    def __init__(self, file):
        self.writer = None
        self.file = file
        self.writer = imageio.get_writer(self.file, macro_block_size=None)

    def add_frame(self, screen, format):

        frame = NumpyRGBWrapper(screen, format).numpyRGB
        self.writer.append_data(screen)

    def endSession(self):
        self.writer.close()


RLStep = namedtuple('Step', 'screen, observation, action, reward, done, meta')


class ActionEmbedding:
    """
    Simple one-hot embedding of the action space
    """
    def __init__(self, env):
        self.env = env

    def tensor(self, action):
        action_t = torch.zeros(self.env.action_space.n)
        action_t[action] = 1.0
        return action_t

    def numpy(self, action):
Ejemplo n.º 7
0
class Member(
        namedtuple("Member", [
            "id", "hid", "system", "color", "avatar_url", "name", "birthday",
            "pronouns", "description", "prefix", "suffix", "created"
        ])):
    """An immutable representation of a system member fetched from the database."""
    id: int
    hid: str
    system: int
    color: str
    avatar_url: str
    name: str
    birthday: date
    pronouns: str
    description: str
    prefix: str
    suffix: str
    created: datetime

    def to_json(self):
        return {
            "id": self.hid,
            "name": self.name,
            "color": self.color,
            "avatar_url": self.avatar_url,
            "birthday": self.birthday.isoformat() if self.birthday else None,
            "pronouns": self.pronouns,
            "description": self.description,
            "prefix": self.prefix,
            "suffix": self.suffix
        }

    @staticmethod
    async def get_member_by_name(conn, system_id: int,
                                 member_name: str) -> "Optional[Member]":
        """Fetch a member by the given name in the given system from the database."""
        member = await db.get_member_by_name(conn, system_id, member_name)
        return member

    @staticmethod
    async def get_member_by_hid(conn, system_id: Optional[int],
                                member_hid: str) -> "Optional[Member]":
        """Fetch a member by the given hid from the database. If @`system_id` is present, will only return members from that system."""
        if system_id:
            member = await db.get_member_by_hid_in_system(
                conn, system_id, member_hid)
        else:
            member = await db.get_member_by_hid(conn, member_hid)

        return member

    @staticmethod
    async def get_member_fuzzy(conn, system_id: int,
                               name: str) -> "Optional[Member]":
        by_hid = await Member.get_member_by_hid(conn, system_id, name)
        if by_hid:
            return by_hid

        by_name = await Member.get_member_by_name(conn, system_id, name)
        return by_name

    async def set_name(self, conn, new_name: str):
        """
        Set the name of a member.
        :raises: CustomEmojiError
        """
        # Custom emojis can't go in the member name
        # Technically they *could*, but they wouldn't render properly
        # so I'd rather explicitly ban them to in order to avoid confusion
        if contains_custom_emoji(new_name):
            raise errors.CustomEmojiError()

        await db.update_member_field(conn, self.id, "name", new_name)

    async def set_description(self, conn, new_description: Optional[str]):
        """
        Set or clear the description of a member.
        :raises: DescriptionTooLongError
        """
        # Explicit length checking
        if new_description and len(new_description) > 1024:
            raise errors.DescriptionTooLongError()

        await db.update_member_field(conn, self.id, "description",
                                     new_description)

    async def set_avatar(self, conn, new_avatar_url: Optional[str]):
        """
        Set or clear the avatar of a member.
        :raises: InvalidAvatarURLError
        """
        if new_avatar_url:
            validate_avatar_url_or_raise(new_avatar_url)

        await db.update_member_field(conn, self.id, "avatar_url",
                                     new_avatar_url)

    async def set_color(self, conn, new_color: Optional[str]):
        """
        Set or clear the associated color of a member.
        :raises: InvalidColorError
        """
        cleaned_color = None
        if new_color:
            match = re.fullmatch("#?([0-9A-Fa-f]{6})", new_color)
            if not match:
                raise errors.InvalidColorError()

            cleaned_color = match.group(1).lower()

        await db.update_member_field(conn, self.id, "color", cleaned_color)

    async def set_birthdate(self, conn, new_date: Union[date, str]):
        """
        Set or clear the birthdate of a member. To hide the birth year, pass a year of 0001.

        If passed a string, will attempt to parse the string as a date.
        :raises: InvalidDateStringError
        """

        if isinstance(new_date, str):
            date_str = new_date
            try:
                new_date = datetime.strptime(date_str, "%Y-%m-%d").date()
            except ValueError:
                try:
                    # Try again, adding 0001 as a placeholder year
                    # This is considered a "null year" and will be omitted from the info card
                    # Useful if you want your birthday to be displayed yearless.
                    new_date = datetime.strptime("0001-" + date_str,
                                                 "%Y-%m-%d").date()
                except ValueError:
                    raise errors.InvalidDateStringError()

        await db.update_member_field(conn, self.id, "birthday", new_date)

    async def set_pronouns(self, conn, new_pronouns: str):
        """Set or clear the associated pronouns with a member."""
        await db.update_member_field(conn, self.id, "pronouns", new_pronouns)

    async def set_proxy_tags(self, conn, prefix: Optional[str],
                             suffix: Optional[str]):
        """
        Set the proxy tags for a member. Having no prefix *and* no suffix will disable proxying.
        """
        # Make sure empty strings or other falsey values are actually None
        prefix = prefix or None
        suffix = suffix or None

        async with conn.transaction():
            await db.update_member_field(conn,
                                         member_id=self.id,
                                         field="prefix",
                                         value=prefix)
            await db.update_member_field(conn,
                                         member_id=self.id,
                                         field="suffix",
                                         value=suffix)

    async def delete(self, conn):
        """Delete this member from the database."""
        await db.delete_member(conn, self.id)

    async def fetch_system(self, conn) -> "System":
        """Fetch the member's system from the database"""
        return await db.get_system(conn, self.system)

    async def message_count(self, conn) -> int:
        return await db.get_member_message_count(conn, self.id)
Ejemplo n.º 8
0
from collections.__init__ import namedtuple

NeuralProcessParams = namedtuple('NeuralProcessParams', ['dim_r', 'dim_z', 'n_hidden_units_h', 'n_hidden_units_g'])
GaussianParams = namedtuple('GaussianParams', ['mu', 'sigma'])


from collections.__init__ import namedtuple
from xml.etree import ElementTree

import requests

Episode = namedtuple('Episode', 'title link pubdate show_id')
episode_data = {}


def get_episode(show_id):
    # GET EPISODE
    info = episode_data.get(show_id)
    print("{}. {}".format(info.show_id, info.title))


def get_latest_show_id():
    # GET LATEST SHOW ID
    latest_show_id = max(episode_data.keys())
    return latest_show_id


def download_data():
    # DOWNLOAD THE EPISODE DATA
    url = 'https://talkpython.fm/episodes/rss'
    resp = requests.get(url)
    resp.raise_for_status()
    dom = ElementTree.fromstring(resp.text)
    items = dom.findall('channel/item')
    episode_count = len(items)
    for idx, item in enumerate(items):
        episode = Episode(
Ejemplo n.º 10
0
from collections.__init__ import namedtuple

ResultRow = namedtuple(
    'ResultRow',
    ['position', 'state', 'bib', 'laps_done', 'total_time', 'lap_times'])
Ejemplo n.º 11
0
class System(namedtuple("System", ["id", "hid", "name", "description", "tag", "avatar_url", "created"])):
    id: int
    hid: str
    name: str
    description: str
    tag: str
    avatar_url: str
    created: datetime

    @staticmethod
    async def get_by_account(conn, account_id: str) -> "System":
        return await db.get_system_by_account(conn, account_id)

    @staticmethod
    async def create_system(conn, account_id: str, system_name: Optional[str] = None) -> "System":
        existing_system = await System.get_by_account(conn, account_id)
        if existing_system:
            raise errors.ExistingSystemError()

        new_hid = generate_hid()

        async with conn.transaction():
            new_system = await db.create_system(conn, system_name, new_hid)
            await db.link_account(conn, new_system.id, account_id)

        return new_system

    async def set_name(self, conn, new_name: Optional[str]):
        await db.update_system_field(conn, self.id, "name", new_name)

    async def set_description(self, conn, new_description: Optional[str]):
        if new_description and len(new_description) > 1024:
            raise errors.DescriptionTooLongError()

        await db.update_system_field(conn, self.id, "description", new_description)

    async def set_tag(self, conn, new_tag: Optional[str]):
        if new_tag:
            if len(new_tag) > 32:
                raise errors.TagTooLongError()

            if contains_custom_emoji(new_tag):
                raise errors.CustomEmojiError()

            members_exceeding = await db.get_members_exceeding(conn, system_id=self.id, length=32 - len(new_tag) - 1)
            if len(members_exceeding) > 0:
                raise errors.TagTooLongWithMembersError([member.name for member in members_exceeding])

        await db.update_system_field(conn, self.id, "tag", new_tag)

    async def set_avatar(self, conn, new_avatar_url: Optional[str]):
        if new_avatar_url:
            validate_avatar_url_or_raise(new_avatar_url)

        await db.update_system_field(conn, self.id, "avatar", new_avatar_url)

    async def link_account(self, conn, new_account_id: str):
        existing_system = await System.get_by_account(conn, new_account_id)

        if existing_system:
            if existing_system.id == self.id:
                raise errors.AccountInOwnSystemError()

            raise errors.AccountAlreadyLinkedError(existing_system)

        await db.link_account(conn, self.id, new_account_id)

    async def unlink_account(self, conn, account_id: str):
        linked_accounts = await db.get_linked_accounts(conn, self.id)
        if len(linked_accounts) == 1:
            raise errors.UnlinkingLastAccountError()

        await db.unlink_account(conn, self.id, account_id)

    async def delete(self, conn):
        await db.remove_system(conn, self.id)

    def to_json(self):
        return {
            "id": self.hid,
            "name": self.name,
            "description": self.description,
            "tag": self.tag,
            "avatar_url": self.avatar_url
        }
Ejemplo n.º 12
0
from collections.__init__ import namedtuple

SongGenerationParams = namedtuple('SongGenerationParams',
                                  ['xml', 'back_track', 'outf'])

# parameters for each standard to generate over:
# xml: path to xml file with standard notes and chords
# back_track: path to mp3 file with backing track with the same xml chords and tempo
# outf: saving name for output
song_params_dict = {
    'fly':
    SongGenerationParams('Fly_me_to_the_moon.xml', 'Fly_me_to_the_moon_v2.mp3',
                         'Fly_me_to_the_moon'),
    'giant':
    SongGenerationParams('Giant_Steps_200.xml', 'Giant_Steps_200_BT.mp3',
                         'Giant_steps'),
    'blue':
    SongGenerationParams('Blue_Bossa.xml', 'blue_bossa.mp3', 'Blue_bossa'),
    'it':
    SongGenerationParams('It_dont_mean_a_thing.xml',
                         'It_dont_mean_a_thing.mp3', 'It_dont_mean_a_thing'),
    'well':
    SongGenerationParams('Well_You_Neednt.xml', 'Well_You_Neednt.mp3',
                         'Well_you_neednt'),
    'over':
    SongGenerationParams('Over_The_Rainbow_with_chords.xml',
                         'Over_the_Rainbow.mp3', 'Over_the_rainbow'),
    'just':
    SongGenerationParams('just_friends.xml', 'Just_Friends.mp3',
                         'Just_friends'),
    'there':
Ejemplo n.º 13
0
import random
from collections.__init__ import namedtuple
from typing import Dict

import numpy as np
import tensorflow as tf

NeuralProcessParams = namedtuple(
    "NeuralProcessParams",
    ["dim_x", "dim_y", "dim_r", "dim_z", "n_hidden_units_h", "n_hidden_units_g"],
)


def split_context_target(xs, ys, n_context):
    """Randomly split a set of x,y samples into context and target sets"""
    context_mask = np.zeros(xs.shape[0], dtype=bool)
    context_mask[[i for i in random.sample(range(xs.shape[0]), n_context)]] = True

    context_xs = xs[context_mask]
    context_ys = ys[context_mask]

    target_xs = xs[~context_mask]
    target_ys = ys[~context_mask]

    return context_xs, context_ys, target_xs, target_ys
Ejemplo n.º 14
0
from typing import List

from collections.__init__ import namedtuple
from xml.etree.ElementTree import Element, SubElement

Tag = namedtuple('Tag', 'id name applicability')


def create_tag_xml(tags: List[Tag]) -> Element:
    root = Element('odoo')
    for tag in tags:
        record = SubElement(root, 'record', {
            'id': tag.id,
            'model': 'account.account.tag'
        })
        SubElement(record, 'field', {'name': 'name'}).text = tag.name
        SubElement(record, 'field', {
            'name': 'applicability'
        }).text = tag.applicability
    return root
Ejemplo n.º 15
0
from collections.__init__ import namedtuple

from django.utils.translation import ugettext_lazy as _

# the parameter labels is a list of string and tuple
# labels=['welcome_introduction',
#         ('caap', 'specific,custom')]
# second parameter can have few values:
# specific
# common
# specific,common
# Used base.views.education_groups.detail.EducationGroupGeneralInformation#get_sections_with_translated_labels
Section = namedtuple('Section', 'title labels')

SECTION_LIST = [
    Section(title=_('Welcome'),
            labels=[
                ('welcome_introduction', 'specific'),
                ('welcome_profil', 'specific'),
                ('welcome_job', 'specific'),
                ('welcome_programme', 'specific'),
                ('welcome_parcours', 'specific'),
            ]),
    Section(title=_('Teaching profile'),
            labels=[('comp_acquis', 'specific'), ('structure', 'specific')]),
    Section(title=_('Detailed programme'),
            labels=[
                ('mineures', 'specific'),
                ('majeures', 'specific'),
                ('programme_detaille', 'specific'),
                ('finalites', 'specific'),
Ejemplo n.º 16
0
class TupperboxImportResult(
        namedtuple("TupperboxImportResult", ["updated", "created", "tags"])):
    pass
Ejemplo n.º 17
0
def cast_to_int(arg: str):
    return int(float(arg))


def put_in_xml_setter(arg: str):
    setters = [XMLSetter(*v.split(",")) for v in arg]
    mirroring = [
        XMLSetter(p.replace("_l_", "_r_"), v) for p, v in setters if "_l_" in p
    ] + [
        XMLSetter(p.replace("_r_", "_l_"), v) for p, v in setters if "_r_" in p
    ]
    return [s._replace(path=s.path) for s in setters + mirroring]


XMLSetter = namedtuple("XMLSetter", "path value")


@contextmanager
def mutate_xml(
    changes: List[XMLSetter],
    dofs: List[str],
    goal_space: Box,
    n_blocks: int,
    xml_filepath: Path,
):
    def rel_to_abs(path: Path):
        return Path(xml_filepath.parent, path)

    def mutate_tree(tree: ET.ElementTree):
Ejemplo n.º 18
0
class System(
        namedtuple("System", [
            "id", "hid", "name", "description", "tag", "avatar_url", "token",
            "created", "ui_tz"
        ])):
    id: int
    hid: str
    name: str
    description: str
    tag: str
    avatar_url: str
    token: str
    created: datetime
    # pytz-compatible time zone name, usually Olson-style (eg. Europe/Amsterdam)
    ui_tz: str

    @staticmethod
    async def get_by_id(conn, system_id: int) -> Optional["System"]:
        return await db.get_system(conn, system_id)

    @staticmethod
    async def get_by_account(conn, account_id: int) -> Optional["System"]:
        return await db.get_system_by_account(conn, account_id)

    @staticmethod
    async def get_by_token(conn, token: str) -> Optional["System"]:
        return await db.get_system_by_token(conn, token)

    @staticmethod
    async def create_system(conn,
                            account_id: int,
                            system_name: Optional[str] = None) -> "System":
        async with conn.transaction():
            existing_system = await System.get_by_account(conn, account_id)
            if existing_system:
                raise errors.ExistingSystemError()

            new_hid = generate_hid()

            async with conn.transaction():
                new_system = await db.create_system(conn, system_name, new_hid)
                await db.link_account(conn, new_system.id, account_id)

            return new_system

    async def set_name(self, conn, new_name: Optional[str]):
        await db.update_system_field(conn, self.id, "name", new_name)

    async def set_description(self, conn, new_description: Optional[str]):
        # Explicit length error
        if new_description and len(new_description) > 1024:
            raise errors.DescriptionTooLongError()

        await db.update_system_field(conn, self.id, "description",
                                     new_description)

    async def set_tag(self, conn, new_tag: Optional[str]):
        if new_tag:
            # Explicit length error
            if len(new_tag) > 32:
                raise errors.TagTooLongError()

            if contains_custom_emoji(new_tag):
                raise errors.CustomEmojiError()

        await db.update_system_field(conn, self.id, "tag", new_tag)

    async def set_avatar(self, conn, new_avatar_url: Optional[str]):
        if new_avatar_url:
            validate_avatar_url_or_raise(new_avatar_url)

        await db.update_system_field(conn, self.id, "avatar_url",
                                     new_avatar_url)

    async def link_account(self, conn, new_account_id: int):
        async with conn.transaction():
            existing_system = await System.get_by_account(conn, new_account_id)

            if existing_system:
                if existing_system.id == self.id:
                    raise errors.AccountInOwnSystemError()

                raise errors.AccountAlreadyLinkedError(existing_system)

            await db.link_account(conn, self.id, new_account_id)

    async def unlink_account(self, conn, account_id: int):
        async with conn.transaction():
            linked_accounts = await db.get_linked_accounts(conn, self.id)
            if len(linked_accounts) == 1:
                raise errors.UnlinkingLastAccountError()

            await db.unlink_account(conn, self.id, account_id)

    async def get_linked_account_ids(self, conn) -> List[int]:
        return await db.get_linked_accounts(conn, self.id)

    async def delete(self, conn):
        await db.remove_system(conn, self.id)

    async def refresh_token(self, conn) -> str:
        new_token = "".join(
            random.choices(string.ascii_letters + string.digits, k=64))
        await db.update_system_field(conn, self.id, "token", new_token)
        return new_token

    async def create_member(self, conn, member_name: str) -> Member:
        # TODO: figure out what to do if this errors out on collision on generate_hid
        new_hid = generate_hid()

        if len(member_name) > self.get_member_name_limit():
            raise errors.MemberNameTooLongError(tag_present=bool(self.tag))

        member = await db.create_member(conn, self.id, member_name, new_hid)
        return member

    async def get_members(self, conn) -> List[Member]:
        return await db.get_all_members(conn, self.id)

    async def get_switches(self, conn, count) -> List[Switch]:
        """Returns the latest `count` switches logged for this system, ordered latest to earliest."""
        return [
            Switch(**s) for s in await db.front_history(conn, self.id, count)
        ]

    async def get_latest_switch(self, conn) -> Optional[Switch]:
        """Returns the latest switch logged for this system, or None if no switches have been logged"""
        switches = await self.get_switches(conn, 1)
        if switches:
            return switches[0]
        else:
            return None

    async def add_switch(self, conn, members: List[Member]) -> Switch:
        """
        Logs a new switch for a system.

        :raises: MembersAlreadyFrontingError, DuplicateSwitchMembersError
        """
        new_ids = [member.id for member in members]

        last_switch = await self.get_latest_switch(conn)

        # If we have a switch logged before, make sure this isn't a dupe switch
        if last_switch:
            last_switch_members = await last_switch.fetch_members(conn)
            last_ids = [member.id for member in last_switch_members]

            # We don't compare by set() here because swapping multiple is a valid operation
            if last_ids == new_ids:
                raise errors.MembersAlreadyFrontingError(members)

        # Check for dupes
        if len(set(new_ids)) != len(new_ids):
            raise errors.DuplicateSwitchMembersError()

        async with conn.transaction():
            switch_id = await db.add_switch(conn, self.id)

            # TODO: batch query here
            for member in members:
                await db.add_switch_member(conn, switch_id, member.id)

            return await self.get_latest_switch(conn)

    def get_member_name_limit(self) -> int:
        """Returns the maximum length a member's name or nickname is allowed to be in order for the member to be proxied. Depends on the system tag."""
        if self.tag:
            return 32 - len(self.tag) - 1
        else:
            return 32

    async def match_proxy(self, conn,
                          message: str) -> Optional[Tuple[Member, str]]:
        """Tries to find a member with proxy tags matching the given message. Returns the member and the inner contents."""
        members = await db.get_all_members(conn, self.id)

        # Sort by specificity (members with both prefix and suffix defined go higher)
        # This will make sure more "precise" proxy tags get tried first and match properly
        members = sorted(
            members,
            key=lambda x: int(bool(x.prefix)) + int(bool(x.suffix)),
            reverse=True)

        for member in members:
            proxy_prefix = member.prefix or ""
            proxy_suffix = member.suffix or ""

            if not proxy_prefix and not proxy_suffix:
                # If the member has neither a prefix or a suffix, cancel early
                # Otherwise it'd match any message no matter what
                continue

            # Check if the message matches these tags
            if message.startswith(proxy_prefix) and message.endswith(
                    proxy_suffix):
                # If the message starts with a mention, "separate" that and match the bit after
                mention_match = re.match(r"^(<(@|@!|#|@&|a?:\w+:)\d+>\s*)+",
                                         message)
                leading_mentions = ""
                if mention_match:
                    message = message[mention_match.span(0)[1]:].strip()
                    leading_mentions = mention_match.group(0)

                # Extract the inner message (special case because -0 is invalid as an end slice)
                if len(proxy_suffix) == 0:
                    inner_message = message[len(proxy_prefix):]
                else:
                    inner_message = message[len(proxy_prefix
                                                ):-len(proxy_suffix)]

                # Add the stripped mentions back if there are any
                inner_message = leading_mentions + inner_message
                return member, inner_message

    def format_time(self, dt: datetime) -> str:
        """
        Localizes the given `datetime` to a string based on the system's preferred time zone.

        Assumes `dt` is a naïve `datetime` instance set to UTC, which is consistent with the rest of PluralKit.
        """
        tz = pytz.timezone(self.ui_tz)

        # Set to aware (UTC), convert to tz, set to naive (tz), then format and append name
        return tz.normalize(
            pytz.utc.localize(dt)).replace(tzinfo=None).isoformat(
                sep=" ", timespec="seconds") + " " + tz.tzname(dt)

    async def set_time_zone(self, conn, tz_name: str) -> pytz.tzinfo:
        """
        Sets the system time zone to the time zone represented by the given string.

        If `tz_name` is None or an empty string, will default to UTC.
        If `tz_name` does not represent a valid time zone string, will raise InvalidTimeZoneError.

        :raises: InvalidTimeZoneError
        :returns: The `pytz.tzinfo` instance of the newly set time zone.
        """

        canonical_name = canonicalize_tz_name(tz_name or "UTC")
        if not canonical_name:
            raise errors.InvalidTimeZoneError(tz_name)
        tz = pytz.timezone(canonical_name)

        await db.update_system_field(conn, self.id, "ui_tz", tz.zone)
        return tz

    async def import_from_tupperbox(self, conn, data: dict):
        """
        Imports from a Tupperbox JSON data file.
        :raises: TupperboxImportError
        """
        if not "tuppers" in data:
            raise errors.TupperboxImportError()
        if not isinstance(data["tuppers"], list):
            raise errors.TupperboxImportError()

        all_tags = set()
        created_members = set()
        updated_members = set()
        for tupper in data["tuppers"]:
            # Sanity check tupper fields
            for field in [
                    "name", "avatar_url", "brackets", "birthday",
                    "description", "tag"
            ]:
                if field not in tupper:
                    raise errors.TupperboxImportError()

            # Find member by name, create if not exists
            member_name = str(tupper["name"])
            member = await Member.get_member_by_name(conn, self.id,
                                                     member_name)
            if not member:
                # And keep track of created members
                created_members.add(member_name)
                member = await self.create_member(conn, member_name)
            else:
                # Keep track of updated members
                updated_members.add(member_name)

            # Set avatar
            await member.set_avatar(conn, str(tupper["avatar_url"]))

            # Set proxy tags
            if not (isinstance(tupper["brackets"], list)
                    and len(tupper["brackets"]) >= 2):
                raise errors.TupperboxImportError()
            await member.set_proxy_tags(conn, str(tupper["brackets"][0]),
                                        str(tupper["brackets"][1]))

            # Set birthdate (input is in ISO-8601, first 10 characters is the date)
            if tupper["birthday"]:
                try:
                    await member.set_birthdate(conn,
                                               str(tupper["birthday"][:10]))
                except errors.InvalidDateStringError:
                    pass

            # Set description
            await member.set_description(conn, tupper["description"])

            # Keep track of tag
            all_tags.add(tupper["tag"])

        # Since Tupperbox does tags on a per-member basis, we only apply a system tag if
        # every member has the same tag (surprisingly common)
        # If not, we just do nothing. (This will be reported in the caller function through the returned result)
        if len(all_tags) == 1:
            tag = list(all_tags)[0]
            await self.set_tag(conn, tag)

        return TupperboxImportResult(updated=updated_members,
                                     created=created_members,
                                     tags=all_tags)

    def to_json(self):
        return {
            "id": self.hid,
            "name": self.name,
            "description": self.description,
            "tag": self.tag,
            "avatar_url": self.avatar_url
        }
Ejemplo n.º 19
0
"""
exceptions
"""
from collections.__init__ import namedtuple


ODBRequestErrorMessage = namedtuple("ODBException", "class_name, message")
Ejemplo n.º 20
0
import os
from collections.__init__ import namedtuple
from contextlib import contextmanager
from tempfile import NamedTemporaryFile
from typing import List, Sequence, Iterable


def as_lines(*lines: str) -> List[bytes]:
    return [f'{line}\n'.encode('utf-8') for line in lines]


Replacement = namedtuple('Replacement', ['line', 'from_column', 'to_column', 'text'])


def apply_replacements(file_text: Sequence[bytes], replacements: Iterable[Replacement]) -> List[bytes]:
    replaced_text = list(file_text)
    replacements_by_line = {}
    for replacement in replacements:
        replacements_by_line.setdefault(replacement.line, []).append(replacement)
    for line, line_replacements in replacements_by_line.items():
        sorted_replacements = sorted(line_replacements, key=lambda replacement: replacement.from_column)
        delta = 0
        for replacement in sorted_replacements:
            prev_line = replaced_text[line]
            replaced_text[line] = prev_line[:replacement.from_column + delta] \
                                  + replacement.text.encode('utf-8') \
                                  + prev_line[replacement.to_column + delta:]
            delta += len(replacement.text) - (replacement.to_column - replacement.from_column)
    return replaced_text

    if not user:
        user = conf.get('feeds', {}).get('anonymous_user_username')
        password = conf.get('feeds', {}).get('anonymous_user_password')

    logger.debug("using values: " + str([feeds_url, token_url, client_url, user, conn_timeout, read_timeout]))
    # TODO need a better way to select which client to return, here
    if token_url and client_url:
        http_client = Oauth2AuthenticatedClient(token_url=token_url,
                                                client_url=client_url,
                                                username=user,
                                                password=password,
                                                connect_timeout=conn_timeout,
                                                read_timeout=read_timeout,
                                                verify=verify)
    else:
        http_client = HTTPBasicAuthClient(username=user,
                                          password=password,
                                          connect_timeout=conn_timeout,
                                          read_timeout=read_timeout, 
                                          verify=verify)

    return FeedClient(endpoint=feeds_url, http_client=http_client)


FeedGroup = namedtuple('FeedGroup', ['feed', 'name', 'access_tier', 'description'])
FeedGroupList = namedtuple('FeedList', ['next_token', 'groups'])
Feed = namedtuple('Feed', ['name', 'description', 'access_tier'])
FeedList = namedtuple('FeedList', ['next_token', 'feeds'])
GroupData = namedtuple('GroupData', ['data', 'next_token', 'since'])
Ejemplo n.º 22
0
import random
from collections.__init__ import namedtuple

from rl_agents.configuration import Configurable

Transition = namedtuple(
    'Transition',
    ('state', 'action', 'reward', 'next_state', 'terminal', 'info'))


class ReplayMemory(Configurable):
    """
        Container that stores and samples transitions.
    """
    def __init__(self, config=None, transition_type=Transition):
        super(ReplayMemory, self).__init__(config)
        self.capacity = int(self.config['memory_capacity'])
        self.transition_type = transition_type
        self.memory = []
        self.position = 0

    @classmethod
    def default_config(cls):
        return dict(memory_capacity=10000, n_steps=1, gamma=0.99)

    def push(self, *args):
        """Saves a transition."""
        if len(self.memory) < self.capacity:
            self.memory.append(None)
            self.position = len(self.memory) - 1
        elif len(self.memory) > self.capacity:
Ejemplo n.º 23
0
# 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.

import queue
import warnings
from collections.__init__ import namedtuple
from ..core.utils import is_real_iterable

EdgeType = namedtuple("EdgeType", "n1 rel n2")


class GraphSchema:
    """
    Class to encapsulate the schema information for a heterogeneous graph.

    Typically this should be created from a StellarGraph object, using the
    :func:`~stellargraph.core.graph.create_graph_schema` method.
    """
    def __init__(self, is_directed, node_types, edge_types, schema):
        self._is_directed = is_directed
        self.node_types = node_types
        self.edge_types = edge_types
        self.schema = schema
Ejemplo n.º 24
0
from collections.__init__ import namedtuple
from typing import Iterable, Tuple

from tetrominoes.shape import Shape

Cell = namedtuple("Cell", 'x,y')


class BoardException(Exception):
    pass


class IllegalBoardAction(BoardException):
    pass


class Board:
    def __init__(self, size):
        self._cells = [[0 for _ in range(size)] for _ in range(size)]
        self._size = size
        self._shapes_in_board = []

    def can_fit(self, shape: Shape, start_cell: Cell):
        return self.all_cells_available(start_cell, shape)

    def insert_shape(self, shape: Shape, start_cell: Cell):
        if not self.can_fit(shape, start_cell):
            raise IllegalBoardAction()
        self._fill_cells(start_cell, shape)
        self._shapes_in_board.append((shape.name, shape.rotation))
        return self
Ejemplo n.º 25
0
        help=
        "String to prepend to all main commands, for example, sourcing a virtualenv"
    )
    parser.add_argument(
        '--flag',
        '-f',
        default=[],
        action='append',
        help="directories to create and sync automatically with each run")
    return parser
    # new_parser.add_argument(
    #     '--summary-path',
    #     help='Path where Tensorflow summary of run is to be written.')


Flag = namedtuple('Flag', 'key values')


class SpecObj:
    def __init__(self, command: str, flags: List[Flag], delimiter: str = '='):
        self.command = command
        self.flags = [Flag(*f) for f in flags]
        self.delimiter = delimiter


@Transaction.wrapper
def cli(prefix: str, path: PurePath, spec: Path, flags: List[str], logger: UI,
        description: str, transaction: Transaction, *args, **kwargs):
    # spec: Path
    with spec.open() as f:
        obj = json.load(f, object_pairs_hook=lambda pairs: pairs)
Ejemplo n.º 26
0
class SynchronizedData(namedtuple('SynchronizedData', ('timestamp', 'order', 'type', 'data'))):
    """ Small class used to store and sort data to synchronize for a game. Properties:
Ejemplo n.º 27
0
from collections.__init__ import namedtuple
from datetime import datetime
from unittest import mock
import pandas as pd
import numpy as np

Cut = namedtuple('corte', ['start', 'end'])


class Cuts(object):
    argentinaCuts = {
        'hiperinflacion':
        Cut(datetime(year=1980, month=1, day=1),
            datetime(year=1991, month=3, day=1)),
        'convertibilidad':
        Cut(datetime(year=1991, month=4, day=1),
            datetime(year=1999, month=12, day=1)),
        'crisisYrecuperacion':
        Cut(datetime(year=2000, month=1, day=1),
            datetime(year=2003, month=4, day=1)),
        'kirchner':
        Cut(datetime(year=2003, month=5, day=1),
            datetime(year=2015, month=12, day=1)),
        'macri':
        Cut(datetime(year=2016, month=1, day=1),
            datetime(year=2018, month=3, day=1)),
    }

    CUT_NAMES_IN_ORDER = [
        'hiperinflacion', 'convertibilidad', 'crisisYrecuperacion', 'kirchner',
        'macri'
Ejemplo n.º 28
0
import elasticsearch_dsl
import pandas as pd
import re
from pandas.errors import EmptyDataError

TRAINING_JOBS = 'training_jobs'
VALIDATION_JOBS = 'validation_jobs'
JOB_INDEX = elasticsearch_dsl.Index(TRAINING_JOBS)
VALIDATION_JOB_INDEX = elasticsearch_dsl.Index(VALIDATION_JOBS)

Metrics = namedtuple('Metrics', ['epochs',
                                 'train_acc',
                                 'final_val_acc',
                                 'best_val_acc',
                                 'final_val_loss',
                                 'best_val_loss',
                                 'final_val_sensitivity',
                                 'best_val_sensitivity',
                                 'final_val_specificity',
                                 'best_val_specificity'])


class TrainingJob(elasticsearch_dsl.Document):
    id = elasticsearch_dsl.Integer()
    schema_version = elasticsearch_dsl.Integer()
    job_name = elasticsearch_dsl.Keyword()
    author = elasticsearch_dsl.Keyword()
    created_at = elasticsearch_dsl.Date()
    ended_at = elasticsearch_dsl.Date()
    params = elasticsearch_dsl.Text()
    raw_log = elasticsearch_dsl.Text()