Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: python
python:
- "3.5"
- "3.6"
# command to install dependencies
install:
- pip install -r requirements.txt
# command to run tests
script:
- pytest
74 changes: 5 additions & 69 deletions autoria/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import requests
import json
from fnmatch import fnmatch
# from fnmatch import fnmatch
from typing import Any, Dict, List
from collections import namedtuple
from .utils import select_item, select_list


class RiaAPI:
Expand Down Expand Up @@ -314,9 +315,9 @@ def __init__(self, category: str, mark: str, model: str,
drives: list = None, color: str = None,
engine_volume: float = None,
seats: int = None, doors: int = None,
carrying: int = None, custom: bool = False,
damage: bool = False, under_credit: bool = False,
confiscated: bool = False, on_repair_parts: bool = False
carrying: int = None, custom: bool = 0,
damage: bool = 0, under_credit: bool = 0,
confiscated: bool = 0, on_repair_parts: bool = 0
) -> None:
"""Constructor.

Expand Down Expand Up @@ -447,69 +448,4 @@ def get_average(self) -> dict:
return self._api.average_price(self._params._asdict())


def select_item(item_to_select: str, items_list: list) -> int:
"""Select vehicle type, bodystyle, mark, model from the given list.

This function is intended to convert human-readable search
parameter, for instance, ''Винница'' into API-understandable
state identifier, for the given example it would be 1.

Args:
item_to_select - could be not 100% accurate as it is in the
auto.ria.ua lists, e.g. value ''Харьков'' is acceptable,
because the parameter is wild-carded in comprasion like
''*Харьков*'', the function will find suitable name
''Харьковская'' and will return its id.
items_list - JSON-formatted list of pairs ''name: value'',
obtained from one of the ''get_'' functions.

Returns:
needed item (category, bodystyle, mark etc.) identifyer.
"""
if item_to_select is not None and items_list is not None:
for item in items_list:
if fnmatch(item['name'], item_to_select):
return item['value']
for item in items_list:
if fnmatch(item['name'], '{}*'.format(item_to_select)):
return item['value']
for item in items_list:
if fnmatch(item['name'], '*{}*'.format(item_to_select)):
return item['value']


def select_list(list_to_select: list, items_list: list) -> list:
"""Select a list of ids in the list of dictionaries.

The function is intended to select a list of options inside
the list of dictionaries returned by any ''get_'' method of
RiaAPI.
For example, we have:
gears=['Ручная / Механика', 'Автомат']
as a search parameter, we need to:
1. Get a list of dictionaries (happens not in this function)
2. Define ids of provided options (happens right here)

Args:
list_to_select - a list of options to select, for instance,
['Ручная / Механика', 'Автомат']
items_list - list of dictionaries with option names and ids,
for example:
[
{
name: "Ручная / Механика",
value: 1
},
...
]

Returns:
The list of ids, ready-to-use in other methods, for example:
[1, 2]
"""
if list_to_select is not None:
selected_list = []
for item in list_to_select:
selected_item = select_item(item, items_list)
selected_list.append(selected_item)
return selected_list
70 changes: 70 additions & 0 deletions autoria/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from typing import Any, Dict, List
from fnmatch import fnmatch


def select_item(item_to_select: str, items_list: list) -> int:
"""Select vehicle type, bodystyle, mark, model from the given list.

This function is intended to convert human-readable search
parameter, for instance, ''Винница'' into API-understandable
state identifier, for the given example it would be 1.

Args:
item_to_select - could be not 100% accurate as it is in the
auto.ria.ua lists, e.g. value ''Харьков'' is acceptable,
because the parameter is wild-carded in comprasion like
''*Харьков*'', the function will find suitable name
''Харьковская'' and will return its id.
items_list - JSON-formatted list of pairs ''name: value'',
obtained from one of the ''get_'' functions.

Returns:
needed item (category, bodystyle, mark etc.) identifyer.
"""
if item_to_select is not None and items_list is not None:
for item in items_list:
if fnmatch(item['name'], item_to_select):
return item['value']
for item in items_list:
if fnmatch(item['name'], '{}*'.format(item_to_select)):
return item['value']
for item in items_list:
if fnmatch(item['name'], '*{}*'.format(item_to_select)):
return item['value']


def select_list(list_to_select: list, items_list: list) -> list:
"""Select a list of ids in the list of dictionaries.

The function is intended to select a list of options inside
the list of dictionaries returned by any ''get_'' method of
RiaAPI.
For example, we have:
gears=['Ручная / Механика', 'Автомат']
as a search parameter, we need to:
1. Get a list of dictionaries (happens not in this function)
2. Define ids of provided options (happens right here)

Args:
list_to_select - a list of options to select, for instance,
['Ручная / Механика', 'Автомат']
items_list - list of dictionaries with option names and ids,
for example:
[
{
name: "Ручная / Механика",
value: 1
},
...
]

Returns:
The list of ids, ready-to-use in other methods, for example:
[1, 2]
"""
if list_to_select is not None:
selected_list = []
for item in list_to_select:
selected_item = select_item(item, items_list)
selected_list.append(selected_item)
return selected_list
10 changes: 5 additions & 5 deletions average_calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@
# 'seats': 5,
# 'doors': 3,
# 'carrying': 1500,
'custom': False,
'damage': False,
'under_credit': False,
'confiscated': False,
'on_repair_parts': False
# 'custom': 0,
# 'damage': 0,
# 'under_credit': 0,
# 'confiscated': 0,
# 'on_repair_parts': 0
}

myCarAveragePrice = RiaAverageCarPrice(**search_params)
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
requests
typing # backported python type hints library
pytest
pytest-cov
pytest-greendots
coverage
flake8 # lint Python
Expand Down
24 changes: 13 additions & 11 deletions tests/autoria/test_select.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from autoria.api import select_item
from autoria.utils import select_item, select_list


class TestSelect:
"""Tests for ``select_item`` function."""

def test_select_exact(self):
def test_select_exact(self, select_data):
"""Item can be found by exact name match."""
data = [{
'name': 'one',
'value': 1,
}, {
'name': 'two',
'value': 2,
}]
assert select_item('one', data) == 1
assert select_item('two', data) == 2
assert select_item('one', select_data) == 1
assert select_item('two', select_data) == 2

def test_select_inexact(self, select_data):
"""Item can be found by inexact name match."""
assert select_item('on', select_data) == 1
assert select_item('tw', select_data) == 2

def test_select_list(self, list_to_select, items_list):
"""Select a list from provided items_list"""
assert select_list(list_to_select, items_list) == [1, 2]
31 changes: 31 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,34 @@ def ria_average():
12000],
'total': 8
}


@pytest.fixture()
def select_data():
return [{
'name': 'one',
'value': 1,
}, {
'name': 'two',
'value': 2,
}]


@pytest.fixture()
def list_to_select():
return ['Ручная / Механика', 'Автомат']


@pytest.fixture()
def items_list():
return [
{
'name': "Ручная / Механика",
'value': 1
},
{
'name': "Автомат",
'value': 2
}
]