Пример #1
0
def test_get_score(use_key_from_env, api_key_override, address,
                   longitude_latitude, return_transit_score, return_bike_score,
                   expected_status, error):
    if api_key_override:
        api_key = api_key_override
    elif use_key_from_env:
        api_key = DEFAULT_API_KEY
    else:
        api_key = None

    api = WalkScoreAPI(api_key=api_key)

    if not error:
        if longitude_latitude:
            result = api.get_score(latitude=longitude_latitude[1],
                                   longitude=longitude_latitude[0],
                                   address=address,
                                   return_transit_score=return_transit_score,
                                   return_bike_score=return_bike_score)

        else:
            result = api.get_score(address=address)
    else:
        with pytest.raises(error):
            if longitude_latitude:
                result = api.get_score(
                    latitude=longitude_latitude[1],
                    longitude=longitude_latitude[0],
                    address=address,
                    return_transit_score=return_transit_score,
                    return_bike_score=return_bike_score)
            else:
                result = api.get_score(address=address)

    if not error:
        assert result is not None
        assert isinstance(result, LocationScore) is True
        assert result.status == expected_status
        assert result.walk_score is not None
        assert checkers.is_numeric(result.walk_score) is True

        if return_transit_score:
            assert result.transit_score is not None
            assert checkers.is_numeric(result.transit_score) is True
        else:
            assert result.transit_score is None
        if return_bike_score:
            assert result.bike_score is not None
            assert checkers.is_numeric(result.bike_score) is True
        else:
            assert result.bike_score is None
Пример #2
0
 def ground_temperatures(self, value):
     if checkers.is_numeric(value):
         ground_temperatures = [value] * 12
     elif checkers.is_iterable(value):
         ground_temperature = validators.iterable(value,
                                                  minimum_length=12,
                                                  maximum_length=12)
         ground_temperatures = [temp for temp in ground_temperature]
     else:
         raise ValueError(
             "Input error for value 'ground_temperature'. Value must "
             "be numeric or an iterable of length 12.")
     self._ground_temperatures = ground_temperatures
Пример #3
0
    def missing_value_metadata(self, value):
        if not value:
            self._missing_value_metadata = None
            return
        elif checkers.is_string(value):
            value = [value]
        elif checkers.is_numeric(value):
            value = [value]

        validated_values = []
        for item in value:
            try:
                validated_item = validators.string(item, allow_empty = False)
            except (ValueError, TypeError):
                validated_item = validators.int(item, allow_empty = False)

            validated_values.append(validated_item)

        self._missing_value_metadata = validated_values
def from_datetime(value):
    if checkers.is_numeric(value):
        return from_timedelta(value)

    return validators.datetime(value, allow_empty=True)
def get_type_mapping(value,
                     type_mapping=None,
                     skip_nested=True,
                     default_to_str=False):
    """Retrieve the SQL type mapping for ``value``.

    :param value: The value whose SQL type will be returned.

    :param type_mapping: Determines how the value type of ``value`` map to
      SQL column data types. To add a new mapping or override a default, set a
      key to the name of the value type in Python, and set the value to a
      :doc:`SQLAlchemy Data Type <sqlalchemy:core/types>`. The following are the
      default mappings applied:

      .. list-table::
         :widths: 30 30
         :header-rows: 1

         * - Python Literal
           - SQL Column Type
         * - ``bool``
           - :class:`Boolean <sqlalchemy:sqlalchemy.types.Boolean>`
         * - ``str``
           - :class:`Text <sqlalchemy:sqlalchemy.types.Text>`
         * - ``int``
           - :class:`Integer <sqlalchemy:sqlalchemy.types.Integer>`
         * - ``float``
           - :class:`Float <sqlalchemy:sqlalchemy.types.Float>`
         * - ``date``
           - :class:`Date <sqlalchemy:sqlalchemy.types.Date>`
         * - ``datetime``
           - :class:`DateTime <sqlalchemy:sqlalchemy.types.DateTime>`
         * - ``time``
           - :class:`Time <sqlalchemy:sqlalchemy.types.Time>`
         * - ``timedelta``
           - :class:`Interval <sqlalchemy:sqlalchemy.types.Interval>`

    :type type_mapping: :class:`dict <python:dict>` with type names as keys and
      column data types as values / :obj:`None <python:None>`

    :param skip_nested: If ``True`` then if ``value`` is a nested item (e.g.
      iterable, :class:`dict <python:dict>` objects, etc.) it will return
      :obj:`None <python:None>`. If ``False``, will treat nested items as
      :class:`str <python:str>`. Defaults to ``True``.
    :type skip_nested: :class:`bool <python:bool>`

    :param default_to_str: If ``True``, will automatically set a ``value`` whose
      value type cannot be determined to ``str``
      (:class:`Text <sqlalchemy:sqlalchemy.types.Text>`). If ``False``, will
      use the value type's ``__name__`` attribute and attempt to find a mapping.
      Defaults to ``False``.
    :type default_to_str: :class:`bool <python:bool>`

    :returns: The :doc:`SQLAlchemy Data Type <sqlalchemy:core/types>` for ``value``, or
      :obj:`None <python:None>` if the value should be skipped
    :rtype: :doc:`SQLAlchemy Data Type <sqlalchemy:core/types>` / :obj:`None`

    :raises UnsupportedValueTypeError: when ``value`` does not have corresponding
      :doc:`SQLAlchemy Data Type <sqlalchemy:core/types>`

    """
    if not type_mapping:
        type_mapping = DEFAULT_PYTHON_SQL_TYPE_MAPPING

    for key in DEFAULT_PYTHON_SQL_TYPE_MAPPING:
        if key not in type_mapping:
            type_mapping[key] = DEFAULT_PYTHON_SQL_TYPE_MAPPING[key]

    if checkers.is_callable(value):
        raise UnsupportedValueTypeError('value ("%s") cannot be callable' %
                                        value)
    elif checkers.is_iterable(value) and skip_nested:
        return None
    elif checkers.is_iterable(value) and default_to_str:
        target_type = 'str'
    elif value is None and default_to_str:
        target_type = 'str'
    elif isinstance(value, bool):
        target_type = 'bool'
    elif checkers.is_numeric(value):
        if checkers.is_integer(value):
            target_type = 'int'
        else:
            target_type = 'float'
    elif checkers.is_time(value) and not checkers.is_datetime(value):
        target_type = 'time'
    elif checkers.is_datetime(value):
        target_type = 'datetime'
    elif checkers.is_date(value):
        target_type = 'date'
    elif default_to_str:
        target_type = 'str'
    else:
        target_type = type(value).__name__

    column_type = type_mapping.get(target_type, None)
    if not column_type:
        raise UnsupportedValueTypeError(
            'value ("%s") is not a supported type (%s)' % (value, target_type))

    return column_type
Пример #6
0
    def _get_attribute_csv_data(self,
                                attributes,
                                is_dumping=False,
                                delimiter='|',
                                wrap_all_strings=False,
                                null_text='None',
                                wrapper_character="'",
                                double_wrapper_character_when_nested=False,
                                escape_character="\\",
                                line_terminator='\r\n',
                                config_set=None):
        r"""Return the CSV representation of ``attributes`` extracted from the
        model instance (record).

        :param attributes: Names of :term:`model attributes <model attribute>` to
          include in the CSV output.
        :type attributes: :class:`list <python:list>` of :class:`str <python:str>`

        :param is_dumping: If ``True``, then allow
          :exc:`UnsupportedSerializationError <sqlathanor.errors.UnsupportedSerializationError>`.
          Defaults to ``False``.
        :type is_dumping: :class:`bool <python:bool>`

        :param delimiter: The delimiter used between columns. Defaults to ``|``.
        :type delimiter: :class:`str <python:str>`

        :param wrap_all_strings: If ``True``, wraps any string data in the
          ``wrapper_character``. If ``None``, only wraps string data if it contains
          the ``delimiter``. Defaults to ``False``.
        :type wrap_all_strings: :class:`bool <python:bool>`

        :param null_text: The text value to use in place of empty values. Only
          applies if ``wrap_empty_values`` is ``True``. Defaults to ``'None'``.
        :type null_text: :class:`str <python:str>`

        :param wrapper_character: The string used to wrap string values when
          wrapping is necessary. Defaults to ``'``.
        :type wrapper_character: :class:`str <python:str>`

        :param double_wrapper_character_when_nested: If ``True``, will double the
          ``wrapper_character`` when it is found inside a column value. If ``False``,
          will precede the ``wrapper_character`` by the ``escape_character`` when
          it is found inside a column value. Defaults to ``False``.
        :type double_wrapper_character_when_nested: :class:`bool <python:bool>`

        :param escape_character: The character to use when escaping nested wrapper
          characters. Defaults to ``\``.
        :type escape_character: :class:`str <python:str>`

        :param line_terminator: The character used to mark the end of a line.
          Defaults to ``\r\n``.
        :type line_terminator: :class:`str <python:str>`

        :param config_set: If not :obj:`None <python:None>`, the named configuration set
          to use. Defaults to :obj:`None <python:None>`.
        :type config_set: :class:`str <python:str>` / :obj:`None <python:None>`

        :returns: Data from the object in CSV format ending in ``line_terminator``.
        :rtype: :class:`str <python:str>`
        """
        if not wrapper_character:
            wrapper_character = '\''

        if not attributes:
            raise SerializableAttributeError("attributes cannot be empty")

        if wrap_all_strings:
            quoting = csv.QUOTE_NONNUMERIC
        else:
            quoting = csv.QUOTE_MINIMAL

        if 'sqlathanor' in csv.list_dialects():
            csv.unregister_dialect('sqlathanor')

        csv.register_dialect('sqlathanor',
                             delimiter=delimiter,
                             doublequote=double_wrapper_character_when_nested,
                             escapechar=escape_character,
                             quotechar=wrapper_character,
                             quoting=quoting,
                             lineterminator=line_terminator)

        data = []
        for item in attributes:
            try:
                value = self._get_serialized_value(format='csv',
                                                   attribute=item,
                                                   config_set=config_set)
            except UnsupportedSerializationError as error:
                if is_dumping:
                    value = getattr(self, item)
                else:
                    raise error

            data.append(value)

        for index, item in enumerate(data):
            if item == '' or item is None or item == 'None':
                data[index] = null_text
            elif not checkers.is_string(item) and not checkers.is_numeric(
                    item):
                data[index] = str(item)

        data_dict = dict_()
        for index, column_name in enumerate(attributes):
            data_dict[column_name] = data[index]

        output = StringIO()
        csv_writer = csv.DictWriter(output,
                                    fieldnames=attributes,
                                    dialect='sqlathanor')

        csv_writer.writerow(data_dict)

        data_row = output.getvalue()
        output.close()

        csv.unregister_dialect('sqlathanor')

        return data_row
 def validate_number(number) -> bool:
     return checkers.is_numeric(number)