INSTRUCTION
stringlengths
1
8.43k
RESPONSE
stringlengths
75
104k
Adds events to the queue. Will ignore events that occur before the settle time for that pin/ direction. Such events are assumed to be bouncing.
def add_event(self, event): """Adds events to the queue. Will ignore events that occur before the settle time for that pin/direction. Such events are assumed to be bouncing. """ # print("Trying to add event:") # print(event) # find out the pin settle time for pin_function_map in self.pin_function_maps: if _event_matches_pin_function_map(event, pin_function_map): # if pin_function_map.pin_num == event.pin_num and ( # pin_function_map.direction == event.direction or # pin_function_map.direction == IODIR_BOTH): pin_settle_time = pin_function_map.settle_time # print("EventQueue: Found event in map.") break else: # Couldn't find event in map, don't bother adding it to the queue # print("EventQueue: Couldn't find event in map:") # for pin_function_map in self.pin_function_maps: # print(pin_function_map) return threshold_time = self.last_event_time[event.pin_num] + pin_settle_time if event.timestamp > threshold_time: self.put(event) self.last_event_time[event.pin_num] = event.timestamp
Registers a pin number and direction to a callback function.
def register(self, pin_num, direction, callback, settle_time=DEFAULT_SETTLE_TIME): """Registers a pin number and direction to a callback function. :param pin_num: The pin pin number. :type pin_num: int :param direction: The event direction (use: IODIR_ON/IODIR_OFF/IODIR_BOTH) :type direction: int :param callback: The function to run when event is detected. :type callback: function :param settle_time: Time within which subsequent events are ignored. :type settle_time: int """ self.pin_function_maps.append( PinFunctionMap(pin_num, direction, callback, settle_time))
De - registers callback functions
def deregister(self, pin_num=None, direction=None): """De-registers callback functions :param pin_num: The pin number. If None then all functions are de-registered :type pin_num: int :param direction: The event direction. If None then all functions for the given pin are de-registered :type direction:int """ to_delete = [] for i, function_map in enumerate(self.pin_function_maps): if ( pin_num == None or ( function_map.pin_num == pin_num and ( direction == None or function_map.direction == direction ) ) ): to_delete.append(i) for i in reversed(to_delete): del self.pin_function_maps[i]
When deactivated the: class: PortEventListener will not run anything.
def deactivate(self): """When deactivated the :class:`PortEventListener` will not run anything. """ self.event_queue.put(self.TERMINATE_SIGNAL) self.dispatcher.join() self.detector.terminate() self.detector.join()
Enables GPIO interrupts.
def gpio_interrupts_enable(self): """Enables GPIO interrupts.""" try: bring_gpio_interrupt_into_userspace() set_gpio_interrupt_edge() except Timeout as e: raise InterruptEnableException( "There was an error bringing gpio%d into userspace. %s" % (GPIO_INTERRUPT_PIN, e.message) )
Sends bytes via the SPI bus.
def spisend(self, bytes_to_send): """Sends bytes via the SPI bus. :param bytes_to_send: The bytes to send on the SPI device. :type bytes_to_send: bytes :returns: bytes -- returned bytes from SPI device :raises: InitError """ # make some buffer space to store reading/writing wbuffer = ctypes.create_string_buffer(bytes_to_send, len(bytes_to_send)) rbuffer = ctypes.create_string_buffer(len(bytes_to_send)) # create the spi transfer struct transfer = spi_ioc_transfer( tx_buf=ctypes.addressof(wbuffer), rx_buf=ctypes.addressof(rbuffer), len=ctypes.sizeof(wbuffer), speed_hz=ctypes.c_uint32(self.speed_hz) ) if self.spi_callback is not None: self.spi_callback(bytes_to_send) # send the spi command ioctl(self.fd, SPI_IOC_MESSAGE(1), transfer) return ctypes.string_at(rbuffer, ctypes.sizeof(rbuffer))
Re - implement almost the same code from crispy_forms but passing form instance to item render_link method.
def render(self, form, form_style, context, template_pack=TEMPLATE_PACK): """ Re-implement almost the same code from crispy_forms but passing ``form`` instance to item ``render_link`` method. """ links, content = '', '' # accordion group needs the parent div id to set `data-parent` (I don't # know why). This needs to be a unique id if not self.css_id: self.css_id = "-".join(["tabsholder", text_type(randint(1000, 9999))]) for tab in self.fields: tab.active = False # Activate item self.open_target_group_for_form(form) for tab in self.fields: content += render_field( tab, form, form_style, context, template_pack=template_pack ) links += tab.render_link(form, template_pack) context.update({ 'tabs': self, 'links': links, 'content': content }) template = self.get_template_name(template_pack) return render_to_string(template, context.flatten())
Find tab fields listed as invalid
def has_errors(self, form): """ Find tab fields listed as invalid """ return any([fieldname_error for fieldname_error in form.errors.keys() if fieldname_error in self])
Render the link for the tab - pane. It must be called after render so css_class is updated with active class name if needed.
def render_link(self, form, template_pack=TEMPLATE_PACK, **kwargs): """ Render the link for the tab-pane. It must be called after render so ``css_class`` is updated with ``active`` class name if needed. """ link_template = self.link_template % template_pack return render_to_string(link_template, { 'link': self, 'item_has_errors': self.has_errors(form) })
Get package version from installed distribution or configuration file if not installed
def _extract_version(package_name): """ Get package version from installed distribution or configuration file if not installed """ try: return pkg_resources.get_distribution(package_name).version except pkg_resources.DistributionNotFound: _conf = read_configuration(os.path.join(PROJECT_DIR, "setup.cfg")) return _conf["metadata"]["version"]
Pass template pack argument
def get_form_kwargs(self): """ Pass template pack argument """ kwargs = super(FormContainersMixin, self).get_form_kwargs() kwargs.update({ 'pack': "foundation-{}".format(self.kwargs.get('foundation_version')) }) return kwargs
Check the status of the incoming response raise exception if status is not 200.
def _check_status(cls, response_json): """Check the status of the incoming response, raise exception if status is not 200. Args: response_json (dict): results of the response of the GET request. Returns: None """ status = response_json['status'] msg = response_json['msg'] if status == 400: raise BadRequestException(msg) elif status == 403: raise PermissionDeniedException(msg) elif status == 404: raise FileNotFoundException(msg) elif status == 451: raise UnavailableForLegalReasonsException(msg) elif status == 509: raise BandwidthUsageExceeded(msg) elif status >= 500: raise ServerErrorException(msg)
Used by every other method it makes a GET request with the given params.
def _get(self, url, params=None): """Used by every other method, it makes a GET request with the given params. Args: url (str): relative path of a specific service (account_info, ...). params (:obj:`dict`, optional): contains parameters to be sent in the GET request. Returns: dict: results of the response of the GET request. """ if not params: params = {} params.update({'login': self.login, 'key': self.key}) response_json = requests.get(self.api_url + url, params).json() return self._process_response(response_json)
Requests direct download link for requested file this method makes use of the response of prepare_download prepare_download must be called first.
def get_download_link(self, file_id, ticket, captcha_response=None): """Requests direct download link for requested file, this method makes use of the response of prepare_download, prepare_download must be called first. Args: file_id (str): id of the file to be downloaded. ticket (str): preparation ticket is found in prepare_download response,\ this is why we need to call prepare_download before get_download_link. captcha_response (:obj:`str`, optional): sometimes prepare_download will have captcha url to be solved, \ first, this is the solution of the captcha. Returns: dict: dictionary containing (file info, download url, ...). :: { "name": "The quick brown fox.txt", "size": 12345, "sha1": "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", "content_type": "plain/text", "upload_at": "2011-01-26 13:33:37", "url": "https://abvzps.example.com/dl/l/4spxX_-cSO4/The+quick+brown+fox.txt", "token": "4spxX_-cSO4" } """ params = {'ticket': ticket, 'file': file_id} if captcha_response: params['captcha_response'] = captcha_response return self._get('file/dl', params)
Makes a request to prepare for file upload.
def upload_link(self, folder_id=None, sha1=None, httponly=False): """Makes a request to prepare for file upload. Note: If folder_id is not provided, it will make and upload link to the ``Home`` folder. Args: folder_id (:obj:`str`, optional): folder-ID to upload to. sha1 (:obj:`str`, optional): expected sha1 If sha1 of uploaded file doesn't match this value, upload fails. httponly (:obj:`bool`, optional): If this is set to true, use only http upload links. Returns: dict: dictionary containing (url: will be used in actual upload, valid_until). :: { "url": "https://1fiafqj.oloadcdn.net/uls/nZ8H3X9e0AotInbU", "valid_until": "2017-08-19 19:06:46" } """ kwargs = {'folder': folder_id, 'sha1': sha1, 'httponly': httponly} params = {key: value for key, value in kwargs.items() if value} return self._get('file/ul', params=params)
Calls upload_link request to get valid url then it makes a post request with given file to be uploaded. No need to call upload_link explicitly since upload_file calls it.
def upload_file(self, file_path, folder_id=None, sha1=None, httponly=False): """Calls upload_link request to get valid url, then it makes a post request with given file to be uploaded. No need to call upload_link explicitly since upload_file calls it. Note: If folder_id is not provided, the file will be uploaded to ``Home`` folder. Args: file_path (str): full path of the file to be uploaded. folder_id (:obj:`str`, optional): folder-ID to upload to. sha1 (:obj:`str`, optional): expected sha1 If sha1 of uploaded file doesn't match this value, upload fails. httponly (:obj:`bool`, optional): If this is set to true, use only http upload links. Returns: dict: dictionary containing uploaded file info. :: { "content_type": "application/zip", "id": "0yiQTPzi4Y4", "name": 'favicons.zip', "sha1": 'f2cb05663563ec1b7e75dbcd5b96d523cb78d80c', "size": '24160', "url": 'https://openload.co/f/0yiQTPzi4Y4/favicons.zip' } """ upload_url_response_json = self.upload_link(folder_id=folder_id, sha1=sha1, httponly=httponly) upload_url = upload_url_response_json['url'] with open(file_path, 'rb') as f: response_json = requests.post(upload_url, files={'upload_file': f}).json() self._check_status(response_json) return response_json['result']
Used to make a remote file upload to openload. co
def remote_upload(self, remote_url, folder_id=None, headers=None): """Used to make a remote file upload to openload.co Note: If folder_id is not provided, the file will be uploaded to ``Home`` folder. Args: remote_url (str): direct link of file to be remotely downloaded. folder_id (:obj:`str`, optional): folder-ID to upload to. headers (:obj:`dict`, optional): additional HTTP headers (e.g. Cookies or HTTP Basic-Auth) Returns: dict: dictionary containing ("id": uploaded file id, "folderid"). :: { "id": "12", "folderid": "4248" } """ kwargs = {'folder': folder_id, 'headers': headers} params = {'url': remote_url} params.update({key: value for key, value in kwargs.items() if value}) return self._get('remotedl/add', params=params)
Checks a remote file upload to status.
def remote_upload_status(self, limit=None, remote_upload_id=None): """Checks a remote file upload to status. Args: limit (:obj:`int`, optional): Maximum number of results (Default: 5, Maximum: 100). remote_upload_id (:obj:`str`, optional): Remote Upload ID. Returns: dict: dictionary containing all remote uploads, each dictionary element is a dictionary. :: { "24": { "id": "24", "remoteurl": "http://proof.ovh.net/files/100Mio.dat", "status": "new", "folderid": "4248", "added": "2015-02-21 09:20:26", "last_update": "2015-02-21 09:20:26", "extid": False, "url": False }, "22": { "id": "22", "remoteurl": "http://proof.ovh.net/files/1Gio.dat", "status": "downloading", "bytes_loaded": "823997062", "bytes_total": "1073741824", "folderid": "4248", "added": "2015-02-21 09:20:26", "last_update": "2015-02-21 09:21:56", "extid": False, "url": False }, ... } """ kwargs = {'limit': limit, 'id': remote_upload_id} params = {key: value for key, value in kwargs.items() if value} return self._get('remotedl/status', params=params)
Request a list of files and folders in specified folder.
def list_folder(self, folder_id=None): """Request a list of files and folders in specified folder. Note: if folder_id is not provided, ``Home`` folder will be listed Args: folder_id (:obj:`str`, optional): id of the folder to be listed. Returns: dict: dictionary containing only two keys ("folders", "files"), \ each key represents a list of dictionaries. :: { "folders": [ { "id": "5144", "name": ".videothumb" }, { "id": "5792", "name": ".subtitles" }, ... ], "files": [ { "name": "big_buck_bunny.mp4.mp4", "sha1": "c6531f5ce9669d6547023d92aea4805b7c45d133", "folderid": "4258", "upload_at": "1419791256", "status": "active", "size": "5114011", "content_type": "video/mp4", "download_count": "48", "cstatus": "ok", "link": "https://openload.co/f/UPPjeAk--30/big_buck_bunny.mp4.mp4", "linkextid": "UPPjeAk--30" }, ... ] } """ params = {'folder': folder_id} if folder_id else {} return self._get('file/listfolder', params=params)
Shows running file converts by folder
def running_conversions(self, folder_id=None): """Shows running file converts by folder Note: If folder_id is not provided, ``Home`` folder will be used. Args: folder_id (:obj:`str`, optional): id of the folder to list conversions of files exist in it. Returns: list: list of dictionaries, each dictionary represents a file conversion info. :: [ { "name": "Geysir.AVI", "id": "3565411", "status": "pending", "last_update": "2015-08-23 19:41:40", "progress": 0.32, "retries": "0", "link": "https://openload.co/f/f02JFG293J8/Geysir.AVI", "linkextid": "f02JFG293J8" }, .... ] """ params = {'folder': folder_id} if folder_id else {} return self._get('file/runningconverts', params=params)
calculates the heat index based upon temperature ( in F ) and humidity. http:// www. srh. noaa. gov/ bmx/ tables/ heat_index. html
def calc_heat_index(temp, hum): ''' calculates the heat index based upon temperature (in F) and humidity. http://www.srh.noaa.gov/bmx/tables/heat_index.html returns the heat index in degrees F. ''' if (temp < 80): return temp else: return -42.379 + 2.04901523 * temp + 10.14333127 * hum - 0.22475541 * \ temp * hum - 6.83783 * (10 ** -3) * (temp ** 2) - 5.481717 * \ (10 ** -2) * (hum ** 2) + 1.22874 * (10 ** -3) * (temp ** 2) * \ hum + 8.5282 * (10 ** -4) * temp * (hum ** 2) - 1.99 * \ (10 ** -6) * (temp ** 2) * (hum ** 2);
calculates the wind chill value based upon the temperature ( F ) and wind.
def calc_wind_chill(t, windspeed, windspeed10min=None): ''' calculates the wind chill value based upon the temperature (F) and wind. returns the wind chill in degrees F. ''' w = max(windspeed10min, windspeed) return 35.74 + 0.6215 * t - 35.75 * (w ** 0.16) + 0.4275 * t * (w ** 0.16);
calculates the humidity via the formula from weatherwise. org return the relative humidity
def calc_humidity(temp, dewpoint): ''' calculates the humidity via the formula from weatherwise.org return the relative humidity ''' t = fahrenheit_to_celsius(temp) td = fahrenheit_to_celsius(dewpoint) num = 112 - (0.1 * t) + td denom = 112 + (0.9 * t) rh = math.pow((num / denom), 8) return rh
calculates the dewpoint via the formula from weatherwise. org return the dewpoint in degrees F.
def calc_dewpoint(temp, hum): ''' calculates the dewpoint via the formula from weatherwise.org return the dewpoint in degrees F. ''' c = fahrenheit_to_celsius(temp) x = 1 - 0.01 * hum; dewpoint = (14.55 + 0.114 * c) * x; dewpoint = dewpoint + ((2.5 + 0.007 * c) * x) ** 3; dewpoint = dewpoint + (15.9 + 0.117 * c) * x ** 14; dewpoint = c - dewpoint; return celsius_to_fahrenheit(dewpoint)
Perform HTTP session to transmit defined weather values.
def publish(self): ''' Perform HTTP session to transmit defined weather values. ''' return self._publish( self.args, self.server, self.URI)
return CRC calc value from raw serial data
def get(data): ''' return CRC calc value from raw serial data ''' crc = 0 for byte in array('B', data): crc = (VProCRC.CRC_TABLE[(crc >> 8) ^ byte] ^ ((crc & 0xFF) << 8)) return crc
perform CRC check on raw serial data return true if valid. a valid CRC == 0.
def verify(data): ''' perform CRC check on raw serial data, return true if valid. a valid CRC == 0. ''' if len(data) == 0: return False crc = VProCRC.get(data) if crc: log.info("CRC Bad") else: log.debug("CRC OK") return not crc
given a packed storm date field unpack and return YYYY - MM - DD string.
def _unpack_storm_date(date): ''' given a packed storm date field, unpack and return 'YYYY-MM-DD' string. ''' year = (date & 0x7f) + 2000 # 7 bits day = (date >> 7) & 0x01f # 5 bits month = (date >> 12) & 0x0f # 4 bits return "%s-%s-%s" % (year, month, day)
return True if weather station returns Rev. B archives
def _use_rev_b_archive(self, records, offset): ''' return True if weather station returns Rev.B archives ''' # if pre-determined, return result if type(self._ARCHIVE_REV_B) is bool: return self._ARCHIVE_REV_B # assume, B and check 'RecType' field data = ArchiveBStruct.unpack_from(records, offset) if data['RecType'] == 0: log.info('detected archive rev. B') self._ARCHIVE_REV_B = True else: log.info('detected archive rev. A') self._ARCHIVE_REV_B = False return self._ARCHIVE_REV_B
issue wakeup command to device to take out of standby mode.
def _wakeup(self): ''' issue wakeup command to device to take out of standby mode. ''' log.info("send: WAKEUP") for i in xrange(3): self.port.write('\n') # wakeup device ack = self.port.read(len(self.WAKE_ACK)) # read wakeup string log_raw('read', ack) if ack == self.WAKE_ACK: return raise NoDeviceException('Can not access weather station')
write a single command with variable number of arguments. after the command the device must return ACK
def _cmd(self, cmd, *args, **kw): ''' write a single command, with variable number of arguments. after the command, the device must return ACK ''' ok = kw.setdefault('ok', False) self._wakeup() if args: cmd = "%s %s" % (cmd, ' '.join(str(a) for a in args)) for i in xrange(3): log.info("send: " + cmd) self.port.write(cmd + '\n') if ok: ack = self.port.read(len(self.OK)) # read OK log_raw('read', ack) if ack == self.OK: return else: ack = self.port.read(len(self.ACK)) # read ACK log_raw('read', ack) if ack == self.ACK: return raise NoDeviceException('Can not access weather station')
reads a raw string containing data read from the device provided ( in/ dev/ XXX ) format. all reads are non - blocking.
def _loop_cmd(self): ''' reads a raw string containing data read from the device provided (in /dev/XXX) format. all reads are non-blocking. ''' self._cmd('LOOP', 1) raw = self.port.read(LoopStruct.size) # read data log_raw('read', raw) return raw
issue a command to read the archive records after a known time stamp.
def _dmpaft_cmd(self, time_fields): ''' issue a command to read the archive records after a known time stamp. ''' records = [] # convert time stamp fields to buffer tbuf = struct.pack('2H', *time_fields) # 1. send 'DMPAFT' cmd self._cmd('DMPAFT') # 2. send time stamp + crc crc = VProCRC.get(tbuf) crc = struct.pack('>H', crc) # crc in big-endian format log_raw('send', tbuf + crc) self.port.write(tbuf + crc) # send time stamp + crc ack = self.port.read(len(self.ACK)) # read ACK log_raw('read', ack) if ack != self.ACK: return # if bad ack, return # 3. read pre-amble data raw = self.port.read(DmpStruct.size) log_raw('read', raw) if not VProCRC.verify(raw): # check CRC value log_raw('send ESC', self.ESC) self.port.write(self.ESC) # if bad, escape and abort return log_raw('send ACK', self.ACK) self.port.write(self.ACK) # send ACK # 4. loop through all page records dmp = DmpStruct.unpack(raw) log.info('reading %d pages, start offset %d' % (dmp['Pages'], dmp['Offset'])) for i in xrange(dmp['Pages']): # 5. read page data raw = self.port.read(DmpPageStruct.size) log_raw('read', raw) if not VProCRC.verify(raw): # check CRC value log_raw('send ESC', self.ESC) self.port.write(self.ESC) # if bad, escape and abort return log_raw('send ACK', self.ACK) self.port.write(self.ACK) # send ACK # 6. loop through archive records page = DmpPageStruct.unpack(raw) offset = 0 # assume offset at 0 if i == 0: offset = dmp['Offset'] * ArchiveAStruct.size while offset < ArchiveAStruct.size * 5: log.info('page %d, reading record at offset %d' % (page['Index'], offset)) if self._use_rev_b_archive(page['Records'], offset): a = ArchiveBStruct.unpack_from(page['Records'], offset) else: a = ArchiveAStruct.unpack_from(page['Records'], offset) # 7. verify that record has valid data, and store if a['DateStamp'] != 0xffff and a['TimeStamp'] != 0xffff: records.append(a) offset += ArchiveAStruct.size log.info('read all pages') return records
returns a dictionary of fields from the newest archive record in the device. return None when no records are new.
def _get_new_archive_fields(self): ''' returns a dictionary of fields from the newest archive record in the device. return None when no records are new. ''' for i in xrange(3): records = self._dmpaft_cmd(self._archive_time) if records is not None: break time.sleep(1) if records is None: raise NoDeviceException('Can not access weather station') # find the newest record new_rec = None for r in records: new_time = (r['DateStamp'], r['TimeStamp']) if self._archive_time < new_time: self._archive_time = new_time new_rec = r return new_rec
calculates the derived fields ( those fields that are calculated )
def _calc_derived_fields(self, fields): ''' calculates the derived fields (those fields that are calculated) ''' # convenience variables for the calculations below temp = fields['TempOut'] hum = fields['HumOut'] wind = fields['WindSpeed'] wind10min = fields['WindSpeed10Min'] fields['HeatIndex'] = calc_heat_index(temp, hum) fields['WindChill'] = calc_wind_chill(temp, wind, wind10min) fields['DewPoint'] = calc_dewpoint(temp, hum) # store current data string now = time.localtime() fields['DateStamp'] = time.strftime("%Y-%m-%d %H:%M:%S", now) fields['Year'] = now[0] fields['Month'] = str(now[1]).zfill(2) now = time.gmtime() fields['DateStampUtc'] = time.strftime("%Y-%m-%d %H:%M:%S", now) fields['YearUtc'] = now[0] fields['MonthUtc'] = str(now[1]).zfill(2)
read and parse a set of data read from the console. after the data is parsed it is available in the fields variable.
def parse(self): ''' read and parse a set of data read from the console. after the data is parsed it is available in the fields variable. ''' fields = self._get_loop_fields() fields['Archive'] = self._get_new_archive_fields() self._calc_derived_fields(fields) # set the fields variable the the values in the dict self.fields = fields
unpacks data from buf and returns a dication of named fields. the fields can be post - processed by extending the _post_unpack () method.
def unpack_from(self, buf, offset=0 ): ''' unpacks data from 'buf' and returns a dication of named fields. the fields can be post-processed by extending the _post_unpack() method. ''' data = super(Struct,self).unpack_from( buf, offset) items = dict(zip(self.fields,data)) return self._post_unpack(items)
main execution loop. query weather data and post to online service.
def weather_update(station, pub_sites, interval): ''' main execution loop. query weather data and post to online service. ''' station.parse() # read weather data # santity check weather data if station.fields['TempOut'] > 200: raise NoSensorException( 'Out of range temperature value: %.1f, check sensors' % (station.fields['TempOut'],)) gust, gust_dir = WindGust.get( station, interval ) # upload data in the following order: for ps in pub_sites: try: # try block necessary to attempt every publisher ps.set( pressure = station.fields['Pressure'], dewpoint = station.fields['DewPoint'], humidity = station.fields['HumOut'], tempf = station.fields['TempOut'], rainin = station.fields['RainRate'], rainday = station.fields['RainDay'], dateutc = station.fields['DateStampUtc'], windspeed = station.fields['WindSpeed10Min'], winddir = station.fields['WindDir'], windgust = gust, windgustdir = gust_dir, ) ps.publish() except (Exception) as e: log.warn('publisher %s: %s'%(ps.__class__.__name__,e))
setup system logging to desired verbosity.
def init_log( quiet, debug ): ''' setup system logging to desired verbosity. ''' from logging.handlers import SysLogHandler fmt = logging.Formatter( os.path.basename(sys.argv[0]) + ".%(name)s %(levelname)s - %(message)s") facility = SysLogHandler.LOG_DAEMON syslog = SysLogHandler(address='/dev/log',facility=facility) syslog.setFormatter( fmt ) log.addHandler(syslog) if not quiet: console = logging.StreamHandler() console.setFormatter( fmt ) log.addHandler(console) log.setLevel(logging.INFO) if debug: log.setLevel(logging.DEBUG)
use values in opts data to generate instances of publication services.
def get_pub_services(opts): ''' use values in opts data to generate instances of publication services. ''' sites = [] for p_key in vars(opts).keys(): args = getattr(opts,p_key) if p_key in PUB_SERVICES and args: if isinstance(args,tuple): ps = PUB_SERVICES[p_key](*args) else: ps = PUB_SERVICES[p_key](args) sites.append( ps ) return sites
read command line options to configure program behavior.
def get_options(parser): ''' read command line options to configure program behavior. ''' # station services # publication services pub_g = optparse.OptionGroup( parser, "Publication Services", '''One or more publication service must be specified to enable upload of weather data.''', ) pub_g.add_option('-w', '--wundergound', nargs=2, type='string', dest='wug', help='Weather Underground service; WUG=[SID(station ID), PASSWORD]') pub_g.add_option('-p', '--pws', nargs=2, type='string', dest='pws', help='PWS service; PWS=[SID(station ID), PASSWORD]') pub_g.add_option('-f', '--file', nargs=1, type='string', dest='file', help='Local file; FILE=[FILE_NAME]') parser.add_option_group(pub_g) parser.add_option('-d', '--debug', dest='debug', action="store_true", default=False, help='enable verbose debug logging') parser.add_option('-q', '--quiet', dest='quiet', action="store_true", default=False, help='disable all console logging') parser.add_option('-t', '--tty', dest='tty', default='/dev/ttyS0', help='set serial port device [/dev/ttyS0]') parser.add_option('-n', '--interval', dest='interval', default=60, type='int', help='polling/update interval in seconds [60]') return parser.parse_args()
return gust data if above threshold value and current time is inside reporting window period
def get( self, station, interval ): ''' return gust data, if above threshold value and current time is inside reporting window period ''' rec = station.fields['Archive'] # process new data if rec: threshold = station.fields['WindSpeed10Min'] + GUST_MPH_MIN if rec['WindHi'] >= threshold: self.value = (rec['WindHi'],rec['WindHiDir']) self.count = GUST_TTL * 60 / interval else: self.value = self.NO_VALUE # return gust value, if remaining time is left, and valid if self.count: self.count -= 1 else: self.value = self.NO_VALUE log.debug('wind gust of {0} mph from {1}'.format(*self.value)) return self.value
Useful for defining weather data published to the server. Parameters not set will be reset and not sent to server. Unknown keyword args will be silently ignored so be careful. This is necessary for publishers that support more fields than others.
def set( self, pressure='NA', dewpoint='NA', humidity='NA', tempf='NA', rainin='NA', rainday='NA', dateutc='NA', windgust='NA', windgustdir='NA', windspeed='NA', winddir='NA', clouds='NA', weather='NA', *args, **kw): ''' Useful for defining weather data published to the server. Parameters not set will be reset and not sent to server. Unknown keyword args will be silently ignored, so be careful. This is necessary for publishers that support more fields than others. ''' # see: http://wiki.wunderground.com/index.php/PWS_-_Upload_Protocol # unused, but valid, parameters are: # windspdmph_avg2m, winddir_avg2m, windgustmph_10m, windgusdir_10m # soiltempf, soilmoisture, leafwetness, solarradiation, UV # indoortempf, indoorhumidity self.args.update( { 'baromin':pressure, 'clouds':clouds, 'dailyrainin':rainday, 'dateutc':dateutc, 'dewptf':dewpoint, 'humidity':humidity, 'rainin':rainin, 'tempf':tempf, 'weather':weather, 'winddir':winddir, 'windgustdir':windgustdir, 'windgustmph':windgust, 'windspeedmph':windspeed, } ) log.debug( self.args )
Store keyword args to be written to output file.
def set( self, **kw): ''' Store keyword args to be written to output file. ''' self.args = kw log.debug( self.args )
Write output file.
def publish(self): ''' Write output file. ''' with open( self.file_name, 'w') as fh: for k,v in self.args.iteritems(): buf = StringIO.StringIO() buf.write(k) self._append_vals(buf,v) fh.write(buf.getvalue() + '\n') buf.close()
Standalone decorator to apply requirements to routes either function handlers or class based views::
def requires(*requirements, **opts): """ Standalone decorator to apply requirements to routes, either function handlers or class based views:: @requires(MyRequirement()) def a_view(): pass class AView(View): decorators = [requires(MyRequirement())] :param requirements: The requirements to apply to this route :param throws: Optional. Exception or exception instance to throw if authorization fails. :param on_fail: Optional. Value or function to use when authorization fails. :param identity: Optional. An identity to use in place of the currently loaded identity. """ identity = opts.get("identity") on_fail = opts.get("on_fail") throws = opts.get("throws") def decorator(f): @wraps(f) def allower(*args, **kwargs): result = allows.run( requirements, identity=identity, on_fail=on_fail, throws=throws, f_args=args, f_kwargs=kwargs, ) # authorization failed if result is not None: return result return f(*args, **kwargs) return allower return decorator
Used to protect an entire blueprint with a set of requirements. If a route handler inside the blueprint should be exempt then it may be decorated with the: func: ~flask_allows. views. exempt_from_requirements decorator.
def guard_entire(requirements, identity=None, throws=None, on_fail=None): """ Used to protect an entire blueprint with a set of requirements. If a route handler inside the blueprint should be exempt, then it may be decorated with the :func:`~flask_allows.views.exempt_from_requirements` decorator. This function should be registered as a before_request handler on the blueprint and provided with the requirements to guard the blueprint with:: my_bp = Blueprint(__name__, 'namespace') my_bp.before_request(guard_entire(MustBeLoggedIn())) ``identity``, ``on_fail`` and ``throws`` may also be provided but are optional. If on_fails returns a non-None result, that will be considered the return value of the routing:: from flask import flash, redirect def flash_and_redirect(message, level, endpoint): def _(*a, **k): flash(message, level) return redirect(endpoint) return _ bp = Blueprint(__name__, 'namespace') bp.before_request( guard_entire( [MustBeLoggedIn()], on_fail=flash_and_redirect( "Please login in first", "warning", "login" ) ) ) ``on_fail`` will also receive anything found in ``flask.request.view_args`` as keyword arguments. If needed, this guard may be applied multiple times. This may be useful if different conditions should result in different `on_fail` mechanisms being invoked:: bp = Blueprint(__name__, "admin_panel") bp.before_request( guard_entire( [MustBeLoggedIn()], on_fail=flash_and_redirect( "Please login in first", "warning", "login" ) ) ) bp.before_request( guard_entire( [MustBeAdmin()], on_fail=flash_and_redirect( "You are not an admin.", "danger", "index" ) ) ) :param requirements: An iterable of requirements to apply to every request routed to the blueprint. :param identity: Optional. The identity that should be used for fulfilling requirements on the blueprint level. :param throws: Optional. Exception or exception type to be thrown if authorization fails. :param on_fail: Optional. Value or function to use if authorization fails. .. versionadded: 0.7.0 """ def guarder(): if _should_run_requirements(): return allows.run( requirements, identity=identity, on_fail=on_fail, throws=throws, f_kwargs=request.view_args, ) return None return guarder
Helper decorator for transitioning to user - only requirements this aids in situations where the request may be marked optional and causes an incorrect flow into user - only requirements.
def wants_request(f): """ Helper decorator for transitioning to user-only requirements, this aids in situations where the request may be marked optional and causes an incorrect flow into user-only requirements. This decorator causes the requirement to look like a user-only requirement but passes the current request context internally to the requirement. This decorator is intended only to assist during a transitionary phase and will be removed in flask-allows 1.0 See: :issue:`20,27` """ @wraps(f) def wrapper(user): return f(user, request) return wrapper
Short cut helper to construct a combinator that uses: meth: operator. and_ to reduce requirement results and stops evaluating on the first False.
def And(cls, *requirements): """ Short cut helper to construct a combinator that uses :meth:`operator.and_` to reduce requirement results and stops evaluating on the first False. This is also exported at the module level as ``And`` """ return cls(*requirements, op=operator.and_, until=False)
Short cut helper to construct a combinator that uses: meth: operator. or_ to reduce requirement results and stops evaluating on the first True.
def Or(cls, *requirements): """ Short cut helper to construct a combinator that uses :meth:`operator.or_` to reduce requirement results and stops evaluating on the first True. This is also exported at the module level as ``Or`` """ return cls(*requirements, op=operator.or_, until=True)
Initializes the Flask - Allows object against the provided application
def init_app(self, app): """ Initializes the Flask-Allows object against the provided application """ if not hasattr(app, "extensions"): # pragma: no cover app.extensions = {} app.extensions["allows"] = self @app.before_request def start_context(*a, **k): self.overrides.push(Override()) self.additional.push(Additional()) @app.after_request def cleanup(response): self.clear_all_overrides() self.clear_all_additional() return response
Checks that the provided or current identity meets each requirement passed to this method.
def fulfill(self, requirements, identity=None): """ Checks that the provided or current identity meets each requirement passed to this method. This method takes into account both additional and overridden requirements, with overridden requirements taking precedence:: allows.additional.push(Additional(Has('foo'))) allows.overrides.push(Override(Has('foo'))) allows.fulfill([], user_without_foo) # return True :param requirements: The requirements to check the identity against. :param identity: Optional. Identity to use in place of the current identity. """ identity = identity or self._identity_loader() if self.additional.current: all_requirements = chain(iter(self.additional.current), requirements) else: all_requirements = iter(requirements) if self.overrides.current is not None: all_requirements = ( r for r in all_requirements if r not in self.overrides.current ) return all(_call_requirement(r, identity, request) for r in all_requirements)
Used to preform a full run of the requirements and the options given this method will invoke on_fail and/ or throw the appropriate exception type. Can be passed arguments to call on_fail with via f_args ( which are passed positionally ) and f_kwargs ( which are passed as keyword ).
def run( self, requirements, identity=None, throws=None, on_fail=None, f_args=(), f_kwargs=ImmutableDict(), # noqa: B008 use_on_fail_return=True, ): """ Used to preform a full run of the requirements and the options given, this method will invoke on_fail and/or throw the appropriate exception type. Can be passed arguments to call on_fail with via f_args (which are passed positionally) and f_kwargs (which are passed as keyword). :param requirements: The requirements to check :param identity: Optional. A specific identity to use for the check :param throws: Optional. A specific exception to throw for this check :param on_fail: Optional. A callback to invoke after failure, alternatively a value to return when failure happens :param f_args: Positional arguments to pass to the on_fail callback :param f_kwargs: Keyword arguments to pass to the on_fail callback :param use_on_fail_return: Boolean (default True) flag to determine if the return value should be used. If true, the return value will be considered, else failure will always progress to exception raising. """ throws = throws or self.throws on_fail = _make_callable(on_fail) if on_fail is not None else self.on_fail if not self.fulfill(requirements, identity): result = on_fail(*f_args, **f_kwargs) if use_on_fail_return and result is not None: return result raise throws
Binds an override to the current context optionally use the current overrides in conjunction with this override
def push(self, override, use_parent=False): """ Binds an override to the current context, optionally use the current overrides in conjunction with this override If ``use_parent`` is true, a new override is created from the parent and child overrides rather than manipulating either directly. """ current = self.current if use_parent and current: override = current + override _override_ctx_stack.push((self, override))
Pops the latest override context.
def pop(self): """ Pops the latest override context. If the override context was pushed by a different override manager, a ``RuntimeError`` is raised. """ rv = _override_ctx_stack.pop() if rv is None or rv[0] is not self: raise RuntimeError( "popped wrong override context ({} instead of {})".format(rv, self) )
Allows temporarily pushing an override context yields the new context into the following block.
def override(self, override, use_parent=False): """ Allows temporarily pushing an override context, yields the new context into the following block. """ self.push(override, use_parent) yield self.current self.pop()
Binds an additional to the current context optionally use the current additionals in conjunction with this additional
def push(self, additional, use_parent=False): """ Binds an additional to the current context, optionally use the current additionals in conjunction with this additional If ``use_parent`` is true, a new additional is created from the parent and child additionals rather than manipulating either directly. """ current = self.current if use_parent and current: additional = current + additional _additional_ctx_stack.push((self, additional))
Pops the latest additional context.
def pop(self): """ Pops the latest additional context. If the additional context was pushed by a different additional manager, a ``RuntimeError`` is raised. """ rv = _additional_ctx_stack.pop() if rv is None or rv[0] is not self: raise RuntimeError( "popped wrong additional context ({} instead of {})".format(rv, self) )
Allows temporarily pushing an additional context yields the new context into the following block.
def additional(self, additional, use_parent=False): """ Allows temporarily pushing an additional context, yields the new context into the following block. """ self.push(additional, use_parent) yield self.current self.pop()
Append a number to duplicate field names to make them unique.
def unduplicate_field_names(field_names): """Append a number to duplicate field names to make them unique. """ res = [] for k in field_names: if k in res: i = 1 while k + '_' + str(i) in res: i += 1 k += '_' + str(i) res.append(k) return res
Generates the string to be shown as updates after the execution of a Cypher query
def interpret_stats(results): """Generates the string to be shown as updates after the execution of a Cypher query :param results: ``ResultSet`` with the raw results of the execution of the Cypher query """ stats = results.stats contains_updates = stats.pop("contains_updates", False) if stats else False if not contains_updates: result = '{} rows affected.'.format(len(results)) else: result = '' for stat, value in stats.items(): if value: result = "{}\n{} {}.".format(result, value, stat.replace("_", " ")) return result.strip()
Generates a dictionary with safe keys and values to pass onto Neo4j
def extract_params_from_query(query, user_ns): """Generates a dictionary with safe keys and values to pass onto Neo4j :param query: string with the Cypher query to execute :param user_ns: dictionary with the IPython user space """ # TODO: Optmize this function params = {} for k, v in user_ns.items(): try: json.dumps(v) params[k] = v except: pass return params
Executes a query and depending on the options of the extensions will return raw data a ResultSet a Pandas DataFrame or a NetworkX graph.
def run(query, params=None, config=None, conn=None, **kwargs): """Executes a query and depending on the options of the extensions will return raw data, a ``ResultSet``, a Pandas ``DataFrame`` or a NetworkX graph. :param query: string with the Cypher query :param params: dictionary with parameters for the query (default=``None``) :param config: Configurable or NamedTuple with extra IPython configuration details. If ``None``, a new object will be created (defaults=``None``) :param conn: connection dictionary or string for the Neo4j backend. If ``None``, a new connection will be created (default=``None``) :param **kwargs: Any of the cell configuration options. """ if params is None: params = {} if conn is None: conn = Connection.get(DEFAULT_CONFIGURABLE["uri"]) elif isinstance(conn, string_types): conn = Connection.get(conn) if config is None: default_config = DEFAULT_CONFIGURABLE.copy() kwargs.update(default_config) config = DefaultConfigurable(**kwargs) if query.strip(): # TODO: Handle multiple queries params = extract_params_from_query(query, params) result = conn.session.query(query, params, data_contents=config.data_contents) if config.feedback: print(interpret_stats(result)) resultset = ResultSet(result, query, config) if config.auto_pandas: return resultset.get_dataframe() elif config.auto_networkx: graph = resultset.get_graph() resultset.draw() return graph else: return resultset # returning only last result, intentionally else: return 'Connected: %s' % conn.name
Returns a Pandas DataFrame instance built from the result set.
def get_dataframe(self): """Returns a Pandas DataFrame instance built from the result set.""" if pd is None: raise ImportError("Try installing Pandas first.") frame = pd.DataFrame(self[:], columns=(self and self.keys) or []) return frame
Returns a NetworkX multi - graph instance built from the result set
def get_graph(self, directed=True): """Returns a NetworkX multi-graph instance built from the result set :param directed: boolean, optional (default=`True`). Whether to create a direted or an undirected graph. """ if nx is None: raise ImportError("Try installing NetworkX first.") if directed: graph = nx.MultiDiGraph() else: graph = nx.MultiGraph() for item in self._results.graph: for node in item['nodes']: properties = copy.deepcopy(node['properties']) properties['labels'] = node['labels'] graph.add_node(node['id'], **properties) for rel in item['relationships']: properties = copy.deepcopy(rel['properties']) properties.update( id=rel['id'], type=rel['type'] ) graph.add_edge(rel['startNode'], rel['endNode'], key=rel.get('type'), **properties) return graph
Plot of a NetworkX multi - graph instance
def draw(self, directed=True, layout="spring", node_label_attr=None, show_node_labels=True, edge_label_attr=None, show_edge_labels=True, node_size=1600, node_color='blue', node_alpha=0.3, node_text_size=12, edge_color='blue', edge_alpha=0.3, edge_tickness=1, edge_text_pos=0.3, text_font='sans-serif', ax=None): """Plot of a NetworkX multi-graph instance :param directed: boolean, optional (default=`True`). Whether to return a directed graph or not. :param layout: string, optional (default=`"spring"`). Layout to apply. Any of the possible NetworkX layouts will work: ``'circular_layout'``, ``'random_layout'``, ``'shell_layout'``, ``'spring_layout'``, ``'spectral_layout'``, or ``'fruchterman_reingold_layout'``. :param node_label_attr: string, optional (default=`None`). Attribute of the nodes that has to be used as the label. :param show_node_labels: boolean, optional (default=`True`). Whether to show or not the labels of the nodes. :param edge_label_attr: boolean, optional (default=`None`). Attribute of the edges that has to be used as the label. :param show_edge_labels: . optional (default=`True`). Whether to show or not the labels of the edges. :param node_size: integer, optional (default=`1600`). Desired size for nodes. :param node_color: color string, or array of floats, (default=`'blue'`) Node color. Can be a single color format string, or a sequence of colors with the same length as nodelist. If numeric values are specified they will be mapped to colors using the ``cmap`` and ``vmin``, ``vmax`` parameters. See ``matplotlib.scatter`` for more details. :param node_alpha: float, optional (default=`0.3`). Between 0 and 1 for transparency of nodes. :param node_text_size: integer, optional (default=`12`). Size of the node text. :param edge_color: color string, or array of floats (default=`'blue'`) Edge color. Can be a single color format string, or a sequence of colors with the same length as edgelist. If numeric values are specified they will be mapped to colors using the ``edge_cmap`` and ``edge_vmin``, ``edge_vmax`` parameters. :param edge_alpha: float, optional (default=`0.3`) Transparency for thee edges. :param edge_tickness: float or integer, optional (default=`1`). Thickness of the lines drawn for the edges. :param edge_text_pos: . Default to optional (d0)= :param text_font: . Default to optional (default=`'sans-serif'`). :param ax: ``matplotlib.Figure``, optional (default=`None`). A ``matplotlib.Figure`` to use when rendering the graph. If `None`, a new object is created and returned.---- :return: a ``matplotlib.Figure`` with the graph rendered. """ graph = self.get_graph(directed=directed) pos = getattr(nx, "{}_layout".format(layout))(graph) node_labels = {} edge_labels = {} node_colors = set() if show_node_labels: for node, props in graph.nodes(data=True): labels = props.pop('labels', []) for label in labels: node_colors.add(label) if node_label_attr is None: node_labels[node] = "$:{}$\n{}".format( ":".join(labels), next(iter(props.values())) if props else "", ) else: props_list = ["{}: {}".format(k, v) for k, v in props.items()] node_labels[node] = "$:{}$\n{}".format( ":".join(labels), "\n".join(props_list) ) node_color = [] node_colors = list(node_colors) legend_colors = [] colors = list(plt.matplotlib.colors.ColorConverter().cache.items())[2:] for _, color_rgb in colors[:len(node_colors)]: node_color.append(color_rgb) legend_colors.append(color_rgb) if show_edge_labels: for start, end, props in graph.edges(data=True): if edge_label_attr is None: edge_label = props.get("type", '') else: edge_label = props.get(edge_label_attr, '') edge_labels[(start, end)] = edge_label if not ax: fig = plt.figure() ax = fig.add_subplot(111) nodes = nx.draw_networkx_nodes( graph, pos=pos, node_color=node_color, node_size=node_size, alpha=node_alpha, ax=ax ) nx.draw_networkx_labels( graph, pos=pos, labels=node_labels, font_size=node_text_size, font_family=text_font, ax=ax ) nx.draw_networkx_edges( graph, pos=pos, width=edge_tickness, alpha=edge_alpha, edge_color=edge_color, ax=ax ) nx.draw_networkx_edge_labels( graph, pos=pos, edge_labels=edge_labels, ax=ax ) ax.legend([plt.Line2D([0], [0], linestyle="none", marker="o", alpha=node_alpha, markersize=10, markerfacecolor=color) for color in legend_colors], node_colors, loc=(-0.25, 1), numpoints=1, frameon=False) ax.set_axis_off() return graph, ax, nodes
Generates a pylab pie chart from the result set.
def pie(self, key_word_sep=" ", title=None, **kwargs): """Generates a pylab pie chart from the result set. ``matplotlib`` must be installed, and in an IPython Notebook, inlining must be on:: %%matplotlib inline Values (pie slice sizes) are taken from the rightmost column (numerical values required). All other columns are used to label the pie slices. :param key_word_sep: string used to separate column values from each other in pie labels :param title: plot title, defaults to name of value column :kwargs: any additional keyword arguments will be passsed through to ``matplotlib.pylab.pie``. """ if not plt: raise ImportError("Try installing matplotlib first.") self.guess_pie_columns(xlabel_sep=key_word_sep) pie = plt.pie(self.ys[0], labels=self.xlabels, **kwargs) plt.title(title or self.ys[0].name) return pie
Generates a pylab plot from the result set.
def plot(self, title=None, **kwargs): """Generates a pylab plot from the result set. ``matplotlib`` must be installed, and in an IPython Notebook, inlining must be on:: %%matplotlib inline The first and last columns are taken as the X and Y values. Any columns between are ignored. :param title: plot title, defaults to names of Y value columns Any additional keyword arguments will be passsed through to ``matplotlib.pylab.plot``. """ if not plt: raise ImportError("Try installing matplotlib first.") self.guess_plot_columns() self.x = self.x or range(len(self.ys[0])) coords = reduce(operator.add, [(self.x, y) for y in self.ys]) plot = plt.plot(*coords, **kwargs) if hasattr(self.x, 'name'): plt.xlabel(self.x.name) ylabel = ", ".join(y.name for y in self.ys) plt.title(title or ylabel) plt.ylabel(ylabel) return plot
Generates a pylab bar plot from the result set.
def bar(self, key_word_sep=" ", title=None, **kwargs): """Generates a pylab bar plot from the result set. ``matplotlib`` must be installed, and in an IPython Notebook, inlining must be on:: %%matplotlib inline The last quantitative column is taken as the Y values; all other columns are combined to label the X axis. :param title: plot title, defaults to names of Y value columns :param key_word_sep: string used to separate column values from each other in labels Any additional keyword arguments will be passsed through to ``matplotlib.pylab.bar``. """ if not plt: raise ImportError("Try installing matplotlib first.") self.guess_pie_columns(xlabel_sep=key_word_sep) plot = plt.bar(range(len(self.ys[0])), self.ys[0], **kwargs) if self.xlabels: plt.xticks(range(len(self.xlabels)), self.xlabels, rotation=45) plt.xlabel(self.xlabel) plt.ylabel(self.ys[0].name) return plot
Generates results in comma - separated form. Write to filename if given. Any other parameter will be passed on to csv. writer.
def csv(self, filename=None, **format_params): """Generates results in comma-separated form. Write to ``filename`` if given. Any other parameter will be passed on to ``csv.writer``. :param filename: if given, the CSV will be written to filename. Any additional keyword arguments will be passsed through to ``csv.writer``. """ if not self.pretty: return None # no results if filename: outfile = open(filename, 'w') else: outfile = StringIO() writer = UnicodeWriter(outfile, **format_params) writer.writerow(self.field_names) for row in self: writer.writerow(row) if filename: outfile.close() return CsvResultDescriptor(filename) else: return outfile.getvalue()
Re - implementation of the permission_required decorator honors settings.
def permission_required(perm, login_url=None, raise_exception=False): """ Re-implementation of the permission_required decorator, honors settings. If ``DASHBOARD_REQUIRE_LOGIN`` is False, this decorator will always return ``True``, otherwise it will check for the permission as usual. """ def check_perms(user): if not getattr(settings, 'DASHBOARD_REQUIRE_LOGIN', app_settings.REQUIRE_LOGIN): return True # First check if the user has the permission (even anon users) if user.has_perm(perm): return True # In case the 403 handler should be called raise the exception if raise_exception: # pragma: no cover raise PermissionDenied # As the last resort, show the login form return False return user_passes_test(check_perms, login_url=login_url)
Adds is_rendered to the context and the widget s context data.
def get_context_data(self, **kwargs): """ Adds ``is_rendered`` to the context and the widget's context data. ``is_rendered`` signals that the AJAX view has been called and that we are displaying the full widget now. When ``is_rendered`` is not found in the widget template it means that we are seeing the first page load and all widgets still have to get their real data from this AJAX view. """ ctx = super(RenderWidgetMixin, self).get_context_data(**kwargs) ctx.update({ 'is_rendered': True, 'widget': self.widget, }) ctx.update(self.widget.get_context_data()) return ctx
Returns the widgets sorted by position.
def get_widgets_sorted(self): """Returns the widgets sorted by position.""" result = [] for widget_name, widget in self.get_widgets().items(): result.append((widget_name, widget, widget.position)) result.sort(key=lambda x: x[2]) return result
Returns all widgets that need an update.
def get_widgets_that_need_update(self): """ Returns all widgets that need an update. This should be scheduled every minute via crontab. """ result = [] for widget_name, widget in self.get_widgets().items(): if widget.should_update(): result.append(widget) return result
Registers the given widget.
def register_widget(self, widget_cls, **widget_kwargs): """ Registers the given widget. Widgets must inherit ``DashboardWidgetBase`` and you cannot register the same widget twice. :widget_cls: A class that inherits ``DashboardWidgetBase``. """ if not issubclass(widget_cls, DashboardWidgetBase): raise ImproperlyConfigured( 'DashboardWidgets must be subclasses of DashboardWidgetBase,' ' {0} is not.'.format(widget_cls)) widget = widget_cls(**widget_kwargs) widget_name = widget.get_name() if widget_name in self.widgets: raise WidgetAlreadyRegistered( 'Cannot register {0}, a plugin with this name {1} is already ' 'registered.'.format(widget_cls, widget_name)) self.widgets[widget_name] = widget
Unregisters the given widget.
def unregister_widget(self, widget_cls): """Unregisters the given widget.""" if widget_cls.__name__ in self.widgets: del self.widgets[widget_cls().get_name()]
Gets or creates the last update object for this widget.
def get_last_update(self): """Gets or creates the last update object for this widget.""" instance, created = \ models.DashboardWidgetLastUpdate.objects.get_or_create( widget_name=self.get_name()) return instance
Returns the setting for this widget from the database.
def get_setting(self, setting_name, default=None): """ Returns the setting for this widget from the database. :setting_name: The name of the setting. :default: Optional default value if the setting cannot be found. """ try: setting = models.DashboardWidgetSettings.objects.get( widget_name=self.get_name(), setting_name=setting_name) except models.DashboardWidgetSettings.DoesNotExist: setting = default return setting
Saves the setting value into the database.
def save_setting(self, setting_name, value): """Saves the setting value into the database.""" setting = self.get_setting(setting_name) if setting is None: setting = models.DashboardWidgetSettings.objects.create( widget_name=self.get_name(), setting_name=setting_name, value=value) setting.value = value setting.save() return setting
Checks if an update is needed.
def should_update(self): """ Checks if an update is needed. Checks against ``self.update_interval`` and this widgets ``DashboardWidgetLastUpdate`` instance if an update is overdue. This should be called by ``DashboardWidgetPool.get_widgets_that_need_update()``, which in turn should be called by an admin command which should be scheduled every minute via crontab. """ last_update = self.get_last_update() time_since = now() - last_update.last_update if time_since.seconds < self.update_interval: return False return True
: param q: query by city name: param lat: latitude: param lon: longitude: param city_ids: comma separated city_id values: param count: number of max results to display
def getCityDetails(self, **kwargs): """ :param q: query by city name :param lat: latitude :param lon: longitude :param city_ids: comma separated city_id values :param count: number of max results to display Find the Zomato ID and other details for a city . You can obtain the Zomato City ID in one of the following ways: -City Name in the Search Query - Returns list of cities matching the query -Using coordinates - Identifies the city details based on the coordinates of any location inside a city If you already know the Zomato City ID, this API can be used to get other details of the city. """ params = {} available_keys = ["q", "lat", "lon", "city_ids", "count"] for key in available_keys: if key in kwargs: params[key] = kwargs[key] cities = self.api.get("/cities", params) return cities
: param city_id: id of the city for which collections are needed: param lat: latitude: param lon: longitude: param count: number of max results to display Returns Zomato Restaurant Collections in a City. The location/ City input can be provided in the following ways - Using Zomato City ID - Using coordinates of any location within a city - List of all restaurants listed in any particular Zomato Collection can be obtained using the/ search API with Collection ID and Zomato City ID as the input
def getCollectionsViaCityId(self, city_id, **kwargs): """ :param city_id: id of the city for which collections are needed :param lat: latitude :param lon: longitude :param count: number of max results to display Returns Zomato Restaurant Collections in a City. The location/City input can be provided in the following ways - Using Zomato City ID - Using coordinates of any location within a city - List of all restaurants listed in any particular Zomato Collection can be obtained using the '/search' API with Collection ID and Zomato City ID as the input """ params = {"city_id": city_id} optional_params = ["lat", "lon", "count"] for key in optional_params: if key in kwargs: params[key] = kwargs[key] collections = self.api.get("/collections", params) return collections
: param city_id: id of the city for which collections are needed: param lat: latitude: param lon: longitude Get a list of restaurant types in a city. The location/ City input can be provided in the following ways - Using Zomato City ID - Using coordinates of any location within a city List of all restaurants categorized under a particular restaurant type can obtained using/ Search API with Establishment ID and location details as inputs
def getEstablishments(self, city_id, **kwargs): """ :param city_id: id of the city for which collections are needed :param lat: latitude :param lon: longitude Get a list of restaurant types in a city. The location/City input can be provided in the following ways - Using Zomato City ID - Using coordinates of any location within a city List of all restaurants categorized under a particular restaurant type can obtained using /Search API with Establishment ID and location details as inputs """ params = {"city_id": city_id} optional_params = ["lat", "lon"] for key in optional_params: if key in kwargs: params[key] = kwargs[key] establishments = self.api.get("/establishments", params) return establishments
: param lat: latitude: param lon: longitude Get Foodie and Nightlife Index list of popular cuisines and nearby restaurants around the given coordinates
def getByGeocode(self, lat, lon): """ :param lat: latitude :param lon: longitude Get Foodie and Nightlife Index, list of popular cuisines and nearby restaurants around the given coordinates """ params = {"lat": lat, "lon": lon} response = self.api.get("/geocode", params) return response
: param entity_id: location id obtained from locations api: param entity_type: location type obtained from locations api: return: Get Foodie Index Nightlife Index Top Cuisines and Best rated restaurants in a given location
def getLocationDetails(self, entity_id, entity_type): """ :param entity_id: location id obtained from locations api :param entity_type: location type obtained from locations api :return: Get Foodie Index, Nightlife Index, Top Cuisines and Best rated restaurants in a given location """ params = {"entity_id": entity_id, "entity_type": entity_type} location_details = self.api.get("/location_details", params) return location_details
: param query: suggestion for location name: param lat: latitude: param lon: longitude: param count: number of max results to display: return: json response Search for Zomato locations by keyword. Provide coordinates to get better search results
def getLocations(self, query, **kwargs): """ :param query: suggestion for location name :param lat: latitude :param lon: longitude :param count: number of max results to display :return: json response Search for Zomato locations by keyword. Provide coordinates to get better search results """ params = {"query": query} optional_params = ["lat", "lon", "count"] for key in optional_params: if key in kwargs: params[key] = kwargs[key] locations = self.api.get("/locations", params) return locations
: param restaurant_id: id of restaurant whose details are requested: return: json response Get daily menu using Zomato restaurant ID.
def getDailyMenu(self, restaurant_id): """ :param restaurant_id: id of restaurant whose details are requested :return: json response Get daily menu using Zomato restaurant ID. """ params = {"res_id": restaurant_id} daily_menu = self.api.get("/dailymenu", params) return daily_menu
: param restaurant_id: id of restaurant whose details are requested: return: json response Get detailed restaurant information using Zomato restaurant ID. Partner Access is required to access photos and reviews.
def getRestaurantDetails(self, restaurant_id): """ :param restaurant_id: id of restaurant whose details are requested :return: json response Get detailed restaurant information using Zomato restaurant ID. Partner Access is required to access photos and reviews. """ params = {"res_id": restaurant_id} restaurant_details = self.api.get("/restaurant", params) return restaurant_details
: param restaurant_id: id of restaurant whose details are requested: param start: fetch results after this offset: param count: max number of results to retrieve: return: json response Get restaurant reviews using the Zomato restaurant ID
def getRestaurantReviews(self, restaurant_id, **kwargs): """ :param restaurant_id: id of restaurant whose details are requested :param start: fetch results after this offset :param count: max number of results to retrieve :return: json response Get restaurant reviews using the Zomato restaurant ID """ params = {"res_id": restaurant_id} optional_params = ["start", "count"] for key in optional_params: if key in kwargs: params[key] = kwargs[key] reviews = self.api.get("/reviews", params) return reviews
: param entity_id: location id: param entity_type: location type ( city subzone zone lanmark metro group ): param q: search keyword: param start: fetch results after offset: param count: max number of results to display: param lat: latitude: param lon: longitude: param radius: radius around ( lat lon ) ; to define search area defined in meters ( M ): param cuisines: list of cuisine id s separated by comma: param establishment_type: estblishment id obtained from establishments call: param collection_id: collection id obtained from collections call: param category: category ids obtained from categories call: param sort: sort restaurants by ( cost rating real_distance ): param order: used with sort parameter to define ascending/ descending: return: json response The location input can be specified using Zomato location ID or coordinates. Cuisine/ Establishment/ Collection IDs can be obtained from respective api calls.
def search(self, **kwargs): """ :param entity_id: location id :param entity_type: location type (city, subzone, zone, lanmark, metro , group) :param q: search keyword :param start: fetch results after offset :param count: max number of results to display :param lat: latitude :param lon: longitude :param radius: radius around (lat,lon); to define search area, defined in meters(M) :param cuisines: list of cuisine id's separated by comma :param establishment_type: estblishment id obtained from establishments call :param collection_id: collection id obtained from collections call :param category: category ids obtained from categories call :param sort: sort restaurants by (cost, rating, real_distance) :param order: used with 'sort' parameter to define ascending / descending :return: json response The location input can be specified using Zomato location ID or coordinates. Cuisine / Establishment / Collection IDs can be obtained from respective api calls. Partner Access is required to access photos and reviews. Examples: - To search for 'Italian' restaurants in 'Manhattan, New York City', set cuisines = 55, entity_id = 94741 and entity_type = zone - To search for 'cafes' in 'Manhattan, New York City', set establishment_type = 1, entity_type = zone and entity_id = 94741 - Get list of all restaurants in 'Trending this Week' collection in 'New York City' by using entity_id = 280, entity_type = city and collection_id = 1 """ params = {} available_params = [ "entity_id", "entity_type", "q", "start", "count", "lat", "lon", "radius", "cuisines", "establishment_type", "collection_id", "category", "sort", "order"] for key in available_params: if key in kwargs: params[key] = kwargs[key] results = self.api.get("/search", params) return results
Create a spark bolt array from a local array.
def array(a, context=None, axis=(0,), dtype=None, npartitions=None): """ Create a spark bolt array from a local array. Parameters ---------- a : array-like An array, any object exposing the array interface, an object whose __array__ method returns an array, or any (nested) sequence. context : SparkContext A context running Spark. (see pyspark) axis : tuple, optional, default=(0,) Which axes to distribute the array along. The resulting distributed object will use keys to represent these axes, with the remaining axes represented by values. dtype : data-type, optional, default=None The desired data-type for the array. If None, will be determined from the data. (see numpy) npartitions : int Number of partitions for parallization. Returns ------- BoltArraySpark """ if dtype is None: arry = asarray(a) dtype = arry.dtype else: arry = asarray(a, dtype) shape = arry.shape ndim = len(shape) # handle the axes specification and transpose if necessary axes = ConstructSpark._format_axes(axis, arry.shape) key_axes, value_axes = get_kv_axes(arry.shape, axes) permutation = key_axes + value_axes arry = arry.transpose(*permutation) split = len(axes) if split < 1: raise ValueError("split axis must be greater than 0, got %g" % split) if split > len(shape): raise ValueError("split axis must not exceed number of axes %g, got %g" % (ndim, split)) key_shape = shape[:split] val_shape = shape[split:] keys = zip(*unravel_index(arange(0, int(prod(key_shape))), key_shape)) vals = arry.reshape((prod(key_shape),) + val_shape) rdd = context.parallelize(zip(keys, vals), npartitions) return BoltArraySpark(rdd, shape=shape, split=split, dtype=dtype)
Create a spark bolt array of ones.
def ones(shape, context=None, axis=(0,), dtype=float64, npartitions=None): """ Create a spark bolt array of ones. Parameters ---------- shape : tuple The desired shape of the array. context : SparkContext A context running Spark. (see pyspark) axis : tuple, optional, default=(0,) Which axes to distribute the array along. The resulting distributed object will use keys to represent these axes, with the remaining axes represented by values. dtype : data-type, optional, default=float64 The desired data-type for the array. If None, will be determined from the data. (see numpy) npartitions : int Number of partitions for parallization. Returns ------- BoltArraySpark """ from numpy import ones return ConstructSpark._wrap(ones, shape, context, axis, dtype, npartitions)
Join two bolt arrays together at least one of which is in spark.
def concatenate(arrays, axis=0): """ Join two bolt arrays together, at least one of which is in spark. Parameters ---------- arrays : tuple A pair of arrays. At least one must be a spark array, the other can be a local bolt array, a local numpy array, or an array-like. axis : int, optional, default=0 The axis along which the arrays will be joined. Returns ------- BoltArraySpark """ if not isinstance(arrays, tuple): raise ValueError("data type not understood") if not len(arrays) == 2: raise NotImplementedError("spark concatenation only supports two arrays") first, second = arrays if isinstance(first, BoltArraySpark): return first.concatenate(second, axis) elif isinstance(second, BoltArraySpark): first = ConstructSpark.array(first, second._rdd.context) return first.concatenate(second, axis) else: raise ValueError("at least one array must be a spark bolt array")
Check that arguments are consistent with spark array construction.
def _argcheck(*args, **kwargs): """ Check that arguments are consistent with spark array construction. Conditions are: (1) a positional argument is a SparkContext (2) keyword arg 'context' is a SparkContext (3) an argument is a BoltArraySpark, or (4) an argument is a nested list containing a BoltArraySpark """ try: from pyspark import SparkContext except ImportError: return False cond1 = any([isinstance(arg, SparkContext) for arg in args]) cond2 = isinstance(kwargs.get('context', None), SparkContext) cond3 = any([isinstance(arg, BoltArraySpark) for arg in args]) cond4 = any([any([isinstance(sub, BoltArraySpark) for sub in arg]) if isinstance(arg, (tuple, list)) else False for arg in args]) return cond1 or cond2 or cond3 or cond4
Format target axes given an array shape
def _format_axes(axes, shape): """ Format target axes given an array shape """ if isinstance(axes, int): axes = (axes,) elif isinstance(axes, list) or hasattr(axes, '__iter__'): axes = tuple(axes) if not isinstance(axes, tuple): raise ValueError("axes argument %s in the constructor not specified correctly" % str(axes)) if min(axes) < 0 or max(axes) > len(shape) - 1: raise ValueError("invalid key axes %s given shape %s" % (str(axes), str(shape))) return axes
Wrap an existing numpy constructor in a parallelized construction
def _wrap(func, shape, context=None, axis=(0,), dtype=None, npartitions=None): """ Wrap an existing numpy constructor in a parallelized construction """ if isinstance(shape, int): shape = (shape,) key_shape, value_shape = get_kv_shape(shape, ConstructSpark._format_axes(axis, shape)) split = len(key_shape) # make the keys rdd = context.parallelize(list(product(*[arange(x) for x in key_shape])), npartitions) # use a map to make the arrays in parallel rdd = rdd.map(lambda x: (x, func(value_shape, dtype, order='C'))) return BoltArraySpark(rdd, shape=shape, split=split, dtype=dtype)
Align local bolt array so that axes for iteration are in the keys.
def _align(self, axes, key_shape=None): """ Align local bolt array so that axes for iteration are in the keys. This operation is applied before most functional operators. It ensures that the specified axes are valid, and might transpose/reshape the underlying array so that the functional operators can be applied over the correct records. Parameters ---------- axes: tuple[int] One or more axes that will be iterated over by a functional operator Returns ------- BoltArrayLocal """ # ensure that the key axes are valid for an ndarray of this shape inshape(self.shape, axes) # compute the set of dimensions/axes that will be used to reshape remaining = [dim for dim in range(len(self.shape)) if dim not in axes] key_shape = key_shape if key_shape else [self.shape[axis] for axis in axes] remaining_shape = [self.shape[axis] for axis in remaining] linearized_shape = [prod(key_shape)] + remaining_shape # compute the transpose permutation transpose_order = axes + remaining # transpose the array so that the keys being mapped over come first, then linearize keys reshaped = self.transpose(*transpose_order).reshape(*linearized_shape) return reshaped
Filter array along an axis.
def filter(self, func, axis=(0,)): """ Filter array along an axis. Applies a function which should evaluate to boolean, along a single axis or multiple axes. Array will be aligned so that the desired set of axes are in the keys, which may require a transpose/reshape. Parameters ---------- func : function Function to apply, should return boolean axis : tuple or int, optional, default=(0,) Axis or multiple axes to filter along. Returns ------- BoltArrayLocal """ axes = sorted(tupleize(axis)) reshaped = self._align(axes) filtered = asarray(list(filter(func, reshaped))) return self._constructor(filtered)
Apply a function across an axis.
def map(self, func, axis=(0,)): """ Apply a function across an axis. Array will be aligned so that the desired set of axes are in the keys, which may require a transpose/reshape. Parameters ---------- func : function Function of a single array to apply axis : tuple or int, optional, default=(0,) Axis or multiple axes to apply function along. Returns ------- BoltArrayLocal """ axes = sorted(tupleize(axis)) key_shape = [self.shape[axis] for axis in axes] reshaped = self._align(axes, key_shape=key_shape) mapped = asarray(list(map(func, reshaped))) elem_shape = mapped[0].shape # invert the previous reshape operation, using the shape of the map result linearized_shape_inv = key_shape + list(elem_shape) reordered = mapped.reshape(*linearized_shape_inv) return self._constructor(reordered)
Reduce an array along an axis.
def reduce(self, func, axis=0): """ Reduce an array along an axis. Applies an associative/commutative function of two arguments cumulatively to all arrays along an axis. Array will be aligned so that the desired set of axes are in the keys, which may require a transpose/reshape. Parameters ---------- func : function Function of two arrays that returns a single array axis : tuple or int, optional, default=(0,) Axis or multiple axes to reduce along. Returns ------- BoltArrayLocal """ axes = sorted(tupleize(axis)) # if the function is a ufunc, it can automatically handle reducing over multiple axes if isinstance(func, ufunc): inshape(self.shape, axes) reduced = func.reduce(self, axis=tuple(axes)) else: reshaped = self._align(axes) reduced = reduce(func, reshaped) new_array = self._constructor(reduced) # ensure that the shape of the reduced array is valid expected_shape = [self.shape[i] for i in range(len(self.shape)) if i not in axes] if new_array.shape != tuple(expected_shape): raise ValueError("reduce did not yield a BoltArray with valid dimensions") return new_array