This file is indexed.

/usr/lib/python2.7/dist-packages/measurement/base.py is in python-measurement 2.0.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
# Copyright (c) 2007, Robert Coup <robert.coup@onetrackmind.co.nz>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
#   1. Redistributions of source code must retain the above copyright notice,
#      this list of conditions and the following disclaimer.
#
#   2. Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
#
#   3. Neither the name of Distance nor the names of its contributors may be used
#      to endorse or promote products derived from this software without
#      specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
from decimal import Decimal

import six
import sympy
from sympy.solvers import solve_linear

from measurement.utils import total_ordering


NUMERIC_TYPES = six.integer_types + (float, Decimal)


def pretty_name(obj):
    return obj.__name__ if obj.__class__ == type else obj.__class__.__name__


class classproperty(property):
    def __get__(self, cls, owner):
        return self.fget.__get__(None, owner)()


@total_ordering
class MeasureBase(object):
    STANDARD_UNIT = None
    ALIAS = {}
    UNITS = {}
    SI_UNITS = []
    SI_PREFIXES = {
        'yocto': 'y',
        'zepto': 'z',
        'atto': 'a',
        'femto': 'f',
        'pico': 'p',
        'nano': 'n',
        'micro': 'u',
        'milli': 'm',
        'centi': 'c',
        'deci': 'd',
        'deca': 'da',
        'hecto': 'h',
        'kilo': 'k',
        'mega': 'M',
        'giga': 'G',
        'tera': 'T',
        'peta': 'P',
        'exa': 'E',
        'zeta': 'Z',
        'yotta': 'Y',
    }
    SI_MAGNITUDES = {
        'yocto': 1e-24,
        'zepto': 1e-21,
        'atto': 1e-18,
        'femto': 1e-15,
        'pico': 1e-12,
        'nano': 1e-9,
        'micro': 1e-6,
        'milli': 1e-3,
        'centi': 1e-2,
        'deci': 1e-1,
        'deca': 1e1,
        'hecto': 1e2,
        'kilo': 1e3,
        'mega': 1e6,
        'giga': 1e9,
        'tera': 1e12,
        'peta': 1e15,
        'exa': 1e18,
        'zeta': 1e21,
        'yotta': 1e24,
    }

    def __init__(self, default_unit=None, **kwargs):
        value, default = self.default_units(kwargs)
        self._default_unit = default
        setattr(self, self.STANDARD_UNIT, value)
        if default_unit and isinstance(default_unit, six.string_types):
            self._default_unit = default_unit

    @classmethod
    def get_units(cls):
        units = cls.UNITS.copy()
        for unit in cls.SI_UNITS:
            unit_value = units[unit]
            for magnitude, value in cls.SI_MAGNITUDES.items():
                unit_abbreviation = cls.SI_PREFIXES[magnitude] + unit
                units[unit_abbreviation] = unit_value * value
        return units

    @classmethod
    def get_si_aliases(cls):
        si_aliases = {}
        for alias, abbrev in cls.ALIAS.items():
            if abbrev in cls.SI_UNITS:
                si_aliases[alias] = abbrev
        return si_aliases

    @classmethod
    def get_aliases(cls):
        aliases = cls.ALIAS.copy()
        si_aliases = cls.get_si_aliases()
        for si_alias, unit_abbrev in si_aliases.items():
            for magnitude, _ in cls.SI_MAGNITUDES.items():
                magnitude_alias = magnitude + si_alias
                prefix = cls.SI_PREFIXES[magnitude]
                aliases[magnitude_alias] = prefix + unit_abbrev
        return aliases

    @classmethod
    def get_lowercase_aliases(self):
        lowercased = {}
        for alias, value in self.get_aliases().items():
            lowercased[alias.lower()] = value
        return lowercased

    @property
    def standard(self):
        return getattr(self, self.STANDARD_UNIT)

    @standard.setter
    def standard(self, value):
        setattr(self, self.STANDARD_UNIT, value)

    @property
    def value(self):
        return getattr(self, self._default_unit)

    @value.setter
    def value(self, value):
        units = self.get_units()
        u1 = units[self.STANDARD_UNIT]
        u2 = units[self.unit]

        self.standard = value * (u2 / u1)

    @property
    def unit(self):
        return self._default_unit

    @unit.setter
    def unit(self, value):
        aliases = self.get_aliases()
        laliases = self.get_lowercase_aliases()
        units = self.get_units()
        unit = None
        if value in self.UNITS:
            unit = value
        elif value in aliases:
            unit = aliases[value]
        elif value.lower() in units:
            unit = value.lower()
        elif value.lower() in laliases:
            unit = laliases[value.lower]
        if not unit:
            raise ValueError('Invalid unit %s' % value)
        self._default_unit = unit

    def __getattr__(self, name):
        units = self.get_units()
        if name in units:
            return self._convert_value_to(
                units[name],
                self.standard,
            )
        else:
            raise AttributeError('Unknown unit type: %s' % name)

    def __repr__(self):
        return '%s(%s=%s)' % (
            pretty_name(self),
            self.unit,
            getattr(self, self._default_unit)
        )

    def __str__(self):
        return '%s %s' % (
            getattr(self, self._default_unit),
            self.unit
        )

    # **** Comparison methods ****

    def __eq__(self, other):
        if isinstance(other, self.__class__):
            return self.standard == other.standard
        else:
            return NotImplemented

    def __lt__(self, other):
        if isinstance(other, self.__class__):
            return self.standard < other.standard
        else:
            return NotImplemented

    # **** Operators methods ****

    def __add__(self, other):
        if isinstance(other, self.__class__):
            return self.__class__(
                default_unit=self._default_unit,
                **{self.STANDARD_UNIT: (self.standard + other.standard)}
            )
        else:
            raise TypeError(
                '%(class)s must be added with %(class)s' % {
                    "class": pretty_name(self)
                }
            )

    def __iadd__(self, other):
        if isinstance(other, self.__class__):
            self.standard += other.standard
            return self
        else:
            raise TypeError(
                '%(class)s must be added with %(class)s' % {
                    "class": pretty_name(self)
                }
            )

    def __sub__(self, other):
        if isinstance(other, self.__class__):
            return self.__class__(
                default_unit=self._default_unit,
                **{self.STANDARD_UNIT: (self.standard - other.standard)}
            )
        else:
            raise TypeError(
                '%(class)s must be subtracted from %(class)s' % {
                    "class": pretty_name(self)
                }
            )

    def __isub__(self, other):
        if isinstance(other, self.__class__):
            self.standard -= other.standard
            return self
        else:
            raise TypeError(
                '%(class)s must be subtracted from %(class)s' % {
                    "class": pretty_name(self)
                }
            )

    def __mul__(self, other):
        if isinstance(other, NUMERIC_TYPES):
            return self.__class__(
                default_unit=self._default_unit,
                **{self.STANDARD_UNIT: (self.standard * other)}
            )
        else:
            raise TypeError(
                '%(class)s must be multiplied with number' % {
                    "class": pretty_name(self)
                }
            )

    def __imul__(self, other):
        if isinstance(other, NUMERIC_TYPES):
            self.standard *= float(other)
            return self
        else:
            raise TypeError(
                '%(class)s must be multiplied with number' % {
                    "class": pretty_name(self)
                }
            )

    def __rmul__(self, other):
        return self * other

    def __truediv__(self, other):
        if isinstance(other, self.__class__):
            return self.standard / other.standard
        if isinstance(other, NUMERIC_TYPES):
            return self.__class__(
                default_unit=self._default_unit,
                **{self.STANDARD_UNIT: (self.standard / other)}
            )
        else:
            raise TypeError(
                '%(class)s must be divided with number or %(class)s' % {
                    "class": pretty_name(self)
                }
            )

    def __div__(self, other):   # Python 2 compatibility
        return type(self).__truediv__(self, other)

    def __itruediv__(self, other):
        if isinstance(other, NUMERIC_TYPES):
            self.standard /= float(other)
            return self
        else:
            raise TypeError(
                '%(class)s must be divided with number' % {
                    "class": pretty_name(self)
                }
            )

    def __idiv__(self, other):  # Python 2 compatibility
        return type(self).__itruediv__(self, other)

    def __bool__(self):
        return bool(self.standard)

    def __nonzero__(self):      # Python 2 compatibility
        return type(self).__bool__(self)

    def _convert_value_to(self, unit, value):
        if not isinstance(value, float):
            value = float(value)

        if isinstance(unit, sympy.Expr):
            result = unit.evalf(
                subs={
                    self.SU: value
                }
            )
            return float(result)
        return value / unit

    def _convert_value_from(self, unit, value):
        if not isinstance(value, float):
            value = float(value)

        if isinstance(unit, sympy.Expr):
            _, result = solve_linear(unit, value)
            return result
        return unit * value

    def default_units(self, kwargs):
        """
        Return the unit value and the default units specified
        from the given keyword arguments dictionary.
        """
        aliases = self.get_aliases()
        laliases = self.get_lowercase_aliases()
        units = self.get_units()
        val = 0.0
        default_unit = self.STANDARD_UNIT
        for unit, value in six.iteritems(kwargs):
            if unit in units:
                val = self._convert_value_from(units[unit], value)
                default_unit = unit
            elif unit in aliases:
                u = aliases[unit]
                val = self._convert_value_from(units[u], value)
                default_unit = u
            else:
                lower = unit.lower()
                if lower in units:
                    val = self._convert_value_from(units[lower], value)
                    default_unit = lower
                elif lower in laliases:
                    u = laliases[lower]
                    val = self._convert_value_from(units[u], value)
                    default_unit = u
                else:
                    raise AttributeError('Unknown unit type: %s' % unit)
        return val, default_unit

    @classmethod
    def unit_attname(cls, unit_str):
        """
        Retrieves the unit attribute name for the given unit string.
        For example, if the given unit string is 'metre', 'm' would be returned.
        An exception is raised if an attribute cannot be found.
        """
        laliases = cls.get_lowercase_aliases()
        units = cls.get_units()
        lower = unit_str.lower()
        if unit_str in units:
            return unit_str
        elif lower in units:
            return lower
        elif lower in laliases:
            return laliases[lower]
        else:
            raise Exception(
                'Could not find a unit keyword associated with "%s"' % (
                    unit_str,
                )
            )


@total_ordering
class BidimensionalMeasure(object):
    PRIMARY_DIMENSION = None
    REFERENCE_DIMENSION = None

    ALIAS = {
    }

    def __init__(self, **kwargs):
        if 'primary' in kwargs and 'reference' in kwargs:
            self.primary = kwargs['primary']
            self.reference = kwargs['reference']
        else:
            items = list(six.iteritems(kwargs))
            if len(items) > 1:
                raise ValueError('Only one keyword argument is expected')
            measure_string, value = items[0]

            self.primary, self.reference = self._get_measures(
                measure_string,
                value
            )

    def _get_unit_parts(self, measure_string):
        if measure_string in self.ALIAS:
            measure_string = self.ALIAS[measure_string]
        try:
            primary_unit, reference_unit = measure_string.split('__')
        except ValueError:
            raise AttributeError(
                (
                    'Unit not found: \'%s\';'
                    'Units should be expressed using double-underscore '
                    'separated units; for example: meters-per-second would be '
                    'expressed with either \'meter__second\' or \'m__sec\'.'
                ) % (
                    measure_string
                )
            )
        return primary_unit, reference_unit

    def _get_measures(self, measure_string, value):
        primary_unit, reference_unit = self._get_unit_parts(measure_string)
        primary = self.PRIMARY_DIMENSION(**{primary_unit: value})
        reference = self.REFERENCE_DIMENSION(**{reference_unit: 1})

        return primary, reference

    @property
    def standard(self):
        return self.primary.standard / self.reference.standard

    @classproperty
    @classmethod
    def STANDARD_UNIT(self):
        return '%s__%s' % (
            self.PRIMARY_DIMENSION.STANDARD_UNIT,
            self.REFERENCE_DIMENSION.STANDARD_UNIT,
        )

    @property
    def value(self):
        return self.primary.value

    @property
    def unit(self):
        return '%s__%s' % (
            self.primary.unit,
            self.reference.unit,
        )

    @unit.setter
    def unit(self, value):
        primary, reference = value.split('__')
        reference_units = self.REFERENCE_DIMENSION.get_units()
        if reference != self.reference.unit:
            reference_chg = (
                reference_units[self.reference.unit]/reference_units[reference]
            )
            self.primary.standard = self.primary.standard / reference_chg
        self.primary.unit = primary
        self.reference.unit = reference

    def _normalize(self, other):
        std_value = getattr(other, self.unit)

        primary = self.PRIMARY_DIMENSION(**{self.primary.unit: std_value})
        reference = self.REFERENCE_DIMENSION(**{self.reference.unit: 1})

        return self.__class__(primary=primary, reference=reference)

    def __getattr__(self, measure_string):
        primary_units = self.PRIMARY_DIMENSION.get_units()
        reference_units = self.REFERENCE_DIMENSION.get_units()

        p1, r1 = self.primary.unit, self.reference.unit
        p2, r2 = self._get_unit_parts(measure_string)

        primary_chg = primary_units[p2]/primary_units[p1]
        reference_chg = reference_units[r2]/reference_units[r1]

        return self.primary.value / primary_chg * reference_chg

    def __repr__(self):
        return '%s(%s__%s=%s)' % (
            pretty_name(self),
            self.primary.unit,
            self.reference.unit,
            self.primary.value,
        )

    def __str__(self):
        return '%s %s/%s' % (
            self.primary.value,
            self.primary.unit,
            self.reference.unit,
        )

    def __eq__(self, other):
        if isinstance(other, self.__class__):
            return self.standard == other.standard
        else:
            return NotImplemented

    def __lt__(self, other):
        if isinstance(other, self.__class__):
            return self.standard < other.standard
        else:
            return NotImplemented

    def __add__(self, other):
        if isinstance(other, self.__class__):
            normalized = self._normalize(other)
            total_value = normalized.primary.value + self.primary.value
            return self.__class__(
                primary=self.PRIMARY_DIMENSION(
                    **{self.primary.unit: total_value}
                ),
                reference=self.REFERENCE_DIMENSION(
                    **{self.reference.unit: 1}
                )
            )
        else:
            raise TypeError(
                '%(class)s must be added with %(class)s' % {
                    "class": pretty_name(self)
                }
            )

    def __iadd__(self, other):
        if isinstance(other, self.__class__):
            normalized = self._normalize(other)
            self.primary.standard += normalized.primary.standard
            return self
        else:
            raise TypeError(
                '%(class)s must be added with %(class)s' % {
                    "class": pretty_name(self)
                }
            )

    def __sub__(self, other):
        if isinstance(other, self.__class__):
            normalized = self._normalize(other)
            total_value = self.primary.value - normalized.primary.value
            return self.__class__(
                primary=self.PRIMARY_DIMENSION(
                    **{self.primary.unit: total_value}
                ),
                reference=self.REFERENCE_DIMENSION(
                    **{self.reference.unit: 1}
                )
            )
        else:
            raise TypeError(
                '%(class)s must be added with %(class)s' % {
                    "class": pretty_name(self)
                }
            )

    def __isub__(self, other):
        if isinstance(other, self.__class__):
            normalized = self._normalize(other)
            self.primary.standard -= normalized.primary.standard
            return self
        else:
            raise TypeError(
                '%(class)s must be added with %(class)s' % {
                    "class": pretty_name(self)
                }
            )

    def __mul__(self, other):
        if isinstance(other, NUMERIC_TYPES):
            total_value = self.primary.value * other
            return self.__class__(
                primary=self.PRIMARY_DIMENSION(
                    **{self.primary.unit: total_value}
                ),
                reference=self.REFERENCE_DIMENSION(
                    **{self.reference.unit: 1}
                )
            )
        else:
            raise TypeError(
                '%(class)s must be multiplied with number' % {
                    "class": pretty_name(self)
                }
            )

    def __rmul__(self, other):
        return self * other

    def __imul__(self, other):
        if isinstance(other, NUMERIC_TYPES):
            self.primary.standard *= float(other)
            return self
        else:
            raise TypeError(
                '%(class)s must be multiplied with number' % {
                    "class": pretty_name(self)
                }
            )

    def __truediv__(self, other):
        if isinstance(other, self.__class__):
            normalized = self._normalize(other)
            return self.primary.standard / normalized.primary.standard
        if isinstance(other, NUMERIC_TYPES):
            total_value = self.primary.value / other
            return self.__class__(
                primary=self.PRIMARY_DIMENSION(
                    **{self.primary.unit: total_value}
                ),
                reference=self.REFERENCE_DIMENSION(
                    **{self.reference.unit: 1}
                )
            )
        else:
            raise TypeError(
                '%(class)s must be divided with number or %(class)s' % {
                    "class": pretty_name(self)
                }
            )

    def __itruediv__(self, other):
        if isinstance(other, NUMERIC_TYPES):
            self.primary.standard /= float(other)
            return self
        else:
            raise TypeError(
                '%(class)s must be divided with number' % {
                    "class": pretty_name(self)
                }
            )

    def __div__(self, other):   # Python 2 compatibility
        return type(self).__truediv__(self, other)

    def __idiv__(self, other):  # Python 2 compatibility
        return type(self).__itruediv__(self, other)

    def __bool__(self):
        return bool(self.primary.standard)

    def __nonzero__(self):  # Python 2 compatibility
        return type(self).__bool__(self)