Limit to rows returned and more use of default values

This commit is contained in:
kulbinder dio
2023-01-27 17:16:54 +00:00
parent c5949a4992
commit 16c64e9316
4 changed files with 85 additions and 63 deletions

View File

@@ -20,7 +20,7 @@ def pcd_format(postcode: str):
@log_start_end(log=logger)
def get_estate_sales(postcode: str) -> pd.DataFrame:
def get_estate_sales(postcode: str, limit: int) -> pd.DataFrame:
"""All sales for specified postcode.
@@ -29,6 +29,9 @@ def get_estate_sales(postcode: str) -> pd.DataFrame:
postcode : str
Postcode
limit : int
number of rows to return
Returns
-------
@@ -70,6 +73,8 @@ def get_estate_sales(postcode: str) -> pd.DataFrame:
OPTIONAL {{ ?addr lrcommon:town ?town }}
}}
ORDER BY DESC(?date)
LIMIT {limit}
"""
endpoint = "http://landregistry.data.gov.uk/landregistry/query"
@@ -91,7 +96,10 @@ def get_estate_sales(postcode: str) -> pd.DataFrame:
@log_start_end(log=logger)
def get_towns_sold_prices(
town: str, startdate: str = "2010-01-01", enddate: str = ""
town: str,
startdate: str,
enddate: str,
limit: int,
) -> pd.DataFrame:
"""Get towns sold house price data.
@@ -106,6 +114,9 @@ def get_towns_sold_prices(
enddate : str
endDate
limit : int
number of rows to return
Returns
-------
@@ -152,6 +163,8 @@ def get_towns_sold_prices(
OPTIONAL {{?addr lrcommon:street ?street}}
}}
ORDER BY (?date && ?postcode)
LIMIT {limit}
"""
endpoint = "http://landregistry.data.gov.uk/landregistry/query"

View File

@@ -13,7 +13,7 @@ logger = logging.getLogger(__name__)
@log_start_end(log=logger)
def display_estate_sales(postcode: str, export: str = "") -> None:
def display_estate_sales(postcode: str, limit: int, export: str = "") -> None:
"""Display real estate sales.
Parameters
@@ -21,8 +21,11 @@ def display_estate_sales(postcode: str, export: str = "") -> None:
postcode : str
Postcode
limit : int
number of rows to return
"""
sales = landRegistry_model.get_estate_sales(postcode)
sales = landRegistry_model.get_estate_sales(postcode, limit)
if sales.empty or len(sales) == 0:
console.print(
@@ -49,7 +52,7 @@ def display_estate_sales(postcode: str, export: str = "") -> None:
@log_start_end(log=logger)
def display_towns_sold_prices(
town: str, startdate: str, enddate: str, export: str = ""
town: str, startdate: str, enddate: str, limit: int, export: str = ""
):
"""Get towns sold house price data.
@@ -64,6 +67,9 @@ def display_towns_sold_prices(
enddate : str
endDate
limit : int
number of rows to return
Returns
-------
@@ -71,7 +77,7 @@ def display_towns_sold_prices(
All sales for that town within the date range specified
"""
sales = landRegistry_model.get_towns_sold_prices(town, startdate, enddate)
sales = landRegistry_model.get_towns_sold_prices(town, startdate, enddate, limit)
if sales.empty or len(sales) == 0:
console.print(

View File

@@ -3,7 +3,7 @@ __docformat__ = "numpy"
import argparse
import logging
from datetime import datetime, timedelta
from typing import List
from openbb_terminal.custom_prompt_toolkit import NestedCompleter
@@ -13,6 +13,7 @@ from openbb_terminal.decorators import log_start_end
from openbb_terminal.menu import session
from openbb_terminal.helper_funcs import (
EXPORT_ONLY_RAW_DATA_ALLOWED,
check_positive,
)
from openbb_terminal.rich_config import console, MenuText
from openbb_terminal.parent_classes import BaseController
@@ -21,6 +22,19 @@ from openbb_terminal.alternative.realestate import landRegistry_view
logger = logging.getLogger(__name__)
def get_relative_date(months: int):
date = datetime.now() - timedelta(days=365 / 12 * months)
return date.strftime("%Y-%m-%d")
def start_date():
return get_relative_date(13)
def end_date():
return get_relative_date(1)
class RealEstateController(BaseController):
"""Real Estate Controller class."""
@@ -39,6 +53,7 @@ class RealEstateController(BaseController):
self.town = None
self.postcode = None
self.region = None
self.limit = 25
if session and obbff.USE_PROMPT_TOOLKIT:
choices: dict = self.choices_default
@@ -54,14 +69,14 @@ class RealEstateController(BaseController):
mt.add_cmd("townsales")
mt.add_raw("\n")
mt.add_param("_town", self.town or "")
mt.add_param("_startdate <YYYY-MM-DD>", self.startdate or "")
mt.add_param("_enddate <YYYY-MM-DD>", self.enddate or "")
mt.add_param("_startdate", self.startdate or "")
mt.add_param("_enddate", self.enddate or "")
mt.add_raw("\n")
mt.add_cmd("regionstats")
mt.add_raw("\n")
mt.add_param("_region", self.region or "")
mt.add_param("_startdate <YYYY-MM-DD>", self.startdate or "")
mt.add_param("_enddate <YYYY-MM-DD>", self.enddate or "")
mt.add_param("_startdate", self.startdate or "")
mt.add_param("_enddate", self.enddate or "")
console.print(text=mt.menu_text, menu="UK Real Estate Sales Data")
@@ -84,6 +99,17 @@ class RealEstateController(BaseController):
metavar="postcode",
)
parser.add_argument(
"-l",
"--limit",
help="Number of entries to return",
type=check_positive,
required=False,
dest="limit",
metavar="limit",
default=25,
)
if (
other_args
and "-p" not in other_args[0]
@@ -99,12 +125,10 @@ class RealEstateController(BaseController):
if ns_parser:
if ns_parser.postcode:
landRegistry_view.display_estate_sales(
ns_parser.postcode, export=ns_parser.export
ns_parser.postcode, ns_parser.limit, export=ns_parser.export
)
else:
console.print("[red]Select valid postcode[/red]\n")
else:
console.print("[red]Select valid postcode[/red]\n")
@log_start_end(log=logger)
def call_townsales(self, other_args: List[str]):
@@ -132,9 +156,10 @@ class RealEstateController(BaseController):
"--startdate",
help="Start date that we want sales information for",
type=str,
required=True,
required=False,
dest="startdate",
metavar="startdate",
default=start_date(),
)
parser.add_argument(
@@ -142,9 +167,21 @@ class RealEstateController(BaseController):
"--enddate",
help="End date that we want sales information for",
type=str,
required=True,
required=False,
dest="enddate",
metavar="enddate",
default=end_date(),
)
parser.add_argument(
"-l",
"--limit",
help="Number of entries to return",
type=check_positive,
required=False,
dest="limit",
metavar="limit",
default=25,
)
if (
@@ -155,26 +192,10 @@ class RealEstateController(BaseController):
):
other_args.insert(0, "-t")
if (
other_args
and len(other_args) > 2
and "-s" not in other_args[2]
and "--startdate" not in other_args[2]
and "-h" not in other_args
):
other_args.insert(2, "-s")
if (
other_args
and len(other_args) > 4
and "-e" not in other_args[4]
and "--enddate" not in other_args[4]
and "-h" not in other_args
):
other_args.insert(4, "-e")
ns_parser = self.parse_known_args_and_warn(
parser, other_args, EXPORT_ONLY_RAW_DATA_ALLOWED
parser,
other_args,
EXPORT_ONLY_RAW_DATA_ALLOWED,
)
if ns_parser:
@@ -183,16 +204,13 @@ class RealEstateController(BaseController):
ns_parser.town,
ns_parser.startdate,
ns_parser.enddate,
ns_parser.limit,
ns_parser.export,
)
else:
console.print(
"[red]Select the town you want to know about and a valid date range.[/red]\n"
)
else:
console.print(
"[red]Select the town you want to know about and a valid date range.[/red]\n"
)
@log_start_end(log=logger)
def call_regionstats(self, other_args: List[str]):
@@ -219,9 +237,10 @@ class RealEstateController(BaseController):
"--startdate",
help="Start date that we want sales information for",
type=str,
required=True,
required=False,
dest="startdate",
metavar="startdate",
default=start_date(),
)
parser.add_argument(
@@ -229,9 +248,10 @@ class RealEstateController(BaseController):
"--enddate",
help="End date that we want sales information for",
type=str,
required=True,
required=False,
dest="enddate",
metavar="enddate",
default=end_date(),
)
if (
@@ -242,24 +262,6 @@ class RealEstateController(BaseController):
):
other_args.insert(0, "-r")
if (
other_args
and len(other_args) > 2
and "-s" not in other_args[2]
and "--startdate" not in other_args[2]
and "-h" not in other_args
):
other_args.insert(2, "-s")
if (
other_args
and len(other_args) > 4
and "-e" not in other_args[4]
and "--enddate" not in other_args[4]
and "-h" not in other_args
):
other_args.insert(4, "-e")
ns_parser = self.parse_known_args_and_warn(
parser, other_args, EXPORT_ONLY_RAW_DATA_ALLOWED
)
@@ -276,7 +278,3 @@ class RealEstateController(BaseController):
console.print(
"[red]Select the region you want to know about and a valid date range.[/red]\n"
)
else:
console.print(
"[red]Select the region you want to know about and a valid date range.[/red]\n"
)

View File

@@ -876,8 +876,13 @@ en:
alternative/oss/sh: repo star history
alternative/oss/tr: top starred repos
alternative/realestate/sales: list UK house sales via postcode
alternative/realestate/_postcode: UK Postcode
alternative/realestate/townsales: list UK house sales via town
alternative/realestate/_town: Town
alternative/realestate/_startdate: Start Date <YYYY-MM-DD>
alternative/realestate/_enddate: End Date <YYYY-MM-DD>
alternative/realestate/regionstats: retrieve UK sold house price stats by region
alternative/realestate/_region: Region
econometrics/_data_loc: Looking for data in
econometrics/load: load a dataset (also works with StatsModels datasets)
econometrics/export: export a processed dataset