Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Metric failed
from re import split __name__ = '__metric__' class DataModel(object): pass class UnitError(Exception): pass class SpecialDict(dict): pass class MetricSystem(DataModel): def __init__(self, metric_type, base_unit, unit_dict = {}): self.metric_type = metric_type self.base_unit = base_unit self.unit_dict = unit_dict def __getitem__(self, unit): return self.unit_dict[self.strip_power(unit)[0]] def is_same_type(self, unit): return self.strip_power(unit)[0] in self.unit_dict def convert(self, exp, unit, convert_to = None): if not self.is_same_type(convert_to): raise UnitError('Undefined or inconvertible type') convert_to = convert_to or self.base_unit return '(({:s})*{}){}'.format(exp, self._convert_to(self._convert_from(unit), convert_to), convert_to) def _convert_from(self, unit): return '{}^{}'.format(self[unit], self.strip_power(unit)[1]) def _convert_to(self, exp, convert_to): return '({})/{}^{}'.format(exp, self[convert_to], self.strip_power(convert_to)[1]) def strip_power(self, unit): return split(r'^|\*\*', unit) if '^' in unit or '**' in unit else [unit, 1] def eval(self, final_exp): pass def __str__(self): return '<Metric System for {:s} (base unit: {!r})>'.format(self.metric_type, self.base_unit) length = MetricSystem('length', 'm', { 'feet': 0.3048, 'cm': 0.01, 'pm': 1e-12 }) print(length, length.convert('48', 'cm', 'pm'))
run
|
edit
|
history
|
help
0
Email Validation
python_study_note_for loop@nested loop
134
Ns
Minesweeper
dictionary function
linked_lists_2
find minimum in right sorted array
Simulare
Bmi calculator dictionary