original_docstring: API route for latest rates.
docstring: API route for latest rates.
docstring_tokens: API route for latest rates .
code: async def latest(base: Optional[str] = None,
                   symbols: Optional[str] = None):
      """API route for latest rates."""
  
      if not base:
          base = "EUR"
      latest_date = session.query(func.max(Currency.date)).first()
      latest_date = latest_date[0].strftime("%Y-%m-%d")
      base_rate = (
          session.query(Currency.rate, func.max(Currency.date))
          .filter(func.date(Currency.date) == latest_date, Currency.currency == base)
          .first()
      )
      if base_rate[0] is None:
          base = "EUR"
          base_rate = (
              session.query(Currency.rate, func.max(Currency.date))
              .filter(func.date(Currency.date) == latest_date,
             Currency.currency == base)
              .first()
          )
      currencies = (
          session.query(Currency.currency, Currency.rate)
          .filter(func.date(Currency.date) == latest_date)
          .all()
      )
      rates = {}
      for k, v in sorted(currencies):
          if k == base:
              pass
          elif base == "EUR":
              if symbols:
                  cur_symbols = symbols.split(",")
                  if k in cur_symbols:
                      rates.update({k: v})
              else:
                  rates.update({k: v})
          else:
              new_rate = float(v) / float(base_rate[0])
              new_rate = round(new_rate, 5)
              if symbols:
                  cur_symbols = symbols.split(",")
                  if k in cur_symbols:
                      rates.update({k: new_rate})
              else:
                  rates.update({k: new_rate})
      if base != "EUR":
          eur_rate = float(1) / float(base_rate[0])
          rates.update({"EUR": round(eur_rate, 5)})
          rates = OrderedDict(sorted(rates.items(), key=lambda t: t[0]))
      return {"base": base, "date": latest_date, "rates": rates}
code_tokens: async def latest ( base : Optional [ str ] = None , symbols : Optional [ str ] = None ) : if not base : base = "EUR" latest_date = session . query ( func . max ( Currency . date ) ) . first ( ) latest_date = latest_date [ 0 ] . strftime ( "%Y-%m-%d" ) base_rate = ( session . query ( Currency . rate , func . max ( Currency . date ) ) . filter ( func . date ( Currency . date ) == latest_date , Currency . currency == base ) . first ( ) ) if base_rate [ 0 ] is None : base = "EUR" base_rate = ( session . query ( Currency . rate , func . max ( Currency . date ) ) . filter ( func . date ( Currency . date ) == latest_date , Currency . currency == base ) . first ( ) ) currencies = ( session . query ( Currency . currency , Currency . rate ) . filter ( func . date ( Currency . date ) == latest_date ) . all ( ) ) rates = { } for k , v in sorted ( currencies ) : if k == base : pass elif base == "EUR" : if symbols : cur_symbols = symbols . split ( "," ) if k in cur_symbols : rates . update ( { k : v } ) else : rates . update ( { k : v } ) else : new_rate = float ( v ) / float ( base_rate [ 0 ] ) new_rate = round ( new_rate , 5 ) if symbols : cur_symbols = symbols . split ( "," ) if k in cur_symbols : rates . update ( { k : new_rate } ) else : rates . update ( { k : new_rate } ) if base != "EUR" : eur_rate = float ( 1 ) / float ( base_rate [ 0 ] ) rates . update ( { "EUR" : round ( eur_rate , 5 ) } ) rates = OrderedDict ( sorted ( rates . items ( ) , key = lambda t : t [ 0 ] ) ) return { "base" : base , "date" : latest_date , "rates" : rates }
short_docstring: API route for latest rates.
short_docstring_tokens: API route for latest rates .
comment: """API route for latest rates."""