Skip to content

Create persistent convertible temperature objects with a shared global scale

License

Notifications You must be signed in to change notification settings

Hyperdimensionals/tempera

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Tempera

Description

Create temperature objects which return their value in a set 'global' unit scale - either Celsius, Fahrenheit, or Kelvin - using the method getTemperature(). When the Global temperature scale is changed, all temperature objects are returned in that scale.

I created this module to easily convert multiple temperatures displayed on a GUI when a user selects a different scale. I imagine it could be useful any time one has multiple persistant temperatures that need to undergo unit conversion at will.

Using this Module

The Global temperature scale can be set with the method self.setGlobalScale(), which accepts a string representing a unit scale - either 'c', 'f', or 'k', or the full word ('celsius', etc). All Temperature objects will return in this scale with self.getTemperature().

Set or return from a specific scale using self.c for Celsius, self.f for Fahrenheit, and self.k for Kelvin.

See examples file for a more thorough outline of features.

Local Scales

If you want a specific Temperature object to return self.getTemperature() in a different scale regardless of the current global scale, use self.setLocalScale(). Can return to using global scale with self.setLocalScale(False) or self.useGlobalScale().

Rounding

Set the number of decimal places with the decimal_place keyword argument:

from tempera import Temperature

temp1 = Temperature(12, decimal_place=2)

Or with self.setDecimalPlace(int), where int is an integer. The default decimal place is 2. Setting to None will always return a rounded integer.

Intervals

Temperature objects can also represent intervals instead of actual temperatures with the keyword argument isinterval=True. For example, if you wanted to represent an interval of 3 celsius units:

temp_interval = Temperature(3, isinterval=True)

print(temp1.C + temp_interval.C)  # Adds 3 celsius to temp1 and prints

How it functions

The temperature is stored internally in celsius. All setting and getting of the temperature in different scales is a conversion from this 'private' celsius attribute. This attribute is not rounded. Any rounding is done when getTemperature() (or a similar method) is called, so setting a decimal place does not affect the internally stored temperature.