Skip to content

Commit

Permalink
add github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
mazarimono committed Nov 8, 2023
1 parent 9f3bdfb commit 922c5c6
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 127 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Python Prepro

on: [push]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: set up python
uses: actions/setup-python@v4
with:
python-version: '3.12'
- name: Install dependencies
run: |
python -m pip install -U pip
pip install -r dev_requirements.txt
- name: Test with pytest
run: |
python -m pytest tests/test_unit_citycode_class.py tests/test_unit_proptransactions_class.py
- name: code formatter
run: |
python -m isort .
python -m black *
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## 開発メモ


### add-new-feature

- お値段の通貨選択機能の追加を検討中。


### 20231020

- released version_0.1
Expand Down
2 changes: 1 addition & 1 deletion dev_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ flake8
mypy
pytest
build
wheel
wheel
3 changes: 1 addition & 2 deletions j_realty_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from . import j_realty_en
from . import j_realty_jp
from . import j_realty_en, j_realty_jp
68 changes: 34 additions & 34 deletions j_realty_api/j_realty_en.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import json
import math
from dataclasses import dataclass
from typing import Union
from datetime import datetime
from typing import Union

import numpy as np
import pandas as pd
Expand All @@ -23,9 +23,9 @@
class CityCode:

"""
A class that, when given a prefecture name, returns data containing
the names and codes of the municipalities in that prefecture. When a
municipality name is specified, it returns the municipality code.
A class that, when given a prefecture name, returns data containing
the names and codes of the municipalities in that prefecture. When a
municipality name is specified, it returns the municipality code.
"""

Expand All @@ -48,31 +48,31 @@ def pref_code(self) -> str:

@property
def city_json(self) -> dict[str]:
'''
"""
api returns each pref's city_codes json.
Returns:
Dict[str]: JSON with {'cityname' : 'citycode'}
'''
"""
r = requests.get(self.base_path, params={"area": self.pref_code})
city_json = r.json()
new_list = list()
for d in city_json['data']:
d['name'] = d['name'].lower()
for d in city_json["data"]:
d["name"] = d["name"].lower()
new_list.append(d)
city_json['data'] = new_list
city_json["data"] = new_list
return city_json

def city_code(self, city_name: str) -> str:
'''
"""
Searches for the city code corresponding to the provided
city name from the city_json.
Args:
city_name str: City name for which to find the city code.
Returns:
str: city code
'''
"""
for d in self.city_json.get("data", []):
if d["name"].startswith(city_name.lower()):
return d["id"]
Expand All @@ -82,28 +82,28 @@ def city_code(self, city_name: str) -> str:
@dataclass
class PropTransactions:

'''
"""
A class that retrieves property transaction data based on the specified parameters
such as prefecture code, city code, and date range from a given API endpoint.
'''
"""

pref_code: str
city_code: str
from_dt: int = 20222
to_dt: int = 20223
base_url: str = TRADESEARCH_EN_URL

def get_data(self) -> pd.DataFrame:
'''
Sends a GET request to the API with the specified parameters and retrieves
"""
Sends a GET request to the API with the specified parameters and retrieves
property transaction data.
The method constructs a DataFrame from the received JSON data and returns it.
Returns:
pd.DataFrame: A DataFrame containing the property transaction data.
'''
"""
params = {
"from": self.from_dt,
"to": self.to_dt,
Expand All @@ -114,49 +114,49 @@ def get_data(self) -> pd.DataFrame:
if r.status_code == 200:
try:
df = pd.DataFrame(r.json()["data"])
df['Period'] = df['Period'].map(self.custom_to_datetime)
df["Period"] = df["Period"].map(self.custom_to_datetime)
# for col in ['TradePrice']: # 'Area'は文字列のものが入っているので、対応を考えて追加する
# df[col] = df[col].astype(int)
return df
except KeyError:
print(f'''KeyError: The received JSON does not contain the expected "data" key.
print(
f"""KeyError: The received JSON does not contain the expected "data" key.
pref_code: {self.pref_code}, city_code: {self.city_code},
from_dt: {self.from_dt}, to_dt: {self.to_dt}
''')
"""
)
else:
raise Exception(f"Status_code: {r.status_code}")


def custom_to_datetime(self, p_str):
split_str = p_str.split(' ')
split_str = p_str.split(" ")
year = int(split_str[2])
q = split_str[0]
if q == '1st':
if q == "1st":
qt = self.dt_to_qt(year, 2)
return qt
elif q == '2nd':
elif q == "2nd":
qt = self.dt_to_qt(year, 5)
return qt
elif q == '3rd':
elif q == "3rd":
qt = self.dt_to_qt(year, 8)
return qt
elif q == '4th':
elif q == "4th":
qt = self.dt_to_qt(year, 11)
return qt
else:
raise ValueError(f'invalid quater: {p_str}')

raise ValueError(f"invalid quater: {p_str}")

def dt_to_qt(self, year, month):
dt = datetime(year, month, 1)
ts = pd.Timestamp(dt)
qt = ts.to_period('Q')
qt = ts.to_period("Q")
return qt


if __name__ == "__main__":
k = CityCode("Kyoto")
pref_code = k.pref_code
city_code = k.city_code('Kyoto')
city_code = k.city_code("Kyoto")
print(pref_code)
print(city_code)
print(city_code)
12 changes: 6 additions & 6 deletions j_realty_api/j_realty_jp.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ def pref_code(self) -> str:

@property
def city_json(self) -> dict[str]:
'''
"""
指定した都道府県の市町村コードのJSON
'''
"""
r = requests.get(self.base_path, params={"area": self.pref_code})
return r.json()

def city_code(self, city_name: str) -> str:
'''
"""
city_nameに対応したcitycodeが返される
'''
"""
for d in self.city_json.get("data", []):
if d["name"].startswith(city_name):
return d["id"]
Expand Down Expand Up @@ -86,13 +86,13 @@ class PropTransactions:
base_url: str = TRADESEARCH_URL

def get_data(self) -> pd.DataFrame:
'''
"""
指定された都道府県コード、市町村コード、期間の
不動産取引価格情報を取得する。
データはJSONをDataFrameにして返す。
Returns:
pd.DataFrame
'''
"""
params = {
"from": self.from_dt,
"to": self.to_dt,
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pandas
requests
requests
4 changes: 2 additions & 2 deletions samples/test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from j_realty_api import j_realty_en

d = j_realty_en.CityCode('Kyoto')
print(d.pref_code)
d = j_realty_en.CityCode("Kyoto")
print(d.pref_code)
14 changes: 5 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
from setuptools import setup, find_packages

from setuptools import find_packages, setup

setup(
name='j_realty_api',
name="j_realty_api",
version=0.1,
packages=find_packages(),
install_requires=[
'pandas',
'requests'
],
include_package_data=True
)
install_requires=["pandas", "requests"],
include_package_data=True,
)
13 changes: 4 additions & 9 deletions tests/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@

TRADESEARCH_EN_URL = "https://www.land.mlit.go.jp/webland_english/api/TradeListSearch"

pref_code: str = '26'
city_code: str = '26100'
pref_code: str = "26"
city_code: str = "26100"
from_dt: int = 20222
to_dt: int = 20223
base_url: str = TRADESEARCH_EN_URL


params = {
'from': from_dt,
'to': to_dt,
'area': pref_code,
'city': city_code
}
params = {"from": from_dt, "to": to_dt, "area": pref_code, "city": city_code}
r = requests.get(base_url, params)
if r.status_code:
print(r.json())
print(r.json())
6 changes: 3 additions & 3 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from j_realty_api import j_realty_en as je

cc = je.CityCode('kyoto')
cc = je.CityCode("kyoto")
pref_code = cc.pref_code
city_code = cc.city_code('kyoto')
city_code = cc.city_code("kyoto")
pt = je.PropTransactions(
pref_code,
city_code,
)

df = pt.get_data()
df.to_csv('./tests/data/test.csv')
df.to_csv("./tests/data/test.csv")
print(df.info())
Loading

0 comments on commit 922c5c6

Please sign in to comment.