Metadata-Version: 2.4
Name: tw-powercalc
Version: 0.8.0
Summary: Taiwan residential power consumption and electricity bill calculator
Author: hello world 07
License-Expression: MIT AND OGDL-Taiwan-1.0
Keywords: taiwan,taipower,electricity,electricity-bill,power-consumption,energy,calculator
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Education
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Utilities
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: src/tw_powercalc/data/LICENSE-OGDL.txt
License-File: src/tw_powercalc/data/ATTRIBUTION.txt
Requires-Dist: requests<3,>=2.32
Requires-Dist: tzdata
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: pytest-cov; extra == "dev"
Requires-Dist: build; extra == "dev"
Requires-Dist: twine; extra == "dev"
Dynamic: license-file

# tw-powercalc

[繁體中文](README.zh-TW.md)

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:

```bash
python -m pip install .
```

For development:

```bash
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.

```bash
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

```bash
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.

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

### Offline mode

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

### Alternative module entry point

```bash
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

```python
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:

```text
1.6
48.0
```

### Multiple appliances

```python
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

```python
from tw_powercalc import BillingPeriod

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

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

### Electricity billing

```python
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:

```text
~/.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

```text
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:

```bash
python -m pytest
```

Generate a statement coverage report:

```bash
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](LICENSE).

Copyright (c) 2026 hello world 07.

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

- License: https://data.gov.tw/license
- Dataset: https://data.gov.tw/dataset/17060
- [Data attribution](src/tw_powercalc/data/ATTRIBUTION.txt)

## Acknowledgements

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

Developed as an Object-Oriented Programming course project.

## Design Documentation

- [System Architecture](docs/architecture.md)
- [UML Class Diagram](docs/class_diagram.md)
- [Object-Oriented Design](docs/oop_design.md)
- [SOLID Design Principles](docs/solid_design.md)
