Coverage for ausdex/main.py : 100.00%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from pathlib import Path
2from typing import List, Union
3import webbrowser
4import typer
7from typing import Optional
8import subprocess
10from .inflation import calc_inflation
11from .location import Location
12from . import viz
14app = typer.Typer()
17def version_callback(value: bool):
18 if value:
19 import importlib_metadata as lib_metadata
20 version = lib_metadata.version("ausdex")
21 typer.echo(version)
22 raise typer.Exit()
25@app.command()
26def repo():
27 """
28 Opens the repository in a web browser.
29 """
30 typer.launch("https://github.com/rbturnbull/ausdex")
33@app.command()
34def docs(live: bool = True):
35 """Builds the documentation.
37 Args:
38 live (bool, optional): Whether or not to use sphinx-autobuild to automatically build the documentation as files are edited. Defaults to True.
39 """
40 root_dir = Path(__file__).parent.parent.resolve()
41 docs_dir = root_dir / "docs"
42 docs_build_dir = docs_dir / "_build/html"
44 if live:
45 command = f"sphinx-autobuild {docs_dir} {docs_build_dir} --open-browser"
46 else:
47 command = f"sphinx-build -E -b html {docs_dir} {docs_build_dir}"
49 subprocess.run(command, shell=True)
51 if not live:
52 index_page = docs_build_dir / "index.html"
53 print(f"Open the index page at {index_page}")
54 webbrowser.open_new("file://" + str(index_page))
57@app.command()
58def inflation(
59 value: float = typer.Argument(..., help="The dollar value to be converted."),
60 original_date: str = typer.Argument(..., help="The date that the value is in relation to."),
61 evaluation_date: str = typer.Option(None, help="The date to adjust the value to. Defaults to the current date."),
62 location: Location = typer.Option(
63 Location.AUSTRALIA, case_sensitive=False, help="The location for calculating the CPI."
64 ),
65):
66 """
67 Adjusts Australian dollars for inflation.
69 Prints output to stdout.
71 Args:
72 value (float): The dollar value to be converted.
73 original_date (str): The date that the value is in relation to.
74 evaluation_date (str, optional): The date to adjust the value to. Defaults to the current date.
75 location (Location, optional): The location for calculating the CPI.
76 Options are 'Australia', 'Sydney', 'Melbourne', 'Brisbane', 'Adelaide', 'Perth', 'Hobart', 'Darwin', and 'Canberra'.
77 Default is 'Australia'.
78 """
80 result = calc_inflation(
81 value=value, original_date=original_date, evaluation_date=evaluation_date, location=location
82 )
83 typer.echo(f"{result:.2f}")
86@app.command()
87def plot_inflation(
88 compare_date: str = typer.Argument(..., help="Date to set relative value of the dollars too."),
89 show: bool = typer.Option(True, help="Whether or not to show the figure in a browser."),
90 output: Path = typer.Option(
91 None,
92 help="The path to where the figure will be saved. Output can be PDF, JPG, PNG or HTML based on the extension.",
93 ),
94 start_date: str = typer.Option(
95 None, help="Date to set the beginning of the time series graph. Defaults to None, which starts in 1948."
96 ),
97 end_date: str = typer.Option(
98 None,
99 help="Date to set the end of the time series graph too. If empty, then the end date to the most recent quarter.",
100 ),
101 value: float = typer.Option(
102 1.0, help="Value you in `compare_date` dollars to plot on the time series. Defaults to 1."
103 ),
104 location: Location = typer.Option(
105 Location.AUSTRALIA,
106 help="The location for calculating the CPI.",
107 case_sensitive=False,
108 ),
109):
110 """
111 Plots a time series of dollar values attached to a particular date's dollar value.
113 Args:
114 compare_date (str): Date to set relative value of the dollars too.
115 out (Path): The path to where the figure will be saved. Output can be PDF, JPG, PNG or HTML based on the extension.
116 start_date (str, optional): Date to set the beginning of the time series graph. Defaults to None, which starts in 1948.
117 end_date (str, optional): Date to set the end of the time series graph too. Defaults to None, which will set the end date to the most recent quarter.
118 value (float, optional): Value you in `compare_date` dollars to plot on the time series. Defaults to 1.
119 """
120 from ausdex.viz import plot_inflation_timeseries
122 fig = plot_inflation_timeseries(
123 compare_date=compare_date, start_date=start_date, end_date=end_date, value=value, location=location
124 )
125 if output:
126 print(f"Writing figure to '{output}'.")
127 viz.write_fig(fig, output)
128 if show:
129 fig.show()
132@app.command()
133def plot_cpi(
134 show: bool = typer.Option(True, help="Whether or not to show the figure in a browser."),
135 output: Path = typer.Option(
136 None,
137 help="The path to where the figure will be saved. Output can be PDF, JPG, PNG or HTML based on the extension.",
138 ),
139 start_date: str = typer.Option(
140 None, help="Date to set the beginning of the time series graph. Defaults to None, which starts in 1948."
141 ),
142 end_date: str = typer.Option(
143 None,
144 help="Date to set the end of the time series graph too. If empty, then the end date to the most recent quarter.",
145 ),
146 location: List[Location] = typer.Option(
147 None,
148 help="The location for calculating the CPI.",
149 case_sensitive=False,
150 ),
151 title: str = typer.Option(None, help="A custom title of the plot."),
152):
153 """
154 Plot the Australian CPI over time.
156 Args:
157 show (bool): Whether or not to show the figure in a browser. Default True.
158 output (Path): The path to where the figure will be saved. Output can be PDF, SVG, JPG, PNG or HTML based on the extension.
159 start_date (str, optional): Date to set the beginning of the time series graph. If empty, it defaults to 1948.
160 end_date (str, optional): Date to set the end of the time series graph too. If empty, then the end date to the most recent quarter.
161 location (List[location]): The location for calculating the CPI.
162 title (str, optional): A custom title of the plot.
163 """
164 from ausdex.viz import plot_cpi_timeseries
166 fig = plot_cpi_timeseries(start_date=start_date, end_date=end_date, locations=location, title=title)
167 if output:
168 print(f"Writing figure to '{output}'.")
169 viz.write_fig(fig, output)
170 if show:
171 fig.show()
174@app.command()
175def plot_cpi_change(
176 show: bool = typer.Option(True, help="Whether or not to show the figure in a browser."),
177 start_date: str = typer.Option(
178 None, help="Date to set the beginning of the time series graph. Defaults to None, which starts in 1948."
179 ),
180 end_date: str = typer.Option(
181 None,
182 help="Date to set the end of the time series graph too. If empty, then the end date to the most recent quarter.",
183 ),
184 output: Path = typer.Option(
185 None,
186 help="The path to where the figure will be saved. Output can be PDF, JPG, PNG or HTML based on the extension.",
187 ),
188 location: List[Location] = typer.Option(
189 None,
190 help="The location for calculating the CPI.",
191 case_sensitive=False,
192 ),
193 title: str = typer.Option(None, help="A custom title of the plot."),
194):
195 """
196 Produces a plot of the percentage change from corresponding quarter of previous year.
198 Args:
199 show (bool): Whether or not to show the figure in a browser. Default True.
200 output (Path): The path to where the figure will be saved. Output can be PDF, SVG, JPG, PNG or HTML based on the extension.
201 location (List[location]): The location for calculating the CPI.
202 """
203 from ausdex.viz import plot_cpi_change
205 fig = plot_cpi_change(
206 start_date=start_date,
207 end_date=end_date,
208 output=output,
209 locations=location,
210 title=title,
211 )
212 if show:
213 fig.show()
216@app.callback()
217def main(
218 version: Optional[bool] = typer.Option(None, "--version", "-v", callback=version_callback, is_eager=True),
219):
220 """Adjusts Australian dollars for inflation."""
221 pass