Skip to content

Instantly share code, notes, and snippets.

@lueo
Created April 16, 2012 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lueo/2396916 to your computer and use it in GitHub Desktop.
Save lueo/2396916 to your computer and use it in GitHub Desktop.
台灣電費計算
# -*- coding: utf-8 -*-
# Calculate basic electric usage cost in Taiwan
#
# From: http://www.taipower.com.tw/TaipowerWeb/upload/files/11/main_3_6_3.pdf
# Update (4/13/2012): 最新電價表 http://www.moea.gov.tw/Mns/populace/news/wHandNews_File.ashx?news_id=25156&serial_no=2
#
# ===> 以下為舊電價表
#四、表燈用電
# (一)非時間電價
#分 類
#非營業用
#夏 月
#(6月1日至9月30日)
#110 度以下部分 每 度 2.10 元
#111~330 度部分 每 度 3.02
#331~500 度部分 每 度 4.05
#501~700 度部分 每 度 4.51
#701 度以上部分 每 度 5.10
#非 夏 月
#(夏月以外時間)
#110 度以下部分 每 度 2.10 元
#111~330 度部分 每 度 2.68
#331~500 度部分 每 度 3.27
#501~700 度部分 每 度 3.55
#701 度以上部分 每 度 3.97
#註:
#1.公私立各級學校用電按非營業用電第一段單價計收(公私立國中小學電價按附表三電價計收)。
#2.公用路燈按附表二電價計收。
#3. 用戶因實施隔月抄表、收費,其計費之分段度數概加倍計算。
#
# ===> 新電價表 (5/15/2012實施)
#
#四、表燈用電
# (一)非時間電價
#分 類
#非營業用
#夏 月
#(6月1日至9月30日)
#120 度以下部分 每 度 2.10 元
#121~330 度部分 每 度 3.47
#331~500 度部分 每 度 4.89
#501~700 度部分 每 度 5.67
#701 度以上部分 每 度 6.43
#非 夏 月
#(夏月以外時間)
#120 度以下部分 每 度 2.10 元
#121~330 度部分 每 度 3.13
#331~500 度部分 每 度 4.11
#501~700 度部分 每 度 4.71
#701 度以上部分 每 度 5.30
import nose
def elec_calc(watts, is_summer=False, is_new=False):
table_summer = [
(121, 2.10),
(331, 3.47),
(501, 4.89),
(701, 5.67),
(-1, 6.43)
]
table_not_summer = [
(121, 2.10),
(331, 3.13),
(501, 4.11),
(701, 4.71),
(-1, 5.30)
]
table_summer_old = [
(111, 2.1),
(331, 3.02),
(501, 4.05),
(701, 4.51),
(-1, 5.1)
]
table_not_summer_old = [
(111, 2.1),
(331, 2.68),
(501, 3.27),
(701, 3.55),
(-1, 3.97)
]
table = table_not_summer
if is_summer:
table = table_summer
cost = 0
last_meter = 0
for meter, rate in table:
if watts > meter and meter != -1:
cost += (meter - last_meter) * rate
else:
cost += (watts - last_meter) * rate
break
last_meter = meter
return cost
def main():
print elec_calc(1802.44, is_summer=True)
def test_elec_calc():
# Test is_summer=True
test_pair = [
(1, 2.10),
(5, 10.5),
(110.5, 232.05),
(115, 245.18),
(331, 897.5),
(339, 929.9),
(501, 1586),
(550, 1806.99),
(700, 2483.49),
(701, 2488),
(790, 2941.9),
]
for watts, desired in test_pair:
check_elec_calc(watts, desired, is_summer=True)
# Test is_summer=True
test_pair2 = [
(1, 2.1),
(5, 10.5),
(110.5, 232.05),
(115, 243.82),
(331, 822.7),
(339, 848.86),
(501, 1378.6),
(550, 1552.55),
(700, 2085.05),
(701, 2088.6),
(790, 2441.93)
]
for watts, desired in test_pair2:
check_elec_calc(watts, desired)
def check_elec_calc(watts, desired, is_summer=False):
nose.tools.assert_almost_equal(elec_calc(watts, is_summer), desired)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment