File: util.py

package info (click to toggle)
python-nuheat 1.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 228 kB
  • sloc: python: 897; sh: 5; makefile: 5
file content (70 lines) | stat: -rw-r--r-- 1,884 bytes parent folder | download | duplicates (2)
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
from decimal import Decimal, ROUND_HALF_UP


def round_half(number):
    """
    Python's round() function behaves differently in Python 2 and 3
    This method makes it consistent.

    :param number: The number to round
    """
    return Decimal(number).quantize(Decimal("1"), rounding=ROUND_HALF_UP)


def fahrenheit_to_celsius(fahrenheit):
    """
    Convert Fahrenheit to Celsius

    :param fahrenheit: The temperature to convert to Celsius
    """
    return int(round_half((fahrenheit - 32) / 1.8))


def fahrenheit_to_nuheat(fahrenheit):
    """
    Convert Fahrenheit to a temperature value that NuHeat understands
    Formula f(x) = ((x - 33) * 56) + 33

    :param fahrenheit: The temperature to convert to NuHeat
    """
    return int(round_half(((fahrenheit - 33) * 56) + 33))


def celsius_to_fahrenheit(celsius):
    """
    Convert Celsius to Fahrenheit

    :param celsius: The temperature to convert to Fahrenheit
    """
    return int(round_half(celsius * 1.8 + 32))


def celsius_to_nuheat(celsius):
    """
    Convert Celsius to a temperature value that NuHeat understands
    Formula f(x) = ((x - 33) * 56) + 33

    :param celsius: The temperature to convert to NuHeat
    """
    fahrenheit = celsius_to_fahrenheit(celsius)
    return int(round_half(((fahrenheit - 33) * 56) + 33))


def nuheat_to_fahrenheit(nuheat_temperature):
    """
    Convert the NuHeat temp value to Fahrenheit
    Formula f(x) = ((x - 33) / 56) + 33

    :param nuheat_temperature: The temperature to convert to Fahrenheit
    """
    return int(round_half(((nuheat_temperature - 33) / 56.0) + 33))


def nuheat_to_celsius(nuheat_temperature):
    """
    Convert the NuHeat temp value to Celsius

    :param nuheat_temperature: The temperature to convert to Celsius
    """
    fahrenheit = nuheat_to_fahrenheit(nuheat_temperature)
    return fahrenheit_to_celsius(fahrenheit)