Beispiel #1
0
 def numericalize(self, amt):
     if amt > 100.0 and amt < 1e6:
         return humanize.intcomma(int(amt))
     if amt >= 1e6:
         return humanize.intword(int(amt))
     elif isinstance(amt, int) or amt.is_integer():
         return humanize.apnumber(int(amt))
     else:
         return humanize.fractional(amt)
Beispiel #2
0
    def _create_quantity_text(self, quantity, unit):
        definition = self.definitions[unit]

        # Round value
        quantity = round(quantity, definition.digits)

        # Create text
        quantity_str = humanize.fractional(quantity)
        unit = ureg(unit).units
        return f"{quantity_str} {unit:~P}"
    def get(self):
        args = parser.parse_args()

        start_of_the_day = datetime.combine(date.today(), time())
        end_of_the_day = start_of_the_day + timedelta(days=1) - timedelta(
            microseconds=1)

        processing_time = timedelta(microseconds=0)
        count = 0
        processed_count = 0
        processed_today = 0
        total_input_file_size = 0
        total_processed_file_size = 0
        for media_file in mp.mfq:
            if media_file.date_started and media_file.date_finished:
                processed_count += 1
                processing_time += media_file.date_finished - media_file.date_started

                if media_file.date_started >= start_of_the_day and media_file.date_finished <= end_of_the_day:
                    processed_today += 1
                if media_file.transcoded_file_size:
                    total_processed_file_size += media_file.transcoded_file_size
            total_input_file_size += media_file.file_size
            count += 1

        average_processing_time = QueueStats.mean(processing_time,
                                                  processed_count)
        average_processed_per_day = QueueStats.mean(
            processed_count,
            (processing_time.total_seconds() / (60 * 60 * 24)))
        average_processed_file_size = total_processed_file_size / processed_count
        average_input_file_size = total_input_file_size / count
        input_to_processed_file_size_ratio = float(
            average_input_file_size) / max(average_processed_file_size, 1)

        return {
            'processed_today':
            apnumber(processed_today) if args.humanize else processed_today,
            'average_processed_per_day':
            apnumber(average_processed_per_day)
            if args.humanize else str(average_processed_per_day),
            'average_processing_time':
            naturaldelta(average_processing_time)
            if args.humanize else str(average_processing_time),
            'average_processed_file_size':
            naturalsize(average_processed_file_size)
            if args.humanize else average_processed_file_size,
            'average_input_file_size':
            naturalsize(average_input_file_size)
            if args.humanize else average_input_file_size,
            'input_to_processed_file_size_ratio':
            fractional(input_to_processed_file_size_ratio)
            if args.humanize else input_to_processed_file_size_ratio
        }
Beispiel #4
0
    def add_ingredient(self, name, quantity, unit=None):
        quantity = float(quantity)

        # No unit
        if not unit:
            self.ingredients.setdefault(name, 0.0)

            try:
                self.ingredients[name] += ureg.Quantity(quantity)
            except:
                other_unit = self.ingredients[name].units
                error = UnitMismatchError(other_unit)
                self._log_error(error)
                return f"!!!error {error}"

            quantity_text = humanize.fractional(quantity)
            return f"*{name}* ({quantity_text})"

        # Create quantity
        try:
            uquantity = quantity * ureg(unit)
        except:
            error = UnknownUnitError(unit)
            self._log_error(error)
            return f"!!!error {error}"

        # Convert to quantity to mass, if possible
        if uquantity.check("[mass]") and name in self.densities:
            uquantity = uquantity / self.densities[name]

        # Add to ingredients
        self.ingredients.setdefault(name, 0.0 * uquantity.units)

        try:
            self.ingredients[name] += uquantity
        except:
            other_unit = self.ingredients[name].units
            error = UnitMismatchError(other_unit)
            self._log_error(error)
            return f"!!!error {error}"

        # No conversion for this ingredient
        if name not in self.conversions:
            error = NoUnitConversionError(name)
            self._log_error(error)

            quantity_text = self._create_quantity_text(quantity, unit)
            return f"*{name}* ({quantity_text})"

        # Apply conversion
        equivalences = self._create_equivalences(name, uquantity)
        return f"*{name}* (" + " | ".join(equivalences) + ")"
Beispiel #5
0
    def get_lines(self):
        lines = []

        for name, uquantity in sorted(self.ingredients.items()):
            if uquantity.dimensionless:
                quantity_text = humanize.fractional(uquantity.magnitude)

            elif name not in self.conversions:
                quantity_text = self._create_quantity_text(
                    uquantity.magnitude, str(uquantity.units))

            else:
                equivalences = self._create_equivalences(name, uquantity)
                quantity_text = " | ".join(equivalences)

            lines.append(f"* {name}: {quantity_text}")

        lines.append("")
        return lines
Beispiel #6
0
def test_fractional(test_input, expected):
    assert humanize.fractional(test_input) == expected
Beispiel #7
0
import humanize

assert humanize.intcomma(12345) == '12,345'
assert humanize.intword(123455913) == '123.5 million'
assert humanize.naturalsize(1000000, binary=True) == '976.6 KiB'
assert humanize.fractional(0.3) == '3/10'
Beispiel #8
0
 async def fractional(self, ctx, *, val: float):
     """Convert a decimal into a more readable fraction"""
     await ctx.send(humanize.fractional(val))