Ejemplo n.º 1
0
 def test_timer(self):
     """Test :func:`humanfriendly.Timer`."""
     for seconds, text in ((1, '1 second'),
                           (2, '2 seconds'),
                           (60, '1 minute'),
                           (60*2, '2 minutes'),
                           (60*60, '1 hour'),
                           (60*60*2, '2 hours'),
                           (60*60*24, '1 day'),
                           (60*60*24*2, '2 days'),
                           (60*60*24*7, '1 week'),
                           (60*60*24*7*2, '2 weeks')):
         t = humanfriendly.Timer(time.time() - seconds)
         self.assertEqual(humanfriendly.round_number(t.elapsed_time, keep_width=True), '%i.00' % seconds)
         self.assertEqual(str(t), text)
     # Test rounding to seconds.
     t = humanfriendly.Timer(time.time() - 2.2)
     self.assertEqual(t.rounded, '2 seconds')
     # Test automatic timer.
     automatic_timer = humanfriendly.Timer()
     time.sleep(1)
     self.assertEqual(normalize_timestamp(humanfriendly.round_number(
         automatic_timer.elapsed_time,
         keep_width=True,
     )), '1.00')
     # Test resumable timer.
     resumable_timer = humanfriendly.Timer(resumable=True)
     for i in range(2):
         with resumable_timer:
             time.sleep(1)
     self.assertEqual(normalize_timestamp(humanfriendly.round_number(
         resumable_timer.elapsed_time,
         keep_width=True,
     )), '2.00')
Ejemplo n.º 2
0
 def test_round_number(self):
     """Test :func:`humanfriendly.round_number()`."""
     self.assertEqual('1', humanfriendly.round_number(1))
     self.assertEqual('1', humanfriendly.round_number(1.0))
     self.assertEqual('1.00', humanfriendly.round_number(1,
                                                         keep_width=True))
     self.assertEqual('3.14', humanfriendly.round_number(3.141592653589793))
Ejemplo n.º 3
0
def format_timespan(num_seconds):
    """
    Taken from the humanfriendly library and changed the time units to
    their abbreviations.
    """

    if num_seconds < 60:
        # Fast path.
        rounded_number = round_number(num_seconds, False)
        return pluralize(rounded_number, 's', 's')
    else:
        # Slow path.
        result = []
        for unit in reversed(time_units):
            if num_seconds >= unit['divider']:
                count = int(num_seconds / unit['divider'])
                num_seconds %= unit['divider']
                result.append(
                    pluralize(count, unit['singular'], unit['plural']))
        if len(result) == 1:
            # A single count/unit combination.
            return result[0]
        else:
            # Remove insignificant data from the formatted timespan and format
            # it in a readable way.
            return concatenate(result[:2])
def format_timespan(num_seconds):
    """
    Taken from the humanfriendly library and changed the time units to
    their abbreviations.
    """

    if num_seconds < 60:
        # Fast path.
        rounded_number = round_number(num_seconds, False)
        return pluralize(rounded_number, 's', 's')
    else:
        # Slow path.
        result = []
        for unit in reversed(time_units):
            if num_seconds >= unit['divider']:
                count = int(num_seconds / unit['divider'])
                num_seconds %= unit['divider']
                result.append(pluralize(count, unit['singular'], unit['plural']))
        if len(result) == 1:
            # A single count/unit combination.
            return result[0]
        else:
            # Remove insignificant data from the formatted timespan and format
            # it in a readable way.
            return concatenate(result[:2])
Ejemplo n.º 5
0
 def test_timer(self):
     """Test :func:`humanfriendly.Timer`."""
     for seconds, text in ((1, '1 second'),
                           (2, '2 seconds'),
                           (60, '1 minute'),
                           (60 * 2, '2 minutes'),
                           (60 * 60, '1 hour'),
                           (60 * 60 * 2, '2 hours'),
                           (60 * 60 * 24, '1 day'),
                           (60 * 60 * 24 * 2, '2 days'),
                           (60 * 60 * 24 * 7, '1 week'),
                           (60 * 60 * 24 * 7 * 2, '2 weeks')):
         t = humanfriendly.Timer(time.time() - seconds)
         self.assertEqual(humanfriendly.round_number(t.elapsed_time, keep_width=True), '%i.00' % seconds)
         self.assertEqual(str(t), text)
     # Test rounding to seconds.
     t = humanfriendly.Timer(time.time() - 2.2)
     self.assertEqual(t.rounded, '2 seconds')
     # Test automatic timer.
     automatic_timer = humanfriendly.Timer()
     time.sleep(1)
     self.assertEqual(normalize_timestamp(humanfriendly.round_number(
         automatic_timer.elapsed_time,
         keep_width=True,
     )), '1.00')
     # Test resumable timer.
     resumable_timer = humanfriendly.Timer(resumable=True)
     for i in range(2):
         with resumable_timer:
             time.sleep(1)
     self.assertEqual(normalize_timestamp(humanfriendly.round_number(
         resumable_timer.elapsed_time,
         keep_width=True,
     )), '2.00')
     # Make sure Timer.__enter__() returns the timer object.
     with humanfriendly.Timer(resumable=True) as timer:
         assert timer is not None
Ejemplo n.º 6
0
def create_dos_partitions(self):
    """Create dos partition on user's selected drive.

    Modules
    -------
        humanfriendly: "Human readable data libraries"
        logging: "Event logging system for applications and libraries"
        time: "Various functions to manipulate time values"

    Submodules
    ----------
        `run_command`: "Subprocess Popen with console output"
    """
    for partition, size in zip(self.user['partitions']['name'],
                               self.user['partitions']['size']):

        logging.info(
            self.trad(
                'create {partition} partition [{size}] on {drive}').format(
                    partition=partition,
                    size=size,
                    drive=self.user['drive']['name']))

        if size == 'freespace' or \
                ((self.user['drive']['lvm'] is True) and
                 (partition == 'root')):
            size = '16T'

        size = parse_size(size.replace(',', '.'))
        size = round_number(size / 1000)

        pipe = ['/usr/bin/printf', 'size={size}K'.format(size=size)]
        cmd = 'sfdisk -f -q --no-reread -W always --append {drive}'.format(
            drive=self.user['drive']['name'])

        run_command(cmd, args=pipe, exit_on_error=True)
        time.sleep(1)
Ejemplo n.º 7
0
 def test_round_number(self):
     """Test :func:`humanfriendly.round_number()`."""
     self.assertEqual('1', humanfriendly.round_number(1))
     self.assertEqual('1', humanfriendly.round_number(1.0))
     self.assertEqual('1.00', humanfriendly.round_number(1, keep_width=True))
     self.assertEqual('3.14', humanfriendly.round_number(3.141592653589793))