Esempio n. 1
0
def get_file_name() -> str:
    """
    :return: generate file name from file format + file path
    """
    file_format = Arguments.get_format_of_file()
    customer_file_name = Arguments.get_first_file_name()
    return f'{file_format}/{customer_file_name}'
Esempio n. 2
0
def main():
  args = Arguments()
  args.parse()
  if args.error:
    print(args.error)
    return
  Frontend(args.dict)
Esempio n. 3
0
def main(argv):
    t1 = time.time()
    desc = "Takes a file containing pairs of words \
(English - OtherLanguage) and a second file containing \
the translation to the OtherLanguage words, and get three \
similarity scores based on WordNet to each pair."

    required = [desc, 'inputfile', 'outputfile']
    obj_args = Arguments(required)
    logger.info(obj_args)
    args = obj_args.getArgs()

    comments = """# This file is composed by the list of terms
# from the papers of Rubenstein and Goodenough (1965) and
# Joubarne and InkPen (2011). The pairs are composed as the
# first word of Goodenough and the second word of Joubarne pairs.
# The word in parentesis is the most frequent translation to the
# French word. The score is the average of the scores of both 
# pairs. For instance, the score to the pair "cord - sourire" 
# is the average between the scores of the pairs "cord - smile 
# = 0.02" (Rubenstein and Goodenough) and "corde - sourire = 0.00" 
# (Joubarne and InkPen). All pairs can be found on the page:
# http://www.site.uottawa.ca/~mjoub063/wordsims.htm\n#
# References:
# - Rubenstein, H. and J. B. Goodenough. “Contextual correlates of
# synonymy.”, Communications of the ACM 8(10), 1965, pp. 627–633.
# - Joubarne, C. and InkPen, D. "Comparison of Semantic Similarity
# for Different Languages Using the Google n-gram Corpus and Second-
# Order Co-occurrence Measures", Advances in Artificial Intelligence,
# v. 6657, 2011, pp. 216-221.\n#
# The pairs are organized as follows:
# English_word [\\t] French_word (English_translation) [\\t] Human_score [\\t]
# Path_similarity [\\t] Leacock-Chodorow_similarity[\\t] Wu-Palmer_similarity [\\n]\n#\n"""

    regex_tr = re.compile('\((\w+)\)')
    args.outputfile.write(comments)
    for line in args.inputfile:
        if not "#" in line:  # Skip comments
            term_1, term_2, human_score = line.strip().split('\t')
            translation = regex_tr.search(term_2).group(1)
            synset_1 = wordnet.synset(term_1 + '.n.01')
            synset_2 = wordnet.synset(translation + '.n.01')
            logger.info('calculating similarity to the pair: %s %s' %
                        (term_1, term_2.decode('utf-8')))

            # Similarity measures
            path_sim = synset_1.path_similarity(synset_2)
            lch_sim = synset_1.lch_similarity(synset_2)
            wup_sim = synset_1.wup_similarity(synset_2)
            args.outputfile.write(
                '%s\t%s\t%s\t%f\t%f\t%f\n' %
                (term_1, term_2, human_score, path_sim, lch_sim, wup_sim))

    t2 = time.time()
    total_time = (t2 - t1) / 60
    logger.info('processing time: %f minutes.' % total_time)
Esempio n. 4
0
def main():
    file_format = Arguments.get_format_of_file()
    try:
        if file_format == 'csv':
            first_argument = Arguments.get_first_file_name()
            second_argument = Arguments.get_second_file_name()

            csv_parser = CsvParser(first_argument, second_argument)

            customer_data = csv_parser.extract_customer_data()
            vehicle_data = csv_parser.extract_vehicle_data()

            customers = [Customer(**customer) for customer in customer_data]
            for customer in customers:
                vehicles = [
                    Vehicle(**vehicle) for vehicle in vehicle_data
                    if customer.id == vehicle.get('owner_id')
                ]
                customer.add_vehicles(vehicles)

                result = csv_parser.generate_json_file_structure(customer)
                csv_parser.save_data_as_json(result)

        elif file_format == 'xml':
            first_argument = Arguments.get_first_file_name()

            xml_parser = XmlParser(first_argument)

            customer_data = xml_parser.extract_customer_data()
            vehicle_data = xml_parser.extract_vehicle_data()

            current_customer = Customer(**customer_data)
            all_vehicles = [Vehicle(**vehicle) for vehicle in vehicle_data]
            current_customer.add_vehicles(all_vehicles)

            result = xml_parser.generate_json_file_structure(current_customer)
            xml_parser.save_data_as_json(result)
        else:
            raise NotImplemented('Not Implemented yet')
    except ParserException as e:
        logger.error(e)
    except Exception as e:
        logger.error(e)
Esempio n. 5
0
    async def register(self, ctx, *, msg):
        parser = Arguments(allow_abbrev=False, prog='sudo register')
        parser.add_argument('user',
                            type=choices.user(ctx.message.channel.server))
        parser.add_argument('username', nargs='+')

        await self.bot.send_typing(ctx.message.channel)

        args = await parser.do_parse(self.bot, msg)
        if not args:
            return

        # Joining username
        args.username = '******'.join(args.username)

        # Checking if username is registered already
        if User.select().where(User.nitro_name % args.username).first():
            await self.bot.say(
                '**%s** is already registered to another user.' % args.username
            )
            return

        # Getting profile for user
        profile = await get_profile(args.username)

        # Checking if username has profile
        if not profile:
            await self.bot.say('No profile found with the name **%s**.' %
                               args.username)

        await self.bot.cogs['Register'].update_discord_user(
            args.user, self.bot, profile, ctx.message.channel.server.roles)
        await self.bot.say('Successfully registered %s racer **%s**.' %
                           (args.user.mention, profile['username']))
Esempio n. 6
0
    async def unregister(self, ctx, *, msg):
        parser = Arguments(allow_abbrev=False, prog='sudo register')
        parser.add_argument('user',
                            type=choices.user(ctx.message.channel.server))

        await self.bot.send_typing(ctx.message.channel)

        args = await parser.do_parse(self.bot, msg)
        if not args:
            return

        # Checking if the user is in the DB
        u = User.select().where(User.id == args.user.id).first()
        if not u:
            await self.bot.say(
                '**%s**\'s Discord account is not linked to Nitro Type.' %
                args.user.display_name)
            return

        await self.bot.cogs['Unregister'].update_discord_user(
            self.bot, args.user, u)
        await self.bot.say(
            '**%s**\'s Nitro Type account has been unregistered.' %
            args.user.display_name)
Esempio n. 7
0
    async def prune(self, ctx, *, msg=''):
        parser = Arguments(allow_abbrev=False, prog='sudo prune')
        parser.add_argument('-b',
                            '--bot',
                            action='store_true',
                            default=False,
                            help='Deletes messages from bots.')
        parser.add_argument('-e',
                            '--embeds',
                            action='store_true',
                            default=False,
                            help='Deletes messages with embeds.')
        parser.add_argument('-c',
                            '--contains',
                            nargs='?',
                            default=None,
                            help='Deletes messages containing a phrase.')
        parser.add_argument('-u',
                            '--user',
                            nargs='?',
                            default=None,
                            type=choices.user(ctx.message.channel.server),
                            help='Deletes messages from a user.')
        parser.add_argument(
            '-l',
            '--limit',
            type=choices.between(1, 1000, True),
            default=100,
            help=
            'The number of messages to look through to see what messages can be purged.'
        )

        await self.bot.send_typing(ctx.message.channel)
        args = await parser.do_parse(self.bot, msg)
        if not args:
            return

        _all = not args.bot and not args.embeds and args.contains is None and args.user is None
        deleted_messages = await self.bot.purge_from(
            ctx.message.channel,
            limit=args.limit,
            check=lambda m: _all or (args.bot and m.author.bot) or
            (m.id == ctx.message.id) or (args.embeds and
                                         (m.embeds or m.attachments)) or
            (args.user and m.author.id == args.user.id) or
            (args.contains and args.contains.lower() in m.content.lower()))
        await self.bot.say('Deleted **{:,}** messages.'.format(
            len(deleted_messages)),
                           delete_after=5)
Esempio n. 8
0
        print("Epoch finished with loss", epoch_loss)
        losses.append(epoch_loss)

        if e % args.eval_step == 0:
            acc = evaluator.evaluate()
            accuracies.append(acc)
            best_acc = max(acc, best_acc)
            print("[Epoch {}] Accuracy:{:.2f}, Best Accuracy:{:.2f}".format(
                e, acc, best_acc))

        if e % args.save_step == 0:
            model.save_model(e)

        model.update_lr()

        plt.figure()
        plt.plot(range(len(losses)), losses)
        plt.xlabel('Epochs')
        plt.ylabel('Training Loss')
        plt.savefig(os.path.join(args.exp_dir, 'losses.png'))

        plt.figure()
        plt.plot(range(len(accuracies)), accuracies)
        plt.xlabel('Epochs')
        plt.ylabel('Test Accuracy')
        plt.savefig(os.path.join(args.exp_dir, 'accuracies.png'))


if __name__ == '__main__':
    args = Arguments().parse()
    main(args)
            formatted_link.append(parsed_link['scheme'])
        if not parsed_link['netloc']:
            formatted_link.append('travel.state.gov')
        else:
            formatted_link.append(parsed_link['netloc'])
        formatted_link.extend([
            parsed_link['path'], parsed_link['params'], parsed_link['query'],
            parsed_link['fragment']
        ])
        return build_url_from_parts(formatted_link)

    def get_country(self, links=None):
        for link in links:
            if link['link'].find('/country/') != -1:
                try:
                    return link['link'].split('/country/')[1].split(
                        '.html')[0].replace('-', ' ')
                except Exception as e:
                    logger.error(e)

    def get_region(self):
        pass


if __name__ == "__main__":
    args = Arguments()
    args.add_argument('--alerts')
    alerts_source = args.get('alerts', str())
    reader = TravelAlertsReader(alerts_path=alerts_source)
    reader.read_travel_alerts()
            if not parsed_link['netloc']:
                formatted_link.append('travel.state.gov')
            else:
                formatted_link.append(parsed_link['netloc'])
            formatted_link.extend([
                parsed_link['path'],
                parsed_link['params'],
                parsed_link['query'],
                parsed_link['fragment']
            ])
            return build_url_from_parts(formatted_link)

    def get_country(self, links=None):
        for link in links:
            if link['link'].find('/country/') != -1:
                try:
                    return link['link'].split('/country/')[1].split('.html')[0].replace('-', ' ')
                except Exception as e:
                    logger.error(e)

    def get_region(self):
        pass


if __name__ == "__main__":
    args = Arguments()
    args.add_argument('--alerts')
    alerts_source = args.get('alerts', str())
    reader = TravelAlertsReader(alerts_path=alerts_source)
    reader.read_travel_alerts()
Esempio n. 11
0
    transform = Augmentation.get('Transform')(
        args.size).train(args.command == 'train')
    dataset = datasets.ImageFolder(args.dataset, transform=transform)

    model = Model.new(args.backbone, len(dataset.classes), pretrained=True)

    model = executor.init(model, device, args)
    Path(args.dest).mkdir(exist_ok=True, parents=True)

    criterion = model.LOSS()
    optimizer = optim.SGD(model.parameters(),
                          lr=args.lr,
                          momentum=args.momentum,
                          weight_decay=args.decay)
    scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=7, gamma=.1)

    executor(
        model,
        dataset=dataset,
        criterion=criterion,
        optimizer=optimizer,
        scheduler=scheduler,  # train args
        device=device,
        args=args)


if __name__ == '__main__':
    arguments = Arguments()
    seed(arguments.seed)
    main(arguments)
Esempio n. 12
0
def main():
    args = Arguments()
    print(args.file())
    print("verbose=%s" % (str(args.verbose())))
    return 0
Esempio n. 13
0
    async def stats(self, ctx, *, msg=''):
        parser = Arguments(allow_abbrev=False, prog='stats')
        parser.add_argument(
            'user',
            nargs='?',
            type=user(ctx.message.channel.server),
            default=ctx.message.author,
            help=
            'The name of the user to look up Nitro Type stats for. (As a mention)'
        )
        await self.bot.send_typing(ctx.message.channel)
        args = await parser.do_parse(self.bot, msg)

        # Checking if parse was success
        if not args:
            return

        nitro_record = User.select().where(
            User.id == args.user.id).limit(1).first()

        # Checking if the is a user for the duser
        if not nitro_record:
            await self.bot.say(
                'That user does not have their Discord account linked to Nitro Type.'
            )
            return

        profile = await get_profile(nitro_record.nitro_name)

        # Checking if the profile was found
        if not profile:
            await self.bot.say(
                'Could not retrieve **%s**\'s Nitro Type account.' %
                nitro_record.nitro_name)
            return

        with Image.open('assets/images/stats_template.png') as i:
            d = ImageDraw.Draw(i)
            fnt_bold = ImageFont.truetype('assets/fonts/DroidSerif-Bold.ttf',
                                          22)
            fnt_reg = ImageFont.truetype('assets/fonts/DroidSerif-Regular.ttf',
                                         16)

            # Racing record text
            d.text((35, 96),
                   '{:,} WPM'.format(profile['avgSpeed']),
                   '#2f93db',
                   font=fnt_bold)
            d.text((194, 96),
                   '{:,} WPM'.format(profile['highestSpeed']),
                   '#2f93db',
                   font=fnt_bold)

            # Profile info text
            d.text((396, 96),
                   '${:,}'.format(profile['money'] + profile['moneySpent']),
                   '#44b938',
                   font=fnt_bold)
            d.text((578, 96),
                   '${:,}'.format(profile['moneySpent']),
                   '#44b938',
                   font=fnt_bold)
            d.text((761, 96),
                   '%s' % datetime.fromtimestamp(
                       profile['createdStamp']).strftime('%b %d, %Y'),
                   '#2f93db',
                   font=fnt_bold)

            d.text((87, 186),
                   'x{:,}'.format(profile['placed1']),
                   '#2f93db',
                   font=fnt_reg)
            d.text((193, 186),
                   'x{:,}'.format(profile['placed2']),
                   '#2f93db',
                   font=fnt_reg)
            d.text((299, 186),
                   'x{:,}'.format(profile['placed3']),
                   '#2f93db',
                   font=fnt_reg)

            fnt_reg.size = 10
            d.text((162, 262 - 4),
                   '{:,} races'.format(profile['racesPlayed']),
                   '#2f93db',
                   font=fnt_reg)
            d.text((162, 296 - 4),
                   '{:,} races'.format(profile['longestSession']),
                   '#2f93db',
                   font=fnt_reg)
            d.text((162, 330 - 4),
                   '{:,} nitros'.format(profile['nitrosUsed']),
                   '#2f93db',
                   font=fnt_reg)

            # Race summary text
            fnt_reg.size = 15
            offset = [0, 3, -38, -57]
            for x in range(4):
                b = profile['boards'][x]
                played = b.get('played', 0)
                rank = b.get('rank', 0)
                wpm = int(round(b['typed'] / 5 / (float(b['secs']) / 60))) \
                    if b.get('secs', False) is not False else 0

                acc = round(100 - b['errs'] / b['typed'] * 100, 2) \
                    if b.get('secs', False) is not False else 0

                d.text((246 + (x * 199) + offset[x], 509),
                       '{:,}'.format(played),
                       '#2f93db',
                       font=fnt_reg)
                d.text((246 + (x * 199) + offset[x], 557),
                       '{:,} WPM'.format(wpm),
                       '#2f93db',
                       font=fnt_reg)
                d.text((246 + (x * 199) + offset[x], 605),
                       '{}%'.format(acc),
                       '#2f93db',
                       font=fnt_reg)
                d.text((246 + (x * 199) + offset[x], 653),
                       '{:,}'.format(rank),
                       '#2f93db',
                       font=fnt_reg)

            del d
            del fnt_bold
            del fnt_reg
            with BytesIO() as b:
                i.save(b, 'PNG')
                b.seek(0)
                await self.bot.send_file(
                    ctx.message.channel,
                    b,
                    filename='stats.png',
                    content='<https://www.nitrotype.com/racer/%s>' %
                    profile['username'])

        gc.collect()
Esempio n. 14
0
        metadata = dict()
        metadata['title'] = self.get_title()
        metadata['subtitle'] = self.get_subtitle()
        metadata['description'] = self.get_description()
        return metadata

    def get_title(self):
        return self.raw_feed.feed.title

    def get_subtitle(self):
        return self.raw_feed.feed.subtitle

    def get_description(self):
        return self.raw_feed.feed.description

    def get_feed_entries(self):
        return self.raw_feed.entries

    def read_feed(self):
        feed = dict()
        feed['metadata'] = self.get_feed_metadata()
        feed['entries'] = self.get_feed_entries()
        return feed


if __name__ == "__main__":
    args = Arguments()
    args.add_argument('--feed')
    feed_source = args.get('feed', str())
    reader = FeedReader(feed_path=feed_source)
Esempio n. 15
0
        metadata = dict()
        metadata['title'] = self.get_title()
        metadata['subtitle'] = self.get_subtitle()
        metadata['description'] = self.get_description()
        return metadata

    def get_title(self):
        return self.raw_feed.feed.title

    def get_subtitle(self):
        return self.raw_feed.feed.subtitle

    def get_description(self):
        return self.raw_feed.feed.description

    def get_feed_entries(self):
        return self.raw_feed.entries

    def read_feed(self):
        feed = dict()
        feed['metadata'] = self.get_feed_metadata()
        feed['entries'] = self.get_feed_entries()
        return feed


if __name__ == "__main__":
    args = Arguments()
    args.add_argument('--feed')
    feed_source = args.get('feed', str())
    reader = FeedReader(feed_path=feed_source)
Esempio n. 16
0
    async def racer(self, ctx, *, msg=''):
        parser = Arguments(allow_abbrev=False, prog='racer')
        parser.add_argument(
            'user',
            type=user(ctx.message.channel.server),
            default=ctx.message.author,
            nargs='?',
            help=
            'The name of the user to look up Nitro racer card for. (As a mention)'
        )

        await self.bot.send_typing(ctx.message.channel)
        args = await parser.do_parse(self.bot, msg)

        # Checking if parse was success
        if not args:
            return

        nitro_record = User.select().where(
            User.id == args.user.id).limit(1).first()

        # Checking if the is a user for the duser
        if not nitro_record:
            await self.bot.say(
                'That user does not have their Discord account linked to Nitro Type.'
            )
            return

        profile = await get_profile(nitro_record.nitro_name)

        # Checking if the profile was found
        if not profile:
            await self.bot.say(
                'Could not retrieve **%s**\'s Nitro Type account.' %
                nitro_record.nitro_name)
            return

        with Image.open('assets/images/racer_template.png') as i:
            i.convert('RGBA')
            fnt_12 = ImageFont.truetype('assets/fonts/DroidSerif-Regular.ttf',
                                        12)
            fnt_16 = ImageFont.truetype('assets/fonts/DroidSerif-Regular.ttf',
                                        16)
            fnt_18 = ImageFont.truetype('assets/fonts/DroidSerif-Regular.ttf',
                                        18)
            d = ImageDraw.Draw(i)

            # Preparing data
            is_gold = profile['membership'] == 'gold'
            top3 = len([
                b for b in profile['boards']
                if b.get('rank', 0) <= 3 and b.get('rank', 0) != 0
            ]) > 0
            money = '${:,}'.format(profile['money'])
            nitros = '{:,} Nitros'.format(profile['nitros'])
            car_name = get_car_name(profile['carID'])
            title = profile['title']
            name = ('' if not profile['tag'] else '[%s] ' % profile['tag'])\
                   + (profile['displayName'] or profile['username'])

            # Applying banner
            img = 'assets/images/banner_gold.png' if is_gold else 'assets/images/banner_silver.png'
            with Image.open(img) as b:
                i.paste(b, (1, 13), b)

            # Applying top 3
            if top3:
                with Image.open('assets/images/scoreboard_champion.png') as b:
                    i.paste(b, (31, 111), b)

            # Title stuff
            d.text((149 - fnt_18.getsize(name)[0] / 2, 66),
                   name,
                   fill='#333' if is_gold else '#2f93db',
                   font=fnt_18)
            d.text((149 - fnt_12.getsize(title)[0] / 2, 41),
                   profile['title'],
                   fill='#333',
                   font=fnt_12)
            d.text((338, 41),
                   str(profile['level']),
                   fill='#2f93db',
                   font=fnt_18)

            if is_gold:
                d.text((338, 73),
                       'Nitro Type Gold Member',
                       fill='#fed034',
                       font=fnt_16)

            # Stats stuff
            d.text((31, 173), car_name, fill='#2f93db', font=fnt_16)
            d.text((31, 221), money, fill='#44b938', font=fnt_16)
            d.text((31, 269), nitros, fill='#2f93db', font=fnt_16)

            # Drawing car
            car = await get_car(profile['carID'], profile['carHueAngle'])
            with Image.open(car) as c:
                with c.convert('RGBA') as c1:
                    i.paste(c1, (599 - c.size[0], 280 - c.size[1]), c1)

            car.close()

            del fnt_12, fnt_16, fnt_18, d, car
            with BytesIO() as b:
                i.save(b, 'PNG')
                b.seek(0)
                await self.bot.send_file(
                    ctx.message.channel,
                    b,
                    filename='racer.png',
                    content='<https://www.nitrotype.com/racer/%s>' %
                    profile['username'])

        gc.collect()