LookupLib

class pyhamtools.lookuplib.LookupLib(lookuptype='countryfile', apikey=None, filename=None, logger=None, redis_instance=None, redis_prefix=None)

This class is a wrapper for the following three Amateur Radio databases:

  1. Clublog.org (daily updated XML File)
  2. Clublog.org (HTTPS lookup)
  3. Country-files.com (infrequently updated PLIST File)

It’s aim is to provide a homogeneous interface to different databases.

Typically an instance of this class is injected as a dependency in the Callinfo class, but it can also be used directly.

Even the interface is the same for all lookup sources, the returning data can be different. The documentation of the various methods provide more detail.

By default, LookupLib requires an Internet connection to download the libraries or perform the lookup against the Clublog API.

The entire lookup data can also be copied into Redis, which an extremely fast in-memory Key/Value store. A LookupLib object can be instanciated to perform then all lookups in Redis, instead processing and loading the data from Internet / File. This saves some time and allows several instances of LookupLib to query the same data concurrently.

Parameters:
  • lookuptype (str) – “clublogxml” or “clublogapi” or “countryfile” or “redis”
  • apikey (str) – Clublog API Key
  • filename (str, optional) – Filename for Clublog XML or Country-files.com cty.plist file. When a local file is
  • logger (logging.getLogger(__name__), optional) – Python logger
  • redis_instance (redis.Redis(), optional) – Instance of Redis
  • redis_prefix (str, optional) – Prefix to identify the lookup data set in Redis
copy_data_in_redis(redis_prefix, redis_instance)

Copy the complete lookup data into redis. Old data will be overwritten.

Parameters:
  • redis_prefix (str) – Prefix to distinguish the data in redis for the different looktypes
  • redis_instance (str) – an Instance of Redis
Returns:

returns True when the data has been copied successfully into Redis

Return type:

bool

Example

Copy the entire lookup data from the Country-files.com PLIST File into Redis. This example requires a running instance of Redis, as well the python Redis connector (pip install redis-py).

>>> from pyhamtools import LookupLib
>>> import redis
>>> r = redis.Redis()
>>> my_lookuplib = LookupLib(lookuptype="countryfile")
>>> print my_lookuplib.copy_data_in_redis(redis_prefix="CF", redis_instance=r)
True

Now let’s create an instance of LookupLib, using Redis to query the data

>>> from pyhamtools import LookupLib
>>> import redis
>>> r = redis.Redis()
>>> my_lookuplib = LookupLib(lookuptype="countryfile", redis_instance=r, redis_prefix="CF")
>>> my_lookuplib.lookup_callsign("3D2RI")
{
  u'adif': 460,
  u'continent': 'OC',
  u'country': 'Rotuma Island',
  u'cqz': 32,
  u'ituz': 56,
  u'latitude': -12.48,
  u'longitude': -177.08
}

Note

This method is available for the following lookup type

  • clublogxml
  • countryfile
is_invalid_operation(callsign, timestamp=datetime.datetime(2014, 9, 26, 23, 10, 28, 994044, tzinfo=<UTC>))

Returns True if an operations is known as invalid

Parameters:
  • callsign (string) – Amateur Radio callsign
  • timestamp (datetime, optional) – datetime in UTC (tzinfo=pytz.UTC)
Returns:

True if a record exists for this callsign (at the given time)

Return type:

bool

Raises:
  • KeyError – No matching callsign found
  • APIKeyMissingError – API Key for Clublog missing or incorrect

Example

The following code checks the Clublog XML database if the operation is valid for two dates.

>>> from pyhamtools import LookupLib
>>> from datetime import datetime
>>> import pytz
>>> my_lookuplib = LookupLib(lookuptype="clublogxml", apikey="myapikey")
>>> print my_lookuplib.is_invalid_operation("5W1CFN")
True
>>> try:
>>>   timestamp = datetime(year=2012, month=1, day=31).replace(tzinfo=pytz.UTC)
>>>   my_lookuplib.is_invalid_operation("5W1CFN", timestamp)
>>> except KeyError:
>>>   print "Seems to be invalid operation before 31.1.2012"
Seems to be an invalid operation before 31.1.2012

Note

This method is available for

  • clublogxml
  • redis
lookup_callsign(callsign=None, timestamp=datetime.datetime(2014, 9, 26, 23, 10, 28, 994018, tzinfo=<UTC>))

Returns lookup data if an exception exists for a callsign

Parameters:
  • callsign (string) – Amateur radio callsign
  • timestamp (datetime, optional) – datetime in UTC (tzinfo=pytz.UTC)
Returns:

Dictionary containing the country specific data of the callsign

Return type:

dict

Raises:
  • KeyError – No matching callsign found
  • APIKeyMissingError – API Key for Clublog missing or incorrect

Example

The following code queries the the online Clublog API for the callsign “VK9XO” on a specific date.

>>> from pyhamtools import LookupLib
>>> from datetime import datetime
>>> import pytz
>>> my_lookuplib = LookupLib(lookuptype="clublogapi", apikey="myapikey")
>>> timestamp = datetime(year=1962, month=7, day=7, tzinfo=pytz.UTC)
>>> print my_lookuplib.lookup_callsign("VK9XO", timestamp)
{
 'country': 'CHRISTMAS ISLAND',
 'longitude': -105.7,
 'cqz': 29,
 'adif': 35,
 'latitude': -10.5,
 'continent': 'OC'
}

Note

This method is available for

  • clublogxml
  • clublogapi
  • countryfile
  • redis
lookup_entity(entity=None)

Returns lookup data of an ADIF Entity

Parameters:entity (int) – ADIF identifier of country
Returns:Dictionary containing the country specific data
Return type:dict
Raises:KeyError – No matching entity found

Example

The following code queries the the Clublog XML database for the ADIF entity Turkmenistan, which has the id 273.

>>> from pyhamtools import LookupLib
>>> my_lookuplib = LookupLib(lookuptype="clublogapi", apikey="myapikey")
>>> print my_lookuplib.lookup_entity(273)
{
 'deleted': False,
 'country': 'TURKMENISTAN',
 'longitude': -58.4,
 'cqz': 17,
 'prefix': 'EZ',
 'latitude': 38.0,
 'continent': 'AS'
}

Note

This method is available for the following lookup type

  • clublogxml
  • redis
lookup_prefix(prefix, timestamp=datetime.datetime(2014, 9, 26, 23, 10, 28, 994018, tzinfo=<UTC>))

Returns lookup data of a Prefix

Parameters:
  • prefix (string) – Prefix of a Amateur Radio callsign
  • timestamp (datetime, optional) – datetime in UTC (tzinfo=pytz.UTC)
Returns:

Dictionary containing the country specific data of the Prefix

Return type:

dict

Raises:
  • KeyError – No matching Prefix found
  • APIKeyMissingError – API Key for Clublog missing or incorrect

Example

The following code shows how to obtain the information for the prefix “DH” from the countryfile.com database (default database).

>>> from pyhamtools import LookupLib
>>> myLookupLib = LookupLib()
>>> print myLookupLib.lookup_prefix("DH")
{
 'adif': 230,
 'country': 'Fed. Rep. of Germany',
 'longitude': -10.0,
 'cqz': 14,
 'ituz': 28,
 'latitude': 51.0,
 'continent': 'EU'
}

Note

This method is available for

  • clublogxml
  • countryfile
  • redis
lookup_zone_exception(callsign, timestamp=datetime.datetime(2014, 9, 26, 23, 10, 28, 994050, tzinfo=<UTC>))

Returns a CQ Zone if an exception exists for the given callsign

Args: callsign (string): Amateur radio callsign timestamp (datetime, optional): datetime in UTC (tzinfo=pytz.UTC)

Returns:

Value of the the CQ Zone exception which exists for this callsign (at the given time)

Return type:

int

Raises:
  • KeyError – No matching callsign found
  • APIKeyMissingError – API Key for Clublog missing or incorrect

Example

The following code checks the Clublog XML database if a CQ Zone exception exists for the callsign DP0GVN.

>>> from pyhamtools import LookupLib
>>> my_lookuplib = LookupLib(lookuptype="clublogxml", apikey="myapikey")
>>> print my_lookuplib.lookup_zone_exception("DP0GVN")
38

The prefix “DP” It is assigned to Germany, but the station is located in Antarctica, and therefore in CQ Zone 38

Note

This method is available for

  • clublogxml
  • redis

This Page