-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added LICENSE added logging added better version
- Loading branch information
Showing
10 changed files
with
331 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
FROM python:3.9.17-bookworm as poetry | ||
ENV POETRY_VERSION = "1.5.1" | ||
FROM python:3.11.9-bookworm AS poetry | ||
ENV POETRY_VERSION="1.8.3" | ||
|
||
RUN pip install "poetry==${POETRY_VERSION}" | ||
|
||
|
@@ -16,7 +16,7 @@ COPY calculate_pi /calculate_pi/calculate_pi/ | |
RUN poetry build | ||
|
||
|
||
FROM python:3.9.17-bookworm | ||
FROM python:3.11.9-bookworm | ||
LABEL maintainer="Erik Ferlanti <[email protected]>" | ||
|
||
# Update OS | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
BSD 3-Clause License | ||
|
||
Copyright (c) 2024, Texas Advanced Computing Center | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
* Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,71 @@ | ||
Calculate PI | ||
============ | ||
# Calculate PI | ||
|
||
A python poetry project demo for calculating PI. | ||
|
||
Installation | ||
------------ | ||
## Prerequisites | ||
|
||
- Git | ||
- Docker | ||
- Python >= 3.11 (prefer using [asdf](https://asdf-vm.com/) or [pyenv](https://github.com/pyenv/pyenv) to system python) | ||
- [Poetry](https://python-poetry.org/) (prefer [asdf-poetry](https://github.com/asdf-community/asdf-poetry) plugin or installing with [pipx](https://github.com/pypa/pipx)) | ||
|
||
```console | ||
> curl -sSL https://install.python-poetry.org | python3 - | ||
``` | ||
|
||
- poetry-bumpversion plugin | ||
```console | ||
> poetry self add poetry-bumpversion | ||
``` | ||
|
||
## Installation | ||
|
||
```console | ||
> git clone [email protected]:eriksf/calculate-pi.git | ||
> cd calculate-pi | ||
> poetry install | ||
``` | ||
|
||
## Usage | ||
|
||
```console | ||
> calculate-pi --help | ||
Usage: calculate-pi [OPTIONS] NUMBER | ||
|
||
Calculate pi using a Monte Carlo estimation. | ||
|
||
NUMBER is the number of random points. | ||
|
||
Options: | ||
--version Show the version and exit. | ||
--log-level [NOTSET|DEBUG|INFO|WARNING|ERROR|CRITICAL] | ||
Set the log level [default: 20] | ||
--log-file PATH Set the log file | ||
--help Show this message and exit. | ||
``` | ||
$ git clone [email protected]:eriksf/calculate-pi.git | ||
$ cd calculate-pi | ||
$ poetry install | ||
|
||
## Development | ||
|
||
To update the version, use the `poetry version <major|minor|patch>` command (aided by the poetry-bumpversion plugin): | ||
|
||
```console | ||
> poetry version patch | ||
Bumping version from 0.3.0 to 0.3.1 | ||
poetry_bumpversion: processed file calculate_pi/version.py | ||
``` | ||
|
||
This will update the version in both the `pyproject.toml` and the `calculate_pi/version.py` files. If you want to test the version bump before updating files, you can use the `--dry-run` option: | ||
|
||
```console | ||
> poetry version patch --dry-run | ||
Bumping version from 0.3.0 to 0.3.1 | ||
poetry_bumpversion: processed file calculate_pi/version.py | ||
``` | ||
|
||
After updating the version and committing the changes back to the repo, you should `tag` the repo to match this version: | ||
|
||
```console | ||
> git tag -a 0.3.1 -m "Version 0.3.1" | ||
> git push origin 0.3.1 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,70 @@ | ||
#!/usr/bin/env python3 | ||
import click | ||
from random import random as r | ||
import logging | ||
from math import pow as p | ||
from sys import argv | ||
from random import random as r | ||
|
||
import click | ||
from click_loglevel import LogLevel | ||
|
||
from .version import __version__ | ||
|
||
LOG_FORMAT = '%(asctime)s [%(name)s.%(funcName)s - %(levelname)s] %(message)s' | ||
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT) | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
VERSION = '0.1.0' | ||
def set_logging_level(ctx, param, value): | ||
""" | ||
Callback function for click that sets the logging level. | ||
""" | ||
logger.setLevel(value) | ||
return value | ||
|
||
|
||
def set_log_file(ctx, param, value): | ||
""" | ||
Callback function for click that sets a log file. | ||
""" | ||
if value: | ||
fileHandler = logging.FileHandler(value, mode='w') | ||
logFormatter = logging.Formatter(LOG_FORMAT) | ||
fileHandler.setFormatter(logFormatter) | ||
logger.addHandler(fileHandler) | ||
return value | ||
|
||
|
||
@click.command() | ||
@click.version_option(VERSION) | ||
@click.version_option(__version__) | ||
@click.option('--log-level', type=LogLevel(), default=logging.INFO, is_eager=True, callback=set_logging_level, help='Set the log level', show_default=True) | ||
@click.option('--log-file', type=click.Path(writable=True), is_eager=True, callback=set_log_file, help='Set the log file') | ||
@click.argument('number', type=click.INT, required=True) | ||
def main(number): | ||
"""Calculate pi using Monte Carlo estimation. | ||
def main(log_level, log_file, number): | ||
"""Calculate pi using a Monte Carlo estimation. | ||
NUMBER is the number of random points. | ||
""" | ||
|
||
logger.debug(f"Logging is set to level {logging.getLevelName(log_level)}") | ||
if log_file: | ||
logger.debug(f"Log file is {log_file}") | ||
|
||
attempts = number | ||
inside = 0 | ||
tries = 0 | ||
logger.debug(f"Attempting to calculate pi using {attempts} random points") | ||
|
||
# Try the specified number of random points | ||
while (tries < attempts): | ||
tries += 1 | ||
if (p(r(),2) + p(r(),2) < 1): | ||
inside += 1 | ||
|
||
logger.debug(f"Found {inside} points inside the circle") | ||
|
||
# Compute and print a final ratio | ||
print( f'Final pi estimate from {attempts} attempts = {4*(inside/tries)}' ) | ||
try: | ||
print( f'Final pi estimate from {attempts} attempts = {4*(inside/tries)}' ) | ||
except Exception as e: | ||
logger.exception(e, stack_info=True) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__version__ = '0.4.0' |
Oops, something went wrong.