Skip to content

Commit

Permalink
added docs for Decimal and Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
dantownsend committed May 12, 2021
1 parent 1f51ba0 commit a24e811
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
46 changes: 46 additions & 0 deletions docs/src/supported_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Targ currently supports basic Python types:
* int
* bool
* float
* Decimal
* Optional

You must specify a type annotation for all function arguments, so Targ can
convert the input it receives from the command line into the correct type.
Expand Down Expand Up @@ -103,3 +105,47 @@ Example usage:
>>> python main.py compound_interest 0.05 5
0.27628156250000035
Decimal
-------

.. code-block:: python
from decimal import Decimal
def compound_interest(interest_rate: Decimal, years: int):
print(((interest_rate + 1) ** years) - 1)
Example usage:

.. code-block:: bash
>>> python main.py compound_interest 0.05 5
0.2762815625
Optional
--------

.. code-block:: python
from typing import Optional
def print_address(
number: int, street: str, postcode: str, city: Optional[str] = None
):
address = f"{number} {street}"
if city is not None:
address += f", {city}"
address += f", {postcode}"
print(address)
Example usage:

.. code-block:: bash
>>> python print_address --number=1 --street="Royal Avenue" --postcode="XYZ 123" --city=London
1 Royal Avenue, London, XYZ 123
>>> python print_address --number=1 --street="Royal Avenue" --postcode="XYZ 123"
1 Royal Avenue, XYZ 123
42 changes: 42 additions & 0 deletions example_script.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import asyncio
import decimal
import typing as t

from targ import CLI

Expand Down Expand Up @@ -44,6 +46,32 @@ def say_hello(name: str, greeting: str = "hello"):
print(f"{greeting} {name}")


# print_address --number=1 --street="Royal Avenue" --postcode="XYZ 123"
# --city=London
def print_address(
number: int, street: str, postcode: str, city: t.Optional[str] = None
):
"""
Print out the full address.
:param number:
House number, e.g. 8
:street:
Street name, e.g. "Royal Avenue"
:postcode:
e.g. "XYZ 123"
:city:
e.g. London
"""
address = f"{number} {street}"
if city is not None:
address += f", {city}"
address += f", {postcode}"

print(address)


def print_pi(precise: bool = False):
"""
Print out the digits of Pi.
Expand All @@ -69,6 +97,18 @@ def compound_interest(interest_rate: float, years: int):
print(((interest_rate + 1) ** years) - 1)


def compound_interest_decimal(interest_rate: decimal.Decimal, years: int):
"""
Work out the compound interest over the given number of years.
:param interest_rate:
The annual interest rate e.g. 0.05
:param years:
The number of years over which to compound.
"""
print(((interest_rate + 1) ** years) - 1)


def create(username: str):
"""
Create a new user.
Expand Down Expand Up @@ -98,7 +138,9 @@ async def timer(seconds: int):
cli.register(add)
cli.register(print_pi)
cli.register(compound_interest)
cli.register(compound_interest_decimal)
cli.register(create, group_name="user")
cli.register(timer)
cli.register(add, command_name="sum")
cli.register(print_address)
cli.run()

0 comments on commit a24e811

Please sign in to comment.