} URI = "http://api.openweathermap.org/data/2.5/weather" def url( self, **kwds ): """Produce a url by joining the class' URI and OPTs with any keyword parameters""" return self.URI + "?" + urlencode( dict( self.OPT, **kwds )) def __getitem__( self, key ): """Obtain the temperature of the city's matching our Attribute's name, convert it to an appropriate type; return a value appropriate to the request.""" try: # eg. "http://api.openweathermap.org/...?...&q=City Name" data = urlopen( self.url( q=self.name )).read() if type( data ) is not str: # Python3 urlopen.read returns bytes data = data.decode( 'utf-8' ) weather = json.loads( data ) assert weather.get( 'cod' ) == 200 and 'main' in weather, \ weather.get( 'message', "Unknown error obtaining weather data" ) cast = float if isinstance( self.parser, REAL ) else int temperature = cast( weather['main']['temp'] ) except Exception as exc: logging.warning( "Couldn't get temperature for %s via %r: %s", self.name, self.url( q=self.name ), exc ) raise return [ temperature ] if self._validate_key( key ) is slice else temperature def __setitem__( self, key, value ): raise Exception( "Changing the weather isn't that easy..." ) sys.exit( main( attribute_class=Attribute_weather ))
def __setitem__( self, key, value ): try: # vvvv -- Process an EtherNet/IP CIP Write [Tag [Fragmented]]. # # We'll just store the value, and output the write request (and the value written) to # our time-series history file. # super( Attribute_historize, self ).__setitem__( key, value ) self.__logger.write( { 'write': value }, serial=(self.name, ( key.indices( len( self ))[0] if isinstance( key, slice ) else key, key.indices( len( self ))[1]-1 if isinstance( key, slice ) else key, ))) # ^^^^ except Exception as exc: # vvvv -- Process an EtherNet/IP CIP Write [Tag [Fragmented]] Exception. # # Something went wrong with the Write request processing. Log something intelligent and # re-raise the exception, to return a failure to the EtherNet/IP client. # self.__logger.comment( "%s: PLC I/O Write Tag %20s[%5s-%-5s] Exception: %s" % ( history.timestamp(), self.name, key.indices( len( self ))[0] if isinstance( key, slice ) else key, key.indices( len( self ))[1]-1 if isinstance( key, slice ) else key, exc )) # ^^^^ raise sys.exit( main( attribute_class=Attribute_historize ))
# vvvv -- Process an EtherNet/IP CIP Write [Tag [Fragmented]]. # # We'll just store the value, and output the write request (and the value written) to # our time-series history file. # super(Attribute_historize, self).__setitem__(key, value) self.__logger.write({'write': value}, serial=(self.name, ( key.indices(len(self))[0] if isinstance( key, slice) else key, key.indices(len(self))[1] - 1 if isinstance(key, slice) else key, ))) # ^^^^ except Exception as exc: # vvvv -- Process an EtherNet/IP CIP Write [Tag [Fragmented]] Exception. # # Something went wrong with the Write request processing. Log something intelligent and # re-raise the exception, to return a failure to the EtherNet/IP client. # self.__logger.comment( "%s: PLC I/O Write Tag %20s[%5s-%-5s] Exception: %s" % (history.timestamp(), self.name, key.indices(len(self))[0] if isinstance(key, slice) else key, key.indices(len(self))[1] - 1 if isinstance(key, slice) else key, exc)) # ^^^^ raise sys.exit(main(attribute_class=Attribute_historize))