GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — StarterLearningPython/python3code/chapter04/temperature.py at master · smajun/StarterLearningPython

Expand file tree

/

temperature.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
40 lines (34 loc) · 1.1 KB
/

temperature.py

Copy path

File metadata and controls

40 lines (34 loc) · 1.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#coding:utf-8
'''
filename: temperature.py
'''
class Temperature:
coefficient = {"c": (1.0, 0.0, -273.15), "f": (1.8, -273.15, 32.0)}
def __init__(self, **kargs):
assert set(kargs.keys()).intersection("kfcKFC"), "invalid arguments {0}".format(kargs)
name, value = kargs.popitem()
name = name.lower()
setattr(self, name, float(value))
def __getattr__(self, name):
try:
eq = self.coefficient[name.lower()]
except KeyError:
raise AttributeError(name)
return (self.k + eq[1]) * eq[0] + eq[2]
def __setattr__(self, name, value):
name = name.lower()
if name in self.coefficient:
eq = self.coefficient[name]
self.k = (value - eq[2]) / eq[0] - eq[1]
elif name == 'k':
object.__setattr__(self, name, value)
else:
raise AttributeError(name)
def __str__(self):
return "{0}K".format(self.k)
def __repr__(self):
return "Temperature(K={0}".format(self.k)
t = Temperature(c=64)
print("c=64, f=", t.f)
t.f = 23
print("f=23, c=", t.c)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.