예제 #1
0
    def __init__(self, client):
        Module.__init__(self, client)

        self.users = Users(self)
        self.invites = None

        self.track_messages = Config.get_module_setting('usertracking', 'track_messages', True)
        self.tracking_exceptions = Config.get_module_setting('usertracking', 'tracking_exceptions', [])

        self.module_time_start = time.time()
        self.member_status_time_start = {}
        self.track_statuses = Config.get_module_setting('usertracking', 'track_statuses', True)

        self.audit_log_entries = {}
        self.level_cooldowns = {}
        self.level_cooldown = assert_type(Config.get_module_setting('usertracking', 'level_cooldown'), int, otherwise=5)
        self.level_cap = assert_type(Config.get_module_setting('usertracking', 'level_cap'), int, otherwise=-1)
        self.leveling_exceptions = Config.get_module_setting('usertracking', 'leveling_exceptions', [])
        self.allow_user_leveling = Config.get_module_setting('usertracking', 'allow_user_leveling', True)
        self.allow_user_rewards = Config.get_module_setting('usertracking', 'allow_user_rewards', True)
        self.allow_bot_leveling = Config.get_module_setting('usertracking', 'allow_bot_leveling', False)
        self.allow_bot_rewards = Config.get_module_setting('usertracking', 'allow_bot_rewards', False)
        self.allow_mod_leveling = Config.get_module_setting('usertracking', 'allow_mod_leveling', True)
        self.allow_mod_rewards = Config.get_module_setting('usertracking', 'allow_mod_rewards', False)
        self.regular_role = discord.utils.get(client.focused_guild.roles, id=Config.get_module_setting('usertracking', 'regular_role_id'))

        self.spam_channel = Config.get_module_setting('usertracking', 'spam_channel')
        self.log_channel = Config.get_module_setting('usertracking', 'log_channel')
예제 #2
0
 def sword_attack_normal_giant(self, target):
     assert_type(target, characters.Character)
     attacker = self.action_manager
     if attacker.check_skill():
         damage = d6(2) + attacker.get_force()
         defense = (target.get_armor() * 4)
         target.apply_damage(damage - defense)
예제 #3
0
    def create_constraint(self, identifier, callback=None, *args):
        """
		Factory method for creating Constraint objects.

		:param str identifier: identifier of Constraint to create
		:param clause: function that takes an Entity object and returns a bool if the constraint is satisfied,
			false otherwise
		:param callback: function to be called when the constraint changes state; takes a boolean representing the
			constraint state (True -> satisfied, False -> not)
		:return: Constraint object of type identifier
		:raises: AttributeError if constraint type is not registered.
		"""

        print(args)
        if callback is None:
            utils.assert_type(identifier, str)
        else:
            utils.assert_params([identifier, callback], [str, callable])

        if identifier in self._constraint_map:
            clause = self._constraint_clauses[identifier]

            if utils.is_nested_clause(clause):
                print("nested")
                utils.assert_num_params(self._constraint_clauses[identifier],
                                        *args)
                clause = clause(*args)

            print(clause)

            return _Constraint(identifier, clause, callback)

        raise AttributeError("Constraint type \"" + identifier +
                             "\" is not registered.")
예제 #4
0
 def validate(self):
     types = {
         "id":
         AssertType(int),
         "name":
         AssertType(str, 50),
         "picture":
         AssertType(str, 100),
         "company":
         AssertType(str, 50),
         "email":
         AssertType(str, 50),
         "phone":
         AssertType(str, 20),
         "latitude":
         AssertType(type=numbers.Number,
                    check_range=True,
                    minimum=-90,
                    maximum=90),
         "longitude":
         AssertType(type=numbers.Number,
                    check_range=True,
                    minimum=-180,
                    maximum=180),
     }
     assert_type(self.to_json(), types)
예제 #5
0
 def sword_attack_giant_normal(self, target):
     assert_type(target, characters.Character)
     attacker = self.action_manager
     if d6() == 1:
         damage = d6(2) + (attacker.get_force() * 4)
         defense = target.get_armor()
         target.apply_damage(damage - defense)
예제 #6
0
    def create_entity(self, identifier, obj=None):
        """
		Factory method for creating Entity objects.

		:param str identifier: identifier of Entity to create
		:param obj: object or dict; if object, uses object properties to populate entity properties;
			if dict, uses key-value pairs
		:return: Entity object of type identifier
		:raises: AttributeError if entity type is not registered.
		"""

        utils.assert_type(identifier, str)

        if identifier in self._entity_map:
            entity = _Entity(identifier, self._entity_map[identifier])

            if obj is not None:
                if type(obj) != dict:
                    obj = obj.__dict__

                for p in obj:
                    entity[p] = obj[p]

            return entity

        raise AttributeError("Entity type \"" + identifier +
                             "\" is not registered.")
예제 #7
0
 def sword_attack_normal_giant(self, target):
     assert_type(target, characters.Character)
     attacker = self.action_manager
     if attacker.check_skill():
         damage = d6(2) + attacker.get_force()
         defense = (target.get_armor() * 4)
         target.apply_damage(damage - defense)
예제 #8
0
 def sword_attack_giant_normal(self, target):
     assert_type(target, characters.Character)
     attacker = self.action_manager
     if d6() == 1:
         damage = d6(2) + (attacker.get_force() * 4)
         defense = target.get_armor()
         target.apply_damage(damage - defense)
예제 #9
0
def tuplelength(tup, state):
    """
    @ = Tuple
    
    Return the length of `Tuple`
    """
    assert_type(tup, 'tuple', state)
    return Node('number', len(tup.content))
예제 #10
0
def getfilename(func, state):
    """
    @ = Function

    Return the filename where `Function` was defined. Does not work for builtin functions.
    """
    assert_type(func, 'function', state)
    return Node('str', func.content['filename'])
예제 #11
0
 def light_saber_attack_normal_giant(self, target):
     assert_type(target, characters.Character)
     attacker = self.action_manager
     if self.light_saber_mode and attacker.check_skill():
         damage = d6(3) + attacker.get_force()
         defense = (target.get_armor() * 4)
         target.apply_damage(damage - defense)
         self.light_saber_mode = False
예제 #12
0
def stringlength(st, state):
    """
    @ = String
    
    Return the length of `String`
    """
    assert_type(st, 'str', state)
    return Node('number', len(st.content))
예제 #13
0
 def light_saber_attack_giant_normal(self, target):
     assert_type(target, characters.Character)
     attacker = self.action_manager
     if self.light_saber_mode and d6() == 1:
         damage = d6(3) + (attacker.get_force() * 4)
         defense = target.get_armor()
         target.apply_damage(damage - defense)
         self.light_saber_mode = False
예제 #14
0
 def light_saber_attack_giant_normal(self, target):
     assert_type(target, characters.Character)
     attacker = self.action_manager
     if self.light_saber_mode and d6() == 1:
         damage = d6(3) + (attacker.get_force() * 4)
         defense = target.get_armor()
         target.apply_damage(damage - defense)
         self.light_saber_mode = False
예제 #15
0
 def light_saber_attack_normal_giant(self, target):
     assert_type(target, characters.Character)
     attacker = self.action_manager
     if self.light_saber_mode and attacker.check_skill():
         damage = d6(3) + attacker.get_force()
         defense = (target.get_armor() * 4)
         target.apply_damage(damage - defense)
         self.light_saber_mode = False
예제 #16
0
 def assign_to(self, action_manager):
     """
     :param action_manager: characters.Character
     """
     assert_type(action_manager, characters.Character)
     advantage_system.Advantage.assign_to(self, action_manager)
     self.action_manager.set_action("attack with force", self.sword_attack_action)
     self.action_manager.set_action("attack with light saber", self.light_saber_attack_action)
예제 #17
0
def _input(prompt, state):
    """
    @ = Prompt

    Get input from the user, with the prompt `Prompt`.
    """
    assert_type(prompt, 'str', state)
    return Node('str', input(prompt.content))
예제 #18
0
    def __getattr__(self, item):
        utils.assert_type(item, str)

        if item not in self.attribute_map:
            raise AttributeError("Entity of type \"" + self.identifier +
                                 "\" does not have attribute \"" + item + "\"")

        return self.__dict__[item]
예제 #19
0
def stringlower(st, state):
    """
    @ = String
    
    Return a version of `String` with lowercase characters.
    """
    assert_type(st, 'str', state)
    return Node('str', st.content.lower())
예제 #20
0
    def satisfied(self, value):
        utils.assert_type(value, bool)

        if self._satisfied != value:
            self._satisfied = value

            if self._value_changed is not None:
                self._value_changed(self._satisfied)
예제 #21
0
 def get_by_name(cls, tag_name, create_on_demand = False):
     if type(tag_name) == str:
         tag_name = unicode(tag_name)
     utils.assert_type(tag_name, unicode)
     tag_obj = db.Query(ArticleTag).filter('name', tag_name).get()
     if not tag_obj and create_on_demand:
         tag_obj = ArticleTag(name=tag_name, title=tag_name, counter=0)
         tag_obj.save()
     return tag_obj
예제 #22
0
def sleep(amount, state):
    """
    @ = Ms

    Pause execution for `Ms` milliseconds.
    """
    assert_type(amount, 'number', state)
    time.sleep(amount.content/1000.0)
    return Node('atom', 'ok')
예제 #23
0
def check_geocode_address(guid, spatial_type, name, value):
    element = IfcFile.by_guid(guid)
    ifc_class = get_ifc_class_from_spatial_type(spatial_type)
    assert_type(element, ifc_class)
    if ifc_class == "IfcSite":
        address_name = "SiteAddress"
    elif ifc_class == "IfcBuilding":
        address_name = "BuildingAddress"
    assert_attribute(element, address_name)
    assert_attribute(getattr(element, address_name), name, value)
예제 #24
0
 def combo_attack(self, attackers, target):
     assert_type(target, characters.Character)
     if all([character.has_advantage(self.name) for character in attackers]) and not self.already_used:
         best_skill = max([character.get_skill() for character in attackers])
         if d6() <= best_skill:
             combo_fire_power = sum([character.get_fire_power() for character in attackers])
             damage = d6(1) + combo_fire_power
             defense = target.get_armor()
             target.apply_damage(damage - defense)
             self.already_used = True
예제 #25
0
def stringrepr(string, state):
    """
    @ = String

    Replaces tabs with \\t, newlines with \\n, backslashes with \\\\
    and double-quotes with \\\" in `String`, wraps that in double-quotes
    and returns the result.
    """
    assert_type(string, 'str', state)
    return Node('str', utils.stringrepr(string))
예제 #26
0
 def assign_to(self, action_manager):
     """
     :param action_manager: characters.Character
     """
     assert_type(action_manager, characters.Character)
     advantage_system.Advantage.assign_to(self, action_manager)
     self.action_manager.set_action("attack with force",
                                    self.sword_attack_action)
     self.action_manager.set_action("attack with light saber",
                                    self.light_saber_attack_action)
예제 #27
0
def call_malang_func(func, arg, state):
    """
    Call the already evaluated `func` with the already evaluated `value`.
    """
    utils.assert_type(func, ('builtin', 'function'), state)
    if func._type == 'builtin':
        return func.content(arg, state.newinfonode(expr))
    elif func._type == 'function':
        return trampoline(func.content['code'],
                          utils.State(env=Env(parent=func.content['parent_env'], bindings={'@': arg}),
                                      filename=func.content['filename']))
예제 #28
0
    def add_constraint(self, constraint):
        """
		Add constraint to entity

		:param _Constraint constraint:
		:return: None
		"""
        utils.assert_type(constraint, _Constraint)

        self.constraints.append(constraint)
        constraint.check()
예제 #29
0
    def remove_constraint(self, constraint):
        """
		Remove constraint from entity

		:param _Constraint constraint:
		:return: None
		"""
        utils.assert_type(constraint, _Constraint)

        if constraint in self.constraints:
            self.constraints.remove(constraint)
예제 #30
0
def readfile(fname, state):
    """
    @ = Filename

    Read the text in `Filename` and return the resulting string.
    """
    assert_type(fname, 'str', state)
    try:
        with open(fname.content) as f:
            return Node('str', f.read())
    except IOError as e:
        raise MalangError(e.args[1], state)
예제 #31
0
def tolist(tup, state):
    """
    @ = Tuple

    Convert `Tuple` to a malang list.
    """
    assert_type(tup, 'tuple', state)
    if tup.content == ():
        return Node('list', 'nil')
    else:
        return Node('list', (tup.content[0],
                             tolist(Node('tuple', tup.content[1:]), state)))
예제 #32
0
    def link_to(self, entity):
        """
		Link the constraint to an Entity. Required in order to check the constraint. If already linked to an entity,
			does nothing.

		:param _Entity entity: entity to link constraint to
		:return: None
		"""

        utils.assert_type(entity, _Entity)

        if self.entity is None:
            self.entity = entity
예제 #33
0
 def combo_attack(self, attackers, target):
     assert_type(target, characters.Character)
     if all([character.has_advantage(self.name)
             for character in attackers]) and not self.already_used:
         best_skill = max(
             [character.get_skill() for character in attackers])
         if d6() <= best_skill:
             combo_fire_power = sum(
                 [character.get_fire_power() for character in attackers])
             damage = d6(1) + combo_fire_power
             defense = target.get_armor()
             target.apply_damage(damage - defense)
             self.already_used = True
예제 #34
0
 def load(self, path):
     """ Loads Image_Generator() saved at path. Sets initialized to True """
     # FIXME: ERROR: Unknown loss function:loss
     u.assert_type('path', path, str)
     assert (u.os.path.exists(path)), f"path {path} not found."
     # self.summarizerStruct = load_model(f'{path}/summarizerStruct.h5')
     self.generatorStruct = load_model(f'{path}/generatorStruct.h5')
     self.discriminatorStruct = load_model(f'{path}/discriminatorStruct.h5')
     self.describerStruct = load_model(f'{path}/describerStruct.h5')
     self.discriminatorModel = load_model(f'{path}/discriminatorModel.h5')
     self.describerModel = load_model(f'{path}/describerModel.h5')
     self.adversarialModel = load_model(f'{path}/adversarialModel.h5')
     self.creativeModel = load_model(f'{path}/creativeModel.h5')
     self.initizalized = True
예제 #35
0
def getdocstring(func, state):
    """
    @ = Function

    Return the docstring for `Function`, or `nope` if there is no docstring for `Function`.
    """
    assert_type(func, ('builtin', 'function'), state)

    docstring = func.content.__doc__ if func._type == 'builtin' else func.content['docstring'] 

    if docstring is None:
        return Node('atom', 'nope')
    else:
        return Node('str', docstring)
예제 #36
0
def string_to_number(string, state):
    """
    @ = String

    Convert the `String` to a number. `String` can be of the form `<base>#<numeral>`
    where `base` can be from 2 (binary) to 16 (hexadecimal), or just `<numberal>`,
    where a base of 10 will be assumed. You can write numbers in your malang programs
    this way too.
    """
    assert_type(string, 'str', state)
    try:
        return Node('number', to_number(string.content))
    except ValueError:
        raise MalangError("Can't convert string to number", state)
예제 #37
0
    def refresh_available_roles(self):
        self.available_roles = assert_type(Config.get_module_setting(
            'notifyMe', 'roles'),
                                           dict,
                                           otherwise={})
        for role, data in self.available_roles.items():
            if not assert_type(data[0], int, otherwise=None):
                del self.available_roles[role]

            role_object = discord.utils.get(self.client.focused_guild.roles,
                                            id=data[0])
            if not role_object:
                del self.available_roles[role]

            self.available_roles[role] = (role_object, data[1])
예제 #38
0
 def save(self, path):
     """ Saves Image_Generator() object to path """
     assert self.initizalized, 'models must be initialized before saving.'
     u.assert_type('path', path, str)
     u.safe_make_folder(path)
     # self.summarizerStruct.save(f'{path}/summarizerStruct.h5')
     self.generatorStruct.save(f'{path}/generatorStruct.h5')
     self.discriminatorStruct.save(f'{path}/discriminatorStruct.h5')
     self.describerStruct.save(f'{path}/describerStruct.h5')
     self.discriminatorModel.save(f'{path}/discriminatorModel.h5')
     self.describerModel.save(f'{path}/describerModel.h5')
     self.adversarialModel.save(f'{path}/adversarialModel.h5')
     self.creativeModel.save(f'{path}/creativeModel.h5')
     print(f"Model saved to {path}.")
     return True
예제 #39
0
class NewPostAnnouncer(Announcer):
    CHANNEL_ID = Config.get_module_setting('news', 'announcements')
    NOTIFICATION_ROLES = assert_type(Config.get_module_setting('news', 'notification_roles'), list, otherwise=[])
    
    async def announce(self, data):
        alphanumeric = re.compile('[^A-Za-z0-9-]')

        url = 'https://www.toontownrewritten.com/news/item/' + str(data['postId']) + '/' + alphanumeric.sub('', data['title'].lower().replace(' ', '-'))

        info = "**{}**\nPosted by {} on {}".format(data['title'], data['author'], data['date'])
        image = Embed.Empty
        if data['image']:
            k = data['image'].rfind('?')
            k = len(data['image']) if k == -1 else k
            image = data['image'][:k]
        embed = self.module.create_discord_embed(subtitle="New Blog Post!", subtitle_url=url, info=info, image=image)

        roles = []
        for role_id in self.NOTIFICATION_ROLES:
            role = discord.utils.get(self.module.client.focused_guild.roles, id=role_id)
            if role:
                roles.append(role)

        if roles:
            ping_string = ' '.join([r.mention for r in roles])
            return await self.send(content=ping_string, embed=embed)
        else:
            return await self.send(embed=embed)
예제 #40
0
def require(filename_to_open, state):
    """
    @ = Filename

    Evaluate the program in the file `Filename`, and return a module.
    """
    utils.assert_type(filename_to_open, 'str', state)

    try:
        with open(filename_to_open.content) as f:
            code = f.read()
    except IOError as e:
        raise MalangError(e.args[1], state.filename, state.infonode)

    module_env = Env(parent=main_env)
    abspath = path.abspath(filename_to_open.content)
    with change_directory(path.dirname(abspath)):
        eval_malang(code, module_env, abspath)
    return Node('module', module_env)
예제 #41
0
def stringreplace(tup, state):
    """
    @ = What Replacement String

    Replace every occurence of the string `What` in `String` by the string `Replacement`.
    """
    assert_type(tup, 'tuple', state, tuplelength=3)
    what, replacement, string = tup.content
    assert_type(what,        'str', state)
    assert_type(replacement, 'str', state)
    assert_type(string,      'str', state)
    
    return Node('str', string.content.replace(what.content, replacement.content))
예제 #42
0
def fmt(tup, state):
    """
    @ = {String, Thing1, ..., ThingN}

    Format `String` using the `Thing<N>`s. It will search for numbers
    between curly braces in `String` and replace them with the thing in
    that position.

    `Fmt {"{1} hay", "test"}` should return "test hay".
    `Fmt {"{2}{1} {1} hay", 42, "?"}` should return "?42 42 hay".

    You can optionally put "|<fillchar><direction><width>" at the end of the
    index in the curly braces, where `width` will set a minimum width,
    `direction` must be either "<" or ">" to specify which way to justify
    the value when `width` is longer than the value, and `fillchar` specifies
    what character to pad with then `width` is longer than the value.

    So `Fmt {"{1|->5}$", 32}` should return "---32$".
    """
    assert_type(tup, 'tuple', state)
    if not len(tup.content) >= 2:
        raise MalangError("Fmt needs a string and at least 1 thing", state)

    string = tup.content[0]
    assert_type(string, 'str', state)

    things = Node('tuple', tup.content[1:])

    def subfun(match):
        idx = int(match.group('idx'))
        thing = tuplenth(Node('tuple', (Node('number', idx), things)), state)
        string = tostr(thing).content

        if match.group('configs'):
            just = {'<': str.ljust, '>': str.rjust}[match.group('direction')]
            string = just(string, int(match.group('width')), match.group('fillchar'))
        return string

    pattern = (r"\{(?P<idx>[0-9]+)"
               r"(?P<configs>\|(?P<fillchar>.)(?P<direction>[<>])(?P<width>[0-9]+))?\}")
    return Node('str', re.sub(pattern, subfun, string.content))
예제 #43
0
    def __init__(self, client):
        Module.__init__(self, client)

        reddit = self.reddit = praw.Reddit(
            client_id=assert_type(
                Config.get_module_setting('reddit', 'client_id'), str),
            client_secret=assert_type(
                Config.get_module_setting('reddit', 'client_secret'), str),
            user_agent=assert_type(Config.get_module_setting('reddit', 'ua'),
                                   str),
            username=assert_type(
                Config.get_module_setting('reddit', 'username'), str),
            password=assert_type(
                Config.get_module_setting('reddit', 'password'), str))

        self.subreddit_name = assert_type(
            Config.get_module_setting('reddit', 'subreddit'), str)
        self.subreddit = reddit.subreddit(self.subreddit_name)

        self.post_stream = None
        self.comment_stream = None
        self.live_stream = None
        self.live = None
        self.ready_to_stop = False

        self.post_announcer, self.comment_announcer, self.live_announcer = self.create_announcers(
            NewPostAnnouncer, NewCommentAnnouncer, NewUpdateAnnouncer)
예제 #44
0
def exhibit(module, state):
    """
    @ = Module | exit

    Change the REPL environment to be the same as the environment in `Module`.

    if the argument is `exit`, then it will exit the exhibit and change back to the
    original REPL environment.
    
    This builtin only has an effect in the REPL, it makes no sense elsewhere.
    """
    global REPL_env

    if module._type == 'atom' and module.content == 'exit':
        REPL_env = original_REPL_env
    else:
        utils.assert_type(module, 'module', state)
        REPL_env = module.content

    if readline_imported:
        readline.set_completer(utils.Completer(REPL_env))

    return Node('atom', 'ok')
예제 #45
0
    async def update(self, *args, **kwargs):
        if self.module.is_first_loop:
            msg = self.module.create_discord_embed(title=self.TITLE, info='Collecting the latest information...', color=Color.light_grey())
            return await self.send(msg)

        megainvs = []
        invs = []
        for inv in self.module.invasions:
            if inv.mega_invasion:
                megainvs.append(inv)
            else:
                invs.append(inv)
        megainvs = sorted(megainvs, key=lambda k: -k.start_time)
        invs = sorted(invs, key=lambda k: (-k.etr if k.etr != -1 else (k.defeated/k.total)))

        invs = megainvs + invs

        if time.time() >= (assert_type(self.module.last_updated, int, otherwise=0) + 300):
            desc = 'We\'re experiencing some technical difficulties.\nInvasion tracking will be made reavailable as soon as possible.'
            msg = self.module.create_discord_embed(title=self.TITLE, info=desc, color=Color.light_grey())
            msg.set_footer(text='We apologize for the inconvenience.')
        elif len(invs) > 0:
            cogs = []
            districts = []
            etrs = []
            progress = []
            for inv in invs:
                if inv.etr != -1:
                    etr = get_time_from_seconds(inv.etr)
                    etr = 'A few seconds' if inv.etr < 0 else etr
                    etr = 'Calculating...' if time.time() - inv.start_time < 60 else etr
                    etr = 'Mega Invasion!' if inv.mega_invasion else etr
                    etrs.append(etr)
                else:
                    p = int((inv.defeated/inv.total) * 10)
                    # Pray this never has to be debugged.
                    progress.append('[{}{}]'.format('■' * p, ('  '*(10-p))+(' '*ceil((10-p)/2))))
                cogs.append(inv.cog.plural())
                districts.append(inv.district)
            fields = [
                {'name': 'Cog', 'value': '\n'.join(cogs)},
                {'name': 'District', 'value': '\n'.join(districts)},
                {'name': 'Time Remaining', 'value': '\n'.join(etrs)} if etrs else {'name': 'Progress', 'value': '\n'.join(progress)}
            ]
            msg = self.module.create_discord_embed(title=self.TITLE, title_url=self.module.route[1], color=Color.light_grey(), fields=fields)
        else:
            desc = 'No invasions to report.\nThe last invasion seen was __{} ago__.'.format(
                get_time_from_seconds(int(time.time()) - self.module.drought_start))
            msg = self.module.create_discord_embed(title=self.TITLE, info=desc, color=Color.light_grey())
        return await self.send(msg)
예제 #46
0
def stringslice(tup, state):
    """
    @ = Start Stop String
    
    Returns a substring of `String` starting at index `Start` (inclusive),
    ending at index `Stop` (exclusive).
    """
    assert_type(tup, 'tuple', state, tuplelength=3)
    start, stop, string = tup.content
    assert_type(start,  'number', state)
    assert_type(stop,   'number', state)
    assert_type(string, 'str', state)

    if start.content < 1 or stop.content < 1:
        raise MalangError("Indices start at 1", state)

    return Node('str', string.content[start.content-1:stop.content-1])
예제 #47
0
def tupleslice(tup, state):
    """
    @ = Start Stop Tuple
    
    Returns a subtuple of `Tuple` starting at index `Start` (inclusive),
    ending at index `Stop` (exclusive).
    """
    assert_type(tup, 'tuple', state, tuplelength=3)
    start, stop, actual_tuple = tup.content
    assert_type(start,  'number', state)
    assert_type(stop,   'number', state)
    assert_type(actual_tuple, 'tuple',  state)

    if start.content < 1 or stop.content < 1:
        raise MalangError("Indices start at 1", state)

    return Node('tuple', actual_tuple.content[start.content-1:stop.content-1])
예제 #48
0
def randrange(tup, state):
    """
    @ = Start Stop

    Returns a random integer between `Start` (inclusive) and `Stop` (exclusive).
    """
    assert_type(tup, 'tuple', state, tuplelength=2)
    num1, num2 = tup.content
    assert_type(num1, 'number', state)
    assert_type(num2, 'number', state)
    return Node('number', random.randrange(num1.content,num2.content))
예제 #49
0
def stringlstrip(tup, state):
    """
    @ = Chars String
    
    Return `String` with all leading characters that appear in the string `Chars` removed.
    """
    assert_type(tup, 'tuple', state, tuplelength=2)
    chars, string = tup.content
    assert_type(chars,  'str', state)
    assert_type(string, 'str', state)

    return Node('str', string.content.lstrip(chars.content))
예제 #50
0
def stringsplit(tup, state):
    """
    @ = Sep String
    
    Splits `String` into a list of strings, using the non-empty string `Sep` as the delimiter.
    """
    assert_type(tup, 'tuple', state, tuplelength=2)
    sep, string = tup.content
    assert_type(sep,    'str', state)
    assert_type(string, 'str', state)

    if sep.content == "":
        raise MalangError("The separator cannot be the empty string", state)

    return iter_to_malang_list(
        list(map(lambda s: Node('str', s), string.content.split(sep.content)))
    )
예제 #51
0
def writefile(arg, state):
    """
    @ = Str Filename

    Overwrite the contents of `Filename` to be `Str`.
    """
    assert_type(arg, 'tuple', state, tuplelength=2)
    _str, fname = arg.content
    assert_type(_str, 'str',  state)
    assert_type(fname, 'str', state)

    try:
        with open(fname.content, mode="w") as f:
            f.write(_str.content)
    except IOError as e:
        raise MalangError(e.args[1], state)

    return Node('atom', 'ok')
예제 #52
0
def tuplenth(arg, state):
    """
    @ = N Tuple

    Returns the `N`th element of `Tuple`. (Starting from 1)
    """
    assert_type(arg, 'tuple', state, tuplelength=2)
    N, actual_tuple = arg.content
    assert_type(N, 'number', state)
    assert_type(actual_tuple, 'tuple', state)
    
    try:
        idx = N.content-1
        if idx < 0:
            raise MalangError("Indices start at 1", state)
        return actual_tuple.content[idx]
    except IndexError:
        raise MalangError("Tuple index out of range", state)
예제 #53
0
    def __init__(self, client):
        self.client = client

        module_name = os.path.basename(
            sys.modules[self.__module__].__file__).replace('.py', '')
        self.run_without_restored_session = assert_type(
            Config.get_module_setting(module_name, 'run_wo_restored_session'),
            bool,
            otherwise=False)
        self.restart_on_exception = assert_type(Config.get_module_setting(
            module_name, 'restart_on_exception'),
                                                bool,
                                                otherwise=True)
        self.cooldown_interval = assert_type(Config.get_module_setting(
            module_name, 'cooldown_interval'),
                                             int,
                                             otherwise=60)
        self.restart_limit = assert_type(Config.get_module_setting(
            module_name, 'restart_limit'),
                                         int,
                                         otherwise=3)
        self.restart_limit_reset_interval = assert_type(
            Config.get_module_setting(module_name,
                                      'restart_limit_reset_interval'),
            int,
            otherwise=1800  # 30 minutes
        )
        self.public_module = assert_type(Config.get_module_setting(
            module_name, 'public_module'),
                                         bool,
                                         otherwise=True)
        self.is_first_loop = True
        self.running_loop = None

        self._commands = [
            attr for attr in self.__class__.__dict__.values()
            if isclass(attr) and issubclass(attr, Command)
        ]

        self.restarts = 0
        self.restart_time = 0
예제 #54
0
 def assign_to(self, action_manager):
     assert_type(action_manager, characters.Character)
     advantage_system.Advantage.assign_to(self, action_manager)
     self.action_manager.set_action("command robot",
                                    self.command_robot_action)
예제 #55
0
 def devastating_blow_fire_power(self, target):
     assert_type(target, characters.Character)
     if self.action_manager.check_skill():
         damage = (d6() + self.action_manager.get_force()) * 2
         defense = target.get_armor()
         target.apply_damage(damage - defense)
예제 #56
0
def step_impl(context, guid, ifc_class):
    element = IfcFile.by_guid(guid)
    assert_type(element, ifc_class, is_exact=True)
예제 #57
0
 def load(self, path):
     """ Loads from path """
     u.assert_type('path', path, str)
     assert u.os.path.exists(path), f'path "{path}" not found.'
     self.trainIdx = u.load(f'{path}/trainIdx.sav')
     self.indexSize = len(self.trainIdx)
예제 #58
0
 def assign_to(self, action_manager):
     assert_type(action_manager, characters.Character)
     advantage_system.Advantage.assign_to(self, action_manager)
     self.action_manager.set_action("weapon combo",
                                    self.combo_attack_action)
예제 #59
0
 def assign_to(self, action_manager):
     utils.assert_type(action_manager, action.ActionManager)
     self.action_manager = action_manager
예제 #60
0
    def train_models(self,
                     dataObj,
                     iter,
                     preIter=10,
                     batchSize=100,
                     saveInt=200):
        """
        Train summarizer, generator, discriminator, describer, adversarial,
        and creative models on dataset with end-goal of text-to-image
        generation.
        Args:
            dataObj:            Object of the data on which to train. Must have
                                method 'fetch_batch', which returns a tuple of
                                lists of captionTexts, captionVecs, imageArrays.
            iter:               Number of iterations for which to train
            batchSize:          Size of batch with with to train
            preIter (opt):      Number of pretraining iterations in which
                                only the discriminator and describer train.
                                Defaults to 10.
            preBatch (opt):     Defaults to 100.
            saveInt (opt):      Interval at which to save examples and struct
                                of generator model. Defaults 500.
        """
        u.assert_type('iter', iter, int)
        u.assert_type('batchSize', batchSize, int)
        u.assert_pos('iter', iter)
        u.assert_pos('batchSize', batchSize)

        u.safe_make_folder('training_data')

        def make_discriminator_batch(captions, images):
            """
            Makes discriminator batch from real images and generated images
            Images must be (n,512,512,3)
            """
            fake_set = self.image_from_textVec(captions)
            batch = np.concatenate((fake_set, images), axis=0)

            num_fake = fake_set.shape[0]
            num_real = images.shape[0]
            answer_key = np.zeros(num_fake + num_real)
            answer_key[num_fake:] = 1
            return batch, answer_key

        def make_describer_batch(captions, images):
            """
            Makes describer batch from real caption cls and describer-generated
            pseudo-cls tokens
            captions: (n,1024)
            images: (n,512,512,3)
            """
            correct_captions = captions
            images_to_caption = images
            return images_to_caption, correct_captions

        def make_adversarial_batch(captions):
            """
            Makes adversarial batch of entirely fake, positively-scored images
            using captions for generator improvement
            captions: (n,1024)
            """
            score = np.ones(captions.shape[0])
            return captions, score

        def make_creative_batch(captions, images):
            """
            Makes creative batch of fake images and real captions for
            generator improvement
            """
            return captions, captions

        def update_lr(lr, discL, discA, descL, advL, advA, creativel,
                      trainingRound):
            lr[0] = lr[0] - lr[0] * (advA - 0.5)
            lr[1] = 0.1
            lr[2] = lr[2]
            lr[3] = lr[3]
            return lr

        # pretrain
        for curPre in range(preIter):
            captionStrings, captionVecs, images = dataObj.fetch_batch(
                batchSize)
            (discriminatorX,
             discriminatorY) = make_discriminator_batch(captionVecs, images)
            # (describerX,
            # describerY) = make_describer_batch(captionVecs, images)
            discData = self.discriminatorModel.train_on_batch(
                discriminatorX, discriminatorY)
            # descData = self.describerModel.train_on_batch(describerX,
            #                                             describerY)
            discL, discA = round(discData[0], 3), round(discData[1], 3)
            # descL = round(descData, 3)
            print(f'PreIter: {curPre}\n\tDiscriminator:'
                  f'[L: {discL} | A: {discA}]\n\t{"-"*80}')
            # f'Describer: [L: {descL}]\n')

        startTime = time()
        for i in range(iter):
            # load array of current batch
            # batchArray = np.load(fileList[(i % fileNum)])
            captionStrings, captionVecs, images = dataObj.fetch_batch(
                batchSize)
            # make batches for each model from batchArray
            (discriminatorX,
             discriminatorY) = make_discriminator_batch(captionVecs, images)
            # (describerX,
            # describerY) = make_describer_batch(captionVecs, images)
            (adversarialX, adversarialY) = make_adversarial_batch(captionVecs)
            # (creativeX,
            # creativeY) = make_creative_batch(captionVecs, images)
            # train each model on respective batch

            # if i == 0:
            #     self.lr = self.INIT_LR

            # K.set_value(self.discriminatorModel.optimizer.lr,self.lr[0])
            # K.set_value(self.describerModel.optimizer.lr,self.lr[1])
            # K.set_value(self.adversarialModel.optimizer.lr,self.lr[2])
            # K.set_value(self.creativeModel.optimizer.lr,self.lr[3])
            # print('values set')

            discData = self.discriminatorModel.train_on_batch(
                discriminatorX, discriminatorY)
            # descData = self.describerModel.train_on_batch(describerX,
            #                                             describerY)

            advData = self.adversarialModel.train_on_batch(
                adversarialX, adversarialY)
            # creativeData = self.creativeModel.train_on_batch(creativeX,
            #                                                     creativeY)
            # round and log
            discL, discA = round(discData[0], 3), round(discData[1], 3)
            # descL = round(descData, 3)
            advL, advA = round(advData[0], 3), round(advData[1], 3)
            # creativeL = round(creativeData, 3)
            print(f'Iter: {i} | Runtime: {round(time() - startTime)}\n\t'
                  f'Discriminator: [L: {discL} | A: {discA}]\n\t'
                  # f'Describer: [L: {descL}]\n\t'
                  f'Adversarial: [L: {advL} A: {advA}]\n\t\n{"-"*80}')
            # f'Creative: [L {creativeL}]\n{"-"*80}')

            # self.lr = update_lr(lr,discL,discA, descL, advL,
            #               advA, creativel, i)

            if (((i % saveInt) == 0) and (i != 0)):
                # TODO: imp generate_and_plot
                captions, vecs, _ = dataObj.fetch_batch(1)
                print(captions, vecs)
                vec = np.expand_dims(vecs[0], axis=1).T
                p = self.image_from_textVec(vec)
                plt.imshow(p[0, :, :, :])
                plt.title(captions[0])
                plt.savefig(f'training_data/Step_{i}')
                plt.close()