def test_enforces_units(self): # only wh or kwh supported with self.assertRaises(ValueError): Reading(self.sometime, self.later, "mwh", 1) # incorrect capitalization is okay Reading(self.sometime, self.later, "Wh", 1)
def test_start_time_must_be_before_end_time(self): # start and end time the same with self.assertRaises(ValueError): Reading(self.sometime, self.sometime, "wh", 1) # start time after end time with self.assertRaises(ValueError): Reading(self.later, self.sometime, "wh", 1)
def read_text_list(self, reading: Reading) -> list: db_params = reading.get_db_params() formatted_query = self.QUERY.format(**db_params) self._cursor.execute(formatted_query) results = [] for row in self._cursor: curr_result = self._parse_row(row) if reading.is_target(curr_result["chapter"], curr_result["verse"]): results.append(curr_result) return results
def parse(self): try: #http://www.marcus-povey.co.uk - USED REGEX REGEX! uidRegex = re.compile('<id>([0-9]+)</id>') valueRegex = re.compile('<watts>([0-9]+)</watts>') timeRegex = re.compile('<time>([0-9\.\:]+)</time>') value = str(int(valueRegex.findall(self._data)[0])) time = timeRegex.findall(self._data)[0] self.uid = uidRegex.findall(self._data)[0] self.logger.info('Parsed data sucessfully!') self._r = Reading(self, value, time, self.logger) except Exception: self.logger.error('Could not get details from device')
def run(self): run = True while run: self.draw() for event in pygame.event.get(): if event.type == pygame.QUIT: run = False keys = pygame.key.get_pressed() if keys[pygame.K_t]: tiping = Tiping(self.win) tiping.run() del tiping elif keys[pygame.K_r]: reading = Reading(self.win) reading.run() del reading pygame.quit()
def _reading_generator(self, samples, start, stop): """Yields one reading at a time. Samples are grouped by resource id(already sorted by resource id) and then used to create a reading object. :param samples: List of samples sorted by resource_id and timestamp. :type samples: List :param start: Reading start time :type start: Datetime :param stop: Reading stop time :type stop: Datetime :yields: Reading objects """ # Yield a reading for each resource/meter pair for _, g in itertools.groupby(samples, lambda x: x.resource_id): try: yield Reading(list(g), start, stop) except NoSamplesError: continue
class Representation(object): R = Reading() reading_cleaned = R.cleaned_and_tokenized() reading = R.get_readable() def __init__(self): self.df_songs = pd.read_csv('../CSV/prep_songs_pl_lem_stop_wcrft2.csv', header=0, index_col=None) def bigram_count(self): bigram_vect = CountVectorizer(ngram_range=(2, 2), binary=False) bigram_sparse = bigram_vect.fit_transform(self.df_songs.iloc[:, -1]) df_bigram = pd.DataFrame(bigram_sparse.toarray()) df_bigram.columns = bigram_vect.get_feature_names() bigram_sparse2 = bigram_vect.transform(self.reading_cleaned) df2_bigram = pd.DataFrame(bigram_sparse2.toarray()) df2_bigram.columns = bigram_vect.get_feature_names() return df_bigram, df2_bigram
def __init__(self, latitude=None, longitude=None, temperature=None, precipitation=None, humidity=None, wind=None): """""" if temperature is None: self.temperature = Temperature() else: # TODO type check on input self.temperature = temperature # Feels like, unit self.humidity = None self.summary = None self.icon = None self.date_time = None self.latitude = latitude # TODO input validation self.longitude = longitude # TODO input validation self.altitude = None if precipitation is None: self.precipitation = Precipitation() else: # TODO type check on input self.precipitation = precipitation # TODO type check on input self.humidity = Reading(humidity, "%") if wind is None: self.wind = Wind() else: # TODO type check on input self.wind = wind self.cloud_cover = None self.moon_phase = None self.uv_index = None self.is_daytime = None self.alerts = [] self.unix = None
def add_reading(self, line: str): if Reading.is_reading(line): new_reading = Reading(line) self.readings.append(new_reading)
def scrape(self): page = requests.get( 'https://www.ndbc.noaa.gov/station_page.php?station=' + self.stationID) self.tree = html.fromstring(page.content) if not self.isValidStation(): return # setup the dictionaries buoy = {} reading = {} buoy['station_id'] = self.stationID reading['station_id'] = self.stationID # Main box variables reading['wind_direction'] = self.grabDirectionTupleFromString( self.grabFromTree('"Wind Direction (WDIR):"')) reading['wind_speed'] = self.grabNumberFromString( self.grabFromTree('"Wind Speed (WSPD):"')) reading['wind_gust'] = self.grabNumberFromString( self.grabFromTree('"Wind Gust (GST):"')) reading['wave_height'] = self.grabNumberFromString( self.grabFromTree('"Wave Height (WVHT):"') ) # this also seems to track 'Significant Wave Height' reading['dominant_period'] = self.grabNumberFromString( self.grabFromTree('"Dominant Wave Period (DPD):"')) reading['average_period'] = self.grabNumberFromString( self.grabFromTree('"Average Period (APD):"')) reading['wave_direction'] = self.grabDirectionTupleFromString( self.grabFromTree('"Mean Wave Direction (MWD):"')) reading['air_temperature'] = self.grabNumberFromString( self.grabFromTree('"Air Temperature (ATMP):"')) # Secondary box variables reading['significant_wave_height'] = self.grabNumberFromString( self.grabFromTree('"Significant Wave Height (WVHT):"')) reading['swell_height'] = self.grabNumberFromString( self.grabFromTree('"Swell Height (SwH):"')) reading['swell_period'] = self.grabNumberFromString( self.grabFromTree('"Swell Period (SwP):"')) reading['swell_direction'] = self.constructDirectionTupleFromString( self.grabFromTree('"Swell Direction (SwD):"')) reading['wind_wave_height'] = self.grabNumberFromString( self.grabFromTree('"Wind Wave Height (WWH):"')) reading['wind_wave_period'] = self.grabNumberFromString( self.grabFromTree('"Wind Wave Period (WWP):"')) reading[ 'wind_wave_direction'] = self.constructDirectionTupleFromString( self.grabFromTree('"Wind Wave Direction (WWD):"')) reading['average_wave_period'] = self.grabNumberFromString( self.grabFromTree('"Average Wave Period (APD):"')) # Grab the time reading['first_time'] = self.grabLocalTime(True) reading['second_time'] = self.grabLocalTime(False) # Grab the buoy name buoyName = self.grabBuoyName() buoy['name'] = buoyName reading['buoy_name'] = buoyName # Remove nulls reading = {k: v for k, v in reading.items() if v != None} # Instantiate model objects buoyObject = Buoy(**buoy) readingObject = Reading(**reading) #readingObject.id = self.db.readings.insert_one(readingObject.mongoDB()).inserted_id self.db.readings.update({'station_id': self.stationID}, readingObject.mongoDB(), upsert=True) self.db.buoys.update({'station_id': self.stationID}, buoyObject.mongoDB(), upsert=True)
def update(self, stationID): potentialBuoy = self.db.potentialBuoys.find_one( {'station_id': stationID}) if potentialBuoy is None: # This is not a valid stationID return buoyName = potentialBuoy['name'] buoy = {'station_id': potentialBuoy['station_id'], 'name': buoyName} # gets you the first box: # https://www.ndbc.noaa.gov/data/realtime2/41013.txt # then this for second box: # https://www.ndbc.noaa.gov/data/realtime2/44013.spec try: firstBoxData = urllib2.urlopen( "https://www.ndbc.noaa.gov/data/realtime2/" + stationID + ".txt").read(2000) secondBoxData = urllib2.urlopen( "https://www.ndbc.noaa.gov/data/realtime2/" + stationID + ".spec").read(2000) except urllib2.HTTPError: print('error caught trying to fetch ' + stationID) # TODO make this still update the reading as empty or something return # For some stations (e.g. 44008) the first box data is taken every 10 min, # but only 1 per hour (usually on the 50 min) has all the data. # First box columns and units: # YY MM DD hh mm WDIR WSPD GST WVHT DPD APD MWD PRES ATMP WTMP DEWP VIS PTDY TIDE # yr mo dy hr mn degT m/s m/s m sec sec degT hPa degC degC degC nmi hPa ft # Second box columns and units: # YY MM DD hh mm WVHT SwH SwP WWH WWP SwD WWD STEEPNESS APD MWD # yr mo dy hr mn m m sec m sec - degT - sec degT lines = firstBoxData.split('\n') firstLine = lines[0] firstLineValues = firstLine.split() minuteIndex = firstLineValues.index('mm') readingLines = lines[2:] # default to the latest if no 50 min readings are found latestFullReadingValues = readingLines[0].split() for readingLine in readingLines: readingValues = readingLine.split() minutes = readingValues[minuteIndex] if minutes == '50': latestFullReadingValues = readingValues break firstBoxDictionary = {} for index, label in enumerate(firstLineValues): readingValue = latestFullReadingValues[index] firstBoxDictionary[label] = readingValue lines = secondBoxData.split('\n') firstLine = lines[0] firstLineValues = firstLine.split() readingLine = lines[2] latestFullReadingValues = readingLine.split() secondBoxDictionary = {} for index, label in enumerate(firstLineValues): readingValue = latestFullReadingValues[index] secondBoxDictionary[label] = readingValue waveHeight = self.feetFromReading(firstBoxDictionary, 'WVHT') wavePeriod = self.floatFromReading(firstBoxDictionary, 'DPD') waveDirection = self.floatFromReading(firstBoxDictionary, 'MWD') swellHeight = self.feetFromReading(secondBoxDictionary, 'SwH') swellPeriod = self.floatFromReading(secondBoxDictionary, 'SwP') swellDirection = self.compassToDegreesFromReading( secondBoxDictionary, 'SwD') # Are all of these Strings? #2019-10-20T17:50:00Z try: wavesDatetime = (firstBoxDictionary['#YY'] + '-' + firstBoxDictionary['MM'] + '-' + firstBoxDictionary['DD'] + 'T' + firstBoxDictionary['hh'] + ':' + firstBoxDictionary['mm'] + ':00Z') except KeyError: wavesDatetime = None windDirection = self.floatFromReading(firstBoxDictionary, 'WDIR') windSpeed = self.knotsFromReading(firstBoxDictionary, 'WSPD') reading = { 'station_id': stationID, 'buoy_name': buoyName, 'wind_direction': windDirection, 'wind_speed': windSpeed, 'wave_height': waveHeight, 'dominant_period': wavePeriod, 'wave_direction': waveDirection, 'swell_height': swellHeight, 'swell_period': swellPeriod, 'swell_direction': swellDirection, 'datetime': wavesDatetime } # Remove nulls reading = {k: v for k, v in reading.items() if v != None} # Instantiate model objects buoyObject = Buoy(**buoy) readingObject = Reading(**reading) #readingObject.id = self.db.readings.insert_one(readingObject.mongoDB()).inserted_id self.db.readings.update({'station_id': stationID}, readingObject.mongoDB(), upsert=True) self.db.buoys.update({'station_id': stationID}, buoyObject.mongoDB(), upsert=True)
class Meter(object): class MeterReadngException(Exception): def __init__(self, msg): self.msg = msg def __str__(self): return self.msg '''Constructors''' def __init__(self, port='/dev/ttyUSB0', baud=57600, timeout=10, uid=None, unit='w', logger=None): '''Public Vars''' self.port = port self.baud = baud self.timeout = timeout self.uid = uid self.unit = unit self.logger = logger or logging.getLogger(__name__) '''Prvate Vars''' self._r = None self._data = None #self._meter = None self._meter = serial.Serial(self.port, self.baud, timeout=self.timeout) '''Properties (Getters/Setters''' '''? @property decorator?''' ''' def set_email(self, value) if '@' not in value: raise Exception("This doesn't look like an email address.") self._email = value def get_email(self): return self._email email = property(get_email, set_email) ''' '''Methods''' def __str__(self): return "Meter: " + self.uid def open(self): self._meter.open() def read(self): try: self._data = self._meter.readline() except: pass def close(self): self._meter.close() def parse(self): try: #http://www.marcus-povey.co.uk - USED REGEX REGEX! uidRegex = re.compile('<id>([0-9]+)</id>') valueRegex = re.compile('<watts>([0-9]+)</watts>') timeRegex = re.compile('<time>([0-9\.\:]+)</time>') value = str(int(valueRegex.findall(self._data)[0])) time = timeRegex.findall(self._data)[0] self.uid = uidRegex.findall(self._data)[0] self.logger.info('Parsed data sucessfully!') self._r = Reading(self, value, time, self.logger) except Exception: self.logger.error('Could not get details from device') def submit(self): try: self._r.submit() except Exception: self.logger.error('Reading not initialised') self._r = None
def Main(args=None): ##creates a main for the interface print("*Welcome to the robot interface*") ##demo str = raw_input("*Press c to connect to the robot*") if str == "c": inter = Interface() read = Reading() cond = raw_input("*enter a for commands, enter b for reading, enter c for exit*") if cond == "a": is_go = False while is_go == False: print("*enter the number you want for the command*") var = raw_input("*rp for reset and passive, ra2 for reading button, d to disconnect, r to reset\n sa for safe,pdc for pd controller, dr for drive*,pe for Pentagon drive, ! for stop, e for exit\n , f for full mode, drd for drive direct,p for play,so for song:") if var == "e": is_go = True inter.close() if var == "ra": read.isActive() if var == "ra2": inter.full() read.isActive2() if var == "wall": read.wall() if var == "pdc": read.pdcontroller() if var == "para": read.pararun() if var == "IC3": read.testIC3() if var == "IC5": read.testIC5() if var == "p": inter.play(0) ##inter.play(1) if var == "so": inter.song() else: inter.command(var) if cond == "b": inter.passive() choices = raw_input("*enter 1 for reading raw input and enter 2 to read sensor*") if choices == "1": raw_bytes = raw_input("*enter the number of bytes to read*") inter.read_raw(raw_bytes) if choices == "2": sensor_id = raw_input("*enter the sensor packet id*") inter.read_sensor(sensor_id) if cond == "c": is_connected = False inter.close() else: is_connected = False inter.close()
def test_duration(self): r = Reading(self.sometime, self.later, "wh", 1) self.assertEqual(r.duration(), timedelta(seconds=1))
import time from card import Card from querent import Querent from reading import Reading from tarotdb import TarotDB from tweeter import Tweeter import os from os import environ CONSUMER_KEY = os.environ.get('CONSUMER_KEY') CONSUMER_SECRET = os.environ.get('CONSUMER_SECRET') ACCESS_KEY = os.environ.get('ACCESS_KEY') ACCESS_SECRET = os.environ.get('ACCESS_SECRET') db = TarotDB("diviner.db") tw = Tweeter(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_SECRET) querents = Querent() for q in querents.get_querents(): reading = Reading(db, q) tweet = reading.get_reading() tw.tweet(tweet)
class ResourceApi: datastream = Datastream() sensor = Sensor() reading = Reading() statio = Station()
usage_json = coned.get_usage() except Exception as e: raise e usage = json.loads(usage_json) readings = [] for read in usage["reads"]: # Opower gives readings with null value for intervals that don't have data # yet, so skip them. if read["value"] is None: continue reading = Reading( dateutil.parser.isoparse(read["startTime"]), dateutil.parser.isoparse(read["endTime"]), usage["unit"], read["value"], ) readings.append(reading) cnx = mysql.connector.connect(user=MYSQL_USER, password=MYSQL_PASS, host=MYSQL_HOST, database=MYSQL_DB) cursor = cnx.cursor() for r in readings: add_reading = ("INSERT IGNORE INTO readings " "(start, end, wh) " "VALUES (%s, %s, %s)") data_reading = (r.start_time.astimezone(tz=timezone.utc),