Skip to main content

tw-powercalc

繁體中文

A Python package for estimating household appliance power consumption and residential electricity costs in Taiwan.

The project uses object-oriented programming principles to separate power calculation, tariff management, billing periods, electricity billing, and command-line interaction.

Project Status

  • Version: 0.8.0
  • Automated tests: 185 passed (current development state)
  • Statement coverage: 99%
  • Tested environment: Ubuntu, Python 3.10-3.14

Features

  • Single and multiple appliance power consumption calculation.
  • Daily and monthly electricity usage estimation.
  • Taiwan residential progressive electricity billing.
  • Summer and non-summer tariff selection.
  • Automatic month length and leap year handling.
  • Additional electricity cost estimation.
  • Official Taiwan Power Company tariff retrieval.
  • Local tariff caching and automatic offline fallback.
  • Command-line interface and reusable Python API.
  • Input validation and exception handling.

Installation

The package currently requires Python 3.10 or later. Python 3.10 through 3.14 have passed all automated tests on Ubuntu.

Install from the local project directory:

python -m pip install .

For development:

python -m pip install -e ".[dev]"

Public PyPI installation is not yet available.

Command-Line Usage

Basic power consumption calculation

Calculate the monthly electricity consumption of a 200 W appliance used for 8 hours per day.

tw-powercalc 200 8 --month 2026-06

Expected results using the bundled tariff:

  • Daily consumption: 1.60 kWh
  • Monthly consumption: 48.00 kWh
  • Estimated standalone cost: 85.44 NTD

Multiple appliances

tw-powercalc 200 8 \
    --add 100 10 \
    --add 60 5 \
    --month 2026-06

Additional electricity cost

Estimate the additional cost of an appliance when the household already consumes 110 kWh per month.

tw-powercalc 200 8 \
    --base 110 \
    --month 2026-06

Offline mode

tw-powercalc 200 8 --month 2026-06 --offline

Alternative module entry point

python -m tw_powercalc 200 8 --month 2026-06

Command-line arguments

Argument Description
power Appliance power in watts
hours Daily usage in hours, from 0 to 24
--month YYYY-MM Billing month; defaults to the current month in Taiwan
--add POWER HOURS Add another appliance; repeatable
--base KWH Existing monthly household electricity consumption
--offline Use local tariff data without network access
--help Display command-line help

Python API

Single appliance

from tw_powercalc import Appliance, PowerCalculator

appliance = Appliance(200, 8)
calculator = PowerCalculator(appliance, days=30)

daily, monthly = calculator.calculate_usage()

print(daily)
print(monthly)

Output:

1.6
48.0

Multiple appliances

from tw_powercalc import (
    Appliance,
    ApplianceGroupCalculator
)

appliances = [
    Appliance(200, 8),
    Appliance(100, 10),
    Appliance(60, 5)
]

calculator = ApplianceGroupCalculator(
    appliances,
    days=30
)

daily, monthly = calculator.calculate_usage()

print(daily)
print(monthly)

Billing period

from tw_powercalc import BillingPeriod

period = BillingPeriod.from_string("2026-06")

print(period.days)
print(period.is_summer)

Electricity billing

from tw_powercalc import (
    OnlineTariffProvider,
    BillCalculator
)

provider = OnlineTariffProvider()
table = provider.load()

calculator = BillCalculator(table)

cost = calculator.calculate_bill(
    48,
    summer=True
)

additional_cost = calculator.calculate_additional_bill(
    110,
    48,
    summer=True
)

print(cost)
print(additional_cost)

This example requires network access and uses the available official tariff.

Tariff Data Source

Electricity tariff data is obtained from Taiwan Power Company (Taipower).

Official JSON endpoint:

https://service.taipower.com.tw/data/opendata/apply/file/d007008/001.json

The package includes a bundled tariff snapshot with an effective date of October 1, 2025.

The tariff parser validates:

  • Supported schema version.
  • Currency and pricing units.
  • Effective date.
  • Tariff tier continuity.
  • Valid numeric electricity rates.
  • Presence of an unlimited final tier.

Cache and Offline Fallback

The default CLI behavior is to retrieve official tariff data online.

On Linux, validated data is cached at:

~/.cache/tw-powercalc/tariff.json

Tariff sources are evaluated in the following order:

  1. Official online tariff.
  2. Valid local cache.
  3. Bundled tariff.

Offline mode skips online retrieval entirely.

If the local cache is missing or corrupted, the bundled tariff is used instead. A successful online retrieval can repair a corrupted cache.

Offline tariff data may be outdated.

Project Architecture

tw-powercalc/
├── src/
│   └── tw_powercalc/
│       ├── __init__.py
│       ├── __main__.py
│       ├── powercalc.py
│       ├── tariff.py
│       ├── tariff_provider.py
│       ├── billing_period.py
│       ├── bill_calculator.py
│       ├── cli.py
│       └── data/
│           └── tariff.json
├── tests/
├── examples/
├── pyproject.toml
└── README.md
Module Responsibility
powercalc Appliance and power consumption calculation
tariff Tariff data models and validation
tariff_provider Online, local and fallback data providers
billing_period Billing month and season management
bill_calculator Progressive electricity billing
cli Command-line interface

Testing

Run all automated tests:

python -m pytest

Generate a statement coverage report:

python -m pytest \
    --cov=tw_powercalc \
    --cov-report=term-missing \
    --cov-report=html

Version 0.6.0 test results:

Test module Passed
Power calculation 27
Bill calculation 37
Billing period 53
Tariff validation 29
Tariff provider 12
CLI integration and module entry point 20
Total 178

Overall statement coverage: 99%.

The current development state adds seven fallback tests: 185 tests passed, with 99% statement coverage.

Limitations

  • Designed for Taiwan residential non-time-of-use tariffs.
  • Estimates one full calendar month at a time.
  • Does not calculate two-month or cross-season bills.
  • Does not maintain a complete historical tariff database.
  • Future estimates use the available tariff.
  • Appliance power consumption is assumed to be constant.
  • Does not reproduce every adjustment on an official bill.
  • Results are estimates, not official electricity bills.

License

The Python source code is licensed under the MIT License.

Copyright (c) 2026 hello world 07.

The bundled Taipower tariff data is provided under the Open Government Data License, version 1.0.

Acknowledgements

Taiwan Power Company (Taipower) for providing electricity tariff data.

Developed as an Object-Oriented Programming course project.

Design Documentation

Metadata

Release files for tw-powercalc 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tw-powercalc 0.8.0
File Size Uploaded Provenance Actions
tw_powercalc-0.8.0.tar.gz 40.5 kB Without provenance Details for tw_powercalc-0.8.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tw-powercalc 0.8.0
File Interpreter ABI Platform Actions
tw_powercalc-0.8.0-py3-none-any.whl Python 3 none any Details for tw_powercalc-0.8.0-py3-none-any.whl

Total release size: 68.3 kB

Release files / tw_powercalc-0.8.0.tar.gz

Download URL tw_powercalc-0.8.0.tar.gz
Size 40.5 kB
Tags Source
SHA-256 checksum
How to use checksums
fc0a9e56c4357354afac230feeba7fe39eee87ac21bc99d0e84141d28db72ed8
BLAKE2b-256 checksum
How to use checksums
2b08a66093a6dead76bd08cfc98ecd12d44f064597664c591ae72297a4fb34aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / tw_powercalc-0.8.0-py3-none-any.whl

Download URL tw_powercalc-0.8.0-py3-none-any.whl
Size 27.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2668c41a8fcd6c42798b47948e482576b9a92bef821ba5aaa959982725770b2a
BLAKE2b-256 checksum
How to use checksums
d551f1994cfafc7f0fbac6faceed7bc814e6e78cda9eb90e28ef9ad70ae9e3ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for tw-powercalc 0.8.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.8.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page