Skip to content
This repository was archived by the owner on Aug 17, 2025. It is now read-only.

Conversation

@rarycoringa
Copy link
Owner

@rarycoringa rarycoringa commented Apr 23, 2021

This PR add a new unit conversion feature.

New models available

Now, two models of metrics are available:

  • Temperature
  • Distance

This models manage the default value requested with the OpenWeatherMap API and return for the user the value converted to the desired unit.

This management is done by the context attribute.

Usage the feature

GET http://localhost:5000/weather/{city_name} get a city weather

  • Parameters

    key description type required
    temperature Converts all temperatures to the desired unit. Are available for use: kelvin, celsius and fahrenheit. If not informed, the default value will be kelvin. string no
    distance Converts all distances to the desired unit. Are available for use: 'meters', 'kilometers' and 'miles'. If not informed, the default value will be meters. string no
  • Responses

    This is the result for http://localhost:5000/weather/london with temperature=fahrenheit and distance=miles:

    {
      "city": {
        "name": "London",
        "country": "GB",
        "coordinates": [51.5085, -0.1257]
      },
      "description": "Clouds",
      "long_description": "Broken Clouds",
      "temperature": {
        "value": 53.92,
        "unit": "fahrenheit"
      },
      "feels_like": {
        "value": 51.33,
        "unit": "fahrenheit"
      },
      "max_temperature": {
        "value": 56.26,
        "unit": "fahrenheit"
      },
      "min_temperature": {
        "value": 53.28,
        "unit": "fahrenheit"
      },
      "wind": {
        "speed": {
          "value": 10.8,
          "unit": "meters per second"
        },
        "degree": {
          "value": 270.0,
          "unit": "degrees"
        }
      },
      "visibility": {
        "value": 6.21,
        "unit": "miles"
      },
      "id": "LONDON"
    }

@rarycoringa rarycoringa added the feature New feature or request label Apr 23, 2021
@rarycoringa rarycoringa self-assigned this Apr 23, 2021
@rarycoringa rarycoringa linked an issue Apr 23, 2021 that may be closed by this pull request
@rarycoringa rarycoringa changed the base branch from develop to main April 28, 2021 12:26
@rarycoringa rarycoringa changed the base branch from main to develop April 28, 2021 12:26
@rarycoringa rarycoringa force-pushed the feature/units-params branch from 03e4efe to b055cc2 Compare April 28, 2021 12:38
@rarycoringa rarycoringa marked this pull request as ready for review May 4, 2021 12:23
@rarycoringa rarycoringa requested review from dluiscosta and removed request for dluiscosta May 4, 2021 12:25
unit = StringType(required=True)


class Temperature(Model):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this is quite an improvement from what we've had so far. Congrats.

temperature = self.context.get('temperature', 'kelvin')

if temperature.upper() == self.TemperatureMetrics.CELSIUS:
value = round(self.value - 273.0, 2) # Converts Kelvin temperature to Celsius
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of rounding to 2 dpp every time you could use a custom type inheriting from FloatType which specifies that centrally.

temperature = self.context.get('temperature', 'kelvin')

if temperature.upper() == self.TemperatureMetrics.CELSIUS:
value = round(self.value - 273.0, 2) # Converts Kelvin temperature to Celsius
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a side note, rounding floats is not very reliable. This reference is quite outdated but still relevant.

return unit.lower()


class Distance(Model):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe Distance and Temperature could inherit from an generic abstract "Number with unit" data type, leaving only the metrics, the default one and the transformation functions between each pair (or to and from default) to be defined in each of these.

That would add quite a bit of complexity though and would only be justifiable for production code if there were to exist more classes like these.

def __new__(mcs, name, bases, attrs):
attrs['choices'] = [v for k, v in attrs.items(
) if not k.startswith('_') and k.isupper()]
return TypeMeta.__new__(mcs, name, bases, attrs)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I've sent this to you, but do you understand what it does?

Copy link
Collaborator

@dluiscosta dluiscosta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I'm submitting this review, working on this repo should be de-prioritized for now,

'min_temperature': Temperature({
'value': float(response['main']['temp_min'])
}) if response['main']['temp_min'] != response['main']['temp'] else None,
'wind': Wind({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should follow the changes of the other fields as well.


@serializable(type=StringType, serialized_name='id')
def id(self):
return unidecode(f'{self.city.name.upper()}')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At PassFort's team we use UUID. Please, take a look.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parameterize temperature unit

3 participants