File: _base.py

package info (click to toggle)
python-skbio 0.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 9,312 kB
  • sloc: python: 60,482; ansic: 672; makefile: 224
file content (57 lines) | stat: -rw-r--r-- 1,688 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# ----------------------------------------------------------------------------
# Copyright (c) 2013--, scikit-bio development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE.txt, distributed with this software.
# ----------------------------------------------------------------------------

import abc


class SkbioObject(metaclass=abc.ABCMeta):
    """Abstract base class defining core API common to all scikit-bio objects.

    Public scikit-bio classes should subclass this class to ensure a common,
    core API is present. All abstract methods and properties defined here must
    be implemented in subclasses, otherwise they will not be instantiable.

    """

    @abc.abstractmethod
    def __str__(self):
        raise NotImplementedError


class ElasticLines:
    """Store blocks of content separated by dashed lines.

    Each dashed line (separator) is as long as the longest content
    (non-separator) line.

    """

    def __init__(self):
        self._lines = []
        self._separator_idxs = []
        self._max_line_len = -1

    def add_line(self, line):
        line_len = len(line)
        if line_len > self._max_line_len:
            self._max_line_len = line_len
        self._lines.append(line)

    def add_lines(self, lines):
        for line in lines:
            self.add_line(line)

    def add_separator(self):
        self._lines.append(None)
        self._separator_idxs.append(len(self._lines) - 1)

    def to_str(self):
        separator = "-" * self._max_line_len
        for idx in self._separator_idxs:
            self._lines[idx] = separator
        return "\n".join(self._lines)