Subpackages

Submodules

speck.cache module

Cache utility. The primary purpose of this is to reduce the number of required API calls.

class speck.cache.BufferedCacheManager(path=None)

Bases: speck.cache.CacheManager

Buffered Cache Manager implementation. Stored cache in memory.

cleanup(name)

Cleanup all cache with a name. Supports wildcard (*) deletion.

debug_size()

Get size of internal cache for debugging.

Returns

Cache size in memory in bytes.

dump(name, data)

Save data into cache.

find_all()

Find all cache stored in memory.

Return type

generator

read(name)

Reads cache with name if it exists.

class speck.cache.CacheManager(path)

Bases: object

Abstract class representing a cache manager.

cleanup(name)

Cleanup all cache with a name

debug_size()

Get size of cache for debugging.

dump(name, data)

Save data into cache.

find_all()

Find all stored cache.

read(name)

Reads cache with name if it exists.

class speck.cache.FileCacheManager(path)

Bases: speck.cache.CacheManager

File based Cache Manager implementation. Keeps track of and gets/updates data from cache files.

Parameters

path – Path to the “cache directory”. Cache files will be stored here.

cleanup(name)

Cleans up all cache files with a given name. Supports wildcard (*) deletion.

dump(name, data)

Writes data to a cache file with name. name must be kept track of manually.

find_all()

Return a list of all tracked cache files.

Return type

generator

property path

Returns the directory to which cache files are being stored.

Return type

str

read(name)

Tries to read cache with name. Returns None if no such file is found.

speck.client module

WeatherAPI client implementation. Use this to make requests to weatherapi.com.

class speck.client.Client(token, use_cache=False, cache_file=False, cache_path='.cache')

Bases: object

Represents a connection to weatherapi.com. Use this class to interact with the weatherapi API.

Variables

session – A requests.Session object. Requests are made with this (session.get).

BASE = 'https://api.weatherapi.com/v1'
astro(*args, **kwargs)

Alias for astronomy.

astronomy(loc)

Get astronomy information for a location.

Parameters

loc – See docs on method current.

Return type

types.AstroPoint

current(loc)

Get current weather conditions in a location.

Parameters

loc

Query location to find data for. It could be following:

  • Latitude and Longitude (Decimal degree). e.g:’48.8567,2.3508’,

  • city name e.g: ‘Paris’,

  • US zip e.g: ‘10001’,

  • UK postcode e.g: ‘SW1’,

  • Canada postal code e.g: ‘G2J’,

  • metar:<metar code> e.g: ‘metar:EGLL’,

  • iata:<3 digit airport code> e.g: ‘iata:DXB’,

  • auto:ip IP lookup e.g: ‘auto:ip’,

  • IP address (IPv4 and IPv6 supported) e.g: ‘100.0.0.1

Return type

types.HourlyPoint

find_city(loc)

Try to find a city with a match from a known list of locations. This method is highly unlikely to be used frequently, since weatherAPI tries to interpret location names even if incorrect, but is provided anyway.

forecast(loc, days=3)

Get weather forecast for a location.

Parameters
  • loc – See docs on method current.

  • days – Number of days to restrict the forecast for. WeatherAPI allows up to 10 days, but it in practice the maximum is 3.

Returns

A tuple with the current weather, and a list of forecasted days.

Return type

(types.HourlyPoint, list[types.DailyPoint])

history(loc, days)

Get weather history for a location. This hasn’t been tested since it requires a paid weatherapi.com plan, but should work.

Parameters
  • loc – See docs on method current.

  • dt – Datetime string in the format YYY-MM-DD. Data starting from this date will be returned.

Returns

A tuple with the current weather, and a list of historical weather per day.

Return type

(types.HourlyPoint, list[types.DailyPoint])

ip(*args, **kwargs)

Alias for ip_lookup.

ip_lookup(ip)

Get information for an IP address.

Parameters

ip – IPv6 or IPv4 string.

Return type

IpPoint

search(loc)

Get a list of location objects based on query parameter.

Parameters

loc – See docs on method current.

Return type

list[types.Location]

sports(*args, **kwargs)

Alias for sports_lookup.

sports_lookup(loc)

Get listing of all upcoming sports events for football, cricket and golf. From the behaviour of the WeatherAPI Sports API, parameter loc doesn’t actually matter but is required anyway.

Parameters

loc – See docs on method current.

Return type

dict{str: list[SportsPoint]}

timezone_info(loc)

Get timezone and associated information for a location.

Parameters

loc – See docs on method current.

Return type

types.Location

tz(*args, **kwargs)

Alias for timezone_info.

speck.errors module

All exceptions that can be raised during handling of a weatherapi request.

exception speck.errors.ApiKeyDisabled(message, internal_code)

Bases: speck.errors.WeatherApiError

Raised when weatherapi key used for a request has been disabled.

exception speck.errors.InternalError(message, internal_code)

Bases: speck.errors.WeatherApiError

Raised when an internal weatherapi error is encountered.

exception speck.errors.InvalidApiKey(message, internal_code)

Bases: speck.errors.WeatherApiError

Raised when an invalid weatherapi key has been used for a request.

exception speck.errors.InvalidLocation(message, internal_code)

Bases: speck.errors.WeatherApiError

Raised when location request is not found.

exception speck.errors.InvalidRequestUrl(message, internal_code)

Bases: speck.errors.WeatherApiError

Raised when weatherapi request url is invalid.

exception speck.errors.NoApiKey(message, internal_code)

Bases: speck.errors.WeatherApiError

Raised when no weatherapi key has been provided in a request.

exception speck.errors.QueryNotProvided(message, internal_code)

Bases: speck.errors.WeatherApiError

Raised when a query parameter has not been provided for a request.

exception speck.errors.QuotaExceeded(message, internal_code)

Bases: speck.errors.WeatherApiError

Raised when monthly requests limit has been reached.

exception speck.errors.WeatherApiError(message, internal_code)

Bases: Exception

Raised when an unknown weatherapi error is encountered.

Variables
  • internal_codeint The error code returned by weatherapi.

  • messagestr A more detailed description of the error.

Module contents

Minimal weatherapi API wrapper.