pure python code to do modular-arithmetic unit conversions?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dan Stromberg

    #1

    pure python code to do modular-arithmetic unit conversions?


    Is there already a pure python module that can do modular-arithmetic unit
    conversions, like converting a huge number of seconds into months,
    weeks... or a bandwidth measure into megabits/s or gigabits/s or
    megabytes/s or gigabytes/s, whatever's the most useful (ala df -h)?

    Thanks!

  • Dan Bishop

    #2
    Re: pure python code to do modular-arithmetic unit conversions?

    Dan Stromberg wrote:[color=blue]
    > Is there already a pure python module that can do modular-arithmetic[/color]
    unit[color=blue]
    > conversions, like converting a huge number of seconds into months,
    > weeks...[/color]

    Use the divmod function.


    SECONDS_PER_MON TH = 2629746 # 1/4800 of 400 Gregorian years

    def convert_seconds (seconds):
    "Return (months, weeks, days, hours, minutes, seconds)."
    months, seconds = divmod(seconds, SECONDS_PER_MON TH)
    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)
    days, hours = divmod(hours, 24)
    weeks, days = divmod(days, 7)
    return months, weeks, days, hours, minutes, seconds

    def to_seconds(mont hs, weeks, days, hours, minutes, seconds):
    return (((weeks * 7 + days) * 24 + hours) * 60 + minutes) * 60 + \
    seconds + months * SECONDS_PER_MON TH
    [color=blue][color=green]
    >> convert_seconds (10**9)[/color][/color]
    (380, 1, 1, 1, 28, 40)[color=blue][color=green][color=darkred]
    >>> to_seconds(*_)[/color][/color][/color]
    1000000000
    [color=blue]
    > or a bandwidth measure into megabits/s or gigabits/s or
    > megabytes/s or gigabytes/s, whatever's the most useful (ala df -h)?[/color]

    def convert_bytes(b ytes):
    PREFIXES = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']
    x = bytes
    for prefix in PREFIXES:
    if x < 1024:
    return '%.4g %sB' % (x, prefix)
    x /= 1024.
    # No SI prefixes left, so revert to scientific notation
    return '%.3e B' % bytes[color=blue][color=green][color=darkred]
    >>> convert_bytes(4 0e9)[/color][/color][/color]
    '37.25 GB'[color=blue][color=green][color=darkred]
    >>> convert_bytes(2 048)[/color][/color][/color]
    '2 KB'

    Comment

    • Michael Spencer

      #3
      Re: pure python code to do modular-arithmetic unit conversions?

      Dan Stromberg wrote:[color=blue]
      > Is there already a pure python module that can do modular-arithmetic unit
      > conversions, like converting a huge number of seconds into months,
      > weeks... or a bandwidth measure into megabits/s or gigabits/s or
      > megabytes/s or gigabytes/s, whatever's the most useful (ala df -h)?
      >
      > Thanks!
      >[/color]
      Take a look at:


      From the intro:

      "Unum stands for 'unit-numbers'. It is a Python module that allows to define and
      manipulate true quantities, i.e. numbers with units such as 60 seconds, 500
      watts, 42 miles-per-hour, 100 kg per square meter, 14400 bits per second, 30
      dollars etc. The module validates unit consistency in arithmetic expressions; it
      provides also automatic conversion and output formatting."

      Michael

      Comment

      Working...