Skip to content

Commit

Permalink
v0.2.0 pre-release commit
Browse files Browse the repository at this point in the history
- Changes to "setup.py".
- Updated requirements.
- Changed method name "set()" to "set_precision()".
- Implemented strict typing.
- Updates to docstring.
- Changed parameter name "digit" to "number".
- Updated README.

Signed-off-by: schlopp96 <71921821+schlopp96@users.noreply.github.com>
  • Loading branch information
schlopp96 committed Jun 28, 2022
1 parent 14ef9e0 commit f5e8aa4
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 38 deletions.
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- To install _**`SetPrecision`**_ using `pip`, enter the following:

```python
pip install SetPrecision
python -m pip install SetPrecision
```

- Done!
Expand All @@ -28,15 +28,19 @@
1. Before use, navigate to intended installation location, and create a new directory.

2. Clone repository with the git client of your preference.
2. Clone repository with the git client of your preference using the following command:

- ```bash
git clone https://github.com/schlopp96/SetPrecision/releases/latest
```

3. Install all dependencies for this package within said directory using:

```text
- ```bash
pip install -r requirements.txt
```
```

- (Optional) move installation directory to `"path/to/Python/Libs/site_packages"` to be able to import this package to a Python program like any other importable package.
- **(Optional)**: move installation directory to `"path/to/Python/Libs/site_packages"` to be able to import this package to a Python program like any other importable package.

- Done!

Expand All @@ -47,33 +51,33 @@
- In order to use _**`SetPrecision`**_, start by importing the module to your Python environment:

```python
from SetPrecision import set
from SetPrecision import set_precision
```

- Now, simply call the `set` method and enter your desired number to be formatted as the `digit` parameter, and the level of precision as the `precision` parameter:
- Now, simply call the `set_precision` method and enter your desired number to be formatted as the `number` parameter, and the level of precision as the `precision` parameter:

```python
>>> testA = 3.141592653589793 # Not necessary to set number as variable.
>>> testA = set(testA, 2)
>>> testA = set_precision(testA, 2)
>>> print(testA)
'3.15'
>>> testB = 3.141592653589793
>>> testB = set(testB, 4)
>>> testB = set_precision(testB, 4)
>>> print(testB)
'3.1416'
```

> Note that the output is automatically rounded up when `digit >= 5`, and down when `digit < 5`.
> Note that the output is automatically rounded up when `number >= 5`, and down when `number < 5`.

- Both params can be entered in string format, and will output successfully assuming that both paramaters can be cast to their appropriate types.
- Both params can be entered in string format, and will output successfully assuming that both parameters can be cast to their appropriate types.
- This is done automatically.

---
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
setuptools==58.1.0
setuptools>=58.1.0
2 changes: 1 addition & 1 deletion setPrecision/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .SetPrecision import set
from .SetPrecision import set_precision
30 changes: 16 additions & 14 deletions setPrecision/setPrecision.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,33 @@
# -*- coding: utf-8 -*-


def set(digit: float | str, precision: int | str = 1) -> str | Exception:
"""Returns a string representation of a number :float:`digit` formatted to the given precision of :int:`precision` significant digits.
def set_precision(number: float | str,
precision: int | str = 1) -> str | Exception:
"""Returns a string representation of a :param:`number` formatted to the given precision of :param:`precision` significant figures.
Example:
```python
>>> from SetPrecision import set
>>> x = 3.1459 # Not necessary to set number as variable.
>>> y = set(digit=x, precision=2)
>>> print(y)
>>> from SetPrecision import set_precision
>>> decimal = set_precision(number=3.1459, precision=2)
>>> print(decimal)
'3.15'
```
---
:param digit: float/decimal to be formatted. Will attempt to convert strings to floats.
:type digit: float | str
:param precision: number of decimal places to which `digit` is set to contain. Will attempt to convert strings to integers, defaults to 1.
:type precision: int | str
:return: final string representation of formatted float/decimal `digit`.
:rtype: str
:param number: float/decimal to be formatted. Will attempt to convert strings to floats
:type number: :class:`float` | :class:`str`
:param precision: number of decimal places to which :param:`number` is set to contain. Will attempt to convert strings to integers, defaults to 1
:type precision: :class:`int` | :class:`str`, optional
:return: final string representation of formatted float/decimal :param:`number`
:rtype: :class:`str`
"""
try:
digit = float(digit)
number = float(number)
precision = int(precision)
return f"{digit:.{precision}f}"

return f"{number:.{precision}f}"

except Exception as exc:
return exc
12 changes: 6 additions & 6 deletions setPrecision/tests/setPrecision_test.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from ..SetPrecision import set
from ..SetPrecision import set_precision


def test_setPrecisionA() -> None:
assert set(3.1415926, 3) == '3.142'
assert set_precision(3.1415926, 3) == '3.142'


def test_setPrecisionB() -> None:
assert set(10.5, 0) == '10'
assert set_precision(10.5, 0) == '10'


def test_setPrecisionC() -> None:
assert set(5280.40836894, 2) == '5280.41'
assert set_precision(5280.40836894, 2) == '5280.41'


def test_setPrecisionD() -> None:
assert set(150, 5) == '150.00000'
assert set_precision(150, 5) == '150.00000'


def test_setPrecisionE() -> None:
assert set(1485809.4861172) == '1485809.5'
assert set_precision(1485809.4861172) == '1485809.5'
23 changes: 18 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
reqs = pathlib.Path("requirements.txt").read_text()
setup(
name="SetPrecision",
version="0.1.0",
version="0.2.0",
description=
"`SetPrecision` is a small module providing a simple way to set the precision of a floating point number or decimal to the desired amount of significant digits.",
"Python module providing an easy way to set the precision of a floating-point number/decimal to the desired amount of significant figures.",
url='https://github.com/schlopp96/SetPrecision',
author='schlopp96',
author_email='schloppdaddy@gmail.com',
Expand All @@ -31,7 +31,20 @@
"Topic :: Utilities",
],
keywords=[
'SetPrecision', 'float', 'decimal', 'precision', 'sig', 'figs',
'significant', 'digits', 'scientific', 'notation', 'math',
'conversion', 'script', 'point', 'floating'
'SetPrecision',
'float',
'decimal',
'precision',
'sig',
'figs',
'significant',
'digits',
'scientific',
'notation',
'math',
'conversion',
'script',
'point',
'floating',
'scientific-notation',
])

0 comments on commit f5e8aa4

Please sign in to comment.