<commit_before>import re
from lxml import etree
from collections import namedtuple

import requests

from . import BASE
from .utils import format_date, clean
from .exceptions import InvalidStopNumber, InvalidDirection

STOPNUM_RE = re.compile(r'\d{5}')


__all__ = [
    'determine_location',
    'Location',
    'parse_locations',
    'LocationT'
]

LocationT = namedtuple('LocationT', 'name,code')

is_location = lambda arg: isinstance(arg, Location)
are_locations = lambda *args: all(map(is_location, args))


def determine_location(from_d, to_d):
    """
    Takes two location objects, and returns a dict of lists of LocationTs
    mapping possible corresponding locations and their codes.

    See :func:`parse_locations` for precise output format.

    """
    assert are_locations(from_d, to_d)

    URL = BASE + 'DesktopModules/JourneyPlanner/JP.aspx'

    params = {
        'jpDate': format_date(),
        'jpDirection': 'B',
        # 'jpAMPM': 'AM',
        # 'jpHour': '11',
        # 'jpMinute': '00',
        'fSet': 'False',
        'fGadget': 'False',
        'mode': 't1,b1,f1,s1',
        'jpnMaxJourneys': '5',
        'jpMaxChanges': '-1',
        'jpWalkChange': 'NORMAL',
        'jpWheelchairOnly': '0'
    }

    params.update(from_d.as_('from'))
    params.update(to_d.as_('to'))

    return parse_locations(
        requests.get(URL, params=params).text
    )


def parse_locations(locations):
    """
    Takes the (pure) XML from the locations request and returns in the format;

    .. code-block:: python

        {
            "from": [
                LocationT('<NAME>', '<CODE>')
            ]
        }
    """
    root = etree.XML(locations)<commit_msg>Be consistent with others methods<commit_after>import re
from lxml import etree
from collections import namedtuple

import requests

from . import BASE
from .utils import format_date, clean
from .exceptions import InvalidStopNumber, InvalidDirection

STOPNUM_RE = re.compile(r'\d{5}')


__all__ = [
    'determine_location',
    'Location',
    'parse_locations',
    'LocationT'
]

LocationT = namedtuple('LocationT', 'name,code')

is_location = lambda arg: isinstance(arg, Location)
are_locations = lambda *args: all(map(is_location, args))


def determine_location(from_loco, to_loco):
    """
    Takes two location objects, and returns a dict of lists of LocationTs
    mapping possible corresponding locations and their codes.

    See :func:`parse_locations` for precise output format.

    """
    assert are_locations(from_loco, to_loco)

    URL = BASE + 'DesktopModules/JourneyPlanner/JP.aspx'

    params = {
        'jpDate': format_date(),
        'jpDirection': 'B',
        # 'jpAMPM': 'AM',
        # 'jpHour': '11',
        # 'jpMinute': '00',
        'fSet': 'False',
        'fGadget': 'False',
        'mode': 't1,b1,f1,s1',
        'jpnMaxJourneys': '5',
        'jpMaxChanges': '-1',
        'jpWalkChange': 'NORMAL',
        'jpWheelchairOnly': '0'
    }

    params.update(from_loco.as_('from'))
    params.update(to_loco.as_('to'))

    return parse_locations(
        requests.get(URL, params=params).text
    )


def parse_locations(locations):
    """
    Takes the (pure) XML from the locations request and returns in the format;

    .. code-block:: python

        {
            "from": [
                LocationT('<NAME>', '<CODE>')
            ]
        }
    """
    root = etree.XML(locations)
{'fraction_non_alphanumeric': 0.1309175920514319, 'fraction_numerical': 0.0070134424313267095, 'mean_word_length': 2.8160535117056855, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '734498', 'n_tokens_mistral': 1145, 'n_tokens_neox': 1081, 'n_words': 267}