HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

GPSD socket connection and decoding JSON into Python dictionaries

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
intojsongpsdpythondecodinganddictionariessocketconnection

Problem

GPS3 is a python 2.7-3.5 interface to GPSD.

I've stripped back everything to two classes.

```
#!/usr/bin/env python3
# coding=utf-8
"""
GPS3 (gps3.py) is a Python 2.7-3.5 GPSD interface (http://www.catb.org/gpsd)
Defaults host='127.0.0.1', port=2947, gpsd_protocol='json'

GPS3 has two classes.
1) 'GPSDSocket' to create a socket connection and retreive the output from GPSD.
2) 'Fix' unpacks the streamed gpsd data into python dictionaries.

These dictionaries are populated from the JSON data packet sent from the GPSD.

Import import gps3
Instantiate gps_connection = gps3.GPSDSocket()
gps_fix = gps3.Fix()
Use print('Altitude = 'gps_fix.TPV['alt'])
print('Latitude = 'gps_fix.TPV['lat'])

Consult Lines 150-ff for Attribute/Key possibilities.
or http://www.catb.org/gpsd/gpsd_json.html

Run human.py; python[X] human.py [arguments] for a human experience.
"""
from __future__ import print_function

import json
import select
import socket
import sys

__author__ = 'Moe'
__copyright__ = "Copyright 2015-2016 Moe"
__license__ = "MIT"
__version__ = "0.11a"

HOST = "127.0.0.1" # gpsd defaults
GPSD_PORT = 2947 # "
PROTOCOL = 'json' # "

class GPSDSocket(object):
"""Establish a socket with gpsd, by which to send commands and receive data.
"""

def __init__(self, host=HOST, port=GPSD_PORT, gpsd_protocol=PROTOCOL, devicepath=None):
self.devicepath_alternate = devicepath
self.response = None
self.protocol = gpsd_protocol
self.streamSock = None

if host:
self.connect(host, port)

def connect(self, host, port):
"""Connect to a host on a given port.
:param port:
:param host:
"""
for alotta_stuff in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
family, socktype, proto, _canonname, host_port = alotta_stuff
try:
self.streamSock = socket.socket(family, socktype, proto)

Solution

In general looks good and well documented.

  • In the close method the return statement is unnecessary.



  • Quotes are inconsistently used.



  • Most of the :param annotations in the docstrings are unused. If


you're not going to document them, just leave them out. The watch
method is also not using the syntax at all, where it would make a lot
of sense to use it.

  • The finally block in connect seems weird. If I'm not mistaken it


will be executed even if sys.exit is called (since that's
implemented using a SystemExit exception) - is that intentional?
I'd put a comment on it if so.

  • Also, is the watch method intended to be called from outside the


class? If not, then the default arguments are moot. Possibly also
prefix it to avoid calling it from outside the class.

  • In next the else block can be put inline as the if already


returns from the method. Again, the return in the except
handler is not necessary.

  • Also, return None is the same as return, but I imagine that's done


for clarity.

-
In the _emptydict creation, the parens around key aren't needed:

_emptydict = {key: 'n/a' for key in datalist}


If possible I'd use the same construction for SKY and DEVICES
btw.

  • The documentation for refresh is wrong, there's nothing returned


from that method (well None, but that doesn't count).

Code Snippets

_emptydict = {key: 'n/a' for key in datalist}

Context

StackExchange Code Review Q#120951, answer score: 3

Revisions (0)

No revisions yet.