Beispiel #1
0
 def _gen_report(self, data, format, country_stats):
     """For a given set of data, format and country_stats, return the corresponding report."""
     # first checking if any data
     if len(data) == 0:
         return data
     
     # checking format
     if format == 'json':
         return data
     elif format == 'txt':
         if country_stats:
             headers = ['GeoIP', 'Count']
             to_return = data.items()
             return tabulate(to_return, headers=headers)
         else:
             headers = [inflection.humanize(_) for _ in data[0].keys()]
             to_return = [
                 [value for key, value in _.items()]
                 for _ in data
             ]
             return tabulate(to_return, headers=headers)
     elif format == 'csv':
         if country_stats:
             headers = ['GeoIP', 'Count']
             to_return = [';'.join([key, str(value)]) for key, value in data.items()]
             return ';'.join(headers) + '\n' + '\n'.join(to_return)
         else:
             headers = [inflection.humanize(_) for _ in data[0].keys()]
             to_return = [
                 ';'.join([value for key, value in _.items()])
                 for _ in data
             ]
             return ';'.join(headers) + '\n' + '\n'.join(to_return)
     else:
         return None
    def on_begin(self, start_epoch=None, end_epoch=None, metrics_name=None):
        self.modes, self.metrics = zip(
            *[metric.split('_', 1) for metric in metrics_name])

        self.metrics = list(OrderedDict.fromkeys(self.metrics))
        self.modes = list(OrderedDict.fromkeys(self.modes))
        self.modes = self.modes or ['']

        self.viz_windows = {m: None for m in self.metrics}

        self.opts = {
            m: dict(title=titleize(humanize(m)),
                    ylabel=titleize(humanize(m)),
                    xlabel='Epoch',
                    legend=[titleize(mode) for mode in self.modes])
            for m in self.metrics
        }

        if not len(self.history):
            for name in metrics_name:
                self.history[name] = np.zeros(end_epoch)
        self.history['epochs'] = np.arange(1, end_epoch + 1)

        if start_epoch != 0:
            for m in self.metrics:
                self.viz_windows[m] = self.viz.line(
                    X=np.column_stack([
                        self.history['epochs'][0:start_epoch]
                        for _ in self.modes
                    ]),
                    Y=np.column_stack([
                        self.history['{}_{}'.format(mode, m)][0:start_epoch]
                        for mode in self.modes
                    ]),
                    opts=self.opts[m])
Beispiel #3
0
def plot_history_metric(history_dict, metric_name, save_dir):
    metric = history_dict[metric_name]
    val_metric = history_dict['val_{}'.format(metric_name)]
    epochs = range(1, len(metric) + 1)
    plt.plot(epochs,
             metric,
             '--g',
             label='Training {}'.format(inflection.humanize(metric_name)))
    plt.plot(epochs,
             val_metric,
             '--r',
             label='Validation {}'.format(inflection.humanize(metric_name)))
    # plt.plot(epochs, acc, '-g', label='Training acc')
    # plt.plot(epochs, val_acc, '-r', label='Validation acc')
    plt.title('{} over time'.format(metric_name))
    plt.xlabel('Epochs')
    plt.ylabel(metric_name)
    metric_file_path = '{}{}.svg'.format(save_dir,
                                         inflection.underscore(metric_name))
    if save_dir:
        io.gfile.makedirs(save_dir)
        plt.savefig(metric_file_path)
    plt.legend()
    plt.show()
    return metric_file_path
Beispiel #4
0
def run_cases(
    prob: problem.Problem,
    working_dir: pathlib.Path,
    test_ids: Iterable[str],
) -> bool:
    assert (working_dir.is_dir())

    with color_utils.ColorizeStderrBar1():
        print('=' * 20, 'Executing', prob.name, '=' * 20, file=sys.stderr)
    print('Command:', prob.run_command, file=sys.stderr)

    test_ids = sorted(test_ids)
    if not test_ids:
        with color_utils.ColorizeStderrWarning():
            print(
                'No test cases to run! Generate some with '
                '"pcu setin" and "pcu setans".',
                file=sys.stderr)
        return True

    prob_test_ids = set(prob.get_test_ids())
    for test_id in test_ids:
        if test_id not in prob_test_ids:
            with color_utils.ColorizeStderrError():
                print('ERROR: Test case',
                      test_id,
                      'not found for problem',
                      prob.name,
                      file=sys.stderr)
                return False

    print('Number of testcases:', len(test_ids), file=sys.stderr)

    result_counts = \
        collections.defaultdict(int)  # type: DefaultDict[TestCaseResult, int]

    for test_id in test_ids:
        with color_utils.ColorizeStderrBar2():
            print('->', 'Test case', test_id, '-' * 20, file=sys.stderr)
        test_result = _run_case(prob, working_dir, test_id)
        result_counts[test_result] += 1

        with color_utils.ColorizeStderr(*test_result.get_colorize_colors()):
            print('Status:', inflection.humanize(test_result.name))

    with color_utils.ColorizeStderrBar2():
        print('-' * 16, 'Summmary', '-' * 16, file=sys.stderr)

    for value in TestCaseResult:
        with color_utils.ColorizeStderr(*value.get_colorize_colors()):
            print('{:25}'.format(inflection.humanize(value.name)),
                  file=sys.stderr,
                  end='')
            print('{:>7} ({:5.1f}%)'.format(
                result_counts[value],
                result_counts[value] * 100.0 / len(test_ids)),
                  file=sys.stderr)

    return True
Beispiel #5
0
def run_cases(prob: problem.Problem,
              working_dir: pathlib.Path,
              test_ids: Iterable[str],
) -> bool:
    assert(working_dir.is_dir())

    with color_utils.ColorizeStderrBar1():
        print('=' * 20, 'Executing', prob.name, '=' * 20,
              file=sys.stderr)
    print('Command:', prob.run_command,
          file=sys.stderr)

    test_ids = sorted(test_ids)
    if not test_ids:
        with color_utils.ColorizeStderrWarning():
            print('No test cases to run! Generate some with '
                  '"pcu setin" and "pcu setans".',
                  file=sys.stderr)
        return True

    prob_test_ids = set(prob.get_test_ids())
    for test_id in test_ids:
        if test_id not in prob_test_ids:
            with color_utils.ColorizeStderrError():
                print('ERROR: Test case', test_id, 'not found for problem',
                      prob.name,
                      file=sys.stderr)
                return False

    print('Number of testcases:', len(test_ids),
          file=sys.stderr)

    result_counts = \
        collections.defaultdict(int)  # type: DefaultDict[TestCaseResult, int]

    for test_id in test_ids:
        with color_utils.ColorizeStderrBar2():
            print('->', 'Test case', test_id, '-' * 20,
                  file=sys.stderr)
        test_result = _run_case(prob, working_dir, test_id)
        result_counts[test_result] += 1

        with color_utils.ColorizeStderr(*test_result.get_colorize_colors()):
            print('Status:', inflection.humanize(test_result.name))

    with color_utils.ColorizeStderrBar2():
        print('-' * 16, 'Summmary', '-' * 16,
              file=sys.stderr)

    for value in TestCaseResult:
        with color_utils.ColorizeStderr(*value.get_colorize_colors()):
            print('{:25}'.format(inflection.humanize(value.name)),
                  file=sys.stderr, end='')
            print('{:>7} ({:5.1f}%)'.format(result_counts[value],
                                   result_counts[value] * 100.0 / len(test_ids)),
              file=sys.stderr)

    return True
Beispiel #6
0
def mangle(original):
    title = original.istitle()
    human = inflection.humanize(original) == original

    mangled = ' '.join(map(mangle_word, original.split()))

    if title:
        return inflection.titleize(mangled)
    if human:
        return inflection.humanize(mangled)
    return mangled
Beispiel #7
0
 async def middleware(self, request, handler):
     try:
         if hasattr(handler, 'config'):
             handler.config = self.config
         return await handler(request)
     except Exception as exc:
         status = exc.status if hasattr(exc, 'status') else 400
         if isinstance(exc, (RuntimeError, TypeError, AttributeError,
                             AssertionError, KeyError)):
             logging.exception(exc)
             status = 500
         error = exc.error if hasattr(exc,
                                      'error') else inflection.underscore(
                                          type(exc).__name__).upper()
         message = exc.message if hasattr(
             exc,
             'message') else inflection.humanize(error).capitalize() + '.'
         detail = exc.detail if hasattr(exc, 'detail') else None
         if isinstance(exc, ValidationError):
             detail = exc.messages
         elif isinstance(exc, JSONDecodeError):
             detail = exc.msg
         return json_response(status=status,
                              data=dict(error=error,
                                        message=message,
                                        detail=detail))
 def get_success_message(self):
     """
     Returns the formatted success message (if any)
     """
     return self.success_message % dict(
         model=humanize(self.model_class.__name__)
     )
Beispiel #9
0
def send_fail_message(config_entity, bundle, job, publisher_name, signal_proportion_lookup_name, user):
    job.status = "Failed"
    exc_type, exc_value, exc_traceback = sys.exc_info()
    readable_exception = traceback.format_exception(exc_type, exc_value, exc_traceback)
    job.data = readable_exception
    event = 'postSavePublisherFailed'
    logger.error("Sending Failed message %s for signal %s to user %s with %s readable_exception %s" % \
        (event, signal_proportion_lookup_name, user.username, unicode(bundle), readable_exception))

    send_message_to_client(user.id, dict(
        event=event,
        config_entity_id=config_entity and config_entity.id,
        config_entity_class_name=config_entity and config_entity.__class__.__name__,
        # Send the key since the id of new instances might be meaningless to the client
        # If it hasn't updated the record's id yet
        class_name=bundle.class_name_for_client,
        ids=bundle.client_instance_ids,
        keys=bundle.keys,
        # Used for Features and other things that have a class scope key
        class_key=bundle.class_key,
        publisher_name=publisher_name,
        # The client can display a capitalized version of this to describe the progress
        progress_description=humanize(signal_proportion_lookup_name),
        trace=readable_exception)
    )
    return readable_exception
Beispiel #10
0
    def create(self, name):
        docstring = name.rstrip('.')
        name = re.sub('\W', '_', name.rstrip('.'))

        if re.match('^\w*$', docstring):
            # docstring seems to not be a sentence but a camelcase classname
            # or a underscored upgrade directory name.
            # Lets make it human readable for use as docstring.
            docstring = inflection.humanize(
                inflection.underscore(name))

        step_name = '{0}_{1}'.format(
            datetime.now().strftime(DATETIME_FORMAT),
            inflection.underscore(name))
        step_directory = os.path.join(self.upgrades_directory, step_name)
        os.mkdir(step_directory)

        Path(step_directory).joinpath('__init__.py').touch()

        code_path = os.path.join(step_directory, 'upgrade.py')
        with open(code_path, 'w+') as code_file:
            code_file.write(
                PYTHON_TEMPLATE.format(
                    classname=inflection.camelize(name),
                    docstring=docstring))

        return step_directory
Beispiel #11
0
def app(name, path, author, pubget):
    """To Create App."""
    click.echo("Creating name %s ..." % name)
    app_create(name, path, inflection.humanize(author))
    click.echo("App %s Created" % name)
    if pubget:
        subprocess.run('pub get', shell=True)
    def on_step_end(self):
        msg = []

        if self.mode.startswith('train'):
            msg += [
                'Epoch: [{0}][{1}/{2}]  '.format(self.epoch, self.batch,
                                                 self.size)
            ]
        else:
            msg += [
                '{0}: [{1}/{2}]  '.format(titleize(self.mode), self.batch,
                                          self.size)
            ]
        msg += ['Time {0.sum:.3f}  '.format(self.batch_time)]
        msg += ['Data {0.sum:.3f}  '.format(self.data_time)]

        # Add metrics alongsise with the loss
        msg += [
            '{0} {1.avg:.3f}  '.format(
                titleize(humanize(name.rsplit('_')[1].replace('-score', ''))),
                meter) for name, meter in self.metrics.items()
            if not name.startswith('_')
        ]

        print(''.join(msg))
Beispiel #13
0
def send_fail_message(config_entity, bundle, job, publisher_name,
                      signal_proportion_lookup_name, user):
    job.status = "Failed"
    exc_type, exc_value, exc_traceback = sys.exc_info()
    readable_exception = traceback.format_exception(exc_type, exc_value,
                                                    exc_traceback)
    job.data = readable_exception
    event = 'postSavePublisherFailed'
    logger.error("Sending Failed message %s for signal %s to user %s with %s readable_exception %s" % \
        (event, signal_proportion_lookup_name, user.username, unicode(bundle), readable_exception))

    send_message_to_client(
        user.id,
        dict(
            event=event,
            config_entity_id=config_entity and config_entity.id,
            config_entity_class_name=config_entity
            and config_entity.__class__.__name__,
            # Send the key since the id of new instances might be meaningless to the client
            # If it hasn't updated the record's id yet
            class_name=bundle.class_name_for_client,
            ids=bundle.client_instance_ids,
            keys=bundle.keys,
            # Used for Features and other things that have a class scope key
            class_key=bundle.class_key,
            publisher_name=publisher_name,
            # The client can display a capitalized version of this to describe the progress
            progress_description=humanize(signal_proportion_lookup_name),
            trace=readable_exception))
    return readable_exception
    def on_batch_end(self, metrics):
        # measure elapsed time
        self.batch_time.update(time.time() - self.end)
        self.end = time.time()
        if self.batch % self.print_freq == 0:
            msg = []
            if self.mode.startswith('train'):
                msg += [
                    'Epoch: [{0}][{1}/{2}]  '.format(self.epoch, self.batch,
                                                     self.size)
                ]
            else:
                msg += [
                    '{0}: [{1}/{2}]  '.format(titleize(self.mode), self.batch,
                                              self.size)
                ]
            msg += ['Time {0.val:.3f} ({0.avg:.3f})  '.format(self.batch_time)]
            msg += ['Data {0.val:.3f} ({0.avg:.3f})  '.format(self.data_time)]

            # Add metrics alongsise with the loss
            msg += [('{0} {1.val:.3f} ({1.avg:.3f})  ' if isinstance(
                meter, AverageMeter) else '{0} {1.val:.3f} ').format(
                    titleize(
                        humanize(name.rsplit('_')[1].replace('-score', ''))),
                    meter) for name, meter in metrics.items()
                    if not name.startswith('_')]

            print(''.join(msg))
            self.metrics = metrics
 def get_failure_message(self):
     """
     Returns the formatted failure message (if any)
     """
     return self.failure_message % dict(
         model=humanize(self.model_class.__name__)
     )
Beispiel #16
0
    async def act(self, ctx, *, target: Union[discord.Member, str] = None):
        """
        Acts on the specified user.
        """
        if not target or isinstance(target, str):
            return  # no help text

        action = inflection.humanize(ctx.invoked_with).split()
        iverb = -1

        for cycle in range(2):
            if iverb > -1:
                break
            for i, act in enumerate(action):
                act = act.lower()
                if (act in NOLY_ADV or act in CONJ
                        or (act.endswith("ly") and act not in LY_VERBS)
                        or (not cycle and act in SOFT_VERBS)):
                    continue
                action[i] = inflection.pluralize(action[i])
                iverb = max(iverb, i)

        if iverb < 0:
            return
        action.insert(iverb + 1, target.mention)
        await ctx.send(italics(" ".join(action)))
Beispiel #17
0
def display_str(x):
    if x == "MNI152NLin6Asym":
        return "MNI ICBM 152 non-linear 6th Generation Asymmetric (FSL)"
    elif x == "MNI152NLin2009cAsym":
        return "MNI ICBM 2009c Nonlinear Asymmetric"
    elif x == "slice_encoding_direction":
        return "slice acquisition direction"
    return humanize(x)
    async def member_declined(
        self,
        bot: GroupCommonBot,
        member: GroupMember,
    ):
        debug(humanize(get_current_function_name()))

        await bot.group_msg(Text(f"{member.user_id}已退群"), )
Beispiel #19
0
def format_titles(lists_from_posts):
	titles = []
	for title in lists_from_posts:
		titles_with_underscores = inflection.underscore(title)
		titles_humanized = inflection.humanize(titles_with_underscores)
		
		titles.append(titles_humanized.title())

	return titles
    def humanize(cls, val: str) -> str:
        """

        Don't use the humanize function for names with punctuation,
        as humanize may make them worse. For instance "Anne-marie"
        instead of "Anne-Marie".
        """
        if re.fullmatch(cls._can_humanize_expr, val):
            return humanize(val)
        return val
Beispiel #21
0
def get_string_from_item_node(item_node):
    item_class = item_node['class']
    result = item_node['label']
    if result.startswith('object_'):
        result = result[len('object_'):]
    elif result.startswith('person_'):
        result = result[len('person_'):]
    result = inflection.underscore(result)
    if item_class == 'unknown':
        return None
    elif item_class == 'person':
        result = inflection.humanize(result)
        result = inflection.titleize(result)
    elif item_class == 'video':
        result = 'the video'
    if item_class != 'person':
        result = inflection.humanize(result)
        result = result.lower()
    return result
Beispiel #22
0
	def iprint(self,msg,key):

		if key=="LOCATION":
			for data in msg:
				addr=humanize(data)
				addr=addr.decode('UTF-8')
				print "* ",addr
		else:		
			addr=' '.join(msg)
			print addr.decode('UTF-8')
			print " "
Beispiel #23
0
def _get_fe_aggregate(ctx, inputname, across):
    assert all(entity in aggregate_order for entity in across)

    for obj in ctx.spec.models:
        if obj.type != "fe":
            continue
        if hasattr(obj, "filters") and obj.filters is not None and len(
                obj.filters) > 0:
            continue
        if inputname not in obj.inputs or len(obj.inputs) > 1:
            continue
        if not hasattr(obj, "across"):
            continue

        obj_across = _resolve_across(ctx, obj.name)
        if tuple(obj_across) != tuple(across):
            continue

        return obj.name

    # need to create

    display_strs = [
        entity_display_aliases[entity]
        if entity in entity_display_aliases else entity for entity in across
    ]
    acrossstr = " then ".join(
        [p.plural(display_str) for display_str in display_strs])
    inputname_with_spaces = humanize(underscore(inputname))
    basename = format_like_bids(
        f"aggregate {inputname_with_spaces} across {acrossstr}")

    entity = across.pop()

    if len(across) > 0:
        inputname = _get_fe_aggregate(ctx, inputname, across)

    aggregatename = basename
    analysis_names = set(model_obj.name for model_obj in ctx.spec.models)
    i = 0
    while aggregatename in analysis_names:  # assure unique name
        aggregatename = f"{basename}{i}"
        i += 1

    modelobj = FixedEffectsModelSchema().load({
        "name": aggregatename,
        "inputs": [inputname],
        "type": "fe",
        "across": entity
    })
    assert isinstance(modelobj, Model)
    ctx.spec.models.insert(-1, modelobj)

    return modelobj.name
Beispiel #24
0
        def setup(self, ctx):
            self.is_first_run = True
            self.choice = None

            bold_filedict = {"datatype": "func", "suffix": "bold"}
            filepaths = ctx.database.get(**bold_filedict)
            self.filepaths = list(filepaths)
            assert len(self.filepaths) > 0

            db_entities, db_tags_set = ctx.database.multitagvalset(
                entities, filepaths=self.filepaths)

            self.entities = []
            options = []
            self.tagval_by_str = {}
            values = []
            for entity, tagvals_list in zip(db_entities, zip(*db_tags_set)):
                if entity == "sub":
                    continue

                tagvals_set = set(tagvals_list)
                if len(tagvals_set) > 1:
                    self.entities.append(entity)

                    entity_str = entity
                    if entity_str in entity_display_aliases:
                        entity_str = entity_display_aliases[entity_str]
                    entity_str = humanize(entity_str)
                    options.append(
                        TextElement(entity_str, color=entity_colors[entity]))

                    if None in tagvals_set:
                        tagvals_set.remove(None)

                    tagvals = sorted(list(tagvals_set))

                    row = [f'"{tagval}"' for tagval in tagvals]
                    values.append(row)

                    self.tagval_by_str.update(dict(zip(row, tagvals)))

            if len(options) == 0:
                self.should_run = False
            else:
                self.should_run = True
                self._append_view(TextView("Specify images to use"))

                self.input_view = MultiMultipleChoiceInputView(options,
                                                               values,
                                                               checked=values)

                self._append_view(self.input_view)
                self._append_view(SpacerView(1))
Beispiel #25
0
    def load_term(self, o_term, ontology, session, process_relation=True):
        """
        :param o_term:
        :param ontology:
        :param session:
        :param process_relation:
        :return: Term
        """
        if type(ontology) is str:
            m_ontology = self.load_ontology(ontology, session,
                                            o_term.namespace)
        elif isinstance(ontology, Ontology):
            m_ontology = ontology
        elif isinstance(ontology, helpers.Ontology):
            m_ontology, created = get_one_or_create(
                Ontology,
                session,
                name=ontology.ontology_id.upper(),
                namespace=o_term.namespace)
        else:
            raise RuntimeError('Wrong parameter')
        session.merge(m_ontology)
        self.current_ontology = m_ontology.name
        logger = self.get_term_logger(self.current_ontology)
        logger.info("Loading term details %s", o_term)
        if has_accession(o_term):
            if not o_term.description:
                o_term.description = [inflection.humanize(o_term.label)]
            m_term, created = get_one_or_create(Term,
                                                session,
                                                accession=o_term.accession,
                                                create_method_kwargs=dict(
                                                    helper=o_term,
                                                    ontology=m_ontology))

            logger.info('Loaded Term [%s][%s][%s]', m_term.accession,
                        o_term.namespace, m_term.iri)
            if created:
                self.load_term_subsets(m_term, session)
                self.load_alt_ids(m_term, o_term, session)
                self.load_term_synonyms(m_term, o_term, session)
                if o_term.ontology_name.upper() in self.allowed_ontologies \
                        and self.options.get('process_relations', True) \
                        and process_relation:
                    self.load_term_relations(m_term, o_term, session)
                if not m_term.is_root and self.options.get(
                        'process_parents', True):
                    self.load_term_ancestors(m_term, o_term, session)
            return m_term
        else:
            logger.info("O_term %s has no accession", o_term)
            return None
Beispiel #26
0
    def parse_schema_dict(self, data):
        """Convert the responses' JSON into a dictionary of resources"""
        schema = {}
        for key in data:
            name = key.split('_url')[0]
            if key.endswith('_url'):
                if data[key]:
                    schema[name] = Resource(self.session, url=data[key],
                                            name=humanize(name))
                else:
                    schema[name] = data[key]
            else:
                data_type = type(data[key])
                if data_type == dict:
                    schema[name] = Resource(self.session, schema=data[key],
                                            name=humanize(name))
                elif data_type == list:
                    schema[name] = self.parse_schema_list(data[key], name=name)
                else:
                    schema[name] = data[key]

        return schema
    def accumulate_nodes(accum, raw_node, i):
        """
            Accumulate each node, keying by the name of the node's stage key
            Since nodes share stage keys these each result is an array of nodes
        :param accum:
        :param raw_node:
        :param i:
        :return:
        """
        location_obj = resolve_coordinates(default_location, R.prop_or(None, location_key, raw_node), i)
        location = R.prop('location', location_obj)
        is_generalized = R.prop('isGeneralized', location_obj)
        # The key where then node is stored is the stage key
        node_stage = raw_node[stage_key]
        # Get key from name or it's already a key
        key = R.prop('key', R.prop_or(dict(key=node_stage), node_stage, stage_by_name))

        # Copy all properties from resource.data  except settings and raw_data
        # Also grab raw_node properties
        # This is for arbitrary properties defined in the data
        # We put them in properties and propertyValues since graphql hates arbitrary key/values
        properties = R.merge(
            R.omit(['settings', 'rawData'], R.prop('data', resource)),
            raw_node
        )
        properties[node_name_key] = humanize(properties[node_name_key])
        return R.merge(
            # Omit accum[key] since we'll concat it with the new node
            R.omit([key], accum),
            {
                # concat accum[key] or [] with the new node
                key: R.concat(
                    R.prop_or([], key, accum),
                    # Note that the value is an array so we can combine nodes with the same stage key
                    [
                        dict(
                            value=string_to_float(R.prop(value_key, raw_node)),
                            type='Feature',
                            geometry=dict(
                                type='Point',
                                coordinates=location
                            ),
                            name=R.prop(node_name_key, raw_node),
                            isGeneralized=is_generalized,
                            properties=list(R.keys(properties)),
                            propertyValues=list(R.values(properties))
                        )
                    ]
                )
            }
        )
Beispiel #28
0
    def get_all_fields(self):
        """Returns the entire serializer field set.

        Does not respect dynamic field inclusions/exclusions.
        """
        if not hasattr(self, '_all_fields'):
            self._all_fields = super(WithDynamicSerializerMixin,
                                     self).get_fields()
            for k, field in six.iteritems(self._all_fields):
                field.field_name = k
                label = inflection.humanize(k)
                field.label = getattr(field, 'label', label) or label
                field.parent = self
        return self._all_fields
    async def member_increased(
        self,
        bot: GroupCommonBot,
        member: GroupMember,
    ):
        debug(humanize(get_current_function_name()))

        await bot.group_msg(
            Text("欢迎"),
            At(member.user_id),
            Image(
                f"https://q4.qlogo.cn/headimg_dl?dst_uin={member.user_id}&spec=640"
            ),
            Text("进群"),
        )
Beispiel #30
0
def make_xls(formNames):
	wb = xlwt.Workbook()

	for sheet in formNames:
		# create worksheet
		worksheetTitle = sheet[0:30]
		wa = wb.add_sheet(inflection.humanize(worksheetTitle))
		num_width = 0
		titleArray = []
		formData = []
		
		# put the json data to array
		for idx1, data1 in enumerate(formNames[sheet]):
			formData.append([])
			formData[idx1] = {}
			for idx2, data2 in enumerate(data1):
				if not data2['name'] in titleArray:
					titleArray.insert(idx2, data2['name'])
				value = data2.get('value')
				if value is None:
					value = '-'
				formData[idx1][data2['name']] = value

		# write al data to worksheet
		for idx1, data1 in enumerate(formData):
			for idx2, data2 in enumerate(titleArray):
				if idx1 == 0:
					wa.write(0, idx2, inflection.titleize(data2), style0)
				if data2 in data1:
					value = data1[data2]
					wa.col(idx2).width = get_width(len(value)) if get_width(len(value)) > wa.col(idx2).width else wa.col(idx2).width
					wa.write(idx1+1, idx2, inflection.humanize(value), style1)
				else:
					wa.write(idx1+1, idx2, '-', style1)

	return wb
Beispiel #31
0
def create_xls(result_json, xlsfile, wb):
	all_form = {}

	for row in result_json:
		if not row["formName"] in all_form:
			all_form[row["formName"]] = []
		jsondata = (json.loads(row["formInstance"]))
		jsonfield = list()
		jsonfield.append({'name': "UserID", 'value': row["anmId"]})
		jsonfield.append({'name': bindTypesId.get(jsondata["form"]["bind_type"], "none"), 'value': row["entityId"]})
		jsonfield.extend(jsondata["form"]["fields"])
		jsonfield.append({'name': "clientVersionSubmissionDate", 'value': datetime.fromtimestamp(int(row["clientVersion"])/1000.0).strftime('%Y-%m-%d %H:%M:%S')})
		jsonfield.append({'name': "serverVersionSubmissionDate", 'value': datetime.fromtimestamp(int(row["serverVersion"])/1000.0).strftime('%Y-%m-%d %H:%M:%S')})
		all_form[row["formName"]].append(jsonfield)

	for sheet in all_form:
		# create worksheet
		worksheet_title = sheet[0:30]
		wa = wb.add_sheet(inflection.humanize(worksheet_title))
		title_array = []
		form_data = []

		# put the json data to array
		for idx1, data1 in enumerate(all_form[sheet]):
			form_data.append([])
			form_data[idx1] = {}
			for idx2, data2 in enumerate(data1):
				if data2['name'] != 'id':
					if not data2['name'] in title_array:
						title_array.insert(idx2, data2['name'])
					value = data2.get('value')
					if value is None:
						value = '-'
					form_data[idx1][data2['name']] = value

		# write al data to worksheet
		for idx1, data1 in enumerate(form_data):
			for idx2, data2 in enumerate(title_array):
				if idx1 == 0:
					wa.write(0, idx2, data2, style0)
				if data2 in data1:
					value = data1[data2]
					wa.col(idx2).width = get_width(len(value)) if get_width(len(value)) > wa.col(idx2).width else wa.col(idx2).width
					wa.write(idx1+1, idx2, value, style1)
				else:
					wa.write(idx1+1, idx2, '-', style1)

	return xls_to_response(wb, xlsfile.response_username+'.xls')
def get_concepts(dfs):
    concepts = set()
    concept_types = {}
    names = {}
    for df in dfs:
        for col in df.columns:
            if col not in concepts:
                concepts.add(col)
                concept_types[col] = get_concept_type(df[col])
                names[col] = humanize(col)

    return pd.DataFrame({
        'concept': list(concepts),
        'concept_type': [concept_types[c] for c in concepts],
        'name': [names[c] for c in concepts]
    })
Beispiel #33
0
    def _create_upgrade(self):
        name = self.name.replace(' ', '_').replace('\.$', '')
        step_name = '{0}_{1}'.format(self.destination_version,
                                     inflection.underscore(name))
        if self.code is None:
            self.code = scaffold.PYTHON_TEMPLATE.format(
                classname=inflection.camelize(name),
                docstring=inflection.humanize(inflection.underscore(name)))

        self.package.with_file(os.path.join(step_name, '__init__.py'),
                               '',
                               makedirs=True)

        self.package.with_file(os.path.join(step_name, 'upgrade.py'),
                               self.code)
        return step_name
Beispiel #34
0
def save_video_details(video, details):
    """
    :type video: Video
    :type details: dict
    """

    for found_tag in details.get('tags'):
        slug = parameterize(found_tag.strip())
        tag = humanize(slug)

        if not slug:
            continue

        tag, created = Tag.get_or_create(tag=tag, slug=slug)

        VideoTag.get_or_create(video=video, tag=tag)
Beispiel #35
0
def _field_type(preference, required=True, choices=None):
    """ Get the correct Django forms field based on the provided value """

    platforms = ['mac', 'linux', 'windowsxp', 'windowsnewerthanxp']
    label = preference.description
    if not label or any(preference.name.startswith(platform) for platform in platforms):
        label = inflection.humanize(inflection.underscore(preference.name))

    if 'Windowsxp' in label:
        label = label.replace('Windowsxp', 'Windows XP')
    if 'Windowsnewerthanxp' in label:
        label = label.replace('Windowsnewerthanxp', 'Windows Vista+')

    if preference.field_type == 'string[]':
        widget = forms.Textarea()
        if preference.name in ['macAdvancedBackupSelectionSelected',
                               'macAdvancedBackupSelectionDeselected']:
            widget.attrs['placeholder'] = '$HOME/Pictures\n$HOME/Documents'
        if preference.name in ['linuxAdvancedBackupSelectionSelected',
                               'linuxAdvancedBackupSelectionDeselected']:
            widget.attrs['placeholder'] = '$HOME/pictures\n$HOME/documents'
        if preference.name in ['windowsxpAdvancedBackupSelectionSelected',
                               'windowsxpAdvancedBackupSelectionDeselected']:
            widget.attrs['placeholder'] = '%USERPROFILE%\My Documents\n%USERPROFILE%\My Pictures'
        if preference.name in ['windowsnewerthanxpAdvancedBackupSelectionSelected',
                               'windowsnewerthanxpAdvancedBackupSelectionDeselected']:
            widget.attrs['placeholder'] = '%USERPROFILE%\Documents\n%USERPROFILE%\Pictures'
        return ListField(required=required, label=label, widget=widget)

    if preference.field_type == 'string':
        if choices:
            return forms.ChoiceField(
                choices=_build_choices(choices),
                required=required,
                label=label
            )
        return forms.CharField(required=required, label=label)

    if preference.field_type == 'integer':
        return forms.IntegerField(required=required, label=label, min_value=0)

    if preference.field_type == 'boolean':
        return forms.BooleanField(required=False, label=label)

    LOG.error("Unable to get field type. {} is an invalid option".format(preference.field_type))  # NOQA
Beispiel #36
0
 def handle_search_exception(e: Exception, context: RenderingContext):
     if isinstance(e, ValidationError):
         context.status_code = 400
         bad_fields = []
         for error in e.errors():
             field_name = humanize(underscore(error["loc"][0])).lower()
             message = error["msg"]
             bad_fields.append(f"{field_name} ({message})")
         context.error = ErrorModel(msg=f"Invalid input for {'; '.join(bad_fields)}")
     elif isinstance(e, HTTPException):
         context.error = ErrorModel(msg=str(e))
         context.status_code = e.code
     else:
         context.status_code = 500
         context.error = ErrorModel(
             msg="Something unexpected happened. Please try again or "
             "email [email protected] describing your problem."
         )
Beispiel #37
0
    def _create_upgrade(self):
        name = self.name.replace(' ', '_').replace('\.$', '')
        step_name = '{0}_{1}'.format(self.destination_version,
                                     inflection.underscore(name))
        if self.code is None:
            self.code = scaffold.PYTHON_TEMPLATE.format(
                classname=inflection.camelize(name),
                docstring=inflection.humanize(
                    inflection.underscore(name)))

        self.package.with_file(
            os.path.join(step_name, '__init__.py'),
            '',
            makedirs=True)

        self.package.with_file(
            os.path.join(step_name, 'upgrade.py'),
            self.code)
        return step_name
Beispiel #38
0
    def load_term_subsets(self, term, session):
        logger = self.get_term_logger(self.current_ontology)
        subsets = []
        if term.subsets:
            s_subsets = self.client.search(query=term.subsets,
                                           filters={
                                               'type': 'property',
                                               'exact': 'false'
                                           })
            seen = set()
            unique_subsets = [
                x for x in s_subsets if x.short_form.lower() not in seen
                and not seen.add(x.short_form.lower())
            ]
            logger.debug("Loading unique subsets %s", unique_subsets)

            for subset in unique_subsets:
                subset_def = inflection.humanize(subset.label)
                m_subset, created = get_one_or_create(
                    Subset,
                    session,
                    name=inflection.underscore(subset.label),
                    create_method_kwargs=dict(definition=subset_def))
                if created:
                    # avoid call to API if already exists
                    logger.info("Created new subset %s", m_subset.name)
                    try:
                        details = self.client.property(identifier=subset.iri)
                        if not details:
                            logger.warning(
                                'Unable to retrieve subset details %s for ontology %s',
                                subset.label, term.ontology.name)
                        else:
                            m_subset.definition = details.definition
                            session.merge(m_subset)
                            session.commit()
                    except ebi.ols.api.exceptions.ObjectNotRetrievedError:
                        logger.error('Too Many errors from API %s %s',
                                     subset.label, term.ontology.name)
            logger.info('Loaded subsets: %s ', subsets)
        else:
            logger.info('...No Subset')
        return subsets
Beispiel #39
0
def get_datasets():
    web_dir = os.path.join(os.path.dirname(__file__), 'web_ready')

    datasetNames = {}
    dataframes = {}
    datasets = {}

    for (i, f_name) in enumerate(os.listdir(web_dir)):
        fp = os.path.join(web_dir, f_name)

        if fp.endswith('.csv'):
            datasetName = f_name.replace(".csv", "")
            df = read_csv(fp)

            if datasetName == "country_year_master":
                df = df[[
                    "year",
                    "Existence of national screening program for breast cancer",
                    "Existence of national screening program for cervical cancer",
                    "Existence of national HPV vaccination programme",
                    "Current health expenditure (CHE) as percentage of gross domestic product (GDP) (%)",
                    "Current health expenditure (CHE) per capita in PPP int$",
                    "Current health expenditure (CHE) per capita in US$",
                    "Out-of-pocket expenditure (OOP) per capita in PPP int$",
                    "Out-of-pocket expenditure (OOP) per capita in US$",
                    "Breast cancer deaths per 100000 women",
                    "Breast cancer new cases per 100000 women",
                    "Gdp pc usd inflation adjusted", "Gni pc constant 2010 u",
                    "Prostate cancer deaths per 100000 men",
                    "Prostate cancer new cases per 100000 men"
                ]]

            datasetNames[i] = datasetName
            dataframes[datasetName] = df
            datasets[datasetName] = {
                'title': humanize(datasetName),
                'table': df_to_datatable(df),
                'columns': df.columns.values.tolist(),
                'indices': df.index.values.tolist()
            }

    return (datasetNames, datasets, dataframes)
Beispiel #40
0
    def create(self, name):
        name = name.replace(' ', '_').replace('\.$', '')

        step_name = '{0}_{1}'.format(
            datetime.now().strftime(DATETIME_FORMAT),
            inflection.underscore(name))
        step_directory = os.path.join(self.upgrades_directory, step_name)
        os.mkdir(step_directory)

        Path(step_directory).joinpath('__init__.py').touch()

        code_path = os.path.join(step_directory, 'upgrade.py')
        with open(code_path, 'w+') as code_file:
            code_file.write(
                PYTHON_TEMPLATE.format(
                    classname=inflection.camelize(name),
                    docstring=inflection.humanize(
                        inflection.underscore(name))))

        return step_directory
Beispiel #41
0
    def fetch_resource(self, method, *args, **kwargs):
        """Fetch the endpoint from the API and return it as a Resource.

        method         - HTTP method.
        *args          - Uri template argument
        **kwargs       – Uri template arguments
        """
        variables = self.variables()
        if len(args) == 1 and len(variables) == 1:
            kwargs[next(iter(variables))] = args[0]

        url_args = {k: kwargs[k] for k in kwargs if k in variables}
        req_args = {k: kwargs[k] for k in kwargs if k not in variables}

        url = uritemplate.expand(self.url, url_args)
        request = requests.Request(method, url, **req_args)
        prepared_req = self.session.prepare_request(request)
        response = self.session.send(prepared_req)

        return Resource(self.session, response=response,
                        name=humanize(self._name))
def humanize(value):
    return inflection.humanize(value)
Beispiel #43
0
 def __iter__(self):
     """Generator that returns field names and values for each value that is not None."""
     for i in self._meta.get_all_field_names():
         if getattr(self, i) is not None:
             yield '{}: {}'.format(inflection.humanize(i), getattr(self, i))
Beispiel #44
0
def test_humanize(underscore, human):
    assert human == inflection.humanize(underscore)
Beispiel #45
0
def scrappe_data(samples):
    """
    Scrapper main function.

    :param dict samples: A dictionary with the ids samples.
    :return: A dictionary with the scrapped data of the form.
    :rtype: dict
    """

    headers = {'Content-Type': 'application/json'}
    scrapped_data = OrderedDict()

    with tqdm(
            total=len(samples), unit='r',
            leave=True, desc='POST requests') as pbar:

        # Iterate samples to grab data from web service
        for district, id_voter in samples.items():
            payload = dumps({'numeroCedula': str(id_voter)})

            retries = 5
            while retries > 0:

                try:
                    response = post(
                        SCRAPPER_URL,
                        headers=headers,
                        data=payload
                    )
                    response.raise_for_status()

                    data = response.json()['d']['lista']

                    latitude, longitude = parse_location(data['url'])
                    address = humanize(
                        data['direccionEscuela'].strip().lower()
                    )
                    name = titleize(
                        data['nombreCentroVotacion'].strip().lower()
                    )

                    # Record data
                    scrapped_data[district] = {
                        'latitude': latitude,
                        'longitude': longitude,
                        'address': address,
                        'name': name
                    }

                    pbar.update(1)
                    break
                except:
                    log.error(
                        'Error while processing district #{} '
                        'using voter id #{} :: (RETRIES LEFT: {})\n{}'.format(
                            district, id_voter, retries, format_exc()
                        )
                    )

                retries -= 1

    return scrapped_data
Beispiel #46
0
 def humanize(self, word):
     return inflection.humanize()
Beispiel #47
0
    def full_messages_for(self, field):
        """Returns all the full error messages for a given *field* as a list.

        """
        humanized_field = inflection.titleize(inflection.humanize(field))
        return ["%s %s" % (humanized_field, msg) for msg in self.get(field)]
import inflection as i

text = "foo_bar_boo"
print("camelize", i.camelize(text))

print("dasherize", i.dasherize(text))

print("humanize", i.humanize(text))

print("----------------------------------------")

print("pluralize", i.pluralize("dog"))

print("singularize", i.singularize("dogs"))


print(i.camelize(i.singularize("cats")))
Beispiel #49
0
def download_all(request, responses_id):
	wb = xlwt.Workbook()
	allform = {}
	userid = responses_id.split("/")
	xlsfile = []

	for uid in userid:
		_object = get_object_or_404(Response, pk=uid)
		xlsfile.append(_object)
		result_json = json.loads(_object.response_text)
		for row in result_json:
			if not row["formName"] in allform:
				allform[row["formName"]] = []
			jsondata = (json.loads(row["formInstance"]))
			jsonfield = list()
			jsonfield.append({'name': "UserID", 'value': row["anmId"]})
			jsonfield.append({'name': bindTypesId.get(jsondata["form"]["bind_type"], "none"), 'value': row["entityId"]})
			jsonfield.extend(jsondata["form"]["fields"])
			jsonfield.append({'name': "clientVersionSubmissionDate", 'value' : datetime.fromtimestamp(int(row["clientVersion"])/1000.0).strftime('%Y-%m-%d %H:%M:%S')})
			jsonfield.append({'name': "serverVersionSubmissionDate", 'value' : datetime.fromtimestamp(int(row["serverVersion"])/1000.0).strftime('%Y-%m-%d %H:%M:%S')})
			allform[row["formName"]].append(jsonfield)

	for sheet in allform:
		# create worksheet
		worksheetTitle = sheet[0:30]
		wa = wb.add_sheet(inflection.humanize(worksheetTitle))
		titleArray = []
		formData = []
		
		# put the json data to array
		for idx1, data1 in enumerate(allform[sheet]):
			formData.append([])
			formData[idx1] = {}
			for idx2, data2 in enumerate(data1):
				if data2['name'] != 'id':
					if not data2['name'] in titleArray:
						titleArray.insert(idx2, data2['name'])
					value = data2.get('value')
					if value is None:
						value = '-'
					formData[idx1][data2['name']] = value

		# write al data to worksheet
		for idx1, data1 in enumerate(formData):
			for idx2, data2 in enumerate(titleArray):
				if idx1 == 0:
					wa.write(0, idx2, data2, style0)
				if data2 in data1:
					value = data1[data2]
					wa.col(idx2).width = get_width(len(value)) if get_width(len(value)) > wa.col(idx2).width else wa.col(idx2).width
					wa.write(idx1+1, idx2, inflection.humanize(value), style1)
				else:
					wa.write(idx1+1, idx2, '-', style1)

	xlsname = ""
	for xls in xlsfile :
		xlsname += xls.response_username + "+"
	xlsname = xlsname[:-1]
	xlsname += ".xls"

	return xls_to_response(wb, xlsname)	
Beispiel #50
0
    send_message_to_client(user.id, dict(
        event=event,
        job_id=str(job.hashid),
        config_entity_id=config_entity and config_entity.id,
        config_entity_class_name=config_entity and config_entity.__class__.__name__,
        # Send the key since the id of new instances might be meaningless to the client
        # If it hasn't updated the record's id yet
        publisher_name=publishing_info['publisher_name'],
        class_name=bundle.class_name_for_client,
        ids=bundle.client_instance_ids,
        keys=bundle.keys,
        # Used for Features and other things that have a class scope key
        class_key=bundle.class_key,
        # Send the proportion of work that completing this signal signifies--0 to 1
        proportion=publishing_info['proportion'],
        # The client can display a this to describe the progress
        progress_description=humanize(publishing_info['signal_path']),
    ))

    # Find all dependent signals of this one and run each in parallel
    for dependent_signal_path in publishing_info['dependent_signal_paths']:
        # Recurse
        post_save_publishing(
            dependent_signal_path,
            config_entity,
            user,
            **updated_kwargs
        )
    job.status = 'Complete'
Beispiel #51
0
        instance = association_class.objects.get(**kwargs)
    except association_class.DoesNotExist:
        logger.exception(
            "%s instance with kwargs %s not found",
            class_name(association_class), kwargs)
        raise

    return instance.delete()


def create_association(association_class, **kwargs):
    """Create association instance."""
    return association_class.objects.create(**kwargs)

_contact_type = "ContactType"
_contact_type_vebose = humanize(underscore(_contact_type))


class ContactType(NamedModel):
    """Contact type model class.

    Allows the organization of contacts into logical types.
    Values may include individual, organization, unknown.
    """
    # @TODO: is this duplicate of category?
    class Meta(NamedModel.Meta):
        """Model meta class declaration."""
        app_label = _app_label
        db_table = db_table(_app_label, _contact_type)
        verbose_name = _(_contact_type_vebose)
        verbose_name_plural = _(pluralize(_contact_type_vebose))
Beispiel #52
0
*demographics* application models module.

"""
from __future__ import absolute_import

from django.utils.translation import ugettext_lazy as _
from inflection import humanize, pluralize, underscore

from django_core_utils.constants import UNKNOWN
from django_core_utils.models import NamedModel, db_table

_app_label = "demographics"


_age = "Age"
_age_verbose = humanize(underscore(_age))


class Age(NamedModel):
    """Age demographics model class.

    Sample name field values may include:

    -  age < 12
    -  50 < age < 60

    """
    class Meta(NamedModel.Meta):
        """Model meta class declaration."""
        app_label = _app_label
        db_table = db_table(_app_label, _age)
Beispiel #53
0
 def parse_schema_list(self, data, name):
     """Convert the responses' JSON into a list of resources"""
     return [
       Resource(self.session, schema=s, name=humanize(singularize(name)))
       for s in data
     ]
def create_csv():

	# fetch forms
	for user in allUsers:
		apiUrl = URL + "/form-submissions?anm-id="+user+"&timestamp=0"
		try:
			print("Fetch form submissions %s" % user)
			req = urllib2.Request(apiUrl)
			base64String = base64.encodestring('%s:%s' % (USERLOGIN, PASSWORDLOGIN)).replace('\n', '')
			req.add_header("Authorization", "Basic %s" % base64String)
			result = urllib2.urlopen(req)
			resultJson = json.load(result.fp)
			for row in resultJson:
				if not row["formName"] in formNames:
					formNames[row["formName"]] = []
				jsondata = (json.loads(row["formInstance"]))
				jsonfield = []
				jsonfield.append({'name' : "userID", 'value' : row["anmId"]})
				jsonfield.extend(jsondata["form"]["fields"])
				jsonfield.append({'name' : "clientVersionSubmissionDate", 'value' : datetime.fromtimestamp(int(row["clientVersion"])/1000.0).strftime('%Y-%m-%d %H:%M:%S')})
				jsonfield.append({'name' : "serverVersionSubmissionDate", 'value' : datetime.fromtimestamp(int(row["serverVersion"])/1000.0).strftime('%Y-%m-%d %H:%M:%S')})
				formNames[row["formName"]].append(jsonfield)

			result.close()
		except(socket.timeout, urllib2.HTTPError) as e:
			print("Error : %s " % e)
			sys.exit(1)

	# create csv
	for sheet in formNames:
		print("Create %s" % sheet)

		# create worksheet
		worksheetTitle = inflection.humanize(sheet[0:30])
		titleArray = []
		formData = []
		allForms = []
		
		# put the json data to array
		for idx1, data1 in enumerate(formNames[sheet]):
			formData.append([])
			formData[idx1] = {}
			for idx2, data2 in enumerate(data1):
				if not data2['name'] in titleArray:
					titleArray.insert(idx2, data2['name'])
				value = data2.get('value')
				if value is None:
					value = '-'
				formData[idx1][data2['name']] = value

		allForms.append(titleArray)
		for idx1, data1 in enumerate(formData):
			aForm = []
			for idx2, data2 in enumerate(titleArray):
				if data2 in data1:
					value = data1[data2]
					aForm.append(value)
				else:
					aForm.append("-")
			allForms.append(aForm)

		if not os.path.exists(csvPathFolder):
			os.makedirs(csvPathFolder)

		with open(csvPathFolder+sheet+".csv", "wb") as csv_file:
			writer = csv.writer(csv_file, delimiter=',')
			for line in allForms:
				writer.writerow(line)
Beispiel #55
0
def _run_case(prob: problem.Problem,
              working_dir: pathlib.Path,
              test_id: str
) -> TestCaseResult:
    settings = config.get_settings()

    test_input_path = prob.get_test_input_path(test_id)
    test_output_path = prob.get_test_output_path(test_id)
    test_error_path = prob.get_test_error_path(test_id)
    test_answer_path = prob.get_test_answer_path(test_id)
    assert test_input_path.is_file()

    sp_result = None
    input_file = None
    pcu_stdout_path = working_dir / '.pcu_stdout'
    pcu_stderr_path = working_dir / '.pcu_stderr'
    try:
        with open(pcu_stdout_path, 'wb') as stdout, \
                open(pcu_stderr_path, 'wb') as stderr:
            stdin = subprocess.DEVNULL
            if prob.input_file == 'PCU_STDIN':
                input_file = open(test_input_path, 'rb')
                stdin = input_file
            else:
                shutil.copy2(
                    test_input_path, working_dir / prob.input_file)

            try:
                sp_result = subprocess.run(prob.run_command,  # type: ignore
                    cwd=working_dir,
                    stdin=stdin,
                    stdout=stdout,
                    stderr=stderr,
                    timeout=prob.env.run_timelimit_msec * 0.001,
                    shell=True)
            except subprocess.TimeoutExpired:
                return TestCaseResult.TIME_LIMIT_EXCEEDED

    finally:
        if input_file is not None:
            try:
                input_file.close()
            except Exception:
                pass

    # Handle some edge cases
    assert sp_result is not None
    if sp_result.returncode != 0:
        print('Exit code', sp_result.returncode,
              file=sys.stderr)
        return TestCaseResult.RUNTIME_ERROR

    run_output_path = pcu_stdout_path
    if prob.output_file != 'PCU_STDOUT':
        run_output_path = working_dir / prob.output_file
    if not run_output_path.is_file():
        return TestCaseResult.NO_OUTPUT_FILE_PRODUCED
    shutil.copy2(run_output_path, test_output_path)
    shutil.copy2(pcu_stderr_path, test_error_path)

    # Edge cases are handled, time for the fun stuff now
    with open(test_output_path, 'r') as infile:
        actual_output = infile.read()
        actual_output_lines = actual_output.splitlines()
    result = None
    expected_output = None

    # If no answer file was provided, then we should exit after printing
    # the output/error preview.
    if not test_answer_path.is_file():
        result = TestCaseResult.NO_ANSWER_FILE_PROVIDED

    else:
        with open(test_answer_path, 'r') as infile:
            expected_output = infile.read()
            expected_output_lines = expected_output.splitlines()

        match_exact = expected_output == actual_output
        match_minus_whitespace = match_exact or \
            (''.join(expected_output.split()) ==
             ''.join(actual_output.split()))

        # Figure out the disposition of the test case.
        if prob.env.format_strictness == environment.FormatStrictness.LAX:
            result = (TestCaseResult.CORRECT
                      if match_minus_whitespace
                      else TestCaseResult.WRONG_ANSWER)
        else:
            assert prob.env.format_strictness == environment.FormatStrictness.STRICT
            result = (TestCaseResult.CORRECT
                      if match_exact
                      else (TestCaseResult.PRESENTATION_ERROR
                            if match_minus_whitespace
                            else TestCaseResult.WRONG_ANSWER))

    # If the program was correct, there's nothing to print.
    if result == TestCaseResult.CORRECT:
        return result

    # print either the output or the diff
    if result == TestCaseResult.NO_ANSWER_FILE_PROVIDED:
        with color_utils.ColorizeStderr(*result.get_colorize_colors()):
            print(inflection.humanize(result.name), "-- here's the output:",
                  file=sys.stderr)
        with open(test_output_path, 'r') as infile:
            _print_truncate(infile, settings.max_lines_output, sys.stderr)

    else:
        with color_utils.ColorizeStderr(*result.get_colorize_colors()):
            print(inflection.humanize(result.name), "-- here's the diff:",
                  file=sys.stderr)
        diff_iter = difflib.unified_diff(
            expected_output.splitlines(keepends=True),
            actual_output.splitlines(keepends=True),
            fromfile='expected output',
            tofile='actual output',
            n=(1 ** 30))
        _print_truncate(diff_iter, settings.max_lines_output, sys.stderr)

    # print stderr
    if os.path.getsize(test_error_path) > 0:
        with color_utils.ColorizeStderr(*result.get_colorize_colors()):
            print("> and here's the stderr:",
                  file=sys.stderr)
        with open(test_error_path, 'r') as infile:
            _print_truncate(infile, settings.max_lines_error, sys.stderr)

    return result
Beispiel #56
0
 def dispatch_request(self, *args, **kwargs):
     current_context["admin_section_title"] = self.title or inflection.humanize(self.name)
     current_context["admin_section_desc"] = self.description
     return super(AdminView, self).dispatch_request(*args, **kwargs)
Beispiel #57
0
def handler(context, event):

    # extract the stuff we need
    image_url = event.body.decode('utf-8').strip()
    key = os.environ.get('FACE_API_KEY')
    base_url = os.environ.get('FACE_API_BASE_URL')

    if key is None:
        context.logger.warn('Face API key not set, cannot continue')
        return _build_response(context, 'Function misconfigured: Face API key not set', 503)

    if base_url is None:
        context.logger.warn('Face API base URL not set, cannot continue')
        return _build_response(context, 'Function misconfigured: Face API base URL not set', 503)

    if not image_url:
        context.logger.warn('No URL given in request body')
        return _build_response(context, 'Image URL required', 400)

    # configure cognitive face wrapper
    cf.Key.set(key)
    cf.BaseUrl.set(base_url)

    # attempt to request using the provided info
    try:
        context.logger.info('Requesting detection from Face API: {0}'.format(image_url))
        detected_faces = cf.face.detect(image_url,
                                        face_id=False,
                                        attributes='age,gender,glasses,smile,emotion')
    except Exception as error:
        context.logger.warn('Face API error occurred: {0}'.format(error))
        return _build_response(context, 'Face API error occurred', 503)

    parsed_faces = []

    # determine the center point of each detected face and map it to its attributes,
    # as well as clean up the retreived data for viewing comfort
    for face in detected_faces:
        coordinates = face['faceRectangle']
        attributes = face['faceAttributes']

        center_x = coordinates['left'] + coordinates['width'] / 2
        center_y = coordinates['top'] + coordinates['height'] / 2

        # determine the primary emotion based on its weighing
        primary_emotion = sorted(attributes['emotion'].items(), key=lambda item: item[1])[-1][0]

        parsed_face = {
            'x': center_x,
            'y': center_y,
            'position': '({0},{1})'.format(int(center_x), int(center_y)),
            'gender': inflection.humanize(attributes['gender']),
            'age': int(attributes['age']),
            'glasses': inflection.humanize(inflection.underscore(attributes['glasses'])),
            'primary_emotion': inflection.humanize(primary_emotion),
            'smile': '{0:.1f}%'.format(attributes['smile'] * 100),
        }

        parsed_faces.append(parsed_face)

    # sort according to center point, first x then y
    parsed_faces.sort(key=lambda face: (face['x'], face['y']))

    # prepare the data for tabulation
    first_row = ('',) + tuple(face['position'] for face in parsed_faces)
    make_row = lambda name: (inflection.humanize(name),) + tuple(
                            face[name] for face in parsed_faces)

    other_rows = [make_row(name) for name in [
                  'gender', 'age', 'primary_emotion', 'glasses', 'smile']]

    # return the human-readable face data in a neat table format
    return _build_response(context,
                           tabulate.tabulate([first_row] + other_rows,
                                             headers='firstrow',
                                             tablefmt='fancy_grid',
                                             numalign='center',
                                             stralign='center'),
                           200)