コード例 #1
0
ファイル: core.py プロジェクト: davebrent/stitches
 def __call__(self, event):
     # pylint: disable=no-member
     if isinstance(event, TaskStartEvent):
         print(
             colorful.format('{c.bold}[{}]: {}{c.reset}', event.ref,
                             event.description))
     elif isinstance(event, TaskSkipEvent):
         print(colorful.format('  {c.orange}Skipped{c.reset}'))
     elif isinstance(event, TaskCompleteEvent):
         print(colorful.format('  {c.green}Completed{c.reset}'))
     elif isinstance(event, TaskFatalEvent):
         lines = [
             colorful.format('  {c.red}{}{c.reset}', line)
             for line in event.traceback.splitlines()
         ]
         for line in lines:
             print(line, file=sys.stderr)
コード例 #2
0
ファイル: core.py プロジェクト: davebrent/stitches
 def __call__(self, event):
     if isinstance(event, TaskStartEvent):
         self.current_task = event
     elif isinstance(event, TaskCompleteEvent):
         self.current_task = None
     elif isinstance(event, TaskFatalEvent):
         # pylint: disable=no-member
         lines = [
             colorful.format('{c.red}{}{c.reset}', line)
             for line in event.traceback.splitlines()
         ]
         if self.current_task:
             print(
                 colorful.format('{c.bold}[{}]: {}{c.reset}',
                                 self.current_task.ref,
                                 self.current_task.description))
             lines = ['  {}'.format(line) for line in lines]
         for line in lines:
             print(line, file=sys.stderr)
コード例 #3
0
def colorize_data(data):
    if isinstance(data, dict):
        for key, value in data.items():
            data[key] = colorize_data(value)
    elif isinstance(data, (list, tuple)):
        data = list(data)
        for index, value in enumerate(data):
            data[index] = colorize_data(value)
    elif isinstance(data, str):
        try:
            return colorful.format(data)
        except Exception as e:
            pass
    return data
コード例 #4
0
ファイル: vis_seq_tags.py プロジェクト: yyht/sciwing
    def visualize_tokens(self, text: List[str], labels: List[str]) -> str:
        """ Visualizes sequential tagged data where the string is represented as a set of words
        and every word has a corresponding label. This can be extended to having different
        tagging schemes at a later point in time

        Parameters
        ----------
        text: List[str]
        String to be tagged represented as a list of strings

        labels: List[str]
        The labels corresponding to each word in the string

        Returns
        -------
        None
        """
        if len(text) != len(labels):
            raise ValueError(
                f"string and labels should of same length. String you passed has len {len(text)} "
                f"and labels you passed has len {len(labels)}"
            )

        if not self.tags:
            unique_labels = list(set(labels))
        else:
            unique_labels = self.tags
        len_labels = len(unique_labels)

        tag_colors = {
            unique_labels[idx]: f"{{c.on_{self._get_next_color()}}}"
            for idx in range(len_labels)
        }

        stylized_words = []

        for idx, word in enumerate(text):
            tag = labels[idx]
            tag_color = tag_colors[tag]

            formatted_string = (
                f"{colorful.reset}{tag_color}{word.strip()} "
                f"{{c.bold}}{tag.strip().upper()}{colorful.close_bg_color}"
            )
            formatted_string = colorful.format(formatted_string)
            stylized_words.append(formatted_string)

        tagged_string = " ".join(stylized_words)
        return tagged_string
コード例 #5
0
ファイル: cli_logger.py プロジェクト: wangziyuruc/ray
def _format_msg(msg: str,
                *args: Any,
                _tags: Dict[str, Any] = None,
                _numbered: Tuple[str, int, int] = None,
                _no_format: bool = None,
                **kwargs: Any):
    """Formats a message for printing.

    Renders `msg` using the built-in `str.format` and the passed-in
    `*args` and `**kwargs`.

    Args:
        *args (Any): `.format` arguments for `msg`.
        _tags (Dict[str, Any]):
            key-value pairs to display at the end of
            the message in square brackets.

            If a tag is set to `True`, it is printed without the value,
            the presence of the tag treated as a "flag".

            E.g. `_format_msg("hello", _tags=dict(from=mom, signed=True))`
                 `hello [from=Mom, signed]`
        _numbered (Tuple[str, int, int]):
            `(brackets, i, n)`

            The `brackets` string is composed of two "bracket" characters,
            `i` is the index, `n` is the total.

            The string `{i}/{n}` surrounded by the "brackets" is
            prepended to the message.

            This is used to number steps in a procedure, with different
            brackets specifying different major tasks.

            E.g. `_format_msg("hello", _numbered=("[]", 0, 5))`
                 `[0/5] hello`
        _no_format (bool):
            If `_no_format` is `True`,
            `.format` will not be called on the message.

            Useful if the output is user-provided or may otherwise
            contain an unexpected formatting string (e.g. "{}").

    Returns:
        The formatted message.
    """

    if isinstance(msg, str) or isinstance(msg, ColorfulString):
        tags_str = ""
        if _tags is not None:
            tags_list = []
            for k, v in _tags.items():
                if v is True:
                    tags_list += [k]
                    continue
                if v is False:
                    continue

                tags_list += [k + "=" + v]
            if tags_list:
                tags_str = cf.reset(
                    cf.dimmed(" [{}]".format(", ".join(tags_list))))

        numbering_str = ""
        if _numbered is not None:
            chars, i, n = _numbered
            numbering_str = cf.dimmed(chars[0] + str(i) + "/" + str(n) +
                                      chars[1]) + " "

        if _no_format:
            # todo: throw if given args/kwargs?
            return numbering_str + msg + tags_str
        return numbering_str + cf.format(msg, *args, **kwargs) + tags_str

    if kwargs:
        raise ValueError("We do not support printing kwargs yet.")

    res = [msg, *args]
    res = [str(x) for x in res]
    return ", ".join(res)
コード例 #6
0
ファイル: printer.py プロジェクト: edmundpf/print_tools
 def format(self, text, format_op='[]', reset=True, ret=False, log=False):
     format_start = format_op[0]
     format_end = format_op[1]
     format_count = text.count(format_start)
     format_ops = []
     for i in range(0, format_count):
         format_ops.append({
             'start':
             find_nth(text, format_start, i + 1),
             'end':
             find_nth(text, format_end, i + 1),
             'string':
             text[find_nth(text, format_start, i +
                           1):(find_nth(text, format_end, i + 1) + 1)],
             'args': []
         })
         string_args = text[(format_ops[-1]['start'] +
                             1):format_ops[-1]['end']]
         args = string_args.split(':')
         for arg in args:
             arg = arg.lower()
             on = not 'no_' in arg
             arg = arg.replace('no_', '')
             if any(arg in mod for mod in self.mods):
                 if arg == 'reset':
                     format_ops[-1]['args'] = [{
                         'type': 'mod',
                         'arg': arg,
                         'on': on
                     }] + format_ops[-1]['args']
                 else:
                     format_ops[-1]['args'].append({
                         'type': 'mod',
                         'arg': arg,
                         'on': on
                     })
             elif any(
                     arg.replace('on_', '') in color
                     for color in self.colors):
                 color_type = 'fg' if not 'on_' in arg and not 'bg' in arg else 'bg'
                 arg = arg.replace('on_', '')
                 if arg in self.color_map:
                     format_ops[-1]['args'].append({
                         'type':
                         color_type,
                         'arg':
                         self.color_map[arg],
                         'on':
                         on
                     })
                 else:
                     format_ops[-1]['args'].append({
                         'type': color_type,
                         'arg': arg,
                         'on': on
                     })
     for i in range(0, len(format_ops)):
         format_str = ''
         var_check = False
         for x in range(0, len(format_ops[i]['args'])):
             if not format_ops[i]['args'][x]['on']:
                 if format_ops[i]['args'][x]['type'] == 'mod':
                     format_str += '{' + f"c.no_{format_ops[i]['args'][x]['arg']}" + '}'
                 elif format_ops[i]['args'][x]['type'] in ['fg', 'bg']:
                     format_str += '{' + f"c.close_{format_ops[i]['args'][x]['type']}_color" + '}'
             else:
                 if not var_check:
                     format_str += '{c.'
                     var_check = True
                 if format_ops[i]['args'][x]['type'] == 'background':
                     format_str += 'on_'
                 format_str += format_ops[i]['args'][x]['arg']
                 if x != len(format_ops[i]['args']) - 1:
                     format_str += '_'
                 else:
                     format_str += '}'
         text = text.replace(format_ops[i]['string'], format_str)
     if reset:
         text = text + '{c.reset}'
     if ret:
         return c.format(text)
     else:
         self.log(c.format(text)) if log else print(c.format(text))
コード例 #7
0
ファイル: vis_seq_tags.py プロジェクト: yyht/sciwing
    def visualize_tags_from_json(
        self, json_annotation: Dict[str, Any], show_only_entities: List[str] = None
    ):
        """ Visualize the tags from json.

        Parameters
        ----------
        json_annotation: str
            You can send a json that has the following format
            {'text': str,
            'tags': [{'start':int, 'end':str, 'tag': str}]
            }
        show_only_entities: List[str]
            You can filter to show only these entities.

        """
        text = json_annotation.get("text", None)
        tags = json_annotation.get("tags", None)

        if not text or not tags:
            raise ValueError(
                "The json string should have the format "
                "{text:str, tags: [{'start': int, 'end': int, 'tag': str}]}"
            )

        if not self.tags:
            all_tags = list(set(annotation["tag"] for annotation in tags))
        else:
            all_tags = self.tags

        len_tags = len(all_tags)
        tag_colors = {
            all_tags[idx]: f"{{c.on_{self._get_next_color()}}}"
            for idx in range(len_tags)
        }

        valid_tags = {}
        for tag in tags:
            valid_tags[(tag["start"], tag["end"])] = tag["tag"]

        # assuming that the tags are given in order
        start_ends = []
        for tag in tags:
            start = tag["start"]
            end = tag["end"]
            start_ends.extend([start, end])

        if 0 not in start_ends:
            start_ends.insert(0, 0)
        if len(text) not in start_ends:
            start_ends.append(len(text))

        formatted_strings = []
        for start, end in pairwise(start_ends):
            if valid_tags.get((start, end), None) is not None:
                tag = valid_tags[(start, end)]
                if show_only_entities and tag not in show_only_entities:
                    continue
                tag_color = tag_colors[tag]
                formatted_string = f"{tag_color} {text[start:end]} {{c.bold}}{tag} {colorful.close_bg_color}"
                formatted_string = colorful.format(formatted_string)
            else:
                formatted_string = text[start:end]

            formatted_strings.append(formatted_string)
        tagged_string = " ".join(formatted_strings)
        print(tagged_string)